nfs_bio.c revision 1.45.4.5 1 /* $NetBSD: nfs_bio.c,v 1.45.4.5 1999/08/31 21:03:44 perseant 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 * Check for a valid write lease.
668 */
669 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
670 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
671 do {
672 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
673 } while (error == NQNFS_EXPIRED);
674 if (error)
675 return (error);
676 if (np->n_lrev != np->n_brev ||
677 (np->n_flag & NQNFSNONCACHE)) {
678 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
679 if (error)
680 return (error);
681 np->n_brev = np->n_lrev;
682 }
683 }
684 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
685 iomode = NFSV3WRITE_FILESYNC;
686 error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
687 if (must_commit)
688 nfs_clearcommit(vp->v_mount);
689 return (error);
690 }
691 nfsstats.biocache_writes++;
692
693 #if 1
694 np->n_flag |= NMODIFIED;
695 if (np->n_size < uio->uio_offset + n) {
696 np->n_size = uio->uio_offset + n;
697 uvm_vnp_setsize(vp, np->n_size);
698 }
699
700 /* XXX check dirty region stuff */
701
702 win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset, &bytelen,
703 UBC_WRITE);
704 #ifdef DIAGNOSTIC
705 if (win == NULL) {
706 panic("nfs_bioread: ubc_alloc -> NULL");
707 }
708 #endif
709 error = uiomove(win, bytelen, uio);
710 ubc_release(win, 0);
711
712 /* XXX abstract this somehow */
713 simple_lock(&vp->v_uvm.u_obj.vmobjlock);
714 vp->v_uvm.u_obj.pgops->pgo_flush(&vp->v_uvm.u_obj,
715 trunc_page(oldoff),
716 oldoff + bytelen,
717 PGO_CLEANIT | PGO_SYNCIO);
718 simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
719
720 if (error) {
721 /*
722 * XXX zero out any part of the current window
723 * that we might have failed to copyin.
724 */
725 break;
726 }
727
728 /* XXX set dirty region stuff */
729 /* XXX set page NEEDCOMMIT flag */
730 /* XXX handle IO_SYNC and NQNFSNONCACHE */
731
732 #else
733 lbn = uio->uio_offset / biosize;
734 on = uio->uio_offset & (biosize-1);
735 n = min((unsigned)(biosize - on), uio->uio_resid);
736 bn = lbn * (biosize / DEV_BSIZE);
737 again:
738 bp = nfs_getcacheblk(vp, bn, biosize, p);
739 if (!bp)
740 return (EINTR);
741 if (bp->b_wcred == NOCRED) {
742 crhold(cred);
743 bp->b_wcred = cred;
744 }
745 np->n_flag |= NMODIFIED;
746 if (uio->uio_offset + n > np->n_size) {
747 np->n_size = uio->uio_offset + n;
748 uvm_vnp_setsize(vp, np->n_size);
749 }
750
751 /*
752 * If the new write will leave a contiguous dirty
753 * area, just update the b_dirtyoff and b_dirtyend,
754 * otherwise force a write rpc of the old dirty area.
755 */
756 if (bp->b_dirtyend > 0 &&
757 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
758 bp->b_proc = p;
759 if (VOP_BWRITE(bp) == EINTR)
760 return (EINTR);
761 goto again;
762 }
763
764 /*
765 * Check for valid write lease and get one as required.
766 * In case getblk() and/or bwrite() delayed us.
767 */
768 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
769 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
770 do {
771 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
772 } while (error == NQNFS_EXPIRED);
773 if (error) {
774 brelse(bp);
775 return (error);
776 }
777 if (np->n_lrev != np->n_brev ||
778 (np->n_flag & NQNFSNONCACHE)) {
779 brelse(bp);
780 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
781 if (error)
782 return (error);
783 np->n_brev = np->n_lrev;
784 goto again;
785 }
786 }
787 error = uiomove((char *)bp->b_data + on, n, uio);
788 if (error) {
789 bp->b_flags |= B_ERROR;
790 brelse(bp);
791 return (error);
792 }
793 if (bp->b_dirtyend > 0) {
794 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
795 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
796 } else {
797 bp->b_dirtyoff = on;
798 bp->b_dirtyend = on + n;
799 }
800 if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff ||
801 bp->b_validoff > bp->b_dirtyend) {
802 bp->b_validoff = bp->b_dirtyoff;
803 bp->b_validend = bp->b_dirtyend;
804 } else {
805 bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff);
806 bp->b_validend = max(bp->b_validend, bp->b_dirtyend);
807 }
808
809 /*
810 * Since this block is being modified, it must be written
811 * again and not just committed.
812 */
813 bp->b_flags &= ~B_NEEDCOMMIT;
814
815 /*
816 * If the lease is non-cachable or IO_SYNC do bwrite().
817 */
818 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
819 bp->b_proc = p;
820 error = VOP_BWRITE(bp);
821 if (error)
822 return (error);
823 if (np->n_flag & NQNFSNONCACHE) {
824 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
825 if (error)
826 return (error);
827 }
828 } else if ((n + on) == biosize &&
829 (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
830 bp->b_proc = (struct proc *)0;
831 bp->b_flags |= B_ASYNC;
832 (void)nfs_writebp(bp, 0);
833 } else {
834 bdwrite(bp);
835 }
836 #endif
837 } while (uio->uio_resid > 0 && n > 0);
838 return (0);
839 }
840
841 /*
842 * Get an nfs cache block.
843 * Allocate a new one if the block isn't currently in the cache
844 * and return the block marked busy. If the calling process is
845 * interrupted by a signal for an interruptible mount point, return
846 * NULL.
847 */
848 struct buf *
849 nfs_getcacheblk(vp, bn, size, p)
850 struct vnode *vp;
851 daddr_t bn;
852 int size;
853 struct proc *p;
854 {
855 register struct buf *bp;
856 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
857
858 if (nmp->nm_flag & NFSMNT_INT) {
859 bp = getblk(vp, bn, size, PCATCH, 0);
860 while (bp == (struct buf *)0) {
861 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
862 return ((struct buf *)0);
863 bp = getblk(vp, bn, size, 0, 2 * hz);
864 }
865 } else
866 bp = getblk(vp, bn, size, 0, 0);
867 return (bp);
868 }
869
870 /*
871 * Flush and invalidate all dirty buffers. If another process is already
872 * doing the flush, just wait for completion.
873 */
874 int
875 nfs_vinvalbuf(vp, flags, cred, p, intrflg)
876 struct vnode *vp;
877 int flags;
878 struct ucred *cred;
879 struct proc *p;
880 int intrflg;
881 {
882 register struct nfsnode *np = VTONFS(vp);
883 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
884 int error = 0, slpflag, slptimeo;
885
886 if ((nmp->nm_flag & NFSMNT_INT) == 0)
887 intrflg = 0;
888 if (intrflg) {
889 slpflag = PCATCH;
890 slptimeo = 2 * hz;
891 } else {
892 slpflag = 0;
893 slptimeo = 0;
894 }
895 /*
896 * First wait for any other process doing a flush to complete.
897 */
898 while (np->n_flag & NFLUSHINPROG) {
899 np->n_flag |= NFLUSHWANT;
900 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
901 slptimeo);
902 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
903 return (EINTR);
904 }
905
906 /*
907 * Now, flush as required.
908 */
909 np->n_flag |= NFLUSHINPROG;
910 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
911 while (error) {
912 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
913 np->n_flag &= ~NFLUSHINPROG;
914 if (np->n_flag & NFLUSHWANT) {
915 np->n_flag &= ~NFLUSHWANT;
916 wakeup((caddr_t)&np->n_flag);
917 }
918 return (EINTR);
919 }
920 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
921 }
922 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
923 if (np->n_flag & NFLUSHWANT) {
924 np->n_flag &= ~NFLUSHWANT;
925 wakeup((caddr_t)&np->n_flag);
926 }
927 return (0);
928 }
929
930 /*
931 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
932 * This is mainly to avoid queueing async I/O requests when the nfsiods
933 * are all hung on a dead server.
934 */
935 int
936 nfs_asyncio(bp, cred)
937 register struct buf *bp;
938 struct ucred *cred;
939 {
940 register int i;
941 register struct nfsmount *nmp;
942 int gotiod, slpflag = 0, slptimeo = 0, error;
943
944 if (nfs_numasync == 0)
945 return (EIO);
946
947
948 nmp = VFSTONFS(bp->b_vp->v_mount);
949 again:
950 if (nmp->nm_flag & NFSMNT_INT)
951 slpflag = PCATCH;
952 gotiod = FALSE;
953
954 /*
955 * Find a free iod to process this request.
956 */
957
958 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
959 if (nfs_iodwant[i]) {
960 /*
961 * Found one, so wake it up and tell it which
962 * mount to process.
963 */
964 nfs_iodwant[i] = (struct proc *)0;
965 nfs_iodmount[i] = nmp;
966 nmp->nm_bufqiods++;
967 wakeup((caddr_t)&nfs_iodwant[i]);
968 gotiod = TRUE;
969 break;
970 }
971 /*
972 * If none are free, we may already have an iod working on this mount
973 * point. If so, it will process our request.
974 */
975 if (!gotiod && nmp->nm_bufqiods > 0)
976 gotiod = TRUE;
977
978 /*
979 * If we have an iod which can process the request, then queue
980 * the buffer.
981 */
982 if (gotiod) {
983 /*
984 * Ensure that the queue never grows too large.
985 */
986 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
987 nmp->nm_bufqwant = TRUE;
988 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
989 "nfsaio", slptimeo);
990 if (error) {
991 if (nfs_sigintr(nmp, NULL, bp->b_proc))
992 return (EINTR);
993 if (slpflag == PCATCH) {
994 slpflag = 0;
995 slptimeo = 2 * hz;
996 }
997 }
998 /*
999 * We might have lost our iod while sleeping,
1000 * so check and loop if nescessary.
1001 */
1002 if (nmp->nm_bufqiods == 0)
1003 goto again;
1004 }
1005
1006 if (bp->b_flags & B_READ) {
1007 if (bp->b_rcred == NOCRED && cred != NOCRED) {
1008 crhold(cred);
1009 bp->b_rcred = cred;
1010 }
1011 } else {
1012 bp->b_flags |= B_WRITEINPROG;
1013 if (bp->b_wcred == NOCRED && cred != NOCRED) {
1014 crhold(cred);
1015 bp->b_wcred = cred;
1016 }
1017 }
1018
1019 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1020 nmp->nm_bufqlen++;
1021 return (0);
1022 }
1023
1024 /*
1025 * All the iods are busy on other mounts, so return EIO to
1026 * force the caller to process the i/o synchronously.
1027 */
1028 return (EIO);
1029 }
1030
1031 /*
1032 * Do an I/O operation to/from a cache block. This may be called
1033 * synchronously or from an nfsiod.
1034 */
1035 int
1036 nfs_doio(bp, cr, p)
1037 register struct buf *bp;
1038 struct ucred *cr;
1039 struct proc *p;
1040 {
1041 register struct uio *uiop;
1042 register struct vnode *vp;
1043 struct nfsnode *np;
1044 struct nfsmount *nmp;
1045 int error = 0, diff, len, iomode, must_commit = 0;
1046 struct uio uio;
1047 struct iovec io;
1048
1049 vp = bp->b_vp;
1050 np = VTONFS(vp);
1051 nmp = VFSTONFS(vp->v_mount);
1052 uiop = &uio;
1053 uiop->uio_iov = &io;
1054 uiop->uio_iovcnt = 1;
1055 uiop->uio_segflg = UIO_SYSSPACE;
1056 uiop->uio_procp = p;
1057
1058 /*
1059 * Historically, paging was done with physio, but no more...
1060 */
1061 if (bp->b_flags & B_PHYS) {
1062 /*
1063 * ...though reading /dev/drum still gets us here.
1064 */
1065 io.iov_len = uiop->uio_resid = bp->b_bcount;
1066 /* mapping was done by vmapbuf() */
1067 io.iov_base = bp->b_data;
1068 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1069 if (bp->b_flags & B_READ) {
1070 uiop->uio_rw = UIO_READ;
1071 nfsstats.read_physios++;
1072 error = nfs_readrpc(vp, uiop, cr);
1073 } else {
1074 iomode = NFSV3WRITE_DATASYNC;
1075 uiop->uio_rw = UIO_WRITE;
1076 nfsstats.write_physios++;
1077 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
1078 }
1079 if (error) {
1080 bp->b_flags |= B_ERROR;
1081 bp->b_error = error;
1082 }
1083 } else if (bp->b_flags & B_READ) {
1084 io.iov_len = uiop->uio_resid = bp->b_bcount;
1085 io.iov_base = bp->b_data;
1086 uiop->uio_rw = UIO_READ;
1087 switch (vp->v_type) {
1088 case VREG:
1089 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1090 nfsstats.read_bios++;
1091 error = nfs_readrpc(vp, uiop, cr);
1092 if (!error) {
1093 bp->b_validoff = 0;
1094 if (uiop->uio_resid) {
1095 /*
1096 * If len > 0, there is a hole in the file and
1097 * no writes after the hole have been pushed to
1098 * the server yet.
1099 * Just zero fill the rest of the valid area.
1100 */
1101 diff = bp->b_bcount - uiop->uio_resid;
1102 len = np->n_size - (((u_quad_t)bp->b_blkno) * DEV_BSIZE
1103 + diff);
1104 if (len > 0) {
1105 len = min(len, uiop->uio_resid);
1106 memset((char *)bp->b_data + diff, 0, len);
1107 bp->b_validend = diff + len;
1108 } else
1109 bp->b_validend = diff;
1110 } else
1111 bp->b_validend = bp->b_bcount;
1112 }
1113 if (p && (vp->v_flag & VTEXT) &&
1114 (((nmp->nm_flag & NFSMNT_NQNFS) &&
1115 NQNFS_CKINVALID(vp, np, ND_READ) &&
1116 np->n_lrev != np->n_brev) ||
1117 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
1118 np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
1119 uprintf("Process killed due to text file modification\n");
1120 psignal(p, SIGKILL);
1121 p->p_holdcnt++;
1122 }
1123 break;
1124 case VLNK:
1125 uiop->uio_offset = (off_t)0;
1126 nfsstats.readlink_bios++;
1127 error = nfs_readlinkrpc(vp, uiop, cr);
1128 break;
1129 case VDIR:
1130 nfsstats.readdir_bios++;
1131 uiop->uio_offset = bp->b_dcookie;
1132 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1133 error = nfs_readdirplusrpc(vp, uiop, cr);
1134 if (error == NFSERR_NOTSUPP)
1135 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1136 }
1137 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1138 error = nfs_readdirrpc(vp, uiop, cr);
1139 if (!error) {
1140 bp->b_dcookie = uiop->uio_offset;
1141 bp->b_validoff = 0;
1142 bp->b_validend = bp->b_bcount - uiop->uio_resid;
1143 }
1144 break;
1145 default:
1146 printf("nfs_doio: type %x unexpected\n",vp->v_type);
1147 break;
1148 };
1149 if (error) {
1150 bp->b_flags |= B_ERROR;
1151 bp->b_error = error;
1152 }
1153 } else {
1154 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1155 - bp->b_dirtyoff;
1156 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE
1157 + bp->b_dirtyoff;
1158 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1159 uiop->uio_rw = UIO_WRITE;
1160 nfsstats.write_bios++;
1161 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE)) == B_ASYNC)
1162 iomode = NFSV3WRITE_UNSTABLE;
1163 else
1164 iomode = NFSV3WRITE_FILESYNC;
1165 bp->b_flags |= B_WRITEINPROG;
1166 #ifdef fvdl_debug
1167 printf("nfs_doio(%x): bp %x doff %d dend %d\n",
1168 vp, bp, bp->b_dirtyoff, bp->b_dirtyend);
1169 #endif
1170 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
1171 if (!error && iomode == NFSV3WRITE_UNSTABLE)
1172 bp->b_flags |= B_NEEDCOMMIT;
1173 else
1174 bp->b_flags &= ~B_NEEDCOMMIT;
1175 bp->b_flags &= ~B_WRITEINPROG;
1176
1177 /*
1178 * For an interrupted write, the buffer is still valid and the
1179 * write hasn't been pushed to the server yet, so we can't set
1180 * B_ERROR and report the interruption by setting B_EINTR. For
1181 * the B_ASYNC case, B_EINTR is not relevant, so the rpc attempt
1182 * is essentially a noop.
1183 * For the case of a V3 write rpc not being committed to stable
1184 * storage, the block is still dirty and requires either a commit
1185 * rpc or another write rpc with iomode == NFSV3WRITE_FILESYNC
1186 * before the block is reused. This is indicated by setting the
1187 * B_DELWRI and B_NEEDCOMMIT flags.
1188 */
1189 if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1190 bp->b_flags |= B_DELWRI;
1191
1192 /*
1193 * Since for the B_ASYNC case, nfs_bwrite() has reassigned the
1194 * buffer to the clean list, we have to reassign it back to the
1195 * dirty one. Ugh.
1196 */
1197 if (bp->b_flags & B_ASYNC)
1198 reassignbuf(bp, vp);
1199 else if (error)
1200 bp->b_flags |= B_EINTR;
1201 } else {
1202 if (error) {
1203 bp->b_flags |= B_ERROR;
1204 bp->b_error = np->n_error = error;
1205 np->n_flag |= NWRITEERR;
1206 }
1207 bp->b_dirtyoff = bp->b_dirtyend = 0;
1208 }
1209 }
1210 bp->b_resid = uiop->uio_resid;
1211 if (must_commit)
1212 nfs_clearcommit(vp->v_mount);
1213 biodone(bp);
1214 return (error);
1215 }
1216
1217 /*
1218 * Vnode op for VM getpages.
1219 */
1220 int
1221 nfs_getpages(v)
1222 void *v;
1223 {
1224 struct vop_getpages_args /* {
1225 struct vnode *a_vp;
1226 vaddr_t a_offset;
1227 vm_page_t *a_m;
1228 int *a_count;
1229 int a_centeridx;
1230 vm_prot_t a_access_type;
1231 int a_advice;
1232 int a_flags;
1233 } */ *ap = v;
1234
1235 int i, error, npages;
1236 struct uio uio;
1237 struct iovec iov;
1238 vaddr_t kva;
1239 struct buf tmpbuf, *bp;
1240 struct vnode *vp = ap->a_vp;
1241 struct uvm_object *uobj = &vp->v_uvm.u_obj;
1242 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1243 vaddr_t offset = ap->a_offset;
1244 off_t eof;
1245 vm_page_t pgs[2]; /* XXX tmp hack: 4k page 8k rsize */
1246 int cidx, vidx;
1247
1248 UVMHIST_FUNC("nfs_getpages"); UVMHIST_CALLED(ubchist);
1249 UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)offset, 0,0);
1250
1251 #ifdef DIAGNOSTIC
1252 if (ap->a_centeridx < 0 || ap->a_centeridx >= *ap->a_count) {
1253 panic("nfs_getpages: centeridx %d out of range",
1254 ap->a_centeridx);
1255 }
1256 #endif
1257
1258 eof = vp->v_uvm.u_size;
1259 if (offset >= eof) {
1260 if ((ap->a_flags & PGO_LOCKED) == 0) {
1261 simple_unlock(&uobj->vmobjlock);
1262 }
1263 UVMHIST_LOG(ubchist, "off 0x%x past EOF 0x%x",
1264 offset, eof,0,0);
1265 return EINVAL;
1266 }
1267
1268
1269 if (ap->a_flags & PGO_LOCKED) {
1270 uvn_findpages(uobj, offset, ap->a_count, ap->a_m,
1271 UFP_NOWAIT|UFP_NOALLOC);
1272
1273 /* XXX PGO_ALLPAGES? */
1274 return 0;
1275 }
1276
1277 /* vnode is VOP_LOCKed, uobj is locked */
1278
1279 /*
1280 * first see if center page already exists.
1281 * if it does, return the page.
1282 * XXX
1283 * this is needed because ubc_fault() doesn't
1284 * do a PGO_LOCKED call first.
1285 * XXX change ubc_fault() to do the PGO_LOCKED call.
1286 */
1287
1288 npages = 1;
1289 uvn_findpages(uobj, offset + (ap->a_centeridx << PAGE_SHIFT),
1290 &npages, &ap->a_m[ap->a_centeridx], UFP_NOALLOC);
1291 if (npages == 1) {
1292 simple_unlock(&uobj->vmobjlock);
1293 return 0;
1294 }
1295
1296 npages = nmp->nm_rsize >> PAGE_SHIFT;
1297 offset &= ~(nmp->nm_rsize - 1);
1298 cidx = ap->a_centeridx + ((ap->a_offset - offset) >> PAGE_SHIFT);
1299 UVMHIST_LOG(ubchist, "npages %d offset 0x%lx cidx %d",
1300 npages, offset, cidx,0);
1301 memset(pgs, 0, sizeof(pgs));
1302 uvn_findpages(uobj, offset, &npages, pgs, UFP_NOCACHE);
1303 vidx = (npages == 2) ? 0 : cidx;
1304
1305 simple_unlock(&uobj->vmobjlock);
1306
1307 #ifdef DIAGNOSTIC
1308 if (npages == 0) {
1309 panic("nfs_getpages: nothing to read, vp %p", vp);
1310 }
1311 #endif
1312
1313 /*
1314 * read at last part of the page.
1315 */
1316
1317 kva = uvm_pagermapin(&pgs[vidx], npages, M_WAITOK);
1318
1319 uio.uio_iov = &iov;
1320 iov.iov_len = 0;
1321
1322 bp = &tmpbuf;
1323 bzero(bp, sizeof *bp);
1324
1325 bp->b_bufsize = npages << PAGE_SHIFT;
1326 bp->b_bcount = min(bp->b_bufsize, vp->v_uvm.u_size -
1327 (offset + (vidx << PAGE_SHIFT)));
1328 bp->b_data = (void *)kva;
1329 bp->b_blkno = bp->b_lblkno =
1330 (offset + (vidx << PAGE_SHIFT)) >> DEV_BSHIFT;
1331 bp->b_vp = vp;
1332 bp->b_rcred = 0;
1333 bp->b_flags = B_BUSY|B_READ;
1334
1335 UVMHIST_LOG(ubchist, "reading blkno 0x%x bcount 0x%x",
1336 bp->b_blkno, bp->b_bcount,0,0);
1337 error = nfs_doio(bp, curproc->p_ucred, curproc);
1338
1339 uvm_pagermapout(kva, npages);
1340
1341 simple_lock(&uobj->vmobjlock);
1342 uvm_lock_pageq();
1343 if (error) {
1344 for (i = vidx; i < vidx + npages; i++) {
1345 if (pgs[i]->flags & PG_WANTED) {
1346 wakeup(pgs[i]);
1347 }
1348 pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
1349 UVM_PAGE_OWN(pgs[i], NULL);
1350 uvm_pagefree(pgs[i]);
1351 }
1352 } else {
1353 for (i = vidx; i < vidx + npages; i++) {
1354 if (i != cidx) {
1355 if (pgs[i]->flags & PG_WANTED) {
1356 wakeup(pgs[i]);
1357 }
1358 pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
1359 UVM_PAGE_OWN(pgs[i], NULL);
1360
1361 }
1362 pgs[i]->flags &= ~PG_FAKE;
1363 pmap_clear_modify(PMAP_PGARG(pgs[i]));
1364 uvm_pageactivate(pgs[i]);
1365 }
1366 }
1367 uvm_unlock_pageq();
1368 simple_unlock(&uobj->vmobjlock);
1369
1370 ap->a_m[ap->a_centeridx] = pgs[cidx];
1371 UVMHIST_LOG(ubchist, "a_m[%d] = pgs[%d] = %p", ap->a_centeridx,
1372 cidx, pgs[cidx],0);
1373 #ifdef DIAGNOSTIC
1374 if (ap->a_m[ap->a_centeridx] == NULL) {
1375 panic("nfs_getpages: returning null page?");
1376 }
1377 #endif
1378 return error;
1379 }
1380
1381 /*
1382 * Vnode op for VM putpages.
1383 */
1384 int
1385 nfs_putpages(v)
1386 void *v;
1387 {
1388 struct vop_putpages_args /* {
1389 struct vnode *a_vp;
1390 vm_page_t *a_m;
1391 int a_count;
1392 int a_flags;
1393 int *a_rtvals;
1394 } */ *ap = v;
1395
1396 struct vnode *vp = ap->a_vp;
1397 int mode, commit;
1398 int error;
1399 struct uio uio;
1400 struct iovec iov;
1401 vm_page_t m;
1402 vaddr_t kva;
1403 int iosize;
1404
1405 /* XXX for now, just do one page at a time */
1406 if (ap->a_count != 1) {
1407 panic("nfs_putpages: one at a time, please\n");
1408 }
1409
1410 m = ap->a_m[0];
1411 kva = uvm_pagermapin(ap->a_m, ap->a_count, M_WAITOK);
1412
1413 iosize = min(PAGE_SIZE, vp->v_uvm.u_size - m->offset);
1414
1415 iov.iov_base = (caddr_t)kva;
1416 iov.iov_len = iosize;
1417 uio.uio_iov = &iov;
1418 uio.uio_iovcnt = 1;
1419 uio.uio_offset = m->offset;
1420 uio.uio_resid = iosize;
1421 uio.uio_segflg = UIO_SYSSPACE;
1422 uio.uio_rw = UIO_WRITE;
1423 uio.uio_procp = NULL;
1424
1425 /* XXX */
1426 mode = NFSV3WRITE_FILESYNC;
1427
1428 error = nfs_writerpc(vp, &uio, curproc->p_ucred, &mode, &commit);
1429
1430 uvm_pagermapout(kva, ap->a_count);
1431
1432 return error;
1433 }
1434