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 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/systm.h> 34 #include <sys/lock.h> 35 #include <sys/malloc.h> 36 #include <sys/module.h> 37 #include <sys/mount.h> 38 #include <sys/mutex.h> 39 #include <sys/proc.h> 40 #include <sys/sbuf.h> 41 #include <sys/sysctl.h> 42 #include <sys/vnode.h> 43 44 #include <fs/pseudofs/pseudofs.h> 45 #include <fs/pseudofs/pseudofs_internal.h> 46 47 static MALLOC_DEFINE(M_PFSNODES, "pfs_nodes", "pseudofs nodes"); 48 49 SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW, 0, 50 "pseudofs"); 51 52 #if PFS_FSNAMELEN != MFSNAMELEN 53 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN" 54 #endif 55 56 /* 57 * Add a node to a directory 58 */ 59 static int 60 _pfs_add_node(struct pfs_node *parent, struct pfs_node *node) 61 { 62 KASSERT(parent != NULL, 63 (__FUNCTION__ "(): parent is NULL")); 64 KASSERT(parent->pn_info != NULL, 65 (__FUNCTION__ "(): parent has no pn_info")); 66 KASSERT(parent->pn_type == pfstype_dir || 67 parent->pn_type == pfstype_procdir || 68 parent->pn_type == pfstype_root, 69 (__FUNCTION__ "(): parent is not a directory")); 70 71 /* XXX should check for duplicate names etc. */ 72 73 mtx_lock(&parent->pn_info->pi_mutex); 74 node->pn_info = parent->pn_info; 75 node->pn_parent = parent; 76 node->pn_next = parent->pn_nodes; 77 parent->pn_nodes = node; 78 mtx_unlock(&parent->pn_info->pi_mutex); 79 80 return (0); 81 } 82 83 /* 84 * Add . and .. to a directory 85 */ 86 static int 87 _pfs_fixup_dir(struct pfs_node *parent) 88 { 89 struct pfs_node *dir; 90 91 MALLOC(dir, struct pfs_node *, sizeof *dir, 92 M_PFSNODES, M_WAITOK|M_ZERO); 93 dir->pn_name[0] = '.'; 94 dir->pn_type = pfstype_this; 95 96 if (_pfs_add_node(parent, dir) != 0) { 97 FREE(dir, M_PFSNODES); 98 return (-1); 99 } 100 101 MALLOC(dir, struct pfs_node *, sizeof *dir, 102 M_PFSNODES, M_WAITOK|M_ZERO); 103 dir->pn_name[0] = dir->pn_name[1] = '.'; 104 dir->pn_type = pfstype_parent; 105 106 if (_pfs_add_node(parent, dir) != 0) { 107 FREE(dir, M_PFSNODES); 108 return (-1); 109 } 110 111 return (0); 112 } 113 114 /* 115 * Create a directory 116 */ 117 struct pfs_node * 118 pfs_create_dir(struct pfs_node *parent, char *name, 119 pfs_attr_t attr, pfs_vis_t vis, int flags) 120 { 121 struct pfs_node *dir; 122 123 KASSERT(strlen(name) < PFS_NAMELEN, 124 (__FUNCTION__ "(): node name is too long")); 125 126 MALLOC(dir, struct pfs_node *, sizeof *dir, 127 M_PFSNODES, M_WAITOK|M_ZERO); 128 strcpy(dir->pn_name, name); 129 dir->pn_type = (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir; 130 dir->pn_attr = attr; 131 dir->pn_vis = vis; 132 dir->pn_flags = flags & ~PFS_PROCDEP; 133 134 if (_pfs_add_node(parent, dir) != 0) { 135 FREE(dir, M_PFSNODES); 136 return (NULL); 137 } 138 139 if (_pfs_fixup_dir(dir) != 0) { 140 pfs_destroy(dir); 141 return (NULL); 142 } 143 144 return (dir); 145 } 146 147 /* 148 * Create a file 149 */ 150 struct pfs_node * 151 pfs_create_file(struct pfs_node *parent, char *name, pfs_fill_t fill, 152 pfs_attr_t attr, pfs_vis_t vis, int flags) 153 { 154 struct pfs_node *node; 155 156 KASSERT(strlen(name) < PFS_NAMELEN, 157 (__FUNCTION__ "(): node name is too long")); 158 159 MALLOC(node, struct pfs_node *, sizeof *node, 160 M_PFSNODES, M_WAITOK|M_ZERO); 161 strcpy(node->pn_name, name); 162 node->pn_type = pfstype_file; 163 node->pn_func = fill; 164 node->pn_attr = attr; 165 node->pn_vis = vis; 166 node->pn_flags = flags; 167 168 if (_pfs_add_node(parent, node) != 0) { 169 FREE(node, M_PFSNODES); 170 return (NULL); 171 } 172 173 return (node); 174 } 175 176 /* 177 * Create a symlink 178 */ 179 struct pfs_node * 180 pfs_create_link(struct pfs_node *parent, char *name, pfs_fill_t fill, 181 pfs_attr_t attr, pfs_vis_t vis, int flags) 182 { 183 struct pfs_node *node; 184 185 node = pfs_create_file(parent, name, fill, attr, vis, flags); 186 if (node == NULL) 187 return (NULL); 188 node->pn_type = pfstype_symlink; 189 return (node); 190 } 191 192 /* 193 * Destroy a node or a tree of nodes 194 */ 195 int 196 pfs_destroy(struct pfs_node *node) 197 { 198 struct pfs_node *parent, *rover; 199 200 KASSERT(node != NULL, 201 (__FUNCTION__ "(): node is NULL")); 202 KASSERT(node->pn_info != NULL, 203 (__FUNCTION__ "(): node has no pn_info")); 204 205 /* destroy children */ 206 if (node->pn_type == pfstype_dir || 207 node->pn_type == pfstype_procdir || 208 node->pn_type == pfstype_root) 209 while (node->pn_nodes != NULL) 210 pfs_destroy(node->pn_nodes); 211 212 /* unlink from parent */ 213 if ((parent = node->pn_parent) != NULL) { 214 KASSERT(parent->pn_info == node->pn_info, 215 (__FUNCTION__ "(): parent has different pn_info")); 216 mtx_lock(&node->pn_info->pi_mutex); 217 if (parent->pn_nodes == node) { 218 parent->pn_nodes = node->pn_next; 219 } else { 220 rover = parent->pn_nodes; 221 while (rover->pn_next != NULL) { 222 if (rover->pn_next == node) { 223 rover->pn_next = node->pn_next; 224 break; 225 } 226 rover = rover->pn_next; 227 } 228 } 229 mtx_unlock(&node->pn_info->pi_mutex); 230 } 231 232 /* revoke vnodes and release memory */ 233 pfs_disable(node); 234 FREE(node, M_PFSNODES); 235 236 return (0); 237 } 238 239 /* 240 * Mount a pseudofs instance 241 */ 242 int 243 pfs_mount(struct pfs_info *pi, struct mount *mp, char *path, caddr_t data, 244 struct nameidata *ndp, struct thread *td) 245 { 246 struct statfs *sbp; 247 248 if (mp->mnt_flag & MNT_UPDATE) 249 return (EOPNOTSUPP); 250 251 mp->mnt_flag |= MNT_LOCAL; 252 mp->mnt_data = (qaddr_t)pi; 253 vfs_getnewfsid(mp); 254 255 sbp = &mp->mnt_stat; 256 bcopy(pi->pi_name, sbp->f_mntfromname, sizeof pi->pi_name); 257 sbp->f_bsize = PAGE_SIZE; 258 sbp->f_iosize = PAGE_SIZE; 259 sbp->f_blocks = 1; 260 sbp->f_bfree = 0; 261 sbp->f_bavail = 0; 262 sbp->f_files = 1; 263 sbp->f_ffree = 0; 264 265 return (0); 266 } 267 268 /* 269 * Unmount a pseudofs instance 270 */ 271 int 272 pfs_unmount(struct mount *mp, int mntflags, struct thread *td) 273 { 274 struct pfs_info *pi; 275 int error; 276 277 pi = (struct pfs_info *)mp->mnt_data; 278 279 /* XXX do stuff with pi... */ 280 281 error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0); 282 return (error); 283 } 284 285 /* 286 * Return a root vnode 287 */ 288 int 289 pfs_root(struct mount *mp, struct vnode **vpp) 290 { 291 struct pfs_info *pi; 292 293 pi = (struct pfs_info *)mp->mnt_data; 294 return pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID); 295 } 296 297 /* 298 * Return filesystem stats 299 */ 300 int 301 pfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 302 { 303 bcopy(&mp->mnt_stat, sbp, sizeof *sbp); 304 return (0); 305 } 306 307 /* 308 * Initialize a pseudofs instance 309 */ 310 int 311 pfs_init(struct pfs_info *pi, struct vfsconf *vfc) 312 { 313 struct pfs_node *root; 314 int error; 315 316 mtx_init(&pi->pi_mutex, "pseudofs", MTX_DEF); 317 318 /* set up the root diretory */ 319 MALLOC(root, struct pfs_node *, sizeof *root, 320 M_PFSNODES, M_WAITOK|M_ZERO); 321 root->pn_type = pfstype_root; 322 root->pn_name[0] = '/'; 323 root->pn_info = pi; 324 if (_pfs_fixup_dir(root) != 0) { 325 FREE(root, M_PFSNODES); 326 return (ENODEV); /* XXX not really the right errno */ 327 } 328 pi->pi_root = root; 329 330 /* construct file hierarchy */ 331 error = (pi->pi_init)(pi, vfc); 332 if (error) { 333 pfs_destroy(root); 334 pi->pi_root = NULL; 335 mtx_destroy(&pi->pi_mutex); 336 return (error); 337 } 338 339 pfs_fileno_init(pi); 340 if (bootverbose) 341 printf("%s registered\n", pi->pi_name); 342 return (0); 343 } 344 345 /* 346 * Destroy a pseudofs instance 347 */ 348 int 349 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc) 350 { 351 int error; 352 353 pfs_fileno_uninit(pi); 354 pfs_destroy(pi->pi_root); 355 pi->pi_root = NULL; 356 mtx_destroy(&pi->pi_mutex); 357 if (bootverbose) 358 printf("%s unregistered\n", pi->pi_name); 359 error = (pi->pi_uninit)(pi, vfc); 360 return (error); 361 } 362 363 /* 364 * Handle load / unload events 365 */ 366 static int 367 pfs_modevent(module_t mod, int evt, void *arg) 368 { 369 switch (evt) { 370 case MOD_LOAD: 371 pfs_fileno_load(); 372 pfs_vncache_load(); 373 break; 374 case MOD_UNLOAD: 375 case MOD_SHUTDOWN: 376 pfs_vncache_unload(); 377 pfs_fileno_unload(); 378 break; 379 default: 380 printf("pseudofs: unexpected event type %d\n", evt); 381 break; 382 } 383 return 0; 384 } 385 386 /* 387 * Module declaration 388 */ 389 static moduledata_t pseudofs_data = { 390 "pseudofs", 391 pfs_modevent, 392 NULL 393 }; 394 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST); 395 MODULE_VERSION(pseudofs, 1); 396