sys/vfs/tmpfs/tmpfs_vfsops.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | /* $NetBSD: tmpfs_vfsops.c,v 1.10 2005/12/11 12:24:29 christos Exp $ */ /*- * Copyright (c) 2005 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Julio M. Merino Vidal, developed as part of Google's Summer of Code * 2005 program. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* * Efficient memory file system. * * tmpfs is a file system that uses virtual memory to store file data and * metadata efficiently. It does not follow the structure of an on-disk * file system because it simply does not need to. Instead, it uses * memory-specific data structures and algorithms to automatically * allocate and release resources. */ #include <sys/conf.h> #include <sys/param.h> #include <sys/limits.h> #include <sys/lock.h> #include <sys/kernel.h> #include <sys/stat.h> #include <sys/systm.h> #include <sys/sysctl.h> #include <vm/vm.h> #include <vm/vm_object.h> #include <vm/vm_param.h> #if 0 #include <vfs/tmpfs/tmpfs.h> #endif #include "tmpfs.h" #include <vfs/tmpfs/tmpfs_vnops.h> #include <vfs/tmpfs/tmpfs_mount.h> /* * Default permission for root node */ #define TMPFS_DEFAULT_ROOT_MODE (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures"); /* --------------------------------------------------------------------- */ static int tmpfs_mount(struct mount *, char *, caddr_t, struct ucred *); static int tmpfs_unmount(struct mount *, int); static int tmpfs_root(struct mount *, struct vnode **); static int tmpfs_fhtovp(struct mount *, struct vnode *, struct fid *, struct vnode **); static int tmpfs_statfs(struct mount *, struct statfs *, struct ucred *cred); /* --------------------------------------------------------------------- */ void tmpfs_node_init(struct tmpfs_node *node) { node->tn_blksize = PAGE_SIZE; /* start small */ lockinit(&node->tn_interlock, "tmpfs node interlock", 0, LK_CANRECURSE); node->tn_gen = karc4random(); } void tmpfs_node_uninit(struct tmpfs_node *node) { node->tn_type = VNON; node->tn_vpstate = TMPFS_VNODE_DOOMED; lockuninit(&node->tn_interlock); } static int tmpfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred) { struct tmpfs_mount *tmp; struct tmpfs_node *root; struct tmpfs_mount_info args; vm_pindex_t pages; vm_pindex_t pages_limit; ino_t nodes; u_int64_t maxfsize; int error; /* Size counters. */ ino_t nodes_max; off_t size_max; size_t maxfsize_max; size_t size; /* Root node attributes. */ uid_t root_uid = cred->cr_uid; gid_t root_gid = cred->cr_gid; mode_t root_mode = (VREAD | VWRITE); if (mp->mnt_flag & MNT_UPDATE) { /* XXX: There is no support yet to update file system * settings. Should be added. */ return EOPNOTSUPP; } /* * mount info */ bzero(&args, sizeof(args)); size_max = 0; nodes_max = 0; maxfsize_max = 0; if (path) { if (data) { error = copyin(data, &args, sizeof(args)); if (error) return (error); } size_max = args.ta_size_max; nodes_max = args.ta_nodes_max; maxfsize_max = args.ta_maxfsize_max; root_uid = args.ta_root_uid; root_gid = args.ta_root_gid; root_mode = args.ta_root_mode; } /* * If mount by non-root, then verify that user has necessary * permissions on the device. */ if (cred->cr_uid != 0) { root_mode = VREAD; if ((mp->mnt_flag & MNT_RDONLY) == 0) root_mode |= VWRITE; } pages_limit = vm_swap_max + vmstats.v_page_count / 2; if (size_max == 0) { pages = pages_limit / 2; } else if (size_max < PAGE_SIZE) { pages = 1; } else if (OFF_TO_IDX(size_max) > pages_limit) { /* * do not force pages = pages_limit for this case, otherwise * we might not honor tmpfs size requests from /etc/fstab * during boot because they are mounted prior to swap being * turned on. */ pages = OFF_TO_IDX(size_max); } else { pages = OFF_TO_IDX(size_max); } if (nodes_max == 0) nodes = 3 + pages * PAGE_SIZE / 1024; else if (nodes_max < 3) nodes = 3; else if (nodes_max > pages) nodes = pages; else nodes = nodes_max; maxfsize = 0x7FFFFFFFFFFFFFFFLLU - TMPFS_BLKSIZE; if (maxfsize_max != 0 && maxfsize > maxfsize_max) maxfsize = maxfsize_max; /* Allocate the tmpfs mount structure and fill it. */ tmp = kmalloc(sizeof(*tmp), M_TMPFSMNT, M_WAITOK | M_ZERO); tmp->tm_mount = mp; tmp->tm_nodes_max = nodes; tmp->tm_nodes_inuse = 0; tmp->tm_maxfilesize = maxfsize; LIST_INIT(&tmp->tm_nodes_used); tmp->tm_pages_max = pages; tmp->tm_pages_used = 0; kmalloc_create_obj(&tmp->tm_node_zone, "tmpfs node", sizeof(struct tmpfs_node)); kmalloc_create_obj(&tmp->tm_dirent_zone, "tmpfs dirent", sizeof(struct tmpfs_dirent)); kmalloc_create(&tmp->tm_name_zone, "tmpfs name zone"); kmalloc_obj_raise_limit(tmp->tm_node_zone, sizeof(struct tmpfs_node) * tmp->tm_nodes_max); tmp->tm_ino = TMPFS_ROOTINO; /* Allocate the root node. */ error = tmpfs_alloc_node(tmp, VDIR, root_uid, root_gid, root_mode & ALLPERMS, NULL, VNOVAL, VNOVAL, &root); /* * We are backed by swap, set snocache chflags flag so we * don't trip over swapcache. */ root->tn_flags = SF_NOCACHE; if (error != 0 || root == NULL) { kmalloc_destroy(&tmp->tm_name_zone); kmalloc_destroy(&tmp->tm_dirent_zone_obj); kmalloc_destroy(&tmp->tm_node_zone_obj); kfree(tmp, M_TMPFSMNT); return error; } KASSERT(root->tn_id == TMPFS_ROOTINO, ("tmpfs root with invalid ino: %ju", (uintmax_t)root->tn_id)); atomic_add_int(&root->tn_links, 1); /* keep around */ tmp->tm_root = root; mp->mnt_flag |= MNT_LOCAL; mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; mp->mnt_kern_flag |= MNTK_NOMSYNC; mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */ mp->mnt_kern_flag |= MNTK_QUICKHALT; /* no teardown needed on halt */ mp->mnt_data = (qaddr_t)tmp; mp->mnt_iosize_max = MAXBSIZE; vfs_getnewfsid(mp); vfs_add_vnodeops(mp, &tmpfs_vnode_vops, &mp->mnt_vn_norm_ops); vfs_add_vnodeops(mp, &tmpfs_fifo_vops, &mp->mnt_vn_fifo_ops); copystr("tmpfs", mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size); bzero(mp->mnt_stat.f_mntfromname +size, MNAMELEN - size); bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname)); copyinstr(path, mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname) -1, &size); tmpfs_statfs(mp, &mp->mnt_stat, cred); return 0; } /* --------------------------------------------------------------------- */ /* ARGSUSED2 */ static int tmpfs_unmount(struct mount *mp, int mntflags) { int error; int flags = 0; struct tmpfs_mount *tmp; struct tmpfs_node *node; struct vnode *vp; int isok; tmp = VFS_TO_TMPFS(mp); TMPFS_LOCK(tmp); /* Handle forced unmounts. */ if (mntflags & MNT_FORCE) flags |= FORCECLOSE; /* * Finalize all pending I/O. In the case of tmpfs we want * to throw all the data away so clean out the buffer cache * and vm objects before calling vflush(). */ LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) { /* * tn_links is mnt_token protected */ atomic_add_int(&node->tn_links, 1); TMPFS_NODE_LOCK(node); while (node->tn_type == VREG && node->tn_vnode) { vp = node->tn_vnode; vhold(vp); TMPFS_NODE_UNLOCK(node); lwkt_yield(); /* * vx_get/vx_put and tmpfs_truncate may block, * releasing the tmpfs mountpoint token. * * Make sure the lock order is correct. */ vx_get(vp); /* held vnode */ TMPFS_NODE_LOCK(node); if (node->tn_vnode == vp) { tmpfs_truncate(vp, 0); isok = 1; } else { isok = 0; } TMPFS_NODE_UNLOCK(node); vx_put(vp); vdrop(vp); TMPFS_NODE_LOCK(node); if (isok) break; /* retry */ } TMPFS_NODE_UNLOCK(node); atomic_add_int(&node->tn_links, -1); } /* * Flush all vnodes on the unmount. * * If we fail to flush, we cannot unmount, but all the nodes have * already been truncated. Erroring out is the best we can do. */ error = vflush(mp, 0, flags); if (error != 0) { TMPFS_UNLOCK(tmp); return (error); } /* * First pass get rid of all the directory entries and * vnode associations. This will also destroy the * directory topology and should drop all link counts * to 0 except for the root. * * No vnodes should remain after the vflush above. */ LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) { lwkt_yield(); atomic_add_int(&node->tn_links, 1); TMPFS_NODE_LOCK(node); if (node->tn_type == VDIR) { struct tmpfs_dirent *de; while ((de = RB_ROOT(&node->tn_dir.tn_dirtree)) != NULL) { tmpfs_dir_detach_locked(node, de); tmpfs_free_dirent(tmp, de); } } KKASSERT(node->tn_vnode == NULL); TMPFS_NODE_UNLOCK(node); atomic_add_int(&node->tn_links, -1); } /* * Allow the root node to be destroyed by dropping the link count * we bumped in the mount code. */ KKASSERT(tmp->tm_root); TMPFS_NODE_LOCK(tmp->tm_root); atomic_add_int(&tmp->tm_root->tn_links, -1); TMPFS_NODE_UNLOCK(tmp->tm_root); /* * At this point all nodes, including the root node, should have a * link count of 0. The root is not necessarily going to be last. */ while ((node = LIST_FIRST(&tmp->tm_nodes_used)) != NULL) { if (node->tn_links) { panic("tmpfs: Dangling nodes during umount (%p)!\n", node); } TMPFS_NODE_LOCK(node); tmpfs_free_node(tmp, node); /* eats lock */ lwkt_yield(); } KKASSERT(tmp->tm_root == NULL); kmalloc_destroy(&tmp->tm_name_zone); kmalloc_destroy(&tmp->tm_dirent_zone_obj); kmalloc_destroy(&tmp->tm_node_zone_obj); tmp->tm_node_zone_obj = NULL; tmp->tm_dirent_zone_obj = NULL; KKASSERT(tmp->tm_pages_used == 0); KKASSERT(tmp->tm_nodes_inuse == 0); TMPFS_UNLOCK(tmp); /* Throw away the tmpfs_mount structure. */ kfree(tmp, M_TMPFSMNT); mp->mnt_data = NULL; mp->mnt_flag &= ~MNT_LOCAL; return 0; } /* --------------------------------------------------------------------- */ static int tmpfs_root(struct mount *mp, struct vnode **vpp) { struct tmpfs_mount *tmp; int error; tmp = VFS_TO_TMPFS(mp); if (tmp->tm_root == NULL) { kprintf("tmpfs_root: called without root node %p\n", mp); print_backtrace(-1); *vpp = NULL; error = EINVAL; } else { error = tmpfs_alloc_vp(mp, NULL, tmp->tm_root, LK_EXCLUSIVE, vpp); (*vpp)->v_flag |= VROOT; (*vpp)->v_type = VDIR; } return error; } /* --------------------------------------------------------------------- */ static int tmpfs_fhtovp(struct mount *mp, struct vnode *rootvp, struct fid *fhp, struct vnode **vpp) { boolean_t found; struct tmpfs_fid *tfhp; struct tmpfs_mount *tmp; struct tmpfs_node *node; int rc; tmp = VFS_TO_TMPFS(mp); tfhp = (struct tmpfs_fid *) fhp; if (tfhp->tf_len != sizeof(struct tmpfs_fid)) return EINVAL; rc = EINVAL; found = FALSE; TMPFS_LOCK(tmp); LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) { if (node->tn_id == tfhp->tf_id && node->tn_gen == tfhp->tf_gen) { found = TRUE; break; } } if (found) rc = tmpfs_alloc_vp(mp, NULL, node, LK_EXCLUSIVE, vpp); TMPFS_UNLOCK(tmp); return (rc); } /* --------------------------------------------------------------------- */ /* ARGSUSED2 */ static int tmpfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred) { fsfilcnt_t freenodes; struct tmpfs_mount *tmp; tmp = VFS_TO_TMPFS(mp); /* TMPFS_LOCK(tmp); not really needed */ sbp->f_iosize = PAGE_SIZE; sbp->f_bsize = PAGE_SIZE; sbp->f_blocks = tmp->tm_pages_max; sbp->f_bavail = tmp->tm_pages_max - tmp->tm_pages_used; sbp->f_bfree = sbp->f_bavail; freenodes = tmp->tm_nodes_max - tmp->tm_nodes_inuse; sbp->f_files = freenodes + tmp->tm_nodes_inuse; sbp->f_ffree = freenodes; sbp->f_owner = tmp->tm_root->tn_uid; /* TMPFS_UNLOCK(tmp); */ return 0; } /* --------------------------------------------------------------------- */ static int tmpfs_vptofh(struct vnode *vp, struct fid *fhp) { struct tmpfs_node *node; struct tmpfs_fid tfh; node = VP_TO_TMPFS_NODE(vp); memset(&tfh, 0, sizeof(tfh)); tfh.tf_len = sizeof(struct tmpfs_fid); tfh.tf_gen = node->tn_gen; tfh.tf_id = node->tn_id; memcpy(fhp, &tfh, sizeof(tfh)); return (0); } /* --------------------------------------------------------------------- */ static int tmpfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp, struct ucred **credanonp) { struct tmpfs_mount *tmp; struct netcred *nc; tmp = (struct tmpfs_mount *) mp->mnt_data; nc = vfs_export_lookup(mp, &tmp->tm_export, nam); if (nc == NULL) return (EACCES); *exflagsp = nc->netc_exflags; *credanonp = &nc->netc_anon; return (0); } /* --------------------------------------------------------------------- */ /* * tmpfs vfs operations. */ static struct vfsops tmpfs_vfsops = { .vfs_flags = 0, .vfs_mount = tmpfs_mount, .vfs_unmount = tmpfs_unmount, .vfs_root = tmpfs_root, .vfs_statfs = tmpfs_statfs, .vfs_fhtovp = tmpfs_fhtovp, .vfs_vptofh = tmpfs_vptofh, .vfs_checkexp = tmpfs_checkexp, }; VFS_SET(tmpfs_vfsops, tmpfs, VFCF_MPSAFE); MODULE_VERSION(tmpfs, 1); |