btrfs-progs: replace test_issubvolume() with btrfs_util_is_subvolume()
[platform/upstream/btrfs-progs.git] / utils.c
diff --git a/utils.c b/utils.c
index 243ee1e..715bab0 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -24,6 +24,7 @@
 #include <sys/mount.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/sysinfo.h>
 #include <uuid/uuid.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -39,6 +40,8 @@
 #include <linux/magic.h>
 #include <getopt.h>
 
+#include <btrfsutil.h>
+
 #include "kerncompat.h"
 #include "radix-tree.h"
 #include "ctree.h"
@@ -297,7 +300,7 @@ static int btrfs_wipe_existing_sb(int fd)
        memset(buf, 0, len);
        ret = pwrite(fd, buf, len, offset);
        if (ret < 0) {
-               error("cannot wipe existing superblock: %s", strerror(errno));
+               error("cannot wipe existing superblock: %m");
                ret = -1;
        } else if (ret != len) {
                error("cannot wipe existing superblock: wrote %d of %zd", ret, len);
@@ -319,7 +322,7 @@ int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret,
 
        ret = fstat(fd, &st);
        if (ret < 0) {
-               error("unable to stat %s: %s", file, strerror(errno));
+               error("unable to stat %s: %m", file);
                return 1;
        }
 
@@ -446,7 +449,7 @@ int is_mount_point(const char *path)
        return ret;
 }
 
-static int is_reg_file(const char *path)
+int is_reg_file(const char *path)
 {
        struct stat statbuf;
 
@@ -455,6 +458,21 @@ static int is_reg_file(const char *path)
        return S_ISREG(statbuf.st_mode);
 }
 
+int is_path_exist(const char *path)
+{
+       struct stat statbuf;
+       int ret;
+
+       ret = stat(path, &statbuf);
+       if (ret < 0) {
+               if (errno == ENOENT)
+                       return 0;
+               else
+                       return -errno;
+       }
+       return 1;
+}
+
 /*
  * This function checks if the given input parameter is
  * an uuid or a path
@@ -518,7 +536,7 @@ int get_btrfs_mount(const char *dev, char *mp, size_t mp_size)
        fd = open(dev, O_RDONLY);
        if (fd < 0) {
                ret = -errno;
-               error("cannot open %s: %s", dev, strerror(errno));
+               error("cannot open %s: %m", dev);
                goto out;
        }
 
@@ -556,8 +574,8 @@ int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose)
                        return -1;
                }
                ret = open_file_or_dir(mp, dirstream);
-               error_on(verbose && ret < 0, "can't access '%s': %s",
-                        path, strerror(errno));
+               error_on(verbose && ret < 0, "can't access '%s': %m",
+                        path);
        } else {
                ret = btrfs_open_dir(path, dirstream, 1);
        }
@@ -568,17 +586,16 @@ int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose)
 /*
  * Do the following checks before calling open_file_or_dir():
  * 1: path is in a btrfs filesystem
- * 2: path is a directory
+ * 2: path is a directory if dir_only is 1
  */
-int btrfs_open_dir(const char *path, DIR **dirstream, int verbose)
+int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only)
 {
        struct statfs stfs;
        struct stat st;
        int ret;
 
        if (statfs(path, &stfs) != 0) {
-               error_on(verbose, "cannot access '%s': %s", path,
-                               strerror(errno));
+               error_on(verbose, "cannot access '%s': %m", path);
                return -1;
        }
 
@@ -588,25 +605,33 @@ int btrfs_open_dir(const char *path, DIR **dirstream, int verbose)
        }
 
        if (stat(path, &st) != 0) {
-               error_on(verbose, "cannot access '%s': %s", path,
-                               strerror(errno));
+               error_on(verbose, "cannot access '%s': %m", path);
                return -1;
        }
 
-       if (!S_ISDIR(st.st_mode)) {
+       if (dir_only && !S_ISDIR(st.st_mode)) {
                error_on(verbose, "not a directory: %s", path);
                return -3;
        }
 
        ret = open_file_or_dir(path, dirstream);
        if (ret < 0) {
-               error_on(verbose, "cannot access '%s': %s", path,
-                               strerror(errno));
+               error_on(verbose, "cannot access '%s': %m", path);
        }
 
        return ret;
 }
 
+int btrfs_open_dir(const char *path, DIR **dirstream, int verbose)
+{
+       return btrfs_open(path, dirstream, verbose, 1);
+}
+
+int btrfs_open_file_or_dir(const char *path, DIR **dirstream, int verbose)
+{
+       return btrfs_open(path, dirstream, verbose, 0);
+}
+
 /* checks if a device is a loop device */
 static int is_loop_device (const char* device) {
        struct stat statbuf;
@@ -793,14 +818,9 @@ static int blk_file_in_dev_list(struct btrfs_fs_devices* fs_devices,
                const char* file)
 {
        int ret;
-       struct list_head *head;
-       struct list_head *cur;
        struct btrfs_device *device;
 
-       head = &fs_devices->devices;
-       list_for_each(cur, head) {
-               device = list_entry(cur, struct btrfs_device, dev_list);
-
+       list_for_each_entry(device, &fs_devices->devices, dev_list) {
                if((ret = is_same_loop_file(device->name, file)))
                        return ret;
        }
@@ -877,8 +897,7 @@ int check_mounted(const char* file)
 
        fd = open(file, O_RDONLY);
        if (fd < 0) {
-               error("mount check: cannot open %s: %s", file,
-                               strerror(errno));
+               error("mount check: cannot open %s: %m", file);
                return -errno;
        }
 
@@ -967,16 +986,14 @@ int btrfs_register_one_device(const char *fname)
        fd = open("/dev/btrfs-control", O_RDWR);
        if (fd < 0) {
                warning(
-       "failed to open /dev/btrfs-control, skipping device registration: %s",
-                       strerror(errno));
+       "failed to open /dev/btrfs-control, skipping device registration: %m");
                return -errno;
        }
        memset(&args, 0, sizeof(args));
        strncpy_null(args.name, fname);
        ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
        if (ret < 0) {
-               error("device scan failed on '%s': %s", fname,
-                               strerror(errno));
+               error("device scan failed on '%s': %m", fname);
                ret = -errno;
        }
        close(fd);
@@ -1229,6 +1246,7 @@ static int set_label_unmounted(const char *dev, const char *label)
                return -1;
 
        trans = btrfs_start_transaction(root, 1);
+       BUG_ON(IS_ERR(trans));
        __strncpy_null(root->fs_info->super_copy->label, label, BTRFS_LABEL_SIZE - 1);
 
        btrfs_commit_transaction(trans, root);
@@ -1245,15 +1263,14 @@ static int set_label_mounted(const char *mount_path, const char *labelp)
 
        fd = open(mount_path, O_RDONLY | O_NOATIME);
        if (fd < 0) {
-               error("unable to access %s: %s", mount_path, strerror(errno));
+               error("unable to access %s: %m", mount_path);
                return -1;
        }
 
        memset(label, 0, sizeof(label));
        __strncpy_null(label, labelp, BTRFS_LABEL_SIZE - 1);
        if (ioctl(fd, BTRFS_IOC_SET_FSLABEL, label) < 0) {
-               error("unable to set label of %s: %s", mount_path,
-                               strerror(errno));
+               error("unable to set label of %s: %m", mount_path);
                close(fd);
                return -1;
        }
@@ -1301,7 +1318,7 @@ int get_label_mounted(const char *mount_path, char *labelp)
 
        fd = open(mount_path, O_RDONLY | O_NOATIME);
        if (fd < 0) {
-               error("unable to access %s: %s", mount_path, strerror(errno));
+               error("unable to access %s: %m", mount_path);
                return -1;
        }
 
@@ -1309,8 +1326,7 @@ int get_label_mounted(const char *mount_path, char *labelp)
        ret = ioctl(fd, BTRFS_IOC_GET_FSLABEL, label);
        if (ret < 0) {
                if (errno != ENOTTY)
-                       error("unable to get label of %s: %s", mount_path,
-                                       strerror(errno));
+                       error("unable to get label of %s: %m", mount_path);
                ret = -errno;
                close(fd);
                return ret;
@@ -1439,6 +1455,7 @@ u64 parse_qgroupid(const char *p)
        char *s = strchr(p, '/');
        const char *ptr_src_end = p + strlen(p);
        char *ptr_parse_end = NULL;
+       enum btrfs_util_error err;
        u64 level;
        u64 id;
        int fd;
@@ -1466,8 +1483,8 @@ u64 parse_qgroupid(const char *p)
 
 path:
        /* Path format like subv at 'my_subvol' is the fallback case */
-       ret = test_issubvolume(p);
-       if (ret < 0 || !ret)
+       err = btrfs_util_is_subvolume(p);
+       if (err)
                goto err;
        fd = open(p, O_RDONLY);
        if (fd < 0)
@@ -1527,10 +1544,16 @@ int open_file_or_dir(const char *fname, DIR **dirstream)
 
 void close_file_or_dir(int fd, DIR *dirstream)
 {
-       if (dirstream)
+       int old_errno;
+
+       old_errno = errno;
+       if (dirstream) {
                closedir(dirstream);
-       else if (fd >= 0)
+       } else if (fd >= 0) {
                close(fd);
+       }
+
+       errno = old_errno;
 }
 
 int get_device_info(int fd, u64 devid,
@@ -1648,7 +1671,7 @@ int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args,
                fd = open(path, O_RDONLY);
                if (fd < 0) {
                        ret = -errno;
-                       error("cannot open %s: %s", path, strerror(errno));
+                       error("cannot open %s: %m", path);
                        goto out;
                }
                ret = check_mounted_where(fd, path, mp, sizeof(mp),
@@ -1747,6 +1770,109 @@ out:
        return ret;
 }
 
+int get_fsid(const char *path, u8 *fsid, int silent)
+{
+       int ret;
+       int fd;
+       struct btrfs_ioctl_fs_info_args args;
+
+       fd = open(path, O_RDONLY);
+       if (fd < 0) {
+               ret = -errno;
+               if (!silent)
+                       error("failed to open %s: %s", path,
+                               strerror(-ret));
+               goto out;
+       }
+
+       ret = ioctl(fd, BTRFS_IOC_FS_INFO, &args);
+       if (ret < 0) {
+               ret = -errno;
+               goto out;
+       }
+
+       memcpy(fsid, args.fsid, BTRFS_FSID_SIZE);
+       ret = 0;
+
+out:
+       if (fd != -1)
+               close(fd);
+       return ret;
+}
+
+int is_seen_fsid(u8 *fsid, struct seen_fsid *seen_fsid_hash[])
+{
+       u8 hash = fsid[0];
+       int slot = hash % SEEN_FSID_HASH_SIZE;
+       struct seen_fsid *seen = seen_fsid_hash[slot];
+
+       while (seen) {
+               if (memcmp(seen->fsid, fsid, BTRFS_FSID_SIZE) == 0)
+                       return 1;
+
+               seen = seen->next;
+       }
+
+       return 0;
+}
+
+int add_seen_fsid(u8 *fsid, struct seen_fsid *seen_fsid_hash[],
+               int fd, DIR *dirstream)
+{
+       u8 hash = fsid[0];
+       int slot = hash % SEEN_FSID_HASH_SIZE;
+       struct seen_fsid *seen = seen_fsid_hash[slot];
+       struct seen_fsid *alloc;
+
+       if (!seen)
+               goto insert;
+
+       while (1) {
+               if (memcmp(seen->fsid, fsid, BTRFS_FSID_SIZE) == 0)
+                       return -EEXIST;
+
+               if (!seen->next)
+                       break;
+
+               seen = seen->next;
+       }
+
+insert:
+       alloc = malloc(sizeof(*alloc));
+       if (!alloc)
+               return -ENOMEM;
+
+       alloc->next = NULL;
+       memcpy(alloc->fsid, fsid, BTRFS_FSID_SIZE);
+       alloc->fd = fd;
+       alloc->dirstream = dirstream;
+
+       if (seen)
+               seen->next = alloc;
+       else
+               seen_fsid_hash[slot] = alloc;
+
+       return 0;
+}
+
+void free_seen_fsid(struct seen_fsid *seen_fsid_hash[])
+{
+       int slot;
+       struct seen_fsid *seen;
+       struct seen_fsid *next;
+
+       for (slot = 0; slot < SEEN_FSID_HASH_SIZE; slot++) {
+               seen = seen_fsid_hash[slot];
+               while (seen) {
+                       next = seen->next;
+                       close_file_or_dir(seen->fd, seen->dirstream);
+                       free(seen);
+                       seen = next;
+               }
+               seen_fsid_hash[slot] = NULL;
+       }
+}
+
 static int group_profile_devs_min(u64 flag)
 {
        switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
@@ -1865,7 +1991,7 @@ int btrfs_scan_devices(void)
 
                fd = open(path, O_RDONLY);
                if (fd < 0) {
-                       error("cannot open %s: %s", path, strerror(errno));
+                       error("cannot open %s: %m", path);
                        continue;
                }
                ret = btrfs_scan_one_device(fd, path, &tmp_devices,
@@ -2328,111 +2454,23 @@ int test_issubvolname(const char *name)
                strcmp(name, ".") && strcmp(name, "..");
 }
 
-/*
- * Test if path is a subvolume
- * Returns:
- *   0 - path exists but it is not a subvolume
- *   1 - path exists and it is  a subvolume
- * < 0 - error
- */
-int test_issubvolume(const char *path)
-{
-       struct stat     st;
-       struct statfs stfs;
-       int             res;
-
-       res = stat(path, &st);
-       if (res < 0)
-               return -errno;
-
-       if (st.st_ino != BTRFS_FIRST_FREE_OBJECTID || !S_ISDIR(st.st_mode))
-               return 0;
-
-       res = statfs(path, &stfs);
-       if (res < 0)
-               return -errno;
-
-       return (int)stfs.f_type == BTRFS_SUPER_MAGIC;
-}
-
 const char *subvol_strip_mountpoint(const char *mnt, const char *full_path)
 {
        int len = strlen(mnt);
        if (!len)
                return full_path;
 
+       if ((strncmp(mnt, full_path, len) != 0) || (full_path[len] != '/')) {
+               error("not on mount point: %s", mnt);
+               exit(1);
+       }
+
        if (mnt[len - 1] != '/')
                len += 1;
 
        return full_path + len;
 }
 
-/*
- * Returns
- * <0: Std error
- * 0: All fine
- * 1: Error; and error info printed to the terminal. Fixme.
- * 2: If the fullpath is root tree instead of subvol tree
- */
-int get_subvol_info(const char *fullpath, struct root_info *get_ri)
-{
-       u64 sv_id;
-       int ret = 1;
-       int fd = -1;
-       int mntfd = -1;
-       char *mnt = NULL;
-       const char *svpath = NULL;
-       DIR *dirstream1 = NULL;
-       DIR *dirstream2 = NULL;
-
-       ret = test_issubvolume(fullpath);
-       if (ret < 0)
-               return ret;
-       if (!ret) {
-               error("not a subvolume: %s", fullpath);
-               return 1;
-       }
-
-       ret = find_mount_root(fullpath, &mnt);
-       if (ret < 0)
-               return ret;
-       if (ret > 0) {
-               error("%s doesn't belong to btrfs mount point", fullpath);
-               return 1;
-       }
-       ret = 1;
-       svpath = subvol_strip_mountpoint(mnt, fullpath);
-
-       fd = btrfs_open_dir(fullpath, &dirstream1, 1);
-       if (fd < 0)
-               goto out;
-
-       ret = btrfs_list_get_path_rootid(fd, &sv_id);
-       if (ret)
-               goto out;
-
-       mntfd = btrfs_open_dir(mnt, &dirstream2, 1);
-       if (mntfd < 0)
-               goto out;
-
-       memset(get_ri, 0, sizeof(*get_ri));
-       get_ri->root_id = sv_id;
-
-       if (sv_id == BTRFS_FS_TREE_OBJECTID)
-               ret = btrfs_get_toplevel_subvol(mntfd, get_ri);
-       else
-               ret = btrfs_get_subvol(mntfd, get_ri);
-       if (ret)
-               error("can't find '%s': %d", svpath, ret);
-
-out:
-       close_file_or_dir(mntfd, dirstream2);
-       close_file_or_dir(fd, dirstream1);
-       free(mnt);
-
-       return ret;
-}
-
 /* Set the seed manually */
 void init_rand_seed(u64 seed)
 {
@@ -2522,3 +2560,33 @@ u8 rand_u8(void)
 void btrfs_config_init(void)
 {
 }
+
+/* Returns total size of main memory in bytes, -1UL if error. */
+unsigned long total_memory(void)
+{
+        struct sysinfo si;
+
+        if (sysinfo(&si) < 0) {
+                error("can't determine memory size");
+                return -1UL;
+        }
+        return si.totalram * si.mem_unit;       /* bytes */
+}
+
+void print_device_info(struct btrfs_device *device, char *prefix)
+{
+       if (prefix)
+               printf("%s", prefix);
+       printf("Device: id = %llu, name = %s\n",
+              device->devid, device->name);
+}
+
+void print_all_devices(struct list_head *devices)
+{
+       struct btrfs_device *dev;
+
+       printf("All Devices:\n");
+       list_for_each_entry(dev, devices, dev_list)
+               print_device_info(dev, "\t");
+       printf("\n");
+}