nfs_bio.c revision 1.24 1 /* $NetBSD: nfs_bio.c,v 1.24 1996/02/18 11:53:39 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 * If the lease is non-cachable or IO_SYNC do bwrite().
570 */
571 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
572 bp->b_proc = p;
573 error = VOP_BWRITE(bp);
574 if (error)
575 return (error);
576 if (np->n_flag & NQNFSNONCACHE) {
577 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
578 if (error)
579 return (error);
580 }
581 } else if ((n + on) == biosize &&
582 (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
583 bp->b_proc = (struct proc *)0;
584 bp->b_flags |= B_ASYNC;
585 (void)nfs_writebp(bp, 0);
586 } else {
587 bdwrite(bp);
588 }
589 } while (uio->uio_resid > 0 && n > 0);
590 return (0);
591 }
592
593 /*
594 * Get an nfs cache block.
595 * Allocate a new one if the block isn't currently in the cache
596 * and return the block marked busy. If the calling process is
597 * interrupted by a signal for an interruptible mount point, return
598 * NULL.
599 */
600 struct buf *
601 nfs_getcacheblk(vp, bn, size, p)
602 struct vnode *vp;
603 daddr_t bn;
604 int size;
605 struct proc *p;
606 {
607 register struct buf *bp;
608 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
609
610 if (nmp->nm_flag & NFSMNT_INT) {
611 bp = getblk(vp, bn, size, PCATCH, 0);
612 while (bp == (struct buf *)0) {
613 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
614 return ((struct buf *)0);
615 bp = getblk(vp, bn, size, 0, 2 * hz);
616 }
617 } else
618 bp = getblk(vp, bn, size, 0, 0);
619 return (bp);
620 }
621
622 /*
623 * Flush and invalidate all dirty buffers. If another process is already
624 * doing the flush, just wait for completion.
625 */
626 int
627 nfs_vinvalbuf(vp, flags, cred, p, intrflg)
628 struct vnode *vp;
629 int flags;
630 struct ucred *cred;
631 struct proc *p;
632 int intrflg;
633 {
634 register struct nfsnode *np = VTONFS(vp);
635 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
636 int error = 0, slpflag, slptimeo;
637
638 if ((nmp->nm_flag & NFSMNT_INT) == 0)
639 intrflg = 0;
640 if (intrflg) {
641 slpflag = PCATCH;
642 slptimeo = 2 * hz;
643 } else {
644 slpflag = 0;
645 slptimeo = 0;
646 }
647 /*
648 * First wait for any other process doing a flush to complete.
649 */
650 while (np->n_flag & NFLUSHINPROG) {
651 np->n_flag |= NFLUSHWANT;
652 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
653 slptimeo);
654 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
655 return (EINTR);
656 }
657
658 /*
659 * Now, flush as required.
660 */
661 np->n_flag |= NFLUSHINPROG;
662 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
663 while (error) {
664 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
665 np->n_flag &= ~NFLUSHINPROG;
666 if (np->n_flag & NFLUSHWANT) {
667 np->n_flag &= ~NFLUSHWANT;
668 wakeup((caddr_t)&np->n_flag);
669 }
670 return (EINTR);
671 }
672 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
673 }
674 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
675 if (np->n_flag & NFLUSHWANT) {
676 np->n_flag &= ~NFLUSHWANT;
677 wakeup((caddr_t)&np->n_flag);
678 }
679 return (0);
680 }
681
682 /*
683 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
684 * This is mainly to avoid queueing async I/O requests when the nfsiods
685 * are all hung on a dead server.
686 */
687 int
688 nfs_asyncio(bp, cred)
689 register struct buf *bp;
690 struct ucred *cred;
691 {
692 register int i;
693
694 if (nfs_numasync == 0)
695 return (EIO);
696 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
697 if (nfs_iodwant[i]) {
698 if (bp->b_flags & B_READ) {
699 if (bp->b_rcred == NOCRED && cred != NOCRED) {
700 crhold(cred);
701 bp->b_rcred = cred;
702 }
703 } else {
704 bp->b_flags |= B_WRITEINPROG;
705 if (bp->b_wcred == NOCRED && cred != NOCRED) {
706 crhold(cred);
707 bp->b_wcred = cred;
708 }
709 }
710
711 TAILQ_INSERT_TAIL(&nfs_bufq, bp, b_freelist);
712 nfs_iodwant[i] = (struct proc *)0;
713 wakeup((caddr_t)&nfs_iodwant[i]);
714 return (0);
715 }
716
717 /*
718 * If it is a read or a write already marked B_WRITEINPROG or B_NOCACHE
719 * return EIO so the process will call nfs_doio() and do it
720 * synchronously.
721 */
722 if (bp->b_flags & (B_READ | B_WRITEINPROG | B_NOCACHE))
723 return (EIO);
724
725 /*
726 * Just turn the async write into a delayed write, instead of
727 * doing in synchronously. Hopefully, at least one of the nfsiods
728 * is currently doing a write for this file and will pick up the
729 * delayed writes before going back to sleep.
730 */
731 bp->b_flags |= B_DELWRI;
732 reassignbuf(bp, bp->b_vp);
733 biodone(bp);
734 return (0);
735 }
736
737 /*
738 * Do an I/O operation to/from a cache block. This may be called
739 * synchronously or from an nfsiod.
740 */
741 int
742 nfs_doio(bp, cr, p)
743 register struct buf *bp;
744 struct ucred *cr;
745 struct proc *p;
746 {
747 register struct uio *uiop;
748 register struct vnode *vp;
749 struct nfsnode *np;
750 struct nfsmount *nmp;
751 int error = 0, diff, len, iomode, must_commit = 0;
752 struct uio uio;
753 struct iovec io;
754
755 vp = bp->b_vp;
756 np = VTONFS(vp);
757 nmp = VFSTONFS(vp->v_mount);
758 uiop = &uio;
759 uiop->uio_iov = &io;
760 uiop->uio_iovcnt = 1;
761 uiop->uio_segflg = UIO_SYSSPACE;
762 uiop->uio_procp = p;
763
764 /*
765 * Historically, paging was done with physio, but no more...
766 */
767 if (bp->b_flags & B_PHYS) {
768 /*
769 * ...though reading /dev/drum still gets us here.
770 */
771 io.iov_len = uiop->uio_resid = bp->b_bcount;
772 /* mapping was done by vmapbuf() */
773 io.iov_base = bp->b_data;
774 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
775 if (bp->b_flags & B_READ) {
776 uiop->uio_rw = UIO_READ;
777 nfsstats.read_physios++;
778 error = nfs_readrpc(vp, uiop, cr);
779 } else {
780 iomode = NFSV3WRITE_DATASYNC;
781 uiop->uio_rw = UIO_WRITE;
782 nfsstats.write_physios++;
783 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
784 }
785 if (error) {
786 bp->b_flags |= B_ERROR;
787 bp->b_error = error;
788 }
789 } else if (bp->b_flags & B_READ) {
790 io.iov_len = uiop->uio_resid = bp->b_bcount;
791 io.iov_base = bp->b_data;
792 uiop->uio_rw = UIO_READ;
793 switch (vp->v_type) {
794 case VREG:
795 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
796 nfsstats.read_bios++;
797 error = nfs_readrpc(vp, uiop, cr);
798 if (!error) {
799 bp->b_validoff = 0;
800 if (uiop->uio_resid) {
801 /*
802 * If len > 0, there is a hole in the file and
803 * no writes after the hole have been pushed to
804 * the server yet.
805 * Just zero fill the rest of the valid area.
806 */
807 diff = bp->b_bcount - uiop->uio_resid;
808 len = np->n_size - (((u_quad_t)bp->b_blkno) * DEV_BSIZE
809 + diff);
810 if (len > 0) {
811 len = min(len, uiop->uio_resid);
812 bzero((char *)bp->b_data + diff, len);
813 bp->b_validend = diff + len;
814 } else
815 bp->b_validend = diff;
816 } else
817 bp->b_validend = bp->b_bcount;
818 }
819 if (p && (vp->v_flag & VTEXT) &&
820 (((nmp->nm_flag & NFSMNT_NQNFS) &&
821 NQNFS_CKINVALID(vp, np, ND_READ) &&
822 np->n_lrev != np->n_brev) ||
823 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
824 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) {
825 uprintf("Process killed due to text file modification\n");
826 psignal(p, SIGKILL);
827 p->p_holdcnt++;
828 }
829 break;
830 case VLNK:
831 uiop->uio_offset = (off_t)0;
832 nfsstats.readlink_bios++;
833 error = nfs_readlinkrpc(vp, uiop, cr);
834 break;
835 case VDIR:
836 nfsstats.readdir_bios++;
837 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
838 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
839 error = nfs_readdirplusrpc(vp, uiop, cr);
840 if (error == NFSERR_NOTSUPP)
841 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
842 }
843 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
844 error = nfs_readdirrpc(vp, uiop, cr);
845 break;
846 default:
847 printf("nfs_doio: type %x unexpected\n",vp->v_type);
848 break;
849 };
850 if (error) {
851 bp->b_flags |= B_ERROR;
852 bp->b_error = error;
853 }
854 } else {
855 io.iov_len = uiop->uio_resid = bp->b_dirtyend
856 - bp->b_dirtyoff;
857 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE
858 + bp->b_dirtyoff;
859 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
860 uiop->uio_rw = UIO_WRITE;
861 nfsstats.write_bios++;
862 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE)) == B_ASYNC)
863 iomode = NFSV3WRITE_UNSTABLE;
864 else
865 iomode = NFSV3WRITE_FILESYNC;
866 bp->b_flags |= B_WRITEINPROG;
867 #ifdef fvdl_debug
868 printf("nfs_doio(%x): bp %x doff %d dend %d\n",
869 vp, bp, bp->b_dirtyoff, bp->b_dirtyend);
870 #endif
871 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
872 if (!error && iomode == NFSV3WRITE_UNSTABLE)
873 bp->b_flags |= B_NEEDCOMMIT;
874 else
875 bp->b_flags &= ~B_NEEDCOMMIT;
876 bp->b_flags &= ~B_WRITEINPROG;
877
878 /*
879 * For an interrupted write, the buffer is still valid and the
880 * write hasn't been pushed to the server yet, so we can't set
881 * B_ERROR and report the interruption by setting B_EINTR. For
882 * the B_ASYNC case, B_EINTR is not relevant, so the rpc attempt
883 * is essentially a noop.
884 * For the case of a V3 write rpc not being committed to stable
885 * storage, the block is still dirty and requires either a commit
886 * rpc or another write rpc with iomode == NFSV3WRITE_FILESYNC
887 * before the block is reused. This is indicated by setting the
888 * B_DELWRI and B_NEEDCOMMIT flags.
889 */
890 if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
891 bp->b_flags |= B_DELWRI;
892
893 /*
894 * Since for the B_ASYNC case, nfs_bwrite() has reassigned the
895 * buffer to the clean list, we have to reassign it back to the
896 * dirty one. Ugh.
897 */
898 if (bp->b_flags & B_ASYNC)
899 reassignbuf(bp, vp);
900 else
901 bp->b_flags |= B_EINTR;
902 } else {
903 if (error) {
904 bp->b_flags |= B_ERROR;
905 bp->b_error = np->n_error = error;
906 np->n_flag |= NWRITEERR;
907 }
908 bp->b_dirtyoff = bp->b_dirtyend = 0;
909 }
910 }
911 bp->b_resid = uiop->uio_resid;
912 if (must_commit)
913 nfs_clearcommit(vp->v_mount);
914 biodone(bp);
915 return (error);
916 }
917