1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include "opt_pseudofs.h" 35 36 #include <sys/param.h> 37 #include <sys/kernel.h> 38 #include <sys/systm.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/mount.h> 43 #include <sys/mutex.h> 44 #include <sys/proc.h> 45 #include <sys/sbuf.h> 46 #include <sys/sysctl.h> 47 #include <sys/vnode.h> 48 49 #include <fs/pseudofs/pseudofs.h> 50 #include <fs/pseudofs/pseudofs_internal.h> 51 52 static MALLOC_DEFINE(M_PFSNODES, "pfs_nodes", "pseudofs nodes"); 53 54 SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 55 "pseudofs"); 56 57 #ifdef PSEUDOFS_TRACE 58 int pfs_trace; 59 SYSCTL_INT(_vfs_pfs, OID_AUTO, trace, CTLFLAG_RW, &pfs_trace, 0, 60 "enable tracing of pseudofs vnode operations"); 61 #endif 62 63 #if PFS_FSNAMELEN != MFSNAMELEN 64 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN" 65 #endif 66 67 /* 68 * Allocate and initialize a node 69 */ 70 static struct pfs_node * 71 pfs_alloc_node_flags(struct pfs_info *pi, const char *name, pfs_type_t type, int flags) 72 { 73 struct pfs_node *pn; 74 int malloc_flags; 75 size_t len; 76 77 len = strlen(name); 78 KASSERT(len < PFS_NAMELEN, 79 ("%s(): node name is too long", __func__)); 80 if (flags & PFS_NOWAIT) 81 malloc_flags = M_NOWAIT | M_ZERO; 82 else 83 malloc_flags = M_WAITOK | M_ZERO; 84 pn = malloc(sizeof(*pn) + len + 1, M_PFSNODES, malloc_flags); 85 if (pn == NULL) 86 return (NULL); 87 mtx_init(&pn->pn_mutex, "pfs_node", NULL, MTX_DEF | MTX_DUPOK); 88 memcpy(pn->pn_name, name, len); 89 pn->pn_type = type; 90 pn->pn_info = pi; 91 return (pn); 92 } 93 94 static struct pfs_node * 95 pfs_alloc_node(struct pfs_info *pi, const char *name, pfs_type_t type) 96 { 97 return (pfs_alloc_node_flags(pi, name, type, 0)); 98 } 99 100 /* 101 * Add a node to a directory 102 */ 103 static void 104 pfs_add_node(struct pfs_node *parent, struct pfs_node *pn) 105 { 106 #ifdef INVARIANTS 107 struct pfs_node *iter; 108 #endif 109 110 KASSERT(parent != NULL, 111 ("%s(): parent is NULL", __func__)); 112 KASSERT(pn->pn_parent == NULL, 113 ("%s(): node already has a parent", __func__)); 114 KASSERT(parent->pn_info != NULL, 115 ("%s(): parent has no pn_info", __func__)); 116 KASSERT(parent->pn_type == pfstype_dir || 117 parent->pn_type == pfstype_procdir || 118 parent->pn_type == pfstype_root, 119 ("%s(): parent is not a directory", __func__)); 120 121 #ifdef INVARIANTS 122 /* XXX no locking! */ 123 if (pn->pn_type == pfstype_procdir) 124 for (iter = parent; iter != NULL; iter = iter->pn_parent) 125 KASSERT(iter->pn_type != pfstype_procdir, 126 ("%s(): nested process directories", __func__)); 127 for (iter = parent->pn_nodes; iter != NULL; iter = iter->pn_next) { 128 KASSERT(strcmp(pn->pn_name, iter->pn_name) != 0, 129 ("%s(): homonymous siblings", __func__)); 130 if (pn->pn_type == pfstype_procdir) 131 KASSERT(iter->pn_type != pfstype_procdir, 132 ("%s(): sibling process directories", __func__)); 133 } 134 #endif 135 136 pn->pn_parent = parent; 137 pfs_fileno_alloc(pn); 138 139 pfs_lock(parent); 140 if ((parent->pn_flags & PFS_PROCDEP) != 0) 141 pn->pn_flags |= PFS_PROCDEP; 142 if (parent->pn_nodes == NULL) { 143 KASSERT(parent->pn_last_node == NULL, 144 ("%s(): pn_last_node not NULL", __func__)); 145 parent->pn_nodes = pn; 146 parent->pn_last_node = pn; 147 } else { 148 KASSERT(parent->pn_last_node != NULL, 149 ("%s(): pn_last_node is NULL", __func__)); 150 KASSERT(parent->pn_last_node->pn_next == NULL, 151 ("%s(): pn_last_node->pn_next not NULL", __func__)); 152 parent->pn_last_node->pn_next = pn; 153 parent->pn_last_node = pn; 154 } 155 pfs_unlock(parent); 156 } 157 158 /* 159 * Detach a node from its aprent 160 */ 161 static void 162 pfs_detach_node(struct pfs_node *pn) 163 { 164 struct pfs_node *node, *parent = pn->pn_parent; 165 struct pfs_node **iter; 166 167 KASSERT(parent != NULL, ("%s(): node has no parent", __func__)); 168 KASSERT(parent->pn_info == pn->pn_info, 169 ("%s(): parent has different pn_info", __func__)); 170 171 pfs_lock(parent); 172 if (pn == parent->pn_last_node) { 173 if (pn == pn->pn_nodes) { 174 parent->pn_last_node = NULL; 175 } else { 176 for (node = parent->pn_nodes; 177 node->pn_next != pn; node = node->pn_next) 178 continue; 179 parent->pn_last_node = node; 180 } 181 } 182 iter = &parent->pn_nodes; 183 while (*iter != NULL) { 184 if (*iter == pn) { 185 *iter = pn->pn_next; 186 break; 187 } 188 iter = &(*iter)->pn_next; 189 } 190 pn->pn_parent = NULL; 191 pfs_unlock(parent); 192 } 193 194 /* 195 * Add . and .. to a directory 196 */ 197 static int 198 pfs_fixup_dir_flags(struct pfs_node *parent, int flags) 199 { 200 struct pfs_node *dot, *dotdot; 201 202 dot = pfs_alloc_node_flags(parent->pn_info, ".", pfstype_this, flags); 203 if (dot == NULL) 204 return (ENOMEM); 205 dotdot = pfs_alloc_node_flags(parent->pn_info, "..", pfstype_parent, flags); 206 if (dotdot == NULL) { 207 pfs_destroy(dot); 208 return (ENOMEM); 209 } 210 pfs_add_node(parent, dot); 211 pfs_add_node(parent, dotdot); 212 return (0); 213 } 214 215 static void 216 pfs_fixup_dir(struct pfs_node *parent) 217 { 218 219 pfs_fixup_dir_flags(parent, 0); 220 } 221 222 /* 223 * Create a directory 224 */ 225 struct pfs_node * 226 pfs_create_dir(struct pfs_node *parent, const char *name, 227 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy, 228 int flags) 229 { 230 struct pfs_node *pn; 231 int rc; 232 233 pn = pfs_alloc_node_flags(parent->pn_info, name, 234 (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir, flags); 235 if (pn == NULL) 236 return (NULL); 237 pn->pn_attr = attr; 238 pn->pn_vis = vis; 239 pn->pn_destroy = destroy; 240 pn->pn_flags = flags; 241 pfs_add_node(parent, pn); 242 rc = pfs_fixup_dir_flags(pn, flags); 243 if (rc) { 244 pfs_destroy(pn); 245 return (NULL); 246 } 247 return (pn); 248 } 249 250 /* 251 * Create a file 252 */ 253 struct pfs_node * 254 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill, 255 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy, 256 int flags) 257 { 258 struct pfs_node *pn; 259 260 pn = pfs_alloc_node_flags(parent->pn_info, name, pfstype_file, flags); 261 if (pn == NULL) 262 return (NULL); 263 pn->pn_fill = fill; 264 pn->pn_attr = attr; 265 pn->pn_vis = vis; 266 pn->pn_destroy = destroy; 267 pn->pn_flags = flags; 268 pfs_add_node(parent, pn); 269 270 return (pn); 271 } 272 273 /* 274 * Create a symlink 275 */ 276 struct pfs_node * 277 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill, 278 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy, 279 int flags) 280 { 281 struct pfs_node *pn; 282 283 pn = pfs_alloc_node_flags(parent->pn_info, name, pfstype_symlink, flags); 284 if (pn == NULL) 285 return (NULL); 286 pn->pn_fill = fill; 287 pn->pn_attr = attr; 288 pn->pn_vis = vis; 289 pn->pn_destroy = destroy; 290 pn->pn_flags = flags; 291 pfs_add_node(parent, pn); 292 293 return (pn); 294 } 295 296 /* 297 * Locate a node by name 298 */ 299 struct pfs_node * 300 pfs_find_node(struct pfs_node *parent, const char *name) 301 { 302 struct pfs_node *pn; 303 304 pfs_lock(parent); 305 for (pn = parent->pn_nodes; pn != NULL; pn = pn->pn_next) 306 if (strcmp(pn->pn_name, name) == 0) 307 break; 308 pfs_unlock(parent); 309 return (pn); 310 } 311 312 /* 313 * Destroy a node and all its descendants. If the node to be destroyed 314 * has a parent, the parent's mutex must be held. 315 */ 316 int 317 pfs_destroy(struct pfs_node *pn) 318 { 319 struct pfs_node *iter; 320 321 KASSERT(pn != NULL, 322 ("%s(): node is NULL", __func__)); 323 KASSERT(pn->pn_info != NULL, 324 ("%s(): node has no pn_info", __func__)); 325 326 if (pn->pn_parent) 327 pfs_detach_node(pn); 328 329 /* destroy children */ 330 if (pn->pn_type == pfstype_dir || 331 pn->pn_type == pfstype_procdir || 332 pn->pn_type == pfstype_root) { 333 pfs_lock(pn); 334 while (pn->pn_nodes != NULL) { 335 iter = pn->pn_nodes; 336 pn->pn_nodes = iter->pn_next; 337 iter->pn_parent = NULL; 338 pfs_unlock(pn); 339 pfs_destroy(iter); 340 pfs_lock(pn); 341 } 342 pfs_unlock(pn); 343 } 344 345 /* revoke vnodes and fileno */ 346 pfs_purge(pn); 347 348 /* callback to free any private resources */ 349 if (pn->pn_destroy != NULL) 350 pn_destroy(pn); 351 352 /* destroy the node */ 353 pfs_fileno_free(pn); 354 mtx_destroy(&pn->pn_mutex); 355 free(pn, M_PFSNODES); 356 357 return (0); 358 } 359 360 /* 361 * Mount a pseudofs instance 362 */ 363 int 364 pfs_mount(struct pfs_info *pi, struct mount *mp) 365 { 366 struct statfs *sbp; 367 368 if (mp->mnt_flag & MNT_UPDATE) 369 return (EOPNOTSUPP); 370 371 MNT_ILOCK(mp); 372 mp->mnt_flag |= MNT_LOCAL; 373 mp->mnt_kern_flag |= MNTK_NOMSYNC; 374 MNT_IUNLOCK(mp); 375 mp->mnt_data = pi; 376 vfs_getnewfsid(mp); 377 378 sbp = &mp->mnt_stat; 379 vfs_mountedfrom(mp, pi->pi_name); 380 sbp->f_bsize = PAGE_SIZE; 381 sbp->f_iosize = PAGE_SIZE; 382 sbp->f_blocks = 2; 383 sbp->f_bfree = 2; 384 sbp->f_bavail = 2; 385 sbp->f_files = 0; 386 sbp->f_ffree = 0; 387 388 return (0); 389 } 390 391 /* 392 * Compatibility shim for old mount(2) system call 393 */ 394 int 395 pfs_cmount(struct mntarg *ma, void *data, uint64_t flags) 396 { 397 int error; 398 399 error = kernel_mount(ma, flags); 400 return (error); 401 } 402 403 /* 404 * Unmount a pseudofs instance 405 */ 406 int 407 pfs_unmount(struct mount *mp, int mntflags) 408 { 409 int error; 410 411 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0, 412 curthread); 413 return (error); 414 } 415 416 /* 417 * Return a root vnode 418 */ 419 int 420 pfs_root(struct mount *mp, int flags, struct vnode **vpp) 421 { 422 struct pfs_info *pi; 423 424 pi = (struct pfs_info *)mp->mnt_data; 425 return (pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID)); 426 } 427 428 /* 429 * Return filesystem stats 430 */ 431 int 432 pfs_statfs(struct mount *mp, struct statfs *sbp) 433 { 434 /* no-op: always called with mp->mnt_stat */ 435 return (0); 436 } 437 438 /* 439 * Initialize a pseudofs instance 440 */ 441 int 442 pfs_init(struct pfs_info *pi, struct vfsconf *vfc) 443 { 444 struct pfs_node *root; 445 int error; 446 447 pfs_fileno_init(pi); 448 449 /* set up the root directory */ 450 root = pfs_alloc_node(pi, "/", pfstype_root); 451 pi->pi_root = root; 452 pfs_fileno_alloc(root); 453 pfs_fixup_dir(root); 454 455 /* construct file hierarchy */ 456 error = (pi->pi_init)(pi, vfc); 457 if (error) { 458 pfs_destroy(root); 459 pi->pi_root = NULL; 460 return (error); 461 } 462 463 if (bootverbose) 464 printf("%s registered\n", pi->pi_name); 465 return (0); 466 } 467 468 /* 469 * Destroy a pseudofs instance 470 */ 471 int 472 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc) 473 { 474 int error; 475 476 pfs_destroy(pi->pi_root); 477 pi->pi_root = NULL; 478 pfs_fileno_uninit(pi); 479 if (bootverbose) 480 printf("%s unregistered\n", pi->pi_name); 481 error = (pi->pi_uninit)(pi, vfc); 482 return (error); 483 } 484 485 /* 486 * Handle load / unload events 487 */ 488 static int 489 pfs_modevent(module_t mod, int evt, void *arg) 490 { 491 switch (evt) { 492 case MOD_LOAD: 493 pfs_vncache_load(); 494 break; 495 case MOD_UNLOAD: 496 case MOD_SHUTDOWN: 497 pfs_vncache_unload(); 498 break; 499 default: 500 return EOPNOTSUPP; 501 break; 502 } 503 return 0; 504 } 505 506 /* 507 * Module declaration 508 */ 509 static moduledata_t pseudofs_data = { 510 "pseudofs", 511 pfs_modevent, 512 NULL 513 }; 514 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST); 515 MODULE_VERSION(pseudofs, 1); 516