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_vnops.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_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $    */
    2 
    3 /*-
    4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
    5  *
    6  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
    7  * All rights reserved.
    8  *
    9  * This code is derived from software contributed to The NetBSD Foundation
   10  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
   11  * 2005 program.
   12  *
   13  * Redistribution and use in source and binary forms, with or without
   14  * modification, are permitted provided that the following conditions
   15  * are met:
   16  * 1. Redistributions of source code must retain the above copyright
   17  *    notice, this list of conditions and the following disclaimer.
   18  * 2. Redistributions in binary form must reproduce the above copyright
   19  *    notice, this list of conditions and the following disclaimer in the
   20  *    documentation and/or other materials provided with the distribution.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32  * POSSIBILITY OF SUCH DAMAGE.
   33  */
   34 
   35 /*
   36  * tmpfs vnode interface.
   37  */
   38 #include <sys/cdefs.h>
   39 __FBSDID("$FreeBSD$");
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/dirent.h>
   44 #include <sys/fcntl.h>
   45 #include <sys/file.h>
   46 #include <sys/filio.h>
   47 #include <sys/limits.h>
   48 #include <sys/lockf.h>
   49 #include <sys/lock.h>
   50 #include <sys/mount.h>
   51 #include <sys/namei.h>
   52 #include <sys/priv.h>
   53 #include <sys/proc.h>
   54 #include <sys/rwlock.h>
   55 #include <sys/sched.h>
   56 #include <sys/smr.h>
   57 #include <sys/stat.h>
   58 #include <sys/sysctl.h>
   59 #include <sys/unistd.h>
   60 #include <sys/vnode.h>
   61 #include <security/audit/audit.h>
   62 #include <security/mac/mac_framework.h>
   63 
   64 #include <vm/vm.h>
   65 #include <vm/vm_param.h>
   66 #include <vm/vm_object.h>
   67 #include <vm/vm_page.h>
   68 #include <vm/vm_pager.h>
   69 #include <vm/swap_pager.h>
   70 
   71 #include <fs/tmpfs/tmpfs_vnops.h>
   72 #include <fs/tmpfs/tmpfs.h>
   73 
   74 SYSCTL_DECL(_vfs_tmpfs);
   75 VFS_SMR_DECLARE;
   76 
   77 static volatile int tmpfs_rename_restarts;
   78 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
   79     __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
   80     "Times rename had to restart due to lock contention");
   81 
   82 static int
   83 tmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
   84     struct vnode **rvp)
   85 {
   86 
   87         return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
   88 }
   89 
   90 static int
   91 tmpfs_lookup1(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
   92 {
   93         struct tmpfs_dirent *de;
   94         struct tmpfs_node *dnode, *pnode;
   95         struct tmpfs_mount *tm;
   96         int error;
   97 
   98         /* Caller assumes responsibility for ensuring access (VEXEC). */
   99         dnode = VP_TO_TMPFS_DIR(dvp);
  100         *vpp = NULLVP;
  101 
  102         /* We cannot be requesting the parent directory of the root node. */
  103         MPASS(IMPLIES(dnode->tn_type == VDIR &&
  104             dnode->tn_dir.tn_parent == dnode,
  105             !(cnp->cn_flags & ISDOTDOT)));
  106 
  107         TMPFS_ASSERT_LOCKED(dnode);
  108         if (dnode->tn_dir.tn_parent == NULL) {
  109                 error = ENOENT;
  110                 goto out;
  111         }
  112         if (cnp->cn_flags & ISDOTDOT) {
  113                 tm = VFS_TO_TMPFS(dvp->v_mount);
  114                 pnode = dnode->tn_dir.tn_parent;
  115                 tmpfs_ref_node(pnode);
  116                 error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
  117                     pnode, cnp->cn_lkflags, vpp);
  118                 tmpfs_free_node(tm, pnode);
  119                 if (error != 0)
  120                         goto out;
  121         } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
  122                 VREF(dvp);
  123                 *vpp = dvp;
  124                 error = 0;
  125         } else {
  126                 de = tmpfs_dir_lookup(dnode, NULL, cnp);
  127                 if (de != NULL && de->td_node == NULL)
  128                         cnp->cn_flags |= ISWHITEOUT;
  129                 if (de == NULL || de->td_node == NULL) {
  130                         /*
  131                          * The entry was not found in the directory.
  132                          * This is OK if we are creating or renaming an
  133                          * entry and are working on the last component of
  134                          * the path name.
  135                          */
  136                         if ((cnp->cn_flags & ISLASTCN) &&
  137                             (cnp->cn_nameiop == CREATE || \
  138                             cnp->cn_nameiop == RENAME ||
  139                             (cnp->cn_nameiop == DELETE &&
  140                             cnp->cn_flags & DOWHITEOUT &&
  141                             cnp->cn_flags & ISWHITEOUT))) {
  142                                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
  143                                     cnp->cn_thread);
  144                                 if (error != 0)
  145                                         goto out;
  146 
  147                                 /*
  148                                  * Keep the component name in the buffer for
  149                                  * future uses.
  150                                  */
  151                                 cnp->cn_flags |= SAVENAME;
  152 
  153                                 error = EJUSTRETURN;
  154                         } else
  155                                 error = ENOENT;
  156                 } else {
  157                         struct tmpfs_node *tnode;
  158 
  159                         /*
  160                          * The entry was found, so get its associated
  161                          * tmpfs_node.
  162                          */
  163                         tnode = de->td_node;
  164 
  165                         /*
  166                          * If we are not at the last path component and
  167                          * found a non-directory or non-link entry (which
  168                          * may itself be pointing to a directory), raise
  169                          * an error.
  170                          */
  171                         if ((tnode->tn_type != VDIR &&
  172                             tnode->tn_type != VLNK) &&
  173                             !(cnp->cn_flags & ISLASTCN)) {
  174                                 error = ENOTDIR;
  175                                 goto out;
  176                         }
  177 
  178                         /*
  179                          * If we are deleting or renaming the entry, keep
  180                          * track of its tmpfs_dirent so that it can be
  181                          * easily deleted later.
  182                          */
  183                         if ((cnp->cn_flags & ISLASTCN) &&
  184                             (cnp->cn_nameiop == DELETE ||
  185                             cnp->cn_nameiop == RENAME)) {
  186                                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
  187                                     cnp->cn_thread);
  188                                 if (error != 0)
  189                                         goto out;
  190 
  191                                 /* Allocate a new vnode on the matching entry. */
  192                                 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
  193                                     cnp->cn_lkflags, vpp);
  194                                 if (error != 0)
  195                                         goto out;
  196 
  197                                 if ((dnode->tn_mode & S_ISTXT) &&
  198                                   VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
  199                                   cnp->cn_thread) && VOP_ACCESS(*vpp, VADMIN,
  200                                   cnp->cn_cred, cnp->cn_thread)) {
  201                                         error = EPERM;
  202                                         vput(*vpp);
  203                                         *vpp = NULL;
  204                                         goto out;
  205                                 }
  206                                 cnp->cn_flags |= SAVENAME;
  207                         } else {
  208                                 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
  209                                     cnp->cn_lkflags, vpp);
  210                                 if (error != 0)
  211                                         goto out;
  212                         }
  213                 }
  214         }
  215 
  216         /*
  217          * Store the result of this lookup in the cache.  Avoid this if the
  218          * request was for creation, as it does not improve timings on
  219          * emprical tests.
  220          */
  221         if ((cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
  222                 cache_enter(dvp, *vpp, cnp);
  223 
  224 out:
  225         /*
  226          * If there were no errors, *vpp cannot be null and it must be
  227          * locked.
  228          */
  229         MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
  230 
  231         return (error);
  232 }
  233 
  234 static int
  235 tmpfs_cached_lookup(struct vop_cachedlookup_args *v)
  236 {
  237 
  238         return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
  239 }
  240 
  241 static int
  242 tmpfs_lookup(struct vop_lookup_args *v)
  243 {
  244         struct vnode *dvp = v->a_dvp;
  245         struct vnode **vpp = v->a_vpp;
  246         struct componentname *cnp = v->a_cnp;
  247         int error;
  248 
  249         /* Check accessibility of requested node as a first step. */
  250         error = vn_dir_check_exec(dvp, cnp);
  251         if (error != 0)
  252                 return (error);
  253 
  254         return (tmpfs_lookup1(dvp, vpp, cnp));
  255 }
  256 
  257 static int
  258 tmpfs_create(struct vop_create_args *v)
  259 {
  260         struct vnode *dvp = v->a_dvp;
  261         struct vnode **vpp = v->a_vpp;
  262         struct componentname *cnp = v->a_cnp;
  263         struct vattr *vap = v->a_vap;
  264         int error;
  265 
  266         MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
  267 
  268         error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
  269         if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
  270                 cache_enter(dvp, *vpp, cnp);
  271         return (error);
  272 }
  273 
  274 static int
  275 tmpfs_mknod(struct vop_mknod_args *v)
  276 {
  277         struct vnode *dvp = v->a_dvp;
  278         struct vnode **vpp = v->a_vpp;
  279         struct componentname *cnp = v->a_cnp;
  280         struct vattr *vap = v->a_vap;
  281 
  282         if (vap->va_type != VBLK && vap->va_type != VCHR &&
  283             vap->va_type != VFIFO)
  284                 return (EINVAL);
  285 
  286         return (tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL));
  287 }
  288 
  289 struct fileops tmpfs_fnops;
  290 
  291 static int
  292 tmpfs_open(struct vop_open_args *v)
  293 {
  294         struct vnode *vp;
  295         struct tmpfs_node *node;
  296         struct file *fp;
  297         int error, mode;
  298 
  299         vp = v->a_vp;
  300         mode = v->a_mode;
  301         node = VP_TO_TMPFS_NODE(vp);
  302 
  303         /*
  304          * The file is still active but all its names have been removed
  305          * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
  306          * it is about to die.
  307          */
  308         if (node->tn_links < 1)
  309                 return (ENOENT);
  310 
  311         /* If the file is marked append-only, deny write requests. */
  312         if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
  313                 error = EPERM;
  314         else {
  315                 error = 0;
  316                 /* For regular files, the call below is nop. */
  317                 KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
  318                     OBJ_DEAD) == 0, ("dead object"));
  319                 vnode_create_vobject(vp, node->tn_size, v->a_td);
  320         }
  321 
  322         fp = v->a_fp;
  323         MPASS(fp == NULL || fp->f_data == NULL);
  324         if (error == 0 && fp != NULL && vp->v_type == VREG) {
  325                 tmpfs_ref_node(node);
  326                 finit_vnode(fp, mode, node, &tmpfs_fnops);
  327         }
  328 
  329         return (error);
  330 }
  331 
  332 static int
  333 tmpfs_close(struct vop_close_args *v)
  334 {
  335         struct vnode *vp = v->a_vp;
  336 
  337         /* Update node times. */
  338         tmpfs_update(vp);
  339 
  340         return (0);
  341 }
  342 
  343 int
  344 tmpfs_fo_close(struct file *fp, struct thread *td)
  345 {
  346         struct tmpfs_node *node;
  347 
  348         node = fp->f_data;
  349         if (node != NULL) {
  350                 MPASS(node->tn_type == VREG);
  351                 tmpfs_free_node(node->tn_reg.tn_tmp, node);
  352         }
  353         return (vnops.fo_close(fp, td));
  354 }
  355 
  356 /*
  357  * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
  358  * the comment above cache_fplookup for details.
  359  */
  360 int
  361 tmpfs_fplookup_vexec(struct vop_fplookup_vexec_args *v)
  362 {
  363         struct vnode *vp;
  364         struct tmpfs_node *node;
  365         struct ucred *cred;
  366         mode_t all_x, mode;
  367 
  368         vp = v->a_vp;
  369         node = VP_TO_TMPFS_NODE_SMR(vp);
  370         if (__predict_false(node == NULL))
  371                 return (EAGAIN);
  372 
  373         all_x = S_IXUSR | S_IXGRP | S_IXOTH;
  374         mode = atomic_load_short(&node->tn_mode);
  375         if (__predict_true((mode & all_x) == all_x))
  376                 return (0);
  377 
  378         cred = v->a_cred;
  379         return (vaccess_vexec_smr(mode, node->tn_uid, node->tn_gid, cred));
  380 }
  381 
  382 int
  383 tmpfs_access(struct vop_access_args *v)
  384 {
  385         struct vnode *vp = v->a_vp;
  386         accmode_t accmode = v->a_accmode;
  387         struct ucred *cred = v->a_cred;
  388         mode_t all_x = S_IXUSR | S_IXGRP | S_IXOTH;
  389         int error;
  390         struct tmpfs_node *node;
  391 
  392         MPASS(VOP_ISLOCKED(vp));
  393 
  394         node = VP_TO_TMPFS_NODE(vp);
  395 
  396         /*
  397          * Common case path lookup.
  398          */
  399         if (__predict_true(accmode == VEXEC && (node->tn_mode & all_x) == all_x))
  400                 return (0);
  401 
  402         switch (vp->v_type) {
  403         case VDIR:
  404                 /* FALLTHROUGH */
  405         case VLNK:
  406                 /* FALLTHROUGH */
  407         case VREG:
  408                 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
  409                         error = EROFS;
  410                         goto out;
  411                 }
  412                 break;
  413 
  414         case VBLK:
  415                 /* FALLTHROUGH */
  416         case VCHR:
  417                 /* FALLTHROUGH */
  418         case VSOCK:
  419                 /* FALLTHROUGH */
  420         case VFIFO:
  421                 break;
  422 
  423         default:
  424                 error = EINVAL;
  425                 goto out;
  426         }
  427 
  428         if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
  429                 error = EPERM;
  430                 goto out;
  431         }
  432 
  433         error = vaccess(vp->v_type, node->tn_mode, node->tn_uid, node->tn_gid,
  434             accmode, cred);
  435 
  436 out:
  437         MPASS(VOP_ISLOCKED(vp));
  438 
  439         return (error);
  440 }
  441 
  442 int
  443 tmpfs_stat(struct vop_stat_args *v)
  444 {
  445         struct vnode *vp = v->a_vp;
  446         struct stat *sb = v->a_sb;
  447         struct tmpfs_node *node;
  448         int error;
  449 
  450         node = VP_TO_TMPFS_NODE(vp);
  451 
  452         tmpfs_update_getattr(vp);
  453 
  454         error = vop_stat_helper_pre(v);
  455         if (__predict_false(error))
  456                 return (error);
  457 
  458         sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
  459         sb->st_ino = node->tn_id;
  460         sb->st_mode = node->tn_mode | VTTOIF(vp->v_type);
  461         sb->st_nlink = node->tn_links;
  462         sb->st_uid = node->tn_uid;
  463         sb->st_gid = node->tn_gid;
  464         sb->st_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
  465                 node->tn_rdev : NODEV;
  466         sb->st_size = node->tn_size;
  467         sb->st_atim.tv_sec = node->tn_atime.tv_sec;
  468         sb->st_atim.tv_nsec = node->tn_atime.tv_nsec;
  469         sb->st_mtim.tv_sec = node->tn_mtime.tv_sec;
  470         sb->st_mtim.tv_nsec = node->tn_mtime.tv_nsec;
  471         sb->st_ctim.tv_sec = node->tn_ctime.tv_sec;
  472         sb->st_ctim.tv_nsec = node->tn_ctime.tv_nsec;
  473         sb->st_birthtim.tv_sec = node->tn_birthtime.tv_sec;
  474         sb->st_birthtim.tv_nsec = node->tn_birthtime.tv_nsec;
  475         sb->st_blksize = PAGE_SIZE;
  476         sb->st_flags = node->tn_flags;
  477         sb->st_gen = node->tn_gen;
  478         if (vp->v_type == VREG) {
  479 #ifdef __ILP32__
  480                 vm_object_t obj = node->tn_reg.tn_aobj;
  481 
  482                 /* Handle torn read */
  483                 VM_OBJECT_RLOCK(obj);
  484 #endif
  485                 sb->st_blocks = ptoa(node->tn_reg.tn_pages);
  486 #ifdef __ILP32__
  487                 VM_OBJECT_RUNLOCK(obj);
  488 #endif
  489         } else {
  490                 sb->st_blocks = node->tn_size;
  491         }
  492         sb->st_blocks /= S_BLKSIZE;
  493         return (vop_stat_helper_post(v, error));
  494 }
  495 
  496 int
  497 tmpfs_getattr(struct vop_getattr_args *v)
  498 {
  499         struct vnode *vp = v->a_vp;
  500         struct vattr *vap = v->a_vap;
  501         struct tmpfs_node *node;
  502 
  503         node = VP_TO_TMPFS_NODE(vp);
  504 
  505         tmpfs_update_getattr(vp);
  506 
  507         vap->va_type = vp->v_type;
  508         vap->va_mode = node->tn_mode;
  509         vap->va_nlink = node->tn_links;
  510         vap->va_uid = node->tn_uid;
  511         vap->va_gid = node->tn_gid;
  512         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
  513         vap->va_fileid = node->tn_id;
  514         vap->va_size = node->tn_size;
  515         vap->va_blocksize = PAGE_SIZE;
  516         vap->va_atime = node->tn_atime;
  517         vap->va_mtime = node->tn_mtime;
  518         vap->va_ctime = node->tn_ctime;
  519         vap->va_birthtime = node->tn_birthtime;
  520         vap->va_gen = node->tn_gen;
  521         vap->va_flags = node->tn_flags;
  522         vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
  523             node->tn_rdev : NODEV;
  524         if (vp->v_type == VREG) {
  525 #ifdef __ILP32__
  526                 vm_object_t obj = node->tn_reg.tn_aobj;
  527 
  528                 VM_OBJECT_RLOCK(obj);
  529 #endif
  530                 vap->va_bytes = ptoa(node->tn_reg.tn_pages);
  531 #ifdef __ILP32__
  532                 VM_OBJECT_RUNLOCK(obj);
  533 #endif
  534         } else {
  535                 vap->va_bytes = node->tn_size;
  536         }
  537         vap->va_filerev = 0;
  538 
  539         return (0);
  540 }
  541 
  542 int
  543 tmpfs_setattr(struct vop_setattr_args *v)
  544 {
  545         struct vnode *vp = v->a_vp;
  546         struct vattr *vap = v->a_vap;
  547         struct ucred *cred = v->a_cred;
  548         struct thread *td = curthread;
  549 
  550         int error;
  551 
  552         MPASS(VOP_ISLOCKED(vp));
  553         ASSERT_VOP_IN_SEQC(vp);
  554 
  555         error = 0;
  556 
  557         /* Abort if any unsettable attribute is given. */
  558         if (vap->va_type != VNON ||
  559             vap->va_nlink != VNOVAL ||
  560             vap->va_fsid != VNOVAL ||
  561             vap->va_fileid != VNOVAL ||
  562             vap->va_blocksize != VNOVAL ||
  563             vap->va_gen != VNOVAL ||
  564             vap->va_rdev != VNOVAL ||
  565             vap->va_bytes != VNOVAL)
  566                 error = EINVAL;
  567 
  568         if (error == 0 && (vap->va_flags != VNOVAL))
  569                 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
  570 
  571         if (error == 0 && (vap->va_size != VNOVAL))
  572                 error = tmpfs_chsize(vp, vap->va_size, cred, td);
  573 
  574         if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
  575                 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
  576 
  577         if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
  578                 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
  579 
  580         if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
  581             vap->va_atime.tv_nsec != VNOVAL) ||
  582             (vap->va_mtime.tv_sec != VNOVAL &&
  583             vap->va_mtime.tv_nsec != VNOVAL) ||
  584             (vap->va_birthtime.tv_sec != VNOVAL &&
  585             vap->va_birthtime.tv_nsec != VNOVAL)))
  586                 error = tmpfs_chtimes(vp, vap, cred, td);
  587 
  588         /*
  589          * Update the node times.  We give preference to the error codes
  590          * generated by this function rather than the ones that may arise
  591          * from tmpfs_update.
  592          */
  593         tmpfs_update(vp);
  594 
  595         MPASS(VOP_ISLOCKED(vp));
  596 
  597         return (error);
  598 }
  599 
  600 static int
  601 tmpfs_read(struct vop_read_args *v)
  602 {
  603         struct vnode *vp;
  604         struct uio *uio;
  605         struct tmpfs_node *node;
  606 
  607         vp = v->a_vp;
  608         if (vp->v_type != VREG)
  609                 return (EISDIR);
  610         uio = v->a_uio;
  611         if (uio->uio_offset < 0)
  612                 return (EINVAL);
  613         node = VP_TO_TMPFS_NODE(vp);
  614         tmpfs_set_accessed(VFS_TO_TMPFS(vp->v_mount), node);
  615         return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
  616 }
  617 
  618 static int
  619 tmpfs_read_pgcache(struct vop_read_pgcache_args *v)
  620 {
  621         struct vnode *vp;
  622         struct tmpfs_node *node;
  623         vm_object_t object;
  624         off_t size;
  625         int error;
  626 
  627         vp = v->a_vp;
  628         VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) != 0, vp);
  629 
  630         if (v->a_uio->uio_offset < 0)
  631                 return (EINVAL);
  632 
  633         error = EJUSTRETURN;
  634         vfs_smr_enter();
  635 
  636         node = VP_TO_TMPFS_NODE_SMR(vp);
  637         if (node == NULL)
  638                 goto out_smr;
  639         MPASS(node->tn_type == VREG);
  640         MPASS(node->tn_refcount >= 1);
  641         object = node->tn_reg.tn_aobj;
  642         if (object == NULL)
  643                 goto out_smr;
  644 
  645         MPASS(object->type == tmpfs_pager_type);
  646         MPASS((object->flags & (OBJ_ANON | OBJ_DEAD | OBJ_SWAP)) ==
  647             OBJ_SWAP);
  648         if (!VN_IS_DOOMED(vp)) {
  649                 /* size cannot become shorter due to rangelock. */
  650                 size = node->tn_size;
  651                 tmpfs_set_accessed(node->tn_reg.tn_tmp, node);
  652                 vfs_smr_exit();
  653                 error = uiomove_object(object, size, v->a_uio);
  654                 return (error);
  655         }
  656 out_smr:
  657         vfs_smr_exit();
  658         return (error);
  659 }
  660 
  661 static int
  662 tmpfs_write(struct vop_write_args *v)
  663 {
  664         struct vnode *vp;
  665         struct uio *uio;
  666         struct tmpfs_node *node;
  667         off_t oldsize;
  668         ssize_t r;
  669         int error, ioflag;
  670         mode_t newmode;
  671 
  672         vp = v->a_vp;
  673         uio = v->a_uio;
  674         ioflag = v->a_ioflag;
  675         error = 0;
  676         node = VP_TO_TMPFS_NODE(vp);
  677         oldsize = node->tn_size;
  678 
  679         if (uio->uio_offset < 0 || vp->v_type != VREG)
  680                 return (EINVAL);
  681         if (uio->uio_resid == 0)
  682                 return (0);
  683         if (ioflag & IO_APPEND)
  684                 uio->uio_offset = node->tn_size;
  685         error = vn_rlimit_fsizex(vp, uio, VFS_TO_TMPFS(vp->v_mount)->
  686             tm_maxfilesize, &r, uio->uio_td);
  687         if (error != 0) {
  688                 vn_rlimit_fsizex_res(uio, r);
  689                 return (error);
  690         }
  691 
  692         if (uio->uio_offset + uio->uio_resid > node->tn_size) {
  693                 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
  694                     FALSE);
  695                 if (error != 0)
  696                         goto out;
  697         }
  698 
  699         error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
  700         node->tn_status |= TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED;
  701         node->tn_accessed = true;
  702         if (node->tn_mode & (S_ISUID | S_ISGID)) {
  703                 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID)) {
  704                         newmode = node->tn_mode & ~(S_ISUID | S_ISGID);
  705                         vn_seqc_write_begin(vp);
  706                         atomic_store_short(&node->tn_mode, newmode);
  707                         vn_seqc_write_end(vp);
  708                 }
  709         }
  710         if (error != 0)
  711                 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
  712 
  713 out:
  714         MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
  715         MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
  716 
  717         vn_rlimit_fsizex_res(uio, r);
  718         return (error);
  719 }
  720 
  721 static int
  722 tmpfs_fsync(struct vop_fsync_args *v)
  723 {
  724         struct vnode *vp = v->a_vp;
  725 
  726         MPASS(VOP_ISLOCKED(vp));
  727 
  728         tmpfs_check_mtime(vp);
  729         tmpfs_update(vp);
  730 
  731         return (0);
  732 }
  733 
  734 static int
  735 tmpfs_remove(struct vop_remove_args *v)
  736 {
  737         struct vnode *dvp = v->a_dvp;
  738         struct vnode *vp = v->a_vp;
  739 
  740         int error;
  741         struct tmpfs_dirent *de;
  742         struct tmpfs_mount *tmp;
  743         struct tmpfs_node *dnode;
  744         struct tmpfs_node *node;
  745 
  746         MPASS(VOP_ISLOCKED(dvp));
  747         MPASS(VOP_ISLOCKED(vp));
  748 
  749         if (vp->v_type == VDIR) {
  750                 error = EISDIR;
  751                 goto out;
  752         }
  753 
  754         dnode = VP_TO_TMPFS_DIR(dvp);
  755         node = VP_TO_TMPFS_NODE(vp);
  756         tmp = VFS_TO_TMPFS(vp->v_mount);
  757         de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
  758         MPASS(de != NULL);
  759 
  760         /* Files marked as immutable or append-only cannot be deleted. */
  761         if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
  762             (dnode->tn_flags & APPEND)) {
  763                 error = EPERM;
  764                 goto out;
  765         }
  766 
  767         /* Remove the entry from the directory; as it is a file, we do not
  768          * have to change the number of hard links of the directory. */
  769         tmpfs_dir_detach(dvp, de);
  770         if (v->a_cnp->cn_flags & DOWHITEOUT)
  771                 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
  772 
  773         /* Free the directory entry we just deleted.  Note that the node
  774          * referred by it will not be removed until the vnode is really
  775          * reclaimed. */
  776         tmpfs_free_dirent(tmp, de);
  777 
  778         node->tn_status |= TMPFS_NODE_CHANGED;
  779         node->tn_accessed = true;
  780         error = 0;
  781 
  782 out:
  783         return (error);
  784 }
  785 
  786 static int
  787 tmpfs_link(struct vop_link_args *v)
  788 {
  789         struct vnode *dvp = v->a_tdvp;
  790         struct vnode *vp = v->a_vp;
  791         struct componentname *cnp = v->a_cnp;
  792 
  793         int error;
  794         struct tmpfs_dirent *de;
  795         struct tmpfs_node *node;
  796 
  797         MPASS(VOP_ISLOCKED(dvp));
  798         MPASS(cnp->cn_flags & HASBUF);
  799         MPASS(dvp != vp); /* XXX When can this be false? */
  800         node = VP_TO_TMPFS_NODE(vp);
  801 
  802         /* Ensure that we do not overflow the maximum number of links imposed
  803          * by the system. */
  804         MPASS(node->tn_links <= TMPFS_LINK_MAX);
  805         if (node->tn_links == TMPFS_LINK_MAX) {
  806                 error = EMLINK;
  807                 goto out;
  808         }
  809 
  810         /* We cannot create links of files marked immutable or append-only. */
  811         if (node->tn_flags & (IMMUTABLE | APPEND)) {
  812                 error = EPERM;
  813                 goto out;
  814         }
  815 
  816         /* Allocate a new directory entry to represent the node. */
  817         error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
  818             cnp->cn_nameptr, cnp->cn_namelen, &de);
  819         if (error != 0)
  820                 goto out;
  821 
  822         /* Insert the new directory entry into the appropriate directory. */
  823         if (cnp->cn_flags & ISWHITEOUT)
  824                 tmpfs_dir_whiteout_remove(dvp, cnp);
  825         tmpfs_dir_attach(dvp, de);
  826 
  827         /* vp link count has changed, so update node times. */
  828         node->tn_status |= TMPFS_NODE_CHANGED;
  829         tmpfs_update(vp);
  830 
  831         error = 0;
  832 
  833 out:
  834         return (error);
  835 }
  836 
  837 /*
  838  * We acquire all but fdvp locks using non-blocking acquisitions.  If we
  839  * fail to acquire any lock in the path we will drop all held locks,
  840  * acquire the new lock in a blocking fashion, and then release it and
  841  * restart the rename.  This acquire/release step ensures that we do not
  842  * spin on a lock waiting for release.  On error release all vnode locks
  843  * and decrement references the way tmpfs_rename() would do.
  844  */
  845 static int
  846 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
  847     struct vnode *tdvp, struct vnode **tvpp,
  848     struct componentname *fcnp, struct componentname *tcnp)
  849 {
  850         struct vnode *nvp;
  851         struct mount *mp;
  852         struct tmpfs_dirent *de;
  853         int error, restarts = 0;
  854 
  855         VOP_UNLOCK(tdvp);
  856         if (*tvpp != NULL && *tvpp != tdvp)
  857                 VOP_UNLOCK(*tvpp);
  858         mp = fdvp->v_mount;
  859 
  860 relock:
  861         restarts += 1;
  862         error = vn_lock(fdvp, LK_EXCLUSIVE);
  863         if (error)
  864                 goto releout;
  865         if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
  866                 VOP_UNLOCK(fdvp);
  867                 error = vn_lock(tdvp, LK_EXCLUSIVE);
  868                 if (error)
  869                         goto releout;
  870                 VOP_UNLOCK(tdvp);
  871                 goto relock;
  872         }
  873         /*
  874          * Re-resolve fvp to be certain it still exists and fetch the
  875          * correct vnode.
  876          */
  877         de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
  878         if (de == NULL) {
  879                 VOP_UNLOCK(fdvp);
  880                 VOP_UNLOCK(tdvp);
  881                 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
  882                     (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
  883                         error = EINVAL;
  884                 else
  885                         error = ENOENT;
  886                 goto releout;
  887         }
  888         error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
  889         if (error != 0) {
  890                 VOP_UNLOCK(fdvp);
  891                 VOP_UNLOCK(tdvp);
  892                 if (error != EBUSY)
  893                         goto releout;
  894                 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
  895                 if (error != 0)
  896                         goto releout;
  897                 VOP_UNLOCK(nvp);
  898                 /*
  899                  * Concurrent rename race.
  900                  */
  901                 if (nvp == tdvp) {
  902                         vrele(nvp);
  903                         error = EINVAL;
  904                         goto releout;
  905                 }
  906                 vrele(*fvpp);
  907                 *fvpp = nvp;
  908                 goto relock;
  909         }
  910         vrele(*fvpp);
  911         *fvpp = nvp;
  912         VOP_UNLOCK(*fvpp);
  913         /*
  914          * Re-resolve tvp and acquire the vnode lock if present.
  915          */
  916         de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
  917         /*
  918          * If tvp disappeared we just carry on.
  919          */
  920         if (de == NULL && *tvpp != NULL) {
  921                 vrele(*tvpp);
  922                 *tvpp = NULL;
  923         }
  924         /*
  925          * Get the tvp ino if the lookup succeeded.  We may have to restart
  926          * if the non-blocking acquire fails.
  927          */
  928         if (de != NULL) {
  929                 nvp = NULL;
  930                 error = tmpfs_alloc_vp(mp, de->td_node,
  931                     LK_EXCLUSIVE | LK_NOWAIT, &nvp);
  932                 if (*tvpp != NULL)
  933                         vrele(*tvpp);
  934                 *tvpp = nvp;
  935                 if (error != 0) {
  936                         VOP_UNLOCK(fdvp);
  937                         VOP_UNLOCK(tdvp);
  938                         if (error != EBUSY)
  939                                 goto releout;
  940                         error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
  941                             &nvp);
  942                         if (error != 0)
  943                                 goto releout;
  944                         VOP_UNLOCK(nvp);
  945                         /*
  946                          * fdvp contains fvp, thus tvp (=fdvp) is not empty.
  947                          */
  948                         if (nvp == fdvp) {
  949                                 error = ENOTEMPTY;
  950                                 goto releout;
  951                         }
  952                         goto relock;
  953                 }
  954         }
  955         tmpfs_rename_restarts += restarts;
  956 
  957         return (0);
  958 
  959 releout:
  960         vrele(fdvp);
  961         vrele(*fvpp);
  962         vrele(tdvp);
  963         if (*tvpp != NULL)
  964                 vrele(*tvpp);
  965         tmpfs_rename_restarts += restarts;
  966 
  967         return (error);
  968 }
  969 
  970 static int
  971 tmpfs_rename(struct vop_rename_args *v)
  972 {
  973         struct vnode *fdvp = v->a_fdvp;
  974         struct vnode *fvp = v->a_fvp;
  975         struct componentname *fcnp = v->a_fcnp;
  976         struct vnode *tdvp = v->a_tdvp;
  977         struct vnode *tvp = v->a_tvp;
  978         struct componentname *tcnp = v->a_tcnp;
  979         char *newname;
  980         struct tmpfs_dirent *de;
  981         struct tmpfs_mount *tmp;
  982         struct tmpfs_node *fdnode;
  983         struct tmpfs_node *fnode;
  984         struct tmpfs_node *tnode;
  985         struct tmpfs_node *tdnode;
  986         int error;
  987         bool want_seqc_end;
  988 
  989         MPASS(VOP_ISLOCKED(tdvp));
  990         MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
  991         MPASS(fcnp->cn_flags & HASBUF);
  992         MPASS(tcnp->cn_flags & HASBUF);
  993 
  994         want_seqc_end = false;
  995 
  996         /*
  997          * Disallow cross-device renames.
  998          * XXX Why isn't this done by the caller?
  999          */
 1000         if (fvp->v_mount != tdvp->v_mount ||
 1001             (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
 1002                 error = EXDEV;
 1003                 goto out;
 1004         }
 1005 
 1006         /* If source and target are the same file, there is nothing to do. */
 1007         if (fvp == tvp) {
 1008                 error = 0;
 1009                 goto out;
 1010         }
 1011 
 1012         /*
 1013          * If we need to move the directory between entries, lock the
 1014          * source so that we can safely operate on it.
 1015          */
 1016         if (fdvp != tdvp && fdvp != tvp) {
 1017                 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
 1018                         error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
 1019                             fcnp, tcnp);
 1020                         if (error != 0)
 1021                                 return (error);
 1022                         ASSERT_VOP_ELOCKED(fdvp,
 1023                             "tmpfs_rename: fdvp not locked");
 1024                         ASSERT_VOP_ELOCKED(tdvp,
 1025                             "tmpfs_rename: tdvp not locked");
 1026                         if (tvp != NULL)
 1027                                 ASSERT_VOP_ELOCKED(tvp,
 1028                                     "tmpfs_rename: tvp not locked");
 1029                         if (fvp == tvp) {
 1030                                 error = 0;
 1031                                 goto out_locked;
 1032                         }
 1033                 }
 1034         }
 1035 
 1036         if (tvp != NULL)
 1037                 vn_seqc_write_begin(tvp);
 1038         vn_seqc_write_begin(tdvp);
 1039         vn_seqc_write_begin(fvp);
 1040         vn_seqc_write_begin(fdvp);
 1041         want_seqc_end = true;
 1042 
 1043         tmp = VFS_TO_TMPFS(tdvp->v_mount);
 1044         tdnode = VP_TO_TMPFS_DIR(tdvp);
 1045         tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
 1046         fdnode = VP_TO_TMPFS_DIR(fdvp);
 1047         fnode = VP_TO_TMPFS_NODE(fvp);
 1048         de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
 1049 
 1050         /*
 1051          * Entry can disappear before we lock fdvp,
 1052          * also avoid manipulating '.' and '..' entries.
 1053          */
 1054         if (de == NULL) {
 1055                 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
 1056                     (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
 1057                         error = EINVAL;
 1058                 else
 1059                         error = ENOENT;
 1060                 goto out_locked;
 1061         }
 1062         MPASS(de->td_node == fnode);
 1063 
 1064         /*
 1065          * If re-naming a directory to another preexisting directory
 1066          * ensure that the target directory is empty so that its
 1067          * removal causes no side effects.
 1068          * Kern_rename guarantees the destination to be a directory
 1069          * if the source is one.
 1070          */
 1071         if (tvp != NULL) {
 1072                 MPASS(tnode != NULL);
 1073 
 1074                 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
 1075                     (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
 1076                         error = EPERM;
 1077                         goto out_locked;
 1078                 }
 1079 
 1080                 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
 1081                         if (tnode->tn_size > 0) {
 1082                                 error = ENOTEMPTY;
 1083                                 goto out_locked;
 1084                         }
 1085                 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
 1086                         error = ENOTDIR;
 1087                         goto out_locked;
 1088                 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
 1089                         error = EISDIR;
 1090                         goto out_locked;
 1091                 } else {
 1092                         MPASS(fnode->tn_type != VDIR &&
 1093                                 tnode->tn_type != VDIR);
 1094                 }
 1095         }
 1096 
 1097         if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
 1098             || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
 1099                 error = EPERM;
 1100                 goto out_locked;
 1101         }
 1102 
 1103         /*
 1104          * Ensure that we have enough memory to hold the new name, if it
 1105          * has to be changed.
 1106          */
 1107         if (fcnp->cn_namelen != tcnp->cn_namelen ||
 1108             bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
 1109                 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
 1110         } else
 1111                 newname = NULL;
 1112 
 1113         /*
 1114          * If the node is being moved to another directory, we have to do
 1115          * the move.
 1116          */
 1117         if (fdnode != tdnode) {
 1118                 /*
 1119                  * In case we are moving a directory, we have to adjust its
 1120                  * parent to point to the new parent.
 1121                  */
 1122                 if (de->td_node->tn_type == VDIR) {
 1123                         struct tmpfs_node *n;
 1124 
 1125                         /*
 1126                          * Ensure the target directory is not a child of the
 1127                          * directory being moved.  Otherwise, we'd end up
 1128                          * with stale nodes.
 1129                          */
 1130                         n = tdnode;
 1131                         /*
 1132                          * TMPFS_LOCK guaranties that no nodes are freed while
 1133                          * traversing the list. Nodes can only be marked as
 1134                          * removed: tn_parent == NULL.
 1135                          */
 1136                         TMPFS_LOCK(tmp);
 1137                         TMPFS_NODE_LOCK(n);
 1138                         while (n != n->tn_dir.tn_parent) {
 1139                                 struct tmpfs_node *parent;
 1140 
 1141                                 if (n == fnode) {
 1142                                         TMPFS_NODE_UNLOCK(n);
 1143                                         TMPFS_UNLOCK(tmp);
 1144                                         error = EINVAL;
 1145                                         if (newname != NULL)
 1146                                                     free(newname, M_TMPFSNAME);
 1147                                         goto out_locked;
 1148                                 }
 1149                                 parent = n->tn_dir.tn_parent;
 1150                                 TMPFS_NODE_UNLOCK(n);
 1151                                 if (parent == NULL) {
 1152                                         n = NULL;
 1153                                         break;
 1154                                 }
 1155                                 TMPFS_NODE_LOCK(parent);
 1156                                 if (parent->tn_dir.tn_parent == NULL) {
 1157                                         TMPFS_NODE_UNLOCK(parent);
 1158                                         n = NULL;
 1159                                         break;
 1160                                 }
 1161                                 n = parent;
 1162                         }
 1163                         TMPFS_UNLOCK(tmp);
 1164                         if (n == NULL) {
 1165                                 error = EINVAL;
 1166                                 if (newname != NULL)
 1167                                             free(newname, M_TMPFSNAME);
 1168                                 goto out_locked;
 1169                         }
 1170                         TMPFS_NODE_UNLOCK(n);
 1171 
 1172                         /* Adjust the parent pointer. */
 1173                         TMPFS_VALIDATE_DIR(fnode);
 1174                         TMPFS_NODE_LOCK(de->td_node);
 1175                         de->td_node->tn_dir.tn_parent = tdnode;
 1176                         TMPFS_NODE_UNLOCK(de->td_node);
 1177 
 1178                         /*
 1179                          * As a result of changing the target of the '..'
 1180                          * entry, the link count of the source and target
 1181                          * directories has to be adjusted.
 1182                          */
 1183                         TMPFS_NODE_LOCK(tdnode);
 1184                         TMPFS_ASSERT_LOCKED(tdnode);
 1185                         tdnode->tn_links++;
 1186                         TMPFS_NODE_UNLOCK(tdnode);
 1187 
 1188                         TMPFS_NODE_LOCK(fdnode);
 1189                         TMPFS_ASSERT_LOCKED(fdnode);
 1190                         fdnode->tn_links--;
 1191                         TMPFS_NODE_UNLOCK(fdnode);
 1192                 }
 1193         }
 1194 
 1195         /*
 1196          * Do the move: just remove the entry from the source directory
 1197          * and insert it into the target one.
 1198          */
 1199         tmpfs_dir_detach(fdvp, de);
 1200 
 1201         if (fcnp->cn_flags & DOWHITEOUT)
 1202                 tmpfs_dir_whiteout_add(fdvp, fcnp);
 1203         if (tcnp->cn_flags & ISWHITEOUT)
 1204                 tmpfs_dir_whiteout_remove(tdvp, tcnp);
 1205 
 1206         /*
 1207          * If the name has changed, we need to make it effective by changing
 1208          * it in the directory entry.
 1209          */
 1210         if (newname != NULL) {
 1211                 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
 1212 
 1213                 free(de->ud.td_name, M_TMPFSNAME);
 1214                 de->ud.td_name = newname;
 1215                 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
 1216 
 1217                 fnode->tn_status |= TMPFS_NODE_CHANGED;
 1218                 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
 1219         }
 1220 
 1221         /*
 1222          * If we are overwriting an entry, we have to remove the old one
 1223          * from the target directory.
 1224          */
 1225         if (tvp != NULL) {
 1226                 struct tmpfs_dirent *tde;
 1227 
 1228                 /* Remove the old entry from the target directory. */
 1229                 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
 1230                 tmpfs_dir_detach(tdvp, tde);
 1231 
 1232                 /*
 1233                  * Free the directory entry we just deleted.  Note that the
 1234                  * node referred by it will not be removed until the vnode is
 1235                  * really reclaimed.
 1236                  */
 1237                 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
 1238         }
 1239 
 1240         tmpfs_dir_attach(tdvp, de);
 1241 
 1242         if (tmpfs_use_nc(fvp)) {
 1243                 cache_vop_rename(fdvp, fvp, tdvp, tvp, fcnp, tcnp);
 1244         }
 1245 
 1246         error = 0;
 1247 
 1248 out_locked:
 1249         if (fdvp != tdvp && fdvp != tvp)
 1250                 VOP_UNLOCK(fdvp);
 1251 
 1252 out:
 1253         if (want_seqc_end) {
 1254                 if (tvp != NULL)
 1255                         vn_seqc_write_end(tvp);
 1256                 vn_seqc_write_end(tdvp);
 1257                 vn_seqc_write_end(fvp);
 1258                 vn_seqc_write_end(fdvp);
 1259         }
 1260 
 1261         /*
 1262          * Release target nodes.
 1263          * XXX: I don't understand when tdvp can be the same as tvp, but
 1264          * other code takes care of this...
 1265          */
 1266         if (tdvp == tvp)
 1267                 vrele(tdvp);
 1268         else
 1269                 vput(tdvp);
 1270         if (tvp != NULL)
 1271                 vput(tvp);
 1272 
 1273         /* Release source nodes. */
 1274         vrele(fdvp);
 1275         vrele(fvp);
 1276 
 1277         return (error);
 1278 }
 1279 
 1280 static int
 1281 tmpfs_mkdir(struct vop_mkdir_args *v)
 1282 {
 1283         struct vnode *dvp = v->a_dvp;
 1284         struct vnode **vpp = v->a_vpp;
 1285         struct componentname *cnp = v->a_cnp;
 1286         struct vattr *vap = v->a_vap;
 1287 
 1288         MPASS(vap->va_type == VDIR);
 1289 
 1290         return (tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL));
 1291 }
 1292 
 1293 static int
 1294 tmpfs_rmdir(struct vop_rmdir_args *v)
 1295 {
 1296         struct vnode *dvp = v->a_dvp;
 1297         struct vnode *vp = v->a_vp;
 1298 
 1299         int error;
 1300         struct tmpfs_dirent *de;
 1301         struct tmpfs_mount *tmp;
 1302         struct tmpfs_node *dnode;
 1303         struct tmpfs_node *node;
 1304 
 1305         MPASS(VOP_ISLOCKED(dvp));
 1306         MPASS(VOP_ISLOCKED(vp));
 1307 
 1308         tmp = VFS_TO_TMPFS(dvp->v_mount);
 1309         dnode = VP_TO_TMPFS_DIR(dvp);
 1310         node = VP_TO_TMPFS_DIR(vp);
 1311 
 1312         /* Directories with more than two entries ('.' and '..') cannot be
 1313          * removed. */
 1314          if (node->tn_size > 0) {
 1315                  error = ENOTEMPTY;
 1316                  goto out;
 1317          }
 1318 
 1319         if ((dnode->tn_flags & APPEND)
 1320             || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
 1321                 error = EPERM;
 1322                 goto out;
 1323         }
 1324 
 1325         /* This invariant holds only if we are not trying to remove "..".
 1326           * We checked for that above so this is safe now. */
 1327         MPASS(node->tn_dir.tn_parent == dnode);
 1328 
 1329         /* Get the directory entry associated with node (vp).  This was
 1330          * filled by tmpfs_lookup while looking up the entry. */
 1331         de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
 1332         MPASS(TMPFS_DIRENT_MATCHES(de,
 1333             v->a_cnp->cn_nameptr,
 1334             v->a_cnp->cn_namelen));
 1335 
 1336         /* Check flags to see if we are allowed to remove the directory. */
 1337         if ((dnode->tn_flags & APPEND) != 0 ||
 1338             (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
 1339                 error = EPERM;
 1340                 goto out;
 1341         }
 1342 
 1343         /* Detach the directory entry from the directory (dnode). */
 1344         tmpfs_dir_detach(dvp, de);
 1345         if (v->a_cnp->cn_flags & DOWHITEOUT)
 1346                 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
 1347 
 1348         /* No vnode should be allocated for this entry from this point */
 1349         TMPFS_NODE_LOCK(node);
 1350         node->tn_links--;
 1351         node->tn_dir.tn_parent = NULL;
 1352         node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
 1353         node->tn_accessed = true;
 1354 
 1355         TMPFS_NODE_UNLOCK(node);
 1356 
 1357         TMPFS_NODE_LOCK(dnode);
 1358         dnode->tn_links--;
 1359         dnode->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
 1360         dnode->tn_accessed = true;
 1361         TMPFS_NODE_UNLOCK(dnode);
 1362 
 1363         if (tmpfs_use_nc(dvp)) {
 1364                 cache_vop_rmdir(dvp, vp);
 1365         }
 1366 
 1367         /* Free the directory entry we just deleted.  Note that the node
 1368          * referred by it will not be removed until the vnode is really
 1369          * reclaimed. */
 1370         tmpfs_free_dirent(tmp, de);
 1371 
 1372         /* Release the deleted vnode (will destroy the node, notify
 1373          * interested parties and clean it from the cache). */
 1374 
 1375         dnode->tn_status |= TMPFS_NODE_CHANGED;
 1376         tmpfs_update(dvp);
 1377 
 1378         error = 0;
 1379 
 1380 out:
 1381         return (error);
 1382 }
 1383 
 1384 static int
 1385 tmpfs_symlink(struct vop_symlink_args *v)
 1386 {
 1387         struct vnode *dvp = v->a_dvp;
 1388         struct vnode **vpp = v->a_vpp;
 1389         struct componentname *cnp = v->a_cnp;
 1390         struct vattr *vap = v->a_vap;
 1391         const char *target = v->a_target;
 1392 
 1393 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
 1394         MPASS(vap->va_type == VLNK);
 1395 #else
 1396         vap->va_type = VLNK;
 1397 #endif
 1398 
 1399         return (tmpfs_alloc_file(dvp, vpp, vap, cnp, target));
 1400 }
 1401 
 1402 static int
 1403 tmpfs_readdir(struct vop_readdir_args *va)
 1404 {
 1405         struct vnode *vp;
 1406         struct uio *uio;
 1407         struct tmpfs_mount *tm;
 1408         struct tmpfs_node *node;
 1409         u_long **cookies;
 1410         int *eofflag, *ncookies;
 1411         ssize_t startresid;
 1412         int error, maxcookies;
 1413 
 1414         vp = va->a_vp;
 1415         uio = va->a_uio;
 1416         eofflag = va->a_eofflag;
 1417         cookies = va->a_cookies;
 1418         ncookies = va->a_ncookies;
 1419 
 1420         /* This operation only makes sense on directory nodes. */
 1421         if (vp->v_type != VDIR)
 1422                 return (ENOTDIR);
 1423 
 1424         maxcookies = 0;
 1425         node = VP_TO_TMPFS_DIR(vp);
 1426         tm = VFS_TO_TMPFS(vp->v_mount);
 1427 
 1428         startresid = uio->uio_resid;
 1429 
 1430         /* Allocate cookies for NFS and compat modules. */
 1431         if (cookies != NULL && ncookies != NULL) {
 1432                 maxcookies = howmany(node->tn_size,
 1433                     sizeof(struct tmpfs_dirent)) + 2;
 1434                 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
 1435                     M_WAITOK);
 1436                 *ncookies = 0;
 1437         }
 1438 
 1439         if (cookies == NULL)
 1440                 error = tmpfs_dir_getdents(tm, node, uio, 0, NULL, NULL);
 1441         else
 1442                 error = tmpfs_dir_getdents(tm, node, uio, maxcookies, *cookies,
 1443                     ncookies);
 1444 
 1445         /* Buffer was filled without hitting EOF. */
 1446         if (error == EJUSTRETURN)
 1447                 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
 1448 
 1449         if (error != 0 && cookies != NULL && ncookies != NULL) {
 1450                 free(*cookies, M_TEMP);
 1451                 *cookies = NULL;
 1452                 *ncookies = 0;
 1453         }
 1454 
 1455         if (eofflag != NULL)
 1456                 *eofflag =
 1457                     (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
 1458 
 1459         return (error);
 1460 }
 1461 
 1462 static int
 1463 tmpfs_readlink(struct vop_readlink_args *v)
 1464 {
 1465         struct vnode *vp = v->a_vp;
 1466         struct uio *uio = v->a_uio;
 1467 
 1468         int error;
 1469         struct tmpfs_node *node;
 1470 
 1471         MPASS(uio->uio_offset == 0);
 1472         MPASS(vp->v_type == VLNK);
 1473 
 1474         node = VP_TO_TMPFS_NODE(vp);
 1475 
 1476         error = uiomove(node->tn_link_target, MIN(node->tn_size, uio->uio_resid),
 1477             uio);
 1478         tmpfs_set_accessed(VFS_TO_TMPFS(vp->v_mount), node);
 1479 
 1480         return (error);
 1481 }
 1482 
 1483 /*
 1484  * VOP_FPLOOKUP_SYMLINK routines are subject to special circumstances, see
 1485  * the comment above cache_fplookup for details.
 1486  *
 1487  * Check tmpfs_alloc_node for tmpfs-specific synchronisation notes.
 1488  */
 1489 static int
 1490 tmpfs_fplookup_symlink(struct vop_fplookup_symlink_args *v)
 1491 {
 1492         struct vnode *vp;
 1493         struct tmpfs_node *node;
 1494         char *symlink;
 1495 
 1496         vp = v->a_vp;
 1497         node = VP_TO_TMPFS_NODE_SMR(vp);
 1498         if (__predict_false(node == NULL))
 1499                 return (EAGAIN);
 1500         if (!atomic_load_char(&node->tn_link_smr))
 1501                 return (EAGAIN);
 1502         symlink = atomic_load_ptr(&node->tn_link_target);
 1503         if (symlink == NULL)
 1504                 return (EAGAIN);
 1505 
 1506         return (cache_symlink_resolve(v->a_fpl, symlink, node->tn_size));
 1507 }
 1508 
 1509 static int
 1510 tmpfs_inactive(struct vop_inactive_args *v)
 1511 {
 1512         struct vnode *vp;
 1513         struct tmpfs_node *node;
 1514 
 1515         vp = v->a_vp;
 1516         node = VP_TO_TMPFS_NODE(vp);
 1517         if (node->tn_links == 0)
 1518                 vrecycle(vp);
 1519         else
 1520                 tmpfs_check_mtime(vp);
 1521         return (0);
 1522 }
 1523 
 1524 static int
 1525 tmpfs_need_inactive(struct vop_need_inactive_args *ap)
 1526 {
 1527         struct vnode *vp;
 1528         struct tmpfs_node *node;
 1529         struct vm_object *obj;
 1530 
 1531         vp = ap->a_vp;
 1532         node = VP_TO_TMPFS_NODE(vp);
 1533         if (node->tn_links == 0)
 1534                 goto need;
 1535         if (vp->v_type == VREG) {
 1536                 obj = vp->v_object;
 1537                 if (obj->generation != obj->cleangeneration)
 1538                         goto need;
 1539         }
 1540         return (0);
 1541 need:
 1542         return (1);
 1543 }
 1544 
 1545 int
 1546 tmpfs_reclaim(struct vop_reclaim_args *v)
 1547 {
 1548         struct vnode *vp;
 1549         struct tmpfs_mount *tmp;
 1550         struct tmpfs_node *node;
 1551         bool unlock;
 1552 
 1553         vp = v->a_vp;
 1554         node = VP_TO_TMPFS_NODE(vp);
 1555         tmp = VFS_TO_TMPFS(vp->v_mount);
 1556 
 1557         if (vp->v_type == VREG)
 1558                 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
 1559         vp->v_object = NULL;
 1560 
 1561         TMPFS_LOCK(tmp);
 1562         TMPFS_NODE_LOCK(node);
 1563         tmpfs_free_vp(vp);
 1564 
 1565         /*
 1566          * If the node referenced by this vnode was deleted by the user,
 1567          * we must free its associated data structures (now that the vnode
 1568          * is being reclaimed).
 1569          */
 1570         unlock = true;
 1571         if (node->tn_links == 0 &&
 1572             (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
 1573                 node->tn_vpstate = TMPFS_VNODE_DOOMED;
 1574                 unlock = !tmpfs_free_node_locked(tmp, node, true);
 1575         }
 1576 
 1577         if (unlock) {
 1578                 TMPFS_NODE_UNLOCK(node);
 1579                 TMPFS_UNLOCK(tmp);
 1580         }
 1581 
 1582         MPASS(vp->v_data == NULL);
 1583         return (0);
 1584 }
 1585 
 1586 int
 1587 tmpfs_print(struct vop_print_args *v)
 1588 {
 1589         struct vnode *vp = v->a_vp;
 1590 
 1591         struct tmpfs_node *node;
 1592 
 1593         node = VP_TO_TMPFS_NODE(vp);
 1594 
 1595         printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %jd\n",
 1596             node, node->tn_flags, (uintmax_t)node->tn_links);
 1597         printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
 1598             node->tn_mode, node->tn_uid, node->tn_gid,
 1599             (intmax_t)node->tn_size, node->tn_status);
 1600 
 1601         if (vp->v_type == VFIFO)
 1602                 fifo_printinfo(vp);
 1603 
 1604         printf("\n");
 1605 
 1606         return (0);
 1607 }
 1608 
 1609 int
 1610 tmpfs_pathconf(struct vop_pathconf_args *v)
 1611 {
 1612         struct vnode *vp = v->a_vp;
 1613         int name = v->a_name;
 1614         long *retval = v->a_retval;
 1615 
 1616         int error;
 1617 
 1618         error = 0;
 1619 
 1620         switch (name) {
 1621         case _PC_LINK_MAX:
 1622                 *retval = TMPFS_LINK_MAX;
 1623                 break;
 1624 
 1625         case _PC_SYMLINK_MAX:
 1626                 *retval = MAXPATHLEN;
 1627                 break;
 1628 
 1629         case _PC_NAME_MAX:
 1630                 *retval = NAME_MAX;
 1631                 break;
 1632 
 1633         case _PC_PIPE_BUF:
 1634                 if (vp->v_type == VDIR || vp->v_type == VFIFO)
 1635                         *retval = PIPE_BUF;
 1636                 else
 1637                         error = EINVAL;
 1638                 break;
 1639 
 1640         case _PC_CHOWN_RESTRICTED:
 1641                 *retval = 1;
 1642                 break;
 1643 
 1644         case _PC_NO_TRUNC:
 1645                 *retval = 1;
 1646                 break;
 1647 
 1648         case _PC_SYNC_IO:
 1649                 *retval = 1;
 1650                 break;
 1651 
 1652         case _PC_FILESIZEBITS:
 1653                 *retval = 64;
 1654                 break;
 1655 
 1656         case _PC_MIN_HOLE_SIZE:
 1657                 *retval = PAGE_SIZE;
 1658                 break;
 1659 
 1660         default:
 1661                 error = vop_stdpathconf(v);
 1662         }
 1663 
 1664         return (error);
 1665 }
 1666 
 1667 static int
 1668 tmpfs_vptofh(struct vop_vptofh_args *ap)
 1669 /*
 1670 vop_vptofh {
 1671         IN struct vnode *a_vp;
 1672         IN struct fid *a_fhp;
 1673 };
 1674 */
 1675 {
 1676         struct tmpfs_fid_data tfd;
 1677         struct tmpfs_node *node;
 1678         struct fid *fhp;
 1679 
 1680         node = VP_TO_TMPFS_NODE(ap->a_vp);
 1681         fhp = ap->a_fhp;
 1682         fhp->fid_len = sizeof(tfd);
 1683 
 1684         /*
 1685          * Copy into fid_data from the stack to avoid unaligned pointer use.
 1686          * See the comment in sys/mount.h on struct fid for details.
 1687          */
 1688         tfd.tfd_id = node->tn_id;
 1689         tfd.tfd_gen = node->tn_gen;
 1690         memcpy(fhp->fid_data, &tfd, fhp->fid_len);
 1691 
 1692         return (0);
 1693 }
 1694 
 1695 static int
 1696 tmpfs_whiteout(struct vop_whiteout_args *ap)
 1697 {
 1698         struct vnode *dvp = ap->a_dvp;
 1699         struct componentname *cnp = ap->a_cnp;
 1700         struct tmpfs_dirent *de;
 1701 
 1702         switch (ap->a_flags) {
 1703         case LOOKUP:
 1704                 return (0);
 1705         case CREATE:
 1706                 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
 1707                 if (de != NULL)
 1708                         return (de->td_node == NULL ? 0 : EEXIST);
 1709                 return (tmpfs_dir_whiteout_add(dvp, cnp));
 1710         case DELETE:
 1711                 tmpfs_dir_whiteout_remove(dvp, cnp);
 1712                 return (0);
 1713         default:
 1714                 panic("tmpfs_whiteout: unknown op");
 1715         }
 1716 }
 1717 
 1718 static int
 1719 tmpfs_vptocnp_dir(struct tmpfs_node *tn, struct tmpfs_node *tnp,
 1720     struct tmpfs_dirent **pde)
 1721 {
 1722         struct tmpfs_dir_cursor dc;
 1723         struct tmpfs_dirent *de;
 1724 
 1725         for (de = tmpfs_dir_first(tnp, &dc); de != NULL;
 1726              de = tmpfs_dir_next(tnp, &dc)) {
 1727                 if (de->td_node == tn) {
 1728                         *pde = de;
 1729                         return (0);
 1730                 }
 1731         }
 1732         return (ENOENT);
 1733 }
 1734 
 1735 static int
 1736 tmpfs_vptocnp_fill(struct vnode *vp, struct tmpfs_node *tn,
 1737     struct tmpfs_node *tnp, char *buf, size_t *buflen, struct vnode **dvp)
 1738 {
 1739         struct tmpfs_dirent *de;
 1740         int error, i;
 1741 
 1742         error = vn_vget_ino_gen(vp, tmpfs_vn_get_ino_alloc, tnp, LK_SHARED,
 1743             dvp);
 1744         if (error != 0)
 1745                 return (error);
 1746         error = tmpfs_vptocnp_dir(tn, tnp, &de);
 1747         if (error == 0) {
 1748                 i = *buflen;
 1749                 i -= de->td_namelen;
 1750                 if (i < 0) {
 1751                         error = ENOMEM;
 1752                 } else {
 1753                         bcopy(de->ud.td_name, buf + i, de->td_namelen);
 1754                         *buflen = i;
 1755                 }
 1756         }
 1757         if (error == 0) {
 1758                 if (vp != *dvp)
 1759                         VOP_UNLOCK(*dvp);
 1760         } else {
 1761                 if (vp != *dvp)
 1762                         vput(*dvp);
 1763                 else
 1764                         vrele(vp);
 1765         }
 1766         return (error);
 1767 }
 1768 
 1769 static int
 1770 tmpfs_vptocnp(struct vop_vptocnp_args *ap)
 1771 {
 1772         struct vnode *vp, **dvp;
 1773         struct tmpfs_node *tn, *tnp, *tnp1;
 1774         struct tmpfs_dirent *de;
 1775         struct tmpfs_mount *tm;
 1776         char *buf;
 1777         size_t *buflen;
 1778         int error;
 1779 
 1780         vp = ap->a_vp;
 1781         dvp = ap->a_vpp;
 1782         buf = ap->a_buf;
 1783         buflen = ap->a_buflen;
 1784 
 1785         tm = VFS_TO_TMPFS(vp->v_mount);
 1786         tn = VP_TO_TMPFS_NODE(vp);
 1787         if (tn->tn_type == VDIR) {
 1788                 tnp = tn->tn_dir.tn_parent;
 1789                 if (tnp == NULL)
 1790                         return (ENOENT);
 1791                 tmpfs_ref_node(tnp);
 1792                 error = tmpfs_vptocnp_fill(vp, tn, tn->tn_dir.tn_parent, buf,
 1793                     buflen, dvp);
 1794                 tmpfs_free_node(tm, tnp);
 1795                 return (error);
 1796         }
 1797 restart:
 1798         TMPFS_LOCK(tm);
 1799 restart_locked:
 1800         LIST_FOREACH_SAFE(tnp, &tm->tm_nodes_used, tn_entries, tnp1) {
 1801                 if (tnp->tn_type != VDIR)
 1802                         continue;
 1803                 TMPFS_NODE_LOCK(tnp);
 1804                 tmpfs_ref_node(tnp);
 1805 
 1806                 /*
 1807                  * tn_vnode cannot be instantiated while we hold the
 1808                  * node lock, so the directory cannot be changed while
 1809                  * we iterate over it.  Do this to avoid instantiating
 1810                  * vnode for directories which cannot point to our
 1811                  * node.
 1812                  */
 1813                 error = tnp->tn_vnode == NULL ? tmpfs_vptocnp_dir(tn, tnp,
 1814                     &de) : 0;
 1815 
 1816                 if (error == 0) {
 1817                         TMPFS_NODE_UNLOCK(tnp);
 1818                         TMPFS_UNLOCK(tm);
 1819                         error = tmpfs_vptocnp_fill(vp, tn, tnp, buf, buflen,
 1820                             dvp);
 1821                         if (error == 0) {
 1822                                 tmpfs_free_node(tm, tnp);
 1823                                 return (0);
 1824                         }
 1825                         if (VN_IS_DOOMED(vp)) {
 1826                                 tmpfs_free_node(tm, tnp);
 1827                                 return (ENOENT);
 1828                         }
 1829                         TMPFS_LOCK(tm);
 1830                         TMPFS_NODE_LOCK(tnp);
 1831                 }
 1832                 if (tmpfs_free_node_locked(tm, tnp, false)) {
 1833                         goto restart;
 1834                 } else {
 1835                         KASSERT(tnp->tn_refcount > 0,
 1836                             ("node %p refcount zero", tnp));
 1837                         if (tnp->tn_attached) {
 1838                                 tnp1 = LIST_NEXT(tnp, tn_entries);
 1839                                 TMPFS_NODE_UNLOCK(tnp);
 1840                         } else {
 1841                                 TMPFS_NODE_UNLOCK(tnp);
 1842                                 goto restart_locked;
 1843                         }
 1844                 }
 1845         }
 1846         TMPFS_UNLOCK(tm);
 1847         return (ENOENT);
 1848 }
 1849 
 1850 static off_t
 1851 tmpfs_seek_data_locked(vm_object_t obj, off_t noff)
 1852 {
 1853         vm_page_t m;
 1854         vm_pindex_t p, p_m, p_swp;
 1855 
 1856         p = OFF_TO_IDX(noff);
 1857         m = vm_page_find_least(obj, p);
 1858 
 1859         /*
 1860          * Microoptimize the most common case for SEEK_DATA, where
 1861          * there is no hole and the page is resident.
 1862          */
 1863         if (m != NULL && vm_page_any_valid(m) && m->pindex == p)
 1864                 return (noff);
 1865 
 1866         p_swp = swap_pager_find_least(obj, p);
 1867         if (p_swp == p)
 1868                 return (noff);
 1869 
 1870         p_m = m == NULL ? obj->size : m->pindex;
 1871         return (IDX_TO_OFF(MIN(p_m, p_swp)));
 1872 }
 1873 
 1874 static off_t
 1875 tmpfs_seek_next(off_t noff)
 1876 {
 1877         return (noff + PAGE_SIZE - (noff & PAGE_MASK));
 1878 }
 1879 
 1880 static int
 1881 tmpfs_seek_clamp(struct tmpfs_node *tn, off_t *noff, bool seekdata)
 1882 {
 1883         if (*noff < tn->tn_size)
 1884                 return (0);
 1885         if (seekdata)
 1886                 return (ENXIO);
 1887         *noff = tn->tn_size;
 1888         return (0);
 1889 }
 1890 
 1891 static off_t
 1892 tmpfs_seek_hole_locked(vm_object_t obj, off_t noff)
 1893 {
 1894         vm_page_t m;
 1895         vm_pindex_t p, p_swp;
 1896 
 1897         for (;; noff = tmpfs_seek_next(noff)) {
 1898                 /*
 1899                  * Walk over the largest sequential run of the valid pages.
 1900                  */
 1901                 for (m = vm_page_lookup(obj, OFF_TO_IDX(noff));
 1902                     m != NULL && vm_page_any_valid(m);
 1903                     m = vm_page_next(m), noff = tmpfs_seek_next(noff))
 1904                         ;
 1905 
 1906                 /*
 1907                  * Found a hole in the object's page queue.  Check if
 1908                  * there is a hole in the swap at the same place.
 1909                  */
 1910                 p = OFF_TO_IDX(noff);
 1911                 p_swp = swap_pager_find_least(obj, p);
 1912                 if (p_swp != p) {
 1913                         noff = IDX_TO_OFF(p);
 1914                         break;
 1915                 }
 1916         }
 1917         return (noff);
 1918 }
 1919 
 1920 static int
 1921 tmpfs_seek_datahole(struct vnode *vp, off_t *off, bool seekdata)
 1922 {
 1923         struct tmpfs_node *tn;
 1924         vm_object_t obj;
 1925         off_t noff;
 1926         int error;
 1927 
 1928         if (vp->v_type != VREG)
 1929                 return (ENOTTY);
 1930         tn = VP_TO_TMPFS_NODE(vp);
 1931         noff = *off;
 1932         if (noff < 0)
 1933                 return (ENXIO);
 1934         error = tmpfs_seek_clamp(tn, &noff, seekdata);
 1935         if (error != 0)
 1936                 return (error);
 1937         obj = tn->tn_reg.tn_aobj;
 1938 
 1939         VM_OBJECT_RLOCK(obj);
 1940         noff = seekdata ? tmpfs_seek_data_locked(obj, noff) :
 1941             tmpfs_seek_hole_locked(obj, noff);
 1942         VM_OBJECT_RUNLOCK(obj);
 1943 
 1944         error = tmpfs_seek_clamp(tn, &noff, seekdata);
 1945         if (error == 0)
 1946                 *off = noff;
 1947         return (error);
 1948 }
 1949 
 1950 static int
 1951 tmpfs_ioctl(struct vop_ioctl_args *ap)
 1952 {
 1953         struct vnode *vp = ap->a_vp;
 1954         int error = 0;
 1955 
 1956         switch (ap->a_command) {
 1957         case FIOSEEKDATA:
 1958         case FIOSEEKHOLE:
 1959                 error = vn_lock(vp, LK_SHARED);
 1960                 if (error != 0) {
 1961                         error = EBADF;
 1962                         break;
 1963                 }
 1964                 error = tmpfs_seek_datahole(vp, (off_t *)ap->a_data,
 1965                     ap->a_command == FIOSEEKDATA);
 1966                 VOP_UNLOCK(vp);
 1967                 break;
 1968         default:
 1969                 error = ENOTTY;
 1970                 break;
 1971         }
 1972         return (error);
 1973 }
 1974 
 1975 /*
 1976  * Vnode operations vector used for files stored in a tmpfs file system.
 1977  */
 1978 struct vop_vector tmpfs_vnodeop_entries = {
 1979         .vop_default =                  &default_vnodeops,
 1980         .vop_lookup =                   vfs_cache_lookup,
 1981         .vop_cachedlookup =             tmpfs_cached_lookup,
 1982         .vop_create =                   tmpfs_create,
 1983         .vop_mknod =                    tmpfs_mknod,
 1984         .vop_open =                     tmpfs_open,
 1985         .vop_close =                    tmpfs_close,
 1986         .vop_fplookup_vexec =           tmpfs_fplookup_vexec,
 1987         .vop_fplookup_symlink =         tmpfs_fplookup_symlink,
 1988         .vop_access =                   tmpfs_access,
 1989         .vop_stat =                     tmpfs_stat,
 1990         .vop_getattr =                  tmpfs_getattr,
 1991         .vop_setattr =                  tmpfs_setattr,
 1992         .vop_read =                     tmpfs_read,
 1993         .vop_read_pgcache =             tmpfs_read_pgcache,
 1994         .vop_write =                    tmpfs_write,
 1995         .vop_fsync =                    tmpfs_fsync,
 1996         .vop_remove =                   tmpfs_remove,
 1997         .vop_link =                     tmpfs_link,
 1998         .vop_rename =                   tmpfs_rename,
 1999         .vop_mkdir =                    tmpfs_mkdir,
 2000         .vop_rmdir =                    tmpfs_rmdir,
 2001         .vop_symlink =                  tmpfs_symlink,
 2002         .vop_readdir =                  tmpfs_readdir,
 2003         .vop_readlink =                 tmpfs_readlink,
 2004         .vop_inactive =                 tmpfs_inactive,
 2005         .vop_need_inactive =            tmpfs_need_inactive,
 2006         .vop_reclaim =                  tmpfs_reclaim,
 2007         .vop_print =                    tmpfs_print,
 2008         .vop_pathconf =                 tmpfs_pathconf,
 2009         .vop_vptofh =                   tmpfs_vptofh,
 2010         .vop_whiteout =                 tmpfs_whiteout,
 2011         .vop_bmap =                     VOP_EOPNOTSUPP,
 2012         .vop_vptocnp =                  tmpfs_vptocnp,
 2013         .vop_lock1 =                    vop_lock,
 2014         .vop_unlock =                   vop_unlock,
 2015         .vop_islocked =                 vop_islocked,
 2016         .vop_ioctl =                    tmpfs_ioctl,
 2017 };
 2018 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_entries);
 2019 
 2020 /*
 2021  * Same vector for mounts which do not use namecache.
 2022  */
 2023 struct vop_vector tmpfs_vnodeop_nonc_entries = {
 2024         .vop_default =                  &tmpfs_vnodeop_entries,
 2025         .vop_lookup =                   tmpfs_lookup,
 2026 };
 2027 VFS_VOP_VECTOR_REGISTER(tmpfs_vnodeop_nonc_entries);

Cache object: 97bb58aaf9a7e5968c2a5aa7c90d80b3


[ 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.