Mark up sysctl node with Li, like in sysctl(7).
[netbsd-mini2440.git] / sys / kern / vfs_hooks.c
blob7ab9d3342fa334b9754663fbe3a52f5e67e03b0e
1 /* $NetBSD: vfs_hooks.c,v 1.5 2008/11/19 18:36:07 ad Exp $ */
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal.
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.
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.
33 * VFS hooks.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: vfs_hooks.c,v 1.5 2008/11/19 18:36:07 ad Exp $");
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/mount.h>
42 #include <sys/mutex.h>
44 LIST_HEAD(vfs_hooks_head, vfs_hooks) vfs_hooks_head =
45 LIST_HEAD_INITIALIZER(vfs_hooks_head);
47 kmutex_t vfs_hooks_lock;
49 void
50 vfs_hooks_init(void)
53 mutex_init(&vfs_hooks_lock, MUTEX_DEFAULT, IPL_NONE);
56 int
57 vfs_hooks_attach(struct vfs_hooks *vfs_hooks)
60 mutex_enter(&vfs_hooks_lock);
61 LIST_INSERT_HEAD(&vfs_hooks_head, vfs_hooks, vfs_hooks_list);
62 mutex_exit(&vfs_hooks_lock);
64 return (0);
67 int
68 vfs_hooks_detach(struct vfs_hooks *vfs_hooks)
70 struct vfs_hooks *hp;
71 int ret = 0;
73 mutex_enter(&vfs_hooks_lock);
74 LIST_FOREACH(hp, &vfs_hooks_head, vfs_hooks_list) {
75 if (hp == vfs_hooks) {
76 LIST_REMOVE(hp, vfs_hooks_list);
77 break;
80 if (hp == NULL)
81 ret = ESRCH;
82 mutex_exit(&vfs_hooks_lock);
84 return (ret);
88 * Macro to be used in one of the vfs_hooks_* function for hooks that
89 * return an error code. Calls will stop as soon as one of the hooks
90 * fails.
92 #define VFS_HOOKS_W_ERROR(func, fargs, hook, hargs) \
93 int \
94 func fargs \
95 { \
96 int error; \
97 struct vfs_hooks *hp; \
99 error = EJUSTRETURN; \
101 mutex_enter(&vfs_hooks_lock); \
102 LIST_FOREACH(hp, &vfs_hooks_head, vfs_hooks_list) { \
103 if (hp-> hook != NULL) { \
104 error = hp-> hook hargs; \
105 if (error != 0) \
106 break; \
109 mutex_exit(&vfs_hooks_lock); \
111 return error; \
115 * Macro to be used in one of the vfs_hooks_* function for hooks that
116 * do not return any error code. All hooks will be executed
117 * unconditionally.
119 #define VFS_HOOKS_WO_ERROR(func, fargs, hook, hargs) \
120 void \
121 func fargs \
123 struct vfs_hooks *hp; \
125 mutex_enter(&vfs_hooks_lock); \
126 LIST_FOREACH(hp, &vfs_hooks_head, vfs_hooks_list) { \
127 if (hp-> hook != NULL) \
128 hp-> hook hargs; \
130 mutex_exit(&vfs_hooks_lock); \
134 * Routines to iterate over VFS hooks lists and execute them.
137 VFS_HOOKS_WO_ERROR(vfs_hooks_unmount, (struct mount *mp), vh_unmount, (mp));
138 VFS_HOOKS_W_ERROR(vfs_hooks_reexport, (struct mount *mp, const char *path, void *data), vh_reexport, (mp, path, data));