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.41 2007/11/20 21:03:50 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
40 #include <sys/namei.h>
41 #include <sys/fcntl.h>
42 #include <sys/mount.h>
43 #include <sys/unistd.h>
44 #include <sys/vnode.h>
45 #include <sys/lockf.h>
48 #include <vm/vm_extern.h>
49 #include <vm/vm_zone.h>
52 #include <netproto/smb/smb.h>
53 #include <netproto/smb/smb_conn.h>
54 #include <netproto/smb/smb_subr.h>
57 #include "smbfs_node.h"
58 #include "smbfs_subr.h"
63 * Prototypes for SMBFS vnode operations
65 static int smbfs_create(struct vop_old_create_args *);
66 static int smbfs_mknod(struct vop_old_mknod_args *);
67 static int smbfs_open(struct vop_open_args *);
68 static int smbfs_closel(struct vop_close_args *);
69 static int smbfs_access(struct vop_access_args *);
70 static int smbfs_getattr(struct vop_getattr_args *);
71 static int smbfs_setattr(struct vop_setattr_args *);
72 static int smbfs_read(struct vop_read_args *);
73 static int smbfs_write(struct vop_write_args *);
74 static int smbfs_fsync(struct vop_fsync_args *);
75 static int smbfs_remove(struct vop_old_remove_args *);
76 static int smbfs_link(struct vop_old_link_args *);
77 static int smbfs_lookup(struct vop_old_lookup_args *);
78 static int smbfs_rename(struct vop_old_rename_args *);
79 static int smbfs_mkdir(struct vop_old_mkdir_args *);
80 static int smbfs_rmdir(struct vop_old_rmdir_args *);
81 static int smbfs_symlink(struct vop_old_symlink_args *);
82 static int smbfs_readdir(struct vop_readdir_args *);
83 static int smbfs_bmap(struct vop_bmap_args *);
84 static int smbfs_strategy(struct vop_strategy_args *);
85 static int smbfs_print(struct vop_print_args *);
86 static int smbfs_pathconf(struct vop_pathconf_args *ap);
87 static int smbfs_advlock(struct vop_advlock_args *);
88 static int smbfs_getextattr(struct vop_getextattr_args *ap);
90 struct vop_ops smbfs_vnode_vops = {
91 .vop_default = vop_defaultop,
92 .vop_access = smbfs_access,
93 .vop_advlock = smbfs_advlock,
94 .vop_bmap = smbfs_bmap,
95 .vop_close = smbfs_closel,
96 .vop_old_create = smbfs_create,
97 .vop_fsync = smbfs_fsync,
98 .vop_getattr = smbfs_getattr,
99 .vop_getpages = smbfs_getpages,
100 .vop_inactive = smbfs_inactive,
101 .vop_ioctl = smbfs_ioctl,
102 .vop_old_link = smbfs_link,
103 .vop_old_lookup = smbfs_lookup,
104 .vop_old_mkdir = smbfs_mkdir,
105 .vop_old_mknod = smbfs_mknod,
106 .vop_open = smbfs_open,
107 .vop_pathconf = smbfs_pathconf,
108 .vop_print = smbfs_print,
109 .vop_putpages = smbfs_putpages,
110 .vop_read = smbfs_read,
111 .vop_readdir = smbfs_readdir,
112 .vop_reclaim = smbfs_reclaim,
113 .vop_old_remove = smbfs_remove,
114 .vop_old_rename = smbfs_rename,
115 .vop_old_rmdir = smbfs_rmdir,
116 .vop_setattr = smbfs_setattr,
117 .vop_strategy = smbfs_strategy,
118 .vop_old_symlink = smbfs_symlink,
119 .vop_write = smbfs_write,
120 .vop_getextattr = smbfs_getextattr
121 /* .vop_setextattr = smbfs_setextattr */
125 * smbfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
128 smbfs_access(struct vop_access_args *ap)
130 struct vnode *vp = ap->a_vp;
131 struct smbmount *smp = VTOSMBFS(vp);
136 mode = ((vp->v_type == VREG) ?
137 smp->sm_args.file_mode : smp->sm_args.dir_mode);
138 error = vop_helper_access(ap, smp->sm_args.uid, smp->sm_args.gid,
144 * smbfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
149 smbfs_open(struct vop_open_args *ap)
151 struct vnode *vp = ap->a_vp;
152 struct smbnode *np = VTOSMB(vp);
153 struct smb_cred scred;
155 int mode = ap->a_mode;
158 SMBVDEBUG("%s,%d\n", np->n_name, np->n_opencount);
159 if (vp->v_type != VREG && vp->v_type != VDIR) {
160 SMBFSERR("open eacces vtype=%d\n", vp->v_type);
163 if (vp->v_type == VDIR) {
164 if (np->n_opencount == 0)
165 np->n_cached_cred = crhold(ap->a_cred);
167 return (vop_stdopen(ap));
169 if (np->n_flag & NMODIFIED) {
170 if ((error = smbfs_vinvalbuf(vp, V_SAVE, 1)) == EINTR)
172 smbfs_attr_cacheremove(vp);
173 error = VOP_GETATTR(vp, &vattr);
176 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
178 error = VOP_GETATTR(vp, &vattr);
181 if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
182 error = smbfs_vinvalbuf(vp, V_SAVE, 1);
185 np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
188 if (np->n_opencount) {
190 return (vop_stdopen(ap));
192 accmode = SMB_AM_OPENREAD;
193 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
194 accmode = SMB_AM_OPENRW;
195 smb_makescred(&scred, curthread, ap->a_cred);
196 error = smbfs_smb_open(np, accmode, &scred);
200 accmode = SMB_AM_OPENREAD;
201 error = smbfs_smb_open(np, accmode, &scred);
204 np->n_cached_cred = crhold(ap->a_cred);
207 smbfs_attr_cacheremove(vp);
214 smbfs_closel(struct vop_close_args *ap)
216 struct vnode *vp = ap->a_vp;
217 struct smbnode *np = VTOSMB(vp);
218 struct smb_cred scred;
222 SMBVDEBUG("name=%s, pid=%d, c=%d\n",
223 np->n_name, p->p_pid, np->n_opencount);
224 vn_lock(vp, LK_UPGRADE | LK_RETRY);
226 smb_makescred(&scred, curthread, proc0.p_ucred);
229 if (np->n_opencount == 0) {
230 if (vp->v_type != VDIR)
231 SMBERROR("Negative opencount\n");
235 if (vp->v_type == VDIR) {
239 smbfs_findclose(np->n_dirseq, &scred);
243 error = smbfs_vinvalbuf(vp, V_SAVE, 1);
246 VOP_GETATTR(vp, &vattr);
247 error = smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
248 &np->n_mtime, &scred);
250 crfree(np->n_cached_cred);
251 np->n_cached_cred = NULL;
252 smbfs_attr_cacheremove(vp);
259 * smbfs_getattr call from vfs.
261 * smbfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
264 smbfs_getattr(struct vop_getattr_args *ap)
266 struct vnode *vp = ap->a_vp;
267 struct smbnode *np = VTOSMB(vp);
268 struct vattr *va=ap->a_vap;
269 struct smbfattr fattr;
270 struct smb_cred scred;
274 SMBVDEBUG("%lx: '%s' %d\n", (long)vp, np->n_name, (vp->v_flag & VROOT) != 0);
275 error = smbfs_attr_cachelookup(vp, va);
278 SMBVDEBUG("not in the cache\n");
279 smb_makescred(&scred, curthread, proc0.p_ucred);
280 oldsize = np->n_size;
281 error = smbfs_smb_lookup(np, NULL, 0, &fattr, &scred);
283 SMBVDEBUG("error %d\n", error);
286 smbfs_attr_cacheenter(vp, &fattr);
287 smbfs_attr_cachelookup(vp, va);
289 np->n_size = oldsize;
294 * smbfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
297 smbfs_setattr(struct vop_setattr_args *ap)
299 struct vnode *vp = ap->a_vp;
300 struct smbnode *np = VTOSMB(vp);
301 struct vattr *vap = ap->a_vap;
302 struct timespec *mtime, *atime;
303 struct smb_cred scred;
304 struct smb_share *ssp = np->n_mount->sm_share;
305 struct smb_vc *vcp = SSTOVC(ssp);
307 int isreadonly, doclose, error = 0;
310 if (vap->va_flags != VNOVAL)
312 isreadonly = (vp->v_mount->mnt_flag & MNT_RDONLY);
314 * Disallow write attempts if the filesystem is mounted read-only.
316 if ((vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL ||
317 vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
318 vap->va_mode != (mode_t)VNOVAL) && isreadonly)
320 smb_makescred(&scred, curthread, ap->a_cred);
321 if (vap->va_size != VNOVAL) {
322 switch (vp->v_type) {
333 vnode_pager_setsize(vp, (u_long)vap->va_size);
335 np->n_size = vap->va_size;
336 if (np->n_opencount == 0) {
337 error = smbfs_smb_open(np, SMB_AM_OPENRW, &scred);
342 error = smbfs_smb_setfsize(np, vap->va_size, &scred);
344 smbfs_smb_close(ssp, np->n_fid, NULL, &scred);
347 vnode_pager_setsize(vp, (u_long)tsize);
351 mtime = atime = NULL;
352 if (vap->va_mtime.tv_sec != VNOVAL)
353 mtime = &vap->va_mtime;
354 if (vap->va_atime.tv_sec != VNOVAL)
355 atime = &vap->va_atime;
356 if (mtime != atime) {
357 if (ap->a_cred->cr_uid != VTOSMBFS(vp)->sm_args.uid &&
358 (error = priv_check_cred(ap->a_cred, PRIV_VFS_SETATTR, 0)) &&
359 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
360 (error = VOP_EACCESS(vp, VWRITE, ap->a_cred))))
364 mtime = &np->n_mtime;
366 atime = &np->n_atime;
369 * If file is opened, then we can use handle based calls.
370 * If not, use path based ones.
372 if (np->n_opencount == 0) {
373 if (vcp->vc_flags & SMBV_WIN95) {
374 error = VOP_OPEN(vp, FWRITE, ap->a_cred, NULL);
376 /* error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
377 VOP_GETATTR(vp, &vattr);*/
379 np->n_mtime = *mtime;
380 VOP_CLOSE(vp, FWRITE);
382 } else if ((vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS)) {
383 error = smbfs_smb_setptime2(np, mtime, atime, 0, &scred);
384 /* error = smbfs_smb_setpattrNT(np, 0, mtime, atime, &scred);*/
385 } else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN2_0) {
386 error = smbfs_smb_setptime2(np, mtime, atime, 0, &scred);
388 error = smbfs_smb_setpattr(np, 0, mtime, &scred);
391 if (vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS) {
392 error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
393 } else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN1_0) {
394 error = smbfs_smb_setftime(np, mtime, atime, &scred);
397 * I have no idea how to handle this for core
398 * level servers. The possible solution is to
399 * update mtime after file is closed.
401 SMBERROR("can't update times on an opened file\n");
406 * Invalidate attribute cache in case if server doesn't set
407 * required attributes.
409 smbfs_attr_cacheremove(vp); /* invalidate cache */
410 VOP_GETATTR(vp, vap);
411 np->n_mtime.tv_sec = vap->va_mtime.tv_sec;
417 * smbfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
418 * struct ucred *a_cred)
421 smbfs_read(struct vop_read_args *ap)
423 struct vnode *vp = ap->a_vp;
424 struct uio *uio = ap->a_uio;
427 if (vp->v_type != VREG && vp->v_type != VDIR)
429 return smbfs_readvnode(vp, uio, ap->a_cred);
433 * smbfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
434 * struct ucred *a_cred)
437 smbfs_write(struct vop_write_args *ap)
439 struct vnode *vp = ap->a_vp;
440 struct uio *uio = ap->a_uio;
442 SMBVDEBUG("%d,ofs=%d,sz=%d\n",vp->v_type, (int)uio->uio_offset, uio->uio_resid);
443 if (vp->v_type != VREG)
445 return smbfs_writevnode(vp, uio, ap->a_cred,ap->a_ioflag);
449 * Create a regular file. On entry the directory to contain the file being
450 * created is locked. We must release before we return.
452 * smbfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
453 * struct componentname *a_cnp, struct vattr *a_vap)
456 smbfs_create(struct vop_old_create_args *ap)
458 struct vnode *dvp = ap->a_dvp;
459 struct vattr *vap = ap->a_vap;
460 struct vnode **vpp=ap->a_vpp;
461 struct componentname *cnp = ap->a_cnp;
462 struct smbnode *dnp = VTOSMB(dvp);
465 struct smbfattr fattr;
466 struct smb_cred scred;
467 char *name = cnp->cn_nameptr;
468 int nmlen = cnp->cn_namelen;
474 if (vap->va_type != VREG)
476 if ((error = VOP_GETATTR(dvp, &vattr)))
478 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
480 error = smbfs_smb_create(dnp, name, nmlen, &scred);
483 error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, &scred);
486 error = smbfs_nget(VTOVFS(dvp), dvp, name, nmlen, &fattr, &vp);
494 * smbfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
495 * struct componentname *a_cnp)
498 smbfs_remove(struct vop_old_remove_args *ap)
500 struct vnode *vp = ap->a_vp;
501 /* struct vnode *dvp = ap->a_dvp;*/
502 struct componentname *cnp = ap->a_cnp;
503 struct smbnode *np = VTOSMB(vp);
504 struct smb_cred scred;
507 if (vp->v_type == VDIR || np->n_opencount || VREFCNT(vp) > 1)
509 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
510 error = smbfs_smb_delete(np, &scred);
515 * smbfs_file rename call
517 * smbfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
518 * struct componentname *a_fcnp, struct vnode *a_tdvp,
519 * struct vnode *a_tvp, struct componentname *a_tcnp)
522 smbfs_rename(struct vop_old_rename_args *ap)
524 struct vnode *fvp = ap->a_fvp;
525 struct vnode *tvp = ap->a_tvp;
526 struct vnode *fdvp = ap->a_fdvp;
527 struct vnode *tdvp = ap->a_tdvp;
528 struct componentname *tcnp = ap->a_tcnp;
529 /* struct componentname *fcnp = ap->a_fcnp;*/
530 struct smb_cred scred;
534 /* Check for cross-device rename */
535 if ((fvp->v_mount != tdvp->v_mount) ||
536 (tvp && (fvp->v_mount != tvp->v_mount))) {
541 if (tvp && VREFCNT(tvp) > 1) {
545 flags = 0x10; /* verify all writes */
546 if (fvp->v_type == VDIR) {
548 } else if (fvp->v_type == VREG) {
554 smb_makescred(&scred, tcnp->cn_td, tcnp->cn_cred);
556 * It seems that Samba doesn't implement SMB_COM_MOVE call...
559 if (SMB_DIALECT(SSTOCN(smp->sm_share)) >= SMB_DIALECT_LANMAN1_0) {
560 error = smbfs_smb_move(VTOSMB(fvp), VTOSMB(tdvp),
561 tcnp->cn_nameptr, tcnp->cn_namelen, flags, &scred);
566 * We have to do the work atomicaly
568 if (tvp && tvp != fvp) {
569 error = smbfs_smb_delete(VTOSMB(tvp), &scred);
573 error = smbfs_smb_rename(VTOSMB(fvp), VTOSMB(tdvp),
574 tcnp->cn_nameptr, tcnp->cn_namelen, &scred);
578 smbfs_attr_cacheremove(fdvp);
579 smbfs_attr_cacheremove(tdvp);
589 #ifdef possible_mistake
599 * somtime it will come true...
601 * smbfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
602 * struct componentname *a_cnp)
605 smbfs_link(struct vop_old_link_args *ap)
611 * smbfs_symlink link create call.
612 * Sometime it will be functional...
614 * smbfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
615 * struct componentname *a_cnp, struct vattr *a_vap,
619 smbfs_symlink(struct vop_old_symlink_args *ap)
625 smbfs_mknod(struct vop_old_mknod_args *ap)
631 * smbfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
632 * struct componentname *a_cnp, struct vattr *a_vap)
635 smbfs_mkdir(struct vop_old_mkdir_args *ap)
637 struct vnode *dvp = ap->a_dvp;
638 /* struct vattr *vap = ap->a_vap;*/
640 struct componentname *cnp = ap->a_cnp;
641 struct smbnode *dnp = VTOSMB(dvp);
643 struct smb_cred scred;
644 struct smbfattr fattr;
645 char *name = cnp->cn_nameptr;
646 int len = cnp->cn_namelen;
649 if ((error = VOP_GETATTR(dvp, &vattr))) {
652 if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.'))))
654 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
655 error = smbfs_smb_mkdir(dnp, name, len, &scred);
658 error = smbfs_smb_lookup(dnp, name, len, &fattr, &scred);
661 error = smbfs_nget(VTOVFS(dvp), dvp, name, len, &fattr, &vp);
669 * smbfs_remove directory call
671 * smbfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
672 * struct componentname *a_cnp)
675 smbfs_rmdir(struct vop_old_rmdir_args *ap)
677 struct vnode *vp = ap->a_vp;
678 struct vnode *dvp = ap->a_dvp;
679 struct componentname *cnp = ap->a_cnp;
680 /* struct smbmount *smp = VTOSMBFS(vp);*/
681 struct smbnode *dnp = VTOSMB(dvp);
682 struct smbnode *np = VTOSMB(vp);
683 struct smb_cred scred;
689 smb_makescred(&scred, cnp->cn_td, cnp->cn_cred);
690 error = smbfs_smb_rmdir(np, &scred);
691 dnp->n_flag |= NMODIFIED;
692 smbfs_attr_cacheremove(dvp);
699 * smbfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
700 * int *a_eofflag, off_t *a_cookies, int a_ncookies)
703 smbfs_readdir(struct vop_readdir_args *ap)
705 struct vnode *vp = ap->a_vp;
706 struct uio *uio = ap->a_uio;
709 if (vp->v_type != VDIR)
712 if (ap->a_ncookies) {
713 kprintf("smbfs_readdir: no support for cookies now...");
717 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
719 error = smbfs_readvnode(vp, uio, ap->a_cred);
726 * smbfs_fsync(struct vnode *a_vp, int a_waitfor)
730 smbfs_fsync(struct vop_fsync_args *ap)
732 /* return (smb_flush(ap->a_vp, ap->a_waitfor, curthread, 1));*/
737 * smbfs_print(struct vnode *a_vp)
740 smbfs_print(struct vop_print_args *ap)
742 struct vnode *vp = ap->a_vp;
743 struct smbnode *np = VTOSMB(vp);
746 kprintf("no smbnode data\n");
749 kprintf("tag VT_SMBFS, name = %s, parent = %p, opencount = %d",
750 np->n_name, np->n_parent ? np->n_parent : NULL,
752 lockmgr_printinfo(&vp->v_lock);
758 * smbfs_pathconf(struct vnode *vp, int name, register_t *retval)
761 smbfs_pathconf(struct vop_pathconf_args *ap)
763 struct smbmount *smp = VFSTOSMBFS(VTOVFS(ap->a_vp));
764 struct smb_vc *vcp = SSTOVC(smp->sm_share);
765 register_t *retval = ap->a_retval;
768 switch (ap->a_name) {
773 *retval = (vcp->vc_hflags2 & SMB_FLAGS2_KNOWS_LONG_NAMES) ? 255 : 12;
776 *retval = 800; /* XXX: a correct one ? */
785 * smbfs_strategy(struct vnode *a_vp, struct bio *a_bio)
788 smbfs_strategy(struct vop_strategy_args *ap)
790 struct bio *bio = ap->a_bio;
791 struct thread *td = NULL;
795 if (bio->bio_flags & BIO_SYNC)
796 td = curthread; /* XXX */
798 if (bio->bio_flags & BIO_SYNC)
799 error = smbfs_doio(ap->a_vp, bio, proc0.p_ucred, td);
804 * smbfs_bmap(struct vnode *a_vp, off_t a_loffset,
805 * off_t *a_doffsetp, int *a_runp, int *a_runb)
808 smbfs_bmap(struct vop_bmap_args *ap)
810 if (ap->a_doffsetp != NULL)
811 *ap->a_doffsetp = ap->a_loffset;
812 if (ap->a_runp != NULL)
814 if (ap->a_runb != NULL)
820 * smbfs_ioctl(struct vnode *a_vp, u_long a_command, caddr_t a_data,
821 * int fflag, struct ucred *cred, struct proc *p)
824 smbfs_ioctl(struct vop_ioctl_args *ap)
829 static char smbfs_atl[] = "rhsvda";
832 * smbfs_getextattr(struct vnode *a_vp, char *a_name, struct uio *a_uio,
833 * struct ucred *a_cred)
836 smbfs_getextattr(struct vop_getextattr_args *ap)
838 struct vnode *vp = ap->a_vp;
839 struct ucred *cred = ap->a_cred;
840 struct uio *uio = ap->a_uio;
841 const char *name = ap->a_attrname;
842 struct smbnode *np = VTOSMB(vp);
847 error = VOP_EACCESS(vp, VREAD, cred);
850 error = VOP_GETATTR(vp, &vattr);
853 if (strcmp(name, "dosattr") == 0) {
854 attr = np->n_dosattr;
855 for (i = 0; i < 6; i++, attr >>= 1)
856 buf[i] = (attr & 1) ? smbfs_atl[i] : '-';
858 error = uiomove(buf, i, uio);
866 * Since we expected to support F_GETLK (and SMB protocol has no such function),
867 * it is necessary to use lf_advlock(). It would be nice if this function had
868 * a callback mechanism because it will help to improve a level of consistency.
870 * smbfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
871 * struct flock *a_fl, int a_flags)
874 smbfs_advlock(struct vop_advlock_args *ap)
876 struct vnode *vp = ap->a_vp;
877 struct smbnode *np = VTOSMB(vp);
878 struct flock *fl = ap->a_fl;
879 caddr_t id = (caddr_t)1 /* ap->a_id */;
880 /* int flags = ap->a_flags;*/
881 struct thread *td = curthread; /* XXX */
882 struct smb_cred scred;
883 off_t start, end, size;
886 if (vp->v_type == VDIR) {
888 * SMB protocol have no support for directory locking.
889 * Although locks can be processed on local machine, I don't
890 * think that this is a good idea, because some programs
891 * can work wrong assuming directory is locked. So, we just
892 * return 'operation not supported
897 switch (fl->l_whence) {
903 start = fl->l_start + size;
912 end = start + fl->l_len - 1;
916 smb_makescred(&scred, td, td->td_proc ? td->td_proc->p_ucred : NULL);
919 switch (fl->l_type) {
921 lkop = SMB_LOCK_EXCL;
924 lkop = SMB_LOCK_SHARED;
927 lkop = SMB_LOCK_RELEASE;
932 error = lf_advlock(ap, &np->n_lockf, size);
935 lkop = SMB_LOCK_EXCL;
936 error = smbfs_smb_lock(np, lkop, id, start, end, &scred);
939 lf_advlock(ap, &np->n_lockf, size);
943 lf_advlock(ap, &np->n_lockf, size);
944 error = smbfs_smb_lock(np, SMB_LOCK_RELEASE, id, start, end, &scred);
947 error = lf_advlock(ap, &np->n_lockf, size);
956 smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop)
958 static const char *badchars = "*/:<>;?";
959 static const char *badchars83 = " +|,";
964 * Backslash characters, being a path delimiter, are prohibited
965 * within a path component even for LOOKUP operations.
967 if (index(name, '\\') != NULL)
970 if (nameiop == NAMEI_LOOKUP)
973 if (SMB_DIALECT(SSTOVC(smp->sm_share)) < SMB_DIALECT_LANMAN2_0) {
975 * Name should conform 8.3 format
979 cp = index(name, '.');
982 if (cp == name || (cp - name) > 8)
984 cp = index(cp + 1, '.');
987 for (cp = name, i = 0; i < nmlen; i++, cp++)
988 if (index(badchars83, *cp) != NULL)
991 for (cp = name, i = 0; i < nmlen; i++, cp++)
992 if (index(badchars, *cp) != NULL)
998 * Things go even weird without fixed inode numbers...
1000 * smbfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1001 * struct componentname *a_cnp)
1004 smbfs_lookup(struct vop_old_lookup_args *ap)
1006 struct componentname *cnp = ap->a_cnp;
1007 struct thread *td = cnp->cn_td;
1008 struct vnode *dvp = ap->a_dvp;
1009 struct vnode **vpp = ap->a_vpp;
1011 struct smbmount *smp;
1012 struct mount *mp = dvp->v_mount;
1013 struct smbnode *dnp;
1014 struct smbfattr fattr, *fap;
1015 struct smb_cred scred;
1016 char *name = cnp->cn_nameptr;
1017 int flags = cnp->cn_flags;
1018 int nameiop = cnp->cn_nameiop;
1019 int nmlen = cnp->cn_namelen;
1020 int lockparent, wantparent, error, isdot;
1023 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1025 if (dvp->v_type != VDIR)
1027 if ((flags & CNP_ISDOTDOT) && (dvp->v_flag & VROOT)) {
1028 SMBFSERR("invalid '..'\n");
1031 #ifdef SMB_VNODE_DEBUG
1038 SMBVDEBUG("%d '%s' in '%s' id=d\n", nameiop, name,
1039 VTOSMB(dvp)->n_name);
1043 if ((mp->mnt_flag & MNT_RDONLY) && nameiop != NAMEI_LOOKUP)
1045 if ((error = VOP_EACCESS(dvp, VEXEC, cnp->cn_cred)) != 0)
1047 lockparent = flags & CNP_LOCKPARENT;
1048 wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
1049 smp = VFSTOSMBFS(mp);
1051 isdot = (nmlen == 1 && name[0] == '.');
1053 error = smbfs_pathcheck(smp, cnp->cn_nameptr, cnp->cn_namelen, nameiop);
1059 smb_makescred(&scred, td, cnp->cn_cred);
1061 if (flags & CNP_ISDOTDOT) {
1062 error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0, fap,
1064 SMBVDEBUG("result of dotdot lookup: %d\n", error);
1067 error = smbfs_smb_lookup(dnp, name, nmlen, fap, &scred);
1068 /* if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')*/
1069 SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1071 if (error && error != ENOENT)
1073 if (error) { /* entry not found */
1075 * Handle RENAME or CREATE case...
1077 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) && wantparent) {
1078 error = VOP_EACCESS(dvp, VWRITE, cnp->cn_cred);
1083 cnp->cn_flags |= CNP_PDIRUNLOCK;
1085 return (EJUSTRETURN);
1089 SMBVDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
1092 * handle DELETE case ...
1094 if (nameiop == NAMEI_DELETE) { /* delete last component */
1095 error = VOP_EACCESS(dvp, VWRITE, cnp->cn_cred);
1103 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1109 cnp->cn_flags |= CNP_PDIRUNLOCK;
1113 if (nameiop == NAMEI_RENAME && wantparent) {
1114 error = VOP_EACCESS(dvp, VWRITE, cnp->cn_cred);
1119 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1125 cnp->cn_flags |= CNP_PDIRUNLOCK;
1129 if (flags & CNP_ISDOTDOT) {
1131 error = smbfs_nget(mp, dvp, name, nmlen, NULL, &vp);
1133 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1137 error = vn_lock(dvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
1139 cnp->cn_flags |= CNP_PDIRUNLOCK;
1149 error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1153 SMBVDEBUG("lookup: getnewvp!\n");
1156 cnp->cn_flags |= CNP_PDIRUNLOCK;