Add Btrfs snapshot support for atomic sync and recovery consistency

This commit is contained in:
Andrea Mazzoleni 2026-04-21 15:19:25 +02:00
parent d2c1029017
commit cffd1ca15c
18 changed files with 1094 additions and 39 deletions

View file

@ -753,11 +753,15 @@ struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev
disk = malloc_nofail(sizeof(struct snapraid_disk));
pathcpy(disk->name, sizeof(disk->name), name);
pathimport(disk->dir, sizeof(disk->dir), dir);
pathimport(disk->mount_point, sizeof(disk->mount_point), dir);
pathcpy(disk->uuid, sizeof(disk->uuid), uuid);
/* ensure that the dir terminate with "/" if it isn't empty */
pathslash(disk->dir, sizeof(disk->dir));
pathslash(disk->mount_point, sizeof(disk->mount_point));
/* no snapshot by default */
disk->snapshot_root[0] = 0;
pathcpy(disk->dir, sizeof(disk->dir), disk->mount_point);
#if HAVE_THREAD
disk->single_thread = 0;
@ -1578,7 +1582,7 @@ const char* fmt_term(const struct snapraid_disk* disk, const char* str)
out[2] = str;
return esc_shell_multi(out, 3, esc_buf());
case FMT_PATH :
out[0] = disk->dir;
out[0] = disk->mount_point;
out[1] = str;
return esc_shell_multi(out, 2, esc_buf());
}

View file

@ -358,7 +358,9 @@ struct snapraid_extra {
*/
struct snapraid_disk {
char name[PATH_MAX]; /**< Name of the disk. */
char dir[PATH_MAX]; /**< Mount point of the disk. It always terminates with /. */
char mount_point[PATH_MAX]; /**< Configured mount point of the disk. It always terminates with /. */
char dir[PATH_MAX]; /**< Effective mount point of the disk. It could be either the real mount point or a snapshot. It always terminates with /. */
char snapshot_root[PATH_MAX]; /**< Subvolume root directory. It always terminates with /. Empty if not supported. */
char smartctl[PATH_MAX]; /**< Custom command for smartctl. Empty means auto. */
int smartignore[SMART_IGNORE_MAX]; /**< Smart attributes to ignore for this device. */
char uuid[UUID_MAX]; /**< UUID of the disk. They are probed during the config reading. */

View file

@ -2038,6 +2038,52 @@ int fsinfo(const char* path, int* has_persistent_inode, int* has_syncronized_har
return 0;
}
int fssnapshot(const char* path, char* root, size_t root_size)
{
(void)path;
(void)root;
(void)root_size;
return -1;
}
int fssnapshot_create(const char* source, const char* parent_dir, const char* name)
{
(void)source;
(void)parent_dir;
(void)name;
return -1;
}
int fssnapshot_readonly(const char* parent_dir, const char* name)
{
(void)parent_dir;
(void)name;
return -1;
}
int fssnapshot_delete(const char* parent_dir, const char* name)
{
(void)parent_dir;
(void)name;
return -1;
}
int fssnapshot_rename(const char* parent_dir, const char* old_name, const char* new_name)
{
(void)parent_dir;
(void)old_name;
(void)new_name;
return -1;
}
int fssnapshot_clone(const char* source, const char* dest)
{
(void)source;
(void)dest;
return -1;
}
/* ensure to call the real C strerror() */
#undef strerror

View file

@ -111,6 +111,10 @@
#include <linux/fs.h>
#endif
#if HAVE_LINUX_BTRFS_H
#include <linux/btrfs.h>
#endif
#if HAVE_LINUX_FIEMAP_H
#include <linux/fiemap.h>
#endif
@ -358,6 +362,51 @@ int filephy(const char* path, uint64_t size, uint64_t* physical);
*/
int fsinfo(const char* path, int* has_persistent_inode, int* has_syncronized_hardlinks, uint64_t* total_space, uint64_t* free_space, char* fstype, size_t fstype_size, char* fslabel, size_t fslabel_size);
/**
* Get the subvolume directory if the underline filesystem support it
* @param path Full path of the mount point to check. It must end with /
* @param root The full path of the subvolume root that contains the specified path. It will end with /.
*/
int fssnapshot(const char* path, char* root, size_t root_size);
/**
* Creates a snapshot of a subvolume.
* @param source Path to the source subvolume.
* @param parent_dir Directory where the snapshot should be created.
* @param name Name of snapshot.
* @return 0 on success, -1 on failure.
*/
int fssnapshot_create(const char* source, const char* parent_dir, const char* name);
/**
* Make a snapshot read-only
* @param parent_dir Directory where the snapshot is.
* @param name Name of snapshot.
* @return 0 on success, -1 on failure.
*/
int fssnapshot_readonly(const char* parent_dir, const char* name);
/**
* Deletes a subvolume/snapshot.
* @param parent_dir Directory where the snapshot is.
* @param name Name of snapshot.
* @return 0 on success, -1 on failure.
*/
int fssnapshot_delete(const char* parent_dir, const char* name);
/**
* Renames a subvolume.
* @return 0 on success, -1 on failure.
*/
int fssnapshot_rename(const char* parent_dir, const char* old_name, const char* new_name);
/**
* Clone a file between snapshots
* @return 0 on success, -1 on failure.
*/
int fssnapshot_clone(const char* source, const char* dest);
/**
* Log file.
*

View file

@ -1006,7 +1006,7 @@ static void scan_file(struct snapraid_scan* scan, int is_diff, const char* sub,
if (is_original_file_size_different_than_zero && st->st_size == 0) {
if (!state->opt.force_zero) {
/* LCOV_EXCL_START */
log_error(ESOFT, "The file '%s%s' has unexpected zero size!\n", disk->dir, sub);
log_error(ESOFT, "The file '%s%s' has unexpected zero size!\n", disk->mount_point, sub);
log_error(ESOFT, "It's possible that after a kernel crash this file was lost,\n");
log_error(ESOFT, "and you can use 'snapraid fix -f /%s' to recover it.\n", fmt_poll(disk, sub));
if (!is_diff) {
@ -1815,7 +1815,7 @@ static int state_diffscan(struct snapraid_state* state, int is_diff)
/* if verbose, print the list of duplicates real offsets */
/* other cases are for offsets not supported, so we don't need to report them file by file */
if (phy_last >= FILEPHY_REAL_OFFSET) {
log_error(ESOFT, "WARNING! Files '%s%s' and '%s%s' share the same physical offset %" PRId64 ".\n", disk->dir, phy_file_last->sub, disk->dir, file->sub, phy_last);
log_error(ESOFT, "WARNING! Files '%s%s' and '%s%s' share the same physical offset %" PRId64 ".\n", disk->mount_point, phy_file_last->sub, disk->mount_point, file->sub, phy_last);
}
++phy_dup;
}
@ -1885,9 +1885,9 @@ static int state_diffscan(struct snapraid_state* state, int is_diff)
) {
if (!done) {
done = 1;
log_error(ESOFT, "WARNING! All the files previously present in disk '%s' at dir '%s'", disk->name, disk->dir);
log_error(ESOFT, "WARNING! All the files previously present in disk '%s' at dir '%s'", disk->name, disk->mount_point);
} else {
log_error(ESOFT, ", disk '%s' at dir '%s'", disk->name, disk->dir);
log_error(ESOFT, ", disk '%s' at dir '%s'", disk->name, disk->mount_point);
}
/* detect the special condition of all files missing */

View file

@ -1835,6 +1835,11 @@ int snapraid_main(int argc, char* argv[])
if (opt.gui_touch_before)
state_touch(&state);
ret = state_snapshot_new(&state);
if (ret != 0) {
exit(EXIT_FAILURE);
}
state_scan(&state);
if (opt.gui_threshold_removes != 0 && state.removed_files >= opt.gui_threshold_removes) {
@ -1884,6 +1889,10 @@ int snapraid_main(int argc, char* argv[])
sleep(2);
ret = state_sync(&state, blockstart, blockcount);
/* commit the snapshot as stable */
if (ret == 0)
state_snapshot_commit(&state);
} else if (operation == OPERATION_DRY) {
state_read(&state);
@ -1929,6 +1938,8 @@ int snapraid_main(int argc, char* argv[])
/* intercept signals while operating */
signal_init();
state_snapshot_read(&state);
ret = state_scrub(&state, plan100, olderthan);
} else if (operation == OPERATION_REWRITE) {
state_read(&state);
@ -2045,6 +2056,8 @@ int snapraid_main(int argc, char* argv[])
/* intercept signals while operating */
signal_init();
state_snapshot_write(&state, &filterlist_disk);
if (operation == OPERATION_CHECK) {
ret = state_check(&state, 0, blockstart, blockcount);
} else { /* it's fix */

View file

@ -125,6 +125,7 @@ void state_init(struct snapraid_state* state)
memset(&state->opt, 0, sizeof(state->opt));
state->mapped_device = 0;
state->snapshot = 0;
state->filter_hidden = 0;
state->autosave = 0;
state->need_write = 0;
@ -294,7 +295,7 @@ static void state_config_check(struct snapraid_state* state, const char* path, t
log_fatal(EUSER, "DANGER! Ignoring that disks '%s' and '%s' are on the same device\n", disk->name, other->name);
} else {
/* LCOV_EXCL_START */
log_fatal(ESOFT, "Disks '%s' and '%s' are on the same device.\n", disk->dir, other->dir);
log_fatal(ESOFT, "Disks '%s' and '%s' are on the same device.\n", disk->mount_point, other->mount_point);
#ifdef _WIN32
log_fatal(ESOFT, "Both have the serial number '%" PRIx64 "'.\n", disk->device);
log_fatal(ESOFT, "Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
@ -321,10 +322,10 @@ static void state_config_check(struct snapraid_state* state, const char* path, t
/* and we DON'T mark the disk to be skipped */
/* because we want to use these disks */
if (!state->opt.no_warnings)
log_fatal(EUSER, "DANGER! Ignoring that disks '%s' and %s '%s' are on the same device\n", disk->dir, lev_name(l), state->parity[l].split_map[s].path);
log_fatal(EUSER, "DANGER! Ignoring that disks '%s' and %s '%s' are on the same device\n", disk->mount_point, lev_name(l), state->parity[l].split_map[s].path);
} else {
/* LCOV_EXCL_START */
log_fatal(ESOFT, "Disk '%s' and %s '%s' are on the same device.\n", disk->dir, lev_name(l), state->parity[l].split_map[s].path);
log_fatal(ESOFT, "Disk '%s' and %s '%s' are on the same device.\n", disk->mount_point, lev_name(l), state->parity[l].split_map[s].path);
#ifdef _WIN32
log_fatal(ESOFT, "Both have the serial number '%" PRIx64 "'.\n", disk->device);
log_fatal(ESOFT, "Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
@ -340,7 +341,7 @@ static void state_config_check(struct snapraid_state* state, const char* path, t
if (state->pool[0] != 0 && disk->device == state->pool_device) {
/* LCOV_EXCL_START */
log_fatal(ESOFT, "Disk '%s' and pool '%s' are on the same device.\n", disk->dir, state->pool);
log_fatal(ESOFT, "Disk '%s' and pool '%s' are on the same device.\n", disk->mount_point, state->pool);
#ifdef _WIN32
log_fatal(ESOFT, "Both have the serial number '%" PRIx64 "'.\n", disk->device);
log_fatal(ESOFT, "Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
@ -1323,6 +1324,8 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
state->thermal_cooldown_time = time * 60;
} else if (strcmp(tag, "nohidden") == 0) {
state->filter_hidden = 1;
} else if (strcmp(tag, "snapshot") == 0) {
state->snapshot = 1;
} else if (strcmp(tag, "exclude") == 0) {
struct snapraid_filter* filter;
@ -1477,7 +1480,7 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
log_tag("blocksize:%u\n", state->block_size);
for (i = state->disklist; i != 0; i = i->next) {
struct snapraid_disk* disk = i->data;
log_tag("data:%s:%s:%s\n", esc_tag(disk->name), esc_tag(disk->dir), disk->uuid);
log_tag("data:%s:%s:%s\n", esc_tag(disk->name), esc_tag(disk->mount_point), disk->uuid);
}
for (i = state->extralist; i != 0; i = i->next) {
struct snapraid_extra* extra = i->data;
@ -1803,10 +1806,10 @@ void state_refresh(struct snapraid_state* state)
if (disk->skip_access)
continue;
ret = fsinfo(disk->dir, 0, 0, &total_space, &free_space, disk->fstype, sizeof(disk->fstype), disk->fslabel, sizeof(disk->fslabel));
ret = fsinfo(disk->mount_point, 0, 0, &total_space, &free_space, disk->fstype, sizeof(disk->fstype), disk->fslabel, sizeof(disk->fslabel));
if (ret != 0) {
/* LCOV_EXCL_START */
log_fatal(errno, "Error accessing disk '%s' to retrieve file-system info. %s.\n", disk->dir, strerror(errno));
log_fatal(errno, "Error accessing disk '%s' to retrieve file-system info. %s.\n", disk->mount_point, strerror(errno));
exit(EXIT_FAILURE);
/* LCOV_EXCL_STOP */
}
@ -5608,3 +5611,227 @@ void state_load_ignore_file(tommy_list* filter_list, const char* path, const cha
sclose(f);
}
#define SNAPSHOT_CONTAINER ".snapraid"
#define SNAPSHOT_PENDING "pending"
#define SNAPSHOT_STABLE "stable"
int state_snapshot_new(struct snapraid_state* state)
{
if (!state->snapshot)
return 0;
for (tommy_node* i = state->disklist; i != 0; i = i->next) {
struct snapraid_disk* disk = i->data;
char root[PATH_MAX];
char container[PATH_MAX];
int ret;
/* check if it supports snapshot */
if (fssnapshot(disk->mount_point, root, sizeof(root)) != 0)
continue;
size_t root_len = strlen(root);
pathcpy(container, sizeof(container), root);
pathcat(container, sizeof(container), SNAPSHOT_CONTAINER);
/* create the snapshot container directory if not existing */
ret = mkdir(container, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
if (ret != 0 && errno != EEXIST) {
log_error(errno, "Error creating '%s'. %s.\n", container, strerror(errno));
return -1;
}
/* delete a potential previous pending snapshot */
if (fssnapshot_delete(container, SNAPSHOT_PENDING) != 0) {
log_fatal(errno, "Failed to delete pending snapshot in '%s' \n", container);
return -1;
}
/* create a new snapshot */
if (fssnapshot_create(root, container, SNAPSHOT_PENDING) != 0) {
log_fatal(errno, "Failed to create pending snapshot '%s'\n", container);
return -1;
}
msg_progress("Created disk %s pending snapshot...\n", disk->name);
/* setup the snapshot in use */
pathcpy(disk->dir, sizeof(disk->dir), container);
pathcat(disk->dir, sizeof(disk->dir), "/" SNAPSHOT_PENDING "/");
pathcat(disk->dir, sizeof(disk->dir), disk->mount_point + root_len);
/* store the snapshot root */
pathcpy(disk->snapshot_root, sizeof(disk->snapshot_root), root);
}
return 0;
}
int state_snapshot_commit(struct snapraid_state* state)
{
if (!state->snapshot)
return 0;
for (tommy_node* i = state->disklist; i != 0; i = i->next) {
struct snapraid_disk* disk = i->data;
char container[PATH_MAX];
/* check if it supports snapshot */
if (disk->snapshot_root[0] == 0)
continue;
size_t root_len = strlen(disk->snapshot_root);
pathcpy(container, sizeof(container), disk->snapshot_root);
pathcat(container, sizeof(container), SNAPSHOT_CONTAINER);
/* for each content file */
for (tommy_node* j = state->contentlist; j != 0; j = j->next) {
struct snapraid_content* content = j->data;
/* check if the content file is in the subvolume */
if (strncmp(content->content, disk->snapshot_root, root_len) == 0) {
/* clone the just written content file into the snapshot */
char clone[PATH_MAX];
pathcpy(clone, sizeof(clone), container);
pathcat(clone, sizeof(clone), "/" SNAPSHOT_PENDING "/");
pathcat(clone, sizeof(clone), content->content + root_len);
if (fssnapshot_clone(content->content, clone) != 0) {
log_fatal(errno, "Failed to clone the content file '%s'\n", content->content);
return -1;
}
}
}
if (fssnapshot_readonly(container, SNAPSHOT_PENDING) != 0) {
log_fatal(errno, "Failed to make pending snapshot read-only in '%s'\n", container);
return -1;
}
/* delete a potential previous stable snapshot */
if (fssnapshot_delete(container, SNAPSHOT_STABLE) != 0) {
log_fatal(errno, "Failed to delete stable snapshot in '%s'\n", container);
return -1;
}
/* rename pending to stable */
if (fssnapshot_rename(container, SNAPSHOT_PENDING, SNAPSHOT_STABLE) != 0) {
log_fatal(errno, "Failed to rename snapshot in '%s'\n", container);
return -1;
}
msg_progress("Committed disk %s stable snapshot...\n", disk->name);
}
return 0;
}
static int is_dir(const char* path)
{
struct stat st;
if (lstat(path, &st) != 0)
return 0;
return S_ISDIR(st.st_mode);
}
void state_snapshot_read(struct snapraid_state* state)
{
if (!state->snapshot)
return;
for (tommy_node* i = state->disklist; i != 0; i = i->next) {
struct snapraid_disk* disk = i->data;
char root[PATH_MAX];
char container[PATH_MAX];
char vol[PATH_MAX];
/* check if it supports snapshot */
if (fssnapshot(disk->mount_point, root, sizeof(root)) != 0)
continue;
size_t root_len = strlen(root);
pathcpy(container, sizeof(container), root);
pathcat(container, sizeof(container), SNAPSHOT_CONTAINER);
pathcpy(vol, sizeof(vol), container);
pathcat(vol, sizeof(vol), "/" SNAPSHOT_PENDING);
if (is_dir(vol)) {
msg_progress("Using disk %s pending snapshot...\n", disk->name);
pathcpy(disk->dir, sizeof(disk->dir), vol);
pathcat(disk->dir, sizeof(disk->dir), "/");
pathcat(disk->dir, sizeof(disk->dir), disk->mount_point + root_len);
} else {
pathcpy(vol, sizeof(vol), container);
pathcat(vol, sizeof(vol), "/" SNAPSHOT_STABLE);
if (is_dir(vol)) {
msg_progress("Using disk %s stable snapshot...\n", disk->name);
pathcpy(disk->dir, sizeof(disk->dir), vol);
pathcat(disk->dir, sizeof(disk->dir), "/");
pathcat(disk->dir, sizeof(disk->dir), disk->mount_point + root_len);
} else {
msg_progress("Using disk %s live filesystem...\n", disk->name);
}
}
}
}
void state_snapshot_write(struct snapraid_state* state, tommy_list* filterlist_disk)
{
if (!state->snapshot)
return;
/* if the filter disk is empty, all disks are potentially written */
if (tommy_list_empty(filterlist_disk)) {
msg_progress("Using live filesystems...\n");
return;
}
for (tommy_node* i = state->disklist; i != 0; i = i->next) {
struct snapraid_disk* disk = i->data;
char root[PATH_MAX];
char container[PATH_MAX];
char vol[PATH_MAX];
/* check if it supports snapshot */
if (fssnapshot(disk->mount_point, root, sizeof(root)) != 0)
continue;
size_t root_len = strlen(root);
/* if the disk is filtered in, it could be written, then we cannot use the snapshot */
if (filter_path(filterlist_disk, 0, disk->name, 0) == 0) {
msg_progress("Using disk %s live filesystem...\n", disk->name);
continue;
}
pathcpy(container, sizeof(container), root);
pathcat(container, sizeof(container), SNAPSHOT_CONTAINER);
pathcpy(vol, sizeof(vol), container);
pathcat(vol, sizeof(vol), "/" SNAPSHOT_PENDING);
if (is_dir(vol)) {
msg_progress("Using disk %s pending snapshot...\n", disk->name);
pathcpy(disk->dir, sizeof(disk->dir), vol);
pathcat(disk->dir, sizeof(disk->dir), "/");
pathcat(disk->dir, sizeof(disk->dir), disk->mount_point + root_len);
} else {
pathcpy(vol, sizeof(vol), container);
pathcat(vol, sizeof(vol), "/" SNAPSHOT_STABLE);
if (is_dir(vol)) {
msg_progress("Using disk %s stable snapshot...\n", disk->name);
pathcpy(disk->dir, sizeof(disk->dir), vol);
pathcat(disk->dir, sizeof(disk->dir), "/");
pathcat(disk->dir, sizeof(disk->dir), disk->mount_point + root_len);
} else {
msg_progress("Using disk %s live filesystem...\n", disk->name);
}
}
}
}

View file

@ -112,6 +112,7 @@ struct snapraid_option {
struct snapraid_state {
struct snapraid_option opt; /**< Setup options. */
int mapped_device; /**< Devices were already mapped */
int snapshot; /**< Enable snapshot support */
int filter_hidden; /**< Filter out hidden files. */
uint64_t autosave; /**< Autosave after the specified amount of data. 0 to disable. */
int need_write; /**< If the state is changed. */
@ -420,6 +421,26 @@ void state_load_ignore_file(tommy_list* filter_list, const char* path, const cha
*/
void state_attr(struct snapraid_state* state, tommy_list* low);
/**
* Create (or reuse existing) pending snapshots
*/
int state_snapshot_new(struct snapraid_state* state);
/**
* Make the pending snapshots as stable
*/
int state_snapshot_commit(struct snapraid_state* state);
/**
* Use the snapshots for reading only commands
*/
void state_snapshot_read(struct snapraid_state* state);
/**
* Use the snapshots for the reading part (filtered out disks) of writing commands
*/
void state_snapshot_write(struct snapraid_state* state, tommy_list* filterlist_disk);
/****************************************************************************/
/* misc */

View file

@ -1165,6 +1165,25 @@ void pathcut(char* dst)
dst[0] = 0;
}
void pathup(char* dst)
{
size_t len = strlen(dst);
if (len == 0)
return;
/* if ending with slash, remove it */
if (dst[len - 1] == '/')
--len;
/* search previous / */
while (len > 0 && dst[len - 1] != '/')
--len;
/* cut everything after the slash (if there are no more slash, return the empty string) */
dst[len] = 0;
}
int pathcmp(const char* a, const char* b)
{
#ifdef _WIN32

View file

@ -360,6 +360,13 @@ void pathslash(char* dst, size_t size);
*/
void pathcut(char* dst);
/**
* Given a string ending with slash, remove that ending slash and any char up to the previous slash.
*
* If there are not more slash, it returns the empty string.
*/
void pathup(char* dst);
/**
* Compare two paths.
* In Windows it's case insensitive and assumes \ equal at /.

View file

@ -131,7 +131,7 @@ void state_thermal(struct snapraid_state* state, time_t now)
entry->device = disk->device;
device_name_set(entry, disk->name, 0);
pathcpy(entry->mount, sizeof(entry->mount), disk->dir);
pathcpy(entry->mount, sizeof(entry->mount), disk->mount_point);
pathcpy(entry->smartctl, sizeof(entry->smartctl), disk->smartctl);
memcpy(entry->smartignore, disk->smartignore, sizeof(entry->smartignore));

View file

@ -1375,6 +1375,224 @@ int fsinfo(const char* path, int* has_persistent_inode, int* has_syncronized_har
return 0;
}
int fssnapshot(const char* path, char* root, size_t root_size)
{
#if HAVE_LINUX_DEVICE
struct statfs sfs;
struct stat st;
char current_path[PATH_MAX];
pathcpy(current_path, sizeof(current_path), path);
if (statfs(current_path, &sfs) != 0) {
log_error(errno, "Error stating filesystem '%s'. %s.\n", current_path, strerror(errno));
return -1;
}
/* only btrfs */
if (sfs.f_type != BTRFS_SUPER_MAGIC)
return -1;
/* walk up the directory tree to find the subvolume root (Inode 256) */
while (1) {
if (stat(current_path, &st) != 0) {
log_error(errno, "Error stating '%s'. %s.\n", current_path, strerror(errno));
return -1;
}
/* btrfs reserved inode 256 for subvolume roots */
if (st.st_ino == 256) {
/* copy the subvol root */
pathcpy(root, root_size, current_path);
return 0;
}
/* move to parent directory */
pathup(current_path);
if (current_path[0] == 0) {
log_error(ENOENT, "No subvolume root found for '%s'. %s.\n", path, strerror(errno));
return -1;
}
}
#else
(void)path;
(void)root;
(void)root_size;
return -1;
#endif
}
int fssnapshot_create(const char* source, const char* parent_dir, const char* name)
{
#if HAVE_LINUX_DEVICE
struct btrfs_ioctl_vol_args_v2 args;
int fd_source;
int fd_dest_parent;
int ret;
/* open the source subvolume to get a file descriptor */
fd_source = open(source, O_RDONLY | O_DIRECTORY);
if (fd_source < 0) {
return -1;
}
fd_dest_parent = open(parent_dir, O_RDONLY | O_DIRECTORY);
if (fd_dest_parent < 0) {
close(fd_source);
return -1;
}
memset(&args, 0, sizeof(args));
args.fd = fd_source;
args.flags = 0;
pathcpy(args.name, BTRFS_SUBVOL_NAME_MAX, name);
/* issue the snapshot command to the PARENT directory of the destination */
ret = ioctl(fd_dest_parent, BTRFS_IOC_SNAP_CREATE_V2, &args);
if (ret < 0) {
close(fd_source);
close(fd_dest_parent);
return -1;
}
close(fd_source);
close(fd_dest_parent);
return 0;
#else
(void)source;
(void)parent_dir;
(void)name;
return -1;
#endif
}
int fssnapshot_readonly(const char* parent_dir, const char* name)
{
#if HAVE_LINUX_DEVICE
char path[PATH_MAX];
pathcpy(path, sizeof(path), parent_dir);
pathcatc(path, sizeof(path), '/');
pathcat(path, sizeof(path), name);
int fd = open(path, O_RDONLY | O_DIRECTORY);
if (fd < 0)
return -1;
/* get existing flags to avoid overwriting other potential flags */
__u64 flags;
if (ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags) < 0) {
close(fd);
return -1;
}
flags |= BTRFS_SUBVOL_RDONLY;
if (ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags) < 0) {
close(fd);
return -1;
}
close(fd);
return 0;
#else
(void)parent_dir;
(void)name;
return -1;
#endif
}
int fssnapshot_delete(const char* parent_dir, const char* name)
{
#if HAVE_LINUX_DEVICE
struct btrfs_ioctl_vol_args args;
int fd_parent;
int ret;
fd_parent = open(parent_dir, O_RDONLY | O_DIRECTORY);
if (fd_parent < 0) {
return -1;
}
memset(&args, 0, sizeof(args));
pathcpy(args.name, BTRFS_PATH_NAME_MAX, name);
ret = ioctl(fd_parent, BTRFS_IOC_SNAP_DESTROY, &args);
if (ret < 0 && errno != ENOENT) {
close(fd_parent);
return -1;
}
close(fd_parent);
return 0;
#else
(void)parent_dir;
(void)name;
return -1;
#endif
}
int fssnapshot_rename(const char* parent_dir, const char* old_name, const char* new_name)
{
#if HAVE_LINUX_DEVICE
char old_path[PATH_MAX];
char new_path[PATH_MAX];
pathcpy(old_path, sizeof(old_path), parent_dir);
pathcatc(old_path, sizeof(old_path), '/');
pathcat(old_path, sizeof(old_path), old_name);
pathcpy(new_path, sizeof(new_path), parent_dir);
pathcatc(new_path, sizeof(new_path), '/');
pathcat(new_path, sizeof(new_path), new_name);
if (rename(old_path, new_path) < 0)
return -1;
return 0;
#else
(void)parent_dir;
(void)old_name;
(void)new_name;
return -1;
#endif
}
int fssnapshot_clone(const char* source, const char* dest)
{
#if HAVE_LINUX_DEVICE
int fs, fd;
fs = open(source, O_RDONLY);
if (fs < 0)
return -1;
fd = open(dest, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
close(fs);
return 1;
}
/* perform the reflink copy */
if (ioctl(fd, FICLONE, fs) < 0) {
close(fd);
close(fs);
return -1;
}
close(fs);
close(fd);
return 0;
#else
(void)source;
(void)dest;
return -1;
#endif
}
uint64_t os_tick(void)
{
#if HAVE_MACH_ABSOLUTE_TIME