nfs_bio.c revision 1.45.8.2 1 /* $NetBSD: nfs_bio.c,v 1.45.8.2 2000/12/08 09:19:20 bouyer 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 #include "opt_nfs.h"
42 #include "opt_ddb.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
48 #include <sys/proc.h>
49 #include <sys/buf.h>
50 #include <sys/vnode.h>
51 #include <sys/trace.h>
52 #include <sys/mount.h>
53 #include <sys/kernel.h>
54 #include <sys/namei.h>
55 #include <sys/dirent.h>
56 #include <sys/malloc.h>
57
58 #include <uvm/uvm_extern.h>
59 #include <uvm/uvm.h>
60
61 #include <nfs/rpcv2.h>
62 #include <nfs/nfsproto.h>
63 #include <nfs/nfs.h>
64 #include <nfs/nfsmount.h>
65 #include <nfs/nqnfs.h>
66 #include <nfs/nfsnode.h>
67 #include <nfs/nfs_var.h>
68
69 extern int nfs_numasync;
70 extern struct nfsstats nfsstats;
71
72 /*
73 * Vnode op for read using bio
74 * Any similarity to readip() is purely coincidental
75 */
76 int
77 nfs_bioread(vp, uio, ioflag, cred, cflag)
78 struct vnode *vp;
79 struct uio *uio;
80 int ioflag, cflag;
81 struct ucred *cred;
82 {
83 struct nfsnode *np = VTONFS(vp);
84 int biosize;
85 struct buf *bp = NULL, *rabp;
86 struct vattr vattr;
87 struct proc *p;
88 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
89 struct nfsdircache *ndp = NULL, *nndp = NULL;
90 caddr_t baddr, ep, edp;
91 int got_buf = 0, error = 0, n = 0, on = 0, en, enn;
92 int enough = 0;
93 struct dirent *dp, *pdp;
94 off_t curoff = 0;
95
96 #ifdef DIAGNOSTIC
97 if (uio->uio_rw != UIO_READ)
98 panic("nfs_read mode");
99 #endif
100 if (uio->uio_resid == 0)
101 return (0);
102 if (vp->v_type != VDIR && uio->uio_offset < 0)
103 return (EINVAL);
104 p = uio->uio_procp;
105 #ifndef NFS_V2_ONLY
106 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
107 !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
108 (void)nfs_fsinfo(nmp, vp, cred, p);
109 #endif
110 if (vp->v_type != VDIR &&
111 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
112 return (EFBIG);
113 biosize = nmp->nm_rsize;
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 * For nqnfs, full cache consistency is maintained within the loop.
120 * For nfs:
121 * If the file's modify time on the server has changed since the
122 * last read rpc or you have written to the file,
123 * you may have lost data cache consistency with the
124 * server, so flush all of the file's data out of the cache.
125 * Then force a getattr rpc to ensure that you have up to date
126 * attributes.
127 * NB: This implies that cache data can be read when up to
128 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
129 * attributes this could be forced by setting n_attrstamp to 0 before
130 * the VOP_GETATTR() call.
131 */
132
133 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0 && vp->v_type != VLNK) {
134 if (np->n_flag & NMODIFIED) {
135 if (vp->v_type != VREG) {
136 if (vp->v_type != VDIR)
137 panic("nfs: bioread, not dir");
138 nfs_invaldircache(vp, 0);
139 np->n_direofoffset = 0;
140 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
141 if (error)
142 return (error);
143 }
144 np->n_attrstamp = 0;
145 error = VOP_GETATTR(vp, &vattr, cred, p);
146 if (error)
147 return (error);
148 np->n_mtime = vattr.va_mtime.tv_sec;
149 } else {
150 error = VOP_GETATTR(vp, &vattr, cred, p);
151 if (error)
152 return (error);
153 if (np->n_mtime != vattr.va_mtime.tv_sec) {
154 if (vp->v_type == VDIR) {
155 nfs_invaldircache(vp, 0);
156 np->n_direofoffset = 0;
157 }
158 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
159 if (error)
160 return (error);
161 np->n_mtime = vattr.va_mtime.tv_sec;
162 }
163 }
164 }
165
166 /*
167 * update the cached read creds for this node.
168 */
169
170 if (np->n_rcred) {
171 crfree(np->n_rcred);
172 }
173 np->n_rcred = cred;
174 crhold(cred);
175
176 do {
177 #ifndef NFS_V2_ONLY
178 /*
179 * Get a valid lease. If cached data is stale, flush it.
180 */
181 if (nmp->nm_flag & NFSMNT_NQNFS) {
182 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
183 do {
184 error = nqnfs_getlease(vp, ND_READ, cred, p);
185 } while (error == NQNFS_EXPIRED);
186 if (error)
187 return (error);
188 if (np->n_lrev != np->n_brev ||
189 (np->n_flag & NQNFSNONCACHE) ||
190 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
191 if (vp->v_type == VDIR) {
192 nfs_invaldircache(vp, 0);
193 np->n_direofoffset = 0;
194 }
195 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
196 if (error)
197 return (error);
198 np->n_brev = np->n_lrev;
199 }
200 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
201 nfs_invaldircache(vp, 0);
202 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
203 np->n_direofoffset = 0;
204 if (error)
205 return (error);
206 }
207 }
208 #endif
209 /*
210 * Don't cache symlinks.
211 */
212 if (np->n_flag & NQNFSNONCACHE
213 || ((vp->v_flag & VROOT) && vp->v_type == VLNK)) {
214 switch (vp->v_type) {
215 case VREG:
216 return (nfs_readrpc(vp, uio));
217 case VLNK:
218 return (nfs_readlinkrpc(vp, uio, cred));
219 case VDIR:
220 break;
221 default:
222 printf(" NQNFSNONCACHE: type %x unexpected\n",
223 vp->v_type);
224 };
225 }
226 baddr = (caddr_t)0;
227 switch (vp->v_type) {
228 case VREG:
229 nfsstats.biocache_reads++;
230
231 error = 0;
232 while (uio->uio_resid > 0) {
233 void *win;
234 vsize_t bytelen = min(np->n_size - uio->uio_offset,
235 uio->uio_resid);
236
237 if (bytelen == 0)
238 break;
239 win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset,
240 &bytelen, UBC_READ);
241 error = uiomove(win, bytelen, uio);
242 ubc_release(win, 0);
243 if (error) {
244 break;
245 }
246 }
247 n = 0;
248 break;
249
250 case VLNK:
251 nfsstats.biocache_readlinks++;
252 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
253 if (!bp)
254 return (EINTR);
255 if ((bp->b_flags & B_DONE) == 0) {
256 bp->b_flags |= B_READ;
257 error = nfs_doio(bp, p);
258 if (error) {
259 brelse(bp);
260 return (error);
261 }
262 }
263 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
264 got_buf = 1;
265 on = 0;
266 break;
267 case VDIR:
268 diragain:
269 nfsstats.biocache_readdirs++;
270 ndp = nfs_searchdircache(vp, uio->uio_offset,
271 (nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
272 if (!ndp) {
273 /*
274 * We've been handed a cookie that is not
275 * in the cache. If we're not translating
276 * 32 <-> 64, it may be a value that was
277 * flushed out of the cache because it grew
278 * too big. Let the server judge if it's
279 * valid or not. In the translation case,
280 * we have no way of validating this value,
281 * so punt.
282 */
283 if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
284 return (EINVAL);
285 ndp = nfs_enterdircache(vp, uio->uio_offset,
286 uio->uio_offset, 0, 0);
287 }
288
289 if (uio->uio_offset != 0 &&
290 ndp->dc_cookie == np->n_direofoffset) {
291 nfsstats.direofcache_hits++;
292 return (0);
293 }
294
295 bp = nfs_getcacheblk(vp, ndp->dc_blkno, NFS_DIRBLKSIZ, p);
296 if (!bp)
297 return (EINTR);
298 if ((bp->b_flags & B_DONE) == 0) {
299 bp->b_flags |= B_READ;
300 bp->b_dcookie = ndp->dc_blkcookie;
301 error = nfs_doio(bp, p);
302 if (error) {
303 /*
304 * Yuck! The directory has been modified on the
305 * server. Punt and let the userland code
306 * deal with it.
307 */
308 brelse(bp);
309 if (error == NFSERR_BAD_COOKIE) {
310 nfs_invaldircache(vp, 0);
311 nfs_vinvalbuf(vp, 0, cred, p, 1);
312 error = EINVAL;
313 }
314 return (error);
315 }
316 }
317
318 /*
319 * Just return if we hit EOF right away with this
320 * block. Always check here, because direofoffset
321 * may have been set by an nfsiod since the last
322 * check.
323 */
324 if (np->n_direofoffset != 0 &&
325 ndp->dc_blkcookie == np->n_direofoffset) {
326 brelse(bp);
327 return (0);
328 }
329
330 /*
331 * Find the entry we were looking for in the block.
332 */
333
334 en = ndp->dc_entry;
335
336 pdp = dp = (struct dirent *)bp->b_data;
337 edp = bp->b_data + bp->b_bcount;
338 enn = 0;
339 while (enn < en && (caddr_t)dp < edp) {
340 pdp = dp;
341 dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
342 enn++;
343 }
344
345 /*
346 * If the entry number was bigger than the number of
347 * entries in the block, or the cookie of the previous
348 * entry doesn't match, the directory cache is
349 * stale. Flush it and try again (i.e. go to
350 * the server).
351 */
352 if ((caddr_t)dp >= edp || (caddr_t)dp + dp->d_reclen > edp ||
353 (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
354 #ifdef DEBUG
355 printf("invalid cache: %p %p %p off %lx %lx\n",
356 pdp, dp, edp,
357 (unsigned long)uio->uio_offset,
358 (unsigned long)NFS_GETCOOKIE(pdp));
359 #endif
360 brelse(bp);
361 nfs_invaldircache(vp, 0);
362 nfs_vinvalbuf(vp, 0, cred, p, 0);
363 goto diragain;
364 }
365
366 on = (caddr_t)dp - bp->b_data;
367
368 /*
369 * Cache all entries that may be exported to the
370 * user, as they may be thrown back at us. The
371 * NFSBIO_CACHECOOKIES flag indicates that all
372 * entries are being 'exported', so cache them all.
373 */
374
375 if (en == 0 && pdp == dp) {
376 dp = (struct dirent *)
377 ((caddr_t)dp + dp->d_reclen);
378 enn++;
379 }
380
381 if (uio->uio_resid < (bp->b_bcount - on)) {
382 n = uio->uio_resid;
383 enough = 1;
384 } else
385 n = bp->b_bcount - on;
386
387 ep = bp->b_data + on + n;
388
389 /*
390 * Find last complete entry to copy, caching entries
391 * (if requested) as we go.
392 */
393
394 while ((caddr_t)dp < ep && (caddr_t)dp + dp->d_reclen <= ep) {
395 if (cflag & NFSBIO_CACHECOOKIES) {
396 nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
397 ndp->dc_blkcookie, enn, bp->b_lblkno);
398 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
399 NFS_STASHCOOKIE32(pdp,
400 nndp->dc_cookie32);
401 }
402 }
403 pdp = dp;
404 dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
405 enn++;
406 }
407
408 /*
409 * If the last requested entry was not the last in the
410 * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
411 * cache the cookie of the last requested one, and
412 * set of the offset to it.
413 */
414
415 if ((on + n) < bp->b_bcount) {
416 curoff = NFS_GETCOOKIE(pdp);
417 nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
418 enn, bp->b_lblkno);
419 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
420 NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
421 curoff = nndp->dc_cookie32;
422 }
423 } else
424 curoff = bp->b_dcookie;
425
426 /*
427 * Always cache the entry for the next block,
428 * so that readaheads can use it.
429 */
430 nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
431 if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
432 if (curoff == bp->b_dcookie) {
433 NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
434 curoff = nndp->dc_cookie32;
435 }
436 }
437
438 n = ((caddr_t)pdp + pdp->d_reclen) - (bp->b_data + on);
439
440 /*
441 * If not eof and read aheads are enabled, start one.
442 * (You need the current block first, so that you have the
443 * directory offset cookie of the next block.)
444 */
445 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
446 np->n_direofoffset == 0 && !(np->n_flag & NQNFSNONCACHE)) {
447 rabp = nfs_getcacheblk(vp, nndp->dc_blkno,
448 NFS_DIRBLKSIZ, p);
449 if (rabp) {
450 if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
451 rabp->b_dcookie = nndp->dc_cookie;
452 rabp->b_flags |= (B_READ | B_ASYNC);
453 if (nfs_asyncio(rabp)) {
454 rabp->b_flags |= B_INVAL;
455 brelse(rabp);
456 }
457 } else
458 brelse(rabp);
459 }
460 }
461 got_buf = 1;
462 break;
463 default:
464 printf(" nfsbioread: type %x unexpected\n",vp->v_type);
465 break;
466 }
467
468 if (n > 0) {
469 if (!baddr)
470 baddr = bp->b_data;
471 error = uiomove(baddr + on, (int)n, uio);
472 }
473 switch (vp->v_type) {
474 case VREG:
475 break;
476 case VLNK:
477 n = 0;
478 break;
479 case VDIR:
480 if (np->n_flag & NQNFSNONCACHE)
481 bp->b_flags |= B_INVAL;
482 uio->uio_offset = curoff;
483 if (enough)
484 n = 0;
485 break;
486 default:
487 printf(" nfsbioread: type %x unexpected\n",vp->v_type);
488 }
489 if (got_buf)
490 brelse(bp);
491 } while (error == 0 && uio->uio_resid > 0 && n > 0);
492 return (error);
493 }
494
495 /*
496 * Vnode op for write using bio
497 */
498 int
499 nfs_write(v)
500 void *v;
501 {
502 struct vop_write_args /* {
503 struct vnode *a_vp;
504 struct uio *a_uio;
505 int a_ioflag;
506 struct ucred *a_cred;
507 } */ *ap = v;
508 struct uio *uio = ap->a_uio;
509 struct proc *p = uio->uio_procp;
510 struct vnode *vp = ap->a_vp;
511 struct nfsnode *np = VTONFS(vp);
512 struct ucred *cred = ap->a_cred;
513 int ioflag = ap->a_ioflag;
514 struct vattr vattr;
515 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
516 int error = 0, iomode, must_commit;
517 int rv;
518
519 #ifdef DIAGNOSTIC
520 if (uio->uio_rw != UIO_WRITE)
521 panic("nfs_write mode");
522 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
523 panic("nfs_write proc");
524 #endif
525 if (vp->v_type != VREG)
526 return (EIO);
527 if (np->n_flag & NWRITEERR) {
528 np->n_flag &= ~NWRITEERR;
529 return (np->n_error);
530 }
531 #ifndef NFS_V2_ONLY
532 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
533 !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
534 (void)nfs_fsinfo(nmp, vp, cred, p);
535 #endif
536 if (ioflag & (IO_APPEND | IO_SYNC)) {
537 if (np->n_flag & NMODIFIED) {
538 np->n_attrstamp = 0;
539 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
540 if (error)
541 return (error);
542 }
543 if (ioflag & IO_APPEND) {
544 np->n_attrstamp = 0;
545 error = VOP_GETATTR(vp, &vattr, cred, p);
546 if (error)
547 return (error);
548 uio->uio_offset = np->n_size;
549 }
550 }
551 if (uio->uio_offset < 0)
552 return (EINVAL);
553 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
554 return (EFBIG);
555 if (uio->uio_resid == 0)
556 return (0);
557 /*
558 * Maybe this should be above the vnode op call, but so long as
559 * file servers have no limits, i don't think it matters
560 */
561 if (p && uio->uio_offset + uio->uio_resid >
562 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
563 psignal(p, SIGXFSZ);
564 return (EFBIG);
565 }
566
567 /*
568 * update the cached write creds for this node.
569 */
570
571 if (np->n_wcred) {
572 crfree(np->n_wcred);
573 }
574 np->n_wcred = cred;
575 crhold(cred);
576
577 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
578 iomode = NFSV3WRITE_FILESYNC;
579 error = nfs_writerpc(vp, uio, &iomode, &must_commit);
580 if (must_commit)
581 nfs_clearcommit(vp->v_mount);
582 return (error);
583 }
584
585 do {
586 void *win;
587 voff_t oldoff = uio->uio_offset;
588 vsize_t bytelen = uio->uio_resid;
589
590 #ifndef NFS_V2_ONLY
591 /*
592 * Check for a valid write lease.
593 */
594 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
595 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
596 do {
597 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
598 } while (error == NQNFS_EXPIRED);
599 if (error)
600 return (error);
601 if (np->n_lrev != np->n_brev ||
602 (np->n_flag & NQNFSNONCACHE)) {
603 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
604 if (error)
605 return (error);
606 np->n_brev = np->n_lrev;
607 }
608 }
609 #endif
610 nfsstats.biocache_writes++;
611
612 np->n_flag |= NMODIFIED;
613 if (np->n_size < uio->uio_offset + bytelen) {
614 np->n_size = uio->uio_offset + bytelen;
615 uvm_vnp_setsize(vp, np->n_size);
616 }
617 win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset, &bytelen,
618 UBC_WRITE);
619 error = uiomove(win, bytelen, uio);
620 if (error) {
621 memset((void *)trunc_page((vaddr_t)win), 0,
622 round_page((vaddr_t)win + bytelen) -
623 trunc_page((vaddr_t)win));
624 }
625 ubc_release(win, 0);
626 rv = 1;
627 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
628 simple_lock(&vp->v_uvm.u_obj.vmobjlock);
629 rv = vp->v_uvm.u_obj.pgops->pgo_flush(
630 &vp->v_uvm.u_obj,
631 oldoff & ~(nmp->nm_wsize - 1),
632 uio->uio_offset & ~(nmp->nm_wsize - 1),
633 PGO_CLEANIT|PGO_SYNCIO);
634 simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
635 } else if ((oldoff & ~(nmp->nm_wsize - 1)) !=
636 (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
637 simple_lock(&vp->v_uvm.u_obj.vmobjlock);
638 rv = vp->v_uvm.u_obj.pgops->pgo_flush(
639 &vp->v_uvm.u_obj,
640 oldoff & ~(nmp->nm_wsize - 1),
641 uio->uio_offset & ~(nmp->nm_wsize - 1),
642 PGO_CLEANIT|PGO_WEAK);
643 simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
644 }
645 if (!rv) {
646 error = EIO;
647 break;
648 }
649 } while (uio->uio_resid > 0);
650 return error;
651 }
652
653 /*
654 * Get an nfs cache block.
655 * Allocate a new one if the block isn't currently in the cache
656 * and return the block marked busy. If the calling process is
657 * interrupted by a signal for an interruptible mount point, return
658 * NULL.
659 */
660 struct buf *
661 nfs_getcacheblk(vp, bn, size, p)
662 struct vnode *vp;
663 daddr_t bn;
664 int size;
665 struct proc *p;
666 {
667 struct buf *bp;
668 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
669
670 if (nmp->nm_flag & NFSMNT_INT) {
671 bp = getblk(vp, bn, size, PCATCH, 0);
672 while (bp == NULL) {
673 if (nfs_sigintr(nmp, NULL, p))
674 return (NULL);
675 bp = getblk(vp, bn, size, 0, 2 * hz);
676 }
677 } else
678 bp = getblk(vp, bn, size, 0, 0);
679 return (bp);
680 }
681
682 /*
683 * Flush and invalidate all dirty buffers. If another process is already
684 * doing the flush, just wait for completion.
685 */
686 int
687 nfs_vinvalbuf(vp, flags, cred, p, intrflg)
688 struct vnode *vp;
689 int flags;
690 struct ucred *cred;
691 struct proc *p;
692 int intrflg;
693 {
694 struct nfsnode *np = VTONFS(vp);
695 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
696 int error = 0, slpflag, slptimeo;
697
698 if ((nmp->nm_flag & NFSMNT_INT) == 0)
699 intrflg = 0;
700 if (intrflg) {
701 slpflag = PCATCH;
702 slptimeo = 2 * hz;
703 } else {
704 slpflag = 0;
705 slptimeo = 0;
706 }
707 /*
708 * First wait for any other process doing a flush to complete.
709 */
710 while (np->n_flag & NFLUSHINPROG) {
711 np->n_flag |= NFLUSHWANT;
712 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
713 slptimeo);
714 if (error && intrflg && nfs_sigintr(nmp, NULL, p))
715 return (EINTR);
716 }
717
718 /*
719 * Now, flush as required.
720 */
721 np->n_flag |= NFLUSHINPROG;
722 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
723 while (error) {
724 if (intrflg && nfs_sigintr(nmp, NULL, p)) {
725 np->n_flag &= ~NFLUSHINPROG;
726 if (np->n_flag & NFLUSHWANT) {
727 np->n_flag &= ~NFLUSHWANT;
728 wakeup((caddr_t)&np->n_flag);
729 }
730 return (EINTR);
731 }
732 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
733 }
734 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
735 if (np->n_flag & NFLUSHWANT) {
736 np->n_flag &= ~NFLUSHWANT;
737 wakeup((caddr_t)&np->n_flag);
738 }
739 return (0);
740 }
741
742 /*
743 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
744 * This is mainly to avoid queueing async I/O requests when the nfsiods
745 * are all hung on a dead server.
746 */
747 int
748 nfs_asyncio(bp)
749 struct buf *bp;
750 {
751 int i;
752 struct nfsmount *nmp;
753 int gotiod, slpflag = 0, slptimeo = 0, error;
754
755 if (nfs_numasync == 0)
756 return (EIO);
757
758
759 nmp = VFSTONFS(bp->b_vp->v_mount);
760 again:
761 if (nmp->nm_flag & NFSMNT_INT)
762 slpflag = PCATCH;
763 gotiod = FALSE;
764
765 /*
766 * Find a free iod to process this request.
767 */
768
769 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
770 if (nfs_iodwant[i]) {
771 /*
772 * Found one, so wake it up and tell it which
773 * mount to process.
774 */
775 nfs_iodwant[i] = NULL;
776 nfs_iodmount[i] = nmp;
777 nmp->nm_bufqiods++;
778 wakeup((caddr_t)&nfs_iodwant[i]);
779 gotiod = TRUE;
780 break;
781 }
782 /*
783 * If none are free, we may already have an iod working on this mount
784 * point. If so, it will process our request.
785 */
786 if (!gotiod && nmp->nm_bufqiods > 0)
787 gotiod = TRUE;
788
789 /*
790 * If we have an iod which can process the request, then queue
791 * the buffer.
792 */
793 if (gotiod) {
794 /*
795 * Ensure that the queue never grows too large.
796 */
797 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
798 nmp->nm_bufqwant = TRUE;
799 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
800 "nfsaio", slptimeo);
801 if (error) {
802 if (nfs_sigintr(nmp, NULL, bp->b_proc))
803 return (EINTR);
804 if (slpflag == PCATCH) {
805 slpflag = 0;
806 slptimeo = 2 * hz;
807 }
808 }
809 /*
810 * We might have lost our iod while sleeping,
811 * so check and loop if nescessary.
812 */
813 if (nmp->nm_bufqiods == 0)
814 goto again;
815 }
816 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
817 nmp->nm_bufqlen++;
818 return (0);
819 }
820
821 /*
822 * All the iods are busy on other mounts, so return EIO to
823 * force the caller to process the i/o synchronously.
824 */
825 return (EIO);
826 }
827
828 /*
829 * Do an I/O operation to/from a cache block. This may be called
830 * synchronously or from an nfsiod.
831 */
832 int
833 nfs_doio(bp, p)
834 struct buf *bp;
835 struct proc *p;
836 {
837 struct uio *uiop;
838 struct vnode *vp;
839 struct nfsnode *np;
840 struct nfsmount *nmp;
841 int error = 0, diff, len, iomode, must_commit = 0;
842 struct uio uio;
843 struct iovec io;
844
845 vp = bp->b_vp;
846 np = VTONFS(vp);
847 nmp = VFSTONFS(vp->v_mount);
848 uiop = &uio;
849 uiop->uio_iov = &io;
850 uiop->uio_iovcnt = 1;
851 uiop->uio_segflg = UIO_SYSSPACE;
852 uiop->uio_procp = p;
853
854 /*
855 * Historically, paging was done with physio, but no more...
856 */
857 if (bp->b_flags & B_PHYS) {
858 /*
859 * ...though reading /dev/drum still gets us here.
860 */
861 io.iov_len = uiop->uio_resid = bp->b_bcount;
862 /* mapping was done by vmapbuf() */
863 io.iov_base = bp->b_data;
864 uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
865 if (bp->b_flags & B_READ) {
866 uiop->uio_rw = UIO_READ;
867 nfsstats.read_physios++;
868 error = nfs_readrpc(vp, uiop);
869 } else {
870 iomode = NFSV3WRITE_DATASYNC;
871 uiop->uio_rw = UIO_WRITE;
872 nfsstats.write_physios++;
873 error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
874 }
875 if (error) {
876 bp->b_flags |= B_ERROR;
877 bp->b_error = error;
878 }
879 } else if (bp->b_flags & B_READ) {
880 io.iov_len = uiop->uio_resid = bp->b_bcount;
881 io.iov_base = bp->b_data;
882 uiop->uio_rw = UIO_READ;
883 switch (vp->v_type) {
884 case VREG:
885 uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
886 nfsstats.read_bios++;
887 error = nfs_readrpc(vp, uiop);
888 if (!error && uiop->uio_resid) {
889
890 /*
891 * If len > 0, there is a hole in the file and
892 * no writes after the hole have been pushed to
893 * the server yet.
894 * Just zero fill the rest of the valid area.
895 */
896
897 diff = bp->b_bcount - uiop->uio_resid;
898 len = np->n_size - ((((off_t)bp->b_blkno) << DEV_BSHIFT)
899 + diff);
900 if (len > 0) {
901 len = min(len, uiop->uio_resid);
902 memset((char *)bp->b_data + diff, 0, len);
903 }
904 }
905 if (p && (vp->v_flag & VTEXT) &&
906 (((nmp->nm_flag & NFSMNT_NQNFS) &&
907 NQNFS_CKINVALID(vp, np, ND_READ) &&
908 np->n_lrev != np->n_brev) ||
909 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
910 np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
911 uprintf("Process killed due to "
912 "text file modification\n");
913 psignal(p, SIGKILL);
914 p->p_holdcnt++;
915 }
916 break;
917 case VLNK:
918 uiop->uio_offset = (off_t)0;
919 nfsstats.readlink_bios++;
920 error = nfs_readlinkrpc(vp, uiop, curproc->p_ucred);
921 break;
922 case VDIR:
923 nfsstats.readdir_bios++;
924 uiop->uio_offset = bp->b_dcookie;
925 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
926 error = nfs_readdirplusrpc(vp, uiop, curproc->p_ucred);
927 if (error == NFSERR_NOTSUPP)
928 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
929 }
930 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
931 error = nfs_readdirrpc(vp, uiop, curproc->p_ucred);
932 if (!error) {
933 bp->b_dcookie = uiop->uio_offset;
934 }
935 break;
936 default:
937 printf("nfs_doio: type %x unexpected\n",vp->v_type);
938 break;
939 }
940 if (error) {
941 bp->b_flags |= B_ERROR;
942 bp->b_error = error;
943 }
944 } else {
945 /*
946 * If B_NEEDCOMMIT is set, a commit rpc may do the trick. If not
947 * an actual write will have to be scheduled.
948 */
949
950 io.iov_base = bp->b_data;
951 io.iov_len = uiop->uio_resid = bp->b_bcount;
952 uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
953 uiop->uio_rw = UIO_WRITE;
954 nfsstats.write_bios++;
955 iomode = NFSV3WRITE_UNSTABLE;
956 error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
957 }
958 bp->b_resid = uiop->uio_resid;
959 if (must_commit)
960 nfs_clearcommit(vp->v_mount);
961 biodone(bp);
962 return (error);
963 }
964
965 /*
966 * Vnode op for VM getpages.
967 */
968 int
969 nfs_getpages(v)
970 void *v;
971 {
972 struct vop_getpages_args /* {
973 struct vnode *a_vp;
974 voff_t a_offset;
975 vm_page_t *a_m;
976 int *a_count;
977 int a_centeridx;
978 vm_prot_t a_access_type;
979 int a_advice;
980 int a_flags;
981 } */ *ap = v;
982
983 off_t eof, offset, origoffset, startoffset, endoffset;
984 int s, i, error, npages, orignpages, npgs, ridx, pidx, pcount;
985 vaddr_t kva;
986 struct buf *bp, *mbp;
987 struct vnode *vp = ap->a_vp;
988 struct nfsnode *np = VTONFS(vp);
989 struct uvm_object *uobj = &vp->v_uvm.u_obj;
990 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
991 size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
992 int flags = ap->a_flags;
993 int bsize;
994 struct vm_page *pgs[16]; /* XXXUBC 16 */
995 boolean_t v3 = NFS_ISV3(vp);
996 boolean_t async = (flags & PGO_SYNCIO) == 0;
997 boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
998
999 UVMHIST_FUNC("nfs_getpages"); UVMHIST_CALLED(ubchist);
1000 UVMHIST_LOG(ubchist, "vp %p off 0x%x count %d", vp, (int)ap->a_offset,
1001 *ap->a_count,0);
1002
1003 #ifdef DIAGNOSTIC
1004 if (ap->a_centeridx < 0 || ap->a_centeridx >= *ap->a_count) {
1005 panic("nfs_getpages: centeridx %d out of range",
1006 ap->a_centeridx);
1007 }
1008 #endif
1009
1010 error = 0;
1011 origoffset = ap->a_offset;
1012 eof = vp->v_uvm.u_size;
1013 if (origoffset >= eof) {
1014 if ((flags & PGO_LOCKED) == 0) {
1015 simple_unlock(&uobj->vmobjlock);
1016 }
1017 UVMHIST_LOG(ubchist, "off 0x%x past EOF 0x%x",
1018 (int)origoffset, (int)eof,0,0);
1019 return EINVAL;
1020 }
1021
1022 if (flags & PGO_LOCKED) {
1023 uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
1024 UFP_NOWAIT|UFP_NOALLOC);
1025 return 0;
1026 }
1027
1028 /* vnode is VOP_LOCKed, uobj is locked */
1029
1030 bsize = nmp->nm_rsize;
1031 orignpages = min(*ap->a_count,
1032 round_page(eof - origoffset) >> PAGE_SHIFT);
1033 npages = orignpages;
1034 startoffset = origoffset & ~(bsize - 1);
1035 endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
1036 + bsize - 1) & ~(bsize - 1));
1037 endoffset = min(endoffset, round_page(eof));
1038 ridx = (origoffset - startoffset) >> PAGE_SHIFT;
1039
1040 if (!async && !write) {
1041 int rapages = max(PAGE_SIZE, nmp->nm_rsize) >> PAGE_SHIFT;
1042
1043 (void) VOP_GETPAGES(vp, endoffset, NULL, &rapages, 0,
1044 VM_PROT_READ, 0, 0);
1045 simple_lock(&uobj->vmobjlock);
1046 }
1047
1048 UVMHIST_LOG(ubchist, "npages %d offset 0x%x", npages,
1049 (int)origoffset, 0,0);
1050 memset(pgs, 0, sizeof(pgs));
1051 uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
1052
1053 if (flags & PGO_OVERWRITE) {
1054 UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
1055
1056 /* XXXUBC for now, zero the page if we allocated it */
1057 for (i = 0; i < npages; i++) {
1058 struct vm_page *pg = pgs[ridx + i];
1059
1060 if (pg->flags & PG_FAKE) {
1061 uvm_pagezero(pg);
1062 pg->flags &= ~(PG_FAKE);
1063 }
1064 }
1065 goto out;
1066 }
1067
1068 /*
1069 * if the pages are already resident, just return them.
1070 */
1071
1072 for (i = 0; i < npages; i++) {
1073 struct vm_page *pg = pgs[ridx + i];
1074
1075 if ((pg->flags & PG_FAKE) != 0 ||
1076 ((ap->a_access_type & VM_PROT_WRITE) &&
1077 (pg->flags & PG_RDONLY))) {
1078 break;
1079 }
1080 }
1081 if (i == npages) {
1082 UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
1083 goto out;
1084 }
1085
1086 /*
1087 * the page wasn't resident and we're not overwriting,
1088 * so we're going to have to do some i/o.
1089 * find any additional pages needed to cover the expanded range.
1090 */
1091
1092 if (startoffset != origoffset ||
1093 startoffset + (npages << PAGE_SHIFT) != endoffset) {
1094 UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
1095 (int)startoffset, (int)endoffset, 0,0);
1096 npages = (endoffset - startoffset) >> PAGE_SHIFT;
1097 KASSERT(npages != 0);
1098 npgs = npages;
1099 uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
1100 }
1101 simple_unlock(&uobj->vmobjlock);
1102
1103 /*
1104 * update the cached read creds for this node.
1105 */
1106
1107 if (np->n_rcred) {
1108 crfree(np->n_rcred);
1109 }
1110 np->n_rcred = curproc->p_ucred;
1111 crhold(np->n_rcred);
1112
1113 /*
1114 * read the desired page(s).
1115 */
1116
1117 totalbytes = npages << PAGE_SHIFT;
1118 bytes = min(totalbytes, vp->v_uvm.u_size - startoffset);
1119 tailbytes = totalbytes - bytes;
1120 skipbytes = 0;
1121
1122 kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WAITOK |
1123 UVMPAGER_MAPIN_READ);
1124
1125 s = splbio();
1126 mbp = pool_get(&bufpool, PR_WAITOK);
1127 splx(s);
1128 mbp->b_bufsize = totalbytes;
1129 mbp->b_data = (void *)kva;
1130 mbp->b_resid = mbp->b_bcount = bytes;
1131 mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL|B_ASYNC : 0);
1132 mbp->b_iodone = uvm_aio_biodone;
1133 mbp->b_vp = vp;
1134 LIST_INIT(&mbp->b_dep);
1135
1136 /*
1137 * if EOF is in the middle of the last page, zero the part past EOF.
1138 */
1139
1140 if (tailbytes > 0) {
1141 memset((char *)kva + bytes, 0, tailbytes);
1142 }
1143
1144 /*
1145 * now loop over the pages, reading as needed.
1146 */
1147
1148 bp = NULL;
1149 for (offset = startoffset;
1150 bytes > 0;
1151 offset += iobytes, bytes -= iobytes) {
1152
1153 /*
1154 * skip pages which don't need to be read.
1155 */
1156
1157 pidx = (offset - startoffset) >> PAGE_SHIFT;
1158 UVMHIST_LOG(ubchist, "pidx %d offset 0x%x startoffset 0x%x",
1159 pidx, (int)offset, (int)startoffset,0);
1160 while ((pgs[pidx]->flags & PG_FAKE) == 0) {
1161 size_t b;
1162
1163 #ifdef DEBUG
1164 if (offset & (PAGE_SIZE - 1)) {
1165 panic("nfs_getpages: skipping from middle "
1166 "of page");
1167 }
1168 #endif
1169
1170 b = min(PAGE_SIZE, bytes);
1171 offset += b;
1172 bytes -= b;
1173 skipbytes += b;
1174 pidx++;
1175 UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
1176 (int)offset, 0,0,0);
1177 if (bytes == 0) {
1178 goto loopdone;
1179 }
1180 }
1181
1182 /*
1183 * see how many pages can be read with this i/o.
1184 * reduce the i/o size if necessary.
1185 */
1186
1187 iobytes = bytes;
1188 if (offset + iobytes > round_page(offset)) {
1189 pcount = 1;
1190 while (pidx + pcount < npages &&
1191 pgs[pidx + pcount]->flags & PG_FAKE) {
1192 pcount++;
1193 }
1194 iobytes = min(iobytes, (pcount << PAGE_SHIFT) -
1195 (offset - trunc_page(offset)));
1196 }
1197 iobytes = min(iobytes, nmp->nm_rsize);
1198
1199 /*
1200 * allocate a sub-buf for this piece of the i/o
1201 * (or just use mbp if there's only 1 piece),
1202 * and start it going.
1203 */
1204
1205 if (offset == startoffset && iobytes == bytes) {
1206 bp = mbp;
1207 } else {
1208 s = splbio();
1209 bp = pool_get(&bufpool, PR_WAITOK);
1210 splx(s);
1211 bp->b_data = (char *)kva + offset - startoffset;
1212 bp->b_resid = bp->b_bcount = iobytes;
1213 bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC;
1214 bp->b_iodone = uvm_aio_biodone1;
1215 bp->b_vp = vp;
1216 LIST_INIT(&bp->b_dep);
1217 }
1218 bp->b_private = mbp;
1219 bp->b_lblkno = bp->b_blkno = offset >> DEV_BSHIFT;
1220
1221 UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
1222 bp, offset, iobytes, bp->b_blkno);
1223
1224 VOP_STRATEGY(bp);
1225 }
1226
1227 loopdone:
1228 if (skipbytes) {
1229 s = splbio();
1230 mbp->b_resid -= skipbytes;
1231 if (mbp->b_resid == 0) {
1232 biodone(mbp);
1233 }
1234 splx(s);
1235 }
1236 if (async) {
1237 UVMHIST_LOG(ubchist, "returning PEND",0,0,0,0);
1238 return EINPROGRESS;
1239 }
1240 if (bp != NULL) {
1241 error = biowait(mbp);
1242 }
1243 s = splbio();
1244 pool_put(&bufpool, mbp);
1245 splx(s);
1246 uvm_pagermapout(kva, npages);
1247
1248 if (write && v3) {
1249 lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1250 nfs_del_committed_range(vp, origoffset, npages);
1251 nfs_del_tobecommitted_range(vp, origoffset, npages);
1252 for (i = 0; i < npages; i++) {
1253 if (pgs[i] == NULL) {
1254 continue;
1255 }
1256 pgs[i]->flags &= ~(PG_NEEDCOMMIT|PG_RDONLY);
1257 }
1258 lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1259 }
1260
1261 simple_lock(&uobj->vmobjlock);
1262
1263 out:
1264 uvm_lock_pageq();
1265 if (error) {
1266 for (i = 0; i < npages; i++) {
1267 if (pgs[i] == NULL) {
1268 continue;
1269 }
1270 UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
1271 pgs[i], pgs[i]->flags, 0,0);
1272 if ((pgs[i]->flags & PG_FAKE) == 0) {
1273 continue;
1274 }
1275 if (pgs[i]->flags & PG_WANTED) {
1276 wakeup(pgs[i]);
1277 }
1278 uvm_pagefree(pgs[i]);
1279 }
1280 goto done;
1281 }
1282
1283 UVMHIST_LOG(ubchist, "ridx %d count %d", ridx, npages, 0,0);
1284 for (i = 0; i < npages; i++) {
1285 if (pgs[i] == NULL) {
1286 continue;
1287 }
1288 UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
1289 pgs[i], pgs[i]->flags, 0,0);
1290 if (pgs[i]->flags & PG_FAKE) {
1291 UVMHIST_LOG(ubchist, "unfaking pg %p offset 0x%x",
1292 pgs[i], (int)pgs[i]->offset,0,0);
1293 pgs[i]->flags &= ~(PG_FAKE);
1294 pmap_clear_modify(pgs[i]);
1295 pmap_clear_reference(pgs[i]);
1296 }
1297 if (i < ridx || i >= ridx + orignpages || async) {
1298 UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
1299 pgs[i], (int)pgs[i]->offset,0,0);
1300 KASSERT((pgs[i]->flags & PG_RELEASED) == 0);
1301 if (pgs[i]->flags & PG_WANTED) {
1302 wakeup(pgs[i]);
1303 }
1304 if (pgs[i]->wire_count == 0) {
1305 uvm_pageactivate(pgs[i]);
1306 }
1307 pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
1308 UVM_PAGE_OWN(pgs[i], NULL);
1309 }
1310 }
1311
1312 done:
1313 uvm_unlock_pageq();
1314 simple_unlock(&uobj->vmobjlock);
1315 if (ap->a_m != NULL) {
1316 memcpy(ap->a_m, &pgs[ridx],
1317 *ap->a_count * sizeof(struct vm_page *));
1318 }
1319
1320 UVMHIST_LOG(ubchist, "done -> %d", error, 0,0,0);
1321 return error;
1322 }
1323
1324 /*
1325 * Vnode op for VM putpages.
1326 */
1327 int
1328 nfs_putpages(v)
1329 void *v;
1330 {
1331 struct vop_putpages_args /* {
1332 struct vnode *a_vp;
1333 struct vm_page **a_m;
1334 int a_count;
1335 int a_flags;
1336 int *a_rtvals;
1337 } */ *ap = v;
1338
1339 struct vnode *vp = ap->a_vp;
1340 struct nfsnode *np = VTONFS(vp);
1341 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1342 struct buf *bp, *mbp;
1343 struct vm_page **pgs = ap->a_m;
1344 int flags = ap->a_flags;
1345 int npages = ap->a_count;
1346 int s, error = 0, i;
1347 size_t bytes, iobytes, skipbytes;
1348 vaddr_t kva;
1349 off_t offset, origoffset, commitoff;
1350 uint32_t commitbytes;
1351 boolean_t v3 = NFS_ISV3(vp);
1352 boolean_t async = (flags & PGO_SYNCIO) == 0;
1353 boolean_t weak = (flags & PGO_WEAK) && v3;
1354 UVMHIST_FUNC("nfs_putpages"); UVMHIST_CALLED(ubchist);
1355
1356 UVMHIST_LOG(ubchist, "vp %p pgp %p count %d",
1357 vp, ap->a_m, ap->a_count,0);
1358
1359 simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
1360
1361 origoffset = pgs[0]->offset;
1362 bytes = min(ap->a_count << PAGE_SHIFT, vp->v_uvm.u_size - origoffset);
1363 skipbytes = 0;
1364
1365 /*
1366 * if the range has been committed already, mark the pages thus.
1367 * if the range just needs to be committed, we're done
1368 * if it's a weak putpage, otherwise commit the range.
1369 */
1370
1371 if (v3) {
1372 lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1373 if (nfs_in_committed_range(vp, origoffset, bytes)) {
1374 goto committed;
1375 }
1376 if (nfs_in_tobecommitted_range(vp, origoffset, bytes)) {
1377 if (weak) {
1378 lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1379 return 0;
1380 } else {
1381 commitoff = np->n_pushlo;
1382 commitbytes = (uint32_t)(np->n_pushhi -
1383 np->n_pushlo);
1384 goto commit;
1385 }
1386 }
1387 lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1388 }
1389
1390 /*
1391 * otherwise write or commit all the pages.
1392 */
1393
1394 kva = uvm_pagermapin(pgs, ap->a_count, UVMPAGER_MAPIN_WAITOK|
1395 UVMPAGER_MAPIN_WRITE);
1396
1397 s = splbio();
1398 vp->v_numoutput += 2;
1399 mbp = pool_get(&bufpool, PR_WAITOK);
1400 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
1401 vp, mbp, vp->v_numoutput, bytes);
1402 splx(s);
1403 mbp->b_bufsize = npages << PAGE_SHIFT;
1404 mbp->b_data = (void *)kva;
1405 mbp->b_resid = mbp->b_bcount = bytes;
1406 mbp->b_flags = B_BUSY|B_WRITE|B_AGE |
1407 (async ? B_CALL|B_ASYNC : 0) |
1408 (curproc == uvm.pagedaemon_proc ? B_PDAEMON : 0);
1409 mbp->b_iodone = uvm_aio_aiodone;
1410 mbp->b_vp = vp;
1411 LIST_INIT(&mbp->b_dep);
1412
1413 for (offset = origoffset;
1414 bytes > 0;
1415 offset += iobytes, bytes -= iobytes) {
1416 iobytes = min(nmp->nm_wsize, bytes);
1417
1418 /*
1419 * skip writing any pages which only need a commit.
1420 */
1421
1422 if ((pgs[(offset - origoffset) >> PAGE_SHIFT]->flags &
1423 PG_NEEDCOMMIT) != 0) {
1424 iobytes = PAGE_SIZE;
1425 skipbytes += min(iobytes, vp->v_uvm.u_size - offset);
1426 continue;
1427 }
1428
1429 /* if it's really one i/o, don't make a second buf */
1430 if (offset == origoffset && iobytes == bytes) {
1431 bp = mbp;
1432 } else {
1433 s = splbio();
1434 vp->v_numoutput++;
1435 bp = pool_get(&bufpool, PR_WAITOK);
1436 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1437 vp, bp, vp->v_numoutput, 0);
1438 splx(s);
1439 bp->b_data = (char *)kva + (offset - origoffset);
1440 bp->b_resid = bp->b_bcount = iobytes;
1441 bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
1442 bp->b_iodone = uvm_aio_biodone1;
1443 bp->b_vp = vp;
1444 LIST_INIT(&bp->b_dep);
1445 }
1446 bp->b_private = mbp;
1447 bp->b_lblkno = bp->b_blkno = (daddr_t)(offset >> DEV_BSHIFT);
1448 UVMHIST_LOG(ubchist, "bp %p numout %d",
1449 bp, vp->v_numoutput,0,0);
1450 VOP_STRATEGY(bp);
1451 }
1452 if (skipbytes) {
1453 UVMHIST_LOG(ubchist, "skipbytes %d", bytes, 0,0,0);
1454 s = splbio();
1455 mbp->b_resid -= skipbytes;
1456 if (mbp->b_resid == 0) {
1457 biodone(mbp);
1458 }
1459 splx(s);
1460 }
1461 if (async) {
1462 return EINPROGRESS;
1463 }
1464 error = biowait(mbp);
1465
1466 s = splbio();
1467 vwakeup(mbp);
1468 pool_put(&bufpool, mbp);
1469 splx(s);
1470
1471 uvm_pagermapout(kva, ap->a_count);
1472 if (error || !v3) {
1473 UVMHIST_LOG(ubchist, "returning error %d", error, 0,0,0);
1474 return error;
1475 }
1476
1477 /*
1478 * for a weak put, mark the range as "to be committed"
1479 * and mark the pages read-only so that we will be notified
1480 * to remove the pages from the "to be committed" range
1481 * if they are made dirty again.
1482 * for a strong put, commit the pages and remove them from the
1483 * "to be committed" range. also, mark them as writable
1484 * and not cleanable with just a commit.
1485 */
1486
1487 lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1488 if (weak) {
1489 nfs_add_tobecommitted_range(vp, origoffset,
1490 npages << PAGE_SHIFT);
1491 for (i = 0; i < npages; i++) {
1492 pgs[i]->flags |= PG_NEEDCOMMIT|PG_RDONLY;
1493 }
1494 } else {
1495 commitoff = origoffset;
1496 commitbytes = npages << PAGE_SHIFT;
1497 commit:
1498 error = nfs_commit(vp, commitoff, commitbytes, curproc);
1499 nfs_del_tobecommitted_range(vp, commitoff, commitbytes);
1500 committed:
1501 for (i = 0; i < npages; i++) {
1502 pgs[i]->flags &= ~(PG_NEEDCOMMIT|PG_RDONLY);
1503 }
1504 }
1505 lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1506 return error;
1507 }
1508