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 $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #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 <netproto/smb/smb.h>
52 #include <netproto/smb/smb_conn.h>
53 #include <netproto/smb/smb_subr.h>
56 #include "smbfs_node.h"
57 #include "smbfs_subr.h"
62 * Prototypes for SMBFS vnode operations
64 static int smbfs_create(struct vop_old_create_args *);
65 static int smbfs_mknod(struct vop_old_mknod_args *);
66 static int smbfs_open(struct vop_open_args *);
67 static int smbfs_closel(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_old_remove_args *);
75 static int smbfs_link(struct vop_old_link_args *);
76 static int smbfs_lookup(struct vop_old_lookup_args *);
77 static int smbfs_rename(struct vop_old_rename_args *);
78 static int smbfs_mkdir(struct vop_old_mkdir_args *);
79 static int smbfs_rmdir(struct vop_old_rmdir_args *);
80 static int smbfs_symlink(struct vop_old_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 struct vop_ops smbfs_vnode_vops = {
90 .vop_default = vop_defaultop,
91 .vop_access = smbfs_access,
92 .vop_advlock = smbfs_advlock,
93 .vop_bmap = smbfs_bmap,
94 .vop_close = smbfs_closel,
95 .vop_old_create = smbfs_create,
96 .vop_fsync = smbfs_fsync,
97 .vop_getattr = smbfs_getattr,
98 .vop_getpages = smbfs_getpages,
99 .vop_inactive = smbfs_inactive,
100 .vop_ioctl = smbfs_ioctl,
101 .vop_old_link = smbfs_link,
102 .vop_old_lookup = smbfs_lookup,
103 .vop_old_mkdir = smbfs_mkdir,
104 .vop_old_mknod = smbfs_mknod,
105 .vop_open = smbfs_open,
106 .vop_pathconf = smbfs_pathconf,
107 .vop_print = smbfs_print,
108 .vop_putpages = smbfs_putpages,
109 .vop_read = smbfs_read,
110 .vop_readdir = smbfs_readdir,
111 .vop_reclaim = smbfs_reclaim,
112 .vop_old_remove = smbfs_remove,
113 .vop_old_rename = smbfs_rename,
114 .vop_old_rmdir = smbfs_rmdir,
115 .vop_setattr = smbfs_setattr,
116 .vop_strategy = smbfs_strategy,
117 .vop_old_symlink = smbfs_symlink,
118 .vop_write = smbfs_write,
119 .vop_getextattr = smbfs_getextattr
120 /* .vop_setextattr = smbfs_setextattr */
124 * smbfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
127 smbfs_access(struct vop_access_args *ap)
129 struct vnode *vp = ap->a_vp;
130 struct smbmount *smp = VTOSMBFS(vp);
135 mode = ((vp->v_type == VREG) ?
136 smp->sm_args.file_mode : smp->sm_args.dir_mode);
137 error = vop_helper_access(ap, smp->sm_args.uid, smp->sm_args.gid,
143 * smbfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
148 smbfs_open(struct vop_open_args *ap)
150 struct vnode *vp = ap->a_vp;
151 struct smbnode *np = VTOSMB(vp);
152 struct smb_cred scred;
154 int mode = ap->a_mode;
157 SMBVDEBUG("%s,%d\n", np->n_name, np->n_opencount);
158 if (vp->v_type != VREG && vp->v_type != VDIR) {
159 SMBFSERR("open eacces vtype=%d\n", vp->v_type);
162 if (vp->v_type == VDIR) {
163 if (np->n_opencount == 0)
164 np->n_cached_cred = crhold(ap->a_cred);
166 return (vop_stdopen(ap));
168 if (np->n_flag & NMODIFIED) {
169 if ((error = smbfs_vinvalbuf(vp, V_SAVE, 1)) == EINTR)
171 smbfs_attr_cacheremove(vp);
172 error = VOP_GETATTR(vp, &vattr);
175 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
177 error = VOP_GETATTR(vp, &vattr);
180 if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
181 error = smbfs_vinvalbuf(vp, V_SAVE, 1);
184 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
187 if (np->n_opencount) {
189 return (vop_stdopen(ap));
191 accmode = SMB_AM_OPENREAD;
192 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
193 accmode = SMB_AM_OPENRW;
194 smb_makescred(&scred, curthread, ap->a_cred);
195 error = smbfs_smb_open(np, accmode, &scred);
199 accmode = SMB_AM_OPENREAD;
200 error = smbfs_smb_open(np, accmode, &scred);
203 np->n_cached_cred = crhold(ap->a_cred);
206 smbfs_attr_cacheremove(vp);
213 smbfs_closel(struct vop_close_args *ap)
215 struct vnode *vp = ap->a_vp;
216 struct smbnode *np = VTOSMB(vp);
217 struct smb_cred scred;
221 SMBVDEBUG("name=%s, pid=%d, c=%d\n",
222 np->n_name, p->p_pid, np->n_opencount);
223 vn_lock(vp, LK_UPGRADE | LK_RETRY);
225 smb_makescred(&scred, curthread, proc0.p_ucred);
228 if (np->n_opencount == 0) {
229 if (vp->v_type != VDIR)
230 SMBERROR("Negative opencount\n");
234 if (vp->v_type == VDIR) {
238 smbfs_findclose(np->n_dirseq, &scred);
242 error = smbfs_vinvalbuf(vp, V_SAVE, 1);
245 VOP_GETATTR(vp, &vattr);
246 error = smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
247 &np->n_mtime, &scred);
249 crfree(np->n_cached_cred);
250 np->n_cached_cred = NULL;
251 smbfs_attr_cacheremove(vp);
258 * smbfs_getattr call from vfs.
260 * smbfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
263 smbfs_getattr(struct vop_getattr_args *ap)
265 struct vnode *vp = ap->a_vp;
266 struct smbnode *np = VTOSMB(vp);
267 struct vattr *va=ap->a_vap;
268 struct smbfattr fattr;
269 struct smb_cred scred;
273 SMBVDEBUG("%lx: '%s' %d\n", (long)vp, np->n_name, (vp->v_flag & VROOT) != 0);
274 error = smbfs_attr_cachelookup(vp, va);
277 SMBVDEBUG("not in the cache\n");
278 smb_makescred(&scred, curthread, proc0.p_ucred);
279 oldsize = np->n_size;
280 error = smbfs_smb_lookup(np, NULL, 0, &fattr, &scred);
282 SMBVDEBUG("error %d\n", error);
285 smbfs_attr_cacheenter(vp, &fattr);
286 smbfs_attr_cachelookup(vp, va);
288 np->n_size = oldsize;
293 * smbfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
296 smbfs_setattr(struct vop_setattr_args *ap)
298 struct vnode *vp = ap->a_vp;
299 struct smbnode *np = VTOSMB(vp);
300 struct vattr *vap = ap->a_vap;
301 struct timespec *mtime, *atime;
302 struct smb_cred scred;
303 struct smb_share *ssp = np->n_mount->sm_share;
304 struct smb_vc *vcp = SSTOVC(ssp);
306 int isreadonly, doclose, error = 0;
309 if (vap->va_flags != VNOVAL)
311 isreadonly = (vp->v_mount->mnt_flag & MNT_RDONLY);
313 * Disallow write attempts if the filesystem is mounted read-only.
315 if ((vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL ||
316 vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
317 vap->va_mode != (mode_t)VNOVAL) && isreadonly)
319 smb_makescred(&scred, curthread, ap->a_cred);
320 if (vap->va_size != VNOVAL) {
321 switch (vp->v_type) {
332 vnode_pager_setsize(vp, (u_long)vap->va_size);
334 np->n_size = vap->va_size;
335 if (np->n_opencount == 0) {
336 error = smbfs_smb_open(np, SMB_AM_OPENRW, &scred);
341 error = smbfs_smb_setfsize(np, vap->va_size, &scred);
343 smbfs_smb_close(ssp, np->n_fid, NULL, &scred);
346 vnode_pager_setsize(vp, (u_long)tsize);
350 mtime = atime = NULL;
351 if (vap->va_mtime.tv_sec != VNOVAL)
352 mtime = &vap->va_mtime;
353 if (vap->va_atime.tv_sec != VNOVAL)
354 atime = &vap->va_atime;
355 if (mtime != atime) {
356 if (ap->a_cred->cr_uid != VTOSMBFS(vp)->sm_args.uid &&
357 (error = priv_check_cred(ap->a_cred, PRIV_VFS_SETATTR, 0)) &&
358 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
359 (error = VOP_EACCESS(vp, VWRITE, ap->a_cred))))
363 mtime = &np->n_mtime;
365 atime = &np->n_atime;
368 * If file is opened, then we can use handle based calls.
369 * If not, use path based ones.
371 if (np->n_opencount == 0) {
372 if (vcp->vc_flags & SMBV_WIN95) {
373 error = VOP_OPEN(vp, FWRITE, ap->a_cred, NULL);
375 /* error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
376 VOP_GETATTR(vp, &vattr);*/
378 np->n_mtime = *mtime;
379 VOP_CLOSE(vp, FWRITE, NULL);
381 } else if ((vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS)) {
382 error = smbfs_smb_setptime2(np, mtime, atime, 0, &scred);
383 /* error = smbfs_smb_setpattrNT(np, 0, mtime, atime, &scred);*/
384 } else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN2_0) {
385 error = smbfs_smb_setptime2(np, mtime, atime, 0, &scred);
387 error = smbfs_smb_setpattr(np, 0, mtime, &scred);
390 if (vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS) {
391 error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
392 } else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN1_0) {
393 error = smbfs_smb_setftime(np, mtime, atime, &scred);
396 * I have no idea how to handle this for core
397 * level servers. The possible solution is to
398 * update mtime after file is closed.
400 SMBERROR("can't update times on an opened file\n");
405 * Invalidate attribute cache in case if server doesn't set
406 * required attributes.
408 smbfs_attr_cacheremove(vp); /* invalidate cache */
409 VOP_GETATTR(vp, vap);
410 np->n_mtime.tv_sec = vap->va_mtime.tv_sec;
416 * smbfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
417 * struct ucred *a_cred)
420 smbfs_read(struct vop_read_args *ap)
422 struct vnode *vp = ap->a_vp;
423 struct uio *uio = ap->a_uio;
426 if (vp->v_type != VREG && vp->v_type != VDIR)
428 return smbfs_readvnode(vp, uio, ap->a_cred);
432 * smbfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
433 * struct ucred *a_cred)
436 smbfs_write(struct vop_write_args *ap)
438 struct vnode *vp = ap->a_vp;
439 struct uio *uio = ap->a_uio;
441 SMBVDEBUG("%d,ofs=%d,sz=%d\n",vp->v_type, (int)uio->uio_offset, uio->uio_resid);
442 if (vp->v_type != VREG)
444 return smbfs_writevnode(vp, uio, ap->a_cred,ap->a_ioflag);
448 * Create a regular file. On entry the directory to contain the file being
449 * created is locked. We must release before we return.
451 * smbfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
452 * struct componentname *a_cnp, struct vattr *a_vap)
455 smbfs_create(struct vop_old_create_args *ap)
457 struct vnode *dvp = ap->a_dvp;
458 struct vattr *vap = ap->a_vap;
459 struct vnode **vpp=ap->a_vpp;
460 struct componentname *cnp = ap->a_cnp;
461 struct smbnode *dnp = VTOSMB(dvp);
464 struct smbfattr fattr;
465 struct smb_cred scred;
466 char *name = cnp->cn_nameptr;
467 int nmlen = cnp->cn_namelen;
473 if (vap->va_type != VREG)
475 if ((error = VOP_GETATTR(dvp, &vattr)))
477 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
479 error = smbfs_smb_create(dnp, name, nmlen, &scred);
482 error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, &scred);
485 error = smbfs_nget(VTOVFS(dvp), dvp, name, nmlen, &fattr, &vp);
493 * smbfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
494 * struct componentname *a_cnp)
497 smbfs_remove(struct vop_old_remove_args *ap)
499 struct vnode *vp = ap->a_vp;
500 /* struct vnode *dvp = ap->a_dvp;*/
501 struct componentname *cnp = ap->a_cnp;
502 struct smbnode *np = VTOSMB(vp);
503 struct smb_cred scred;
506 if (vp->v_type == VDIR || np->n_opencount || VREFCNT(vp) > 1)
508 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
509 error = smbfs_smb_delete(np, &scred);
514 * smbfs_file rename call
516 * smbfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
517 * struct componentname *a_fcnp, struct vnode *a_tdvp,
518 * struct vnode *a_tvp, struct componentname *a_tcnp)
521 smbfs_rename(struct vop_old_rename_args *ap)
523 struct vnode *fvp = ap->a_fvp;
524 struct vnode *tvp = ap->a_tvp;
525 struct vnode *fdvp = ap->a_fdvp;
526 struct vnode *tdvp = ap->a_tdvp;
527 struct componentname *tcnp = ap->a_tcnp;
528 /* struct componentname *fcnp = ap->a_fcnp;*/
529 struct smb_cred scred;
533 /* Check for cross-device rename */
534 if ((fvp->v_mount != tdvp->v_mount) ||
535 (tvp && (fvp->v_mount != tvp->v_mount))) {
540 if (tvp && VREFCNT(tvp) > 1) {
544 flags = 0x10; /* verify all writes */
545 if (fvp->v_type == VDIR) {
547 } else if (fvp->v_type == VREG) {
553 smb_makescred(&scred, tcnp->cn_td, tcnp->cn_cred);
555 * It seems that Samba doesn't implement SMB_COM_MOVE call...
558 if (SMB_DIALECT(SSTOCN(smp->sm_share)) >= SMB_DIALECT_LANMAN1_0) {
559 error = smbfs_smb_move(VTOSMB(fvp), VTOSMB(tdvp),
560 tcnp->cn_nameptr, tcnp->cn_namelen, flags, &scred);
565 * We have to do the work atomicaly
567 if (tvp && tvp != fvp) {
568 error = smbfs_smb_delete(VTOSMB(tvp), &scred);
572 error = smbfs_smb_rename(VTOSMB(fvp), VTOSMB(tdvp),
573 tcnp->cn_nameptr, tcnp->cn_namelen, &scred);
577 smbfs_attr_cacheremove(fdvp);
578 smbfs_attr_cacheremove(tdvp);
588 #ifdef possible_mistake
598 * somtime it will come true...
600 * smbfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
601 * struct componentname *a_cnp)
604 smbfs_link(struct vop_old_link_args *ap)
610 * smbfs_symlink link create call.
611 * Sometime it will be functional...
613 * smbfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
614 * struct componentname *a_cnp, struct vattr *a_vap,
618 smbfs_symlink(struct vop_old_symlink_args *ap)
624 smbfs_mknod(struct vop_old_mknod_args *ap)
630 * smbfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
631 * struct componentname *a_cnp, struct vattr *a_vap)
634 smbfs_mkdir(struct vop_old_mkdir_args *ap)
636 struct vnode *dvp = ap->a_dvp;
637 /* struct vattr *vap = ap->a_vap;*/
639 struct componentname *cnp = ap->a_cnp;
640 struct smbnode *dnp = VTOSMB(dvp);
642 struct smb_cred scred;
643 struct smbfattr fattr;
644 char *name = cnp->cn_nameptr;
645 int len = cnp->cn_namelen;
648 if ((error = VOP_GETATTR(dvp, &vattr))) {
651 if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.'))))
653 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
654 error = smbfs_smb_mkdir(dnp, name, len, &scred);
657 error = smbfs_smb_lookup(dnp, name, len, &fattr, &scred);
660 error = smbfs_nget(VTOVFS(dvp), dvp, name, len, &fattr, &vp);
668 * smbfs_remove directory call
670 * smbfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
671 * struct componentname *a_cnp)
674 smbfs_rmdir(struct vop_old_rmdir_args *ap)
676 struct vnode *vp = ap->a_vp;
677 struct vnode *dvp = ap->a_dvp;
678 struct componentname *cnp = ap->a_cnp;
679 /* struct smbmount *smp = VTOSMBFS(vp);*/
680 struct smbnode *dnp = VTOSMB(dvp);
681 struct smbnode *np = VTOSMB(vp);
682 struct smb_cred scred;
688 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
689 error = smbfs_smb_rmdir(np, &scred);
690 dnp->n_flag |= NMODIFIED;
691 smbfs_attr_cacheremove(dvp);
698 * smbfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
699 * int *a_eofflag, off_t *a_cookies, int a_ncookies)
702 smbfs_readdir(struct vop_readdir_args *ap)
704 struct vnode *vp = ap->a_vp;
705 struct uio *uio = ap->a_uio;
708 if (vp->v_type != VDIR)
711 if (ap->a_ncookies) {
712 kprintf("smbfs_readdir: no support for cookies now...");
716 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
718 error = smbfs_readvnode(vp, uio, ap->a_cred);
725 * smbfs_fsync(struct vnode *a_vp, int a_waitfor)
729 smbfs_fsync(struct vop_fsync_args *ap)
731 /* return (smb_flush(ap->a_vp, ap->a_waitfor, curthread, 1));*/
736 * smbfs_print(struct vnode *a_vp)
739 smbfs_print(struct vop_print_args *ap)
741 struct vnode *vp = ap->a_vp;
742 struct smbnode *np = VTOSMB(vp);
745 kprintf("no smbnode data\n");
748 kprintf("tag VT_SMBFS, name = %s, parent = %p, opencount = %d",
749 np->n_name, np->n_parent ? np->n_parent : NULL,
751 lockmgr_printinfo(&vp->v_lock);
757 * smbfs_pathconf(struct vnode *vp, int name, register_t *retval)
760 smbfs_pathconf(struct vop_pathconf_args *ap)
762 struct smbmount *smp = VFSTOSMBFS(VTOVFS(ap->a_vp));
763 struct smb_vc *vcp = SSTOVC(smp->sm_share);
764 register_t *retval = ap->a_retval;
767 switch (ap->a_name) {
772 *retval = (vcp->vc_hflags2 & SMB_FLAGS2_KNOWS_LONG_NAMES) ? 255 : 12;
775 *retval = 800; /* XXX: a correct one ? */
784 * smbfs_strategy(struct vnode *a_vp, struct bio *a_bio)
787 smbfs_strategy(struct vop_strategy_args *ap)
789 struct bio *bio = ap->a_bio;
790 struct thread *td = NULL;
794 if (bio->bio_flags & BIO_SYNC)
795 td = curthread; /* XXX */
797 if (bio->bio_flags & BIO_SYNC)
798 error = smbfs_doio(ap->a_vp, bio, proc0.p_ucred, td);
803 * smbfs_bmap(struct vnode *a_vp, off_t a_loffset,
804 * off_t *a_doffsetp, int *a_runp, int *a_runb)
807 smbfs_bmap(struct vop_bmap_args *ap)
809 if (ap->a_doffsetp != NULL)
810 *ap->a_doffsetp = ap->a_loffset;
811 if (ap->a_runp != NULL)
813 if (ap->a_runb != NULL)
819 * smbfs_ioctl(struct vnode *a_vp, u_long a_command, caddr_t a_data,
820 * int fflag, struct ucred *cred, struct proc *p)
823 smbfs_ioctl(struct vop_ioctl_args *ap)
828 static char smbfs_atl[] = "rhsvda";
831 * smbfs_getextattr(struct vnode *a_vp, char *a_name, struct uio *a_uio,
832 * struct ucred *a_cred)
835 smbfs_getextattr(struct vop_getextattr_args *ap)
837 struct vnode *vp = ap->a_vp;
838 struct ucred *cred = ap->a_cred;
839 struct uio *uio = ap->a_uio;
840 const char *name = ap->a_attrname;
841 struct smbnode *np = VTOSMB(vp);
846 error = VOP_EACCESS(vp, VREAD, cred);
849 error = VOP_GETATTR(vp, &vattr);
852 if (strcmp(name, "dosattr") == 0) {
853 attr = np->n_dosattr;
854 for (i = 0; i < 6; i++, attr >>= 1)
855 buf[i] = (attr & 1) ? smbfs_atl[i] : '-';
857 error = uiomove(buf, i, uio);
865 * Since we expected to support F_GETLK (and SMB protocol has no such function),
866 * it is necessary to use lf_advlock(). It would be nice if this function had
867 * a callback mechanism because it will help to improve a level of consistency.
869 * smbfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
870 * struct flock *a_fl, int a_flags)
873 smbfs_advlock(struct vop_advlock_args *ap)
875 struct vnode *vp = ap->a_vp;
876 struct smbnode *np = VTOSMB(vp);
877 struct flock *fl = ap->a_fl;
878 caddr_t id = (caddr_t)1 /* ap->a_id */;
879 /* int flags = ap->a_flags;*/
880 struct thread *td = curthread; /* XXX */
881 struct smb_cred scred;
882 off_t start, end, size;
885 if (vp->v_type == VDIR) {
887 * SMB protocol have no support for directory locking.
888 * Although locks can be processed on local machine, I don't
889 * think that this is a good idea, because some programs
890 * can work wrong assuming directory is locked. So, we just
891 * return 'operation not supported
896 switch (fl->l_whence) {
902 start = fl->l_start + size;
911 end = start + fl->l_len - 1;
915 smb_makescred(&scred, td, td->td_proc ? td->td_proc->p_ucred : NULL);
918 switch (fl->l_type) {
920 lkop = SMB_LOCK_EXCL;
923 lkop = SMB_LOCK_SHARED;
926 lkop = SMB_LOCK_RELEASE;
931 error = lf_advlock(ap, &np->n_lockf, size);
934 lkop = SMB_LOCK_EXCL;
935 error = smbfs_smb_lock(np, lkop, id, start, end, &scred);
938 lf_advlock(ap, &np->n_lockf, size);
942 lf_advlock(ap, &np->n_lockf, size);
943 error = smbfs_smb_lock(np, SMB_LOCK_RELEASE, id, start, end, &scred);
946 error = lf_advlock(ap, &np->n_lockf, size);
955 smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop)
957 static const char *badchars = "*/:<>;?";
958 static const char *badchars83 = " +|,";
963 * Backslash characters, being a path delimiter, are prohibited
964 * within a path component even for LOOKUP operations.
966 if (index(name, '\\') != NULL)
969 if (nameiop == NAMEI_LOOKUP)
972 if (SMB_DIALECT(SSTOVC(smp->sm_share)) < SMB_DIALECT_LANMAN2_0) {
974 * Name should conform 8.3 format
978 cp = index(name, '.');
981 if (cp == name || (cp - name) > 8)
983 cp = index(cp + 1, '.');
986 for (cp = name, i = 0; i < nmlen; i++, cp++)
987 if (index(badchars83, *cp) != NULL)
990 for (cp = name, i = 0; i < nmlen; i++, cp++)
991 if (index(badchars, *cp) != NULL)
997 * Things go even weird without fixed inode numbers...
999 * smbfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1000 * struct componentname *a_cnp)
1003 smbfs_lookup(struct vop_old_lookup_args *ap)
1005 struct componentname *cnp = ap->a_cnp;
1006 struct thread *td = cnp->cn_td;
1007 struct vnode *dvp = ap->a_dvp;
1008 struct vnode **vpp = ap->a_vpp;
1010 struct smbmount *smp;
1011 struct mount *mp = dvp->v_mount;
1012 struct smbnode *dnp;
1013 struct smbfattr fattr, *fap;
1014 struct smb_cred scred;
1015 char *name = cnp->cn_nameptr;
1016 int flags = cnp->cn_flags;
1017 int nameiop = cnp->cn_nameiop;
1018 int nmlen = cnp->cn_namelen;
1019 int lockparent, wantparent, error, isdot;
1022 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1024 if (dvp->v_type != VDIR)
1026 if ((flags & CNP_ISDOTDOT) && (dvp->v_flag & VROOT)) {
1027 SMBFSERR("invalid '..'\n");
1030 #ifdef SMB_VNODE_DEBUG
1037 SMBVDEBUG("%d '%s' in '%s' id=d\n", nameiop, name,
1038 VTOSMB(dvp)->n_name);
1042 if ((mp->mnt_flag & MNT_RDONLY) && nameiop != NAMEI_LOOKUP)
1044 if ((error = VOP_EACCESS(dvp, VEXEC, cnp->cn_cred)) != 0)
1046 lockparent = flags & CNP_LOCKPARENT;
1047 wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
1048 smp = VFSTOSMBFS(mp);
1050 isdot = (nmlen == 1 && name[0] == '.');
1052 error = smbfs_pathcheck(smp, cnp->cn_nameptr, cnp->cn_namelen, nameiop);
1058 smb_makescred(&scred, td, cnp->cn_cred);
1060 if (flags & CNP_ISDOTDOT) {
1061 error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0, fap,
1063 SMBVDEBUG("result of dotdot lookup: %d\n", error);
1066 error = smbfs_smb_lookup(dnp, name, nmlen, fap, &scred);
1067 /* if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')*/
1068 SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1070 if (error && error != ENOENT)
1072 if (error) { /* entry not found */
1074 * Handle RENAME or CREATE case...
1076 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) && wantparent) {
1077 error = VOP_EACCESS(dvp, VWRITE, cnp->cn_cred);
1082 cnp->cn_flags |= CNP_PDIRUNLOCK;
1084 return (EJUSTRETURN);
1088 SMBVDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
1091 * handle DELETE case ...
1093 if (nameiop == NAMEI_DELETE) { /* delete last component */
1094 error = VOP_EACCESS(dvp, VWRITE, cnp->cn_cred);
1102 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1108 cnp->cn_flags |= CNP_PDIRUNLOCK;
1112 if (nameiop == NAMEI_RENAME && wantparent) {
1113 error = VOP_EACCESS(dvp, VWRITE, cnp->cn_cred);
1118 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1124 cnp->cn_flags |= CNP_PDIRUNLOCK;
1128 if (flags & CNP_ISDOTDOT) {
1130 error = smbfs_nget(mp, dvp, name, nmlen, NULL, &vp);
1132 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1136 error = vn_lock(dvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
1138 cnp->cn_flags |= CNP_PDIRUNLOCK;
1148 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1152 SMBVDEBUG("lookup: getnewvp!\n");
1155 cnp->cn_flags |= CNP_PDIRUNLOCK;