Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_readwrite.c revision 1.12
      1 /*	$NetBSD: ext2fs_readwrite.c,v 1.12 2000/05/13 23:43:13 perseant 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 <vm/vm.h>
     55 
     56 #include <uvm/uvm_extern.h>
     57 
     58 #include <ufs/ufs/quota.h>
     59 #include <ufs/ufs/inode.h>
     60 #include <ufs/ext2fs/ext2fs.h>
     61 #include <ufs/ext2fs/ext2fs_extern.h>
     62 
     63 
     64 #define doclusterread 0 /* XXX underway */
     65 #define doclusterwrite 0
     66 
     67 /*
     68  * Vnode op for reading.
     69  */
     70 /* ARGSUSED */
     71 int
     72 ext2fs_read(v)
     73 	void *v;
     74 {
     75 	struct vop_read_args /* {
     76 		struct vnode *a_vp;
     77 		struct uio *a_uio;
     78 		int a_ioflag;
     79 		struct ucred *a_cred;
     80 	} */ *ap = v;
     81 	struct vnode *vp;
     82 	struct inode *ip;
     83 	struct uio *uio;
     84 	struct m_ext2fs *fs;
     85 	struct buf *bp;
     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 
    114 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
    115 		if ((bytesinfile = ip->i_e2fs_size - uio->uio_offset) <= 0)
    116 			break;
    117 		lbn = lblkno(fs, uio->uio_offset);
    118 		nextlbn = lbn + 1;
    119 		size = fs->e2fs_bsize;
    120 		blkoffset = blkoff(fs, uio->uio_offset);
    121 		xfersize = fs->e2fs_bsize - blkoffset;
    122 		if (uio->uio_resid < xfersize)
    123 			xfersize = uio->uio_resid;
    124 		if (bytesinfile < xfersize)
    125 			xfersize = bytesinfile;
    126 
    127 		if (lblktosize(fs, nextlbn) >= ip->i_e2fs_size)
    128 			error = bread(vp, lbn, size, NOCRED, &bp);
    129 		else if (doclusterread)
    130 			error = cluster_read(vp,
    131 				ip->i_e2fs_size, lbn, size, NOCRED, &bp);
    132 		else if (lbn - 1 == vp->v_lastr) {
    133 			int nextsize = fs->e2fs_bsize;
    134 			error = breadn(vp, lbn,
    135 				size, &nextlbn, &nextsize, 1, NOCRED, &bp);
    136 		} else
    137 			error = bread(vp, lbn, size, NOCRED, &bp);
    138 		if (error)
    139 			break;
    140 		vp->v_lastr = lbn;
    141 
    142 		/*
    143 		 * We should only get non-zero b_resid when an I/O error
    144 		 * has occurred, which should cause us to break above.
    145 		 * However, if the short read did not cause an error,
    146 		 * then we want to ensure that we do not uiomove bad
    147 		 * or uninitialized data.
    148 		 */
    149 		size -= bp->b_resid;
    150 		if (size < xfersize) {
    151 			if (size == 0)
    152 				break;
    153 			xfersize = size;
    154 		}
    155 		error = uiomove((char *)bp->b_data + blkoffset, (int)xfersize,
    156 				uio);
    157 		if (error)
    158 			break;
    159 		brelse(bp);
    160 	}
    161 	if (bp != NULL)
    162 		brelse(bp);
    163 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    164 		ip->i_flag |= IN_ACCESS;
    165 		if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
    166 			error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    167 	}
    168 	return (error);
    169 }
    170 
    171 /*
    172  * Vnode op for writing.
    173  */
    174 int
    175 ext2fs_write(v)
    176 	void *v;
    177 {
    178 	struct vop_write_args /* {
    179 		struct vnode *a_vp;
    180 		struct uio *a_uio;
    181 		int a_ioflag;
    182 		struct ucred *a_cred;
    183 	} */ *ap = v;
    184 	struct vnode *vp;
    185 	struct uio *uio;
    186 	struct inode *ip;
    187 	struct m_ext2fs *fs;
    188 	struct buf *bp;
    189 	struct proc *p;
    190 	ufs_daddr_t lbn;
    191 	off_t osize;
    192 	int blkoffset, error, flags, ioflag, resid, size, xfersize;
    193 
    194 	ioflag = ap->a_ioflag;
    195 	uio = ap->a_uio;
    196 	vp = ap->a_vp;
    197 	ip = VTOI(vp);
    198 
    199 #ifdef DIAGNOSTIC
    200 	if (uio->uio_rw != UIO_WRITE)
    201 		panic("%s: mode", "ext2fs_write");
    202 #endif
    203 
    204 	switch (vp->v_type) {
    205 	case VREG:
    206 		if (ioflag & IO_APPEND)
    207 			uio->uio_offset = ip->i_e2fs_size;
    208 		if ((ip->i_e2fs_flags & EXT2_APPEND) &&
    209 			uio->uio_offset != ip->i_e2fs_size)
    210 			return (EPERM);
    211 		/* FALLTHROUGH */
    212 	case VLNK:
    213 		break;
    214 	case VDIR:
    215 		if ((ioflag & IO_SYNC) == 0)
    216 			panic("%s: nonsync dir write", "ext2fs_write");
    217 		break;
    218 	default:
    219 		panic("%s: type", "ext2fs_write");
    220 	}
    221 
    222 	fs = ip->i_e2fs;
    223 	if (uio->uio_offset < 0 ||
    224 		(u_int64_t)uio->uio_offset + uio->uio_resid >
    225 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
    226 		return (EFBIG);
    227 	/*
    228 	 * Maybe this should be above the vnode op call, but so long as
    229 	 * file servers have no limits, I don't think it matters.
    230 	 */
    231 	p = uio->uio_procp;
    232 	if (vp->v_type == VREG && p &&
    233 		uio->uio_offset + uio->uio_resid >
    234 		p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    235 		psignal(p, SIGXFSZ);
    236 		return (EFBIG);
    237 	}
    238 
    239 	resid = uio->uio_resid;
    240 	osize = ip->i_e2fs_size;
    241 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    242 
    243 	for (error = 0; uio->uio_resid > 0;) {
    244 		lbn = lblkno(fs, uio->uio_offset);
    245 		blkoffset = blkoff(fs, uio->uio_offset);
    246 		xfersize = fs->e2fs_bsize - blkoffset;
    247 		if (uio->uio_resid < xfersize)
    248 			xfersize = uio->uio_resid;
    249 		if (fs->e2fs_bsize > xfersize)
    250 			flags |= B_CLRBUF;
    251 		else
    252 			flags &= ~B_CLRBUF;
    253 
    254 		error = ext2fs_balloc(ip,
    255 			lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
    256 		if (error)
    257 			break;
    258 		if (uio->uio_offset + xfersize > ip->i_e2fs_size) {
    259 			ip->i_e2fs_size = uio->uio_offset + xfersize;
    260 			uvm_vnp_setsize(vp, ip->i_e2fs_size);
    261 		}
    262 		(void)uvm_vnp_uncache(vp);
    263 
    264 		size = fs->e2fs_bsize - bp->b_resid;
    265 		if (size < xfersize)
    266 			xfersize = size;
    267 
    268 		error =
    269 			uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
    270 		if (ioflag & IO_SYNC)
    271 			(void)bwrite(bp);
    272 		else if (xfersize + blkoffset == fs->e2fs_bsize)
    273 			if (doclusterwrite)
    274 				cluster_write(bp, ip->i_e2fs_size);
    275 			else
    276 				bawrite(bp);
    277 		else
    278 			bdwrite(bp);
    279 		if (error || xfersize == 0)
    280 			break;
    281 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    282 	}
    283 	/*
    284 	 * If we successfully wrote any data, and we are not the superuser
    285 	 * we clear the setuid and setgid bits as a precaution against
    286 	 * tampering.
    287 	 */
    288 	if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
    289 		ip->i_e2fs_mode &= ~(ISUID | ISGID);
    290 	if (error) {
    291 		if (ioflag & IO_UNIT) {
    292 			(void)VOP_TRUNCATE(vp, osize,
    293 				ioflag & IO_SYNC, ap->a_cred, uio->uio_procp);
    294 			uio->uio_offset -= resid - uio->uio_resid;
    295 			uio->uio_resid = resid;
    296 		}
    297 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
    298 		error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    299 	return (error);
    300 }
    301