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