Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_readwrite.c revision 1.15
      1 /*	$NetBSD: ext2fs_readwrite.c,v 1.15 2000/12/01 07:02:40 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 
    112 	if (vp->v_type == VREG) {
    113 		error = 0;
    114 		while (uio->uio_resid > 0) {
    115 
    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_uvm.u_obj, 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 if (doclusterread)
    149 			error = cluster_read(vp,
    150 				ip->i_e2fs_size, lbn, size, NOCRED, &bp);
    151 		else if (lbn - 1 == vp->v_lastr) {
    152 			int nextsize = fs->e2fs_bsize;
    153 			error = breadn(vp, lbn,
    154 				size, &nextlbn, &nextsize, 1, NOCRED, &bp);
    155 		} else
    156 			error = bread(vp, lbn, size, NOCRED, &bp);
    157 		if (error)
    158 			break;
    159 		vp->v_lastr = lbn;
    160 
    161 		/*
    162 		 * We should only get non-zero b_resid when an I/O error
    163 		 * has occurred, which should cause us to break above.
    164 		 * However, if the short read did not cause an error,
    165 		 * then we want to ensure that we do not uiomove bad
    166 		 * or uninitialized data.
    167 		 */
    168 		size -= bp->b_resid;
    169 		if (size < xfersize) {
    170 			if (size == 0)
    171 				break;
    172 			xfersize = size;
    173 		}
    174 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    175 		if (error)
    176 			break;
    177 		brelse(bp);
    178 	}
    179 	if (bp != NULL)
    180 		brelse(bp);
    181 
    182 out:
    183 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    184 		ip->i_flag |= IN_ACCESS;
    185 		if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
    186 			error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    187 	}
    188 	return (error);
    189 }
    190 
    191 /*
    192  * Vnode op for writing.
    193  */
    194 int
    195 ext2fs_write(v)
    196 	void *v;
    197 {
    198 	struct vop_write_args /* {
    199 		struct vnode *a_vp;
    200 		struct uio *a_uio;
    201 		int a_ioflag;
    202 		struct ucred *a_cred;
    203 	} */ *ap = v;
    204 	struct vnode *vp;
    205 	struct uio *uio;
    206 	struct inode *ip;
    207 	struct m_ext2fs *fs;
    208 	struct buf *bp;
    209 	struct proc *p;
    210 	ufs_daddr_t lbn;
    211 	off_t osize;
    212 	int blkoffset, error, flags, ioflag, resid, xfersize;
    213 	vsize_t bytelen;
    214 	void *win;
    215 	off_t oldoff;
    216 	boolean_t rv;
    217 
    218 	ioflag = ap->a_ioflag;
    219 	uio = ap->a_uio;
    220 	vp = ap->a_vp;
    221 	ip = VTOI(vp);
    222 	error = 0;
    223 
    224 #ifdef DIAGNOSTIC
    225 	if (uio->uio_rw != UIO_WRITE)
    226 		panic("%s: mode", "ext2fs_write");
    227 #endif
    228 
    229 	switch (vp->v_type) {
    230 	case VREG:
    231 		if (ioflag & IO_APPEND)
    232 			uio->uio_offset = ip->i_e2fs_size;
    233 		if ((ip->i_e2fs_flags & EXT2_APPEND) &&
    234 			uio->uio_offset != ip->i_e2fs_size)
    235 			return (EPERM);
    236 		/* FALLTHROUGH */
    237 	case VLNK:
    238 		break;
    239 	case VDIR:
    240 		if ((ioflag & IO_SYNC) == 0)
    241 			panic("%s: nonsync dir write", "ext2fs_write");
    242 		break;
    243 	default:
    244 		panic("%s: type", "ext2fs_write");
    245 	}
    246 
    247 	fs = ip->i_e2fs;
    248 	if (uio->uio_offset < 0 ||
    249 		(u_int64_t)uio->uio_offset + uio->uio_resid >
    250 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
    251 		return (EFBIG);
    252 	/*
    253 	 * Maybe this should be above the vnode op call, but so long as
    254 	 * file servers have no limits, I don't think it matters.
    255 	 */
    256 	p = uio->uio_procp;
    257 	if (vp->v_type == VREG && p &&
    258 		uio->uio_offset + uio->uio_resid >
    259 		p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    260 		psignal(p, SIGXFSZ);
    261 		return (EFBIG);
    262 	}
    263 
    264 	resid = uio->uio_resid;
    265 	osize = ip->i_e2fs_size;
    266 
    267 	if (vp->v_type == VREG) {
    268 		while (uio->uio_resid > 0) {
    269 			oldoff = uio->uio_offset;
    270 			blkoffset = blkoff(fs, uio->uio_offset);
    271 			bytelen = min(fs->e2fs_bsize - blkoffset,
    272 			    uio->uio_resid);
    273 
    274 			/*
    275 			 * XXXUBC if file is mapped and this is the last block,
    276 			 * process one page at a time.
    277 			 */
    278 
    279 			error = ext2fs_balloc_range(vp, uio->uio_offset,
    280 			    bytelen, ap->a_cred, 0);
    281 			if (error) {
    282 				break;
    283 			}
    284 			win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset,
    285 			    &bytelen, UBC_WRITE);
    286 			error = uiomove(win, bytelen, uio);
    287 			ubc_release(win, 0);
    288 			if (error) {
    289 				break;
    290 			}
    291 
    292 			/*
    293 			 * flush what we just wrote if necessary.
    294 			 * XXXUBC simplistic async flushing.
    295 			 */
    296 
    297 			if (oldoff >> 16 != uio->uio_offset >> 16) {
    298 				simple_lock(&vp->v_uvm.u_obj.vmobjlock);
    299 				rv = vp->v_uvm.u_obj.pgops->pgo_flush(
    300 				    &vp->v_uvm.u_obj, (oldoff >> 16) << 16,
    301 				    (uio->uio_offset >> 16) << 16, PGO_CLEANIT);
    302 				simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
    303 			}
    304 		}
    305 		goto out;
    306 	}
    307 
    308 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    309 	for (error = 0; uio->uio_resid > 0;) {
    310 		lbn = lblkno(fs, uio->uio_offset);
    311 		blkoffset = blkoff(fs, uio->uio_offset);
    312 		xfersize = min(fs->e2fs_bsize - blkoffset, uio->uio_resid);
    313 		if (xfersize < fs->e2fs_bsize)
    314 			flags |= B_CLRBUF;
    315 		else
    316 			flags &= ~B_CLRBUF;
    317 		error = ext2fs_balloc(ip,
    318 		    lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
    319 		if (error)
    320 			break;
    321 		if (ip->i_e2fs_size < uio->uio_offset + xfersize) {
    322 			ip->i_e2fs_size = uio->uio_offset + xfersize;
    323 		}
    324 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    325 		if (ioflag & IO_SYNC)
    326 			(void)bwrite(bp);
    327 		else if (xfersize + blkoffset == fs->e2fs_bsize)
    328 			if (doclusterwrite)
    329 				cluster_write(bp, ip->i_e2fs_size);
    330 			else
    331 				bawrite(bp);
    332 		else
    333 			bdwrite(bp);
    334 		if (error || xfersize == 0)
    335 			break;
    336 	}
    337 	/*
    338 	 * If we successfully wrote any data, and we are not the superuser
    339 	 * we clear the setuid and setgid bits as a precaution against
    340 	 * tampering.
    341 	 */
    342 out:
    343 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    344 	if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
    345 		ip->i_e2fs_mode &= ~(ISUID | ISGID);
    346 	if (error) {
    347 		if (ioflag & IO_UNIT) {
    348 			(void)VOP_TRUNCATE(vp, osize,
    349 				ioflag & IO_SYNC, ap->a_cred, uio->uio_procp);
    350 			uio->uio_offset -= resid - uio->uio_resid;
    351 			uio->uio_resid = resid;
    352 		}
    353 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
    354 		error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    355 	return (error);
    356 }
    357