1 /*- 2 * Copyright (c) 2001 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_pseudofs.h" 33 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/systm.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/mount.h> 41 #include <sys/mutex.h> 42 #include <sys/proc.h> 43 #include <sys/sbuf.h> 44 #include <sys/sysctl.h> 45 #include <sys/vnode.h> 46 47 #include <fs/pseudofs/pseudofs.h> 48 #include <fs/pseudofs/pseudofs_internal.h> 49 50 static MALLOC_DEFINE(M_PFSNODES, "pfs_nodes", "pseudofs nodes"); 51 52 SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW, 0, 53 "pseudofs"); 54 55 #if PFS_FSNAMELEN != MFSNAMELEN 56 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN" 57 #endif 58 59 /* 60 * Add a node to a directory 61 */ 62 static int 63 _pfs_add_node(struct pfs_node *parent, struct pfs_node *node) 64 { 65 KASSERT(parent != NULL, 66 ("%s(): parent is NULL", __func__)); 67 KASSERT(parent->pn_info != NULL, 68 ("%s(): parent has no pn_info", __func__)); 69 KASSERT(parent->pn_type == pfstype_dir || 70 parent->pn_type == pfstype_procdir || 71 parent->pn_type == pfstype_root, 72 ("%s(): parent is not a directory", __func__)); 73 74 /* XXX should check for duplicate names etc. */ 75 76 mtx_lock(&parent->pn_info->pi_mutex); 77 node->pn_info = parent->pn_info; 78 node->pn_parent = parent; 79 node->pn_next = parent->pn_nodes; 80 parent->pn_nodes = node; 81 /* Propagate flag to all child nodes (and thus their vnodes) */ 82 if ((parent->pn_flags & PFS_PROCDEP) != 0) 83 node->pn_flags |= PFS_PROCDEP; 84 mtx_unlock(&parent->pn_info->pi_mutex); 85 86 return (0); 87 } 88 89 /* 90 * Add . and .. to a directory 91 */ 92 static int 93 _pfs_fixup_dir(struct pfs_node *parent) 94 { 95 struct pfs_node *dir; 96 97 MALLOC(dir, struct pfs_node *, sizeof *dir, 98 M_PFSNODES, M_WAITOK|M_ZERO); 99 dir->pn_name[0] = '.'; 100 dir->pn_type = pfstype_this; 101 102 if (_pfs_add_node(parent, dir) != 0) { 103 FREE(dir, M_PFSNODES); 104 return (-1); 105 } 106 107 MALLOC(dir, struct pfs_node *, sizeof *dir, 108 M_PFSNODES, M_WAITOK|M_ZERO); 109 dir->pn_name[0] = dir->pn_name[1] = '.'; 110 dir->pn_type = pfstype_parent; 111 112 if (_pfs_add_node(parent, dir) != 0) { 113 FREE(dir, M_PFSNODES); 114 return (-1); 115 } 116 117 return (0); 118 } 119 120 /* 121 * Create a directory 122 */ 123 struct pfs_node * 124 pfs_create_dir(struct pfs_node *parent, const char *name, 125 pfs_attr_t attr, pfs_vis_t vis, int flags) 126 { 127 struct pfs_node *dir; 128 129 KASSERT(strlen(name) < PFS_NAMELEN, 130 ("%s(): node name is too long", __func__)); 131 132 MALLOC(dir, struct pfs_node *, sizeof *dir, 133 M_PFSNODES, M_WAITOK|M_ZERO); 134 strcpy(dir->pn_name, name); 135 dir->pn_type = (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir; 136 dir->pn_attr = attr; 137 dir->pn_vis = vis; 138 dir->pn_flags = flags; 139 140 if (_pfs_add_node(parent, dir) != 0) { 141 FREE(dir, M_PFSNODES); 142 return (NULL); 143 } 144 145 if (_pfs_fixup_dir(dir) != 0) { 146 pfs_destroy(dir); 147 return (NULL); 148 } 149 150 return (dir); 151 } 152 153 /* 154 * Create a file 155 */ 156 struct pfs_node * 157 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill, 158 pfs_attr_t attr, pfs_vis_t vis, int flags) 159 { 160 struct pfs_node *node; 161 162 KASSERT(strlen(name) < PFS_NAMELEN, 163 ("%s(): node name is too long", __func__)); 164 165 MALLOC(node, struct pfs_node *, sizeof *node, 166 M_PFSNODES, M_WAITOK|M_ZERO); 167 strcpy(node->pn_name, name); 168 node->pn_type = pfstype_file; 169 node->pn_func = fill; 170 node->pn_attr = attr; 171 node->pn_vis = vis; 172 node->pn_flags = flags; 173 174 if (_pfs_add_node(parent, node) != 0) { 175 FREE(node, M_PFSNODES); 176 return (NULL); 177 } 178 179 return (node); 180 } 181 182 /* 183 * Create a symlink 184 */ 185 struct pfs_node * 186 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill, 187 pfs_attr_t attr, pfs_vis_t vis, int flags) 188 { 189 struct pfs_node *node; 190 191 node = pfs_create_file(parent, name, fill, attr, vis, flags); 192 if (node == NULL) 193 return (NULL); 194 node->pn_type = pfstype_symlink; 195 return (node); 196 } 197 198 /* 199 * Locate a node by name 200 */ 201 struct pfs_node * 202 pfs_find_node(struct pfs_node *parent, const char *name) 203 { 204 struct pfs_node *node; 205 206 for (node = parent->pn_nodes; node != NULL; node = node->pn_next) 207 if (strcmp(node->pn_name, name) == 0) 208 return (node); 209 return (NULL); 210 } 211 212 /* 213 * Destroy a node or a tree of nodes 214 */ 215 int 216 pfs_destroy(struct pfs_node *node) 217 { 218 struct pfs_node *parent, *rover; 219 220 KASSERT(node != NULL, 221 ("%s(): node is NULL", __func__)); 222 KASSERT(node->pn_info != NULL, 223 ("%s(): node has no pn_info", __func__)); 224 225 /* destroy children */ 226 if (node->pn_type == pfstype_dir || 227 node->pn_type == pfstype_procdir || 228 node->pn_type == pfstype_root) 229 while (node->pn_nodes != NULL) 230 pfs_destroy(node->pn_nodes); 231 232 /* unlink from parent */ 233 if ((parent = node->pn_parent) != NULL) { 234 KASSERT(parent->pn_info == node->pn_info, 235 ("%s(): parent has different pn_info", __func__)); 236 mtx_lock(&node->pn_info->pi_mutex); 237 if (parent->pn_nodes == node) { 238 parent->pn_nodes = node->pn_next; 239 } else { 240 rover = parent->pn_nodes; 241 while (rover->pn_next != NULL) { 242 if (rover->pn_next == node) { 243 rover->pn_next = node->pn_next; 244 break; 245 } 246 rover = rover->pn_next; 247 } 248 } 249 mtx_unlock(&node->pn_info->pi_mutex); 250 } 251 252 /* revoke vnodes and release memory */ 253 pfs_disable(node); 254 FREE(node, M_PFSNODES); 255 256 return (0); 257 } 258 259 /* 260 * Mount a pseudofs instance 261 */ 262 int 263 pfs_mount(struct pfs_info *pi, struct mount *mp, struct thread *td) 264 { 265 struct statfs *sbp; 266 267 if (mp->mnt_flag & MNT_UPDATE) 268 return (EOPNOTSUPP); 269 270 mp->mnt_flag |= MNT_LOCAL; 271 mp->mnt_data = (qaddr_t)pi; 272 vfs_getnewfsid(mp); 273 274 sbp = &mp->mnt_stat; 275 vfs_mountedfrom(mp, pi->pi_name); 276 sbp->f_bsize = PAGE_SIZE; 277 sbp->f_iosize = PAGE_SIZE; 278 sbp->f_blocks = 1; 279 sbp->f_bfree = 0; 280 sbp->f_bavail = 0; 281 sbp->f_files = 1; 282 sbp->f_ffree = 0; 283 284 return (0); 285 } 286 287 /* 288 * Compatibility shim for old mount(2) system call. 289 */ 290 int 291 pfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td) 292 { 293 return kernel_mount(ma, flags); 294 } 295 296 /* 297 * Unmount a pseudofs instance 298 */ 299 int 300 pfs_unmount(struct mount *mp, int mntflags, struct thread *td) 301 { 302 struct pfs_info *pi; 303 int error; 304 305 pi = (struct pfs_info *)mp->mnt_data; 306 307 /* XXX do stuff with pi... */ 308 309 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0, td); 310 return (error); 311 } 312 313 /* 314 * Return a root vnode 315 */ 316 int 317 pfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) 318 { 319 struct pfs_info *pi; 320 321 pi = (struct pfs_info *)mp->mnt_data; 322 return pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID); 323 } 324 325 /* 326 * Return filesystem stats 327 */ 328 int 329 pfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 330 { 331 /* no-op: always called with mp->mnt_stat */ 332 return (0); 333 } 334 335 /* 336 * Initialize a pseudofs instance 337 */ 338 int 339 pfs_init(struct pfs_info *pi, struct vfsconf *vfc) 340 { 341 struct pfs_node *root; 342 int error; 343 344 mtx_init(&pi->pi_mutex, "pseudofs", NULL, MTX_DEF); 345 346 /* set up the root diretory */ 347 MALLOC(root, struct pfs_node *, sizeof *root, 348 M_PFSNODES, M_WAITOK|M_ZERO); 349 root->pn_type = pfstype_root; 350 root->pn_name[0] = '/'; 351 root->pn_info = pi; 352 if (_pfs_fixup_dir(root) != 0) { 353 FREE(root, M_PFSNODES); 354 return (ENODEV); /* XXX not really the right errno */ 355 } 356 pi->pi_root = root; 357 358 /* construct file hierarchy */ 359 error = (pi->pi_init)(pi, vfc); 360 if (error) { 361 pfs_destroy(root); 362 pi->pi_root = NULL; 363 mtx_destroy(&pi->pi_mutex); 364 return (error); 365 } 366 367 pfs_fileno_init(pi); 368 if (bootverbose) 369 printf("%s registered\n", pi->pi_name); 370 return (0); 371 } 372 373 /* 374 * Destroy a pseudofs instance 375 */ 376 int 377 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc) 378 { 379 int error; 380 381 pfs_fileno_uninit(pi); 382 pfs_destroy(pi->pi_root); 383 pi->pi_root = NULL; 384 mtx_destroy(&pi->pi_mutex); 385 if (bootverbose) 386 printf("%s unregistered\n", pi->pi_name); 387 error = (pi->pi_uninit)(pi, vfc); 388 return (error); 389 } 390 391 /* 392 * Handle load / unload events 393 */ 394 static int 395 pfs_modevent(module_t mod, int evt, void *arg) 396 { 397 switch (evt) { 398 case MOD_LOAD: 399 pfs_vncache_load(); 400 break; 401 case MOD_UNLOAD: 402 case MOD_SHUTDOWN: 403 pfs_vncache_unload(); 404 break; 405 default: 406 return EOPNOTSUPP; 407 break; 408 } 409 return 0; 410 } 411 412 /* 413 * Module declaration 414 */ 415 static moduledata_t pseudofs_data = { 416 "pseudofs", 417 pfs_modevent, 418 NULL 419 }; 420 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST); 421 MODULE_VERSION(pseudofs, 1); 422