xref: /freebsd/sys/fs/smbfs/smbfs_io.c (revision 41466b50c1d5bfd1cf6adaae547a579a75d7c04e)
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 		PROC_LOCK(p);
277 		psignal(p, SIGXFSZ);
278 		PROC_UNLOCK(p);
279 		return EFBIG;
280 	}
281 	smb_makescred(&scred, p, 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 proc *p)
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_procp = p;
311 
312 	smb_makescred(&scred, p, cr);
313 
314 	if (bp->b_iocmd == BIO_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_ioflags |= BIO_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 		bp->b_flags |= B_WRITEINPROG;
349 		error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
350 		bp->b_flags &= ~B_WRITEINPROG;
351 
352 		/*
353 		 * For an interrupted write, the buffer is still valid
354 		 * and the write hasn't been pushed to the server yet,
355 		 * so we can't set BIO_ERROR and report the interruption
356 		 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
357 		 * is not relevant, so the rpc attempt is essentially
358 		 * a noop.  For the case of a V3 write rpc not being
359 		 * committed to stable storage, the block is still
360 		 * dirty and requires either a commit rpc or another
361 		 * write rpc with iomode == NFSV3WRITE_FILESYNC before
362 		 * the block is reused. This is indicated by setting
363 		 * the B_DELWRI and B_NEEDCOMMIT flags.
364 		 */
365     		if (error == EINTR
366 		    || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
367 			int s;
368 
369 			s = splbio();
370 			bp->b_flags &= ~(B_INVAL|B_NOCACHE);
371 			if ((bp->b_flags & B_ASYNC) == 0)
372 			    bp->b_flags |= B_EINTR;
373 			if ((bp->b_flags & B_PAGING) == 0) {
374 			    bdirty(bp);
375 			    bp->b_flags &= ~B_DONE;
376 			}
377 			if ((bp->b_flags & B_ASYNC) == 0)
378 			    bp->b_flags |= B_EINTR;
379 			splx(s);
380 	    	} else {
381 			if (error) {
382 				bp->b_ioflags |= BIO_ERROR;
383 				bp->b_error = error;
384 			}
385 			bp->b_dirtyoff = bp->b_dirtyend = 0;
386 		}
387 	    } else {
388 		bp->b_resid = 0;
389 		bufdone(bp);
390 		return 0;
391 	    }
392 	}
393 	bp->b_resid = uiop->uio_resid;
394 	bufdone(bp);
395 	return error;
396 }
397 
398 /*
399  * Vnode op for VM getpages.
400  * Wish wish .... get rid from multiple IO routines
401  */
402 int
403 smbfs_getpages(ap)
404 	struct vop_getpages_args /* {
405 		struct vnode *a_vp;
406 		vm_page_t *a_m;
407 		int a_count;
408 		int a_reqpage;
409 		vm_ooffset_t a_offset;
410 	} */ *ap;
411 {
412 #ifdef SMBFS_RWGENERIC
413 	return vop_stdgetpages(ap);
414 #else
415 	int i, error, nextoff, size, toff, npages, count;
416 	struct uio uio;
417 	struct iovec iov;
418 	vm_offset_t kva;
419 	struct buf *bp;
420 	struct vnode *vp;
421 	struct proc *p;
422 	struct ucred *cred;
423 	struct smbmount *smp;
424 	struct smbnode *np;
425 	struct smb_cred scred;
426 	vm_page_t *pages;
427 
428 	vp = ap->a_vp;
429 	p = curproc;				/* XXX */
430 	cred = curproc->p_ucred;		/* XXX */
431 	np = VTOSMB(vp);
432 	smp = VFSTOSMBFS(vp->v_mount);
433 	pages = ap->a_m;
434 	count = ap->a_count;
435 
436 	if (vp->v_object == NULL) {
437 		printf("smbfs_getpages: called with non-merged cache vnode??\n");
438 		return VM_PAGER_ERROR;
439 	}
440 	smb_makescred(&scred, p, cred);
441 
442 #if __FreeBSD_version >= 400000
443 	bp = getpbuf(&smbfs_pbuf_freecnt);
444 #else
445 	bp = getpbuf();
446 #endif
447 	npages = btoc(count);
448 	kva = (vm_offset_t) bp->b_data;
449 	pmap_qenter(kva, pages, npages);
450 	cnt.v_vnodein++;
451 	cnt.v_vnodepgsin += count;
452 
453 	iov.iov_base = (caddr_t) kva;
454 	iov.iov_len = count;
455 	uio.uio_iov = &iov;
456 	uio.uio_iovcnt = 1;
457 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
458 	uio.uio_resid = count;
459 	uio.uio_segflg = UIO_SYSSPACE;
460 	uio.uio_rw = UIO_READ;
461 	uio.uio_procp = p;
462 
463 	error = smb_read(smp->sm_share, np->n_fid, &uio, &scred);
464 	pmap_qremove(kva, npages);
465 
466 #if __FreeBSD_version >= 400000
467 	relpbuf(bp, &smbfs_pbuf_freecnt);
468 #else
469 	relpbuf(bp);
470 #endif
471 
472 	if (error && (uio.uio_resid == count)) {
473 		printf("smbfs_getpages: error %d\n",error);
474 		for (i = 0; i < npages; i++) {
475 			if (ap->a_reqpage != i)
476 				vm_page_free(pages[i]);
477 		}
478 		return VM_PAGER_ERROR;
479 	}
480 
481 	size = count - uio.uio_resid;
482 
483 	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
484 		vm_page_t m;
485 		nextoff = toff + PAGE_SIZE;
486 		m = pages[i];
487 
488 		m->flags &= ~PG_ZERO;
489 
490 		if (nextoff <= size) {
491 			m->valid = VM_PAGE_BITS_ALL;
492 			vm_page_undirty(m);
493 		} else {
494 			int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1);
495 			vm_page_set_validclean(m, 0, nvalid);
496 		}
497 
498 		if (i != ap->a_reqpage) {
499 			/*
500 			 * Whether or not to leave the page activated is up in
501 			 * the air, but we should put the page on a page queue
502 			 * somewhere (it already is in the object).  Result:
503 			 * It appears that emperical results show that
504 			 * deactivating pages is best.
505 			 */
506 
507 			/*
508 			 * Just in case someone was asking for this page we
509 			 * now tell them that it is ok to use.
510 			 */
511 			if (!error) {
512 				if (m->flags & PG_WANTED)
513 					vm_page_activate(m);
514 				else
515 					vm_page_deactivate(m);
516 				vm_page_wakeup(m);
517 			} else {
518 				vm_page_free(m);
519 			}
520 		}
521 	}
522 	return 0;
523 #endif /* SMBFS_RWGENERIC */
524 }
525 
526 /*
527  * Vnode op for VM putpages.
528  * possible bug: all IO done in sync mode
529  * Note that vop_close always invalidate pages before close, so it's
530  * not necessary to open vnode.
531  */
532 int
533 smbfs_putpages(ap)
534 	struct vop_putpages_args /* {
535 		struct vnode *a_vp;
536 		vm_page_t *a_m;
537 		int a_count;
538 		int a_sync;
539 		int *a_rtvals;
540 		vm_ooffset_t a_offset;
541 	} */ *ap;
542 {
543 	int error;
544 	struct vnode *vp = ap->a_vp;
545 	struct proc *p;
546 	struct ucred *cred;
547 
548 #ifdef SMBFS_RWGENERIC
549 	p = curproc;			/* XXX */
550 	cred = p->p_ucred;		/* XXX */
551 	VOP_OPEN(vp, FWRITE, cred, p);
552 	error = vop_stdputpages(ap);
553 	VOP_CLOSE(vp, FWRITE, cred, p);
554 	return error;
555 #else
556 	struct uio uio;
557 	struct iovec iov;
558 	vm_offset_t kva;
559 	struct buf *bp;
560 	int i, npages, count;
561 	int *rtvals;
562 	struct smbmount *smp;
563 	struct smbnode *np;
564 	struct smb_cred scred;
565 	vm_page_t *pages;
566 
567 	p = curproc;			/* XXX */
568 	cred = p->p_ucred;		/* XXX */
569 /*	VOP_OPEN(vp, FWRITE, cred, p);*/
570 	np = VTOSMB(vp);
571 	smp = VFSTOSMBFS(vp->v_mount);
572 	pages = ap->a_m;
573 	count = ap->a_count;
574 	rtvals = ap->a_rtvals;
575 	npages = btoc(count);
576 
577 	for (i = 0; i < npages; i++) {
578 		rtvals[i] = VM_PAGER_AGAIN;
579 	}
580 
581 #if __FreeBSD_version >= 400000
582 	bp = getpbuf(&smbfs_pbuf_freecnt);
583 #else
584 	bp = getpbuf();
585 #endif
586 	kva = (vm_offset_t) bp->b_data;
587 	pmap_qenter(kva, pages, npages);
588 	cnt.v_vnodeout++;
589 	cnt.v_vnodepgsout += count;
590 
591 	iov.iov_base = (caddr_t) kva;
592 	iov.iov_len = count;
593 	uio.uio_iov = &iov;
594 	uio.uio_iovcnt = 1;
595 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
596 	uio.uio_resid = count;
597 	uio.uio_segflg = UIO_SYSSPACE;
598 	uio.uio_rw = UIO_WRITE;
599 	uio.uio_procp = p;
600 	SMBVDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
601 
602 	smb_makescred(&scred, p, cred);
603 	error = smb_write(smp->sm_share, np->n_fid, &uio, &scred);
604 /*	VOP_CLOSE(vp, FWRITE, cred, p);*/
605 	SMBVDEBUG("paged write done: %d\n", error);
606 
607 	pmap_qremove(kva, npages);
608 #if __FreeBSD_version >= 400000
609 	relpbuf(bp, &smbfs_pbuf_freecnt);
610 #else
611 	relpbuf(bp);
612 #endif
613 
614 	if (!error) {
615 		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
616 		for (i = 0; i < nwritten; i++) {
617 			rtvals[i] = VM_PAGER_OK;
618 			vm_page_undirty(pages[i]);
619 		}
620 	}
621 	return rtvals[0];
622 #endif /* SMBFS_RWGENERIC */
623 }
624 
625 /*
626  * Flush and invalidate all dirty buffers. If another process is already
627  * doing the flush, just wait for completion.
628  */
629 int
630 smbfs_vinvalbuf(vp, flags, cred, p, intrflg)
631 	struct vnode *vp;
632 	int flags;
633 	struct ucred *cred;
634 	struct proc *p;
635 	int intrflg;
636 {
637 	struct smbnode *np = VTOSMB(vp);
638 	int error = 0, slpflag, slptimeo;
639 
640 	if (vp->v_flag & VXLOCK)
641 		return 0;
642 	if (intrflg) {
643 		slpflag = PCATCH;
644 		slptimeo = 2 * hz;
645 	} else {
646 		slpflag = 0;
647 		slptimeo = 0;
648 	}
649 	while (np->n_flag & NFLUSHINPROG) {
650 		np->n_flag |= NFLUSHWANT;
651 		error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "smfsvinv", slptimeo);
652 		error = smb_proc_intr(p);
653 		if (error == EINTR && intrflg)
654 			return EINTR;
655 	}
656 	np->n_flag |= NFLUSHINPROG;
657 	error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
658 	while (error) {
659 		if (intrflg && (error == ERESTART || error == EINTR)) {
660 			np->n_flag &= ~NFLUSHINPROG;
661 			if (np->n_flag & NFLUSHWANT) {
662 				np->n_flag &= ~NFLUSHWANT;
663 				wakeup((caddr_t)&np->n_flag);
664 			}
665 			return EINTR;
666 		}
667 		error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
668 	}
669 	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
670 	if (np->n_flag & NFLUSHWANT) {
671 		np->n_flag &= ~NFLUSHWANT;
672 		wakeup((caddr_t)&np->n_flag);
673 	}
674 	return (error);
675 }
676