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

Cache object: e40d62ca1703707e9fdcd55748549ad7


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