Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_readwrite.c revision 1.70
      1 /*	$NetBSD: ext2fs_readwrite.c,v 1.70 2015/03/28 03:53:36 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)ufs_readwrite.c	8.8 (Berkeley) 8/4/94
     32  * Modified for ext2fs by Manuel Bouyer.
     33  */
     34 
     35 /*-
     36  * Copyright (c) 1997 Manuel Bouyer.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  *
     58  *	@(#)ufs_readwrite.c	8.8 (Berkeley) 8/4/94
     59  * Modified for ext2fs by Manuel Bouyer.
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.70 2015/03/28 03:53:36 riastradh Exp $");
     64 
     65 #include <sys/param.h>
     66 #include <sys/systm.h>
     67 #include <sys/resourcevar.h>
     68 #include <sys/kernel.h>
     69 #include <sys/file.h>
     70 #include <sys/stat.h>
     71 #include <sys/buf.h>
     72 #include <sys/proc.h>
     73 #include <sys/mount.h>
     74 #include <sys/vnode.h>
     75 #include <sys/signalvar.h>
     76 #include <sys/kauth.h>
     77 
     78 #include <ufs/ufs/inode.h>
     79 #include <ufs/ufs/ufsmount.h>
     80 #include <ufs/ufs/ufs_extern.h>
     81 #include <ufs/ext2fs/ext2fs.h>
     82 #include <ufs/ext2fs/ext2fs_extern.h>
     83 
     84 /*
     85  * Vnode op for reading.
     86  */
     87 /* ARGSUSED */
     88 int
     89 ext2fs_read(void *v)
     90 {
     91 	struct vop_read_args /* {
     92 		struct vnode *a_vp;
     93 		struct uio *a_uio;
     94 		int a_ioflag;
     95 		kauth_cred_t a_cred;
     96 	} */ *ap = v;
     97 	struct vnode *vp;
     98 	struct inode *ip;
     99 	struct uio *uio;
    100 	struct ufsmount *ump;
    101 	vsize_t bytelen;
    102 	int advice;
    103 	int error;
    104 
    105 	vp = ap->a_vp;
    106 	ip = VTOI(vp);
    107 	ump = ip->i_ump;
    108 	uio = ap->a_uio;
    109 	error = 0;
    110 
    111 	KASSERT(uio->uio_rw == UIO_READ);
    112 	KASSERT(vp->v_type == VREG || vp->v_type == VDIR);
    113 
    114 	/* XXX Eliminate me by refusing directory reads from userland.  */
    115 	if (vp->v_type == VDIR)
    116 		return ext2fs_bufrd(vp, uio, ap->a_ioflag, ap->a_cred);
    117 
    118 	if ((uint64_t)uio->uio_offset > ump->um_maxfilesize)
    119 		return (EFBIG);
    120 	if (uio->uio_resid == 0)
    121 		return (0);
    122 	if (uio->uio_offset >= ext2fs_size(ip))
    123 		goto out;
    124 
    125 	KASSERT(vp->v_type == VREG);
    126 	advice = IO_ADV_DECODE(ap->a_ioflag);
    127 	while (uio->uio_resid > 0) {
    128 		bytelen = MIN(ext2fs_size(ip) - uio->uio_offset,
    129 			    uio->uio_resid);
    130 		if (bytelen == 0)
    131 			break;
    132 
    133 		error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
    134 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
    135 		if (error)
    136 			break;
    137 	}
    138 
    139 out:
    140 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    141 		ip->i_flag |= IN_ACCESS;
    142 		if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
    143 			error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
    144 	}
    145 	return (error);
    146 }
    147 
    148 /*
    149  * UFS op for reading via the buffer cache
    150  */
    151 int
    152 ext2fs_bufrd(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
    153 {
    154 	struct inode *ip;
    155 	struct ufsmount *ump;
    156 	struct m_ext2fs *fs;
    157 	struct buf *bp;
    158 	off_t bytesinfile;
    159 	daddr_t lbn, nextlbn;
    160 	long size, xfersize, blkoffset;
    161 	int error;
    162 
    163 	KASSERT(uio->uio_rw == UIO_READ);
    164 	KASSERT(VOP_ISLOCKED(vp));
    165 	KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
    166 
    167 	ip = VTOI(vp);
    168 	ump = ip->i_ump;
    169 	fs = ip->i_e2fs;
    170 	error = 0;
    171 
    172 	KASSERT(vp->v_type != VLNK ||
    173 	    ext2fs_size(ip) >= ump->um_maxsymlinklen);
    174 	KASSERT(vp->v_type != VLNK || ump->um_maxsymlinklen != 0 ||
    175 	    ext2fs_nblock(ip) != 0);
    176 
    177 	if (uio->uio_offset > ump->um_maxfilesize)
    178 		return EFBIG;
    179 	if (uio->uio_resid == 0)
    180 		return 0;
    181 	if (uio->uio_offset >= ext2fs_size(ip))
    182 		goto out;
    183 
    184 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
    185 		bytesinfile = ext2fs_size(ip) - uio->uio_offset;
    186 		if (bytesinfile <= 0)
    187 			break;
    188 		lbn = ext2_lblkno(fs, uio->uio_offset);
    189 		nextlbn = lbn + 1;
    190 		size = fs->e2fs_bsize;
    191 		blkoffset = ext2_blkoff(fs, uio->uio_offset);
    192 		xfersize = fs->e2fs_bsize - blkoffset;
    193 		if (uio->uio_resid < xfersize)
    194 			xfersize = uio->uio_resid;
    195 		if (bytesinfile < xfersize)
    196 			xfersize = bytesinfile;
    197 
    198 		if (ext2_lblktosize(fs, nextlbn) >= ext2fs_size(ip))
    199 			error = bread(vp, lbn, size, NOCRED, 0, &bp);
    200 		else {
    201 			int nextsize = fs->e2fs_bsize;
    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 = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
    234 	}
    235 	return (error);
    236 }
    237 
    238 /*
    239  * Vnode op for writing.
    240  */
    241 int
    242 ext2fs_write(void *v)
    243 {
    244 	struct vop_write_args /* {
    245 		struct vnode *a_vp;
    246 		struct uio *a_uio;
    247 		int a_ioflag;
    248 		kauth_cred_t a_cred;
    249 	} */ *ap = v;
    250 	struct vnode *vp;
    251 	struct uio *uio;
    252 	struct inode *ip;
    253 	struct m_ext2fs *fs;
    254 	struct ufsmount *ump;
    255 	off_t osize;
    256 	int blkoffset, error, ioflag, resid;
    257 	vsize_t bytelen;
    258 	off_t oldoff = 0;					/* XXX */
    259 	bool async;
    260 	int extended = 0;
    261 	int advice;
    262 
    263 	ioflag = ap->a_ioflag;
    264 	advice = IO_ADV_DECODE(ioflag);
    265 	uio = ap->a_uio;
    266 	vp = ap->a_vp;
    267 	ip = VTOI(vp);
    268 	ump = ip->i_ump;
    269 	error = 0;
    270 
    271 	KASSERT(uio->uio_rw == UIO_WRITE);
    272 	KASSERT(vp->v_type == VREG);
    273 
    274 	if (ioflag & IO_APPEND)
    275 		uio->uio_offset = ext2fs_size(ip);
    276 	if ((ip->i_e2fs_flags & EXT2_APPEND) &&
    277 	    uio->uio_offset != ext2fs_size(ip))
    278 		return (EPERM);
    279 
    280 	fs = ip->i_e2fs;
    281 	if (uio->uio_offset < 0 ||
    282 	    (uint64_t)uio->uio_offset + uio->uio_resid > ump->um_maxfilesize)
    283 		return (EFBIG);
    284 	if (uio->uio_resid == 0)
    285 		return (0);
    286 
    287 	async = vp->v_mount->mnt_flag & MNT_ASYNC;
    288 	resid = uio->uio_resid;
    289 	osize = ext2fs_size(ip);
    290 
    291 	KASSERT(vp->v_type == VREG);
    292 	while (uio->uio_resid > 0) {
    293 		oldoff = uio->uio_offset;
    294 		blkoffset = ext2_blkoff(fs, uio->uio_offset);
    295 		bytelen = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
    296 
    297 		if (vp->v_size < oldoff + bytelen) {
    298 			uvm_vnp_setwritesize(vp, oldoff + bytelen);
    299 		}
    300 		error = ufs_balloc_range(vp, uio->uio_offset, bytelen,
    301 		    ap->a_cred, 0);
    302 		if (error)
    303 			break;
    304 		error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
    305 		    UBC_WRITE | UBC_UNMAP_FLAG(vp));
    306 		if (error)
    307 			break;
    308 
    309 		/*
    310 		 * update UVM's notion of the size now that we've
    311 		 * copied the data into the vnode's pages.
    312 		 */
    313 
    314 		if (vp->v_size < uio->uio_offset) {
    315 			uvm_vnp_setsize(vp, uio->uio_offset);
    316 			extended = 1;
    317 		}
    318 
    319 		/*
    320 		 * flush what we just wrote if necessary.
    321 		 * XXXUBC simplistic async flushing.
    322 		 */
    323 
    324 		if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
    325 			mutex_enter(vp->v_interlock);
    326 			error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
    327 			    (uio->uio_offset >> 16) << 16,
    328 			    PGO_CLEANIT | PGO_LAZY);
    329 		}
    330 	}
    331 	if (error == 0 && ioflag & IO_SYNC) {
    332 		mutex_enter(vp->v_interlock);
    333 		error = VOP_PUTPAGES(vp, trunc_page(oldoff),
    334 		    round_page(ext2_blkroundup(fs, uio->uio_offset)),
    335 		    PGO_CLEANIT | PGO_SYNCIO);
    336 	}
    337 
    338 	/*
    339 	 * If we successfully wrote any data, and we are not the superuser
    340 	 * we clear the setuid and setgid bits as a precaution against
    341 	 * tampering.
    342 	 */
    343 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    344 	if (vp->v_mount->mnt_flag & MNT_RELATIME)
    345 		ip->i_flag |= IN_ACCESS;
    346 	if (resid > uio->uio_resid && ap->a_cred) {
    347 		if (ip->i_e2fs_mode & ISUID) {
    348 			if (kauth_authorize_vnode(ap->a_cred,
    349 			    KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
    350 				ip->i_e2fs_mode &= ISUID;
    351 		}
    352 
    353 		if (ip->i_e2fs_mode & ISGID) {
    354 			if (kauth_authorize_vnode(ap->a_cred,
    355 			    KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
    356 				ip->i_e2fs_mode &= ~ISGID;
    357 		}
    358 	}
    359 	if (resid > uio->uio_resid)
    360 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    361 	if (error) {
    362 		(void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred);
    363 		uio->uio_offset -= resid - uio->uio_resid;
    364 		uio->uio_resid = resid;
    365 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
    366 		error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
    367 	KASSERT(vp->v_size == ext2fs_size(ip));
    368 	return (error);
    369 }
    370 
    371 /*
    372  * UFS op for writing via the buffer cache
    373  */
    374 int
    375 ext2fs_bufwr(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
    376 {
    377 	struct inode *ip;
    378 	struct ufsmount *ump;
    379 	struct m_ext2fs *fs;
    380 	struct buf *bp;
    381 	int flags;
    382 	off_t osize;
    383 	daddr_t lbn;
    384 	int resid, blkoffset, xfersize;
    385 	int extended = 0;
    386 	int error;
    387 
    388 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    389 	KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
    390 	KASSERT(vp->v_type != VDIR || ISSET(ioflag, IO_SYNC));
    391 	KASSERT(uio->uio_rw == UIO_WRITE);
    392 
    393 	ip = VTOI(vp);
    394 	ump = ip->i_ump;
    395 	fs = ip->i_e2fs;
    396 	error = 0;
    397 
    398 	if (uio->uio_offset < 0 ||
    399 	    uio->uio_resid > ump->um_maxfilesize ||
    400 	    uio->uio_offset > (ump->um_maxfilesize - uio->uio_resid))
    401 		return EFBIG;
    402 	if (uio->uio_resid == 0)
    403 		return 0;
    404 
    405 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    406 	resid = uio->uio_resid;
    407 	osize = ext2fs_size(ip);
    408 
    409 	for (error = 0; uio->uio_resid > 0;) {
    410 		lbn = ext2_lblkno(fs, uio->uio_offset);
    411 		blkoffset = ext2_blkoff(fs, uio->uio_offset);
    412 		xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
    413 		if (xfersize < fs->e2fs_bsize)
    414 			flags |= B_CLRBUF;
    415 		else
    416 			flags &= ~B_CLRBUF;
    417 		error = ext2fs_balloc(ip, lbn, blkoffset + xfersize, cred, &bp,
    418 		    flags);
    419 		if (error)
    420 			break;
    421 		if (ext2fs_size(ip) < uio->uio_offset + xfersize) {
    422 			error = ext2fs_setsize(ip, uio->uio_offset + xfersize);
    423 			if (error)
    424 				break;
    425 		}
    426 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    427 
    428 		/*
    429 		 * update UVM's notion of the size now that we've
    430 		 * copied the data into the vnode's pages.
    431 		 */
    432 
    433 		if (vp->v_size < uio->uio_offset) {
    434 			uvm_vnp_setsize(vp, uio->uio_offset);
    435 			extended = 1;
    436 		}
    437 
    438 		if (ioflag & IO_SYNC)
    439 			(void)bwrite(bp);
    440 		else if (xfersize + blkoffset == fs->e2fs_bsize)
    441 			bawrite(bp);
    442 		else
    443 			bdwrite(bp);
    444 		if (error || xfersize == 0)
    445 			break;
    446 	}
    447 
    448 	/*
    449 	 * If we successfully wrote any data, and we are not the superuser
    450 	 * we clear the setuid and setgid bits as a precaution against
    451 	 * tampering.
    452 	 */
    453 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    454 	if (vp->v_mount->mnt_flag & MNT_RELATIME)
    455 		ip->i_flag |= IN_ACCESS;
    456 	if (resid > uio->uio_resid && cred) {
    457 		if (ip->i_e2fs_mode & ISUID) {
    458 			if (kauth_authorize_vnode(cred,
    459 			    KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
    460 				ip->i_e2fs_mode &= ISUID;
    461 		}
    462 
    463 		if (ip->i_e2fs_mode & ISGID) {
    464 			if (kauth_authorize_vnode(cred,
    465 			    KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
    466 				ip->i_e2fs_mode &= ~ISGID;
    467 		}
    468 	}
    469 	if (resid > uio->uio_resid)
    470 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    471 	if (error) {
    472 		(void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, cred);
    473 		uio->uio_offset -= resid - uio->uio_resid;
    474 		uio->uio_resid = resid;
    475 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
    476 		error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
    477 	KASSERT(vp->v_size == ext2fs_size(ip));
    478 	return (error);
    479 }
    480