Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_readwrite.c revision 1.16.2.3
      1 /*	$NetBSD: ext2fs_readwrite.c,v 1.16.2.3 2001/11/14 19:18:53 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 Manuel Bouyer.
      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. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)ufs_readwrite.c	8.8 (Berkeley) 8/4/94
     37  * Modified for ext2fs by Manuel Bouyer.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.16.2.3 2001/11/14 19:18:53 nathanw Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/resourcevar.h>
     46 #include <sys/kernel.h>
     47 #include <sys/file.h>
     48 #include <sys/stat.h>
     49 #include <sys/buf.h>
     50 #include <sys/proc.h>
     51 #include <sys/conf.h>
     52 #include <sys/mount.h>
     53 #include <sys/vnode.h>
     54 #include <sys/malloc.h>
     55 #include <sys/signalvar.h>
     56 
     57 #include <ufs/ufs/inode.h>
     58 #include <ufs/ext2fs/ext2fs.h>
     59 #include <ufs/ext2fs/ext2fs_extern.h>
     60 
     61 
     62 #define doclusterread 0 /* XXX underway */
     63 #define doclusterwrite 0
     64 
     65 /*
     66  * Vnode op for reading.
     67  */
     68 /* ARGSUSED */
     69 int
     70 ext2fs_read(v)
     71 	void *v;
     72 {
     73 	struct vop_read_args /* {
     74 		struct vnode *a_vp;
     75 		struct uio *a_uio;
     76 		int a_ioflag;
     77 		struct ucred *a_cred;
     78 	} */ *ap = v;
     79 	struct vnode *vp;
     80 	struct inode *ip;
     81 	struct uio *uio;
     82 	struct m_ext2fs *fs;
     83 	struct buf *bp;
     84 	void *win;
     85 	vsize_t bytelen;
     86 	ufs_daddr_t lbn, nextlbn;
     87 	off_t bytesinfile;
     88 	long size, xfersize, blkoffset;
     89 	int error;
     90 
     91 	vp = ap->a_vp;
     92 	ip = VTOI(vp);
     93 	uio = ap->a_uio;
     94 
     95 #ifdef DIAGNOSTIC
     96 	if (uio->uio_rw != UIO_READ)
     97 		panic("%s: mode", "ext2fs_read");
     98 
     99 	if (vp->v_type == VLNK) {
    100 		if ((int)ip->i_e2fs_size < vp->v_mount->mnt_maxsymlinklen ||
    101 			(vp->v_mount->mnt_maxsymlinklen == 0 &&
    102 			 ip->i_e2fs_nblock == 0))
    103 			panic("%s: short symlink", "ext2fs_read");
    104 	} else if (vp->v_type != VREG && vp->v_type != VDIR)
    105 		panic("%s: type %d", "ext2fs_read", vp->v_type);
    106 #endif
    107 	fs = ip->i_e2fs;
    108 	if ((u_int64_t)uio->uio_offset >
    109 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
    110 		return (EFBIG);
    111 	if (uio->uio_resid == 0)
    112 		return (0);
    113 	if (uio->uio_offset >= ip->i_e2fs_size)
    114 		return (0);
    115 
    116 	if (vp->v_type == VREG) {
    117 		error = 0;
    118 		while (uio->uio_resid > 0) {
    119 			bytelen = MIN(ip->i_e2fs_size - uio->uio_offset,
    120 			    uio->uio_resid);
    121 
    122 			if (bytelen == 0) {
    123 				break;
    124 			}
    125 			win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
    126 			    &bytelen, UBC_READ);
    127 			error = uiomove(win, bytelen, uio);
    128 			ubc_release(win, 0);
    129 			if (error) {
    130 				break;
    131 			}
    132 		}
    133 		goto out;
    134 	}
    135 
    136 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
    137 		if ((bytesinfile = ip->i_e2fs_size - uio->uio_offset) <= 0)
    138 			break;
    139 		lbn = lblkno(fs, uio->uio_offset);
    140 		nextlbn = lbn + 1;
    141 		size = fs->e2fs_bsize;
    142 		blkoffset = blkoff(fs, uio->uio_offset);
    143 		xfersize = fs->e2fs_bsize - blkoffset;
    144 		if (uio->uio_resid < xfersize)
    145 			xfersize = uio->uio_resid;
    146 		if (bytesinfile < xfersize)
    147 			xfersize = bytesinfile;
    148 
    149 		if (lblktosize(fs, nextlbn) >= ip->i_e2fs_size)
    150 			error = bread(vp, lbn, size, NOCRED, &bp);
    151 		else {
    152 			int nextsize = fs->e2fs_bsize;
    153 			error = breadn(vp, lbn,
    154 				size, &nextlbn, &nextsize, 1, NOCRED, &bp);
    155 		}
    156 		if (error)
    157 			break;
    158 
    159 		/*
    160 		 * We should only get non-zero b_resid when an I/O error
    161 		 * has occurred, which should cause us to break above.
    162 		 * However, if the short read did not cause an error,
    163 		 * then we want to ensure that we do not uiomove bad
    164 		 * or uninitialized data.
    165 		 */
    166 		size -= bp->b_resid;
    167 		if (size < xfersize) {
    168 			if (size == 0)
    169 				break;
    170 			xfersize = size;
    171 		}
    172 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    173 		if (error)
    174 			break;
    175 		brelse(bp);
    176 	}
    177 	if (bp != NULL)
    178 		brelse(bp);
    179 
    180 out:
    181 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    182 		ip->i_flag |= IN_ACCESS;
    183 		if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
    184 			error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    185 	}
    186 	return (error);
    187 }
    188 
    189 /*
    190  * Vnode op for writing.
    191  */
    192 int
    193 ext2fs_write(v)
    194 	void *v;
    195 {
    196 	struct vop_write_args /* {
    197 		struct vnode *a_vp;
    198 		struct uio *a_uio;
    199 		int a_ioflag;
    200 		struct ucred *a_cred;
    201 	} */ *ap = v;
    202 	struct vnode *vp;
    203 	struct uio *uio;
    204 	struct inode *ip;
    205 	struct m_ext2fs *fs;
    206 	struct buf *bp;
    207 	struct proc *p;
    208 	ufs_daddr_t lbn;
    209 	off_t osize;
    210 	int blkoffset, error, flags, ioflag, resid, xfersize;
    211 	vsize_t bytelen;
    212 	void *win;
    213 	off_t oldoff;
    214 
    215 	ioflag = ap->a_ioflag;
    216 	uio = ap->a_uio;
    217 	vp = ap->a_vp;
    218 	ip = VTOI(vp);
    219 	error = 0;
    220 
    221 #ifdef DIAGNOSTIC
    222 	if (uio->uio_rw != UIO_WRITE)
    223 		panic("%s: mode", "ext2fs_write");
    224 #endif
    225 
    226 	switch (vp->v_type) {
    227 	case VREG:
    228 		if (ioflag & IO_APPEND)
    229 			uio->uio_offset = ip->i_e2fs_size;
    230 		if ((ip->i_e2fs_flags & EXT2_APPEND) &&
    231 			uio->uio_offset != ip->i_e2fs_size)
    232 			return (EPERM);
    233 		/* FALLTHROUGH */
    234 	case VLNK:
    235 		break;
    236 	case VDIR:
    237 		if ((ioflag & IO_SYNC) == 0)
    238 			panic("%s: nonsync dir write", "ext2fs_write");
    239 		break;
    240 	default:
    241 		panic("%s: type", "ext2fs_write");
    242 	}
    243 
    244 	fs = ip->i_e2fs;
    245 	if (uio->uio_offset < 0 ||
    246 		(u_int64_t)uio->uio_offset + uio->uio_resid >
    247 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
    248 		return (EFBIG);
    249 	/*
    250 	 * Maybe this should be above the vnode op call, but so long as
    251 	 * file servers have no limits, I don't think it matters.
    252 	 */
    253 	p = uio->uio_procp;
    254 	if (vp->v_type == VREG && p &&
    255 		uio->uio_offset + uio->uio_resid >
    256 		p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    257 		psignal(p, SIGXFSZ);
    258 		return (EFBIG);
    259 	}
    260 
    261 	resid = uio->uio_resid;
    262 	osize = ip->i_e2fs_size;
    263 
    264 	if (vp->v_type == VREG) {
    265 		while (uio->uio_resid > 0) {
    266 			oldoff = uio->uio_offset;
    267 			blkoffset = blkoff(fs, uio->uio_offset);
    268 			bytelen = MIN(fs->e2fs_bsize - blkoffset,
    269 			    uio->uio_resid);
    270 
    271 			error = ext2fs_balloc_range(vp, uio->uio_offset,
    272 			    bytelen, ap->a_cred, 0);
    273 			if (error) {
    274 				break;
    275 			}
    276 			win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
    277 			    &bytelen, UBC_WRITE);
    278 			error = uiomove(win, bytelen, uio);
    279 			ubc_release(win, 0);
    280 			if (error) {
    281 				break;
    282 			}
    283 
    284 			/*
    285 			 * update UVM's notion of the size now that we've
    286 			 * copied the data into the vnode's pages.
    287 			 */
    288 
    289 			if (vp->v_size < uio->uio_offset) {
    290 				uvm_vnp_setsize(vp, uio->uio_offset);
    291 			}
    292 
    293 			/*
    294 			 * flush what we just wrote if necessary.
    295 			 * XXXUBC simplistic async flushing.
    296 			 */
    297 
    298 			if (oldoff >> 16 != uio->uio_offset >> 16) {
    299 				simple_lock(&vp->v_uobj.vmobjlock);
    300 				error = vp->v_uobj.pgops->pgo_put(
    301 				    &vp->v_uobj, (oldoff >> 16) << 16,
    302 				    (uio->uio_offset >> 16) << 16, PGO_CLEANIT);
    303 			}
    304 		}
    305 		if (error == 0 && ioflag & IO_SYNC) {
    306 			simple_lock(&vp->v_uobj.vmobjlock);
    307 			error = vp->v_uobj.pgops->pgo_put(&vp->v_uobj, oldoff,
    308 			    oldoff + bytelen, PGO_CLEANIT|PGO_SYNCIO);
    309 		}
    310 
    311 		goto out;
    312 	}
    313 
    314 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    315 	for (error = 0; uio->uio_resid > 0;) {
    316 		lbn = lblkno(fs, uio->uio_offset);
    317 		blkoffset = blkoff(fs, uio->uio_offset);
    318 		xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
    319 		if (xfersize < fs->e2fs_bsize)
    320 			flags |= B_CLRBUF;
    321 		else
    322 			flags &= ~B_CLRBUF;
    323 		error = ext2fs_balloc(ip,
    324 		    lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
    325 		if (error)
    326 			break;
    327 		if (ip->i_e2fs_size < uio->uio_offset + xfersize) {
    328 			ip->i_e2fs_size = uio->uio_offset + xfersize;
    329 		}
    330 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    331 
    332 		/*
    333 		 * update UVM's notion of the size now that we've
    334 		 * copied the data into the vnode's pages.
    335 		 */
    336 
    337 		if (vp->v_size < uio->uio_offset) {
    338 			uvm_vnp_setsize(vp, uio->uio_offset);
    339 		}
    340 
    341 		if (ioflag & IO_SYNC)
    342 			(void)bwrite(bp);
    343 		else if (xfersize + blkoffset == fs->e2fs_bsize)
    344 			bawrite(bp);
    345 		else
    346 			bdwrite(bp);
    347 		if (error || xfersize == 0)
    348 			break;
    349 	}
    350 
    351 	/*
    352 	 * If we successfully wrote any data, and we are not the superuser
    353 	 * we clear the setuid and setgid bits as a precaution against
    354 	 * tampering.
    355 	 */
    356 
    357 out:
    358 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    359 	if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
    360 		ip->i_e2fs_mode &= ~(ISUID | ISGID);
    361 	if (error) {
    362 		(void) VOP_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred,
    363 		    uio->uio_procp);
    364 		uio->uio_offset -= resid - uio->uio_resid;
    365 		uio->uio_resid = resid;
    366 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
    367 		error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    368 	KASSERT(vp->v_size == ip->i_e2fs_size);
    369 	return (error);
    370 }
    371