xref: /freebsd/sys/fs/smbfs/smbfs_io.c (revision 3f36e6f21a32fb32725b0e538fc46fb239bf7564)
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 #include <vm/vm_page.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_pager.h>
56 #include <vm/vnode_pager.h>
57 /*
58 #include <sys/ioccom.h>
59 */
60 #include <netsmb/smb.h>
61 #include <netsmb/smb_conn.h>
62 #include <netsmb/smb_subr.h>
63 
64 #include <fs/smbfs/smbfs.h>
65 #include <fs/smbfs/smbfs_node.h>
66 #include <fs/smbfs/smbfs_subr.h>
67 
68 /*#define SMBFS_RWGENERIC*/
69 
70 extern int smbfs_pbuf_freecnt;
71 
72 static int smbfs_fastlookup = 1;
73 
74 SYSCTL_DECL(_vfs_smbfs);
75 SYSCTL_INT(_vfs_smbfs, OID_AUTO, fastlookup, CTLFLAG_RW, &smbfs_fastlookup, 0, "");
76 
77 
78 #define DE_SIZE	(sizeof(struct dirent))
79 
80 static int
81 smbfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred)
82 {
83 	struct dirent de;
84 	struct componentname cn;
85 	struct smb_cred scred;
86 	struct smbfs_fctx *ctx;
87 	struct vnode *newvp;
88 	struct smbnode *np = VTOSMB(vp);
89 	int error/*, *eofflag = ap->a_eofflag*/;
90 	long offset, limit;
91 
92 	np = VTOSMB(vp);
93 	SMBVDEBUG("dirname='%s'\n", np->n_name);
94 	smb_makescred(&scred, uio->uio_td, cred);
95 	offset = uio->uio_offset / DE_SIZE; 	/* offset in the directory */
96 	limit = uio->uio_resid / DE_SIZE;
97 	if (uio->uio_resid < DE_SIZE || uio->uio_offset < 0)
98 		return EINVAL;
99 	while (limit && offset < 2) {
100 		limit--;
101 		bzero((caddr_t)&de, DE_SIZE);
102 		de.d_reclen = DE_SIZE;
103 		de.d_fileno = (offset == 0) ? np->n_ino :
104 		    (np->n_parent ? np->n_parent->n_ino : 2);
105 		if (de.d_fileno == 0)
106 			de.d_fileno = 0x7ffffffd + offset;
107 		de.d_namlen = offset + 1;
108 		de.d_name[0] = '.';
109 		de.d_name[1] = '.';
110 		de.d_name[offset + 1] = '\0';
111 		de.d_type = DT_DIR;
112 		error = uiomove((caddr_t)&de, DE_SIZE, uio);
113 		if (error)
114 			return error;
115 		offset++;
116 		uio->uio_offset += DE_SIZE;
117 	}
118 	if (limit == 0)
119 		return 0;
120 	if (offset != np->n_dirofs || np->n_dirseq == NULL) {
121 		SMBVDEBUG("Reopening search %ld:%ld\n", offset, np->n_dirofs);
122 		if (np->n_dirseq) {
123 			smbfs_findclose(np->n_dirseq, &scred);
124 			np->n_dirseq = NULL;
125 		}
126 		np->n_dirofs = 2;
127 		error = smbfs_findopen(np, "*", 1,
128 		    SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR,
129 		    &scred, &ctx);
130 		if (error) {
131 			SMBVDEBUG("can not open search, error = %d", error);
132 			return error;
133 		}
134 		np->n_dirseq = ctx;
135 	} else
136 		ctx = np->n_dirseq;
137 	while (np->n_dirofs < offset) {
138 		error = smbfs_findnext(ctx, offset - np->n_dirofs++, &scred);
139 		if (error) {
140 			smbfs_findclose(np->n_dirseq, &scred);
141 			np->n_dirseq = NULL;
142 			return error == ENOENT ? 0 : error;
143 		}
144 	}
145 	error = 0;
146 	for (; limit; limit--, offset++) {
147 		error = smbfs_findnext(ctx, limit, &scred);
148 		if (error)
149 			break;
150 		np->n_dirofs++;
151 		bzero((caddr_t)&de, DE_SIZE);
152 		de.d_reclen = DE_SIZE;
153 		de.d_fileno = ctx->f_attr.fa_ino;
154 		de.d_type = (ctx->f_attr.fa_attr & SMB_FA_DIR) ? DT_DIR : DT_REG;
155 		de.d_namlen = ctx->f_nmlen;
156 		bcopy(ctx->f_name, de.d_name, de.d_namlen);
157 		de.d_name[de.d_namlen] = '\0';
158 		if (smbfs_fastlookup) {
159 			error = smbfs_nget(vp->v_mount, vp, ctx->f_name,
160 			    ctx->f_nmlen, &ctx->f_attr, &newvp);
161 			if (!error) {
162 				cn.cn_nameptr = de.d_name;
163 				cn.cn_namelen = de.d_namlen;
164 		    		cache_enter(vp, newvp, &cn);
165 				vput(newvp);
166 			}
167 		}
168 		error = uiomove((caddr_t)&de, DE_SIZE, uio);
169 		if (error)
170 			break;
171 	}
172 	if (error == ENOENT)
173 		error = 0;
174 	uio->uio_offset = offset * DE_SIZE;
175 	return error;
176 }
177 
178 int
179 smbfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred)
180 {
181 	struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
182 	struct smbnode *np = VTOSMB(vp);
183 	struct thread *td;
184 	struct vattr vattr;
185 	struct smb_cred scred;
186 	int error, lks;
187 
188 	if (vp->v_type != VREG && vp->v_type != VDIR) {
189 		SMBFSERR("vn types other than VREG or VDIR are unsupported !\n");
190 		return EIO;
191 	}
192 	if (uiop->uio_resid == 0)
193 		return 0;
194 	if (uiop->uio_offset < 0)
195 		return EINVAL;
196 /*	if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
197 		return EFBIG;*/
198 	td = uiop->uio_td;
199 	if (vp->v_type == VDIR) {
200 		lks = LK_EXCLUSIVE;/*lockstatus(&vp->v_lock, td);*/
201 		if (lks == LK_SHARED)
202 			vn_lock(vp, LK_UPGRADE | LK_RETRY, td);
203 		error = smbfs_readvdir(vp, uiop, cred);
204 		if (lks == LK_SHARED)
205 			vn_lock(vp, LK_DOWNGRADE | LK_RETRY, td);
206 		return error;
207 	}
208 
209 /*	biosize = SSTOCN(smp->sm_share)->sc_txmax;*/
210 	if (np->n_flag & NMODIFIED) {
211 		smbfs_attr_cacheremove(vp);
212 		error = VOP_GETATTR(vp, &vattr, cred, td);
213 		if (error)
214 			return error;
215 		np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
216 	} else {
217 		error = VOP_GETATTR(vp, &vattr, cred, td);
218 		if (error)
219 			return error;
220 		if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
221 			error = smbfs_vinvalbuf(vp, V_SAVE, cred, td, 1);
222 			if (error)
223 				return error;
224 			np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
225 		}
226 	}
227 	smb_makescred(&scred, td, cred);
228 	return smb_read(smp->sm_share, np->n_fid, uiop, &scred);
229 }
230 
231 int
232 smbfs_writevnode(struct vnode *vp, struct uio *uiop,
233 	struct ucred *cred, int ioflag)
234 {
235 	struct smbmount *smp = VTOSMBFS(vp);
236 	struct smbnode *np = VTOSMB(vp);
237 	struct smb_cred scred;
238 	struct proc *p;
239 	struct thread *td;
240 	int error = 0;
241 
242 	if (vp->v_type != VREG) {
243 		SMBERROR("vn types other than VREG unsupported !\n");
244 		return EIO;
245 	}
246 	SMBVDEBUG("ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
247 	if (uiop->uio_offset < 0)
248 		return EINVAL;
249 /*	if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
250 		return (EFBIG);*/
251 	td = uiop->uio_td;
252 	p = td->td_proc;
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, td, 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, td);
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 		PROC_LOCK(td->td_proc);
276 		psignal(td->td_proc, SIGXFSZ);
277 		PROC_UNLOCK(td->td_proc);
278 		return EFBIG;
279 	}
280 	smb_makescred(&scred, td, 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 thread *td)
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_td = td;
310 
311 	smb_makescred(&scred, td, 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 thread *td;
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 	td = curthread;				/* XXX */
429 	cred = td->td_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, td, cred);
440 
441 	bp = getpbuf(&smbfs_pbuf_freecnt);
442 
443 	npages = btoc(count);
444 	kva = (vm_offset_t) bp->b_data;
445 	pmap_qenter(kva, pages, npages);
446 	cnt.v_vnodein++;
447 	cnt.v_vnodepgsin += count;
448 
449 	iov.iov_base = (caddr_t) kva;
450 	iov.iov_len = count;
451 	uio.uio_iov = &iov;
452 	uio.uio_iovcnt = 1;
453 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
454 	uio.uio_resid = count;
455 	uio.uio_segflg = UIO_SYSSPACE;
456 	uio.uio_rw = UIO_READ;
457 	uio.uio_td = td;
458 
459 	error = smb_read(smp->sm_share, np->n_fid, &uio, &scred);
460 	pmap_qremove(kva, npages);
461 
462 	relpbuf(bp, &smbfs_pbuf_freecnt);
463 
464 	if (error && (uio.uio_resid == count)) {
465 		printf("smbfs_getpages: error %d\n",error);
466 		for (i = 0; i < npages; i++) {
467 			if (ap->a_reqpage != i)
468 				vm_page_free(pages[i]);
469 		}
470 		return VM_PAGER_ERROR;
471 	}
472 
473 	size = count - uio.uio_resid;
474 
475 	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
476 		vm_page_t m;
477 		nextoff = toff + PAGE_SIZE;
478 		m = pages[i];
479 
480 		m->flags &= ~PG_ZERO;
481 
482 		if (nextoff <= size) {
483 			m->valid = VM_PAGE_BITS_ALL;
484 			vm_page_undirty(m);
485 		} else {
486 			int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1);
487 			vm_page_set_validclean(m, 0, nvalid);
488 		}
489 
490 		if (i != ap->a_reqpage) {
491 			/*
492 			 * Whether or not to leave the page activated is up in
493 			 * the air, but we should put the page on a page queue
494 			 * somewhere (it already is in the object).  Result:
495 			 * It appears that emperical results show that
496 			 * deactivating pages is best.
497 			 */
498 
499 			/*
500 			 * Just in case someone was asking for this page we
501 			 * now tell them that it is ok to use.
502 			 */
503 			if (!error) {
504 				if (m->flags & PG_WANTED)
505 					vm_page_activate(m);
506 				else
507 					vm_page_deactivate(m);
508 				vm_page_wakeup(m);
509 			} else {
510 				vm_page_free(m);
511 			}
512 		}
513 	}
514 	return 0;
515 #endif /* SMBFS_RWGENERIC */
516 }
517 
518 /*
519  * Vnode op for VM putpages.
520  * possible bug: all IO done in sync mode
521  * Note that vop_close always invalidate pages before close, so it's
522  * not necessary to open vnode.
523  */
524 int
525 smbfs_putpages(ap)
526 	struct vop_putpages_args /* {
527 		struct vnode *a_vp;
528 		vm_page_t *a_m;
529 		int a_count;
530 		int a_sync;
531 		int *a_rtvals;
532 		vm_ooffset_t a_offset;
533 	} */ *ap;
534 {
535 	int error;
536 	struct vnode *vp = ap->a_vp;
537 	struct thread *td;
538 	struct ucred *cred;
539 
540 #ifdef SMBFS_RWGENERIC
541 	td = curthread;			/* XXX */
542 	cred = td->td_ucred;		/* XXX */
543 	VOP_OPEN(vp, FWRITE, cred, td);
544 	error = vop_stdputpages(ap);
545 	VOP_CLOSE(vp, FWRITE, cred, td);
546 	return error;
547 #else
548 	struct uio uio;
549 	struct iovec iov;
550 	vm_offset_t kva;
551 	struct buf *bp;
552 	int i, npages, count;
553 	int *rtvals;
554 	struct smbmount *smp;
555 	struct smbnode *np;
556 	struct smb_cred scred;
557 	vm_page_t *pages;
558 
559 	td = curthread;			/* XXX */
560 	cred = td->td_ucred;		/* XXX */
561 /*	VOP_OPEN(vp, FWRITE, cred, td);*/
562 	np = VTOSMB(vp);
563 	smp = VFSTOSMBFS(vp->v_mount);
564 	pages = ap->a_m;
565 	count = ap->a_count;
566 	rtvals = ap->a_rtvals;
567 	npages = btoc(count);
568 
569 	for (i = 0; i < npages; i++) {
570 		rtvals[i] = VM_PAGER_AGAIN;
571 	}
572 
573 	bp = getpbuf(&smbfs_pbuf_freecnt);
574 
575 	kva = (vm_offset_t) bp->b_data;
576 	pmap_qenter(kva, pages, npages);
577 	cnt.v_vnodeout++;
578 	cnt.v_vnodepgsout += count;
579 
580 	iov.iov_base = (caddr_t) kva;
581 	iov.iov_len = count;
582 	uio.uio_iov = &iov;
583 	uio.uio_iovcnt = 1;
584 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
585 	uio.uio_resid = count;
586 	uio.uio_segflg = UIO_SYSSPACE;
587 	uio.uio_rw = UIO_WRITE;
588 	uio.uio_td = td;
589 	SMBVDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
590 
591 	smb_makescred(&scred, td, cred);
592 	error = smb_write(smp->sm_share, np->n_fid, &uio, &scred);
593 /*	VOP_CLOSE(vp, FWRITE, cred, td);*/
594 	SMBVDEBUG("paged write done: %d\n", error);
595 
596 	pmap_qremove(kva, npages);
597 
598 	relpbuf(bp, &smbfs_pbuf_freecnt);
599 
600 	if (!error) {
601 		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
602 		for (i = 0; i < nwritten; i++) {
603 			rtvals[i] = VM_PAGER_OK;
604 			vm_page_undirty(pages[i]);
605 		}
606 	}
607 	return rtvals[0];
608 #endif /* SMBFS_RWGENERIC */
609 }
610 
611 /*
612  * Flush and invalidate all dirty buffers. If another process is already
613  * doing the flush, just wait for completion.
614  */
615 int
616 smbfs_vinvalbuf(vp, flags, cred, td, intrflg)
617 	struct vnode *vp;
618 	int flags;
619 	struct ucred *cred;
620 	struct thread *td;
621 	int intrflg;
622 {
623 	struct smbnode *np = VTOSMB(vp);
624 	int error = 0, slpflag, slptimeo;
625 
626 	if (vp->v_flag & VXLOCK)
627 		return 0;
628 	if (intrflg) {
629 		slpflag = PCATCH;
630 		slptimeo = 2 * hz;
631 	} else {
632 		slpflag = 0;
633 		slptimeo = 0;
634 	}
635 	while (np->n_flag & NFLUSHINPROG) {
636 		np->n_flag |= NFLUSHWANT;
637 		error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "smfsvinv", slptimeo);
638 		error = smb_proc_intr(td->td_proc);
639 		if (error == EINTR && intrflg)
640 			return EINTR;
641 	}
642 	np->n_flag |= NFLUSHINPROG;
643 	error = vinvalbuf(vp, flags, cred, td, slpflag, 0);
644 	while (error) {
645 		if (intrflg && (error == ERESTART || error == EINTR)) {
646 			np->n_flag &= ~NFLUSHINPROG;
647 			if (np->n_flag & NFLUSHWANT) {
648 				np->n_flag &= ~NFLUSHWANT;
649 				wakeup((caddr_t)&np->n_flag);
650 			}
651 			return EINTR;
652 		}
653 		error = vinvalbuf(vp, flags, cred, td, slpflag, 0);
654 	}
655 	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
656 	if (np->n_flag & NFLUSHWANT) {
657 		np->n_flag &= ~NFLUSHWANT;
658 		wakeup((caddr_t)&np->n_flag);
659 	}
660 	return (error);
661 }
662