Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_readwrite.c revision 1.19
      1 /*	$NetBSD: ext2fs_readwrite.c,v 1.19 2001/10/26 05:56:08 lukem 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/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/resourcevar.h>
     43 #include <sys/kernel.h>
     44 #include <sys/file.h>
     45 #include <sys/stat.h>
     46 #include <sys/buf.h>
     47 #include <sys/proc.h>
     48 #include <sys/conf.h>
     49 #include <sys/mount.h>
     50 #include <sys/vnode.h>
     51 #include <sys/malloc.h>
     52 #include <sys/signalvar.h>
     53 
     54 #include <ufs/ufs/inode.h>
     55 #include <ufs/ext2fs/ext2fs.h>
     56 #include <ufs/ext2fs/ext2fs_extern.h>
     57 
     58 
     59 #define doclusterread 0 /* XXX underway */
     60 #define doclusterwrite 0
     61 
     62 /*
     63  * Vnode op for reading.
     64  */
     65 /* ARGSUSED */
     66 int
     67 ext2fs_read(v)
     68 	void *v;
     69 {
     70 	struct vop_read_args /* {
     71 		struct vnode *a_vp;
     72 		struct uio *a_uio;
     73 		int a_ioflag;
     74 		struct ucred *a_cred;
     75 	} */ *ap = v;
     76 	struct vnode *vp;
     77 	struct inode *ip;
     78 	struct uio *uio;
     79 	struct m_ext2fs *fs;
     80 	struct buf *bp;
     81 	void *win;
     82 	vsize_t bytelen;
     83 	ufs_daddr_t lbn, nextlbn;
     84 	off_t bytesinfile;
     85 	long size, xfersize, blkoffset;
     86 	int error;
     87 
     88 	vp = ap->a_vp;
     89 	ip = VTOI(vp);
     90 	uio = ap->a_uio;
     91 
     92 #ifdef DIAGNOSTIC
     93 	if (uio->uio_rw != UIO_READ)
     94 		panic("%s: mode", "ext2fs_read");
     95 
     96 	if (vp->v_type == VLNK) {
     97 		if ((int)ip->i_e2fs_size < vp->v_mount->mnt_maxsymlinklen ||
     98 			(vp->v_mount->mnt_maxsymlinklen == 0 &&
     99 			 ip->i_e2fs_nblock == 0))
    100 			panic("%s: short symlink", "ext2fs_read");
    101 	} else if (vp->v_type != VREG && vp->v_type != VDIR)
    102 		panic("%s: type %d", "ext2fs_read", vp->v_type);
    103 #endif
    104 	fs = ip->i_e2fs;
    105 	if ((u_int64_t)uio->uio_offset >
    106 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
    107 		return (EFBIG);
    108 	if (uio->uio_resid == 0)
    109 		return (0);
    110 	if (uio->uio_offset >= ip->i_e2fs_size)
    111 		return (0);
    112 
    113 	if (vp->v_type == VREG) {
    114 		error = 0;
    115 		while (uio->uio_resid > 0) {
    116 			bytelen = MIN(ip->i_e2fs_size - uio->uio_offset,
    117 			    uio->uio_resid);
    118 
    119 			if (bytelen == 0) {
    120 				break;
    121 			}
    122 			win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
    123 			    &bytelen, UBC_READ);
    124 			error = uiomove(win, bytelen, uio);
    125 			ubc_release(win, 0);
    126 			if (error) {
    127 				break;
    128 			}
    129 		}
    130 		goto out;
    131 	}
    132 
    133 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
    134 		if ((bytesinfile = ip->i_e2fs_size - uio->uio_offset) <= 0)
    135 			break;
    136 		lbn = lblkno(fs, uio->uio_offset);
    137 		nextlbn = lbn + 1;
    138 		size = fs->e2fs_bsize;
    139 		blkoffset = blkoff(fs, uio->uio_offset);
    140 		xfersize = fs->e2fs_bsize - blkoffset;
    141 		if (uio->uio_resid < xfersize)
    142 			xfersize = uio->uio_resid;
    143 		if (bytesinfile < xfersize)
    144 			xfersize = bytesinfile;
    145 
    146 		if (lblktosize(fs, nextlbn) >= ip->i_e2fs_size)
    147 			error = bread(vp, lbn, size, NOCRED, &bp);
    148 		else {
    149 			int nextsize = fs->e2fs_bsize;
    150 			error = breadn(vp, lbn,
    151 				size, &nextlbn, &nextsize, 1, NOCRED, &bp);
    152 		}
    153 		if (error)
    154 			break;
    155 
    156 		/*
    157 		 * We should only get non-zero b_resid when an I/O error
    158 		 * has occurred, which should cause us to break above.
    159 		 * However, if the short read did not cause an error,
    160 		 * then we want to ensure that we do not uiomove bad
    161 		 * or uninitialized data.
    162 		 */
    163 		size -= bp->b_resid;
    164 		if (size < xfersize) {
    165 			if (size == 0)
    166 				break;
    167 			xfersize = size;
    168 		}
    169 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    170 		if (error)
    171 			break;
    172 		brelse(bp);
    173 	}
    174 	if (bp != NULL)
    175 		brelse(bp);
    176 
    177 out:
    178 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    179 		ip->i_flag |= IN_ACCESS;
    180 		if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
    181 			error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    182 	}
    183 	return (error);
    184 }
    185 
    186 /*
    187  * Vnode op for writing.
    188  */
    189 int
    190 ext2fs_write(v)
    191 	void *v;
    192 {
    193 	struct vop_write_args /* {
    194 		struct vnode *a_vp;
    195 		struct uio *a_uio;
    196 		int a_ioflag;
    197 		struct ucred *a_cred;
    198 	} */ *ap = v;
    199 	struct vnode *vp;
    200 	struct uio *uio;
    201 	struct inode *ip;
    202 	struct m_ext2fs *fs;
    203 	struct buf *bp;
    204 	struct proc *p;
    205 	ufs_daddr_t lbn;
    206 	off_t osize;
    207 	int blkoffset, error, flags, ioflag, resid, xfersize;
    208 	vsize_t bytelen;
    209 	void *win;
    210 	off_t oldoff;
    211 
    212 	ioflag = ap->a_ioflag;
    213 	uio = ap->a_uio;
    214 	vp = ap->a_vp;
    215 	ip = VTOI(vp);
    216 	error = 0;
    217 
    218 #ifdef DIAGNOSTIC
    219 	if (uio->uio_rw != UIO_WRITE)
    220 		panic("%s: mode", "ext2fs_write");
    221 #endif
    222 
    223 	switch (vp->v_type) {
    224 	case VREG:
    225 		if (ioflag & IO_APPEND)
    226 			uio->uio_offset = ip->i_e2fs_size;
    227 		if ((ip->i_e2fs_flags & EXT2_APPEND) &&
    228 			uio->uio_offset != ip->i_e2fs_size)
    229 			return (EPERM);
    230 		/* FALLTHROUGH */
    231 	case VLNK:
    232 		break;
    233 	case VDIR:
    234 		if ((ioflag & IO_SYNC) == 0)
    235 			panic("%s: nonsync dir write", "ext2fs_write");
    236 		break;
    237 	default:
    238 		panic("%s: type", "ext2fs_write");
    239 	}
    240 
    241 	fs = ip->i_e2fs;
    242 	if (uio->uio_offset < 0 ||
    243 		(u_int64_t)uio->uio_offset + uio->uio_resid >
    244 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
    245 		return (EFBIG);
    246 	/*
    247 	 * Maybe this should be above the vnode op call, but so long as
    248 	 * file servers have no limits, I don't think it matters.
    249 	 */
    250 	p = uio->uio_procp;
    251 	if (vp->v_type == VREG && p &&
    252 		uio->uio_offset + uio->uio_resid >
    253 		p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    254 		psignal(p, SIGXFSZ);
    255 		return (EFBIG);
    256 	}
    257 
    258 	resid = uio->uio_resid;
    259 	osize = ip->i_e2fs_size;
    260 
    261 	if (vp->v_type == VREG) {
    262 		while (uio->uio_resid > 0) {
    263 			oldoff = uio->uio_offset;
    264 			blkoffset = blkoff(fs, uio->uio_offset);
    265 			bytelen = MIN(fs->e2fs_bsize - blkoffset,
    266 			    uio->uio_resid);
    267 
    268 			error = ext2fs_balloc_range(vp, uio->uio_offset,
    269 			    bytelen, ap->a_cred, 0);
    270 			if (error) {
    271 				break;
    272 			}
    273 			win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
    274 			    &bytelen, UBC_WRITE);
    275 			error = uiomove(win, bytelen, uio);
    276 			ubc_release(win, 0);
    277 			if (error) {
    278 				break;
    279 			}
    280 
    281 			/*
    282 			 * flush what we just wrote if necessary.
    283 			 * XXXUBC simplistic async flushing.
    284 			 */
    285 
    286 			if (oldoff >> 16 != uio->uio_offset >> 16) {
    287 				simple_lock(&vp->v_uobj.vmobjlock);
    288 				error = vp->v_uobj.pgops->pgo_put(
    289 				    &vp->v_uobj, (oldoff >> 16) << 16,
    290 				    (uio->uio_offset >> 16) << 16, PGO_CLEANIT);
    291 			}
    292 		}
    293 		if (error == 0 && ioflag & IO_SYNC) {
    294 			simple_lock(&vp->v_uobj.vmobjlock);
    295 			error = vp->v_uobj.pgops->pgo_put(&vp->v_uobj, oldoff,
    296 			    oldoff + bytelen, PGO_CLEANIT|PGO_SYNCIO);
    297 		}
    298 
    299 		goto out;
    300 	}
    301 
    302 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    303 	for (error = 0; uio->uio_resid > 0;) {
    304 		lbn = lblkno(fs, uio->uio_offset);
    305 		blkoffset = blkoff(fs, uio->uio_offset);
    306 		xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
    307 		if (xfersize < fs->e2fs_bsize)
    308 			flags |= B_CLRBUF;
    309 		else
    310 			flags &= ~B_CLRBUF;
    311 		error = ext2fs_balloc(ip,
    312 		    lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
    313 		if (error)
    314 			break;
    315 		if (ip->i_e2fs_size < uio->uio_offset + xfersize) {
    316 			ip->i_e2fs_size = uio->uio_offset + xfersize;
    317 		}
    318 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    319 		if (ioflag & IO_SYNC)
    320 			(void)bwrite(bp);
    321 		else if (xfersize + blkoffset == fs->e2fs_bsize)
    322 			bawrite(bp);
    323 		else
    324 			bdwrite(bp);
    325 		if (error || xfersize == 0)
    326 			break;
    327 	}
    328 	/*
    329 	 * If we successfully wrote any data, and we are not the superuser
    330 	 * we clear the setuid and setgid bits as a precaution against
    331 	 * tampering.
    332 	 */
    333 out:
    334 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    335 	if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
    336 		ip->i_e2fs_mode &= ~(ISUID | ISGID);
    337 	if (error) {
    338 		(void) VOP_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred,
    339 		    uio->uio_procp);
    340 		uio->uio_offset -= resid - uio->uio_resid;
    341 		uio->uio_resid = resid;
    342 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
    343 		error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    344 	KASSERT(vp->v_size == ip->i_e2fs_size);
    345 	return (error);
    346 }
    347