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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/fcntl.h>
35 #include <sys/mount.h>
36 #include <sys/namei.h>
37 #include <sys/vnode.h>
38 #include <sys/dirent.h>
39 #include <sys/signalvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/vmmeter.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_extern.h>
46 #include <vm/vm_object.h>
47 #include <vm/vm_pager.h>
48 #include <vm/vnode_pager.h>
50 #include <sys/ioccom.h>
52 #include <netsmb/smb.h>
53 #include <netsmb/smb_conn.h>
54 #include <netsmb/smb_subr.h>
56 #include <fs/smbfs/smbfs.h>
57 #include <fs/smbfs/smbfs_node.h>
58 #include <fs/smbfs/smbfs_subr.h>
60 /*#define SMBFS_RWGENERIC*/
62 extern int smbfs_pbuf_freecnt;
64 static int smbfs_fastlookup = 1;
66 SYSCTL_DECL(_vfs_smbfs);
67 SYSCTL_INT(_vfs_smbfs, OID_AUTO, fastlookup, CTLFLAG_RW, &smbfs_fastlookup, 0, "");
70 #define DE_SIZE (sizeof(struct dirent))
73 smbfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred)
76 struct componentname cn;
77 struct smb_cred scred;
78 struct smbfs_fctx *ctx;
80 struct smbnode *np = VTOSMB(vp);
81 int error/*, *eofflag = ap->a_eofflag*/;
85 SMBVDEBUG("dirname='%s'\n", np->n_name);
86 smb_makescred(&scred, uio->uio_td, cred);
87 offset = uio->uio_offset / DE_SIZE; /* offset in the directory */
88 limit = uio->uio_resid / DE_SIZE;
89 if (uio->uio_resid < DE_SIZE || uio->uio_offset < 0)
91 while (limit && offset < 2) {
93 bzero((caddr_t)&de, DE_SIZE);
94 de.d_reclen = DE_SIZE;
95 de.d_fileno = (offset == 0) ? np->n_ino :
96 (np->n_parent ? VTOSMB(np->n_parent)->n_ino : 2);
98 de.d_fileno = 0x7ffffffd + offset;
99 de.d_namlen = offset + 1;
102 de.d_name[offset + 1] = '\0';
104 error = uiomove(&de, DE_SIZE, uio);
108 uio->uio_offset += DE_SIZE;
112 if (offset != np->n_dirofs || np->n_dirseq == NULL) {
113 SMBVDEBUG("Reopening search %ld:%ld\n", offset, np->n_dirofs);
115 smbfs_findclose(np->n_dirseq, &scred);
119 error = smbfs_findopen(np, "*", 1,
120 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR,
123 SMBVDEBUG("can not open search, error = %d", error);
129 while (np->n_dirofs < offset) {
130 error = smbfs_findnext(ctx, offset - np->n_dirofs++, &scred);
132 smbfs_findclose(np->n_dirseq, &scred);
134 return error == ENOENT ? 0 : error;
138 for (; limit; limit--, offset++) {
139 error = smbfs_findnext(ctx, limit, &scred);
143 bzero((caddr_t)&de, DE_SIZE);
144 de.d_reclen = DE_SIZE;
145 de.d_fileno = ctx->f_attr.fa_ino;
146 de.d_type = (ctx->f_attr.fa_attr & SMB_FA_DIR) ? DT_DIR : DT_REG;
147 de.d_namlen = ctx->f_nmlen;
148 bcopy(ctx->f_name, de.d_name, de.d_namlen);
149 de.d_name[de.d_namlen] = '\0';
150 if (smbfs_fastlookup) {
151 error = smbfs_nget(vp->v_mount, vp, ctx->f_name,
152 ctx->f_nmlen, &ctx->f_attr, &newvp);
154 cn.cn_nameptr = de.d_name;
155 cn.cn_namelen = de.d_namlen;
156 cache_enter(vp, newvp, &cn);
160 error = uiomove(&de, DE_SIZE, uio);
166 uio->uio_offset = offset * DE_SIZE;
171 smbfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred)
173 struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
174 struct smbnode *np = VTOSMB(vp);
177 struct smb_cred scred;
181 * Protect against method which is not supported for now
183 if (uiop->uio_segflg == UIO_NOCOPY)
186 if (vp->v_type != VREG && vp->v_type != VDIR) {
187 SMBFSERR("vn types other than VREG or VDIR are unsupported !\n");
190 if (uiop->uio_resid == 0)
192 if (uiop->uio_offset < 0)
194 /* if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
197 if (vp->v_type == VDIR) {
198 lks = LK_EXCLUSIVE; /* lockstatus(vp->v_vnlock); */
199 if (lks == LK_SHARED)
200 vn_lock(vp, LK_UPGRADE | LK_RETRY);
201 error = smbfs_readvdir(vp, uiop, cred);
202 if (lks == LK_SHARED)
203 vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
207 /* biosize = SSTOCN(smp->sm_share)->sc_txmax;*/
208 if (np->n_flag & NMODIFIED) {
209 smbfs_attr_cacheremove(vp);
210 error = VOP_GETATTR(vp, &vattr, cred);
213 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
215 error = VOP_GETATTR(vp, &vattr, cred);
218 if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
219 error = smbfs_vinvalbuf(vp, td);
222 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
225 smb_makescred(&scred, td, cred);
226 return smb_read(smp->sm_share, np->n_fid, uiop, &scred);
230 smbfs_writevnode(struct vnode *vp, struct uio *uiop,
231 struct ucred *cred, int ioflag)
233 struct smbmount *smp = VTOSMBFS(vp);
234 struct smbnode *np = VTOSMB(vp);
235 struct smb_cred scred;
239 if (vp->v_type != VREG) {
240 SMBERROR("vn types other than VREG unsupported !\n");
243 SMBVDEBUG("ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
244 if (uiop->uio_offset < 0)
246 /* if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
249 if (ioflag & (IO_APPEND | IO_SYNC)) {
250 if (np->n_flag & NMODIFIED) {
251 smbfs_attr_cacheremove(vp);
252 error = smbfs_vinvalbuf(vp, td);
256 if (ioflag & IO_APPEND) {
259 * File size can be changed by another client
261 smbfs_attr_cacheremove(vp);
262 error = VOP_GETATTR(vp, &vattr, cred);
263 if (error) return (error);
265 uiop->uio_offset = np->n_size;
268 if (uiop->uio_resid == 0)
271 if (vn_rlimit_fsize(vp, uiop, td))
274 smb_makescred(&scred, td, cred);
275 error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
276 SMBVDEBUG("after: ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
278 if (uiop->uio_offset > np->n_size) {
279 np->n_size = uiop->uio_offset;
280 vnode_pager_setsize(vp, np->n_size);
287 * Do an I/O operation to/from a cache block.
290 smbfs_doio(struct vnode *vp, struct buf *bp, struct ucred *cr, struct thread *td)
292 struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
293 struct smbnode *np = VTOSMB(vp);
294 struct uio uio, *uiop = &uio;
296 struct smb_cred scred;
300 uiop->uio_iovcnt = 1;
301 uiop->uio_segflg = UIO_SYSSPACE;
304 smb_makescred(&scred, td, cr);
306 if (bp->b_iocmd == BIO_READ) {
307 io.iov_len = uiop->uio_resid = bp->b_bcount;
308 io.iov_base = bp->b_data;
309 uiop->uio_rw = UIO_READ;
310 switch (vp->v_type) {
312 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
313 error = smb_read(smp->sm_share, np->n_fid, uiop, &scred);
316 if (uiop->uio_resid) {
317 int left = uiop->uio_resid;
318 int nread = bp->b_bcount - left;
320 bzero((char *)bp->b_data + nread, left);
324 printf("smbfs_doio: type %x unexpected\n",vp->v_type);
329 bp->b_ioflags |= BIO_ERROR;
332 if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
333 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
335 if (bp->b_dirtyend > bp->b_dirtyoff) {
336 io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
337 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
338 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
339 uiop->uio_rw = UIO_WRITE;
340 error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
343 * For an interrupted write, the buffer is still valid
344 * and the write hasn't been pushed to the server yet,
345 * so we can't set BIO_ERROR and report the interruption
346 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
347 * is not relevant, so the rpc attempt is essentially
348 * a noop. For the case of a V3 write rpc not being
349 * committed to stable storage, the block is still
350 * dirty and requires either a commit rpc or another
351 * write rpc with iomode == NFSV3WRITE_FILESYNC before
352 * the block is reused. This is indicated by setting
353 * the B_DELWRI and B_NEEDCOMMIT flags.
356 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
360 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
361 if ((bp->b_flags & B_ASYNC) == 0)
362 bp->b_flags |= B_EINTR;
363 if ((bp->b_flags & B_PAGING) == 0) {
365 bp->b_flags &= ~B_DONE;
367 if ((bp->b_flags & B_ASYNC) == 0)
368 bp->b_flags |= B_EINTR;
372 bp->b_ioflags |= BIO_ERROR;
375 bp->b_dirtyoff = bp->b_dirtyend = 0;
383 bp->b_resid = uiop->uio_resid;
389 * Vnode op for VM getpages.
390 * Wish wish .... get rid from multiple IO routines
394 struct vop_getpages_args /* {
399 vm_ooffset_t a_offset;
402 #ifdef SMBFS_RWGENERIC
403 return vop_stdgetpages(ap);
405 int i, error, nextoff, size, toff, npages, count, reqpage;
413 struct smbmount *smp;
415 struct smb_cred scred;
420 if ((object = vp->v_object) == NULL) {
421 printf("smbfs_getpages: called with non-merged cache vnode??\n");
422 return VM_PAGER_ERROR;
425 td = curthread; /* XXX */
426 cred = td->td_ucred; /* XXX */
428 smp = VFSTOSMBFS(vp->v_mount);
431 npages = btoc(count);
432 reqpage = ap->a_reqpage;
435 * If the requested page is partially valid, just return it and
436 * allow the pager to zero-out the blanks. Partially valid pages
437 * can only occur at the file EOF.
441 VM_OBJECT_LOCK(object);
443 for (i = 0; i < npages; ++i) {
445 vm_page_lock(pages[i]);
446 vm_page_free(pages[i]);
447 vm_page_unlock(pages[i]);
450 VM_OBJECT_UNLOCK(object);
453 VM_OBJECT_UNLOCK(object);
455 smb_makescred(&scred, td, cred);
457 bp = getpbuf(&smbfs_pbuf_freecnt);
459 kva = (vm_offset_t) bp->b_data;
460 pmap_qenter(kva, pages, npages);
461 PCPU_INC(cnt.v_vnodein);
462 PCPU_ADD(cnt.v_vnodepgsin, npages);
464 iov.iov_base = (caddr_t) kva;
468 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
469 uio.uio_resid = count;
470 uio.uio_segflg = UIO_SYSSPACE;
471 uio.uio_rw = UIO_READ;
474 error = smb_read(smp->sm_share, np->n_fid, &uio, &scred);
475 pmap_qremove(kva, npages);
477 relpbuf(bp, &smbfs_pbuf_freecnt);
479 VM_OBJECT_LOCK(object);
480 if (error && (uio.uio_resid == count)) {
481 printf("smbfs_getpages: error %d\n",error);
482 for (i = 0; i < npages; i++) {
484 vm_page_lock(pages[i]);
485 vm_page_free(pages[i]);
486 vm_page_unlock(pages[i]);
489 VM_OBJECT_UNLOCK(object);
490 return VM_PAGER_ERROR;
493 size = count - uio.uio_resid;
495 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
497 nextoff = toff + PAGE_SIZE;
500 if (nextoff <= size) {
502 * Read operation filled an entire page
504 m->valid = VM_PAGE_BITS_ALL;
505 KASSERT(m->dirty == 0,
506 ("smbfs_getpages: page %p is dirty", m));
507 } else if (size > toff) {
509 * Read operation filled a partial page.
512 vm_page_set_valid(m, 0, size - toff);
513 KASSERT(m->dirty == 0,
514 ("smbfs_getpages: page %p is dirty", m));
517 * Read operation was short. If no error occured
518 * we may have hit a zero-fill section. We simply
519 * leave valid set to 0.
526 * Whether or not to leave the page activated is up in
527 * the air, but we should put the page on a page queue
528 * somewhere (it already is in the object). Result:
529 * It appears that emperical results show that
530 * deactivating pages is best.
534 * Just in case someone was asking for this page we
535 * now tell them that it is ok to use.
538 if (m->oflags & VPO_WANTED) {
544 vm_page_deactivate(m);
555 VM_OBJECT_UNLOCK(object);
557 #endif /* SMBFS_RWGENERIC */
561 * Vnode op for VM putpages.
562 * possible bug: all IO done in sync mode
563 * Note that vop_close always invalidate pages before close, so it's
564 * not necessary to open vnode.
568 struct vop_putpages_args /* {
574 vm_ooffset_t a_offset;
578 struct vnode *vp = ap->a_vp;
582 #ifdef SMBFS_RWGENERIC
583 td = curthread; /* XXX */
584 cred = td->td_ucred; /* XXX */
585 VOP_OPEN(vp, FWRITE, cred, td, NULL);
586 error = vop_stdputpages(ap);
587 VOP_CLOSE(vp, FWRITE, cred, td);
594 int i, npages, count;
596 struct smbmount *smp;
598 struct smb_cred scred;
601 td = curthread; /* XXX */
602 cred = td->td_ucred; /* XXX */
603 /* VOP_OPEN(vp, FWRITE, cred, td, NULL);*/
605 smp = VFSTOSMBFS(vp->v_mount);
608 rtvals = ap->a_rtvals;
609 npages = btoc(count);
611 for (i = 0; i < npages; i++) {
612 rtvals[i] = VM_PAGER_ERROR;
615 bp = getpbuf(&smbfs_pbuf_freecnt);
617 kva = (vm_offset_t) bp->b_data;
618 pmap_qenter(kva, pages, npages);
619 PCPU_INC(cnt.v_vnodeout);
620 PCPU_ADD(cnt.v_vnodepgsout, count);
622 iov.iov_base = (caddr_t) kva;
626 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
627 uio.uio_resid = count;
628 uio.uio_segflg = UIO_SYSSPACE;
629 uio.uio_rw = UIO_WRITE;
631 SMBVDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
633 smb_makescred(&scred, td, cred);
634 error = smb_write(smp->sm_share, np->n_fid, &uio, &scred);
635 /* VOP_CLOSE(vp, FWRITE, cred, td);*/
636 SMBVDEBUG("paged write done: %d\n", error);
638 pmap_qremove(kva, npages);
640 relpbuf(bp, &smbfs_pbuf_freecnt);
643 vnode_pager_undirty_pages(pages, rtvals, count - uio.uio_resid);
645 #endif /* SMBFS_RWGENERIC */
649 * Flush and invalidate all dirty buffers. If another process is already
650 * doing the flush, just wait for completion.
653 smbfs_vinvalbuf(struct vnode *vp, struct thread *td)
655 struct smbnode *np = VTOSMB(vp);
658 if (vp->v_iflag & VI_DOOMED)
661 while (np->n_flag & NFLUSHINPROG) {
662 np->n_flag |= NFLUSHWANT;
663 error = tsleep(&np->n_flag, PRIBIO + 2, "smfsvinv", 2 * hz);
664 error = smb_td_intr(td);
668 np->n_flag |= NFLUSHINPROG;
670 if (vp->v_bufobj.bo_object != NULL) {
671 VM_OBJECT_LOCK(vp->v_bufobj.bo_object);
672 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
673 VM_OBJECT_UNLOCK(vp->v_bufobj.bo_object);
676 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
678 if (error == ERESTART || error == EINTR) {
679 np->n_flag &= ~NFLUSHINPROG;
680 if (np->n_flag & NFLUSHWANT) {
681 np->n_flag &= ~NFLUSHWANT;
686 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
688 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
689 if (np->n_flag & NFLUSHWANT) {
690 np->n_flag &= ~NFLUSHWANT;