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  * Copyright (c) 2005, 2006 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  * tmpfs vnode interface.
   35  */
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD$");
   38 
   39 #include <sys/param.h>
   40 #include <sys/fcntl.h>
   41 #include <sys/lockf.h>
   42 #include <sys/lock.h>
   43 #include <sys/namei.h>
   44 #include <sys/priv.h>
   45 #include <sys/proc.h>
   46 #include <sys/rwlock.h>
   47 #include <sys/sched.h>
   48 #include <sys/stat.h>
   49 #include <sys/systm.h>
   50 #include <sys/sysctl.h>
   51 #include <sys/unistd.h>
   52 #include <sys/vnode.h>
   53 
   54 #include <vm/vm.h>
   55 #include <vm/vm_param.h>
   56 #include <vm/vm_object.h>
   57 #include <vm/vm_page.h>
   58 #include <vm/vm_pager.h>
   59 
   60 #include <fs/tmpfs/tmpfs_vnops.h>
   61 #include <fs/tmpfs/tmpfs.h>
   62 
   63 SYSCTL_DECL(_vfs_tmpfs);
   64 
   65 static volatile int tmpfs_rename_restarts;
   66 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
   67     __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
   68     "Times rename had to restart due to lock contention");
   69 
   70 static int
   71 tmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
   72     struct vnode **rvp)
   73 {
   74 
   75         return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
   76 }
   77 
   78 static int
   79 tmpfs_lookup1(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
   80 {
   81         struct tmpfs_dirent *de;
   82         struct tmpfs_node *dnode, *pnode;
   83         struct tmpfs_mount *tm;
   84         int error;
   85 
   86         dnode = VP_TO_TMPFS_DIR(dvp);
   87         *vpp = NULLVP;
   88 
   89         /* Check accessibility of requested node as a first step. */
   90         error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
   91         if (error != 0)
   92                 goto out;
   93 
   94         /* We cannot be requesting the parent directory of the root node. */
   95         MPASS(IMPLIES(dnode->tn_type == VDIR &&
   96             dnode->tn_dir.tn_parent == dnode,
   97             !(cnp->cn_flags & ISDOTDOT)));
   98 
   99         TMPFS_ASSERT_LOCKED(dnode);
  100         if (dnode->tn_dir.tn_parent == NULL) {
  101                 error = ENOENT;
  102                 goto out;
  103         }
  104         if (cnp->cn_flags & ISDOTDOT) {
  105                 tm = VFS_TO_TMPFS(dvp->v_mount);
  106                 pnode = dnode->tn_dir.tn_parent;
  107                 tmpfs_ref_node(pnode);
  108                 error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
  109                     pnode, cnp->cn_lkflags, vpp);
  110                 tmpfs_free_node(tm, pnode);
  111                 if (error != 0)
  112                         goto out;
  113         } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
  114                 VREF(dvp);
  115                 *vpp = dvp;
  116                 error = 0;
  117         } else {
  118                 de = tmpfs_dir_lookup(dnode, NULL, cnp);
  119                 if (de != NULL && de->td_node == NULL)
  120                         cnp->cn_flags |= ISWHITEOUT;
  121                 if (de == NULL || de->td_node == NULL) {
  122                         /*
  123                          * The entry was not found in the directory.
  124                          * This is OK if we are creating or renaming an
  125                          * entry and are working on the last component of
  126                          * the path name.
  127                          */
  128                         if ((cnp->cn_flags & ISLASTCN) &&
  129                             (cnp->cn_nameiop == CREATE || \
  130                             cnp->cn_nameiop == RENAME ||
  131                             (cnp->cn_nameiop == DELETE &&
  132                             cnp->cn_flags & DOWHITEOUT &&
  133                             cnp->cn_flags & ISWHITEOUT))) {
  134                                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
  135                                     cnp->cn_thread);
  136                                 if (error != 0)
  137                                         goto out;
  138 
  139                                 /*
  140                                  * Keep the component name in the buffer for
  141                                  * future uses.
  142                                  */
  143                                 cnp->cn_flags |= SAVENAME;
  144 
  145                                 error = EJUSTRETURN;
  146                         } else
  147                                 error = ENOENT;
  148                 } else {
  149                         struct tmpfs_node *tnode;
  150 
  151                         /*
  152                          * The entry was found, so get its associated
  153                          * tmpfs_node.
  154                          */
  155                         tnode = de->td_node;
  156 
  157                         /*
  158                          * If we are not at the last path component and
  159                          * found a non-directory or non-link entry (which
  160                          * may itself be pointing to a directory), raise
  161                          * an error.
  162                          */
  163                         if ((tnode->tn_type != VDIR &&
  164                             tnode->tn_type != VLNK) &&
  165                             !(cnp->cn_flags & ISLASTCN)) {
  166                                 error = ENOTDIR;
  167                                 goto out;
  168                         }
  169 
  170                         /*
  171                          * If we are deleting or renaming the entry, keep
  172                          * track of its tmpfs_dirent so that it can be
  173                          * easily deleted later.
  174                          */
  175                         if ((cnp->cn_flags & ISLASTCN) &&
  176                             (cnp->cn_nameiop == DELETE ||
  177                             cnp->cn_nameiop == RENAME)) {
  178                                 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
  179                                     cnp->cn_thread);
  180                                 if (error != 0)
  181                                         goto out;
  182 
  183                                 /* Allocate a new vnode on the matching entry. */
  184                                 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
  185                                     cnp->cn_lkflags, vpp);
  186                                 if (error != 0)
  187                                         goto out;
  188 
  189                                 if ((dnode->tn_mode & S_ISTXT) &&
  190                                   VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
  191                                   cnp->cn_thread) && VOP_ACCESS(*vpp, VADMIN,
  192                                   cnp->cn_cred, cnp->cn_thread)) {
  193                                         error = EPERM;
  194                                         vput(*vpp);
  195                                         *vpp = NULL;
  196                                         goto out;
  197                                 }
  198                                 cnp->cn_flags |= SAVENAME;
  199                         } else {
  200                                 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
  201                                     cnp->cn_lkflags, vpp);
  202                                 if (error != 0)
  203                                         goto out;
  204                         }
  205                 }
  206         }
  207 
  208         /*
  209          * Store the result of this lookup in the cache.  Avoid this if the
  210          * request was for creation, as it does not improve timings on
  211          * emprical tests.
  212          */
  213         if ((cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
  214                 cache_enter(dvp, *vpp, cnp);
  215 
  216 out:
  217         /*
  218          * If there were no errors, *vpp cannot be null and it must be
  219          * locked.
  220          */
  221         MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
  222 
  223         return (error);
  224 }
  225 
  226 static int
  227 tmpfs_cached_lookup(struct vop_cachedlookup_args *v)
  228 {
  229 
  230         return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
  231 }
  232 
  233 static int
  234 tmpfs_lookup(struct vop_lookup_args *v)
  235 {
  236 
  237         return (tmpfs_lookup1(v->a_dvp, v->a_vpp, v->a_cnp));
  238 }
  239 
  240 static int
  241 tmpfs_create(struct vop_create_args *v)
  242 {
  243         struct vnode *dvp = v->a_dvp;
  244         struct vnode **vpp = v->a_vpp;
  245         struct componentname *cnp = v->a_cnp;
  246         struct vattr *vap = v->a_vap;
  247         int error;
  248 
  249         MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
  250 
  251         error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
  252         if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0 && tmpfs_use_nc(dvp))
  253                 cache_enter(dvp, *vpp, cnp);
  254         return (error);
  255 }
  256 
  257 static int
  258 tmpfs_mknod(struct vop_mknod_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 
  265         if (vap->va_type != VBLK && vap->va_type != VCHR &&
  266             vap->va_type != VFIFO)
  267                 return EINVAL;
  268 
  269         return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
  270 }
  271 
  272 static int
  273 tmpfs_open(struct vop_open_args *v)
  274 {
  275         struct vnode *vp = v->a_vp;
  276         int mode = v->a_mode;
  277 
  278         int error;
  279         struct tmpfs_node *node;
  280 
  281         MPASS(VOP_ISLOCKED(vp));
  282 
  283         node = VP_TO_TMPFS_NODE(vp);
  284 
  285         /* The file is still active but all its names have been removed
  286          * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
  287          * it is about to die. */
  288         if (node->tn_links < 1)
  289                 return (ENOENT);
  290 
  291         /* If the file is marked append-only, deny write requests. */
  292         if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
  293                 error = EPERM;
  294         else {
  295                 error = 0;
  296                 /* For regular files, the call below is nop. */
  297                 KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
  298                     OBJ_DEAD) == 0, ("dead object"));
  299                 vnode_create_vobject(vp, node->tn_size, v->a_td);
  300         }
  301 
  302         MPASS(VOP_ISLOCKED(vp));
  303         return error;
  304 }
  305 
  306 static int
  307 tmpfs_close(struct vop_close_args *v)
  308 {
  309         struct vnode *vp = v->a_vp;
  310 
  311         /* Update node times. */
  312         tmpfs_update(vp);
  313 
  314         return (0);
  315 }
  316 
  317 int
  318 tmpfs_access(struct vop_access_args *v)
  319 {
  320         struct vnode *vp = v->a_vp;
  321         accmode_t accmode = v->a_accmode;
  322         struct ucred *cred = v->a_cred;
  323 
  324         int error;
  325         struct tmpfs_node *node;
  326 
  327         MPASS(VOP_ISLOCKED(vp));
  328 
  329         node = VP_TO_TMPFS_NODE(vp);
  330 
  331         switch (vp->v_type) {
  332         case VDIR:
  333                 /* FALLTHROUGH */
  334         case VLNK:
  335                 /* FALLTHROUGH */
  336         case VREG:
  337                 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
  338                         error = EROFS;
  339                         goto out;
  340                 }
  341                 break;
  342 
  343         case VBLK:
  344                 /* FALLTHROUGH */
  345         case VCHR:
  346                 /* FALLTHROUGH */
  347         case VSOCK:
  348                 /* FALLTHROUGH */
  349         case VFIFO:
  350                 break;
  351 
  352         default:
  353                 error = EINVAL;
  354                 goto out;
  355         }
  356 
  357         if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
  358                 error = EPERM;
  359                 goto out;
  360         }
  361 
  362         error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
  363             node->tn_gid, accmode, cred, NULL);
  364 
  365 out:
  366         MPASS(VOP_ISLOCKED(vp));
  367 
  368         return error;
  369 }
  370 
  371 int
  372 tmpfs_getattr(struct vop_getattr_args *v)
  373 {
  374         struct vnode *vp = v->a_vp;
  375         struct vattr *vap = v->a_vap;
  376 
  377         struct tmpfs_node *node;
  378 
  379         node = VP_TO_TMPFS_NODE(vp);
  380 
  381         tmpfs_update(vp);
  382 
  383         vap->va_type = vp->v_type;
  384         vap->va_mode = node->tn_mode;
  385         vap->va_nlink = node->tn_links;
  386         vap->va_uid = node->tn_uid;
  387         vap->va_gid = node->tn_gid;
  388         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
  389         vap->va_fileid = node->tn_id;
  390         vap->va_size = node->tn_size;
  391         vap->va_blocksize = PAGE_SIZE;
  392         vap->va_atime = node->tn_atime;
  393         vap->va_mtime = node->tn_mtime;
  394         vap->va_ctime = node->tn_ctime;
  395         vap->va_birthtime = node->tn_birthtime;
  396         vap->va_gen = node->tn_gen;
  397         vap->va_flags = node->tn_flags;
  398         vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
  399                 node->tn_rdev : NODEV;
  400         vap->va_bytes = round_page(node->tn_size);
  401         vap->va_filerev = 0;
  402 
  403         return 0;
  404 }
  405 
  406 int
  407 tmpfs_setattr(struct vop_setattr_args *v)
  408 {
  409         struct vnode *vp = v->a_vp;
  410         struct vattr *vap = v->a_vap;
  411         struct ucred *cred = v->a_cred;
  412         struct thread *td = curthread;
  413 
  414         int error;
  415 
  416         MPASS(VOP_ISLOCKED(vp));
  417 
  418         error = 0;
  419 
  420         /* Abort if any unsettable attribute is given. */
  421         if (vap->va_type != VNON ||
  422             vap->va_nlink != VNOVAL ||
  423             vap->va_fsid != VNOVAL ||
  424             vap->va_fileid != VNOVAL ||
  425             vap->va_blocksize != VNOVAL ||
  426             vap->va_gen != VNOVAL ||
  427             vap->va_rdev != VNOVAL ||
  428             vap->va_bytes != VNOVAL)
  429                 error = EINVAL;
  430 
  431         if (error == 0 && (vap->va_flags != VNOVAL))
  432                 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
  433 
  434         if (error == 0 && (vap->va_size != VNOVAL))
  435                 error = tmpfs_chsize(vp, vap->va_size, cred, td);
  436 
  437         if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
  438                 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
  439 
  440         if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
  441                 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
  442 
  443         if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
  444             vap->va_atime.tv_nsec != VNOVAL) ||
  445             (vap->va_mtime.tv_sec != VNOVAL &&
  446             vap->va_mtime.tv_nsec != VNOVAL) ||
  447             (vap->va_birthtime.tv_sec != VNOVAL &&
  448             vap->va_birthtime.tv_nsec != VNOVAL)))
  449                 error = tmpfs_chtimes(vp, vap, cred, td);
  450 
  451         /* Update the node times.  We give preference to the error codes
  452          * generated by this function rather than the ones that may arise
  453          * from tmpfs_update. */
  454         tmpfs_update(vp);
  455 
  456         MPASS(VOP_ISLOCKED(vp));
  457 
  458         return error;
  459 }
  460 
  461 static int
  462 tmpfs_read(struct vop_read_args *v)
  463 {
  464         struct vnode *vp;
  465         struct uio *uio;
  466         struct tmpfs_node *node;
  467 
  468         vp = v->a_vp;
  469         if (vp->v_type != VREG)
  470                 return (EISDIR);
  471         uio = v->a_uio;
  472         if (uio->uio_offset < 0)
  473                 return (EINVAL);
  474         node = VP_TO_TMPFS_NODE(vp);
  475         tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
  476         return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
  477 }
  478 
  479 static int
  480 tmpfs_write(struct vop_write_args *v)
  481 {
  482         struct vnode *vp;
  483         struct uio *uio;
  484         struct tmpfs_node *node;
  485         off_t oldsize;
  486         int error, ioflag;
  487 
  488         vp = v->a_vp;
  489         uio = v->a_uio;
  490         ioflag = v->a_ioflag;
  491         error = 0;
  492         node = VP_TO_TMPFS_NODE(vp);
  493         oldsize = node->tn_size;
  494 
  495         if (uio->uio_offset < 0 || vp->v_type != VREG)
  496                 return (EINVAL);
  497         if (uio->uio_resid == 0)
  498                 return (0);
  499         if (ioflag & IO_APPEND)
  500                 uio->uio_offset = node->tn_size;
  501         if (uio->uio_offset + uio->uio_resid >
  502           VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
  503                 return (EFBIG);
  504         if (vn_rlimit_fsize(vp, uio, uio->uio_td))
  505                 return (EFBIG);
  506         if (uio->uio_offset + uio->uio_resid > node->tn_size) {
  507                 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
  508                     FALSE);
  509                 if (error != 0)
  510                         goto out;
  511         }
  512 
  513         error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
  514         node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
  515             TMPFS_NODE_CHANGED;
  516         if (node->tn_mode & (S_ISUID | S_ISGID)) {
  517                 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
  518                         node->tn_mode &= ~(S_ISUID | S_ISGID);
  519         }
  520         if (error != 0)
  521                 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
  522 
  523 out:
  524         MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
  525         MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
  526 
  527         return (error);
  528 }
  529 
  530 static int
  531 tmpfs_fsync(struct vop_fsync_args *v)
  532 {
  533         struct vnode *vp = v->a_vp;
  534 
  535         MPASS(VOP_ISLOCKED(vp));
  536 
  537         tmpfs_check_mtime(vp);
  538         tmpfs_update(vp);
  539 
  540         return 0;
  541 }
  542 
  543 static int
  544 tmpfs_remove(struct vop_remove_args *v)
  545 {
  546         struct vnode *dvp = v->a_dvp;
  547         struct vnode *vp = v->a_vp;
  548 
  549         int error;
  550         struct tmpfs_dirent *de;
  551         struct tmpfs_mount *tmp;
  552         struct tmpfs_node *dnode;
  553         struct tmpfs_node *node;
  554 
  555         MPASS(VOP_ISLOCKED(dvp));
  556         MPASS(VOP_ISLOCKED(vp));
  557 
  558         if (vp->v_type == VDIR) {
  559                 error = EISDIR;
  560                 goto out;
  561         }
  562 
  563         dnode = VP_TO_TMPFS_DIR(dvp);
  564         node = VP_TO_TMPFS_NODE(vp);
  565         tmp = VFS_TO_TMPFS(vp->v_mount);
  566         de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
  567         MPASS(de != NULL);
  568 
  569         /* Files marked as immutable or append-only cannot be deleted. */
  570         if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
  571             (dnode->tn_flags & APPEND)) {
  572                 error = EPERM;
  573                 goto out;
  574         }
  575 
  576         /* Remove the entry from the directory; as it is a file, we do not
  577          * have to change the number of hard links of the directory. */
  578         tmpfs_dir_detach(dvp, de);
  579         if (v->a_cnp->cn_flags & DOWHITEOUT)
  580                 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
  581 
  582         /* Free the directory entry we just deleted.  Note that the node
  583          * referred by it will not be removed until the vnode is really
  584          * reclaimed. */
  585         tmpfs_free_dirent(tmp, de);
  586 
  587         node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
  588         error = 0;
  589 
  590 out:
  591 
  592         return error;
  593 }
  594 
  595 static int
  596 tmpfs_link(struct vop_link_args *v)
  597 {
  598         struct vnode *dvp = v->a_tdvp;
  599         struct vnode *vp = v->a_vp;
  600         struct componentname *cnp = v->a_cnp;
  601 
  602         int error;
  603         struct tmpfs_dirent *de;
  604         struct tmpfs_node *node;
  605 
  606         MPASS(VOP_ISLOCKED(dvp));
  607         MPASS(cnp->cn_flags & HASBUF);
  608         MPASS(dvp != vp); /* XXX When can this be false? */
  609         node = VP_TO_TMPFS_NODE(vp);
  610 
  611         /* Ensure that we do not overflow the maximum number of links imposed
  612          * by the system. */
  613         MPASS(node->tn_links <= LINK_MAX);
  614         if (node->tn_links == LINK_MAX) {
  615                 error = EMLINK;
  616                 goto out;
  617         }
  618 
  619         /* We cannot create links of files marked immutable or append-only. */
  620         if (node->tn_flags & (IMMUTABLE | APPEND)) {
  621                 error = EPERM;
  622                 goto out;
  623         }
  624 
  625         /* Allocate a new directory entry to represent the node. */
  626         error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
  627             cnp->cn_nameptr, cnp->cn_namelen, &de);
  628         if (error != 0)
  629                 goto out;
  630 
  631         /* Insert the new directory entry into the appropriate directory. */
  632         if (cnp->cn_flags & ISWHITEOUT)
  633                 tmpfs_dir_whiteout_remove(dvp, cnp);
  634         tmpfs_dir_attach(dvp, de);
  635 
  636         /* vp link count has changed, so update node times. */
  637         node->tn_status |= TMPFS_NODE_CHANGED;
  638         tmpfs_update(vp);
  639 
  640         error = 0;
  641 
  642 out:
  643         return error;
  644 }
  645 
  646 /*
  647  * We acquire all but fdvp locks using non-blocking acquisitions.  If we
  648  * fail to acquire any lock in the path we will drop all held locks,
  649  * acquire the new lock in a blocking fashion, and then release it and
  650  * restart the rename.  This acquire/release step ensures that we do not
  651  * spin on a lock waiting for release.  On error release all vnode locks
  652  * and decrement references the way tmpfs_rename() would do.
  653  */
  654 static int
  655 tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
  656     struct vnode *tdvp, struct vnode **tvpp,
  657     struct componentname *fcnp, struct componentname *tcnp)
  658 {
  659         struct vnode *nvp;
  660         struct mount *mp;
  661         struct tmpfs_dirent *de;
  662         int error, restarts = 0;
  663 
  664         VOP_UNLOCK(tdvp, 0);
  665         if (*tvpp != NULL && *tvpp != tdvp)
  666                 VOP_UNLOCK(*tvpp, 0);
  667         mp = fdvp->v_mount;
  668 
  669 relock:
  670         restarts += 1;
  671         error = vn_lock(fdvp, LK_EXCLUSIVE);
  672         if (error)
  673                 goto releout;
  674         if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
  675                 VOP_UNLOCK(fdvp, 0);
  676                 error = vn_lock(tdvp, LK_EXCLUSIVE);
  677                 if (error)
  678                         goto releout;
  679                 VOP_UNLOCK(tdvp, 0);
  680                 goto relock;
  681         }
  682         /*
  683          * Re-resolve fvp to be certain it still exists and fetch the
  684          * correct vnode.
  685          */
  686         de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
  687         if (de == NULL) {
  688                 VOP_UNLOCK(fdvp, 0);
  689                 VOP_UNLOCK(tdvp, 0);
  690                 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
  691                     (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
  692                         error = EINVAL;
  693                 else
  694                         error = ENOENT;
  695                 goto releout;
  696         }
  697         error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
  698         if (error != 0) {
  699                 VOP_UNLOCK(fdvp, 0);
  700                 VOP_UNLOCK(tdvp, 0);
  701                 if (error != EBUSY)
  702                         goto releout;
  703                 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
  704                 if (error != 0)
  705                         goto releout;
  706                 VOP_UNLOCK(nvp, 0);
  707                 /*
  708                  * Concurrent rename race.
  709                  */
  710                 if (nvp == tdvp) {
  711                         vrele(nvp);
  712                         error = EINVAL;
  713                         goto releout;
  714                 }
  715                 vrele(*fvpp);
  716                 *fvpp = nvp;
  717                 goto relock;
  718         }
  719         vrele(*fvpp);
  720         *fvpp = nvp;
  721         VOP_UNLOCK(*fvpp, 0);
  722         /*
  723          * Re-resolve tvp and acquire the vnode lock if present.
  724          */
  725         de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
  726         /*
  727          * If tvp disappeared we just carry on.
  728          */
  729         if (de == NULL && *tvpp != NULL) {
  730                 vrele(*tvpp);
  731                 *tvpp = NULL;
  732         }
  733         /*
  734          * Get the tvp ino if the lookup succeeded.  We may have to restart
  735          * if the non-blocking acquire fails.
  736          */
  737         if (de != NULL) {
  738                 nvp = NULL;
  739                 error = tmpfs_alloc_vp(mp, de->td_node,
  740                     LK_EXCLUSIVE | LK_NOWAIT, &nvp);
  741                 if (*tvpp != NULL)
  742                         vrele(*tvpp);
  743                 *tvpp = nvp;
  744                 if (error != 0) {
  745                         VOP_UNLOCK(fdvp, 0);
  746                         VOP_UNLOCK(tdvp, 0);
  747                         if (error != EBUSY)
  748                                 goto releout;
  749                         error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
  750                             &nvp);
  751                         if (error != 0)
  752                                 goto releout;
  753                         VOP_UNLOCK(nvp, 0);
  754                         /*
  755                          * fdvp contains fvp, thus tvp (=fdvp) is not empty.
  756                          */
  757                         if (nvp == fdvp) {
  758                                 error = ENOTEMPTY;
  759                                 goto releout;
  760                         }
  761                         goto relock;
  762                 }
  763         }
  764         tmpfs_rename_restarts += restarts;
  765 
  766         return (0);
  767 
  768 releout:
  769         vrele(fdvp);
  770         vrele(*fvpp);
  771         vrele(tdvp);
  772         if (*tvpp != NULL)
  773                 vrele(*tvpp);
  774         tmpfs_rename_restarts += restarts;
  775 
  776         return (error);
  777 }
  778 
  779 static int
  780 tmpfs_rename(struct vop_rename_args *v)
  781 {
  782         struct vnode *fdvp = v->a_fdvp;
  783         struct vnode *fvp = v->a_fvp;
  784         struct componentname *fcnp = v->a_fcnp;
  785         struct vnode *tdvp = v->a_tdvp;
  786         struct vnode *tvp = v->a_tvp;
  787         struct componentname *tcnp = v->a_tcnp;
  788         struct mount *mp = NULL;
  789 
  790         char *newname;
  791         int error;
  792         struct tmpfs_dirent *de;
  793         struct tmpfs_mount *tmp;
  794         struct tmpfs_node *fdnode;
  795         struct tmpfs_node *fnode;
  796         struct tmpfs_node *tnode;
  797         struct tmpfs_node *tdnode;
  798 
  799         MPASS(VOP_ISLOCKED(tdvp));
  800         MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
  801         MPASS(fcnp->cn_flags & HASBUF);
  802         MPASS(tcnp->cn_flags & HASBUF);
  803 
  804         /* Disallow cross-device renames.
  805          * XXX Why isn't this done by the caller? */
  806         if (fvp->v_mount != tdvp->v_mount ||
  807             (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
  808                 error = EXDEV;
  809                 goto out;
  810         }
  811 
  812         /* If source and target are the same file, there is nothing to do. */
  813         if (fvp == tvp) {
  814                 error = 0;
  815                 goto out;
  816         }
  817 
  818         /* If we need to move the directory between entries, lock the
  819          * source so that we can safely operate on it. */
  820         if (fdvp != tdvp && fdvp != tvp) {
  821                 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
  822                         mp = tdvp->v_mount;
  823                         error = vfs_busy(mp, 0);
  824                         if (error != 0) {
  825                                 mp = NULL;
  826                                 goto out;
  827                         }
  828                         error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
  829                             fcnp, tcnp);
  830                         if (error != 0) {
  831                                 vfs_unbusy(mp);
  832                                 return (error);
  833                         }
  834                         ASSERT_VOP_ELOCKED(fdvp,
  835                             "tmpfs_rename: fdvp not locked");
  836                         ASSERT_VOP_ELOCKED(tdvp,
  837                             "tmpfs_rename: tdvp not locked");
  838                         if (tvp != NULL)
  839                                 ASSERT_VOP_ELOCKED(tvp,
  840                                     "tmpfs_rename: tvp not locked");
  841                         if (fvp == tvp) {
  842                                 error = 0;
  843                                 goto out_locked;
  844                         }
  845                 }
  846         }
  847 
  848         tmp = VFS_TO_TMPFS(tdvp->v_mount);
  849         tdnode = VP_TO_TMPFS_DIR(tdvp);
  850         tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
  851         fdnode = VP_TO_TMPFS_DIR(fdvp);
  852         fnode = VP_TO_TMPFS_NODE(fvp);
  853         de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
  854 
  855         /* Entry can disappear before we lock fdvp,
  856          * also avoid manipulating '.' and '..' entries. */
  857         if (de == NULL) {
  858                 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
  859                     (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
  860                         error = EINVAL;
  861                 else
  862                         error = ENOENT;
  863                 goto out_locked;
  864         }
  865         MPASS(de->td_node == fnode);
  866 
  867         /* If re-naming a directory to another preexisting directory
  868          * ensure that the target directory is empty so that its
  869          * removal causes no side effects.
  870          * Kern_rename gurantees the destination to be a directory
  871          * if the source is one. */
  872         if (tvp != NULL) {
  873                 MPASS(tnode != NULL);
  874 
  875                 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
  876                     (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
  877                         error = EPERM;
  878                         goto out_locked;
  879                 }
  880 
  881                 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
  882                         if (tnode->tn_size > 0) {
  883                                 error = ENOTEMPTY;
  884                                 goto out_locked;
  885                         }
  886                 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
  887                         error = ENOTDIR;
  888                         goto out_locked;
  889                 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
  890                         error = EISDIR;
  891                         goto out_locked;
  892                 } else {
  893                         MPASS(fnode->tn_type != VDIR &&
  894                                 tnode->tn_type != VDIR);
  895                 }
  896         }
  897 
  898         if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
  899             || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
  900                 error = EPERM;
  901                 goto out_locked;
  902         }
  903 
  904         /* Ensure that we have enough memory to hold the new name, if it
  905          * has to be changed. */
  906         if (fcnp->cn_namelen != tcnp->cn_namelen ||
  907             bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
  908                 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
  909         } else
  910                 newname = NULL;
  911 
  912         /* If the node is being moved to another directory, we have to do
  913          * the move. */
  914         if (fdnode != tdnode) {
  915                 /* In case we are moving a directory, we have to adjust its
  916                  * parent to point to the new parent. */
  917                 if (de->td_node->tn_type == VDIR) {
  918                         struct tmpfs_node *n;
  919 
  920                         /* Ensure the target directory is not a child of the
  921                          * directory being moved.  Otherwise, we'd end up
  922                          * with stale nodes. */
  923                         n = tdnode;
  924                         /* TMPFS_LOCK garanties that no nodes are freed while
  925                          * traversing the list. Nodes can only be marked as
  926                          * removed: tn_parent == NULL. */
  927                         TMPFS_LOCK(tmp);
  928                         TMPFS_NODE_LOCK(n);
  929                         while (n != n->tn_dir.tn_parent) {
  930                                 struct tmpfs_node *parent;
  931 
  932                                 if (n == fnode) {
  933                                         TMPFS_NODE_UNLOCK(n);
  934                                         TMPFS_UNLOCK(tmp);
  935                                         error = EINVAL;
  936                                         if (newname != NULL)
  937                                                     free(newname, M_TMPFSNAME);
  938                                         goto out_locked;
  939                                 }
  940                                 parent = n->tn_dir.tn_parent;
  941                                 TMPFS_NODE_UNLOCK(n);
  942                                 if (parent == NULL) {
  943                                         n = NULL;
  944                                         break;
  945                                 }
  946                                 TMPFS_NODE_LOCK(parent);
  947                                 if (parent->tn_dir.tn_parent == NULL) {
  948                                         TMPFS_NODE_UNLOCK(parent);
  949                                         n = NULL;
  950                                         break;
  951                                 }
  952                                 n = parent;
  953                         }
  954                         TMPFS_UNLOCK(tmp);
  955                         if (n == NULL) {
  956                                 error = EINVAL;
  957                                 if (newname != NULL)
  958                                             free(newname, M_TMPFSNAME);
  959                                 goto out_locked;
  960                         }
  961                         TMPFS_NODE_UNLOCK(n);
  962 
  963                         /* Adjust the parent pointer. */
  964                         TMPFS_VALIDATE_DIR(fnode);
  965                         TMPFS_NODE_LOCK(de->td_node);
  966                         de->td_node->tn_dir.tn_parent = tdnode;
  967                         TMPFS_NODE_UNLOCK(de->td_node);
  968 
  969                         /* As a result of changing the target of the '..'
  970                          * entry, the link count of the source and target
  971                          * directories has to be adjusted. */
  972                         TMPFS_NODE_LOCK(tdnode);
  973                         TMPFS_ASSERT_LOCKED(tdnode);
  974                         tdnode->tn_links++;
  975                         TMPFS_NODE_UNLOCK(tdnode);
  976 
  977                         TMPFS_NODE_LOCK(fdnode);
  978                         TMPFS_ASSERT_LOCKED(fdnode);
  979                         fdnode->tn_links--;
  980                         TMPFS_NODE_UNLOCK(fdnode);
  981                 }
  982         }
  983 
  984         /* Do the move: just remove the entry from the source directory
  985          * and insert it into the target one. */
  986         tmpfs_dir_detach(fdvp, de);
  987 
  988         if (fcnp->cn_flags & DOWHITEOUT)
  989                 tmpfs_dir_whiteout_add(fdvp, fcnp);
  990         if (tcnp->cn_flags & ISWHITEOUT)
  991                 tmpfs_dir_whiteout_remove(tdvp, tcnp);
  992 
  993         /* If the name has changed, we need to make it effective by changing
  994          * it in the directory entry. */
  995         if (newname != NULL) {
  996                 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
  997 
  998                 free(de->ud.td_name, M_TMPFSNAME);
  999                 de->ud.td_name = newname;
 1000                 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
 1001 
 1002                 fnode->tn_status |= TMPFS_NODE_CHANGED;
 1003                 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
 1004         }
 1005 
 1006         /* If we are overwriting an entry, we have to remove the old one
 1007          * from the target directory. */
 1008         if (tvp != NULL) {
 1009                 struct tmpfs_dirent *tde;
 1010 
 1011                 /* Remove the old entry from the target directory. */
 1012                 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
 1013                 tmpfs_dir_detach(tdvp, tde);
 1014 
 1015                 /* Free the directory entry we just deleted.  Note that the
 1016                  * node referred by it will not be removed until the vnode is
 1017                  * really reclaimed. */
 1018                 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
 1019         }
 1020 
 1021         tmpfs_dir_attach(tdvp, de);
 1022 
 1023         if (tmpfs_use_nc(fvp)) {
 1024                 cache_purge(fvp);
 1025                 if (tvp != NULL)
 1026                         cache_purge(tvp);
 1027                 cache_purge_negative(tdvp);
 1028         }
 1029 
 1030         error = 0;
 1031 
 1032 out_locked:
 1033         if (fdvp != tdvp && fdvp != tvp)
 1034                 VOP_UNLOCK(fdvp, 0);
 1035 
 1036 out:
 1037         /* Release target nodes. */
 1038         /* XXX: I don't understand when tdvp can be the same as tvp, but
 1039          * other code takes care of this... */
 1040         if (tdvp == tvp)
 1041                 vrele(tdvp);
 1042         else
 1043                 vput(tdvp);
 1044         if (tvp != NULL)
 1045                 vput(tvp);
 1046 
 1047         /* Release source nodes. */
 1048         vrele(fdvp);
 1049         vrele(fvp);
 1050 
 1051         if (mp != NULL)
 1052                 vfs_unbusy(mp);
 1053 
 1054         return error;
 1055 }
 1056 
 1057 static int
 1058 tmpfs_mkdir(struct vop_mkdir_args *v)
 1059 {
 1060         struct vnode *dvp = v->a_dvp;
 1061         struct vnode **vpp = v->a_vpp;
 1062         struct componentname *cnp = v->a_cnp;
 1063         struct vattr *vap = v->a_vap;
 1064 
 1065         MPASS(vap->va_type == VDIR);
 1066 
 1067         return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
 1068 }
 1069 
 1070 static int
 1071 tmpfs_rmdir(struct vop_rmdir_args *v)
 1072 {
 1073         struct vnode *dvp = v->a_dvp;
 1074         struct vnode *vp = v->a_vp;
 1075 
 1076         int error;
 1077         struct tmpfs_dirent *de;
 1078         struct tmpfs_mount *tmp;
 1079         struct tmpfs_node *dnode;
 1080         struct tmpfs_node *node;
 1081 
 1082         MPASS(VOP_ISLOCKED(dvp));
 1083         MPASS(VOP_ISLOCKED(vp));
 1084 
 1085         tmp = VFS_TO_TMPFS(dvp->v_mount);
 1086         dnode = VP_TO_TMPFS_DIR(dvp);
 1087         node = VP_TO_TMPFS_DIR(vp);
 1088 
 1089         /* Directories with more than two entries ('.' and '..') cannot be
 1090          * removed. */
 1091          if (node->tn_size > 0) {
 1092                  error = ENOTEMPTY;
 1093                  goto out;
 1094          }
 1095 
 1096         if ((dnode->tn_flags & APPEND)
 1097             || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
 1098                 error = EPERM;
 1099                 goto out;
 1100         }
 1101 
 1102         /* This invariant holds only if we are not trying to remove "..".
 1103           * We checked for that above so this is safe now. */
 1104         MPASS(node->tn_dir.tn_parent == dnode);
 1105 
 1106         /* Get the directory entry associated with node (vp).  This was
 1107          * filled by tmpfs_lookup while looking up the entry. */
 1108         de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
 1109         MPASS(TMPFS_DIRENT_MATCHES(de,
 1110             v->a_cnp->cn_nameptr,
 1111             v->a_cnp->cn_namelen));
 1112 
 1113         /* Check flags to see if we are allowed to remove the directory. */
 1114         if ((dnode->tn_flags & APPEND) != 0 ||
 1115             (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
 1116                 error = EPERM;
 1117                 goto out;
 1118         }
 1119 
 1120 
 1121         /* Detach the directory entry from the directory (dnode). */
 1122         tmpfs_dir_detach(dvp, de);
 1123         if (v->a_cnp->cn_flags & DOWHITEOUT)
 1124                 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
 1125 
 1126         /* No vnode should be allocated for this entry from this point */
 1127         TMPFS_NODE_LOCK(node);
 1128         node->tn_links--;
 1129         node->tn_dir.tn_parent = NULL;
 1130         node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
 1131             TMPFS_NODE_MODIFIED;
 1132 
 1133         TMPFS_NODE_UNLOCK(node);
 1134 
 1135         TMPFS_NODE_LOCK(dnode);
 1136         dnode->tn_links--;
 1137         dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
 1138             TMPFS_NODE_MODIFIED;
 1139         TMPFS_NODE_UNLOCK(dnode);
 1140 
 1141         if (tmpfs_use_nc(dvp)) {
 1142                 cache_purge(dvp);
 1143                 cache_purge(vp);
 1144         }
 1145 
 1146         /* Free the directory entry we just deleted.  Note that the node
 1147          * referred by it will not be removed until the vnode is really
 1148          * reclaimed. */
 1149         tmpfs_free_dirent(tmp, de);
 1150 
 1151         /* Release the deleted vnode (will destroy the node, notify
 1152          * interested parties and clean it from the cache). */
 1153 
 1154         dnode->tn_status |= TMPFS_NODE_CHANGED;
 1155         tmpfs_update(dvp);
 1156 
 1157         error = 0;
 1158 
 1159 out:
 1160         return error;
 1161 }
 1162 
 1163 static int
 1164 tmpfs_symlink(struct vop_symlink_args *v)
 1165 {
 1166         struct vnode *dvp = v->a_dvp;
 1167         struct vnode **vpp = v->a_vpp;
 1168         struct componentname *cnp = v->a_cnp;
 1169         struct vattr *vap = v->a_vap;
 1170         char *target = v->a_target;
 1171 
 1172 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
 1173         MPASS(vap->va_type == VLNK);
 1174 #else
 1175         vap->va_type = VLNK;
 1176 #endif
 1177 
 1178         return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
 1179 }
 1180 
 1181 static int
 1182 tmpfs_readdir(struct vop_readdir_args *v)
 1183 {
 1184         struct vnode *vp = v->a_vp;
 1185         struct uio *uio = v->a_uio;
 1186         int *eofflag = v->a_eofflag;
 1187         u_long **cookies = v->a_cookies;
 1188         int *ncookies = v->a_ncookies;
 1189 
 1190         int error;
 1191         ssize_t startresid;
 1192         int maxcookies;
 1193         struct tmpfs_node *node;
 1194 
 1195         /* This operation only makes sense on directory nodes. */
 1196         if (vp->v_type != VDIR)
 1197                 return ENOTDIR;
 1198 
 1199         maxcookies = 0;
 1200         node = VP_TO_TMPFS_DIR(vp);
 1201 
 1202         startresid = uio->uio_resid;
 1203 
 1204         /* Allocate cookies for NFS and compat modules. */
 1205         if (cookies != NULL && ncookies != NULL) {
 1206                 maxcookies = howmany(node->tn_size,
 1207                     sizeof(struct tmpfs_dirent)) + 2;
 1208                 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
 1209                     M_WAITOK);
 1210                 *ncookies = 0;
 1211         }
 1212 
 1213         if (cookies == NULL)
 1214                 error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
 1215         else
 1216                 error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
 1217                     ncookies);
 1218 
 1219         /* Buffer was filled without hitting EOF. */
 1220         if (error == EJUSTRETURN)
 1221                 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
 1222 
 1223         if (error != 0 && cookies != NULL && ncookies != NULL) {
 1224                 free(*cookies, M_TEMP);
 1225                 *cookies = NULL;
 1226                 *ncookies = 0;
 1227         }
 1228 
 1229         if (eofflag != NULL)
 1230                 *eofflag =
 1231                     (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
 1232 
 1233         return error;
 1234 }
 1235 
 1236 static int
 1237 tmpfs_readlink(struct vop_readlink_args *v)
 1238 {
 1239         struct vnode *vp = v->a_vp;
 1240         struct uio *uio = v->a_uio;
 1241 
 1242         int error;
 1243         struct tmpfs_node *node;
 1244 
 1245         MPASS(uio->uio_offset == 0);
 1246         MPASS(vp->v_type == VLNK);
 1247 
 1248         node = VP_TO_TMPFS_NODE(vp);
 1249 
 1250         error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
 1251             uio);
 1252         tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
 1253 
 1254         return (error);
 1255 }
 1256 
 1257 static int
 1258 tmpfs_inactive(struct vop_inactive_args *v)
 1259 {
 1260         struct vnode *vp;
 1261         struct tmpfs_node *node;
 1262 
 1263         vp = v->a_vp;
 1264         node = VP_TO_TMPFS_NODE(vp);
 1265         if (node->tn_links == 0)
 1266                 vrecycle(vp);
 1267         else
 1268                 tmpfs_check_mtime(vp);
 1269         return (0);
 1270 }
 1271 
 1272 int
 1273 tmpfs_reclaim(struct vop_reclaim_args *v)
 1274 {
 1275         struct vnode *vp = v->a_vp;
 1276 
 1277         struct tmpfs_mount *tmp;
 1278         struct tmpfs_node *node;
 1279 
 1280         node = VP_TO_TMPFS_NODE(vp);
 1281         tmp = VFS_TO_TMPFS(vp->v_mount);
 1282 
 1283         if (vp->v_type == VREG)
 1284                 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
 1285         else
 1286                 vnode_destroy_vobject(vp);
 1287         vp->v_object = NULL;
 1288         if (tmpfs_use_nc(vp))
 1289                 cache_purge(vp);
 1290 
 1291         TMPFS_NODE_LOCK(node);
 1292         tmpfs_free_vp(vp);
 1293 
 1294         /* If the node referenced by this vnode was deleted by the user,
 1295          * we must free its associated data structures (now that the vnode
 1296          * is being reclaimed). */
 1297         if (node->tn_links == 0 &&
 1298             (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
 1299                 node->tn_vpstate = TMPFS_VNODE_DOOMED;
 1300                 TMPFS_NODE_UNLOCK(node);
 1301                 tmpfs_free_node(tmp, node);
 1302         } else
 1303                 TMPFS_NODE_UNLOCK(node);
 1304 
 1305         MPASS(vp->v_data == NULL);
 1306         return 0;
 1307 }
 1308 
 1309 static int
 1310 tmpfs_print(struct vop_print_args *v)
 1311 {
 1312         struct vnode *vp = v->a_vp;
 1313 
 1314         struct tmpfs_node *node;
 1315 
 1316         node = VP_TO_TMPFS_NODE(vp);
 1317 
 1318         printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
 1319             node, node->tn_flags, node->tn_links);
 1320         printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
 1321             node->tn_mode, node->tn_uid, node->tn_gid,
 1322             (intmax_t)node->tn_size, node->tn_status);
 1323 
 1324         if (vp->v_type == VFIFO)
 1325                 fifo_printinfo(vp);
 1326 
 1327         printf("\n");
 1328 
 1329         return 0;
 1330 }
 1331 
 1332 static int
 1333 tmpfs_pathconf(struct vop_pathconf_args *v)
 1334 {
 1335         int name = v->a_name;
 1336         register_t *retval = v->a_retval;
 1337 
 1338         int error;
 1339 
 1340         error = 0;
 1341 
 1342         switch (name) {
 1343         case _PC_LINK_MAX:
 1344                 *retval = LINK_MAX;
 1345                 break;
 1346 
 1347         case _PC_NAME_MAX:
 1348                 *retval = NAME_MAX;
 1349                 break;
 1350 
 1351         case _PC_PATH_MAX:
 1352                 *retval = PATH_MAX;
 1353                 break;
 1354 
 1355         case _PC_PIPE_BUF:
 1356                 *retval = PIPE_BUF;
 1357                 break;
 1358 
 1359         case _PC_CHOWN_RESTRICTED:
 1360                 *retval = 1;
 1361                 break;
 1362 
 1363         case _PC_NO_TRUNC:
 1364                 *retval = 1;
 1365                 break;
 1366 
 1367         case _PC_SYNC_IO:
 1368                 *retval = 1;
 1369                 break;
 1370 
 1371         case _PC_FILESIZEBITS:
 1372                 *retval = 0; /* XXX Don't know which value should I return. */
 1373                 break;
 1374 
 1375         default:
 1376                 error = EINVAL;
 1377         }
 1378 
 1379         return error;
 1380 }
 1381 
 1382 static int
 1383 tmpfs_vptofh(struct vop_vptofh_args *ap)
 1384 {
 1385         struct tmpfs_fid *tfhp;
 1386         struct tmpfs_node *node;
 1387 
 1388         tfhp = (struct tmpfs_fid *)ap->a_fhp;
 1389         node = VP_TO_TMPFS_NODE(ap->a_vp);
 1390 
 1391         tfhp->tf_len = sizeof(struct tmpfs_fid);
 1392         tfhp->tf_id = node->tn_id;
 1393         tfhp->tf_gen = node->tn_gen;
 1394 
 1395         return (0);
 1396 }
 1397 
 1398 static int
 1399 tmpfs_whiteout(struct vop_whiteout_args *ap)
 1400 {
 1401         struct vnode *dvp = ap->a_dvp;
 1402         struct componentname *cnp = ap->a_cnp;
 1403         struct tmpfs_dirent *de;
 1404 
 1405         switch (ap->a_flags) {
 1406         case LOOKUP:
 1407                 return (0);
 1408         case CREATE:
 1409                 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
 1410                 if (de != NULL)
 1411                         return (de->td_node == NULL ? 0 : EEXIST);
 1412                 return (tmpfs_dir_whiteout_add(dvp, cnp));
 1413         case DELETE:
 1414                 tmpfs_dir_whiteout_remove(dvp, cnp);
 1415                 return (0);
 1416         default:
 1417                 panic("tmpfs_whiteout: unknown op");
 1418         }
 1419 }
 1420 
 1421 static int
 1422 tmpfs_vptocnp_dir(struct tmpfs_node *tn, struct tmpfs_node *tnp,
 1423     struct tmpfs_dirent **pde)
 1424 {
 1425         struct tmpfs_dir_cursor dc;
 1426         struct tmpfs_dirent *de;
 1427 
 1428         for (de = tmpfs_dir_first(tnp, &dc); de != NULL;
 1429              de = tmpfs_dir_next(tnp, &dc)) {
 1430                 if (de->td_node == tn) {
 1431                         *pde = de;
 1432                         return (0);
 1433                 }
 1434         }
 1435         return (ENOENT);
 1436 }
 1437 
 1438 static int
 1439 tmpfs_vptocnp_fill(struct vnode *vp, struct tmpfs_node *tn,
 1440     struct tmpfs_node *tnp, char *buf, int *buflen, struct vnode **dvp)
 1441 {
 1442         struct tmpfs_dirent *de;
 1443         int error, i;
 1444 
 1445         error = vn_vget_ino_gen(vp, tmpfs_vn_get_ino_alloc, tnp, LK_SHARED,
 1446             dvp);
 1447         if (error != 0)
 1448                 return (error);
 1449         error = tmpfs_vptocnp_dir(tn, tnp, &de);
 1450         if (error == 0) {
 1451                 i = *buflen;
 1452                 i -= de->td_namelen;
 1453                 if (i < 0) {
 1454                         error = ENOMEM;
 1455                 } else {
 1456                         bcopy(de->ud.td_name, buf + i, de->td_namelen);
 1457                         *buflen = i;
 1458                 }
 1459         }
 1460         if (error == 0) {
 1461                 if (vp != *dvp)
 1462                         VOP_UNLOCK(*dvp, 0);
 1463         } else {
 1464                 if (vp != *dvp)
 1465                         vput(*dvp);
 1466                 else
 1467                         vrele(vp);
 1468         }
 1469         return (error);
 1470 }
 1471 
 1472 static int
 1473 tmpfs_vptocnp(struct vop_vptocnp_args *ap)
 1474 {
 1475         struct vnode *vp, **dvp;
 1476         struct tmpfs_node *tn, *tnp, *tnp1;
 1477         struct tmpfs_dirent *de;
 1478         struct tmpfs_mount *tm;
 1479         char *buf;
 1480         int *buflen;
 1481         int error;
 1482 
 1483         vp = ap->a_vp;
 1484         dvp = ap->a_vpp;
 1485         buf = ap->a_buf;
 1486         buflen = ap->a_buflen;
 1487 
 1488         tm = VFS_TO_TMPFS(vp->v_mount);
 1489         tn = VP_TO_TMPFS_NODE(vp);
 1490         if (tn->tn_type == VDIR) {
 1491                 tnp = tn->tn_dir.tn_parent;
 1492                 if (tnp == NULL)
 1493                         return (ENOENT);
 1494                 tmpfs_ref_node(tnp);
 1495                 error = tmpfs_vptocnp_fill(vp, tn, tn->tn_dir.tn_parent, buf,
 1496                     buflen, dvp);
 1497                 tmpfs_free_node(tm, tnp);
 1498                 return (error);
 1499         }
 1500 restart:
 1501         TMPFS_LOCK(tm);
 1502         LIST_FOREACH_SAFE(tnp, &tm->tm_nodes_used, tn_entries, tnp1) {
 1503                 if (tnp->tn_type != VDIR)
 1504                         continue;
 1505                 TMPFS_NODE_LOCK(tnp);
 1506                 tmpfs_ref_node_locked(tnp);
 1507 
 1508                 /*
 1509                  * tn_vnode cannot be instantiated while we hold the
 1510                  * node lock, so the directory cannot be changed while
 1511                  * we iterate over it.  Do this to avoid instantiating
 1512                  * vnode for directories which cannot point to our
 1513                  * node.
 1514                  */
 1515                 error = tnp->tn_vnode == NULL ? tmpfs_vptocnp_dir(tn, tnp,
 1516                     &de) : 0;
 1517 
 1518                 if (error == 0) {
 1519                         TMPFS_NODE_UNLOCK(tnp);
 1520                         TMPFS_UNLOCK(tm);
 1521                         error = tmpfs_vptocnp_fill(vp, tn, tnp, buf, buflen,
 1522                             dvp);
 1523                         if (error == 0) {
 1524                                 tmpfs_free_node(tm, tnp);
 1525                                 return (0);
 1526                         }
 1527                         if ((vp->v_iflag & VI_DOOMED) != 0) {
 1528                                 tmpfs_free_node(tm, tnp);
 1529                                 return (ENOENT);
 1530                         }
 1531                         TMPFS_LOCK(tm);
 1532                         TMPFS_NODE_LOCK(tnp);
 1533                 }
 1534                 if (tmpfs_free_node_locked(tm, tnp, false)) {
 1535                         goto restart;
 1536                 } else {
 1537                         KASSERT(tnp->tn_refcount > 0,
 1538                             ("node %p refcount zero", tnp));
 1539                         tnp1 = LIST_NEXT(tnp, tn_entries);
 1540                         TMPFS_NODE_UNLOCK(tnp);
 1541                 }
 1542         }
 1543         TMPFS_UNLOCK(tm);
 1544         return (ENOENT);
 1545 }
 1546 
 1547 /*
 1548  * Vnode operations vector used for files stored in a tmpfs file system.
 1549  */
 1550 struct vop_vector tmpfs_vnodeop_entries = {
 1551         .vop_default =                  &default_vnodeops,
 1552         .vop_lookup =                   vfs_cache_lookup,
 1553         .vop_cachedlookup =             tmpfs_cached_lookup,
 1554         .vop_create =                   tmpfs_create,
 1555         .vop_mknod =                    tmpfs_mknod,
 1556         .vop_open =                     tmpfs_open,
 1557         .vop_close =                    tmpfs_close,
 1558         .vop_access =                   tmpfs_access,
 1559         .vop_getattr =                  tmpfs_getattr,
 1560         .vop_setattr =                  tmpfs_setattr,
 1561         .vop_read =                     tmpfs_read,
 1562         .vop_write =                    tmpfs_write,
 1563         .vop_fsync =                    tmpfs_fsync,
 1564         .vop_remove =                   tmpfs_remove,
 1565         .vop_link =                     tmpfs_link,
 1566         .vop_rename =                   tmpfs_rename,
 1567         .vop_mkdir =                    tmpfs_mkdir,
 1568         .vop_rmdir =                    tmpfs_rmdir,
 1569         .vop_symlink =                  tmpfs_symlink,
 1570         .vop_readdir =                  tmpfs_readdir,
 1571         .vop_readlink =                 tmpfs_readlink,
 1572         .vop_inactive =                 tmpfs_inactive,
 1573         .vop_reclaim =                  tmpfs_reclaim,
 1574         .vop_print =                    tmpfs_print,
 1575         .vop_pathconf =                 tmpfs_pathconf,
 1576         .vop_vptofh =                   tmpfs_vptofh,
 1577         .vop_whiteout =                 tmpfs_whiteout,
 1578         .vop_bmap =                     VOP_EOPNOTSUPP,
 1579         .vop_vptocnp =                  tmpfs_vptocnp,
 1580 };
 1581 
 1582 /*
 1583  * Same vector for mounts which do not use namecache.
 1584  */
 1585 struct vop_vector tmpfs_vnodeop_nonc_entries = {
 1586         .vop_default =                  &tmpfs_vnodeop_entries,
 1587         .vop_lookup =                   tmpfs_lookup,
 1588 };

Cache object: 31dfbe3695be8f47f57bc94051b8eac1


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