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