Merge from vendor branch FILE:
[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.17 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/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 <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 <sys/ioccom.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
80 #define DE_SIZE (sizeof(struct dirent))
81
82 static int
83 smbfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred)
84 {
85         struct dirent de;
86         struct smb_cred scred;
87         struct smbfs_fctx *ctx;
88         struct vnode *newvp;
89         struct smbnode *np = VTOSMB(vp);
90         int error/*, *eofflag = ap->a_eofflag*/;
91         long offset, limit;
92
93         np = VTOSMB(vp);
94         SMBVDEBUG("dirname='%s'\n", np->n_name);
95         smb_makescred(&scred, uio->uio_td, cred);
96         offset = uio->uio_offset / DE_SIZE;     /* offset in the directory */
97         limit = uio->uio_resid / DE_SIZE;
98         if (uio->uio_resid < DE_SIZE || uio->uio_offset < 0)
99                 return EINVAL;
100         while (limit && offset < 2) {
101                 limit--;
102                 bzero((caddr_t)&de, DE_SIZE);
103                 de.d_reclen = DE_SIZE;
104                 de.d_fileno = (offset == 0) ? np->n_ino :
105                     (np->n_parent ? VTOSMB(np->n_parent)->n_ino : 2);
106                 if (de.d_fileno == 0)
107                         de.d_fileno = 0x7ffffffd + offset;
108                 de.d_namlen = offset + 1;
109                 de.d_name[0] = '.';
110                 de.d_name[1] = '.';
111                 de.d_name[offset + 1] = '\0';
112                 de.d_type = DT_DIR;
113                 error = uiomove((caddr_t)&de, DE_SIZE, uio);
114                 if (error)
115                         return error;
116                 offset++;
117                 uio->uio_offset += DE_SIZE;
118         }
119         if (limit == 0)
120                 return 0;
121         if (offset != np->n_dirofs || np->n_dirseq == NULL) {
122                 SMBVDEBUG("Reopening search %ld:%ld\n", offset, np->n_dirofs);
123                 if (np->n_dirseq) {
124                         smbfs_findclose(np->n_dirseq, &scred);
125                         np->n_dirseq = NULL;
126                 }
127                 np->n_dirofs = 2;
128                 error = smbfs_findopen(np, "*", 1,
129                     SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR,
130                     &scred, &ctx);
131                 if (error) {
132                         SMBVDEBUG("can not open search, error = %d", error);
133                         return error;
134                 }
135                 np->n_dirseq = ctx;
136         } else
137                 ctx = np->n_dirseq;
138         while (np->n_dirofs < offset) {
139                 error = smbfs_findnext(ctx, offset - np->n_dirofs++, &scred);
140                 if (error) {
141                         smbfs_findclose(np->n_dirseq, &scred);
142                         np->n_dirseq = NULL;
143                         return error == ENOENT ? 0 : error;
144                 }
145         }
146         error = 0;
147         for (; limit; limit--, offset++) {
148                 error = smbfs_findnext(ctx, limit, &scred);
149                 if (error)
150                         break;
151                 np->n_dirofs++;
152                 bzero((caddr_t)&de, DE_SIZE);
153                 de.d_reclen = DE_SIZE;
154                 de.d_fileno = ctx->f_attr.fa_ino;
155                 de.d_type = (ctx->f_attr.fa_attr & SMB_FA_DIR) ? DT_DIR : DT_REG;
156                 de.d_namlen = ctx->f_nmlen;
157                 bcopy(ctx->f_name, de.d_name, de.d_namlen);
158                 de.d_name[de.d_namlen] = '\0';
159                 if (smbfs_fastlookup) {
160                         error = smbfs_nget(vp->v_mount, vp, ctx->f_name,
161                             ctx->f_nmlen, &ctx->f_attr, &newvp);
162                         if (!error)
163                                 vput(newvp);
164                 }
165                 error = uiomove((caddr_t)&de, DE_SIZE, uio);
166                 if (error)
167                         break;
168         }
169         if (error == ENOENT)
170                 error = 0;
171         uio->uio_offset = offset * DE_SIZE;
172         return error;
173 }
174
175 int
176 smbfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred)
177 {
178         struct thread *td;
179         struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
180         struct smbnode *np = VTOSMB(vp);
181         struct vattr vattr;
182         struct smb_cred scred;
183         int error, lks;
184
185         /*
186          * Protect against method which is not supported for now
187          */
188         if (uiop->uio_segflg == UIO_NOCOPY)
189                 return EOPNOTSUPP;
190
191         if (vp->v_type != VREG && vp->v_type != VDIR) {
192                 SMBFSERR("vn types other than VREG or VDIR are unsupported !\n");
193                 return EIO;
194         }
195         if (uiop->uio_resid == 0)
196                 return 0;
197         if (uiop->uio_offset < 0)
198                 return EINVAL;
199 /*      if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
200                 return EFBIG;*/
201         td = uiop->uio_td;
202         if (vp->v_type == VDIR) {
203                 lks = LK_EXCLUSIVE;/*lockstatus(&vp->v_lock, td);*/
204                 if (lks == LK_SHARED)
205                         vn_lock(vp, LK_UPGRADE | LK_RETRY, td);
206                 error = smbfs_readvdir(vp, uiop, cred);
207                 if (lks == LK_SHARED)
208                         vn_lock(vp, LK_DOWNGRADE | LK_RETRY, td);
209                 return error;
210         }
211
212 /*      biosize = SSTOCN(smp->sm_share)->sc_txmax;*/
213         if (np->n_flag & NMODIFIED) {
214                 smbfs_attr_cacheremove(vp);
215                 error = VOP_GETATTR(vp, &vattr, td);
216                 if (error)
217                         return error;
218                 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
219         } else {
220                 error = VOP_GETATTR(vp, &vattr, td);
221                 if (error)
222                         return error;
223                 if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
224                         error = smbfs_vinvalbuf(vp, V_SAVE, td, 1);
225                         if (error)
226                                 return error;
227                         np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
228                 }
229         }
230         smb_makescred(&scred, td, cred);
231         return smb_read(smp->sm_share, np->n_fid, uiop, &scred);
232 }
233
234 int
235 smbfs_writevnode(struct vnode *vp, struct uio *uiop,
236                  struct ucred *cred, int ioflag)
237 {
238         struct thread *td;
239         struct smbmount *smp = VTOSMBFS(vp);
240         struct smbnode *np = VTOSMB(vp);
241         struct smb_cred scred;
242         int error = 0;
243
244         if (vp->v_type != VREG) {
245                 SMBERROR("vn types other than VREG unsupported !\n");
246                 return EIO;
247         }
248         SMBVDEBUG("ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
249         if (uiop->uio_offset < 0)
250                 return EINVAL;
251 /*      if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
252                 return (EFBIG);*/
253         td = uiop->uio_td;
254         if (ioflag & (IO_APPEND | IO_SYNC)) {
255                 if (np->n_flag & NMODIFIED) {
256                         smbfs_attr_cacheremove(vp);
257                         error = smbfs_vinvalbuf(vp, V_SAVE, td, 1);
258                         if (error)
259                                 return error;
260                 }
261                 if (ioflag & IO_APPEND) {
262 #if notyet
263                         /*
264                          * File size can be changed by another client
265                          */
266                         smbfs_attr_cacheremove(vp);
267                         error = VOP_GETATTR(vp, &vattr, td);
268                         if (error) return (error);
269 #endif
270                         uiop->uio_offset = np->n_size;
271                 }
272         }
273         if (uiop->uio_resid == 0)
274                 return 0;
275         if (td->td_proc &&
276             uiop->uio_offset + uiop->uio_resid >
277             td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
278                 psignal(td->td_proc, SIGXFSZ);
279                 return EFBIG;
280         }
281         smb_makescred(&scred, td, cred);
282         error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
283         SMBVDEBUG("after: ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
284         if (!error) {
285                 if (uiop->uio_offset > np->n_size) {
286                         np->n_size = uiop->uio_offset;
287                         vnode_pager_setsize(vp, np->n_size);
288                 }
289         }
290         return error;
291 }
292
293 /*
294  * Do an I/O operation to/from a cache block.
295  */
296 int
297 smbfs_doio(struct buf *bp, struct ucred *cr, struct thread *td)
298 {
299         struct vnode *vp = bp->b_vp;
300         struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
301         struct smbnode *np = VTOSMB(vp);
302         struct uio uio, *uiop = &uio;
303         struct iovec io;
304         struct smb_cred scred;
305         int error = 0;
306
307         uiop->uio_iov = &io;
308         uiop->uio_iovcnt = 1;
309         uiop->uio_segflg = UIO_SYSSPACE;
310         uiop->uio_td = td;
311
312         smb_makescred(&scred, td, cr);
313
314         if (bp->b_flags & B_READ) {
315             io.iov_len = uiop->uio_resid = bp->b_bcount;
316             io.iov_base = bp->b_data;
317             uiop->uio_rw = UIO_READ;
318             switch (vp->v_type) {
319               case VREG:
320                 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
321                 error = smb_read(smp->sm_share, np->n_fid, uiop, &scred);
322                 if (error)
323                         break;
324                 if (uiop->uio_resid) {
325                         int left = uiop->uio_resid;
326                         int nread = bp->b_bcount - left;
327                         if (left > 0)
328                             bzero((char *)bp->b_data + nread, left);
329                 }
330                 break;
331             default:
332                 printf("smbfs_doio:  type %x unexpected\n",vp->v_type);
333                 break;
334             };
335             if (error) {
336                 bp->b_error = error;
337                 bp->b_flags |= B_ERROR;
338             }
339         } else { /* write */
340             if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
341                 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
342
343             if (bp->b_dirtyend > bp->b_dirtyoff) {
344                 io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
345                 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
346                 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
347                 uiop->uio_rw = UIO_WRITE;
348                 error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
349
350                 /*
351                  * For an interrupted write, the buffer is still valid
352                  * and the write hasn't been pushed to the server yet,
353                  * so we can't set BIO_ERROR and report the interruption
354                  * by setting B_EINTR. For the B_ASYNC case, B_EINTR
355                  * is not relevant, so the rpc attempt is essentially
356                  * a noop.  For the case of a V3 write rpc not being
357                  * committed to stable storage, the block is still
358                  * dirty and requires either a commit rpc or another
359                  * write rpc with iomode == NFSV3WRITE_FILESYNC before
360                  * the block is reused. This is indicated by setting
361                  * the B_DELWRI and B_NEEDCOMMIT flags.
362                  */
363                 if (error == EINTR
364                     || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
365
366                         crit_enter();
367                         bp->b_flags &= ~(B_INVAL|B_NOCACHE);
368                         if ((bp->b_flags & B_ASYNC) == 0)
369                             bp->b_flags |= B_EINTR;
370                         if ((bp->b_flags & B_PAGING) == 0) {
371                             bdirty(bp);
372                             bp->b_flags &= ~B_DONE;
373                         }
374                         if ((bp->b_flags & B_ASYNC) == 0)
375                             bp->b_flags |= B_EINTR;
376                         crit_exit();
377                 } else {
378                         if (error) {
379                                 bp->b_flags |= B_ERROR;
380                                 bp->b_error = error;
381                         }
382                         bp->b_dirtyoff = bp->b_dirtyend = 0;
383                 }
384             } else {
385                 bp->b_resid = 0;
386                 biodone(bp);
387                 return 0;
388             }
389         }
390         bp->b_resid = uiop->uio_resid;
391         biodone(bp);
392         return error;
393 }
394
395 /*
396  * Vnode op for VM getpages.
397  * Wish wish .... get rid from multiple IO routines
398  *
399  * smbfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
400  *                int a_reqpage, vm_ooffset_t a_offset)
401  */
402 int
403 smbfs_getpages(struct vop_getpages_args *ap)
404 {
405 #ifdef SMBFS_RWGENERIC
406         return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
407                 ap->a_reqpage);
408 #else
409         int i, error, nextoff, size, toff, npages, count;
410         int doclose;
411         struct uio uio;
412         struct iovec iov;
413         vm_offset_t kva;
414         struct buf *bp;
415         struct vnode *vp;
416         struct thread *td = curthread;  /* XXX */
417         struct ucred *cred;
418         struct smbmount *smp;
419         struct smbnode *np;
420         struct smb_cred scred;
421         vm_page_t *pages;
422
423         KKASSERT(td->td_proc);
424
425         vp = ap->a_vp;
426         cred = td->td_proc->p_ucred;
427         np = VTOSMB(vp);
428         smp = VFSTOSMBFS(vp->v_mount);
429         pages = ap->a_m;
430         count = ap->a_count;
431
432         if (vp->v_object == NULL) {
433                 printf("smbfs_getpages: called with non-merged cache vnode??\n");
434                 return VM_PAGER_ERROR;
435         }
436         smb_makescred(&scred, td, cred);
437
438         bp = getpbuf(&smbfs_pbuf_freecnt);
439         npages = btoc(count);
440         kva = (vm_offset_t) bp->b_data;
441         pmap_qenter(kva, pages, npages);
442
443         iov.iov_base = (caddr_t) kva;
444         iov.iov_len = count;
445         uio.uio_iov = &iov;
446         uio.uio_iovcnt = 1;
447         uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
448         uio.uio_resid = count;
449         uio.uio_segflg = UIO_SYSSPACE;
450         uio.uio_rw = UIO_READ;
451         uio.uio_td = td;
452
453         /*
454          * This is kinda nasty.  Since smbfs is physically closing the
455          * fid on close(), we have to reopen it if necessary.  There are
456          * other races here too, such as if another process opens the same
457          * file while we are blocked in read. XXX
458          */
459         error = 0;
460         doclose = 0;
461         if (np->n_opencount == 0) {
462                 error = smbfs_smb_open(np, SMB_AM_OPENREAD, &scred);
463                 if (error == 0)
464                         doclose = 1;
465         }
466         if (error == 0)
467                 error = smb_read(smp->sm_share, np->n_fid, &uio, &scred);
468         if (doclose)
469                 smbfs_smb_close(smp->sm_share, np->n_fid, NULL, &scred);
470         pmap_qremove(kva, npages);
471
472         relpbuf(bp, &smbfs_pbuf_freecnt);
473
474         if (error && (uio.uio_resid == count)) {
475                 printf("smbfs_getpages: error %d\n",error);
476                 for (i = 0; i < npages; i++) {
477                         if (ap->a_reqpage != i)
478                                 vnode_pager_freepage(pages[i]);
479                 }
480                 return VM_PAGER_ERROR;
481         }
482
483         size = count - uio.uio_resid;
484
485         for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
486                 vm_page_t m;
487                 nextoff = toff + PAGE_SIZE;
488                 m = pages[i];
489
490                 m->flags &= ~PG_ZERO;
491
492                 if (nextoff <= size) {
493                         m->valid = VM_PAGE_BITS_ALL;
494                         m->dirty = 0;
495                 } else {
496                         int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1);
497                         vm_page_set_validclean(m, 0, nvalid);
498                 }
499                 
500                 if (i != ap->a_reqpage) {
501                         /*
502                          * Whether or not to leave the page activated is up in
503                          * the air, but we should put the page on a page queue
504                          * somewhere (it already is in the object).  Result:
505                          * It appears that emperical results show that
506                          * deactivating pages is best.
507                          */
508
509                         /*
510                          * Just in case someone was asking for this page we
511                          * now tell them that it is ok to use.
512                          */
513                         if (!error) {
514                                 if (m->flags & PG_WANTED)
515                                         vm_page_activate(m);
516                                 else
517                                         vm_page_deactivate(m);
518                                 vm_page_wakeup(m);
519                         } else {
520                                 vnode_pager_freepage(m);
521                         }
522                 }
523         }
524         return 0;
525 #endif /* SMBFS_RWGENERIC */
526 }
527
528 /*
529  * Vnode op for VM putpages.
530  * possible bug: all IO done in sync mode
531  * Note that vop_close always invalidate pages before close, so it's
532  * not necessary to open vnode.
533  *
534  * smbfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count, int a_sync,
535  *                int *a_rtvals, vm_ooffset_t a_offset)
536  */
537 int
538 smbfs_putpages(struct vop_putpages_args *ap)
539 {
540         int error;
541         struct vnode *vp = ap->a_vp;
542         struct thread *td = curthread;  /* XXX */
543         struct ucred *cred;
544
545 #ifdef SMBFS_RWGENERIC
546         KKASSERT(td->td_proc);
547         cred = td->td_proc->p_ucred;
548         VOP_OPEN(vp, FWRITE, cred, NULL, td);
549         error = vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
550                 ap->a_sync, ap->a_rtvals);
551         VOP_CLOSE(vp, FWRITE, cred, td);
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, td);*/
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, td);*/
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, struct thread *td, 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(td);
655                 if (error == EINTR && intrflg)
656                         return EINTR;
657         }
658         np->n_flag |= NFLUSHINPROG;
659         error = vinvalbuf(vp, flags, td, 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, td, 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 }