1 /* $NetBSD: tmpfs_vfsops.c,v 1.10 2005/12/11 12:24:29 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2005 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 9 * 2005 program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Efficient memory file system. 35 * 36 * tmpfs is a file system that uses NetBSD's virtual memory sub-system 37 * (the well-known UVM) to store file data and metadata in an efficient 38 * way. This means that it does not follow the structure of an on-disk 39 * file system because it simply does not need to. Instead, it uses 40 * memory-specific data structures and algorithms to automatically 41 * allocate and release resources. 42 */ 43 #include <sys/cdefs.h> 44 #include <sys/conf.h> 45 #include <sys/param.h> 46 #include <sys/limits.h> 47 #include <sys/lock.h> 48 #include <sys/mutex.h> 49 #include <sys/kernel.h> 50 #include <sys/stat.h> 51 #include <sys/systm.h> 52 #include <sys/sysctl.h> 53 #include <sys/objcache.h> 54 55 #include <vm/vm.h> 56 #include <vm/vm_object.h> 57 #include <vm/vm_param.h> 58 59 #include <vfs/tmpfs/tmpfs.h> 60 #include <vfs/tmpfs/tmpfs_vnops.h> 61 #include <vfs/tmpfs/tmpfs_args.h> 62 63 /* 64 * Default permission for root node 65 */ 66 #define TMPFS_DEFAULT_ROOT_MODE (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) 67 68 MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures"); 69 MALLOC_DEFINE(M_TMPFSNAME, "tmpfs name", "tmpfs file names"); 70 MALLOC_DEFINE(M_TMPFS_DIRENT, "tmpfs dirent", "tmpfs dirent structures"); 71 MALLOC_DEFINE(M_TMPFS_NODE, "tmpfs node", "tmpfs node structures"); 72 73 /* --------------------------------------------------------------------- */ 74 75 static int tmpfs_mount(struct mount *, char *, caddr_t, struct ucred *); 76 static int tmpfs_unmount(struct mount *, int); 77 static int tmpfs_root(struct mount *, struct vnode **); 78 static int tmpfs_fhtovp(struct mount *, struct vnode *, struct fid *, struct vnode **); 79 static int tmpfs_statfs(struct mount *, struct statfs *, struct ucred *cred); 80 81 /* --------------------------------------------------------------------- */ 82 int 83 tmpfs_node_ctor(void *obj, void *privdata, int flags) 84 { 85 struct tmpfs_node *node = (struct tmpfs_node *)obj; 86 87 node->tn_gen++; 88 node->tn_size = 0; 89 node->tn_status = 0; 90 node->tn_flags = 0; 91 node->tn_links = 0; 92 node->tn_vnode = NULL; 93 node->tn_vpstate = TMPFS_VNODE_WANT; 94 bzero(&node->tn_spec, sizeof(node->tn_spec)); 95 96 return (1); 97 } 98 99 static void 100 tmpfs_node_dtor(void *obj, void *privdata) 101 { 102 struct tmpfs_node *node = (struct tmpfs_node *)obj; 103 node->tn_type = VNON; 104 node->tn_vpstate = TMPFS_VNODE_DOOMED; 105 } 106 107 static void* 108 tmpfs_node_init(void *args, int flags) 109 { 110 struct tmpfs_node *node = (struct tmpfs_node *)objcache_malloc_alloc(args, flags); 111 node->tn_id = 0; 112 113 lockinit(&node->tn_interlock, "tmpfs node interlock", 0, LK_CANRECURSE); 114 node->tn_gen = karc4random(); 115 116 return node; 117 } 118 119 static void 120 tmpfs_node_fini(void *obj, void *args) 121 { 122 struct tmpfs_node *node = (struct tmpfs_node *)obj; 123 lockuninit(&node->tn_interlock); 124 objcache_malloc_free(obj, args); 125 } 126 127 struct objcache_malloc_args tmpfs_dirent_pool_malloc_args = 128 { sizeof(struct tmpfs_dirent), M_TMPFS_DIRENT }; 129 struct objcache_malloc_args tmpfs_node_pool_malloc_args = 130 { sizeof(struct tmpfs_node), M_TMPFS_NODE }; 131 132 static int 133 tmpfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred) 134 { 135 struct tmpfs_mount *tmp; 136 struct tmpfs_node *root; 137 struct tmpfs_args args; 138 vm_pindex_t pages; 139 vm_pindex_t pages_limit; 140 ino_t nodes; 141 int error; 142 /* Size counters. */ 143 ino_t nodes_max; 144 off_t size_max; 145 size_t size; 146 147 /* Root node attributes. */ 148 uid_t root_uid = cred->cr_uid; 149 gid_t root_gid = cred->cr_gid; 150 mode_t root_mode = (VREAD | VWRITE); 151 152 if (mp->mnt_flag & MNT_UPDATE) { 153 /* XXX: There is no support yet to update file system 154 * settings. Should be added. */ 155 156 return EOPNOTSUPP; 157 } 158 159 /* 160 * mount info 161 */ 162 bzero(&args, sizeof(args)); 163 size_max = 0; 164 nodes_max = 0; 165 166 if (path) { 167 if (data) { 168 error = copyin(data, &args, sizeof(args)); 169 if (error) 170 return (error); 171 } 172 size_max = args.ta_size_max; 173 nodes_max = args.ta_nodes_max; 174 root_uid = args.ta_root_uid; 175 root_gid = args.ta_root_gid; 176 root_mode = args.ta_root_mode; 177 } 178 179 /* 180 * If mount by non-root, then verify that user has necessary 181 * permissions on the device. 182 */ 183 if (cred->cr_uid != 0) { 184 root_mode = VREAD; 185 if ((mp->mnt_flag & MNT_RDONLY) == 0) 186 root_mode |= VWRITE; 187 } 188 189 pages_limit = vm_swap_max + vmstats.v_page_count / 2; 190 191 if (size_max == 0) 192 pages = pages_limit / 2; 193 else if (size_max < PAGE_SIZE) 194 pages = 1; 195 else if (OFF_TO_IDX(size_max) > pages_limit) 196 pages = pages_limit; 197 else 198 pages = OFF_TO_IDX(size_max); 199 200 if (nodes_max == 0) 201 nodes = 3 + pages * PAGE_SIZE / 1024; 202 else if (nodes_max < 3) 203 nodes = 3; 204 else if (nodes_max > pages) 205 nodes = pages; 206 else 207 nodes = nodes_max; 208 209 /* Allocate the tmpfs mount structure and fill it. */ 210 tmp = kmalloc(sizeof(*tmp), M_TMPFSMNT, M_WAITOK | M_ZERO); 211 212 lockinit(&(tmp->allnode_lock), "tmpfs allnode lock", 0, LK_CANRECURSE); 213 tmp->tm_nodes_max = nodes; 214 tmp->tm_nodes_inuse = 0; 215 tmp->tm_maxfilesize = IDX_TO_OFF(pages_limit); 216 LIST_INIT(&tmp->tm_nodes_used); 217 218 tmp->tm_pages_max = pages; 219 tmp->tm_pages_used = 0; 220 221 tmp->tm_dirent_pool = objcache_create( "tmpfs dirent cache", 222 0, 0, 223 NULL, NULL, NULL, 224 objcache_malloc_alloc, objcache_malloc_free, 225 &tmpfs_dirent_pool_malloc_args); 226 tmp->tm_node_pool = objcache_create( "tmpfs node cache", 227 0, 0, 228 tmpfs_node_ctor, tmpfs_node_dtor, NULL, 229 tmpfs_node_init, tmpfs_node_fini, 230 &tmpfs_node_pool_malloc_args); 231 232 /* Allocate the root node. */ 233 error = tmpfs_alloc_node(tmp, VDIR, root_uid, root_gid, 234 root_mode & ALLPERMS, NULL, NULL, 235 VNOVAL, VNOVAL, &root); 236 237 /* 238 * We are backed by swap, set snocache chflags flag so we 239 * don't trip over swapcache. 240 */ 241 root->tn_flags = SF_NOCACHE; 242 243 if (error != 0 || root == NULL) { 244 objcache_destroy(tmp->tm_node_pool); 245 objcache_destroy(tmp->tm_dirent_pool); 246 kfree(tmp, M_TMPFSMNT); 247 return error; 248 } 249 KASSERT(root->tn_id >= 0, ("tmpfs root with invalid ino: %d", (int)root->tn_id)); 250 tmp->tm_root = root; 251 252 mp->mnt_flag |= MNT_LOCAL; 253 #if 0 254 mp->mnt_kern_flag |= MNTK_RD_MPSAFE | MNTK_WR_MPSAFE | MNTK_GA_MPSAFE | 255 MNTK_IN_MPSAFE | MNTK_SG_MPSAFE; 256 #endif 257 mp->mnt_kern_flag |= MNTK_NOMSYNC; 258 mp->mnt_data = (qaddr_t)tmp; 259 vfs_getnewfsid(mp); 260 261 262 vfs_add_vnodeops(mp, &tmpfs_vnode_vops, &mp->mnt_vn_norm_ops); 263 vfs_add_vnodeops(mp, &tmpfs_fifo_vops, &mp->mnt_vn_fifo_ops); 264 265 copystr("tmpfs", mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size); 266 bzero(mp->mnt_stat.f_mntfromname +size, MNAMELEN - size); 267 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname)); 268 copyinstr(path, mp->mnt_stat.f_mntonname, 269 sizeof(mp->mnt_stat.f_mntonname) -1, 270 &size); 271 272 tmpfs_statfs(mp, &mp->mnt_stat, cred); 273 274 return 0; 275 } 276 277 /* --------------------------------------------------------------------- */ 278 279 /* ARGSUSED2 */ 280 static int 281 tmpfs_unmount(struct mount *mp, int mntflags) 282 { 283 int error; 284 int flags = 0; 285 int found; 286 struct tmpfs_mount *tmp; 287 struct tmpfs_node *node; 288 289 /* Handle forced unmounts. */ 290 if (mntflags & MNT_FORCE) 291 flags |= FORCECLOSE; 292 293 tmp = VFS_TO_TMPFS(mp); 294 295 /* 296 * Finalize all pending I/O. In the case of tmpfs we want 297 * to throw all the data away so clean out the buffer cache 298 * and vm objects before calling vflush(). 299 */ 300 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) { 301 if (node->tn_type == VREG && node->tn_vnode) { 302 ++node->tn_links; 303 TMPFS_NODE_LOCK(node); 304 vx_get(node->tn_vnode); 305 tmpfs_truncate(node->tn_vnode, 0); 306 vx_put(node->tn_vnode); 307 TMPFS_NODE_UNLOCK(node); 308 --node->tn_links; 309 } 310 } 311 error = vflush(mp, 0, flags); 312 if (error != 0) 313 return error; 314 315 /* 316 * First pass get rid of all the directory entries and 317 * vnode associations. The directory structure will 318 * remain via the extra link count representing tn_dir.tn_parent. 319 * 320 * No vnodes should remain after the vflush above. 321 */ 322 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) { 323 ++node->tn_links; 324 TMPFS_NODE_LOCK(node); 325 if (node->tn_type == VDIR) { 326 struct tmpfs_dirent *de; 327 328 while (!TAILQ_EMPTY(&node->tn_dir.tn_dirhead)) { 329 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead); 330 tmpfs_dir_detach(node, de); 331 tmpfs_free_dirent(tmp, de); 332 node->tn_size -= sizeof(struct tmpfs_dirent); 333 } 334 } 335 KKASSERT(node->tn_vnode == NULL); 336 #if 0 337 vp = node->tn_vnode; 338 if (vp != NULL) { 339 tmpfs_free_vp(vp); 340 vrecycle(vp); 341 node->tn_vnode = NULL; 342 } 343 #endif 344 TMPFS_NODE_UNLOCK(node); 345 --node->tn_links; 346 } 347 348 /* 349 * Now get rid of all nodes. We can remove any node with a 350 * link count of 0 or any directory node with a link count of 351 * 1. The parents will not be destroyed until all their children 352 * have been destroyed. 353 * 354 * Recursion in tmpfs_free_node() can further modify the list so 355 * we cannot use a next pointer here. 356 * 357 * The root node will be destroyed by this loop (it will be last). 358 */ 359 while (!LIST_EMPTY(&tmp->tm_nodes_used)) { 360 found = 0; 361 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) { 362 if (node->tn_links == 0 || 363 (node->tn_links == 1 && node->tn_type == VDIR)) { 364 TMPFS_NODE_LOCK(node); 365 tmpfs_free_node(tmp, node); 366 /* eats lock */ 367 found = 1; 368 break; 369 } 370 } 371 if (found == 0) { 372 kprintf("tmpfs: Cannot free entire node tree!"); 373 break; 374 } 375 } 376 377 KKASSERT(tmp->tm_root == NULL); 378 379 objcache_destroy(tmp->tm_dirent_pool); 380 objcache_destroy(tmp->tm_node_pool); 381 382 lockuninit(&tmp->allnode_lock); 383 KKASSERT(tmp->tm_pages_used == 0); 384 KKASSERT(tmp->tm_nodes_inuse == 0); 385 386 /* Throw away the tmpfs_mount structure. */ 387 kfree(tmp, M_TMPFSMNT); 388 mp->mnt_data = NULL; 389 390 mp->mnt_flag &= ~MNT_LOCAL; 391 return 0; 392 } 393 394 /* --------------------------------------------------------------------- */ 395 396 static int 397 tmpfs_root(struct mount *mp, struct vnode **vpp) 398 { 399 struct tmpfs_mount *tmp; 400 int error; 401 402 tmp = VFS_TO_TMPFS(mp); 403 if (tmp->tm_root == NULL) { 404 kprintf("tmpfs_root: called without root node %p\n", mp); 405 print_backtrace(); 406 *vpp = NULL; 407 error = EINVAL; 408 } else { 409 error = tmpfs_alloc_vp(mp, tmp->tm_root, LK_EXCLUSIVE, vpp); 410 (*vpp)->v_flag |= VROOT; 411 (*vpp)->v_type = VDIR; 412 } 413 return error; 414 } 415 416 /* --------------------------------------------------------------------- */ 417 418 static int 419 tmpfs_fhtovp(struct mount *mp, struct vnode *rootvp, struct fid *fhp, struct vnode **vpp) 420 { 421 boolean_t found; 422 struct tmpfs_fid *tfhp; 423 struct tmpfs_mount *tmp; 424 struct tmpfs_node *node; 425 426 tmp = VFS_TO_TMPFS(mp); 427 428 tfhp = (struct tmpfs_fid *)fhp; 429 if (tfhp->tf_len != sizeof(struct tmpfs_fid)) 430 return EINVAL; 431 432 if (tfhp->tf_id >= tmp->tm_nodes_max) 433 return EINVAL; 434 435 found = FALSE; 436 437 TMPFS_LOCK(tmp); 438 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) { 439 if (node->tn_id == tfhp->tf_id && 440 node->tn_gen == tfhp->tf_gen) { 441 found = TRUE; 442 break; 443 } 444 } 445 TMPFS_UNLOCK(tmp); 446 447 if (found) 448 return (tmpfs_alloc_vp(mp, node, LK_EXCLUSIVE, vpp)); 449 450 return (EINVAL); 451 } 452 453 /* --------------------------------------------------------------------- */ 454 455 /* ARGSUSED2 */ 456 static int 457 tmpfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) 458 { 459 fsfilcnt_t freenodes; 460 struct tmpfs_mount *tmp; 461 462 tmp = VFS_TO_TMPFS(mp); 463 464 sbp->f_iosize = PAGE_SIZE; 465 sbp->f_bsize = PAGE_SIZE; 466 467 sbp->f_blocks = tmp->tm_pages_max; 468 sbp->f_bavail = tmp->tm_pages_max - tmp->tm_pages_used; 469 sbp->f_bfree = sbp->f_bavail; 470 471 freenodes = tmp->tm_nodes_max - tmp->tm_nodes_inuse; 472 473 sbp->f_files = freenodes + tmp->tm_nodes_inuse; 474 sbp->f_ffree = freenodes; 475 /* sbp->f_owner = tmp->tn_uid; */ 476 477 return 0; 478 } 479 480 /* --------------------------------------------------------------------- */ 481 482 /* 483 * tmpfs vfs operations. 484 */ 485 486 static struct vfsops tmpfs_vfsops = { 487 .vfs_mount = tmpfs_mount, 488 .vfs_unmount = tmpfs_unmount, 489 .vfs_root = tmpfs_root, 490 .vfs_statfs = tmpfs_statfs, 491 .vfs_fhtovp = tmpfs_fhtovp, 492 .vfs_sync = vfs_stdsync 493 }; 494 495 VFS_SET(tmpfs_vfsops, tmpfs, 0); 496