xref: /freebsd/sys/fs/smbfs/smbfs_io.c (revision 54ab3ed82b639b593fb0fe6db845e38a9d18970e)
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_td, 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 thread *td;
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 	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, cred, 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, cred, 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, cred, 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 smbmount *smp = VTOSMBFS(vp);
239 	struct smbnode *np = VTOSMB(vp);
240 	struct smb_cred scred;
241 	struct proc *p;
242 	struct thread *td;
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 	td = uiop->uio_td;
255 	p = td->td_proc;
256 	if (ioflag & (IO_APPEND | IO_SYNC)) {
257 		if (np->n_flag & NMODIFIED) {
258 			smbfs_attr_cacheremove(vp);
259 			error = smbfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
260 			if (error)
261 				return error;
262 		}
263 		if (ioflag & IO_APPEND) {
264 #if notyet
265 			/*
266 			 * File size can be changed by another client
267 			 */
268 			smbfs_attr_cacheremove(vp);
269 			error = VOP_GETATTR(vp, &vattr, cred, td);
270 			if (error) return (error);
271 #endif
272 			uiop->uio_offset = np->n_size;
273 		}
274 	}
275 	if (uiop->uio_resid == 0)
276 		return 0;
277 	if (p && uiop->uio_offset + uiop->uio_resid > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
278 		PROC_LOCK(td->td_proc);
279 		psignal(td->td_proc, SIGXFSZ);
280 		PROC_UNLOCK(td->td_proc);
281 		return EFBIG;
282 	}
283 	smb_makescred(&scred, td, cred);
284 	error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
285 	SMBVDEBUG("after: ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
286 	if (!error) {
287 		if (uiop->uio_offset > np->n_size) {
288 			np->n_size = uiop->uio_offset;
289 			vnode_pager_setsize(vp, np->n_size);
290 		}
291 	}
292 	return error;
293 }
294 
295 /*
296  * Do an I/O operation to/from a cache block.
297  */
298 int
299 smbfs_doio(struct buf *bp, struct ucred *cr, struct thread *td)
300 {
301 	struct vnode *vp = bp->b_vp;
302 	struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
303 	struct smbnode *np = VTOSMB(vp);
304 	struct uio uio, *uiop = &uio;
305 	struct iovec io;
306 	struct smb_cred scred;
307 	int error = 0;
308 
309 	uiop->uio_iov = &io;
310 	uiop->uio_iovcnt = 1;
311 	uiop->uio_segflg = UIO_SYSSPACE;
312 	uiop->uio_td = td;
313 
314 	smb_makescred(&scred, td, cr);
315 
316 	if (bp->b_iocmd == BIO_READ) {
317 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
318 	    io.iov_base = bp->b_data;
319 	    uiop->uio_rw = UIO_READ;
320 	    switch (vp->v_type) {
321 	      case VREG:
322 		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
323 		error = smb_read(smp->sm_share, np->n_fid, uiop, &scred);
324 		if (error)
325 			break;
326 		if (uiop->uio_resid) {
327 			int left = uiop->uio_resid;
328 			int nread = bp->b_bcount - left;
329 			if (left > 0)
330 			    bzero((char *)bp->b_data + nread, left);
331 		}
332 		break;
333 	    default:
334 		printf("smbfs_doio:  type %x unexpected\n",vp->v_type);
335 		break;
336 	    };
337 	    if (error) {
338 		bp->b_error = error;
339 		bp->b_ioflags |= BIO_ERROR;
340 	    }
341 	} else { /* write */
342 	    if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
343 		bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
344 
345 	    if (bp->b_dirtyend > bp->b_dirtyoff) {
346 		io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
347 		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
348 		io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
349 		uiop->uio_rw = UIO_WRITE;
350 		bp->b_flags |= B_WRITEINPROG;
351 		error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
352 		bp->b_flags &= ~B_WRITEINPROG;
353 
354 		/*
355 		 * For an interrupted write, the buffer is still valid
356 		 * and the write hasn't been pushed to the server yet,
357 		 * so we can't set BIO_ERROR and report the interruption
358 		 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
359 		 * is not relevant, so the rpc attempt is essentially
360 		 * a noop.  For the case of a V3 write rpc not being
361 		 * committed to stable storage, the block is still
362 		 * dirty and requires either a commit rpc or another
363 		 * write rpc with iomode == NFSV3WRITE_FILESYNC before
364 		 * the block is reused. This is indicated by setting
365 		 * the B_DELWRI and B_NEEDCOMMIT flags.
366 		 */
367     		if (error == EINTR
368 		    || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
369 			int s;
370 
371 			s = splbio();
372 			bp->b_flags &= ~(B_INVAL|B_NOCACHE);
373 			if ((bp->b_flags & B_ASYNC) == 0)
374 			    bp->b_flags |= B_EINTR;
375 			if ((bp->b_flags & B_PAGING) == 0) {
376 			    bdirty(bp);
377 			    bp->b_flags &= ~B_DONE;
378 			}
379 			if ((bp->b_flags & B_ASYNC) == 0)
380 			    bp->b_flags |= B_EINTR;
381 			splx(s);
382 	    	} else {
383 			if (error) {
384 				bp->b_ioflags |= BIO_ERROR;
385 				bp->b_error = error;
386 			}
387 			bp->b_dirtyoff = bp->b_dirtyend = 0;
388 		}
389 	    } else {
390 		bp->b_resid = 0;
391 		bufdone(bp);
392 		return 0;
393 	    }
394 	}
395 	bp->b_resid = uiop->uio_resid;
396 	bufdone(bp);
397 	return error;
398 }
399 
400 /*
401  * Vnode op for VM getpages.
402  * Wish wish .... get rid from multiple IO routines
403  */
404 int
405 smbfs_getpages(ap)
406 	struct vop_getpages_args /* {
407 		struct vnode *a_vp;
408 		vm_page_t *a_m;
409 		int a_count;
410 		int a_reqpage;
411 		vm_ooffset_t a_offset;
412 	} */ *ap;
413 {
414 #ifdef SMBFS_RWGENERIC
415 	return vop_stdgetpages(ap);
416 #else
417 	int i, error, nextoff, size, toff, npages, count;
418 	struct uio uio;
419 	struct iovec iov;
420 	vm_offset_t kva;
421 	struct buf *bp;
422 	struct vnode *vp;
423 	struct thread *td;
424 	struct ucred *cred;
425 	struct smbmount *smp;
426 	struct smbnode *np;
427 	struct smb_cred scred;
428 	vm_page_t *pages;
429 
430 	vp = ap->a_vp;
431 	td = curthread;				/* XXX */
432 	cred = td->td_proc->p_ucred;		/* XXX */
433 	np = VTOSMB(vp);
434 	smp = VFSTOSMBFS(vp->v_mount);
435 	pages = ap->a_m;
436 	count = ap->a_count;
437 
438 	if (vp->v_object == NULL) {
439 		printf("smbfs_getpages: called with non-merged cache vnode??\n");
440 		return VM_PAGER_ERROR;
441 	}
442 	smb_makescred(&scred, td, cred);
443 
444 #if __FreeBSD_version >= 400000
445 	bp = getpbuf(&smbfs_pbuf_freecnt);
446 #else
447 	bp = getpbuf();
448 #endif
449 	npages = btoc(count);
450 	kva = (vm_offset_t) bp->b_data;
451 	pmap_qenter(kva, pages, npages);
452 	cnt.v_vnodein++;
453 	cnt.v_vnodepgsin += count;
454 
455 	iov.iov_base = (caddr_t) kva;
456 	iov.iov_len = count;
457 	uio.uio_iov = &iov;
458 	uio.uio_iovcnt = 1;
459 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
460 	uio.uio_resid = count;
461 	uio.uio_segflg = UIO_SYSSPACE;
462 	uio.uio_rw = UIO_READ;
463 	uio.uio_td = td;
464 
465 	error = smb_read(smp->sm_share, np->n_fid, &uio, &scred);
466 	pmap_qremove(kva, npages);
467 
468 #if __FreeBSD_version >= 400000
469 	relpbuf(bp, &smbfs_pbuf_freecnt);
470 #else
471 	relpbuf(bp);
472 #endif
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 				vm_page_free(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 			vm_page_undirty(m);
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 				vm_page_free(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 int
535 smbfs_putpages(ap)
536 	struct vop_putpages_args /* {
537 		struct vnode *a_vp;
538 		vm_page_t *a_m;
539 		int a_count;
540 		int a_sync;
541 		int *a_rtvals;
542 		vm_ooffset_t a_offset;
543 	} */ *ap;
544 {
545 	int error;
546 	struct vnode *vp = ap->a_vp;
547 	struct thread *td;
548 	struct ucred *cred;
549 
550 #ifdef SMBFS_RWGENERIC
551 	td = curthread;			/* XXX */
552 	cred = td->td_proc->p_ucred;	/* XXX */
553 	VOP_OPEN(vp, FWRITE, cred, td);
554 	error = vop_stdputpages(ap);
555 	VOP_CLOSE(vp, FWRITE, cred, td);
556 	return error;
557 #else
558 	struct uio uio;
559 	struct iovec iov;
560 	vm_offset_t kva;
561 	struct buf *bp;
562 	int i, npages, count;
563 	int *rtvals;
564 	struct smbmount *smp;
565 	struct smbnode *np;
566 	struct smb_cred scred;
567 	vm_page_t *pages;
568 
569 	td = curthread;			/* XXX */
570 	cred = td->td_proc->p_ucred;	/* XXX */
571 /*	VOP_OPEN(vp, FWRITE, cred, td);*/
572 	np = VTOSMB(vp);
573 	smp = VFSTOSMBFS(vp->v_mount);
574 	pages = ap->a_m;
575 	count = ap->a_count;
576 	rtvals = ap->a_rtvals;
577 	npages = btoc(count);
578 
579 	for (i = 0; i < npages; i++) {
580 		rtvals[i] = VM_PAGER_AGAIN;
581 	}
582 
583 #if __FreeBSD_version >= 400000
584 	bp = getpbuf(&smbfs_pbuf_freecnt);
585 #else
586 	bp = getpbuf();
587 #endif
588 	kva = (vm_offset_t) bp->b_data;
589 	pmap_qenter(kva, pages, npages);
590 	cnt.v_vnodeout++;
591 	cnt.v_vnodepgsout += count;
592 
593 	iov.iov_base = (caddr_t) kva;
594 	iov.iov_len = count;
595 	uio.uio_iov = &iov;
596 	uio.uio_iovcnt = 1;
597 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
598 	uio.uio_resid = count;
599 	uio.uio_segflg = UIO_SYSSPACE;
600 	uio.uio_rw = UIO_WRITE;
601 	uio.uio_td = td;
602 	SMBVDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
603 
604 	smb_makescred(&scred, td, cred);
605 	error = smb_write(smp->sm_share, np->n_fid, &uio, &scred);
606 /*	VOP_CLOSE(vp, FWRITE, cred, td);*/
607 	SMBVDEBUG("paged write done: %d\n", error);
608 
609 	pmap_qremove(kva, npages);
610 #if __FreeBSD_version >= 400000
611 	relpbuf(bp, &smbfs_pbuf_freecnt);
612 #else
613 	relpbuf(bp);
614 #endif
615 
616 	if (!error) {
617 		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
618 		for (i = 0; i < nwritten; i++) {
619 			rtvals[i] = VM_PAGER_OK;
620 			vm_page_undirty(pages[i]);
621 		}
622 	}
623 	return rtvals[0];
624 #endif /* SMBFS_RWGENERIC */
625 }
626 
627 /*
628  * Flush and invalidate all dirty buffers. If another process is already
629  * doing the flush, just wait for completion.
630  */
631 int
632 smbfs_vinvalbuf(vp, flags, cred, td, intrflg)
633 	struct vnode *vp;
634 	int flags;
635 	struct ucred *cred;
636 	struct thread *td;
637 	int intrflg;
638 {
639 	struct smbnode *np = VTOSMB(vp);
640 	int error = 0, slpflag, slptimeo;
641 
642 	if (vp->v_flag & VXLOCK)
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, PRIBIO + 2, "smfsvinv", slptimeo);
654 		error = smb_proc_intr(td->td_proc);
655 		if (error == EINTR && intrflg)
656 			return EINTR;
657 	}
658 	np->n_flag |= NFLUSHINPROG;
659 	error = vinvalbuf(vp, flags, cred, 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, cred, 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 }
678