]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
btrfs-util: add helper that abstracts "might be btrfs subvol?" check
authorLennart Poettering <lennart@poettering.net>
Fri, 26 Feb 2021 16:39:55 +0000 (17:39 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 2 Mar 2021 04:11:37 +0000 (13:11 +0900)
Let#s not hardcode inode nr 256 everywhere, but abstract this check
slightly.

src/basic/btrfs-util.c
src/basic/btrfs-util.h
src/basic/rm-rf.c
src/import/export-tar.c
src/shared/discover-image.c

index fa435739ba4f36b3f3e2dd12d96e1deeacb30a93..0fb8dfedebd878033fdc76630323f62a7ae71aa9 100644 (file)
@@ -91,7 +91,7 @@ int btrfs_is_subvol_fd(int fd) {
         if (fstat(fd, &st) < 0)
                 return -errno;
 
-        if (!S_ISDIR(st.st_mode) || st.st_ino != 256)
+        if (!btrfs_might_be_subvol(&st))
                 return 0;
 
         return btrfs_is_filesystem(fd);
@@ -194,7 +194,7 @@ int btrfs_subvol_set_read_only_fd(int fd, bool b) {
         if (fstat(fd, &st) < 0)
                 return -errno;
 
-        if (!S_ISDIR(st.st_mode) || st.st_ino != 256)
+        if (!btrfs_might_be_subvol(&st))
                 return -EINVAL;
 
         if (ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags) < 0)
@@ -229,7 +229,7 @@ int btrfs_subvol_get_read_only_fd(int fd) {
         if (fstat(fd, &st) < 0)
                 return -errno;
 
-        if (!S_ISDIR(st.st_mode) || st.st_ino != 256)
+        if (!btrfs_might_be_subvol(&st))
                 return -EINVAL;
 
         if (ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags) < 0)
index c8b44f6162718da606465cfc0423b8add709c236..0f569b6f5046ea2685d0ae371d30d8f5072c051c 100644 (file)
@@ -127,3 +127,13 @@ static inline int btrfs_log_dev_root(int level, int ret, const char *p) {
                               "File system behind %s is reported by btrfs to be backed by pseudo-device /dev/root, which is not a valid userspace accessible device node. "
                               "Cannot determine correct backing block device.", p);
 }
+
+static inline bool btrfs_might_be_subvol(const struct stat *st) {
+        if (!st)
+                return false;
+
+        /* Returns true if this 'struct stat' looks like it could refer to a btrfs subvolume. To make a final
+         * decision, needs to be combined with an fstatfs() check to see if this is actually btrfs. */
+
+        return S_ISDIR(st->st_mode) && st->st_ino == 256;
+}
index b0d682f764f3fdb2c2efc807db01f659fb09eea2..4c39ce829042006d9961cb778ac56ee4167208bc 100644 (file)
@@ -147,7 +147,7 @@ int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
                         if (r > 0)
                                 continue;
 
-                        if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) {
+                        if ((flags & REMOVE_SUBVOLUME) && btrfs_might_be_subvol(&st)) {
 
                                 /* This could be a subvolume, try to remove it */
 
index b8b650f01e1d88605fb5c4bb1df45451494d3e1c..1e6b2c12683f594de9a24e7e76a109d556a4f0a3 100644 (file)
@@ -283,7 +283,7 @@ int tar_export_start(TarExport *e, const char *path, int fd, ImportCompressType
 
         e->quota_referenced = (uint64_t) -1;
 
-        if (e->st.st_ino == 256) { /* might be a btrfs subvolume? */
+        if (btrfs_might_be_subvol(&e->st)) {
                 BtrfsQuotaInfo q;
 
                 r = btrfs_subvol_get_subtree_quota_fd(sfd, 0, &q);
index fb59d1d704a5628ae56ce620fa173614ad3e4f38..79c4c70a41cf88028074f3d722e08f803f70ccd2 100644 (file)
@@ -260,8 +260,7 @@ static int image_make(
                 if (fd < 0)
                         return -errno;
 
-                /* btrfs subvolumes have inode 256 */
-                if (st->st_ino == 256) {
+                if (btrfs_might_be_subvol(st)) {
 
                         r = btrfs_is_filesystem(fd);
                         if (r < 0)