Home | History | Annotate | Line # | Download | only in lfs
ulfs_readwrite.c revision 1.15
      1 /*	$NetBSD: ulfs_readwrite.c,v 1.15 2015/03/28 19:24:05 maxv Exp $	*/
      2 /*  from NetBSD: ufs_readwrite.c,v 1.105 2013/01/22 09:39:18 dholland Exp  */
      3 
      4 /*-
      5  * Copyright (c) 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the University nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  *
     32  *	@(#)ufs_readwrite.c	8.11 (Berkeley) 5/8/95
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(1, "$NetBSD: ulfs_readwrite.c,v 1.15 2015/03/28 19:24:05 maxv Exp $");
     37 
     38 #ifdef LFS_READWRITE
     39 #define	FS			struct lfs
     40 #define	I_FS			i_lfs
     41 #define	READ			lfs_read
     42 #define	READ_S			"lfs_read"
     43 #define	WRITE			lfs_write
     44 #define	WRITE_S			"lfs_write"
     45 #define	BUFRD			lfs_bufrd
     46 #define	BUFWR			lfs_bufwr
     47 #define	fs_bsize		lfs_bsize
     48 #define	fs_bmask		lfs_bmask
     49 #else
     50 #define	FS			struct fs
     51 #define	I_FS			i_fs
     52 #define	READ			ffs_read
     53 #define	READ_S			"ffs_read"
     54 #define	WRITE			ffs_write
     55 #define	WRITE_S			"ffs_write"
     56 #define	BUFRD			ffs_bufrd
     57 #define	BUFWR			ffs_bufwr
     58 #endif
     59 
     60 static int	ulfs_post_read_update(struct vnode *, int, int);
     61 static int	ulfs_post_write_update(struct vnode *, struct uio *, int,
     62 		    kauth_cred_t, off_t, int, int, int);
     63 
     64 /*
     65  * Vnode op for reading.
     66  */
     67 /* ARGSUSED */
     68 int
     69 READ(void *v)
     70 {
     71 	struct vop_read_args /* {
     72 		struct vnode *a_vp;
     73 		struct uio *a_uio;
     74 		int a_ioflag;
     75 		kauth_cred_t a_cred;
     76 	} */ *ap = v;
     77 	struct vnode *vp;
     78 	struct inode *ip;
     79 	struct uio *uio;
     80 	FS *fs;
     81 	vsize_t bytelen;
     82 	int error, ioflag, advice;
     83 
     84 	vp = ap->a_vp;
     85 	ip = VTOI(vp);
     86 	fs = ip->I_FS;
     87 	uio = ap->a_uio;
     88 	ioflag = ap->a_ioflag;
     89 	error = 0;
     90 
     91 	KASSERT(uio->uio_rw == UIO_READ);
     92 	KASSERT(vp->v_type == VREG || vp->v_type == VDIR);
     93 
     94 	/* XXX Eliminate me by refusing directory reads from userland.  */
     95 	if (vp->v_type == VDIR)
     96 		return BUFRD(vp, uio, ioflag, ap->a_cred);
     97 #ifdef LFS_READWRITE
     98 	/* XXX Eliminate me by using ufs_bufio in lfs.  */
     99 	if (vp->v_type == VREG && ip->i_number == LFS_IFILE_INUM)
    100 		return BUFRD(vp, uio, ioflag, ap->a_cred);
    101 #endif
    102 	if ((u_int64_t)uio->uio_offset > fs->um_maxfilesize)
    103 		return (EFBIG);
    104 	if (uio->uio_resid == 0)
    105 		return (0);
    106 
    107 #ifndef LFS_READWRITE
    108 	if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) == SF_SNAPSHOT)
    109 		return ffs_snapshot_read(vp, uio, ioflag);
    110 #endif /* !LFS_READWRITE */
    111 
    112 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    113 
    114 	if (uio->uio_offset >= ip->i_size)
    115 		goto out;
    116 
    117 	KASSERT(vp->v_type == VREG);
    118 	advice = IO_ADV_DECODE(ap->a_ioflag);
    119 	while (uio->uio_resid > 0) {
    120 		if (ioflag & IO_DIRECT) {
    121 			genfs_directio(vp, uio, ioflag);
    122 		}
    123 		bytelen = MIN(ip->i_size - uio->uio_offset, uio->uio_resid);
    124 		if (bytelen == 0)
    125 			break;
    126 		error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
    127 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
    128 		if (error)
    129 			break;
    130 	}
    131 
    132  out:
    133 	error = ulfs_post_read_update(vp, ap->a_ioflag, error);
    134 	fstrans_done(vp->v_mount);
    135 	return (error);
    136 }
    137 
    138 /*
    139  * UFS op for reading via the buffer cache
    140  */
    141 int
    142 BUFRD(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
    143 {
    144 	struct inode *ip;
    145 	FS *fs;
    146 	struct buf *bp;
    147 	daddr_t lbn, nextlbn;
    148 	off_t bytesinfile;
    149 	long size, xfersize, blkoffset;
    150 	int error;
    151 
    152 	KASSERT(VOP_ISLOCKED(vp));
    153 	KASSERT(vp->v_type == VDIR || vp->v_type == VLNK ||
    154 	    vp->v_type == VREG);
    155 	KASSERT(uio->uio_rw == UIO_READ);
    156 
    157 	ip = VTOI(vp);
    158 	fs = ip->I_FS;
    159 	error = 0;
    160 
    161 	KASSERT(vp->v_type != VLNK || ip->i_size < fs->um_maxsymlinklen);
    162 	KASSERT(vp->v_type != VLNK || fs->um_maxsymlinklen != 0 ||
    163 	    DIP(ip, blocks) == 0);
    164 	KASSERT(vp->v_type != VREG || vp == fs->lfs_ivnode);
    165 	KASSERT(vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM);
    166 
    167 	if (uio->uio_offset > fs->um_maxfilesize)
    168 		return EFBIG;
    169 	if (uio->uio_resid == 0)
    170 		return 0;
    171 
    172 #ifndef LFS_READWRITE
    173 	KASSERT(!ISSET(ip->i_flags, (SF_SNAPSHOT | SF_SNAPINVAL)));
    174 #endif
    175 
    176 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    177 
    178 	if (uio->uio_offset >= ip->i_size)
    179 		goto out;
    180 
    181 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
    182 		bytesinfile = ip->i_size - uio->uio_offset;
    183 		if (bytesinfile <= 0)
    184 			break;
    185 		lbn = lfs_lblkno(fs, uio->uio_offset);
    186 		nextlbn = lbn + 1;
    187 		size = lfs_blksize(fs, ip, lbn);
    188 		blkoffset = lfs_blkoff(fs, uio->uio_offset);
    189 		xfersize = MIN(MIN(fs->fs_bsize - blkoffset, uio->uio_resid),
    190 		    bytesinfile);
    191 
    192 		if (lfs_lblktosize(fs, nextlbn) >= ip->i_size)
    193 			error = bread(vp, lbn, size, 0, &bp);
    194 		else {
    195 			int nextsize = lfs_blksize(fs, ip, nextlbn);
    196 			error = breadn(vp, lbn,
    197 			    size, &nextlbn, &nextsize, 1, 0, &bp);
    198 		}
    199 		if (error)
    200 			break;
    201 
    202 		/*
    203 		 * We should only get non-zero b_resid when an I/O error
    204 		 * has occurred, which should cause us to break above.
    205 		 * However, if the short read did not cause an error,
    206 		 * then we want to ensure that we do not uiomove bad
    207 		 * or uninitialized data.
    208 		 */
    209 		size -= bp->b_resid;
    210 		if (size < xfersize) {
    211 			if (size == 0)
    212 				break;
    213 			xfersize = size;
    214 		}
    215 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    216 		if (error)
    217 			break;
    218 		brelse(bp, 0);
    219 	}
    220 	if (bp != NULL)
    221 		brelse(bp, 0);
    222 
    223  out:
    224 	error = ulfs_post_read_update(vp, ioflag, error);
    225 	fstrans_done(vp->v_mount);
    226 	return (error);
    227 }
    228 
    229 static int
    230 ulfs_post_read_update(struct vnode *vp, int ioflag, int oerror)
    231 {
    232 	struct inode *ip = VTOI(vp);
    233 	int error = oerror;
    234 
    235 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    236 		ip->i_flag |= IN_ACCESS;
    237 		if ((ioflag & IO_SYNC) == IO_SYNC) {
    238 			error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
    239 		}
    240 	}
    241 
    242 	/* Read error overrides any inode update error.  */
    243 	if (oerror)
    244 		error = oerror;
    245 	return error;
    246 }
    247 
    248 /*
    249  * Vnode op for writing.
    250  */
    251 int
    252 WRITE(void *v)
    253 {
    254 	struct vop_write_args /* {
    255 		struct vnode *a_vp;
    256 		struct uio *a_uio;
    257 		int a_ioflag;
    258 		kauth_cred_t a_cred;
    259 	} */ *ap = v;
    260 	struct vnode *vp;
    261 	struct uio *uio;
    262 	struct inode *ip;
    263 	FS *fs;
    264 	kauth_cred_t cred;
    265 	off_t osize, origoff, oldoff, preallocoff, endallocoff, nsize;
    266 	int blkoffset, error, flags, ioflag, resid;
    267 	int aflag;
    268 	int extended=0;
    269 	vsize_t bytelen;
    270 	bool async;
    271 
    272 	cred = ap->a_cred;
    273 	ioflag = ap->a_ioflag;
    274 	uio = ap->a_uio;
    275 	vp = ap->a_vp;
    276 	ip = VTOI(vp);
    277 
    278 	KASSERT(vp->v_size == ip->i_size);
    279 	KASSERT(uio->uio_rw == UIO_WRITE);
    280 	KASSERT(vp->v_type == VREG);
    281 
    282 	if (ioflag & IO_APPEND)
    283 		uio->uio_offset = ip->i_size;
    284 	if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
    285 		return (EPERM);
    286 
    287 	fs = ip->I_FS;
    288 	if (uio->uio_offset < 0 ||
    289 	    (u_int64_t)uio->uio_offset + uio->uio_resid > fs->um_maxfilesize)
    290 		return (EFBIG);
    291 #ifdef LFS_READWRITE
    292 	/* Disallow writes to the Ifile, even if noschg flag is removed */
    293 	/* XXX can this go away when the Ifile is no longer in the namespace? */
    294 	if (vp == fs->lfs_ivnode)
    295 		return (EPERM);
    296 #endif
    297 	if (uio->uio_resid == 0)
    298 		return (0);
    299 
    300 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    301 
    302 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    303 	async = vp->v_mount->mnt_flag & MNT_ASYNC;
    304 	origoff = uio->uio_offset;
    305 	resid = uio->uio_resid;
    306 	osize = ip->i_size;
    307 	error = 0;
    308 
    309 	KASSERT(vp->v_type == VREG);
    310 
    311 #ifdef LFS_READWRITE
    312 	async = true;
    313 	lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
    314 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    315 #endif /* !LFS_READWRITE */
    316 
    317 	preallocoff = round_page(lfs_blkroundup(fs, MAX(osize, uio->uio_offset)));
    318 	aflag = ioflag & IO_SYNC ? B_SYNC : 0;
    319 	nsize = MAX(osize, uio->uio_offset + uio->uio_resid);
    320 	endallocoff = nsize - lfs_blkoff(fs, nsize);
    321 
    322 	/*
    323 	 * if we're increasing the file size, deal with expanding
    324 	 * the fragment if there is one.
    325 	 */
    326 
    327 	if (nsize > osize && lfs_lblkno(fs, osize) < ULFS_NDADDR &&
    328 	    lfs_lblkno(fs, osize) != lfs_lblkno(fs, nsize) &&
    329 	    lfs_blkroundup(fs, osize) != osize) {
    330 		off_t eob;
    331 
    332 		eob = lfs_blkroundup(fs, osize);
    333 		uvm_vnp_setwritesize(vp, eob);
    334 		error = ulfs_balloc_range(vp, osize, eob - osize, cred, aflag);
    335 		if (error)
    336 			goto out;
    337 		if (flags & B_SYNC) {
    338 			mutex_enter(vp->v_interlock);
    339 			VOP_PUTPAGES(vp, trunc_page(osize & fs->fs_bmask),
    340 			    round_page(eob),
    341 			    PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
    342 		}
    343 	}
    344 
    345 	while (uio->uio_resid > 0) {
    346 		int ubc_flags = UBC_WRITE;
    347 		bool overwrite; /* if we're overwrite a whole block */
    348 		off_t newoff;
    349 
    350 		if (ioflag & IO_DIRECT) {
    351 			genfs_directio(vp, uio, ioflag | IO_JOURNALLOCKED);
    352 		}
    353 
    354 		oldoff = uio->uio_offset;
    355 		blkoffset = lfs_blkoff(fs, uio->uio_offset);
    356 		bytelen = MIN(fs->fs_bsize - blkoffset, uio->uio_resid);
    357 		if (bytelen == 0) {
    358 			break;
    359 		}
    360 
    361 		/*
    362 		 * if we're filling in a hole, allocate the blocks now and
    363 		 * initialize the pages first.  if we're extending the file,
    364 		 * we can safely allocate blocks without initializing pages
    365 		 * since the new blocks will be inaccessible until the write
    366 		 * is complete.
    367 		 */
    368 		overwrite = uio->uio_offset >= preallocoff &&
    369 		    uio->uio_offset < endallocoff;
    370 		if (!overwrite && (vp->v_vflag & VV_MAPPED) == 0 &&
    371 		    lfs_blkoff(fs, uio->uio_offset) == 0 &&
    372 		    (uio->uio_offset & PAGE_MASK) == 0) {
    373 			vsize_t len;
    374 
    375 			len = trunc_page(bytelen);
    376 			len -= lfs_blkoff(fs, len);
    377 			if (len > 0) {
    378 				overwrite = true;
    379 				bytelen = len;
    380 			}
    381 		}
    382 
    383 		newoff = oldoff + bytelen;
    384 		if (vp->v_size < newoff) {
    385 			uvm_vnp_setwritesize(vp, newoff);
    386 		}
    387 
    388 		if (!overwrite) {
    389 			error = ulfs_balloc_range(vp, uio->uio_offset, bytelen,
    390 			    cred, aflag);
    391 			if (error)
    392 				break;
    393 		} else {
    394 			genfs_node_wrlock(vp);
    395 			error = GOP_ALLOC(vp, uio->uio_offset, bytelen,
    396 			    aflag, cred);
    397 			genfs_node_unlock(vp);
    398 			if (error)
    399 				break;
    400 			ubc_flags |= UBC_FAULTBUSY;
    401 		}
    402 
    403 		/*
    404 		 * copy the data.
    405 		 */
    406 
    407 		error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
    408 		    IO_ADV_DECODE(ioflag), ubc_flags | UBC_UNMAP_FLAG(vp));
    409 
    410 		/*
    411 		 * update UVM's notion of the size now that we've
    412 		 * copied the data into the vnode's pages.
    413 		 *
    414 		 * we should update the size even when uiomove failed.
    415 		 */
    416 
    417 		if (vp->v_size < newoff) {
    418 			uvm_vnp_setsize(vp, newoff);
    419 			extended = 1;
    420 		}
    421 
    422 		if (error)
    423 			break;
    424 
    425 		/*
    426 		 * flush what we just wrote if necessary.
    427 		 * XXXUBC simplistic async flushing.
    428 		 */
    429 
    430 #ifndef LFS_READWRITE
    431 		if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
    432 			mutex_enter(vp->v_interlock);
    433 			error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
    434 			    (uio->uio_offset >> 16) << 16,
    435 			    PGO_CLEANIT | PGO_JOURNALLOCKED | PGO_LAZY);
    436 			if (error)
    437 				break;
    438 		}
    439 #else
    440 		__USE(async);
    441 #endif
    442 	}
    443 	if (error == 0 && ioflag & IO_SYNC) {
    444 		mutex_enter(vp->v_interlock);
    445 		error = VOP_PUTPAGES(vp, trunc_page(origoff & fs->fs_bmask),
    446 		    round_page(lfs_blkroundup(fs, uio->uio_offset)),
    447 		    PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
    448 	}
    449 
    450 out:
    451 	error = ulfs_post_write_update(vp, uio, ioflag, cred, osize, resid,
    452 	    extended, error);
    453 	fstrans_done(vp->v_mount);
    454 
    455 	return (error);
    456 }
    457 
    458 /*
    459  * UFS op for writing via the buffer cache
    460  */
    461 int
    462 BUFWR(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
    463 {
    464 	struct inode *ip;
    465 	FS *fs;
    466 	int flags;
    467 	struct buf *bp;
    468 	off_t osize, origoff;
    469 	int resid, xfersize, size, blkoffset;
    470 	daddr_t lbn;
    471 	int extended=0;
    472 	int error;
    473 #ifdef LFS_READWRITE
    474 	bool need_unreserve = false;
    475 #endif
    476 
    477 	KASSERT(ISSET(ioflag, IO_NODELOCKED));
    478 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    479 	KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
    480 	KASSERT(vp->v_type != VDIR || ISSET(ioflag, IO_SYNC));
    481 	KASSERT(uio->uio_rw == UIO_WRITE);
    482 
    483 	ip = VTOI(vp);
    484 	fs = ip->I_FS;
    485 
    486 	KASSERT(vp->v_size == ip->i_size);
    487 
    488 	if (uio->uio_offset < 0 ||
    489 	    uio->uio_resid > fs->um_maxfilesize ||
    490 	    uio->uio_offset > (fs->um_maxfilesize - uio->uio_resid))
    491 		return EFBIG;
    492 #ifdef LFS_READWRITE
    493 	KASSERT(vp != fs->lfs_ivnode);
    494 #endif
    495 	if (uio->uio_resid == 0)
    496 		return 0;
    497 
    498 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    499 
    500 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    501 	origoff = uio->uio_offset;
    502 	resid = uio->uio_resid;
    503 	osize = ip->i_size;
    504 	error = 0;
    505 
    506 	KASSERT(vp->v_type != VREG);
    507 
    508 #ifdef LFS_READWRITE
    509 	lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
    510 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    511 #endif /* !LFS_READWRITE */
    512 
    513 	/* XXX Should never have pages cached here.  */
    514 	mutex_enter(vp->v_interlock);
    515 	VOP_PUTPAGES(vp, trunc_page(origoff), round_page(origoff + resid),
    516 	    PGO_CLEANIT | PGO_FREE | PGO_SYNCIO | PGO_JOURNALLOCKED);
    517 	while (uio->uio_resid > 0) {
    518 		lbn = lfs_lblkno(fs, uio->uio_offset);
    519 		blkoffset = lfs_blkoff(fs, uio->uio_offset);
    520 		xfersize = MIN(fs->fs_bsize - blkoffset, uio->uio_resid);
    521 		if (fs->fs_bsize > xfersize)
    522 			flags |= B_CLRBUF;
    523 		else
    524 			flags &= ~B_CLRBUF;
    525 
    526 #ifdef LFS_READWRITE
    527 		error = lfs_reserve(fs, vp, NULL,
    528 		    lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
    529 		if (error)
    530 			break;
    531 		need_unreserve = true;
    532 #endif
    533 		error = lfs_balloc(vp, uio->uio_offset, xfersize, cred, flags,
    534 		    &bp);
    535 
    536 		if (error)
    537 			break;
    538 		if (uio->uio_offset + xfersize > ip->i_size) {
    539 			ip->i_size = uio->uio_offset + xfersize;
    540 			DIP_ASSIGN(ip, size, ip->i_size);
    541 			uvm_vnp_setsize(vp, ip->i_size);
    542 			extended = 1;
    543 		}
    544 		size = lfs_blksize(fs, ip, lbn) - bp->b_resid;
    545 		if (xfersize > size)
    546 			xfersize = size;
    547 
    548 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    549 
    550 		/*
    551 		 * if we didn't clear the block and the uiomove failed,
    552 		 * the buf will now contain part of some other file,
    553 		 * so we need to invalidate it.
    554 		 */
    555 		if (error && (flags & B_CLRBUF) == 0) {
    556 			brelse(bp, BC_INVAL);
    557 			break;
    558 		}
    559 #ifdef LFS_READWRITE
    560 		(void)VOP_BWRITE(bp->b_vp, bp);
    561 		lfs_reserve(fs, vp, NULL,
    562 		    -lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
    563 		need_unreserve = false;
    564 #else
    565 		if (ioflag & IO_SYNC)
    566 			(void)bwrite(bp);
    567 		else if (xfersize + blkoffset == fs->fs_bsize)
    568 			bawrite(bp);
    569 		else
    570 			bdwrite(bp);
    571 #endif
    572 		if (error || xfersize == 0)
    573 			break;
    574 	}
    575 #ifdef LFS_READWRITE
    576 	if (need_unreserve) {
    577 		lfs_reserve(fs, vp, NULL,
    578 		    -lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
    579 	}
    580 #endif
    581 
    582 	error = ulfs_post_write_update(vp, uio, ioflag, cred, osize, resid,
    583 	    extended, error);
    584 	fstrans_done(vp->v_mount);
    585 
    586 	return (error);
    587 }
    588 
    589 static int
    590 ulfs_post_write_update(struct vnode *vp, struct uio *uio, int ioflag,
    591     kauth_cred_t cred, off_t osize, int resid, int extended, int oerror)
    592 {
    593 	struct inode *ip = VTOI(vp);
    594 	int error = oerror;
    595 
    596 	/* Trigger ctime and mtime updates, and atime if MNT_RELATIME.  */
    597 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    598 	if (vp->v_mount->mnt_flag & MNT_RELATIME)
    599 		ip->i_flag |= IN_ACCESS;
    600 
    601 	/*
    602 	 * If we successfully wrote any data and we are not the superuser,
    603 	 * we clear the setuid and setgid bits as a precaution against
    604 	 * tampering.
    605 	 */
    606 	if (resid > uio->uio_resid && cred) {
    607 		if (ip->i_mode & ISUID) {
    608 			if (kauth_authorize_vnode(cred,
    609 			    KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0) {
    610 				ip->i_mode &= ~ISUID;
    611 				DIP_ASSIGN(ip, mode, ip->i_mode);
    612 			}
    613 		}
    614 
    615 		if (ip->i_mode & ISGID) {
    616 			if (kauth_authorize_vnode(cred,
    617 			    KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0) {
    618 				ip->i_mode &= ~ISGID;
    619 				DIP_ASSIGN(ip, mode, ip->i_mode);
    620 			}
    621 		}
    622 	}
    623 
    624 	/* If we successfully wrote anything, notify kevent listeners.  */
    625 	if (resid > uio->uio_resid)
    626 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    627 
    628 	/*
    629 	 * Update the size on disk: truncate back to original size on
    630 	 * error, or reflect the new size on success.
    631 	 */
    632 	if (error) {
    633 		(void) lfs_truncate(vp, osize, ioflag & IO_SYNC, cred);
    634 		uio->uio_offset -= resid - uio->uio_resid;
    635 		uio->uio_resid = resid;
    636 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC) {
    637 		error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
    638 	} else {
    639 		/* nothing */
    640 	}
    641 
    642 	/* Make sure the vnode uvm size matches the inode file size.  */
    643 	KASSERT(vp->v_size == ip->i_size);
    644 
    645 	/* Write error overrides any inode update error.  */
    646 	if (oerror)
    647 		error = oerror;
    648 	return error;
    649 }
    650