Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_readwrite.c revision 1.72
      1 /*	$NetBSD: ext2fs_readwrite.c,v 1.72 2015/03/28 17:23:42 maxv 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.72 2015/03/28 17:23:42 maxv 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 static int	ext2fs_post_read_update(struct vnode *, int, int);
     85 static int	ext2fs_post_write_update(struct vnode *, struct uio *, int,
     86 		    kauth_cred_t, off_t, int, int, int);
     87 
     88 /*
     89  * Vnode op for reading.
     90  */
     91 /* ARGSUSED */
     92 int
     93 ext2fs_read(void *v)
     94 {
     95 	struct vop_read_args /* {
     96 		struct vnode *a_vp;
     97 		struct uio *a_uio;
     98 		int a_ioflag;
     99 		kauth_cred_t a_cred;
    100 	} */ *ap = v;
    101 	struct vnode *vp;
    102 	struct inode *ip;
    103 	struct uio *uio;
    104 	struct ufsmount *ump;
    105 	vsize_t bytelen;
    106 	int advice;
    107 	int error;
    108 
    109 	vp = ap->a_vp;
    110 	ip = VTOI(vp);
    111 	ump = ip->i_ump;
    112 	uio = ap->a_uio;
    113 	error = 0;
    114 
    115 	KASSERT(uio->uio_rw == UIO_READ);
    116 	KASSERT(vp->v_type == VREG || vp->v_type == VDIR);
    117 
    118 	/* XXX Eliminate me by refusing directory reads from userland.  */
    119 	if (vp->v_type == VDIR)
    120 		return ext2fs_bufrd(vp, uio, ap->a_ioflag, ap->a_cred);
    121 
    122 	if ((uint64_t)uio->uio_offset > ump->um_maxfilesize)
    123 		return (EFBIG);
    124 	if (uio->uio_resid == 0)
    125 		return (0);
    126 	if (uio->uio_offset >= ext2fs_size(ip))
    127 		goto out;
    128 
    129 	KASSERT(vp->v_type == VREG);
    130 	advice = IO_ADV_DECODE(ap->a_ioflag);
    131 	while (uio->uio_resid > 0) {
    132 		bytelen = MIN(ext2fs_size(ip) - uio->uio_offset,
    133 			    uio->uio_resid);
    134 		if (bytelen == 0)
    135 			break;
    136 
    137 		error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
    138 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
    139 		if (error)
    140 			break;
    141 	}
    142 
    143 out:
    144 	error = ext2fs_post_read_update(vp, ap->a_ioflag, error);
    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, 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 	error = ext2fs_post_read_update(vp, ioflag, error);
    231 	return (error);
    232 }
    233 
    234 static int
    235 ext2fs_post_read_update(struct vnode *vp, int ioflag, int error)
    236 {
    237 	struct inode *ip = VTOI(vp);
    238 
    239 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    240 		ip->i_flag |= IN_ACCESS;
    241 		if ((ioflag & IO_SYNC) == IO_SYNC)
    242 			error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
    243 	}
    244 
    245 	return error;
    246 }
    247 
    248 /*
    249  * Vnode op for writing.
    250  */
    251 int
    252 ext2fs_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 	struct m_ext2fs *fs;
    264 	struct ufsmount *ump;
    265 	off_t osize;
    266 	int blkoffset, error, ioflag, resid;
    267 	vsize_t bytelen;
    268 	off_t oldoff = 0;					/* XXX */
    269 	bool async;
    270 	int extended = 0;
    271 	int advice;
    272 
    273 	ioflag = ap->a_ioflag;
    274 	advice = IO_ADV_DECODE(ioflag);
    275 	uio = ap->a_uio;
    276 	vp = ap->a_vp;
    277 	ip = VTOI(vp);
    278 	ump = ip->i_ump;
    279 	error = 0;
    280 
    281 	KASSERT(uio->uio_rw == UIO_WRITE);
    282 	KASSERT(vp->v_type == VREG);
    283 
    284 	if (ioflag & IO_APPEND)
    285 		uio->uio_offset = ext2fs_size(ip);
    286 	if ((ip->i_e2fs_flags & EXT2_APPEND) &&
    287 	    uio->uio_offset != ext2fs_size(ip))
    288 		return (EPERM);
    289 
    290 	fs = ip->i_e2fs;
    291 	if (uio->uio_offset < 0 ||
    292 	    (uint64_t)uio->uio_offset + uio->uio_resid > ump->um_maxfilesize)
    293 		return (EFBIG);
    294 	if (uio->uio_resid == 0)
    295 		return (0);
    296 
    297 	async = vp->v_mount->mnt_flag & MNT_ASYNC;
    298 	resid = uio->uio_resid;
    299 	osize = ext2fs_size(ip);
    300 
    301 	KASSERT(vp->v_type == VREG);
    302 	while (uio->uio_resid > 0) {
    303 		oldoff = uio->uio_offset;
    304 		blkoffset = ext2_blkoff(fs, uio->uio_offset);
    305 		bytelen = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
    306 
    307 		if (vp->v_size < oldoff + bytelen) {
    308 			uvm_vnp_setwritesize(vp, oldoff + bytelen);
    309 		}
    310 		error = ufs_balloc_range(vp, uio->uio_offset, bytelen,
    311 		    ap->a_cred, 0);
    312 		if (error)
    313 			break;
    314 		error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
    315 		    UBC_WRITE | UBC_UNMAP_FLAG(vp));
    316 		if (error)
    317 			break;
    318 
    319 		/*
    320 		 * update UVM's notion of the size now that we've
    321 		 * copied the data into the vnode's pages.
    322 		 */
    323 
    324 		if (vp->v_size < uio->uio_offset) {
    325 			uvm_vnp_setsize(vp, uio->uio_offset);
    326 			extended = 1;
    327 		}
    328 
    329 		/*
    330 		 * flush what we just wrote if necessary.
    331 		 * XXXUBC simplistic async flushing.
    332 		 */
    333 
    334 		if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
    335 			mutex_enter(vp->v_interlock);
    336 			error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
    337 			    (uio->uio_offset >> 16) << 16,
    338 			    PGO_CLEANIT | PGO_LAZY);
    339 		}
    340 	}
    341 	if (error == 0 && ioflag & IO_SYNC) {
    342 		mutex_enter(vp->v_interlock);
    343 		error = VOP_PUTPAGES(vp, trunc_page(oldoff),
    344 		    round_page(ext2_blkroundup(fs, uio->uio_offset)),
    345 		    PGO_CLEANIT | PGO_SYNCIO);
    346 	}
    347 
    348 	error = ext2fs_post_write_update(vp, uio, ioflag, ap->a_cred, osize,
    349 	    resid, extended, error);
    350 	return (error);
    351 }
    352 
    353 /*
    354  * UFS op for writing via the buffer cache
    355  */
    356 int
    357 ext2fs_bufwr(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
    358 {
    359 	struct inode *ip;
    360 	struct ufsmount *ump;
    361 	struct m_ext2fs *fs;
    362 	struct buf *bp;
    363 	int flags;
    364 	off_t osize;
    365 	daddr_t lbn;
    366 	int resid, blkoffset, xfersize;
    367 	int extended = 0;
    368 	int error;
    369 
    370 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
    371 	KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
    372 	KASSERT(vp->v_type != VDIR || ISSET(ioflag, IO_SYNC));
    373 	KASSERT(uio->uio_rw == UIO_WRITE);
    374 
    375 	ip = VTOI(vp);
    376 	ump = ip->i_ump;
    377 	fs = ip->i_e2fs;
    378 	error = 0;
    379 
    380 	if (uio->uio_offset < 0 ||
    381 	    uio->uio_resid > ump->um_maxfilesize ||
    382 	    uio->uio_offset > (ump->um_maxfilesize - uio->uio_resid))
    383 		return EFBIG;
    384 	if (uio->uio_resid == 0)
    385 		return 0;
    386 
    387 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    388 	resid = uio->uio_resid;
    389 	osize = ext2fs_size(ip);
    390 
    391 	for (error = 0; uio->uio_resid > 0;) {
    392 		lbn = ext2_lblkno(fs, uio->uio_offset);
    393 		blkoffset = ext2_blkoff(fs, uio->uio_offset);
    394 		xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
    395 		if (xfersize < fs->e2fs_bsize)
    396 			flags |= B_CLRBUF;
    397 		else
    398 			flags &= ~B_CLRBUF;
    399 		error = ext2fs_balloc(ip, lbn, blkoffset + xfersize, cred, &bp,
    400 		    flags);
    401 		if (error)
    402 			break;
    403 		if (ext2fs_size(ip) < uio->uio_offset + xfersize) {
    404 			error = ext2fs_setsize(ip, uio->uio_offset + xfersize);
    405 			if (error)
    406 				break;
    407 		}
    408 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    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 
    415 		if (vp->v_size < uio->uio_offset) {
    416 			uvm_vnp_setsize(vp, uio->uio_offset);
    417 			extended = 1;
    418 		}
    419 
    420 		if (ioflag & IO_SYNC)
    421 			(void)bwrite(bp);
    422 		else if (xfersize + blkoffset == fs->e2fs_bsize)
    423 			bawrite(bp);
    424 		else
    425 			bdwrite(bp);
    426 		if (error || xfersize == 0)
    427 			break;
    428 	}
    429 
    430 	error = ext2fs_post_write_update(vp, uio, ioflag, cred, osize, resid,
    431 	    extended, error);
    432 	return (error);
    433 }
    434 
    435 static int
    436 ext2fs_post_write_update(struct vnode *vp, struct uio *uio, int ioflag,
    437     kauth_cred_t cred, off_t osize, int resid, int extended, int error)
    438 {
    439 	struct inode *ip = VTOI(vp);
    440 
    441 	/* Trigger ctime and mtime updates, and atime if MNT_RELATIME.  */
    442 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    443 	if (vp->v_mount->mnt_flag & MNT_RELATIME)
    444 		ip->i_flag |= IN_ACCESS;
    445 
    446 	/*
    447 	 * If we successfully wrote any data and we are not the superuser,
    448 	 * we clear the setuid and setgid bits as a precaution against
    449 	 * tampering.
    450 	 */
    451 	if (resid > uio->uio_resid && cred) {
    452 		if (ip->i_e2fs_mode & ISUID) {
    453 			if (kauth_authorize_vnode(cred,
    454 			    KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
    455 				ip->i_e2fs_mode &= ISUID;
    456 		}
    457 
    458 		if (ip->i_e2fs_mode & ISGID) {
    459 			if (kauth_authorize_vnode(cred,
    460 			    KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
    461 				ip->i_e2fs_mode &= ~ISGID;
    462 		}
    463 	}
    464 
    465 	/* If we successfully wrote anything, notify kevent listeners.  */
    466 	if (resid > uio->uio_resid)
    467 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    468 
    469 	/*
    470 	 * Update the size on disk: truncate back to original size on
    471 	 * error, or reflect the new size on success.
    472 	 */
    473 	if (error) {
    474 		(void) ext2fs_truncate(vp, osize, ioflag & IO_SYNC, cred);
    475 		uio->uio_offset -= resid - uio->uio_resid;
    476 		uio->uio_resid = resid;
    477 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
    478 		error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
    479 
    480 	/* Make sure the vnode uvm size matches the inode file size.  */
    481 	KASSERT(vp->v_size == ext2fs_size(ip));
    482 
    483 	return error;
    484 }
    485