xref: /freebsd/sys/fs/smbfs/smbfs_io.c (revision 88ba5e8955518c1e032eafbce27d548eaf5a59ea)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000-2001 Boris Popov
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  *
30  */
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/fcntl.h>
35 #include <sys/bio.h>
36 #include <sys/buf.h>
37 #include <sys/mount.h>
38 #include <sys/namei.h>
39 #include <sys/vnode.h>
40 #include <sys/dirent.h>
41 #include <sys/rwlock.h>
42 #include <sys/signalvar.h>
43 #include <sys/sysctl.h>
44 #include <sys/vmmeter.h>
45 
46 #include <vm/vm.h>
47 #include <vm/vm_param.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_object.h>
51 #include <vm/vm_pager.h>
52 #include <vm/vnode_pager.h>
53 /*
54 #include <sys/ioccom.h>
55 */
56 #include <netsmb/smb.h>
57 #include <netsmb/smb_conn.h>
58 #include <netsmb/smb_subr.h>
59 
60 #include <fs/smbfs/smbfs.h>
61 #include <fs/smbfs/smbfs_node.h>
62 #include <fs/smbfs/smbfs_subr.h>
63 
64 /*#define SMBFS_RWGENERIC*/
65 
66 extern uma_zone_t smbfs_pbuf_zone;
67 
68 static int smbfs_fastlookup = 1;
69 
70 SYSCTL_DECL(_vfs_smbfs);
71 SYSCTL_INT(_vfs_smbfs, OID_AUTO, fastlookup, CTLFLAG_RW, &smbfs_fastlookup, 0, "");
72 
73 #define DE_SIZE	(sizeof(struct dirent))
74 
75 static int
76 smbfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred)
77 {
78 	struct dirent de;
79 	struct componentname cn;
80 	struct smb_cred *scred;
81 	struct smbfs_fctx *ctx;
82 	struct vnode *newvp;
83 	struct smbnode *np = VTOSMB(vp);
84 	int error/*, *eofflag = ap->a_eofflag*/;
85 	long offset, limit;
86 
87 	np = VTOSMB(vp);
88 	SMBVDEBUG("dirname='%s'\n", np->n_name);
89 	scred = smbfs_malloc_scred();
90 	smb_makescred(scred, uio->uio_td, cred);
91 	offset = uio->uio_offset / DE_SIZE;	/* offset in the directory */
92 	limit = uio->uio_resid / DE_SIZE;
93 	if (uio->uio_resid < DE_SIZE || uio->uio_offset < 0) {
94 		error = EINVAL;
95 		goto out;
96 	}
97 	while (limit && offset < 2) {
98 		limit--;
99 		bzero((caddr_t)&de, DE_SIZE);
100 		de.d_reclen = DE_SIZE;
101 		de.d_fileno = (offset == 0) ? np->n_ino :
102 		    (np->n_parent ? np->n_parentino : 2);
103 		if (de.d_fileno == 0)
104 			de.d_fileno = 0x7ffffffd + offset;
105 		de.d_off = offset + 1;
106 		de.d_namlen = offset + 1;
107 		de.d_name[0] = '.';
108 		de.d_name[1] = '.';
109 		de.d_type = DT_DIR;
110 		dirent_terminate(&de);
111 		error = uiomove(&de, DE_SIZE, uio);
112 		if (error)
113 			goto out;
114 		offset++;
115 		uio->uio_offset += DE_SIZE;
116 	}
117 	if (limit == 0) {
118 		error = 0;
119 		goto out;
120 	}
121 	if (offset != np->n_dirofs || np->n_dirseq == NULL) {
122 		SMBVDEBUG("Reopening search %ld:%ld\n", offset, np->n_dirofs);
123 		if (np->n_dirseq) {
124 			smbfs_findclose(np->n_dirseq, scred);
125 			np->n_dirseq = NULL;
126 		}
127 		np->n_dirofs = 2;
128 		error = smbfs_findopen(np, "*", 1,
129 		    SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR,
130 		    scred, &ctx);
131 		if (error) {
132 			SMBVDEBUG("can not open search, error = %d", error);
133 			goto out;
134 		}
135 		np->n_dirseq = ctx;
136 	} else
137 		ctx = np->n_dirseq;
138 	while (np->n_dirofs < offset) {
139 		error = smbfs_findnext(ctx, offset - np->n_dirofs++, scred);
140 		if (error) {
141 			smbfs_findclose(np->n_dirseq, scred);
142 			np->n_dirseq = NULL;
143 			error = ENOENT ? 0 : error;
144 			goto out;
145 		}
146 	}
147 	error = 0;
148 	for (; limit; limit--, offset++) {
149 		error = smbfs_findnext(ctx, limit, scred);
150 		if (error)
151 			break;
152 		np->n_dirofs++;
153 		bzero((caddr_t)&de, DE_SIZE);
154 		de.d_reclen = DE_SIZE;
155 		de.d_fileno = ctx->f_attr.fa_ino;
156 		de.d_off = offset + 1;
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 		dirent_terminate(&de);
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(&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 out:
179 	smbfs_free_scred(scred);
180 	return error;
181 }
182 
183 int
184 smbfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred)
185 {
186 	struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
187 	struct smbnode *np = VTOSMB(vp);
188 	struct thread *td;
189 	struct vattr vattr;
190 	struct smb_cred *scred;
191 	int error, lks;
192 
193 	/*
194 	 * Protect against method which is not supported for now
195 	 */
196 	if (uiop->uio_segflg == UIO_NOCOPY)
197 		return EOPNOTSUPP;
198 
199 	if (vp->v_type != VREG && vp->v_type != VDIR) {
200 		SMBFSERR("vn types other than VREG or VDIR are unsupported !\n");
201 		return EIO;
202 	}
203 	if (uiop->uio_resid == 0)
204 		return 0;
205 	if (uiop->uio_offset < 0)
206 		return EINVAL;
207 /*	if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
208 		return EFBIG;*/
209 	td = uiop->uio_td;
210 	if (vp->v_type == VDIR) {
211 		lks = LK_EXCLUSIVE;	/* lockstatus(vp->v_vnlock); */
212 		if (lks == LK_SHARED)
213 			vn_lock(vp, LK_UPGRADE | LK_RETRY);
214 		error = smbfs_readvdir(vp, uiop, cred);
215 		if (lks == LK_SHARED)
216 			vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
217 		return error;
218 	}
219 
220 /*	biosize = SSTOCN(smp->sm_share)->sc_txmax;*/
221 	if (np->n_flag & NMODIFIED) {
222 		smbfs_attr_cacheremove(vp);
223 		error = VOP_GETATTR(vp, &vattr, cred);
224 		if (error)
225 			return error;
226 		np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
227 	} else {
228 		error = VOP_GETATTR(vp, &vattr, cred);
229 		if (error)
230 			return error;
231 		if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
232 			error = smbfs_vinvalbuf(vp, td);
233 			if (error)
234 				return error;
235 			np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
236 		}
237 	}
238 	scred = smbfs_malloc_scred();
239 	smb_makescred(scred, td, cred);
240 	error = smb_read(smp->sm_share, np->n_fid, uiop, scred);
241 	smbfs_free_scred(scred);
242 	return (error);
243 }
244 
245 int
246 smbfs_writevnode(struct vnode *vp, struct uio *uiop,
247 	struct ucred *cred, int ioflag)
248 {
249 	struct smbmount *smp = VTOSMBFS(vp);
250 	struct smbnode *np = VTOSMB(vp);
251 	struct smb_cred *scred;
252 	struct thread *td;
253 	int error = 0;
254 
255 	if (vp->v_type != VREG) {
256 		SMBERROR("vn types other than VREG unsupported !\n");
257 		return EIO;
258 	}
259 	SMBVDEBUG("ofs=%jd,resid=%zd\n", (intmax_t)uiop->uio_offset,
260 	    uiop->uio_resid);
261 	if (uiop->uio_offset < 0)
262 		return EINVAL;
263 /*	if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
264 		return (EFBIG);*/
265 	td = uiop->uio_td;
266 	if (ioflag & (IO_APPEND | IO_SYNC)) {
267 		if (np->n_flag & NMODIFIED) {
268 			smbfs_attr_cacheremove(vp);
269 			error = smbfs_vinvalbuf(vp, td);
270 			if (error)
271 				return error;
272 		}
273 		if (ioflag & IO_APPEND) {
274 #ifdef notyet
275 			/*
276 			 * File size can be changed by another client
277 			 */
278 			smbfs_attr_cacheremove(vp);
279 			error = VOP_GETATTR(vp, &vattr, cred);
280 			if (error) return (error);
281 #endif
282 			uiop->uio_offset = np->n_size;
283 		}
284 	}
285 	if (uiop->uio_resid == 0)
286 		return 0;
287 
288 	if (vn_rlimit_fsize(vp, uiop, td))
289 		return (EFBIG);
290 
291 	scred = smbfs_malloc_scred();
292 	smb_makescred(scred, td, cred);
293 	error = smb_write(smp->sm_share, np->n_fid, uiop, scred);
294 	smbfs_free_scred(scred);
295 	SMBVDEBUG("after: ofs=%jd,resid=%zd\n", (intmax_t)uiop->uio_offset,
296 	    uiop->uio_resid);
297 	if (!error) {
298 		if (uiop->uio_offset > np->n_size) {
299 			np->n_size = uiop->uio_offset;
300 			vnode_pager_setsize(vp, np->n_size);
301 		}
302 	}
303 	return error;
304 }
305 
306 /*
307  * Do an I/O operation to/from a cache block.
308  */
309 int
310 smbfs_doio(struct vnode *vp, struct buf *bp, struct ucred *cr, struct thread *td)
311 {
312 	struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
313 	struct smbnode *np = VTOSMB(vp);
314 	struct uio *uiop;
315 	struct iovec io;
316 	struct smb_cred *scred;
317 	int error = 0;
318 
319 	uiop = malloc(sizeof(struct uio), M_SMBFSDATA, M_WAITOK);
320 	uiop->uio_iov = &io;
321 	uiop->uio_iovcnt = 1;
322 	uiop->uio_segflg = UIO_SYSSPACE;
323 	uiop->uio_td = td;
324 
325 	scred = smbfs_malloc_scred();
326 	smb_makescred(scred, td, cr);
327 
328 	if (bp->b_iocmd == BIO_READ) {
329 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
330 	    io.iov_base = bp->b_data;
331 	    uiop->uio_rw = UIO_READ;
332 	    switch (vp->v_type) {
333 	      case VREG:
334 		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
335 		error = smb_read(smp->sm_share, np->n_fid, uiop, scred);
336 		if (error)
337 			break;
338 		if (uiop->uio_resid) {
339 			int left = uiop->uio_resid;
340 			int nread = bp->b_bcount - left;
341 			if (left > 0)
342 			    bzero((char *)bp->b_data + nread, left);
343 		}
344 		break;
345 	    default:
346 		printf("smbfs_doio:  type %x unexpected\n",vp->v_type);
347 		break;
348 	    }
349 	    if (error) {
350 		bp->b_error = error;
351 		bp->b_ioflags |= BIO_ERROR;
352 	    }
353 	} else { /* write */
354 	    if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
355 		bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
356 
357 	    if (bp->b_dirtyend > bp->b_dirtyoff) {
358 		io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
359 		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
360 		io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
361 		uiop->uio_rw = UIO_WRITE;
362 		error = smb_write(smp->sm_share, np->n_fid, uiop, scred);
363 
364 		/*
365 		 * For an interrupted write, the buffer is still valid
366 		 * and the write hasn't been pushed to the server yet,
367 		 * so we can't set BIO_ERROR and report the interruption
368 		 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
369 		 * is not relevant, so the rpc attempt is essentially
370 		 * a noop.  For the case of a V3 write rpc not being
371 		 * committed to stable storage, the block is still
372 		 * dirty and requires either a commit rpc or another
373 		 * write rpc with iomode == NFSV3WRITE_FILESYNC before
374 		 * the block is reused. This is indicated by setting
375 		 * the B_DELWRI and B_NEEDCOMMIT flags.
376 		 */
377 		if (error == EINTR
378 		    || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
379 			bp->b_flags &= ~(B_INVAL|B_NOCACHE);
380 			if ((bp->b_flags & B_ASYNC) == 0)
381 			    bp->b_flags |= B_EINTR;
382 			if ((bp->b_flags & B_PAGING) == 0) {
383 			    bdirty(bp);
384 			    bp->b_flags &= ~B_DONE;
385 			}
386 			if ((bp->b_flags & B_ASYNC) == 0)
387 			    bp->b_flags |= B_EINTR;
388 		} else {
389 			if (error) {
390 				bp->b_ioflags |= BIO_ERROR;
391 				bp->b_error = error;
392 			}
393 			bp->b_dirtyoff = bp->b_dirtyend = 0;
394 		}
395 	    } else {
396 		bp->b_resid = 0;
397 		bufdone(bp);
398 		free(uiop, M_SMBFSDATA);
399 		smbfs_free_scred(scred);
400 		return 0;
401 	    }
402 	}
403 	bp->b_resid = uiop->uio_resid;
404 	bufdone(bp);
405 	free(uiop, M_SMBFSDATA);
406 	smbfs_free_scred(scred);
407 	return error;
408 }
409 
410 /*
411  * Vnode op for VM getpages.
412  * Wish wish .... get rid from multiple IO routines
413  */
414 int
415 smbfs_getpages(ap)
416 	struct vop_getpages_args /* {
417 		struct vnode *a_vp;
418 		vm_page_t *a_m;
419 		int a_count;
420 		int a_reqpage;
421 	} */ *ap;
422 {
423 #ifdef SMBFS_RWGENERIC
424 	return vop_stdgetpages(ap);
425 #else
426 	int i, error, nextoff, size, toff, npages, count;
427 	struct uio uio;
428 	struct iovec iov;
429 	vm_offset_t kva;
430 	struct buf *bp;
431 	struct vnode *vp;
432 	struct thread *td;
433 	struct ucred *cred;
434 	struct smbmount *smp;
435 	struct smbnode *np;
436 	struct smb_cred *scred;
437 	vm_object_t object;
438 	vm_page_t *pages;
439 
440 	vp = ap->a_vp;
441 	if ((object = vp->v_object) == NULL) {
442 		printf("smbfs_getpages: called with non-merged cache vnode??\n");
443 		return VM_PAGER_ERROR;
444 	}
445 
446 	td = curthread;				/* XXX */
447 	cred = td->td_ucred;		/* XXX */
448 	np = VTOSMB(vp);
449 	smp = VFSTOSMBFS(vp->v_mount);
450 	pages = ap->a_m;
451 	npages = ap->a_count;
452 
453 	/*
454 	 * If the requested page is partially valid, just return it and
455 	 * allow the pager to zero-out the blanks.  Partially valid pages
456 	 * can only occur at the file EOF.
457 	 *
458 	 * XXXGL: is that true for SMB filesystem?
459 	 */
460 	VM_OBJECT_WLOCK(object);
461 	if (!vm_page_none_valid(pages[npages - 1]) && --npages == 0)
462 		goto out;
463 	VM_OBJECT_WUNLOCK(object);
464 
465 	scred = smbfs_malloc_scred();
466 	smb_makescred(scred, td, cred);
467 
468 	bp = uma_zalloc(smbfs_pbuf_zone, M_WAITOK);
469 
470 	kva = (vm_offset_t) bp->b_data;
471 	pmap_qenter(kva, pages, npages);
472 	VM_CNT_INC(v_vnodein);
473 	VM_CNT_ADD(v_vnodepgsin, npages);
474 
475 	count = npages << PAGE_SHIFT;
476 	iov.iov_base = (caddr_t) kva;
477 	iov.iov_len = count;
478 	uio.uio_iov = &iov;
479 	uio.uio_iovcnt = 1;
480 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
481 	uio.uio_resid = count;
482 	uio.uio_segflg = UIO_SYSSPACE;
483 	uio.uio_rw = UIO_READ;
484 	uio.uio_td = td;
485 
486 	error = smb_read(smp->sm_share, np->n_fid, &uio, scred);
487 	smbfs_free_scred(scred);
488 	pmap_qremove(kva, npages);
489 
490 	uma_zfree(smbfs_pbuf_zone, bp);
491 
492 	if (error && (uio.uio_resid == count)) {
493 		printf("smbfs_getpages: error %d\n",error);
494 		return VM_PAGER_ERROR;
495 	}
496 
497 	size = count - uio.uio_resid;
498 
499 	VM_OBJECT_WLOCK(object);
500 	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
501 		vm_page_t m;
502 		nextoff = toff + PAGE_SIZE;
503 		m = pages[i];
504 
505 		if (nextoff <= size) {
506 			/*
507 			 * Read operation filled an entire page
508 			 */
509 			vm_page_valid(m);
510 			KASSERT(m->dirty == 0,
511 			    ("smbfs_getpages: page %p is dirty", m));
512 		} else if (size > toff) {
513 			/*
514 			 * Read operation filled a partial page.
515 			 */
516 			vm_page_invalid(m);
517 			vm_page_set_valid_range(m, 0, size - toff);
518 			KASSERT(m->dirty == 0,
519 			    ("smbfs_getpages: page %p is dirty", m));
520 		} else {
521 			/*
522 			 * Read operation was short.  If no error occurred
523 			 * we may have hit a zero-fill section.   We simply
524 			 * leave valid set to 0.
525 			 */
526 			;
527 		}
528 	}
529 out:
530 	VM_OBJECT_WUNLOCK(object);
531 	if (ap->a_rbehind)
532 		*ap->a_rbehind = 0;
533 	if (ap->a_rahead)
534 		*ap->a_rahead = 0;
535 	return (VM_PAGER_OK);
536 #endif /* SMBFS_RWGENERIC */
537 }
538 
539 /*
540  * Vnode op for VM putpages.
541  * possible bug: all IO done in sync mode
542  * Note that vop_close always invalidate pages before close, so it's
543  * not necessary to open vnode.
544  */
545 int
546 smbfs_putpages(ap)
547 	struct vop_putpages_args /* {
548 		struct vnode *a_vp;
549 		vm_page_t *a_m;
550 		int a_count;
551 		int a_sync;
552 		int *a_rtvals;
553 	} */ *ap;
554 {
555 	int error;
556 	struct vnode *vp = ap->a_vp;
557 	struct thread *td;
558 	struct ucred *cred;
559 
560 #ifdef SMBFS_RWGENERIC
561 	td = curthread;			/* XXX */
562 	cred = td->td_ucred;		/* XXX */
563 	VOP_OPEN(vp, FWRITE, cred, td, NULL);
564 	error = vop_stdputpages(ap);
565 	VOP_CLOSE(vp, FWRITE, cred, td);
566 	return error;
567 #else
568 	struct uio uio;
569 	struct iovec iov;
570 	vm_offset_t kva;
571 	struct buf *bp;
572 	int i, npages, count;
573 	int *rtvals;
574 	struct smbmount *smp;
575 	struct smbnode *np;
576 	struct smb_cred *scred;
577 	vm_page_t *pages;
578 
579 	td = curthread;			/* XXX */
580 	cred = td->td_ucred;		/* XXX */
581 /*	VOP_OPEN(vp, FWRITE, cred, td, NULL);*/
582 	np = VTOSMB(vp);
583 	smp = VFSTOSMBFS(vp->v_mount);
584 	pages = ap->a_m;
585 	count = ap->a_count;
586 	rtvals = ap->a_rtvals;
587 	npages = btoc(count);
588 
589 	for (i = 0; i < npages; i++) {
590 		rtvals[i] = VM_PAGER_ERROR;
591 	}
592 
593 	bp = uma_zalloc(smbfs_pbuf_zone, M_WAITOK);
594 
595 	kva = (vm_offset_t) bp->b_data;
596 	pmap_qenter(kva, pages, npages);
597 	VM_CNT_INC(v_vnodeout);
598 	VM_CNT_ADD(v_vnodepgsout, count);
599 
600 	iov.iov_base = (caddr_t) kva;
601 	iov.iov_len = count;
602 	uio.uio_iov = &iov;
603 	uio.uio_iovcnt = 1;
604 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
605 	uio.uio_resid = count;
606 	uio.uio_segflg = UIO_SYSSPACE;
607 	uio.uio_rw = UIO_WRITE;
608 	uio.uio_td = td;
609 	SMBVDEBUG("ofs=%jd,resid=%zd\n", (intmax_t)uio.uio_offset,
610 	    uio.uio_resid);
611 
612 	scred = smbfs_malloc_scred();
613 	smb_makescred(scred, td, cred);
614 	error = smb_write(smp->sm_share, np->n_fid, &uio, scred);
615 	smbfs_free_scred(scred);
616 /*	VOP_CLOSE(vp, FWRITE, cred, td);*/
617 	SMBVDEBUG("paged write done: %d\n", error);
618 
619 	pmap_qremove(kva, npages);
620 
621 	uma_zfree(smbfs_pbuf_zone, bp);
622 
623 	if (error == 0) {
624 		vnode_pager_undirty_pages(pages, rtvals, count - uio.uio_resid,
625 		    npages * PAGE_SIZE, npages * PAGE_SIZE);
626 	}
627 	return (rtvals[0]);
628 #endif /* SMBFS_RWGENERIC */
629 }
630 
631 /*
632  * Flush and invalidate all dirty buffers. If another process is already
633  * doing the flush, just wait for completion.
634  */
635 int
636 smbfs_vinvalbuf(struct vnode *vp, struct thread *td)
637 {
638 	struct smbnode *np = VTOSMB(vp);
639 	int error = 0;
640 
641 	if (VN_IS_DOOMED(vp))
642 		return 0;
643 
644 	while (np->n_flag & NFLUSHINPROG) {
645 		np->n_flag |= NFLUSHWANT;
646 		error = tsleep(&np->n_flag, PRIBIO + 2, "smfsvinv", 2 * hz);
647 		error = smb_td_intr(td);
648 		if (error == EINTR)
649 			return EINTR;
650 	}
651 	np->n_flag |= NFLUSHINPROG;
652 
653 	if (vp->v_bufobj.bo_object != NULL) {
654 		VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
655 		vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
656 		VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
657 	}
658 
659 	error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
660 	while (error) {
661 		if (error == ERESTART || error == EINTR) {
662 			np->n_flag &= ~NFLUSHINPROG;
663 			if (np->n_flag & NFLUSHWANT) {
664 				np->n_flag &= ~NFLUSHWANT;
665 				wakeup(&np->n_flag);
666 			}
667 			return EINTR;
668 		}
669 		error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
670 	}
671 	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
672 	if (np->n_flag & NFLUSHWANT) {
673 		np->n_flag &= ~NFLUSHWANT;
674 		wakeup(&np->n_flag);
675 	}
676 	return (error);
677 }
678