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