Merge branch 'vendor/LIBARCHIVE' (early part)
[dragonfly.git] / sys / vfs / smbfs / smbfs_io.c
1 /*
2  * Copyright (c) 2000-2001, 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/fs/smbfs/smbfs_io.c,v 1.3.2.3 2003/01/17 08:20:26 tjr Exp $
33  * $DragonFly: src/sys/vfs/smbfs/smbfs_io.c,v 1.29 2007/08/28 01:04:33 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/proc.h>
41 #include <sys/fcntl.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  <machine/limits.h>
50
51 #include <vm/vm.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_pager.h>
56 #include <vm/vnode_pager.h>
57
58 #include <netproto/smb/smb.h>
59 #include <netproto/smb/smb_conn.h>
60 #include <netproto/smb/smb_subr.h>
61
62 #include "smbfs.h"
63 #include "smbfs_node.h"
64 #include "smbfs_subr.h"
65
66 #include <sys/buf.h>
67
68 #include <sys/thread2.h>
69
70 /*#define SMBFS_RWGENERIC*/
71
72 extern int smbfs_pbuf_freecnt;
73
74 static int smbfs_fastlookup = 1;
75
76 SYSCTL_DECL(_vfs_smbfs);
77 SYSCTL_INT(_vfs_smbfs, OID_AUTO, fastlookup, CTLFLAG_RW, &smbfs_fastlookup, 0, "");
78
79 static int
80 smbfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred)
81 {
82         struct smb_cred scred;
83         struct smbfs_fctx *ctx;
84         struct vnode *newvp;
85         struct smbnode *np;
86         int error, offset, retval/*, *eofflag = ap->a_eofflag*/;
87
88         np = VTOSMB(vp);
89         SMBVDEBUG("dirname='%s'\n", np->n_name);
90         smb_makescred(&scred, uio->uio_td, cred);
91
92         if (uio->uio_resid < 0 || uio->uio_offset < 0 ||
93             uio->uio_offset > INT_MAX)
94                 return(EINVAL);
95
96         error = 0;
97         offset = uio->uio_offset;
98
99         if (uio->uio_resid > 0 && offset < 1) {
100                 if (vop_write_dirent(&error, uio, np->n_ino, DT_DIR, 1, "."))
101                         goto done;
102                 if (error)
103                         goto done;
104                 ++offset;
105         }
106
107         if (uio->uio_resid > 0 && offset < 2) {
108                 if (vop_write_dirent(&error, uio,
109                     np->n_parent ? VTOSMB(np->n_parent)->n_ino : 2,
110                     DT_DIR, 2, ".."))
111                         goto done;
112                 if (error)
113                         goto done;
114                 ++offset;
115         }
116
117         if (uio->uio_resid == 0)
118                 goto done;
119
120         if (offset != np->n_dirofs || np->n_dirseq == NULL) {
121                 SMBVDEBUG("Reopening search %ld:%ld\n", offset, np->n_dirofs);
122                 if (np->n_dirseq) {
123                         smbfs_findclose(np->n_dirseq, &scred);
124                         np->n_dirseq = NULL;
125                 }
126                 np->n_dirofs = 2;
127                 error = smbfs_findopen(np, "*", 1,
128                     SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR,
129                     &scred, &ctx);
130                 if (error) {
131                         SMBVDEBUG("can not open search, error = %d", error);
132                         return error;
133                 }
134                 np->n_dirseq = ctx;
135         } else {
136                 ctx = np->n_dirseq;
137         }
138         while (np->n_dirofs < offset) {
139                 error = smbfs_findnext(ctx, offset - np->n_dirofs, &scred);
140                 ++np->n_dirofs;
141                 if (error) {
142                         smbfs_findclose(np->n_dirseq, &scred);
143                         np->n_dirseq = NULL;
144                         return error == ENOENT ? 0 : error;
145                 }
146         }
147         error = 0;
148         while (uio->uio_resid > 0 && !error) {
149                 /*
150                  * Overestimate the size of a record a bit, doesn't really
151                  * hurt to be wrong here.
152                  */
153                 error = smbfs_findnext(ctx, uio->uio_resid / _DIRENT_RECLEN(255) + 1, &scred);
154                 if (error)
155                         break;
156                 np->n_dirofs++;
157                 ++offset;
158
159                 retval = vop_write_dirent(&error, uio, ctx->f_attr.fa_ino,
160                     (ctx->f_attr.fa_attr & SMB_FA_DIR) ? DT_DIR : DT_REG,
161                     ctx->f_nmlen, ctx->f_name);
162                 if (retval)
163                         break;
164                 if (smbfs_fastlookup && !error) {
165                         error = smbfs_nget(vp->v_mount, vp, ctx->f_name,
166                             ctx->f_nmlen, &ctx->f_attr, &newvp);
167                         if (!error)
168                                 vput(newvp);
169                 }
170         }
171         if (error == ENOENT)
172                 error = 0;
173 done:
174         uio->uio_offset = offset;
175         return error;
176 }
177
178 int
179 smbfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred)
180 {
181         struct thread *td;
182         struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
183         struct smbnode *np = VTOSMB(vp);
184         struct vattr vattr;
185         struct smb_cred scred;
186         int error, lks;
187
188         /*
189          * Protect against method which is not supported for now
190          */
191         if (uiop->uio_segflg == UIO_NOCOPY)
192                 return EOPNOTSUPP;
193
194         if (vp->v_type != VREG && vp->v_type != VDIR) {
195                 SMBFSERR("vn types other than VREG or VDIR are unsupported !\n");
196                 return EIO;
197         }
198         if (uiop->uio_resid == 0)
199                 return 0;
200         if (uiop->uio_offset < 0)
201                 return EINVAL;
202 /*      if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
203                 return EFBIG;*/
204         td = uiop->uio_td;
205         if (vp->v_type == VDIR) {
206                 lks = LK_EXCLUSIVE;/*lockstatus(&vp->v_lock, td);*/
207                 if (lks == LK_SHARED)
208                         vn_lock(vp, LK_UPGRADE | LK_RETRY);
209                 error = smbfs_readvdir(vp, uiop, cred);
210                 if (lks == LK_SHARED)
211                         vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
212                 return error;
213         }
214
215 /*      biosize = SSTOCN(smp->sm_share)->sc_txmax;*/
216         if (np->n_flag & NMODIFIED) {
217                 smbfs_attr_cacheremove(vp);
218                 error = VOP_GETATTR(vp, &vattr);
219                 if (error)
220                         return error;
221                 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
222         } else {
223                 error = VOP_GETATTR(vp, &vattr);
224                 if (error)
225                         return error;
226                 if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
227                         error = smbfs_vinvalbuf(vp, V_SAVE, 1);
228                         if (error)
229                                 return error;
230                         np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
231                 }
232         }
233         smb_makescred(&scred, td, cred);
234         return smb_read(smp->sm_share, np->n_fid, uiop, &scred);
235 }
236
237 int
238 smbfs_writevnode(struct vnode *vp, struct uio *uiop,
239                  struct ucred *cred, int ioflag)
240 {
241         struct thread *td;
242         struct smbmount *smp = VTOSMBFS(vp);
243         struct smbnode *np = VTOSMB(vp);
244         struct smb_cred scred;
245         int error = 0;
246
247         if (vp->v_type != VREG) {
248                 SMBERROR("vn types other than VREG unsupported !\n");
249                 return EIO;
250         }
251         SMBVDEBUG("ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
252         if (uiop->uio_offset < 0)
253                 return EINVAL;
254 /*      if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
255                 return (EFBIG);*/
256         td = uiop->uio_td;
257         if (ioflag & (IO_APPEND | IO_SYNC)) {
258                 if (np->n_flag & NMODIFIED) {
259                         smbfs_attr_cacheremove(vp);
260                         error = smbfs_vinvalbuf(vp, V_SAVE, 1);
261                         if (error)
262                                 return error;
263                 }
264                 if (ioflag & IO_APPEND) {
265 #if notyet
266                         /*
267                          * File size can be changed by another client
268                          */
269                         smbfs_attr_cacheremove(vp);
270                         error = VOP_GETATTR(vp, &vattr);
271                         if (error) return (error);
272 #endif
273                         uiop->uio_offset = np->n_size;
274                 }
275         }
276         if (uiop->uio_resid == 0)
277                 return 0;
278         if (td->td_proc &&
279             uiop->uio_offset + uiop->uio_resid >
280             td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
281                 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
282                 return EFBIG;
283         }
284         smb_makescred(&scred, td, cred);
285         error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
286         SMBVDEBUG("after: ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
287         if (!error) {
288                 if (uiop->uio_offset > np->n_size) {
289                         np->n_size = uiop->uio_offset;
290                         vnode_pager_setsize(vp, np->n_size);
291                 }
292         }
293         return error;
294 }
295
296 /*
297  * Do an I/O operation to/from a cache block.
298  */
299 int
300 smbfs_doio(struct vnode *vp, struct bio *bio, struct ucred *cr, struct thread *td)
301 {
302         struct buf *bp = bio->bio_buf;
303         struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
304         struct smbnode *np = VTOSMB(vp);
305         struct uio uio, *uiop = &uio;
306         struct iovec io;
307         struct smb_cred scred;
308         int error = 0;
309
310         uiop->uio_iov = &io;
311         uiop->uio_iovcnt = 1;
312         uiop->uio_segflg = UIO_SYSSPACE;
313         uiop->uio_td = td;
314
315         smb_makescred(&scred, td, cr);
316
317         if (bp->b_cmd == BUF_CMD_READ) {
318             io.iov_len = uiop->uio_resid = bp->b_bcount;
319             io.iov_base = bp->b_data;
320             uiop->uio_rw = UIO_READ;
321             switch (vp->v_type) {
322               case VREG:
323                 uiop->uio_offset = bio->bio_offset;
324                 error = smb_read(smp->sm_share, np->n_fid, uiop, &scred);
325                 if (error)
326                         break;
327                 if (uiop->uio_resid) {
328                         int left = uiop->uio_resid;
329                         int nread = bp->b_bcount - left;
330                         if (left > 0)
331                             bzero((char *)bp->b_data + nread, left);
332                 }
333                 break;
334             default:
335                 kprintf("smbfs_doio:  type %x unexpected\n",vp->v_type);
336                 break;
337             };
338             if (error) {
339                 bp->b_error = error;
340                 bp->b_flags |= B_ERROR;
341             }
342         } else { /* write */
343             KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
344             if (bio->bio_offset + bp->b_dirtyend > np->n_size)
345                 bp->b_dirtyend = np->n_size - bio->bio_offset;
346
347             if (bp->b_dirtyend > bp->b_dirtyoff) {
348                 io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
349                 uiop->uio_offset = bio->bio_offset + bp->b_dirtyoff;
350                 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
351                 uiop->uio_rw = UIO_WRITE;
352                 error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
353
354                 /*
355                  * For an interrupted write, the buffer is still valid
356                  * and the write hasn't been pushed to the server yet,
357                  * so we can't set BIO_ERROR and report the interruption
358                  * by setting B_EINTR. For the B_ASYNC case, B_EINTR
359                  * is not relevant, so the rpc attempt is essentially
360                  * a noop.  For the case of a V3 write rpc not being
361                  * committed to stable storage, the block is still
362                  * dirty and requires either a commit rpc or another
363                  * write rpc with iomode == NFSV3WRITE_FILESYNC before
364                  * the block is reused. This is indicated by setting
365                  * the B_DELWRI and B_NEEDCOMMIT flags.
366                  */
367                 if (error == EINTR
368                     || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
369
370                         crit_enter();
371                         bp->b_flags &= ~(B_INVAL|B_NOCACHE);
372                         if ((bp->b_flags & B_ASYNC) == 0)
373                             bp->b_flags |= B_EINTR;
374                         if ((bp->b_flags & B_PAGING) == 0)
375                             bdirty(bp);
376                         if ((bp->b_flags & B_ASYNC) == 0)
377                             bp->b_flags |= B_EINTR;
378                         crit_exit();
379                 } else {
380                         if (error) {
381                                 bp->b_flags |= B_ERROR;
382                                 bp->b_error = error;
383                         }
384                         bp->b_dirtyoff = bp->b_dirtyend = 0;
385                 }
386             } else {
387                 bp->b_resid = 0;
388                 biodone(bio);
389                 return 0;
390             }
391         }
392         bp->b_resid = uiop->uio_resid;
393         biodone(bio);
394         return error;
395 }
396
397 /*
398  * Vnode op for VM getpages.
399  * Wish wish .... get rid from multiple IO routines
400  *
401  * smbfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
402  *                int a_reqpage, vm_ooffset_t a_offset)
403  */
404 int
405 smbfs_getpages(struct vop_getpages_args *ap)
406 {
407 #ifdef SMBFS_RWGENERIC
408         return vop_stdgetpages(ap);
409 #else
410         int i, error, nextoff, size, toff, npages, count;
411         int doclose;
412         struct uio uio;
413         struct iovec iov;
414         vm_offset_t kva;
415         struct buf *bp;
416         struct vnode *vp;
417         struct thread *td = curthread;  /* XXX */
418         struct ucred *cred;
419         struct smbmount *smp;
420         struct smbnode *np;
421         struct smb_cred scred;
422         vm_page_t *pages;
423
424         KKASSERT(td->td_proc);
425
426         vp = ap->a_vp;
427         cred = td->td_proc->p_ucred;
428         np = VTOSMB(vp);
429         smp = VFSTOSMBFS(vp->v_mount);
430         pages = ap->a_m;
431         count = ap->a_count;
432
433         if (vp->v_object == NULL) {
434                 kprintf("smbfs_getpages: called with non-merged cache vnode??\n");
435                 return VM_PAGER_ERROR;
436         }
437         smb_makescred(&scred, td, cred);
438
439         bp = getpbuf(&smbfs_pbuf_freecnt);
440         npages = btoc(count);
441         kva = (vm_offset_t) bp->b_data;
442         pmap_qenter(kva, pages, npages);
443
444         iov.iov_base = (caddr_t) kva;
445         iov.iov_len = count;
446         uio.uio_iov = &iov;
447         uio.uio_iovcnt = 1;
448         uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
449         uio.uio_resid = count;
450         uio.uio_segflg = UIO_SYSSPACE;
451         uio.uio_rw = UIO_READ;
452         uio.uio_td = td;
453
454         /*
455          * This is kinda nasty.  Since smbfs is physically closing the
456          * fid on close(), we have to reopen it if necessary.  There are
457          * other races here too, such as if another process opens the same
458          * file while we are blocked in read. XXX
459          */
460         error = 0;
461         doclose = 0;
462         if (np->n_opencount == 0) {
463                 error = smbfs_smb_open(np, SMB_AM_OPENREAD, &scred);
464                 if (error == 0)
465                         doclose = 1;
466         }
467         if (error == 0)
468                 error = smb_read(smp->sm_share, np->n_fid, &uio, &scred);
469         if (doclose)
470                 smbfs_smb_close(smp->sm_share, np->n_fid, NULL, &scred);
471         pmap_qremove(kva, npages);
472
473         relpbuf(bp, &smbfs_pbuf_freecnt);
474
475         if (error && (uio.uio_resid == count)) {
476                 kprintf("smbfs_getpages: error %d\n",error);
477                 for (i = 0; i < npages; i++) {
478                         if (ap->a_reqpage != i)
479                                 vnode_pager_freepage(pages[i]);
480                 }
481                 return VM_PAGER_ERROR;
482         }
483
484         size = count - uio.uio_resid;
485
486         for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
487                 vm_page_t m;
488                 nextoff = toff + PAGE_SIZE;
489                 m = pages[i];
490
491                 m->flags &= ~PG_ZERO;
492
493                 if (nextoff <= size) {
494                         m->valid = VM_PAGE_BITS_ALL;
495                         m->dirty = 0;
496                 } else {
497                         int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1);
498                         vm_page_set_validclean(m, 0, nvalid);
499                 }
500                 
501                 if (i != ap->a_reqpage) {
502                         /*
503                          * Whether or not to leave the page activated is up in
504                          * the air, but we should put the page on a page queue
505                          * somewhere (it already is in the object).  Result:
506                          * It appears that emperical results show that
507                          * deactivating pages is best.
508                          */
509
510                         /*
511                          * Just in case someone was asking for this page we
512                          * now tell them that it is ok to use.
513                          */
514                         if (!error) {
515                                 if (m->flags & PG_WANTED)
516                                         vm_page_activate(m);
517                                 else
518                                         vm_page_deactivate(m);
519                                 vm_page_wakeup(m);
520                         } else {
521                                 vnode_pager_freepage(m);
522                         }
523                 }
524         }
525         return 0;
526 #endif /* SMBFS_RWGENERIC */
527 }
528
529 /*
530  * Vnode op for VM putpages.
531  * possible bug: all IO done in sync mode
532  * Note that vop_close always invalidate pages before close, so it's
533  * not necessary to open vnode.
534  *
535  * smbfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count, int a_sync,
536  *                int *a_rtvals, vm_ooffset_t a_offset)
537  */
538 int
539 smbfs_putpages(struct vop_putpages_args *ap)
540 {
541         int error;
542         struct vnode *vp = ap->a_vp;
543         struct thread *td = curthread;  /* XXX */
544         struct ucred *cred;
545
546 #ifdef SMBFS_RWGENERIC
547         KKASSERT(td->td_proc);
548         cred = td->td_proc->p_ucred;
549         VOP_OPEN(vp, FWRITE, cred, NULL);
550         error = vop_stdputpages(ap);
551         VOP_CLOSE(vp, FWRITE, cred);
552         return error;
553 #else
554         struct uio uio;
555         struct iovec iov;
556         vm_offset_t kva;
557         struct buf *bp;
558         int i, npages, count;
559         int doclose;
560         int *rtvals;
561         struct smbmount *smp;
562         struct smbnode *np;
563         struct smb_cred scred;
564         vm_page_t *pages;
565
566         KKASSERT(td->td_proc);
567         cred = td->td_proc->p_ucred;
568 /*      VOP_OPEN(vp, FWRITE, cred, NULL);*/
569         np = VTOSMB(vp);
570         smp = VFSTOSMBFS(vp->v_mount);
571         pages = ap->a_m;
572         count = ap->a_count;
573         rtvals = ap->a_rtvals;
574         npages = btoc(count);
575
576         for (i = 0; i < npages; i++) {
577                 rtvals[i] = VM_PAGER_AGAIN;
578         }
579
580         bp = getpbuf(&smbfs_pbuf_freecnt);
581         kva = (vm_offset_t) bp->b_data;
582         pmap_qenter(kva, pages, npages);
583
584         iov.iov_base = (caddr_t) kva;
585         iov.iov_len = count;
586         uio.uio_iov = &iov;
587         uio.uio_iovcnt = 1;
588         uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
589         uio.uio_resid = count;
590         uio.uio_segflg = UIO_SYSSPACE;
591         uio.uio_rw = UIO_WRITE;
592         uio.uio_td = td;
593         SMBVDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
594
595         smb_makescred(&scred, td, cred);
596
597         /*
598          * This is kinda nasty.  Since smbfs is physically closing the
599          * fid on close(), we have to reopen it if necessary.  There are
600          * other races here too, such as if another process opens the same
601          * file while we are blocked in read, or the file is open read-only
602          * XXX
603          */
604         error = 0;
605         doclose = 0;
606         if (np->n_opencount == 0) {
607                 error = smbfs_smb_open(np, SMB_AM_OPENRW, &scred);
608                 if (error == 0)
609                         doclose = 1;
610         }
611         if (error == 0)
612                 error = smb_write(smp->sm_share, np->n_fid, &uio, &scred);
613         if (doclose)
614                 smbfs_smb_close(smp->sm_share, np->n_fid, NULL, &scred);
615 /*      VOP_CLOSE(vp, FWRITE, cred);*/
616         SMBVDEBUG("paged write done: %d\n", error);
617
618         pmap_qremove(kva, npages);
619         relpbuf(bp, &smbfs_pbuf_freecnt);
620
621         if (!error) {
622                 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
623                 for (i = 0; i < nwritten; i++) {
624                         rtvals[i] = VM_PAGER_OK;
625                         pages[i]->dirty = 0;
626                 }
627         }
628         return rtvals[0];
629 #endif /* SMBFS_RWGENERIC */
630 }
631
632 /*
633  * Flush and invalidate all dirty buffers. If another process is already
634  * doing the flush, just wait for completion.
635  */
636 int
637 smbfs_vinvalbuf(struct vnode *vp, int flags, int intrflg)
638 {
639         struct smbnode *np = VTOSMB(vp);
640         int error = 0, slpflag, slptimeo;
641
642         if (vp->v_flag & VRECLAIMED)
643                 return 0;
644         if (intrflg) {
645                 slpflag = PCATCH;
646                 slptimeo = 2 * hz;
647         } else {
648                 slpflag = 0;
649                 slptimeo = 0;
650         }
651         while (np->n_flag & NFLUSHINPROG) {
652                 np->n_flag |= NFLUSHWANT;
653                 error = tsleep((caddr_t)&np->n_flag, 0, "smfsvinv", slptimeo);
654                 error = smb_proc_intr(curthread);
655                 if (error == EINTR && intrflg)
656                         return EINTR;
657         }
658         np->n_flag |= NFLUSHINPROG;
659         error = vinvalbuf(vp, flags, slpflag, 0);
660         while (error) {
661                 if (intrflg && (error == ERESTART || error == EINTR)) {
662                         np->n_flag &= ~NFLUSHINPROG;
663                         if (np->n_flag & NFLUSHWANT) {
664                                 np->n_flag &= ~NFLUSHWANT;
665                                 wakeup((caddr_t)&np->n_flag);
666                         }
667                         return EINTR;
668                 }
669                 error = vinvalbuf(vp, flags, slpflag, 0);
670         }
671         np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
672         if (np->n_flag & NFLUSHWANT) {
673                 np->n_flag &= ~NFLUSHWANT;
674                 wakeup((caddr_t)&np->n_flag);
675         }
676         return (error);
677 }