]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: add mnt_fs_is_{moved,attached,detached} functions
authorKarel Zak <kzak@redhat.com>
Wed, 16 Jul 2025 11:52:07 +0000 (13:52 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Aug 2025 13:03:29 +0000 (15:03 +0200)
Let's use these simple functions to report what the last operation or
status detected by the library means to the filesystem. It will be
used by the fanotify monitor as well.

Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/docs/libmount-sections.txt
libmount/src/context_umount.c
libmount/src/fs.c
libmount/src/hook_mount.c
libmount/src/hook_mount_legacy.c
libmount/src/libmount.h.in
libmount/src/libmount.sym
libmount/src/mountP.h
libmount/src/tab_parse.c

index e791415d9606c18032c0a796576018e570c73f58..95ad55fe57c5f09b15fc8f5b8fdaad67bc0a548a 100644 (file)
@@ -256,7 +256,10 @@ mnt_fs_get_userdata
 mnt_fs_get_user_options
 mnt_fs_get_vfs_options
 mnt_fs_get_vfs_options_all
+mnt_fs_is_attached
+mnt_fs_is_detached
 mnt_fs_is_kernel
+mnt_fs_is_moved
 mnt_fs_is_netfs
 mnt_fs_is_pseudofs
 mnt_fs_is_regularfs
index 27319f77cf2da118f747886c33d194dffb6755e0..8fbd662ab4894fd1ed7bebc44c83acdda2369222 100644 (file)
@@ -934,6 +934,7 @@ static int do_umount(struct libmnt_context *cxt)
                }
                cxt->syscall_status = 0;
                DBG(CXT, ul_debugobj(cxt, "read-only re-mount(2) success"));
+               mnt_fs_mark_attached(cxt->fs);
                return 0;
        }
 
@@ -944,6 +945,7 @@ static int do_umount(struct libmnt_context *cxt)
        }
 
        cxt->syscall_status = 0;
+       mnt_fs_mark_detached(cxt->fs);
        DBG(CXT, ul_debugobj(cxt, "umount(2) success"));
        return 0;
 }
index 64c7ee865c0bd844b05c19791a9185542ae30966..6147d1f534c65da69e450e1595409d109278f1f6 100644 (file)
@@ -706,6 +706,51 @@ int mnt_fs_is_kernel(struct libmnt_fs *fs)
        return mnt_fs_get_flags(fs) & MNT_FS_KERNEL ? 1 : 0;
 }
 
+/**
+ * mnt_fs_is_moved:
+ * @fs: filesystem
+ *
+ * The move/attach/detach status depends on how @fs has been used by the library.  
+ * The status is not set when working with fstab, etc.
+ *
+ * Returns: 1 if the filesystem has been moved.
+ */
+int mnt_fs_is_moved(struct libmnt_fs *fs)
+{
+       return fs->flags & MNT_FS_STATUS_ATTACH &&
+              fs->flags & MNT_FS_STATUS_DETACH ? 1 : 0;
+}
+
+/**
+ * mnt_fs_is_attached:
+ * @fs: filesystem
+ *
+ * The move/attach/detach status depends on how @fs has been used by the library.  
+ * The status is not set when working with fstab, etc.
+ *
+ * Returns: 1 if the filesystem has been attached.
+ */
+int mnt_fs_is_attached(struct libmnt_fs *fs)
+{
+       return fs->flags & MNT_FS_STATUS_ATTACH
+              && !(fs->flags & MNT_FS_STATUS_DETACH) ? 1 : 0;
+}
+
+/**
+ * mnt_fs_is_detached:
+ * @fs: filesystem
+ *
+ * The move/attach/detach status depends on how @fs has been used by the library.  
+ * The status is not set when working with fstab, etc.
+ *
+ * Returns: 1 if the filesystem has been dettached.
+ */
+int mnt_fs_is_detached(struct libmnt_fs *fs)
+{
+       return fs->flags & MNT_FS_STATUS_DETACH &&
+              !(fs->flags & MNT_FS_STATUS_ATTACH) ? 1 : 0;
+}
+
 /**
  * mnt_fs_is_swaparea:
  * @fs: filesystem
index b545d2441f51d08cf56c317251676b312a5251c1..e1c67990ea454519455c0f2849f28060ce13ae0c 100644 (file)
@@ -545,6 +545,15 @@ static int hook_attach_target(struct libmnt_context *cxt,
        rc = move_mount(api->fd_tree, "", AT_FDCWD, target, MOVE_MOUNT_F_EMPTY_PATH);
        hookset_set_syscall_status(cxt, "move_mount", rc == 0);
 
+       if (rc == 0) {
+               struct libmnt_optlist *ol = mnt_context_get_optlist(cxt);
+
+               if (ol && mnt_optlist_is_move(ol))
+                       mnt_fs_mark_moved(cxt->fs);
+               else
+                       mnt_fs_mark_attached(cxt->fs);
+       }
+
        return rc == 0 ? 0 : -errno;
 }
 
index 18b7a006633df88f46836dfb120195743d058e3e..cfdd7af2b2c1db7e4e45cb452b983f33ea3ea148 100644 (file)
@@ -158,6 +158,9 @@ static int hook_bindremount(struct libmnt_context *cxt,
        if (rc)
                DBG(HOOK, ul_debugobj(hs, "  mount(2) failed"
                                  " [rc=%d errno=%d %m]", rc, errno));
+       else
+               mnt_fs_mark_attached(cxt->fs);
+
        return rc;
 }
 
@@ -243,6 +246,11 @@ static int hook_mount(struct libmnt_context *cxt,
                return rc;
        }
 
+       if (mnt_optlist_is_move(ol))
+               mnt_fs_mark_moved(cxt->fs);
+       else
+               mnt_fs_mark_attached(cxt->fs);
+
        cxt->syscall_status = 0;
        return rc;
 }
index 50e8658994f20d7f291d86c2bcbb350b37d6b5b4..23cf0a4fdadca8f7d0a25d6863ab525d6097404f 100644 (file)
@@ -577,6 +577,10 @@ extern int mnt_fs_is_netfs(struct libmnt_fs *fs);
 extern int mnt_fs_is_pseudofs(struct libmnt_fs *fs);
 extern int mnt_fs_is_regularfs(struct libmnt_fs *fs);
 
+extern int mnt_fs_is_moved(struct libmnt_fs *fs);
+extern int mnt_fs_is_attached(struct libmnt_fs *fs);
+extern int mnt_fs_is_detached(struct libmnt_fs *fs);
+
 extern void mnt_free_mntent(struct mntent *mnt);
 extern int mnt_fs_to_mntent(struct libmnt_fs *fs, struct mntent **mnt);
 
index f8fdd0ef63845f5ba9fb7d1d1b0d75246ea75a91..cd3c5fb869e3293588105d7644f1a05fb5b7a0b7 100644 (file)
@@ -413,4 +413,7 @@ MOUNT_2_42 {
        mnt_context_enable_exclusive;
        mnt_context_is_exclusive;
        mnt_monitor_event_next_fs;
+       mnt_fs_is_moved;
+       mnt_fs_is_attached;
+       mnt_fs_is_detached;
 } MOUNT_2_41;
index 7c504f95ce68176d1d05cd43e8423e1ad00e585e..8dc31acad9035bc3035ea544b418e48cc024d7a6 100644 (file)
@@ -286,6 +286,26 @@ struct libmnt_fs {
 #define MNT_FS_KERNEL  (1 << 4) /* data from /proc/{mounts,self/mountinfo} */
 #define MNT_FS_MERGED  (1 << 5) /* already merged data from /run/mount/utab */
 
+#define MNT_FS_STATUS_ATTACH   (1 << 6)
+#define MNT_FS_STATUS_DETACH   (1 << 7)
+
+static inline void mnt_fs_mark_attached(struct libmnt_fs *fs)
+{
+       fs->flags &= ~MNT_FS_STATUS_DETACH;
+       fs->flags |= MNT_FS_STATUS_ATTACH;
+}
+
+static inline void mnt_fs_mark_detached(struct libmnt_fs *fs)
+{
+       fs->flags &= ~MNT_FS_STATUS_ATTACH;
+       fs->flags |= MNT_FS_STATUS_DETACH;
+}
+
+static inline void mnt_fs_mark_moved(struct libmnt_fs *fs)
+{
+       fs->flags |= MNT_FS_STATUS_ATTACH | MNT_FS_STATUS_DETACH;
+}
+
 #ifdef HAVE_STATMOUNT_API
 # define       mnt_fs_try_statmount(FS, MEMBER, FLAGS) __extension__ ({        \
                        if (!(FS)->MEMBER                                       \
index 3dd8538802e232f8680d22d5bf437ac304421d04..bd15ad90985ca8e98df36bca31a71ecb8810dd03 100644 (file)
@@ -188,6 +188,7 @@ static int mnt_parse_mountinfo_line(struct libmnt_fs *fs, const char *s)
        char *p;
 
        fs->flags |= MNT_FS_KERNEL;
+       mnt_fs_mark_attached(fs);
 
        /* (1) id */
        s = next_s32(s, &fs->id, &rc);
@@ -755,9 +756,9 @@ int mnt_table_parse_stream(struct libmnt_table *tb, FILE *f, const char *filenam
         * parser sets the flag properly
         */
        if (tb->fmt == MNT_FMT_SWAPS)
-               flags = MNT_FS_SWAP;
+               flags = MNT_FS_SWAP | MNT_FS_STATUS_ATTACH;
        else if (filename && strcmp(filename, _PATH_PROC_MOUNTS) == 0)
-               flags = MNT_FS_KERNEL;
+               flags = MNT_FS_KERNEL | MNT_FS_STATUS_ATTACH;
 
        do {
                struct libmnt_fs *fs;