Skip to content

Commit

Permalink
Merge pull request #595 from vojtechtrefny/master_btrfs-as-fs
Browse files Browse the repository at this point in the history
Btrfs support in filesystem plugin
  • Loading branch information
vojtechtrefny authored Dec 10, 2020
2 parents 4b9ed6a + 073f8ce commit 0bc3006
Show file tree
Hide file tree
Showing 12 changed files with 1,235 additions and 7 deletions.
13 changes: 13 additions & 0 deletions docs/libblockdev-sections.txt
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,19 @@ bd_fs_exfat_repair
bd_fs_exfat_set_label
bd_fs_exfat_check_label
bd_fs_exfat_wipe
BDFSBtrfsInfo
bd_fs_btrfs_get_info
bd_fs_btrfs_info_copy
bd_fs_btrfs_info_free
bd_fs_btrfs_mkfs
bd_fs_btrfs_wipe
bd_fs_btrfs_check
bd_fs_btrfs_repair
bd_fs_btrfs_set_label
bd_fs_btrfs_check_label
bd_fs_btrfs_set_uuid
bd_fs_btrfs_check_uuid
bd_fs_btrfs_resize
</SECTION>

<SECTION>
Expand Down
209 changes: 209 additions & 0 deletions src/lib/plugin_apis/fs.api
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ typedef enum {
BD_FS_TECH_REISERFS,
BD_FS_TECH_NILFS2,
BD_FS_TECH_EXFAT,
BD_FS_TECH_BTRFS,
} BDFSTech;

typedef enum {
Expand Down Expand Up @@ -730,6 +731,67 @@ GType bd_fs_exfat_info_get_type () {
return type;
}

/**
* BDFSBtrfsInfo:
* @label: label of the filesystem
* @uuid: uuid of the filesystem
* @size: size of the filesystem in bytes
* @free_space: free space on the filesystem in bytes
*/
typedef struct BDFSBtrfsInfo {
gchar *label;
gchar *uuid;
guint64 size;
guint64 free_space;
} BDFSBtrfsInfo;

/**
* bd_fs_btrfs_info_copy: (skip)
* @data: (allow-none): %BDFSBtrfsInfo to copy
*
* Creates a new copy of @data.
*/
BDFSBtrfsInfo* bd_fs_btrfs_info_copy (BDFSBtrfsInfo *data) {
if (data == NULL)
return NULL;

BDFSBtrfsInfo *ret = g_new0 (BDFSBtrfsInfo, 1);

ret->label = g_strdup (data->label);
ret->uuid = g_strdup (data->uuid);
ret->size = data->size;
ret->free_space = data->free_space;

return ret;
}

/**
* bd_fs_btrfs_info_free: (skip)
* @data: (allow-none): %BDFSBtrfsInfo to free
*
* Frees @data.
*/
void bd_fs_btrfs_info_free (BDFSBtrfsInfo *data) {
if (data == NULL)
return;

g_free (data->label);
g_free (data->uuid);
g_free (data);
}

GType bd_fs_btrfs_info_get_type () {
static GType type = 0;

if (G_UNLIKELY(type == 0)) {
type = g_boxed_type_register_static("BDFSBtrfsInfo",
(GBoxedCopyFunc) bd_fs_btrfs_info_copy,
(GBoxedFreeFunc) bd_fs_btrfs_info_free);
}

return type;
}

/**
* bd_fs_is_tech_avail:
* @tech: the queried tech
Expand Down Expand Up @@ -2315,4 +2377,151 @@ gboolean bd_fs_exfat_check_label (const gchar *label, GError **error);
*/
BDFSExfatInfo* bd_fs_exfat_get_info (const gchar *device, GError **error);

/**
* bd_fs_btrfs_mkfs:
* @device: the device to create a new btrfs fs on
* @extra: (allow-none) (array zero-terminated=1): extra options for the creation (right now
* passed to the 'mkfs.btrfs' utility)
* @error: (out): place to store error (if any)
*
* Returns: whether a new btrfs fs was successfully created on @device or not
*
* Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_MKFS
*
*/
gboolean bd_fs_btrfs_mkfs (const gchar *device, const BDExtraArg **extra, GError **error);

/**
* bd_fs_btrfs_wipe:
* @device: the device to wipe a Btrfs signature from
* @error: (out): place to store error (if any)
*
* Returns: whether the Btrfs signature was successfully wiped from the @device or
* not
*
* Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_WIPE
*/
gboolean bd_fs_btrfs_wipe (const gchar *device, GError **error);

/**
* bd_fs_btrfs_check:
* @device: the device containing the file system to check
* @extra: (allow-none) (array zero-terminated=1): extra options for the check (right now
* passed to the 'btrfsck' utility)
* @error: (out): place to store error (if any)
*
* Returns: whether the filesystem was successfully checked or not
*
* Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_CHECK
*/
gboolean bd_fs_btrfs_check (const gchar *device, const BDExtraArg **extra, GError **error);

/**
* bd_fs_btrfs_repair:
* @device: the device containing the file system to repair
* @extra: (allow-none) (array zero-terminated=1): extra options for the repair (right now
* passed to the 'btrfs' utility)
* @error: (out): place to store error (if any)
*
* Returns: whether the filesystem was successfully checked and repaired or not
*
* Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_REPAIR
*/
gboolean bd_fs_btrfs_repair (const gchar *device, const BDExtraArg **extra, GError **error);

/**
* bd_fs_btrfs_set_label:
* @mpoint: the mount point of the file system to resize
* @label: label to set
* @error: (out): place to store error (if any)
*
* Returns: whether the label of Btrfs file system on the @mpoint was
* successfully set or not
*
* Note: This function is intended to be used for btrfs filesystem on a single device,
* for more complicated setups use the btrfs plugin instead.
*
* Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_SET_LABEL
*/
gboolean bd_fs_btrfs_set_label (const gchar *mpoint, const gchar *label, GError **error);

/**
* bd_fs_btrfs_check_label:
* @label: label to check
* @error: (out) (allow-none): place to store error
*
* Returns: whether @label is a valid label for the Btrfs file system or not
* (reason is provided in @error)
*
* Note: This function is intended to be used for btrfs filesystem on a single device,
* for more complicated setups use the btrfs plugin instead.
*
* Tech category: always available
*/
gboolean bd_fs_btrfs_check_label (const gchar *label, GError **error);

/**
* bd_fs_btrfs_set_uuid:
* @device: the device containing the file system to set the UUID (serial number) for
* @uuid: (allow-none): UUID to set or %NULL to generate a new one
* @error: (out): place to store error (if any)
*
* Returns: whether the UUID of the Btrfs file system on the @device was
* successfully set or not
*
* Note: This function is intended to be used for btrfs filesystem on a single device,
* for more complicated setups use the btrfs plugin instead.
*
* Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_SET_UUID
*/
gboolean bd_fs_btrfs_set_uuid (const gchar *device, const gchar *uuid, GError **error);

/**
* bd_fs_btrfs_check_uuid:
* @uuid: UUID to check
* @error: (out) (allow-none): place to store error
*
* Returns: whether @uuid is a valid UUID for the Btrfs file system or not
* (reason is provided in @error)
*
* Note: This function is intended to be used for btrfs filesystem on a single device,
* for more complicated setups use the btrfs plugin instead.
*
* Tech category: always available
*/
gboolean bd_fs_btrfs_check_uuid (const gchar *uuid, GError **error);

/**
* bd_fs_btrfs_get_info:
* @mpoint: a mountpoint of the btrfs filesystem to get information about
* @error: (out): place to store error (if any)
*
* Returns: (transfer full): information about the file system on @device or
* %NULL in case of error
*
* Note: This function WON'T WORK for multi device btrfs filesystems,
* for more complicated setups use the btrfs plugin instead.
*
* Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_QUERY
*/
BDFSBtrfsInfo* bd_fs_btrfs_get_info (const gchar *mpoint, GError **error);

/**
* bd_fs_btrfs_resize:
* @mpoint: a mountpoint of the to be resized btrfs filesystem
* @new_size: requested new size
* @extra: (allow-none) (array zero-terminated=1): extra options for the volume resize (right now
* passed to the 'btrfs' utility)
* @error: (out): place to store error (if any)
*
* Returns: whether the @mpoint filesystem was successfully resized to @new_size
* or not
*
* Note: This function WON'T WORK for multi device btrfs filesystems,
* for more complicated setups use the btrfs plugin instead.
*
* Tech category: %BD_BTRFS_TECH_FS-%BD_BTRFS_TECH_MODE_MODIFY
*/
gboolean bd_fs_btrfs_resize (const gchar *mpoint, guint64 new_size, const BDExtraArg **extra, GError **error);

#endif /* BD_FS_API */
14 changes: 14 additions & 0 deletions src/plugins/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern gboolean bd_fs_f2fs_is_tech_avail (BDFSTech tech, guint64 mode, GError **
extern gboolean bd_fs_reiserfs_is_tech_avail (BDFSTech tech, guint64 mode, GError **error);
extern gboolean bd_fs_nilfs2_is_tech_avail (BDFSTech tech, guint64 mode, GError **error);
extern gboolean bd_fs_exfat_is_tech_avail (BDFSTech tech, guint64 mode, GError **error);
extern gboolean bd_fs_btrfs_is_tech_avail (BDFSTech tech, guint64 mode, GError **error);

/**
* bd_fs_error_quark: (skip)
Expand Down Expand Up @@ -142,6 +143,17 @@ gboolean bd_fs_check_deps (void) {
g_clear_error (&error);
}

ret = ret && bd_fs_btrfs_is_tech_avail (BD_FS_TECH_BTRFS,
BD_FS_TECH_MODE_MKFS | BD_FS_TECH_MODE_WIPE |
BD_FS_TECH_MODE_CHECK | BD_FS_TECH_MODE_REPAIR |
BD_FS_TECH_MODE_SET_LABEL | BD_FS_TECH_MODE_QUERY |
BD_FS_TECH_MODE_RESIZE | BD_FS_TECH_MODE_SET_UUID,
&error);
if (!ret && error) {
bd_utils_log_format (BD_UTILS_LOG_WARNING, "%s", error->message);
g_clear_error (&error);
}

return ret;
}

Expand Down Expand Up @@ -206,6 +218,8 @@ gboolean bd_fs_is_tech_avail (BDFSTech tech, guint64 mode, GError **error) {
return bd_fs_nilfs2_is_tech_avail (tech, mode, error);
case BD_FS_TECH_EXFAT:
return bd_fs_exfat_is_tech_avail (tech, mode, error);
case BD_FS_TECH_BTRFS:
return bd_fs_btrfs_is_tech_avail (tech, mode, error);
/* coverity[dead_error_begin] */
default:
/* this should never be reached (see the comparison with LAST_FS
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef enum {

/* XXX: where the file systems start at the enum of technologies */
#define BD_FS_OFFSET 2
#define BD_FS_LAST_FS 11
#define BD_FS_LAST_FS 13
typedef enum {
BD_FS_TECH_GENERIC = 0,
BD_FS_TECH_MOUNT = 1,
Expand All @@ -35,7 +35,8 @@ typedef enum {
BD_FS_TECH_F2FS = 8,
BD_FS_TECH_REISERFS = 9,
BD_FS_TECH_NILFS2 = 10,
BD_FS_TECH_EXFAT = 11
BD_FS_TECH_EXFAT = 11,
BD_FS_TECH_BTRFS = 12
} BDFSTech;

/* XXX: number of the highest bit of all modes */
Expand Down Expand Up @@ -79,3 +80,4 @@ gboolean bd_fs_is_tech_avail (BDFSTech tech, guint64 mode, GError **error);
#include "fs/reiserfs.h"
#include "fs/nilfs.h"
#include "fs/exfat.h"
#include "fs/btrfs.h"
6 changes: 4 additions & 2 deletions src/plugins/fs/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ libbd_fs_la_SOURCES = ../check_deps.c ../check_deps.h \
f2fs.c f2fs.h \
reiserfs.c reiserfs.h \
nilfs.c nilfs.h \
exfat.c exfat.h
exfat.c exfat.h \
btrfs.c btrfs.h

libincludefsdir = $(includedir)/blockdev/fs/
libincludefs_HEADERS = ext.h \
Expand All @@ -30,4 +31,5 @@ libincludefs_HEADERS = ext.h \
f2fs.h \
reiserfs.h \
nilfs.h \
exfat.h
exfat.h \
btrfs.h
Loading

0 comments on commit 0bc3006

Please sign in to comment.