vfs_hooks.c revision 1.7 1 /* $NetBSD: vfs_hooks.c,v 1.7 2024/12/07 02:11:42 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * VFS hooks.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: vfs_hooks.c,v 1.7 2024/12/07 02:11:42 riastradh Exp $");
38
39 #include <sys/param.h>
40
41 #include <sys/errno.h>
42 #include <sys/mount.h>
43 #include <sys/mutex.h>
44 #include <sys/queue.h>
45
46 LIST_HEAD(vfs_hooks_head, vfs_hooks) vfs_hooks_head =
47 LIST_HEAD_INITIALIZER(vfs_hooks_head);
48
49 kmutex_t vfs_hooks_lock;
50
51 void
52 vfs_hooks_init(void)
53 {
54
55 mutex_init(&vfs_hooks_lock, MUTEX_DEFAULT, IPL_NONE);
56 }
57
58 int
59 vfs_hooks_attach(struct vfs_hooks *vfs_hooks)
60 {
61
62 mutex_enter(&vfs_hooks_lock);
63 LIST_INSERT_HEAD(&vfs_hooks_head, vfs_hooks, vfs_hooks_list);
64 mutex_exit(&vfs_hooks_lock);
65
66 return 0;
67 }
68
69 int
70 vfs_hooks_detach(struct vfs_hooks *vfs_hooks)
71 {
72 struct vfs_hooks *hp;
73 int ret = 0;
74
75 mutex_enter(&vfs_hooks_lock);
76 LIST_FOREACH(hp, &vfs_hooks_head, vfs_hooks_list) {
77 if (hp == vfs_hooks) {
78 LIST_REMOVE(hp, vfs_hooks_list);
79 break;
80 }
81 }
82 if (hp == NULL)
83 ret = ESRCH;
84 mutex_exit(&vfs_hooks_lock);
85
86 return ret;
87 }
88
89 /*
90 * Macro to be used in one of the vfs_hooks_* function for hooks that
91 * return an error code. Calls will stop as soon as one of the hooks
92 * fails.
93 */
94 #define VFS_HOOKS_W_ERROR(func, fargs, hook, hargs) \
95 int \
96 func fargs \
97 { \
98 int error; \
99 struct vfs_hooks *hp; \
100 \
101 error = EJUSTRETURN; \
102 \
103 mutex_enter(&vfs_hooks_lock); \
104 LIST_FOREACH(hp, &vfs_hooks_head, vfs_hooks_list) { \
105 if (hp-> hook != NULL) { \
106 error = hp-> hook hargs; \
107 if (error != 0) \
108 break; \
109 } \
110 } \
111 mutex_exit(&vfs_hooks_lock); \
112 \
113 return error; \
114 }
115
116 /*
117 * Macro to be used in one of the vfs_hooks_* function for hooks that
118 * do not return any error code. All hooks will be executed
119 * unconditionally.
120 */
121 #define VFS_HOOKS_WO_ERROR(func, fargs, hook, hargs) \
122 void \
123 func fargs \
124 { \
125 struct vfs_hooks *hp; \
126 \
127 mutex_enter(&vfs_hooks_lock); \
128 LIST_FOREACH(hp, &vfs_hooks_head, vfs_hooks_list) { \
129 if (hp-> hook != NULL) \
130 hp-> hook hargs; \
131 } \
132 mutex_exit(&vfs_hooks_lock); \
133 }
134
135 /*
136 * Routines to iterate over VFS hooks lists and execute them.
137 */
138
139 VFS_HOOKS_WO_ERROR(vfs_hooks_unmount, (struct mount *mp), vh_unmount, (mp));
140 VFS_HOOKS_W_ERROR(vfs_hooks_reexport, (struct mount *mp, const char *path, void *data), vh_reexport, (mp, path, data));
141