Home | History | Annotate | Line # | Download | only in lfs
ulfs_readwrite.c revision 1.8
      1 /*	$NetBSD: ulfs_readwrite.c,v 1.8 2015/03/27 17:27:56 riastradh 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.8 2015/03/27 17:27:56 riastradh 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 /*
     61  * Vnode op for reading.
     62  */
     63 /* ARGSUSED */
     64 int
     65 READ(void *v)
     66 {
     67 	struct vop_read_args /* {
     68 		struct vnode *a_vp;
     69 		struct uio *a_uio;
     70 		int a_ioflag;
     71 		kauth_cred_t a_cred;
     72 	} */ *ap = v;
     73 	struct vnode *vp;
     74 	struct inode *ip;
     75 	struct uio *uio;
     76 	FS *fs;
     77 	vsize_t bytelen;
     78 	int error, ioflag, advice;
     79 
     80 	vp = ap->a_vp;
     81 	ip = VTOI(vp);
     82 	fs = ip->I_FS;
     83 	uio = ap->a_uio;
     84 	ioflag = ap->a_ioflag;
     85 	error = 0;
     86 
     87 #ifdef DIAGNOSTIC
     88 	if (uio->uio_rw != UIO_READ)
     89 		panic("%s: mode", READ_S);
     90 
     91 	if (vp->v_type != VREG && vp->v_type != VDIR)
     92 		panic("%s: type %d", READ_S, vp->v_type);
     93 #endif
     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 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    134 		ip->i_flag |= IN_ACCESS;
    135 		if ((ap->a_ioflag & IO_SYNC) == IO_SYNC) {
    136 			error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
    137 		}
    138 	}
    139 
    140 	fstrans_done(vp->v_mount);
    141 	return (error);
    142 }
    143 
    144 /*
    145  * UFS op for reading via the buffer cache
    146  */
    147 int
    148 BUFRD(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
    149 {
    150 	struct inode *ip;
    151 	FS *fs;
    152 	struct buf *bp;
    153 	daddr_t lbn, nextlbn;
    154 	off_t bytesinfile;
    155 	long size, xfersize, blkoffset;
    156 	int error;
    157 
    158 	KASSERT(VOP_ISLOCKED(vp));
    159 	KASSERT(vp->v_type == VDIR || vp->v_type == VLNK ||
    160 	    vp->v_type == VREG);
    161 	KASSERT(uio->uio_rw == UIO_READ);
    162 
    163 	ip = VTOI(vp);
    164 	fs = ip->I_FS;
    165 	error = 0;
    166 
    167 	KASSERT(vp->v_type != VLNK || ip->i_size < fs->um_maxsymlinklen);
    168 	KASSERT(vp->v_type != VLNK || fs->um_maxsymlinklen != 0 ||
    169 	    DIP(ip, blocks) == 0);
    170 	KASSERT(vp->v_type != VREG || vp == fs->lfs_ivnode);
    171 	KASSERT(vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM);
    172 
    173 	if (uio->uio_offset > fs->um_maxfilesize)
    174 		return EFBIG;
    175 	if (uio->uio_resid == 0)
    176 		return 0;
    177 
    178 #ifndef LFS_READWRITE
    179 	KASSERT(!ISSET(ip->i_flags, (SF_SNAPSHOT | SF_SNAPINVAL)));
    180 #endif
    181 
    182 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    183 
    184 	if (uio->uio_offset >= ip->i_size)
    185 		goto out;
    186 
    187 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
    188 		bytesinfile = ip->i_size - uio->uio_offset;
    189 		if (bytesinfile <= 0)
    190 			break;
    191 		lbn = lfs_lblkno(fs, uio->uio_offset);
    192 		nextlbn = lbn + 1;
    193 		size = lfs_blksize(fs, ip, lbn);
    194 		blkoffset = lfs_blkoff(fs, uio->uio_offset);
    195 		xfersize = MIN(MIN(fs->fs_bsize - blkoffset, uio->uio_resid),
    196 		    bytesinfile);
    197 
    198 		if (lfs_lblktosize(fs, nextlbn) >= ip->i_size)
    199 			error = bread(vp, lbn, size, NOCRED, 0, &bp);
    200 		else {
    201 			int nextsize = lfs_blksize(fs, ip, nextlbn);
    202 			error = breadn(vp, lbn,
    203 			    size, &nextlbn, &nextsize, 1, NOCRED, 0, &bp);
    204 		}
    205 		if (error)
    206 			break;
    207 
    208 		/*
    209 		 * We should only get non-zero b_resid when an I/O error
    210 		 * has occurred, which should cause us to break above.
    211 		 * However, if the short read did not cause an error,
    212 		 * then we want to ensure that we do not uiomove bad
    213 		 * or uninitialized data.
    214 		 */
    215 		size -= bp->b_resid;
    216 		if (size < xfersize) {
    217 			if (size == 0)
    218 				break;
    219 			xfersize = size;
    220 		}
    221 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    222 		if (error)
    223 			break;
    224 		brelse(bp, 0);
    225 	}
    226 	if (bp != NULL)
    227 		brelse(bp, 0);
    228 
    229  out:
    230 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    231 		ip->i_flag |= IN_ACCESS;
    232 		if ((ioflag & IO_SYNC) == IO_SYNC) {
    233 			error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
    234 		}
    235 	}
    236 
    237 	fstrans_done(vp->v_mount);
    238 	return (error);
    239 }
    240 
    241 /*
    242  * Vnode op for writing.
    243  */
    244 int
    245 WRITE(void *v)
    246 {
    247 	struct vop_write_args /* {
    248 		struct vnode *a_vp;
    249 		struct uio *a_uio;
    250 		int a_ioflag;
    251 		kauth_cred_t a_cred;
    252 	} */ *ap = v;
    253 	struct vnode *vp;
    254 	struct uio *uio;
    255 	struct inode *ip;
    256 	FS *fs;
    257 	kauth_cred_t cred;
    258 	off_t osize, origoff, oldoff, preallocoff, endallocoff, nsize;
    259 	int blkoffset, error, flags, ioflag, resid;
    260 	int aflag;
    261 	int extended=0;
    262 	vsize_t bytelen;
    263 	bool async;
    264 
    265 	cred = ap->a_cred;
    266 	ioflag = ap->a_ioflag;
    267 	uio = ap->a_uio;
    268 	vp = ap->a_vp;
    269 	ip = VTOI(vp);
    270 
    271 	KASSERT(vp->v_size == ip->i_size);
    272 #ifdef DIAGNOSTIC
    273 	if (uio->uio_rw != UIO_WRITE)
    274 		panic("%s: mode", WRITE_S);
    275 #endif
    276 
    277 	switch (vp->v_type) {
    278 	case VREG:
    279 		if (ioflag & IO_APPEND)
    280 			uio->uio_offset = ip->i_size;
    281 		if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
    282 			return (EPERM);
    283 		break;
    284 	default:
    285 		panic("%s: type", WRITE_S);
    286 	}
    287 
    288 	fs = ip->I_FS;
    289 	if (uio->uio_offset < 0 ||
    290 	    (u_int64_t)uio->uio_offset + uio->uio_resid > fs->um_maxfilesize)
    291 		return (EFBIG);
    292 #ifdef LFS_READWRITE
    293 	/* Disallow writes to the Ifile, even if noschg flag is removed */
    294 	/* XXX can this go away when the Ifile is no longer in the namespace? */
    295 	if (vp == fs->lfs_ivnode)
    296 		return (EPERM);
    297 #endif
    298 	if (uio->uio_resid == 0)
    299 		return (0);
    300 
    301 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    302 
    303 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    304 	async = vp->v_mount->mnt_flag & MNT_ASYNC;
    305 	origoff = uio->uio_offset;
    306 	resid = uio->uio_resid;
    307 	osize = ip->i_size;
    308 	error = 0;
    309 
    310 	KASSERT(vp->v_type == VREG);
    311 
    312 #ifdef LFS_READWRITE
    313 	async = true;
    314 	lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
    315 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    316 #endif /* !LFS_READWRITE */
    317 
    318 	preallocoff = round_page(lfs_blkroundup(fs, MAX(osize, uio->uio_offset)));
    319 	aflag = ioflag & IO_SYNC ? B_SYNC : 0;
    320 	nsize = MAX(osize, uio->uio_offset + uio->uio_resid);
    321 	endallocoff = nsize - lfs_blkoff(fs, nsize);
    322 
    323 	/*
    324 	 * if we're increasing the file size, deal with expanding
    325 	 * the fragment if there is one.
    326 	 */
    327 
    328 	if (nsize > osize && lfs_lblkno(fs, osize) < ULFS_NDADDR &&
    329 	    lfs_lblkno(fs, osize) != lfs_lblkno(fs, nsize) &&
    330 	    lfs_blkroundup(fs, osize) != osize) {
    331 		off_t eob;
    332 
    333 		eob = lfs_blkroundup(fs, osize);
    334 		uvm_vnp_setwritesize(vp, eob);
    335 		error = ulfs_balloc_range(vp, osize, eob - osize, cred, aflag);
    336 		if (error)
    337 			goto out;
    338 		if (flags & B_SYNC) {
    339 			mutex_enter(vp->v_interlock);
    340 			VOP_PUTPAGES(vp, trunc_page(osize & fs->fs_bmask),
    341 			    round_page(eob),
    342 			    PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
    343 		}
    344 	}
    345 
    346 	while (uio->uio_resid > 0) {
    347 		int ubc_flags = UBC_WRITE;
    348 		bool overwrite; /* if we're overwrite a whole block */
    349 		off_t newoff;
    350 
    351 		if (ioflag & IO_DIRECT) {
    352 			genfs_directio(vp, uio, ioflag | IO_JOURNALLOCKED);
    353 		}
    354 
    355 		oldoff = uio->uio_offset;
    356 		blkoffset = lfs_blkoff(fs, uio->uio_offset);
    357 		bytelen = MIN(fs->fs_bsize - blkoffset, uio->uio_resid);
    358 		if (bytelen == 0) {
    359 			break;
    360 		}
    361 
    362 		/*
    363 		 * if we're filling in a hole, allocate the blocks now and
    364 		 * initialize the pages first.  if we're extending the file,
    365 		 * we can safely allocate blocks without initializing pages
    366 		 * since the new blocks will be inaccessible until the write
    367 		 * is complete.
    368 		 */
    369 		overwrite = uio->uio_offset >= preallocoff &&
    370 		    uio->uio_offset < endallocoff;
    371 		if (!overwrite && (vp->v_vflag & VV_MAPPED) == 0 &&
    372 		    lfs_blkoff(fs, uio->uio_offset) == 0 &&
    373 		    (uio->uio_offset & PAGE_MASK) == 0) {
    374 			vsize_t len;
    375 
    376 			len = trunc_page(bytelen);
    377 			len -= lfs_blkoff(fs, len);
    378 			if (len > 0) {
    379 				overwrite = true;
    380 				bytelen = len;
    381 			}
    382 		}
    383 
    384 		newoff = oldoff + bytelen;
    385 		if (vp->v_size < newoff) {
    386 			uvm_vnp_setwritesize(vp, newoff);
    387 		}
    388 
    389 		if (!overwrite) {
    390 			error = ulfs_balloc_range(vp, uio->uio_offset, bytelen,
    391 			    cred, aflag);
    392 			if (error)
    393 				break;
    394 		} else {
    395 			genfs_node_wrlock(vp);
    396 			error = GOP_ALLOC(vp, uio->uio_offset, bytelen,
    397 			    aflag, cred);
    398 			genfs_node_unlock(vp);
    399 			if (error)
    400 				break;
    401 			ubc_flags |= UBC_FAULTBUSY;
    402 		}
    403 
    404 		/*
    405 		 * copy the data.
    406 		 */
    407 
    408 		error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
    409 		    IO_ADV_DECODE(ioflag), ubc_flags | UBC_UNMAP_FLAG(vp));
    410 
    411 		/*
    412 		 * update UVM's notion of the size now that we've
    413 		 * copied the data into the vnode's pages.
    414 		 *
    415 		 * we should update the size even when uiomove failed.
    416 		 */
    417 
    418 		if (vp->v_size < newoff) {
    419 			uvm_vnp_setsize(vp, newoff);
    420 			extended = 1;
    421 		}
    422 
    423 		if (error)
    424 			break;
    425 
    426 		/*
    427 		 * flush what we just wrote if necessary.
    428 		 * XXXUBC simplistic async flushing.
    429 		 */
    430 
    431 #ifndef LFS_READWRITE
    432 		if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
    433 			mutex_enter(vp->v_interlock);
    434 			error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
    435 			    (uio->uio_offset >> 16) << 16,
    436 			    PGO_CLEANIT | PGO_JOURNALLOCKED | PGO_LAZY);
    437 			if (error)
    438 				break;
    439 		}
    440 #else
    441 		__USE(async);
    442 #endif
    443 	}
    444 	if (error == 0 && ioflag & IO_SYNC) {
    445 		mutex_enter(vp->v_interlock);
    446 		error = VOP_PUTPAGES(vp, trunc_page(origoff & fs->fs_bmask),
    447 		    round_page(lfs_blkroundup(fs, uio->uio_offset)),
    448 		    PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
    449 	}
    450 
    451 	/*
    452 	 * If we successfully wrote any data, and we are not the superuser
    453 	 * we clear the setuid and setgid bits as a precaution against
    454 	 * tampering.
    455 	 */
    456 out:
    457 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    458 	if (vp->v_mount->mnt_flag & MNT_RELATIME)
    459 		ip->i_flag |= IN_ACCESS;
    460 	if (resid > uio->uio_resid && ap->a_cred) {
    461 		if (ip->i_mode & ISUID) {
    462 			if (kauth_authorize_vnode(ap->a_cred,
    463 			    KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0) {
    464 				ip->i_mode &= ~ISUID;
    465 				DIP_ASSIGN(ip, mode, ip->i_mode);
    466 			}
    467 		}
    468 
    469 		if (ip->i_mode & ISGID) {
    470 			if (kauth_authorize_vnode(ap->a_cred,
    471 			    KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0) {
    472 				ip->i_mode &= ~ISGID;
    473 				DIP_ASSIGN(ip, mode, ip->i_mode);
    474 			}
    475 		}
    476 	}
    477 	if (resid > uio->uio_resid)
    478 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    479 	if (error) {
    480 		(void) lfs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred);
    481 		uio->uio_offset -= resid - uio->uio_resid;
    482 		uio->uio_resid = resid;
    483 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC) {
    484 		error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
    485 	} else {
    486 		/* nothing */
    487 	}
    488 	KASSERT(vp->v_size == ip->i_size);
    489 	fstrans_done(vp->v_mount);
    490 
    491 	return (error);
    492 }
    493 
    494 /*
    495  * UFS op for writing via the buffer cache
    496  */
    497 int
    498 BUFWR(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
    499 {
    500 	struct inode *ip;
    501 	FS *fs;
    502 	int flags;
    503 	struct buf *bp;
    504 	off_t osize, origoff;
    505 	int resid, xfersize, size, blkoffset;
    506 	daddr_t lbn;
    507 	int extended=0;
    508 	int error;
    509 #ifdef LFS_READWRITE
    510 	bool need_unreserve = false;
    511 #endif
    512 
    513 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    514 	KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
    515 	KASSERT(vp->v_type != VDIR || ISSET(ioflag, IO_SYNC));
    516 	KASSERT(uio->uio_rw == UIO_WRITE);
    517 
    518 	ip = VTOI(vp);
    519 	fs = ip->I_FS;
    520 
    521 	KASSERT(vp->v_size == ip->i_size);
    522 
    523 	if (uio->uio_offset < 0 ||
    524 	    uio->uio_resid > fs->um_maxfilesize ||
    525 	    uio->uio_offset > (fs->um_maxfilesize - uio->uio_resid))
    526 		return EFBIG;
    527 #ifdef LFS_READWRITE
    528 	KASSERT(vp != fs->lfs_ivnode);
    529 #endif
    530 	if (uio->uio_resid == 0)
    531 		return 0;
    532 
    533 	fstrans_start(vp->v_mount, FSTRANS_SHARED);
    534 
    535 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    536 	origoff = uio->uio_offset;
    537 	resid = uio->uio_resid;
    538 	osize = ip->i_size;
    539 	error = 0;
    540 
    541 	KASSERT(vp->v_type != VREG);
    542 
    543 #ifdef LFS_READWRITE
    544 	lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
    545 	lfs_check(vp, LFS_UNUSED_LBN, 0);
    546 #endif /* !LFS_READWRITE */
    547 
    548 	/* XXX Should never have cached pages here.  */
    549 	mutex_enter(vp->v_interlock);
    550 	VOP_PUTPAGES(vp, trunc_page(origoff), round_page(origoff + resid),
    551 	    PGO_CLEANIT | PGO_FREE | PGO_SYNCIO | PGO_JOURNALLOCKED);
    552 	while (uio->uio_resid > 0) {
    553 		lbn = lfs_lblkno(fs, uio->uio_offset);
    554 		blkoffset = lfs_blkoff(fs, uio->uio_offset);
    555 		xfersize = MIN(fs->fs_bsize - blkoffset, uio->uio_resid);
    556 		if (fs->fs_bsize > xfersize)
    557 			flags |= B_CLRBUF;
    558 		else
    559 			flags &= ~B_CLRBUF;
    560 
    561 #ifdef LFS_READWRITE
    562 		error = lfs_reserve(fs, vp, NULL,
    563 		    lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
    564 		if (error)
    565 			break;
    566 		need_unreserve = true;
    567 #endif
    568 		error = lfs_balloc(vp, uio->uio_offset, xfersize, cred, flags,
    569 		    &bp);
    570 
    571 		if (error)
    572 			break;
    573 		if (uio->uio_offset + xfersize > ip->i_size) {
    574 			ip->i_size = uio->uio_offset + xfersize;
    575 			DIP_ASSIGN(ip, size, ip->i_size);
    576 			uvm_vnp_setsize(vp, ip->i_size);
    577 			extended = 1;
    578 		}
    579 		size = lfs_blksize(fs, ip, lbn) - bp->b_resid;
    580 		if (xfersize > size)
    581 			xfersize = size;
    582 
    583 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    584 
    585 		/*
    586 		 * if we didn't clear the block and the uiomove failed,
    587 		 * the buf will now contain part of some other file,
    588 		 * so we need to invalidate it.
    589 		 */
    590 		if (error && (flags & B_CLRBUF) == 0) {
    591 			brelse(bp, BC_INVAL);
    592 			break;
    593 		}
    594 #ifdef LFS_READWRITE
    595 		(void)VOP_BWRITE(bp->b_vp, bp);
    596 		lfs_reserve(fs, vp, NULL,
    597 		    -lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
    598 		need_unreserve = false;
    599 #else
    600 		if (ioflag & IO_SYNC)
    601 			(void)bwrite(bp);
    602 		else if (xfersize + blkoffset == fs->fs_bsize)
    603 			bawrite(bp);
    604 		else
    605 			bdwrite(bp);
    606 #endif
    607 		if (error || xfersize == 0)
    608 			break;
    609 	}
    610 #ifdef LFS_READWRITE
    611 	if (need_unreserve) {
    612 		lfs_reserve(fs, vp, NULL,
    613 		    -lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
    614 	}
    615 #endif
    616 
    617 	/*
    618 	 * If we successfully wrote any data, and we are not the superuser
    619 	 * we clear the setuid and setgid bits as a precaution against
    620 	 * tampering.
    621 	 */
    622 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    623 	if (vp->v_mount->mnt_flag & MNT_RELATIME)
    624 		ip->i_flag |= IN_ACCESS;
    625 	if (resid > uio->uio_resid && cred) {
    626 		if (ip->i_mode & ISUID) {
    627 			if (kauth_authorize_vnode(cred,
    628 			    KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0) {
    629 				ip->i_mode &= ~ISUID;
    630 				DIP_ASSIGN(ip, mode, ip->i_mode);
    631 			}
    632 		}
    633 
    634 		if (ip->i_mode & ISGID) {
    635 			if (kauth_authorize_vnode(cred,
    636 			    KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0) {
    637 				ip->i_mode &= ~ISGID;
    638 				DIP_ASSIGN(ip, mode, ip->i_mode);
    639 			}
    640 		}
    641 	}
    642 	if (resid > uio->uio_resid)
    643 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    644 	if (error) {
    645 		(void) lfs_truncate(vp, osize, ioflag & IO_SYNC, cred);
    646 		uio->uio_offset -= resid - uio->uio_resid;
    647 		uio->uio_resid = resid;
    648 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC) {
    649 		error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
    650 	} else {
    651 		/* nothing */
    652 	}
    653 	KASSERT(vp->v_size == ip->i_size);
    654 	fstrans_done(vp->v_mount);
    655 
    656 	return (error);
    657 }
    658