1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the University of California, Berkeley. The name of the 14 * University may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * 20 * @(#)nfs_bio.c 7.12 (Berkeley) 02/23/90 21 */ 22 23 #include "param.h" 24 #include "user.h" 25 #include "buf.h" 26 #include "vnode.h" 27 #include "trace.h" 28 #include "mount.h" 29 #include "nfsnode.h" 30 #include "nfsv2.h" 31 #include "nfs.h" 32 #include "nfsiom.h" 33 34 /* True and false, how exciting */ 35 #define TRUE 1 36 #define FALSE 0 37 38 /* 39 * Vnode op for read using bio 40 * Any similarity to readip() is purely coincidental 41 */ 42 nfs_read(vp, uio, ioflag, cred) 43 register struct vnode *vp; 44 struct uio *uio; 45 int ioflag; 46 struct ucred *cred; 47 { 48 register struct nfsnode *np = VTONFS(vp); 49 struct buf *bp; 50 struct vattr vattr; 51 daddr_t lbn, bn, rablock; 52 int diff, error = 0; 53 long n, on; 54 55 /* 56 * Avoid caching directories. Once everything is using getdirentries() 57 * this will never happen anyhow. 58 */ 59 if (vp->v_type == VDIR) 60 return (nfs_readrpc(vp, uio, cred)); 61 if (uio->uio_rw != UIO_READ) 62 panic("nfs_read mode"); 63 if (vp->v_type != VREG) 64 panic("nfs_read type"); 65 if (uio->uio_resid == 0) 66 return (0); 67 if (uio->uio_offset < 0) 68 return (EINVAL); 69 /* 70 * If the file's modify time on the server has changed since the 71 * last read rpc or you have written to the file, 72 * you may have lost data cache consistency with the 73 * server, so flush all of the file's data out of the cache. 74 * This will implicitly bring the modify time up to date, since 75 * up to date attributes are returned in the reply to any write rpc's 76 * NB: This implies that cache data can be read when up to 77 * NFS_ATTRTIMEO seconds out of date. If you find that you need current 78 * attributes this could be forced by setting n_attrstamp to 0 before 79 * the nfs_getattr() call. 80 */ 81 if (np->n_flag & NMODIFIED) { 82 np->n_flag &= ~NMODIFIED; 83 if (vinvalbuf(vp, TRUE)) { 84 if (error = nfs_getattr(vp, &vattr, cred)) 85 return (error); 86 np->n_mtime = vattr.va_mtime.tv_sec; 87 } 88 } else if (vp->v_cleanblkhd || vp->v_dirtyblkhd) { 89 if (error = nfs_getattr(vp, &vattr, cred)) 90 return (error); 91 if (np->n_mtime != vattr.va_mtime.tv_sec) { 92 vinvalbuf(vp, TRUE); 93 np->n_mtime = vattr.va_mtime.tv_sec; 94 } 95 } 96 np->n_flag |= NBUFFERED; 97 do { 98 nfsstats.biocache_reads++; 99 lbn = uio->uio_offset >> NFS_BIOSHIFT; 100 on = uio->uio_offset & (NFS_BIOSIZE-1); 101 n = MIN((unsigned)(NFS_BIOSIZE - on), uio->uio_resid); 102 diff = np->n_size - uio->uio_offset; 103 if (diff <= 0) 104 return (error); 105 if (diff < n) 106 n = diff; 107 bn = lbn*(NFS_BIOSIZE/DEV_BSIZE); 108 rablock = (lbn+1)*(NFS_BIOSIZE/DEV_BSIZE); 109 if (vp->v_lastr + 1 == lbn && 110 np->n_size > (rablock * DEV_BSIZE)) 111 error = breada(vp, bn, NFS_BIOSIZE, rablock, NFS_BIOSIZE, 112 cred, &bp); 113 else 114 error = bread(vp, bn, NFS_BIOSIZE, cred, &bp); 115 vp->v_lastr = lbn; 116 if (bp->b_resid) { 117 diff = (on >= (NFS_BIOSIZE-bp->b_resid)) ? 0 : 118 (NFS_BIOSIZE-bp->b_resid-on); 119 n = MIN(n, diff); 120 } 121 if (error) { 122 brelse(bp); 123 return (error); 124 } 125 if (n > 0) 126 error = uiomove(bp->b_un.b_addr + on, (int)n, uio); 127 if (n+on == NFS_BIOSIZE || uio->uio_offset == np->n_size) 128 bp->b_flags |= B_AGE; 129 brelse(bp); 130 } while (error == 0 && uio->uio_resid > 0 && n != 0); 131 return (error); 132 } 133 134 /* 135 * Vnode op for write using bio 136 */ 137 nfs_write(vp, uio, ioflag, cred) 138 register struct vnode *vp; 139 register struct uio *uio; 140 int ioflag; 141 struct ucred *cred; 142 { 143 struct buf *bp; 144 struct nfsnode *np = VTONFS(vp); 145 daddr_t lbn, bn; 146 int n, on, error = 0; 147 148 /* Should we try and do this ?? */ 149 if (vp->v_type == VREG && (ioflag & IO_APPEND)) 150 uio->uio_offset = np->n_size; 151 #ifdef notdef 152 cnt = uio->uio_resid; 153 osize = np->n_size; 154 #endif 155 if (uio->uio_rw != UIO_WRITE) 156 panic("nfs_write mode"); 157 if (vp->v_type != VREG) 158 panic("nfs_write type"); 159 if (uio->uio_offset < 0) 160 return (EINVAL); 161 if (uio->uio_resid == 0) 162 return (0); 163 /* 164 * Maybe this should be above the vnode op call, but so long as 165 * file servers have no limits, i don't think it matters 166 */ 167 if (vp->v_type == VREG && 168 uio->uio_offset + uio->uio_resid > 169 u.u_rlimit[RLIMIT_FSIZE].rlim_cur) { 170 psignal(u.u_procp, SIGXFSZ); 171 return (EFBIG); 172 } 173 np->n_flag |= (NMODIFIED|NBUFFERED); 174 do { 175 nfsstats.biocache_writes++; 176 lbn = uio->uio_offset >> NFS_BIOSHIFT; 177 on = uio->uio_offset & (NFS_BIOSIZE-1); 178 n = MIN((unsigned)(NFS_BIOSIZE - on), uio->uio_resid); 179 if (uio->uio_offset+n > np->n_size) 180 np->n_size = uio->uio_offset+n; 181 bn = lbn*(NFS_BIOSIZE/DEV_BSIZE); 182 again: 183 bp = getblk(vp, bn, NFS_BIOSIZE); 184 if (bp->b_wcred == NOCRED) { 185 crhold(cred); 186 bp->b_wcred = cred; 187 } 188 if (bp->b_dirtyend > 0) { 189 /* 190 * If the new write will leave a contiguous dirty 191 * area, just update the b_dirtyoff and b_dirtyend, 192 * otherwise force a write rpc of the old dirty area. 193 */ 194 if (on <= bp->b_dirtyend && (on+n) >= bp->b_dirtyoff) { 195 bp->b_dirtyoff = MIN(on, bp->b_dirtyoff); 196 bp->b_dirtyend = MAX((on+n), bp->b_dirtyend); 197 } else { 198 if (error = bwrite(bp)) 199 return (error); 200 goto again; 201 } 202 } else { 203 bp->b_dirtyoff = on; 204 bp->b_dirtyend = on+n; 205 } 206 if (error = uiomove(bp->b_un.b_addr + on, n, uio)) { 207 brelse(bp); 208 return (error); 209 } 210 if ((n+on) == NFS_BIOSIZE) { 211 bp->b_flags |= B_AGE; 212 bawrite(bp); 213 } else { 214 bdwrite(bp); 215 } 216 } while (error == 0 && uio->uio_resid > 0 && n != 0); 217 #ifdef notdef 218 /* Should we try and do this for nfs ?? */ 219 if (error && (ioflag & IO_UNIT)) { 220 np->n_size = osize; 221 uio->uio_offset -= cnt - uio->uio_resid; 222 uio->uio_resid = cnt; 223 } 224 #endif 225 return (error); 226 } 227