Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_readwrite.c revision 1.13
      1  1.13       mrg /*	$NetBSD: ext2fs_readwrite.c,v 1.13 2000/06/28 14:16:38 mrg Exp $	*/
      2   1.1    bouyer 
      3   1.1    bouyer /*-
      4   1.1    bouyer  * Copyright (c) 1997 Manuel Bouyer.
      5   1.1    bouyer  * Copyright (c) 1993
      6   1.1    bouyer  *	The Regents of the University of California.  All rights reserved.
      7   1.1    bouyer  *
      8   1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      9   1.1    bouyer  * modification, are permitted provided that the following conditions
     10   1.1    bouyer  * are met:
     11   1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     12   1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     13   1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     15   1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     16   1.1    bouyer  * 3. All advertising materials mentioning features or use of this software
     17   1.1    bouyer  *    must display the following acknowledgement:
     18   1.1    bouyer  *	This product includes software developed by the University of
     19   1.1    bouyer  *	California, Berkeley and its contributors.
     20   1.1    bouyer  * 4. Neither the name of the University nor the names of its contributors
     21   1.1    bouyer  *    may be used to endorse or promote products derived from this software
     22   1.1    bouyer  *    without specific prior written permission.
     23   1.1    bouyer  *
     24   1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1    bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1    bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1    bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1    bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1    bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1    bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1    bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1    bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1    bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1    bouyer  * SUCH DAMAGE.
     35   1.1    bouyer  *
     36   1.1    bouyer  *	@(#)ufs_readwrite.c	8.8 (Berkeley) 8/4/94
     37   1.1    bouyer  * Modified for ext2fs by Manuel Bouyer.
     38   1.1    bouyer  */
     39   1.5       mrg 
     40   1.1    bouyer #include <sys/param.h>
     41   1.1    bouyer #include <sys/systm.h>
     42   1.1    bouyer #include <sys/resourcevar.h>
     43   1.1    bouyer #include <sys/kernel.h>
     44   1.1    bouyer #include <sys/file.h>
     45   1.1    bouyer #include <sys/stat.h>
     46   1.1    bouyer #include <sys/buf.h>
     47   1.1    bouyer #include <sys/proc.h>
     48   1.1    bouyer #include <sys/conf.h>
     49   1.1    bouyer #include <sys/mount.h>
     50   1.1    bouyer #include <sys/vnode.h>
     51   1.1    bouyer #include <sys/malloc.h>
     52   1.1    bouyer #include <sys/signalvar.h>
     53   1.1    bouyer 
     54   1.1    bouyer #include <ufs/ufs/quota.h>
     55   1.1    bouyer #include <ufs/ufs/inode.h>
     56   1.1    bouyer #include <ufs/ext2fs/ext2fs.h>
     57   1.1    bouyer #include <ufs/ext2fs/ext2fs_extern.h>
     58   1.1    bouyer 
     59   1.1    bouyer 
     60   1.1    bouyer #define doclusterread 0 /* XXX underway */
     61   1.1    bouyer #define doclusterwrite 0
     62   1.1    bouyer 
     63   1.1    bouyer /*
     64   1.1    bouyer  * Vnode op for reading.
     65   1.1    bouyer  */
     66   1.1    bouyer /* ARGSUSED */
     67   1.1    bouyer int
     68   1.1    bouyer ext2fs_read(v)
     69   1.1    bouyer 	void *v;
     70   1.1    bouyer {
     71   1.1    bouyer 	struct vop_read_args /* {
     72   1.1    bouyer 		struct vnode *a_vp;
     73   1.1    bouyer 		struct uio *a_uio;
     74   1.1    bouyer 		int a_ioflag;
     75   1.1    bouyer 		struct ucred *a_cred;
     76   1.1    bouyer 	} */ *ap = v;
     77  1.11  augustss 	struct vnode *vp;
     78  1.11  augustss 	struct inode *ip;
     79  1.11  augustss 	struct uio *uio;
     80  1.11  augustss 	struct m_ext2fs *fs;
     81   1.1    bouyer 	struct buf *bp;
     82   1.6      fvdl 	ufs_daddr_t lbn, nextlbn;
     83   1.1    bouyer 	off_t bytesinfile;
     84   1.1    bouyer 	long size, xfersize, blkoffset;
     85   1.1    bouyer 	int error;
     86   1.1    bouyer 
     87   1.1    bouyer 	vp = ap->a_vp;
     88   1.1    bouyer 	ip = VTOI(vp);
     89   1.1    bouyer 	uio = ap->a_uio;
     90   1.1    bouyer 
     91   1.1    bouyer #ifdef DIAGNOSTIC
     92   1.1    bouyer 	if (uio->uio_rw != UIO_READ)
     93   1.1    bouyer 		panic("%s: mode", "ext2fs_read");
     94   1.1    bouyer 
     95   1.1    bouyer 	if (vp->v_type == VLNK) {
     96   1.1    bouyer 		if ((int)ip->i_e2fs_size < vp->v_mount->mnt_maxsymlinklen ||
     97   1.1    bouyer 			(vp->v_mount->mnt_maxsymlinklen == 0 &&
     98   1.1    bouyer 			 ip->i_e2fs_nblock == 0))
     99   1.1    bouyer 			panic("%s: short symlink", "ext2fs_read");
    100   1.1    bouyer 	} else if (vp->v_type != VREG && vp->v_type != VDIR)
    101   1.1    bouyer 		panic("%s: type %d", "ext2fs_read", vp->v_type);
    102   1.1    bouyer #endif
    103   1.1    bouyer 	fs = ip->i_e2fs;
    104   1.1    bouyer 	if ((u_int64_t)uio->uio_offset >
    105   1.1    bouyer 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
    106   1.1    bouyer 		return (EFBIG);
    107   1.1    bouyer 	if (uio->uio_resid == 0)
    108   1.1    bouyer 		return (0);
    109   1.1    bouyer 
    110   1.1    bouyer 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
    111   1.1    bouyer 		if ((bytesinfile = ip->i_e2fs_size - uio->uio_offset) <= 0)
    112   1.1    bouyer 			break;
    113   1.1    bouyer 		lbn = lblkno(fs, uio->uio_offset);
    114   1.1    bouyer 		nextlbn = lbn + 1;
    115   1.1    bouyer 		size = fs->e2fs_bsize;
    116   1.1    bouyer 		blkoffset = blkoff(fs, uio->uio_offset);
    117   1.1    bouyer 		xfersize = fs->e2fs_bsize - blkoffset;
    118   1.1    bouyer 		if (uio->uio_resid < xfersize)
    119   1.1    bouyer 			xfersize = uio->uio_resid;
    120   1.1    bouyer 		if (bytesinfile < xfersize)
    121   1.1    bouyer 			xfersize = bytesinfile;
    122   1.1    bouyer 
    123   1.1    bouyer 		if (lblktosize(fs, nextlbn) >= ip->i_e2fs_size)
    124   1.1    bouyer 			error = bread(vp, lbn, size, NOCRED, &bp);
    125   1.1    bouyer 		else if (doclusterread)
    126   1.1    bouyer 			error = cluster_read(vp,
    127   1.1    bouyer 				ip->i_e2fs_size, lbn, size, NOCRED, &bp);
    128   1.1    bouyer 		else if (lbn - 1 == vp->v_lastr) {
    129   1.1    bouyer 			int nextsize = fs->e2fs_bsize;
    130   1.1    bouyer 			error = breadn(vp, lbn,
    131   1.1    bouyer 				size, &nextlbn, &nextsize, 1, NOCRED, &bp);
    132   1.1    bouyer 		} else
    133   1.1    bouyer 			error = bread(vp, lbn, size, NOCRED, &bp);
    134   1.1    bouyer 		if (error)
    135   1.1    bouyer 			break;
    136   1.1    bouyer 		vp->v_lastr = lbn;
    137   1.1    bouyer 
    138   1.1    bouyer 		/*
    139   1.1    bouyer 		 * We should only get non-zero b_resid when an I/O error
    140   1.1    bouyer 		 * has occurred, which should cause us to break above.
    141   1.1    bouyer 		 * However, if the short read did not cause an error,
    142   1.1    bouyer 		 * then we want to ensure that we do not uiomove bad
    143   1.1    bouyer 		 * or uninitialized data.
    144   1.1    bouyer 		 */
    145   1.1    bouyer 		size -= bp->b_resid;
    146   1.1    bouyer 		if (size < xfersize) {
    147   1.1    bouyer 			if (size == 0)
    148   1.1    bouyer 				break;
    149   1.1    bouyer 			xfersize = size;
    150   1.1    bouyer 		}
    151   1.1    bouyer 		error = uiomove((char *)bp->b_data + blkoffset, (int)xfersize,
    152   1.1    bouyer 				uio);
    153   1.1    bouyer 		if (error)
    154   1.1    bouyer 			break;
    155   1.1    bouyer 		brelse(bp);
    156   1.1    bouyer 	}
    157   1.1    bouyer 	if (bp != NULL)
    158   1.1    bouyer 		brelse(bp);
    159   1.7    kleink 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    160   1.1    bouyer 		ip->i_flag |= IN_ACCESS;
    161   1.9   mycroft 		if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
    162  1.12  perseant 			error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    163   1.7    kleink 	}
    164   1.1    bouyer 	return (error);
    165   1.1    bouyer }
    166   1.1    bouyer 
    167   1.1    bouyer /*
    168   1.1    bouyer  * Vnode op for writing.
    169   1.1    bouyer  */
    170   1.1    bouyer int
    171   1.1    bouyer ext2fs_write(v)
    172   1.1    bouyer 	void *v;
    173   1.1    bouyer {
    174   1.1    bouyer 	struct vop_write_args /* {
    175   1.1    bouyer 		struct vnode *a_vp;
    176   1.1    bouyer 		struct uio *a_uio;
    177   1.1    bouyer 		int a_ioflag;
    178   1.1    bouyer 		struct ucred *a_cred;
    179   1.1    bouyer 	} */ *ap = v;
    180  1.11  augustss 	struct vnode *vp;
    181  1.11  augustss 	struct uio *uio;
    182  1.11  augustss 	struct inode *ip;
    183  1.11  augustss 	struct m_ext2fs *fs;
    184   1.1    bouyer 	struct buf *bp;
    185   1.1    bouyer 	struct proc *p;
    186   1.6      fvdl 	ufs_daddr_t lbn;
    187   1.1    bouyer 	off_t osize;
    188   1.1    bouyer 	int blkoffset, error, flags, ioflag, resid, size, xfersize;
    189   1.1    bouyer 
    190   1.1    bouyer 	ioflag = ap->a_ioflag;
    191   1.1    bouyer 	uio = ap->a_uio;
    192   1.1    bouyer 	vp = ap->a_vp;
    193   1.1    bouyer 	ip = VTOI(vp);
    194   1.1    bouyer 
    195   1.1    bouyer #ifdef DIAGNOSTIC
    196   1.1    bouyer 	if (uio->uio_rw != UIO_WRITE)
    197   1.1    bouyer 		panic("%s: mode", "ext2fs_write");
    198   1.1    bouyer #endif
    199   1.1    bouyer 
    200   1.1    bouyer 	switch (vp->v_type) {
    201   1.1    bouyer 	case VREG:
    202   1.1    bouyer 		if (ioflag & IO_APPEND)
    203   1.1    bouyer 			uio->uio_offset = ip->i_e2fs_size;
    204   1.1    bouyer 		if ((ip->i_e2fs_flags & EXT2_APPEND) &&
    205   1.1    bouyer 			uio->uio_offset != ip->i_e2fs_size)
    206   1.1    bouyer 			return (EPERM);
    207   1.1    bouyer 		/* FALLTHROUGH */
    208   1.1    bouyer 	case VLNK:
    209   1.1    bouyer 		break;
    210   1.1    bouyer 	case VDIR:
    211   1.1    bouyer 		if ((ioflag & IO_SYNC) == 0)
    212   1.1    bouyer 			panic("%s: nonsync dir write", "ext2fs_write");
    213   1.1    bouyer 		break;
    214   1.1    bouyer 	default:
    215   1.1    bouyer 		panic("%s: type", "ext2fs_write");
    216   1.1    bouyer 	}
    217   1.1    bouyer 
    218   1.1    bouyer 	fs = ip->i_e2fs;
    219   1.1    bouyer 	if (uio->uio_offset < 0 ||
    220   1.1    bouyer 		(u_int64_t)uio->uio_offset + uio->uio_resid >
    221   1.1    bouyer 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
    222   1.1    bouyer 		return (EFBIG);
    223   1.1    bouyer 	/*
    224   1.1    bouyer 	 * Maybe this should be above the vnode op call, but so long as
    225   1.1    bouyer 	 * file servers have no limits, I don't think it matters.
    226   1.1    bouyer 	 */
    227   1.1    bouyer 	p = uio->uio_procp;
    228   1.1    bouyer 	if (vp->v_type == VREG && p &&
    229   1.1    bouyer 		uio->uio_offset + uio->uio_resid >
    230   1.1    bouyer 		p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    231   1.1    bouyer 		psignal(p, SIGXFSZ);
    232   1.1    bouyer 		return (EFBIG);
    233   1.1    bouyer 	}
    234   1.1    bouyer 
    235   1.1    bouyer 	resid = uio->uio_resid;
    236   1.1    bouyer 	osize = ip->i_e2fs_size;
    237   1.1    bouyer 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    238   1.1    bouyer 
    239   1.1    bouyer 	for (error = 0; uio->uio_resid > 0;) {
    240   1.1    bouyer 		lbn = lblkno(fs, uio->uio_offset);
    241   1.1    bouyer 		blkoffset = blkoff(fs, uio->uio_offset);
    242   1.1    bouyer 		xfersize = fs->e2fs_bsize - blkoffset;
    243   1.1    bouyer 		if (uio->uio_resid < xfersize)
    244   1.1    bouyer 			xfersize = uio->uio_resid;
    245   1.1    bouyer 		if (fs->e2fs_bsize > xfersize)
    246   1.1    bouyer 			flags |= B_CLRBUF;
    247   1.1    bouyer 		else
    248   1.1    bouyer 			flags &= ~B_CLRBUF;
    249   1.1    bouyer 
    250   1.1    bouyer 		error = ext2fs_balloc(ip,
    251   1.1    bouyer 			lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
    252   1.1    bouyer 		if (error)
    253   1.1    bouyer 			break;
    254   1.1    bouyer 		if (uio->uio_offset + xfersize > ip->i_e2fs_size) {
    255   1.1    bouyer 			ip->i_e2fs_size = uio->uio_offset + xfersize;
    256   1.4       mrg 			uvm_vnp_setsize(vp, ip->i_e2fs_size);
    257   1.1    bouyer 		}
    258   1.4       mrg 		(void)uvm_vnp_uncache(vp);
    259   1.1    bouyer 
    260   1.1    bouyer 		size = fs->e2fs_bsize - bp->b_resid;
    261   1.1    bouyer 		if (size < xfersize)
    262   1.1    bouyer 			xfersize = size;
    263   1.1    bouyer 
    264   1.1    bouyer 		error =
    265   1.1    bouyer 			uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
    266   1.1    bouyer 		if (ioflag & IO_SYNC)
    267   1.1    bouyer 			(void)bwrite(bp);
    268   1.1    bouyer 		else if (xfersize + blkoffset == fs->e2fs_bsize)
    269   1.1    bouyer 			if (doclusterwrite)
    270   1.1    bouyer 				cluster_write(bp, ip->i_e2fs_size);
    271   1.1    bouyer 			else
    272   1.1    bouyer 				bawrite(bp);
    273   1.1    bouyer 		else
    274   1.1    bouyer 			bdwrite(bp);
    275   1.1    bouyer 		if (error || xfersize == 0)
    276   1.1    bouyer 			break;
    277   1.1    bouyer 		ip->i_flag |= IN_CHANGE | IN_UPDATE;
    278   1.1    bouyer 	}
    279   1.1    bouyer 	/*
    280   1.1    bouyer 	 * If we successfully wrote any data, and we are not the superuser
    281   1.1    bouyer 	 * we clear the setuid and setgid bits as a precaution against
    282   1.1    bouyer 	 * tampering.
    283   1.1    bouyer 	 */
    284   1.1    bouyer 	if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
    285   1.1    bouyer 		ip->i_e2fs_mode &= ~(ISUID | ISGID);
    286   1.1    bouyer 	if (error) {
    287   1.1    bouyer 		if (ioflag & IO_UNIT) {
    288   1.1    bouyer 			(void)VOP_TRUNCATE(vp, osize,
    289   1.1    bouyer 				ioflag & IO_SYNC, ap->a_cred, uio->uio_procp);
    290   1.1    bouyer 			uio->uio_offset -= resid - uio->uio_resid;
    291   1.1    bouyer 			uio->uio_resid = resid;
    292   1.1    bouyer 		}
    293   1.9   mycroft 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
    294  1.12  perseant 		error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    295   1.1    bouyer 	return (error);
    296   1.1    bouyer }
    297