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

Cache object: 8a09ba16457f08d0cdb8d4c8f32d9ad2


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