From: Karel Zak Date: Mon, 30 May 2022 07:47:04 +0000 (+0200) Subject: libmount: remove hooks initialization X-Git-Tag: v2.39-rc1~342 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=bd599524c879489041b25f32e53da913ceb60b41;p=thirdparty%2Futil-linux.git libmount: remove hooks initialization 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 --- diff --git a/libmount/src/context_mount.c b/libmount/src/context_mount.c index e176607551..f986cf8ead 100644 --- a/libmount/src/context_mount.c +++ b/libmount/src/context_mount.c @@ -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) diff --git a/libmount/src/hook_mkdir.c b/libmount/src/hook_mkdir.c index 191899f303..dd839e0515 100644 --- a/libmount/src/hook_mkdir.c +++ b/libmount/src/hook_mkdir.c @@ -15,14 +15,6 @@ 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 }; diff --git a/libmount/src/hook_mount_legacy.c b/libmount/src/hook_mount_legacy.c index a82b682d6b..c05dc5ed67 100644 --- a/libmount/src/hook_mount_legacy.c +++ b/libmount/src/hook_mount_legacy.c @@ -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 }; diff --git a/libmount/src/hook_subdir.c b/libmount/src/hook_subdir.c index 7a069d0950..79e02474aa 100644 --- a/libmount/src/hook_subdir.c +++ b/libmount/src/hook_subdir.c @@ -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 }; diff --git a/libmount/src/hooks.c b/libmount/src/hooks.c index 13a3185993..5b345c4a63 100644 --- a/libmount/src/hooks.c +++ b/libmount/src/hooks.c @@ -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); diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index 80f02af761..9a748513ed 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -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);