ef8fe7fb017cb3d2f6f5963527c6490c3c630155
[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.13 2004/11/12 00:09:42 dillon 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 "nwfs.h"
61 #include "nwfs_node.h"
62 #include "nwfs_subr.h"
63
64 static int nwfs_fastlookup = 1;
65
66 SYSCTL_DECL(_vfs_nwfs);
67 SYSCTL_INT(_vfs_nwfs, OID_AUTO, fastlookup, CTLFLAG_RW, &nwfs_fastlookup, 0, "");
68
69
70 extern int nwfs_pbuf_freecnt;
71
72 #define DE_SIZE (sizeof(struct dirent))
73 #define NWFS_RWCACHE
74
75 static int
76 nwfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred)
77 {
78         struct nwmount *nmp = VTONWFS(vp);
79         int error, count, i;
80         struct dirent dp;
81         struct nwnode *np = VTONW(vp);
82         struct nw_entry_info fattr;
83         struct vnode *newvp;
84         ncpfid fid;
85
86         np = VTONW(vp);
87         NCPVNDEBUG("dirname='%s'\n",np->n_name);
88         if (uio->uio_resid < DE_SIZE || (uio->uio_offset < 0))
89                 return (EINVAL);
90         error = 0;
91         count = 0;
92         i = uio->uio_offset / DE_SIZE; /* offset in directory */
93         if (i == 0) {
94                 error = ncp_initsearch(vp, uio->uio_td, cred);
95                 if (error) {
96                         NCPVNDEBUG("cannot initialize search, error=%d",error);
97                         return( error );
98                 }
99         }
100
101         for (; uio->uio_resid >= DE_SIZE; i++) {
102                 bzero((char *) &dp, DE_SIZE);
103                 dp.d_reclen = DE_SIZE;
104                 switch (i) {
105                     case 0:             /* `.' */
106                     case 1:             /* `..' */
107                         dp.d_fileno = (i == 0) ? np->n_fid.f_id : np->n_parent.f_id;
108                         if (!dp.d_fileno) dp.d_fileno = NWFS_ROOT_INO;
109                         dp.d_namlen = i + 1;
110                         dp.d_name[0] = '.';
111                         dp.d_name[1] = '.';
112                         dp.d_name[i + 1] = '\0';
113                         dp.d_type = DT_DIR;
114                         break;
115                     default:
116                         error = ncp_search_for_file_or_subdir(nmp, &np->n_seq, &fattr, uio->uio_td, cred);
117                         if (error && error < 0x80) break;
118                         dp.d_fileno = fattr.dirEntNum;
119                         dp.d_type = (fattr.attributes & aDIR) ? DT_DIR : DT_REG;
120                         dp.d_namlen = fattr.nameLen;
121                         bcopy(fattr.entryName, dp.d_name, dp.d_namlen);
122                         dp.d_name[dp.d_namlen] = '\0';
123 #if 0
124                         if (error && eofflag) {
125                         /*      *eofflag = 1;*/
126                                 break;
127                         }
128 #endif
129                         break;
130                 }
131                 if (nwfs_fastlookup && !error && i > 1) {
132                         fid.f_id = fattr.dirEntNum;
133                         fid.f_parent = np->n_fid.f_id;
134                         error = nwfs_nget(vp->v_mount, fid, &fattr, vp, &newvp);
135                         if (!error) {
136                                 VTONW(newvp)->n_ctime = VTONW(newvp)->n_vattr.va_ctime.tv_sec;
137                                 vput(newvp);
138                         } else
139                                 error = 0;
140                 }
141                 if (error >= 0x80) {
142                     error = 0;
143                     break;
144                 }
145                 if ((error = uiomove((caddr_t)&dp, DE_SIZE, uio)))
146                         break;
147         }
148
149         uio->uio_offset = i * DE_SIZE;
150         return (error);
151 }
152
153 int
154 nwfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred)
155 {
156         struct nwmount *nmp = VFSTONWFS(vp->v_mount);
157         struct nwnode *np = VTONW(vp);
158         struct thread *td;
159         struct vattr vattr;
160         int error, biosize;
161
162         if (vp->v_type != VREG && vp->v_type != VDIR) {
163                 printf("%s: vn types other than VREG or VDIR are unsupported !\n",__FUNCTION__);
164                 return EIO;
165         }
166         if (uiop->uio_resid == 0) return 0;
167         if (uiop->uio_offset < 0) return EINVAL;
168 /*      if (uiop->uio_offset + uiop->uio_resid > nmp->nm_maxfilesize)
169                 return (EFBIG);*/
170         td = uiop->uio_td;
171         if (vp->v_type == VDIR) {
172                 error = nwfs_readvdir(vp, uiop, cred);
173                 return error;
174         }
175         biosize = NWFSTOCONN(nmp)->buffer_size;
176         if (np->n_flag & NMODIFIED) {
177                 nwfs_attr_cacheremove(vp);
178                 error = VOP_GETATTR(vp, &vattr, td);
179                 if (error) return (error);
180                 np->n_mtime = vattr.va_mtime.tv_sec;
181         } else {
182                 error = VOP_GETATTR(vp, &vattr, td);
183                 if (error) return (error);
184                 if (np->n_mtime != vattr.va_mtime.tv_sec) {
185                         error = nwfs_vinvalbuf(vp, V_SAVE, td, 1);
186                         if (error) return (error);
187                         np->n_mtime = vattr.va_mtime.tv_sec;
188                 }
189         }
190         error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, uiop,cred);
191         return (error);
192 }
193
194 int
195 nwfs_writevnode(struct vnode *vp, struct uio *uiop, struct ucred *cred,
196                 int ioflag)
197 {
198         struct nwmount *nmp = VTONWFS(vp);
199         struct nwnode *np = VTONW(vp);
200         struct thread *td;
201 /*      struct vattr vattr;*/
202         int error = 0;
203
204         if (vp->v_type != VREG) {
205                 printf("%s: vn types other than VREG unsupported !\n",__FUNCTION__);
206                 return EIO;
207         }
208         NCPVNDEBUG("ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
209         if (uiop->uio_offset < 0) return EINVAL;
210 /*      if (uiop->uio_offset + uiop->uio_resid > nmp->nm_maxfilesize)
211                 return (EFBIG);*/
212         td = uiop->uio_td;
213         if (ioflag & (IO_APPEND | IO_SYNC)) {
214                 if (np->n_flag & NMODIFIED) {
215                         nwfs_attr_cacheremove(vp);
216                         error = nwfs_vinvalbuf(vp, V_SAVE, td, 1);
217                         if (error) return (error);
218                 }
219                 if (ioflag & IO_APPEND) {
220                 /* We can relay only on local information about file size,
221                  * because until file is closed NetWare will not return
222                  * the correct size. */
223 #if notyet
224                         nwfs_attr_cacheremove(vp);
225                         error = VOP_GETATTR(vp, &vattr, td);
226                         if (error) return (error);
227 #endif
228                         uiop->uio_offset = np->n_size;
229                 }
230         }
231         if (uiop->uio_resid == 0) return 0;
232         if (td->td_proc && uiop->uio_offset + uiop->uio_resid > 
233             td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
234                 psignal(td->td_proc, SIGXFSZ);
235                 return (EFBIG);
236         }
237         error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, uiop, cred);
238         NCPVNDEBUG("after: ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
239         if (!error) {
240                 if (uiop->uio_offset > np->n_size) {
241                         np->n_vattr.va_size = np->n_size = uiop->uio_offset;
242                         vnode_pager_setsize(vp, np->n_size);
243                 }
244         }
245         return (error);
246 }
247
248 /*
249  * Do an I/O operation to/from a cache block.
250  */
251 int
252 nwfs_doio(struct buf *bp, struct ucred *cr, struct thread *td)
253 {
254         struct uio *uiop;
255         struct vnode *vp;
256         struct nwnode *np;
257         struct nwmount *nmp;
258         int error = 0;
259         struct uio uio;
260         struct iovec io;
261
262         vp = bp->b_vp;
263         np = VTONW(vp);
264         nmp = VFSTONWFS(vp->v_mount);
265         uiop = &uio;
266         uiop->uio_iov = &io;
267         uiop->uio_iovcnt = 1;
268         uiop->uio_segflg = UIO_SYSSPACE;
269         uiop->uio_td = td;
270         if (bp->b_flags & B_READ) {
271             io.iov_len = uiop->uio_resid = bp->b_bcount;
272             io.iov_base = bp->b_data;
273             uiop->uio_rw = UIO_READ;
274             switch (vp->v_type) {
275               case VREG:
276                 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
277                 error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, uiop, cr);
278                 if (error)
279                         break;
280                 if (uiop->uio_resid) {
281                         int left = uiop->uio_resid;
282                         int nread = bp->b_bcount - left;
283                         if (left > 0)
284                             bzero((char *)bp->b_data + nread, left);
285                 }
286                 break;
287 /*          case VDIR:
288                 nfsstats.readdir_bios++;
289                 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
290                 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
291                         error = nfs_readdirplusrpc(vp, uiop, cr);
292                         if (error == NFSERR_NOTSUPP)
293                                 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
294                 }
295                 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
296                         error = nfs_readdirrpc(vp, uiop, cr);
297                 if (error == 0 && uiop->uio_resid == bp->b_bcount)
298                         bp->b_flags |= B_INVAL;
299                 break;
300 */
301             default:
302                 printf("nwfs_doio:  type %x unexpected\n",vp->v_type);
303                 break;
304             };
305             if (error) {
306                 bp->b_flags |= B_ERROR;
307                 bp->b_error = error;
308             }
309         } else { /* write */
310             if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
311                 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
312
313             if (bp->b_dirtyend > bp->b_dirtyoff) {
314                 io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
315                 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
316                 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
317                 uiop->uio_rw = UIO_WRITE;
318                 bp->b_flags |= B_WRITEINPROG;
319                 error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, uiop, cr);
320                 bp->b_flags &= ~B_WRITEINPROG;
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                         int s;
338
339                         s = splbio();
340                         bp->b_flags &= ~(B_INVAL|B_NOCACHE);
341                         if ((bp->b_flags & B_ASYNC) == 0)
342                             bp->b_flags |= B_EINTR;
343                         if ((bp->b_flags & B_PAGING) == 0) {
344                             bdirty(bp);
345                             bp->b_flags &= ~B_DONE;
346                         }
347                         if ((bp->b_flags & B_ASYNC) == 0)
348                             bp->b_flags |= B_EINTR;
349                         splx(s);
350                 } else {
351                         if (error) {
352                                 bp->b_flags |= B_ERROR;
353                                 bp->b_error /*= np->n_error */= error;
354 /*                              np->n_flag |= NWRITEERR;*/
355                         }
356                         bp->b_dirtyoff = bp->b_dirtyend = 0;
357                 }
358             } else {
359                 bp->b_resid = 0;
360                 biodone(bp);
361                 return (0);
362             }
363         }
364         bp->b_resid = uiop->uio_resid;
365         biodone(bp);
366         return (error);
367 }
368
369 /*
370  * Vnode op for VM getpages.
371  * Wish wish .... get rid from multiple IO routines
372  *
373  * nwfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
374  *               int a_reqpage, vm_ooffset_t a_offset)
375  */
376 int
377 nwfs_getpages(struct vop_getpages_args *ap)
378 {
379 #ifndef NWFS_RWCACHE
380         return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
381                 ap->a_reqpage);
382 #else
383         int i, error, nextoff, size, toff, npages, count;
384         struct uio uio;
385         struct iovec iov;
386         vm_offset_t kva;
387         struct buf *bp;
388         struct vnode *vp;
389         struct thread *td = curthread;  /* XXX */
390         struct ucred *cred;
391         struct nwmount *nmp;
392         struct nwnode *np;
393         vm_page_t *pages;
394
395         KKASSERT(td->td_proc);
396         cred = td->td_proc->p_ucred;
397
398         vp = ap->a_vp;
399         np = VTONW(vp);
400         nmp = VFSTONWFS(vp->v_mount);
401         pages = ap->a_m;
402         count = ap->a_count;
403
404         if (vp->v_object == NULL) {
405                 printf("nwfs_getpages: called with non-merged cache vnode??\n");
406                 return VM_PAGER_ERROR;
407         }
408
409         bp = getpbuf(&nwfs_pbuf_freecnt);
410         npages = btoc(count);
411         kva = (vm_offset_t) bp->b_data;
412         pmap_qenter(kva, pages, npages);
413
414         iov.iov_base = (caddr_t) kva;
415         iov.iov_len = count;
416         uio.uio_iov = &iov;
417         uio.uio_iovcnt = 1;
418         uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
419         uio.uio_resid = count;
420         uio.uio_segflg = UIO_SYSSPACE;
421         uio.uio_rw = UIO_READ;
422         uio.uio_td = td;
423
424         error = ncp_read(NWFSTOCONN(nmp), &np->n_fh, &uio,cred);
425         pmap_qremove(kva, npages);
426
427         relpbuf(bp, &nwfs_pbuf_freecnt);
428
429         if (error && (uio.uio_resid == count)) {
430                 printf("nwfs_getpages: error %d\n",error);
431                 for (i = 0; i < npages; i++) {
432                         if (ap->a_reqpage != i)
433                                 vnode_pager_freepage(pages[i]);
434                 }
435                 return VM_PAGER_ERROR;
436         }
437
438         size = count - uio.uio_resid;
439
440         for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
441                 vm_page_t m;
442                 nextoff = toff + PAGE_SIZE;
443                 m = pages[i];
444
445                 m->flags &= ~PG_ZERO;
446
447                 if (nextoff <= size) {
448                         m->valid = VM_PAGE_BITS_ALL;
449                         m->dirty = 0;
450                 } else {
451                         int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1);
452                         vm_page_set_validclean(m, 0, nvalid);
453                 }
454                 
455                 if (i != ap->a_reqpage) {
456                         /*
457                          * Whether or not to leave the page activated is up in
458                          * the air, but we should put the page on a page queue
459                          * somewhere (it already is in the object).  Result:
460                          * It appears that emperical results show that
461                          * deactivating pages is best.
462                          */
463
464                         /*
465                          * Just in case someone was asking for this page we
466                          * now tell them that it is ok to use.
467                          */
468                         if (!error) {
469                                 if (m->flags & PG_WANTED)
470                                         vm_page_activate(m);
471                                 else
472                                         vm_page_deactivate(m);
473                                 vm_page_wakeup(m);
474                         } else {
475                                 vnode_pager_freepage(m);
476                         }
477                 }
478         }
479         return 0;
480 #endif /* NWFS_RWCACHE */
481 }
482
483 /*
484  * Vnode op for VM putpages.
485  * possible bug: all IO done in sync mode
486  * Note that vop_close always invalidate pages before close, so it's
487  * not necessary to open vnode.
488  *
489  * nwfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
490  *               int a_sync, int *a_rtvals, vm_ooffset_t a_offset)
491  */
492 int
493 nwfs_putpages(struct vop_putpages_args *ap)
494 {
495         int error;
496         struct thread *td = curthread;  /* XXX */
497         struct vnode *vp = ap->a_vp;
498         struct ucred *cred;
499
500 #ifndef NWFS_RWCACHE
501         KKASSERT(td->td_proc);
502         cred = td->td_proc->p_ucred;            /* XXX */
503         VOP_OPEN(vp, FWRITE, cred, NULL, td);
504         error = vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
505                 ap->a_sync, ap->a_rtvals);
506         VOP_CLOSE(vp, FWRITE, cred, td);
507         return error;
508 #else
509         struct uio uio;
510         struct iovec iov;
511         vm_offset_t kva;
512         struct buf *bp;
513         int i, npages, count;
514         int *rtvals;
515         struct nwmount *nmp;
516         struct nwnode *np;
517         vm_page_t *pages;
518
519         KKASSERT(td->td_proc);
520         cred = td->td_proc->p_ucred;            /* XXX */
521
522 /*      VOP_OPEN(vp, FWRITE, cred, td);*/
523         np = VTONW(vp);
524         nmp = VFSTONWFS(vp->v_mount);
525         pages = ap->a_m;
526         count = ap->a_count;
527         rtvals = ap->a_rtvals;
528         npages = btoc(count);
529
530         for (i = 0; i < npages; i++) {
531                 rtvals[i] = VM_PAGER_AGAIN;
532         }
533
534         bp = getpbuf(&nwfs_pbuf_freecnt);
535         kva = (vm_offset_t) bp->b_data;
536         pmap_qenter(kva, pages, npages);
537
538         iov.iov_base = (caddr_t) kva;
539         iov.iov_len = count;
540         uio.uio_iov = &iov;
541         uio.uio_iovcnt = 1;
542         uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
543         uio.uio_resid = count;
544         uio.uio_segflg = UIO_SYSSPACE;
545         uio.uio_rw = UIO_WRITE;
546         uio.uio_td = td;
547         NCPVNDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
548
549         error = ncp_write(NWFSTOCONN(nmp), &np->n_fh, &uio, cred);
550 /*      VOP_CLOSE(vp, FWRITE, cred, td);*/
551         NCPVNDEBUG("paged write done: %d\n", error);
552
553         pmap_qremove(kva, npages);
554         relpbuf(bp, &nwfs_pbuf_freecnt);
555
556         if (!error) {
557                 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
558                 for (i = 0; i < nwritten; i++) {
559                         rtvals[i] = VM_PAGER_OK;
560                         pages[i]->dirty = 0;
561                 }
562         }
563         return rtvals[0];
564 #endif /* NWFS_RWCACHE */
565 }
566 /*
567  * Flush and invalidate all dirty buffers. If another process is already
568  * doing the flush, just wait for completion.
569  */
570 int
571 nwfs_vinvalbuf(struct vnode *vp, int flags, struct thread *td, int intrflg)
572 {
573         struct nwnode *np = VTONW(vp);
574 /*      struct nwmount *nmp = VTONWFS(vp);*/
575         int error = 0, slpflag, slptimeo;
576
577         if (vp->v_flag & VRECLAIMED) {
578                 return (0);
579         }
580         if (intrflg) {
581                 slpflag = PCATCH;
582                 slptimeo = 2 * hz;
583         } else {
584                 slpflag = 0;
585                 slptimeo = 0;
586         }
587         while (np->n_flag & NFLUSHINPROG) {
588                 np->n_flag |= NFLUSHWANT;
589                 error = tsleep((caddr_t)&np->n_flag, 0, "nwfsvinv", slptimeo);
590                 error = ncp_chkintr(NWFSTOCONN(VTONWFS(vp)), td);
591                 if (error == EINTR && intrflg)
592                         return EINTR;
593         }
594         np->n_flag |= NFLUSHINPROG;
595         error = vinvalbuf(vp, flags, td, slpflag, 0);
596         while (error) {
597                 if (intrflg && (error == ERESTART || error == EINTR)) {
598                         np->n_flag &= ~NFLUSHINPROG;
599                         if (np->n_flag & NFLUSHWANT) {
600                                 np->n_flag &= ~NFLUSHWANT;
601                                 wakeup((caddr_t)&np->n_flag);
602                         }
603                         return EINTR;
604                 }
605                 error = vinvalbuf(vp, flags, td, slpflag, 0);
606         }
607         np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
608         if (np->n_flag & NFLUSHWANT) {
609                 np->n_flag &= ~NFLUSHWANT;
610                 wakeup((caddr_t)&np->n_flag);
611         }
612         return (error);
613 }