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