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_param.h>
45 #include <vm/vm_page.h>
46 #include <vm/vm_extern.h>
47 #include <vm/vm_object.h>
48 #include <vm/vm_pager.h>
49 #include <vm/vnode_pager.h>
51 #include <sys/ioccom.h>
53 #include <netsmb/smb.h>
54 #include <netsmb/smb_conn.h>
55 #include <netsmb/smb_subr.h>
57 #include <fs/smbfs/smbfs.h>
58 #include <fs/smbfs/smbfs_node.h>
59 #include <fs/smbfs/smbfs_subr.h>
61 /*#define SMBFS_RWGENERIC*/
63 extern int smbfs_pbuf_freecnt;
65 static int smbfs_fastlookup = 1;
67 SYSCTL_DECL(_vfs_smbfs);
68 SYSCTL_INT(_vfs_smbfs, OID_AUTO, fastlookup, CTLFLAG_RW, &smbfs_fastlookup, 0, "");
71 #define DE_SIZE (sizeof(struct dirent))
74 smbfs_readvdir(struct vnode *vp, struct uio *uio, struct ucred *cred)
77 struct componentname cn;
78 struct smb_cred scred;
79 struct smbfs_fctx *ctx;
81 struct smbnode *np = VTOSMB(vp);
82 int error/*, *eofflag = ap->a_eofflag*/;
86 SMBVDEBUG("dirname='%s'\n", np->n_name);
87 smb_makescred(&scred, uio->uio_td, cred);
88 offset = uio->uio_offset / DE_SIZE; /* offset in the directory */
89 limit = uio->uio_resid / DE_SIZE;
90 if (uio->uio_resid < DE_SIZE || uio->uio_offset < 0)
92 while (limit && offset < 2) {
94 bzero((caddr_t)&de, DE_SIZE);
95 de.d_reclen = DE_SIZE;
96 de.d_fileno = (offset == 0) ? np->n_ino :
97 (np->n_parent ? VTOSMB(np->n_parent)->n_ino : 2);
99 de.d_fileno = 0x7ffffffd + offset;
100 de.d_namlen = offset + 1;
103 de.d_name[offset + 1] = '\0';
105 error = uiomove(&de, DE_SIZE, uio);
109 uio->uio_offset += DE_SIZE;
113 if (offset != np->n_dirofs || np->n_dirseq == NULL) {
114 SMBVDEBUG("Reopening search %ld:%ld\n", offset, np->n_dirofs);
116 smbfs_findclose(np->n_dirseq, &scred);
120 error = smbfs_findopen(np, "*", 1,
121 SMB_FA_SYSTEM | SMB_FA_HIDDEN | SMB_FA_DIR,
124 SMBVDEBUG("can not open search, error = %d", error);
130 while (np->n_dirofs < offset) {
131 error = smbfs_findnext(ctx, offset - np->n_dirofs++, &scred);
133 smbfs_findclose(np->n_dirseq, &scred);
135 return error == ENOENT ? 0 : error;
139 for (; limit; limit--, offset++) {
140 error = smbfs_findnext(ctx, limit, &scred);
144 bzero((caddr_t)&de, DE_SIZE);
145 de.d_reclen = DE_SIZE;
146 de.d_fileno = ctx->f_attr.fa_ino;
147 de.d_type = (ctx->f_attr.fa_attr & SMB_FA_DIR) ? DT_DIR : DT_REG;
148 de.d_namlen = ctx->f_nmlen;
149 bcopy(ctx->f_name, de.d_name, de.d_namlen);
150 de.d_name[de.d_namlen] = '\0';
151 if (smbfs_fastlookup) {
152 error = smbfs_nget(vp->v_mount, vp, ctx->f_name,
153 ctx->f_nmlen, &ctx->f_attr, &newvp);
155 cn.cn_nameptr = de.d_name;
156 cn.cn_namelen = de.d_namlen;
157 cache_enter(vp, newvp, &cn);
161 error = uiomove(&de, DE_SIZE, uio);
167 uio->uio_offset = offset * DE_SIZE;
172 smbfs_readvnode(struct vnode *vp, struct uio *uiop, struct ucred *cred)
174 struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
175 struct smbnode *np = VTOSMB(vp);
178 struct smb_cred scred;
182 * Protect against method which is not supported for now
184 if (uiop->uio_segflg == UIO_NOCOPY)
187 if (vp->v_type != VREG && vp->v_type != VDIR) {
188 SMBFSERR("vn types other than VREG or VDIR are unsupported !\n");
191 if (uiop->uio_resid == 0)
193 if (uiop->uio_offset < 0)
195 /* if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
198 if (vp->v_type == VDIR) {
199 lks = LK_EXCLUSIVE; /* lockstatus(vp->v_vnlock); */
200 if (lks == LK_SHARED)
201 vn_lock(vp, LK_UPGRADE | LK_RETRY);
202 error = smbfs_readvdir(vp, uiop, cred);
203 if (lks == LK_SHARED)
204 vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
208 /* biosize = SSTOCN(smp->sm_share)->sc_txmax;*/
209 if (np->n_flag & NMODIFIED) {
210 smbfs_attr_cacheremove(vp);
211 error = VOP_GETATTR(vp, &vattr, cred);
214 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
216 error = VOP_GETATTR(vp, &vattr, cred);
219 if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
220 error = smbfs_vinvalbuf(vp, td);
223 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
226 smb_makescred(&scred, td, cred);
227 return smb_read(smp->sm_share, np->n_fid, uiop, &scred);
231 smbfs_writevnode(struct vnode *vp, struct uio *uiop,
232 struct ucred *cred, int ioflag)
234 struct smbmount *smp = VTOSMBFS(vp);
235 struct smbnode *np = VTOSMB(vp);
236 struct smb_cred scred;
240 if (vp->v_type != VREG) {
241 SMBERROR("vn types other than VREG unsupported !\n");
244 SMBVDEBUG("ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
245 if (uiop->uio_offset < 0)
247 /* if (uiop->uio_offset + uiop->uio_resid > smp->nm_maxfilesize)
250 if (ioflag & (IO_APPEND | IO_SYNC)) {
251 if (np->n_flag & NMODIFIED) {
252 smbfs_attr_cacheremove(vp);
253 error = smbfs_vinvalbuf(vp, td);
257 if (ioflag & IO_APPEND) {
260 * File size can be changed by another client
262 smbfs_attr_cacheremove(vp);
263 error = VOP_GETATTR(vp, &vattr, cred);
264 if (error) return (error);
266 uiop->uio_offset = np->n_size;
269 if (uiop->uio_resid == 0)
272 if (vn_rlimit_fsize(vp, uiop, td))
275 smb_makescred(&scred, td, cred);
276 error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
277 SMBVDEBUG("after: ofs=%d,resid=%d\n",(int)uiop->uio_offset, uiop->uio_resid);
279 if (uiop->uio_offset > np->n_size) {
280 np->n_size = uiop->uio_offset;
281 vnode_pager_setsize(vp, np->n_size);
288 * Do an I/O operation to/from a cache block.
291 smbfs_doio(struct vnode *vp, struct buf *bp, struct ucred *cr, struct thread *td)
293 struct smbmount *smp = VFSTOSMBFS(vp->v_mount);
294 struct smbnode *np = VTOSMB(vp);
295 struct uio uio, *uiop = &uio;
297 struct smb_cred scred;
301 uiop->uio_iovcnt = 1;
302 uiop->uio_segflg = UIO_SYSSPACE;
305 smb_makescred(&scred, td, cr);
307 if (bp->b_iocmd == BIO_READ) {
308 io.iov_len = uiop->uio_resid = bp->b_bcount;
309 io.iov_base = bp->b_data;
310 uiop->uio_rw = UIO_READ;
311 switch (vp->v_type) {
313 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
314 error = smb_read(smp->sm_share, np->n_fid, uiop, &scred);
317 if (uiop->uio_resid) {
318 int left = uiop->uio_resid;
319 int nread = bp->b_bcount - left;
321 bzero((char *)bp->b_data + nread, left);
325 printf("smbfs_doio: type %x unexpected\n",vp->v_type);
330 bp->b_ioflags |= BIO_ERROR;
333 if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
334 bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
336 if (bp->b_dirtyend > bp->b_dirtyoff) {
337 io.iov_len = uiop->uio_resid = bp->b_dirtyend - bp->b_dirtyoff;
338 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
339 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
340 uiop->uio_rw = UIO_WRITE;
341 error = smb_write(smp->sm_share, np->n_fid, uiop, &scred);
344 * For an interrupted write, the buffer is still valid
345 * and the write hasn't been pushed to the server yet,
346 * so we can't set BIO_ERROR and report the interruption
347 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
348 * is not relevant, so the rpc attempt is essentially
349 * a noop. For the case of a V3 write rpc not being
350 * committed to stable storage, the block is still
351 * dirty and requires either a commit rpc or another
352 * write rpc with iomode == NFSV3WRITE_FILESYNC before
353 * the block is reused. This is indicated by setting
354 * the B_DELWRI and B_NEEDCOMMIT flags.
357 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
361 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
362 if ((bp->b_flags & B_ASYNC) == 0)
363 bp->b_flags |= B_EINTR;
364 if ((bp->b_flags & B_PAGING) == 0) {
366 bp->b_flags &= ~B_DONE;
368 if ((bp->b_flags & B_ASYNC) == 0)
369 bp->b_flags |= B_EINTR;
373 bp->b_ioflags |= BIO_ERROR;
376 bp->b_dirtyoff = bp->b_dirtyend = 0;
384 bp->b_resid = uiop->uio_resid;
390 * Vnode op for VM getpages.
391 * Wish wish .... get rid from multiple IO routines
395 struct vop_getpages_args /* {
400 vm_ooffset_t a_offset;
403 #ifdef SMBFS_RWGENERIC
404 return vop_stdgetpages(ap);
406 int i, error, nextoff, size, toff, npages, count, reqpage;
414 struct smbmount *smp;
416 struct smb_cred scred;
421 if ((object = vp->v_object) == NULL) {
422 printf("smbfs_getpages: called with non-merged cache vnode??\n");
423 return VM_PAGER_ERROR;
426 td = curthread; /* XXX */
427 cred = td->td_ucred; /* XXX */
429 smp = VFSTOSMBFS(vp->v_mount);
432 npages = btoc(count);
433 reqpage = ap->a_reqpage;
436 * If the requested page is partially valid, just return it and
437 * allow the pager to zero-out the blanks. Partially valid pages
438 * can only occur at the file EOF.
442 VM_OBJECT_LOCK(object);
444 for (i = 0; i < npages; ++i) {
446 vm_page_lock(pages[i]);
447 vm_page_free(pages[i]);
448 vm_page_unlock(pages[i]);
451 VM_OBJECT_UNLOCK(object);
454 VM_OBJECT_UNLOCK(object);
456 smb_makescred(&scred, td, cred);
458 bp = getpbuf(&smbfs_pbuf_freecnt);
460 kva = (vm_offset_t) bp->b_data;
461 pmap_qenter(kva, pages, npages);
462 PCPU_INC(cnt.v_vnodein);
463 PCPU_ADD(cnt.v_vnodepgsin, npages);
465 iov.iov_base = (caddr_t) kva;
469 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
470 uio.uio_resid = count;
471 uio.uio_segflg = UIO_SYSSPACE;
472 uio.uio_rw = UIO_READ;
475 error = smb_read(smp->sm_share, np->n_fid, &uio, &scred);
476 pmap_qremove(kva, npages);
478 relpbuf(bp, &smbfs_pbuf_freecnt);
480 VM_OBJECT_LOCK(object);
481 if (error && (uio.uio_resid == count)) {
482 printf("smbfs_getpages: error %d\n",error);
483 for (i = 0; i < npages; i++) {
485 vm_page_lock(pages[i]);
486 vm_page_free(pages[i]);
487 vm_page_unlock(pages[i]);
490 VM_OBJECT_UNLOCK(object);
491 return VM_PAGER_ERROR;
494 size = count - uio.uio_resid;
496 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
498 nextoff = toff + PAGE_SIZE;
501 if (nextoff <= size) {
503 * Read operation filled an entire page
505 m->valid = VM_PAGE_BITS_ALL;
506 KASSERT(m->dirty == 0,
507 ("smbfs_getpages: page %p is dirty", m));
508 } else if (size > toff) {
510 * Read operation filled a partial page.
513 vm_page_set_valid(m, 0, size - toff);
514 KASSERT(m->dirty == 0,
515 ("smbfs_getpages: page %p is dirty", m));
518 * Read operation was short. If no error occured
519 * we may have hit a zero-fill section. We simply
520 * leave valid set to 0.
526 vm_page_readahead_finish(m);
528 VM_OBJECT_UNLOCK(object);
530 #endif /* SMBFS_RWGENERIC */
534 * Vnode op for VM putpages.
535 * possible bug: all IO done in sync mode
536 * Note that vop_close always invalidate pages before close, so it's
537 * not necessary to open vnode.
541 struct vop_putpages_args /* {
547 vm_ooffset_t a_offset;
551 struct vnode *vp = ap->a_vp;
555 #ifdef SMBFS_RWGENERIC
556 td = curthread; /* XXX */
557 cred = td->td_ucred; /* XXX */
558 VOP_OPEN(vp, FWRITE, cred, td, NULL);
559 error = vop_stdputpages(ap);
560 VOP_CLOSE(vp, FWRITE, cred, td);
567 int i, npages, count;
569 struct smbmount *smp;
571 struct smb_cred scred;
574 td = curthread; /* XXX */
575 cred = td->td_ucred; /* XXX */
576 /* VOP_OPEN(vp, FWRITE, cred, td, NULL);*/
578 smp = VFSTOSMBFS(vp->v_mount);
581 rtvals = ap->a_rtvals;
582 npages = btoc(count);
584 for (i = 0; i < npages; i++) {
585 rtvals[i] = VM_PAGER_ERROR;
588 bp = getpbuf(&smbfs_pbuf_freecnt);
590 kva = (vm_offset_t) bp->b_data;
591 pmap_qenter(kva, pages, npages);
592 PCPU_INC(cnt.v_vnodeout);
593 PCPU_ADD(cnt.v_vnodepgsout, count);
595 iov.iov_base = (caddr_t) kva;
599 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
600 uio.uio_resid = count;
601 uio.uio_segflg = UIO_SYSSPACE;
602 uio.uio_rw = UIO_WRITE;
604 SMBVDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
606 smb_makescred(&scred, td, cred);
607 error = smb_write(smp->sm_share, np->n_fid, &uio, &scred);
608 /* VOP_CLOSE(vp, FWRITE, cred, td);*/
609 SMBVDEBUG("paged write done: %d\n", error);
611 pmap_qremove(kva, npages);
613 relpbuf(bp, &smbfs_pbuf_freecnt);
616 vnode_pager_undirty_pages(pages, rtvals, count - uio.uio_resid);
618 #endif /* SMBFS_RWGENERIC */
622 * Flush and invalidate all dirty buffers. If another process is already
623 * doing the flush, just wait for completion.
626 smbfs_vinvalbuf(struct vnode *vp, struct thread *td)
628 struct smbnode *np = VTOSMB(vp);
631 if (vp->v_iflag & VI_DOOMED)
634 while (np->n_flag & NFLUSHINPROG) {
635 np->n_flag |= NFLUSHWANT;
636 error = tsleep(&np->n_flag, PRIBIO + 2, "smfsvinv", 2 * hz);
637 error = smb_td_intr(td);
641 np->n_flag |= NFLUSHINPROG;
643 if (vp->v_bufobj.bo_object != NULL) {
644 VM_OBJECT_LOCK(vp->v_bufobj.bo_object);
645 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
646 VM_OBJECT_UNLOCK(vp->v_bufobj.bo_object);
649 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
651 if (error == ERESTART || error == EINTR) {
652 np->n_flag &= ~NFLUSHINPROG;
653 if (np->n_flag & NFLUSHWANT) {
654 np->n_flag &= ~NFLUSHWANT;
659 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
661 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
662 if (np->n_flag & NFLUSHWANT) {
663 np->n_flag &= ~NFLUSHWANT;