]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: optimize built-in options map use
authorKarel Zak <kzak@redhat.com>
Fri, 19 Aug 2022 08:58:51 +0000 (10:58 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 3 Jan 2023 11:58:42 +0000 (12:58 +0100)
Let's keep the maps in libmnt_context to reduce
mnt_get_builtin_optmap() calls and to simplify code.

Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/context.c
libmount/src/context_mount.c
libmount/src/mountP.h

index 3bc0b5fa20c5efee558723baa0bd515438dfac8d..77f453b3e43a64e2adb6867a0422cad06cb8e8b3 100644 (file)
@@ -67,6 +67,9 @@ struct libmnt_context *mnt_new_context(void)
        cxt->ns_tgt.fd = -1;
        cxt->ns_cur = &cxt->ns_orig;
 
+       cxt->map_linux = mnt_get_builtin_optmap(MNT_LINUX_MAP);
+       cxt->map_userspace = mnt_get_builtin_optmap(MNT_USERSPACE_MAP);
+
        INIT_LIST_HEAD(&cxt->hooksets_hooks);
        INIT_LIST_HEAD(&cxt->hooksets_datas);
 
@@ -167,6 +170,9 @@ int mnt_reset_context(struct libmnt_context *cxt)
        cxt->flags = MNT_FL_DEFAULT;
        cxt->noautofs = 1;
 
+       cxt->map_linux = mnt_get_builtin_optmap(MNT_LINUX_MAP);
+       cxt->map_userspace = mnt_get_builtin_optmap(MNT_USERSPACE_MAP);
+
        mnt_context_reset_status(cxt);
        mnt_context_deinit_hooksets(cxt);
 
@@ -292,6 +298,9 @@ struct libmnt_context *mnt_copy_context(struct libmnt_context *o)
        n->mountflags = o->mountflags;
        n->mountdata = o->mountdata;
 
+       n->map_linux = o->map_linux;
+       n->map_userspace = o->map_userspace;
+
        mnt_context_reset_status(n);
 
        n->table_fltrcb = o->table_fltrcb;
@@ -1104,11 +1113,9 @@ struct libmnt_optlist *mnt_context_get_optlist(struct libmnt_context *cxt)
                cxt->optlist = mnt_new_optlist();
                if (!cxt->optlist)
                        return NULL;
-               if (mnt_optlist_register_map(cxt->optlist,
-                               mnt_get_builtin_optmap(MNT_LINUX_MAP)))
+               if (mnt_optlist_register_map(cxt->optlist, cxt->map_linux))
                        goto fail;
-               if (mnt_optlist_register_map(cxt->optlist,
-                               mnt_get_builtin_optmap(MNT_USERSPACE_MAP)))
+               if (mnt_optlist_register_map(cxt->optlist, cxt->map_userspace))
                        goto fail;
        }
 
@@ -1686,8 +1693,7 @@ int mnt_context_set_mflags(struct libmnt_context *cxt, unsigned long flags)
        if (!ls)
                return -ENOMEM;
 
-       return mnt_optlist_set_flags(ls, flags,
-                               mnt_get_builtin_optmap(MNT_LINUX_MAP));
+       return mnt_optlist_set_flags(ls, flags, cxt->map_linux);
 }
 
 /**
@@ -1707,8 +1713,7 @@ int mnt_context_get_mflags(struct libmnt_context *cxt, unsigned long *flags)
        if (!ls)
                return -ENOMEM;
 
-       return mnt_optlist_get_flags(ls, flags,
-                               mnt_get_builtin_optmap(MNT_LINUX_MAP), 0);
+       return mnt_optlist_get_flags(ls, flags, cxt->map_userspace, 0);
 }
 
 /**
@@ -1729,8 +1734,7 @@ int mnt_context_set_user_mflags(struct libmnt_context *cxt, unsigned long flags)
        if (!ls)
                return -ENOMEM;
 
-       return mnt_optlist_set_flags(ls, flags,
-                               mnt_get_builtin_optmap(MNT_USERSPACE_MAP));
+       return mnt_optlist_set_flags(ls, flags, cxt->map_userspace);
 }
 
 /**
@@ -1750,8 +1754,7 @@ int mnt_context_get_user_mflags(struct libmnt_context *cxt, unsigned long *flags
        if (!ls)
                return -ENOMEM;
 
-       return mnt_optlist_get_flags(ls, flags,
-                               mnt_get_builtin_optmap(MNT_USERSPACE_MAP), 0);
+       return mnt_optlist_get_flags(ls, flags, cxt->map_userspace, 0);
 }
 
 /**
index b3e5b19e4f0547a29b8781a8b1aba7945f68f5e3..e8e4cab7f5db4049cc72ad7638e564ebee5e0372 100644 (file)
@@ -75,10 +75,7 @@ static int fix_optstr(struct libmnt_context *cxt)
 
        /* Fix user (convert "user" to "user=username") */
        if (mnt_context_is_restricted(cxt)) {
-               const struct libmnt_optmap *map = mnt_get_builtin_optmap(MNT_USERSPACE_MAP);
-               assert(map);
-
-               opt = mnt_optlist_get_opt(ol, MNT_MS_USER, map);
+               opt = mnt_optlist_get_opt(ol, MNT_MS_USER, cxt->map_userspace);
                if (opt) {
                        char *name = mnt_get_username(getuid());
 
@@ -208,7 +205,6 @@ static int fix_optstr(struct libmnt_context *cxt)
 
        /* For backward compatinility update context fs mount options */
        if (fs) {
-               const struct libmnt_optmap *map;
                const char *p;
 
                /* All options */
@@ -220,13 +216,11 @@ static int fix_optstr(struct libmnt_context *cxt)
                strdup_to_struct_member(fs, fs_optstr, p);
 
                /* VFS options */
-               map = mnt_get_builtin_optmap(MNT_LINUX_MAP);
-               mnt_optlist_get_optstr(ol, &p, map, 0);
+               mnt_optlist_get_optstr(ol, &p, cxt->map_linux, 0);
                strdup_to_struct_member(fs, vfs_optstr, p);
 
                /* Userspace options */
-               map = mnt_get_builtin_optmap(MNT_USERSPACE_MAP);
-               mnt_optlist_get_optstr(ol, &p, map, 0);
+               mnt_optlist_get_optstr(ol, &p, cxt->map_userspace, 0);
                strdup_to_struct_member(fs, user_optstr, p);
 
                DBG(CXT, ul_debugobj(cxt, " fixed options: "
@@ -254,7 +248,6 @@ static int evaluate_permissions(struct libmnt_context *cxt)
 {
        struct libmnt_optlist *ol;
        unsigned long user_flags = 0;   /* userspace mount flags */
-       const struct libmnt_optmap *user_map;   /* map with userspace options */
        int rc;
 
        assert(cxt);
@@ -269,11 +262,8 @@ static int evaluate_permissions(struct libmnt_context *cxt)
        if (!ol)
                return -EINVAL;
 
-       user_map = mnt_get_builtin_optmap(MNT_USERSPACE_MAP);
-       assert(user_map);
-
        /* get userspace mount flags (user[=<name>] etc.*/
-       rc = mnt_optlist_get_flags(ol, &user_flags, user_map, 0);
+       rc = mnt_optlist_get_flags(ol, &user_flags, cxt->map_userspace, 0);
        if (rc)
                return rc;
 
@@ -283,11 +273,10 @@ static int evaluate_permissions(struct libmnt_context *cxt)
                 */
                if (user_flags & (MNT_MS_OWNER | MNT_MS_GROUP))
                        mnt_optlist_remove_flags(ol,
-                                       MNT_MS_OWNER | MNT_MS_GROUP, user_map);
+                                       MNT_MS_OWNER | MNT_MS_GROUP, cxt->map_userspace);
                DBG(CXT, ul_debugobj(cxt, "perms: superuser"));
        } else {
                struct libmnt_opt *opt;
-               const struct libmnt_optmap *sys_map;    /* map with system mount flags */
 
                /*
                 * user mount
@@ -298,16 +287,13 @@ static int evaluate_permissions(struct libmnt_context *cxt)
                        return -EPERM;
                }
 
-               sys_map = mnt_get_builtin_optmap(MNT_LINUX_MAP);
-               assert(sys_map);
-
                /*
                * Ignore user=<name> (if <name> is set). Let's keep it hidden
                * for normal library operations, but visible for /sbin/mount.<type>
                * helpers.
                */
                if (user_flags & MNT_MS_USER
-                   && (opt = mnt_optlist_get_opt(ol, MNT_MS_USER, user_map))
+                   && (opt = mnt_optlist_get_opt(ol, MNT_MS_USER, cxt->map_userspace))
                    && mnt_opt_has_value(opt)) {
                        DBG(CXT, ul_debugobj(cxt, "perms: user=<name> detected, ignore"));
 
@@ -321,13 +307,13 @@ static int evaluate_permissions(struct libmnt_context *cxt)
                 * Insert MS_SECURE between system flags on position where is MNT_MS_USER
                 */
                if ((user_flags & MNT_MS_USER)
-                   && (rc = mnt_optlist_insert_flags(ol, MS_SECURE, sys_map,
-                                                   MNT_MS_USER, user_map)))
+                   && (rc = mnt_optlist_insert_flags(ol, MS_SECURE, cxt->map_linux,
+                                                   MNT_MS_USER, cxt->map_userspace)))
                        return rc;
 
                if ((user_flags & MNT_MS_USERS)
-                   && (rc = mnt_optlist_insert_flags(ol, MS_SECURE, sys_map,
-                                                   MNT_MS_USERS, user_map)))
+                   && (rc = mnt_optlist_insert_flags(ol, MS_SECURE, cxt->map_linux,
+                                                   MNT_MS_USERS, cxt->map_userspace)))
                        return rc;
 
                /*
@@ -365,16 +351,16 @@ static int evaluate_permissions(struct libmnt_context *cxt)
                                /* insert MS_OWNERSECURE between system flags */
                                if (user_flags & MNT_MS_OWNER)
                                        mnt_optlist_insert_flags(ol,
-                                                       MS_OWNERSECURE, sys_map,
-                                                       MNT_MS_OWNER, user_map);
+                                                       MS_OWNERSECURE, cxt->map_linux,
+                                                       MNT_MS_OWNER, cxt->map_userspace);
                                if (user_flags & MNT_MS_GROUP)
                                        mnt_optlist_insert_flags(ol,
-                                                       MS_OWNERSECURE, sys_map,
-                                                       MNT_MS_GROUP, user_map);
+                                                       MS_OWNERSECURE, cxt->map_linux,
+                                                       MNT_MS_GROUP, cxt->map_userspace);
 
                                /* continue as like "user" was specified */
                                user_flags |= MNT_MS_USER;
-                               mnt_optlist_append_flags(ol, MNT_MS_USER, user_map);
+                               mnt_optlist_append_flags(ol, MNT_MS_USER, cxt->map_userspace);
                        }
 
                        if (!cache)
@@ -1892,7 +1878,7 @@ static int test_fixopts(struct libmnt_test *ts, int argc, char *argv[])
 
        mnt_optlist_get_optstr(ls, &p, NULL, 0);
 
-       mnt_optlist_get_flags(ls, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP), 0);
+       mnt_optlist_get_flags(ls, &flags, cxt->map_linux, 0);
        printf("options (dfl): '%s' [mount flags: %08lx]\n", p, flags);
 
        mnt_optlist_get_optstr(ls, &p, NULL, MNT_OL_FLTR_ALL);
index 57a55a4733f4aba338e866ba22aa560c79cc781e..05e2e84781a45feacafcb3729efe1ed98358c085 100644 (file)
@@ -389,6 +389,9 @@ struct libmnt_context
        struct libmnt_update    *update;        /* utab update */
        struct libmnt_optlist   *optlist;       /* parsed mount options */
 
+       const struct libmnt_optmap *map_linux;          /* system options map */
+       const struct libmnt_optmap *map_userspace;      /* userspace options map */
+
        const char      *mountinfo_path; /* usualy /proc/self/moutinfo */
 
        const char      *utab_path; /* path to utab */