1/* $NetBSD: tmpfs_subr.c,v 1.21 2006/06/07 22:33:39 kardel 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 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40/* 41 * Efficient memory file system supporting functions. 42 */ 43#include <sys/cdefs.h>
|
44__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_subr.c 171068 2007-06-28 02:34:32Z delphij $");
|
44__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_subr.c 171070 2007-06-28 02:39:31Z delphij $"); |
45 46#include <sys/param.h> 47#include <sys/namei.h> 48#include <sys/priv.h> 49#include <sys/proc.h> 50#include <sys/stat.h> 51#include <sys/systm.h> 52#include <sys/vnode.h> 53#include <sys/vmmeter.h> 54 55#include <vm/vm.h> 56#include <vm/vm_object.h> 57#include <vm/vm_page.h> 58#include <vm/vm_pager.h> 59#include <vm/vm_extern.h> 60 61#include <fs/tmpfs/tmpfs.h> 62#include <fs/tmpfs/tmpfs_fifoops.h> 63#include <fs/tmpfs/tmpfs_vnops.h> 64 65/* --------------------------------------------------------------------- */ 66 67/* 68 * Allocates a new node of type 'type' inside the 'tmp' mount point, with 69 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode', 70 * using the credentials of the process 'p'. 71 * 72 * If the node type is set to 'VDIR', then the parent parameter must point 73 * to the parent directory of the node being created. It may only be NULL 74 * while allocating the root node. 75 * 76 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter 77 * specifies the device the node represents. 78 * 79 * If the node type is set to 'VLNK', then the parameter target specifies 80 * the file name of the target file for the symbolic link that is being 81 * created. 82 * 83 * Note that new nodes are retrieved from the available list if it has 84 * items or, if it is empty, from the node pool as long as there is enough 85 * space to create them. 86 * 87 * Returns zero on success or an appropriate error code on failure. 88 */ 89int 90tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type, 91 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent, 92 char *target, dev_t rdev, struct thread *p, struct tmpfs_node **node) 93{ 94 struct tmpfs_node *nnode; 95 96 /* If the root directory of the 'tmp' file system is not yet 97 * allocated, this must be the request to do it. */ 98 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR)); 99 100 MPASS(IFF(type == VLNK, target != NULL)); 101 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL)); 102 103 if (tmp->tm_nodes_inuse > tmp->tm_nodes_max) 104 return (ENOSPC); 105 106 nnode = (struct tmpfs_node *)uma_zalloc_arg( 107 tmp->tm_node_pool, tmp, M_WAITOK); 108 if (nnode == NULL) 109 return (ENOSPC); 110 111 /* Generic initialization. */ 112 nnode->tn_type = type; 113 vfs_timestamp(&nnode->tn_atime); 114 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime = 115 nnode->tn_atime; 116 nnode->tn_uid = uid; 117 nnode->tn_gid = gid; 118 nnode->tn_mode = mode;
|
119
|
119 |
120 /* Type-specific initialization. */ 121 switch (nnode->tn_type) { 122 case VBLK: 123 case VCHR: 124 nnode->tn_rdev = rdev; 125 break; 126 127 case VDIR: 128 TAILQ_INIT(&nnode->tn_dir.tn_dirhead); 129 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent; 130 nnode->tn_dir.tn_readdir_lastn = 0; 131 nnode->tn_dir.tn_readdir_lastp = NULL; 132 nnode->tn_links++; 133 nnode->tn_dir.tn_parent->tn_links++; 134 break; 135 136 case VFIFO: 137 /* FALLTHROUGH */ 138 case VSOCK: 139 break; 140 141 case VLNK: 142 MPASS(strlen(target) < MAXPATHLEN); 143 nnode->tn_size = strlen(target); 144 nnode->tn_link = tmpfs_str_zone_alloc(&tmp->tm_str_pool, 145 M_WAITOK, nnode->tn_size); 146 if (nnode->tn_link == NULL) { 147 nnode->tn_type = VNON; 148 uma_zfree(tmp->tm_node_pool, nnode); 149 return ENOSPC; 150 } 151 memcpy(nnode->tn_link, target, nnode->tn_size); 152 break; 153 154 case VREG:
|
155 nnode->tn_reg.tn_aobj =
|
155 nnode->tn_reg.tn_aobj = |
156 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0); 157 nnode->tn_reg.tn_aobj_pages = 0; 158 break; 159 160 default: 161 MPASS(0); 162 } 163 164 TMPFS_LOCK(tmp); 165 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries); 166 tmp->tm_nodes_inuse++; 167 TMPFS_UNLOCK(tmp); 168 169 *node = nnode; 170 return 0; 171} 172 173/* --------------------------------------------------------------------- */ 174 175/* 176 * Destroys the node pointed to by node from the file system 'tmp'. 177 * If the node does not belong to the given mount point, the results are 178 * unpredicted. 179 * 180 * If the node references a directory; no entries are allowed because 181 * their removal could need a recursive algorithm, something forbidden in 182 * kernel space. Furthermore, there is not need to provide such 183 * functionality (recursive removal) because the only primitives offered 184 * to the user are the removal of empty directories and the deletion of 185 * individual files. 186 * 187 * Note that nodes are not really deleted; in fact, when a node has been 188 * allocated, it cannot be deleted during the whole life of the file 189 * system. Instead, they are moved to the available list and remain there 190 * until reused. 191 */ 192void 193tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node) 194{ 195 size_t pages = 0; 196 197 TMPFS_LOCK(tmp); 198 LIST_REMOVE(node, tn_entries); 199 tmp->tm_nodes_inuse--; 200 TMPFS_UNLOCK(tmp); 201 202 switch (node->tn_type) { 203 case VNON: 204 /* Do not do anything. VNON is provided to let the 205 * allocation routine clean itself easily by avoiding 206 * duplicating code in it. */ 207 /* FALLTHROUGH */ 208 case VBLK: 209 /* FALLTHROUGH */ 210 case VCHR: 211 /* FALLTHROUGH */ 212 case VDIR: 213 /* FALLTHROUGH */ 214 case VFIFO: 215 /* FALLTHROUGH */ 216 case VSOCK: 217 break; 218 219 case VLNK: 220 tmpfs_str_zone_free(&tmp->tm_str_pool, node->tn_link, 221 node->tn_size); 222 break; 223 224 case VREG: 225 if (node->tn_reg.tn_aobj != NULL) { 226 vm_object_deallocate(node->tn_reg.tn_aobj); 227 } 228 pages = node->tn_reg.tn_aobj_pages; 229 break; 230 231 default: 232 MPASS(0); 233 break; 234 } 235 236 uma_zfree(tmp->tm_node_pool, node); 237 238 TMPFS_LOCK(tmp); 239 tmp->tm_pages_used -= pages; 240 TMPFS_UNLOCK(tmp); 241} 242 243/* --------------------------------------------------------------------- */ 244 245/* 246 * Allocates a new directory entry for the node node with a name of name. 247 * The new directory entry is returned in *de. 248 * 249 * The link count of node is increased by one to reflect the new object 250 * referencing it. 251 * 252 * Returns zero on success or an appropriate error code on failure. 253 */ 254int 255tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node, 256 const char *name, uint16_t len, struct tmpfs_dirent **de) 257{ 258 struct tmpfs_dirent *nde; 259 260 nde = (struct tmpfs_dirent *)uma_zalloc( 261 tmp->tm_dirent_pool, M_WAITOK); 262 if (nde == NULL) 263 return ENOSPC; 264 265 nde->td_name = tmpfs_str_zone_alloc(&tmp->tm_str_pool, M_WAITOK, len); 266 if (nde->td_name == NULL) { 267 uma_zfree(tmp->tm_dirent_pool, nde); 268 return ENOSPC; 269 } 270 nde->td_namelen = len; 271 memcpy(nde->td_name, name, len); 272 273 nde->td_node = node; 274 node->tn_links++; 275 276 *de = nde; 277 278 return 0; 279} 280 281/* --------------------------------------------------------------------- */ 282 283/* 284 * Frees a directory entry. It is the caller's responsibility to destroy 285 * the node referenced by it if needed. 286 * 287 * The link count of node is decreased by one to reflect the removal of an 288 * object that referenced it. This only happens if 'node_exists' is true; 289 * otherwise the function will not access the node referred to by the 290 * directory entry, as it may already have been released from the outside. 291 */ 292void 293tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de, 294 boolean_t node_exists) 295{ 296 if (node_exists) { 297 struct tmpfs_node *node; 298 299 node = de->td_node; 300 301 MPASS(node->tn_links > 0); 302 node->tn_links--; 303 } 304 305 tmpfs_str_zone_free(&tmp->tm_str_pool, de->td_name, de->td_namelen); 306 uma_zfree(tmp->tm_dirent_pool, de); 307} 308 309/* --------------------------------------------------------------------- */ 310 311/* 312 * Allocates a new vnode for the node node or returns a new reference to 313 * an existing one if the node had already a vnode referencing it. The 314 * resulting locked vnode is returned in *vpp. 315 * 316 * Returns zero on success or an appropriate error code on failure. 317 */ 318int 319tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, struct vnode **vpp, 320 struct thread *td) 321{ 322 int error; 323 struct vnode *vp; 324 325 vp = NULL; 326 327loop: 328 if (node->tn_vnode != NULL) { 329 vp = node->tn_vnode; 330 vget(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
331
|
331 |
332 /* 333 * Make sure the vnode is still there after 334 * getting the interlock to avoid racing a free. 335 */ 336 if (node->tn_vnode == NULL || node->tn_vnode != vp) { 337 vput(vp); 338 goto loop; 339 } 340 341 error = 0; 342 goto out; 343 } 344 345 /* 346 * otherwise lock the vp list while we call getnewvnode 347 * since that can block. 348 */ 349 TMPFS_NODE_LOCK(node); 350 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) { 351 node->tn_vpstate |= TMPFS_VNODE_WANT; 352 TMPFS_NODE_UNLOCK(node); 353 (void) tsleep((caddr_t) &node->tn_vpstate, 0, "tmpfs_vplock", 0); 354 goto loop; 355 } 356 357 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING; 358 TMPFS_NODE_UNLOCK(node); 359 360 /* Get a new vnode and associate it with our node. */ 361 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp); 362 if (error != 0) 363 goto unlock; 364 MPASS(vp != NULL); 365 366 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 367 if (error != 0) { 368 vp->v_data = NULL; 369 vput(vp); 370 vp = NULL; 371 goto unlock; 372 } 373 374 vp->v_data = node; 375 vp->v_type = node->tn_type; 376 377 /* Type-specific initialization. */ 378 switch (node->tn_type) { 379 case VBLK: 380 /* FALLTHROUGH */ 381 case VCHR: 382 break; 383 384 case VDIR: 385 break; 386 387 case VFIFO: 388 vp->v_op = &tmpfs_fifoop_entries; 389 break; 390 391 case VLNK: 392 /* FALLTHROUGH */ 393 case VREG: 394 /* FALLTHROUGH */ 395 case VSOCK: 396 break; 397 398 default: 399 MPASS(0); 400 } 401 402 vnode_pager_setsize(vp, node->tn_size); 403 insmntque(vp, mp); 404 405 error = 0; 406 node->tn_vnode = vp; 407 408unlock: 409 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING); 410 TMPFS_NODE_LOCK(node); 411 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
|
412
|
412 |
413 if (node->tn_vpstate & TMPFS_VNODE_WANT) { 414 node->tn_vpstate &= ~TMPFS_VNODE_WANT; 415 TMPFS_NODE_UNLOCK(node); 416 wakeup((caddr_t) &node->tn_vpstate); 417 } else 418 TMPFS_NODE_UNLOCK(node);
|
419
|
419 |
420out: 421 *vpp = vp; 422 423 MPASS(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp, td))); 424 MPASS(*vpp == node->tn_vnode); 425 426 return error; 427} 428 429/* --------------------------------------------------------------------- */ 430 431/* 432 * Destroys the association between the vnode vp and the node it 433 * references. 434 */ 435void 436tmpfs_free_vp(struct vnode *vp) 437{ 438 struct tmpfs_node *node; 439 440 node = VP_TO_TMPFS_NODE(vp); 441 442 node->tn_vnode = NULL; 443 vp->v_data = NULL; 444} 445 446/* --------------------------------------------------------------------- */ 447 448/* 449 * Allocates a new file of type 'type' and adds it to the parent directory 450 * 'dvp'; this addition is done using the component name given in 'cnp'. 451 * The ownership of the new file is automatically assigned based on the 452 * credentials of the caller (through 'cnp'), the group is set based on 453 * the parent directory and the mode is determined from the 'vap' argument. 454 * If successful, *vpp holds a vnode to the newly created file and zero 455 * is returned. Otherwise *vpp is NULL and the function returns an 456 * appropriate error code. 457 */ 458int 459tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, 460 struct componentname *cnp, char *target) 461{ 462 int error; 463 struct tmpfs_dirent *de; 464 struct tmpfs_mount *tmp; 465 struct tmpfs_node *dnode; 466 struct tmpfs_node *node; 467 struct tmpfs_node *parent; 468 469 MPASS(VOP_ISLOCKED(dvp, cnp->cn_thread)); 470 MPASS(cnp->cn_flags & HASBUF); 471 472 tmp = VFS_TO_TMPFS(dvp->v_mount); 473 dnode = VP_TO_TMPFS_DIR(dvp); 474 *vpp = NULL; 475 476 /* If the entry we are creating is a directory, we cannot overflow 477 * the number of links of its parent, because it will get a new 478 * link. */ 479 if (vap->va_type == VDIR) { 480 /* Ensure that we do not overflow the maximum number of links 481 * imposed by the system. */ 482 MPASS(dnode->tn_links <= LINK_MAX); 483 if (dnode->tn_links == LINK_MAX) { 484 error = EMLINK; 485 goto out; 486 } 487 488 parent = dnode; 489 } else 490 parent = NULL; 491 492 /* Allocate a node that represents the new file. */ 493 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid, 494 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, 495 cnp->cn_thread, &node); 496 if (error != 0) 497 goto out; 498 499 /* Allocate a directory entry that points to the new file. */ 500 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen, 501 &de); 502 if (error != 0) { 503 tmpfs_free_node(tmp, node); 504 goto out; 505 } 506 507 /* Allocate a vnode for the new file. */ 508 error = tmpfs_alloc_vp(dvp->v_mount, node, vpp, cnp->cn_thread); 509 if (error != 0) { 510 tmpfs_free_dirent(tmp, de, TRUE); 511 tmpfs_free_node(tmp, node); 512 goto out; 513 } 514 515 /* Now that all required items are allocated, we can proceed to 516 * insert the new node into the directory, an operation that 517 * cannot fail. */ 518 tmpfs_dir_attach(dvp, de); 519 520out: 521 522 return error; 523} 524 525/* --------------------------------------------------------------------- */ 526 527/* 528 * Attaches the directory entry de to the directory represented by vp. 529 * Note that this does not change the link count of the node pointed by 530 * the directory entry, as this is done by tmpfs_alloc_dirent. 531 */ 532void 533tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de) 534{ 535 struct tmpfs_node *dnode; 536 537 dnode = VP_TO_TMPFS_DIR(vp); 538 TAILQ_INSERT_TAIL(&dnode->tn_dir.tn_dirhead, de, td_entries); 539 dnode->tn_size += sizeof(struct tmpfs_dirent); 540 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 541 TMPFS_NODE_MODIFIED; 542} 543 544/* --------------------------------------------------------------------- */ 545 546/* 547 * Detaches the directory entry de from the directory represented by vp. 548 * Note that this does not change the link count of the node pointed by 549 * the directory entry, as this is done by tmpfs_free_dirent. 550 */ 551void 552tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de) 553{ 554 struct tmpfs_node *dnode; 555 556 dnode = VP_TO_TMPFS_DIR(vp); 557 558 if (dnode->tn_dir.tn_readdir_lastp == de) { 559 dnode->tn_dir.tn_readdir_lastn = 0; 560 dnode->tn_dir.tn_readdir_lastp = NULL; 561 } 562 563 TAILQ_REMOVE(&dnode->tn_dir.tn_dirhead, de, td_entries); 564 dnode->tn_size -= sizeof(struct tmpfs_dirent); 565 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \ 566 TMPFS_NODE_MODIFIED; 567} 568 569/* --------------------------------------------------------------------- */ 570 571/* 572 * Looks for a directory entry in the directory represented by node. 573 * 'cnp' describes the name of the entry to look for. Note that the . 574 * and .. components are not allowed as they do not physically exist 575 * within directories. 576 * 577 * Returns a pointer to the entry when found, otherwise NULL. 578 */ 579struct tmpfs_dirent * 580tmpfs_dir_lookup(struct tmpfs_node *node, struct componentname *cnp) 581{ 582 boolean_t found; 583 struct tmpfs_dirent *de; 584 585 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.')); 586 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' && 587 cnp->cn_nameptr[1] == '.'))); 588 TMPFS_VALIDATE_DIR(node); 589 590 found = 0; 591 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) { 592 MPASS(cnp->cn_namelen < 0xffff); 593 if (de->td_namelen == (uint16_t)cnp->cn_namelen && 594 memcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) { 595 found = 1; 596 break; 597 } 598 } 599 node->tn_status |= TMPFS_NODE_ACCESSED;
|
600
|
600 |
601 return found ? de : NULL; 602} 603 604/* --------------------------------------------------------------------- */ 605 606/* 607 * Helper function for tmpfs_readdir. Creates a '.' entry for the given 608 * directory and returns it in the uio space. The function returns 0 609 * on success, -1 if there was not enough space in the uio structure to 610 * hold the directory entry or an appropriate error code if another 611 * error happens. 612 */ 613int 614tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio) 615{ 616 int error; 617 struct dirent dent; 618 619 TMPFS_VALIDATE_DIR(node); 620 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT); 621 622 dent.d_fileno = node->tn_id; 623 dent.d_type = DT_DIR; 624 dent.d_namlen = 1; 625 dent.d_name[0] = '.'; 626 dent.d_name[1] = '\0'; 627 dent.d_reclen = GENERIC_DIRSIZ(&dent); 628 629 if (dent.d_reclen > uio->uio_resid) 630 error = -1; 631 else { 632 error = uiomove(&dent, dent.d_reclen, uio); 633 if (error == 0) 634 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT; 635 } 636 637 node->tn_status |= TMPFS_NODE_ACCESSED; 638 639 return error; 640} 641 642/* --------------------------------------------------------------------- */ 643 644/* 645 * Helper function for tmpfs_readdir. Creates a '..' entry for the given 646 * directory and returns it in the uio space. The function returns 0 647 * on success, -1 if there was not enough space in the uio structure to 648 * hold the directory entry or an appropriate error code if another 649 * error happens. 650 */ 651int 652tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio) 653{ 654 int error; 655 struct dirent dent; 656 657 TMPFS_VALIDATE_DIR(node); 658 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT); 659 660 dent.d_fileno = node->tn_dir.tn_parent->tn_id; 661 dent.d_type = DT_DIR; 662 dent.d_namlen = 2; 663 dent.d_name[0] = '.'; 664 dent.d_name[1] = '.'; 665 dent.d_name[2] = '\0'; 666 dent.d_reclen = GENERIC_DIRSIZ(&dent); 667 668 if (dent.d_reclen > uio->uio_resid) 669 error = -1; 670 else { 671 error = uiomove(&dent, dent.d_reclen, uio); 672 if (error == 0) { 673 struct tmpfs_dirent *de; 674 675 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead); 676 if (de == NULL) 677 uio->uio_offset = TMPFS_DIRCOOKIE_EOF; 678 else 679 uio->uio_offset = TMPFS_DIRCOOKIE(de); 680 } 681 } 682 683 node->tn_status |= TMPFS_NODE_ACCESSED; 684 685 return error; 686} 687 688/* --------------------------------------------------------------------- */ 689 690/* 691 * Lookup a directory entry by its associated cookie. 692 */ 693struct tmpfs_dirent * 694tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie) 695{ 696 struct tmpfs_dirent *de; 697 698 if (cookie == node->tn_dir.tn_readdir_lastn && 699 node->tn_dir.tn_readdir_lastp != NULL) { 700 return node->tn_dir.tn_readdir_lastp; 701 } 702 703 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) { 704 if (TMPFS_DIRCOOKIE(de) == cookie) { 705 break; 706 } 707 } 708 709 return de; 710} 711 712/* --------------------------------------------------------------------- */ 713 714/* 715 * Helper function for tmpfs_readdir. Returns as much directory entries 716 * as can fit in the uio space. The read starts at uio->uio_offset. 717 * The function returns 0 on success, -1 if there was not enough space 718 * in the uio structure to hold the directory entry or an appropriate 719 * error code if another error happens. 720 */ 721int 722tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp) 723{ 724 int error; 725 off_t startcookie; 726 struct tmpfs_dirent *de; 727 728 TMPFS_VALIDATE_DIR(node); 729 730 /* Locate the first directory entry we have to return. We have cached 731 * the last readdir in the node, so use those values if appropriate. 732 * Otherwise do a linear scan to find the requested entry. */ 733 startcookie = uio->uio_offset; 734 MPASS(startcookie != TMPFS_DIRCOOKIE_DOT); 735 MPASS(startcookie != TMPFS_DIRCOOKIE_DOTDOT); 736 if (startcookie == TMPFS_DIRCOOKIE_EOF) { 737 return 0; 738 } else { 739 de = tmpfs_dir_lookupbycookie(node, startcookie); 740 } 741 if (de == NULL) { 742 return EINVAL; 743 } 744 745 /* Read as much entries as possible; i.e., until we reach the end of 746 * the directory or we exhaust uio space. */ 747 do { 748 struct dirent d; 749 750 /* Create a dirent structure representing the current 751 * tmpfs_node and fill it. */ 752 d.d_fileno = de->td_node->tn_id; 753 switch (de->td_node->tn_type) { 754 case VBLK: 755 d.d_type = DT_BLK; 756 break; 757 758 case VCHR: 759 d.d_type = DT_CHR; 760 break; 761 762 case VDIR: 763 d.d_type = DT_DIR; 764 break; 765 766 case VFIFO: 767 d.d_type = DT_FIFO; 768 break; 769 770 case VLNK: 771 d.d_type = DT_LNK; 772 break; 773 774 case VREG: 775 d.d_type = DT_REG; 776 break; 777 778 case VSOCK: 779 d.d_type = DT_SOCK; 780 break; 781 782 default: 783 MPASS(0); 784 } 785 d.d_namlen = de->td_namelen; 786 MPASS(de->td_namelen < sizeof(d.d_name)); 787 (void)memcpy(d.d_name, de->td_name, de->td_namelen); 788 d.d_name[de->td_namelen] = '\0'; 789 d.d_reclen = GENERIC_DIRSIZ(&d); 790 791 /* Stop reading if the directory entry we are treating is 792 * bigger than the amount of data that can be returned. */ 793 if (d.d_reclen > uio->uio_resid) { 794 error = -1; 795 break; 796 } 797 798 /* Copy the new dirent structure into the output buffer and 799 * advance pointers. */ 800 error = uiomove(&d, d.d_reclen, uio); 801 802 (*cntp)++; 803 de = TAILQ_NEXT(de, td_entries); 804 } while (error == 0 && uio->uio_resid > 0 && de != NULL); 805 806 /* Update the offset and cache. */ 807 if (de == NULL) { 808 uio->uio_offset = TMPFS_DIRCOOKIE_EOF; 809 node->tn_dir.tn_readdir_lastn = 0; 810 node->tn_dir.tn_readdir_lastp = NULL; 811 } else { 812 node->tn_dir.tn_readdir_lastn = uio->uio_offset = TMPFS_DIRCOOKIE(de); 813 node->tn_dir.tn_readdir_lastp = de; 814 } 815 816 node->tn_status |= TMPFS_NODE_ACCESSED; 817 return error; 818} 819 820/* --------------------------------------------------------------------- */ 821 822/* 823 * Resizes the aobj associated to the regular file pointed to by vp to 824 * the size newsize. 'vp' must point to a vnode that represents a regular 825 * file. 'newsize' must be positive. 826 * 827 * Returns zero on success or an appropriate error code on failure. 828 */ 829int 830tmpfs_reg_resize(struct vnode *vp, off_t newsize) 831{ 832 int error; 833 size_t newpages, oldpages; 834 struct tmpfs_mount *tmp; 835 struct tmpfs_node *node; 836 off_t oldsize; 837 838 MPASS(vp->v_type == VREG); 839 MPASS(newsize >= 0); 840 841 node = VP_TO_TMPFS_NODE(vp); 842 tmp = VFS_TO_TMPFS(vp->v_mount); 843 844 /* Convert the old and new sizes to the number of pages needed to 845 * store them. It may happen that we do not need to do anything 846 * because the last allocated page can accommodate the change on 847 * its own. */ 848 oldsize = node->tn_size; 849 oldpages = round_page(oldsize) / PAGE_SIZE; 850 MPASS(oldpages == node->tn_reg.tn_aobj_pages); 851 newpages = round_page(newsize) / PAGE_SIZE; 852 853 if (newpages > oldpages && 854 newpages - oldpages > TMPFS_PAGES_AVAIL(tmp)) { 855 error = ENOSPC; 856 goto out; 857 } 858 859 node->tn_reg.tn_aobj_pages = newpages; 860 861 TMPFS_LOCK(tmp); 862 tmp->tm_pages_used += (newpages - oldpages); 863 TMPFS_UNLOCK(tmp); 864 865 node->tn_size = newsize; 866 vnode_pager_setsize(vp, newsize); 867 if (newsize < oldsize) { 868 size_t zerolen = MIN(round_page(newsize), node->tn_size) - newsize; 869 struct vm_object *uobj = node->tn_reg.tn_aobj; 870 vm_page_t m; 871 872 /* 873 * free "backing store" 874 */ 875 876 if (newpages < oldpages) { 877 VM_OBJECT_LOCK(uobj);
|
878 swap_pager_freespace(uobj,
|
878 swap_pager_freespace(uobj, |
879 newpages, oldpages - newpages); 880 VM_OBJECT_UNLOCK(uobj); 881 } 882 883 /* 884 * zero out the truncated part of the last page. 885 */ 886 887 if (zerolen > 0) {
|
888 m = vm_page_grab(uobj, OFF_TO_IDX(newsize),
|
888 m = vm_page_grab(uobj, OFF_TO_IDX(newsize), |
889 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
|
890 pmap_zero_page_area(m, PAGE_SIZE - zerolen,
|
890 pmap_zero_page_area(m, PAGE_SIZE - zerolen, |
891 zerolen); 892 vm_page_wakeup(m); 893 } 894 895 } 896 897 error = 0; 898 899out: 900 return error; 901} 902 903/* --------------------------------------------------------------------- */ 904 905/* 906 * Change flags of the given vnode. 907 * Caller should execute tmpfs_update on vp after a successful execution. 908 * The vnode must be locked on entry and remain locked on exit. 909 */ 910int 911tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct thread *p) 912{ 913 int error; 914 struct tmpfs_node *node; 915 916 MPASS(VOP_ISLOCKED(vp, p)); 917 918 node = VP_TO_TMPFS_NODE(vp); 919 920 /* Disallow this operation if the file system is mounted read-only. */ 921 if (vp->v_mount->mnt_flag & MNT_RDONLY) 922 return EROFS; 923 924 /* 925 * Callers may only modify the file flags on objects they 926 * have VADMIN rights for. 927 */ 928 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 929 return (error); 930 /* 931 * Unprivileged processes are not permitted to unset system 932 * flags, or modify flags if any system flags are set. 933 */ 934 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) { 935 if (node->tn_flags 936 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) { 937 error = securelevel_gt(cred, 0); 938 if (error) 939 return (error); 940 } 941 /* Snapshot flag cannot be set or cleared */ 942 if (((flags & SF_SNAPSHOT) != 0 && 943 (node->tn_flags & SF_SNAPSHOT) == 0) || 944 ((flags & SF_SNAPSHOT) == 0 && 945 (node->tn_flags & SF_SNAPSHOT) != 0)) 946 return (EPERM); 947 node->tn_flags = flags; 948 } else { 949 if (node->tn_flags 950 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) || 951 (flags & UF_SETTABLE) != flags) 952 return (EPERM); 953 node->tn_flags &= SF_SETTABLE; 954 node->tn_flags |= (flags & UF_SETTABLE); 955 } 956 node->tn_status |= TMPFS_NODE_CHANGED; 957 958 MPASS(VOP_ISLOCKED(vp, p)); 959 960 return 0; 961} 962 963/* --------------------------------------------------------------------- */ 964 965/* 966 * Change access mode on the given vnode. 967 * Caller should execute tmpfs_update on vp after a successful execution. 968 * The vnode must be locked on entry and remain locked on exit. 969 */ 970int 971tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p) 972{ 973 int error; 974 struct tmpfs_node *node; 975 976 MPASS(VOP_ISLOCKED(vp, p)); 977 978 node = VP_TO_TMPFS_NODE(vp); 979 980 /* Disallow this operation if the file system is mounted read-only. */ 981 if (vp->v_mount->mnt_flag & MNT_RDONLY) 982 return EROFS; 983 984 /* Immutable or append-only files cannot be modified, either. */ 985 if (node->tn_flags & (IMMUTABLE | APPEND)) 986 return EPERM; 987 988 /* 989 * To modify the permissions on a file, must possess VADMIN 990 * for that file. 991 */ 992 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 993 return (error); 994 995 /* 996 * Privileged processes may set the sticky bit on non-directories, 997 * as well as set the setgid bit on a file with a group that the 998 * process is not a member of. 999 */ 1000 if (vp->v_type != VDIR && (mode & S_ISTXT)) { 1001 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0)) 1002 return (EFTYPE); 1003 } 1004 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) { 1005 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0); 1006 if (error) 1007 return (error); 1008 } 1009 1010 1011 node->tn_mode &= ~ALLPERMS; 1012 node->tn_mode |= mode & ALLPERMS; 1013 1014 node->tn_status |= TMPFS_NODE_CHANGED; 1015 1016 MPASS(VOP_ISLOCKED(vp, p)); 1017 1018 return 0; 1019} 1020 1021/* --------------------------------------------------------------------- */ 1022 1023/* 1024 * Change ownership of the given vnode. At least one of uid or gid must 1025 * be different than VNOVAL. If one is set to that value, the attribute 1026 * is unchanged. 1027 * Caller should execute tmpfs_update on vp after a successful execution. 1028 * The vnode must be locked on entry and remain locked on exit. 1029 */ 1030int 1031tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred, 1032 struct thread *p) 1033{ 1034 int error; 1035 struct tmpfs_node *node; 1036 uid_t ouid; 1037 gid_t ogid; 1038 1039 MPASS(VOP_ISLOCKED(vp, p)); 1040 1041 node = VP_TO_TMPFS_NODE(vp); 1042 1043 /* Assign default values if they are unknown. */ 1044 MPASS(uid != VNOVAL || gid != VNOVAL); 1045 if (uid == VNOVAL) 1046 uid = node->tn_uid; 1047 if (gid == VNOVAL) 1048 gid = node->tn_gid; 1049 MPASS(uid != VNOVAL && gid != VNOVAL); 1050 1051 /* Disallow this operation if the file system is mounted read-only. */ 1052 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1053 return EROFS; 1054 1055 /* Immutable or append-only files cannot be modified, either. */ 1056 if (node->tn_flags & (IMMUTABLE | APPEND)) 1057 return EPERM; 1058 1059 /* 1060 * To modify the ownership of a file, must possess VADMIN for that 1061 * file. 1062 */ 1063 if ((error = VOP_ACCESS(vp, VADMIN, cred, p))) 1064 return (error); 1065 1066 /* 1067 * To change the owner of a file, or change the group of a file to a 1068 * group of which we are not a member, the caller must have 1069 * privilege. 1070 */
|
1071 if ((uid != node->tn_uid ||
|
1071 if ((uid != node->tn_uid || |
1072 (gid != node->tn_gid && !groupmember(gid, cred))) && 1073 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0))) 1074 return (error); 1075 1076 ogid = node->tn_gid; 1077 ouid = node->tn_uid; 1078 1079 node->tn_uid = uid; 1080 node->tn_gid = gid; 1081 1082 node->tn_status |= TMPFS_NODE_CHANGED; 1083 1084 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) { 1085 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) 1086 node->tn_mode &= ~(S_ISUID | S_ISGID); 1087 } 1088 1089 MPASS(VOP_ISLOCKED(vp, p)); 1090 1091 return 0; 1092} 1093 1094/* --------------------------------------------------------------------- */ 1095 1096/* 1097 * Change size of the given vnode. 1098 * Caller should execute tmpfs_update on vp after a successful execution. 1099 * The vnode must be locked on entry and remain locked on exit. 1100 */ 1101int 1102tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred, 1103 struct thread *p) 1104{ 1105 int error; 1106 struct tmpfs_node *node; 1107 1108 MPASS(VOP_ISLOCKED(vp, p)); 1109 1110 node = VP_TO_TMPFS_NODE(vp); 1111 1112 /* Decide whether this is a valid operation based on the file type. */ 1113 error = 0; 1114 switch (vp->v_type) { 1115 case VDIR: 1116 return EISDIR; 1117 1118 case VREG: 1119 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1120 return EROFS; 1121 break; 1122 1123 case VBLK: 1124 /* FALLTHROUGH */ 1125 case VCHR: 1126 /* FALLTHROUGH */ 1127 case VFIFO: 1128 /* Allow modifications of special files even if in the file 1129 * system is mounted read-only (we are not modifying the 1130 * files themselves, but the objects they represent). */ 1131 return 0; 1132 1133 default: 1134 /* Anything else is unsupported. */ 1135 return EOPNOTSUPP; 1136 } 1137 1138 /* Immutable or append-only files cannot be modified, either. */ 1139 if (node->tn_flags & (IMMUTABLE | APPEND)) 1140 return EPERM; 1141 1142 error = tmpfs_truncate(vp, size); 1143 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents 1144 * for us, as will update tn_status; no need to do that here. */ 1145 1146 MPASS(VOP_ISLOCKED(vp, p)); 1147 1148 return error; 1149} 1150 1151/* --------------------------------------------------------------------- */ 1152 1153/* 1154 * Change access and modification times of the given vnode. 1155 * Caller should execute tmpfs_update on vp after a successful execution. 1156 * The vnode must be locked on entry and remain locked on exit. 1157 */ 1158int 1159tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime, 1160 struct timespec *birthtime, int vaflags, struct ucred *cred, struct thread *l) 1161{ 1162 int error; 1163 struct tmpfs_node *node; 1164 1165 MPASS(VOP_ISLOCKED(vp, l)); 1166 1167 node = VP_TO_TMPFS_NODE(vp); 1168 1169 /* Disallow this operation if the file system is mounted read-only. */ 1170 if (vp->v_mount->mnt_flag & MNT_RDONLY) 1171 return EROFS; 1172 1173 /* Immutable or append-only files cannot be modified, either. */ 1174 if (node->tn_flags & (IMMUTABLE | APPEND)) 1175 return EPERM; 1176 1177 /* XXX: The following comes from UFS code, and can be found in 1178 * several other file systems. Shouldn't this be centralized 1179 * somewhere? */ 1180 if (cred->cr_uid != node->tn_uid &&
|
1181 (error = suser_cred(cred, 0)) &&
|
1181 (error = suser_cred(cred, 0)) && |
1182 ((vaflags & VA_UTIMES_NULL) == 0 || 1183 (error = VOP_ACCESS(vp, VWRITE, cred, l)))) 1184 return error; 1185 1186 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL) 1187 node->tn_status |= TMPFS_NODE_ACCESSED; 1188 1189 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL) 1190 node->tn_status |= TMPFS_NODE_MODIFIED; 1191 1192 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL) 1193 node->tn_status |= TMPFS_NODE_MODIFIED;
|
1194
|
1194 |
1195 tmpfs_itimes(vp, atime, mtime);
|
1196
|
1196 |
1197 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL) 1198 node->tn_birthtime = *birthtime; 1199 MPASS(VOP_ISLOCKED(vp, l)); 1200 1201 return 0; 1202} 1203 1204/* --------------------------------------------------------------------- */ 1205/* Sync timestamps */ 1206void 1207tmpfs_itimes(struct vnode *vp, const struct timespec *acc, 1208 const struct timespec *mod) 1209{ 1210 struct tmpfs_node *node; 1211 struct timespec now; 1212 1213 node = VP_TO_TMPFS_NODE(vp); 1214 1215 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | 1216 TMPFS_NODE_CHANGED)) == 0) 1217 return; 1218 1219 vfs_timestamp(&now); 1220 if (node->tn_status & TMPFS_NODE_ACCESSED) { 1221 if (acc == NULL) 1222 acc = &now; 1223 node->tn_atime = *acc; 1224 } 1225 if (node->tn_status & TMPFS_NODE_MODIFIED) { 1226 if (mod == NULL) 1227 mod = &now; 1228 node->tn_mtime = *mod; 1229 } 1230 if (node->tn_status & TMPFS_NODE_CHANGED) { 1231 node->tn_ctime = now; 1232 } 1233 node->tn_status &= 1234 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED); 1235} 1236 1237/* --------------------------------------------------------------------- */ 1238 1239void 1240tmpfs_update(struct vnode *vp) 1241{ 1242 1243 struct tmpfs_node *node; 1244 1245 node = VP_TO_TMPFS_NODE(vp); 1246 1247 tmpfs_itimes(vp, NULL, NULL); 1248} 1249 1250/* --------------------------------------------------------------------- */ 1251 1252int 1253tmpfs_truncate(struct vnode *vp, off_t length) 1254{ 1255 boolean_t extended; 1256 int error; 1257 struct tmpfs_node *node; 1258 1259 node = VP_TO_TMPFS_NODE(vp); 1260 extended = length > node->tn_size; 1261 1262 if (length < 0) { 1263 error = EINVAL; 1264 goto out; 1265 } 1266 1267 if (node->tn_size == length) { 1268 error = 0; 1269 goto out; 1270 } 1271 1272 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) 1273 return (EFBIG); 1274 1275 error = tmpfs_reg_resize(vp, length); 1276 if (error == 0) { 1277 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED; 1278 } 1279 1280out: 1281 tmpfs_update(vp); 1282 1283 return error; 1284}
|