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