xref: /dragonfly/sys/vfs/smbfs/smbfs_io.c (revision 2e3ed54d)
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: src/sys/fs/smbfs/smbfs_io.c,v 1.3.2.3 2003/01/17 08:20:26 tjr Exp $
33  * $DragonFly: src/sys/vfs/smbfs/smbfs_io.c,v 1.19 2005/08/27 20:23:06 joerg Exp $
34  *
35  */
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/fcntl.h>
42 #include <sys/mount.h>
43 #include <sys/namei.h>
44 #include <sys/vnode.h>
45 #include <sys/dirent.h>
46 #include <sys/signalvar.h>
47 #include <sys/sysctl.h>
48 
49 #include  <machine/limits.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 <netproto/smb/smb.h>
61 #include <netproto/smb/smb_conn.h>
62 #include <netproto/smb/smb_subr.h>
63 
64 #include "smbfs.h"
65 #include "smbfs_node.h"
66 #include "smbfs_subr.h"
67 
68 #include <sys/buf.h>
69 
70 #include <sys/thread2.h>
71 
72 /*#define SMBFS_RWGENERIC*/
73 
74 extern int smbfs_pbuf_freecnt;
75 
76 static int smbfs_fastlookup = 1;
77 
78 SYSCTL_DECL(_vfs_smbfs);
79 SYSCTL_INT(_vfs_smbfs, OID_AUTO, fastlookup, CTLFLAG_RW, &smbfs_fastlookup, 0, "");
80 
81 static int
82 smbfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred)
83 {
84 	struct smb_cred scred;
85 	struct smbfs_fctx *ctx;
86 	struct vnode *newvp;
87 	struct smbnode *np = VTOSMB(vp);
88 	int error, i, offset, retval/*, *eofflag = ap->a_eofflag*/;
89 
90 	np = VTOSMB(vp);
91 	SMBVDEBUG("dirname='%s'\n", np->n_name);
92 	smb_makescred(&scred, uio->uio_td, cred);
93 
94 	if (uio->uio_resid < 0 || uio->uio_offset < 0 ||
95 	    uio->uio_offset > INT_MAX)
96 		return(EINVAL);
97 
98 	error = 0;
99 	i = 0;
100 	offset = uio->uio_offset;
101 
102 	if (uio->uio_resid > 0 && offset < 1) {
103 		if (vop_write_dirent(&error, uio, np->n_ino, DT_DIR, 1, "."))
104 			goto done;
105 		if (error)
106 			goto done;
107 		i++;
108 	}
109 
110 	if (uio->uio_resid > 0 && offset < 2) {
111 		if (vop_write_dirent(&error, uio,
112 		    np->n_parent ? VTOSMB(np->n_parent)->n_ino : 2,
113 		    DT_DIR, 2, ".."))
114 			goto done;
115 		if (error)
116 			goto done;
117 		i++;
118 	}
119 
120 	if (uio->uio_resid == 0)
121 		goto done;
122 
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 (; uio->uio_resid > 0 && !error; i++) {
150 		/*
151 		 * Overestimate the size of a record a bit, doesn't really
152 		 * hurt to be wrong here.
153 		 */
154 		error = smbfs_findnext(ctx, uio->uio_resid / _DIRENT_RECLEN(255) + 1, &scred);
155 		if (error)
156 			break;
157 		np->n_dirofs++;
158 
159 		retval = vop_write_dirent(&error, uio, ctx->f_attr.fa_ino,
160 		    (ctx->f_attr.fa_attr & SMB_FA_DIR) ? DT_DIR : DT_REG,
161 		    ctx->f_nmlen, ctx->f_name);
162 		if (retval)
163 			break;
164 		if (smbfs_fastlookup && !error) {
165 			error = smbfs_nget(vp->v_mount, vp, ctx->f_name,
166 			    ctx->f_nmlen, &ctx->f_attr, &newvp);
167 			if (!error)
168 				vput(newvp);
169 		}
170 	}
171 	if (error == ENOENT)
172 		error = 0;
173 
174 done:
175 	uio->uio_offset = i;
176 	return error;
177 }
178 
179 int
180 smbfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred)
181 {
182 	struct thread *td;
183 	struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
184 	struct smbnode *np = VTOSMB(vp);
185 	struct vattr vattr;
186 	struct smb_cred scred;
187 	int error, lks;
188 
189 	/*
190 	 * Protect against method which is not supported for now
191 	 */
192 	if (uiop->uio_segflg == UIO_NOCOPY)
193 		return EOPNOTSUPP;
194 
195 	if (vp->v_type != VREG && vp->v_type != VDIR) {
196 		SMBFSERR("vn types other than VREG or VDIR are unsupported !\n");
197 		return EIO;
198 	}
199 	if (uiop->uio_resid == 0)
200 		return 0;
201 	if (uiop->uio_offset < 0)
202 		return EINVAL;
203 /*	if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
204 		return EFBIG;*/
205 	td = uiop->uio_td;
206 	if (vp->v_type == VDIR) {
207 		lks = LK_EXCLUSIVE;/*lockstatus(&vp->v_lock, td);*/
208 		if (lks == LK_SHARED)
209 			vn_lock(vp, LK_UPGRADE | LK_RETRY, td);
210 		error = smbfs_readvdir(vp, uiop, cred);
211 		if (lks == LK_SHARED)
212 			vn_lock(vp, LK_DOWNGRADE | LK_RETRY, td);
213 		return error;
214 	}
215 
216 /*	biosize = SSTOCN(smp->sm_share)->sc_txmax;*/
217 	if (np->n_flag & NMODIFIED) {
218 		smbfs_attr_cacheremove(vp);
219 		error = VOP_GETATTR(vp, &vattr, td);
220 		if (error)
221 			return error;
222 		np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
223 	} else {
224 		error = VOP_GETATTR(vp, &vattr, td);
225 		if (error)
226 			return error;
227 		if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
228 			error = smbfs_vinvalbuf(vp, V_SAVE, td, 1);
229 			if (error)
230 				return error;
231 			np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
232 		}
233 	}
234 	smb_makescred(&scred, td, cred);
235 	return smb_read(smp->sm_share, np->n_fid, uiop, &scred);
236 }
237 
238 int
239 smbfs_writevnode(struct vnode *vp, struct uio *uiop,
240 		 struct ucred *cred, int ioflag)
241 {
242 	struct thread *td;
243 	struct smbmount *smp = VTOSMBFS(vp);
244 	struct smbnode *np = VTOSMB(vp);
245 	struct smb_cred scred;
246 	int error = 0;
247 
248 	if (vp->v_type != VREG) {
249 		SMBERROR("vn types other than VREG unsupported !\n");
250 		return EIO;
251 	}
252 	SMBVDEBUG("ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
253 	if (uiop->uio_offset < 0)
254 		return EINVAL;
255 /*	if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
256 		return (EFBIG);*/
257 	td = uiop->uio_td;
258 	if (ioflag & (IO_APPEND | IO_SYNC)) {
259 		if (np->n_flag & NMODIFIED) {
260 			smbfs_attr_cacheremove(vp);
261 			error = smbfs_vinvalbuf(vp, V_SAVE, td, 1);
262 			if (error)
263 				return error;
264 		}
265 		if (ioflag & IO_APPEND) {
266 #if notyet
267 			/*
268 			 * File size can be changed by another client
269 			 */
270 			smbfs_attr_cacheremove(vp);
271 			error = VOP_GETATTR(vp, &vattr, td);
272 			if (error) return (error);
273 #endif
274 			uiop->uio_offset = np->n_size;
275 		}
276 	}
277 	if (uiop->uio_resid == 0)
278 		return 0;
279 	if (td->td_proc &&
280 	    uiop->uio_offset + uiop->uio_resid >
281 	    td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
282 		psignal(td->td_proc, SIGXFSZ);
283 		return EFBIG;
284 	}
285 	smb_makescred(&scred, td, cred);
286 	error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
287 	SMBVDEBUG("after: ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
288 	if (!error) {
289 		if (uiop->uio_offset > np->n_size) {
290 			np->n_size = uiop->uio_offset;
291 			vnode_pager_setsize(vp, np->n_size);
292 		}
293 	}
294 	return error;
295 }
296 
297 /*
298  * Do an I/O operation to/from a cache block.
299  */
300 int
301 smbfs_doio(struct buf *bp, struct ucred *cr, struct thread *td)
302 {
303 	struct vnode *vp = bp->b_vp;
304 	struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
305 	struct smbnode *np = VTOSMB(vp);
306 	struct uio uio, *uiop = &uio;
307 	struct iovec io;
308 	struct smb_cred scred;
309 	int error = 0;
310 
311 	uiop->uio_iov = &io;
312 	uiop->uio_iovcnt = 1;
313 	uiop->uio_segflg = UIO_SYSSPACE;
314 	uiop->uio_td = td;
315 
316 	smb_makescred(&scred, td, cr);
317 
318 	if (bp->b_flags & B_READ) {
319 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
320 	    io.iov_base = bp->b_data;
321 	    uiop->uio_rw = UIO_READ;
322 	    switch (vp->v_type) {
323 	      case VREG:
324 		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
325 		error = smb_read(smp->sm_share, np->n_fid, uiop, &scred);
326 		if (error)
327 			break;
328 		if (uiop->uio_resid) {
329 			int left = uiop->uio_resid;
330 			int nread = bp->b_bcount - left;
331 			if (left > 0)
332 			    bzero((char *)bp->b_data + nread, left);
333 		}
334 		break;
335 	    default:
336 		printf("smbfs_doio:  type %x unexpected\n",vp->v_type);
337 		break;
338 	    };
339 	    if (error) {
340 		bp->b_error = error;
341 		bp->b_flags |= B_ERROR;
342 	    }
343 	} else { /* write */
344 	    if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
345 		bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
346 
347 	    if (bp->b_dirtyend > bp->b_dirtyoff) {
348 		io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
349 		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
350 		io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
351 		uiop->uio_rw = UIO_WRITE;
352 		error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
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 
370 			crit_enter();
371 			bp->b_flags &= ~(B_INVAL|B_NOCACHE);
372 			if ((bp->b_flags & B_ASYNC) == 0)
373 			    bp->b_flags |= B_EINTR;
374 			if ((bp->b_flags & B_PAGING) == 0) {
375 			    bdirty(bp);
376 			    bp->b_flags &= ~B_DONE;
377 			}
378 			if ((bp->b_flags & B_ASYNC) == 0)
379 			    bp->b_flags |= B_EINTR;
380 			crit_exit();
381 	    	} else {
382 			if (error) {
383 				bp->b_flags |= B_ERROR;
384 				bp->b_error = error;
385 			}
386 			bp->b_dirtyoff = bp->b_dirtyend = 0;
387 		}
388 	    } else {
389 		bp->b_resid = 0;
390 		biodone(bp);
391 		return 0;
392 	    }
393 	}
394 	bp->b_resid = uiop->uio_resid;
395 	biodone(bp);
396 	return error;
397 }
398 
399 /*
400  * Vnode op for VM getpages.
401  * Wish wish .... get rid from multiple IO routines
402  *
403  * smbfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
404  *		  int a_reqpage, vm_ooffset_t a_offset)
405  */
406 int
407 smbfs_getpages(struct vop_getpages_args *ap)
408 {
409 #ifdef SMBFS_RWGENERIC
410 	return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
411 		ap->a_reqpage);
412 #else
413 	int i, error, nextoff, size, toff, npages, count;
414 	int doclose;
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 = curthread;	/* XXX */
421 	struct ucred *cred;
422 	struct smbmount *smp;
423 	struct smbnode *np;
424 	struct smb_cred scred;
425 	vm_page_t *pages;
426 
427 	KKASSERT(td->td_proc);
428 
429 	vp = ap->a_vp;
430 	cred = td->td_proc->p_ucred;
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, td, cred);
441 
442 	bp = getpbuf(&smbfs_pbuf_freecnt);
443 	npages = btoc(count);
444 	kva = (vm_offset_t) bp->b_data;
445 	pmap_qenter(kva, pages, npages);
446 
447 	iov.iov_base = (caddr_t) kva;
448 	iov.iov_len = count;
449 	uio.uio_iov = &iov;
450 	uio.uio_iovcnt = 1;
451 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
452 	uio.uio_resid = count;
453 	uio.uio_segflg = UIO_SYSSPACE;
454 	uio.uio_rw = UIO_READ;
455 	uio.uio_td = td;
456 
457 	/*
458 	 * This is kinda nasty.  Since smbfs is physically closing the
459 	 * fid on close(), we have to reopen it if necessary.  There are
460 	 * other races here too, such as if another process opens the same
461 	 * file while we are blocked in read. XXX
462 	 */
463 	error = 0;
464 	doclose = 0;
465 	if (np->n_opencount == 0) {
466 		error = smbfs_smb_open(np, SMB_AM_OPENREAD, &scred);
467 		if (error == 0)
468 			doclose = 1;
469 	}
470 	if (error == 0)
471 		error = smb_read(smp->sm_share, np->n_fid, &uio, &scred);
472 	if (doclose)
473 		smbfs_smb_close(smp->sm_share, np->n_fid, NULL, &scred);
474 	pmap_qremove(kva, npages);
475 
476 	relpbuf(bp, &smbfs_pbuf_freecnt);
477 
478 	if (error && (uio.uio_resid == count)) {
479 		printf("smbfs_getpages: error %d\n",error);
480 		for (i = 0; i < npages; i++) {
481 			if (ap->a_reqpage != i)
482 				vnode_pager_freepage(pages[i]);
483 		}
484 		return VM_PAGER_ERROR;
485 	}
486 
487 	size = count - uio.uio_resid;
488 
489 	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
490 		vm_page_t m;
491 		nextoff = toff + PAGE_SIZE;
492 		m = pages[i];
493 
494 		m->flags &= ~PG_ZERO;
495 
496 		if (nextoff <= size) {
497 			m->valid = VM_PAGE_BITS_ALL;
498 			m->dirty = 0;
499 		} else {
500 			int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1);
501 			vm_page_set_validclean(m, 0, nvalid);
502 		}
503 
504 		if (i != ap->a_reqpage) {
505 			/*
506 			 * Whether or not to leave the page activated is up in
507 			 * the air, but we should put the page on a page queue
508 			 * somewhere (it already is in the object).  Result:
509 			 * It appears that emperical results show that
510 			 * deactivating pages is best.
511 			 */
512 
513 			/*
514 			 * Just in case someone was asking for this page we
515 			 * now tell them that it is ok to use.
516 			 */
517 			if (!error) {
518 				if (m->flags & PG_WANTED)
519 					vm_page_activate(m);
520 				else
521 					vm_page_deactivate(m);
522 				vm_page_wakeup(m);
523 			} else {
524 				vnode_pager_freepage(m);
525 			}
526 		}
527 	}
528 	return 0;
529 #endif /* SMBFS_RWGENERIC */
530 }
531 
532 /*
533  * Vnode op for VM putpages.
534  * possible bug: all IO done in sync mode
535  * Note that vop_close always invalidate pages before close, so it's
536  * not necessary to open vnode.
537  *
538  * smbfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count, int a_sync,
539  *		  int *a_rtvals, vm_ooffset_t a_offset)
540  */
541 int
542 smbfs_putpages(struct vop_putpages_args *ap)
543 {
544 	int error;
545 	struct vnode *vp = ap->a_vp;
546 	struct thread *td = curthread;	/* XXX */
547 	struct ucred *cred;
548 
549 #ifdef SMBFS_RWGENERIC
550 	KKASSERT(td->td_proc);
551 	cred = td->td_proc->p_ucred;
552 	VOP_OPEN(vp, FWRITE, cred, NULL, td);
553 	error = vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
554 		ap->a_sync, ap->a_rtvals);
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 doclose;
564 	int *rtvals;
565 	struct smbmount *smp;
566 	struct smbnode *np;
567 	struct smb_cred scred;
568 	vm_page_t *pages;
569 
570 	KKASSERT(td->td_proc);
571 	cred = td->td_proc->p_ucred;
572 /*	VOP_OPEN(vp, FWRITE, cred, td);*/
573 	np = VTOSMB(vp);
574 	smp = VFSTOSMBFS(vp->v_mount);
575 	pages = ap->a_m;
576 	count = ap->a_count;
577 	rtvals = ap->a_rtvals;
578 	npages = btoc(count);
579 
580 	for (i = 0; i < npages; i++) {
581 		rtvals[i] = VM_PAGER_AGAIN;
582 	}
583 
584 	bp = getpbuf(&smbfs_pbuf_freecnt);
585 	kva = (vm_offset_t) bp->b_data;
586 	pmap_qenter(kva, pages, npages);
587 
588 	iov.iov_base = (caddr_t) kva;
589 	iov.iov_len = count;
590 	uio.uio_iov = &iov;
591 	uio.uio_iovcnt = 1;
592 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
593 	uio.uio_resid = count;
594 	uio.uio_segflg = UIO_SYSSPACE;
595 	uio.uio_rw = UIO_WRITE;
596 	uio.uio_td = td;
597 	SMBVDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
598 
599 	smb_makescred(&scred, td, cred);
600 
601 	/*
602 	 * This is kinda nasty.  Since smbfs is physically closing the
603 	 * fid on close(), we have to reopen it if necessary.  There are
604 	 * other races here too, such as if another process opens the same
605 	 * file while we are blocked in read, or the file is open read-only
606 	 * XXX
607 	 */
608 	error = 0;
609 	doclose = 0;
610 	if (np->n_opencount == 0) {
611 		error = smbfs_smb_open(np, SMB_AM_OPENRW, &scred);
612 		if (error == 0)
613 			doclose = 1;
614 	}
615 	if (error == 0)
616 		error = smb_write(smp->sm_share, np->n_fid, &uio, &scred);
617 	if (doclose)
618 		smbfs_smb_close(smp->sm_share, np->n_fid, NULL, &scred);
619 /*	VOP_CLOSE(vp, FWRITE, cred, td);*/
620 	SMBVDEBUG("paged write done: %d\n", error);
621 
622 	pmap_qremove(kva, npages);
623 	relpbuf(bp, &smbfs_pbuf_freecnt);
624 
625 	if (!error) {
626 		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
627 		for (i = 0; i < nwritten; i++) {
628 			rtvals[i] = VM_PAGER_OK;
629 			pages[i]->dirty = 0;
630 		}
631 	}
632 	return rtvals[0];
633 #endif /* SMBFS_RWGENERIC */
634 }
635 
636 /*
637  * Flush and invalidate all dirty buffers. If another process is already
638  * doing the flush, just wait for completion.
639  */
640 int
641 smbfs_vinvalbuf(struct vnode *vp, int flags, struct thread *td, int intrflg)
642 {
643 	struct smbnode *np = VTOSMB(vp);
644 	int error = 0, slpflag, slptimeo;
645 
646 	if (vp->v_flag & VRECLAIMED)
647 		return 0;
648 	if (intrflg) {
649 		slpflag = PCATCH;
650 		slptimeo = 2 * hz;
651 	} else {
652 		slpflag = 0;
653 		slptimeo = 0;
654 	}
655 	while (np->n_flag & NFLUSHINPROG) {
656 		np->n_flag |= NFLUSHWANT;
657 		error = tsleep((caddr_t)&np->n_flag, 0, "smfsvinv", slptimeo);
658 		error = smb_proc_intr(td);
659 		if (error == EINTR && intrflg)
660 			return EINTR;
661 	}
662 	np->n_flag |= NFLUSHINPROG;
663 	error = vinvalbuf(vp, flags, td, slpflag, 0);
664 	while (error) {
665 		if (intrflg && (error == ERESTART || error == EINTR)) {
666 			np->n_flag &= ~NFLUSHINPROG;
667 			if (np->n_flag & NFLUSHWANT) {
668 				np->n_flag &= ~NFLUSHWANT;
669 				wakeup((caddr_t)&np->n_flag);
670 			}
671 			return EINTR;
672 		}
673 		error = vinvalbuf(vp, flags, td, slpflag, 0);
674 	}
675 	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
676 	if (np->n_flag & NFLUSHWANT) {
677 		np->n_flag &= ~NFLUSHWANT;
678 		wakeup((caddr_t)&np->n_flag);
679 	}
680 	return (error);
681 }
682