xref: /freebsd/sys/fs/smbfs/smbfs_vnops.c (revision ce589ae2a75beb3d7632d3826a944801de7ea2c9)
1d167cf6fSWarner Losh /*-
2681a5bbeSBoris Popov  * Copyright (c) 2000-2001 Boris Popov
3681a5bbeSBoris Popov  * All rights reserved.
4681a5bbeSBoris Popov  *
5681a5bbeSBoris Popov  * Redistribution and use in source and binary forms, with or without
6681a5bbeSBoris Popov  * modification, are permitted provided that the following conditions
7681a5bbeSBoris Popov  * are met:
8681a5bbeSBoris Popov  * 1. Redistributions of source code must retain the above copyright
9681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer.
10681a5bbeSBoris Popov  * 2. Redistributions in binary form must reproduce the above copyright
11681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer in the
12681a5bbeSBoris Popov  *    documentation and/or other materials provided with the distribution.
13681a5bbeSBoris Popov  *
14681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24681a5bbeSBoris Popov  * SUCH DAMAGE.
25681a5bbeSBoris Popov  *
26681a5bbeSBoris Popov  * $FreeBSD$
27681a5bbeSBoris Popov  */
28681a5bbeSBoris Popov #include <sys/param.h>
29681a5bbeSBoris Popov #include <sys/systm.h>
30681a5bbeSBoris Popov #include <sys/namei.h>
31681a5bbeSBoris Popov #include <sys/kernel.h>
32681a5bbeSBoris Popov #include <sys/proc.h>
33681a5bbeSBoris Popov #include <sys/bio.h>
34681a5bbeSBoris Popov #include <sys/buf.h>
35681a5bbeSBoris Popov #include <sys/fcntl.h>
36681a5bbeSBoris Popov #include <sys/mount.h>
37681a5bbeSBoris Popov #include <sys/unistd.h>
38681a5bbeSBoris Popov #include <sys/vnode.h>
39104a9b7eSAlexander Kabaev #include <sys/limits.h>
40681a5bbeSBoris Popov #include <sys/lockf.h>
41fb8e9eadSBoris Popov #include <sys/stat.h>
42681a5bbeSBoris Popov 
43681a5bbeSBoris Popov #include <vm/vm.h>
44681a5bbeSBoris Popov #include <vm/vm_extern.h>
45681a5bbeSBoris Popov 
46681a5bbeSBoris Popov 
47681a5bbeSBoris Popov #include <netsmb/smb.h>
48681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
49681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
50681a5bbeSBoris Popov 
51681a5bbeSBoris Popov #include <fs/smbfs/smbfs.h>
52681a5bbeSBoris Popov #include <fs/smbfs/smbfs_node.h>
53681a5bbeSBoris Popov #include <fs/smbfs/smbfs_subr.h>
54681a5bbeSBoris Popov 
55681a5bbeSBoris Popov /*
56681a5bbeSBoris Popov  * Prototypes for SMBFS vnode operations
57681a5bbeSBoris Popov  */
586fde64c7SPoul-Henning Kamp static vop_create_t	smbfs_create;
596fde64c7SPoul-Henning Kamp static vop_mknod_t	smbfs_mknod;
606fde64c7SPoul-Henning Kamp static vop_open_t	smbfs_open;
616fde64c7SPoul-Henning Kamp static vop_close_t	smbfs_close;
626fde64c7SPoul-Henning Kamp static vop_access_t	smbfs_access;
636fde64c7SPoul-Henning Kamp static vop_getattr_t	smbfs_getattr;
646fde64c7SPoul-Henning Kamp static vop_setattr_t	smbfs_setattr;
656fde64c7SPoul-Henning Kamp static vop_read_t	smbfs_read;
666fde64c7SPoul-Henning Kamp static vop_write_t	smbfs_write;
676fde64c7SPoul-Henning Kamp static vop_fsync_t	smbfs_fsync;
686fde64c7SPoul-Henning Kamp static vop_remove_t	smbfs_remove;
696fde64c7SPoul-Henning Kamp static vop_link_t	smbfs_link;
706fde64c7SPoul-Henning Kamp static vop_lookup_t	smbfs_lookup;
716fde64c7SPoul-Henning Kamp static vop_rename_t	smbfs_rename;
726fde64c7SPoul-Henning Kamp static vop_mkdir_t	smbfs_mkdir;
736fde64c7SPoul-Henning Kamp static vop_rmdir_t	smbfs_rmdir;
746fde64c7SPoul-Henning Kamp static vop_symlink_t	smbfs_symlink;
756fde64c7SPoul-Henning Kamp static vop_readdir_t	smbfs_readdir;
766fde64c7SPoul-Henning Kamp static vop_strategy_t	smbfs_strategy;
776fde64c7SPoul-Henning Kamp static vop_print_t	smbfs_print;
786fde64c7SPoul-Henning Kamp static vop_pathconf_t	smbfs_pathconf;
796fde64c7SPoul-Henning Kamp static vop_advlock_t	smbfs_advlock;
806fde64c7SPoul-Henning Kamp static vop_getextattr_t	smbfs_getextattr;
81681a5bbeSBoris Popov 
82aec0fb7bSPoul-Henning Kamp struct vop_vector smbfs_vnodeops = {
83aec0fb7bSPoul-Henning Kamp 	.vop_default =		&default_vnodeops,
8483c64397SPoul-Henning Kamp 
85aec0fb7bSPoul-Henning Kamp 	.vop_access =		smbfs_access,
86aec0fb7bSPoul-Henning Kamp 	.vop_advlock =		smbfs_advlock,
87aec0fb7bSPoul-Henning Kamp 	.vop_close =		smbfs_close,
88aec0fb7bSPoul-Henning Kamp 	.vop_create =		smbfs_create,
89aec0fb7bSPoul-Henning Kamp 	.vop_fsync =		smbfs_fsync,
90aec0fb7bSPoul-Henning Kamp 	.vop_getattr =		smbfs_getattr,
9183c64397SPoul-Henning Kamp 	.vop_getextattr = 	smbfs_getextattr,
92aec0fb7bSPoul-Henning Kamp 	.vop_getpages =		smbfs_getpages,
93aec0fb7bSPoul-Henning Kamp 	.vop_inactive =		smbfs_inactive,
94aec0fb7bSPoul-Henning Kamp 	.vop_ioctl =		smbfs_ioctl,
95aec0fb7bSPoul-Henning Kamp 	.vop_link =		smbfs_link,
96aec0fb7bSPoul-Henning Kamp 	.vop_lookup =		smbfs_lookup,
97aec0fb7bSPoul-Henning Kamp 	.vop_mkdir =		smbfs_mkdir,
98aec0fb7bSPoul-Henning Kamp 	.vop_mknod =		smbfs_mknod,
99aec0fb7bSPoul-Henning Kamp 	.vop_open =		smbfs_open,
100aec0fb7bSPoul-Henning Kamp 	.vop_pathconf =		smbfs_pathconf,
101aec0fb7bSPoul-Henning Kamp 	.vop_print =		smbfs_print,
102aec0fb7bSPoul-Henning Kamp 	.vop_putpages =		smbfs_putpages,
103aec0fb7bSPoul-Henning Kamp 	.vop_read =		smbfs_read,
104aec0fb7bSPoul-Henning Kamp 	.vop_readdir =		smbfs_readdir,
105aec0fb7bSPoul-Henning Kamp 	.vop_reclaim =		smbfs_reclaim,
106aec0fb7bSPoul-Henning Kamp 	.vop_remove =		smbfs_remove,
107aec0fb7bSPoul-Henning Kamp 	.vop_rename =		smbfs_rename,
108aec0fb7bSPoul-Henning Kamp 	.vop_rmdir =		smbfs_rmdir,
109aec0fb7bSPoul-Henning Kamp 	.vop_setattr =		smbfs_setattr,
11083c64397SPoul-Henning Kamp /*	.vop_setextattr =	smbfs_setextattr,*/
111aec0fb7bSPoul-Henning Kamp 	.vop_strategy =		smbfs_strategy,
112aec0fb7bSPoul-Henning Kamp 	.vop_symlink =		smbfs_symlink,
113aec0fb7bSPoul-Henning Kamp 	.vop_write =		smbfs_write,
114681a5bbeSBoris Popov };
115681a5bbeSBoris Popov 
116681a5bbeSBoris Popov static int
117681a5bbeSBoris Popov smbfs_access(ap)
118681a5bbeSBoris Popov 	struct vop_access_args /* {
119681a5bbeSBoris Popov 		struct vnode *a_vp;
12015bc6b2bSEdward Tomasz Napierala 		accmode_t a_accmode;
121681a5bbeSBoris Popov 		struct ucred *a_cred;
122b1c996c4SBoris Popov 		struct thread *a_td;
123681a5bbeSBoris Popov 	} */ *ap;
124681a5bbeSBoris Popov {
125681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
12615bc6b2bSEdward Tomasz Napierala 	accmode_t accmode = ap->a_accmode;
12738356d10STim J. Robbins 	mode_t mpmode;
128681a5bbeSBoris Popov 	struct smbmount *smp = VTOSMBFS(vp);
129681a5bbeSBoris Popov 
130681a5bbeSBoris Popov 	SMBVDEBUG("\n");
13115bc6b2bSEdward Tomasz Napierala 	if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
132681a5bbeSBoris Popov 		switch (vp->v_type) {
133681a5bbeSBoris Popov 		    case VREG: case VDIR: case VLNK:
134681a5bbeSBoris Popov 			return EROFS;
135681a5bbeSBoris Popov 		    default:
136681a5bbeSBoris Popov 			break;
137681a5bbeSBoris Popov 		}
138681a5bbeSBoris Popov 	}
139d14c8441SPoul-Henning Kamp 	mpmode = vp->v_type == VREG ? smp->sm_file_mode : smp->sm_dir_mode;
140d14c8441SPoul-Henning Kamp 	return (vaccess(vp->v_type, mpmode, smp->sm_uid,
14115bc6b2bSEdward Tomasz Napierala 	    smp->sm_gid, ap->a_accmode, ap->a_cred, NULL));
142681a5bbeSBoris Popov }
143681a5bbeSBoris Popov 
144681a5bbeSBoris Popov /* ARGSUSED */
145681a5bbeSBoris Popov static int
146681a5bbeSBoris Popov smbfs_open(ap)
147681a5bbeSBoris Popov 	struct vop_open_args /* {
148681a5bbeSBoris Popov 		struct vnode *a_vp;
149681a5bbeSBoris Popov 		int  a_mode;
150681a5bbeSBoris Popov 		struct ucred *a_cred;
151b1c996c4SBoris Popov 		struct thread *a_td;
152681a5bbeSBoris Popov 	} */ *ap;
153681a5bbeSBoris Popov {
154681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
155681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
156afe09751SDavide Italiano 	struct smb_cred *scred;
157681a5bbeSBoris Popov 	struct vattr vattr;
158681a5bbeSBoris Popov 	int mode = ap->a_mode;
159681a5bbeSBoris Popov 	int error, accmode;
160681a5bbeSBoris Popov 
1612a4ad258STim J. Robbins 	SMBVDEBUG("%s,%d\n", np->n_name, (np->n_flag & NOPEN) != 0);
162681a5bbeSBoris Popov 	if (vp->v_type != VREG && vp->v_type != VDIR) {
163681a5bbeSBoris Popov 		SMBFSERR("open eacces vtype=%d\n", vp->v_type);
164681a5bbeSBoris Popov 		return EACCES;
165681a5bbeSBoris Popov 	}
166681a5bbeSBoris Popov 	if (vp->v_type == VDIR) {
1672a4ad258STim J. Robbins 		np->n_flag |= NOPEN;
168681a5bbeSBoris Popov 		return 0;
169681a5bbeSBoris Popov 	}
170681a5bbeSBoris Popov 	if (np->n_flag & NMODIFIED) {
171e50508dfSPoul-Henning Kamp 		if ((error = smbfs_vinvalbuf(vp, ap->a_td)) == EINTR)
172681a5bbeSBoris Popov 			return error;
173681a5bbeSBoris Popov 		smbfs_attr_cacheremove(vp);
1740359a12eSAttilio Rao 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
175681a5bbeSBoris Popov 		if (error)
176681a5bbeSBoris Popov 			return error;
177681a5bbeSBoris Popov 		np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
178681a5bbeSBoris Popov 	} else {
1790359a12eSAttilio Rao 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
180681a5bbeSBoris Popov 		if (error)
181681a5bbeSBoris Popov 			return error;
182681a5bbeSBoris Popov 		if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
183e50508dfSPoul-Henning Kamp 			error = smbfs_vinvalbuf(vp, ap->a_td);
184681a5bbeSBoris Popov 			if (error == EINTR)
185681a5bbeSBoris Popov 				return error;
186681a5bbeSBoris Popov 			np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
187681a5bbeSBoris Popov 		}
188681a5bbeSBoris Popov 	}
1892a4ad258STim J. Robbins 	if ((np->n_flag & NOPEN) != 0)
190681a5bbeSBoris Popov 		return 0;
19144f3878eSBoris Popov 	/*
19244f3878eSBoris Popov 	 * Use DENYNONE to give unixy semantics of permitting
19344f3878eSBoris Popov 	 * everything not forbidden by permissions.  Ie denial
19444f3878eSBoris Popov 	 * is up to server with clients/openers needing to use
19544f3878eSBoris Popov 	 * advisory locks for further control.
19644f3878eSBoris Popov 	 */
19744f3878eSBoris Popov 	accmode = SMB_SM_DENYNONE|SMB_AM_OPENREAD;
198681a5bbeSBoris Popov 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
19944f3878eSBoris Popov 		accmode = SMB_SM_DENYNONE|SMB_AM_OPENRW;
200afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
201afe09751SDavide Italiano 	smb_makescred(scred, ap->a_td, ap->a_cred);
202afe09751SDavide Italiano 	error = smbfs_smb_open(np, accmode, scred);
203681a5bbeSBoris Popov 	if (error) {
204681a5bbeSBoris Popov 		if (mode & FWRITE)
205681a5bbeSBoris Popov 			return EACCES;
20644f3878eSBoris Popov 		else if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
20744f3878eSBoris Popov 			accmode = SMB_SM_DENYNONE|SMB_AM_OPENREAD;
208afe09751SDavide Italiano 			error = smbfs_smb_open(np, accmode, scred);
209681a5bbeSBoris Popov 		}
21044f3878eSBoris Popov 	}
21172b3e305SPeter Edwards 	if (error == 0) {
2122a4ad258STim J. Robbins 		np->n_flag |= NOPEN;
21372b3e305SPeter Edwards 		vnode_create_vobject(ap->a_vp, vattr.va_size, ap->a_td);
21472b3e305SPeter Edwards 	}
215681a5bbeSBoris Popov 	smbfs_attr_cacheremove(vp);
216afe09751SDavide Italiano 	smbfs_free_scred(scred);
217681a5bbeSBoris Popov 	return error;
218681a5bbeSBoris Popov }
219681a5bbeSBoris Popov 
220681a5bbeSBoris Popov static int
221681a5bbeSBoris Popov smbfs_close(ap)
222681a5bbeSBoris Popov 	struct vop_close_args /* {
223681a5bbeSBoris Popov 		struct vnodeop_desc *a_desc;
224681a5bbeSBoris Popov 		struct vnode *a_vp;
225681a5bbeSBoris Popov 		int  a_fflag;
226681a5bbeSBoris Popov 		struct ucred *a_cred;
227b1c996c4SBoris Popov 		struct thread *a_td;
228681a5bbeSBoris Popov 	} */ *ap;
229681a5bbeSBoris Popov {
230681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
231b1c996c4SBoris Popov 	struct thread *td = ap->a_td;
232835fb616STim J. Robbins 	struct smbnode *np = VTOSMB(vp);
233afe09751SDavide Italiano 	struct smb_cred *scred;
234681a5bbeSBoris Popov 
235835fb616STim J. Robbins 	if (vp->v_type == VDIR && (np->n_flag & NOPEN) != 0 &&
236835fb616STim J. Robbins 	    np->n_dirseq != NULL) {
237afe09751SDavide Italiano 		scred = smbfs_malloc_scred();
238afe09751SDavide Italiano 		smb_makescred(scred, td, ap->a_cred);
239afe09751SDavide Italiano 		smbfs_findclose(np->n_dirseq, scred);
240afe09751SDavide Italiano 		smbfs_free_scred(scred);
241835fb616STim J. Robbins 		np->n_dirseq = NULL;
242835fb616STim J. Robbins 	}
2432a4ad258STim J. Robbins 	return 0;
244681a5bbeSBoris Popov }
245681a5bbeSBoris Popov 
246681a5bbeSBoris Popov /*
247681a5bbeSBoris Popov  * smbfs_getattr call from vfs.
248681a5bbeSBoris Popov  */
249681a5bbeSBoris Popov static int
250681a5bbeSBoris Popov smbfs_getattr(ap)
251681a5bbeSBoris Popov 	struct vop_getattr_args /* {
252681a5bbeSBoris Popov 		struct vnode *a_vp;
253681a5bbeSBoris Popov 		struct vattr *a_vap;
254681a5bbeSBoris Popov 		struct ucred *a_cred;
255681a5bbeSBoris Popov 	} */ *ap;
256681a5bbeSBoris Popov {
257681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
258681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
259681a5bbeSBoris Popov 	struct vattr *va=ap->a_vap;
260681a5bbeSBoris Popov 	struct smbfattr fattr;
261afe09751SDavide Italiano 	struct smb_cred *scred;
26207a65634STim J. Robbins 	u_quad_t oldsize;
263681a5bbeSBoris Popov 	int error;
264681a5bbeSBoris Popov 
265e6e370a7SJeff Roberson 	SMBVDEBUG("%lx: '%s' %d\n", (long)vp, np->n_name, (vp->v_vflag & VV_ROOT) != 0);
266681a5bbeSBoris Popov 	error = smbfs_attr_cachelookup(vp, va);
267681a5bbeSBoris Popov 	if (!error)
268681a5bbeSBoris Popov 		return 0;
269681a5bbeSBoris Popov 	SMBVDEBUG("not in the cache\n");
270afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
271afe09751SDavide Italiano 	smb_makescred(scred, curthread, ap->a_cred);
272681a5bbeSBoris Popov 	oldsize = np->n_size;
273afe09751SDavide Italiano 	error = smbfs_smb_lookup(np, NULL, 0, &fattr, scred);
274681a5bbeSBoris Popov 	if (error) {
275681a5bbeSBoris Popov 		SMBVDEBUG("error %d\n", error);
276afe09751SDavide Italiano 		smbfs_free_scred(scred);
277681a5bbeSBoris Popov 		return error;
278681a5bbeSBoris Popov 	}
279681a5bbeSBoris Popov 	smbfs_attr_cacheenter(vp, &fattr);
280681a5bbeSBoris Popov 	smbfs_attr_cachelookup(vp, va);
2812a4ad258STim J. Robbins 	if (np->n_flag & NOPEN)
282681a5bbeSBoris Popov 		np->n_size = oldsize;
283afe09751SDavide Italiano 		smbfs_free_scred(scred);
284681a5bbeSBoris Popov 	return 0;
285681a5bbeSBoris Popov }
286681a5bbeSBoris Popov 
287681a5bbeSBoris Popov static int
288681a5bbeSBoris Popov smbfs_setattr(ap)
289681a5bbeSBoris Popov 	struct vop_setattr_args /* {
290681a5bbeSBoris Popov 		struct vnode *a_vp;
291681a5bbeSBoris Popov 		struct vattr *a_vap;
292681a5bbeSBoris Popov 		struct ucred *a_cred;
293681a5bbeSBoris Popov 	} */ *ap;
294681a5bbeSBoris Popov {
295681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
296681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
297681a5bbeSBoris Popov 	struct vattr *vap = ap->a_vap;
298681a5bbeSBoris Popov 	struct timespec *mtime, *atime;
299afe09751SDavide Italiano 	struct smb_cred *scred;
300681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
301681a5bbeSBoris Popov 	struct smb_vc *vcp = SSTOVC(ssp);
3020359a12eSAttilio Rao 	struct thread *td = curthread;
303681a5bbeSBoris Popov 	u_quad_t tsize = 0;
304681a5bbeSBoris Popov 	int isreadonly, doclose, error = 0;
305fb8e9eadSBoris Popov 	int old_n_dosattr;
306681a5bbeSBoris Popov 
307681a5bbeSBoris Popov 	SMBVDEBUG("\n");
308681a5bbeSBoris Popov 	if (vap->va_flags != VNOVAL)
309681a5bbeSBoris Popov 		return EOPNOTSUPP;
310681a5bbeSBoris Popov 	isreadonly = (vp->v_mount->mnt_flag & MNT_RDONLY);
311681a5bbeSBoris Popov 	/*
312681a5bbeSBoris Popov 	 * Disallow write attempts if the filesystem is mounted read-only.
313681a5bbeSBoris Popov 	 */
314681a5bbeSBoris Popov   	if ((vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL ||
315681a5bbeSBoris Popov 	     vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
316681a5bbeSBoris Popov 	     vap->va_mode != (mode_t)VNOVAL) && isreadonly)
317681a5bbeSBoris Popov 		return EROFS;
318afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
319afe09751SDavide Italiano 	smb_makescred(scred, td, ap->a_cred);
320681a5bbeSBoris Popov 	if (vap->va_size != VNOVAL) {
321681a5bbeSBoris Popov  		switch (vp->v_type) {
322681a5bbeSBoris Popov  		    case VDIR:
323afe09751SDavide Italiano  			error = EISDIR;
324afe09751SDavide Italiano 			goto out;
325681a5bbeSBoris Popov  		    case VREG:
326681a5bbeSBoris Popov 			break;
327681a5bbeSBoris Popov  		    default:
328afe09751SDavide Italiano 			error = EINVAL;
329afe09751SDavide Italiano 			goto out;
330681a5bbeSBoris Popov   		};
331afe09751SDavide Italiano 		if (isreadonly) {
332afe09751SDavide Italiano 			error = EROFS;
333afe09751SDavide Italiano 			goto out;
334afe09751SDavide Italiano 		}
335681a5bbeSBoris Popov 		doclose = 0;
336681a5bbeSBoris Popov 		vnode_pager_setsize(vp, (u_long)vap->va_size);
337681a5bbeSBoris Popov  		tsize = np->n_size;
338681a5bbeSBoris Popov  		np->n_size = vap->va_size;
3392a4ad258STim J. Robbins 		if ((np->n_flag & NOPEN) == 0) {
34044f3878eSBoris Popov 			error = smbfs_smb_open(np,
34144f3878eSBoris Popov 					       SMB_SM_DENYNONE|SMB_AM_OPENRW,
342afe09751SDavide Italiano 					       scred);
343681a5bbeSBoris Popov 			if (error == 0)
344681a5bbeSBoris Popov 				doclose = 1;
345681a5bbeSBoris Popov 		}
346681a5bbeSBoris Popov 		if (error == 0)
347afe09751SDavide Italiano 			error = smbfs_smb_setfsize(np, vap->va_size, scred);
348681a5bbeSBoris Popov 		if (doclose)
349afe09751SDavide Italiano 			smbfs_smb_close(ssp, np->n_fid, NULL, scred);
350681a5bbeSBoris Popov 		if (error) {
351681a5bbeSBoris Popov 			np->n_size = tsize;
352681a5bbeSBoris Popov 			vnode_pager_setsize(vp, (u_long)tsize);
353afe09751SDavide Italiano 			goto out;
354681a5bbeSBoris Popov 		}
355681a5bbeSBoris Popov   	}
356fb8e9eadSBoris Popov 	if (vap->va_mode != (mode_t)VNOVAL) {
357fb8e9eadSBoris Popov 		old_n_dosattr = np->n_dosattr;
358fb8e9eadSBoris Popov 		if (vap->va_mode & S_IWUSR)
359fb8e9eadSBoris Popov 			np->n_dosattr &= ~SMB_FA_RDONLY;
360fb8e9eadSBoris Popov 		else
361fb8e9eadSBoris Popov 			np->n_dosattr |= SMB_FA_RDONLY;
362fb8e9eadSBoris Popov 		if (np->n_dosattr != old_n_dosattr) {
363afe09751SDavide Italiano 			error = smbfs_smb_setpattr(np, np->n_dosattr, NULL, scred);
364fb8e9eadSBoris Popov 			if (error)
365afe09751SDavide Italiano 				goto out;
366fb8e9eadSBoris Popov 		}
367fb8e9eadSBoris Popov 	}
368681a5bbeSBoris Popov 	mtime = atime = NULL;
369681a5bbeSBoris Popov 	if (vap->va_mtime.tv_sec != VNOVAL)
370681a5bbeSBoris Popov 		mtime = &vap->va_mtime;
371681a5bbeSBoris Popov 	if (vap->va_atime.tv_sec != VNOVAL)
372681a5bbeSBoris Popov 		atime = &vap->va_atime;
373681a5bbeSBoris Popov 	if (mtime != atime) {
374acd3428bSRobert Watson 		if (vap->va_vaflags & VA_UTIMES_NULL) {
3750359a12eSAttilio Rao 			error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td);
376acd3428bSRobert Watson 			if (error)
3770359a12eSAttilio Rao 				error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td);
378acd3428bSRobert Watson 		} else
3790359a12eSAttilio Rao 			error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td);
380681a5bbeSBoris Popov #if 0
381681a5bbeSBoris Popov 		if (mtime == NULL)
382681a5bbeSBoris Popov 			mtime = &np->n_mtime;
383681a5bbeSBoris Popov 		if (atime == NULL)
384681a5bbeSBoris Popov 			atime = &np->n_atime;
385681a5bbeSBoris Popov #endif
386681a5bbeSBoris Popov 		/*
387681a5bbeSBoris Popov 		 * If file is opened, then we can use handle based calls.
388681a5bbeSBoris Popov 		 * If not, use path based ones.
389681a5bbeSBoris Popov 		 */
3902a4ad258STim J. Robbins 		if ((np->n_flag & NOPEN) == 0) {
391681a5bbeSBoris Popov 			if (vcp->vc_flags & SMBV_WIN95) {
3920359a12eSAttilio Rao 				error = VOP_OPEN(vp, FWRITE, ap->a_cred, td,
3930359a12eSAttilio Rao 				    NULL);
394681a5bbeSBoris Popov 				if (!error) {
3950359a12eSAttilio Rao /*					error = smbfs_smb_setfattrNT(np, 0,
396afe09751SDavide Italiano 					    mtime, atime, scred);
3970359a12eSAttilio Rao 					VOP_GETATTR(vp, &vattr, ap->a_cred); */
398681a5bbeSBoris Popov 					if (mtime)
399681a5bbeSBoris Popov 						np->n_mtime = *mtime;
4000359a12eSAttilio Rao 					VOP_CLOSE(vp, FWRITE, ap->a_cred, td);
401681a5bbeSBoris Popov 				}
402681a5bbeSBoris Popov 			} else if ((vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS)) {
403afe09751SDavide Italiano 				error = smbfs_smb_setptime2(np, mtime, atime, 0, scred);
404afe09751SDavide Italiano /*				error = smbfs_smb_setpattrNT(np, 0, mtime, atime, scred);*/
405681a5bbeSBoris Popov 			} else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN2_0) {
406afe09751SDavide Italiano 				error = smbfs_smb_setptime2(np, mtime, atime, 0, scred);
407681a5bbeSBoris Popov 			} else {
408afe09751SDavide Italiano 				error = smbfs_smb_setpattr(np, 0, mtime, scred);
409681a5bbeSBoris Popov 			}
410681a5bbeSBoris Popov 		} else {
411681a5bbeSBoris Popov 			if (vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS) {
412afe09751SDavide Italiano 				error = smbfs_smb_setfattrNT(np, 0, mtime, atime, scred);
413681a5bbeSBoris Popov 			} else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN1_0) {
414afe09751SDavide Italiano 				error = smbfs_smb_setftime(np, mtime, atime, scred);
415681a5bbeSBoris Popov 			} else {
416681a5bbeSBoris Popov 				/*
417681a5bbeSBoris Popov 				 * I have no idea how to handle this for core
418681a5bbeSBoris Popov 				 * level servers. The possible solution is to
419681a5bbeSBoris Popov 				 * update mtime after file is closed.
420681a5bbeSBoris Popov 				 */
421681a5bbeSBoris Popov 				 SMBERROR("can't update times on an opened file\n");
422681a5bbeSBoris Popov 			}
423681a5bbeSBoris Popov 		}
424681a5bbeSBoris Popov 	}
425681a5bbeSBoris Popov 	/*
426681a5bbeSBoris Popov 	 * Invalidate attribute cache in case if server doesn't set
427681a5bbeSBoris Popov 	 * required attributes.
428681a5bbeSBoris Popov 	 */
429681a5bbeSBoris Popov 	smbfs_attr_cacheremove(vp);	/* invalidate cache */
4300359a12eSAttilio Rao 	VOP_GETATTR(vp, vap, ap->a_cred);
431681a5bbeSBoris Popov 	np->n_mtime.tv_sec = vap->va_mtime.tv_sec;
432afe09751SDavide Italiano out:
433afe09751SDavide Italiano 	smbfs_free_scred(scred);
434681a5bbeSBoris Popov 	return error;
435681a5bbeSBoris Popov }
436681a5bbeSBoris Popov /*
437681a5bbeSBoris Popov  * smbfs_read call.
438681a5bbeSBoris Popov  */
439681a5bbeSBoris Popov static int
440681a5bbeSBoris Popov smbfs_read(ap)
441681a5bbeSBoris Popov 	struct vop_read_args /* {
442681a5bbeSBoris Popov 		struct vnode *a_vp;
443681a5bbeSBoris Popov 		struct uio *a_uio;
444681a5bbeSBoris Popov 		int  a_ioflag;
445681a5bbeSBoris Popov 		struct ucred *a_cred;
446681a5bbeSBoris Popov 	} */ *ap;
447681a5bbeSBoris Popov {
448681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
449681a5bbeSBoris Popov 	struct uio *uio = ap->a_uio;
450681a5bbeSBoris Popov 
451681a5bbeSBoris Popov 	SMBVDEBUG("\n");
452681a5bbeSBoris Popov 	if (vp->v_type != VREG && vp->v_type != VDIR)
453681a5bbeSBoris Popov 		return EPERM;
454681a5bbeSBoris Popov 	return smbfs_readvnode(vp, uio, ap->a_cred);
455681a5bbeSBoris Popov }
456681a5bbeSBoris Popov 
457681a5bbeSBoris Popov static int
458681a5bbeSBoris Popov smbfs_write(ap)
459681a5bbeSBoris Popov 	struct vop_write_args /* {
460681a5bbeSBoris Popov 		struct vnode *a_vp;
461681a5bbeSBoris Popov 		struct uio *a_uio;
462681a5bbeSBoris Popov 		int  a_ioflag;
463681a5bbeSBoris Popov 		struct ucred *a_cred;
464681a5bbeSBoris Popov 	} */ *ap;
465681a5bbeSBoris Popov {
466681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
467681a5bbeSBoris Popov 	struct uio *uio = ap->a_uio;
468681a5bbeSBoris Popov 
469994f027fSDavide Italiano 	SMBVDEBUG("%d,ofs=%jd,sz=%zd\n",vp->v_type, (intmax_t)uio->uio_offset,
470469cb18fSDavide Italiano 	    uio->uio_resid);
471681a5bbeSBoris Popov 	if (vp->v_type != VREG)
472681a5bbeSBoris Popov 		return (EPERM);
473681a5bbeSBoris Popov 	return smbfs_writevnode(vp, uio, ap->a_cred,ap->a_ioflag);
474681a5bbeSBoris Popov }
475681a5bbeSBoris Popov /*
476681a5bbeSBoris Popov  * smbfs_create call
477681a5bbeSBoris Popov  * Create a regular file. On entry the directory to contain the file being
478681a5bbeSBoris Popov  * created is locked.  We must release before we return. We must also free
479681a5bbeSBoris Popov  * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
480681a5bbeSBoris Popov  * only if the SAVESTART bit in cn_flags is clear on success.
481681a5bbeSBoris Popov  */
482681a5bbeSBoris Popov static int
483681a5bbeSBoris Popov smbfs_create(ap)
484681a5bbeSBoris Popov 	struct vop_create_args /* {
485681a5bbeSBoris Popov 		struct vnode *a_dvp;
486681a5bbeSBoris Popov 		struct vnode **a_vpp;
487681a5bbeSBoris Popov 		struct componentname *a_cnp;
488681a5bbeSBoris Popov 		struct vattr *a_vap;
489681a5bbeSBoris Popov 	} */ *ap;
490681a5bbeSBoris Popov {
491681a5bbeSBoris Popov 	struct vnode *dvp = ap->a_dvp;
492681a5bbeSBoris Popov 	struct vattr *vap = ap->a_vap;
493681a5bbeSBoris Popov 	struct vnode **vpp=ap->a_vpp;
494681a5bbeSBoris Popov 	struct componentname *cnp = ap->a_cnp;
495681a5bbeSBoris Popov 	struct smbnode *dnp = VTOSMB(dvp);
496681a5bbeSBoris Popov 	struct vnode *vp;
497681a5bbeSBoris Popov 	struct vattr vattr;
498681a5bbeSBoris Popov 	struct smbfattr fattr;
499afe09751SDavide Italiano 	struct smb_cred *scred;
500681a5bbeSBoris Popov 	char *name = cnp->cn_nameptr;
501681a5bbeSBoris Popov 	int nmlen = cnp->cn_namelen;
502681a5bbeSBoris Popov 	int error;
503681a5bbeSBoris Popov 
504681a5bbeSBoris Popov 
505681a5bbeSBoris Popov 	SMBVDEBUG("\n");
506681a5bbeSBoris Popov 	*vpp = NULL;
507681a5bbeSBoris Popov 	if (vap->va_type != VREG)
508681a5bbeSBoris Popov 		return EOPNOTSUPP;
5090359a12eSAttilio Rao 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
510681a5bbeSBoris Popov 		return error;
511afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
512afe09751SDavide Italiano 	smb_makescred(scred, cnp->cn_thread, cnp->cn_cred);
513681a5bbeSBoris Popov 
514afe09751SDavide Italiano 	error = smbfs_smb_create(dnp, name, nmlen, scred);
515681a5bbeSBoris Popov 	if (error)
516afe09751SDavide Italiano 		goto out;
517afe09751SDavide Italiano 	error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, scred);
518681a5bbeSBoris Popov 	if (error)
519afe09751SDavide Italiano 		goto out;
520681a5bbeSBoris Popov 	error = smbfs_nget(VTOVFS(dvp), dvp, name, nmlen, &fattr, &vp);
521681a5bbeSBoris Popov 	if (error)
522afe09751SDavide Italiano 		goto out;
523681a5bbeSBoris Popov 	*vpp = vp;
524681a5bbeSBoris Popov 	if (cnp->cn_flags & MAKEENTRY)
525681a5bbeSBoris Popov 		cache_enter(dvp, vp, cnp);
526afe09751SDavide Italiano out:
527afe09751SDavide Italiano 	smbfs_free_scred(scred);
528681a5bbeSBoris Popov 	return error;
529681a5bbeSBoris Popov }
530681a5bbeSBoris Popov 
531681a5bbeSBoris Popov static int
532681a5bbeSBoris Popov smbfs_remove(ap)
533681a5bbeSBoris Popov 	struct vop_remove_args /* {
534681a5bbeSBoris Popov 		struct vnodeop_desc *a_desc;
535681a5bbeSBoris Popov 		struct vnode * a_dvp;
536681a5bbeSBoris Popov 		struct vnode * a_vp;
537681a5bbeSBoris Popov 		struct componentname * a_cnp;
538681a5bbeSBoris Popov 	} */ *ap;
539681a5bbeSBoris Popov {
540681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
541681a5bbeSBoris Popov /*	struct vnode *dvp = ap->a_dvp;*/
542681a5bbeSBoris Popov 	struct componentname *cnp = ap->a_cnp;
543681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
544afe09751SDavide Italiano 	struct smb_cred *scred;
545681a5bbeSBoris Popov 	int error;
546681a5bbeSBoris Popov 
5472a4ad258STim J. Robbins 	if (vp->v_type == VDIR || (np->n_flag & NOPEN) != 0 || vrefcnt(vp) != 1)
548681a5bbeSBoris Popov 		return EPERM;
549afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
550afe09751SDavide Italiano 	smb_makescred(scred, cnp->cn_thread, cnp->cn_cred);
551afe09751SDavide Italiano 	error = smbfs_smb_delete(np, scred);
552b4484bf0STim J. Robbins 	if (error == 0)
553b4484bf0STim J. Robbins 		np->n_flag |= NGONE;
554681a5bbeSBoris Popov 	cache_purge(vp);
555afe09751SDavide Italiano 	smbfs_free_scred(scred);
556681a5bbeSBoris Popov 	return error;
557681a5bbeSBoris Popov }
558681a5bbeSBoris Popov 
559681a5bbeSBoris Popov /*
560681a5bbeSBoris Popov  * smbfs_file rename call
561681a5bbeSBoris Popov  */
562681a5bbeSBoris Popov static int
563681a5bbeSBoris Popov smbfs_rename(ap)
564681a5bbeSBoris Popov 	struct vop_rename_args  /* {
565681a5bbeSBoris Popov 		struct vnode *a_fdvp;
566681a5bbeSBoris Popov 		struct vnode *a_fvp;
567681a5bbeSBoris Popov 		struct componentname *a_fcnp;
568681a5bbeSBoris Popov 		struct vnode *a_tdvp;
569681a5bbeSBoris Popov 		struct vnode *a_tvp;
570681a5bbeSBoris Popov 		struct componentname *a_tcnp;
571681a5bbeSBoris Popov 	} */ *ap;
572681a5bbeSBoris Popov {
573681a5bbeSBoris Popov 	struct vnode *fvp = ap->a_fvp;
574681a5bbeSBoris Popov 	struct vnode *tvp = ap->a_tvp;
575681a5bbeSBoris Popov 	struct vnode *fdvp = ap->a_fdvp;
576681a5bbeSBoris Popov 	struct vnode *tdvp = ap->a_tdvp;
577681a5bbeSBoris Popov 	struct componentname *tcnp = ap->a_tcnp;
578681a5bbeSBoris Popov /*	struct componentname *fcnp = ap->a_fcnp;*/
579afe09751SDavide Italiano 	struct smb_cred *scred;
580681a5bbeSBoris Popov 	u_int16_t flags = 6;
581681a5bbeSBoris Popov 	int error=0;
582681a5bbeSBoris Popov 
583e346bd81SDavide Italiano 	scred = NULL;
584681a5bbeSBoris Popov 	/* Check for cross-device rename */
585681a5bbeSBoris Popov 	if ((fvp->v_mount != tdvp->v_mount) ||
586681a5bbeSBoris Popov 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
58742039c5bSDavide Italiano 		error = EXDEV;
588681a5bbeSBoris Popov 		goto out;
589681a5bbeSBoris Popov 	}
590681a5bbeSBoris Popov 
5914d93c0beSJeff Roberson 	if (tvp && vrefcnt(tvp) > 1) {
59242039c5bSDavide Italiano 		error = EBUSY;
593681a5bbeSBoris Popov 		goto out;
594681a5bbeSBoris Popov 	}
595681a5bbeSBoris Popov 	flags = 0x10;			/* verify all writes */
596681a5bbeSBoris Popov 	if (fvp->v_type == VDIR) {
597681a5bbeSBoris Popov 		flags |= 2;
598681a5bbeSBoris Popov 	} else if (fvp->v_type == VREG) {
599681a5bbeSBoris Popov 		flags |= 1;
6006dae0c1eSTim J. Robbins 	} else {
601afe09751SDavide Italiano 		return EINVAL;
6026dae0c1eSTim J. Robbins 	}
603afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
604afe09751SDavide Italiano 	smb_makescred(scred, tcnp->cn_thread, tcnp->cn_cred);
605681a5bbeSBoris Popov 	/*
606681a5bbeSBoris Popov 	 * It seems that Samba doesn't implement SMB_COM_MOVE call...
607681a5bbeSBoris Popov 	 */
608681a5bbeSBoris Popov #ifdef notnow
609681a5bbeSBoris Popov 	if (SMB_DIALECT(SSTOCN(smp->sm_share)) >= SMB_DIALECT_LANMAN1_0) {
610681a5bbeSBoris Popov 		error = smbfs_smb_move(VTOSMB(fvp), VTOSMB(tdvp),
611afe09751SDavide Italiano 		    tcnp->cn_nameptr, tcnp->cn_namelen, flags, scred);
612681a5bbeSBoris Popov 	} else
613681a5bbeSBoris Popov #endif
614681a5bbeSBoris Popov 	{
615681a5bbeSBoris Popov 		/*
616681a5bbeSBoris Popov 		 * We have to do the work atomicaly
617681a5bbeSBoris Popov 		 */
618681a5bbeSBoris Popov 		if (tvp && tvp != fvp) {
619afe09751SDavide Italiano 			error = smbfs_smb_delete(VTOSMB(tvp), scred);
620681a5bbeSBoris Popov 			if (error)
6216dae0c1eSTim J. Robbins 				goto out_cacherem;
622b4484bf0STim J. Robbins 			VTOSMB(fvp)->n_flag |= NGONE;
623681a5bbeSBoris Popov 		}
624681a5bbeSBoris Popov 		error = smbfs_smb_rename(VTOSMB(fvp), VTOSMB(tdvp),
625afe09751SDavide Italiano 		    tcnp->cn_nameptr, tcnp->cn_namelen, scred);
626681a5bbeSBoris Popov 	}
627681a5bbeSBoris Popov 
628681a5bbeSBoris Popov 	if (fvp->v_type == VDIR) {
629681a5bbeSBoris Popov 		if (tvp != NULL && tvp->v_type == VDIR)
630681a5bbeSBoris Popov 			cache_purge(tdvp);
631681a5bbeSBoris Popov 		cache_purge(fdvp);
632681a5bbeSBoris Popov 	}
6336dae0c1eSTim J. Robbins 
6346dae0c1eSTim J. Robbins out_cacherem:
6356dae0c1eSTim J. Robbins 	smbfs_attr_cacheremove(fdvp);
6366dae0c1eSTim J. Robbins 	smbfs_attr_cacheremove(tdvp);
637681a5bbeSBoris Popov out:
638afe09751SDavide Italiano 	smbfs_free_scred(scred);
639681a5bbeSBoris Popov 	if (tdvp == tvp)
640681a5bbeSBoris Popov 		vrele(tdvp);
641681a5bbeSBoris Popov 	else
642681a5bbeSBoris Popov 		vput(tdvp);
643681a5bbeSBoris Popov 	if (tvp)
644681a5bbeSBoris Popov 		vput(tvp);
645681a5bbeSBoris Popov 	vrele(fdvp);
646681a5bbeSBoris Popov 	vrele(fvp);
647681a5bbeSBoris Popov #ifdef possible_mistake
648681a5bbeSBoris Popov 	vgone(fvp);
649681a5bbeSBoris Popov 	if (tvp)
650681a5bbeSBoris Popov 		vgone(tvp);
651681a5bbeSBoris Popov #endif
652681a5bbeSBoris Popov 	return error;
653681a5bbeSBoris Popov }
654681a5bbeSBoris Popov 
6558e67c454STim J. Robbins /*
6568e67c454STim J. Robbins  * somtime it will come true...
6578e67c454STim J. Robbins  */
6588e67c454STim J. Robbins static int
6598e67c454STim J. Robbins smbfs_link(ap)
6608e67c454STim J. Robbins 	struct vop_link_args /* {
6618e67c454STim J. Robbins 		struct vnode *a_tdvp;
6628e67c454STim J. Robbins 		struct vnode *a_vp;
6638e67c454STim J. Robbins 		struct componentname *a_cnp;
6648e67c454STim J. Robbins 	} */ *ap;
6658e67c454STim J. Robbins {
6668e67c454STim J. Robbins 	return EOPNOTSUPP;
6678e67c454STim J. Robbins }
6688e67c454STim J. Robbins 
6698e67c454STim J. Robbins /*
6708e67c454STim J. Robbins  * smbfs_symlink link create call.
6718e67c454STim J. Robbins  * Sometime it will be functional...
6728e67c454STim J. Robbins  */
6738e67c454STim J. Robbins static int
6748e67c454STim J. Robbins smbfs_symlink(ap)
6758e67c454STim J. Robbins 	struct vop_symlink_args /* {
6768e67c454STim J. Robbins 		struct vnode *a_dvp;
6778e67c454STim J. Robbins 		struct vnode **a_vpp;
6788e67c454STim J. Robbins 		struct componentname *a_cnp;
6798e67c454STim J. Robbins 		struct vattr *a_vap;
6808e67c454STim J. Robbins 		char *a_target;
6818e67c454STim J. Robbins 	} */ *ap;
6828e67c454STim J. Robbins {
6838e67c454STim J. Robbins 	return EOPNOTSUPP;
6848e67c454STim J. Robbins }
6858e67c454STim J. Robbins 
6868e67c454STim J. Robbins static int
6878e67c454STim J. Robbins smbfs_mknod(ap)
6888e67c454STim J. Robbins 	struct vop_mknod_args /* {
6898e67c454STim J. Robbins 	} */ *ap;
6908e67c454STim J. Robbins {
6918e67c454STim J. Robbins 	return EOPNOTSUPP;
6928e67c454STim J. Robbins }
6938e67c454STim J. Robbins 
694681a5bbeSBoris Popov static int
695681a5bbeSBoris Popov smbfs_mkdir(ap)
696681a5bbeSBoris Popov 	struct vop_mkdir_args /* {
697681a5bbeSBoris Popov 		struct vnode *a_dvp;
698681a5bbeSBoris Popov 		struct vnode **a_vpp;
699681a5bbeSBoris Popov 		struct componentname *a_cnp;
700681a5bbeSBoris Popov 		struct vattr *a_vap;
701681a5bbeSBoris Popov 	} */ *ap;
702681a5bbeSBoris Popov {
703681a5bbeSBoris Popov 	struct vnode *dvp = ap->a_dvp;
704681a5bbeSBoris Popov /*	struct vattr *vap = ap->a_vap;*/
705681a5bbeSBoris Popov 	struct vnode *vp;
706681a5bbeSBoris Popov 	struct componentname *cnp = ap->a_cnp;
707681a5bbeSBoris Popov 	struct smbnode *dnp = VTOSMB(dvp);
708681a5bbeSBoris Popov 	struct vattr vattr;
709afe09751SDavide Italiano 	struct smb_cred *scred;
710681a5bbeSBoris Popov 	struct smbfattr fattr;
711681a5bbeSBoris Popov 	char *name = cnp->cn_nameptr;
712681a5bbeSBoris Popov 	int len = cnp->cn_namelen;
713681a5bbeSBoris Popov 	int error;
714681a5bbeSBoris Popov 
7150359a12eSAttilio Rao 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred))) {
716681a5bbeSBoris Popov 		return error;
717681a5bbeSBoris Popov 	}
718681a5bbeSBoris Popov 	if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.'))))
719681a5bbeSBoris Popov 		return EEXIST;
720afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
721afe09751SDavide Italiano 	smb_makescred(scred, cnp->cn_thread, cnp->cn_cred);
722afe09751SDavide Italiano 	error = smbfs_smb_mkdir(dnp, name, len, scred);
723681a5bbeSBoris Popov 	if (error)
724afe09751SDavide Italiano 		goto out;
725afe09751SDavide Italiano 	error = smbfs_smb_lookup(dnp, name, len, &fattr, scred);
726681a5bbeSBoris Popov 	if (error)
727afe09751SDavide Italiano 		goto out;
728681a5bbeSBoris Popov 	error = smbfs_nget(VTOVFS(dvp), dvp, name, len, &fattr, &vp);
729681a5bbeSBoris Popov 	if (error)
730afe09751SDavide Italiano 		goto out;
731681a5bbeSBoris Popov 	*ap->a_vpp = vp;
732afe09751SDavide Italiano out:
733afe09751SDavide Italiano 	smbfs_free_scred(scred);
734e346bd81SDavide Italiano 	return error;
735681a5bbeSBoris Popov }
736681a5bbeSBoris Popov 
737681a5bbeSBoris Popov /*
738681a5bbeSBoris Popov  * smbfs_remove directory call
739681a5bbeSBoris Popov  */
740681a5bbeSBoris Popov static int
741681a5bbeSBoris Popov smbfs_rmdir(ap)
742681a5bbeSBoris Popov 	struct vop_rmdir_args /* {
743681a5bbeSBoris Popov 		struct vnode *a_dvp;
744681a5bbeSBoris Popov 		struct vnode *a_vp;
745681a5bbeSBoris Popov 		struct componentname *a_cnp;
746681a5bbeSBoris Popov 	} */ *ap;
747681a5bbeSBoris Popov {
748681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
749681a5bbeSBoris Popov 	struct vnode *dvp = ap->a_dvp;
750681a5bbeSBoris Popov 	struct componentname *cnp = ap->a_cnp;
751681a5bbeSBoris Popov /*	struct smbmount *smp = VTOSMBFS(vp);*/
752681a5bbeSBoris Popov 	struct smbnode *dnp = VTOSMB(dvp);
753681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
754afe09751SDavide Italiano 	struct smb_cred *scred;
755681a5bbeSBoris Popov 	int error;
756681a5bbeSBoris Popov 
757681a5bbeSBoris Popov 	if (dvp == vp)
758681a5bbeSBoris Popov 		return EINVAL;
759681a5bbeSBoris Popov 
760afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
761afe09751SDavide Italiano 	smb_makescred(scred, cnp->cn_thread, cnp->cn_cred);
762afe09751SDavide Italiano 	error = smbfs_smb_rmdir(np, scred);
763b4484bf0STim J. Robbins 	if (error == 0)
764b4484bf0STim J. Robbins 		np->n_flag |= NGONE;
765681a5bbeSBoris Popov 	dnp->n_flag |= NMODIFIED;
766681a5bbeSBoris Popov 	smbfs_attr_cacheremove(dvp);
767681a5bbeSBoris Popov /*	cache_purge(dvp);*/
768681a5bbeSBoris Popov 	cache_purge(vp);
769afe09751SDavide Italiano 	smbfs_free_scred(scred);
770681a5bbeSBoris Popov 	return error;
771681a5bbeSBoris Popov }
772681a5bbeSBoris Popov 
773681a5bbeSBoris Popov /*
774681a5bbeSBoris Popov  * smbfs_readdir call
775681a5bbeSBoris Popov  */
776681a5bbeSBoris Popov static int
777681a5bbeSBoris Popov smbfs_readdir(ap)
778681a5bbeSBoris Popov 	struct vop_readdir_args /* {
779681a5bbeSBoris Popov 		struct vnode *a_vp;
780681a5bbeSBoris Popov 		struct uio *a_uio;
781681a5bbeSBoris Popov 		struct ucred *a_cred;
782681a5bbeSBoris Popov 		int *a_eofflag;
783681a5bbeSBoris Popov 		u_long *a_cookies;
784681a5bbeSBoris Popov 		int a_ncookies;
785681a5bbeSBoris Popov 	} */ *ap;
786681a5bbeSBoris Popov {
787681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
788681a5bbeSBoris Popov 	struct uio *uio = ap->a_uio;
789681a5bbeSBoris Popov 	int error;
790681a5bbeSBoris Popov 
791681a5bbeSBoris Popov 	if (vp->v_type != VDIR)
792681a5bbeSBoris Popov 		return (EPERM);
793681a5bbeSBoris Popov #ifdef notnow
794681a5bbeSBoris Popov 	if (ap->a_ncookies) {
795681a5bbeSBoris Popov 		printf("smbfs_readdir: no support for cookies now...");
796681a5bbeSBoris Popov 		return (EOPNOTSUPP);
797681a5bbeSBoris Popov 	}
798681a5bbeSBoris Popov #endif
799681a5bbeSBoris Popov 	error = smbfs_readvnode(vp, uio, ap->a_cred);
800681a5bbeSBoris Popov 	return error;
801681a5bbeSBoris Popov }
802681a5bbeSBoris Popov 
803681a5bbeSBoris Popov /* ARGSUSED */
804681a5bbeSBoris Popov static int
805681a5bbeSBoris Popov smbfs_fsync(ap)
806681a5bbeSBoris Popov 	struct vop_fsync_args /* {
807681a5bbeSBoris Popov 		struct vnodeop_desc *a_desc;
808681a5bbeSBoris Popov 		struct vnode * a_vp;
809681a5bbeSBoris Popov 		struct ucred * a_cred;
810681a5bbeSBoris Popov 		int  a_waitfor;
811b1c996c4SBoris Popov 		struct thread * a_td;
812681a5bbeSBoris Popov 	} */ *ap;
813681a5bbeSBoris Popov {
814b1c996c4SBoris Popov /*	return (smb_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));*/
815681a5bbeSBoris Popov     return (0);
816681a5bbeSBoris Popov }
817681a5bbeSBoris Popov 
818681a5bbeSBoris Popov static
819681a5bbeSBoris Popov int smbfs_print (ap)
820681a5bbeSBoris Popov 	struct vop_print_args /* {
821681a5bbeSBoris Popov 	struct vnode *a_vp;
822681a5bbeSBoris Popov 	} */ *ap;
823681a5bbeSBoris Popov {
824681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
825681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
826681a5bbeSBoris Popov 
827681a5bbeSBoris Popov 	if (np == NULL) {
828681a5bbeSBoris Popov 		printf("no smbnode data\n");
829681a5bbeSBoris Popov 		return (0);
830681a5bbeSBoris Popov 	}
8312a4ad258STim J. Robbins 	printf("\tname = %s, parent = %p, open = %d\n", np->n_name,
8322a4ad258STim J. Robbins 	    np->n_parent ? np->n_parent : NULL, (np->n_flag & NOPEN) != 0);
833681a5bbeSBoris Popov 	return (0);
834681a5bbeSBoris Popov }
835681a5bbeSBoris Popov 
836681a5bbeSBoris Popov static int
837681a5bbeSBoris Popov smbfs_pathconf (ap)
838681a5bbeSBoris Popov 	struct vop_pathconf_args  /* {
839681a5bbeSBoris Popov 	struct vnode *vp;
840681a5bbeSBoris Popov 	int name;
841681a5bbeSBoris Popov 	register_t *retval;
842681a5bbeSBoris Popov 	} */ *ap;
843681a5bbeSBoris Popov {
844681a5bbeSBoris Popov 	struct smbmount *smp = VFSTOSMBFS(VTOVFS(ap->a_vp));
845681a5bbeSBoris Popov 	struct smb_vc *vcp = SSTOVC(smp->sm_share);
846681a5bbeSBoris Popov 	register_t *retval = ap->a_retval;
847681a5bbeSBoris Popov 	int error = 0;
848681a5bbeSBoris Popov 
849681a5bbeSBoris Popov 	switch (ap->a_name) {
850681a5bbeSBoris Popov 	    case _PC_LINK_MAX:
851681a5bbeSBoris Popov 		*retval = 0;
852681a5bbeSBoris Popov 		break;
853681a5bbeSBoris Popov 	    case _PC_NAME_MAX:
8543419dc99SBoris Popov 		*retval = (vcp->vc_hflags2 & SMB_FLAGS2_KNOWS_LONG_NAMES) ? 255 : 12;
855681a5bbeSBoris Popov 		break;
856681a5bbeSBoris Popov 	    case _PC_PATH_MAX:
857681a5bbeSBoris Popov 		*retval = 800;	/* XXX: a correct one ? */
858681a5bbeSBoris Popov 		break;
859681a5bbeSBoris Popov 	    default:
860681a5bbeSBoris Popov 		error = EINVAL;
861681a5bbeSBoris Popov 	}
862681a5bbeSBoris Popov 	return error;
863681a5bbeSBoris Popov }
864681a5bbeSBoris Popov 
865681a5bbeSBoris Popov static int
866681a5bbeSBoris Popov smbfs_strategy (ap)
867681a5bbeSBoris Popov 	struct vop_strategy_args /* {
868681a5bbeSBoris Popov 	struct buf *a_bp
869681a5bbeSBoris Popov 	} */ *ap;
870681a5bbeSBoris Popov {
871681a5bbeSBoris Popov 	struct buf *bp=ap->a_bp;
872681a5bbeSBoris Popov 	struct ucred *cr;
873b1c996c4SBoris Popov 	struct thread *td;
874681a5bbeSBoris Popov 
875681a5bbeSBoris Popov 	SMBVDEBUG("\n");
876681a5bbeSBoris Popov 	if (bp->b_flags & B_ASYNC)
877b1c996c4SBoris Popov 		td = (struct thread *)0;
878681a5bbeSBoris Popov 	else
879b1c996c4SBoris Popov 		td = curthread;	/* XXX */
880681a5bbeSBoris Popov 	if (bp->b_iocmd == BIO_READ)
881681a5bbeSBoris Popov 		cr = bp->b_rcred;
882681a5bbeSBoris Popov 	else
883681a5bbeSBoris Popov 		cr = bp->b_wcred;
884681a5bbeSBoris Popov 
885681a5bbeSBoris Popov 	if ((bp->b_flags & B_ASYNC) == 0 )
88613fd4d21SBjoern A. Zeeb 		(void)smbfs_doio(ap->a_vp, bp, cr, td);
8870da50f6eSEdward Tomasz Napierala 	return (0);
888681a5bbeSBoris Popov }
889681a5bbeSBoris Popov 
8908e67c454STim J. Robbins int
8918e67c454STim J. Robbins smbfs_ioctl(ap)
8928e67c454STim J. Robbins 	struct vop_ioctl_args /* {
8938e67c454STim J. Robbins 		struct vnode *a_vp;
8948e67c454STim J. Robbins 		u_long a_command;
8958e67c454STim J. Robbins 		caddr_t a_data;
8968e67c454STim J. Robbins 		int fflag;
8978e67c454STim J. Robbins 		struct ucred *cred;
8988e67c454STim J. Robbins 		struct thread *td;
8998e67c454STim J. Robbins 	} */ *ap;
9008e67c454STim J. Robbins {
9018e67c454STim J. Robbins 	return ENOTTY;
9028e67c454STim J. Robbins }
9038e67c454STim J. Robbins 
904681a5bbeSBoris Popov static char smbfs_atl[] = "rhsvda";
905681a5bbeSBoris Popov static int
906681a5bbeSBoris Popov smbfs_getextattr(struct vop_getextattr_args *ap)
907681a5bbeSBoris Popov /* {
908681a5bbeSBoris Popov         IN struct vnode *a_vp;
909681a5bbeSBoris Popov         IN char *a_name;
910681a5bbeSBoris Popov         INOUT struct uio *a_uio;
911681a5bbeSBoris Popov         IN struct ucred *a_cred;
912b1c996c4SBoris Popov         IN struct thread *a_td;
913681a5bbeSBoris Popov };
914681a5bbeSBoris Popov */
915681a5bbeSBoris Popov {
916681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
917b1c996c4SBoris Popov 	struct thread *td = ap->a_td;
918681a5bbeSBoris Popov 	struct ucred *cred = ap->a_cred;
919681a5bbeSBoris Popov 	struct uio *uio = ap->a_uio;
920681a5bbeSBoris Popov 	const char *name = ap->a_name;
921681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
922681a5bbeSBoris Popov 	struct vattr vattr;
923681a5bbeSBoris Popov 	char buf[10];
924681a5bbeSBoris Popov 	int i, attr, error;
925681a5bbeSBoris Popov 
926b1c996c4SBoris Popov 	error = VOP_ACCESS(vp, VREAD, cred, td);
927681a5bbeSBoris Popov 	if (error)
928681a5bbeSBoris Popov 		return error;
9290359a12eSAttilio Rao 	error = VOP_GETATTR(vp, &vattr, cred);
930681a5bbeSBoris Popov 	if (error)
931681a5bbeSBoris Popov 		return error;
932681a5bbeSBoris Popov 	if (strcmp(name, "dosattr") == 0) {
933681a5bbeSBoris Popov 		attr = np->n_dosattr;
934681a5bbeSBoris Popov 		for (i = 0; i < 6; i++, attr >>= 1)
935681a5bbeSBoris Popov 			buf[i] = (attr & 1) ? smbfs_atl[i] : '-';
936681a5bbeSBoris Popov 		buf[i] = 0;
937681a5bbeSBoris Popov 		error = uiomove(buf, i, uio);
938681a5bbeSBoris Popov 
939681a5bbeSBoris Popov 	} else
940681a5bbeSBoris Popov 		error = EINVAL;
941681a5bbeSBoris Popov 	return error;
942681a5bbeSBoris Popov }
943681a5bbeSBoris Popov 
944681a5bbeSBoris Popov /*
945681a5bbeSBoris Popov  * Since we expected to support F_GETLK (and SMB protocol has no such function),
946681a5bbeSBoris Popov  * it is necessary to use lf_advlock(). It would be nice if this function had
947681a5bbeSBoris Popov  * a callback mechanism because it will help to improve a level of consistency.
948681a5bbeSBoris Popov  */
949681a5bbeSBoris Popov int
950681a5bbeSBoris Popov smbfs_advlock(ap)
951681a5bbeSBoris Popov 	struct vop_advlock_args /* {
952681a5bbeSBoris Popov 		struct vnode *a_vp;
953681a5bbeSBoris Popov 		caddr_t  a_id;
954681a5bbeSBoris Popov 		int  a_op;
955681a5bbeSBoris Popov 		struct flock *a_fl;
956681a5bbeSBoris Popov 		int  a_flags;
957681a5bbeSBoris Popov 	} */ *ap;
958681a5bbeSBoris Popov {
959681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
960681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
961681a5bbeSBoris Popov 	struct flock *fl = ap->a_fl;
962681a5bbeSBoris Popov 	caddr_t id = (caddr_t)1 /* ap->a_id */;
963681a5bbeSBoris Popov /*	int flags = ap->a_flags;*/
964b1c996c4SBoris Popov 	struct thread *td = curthread;
965afe09751SDavide Italiano 	struct smb_cred *scred;
96647790174SAndrey A. Chernov 	u_quad_t size;
967fcbe9614SAndrey A. Chernov 	off_t start, end, oadd;
968681a5bbeSBoris Popov 	int error, lkop;
969681a5bbeSBoris Popov 
970681a5bbeSBoris Popov 	if (vp->v_type == VDIR) {
971681a5bbeSBoris Popov 		/*
972681a5bbeSBoris Popov 		 * SMB protocol have no support for directory locking.
973681a5bbeSBoris Popov 		 * Although locks can be processed on local machine, I don't
974681a5bbeSBoris Popov 		 * think that this is a good idea, because some programs
975681a5bbeSBoris Popov 		 * can work wrong assuming directory is locked. So, we just
976681a5bbeSBoris Popov 		 * return 'operation not supported
977681a5bbeSBoris Popov 		 */
978681a5bbeSBoris Popov 		 return EOPNOTSUPP;
979681a5bbeSBoris Popov 	}
980681a5bbeSBoris Popov 	size = np->n_size;
981681a5bbeSBoris Popov 	switch (fl->l_whence) {
98215924778SAndrey A. Chernov 
983681a5bbeSBoris Popov 	case SEEK_SET:
984681a5bbeSBoris Popov 	case SEEK_CUR:
985681a5bbeSBoris Popov 		start = fl->l_start;
986681a5bbeSBoris Popov 		break;
98715924778SAndrey A. Chernov 
988681a5bbeSBoris Popov 	case SEEK_END:
98915924778SAndrey A. Chernov 		if (size > OFF_MAX ||
99015924778SAndrey A. Chernov 		    (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
99147790174SAndrey A. Chernov 			return EOVERFLOW;
99215924778SAndrey A. Chernov 		start = size + fl->l_start;
993bbf6984cSAndrey A. Chernov 		break;
99415924778SAndrey A. Chernov 
995681a5bbeSBoris Popov 	default:
996681a5bbeSBoris Popov 		return EINVAL;
997681a5bbeSBoris Popov 	}
998681a5bbeSBoris Popov 	if (start < 0)
999681a5bbeSBoris Popov 		return EINVAL;
1000e3e2c03dSAndrey A. Chernov 	if (fl->l_len < 0) {
1001ea4313e3SAndrey A. Chernov 		if (start == 0)
1002e3e2c03dSAndrey A. Chernov 			return EINVAL;
1003e3e2c03dSAndrey A. Chernov 		end = start - 1;
1004ea4313e3SAndrey A. Chernov 		start += fl->l_len;
1005ea4313e3SAndrey A. Chernov 		if (start < 0)
1006ea4313e3SAndrey A. Chernov 			return EINVAL;
1007e3e2c03dSAndrey A. Chernov 	} else if (fl->l_len == 0)
1008681a5bbeSBoris Popov 		end = -1;
1009681a5bbeSBoris Popov 	else {
1010fcbe9614SAndrey A. Chernov 		oadd = fl->l_len - 1;
101147790174SAndrey A. Chernov 		if (oadd > OFF_MAX - start)
101247790174SAndrey A. Chernov 			return EOVERFLOW;
101347790174SAndrey A. Chernov 		end = start + oadd;
1014681a5bbeSBoris Popov 	}
1015afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
1016afe09751SDavide Italiano 	smb_makescred(scred, td, td->td_ucred);
1017681a5bbeSBoris Popov 	switch (ap->a_op) {
1018681a5bbeSBoris Popov 	    case F_SETLK:
1019681a5bbeSBoris Popov 		switch (fl->l_type) {
1020681a5bbeSBoris Popov 		    case F_WRLCK:
1021681a5bbeSBoris Popov 			lkop = SMB_LOCK_EXCL;
1022681a5bbeSBoris Popov 			break;
1023681a5bbeSBoris Popov 		    case F_RDLCK:
1024681a5bbeSBoris Popov 			lkop = SMB_LOCK_SHARED;
1025681a5bbeSBoris Popov 			break;
1026681a5bbeSBoris Popov 		    case F_UNLCK:
1027681a5bbeSBoris Popov 			lkop = SMB_LOCK_RELEASE;
1028681a5bbeSBoris Popov 			break;
1029681a5bbeSBoris Popov 		    default:
1030afe09751SDavide Italiano 			smbfs_free_scred(scred);
1031681a5bbeSBoris Popov 			return EINVAL;
1032681a5bbeSBoris Popov 		}
1033eab626f1SKonstantin Belousov 		error = lf_advlock(ap, &vp->v_lockf, size);
1034681a5bbeSBoris Popov 		if (error)
1035681a5bbeSBoris Popov 			break;
1036681a5bbeSBoris Popov 		lkop = SMB_LOCK_EXCL;
1037afe09751SDavide Italiano 		error = smbfs_smb_lock(np, lkop, id, start, end, scred);
1038681a5bbeSBoris Popov 		if (error) {
103918121c17SDoug Rabson 			int oldtype = fl->l_type;
104018121c17SDoug Rabson 			fl->l_type = F_UNLCK;
1041681a5bbeSBoris Popov 			ap->a_op = F_UNLCK;
1042eab626f1SKonstantin Belousov 			lf_advlock(ap, &vp->v_lockf, size);
104318121c17SDoug Rabson 			fl->l_type = oldtype;
1044681a5bbeSBoris Popov 		}
1045681a5bbeSBoris Popov 		break;
1046681a5bbeSBoris Popov 	    case F_UNLCK:
1047eab626f1SKonstantin Belousov 		lf_advlock(ap, &vp->v_lockf, size);
1048afe09751SDavide Italiano 		error = smbfs_smb_lock(np, SMB_LOCK_RELEASE, id, start, end, scred);
1049681a5bbeSBoris Popov 		break;
1050681a5bbeSBoris Popov 	    case F_GETLK:
1051eab626f1SKonstantin Belousov 		error = lf_advlock(ap, &vp->v_lockf, size);
1052681a5bbeSBoris Popov 		break;
1053681a5bbeSBoris Popov 	    default:
1054afe09751SDavide Italiano 		smbfs_free_scred(scred);
1055681a5bbeSBoris Popov 		return EINVAL;
1056681a5bbeSBoris Popov 	}
1057afe09751SDavide Italiano 	smbfs_free_scred(scred);
1058681a5bbeSBoris Popov 	return error;
1059681a5bbeSBoris Popov }
1060681a5bbeSBoris Popov 
1061681a5bbeSBoris Popov static int
1062681a5bbeSBoris Popov smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop)
1063681a5bbeSBoris Popov {
106472f6a0faSColin Percival 	static const char *badchars = "*/:<>;?";
1065aa808a7fSTim J. Robbins 	static const char *badchars83 = " +|,[]=";
1066681a5bbeSBoris Popov 	const char *cp;
1067681a5bbeSBoris Popov 	int i, error;
1068681a5bbeSBoris Popov 
106972f6a0faSColin Percival 	/*
107072f6a0faSColin Percival 	 * Backslash characters, being a path delimiter, are prohibited
107172f6a0faSColin Percival 	 * within a path component even for LOOKUP operations.
107272f6a0faSColin Percival 	 */
1073dc15eac0SEd Schouten 	if (strchr(name, '\\') != NULL)
107472f6a0faSColin Percival 		return ENOENT;
107572f6a0faSColin Percival 
1076681a5bbeSBoris Popov 	if (nameiop == LOOKUP)
1077681a5bbeSBoris Popov 		return 0;
1078681a5bbeSBoris Popov 	error = ENOENT;
1079681a5bbeSBoris Popov 	if (SMB_DIALECT(SSTOVC(smp->sm_share)) < SMB_DIALECT_LANMAN2_0) {
1080681a5bbeSBoris Popov 		/*
1081681a5bbeSBoris Popov 		 * Name should conform 8.3 format
1082681a5bbeSBoris Popov 		 */
1083681a5bbeSBoris Popov 		if (nmlen > 12)
1084681a5bbeSBoris Popov 			return ENAMETOOLONG;
1085dc15eac0SEd Schouten 		cp = strchr(name, '.');
1086681a5bbeSBoris Popov 		if (cp == NULL)
1087681a5bbeSBoris Popov 			return error;
1088681a5bbeSBoris Popov 		if (cp == name || (cp - name) > 8)
1089681a5bbeSBoris Popov 			return error;
1090dc15eac0SEd Schouten 		cp = strchr(cp + 1, '.');
1091681a5bbeSBoris Popov 		if (cp != NULL)
1092681a5bbeSBoris Popov 			return error;
1093681a5bbeSBoris Popov 		for (cp = name, i = 0; i < nmlen; i++, cp++)
1094dc15eac0SEd Schouten 			if (strchr(badchars83, *cp) != NULL)
1095681a5bbeSBoris Popov 				return error;
1096681a5bbeSBoris Popov 	}
1097681a5bbeSBoris Popov 	for (cp = name, i = 0; i < nmlen; i++, cp++)
1098dc15eac0SEd Schouten 		if (strchr(badchars, *cp) != NULL)
1099681a5bbeSBoris Popov 			return error;
1100681a5bbeSBoris Popov 	return 0;
1101681a5bbeSBoris Popov }
1102681a5bbeSBoris Popov 
1103681a5bbeSBoris Popov /*
1104681a5bbeSBoris Popov  * Things go even weird without fixed inode numbers...
1105681a5bbeSBoris Popov  */
1106681a5bbeSBoris Popov int
1107681a5bbeSBoris Popov smbfs_lookup(ap)
1108681a5bbeSBoris Popov 	struct vop_lookup_args /* {
1109681a5bbeSBoris Popov 		struct vnodeop_desc *a_desc;
1110681a5bbeSBoris Popov 		struct vnode *a_dvp;
1111681a5bbeSBoris Popov 		struct vnode **a_vpp;
1112681a5bbeSBoris Popov 		struct componentname *a_cnp;
1113681a5bbeSBoris Popov 	} */ *ap;
1114681a5bbeSBoris Popov {
1115681a5bbeSBoris Popov 	struct componentname *cnp = ap->a_cnp;
1116b1c996c4SBoris Popov 	struct thread *td = cnp->cn_thread;
1117681a5bbeSBoris Popov 	struct vnode *dvp = ap->a_dvp;
1118681a5bbeSBoris Popov 	struct vnode **vpp = ap->a_vpp;
1119681a5bbeSBoris Popov 	struct vnode *vp;
1120681a5bbeSBoris Popov 	struct smbmount *smp;
1121681a5bbeSBoris Popov 	struct mount *mp = dvp->v_mount;
1122681a5bbeSBoris Popov 	struct smbnode *dnp;
1123681a5bbeSBoris Popov 	struct smbfattr fattr, *fap;
1124afe09751SDavide Italiano 	struct smb_cred *scred;
1125681a5bbeSBoris Popov 	char *name = cnp->cn_nameptr;
1126681a5bbeSBoris Popov 	int flags = cnp->cn_flags;
1127681a5bbeSBoris Popov 	int nameiop = cnp->cn_nameiop;
1128681a5bbeSBoris Popov 	int nmlen = cnp->cn_namelen;
1129da1c9cb2SJeff Roberson 	int error, islastcn, isdot;
1130b4484bf0STim J. Robbins 	int killit;
1131681a5bbeSBoris Popov 
1132681a5bbeSBoris Popov 	SMBVDEBUG("\n");
1133681a5bbeSBoris Popov 	if (dvp->v_type != VDIR)
1134681a5bbeSBoris Popov 		return ENOTDIR;
1135e6e370a7SJeff Roberson 	if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) {
1136681a5bbeSBoris Popov 		SMBFSERR("invalid '..'\n");
1137681a5bbeSBoris Popov 		return EIO;
1138681a5bbeSBoris Popov 	}
1139681a5bbeSBoris Popov 	islastcn = flags & ISLASTCN;
1140681a5bbeSBoris Popov 	if (islastcn && (mp->mnt_flag & MNT_RDONLY) && (nameiop != LOOKUP))
1141681a5bbeSBoris Popov 		return EROFS;
1142b1c996c4SBoris Popov 	if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0)
1143681a5bbeSBoris Popov 		return error;
1144681a5bbeSBoris Popov 	smp = VFSTOSMBFS(mp);
1145681a5bbeSBoris Popov 	dnp = VTOSMB(dvp);
1146681a5bbeSBoris Popov 	isdot = (nmlen == 1 && name[0] == '.');
1147681a5bbeSBoris Popov 
1148681a5bbeSBoris Popov 	error = smbfs_pathcheck(smp, cnp->cn_nameptr, cnp->cn_namelen, nameiop);
1149681a5bbeSBoris Popov 
1150681a5bbeSBoris Popov 	if (error)
1151681a5bbeSBoris Popov 		return ENOENT;
1152681a5bbeSBoris Popov 
1153bf40d24aSJohn Baldwin 	error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
1154681a5bbeSBoris Popov 	SMBVDEBUG("cache_lookup returned %d\n", error);
1155681a5bbeSBoris Popov 	if (error > 0)
1156681a5bbeSBoris Popov 		return error;
1157681a5bbeSBoris Popov 	if (error) {		/* name was found */
1158681a5bbeSBoris Popov 		struct vattr vattr;
1159681a5bbeSBoris Popov 
1160b4484bf0STim J. Robbins 		killit = 0;
1161e8943128SXin LI 		vp = *vpp;
11620359a12eSAttilio Rao 		error = VOP_GETATTR(vp, &vattr, cnp->cn_cred);
1163b4484bf0STim J. Robbins 		/*
1164b4484bf0STim J. Robbins 		 * If the file type on the server is inconsistent
1165b4484bf0STim J. Robbins 		 * with what it was when we created the vnode,
1166b4484bf0STim J. Robbins 		 * kill the bogus vnode now and fall through to
1167b4484bf0STim J. Robbins 		 * the code below to create a new one with the
1168b4484bf0STim J. Robbins 		 * right type.
1169b4484bf0STim J. Robbins 		 */
1170b4484bf0STim J. Robbins 		if (error == 0 &&
1171b4484bf0STim J. Robbins 		   ((vp->v_type == VDIR &&
1172b4484bf0STim J. Robbins 		   (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) == 0) ||
1173b4484bf0STim J. Robbins 		   (vp->v_type == VREG &&
1174b4484bf0STim J. Robbins 		   (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) != 0)))
1175b4484bf0STim J. Robbins 		   killit = 1;
1176b4484bf0STim J. Robbins 		else if (error == 0
1177681a5bbeSBoris Popov 	     /*    && vattr.va_ctime.tv_sec == VTOSMB(vp)->n_ctime*/) {
1178681a5bbeSBoris Popov 		     if (nameiop != LOOKUP && islastcn)
1179681a5bbeSBoris Popov 			     cnp->cn_flags |= SAVENAME;
1180681a5bbeSBoris Popov 		     SMBVDEBUG("use cached vnode\n");
1181681a5bbeSBoris Popov 		     return (0);
1182681a5bbeSBoris Popov 		}
1183681a5bbeSBoris Popov 		cache_purge(vp);
1184f6576f19SJeff Roberson 		/*
1185f6576f19SJeff Roberson 		 * XXX This is not quite right, if '.' is
1186f6576f19SJeff Roberson 		 * inconsistent, we really need to start the lookup
1187f6576f19SJeff Roberson 		 * all over again.  Hopefully there is some other
1188f6576f19SJeff Roberson 		 * guarantee that prevents this case from happening.
1189f6576f19SJeff Roberson 		 */
1190f6576f19SJeff Roberson 		if (killit && vp != dvp)
1191b4484bf0STim J. Robbins 			vgone(vp);
1192f6576f19SJeff Roberson 		if (vp != dvp)
11938da00465SJeff Roberson 			vput(vp);
1194f6576f19SJeff Roberson 		else
1195f6576f19SJeff Roberson 			vrele(vp);
1196681a5bbeSBoris Popov 		*vpp = NULLVP;
1197681a5bbeSBoris Popov 	}
1198681a5bbeSBoris Popov 	/*
1199681a5bbeSBoris Popov 	 * entry is not in the cache or has been expired
1200681a5bbeSBoris Popov 	 */
1201681a5bbeSBoris Popov 	error = 0;
1202681a5bbeSBoris Popov 	*vpp = NULLVP;
1203afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
1204afe09751SDavide Italiano 	smb_makescred(scred, td, cnp->cn_cred);
1205681a5bbeSBoris Popov 	fap = &fattr;
1206681a5bbeSBoris Popov 	if (flags & ISDOTDOT) {
1207*ce589ae2SDavide Italiano 		/*
1208*ce589ae2SDavide Italiano 		 * In the DOTDOT case, don't go over-the-wire
1209*ce589ae2SDavide Italiano 		 * in order to request attributes. We already
1210*ce589ae2SDavide Italiano 		 * know it's a directory and subsequent call to
1211*ce589ae2SDavide Italiano 		 * smbfs_getattr() will restore consistency.
1212*ce589ae2SDavide Italiano 		 *
1213*ce589ae2SDavide Italiano 		 */
1214*ce589ae2SDavide Italiano 		SMBVDEBUG("smbfs_smb_lookup: dotdot\n");
1215*ce589ae2SDavide Italiano 	} else if (isdot) {
1216*ce589ae2SDavide Italiano 		error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred);
1217*ce589ae2SDavide Italiano 		SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1218*ce589ae2SDavide Italiano 	}
1219*ce589ae2SDavide Italiano 	else {
1220afe09751SDavide Italiano 		error = smbfs_smb_lookup(dnp, name, nmlen, fap, scred);
1221681a5bbeSBoris Popov 		SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1222681a5bbeSBoris Popov 	}
1223681a5bbeSBoris Popov 	if (error && error != ENOENT)
1224afe09751SDavide Italiano 		goto out;
1225681a5bbeSBoris Popov 	if (error) {			/* entry not found */
1226681a5bbeSBoris Popov 		/*
1227681a5bbeSBoris Popov 		 * Handle RENAME or CREATE case...
1228681a5bbeSBoris Popov 		 */
1229da1c9cb2SJeff Roberson 		if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
12306e8681aaSBoris Popov 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
12316e8681aaSBoris Popov 			if (error)
1232afe09751SDavide Italiano 				goto out;
1233681a5bbeSBoris Popov 			cnp->cn_flags |= SAVENAME;
1234afe09751SDavide Italiano 			error = EJUSTRETURN;
1235afe09751SDavide Italiano 			goto out;
1236681a5bbeSBoris Popov 		}
1237afe09751SDavide Italiano 		error = ENOENT;
1238afe09751SDavide Italiano 		goto out;
1239681a5bbeSBoris Popov 	}/* else {
1240681a5bbeSBoris Popov 		SMBVDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
1241681a5bbeSBoris Popov 	}*/
1242681a5bbeSBoris Popov 	/*
1243681a5bbeSBoris Popov 	 * handle DELETE case ...
1244681a5bbeSBoris Popov 	 */
1245681a5bbeSBoris Popov 	if (nameiop == DELETE && islastcn) { 	/* delete last component */
1246b1c996c4SBoris Popov 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1247681a5bbeSBoris Popov 		if (error)
1248afe09751SDavide Italiano 			goto out;
1249681a5bbeSBoris Popov 		if (isdot) {
1250681a5bbeSBoris Popov 			VREF(dvp);
1251681a5bbeSBoris Popov 			*vpp = dvp;
1252afe09751SDavide Italiano 			goto out;
1253681a5bbeSBoris Popov 		}
1254681a5bbeSBoris Popov 		error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1255681a5bbeSBoris Popov 		if (error)
1256afe09751SDavide Italiano 			goto out;
1257681a5bbeSBoris Popov 		*vpp = vp;
1258681a5bbeSBoris Popov 		cnp->cn_flags |= SAVENAME;
1259afe09751SDavide Italiano 		goto out;
1260681a5bbeSBoris Popov 	}
1261da1c9cb2SJeff Roberson 	if (nameiop == RENAME && islastcn) {
1262b1c996c4SBoris Popov 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1263681a5bbeSBoris Popov 		if (error)
1264afe09751SDavide Italiano 			goto out;
1265afe09751SDavide Italiano 		if (isdot) {
1266afe09751SDavide Italiano 			error = EISDIR;
1267afe09751SDavide Italiano 			goto out;
1268afe09751SDavide Italiano 		}
1269681a5bbeSBoris Popov 		error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1270681a5bbeSBoris Popov 		if (error)
1271afe09751SDavide Italiano 			goto out;
1272681a5bbeSBoris Popov 		*vpp = vp;
1273681a5bbeSBoris Popov 		cnp->cn_flags |= SAVENAME;
1274afe09751SDavide Italiano 		goto out;
1275681a5bbeSBoris Popov 	}
1276681a5bbeSBoris Popov 	if (flags & ISDOTDOT) {
12779dbe0b12SDavide Italiano 		mp = dvp->v_mount;
12789dbe0b12SDavide Italiano 		error = vfs_busy(mp, MBF_NOWAIT);
12799dbe0b12SDavide Italiano 		if (error != 0) {
12809dbe0b12SDavide Italiano 			vfs_ref(mp);
12819dbe0b12SDavide Italiano 			VOP_UNLOCK(dvp, 0);
12829dbe0b12SDavide Italiano 			error = vfs_busy(mp, 0);
12839dbe0b12SDavide Italiano 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
12849dbe0b12SDavide Italiano 			vfs_rel(mp);
12859dbe0b12SDavide Italiano 			if (error)
12869dbe0b12SDavide Italiano 				return (ENOENT);
12879dbe0b12SDavide Italiano 			if ((dvp->v_iflag & VI_DOOMED) != 0) {
12889dbe0b12SDavide Italiano 				vfs_unbusy(mp);
12899dbe0b12SDavide Italiano 				return (ENOENT);
12909dbe0b12SDavide Italiano 			}
12919dbe0b12SDavide Italiano 		}
129222db15c0SAttilio Rao 		VOP_UNLOCK(dvp, 0);
1293681a5bbeSBoris Popov 		error = smbfs_nget(mp, dvp, name, nmlen, NULL, &vp);
12949dbe0b12SDavide Italiano 		vfs_unbusy(mp);
1295cb05b60aSAttilio Rao 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
12969dbe0b12SDavide Italiano 		if ((dvp->v_iflag & VI_DOOMED) != 0) {
12979dbe0b12SDavide Italiano 			if (error == 0)
12989dbe0b12SDavide Italiano 				vput(vp);
12999dbe0b12SDavide Italiano 			error = ENOENT;
13009dbe0b12SDavide Italiano 		}
13014585e3acSJeff Roberson 		if (error)
1302afe09751SDavide Italiano 			goto out;
1303681a5bbeSBoris Popov 		*vpp = vp;
1304681a5bbeSBoris Popov 	} else if (isdot) {
1305681a5bbeSBoris Popov 		vref(dvp);
1306681a5bbeSBoris Popov 		*vpp = dvp;
1307681a5bbeSBoris Popov 	} else {
1308681a5bbeSBoris Popov 		error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1309681a5bbeSBoris Popov 		if (error)
1310afe09751SDavide Italiano 			goto out;
1311681a5bbeSBoris Popov 		*vpp = vp;
1312681a5bbeSBoris Popov 		SMBVDEBUG("lookup: getnewvp!\n");
1313681a5bbeSBoris Popov 	}
1314681a5bbeSBoris Popov 	if ((cnp->cn_flags & MAKEENTRY)/* && !islastcn*/) {
1315681a5bbeSBoris Popov /*		VTOSMB(*vpp)->n_ctime = VTOSMB(*vpp)->n_vattr.va_ctime.tv_sec;*/
1316681a5bbeSBoris Popov 		cache_enter(dvp, *vpp, cnp);
1317681a5bbeSBoris Popov 	}
1318afe09751SDavide Italiano out:
1319afe09751SDavide Italiano 	smbfs_free_scred(scred);
1320afe09751SDavide Italiano 	return (error);
1321681a5bbeSBoris Popov }
1322