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