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