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