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