Merge from vendor branch CVS:
[dragonfly.git] / sys / vfs / nwfs / nwfs_io.c
1 /*
2  * Copyright (c) 1999, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/nwfs/nwfs_io.c,v 1.6.2.1 2000/10/25 02:11:10 bp Exp $
33  * $DragonFly: src/sys/vfs/nwfs/nwfs_io.c,v 1.16 2005/06/06 15:09:38 drhodus Exp $
34  *
35  */
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/resourcevar.h>    /* defines plimit structure in proc struct */
39 #include <sys/kernel.h>
40 #include <sys/buf.h>
41 #include <sys/proc.h>
42 #include <sys/mount.h>
43 #include <sys/namei.h>
44 #include <sys/vnode.h>
45 #include <sys/dirent.h>
46 #include <sys/signalvar.h>
47 #include <sys/sysctl.h>
48
49 #include <vm/vm.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_pager.h>
54 #include <vm/vnode_pager.h>
55
56 #include <netproto/ncp/ncp.h>
57 #include <netproto/ncp/ncp_conn.h>
58 #include <netproto/ncp/ncp_subr.h>
59
60 #include <sys/thread2.h>
61
62 #include "nwfs.h"
63 #include "nwfs_node.h"
64 #include "nwfs_subr.h"
65
66 static int nwfs_fastlookup = 1;
67
68 SYSCTL_DECL(_vfs_nwfs);
69 SYSCTL_INT(_vfs_nwfs, OID_AUTO, fastlookup, CTLFLAG_RW, &nwfs_fastlookup, 0, "");
70
71
72 extern int nwfs_pbuf_freecnt;
73
74 #define DE_SIZE (sizeof(struct dirent))
75 #define NWFS_RWCACHE
76
77 static int
78 nwfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred)
79 {
80         struct nwmount *nmp = VTONWFS(vp);
81         int error, count, i;
82         struct dirent dp;
83         struct nwnode *np = VTONW(vp);
84         struct nw_entry_info fattr;
85         struct vnode *newvp;
86         ncpfid fid;
87
88         np = VTONW(vp);
89         NCPVNDEBUG("dirname='%s'\n",np->n_name);
90         if (uio->uio_resid < DE_SIZE || (uio->uio_offset < 0))
91                 return (EINVAL);
92         error = 0;
93         count = 0;
94         i = uio->uio_offset / DE_SIZE; /* offset in directory */
95         if (i == 0) {
96                 error = ncp_initsearch(vp, uio->uio_td, cred);
97                 if (error) {
98                         NCPVNDEBUG("cannot initialize search, error=%d",error);
99                         return( error );
100                 }
101         }
102
103         for (; uio->uio_resid >= DE_SIZE; i++) {
104                 bzero((char *) &dp, DE_SIZE);
105                 dp.d_reclen = DE_SIZE;
106                 switch (i) {
107                     case 0:             /* `.' */
108                     case 1:             /* `..' */
109                         dp.d_fileno = (i == 0) ? np->n_fid.f_id : np->n_parent.f_id;
110                         if (!dp.d_fileno) dp.d_fileno = NWFS_ROOT_INO;
111                         dp.d_namlen = i + 1;
112                         dp.d_name[0] = '.';
113                         dp.d_name[1] = '.';
114                         dp.d_name[i + 1] = '\0';
115                         dp.d_type = DT_DIR;
116                         break;
117                     default:
118                         error = ncp_search_for_file_or_subdir(nmp, &np->n_seq, &fattr, uio->uio_td, cred);
119                         if (error && error < 0x80) break;
120                         dp.d_fileno = fattr.dirEntNum;
121                         dp.d_type = (fattr.attributes & aDIR) ? DT_DIR : DT_REG;
122                         dp.d_namlen = fattr.nameLen;
123                         bcopy(fattr.entryName, dp.d_name, dp.d_namlen);
124                         dp.d_name[dp.d_namlen] = '\0';
125 #if 0
126                         if (error && eofflag) {
127                         /*      *eofflag = 1;*/
128                                 break;
129                         }
130 #endif
131                         break;
132                 }
133                 if (nwfs_fastlookup && !error && i > 1) {
134                         fid.f_id = fattr.dirEntNum;
135                         fid.f_parent = np->n_fid.f_id;
136                         error = nwfs_nget(vp->v_mount, fid, &fattr, vp, &newvp);
137                         if (!error) {
138                                 VTONW(newvp)->n_ctime = VTONW(newvp)->n_vattr.va_ctime.tv_sec;
139                                 vput(newvp);
140                         } else
141                                 error = 0;
142                 }
143                 if (error >= 0x80) {
144                     error = 0;
145                     break;
146                 }
147                 if ((error = uiomove((caddr_t)&dp, DE_SIZE, uio)))
148                         break;
149         }
150
151         uio->uio_offset = i * DE_SIZE;
152         return (error);
153 }
154
155 int
156 nwfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred)
157 {
158         struct nwmount *nmp = VFSTONWFS(vp->v_mount);
159         struct nwnode *np = VTONW(vp);
160         struct thread *td;
161         struct vattr vattr;
162         int error, biosize;
163
164         if (vp->v_type != VREG && vp->v_type != VDIR) {
165                 printf("%s: vn types other than VREG or VDIR are unsupported !\n",__func__);
166                 return EIO;
167         }
168         if (uiop->uio_resid == 0) return 0;
169         if (uiop->uio_offset < 0) return EINVAL;
170 /*      if (uiop->uio_offset + uiop->uio_resid > nmp->nm_maxfilesize)
171                 return (EFBIG);*/
172         td = uiop->uio_td;
173         if (vp->v_type == VDIR) {
174                 error = nwfs_readvdir(vp, uiop, cred);
175                 return error;
176         }
177         biosize = NWFSTOCONN(nmp)->buffer_size;
178         if (np->n_flag & NMODIFIED) {
179                 nwfs_attr_cacheremove(vp);
180                 error = VOP_GETATTR(vp, &vattr, td);
181                 if (error) return (error);
182                 np->n_mtime = vattr.va_mtime.tv_sec;
183         } else {
184                 error = VOP_GETATTR(vp, &vattr, td);
185                 if (error) return (error);
186                 if (np->n_mtime != vattr.va_mtime.tv_sec) {
187                         error = nwfs_vinvalbuf(vp, V_SAVE, td, 1);
188                         if (error) return (error);
189                         np->n_mtime = vattr.va_mtime.tv_sec;
190                 }
191         }
192         error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, uiop,cred);
193         return (error);
194 }
195
196 int
197 nwfs_writevnode(struct vnode *vp, struct uio *uiop, struct ucred *cred,
198                 int ioflag)
199 {
200         struct nwmount *nmp = VTONWFS(vp);
201         struct nwnode *np = VTONW(vp);
202         struct thread *td;
203 /*      struct vattr vattr;*/
204         int error = 0;
205
206         if (vp->v_type != VREG) {
207                 printf("%s: vn types other than VREG unsupported !\n",__func__);
208                 return EIO;
209         }
210         NCPVNDEBUG("ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
211         if (uiop->uio_offset < 0) return EINVAL;
212 /*      if (uiop->uio_offset + uiop->uio_resid > nmp->nm_maxfilesize)
213                 return (EFBIG);*/
214         td = uiop->uio_td;
215         if (ioflag & (IO_APPEND | IO_SYNC)) {
216                 if (np->n_flag & NMODIFIED) {
217                         nwfs_attr_cacheremove(vp);
218                         error = nwfs_vinvalbuf(vp, V_SAVE, td, 1);
219                         if (error) return (error);
220                 }
221                 if (ioflag & IO_APPEND) {
222                 /* We can relay only on local information about file size,
223                  * because until file is closed NetWare will not return
224                  * the correct size. */
225 #if notyet
226                         nwfs_attr_cacheremove(vp);
227                         error = VOP_GETATTR(vp, &vattr, td);
228                         if (error) return (error);
229 #endif
230                         uiop->uio_offset = np->n_size;
231                 }
232         }
233         if (uiop->uio_resid == 0) return 0;
234         if (td->td_proc && uiop->uio_offset + uiop->uio_resid > 
235             td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
236                 psignal(td->td_proc, SIGXFSZ);
237                 return (EFBIG);
238         }
239         error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, uiop, cred);
240         NCPVNDEBUG("after: ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
241         if (!error) {
242                 if (uiop->uio_offset > np->n_size) {
243                         np->n_vattr.va_size = np->n_size = uiop->uio_offset;
244                         vnode_pager_setsize(vp, np->n_size);
245                 }
246         }
247         return (error);
248 }
249
250 /*
251  * Do an I/O operation to/from a cache block.
252  */
253 int
254 nwfs_doio(struct buf *bp, struct ucred *cr, struct thread *td)
255 {
256         struct uio *uiop;
257         struct vnode *vp;
258         struct nwnode *np;
259         struct nwmount *nmp;
260         int error = 0;
261         struct uio uio;
262         struct iovec io;
263
264         vp = bp->b_vp;
265         np = VTONW(vp);
266         nmp = VFSTONWFS(vp->v_mount);
267         uiop = &uio;
268         uiop->uio_iov = &io;
269         uiop->uio_iovcnt = 1;
270         uiop->uio_segflg = UIO_SYSSPACE;
271         uiop->uio_td = td;
272         if (bp->b_flags & B_READ) {
273             io.iov_len = uiop->uio_resid = bp->b_bcount;
274             io.iov_base = bp->b_data;
275             uiop->uio_rw = UIO_READ;
276             switch (vp->v_type) {
277               case VREG:
278                 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
279                 error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, uiop, cr);
280                 if (error)
281                         break;
282                 if (uiop->uio_resid) {
283                         int left = uiop->uio_resid;
284                         int nread = bp->b_bcount - left;
285                         if (left > 0)
286                             bzero((char *)bp->b_data + nread, left);
287                 }
288                 break;
289 /*          case VDIR:
290                 nfsstats.readdir_bios++;
291                 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
292                 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
293                         error = nfs_readdirplusrpc(vp, uiop, cr);
294                         if (error == NFSERR_NOTSUPP)
295                                 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
296                 }
297                 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
298                         error = nfs_readdirrpc(vp, uiop, cr);
299                 if (error == 0 && uiop->uio_resid == bp->b_bcount)
300                         bp->b_flags |= B_INVAL;
301                 break;
302 */
303             default:
304                 printf("nwfs_doio:  type %x unexpected\n",vp->v_type);
305                 break;
306             };
307             if (error) {
308                 bp->b_flags |= B_ERROR;
309                 bp->b_error = error;
310             }
311         } else { /* write */
312             if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
313                 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
314
315             if (bp->b_dirtyend > bp->b_dirtyoff) {
316                 io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
317                 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
318                 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
319                 uiop->uio_rw = UIO_WRITE;
320                 error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, uiop, cr);
321
322                 /*
323                  * For an interrupted write, the buffer is still valid
324                  * and the write hasn't been pushed to the server yet,
325                  * so we can't set B_ERROR and report the interruption
326                  * by setting B_EINTR. For the B_ASYNC case, B_EINTR
327                  * is not relevant, so the rpc attempt is essentially
328                  * a noop.  For the case of a V3 write rpc not being
329                  * committed to stable storage, the block is still
330                  * dirty and requires either a commit rpc or another
331                  * write rpc with iomode == NFSV3WRITE_FILESYNC before
332                  * the block is reused. This is indicated by setting
333                  * the B_DELWRI and B_NEEDCOMMIT flags.
334                  */
335                 if (error == EINTR
336                     || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
337
338                         crit_enter();
339                         bp->b_flags &= ~(B_INVAL|B_NOCACHE);
340                         if ((bp->b_flags & B_ASYNC) == 0)
341                             bp->b_flags |= B_EINTR;
342                         if ((bp->b_flags & B_PAGING) == 0) {
343                             bdirty(bp);
344                             bp->b_flags &= ~B_DONE;
345                         }
346                         if ((bp->b_flags & B_ASYNC) == 0)
347                             bp->b_flags |= B_EINTR;
348                         crit_exit();
349                 } else {
350                         if (error) {
351                                 bp->b_flags |= B_ERROR;
352                                 bp->b_error /*= np->n_error */= error;
353 /*                              np->n_flag |= NWRITEERR;*/
354                         }
355                         bp->b_dirtyoff = bp->b_dirtyend = 0;
356                 }
357             } else {
358                 bp->b_resid = 0;
359                 biodone(bp);
360                 return (0);
361             }
362         }
363         bp->b_resid = uiop->uio_resid;
364         biodone(bp);
365         return (error);
366 }
367
368 /*
369  * Vnode op for VM getpages.
370  * Wish wish .... get rid from multiple IO routines
371  *
372  * nwfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
373  *               int a_reqpage, vm_ooffset_t a_offset)
374  */
375 int
376 nwfs_getpages(struct vop_getpages_args *ap)
377 {
378 #ifndef NWFS_RWCACHE
379         return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
380                 ap->a_reqpage);
381 #else
382         int i, error, nextoff, size, toff, npages, count;
383         struct uio uio;
384         struct iovec iov;
385         vm_offset_t kva;
386         struct buf *bp;
387         struct vnode *vp;
388         struct thread *td = curthread;  /* XXX */
389         struct ucred *cred;
390         struct nwmount *nmp;
391         struct nwnode *np;
392         vm_page_t *pages;
393
394         KKASSERT(td->td_proc);
395         cred = td->td_proc->p_ucred;
396
397         vp = ap->a_vp;
398         np = VTONW(vp);
399         nmp = VFSTONWFS(vp->v_mount);
400         pages = ap->a_m;
401         count = ap->a_count;
402
403         if (vp->v_object == NULL) {
404                 printf("nwfs_getpages: called with non-merged cache vnode??\n");
405                 return VM_PAGER_ERROR;
406         }
407
408         bp = getpbuf(&nwfs_pbuf_freecnt);
409         npages = btoc(count);
410         kva = (vm_offset_t) bp->b_data;
411         pmap_qenter(kva, pages, npages);
412
413         iov.iov_base = (caddr_t) kva;
414         iov.iov_len = count;
415         uio.uio_iov = &iov;
416         uio.uio_iovcnt = 1;
417         uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
418         uio.uio_resid = count;
419         uio.uio_segflg = UIO_SYSSPACE;
420         uio.uio_rw = UIO_READ;
421         uio.uio_td = td;
422
423         error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, &uio,cred);
424         pmap_qremove(kva, npages);
425
426         relpbuf(bp, &nwfs_pbuf_freecnt);
427
428         if (error && (uio.uio_resid == count)) {
429                 printf("nwfs_getpages: error %d\n",error);
430                 for (i = 0; i < npages; i++) {
431                         if (ap->a_reqpage != i)
432                                 vnode_pager_freepage(pages[i]);
433                 }
434                 return VM_PAGER_ERROR;
435         }
436
437         size = count - uio.uio_resid;
438
439         for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
440                 vm_page_t m;
441                 nextoff = toff + PAGE_SIZE;
442                 m = pages[i];
443
444                 m->flags &= ~PG_ZERO;
445
446                 if (nextoff <= size) {
447                         m->valid = VM_PAGE_BITS_ALL;
448                         m->dirty = 0;
449                 } else {
450                         int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1);
451                         vm_page_set_validclean(m, 0, nvalid);
452                 }
453                 
454                 if (i != ap->a_reqpage) {
455                         /*
456                          * Whether or not to leave the page activated is up in
457                          * the air, but we should put the page on a page queue
458                          * somewhere (it already is in the object).  Result:
459                          * It appears that emperical results show that
460                          * deactivating pages is best.
461                          */
462
463                         /*
464                          * Just in case someone was asking for this page we
465                          * now tell them that it is ok to use.
466                          */
467                         if (!error) {
468                                 if (m->flags & PG_WANTED)
469                                         vm_page_activate(m);
470                                 else
471                                         vm_page_deactivate(m);
472                                 vm_page_wakeup(m);
473                         } else {
474                                 vnode_pager_freepage(m);
475                         }
476                 }
477         }
478         return 0;
479 #endif /* NWFS_RWCACHE */
480 }
481
482 /*
483  * Vnode op for VM putpages.
484  * possible bug: all IO done in sync mode
485  * Note that vop_close always invalidate pages before close, so it's
486  * not necessary to open vnode.
487  *
488  * nwfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
489  *               int a_sync, int *a_rtvals, vm_ooffset_t a_offset)
490  */
491 int
492 nwfs_putpages(struct vop_putpages_args *ap)
493 {
494         int error;
495         struct thread *td = curthread;  /* XXX */
496         struct vnode *vp = ap->a_vp;
497         struct ucred *cred;
498
499 #ifndef NWFS_RWCACHE
500         KKASSERT(td->td_proc);
501         cred = td->td_proc->p_ucred;            /* XXX */
502         VOP_OPEN(vp, FWRITE, cred, NULL, td);
503         error = vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
504                 ap->a_sync, ap->a_rtvals);
505         VOP_CLOSE(vp, FWRITE, cred, td);
506         return error;
507 #else
508         struct uio uio;
509         struct iovec iov;
510         vm_offset_t kva;
511         struct buf *bp;
512         int i, npages, count;
513         int *rtvals;
514         struct nwmount *nmp;
515         struct nwnode *np;
516         vm_page_t *pages;
517
518         KKASSERT(td->td_proc);
519         cred = td->td_proc->p_ucred;            /* XXX */
520
521 /*      VOP_OPEN(vp, FWRITE, cred, td);*/
522         np = VTONW(vp);
523         nmp = VFSTONWFS(vp->v_mount);
524         pages = ap->a_m;
525         count = ap->a_count;
526         rtvals = ap->a_rtvals;
527         npages = btoc(count);
528
529         for (i = 0; i < npages; i++) {
530                 rtvals[i] = VM_PAGER_AGAIN;
531         }
532
533         bp = getpbuf(&nwfs_pbuf_freecnt);
534         kva = (vm_offset_t) bp->b_data;
535         pmap_qenter(kva, pages, npages);
536
537         iov.iov_base = (caddr_t) kva;
538         iov.iov_len = count;
539         uio.uio_iov = &iov;
540         uio.uio_iovcnt = 1;
541         uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
542         uio.uio_resid = count;
543         uio.uio_segflg = UIO_SYSSPACE;
544         uio.uio_rw = UIO_WRITE;
545         uio.uio_td = td;
546         NCPVNDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
547
548         error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, &uio, cred);
549 /*      VOP_CLOSE(vp, FWRITE, cred, td);*/
550         NCPVNDEBUG("paged write done: %d\n", error);
551
552         pmap_qremove(kva, npages);
553         relpbuf(bp, &nwfs_pbuf_freecnt);
554
555         if (!error) {
556                 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
557                 for (i = 0; i < nwritten; i++) {
558                         rtvals[i] = VM_PAGER_OK;
559                         pages[i]->dirty = 0;
560                 }
561         }
562         return rtvals[0];
563 #endif /* NWFS_RWCACHE */
564 }
565 /*
566  * Flush and invalidate all dirty buffers. If another process is already
567  * doing the flush, just wait for completion.
568  */
569 int
570 nwfs_vinvalbuf(struct vnode *vp, int flags, struct thread *td, int intrflg)
571 {
572         struct nwnode *np = VTONW(vp);
573 /*      struct nwmount *nmp = VTONWFS(vp);*/
574         int error = 0, slpflag, slptimeo;
575
576         if (vp->v_flag & VRECLAIMED) {
577                 return (0);
578         }
579         if (intrflg) {
580                 slpflag = PCATCH;
581                 slptimeo = 2 * hz;
582         } else {
583                 slpflag = 0;
584                 slptimeo = 0;
585         }
586         while (np->n_flag & NFLUSHINPROG) {
587                 np->n_flag |= NFLUSHWANT;
588                 error = tsleep((caddr_t)&np->n_flag, 0, "nwfsvinv", slptimeo);
589                 error = ncp_chkintr(NWFSTOCONN(VTONWFS(vp)), td);
590                 if (error == EINTR && intrflg)
591                         return EINTR;
592         }
593         np->n_flag |= NFLUSHINPROG;
594         error = vinvalbuf(vp, flags, td, slpflag, 0);
595         while (error) {
596                 if (intrflg && (error == ERESTART || error == EINTR)) {
597                         np->n_flag &= ~NFLUSHINPROG;
598                         if (np->n_flag & NFLUSHWANT) {
599                                 np->n_flag &= ~NFLUSHWANT;
600                                 wakeup((caddr_t)&np->n_flag);
601                         }
602                         return EINTR;
603                 }
604                 error = vinvalbuf(vp, flags, td, slpflag, 0);
605         }
606         np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
607         if (np->n_flag & NFLUSHWANT) {
608                 np->n_flag &= ~NFLUSHWANT;
609                 wakeup((caddr_t)&np->n_flag);
610         }
611         return (error);
612 }