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_vnops.c,v 1.2.2.8 2003/04/04 08:57:23 tjr Exp $
33 * $DragonFly: src/sys/vfs/smbfs/smbfs_vnops.c,v 1.4 2003/06/25 03:55:52 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/namei.h>
40 #include <sys/fcntl.h>
41 #include <sys/mount.h>
42 #include <sys/unistd.h>
43 #include <sys/vnode.h>
44 #include <sys/lockf.h>
47 #include <vm/vm_extern.h>
48 #include <vm/vm_zone.h>
51 #include <netsmb/smb.h>
52 #include <netsmb/smb_conn.h>
53 #include <netsmb/smb_subr.h>
55 #include <fs/smbfs/smbfs.h>
56 #include <fs/smbfs/smbfs_node.h>
57 #include <fs/smbfs/smbfs_subr.h>
62 * Prototypes for SMBFS vnode operations
64 static int smbfs_create(struct vop_create_args *);
65 static int smbfs_mknod(struct vop_mknod_args *);
66 static int smbfs_open(struct vop_open_args *);
67 static int smbfs_close(struct vop_close_args *);
68 static int smbfs_access(struct vop_access_args *);
69 static int smbfs_getattr(struct vop_getattr_args *);
70 static int smbfs_setattr(struct vop_setattr_args *);
71 static int smbfs_read(struct vop_read_args *);
72 static int smbfs_write(struct vop_write_args *);
73 static int smbfs_fsync(struct vop_fsync_args *);
74 static int smbfs_remove(struct vop_remove_args *);
75 static int smbfs_link(struct vop_link_args *);
76 static int smbfs_lookup(struct vop_lookup_args *);
77 static int smbfs_rename(struct vop_rename_args *);
78 static int smbfs_mkdir(struct vop_mkdir_args *);
79 static int smbfs_rmdir(struct vop_rmdir_args *);
80 static int smbfs_symlink(struct vop_symlink_args *);
81 static int smbfs_readdir(struct vop_readdir_args *);
82 static int smbfs_bmap(struct vop_bmap_args *);
83 static int smbfs_strategy(struct vop_strategy_args *);
84 static int smbfs_print(struct vop_print_args *);
85 static int smbfs_pathconf(struct vop_pathconf_args *ap);
86 static int smbfs_advlock(struct vop_advlock_args *);
87 static int smbfs_getextattr(struct vop_getextattr_args *ap);
89 vop_t **smbfs_vnodeop_p;
90 static struct vnodeopv_entry_desc smbfs_vnodeop_entries[] = {
91 { &vop_default_desc, (vop_t *) vop_defaultop },
92 { &vop_access_desc, (vop_t *) smbfs_access },
93 { &vop_advlock_desc, (vop_t *) smbfs_advlock },
94 { &vop_bmap_desc, (vop_t *) smbfs_bmap },
95 { &vop_close_desc, (vop_t *) smbfs_close },
96 { &vop_create_desc, (vop_t *) smbfs_create },
97 { &vop_fsync_desc, (vop_t *) smbfs_fsync },
98 { &vop_getattr_desc, (vop_t *) smbfs_getattr },
99 { &vop_getpages_desc, (vop_t *) smbfs_getpages },
100 { &vop_inactive_desc, (vop_t *) smbfs_inactive },
101 { &vop_ioctl_desc, (vop_t *) smbfs_ioctl },
102 { &vop_islocked_desc, (vop_t *) vop_stdislocked },
103 { &vop_link_desc, (vop_t *) smbfs_link },
104 { &vop_lock_desc, (vop_t *) vop_stdlock },
105 { &vop_lookup_desc, (vop_t *) smbfs_lookup },
106 { &vop_mkdir_desc, (vop_t *) smbfs_mkdir },
107 { &vop_mknod_desc, (vop_t *) smbfs_mknod },
108 { &vop_open_desc, (vop_t *) smbfs_open },
109 { &vop_pathconf_desc, (vop_t *) smbfs_pathconf },
110 { &vop_print_desc, (vop_t *) smbfs_print },
111 { &vop_putpages_desc, (vop_t *) smbfs_putpages },
112 { &vop_read_desc, (vop_t *) smbfs_read },
113 { &vop_readdir_desc, (vop_t *) smbfs_readdir },
114 { &vop_reclaim_desc, (vop_t *) smbfs_reclaim },
115 { &vop_remove_desc, (vop_t *) smbfs_remove },
116 { &vop_rename_desc, (vop_t *) smbfs_rename },
117 { &vop_rmdir_desc, (vop_t *) smbfs_rmdir },
118 { &vop_setattr_desc, (vop_t *) smbfs_setattr },
119 { &vop_strategy_desc, (vop_t *) smbfs_strategy },
120 { &vop_symlink_desc, (vop_t *) smbfs_symlink },
121 { &vop_unlock_desc, (vop_t *) vop_stdunlock },
122 { &vop_write_desc, (vop_t *) smbfs_write },
123 { &vop_getextattr_desc, (vop_t *) smbfs_getextattr },
124 /* { &vop_setextattr_desc, (vop_t *) smbfs_setextattr },*/
128 static struct vnodeopv_desc smbfs_vnodeop_opv_desc =
129 { &smbfs_vnodeop_p, smbfs_vnodeop_entries };
131 VNODEOP_SET(smbfs_vnodeop_opv_desc);
135 struct vop_access_args /* {
138 struct ucred *a_cred;
142 struct vnode *vp = ap->a_vp;
143 struct ucred *cred = ap->a_cred;
144 u_int mode = ap->a_mode;
145 struct smbmount *smp = VTOSMBFS(vp);
149 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
150 switch (vp->v_type) {
151 case VREG: case VDIR: case VLNK:
157 if (cred->cr_uid == 0)
159 if (cred->cr_uid != smp->sm_args.uid) {
161 if (!groupmember(smp->sm_args.gid, cred))
164 error = (((vp->v_type == VREG) ? smp->sm_args.file_mode : smp->sm_args.dir_mode) & mode) == mode ? 0 : EACCES;
171 struct vop_open_args /* {
174 struct ucred *a_cred;
178 struct vnode *vp = ap->a_vp;
179 struct smbnode *np = VTOSMB(vp);
180 struct smb_cred scred;
182 int mode = ap->a_mode;
185 SMBVDEBUG("%s,%d\n", np->n_name, np->n_opencount);
186 if (vp->v_type != VREG && vp->v_type != VDIR) {
187 SMBFSERR("open eacces vtype=%d\n", vp->v_type);
190 if (vp->v_type == VDIR) {
194 if (np->n_flag & NMODIFIED) {
195 if ((error = smbfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_td, 1)) == EINTR)
197 smbfs_attr_cacheremove(vp);
198 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
201 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
203 error = VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);
206 if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
207 error = smbfs_vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_td, 1);
210 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
213 if (np->n_opencount) {
217 accmode = SMB_AM_OPENREAD;
218 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
219 accmode = SMB_AM_OPENRW;
220 smb_makescred(&scred, ap->a_td, ap->a_cred);
221 error = smbfs_smb_open(np, accmode, &scred);
225 accmode = SMB_AM_OPENREAD;
226 error = smbfs_smb_open(np, accmode, &scred);
231 smbfs_attr_cacheremove(vp);
236 smbfs_closel(struct vop_close_args *ap)
238 struct vnode *vp = ap->a_vp;
239 struct smbnode *np = VTOSMB(vp);
240 struct thread *td = ap->a_td;
241 struct smb_cred scred;
245 SMBVDEBUG("name=%s, pid=%d, c=%d\n",np->n_name, p->p_pid, np->n_opencount);
247 smb_makescred(&scred, td, ap->a_cred);
249 if (np->n_opencount == 0) {
250 if (vp->v_type != VDIR)
251 SMBERROR("Negative opencount\n");
255 if (vp->v_type == VDIR) {
259 smbfs_findclose(np->n_dirseq, &scred);
264 error = smbfs_vinvalbuf(vp, V_SAVE, ap->a_cred, td, 1);
267 VOP_GETATTR(vp, &vattr, ap->a_cred, td);
268 error = smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
269 &np->n_mtime, &scred);
271 smbfs_attr_cacheremove(vp);
276 * XXX: VOP_CLOSE() usually called without lock held which is suck. Here we
277 * do some heruistic to determine if vnode should be locked.
281 struct vop_close_args /* {
282 struct vnodeop_desc *a_desc;
285 struct ucred *a_cred;
289 struct vnode *vp = ap->a_vp;
290 struct thread *td = ap->a_td;
294 dolock = (vp->v_flag & VXLOCK) == 0;
296 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK, td);
299 error = smbfs_closel(ap);
301 VOP_UNLOCK(vp, 0, td);
306 * smbfs_getattr call from vfs.
310 struct vop_getattr_args /* {
313 struct ucred *a_cred;
317 struct vnode *vp = ap->a_vp;
318 struct smbnode *np = VTOSMB(vp);
319 struct vattr *va=ap->a_vap;
320 struct smbfattr fattr;
321 struct smb_cred scred;
325 SMBVDEBUG("%lx: '%s' %d\n", (long)vp, np->n_name, (vp->v_flag & VROOT) != 0);
326 error = smbfs_attr_cachelookup(vp, va);
329 SMBVDEBUG("not in the cache\n");
330 smb_makescred(&scred, ap->a_td, ap->a_cred);
331 oldsize = np->n_size;
332 error = smbfs_smb_lookup(np, NULL, 0, &fattr, &scred);
334 SMBVDEBUG("error %d\n", error);
337 smbfs_attr_cacheenter(vp, &fattr);
338 smbfs_attr_cachelookup(vp, va);
340 np->n_size = oldsize;
346 struct vop_setattr_args /* {
349 struct ucred *a_cred;
353 struct vnode *vp = ap->a_vp;
354 struct smbnode *np = VTOSMB(vp);
355 struct vattr *vap = ap->a_vap;
356 struct timespec *mtime, *atime;
357 struct smb_cred scred;
358 struct smb_share *ssp = np->n_mount->sm_share;
359 struct smb_vc *vcp = SSTOVC(ssp);
361 int isreadonly, doclose, error = 0;
364 if (vap->va_flags != VNOVAL)
366 isreadonly = (vp->v_mount->mnt_flag & MNT_RDONLY);
368 * Disallow write attempts if the filesystem is mounted read-only.
370 if ((vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL ||
371 vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
372 vap->va_mode != (mode_t)VNOVAL) && isreadonly)
374 smb_makescred(&scred, ap->a_td, ap->a_cred);
375 if (vap->va_size != VNOVAL) {
376 switch (vp->v_type) {
387 vnode_pager_setsize(vp, (u_long)vap->va_size);
389 np->n_size = vap->va_size;
390 if (np->n_opencount == 0) {
391 error = smbfs_smb_open(np, SMB_AM_OPENRW, &scred);
396 error = smbfs_smb_setfsize(np, vap->va_size, &scred);
398 smbfs_smb_close(ssp, np->n_fid, NULL, &scred);
401 vnode_pager_setsize(vp, (u_long)tsize);
405 mtime = atime = NULL;
406 if (vap->va_mtime.tv_sec != VNOVAL)
407 mtime = &vap->va_mtime;
408 if (vap->va_atime.tv_sec != VNOVAL)
409 atime = &vap->va_atime;
410 if (mtime != atime) {
411 if (ap->a_cred->cr_uid != VTOSMBFS(vp)->sm_args.uid &&
412 (error = suser_cred(ap->a_cred, PRISON_ROOT)) &&
413 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
414 (error = VOP_ACCESS(vp, VWRITE, ap->a_cred, ap->a_td))))
418 mtime = &np->n_mtime;
420 atime = &np->n_atime;
423 * If file is opened, then we can use handle based calls.
424 * If not, use path based ones.
426 if (np->n_opencount == 0) {
427 if (vcp->vc_flags & SMBV_WIN95) {
428 error = VOP_OPEN(vp, FWRITE, ap->a_cred, ap->a_td);
430 /* error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
431 VOP_GETATTR(vp, &vattr, ap->a_cred, ap->a_td);*/
433 np->n_mtime = *mtime;
434 VOP_CLOSE(vp, FWRITE, ap->a_cred, ap->a_td);
436 } else if ((vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS)) {
437 error = smbfs_smb_setptime2(np, mtime, atime, 0, &scred);
438 /* error = smbfs_smb_setpattrNT(np, 0, mtime, atime, &scred);*/
439 } else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN2_0) {
440 error = smbfs_smb_setptime2(np, mtime, atime, 0, &scred);
442 error = smbfs_smb_setpattr(np, 0, mtime, &scred);
445 if (vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS) {
446 error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
447 } else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN1_0) {
448 error = smbfs_smb_setftime(np, mtime, atime, &scred);
451 * I have no idea how to handle this for core
452 * level servers. The possible solution is to
453 * update mtime after file is closed.
455 SMBERROR("can't update times on an opened file\n");
460 * Invalidate attribute cache in case if server doesn't set
461 * required attributes.
463 smbfs_attr_cacheremove(vp); /* invalidate cache */
464 VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td);
465 np->n_mtime.tv_sec = vap->va_mtime.tv_sec;
473 struct vop_read_args /* {
477 struct ucred *a_cred;
480 struct vnode *vp = ap->a_vp;
481 struct uio *uio = ap->a_uio;
484 if (vp->v_type != VREG && vp->v_type != VDIR)
486 return smbfs_readvnode(vp, uio, ap->a_cred);
491 struct vop_write_args /* {
495 struct ucred *a_cred;
498 struct vnode *vp = ap->a_vp;
499 struct uio *uio = ap->a_uio;
501 SMBVDEBUG("%d,ofs=%d,sz=%d\n",vp->v_type, (int)uio->uio_offset, uio->uio_resid);
502 if (vp->v_type != VREG)
504 return smbfs_writevnode(vp, uio, ap->a_cred,ap->a_ioflag);
508 * Create a regular file. On entry the directory to contain the file being
509 * created is locked. We must release before we return. We must also free
510 * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
511 * only if the SAVESTART bit in cn_flags is clear on success.
515 struct vop_create_args /* {
517 struct vnode **a_vpp;
518 struct componentname *a_cnp;
522 struct vnode *dvp = ap->a_dvp;
523 struct vattr *vap = ap->a_vap;
524 struct vnode **vpp=ap->a_vpp;
525 struct componentname *cnp = ap->a_cnp;
526 struct smbnode *dnp = VTOSMB(dvp);
529 struct smbfattr fattr;
530 struct smb_cred scred;
531 char *name = cnp->cn_nameptr;
532 int nmlen = cnp->cn_namelen;
538 if (vap->va_type != VREG)
540 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_td)))
542 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
544 error = smbfs_smb_create(dnp, name, nmlen, &scred);
547 error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, &scred);
550 error = smbfs_nget(VTOVFS(dvp), dvp, name, nmlen, &fattr, &vp);
554 if (cnp->cn_flags & MAKEENTRY)
555 cache_enter(dvp, vp, cnp);
561 struct vop_remove_args /* {
562 struct vnodeop_desc *a_desc;
563 struct vnode * a_dvp;
565 struct componentname * a_cnp;
568 struct vnode *vp = ap->a_vp;
569 /* struct vnode *dvp = ap->a_dvp;*/
570 struct componentname *cnp = ap->a_cnp;
571 struct smbnode *np = VTOSMB(vp);
572 struct smb_cred scred;
575 if (vp->v_type == VDIR || np->n_opencount || vp->v_usecount != 1)
577 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
578 error = smbfs_smb_delete(np, &scred);
584 * smbfs_file rename call
588 struct vop_rename_args /* {
589 struct vnode *a_fdvp;
591 struct componentname *a_fcnp;
592 struct vnode *a_tdvp;
594 struct componentname *a_tcnp;
597 struct vnode *fvp = ap->a_fvp;
598 struct vnode *tvp = ap->a_tvp;
599 struct vnode *fdvp = ap->a_fdvp;
600 struct vnode *tdvp = ap->a_tdvp;
601 struct componentname *tcnp = ap->a_tcnp;
602 /* struct componentname *fcnp = ap->a_fcnp;*/
603 struct smb_cred scred;
607 /* Check for cross-device rename */
608 if ((fvp->v_mount != tdvp->v_mount) ||
609 (tvp && (fvp->v_mount != tvp->v_mount))) {
614 if (tvp && tvp->v_usecount > 1) {
618 flags = 0x10; /* verify all writes */
619 if (fvp->v_type == VDIR) {
621 } else if (fvp->v_type == VREG) {
627 smb_makescred(&scred, tcnp->cn_td, tcnp->cn_cred);
629 * It seems that Samba doesn't implement SMB_COM_MOVE call...
632 if (SMB_DIALECT(SSTOCN(smp->sm_share)) >= SMB_DIALECT_LANMAN1_0) {
633 error = smbfs_smb_move(VTOSMB(fvp), VTOSMB(tdvp),
634 tcnp->cn_nameptr, tcnp->cn_namelen, flags, &scred);
639 * We have to do the work atomicaly
641 if (tvp && tvp != fvp) {
642 error = smbfs_smb_delete(VTOSMB(tvp), &scred);
646 error = smbfs_smb_rename(VTOSMB(fvp), VTOSMB(tdvp),
647 tcnp->cn_nameptr, tcnp->cn_namelen, &scred);
650 if (fvp->v_type == VDIR) {
651 if (tvp != NULL && tvp->v_type == VDIR)
657 smbfs_attr_cacheremove(fdvp);
658 smbfs_attr_cacheremove(tdvp);
668 #ifdef possible_mistake
677 * somtime it will come true...
681 struct vop_link_args /* {
682 struct vnode *a_tdvp;
684 struct componentname *a_cnp;
691 * smbfs_symlink link create call.
692 * Sometime it will be functional...
696 struct vop_symlink_args /* {
698 struct vnode **a_vpp;
699 struct componentname *a_cnp;
709 struct vop_mknod_args /* {
717 struct vop_mkdir_args /* {
719 struct vnode **a_vpp;
720 struct componentname *a_cnp;
724 struct vnode *dvp = ap->a_dvp;
725 /* struct vattr *vap = ap->a_vap;*/
727 struct componentname *cnp = ap->a_cnp;
728 struct smbnode *dnp = VTOSMB(dvp);
730 struct smb_cred scred;
731 struct smbfattr fattr;
732 char *name = cnp->cn_nameptr;
733 int len = cnp->cn_namelen;
736 if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred, cnp->cn_td))) {
739 if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.'))))
741 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
742 error = smbfs_smb_mkdir(dnp, name, len, &scred);
745 error = smbfs_smb_lookup(dnp, name, len, &fattr, &scred);
748 error = smbfs_nget(VTOVFS(dvp), dvp, name, len, &fattr, &vp);
756 * smbfs_remove directory call
760 struct vop_rmdir_args /* {
763 struct componentname *a_cnp;
766 struct vnode *vp = ap->a_vp;
767 struct vnode *dvp = ap->a_dvp;
768 struct componentname *cnp = ap->a_cnp;
769 /* struct smbmount *smp = VTOSMBFS(vp);*/
770 struct smbnode *dnp = VTOSMB(dvp);
771 struct smbnode *np = VTOSMB(vp);
772 struct smb_cred scred;
778 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
779 error = smbfs_smb_rmdir(np, &scred);
780 dnp->n_flag |= NMODIFIED;
781 smbfs_attr_cacheremove(dvp);
782 /* cache_purge(dvp);*/
792 struct vop_readdir_args /* {
795 struct ucred *a_cred;
801 struct vnode *vp = ap->a_vp;
802 struct uio *uio = ap->a_uio;
805 if (vp->v_type != VDIR)
808 if (ap->a_ncookies) {
809 printf("smbfs_readdir: no support for cookies now...");
813 error = smbfs_readvnode(vp, uio, ap->a_cred);
820 struct vop_fsync_args /* {
821 struct vnodeop_desc *a_desc;
823 struct ucred *a_cred;
828 /* return (smb_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));*/
834 struct vop_print_args /* {
838 struct vnode *vp = ap->a_vp;
839 struct smbnode *np = VTOSMB(vp);
842 printf("no smbnode data\n");
845 printf("tag VT_SMBFS, name = %s, parent = %p, opencount = %d",
846 np->n_name, np->n_parent ? np->n_parent : NULL,
848 lockmgr_printinfo(&np->n_lock);
855 struct vop_pathconf_args /* {
861 struct smbmount *smp = VFSTOSMBFS(VTOVFS(ap->a_vp));
862 struct smb_vc *vcp = SSTOVC(smp->sm_share);
863 register_t *retval = ap->a_retval;
866 switch (ap->a_name) {
871 *retval = (vcp->vc_hflags2 & SMB_FLAGS2_KNOWS_LONG_NAMES) ? 255 : 12;
874 *retval = 800; /* XXX: a correct one ? */
884 struct vop_strategy_args /* {
888 struct buf *bp=ap->a_bp;
890 struct thread *td = NULL;
894 if (bp->b_flags & B_PHYS)
895 panic("smbfs physio");
896 if ((bp->b_flags & B_ASYNC) == 0)
897 td = curthread; /* XXX */
898 if (bp->b_flags & B_READ)
903 if ((bp->b_flags & B_ASYNC) == 0 )
904 error = smbfs_doio(bp, cr, td);
910 struct vop_bmap_args /* {
913 struct vnode **a_vpp;
919 struct vnode *vp = ap->a_vp;
921 if (ap->a_vpp != NULL)
923 if (ap->a_bnp != NULL)
924 *ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
925 if (ap->a_runp != NULL)
927 if (ap->a_runb != NULL)
934 struct vop_ioctl_args /* {
946 static char smbfs_atl[] = "rhsvda";
948 smbfs_getextattr(struct vop_getextattr_args *ap)
950 IN struct vnode *a_vp;
952 INOUT struct uio *a_uio;
953 IN struct ucred *a_cred;
954 IN struct thread *a_td;
958 struct vnode *vp = ap->a_vp;
959 struct thread *td = ap->a_td;
960 struct ucred *cred = ap->a_cred;
961 struct uio *uio = ap->a_uio;
962 const char *name = ap->a_name;
963 struct smbnode *np = VTOSMB(vp);
968 error = VOP_ACCESS(vp, VREAD, cred, td);
971 error = VOP_GETATTR(vp, &vattr, cred, td);
974 if (strcmp(name, "dosattr") == 0) {
975 attr = np->n_dosattr;
976 for (i = 0; i < 6; i++, attr >>= 1)
977 buf[i] = (attr & 1) ? smbfs_atl[i] : '-';
979 error = uiomove(buf, i, uio);
987 * Since we expected to support F_GETLK (and SMB protocol has no such function),
988 * it is necessary to use lf_advlock(). It would be nice if this function had
989 * a callback mechanism because it will help to improve a level of consistency.
993 struct vop_advlock_args /* {
1001 struct vnode *vp = ap->a_vp;
1002 struct smbnode *np = VTOSMB(vp);
1003 struct flock *fl = ap->a_fl;
1004 caddr_t id = (caddr_t)1 /* ap->a_id */;
1005 /* int flags = ap->a_flags;*/
1006 struct thread *td = curthread; /* XXX */
1007 struct smb_cred scred;
1008 off_t start, end, size;
1011 if (vp->v_type == VDIR) {
1013 * SMB protocol have no support for directory locking.
1014 * Although locks can be processed on local machine, I don't
1015 * think that this is a good idea, because some programs
1016 * can work wrong assuming directory is locked. So, we just
1017 * return 'operation not supported
1022 switch (fl->l_whence) {
1025 start = fl->l_start;
1028 start = fl->l_start + size;
1037 end = start + fl->l_len - 1;
1041 smb_makescred(&scred, td, td->td_proc ? td->td_proc->p_ucred : NULL);
1044 switch (fl->l_type) {
1046 lkop = SMB_LOCK_EXCL;
1049 lkop = SMB_LOCK_SHARED;
1052 lkop = SMB_LOCK_RELEASE;
1057 error = lf_advlock(ap, &np->n_lockf, size);
1060 lkop = SMB_LOCK_EXCL;
1061 error = smbfs_smb_lock(np, lkop, id, start, end, &scred);
1064 lf_advlock(ap, &np->n_lockf, size);
1068 lf_advlock(ap, &np->n_lockf, size);
1069 error = smbfs_smb_lock(np, SMB_LOCK_RELEASE, id, start, end, &scred);
1072 error = lf_advlock(ap, &np->n_lockf, size);
1081 smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop)
1083 static const char *badchars = "*/\[]:<>=;?";
1084 static const char *badchars83 = " +|,";
1088 if (nameiop == LOOKUP)
1091 if (SMB_DIALECT(SSTOVC(smp->sm_share)) < SMB_DIALECT_LANMAN2_0) {
1093 * Name should conform 8.3 format
1096 return ENAMETOOLONG;
1097 cp = index(name, '.');
1100 if (cp == name || (cp - name) > 8)
1102 cp = index(cp + 1, '.');
1105 for (cp = name, i = 0; i < nmlen; i++, cp++)
1106 if (index(badchars83, *cp) != NULL)
1109 for (cp = name, i = 0; i < nmlen; i++, cp++)
1110 if (index(badchars, *cp) != NULL)
1116 * Things go even weird without fixed inode numbers...
1120 struct vop_lookup_args /* {
1121 struct vnodeop_desc *a_desc;
1122 struct vnode *a_dvp;
1123 struct vnode **a_vpp;
1124 struct componentname *a_cnp;
1127 struct componentname *cnp = ap->a_cnp;
1128 struct thread *td = cnp->cn_td;
1129 struct vnode *dvp = ap->a_dvp;
1130 struct vnode **vpp = ap->a_vpp;
1132 struct smbmount *smp;
1133 struct mount *mp = dvp->v_mount;
1134 struct smbnode *dnp;
1135 struct smbfattr fattr, *fap;
1136 struct smb_cred scred;
1137 char *name = cnp->cn_nameptr;
1138 int flags = cnp->cn_flags;
1139 int nameiop = cnp->cn_nameiop;
1140 int nmlen = cnp->cn_namelen;
1141 int lockparent, wantparent, error, islastcn, isdot;
1144 cnp->cn_flags &= ~PDIRUNLOCK;
1145 if (dvp->v_type != VDIR)
1147 if ((flags & ISDOTDOT) && (dvp->v_flag & VROOT)) {
1148 SMBFSERR("invalid '..'\n");
1151 #ifdef SMB_VNODE_DEBUG
1158 SMBVDEBUG("%d '%s' in '%s' id=d\n", nameiop, name,
1159 VTOSMB(dvp)->n_name);
1163 islastcn = flags & ISLASTCN;
1164 if (islastcn && (mp->mnt_flag & MNT_RDONLY) && (nameiop != LOOKUP))
1166 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1168 lockparent = flags & LOCKPARENT;
1169 wantparent = flags & (LOCKPARENT|WANTPARENT);
1170 smp = VFSTOSMBFS(mp);
1172 isdot = (nmlen == 1 && name[0] == '.');
1174 error = smbfs_pathcheck(smp, cnp->cn_nameptr, cnp->cn_namelen, nameiop);
1179 error = cache_lookup(dvp, vpp, cnp);
1180 SMBVDEBUG("cache_lookup returned %d\n", error);
1183 if (error) { /* name was found */
1189 if (dvp == vp) { /* lookup on current */
1192 SMBVDEBUG("cached '.'\n");
1193 } else if (flags & ISDOTDOT) {
1194 VOP_UNLOCK(dvp, 0, td); /* unlock parent */
1195 cnp->cn_flags |= PDIRUNLOCK;
1196 error = vget(vp, LK_EXCLUSIVE, td);
1197 if (!error && lockparent && islastcn) {
1198 error = vn_lock(dvp, LK_EXCLUSIVE, td);
1200 cnp->cn_flags &= ~PDIRUNLOCK;
1203 error = vget(vp, LK_EXCLUSIVE, td);
1204 if (!lockparent || error || !islastcn) {
1205 VOP_UNLOCK(dvp, 0, td);
1206 cnp->cn_flags |= PDIRUNLOCK;
1210 if (vpid == vp->v_id) {
1211 if (!VOP_GETATTR(vp, &vattr, cnp->cn_cred, td)
1212 /* && vattr.va_ctime.tv_sec == VTOSMB(vp)->n_ctime*/) {
1213 if (nameiop != LOOKUP && islastcn)
1214 cnp->cn_flags |= SAVENAME;
1215 SMBVDEBUG("use cached vnode\n");
1221 if (lockparent && dvp != vp && islastcn)
1222 VOP_UNLOCK(dvp, 0, td);
1224 error = vn_lock(dvp, LK_EXCLUSIVE, td);
1227 cnp->cn_flags |= PDIRUNLOCK;
1230 cnp->cn_flags &= ~PDIRUNLOCK;
1233 * entry is not in the cache or has been expired
1237 smb_makescred(&scred, td, cnp->cn_cred);
1239 if (flags & ISDOTDOT) {
1240 error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0, fap,
1242 SMBVDEBUG("result of dotdot lookup: %d\n", error);
1245 error = smbfs_smb_lookup(dnp, name, nmlen, fap, &scred);
1246 /* if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')*/
1247 SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1249 if (error && error != ENOENT)
1251 if (error) { /* entry not found */
1253 * Handle RENAME or CREATE case...
1255 if ((nameiop == CREATE || nameiop == RENAME) && wantparent && islastcn) {
1256 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1259 cnp->cn_flags |= SAVENAME;
1261 VOP_UNLOCK(dvp, 0, td);
1262 cnp->cn_flags |= PDIRUNLOCK;
1264 return (EJUSTRETURN);
1268 SMBVDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
1271 * handle DELETE case ...
1273 if (nameiop == DELETE && islastcn) { /* delete last component */
1274 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1282 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1286 cnp->cn_flags |= SAVENAME;
1288 VOP_UNLOCK(dvp, 0, td);
1289 cnp->cn_flags |= PDIRUNLOCK;
1293 if (nameiop == RENAME && islastcn && wantparent) {
1294 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1299 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1303 cnp->cn_flags |= SAVENAME;
1305 VOP_UNLOCK(dvp, 0, td);
1306 cnp->cn_flags |= PDIRUNLOCK;
1310 if (flags & ISDOTDOT) {
1311 VOP_UNLOCK(dvp, 0, td);
1312 error = smbfs_nget(mp, dvp, name, nmlen, NULL, &vp);
1314 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1317 if (lockparent && islastcn) {
1318 error = vn_lock(dvp, LK_EXCLUSIVE, td);
1320 cnp->cn_flags |= PDIRUNLOCK;
1330 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1334 SMBVDEBUG("lookup: getnewvp!\n");
1335 if (!lockparent || !islastcn) {
1336 VOP_UNLOCK(dvp, 0, td);
1337 cnp->cn_flags |= PDIRUNLOCK;
1340 if ((cnp->cn_flags & MAKEENTRY)/* && !islastcn*/) {
1341 /* VTOSMB(*vpp)->n_ctime = VTOSMB(*vpp)->n_vattr.va_ctime.tv_sec;*/
1342 cache_enter(dvp, *vpp, cnp);