Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: vfs_hooks.c,v 1.9 2024/12/07 02:27:38 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.9 2024/12/07 02:27:38 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 #include <sys/sdt.h>
     46 
     47 LIST_HEAD(vfs_hooks_head, vfs_hooks) vfs_hooks_head =
     48     LIST_HEAD_INITIALIZER(vfs_hooks_head);
     49 
     50 kmutex_t vfs_hooks_lock;
     51 
     52 void
     53 vfs_hooks_init(void)
     54 {
     55 
     56 	mutex_init(&vfs_hooks_lock, MUTEX_DEFAULT, IPL_NONE);
     57 }
     58 
     59 int
     60 vfs_hooks_attach(struct vfs_hooks *vfs_hooks)
     61 {
     62 
     63 	mutex_enter(&vfs_hooks_lock);
     64 	LIST_INSERT_HEAD(&vfs_hooks_head, vfs_hooks, vfs_hooks_list);
     65 	mutex_exit(&vfs_hooks_lock);
     66 
     67 	return 0;
     68 }
     69 
     70 int
     71 vfs_hooks_detach(struct vfs_hooks *vfs_hooks)
     72 {
     73 	struct vfs_hooks *hp;
     74 	int ret = 0;
     75 
     76 	mutex_enter(&vfs_hooks_lock);
     77         LIST_FOREACH(hp, &vfs_hooks_head, vfs_hooks_list) {
     78                 if (hp == vfs_hooks) {
     79                         LIST_REMOVE(hp, vfs_hooks_list);
     80                         break;
     81                 }
     82         }
     83         if (hp == NULL)
     84 		ret = SET_ERROR(ESRCH);
     85 	mutex_exit(&vfs_hooks_lock);
     86 
     87 	return ret;
     88 }
     89 
     90 /*
     91  * Macro to be used in one of the vfs_hooks_* function for hooks that
     92  * return an error code.  Calls will stop as soon as one of the hooks
     93  * fails.
     94  */
     95 #define VFS_HOOKS_W_ERROR(func, fargs, hook, hargs)			\
     96 int									\
     97 func fargs								\
     98 {									\
     99 	int error;							\
    100 	struct vfs_hooks *hp;						\
    101 									\
    102 	error = EJUSTRETURN;						\
    103 									\
    104 	mutex_enter(&vfs_hooks_lock);					\
    105         LIST_FOREACH(hp, &vfs_hooks_head, vfs_hooks_list) {		\
    106 		if (hp-> hook != NULL) {				\
    107 			error = hp-> hook hargs;			\
    108 			if (error != 0)					\
    109 				break;					\
    110 		}							\
    111 	}								\
    112 	mutex_exit(&vfs_hooks_lock);					\
    113 									\
    114 	return error;							\
    115 }
    116 
    117 /*
    118  * Macro to be used in one of the vfs_hooks_* function for hooks that
    119  * do not return any error code.  All hooks will be executed
    120  * unconditionally.
    121  */
    122 #define VFS_HOOKS_WO_ERROR(func, fargs, hook, hargs)			\
    123 void									\
    124 func fargs								\
    125 {									\
    126 	struct vfs_hooks *hp;						\
    127 									\
    128 	mutex_enter(&vfs_hooks_lock);					\
    129         LIST_FOREACH(hp, &vfs_hooks_head, vfs_hooks_list) {		\
    130 		if (hp-> hook != NULL)					\
    131 			hp-> hook hargs;				\
    132 	}								\
    133 	mutex_exit(&vfs_hooks_lock);					\
    134 }
    135 
    136 /*
    137  * Routines to iterate over VFS hooks lists and execute them.
    138  */
    139 
    140 VFS_HOOKS_WO_ERROR(vfs_hooks_unmount, (struct mount *mp), vh_unmount, (mp));
    141 VFS_HOOKS_W_ERROR(vfs_hooks_reexport, (struct mount *mp, const char *path, void *data), vh_reexport, (mp, path, data));
    142