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

Cache object: 37a94448c6a1d1089366a935bf64e3a0


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