1 /* $NetBSD: autofs.h,v 1.4 2019/11/23 17:13:46 tkusumi Exp $ */ 2 3 /*- 4 * Copyright (c) 2017 The NetBSD Foundation, Inc. 5 * Copyright (c) 2016 The DragonFly Project 6 * Copyright (c) 2014 The FreeBSD Foundation 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>. 11 * 12 * This software was developed by Edward Tomasz Napierala under sponsorship 13 * from the FreeBSD Foundation. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * $FreeBSD$ 37 */ 38 39 #ifndef _SYS_FS_AUTOFS_AUTOFS_H_ 40 #define _SYS_FS_AUTOFS_AUTOFS_H_ 41 42 #include <sys/param.h> 43 #include <sys/kernel.h> 44 #include <sys/module.h> 45 #include <sys/types.h> 46 #include <sys/systm.h> 47 #include <sys/kmem.h> 48 #include <sys/tree.h> 49 #include <sys/mutex.h> 50 #include <sys/condvar.h> 51 #include <sys/mount.h> 52 #include <sys/vnode.h> 53 #include <sys/timespec.h> 54 #include <sys/callout.h> 55 #include <sys/workqueue.h> 56 #include <sys/conf.h> 57 #include <sys/proc.h> 58 59 #include "autofs_ioctl.h" 60 61 #define AUTOFS_ROOTINO ((ino_t)1) 62 #define VFSTOAUTOFS(mp) ((struct autofs_mount *)((mp)->mnt_data)) 63 #define VTOI(vp) ((struct autofs_node *)((vp)->v_data)) 64 65 extern int (**autofs_vnodeop_p)(void *); 66 extern const struct vnodeopv_desc autofs_vnodeop_opv_desc; 67 68 extern struct cdevsw autofs_ops; 69 70 extern struct pool autofs_request_pool; 71 extern struct pool autofs_node_pool; 72 extern struct autofs_softc *autofs_softc; 73 extern struct workqueue *autofs_tmo_wq; 74 75 extern int autofs_debug; 76 extern int autofs_mount_on_stat; 77 extern int autofs_timeout; 78 extern int autofs_cache; 79 extern int autofs_retry_attempts; 80 extern int autofs_retry_delay; 81 extern int autofs_interruptible; 82 83 #define AUTOFS_DEBUG(X, ...) \ 84 do { \ 85 if (autofs_debug > 1) \ 86 printf("%s: " X "\n", \ 87 __func__, ## __VA_ARGS__); \ 88 } while (0) 89 90 #define AUTOFS_WARN(X, ...) \ 91 do { \ 92 if (autofs_debug > 0) { \ 93 printf("WARNING: %s: " X "\n", \ 94 __func__, ## __VA_ARGS__); \ 95 } \ 96 } while (0) 97 98 /* 99 * APRINTF is only for debugging. 100 */ 101 #define APRINTF(X, ...) \ 102 printf("### %s(%s): " X, \ 103 __func__, curproc->p_comm, ## __VA_ARGS__) 104 105 struct autofs_node { 106 RB_ENTRY(autofs_node) an_entry; 107 char *an_name; 108 ino_t an_ino; 109 struct autofs_node *an_parent; 110 RB_HEAD(autofs_node_tree, 111 autofs_node) an_children; 112 struct autofs_mount *an_mount; 113 struct vnode *an_vnode; 114 bool an_cached; 115 bool an_wildcards; 116 struct callout an_callout; 117 int an_retries; 118 struct timespec an_ctime; 119 }; 120 121 struct autofs_mount { 122 struct autofs_node *am_root; 123 struct mount *am_mp; 124 kmutex_t am_lock; 125 char am_from[AUTOFS_MAXPATHLEN]; 126 char am_on[AUTOFS_MAXPATHLEN]; 127 char am_options[AUTOFS_MAXPATHLEN]; 128 char am_prefix[AUTOFS_MAXPATHLEN]; 129 ino_t am_last_ino; 130 }; 131 132 struct autofs_request { 133 struct work ar_wk; /* must be first */ 134 TAILQ_ENTRY(autofs_request) ar_next; 135 struct autofs_mount *ar_mount; 136 int ar_id; 137 bool ar_done; 138 int ar_error; 139 bool ar_wildcards; 140 bool ar_in_progress; 141 char ar_from[AUTOFS_MAXPATHLEN]; 142 char ar_path[AUTOFS_MAXPATHLEN]; 143 char ar_prefix[AUTOFS_MAXPATHLEN]; 144 char ar_key[AUTOFS_MAXPATHLEN]; 145 char ar_options[AUTOFS_MAXPATHLEN]; 146 struct callout ar_callout; 147 volatile unsigned int ar_refcount; 148 }; 149 150 struct autofs_softc { 151 kcondvar_t sc_cv; 152 kmutex_t sc_lock; 153 TAILQ_HEAD(, autofs_request) sc_requests; 154 bool sc_dev_opened; 155 pid_t sc_dev_sid; 156 int sc_last_request_id; 157 }; 158 159 int autofs_trigger(struct autofs_node *anp, const char *component, 160 int componentlen); 161 bool autofs_cached(struct autofs_node *anp, const char *component, 162 int componentlen); 163 void autofs_flush(struct autofs_mount *amp); 164 bool autofs_ignore_thread(void); 165 void autofs_timeout_wq(struct work *wk, void *arg); 166 int autofs_node_new(struct autofs_node *parent, struct autofs_mount *amp, 167 const char *name, int namelen, struct autofs_node **anpp); 168 int autofs_node_find(struct autofs_node *parent, 169 const char *name, int namelen, struct autofs_node **anpp); 170 void autofs_node_delete(struct autofs_node *anp); 171 172 static __inline void 173 autofs_node_cache(struct autofs_node *anp) 174 { 175 176 anp->an_cached = true; 177 } 178 179 static __inline void 180 autofs_node_uncache(struct autofs_node *anp) 181 { 182 183 anp->an_cached = false; 184 } 185 186 static __inline char * 187 autofs_strndup(const char *name, int nlen, km_flag_t fl) 188 { 189 return nlen > 0 ? kmem_strndup(name, nlen, fl) : kmem_strdup(name, fl); 190 } 191 192 RB_PROTOTYPE(autofs_node_tree, autofs_node, an_entry, autofs_node_cmp); 193 194 #endif /* !_SYS_FS_AUTOFS_AUTOFS_H_ */ 195