2 * Copyright (c) 2000-2001, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.2 2003/06/17 04:28:33 dillon Exp $
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>
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>
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>
56 #include <sys/ioccom.h>
58 #include <netsmb/smb.h>
59 #include <netsmb/smb_conn.h>
60 #include <netsmb/smb_subr.h>
62 #include <fs/smbfs/smbfs.h>
63 #include <fs/smbfs/smbfs_node.h>
64 #include <fs/smbfs/smbfs_subr.h>
68 /*#define SMBFS_RWGENERIC*/
70 extern int smbfs_pbuf_freecnt;
72 static int smbfs_fastlookup = 1;
74 extern struct linker_set sysctl_vfs_smbfs;
76 SYSCTL_DECL(_vfs_smbfs);
77 SYSCTL_INT(_vfs_smbfs, OID_AUTO, fastlookup, CTLFLAG_RW, &smbfs_fastlookup, 0, "");
80 #define DE_SIZE (sizeof(struct dirent))
83 smbfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred)
86 struct componentname cn;
87 struct smb_cred scred;
88 struct smbfs_fctx *ctx;
90 struct smbnode *np = VTOSMB(vp);
91 int error/*, *eofflag = ap->a_eofflag*/;
95 SMBVDEBUG("dirname='%s'\n", np->n_name);
96 smb_makescred(&scred, uio->uio_procp, cred);
97 offset = uio->uio_offset / DE_SIZE; /* offset in the directory */
98 limit = uio->uio_resid / DE_SIZE;
99 if (uio->uio_resid < DE_SIZE || uio->uio_offset < 0)
101 while (limit && offset < 2) {
103 bzero((caddr_t)&de, DE_SIZE);
104 de.d_reclen = DE_SIZE;
105 de.d_fileno = (offset == 0) ? np->n_ino :
106 (np->n_parent ? VTOSMB(np->n_parent)->n_ino : 2);
107 if (de.d_fileno == 0)
108 de.d_fileno = 0x7ffffffd + offset;
109 de.d_namlen = offset + 1;
112 de.d_name[offset + 1] = '\0';
114 error = uiomove((caddr_t)&de, DE_SIZE, uio);
118 uio->uio_offset += DE_SIZE;
122 if (offset != np->n_dirofs || np->n_dirseq == NULL) {
123 SMBVDEBUG("Reopening search %ld:%ld\n", offset, np->n_dirofs);
125 smbfs_findclose(np->n_dirseq, &scred);
129 error = smbfs_findopen(np, "*", 1,
130 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR,
133 SMBVDEBUG("can not open search, error = %d", error);
139 while (np->n_dirofs < offset) {
140 error = smbfs_findnext(ctx, offset - np->n_dirofs++, &scred);
142 smbfs_findclose(np->n_dirseq, &scred);
144 return error == ENOENT ? 0 : error;
148 for (; limit; limit--, offset++) {
149 error = smbfs_findnext(ctx, limit, &scred);
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_type = (ctx->f_attr.fa_attr & SMB_FA_DIR) ? DT_DIR : DT_REG;
157 de.d_namlen = ctx->f_nmlen;
158 bcopy(ctx->f_name, de.d_name, de.d_namlen);
159 de.d_name[de.d_namlen] = '\0';
160 if (smbfs_fastlookup) {
161 error = smbfs_nget(vp->v_mount, vp, ctx->f_name,
162 ctx->f_nmlen, &ctx->f_attr, &newvp);
164 cn.cn_nameptr = de.d_name;
165 cn.cn_namelen = de.d_namlen;
166 cache_enter(vp, newvp, &cn);
170 error = uiomove((caddr_t)&de, DE_SIZE, uio);
176 uio->uio_offset = offset * DE_SIZE;
181 smbfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred)
183 struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
184 struct smbnode *np = VTOSMB(vp);
187 struct smb_cred scred;
191 * Protect against method which is not supported for now
193 if (uiop->uio_segflg == UIO_NOCOPY)
196 if (vp->v_type != VREG && vp->v_type != VDIR) {
197 SMBFSERR("vn types other than VREG or VDIR are unsupported !\n");
200 if (uiop->uio_resid == 0)
202 if (uiop->uio_offset < 0)
204 /* if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
207 if (vp->v_type == VDIR) {
208 lks = LK_EXCLUSIVE;/*lockstatus(&vp->v_lock, p);*/
209 if (lks == LK_SHARED)
210 vn_lock(vp, LK_UPGRADE | LK_RETRY, p);
211 error = smbfs_readvdir(vp, uiop, cred);
212 if (lks == LK_SHARED)
213 vn_lock(vp, LK_DOWNGRADE | LK_RETRY, p);
217 /* biosize = SSTOCN(smp->sm_share)->sc_txmax;*/
218 if (np->n_flag & NMODIFIED) {
219 smbfs_attr_cacheremove(vp);
220 error = VOP_GETATTR(vp, &vattr, cred, p);
223 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
225 error = VOP_GETATTR(vp, &vattr, cred, p);
228 if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
229 error = smbfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
232 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
235 smb_makescred(&scred, p, cred);
236 return smb_read(smp->sm_share, np->n_fid, uiop, &scred);
240 smbfs_writevnode(struct vnode *vp, struct uio *uiop,
241 struct ucred *cred, int ioflag)
243 struct smbmount *smp = VTOSMBFS(vp);
244 struct smbnode *np = VTOSMB(vp);
245 struct smb_cred scred;
249 if (vp->v_type != VREG) {
250 SMBERROR("vn types other than VREG unsupported !\n");
253 SMBVDEBUG("ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
254 if (uiop->uio_offset < 0)
256 /* if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
259 if (ioflag & (IO_APPEND | IO_SYNC)) {
260 if (np->n_flag & NMODIFIED) {
261 smbfs_attr_cacheremove(vp);
262 error = smbfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
266 if (ioflag & IO_APPEND) {
269 * File size can be changed by another client
271 smbfs_attr_cacheremove(vp);
272 error = VOP_GETATTR(vp, &vattr, cred, p);
273 if (error) return (error);
275 uiop->uio_offset = np->n_size;
278 if (uiop->uio_resid == 0)
280 if (p && uiop->uio_offset + uiop->uio_resid > p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
284 smb_makescred(&scred, p, cred);
285 error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
286 SMBVDEBUG("after: ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
288 if (uiop->uio_offset > np->n_size) {
289 np->n_size = uiop->uio_offset;
290 vnode_pager_setsize(vp, np->n_size);
297 * Do an I/O operation to/from a cache block.
300 smbfs_doio(struct buf *bp, struct ucred *cr, struct proc *p)
302 struct vnode *vp = bp->b_vp;
303 struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
304 struct smbnode *np = VTOSMB(vp);
305 struct uio uio, *uiop = &uio;
307 struct smb_cred scred;
311 uiop->uio_iovcnt = 1;
312 uiop->uio_segflg = UIO_SYSSPACE;
315 smb_makescred(&scred, p, cr);
317 if (bp->b_flags & B_READ) {
318 io.iov_len = uiop->uio_resid = bp->b_bcount;
319 io.iov_base = bp->b_data;
320 uiop->uio_rw = UIO_READ;
321 switch (vp->v_type) {
323 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
324 error = smb_read(smp->sm_share, np->n_fid, uiop, &scred);
327 if (uiop->uio_resid) {
328 int left = uiop->uio_resid;
329 int nread = bp->b_bcount - left;
331 bzero((char *)bp->b_data + nread, left);
335 printf("smbfs_doio: type %x unexpected\n",vp->v_type);
340 bp->b_flags |= B_ERROR;
343 if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
344 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
346 if (bp->b_dirtyend > bp->b_dirtyoff) {
347 io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
348 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
349 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
350 uiop->uio_rw = UIO_WRITE;
351 bp->b_flags |= B_WRITEINPROG;
352 error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
353 bp->b_flags &= ~B_WRITEINPROG;
356 * For an interrupted write, the buffer is still valid
357 * and the write hasn't been pushed to the server yet,
358 * so we can't set BIO_ERROR and report the interruption
359 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
360 * is not relevant, so the rpc attempt is essentially
361 * a noop. For the case of a V3 write rpc not being
362 * committed to stable storage, the block is still
363 * dirty and requires either a commit rpc or another
364 * write rpc with iomode == NFSV3WRITE_FILESYNC before
365 * the block is reused. This is indicated by setting
366 * the B_DELWRI and B_NEEDCOMMIT flags.
369 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
373 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
374 if ((bp->b_flags & B_ASYNC) == 0)
375 bp->b_flags |= B_EINTR;
376 if ((bp->b_flags & B_PAGING) == 0) {
378 bp->b_flags &= ~B_DONE;
380 if ((bp->b_flags & B_ASYNC) == 0)
381 bp->b_flags |= B_EINTR;
385 bp->b_flags |= B_ERROR;
388 bp->b_dirtyoff = bp->b_dirtyend = 0;
396 bp->b_resid = uiop->uio_resid;
402 * Vnode op for VM getpages.
403 * Wish wish .... get rid from multiple IO routines
407 struct vop_getpages_args /* {
412 vm_ooffset_t a_offset;
415 #ifdef SMBFS_RWGENERIC
416 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
419 int i, error, nextoff, size, toff, npages, count;
427 struct smbmount *smp;
429 struct smb_cred scred;
433 p = curproc; /* XXX */
434 cred = curproc->p_ucred; /* XXX */
436 smp = VFSTOSMBFS(vp->v_mount);
440 if (vp->v_object == NULL) {
441 printf("smbfs_getpages: called with non-merged cache vnode??\n");
442 return VM_PAGER_ERROR;
444 smb_makescred(&scred, p, cred);
446 bp = getpbuf(&smbfs_pbuf_freecnt);
447 npages = btoc(count);
448 kva = (vm_offset_t) bp->b_data;
449 pmap_qenter(kva, pages, npages);
451 iov.iov_base = (caddr_t) kva;
455 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
456 uio.uio_resid = count;
457 uio.uio_segflg = UIO_SYSSPACE;
458 uio.uio_rw = UIO_READ;
461 error = smb_read(smp->sm_share, np->n_fid, &uio, &scred);
462 pmap_qremove(kva, npages);
464 relpbuf(bp, &smbfs_pbuf_freecnt);
466 if (error && (uio.uio_resid == count)) {
467 printf("smbfs_getpages: error %d\n",error);
468 for (i = 0; i < npages; i++) {
469 if (ap->a_reqpage != i)
470 vnode_pager_freepage(pages[i]);
472 return VM_PAGER_ERROR;
475 size = count - uio.uio_resid;
477 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
479 nextoff = toff + PAGE_SIZE;
482 m->flags &= ~PG_ZERO;
484 if (nextoff <= size) {
485 m->valid = VM_PAGE_BITS_ALL;
488 int nvalid = ((size + DEV_BSIZE - 1) - toff) & ~(DEV_BSIZE - 1);
489 vm_page_set_validclean(m, 0, nvalid);
492 if (i != ap->a_reqpage) {
494 * Whether or not to leave the page activated is up in
495 * the air, but we should put the page on a page queue
496 * somewhere (it already is in the object). Result:
497 * It appears that emperical results show that
498 * deactivating pages is best.
502 * Just in case someone was asking for this page we
503 * now tell them that it is ok to use.
506 if (m->flags & PG_WANTED)
509 vm_page_deactivate(m);
512 vnode_pager_freepage(m);
517 #endif /* SMBFS_RWGENERIC */
521 * Vnode op for VM putpages.
522 * possible bug: all IO done in sync mode
523 * Note that vop_close always invalidate pages before close, so it's
524 * not necessary to open vnode.
528 struct vop_putpages_args /* {
534 vm_ooffset_t a_offset;
538 struct vnode *vp = ap->a_vp;
542 #ifdef SMBFS_RWGENERIC
543 p = curproc; /* XXX */
544 cred = p->p_ucred; /* XXX */
545 VOP_OPEN(vp, FWRITE, cred, p);
546 error = vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
547 ap->a_sync, ap->a_rtvals);
548 VOP_CLOSE(vp, FWRITE, cred, p);
555 int i, npages, count;
557 struct smbmount *smp;
559 struct smb_cred scred;
562 p = curproc; /* XXX */
563 cred = p->p_ucred; /* XXX */
564 /* VOP_OPEN(vp, FWRITE, cred, p);*/
566 smp = VFSTOSMBFS(vp->v_mount);
569 rtvals = ap->a_rtvals;
570 npages = btoc(count);
572 for (i = 0; i < npages; i++) {
573 rtvals[i] = VM_PAGER_AGAIN;
576 bp = getpbuf(&smbfs_pbuf_freecnt);
577 kva = (vm_offset_t) bp->b_data;
578 pmap_qenter(kva, pages, npages);
580 iov.iov_base = (caddr_t) kva;
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;
589 SMBVDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
591 smb_makescred(&scred, p, cred);
592 error = smb_write(smp->sm_share, np->n_fid, &uio, &scred);
593 /* VOP_CLOSE(vp, FWRITE, cred, p);*/
594 SMBVDEBUG("paged write done: %d\n", error);
596 pmap_qremove(kva, npages);
597 relpbuf(bp, &smbfs_pbuf_freecnt);
600 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
601 for (i = 0; i < nwritten; i++) {
602 rtvals[i] = VM_PAGER_OK;
607 #endif /* SMBFS_RWGENERIC */
611 * Flush and invalidate all dirty buffers. If another process is already
612 * doing the flush, just wait for completion.
615 smbfs_vinvalbuf(vp, flags, cred, p, intrflg)
622 struct smbnode *np = VTOSMB(vp);
623 int error = 0, slpflag, slptimeo;
625 if (vp->v_flag & VXLOCK)
634 while (np->n_flag & NFLUSHINPROG) {
635 np->n_flag |= NFLUSHWANT;
636 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "smfsvinv", slptimeo);
637 error = smb_proc_intr(p);
638 if (error == EINTR && intrflg)
641 np->n_flag |= NFLUSHINPROG;
642 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
644 if (intrflg && (error == ERESTART || error == EINTR)) {
645 np->n_flag &= ~NFLUSHINPROG;
646 if (np->n_flag & NFLUSHWANT) {
647 np->n_flag &= ~NFLUSHWANT;
648 wakeup((caddr_t)&np->n_flag);
652 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
654 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
655 if (np->n_flag & NFLUSHWANT) {
656 np->n_flag &= ~NFLUSHWANT;
657 wakeup((caddr_t)&np->n_flag);