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