1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2001 Dag-Erling 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 #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 | CTLFLAG_MPSAFE, 0, 53 "pseudofs"); 54 55 #ifdef PSEUDOFS_TRACE 56 int pfs_trace; 57 SYSCTL_INT(_vfs_pfs, OID_AUTO, trace, CTLFLAG_RW, &pfs_trace, 0, 58 "enable tracing of pseudofs vnode operations"); 59 #endif 60 61 #if PFS_FSNAMELEN != MFSNAMELEN 62 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN" 63 #endif 64 65 /* 66 * Allocate and initialize a node 67 */ 68 static struct pfs_node * 69 pfs_alloc_node_flags(struct pfs_info *pi, const char *name, pfs_type_t type, int flags) 70 { 71 struct pfs_node *pn; 72 int malloc_flags; 73 size_t len; 74 75 len = strlen(name); 76 KASSERT(len < PFS_NAMELEN, 77 ("%s(): node name is too long", __func__)); 78 if (flags & PFS_NOWAIT) 79 malloc_flags = M_NOWAIT | M_ZERO; 80 else 81 malloc_flags = M_WAITOK | M_ZERO; 82 pn = malloc(sizeof(*pn) + len + 1, M_PFSNODES, malloc_flags); 83 if (pn == NULL) 84 return (NULL); 85 mtx_init(&pn->pn_mutex, "pfs_node", NULL, MTX_DEF | MTX_DUPOK); 86 memcpy(pn->pn_name, name, len); 87 pn->pn_type = type; 88 pn->pn_info = pi; 89 return (pn); 90 } 91 92 static struct pfs_node * 93 pfs_alloc_node(struct pfs_info *pi, const char *name, pfs_type_t type) 94 { 95 return (pfs_alloc_node_flags(pi, name, type, 0)); 96 } 97 98 /* 99 * Add a node to a directory 100 */ 101 static int 102 pfs_add_node(struct pfs_node *parent, struct pfs_node *pn) 103 { 104 struct pfs_node *iter; 105 106 KASSERT(parent != NULL, 107 ("%s(): parent is NULL", __func__)); 108 KASSERT(pn->pn_parent == NULL, 109 ("%s(): node already has a parent", __func__)); 110 KASSERT(parent->pn_info != NULL, 111 ("%s(): parent has no pn_info", __func__)); 112 KASSERT(parent->pn_type == pfstype_dir || 113 parent->pn_type == pfstype_procdir || 114 parent->pn_type == pfstype_root, 115 ("%s(): parent is not a directory", __func__)); 116 117 #ifdef INVARIANTS 118 /* XXX no locking! */ 119 if (pn->pn_type == pfstype_procdir) 120 for (iter = parent; iter != NULL; iter = iter->pn_parent) 121 KASSERT(iter->pn_type != pfstype_procdir, 122 ("%s(): nested process directories", __func__)); 123 for (iter = parent->pn_nodes; iter != NULL; iter = iter->pn_next) { 124 if (pn->pn_type == pfstype_procdir) 125 KASSERT(iter->pn_type != pfstype_procdir, 126 ("%s(): sibling process directories", __func__)); 127 } 128 #endif 129 130 pn->pn_parent = parent; 131 pfs_fileno_alloc(pn); 132 pfs_lock(parent); 133 for (iter = parent->pn_nodes; iter != NULL; iter = iter->pn_next) { 134 if (strcmp(pn->pn_name, iter->pn_name) != 0) 135 continue; 136 printf("pfs_add_node: homonymous siblings: '%s/%s' type %d", 137 parent->pn_name, pn->pn_name, pn->pn_type); 138 /* Do not detach, because we are not yet attached. */ 139 pn->pn_parent = NULL; 140 pfs_unlock(parent); 141 return (EEXIST); 142 } 143 144 145 if ((parent->pn_flags & PFS_PROCDEP) != 0) 146 pn->pn_flags |= PFS_PROCDEP; 147 if (parent->pn_nodes == NULL) { 148 KASSERT(parent->pn_last_node == NULL, 149 ("%s(): pn_last_node not NULL", __func__)); 150 parent->pn_nodes = pn; 151 parent->pn_last_node = pn; 152 } else { 153 KASSERT(parent->pn_last_node != NULL, 154 ("%s(): pn_last_node is NULL", __func__)); 155 KASSERT(parent->pn_last_node->pn_next == NULL, 156 ("%s(): pn_last_node->pn_next not NULL", __func__)); 157 parent->pn_last_node->pn_next = pn; 158 parent->pn_last_node = pn; 159 } 160 pfs_unlock(parent); 161 return (0); 162 } 163 164 /* 165 * Detach a node from its parent 166 */ 167 static void 168 pfs_detach_node(struct pfs_node *pn) 169 { 170 struct pfs_node *node, *parent = pn->pn_parent; 171 struct pfs_node **iter; 172 173 KASSERT(parent != NULL, ("%s(): node has no parent", __func__)); 174 KASSERT(parent->pn_info == pn->pn_info, 175 ("%s(): parent has different pn_info", __func__)); 176 177 pfs_lock(parent); 178 if (pn == parent->pn_last_node) { 179 if (pn == pn->pn_nodes) { 180 parent->pn_last_node = NULL; 181 } else { 182 for (node = parent->pn_nodes; 183 node->pn_next != pn; node = node->pn_next) 184 continue; 185 parent->pn_last_node = node; 186 } 187 } 188 iter = &parent->pn_nodes; 189 while (*iter != NULL) { 190 if (*iter == pn) { 191 *iter = pn->pn_next; 192 break; 193 } 194 iter = &(*iter)->pn_next; 195 } 196 pn->pn_parent = NULL; 197 pfs_unlock(parent); 198 } 199 200 /* 201 * Add . and .. to a directory 202 */ 203 static int 204 pfs_fixup_dir_flags(struct pfs_node *parent, int flags) 205 { 206 struct pfs_node *dot, *dotdot; 207 int rc; 208 209 dot = pfs_alloc_node_flags(parent->pn_info, ".", pfstype_this, flags); 210 if (dot == NULL) 211 return (ENOMEM); 212 dotdot = pfs_alloc_node_flags(parent->pn_info, "..", pfstype_parent, flags); 213 if (dotdot == NULL) { 214 pfs_destroy(dot); 215 return (ENOMEM); 216 } 217 rc = pfs_add_node(parent, dot); 218 if (rc == 0) 219 rc = pfs_add_node(parent, dotdot); 220 if (rc != 0) { 221 pfs_destroy(dot); 222 pfs_destroy(dotdot); 223 } 224 return (rc); 225 } 226 227 static void 228 pfs_fixup_dir(struct pfs_node *parent) 229 { 230 231 pfs_fixup_dir_flags(parent, 0); 232 } 233 234 /* 235 * Create a directory 236 */ 237 struct pfs_node * 238 pfs_create_dir(struct pfs_node *parent, const char *name, 239 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy, 240 int flags) 241 { 242 struct pfs_node *pn; 243 int rc; 244 245 pn = pfs_alloc_node_flags(parent->pn_info, name, 246 (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir, flags); 247 if (pn == NULL) 248 return (NULL); 249 pn->pn_attr = attr; 250 pn->pn_vis = vis; 251 pn->pn_destroy = destroy; 252 pn->pn_flags = flags; 253 rc = pfs_add_node(parent, pn); 254 if (rc == 0) 255 rc = pfs_fixup_dir_flags(pn, flags); 256 if (rc != 0) { 257 pfs_destroy(pn); 258 pn = NULL; 259 } 260 return (pn); 261 } 262 263 /* 264 * Create a file 265 */ 266 struct pfs_node * 267 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill, 268 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy, 269 int flags) 270 { 271 struct pfs_node *pn; 272 273 pn = pfs_alloc_node_flags(parent->pn_info, name, pfstype_file, flags); 274 if (pn == NULL) 275 return (NULL); 276 pn->pn_fill = fill; 277 pn->pn_attr = attr; 278 pn->pn_vis = vis; 279 pn->pn_destroy = destroy; 280 pn->pn_flags = flags; 281 if (pfs_add_node(parent, pn) != 0) { 282 pfs_destroy(pn); 283 pn = NULL; 284 } 285 return (pn); 286 } 287 288 /* 289 * Create a symlink 290 */ 291 struct pfs_node * 292 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill, 293 pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy, 294 int flags) 295 { 296 struct pfs_node *pn; 297 298 pn = pfs_alloc_node_flags(parent->pn_info, name, pfstype_symlink, flags); 299 if (pn == NULL) 300 return (NULL); 301 pn->pn_fill = fill; 302 pn->pn_attr = attr; 303 pn->pn_vis = vis; 304 pn->pn_destroy = destroy; 305 pn->pn_flags = flags; 306 if (pfs_add_node(parent, pn) != 0) { 307 pfs_destroy(pn); 308 pn = NULL; 309 } 310 311 return (pn); 312 } 313 314 /* 315 * Locate a node by name 316 */ 317 struct pfs_node * 318 pfs_find_node(struct pfs_node *parent, const char *name) 319 { 320 struct pfs_node *pn; 321 322 pfs_lock(parent); 323 for (pn = parent->pn_nodes; pn != NULL; pn = pn->pn_next) 324 if (strcmp(pn->pn_name, name) == 0) 325 break; 326 pfs_unlock(parent); 327 return (pn); 328 } 329 330 /* 331 * Destroy a node and all its descendants. If the node to be destroyed 332 * has a parent, the parent's mutex must be held. 333 */ 334 int 335 pfs_destroy(struct pfs_node *pn) 336 { 337 struct pfs_node *iter; 338 339 KASSERT(pn != NULL, 340 ("%s(): node is NULL", __func__)); 341 KASSERT(pn->pn_info != NULL, 342 ("%s(): node has no pn_info", __func__)); 343 344 if (pn->pn_parent) 345 pfs_detach_node(pn); 346 347 /* destroy children */ 348 if (pn->pn_type == pfstype_dir || 349 pn->pn_type == pfstype_procdir || 350 pn->pn_type == pfstype_root) { 351 pfs_lock(pn); 352 while (pn->pn_nodes != NULL) { 353 iter = pn->pn_nodes; 354 pn->pn_nodes = iter->pn_next; 355 iter->pn_parent = NULL; 356 pfs_unlock(pn); 357 pfs_destroy(iter); 358 pfs_lock(pn); 359 } 360 pfs_unlock(pn); 361 } 362 363 /* revoke vnodes and fileno */ 364 pfs_purge(pn); 365 366 /* callback to free any private resources */ 367 if (pn->pn_destroy != NULL) 368 pn_destroy(pn); 369 370 /* destroy the node */ 371 pfs_fileno_free(pn); 372 mtx_destroy(&pn->pn_mutex); 373 free(pn, M_PFSNODES); 374 375 return (0); 376 } 377 378 /* 379 * Mount a pseudofs instance 380 */ 381 int 382 pfs_mount(struct pfs_info *pi, struct mount *mp) 383 { 384 struct statfs *sbp; 385 386 if (mp->mnt_flag & MNT_UPDATE) 387 return (EOPNOTSUPP); 388 389 MNT_ILOCK(mp); 390 mp->mnt_flag |= MNT_LOCAL; 391 mp->mnt_kern_flag |= MNTK_NOMSYNC; 392 MNT_IUNLOCK(mp); 393 mp->mnt_data = pi; 394 vfs_getnewfsid(mp); 395 396 sbp = &mp->mnt_stat; 397 vfs_mountedfrom(mp, pi->pi_name); 398 sbp->f_bsize = PAGE_SIZE; 399 sbp->f_iosize = PAGE_SIZE; 400 sbp->f_blocks = 2; 401 sbp->f_bfree = 2; 402 sbp->f_bavail = 2; 403 sbp->f_files = 0; 404 sbp->f_ffree = 0; 405 406 return (0); 407 } 408 409 /* 410 * Compatibility shim for old mount(2) system call 411 */ 412 int 413 pfs_cmount(struct mntarg *ma, void *data, uint64_t flags) 414 { 415 int error; 416 417 error = kernel_mount(ma, flags); 418 return (error); 419 } 420 421 /* 422 * Unmount a pseudofs instance 423 */ 424 int 425 pfs_unmount(struct mount *mp, int mntflags) 426 { 427 int error; 428 429 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0, 430 curthread); 431 return (error); 432 } 433 434 /* 435 * Return a root vnode 436 */ 437 int 438 pfs_root(struct mount *mp, int flags, struct vnode **vpp) 439 { 440 struct pfs_info *pi; 441 442 pi = (struct pfs_info *)mp->mnt_data; 443 return (pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID)); 444 } 445 446 /* 447 * Return filesystem stats 448 */ 449 int 450 pfs_statfs(struct mount *mp, struct statfs *sbp) 451 { 452 /* no-op: always called with mp->mnt_stat */ 453 return (0); 454 } 455 456 /* 457 * Initialize a pseudofs instance 458 */ 459 int 460 pfs_init(struct pfs_info *pi, struct vfsconf *vfc) 461 { 462 struct pfs_node *root; 463 int error; 464 465 pfs_fileno_init(pi); 466 467 /* set up the root directory */ 468 root = pfs_alloc_node(pi, "/", pfstype_root); 469 pi->pi_root = root; 470 pfs_fileno_alloc(root); 471 pfs_fixup_dir(root); 472 473 /* construct file hierarchy */ 474 error = (pi->pi_init)(pi, vfc); 475 if (error) { 476 pfs_destroy(root); 477 pi->pi_root = NULL; 478 return (error); 479 } 480 481 if (bootverbose) 482 printf("%s registered\n", pi->pi_name); 483 return (0); 484 } 485 486 /* 487 * Destroy a pseudofs instance 488 */ 489 int 490 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc) 491 { 492 int error; 493 494 pfs_destroy(pi->pi_root); 495 pi->pi_root = NULL; 496 pfs_fileno_uninit(pi); 497 if (bootverbose) 498 printf("%s unregistered\n", pi->pi_name); 499 error = (pi->pi_uninit)(pi, vfc); 500 return (error); 501 } 502 503 /* 504 * Handle load / unload events 505 */ 506 static int 507 pfs_modevent(module_t mod, int evt, void *arg) 508 { 509 switch (evt) { 510 case MOD_LOAD: 511 pfs_vncache_load(); 512 break; 513 case MOD_UNLOAD: 514 case MOD_SHUTDOWN: 515 pfs_vncache_unload(); 516 break; 517 default: 518 return EOPNOTSUPP; 519 break; 520 } 521 return 0; 522 } 523 524 /* 525 * Module declaration 526 */ 527 static moduledata_t pseudofs_data = { 528 "pseudofs", 529 pfs_modevent, 530 NULL 531 }; 532 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST); 533 MODULE_VERSION(pseudofs, 1); 534