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