The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/ext2fs/ext2_vnops.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  *  modified for EXT2FS support in Lites 1.1
    3  *
    4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
    5  *  University of Utah, Department of Computer Science
    6  */
    7 /*-
    8  * Copyright (c) 1982, 1986, 1989, 1993
    9  *      The Regents of the University of California.  All rights reserved.
   10  * (c) UNIX System Laboratories, Inc.
   11  * All or some portions of this file are derived from material licensed
   12  * to the University of California by American Telephone and Telegraph
   13  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   14  * the permission of UNIX System Laboratories, Inc.
   15  *
   16  * Redistribution and use in source and binary forms, with or without
   17  * modification, are permitted provided that the following conditions
   18  * are met:
   19  * 1. Redistributions of source code must retain the above copyright
   20  *    notice, this list of conditions and the following disclaimer.
   21  * 2. Redistributions in binary form must reproduce the above copyright
   22  *    notice, this list of conditions and the following disclaimer in the
   23  *    documentation and/or other materials provided with the distribution.
   24  * 4. Neither the name of the University nor the names of its contributors
   25  *    may be used to endorse or promote products derived from this software
   26  *    without specific prior written permission.
   27  *
   28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   38  * SUCH DAMAGE.
   39  *
   40  *      @(#)ufs_vnops.c 8.7 (Berkeley) 2/3/94
   41  *      @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
   42  * $FreeBSD: releng/9.1/sys/fs/ext2fs/ext2_vnops.c 235820 2012-05-23 02:43:28Z pfg $
   43  */
   44 
   45 #include "opt_suiddir.h"
   46 
   47 #include <sys/param.h>
   48 #include <sys/systm.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/endian.h>
   55 #include <sys/priv.h>
   56 #include <sys/mount.h>
   57 #include <sys/unistd.h>
   58 #include <sys/time.h>
   59 #include <sys/vnode.h>
   60 #include <sys/namei.h>
   61 #include <sys/lockf.h>
   62 #include <sys/event.h>
   63 #include <sys/conf.h>
   64 #include <sys/file.h>
   65 
   66 #include <vm/vm.h>
   67 #include <vm/vm_page.h>
   68 #include <vm/vm_object.h>
   69 #include <vm/vm_extern.h>
   70 #include <vm/vnode_pager.h>
   71 
   72 #include "opt_directio.h"
   73 
   74 #include <fs/fifofs/fifo.h>
   75 
   76 #include <ufs/ufs/dir.h>
   77 
   78 #include <fs/ext2fs/fs.h>
   79 #include <fs/ext2fs/inode.h>
   80 #include <fs/ext2fs/ext2_extern.h>
   81 #include <fs/ext2fs/ext2fs.h>
   82 #include <fs/ext2fs/ext2_dinode.h>
   83 #include <fs/ext2fs/ext2_dir.h>
   84 #include <fs/ext2fs/ext2_mount.h>
   85 
   86 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *);
   87 static void ext2_itimes_locked(struct vnode *);
   88 
   89 static vop_access_t     ext2_access;
   90 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *);
   91 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *,
   92     struct thread *);
   93 static vop_close_t      ext2_close;
   94 static vop_create_t     ext2_create;
   95 static vop_fsync_t      ext2_fsync;
   96 static vop_getattr_t    ext2_getattr;
   97 static vop_link_t       ext2_link;
   98 static vop_mkdir_t      ext2_mkdir;
   99 static vop_mknod_t      ext2_mknod;
  100 static vop_open_t       ext2_open;
  101 static vop_pathconf_t   ext2_pathconf;
  102 static vop_print_t      ext2_print;
  103 static vop_read_t       ext2_read;
  104 static vop_readlink_t   ext2_readlink;
  105 static vop_remove_t     ext2_remove;
  106 static vop_rename_t     ext2_rename;
  107 static vop_rmdir_t      ext2_rmdir;
  108 static vop_setattr_t    ext2_setattr;
  109 static vop_strategy_t   ext2_strategy;
  110 static vop_symlink_t    ext2_symlink;
  111 static vop_write_t      ext2_write;
  112 static vop_vptofh_t     ext2_vptofh;
  113 static vop_close_t      ext2fifo_close;
  114 static vop_kqfilter_t   ext2fifo_kqfilter;
  115 
  116 /* Global vfs data structures for ext2. */
  117 struct vop_vector ext2_vnodeops = {
  118         .vop_default =          &default_vnodeops,
  119         .vop_access =           ext2_access,
  120         .vop_bmap =             ext2_bmap,
  121         .vop_cachedlookup =     ext2_lookup,
  122         .vop_close =            ext2_close,
  123         .vop_create =           ext2_create,
  124         .vop_fsync =            ext2_fsync,
  125         .vop_getattr =          ext2_getattr,
  126         .vop_inactive =         ext2_inactive,
  127         .vop_link =             ext2_link,
  128         .vop_lookup =           vfs_cache_lookup,
  129         .vop_mkdir =            ext2_mkdir,
  130         .vop_mknod =            ext2_mknod,
  131         .vop_open =             ext2_open,
  132         .vop_pathconf =         ext2_pathconf,
  133         .vop_poll =             vop_stdpoll,
  134         .vop_print =            ext2_print,
  135         .vop_read =             ext2_read,
  136         .vop_readdir =          ext2_readdir,
  137         .vop_readlink =         ext2_readlink,
  138         .vop_reallocblks =      ext2_reallocblks,
  139         .vop_reclaim =          ext2_reclaim,
  140         .vop_remove =           ext2_remove,
  141         .vop_rename =           ext2_rename,
  142         .vop_rmdir =            ext2_rmdir,
  143         .vop_setattr =          ext2_setattr,
  144         .vop_strategy =         ext2_strategy,
  145         .vop_symlink =          ext2_symlink,
  146         .vop_write =            ext2_write,
  147         .vop_vptofh =           ext2_vptofh,
  148 };
  149 
  150 struct vop_vector ext2_fifoops = {
  151         .vop_default =          &fifo_specops,
  152         .vop_access =           ext2_access,
  153         .vop_close =            ext2fifo_close,
  154         .vop_fsync =            ext2_fsync,
  155         .vop_getattr =          ext2_getattr,
  156         .vop_inactive =         ext2_inactive,
  157         .vop_kqfilter =         ext2fifo_kqfilter,
  158         .vop_print =            ext2_print,
  159         .vop_read =             VOP_PANIC,
  160         .vop_reclaim =          ext2_reclaim,
  161         .vop_setattr =          ext2_setattr,
  162         .vop_write =            VOP_PANIC,
  163         .vop_vptofh =           ext2_vptofh,
  164 };
  165 
  166 /*
  167  * A virgin directory (no blushing please).
  168  * Note that the type and namlen fields are reversed relative to ext2.
  169  * Also, we don't use `struct odirtemplate', since it would just cause
  170  * endianness problems.
  171  */
  172 static struct dirtemplate mastertemplate = {
  173         0, 12, 1, EXT2_FT_DIR, ".",
  174         0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
  175 };
  176 static struct dirtemplate omastertemplate = {
  177         0, 12, 1, EXT2_FT_UNKNOWN, ".",
  178         0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
  179 };
  180 
  181 static void
  182 ext2_itimes_locked(struct vnode *vp)
  183 {
  184         struct inode *ip;
  185         struct timespec ts;
  186 
  187         ASSERT_VI_LOCKED(vp, __func__); 
  188 
  189         ip = VTOI(vp);
  190         if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
  191                 return;
  192         if ((vp->v_type == VBLK || vp->v_type == VCHR))
  193                 ip->i_flag |= IN_LAZYMOD;
  194         else
  195                 ip->i_flag |= IN_MODIFIED;
  196         if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
  197                 vfs_timestamp(&ts);
  198                 if (ip->i_flag & IN_ACCESS) {
  199                         ip->i_atime = ts.tv_sec;
  200                         ip->i_atimensec = ts.tv_nsec;
  201                 }
  202                 if (ip->i_flag & IN_UPDATE) {
  203                         ip->i_mtime = ts.tv_sec;
  204                         ip->i_mtimensec = ts.tv_nsec;
  205                         ip->i_modrev++;
  206                 }
  207                 if (ip->i_flag & IN_CHANGE) {
  208                         ip->i_ctime = ts.tv_sec;
  209                         ip->i_ctimensec = ts.tv_nsec;
  210                 }
  211         }
  212         ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
  213 }
  214 
  215 void
  216 ext2_itimes(struct vnode *vp)
  217 {
  218 
  219         VI_LOCK(vp);
  220         ext2_itimes_locked(vp);
  221         VI_UNLOCK(vp);
  222 }
  223 
  224 /*
  225  * Create a regular file
  226  */
  227 static int
  228 ext2_create(ap)
  229         struct vop_create_args /* {
  230                 struct vnode *a_dvp;
  231                 struct vnode **a_vpp;
  232                 struct componentname *a_cnp;
  233                 struct vattr *a_vap;
  234         } */ *ap;
  235 {
  236         int error;
  237 
  238         error =
  239             ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
  240             ap->a_dvp, ap->a_vpp, ap->a_cnp);
  241         if (error)
  242                 return (error);
  243         return (0);
  244 }
  245 
  246 static int
  247 ext2_open(ap)
  248         struct vop_open_args /* {
  249                 struct vnode *a_vp;
  250                 int  a_mode;
  251                 struct ucred *a_cred;
  252                 struct thread *a_td;
  253         } */ *ap;
  254 {
  255 
  256         if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR)
  257                 return (EOPNOTSUPP);
  258 
  259         /*
  260          * Files marked append-only must be opened for appending.
  261          */
  262         if ((VTOI(ap->a_vp)->i_flags & APPEND) &&
  263             (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
  264                 return (EPERM);
  265 
  266         vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td);
  267 
  268         return (0);
  269 }
  270 
  271 /*
  272  * Close called.
  273  *
  274  * Update the times on the inode.
  275  */
  276 static int
  277 ext2_close(ap)
  278         struct vop_close_args /* {
  279                 struct vnode *a_vp;
  280                 int  a_fflag;
  281                 struct ucred *a_cred;
  282                 struct thread *a_td;
  283         } */ *ap;
  284 {
  285         struct vnode *vp = ap->a_vp;
  286 
  287         VI_LOCK(vp);
  288         if (vp->v_usecount > 1)
  289                 ext2_itimes_locked(vp);
  290         VI_UNLOCK(vp);
  291         return (0);
  292 }
  293 
  294 static int
  295 ext2_access(ap)
  296         struct vop_access_args /* {
  297                 struct vnode *a_vp;
  298                 accmode_t a_accmode;
  299                 struct ucred *a_cred;
  300                 struct thread *a_td;
  301         } */ *ap;
  302 {
  303         struct vnode *vp = ap->a_vp;
  304         struct inode *ip = VTOI(vp);
  305         accmode_t accmode = ap->a_accmode;
  306         int error;
  307 
  308         if (vp->v_type == VBLK || vp->v_type == VCHR)
  309                 return (EOPNOTSUPP);
  310 
  311         /*
  312          * Disallow write attempts on read-only file systems;
  313          * unless the file is a socket, fifo, or a block or
  314          * character device resident on the file system.
  315          */
  316         if (accmode & VWRITE) {
  317                 switch (vp->v_type) {
  318                 case VDIR:
  319                 case VLNK:
  320                 case VREG:
  321                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
  322                                 return (EROFS);
  323                         break;
  324                 default:
  325                         break;
  326                 }
  327         }
  328 
  329         /* If immutable bit set, nobody gets to write it. */
  330         if ((accmode & VWRITE) && (ip->i_flags & (SF_IMMUTABLE | SF_SNAPSHOT)))
  331                 return (EPERM);
  332 
  333         error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid,
  334             ap->a_accmode, ap->a_cred, NULL);
  335         return (error);
  336 }
  337 
  338 static int
  339 ext2_getattr(ap)
  340         struct vop_getattr_args /* {
  341                 struct vnode *a_vp;
  342                 struct vattr *a_vap;
  343                 struct ucred *a_cred;
  344         } */ *ap;
  345 {
  346         struct vnode *vp = ap->a_vp;
  347         struct inode *ip = VTOI(vp);
  348         struct vattr *vap = ap->a_vap;
  349 
  350         ext2_itimes(vp);
  351         /*
  352          * Copy from inode table
  353          */
  354         vap->va_fsid = dev2udev(ip->i_devvp->v_rdev);
  355         vap->va_fileid = ip->i_number;
  356         vap->va_mode = ip->i_mode & ~IFMT;
  357         vap->va_nlink = ip->i_nlink;
  358         vap->va_uid = ip->i_uid;
  359         vap->va_gid = ip->i_gid;
  360         vap->va_rdev = ip->i_rdev;
  361         vap->va_size = ip->i_size;
  362         vap->va_atime.tv_sec = ip->i_atime;
  363         vap->va_atime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_atimensec : 0;
  364         vap->va_mtime.tv_sec = ip->i_mtime;
  365         vap->va_mtime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_mtimensec : 0;
  366         vap->va_ctime.tv_sec = ip->i_ctime;
  367         vap->va_ctime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_ctimensec : 0;
  368         if E2DI_HAS_XTIME(ip) {
  369                 vap->va_birthtime.tv_sec = ip->i_birthtime;
  370                 vap->va_birthtime.tv_nsec = ip->i_birthnsec;
  371         }
  372         vap->va_flags = ip->i_flags;
  373         vap->va_gen = ip->i_gen;
  374         vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
  375         vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
  376         vap->va_type = IFTOVT(ip->i_mode);
  377         vap->va_filerev = ip->i_modrev;
  378         return (0);
  379 }
  380 
  381 /*
  382  * Set attribute vnode op. called from several syscalls
  383  */
  384 static int
  385 ext2_setattr(ap)
  386         struct vop_setattr_args /* {
  387                 struct vnode *a_vp;
  388                 struct vattr *a_vap;
  389                 struct ucred *a_cred;
  390         } */ *ap;
  391 {
  392         struct vattr *vap = ap->a_vap;
  393         struct vnode *vp = ap->a_vp;
  394         struct inode *ip = VTOI(vp);
  395         struct ucred *cred = ap->a_cred;
  396         struct thread *td = curthread;
  397         int error;
  398 
  399         /*
  400          * Check for unsettable attributes.
  401          */
  402         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
  403             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
  404             (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
  405             ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
  406                 return (EINVAL);
  407         }
  408         if (vap->va_flags != VNOVAL) {
  409                 /* Disallow flags not supported by ext2fs. */
  410                 if(vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP))
  411                         return (EOPNOTSUPP);
  412 
  413                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  414                         return (EROFS);
  415                 /*
  416                  * Callers may only modify the file flags on objects they
  417                  * have VADMIN rights for.
  418                  */
  419                 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
  420                         return (error);
  421                 /*
  422                  * Unprivileged processes and privileged processes in
  423                  * jail() are not permitted to unset system flags, or
  424                  * modify flags if any system flags are set.
  425                  * Privileged non-jail processes may not modify system flags
  426                  * if securelevel > 0 and any existing system flags are set.
  427                  */
  428                 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
  429                         if (ip->i_flags &
  430                             (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
  431                                 error = securelevel_gt(cred, 0);
  432                                 if (error)
  433                                         return (error);
  434                         }
  435                         ip->i_flags = vap->va_flags;
  436                 } else {
  437                         if (ip->i_flags &
  438                             (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
  439                             (vap->va_flags & UF_SETTABLE) != vap->va_flags)
  440                                 return (EPERM);
  441                         ip->i_flags &= SF_SETTABLE;
  442                         ip->i_flags |= (vap->va_flags & UF_SETTABLE);
  443                 }
  444                 ip->i_flag |= IN_CHANGE;
  445                 if (ip->i_flags & (IMMUTABLE | APPEND))
  446                         return (0);
  447         }
  448         if (ip->i_flags & (IMMUTABLE | APPEND))
  449                 return (EPERM);
  450         /*
  451          * Go through the fields and update iff not VNOVAL.
  452          */
  453         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
  454                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  455                         return (EROFS);
  456                 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred,
  457                     td)) != 0)
  458                         return (error);
  459         }
  460         if (vap->va_size != VNOVAL) {
  461                 /*
  462                  * Disallow write attempts on read-only file systems;
  463                  * unless the file is a socket, fifo, or a block or
  464                  * character device resident on the file system.
  465                  */
  466                 switch (vp->v_type) {
  467                 case VDIR:
  468                         return (EISDIR);
  469                 case VLNK:
  470                 case VREG:
  471                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
  472                                 return (EROFS);
  473                         break;
  474                 default:
  475                         break;
  476                 }
  477                 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0)
  478                         return (error);
  479         }
  480         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
  481                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  482                         return (EROFS);
  483                 /*
  484                  * From utimes(2):
  485                  * If times is NULL, ... The caller must be the owner of
  486                  * the file, have permission to write the file, or be the
  487                  * super-user.
  488                  * If times is non-NULL, ... The caller must be the owner of
  489                  * the file or be the super-user.
  490                  */
  491                 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) &&
  492                     ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
  493                     (error = VOP_ACCESS(vp, VWRITE, cred, td))))
  494                         return (error);
  495                 if (vap->va_atime.tv_sec != VNOVAL)
  496                         ip->i_flag |= IN_ACCESS;
  497                 if (vap->va_mtime.tv_sec != VNOVAL)
  498                         ip->i_flag |= IN_CHANGE | IN_UPDATE;
  499                 ext2_itimes(vp);
  500                 if (vap->va_atime.tv_sec != VNOVAL) {
  501                         ip->i_atime = vap->va_atime.tv_sec;
  502                         ip->i_atimensec = vap->va_atime.tv_nsec;
  503                 }
  504                 if (vap->va_mtime.tv_sec != VNOVAL) {
  505                         ip->i_mtime = vap->va_mtime.tv_sec;
  506                         ip->i_mtimensec = vap->va_mtime.tv_nsec;
  507                 }
  508                 ip->i_birthtime = vap->va_birthtime.tv_sec;
  509                 ip->i_birthnsec = vap->va_birthtime.tv_nsec;
  510                 error = ext2_update(vp, 0);
  511                 if (error)
  512                         return (error);
  513         }
  514         error = 0;
  515         if (vap->va_mode != (mode_t)VNOVAL) {
  516                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
  517                         return (EROFS);
  518                 error = ext2_chmod(vp, (int)vap->va_mode, cred, td);
  519         }
  520         return (error);
  521 }
  522 
  523 /*
  524  * Change the mode on a file.
  525  * Inode must be locked before calling.
  526  */
  527 static int
  528 ext2_chmod(vp, mode, cred, td)
  529         struct vnode *vp;
  530         int mode;
  531         struct ucred *cred;
  532         struct thread *td;
  533 {
  534         struct inode *ip = VTOI(vp);
  535         int error;
  536 
  537         /*
  538          * To modify the permissions on a file, must possess VADMIN
  539          * for that file.
  540          */
  541         if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
  542                 return (error);
  543         /*
  544          * Privileged processes may set the sticky bit on non-directories,
  545          * as well as set the setgid bit on a file with a group that the
  546          * process is not a member of.
  547          */
  548         if (vp->v_type != VDIR && (mode & S_ISTXT)) {
  549                 error = priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0);
  550                 if (error)
  551                         return (EFTYPE);
  552         }
  553         if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) {
  554                 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
  555                 if (error)
  556                         return (error);
  557         }
  558         ip->i_mode &= ~ALLPERMS;
  559         ip->i_mode |= (mode & ALLPERMS);
  560         ip->i_flag |= IN_CHANGE;
  561         return (0);
  562 }
  563 
  564 /*
  565  * Perform chown operation on inode ip;
  566  * inode must be locked prior to call.
  567  */
  568 static int
  569 ext2_chown(vp, uid, gid, cred, td)
  570         struct vnode *vp;
  571         uid_t uid;
  572         gid_t gid;
  573         struct ucred *cred;
  574         struct thread *td;
  575 {
  576         struct inode *ip = VTOI(vp);
  577         uid_t ouid;
  578         gid_t ogid;
  579         int error = 0;
  580 
  581         if (uid == (uid_t)VNOVAL)
  582                 uid = ip->i_uid;
  583         if (gid == (gid_t)VNOVAL)
  584                 gid = ip->i_gid;
  585         /*
  586          * To modify the ownership of a file, must possess VADMIN
  587          * for that file.
  588          */
  589         if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
  590                 return (error);
  591         /*
  592          * To change the owner of a file, or change the group of a file
  593          * to a group of which we are not a member, the caller must
  594          * have privilege.
  595          */
  596         if (uid != ip->i_uid || (gid != ip->i_gid &&
  597             !groupmember(gid, cred))) {
  598                 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
  599                 if (error)
  600                         return (error);
  601         }
  602         ogid = ip->i_gid;
  603         ouid = ip->i_uid;
  604         ip->i_gid = gid;
  605         ip->i_uid = uid;
  606         ip->i_flag |= IN_CHANGE;
  607         if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) {
  608                 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0) != 0)
  609                         ip->i_mode &= ~(ISUID | ISGID);
  610         }
  611         return (0);
  612 }
  613 
  614 /*
  615  * Synch an open file.
  616  */
  617 /* ARGSUSED */
  618 static int
  619 ext2_fsync(ap)
  620         struct vop_fsync_args /* {
  621                 struct vnode *a_vp;
  622                 struct ucred *a_cred;
  623                 int a_waitfor;
  624                 struct thread *a_td;
  625         } */ *ap;
  626 {
  627         /*
  628          * Flush all dirty buffers associated with a vnode.
  629          */
  630 
  631         vop_stdfsync(ap);
  632 
  633         return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT));
  634 }
  635 
  636 /*
  637  * Mknod vnode call
  638  */
  639 /* ARGSUSED */
  640 static int
  641 ext2_mknod(ap)
  642         struct vop_mknod_args /* {
  643                 struct vnode *a_dvp;
  644                 struct vnode **a_vpp;
  645                 struct componentname *a_cnp;
  646                 struct vattr *a_vap;
  647         } */ *ap;
  648 {
  649         struct vattr *vap = ap->a_vap;
  650         struct vnode **vpp = ap->a_vpp;
  651         struct inode *ip;
  652         ino_t ino;
  653         int error;
  654 
  655         error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
  656             ap->a_dvp, vpp, ap->a_cnp);
  657         if (error)
  658                 return (error);
  659         ip = VTOI(*vpp);
  660         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
  661         if (vap->va_rdev != VNOVAL) {
  662                 /*
  663                  * Want to be able to use this to make badblock
  664                  * inodes, so don't truncate the dev number.
  665                  */
  666                 ip->i_rdev = vap->va_rdev;
  667         }
  668         /*
  669          * Remove inode, then reload it through VFS_VGET so it is
  670          * checked to see if it is an alias of an existing entry in
  671          * the inode cache.      XXX I don't believe this is necessary now.
  672          */
  673         (*vpp)->v_type = VNON;
  674         ino = ip->i_number;     /* Save this before vgone() invalidates ip. */
  675         vgone(*vpp);
  676         vput(*vpp);
  677         error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp);
  678         if (error) {
  679                 *vpp = NULL;
  680                 return (error);
  681         }
  682         return (0);
  683 }
  684 
  685 static int
  686 ext2_remove(ap)
  687         struct vop_remove_args /* {
  688                 struct vnode *a_dvp;
  689                 struct vnode *a_vp;
  690                 struct componentname *a_cnp;
  691         } */ *ap;
  692 {
  693         struct inode *ip;
  694         struct vnode *vp = ap->a_vp;
  695         struct vnode *dvp = ap->a_dvp;
  696         int error;
  697 
  698         ip = VTOI(vp);
  699         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
  700             (VTOI(dvp)->i_flags & APPEND)) {
  701                 error = EPERM;
  702                 goto out;
  703         }
  704         error = ext2_dirremove(dvp, ap->a_cnp);
  705         if (error == 0) {
  706                 ip->i_nlink--;
  707                 ip->i_flag |= IN_CHANGE;
  708         }
  709 out:
  710         return (error);
  711 }
  712 
  713 /*
  714  * link vnode call
  715  */
  716 static int
  717 ext2_link(ap)
  718         struct vop_link_args /* {
  719                 struct vnode *a_tdvp;
  720                 struct vnode *a_vp;
  721                 struct componentname *a_cnp;
  722         } */ *ap;
  723 {
  724         struct vnode *vp = ap->a_vp;
  725         struct vnode *tdvp = ap->a_tdvp;
  726         struct componentname *cnp = ap->a_cnp;
  727         struct inode *ip;
  728         int error;
  729 
  730 #ifdef DIAGNOSTIC
  731         if ((cnp->cn_flags & HASBUF) == 0)
  732                 panic("ext2_link: no name");
  733 #endif
  734         if (tdvp->v_mount != vp->v_mount) {
  735                 error = EXDEV;
  736                 goto out;
  737         }
  738         ip = VTOI(vp);
  739         if ((nlink_t)ip->i_nlink >= LINK_MAX) {
  740                 error = EMLINK;
  741                 goto out;
  742         }
  743         if (ip->i_flags & (IMMUTABLE | APPEND)) {
  744                 error = EPERM;
  745                 goto out;
  746         }
  747         ip->i_nlink++;
  748         ip->i_flag |= IN_CHANGE;
  749         error = ext2_update(vp, !DOINGASYNC(vp));
  750         if (!error)
  751                 error = ext2_direnter(ip, tdvp, cnp);
  752         if (error) {
  753                 ip->i_nlink--;
  754                 ip->i_flag |= IN_CHANGE;
  755         }
  756 out:
  757         return (error);
  758 }
  759 
  760 /*
  761  * Rename system call.
  762  *      rename("foo", "bar");
  763  * is essentially
  764  *      unlink("bar");
  765  *      link("foo", "bar");
  766  *      unlink("foo");
  767  * but ``atomically''.  Can't do full commit without saving state in the
  768  * inode on disk which isn't feasible at this time.  Best we can do is
  769  * always guarantee the target exists.
  770  *
  771  * Basic algorithm is:
  772  *
  773  * 1) Bump link count on source while we're linking it to the
  774  *    target.  This also ensure the inode won't be deleted out
  775  *    from underneath us while we work (it may be truncated by
  776  *    a concurrent `trunc' or `open' for creation).
  777  * 2) Link source to destination.  If destination already exists,
  778  *    delete it first.
  779  * 3) Unlink source reference to inode if still around. If a
  780  *    directory was moved and the parent of the destination
  781  *    is different from the source, patch the ".." entry in the
  782  *    directory.
  783  */
  784 static int
  785 ext2_rename(ap)
  786         struct vop_rename_args  /* {
  787                 struct vnode *a_fdvp;
  788                 struct vnode *a_fvp;
  789                 struct componentname *a_fcnp;
  790                 struct vnode *a_tdvp;
  791                 struct vnode *a_tvp;
  792                 struct componentname *a_tcnp;
  793         } */ *ap;
  794 {
  795         struct vnode *tvp = ap->a_tvp;
  796         struct vnode *tdvp = ap->a_tdvp;
  797         struct vnode *fvp = ap->a_fvp;
  798         struct vnode *fdvp = ap->a_fdvp;
  799         struct componentname *tcnp = ap->a_tcnp;
  800         struct componentname *fcnp = ap->a_fcnp;
  801         struct inode *ip, *xp, *dp;
  802         struct dirtemplate dirbuf;
  803         int doingdirectory = 0, oldparent = 0, newparent = 0;
  804         int error = 0;
  805         u_char namlen;
  806 
  807 #ifdef DIAGNOSTIC
  808         if ((tcnp->cn_flags & HASBUF) == 0 ||
  809             (fcnp->cn_flags & HASBUF) == 0)
  810                 panic("ext2_rename: no name");
  811 #endif
  812         /*
  813          * Check for cross-device rename.
  814          */
  815         if ((fvp->v_mount != tdvp->v_mount) ||
  816             (tvp && (fvp->v_mount != tvp->v_mount))) {
  817                 error = EXDEV;
  818 abortit:
  819                 if (tdvp == tvp)
  820                         vrele(tdvp);
  821                 else
  822                         vput(tdvp);
  823                 if (tvp)
  824                         vput(tvp);
  825                 vrele(fdvp);
  826                 vrele(fvp);
  827                 return (error);
  828         }
  829 
  830         if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
  831             (VTOI(tdvp)->i_flags & APPEND))) {
  832                 error = EPERM;
  833                 goto abortit;
  834         }
  835 
  836         /*
  837          * Renaming a file to itself has no effect.  The upper layers should
  838          * not call us in that case.  Temporarily just warn if they do.
  839          */
  840         if (fvp == tvp) {
  841                 printf("ext2_rename: fvp == tvp (can't happen)\n");
  842                 error = 0;
  843                 goto abortit;
  844         }
  845 
  846         if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
  847                 goto abortit;
  848         dp = VTOI(fdvp);
  849         ip = VTOI(fvp);
  850         if (ip->i_nlink >= LINK_MAX) {
  851                 VOP_UNLOCK(fvp, 0);
  852                 error = EMLINK;
  853                 goto abortit;
  854         }
  855         if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
  856             || (dp->i_flags & APPEND)) {
  857                 VOP_UNLOCK(fvp, 0);
  858                 error = EPERM;
  859                 goto abortit;
  860         }
  861         if ((ip->i_mode & IFMT) == IFDIR) {
  862                 /*
  863                  * Avoid ".", "..", and aliases of "." for obvious reasons.
  864                  */
  865                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
  866                     dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT ||
  867                     (ip->i_flag & IN_RENAME)) {
  868                         VOP_UNLOCK(fvp, 0);
  869                         error = EINVAL;
  870                         goto abortit;
  871                 }
  872                 ip->i_flag |= IN_RENAME;
  873                 oldparent = dp->i_number;
  874                 doingdirectory++;
  875         }
  876         vrele(fdvp);
  877 
  878         /*
  879          * When the target exists, both the directory
  880          * and target vnodes are returned locked.
  881          */
  882         dp = VTOI(tdvp);
  883         xp = NULL;
  884         if (tvp)
  885                 xp = VTOI(tvp);
  886 
  887         /*
  888          * 1) Bump link count while we're moving stuff
  889          *    around.  If we crash somewhere before
  890          *    completing our work, the link count
  891          *    may be wrong, but correctable.
  892          */
  893         ip->i_nlink++;
  894         ip->i_flag |= IN_CHANGE;
  895         if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) {
  896                 VOP_UNLOCK(fvp, 0);
  897                 goto bad;
  898         }
  899 
  900         /*
  901          * If ".." must be changed (ie the directory gets a new
  902          * parent) then the source directory must not be in the
  903          * directory hierarchy above the target, as this would
  904          * orphan everything below the source directory. Also
  905          * the user must have write permission in the source so
  906          * as to be able to change "..". We must repeat the call
  907          * to namei, as the parent directory is unlocked by the
  908          * call to checkpath().
  909          */
  910         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
  911         VOP_UNLOCK(fvp, 0);
  912         if (oldparent != dp->i_number)
  913                 newparent = dp->i_number;
  914         if (doingdirectory && newparent) {
  915                 if (error)      /* write access check above */
  916                         goto bad;
  917                 if (xp != NULL)
  918                         vput(tvp);
  919                 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
  920                 if (error)
  921                         goto out;
  922                 VREF(tdvp);
  923                 error = relookup(tdvp, &tvp, tcnp);
  924                 if (error)
  925                         goto out;
  926                 vrele(tdvp);
  927                 dp = VTOI(tdvp);
  928                 xp = NULL;
  929                 if (tvp)
  930                         xp = VTOI(tvp);
  931         }
  932         /*
  933          * 2) If target doesn't exist, link the target
  934          *    to the source and unlink the source.
  935          *    Otherwise, rewrite the target directory
  936          *    entry to reference the source inode and
  937          *    expunge the original entry's existence.
  938          */
  939         if (xp == NULL) {
  940                 if (dp->i_devvp != ip->i_devvp)
  941                         panic("ext2_rename: EXDEV");
  942                 /*
  943                  * Account for ".." in new directory.
  944                  * When source and destination have the same
  945                  * parent we don't fool with the link count.
  946                  */
  947                 if (doingdirectory && newparent) {
  948                         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
  949                                 error = EMLINK;
  950                                 goto bad;
  951                         }
  952                         dp->i_nlink++;
  953                         dp->i_flag |= IN_CHANGE;
  954                         error = ext2_update(tdvp, !DOINGASYNC(tdvp));
  955                         if (error)
  956                                 goto bad;
  957                 }
  958                 error = ext2_direnter(ip, tdvp, tcnp);
  959                 if (error) {
  960                         if (doingdirectory && newparent) {
  961                                 dp->i_nlink--;
  962                                 dp->i_flag |= IN_CHANGE;
  963                                 (void)ext2_update(tdvp, 1);
  964                         }
  965                         goto bad;
  966                 }
  967                 vput(tdvp);
  968         } else {
  969                 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp)
  970                        panic("ext2_rename: EXDEV");
  971                 /*
  972                  * Short circuit rename(foo, foo).
  973                  */
  974                 if (xp->i_number == ip->i_number)
  975                         panic("ext2_rename: same file");
  976                 /*
  977                  * If the parent directory is "sticky", then the user must
  978                  * own the parent directory, or the destination of the rename,
  979                  * otherwise the destination may not be changed (except by
  980                  * root). This implements append-only directories.
  981                  */
  982                 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
  983                     tcnp->cn_cred->cr_uid != dp->i_uid &&
  984                     xp->i_uid != tcnp->cn_cred->cr_uid) {
  985                         error = EPERM;
  986                         goto bad;
  987                 }
  988                 /*
  989                  * Target must be empty if a directory and have no links
  990                  * to it. Also, ensure source and target are compatible
  991                  * (both directories, or both not directories).
  992                  */
  993                 if ((xp->i_mode&IFMT) == IFDIR) {
  994                         if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) || 
  995                             xp->i_nlink > 2) {
  996                                 error = ENOTEMPTY;
  997                                 goto bad;
  998                         }
  999                         if (!doingdirectory) {
 1000                                 error = ENOTDIR;
 1001                                 goto bad;
 1002                         }
 1003                         cache_purge(tdvp);
 1004                 } else if (doingdirectory) {
 1005                         error = EISDIR;
 1006                         goto bad;
 1007                 }
 1008                 error = ext2_dirrewrite(dp, ip, tcnp);
 1009                 if (error)
 1010                         goto bad;
 1011                 /*
 1012                  * If the target directory is in the same
 1013                  * directory as the source directory,
 1014                  * decrement the link count on the parent
 1015                  * of the target directory.
 1016                  */
 1017                 if (doingdirectory && !newparent) {
 1018                        dp->i_nlink--;
 1019                        dp->i_flag |= IN_CHANGE;
 1020                 }
 1021                 vput(tdvp);
 1022                 /*
 1023                  * Adjust the link count of the target to
 1024                  * reflect the dirrewrite above.  If this is
 1025                  * a directory it is empty and there are
 1026                  * no links to it, so we can squash the inode and
 1027                  * any space associated with it.  We disallowed
 1028                  * renaming over top of a directory with links to
 1029                  * it above, as the remaining link would point to
 1030                  * a directory without "." or ".." entries.
 1031                  */
 1032                 xp->i_nlink--;
 1033                 if (doingdirectory) {
 1034                         if (--xp->i_nlink != 0)
 1035                                 panic("ext2_rename: linked directory");
 1036                         error = ext2_truncate(tvp, (off_t)0, IO_SYNC,
 1037                             tcnp->cn_cred, tcnp->cn_thread);
 1038                 }
 1039                 xp->i_flag |= IN_CHANGE;
 1040                 vput(tvp);
 1041                 xp = NULL;
 1042         }
 1043 
 1044         /*
 1045          * 3) Unlink the source.
 1046          */
 1047         fcnp->cn_flags &= ~MODMASK;
 1048         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
 1049         VREF(fdvp);
 1050         error = relookup(fdvp, &fvp, fcnp);
 1051         if (error == 0)
 1052                 vrele(fdvp);
 1053         if (fvp != NULL) {
 1054                 xp = VTOI(fvp);
 1055                 dp = VTOI(fdvp);
 1056         } else {
 1057                 /*
 1058                  * From name has disappeared.
 1059                  */
 1060                 if (doingdirectory)
 1061                         panic("ext2_rename: lost dir entry");
 1062                 vrele(ap->a_fvp);
 1063                 return (0);
 1064         }
 1065         /*
 1066          * Ensure that the directory entry still exists and has not
 1067          * changed while the new name has been entered. If the source is
 1068          * a file then the entry may have been unlinked or renamed. In
 1069          * either case there is no further work to be done. If the source
 1070          * is a directory then it cannot have been rmdir'ed; its link
 1071          * count of three would cause a rmdir to fail with ENOTEMPTY.
 1072          * The IN_RENAME flag ensures that it cannot be moved by another
 1073          * rename.
 1074          */
 1075         if (xp != ip) {
 1076                 if (doingdirectory)
 1077                         panic("ext2_rename: lost dir entry");
 1078         } else {
 1079                 /*
 1080                  * If the source is a directory with a
 1081                  * new parent, the link count of the old
 1082                  * parent directory must be decremented
 1083                  * and ".." set to point to the new parent.
 1084                  */
 1085                 if (doingdirectory && newparent) {
 1086                         dp->i_nlink--;
 1087                         dp->i_flag |= IN_CHANGE;
 1088                         error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
 1089                                 sizeof(struct dirtemplate), (off_t)0,
 1090                                 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
 1091                                 tcnp->cn_cred, NOCRED, NULL, NULL);
 1092                         if (error == 0) {
 1093                                 /* Like ufs little-endian: */
 1094                                 namlen = dirbuf.dotdot_type;
 1095                                 if (namlen != 2 ||
 1096                                     dirbuf.dotdot_name[0] != '.' ||
 1097                                     dirbuf.dotdot_name[1] != '.') {
 1098                                         ext2_dirbad(xp, (doff_t)12,
 1099                                             "rename: mangled dir");
 1100                                 } else {
 1101                                         dirbuf.dotdot_ino = newparent;
 1102                                         (void) vn_rdwr(UIO_WRITE, fvp,
 1103                                             (caddr_t)&dirbuf,
 1104                                             sizeof(struct dirtemplate),
 1105                                             (off_t)0, UIO_SYSSPACE,
 1106                                             IO_NODELOCKED | IO_SYNC |
 1107                                             IO_NOMACCHECK, tcnp->cn_cred,
 1108                                             NOCRED, NULL, NULL);
 1109                                         cache_purge(fdvp);
 1110                                 }
 1111                         }
 1112                 }
 1113                 error = ext2_dirremove(fdvp, fcnp);
 1114                 if (!error) {
 1115                         xp->i_nlink--;
 1116                         xp->i_flag |= IN_CHANGE;
 1117                 }
 1118                 xp->i_flag &= ~IN_RENAME;
 1119         }
 1120         if (dp)
 1121                 vput(fdvp);
 1122         if (xp)
 1123                 vput(fvp);
 1124         vrele(ap->a_fvp);
 1125         return (error);
 1126 
 1127 bad:
 1128         if (xp)
 1129                 vput(ITOV(xp));
 1130         vput(ITOV(dp));
 1131 out:
 1132         if (doingdirectory)
 1133                 ip->i_flag &= ~IN_RENAME;
 1134         if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
 1135                 ip->i_nlink--;
 1136                 ip->i_flag |= IN_CHANGE;
 1137                 ip->i_flag &= ~IN_RENAME;
 1138                 vput(fvp);
 1139         } else
 1140                 vrele(fvp);
 1141         return (error);
 1142 }
 1143 
 1144 /*
 1145  * Mkdir system call
 1146  */
 1147 static int
 1148 ext2_mkdir(ap)
 1149         struct vop_mkdir_args /* {
 1150                 struct vnode *a_dvp;
 1151                 struct vnode **a_vpp;
 1152                 struct componentname *a_cnp;
 1153                 struct vattr *a_vap;
 1154         } */ *ap;
 1155 {
 1156         struct vnode *dvp = ap->a_dvp;
 1157         struct vattr *vap = ap->a_vap;
 1158         struct componentname *cnp = ap->a_cnp;
 1159         struct inode *ip, *dp;
 1160         struct vnode *tvp;
 1161         struct dirtemplate dirtemplate, *dtp;
 1162         int error, dmode;
 1163 
 1164 #ifdef DIAGNOSTIC
 1165         if ((cnp->cn_flags & HASBUF) == 0)
 1166                 panic("ext2_mkdir: no name");
 1167 #endif
 1168         dp = VTOI(dvp);
 1169         if ((nlink_t)dp->i_nlink >= LINK_MAX) {
 1170                 error = EMLINK;
 1171                 goto out;
 1172         }
 1173         dmode = vap->va_mode & 0777;
 1174         dmode |= IFDIR;
 1175         /*
 1176          * Must simulate part of ext2_makeinode here to acquire the inode,
 1177          * but not have it entered in the parent directory. The entry is
 1178          * made later after writing "." and ".." entries.
 1179          */
 1180         error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp);
 1181         if (error)
 1182                 goto out;
 1183         ip = VTOI(tvp);
 1184         ip->i_gid = dp->i_gid;
 1185 #ifdef SUIDDIR
 1186         {
 1187                 /*
 1188                  * if we are hacking owners here, (only do this where told to)
 1189                  * and we are not giving it TOO root, (would subvert quotas)
 1190                  * then go ahead and give it to the other user.
 1191                  * The new directory also inherits the SUID bit. 
 1192                  * If user's UID and dir UID are the same,
 1193                  * 'give it away' so that the SUID is still forced on.
 1194                  */
 1195                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
 1196                    (dp->i_mode & ISUID) && dp->i_uid) {
 1197                         dmode |= ISUID;
 1198                         ip->i_uid = dp->i_uid;
 1199                 } else {
 1200                         ip->i_uid = cnp->cn_cred->cr_uid;
 1201                 }
 1202         }
 1203 #else
 1204         ip->i_uid = cnp->cn_cred->cr_uid;
 1205 #endif
 1206         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
 1207         ip->i_mode = dmode;
 1208         tvp->v_type = VDIR;     /* Rest init'd in getnewvnode(). */
 1209         ip->i_nlink = 2;
 1210         if (cnp->cn_flags & ISWHITEOUT)
 1211                 ip->i_flags |= UF_OPAQUE;
 1212         error = ext2_update(tvp, 1);
 1213 
 1214         /*
 1215          * Bump link count in parent directory
 1216          * to reflect work done below.  Should
 1217          * be done before reference is created
 1218          * so reparation is possible if we crash.
 1219          */
 1220         dp->i_nlink++;
 1221         dp->i_flag |= IN_CHANGE;
 1222         error = ext2_update(dvp, !DOINGASYNC(dvp));
 1223         if (error)
 1224                 goto bad;
 1225 
 1226         /* Initialize directory with "." and ".." from static template. */
 1227         if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
 1228             EXT2F_INCOMPAT_FTYPE))
 1229                 dtp = &mastertemplate;
 1230         else
 1231                 dtp = &omastertemplate;
 1232         dirtemplate = *dtp;
 1233         dirtemplate.dot_ino = ip->i_number;
 1234         dirtemplate.dotdot_ino = dp->i_number;
 1235         /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE 
 1236          * so let's just redefine it - for this function only
 1237          */
 1238 #undef  DIRBLKSIZ 
 1239 #define DIRBLKSIZ  VTOI(dvp)->i_e2fs->e2fs_bsize
 1240         dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
 1241         error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
 1242             sizeof(dirtemplate), (off_t)0, UIO_SYSSPACE,
 1243             IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED,
 1244             NULL, NULL);
 1245         if (error) {
 1246                 dp->i_nlink--;
 1247                 dp->i_flag |= IN_CHANGE;
 1248                 goto bad;
 1249         }
 1250         if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
 1251                 /* XXX should grow with balloc() */
 1252                 panic("ext2_mkdir: blksize");
 1253         else {
 1254                 ip->i_size = DIRBLKSIZ;
 1255                 ip->i_flag |= IN_CHANGE;
 1256         }
 1257 
 1258         /* Directory set up, now install its entry in the parent directory. */
 1259         error = ext2_direnter(ip, dvp, cnp);
 1260         if (error) {
 1261                 dp->i_nlink--;
 1262                 dp->i_flag |= IN_CHANGE;
 1263         }
 1264 bad:
 1265         /*
 1266          * No need to do an explicit VOP_TRUNCATE here, vrele will do this
 1267          * for us because we set the link count to 0.
 1268          */
 1269         if (error) {
 1270                 ip->i_nlink = 0;
 1271                 ip->i_flag |= IN_CHANGE;
 1272                 vput(tvp);
 1273         } else
 1274                 *ap->a_vpp = tvp;
 1275 out:
 1276         return (error);
 1277 #undef  DIRBLKSIZ
 1278 #define DIRBLKSIZ  DEV_BSIZE
 1279 }
 1280 
 1281 /*
 1282  * Rmdir system call.
 1283  */
 1284 static int
 1285 ext2_rmdir(ap)
 1286         struct vop_rmdir_args /* {
 1287                 struct vnode *a_dvp;
 1288                 struct vnode *a_vp;
 1289                 struct componentname *a_cnp;
 1290         } */ *ap;
 1291 {
 1292         struct vnode *vp = ap->a_vp;
 1293         struct vnode *dvp = ap->a_dvp;
 1294         struct componentname *cnp = ap->a_cnp;
 1295         struct inode *ip, *dp;
 1296         int error;
 1297 
 1298         ip = VTOI(vp);
 1299         dp = VTOI(dvp);
 1300 
 1301         /*
 1302          * Verify the directory is empty (and valid).
 1303          * (Rmdir ".." won't be valid since
 1304          *  ".." will contain a reference to
 1305          *  the current directory and thus be
 1306          *  non-empty.)
 1307          */
 1308         error = 0;
 1309         if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
 1310                 error = ENOTEMPTY;
 1311                 goto out;
 1312         }
 1313         if ((dp->i_flags & APPEND)
 1314             || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
 1315                 error = EPERM;
 1316                 goto out;
 1317         }
 1318         /*
 1319          * Delete reference to directory before purging
 1320          * inode.  If we crash in between, the directory
 1321          * will be reattached to lost+found,
 1322          */
 1323         error = ext2_dirremove(dvp, cnp);
 1324         if (error)
 1325                 goto out;
 1326         dp->i_nlink--;
 1327         dp->i_flag |= IN_CHANGE;
 1328         cache_purge(dvp);
 1329         VOP_UNLOCK(dvp, 0);
 1330         /*
 1331          * Truncate inode.  The only stuff left
 1332          * in the directory is "." and "..".  The
 1333          * "." reference is inconsequential since
 1334          * we're quashing it.  The ".." reference
 1335          * has already been adjusted above.  We've
 1336          * removed the "." reference and the reference
 1337          * in the parent directory, but there may be
 1338          * other hard links so decrement by 2 and
 1339          * worry about them later.
 1340          */
 1341         ip->i_nlink -= 2;
 1342         error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred,
 1343             cnp->cn_thread);
 1344         cache_purge(ITOV(ip));
 1345         if (vn_lock(dvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
 1346                 VOP_UNLOCK(vp, 0);
 1347                 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
 1348                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
 1349         }
 1350 out:
 1351         return (error);
 1352 }
 1353 
 1354 /*
 1355  * symlink -- make a symbolic link
 1356  */
 1357 static int
 1358 ext2_symlink(ap)
 1359         struct vop_symlink_args /* {
 1360                 struct vnode *a_dvp;
 1361                 struct vnode **a_vpp;
 1362                 struct componentname *a_cnp;
 1363                 struct vattr *a_vap;
 1364                 char *a_target;
 1365         } */ *ap;
 1366 {
 1367         struct vnode *vp, **vpp = ap->a_vpp;
 1368         struct inode *ip;
 1369         int len, error;
 1370 
 1371         error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
 1372             vpp, ap->a_cnp);
 1373         if (error)
 1374                 return (error);
 1375         vp = *vpp;
 1376         len = strlen(ap->a_target);
 1377         if (len < vp->v_mount->mnt_maxsymlinklen) {
 1378                 ip = VTOI(vp);
 1379                 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
 1380                 ip->i_size = len;
 1381                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
 1382         } else
 1383                 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
 1384                     UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
 1385                     ap->a_cnp->cn_cred, NOCRED, NULL, NULL);
 1386         if (error)
 1387                 vput(vp);
 1388         return (error);
 1389 }
 1390 
 1391 /*
 1392  * Return target name of a symbolic link
 1393  */
 1394 static int
 1395 ext2_readlink(ap)
 1396         struct vop_readlink_args /* {
 1397                 struct vnode *a_vp;
 1398                 struct uio *a_uio;
 1399                 struct ucred *a_cred;
 1400         } */ *ap;
 1401 {
 1402         struct vnode *vp = ap->a_vp;
 1403         struct inode *ip = VTOI(vp);
 1404         int isize;
 1405 
 1406         isize = ip->i_size;
 1407         if (isize < vp->v_mount->mnt_maxsymlinklen) {
 1408                 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
 1409                 return (0);
 1410         }
 1411         return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
 1412 }
 1413 
 1414 /*
 1415  * Calculate the logical to physical mapping if not done already,
 1416  * then call the device strategy routine.
 1417  *
 1418  * In order to be able to swap to a file, the ext2_bmaparray() operation may not
 1419  * deadlock on memory.  See ext2_bmap() for details.
 1420  */
 1421 static int
 1422 ext2_strategy(ap)
 1423         struct vop_strategy_args /* {
 1424                 struct vnode *a_vp;
 1425                 struct buf *a_bp;
 1426         } */ *ap;
 1427 {
 1428         struct buf *bp = ap->a_bp;
 1429         struct vnode *vp = ap->a_vp;
 1430         struct inode *ip;
 1431         struct bufobj *bo;
 1432         int32_t blkno;
 1433         int error;
 1434 
 1435         ip = VTOI(vp);
 1436         if (vp->v_type == VBLK || vp->v_type == VCHR)
 1437                 panic("ext2_strategy: spec");
 1438         if (bp->b_blkno == bp->b_lblkno) {
 1439                 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL);
 1440                 bp->b_blkno = blkno;
 1441                 if (error) {
 1442                         bp->b_error = error;
 1443                         bp->b_ioflags |= BIO_ERROR;
 1444                         bufdone(bp);
 1445                         return (0);
 1446                 }
 1447                 if ((long)bp->b_blkno == -1)
 1448                         vfs_bio_clrbuf(bp);
 1449         }
 1450         if ((long)bp->b_blkno == -1) {
 1451                 bufdone(bp);
 1452                 return (0);
 1453         }
 1454         bp->b_iooffset = dbtob(bp->b_blkno);
 1455         bo = VFSTOEXT2(vp->v_mount)->um_bo;
 1456         BO_STRATEGY(bo, bp);
 1457         return (0);
 1458 }
 1459 
 1460 /*
 1461  * Print out the contents of an inode.
 1462  */
 1463 static int
 1464 ext2_print(ap)
 1465         struct vop_print_args /* {
 1466                 struct vnode *a_vp;
 1467         } */ *ap;
 1468 {
 1469         struct vnode *vp = ap->a_vp;
 1470         struct inode *ip = VTOI(vp);
 1471 
 1472         vn_printf(ip->i_devvp, "\tino %lu", (u_long)ip->i_number);
 1473         if (vp->v_type == VFIFO)
 1474                 fifo_printinfo(vp);
 1475         printf("\n");
 1476         return (0);
 1477 }
 1478 
 1479 /*
 1480  * Close wrapper for fifos.
 1481  *
 1482  * Update the times on the inode then do device close.
 1483  */
 1484 static int
 1485 ext2fifo_close(ap)
 1486         struct vop_close_args /* {
 1487                 struct vnode *a_vp;
 1488                 int  a_fflag;
 1489                 struct ucred *a_cred;
 1490                 struct thread *a_td;
 1491         } */ *ap;
 1492 {
 1493         struct vnode *vp = ap->a_vp;
 1494 
 1495         VI_LOCK(vp);
 1496         if (vp->v_usecount > 1)
 1497                 ext2_itimes_locked(vp);
 1498         VI_UNLOCK(vp);
 1499         return (fifo_specops.vop_close(ap));
 1500 }
 1501 
 1502 /*
 1503  * Kqfilter wrapper for fifos.
 1504  *
 1505  * Fall through to ext2 kqfilter routines if needed 
 1506  */
 1507 static int
 1508 ext2fifo_kqfilter(ap)
 1509         struct vop_kqfilter_args *ap;
 1510 {
 1511         int error;
 1512 
 1513         error = fifo_specops.vop_kqfilter(ap);
 1514         if (error)
 1515                 error = vfs_kqfilter(ap);
 1516         return (error);
 1517 }
 1518 
 1519 /*
 1520  * Return POSIX pathconf information applicable to ext2 filesystems.
 1521  */
 1522 static int
 1523 ext2_pathconf(ap)
 1524         struct vop_pathconf_args /* {
 1525                 struct vnode *a_vp;
 1526                 int a_name;
 1527                 int *a_retval;
 1528         } */ *ap;
 1529 {
 1530 
 1531         switch (ap->a_name) {
 1532         case _PC_LINK_MAX:
 1533                 *ap->a_retval = LINK_MAX;
 1534                 return (0);
 1535         case _PC_NAME_MAX:
 1536                 *ap->a_retval = NAME_MAX;
 1537                 return (0);
 1538         case _PC_PATH_MAX:
 1539                 *ap->a_retval = PATH_MAX;
 1540                 return (0);
 1541         case _PC_PIPE_BUF:
 1542                 *ap->a_retval = PIPE_BUF;
 1543                 return (0);
 1544         case _PC_CHOWN_RESTRICTED:
 1545                 *ap->a_retval = 1;
 1546                 return (0);
 1547         case _PC_NO_TRUNC:
 1548                 *ap->a_retval = 1;
 1549                 return (0);
 1550         default:
 1551                 return (EINVAL);
 1552         }
 1553         /* NOTREACHED */
 1554 }
 1555 
 1556 /*
 1557  * Vnode pointer to File handle
 1558  */
 1559 /* ARGSUSED */
 1560 static int
 1561 ext2_vptofh(ap)
 1562         struct vop_vptofh_args /* {
 1563                 struct vnode *a_vp;
 1564                 struct fid *a_fhp;
 1565         } */ *ap;
 1566 {
 1567         struct inode *ip;
 1568         struct ufid *ufhp;
 1569 
 1570         ip = VTOI(ap->a_vp);
 1571         ufhp = (struct ufid *)ap->a_fhp;
 1572         ufhp->ufid_len = sizeof(struct ufid);
 1573         ufhp->ufid_ino = ip->i_number;
 1574         ufhp->ufid_gen = ip->i_gen;
 1575         return (0);
 1576 }
 1577 
 1578 /*
 1579  * Initialize the vnode associated with a new inode, handle aliased
 1580  * vnodes.
 1581  */
 1582 int
 1583 ext2_vinit(mntp, fifoops, vpp)
 1584         struct mount *mntp;
 1585         struct vop_vector *fifoops;
 1586         struct vnode **vpp;
 1587 {
 1588         struct inode *ip;
 1589         struct vnode *vp;
 1590 
 1591         vp = *vpp;
 1592         ip = VTOI(vp);
 1593         vp->v_type = IFTOVT(ip->i_mode);
 1594         if (vp->v_type == VFIFO)
 1595                 vp->v_op = fifoops;
 1596 
 1597         if (ip->i_number == EXT2_ROOTINO)
 1598                 vp->v_vflag |= VV_ROOT;
 1599         ip->i_modrev = init_va_filerev();
 1600         *vpp = vp;
 1601         return (0);
 1602 }
 1603 
 1604 /*
 1605  * Allocate a new inode.
 1606  */
 1607 static int
 1608 ext2_makeinode(mode, dvp, vpp, cnp)
 1609         int mode;
 1610         struct vnode *dvp;
 1611         struct vnode **vpp;
 1612         struct componentname *cnp;
 1613 {
 1614         struct inode *ip, *pdir;
 1615         struct vnode *tvp;
 1616         int error;
 1617 
 1618         pdir = VTOI(dvp);
 1619 #ifdef DIAGNOSTIC
 1620         if ((cnp->cn_flags & HASBUF) == 0)
 1621                 panic("ext2_makeinode: no name");
 1622 #endif
 1623         *vpp = NULL;
 1624         if ((mode & IFMT) == 0)
 1625                 mode |= IFREG;
 1626 
 1627         error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp);
 1628         if (error) {
 1629                 return (error);
 1630         }
 1631         ip = VTOI(tvp);
 1632         ip->i_gid = pdir->i_gid;
 1633 #ifdef SUIDDIR
 1634         {
 1635                 /*
 1636                  * if we are
 1637                  * not the owner of the directory,
 1638                  * and we are hacking owners here, (only do this where told to)
 1639                  * and we are not giving it TOO root, (would subvert quotas)
 1640                  * then go ahead and give it to the other user.
 1641                  * Note that this drops off the execute bits for security.
 1642                  */
 1643                 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
 1644                      (pdir->i_mode & ISUID) &&
 1645                      (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
 1646                         ip->i_uid = pdir->i_uid;
 1647                         mode &= ~07111;
 1648                 } else {
 1649                         ip->i_uid = cnp->cn_cred->cr_uid;
 1650                 }
 1651         }
 1652 #else
 1653         ip->i_uid = cnp->cn_cred->cr_uid;
 1654 #endif
 1655         ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
 1656         ip->i_mode = mode;
 1657         tvp->v_type = IFTOVT(mode);     /* Rest init'd in getnewvnode(). */
 1658         ip->i_nlink = 1;
 1659         if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) {
 1660                 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0))
 1661                         ip->i_mode &= ~ISGID;
 1662         }
 1663 
 1664         if (cnp->cn_flags & ISWHITEOUT)
 1665                 ip->i_flags |= UF_OPAQUE;
 1666 
 1667         /*
 1668          * Make sure inode goes to disk before directory entry.
 1669          */
 1670         error = ext2_update(tvp, !DOINGASYNC(tvp));
 1671         if (error)
 1672                 goto bad;
 1673         error = ext2_direnter(ip, dvp, cnp);
 1674         if (error)
 1675                 goto bad;
 1676 
 1677         *vpp = tvp;
 1678         return (0);
 1679 
 1680 bad:
 1681         /*
 1682          * Write error occurred trying to update the inode
 1683          * or the directory so must deallocate the inode.
 1684          */
 1685         ip->i_nlink = 0;
 1686         ip->i_flag |= IN_CHANGE;
 1687         vput(tvp);
 1688         return (error);
 1689 }
 1690 
 1691 /*
 1692  * Vnode op for reading.
 1693  */
 1694 static int
 1695 ext2_read(ap)
 1696         struct vop_read_args /* {
 1697                 struct vnode *a_vp;
 1698                 struct uio *a_uio;
 1699                 int a_ioflag;
 1700                 struct ucred *a_cred;
 1701         } */ *ap;
 1702 {
 1703         struct vnode *vp;
 1704         struct inode *ip;
 1705         struct uio *uio;
 1706         struct m_ext2fs *fs;
 1707         struct buf *bp;
 1708         daddr_t lbn, nextlbn;
 1709         off_t bytesinfile;
 1710         long size, xfersize, blkoffset;
 1711         int error, orig_resid, seqcount;
 1712         int ioflag;
 1713 
 1714         vp = ap->a_vp;
 1715         uio = ap->a_uio;
 1716         ioflag = ap->a_ioflag;
 1717 
 1718         seqcount = ap->a_ioflag >> IO_SEQSHIFT;
 1719         ip = VTOI(vp);
 1720 
 1721 #ifdef INVARIANTS
 1722         if (uio->uio_rw != UIO_READ)
 1723                 panic("%s: mode", "ext2_read");
 1724 
 1725         if (vp->v_type == VLNK) {
 1726                 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
 1727                         panic("%s: short symlink", "ext2_read");
 1728         } else if (vp->v_type != VREG && vp->v_type != VDIR)
 1729                 panic("%s: type %d", "ext2_read", vp->v_type);
 1730 #endif
 1731         orig_resid = uio->uio_resid;
 1732         KASSERT(orig_resid >= 0, ("ext2_read: uio->uio_resid < 0"));
 1733         if (orig_resid == 0)
 1734                 return (0);
 1735         KASSERT(uio->uio_offset >= 0, ("ext2_read: uio->uio_offset < 0"));
 1736         fs = ip->i_e2fs;
 1737         if (uio->uio_offset < ip->i_size &&
 1738             uio->uio_offset >= fs->e2fs_maxfilesize)
 1739                 return (EOVERFLOW);
 1740 
 1741         for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
 1742                 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
 1743                         break;
 1744                 lbn = lblkno(fs, uio->uio_offset);
 1745                 nextlbn = lbn + 1;
 1746                 size = blksize(fs, ip, lbn);
 1747                 blkoffset = blkoff(fs, uio->uio_offset);
 1748 
 1749                 xfersize = fs->e2fs_fsize - blkoffset;
 1750                 if (uio->uio_resid < xfersize)
 1751                         xfersize = uio->uio_resid;
 1752                 if (bytesinfile < xfersize)
 1753                         xfersize = bytesinfile;
 1754 
 1755                 if (lblktosize(fs, nextlbn) >= ip->i_size)
 1756                         error = bread(vp, lbn, size, NOCRED, &bp);
 1757                 else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0)
 1758                         error = cluster_read(vp, ip->i_size, lbn, size,
 1759                             NOCRED, blkoffset + uio->uio_resid, seqcount, &bp);
 1760                 else if (seqcount > 1) {
 1761                         int nextsize = blksize(fs, ip, nextlbn);
 1762                         error = breadn(vp, lbn,
 1763                             size, &nextlbn, &nextsize, 1, NOCRED, &bp);
 1764                 } else
 1765                         error = bread(vp, lbn, size, NOCRED, &bp);
 1766                 if (error) {
 1767                         brelse(bp);
 1768                         bp = NULL;
 1769                         break;
 1770                 }
 1771 
 1772                 /*
 1773                  * If IO_DIRECT then set B_DIRECT for the buffer.  This
 1774                  * will cause us to attempt to release the buffer later on
 1775                  * and will cause the buffer cache to attempt to free the
 1776                  * underlying pages.
 1777                  */
 1778                 if (ioflag & IO_DIRECT)
 1779                         bp->b_flags |= B_DIRECT;
 1780 
 1781                 /*
 1782                  * We should only get non-zero b_resid when an I/O error
 1783                  * has occurred, which should cause us to break above.
 1784                  * However, if the short read did not cause an error,
 1785                  * then we want to ensure that we do not uiomove bad
 1786                  * or uninitialized data.
 1787                  */
 1788                 size -= bp->b_resid;
 1789                 if (size < xfersize) {
 1790                         if (size == 0)
 1791                                 break;
 1792                         xfersize = size;
 1793                 }
 1794                 error = uiomove((char *)bp->b_data + blkoffset,
 1795                         (int)xfersize, uio);
 1796                 if (error)
 1797                         break;
 1798 
 1799                 if (ioflag & (IO_VMIO|IO_DIRECT)) {
 1800                         /*
 1801                          * If it's VMIO or direct I/O, then we don't
 1802                          * need the buf, mark it available for
 1803                          * freeing. If it's non-direct VMIO, the VM has
 1804                          * the data.
 1805                          */
 1806                         bp->b_flags |= B_RELBUF;
 1807                         brelse(bp);
 1808                 } else {
 1809                         /*
 1810                          * Otherwise let whoever
 1811                          * made the request take care of
 1812                          * freeing it. We just queue
 1813                          * it onto another list.
 1814                          */
 1815                         bqrelse(bp);
 1816                 }
 1817         }
 1818 
 1819         /* 
 1820          * This can only happen in the case of an error
 1821          * because the loop above resets bp to NULL on each iteration
 1822          * and on normal completion has not set a new value into it.
 1823          * so it must have come from a 'break' statement
 1824          */
 1825         if (bp != NULL) {
 1826                 if (ioflag & (IO_VMIO|IO_DIRECT)) {
 1827                         bp->b_flags |= B_RELBUF;
 1828                         brelse(bp);
 1829                 } else {
 1830                         bqrelse(bp);
 1831                 }
 1832         }
 1833 
 1834         if ((error == 0 || uio->uio_resid != orig_resid) &&
 1835             (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
 1836                 ip->i_flag |= IN_ACCESS;
 1837         return (error);
 1838 }
 1839 
 1840 /*
 1841  * Vnode op for writing.
 1842  */
 1843 static int
 1844 ext2_write(ap)
 1845         struct vop_write_args /* {
 1846                 struct vnode *a_vp;
 1847                 struct uio *a_uio;
 1848                 int a_ioflag;
 1849                 struct ucred *a_cred;
 1850         } */ *ap;
 1851 {
 1852         struct vnode *vp;
 1853         struct uio *uio;
 1854         struct inode *ip;
 1855         struct m_ext2fs *fs;
 1856         struct buf *bp;
 1857         daddr_t lbn;
 1858         off_t osize;
 1859         int blkoffset, error, flags, ioflag, resid, size, seqcount, xfersize;
 1860 
 1861         ioflag = ap->a_ioflag;
 1862         uio = ap->a_uio;
 1863         vp = ap->a_vp;
 1864 
 1865         seqcount = ioflag >> IO_SEQSHIFT;
 1866         ip = VTOI(vp);
 1867 
 1868 #ifdef INVARIANTS
 1869         if (uio->uio_rw != UIO_WRITE)
 1870                 panic("%s: mode", "ext2_write");
 1871 #endif
 1872 
 1873         switch (vp->v_type) {
 1874         case VREG:
 1875                 if (ioflag & IO_APPEND)
 1876                         uio->uio_offset = ip->i_size;
 1877                 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
 1878                         return (EPERM);
 1879                 /* FALLTHROUGH */
 1880         case VLNK:
 1881                 break;
 1882         case VDIR:
 1883                 /* XXX differs from ffs -- this is called from ext2_mkdir(). */
 1884                 if ((ioflag & IO_SYNC) == 0)
 1885                 panic("ext2_write: nonsync dir write");
 1886                 break;
 1887         default:
 1888                 panic("ext2_write: type %p %d (%jd,%jd)", (void *)vp,
 1889                     vp->v_type, (intmax_t)uio->uio_offset,
 1890                     (intmax_t)uio->uio_resid);
 1891         }
 1892 
 1893         KASSERT(uio->uio_resid >= 0, ("ext2_write: uio->uio_resid < 0"));
 1894         KASSERT(uio->uio_offset >= 0, ("ext2_write: uio->uio_offset < 0"));
 1895         fs = ip->i_e2fs;
 1896         if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->e2fs_maxfilesize)
 1897                 return (EFBIG);
 1898         /*
 1899          * Maybe this should be above the vnode op call, but so long as
 1900          * file servers have no limits, I don't think it matters.
 1901          */
 1902         if (vn_rlimit_fsize(vp, uio, uio->uio_td))
 1903                 return (EFBIG);
 1904 
 1905         resid = uio->uio_resid;
 1906         osize = ip->i_size;
 1907         if (seqcount > BA_SEQMAX)
 1908                 flags = BA_SEQMAX << BA_SEQSHIFT;
 1909         else
 1910                 flags = seqcount << BA_SEQSHIFT;
 1911         if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
 1912                 flags |= IO_SYNC;
 1913 
 1914         for (error = 0; uio->uio_resid > 0;) {
 1915                 lbn = lblkno(fs, uio->uio_offset);
 1916                 blkoffset = blkoff(fs, uio->uio_offset);
 1917                 xfersize = fs->e2fs_fsize - blkoffset;
 1918                 if (uio->uio_resid < xfersize)
 1919                         xfersize = uio->uio_resid;
 1920                 if (uio->uio_offset + xfersize > ip->i_size)
 1921                         vnode_pager_setsize(vp, uio->uio_offset + xfersize);
 1922 
 1923                 /*
 1924                  * We must perform a read-before-write if the transfer size
 1925                  * does not cover the entire buffer.
 1926                  */
 1927                 if (fs->e2fs_bsize > xfersize)
 1928                         flags |= BA_CLRBUF;
 1929                 else
 1930                         flags &= ~BA_CLRBUF;
 1931                 error = ext2_balloc(ip, lbn, blkoffset + xfersize,
 1932                     ap->a_cred, &bp, flags);
 1933                 if (error != 0)
 1934                         break;
 1935 
 1936                 /*
 1937                  * If the buffer is not valid and we did not clear garbage
 1938                  * out above, we have to do so here even though the write
 1939                  * covers the entire buffer in order to avoid a mmap()/write
 1940                  * race where another process may see the garbage prior to
 1941                  * the uiomove() for a write replacing it.
 1942                  */
 1943                 if ((bp->b_flags & B_CACHE) == 0 && fs->e2fs_bsize <= xfersize)
 1944                         vfs_bio_clrbuf(bp);
 1945                 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
 1946                         bp->b_flags |= B_NOCACHE;
 1947                 if (uio->uio_offset + xfersize > ip->i_size)
 1948                         ip->i_size = uio->uio_offset + xfersize;
 1949                 size = blksize(fs, ip, lbn) - bp->b_resid;
 1950                 if (size < xfersize)
 1951                         xfersize = size;
 1952 
 1953                 error =
 1954                     uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
 1955                 if (ioflag & (IO_VMIO|IO_DIRECT)) {
 1956                         bp->b_flags |= B_RELBUF;
 1957                 }
 1958 
 1959                 /*
 1960                  * If IO_SYNC each buffer is written synchronously.  Otherwise
 1961                  * if we have a severe page deficiency write the buffer
 1962                  * asynchronously.  Otherwise try to cluster, and if that
 1963                  * doesn't do it then either do an async write (if O_DIRECT),
 1964                  * or a delayed write (if not).
 1965                  */
 1966                 if (ioflag & IO_SYNC) {
 1967                         (void)bwrite(bp);
 1968                 } else if (vm_page_count_severe() ||
 1969                     buf_dirty_count_severe() ||
 1970                     (ioflag & IO_ASYNC)) {
 1971                         bp->b_flags |= B_CLUSTEROK;
 1972                         bawrite(bp);
 1973                 } else if (xfersize + blkoffset == fs->e2fs_fsize) {
 1974                         if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
 1975                                 bp->b_flags |= B_CLUSTEROK;
 1976                                 cluster_write(vp, bp, ip->i_size, seqcount);
 1977                         } else {
 1978                                 bawrite(bp);
 1979                         }
 1980                 } else if (ioflag & IO_DIRECT) {
 1981                         bp->b_flags |= B_CLUSTEROK;
 1982                         bawrite(bp);
 1983                 } else {
 1984                         bp->b_flags |= B_CLUSTEROK;
 1985                         bdwrite(bp);
 1986                 }
 1987                 if (error || xfersize == 0)
 1988                         break;
 1989         }
 1990         /*
 1991          * If we successfully wrote any data, and we are not the superuser
 1992          * we clear the setuid and setgid bits as a precaution against
 1993          * tampering.
 1994          */
 1995         if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
 1996             ap->a_cred) {
 1997                 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
 1998                         ip->i_mode &= ~(ISUID | ISGID);
 1999         }
 2000         if (error) {
 2001                 if (ioflag & IO_UNIT) {
 2002                         (void)ext2_truncate(vp, osize,
 2003                             ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
 2004                         uio->uio_offset -= resid - uio->uio_resid;
 2005                         uio->uio_resid = resid;
 2006                 }
 2007         }
 2008         if (uio->uio_resid != resid) {
 2009                ip->i_flag |= IN_CHANGE | IN_UPDATE;
 2010                if (ioflag & IO_SYNC)
 2011                        error = ext2_update(vp, 1);
 2012        }
 2013         return (error);
 2014 }

Cache object: f66fc511ff5b3d67ab5a1adea8cb0e57


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