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