]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount: remove hooks initialization
authorKarel Zak <kzak@redhat.com>
Mon, 30 May 2022 07:47:04 +0000 (09:47 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 3 Jan 2023 11:53:12 +0000 (12:53 +0100)
The hookset initialization is unnecessary. It's enough to do the
initialization in the first hook (usually in the "prepare" stage).

This change safes memory and time.

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

index e1766075514d364667c21c5d5d73893ce6726529..f986cf8eadf4e65778b6d21b3c12f2b1ae615f9f 100644 (file)
@@ -899,8 +899,6 @@ int mnt_context_prepare_mount(struct libmnt_context *cxt)
                rc = mnt_context_merge_mflags(cxt);
        if (!rc)
                rc = evaluate_permissions(cxt);
-       if (!rc)
-               rc = mnt_context_init_hooksets(cxt);
        if (!rc)
                rc = fix_optstr(cxt);
        if (!rc)
index 191899f303bcc888fb2f95f56b651dc9d1da8d90..dd839e051572bcad98635108bb4717ddfed9d113 100644 (file)
 
 static int hook_prepare_target(struct libmnt_context *cxt, const struct libmnt_hookset *hs, void *data);
 
-static int hookset_init(struct libmnt_context *cxt, const struct libmnt_hookset *hs)
-{
-       DBG(HOOK, ul_debugobj(hs, "init '%s'", hs->name));
-
-       return mnt_context_append_hook(cxt, hs,
-                               MNT_STAGE_PREP_TARGET, NULL, hook_prepare_target);
-}
-
 static int hookset_deinit(struct libmnt_context *cxt, const struct libmnt_hookset *hs)
 {
        void *data = NULL;
@@ -86,7 +78,6 @@ static int is_mkdir_required(const char *tgt, struct libmnt_fs *fs, mode_t *mode
        return 1;
 }
 
-
 static int hook_prepare_target(
                        struct libmnt_context *cxt,
                        const struct libmnt_hookset *hs,
@@ -130,10 +121,12 @@ static int hook_prepare_target(
        return rc;
 }
 
-
 const struct libmnt_hookset hookset_mkdir =
 {
        .name = "__mkdir",
-       .init = hookset_init,
+
+       .firststage = MNT_STAGE_PREP_TARGET,
+       .firstcall = hook_prepare_target,
+
        .deinit = hookset_deinit
 };
index a82b682d6b98e2014e684dca749d3159a519b7e9..c05dc5ed67d704bae7b5a5652f4af7fcbf26808a 100644 (file)
@@ -18,21 +18,6 @@ struct hook_data {
        unsigned long mountflags;
 };
 
-static int hookset_init(struct libmnt_context *cxt, const struct libmnt_hookset *hs)
-{
-#ifdef UL_HAVE_MOUNT_API
-       /* do nothing when __builtin-mount succesfully registred */
-       if (mnt_context_has_hook(cxt, &hookset_mount, 0, NULL))
-               return 0;
-#endif
-
-       DBG(HOOK, ul_debugobj(hs, "init '%s'", hs->name));
-
-       /* add very basic callback */
-       return mnt_context_append_hook(cxt, hs,
-                               MNT_STAGE_PREP_OPTIONS, NULL, hook_prepare);
-}
-
 static int hookset_deinit(struct libmnt_context *cxt, const struct libmnt_hookset *hs)
 {
        void *data = NULL;
@@ -275,6 +260,11 @@ static int hook_prepare(struct libmnt_context *cxt,
        assert(cxt);
        assert(hs == &hookset_mount_legacy);
 
+#ifdef UL_HAVE_MOUNT_API
+       /* do nothing when __builtin-mount succesfully registred */
+       if (mnt_context_has_hook(cxt, &hookset_mount, 0, NULL))
+               return 0;
+#endif
        /* add extra mount(2) calls for each propagation flag  */
        if (cxt->mountflags & MS_PROPAGATION) {
                rc = prepare_propagation(cxt, hs);
@@ -299,10 +289,12 @@ static int hook_prepare(struct libmnt_context *cxt,
        return rc;
 }
 
-
 const struct libmnt_hookset hookset_mount_legacy =
 {
        .name = "__legacy-mount",
-       .init = hookset_init,
+
+       .firststage = MNT_STAGE_PREP_OPTIONS,
+       .firstcall = hook_prepare,
+
        .deinit = hookset_deinit
 };
index 7a069d095048ef1f6b2b6919269735ea5b7fc61a..79e02474aa9c44d1b139932013a38e4420abcc92 100644 (file)
@@ -60,15 +60,6 @@ static struct hookset_data *new_hookset_data(
        return hsd;
 }
 
-/* initiallize this module */
-static int hookset_init(struct libmnt_context *cxt, const struct libmnt_hookset *hs)
-{
-       DBG(HOOK, ul_debugobj(hs, "init '%s'", hs->name));
-
-       return mnt_context_append_hook(cxt, hs,
-                               MNT_STAGE_PREP_TARGET, NULL, hook_prepare_target);
-}
-
 /* de-initiallize this module */
 static int hookset_deinit(struct libmnt_context *cxt, const struct libmnt_hookset *hs)
 {
@@ -307,10 +298,12 @@ static int hook_prepare_target(
        return rc;
 }
 
-
 const struct libmnt_hookset hookset_subdir =
 {
        .name = "__subdir",
-       .init = hookset_init,
+
+       .firststage = MNT_STAGE_PREP_TARGET,
+       .firstcall = hook_prepare_target,
+
        .deinit = hookset_deinit
 };
index 13a31859933f24b8fbb9dc8190c05b5e11045df3..5b345c4a63daf8bd0676963dbd1f82a7131cf495 100644 (file)
@@ -62,26 +62,6 @@ static const char *stagenames[] = {
        [MNT_STAGE_POST] = "post",
 };
 
-int mnt_context_init_hooksets(struct libmnt_context *cxt)
-{
-       size_t i;
-       int rc = 0;
-
-       assert(cxt);
-
-       for (i = 0; i <  ARRAY_SIZE(hooksets); i++) {
-               const struct libmnt_hookset *hs = hooksets[i];
-
-               rc = hs->init(cxt, hs);
-               if (rc < 0)
-                       break;
-       }
-
-       if (rc < 0)
-               mnt_context_deinit_hooksets(cxt);
-       return rc;
-}
-
 int mnt_context_deinit_hooksets(struct libmnt_context *cxt)
 {
        size_t i;
@@ -277,7 +257,25 @@ int mnt_context_has_hook(struct libmnt_context *cxt,
 int mnt_context_call_hooks(struct libmnt_context *cxt, int stage)
 {
        struct list_head *p, *next;
+       size_t i;
+
+       /* call initial hooks */
+       for (i = 0; i <  ARRAY_SIZE(hooksets); i++) {
+               int rc;
+               const struct libmnt_hookset *hs = hooksets[i];
+
+               if (hs->firststage != stage)
+                       continue;
+
+               DBG(CXT, ul_debugobj(cxt, "calling %s hook from %s",
+                        stagenames[hs->firststage], hs->name));
+
+               rc = hs->firstcall(cxt, hs, NULL);
+               if (rc < 0)
+                       return rc;
+       }
 
+       /* call already active hooks */
        list_for_each_safe(p, next, &cxt->hooksets_hooks) {
                int rc;
                struct hookset_hook *x = list_entry(p, struct hookset_hook, hooks);
index 80f02af761814db47371634c7d16147191b9dfee..9a748513ed337a5fa81a9f9e9f245cb1911ae60b 100644 (file)
@@ -295,7 +295,10 @@ enum {
 
 struct libmnt_hookset {
        const char *name;                               /* hook set name */
-       int (*init)(struct libmnt_context *, const struct libmnt_hookset *);    /* initialization function */
+
+       int firststage;
+       int (*firstcall)(struct libmnt_context *, const struct libmnt_hookset *, void *);
+
        int (*deinit)(struct libmnt_context *, const struct libmnt_hookset *);  /* cleanup function */
 };
 
@@ -304,8 +307,6 @@ extern const struct libmnt_hookset hookset_mount_legacy;
 extern const struct libmnt_hookset hookset_mkdir;
 extern const struct libmnt_hookset hookset_subdir;
 
-
-extern int mnt_context_init_hooksets(struct libmnt_context *cxt);
 extern int mnt_context_deinit_hooksets(struct libmnt_context *cxt);
 extern const struct libmnt_hookset *mnt_context_get_hookset(struct libmnt_context *cxt, const char *name);