Deleted Added
full compact
nfs_bio.c (46349) nfs_bio.c (46580)
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
37 * $Id: nfs_bio.c,v 1.69 1999/04/06 03:07:54 peter Exp $
37 * $Id: nfs_bio.c,v 1.70 1999/05/02 23:56:24 alc Exp $
38 */
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/mount.h>
49#include <sys/kernel.h>
50
51#include <vm/vm.h>
52#include <vm/vm_extern.h>
53#include <vm/vm_prot.h>
54#include <vm/vm_page.h>
55#include <vm/vm_object.h>
56#include <vm/vm_pager.h>
57#include <vm/vnode_pager.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
66static struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size,
67 struct proc *p));
68
69extern int nfs_numasync;
70extern int nfs_pbuf_freecnt;
71extern struct nfsstats nfsstats;
72
73/*
74 * Vnode op for VM getpages.
75 */
76int
77nfs_getpages(ap)
78 struct vop_getpages_args /* {
79 struct vnode *a_vp;
80 vm_page_t *a_m;
81 int a_count;
82 int a_reqpage;
83 vm_ooffset_t a_offset;
84 } */ *ap;
85{
86 int i, error, nextoff, size, toff, count, npages;
87 struct uio uio;
88 struct iovec iov;
89 vm_offset_t kva;
90 struct buf *bp;
91 struct vnode *vp;
92 struct proc *p;
93 struct ucred *cred;
94 struct nfsmount *nmp;
95 vm_page_t *pages;
96
97 vp = ap->a_vp;
98 p = curproc; /* XXX */
99 cred = curproc->p_ucred; /* XXX */
100 nmp = VFSTONFS(vp->v_mount);
101 pages = ap->a_m;
102 count = ap->a_count;
103
104 if (vp->v_object == NULL) {
105 printf("nfs_getpages: called with non-merged cache vnode??\n");
106 return VM_PAGER_ERROR;
107 }
108
109 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
110 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
111 (void)nfs_fsinfo(nmp, vp, cred, p);
112
113 npages = btoc(count);
114
115 /*
116 * If the requested page is partially valid, just return it and
117 * allow the pager to zero-out the blanks. Partially valid pages
118 * can only occur at the file EOF.
119 */
120
121 {
122 vm_page_t m = pages[ap->a_reqpage];
123
124 if (m->valid != 0) {
125 /* handled by vm_fault now */
126 /* vm_page_zero_invalid(m, TRUE); */
127 for (i = 0; i < npages; ++i) {
128 if (i != ap->a_reqpage)
129 vnode_pager_freepage(pages[i]);
130 }
131 return(0);
132 }
133 }
134
135 /*
136 * We use only the kva address for the buffer, but this is extremely
137 * convienient and fast.
138 */
139 bp = getpbuf(&nfs_pbuf_freecnt);
140
141 kva = (vm_offset_t) bp->b_data;
142 pmap_qenter(kva, pages, npages);
143
144 iov.iov_base = (caddr_t) kva;
145 iov.iov_len = count;
146 uio.uio_iov = &iov;
147 uio.uio_iovcnt = 1;
148 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
149 uio.uio_resid = count;
150 uio.uio_segflg = UIO_SYSSPACE;
151 uio.uio_rw = UIO_READ;
152 uio.uio_procp = p;
153
154 error = nfs_readrpc(vp, &uio, cred);
155 pmap_qremove(kva, npages);
156
157 relpbuf(bp, &nfs_pbuf_freecnt);
158
159 if (error && (uio.uio_resid == count)) {
160 printf("nfs_getpages: error %d\n", error);
161 for (i = 0; i < npages; ++i) {
162 if (i != ap->a_reqpage)
163 vnode_pager_freepage(pages[i]);
164 }
165 return VM_PAGER_ERROR;
166 }
167
168 /*
169 * Calculate the number of bytes read and validate only that number
170 * of bytes. Note that due to pending writes, size may be 0. This
171 * does not mean that the remaining data is invalid!
172 */
173
174 size = count - uio.uio_resid;
175
176 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
177 vm_page_t m;
178 nextoff = toff + PAGE_SIZE;
179 m = pages[i];
180
181 m->flags &= ~PG_ZERO;
182
183 if (nextoff <= size) {
184 /*
185 * Read operation filled an entire page
186 */
187 m->valid = VM_PAGE_BITS_ALL;
188 m->dirty = 0;
189 } else if (size > toff) {
190 /*
191 * Read operation filled a partial page.
192 */
193 m->valid = 0;
194 vm_page_set_validclean(m, 0, size - toff);
195 /* handled by vm_fault now */
196 /* vm_page_zero_invalid(m, TRUE); */
197 }
198
199 if (i != ap->a_reqpage) {
200 /*
201 * Whether or not to leave the page activated is up in
202 * the air, but we should put the page on a page queue
203 * somewhere (it already is in the object). Result:
204 * It appears that emperical results show that
205 * deactivating pages is best.
206 */
207
208 /*
209 * Just in case someone was asking for this page we
210 * now tell them that it is ok to use.
211 */
212 if (!error) {
213 if (m->flags & PG_WANTED)
214 vm_page_activate(m);
215 else
216 vm_page_deactivate(m);
217 vm_page_wakeup(m);
218 } else {
219 vnode_pager_freepage(m);
220 }
221 }
222 }
223 return 0;
224}
225
226/*
227 * Vnode op for VM putpages.
228 */
229int
230nfs_putpages(ap)
231 struct vop_putpages_args /* {
232 struct vnode *a_vp;
233 vm_page_t *a_m;
234 int a_count;
235 int a_sync;
236 int *a_rtvals;
237 vm_ooffset_t a_offset;
238 } */ *ap;
239{
240 struct uio uio;
241 struct iovec iov;
242 vm_offset_t kva;
243 struct buf *bp;
244 int iomode, must_commit, i, error, npages, count;
245 off_t offset;
246 int *rtvals;
247 struct vnode *vp;
248 struct proc *p;
249 struct ucred *cred;
250 struct nfsmount *nmp;
251 struct nfsnode *np;
252 vm_page_t *pages;
253
254 vp = ap->a_vp;
255 np = VTONFS(vp);
256 p = curproc; /* XXX */
257 cred = curproc->p_ucred; /* XXX */
258 nmp = VFSTONFS(vp->v_mount);
259 pages = ap->a_m;
260 count = ap->a_count;
261 rtvals = ap->a_rtvals;
262 npages = btoc(count);
263 offset = IDX_TO_OFF(pages[0]->pindex);
264
265 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
266 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
267 (void)nfs_fsinfo(nmp, vp, cred, p);
268
269 for (i = 0; i < npages; i++) {
270 rtvals[i] = VM_PAGER_AGAIN;
271 }
272
273 /*
274 * When putting pages, do not extend file past EOF.
275 */
276
277 if (offset + count > np->n_size) {
278 count = np->n_size - offset;
279 if (count < 0)
280 count = 0;
281 }
282
283 /*
284 * We use only the kva address for the buffer, but this is extremely
285 * convienient and fast.
286 */
287 bp = getpbuf(&nfs_pbuf_freecnt);
288
289 kva = (vm_offset_t) bp->b_data;
290 pmap_qenter(kva, pages, npages);
291
292 iov.iov_base = (caddr_t) kva;
293 iov.iov_len = count;
294 uio.uio_iov = &iov;
295 uio.uio_iovcnt = 1;
296 uio.uio_offset = offset;
297 uio.uio_resid = count;
298 uio.uio_segflg = UIO_SYSSPACE;
299 uio.uio_rw = UIO_WRITE;
300 uio.uio_procp = p;
301
302 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
303 iomode = NFSV3WRITE_UNSTABLE;
304 else
305 iomode = NFSV3WRITE_FILESYNC;
306
307 error = nfs_writerpc(vp, &uio, cred, &iomode, &must_commit);
308
309 pmap_qremove(kva, npages);
310 relpbuf(bp, &nfs_pbuf_freecnt);
311
312 if (!error) {
313 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
314 for (i = 0; i < nwritten; i++) {
315 rtvals[i] = VM_PAGER_OK;
316 pages[i]->dirty = 0;
317 }
318 if (must_commit)
319 nfs_clearcommit(vp->v_mount);
320 }
321 return rtvals[0];
322}
323
324/*
325 * Vnode op for read using bio
326 */
327int
328nfs_bioread(vp, uio, ioflag, cred)
329 register struct vnode *vp;
330 register struct uio *uio;
331 int ioflag;
332 struct ucred *cred;
333{
334 register struct nfsnode *np = VTONFS(vp);
335 register int biosize, i;
336 struct buf *bp = 0, *rabp;
337 struct vattr vattr;
338 struct proc *p;
339 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
340 daddr_t lbn, rabn;
341 int bcount;
342 int nra, error = 0, n = 0, on = 0;
343
344#ifdef DIAGNOSTIC
345 if (uio->uio_rw != UIO_READ)
346 panic("nfs_read mode");
347#endif
348 if (uio->uio_resid == 0)
349 return (0);
350 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
351 return (EINVAL);
352 p = uio->uio_procp;
353 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
354 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
355 (void)nfs_fsinfo(nmp, vp, cred, p);
356 if (vp->v_type != VDIR &&
357 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
358 return (EFBIG);
359 biosize = vp->v_mount->mnt_stat.f_iosize;
360 /*
361 * For nfs, cache consistency can only be maintained approximately.
362 * Although RFC1094 does not specify the criteria, the following is
363 * believed to be compatible with the reference port.
364 * For nqnfs, full cache consistency is maintained within the loop.
365 * For nfs:
366 * If the file's modify time on the server has changed since the
367 * last read rpc or you have written to the file,
368 * you may have lost data cache consistency with the
369 * server, so flush all of the file's data out of the cache.
370 * Then force a getattr rpc to ensure that you have up to date
371 * attributes.
372 * NB: This implies that cache data can be read when up to
373 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
374 * attributes this could be forced by setting n_attrstamp to 0 before
375 * the VOP_GETATTR() call.
376 */
377 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) {
378 if (np->n_flag & NMODIFIED) {
379 if (vp->v_type != VREG) {
380 if (vp->v_type != VDIR)
381 panic("nfs: bioread, not dir");
382 nfs_invaldir(vp);
383 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
384 if (error)
385 return (error);
386 }
387 np->n_attrstamp = 0;
388 error = VOP_GETATTR(vp, &vattr, cred, p);
389 if (error)
390 return (error);
391 np->n_mtime = vattr.va_mtime.tv_sec;
392 } else {
393 error = VOP_GETATTR(vp, &vattr, cred, p);
394 if (error)
395 return (error);
396 if (np->n_mtime != vattr.va_mtime.tv_sec) {
397 if (vp->v_type == VDIR)
398 nfs_invaldir(vp);
399 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
400 if (error)
401 return (error);
402 np->n_mtime = vattr.va_mtime.tv_sec;
403 }
404 }
405 }
406 do {
407
408 /*
409 * Get a valid lease. If cached data is stale, flush it.
410 */
411 if (nmp->nm_flag & NFSMNT_NQNFS) {
412 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
413 do {
414 error = nqnfs_getlease(vp, ND_READ, cred, p);
415 } while (error == NQNFS_EXPIRED);
416 if (error)
417 return (error);
418 if (np->n_lrev != np->n_brev ||
419 (np->n_flag & NQNFSNONCACHE) ||
420 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
421 if (vp->v_type == VDIR)
422 nfs_invaldir(vp);
423 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
424 if (error)
425 return (error);
426 np->n_brev = np->n_lrev;
427 }
428 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
429 nfs_invaldir(vp);
430 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
431 if (error)
432 return (error);
433 }
434 }
435 if (np->n_flag & NQNFSNONCACHE) {
436 switch (vp->v_type) {
437 case VREG:
438 return (nfs_readrpc(vp, uio, cred));
439 case VLNK:
440 return (nfs_readlinkrpc(vp, uio, cred));
441 case VDIR:
442 break;
443 default:
444 printf(" NQNFSNONCACHE: type %x unexpected\n",
445 vp->v_type);
446 };
447 }
448 switch (vp->v_type) {
449 case VREG:
450 nfsstats.biocache_reads++;
451 lbn = uio->uio_offset / biosize;
452 on = uio->uio_offset & (biosize - 1);
453
454 /*
455 * Start the read ahead(s), as required.
456 */
457 if (nfs_numasync > 0 && nmp->nm_readahead > 0) {
458 for (nra = 0; nra < nmp->nm_readahead &&
459 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
460 rabn = lbn + 1 + nra;
461 if (!incore(vp, rabn)) {
462 rabp = nfs_getcacheblk(vp, rabn, biosize, p);
463 if (!rabp)
464 return (EINTR);
465 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
466 rabp->b_flags |= (B_READ | B_ASYNC);
467 vfs_busy_pages(rabp, 0);
38 */
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/mount.h>
49#include <sys/kernel.h>
50
51#include <vm/vm.h>
52#include <vm/vm_extern.h>
53#include <vm/vm_prot.h>
54#include <vm/vm_page.h>
55#include <vm/vm_object.h>
56#include <vm/vm_pager.h>
57#include <vm/vnode_pager.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
66static struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size,
67 struct proc *p));
68
69extern int nfs_numasync;
70extern int nfs_pbuf_freecnt;
71extern struct nfsstats nfsstats;
72
73/*
74 * Vnode op for VM getpages.
75 */
76int
77nfs_getpages(ap)
78 struct vop_getpages_args /* {
79 struct vnode *a_vp;
80 vm_page_t *a_m;
81 int a_count;
82 int a_reqpage;
83 vm_ooffset_t a_offset;
84 } */ *ap;
85{
86 int i, error, nextoff, size, toff, count, npages;
87 struct uio uio;
88 struct iovec iov;
89 vm_offset_t kva;
90 struct buf *bp;
91 struct vnode *vp;
92 struct proc *p;
93 struct ucred *cred;
94 struct nfsmount *nmp;
95 vm_page_t *pages;
96
97 vp = ap->a_vp;
98 p = curproc; /* XXX */
99 cred = curproc->p_ucred; /* XXX */
100 nmp = VFSTONFS(vp->v_mount);
101 pages = ap->a_m;
102 count = ap->a_count;
103
104 if (vp->v_object == NULL) {
105 printf("nfs_getpages: called with non-merged cache vnode??\n");
106 return VM_PAGER_ERROR;
107 }
108
109 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
110 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
111 (void)nfs_fsinfo(nmp, vp, cred, p);
112
113 npages = btoc(count);
114
115 /*
116 * If the requested page is partially valid, just return it and
117 * allow the pager to zero-out the blanks. Partially valid pages
118 * can only occur at the file EOF.
119 */
120
121 {
122 vm_page_t m = pages[ap->a_reqpage];
123
124 if (m->valid != 0) {
125 /* handled by vm_fault now */
126 /* vm_page_zero_invalid(m, TRUE); */
127 for (i = 0; i < npages; ++i) {
128 if (i != ap->a_reqpage)
129 vnode_pager_freepage(pages[i]);
130 }
131 return(0);
132 }
133 }
134
135 /*
136 * We use only the kva address for the buffer, but this is extremely
137 * convienient and fast.
138 */
139 bp = getpbuf(&nfs_pbuf_freecnt);
140
141 kva = (vm_offset_t) bp->b_data;
142 pmap_qenter(kva, pages, npages);
143
144 iov.iov_base = (caddr_t) kva;
145 iov.iov_len = count;
146 uio.uio_iov = &iov;
147 uio.uio_iovcnt = 1;
148 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
149 uio.uio_resid = count;
150 uio.uio_segflg = UIO_SYSSPACE;
151 uio.uio_rw = UIO_READ;
152 uio.uio_procp = p;
153
154 error = nfs_readrpc(vp, &uio, cred);
155 pmap_qremove(kva, npages);
156
157 relpbuf(bp, &nfs_pbuf_freecnt);
158
159 if (error && (uio.uio_resid == count)) {
160 printf("nfs_getpages: error %d\n", error);
161 for (i = 0; i < npages; ++i) {
162 if (i != ap->a_reqpage)
163 vnode_pager_freepage(pages[i]);
164 }
165 return VM_PAGER_ERROR;
166 }
167
168 /*
169 * Calculate the number of bytes read and validate only that number
170 * of bytes. Note that due to pending writes, size may be 0. This
171 * does not mean that the remaining data is invalid!
172 */
173
174 size = count - uio.uio_resid;
175
176 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
177 vm_page_t m;
178 nextoff = toff + PAGE_SIZE;
179 m = pages[i];
180
181 m->flags &= ~PG_ZERO;
182
183 if (nextoff <= size) {
184 /*
185 * Read operation filled an entire page
186 */
187 m->valid = VM_PAGE_BITS_ALL;
188 m->dirty = 0;
189 } else if (size > toff) {
190 /*
191 * Read operation filled a partial page.
192 */
193 m->valid = 0;
194 vm_page_set_validclean(m, 0, size - toff);
195 /* handled by vm_fault now */
196 /* vm_page_zero_invalid(m, TRUE); */
197 }
198
199 if (i != ap->a_reqpage) {
200 /*
201 * Whether or not to leave the page activated is up in
202 * the air, but we should put the page on a page queue
203 * somewhere (it already is in the object). Result:
204 * It appears that emperical results show that
205 * deactivating pages is best.
206 */
207
208 /*
209 * Just in case someone was asking for this page we
210 * now tell them that it is ok to use.
211 */
212 if (!error) {
213 if (m->flags & PG_WANTED)
214 vm_page_activate(m);
215 else
216 vm_page_deactivate(m);
217 vm_page_wakeup(m);
218 } else {
219 vnode_pager_freepage(m);
220 }
221 }
222 }
223 return 0;
224}
225
226/*
227 * Vnode op for VM putpages.
228 */
229int
230nfs_putpages(ap)
231 struct vop_putpages_args /* {
232 struct vnode *a_vp;
233 vm_page_t *a_m;
234 int a_count;
235 int a_sync;
236 int *a_rtvals;
237 vm_ooffset_t a_offset;
238 } */ *ap;
239{
240 struct uio uio;
241 struct iovec iov;
242 vm_offset_t kva;
243 struct buf *bp;
244 int iomode, must_commit, i, error, npages, count;
245 off_t offset;
246 int *rtvals;
247 struct vnode *vp;
248 struct proc *p;
249 struct ucred *cred;
250 struct nfsmount *nmp;
251 struct nfsnode *np;
252 vm_page_t *pages;
253
254 vp = ap->a_vp;
255 np = VTONFS(vp);
256 p = curproc; /* XXX */
257 cred = curproc->p_ucred; /* XXX */
258 nmp = VFSTONFS(vp->v_mount);
259 pages = ap->a_m;
260 count = ap->a_count;
261 rtvals = ap->a_rtvals;
262 npages = btoc(count);
263 offset = IDX_TO_OFF(pages[0]->pindex);
264
265 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
266 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
267 (void)nfs_fsinfo(nmp, vp, cred, p);
268
269 for (i = 0; i < npages; i++) {
270 rtvals[i] = VM_PAGER_AGAIN;
271 }
272
273 /*
274 * When putting pages, do not extend file past EOF.
275 */
276
277 if (offset + count > np->n_size) {
278 count = np->n_size - offset;
279 if (count < 0)
280 count = 0;
281 }
282
283 /*
284 * We use only the kva address for the buffer, but this is extremely
285 * convienient and fast.
286 */
287 bp = getpbuf(&nfs_pbuf_freecnt);
288
289 kva = (vm_offset_t) bp->b_data;
290 pmap_qenter(kva, pages, npages);
291
292 iov.iov_base = (caddr_t) kva;
293 iov.iov_len = count;
294 uio.uio_iov = &iov;
295 uio.uio_iovcnt = 1;
296 uio.uio_offset = offset;
297 uio.uio_resid = count;
298 uio.uio_segflg = UIO_SYSSPACE;
299 uio.uio_rw = UIO_WRITE;
300 uio.uio_procp = p;
301
302 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
303 iomode = NFSV3WRITE_UNSTABLE;
304 else
305 iomode = NFSV3WRITE_FILESYNC;
306
307 error = nfs_writerpc(vp, &uio, cred, &iomode, &must_commit);
308
309 pmap_qremove(kva, npages);
310 relpbuf(bp, &nfs_pbuf_freecnt);
311
312 if (!error) {
313 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
314 for (i = 0; i < nwritten; i++) {
315 rtvals[i] = VM_PAGER_OK;
316 pages[i]->dirty = 0;
317 }
318 if (must_commit)
319 nfs_clearcommit(vp->v_mount);
320 }
321 return rtvals[0];
322}
323
324/*
325 * Vnode op for read using bio
326 */
327int
328nfs_bioread(vp, uio, ioflag, cred)
329 register struct vnode *vp;
330 register struct uio *uio;
331 int ioflag;
332 struct ucred *cred;
333{
334 register struct nfsnode *np = VTONFS(vp);
335 register int biosize, i;
336 struct buf *bp = 0, *rabp;
337 struct vattr vattr;
338 struct proc *p;
339 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
340 daddr_t lbn, rabn;
341 int bcount;
342 int nra, error = 0, n = 0, on = 0;
343
344#ifdef DIAGNOSTIC
345 if (uio->uio_rw != UIO_READ)
346 panic("nfs_read mode");
347#endif
348 if (uio->uio_resid == 0)
349 return (0);
350 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
351 return (EINVAL);
352 p = uio->uio_procp;
353 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
354 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
355 (void)nfs_fsinfo(nmp, vp, cred, p);
356 if (vp->v_type != VDIR &&
357 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
358 return (EFBIG);
359 biosize = vp->v_mount->mnt_stat.f_iosize;
360 /*
361 * For nfs, cache consistency can only be maintained approximately.
362 * Although RFC1094 does not specify the criteria, the following is
363 * believed to be compatible with the reference port.
364 * For nqnfs, full cache consistency is maintained within the loop.
365 * For nfs:
366 * If the file's modify time on the server has changed since the
367 * last read rpc or you have written to the file,
368 * you may have lost data cache consistency with the
369 * server, so flush all of the file's data out of the cache.
370 * Then force a getattr rpc to ensure that you have up to date
371 * attributes.
372 * NB: This implies that cache data can be read when up to
373 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
374 * attributes this could be forced by setting n_attrstamp to 0 before
375 * the VOP_GETATTR() call.
376 */
377 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) {
378 if (np->n_flag & NMODIFIED) {
379 if (vp->v_type != VREG) {
380 if (vp->v_type != VDIR)
381 panic("nfs: bioread, not dir");
382 nfs_invaldir(vp);
383 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
384 if (error)
385 return (error);
386 }
387 np->n_attrstamp = 0;
388 error = VOP_GETATTR(vp, &vattr, cred, p);
389 if (error)
390 return (error);
391 np->n_mtime = vattr.va_mtime.tv_sec;
392 } else {
393 error = VOP_GETATTR(vp, &vattr, cred, p);
394 if (error)
395 return (error);
396 if (np->n_mtime != vattr.va_mtime.tv_sec) {
397 if (vp->v_type == VDIR)
398 nfs_invaldir(vp);
399 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
400 if (error)
401 return (error);
402 np->n_mtime = vattr.va_mtime.tv_sec;
403 }
404 }
405 }
406 do {
407
408 /*
409 * Get a valid lease. If cached data is stale, flush it.
410 */
411 if (nmp->nm_flag & NFSMNT_NQNFS) {
412 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
413 do {
414 error = nqnfs_getlease(vp, ND_READ, cred, p);
415 } while (error == NQNFS_EXPIRED);
416 if (error)
417 return (error);
418 if (np->n_lrev != np->n_brev ||
419 (np->n_flag & NQNFSNONCACHE) ||
420 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
421 if (vp->v_type == VDIR)
422 nfs_invaldir(vp);
423 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
424 if (error)
425 return (error);
426 np->n_brev = np->n_lrev;
427 }
428 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
429 nfs_invaldir(vp);
430 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
431 if (error)
432 return (error);
433 }
434 }
435 if (np->n_flag & NQNFSNONCACHE) {
436 switch (vp->v_type) {
437 case VREG:
438 return (nfs_readrpc(vp, uio, cred));
439 case VLNK:
440 return (nfs_readlinkrpc(vp, uio, cred));
441 case VDIR:
442 break;
443 default:
444 printf(" NQNFSNONCACHE: type %x unexpected\n",
445 vp->v_type);
446 };
447 }
448 switch (vp->v_type) {
449 case VREG:
450 nfsstats.biocache_reads++;
451 lbn = uio->uio_offset / biosize;
452 on = uio->uio_offset & (biosize - 1);
453
454 /*
455 * Start the read ahead(s), as required.
456 */
457 if (nfs_numasync > 0 && nmp->nm_readahead > 0) {
458 for (nra = 0; nra < nmp->nm_readahead &&
459 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
460 rabn = lbn + 1 + nra;
461 if (!incore(vp, rabn)) {
462 rabp = nfs_getcacheblk(vp, rabn, biosize, p);
463 if (!rabp)
464 return (EINTR);
465 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
466 rabp->b_flags |= (B_READ | B_ASYNC);
467 vfs_busy_pages(rabp, 0);
468 if (nfs_asyncio(rabp, cred)) {
468 if (nfs_asyncio(rabp, cred, p)) {
469 rabp->b_flags |= B_INVAL|B_ERROR;
470 vfs_unbusy_pages(rabp);
471 brelse(rabp);
472 }
473 } else
474 brelse(rabp);
475 }
476 }
477 }
478
479 /*
480 * Obtain the buffer cache block. Figure out the buffer size
481 * when we are at EOF. nfs_getcacheblk() will also force
482 * uncached delayed-writes to be flushed to the server.
483 *
484 * Note that bcount is *not* DEV_BSIZE aligned.
485 */
486
487 bcount = biosize;
488 if ((off_t)lbn * biosize >= np->n_size) {
489 bcount = 0;
490 } else if ((off_t)(lbn + 1) * biosize > np->n_size) {
491 bcount = np->n_size - (off_t)lbn * biosize;
492 }
493
494 bp = nfs_getcacheblk(vp, lbn, bcount, p);
495 if (!bp)
496 return (EINTR);
497
498 /*
499 * If B_CACHE is not set, we must issue the read. If this
500 * fails, we return an error.
501 */
502
503 if ((bp->b_flags & B_CACHE) == 0) {
504 bp->b_flags |= B_READ;
505 vfs_busy_pages(bp, 0);
506 error = nfs_doio(bp, cred, p);
507 if (error) {
508 brelse(bp);
509 return (error);
510 }
511 }
512
513 /*
514 * on is the offset into the current bp. Figure out how many
515 * bytes we can copy out of the bp. Note that bcount is
516 * NOT DEV_BSIZE aligned.
517 *
518 * Then figure out how many bytes we can copy into the uio.
519 */
520
521 n = 0;
522 if (on < bcount)
523 n = min((unsigned)(bcount - on), uio->uio_resid);
524
525 vp->v_lastr = lbn;
526 break;
527 case VLNK:
528 nfsstats.biocache_readlinks++;
529 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
530 if (!bp)
531 return (EINTR);
532 if ((bp->b_flags & B_CACHE) == 0) {
533 bp->b_flags |= B_READ;
534 vfs_busy_pages(bp, 0);
535 error = nfs_doio(bp, cred, p);
536 if (error) {
537 bp->b_flags |= B_ERROR;
538 brelse(bp);
539 return (error);
540 }
541 }
542 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
543 on = 0;
544 break;
545 case VDIR:
546 nfsstats.biocache_readdirs++;
547 if (np->n_direofoffset
548 && uio->uio_offset >= np->n_direofoffset) {
549 return (0);
550 }
551 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
552 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
553 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p);
554 if (!bp)
555 return (EINTR);
556 if ((bp->b_flags & B_CACHE) == 0) {
557 bp->b_flags |= B_READ;
558 vfs_busy_pages(bp, 0);
559 error = nfs_doio(bp, cred, p);
560 if (error) {
561 brelse(bp);
562 }
563 while (error == NFSERR_BAD_COOKIE) {
564 printf("got bad cookie vp %p bp %p\n", vp, bp);
565 nfs_invaldir(vp);
566 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
567 /*
568 * Yuck! The directory has been modified on the
569 * server. The only way to get the block is by
570 * reading from the beginning to get all the
571 * offset cookies.
572 *
573 * Leave the last bp intact unless there is an error.
574 * Loop back up to the while if the error is another
575 * NFSERR_BAD_COOKIE (double yuch!).
576 */
577 for (i = 0; i <= lbn && !error; i++) {
578 if (np->n_direofoffset
579 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
580 return (0);
581 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p);
582 if (!bp)
583 return (EINTR);
584 if ((bp->b_flags & B_CACHE) == 0) {
585 bp->b_flags |= B_READ;
586 vfs_busy_pages(bp, 0);
587 error = nfs_doio(bp, cred, p);
588 /*
589 * no error + B_INVAL == directory EOF,
590 * use the block.
591 */
592 if (error == 0 && (bp->b_flags & B_INVAL))
593 break;
594 }
595 /*
596 * An error will throw away the block and the
597 * for loop will break out. If no error and this
598 * is not the block we want, we throw away the
599 * block and go for the next one via the for loop.
600 */
601 if (error || i < lbn)
602 brelse(bp);
603 }
604 }
605 /*
606 * The above while is repeated if we hit another cookie
607 * error. If we hit an error and it wasn't a cookie error,
608 * we give up.
609 */
610 if (error)
611 return (error);
612 }
613
614 /*
615 * If not eof and read aheads are enabled, start one.
616 * (You need the current block first, so that you have the
617 * directory offset cookie of the next block.)
618 */
619 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
620 (bp->b_flags & B_INVAL) == 0 &&
621 (np->n_direofoffset == 0 ||
622 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
623 !(np->n_flag & NQNFSNONCACHE) &&
624 !incore(vp, lbn + 1)) {
625 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p);
626 if (rabp) {
627 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
628 rabp->b_flags |= (B_READ | B_ASYNC);
629 vfs_busy_pages(rabp, 0);
469 rabp->b_flags |= B_INVAL|B_ERROR;
470 vfs_unbusy_pages(rabp);
471 brelse(rabp);
472 }
473 } else
474 brelse(rabp);
475 }
476 }
477 }
478
479 /*
480 * Obtain the buffer cache block. Figure out the buffer size
481 * when we are at EOF. nfs_getcacheblk() will also force
482 * uncached delayed-writes to be flushed to the server.
483 *
484 * Note that bcount is *not* DEV_BSIZE aligned.
485 */
486
487 bcount = biosize;
488 if ((off_t)lbn * biosize >= np->n_size) {
489 bcount = 0;
490 } else if ((off_t)(lbn + 1) * biosize > np->n_size) {
491 bcount = np->n_size - (off_t)lbn * biosize;
492 }
493
494 bp = nfs_getcacheblk(vp, lbn, bcount, p);
495 if (!bp)
496 return (EINTR);
497
498 /*
499 * If B_CACHE is not set, we must issue the read. If this
500 * fails, we return an error.
501 */
502
503 if ((bp->b_flags & B_CACHE) == 0) {
504 bp->b_flags |= B_READ;
505 vfs_busy_pages(bp, 0);
506 error = nfs_doio(bp, cred, p);
507 if (error) {
508 brelse(bp);
509 return (error);
510 }
511 }
512
513 /*
514 * on is the offset into the current bp. Figure out how many
515 * bytes we can copy out of the bp. Note that bcount is
516 * NOT DEV_BSIZE aligned.
517 *
518 * Then figure out how many bytes we can copy into the uio.
519 */
520
521 n = 0;
522 if (on < bcount)
523 n = min((unsigned)(bcount - on), uio->uio_resid);
524
525 vp->v_lastr = lbn;
526 break;
527 case VLNK:
528 nfsstats.biocache_readlinks++;
529 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
530 if (!bp)
531 return (EINTR);
532 if ((bp->b_flags & B_CACHE) == 0) {
533 bp->b_flags |= B_READ;
534 vfs_busy_pages(bp, 0);
535 error = nfs_doio(bp, cred, p);
536 if (error) {
537 bp->b_flags |= B_ERROR;
538 brelse(bp);
539 return (error);
540 }
541 }
542 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
543 on = 0;
544 break;
545 case VDIR:
546 nfsstats.biocache_readdirs++;
547 if (np->n_direofoffset
548 && uio->uio_offset >= np->n_direofoffset) {
549 return (0);
550 }
551 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
552 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
553 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p);
554 if (!bp)
555 return (EINTR);
556 if ((bp->b_flags & B_CACHE) == 0) {
557 bp->b_flags |= B_READ;
558 vfs_busy_pages(bp, 0);
559 error = nfs_doio(bp, cred, p);
560 if (error) {
561 brelse(bp);
562 }
563 while (error == NFSERR_BAD_COOKIE) {
564 printf("got bad cookie vp %p bp %p\n", vp, bp);
565 nfs_invaldir(vp);
566 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
567 /*
568 * Yuck! The directory has been modified on the
569 * server. The only way to get the block is by
570 * reading from the beginning to get all the
571 * offset cookies.
572 *
573 * Leave the last bp intact unless there is an error.
574 * Loop back up to the while if the error is another
575 * NFSERR_BAD_COOKIE (double yuch!).
576 */
577 for (i = 0; i <= lbn && !error; i++) {
578 if (np->n_direofoffset
579 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
580 return (0);
581 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p);
582 if (!bp)
583 return (EINTR);
584 if ((bp->b_flags & B_CACHE) == 0) {
585 bp->b_flags |= B_READ;
586 vfs_busy_pages(bp, 0);
587 error = nfs_doio(bp, cred, p);
588 /*
589 * no error + B_INVAL == directory EOF,
590 * use the block.
591 */
592 if (error == 0 && (bp->b_flags & B_INVAL))
593 break;
594 }
595 /*
596 * An error will throw away the block and the
597 * for loop will break out. If no error and this
598 * is not the block we want, we throw away the
599 * block and go for the next one via the for loop.
600 */
601 if (error || i < lbn)
602 brelse(bp);
603 }
604 }
605 /*
606 * The above while is repeated if we hit another cookie
607 * error. If we hit an error and it wasn't a cookie error,
608 * we give up.
609 */
610 if (error)
611 return (error);
612 }
613
614 /*
615 * If not eof and read aheads are enabled, start one.
616 * (You need the current block first, so that you have the
617 * directory offset cookie of the next block.)
618 */
619 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
620 (bp->b_flags & B_INVAL) == 0 &&
621 (np->n_direofoffset == 0 ||
622 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
623 !(np->n_flag & NQNFSNONCACHE) &&
624 !incore(vp, lbn + 1)) {
625 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p);
626 if (rabp) {
627 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
628 rabp->b_flags |= (B_READ | B_ASYNC);
629 vfs_busy_pages(rabp, 0);
630 if (nfs_asyncio(rabp, cred)) {
630 if (nfs_asyncio(rabp, cred, p)) {
631 rabp->b_flags |= B_INVAL|B_ERROR;
632 vfs_unbusy_pages(rabp);
633 brelse(rabp);
634 }
635 } else {
636 brelse(rabp);
637 }
638 }
639 }
640 /*
641 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
642 * chopped for the EOF condition, we cannot tell how large
643 * NFS directories are going to be until we hit EOF. So
644 * an NFS directory buffer is *not* chopped to its EOF. Now,
645 * it just so happens that b_resid will effectively chop it
646 * to EOF. *BUT* this information is lost if the buffer goes
647 * away and is reconstituted into a B_CACHE state ( due to
648 * being VMIO ) later. So we keep track of the directory eof
649 * in np->n_direofoffset and chop it off as an extra step
650 * right here.
651 */
652 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
653 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
654 n = np->n_direofoffset - uio->uio_offset;
655 break;
656 default:
657 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
658 break;
659 };
660
661 if (n > 0) {
662 error = uiomove(bp->b_data + on, (int)n, uio);
663 }
664 switch (vp->v_type) {
665 case VREG:
666 break;
667 case VLNK:
668 n = 0;
669 break;
670 case VDIR:
671 /*
672 * Invalidate buffer if caching is disabled, forcing a
673 * re-read from the remote later.
674 */
675 if (np->n_flag & NQNFSNONCACHE)
676 bp->b_flags |= B_INVAL;
677 break;
678 default:
679 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
680 }
681 brelse(bp);
682 } while (error == 0 && uio->uio_resid > 0 && n > 0);
683 return (error);
684}
685
686/*
687 * Vnode op for write using bio
688 */
689int
690nfs_write(ap)
691 struct vop_write_args /* {
692 struct vnode *a_vp;
693 struct uio *a_uio;
694 int a_ioflag;
695 struct ucred *a_cred;
696 } */ *ap;
697{
698 int biosize;
699 struct uio *uio = ap->a_uio;
700 struct proc *p = uio->uio_procp;
701 struct vnode *vp = ap->a_vp;
702 struct nfsnode *np = VTONFS(vp);
703 struct ucred *cred = ap->a_cred;
704 int ioflag = ap->a_ioflag;
705 struct buf *bp;
706 struct vattr vattr;
707 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
708 daddr_t lbn;
709 int bcount;
710 int n, on, error = 0, iomode, must_commit;
711
712#ifdef DIAGNOSTIC
713 if (uio->uio_rw != UIO_WRITE)
714 panic("nfs_write mode");
715 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
716 panic("nfs_write proc");
717#endif
718 if (vp->v_type != VREG)
719 return (EIO);
720 if (np->n_flag & NWRITEERR) {
721 np->n_flag &= ~NWRITEERR;
722 return (np->n_error);
723 }
724 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
725 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
726 (void)nfs_fsinfo(nmp, vp, cred, p);
727 if (ioflag & (IO_APPEND | IO_SYNC)) {
728 if (np->n_flag & NMODIFIED) {
729 np->n_attrstamp = 0;
730 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
731 if (error)
732 return (error);
733 }
734 if (ioflag & IO_APPEND) {
735 np->n_attrstamp = 0;
736 error = VOP_GETATTR(vp, &vattr, cred, p);
737 if (error)
738 return (error);
739 uio->uio_offset = np->n_size;
740 }
741 }
742 if (uio->uio_offset < 0)
743 return (EINVAL);
744 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
745 return (EFBIG);
746 if (uio->uio_resid == 0)
747 return (0);
748 /*
749 * Maybe this should be above the vnode op call, but so long as
750 * file servers have no limits, i don't think it matters
751 */
752 if (p && uio->uio_offset + uio->uio_resid >
753 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
754 psignal(p, SIGXFSZ);
755 return (EFBIG);
756 }
757
758 biosize = vp->v_mount->mnt_stat.f_iosize;
759
760 do {
761 /*
762 * Check for a valid write lease.
763 */
764 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
765 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
766 do {
767 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
768 } while (error == NQNFS_EXPIRED);
769 if (error)
770 return (error);
771 if (np->n_lrev != np->n_brev ||
772 (np->n_flag & NQNFSNONCACHE)) {
773 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
774 if (error)
775 return (error);
776 np->n_brev = np->n_lrev;
777 }
778 }
779 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
780 iomode = NFSV3WRITE_FILESYNC;
781 error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
782 if (must_commit)
783 nfs_clearcommit(vp->v_mount);
784 return (error);
785 }
786 nfsstats.biocache_writes++;
787 lbn = uio->uio_offset / biosize;
788 on = uio->uio_offset & (biosize-1);
789 n = min((unsigned)(biosize - on), uio->uio_resid);
790again:
791 /*
792 * Handle direct append and file extension cases, calculate
793 * unaligned buffer size.
794 */
795
796 if (uio->uio_offset == np->n_size && n) {
797 /*
798 * special append case. Obtain buffer prior to
799 * resizing it to maintain B_CACHE.
800 */
801 long save;
802
803 bcount = on;
804 bp = nfs_getcacheblk(vp, lbn, bcount, p);
805 save = bp->b_flags & B_CACHE;
806
807 np->n_size = uio->uio_offset + n;
808 np->n_flag |= NMODIFIED;
809 vnode_pager_setsize(vp, np->n_size);
810
811 bcount += n;
812 allocbuf(bp, bcount);
813 bp->b_flags |= save;
814 } else {
815 if (uio->uio_offset + n > np->n_size) {
816 np->n_size = uio->uio_offset + n;
817 np->n_flag |= NMODIFIED;
818 vnode_pager_setsize(vp, np->n_size);
819 }
820 bcount = biosize;
821 if ((off_t)(lbn + 1) * biosize > np->n_size)
822 bcount = np->n_size - (off_t)lbn * biosize;
823 bp = nfs_getcacheblk(vp, lbn, bcount, p);
824 }
825
826 /*
827 * Issue a READ if B_CACHE is not set. In special-append
828 * mode, B_CACHE is based on the buffer prior to the write
829 * op and is typically set, avoiding the read. If a read
830 * is required in special append mode, the server will
831 * probably send us a short-read since we extended the file
832 * on our end, resulting in b_resid == 0 and, thusly,
833 * B_CACHE getting set.
834 *
835 * We can also avoid issuing the read if the write covers
836 * the entire buffer. We have to make sure the buffer state
837 * is reasonable in this case since we will not be initiating
838 * I/O. See the comments in kern/vfs_bio.c's getblk() for
839 * more information.
840 *
841 * B_CACHE may also be set due to the buffer being cached
842 * normally.
843 */
844
845 if (on == 0 && n == bcount) {
846 bp->b_flags |= B_CACHE;
847 bp->b_flags &= ~(B_ERROR | B_INVAL);
848 }
849
850 if ((bp->b_flags & B_CACHE) == 0) {
851 bp->b_flags |= B_READ;
852 vfs_busy_pages(bp, 0);
853 error = nfs_doio(bp, cred, p);
854 if (error) {
855 brelse(bp);
856 return (error);
857 }
858 }
859 if (!bp)
860 return (EINTR);
861 if (bp->b_wcred == NOCRED) {
862 crhold(cred);
863 bp->b_wcred = cred;
864 }
865 np->n_flag |= NMODIFIED;
866
867 /*
868 * If dirtyend exceeds file size, chop it down. If this
869 * creates a reverse-indexed or degenerate situation with
870 * dirtyoff/end, 0 them.
871 */
872
873 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
874 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
875 if (bp->b_dirtyoff >= bp->b_dirtyend)
876 bp->b_dirtyoff = bp->b_dirtyend = 0;
877
878 /*
879 * If the new write will leave a contiguous dirty
880 * area, just update the b_dirtyoff and b_dirtyend,
881 * otherwise force a write rpc of the old dirty area.
882 *
883 * While it is possible to merge discontiguous writes due to
884 * our having a B_CACHE buffer ( and thus valid read data
885 * for the hole), we don't because it could lead to
886 * significant cache coherency problems with multiple clients,
887 * especially if locking is implemented later on.
888 *
889 * as an optimization we could theoretically maintain
890 * a linked list of discontinuous areas, but we would still
891 * have to commit them separately so there isn't much
892 * advantage to it except perhaps a bit of asynchronization.
893 */
894
895 if (bp->b_dirtyend > 0 &&
896 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
631 rabp->b_flags |= B_INVAL|B_ERROR;
632 vfs_unbusy_pages(rabp);
633 brelse(rabp);
634 }
635 } else {
636 brelse(rabp);
637 }
638 }
639 }
640 /*
641 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
642 * chopped for the EOF condition, we cannot tell how large
643 * NFS directories are going to be until we hit EOF. So
644 * an NFS directory buffer is *not* chopped to its EOF. Now,
645 * it just so happens that b_resid will effectively chop it
646 * to EOF. *BUT* this information is lost if the buffer goes
647 * away and is reconstituted into a B_CACHE state ( due to
648 * being VMIO ) later. So we keep track of the directory eof
649 * in np->n_direofoffset and chop it off as an extra step
650 * right here.
651 */
652 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
653 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
654 n = np->n_direofoffset - uio->uio_offset;
655 break;
656 default:
657 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
658 break;
659 };
660
661 if (n > 0) {
662 error = uiomove(bp->b_data + on, (int)n, uio);
663 }
664 switch (vp->v_type) {
665 case VREG:
666 break;
667 case VLNK:
668 n = 0;
669 break;
670 case VDIR:
671 /*
672 * Invalidate buffer if caching is disabled, forcing a
673 * re-read from the remote later.
674 */
675 if (np->n_flag & NQNFSNONCACHE)
676 bp->b_flags |= B_INVAL;
677 break;
678 default:
679 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
680 }
681 brelse(bp);
682 } while (error == 0 && uio->uio_resid > 0 && n > 0);
683 return (error);
684}
685
686/*
687 * Vnode op for write using bio
688 */
689int
690nfs_write(ap)
691 struct vop_write_args /* {
692 struct vnode *a_vp;
693 struct uio *a_uio;
694 int a_ioflag;
695 struct ucred *a_cred;
696 } */ *ap;
697{
698 int biosize;
699 struct uio *uio = ap->a_uio;
700 struct proc *p = uio->uio_procp;
701 struct vnode *vp = ap->a_vp;
702 struct nfsnode *np = VTONFS(vp);
703 struct ucred *cred = ap->a_cred;
704 int ioflag = ap->a_ioflag;
705 struct buf *bp;
706 struct vattr vattr;
707 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
708 daddr_t lbn;
709 int bcount;
710 int n, on, error = 0, iomode, must_commit;
711
712#ifdef DIAGNOSTIC
713 if (uio->uio_rw != UIO_WRITE)
714 panic("nfs_write mode");
715 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
716 panic("nfs_write proc");
717#endif
718 if (vp->v_type != VREG)
719 return (EIO);
720 if (np->n_flag & NWRITEERR) {
721 np->n_flag &= ~NWRITEERR;
722 return (np->n_error);
723 }
724 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
725 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
726 (void)nfs_fsinfo(nmp, vp, cred, p);
727 if (ioflag & (IO_APPEND | IO_SYNC)) {
728 if (np->n_flag & NMODIFIED) {
729 np->n_attrstamp = 0;
730 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
731 if (error)
732 return (error);
733 }
734 if (ioflag & IO_APPEND) {
735 np->n_attrstamp = 0;
736 error = VOP_GETATTR(vp, &vattr, cred, p);
737 if (error)
738 return (error);
739 uio->uio_offset = np->n_size;
740 }
741 }
742 if (uio->uio_offset < 0)
743 return (EINVAL);
744 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
745 return (EFBIG);
746 if (uio->uio_resid == 0)
747 return (0);
748 /*
749 * Maybe this should be above the vnode op call, but so long as
750 * file servers have no limits, i don't think it matters
751 */
752 if (p && uio->uio_offset + uio->uio_resid >
753 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
754 psignal(p, SIGXFSZ);
755 return (EFBIG);
756 }
757
758 biosize = vp->v_mount->mnt_stat.f_iosize;
759
760 do {
761 /*
762 * Check for a valid write lease.
763 */
764 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
765 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
766 do {
767 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
768 } while (error == NQNFS_EXPIRED);
769 if (error)
770 return (error);
771 if (np->n_lrev != np->n_brev ||
772 (np->n_flag & NQNFSNONCACHE)) {
773 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
774 if (error)
775 return (error);
776 np->n_brev = np->n_lrev;
777 }
778 }
779 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
780 iomode = NFSV3WRITE_FILESYNC;
781 error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
782 if (must_commit)
783 nfs_clearcommit(vp->v_mount);
784 return (error);
785 }
786 nfsstats.biocache_writes++;
787 lbn = uio->uio_offset / biosize;
788 on = uio->uio_offset & (biosize-1);
789 n = min((unsigned)(biosize - on), uio->uio_resid);
790again:
791 /*
792 * Handle direct append and file extension cases, calculate
793 * unaligned buffer size.
794 */
795
796 if (uio->uio_offset == np->n_size && n) {
797 /*
798 * special append case. Obtain buffer prior to
799 * resizing it to maintain B_CACHE.
800 */
801 long save;
802
803 bcount = on;
804 bp = nfs_getcacheblk(vp, lbn, bcount, p);
805 save = bp->b_flags & B_CACHE;
806
807 np->n_size = uio->uio_offset + n;
808 np->n_flag |= NMODIFIED;
809 vnode_pager_setsize(vp, np->n_size);
810
811 bcount += n;
812 allocbuf(bp, bcount);
813 bp->b_flags |= save;
814 } else {
815 if (uio->uio_offset + n > np->n_size) {
816 np->n_size = uio->uio_offset + n;
817 np->n_flag |= NMODIFIED;
818 vnode_pager_setsize(vp, np->n_size);
819 }
820 bcount = biosize;
821 if ((off_t)(lbn + 1) * biosize > np->n_size)
822 bcount = np->n_size - (off_t)lbn * biosize;
823 bp = nfs_getcacheblk(vp, lbn, bcount, p);
824 }
825
826 /*
827 * Issue a READ if B_CACHE is not set. In special-append
828 * mode, B_CACHE is based on the buffer prior to the write
829 * op and is typically set, avoiding the read. If a read
830 * is required in special append mode, the server will
831 * probably send us a short-read since we extended the file
832 * on our end, resulting in b_resid == 0 and, thusly,
833 * B_CACHE getting set.
834 *
835 * We can also avoid issuing the read if the write covers
836 * the entire buffer. We have to make sure the buffer state
837 * is reasonable in this case since we will not be initiating
838 * I/O. See the comments in kern/vfs_bio.c's getblk() for
839 * more information.
840 *
841 * B_CACHE may also be set due to the buffer being cached
842 * normally.
843 */
844
845 if (on == 0 && n == bcount) {
846 bp->b_flags |= B_CACHE;
847 bp->b_flags &= ~(B_ERROR | B_INVAL);
848 }
849
850 if ((bp->b_flags & B_CACHE) == 0) {
851 bp->b_flags |= B_READ;
852 vfs_busy_pages(bp, 0);
853 error = nfs_doio(bp, cred, p);
854 if (error) {
855 brelse(bp);
856 return (error);
857 }
858 }
859 if (!bp)
860 return (EINTR);
861 if (bp->b_wcred == NOCRED) {
862 crhold(cred);
863 bp->b_wcred = cred;
864 }
865 np->n_flag |= NMODIFIED;
866
867 /*
868 * If dirtyend exceeds file size, chop it down. If this
869 * creates a reverse-indexed or degenerate situation with
870 * dirtyoff/end, 0 them.
871 */
872
873 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
874 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
875 if (bp->b_dirtyoff >= bp->b_dirtyend)
876 bp->b_dirtyoff = bp->b_dirtyend = 0;
877
878 /*
879 * If the new write will leave a contiguous dirty
880 * area, just update the b_dirtyoff and b_dirtyend,
881 * otherwise force a write rpc of the old dirty area.
882 *
883 * While it is possible to merge discontiguous writes due to
884 * our having a B_CACHE buffer ( and thus valid read data
885 * for the hole), we don't because it could lead to
886 * significant cache coherency problems with multiple clients,
887 * especially if locking is implemented later on.
888 *
889 * as an optimization we could theoretically maintain
890 * a linked list of discontinuous areas, but we would still
891 * have to commit them separately so there isn't much
892 * advantage to it except perhaps a bit of asynchronization.
893 */
894
895 if (bp->b_dirtyend > 0 &&
896 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
897 bp->b_proc = p;
898 if (VOP_BWRITE(bp) == EINTR)
899 return (EINTR);
900 goto again;
901 }
902
903 /*
904 * Check for valid write lease and get one as required.
905 * In case getblk() and/or bwrite() delayed us.
906 */
907 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
908 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
909 do {
910 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
911 } while (error == NQNFS_EXPIRED);
912 if (error) {
913 brelse(bp);
914 return (error);
915 }
916 if (np->n_lrev != np->n_brev ||
917 (np->n_flag & NQNFSNONCACHE)) {
918 brelse(bp);
919 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
920 if (error)
921 return (error);
922 np->n_brev = np->n_lrev;
923 goto again;
924 }
925 }
926
927 error = uiomove((char *)bp->b_data + on, n, uio);
928 bp->b_flags &= ~B_NEEDCOMMIT;
929 if (error) {
930 bp->b_flags |= B_ERROR;
931 brelse(bp);
932 return (error);
933 }
934
935 /*
936 * Only update dirtyoff/dirtyend if not a degenerate
937 * condition.
938 */
939 if (n) {
940 if (bp->b_dirtyend > 0) {
941 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
942 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
943 } else {
944 bp->b_dirtyoff = on;
945 bp->b_dirtyend = on + n;
946 }
947 vfs_bio_set_validclean(bp, on, n);
948 }
949
950 /*
951 * Since this block is being modified, it must be written
952 * again and not just committed.
953 */
954 bp->b_flags &= ~B_NEEDCOMMIT;
955
956 /*
957 * If the lease is non-cachable or IO_SYNC do bwrite().
958 *
959 * IO_INVAL appears to be unused. The idea appears to be
960 * to turn off caching in this case. Very odd. XXX
961 */
962 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
897 if (VOP_BWRITE(bp) == EINTR)
898 return (EINTR);
899 goto again;
900 }
901
902 /*
903 * Check for valid write lease and get one as required.
904 * In case getblk() and/or bwrite() delayed us.
905 */
906 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
907 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
908 do {
909 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
910 } while (error == NQNFS_EXPIRED);
911 if (error) {
912 brelse(bp);
913 return (error);
914 }
915 if (np->n_lrev != np->n_brev ||
916 (np->n_flag & NQNFSNONCACHE)) {
917 brelse(bp);
918 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
919 if (error)
920 return (error);
921 np->n_brev = np->n_lrev;
922 goto again;
923 }
924 }
925
926 error = uiomove((char *)bp->b_data + on, n, uio);
927 bp->b_flags &= ~B_NEEDCOMMIT;
928 if (error) {
929 bp->b_flags |= B_ERROR;
930 brelse(bp);
931 return (error);
932 }
933
934 /*
935 * Only update dirtyoff/dirtyend if not a degenerate
936 * condition.
937 */
938 if (n) {
939 if (bp->b_dirtyend > 0) {
940 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
941 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
942 } else {
943 bp->b_dirtyoff = on;
944 bp->b_dirtyend = on + n;
945 }
946 vfs_bio_set_validclean(bp, on, n);
947 }
948
949 /*
950 * Since this block is being modified, it must be written
951 * again and not just committed.
952 */
953 bp->b_flags &= ~B_NEEDCOMMIT;
954
955 /*
956 * If the lease is non-cachable or IO_SYNC do bwrite().
957 *
958 * IO_INVAL appears to be unused. The idea appears to be
959 * to turn off caching in this case. Very odd. XXX
960 */
961 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
963 bp->b_proc = p;
964 if (ioflag & IO_INVAL)
965 bp->b_flags |= B_NOCACHE;
966 error = VOP_BWRITE(bp);
967 if (error)
968 return (error);
969 if (np->n_flag & NQNFSNONCACHE) {
970 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
971 if (error)
972 return (error);
973 }
974 } else if ((n + on) == biosize &&
975 (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
962 if (ioflag & IO_INVAL)
963 bp->b_flags |= B_NOCACHE;
964 error = VOP_BWRITE(bp);
965 if (error)
966 return (error);
967 if (np->n_flag & NQNFSNONCACHE) {
968 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
969 if (error)
970 return (error);
971 }
972 } else if ((n + on) == biosize &&
973 (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
976 bp->b_proc = (struct proc *)0;
977 bp->b_flags |= B_ASYNC;
974 bp->b_flags |= B_ASYNC;
978 (void)nfs_writebp(bp, 0);
975 (void)nfs_writebp(bp, 0, 0);
979 } else {
980 bdwrite(bp);
981 }
982 } while (uio->uio_resid > 0 && n > 0);
983 return (0);
984}
985
986/*
987 * Get an nfs cache block.
988 * Allocate a new one if the block isn't currently in the cache
989 * and return the block marked busy. If the calling process is
990 * interrupted by a signal for an interruptible mount point, return
991 * NULL.
992 */
993static struct buf *
994nfs_getcacheblk(vp, bn, size, p)
995 struct vnode *vp;
996 daddr_t bn;
997 int size;
998 struct proc *p;
999{
1000 register struct buf *bp;
1001 struct mount *mp;
1002 struct nfsmount *nmp;
1003
1004 mp = vp->v_mount;
1005 nmp = VFSTONFS(mp);
1006
1007 if (nmp->nm_flag & NFSMNT_INT) {
1008 bp = getblk(vp, bn, size, PCATCH, 0);
1009 while (bp == (struct buf *)0) {
1010 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
1011 return ((struct buf *)0);
1012 bp = getblk(vp, bn, size, 0, 2 * hz);
1013 }
1014 } else {
1015 bp = getblk(vp, bn, size, 0, 0);
1016 }
1017
1018 if (vp->v_type == VREG) {
1019 int biosize;
1020
1021 biosize = mp->mnt_stat.f_iosize;
1022 bp->b_blkno = bn * (biosize / DEV_BSIZE);
1023 }
1024 return (bp);
1025}
1026
1027/*
1028 * Flush and invalidate all dirty buffers. If another process is already
1029 * doing the flush, just wait for completion.
1030 */
1031int
1032nfs_vinvalbuf(vp, flags, cred, p, intrflg)
1033 struct vnode *vp;
1034 int flags;
1035 struct ucred *cred;
1036 struct proc *p;
1037 int intrflg;
1038{
1039 register struct nfsnode *np = VTONFS(vp);
1040 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1041 int error = 0, slpflag, slptimeo;
1042
1043 if (vp->v_flag & VXLOCK) {
1044 return (0);
1045 }
1046
1047 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1048 intrflg = 0;
1049 if (intrflg) {
1050 slpflag = PCATCH;
1051 slptimeo = 2 * hz;
1052 } else {
1053 slpflag = 0;
1054 slptimeo = 0;
1055 }
1056 /*
1057 * First wait for any other process doing a flush to complete.
1058 */
1059 while (np->n_flag & NFLUSHINPROG) {
1060 np->n_flag |= NFLUSHWANT;
1061 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
1062 slptimeo);
1063 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
1064 return (EINTR);
1065 }
1066
1067 /*
1068 * Now, flush as required.
1069 */
1070 np->n_flag |= NFLUSHINPROG;
1071 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
1072 while (error) {
1073 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
1074 np->n_flag &= ~NFLUSHINPROG;
1075 if (np->n_flag & NFLUSHWANT) {
1076 np->n_flag &= ~NFLUSHWANT;
1077 wakeup((caddr_t)&np->n_flag);
1078 }
1079 return (EINTR);
1080 }
1081 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
1082 }
1083 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
1084 if (np->n_flag & NFLUSHWANT) {
1085 np->n_flag &= ~NFLUSHWANT;
1086 wakeup((caddr_t)&np->n_flag);
1087 }
1088 return (0);
1089}
1090
1091/*
1092 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
1093 * This is mainly to avoid queueing async I/O requests when the nfsiods
1094 * are all hung on a dead server.
1095 *
1096 * Note: nfs_asyncio() does not clear (B_ERROR|B_INVAL) but when the bp
1097 * is eventually dequeued by the async daemon, nfs_doio() *will*.
1098 */
1099int
976 } else {
977 bdwrite(bp);
978 }
979 } while (uio->uio_resid > 0 && n > 0);
980 return (0);
981}
982
983/*
984 * Get an nfs cache block.
985 * Allocate a new one if the block isn't currently in the cache
986 * and return the block marked busy. If the calling process is
987 * interrupted by a signal for an interruptible mount point, return
988 * NULL.
989 */
990static struct buf *
991nfs_getcacheblk(vp, bn, size, p)
992 struct vnode *vp;
993 daddr_t bn;
994 int size;
995 struct proc *p;
996{
997 register struct buf *bp;
998 struct mount *mp;
999 struct nfsmount *nmp;
1000
1001 mp = vp->v_mount;
1002 nmp = VFSTONFS(mp);
1003
1004 if (nmp->nm_flag & NFSMNT_INT) {
1005 bp = getblk(vp, bn, size, PCATCH, 0);
1006 while (bp == (struct buf *)0) {
1007 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
1008 return ((struct buf *)0);
1009 bp = getblk(vp, bn, size, 0, 2 * hz);
1010 }
1011 } else {
1012 bp = getblk(vp, bn, size, 0, 0);
1013 }
1014
1015 if (vp->v_type == VREG) {
1016 int biosize;
1017
1018 biosize = mp->mnt_stat.f_iosize;
1019 bp->b_blkno = bn * (biosize / DEV_BSIZE);
1020 }
1021 return (bp);
1022}
1023
1024/*
1025 * Flush and invalidate all dirty buffers. If another process is already
1026 * doing the flush, just wait for completion.
1027 */
1028int
1029nfs_vinvalbuf(vp, flags, cred, p, intrflg)
1030 struct vnode *vp;
1031 int flags;
1032 struct ucred *cred;
1033 struct proc *p;
1034 int intrflg;
1035{
1036 register struct nfsnode *np = VTONFS(vp);
1037 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1038 int error = 0, slpflag, slptimeo;
1039
1040 if (vp->v_flag & VXLOCK) {
1041 return (0);
1042 }
1043
1044 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1045 intrflg = 0;
1046 if (intrflg) {
1047 slpflag = PCATCH;
1048 slptimeo = 2 * hz;
1049 } else {
1050 slpflag = 0;
1051 slptimeo = 0;
1052 }
1053 /*
1054 * First wait for any other process doing a flush to complete.
1055 */
1056 while (np->n_flag & NFLUSHINPROG) {
1057 np->n_flag |= NFLUSHWANT;
1058 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
1059 slptimeo);
1060 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
1061 return (EINTR);
1062 }
1063
1064 /*
1065 * Now, flush as required.
1066 */
1067 np->n_flag |= NFLUSHINPROG;
1068 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
1069 while (error) {
1070 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
1071 np->n_flag &= ~NFLUSHINPROG;
1072 if (np->n_flag & NFLUSHWANT) {
1073 np->n_flag &= ~NFLUSHWANT;
1074 wakeup((caddr_t)&np->n_flag);
1075 }
1076 return (EINTR);
1077 }
1078 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
1079 }
1080 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
1081 if (np->n_flag & NFLUSHWANT) {
1082 np->n_flag &= ~NFLUSHWANT;
1083 wakeup((caddr_t)&np->n_flag);
1084 }
1085 return (0);
1086}
1087
1088/*
1089 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
1090 * This is mainly to avoid queueing async I/O requests when the nfsiods
1091 * are all hung on a dead server.
1092 *
1093 * Note: nfs_asyncio() does not clear (B_ERROR|B_INVAL) but when the bp
1094 * is eventually dequeued by the async daemon, nfs_doio() *will*.
1095 */
1096int
1100nfs_asyncio(bp, cred)
1097nfs_asyncio(bp, cred, procp)
1101 register struct buf *bp;
1102 struct ucred *cred;
1098 register struct buf *bp;
1099 struct ucred *cred;
1100 struct proc *procp;
1103{
1104 struct nfsmount *nmp;
1105 int i;
1106 int gotiod;
1107 int slpflag = 0;
1108 int slptimeo = 0;
1109 int error;
1110
1111 if (nfs_numasync == 0)
1112 return (EIO);
1113
1114 nmp = VFSTONFS(bp->b_vp->v_mount);
1115again:
1116 if (nmp->nm_flag & NFSMNT_INT)
1117 slpflag = PCATCH;
1118 gotiod = FALSE;
1119
1120 /*
1121 * Find a free iod to process this request.
1122 */
1123 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1124 if (nfs_iodwant[i]) {
1125 /*
1126 * Found one, so wake it up and tell it which
1127 * mount to process.
1128 */
1129 NFS_DPF(ASYNCIO,
1130 ("nfs_asyncio: waking iod %d for mount %p\n",
1131 i, nmp));
1132 nfs_iodwant[i] = (struct proc *)0;
1133 nfs_iodmount[i] = nmp;
1134 nmp->nm_bufqiods++;
1135 wakeup((caddr_t)&nfs_iodwant[i]);
1136 gotiod = TRUE;
1137 break;
1138 }
1139
1140 /*
1141 * If none are free, we may already have an iod working on this mount
1142 * point. If so, it will process our request.
1143 */
1144 if (!gotiod) {
1145 if (nmp->nm_bufqiods > 0) {
1146 NFS_DPF(ASYNCIO,
1147 ("nfs_asyncio: %d iods are already processing mount %p\n",
1148 nmp->nm_bufqiods, nmp));
1149 gotiod = TRUE;
1150 }
1151 }
1152
1153 /*
1154 * If we have an iod which can process the request, then queue
1155 * the buffer.
1156 */
1157 if (gotiod) {
1158 /*
1159 * Ensure that the queue never grows too large.
1160 */
1161 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
1162 NFS_DPF(ASYNCIO,
1163 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
1164 nmp->nm_bufqwant = TRUE;
1165 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
1166 "nfsaio", slptimeo);
1167 if (error) {
1101{
1102 struct nfsmount *nmp;
1103 int i;
1104 int gotiod;
1105 int slpflag = 0;
1106 int slptimeo = 0;
1107 int error;
1108
1109 if (nfs_numasync == 0)
1110 return (EIO);
1111
1112 nmp = VFSTONFS(bp->b_vp->v_mount);
1113again:
1114 if (nmp->nm_flag & NFSMNT_INT)
1115 slpflag = PCATCH;
1116 gotiod = FALSE;
1117
1118 /*
1119 * Find a free iod to process this request.
1120 */
1121 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1122 if (nfs_iodwant[i]) {
1123 /*
1124 * Found one, so wake it up and tell it which
1125 * mount to process.
1126 */
1127 NFS_DPF(ASYNCIO,
1128 ("nfs_asyncio: waking iod %d for mount %p\n",
1129 i, nmp));
1130 nfs_iodwant[i] = (struct proc *)0;
1131 nfs_iodmount[i] = nmp;
1132 nmp->nm_bufqiods++;
1133 wakeup((caddr_t)&nfs_iodwant[i]);
1134 gotiod = TRUE;
1135 break;
1136 }
1137
1138 /*
1139 * If none are free, we may already have an iod working on this mount
1140 * point. If so, it will process our request.
1141 */
1142 if (!gotiod) {
1143 if (nmp->nm_bufqiods > 0) {
1144 NFS_DPF(ASYNCIO,
1145 ("nfs_asyncio: %d iods are already processing mount %p\n",
1146 nmp->nm_bufqiods, nmp));
1147 gotiod = TRUE;
1148 }
1149 }
1150
1151 /*
1152 * If we have an iod which can process the request, then queue
1153 * the buffer.
1154 */
1155 if (gotiod) {
1156 /*
1157 * Ensure that the queue never grows too large.
1158 */
1159 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
1160 NFS_DPF(ASYNCIO,
1161 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
1162 nmp->nm_bufqwant = TRUE;
1163 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
1164 "nfsaio", slptimeo);
1165 if (error) {
1168 if (nfs_sigintr(nmp, NULL, bp->b_proc))
1166 if (nfs_sigintr(nmp, NULL, procp))
1169 return (EINTR);
1170 if (slpflag == PCATCH) {
1171 slpflag = 0;
1172 slptimeo = 2 * hz;
1173 }
1174 }
1175 /*
1176 * We might have lost our iod while sleeping,
1177 * so check and loop if nescessary.
1178 */
1179 if (nmp->nm_bufqiods == 0) {
1180 NFS_DPF(ASYNCIO,
1181 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
1182 goto again;
1183 }
1184 }
1185
1186 if (bp->b_flags & B_READ) {
1187 if (bp->b_rcred == NOCRED && cred != NOCRED) {
1188 crhold(cred);
1189 bp->b_rcred = cred;
1190 }
1191 } else {
1192 bp->b_flags |= B_WRITEINPROG;
1193 if (bp->b_wcred == NOCRED && cred != NOCRED) {
1194 crhold(cred);
1195 bp->b_wcred = cred;
1196 }
1197 }
1198
1199 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1200 nmp->nm_bufqlen++;
1201 return (0);
1202 }
1203
1204 /*
1205 * All the iods are busy on other mounts, so return EIO to
1206 * force the caller to process the i/o synchronously.
1207 */
1208 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
1209 return (EIO);
1210}
1211
1212/*
1213 * Do an I/O operation to/from a cache block. This may be called
1214 * synchronously or from an nfsiod.
1215 */
1216int
1217nfs_doio(bp, cr, p)
1218 struct buf *bp;
1219 struct ucred *cr;
1220 struct proc *p;
1221{
1222 struct uio *uiop;
1223 struct vnode *vp;
1224 struct nfsnode *np;
1225 struct nfsmount *nmp;
1226 int error = 0, iomode, must_commit = 0;
1227 struct uio uio;
1228 struct iovec io;
1229
1230 vp = bp->b_vp;
1231 np = VTONFS(vp);
1232 nmp = VFSTONFS(vp->v_mount);
1233 uiop = &uio;
1234 uiop->uio_iov = &io;
1235 uiop->uio_iovcnt = 1;
1236 uiop->uio_segflg = UIO_SYSSPACE;
1237 uiop->uio_procp = p;
1238
1239 /*
1240 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1241 * do this here so we do not have to do it in all the code that
1242 * calls us.
1243 */
1244 bp->b_flags &= ~(B_ERROR | B_INVAL);
1245
1246 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
1247
1248 /*
1249 * Historically, paging was done with physio, but no more.
1250 */
1251 if (bp->b_flags & B_PHYS) {
1252 /*
1253 * ...though reading /dev/drum still gets us here.
1254 */
1255 io.iov_len = uiop->uio_resid = bp->b_bcount;
1256 /* mapping was done by vmapbuf() */
1257 io.iov_base = bp->b_data;
1258 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1259 if (bp->b_flags & B_READ) {
1260 uiop->uio_rw = UIO_READ;
1261 nfsstats.read_physios++;
1262 error = nfs_readrpc(vp, uiop, cr);
1263 } else {
1264 int com;
1265
1266 iomode = NFSV3WRITE_DATASYNC;
1267 uiop->uio_rw = UIO_WRITE;
1268 nfsstats.write_physios++;
1269 error = nfs_writerpc(vp, uiop, cr, &iomode, &com);
1270 }
1271 if (error) {
1272 bp->b_flags |= B_ERROR;
1273 bp->b_error = error;
1274 }
1275 } else if (bp->b_flags & B_READ) {
1276 io.iov_len = uiop->uio_resid = bp->b_bcount;
1277 io.iov_base = bp->b_data;
1278 uiop->uio_rw = UIO_READ;
1279 switch (vp->v_type) {
1280 case VREG:
1281 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1282 nfsstats.read_bios++;
1283 error = nfs_readrpc(vp, uiop, cr);
1284 if (!error) {
1285 if (uiop->uio_resid) {
1286 /*
1287 * If we had a short read with no error, we must have
1288 * hit a file hole. We should zero-fill the remainder.
1289 * This can also occur if the server hits the file EOF.
1290 *
1291 * Holes used to be able to occur due to pending
1292 * writes, but that is not possible any longer.
1293 */
1294 int nread = bp->b_bcount - uiop->uio_resid;
1295 int left = bp->b_bcount - nread;
1296
1297 if (left > 0)
1298 bzero((char *)bp->b_data + nread, left);
1299 uiop->uio_resid = 0;
1300 }
1301 }
1302 if (p && (vp->v_flag & VTEXT) &&
1303 (((nmp->nm_flag & NFSMNT_NQNFS) &&
1304 NQNFS_CKINVALID(vp, np, ND_READ) &&
1305 np->n_lrev != np->n_brev) ||
1306 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
1307 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) {
1308 uprintf("Process killed due to text file modification\n");
1309 psignal(p, SIGKILL);
1310 PHOLD(p);
1311 }
1312 break;
1313 case VLNK:
1314 uiop->uio_offset = (off_t)0;
1315 nfsstats.readlink_bios++;
1316 error = nfs_readlinkrpc(vp, uiop, cr);
1317 break;
1318 case VDIR:
1319 nfsstats.readdir_bios++;
1320 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
1321 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1322 error = nfs_readdirplusrpc(vp, uiop, cr);
1323 if (error == NFSERR_NOTSUPP)
1324 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1325 }
1326 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1327 error = nfs_readdirrpc(vp, uiop, cr);
1328 /*
1329 * end-of-directory sets B_INVAL but does not generate an
1330 * error.
1331 */
1332 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1333 bp->b_flags |= B_INVAL;
1334 break;
1335 default:
1336 printf("nfs_doio: type %x unexpected\n",vp->v_type);
1337 break;
1338 };
1339 if (error) {
1340 bp->b_flags |= B_ERROR;
1341 bp->b_error = error;
1342 }
1343 } else {
1344 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
1345 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
1346
1347 if (bp->b_dirtyend > bp->b_dirtyoff) {
1348 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1349 - bp->b_dirtyoff;
1350 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
1351 + bp->b_dirtyoff;
1352 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1353 uiop->uio_rw = UIO_WRITE;
1354 nfsstats.write_bios++;
1355
1356 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
1357 iomode = NFSV3WRITE_UNSTABLE;
1358 else
1359 iomode = NFSV3WRITE_FILESYNC;
1360
1361 bp->b_flags |= B_WRITEINPROG;
1362 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
1363 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1364 bp->b_flags |= B_NEEDCOMMIT;
1365 if (bp->b_dirtyoff == 0
1366 && bp->b_dirtyend == bp->b_bcount)
1367 bp->b_flags |= B_CLUSTEROK;
1368 } else {
1369 bp->b_flags &= ~B_NEEDCOMMIT;
1370 }
1371 bp->b_flags &= ~B_WRITEINPROG;
1372
1373 /*
1374 * For an interrupted write, the buffer is still valid
1375 * and the write hasn't been pushed to the server yet,
1376 * so we can't set B_ERROR and report the interruption
1377 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
1378 * is not relevant, so the rpc attempt is essentially
1379 * a noop. For the case of a V3 write rpc not being
1380 * committed to stable storage, the block is still
1381 * dirty and requires either a commit rpc or another
1382 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1383 * the block is reused. This is indicated by setting
1384 * the B_DELWRI and B_NEEDCOMMIT flags.
1385 *
1386 * If the buffer is marked B_PAGING, it does not reside on
1387 * the vp's paging queues so we cannot call bdirty(). The
1388 * bp in this case is not an NFS cache block so we should
1389 * be safe. XXX
1390 */
1391 if (error == EINTR
1392 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1393 int s;
1394
1395 s = splbio();
1396 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1397 if ((bp->b_flags & B_PAGING) == 0) {
1398 bdirty(bp);
1399 bp->b_flags &= ~B_DONE;
1400 }
1401 if ((bp->b_flags & B_ASYNC) == 0)
1402 bp->b_flags |= B_EINTR;
1403 splx(s);
1404 } else {
1405 if (error) {
1406 bp->b_flags |= B_ERROR;
1407 bp->b_error = np->n_error = error;
1408 np->n_flag |= NWRITEERR;
1409 }
1410 bp->b_dirtyoff = bp->b_dirtyend = 0;
1411 }
1412 } else {
1413 bp->b_resid = 0;
1414 biodone(bp);
1415 return (0);
1416 }
1417 }
1418 bp->b_resid = uiop->uio_resid;
1419 if (must_commit)
1420 nfs_clearcommit(vp->v_mount);
1421 biodone(bp);
1422 return (error);
1423}
1167 return (EINTR);
1168 if (slpflag == PCATCH) {
1169 slpflag = 0;
1170 slptimeo = 2 * hz;
1171 }
1172 }
1173 /*
1174 * We might have lost our iod while sleeping,
1175 * so check and loop if nescessary.
1176 */
1177 if (nmp->nm_bufqiods == 0) {
1178 NFS_DPF(ASYNCIO,
1179 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
1180 goto again;
1181 }
1182 }
1183
1184 if (bp->b_flags & B_READ) {
1185 if (bp->b_rcred == NOCRED && cred != NOCRED) {
1186 crhold(cred);
1187 bp->b_rcred = cred;
1188 }
1189 } else {
1190 bp->b_flags |= B_WRITEINPROG;
1191 if (bp->b_wcred == NOCRED && cred != NOCRED) {
1192 crhold(cred);
1193 bp->b_wcred = cred;
1194 }
1195 }
1196
1197 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1198 nmp->nm_bufqlen++;
1199 return (0);
1200 }
1201
1202 /*
1203 * All the iods are busy on other mounts, so return EIO to
1204 * force the caller to process the i/o synchronously.
1205 */
1206 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
1207 return (EIO);
1208}
1209
1210/*
1211 * Do an I/O operation to/from a cache block. This may be called
1212 * synchronously or from an nfsiod.
1213 */
1214int
1215nfs_doio(bp, cr, p)
1216 struct buf *bp;
1217 struct ucred *cr;
1218 struct proc *p;
1219{
1220 struct uio *uiop;
1221 struct vnode *vp;
1222 struct nfsnode *np;
1223 struct nfsmount *nmp;
1224 int error = 0, iomode, must_commit = 0;
1225 struct uio uio;
1226 struct iovec io;
1227
1228 vp = bp->b_vp;
1229 np = VTONFS(vp);
1230 nmp = VFSTONFS(vp->v_mount);
1231 uiop = &uio;
1232 uiop->uio_iov = &io;
1233 uiop->uio_iovcnt = 1;
1234 uiop->uio_segflg = UIO_SYSSPACE;
1235 uiop->uio_procp = p;
1236
1237 /*
1238 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1239 * do this here so we do not have to do it in all the code that
1240 * calls us.
1241 */
1242 bp->b_flags &= ~(B_ERROR | B_INVAL);
1243
1244 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
1245
1246 /*
1247 * Historically, paging was done with physio, but no more.
1248 */
1249 if (bp->b_flags & B_PHYS) {
1250 /*
1251 * ...though reading /dev/drum still gets us here.
1252 */
1253 io.iov_len = uiop->uio_resid = bp->b_bcount;
1254 /* mapping was done by vmapbuf() */
1255 io.iov_base = bp->b_data;
1256 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1257 if (bp->b_flags & B_READ) {
1258 uiop->uio_rw = UIO_READ;
1259 nfsstats.read_physios++;
1260 error = nfs_readrpc(vp, uiop, cr);
1261 } else {
1262 int com;
1263
1264 iomode = NFSV3WRITE_DATASYNC;
1265 uiop->uio_rw = UIO_WRITE;
1266 nfsstats.write_physios++;
1267 error = nfs_writerpc(vp, uiop, cr, &iomode, &com);
1268 }
1269 if (error) {
1270 bp->b_flags |= B_ERROR;
1271 bp->b_error = error;
1272 }
1273 } else if (bp->b_flags & B_READ) {
1274 io.iov_len = uiop->uio_resid = bp->b_bcount;
1275 io.iov_base = bp->b_data;
1276 uiop->uio_rw = UIO_READ;
1277 switch (vp->v_type) {
1278 case VREG:
1279 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1280 nfsstats.read_bios++;
1281 error = nfs_readrpc(vp, uiop, cr);
1282 if (!error) {
1283 if (uiop->uio_resid) {
1284 /*
1285 * If we had a short read with no error, we must have
1286 * hit a file hole. We should zero-fill the remainder.
1287 * This can also occur if the server hits the file EOF.
1288 *
1289 * Holes used to be able to occur due to pending
1290 * writes, but that is not possible any longer.
1291 */
1292 int nread = bp->b_bcount - uiop->uio_resid;
1293 int left = bp->b_bcount - nread;
1294
1295 if (left > 0)
1296 bzero((char *)bp->b_data + nread, left);
1297 uiop->uio_resid = 0;
1298 }
1299 }
1300 if (p && (vp->v_flag & VTEXT) &&
1301 (((nmp->nm_flag & NFSMNT_NQNFS) &&
1302 NQNFS_CKINVALID(vp, np, ND_READ) &&
1303 np->n_lrev != np->n_brev) ||
1304 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
1305 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) {
1306 uprintf("Process killed due to text file modification\n");
1307 psignal(p, SIGKILL);
1308 PHOLD(p);
1309 }
1310 break;
1311 case VLNK:
1312 uiop->uio_offset = (off_t)0;
1313 nfsstats.readlink_bios++;
1314 error = nfs_readlinkrpc(vp, uiop, cr);
1315 break;
1316 case VDIR:
1317 nfsstats.readdir_bios++;
1318 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
1319 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1320 error = nfs_readdirplusrpc(vp, uiop, cr);
1321 if (error == NFSERR_NOTSUPP)
1322 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1323 }
1324 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1325 error = nfs_readdirrpc(vp, uiop, cr);
1326 /*
1327 * end-of-directory sets B_INVAL but does not generate an
1328 * error.
1329 */
1330 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1331 bp->b_flags |= B_INVAL;
1332 break;
1333 default:
1334 printf("nfs_doio: type %x unexpected\n",vp->v_type);
1335 break;
1336 };
1337 if (error) {
1338 bp->b_flags |= B_ERROR;
1339 bp->b_error = error;
1340 }
1341 } else {
1342 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
1343 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
1344
1345 if (bp->b_dirtyend > bp->b_dirtyoff) {
1346 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1347 - bp->b_dirtyoff;
1348 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
1349 + bp->b_dirtyoff;
1350 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1351 uiop->uio_rw = UIO_WRITE;
1352 nfsstats.write_bios++;
1353
1354 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
1355 iomode = NFSV3WRITE_UNSTABLE;
1356 else
1357 iomode = NFSV3WRITE_FILESYNC;
1358
1359 bp->b_flags |= B_WRITEINPROG;
1360 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
1361 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1362 bp->b_flags |= B_NEEDCOMMIT;
1363 if (bp->b_dirtyoff == 0
1364 && bp->b_dirtyend == bp->b_bcount)
1365 bp->b_flags |= B_CLUSTEROK;
1366 } else {
1367 bp->b_flags &= ~B_NEEDCOMMIT;
1368 }
1369 bp->b_flags &= ~B_WRITEINPROG;
1370
1371 /*
1372 * For an interrupted write, the buffer is still valid
1373 * and the write hasn't been pushed to the server yet,
1374 * so we can't set B_ERROR and report the interruption
1375 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
1376 * is not relevant, so the rpc attempt is essentially
1377 * a noop. For the case of a V3 write rpc not being
1378 * committed to stable storage, the block is still
1379 * dirty and requires either a commit rpc or another
1380 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1381 * the block is reused. This is indicated by setting
1382 * the B_DELWRI and B_NEEDCOMMIT flags.
1383 *
1384 * If the buffer is marked B_PAGING, it does not reside on
1385 * the vp's paging queues so we cannot call bdirty(). The
1386 * bp in this case is not an NFS cache block so we should
1387 * be safe. XXX
1388 */
1389 if (error == EINTR
1390 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1391 int s;
1392
1393 s = splbio();
1394 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1395 if ((bp->b_flags & B_PAGING) == 0) {
1396 bdirty(bp);
1397 bp->b_flags &= ~B_DONE;
1398 }
1399 if ((bp->b_flags & B_ASYNC) == 0)
1400 bp->b_flags |= B_EINTR;
1401 splx(s);
1402 } else {
1403 if (error) {
1404 bp->b_flags |= B_ERROR;
1405 bp->b_error = np->n_error = error;
1406 np->n_flag |= NWRITEERR;
1407 }
1408 bp->b_dirtyoff = bp->b_dirtyend = 0;
1409 }
1410 } else {
1411 bp->b_resid = 0;
1412 biodone(bp);
1413 return (0);
1414 }
1415 }
1416 bp->b_resid = uiop->uio_resid;
1417 if (must_commit)
1418 nfs_clearcommit(vp->v_mount);
1419 biodone(bp);
1420 return (error);
1421}