Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_readwrite.c revision 1.51.2.3
      1  1.51.2.3      yamt /*	$NetBSD: ext2fs_readwrite.c,v 1.51.2.3 2010/03/11 15:04:44 yamt Exp $	*/
      2       1.1    bouyer 
      3       1.1    bouyer /*-
      4       1.1    bouyer  * Copyright (c) 1993
      5       1.1    bouyer  *	The Regents of the University of California.  All rights reserved.
      6       1.1    bouyer  *
      7       1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      8       1.1    bouyer  * modification, are permitted provided that the following conditions
      9       1.1    bouyer  * are met:
     10       1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     11       1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     12       1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     14       1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     15      1.30       agc  * 3. Neither the name of the University nor the names of its contributors
     16      1.30       agc  *    may be used to endorse or promote products derived from this software
     17      1.30       agc  *    without specific prior written permission.
     18      1.30       agc  *
     19      1.30       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20      1.30       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21      1.30       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22      1.30       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23      1.30       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24      1.30       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25      1.30       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26      1.30       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27      1.30       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28      1.30       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29      1.30       agc  * SUCH DAMAGE.
     30      1.30       agc  *
     31      1.30       agc  *	@(#)ufs_readwrite.c	8.8 (Berkeley) 8/4/94
     32      1.30       agc  * Modified for ext2fs by Manuel Bouyer.
     33      1.30       agc  */
     34      1.30       agc 
     35      1.30       agc /*-
     36      1.30       agc  * Copyright (c) 1997 Manuel Bouyer.
     37      1.30       agc  *
     38      1.30       agc  * Redistribution and use in source and binary forms, with or without
     39      1.30       agc  * modification, are permitted provided that the following conditions
     40      1.30       agc  * are met:
     41      1.30       agc  * 1. Redistributions of source code must retain the above copyright
     42      1.30       agc  *    notice, this list of conditions and the following disclaimer.
     43      1.30       agc  * 2. Redistributions in binary form must reproduce the above copyright
     44      1.30       agc  *    notice, this list of conditions and the following disclaimer in the
     45      1.30       agc  *    documentation and/or other materials provided with the distribution.
     46       1.1    bouyer  *
     47      1.32    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48      1.32    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49      1.32    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50      1.32    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51      1.32    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52      1.32    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53      1.32    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54      1.32    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55      1.32    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56      1.32    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57       1.1    bouyer  *
     58       1.1    bouyer  *	@(#)ufs_readwrite.c	8.8 (Berkeley) 8/4/94
     59       1.1    bouyer  * Modified for ext2fs by Manuel Bouyer.
     60       1.1    bouyer  */
     61      1.20     lukem 
     62      1.20     lukem #include <sys/cdefs.h>
     63  1.51.2.3      yamt __KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.51.2.3 2010/03/11 15:04:44 yamt Exp $");
     64       1.5       mrg 
     65       1.1    bouyer #include <sys/param.h>
     66       1.1    bouyer #include <sys/systm.h>
     67       1.1    bouyer #include <sys/resourcevar.h>
     68       1.1    bouyer #include <sys/kernel.h>
     69       1.1    bouyer #include <sys/file.h>
     70       1.1    bouyer #include <sys/stat.h>
     71       1.1    bouyer #include <sys/buf.h>
     72       1.1    bouyer #include <sys/proc.h>
     73       1.1    bouyer #include <sys/mount.h>
     74       1.1    bouyer #include <sys/vnode.h>
     75       1.1    bouyer #include <sys/malloc.h>
     76       1.1    bouyer #include <sys/signalvar.h>
     77      1.43      elad #include <sys/kauth.h>
     78       1.1    bouyer 
     79       1.1    bouyer #include <ufs/ufs/inode.h>
     80      1.33   mycroft #include <ufs/ufs/ufsmount.h>
     81      1.25  jdolecek #include <ufs/ufs/ufs_extern.h>
     82       1.1    bouyer #include <ufs/ext2fs/ext2fs.h>
     83       1.1    bouyer #include <ufs/ext2fs/ext2fs_extern.h>
     84       1.1    bouyer 
     85       1.1    bouyer 
     86       1.1    bouyer #define doclusterread 0 /* XXX underway */
     87       1.1    bouyer #define doclusterwrite 0
     88       1.1    bouyer 
     89       1.1    bouyer /*
     90       1.1    bouyer  * Vnode op for reading.
     91       1.1    bouyer  */
     92       1.1    bouyer /* ARGSUSED */
     93       1.1    bouyer int
     94      1.37   xtraeme ext2fs_read(void *v)
     95       1.1    bouyer {
     96       1.1    bouyer 	struct vop_read_args /* {
     97       1.1    bouyer 		struct vnode *a_vp;
     98       1.1    bouyer 		struct uio *a_uio;
     99       1.1    bouyer 		int a_ioflag;
    100      1.43      elad 		kauth_cred_t a_cred;
    101       1.1    bouyer 	} */ *ap = v;
    102      1.11  augustss 	struct vnode *vp;
    103      1.11  augustss 	struct inode *ip;
    104      1.11  augustss 	struct uio *uio;
    105      1.11  augustss 	struct m_ext2fs *fs;
    106       1.1    bouyer 	struct buf *bp;
    107      1.35       chs 	struct ufsmount *ump;
    108      1.14       chs 	vsize_t bytelen;
    109      1.27      fvdl 	daddr_t lbn, nextlbn;
    110       1.1    bouyer 	off_t bytesinfile;
    111       1.1    bouyer 	long size, xfersize, blkoffset;
    112  1.51.2.1      yamt 	int error;
    113       1.1    bouyer 
    114       1.1    bouyer 	vp = ap->a_vp;
    115       1.1    bouyer 	ip = VTOI(vp);
    116      1.33   mycroft 	ump = ip->i_ump;
    117       1.1    bouyer 	uio = ap->a_uio;
    118      1.33   mycroft 	error = 0;
    119       1.1    bouyer 
    120       1.1    bouyer #ifdef DIAGNOSTIC
    121       1.1    bouyer 	if (uio->uio_rw != UIO_READ)
    122       1.1    bouyer 		panic("%s: mode", "ext2fs_read");
    123       1.1    bouyer 
    124       1.1    bouyer 	if (vp->v_type == VLNK) {
    125      1.36        ws 		if (ext2fs_size(ip) < ump->um_maxsymlinklen ||
    126      1.33   mycroft 		    (ump->um_maxsymlinklen == 0 && ip->i_e2fs_nblock == 0))
    127       1.1    bouyer 			panic("%s: short symlink", "ext2fs_read");
    128       1.1    bouyer 	} else if (vp->v_type != VREG && vp->v_type != VDIR)
    129       1.1    bouyer 		panic("%s: type %d", "ext2fs_read", vp->v_type);
    130       1.1    bouyer #endif
    131       1.1    bouyer 	fs = ip->i_e2fs;
    132  1.51.2.2      yamt 	if ((uint64_t)uio->uio_offset > ump->um_maxfilesize)
    133       1.1    bouyer 		return (EFBIG);
    134       1.1    bouyer 	if (uio->uio_resid == 0)
    135      1.18       chs 		return (0);
    136      1.36        ws 	if (uio->uio_offset >= ext2fs_size(ip))
    137      1.33   mycroft 		goto out;
    138       1.1    bouyer 
    139      1.14       chs 	if (vp->v_type == VREG) {
    140      1.39      yamt 		const int advice = IO_ADV_DECODE(ap->a_ioflag);
    141      1.39      yamt 
    142      1.14       chs 		while (uio->uio_resid > 0) {
    143      1.36        ws 			bytelen = MIN(ext2fs_size(ip) - uio->uio_offset,
    144      1.14       chs 			    uio->uio_resid);
    145      1.33   mycroft 			if (bytelen == 0)
    146      1.33   mycroft 				break;
    147      1.14       chs 
    148  1.51.2.1      yamt 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
    149  1.51.2.1      yamt 			    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
    150      1.33   mycroft 			if (error)
    151      1.14       chs 				break;
    152      1.14       chs 		}
    153      1.14       chs 		goto out;
    154      1.14       chs 	}
    155      1.14       chs 
    156       1.1    bouyer 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
    157      1.36        ws 		bytesinfile = ext2fs_size(ip) - uio->uio_offset;
    158      1.33   mycroft 		if (bytesinfile <= 0)
    159       1.1    bouyer 			break;
    160       1.1    bouyer 		lbn = lblkno(fs, uio->uio_offset);
    161       1.1    bouyer 		nextlbn = lbn + 1;
    162       1.1    bouyer 		size = fs->e2fs_bsize;
    163       1.1    bouyer 		blkoffset = blkoff(fs, uio->uio_offset);
    164       1.1    bouyer 		xfersize = fs->e2fs_bsize - blkoffset;
    165       1.1    bouyer 		if (uio->uio_resid < xfersize)
    166       1.1    bouyer 			xfersize = uio->uio_resid;
    167       1.1    bouyer 		if (bytesinfile < xfersize)
    168       1.1    bouyer 			xfersize = bytesinfile;
    169       1.1    bouyer 
    170      1.36        ws 		if (lblktosize(fs, nextlbn) >= ext2fs_size(ip))
    171  1.51.2.1      yamt 			error = bread(vp, lbn, size, NOCRED, 0, &bp);
    172      1.17       chs 		else {
    173       1.1    bouyer 			int nextsize = fs->e2fs_bsize;
    174       1.1    bouyer 			error = breadn(vp, lbn,
    175  1.51.2.1      yamt 				size, &nextlbn, &nextsize, 1, NOCRED, 0, &bp);
    176      1.17       chs 		}
    177       1.1    bouyer 		if (error)
    178       1.1    bouyer 			break;
    179       1.1    bouyer 
    180       1.1    bouyer 		/*
    181       1.1    bouyer 		 * We should only get non-zero b_resid when an I/O error
    182       1.1    bouyer 		 * has occurred, which should cause us to break above.
    183       1.1    bouyer 		 * However, if the short read did not cause an error,
    184       1.1    bouyer 		 * then we want to ensure that we do not uiomove bad
    185       1.1    bouyer 		 * or uninitialized data.
    186       1.1    bouyer 		 */
    187       1.1    bouyer 		size -= bp->b_resid;
    188       1.1    bouyer 		if (size < xfersize) {
    189       1.1    bouyer 			if (size == 0)
    190       1.1    bouyer 				break;
    191       1.1    bouyer 			xfersize = size;
    192       1.1    bouyer 		}
    193      1.14       chs 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    194       1.1    bouyer 		if (error)
    195       1.1    bouyer 			break;
    196      1.48        ad 		brelse(bp, 0);
    197       1.1    bouyer 	}
    198       1.1    bouyer 	if (bp != NULL)
    199      1.48        ad 		brelse(bp, 0);
    200      1.14       chs 
    201      1.14       chs out:
    202       1.7    kleink 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    203       1.1    bouyer 		ip->i_flag |= IN_ACCESS;
    204       1.9   mycroft 		if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
    205      1.38      yamt 			error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
    206       1.7    kleink 	}
    207       1.1    bouyer 	return (error);
    208       1.1    bouyer }
    209       1.1    bouyer 
    210       1.1    bouyer /*
    211       1.1    bouyer  * Vnode op for writing.
    212       1.1    bouyer  */
    213       1.1    bouyer int
    214      1.37   xtraeme ext2fs_write(void *v)
    215       1.1    bouyer {
    216       1.1    bouyer 	struct vop_write_args /* {
    217       1.1    bouyer 		struct vnode *a_vp;
    218       1.1    bouyer 		struct uio *a_uio;
    219       1.1    bouyer 		int a_ioflag;
    220      1.43      elad 		kauth_cred_t a_cred;
    221       1.1    bouyer 	} */ *ap = v;
    222      1.11  augustss 	struct vnode *vp;
    223      1.11  augustss 	struct uio *uio;
    224      1.11  augustss 	struct inode *ip;
    225      1.11  augustss 	struct m_ext2fs *fs;
    226       1.1    bouyer 	struct buf *bp;
    227       1.1    bouyer 	struct proc *p;
    228      1.35       chs 	struct ufsmount *ump;
    229      1.27      fvdl 	daddr_t lbn;
    230       1.1    bouyer 	off_t osize;
    231      1.14       chs 	int blkoffset, error, flags, ioflag, resid, xfersize;
    232      1.14       chs 	vsize_t bytelen;
    233      1.36        ws 	off_t oldoff = 0;					/* XXX */
    234      1.45   thorpej 	bool async;
    235      1.33   mycroft 	int extended = 0;
    236  1.51.2.1      yamt 	int advice;
    237       1.1    bouyer 
    238       1.1    bouyer 	ioflag = ap->a_ioflag;
    239  1.51.2.1      yamt 	advice = IO_ADV_DECODE(ioflag);
    240       1.1    bouyer 	uio = ap->a_uio;
    241       1.1    bouyer 	vp = ap->a_vp;
    242       1.1    bouyer 	ip = VTOI(vp);
    243      1.33   mycroft 	ump = ip->i_ump;
    244      1.14       chs 	error = 0;
    245       1.1    bouyer 
    246       1.1    bouyer #ifdef DIAGNOSTIC
    247       1.1    bouyer 	if (uio->uio_rw != UIO_WRITE)
    248       1.1    bouyer 		panic("%s: mode", "ext2fs_write");
    249       1.1    bouyer #endif
    250       1.1    bouyer 
    251       1.1    bouyer 	switch (vp->v_type) {
    252       1.1    bouyer 	case VREG:
    253       1.1    bouyer 		if (ioflag & IO_APPEND)
    254      1.36        ws 			uio->uio_offset = ext2fs_size(ip);
    255       1.1    bouyer 		if ((ip->i_e2fs_flags & EXT2_APPEND) &&
    256      1.36        ws 		    uio->uio_offset != ext2fs_size(ip))
    257       1.1    bouyer 			return (EPERM);
    258       1.1    bouyer 		/* FALLTHROUGH */
    259       1.1    bouyer 	case VLNK:
    260       1.1    bouyer 		break;
    261       1.1    bouyer 	case VDIR:
    262       1.1    bouyer 		if ((ioflag & IO_SYNC) == 0)
    263       1.1    bouyer 			panic("%s: nonsync dir write", "ext2fs_write");
    264       1.1    bouyer 		break;
    265       1.1    bouyer 	default:
    266       1.1    bouyer 		panic("%s: type", "ext2fs_write");
    267       1.1    bouyer 	}
    268       1.1    bouyer 
    269       1.1    bouyer 	fs = ip->i_e2fs;
    270       1.1    bouyer 	if (uio->uio_offset < 0 ||
    271  1.51.2.2      yamt 	    (uint64_t)uio->uio_offset + uio->uio_resid > ump->um_maxfilesize)
    272       1.1    bouyer 		return (EFBIG);
    273       1.1    bouyer 	/*
    274       1.1    bouyer 	 * Maybe this should be above the vnode op call, but so long as
    275       1.1    bouyer 	 * file servers have no limits, I don't think it matters.
    276       1.1    bouyer 	 */
    277      1.42      yamt 	p = curproc;
    278       1.1    bouyer 	if (vp->v_type == VREG && p &&
    279      1.33   mycroft 	    uio->uio_offset + uio->uio_resid >
    280      1.33   mycroft 	    p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    281      1.51        ad 		mutex_enter(proc_lock);
    282       1.1    bouyer 		psignal(p, SIGXFSZ);
    283      1.51        ad 		mutex_exit(proc_lock);
    284       1.1    bouyer 		return (EFBIG);
    285       1.1    bouyer 	}
    286      1.24       chs 	if (uio->uio_resid == 0)
    287      1.24       chs 		return (0);
    288       1.1    bouyer 
    289      1.23       chs 	async = vp->v_mount->mnt_flag & MNT_ASYNC;
    290       1.1    bouyer 	resid = uio->uio_resid;
    291      1.36        ws 	osize = ext2fs_size(ip);
    292      1.14       chs 
    293      1.14       chs 	if (vp->v_type == VREG) {
    294      1.14       chs 		while (uio->uio_resid > 0) {
    295      1.14       chs 			oldoff = uio->uio_offset;
    296      1.14       chs 			blkoffset = blkoff(fs, uio->uio_offset);
    297      1.16       chs 			bytelen = MIN(fs->e2fs_bsize - blkoffset,
    298      1.14       chs 			    uio->uio_resid);
    299      1.14       chs 
    300      1.47      yamt 			if (vp->v_size < oldoff + bytelen) {
    301      1.47      yamt 				uvm_vnp_setwritesize(vp, oldoff + bytelen);
    302      1.47      yamt 			}
    303      1.25  jdolecek 			error = ufs_balloc_range(vp, uio->uio_offset,
    304      1.14       chs 			    bytelen, ap->a_cred, 0);
    305      1.33   mycroft 			if (error)
    306      1.14       chs 				break;
    307  1.51.2.1      yamt 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
    308  1.51.2.1      yamt 			    UBC_WRITE | UBC_UNMAP_FLAG(vp));
    309      1.33   mycroft 			if (error)
    310      1.14       chs 				break;
    311      1.14       chs 
    312      1.14       chs 			/*
    313      1.21       chs 			 * update UVM's notion of the size now that we've
    314      1.21       chs 			 * copied the data into the vnode's pages.
    315      1.21       chs 			 */
    316      1.21       chs 
    317      1.21       chs 			if (vp->v_size < uio->uio_offset) {
    318      1.21       chs 				uvm_vnp_setsize(vp, uio->uio_offset);
    319      1.26  jdolecek 				extended = 1;
    320      1.21       chs 			}
    321      1.21       chs 
    322      1.21       chs 			/*
    323      1.14       chs 			 * flush what we just wrote if necessary.
    324      1.14       chs 			 * XXXUBC simplistic async flushing.
    325      1.14       chs 			 */
    326      1.14       chs 
    327      1.23       chs 			if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
    328      1.50        ad 				mutex_enter(&vp->v_interlock);
    329      1.22       chs 				error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
    330      1.14       chs 				    (uio->uio_offset >> 16) << 16, PGO_CLEANIT);
    331      1.14       chs 			}
    332      1.14       chs 		}
    333      1.17       chs 		if (error == 0 && ioflag & IO_SYNC) {
    334      1.50        ad 			mutex_enter(&vp->v_interlock);
    335      1.22       chs 			error = VOP_PUTPAGES(vp, trunc_page(oldoff),
    336      1.22       chs 			    round_page(blkroundup(fs, uio->uio_offset)),
    337      1.22       chs 			    PGO_CLEANIT | PGO_SYNCIO);
    338      1.17       chs 		}
    339      1.17       chs 
    340      1.14       chs 		goto out;
    341      1.14       chs 	}
    342      1.14       chs 
    343       1.1    bouyer 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    344       1.1    bouyer 	for (error = 0; uio->uio_resid > 0;) {
    345       1.1    bouyer 		lbn = lblkno(fs, uio->uio_offset);
    346       1.1    bouyer 		blkoffset = blkoff(fs, uio->uio_offset);
    347      1.16       chs 		xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
    348      1.14       chs 		if (xfersize < fs->e2fs_bsize)
    349       1.1    bouyer 			flags |= B_CLRBUF;
    350       1.1    bouyer 		else
    351       1.1    bouyer 			flags &= ~B_CLRBUF;
    352      1.15       chs 		error = ext2fs_balloc(ip,
    353      1.15       chs 		    lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
    354       1.1    bouyer 		if (error)
    355       1.1    bouyer 			break;
    356      1.36        ws 		if (ext2fs_size(ip) < uio->uio_offset + xfersize) {
    357      1.36        ws 			error = ext2fs_setsize(ip, uio->uio_offset + xfersize);
    358      1.36        ws 			if (error)
    359      1.36        ws 				break;
    360       1.1    bouyer 		}
    361      1.14       chs 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    362      1.21       chs 
    363      1.21       chs 		/*
    364      1.21       chs 		 * update UVM's notion of the size now that we've
    365      1.21       chs 		 * copied the data into the vnode's pages.
    366      1.21       chs 		 */
    367      1.21       chs 
    368      1.21       chs 		if (vp->v_size < uio->uio_offset) {
    369      1.21       chs 			uvm_vnp_setsize(vp, uio->uio_offset);
    370      1.26  jdolecek 			extended = 1;
    371      1.21       chs 		}
    372      1.21       chs 
    373       1.1    bouyer 		if (ioflag & IO_SYNC)
    374       1.1    bouyer 			(void)bwrite(bp);
    375       1.1    bouyer 		else if (xfersize + blkoffset == fs->e2fs_bsize)
    376      1.17       chs 			bawrite(bp);
    377       1.1    bouyer 		else
    378       1.1    bouyer 			bdwrite(bp);
    379       1.1    bouyer 		if (error || xfersize == 0)
    380       1.1    bouyer 			break;
    381       1.1    bouyer 	}
    382      1.21       chs 
    383       1.1    bouyer 	/*
    384       1.1    bouyer 	 * If we successfully wrote any data, and we are not the superuser
    385       1.1    bouyer 	 * we clear the setuid and setgid bits as a precaution against
    386       1.1    bouyer 	 * tampering.
    387       1.1    bouyer 	 */
    388      1.21       chs 
    389      1.14       chs out:
    390      1.14       chs 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    391      1.44      elad 	if (resid > uio->uio_resid && ap->a_cred &&
    392      1.44      elad 	    kauth_authorize_generic(ap->a_cred, KAUTH_GENERIC_ISSUSER, NULL))
    393       1.1    bouyer 		ip->i_e2fs_mode &= ~(ISUID | ISGID);
    394      1.26  jdolecek 	if (resid > uio->uio_resid)
    395      1.26  jdolecek 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    396       1.1    bouyer 	if (error) {
    397      1.49     pooka 		(void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred);
    398      1.17       chs 		uio->uio_offset -= resid - uio->uio_resid;
    399      1.17       chs 		uio->uio_resid = resid;
    400       1.9   mycroft 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
    401      1.38      yamt 		error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
    402      1.36        ws 	KASSERT(vp->v_size == ext2fs_size(ip));
    403       1.1    bouyer 	return (error);
    404       1.1    bouyer }
    405