xref: /dflybsd-src/sys/vfs/smbfs/smbfs_io.c (revision 17ea22213f86a5c5966c1e6bf8e95f022ebb92b9)
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.15 2005/01/08 18:57:48 dillon 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 <vm/vm.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_pager.h>
54 #include <vm/vnode_pager.h>
55 /*
56 #include <sys/ioccom.h>
57 */
58 #include <netproto/smb/smb.h>
59 #include <netproto/smb/smb_conn.h>
60 #include <netproto/smb/smb_subr.h>
61 
62 #include "smbfs.h"
63 #include "smbfs_node.h"
64 #include "smbfs_subr.h"
65 
66 #include <sys/buf.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 smb_cred scred;
85 	struct smbfs_fctx *ctx;
86 	struct vnode *newvp;
87 	struct smbnode *np = VTOSMB(vp);
88 	int error/*, *eofflag = ap->a_eofflag*/;
89 	long offset, limit;
90 
91 	np = VTOSMB(vp);
92 	SMBVDEBUG("dirname='%s'\n", np->n_name);
93 	smb_makescred(&scred, uio->uio_td, cred);
94 	offset = uio->uio_offset / DE_SIZE; 	/* offset in the directory */
95 	limit = uio->uio_resid / DE_SIZE;
96 	if (uio->uio_resid < DE_SIZE || uio->uio_offset < 0)
97 		return EINVAL;
98 	while (limit && offset < 2) {
99 		limit--;
100 		bzero((caddr_t)&de, DE_SIZE);
101 		de.d_reclen = DE_SIZE;
102 		de.d_fileno = (offset == 0) ? np->n_ino :
103 		    (np->n_parent ? VTOSMB(np->n_parent)->n_ino : 2);
104 		if (de.d_fileno == 0)
105 			de.d_fileno = 0x7ffffffd + offset;
106 		de.d_namlen = offset + 1;
107 		de.d_name[0] = '.';
108 		de.d_name[1] = '.';
109 		de.d_name[offset + 1] = '\0';
110 		de.d_type = DT_DIR;
111 		error = uiomove((caddr_t)&de, DE_SIZE, uio);
112 		if (error)
113 			return error;
114 		offset++;
115 		uio->uio_offset += DE_SIZE;
116 	}
117 	if (limit == 0)
118 		return 0;
119 	if (offset != np->n_dirofs || np->n_dirseq == NULL) {
120 		SMBVDEBUG("Reopening search %ld:%ld\n", offset, np->n_dirofs);
121 		if (np->n_dirseq) {
122 			smbfs_findclose(np->n_dirseq, &scred);
123 			np->n_dirseq = NULL;
124 		}
125 		np->n_dirofs = 2;
126 		error = smbfs_findopen(np, "*", 1,
127 		    SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR,
128 		    &scred, &ctx);
129 		if (error) {
130 			SMBVDEBUG("can not open search, error = %d", error);
131 			return error;
132 		}
133 		np->n_dirseq = ctx;
134 	} else
135 		ctx = np->n_dirseq;
136 	while (np->n_dirofs < offset) {
137 		error = smbfs_findnext(ctx, offset - np->n_dirofs++, &scred);
138 		if (error) {
139 			smbfs_findclose(np->n_dirseq, &scred);
140 			np->n_dirseq = NULL;
141 			return error == ENOENT ? 0 : error;
142 		}
143 	}
144 	error = 0;
145 	for (; limit; limit--, offset++) {
146 		error = smbfs_findnext(ctx, limit, &scred);
147 		if (error)
148 			break;
149 		np->n_dirofs++;
150 		bzero((caddr_t)&de, DE_SIZE);
151 		de.d_reclen = DE_SIZE;
152 		de.d_fileno = ctx->f_attr.fa_ino;
153 		de.d_type = (ctx->f_attr.fa_attr & SMB_FA_DIR) ? DT_DIR : DT_REG;
154 		de.d_namlen = ctx->f_nmlen;
155 		bcopy(ctx->f_name, de.d_name, de.d_namlen);
156 		de.d_name[de.d_namlen] = '\0';
157 		if (smbfs_fastlookup) {
158 			error = smbfs_nget(vp->v_mount, vp, ctx->f_name,
159 			    ctx->f_nmlen, &ctx->f_attr, &newvp);
160 			if (!error)
161 				vput(newvp);
162 		}
163 		error = uiomove((caddr_t)&de, DE_SIZE, uio);
164 		if (error)
165 			break;
166 	}
167 	if (error == ENOENT)
168 		error = 0;
169 	uio->uio_offset = offset * DE_SIZE;
170 	return error;
171 }
172 
173 int
174 smbfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred)
175 {
176 	struct thread *td;
177 	struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
178 	struct smbnode *np = VTOSMB(vp);
179 	struct vattr vattr;
180 	struct smb_cred scred;
181 	int error, lks;
182 
183 	/*
184 	 * Protect against method which is not supported for now
185 	 */
186 	if (uiop->uio_segflg == UIO_NOCOPY)
187 		return EOPNOTSUPP;
188 
189 	if (vp->v_type != VREG && vp->v_type != VDIR) {
190 		SMBFSERR("vn types other than VREG or VDIR are unsupported !\n");
191 		return EIO;
192 	}
193 	if (uiop->uio_resid == 0)
194 		return 0;
195 	if (uiop->uio_offset < 0)
196 		return EINVAL;
197 /*	if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
198 		return EFBIG;*/
199 	td = uiop->uio_td;
200 	if (vp->v_type == VDIR) {
201 		lks = LK_EXCLUSIVE;/*lockstatus(&vp->v_lock, td);*/
202 		if (lks == LK_SHARED)
203 			vn_lock(vp, LK_UPGRADE | LK_RETRY, td);
204 		error = smbfs_readvdir(vp, uiop, cred);
205 		if (lks == LK_SHARED)
206 			vn_lock(vp, LK_DOWNGRADE | LK_RETRY, td);
207 		return error;
208 	}
209 
210 /*	biosize = SSTOCN(smp->sm_share)->sc_txmax;*/
211 	if (np->n_flag & NMODIFIED) {
212 		smbfs_attr_cacheremove(vp);
213 		error = VOP_GETATTR(vp, &vattr, td);
214 		if (error)
215 			return error;
216 		np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
217 	} else {
218 		error = VOP_GETATTR(vp, &vattr, td);
219 		if (error)
220 			return error;
221 		if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
222 			error = smbfs_vinvalbuf(vp, V_SAVE, td, 1);
223 			if (error)
224 				return error;
225 			np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
226 		}
227 	}
228 	smb_makescred(&scred, td, cred);
229 	return smb_read(smp->sm_share, np->n_fid, uiop, &scred);
230 }
231 
232 int
233 smbfs_writevnode(struct vnode *vp, struct uio *uiop,
234 		 struct ucred *cred, int ioflag)
235 {
236 	struct thread *td;
237 	struct smbmount *smp = VTOSMBFS(vp);
238 	struct smbnode *np = VTOSMB(vp);
239 	struct smb_cred scred;
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 	if (ioflag & (IO_APPEND | IO_SYNC)) {
253 		if (np->n_flag & NMODIFIED) {
254 			smbfs_attr_cacheremove(vp);
255 			error = smbfs_vinvalbuf(vp, V_SAVE, td, 1);
256 			if (error)
257 				return error;
258 		}
259 		if (ioflag & IO_APPEND) {
260 #if notyet
261 			/*
262 			 * File size can be changed by another client
263 			 */
264 			smbfs_attr_cacheremove(vp);
265 			error = VOP_GETATTR(vp, &vattr, td);
266 			if (error) return (error);
267 #endif
268 			uiop->uio_offset = np->n_size;
269 		}
270 	}
271 	if (uiop->uio_resid == 0)
272 		return 0;
273 	if (td->td_proc &&
274 	    uiop->uio_offset + uiop->uio_resid >
275 	    td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
276 		psignal(td->td_proc, SIGXFSZ);
277 		return EFBIG;
278 	}
279 	smb_makescred(&scred, td, 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 thread *td)
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_td = td;
309 
310 	smb_makescred(&scred, td, cr);
311 
312 	if (bp->b_flags & B_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_flags |= B_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_flags |= B_ERROR;
381 				bp->b_error = error;
382 			}
383 			bp->b_dirtyoff = bp->b_dirtyend = 0;
384 		}
385 	    } else {
386 		bp->b_resid = 0;
387 		biodone(bp);
388 		return 0;
389 	    }
390 	}
391 	bp->b_resid = uiop->uio_resid;
392 	biodone(bp);
393 	return error;
394 }
395 
396 /*
397  * Vnode op for VM getpages.
398  * Wish wish .... get rid from multiple IO routines
399  *
400  * smbfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
401  *		  int a_reqpage, vm_ooffset_t a_offset)
402  */
403 int
404 smbfs_getpages(struct vop_getpages_args *ap)
405 {
406 #ifdef SMBFS_RWGENERIC
407 	return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
408 		ap->a_reqpage);
409 #else
410 	int i, error, nextoff, size, toff, npages, count;
411 	int doclose;
412 	struct uio uio;
413 	struct iovec iov;
414 	vm_offset_t kva;
415 	struct buf *bp;
416 	struct vnode *vp;
417 	struct thread *td = curthread;	/* XXX */
418 	struct ucred *cred;
419 	struct smbmount *smp;
420 	struct smbnode *np;
421 	struct smb_cred scred;
422 	vm_page_t *pages;
423 
424 	KKASSERT(td->td_proc);
425 
426 	vp = ap->a_vp;
427 	cred = td->td_proc->p_ucred;
428 	np = VTOSMB(vp);
429 	smp = VFSTOSMBFS(vp->v_mount);
430 	pages = ap->a_m;
431 	count = ap->a_count;
432 
433 	if (vp->v_object == NULL) {
434 		printf("smbfs_getpages: called with non-merged cache vnode??\n");
435 		return VM_PAGER_ERROR;
436 	}
437 	smb_makescred(&scred, td, cred);
438 
439 	bp = getpbuf(&smbfs_pbuf_freecnt);
440 	npages = btoc(count);
441 	kva = (vm_offset_t) bp->b_data;
442 	pmap_qenter(kva, pages, npages);
443 
444 	iov.iov_base = (caddr_t) kva;
445 	iov.iov_len = count;
446 	uio.uio_iov = &iov;
447 	uio.uio_iovcnt = 1;
448 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
449 	uio.uio_resid = count;
450 	uio.uio_segflg = UIO_SYSSPACE;
451 	uio.uio_rw = UIO_READ;
452 	uio.uio_td = td;
453 
454 	/*
455 	 * This is kinda nasty.  Since smbfs is physically closing the
456 	 * fid on close(), we have to reopen it if necessary.  There are
457 	 * other races here too, such as if another process opens the same
458 	 * file while we are blocked in read. XXX
459 	 */
460 	error = 0;
461 	doclose = 0;
462 	if (np->n_opencount == 0) {
463 		error = smbfs_smb_open(np, SMB_AM_OPENREAD, &scred);
464 		if (error == 0)
465 			doclose = 1;
466 	}
467 	if (error == 0)
468 		error = smb_read(smp->sm_share, np->n_fid, &uio, &scred);
469 	if (doclose)
470 		smbfs_smb_close(smp->sm_share, np->n_fid, NULL, &scred);
471 	pmap_qremove(kva, npages);
472 
473 	relpbuf(bp, &smbfs_pbuf_freecnt);
474 
475 	if (error && (uio.uio_resid == count)) {
476 		printf("smbfs_getpages: error %d\n",error);
477 		for (i = 0; i < npages; i++) {
478 			if (ap->a_reqpage != i)
479 				vnode_pager_freepage(pages[i]);
480 		}
481 		return VM_PAGER_ERROR;
482 	}
483 
484 	size = count - uio.uio_resid;
485 
486 	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
487 		vm_page_t m;
488 		nextoff = toff + PAGE_SIZE;
489 		m = pages[i];
490 
491 		m->flags &= ~PG_ZERO;
492 
493 		if (nextoff <= size) {
494 			m->valid = VM_PAGE_BITS_ALL;
495 			m->dirty = 0;
496 		} else {
497 			int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1);
498 			vm_page_set_validclean(m, 0, nvalid);
499 		}
500 
501 		if (i != ap->a_reqpage) {
502 			/*
503 			 * Whether or not to leave the page activated is up in
504 			 * the air, but we should put the page on a page queue
505 			 * somewhere (it already is in the object).  Result:
506 			 * It appears that emperical results show that
507 			 * deactivating pages is best.
508 			 */
509 
510 			/*
511 			 * Just in case someone was asking for this page we
512 			 * now tell them that it is ok to use.
513 			 */
514 			if (!error) {
515 				if (m->flags & PG_WANTED)
516 					vm_page_activate(m);
517 				else
518 					vm_page_deactivate(m);
519 				vm_page_wakeup(m);
520 			} else {
521 				vnode_pager_freepage(m);
522 			}
523 		}
524 	}
525 	return 0;
526 #endif /* SMBFS_RWGENERIC */
527 }
528 
529 /*
530  * Vnode op for VM putpages.
531  * possible bug: all IO done in sync mode
532  * Note that vop_close always invalidate pages before close, so it's
533  * not necessary to open vnode.
534  *
535  * smbfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count, int a_sync,
536  *		  int *a_rtvals, vm_ooffset_t a_offset)
537  */
538 int
539 smbfs_putpages(struct vop_putpages_args *ap)
540 {
541 	int error;
542 	struct vnode *vp = ap->a_vp;
543 	struct thread *td = curthread;	/* XXX */
544 	struct ucred *cred;
545 
546 #ifdef SMBFS_RWGENERIC
547 	KKASSERT(td->td_proc);
548 	cred = td->td_proc->p_ucred;
549 	VOP_OPEN(vp, FWRITE, cred, NULL, td);
550 	error = vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
551 		ap->a_sync, ap->a_rtvals);
552 	VOP_CLOSE(vp, FWRITE, cred, td);
553 	return error;
554 #else
555 	struct uio uio;
556 	struct iovec iov;
557 	vm_offset_t kva;
558 	struct buf *bp;
559 	int i, npages, count;
560 	int doclose;
561 	int *rtvals;
562 	struct smbmount *smp;
563 	struct smbnode *np;
564 	struct smb_cred scred;
565 	vm_page_t *pages;
566 
567 	KKASSERT(td->td_proc);
568 	cred = td->td_proc->p_ucred;
569 /*	VOP_OPEN(vp, FWRITE, cred, td);*/
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 	bp = getpbuf(&smbfs_pbuf_freecnt);
582 	kva = (vm_offset_t) bp->b_data;
583 	pmap_qenter(kva, pages, npages);
584 
585 	iov.iov_base = (caddr_t) kva;
586 	iov.iov_len = count;
587 	uio.uio_iov = &iov;
588 	uio.uio_iovcnt = 1;
589 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
590 	uio.uio_resid = count;
591 	uio.uio_segflg = UIO_SYSSPACE;
592 	uio.uio_rw = UIO_WRITE;
593 	uio.uio_td = td;
594 	SMBVDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
595 
596 	smb_makescred(&scred, td, cred);
597 
598 	/*
599 	 * This is kinda nasty.  Since smbfs is physically closing the
600 	 * fid on close(), we have to reopen it if necessary.  There are
601 	 * other races here too, such as if another process opens the same
602 	 * file while we are blocked in read, or the file is open read-only
603 	 * XXX
604 	 */
605 	error = 0;
606 	doclose = 0;
607 	if (np->n_opencount == 0) {
608 		error = smbfs_smb_open(np, SMB_AM_OPENRW, &scred);
609 		if (error == 0)
610 			doclose = 1;
611 	}
612 	if (error == 0)
613 		error = smb_write(smp->sm_share, np->n_fid, &uio, &scred);
614 	if (doclose)
615 		smbfs_smb_close(smp->sm_share, np->n_fid, NULL, &scred);
616 /*	VOP_CLOSE(vp, FWRITE, cred, td);*/
617 	SMBVDEBUG("paged write done: %d\n", error);
618 
619 	pmap_qremove(kva, npages);
620 	relpbuf(bp, &smbfs_pbuf_freecnt);
621 
622 	if (!error) {
623 		int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
624 		for (i = 0; i < nwritten; i++) {
625 			rtvals[i] = VM_PAGER_OK;
626 			pages[i]->dirty = 0;
627 		}
628 	}
629 	return rtvals[0];
630 #endif /* SMBFS_RWGENERIC */
631 }
632 
633 /*
634  * Flush and invalidate all dirty buffers. If another process is already
635  * doing the flush, just wait for completion.
636  */
637 int
638 smbfs_vinvalbuf(struct vnode *vp, int flags, struct thread *td, int intrflg)
639 {
640 	struct smbnode *np = VTOSMB(vp);
641 	int error = 0, slpflag, slptimeo;
642 
643 	if (vp->v_flag & VRECLAIMED)
644 		return 0;
645 	if (intrflg) {
646 		slpflag = PCATCH;
647 		slptimeo = 2 * hz;
648 	} else {
649 		slpflag = 0;
650 		slptimeo = 0;
651 	}
652 	while (np->n_flag & NFLUSHINPROG) {
653 		np->n_flag |= NFLUSHWANT;
654 		error = tsleep((caddr_t)&np->n_flag, 0, "smfsvinv", slptimeo);
655 		error = smb_proc_intr(td);
656 		if (error == EINTR && intrflg)
657 			return EINTR;
658 	}
659 	np->n_flag |= NFLUSHINPROG;
660 	error = vinvalbuf(vp, flags, td, slpflag, 0);
661 	while (error) {
662 		if (intrflg && (error == ERESTART || error == EINTR)) {
663 			np->n_flag &= ~NFLUSHINPROG;
664 			if (np->n_flag & NFLUSHWANT) {
665 				np->n_flag &= ~NFLUSHWANT;
666 				wakeup((caddr_t)&np->n_flag);
667 			}
668 			return EINTR;
669 		}
670 		error = vinvalbuf(vp, flags, td, slpflag, 0);
671 	}
672 	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
673 	if (np->n_flag & NFLUSHWANT) {
674 		np->n_flag &= ~NFLUSHWANT;
675 		wakeup((caddr_t)&np->n_flag);
676 	}
677 	return (error);
678 }
679