nfs_bio.c revision 1.165 1 /* $NetBSD: nfs_bio.c,v 1.165 2007/08/08 12:27:56 yamt 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: nfs_bio.c,v 1.165 2007/08/08 12:27:56 yamt Exp $");
39
40 #include "opt_nfs.h"
41 #include "opt_ddb.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/resourcevar.h>
46 #include <sys/signalvar.h>
47 #include <sys/proc.h>
48 #include <sys/buf.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/kernel.h>
52 #include <sys/namei.h>
53 #include <sys/dirent.h>
54 #include <sys/malloc.h>
55 #include <sys/kauth.h>
56
57 #include <uvm/uvm_extern.h>
58 #include <uvm/uvm.h>
59
60 #include <nfs/rpcv2.h>
61 #include <nfs/nfsproto.h>
62 #include <nfs/nfs.h>
63 #include <nfs/nfsmount.h>
64 #include <nfs/nfsnode.h>
65 #include <nfs/nfs_var.h>
66
67 extern int nfs_numasync;
68 extern int nfs_commitsize;
69 extern struct nfsstats nfsstats;
70
71 static int nfs_doio_read __P((struct buf *, struct uio *));
72 static int nfs_doio_write __P((struct buf *, struct uio *));
73 static int nfs_doio_phys __P((struct buf *, struct uio *));
74
75 /*
76 * Vnode op for read using bio
77 * Any similarity to readip() is purely coincidental
78 */
79 int
80 nfs_bioread(vp, uio, ioflag, cred, cflag)
81 struct vnode *vp;
82 struct uio *uio;
83 int ioflag, cflag;
84 kauth_cred_t cred;
85 {
86 struct nfsnode *np = VTONFS(vp);
87 struct buf *bp = NULL, *rabp;
88 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
89 struct nfsdircache *ndp = NULL, *nndp = NULL;
90 void *baddr;
91 int got_buf = 0, error = 0, n = 0, on = 0, en, enn;
92 int enough = 0;
93 struct dirent *dp, *pdp, *edp, *ep;
94 off_t curoff = 0;
95 int advice;
96 struct lwp *l = curlwp;
97
98 #ifdef DIAGNOSTIC
99 if (uio->uio_rw != UIO_READ)
100 panic("nfs_read mode");
101 #endif
102 if (uio->uio_resid == 0)
103 return (0);
104 if (vp->v_type != VDIR && uio->uio_offset < 0)
105 return (EINVAL);
106 #ifndef NFS_V2_ONLY
107 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
108 !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
109 (void)nfs_fsinfo(nmp, vp, cred, l);
110 #endif
111 if (vp->v_type != VDIR &&
112 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
113 return (EFBIG);
114
115 /*
116 * For nfs, cache consistency can only be maintained approximately.
117 * Although RFC1094 does not specify the criteria, the following is
118 * believed to be compatible with the reference port.
119 *
120 * If the file's modify time on the server has changed since the
121 * last read rpc or you have written to the file,
122 * you may have lost data cache consistency with the
123 * server, so flush all of the file's data out of the cache.
124 * Then force a getattr rpc to ensure that you have up to date
125 * attributes.
126 * NB: This implies that cache data can be read when up to
127 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
128 * attributes this could be forced by setting n_attrstamp to 0 before
129 * the VOP_GETATTR() call.
130 */
131
132 if (vp->v_type != VLNK) {
133 error = nfs_flushstalebuf(vp, cred, l,
134 NFS_FLUSHSTALEBUF_MYWRITE);
135 if (error)
136 return error;
137 }
138
139 do {
140 /*
141 * Don't cache symlinks.
142 */
143 if ((vp->v_flag & VROOT) && vp->v_type == VLNK) {
144 return (nfs_readlinkrpc(vp, uio, cred));
145 }
146 baddr = (void *)0;
147 switch (vp->v_type) {
148 case VREG:
149 nfsstats.biocache_reads++;
150
151 advice = IO_ADV_DECODE(ioflag);
152 error = 0;
153 while (uio->uio_resid > 0) {
154 vsize_t bytelen;
155
156 nfs_delayedtruncate(vp);
157 if (np->n_size <= uio->uio_offset) {
158 break;
159 }
160 bytelen =
161 MIN(np->n_size - uio->uio_offset, uio->uio_resid);
162 error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
163 advice, UBC_READ | UBC_PARTIALOK |
164 (UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0));
165 if (error) {
166 /*
167 * XXXkludge
168 * the file has been truncated on the server.
169 * there isn't much we can do.
170 */
171 if (uio->uio_offset >= np->n_size) {
172 /* end of file */
173 error = 0;
174 } else {
175 break;
176 }
177 }
178 }
179 break;
180
181 case VLNK:
182 nfsstats.biocache_readlinks++;
183 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, l);
184 if (!bp)
185 return (EINTR);
186 if ((bp->b_flags & B_DONE) == 0) {
187 bp->b_flags |= B_READ;
188 error = nfs_doio(bp);
189 if (error) {
190 brelse(bp);
191 return (error);
192 }
193 }
194 n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
195 got_buf = 1;
196 on = 0;
197 break;
198 case VDIR:
199 diragain:
200 nfsstats.biocache_readdirs++;
201 ndp = nfs_searchdircache(vp, uio->uio_offset,
202 (nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
203 if (!ndp) {
204 /*
205 * We've been handed a cookie that is not
206 * in the cache. If we're not translating
207 * 32 <-> 64, it may be a value that was
208 * flushed out of the cache because it grew
209 * too big. Let the server judge if it's
210 * valid or not. In the translation case,
211 * we have no way of validating this value,
212 * so punt.
213 */
214 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
215 return (EINVAL);
216 ndp = nfs_enterdircache(vp, uio->uio_offset,
217 uio->uio_offset, 0, 0);
218 }
219
220 if (NFS_EOFVALID(np) &&
221 ndp->dc_cookie == np->n_direofoffset) {
222 nfs_putdircache(np, ndp);
223 nfsstats.direofcache_hits++;
224 return (0);
225 }
226
227 bp = nfs_getcacheblk(vp, NFSDC_BLKNO(ndp), NFS_DIRBLKSIZ, l);
228 if (!bp)
229 return (EINTR);
230 if ((bp->b_flags & B_DONE) == 0) {
231 bp->b_flags |= B_READ;
232 bp->b_dcookie = ndp->dc_blkcookie;
233 error = nfs_doio(bp);
234 if (error) {
235 /*
236 * Yuck! The directory has been modified on the
237 * server. Punt and let the userland code
238 * deal with it.
239 */
240 nfs_putdircache(np, ndp);
241 brelse(bp);
242 /*
243 * nfs_request maps NFSERR_BAD_COOKIE to EINVAL.
244 */
245 if (error == EINVAL) { /* NFSERR_BAD_COOKIE */
246 nfs_invaldircache(vp, 0);
247 nfs_vinvalbuf(vp, 0, cred, l, 1);
248 }
249 return (error);
250 }
251 }
252
253 /*
254 * Just return if we hit EOF right away with this
255 * block. Always check here, because direofoffset
256 * may have been set by an nfsiod since the last
257 * check.
258 *
259 * also, empty block implies EOF.
260 */
261
262 if (bp->b_bcount == bp->b_resid ||
263 (NFS_EOFVALID(np) &&
264 ndp->dc_blkcookie == np->n_direofoffset)) {
265 KASSERT(bp->b_bcount != bp->b_resid ||
266 ndp->dc_blkcookie == bp->b_dcookie);
267 nfs_putdircache(np, ndp);
268 bp->b_flags |= B_NOCACHE;
269 brelse(bp);
270 return 0;
271 }
272
273 /*
274 * Find the entry we were looking for in the block.
275 */
276
277 en = ndp->dc_entry;
278
279 pdp = dp = (struct dirent *)bp->b_data;
280 edp = (struct dirent *)(void *)((char *)bp->b_data + bp->b_bcount -
281 bp->b_resid);
282 enn = 0;
283 while (enn < en && dp < edp) {
284 pdp = dp;
285 dp = _DIRENT_NEXT(dp);
286 enn++;
287 }
288
289 /*
290 * If the entry number was bigger than the number of
291 * entries in the block, or the cookie of the previous
292 * entry doesn't match, the directory cache is
293 * stale. Flush it and try again (i.e. go to
294 * the server).
295 */
296 if (dp >= edp || (struct dirent *)_DIRENT_NEXT(dp) > edp ||
297 (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
298 #ifdef DEBUG
299 printf("invalid cache: %p %p %p off %lx %lx\n",
300 pdp, dp, edp,
301 (unsigned long)uio->uio_offset,
302 (unsigned long)NFS_GETCOOKIE(pdp));
303 #endif
304 nfs_putdircache(np, ndp);
305 brelse(bp);
306 nfs_invaldircache(vp, 0);
307 nfs_vinvalbuf(vp, 0, cred, l, 0);
308 goto diragain;
309 }
310
311 on = (char *)dp - (char *)bp->b_data;
312
313 /*
314 * Cache all entries that may be exported to the
315 * user, as they may be thrown back at us. The
316 * NFSBIO_CACHECOOKIES flag indicates that all
317 * entries are being 'exported', so cache them all.
318 */
319
320 if (en == 0 && pdp == dp) {
321 dp = _DIRENT_NEXT(dp);
322 enn++;
323 }
324
325 if (uio->uio_resid < (bp->b_bcount - bp->b_resid - on)) {
326 n = uio->uio_resid;
327 enough = 1;
328 } else
329 n = bp->b_bcount - bp->b_resid - on;
330
331 ep = (struct dirent *)(void *)((char *)bp->b_data + on + n);
332
333 /*
334 * Find last complete entry to copy, caching entries
335 * (if requested) as we go.
336 */
337
338 while (dp < ep && (struct dirent *)_DIRENT_NEXT(dp) <= ep) {
339 if (cflag & NFSBIO_CACHECOOKIES) {
340 nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
341 ndp->dc_blkcookie, enn, bp->b_lblkno);
342 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
343 NFS_STASHCOOKIE32(pdp,
344 nndp->dc_cookie32);
345 }
346 nfs_putdircache(np, nndp);
347 }
348 pdp = dp;
349 dp = _DIRENT_NEXT(dp);
350 enn++;
351 }
352 nfs_putdircache(np, ndp);
353
354 /*
355 * If the last requested entry was not the last in the
356 * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
357 * cache the cookie of the last requested one, and
358 * set of the offset to it.
359 */
360
361 if ((on + n) < bp->b_bcount - bp->b_resid) {
362 curoff = NFS_GETCOOKIE(pdp);
363 nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
364 enn, bp->b_lblkno);
365 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
366 NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
367 curoff = nndp->dc_cookie32;
368 }
369 nfs_putdircache(np, nndp);
370 } else
371 curoff = bp->b_dcookie;
372
373 /*
374 * Always cache the entry for the next block,
375 * so that readaheads can use it.
376 */
377 nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
378 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
379 if (curoff == bp->b_dcookie) {
380 NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
381 curoff = nndp->dc_cookie32;
382 }
383 }
384
385 n = (char *)_DIRENT_NEXT(pdp) - ((char *)bp->b_data + on);
386
387 /*
388 * If not eof and read aheads are enabled, start one.
389 * (You need the current block first, so that you have the
390 * directory offset cookie of the next block.)
391 */
392 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
393 !NFS_EOFVALID(np)) {
394 rabp = nfs_getcacheblk(vp, NFSDC_BLKNO(nndp),
395 NFS_DIRBLKSIZ, l);
396 if (rabp) {
397 if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
398 rabp->b_dcookie = nndp->dc_cookie;
399 rabp->b_flags |= (B_READ | B_ASYNC);
400 if (nfs_asyncio(rabp)) {
401 rabp->b_flags |= B_INVAL;
402 brelse(rabp);
403 }
404 } else
405 brelse(rabp);
406 }
407 }
408 nfs_putdircache(np, nndp);
409 got_buf = 1;
410 break;
411 default:
412 printf(" nfsbioread: type %x unexpected\n",vp->v_type);
413 break;
414 }
415
416 if (n > 0) {
417 if (!baddr)
418 baddr = bp->b_data;
419 error = uiomove((char *)baddr + on, (int)n, uio);
420 }
421 switch (vp->v_type) {
422 case VREG:
423 break;
424 case VLNK:
425 n = 0;
426 break;
427 case VDIR:
428 uio->uio_offset = curoff;
429 if (enough)
430 n = 0;
431 break;
432 default:
433 printf(" nfsbioread: type %x unexpected\n",vp->v_type);
434 }
435 if (got_buf)
436 brelse(bp);
437 } while (error == 0 && uio->uio_resid > 0 && n > 0);
438 return (error);
439 }
440
441 /*
442 * Vnode op for write using bio
443 */
444 int
445 nfs_write(v)
446 void *v;
447 {
448 struct vop_write_args /* {
449 struct vnode *a_vp;
450 struct uio *a_uio;
451 int a_ioflag;
452 kauth_cred_t a_cred;
453 } */ *ap = v;
454 struct uio *uio = ap->a_uio;
455 struct lwp *l = curlwp;
456 struct vnode *vp = ap->a_vp;
457 struct nfsnode *np = VTONFS(vp);
458 kauth_cred_t cred = ap->a_cred;
459 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
460 voff_t oldoff, origoff;
461 vsize_t bytelen;
462 int error = 0;
463 int ioflag = ap->a_ioflag;
464 int extended = 0, wrotedata = 0;
465
466 #ifdef DIAGNOSTIC
467 if (uio->uio_rw != UIO_WRITE)
468 panic("nfs_write mode");
469 #endif
470 if (vp->v_type != VREG)
471 return (EIO);
472 if (np->n_flag & NWRITEERR) {
473 np->n_flag &= ~NWRITEERR;
474 return (np->n_error);
475 }
476 #ifndef NFS_V2_ONLY
477 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
478 !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
479 (void)nfs_fsinfo(nmp, vp, cred, l);
480 #endif
481 if (ioflag & IO_APPEND) {
482 NFS_INVALIDATE_ATTRCACHE(np);
483 error = nfs_flushstalebuf(vp, cred, l,
484 NFS_FLUSHSTALEBUF_MYWRITE);
485 if (error)
486 return (error);
487 uio->uio_offset = np->n_size;
488 }
489 if (uio->uio_offset < 0)
490 return (EINVAL);
491 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
492 return (EFBIG);
493 if (uio->uio_resid == 0)
494 return (0);
495 /*
496 * Maybe this should be above the vnode op call, but so long as
497 * file servers have no limits, i don't think it matters
498 */
499 if (l && l->l_proc && uio->uio_offset + uio->uio_resid >
500 l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
501 mutex_enter(&proclist_mutex);
502 psignal(l->l_proc, SIGXFSZ);
503 mutex_exit(&proclist_mutex);
504 return (EFBIG);
505 }
506
507 origoff = uio->uio_offset;
508 do {
509 bool overwrite; /* if we are overwriting whole pages */
510 u_quad_t oldsize;
511 oldoff = uio->uio_offset;
512 bytelen = uio->uio_resid;
513
514 nfsstats.biocache_writes++;
515
516 oldsize = np->n_size;
517 np->n_flag |= NMODIFIED;
518 if (np->n_size < uio->uio_offset + bytelen) {
519 np->n_size = uio->uio_offset + bytelen;
520 }
521 overwrite = false;
522 if ((uio->uio_offset & PAGE_MASK) == 0) {
523 if ((vp->v_flag & VMAPPED) == 0 &&
524 bytelen > PAGE_SIZE) {
525 bytelen = trunc_page(bytelen);
526 overwrite = true;
527 } else if ((bytelen & PAGE_MASK) == 0 &&
528 uio->uio_offset >= vp->v_size) {
529 overwrite = true;
530 }
531 }
532 if (vp->v_size < uio->uio_offset + bytelen) {
533 uvm_vnp_setwritesize(vp, uio->uio_offset + bytelen);
534 }
535 error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
536 UVM_ADV_RANDOM, UBC_WRITE | UBC_PARTIALOK |
537 (overwrite ? UBC_FAULTBUSY : 0) |
538 (UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0));
539 if (error) {
540 uvm_vnp_setwritesize(vp, vp->v_size);
541 if (overwrite && np->n_size != oldsize) {
542 /*
543 * backout size and free pages past eof.
544 */
545 np->n_size = oldsize;
546 simple_lock(&vp->v_interlock);
547 (void)VOP_PUTPAGES(vp, round_page(vp->v_size),
548 0, PGO_SYNCIO | PGO_FREE);
549 }
550 break;
551 }
552 wrotedata = 1;
553
554 /*
555 * update UVM's notion of the size now that we've
556 * copied the data into the vnode's pages.
557 */
558
559 if (vp->v_size < uio->uio_offset) {
560 uvm_vnp_setsize(vp, uio->uio_offset);
561 extended = 1;
562 }
563
564 if ((oldoff & ~(nmp->nm_wsize - 1)) !=
565 (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
566 simple_lock(&vp->v_interlock);
567 error = VOP_PUTPAGES(vp,
568 trunc_page(oldoff & ~(nmp->nm_wsize - 1)),
569 round_page((uio->uio_offset + nmp->nm_wsize - 1) &
570 ~(nmp->nm_wsize - 1)), PGO_CLEANIT);
571 }
572 } while (uio->uio_resid > 0);
573 if (wrotedata)
574 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
575 if (error == 0 && (ioflag & IO_SYNC) != 0) {
576 simple_lock(&vp->v_interlock);
577 error = VOP_PUTPAGES(vp,
578 trunc_page(origoff & ~(nmp->nm_wsize - 1)),
579 round_page((uio->uio_offset + nmp->nm_wsize - 1) &
580 ~(nmp->nm_wsize - 1)),
581 PGO_CLEANIT | PGO_SYNCIO);
582 }
583 return error;
584 }
585
586 /*
587 * Get an nfs cache block.
588 * Allocate a new one if the block isn't currently in the cache
589 * and return the block marked busy. If the calling process is
590 * interrupted by a signal for an interruptible mount point, return
591 * NULL.
592 */
593 struct buf *
594 nfs_getcacheblk(vp, bn, size, l)
595 struct vnode *vp;
596 daddr_t bn;
597 int size;
598 struct lwp *l;
599 {
600 struct buf *bp;
601 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
602
603 if (nmp->nm_flag & NFSMNT_INT) {
604 bp = getblk(vp, bn, size, PCATCH, 0);
605 while (bp == NULL) {
606 if (nfs_sigintr(nmp, NULL, l))
607 return (NULL);
608 bp = getblk(vp, bn, size, 0, 2 * hz);
609 }
610 } else
611 bp = getblk(vp, bn, size, 0, 0);
612 return (bp);
613 }
614
615 /*
616 * Flush and invalidate all dirty buffers. If another process is already
617 * doing the flush, just wait for completion.
618 */
619 int
620 nfs_vinvalbuf(vp, flags, cred, l, intrflg)
621 struct vnode *vp;
622 int flags;
623 kauth_cred_t cred;
624 struct lwp *l;
625 int intrflg;
626 {
627 struct nfsnode *np = VTONFS(vp);
628 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
629 int error = 0, slpflag, slptimeo;
630
631 if ((nmp->nm_flag & NFSMNT_INT) == 0)
632 intrflg = 0;
633 if (intrflg) {
634 slpflag = PCATCH;
635 slptimeo = 2 * hz;
636 } else {
637 slpflag = 0;
638 slptimeo = 0;
639 }
640 /*
641 * First wait for any other process doing a flush to complete.
642 */
643 simple_lock(&vp->v_interlock);
644 while (np->n_flag & NFLUSHINPROG) {
645 np->n_flag |= NFLUSHWANT;
646 error = ltsleep(&np->n_flag, PRIBIO + 2, "nfsvinval",
647 slptimeo, &vp->v_interlock);
648 if (error && intrflg && nfs_sigintr(nmp, NULL, l)) {
649 simple_unlock(&vp->v_interlock);
650 return EINTR;
651 }
652 }
653
654 /*
655 * Now, flush as required.
656 */
657 np->n_flag |= NFLUSHINPROG;
658 simple_unlock(&vp->v_interlock);
659 error = vinvalbuf(vp, flags, cred, l, slpflag, 0);
660 while (error) {
661 if (intrflg && nfs_sigintr(nmp, NULL, l)) {
662 error = EINTR;
663 break;
664 }
665 error = vinvalbuf(vp, flags, cred, l, 0, slptimeo);
666 }
667 simple_lock(&vp->v_interlock);
668 if (error == 0)
669 np->n_flag &= ~NMODIFIED;
670 np->n_flag &= ~NFLUSHINPROG;
671 if (np->n_flag & NFLUSHWANT) {
672 np->n_flag &= ~NFLUSHWANT;
673 wakeup(&np->n_flag);
674 }
675 simple_unlock(&vp->v_interlock);
676 return error;
677 }
678
679 /*
680 * nfs_flushstalebuf: flush cache if it's stale.
681 *
682 * => caller shouldn't own any pages or buffers which belong to the vnode.
683 */
684
685 int
686 nfs_flushstalebuf(struct vnode *vp, kauth_cred_t cred, struct lwp *l,
687 int flags)
688 {
689 struct nfsnode *np = VTONFS(vp);
690 struct vattr vattr;
691 int error;
692
693 if (np->n_flag & NMODIFIED) {
694 if ((flags & NFS_FLUSHSTALEBUF_MYWRITE) == 0
695 || vp->v_type != VREG) {
696 error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
697 if (error)
698 return error;
699 if (vp->v_type == VDIR) {
700 nfs_invaldircache(vp, 0);
701 }
702 } else {
703 /*
704 * XXX assuming writes are ours.
705 */
706 }
707 NFS_INVALIDATE_ATTRCACHE(np);
708 error = VOP_GETATTR(vp, &vattr, cred, l);
709 if (error)
710 return error;
711 np->n_mtime = vattr.va_mtime;
712 } else {
713 error = VOP_GETATTR(vp, &vattr, cred, l);
714 if (error)
715 return error;
716 if (timespeccmp(&np->n_mtime, &vattr.va_mtime, !=)) {
717 if (vp->v_type == VDIR) {
718 nfs_invaldircache(vp, 0);
719 }
720 error = nfs_vinvalbuf(vp, V_SAVE, cred, l, 1);
721 if (error)
722 return error;
723 np->n_mtime = vattr.va_mtime;
724 }
725 }
726
727 return error;
728 }
729
730 /*
731 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
732 * This is mainly to avoid queueing async I/O requests when the nfsiods
733 * are all hung on a dead server.
734 */
735
736 int
737 nfs_asyncio(bp)
738 struct buf *bp;
739 {
740 int i;
741 struct nfsmount *nmp;
742 int gotiod, slptimeo = 0, error;
743 bool catch = false;
744
745 if (nfs_numasync == 0)
746 return (EIO);
747
748 nmp = VFSTONFS(bp->b_vp->v_mount);
749 again:
750 if (nmp->nm_flag & NFSMNT_INT)
751 catch = true;
752 gotiod = false;
753
754 /*
755 * Find a free iod to process this request.
756 */
757
758 mutex_enter(&nfs_iodlist_lock);
759 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
760 struct nfs_iod *iod = &nfs_asyncdaemon[i];
761
762 mutex_enter(&iod->nid_lock);
763 if (iod->nid_want) {
764 /*
765 * Found one, so wake it up and tell it which
766 * mount to process.
767 */
768 iod->nid_want = false;
769 iod->nid_mount = nmp;
770 cv_signal(&iod->nid_cv);
771 mutex_enter(&nmp->nm_lock);
772 mutex_exit(&iod->nid_lock);
773 nmp->nm_bufqiods++;
774 if (nmp->nm_bufqlen < 2 * nmp->nm_bufqiods) {
775 cv_broadcast(&nmp->nm_aiocv);
776 }
777 gotiod = true;
778 break;
779 }
780 mutex_exit(&iod->nid_lock);
781 }
782 mutex_exit(&nfs_iodlist_lock);
783
784 /*
785 * If none are free, we may already have an iod working on this mount
786 * point. If so, it will process our request.
787 */
788
789 if (!gotiod) {
790 mutex_enter(&nmp->nm_lock);
791 if (nmp->nm_bufqiods > 0)
792 gotiod = true;
793 }
794
795 KASSERT(mutex_owned(&nmp->nm_lock));
796
797 /*
798 * If we have an iod which can process the request, then queue
799 * the buffer. However, even if we have an iod, do not initiate
800 * queue cleaning if curproc is the pageout daemon. if the NFS mount
801 * is via local loopback, we may put curproc (pagedaemon) to sleep
802 * waiting for the writes to complete. But the server (ourself)
803 * may block the write, waiting for its (ie., our) pagedaemon
804 * to produce clean pages to handle the write: deadlock.
805 * XXX: start non-loopback mounts straight away? If "lots free",
806 * let pagedaemon start loopback writes anyway?
807 */
808 if (gotiod) {
809
810 /*
811 * Ensure that the queue never grows too large.
812 */
813 if (curlwp == uvm.pagedaemon_lwp) {
814 /* Enque for later, to avoid free-page deadlock */
815 } else while (nmp->nm_bufqlen >= 2 * nmp->nm_bufqiods) {
816 if (catch) {
817 error = cv_timedwait_sig(&nmp->nm_aiocv,
818 &nmp->nm_lock, slptimeo);
819 } else {
820 error = cv_timedwait(&nmp->nm_aiocv,
821 &nmp->nm_lock, slptimeo);
822 }
823 if (error) {
824 if (nfs_sigintr(nmp, NULL, curlwp)) {
825 mutex_exit(&nmp->nm_lock);
826 return (EINTR);
827 }
828 if (catch) {
829 catch = false;
830 slptimeo = 2 * hz;
831 }
832 }
833
834 /*
835 * We might have lost our iod while sleeping,
836 * so check and loop if necessary.
837 */
838
839 if (nmp->nm_bufqiods == 0) {
840 mutex_exit(&nmp->nm_lock);
841 goto again;
842 }
843 }
844 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
845 nmp->nm_bufqlen++;
846 mutex_exit(&nmp->nm_lock);
847 return (0);
848 }
849 mutex_exit(&nmp->nm_lock);
850
851 /*
852 * All the iods are busy on other mounts, so return EIO to
853 * force the caller to process the i/o synchronously.
854 */
855
856 return (EIO);
857 }
858
859 /*
860 * nfs_doio for read.
861 */
862 static int
863 nfs_doio_read(bp, uiop)
864 struct buf *bp;
865 struct uio *uiop;
866 {
867 struct vnode *vp = bp->b_vp;
868 struct nfsnode *np = VTONFS(vp);
869 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
870 int error = 0;
871
872 uiop->uio_rw = UIO_READ;
873 switch (vp->v_type) {
874 case VREG:
875 nfsstats.read_bios++;
876 error = nfs_readrpc(vp, uiop);
877 if (!error && uiop->uio_resid) {
878 int diff, len;
879
880 /*
881 * If uio_resid > 0, there is a hole in the file and
882 * no writes after the hole have been pushed to
883 * the server yet or the file has been truncated
884 * on the server.
885 * Just zero fill the rest of the valid area.
886 */
887
888 KASSERT(vp->v_size >=
889 uiop->uio_offset + uiop->uio_resid);
890 diff = bp->b_bcount - uiop->uio_resid;
891 len = uiop->uio_resid;
892 memset((char *)bp->b_data + diff, 0, len);
893 uiop->uio_resid = 0;
894 }
895 #if 0
896 if (uiop->uio_lwp && (vp->v_flag & VTEXT) &&
897 timespeccmp(&np->n_mtime, &np->n_vattr->va_mtime, !=)) {
898 killproc(uiop->uio_lwp->l_proc, "process text file was modified");
899 #if 0 /* XXX NJWLWP */
900 uiop->uio_lwp->l_proc->p_holdcnt++;
901 #endif
902 }
903 #endif
904 break;
905 case VLNK:
906 KASSERT(uiop->uio_offset == (off_t)0);
907 nfsstats.readlink_bios++;
908 error = nfs_readlinkrpc(vp, uiop, np->n_rcred);
909 break;
910 case VDIR:
911 nfsstats.readdir_bios++;
912 uiop->uio_offset = bp->b_dcookie;
913 #ifndef NFS_V2_ONLY
914 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
915 error = nfs_readdirplusrpc(vp, uiop,
916 curlwp->l_cred);
917 /*
918 * nfs_request maps NFSERR_NOTSUPP to ENOTSUP.
919 */
920 if (error == ENOTSUP)
921 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
922 }
923 #else
924 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
925 #endif
926 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
927 error = nfs_readdirrpc(vp, uiop,
928 curlwp->l_cred);
929 if (!error) {
930 bp->b_dcookie = uiop->uio_offset;
931 }
932 break;
933 default:
934 printf("nfs_doio: type %x unexpected\n", vp->v_type);
935 break;
936 }
937 if (error) {
938 bp->b_error = error;
939 }
940 return error;
941 }
942
943 /*
944 * nfs_doio for write.
945 */
946 static int
947 nfs_doio_write(bp, uiop)
948 struct buf *bp;
949 struct uio *uiop;
950 {
951 struct vnode *vp = bp->b_vp;
952 struct nfsnode *np = VTONFS(vp);
953 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
954 int iomode;
955 bool stalewriteverf = false;
956 int i, npages = (bp->b_bcount + PAGE_SIZE - 1) >> PAGE_SHIFT;
957 struct vm_page *pgs[npages];
958 #ifndef NFS_V2_ONLY
959 bool needcommit = true; /* need only COMMIT RPC */
960 #else
961 bool needcommit = false; /* need only COMMIT RPC */
962 #endif
963 bool pageprotected;
964 struct uvm_object *uobj = &vp->v_uobj;
965 int error;
966 off_t off, cnt;
967
968 if ((bp->b_flags & B_ASYNC) != 0 && NFS_ISV3(vp)) {
969 iomode = NFSV3WRITE_UNSTABLE;
970 } else {
971 iomode = NFSV3WRITE_FILESYNC;
972 }
973
974 #ifndef NFS_V2_ONLY
975 again:
976 #endif
977 rw_enter(&nmp->nm_writeverflock, RW_READER);
978
979 for (i = 0; i < npages; i++) {
980 pgs[i] = uvm_pageratop((vaddr_t)bp->b_data + (i << PAGE_SHIFT));
981 if (pgs[i]->uobject == uobj &&
982 pgs[i]->offset == uiop->uio_offset + (i << PAGE_SHIFT)) {
983 KASSERT(pgs[i]->flags & PG_BUSY);
984 /*
985 * this page belongs to our object.
986 */
987 simple_lock(&uobj->vmobjlock);
988 /*
989 * write out the page stably if it's about to
990 * be released because we can't resend it
991 * on the server crash.
992 *
993 * XXX assuming PG_RELEASE|PG_PAGEOUT won't be
994 * changed until unbusy the page.
995 */
996 if (pgs[i]->flags & (PG_RELEASED|PG_PAGEOUT))
997 iomode = NFSV3WRITE_FILESYNC;
998 /*
999 * if we met a page which hasn't been sent yet,
1000 * we need do WRITE RPC.
1001 */
1002 if ((pgs[i]->flags & PG_NEEDCOMMIT) == 0)
1003 needcommit = false;
1004 simple_unlock(&uobj->vmobjlock);
1005 } else {
1006 iomode = NFSV3WRITE_FILESYNC;
1007 needcommit = false;
1008 }
1009 }
1010 if (!needcommit && iomode == NFSV3WRITE_UNSTABLE) {
1011 simple_lock(&uobj->vmobjlock);
1012 for (i = 0; i < npages; i++) {
1013 pgs[i]->flags |= PG_NEEDCOMMIT | PG_RDONLY;
1014 pmap_page_protect(pgs[i], VM_PROT_READ);
1015 }
1016 simple_unlock(&uobj->vmobjlock);
1017 pageprotected = true; /* pages can't be modified during i/o. */
1018 } else
1019 pageprotected = false;
1020
1021 /*
1022 * Send the data to the server if necessary,
1023 * otherwise just send a commit rpc.
1024 */
1025 #ifndef NFS_V2_ONLY
1026 if (needcommit) {
1027
1028 /*
1029 * If the buffer is in the range that we already committed,
1030 * there's nothing to do.
1031 *
1032 * If it's in the range that we need to commit, push the
1033 * whole range at once, otherwise only push the buffer.
1034 * In both these cases, acquire the commit lock to avoid
1035 * other processes modifying the range.
1036 */
1037
1038 off = uiop->uio_offset;
1039 cnt = bp->b_bcount;
1040 mutex_enter(&np->n_commitlock);
1041 if (!nfs_in_committed_range(vp, off, bp->b_bcount)) {
1042 bool pushedrange;
1043 if (nfs_in_tobecommitted_range(vp, off, bp->b_bcount)) {
1044 pushedrange = true;
1045 off = np->n_pushlo;
1046 cnt = np->n_pushhi - np->n_pushlo;
1047 } else {
1048 pushedrange = false;
1049 }
1050 error = nfs_commit(vp, off, cnt, curlwp);
1051 if (error == 0) {
1052 if (pushedrange) {
1053 nfs_merge_commit_ranges(vp);
1054 } else {
1055 nfs_add_committed_range(vp, off, cnt);
1056 }
1057 }
1058 } else {
1059 error = 0;
1060 }
1061 mutex_exit(&np->n_commitlock);
1062 rw_exit(&nmp->nm_writeverflock);
1063 if (!error) {
1064 /*
1065 * pages are now on stable storage.
1066 */
1067 uiop->uio_resid = 0;
1068 simple_lock(&uobj->vmobjlock);
1069 for (i = 0; i < npages; i++) {
1070 pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1071 }
1072 simple_unlock(&uobj->vmobjlock);
1073 return 0;
1074 } else if (error == NFSERR_STALEWRITEVERF) {
1075 nfs_clearcommit(vp->v_mount);
1076 goto again;
1077 }
1078 if (error) {
1079 bp->b_error = np->n_error = error;
1080 np->n_flag |= NWRITEERR;
1081 }
1082 return error;
1083 }
1084 #endif
1085 off = uiop->uio_offset;
1086 cnt = bp->b_bcount;
1087 uiop->uio_rw = UIO_WRITE;
1088 nfsstats.write_bios++;
1089 error = nfs_writerpc(vp, uiop, &iomode, pageprotected, &stalewriteverf);
1090 #ifndef NFS_V2_ONLY
1091 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1092 /*
1093 * we need to commit pages later.
1094 */
1095 mutex_enter(&np->n_commitlock);
1096 nfs_add_tobecommitted_range(vp, off, cnt);
1097 /*
1098 * if there can be too many uncommitted pages, commit them now.
1099 */
1100 if (np->n_pushhi - np->n_pushlo > nfs_commitsize) {
1101 off = np->n_pushlo;
1102 cnt = nfs_commitsize >> 1;
1103 error = nfs_commit(vp, off, cnt, curlwp);
1104 if (!error) {
1105 nfs_add_committed_range(vp, off, cnt);
1106 nfs_del_tobecommitted_range(vp, off, cnt);
1107 }
1108 if (error == NFSERR_STALEWRITEVERF) {
1109 stalewriteverf = true;
1110 error = 0; /* it isn't a real error */
1111 }
1112 } else {
1113 /*
1114 * re-dirty pages so that they will be passed
1115 * to us later again.
1116 */
1117 simple_lock(&uobj->vmobjlock);
1118 for (i = 0; i < npages; i++) {
1119 pgs[i]->flags &= ~PG_CLEAN;
1120 }
1121 simple_unlock(&uobj->vmobjlock);
1122 }
1123 mutex_exit(&np->n_commitlock);
1124 } else
1125 #endif
1126 if (!error) {
1127 /*
1128 * pages are now on stable storage.
1129 */
1130 mutex_enter(&np->n_commitlock);
1131 nfs_del_committed_range(vp, off, cnt);
1132 mutex_exit(&np->n_commitlock);
1133 simple_lock(&uobj->vmobjlock);
1134 for (i = 0; i < npages; i++) {
1135 pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1136 }
1137 simple_unlock(&uobj->vmobjlock);
1138 } else {
1139 /*
1140 * we got an error.
1141 */
1142 bp->b_error = np->n_error = error;
1143 np->n_flag |= NWRITEERR;
1144 }
1145
1146 rw_exit(&nmp->nm_writeverflock);
1147
1148 if (stalewriteverf) {
1149 nfs_clearcommit(vp->v_mount);
1150 }
1151 return error;
1152 }
1153
1154 /*
1155 * nfs_doio for B_PHYS.
1156 */
1157 static int
1158 nfs_doio_phys(bp, uiop)
1159 struct buf *bp;
1160 struct uio *uiop;
1161 {
1162 struct vnode *vp = bp->b_vp;
1163 int error;
1164
1165 uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
1166 if (bp->b_flags & B_READ) {
1167 uiop->uio_rw = UIO_READ;
1168 nfsstats.read_physios++;
1169 error = nfs_readrpc(vp, uiop);
1170 } else {
1171 int iomode = NFSV3WRITE_DATASYNC;
1172 bool stalewriteverf;
1173 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1174
1175 uiop->uio_rw = UIO_WRITE;
1176 nfsstats.write_physios++;
1177 rw_enter(&nmp->nm_writeverflock, RW_READER);
1178 error = nfs_writerpc(vp, uiop, &iomode, false, &stalewriteverf);
1179 rw_exit(&nmp->nm_writeverflock);
1180 if (stalewriteverf) {
1181 nfs_clearcommit(bp->b_vp->v_mount);
1182 }
1183 }
1184 if (error) {
1185 bp->b_error = error;
1186 }
1187 return error;
1188 }
1189
1190 /*
1191 * Do an I/O operation to/from a cache block. This may be called
1192 * synchronously or from an nfsiod.
1193 */
1194 int
1195 nfs_doio(bp)
1196 struct buf *bp;
1197 {
1198 int error;
1199 struct uio uio;
1200 struct uio *uiop = &uio;
1201 struct iovec io;
1202 UVMHIST_FUNC("nfs_doio"); UVMHIST_CALLED(ubchist);
1203
1204 uiop->uio_iov = &io;
1205 uiop->uio_iovcnt = 1;
1206 uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
1207 UIO_SETUP_SYSSPACE(uiop);
1208 io.iov_base = bp->b_data;
1209 io.iov_len = uiop->uio_resid = bp->b_bcount;
1210
1211 /*
1212 * Historically, paging was done with physio, but no more...
1213 */
1214 if (bp->b_flags & B_PHYS) {
1215 /*
1216 * ...though reading /dev/drum still gets us here.
1217 */
1218 error = nfs_doio_phys(bp, uiop);
1219 } else if (bp->b_flags & B_READ) {
1220 error = nfs_doio_read(bp, uiop);
1221 } else {
1222 error = nfs_doio_write(bp, uiop);
1223 }
1224 bp->b_resid = uiop->uio_resid;
1225 biodone(bp);
1226 return (error);
1227 }
1228
1229 /*
1230 * Vnode op for VM getpages.
1231 */
1232
1233 int
1234 nfs_getpages(v)
1235 void *v;
1236 {
1237 struct vop_getpages_args /* {
1238 struct vnode *a_vp;
1239 voff_t a_offset;
1240 struct vm_page **a_m;
1241 int *a_count;
1242 int a_centeridx;
1243 vm_prot_t a_access_type;
1244 int a_advice;
1245 int a_flags;
1246 } */ *ap = v;
1247
1248 struct vnode *vp = ap->a_vp;
1249 struct uvm_object *uobj = &vp->v_uobj;
1250 struct nfsnode *np = VTONFS(vp);
1251 const int npages = *ap->a_count;
1252 struct vm_page *pg, **pgs, *opgs[npages];
1253 off_t origoffset, len;
1254 int i, error;
1255 bool v3 = NFS_ISV3(vp);
1256 bool write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1257 bool locked = (ap->a_flags & PGO_LOCKED) != 0;
1258
1259 /*
1260 * call the genfs code to get the pages. `pgs' may be NULL
1261 * when doing read-ahead.
1262 */
1263
1264 pgs = ap->a_m;
1265 if (write && locked && v3) {
1266 KASSERT(pgs != NULL);
1267 #ifdef DEBUG
1268
1269 /*
1270 * If PGO_LOCKED is set, real pages shouldn't exists
1271 * in the array.
1272 */
1273
1274 for (i = 0; i < npages; i++)
1275 KDASSERT(pgs[i] == NULL || pgs[i] == PGO_DONTCARE);
1276 #endif
1277 memcpy(opgs, pgs, npages * sizeof(struct vm_pages *));
1278 }
1279 error = genfs_getpages(v);
1280 if (error) {
1281 return (error);
1282 }
1283
1284 /*
1285 * for read faults where the nfs node is not yet marked NMODIFIED,
1286 * set PG_RDONLY on the pages so that we come back here if someone
1287 * tries to modify later via the mapping that will be entered for
1288 * this fault.
1289 */
1290
1291 if (!write && (np->n_flag & NMODIFIED) == 0 && pgs != NULL) {
1292 if (!locked) {
1293 simple_lock(&uobj->vmobjlock);
1294 }
1295 for (i = 0; i < npages; i++) {
1296 pg = pgs[i];
1297 if (pg == NULL || pg == PGO_DONTCARE) {
1298 continue;
1299 }
1300 pg->flags |= PG_RDONLY;
1301 }
1302 if (!locked) {
1303 simple_unlock(&uobj->vmobjlock);
1304 }
1305 }
1306 if (!write) {
1307 return (0);
1308 }
1309
1310 /*
1311 * this is a write fault, update the commit info.
1312 */
1313
1314 origoffset = ap->a_offset;
1315 len = npages << PAGE_SHIFT;
1316
1317 if (v3) {
1318 if (!locked) {
1319 mutex_enter(&np->n_commitlock);
1320 } else {
1321 if (!mutex_tryenter(&np->n_commitlock)) {
1322
1323 /*
1324 * Since PGO_LOCKED is set, we need to unbusy
1325 * all pages fetched by genfs_getpages() above,
1326 * tell the caller that there are no pages
1327 * available and put back original pgs array.
1328 */
1329
1330 uvm_lock_pageq();
1331 uvm_page_unbusy(pgs, npages);
1332 uvm_unlock_pageq();
1333 *ap->a_count = 0;
1334 memcpy(pgs, opgs,
1335 npages * sizeof(struct vm_pages *));
1336 return EBUSY;
1337 }
1338 }
1339 nfs_del_committed_range(vp, origoffset, len);
1340 nfs_del_tobecommitted_range(vp, origoffset, len);
1341 }
1342 np->n_flag |= NMODIFIED;
1343 if (!locked) {
1344 simple_lock(&uobj->vmobjlock);
1345 }
1346 for (i = 0; i < npages; i++) {
1347 pg = pgs[i];
1348 if (pg == NULL || pg == PGO_DONTCARE) {
1349 continue;
1350 }
1351 pg->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1352 }
1353 if (!locked) {
1354 simple_unlock(&uobj->vmobjlock);
1355 }
1356 if (v3) {
1357 mutex_exit(&np->n_commitlock);
1358 }
1359 return (0);
1360 }
1361