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