]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: make mnt_context_find_umount_fs() more extendable
authorKarel Zak <kzak@redhat.com>
Fri, 27 Mar 2020 10:34:12 +0000 (11:34 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 27 Mar 2020 10:34:12 +0000 (11:34 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/context_umount.c

index 642212fda0aa6cf687eda9eeb755e16d08f2bd81..733e9194db85f5936f467afa213a12679dda181e 100644 (file)
 # define UMOUNT_UNUSED    0x80000000   /* Flag guaranteed to be unused */
 #endif
 
-/**
- * mnt_context_find_umount_fs:
- * @cxt: mount context
- * @tgt: mountpoint, device, ...
- * @pfs: returns point to filesystem
- *
- * Returns: 0 on success, <0 on error, 1 if target filesystem not found
- */
-int mnt_context_find_umount_fs(struct libmnt_context *cxt,
-                              const char *tgt,
-                              struct libmnt_fs **pfs)
+/* search in mountinfo/mtab */
+static int __mtab_find_umount_fs(struct libmnt_context *cxt,
+                           const char *tgt,
+                           struct libmnt_fs **pfs)
 {
        int rc;
        struct libmnt_ns *ns_old;
@@ -61,16 +54,12 @@ int mnt_context_find_umount_fs(struct libmnt_context *cxt,
        struct libmnt_fs *fs;
        char *loopdev = NULL;
 
-       if (pfs)
-               *pfs = NULL;
-
-       if (!cxt || !tgt || !pfs)
-               return -EINVAL;
-
-       DBG(CXT, ul_debugobj(cxt, "umount: lookup FS for '%s'", tgt));
+       assert(cxt);
+       assert(tgt);
+       assert(pfs);
 
-       if (!*tgt)
-               return 1; /* empty string is not an error */
+       *pfs = NULL;
+       DBG(CXT, ul_debugobj(cxt, " search %s in mountinfo", tgt));
 
        /*
         * The mount table may be huge, and on systems with utab we have to
@@ -166,8 +155,7 @@ try_loopdev:
                }
        }
 
-       if (pfs)
-               *pfs = fs;
+       *pfs = fs;
        free(loopdev);
        if (!mnt_context_switch_ns(cxt, ns_old))
                return -MNT_ERR_NAMESPACE;
@@ -182,6 +170,36 @@ err:
        return rc;
 }
 
+/**
+ * mnt_context_find_umount_fs:
+ * @cxt: mount context
+ * @tgt: mountpoint, device, ...
+ * @pfs: returns point to filesystem
+ *
+ * Returns: 0 on success, <0 on error, 1 if target filesystem not found
+ */
+int mnt_context_find_umount_fs(struct libmnt_context *cxt,
+                              const char *tgt,
+                              struct libmnt_fs **pfs)
+{
+       if (pfs)
+               *pfs = NULL;
+
+       if (!cxt || !tgt || !pfs)
+               return -EINVAL;
+
+       DBG(CXT, ul_debugobj(cxt, "umount: lookup FS for '%s'", tgt));
+
+       if (!*tgt)
+               return 1; /* empty string is not an error */
+
+       /* In future this function should be extened to support for example
+        * fsinfo() (or another cheap way kernel will support), for now the
+        * default is expensive mountinfo/mtab.
+        */
+       return __mtab_find_umount_fs(cxt, tgt, pfs);
+}
+
 /* Check if there is something important in the utab file. The parsed utab is
  * stored in context->utab and deallocated by mnt_free_context().
  *
@@ -295,13 +313,13 @@ static int lookup_umount_fs_by_mountinfo(struct libmnt_context *cxt, const char
 
        DBG(CXT, ul_debugobj(cxt, " lookup by mountinfo"));
 
-       rc = mnt_context_find_umount_fs(cxt, tgt, &fs);
+       /* search */
+       rc = __mtab_find_umount_fs(cxt, tgt, &fs);
        if (rc != 0)
                return rc;
 
+       /* apply result */
        if (fs != cxt->fs) {
-               /* copy from mtab to our FS description
-                */
                mnt_fs_set_source(cxt->fs, NULL);
                mnt_fs_set_target(cxt->fs, NULL);
 
@@ -316,8 +334,13 @@ static int lookup_umount_fs_by_mountinfo(struct libmnt_context *cxt, const char
        return 0;
 }
 
-/* this is umount replacement to mnt_context_apply_fstab(), use
- * mnt_context_tab_applied() to check result.
+/* This finction search for FS according to cxt->fs->target,
+ * apply result to cxt->fs and it's umount replacement to
+ * mnt_context_apply_fstab(), use mnt_context_tab_applied()
+ * to check result.
+ *
+ * The goal is to mimimize situations wehn we need to parse
+ * /proc/self/mountinfo.
  */
 static int lookup_umount_fs(struct libmnt_context *cxt)
 {