NFS - implement async state machine for nfs_readrpc_bio()
[dragonfly.git] / sys / vfs / nfs / nfs_bio.c
CommitLineData
984263bc
MD
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
79e5012e 37 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_bio.c,v 1.130 2004/04/14 23:23:55 peadar Exp $
a1a9228a 38 * $DragonFly: src/sys/vfs/nfs/nfs_bio.c,v 1.45 2008/07/18 00:09:39 dillon Exp $
984263bc
MD
39 */
40
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/resourcevar.h>
45#include <sys/signalvar.h>
46#include <sys/proc.h>
47#include <sys/buf.h>
48#include <sys/vnode.h>
49#include <sys/mount.h>
50#include <sys/kernel.h>
edb90c22 51#include <sys/mbuf.h>
ce71fd38 52#include <sys/msfbuf.h>
984263bc
MD
53
54#include <vm/vm.h>
55#include <vm/vm_extern.h>
56#include <vm/vm_page.h>
57#include <vm/vm_object.h>
58#include <vm/vm_pager.h>
59#include <vm/vnode_pager.h>
60
edb90c22 61#include <sys/buf2.h>
165dba55
DR
62#include <sys/thread2.h>
63
1f2de5d4
MD
64#include "rpcv2.h"
65#include "nfsproto.h"
66#include "nfs.h"
67#include "nfsmount.h"
1f2de5d4 68#include "nfsnode.h"
edb90c22
MD
69#include "xdr_subs.h"
70#include "nfsm_subs.h"
71
984263bc 72
54078292
MD
73static struct buf *nfs_getcacheblk(struct vnode *vp, off_t loffset,
74 int size, struct thread *td);
b66959e2 75static int nfs_check_dirent(struct nfs_dirent *dp, int maxlen);
ae8e83e6 76static void nfsiodone_sync(struct bio *bio);
984263bc 77
984263bc
MD
78/*
79 * Vnode op for VM getpages.
e851b29e
CP
80 *
81 * nfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
82 * int a_reqpage, vm_ooffset_t a_offset)
984263bc
MD
83 */
84int
e851b29e 85nfs_getpages(struct vop_getpages_args *ap)
984263bc 86{
dadab5e9 87 struct thread *td = curthread; /* XXX */
984263bc
MD
88 int i, error, nextoff, size, toff, count, npages;
89 struct uio uio;
90 struct iovec iov;
6f901429 91 char *kva;
984263bc 92 struct vnode *vp;
984263bc
MD
93 struct nfsmount *nmp;
94 vm_page_t *pages;
8d429613 95 vm_page_t m;
ce71fd38 96 struct msf_buf *msf;
984263bc
MD
97
98 vp = ap->a_vp;
984263bc
MD
99 nmp = VFSTONFS(vp->v_mount);
100 pages = ap->a_m;
101 count = ap->a_count;
102
103 if (vp->v_object == NULL) {
086c1d7e 104 kprintf("nfs_getpages: called with non-merged cache vnode??\n");
984263bc
MD
105 return VM_PAGER_ERROR;
106 }
107
108 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
109 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
3b568787 110 (void)nfs_fsinfo(nmp, vp, td);
984263bc
MD
111
112 npages = btoc(count);
113
114 /*
8d429613
MD
115 * NOTE that partially valid pages may occur in cases other
116 * then file EOF, such as when a file is partially written and
117 * ftruncate()-extended to a larger size. It is also possible
118 * for the valid bits to be set on garbage beyond the file EOF and
119 * clear in the area before EOF (e.g. m->valid == 0xfc), which can
120 * occur due to vtruncbuf() and the buffer cache's handling of
121 * pages which 'straddle' buffers or when b_bufsize is not a
122 * multiple of PAGE_SIZE.... the buffer cache cannot normally
123 * clear the extra bits. This kind of situation occurs when you
124 * make a small write() (m->valid == 0x03) and then mmap() and
125 * fault in the buffer(m->valid = 0xFF). When NFS flushes the
126 * buffer (vinvalbuf() m->valid = 0xFC) we are left with a mess.
127 *
128 * This is combined with the possibility that the pages are partially
129 * dirty or that there is a buffer backing the pages that is dirty
130 * (even if m->dirty is 0).
131 *
132 * To solve this problem several hacks have been made: (1) NFS
133 * guarentees that the IO block size is a multiple of PAGE_SIZE and
134 * (2) The buffer cache, when invalidating an NFS buffer, will
135 * disregard the buffer's fragmentory b_bufsize and invalidate
136 * the whole page rather then just the piece the buffer owns.
137 *
138 * This allows us to assume that a partially valid page found here
139 * is fully valid (vm_fault will zero'd out areas of the page not
140 * marked as valid).
984263bc 141 */
8d429613
MD
142 m = pages[ap->a_reqpage];
143 if (m->valid != 0) {
144 for (i = 0; i < npages; ++i) {
145 if (i != ap->a_reqpage)
146 vnode_pager_freepage(pages[i]);
984263bc 147 }
8d429613 148 return(0);
984263bc
MD
149 }
150
151 /*
ce71fd38 152 * Use an MSF_BUF as a medium to retrieve data from the pages.
984263bc 153 */
a5d36a1d
MD
154 msf_map_pagelist(&msf, pages, npages, 0);
155 KKASSERT(msf);
ce71fd38 156 kva = msf_buf_kva(msf);
984263bc 157
6f901429 158 iov.iov_base = kva;
984263bc
MD
159 iov.iov_len = count;
160 uio.uio_iov = &iov;
161 uio.uio_iovcnt = 1;
162 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
163 uio.uio_resid = count;
164 uio.uio_segflg = UIO_SYSSPACE;
165 uio.uio_rw = UIO_READ;
dadab5e9 166 uio.uio_td = td;
984263bc 167
edb90c22 168 error = nfs_readrpc_uio(vp, &uio);
ce71fd38 169 msf_buf_free(msf);
984263bc
MD
170
171 if (error && (uio.uio_resid == count)) {
086c1d7e 172 kprintf("nfs_getpages: error %d\n", error);
984263bc
MD
173 for (i = 0; i < npages; ++i) {
174 if (i != ap->a_reqpage)
175 vnode_pager_freepage(pages[i]);
176 }
177 return VM_PAGER_ERROR;
178 }
179
180 /*
181 * Calculate the number of bytes read and validate only that number
182 * of bytes. Note that due to pending writes, size may be 0. This
183 * does not mean that the remaining data is invalid!
184 */
185
186 size = count - uio.uio_resid;
187
188 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
984263bc
MD
189 nextoff = toff + PAGE_SIZE;
190 m = pages[i];
191
192 m->flags &= ~PG_ZERO;
193
194 if (nextoff <= size) {
195 /*
196 * Read operation filled an entire page
197 */
198 m->valid = VM_PAGE_BITS_ALL;
199 vm_page_undirty(m);
200 } else if (size > toff) {
201 /*
202 * Read operation filled a partial page.
203 */
204 m->valid = 0;
205 vm_page_set_validclean(m, 0, size - toff);
206 /* handled by vm_fault now */
207 /* vm_page_zero_invalid(m, TRUE); */
208 } else {
209 /*
210 * Read operation was short. If no error occured
211 * we may have hit a zero-fill section. We simply
212 * leave valid set to 0.
213 */
214 ;
215 }
216 if (i != ap->a_reqpage) {
217 /*
218 * Whether or not to leave the page activated is up in
219 * the air, but we should put the page on a page queue
220 * somewhere (it already is in the object). Result:
221 * It appears that emperical results show that
222 * deactivating pages is best.
223 */
224
225 /*
226 * Just in case someone was asking for this page we
227 * now tell them that it is ok to use.
228 */
229 if (!error) {
230 if (m->flags & PG_WANTED)
231 vm_page_activate(m);
232 else
233 vm_page_deactivate(m);
234 vm_page_wakeup(m);
235 } else {
236 vnode_pager_freepage(m);
237 }
238 }
239 }
240 return 0;
241}
242
243/*
244 * Vnode op for VM putpages.
e851b29e
CP
245 *
246 * nfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count, int a_sync,
247 * int *a_rtvals, vm_ooffset_t a_offset)
984263bc
MD
248 */
249int
e851b29e 250nfs_putpages(struct vop_putpages_args *ap)
984263bc 251{
dadab5e9 252 struct thread *td = curthread;
984263bc
MD
253 struct uio uio;
254 struct iovec iov;
6f901429 255 char *kva;
984263bc
MD
256 int iomode, must_commit, i, error, npages, count;
257 off_t offset;
258 int *rtvals;
259 struct vnode *vp;
984263bc
MD
260 struct nfsmount *nmp;
261 struct nfsnode *np;
262 vm_page_t *pages;
ce71fd38 263 struct msf_buf *msf;
984263bc
MD
264
265 vp = ap->a_vp;
266 np = VTONFS(vp);
984263bc
MD
267 nmp = VFSTONFS(vp->v_mount);
268 pages = ap->a_m;
269 count = ap->a_count;
270 rtvals = ap->a_rtvals;
271 npages = btoc(count);
272 offset = IDX_TO_OFF(pages[0]->pindex);
273
274 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
275 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
3b568787 276 (void)nfs_fsinfo(nmp, vp, td);
984263bc
MD
277
278 for (i = 0; i < npages; i++) {
279 rtvals[i] = VM_PAGER_AGAIN;
280 }
281
282 /*
283 * When putting pages, do not extend file past EOF.
284 */
285
286 if (offset + count > np->n_size) {
287 count = np->n_size - offset;
288 if (count < 0)
289 count = 0;
290 }
291
292 /*
ce71fd38 293 * Use an MSF_BUF as a medium to retrieve data from the pages.
984263bc 294 */
a5d36a1d
MD
295 msf_map_pagelist(&msf, pages, npages, 0);
296 KKASSERT(msf);
ce71fd38 297 kva = msf_buf_kva(msf);
984263bc 298
6f901429 299 iov.iov_base = kva;
984263bc
MD
300 iov.iov_len = count;
301 uio.uio_iov = &iov;
302 uio.uio_iovcnt = 1;
303 uio.uio_offset = offset;
304 uio.uio_resid = count;
305 uio.uio_segflg = UIO_SYSSPACE;
306 uio.uio_rw = UIO_WRITE;
dadab5e9 307 uio.uio_td = td;
984263bc
MD
308
309 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
310 iomode = NFSV3WRITE_UNSTABLE;
311 else
312 iomode = NFSV3WRITE_FILESYNC;
313
3b568787 314 error = nfs_writerpc(vp, &uio, &iomode, &must_commit);
984263bc 315
ce71fd38 316 msf_buf_free(msf);
984263bc
MD
317
318 if (!error) {
319 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
320 for (i = 0; i < nwritten; i++) {
321 rtvals[i] = VM_PAGER_OK;
322 vm_page_undirty(pages[i]);
323 }
324 if (must_commit)
325 nfs_clearcommit(vp->v_mount);
326 }
327 return rtvals[0];
328}
329
330/*
331 * Vnode op for read using bio
332 */
333int
3b568787 334nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag)
984263bc 335{
dadab5e9
MD
336 struct nfsnode *np = VTONFS(vp);
337 int biosize, i;
984263bc
MD
338 struct buf *bp = 0, *rabp;
339 struct vattr vattr;
dadab5e9 340 struct thread *td;
984263bc
MD
341 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
342 daddr_t lbn, rabn;
54078292
MD
343 off_t raoffset;
344 off_t loffset;
984263bc
MD
345 int bcount;
346 int seqcount;
347 int nra, error = 0, n = 0, on = 0;
348
349#ifdef DIAGNOSTIC
350 if (uio->uio_rw != UIO_READ)
351 panic("nfs_read mode");
352#endif
353 if (uio->uio_resid == 0)
354 return (0);
355 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
356 return (EINVAL);
dadab5e9 357 td = uio->uio_td;
984263bc
MD
358
359 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
360 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
3b568787 361 (void)nfs_fsinfo(nmp, vp, td);
984263bc
MD
362 if (vp->v_type != VDIR &&
363 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
364 return (EFBIG);
365 biosize = vp->v_mount->mnt_stat.f_iosize;
366 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
5a9187cb 367
984263bc
MD
368 /*
369 * For nfs, cache consistency can only be maintained approximately.
370 * Although RFC1094 does not specify the criteria, the following is
371 * believed to be compatible with the reference port.
5a9187cb 372 *
5a9187cb
MD
373 * NFS: If local changes have been made and this is a
374 * directory, the directory must be invalidated and
375 * the attribute cache must be cleared.
376 *
377 * GETATTR is called to synchronize the file size.
378 *
379 * If remote changes are detected local data is flushed
380 * and the cache is invalidated.
381 *
5a9187cb
MD
382 * NOTE: In the normal case the attribute cache is not
383 * cleared which means GETATTR may use cached data and
384 * not immediately detect changes made on the server.
984263bc 385 */
e07fef60
MD
386 if ((np->n_flag & NLMODIFIED) && vp->v_type == VDIR) {
387 nfs_invaldir(vp);
87de5057 388 error = nfs_vinvalbuf(vp, V_SAVE, 1);
e07fef60
MD
389 if (error)
390 return (error);
391 np->n_attrstamp = 0;
392 }
87de5057 393 error = VOP_GETATTR(vp, &vattr);
e07fef60
MD
394 if (error)
395 return (error);
396 if (np->n_flag & NRMODIFIED) {
397 if (vp->v_type == VDIR)
5a9187cb 398 nfs_invaldir(vp);
87de5057 399 error = nfs_vinvalbuf(vp, V_SAVE, 1);
5a9187cb
MD
400 if (error)
401 return (error);
e07fef60 402 np->n_flag &= ~NRMODIFIED;
984263bc
MD
403 }
404 do {
e07fef60 405 if (np->n_flag & NDONTCACHE) {
984263bc
MD
406 switch (vp->v_type) {
407 case VREG:
edb90c22 408 return (nfs_readrpc_uio(vp, uio));
984263bc 409 case VLNK:
3b568787 410 return (nfs_readlinkrpc(vp, uio));
984263bc
MD
411 case VDIR:
412 break;
413 default:
086c1d7e 414 kprintf(" NDONTCACHE: type %x unexpected\n", vp->v_type);
e07fef60 415 break;
984263bc
MD
416 };
417 }
418 switch (vp->v_type) {
419 case VREG:
420 nfsstats.biocache_reads++;
421 lbn = uio->uio_offset / biosize;
422 on = uio->uio_offset & (biosize - 1);
54078292 423 loffset = (off_t)lbn * biosize;
984263bc
MD
424
425 /*
426 * Start the read ahead(s), as required.
427 */
edb90c22 428 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp)) {
984263bc
MD
429 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
430 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
431 rabn = lbn + 1 + nra;
54078292 432 raoffset = (off_t)rabn * biosize;
b1c20cfa 433 if (findblk(vp, raoffset, FINDBLK_TEST) == NULL) {
54078292 434 rabp = nfs_getcacheblk(vp, raoffset, biosize, td);
984263bc
MD
435 if (!rabp)
436 return (EINTR);
437 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
10f3fee5
MD
438 rabp->b_cmd = BUF_CMD_READ;
439 vfs_busy_pages(vp, rabp);
edb90c22
MD
440 nfs_asyncio(vp, &rabp->b_bio2);
441#if 0
442 if (nfs_startio(vp, &rabp->b_bio2, td)) {
984263bc
MD
443 rabp->b_flags |= B_INVAL|B_ERROR;
444 vfs_unbusy_pages(rabp);
445 brelse(rabp);
446 break;
447 }
edb90c22 448#endif
984263bc
MD
449 } else {
450 brelse(rabp);
451 }
452 }
453 }
454 }
455
456 /*
457 * Obtain the buffer cache block. Figure out the buffer size
458 * when we are at EOF. If we are modifying the size of the
459 * buffer based on an EOF condition we need to hold
460 * nfs_rslock() through obtaining the buffer to prevent
461 * a potential writer-appender from messing with n_size.
462 * Otherwise we may accidently truncate the buffer and
463 * lose dirty data.
464 *
465 * Note that bcount is *not* DEV_BSIZE aligned.
466 */
467
468again:
469 bcount = biosize;
54078292 470 if (loffset >= np->n_size) {
984263bc 471 bcount = 0;
54078292
MD
472 } else if (loffset + biosize > np->n_size) {
473 bcount = np->n_size - loffset;
984263bc
MD
474 }
475 if (bcount != biosize) {
2313ec23 476 switch(nfs_rslock(np)) {
984263bc
MD
477 case ENOLCK:
478 goto again;
479 /* not reached */
480 case EINTR:
481 case ERESTART:
482 return(EINTR);
483 /* not reached */
484 default:
485 break;
486 }
487 }
488
54078292 489 bp = nfs_getcacheblk(vp, loffset, bcount, td);
984263bc
MD
490
491 if (bcount != biosize)
2313ec23 492 nfs_rsunlock(np);
984263bc
MD
493 if (!bp)
494 return (EINTR);
495
496 /*
497 * If B_CACHE is not set, we must issue the read. If this
498 * fails, we return an error.
499 */
500
501 if ((bp->b_flags & B_CACHE) == 0) {
10f3fee5 502 bp->b_cmd = BUF_CMD_READ;
ae8e83e6
MD
503 bp->b_bio2.bio_done = nfsiodone_sync;
504 bp->b_bio2.bio_flags |= BIO_SYNC;
10f3fee5 505 vfs_busy_pages(vp, bp);
edb90c22
MD
506 nfs_startio(vp, &bp->b_bio2, td);
507 error = nfs_iowait(&bp->b_bio2);
984263bc
MD
508 if (error) {
509 brelse(bp);
510 return (error);
511 }
512 }
513
514 /*
515 * on is the offset into the current bp. Figure out how many
516 * bytes we can copy out of the bp. Note that bcount is
517 * NOT DEV_BSIZE aligned.
518 *
519 * Then figure out how many bytes we can copy into the uio.
520 */
521
522 n = 0;
523 if (on < bcount)
524 n = min((unsigned)(bcount - on), uio->uio_resid);
525 break;
526 case VLNK:
ded0173f 527 biosize = min(NFS_MAXPATHLEN, np->n_size);
984263bc 528 nfsstats.biocache_readlinks++;
ded0173f 529 bp = nfs_getcacheblk(vp, (off_t)0, biosize, td);
81b5c339 530 if (bp == NULL)
984263bc
MD
531 return (EINTR);
532 if ((bp->b_flags & B_CACHE) == 0) {
10f3fee5 533 bp->b_cmd = BUF_CMD_READ;
ae8e83e6
MD
534 bp->b_bio2.bio_done = nfsiodone_sync;
535 bp->b_bio2.bio_flags |= BIO_SYNC;
10f3fee5 536 vfs_busy_pages(vp, bp);
edb90c22
MD
537 nfs_startio(vp, &bp->b_bio2, td);
538 error = nfs_iowait(&bp->b_bio2);
984263bc 539 if (error) {
a1a9228a 540 bp->b_flags |= B_ERROR | B_INVAL;
984263bc
MD
541 brelse(bp);
542 return (error);
543 }
544 }
ded0173f 545 n = min(uio->uio_resid, bp->b_bcount - bp->b_resid);
984263bc
MD
546 on = 0;
547 break;
548 case VDIR:
549 nfsstats.biocache_readdirs++;
550 if (np->n_direofoffset
551 && uio->uio_offset >= np->n_direofoffset) {
552 return (0);
553 }
554 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
555 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
54078292
MD
556 loffset = uio->uio_offset - on;
557 bp = nfs_getcacheblk(vp, loffset, NFS_DIRBLKSIZ, td);
81b5c339 558 if (bp == NULL)
984263bc 559 return (EINTR);
b66959e2 560
984263bc 561 if ((bp->b_flags & B_CACHE) == 0) {
10f3fee5 562 bp->b_cmd = BUF_CMD_READ;
ae8e83e6
MD
563 bp->b_bio2.bio_done = nfsiodone_sync;
564 bp->b_bio2.bio_flags |= BIO_SYNC;
10f3fee5 565 vfs_busy_pages(vp, bp);
edb90c22
MD
566 nfs_startio(vp, &bp->b_bio2, td);
567 error = nfs_iowait(&bp->b_bio2);
984263bc
MD
568 if (error) {
569 brelse(bp);
570 }
571 while (error == NFSERR_BAD_COOKIE) {
086c1d7e 572 kprintf("got bad cookie vp %p bp %p\n", vp, bp);
984263bc 573 nfs_invaldir(vp);
87de5057 574 error = nfs_vinvalbuf(vp, 0, 1);
984263bc
MD
575 /*
576 * Yuck! The directory has been modified on the
577 * server. The only way to get the block is by
578 * reading from the beginning to get all the
579 * offset cookies.
580 *
581 * Leave the last bp intact unless there is an error.
582 * Loop back up to the while if the error is another
583 * NFSERR_BAD_COOKIE (double yuch!).
584 */
585 for (i = 0; i <= lbn && !error; i++) {
586 if (np->n_direofoffset
587 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
588 return (0);
54078292
MD
589 bp = nfs_getcacheblk(vp, (off_t)i * NFS_DIRBLKSIZ,
590 NFS_DIRBLKSIZ, td);
984263bc
MD
591 if (!bp)
592 return (EINTR);
593 if ((bp->b_flags & B_CACHE) == 0) {
10f3fee5 594 bp->b_cmd = BUF_CMD_READ;
ae8e83e6
MD
595 bp->b_bio2.bio_done = nfsiodone_sync;
596 bp->b_bio2.bio_flags |= BIO_SYNC;
10f3fee5 597 vfs_busy_pages(vp, bp);
edb90c22
MD
598 nfs_startio(vp, &bp->b_bio2, td);
599 error = nfs_iowait(&bp->b_bio2);
984263bc
MD
600 /*
601 * no error + B_INVAL == directory EOF,
602 * use the block.
603 */
604 if (error == 0 && (bp->b_flags & B_INVAL))
605 break;
606 }
607 /*
608 * An error will throw away the block and the
609 * for loop will break out. If no error and this
610 * is not the block we want, we throw away the
611 * block and go for the next one via the for loop.
612 */
613 if (error || i < lbn)
614 brelse(bp);
615 }
616 }
617 /*
618 * The above while is repeated if we hit another cookie
619 * error. If we hit an error and it wasn't a cookie error,
620 * we give up.
621 */
622 if (error)
623 return (error);
624 }
625
626 /*
627 * If not eof and read aheads are enabled, start one.
628 * (You need the current block first, so that you have the
629 * directory offset cookie of the next block.)
630 */
edb90c22 631 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp) &&
984263bc
MD
632 (bp->b_flags & B_INVAL) == 0 &&
633 (np->n_direofoffset == 0 ||
54078292 634 loffset + NFS_DIRBLKSIZ < np->n_direofoffset) &&
e07fef60 635 (np->n_flag & NDONTCACHE) == 0 &&
b1c20cfa
MD
636 findblk(vp, loffset + NFS_DIRBLKSIZ, FINDBLK_TEST) == NULL
637 ) {
54078292
MD
638 rabp = nfs_getcacheblk(vp, loffset + NFS_DIRBLKSIZ,
639 NFS_DIRBLKSIZ, td);
984263bc
MD
640 if (rabp) {
641 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
10f3fee5
MD
642 rabp->b_cmd = BUF_CMD_READ;
643 vfs_busy_pages(vp, rabp);
edb90c22
MD
644 nfs_asyncio(vp, &rabp->b_bio2);
645#if 0
646 if (nfs_startio(vp, &rabp->b_bio2, td)) {
984263bc
MD
647 rabp->b_flags |= B_INVAL|B_ERROR;
648 vfs_unbusy_pages(rabp);
649 brelse(rabp);
650 }
edb90c22 651#endif
984263bc
MD
652 } else {
653 brelse(rabp);
654 }
655 }
656 }
657 /*
658 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
659 * chopped for the EOF condition, we cannot tell how large
660 * NFS directories are going to be until we hit EOF. So
661 * an NFS directory buffer is *not* chopped to its EOF. Now,
662 * it just so happens that b_resid will effectively chop it
663 * to EOF. *BUT* this information is lost if the buffer goes
664 * away and is reconstituted into a B_CACHE state ( due to
665 * being VMIO ) later. So we keep track of the directory eof
666 * in np->n_direofoffset and chop it off as an extra step
667 * right here.
668 */
669 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
670 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
671 n = np->n_direofoffset - uio->uio_offset;
672 break;
673 default:
086c1d7e 674 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
984263bc
MD
675 break;
676 };
677
984263bc
MD
678 switch (vp->v_type) {
679 case VREG:
01f31ab3
JS
680 if (n > 0)
681 error = uiomove(bp->b_data + on, (int)n, uio);
984263bc
MD
682 break;
683 case VLNK:
01f31ab3
JS
684 if (n > 0)
685 error = uiomove(bp->b_data + on, (int)n, uio);
984263bc
MD
686 n = 0;
687 break;
688 case VDIR:
01f31ab3
JS
689 if (n > 0) {
690 off_t old_off = uio->uio_offset;
691 caddr_t cpos, epos;
692 struct nfs_dirent *dp;
693
b66959e2
MD
694 /*
695 * We are casting cpos to nfs_dirent, it must be
696 * int-aligned.
697 */
698 if (on & 3) {
699 error = EINVAL;
700 break;
701 }
702
01f31ab3
JS
703 cpos = bp->b_data + on;
704 epos = bp->b_data + on + n;
705 while (cpos < epos && error == 0 && uio->uio_resid > 0) {
706 dp = (struct nfs_dirent *)cpos;
b66959e2
MD
707 error = nfs_check_dirent(dp, (int)(epos - cpos));
708 if (error)
709 break;
01f31ab3 710 if (vop_write_dirent(&error, uio, dp->nfs_ino,
b66959e2 711 dp->nfs_type, dp->nfs_namlen, dp->nfs_name)) {
01f31ab3 712 break;
b66959e2 713 }
01f31ab3
JS
714 cpos += dp->nfs_reclen;
715 }
716 n = 0;
717 if (error == 0)
718 uio->uio_offset = old_off + cpos - bp->b_data - on;
719 }
984263bc
MD
720 /*
721 * Invalidate buffer if caching is disabled, forcing a
722 * re-read from the remote later.
723 */
e07fef60 724 if (np->n_flag & NDONTCACHE)
984263bc
MD
725 bp->b_flags |= B_INVAL;
726 break;
727 default:
086c1d7e 728 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
984263bc
MD
729 }
730 brelse(bp);
731 } while (error == 0 && uio->uio_resid > 0 && n > 0);
732 return (error);
733}
734
b66959e2
MD
735/*
736 * Userland can supply any 'seek' offset when reading a NFS directory.
737 * Validate the structure so we don't panic the kernel. Note that
738 * the element name is nul terminated and the nul is not included
739 * in nfs_namlen.
740 */
741static
742int
743nfs_check_dirent(struct nfs_dirent *dp, int maxlen)
744{
745 int nfs_name_off = offsetof(struct nfs_dirent, nfs_name[0]);
746
747 if (nfs_name_off >= maxlen)
748 return (EINVAL);
749 if (dp->nfs_reclen < nfs_name_off || dp->nfs_reclen > maxlen)
750 return (EINVAL);
751 if (nfs_name_off + dp->nfs_namlen >= dp->nfs_reclen)
752 return (EINVAL);
753 if (dp->nfs_reclen & 3)
754 return (EINVAL);
755 return (0);
756}
757
984263bc
MD
758/*
759 * Vnode op for write using bio
e851b29e
CP
760 *
761 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
762 * struct ucred *a_cred)
984263bc
MD
763 */
764int
e851b29e 765nfs_write(struct vop_write_args *ap)
984263bc 766{
984263bc 767 struct uio *uio = ap->a_uio;
dadab5e9 768 struct thread *td = uio->uio_td;
984263bc
MD
769 struct vnode *vp = ap->a_vp;
770 struct nfsnode *np = VTONFS(vp);
984263bc
MD
771 int ioflag = ap->a_ioflag;
772 struct buf *bp;
773 struct vattr vattr;
774 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
775 daddr_t lbn;
54078292 776 off_t loffset;
984263bc
MD
777 int n, on, error = 0, iomode, must_commit;
778 int haverslock = 0;
81b5c339
MD
779 int bcount;
780 int biosize;
984263bc
MD
781
782#ifdef DIAGNOSTIC
783 if (uio->uio_rw != UIO_WRITE)
784 panic("nfs_write mode");
7b95be2a 785 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
984263bc
MD
786 panic("nfs_write proc");
787#endif
788 if (vp->v_type != VREG)
789 return (EIO);
790 if (np->n_flag & NWRITEERR) {
791 np->n_flag &= ~NWRITEERR;
792 return (np->n_error);
793 }
794 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
795 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
3b568787 796 (void)nfs_fsinfo(nmp, vp, td);
984263bc
MD
797
798 /*
799 * Synchronously flush pending buffers if we are in synchronous
800 * mode or if we are appending.
801 */
802 if (ioflag & (IO_APPEND | IO_SYNC)) {
5a9187cb 803 if (np->n_flag & NLMODIFIED) {
984263bc 804 np->n_attrstamp = 0;
5a9187cb 805 error = nfs_flush(vp, MNT_WAIT, td, 0);
87de5057 806 /* error = nfs_vinvalbuf(vp, V_SAVE, 1); */
984263bc
MD
807 if (error)
808 return (error);
809 }
810 }
811
812 /*
813 * If IO_APPEND then load uio_offset. We restart here if we cannot
814 * get the append lock.
815 */
816restart:
817 if (ioflag & IO_APPEND) {
818 np->n_attrstamp = 0;
87de5057 819 error = VOP_GETATTR(vp, &vattr);
984263bc
MD
820 if (error)
821 return (error);
822 uio->uio_offset = np->n_size;
823 }
824
825 if (uio->uio_offset < 0)
826 return (EINVAL);
827 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
828 return (EFBIG);
829 if (uio->uio_resid == 0)
830 return (0);
831
832 /*
833 * We need to obtain the rslock if we intend to modify np->n_size
834 * in order to guarentee the append point with multiple contending
835 * writers, to guarentee that no other appenders modify n_size
836 * while we are trying to obtain a truncated buffer (i.e. to avoid
837 * accidently truncating data written by another appender due to
838 * the race), and to ensure that the buffer is populated prior to
839 * our extending of the file. We hold rslock through the entire
840 * operation.
841 *
842 * Note that we do not synchronize the case where someone truncates
843 * the file while we are appending to it because attempting to lock
844 * this case may deadlock other parts of the system unexpectedly.
845 */
846 if ((ioflag & IO_APPEND) ||
847 uio->uio_offset + uio->uio_resid > np->n_size) {
2313ec23 848 switch(nfs_rslock(np)) {
984263bc
MD
849 case ENOLCK:
850 goto restart;
851 /* not reached */
852 case EINTR:
853 case ERESTART:
854 return(EINTR);
855 /* not reached */
856 default:
857 break;
858 }
859 haverslock = 1;
860 }
861
862 /*
863 * Maybe this should be above the vnode op call, but so long as
864 * file servers have no limits, i don't think it matters
865 */
dadab5e9
MD
866 if (td->td_proc && uio->uio_offset + uio->uio_resid >
867 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
7278a846 868 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
984263bc 869 if (haverslock)
2313ec23 870 nfs_rsunlock(np);
984263bc
MD
871 return (EFBIG);
872 }
873
874 biosize = vp->v_mount->mnt_stat.f_iosize;
875
876 do {
e07fef60 877 if ((np->n_flag & NDONTCACHE) && uio->uio_iovcnt == 1) {
984263bc 878 iomode = NFSV3WRITE_FILESYNC;
3b568787 879 error = nfs_writerpc(vp, uio, &iomode, &must_commit);
984263bc
MD
880 if (must_commit)
881 nfs_clearcommit(vp->v_mount);
882 break;
883 }
884 nfsstats.biocache_writes++;
885 lbn = uio->uio_offset / biosize;
886 on = uio->uio_offset & (biosize-1);
54078292 887 loffset = uio->uio_offset - on;
984263bc
MD
888 n = min((unsigned)(biosize - on), uio->uio_resid);
889again:
890 /*
891 * Handle direct append and file extension cases, calculate
892 * unaligned buffer size.
893 */
894
895 if (uio->uio_offset == np->n_size && n) {
896 /*
897 * Get the buffer (in its pre-append state to maintain
898 * B_CACHE if it was previously set). Resize the
899 * nfsnode after we have locked the buffer to prevent
900 * readers from reading garbage.
901 */
902 bcount = on;
54078292 903 bp = nfs_getcacheblk(vp, loffset, bcount, td);
984263bc
MD
904
905 if (bp != NULL) {
906 long save;
907
908 np->n_size = uio->uio_offset + n;
5a9187cb 909 np->n_flag |= NLMODIFIED;
984263bc
MD
910 vnode_pager_setsize(vp, np->n_size);
911
912 save = bp->b_flags & B_CACHE;
913 bcount += n;
914 allocbuf(bp, bcount);
915 bp->b_flags |= save;
916 }
917 } else {
918 /*
919 * Obtain the locked cache block first, and then
920 * adjust the file's size as appropriate.
921 */
922 bcount = on + n;
54078292
MD
923 if (loffset + bcount < np->n_size) {
924 if (loffset + biosize < np->n_size)
984263bc
MD
925 bcount = biosize;
926 else
54078292 927 bcount = np->n_size - loffset;
984263bc 928 }
54078292 929 bp = nfs_getcacheblk(vp, loffset, bcount, td);
984263bc
MD
930 if (uio->uio_offset + n > np->n_size) {
931 np->n_size = uio->uio_offset + n;
5a9187cb 932 np->n_flag |= NLMODIFIED;
984263bc
MD
933 vnode_pager_setsize(vp, np->n_size);
934 }
935 }
936
81b5c339 937 if (bp == NULL) {
984263bc
MD
938 error = EINTR;
939 break;
940 }
941
942 /*
943 * Issue a READ if B_CACHE is not set. In special-append
944 * mode, B_CACHE is based on the buffer prior to the write
945 * op and is typically set, avoiding the read. If a read
946 * is required in special append mode, the server will
947 * probably send us a short-read since we extended the file
948 * on our end, resulting in b_resid == 0 and, thusly,
949 * B_CACHE getting set.
950 *
951 * We can also avoid issuing the read if the write covers
952 * the entire buffer. We have to make sure the buffer state
953 * is reasonable in this case since we will not be initiating
954 * I/O. See the comments in kern/vfs_bio.c's getblk() for
955 * more information.
956 *
957 * B_CACHE may also be set due to the buffer being cached
958 * normally.
8aa7625b
MD
959 *
960 * When doing a UIO_NOCOPY write the buffer is not
961 * overwritten and we cannot just set B_CACHE unconditionally
962 * for full-block writes.
984263bc
MD
963 */
964
8aa7625b 965 if (on == 0 && n == bcount && uio->uio_segflg != UIO_NOCOPY) {
984263bc
MD
966 bp->b_flags |= B_CACHE;
967 bp->b_flags &= ~(B_ERROR | B_INVAL);
968 }
969
970 if ((bp->b_flags & B_CACHE) == 0) {
10f3fee5 971 bp->b_cmd = BUF_CMD_READ;
ae8e83e6
MD
972 bp->b_bio2.bio_done = nfsiodone_sync;
973 bp->b_bio2.bio_flags |= BIO_SYNC;
10f3fee5 974 vfs_busy_pages(vp, bp);
edb90c22
MD
975 nfs_startio(vp, &bp->b_bio2, td);
976 error = nfs_iowait(&bp->b_bio2);
984263bc
MD
977 if (error) {
978 brelse(bp);
979 break;
980 }
981 }
982 if (!bp) {
983 error = EINTR;
984 break;
985 }
5a9187cb 986 np->n_flag |= NLMODIFIED;
984263bc
MD
987
988 /*
989 * If dirtyend exceeds file size, chop it down. This should
990 * not normally occur but there is an append race where it
991 * might occur XXX, so we log it.
992 *
993 * If the chopping creates a reverse-indexed or degenerate
994 * situation with dirtyoff/end, we 0 both of them.
995 */
996
997 if (bp->b_dirtyend > bcount) {
086c1d7e 998 kprintf("NFS append race @%08llx:%d\n",
973c11b9 999 (long long)bp->b_bio2.bio_offset,
984263bc
MD
1000 bp->b_dirtyend - bcount);
1001 bp->b_dirtyend = bcount;
1002 }
1003
1004 if (bp->b_dirtyoff >= bp->b_dirtyend)
1005 bp->b_dirtyoff = bp->b_dirtyend = 0;
1006
1007 /*
1008 * If the new write will leave a contiguous dirty
1009 * area, just update the b_dirtyoff and b_dirtyend,
1010 * otherwise force a write rpc of the old dirty area.
1011 *
1012 * While it is possible to merge discontiguous writes due to
1013 * our having a B_CACHE buffer ( and thus valid read data
1014 * for the hole), we don't because it could lead to
1015 * significant cache coherency problems with multiple clients,
1016 * especially if locking is implemented later on.
1017 *
1018 * as an optimization we could theoretically maintain
1019 * a linked list of discontinuous areas, but we would still
1020 * have to commit them separately so there isn't much
1021 * advantage to it except perhaps a bit of asynchronization.
1022 */
1023
1024 if (bp->b_dirtyend > 0 &&
1025 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
62cfda27 1026 if (bwrite(bp) == EINTR) {
984263bc
MD
1027 error = EINTR;
1028 break;
1029 }
1030 goto again;
1031 }
1032
984263bc
MD
1033 error = uiomove((char *)bp->b_data + on, n, uio);
1034
1035 /*
1036 * Since this block is being modified, it must be written
1037 * again and not just committed. Since write clustering does
1038 * not work for the stage 1 data write, only the stage 2
1039 * commit rpc, we have to clear B_CLUSTEROK as well.
1040 */
1041 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1042
1043 if (error) {
1044 bp->b_flags |= B_ERROR;
1045 brelse(bp);
1046 break;
1047 }
1048
1049 /*
1050 * Only update dirtyoff/dirtyend if not a degenerate
1051 * condition.
1052 */
1053 if (n) {
1054 if (bp->b_dirtyend > 0) {
1055 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
1056 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
1057 } else {
1058 bp->b_dirtyoff = on;
1059 bp->b_dirtyend = on + n;
1060 }
1061 vfs_bio_set_validclean(bp, on, n);
1062 }
984263bc
MD
1063
1064 /*
1065 * If the lease is non-cachable or IO_SYNC do bwrite().
1066 *
1067 * IO_INVAL appears to be unused. The idea appears to be
1068 * to turn off caching in this case. Very odd. XXX
a482a28a
MD
1069 *
1070 * If nfs_async is set bawrite() will use an unstable write
1071 * (build dirty bufs on the server), so we might as well
1072 * push it out with bawrite(). If nfs_async is not set we
1073 * use bdwrite() to cache dirty bufs on the client.
984263bc 1074 */
e07fef60 1075 if ((np->n_flag & NDONTCACHE) || (ioflag & IO_SYNC)) {
984263bc
MD
1076 if (ioflag & IO_INVAL)
1077 bp->b_flags |= B_NOCACHE;
62cfda27 1078 error = bwrite(bp);
984263bc
MD
1079 if (error)
1080 break;
e07fef60 1081 if (np->n_flag & NDONTCACHE) {
87de5057 1082 error = nfs_vinvalbuf(vp, V_SAVE, 1);
984263bc
MD
1083 if (error)
1084 break;
1085 }
a482a28a
MD
1086 } else if ((n + on) == biosize && nfs_async) {
1087 bawrite(bp);
984263bc
MD
1088 } else {
1089 bdwrite(bp);
1090 }
1091 } while (uio->uio_resid > 0 && n > 0);
1092
1093 if (haverslock)
2313ec23 1094 nfs_rsunlock(np);
984263bc
MD
1095
1096 return (error);
1097}
1098
1099/*
1100 * Get an nfs cache block.
1101 *
1102 * Allocate a new one if the block isn't currently in the cache
1103 * and return the block marked busy. If the calling process is
1104 * interrupted by a signal for an interruptible mount point, return
1105 * NULL.
1106 *
1107 * The caller must carefully deal with the possible B_INVAL state of
edb90c22 1108 * the buffer. nfs_startio() clears B_INVAL (and nfs_asyncio() clears it
984263bc
MD
1109 * indirectly), so synchronous reads can be issued without worrying about
1110 * the B_INVAL state. We have to be a little more careful when dealing
1111 * with writes (see comments in nfs_write()) when extending a file past
1112 * its EOF.
1113 */
1114static struct buf *
54078292 1115nfs_getcacheblk(struct vnode *vp, off_t loffset, int size, struct thread *td)
984263bc 1116{
40393ded 1117 struct buf *bp;
984263bc
MD
1118 struct mount *mp;
1119 struct nfsmount *nmp;
1120
1121 mp = vp->v_mount;
1122 nmp = VFSTONFS(mp);
1123
1124 if (nmp->nm_flag & NFSMNT_INT) {
4b958e7b 1125 bp = getblk(vp, loffset, size, GETBLK_PCATCH, 0);
81b5c339 1126 while (bp == NULL) {
60233e58 1127 if (nfs_sigintr(nmp, NULL, td))
81b5c339 1128 return (NULL);
54078292 1129 bp = getblk(vp, loffset, size, 0, 2 * hz);
984263bc
MD
1130 }
1131 } else {
54078292 1132 bp = getblk(vp, loffset, size, 0, 0);
984263bc
MD
1133 }
1134
81b5c339 1135 /*
54078292
MD
1136 * bio2, the 'device' layer. Since BIOs use 64 bit byte offsets
1137 * now, no translation is necessary.
81b5c339 1138 */
54078292 1139 bp->b_bio2.bio_offset = loffset;
984263bc
MD
1140 return (bp);
1141}
1142
1143/*
1144 * Flush and invalidate all dirty buffers. If another process is already
1145 * doing the flush, just wait for completion.
1146 */
1147int
87de5057 1148nfs_vinvalbuf(struct vnode *vp, int flags, int intrflg)
984263bc 1149{
40393ded 1150 struct nfsnode *np = VTONFS(vp);
984263bc
MD
1151 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1152 int error = 0, slpflag, slptimeo;
87de5057 1153 thread_t td = curthread;
984263bc 1154
5fd012e0 1155 if (vp->v_flag & VRECLAIMED)
984263bc 1156 return (0);
984263bc
MD
1157
1158 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1159 intrflg = 0;
1160 if (intrflg) {
1161 slpflag = PCATCH;
1162 slptimeo = 2 * hz;
1163 } else {
1164 slpflag = 0;
1165 slptimeo = 0;
1166 }
1167 /*
1168 * First wait for any other process doing a flush to complete.
1169 */
1170 while (np->n_flag & NFLUSHINPROG) {
1171 np->n_flag |= NFLUSHWANT;
377d4740 1172 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo);
87de5057 1173 if (error && intrflg && nfs_sigintr(nmp, NULL, td))
984263bc
MD
1174 return (EINTR);
1175 }
1176
1177 /*
1178 * Now, flush as required.
1179 */
1180 np->n_flag |= NFLUSHINPROG;
87de5057 1181 error = vinvalbuf(vp, flags, slpflag, 0);
984263bc 1182 while (error) {
87de5057 1183 if (intrflg && nfs_sigintr(nmp, NULL, td)) {
984263bc
MD
1184 np->n_flag &= ~NFLUSHINPROG;
1185 if (np->n_flag & NFLUSHWANT) {
1186 np->n_flag &= ~NFLUSHWANT;
1187 wakeup((caddr_t)&np->n_flag);
1188 }
1189 return (EINTR);
1190 }
87de5057 1191 error = vinvalbuf(vp, flags, 0, slptimeo);
984263bc 1192 }
5a9187cb 1193 np->n_flag &= ~(NLMODIFIED | NFLUSHINPROG);
984263bc
MD
1194 if (np->n_flag & NFLUSHWANT) {
1195 np->n_flag &= ~NFLUSHWANT;
1196 wakeup((caddr_t)&np->n_flag);
1197 }
1198 return (0);
1199}
1200
1201/*
edb90c22
MD
1202 * Return true (non-zero) if the txthread and rxthread are operational
1203 * and we do not already have too many not-yet-started BIO's built up.
984263bc
MD
1204 */
1205int
edb90c22
MD
1206nfs_asyncok(struct nfsmount *nmp)
1207{
1208 return (nmp->nm_bioqlen < 64 &&
1209 nmp->nm_rxstate <= NFSSVC_PENDING &&
1210 nmp->nm_txstate <= NFSSVC_PENDING);
1211}
1212
1213/*
1214 * The read-ahead code calls this to queue a bio to the txthread.
1215 *
1216 * We don't touch the bio otherwise... that is, we do not even
1217 * construct or send the initial rpc. The txthread will do it
1218 * for us.
1219 */
1220void
1221nfs_asyncio(struct vnode *vp, struct bio *bio)
984263bc 1222{
81b5c339 1223 struct buf *bp = bio->bio_buf;
edb90c22 1224 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
984263bc 1225
81b5c339 1226 KKASSERT(vp->v_tag == VT_NFS);
52e1cf57 1227 BUF_KERNPROC(bp);
52e1cf57
MD
1228 bio->bio_driver_info = vp;
1229 TAILQ_INSERT_TAIL(&nmp->nm_bioq, bio, bio_act);
1230 nmp->nm_bioqlen++;
1231 nfssvc_iod_writer_wakeup(nmp);
984263bc
MD
1232}
1233
1234/*
edb90c22
MD
1235 * Initiate an I/O operation to/from a cache block. If the BIO is
1236 * flagged BIO_SYNC, or if the async thread is not running, the
1237 * operation will be executed synchronously.
1238 *
1239 * Typically for BIO_SYNC the caller set up the completion and will
1240 * call nfs_iowait() to obtain the error code, then brelse().
1241 * iowait is a degenerate routine.
dadab5e9 1242 *
edb90c22
MD
1243 * For async operation we set up a request and queue it the transmit
1244 * thread along with a done function to deal with cleanup after
1245 * the RPC completes. The presence of a done function causes the
1246 * state machine to automatically move the req onto the reqrxq when
1247 * a reponse is received.
ae8e83e6 1248 *
dadab5e9 1249 * NOTE! TD MIGHT BE NULL
984263bc 1250 */
edb90c22
MD
1251void
1252nfs_startio(struct vnode *vp, struct bio *bio, struct thread *td)
984263bc 1253{
81b5c339 1254 struct buf *bp = bio->bio_buf;
984263bc 1255 struct uio *uiop;
984263bc
MD
1256 struct nfsnode *np;
1257 struct nfsmount *nmp;
1258 int error = 0, iomode, must_commit = 0;
1259 struct uio uio;
1260 struct iovec io;
1261
81b5c339 1262 KKASSERT(vp->v_tag == VT_NFS);
984263bc
MD
1263 np = VTONFS(vp);
1264 nmp = VFSTONFS(vp->v_mount);
1265 uiop = &uio;
1266 uiop->uio_iov = &io;
1267 uiop->uio_iovcnt = 1;
1268 uiop->uio_segflg = UIO_SYSSPACE;
dadab5e9 1269 uiop->uio_td = td;
984263bc
MD
1270
1271 /*
1272 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1273 * do this here so we do not have to do it in all the code that
1274 * calls us.
edb90c22
MD
1275 *
1276 * NOTE: An EINPROGRESS response can be returned if the bio was
1277 * asynchronous.
984263bc
MD
1278 */
1279 bp->b_flags &= ~(B_ERROR | B_INVAL);
1280
10f3fee5
MD
1281 KASSERT(bp->b_cmd != BUF_CMD_DONE,
1282 ("nfs_doio: bp %p already marked done!", bp));
1283
1284 if (bp->b_cmd == BUF_CMD_READ) {
984263bc
MD
1285 io.iov_len = uiop->uio_resid = bp->b_bcount;
1286 io.iov_base = bp->b_data;
1287 uiop->uio_rw = UIO_READ;
1288
1289 switch (vp->v_type) {
1290 case VREG:
edb90c22
MD
1291 /*
1292 * Note: NFS assumes BIO_SYNC is run synchronously, so
1293 * be sure to do that.
1294 */
984263bc 1295 nfsstats.read_bios++;
edb90c22
MD
1296 if ((bio->bio_flags & BIO_SYNC) == 0) {
1297 nfs_readrpc_bio(vp, bio);
1298 return;
1299 }
1300 uiop->uio_offset = bio->bio_offset;
1301 error = nfs_readrpc_uio(vp, uiop);
1302 if (error == 0) {
984263bc
MD
1303 if (uiop->uio_resid) {
1304 /*
1305 * If we had a short read with no error, we must have
1306 * hit a file hole. We should zero-fill the remainder.
1307 * This can also occur if the server hits the file EOF.
1308 *
1309 * Holes used to be able to occur due to pending
1310 * writes, but that is not possible any longer.
1311 */
edb90c22
MD
1312 int nread = bp->b_bcount - bp->b_resid;
1313 int left = bp->b_resid;
984263bc
MD
1314
1315 if (left > 0)
1316 bzero((char *)bp->b_data + nread, left);
edb90c22 1317 bp->b_resid = 0;
984263bc
MD
1318 }
1319 }
dadab5e9 1320 if (td && td->td_proc && (vp->v_flag & VTEXT) &&
e07fef60 1321 np->n_mtime != np->n_vattr.va_mtime.tv_sec) {
984263bc 1322 uprintf("Process killed due to text file modification\n");
84204577 1323 ksignal(td->td_proc, SIGKILL);
984263bc
MD
1324 }
1325 break;
1326 case VLNK:
81b5c339 1327 uiop->uio_offset = 0;
984263bc 1328 nfsstats.readlink_bios++;
3b568787 1329 error = nfs_readlinkrpc(vp, uiop);
984263bc
MD
1330 break;
1331 case VDIR:
1332 nfsstats.readdir_bios++;
54078292 1333 uiop->uio_offset = bio->bio_offset;
984263bc 1334 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
3b568787 1335 error = nfs_readdirplusrpc(vp, uiop);
984263bc
MD
1336 if (error == NFSERR_NOTSUPP)
1337 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1338 }
1339 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
3b568787 1340 error = nfs_readdirrpc(vp, uiop);
984263bc
MD
1341 /*
1342 * end-of-directory sets B_INVAL but does not generate an
1343 * error.
1344 */
1345 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1346 bp->b_flags |= B_INVAL;
1347 break;
1348 default:
086c1d7e 1349 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
984263bc
MD
1350 break;
1351 };
1352 if (error) {
1353 bp->b_flags |= B_ERROR;
1354 bp->b_error = error;
1355 }
1356 } else {
1357 /*
1358 * If we only need to commit, try to commit
1359 */
10f3fee5 1360 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
984263bc
MD
1361 if (bp->b_flags & B_NEEDCOMMIT) {
1362 int retv;
1363 off_t off;
1364
54078292 1365 off = bio->bio_offset + bp->b_dirtyoff;
81b5c339 1366 retv = nfs_commit(vp, off,
3b568787 1367 bp->b_dirtyend - bp->b_dirtyoff, td);
984263bc
MD
1368 if (retv == 0) {
1369 bp->b_dirtyoff = bp->b_dirtyend = 0;
1370 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1371 bp->b_resid = 0;
81b5c339 1372 biodone(bio);
984263bc
MD
1373 }
1374 if (retv == NFSERR_STALEWRITEVERF) {
81b5c339 1375 nfs_clearcommit(vp->v_mount);
984263bc
MD
1376 }
1377 }
1378
1379 /*
1380 * Setup for actual write
1381 */
1382
54078292
MD
1383 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1384 bp->b_dirtyend = np->n_size - bio->bio_offset;
984263bc
MD
1385
1386 if (bp->b_dirtyend > bp->b_dirtyoff) {
1387 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1388 - bp->b_dirtyoff;
54078292 1389 uiop->uio_offset = bio->bio_offset + bp->b_dirtyoff;
984263bc
MD
1390 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1391 uiop->uio_rw = UIO_WRITE;
1392 nfsstats.write_bios++;
1393
ae8e83e6 1394 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
984263bc
MD
1395 iomode = NFSV3WRITE_UNSTABLE;
1396 else
1397 iomode = NFSV3WRITE_FILESYNC;
1398
3b568787 1399 error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
984263bc
MD
1400
1401 /*
1402 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1403 * to cluster the buffers needing commit. This will allow
1404 * the system to submit a single commit rpc for the whole
1405 * cluster. We can do this even if the buffer is not 100%
1406 * dirty (relative to the NFS blocksize), so we optimize the
1407 * append-to-file-case.
1408 *
1409 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1410 * cleared because write clustering only works for commit
1411 * rpc's, not for the data portion of the write).
1412 */
1413
1414 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1415 bp->b_flags |= B_NEEDCOMMIT;
1416 if (bp->b_dirtyoff == 0
1417 && bp->b_dirtyend == bp->b_bcount)
1418 bp->b_flags |= B_CLUSTEROK;
1419 } else {
1420 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1421 }
984263bc
MD
1422
1423 /*
1424 * For an interrupted write, the buffer is still valid
1425 * and the write hasn't been pushed to the server yet,
1426 * so we can't set B_ERROR and report the interruption
ae8e83e6 1427 * by setting B_EINTR. For the async case, B_EINTR
984263bc
MD
1428 * is not relevant, so the rpc attempt is essentially
1429 * a noop. For the case of a V3 write rpc not being
1430 * committed to stable storage, the block is still
1431 * dirty and requires either a commit rpc or another
1432 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1433 * the block is reused. This is indicated by setting
1434 * the B_DELWRI and B_NEEDCOMMIT flags.
1435 *
1436 * If the buffer is marked B_PAGING, it does not reside on
1437 * the vp's paging queues so we cannot call bdirty(). The
1438 * bp in this case is not an NFS cache block so we should
1439 * be safe. XXX
1440 */
1441 if (error == EINTR
1442 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
165dba55 1443 crit_enter();
984263bc 1444 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
10f3fee5 1445 if ((bp->b_flags & B_PAGING) == 0)
984263bc 1446 bdirty(bp);
ae8e83e6 1447 if (error)
984263bc 1448 bp->b_flags |= B_EINTR;
165dba55 1449 crit_exit();
984263bc
MD
1450 } else {
1451 if (error) {
1452 bp->b_flags |= B_ERROR;
1453 bp->b_error = np->n_error = error;
1454 np->n_flag |= NWRITEERR;
1455 }
1456 bp->b_dirtyoff = bp->b_dirtyend = 0;
1457 }
1458 } else {
1459 bp->b_resid = 0;
81b5c339 1460 biodone(bio);
984263bc
MD
1461 }
1462 }
1463 bp->b_resid = uiop->uio_resid;
1464 if (must_commit)
1465 nfs_clearcommit(vp->v_mount);
81b5c339 1466 biodone(bio);
984263bc
MD
1467}
1468
1469/*
1470 * Used to aid in handling ftruncate() operations on the NFS client side.
1471 * Truncation creates a number of special problems for NFS. We have to
1472 * throw away VM pages and buffer cache buffers that are beyond EOF, and
1473 * we have to properly handle VM pages or (potentially dirty) buffers
1474 * that straddle the truncation point.
1475 */
1476
1477int
3b568787 1478nfs_meta_setsize(struct vnode *vp, struct thread *td, u_quad_t nsize)
984263bc
MD
1479{
1480 struct nfsnode *np = VTONFS(vp);
1481 u_quad_t tsize = np->n_size;
1482 int biosize = vp->v_mount->mnt_stat.f_iosize;
1483 int error = 0;
1484
1485 np->n_size = nsize;
1486
1487 if (np->n_size < tsize) {
1488 struct buf *bp;
1489 daddr_t lbn;
54078292 1490 off_t loffset;
984263bc
MD
1491 int bufsize;
1492
1493 /*
1494 * vtruncbuf() doesn't get the buffer overlapping the
1495 * truncation point. We may have a B_DELWRI and/or B_CACHE
1496 * buffer that now needs to be truncated.
1497 */
87de5057 1498 error = vtruncbuf(vp, nsize, biosize);
984263bc
MD
1499 lbn = nsize / biosize;
1500 bufsize = nsize & (biosize - 1);
54078292
MD
1501 loffset = nsize - bufsize;
1502 bp = nfs_getcacheblk(vp, loffset, bufsize, td);
984263bc
MD
1503 if (bp->b_dirtyoff > bp->b_bcount)
1504 bp->b_dirtyoff = bp->b_bcount;
1505 if (bp->b_dirtyend > bp->b_bcount)
1506 bp->b_dirtyend = bp->b_bcount;
1507 bp->b_flags |= B_RELBUF; /* don't leave garbage around */
1508 brelse(bp);
1509 } else {
1510 vnode_pager_setsize(vp, nsize);
1511 }
1512 return(error);
1513}
1514
ae8e83e6
MD
1515/*
1516 * Synchronous completion for nfs_doio. Call bpdone() with elseit=FALSE.
1517 * Caller is responsible for brelse()'ing the bp.
1518 */
1519static void
1520nfsiodone_sync(struct bio *bio)
1521{
1522 bio->bio_flags = 0;
1523 bpdone(bio->bio_buf, 0);
1524}
edb90c22
MD
1525
1526/*
1527 * If nfs_startio() was told to do the request BIO_SYNC it will
1528 * complete the request before returning, so assert that the
1529 * request is in-fact complete.
1530 */
1531int
1532nfs_iowait(struct bio *bio)
1533{
1534 struct buf *bp = bio->bio_buf;
1535
1536 KKASSERT(bp->b_cmd == BUF_CMD_DONE);
1537 if (bp->b_flags & B_EINTR)
1538 return (EINTR);
1539 if (bp->b_flags & B_ERROR)
1540 return (bp->b_error ? bp->b_error : EIO);
1541 return (0);
1542}
1543
1544/*
1545 * nfs read rpc - BIO version
1546 */
1547static void nfs_readrpc_bio_done(nfsm_info_t info);
1548
1549void
1550nfs_readrpc_bio(struct vnode *vp, struct bio *bio)
1551{
1552 struct buf *bp = bio->bio_buf;
1553 u_int32_t *tl;
1554 struct nfsmount *nmp;
1555 int error = 0, len, tsiz;
1556 struct nfsm_info *info;
1557
1558 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1559 info->mrep = NULL;
1560 info->v3 = NFS_ISV3(vp);
1561
1562 nmp = VFSTONFS(vp->v_mount);
1563 tsiz = bp->b_bcount;
1564 if (bio->bio_offset + tsiz > nmp->nm_maxfilesize) {
1565 error = EFBIG;
1566 goto nfsmout;
1567 }
1568 nfsstats.rpccnt[NFSPROC_READ]++;
1569 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1570 nfsm_reqhead(info, vp, NFSPROC_READ,
1571 NFSX_FH(info->v3) + NFSX_UNSIGNED * 3);
1572 ERROROUT(nfsm_fhtom(info, vp));
1573 tl = nfsm_build(info, NFSX_UNSIGNED * 3);
1574 if (info->v3) {
1575 txdr_hyper(bio->bio_offset, tl);
1576 *(tl + 2) = txdr_unsigned(len);
1577 } else {
1578 *tl++ = txdr_unsigned(bio->bio_offset);
1579 *tl++ = txdr_unsigned(len);
1580 *tl = 0;
1581 }
1582 info->bio = bio;
1583 info->done = nfs_readrpc_bio_done;
1584 nfsm_request_bio(info, vp, NFSPROC_READ, NULL,
1585 nfs_vpcred(vp, ND_READ));
1586 return;
1587nfsmout:
1588 kfree(info, M_NFSREQ);
1589 bp->b_error = error;
1590 bp->b_flags |= B_ERROR;
1591 biodone(bio);
1592}
1593
1594static void
1595nfs_readrpc_bio_done(nfsm_info_t info)
1596{
1597 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1598 struct bio *bio = info->bio;
1599 struct buf *bp = bio->bio_buf;
1600 u_int32_t *tl;
1601 int attrflag;
1602 int retlen;
1603 int eof;
1604 int error = 0;
1605
1606 KKASSERT(info->state == NFSM_STATE_DONE);
1607
1608 if (info->v3) {
1609 ERROROUT(nfsm_postop_attr(info, info->vp, &attrflag,
1610 NFS_LATTR_NOSHRINK));
1611 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED));
1612 eof = fxdr_unsigned(int, *(tl + 1));
1613 } else {
1614 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1615 eof = 0;
1616 }
1617 NEGATIVEOUT(retlen = nfsm_strsiz(info, nmp->nm_rsize));
1618 ERROROUT(nfsm_mtobio(info, bio, retlen));
1619 m_freem(info->mrep);
1620 info->mrep = NULL;
1621
1622 /*
1623 * No error occured, fill the hole if any
1624 */
1625 if (retlen < bp->b_bcount) {
1626 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1627 }
1628 bp->b_resid = bp->b_bcount - retlen;
1629#if 0
1630 /* retlen */
1631 tsiz -= retlen;
1632 if (info.v3) {
1633 if (eof || retlen == 0) {
1634 tsiz = 0;
1635 }
1636 } else if (retlen < len) {
1637 tsiz = 0;
1638 }
1639#endif
1640nfsmout:
1641 if (error) {
1642 bp->b_error = error;
1643 bp->b_flags |= B_ERROR;
1644 }
1645 biodone(bio);
1646}
1647
1648#if 0
1649
1650/*
1651 * nfs write call - BIO version
1652 */
1653int
1654nfs_writerpc_bio(struct vnode *vp, struct bio *bio, int *iomode, int *must_commit)
1655{
1656 u_int32_t *tl;
1657 int32_t backup;
1658 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1659 int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1660 int committed = NFSV3WRITE_FILESYNC;
1661 struct nfsm_info info;
1662
1663 info.mrep = NULL;
1664 info.v3 = NFS_ISV3(vp);
1665
1666#ifndef DIAGNOSTIC
1667 if (uiop->uio_iovcnt != 1)
1668 panic("nfs: writerpc iovcnt > 1");
1669#endif
1670 *must_commit = 0;
1671 tsiz = uiop->uio_resid;
1672 if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1673 return (EFBIG);
1674 while (tsiz > 0) {
1675 nfsstats.rpccnt[NFSPROC_WRITE]++;
1676 len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1677 nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1678 NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1679 ERROROUT(nfsm_fhtom(&info, vp));
1680 if (info.v3) {
1681 tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1682 txdr_hyper(uiop->uio_offset, tl);
1683 tl += 2;
1684 *tl++ = txdr_unsigned(len);
1685 *tl++ = txdr_unsigned(*iomode);
1686 *tl = txdr_unsigned(len);
1687 } else {
1688 u_int32_t x;
1689
1690 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1691 /* Set both "begin" and "current" to non-garbage. */
1692 x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1693 *tl++ = x; /* "begin offset" */
1694 *tl++ = x; /* "current offset" */
1695 x = txdr_unsigned(len);
1696 *tl++ = x; /* total to this offset */
1697 *tl = x; /* size of this write */
1698 }
1699 ERROROUT(nfsm_uiotom(&info, uiop, len));
1700 NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1701 nfs_vpcred(vp, ND_WRITE), &error));
1702 if (info.v3) {
1703 /*
1704 * The write RPC returns a before and after mtime. The
1705 * nfsm_wcc_data() macro checks the before n_mtime
1706 * against the before time and stores the after time
1707 * in the nfsnode's cached vattr and n_mtime field.
1708 * The NRMODIFIED bit will be set if the before
1709 * time did not match the original mtime.
1710 */
1711 wccflag = NFSV3_WCCCHK;
1712 ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1713 if (error == 0) {
1714 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1715 rlen = fxdr_unsigned(int, *tl++);
1716 if (rlen == 0) {
1717 error = NFSERR_IO;
1718 m_freem(info.mrep);
1719 info.mrep = NULL;
1720 break;
1721 } else if (rlen < len) {
1722 backup = len - rlen;
1723 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1724 uiop->uio_iov->iov_len += backup;
1725 uiop->uio_offset -= backup;
1726 uiop->uio_resid += backup;
1727 len = rlen;
1728 }
1729 commit = fxdr_unsigned(int, *tl++);
1730
1731 /*
1732 * Return the lowest committment level
1733 * obtained by any of the RPCs.
1734 */
1735 if (committed == NFSV3WRITE_FILESYNC)
1736 committed = commit;
1737 else if (committed == NFSV3WRITE_DATASYNC &&
1738 commit == NFSV3WRITE_UNSTABLE)
1739 committed = commit;
1740 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1741 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1742 NFSX_V3WRITEVERF);
1743 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1744 } else if (bcmp((caddr_t)tl,
1745 (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1746 *must_commit = 1;
1747 bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1748 NFSX_V3WRITEVERF);
1749 }
1750 }
1751 } else {
1752 ERROROUT(nfsm_loadattr(&info, vp, NULL));
1753 }
1754 m_freem(info.mrep);
1755 info.mrep = NULL;
1756 if (error)
1757 break;
1758 tsiz -= len;
1759 }
1760nfsmout:
1761 if (vp->v_mount->mnt_flag & MNT_ASYNC)
1762 committed = NFSV3WRITE_FILESYNC;
1763 *iomode = committed;
1764 if (error)
1765 uiop->uio_resid = tsiz;
1766 return (error);
1767}
1768
1769#endif