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;
501 vm_page_lock_queues();
503 if (nextoff <= size) {
505 * Read operation filled an entire page
507 m->valid = VM_PAGE_BITS_ALL;
508 KASSERT(m->dirty == 0,
509 ("smbfs_getpages: page %p is dirty", m));
510 } else if (size > toff) {
512 * Read operation filled a partial page.
515 vm_page_set_valid(m, 0, size - toff);
516 KASSERT(m->dirty == 0,
517 ("smbfs_getpages: page %p is dirty", m));
520 * Read operation was short. If no error occured
521 * we may have hit a zero-fill section. We simply
522 * leave valid set to 0.
529 * Whether or not to leave the page activated is up in
530 * the air, but we should put the page on a page queue
531 * somewhere (it already is in the object). Result:
532 * It appears that emperical results show that
533 * deactivating pages is best.
537 * Just in case someone was asking for this page we
538 * now tell them that it is ok to use.
541 if (m->oflags & VPO_WANTED)
544 vm_page_deactivate(m);
551 vm_page_unlock_queues();
554 VM_OBJECT_UNLOCK(object);
556 #endif /* SMBFS_RWGENERIC */
560 * Vnode op for VM putpages.
561 * possible bug: all IO done in sync mode
562 * Note that vop_close always invalidate pages before close, so it's
563 * not necessary to open vnode.
567 struct vop_putpages_args /* {
573 vm_ooffset_t a_offset;
577 struct vnode *vp = ap->a_vp;
581 #ifdef SMBFS_RWGENERIC
582 td = curthread; /* XXX */
583 cred = td->td_ucred; /* XXX */
584 VOP_OPEN(vp, FWRITE, cred, td, NULL);
585 error = vop_stdputpages(ap);
586 VOP_CLOSE(vp, FWRITE, cred, td);
593 int i, npages, count;
595 struct smbmount *smp;
597 struct smb_cred scred;
600 td = curthread; /* XXX */
601 cred = td->td_ucred; /* XXX */
602 /* VOP_OPEN(vp, FWRITE, cred, td, NULL);*/
604 smp = VFSTOSMBFS(vp->v_mount);
607 rtvals = ap->a_rtvals;
608 npages = btoc(count);
610 for (i = 0; i < npages; i++) {
611 rtvals[i] = VM_PAGER_AGAIN;
614 bp = getpbuf(&smbfs_pbuf_freecnt);
616 kva = (vm_offset_t) bp->b_data;
617 pmap_qenter(kva, pages, npages);
618 PCPU_INC(cnt.v_vnodeout);
619 PCPU_ADD(cnt.v_vnodepgsout, count);
621 iov.iov_base = (caddr_t) kva;
625 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
626 uio.uio_resid = count;
627 uio.uio_segflg = UIO_SYSSPACE;
628 uio.uio_rw = UIO_WRITE;
630 SMBVDEBUG("ofs=%d,resid=%d\n",(int)uio.uio_offset, uio.uio_resid);
632 smb_makescred(&scred, td, cred);
633 error = smb_write(smp->sm_share, np->n_fid, &uio, &scred);
634 /* VOP_CLOSE(vp, FWRITE, cred, td);*/
635 SMBVDEBUG("paged write done: %d\n", error);
637 pmap_qremove(kva, npages);
639 relpbuf(bp, &smbfs_pbuf_freecnt);
642 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
643 vm_page_lock_queues();
644 for (i = 0; i < nwritten; i++) {
645 rtvals[i] = VM_PAGER_OK;
646 vm_page_undirty(pages[i]);
648 vm_page_unlock_queues();
651 #endif /* SMBFS_RWGENERIC */
655 * Flush and invalidate all dirty buffers. If another process is already
656 * doing the flush, just wait for completion.
659 smbfs_vinvalbuf(struct vnode *vp, struct thread *td)
661 struct smbnode *np = VTOSMB(vp);
664 if (vp->v_iflag & VI_DOOMED)
667 while (np->n_flag & NFLUSHINPROG) {
668 np->n_flag |= NFLUSHWANT;
669 error = tsleep(&np->n_flag, PRIBIO + 2, "smfsvinv", 2 * hz);
670 error = smb_td_intr(td);
674 np->n_flag |= NFLUSHINPROG;
676 if (vp->v_bufobj.bo_object != NULL) {
677 VM_OBJECT_LOCK(vp->v_bufobj.bo_object);
678 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
679 VM_OBJECT_UNLOCK(vp->v_bufobj.bo_object);
682 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
684 if (error == ERESTART || error == EINTR) {
685 np->n_flag &= ~NFLUSHINPROG;
686 if (np->n_flag & NFLUSHWANT) {
687 np->n_flag &= ~NFLUSHWANT;
692 error = vinvalbuf(vp, V_SAVE, PCATCH, 0);
694 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
695 if (np->n_flag & NFLUSHWANT) {
696 np->n_flag &= ~NFLUSHWANT;