The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/tmpfs/tmpfs_subr.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: b63034cf1a14c1011b47027b9d3db19e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.