nfs_bio.c revision 1.19 1 1.19 mycroft /* $NetBSD: nfs_bio.c,v 1.19 1995/01/12 12:08:23 mycroft Exp $ */
2 1.15 cgd
3 1.1 cgd /*
4 1.12 mycroft * Copyright (c) 1989, 1993
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Rick Macklem at The University of Guelph.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd *
38 1.15 cgd * @(#)nfs_bio.c 8.5 (Berkeley) 1/4/94
39 1.1 cgd */
40 1.1 cgd
41 1.8 mycroft #include <sys/param.h>
42 1.8 mycroft #include <sys/systm.h>
43 1.12 mycroft #include <sys/resourcevar.h>
44 1.8 mycroft #include <sys/proc.h>
45 1.8 mycroft #include <sys/buf.h>
46 1.8 mycroft #include <sys/vnode.h>
47 1.8 mycroft #include <sys/trace.h>
48 1.8 mycroft #include <sys/mount.h>
49 1.12 mycroft #include <sys/kernel.h>
50 1.12 mycroft
51 1.12 mycroft #include <vm/vm.h>
52 1.1 cgd
53 1.8 mycroft #include <nfs/nfsnode.h>
54 1.12 mycroft #include <nfs/rpcv2.h>
55 1.8 mycroft #include <nfs/nfsv2.h>
56 1.8 mycroft #include <nfs/nfs.h>
57 1.8 mycroft #include <nfs/nfsmount.h>
58 1.12 mycroft #include <nfs/nqnfs.h>
59 1.1 cgd
60 1.12 mycroft struct buf *incore(), *nfs_getcacheblk();
61 1.12 mycroft extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
62 1.12 mycroft extern int nfs_numasync;
63 1.1 cgd
64 1.1 cgd /*
65 1.1 cgd * Vnode op for read using bio
66 1.1 cgd * Any similarity to readip() is purely coincidental
67 1.1 cgd */
68 1.1 cgd nfs_bioread(vp, uio, ioflag, cred)
69 1.1 cgd register struct vnode *vp;
70 1.1 cgd register struct uio *uio;
71 1.1 cgd int ioflag;
72 1.1 cgd struct ucred *cred;
73 1.1 cgd {
74 1.1 cgd register struct nfsnode *np = VTONFS(vp);
75 1.12 mycroft register int biosize, diff;
76 1.12 mycroft struct buf *bp, *rabp;
77 1.1 cgd struct vattr vattr;
78 1.12 mycroft struct proc *p;
79 1.12 mycroft struct nfsmount *nmp;
80 1.12 mycroft daddr_t lbn, bn, rabn;
81 1.12 mycroft caddr_t baddr;
82 1.12 mycroft int got_buf, nra, error = 0, n, on, not_readin;
83 1.1 cgd
84 1.1 cgd #ifdef lint
85 1.1 cgd ioflag = ioflag;
86 1.1 cgd #endif /* lint */
87 1.1 cgd #ifdef DIAGNOSTIC
88 1.1 cgd if (uio->uio_rw != UIO_READ)
89 1.1 cgd panic("nfs_read mode");
90 1.1 cgd #endif
91 1.1 cgd if (uio->uio_resid == 0)
92 1.1 cgd return (0);
93 1.1 cgd if (uio->uio_offset < 0 && vp->v_type != VDIR)
94 1.1 cgd return (EINVAL);
95 1.12 mycroft nmp = VFSTONFS(vp->v_mount);
96 1.12 mycroft biosize = nmp->nm_rsize;
97 1.12 mycroft p = uio->uio_procp;
98 1.1 cgd /*
99 1.12 mycroft * For nfs, cache consistency can only be maintained approximately.
100 1.12 mycroft * Although RFC1094 does not specify the criteria, the following is
101 1.12 mycroft * believed to be compatible with the reference port.
102 1.12 mycroft * For nqnfs, full cache consistency is maintained within the loop.
103 1.12 mycroft * For nfs:
104 1.1 cgd * If the file's modify time on the server has changed since the
105 1.1 cgd * last read rpc or you have written to the file,
106 1.1 cgd * you may have lost data cache consistency with the
107 1.1 cgd * server, so flush all of the file's data out of the cache.
108 1.1 cgd * Then force a getattr rpc to ensure that you have up to date
109 1.1 cgd * attributes.
110 1.12 mycroft * The mount flag NFSMNT_MYWRITE says "Assume that my writes are
111 1.12 mycroft * the ones changing the modify time.
112 1.1 cgd * NB: This implies that cache data can be read when up to
113 1.1 cgd * NFS_ATTRTIMEO seconds out of date. If you find that you need current
114 1.1 cgd * attributes this could be forced by setting n_attrstamp to 0 before
115 1.12 mycroft * the VOP_GETATTR() call.
116 1.1 cgd */
117 1.12 mycroft if ((nmp->nm_flag & NFSMNT_NQNFS) == 0 && vp->v_type != VLNK) {
118 1.1 cgd if (np->n_flag & NMODIFIED) {
119 1.12 mycroft if ((nmp->nm_flag & NFSMNT_MYWRITE) == 0 ||
120 1.12 mycroft vp->v_type != VREG) {
121 1.12 mycroft if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))
122 1.12 mycroft return (error);
123 1.12 mycroft }
124 1.1 cgd np->n_attrstamp = 0;
125 1.1 cgd np->n_direofoffset = 0;
126 1.12 mycroft if (error = VOP_GETATTR(vp, &vattr, cred, p))
127 1.1 cgd return (error);
128 1.10 cgd np->n_mtime = vattr.va_mtime.ts_sec;
129 1.1 cgd } else {
130 1.12 mycroft if (error = VOP_GETATTR(vp, &vattr, cred, p))
131 1.1 cgd return (error);
132 1.10 cgd if (np->n_mtime != vattr.va_mtime.ts_sec) {
133 1.1 cgd np->n_direofoffset = 0;
134 1.12 mycroft if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))
135 1.12 mycroft return (error);
136 1.10 cgd np->n_mtime = vattr.va_mtime.ts_sec;
137 1.1 cgd }
138 1.1 cgd }
139 1.1 cgd }
140 1.1 cgd do {
141 1.12 mycroft
142 1.12 mycroft /*
143 1.12 mycroft * Get a valid lease. If cached data is stale, flush it.
144 1.12 mycroft */
145 1.12 mycroft if (nmp->nm_flag & NFSMNT_NQNFS) {
146 1.12 mycroft if (NQNFS_CKINVALID(vp, np, NQL_READ)) {
147 1.12 mycroft do {
148 1.12 mycroft error = nqnfs_getlease(vp, NQL_READ, cred, p);
149 1.12 mycroft } while (error == NQNFS_EXPIRED);
150 1.12 mycroft if (error)
151 1.12 mycroft return (error);
152 1.12 mycroft if (np->n_lrev != np->n_brev ||
153 1.12 mycroft (np->n_flag & NQNFSNONCACHE) ||
154 1.12 mycroft ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
155 1.12 mycroft if (vp->v_type == VDIR) {
156 1.12 mycroft np->n_direofoffset = 0;
157 1.12 mycroft cache_purge(vp);
158 1.12 mycroft }
159 1.12 mycroft if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))
160 1.12 mycroft return (error);
161 1.12 mycroft np->n_brev = np->n_lrev;
162 1.12 mycroft }
163 1.12 mycroft } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
164 1.12 mycroft np->n_direofoffset = 0;
165 1.12 mycroft cache_purge(vp);
166 1.12 mycroft if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))
167 1.12 mycroft return (error);
168 1.12 mycroft }
169 1.12 mycroft }
170 1.12 mycroft if (np->n_flag & NQNFSNONCACHE) {
171 1.12 mycroft switch (vp->v_type) {
172 1.12 mycroft case VREG:
173 1.12 mycroft error = nfs_readrpc(vp, uio, cred);
174 1.12 mycroft break;
175 1.12 mycroft case VLNK:
176 1.12 mycroft error = nfs_readlinkrpc(vp, uio, cred);
177 1.12 mycroft break;
178 1.12 mycroft case VDIR:
179 1.12 mycroft error = nfs_readdirrpc(vp, uio, cred);
180 1.12 mycroft break;
181 1.12 mycroft };
182 1.12 mycroft return (error);
183 1.12 mycroft }
184 1.12 mycroft baddr = (caddr_t)0;
185 1.1 cgd switch (vp->v_type) {
186 1.1 cgd case VREG:
187 1.1 cgd nfsstats.biocache_reads++;
188 1.1 cgd lbn = uio->uio_offset / biosize;
189 1.1 cgd on = uio->uio_offset & (biosize-1);
190 1.12 mycroft bn = lbn * (biosize / DEV_BSIZE);
191 1.12 mycroft not_readin = 1;
192 1.12 mycroft
193 1.12 mycroft /*
194 1.12 mycroft * Start the read ahead(s), as required.
195 1.12 mycroft */
196 1.12 mycroft if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
197 1.12 mycroft lbn == vp->v_lastr + 1) {
198 1.12 mycroft for (nra = 0; nra < nmp->nm_readahead &&
199 1.12 mycroft (lbn + 1 + nra) * biosize < np->n_size; nra++) {
200 1.12 mycroft rabn = (lbn + 1 + nra) * (biosize / DEV_BSIZE);
201 1.12 mycroft if (!incore(vp, rabn)) {
202 1.12 mycroft rabp = nfs_getcacheblk(vp, rabn, biosize, p);
203 1.12 mycroft if (!rabp)
204 1.12 mycroft return (EINTR);
205 1.12 mycroft if ((rabp->b_flags & (B_DELWRI | B_DONE)) == 0) {
206 1.12 mycroft rabp->b_flags |= (B_READ | B_ASYNC);
207 1.12 mycroft if (nfs_asyncio(rabp, cred)) {
208 1.12 mycroft rabp->b_flags |= B_INVAL;
209 1.12 mycroft brelse(rabp);
210 1.12 mycroft }
211 1.19 mycroft } else
212 1.19 mycroft brelse(rabp);
213 1.12 mycroft }
214 1.12 mycroft }
215 1.12 mycroft }
216 1.12 mycroft
217 1.12 mycroft /*
218 1.12 mycroft * If the block is in the cache and has the required data
219 1.12 mycroft * in a valid region, just copy it out.
220 1.12 mycroft * Otherwise, get the block and write back/read in,
221 1.12 mycroft * as required.
222 1.12 mycroft */
223 1.12 mycroft if ((bp = incore(vp, bn)) &&
224 1.12 mycroft (bp->b_flags & (B_BUSY | B_WRITEINPROG)) ==
225 1.12 mycroft (B_BUSY | B_WRITEINPROG))
226 1.12 mycroft got_buf = 0;
227 1.12 mycroft else {
228 1.12 mycroft again:
229 1.12 mycroft bp = nfs_getcacheblk(vp, bn, biosize, p);
230 1.12 mycroft if (!bp)
231 1.12 mycroft return (EINTR);
232 1.12 mycroft got_buf = 1;
233 1.12 mycroft if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0) {
234 1.12 mycroft bp->b_flags |= B_READ;
235 1.12 mycroft not_readin = 0;
236 1.12 mycroft if (error = nfs_doio(bp, cred, p)) {
237 1.12 mycroft brelse(bp);
238 1.12 mycroft return (error);
239 1.12 mycroft }
240 1.12 mycroft }
241 1.12 mycroft }
242 1.11 cgd n = min((unsigned)(biosize - on), uio->uio_resid);
243 1.1 cgd diff = np->n_size - uio->uio_offset;
244 1.1 cgd if (diff < n)
245 1.1 cgd n = diff;
246 1.12 mycroft if (not_readin && n > 0) {
247 1.12 mycroft if (on < bp->b_validoff || (on + n) > bp->b_validend) {
248 1.12 mycroft if (!got_buf) {
249 1.12 mycroft bp = nfs_getcacheblk(vp, bn, biosize, p);
250 1.12 mycroft if (!bp)
251 1.12 mycroft return (EINTR);
252 1.12 mycroft got_buf = 1;
253 1.12 mycroft }
254 1.12 mycroft bp->b_flags |= B_INVAL;
255 1.12 mycroft if (bp->b_dirtyend > 0) {
256 1.12 mycroft if ((bp->b_flags & B_DELWRI) == 0)
257 1.12 mycroft panic("nfsbioread");
258 1.12 mycroft if (VOP_BWRITE(bp) == EINTR)
259 1.12 mycroft return (EINTR);
260 1.12 mycroft } else
261 1.12 mycroft brelse(bp);
262 1.12 mycroft goto again;
263 1.12 mycroft }
264 1.12 mycroft }
265 1.1 cgd vp->v_lastr = lbn;
266 1.12 mycroft diff = (on >= bp->b_validend) ? 0 : (bp->b_validend - on);
267 1.12 mycroft if (diff < n)
268 1.12 mycroft n = diff;
269 1.1 cgd break;
270 1.1 cgd case VLNK:
271 1.1 cgd nfsstats.biocache_readlinks++;
272 1.12 mycroft bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
273 1.12 mycroft if (!bp)
274 1.12 mycroft return (EINTR);
275 1.12 mycroft if ((bp->b_flags & B_DONE) == 0) {
276 1.12 mycroft bp->b_flags |= B_READ;
277 1.12 mycroft if (error = nfs_doio(bp, cred, p)) {
278 1.12 mycroft brelse(bp);
279 1.12 mycroft return (error);
280 1.12 mycroft }
281 1.12 mycroft }
282 1.12 mycroft n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
283 1.12 mycroft got_buf = 1;
284 1.1 cgd on = 0;
285 1.1 cgd break;
286 1.1 cgd case VDIR:
287 1.18 mycroft if (uio->uio_resid < NFS_DIRBLKSIZ)
288 1.18 mycroft return (0);
289 1.1 cgd nfsstats.biocache_readdirs++;
290 1.12 mycroft bn = (daddr_t)uio->uio_offset;
291 1.12 mycroft bp = nfs_getcacheblk(vp, bn, NFS_DIRBLKSIZ, p);
292 1.12 mycroft if (!bp)
293 1.12 mycroft return (EINTR);
294 1.12 mycroft if ((bp->b_flags & B_DONE) == 0) {
295 1.12 mycroft bp->b_flags |= B_READ;
296 1.12 mycroft if (error = nfs_doio(bp, cred, p)) {
297 1.12 mycroft brelse(bp);
298 1.12 mycroft return (error);
299 1.12 mycroft }
300 1.12 mycroft }
301 1.12 mycroft
302 1.12 mycroft /*
303 1.12 mycroft * If not eof and read aheads are enabled, start one.
304 1.12 mycroft * (You need the current block first, so that you have the
305 1.12 mycroft * directory offset cookie of the next block.
306 1.12 mycroft */
307 1.12 mycroft rabn = bp->b_blkno;
308 1.12 mycroft if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
309 1.12 mycroft rabn != 0 && rabn != np->n_direofoffset &&
310 1.12 mycroft !incore(vp, rabn)) {
311 1.12 mycroft rabp = nfs_getcacheblk(vp, rabn, NFS_DIRBLKSIZ, p);
312 1.12 mycroft if (rabp) {
313 1.12 mycroft if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
314 1.12 mycroft rabp->b_flags |= (B_READ | B_ASYNC);
315 1.12 mycroft if (nfs_asyncio(rabp, cred)) {
316 1.12 mycroft rabp->b_flags |= B_INVAL;
317 1.12 mycroft brelse(rabp);
318 1.12 mycroft }
319 1.19 mycroft } else
320 1.19 mycroft brelse(rabp);
321 1.12 mycroft }
322 1.12 mycroft }
323 1.1 cgd on = 0;
324 1.11 cgd n = min(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid);
325 1.12 mycroft got_buf = 1;
326 1.1 cgd break;
327 1.1 cgd };
328 1.12 mycroft
329 1.12 mycroft if (n > 0) {
330 1.12 mycroft if (!baddr)
331 1.12 mycroft baddr = bp->b_data;
332 1.12 mycroft error = uiomove(baddr + on, (int)n, uio);
333 1.1 cgd }
334 1.1 cgd switch (vp->v_type) {
335 1.1 cgd case VREG:
336 1.12 mycroft if (n + on == biosize || uio->uio_offset == np->n_size)
337 1.1 cgd bp->b_flags |= B_AGE;
338 1.1 cgd break;
339 1.1 cgd case VLNK:
340 1.1 cgd n = 0;
341 1.1 cgd break;
342 1.1 cgd case VDIR:
343 1.1 cgd uio->uio_offset = bp->b_blkno;
344 1.1 cgd break;
345 1.1 cgd };
346 1.12 mycroft if (got_buf)
347 1.12 mycroft brelse(bp);
348 1.12 mycroft } while (error == 0 && uio->uio_resid > 0 && n > 0);
349 1.1 cgd return (error);
350 1.1 cgd }
351 1.1 cgd
352 1.1 cgd /*
353 1.1 cgd * Vnode op for write using bio
354 1.1 cgd */
355 1.12 mycroft nfs_write(ap)
356 1.12 mycroft struct vop_write_args /* {
357 1.12 mycroft struct vnode *a_vp;
358 1.12 mycroft struct uio *a_uio;
359 1.12 mycroft int a_ioflag;
360 1.12 mycroft struct ucred *a_cred;
361 1.12 mycroft } */ *ap;
362 1.1 cgd {
363 1.12 mycroft register int biosize;
364 1.12 mycroft register struct uio *uio = ap->a_uio;
365 1.1 cgd struct proc *p = uio->uio_procp;
366 1.12 mycroft register struct vnode *vp = ap->a_vp;
367 1.12 mycroft struct nfsnode *np = VTONFS(vp);
368 1.12 mycroft register struct ucred *cred = ap->a_cred;
369 1.12 mycroft int ioflag = ap->a_ioflag;
370 1.1 cgd struct buf *bp;
371 1.1 cgd struct vattr vattr;
372 1.12 mycroft struct nfsmount *nmp;
373 1.1 cgd daddr_t lbn, bn;
374 1.1 cgd int n, on, error = 0;
375 1.1 cgd
376 1.1 cgd #ifdef DIAGNOSTIC
377 1.1 cgd if (uio->uio_rw != UIO_WRITE)
378 1.1 cgd panic("nfs_write mode");
379 1.1 cgd if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
380 1.1 cgd panic("nfs_write proc");
381 1.1 cgd #endif
382 1.1 cgd if (vp->v_type != VREG)
383 1.1 cgd return (EIO);
384 1.12 mycroft if (np->n_flag & NWRITEERR) {
385 1.12 mycroft np->n_flag &= ~NWRITEERR;
386 1.12 mycroft return (np->n_error);
387 1.12 mycroft }
388 1.1 cgd if (ioflag & (IO_APPEND | IO_SYNC)) {
389 1.1 cgd if (np->n_flag & NMODIFIED) {
390 1.12 mycroft np->n_attrstamp = 0;
391 1.12 mycroft if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))
392 1.12 mycroft return (error);
393 1.1 cgd }
394 1.1 cgd if (ioflag & IO_APPEND) {
395 1.1 cgd np->n_attrstamp = 0;
396 1.12 mycroft if (error = VOP_GETATTR(vp, &vattr, cred, p))
397 1.1 cgd return (error);
398 1.1 cgd uio->uio_offset = np->n_size;
399 1.1 cgd }
400 1.1 cgd }
401 1.12 mycroft nmp = VFSTONFS(vp->v_mount);
402 1.1 cgd if (uio->uio_offset < 0)
403 1.1 cgd return (EINVAL);
404 1.1 cgd if (uio->uio_resid == 0)
405 1.1 cgd return (0);
406 1.1 cgd /*
407 1.1 cgd * Maybe this should be above the vnode op call, but so long as
408 1.1 cgd * file servers have no limits, i don't think it matters
409 1.1 cgd */
410 1.12 mycroft if (p && uio->uio_offset + uio->uio_resid >
411 1.1 cgd p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
412 1.1 cgd psignal(p, SIGXFSZ);
413 1.1 cgd return (EFBIG);
414 1.1 cgd }
415 1.1 cgd /*
416 1.1 cgd * I use nm_rsize, not nm_wsize so that all buffer cache blocks
417 1.1 cgd * will be the same size within a filesystem. nfs_writerpc will
418 1.1 cgd * still use nm_wsize when sizing the rpc's.
419 1.1 cgd */
420 1.12 mycroft biosize = nmp->nm_rsize;
421 1.1 cgd do {
422 1.16 cgd
423 1.16 cgd /*
424 1.16 cgd * XXX make sure we aren't cached in the VM page cache
425 1.16 cgd */
426 1.16 cgd (void)vnode_pager_uncache(vp);
427 1.12 mycroft
428 1.12 mycroft /*
429 1.12 mycroft * Check for a valid write lease.
430 1.12 mycroft * If non-cachable, just do the rpc
431 1.12 mycroft */
432 1.12 mycroft if ((nmp->nm_flag & NFSMNT_NQNFS) &&
433 1.12 mycroft NQNFS_CKINVALID(vp, np, NQL_WRITE)) {
434 1.12 mycroft do {
435 1.12 mycroft error = nqnfs_getlease(vp, NQL_WRITE, cred, p);
436 1.12 mycroft } while (error == NQNFS_EXPIRED);
437 1.12 mycroft if (error)
438 1.12 mycroft return (error);
439 1.12 mycroft if (np->n_lrev != np->n_brev ||
440 1.12 mycroft (np->n_flag & NQNFSNONCACHE)) {
441 1.12 mycroft if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))
442 1.12 mycroft return (error);
443 1.12 mycroft np->n_brev = np->n_lrev;
444 1.12 mycroft }
445 1.12 mycroft }
446 1.12 mycroft if (np->n_flag & NQNFSNONCACHE)
447 1.12 mycroft return (nfs_writerpc(vp, uio, cred, ioflag));
448 1.1 cgd nfsstats.biocache_writes++;
449 1.1 cgd lbn = uio->uio_offset / biosize;
450 1.1 cgd on = uio->uio_offset & (biosize-1);
451 1.11 cgd n = min((unsigned)(biosize - on), uio->uio_resid);
452 1.12 mycroft bn = lbn * (biosize / DEV_BSIZE);
453 1.1 cgd again:
454 1.12 mycroft bp = nfs_getcacheblk(vp, bn, biosize, p);
455 1.12 mycroft if (!bp)
456 1.12 mycroft return (EINTR);
457 1.12 mycroft if (bp->b_wcred == NOCRED) {
458 1.1 cgd crhold(cred);
459 1.1 cgd bp->b_wcred = cred;
460 1.1 cgd }
461 1.12 mycroft np->n_flag |= NMODIFIED;
462 1.12 mycroft if (uio->uio_offset + n > np->n_size) {
463 1.12 mycroft np->n_size = uio->uio_offset + n;
464 1.12 mycroft vnode_pager_setsize(vp, (u_long)np->n_size);
465 1.12 mycroft }
466 1.12 mycroft
467 1.12 mycroft /*
468 1.12 mycroft * If the new write will leave a contiguous dirty
469 1.12 mycroft * area, just update the b_dirtyoff and b_dirtyend,
470 1.12 mycroft * otherwise force a write rpc of the old dirty area.
471 1.12 mycroft */
472 1.12 mycroft if (bp->b_dirtyend > 0 &&
473 1.12 mycroft (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
474 1.12 mycroft bp->b_proc = p;
475 1.12 mycroft if (VOP_BWRITE(bp) == EINTR)
476 1.12 mycroft return (EINTR);
477 1.12 mycroft goto again;
478 1.12 mycroft }
479 1.12 mycroft
480 1.12 mycroft /*
481 1.12 mycroft * Check for valid write lease and get one as required.
482 1.12 mycroft * In case getblk() and/or bwrite() delayed us.
483 1.12 mycroft */
484 1.12 mycroft if ((nmp->nm_flag & NFSMNT_NQNFS) &&
485 1.12 mycroft NQNFS_CKINVALID(vp, np, NQL_WRITE)) {
486 1.12 mycroft do {
487 1.12 mycroft error = nqnfs_getlease(vp, NQL_WRITE, cred, p);
488 1.12 mycroft } while (error == NQNFS_EXPIRED);
489 1.12 mycroft if (error) {
490 1.12 mycroft brelse(bp);
491 1.12 mycroft return (error);
492 1.12 mycroft }
493 1.12 mycroft if (np->n_lrev != np->n_brev ||
494 1.12 mycroft (np->n_flag & NQNFSNONCACHE)) {
495 1.12 mycroft brelse(bp);
496 1.12 mycroft if (error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1))
497 1.1 cgd return (error);
498 1.12 mycroft np->n_brev = np->n_lrev;
499 1.1 cgd goto again;
500 1.1 cgd }
501 1.12 mycroft }
502 1.12 mycroft if (error = uiomove((char *)bp->b_data + on, n, uio)) {
503 1.12 mycroft bp->b_flags |= B_ERROR;
504 1.12 mycroft brelse(bp);
505 1.12 mycroft return (error);
506 1.12 mycroft }
507 1.12 mycroft if (bp->b_dirtyend > 0) {
508 1.12 mycroft bp->b_dirtyoff = min(on, bp->b_dirtyoff);
509 1.12 mycroft bp->b_dirtyend = max((on + n), bp->b_dirtyend);
510 1.1 cgd } else {
511 1.1 cgd bp->b_dirtyoff = on;
512 1.12 mycroft bp->b_dirtyend = on + n;
513 1.1 cgd }
514 1.12 mycroft #ifndef notdef
515 1.12 mycroft if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff ||
516 1.12 mycroft bp->b_validoff > bp->b_dirtyend) {
517 1.12 mycroft bp->b_validoff = bp->b_dirtyoff;
518 1.12 mycroft bp->b_validend = bp->b_dirtyend;
519 1.12 mycroft } else {
520 1.12 mycroft bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff);
521 1.12 mycroft bp->b_validend = max(bp->b_validend, bp->b_dirtyend);
522 1.1 cgd }
523 1.12 mycroft #else
524 1.12 mycroft bp->b_validoff = bp->b_dirtyoff;
525 1.12 mycroft bp->b_validend = bp->b_dirtyend;
526 1.12 mycroft #endif
527 1.12 mycroft if (ioflag & IO_APPEND)
528 1.12 mycroft bp->b_flags |= B_APPENDWRITE;
529 1.12 mycroft
530 1.12 mycroft /*
531 1.12 mycroft * If the lease is non-cachable or IO_SYNC do bwrite().
532 1.12 mycroft */
533 1.12 mycroft if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
534 1.12 mycroft bp->b_proc = p;
535 1.12 mycroft if (error = VOP_BWRITE(bp))
536 1.12 mycroft return (error);
537 1.12 mycroft } else if ((n + on) == biosize &&
538 1.12 mycroft (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
539 1.1 cgd bp->b_proc = (struct proc *)0;
540 1.1 cgd bawrite(bp);
541 1.12 mycroft } else
542 1.12 mycroft bdwrite(bp);
543 1.12 mycroft } while (uio->uio_resid > 0 && n > 0);
544 1.12 mycroft return (0);
545 1.12 mycroft }
546 1.12 mycroft
547 1.12 mycroft /*
548 1.12 mycroft * Get an nfs cache block.
549 1.12 mycroft * Allocate a new one if the block isn't currently in the cache
550 1.12 mycroft * and return the block marked busy. If the calling process is
551 1.12 mycroft * interrupted by a signal for an interruptible mount point, return
552 1.12 mycroft * NULL.
553 1.12 mycroft */
554 1.12 mycroft struct buf *
555 1.12 mycroft nfs_getcacheblk(vp, bn, size, p)
556 1.12 mycroft struct vnode *vp;
557 1.12 mycroft daddr_t bn;
558 1.12 mycroft int size;
559 1.12 mycroft struct proc *p;
560 1.12 mycroft {
561 1.12 mycroft register struct buf *bp;
562 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
563 1.12 mycroft
564 1.12 mycroft if (nmp->nm_flag & NFSMNT_INT) {
565 1.12 mycroft bp = getblk(vp, bn, size, PCATCH, 0);
566 1.12 mycroft while (bp == (struct buf *)0) {
567 1.12 mycroft if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
568 1.12 mycroft return ((struct buf *)0);
569 1.12 mycroft bp = getblk(vp, bn, size, 0, 2 * hz);
570 1.12 mycroft }
571 1.12 mycroft } else
572 1.12 mycroft bp = getblk(vp, bn, size, 0, 0);
573 1.12 mycroft return (bp);
574 1.12 mycroft }
575 1.12 mycroft
576 1.12 mycroft /*
577 1.12 mycroft * Flush and invalidate all dirty buffers. If another process is already
578 1.12 mycroft * doing the flush, just wait for completion.
579 1.12 mycroft */
580 1.12 mycroft nfs_vinvalbuf(vp, flags, cred, p, intrflg)
581 1.12 mycroft struct vnode *vp;
582 1.12 mycroft int flags;
583 1.12 mycroft struct ucred *cred;
584 1.12 mycroft struct proc *p;
585 1.12 mycroft int intrflg;
586 1.12 mycroft {
587 1.12 mycroft register struct nfsnode *np = VTONFS(vp);
588 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
589 1.12 mycroft int error = 0, slpflag, slptimeo;
590 1.12 mycroft
591 1.12 mycroft if ((nmp->nm_flag & NFSMNT_INT) == 0)
592 1.12 mycroft intrflg = 0;
593 1.12 mycroft if (intrflg) {
594 1.12 mycroft slpflag = PCATCH;
595 1.12 mycroft slptimeo = 2 * hz;
596 1.12 mycroft } else {
597 1.12 mycroft slpflag = 0;
598 1.12 mycroft slptimeo = 0;
599 1.12 mycroft }
600 1.12 mycroft /*
601 1.12 mycroft * First wait for any other process doing a flush to complete.
602 1.12 mycroft */
603 1.12 mycroft while (np->n_flag & NFLUSHINPROG) {
604 1.12 mycroft np->n_flag |= NFLUSHWANT;
605 1.12 mycroft error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
606 1.12 mycroft slptimeo);
607 1.12 mycroft if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
608 1.12 mycroft return (EINTR);
609 1.12 mycroft }
610 1.12 mycroft
611 1.12 mycroft /*
612 1.12 mycroft * Now, flush as required.
613 1.12 mycroft */
614 1.12 mycroft np->n_flag |= NFLUSHINPROG;
615 1.12 mycroft error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
616 1.12 mycroft while (error) {
617 1.12 mycroft if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
618 1.12 mycroft np->n_flag &= ~NFLUSHINPROG;
619 1.12 mycroft if (np->n_flag & NFLUSHWANT) {
620 1.12 mycroft np->n_flag &= ~NFLUSHWANT;
621 1.12 mycroft wakeup((caddr_t)&np->n_flag);
622 1.12 mycroft }
623 1.12 mycroft return (EINTR);
624 1.12 mycroft }
625 1.12 mycroft error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
626 1.12 mycroft }
627 1.12 mycroft np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
628 1.12 mycroft if (np->n_flag & NFLUSHWANT) {
629 1.12 mycroft np->n_flag &= ~NFLUSHWANT;
630 1.12 mycroft wakeup((caddr_t)&np->n_flag);
631 1.12 mycroft }
632 1.12 mycroft return (0);
633 1.12 mycroft }
634 1.12 mycroft
635 1.12 mycroft /*
636 1.12 mycroft * Initiate asynchronous I/O. Return an error if no nfsiods are available.
637 1.12 mycroft * This is mainly to avoid queueing async I/O requests when the nfsiods
638 1.12 mycroft * are all hung on a dead server.
639 1.12 mycroft */
640 1.12 mycroft nfs_asyncio(bp, cred)
641 1.12 mycroft register struct buf *bp;
642 1.12 mycroft struct ucred *cred;
643 1.12 mycroft {
644 1.12 mycroft register int i;
645 1.12 mycroft
646 1.12 mycroft if (nfs_numasync == 0)
647 1.12 mycroft return (EIO);
648 1.12 mycroft for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
649 1.12 mycroft if (nfs_iodwant[i]) {
650 1.12 mycroft if (bp->b_flags & B_READ) {
651 1.12 mycroft if (bp->b_rcred == NOCRED && cred != NOCRED) {
652 1.12 mycroft crhold(cred);
653 1.12 mycroft bp->b_rcred = cred;
654 1.12 mycroft }
655 1.1 cgd } else {
656 1.12 mycroft if (bp->b_wcred == NOCRED && cred != NOCRED) {
657 1.12 mycroft crhold(cred);
658 1.12 mycroft bp->b_wcred = cred;
659 1.12 mycroft }
660 1.12 mycroft }
661 1.12 mycroft
662 1.12 mycroft TAILQ_INSERT_TAIL(&nfs_bufq, bp, b_freelist);
663 1.12 mycroft nfs_iodwant[i] = (struct proc *)0;
664 1.12 mycroft wakeup((caddr_t)&nfs_iodwant[i]);
665 1.12 mycroft return (0);
666 1.12 mycroft }
667 1.12 mycroft return (EIO);
668 1.12 mycroft }
669 1.12 mycroft
670 1.12 mycroft /*
671 1.12 mycroft * Do an I/O operation to/from a cache block. This may be called
672 1.12 mycroft * synchronously or from an nfsiod.
673 1.12 mycroft */
674 1.12 mycroft int
675 1.12 mycroft nfs_doio(bp, cr, p)
676 1.12 mycroft register struct buf *bp;
677 1.12 mycroft struct cred *cr;
678 1.12 mycroft struct proc *p;
679 1.12 mycroft {
680 1.12 mycroft register struct uio *uiop;
681 1.12 mycroft register struct vnode *vp;
682 1.12 mycroft struct nfsnode *np;
683 1.12 mycroft struct nfsmount *nmp;
684 1.12 mycroft int error, diff, len;
685 1.12 mycroft struct uio uio;
686 1.12 mycroft struct iovec io;
687 1.12 mycroft
688 1.12 mycroft vp = bp->b_vp;
689 1.12 mycroft np = VTONFS(vp);
690 1.12 mycroft nmp = VFSTONFS(vp->v_mount);
691 1.12 mycroft uiop = &uio;
692 1.12 mycroft uiop->uio_iov = &io;
693 1.12 mycroft uiop->uio_iovcnt = 1;
694 1.12 mycroft uiop->uio_segflg = UIO_SYSSPACE;
695 1.12 mycroft uiop->uio_procp = p;
696 1.12 mycroft
697 1.12 mycroft /*
698 1.14 pk * Historically, paging was done with physio, but no more...
699 1.12 mycroft */
700 1.14 pk if (bp->b_flags & B_PHYS) {
701 1.14 pk /*
702 1.14 pk * ...though reading /dev/drum still gets us here.
703 1.14 pk */
704 1.14 pk io.iov_len = uiop->uio_resid = bp->b_bcount;
705 1.14 pk /* mapping was done by vmapbuf() */
706 1.14 pk io.iov_base = bp->b_data;
707 1.14 pk uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
708 1.14 pk if (bp->b_flags & B_READ) {
709 1.14 pk uiop->uio_rw = UIO_READ;
710 1.14 pk nfsstats.read_physios++;
711 1.14 pk error = nfs_readrpc(vp, uiop, cr);
712 1.14 pk } else {
713 1.14 pk uiop->uio_rw = UIO_WRITE;
714 1.14 pk nfsstats.write_physios++;
715 1.14 pk error = nfs_writerpc(vp, uiop, cr);
716 1.14 pk }
717 1.14 pk if (error) {
718 1.14 pk bp->b_flags |= B_ERROR;
719 1.14 pk bp->b_error = error;
720 1.14 pk }
721 1.14 pk } else if (bp->b_flags & B_READ) {
722 1.12 mycroft io.iov_len = uiop->uio_resid = bp->b_bcount;
723 1.12 mycroft io.iov_base = bp->b_data;
724 1.12 mycroft uiop->uio_rw = UIO_READ;
725 1.12 mycroft switch (vp->v_type) {
726 1.12 mycroft case VREG:
727 1.12 mycroft uiop->uio_offset = bp->b_blkno * DEV_BSIZE;
728 1.12 mycroft nfsstats.read_bios++;
729 1.12 mycroft error = nfs_readrpc(vp, uiop, cr);
730 1.12 mycroft if (!error) {
731 1.12 mycroft bp->b_validoff = 0;
732 1.12 mycroft if (uiop->uio_resid) {
733 1.12 mycroft /*
734 1.12 mycroft * If len > 0, there is a hole in the file and
735 1.12 mycroft * no writes after the hole have been pushed to
736 1.12 mycroft * the server yet.
737 1.12 mycroft * Just zero fill the rest of the valid area.
738 1.12 mycroft */
739 1.12 mycroft diff = bp->b_bcount - uiop->uio_resid;
740 1.12 mycroft len = np->n_size - (bp->b_blkno * DEV_BSIZE
741 1.12 mycroft + diff);
742 1.12 mycroft if (len > 0) {
743 1.12 mycroft len = min(len, uiop->uio_resid);
744 1.12 mycroft bzero((char *)bp->b_data + diff, len);
745 1.12 mycroft bp->b_validend = diff + len;
746 1.12 mycroft } else
747 1.12 mycroft bp->b_validend = diff;
748 1.12 mycroft } else
749 1.12 mycroft bp->b_validend = bp->b_bcount;
750 1.12 mycroft }
751 1.12 mycroft if (p && (vp->v_flag & VTEXT) &&
752 1.12 mycroft (((nmp->nm_flag & NFSMNT_NQNFS) &&
753 1.17 mycroft NQNFS_CKINVALID(vp, np, NQL_READ) &&
754 1.12 mycroft np->n_lrev != np->n_brev) ||
755 1.12 mycroft (!(nmp->nm_flag & NFSMNT_NQNFS) &&
756 1.12 mycroft np->n_mtime != np->n_vattr.va_mtime.ts_sec))) {
757 1.12 mycroft uprintf("Process killed due to text file modification\n");
758 1.12 mycroft psignal(p, SIGKILL);
759 1.13 mycroft p->p_holdcnt++;
760 1.12 mycroft }
761 1.12 mycroft break;
762 1.12 mycroft case VLNK:
763 1.12 mycroft uiop->uio_offset = 0;
764 1.12 mycroft nfsstats.readlink_bios++;
765 1.12 mycroft error = nfs_readlinkrpc(vp, uiop, cr);
766 1.12 mycroft break;
767 1.12 mycroft case VDIR:
768 1.12 mycroft uiop->uio_offset = bp->b_lblkno;
769 1.12 mycroft nfsstats.readdir_bios++;
770 1.12 mycroft if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS)
771 1.12 mycroft error = nfs_readdirlookrpc(vp, uiop, cr);
772 1.12 mycroft else
773 1.12 mycroft error = nfs_readdirrpc(vp, uiop, cr);
774 1.12 mycroft /*
775 1.12 mycroft * Save offset cookie in b_blkno.
776 1.12 mycroft */
777 1.12 mycroft bp->b_blkno = uiop->uio_offset;
778 1.12 mycroft break;
779 1.12 mycroft };
780 1.12 mycroft if (error) {
781 1.12 mycroft bp->b_flags |= B_ERROR;
782 1.12 mycroft bp->b_error = error;
783 1.12 mycroft }
784 1.12 mycroft } else {
785 1.12 mycroft io.iov_len = uiop->uio_resid = bp->b_dirtyend
786 1.12 mycroft - bp->b_dirtyoff;
787 1.12 mycroft uiop->uio_offset = (bp->b_blkno * DEV_BSIZE)
788 1.12 mycroft + bp->b_dirtyoff;
789 1.12 mycroft io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
790 1.12 mycroft uiop->uio_rw = UIO_WRITE;
791 1.12 mycroft nfsstats.write_bios++;
792 1.12 mycroft if (bp->b_flags & B_APPENDWRITE)
793 1.12 mycroft error = nfs_writerpc(vp, uiop, cr, IO_APPEND);
794 1.12 mycroft else
795 1.12 mycroft error = nfs_writerpc(vp, uiop, cr, 0);
796 1.12 mycroft bp->b_flags &= ~(B_WRITEINPROG | B_APPENDWRITE);
797 1.12 mycroft
798 1.12 mycroft /*
799 1.12 mycroft * For an interrupted write, the buffer is still valid and the
800 1.12 mycroft * write hasn't been pushed to the server yet, so we can't set
801 1.12 mycroft * B_ERROR and report the interruption by setting B_EINTR. For
802 1.12 mycroft * the B_ASYNC case, B_EINTR is not relevant, so the rpc attempt
803 1.12 mycroft * is essentially a noop.
804 1.12 mycroft */
805 1.12 mycroft if (error == EINTR) {
806 1.12 mycroft bp->b_flags &= ~B_INVAL;
807 1.12 mycroft bp->b_flags |= B_DELWRI;
808 1.12 mycroft
809 1.12 mycroft /*
810 1.12 mycroft * Since for the B_ASYNC case, nfs_bwrite() has reassigned the
811 1.12 mycroft * buffer to the clean list, we have to reassign it back to the
812 1.12 mycroft * dirty one. Ugh.
813 1.12 mycroft */
814 1.12 mycroft if (bp->b_flags & B_ASYNC)
815 1.12 mycroft reassignbuf(bp, vp);
816 1.12 mycroft else
817 1.12 mycroft bp->b_flags |= B_EINTR;
818 1.12 mycroft } else {
819 1.12 mycroft if (error) {
820 1.12 mycroft bp->b_flags |= B_ERROR;
821 1.12 mycroft bp->b_error = np->n_error = error;
822 1.12 mycroft np->n_flag |= NWRITEERR;
823 1.1 cgd }
824 1.12 mycroft bp->b_dirtyoff = bp->b_dirtyend = 0;
825 1.12 mycroft }
826 1.1 cgd }
827 1.12 mycroft bp->b_resid = uiop->uio_resid;
828 1.12 mycroft biodone(bp);
829 1.1 cgd return (error);
830 1.1 cgd }
831