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/ufs/ufs/ufs_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 /*-
    2  * Copyright (c) 1982, 1986, 1989, 1993, 1995
    3  *      The Regents of the University of California.  All rights reserved.
    4  * (c) UNIX System Laboratories, Inc.
    5  * All or some portions of this file are derived from material licensed
    6  * to the University of California by American Telephone and Telegraph
    7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    8  * the permission of UNIX System Laboratories, Inc.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD: releng/7.4/sys/ufs/ufs/ufs_vnops.c 194696 2009-06-23 10:59:59Z kib $");
   39 
   40 #include "opt_mac.h"
   41 #include "opt_quota.h"
   42 #include "opt_suiddir.h"
   43 #include "opt_ufs.h"
   44 #include "opt_ffs.h"
   45 
   46 #include <sys/param.h>
   47 #include <sys/systm.h>
   48 #include <sys/malloc.h>
   49 #include <sys/namei.h>
   50 #include <sys/kernel.h>
   51 #include <sys/fcntl.h>
   52 #include <sys/stat.h>
   53 #include <sys/bio.h>
   54 #include <sys/buf.h>
   55 #include <sys/mount.h>
   56 #include <sys/priv.h>
   57 #include <sys/refcount.h>
   58 #include <sys/unistd.h>
   59 #include <sys/vnode.h>
   60 #include <sys/dirent.h>
   61 #include <sys/lockf.h>
   62 #include <sys/conf.h>
   63 #include <sys/acl.h>
   64 #include <sys/jail.h>
   65 
   66 #include <machine/mutex.h>
   67 
   68 #include <security/mac/mac_framework.h>
   69 
   70 #include <sys/file.h>           /* XXX */
   71 
   72 #include <vm/vm.h>
   73 #include <vm/vm_extern.h>
   74 
   75 #include <fs/fifofs/fifo.h>
   76 
   77 #include <ufs/ufs/acl.h>
   78 #include <ufs/ufs/extattr.h>
   79 #include <ufs/ufs/quota.h>
   80 #include <ufs/ufs/inode.h>
   81 #include <ufs/ufs/dir.h>
   82 #include <ufs/ufs/ufsmount.h>
   83 #include <ufs/ufs/ufs_extern.h>
   84 #ifdef UFS_DIRHASH
   85 #include <ufs/ufs/dirhash.h>
   86 #endif
   87 #ifdef UFS_GJOURNAL
   88 #include <ufs/ufs/gjournal.h>
   89 #endif
   90 
   91 #include <ufs/ffs/ffs_extern.h>
   92 
   93 static vop_access_t     ufs_access;
   94 static int ufs_chmod(struct vnode *, int, struct ucred *, struct thread *);
   95 static int ufs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct thread *);
   96 static vop_close_t      ufs_close;
   97 static vop_create_t     ufs_create;
   98 static vop_getattr_t    ufs_getattr;
   99 static vop_link_t       ufs_link;
  100 static int ufs_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
  101 static vop_mkdir_t      ufs_mkdir;
  102 static vop_mknod_t      ufs_mknod;
  103 static vop_open_t       ufs_open;
  104 static vop_pathconf_t   ufs_pathconf;
  105 static vop_print_t      ufs_print;
  106 static vop_readlink_t   ufs_readlink;
  107 static vop_remove_t     ufs_remove;
  108 static vop_rename_t     ufs_rename;
  109 static vop_rmdir_t      ufs_rmdir;
  110 static vop_setattr_t    ufs_setattr;
  111 static vop_strategy_t   ufs_strategy;
  112 static vop_symlink_t    ufs_symlink;
  113 static vop_whiteout_t   ufs_whiteout;
  114 static vop_close_t      ufsfifo_close;
  115 static vop_kqfilter_t   ufsfifo_kqfilter;
  116 
  117 /*
  118  * A virgin directory (no blushing please).
  119  */
  120 static struct dirtemplate mastertemplate = {
  121         0, 12, DT_DIR, 1, ".",
  122         0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
  123 };
  124 static struct odirtemplate omastertemplate = {
  125         0, 12, 1, ".",
  126         0, DIRBLKSIZ - 12, 2, ".."
  127 };
  128 
  129 static void
  130 ufs_itimes_locked(struct vnode *vp)
  131 {
  132         struct inode *ip;
  133         struct timespec ts;
  134 
  135         ASSERT_VI_LOCKED(vp, __func__);
  136 
  137         ip = VTOI(vp);
  138         if (UFS_RDONLY(ip))
  139                 goto out;
  140         if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
  141                 return;
  142 
  143         if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
  144                 ip->i_flag |= IN_LAZYMOD;
  145         else if (((vp->v_mount->mnt_kern_flag &
  146                     (MNTK_SUSPENDED | MNTK_SUSPEND)) == 0) ||
  147                     (ip->i_flag & (IN_CHANGE | IN_UPDATE)))
  148                 ip->i_flag |= IN_MODIFIED;
  149         else if (ip->i_flag & IN_ACCESS)
  150                 ip->i_flag |= IN_LAZYACCESS;
  151         vfs_timestamp(&ts);
  152         if (ip->i_flag & IN_ACCESS) {
  153                 DIP_SET(ip, i_atime, ts.tv_sec);
  154                 DIP_SET(ip, i_atimensec, ts.tv_nsec);
  155         }
  156         if (ip->i_flag & IN_UPDATE) {
  157                 DIP_SET(ip, i_mtime, ts.tv_sec);
  158                 DIP_SET(ip, i_mtimensec, ts.tv_nsec);
  159                 ip->i_modrev++;
  160         }
  161         if (ip->i_flag & IN_CHANGE) {
  162                 DIP_SET(ip, i_ctime, ts.tv_sec);
  163                 DIP_SET(ip, i_ctimensec, ts.tv_nsec);
  164         }
  165 
  166  out:
  167         ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
  168 }
  169 
  170 void
  171 ufs_itimes(struct vnode *vp)
  172 {
  173 
  174         VI_LOCK(vp);
  175         ufs_itimes_locked(vp);
  176         VI_UNLOCK(vp);
  177 }
  178 
  179 /*
  180  * Create a regular file
  181  */
  182 static int
  183 ufs_create(ap)
  184         struct vop_create_args /* {
  185                 struct vnode *a_dvp;
  186                 struct vnode **a_vpp;
  187                 struct componentname *a_cnp;
  188                 struct vattr *a_vap;
  189         } */ *ap;
  190 {
  191         int error;
  192 
  193         error =
  194             ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
  195             ap->a_dvp, ap->a_vpp, ap->a_cnp);
  196         if (error)
  197                 return (error);
  198         return (0);
  199 }
  200 
  201 /*
  202  * Mknod vnode call
  203  */
  204 /* ARGSUSED */
  205 static int
  206 ufs_mknod(ap)
  207         struct vop_mknod_args /* {
  208                 struct vnode *a_dvp;
  209                 struct vnode **a_vpp;
  210                 struct componentname *a_cnp;
  211                 struct vattr *a_vap;
  212         } */ *ap;
  213 {
  214         struct vattr *vap = ap->a_vap;
  215         struct vnode **vpp = ap->a_vpp;
  216         struct inode *ip;
  217         ino_t ino;
  218         int error;
  219 
  220         error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
  221             ap->a_dvp, vpp, ap->a_cnp);
  222         if (error)
  223                 return (error);
  224         ip = VTOI(*vpp);
  225         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
  226         if (vap->va_rdev != VNOVAL) {
  227                 /*
  228                  * Want to be able to use this to make badblock
  229                  * inodes, so don't truncate the dev number.
  230                  */
  231                 DIP_SET(ip, i_rdev, vap->va_rdev);
  232         }
  233         /*
  234          * Remove inode, then reload it through VFS_VGET so it is
  235          * checked to see if it is an alias of an existing entry in
  236          * the inode cache.  XXX I don't believe this is necessary now.
  237          */
  238         (*vpp)->v_type = VNON;
  239         ino = ip->i_number;     /* Save this before vgone() invalidates ip. */
  240         vgone(*vpp);
  241         vput(*vpp);
  242         error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
  243         if (error) {
  244                 *vpp = NULL;
  245                 return (error);
  246         }
  247         return (0);
  248 }
  249 
  250 /*
  251  * Open called.
  252  */
  253 /* ARGSUSED */
  254 static int
  255 ufs_open(struct vop_open_args *ap)
  256 {
  257         struct vnode *vp = ap->a_vp;
  258         struct inode *ip;
  259 
  260         if (vp->v_type == VCHR || vp->v_type == VBLK)
  261                 return (EOPNOTSUPP);
  262 
  263         ip = VTOI(vp);
  264         /*
  265          * Files marked append-only must be opened for appending.
  266          */
  267         if ((ip->i_flags & APPEND) &&
  268             (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
  269                 return (EPERM);
  270         vnode_create_vobject(vp, DIP(ip, i_size), ap->a_td);
  271         return (0);
  272 }
  273 
  274 /*
  275  * Close called.
  276  *
  277  * Update the times on the inode.
  278  */
  279 /* ARGSUSED */
  280 static int
  281 ufs_close(ap)
  282         struct vop_close_args /* {
  283                 struct vnode *a_vp;
  284                 int  a_fflag;
  285                 struct ucred *a_cred;
  286                 struct thread *a_td;
  287         } */ *ap;
  288 {
  289         struct vnode *vp = ap->a_vp;
  290         int usecount;
  291 
  292         VI_LOCK(vp);
  293         usecount = vp->v_usecount;
  294         if (usecount > 1)
  295                 ufs_itimes_locked(vp);
  296         VI_UNLOCK(vp);
  297         return (0);
  298 }
  299 
  300 static int
  301 ufs_access(ap)
  302         struct vop_access_args /* {
  303                 struct vnode *a_vp;
  304                 int  a_mode;
  305                 struct ucred *a_cred;
  306                 struct thread *a_td;
  307         } */ *ap;
  308 {
  309         struct vnode *vp = ap->a_vp;
  310         struct inode *ip = VTOI(vp);
  311         mode_t mode = ap->a_mode;
  312         int error;
  313 #ifdef QUOTA
  314         int relocked;
  315 #endif
  316 #ifdef UFS_ACL
  317         struct acl *acl;
  318 #endif
  319 
  320         /*
  321          * Disallow write attempts on read-only filesystems;
  322          * unless the file is a socket, fifo, or a block or
  323          * character device resident on the filesystem.
  324          */
  325         if (mode & VWRITE) {
  326                 switch (vp->v_type) {
  327                 case VDIR:
  328                 case VLNK:
  329                 case VREG:
  330                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
  331                                 return (EROFS);
  332 #ifdef QUOTA
  333                         /*
  334                          * Inode is accounted in the quotas only if struct
  335                          * dquot is attached to it. VOP_ACCESS() is called
  336                          * from vn_open_cred() and provides a convenient
  337                          * point to call getinoquota().
  338                          */
  339                         if (VOP_ISLOCKED(vp, ap->a_td) != LK_EXCLUSIVE) {
  340 
  341                                 /*
  342                                  * Upgrade vnode lock, since getinoquota()
  343                                  * requires exclusive lock to modify inode.
  344                                  */
  345                                 relocked = 1;
  346                                 vhold(vp);
  347                                 vn_lock(vp, LK_UPGRADE | LK_RETRY, ap->a_td);
  348                                 VI_LOCK(vp);
  349                                 if (vp->v_iflag & VI_DOOMED) {
  350                                         vdropl(vp);
  351                                         error = ENOENT;
  352                                         goto relock;
  353                                 }
  354                                 vdropl(vp);
  355                         } else
  356                                 relocked = 0;
  357                         error = getinoquota(ip);
  358 relock:
  359                         if (relocked)
  360                                 vn_lock(vp, LK_DOWNGRADE | LK_RETRY, ap->a_td);
  361                         if (error != 0)
  362                                 return (error);
  363 #endif
  364                         break;
  365                 default:
  366                         break;
  367                 }
  368         }
  369 
  370         /* If immutable bit set, nobody gets to write it. */
  371         if ((mode & VWRITE) && (ip->i_flags & (IMMUTABLE | SF_SNAPSHOT)))
  372                 return (EPERM);
  373 
  374 #ifdef UFS_ACL
  375         if ((vp->v_mount->mnt_flag & MNT_ACLS) != 0) {
  376                 acl = uma_zalloc(acl_zone, M_WAITOK);
  377                 error = VOP_GETACL(vp, ACL_TYPE_ACCESS, acl, ap->a_cred,
  378                     ap->a_td);
  379                 switch (error) {
  380                 case EOPNOTSUPP:
  381                         error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
  382                             ip->i_gid, ap->a_mode, ap->a_cred, NULL);
  383                         break;
  384                 case 0:
  385                         error = vaccess_acl_posix1e(vp->v_type, ip->i_uid,
  386                             ip->i_gid, acl, ap->a_mode, ap->a_cred, NULL);
  387                         break;
  388                 default:
  389                         printf(
  390 "ufs_access(): Error retrieving ACL on object (%d).\n",
  391                             error);
  392                         /*
  393                          * XXX: Fall back until debugged.  Should
  394                          * eventually possibly log an error, and return
  395                          * EPERM for safety.
  396                          */
  397                         error = vaccess(vp->v_type, ip->i_mode, ip->i_uid,
  398                             ip->i_gid, ap->a_mode, ap->a_cred, NULL);
  399                 }
  400                 uma_zfree(acl_zone, acl);
  401         } else
  402 #endif /* !UFS_ACL */
  403                 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
  404                     ap->a_mode, ap->a_cred, NULL);
  405         return (error);
  406 }
  407 
  408 /* ARGSUSED */
  409 static int
  410 ufs_getattr(ap)
  411         struct vop_getattr_args /* {
  412                 struct vnode *a_vp;
  413                 struct vattr *a_vap;
  414                 struct ucred *a_cred;
  415                 struct thread *a_td;
  416         } */ *ap;
  417 {
  418         struct vnode *vp = ap->a_vp;
  419         struct inode *ip = VTOI(vp);
  420         struct vattr *vap = ap->a_vap;
  421 
  422         VI_LOCK(vp);
  423         ufs_itimes_locked(vp);
  424         if (ip->i_ump->um_fstype == UFS1) {
  425                 vap->va_atime.tv_sec = ip->i_din1->di_atime;
  426                 vap->va_atime.tv_nsec = ip->i_din1->di_atimensec;
  427         } else {
  428                 vap->va_atime.tv_sec = ip->i_din2->di_atime;
  429                 vap->va_atime.tv_nsec = ip->i_din2->di_atimensec;
  430         }
  431         VI_UNLOCK(vp);
  432         /*
  433          * Copy from inode table
  434          */
  435         vap->va_fsid = dev2udev(ip->i_dev);
  436         vap->va_fileid = ip->i_number;
  437         vap->va_mode = ip->i_mode & ~IFMT;
  438         vap->va_nlink = ip->i_effnlink;
  439         vap->va_uid = ip->i_uid;
  440         vap->va_gid = ip->i_gid;
  441         if (ip->i_ump->um_fstype == UFS1) {
  442                 vap->va_rdev = ip->i_din1->di_rdev;
  443                 vap->va_size = ip->i_din1->di_size;
  444                 vap->va_mtime.tv_sec = ip->i_din1->di_mtime;
  445                 vap->va_mtime.tv_nsec = ip->i_din1->di_mtimensec;
  446                 vap->va_ctime.tv_sec = ip->i_din1->di_ctime;
  447                 vap->va_ctime.tv_nsec = ip->i_din1->di_ctimensec;
  448                 vap->va_bytes = dbtob((u_quad_t)ip->i_din1->di_blocks);
  449         } else {
  450                 vap->va_rdev = ip->i_din2->di_rdev;
  451                 vap->va_size = ip->i_din2->di_size;
  452                 vap->va_mtime.tv_sec = ip->i_din2->di_mtime;
  453                 vap->va_mtime.tv_nsec = ip->i_din2->di_mtimensec;
  454                 vap->va_ctime.tv_sec = ip->i_din2->di_ctime;
  455                 vap->va_ctime.tv_nsec = ip->i_din2->di_ctimensec;
  456                 vap->va_birthtime.tv_sec = ip->i_din2->di_birthtime;
  457                 vap->va_birthtime.tv_nsec = ip->i_din2->di_birthnsec;
  458                 vap->va_bytes = dbtob((u_quad_t)ip->i_din2->di_blocks);
  459         }
  460         vap->va_flags = ip->i_flags;
  461         vap->va_gen = ip->i_gen;
  462         vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
  463         vap->va_type = IFTOVT(ip->i_mode);
  464         vap->va_filerev = ip->i_modrev;
  465         return (0);
  466 }
  467 
  468 /*
  469  * Set attribute vnode op. called from several syscalls
  470  */
  471 static int
  472 ufs_setattr(ap)
  473         struct vop_setattr_args /* {
  474                 struct vnode *a_vp;
  475                 struct vattr *a_vap;
  476                 struct ucred *a_cred;
  477                 struct thread *a_td;
  478         } */ *ap;
  479 {
  480         struct vattr *vap = ap->a_vap;
  481         struct vnode *vp = ap->a_vp;
  482         struct inode *ip = VTOI(vp);
  483         struct ucred *cred = ap->a_cred;
  484         struct thread *td = ap->a_td;
  485         int error;
  486 
  487         /*
  488          * Check for unsettable attributes.
  489          */
  490         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
  491             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
  492             (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
  493             ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
  494                 return (EINVAL);
  495         }
  496         /*
  497          * Mark for update the file's access time for vfs_mark_atime().
  498          * We are doing this here to avoid some of the checks done
  499          * below -- this operation is done by request of the kernel and
  500          * should bypass some security checks.  Things like read-only
  501          * checks get handled by other levels (e.g., ffs_update()).
  502          */
  503         if (vap->va_vaflags & VA_MARK_ATIME) {
  504                 ip->i_flag |= IN_ACCESS;
  505                 return (0);
  506         }
  507         if (vap->va_flags != VNOVAL) {
  508                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  509                         return (EROFS);
  510                 /*
  511                  * Callers may only modify the file flags on objects they
  512                  * have VADMIN rights for.
  513                  */
  514                 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
  515                         return (error);
  516                 /*
  517                  * Unprivileged processes are not permitted to unset system
  518                  * flags, or modify flags if any system flags are set.
  519                  * Privileged non-jail processes may not modify system flags
  520                  * if securelevel > 0 and any existing system flags are set.
  521                  * Privileged jail processes behave like privileged non-jail
  522                  * processes if the security.jail.chflags_allowed sysctl is
  523                  * is non-zero; otherwise, they behave like unprivileged
  524                  * processes.
  525                  */
  526                 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
  527                         if (ip->i_flags
  528                             & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
  529                                 error = securelevel_gt(cred, 0);
  530                                 if (error)
  531                                         return (error);
  532                         }
  533                         /* Snapshot flag cannot be set or cleared */
  534                         if (((vap->va_flags & SF_SNAPSHOT) != 0 &&
  535                              (ip->i_flags & SF_SNAPSHOT) == 0) ||
  536                             ((vap->va_flags & SF_SNAPSHOT) == 0 &&
  537                              (ip->i_flags & SF_SNAPSHOT) != 0))
  538                                 return (EPERM);
  539                         ip->i_flags = vap->va_flags;
  540                         DIP_SET(ip, i_flags, vap->va_flags);
  541                 } else {
  542                         if (ip->i_flags
  543                             & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
  544                             (vap->va_flags & UF_SETTABLE) != vap->va_flags)
  545                                 return (EPERM);
  546                         ip->i_flags &= SF_SETTABLE;
  547                         ip->i_flags |= (vap->va_flags & UF_SETTABLE);
  548                         DIP_SET(ip, i_flags, ip->i_flags);
  549                 }
  550                 ip->i_flag |= IN_CHANGE;
  551                 if (vap->va_flags & (IMMUTABLE | APPEND))
  552                         return (0);
  553         }
  554         if (ip->i_flags & (IMMUTABLE | APPEND))
  555                 return (EPERM);
  556         /*
  557          * Go through the fields and update iff not VNOVAL.
  558          */
  559         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
  560                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  561                         return (EROFS);
  562                 if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred,
  563                     td)) != 0)
  564                         return (error);
  565         }
  566         if (vap->va_size != VNOVAL) {
  567                 /*
  568                  * XXX most of the following special cases should be in
  569                  * callers instead of in N filesystems.  The VDIR check
  570                  * mostly already is.
  571                  */
  572                 switch (vp->v_type) {
  573                 case VDIR:
  574                         return (EISDIR);
  575                 case VLNK:
  576                 case VREG:
  577                         /*
  578                          * Truncation should have an effect in these cases.
  579                          * Disallow it if the filesystem is read-only or
  580                          * the file is being snapshotted.
  581                          */
  582                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
  583                                 return (EROFS);
  584                         if ((ip->i_flags & SF_SNAPSHOT) != 0)
  585                                 return (EPERM);
  586                         break;
  587                 default:
  588                         /*
  589                          * According to POSIX, the result is unspecified
  590                          * for file types other than regular files,
  591                          * directories and shared memory objects.  We
  592                          * don't support shared memory objects in the file
  593                          * system, and have dubious support for truncating
  594                          * symlinks.  Just ignore the request in other cases.
  595                          */
  596                         return (0);
  597                 }
  598                 if ((error = UFS_TRUNCATE(vp, vap->va_size, IO_NORMAL,
  599                     cred, td)) != 0)
  600                         return (error);
  601         }
  602         if (vap->va_atime.tv_sec != VNOVAL ||
  603             vap->va_mtime.tv_sec != VNOVAL ||
  604             vap->va_birthtime.tv_sec != VNOVAL) {
  605                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  606                         return (EROFS);
  607                 if ((ip->i_flags & SF_SNAPSHOT) != 0)
  608                         return (EPERM);
  609                 /*
  610                  * From utimes(2):
  611                  * If times is NULL, ... The caller must be the owner of
  612                  * the file, have permission to write the file, or be the
  613                  * super-user.
  614                  * If times is non-NULL, ... The caller must be the owner of
  615                  * the file or be the super-user.
  616                  *
  617                  * Possibly for historical reasons, try to use VADMIN in
  618                  * preference to VWRITE for a NULL timestamp.  This means we
  619                  * will return EACCES in preference to EPERM if neither
  620                  * check succeeds.
  621                  */
  622                 if (vap->va_vaflags & VA_UTIMES_NULL) {
  623                         error = VOP_ACCESS(vp, VADMIN, cred, td);
  624                         if (error)
  625                                 error = VOP_ACCESS(vp, VWRITE, cred, td);
  626                 } else
  627                         error = VOP_ACCESS(vp, VADMIN, cred, td);
  628                 if (error)
  629                         return (error);
  630                 if (vap->va_atime.tv_sec != VNOVAL)
  631                         ip->i_flag |= IN_ACCESS;
  632                 if (vap->va_mtime.tv_sec != VNOVAL)
  633                         ip->i_flag |= IN_CHANGE | IN_UPDATE;
  634                 if (vap->va_birthtime.tv_sec != VNOVAL &&
  635                     ip->i_ump->um_fstype == UFS2)
  636                         ip->i_flag |= IN_MODIFIED;
  637                 ufs_itimes(vp);
  638                 if (vap->va_atime.tv_sec != VNOVAL) {
  639                         DIP_SET(ip, i_atime, vap->va_atime.tv_sec);
  640                         DIP_SET(ip, i_atimensec, vap->va_atime.tv_nsec);
  641                 }
  642                 if (vap->va_mtime.tv_sec != VNOVAL) {
  643                         DIP_SET(ip, i_mtime, vap->va_mtime.tv_sec);
  644                         DIP_SET(ip, i_mtimensec, vap->va_mtime.tv_nsec);
  645                 }
  646                 if (vap->va_birthtime.tv_sec != VNOVAL &&
  647                     ip->i_ump->um_fstype == UFS2) {
  648                         ip->i_din2->di_birthtime = vap->va_birthtime.tv_sec;
  649                         ip->i_din2->di_birthnsec = vap->va_birthtime.tv_nsec;
  650                 }
  651                 error = UFS_UPDATE(vp, 0);
  652                 if (error)
  653                         return (error);
  654         }
  655         error = 0;
  656         if (vap->va_mode != (mode_t)VNOVAL) {
  657                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  658                         return (EROFS);
  659                 if ((ip->i_flags & SF_SNAPSHOT) != 0 && (vap->va_mode &
  660                    (S_IXUSR | S_IWUSR | S_IXGRP | S_IWGRP | S_IXOTH | S_IWOTH)))
  661                         return (EPERM);
  662                 error = ufs_chmod(vp, (int)vap->va_mode, cred, td);
  663         }
  664         return (error);
  665 }
  666 
  667 /*
  668  * Change the mode on a file.
  669  * Inode must be locked before calling.
  670  */
  671 static int
  672 ufs_chmod(vp, mode, cred, td)
  673         struct vnode *vp;
  674         int mode;
  675         struct ucred *cred;
  676         struct thread *td;
  677 {
  678         struct inode *ip = VTOI(vp);
  679         int error;
  680 
  681         /*
  682          * To modify the permissions on a file, must possess VADMIN
  683          * for that file.
  684          */
  685         if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
  686                 return (error);
  687         /*
  688          * Privileged processes may set the sticky bit on non-directories,
  689          * as well as set the setgid bit on a file with a group that the
  690          * process is not a member of.  Both of these are allowed in
  691          * jail(8).
  692          */
  693         if (vp->v_type != VDIR && (mode & S_ISTXT)) {
  694                 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
  695                         return (EFTYPE);
  696         }
  697         if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
  698                 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
  699                 if (error)
  700                         return (error);
  701         }
  702         ip->i_mode &= ~ALLPERMS;
  703         ip->i_mode |= (mode & ALLPERMS);
  704         DIP_SET(ip, i_mode, ip->i_mode);
  705         ip->i_flag |= IN_CHANGE;
  706         return (0);
  707 }
  708 
  709 /*
  710  * Perform chown operation on inode ip;
  711  * inode must be locked prior to call.
  712  */
  713 static int
  714 ufs_chown(vp, uid, gid, cred, td)
  715         struct vnode *vp;
  716         uid_t uid;
  717         gid_t gid;
  718         struct ucred *cred;
  719         struct thread *td;
  720 {
  721         struct inode *ip = VTOI(vp);
  722         uid_t ouid;
  723         gid_t ogid;
  724         int error = 0;
  725 #ifdef QUOTA
  726         int i;
  727         ufs2_daddr_t change;
  728 #endif
  729 
  730         if (uid == (uid_t)VNOVAL)
  731                 uid = ip->i_uid;
  732         if (gid == (gid_t)VNOVAL)
  733                 gid = ip->i_gid;
  734         /*
  735          * To modify the ownership of a file, must possess VADMIN for that
  736          * file.
  737          */
  738         if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
  739                 return (error);
  740         /*
  741          * To change the owner of a file, or change the group of a file to a
  742          * group of which we are not a member, the caller must have
  743          * privilege.
  744          */
  745         if ((uid != ip->i_uid || 
  746             (gid != ip->i_gid && !groupmember(gid, cred))) &&
  747             (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
  748                 return (error);
  749         ogid = ip->i_gid;
  750         ouid = ip->i_uid;
  751 #ifdef QUOTA
  752         if ((error = getinoquota(ip)) != 0)
  753                 return (error);
  754         if (ouid == uid) {
  755                 dqrele(vp, ip->i_dquot[USRQUOTA]);
  756                 ip->i_dquot[USRQUOTA] = NODQUOT;
  757         }
  758         if (ogid == gid) {
  759                 dqrele(vp, ip->i_dquot[GRPQUOTA]);
  760                 ip->i_dquot[GRPQUOTA] = NODQUOT;
  761         }
  762         change = DIP(ip, i_blocks);
  763         (void) chkdq(ip, -change, cred, CHOWN);
  764         (void) chkiq(ip, -1, cred, CHOWN);
  765         for (i = 0; i < MAXQUOTAS; i++) {
  766                 dqrele(vp, ip->i_dquot[i]);
  767                 ip->i_dquot[i] = NODQUOT;
  768         }
  769 #endif
  770         ip->i_gid = gid;
  771         DIP_SET(ip, i_gid, gid);
  772         ip->i_uid = uid;
  773         DIP_SET(ip, i_uid, uid);
  774 #ifdef QUOTA
  775         if ((error = getinoquota(ip)) == 0) {
  776                 if (ouid == uid) {
  777                         dqrele(vp, ip->i_dquot[USRQUOTA]);
  778                         ip->i_dquot[USRQUOTA] = NODQUOT;
  779                 }
  780                 if (ogid == gid) {
  781                         dqrele(vp, ip->i_dquot[GRPQUOTA]);
  782                         ip->i_dquot[GRPQUOTA] = NODQUOT;
  783                 }
  784                 if ((error = chkdq(ip, change, cred, CHOWN)) == 0) {
  785                         if ((error = chkiq(ip, 1, cred, CHOWN)) == 0)
  786                                 goto good;
  787                         else
  788                                 (void) chkdq(ip, -change, cred, CHOWN|FORCE);
  789                 }
  790                 for (i = 0; i < MAXQUOTAS; i++) {
  791                         dqrele(vp, ip->i_dquot[i]);
  792                         ip->i_dquot[i] = NODQUOT;
  793                 }
  794         }
  795         ip->i_gid = ogid;
  796         DIP_SET(ip, i_gid, ogid);
  797         ip->i_uid = ouid;
  798         DIP_SET(ip, i_uid, ouid);
  799         if (getinoquota(ip) == 0) {
  800                 if (ouid == uid) {
  801                         dqrele(vp, ip->i_dquot[USRQUOTA]);
  802                         ip->i_dquot[USRQUOTA] = NODQUOT;
  803                 }
  804                 if (ogid == gid) {
  805                         dqrele(vp, ip->i_dquot[GRPQUOTA]);
  806                         ip->i_dquot[GRPQUOTA] = NODQUOT;
  807                 }
  808                 (void) chkdq(ip, change, cred, FORCE|CHOWN);
  809                 (void) chkiq(ip, 1, cred, FORCE|CHOWN);
  810                 (void) getinoquota(ip);
  811         }
  812         return (error);
  813 good:
  814         if (getinoquota(ip))
  815                 panic("ufs_chown: lost quota");
  816 #endif /* QUOTA */
  817         ip->i_flag |= IN_CHANGE;
  818         if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
  819                 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0)) {
  820                         ip->i_mode &= ~(ISUID | ISGID);
  821                         DIP_SET(ip, i_mode, ip->i_mode);
  822                 }
  823         }
  824         return (0);
  825 }
  826 
  827 static int
  828 ufs_remove(ap)
  829         struct vop_remove_args /* {
  830                 struct vnode *a_dvp;
  831                 struct vnode *a_vp;
  832                 struct componentname *a_cnp;
  833         } */ *ap;
  834 {
  835         struct inode *ip;
  836         struct vnode *vp = ap->a_vp;
  837         struct vnode *dvp = ap->a_dvp;
  838         int error;
  839         struct thread *td;
  840 
  841         td = curthread;
  842         ip = VTOI(vp);
  843         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
  844             (VTOI(dvp)->i_flags & APPEND)) {
  845                 error = EPERM;
  846                 goto out;
  847         }
  848 #ifdef UFS_GJOURNAL
  849         ufs_gjournal_orphan(vp);
  850 #endif
  851         error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0);
  852         if (ip->i_nlink <= 0)
  853                 vp->v_vflag |= VV_NOSYNC;
  854         if ((ip->i_flags & SF_SNAPSHOT) != 0) {
  855                 /*
  856                  * Avoid deadlock where another thread is trying to
  857                  * update the inodeblock for dvp and is waiting on
  858                  * snaplk.  Temporary unlock the vnode lock for the
  859                  * unlinked file and sync the directory.  This should
  860                  * allow vput() of the directory to not block later on
  861                  * while holding the snapshot vnode locked, assuming
  862                  * that the directory hasn't been unlinked too.
  863                  */
  864                 VOP_UNLOCK(vp, 0, td);
  865                 (void) VOP_FSYNC(dvp, MNT_WAIT, td);
  866                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
  867         }
  868 out:
  869         return (error);
  870 }
  871 
  872 /*
  873  * link vnode call
  874  */
  875 static int
  876 ufs_link(ap)
  877         struct vop_link_args /* {
  878                 struct vnode *a_tdvp;
  879                 struct vnode *a_vp;
  880                 struct componentname *a_cnp;
  881         } */ *ap;
  882 {
  883         struct vnode *vp = ap->a_vp;
  884         struct vnode *tdvp = ap->a_tdvp;
  885         struct componentname *cnp = ap->a_cnp;
  886         struct inode *ip;
  887         struct direct newdir;
  888         int error;
  889 
  890 #ifdef INVARIANTS
  891         if ((cnp->cn_flags & HASBUF) == 0)
  892                 panic("ufs_link: no name");
  893 #endif
  894         if (tdvp->v_mount != vp->v_mount) {
  895                 error = EXDEV;
  896                 goto out;
  897         }
  898         ip = VTOI(vp);
  899         if ((nlink_t)ip->i_nlink >= LINK_MAX) {
  900                 error = EMLINK;
  901                 goto out;
  902         }
  903         if (ip->i_flags & (IMMUTABLE | APPEND)) {
  904                 error = EPERM;
  905                 goto out;
  906         }
  907         ip->i_effnlink++;
  908         ip->i_nlink++;
  909         DIP_SET(ip, i_nlink, ip->i_nlink);
  910         ip->i_flag |= IN_CHANGE;
  911         if (DOINGSOFTDEP(vp))
  912                 softdep_change_linkcnt(ip);
  913         error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp)));
  914         if (!error) {
  915                 ufs_makedirentry(ip, cnp, &newdir);
  916                 error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL);
  917         }
  918 
  919         if (error) {
  920                 ip->i_effnlink--;
  921                 ip->i_nlink--;
  922                 DIP_SET(ip, i_nlink, ip->i_nlink);
  923                 ip->i_flag |= IN_CHANGE;
  924                 if (DOINGSOFTDEP(vp))
  925                         softdep_change_linkcnt(ip);
  926         }
  927 out:
  928         return (error);
  929 }
  930 
  931 /*
  932  * whiteout vnode call
  933  */
  934 static int
  935 ufs_whiteout(ap)
  936         struct vop_whiteout_args /* {
  937                 struct vnode *a_dvp;
  938                 struct componentname *a_cnp;
  939                 int a_flags;
  940         } */ *ap;
  941 {
  942         struct vnode *dvp = ap->a_dvp;
  943         struct componentname *cnp = ap->a_cnp;
  944         struct direct newdir;
  945         int error = 0;
  946 
  947         switch (ap->a_flags) {
  948         case LOOKUP:
  949                 /* 4.4 format directories support whiteout operations */
  950                 if (dvp->v_mount->mnt_maxsymlinklen > 0)
  951                         return (0);
  952                 return (EOPNOTSUPP);
  953 
  954         case CREATE:
  955                 /* create a new directory whiteout */
  956 #ifdef INVARIANTS
  957                 if ((cnp->cn_flags & SAVENAME) == 0)
  958                         panic("ufs_whiteout: missing name");
  959                 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
  960                         panic("ufs_whiteout: old format filesystem");
  961 #endif
  962 
  963                 newdir.d_ino = WINO;
  964                 newdir.d_namlen = cnp->cn_namelen;
  965                 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
  966                 newdir.d_type = DT_WHT;
  967                 error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL);
  968                 break;
  969 
  970         case DELETE:
  971                 /* remove an existing directory whiteout */
  972 #ifdef INVARIANTS
  973                 if (dvp->v_mount->mnt_maxsymlinklen <= 0)
  974                         panic("ufs_whiteout: old format filesystem");
  975 #endif
  976 
  977                 cnp->cn_flags &= ~DOWHITEOUT;
  978                 error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0);
  979                 break;
  980         default:
  981                 panic("ufs_whiteout: unknown op");
  982         }
  983         return (error);
  984 }
  985 
  986 /*
  987  * Rename system call.
  988  *      rename("foo", "bar");
  989  * is essentially
  990  *      unlink("bar");
  991  *      link("foo", "bar");
  992  *      unlink("foo");
  993  * but ``atomically''.  Can't do full commit without saving state in the
  994  * inode on disk which isn't feasible at this time.  Best we can do is
  995  * always guarantee the target exists.
  996  *
  997  * Basic algorithm is:
  998  *
  999  * 1) Bump link count on source while we're linking it to the
 1000  *    target.  This also ensure the inode won't be deleted out
 1001  *    from underneath us while we work (it may be truncated by
 1002  *    a concurrent `trunc' or `open' for creation).
 1003  * 2) Link source to destination.  If destination already exists,
 1004  *    delete it first.
 1005  * 3) Unlink source reference to inode if still around. If a
 1006  *    directory was moved and the parent of the destination
 1007  *    is different from the source, patch the ".." entry in the
 1008  *    directory.
 1009  */
 1010 static int
 1011 ufs_rename(ap)
 1012         struct vop_rename_args  /* {
 1013                 struct vnode *a_fdvp;
 1014                 struct vnode *a_fvp;
 1015                 struct componentname *a_fcnp;
 1016                 struct vnode *a_tdvp;
 1017                 struct vnode *a_tvp;
 1018                 struct componentname *a_tcnp;
 1019         } */ *ap;
 1020 {
 1021         struct vnode *tvp = ap->a_tvp;
 1022         struct vnode *tdvp = ap->a_tdvp;
 1023         struct vnode *fvp = ap->a_fvp;
 1024         struct vnode *fdvp = ap->a_fdvp;
 1025         struct componentname *tcnp = ap->a_tcnp;
 1026         struct componentname *fcnp = ap->a_fcnp;
 1027         struct thread *td = fcnp->cn_thread;
 1028         struct inode *ip, *xp, *dp;
 1029         struct direct newdir;
 1030         int doingdirectory = 0, oldparent = 0, newparent = 0;
 1031         int error = 0, ioflag;
 1032         ino_t fvp_ino;
 1033 
 1034 #ifdef INVARIANTS
 1035         if ((tcnp->cn_flags & HASBUF) == 0 ||
 1036             (fcnp->cn_flags & HASBUF) == 0)
 1037                 panic("ufs_rename: no name");
 1038 #endif
 1039         /*
 1040          * Check for cross-device rename.
 1041          */
 1042         if ((fvp->v_mount != tdvp->v_mount) ||
 1043             (tvp && (fvp->v_mount != tvp->v_mount))) {
 1044                 error = EXDEV;
 1045 abortit:
 1046                 if (tdvp == tvp)
 1047                         vrele(tdvp);
 1048                 else
 1049                         vput(tdvp);
 1050                 if (tvp)
 1051                         vput(tvp);
 1052                 vrele(fdvp);
 1053                 vrele(fvp);
 1054                 return (error);
 1055         }
 1056 
 1057         if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
 1058             (VTOI(tdvp)->i_flags & APPEND))) {
 1059                 error = EPERM;
 1060                 goto abortit;
 1061         }
 1062 
 1063         /*
 1064          * Renaming a file to itself has no effect.  The upper layers should
 1065          * not call us in that case.  Temporarily just warn if they do.
 1066          */
 1067         if (fvp == tvp) {
 1068                 printf("ufs_rename: fvp == tvp (can't happen)\n");
 1069                 error = 0;
 1070                 goto abortit;
 1071         }
 1072 
 1073         if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
 1074                 goto abortit;
 1075         dp = VTOI(fdvp);
 1076         ip = VTOI(fvp);
 1077         if (ip->i_nlink >= LINK_MAX) {
 1078                 VOP_UNLOCK(fvp, 0, td);
 1079                 error = EMLINK;
 1080                 goto abortit;
 1081         }
 1082         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
 1083             || (dp->i_flags & APPEND)) {
 1084                 VOP_UNLOCK(fvp, 0, td);
 1085                 error = EPERM;
 1086                 goto abortit;
 1087         }
 1088         if ((ip->i_mode & IFMT) == IFDIR) {
 1089                 /*
 1090                  * Avoid ".", "..", and aliases of "." for obvious reasons.
 1091                  */
 1092                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
 1093                     dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
 1094                     (ip->i_flag & IN_RENAME)) {
 1095                         VOP_UNLOCK(fvp, 0, td);
 1096                         error = EINVAL;
 1097                         goto abortit;
 1098                 }
 1099                 ip->i_flag |= IN_RENAME;
 1100                 oldparent = dp->i_number;
 1101                 doingdirectory = 1;
 1102         }
 1103         vrele(fdvp);
 1104 
 1105         /*
 1106          * When the target exists, both the directory
 1107          * and target vnodes are returned locked.
 1108          */
 1109         dp = VTOI(tdvp);
 1110         xp = NULL;
 1111         if (tvp)
 1112                 xp = VTOI(tvp);
 1113 
 1114         /*
 1115          * 1) Bump link count while we're moving stuff
 1116          *    around.  If we crash somewhere before
 1117          *    completing our work, the link count
 1118          *    may be wrong, but correctable.
 1119          */
 1120         ip->i_effnlink++;
 1121         ip->i_nlink++;
 1122         DIP_SET(ip, i_nlink, ip->i_nlink);
 1123         ip->i_flag |= IN_CHANGE;
 1124         if (DOINGSOFTDEP(fvp))
 1125                 softdep_change_linkcnt(ip);
 1126         if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) |
 1127                                        DOINGASYNC(fvp)))) != 0) {
 1128                 VOP_UNLOCK(fvp, 0, td);
 1129                 goto bad;
 1130         }
 1131 
 1132         /*
 1133          * If ".." must be changed (ie the directory gets a new
 1134          * parent) then the source directory must not be in the
 1135          * directory hierarchy above the target, as this would
 1136          * orphan everything below the source directory. Also
 1137          * the user must have write permission in the source so
 1138          * as to be able to change "..". We must repeat the call
 1139          * to namei, as the parent directory is unlocked by the
 1140          * call to checkpath().
 1141          */
 1142         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
 1143         fvp_ino = ip->i_number;
 1144         VOP_UNLOCK(fvp, 0, td);
 1145         if (oldparent != dp->i_number)
 1146                 newparent = dp->i_number;
 1147         if (doingdirectory && newparent) {
 1148                 if (error)      /* write access check above */
 1149                         goto bad;
 1150                 if (xp != NULL)
 1151                         vput(tvp);
 1152                 error = ufs_checkpath(fvp_ino, dp, tcnp->cn_cred);
 1153                 if (error)
 1154                         goto out;
 1155                 if ((tcnp->cn_flags & SAVESTART) == 0)
 1156                         panic("ufs_rename: lost to startdir");
 1157                 VREF(tdvp);
 1158                 error = relookup(tdvp, &tvp, tcnp);
 1159                 if (error)
 1160                         goto out;
 1161                 vrele(tdvp);
 1162                 dp = VTOI(tdvp);
 1163                 xp = NULL;
 1164                 if (tvp)
 1165                         xp = VTOI(tvp);
 1166         }
 1167         /*
 1168          * 2) If target doesn't exist, link the target
 1169          *    to the source and unlink the source.
 1170          *    Otherwise, rewrite the target directory
 1171          *    entry to reference the source inode and
 1172          *    expunge the original entry's existence.
 1173          */
 1174         if (xp == NULL) {
 1175                 if (dp->i_dev != ip->i_dev)
 1176                         panic("ufs_rename: EXDEV");
 1177                 /*
 1178                  * Account for ".." in new directory.
 1179                  * When source and destination have the same
 1180                  * parent we don't fool with the link count.
 1181                  */
 1182                 if (doingdirectory && newparent) {
 1183                         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
 1184                                 error = EMLINK;
 1185                                 goto bad;
 1186                         }
 1187                         dp->i_effnlink++;
 1188                         dp->i_nlink++;
 1189                         DIP_SET(dp, i_nlink, dp->i_nlink);
 1190                         dp->i_flag |= IN_CHANGE;
 1191                         if (DOINGSOFTDEP(tdvp))
 1192                                 softdep_change_linkcnt(dp);
 1193                         error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) |
 1194                                                    DOINGASYNC(tdvp)));
 1195                         if (error)
 1196                                 goto bad;
 1197                 }
 1198                 ufs_makedirentry(ip, tcnp, &newdir);
 1199                 error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL);
 1200                 if (error) {
 1201                         if (doingdirectory && newparent) {
 1202                                 dp->i_effnlink--;
 1203                                 dp->i_nlink--;
 1204                                 DIP_SET(dp, i_nlink, dp->i_nlink);
 1205                                 dp->i_flag |= IN_CHANGE;
 1206                                 if (DOINGSOFTDEP(tdvp))
 1207                                         softdep_change_linkcnt(dp);
 1208                                 (void)UFS_UPDATE(tdvp, 1);
 1209                         }
 1210                         goto bad;
 1211                 }
 1212                 vput(tdvp);
 1213         } else {
 1214                 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
 1215                         panic("ufs_rename: EXDEV");
 1216                 /*
 1217                  * Short circuit rename(foo, foo).
 1218                  */
 1219                 if (xp->i_number == ip->i_number)
 1220                         panic("ufs_rename: same file");
 1221                 /*
 1222                  * If the parent directory is "sticky", then the caller
 1223                  * must possess VADMIN for the parent directory, or the
 1224                  * destination of the rename.  This implements append-only
 1225                  * directories.
 1226                  */
 1227                 if ((dp->i_mode & S_ISTXT) &&
 1228                     VOP_ACCESS(tdvp, VADMIN, tcnp->cn_cred, td) &&
 1229                     VOP_ACCESS(tvp, VADMIN, tcnp->cn_cred, td)) {
 1230                         error = EPERM;
 1231                         goto bad;
 1232                 }
 1233                 /*
 1234                  * Target must be empty if a directory and have no links
 1235                  * to it. Also, ensure source and target are compatible
 1236                  * (both directories, or both not directories).
 1237                  */
 1238                 if ((xp->i_mode&IFMT) == IFDIR) {
 1239                         if ((xp->i_effnlink > 2) ||
 1240                             !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) {
 1241                                 error = ENOTEMPTY;
 1242                                 goto bad;
 1243                         }
 1244                         if (!doingdirectory) {
 1245                                 error = ENOTDIR;
 1246                                 goto bad;
 1247                         }
 1248                         cache_purge(tdvp);
 1249                 } else if (doingdirectory) {
 1250                         error = EISDIR;
 1251                         goto bad;
 1252                 }
 1253                 error = ufs_dirrewrite(dp, xp, ip->i_number,
 1254                     IFTODT(ip->i_mode),
 1255                     (doingdirectory && newparent) ? newparent : doingdirectory);
 1256                 if (error)
 1257                         goto bad;
 1258                 if (doingdirectory) {
 1259                         if (!newparent) {
 1260                                 dp->i_effnlink--;
 1261                                 if (DOINGSOFTDEP(tdvp))
 1262                                         softdep_change_linkcnt(dp);
 1263                         }
 1264                         xp->i_effnlink--;
 1265                         if (DOINGSOFTDEP(tvp))
 1266                                 softdep_change_linkcnt(xp);
 1267                 }
 1268                 if (doingdirectory && !DOINGSOFTDEP(tvp)) {
 1269                         /*
 1270                          * Truncate inode. The only stuff left in the directory
 1271                          * is "." and "..". The "." reference is inconsequential
 1272                          * since we are quashing it. We have removed the "."
 1273                          * reference and the reference in the parent directory,
 1274                          * but there may be other hard links. The soft
 1275                          * dependency code will arrange to do these operations
 1276                          * after the parent directory entry has been deleted on
 1277                          * disk, so when running with that code we avoid doing
 1278                          * them now.
 1279                          */
 1280                         if (!newparent) {
 1281                                 dp->i_nlink--;
 1282                                 DIP_SET(dp, i_nlink, dp->i_nlink);
 1283                                 dp->i_flag |= IN_CHANGE;
 1284                         }
 1285                         xp->i_nlink--;
 1286                         DIP_SET(xp, i_nlink, xp->i_nlink);
 1287                         xp->i_flag |= IN_CHANGE;
 1288                         ioflag = IO_NORMAL;
 1289                         if (!DOINGASYNC(tvp))
 1290                                 ioflag |= IO_SYNC;
 1291                         if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag,
 1292                             tcnp->cn_cred, tcnp->cn_thread)) != 0)
 1293                                 goto bad;
 1294                 }
 1295                 vput(tdvp);
 1296                 vput(tvp);
 1297                 xp = NULL;
 1298         }
 1299 
 1300         /*
 1301          * 3) Unlink the source.
 1302          */
 1303         fcnp->cn_flags &= ~MODMASK;
 1304         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
 1305         if ((fcnp->cn_flags & SAVESTART) == 0)
 1306                 panic("ufs_rename: lost from startdir");
 1307         VREF(fdvp);
 1308         error = relookup(fdvp, &fvp, fcnp);
 1309         if (error == 0)
 1310                 vrele(fdvp);
 1311         if (fvp != NULL) {
 1312                 xp = VTOI(fvp);
 1313                 dp = VTOI(fdvp);
 1314         } else {
 1315                 /*
 1316                  * From name has disappeared.  IN_RENAME is not sufficient
 1317                  * to protect against directory races due to timing windows,
 1318                  * so we have to remove the panic.  XXX the only real way
 1319                  * to solve this issue is at a much higher level.  By the
 1320                  * time we hit ufs_rename() it's too late.
 1321                  */
 1322 #if 0
 1323                 if (doingdirectory)
 1324                         panic("ufs_rename: lost dir entry");
 1325 #endif
 1326                 vrele(ap->a_fvp);
 1327                 return (0);
 1328         }
 1329         /*
 1330          * Ensure that the directory entry still exists and has not
 1331          * changed while the new name has been entered. If the source is
 1332          * a file then the entry may have been unlinked or renamed. In
 1333          * either case there is no further work to be done. If the source
 1334          * is a directory then it cannot have been rmdir'ed; the IN_RENAME
 1335          * flag ensures that it cannot be moved by another rename or removed
 1336          * by a rmdir.
 1337          */
 1338         if (xp != ip) {
 1339                 /*
 1340                  * From name resolves to a different inode.  IN_RENAME is
 1341                  * not sufficient protection against timing window races
 1342                  * so we can't panic here.  XXX the only real way
 1343                  * to solve this issue is at a much higher level.  By the
 1344                  * time we hit ufs_rename() it's too late.
 1345                  */
 1346 #if 0
 1347                 if (doingdirectory)
 1348                         panic("ufs_rename: lost dir entry");
 1349 #endif
 1350         } else {
 1351                 /*
 1352                  * If the source is a directory with a
 1353                  * new parent, the link count of the old
 1354                  * parent directory must be decremented
 1355                  * and ".." set to point to the new parent.
 1356                  */
 1357                 if (doingdirectory && newparent) {
 1358                         xp->i_offset = mastertemplate.dot_reclen;
 1359                         ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0);
 1360                         cache_purge(fdvp);
 1361                 }
 1362                 error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0);
 1363                 xp->i_flag &= ~IN_RENAME;
 1364         }
 1365         if (dp)
 1366                 vput(fdvp);
 1367         if (xp)
 1368                 vput(fvp);
 1369         vrele(ap->a_fvp);
 1370         return (error);
 1371 
 1372 bad:
 1373         if (xp)
 1374                 vput(ITOV(xp));
 1375         vput(ITOV(dp));
 1376 out:
 1377         if (doingdirectory)
 1378                 ip->i_flag &= ~IN_RENAME;
 1379         if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) {
 1380                 ip->i_effnlink--;
 1381                 ip->i_nlink--;
 1382                 DIP_SET(ip, i_nlink, ip->i_nlink);
 1383                 ip->i_flag |= IN_CHANGE;
 1384                 ip->i_flag &= ~IN_RENAME;
 1385                 if (DOINGSOFTDEP(fvp))
 1386                         softdep_change_linkcnt(ip);
 1387                 vput(fvp);
 1388         } else
 1389                 vrele(fvp);
 1390         return (error);
 1391 }
 1392 
 1393 /*
 1394  * Mkdir system call
 1395  */
 1396 static int
 1397 ufs_mkdir(ap)
 1398         struct vop_mkdir_args /* {
 1399                 struct vnode *a_dvp;
 1400                 struct vnode **a_vpp;
 1401                 struct componentname *a_cnp;
 1402                 struct vattr *a_vap;
 1403         } */ *ap;
 1404 {
 1405         struct vnode *dvp = ap->a_dvp;
 1406         struct vattr *vap = ap->a_vap;
 1407         struct componentname *cnp = ap->a_cnp;
 1408         struct inode *ip, *dp;
 1409         struct vnode *tvp;
 1410         struct buf *bp;
 1411         struct dirtemplate dirtemplate, *dtp;
 1412         struct direct newdir;
 1413 #ifdef UFS_ACL
 1414         struct acl *acl, *dacl;
 1415 #endif
 1416         int error, dmode;
 1417         long blkoff;
 1418 
 1419 #ifdef INVARIANTS
 1420         if ((cnp->cn_flags & HASBUF) == 0)
 1421                 panic("ufs_mkdir: no name");
 1422 #endif
 1423         dp = VTOI(dvp);
 1424         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
 1425                 error = EMLINK;
 1426                 goto out;
 1427         }
 1428         dmode = vap->va_mode & 0777;
 1429         dmode |= IFDIR;
 1430         /*
 1431          * Must simulate part of ufs_makeinode here to acquire the inode,
 1432          * but not have it entered in the parent directory. The entry is
 1433          * made later after writing "." and ".." entries.
 1434          */
 1435         error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
 1436         if (error)
 1437                 goto out;
 1438         ip = VTOI(tvp);
 1439         ip->i_gid = dp->i_gid;
 1440         DIP_SET(ip, i_gid, dp->i_gid);
 1441 #ifdef SUIDDIR
 1442         {
 1443 #ifdef QUOTA
 1444                 struct ucred ucred, *ucp;
 1445                 ucp = cnp->cn_cred;
 1446 #endif
 1447                 /*
 1448                  * If we are hacking owners here, (only do this where told to)
 1449                  * and we are not giving it TO root, (would subvert quotas)
 1450                  * then go ahead and give it to the other user.
 1451                  * The new directory also inherits the SUID bit.
 1452                  * If user's UID and dir UID are the same,
 1453                  * 'give it away' so that the SUID is still forced on.
 1454                  */
 1455                 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
 1456                     (dp->i_mode & ISUID) && dp->i_uid) {
 1457                         dmode |= ISUID;
 1458                         ip->i_uid = dp->i_uid;
 1459                         DIP_SET(ip, i_uid, dp->i_uid);
 1460 #ifdef QUOTA
 1461                         if (dp->i_uid != cnp->cn_cred->cr_uid) {
 1462                                 /*
 1463                                  * Make sure the correct user gets charged
 1464                                  * for the space.
 1465                                  * Make a dummy credential for the victim.
 1466                                  * XXX This seems to never be accessed out of
 1467                                  * our context so a stack variable is ok.
 1468                                  */
 1469                                 refcount_init(&ucred.cr_ref, 1);
 1470                                 ucred.cr_uid = ip->i_uid;
 1471                                 ucred.cr_ngroups = 1;
 1472                                 ucred.cr_groups[0] = dp->i_gid;
 1473                                 ucp = &ucred;
 1474                         }
 1475 #endif
 1476                 } else {
 1477                         ip->i_uid = cnp->cn_cred->cr_uid;
 1478                         DIP_SET(ip, i_uid, ip->i_uid);
 1479                 }
 1480 #ifdef QUOTA
 1481                 if ((error = getinoquota(ip)) ||
 1482                     (error = chkiq(ip, 1, ucp, 0))) {
 1483                         UFS_VFREE(tvp, ip->i_number, dmode);
 1484                         vput(tvp);
 1485                         return (error);
 1486                 }
 1487 #endif
 1488         }
 1489 #else   /* !SUIDDIR */
 1490         ip->i_uid = cnp->cn_cred->cr_uid;
 1491         DIP_SET(ip, i_uid, ip->i_uid);
 1492 #ifdef QUOTA
 1493         if ((error = getinoquota(ip)) ||
 1494             (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
 1495                 UFS_VFREE(tvp, ip->i_number, dmode);
 1496                 vput(tvp);
 1497                 return (error);
 1498         }
 1499 #endif
 1500 #endif  /* !SUIDDIR */
 1501         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
 1502 #ifdef UFS_ACL
 1503         acl = dacl = NULL;
 1504         if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
 1505                 acl = uma_zalloc(acl_zone, M_WAITOK);
 1506                 dacl = uma_zalloc(acl_zone, M_WAITOK);
 1507 
 1508                 /*
 1509                  * Retrieve default ACL from parent, if any.
 1510                  */
 1511                 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
 1512                     cnp->cn_thread);
 1513                 switch (error) {
 1514                 case 0:
 1515                         /*
 1516                          * Retrieved a default ACL, so merge mode and ACL if
 1517                          * necessary.  If the ACL is empty, fall through to
 1518                          * the "not defined or available" case.
 1519                          */
 1520                         if (acl->acl_cnt != 0) {
 1521                                 dmode = acl_posix1e_newfilemode(dmode, acl);
 1522                                 ip->i_mode = dmode;
 1523                                 DIP_SET(ip, i_mode, dmode);
 1524                                 *dacl = *acl;
 1525                                 ufs_sync_acl_from_inode(ip, acl);
 1526                                 break;
 1527                         }
 1528                         /* FALLTHROUGH */
 1529         
 1530                 case EOPNOTSUPP:
 1531                         /*
 1532                          * Just use the mode as-is.
 1533                          */
 1534                         ip->i_mode = dmode;
 1535                         DIP_SET(ip, i_mode, dmode);
 1536                         uma_zfree(acl_zone, acl);
 1537                         uma_zfree(acl_zone, dacl);
 1538                         dacl = acl = NULL;
 1539                         break;
 1540                 
 1541                 default:
 1542                         UFS_VFREE(tvp, ip->i_number, dmode);
 1543                         vput(tvp);
 1544                         uma_zfree(acl_zone, acl);
 1545                         uma_zfree(acl_zone, dacl);
 1546                         return (error);
 1547                 }
 1548         } else {
 1549 #endif /* !UFS_ACL */
 1550                 ip->i_mode = dmode;
 1551                 DIP_SET(ip, i_mode, dmode);
 1552 #ifdef UFS_ACL
 1553         }
 1554 #endif
 1555         tvp->v_type = VDIR;     /* Rest init'd in getnewvnode(). */
 1556         ip->i_effnlink = 2;
 1557         ip->i_nlink = 2;
 1558         DIP_SET(ip, i_nlink, 2);
 1559         if (DOINGSOFTDEP(tvp))
 1560                 softdep_change_linkcnt(ip);
 1561         if (cnp->cn_flags & ISWHITEOUT) {
 1562                 ip->i_flags |= UF_OPAQUE;
 1563                 DIP_SET(ip, i_flags, ip->i_flags);
 1564         }
 1565 
 1566         /*
 1567          * Bump link count in parent directory to reflect work done below.
 1568          * Should be done before reference is created so cleanup is
 1569          * possible if we crash.
 1570          */
 1571         dp->i_effnlink++;
 1572         dp->i_nlink++;
 1573         DIP_SET(dp, i_nlink, dp->i_nlink);
 1574         dp->i_flag |= IN_CHANGE;
 1575         if (DOINGSOFTDEP(dvp))
 1576                 softdep_change_linkcnt(dp);
 1577         error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp)));
 1578         if (error)
 1579                 goto bad;
 1580 #ifdef MAC
 1581         if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
 1582                 error = mac_create_vnode_extattr(cnp->cn_cred, dvp->v_mount,
 1583                     dvp, tvp, cnp);
 1584                 if (error)
 1585                         goto bad;
 1586         }
 1587 #endif
 1588 #ifdef UFS_ACL
 1589         if (acl != NULL) {
 1590                 /*
 1591                  * XXX: If we abort now, will Soft Updates notify the extattr
 1592                  * code that the EAs for the file need to be released?
 1593                  */
 1594                 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
 1595                     cnp->cn_thread);
 1596                 if (error == 0)
 1597                         error = VOP_SETACL(tvp, ACL_TYPE_DEFAULT, dacl,
 1598                             cnp->cn_cred, cnp->cn_thread);
 1599                 switch (error) {
 1600                 case 0:
 1601                         break;
 1602 
 1603                 case EOPNOTSUPP:
 1604                         /*
 1605                          * XXX: This should not happen, as EOPNOTSUPP above
 1606                          * was supposed to free acl.
 1607                          */
 1608                         printf("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()\n");
 1609                         /*
 1610                         panic("ufs_mkdir: VOP_GETACL() but no VOP_SETACL()");
 1611                          */
 1612                         break;
 1613 
 1614                 default:
 1615                         uma_zfree(acl_zone, acl);
 1616                         uma_zfree(acl_zone, dacl);
 1617                         dacl = acl = NULL;
 1618                         goto bad;
 1619                 }
 1620                 uma_zfree(acl_zone, acl);
 1621                 uma_zfree(acl_zone, dacl);
 1622                 dacl = acl = NULL;
 1623         }
 1624 #endif /* !UFS_ACL */
 1625 
 1626         /*
 1627          * Initialize directory with "." and ".." from static template.
 1628          */
 1629         if (dvp->v_mount->mnt_maxsymlinklen > 0)
 1630                 dtp = &mastertemplate;
 1631         else
 1632                 dtp = (struct dirtemplate *)&omastertemplate;
 1633         dirtemplate = *dtp;
 1634         dirtemplate.dot_ino = ip->i_number;
 1635         dirtemplate.dotdot_ino = dp->i_number;
 1636         if ((error = UFS_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred,
 1637             BA_CLRBUF, &bp)) != 0)
 1638                 goto bad;
 1639         ip->i_size = DIRBLKSIZ;
 1640         DIP_SET(ip, i_size, DIRBLKSIZ);
 1641         ip->i_flag |= IN_CHANGE | IN_UPDATE;
 1642         vnode_pager_setsize(tvp, (u_long)ip->i_size);
 1643         bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate);
 1644         if (DOINGSOFTDEP(tvp)) {
 1645                 /*
 1646                  * Ensure that the entire newly allocated block is a
 1647                  * valid directory so that future growth within the
 1648                  * block does not have to ensure that the block is
 1649                  * written before the inode.
 1650                  */
 1651                 blkoff = DIRBLKSIZ;
 1652                 while (blkoff < bp->b_bcount) {
 1653                         ((struct direct *)
 1654                            (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
 1655                         blkoff += DIRBLKSIZ;
 1656                 }
 1657         }
 1658         if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) |
 1659                                        DOINGASYNC(tvp)))) != 0) {
 1660                 (void)bwrite(bp);
 1661                 goto bad;
 1662         }
 1663         /*
 1664          * Directory set up, now install its entry in the parent directory.
 1665          *
 1666          * If we are not doing soft dependencies, then we must write out the
 1667          * buffer containing the new directory body before entering the new 
 1668          * name in the parent. If we are doing soft dependencies, then the
 1669          * buffer containing the new directory body will be passed to and
 1670          * released in the soft dependency code after the code has attached
 1671          * an appropriate ordering dependency to the buffer which ensures that
 1672          * the buffer is written before the new name is written in the parent.
 1673          */
 1674         if (DOINGASYNC(dvp))
 1675                 bdwrite(bp);
 1676         else if (!DOINGSOFTDEP(dvp) && ((error = bwrite(bp))))
 1677                 goto bad;
 1678         ufs_makedirentry(ip, cnp, &newdir);
 1679         error = ufs_direnter(dvp, tvp, &newdir, cnp, bp);
 1680         
 1681 bad:
 1682         if (error == 0) {
 1683                 *ap->a_vpp = tvp;
 1684         } else {
 1685 #ifdef UFS_ACL
 1686                 if (acl != NULL)
 1687                         uma_zfree(acl_zone, acl);
 1688                 if (dacl != NULL)
 1689                         uma_zfree(acl_zone, dacl);
 1690 #endif
 1691                 dp->i_effnlink--;
 1692                 dp->i_nlink--;
 1693                 DIP_SET(dp, i_nlink, dp->i_nlink);
 1694                 dp->i_flag |= IN_CHANGE;
 1695                 if (DOINGSOFTDEP(dvp))
 1696                         softdep_change_linkcnt(dp);
 1697                 /*
 1698                  * No need to do an explicit VOP_TRUNCATE here, vrele will
 1699                  * do this for us because we set the link count to 0.
 1700                  */
 1701                 ip->i_effnlink = 0;
 1702                 ip->i_nlink = 0;
 1703                 DIP_SET(ip, i_nlink, 0);
 1704                 ip->i_flag |= IN_CHANGE;
 1705                 if (DOINGSOFTDEP(tvp))
 1706                         softdep_change_linkcnt(ip);
 1707                 vput(tvp);
 1708         }
 1709 out:
 1710         return (error);
 1711 }
 1712 
 1713 /*
 1714  * Rmdir system call.
 1715  */
 1716 static int
 1717 ufs_rmdir(ap)
 1718         struct vop_rmdir_args /* {
 1719                 struct vnode *a_dvp;
 1720                 struct vnode *a_vp;
 1721                 struct componentname *a_cnp;
 1722         } */ *ap;
 1723 {
 1724         struct vnode *vp = ap->a_vp;
 1725         struct vnode *dvp = ap->a_dvp;
 1726         struct componentname *cnp = ap->a_cnp;
 1727         struct inode *ip, *dp;
 1728         int error, ioflag;
 1729 
 1730         ip = VTOI(vp);
 1731         dp = VTOI(dvp);
 1732 
 1733         /*
 1734          * Do not remove a directory that is in the process of being renamed.
 1735          * Verify the directory is empty (and valid). Rmdir ".." will not be
 1736          * valid since ".." will contain a reference to the current directory
 1737          * and thus be non-empty. Do not allow the removal of mounted on
 1738          * directories (this can happen when an NFS exported filesystem
 1739          * tries to remove a locally mounted on directory).
 1740          */
 1741         error = 0;
 1742         if ((ip->i_flag & IN_RENAME) || ip->i_effnlink < 2) {
 1743                 error = EINVAL;
 1744                 goto out;
 1745         }
 1746         if (!ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
 1747                 error = ENOTEMPTY;
 1748                 goto out;
 1749         }
 1750         if ((dp->i_flags & APPEND)
 1751             || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
 1752                 error = EPERM;
 1753                 goto out;
 1754         }
 1755         if (vp->v_mountedhere != 0) {
 1756                 error = EINVAL;
 1757                 goto out;
 1758         }
 1759 #ifdef UFS_GJOURNAL
 1760         ufs_gjournal_orphan(vp);
 1761 #endif
 1762         /*
 1763          * Delete reference to directory before purging
 1764          * inode.  If we crash in between, the directory
 1765          * will be reattached to lost+found,
 1766          */
 1767         dp->i_effnlink--;
 1768         ip->i_effnlink--;
 1769         if (DOINGSOFTDEP(vp)) {
 1770                 softdep_change_linkcnt(dp);
 1771                 softdep_change_linkcnt(ip);
 1772         }
 1773         error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1);
 1774         if (error) {
 1775                 dp->i_effnlink++;
 1776                 ip->i_effnlink++;
 1777                 if (DOINGSOFTDEP(vp)) {
 1778                         softdep_change_linkcnt(dp);
 1779                         softdep_change_linkcnt(ip);
 1780                 }
 1781                 goto out;
 1782         }
 1783         cache_purge(dvp);
 1784         /*
 1785          * Truncate inode. The only stuff left in the directory is "." and
 1786          * "..". The "." reference is inconsequential since we are quashing
 1787          * it. The soft dependency code will arrange to do these operations
 1788          * after the parent directory entry has been deleted on disk, so
 1789          * when running with that code we avoid doing them now.
 1790          */
 1791         if (!DOINGSOFTDEP(vp)) {
 1792                 dp->i_nlink--;
 1793                 DIP_SET(dp, i_nlink, dp->i_nlink);
 1794                 dp->i_flag |= IN_CHANGE;
 1795                 ip->i_nlink--;
 1796                 DIP_SET(ip, i_nlink, ip->i_nlink);
 1797                 ip->i_flag |= IN_CHANGE;
 1798                 ioflag = IO_NORMAL;
 1799                 if (!DOINGASYNC(vp))
 1800                         ioflag |= IO_SYNC;
 1801                 error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred,
 1802                     cnp->cn_thread);
 1803         }
 1804         cache_purge(vp);
 1805 #ifdef UFS_DIRHASH
 1806         /* Kill any active hash; i_effnlink == 0, so it will not come back. */
 1807         if (ip->i_dirhash != NULL)
 1808                 ufsdirhash_free(ip);
 1809 #endif
 1810 out:
 1811         return (error);
 1812 }
 1813 
 1814 /*
 1815  * symlink -- make a symbolic link
 1816  */
 1817 static int
 1818 ufs_symlink(ap)
 1819         struct vop_symlink_args /* {
 1820                 struct vnode *a_dvp;
 1821                 struct vnode **a_vpp;
 1822                 struct componentname *a_cnp;
 1823                 struct vattr *a_vap;
 1824                 char *a_target;
 1825         } */ *ap;
 1826 {
 1827         struct vnode *vp, **vpp = ap->a_vpp;
 1828         struct inode *ip;
 1829         int len, error;
 1830 
 1831         error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
 1832             vpp, ap->a_cnp);
 1833         if (error)
 1834                 return (error);
 1835         vp = *vpp;
 1836         len = strlen(ap->a_target);
 1837         if (len < vp->v_mount->mnt_maxsymlinklen) {
 1838                 ip = VTOI(vp);
 1839                 bcopy(ap->a_target, SHORTLINK(ip), len);
 1840                 ip->i_size = len;
 1841                 DIP_SET(ip, i_size, len);
 1842                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
 1843         } else
 1844                 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
 1845                     UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
 1846                     ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
 1847         if (error)
 1848                 vput(vp);
 1849         return (error);
 1850 }
 1851 
 1852 /*
 1853  * Vnode op for reading directories.
 1854  *
 1855  * The routine below assumes that the on-disk format of a directory
 1856  * is the same as that defined by <sys/dirent.h>. If the on-disk
 1857  * format changes, then it will be necessary to do a conversion
 1858  * from the on-disk format that read returns to the format defined
 1859  * by <sys/dirent.h>.
 1860  */
 1861 int
 1862 ufs_readdir(ap)
 1863         struct vop_readdir_args /* {
 1864                 struct vnode *a_vp;
 1865                 struct uio *a_uio;
 1866                 struct ucred *a_cred;
 1867                 int *a_eofflag;
 1868                 int *a_ncookies;
 1869                 u_long **a_cookies;
 1870         } */ *ap;
 1871 {
 1872         struct uio *uio = ap->a_uio;
 1873         int error;
 1874         size_t count, lost;
 1875         off_t off;
 1876 
 1877         if (ap->a_ncookies != NULL)
 1878                 /*
 1879                  * Ensure that the block is aligned.  The caller can use
 1880                  * the cookies to determine where in the block to start.
 1881                  */
 1882                 uio->uio_offset &= ~(DIRBLKSIZ - 1);
 1883         off = uio->uio_offset;
 1884         count = uio->uio_resid;
 1885         /* Make sure we don't return partial entries. */
 1886         if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1)))
 1887                 return (EINVAL);
 1888         count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
 1889         lost = uio->uio_resid - count;
 1890         uio->uio_resid = count;
 1891         uio->uio_iov->iov_len = count;
 1892 #       if (BYTE_ORDER == LITTLE_ENDIAN)
 1893                 if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) {
 1894                         error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
 1895                 } else {
 1896                         struct dirent *dp, *edp;
 1897                         struct uio auio;
 1898                         struct iovec aiov;
 1899                         caddr_t dirbuf;
 1900                         int readcnt;
 1901                         u_char tmp;
 1902 
 1903                         auio = *uio;
 1904                         auio.uio_iov = &aiov;
 1905                         auio.uio_iovcnt = 1;
 1906                         auio.uio_segflg = UIO_SYSSPACE;
 1907                         aiov.iov_len = count;
 1908                         MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
 1909                         aiov.iov_base = dirbuf;
 1910                         error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
 1911                         if (error == 0) {
 1912                                 readcnt = count - auio.uio_resid;
 1913                                 edp = (struct dirent *)&dirbuf[readcnt];
 1914                                 for (dp = (struct dirent *)dirbuf; dp < edp; ) {
 1915                                         tmp = dp->d_namlen;
 1916                                         dp->d_namlen = dp->d_type;
 1917                                         dp->d_type = tmp;
 1918                                         if (dp->d_reclen > 0) {
 1919                                                 dp = (struct dirent *)
 1920                                                     ((char *)dp + dp->d_reclen);
 1921                                         } else {
 1922                                                 error = EIO;
 1923                                                 break;
 1924                                         }
 1925                                 }
 1926                                 if (dp >= edp)
 1927                                         error = uiomove(dirbuf, readcnt, uio);
 1928                         }
 1929                         FREE(dirbuf, M_TEMP);
 1930                 }
 1931 #       else
 1932                 error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred);
 1933 #       endif
 1934         if (!error && ap->a_ncookies != NULL) {
 1935                 struct dirent* dpStart;
 1936                 struct dirent* dpEnd;
 1937                 struct dirent* dp;
 1938                 int ncookies;
 1939                 u_long *cookies;
 1940                 u_long *cookiep;
 1941 
 1942                 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
 1943                         panic("ufs_readdir: unexpected uio from NFS server");
 1944                 dpStart = (struct dirent *)
 1945                     ((char *)uio->uio_iov->iov_base - (uio->uio_offset - off));
 1946                 dpEnd = (struct dirent *) uio->uio_iov->iov_base;
 1947                 for (dp = dpStart, ncookies = 0;
 1948                      dp < dpEnd;
 1949                      dp = (struct dirent *)((caddr_t) dp + dp->d_reclen))
 1950                         ncookies++;
 1951                 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
 1952                     M_WAITOK);
 1953                 for (dp = dpStart, cookiep = cookies;
 1954                      dp < dpEnd;
 1955                      dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) {
 1956                         off += dp->d_reclen;
 1957                         *cookiep++ = (u_long) off;
 1958                 }
 1959                 *ap->a_ncookies = ncookies;
 1960                 *ap->a_cookies = cookies;
 1961         }
 1962         uio->uio_resid += lost;
 1963         if (ap->a_eofflag)
 1964             *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
 1965         return (error);
 1966 }
 1967 
 1968 /*
 1969  * Return target name of a symbolic link
 1970  */
 1971 static int
 1972 ufs_readlink(ap)
 1973         struct vop_readlink_args /* {
 1974                 struct vnode *a_vp;
 1975                 struct uio *a_uio;
 1976                 struct ucred *a_cred;
 1977         } */ *ap;
 1978 {
 1979         struct vnode *vp = ap->a_vp;
 1980         struct inode *ip = VTOI(vp);
 1981         doff_t isize;
 1982 
 1983         isize = ip->i_size;
 1984         if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
 1985             DIP(ip, i_blocks) == 0) { /* XXX - for old fastlink support */
 1986                 return (uiomove(SHORTLINK(ip), isize, ap->a_uio));
 1987         }
 1988         return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
 1989 }
 1990 
 1991 /*
 1992  * Calculate the logical to physical mapping if not done already,
 1993  * then call the device strategy routine.
 1994  *
 1995  * In order to be able to swap to a file, the ufs_bmaparray() operation may not
 1996  * deadlock on memory.  See ufs_bmap() for details.
 1997  */
 1998 static int
 1999 ufs_strategy(ap)
 2000         struct vop_strategy_args /* {
 2001                 struct vnode *a_vp;
 2002                 struct buf *a_bp;
 2003         } */ *ap;
 2004 {
 2005         struct buf *bp = ap->a_bp;
 2006         struct vnode *vp = ap->a_vp;
 2007         struct bufobj *bo;
 2008         struct inode *ip;
 2009         ufs2_daddr_t blkno;
 2010         int error;
 2011 
 2012         ip = VTOI(vp);
 2013         if (bp->b_blkno == bp->b_lblkno) {
 2014                 error = ufs_bmaparray(vp, bp->b_lblkno, &blkno, bp, NULL, NULL);
 2015                 bp->b_blkno = blkno;
 2016                 if (error) {
 2017                         bp->b_error = error;
 2018                         bp->b_ioflags |= BIO_ERROR;
 2019                         bufdone(bp);
 2020                         return (0);
 2021                 }
 2022                 if ((long)bp->b_blkno == -1)
 2023                         vfs_bio_clrbuf(bp);
 2024         }
 2025         if ((long)bp->b_blkno == -1) {
 2026                 bufdone(bp);
 2027                 return (0);
 2028         }
 2029         bp->b_iooffset = dbtob(bp->b_blkno);
 2030         bo = ip->i_umbufobj;
 2031         BO_STRATEGY(bo, bp);
 2032         return (0);
 2033 }
 2034 
 2035 /*
 2036  * Print out the contents of an inode.
 2037  */
 2038 static int
 2039 ufs_print(ap)
 2040         struct vop_print_args /* {
 2041                 struct vnode *a_vp;
 2042         } */ *ap;
 2043 {
 2044         struct vnode *vp = ap->a_vp;
 2045         struct inode *ip = VTOI(vp);
 2046 
 2047         printf("\tino %lu, on dev %s", (u_long)ip->i_number,
 2048             devtoname(ip->i_dev));
 2049         if (vp->v_type == VFIFO)
 2050                 fifo_printinfo(vp);
 2051         printf("\n");
 2052         return (0);
 2053 }
 2054 
 2055 /*
 2056  * Close wrapper for fifos.
 2057  *
 2058  * Update the times on the inode then do device close.
 2059  */
 2060 static int
 2061 ufsfifo_close(ap)
 2062         struct vop_close_args /* {
 2063                 struct vnode *a_vp;
 2064                 int  a_fflag;
 2065                 struct ucred *a_cred;
 2066                 struct thread *a_td;
 2067         } */ *ap;
 2068 {
 2069         struct vnode *vp = ap->a_vp;
 2070         int usecount;
 2071 
 2072         VI_LOCK(vp);
 2073         usecount = vp->v_usecount;
 2074         if (usecount > 1)
 2075                 ufs_itimes_locked(vp);
 2076         VI_UNLOCK(vp);
 2077         return (fifo_specops.vop_close(ap));
 2078 }
 2079 
 2080 /*
 2081  * Kqfilter wrapper for fifos.
 2082  *
 2083  * Fall through to ufs kqfilter routines if needed 
 2084  */
 2085 static int
 2086 ufsfifo_kqfilter(ap)
 2087         struct vop_kqfilter_args *ap;
 2088 {
 2089         int error;
 2090 
 2091         error = fifo_specops.vop_kqfilter(ap);
 2092         if (error)
 2093                 error = vfs_kqfilter(ap);
 2094         return (error);
 2095 }
 2096 
 2097 /*
 2098  * Return POSIX pathconf information applicable to ufs filesystems.
 2099  */
 2100 static int
 2101 ufs_pathconf(ap)
 2102         struct vop_pathconf_args /* {
 2103                 struct vnode *a_vp;
 2104                 int a_name;
 2105                 int *a_retval;
 2106         } */ *ap;
 2107 {
 2108         int error;
 2109 
 2110         error = 0;
 2111         switch (ap->a_name) {
 2112         case _PC_LINK_MAX:
 2113                 *ap->a_retval = LINK_MAX;
 2114                 break;
 2115         case _PC_NAME_MAX:
 2116                 *ap->a_retval = NAME_MAX;
 2117                 break;
 2118         case _PC_PATH_MAX:
 2119                 *ap->a_retval = PATH_MAX;
 2120                 break;
 2121         case _PC_PIPE_BUF:
 2122                 *ap->a_retval = PIPE_BUF;
 2123                 break;
 2124         case _PC_CHOWN_RESTRICTED:
 2125                 *ap->a_retval = 1;
 2126                 break;
 2127         case _PC_NO_TRUNC:
 2128                 *ap->a_retval = 1;
 2129                 break;
 2130         case _PC_ACL_EXTENDED:
 2131 #ifdef UFS_ACL
 2132                 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
 2133                         *ap->a_retval = 1;
 2134                 else
 2135                         *ap->a_retval = 0;
 2136 #else
 2137                 *ap->a_retval = 0;
 2138 #endif
 2139                 break;
 2140         case _PC_ACL_PATH_MAX:
 2141 #ifdef UFS_ACL
 2142                 if (ap->a_vp->v_mount->mnt_flag & MNT_ACLS)
 2143                         *ap->a_retval = ACL_MAX_ENTRIES;
 2144                 else
 2145                         *ap->a_retval = 3;
 2146 #else
 2147                 *ap->a_retval = 3;
 2148 #endif
 2149                 break;
 2150         case _PC_MAC_PRESENT:
 2151 #ifdef MAC
 2152                 if (ap->a_vp->v_mount->mnt_flag & MNT_MULTILABEL)
 2153                         *ap->a_retval = 1;
 2154                 else
 2155                         *ap->a_retval = 0;
 2156 #else
 2157                 *ap->a_retval = 0;
 2158 #endif
 2159                 break;
 2160         case _PC_ASYNC_IO:
 2161                 /* _PC_ASYNC_IO should have been handled by upper layers. */
 2162                 KASSERT(0, ("_PC_ASYNC_IO should not get here"));
 2163                 error = EINVAL;
 2164                 break;
 2165         case _PC_PRIO_IO:
 2166                 *ap->a_retval = 0;
 2167                 break;
 2168         case _PC_SYNC_IO:
 2169                 *ap->a_retval = 0;
 2170                 break;
 2171         case _PC_ALLOC_SIZE_MIN:
 2172                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
 2173                 break;
 2174         case _PC_FILESIZEBITS:
 2175                 *ap->a_retval = 64;
 2176                 break;
 2177         case _PC_REC_INCR_XFER_SIZE:
 2178                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
 2179                 break;
 2180         case _PC_REC_MAX_XFER_SIZE:
 2181                 *ap->a_retval = -1; /* means ``unlimited'' */
 2182                 break;
 2183         case _PC_REC_MIN_XFER_SIZE:
 2184                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
 2185                 break;
 2186         case _PC_REC_XFER_ALIGN:
 2187                 *ap->a_retval = PAGE_SIZE;
 2188                 break;
 2189         case _PC_SYMLINK_MAX:
 2190                 *ap->a_retval = MAXPATHLEN;
 2191                 break;
 2192 
 2193         default:
 2194                 error = EINVAL;
 2195                 break;
 2196         }
 2197         return (error);
 2198 }
 2199 
 2200 /*
 2201  * Initialize the vnode associated with a new inode, handle aliased
 2202  * vnodes.
 2203  */
 2204 int
 2205 ufs_vinit(mntp, fifoops, vpp)
 2206         struct mount *mntp;
 2207         struct vop_vector *fifoops;
 2208         struct vnode **vpp;
 2209 {
 2210         struct inode *ip;
 2211         struct vnode *vp;
 2212 
 2213         vp = *vpp;
 2214         ip = VTOI(vp);
 2215         vp->v_type = IFTOVT(ip->i_mode);
 2216         if (vp->v_type == VFIFO)
 2217                 vp->v_op = fifoops;
 2218         ASSERT_VOP_LOCKED(vp, "ufs_vinit");
 2219         if (ip->i_number == ROOTINO)
 2220                 vp->v_vflag |= VV_ROOT;
 2221         ip->i_modrev = init_va_filerev();
 2222         *vpp = vp;
 2223         return (0);
 2224 }
 2225 
 2226 /*
 2227  * Allocate a new inode.
 2228  * Vnode dvp must be locked.
 2229  */
 2230 static int
 2231 ufs_makeinode(mode, dvp, vpp, cnp)
 2232         int mode;
 2233         struct vnode *dvp;
 2234         struct vnode **vpp;
 2235         struct componentname *cnp;
 2236 {
 2237         struct inode *ip, *pdir;
 2238         struct direct newdir;
 2239         struct vnode *tvp;
 2240 #ifdef UFS_ACL
 2241         struct acl *acl;
 2242 #endif
 2243         int error;
 2244 
 2245         pdir = VTOI(dvp);
 2246 #ifdef INVARIANTS
 2247         if ((cnp->cn_flags & HASBUF) == 0)
 2248                 panic("ufs_makeinode: no name");
 2249 #endif
 2250         *vpp = NULL;
 2251         if ((mode & IFMT) == 0)
 2252                 mode |= IFREG;
 2253 
 2254         error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
 2255         if (error)
 2256                 return (error);
 2257         ip = VTOI(tvp);
 2258         ip->i_gid = pdir->i_gid;
 2259         DIP_SET(ip, i_gid, pdir->i_gid);
 2260 #ifdef SUIDDIR
 2261         {
 2262 #ifdef QUOTA
 2263                 struct ucred ucred, *ucp;
 2264                 ucp = cnp->cn_cred;
 2265 #endif
 2266                 /*
 2267                  * If we are not the owner of the directory,
 2268                  * and we are hacking owners here, (only do this where told to)
 2269                  * and we are not giving it TO root, (would subvert quotas)
 2270                  * then go ahead and give it to the other user.
 2271                  * Note that this drops off the execute bits for security.
 2272                  */
 2273                 if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
 2274                     (pdir->i_mode & ISUID) &&
 2275                     (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
 2276                         ip->i_uid = pdir->i_uid;
 2277                         DIP_SET(ip, i_uid, ip->i_uid);
 2278                         mode &= ~07111;
 2279 #ifdef QUOTA
 2280                         /*
 2281                          * Make sure the correct user gets charged
 2282                          * for the space.
 2283                          * Quickly knock up a dummy credential for the victim.
 2284                          * XXX This seems to never be accessed out of our
 2285                          * context so a stack variable is ok.
 2286                          */
 2287                         refcount_init(&ucred.cr_ref, 1);
 2288                         ucred.cr_uid = ip->i_uid;
 2289                         ucred.cr_ngroups = 1;
 2290                         ucred.cr_groups[0] = pdir->i_gid;
 2291                         ucp = &ucred;
 2292 #endif
 2293                 } else {
 2294                         ip->i_uid = cnp->cn_cred->cr_uid;
 2295                         DIP_SET(ip, i_uid, ip->i_uid);
 2296                 }
 2297 
 2298 #ifdef QUOTA
 2299                 if ((error = getinoquota(ip)) ||
 2300                     (error = chkiq(ip, 1, ucp, 0))) {
 2301                         UFS_VFREE(tvp, ip->i_number, mode);
 2302                         vput(tvp);
 2303                         return (error);
 2304                 }
 2305 #endif
 2306         }
 2307 #else   /* !SUIDDIR */
 2308         ip->i_uid = cnp->cn_cred->cr_uid;
 2309         DIP_SET(ip, i_uid, ip->i_uid);
 2310 #ifdef QUOTA
 2311         if ((error = getinoquota(ip)) ||
 2312             (error = chkiq(ip, 1, cnp->cn_cred, 0))) {
 2313                 UFS_VFREE(tvp, ip->i_number, mode);
 2314                 vput(tvp);
 2315                 return (error);
 2316         }
 2317 #endif
 2318 #endif  /* !SUIDDIR */
 2319         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
 2320 #ifdef UFS_ACL
 2321         acl = NULL;
 2322         if ((dvp->v_mount->mnt_flag & MNT_ACLS) != 0) {
 2323                 acl = uma_zalloc(acl_zone, M_WAITOK);
 2324 
 2325                 /*
 2326                  * Retrieve default ACL for parent, if any.
 2327                  */
 2328                 error = VOP_GETACL(dvp, ACL_TYPE_DEFAULT, acl, cnp->cn_cred,
 2329                     cnp->cn_thread);
 2330                 switch (error) {
 2331                 case 0:
 2332                         /*
 2333                          * Retrieved a default ACL, so merge mode and ACL if
 2334                          * necessary.
 2335                          */
 2336                         if (acl->acl_cnt != 0) {
 2337                                 /*
 2338                                  * Two possible ways for default ACL to not
 2339                                  * be present.  First, the EA can be
 2340                                  * undefined, or second, the default ACL can
 2341                                  * be blank.  If it's blank, fall through to
 2342                                  * the it's not defined case.
 2343                                  */
 2344                                 mode = acl_posix1e_newfilemode(mode, acl);
 2345                                 ip->i_mode = mode;
 2346                                 DIP_SET(ip, i_mode, mode);
 2347                                 ufs_sync_acl_from_inode(ip, acl);
 2348                                 break;
 2349                         }
 2350                         /* FALLTHROUGH */
 2351         
 2352                 case EOPNOTSUPP:
 2353                         /*
 2354                          * Just use the mode as-is.
 2355                          */
 2356                         ip->i_mode = mode;
 2357                         DIP_SET(ip, i_mode, mode);
 2358                         uma_zfree(acl_zone, acl);
 2359                         acl = NULL;
 2360                         break;
 2361         
 2362                 default:
 2363                         UFS_VFREE(tvp, ip->i_number, mode);
 2364                         vput(tvp);
 2365                         uma_zfree(acl_zone, acl);
 2366                         acl = NULL;
 2367                         return (error);
 2368                 }
 2369         } else {
 2370 #endif
 2371                 ip->i_mode = mode;
 2372                 DIP_SET(ip, i_mode, mode);
 2373 #ifdef UFS_ACL
 2374         }
 2375 #endif
 2376         tvp->v_type = IFTOVT(mode);     /* Rest init'd in getnewvnode(). */
 2377         ip->i_effnlink = 1;
 2378         ip->i_nlink = 1;
 2379         DIP_SET(ip, i_nlink, 1);
 2380         if (DOINGSOFTDEP(tvp))
 2381                 softdep_change_linkcnt(ip);
 2382         if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
 2383             priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID, 0)) {
 2384                 ip->i_mode &= ~ISGID;
 2385                 DIP_SET(ip, i_mode, ip->i_mode);
 2386         }
 2387 
 2388         if (cnp->cn_flags & ISWHITEOUT) {
 2389                 ip->i_flags |= UF_OPAQUE;
 2390                 DIP_SET(ip, i_flags, ip->i_flags);
 2391         }
 2392 
 2393         /*
 2394          * Make sure inode goes to disk before directory entry.
 2395          */
 2396         error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp)));
 2397         if (error)
 2398                 goto bad;
 2399 #ifdef MAC
 2400         if (dvp->v_mount->mnt_flag & MNT_MULTILABEL) {
 2401                 error = mac_create_vnode_extattr(cnp->cn_cred, dvp->v_mount,
 2402                     dvp, tvp, cnp);
 2403                 if (error)
 2404                         goto bad;
 2405         }
 2406 #endif
 2407 #ifdef UFS_ACL
 2408         if (acl != NULL) {
 2409                 /*
 2410                  * XXX: If we abort now, will Soft Updates notify the extattr
 2411                  * code that the EAs for the file need to be released?
 2412                  */
 2413                 error = VOP_SETACL(tvp, ACL_TYPE_ACCESS, acl, cnp->cn_cred,
 2414                     cnp->cn_thread);
 2415                 switch (error) {
 2416                 case 0:
 2417                         break;
 2418 
 2419                 case EOPNOTSUPP:
 2420                         /*
 2421                          * XXX: This should not happen, as EOPNOTSUPP above was
 2422                          * supposed to free acl.
 2423                          */
 2424                         printf("ufs_makeinode: VOP_GETACL() but no "
 2425                             "VOP_SETACL()\n");
 2426                         /* panic("ufs_makeinode: VOP_GETACL() but no "
 2427                             "VOP_SETACL()"); */
 2428                         break;
 2429 
 2430                 default:
 2431                         uma_zfree(acl_zone, acl);
 2432                         goto bad;
 2433                 }
 2434                 uma_zfree(acl_zone, acl);
 2435         }
 2436 #endif /* !UFS_ACL */
 2437         ufs_makedirentry(ip, cnp, &newdir);
 2438         error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL);
 2439         if (error)
 2440                 goto bad;
 2441         *vpp = tvp;
 2442         return (0);
 2443 
 2444 bad:
 2445         /*
 2446          * Write error occurred trying to update the inode
 2447          * or the directory so must deallocate the inode.
 2448          */
 2449         ip->i_effnlink = 0;
 2450         ip->i_nlink = 0;
 2451         DIP_SET(ip, i_nlink, 0);
 2452         ip->i_flag |= IN_CHANGE;
 2453         if (DOINGSOFTDEP(tvp))
 2454                 softdep_change_linkcnt(ip);
 2455         vput(tvp);
 2456         return (error);
 2457 }
 2458 
 2459 /* Global vfs data structures for ufs. */
 2460 struct vop_vector ufs_vnodeops = {
 2461         .vop_default =          &default_vnodeops,
 2462         .vop_fsync =            VOP_PANIC,
 2463         .vop_read =             VOP_PANIC,
 2464         .vop_reallocblks =      VOP_PANIC,
 2465         .vop_write =            VOP_PANIC,
 2466         .vop_access =           ufs_access,
 2467         .vop_bmap =             ufs_bmap,
 2468         .vop_cachedlookup =     ufs_lookup,
 2469         .vop_close =            ufs_close,
 2470         .vop_create =           ufs_create,
 2471         .vop_getattr =          ufs_getattr,
 2472         .vop_inactive =         ufs_inactive,
 2473         .vop_link =             ufs_link,
 2474         .vop_lookup =           vfs_cache_lookup,
 2475         .vop_mkdir =            ufs_mkdir,
 2476         .vop_mknod =            ufs_mknod,
 2477         .vop_open =             ufs_open,
 2478         .vop_pathconf =         ufs_pathconf,
 2479         .vop_poll =             vop_stdpoll,
 2480         .vop_print =            ufs_print,
 2481         .vop_readdir =          ufs_readdir,
 2482         .vop_readlink =         ufs_readlink,
 2483         .vop_reclaim =          ufs_reclaim,
 2484         .vop_remove =           ufs_remove,
 2485         .vop_rename =           ufs_rename,
 2486         .vop_rmdir =            ufs_rmdir,
 2487         .vop_setattr =          ufs_setattr,
 2488 #ifdef MAC
 2489         .vop_setlabel =         vop_stdsetlabel_ea,
 2490 #endif
 2491         .vop_strategy =         ufs_strategy,
 2492         .vop_symlink =          ufs_symlink,
 2493         .vop_whiteout =         ufs_whiteout,
 2494 #ifdef UFS_EXTATTR
 2495         .vop_getextattr =       ufs_getextattr,
 2496         .vop_deleteextattr =    ufs_deleteextattr,
 2497         .vop_setextattr =       ufs_setextattr,
 2498 #endif
 2499 #ifdef UFS_ACL
 2500         .vop_getacl =           ufs_getacl,
 2501         .vop_setacl =           ufs_setacl,
 2502         .vop_aclcheck =         ufs_aclcheck,
 2503 #endif
 2504 };
 2505 
 2506 struct vop_vector ufs_fifoops = {
 2507         .vop_default =          &fifo_specops,
 2508         .vop_fsync =            VOP_PANIC,
 2509         .vop_access =           ufs_access,
 2510         .vop_close =            ufsfifo_close,
 2511         .vop_getattr =          ufs_getattr,
 2512         .vop_inactive =         ufs_inactive,
 2513         .vop_kqfilter =         ufsfifo_kqfilter,
 2514         .vop_print =            ufs_print,
 2515         .vop_read =             VOP_PANIC,
 2516         .vop_reclaim =          ufs_reclaim,
 2517         .vop_setattr =          ufs_setattr,
 2518 #ifdef MAC
 2519         .vop_setlabel =         vop_stdsetlabel_ea,
 2520 #endif
 2521         .vop_write =            VOP_PANIC,
 2522 #ifdef UFS_EXTATTR
 2523         .vop_getextattr =       ufs_getextattr,
 2524         .vop_deleteextattr =    ufs_deleteextattr,
 2525         .vop_setextattr =       ufs_setextattr,
 2526 #endif
 2527 #ifdef UFS_ACL
 2528         .vop_getacl =           ufs_getacl,
 2529         .vop_setacl =           ufs_setacl,
 2530         .vop_aclcheck =         ufs_aclcheck,
 2531 #endif
 2532 };

Cache object: 864187a94a5ad1d5b348e33071e5dc4e


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