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