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