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