Home | History | Annotate | Line # | Download | only in ext2fs
ext2fs_readwrite.c revision 1.25
      1  1.25  jdolecek /*	$NetBSD: ext2fs_readwrite.c,v 1.25 2002/09/22 19:32:56 jdolecek 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.20     lukem 
     40  1.20     lukem #include <sys/cdefs.h>
     41  1.25  jdolecek __KERNEL_RCSID(0, "$NetBSD: ext2fs_readwrite.c,v 1.25 2002/09/22 19:32:56 jdolecek Exp $");
     42   1.5       mrg 
     43   1.1    bouyer #include <sys/param.h>
     44   1.1    bouyer #include <sys/systm.h>
     45   1.1    bouyer #include <sys/resourcevar.h>
     46   1.1    bouyer #include <sys/kernel.h>
     47   1.1    bouyer #include <sys/file.h>
     48   1.1    bouyer #include <sys/stat.h>
     49   1.1    bouyer #include <sys/buf.h>
     50   1.1    bouyer #include <sys/proc.h>
     51   1.1    bouyer #include <sys/mount.h>
     52   1.1    bouyer #include <sys/vnode.h>
     53   1.1    bouyer #include <sys/malloc.h>
     54   1.1    bouyer #include <sys/signalvar.h>
     55   1.1    bouyer 
     56   1.1    bouyer #include <ufs/ufs/inode.h>
     57  1.25  jdolecek #include <ufs/ufs/ufs_extern.h>
     58   1.1    bouyer #include <ufs/ext2fs/ext2fs.h>
     59   1.1    bouyer #include <ufs/ext2fs/ext2fs_extern.h>
     60   1.1    bouyer 
     61   1.1    bouyer 
     62   1.1    bouyer #define doclusterread 0 /* XXX underway */
     63   1.1    bouyer #define doclusterwrite 0
     64   1.1    bouyer 
     65   1.1    bouyer /*
     66   1.1    bouyer  * Vnode op for reading.
     67   1.1    bouyer  */
     68   1.1    bouyer /* ARGSUSED */
     69   1.1    bouyer int
     70   1.1    bouyer ext2fs_read(v)
     71   1.1    bouyer 	void *v;
     72   1.1    bouyer {
     73   1.1    bouyer 	struct vop_read_args /* {
     74   1.1    bouyer 		struct vnode *a_vp;
     75   1.1    bouyer 		struct uio *a_uio;
     76   1.1    bouyer 		int a_ioflag;
     77   1.1    bouyer 		struct ucred *a_cred;
     78   1.1    bouyer 	} */ *ap = v;
     79  1.11  augustss 	struct vnode *vp;
     80  1.11  augustss 	struct inode *ip;
     81  1.11  augustss 	struct uio *uio;
     82  1.11  augustss 	struct m_ext2fs *fs;
     83   1.1    bouyer 	struct buf *bp;
     84  1.14       chs 	void *win;
     85  1.14       chs 	vsize_t bytelen;
     86   1.6      fvdl 	ufs_daddr_t lbn, nextlbn;
     87   1.1    bouyer 	off_t bytesinfile;
     88   1.1    bouyer 	long size, xfersize, blkoffset;
     89   1.1    bouyer 	int error;
     90   1.1    bouyer 
     91   1.1    bouyer 	vp = ap->a_vp;
     92   1.1    bouyer 	ip = VTOI(vp);
     93   1.1    bouyer 	uio = ap->a_uio;
     94   1.1    bouyer 
     95   1.1    bouyer #ifdef DIAGNOSTIC
     96   1.1    bouyer 	if (uio->uio_rw != UIO_READ)
     97   1.1    bouyer 		panic("%s: mode", "ext2fs_read");
     98   1.1    bouyer 
     99   1.1    bouyer 	if (vp->v_type == VLNK) {
    100   1.1    bouyer 		if ((int)ip->i_e2fs_size < vp->v_mount->mnt_maxsymlinklen ||
    101   1.1    bouyer 			(vp->v_mount->mnt_maxsymlinklen == 0 &&
    102   1.1    bouyer 			 ip->i_e2fs_nblock == 0))
    103   1.1    bouyer 			panic("%s: short symlink", "ext2fs_read");
    104   1.1    bouyer 	} else if (vp->v_type != VREG && vp->v_type != VDIR)
    105   1.1    bouyer 		panic("%s: type %d", "ext2fs_read", vp->v_type);
    106   1.1    bouyer #endif
    107   1.1    bouyer 	fs = ip->i_e2fs;
    108   1.1    bouyer 	if ((u_int64_t)uio->uio_offset >
    109   1.1    bouyer 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
    110   1.1    bouyer 		return (EFBIG);
    111   1.1    bouyer 	if (uio->uio_resid == 0)
    112  1.18       chs 		return (0);
    113  1.18       chs 	if (uio->uio_offset >= ip->i_e2fs_size)
    114   1.1    bouyer 		return (0);
    115   1.1    bouyer 
    116  1.14       chs 	if (vp->v_type == VREG) {
    117  1.14       chs 		error = 0;
    118  1.14       chs 		while (uio->uio_resid > 0) {
    119  1.16       chs 			bytelen = MIN(ip->i_e2fs_size - uio->uio_offset,
    120  1.14       chs 			    uio->uio_resid);
    121  1.14       chs 
    122  1.14       chs 			if (bytelen == 0) {
    123  1.14       chs 				break;
    124  1.14       chs 			}
    125  1.17       chs 			win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
    126  1.17       chs 			    &bytelen, UBC_READ);
    127  1.14       chs 			error = uiomove(win, bytelen, uio);
    128  1.14       chs 			ubc_release(win, 0);
    129  1.14       chs 			if (error) {
    130  1.14       chs 				break;
    131  1.14       chs 			}
    132  1.14       chs 		}
    133  1.14       chs 		goto out;
    134  1.14       chs 	}
    135  1.14       chs 
    136   1.1    bouyer 	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
    137   1.1    bouyer 		if ((bytesinfile = ip->i_e2fs_size - uio->uio_offset) <= 0)
    138   1.1    bouyer 			break;
    139   1.1    bouyer 		lbn = lblkno(fs, uio->uio_offset);
    140   1.1    bouyer 		nextlbn = lbn + 1;
    141   1.1    bouyer 		size = fs->e2fs_bsize;
    142   1.1    bouyer 		blkoffset = blkoff(fs, uio->uio_offset);
    143   1.1    bouyer 		xfersize = fs->e2fs_bsize - blkoffset;
    144   1.1    bouyer 		if (uio->uio_resid < xfersize)
    145   1.1    bouyer 			xfersize = uio->uio_resid;
    146   1.1    bouyer 		if (bytesinfile < xfersize)
    147   1.1    bouyer 			xfersize = bytesinfile;
    148   1.1    bouyer 
    149   1.1    bouyer 		if (lblktosize(fs, nextlbn) >= ip->i_e2fs_size)
    150   1.1    bouyer 			error = bread(vp, lbn, size, NOCRED, &bp);
    151  1.17       chs 		else {
    152   1.1    bouyer 			int nextsize = fs->e2fs_bsize;
    153   1.1    bouyer 			error = breadn(vp, lbn,
    154   1.1    bouyer 				size, &nextlbn, &nextsize, 1, NOCRED, &bp);
    155  1.17       chs 		}
    156   1.1    bouyer 		if (error)
    157   1.1    bouyer 			break;
    158   1.1    bouyer 
    159   1.1    bouyer 		/*
    160   1.1    bouyer 		 * We should only get non-zero b_resid when an I/O error
    161   1.1    bouyer 		 * has occurred, which should cause us to break above.
    162   1.1    bouyer 		 * However, if the short read did not cause an error,
    163   1.1    bouyer 		 * then we want to ensure that we do not uiomove bad
    164   1.1    bouyer 		 * or uninitialized data.
    165   1.1    bouyer 		 */
    166   1.1    bouyer 		size -= bp->b_resid;
    167   1.1    bouyer 		if (size < xfersize) {
    168   1.1    bouyer 			if (size == 0)
    169   1.1    bouyer 				break;
    170   1.1    bouyer 			xfersize = size;
    171   1.1    bouyer 		}
    172  1.14       chs 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    173   1.1    bouyer 		if (error)
    174   1.1    bouyer 			break;
    175   1.1    bouyer 		brelse(bp);
    176   1.1    bouyer 	}
    177   1.1    bouyer 	if (bp != NULL)
    178   1.1    bouyer 		brelse(bp);
    179  1.14       chs 
    180  1.14       chs out:
    181   1.7    kleink 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    182   1.1    bouyer 		ip->i_flag |= IN_ACCESS;
    183   1.9   mycroft 		if ((ap->a_ioflag & IO_SYNC) == IO_SYNC)
    184  1.12  perseant 			error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    185   1.7    kleink 	}
    186   1.1    bouyer 	return (error);
    187   1.1    bouyer }
    188   1.1    bouyer 
    189   1.1    bouyer /*
    190   1.1    bouyer  * Vnode op for writing.
    191   1.1    bouyer  */
    192   1.1    bouyer int
    193   1.1    bouyer ext2fs_write(v)
    194   1.1    bouyer 	void *v;
    195   1.1    bouyer {
    196   1.1    bouyer 	struct vop_write_args /* {
    197   1.1    bouyer 		struct vnode *a_vp;
    198   1.1    bouyer 		struct uio *a_uio;
    199   1.1    bouyer 		int a_ioflag;
    200   1.1    bouyer 		struct ucred *a_cred;
    201   1.1    bouyer 	} */ *ap = v;
    202  1.11  augustss 	struct vnode *vp;
    203  1.11  augustss 	struct uio *uio;
    204  1.11  augustss 	struct inode *ip;
    205  1.11  augustss 	struct m_ext2fs *fs;
    206   1.1    bouyer 	struct buf *bp;
    207   1.1    bouyer 	struct proc *p;
    208   1.6      fvdl 	ufs_daddr_t lbn;
    209   1.1    bouyer 	off_t osize;
    210  1.14       chs 	int blkoffset, error, flags, ioflag, resid, xfersize;
    211  1.14       chs 	vsize_t bytelen;
    212  1.14       chs 	void *win;
    213  1.14       chs 	off_t oldoff;
    214  1.23       chs 	boolean_t async;
    215   1.1    bouyer 
    216   1.1    bouyer 	ioflag = ap->a_ioflag;
    217   1.1    bouyer 	uio = ap->a_uio;
    218   1.1    bouyer 	vp = ap->a_vp;
    219   1.1    bouyer 	ip = VTOI(vp);
    220  1.14       chs 	error = 0;
    221   1.1    bouyer 
    222   1.1    bouyer #ifdef DIAGNOSTIC
    223   1.1    bouyer 	if (uio->uio_rw != UIO_WRITE)
    224   1.1    bouyer 		panic("%s: mode", "ext2fs_write");
    225   1.1    bouyer #endif
    226   1.1    bouyer 
    227   1.1    bouyer 	switch (vp->v_type) {
    228   1.1    bouyer 	case VREG:
    229   1.1    bouyer 		if (ioflag & IO_APPEND)
    230   1.1    bouyer 			uio->uio_offset = ip->i_e2fs_size;
    231   1.1    bouyer 		if ((ip->i_e2fs_flags & EXT2_APPEND) &&
    232   1.1    bouyer 			uio->uio_offset != ip->i_e2fs_size)
    233   1.1    bouyer 			return (EPERM);
    234   1.1    bouyer 		/* FALLTHROUGH */
    235   1.1    bouyer 	case VLNK:
    236   1.1    bouyer 		break;
    237   1.1    bouyer 	case VDIR:
    238   1.1    bouyer 		if ((ioflag & IO_SYNC) == 0)
    239   1.1    bouyer 			panic("%s: nonsync dir write", "ext2fs_write");
    240   1.1    bouyer 		break;
    241   1.1    bouyer 	default:
    242   1.1    bouyer 		panic("%s: type", "ext2fs_write");
    243   1.1    bouyer 	}
    244   1.1    bouyer 
    245   1.1    bouyer 	fs = ip->i_e2fs;
    246   1.1    bouyer 	if (uio->uio_offset < 0 ||
    247   1.1    bouyer 		(u_int64_t)uio->uio_offset + uio->uio_resid >
    248   1.1    bouyer 		((u_int64_t)0x80000000 * fs->e2fs_bsize - 1))
    249   1.1    bouyer 		return (EFBIG);
    250   1.1    bouyer 	/*
    251   1.1    bouyer 	 * Maybe this should be above the vnode op call, but so long as
    252   1.1    bouyer 	 * file servers have no limits, I don't think it matters.
    253   1.1    bouyer 	 */
    254   1.1    bouyer 	p = uio->uio_procp;
    255   1.1    bouyer 	if (vp->v_type == VREG && p &&
    256   1.1    bouyer 		uio->uio_offset + uio->uio_resid >
    257   1.1    bouyer 		p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    258   1.1    bouyer 		psignal(p, SIGXFSZ);
    259   1.1    bouyer 		return (EFBIG);
    260   1.1    bouyer 	}
    261  1.24       chs 	if (uio->uio_resid == 0)
    262  1.24       chs 		return (0);
    263   1.1    bouyer 
    264  1.23       chs 	async = vp->v_mount->mnt_flag & MNT_ASYNC;
    265   1.1    bouyer 	resid = uio->uio_resid;
    266   1.1    bouyer 	osize = ip->i_e2fs_size;
    267  1.14       chs 
    268  1.14       chs 	if (vp->v_type == VREG) {
    269  1.14       chs 		while (uio->uio_resid > 0) {
    270  1.14       chs 			oldoff = uio->uio_offset;
    271  1.14       chs 			blkoffset = blkoff(fs, uio->uio_offset);
    272  1.16       chs 			bytelen = MIN(fs->e2fs_bsize - blkoffset,
    273  1.14       chs 			    uio->uio_resid);
    274  1.14       chs 
    275  1.25  jdolecek 			error = ufs_balloc_range(vp, uio->uio_offset,
    276  1.14       chs 			    bytelen, ap->a_cred, 0);
    277  1.14       chs 			if (error) {
    278  1.14       chs 				break;
    279  1.14       chs 			}
    280  1.17       chs 			win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
    281  1.14       chs 			    &bytelen, UBC_WRITE);
    282  1.14       chs 			error = uiomove(win, bytelen, uio);
    283  1.14       chs 			ubc_release(win, 0);
    284  1.14       chs 			if (error) {
    285  1.14       chs 				break;
    286  1.14       chs 			}
    287  1.14       chs 
    288  1.14       chs 			/*
    289  1.21       chs 			 * update UVM's notion of the size now that we've
    290  1.21       chs 			 * copied the data into the vnode's pages.
    291  1.21       chs 			 */
    292  1.21       chs 
    293  1.21       chs 			if (vp->v_size < uio->uio_offset) {
    294  1.21       chs 				uvm_vnp_setsize(vp, uio->uio_offset);
    295  1.21       chs 			}
    296  1.21       chs 
    297  1.21       chs 			/*
    298  1.14       chs 			 * flush what we just wrote if necessary.
    299  1.14       chs 			 * XXXUBC simplistic async flushing.
    300  1.14       chs 			 */
    301  1.14       chs 
    302  1.23       chs 			if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
    303  1.22       chs 				simple_lock(&vp->v_interlock);
    304  1.22       chs 				error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
    305  1.14       chs 				    (uio->uio_offset >> 16) << 16, PGO_CLEANIT);
    306  1.14       chs 			}
    307  1.14       chs 		}
    308  1.17       chs 		if (error == 0 && ioflag & IO_SYNC) {
    309  1.22       chs 			simple_lock(&vp->v_interlock);
    310  1.22       chs 			error = VOP_PUTPAGES(vp, trunc_page(oldoff),
    311  1.22       chs 			    round_page(blkroundup(fs, uio->uio_offset)),
    312  1.22       chs 			    PGO_CLEANIT | PGO_SYNCIO);
    313  1.17       chs 		}
    314  1.17       chs 
    315  1.14       chs 		goto out;
    316  1.14       chs 	}
    317  1.14       chs 
    318   1.1    bouyer 	flags = ioflag & IO_SYNC ? B_SYNC : 0;
    319   1.1    bouyer 	for (error = 0; uio->uio_resid > 0;) {
    320   1.1    bouyer 		lbn = lblkno(fs, uio->uio_offset);
    321   1.1    bouyer 		blkoffset = blkoff(fs, uio->uio_offset);
    322  1.16       chs 		xfersize = MIN(fs->e2fs_bsize - blkoffset, uio->uio_resid);
    323  1.14       chs 		if (xfersize < fs->e2fs_bsize)
    324   1.1    bouyer 			flags |= B_CLRBUF;
    325   1.1    bouyer 		else
    326   1.1    bouyer 			flags &= ~B_CLRBUF;
    327  1.15       chs 		error = ext2fs_balloc(ip,
    328  1.15       chs 		    lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
    329   1.1    bouyer 		if (error)
    330   1.1    bouyer 			break;
    331  1.14       chs 		if (ip->i_e2fs_size < uio->uio_offset + xfersize) {
    332   1.1    bouyer 			ip->i_e2fs_size = uio->uio_offset + xfersize;
    333   1.1    bouyer 		}
    334  1.14       chs 		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
    335  1.21       chs 
    336  1.21       chs 		/*
    337  1.21       chs 		 * update UVM's notion of the size now that we've
    338  1.21       chs 		 * copied the data into the vnode's pages.
    339  1.21       chs 		 */
    340  1.21       chs 
    341  1.21       chs 		if (vp->v_size < uio->uio_offset) {
    342  1.21       chs 			uvm_vnp_setsize(vp, uio->uio_offset);
    343  1.21       chs 		}
    344  1.21       chs 
    345   1.1    bouyer 		if (ioflag & IO_SYNC)
    346   1.1    bouyer 			(void)bwrite(bp);
    347   1.1    bouyer 		else if (xfersize + blkoffset == fs->e2fs_bsize)
    348  1.17       chs 			bawrite(bp);
    349   1.1    bouyer 		else
    350   1.1    bouyer 			bdwrite(bp);
    351   1.1    bouyer 		if (error || xfersize == 0)
    352   1.1    bouyer 			break;
    353   1.1    bouyer 	}
    354  1.21       chs 
    355   1.1    bouyer 	/*
    356   1.1    bouyer 	 * If we successfully wrote any data, and we are not the superuser
    357   1.1    bouyer 	 * we clear the setuid and setgid bits as a precaution against
    358   1.1    bouyer 	 * tampering.
    359   1.1    bouyer 	 */
    360  1.21       chs 
    361  1.14       chs out:
    362  1.14       chs 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    363   1.1    bouyer 	if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
    364   1.1    bouyer 		ip->i_e2fs_mode &= ~(ISUID | ISGID);
    365   1.1    bouyer 	if (error) {
    366  1.17       chs 		(void) VOP_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred,
    367  1.17       chs 		    uio->uio_procp);
    368  1.17       chs 		uio->uio_offset -= resid - uio->uio_resid;
    369  1.17       chs 		uio->uio_resid = resid;
    370   1.9   mycroft 	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
    371  1.12  perseant 		error = VOP_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
    372  1.17       chs 	KASSERT(vp->v_size == ip->i_e2fs_size);
    373   1.1    bouyer 	return (error);
    374   1.1    bouyer }
    375