xref: /freebsd/sys/fs/smbfs/smbfs_vnops.c (revision b09b03a19d26e63046f3413cc8be2e4ece08daff)
1d167cf6fSWarner Losh /*-
2d63027b6SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3d63027b6SPedro F. Giffuni  *
4681a5bbeSBoris Popov  * Copyright (c) 2000-2001 Boris Popov
5681a5bbeSBoris Popov  * All rights reserved.
6681a5bbeSBoris Popov  *
7681a5bbeSBoris Popov  * Redistribution and use in source and binary forms, with or without
8681a5bbeSBoris Popov  * modification, are permitted provided that the following conditions
9681a5bbeSBoris Popov  * are met:
10681a5bbeSBoris Popov  * 1. Redistributions of source code must retain the above copyright
11681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer.
12681a5bbeSBoris Popov  * 2. Redistributions in binary form must reproduce the above copyright
13681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer in the
14681a5bbeSBoris Popov  *    documentation and/or other materials provided with the distribution.
15681a5bbeSBoris Popov  *
16681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26681a5bbeSBoris Popov  * SUCH DAMAGE.
27681a5bbeSBoris Popov  *
28681a5bbeSBoris Popov  * $FreeBSD$
29681a5bbeSBoris Popov  */
30681a5bbeSBoris Popov #include <sys/param.h>
31681a5bbeSBoris Popov #include <sys/systm.h>
32681a5bbeSBoris Popov #include <sys/namei.h>
33681a5bbeSBoris Popov #include <sys/kernel.h>
34681a5bbeSBoris Popov #include <sys/proc.h>
35681a5bbeSBoris Popov #include <sys/bio.h>
36681a5bbeSBoris Popov #include <sys/buf.h>
37681a5bbeSBoris Popov #include <sys/fcntl.h>
38681a5bbeSBoris Popov #include <sys/mount.h>
39681a5bbeSBoris Popov #include <sys/unistd.h>
40681a5bbeSBoris Popov #include <sys/vnode.h>
41104a9b7eSAlexander Kabaev #include <sys/limits.h>
42681a5bbeSBoris Popov #include <sys/lockf.h>
43fb8e9eadSBoris Popov #include <sys/stat.h>
44681a5bbeSBoris Popov 
45681a5bbeSBoris Popov #include <vm/vm.h>
46681a5bbeSBoris Popov #include <vm/vm_extern.h>
47681a5bbeSBoris Popov 
48681a5bbeSBoris Popov #include <netsmb/smb.h>
49681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
50681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
51681a5bbeSBoris Popov 
52681a5bbeSBoris Popov #include <fs/smbfs/smbfs.h>
53681a5bbeSBoris Popov #include <fs/smbfs/smbfs_node.h>
54681a5bbeSBoris Popov #include <fs/smbfs/smbfs_subr.h>
55681a5bbeSBoris Popov 
56681a5bbeSBoris Popov /*
57681a5bbeSBoris Popov  * Prototypes for SMBFS vnode operations
58681a5bbeSBoris Popov  */
596fde64c7SPoul-Henning Kamp static vop_create_t	smbfs_create;
606fde64c7SPoul-Henning Kamp static vop_mknod_t	smbfs_mknod;
616fde64c7SPoul-Henning Kamp static vop_open_t	smbfs_open;
626fde64c7SPoul-Henning Kamp static vop_close_t	smbfs_close;
636fde64c7SPoul-Henning Kamp static vop_access_t	smbfs_access;
646fde64c7SPoul-Henning Kamp static vop_getattr_t	smbfs_getattr;
656fde64c7SPoul-Henning Kamp static vop_setattr_t	smbfs_setattr;
666fde64c7SPoul-Henning Kamp static vop_read_t	smbfs_read;
676fde64c7SPoul-Henning Kamp static vop_write_t	smbfs_write;
686fde64c7SPoul-Henning Kamp static vop_fsync_t	smbfs_fsync;
696fde64c7SPoul-Henning Kamp static vop_remove_t	smbfs_remove;
706fde64c7SPoul-Henning Kamp static vop_link_t	smbfs_link;
716fde64c7SPoul-Henning Kamp static vop_lookup_t	smbfs_lookup;
726fde64c7SPoul-Henning Kamp static vop_rename_t	smbfs_rename;
736fde64c7SPoul-Henning Kamp static vop_mkdir_t	smbfs_mkdir;
746fde64c7SPoul-Henning Kamp static vop_rmdir_t	smbfs_rmdir;
756fde64c7SPoul-Henning Kamp static vop_symlink_t	smbfs_symlink;
766fde64c7SPoul-Henning Kamp static vop_readdir_t	smbfs_readdir;
776fde64c7SPoul-Henning Kamp static vop_strategy_t	smbfs_strategy;
786fde64c7SPoul-Henning Kamp static vop_print_t	smbfs_print;
796fde64c7SPoul-Henning Kamp static vop_pathconf_t	smbfs_pathconf;
806fde64c7SPoul-Henning Kamp static vop_advlock_t	smbfs_advlock;
816fde64c7SPoul-Henning Kamp static vop_getextattr_t	smbfs_getextattr;
82681a5bbeSBoris Popov 
83aec0fb7bSPoul-Henning Kamp struct vop_vector smbfs_vnodeops = {
84aec0fb7bSPoul-Henning Kamp 	.vop_default =		&default_vnodeops,
8583c64397SPoul-Henning Kamp 
86aec0fb7bSPoul-Henning Kamp 	.vop_access =		smbfs_access,
87aec0fb7bSPoul-Henning Kamp 	.vop_advlock =		smbfs_advlock,
88aec0fb7bSPoul-Henning Kamp 	.vop_close =		smbfs_close,
89aec0fb7bSPoul-Henning Kamp 	.vop_create =		smbfs_create,
90aec0fb7bSPoul-Henning Kamp 	.vop_fsync =		smbfs_fsync,
91aec0fb7bSPoul-Henning Kamp 	.vop_getattr =		smbfs_getattr,
9283c64397SPoul-Henning Kamp 	.vop_getextattr = 	smbfs_getextattr,
93aec0fb7bSPoul-Henning Kamp 	.vop_getpages =		smbfs_getpages,
94aec0fb7bSPoul-Henning Kamp 	.vop_inactive =		smbfs_inactive,
95aec0fb7bSPoul-Henning Kamp 	.vop_ioctl =		smbfs_ioctl,
96aec0fb7bSPoul-Henning Kamp 	.vop_link =		smbfs_link,
97aec0fb7bSPoul-Henning Kamp 	.vop_lookup =		smbfs_lookup,
98aec0fb7bSPoul-Henning Kamp 	.vop_mkdir =		smbfs_mkdir,
99aec0fb7bSPoul-Henning Kamp 	.vop_mknod =		smbfs_mknod,
100aec0fb7bSPoul-Henning Kamp 	.vop_open =		smbfs_open,
101aec0fb7bSPoul-Henning Kamp 	.vop_pathconf =		smbfs_pathconf,
102aec0fb7bSPoul-Henning Kamp 	.vop_print =		smbfs_print,
103aec0fb7bSPoul-Henning Kamp 	.vop_putpages =		smbfs_putpages,
104aec0fb7bSPoul-Henning Kamp 	.vop_read =		smbfs_read,
105aec0fb7bSPoul-Henning Kamp 	.vop_readdir =		smbfs_readdir,
106aec0fb7bSPoul-Henning Kamp 	.vop_reclaim =		smbfs_reclaim,
107aec0fb7bSPoul-Henning Kamp 	.vop_remove =		smbfs_remove,
108aec0fb7bSPoul-Henning Kamp 	.vop_rename =		smbfs_rename,
109aec0fb7bSPoul-Henning Kamp 	.vop_rmdir =		smbfs_rmdir,
110aec0fb7bSPoul-Henning Kamp 	.vop_setattr =		smbfs_setattr,
11183c64397SPoul-Henning Kamp /*	.vop_setextattr =	smbfs_setextattr,*/
112aec0fb7bSPoul-Henning Kamp 	.vop_strategy =		smbfs_strategy,
113aec0fb7bSPoul-Henning Kamp 	.vop_symlink =		smbfs_symlink,
114aec0fb7bSPoul-Henning Kamp 	.vop_write =		smbfs_write,
115681a5bbeSBoris Popov };
1166fa079fcSMateusz Guzik VFS_VOP_VECTOR_REGISTER(smbfs_vnodeops);
117681a5bbeSBoris Popov 
118681a5bbeSBoris Popov static int
119*b09b03a1SMateusz Guzik smbfs_access(struct vop_access_args *ap)
120681a5bbeSBoris Popov {
121681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
12215bc6b2bSEdward Tomasz Napierala 	accmode_t accmode = ap->a_accmode;
12338356d10STim J. Robbins 	mode_t mpmode;
124681a5bbeSBoris Popov 	struct smbmount *smp = VTOSMBFS(vp);
125681a5bbeSBoris Popov 
126681a5bbeSBoris Popov 	SMBVDEBUG("\n");
12715bc6b2bSEdward Tomasz Napierala 	if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
128681a5bbeSBoris Popov 		switch (vp->v_type) {
129681a5bbeSBoris Popov 		    case VREG: case VDIR: case VLNK:
130681a5bbeSBoris Popov 			return EROFS;
131681a5bbeSBoris Popov 		    default:
132681a5bbeSBoris Popov 			break;
133681a5bbeSBoris Popov 		}
134681a5bbeSBoris Popov 	}
135d14c8441SPoul-Henning Kamp 	mpmode = vp->v_type == VREG ? smp->sm_file_mode : smp->sm_dir_mode;
136d14c8441SPoul-Henning Kamp 	return (vaccess(vp->v_type, mpmode, smp->sm_uid,
137d292b194SMateusz Guzik 	    smp->sm_gid, ap->a_accmode, ap->a_cred));
138681a5bbeSBoris Popov }
139681a5bbeSBoris Popov 
140681a5bbeSBoris Popov /* ARGSUSED */
141681a5bbeSBoris Popov static int
142*b09b03a1SMateusz Guzik smbfs_open(struct vop_open_args *ap)
143681a5bbeSBoris Popov {
144681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
145681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
146afe09751SDavide Italiano 	struct smb_cred *scred;
147681a5bbeSBoris Popov 	struct vattr vattr;
148681a5bbeSBoris Popov 	int mode = ap->a_mode;
149681a5bbeSBoris Popov 	int error, accmode;
150681a5bbeSBoris Popov 
1512a4ad258STim J. Robbins 	SMBVDEBUG("%s,%d\n", np->n_name, (np->n_flag & NOPEN) != 0);
152681a5bbeSBoris Popov 	if (vp->v_type != VREG && vp->v_type != VDIR) {
153681a5bbeSBoris Popov 		SMBFSERR("open eacces vtype=%d\n", vp->v_type);
154681a5bbeSBoris Popov 		return EACCES;
155681a5bbeSBoris Popov 	}
156681a5bbeSBoris Popov 	if (vp->v_type == VDIR) {
1572a4ad258STim J. Robbins 		np->n_flag |= NOPEN;
158681a5bbeSBoris Popov 		return 0;
159681a5bbeSBoris Popov 	}
160681a5bbeSBoris Popov 	if (np->n_flag & NMODIFIED) {
161e50508dfSPoul-Henning Kamp 		if ((error = smbfs_vinvalbuf(vp, ap->a_td)) == EINTR)
162681a5bbeSBoris Popov 			return error;
163681a5bbeSBoris Popov 		smbfs_attr_cacheremove(vp);
1640359a12eSAttilio Rao 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
165681a5bbeSBoris Popov 		if (error)
166681a5bbeSBoris Popov 			return error;
167681a5bbeSBoris Popov 		np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
168681a5bbeSBoris Popov 	} else {
1690359a12eSAttilio Rao 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
170681a5bbeSBoris Popov 		if (error)
171681a5bbeSBoris Popov 			return error;
172681a5bbeSBoris Popov 		if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
173e50508dfSPoul-Henning Kamp 			error = smbfs_vinvalbuf(vp, ap->a_td);
174681a5bbeSBoris Popov 			if (error == EINTR)
175681a5bbeSBoris Popov 				return error;
176681a5bbeSBoris Popov 			np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
177681a5bbeSBoris Popov 		}
178681a5bbeSBoris Popov 	}
1792a4ad258STim J. Robbins 	if ((np->n_flag & NOPEN) != 0)
180681a5bbeSBoris Popov 		return 0;
18144f3878eSBoris Popov 	/*
18244f3878eSBoris Popov 	 * Use DENYNONE to give unixy semantics of permitting
18344f3878eSBoris Popov 	 * everything not forbidden by permissions.  Ie denial
18444f3878eSBoris Popov 	 * is up to server with clients/openers needing to use
18544f3878eSBoris Popov 	 * advisory locks for further control.
18644f3878eSBoris Popov 	 */
18744f3878eSBoris Popov 	accmode = SMB_SM_DENYNONE|SMB_AM_OPENREAD;
188681a5bbeSBoris Popov 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
18944f3878eSBoris Popov 		accmode = SMB_SM_DENYNONE|SMB_AM_OPENRW;
190afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
191afe09751SDavide Italiano 	smb_makescred(scred, ap->a_td, ap->a_cred);
192afe09751SDavide Italiano 	error = smbfs_smb_open(np, accmode, scred);
193681a5bbeSBoris Popov 	if (error) {
194681a5bbeSBoris Popov 		if (mode & FWRITE)
195681a5bbeSBoris Popov 			return EACCES;
19644f3878eSBoris Popov 		else if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
19744f3878eSBoris Popov 			accmode = SMB_SM_DENYNONE|SMB_AM_OPENREAD;
198afe09751SDavide Italiano 			error = smbfs_smb_open(np, accmode, scred);
199681a5bbeSBoris Popov 		}
20044f3878eSBoris Popov 	}
20172b3e305SPeter Edwards 	if (error == 0) {
2022a4ad258STim J. Robbins 		np->n_flag |= NOPEN;
20372b3e305SPeter Edwards 		vnode_create_vobject(ap->a_vp, vattr.va_size, ap->a_td);
20472b3e305SPeter Edwards 	}
205681a5bbeSBoris Popov 	smbfs_attr_cacheremove(vp);
206afe09751SDavide Italiano 	smbfs_free_scred(scred);
207681a5bbeSBoris Popov 	return error;
208681a5bbeSBoris Popov }
209681a5bbeSBoris Popov 
210681a5bbeSBoris Popov static int
211*b09b03a1SMateusz Guzik smbfs_close(struct vop_close_args *ap)
212681a5bbeSBoris Popov {
213681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
214b1c996c4SBoris Popov 	struct thread *td = ap->a_td;
215835fb616STim J. Robbins 	struct smbnode *np = VTOSMB(vp);
216afe09751SDavide Italiano 	struct smb_cred *scred;
217681a5bbeSBoris Popov 
218835fb616STim J. Robbins 	if (vp->v_type == VDIR && (np->n_flag & NOPEN) != 0 &&
219835fb616STim J. Robbins 	    np->n_dirseq != NULL) {
220afe09751SDavide Italiano 		scred = smbfs_malloc_scred();
221afe09751SDavide Italiano 		smb_makescred(scred, td, ap->a_cred);
222afe09751SDavide Italiano 		smbfs_findclose(np->n_dirseq, scred);
223afe09751SDavide Italiano 		smbfs_free_scred(scred);
224835fb616STim J. Robbins 		np->n_dirseq = NULL;
225835fb616STim J. Robbins 	}
2262a4ad258STim J. Robbins 	return 0;
227681a5bbeSBoris Popov }
228681a5bbeSBoris Popov 
229681a5bbeSBoris Popov /*
230681a5bbeSBoris Popov  * smbfs_getattr call from vfs.
231681a5bbeSBoris Popov  */
232681a5bbeSBoris Popov static int
233*b09b03a1SMateusz Guzik smbfs_getattr(struct vop_getattr_args *ap)
234681a5bbeSBoris Popov {
235681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
236681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
237681a5bbeSBoris Popov 	struct vattr *va=ap->a_vap;
238681a5bbeSBoris Popov 	struct smbfattr fattr;
239afe09751SDavide Italiano 	struct smb_cred *scred;
24007a65634STim J. Robbins 	u_quad_t oldsize;
241681a5bbeSBoris Popov 	int error;
242681a5bbeSBoris Popov 
243e6e370a7SJeff Roberson 	SMBVDEBUG("%lx: '%s' %d\n", (long)vp, np->n_name, (vp->v_vflag & VV_ROOT) != 0);
244681a5bbeSBoris Popov 	error = smbfs_attr_cachelookup(vp, va);
245681a5bbeSBoris Popov 	if (!error)
246681a5bbeSBoris Popov 		return 0;
247681a5bbeSBoris Popov 	SMBVDEBUG("not in the cache\n");
248afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
249afe09751SDavide Italiano 	smb_makescred(scred, curthread, ap->a_cred);
250681a5bbeSBoris Popov 	oldsize = np->n_size;
251afe09751SDavide Italiano 	error = smbfs_smb_lookup(np, NULL, 0, &fattr, scred);
252681a5bbeSBoris Popov 	if (error) {
253681a5bbeSBoris Popov 		SMBVDEBUG("error %d\n", error);
254afe09751SDavide Italiano 		smbfs_free_scred(scred);
255681a5bbeSBoris Popov 		return error;
256681a5bbeSBoris Popov 	}
257681a5bbeSBoris Popov 	smbfs_attr_cacheenter(vp, &fattr);
258681a5bbeSBoris Popov 	smbfs_attr_cachelookup(vp, va);
2592a4ad258STim J. Robbins 	if (np->n_flag & NOPEN)
260681a5bbeSBoris Popov 		np->n_size = oldsize;
261afe09751SDavide Italiano 	smbfs_free_scred(scred);
262681a5bbeSBoris Popov 	return 0;
263681a5bbeSBoris Popov }
264681a5bbeSBoris Popov 
265681a5bbeSBoris Popov static int
266*b09b03a1SMateusz Guzik smbfs_setattr(struct vop_setattr_args *ap)
267681a5bbeSBoris Popov {
268681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
269681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
270681a5bbeSBoris Popov 	struct vattr *vap = ap->a_vap;
271681a5bbeSBoris Popov 	struct timespec *mtime, *atime;
272afe09751SDavide Italiano 	struct smb_cred *scred;
273681a5bbeSBoris Popov 	struct smb_share *ssp = np->n_mount->sm_share;
274681a5bbeSBoris Popov 	struct smb_vc *vcp = SSTOVC(ssp);
2750359a12eSAttilio Rao 	struct thread *td = curthread;
276681a5bbeSBoris Popov 	u_quad_t tsize = 0;
277681a5bbeSBoris Popov 	int isreadonly, doclose, error = 0;
278fb8e9eadSBoris Popov 	int old_n_dosattr;
279681a5bbeSBoris Popov 
280681a5bbeSBoris Popov 	SMBVDEBUG("\n");
281681a5bbeSBoris Popov 	isreadonly = (vp->v_mount->mnt_flag & MNT_RDONLY);
282681a5bbeSBoris Popov 	/*
283681a5bbeSBoris Popov 	 * Disallow write attempts if the filesystem is mounted read-only.
284681a5bbeSBoris Popov 	 */
285681a5bbeSBoris Popov   	if ((vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL ||
286681a5bbeSBoris Popov 	     vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
2877da1a731SKenneth D. Merry 	     vap->va_mode != (mode_t)VNOVAL || vap->va_flags != VNOVAL) &&
2887da1a731SKenneth D. Merry 	     isreadonly)
289681a5bbeSBoris Popov 		return EROFS;
2907da1a731SKenneth D. Merry 
2917da1a731SKenneth D. Merry 	/*
2927da1a731SKenneth D. Merry 	 * We only support setting four flags.  Don't allow setting others.
2937da1a731SKenneth D. Merry 	 *
2947da1a731SKenneth D. Merry 	 * We map UF_READONLY to SMB_FA_RDONLY, unlike the MacOS X version
2957da1a731SKenneth D. Merry 	 * of this code, which maps both UF_IMMUTABLE AND SF_IMMUTABLE to
2967da1a731SKenneth D. Merry 	 * SMB_FA_RDONLY.  The immutable flags have different semantics
2977da1a731SKenneth D. Merry 	 * than readonly, which is the reason for the difference.
2987da1a731SKenneth D. Merry 	 */
2997da1a731SKenneth D. Merry 	if (vap->va_flags != VNOVAL) {
3007da1a731SKenneth D. Merry 		if (vap->va_flags & ~(UF_HIDDEN|UF_SYSTEM|UF_ARCHIVE|
3017da1a731SKenneth D. Merry 				      UF_READONLY))
3027da1a731SKenneth D. Merry 			return EINVAL;
3037da1a731SKenneth D. Merry 	}
3047da1a731SKenneth D. Merry 
305afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
306afe09751SDavide Italiano 	smb_makescred(scred, td, ap->a_cred);
307681a5bbeSBoris Popov 	if (vap->va_size != VNOVAL) {
308681a5bbeSBoris Popov  		switch (vp->v_type) {
309681a5bbeSBoris Popov  		    case VDIR:
310afe09751SDavide Italiano  			error = EISDIR;
311afe09751SDavide Italiano 			goto out;
312681a5bbeSBoris Popov  		    case VREG:
313681a5bbeSBoris Popov 			break;
314681a5bbeSBoris Popov  		    default:
315afe09751SDavide Italiano 			error = EINVAL;
316afe09751SDavide Italiano 			goto out;
31774b8d63dSPedro F. Giffuni   		}
318afe09751SDavide Italiano 		if (isreadonly) {
319afe09751SDavide Italiano 			error = EROFS;
320afe09751SDavide Italiano 			goto out;
321afe09751SDavide Italiano 		}
322681a5bbeSBoris Popov 		doclose = 0;
323681a5bbeSBoris Popov 		vnode_pager_setsize(vp, (u_long)vap->va_size);
324681a5bbeSBoris Popov  		tsize = np->n_size;
325681a5bbeSBoris Popov  		np->n_size = vap->va_size;
3262a4ad258STim J. Robbins 		if ((np->n_flag & NOPEN) == 0) {
32744f3878eSBoris Popov 			error = smbfs_smb_open(np,
32844f3878eSBoris Popov 					       SMB_SM_DENYNONE|SMB_AM_OPENRW,
329afe09751SDavide Italiano 					       scred);
330681a5bbeSBoris Popov 			if (error == 0)
331681a5bbeSBoris Popov 				doclose = 1;
332681a5bbeSBoris Popov 		}
333681a5bbeSBoris Popov 		if (error == 0)
334c829016eSAndrey V. Elsukov 			error = smbfs_smb_setfsize(np,
335c829016eSAndrey V. Elsukov 			    (int64_t)vap->va_size, scred);
336681a5bbeSBoris Popov 		if (doclose)
337afe09751SDavide Italiano 			smbfs_smb_close(ssp, np->n_fid, NULL, scred);
338681a5bbeSBoris Popov 		if (error) {
339681a5bbeSBoris Popov 			np->n_size = tsize;
340681a5bbeSBoris Popov 			vnode_pager_setsize(vp, (u_long)tsize);
341afe09751SDavide Italiano 			goto out;
342681a5bbeSBoris Popov 		}
343681a5bbeSBoris Popov   	}
3447da1a731SKenneth D. Merry 	if ((vap->va_flags != VNOVAL) || (vap->va_mode != (mode_t)VNOVAL)) {
345fb8e9eadSBoris Popov 		old_n_dosattr = np->n_dosattr;
3467da1a731SKenneth D. Merry 
3477da1a731SKenneth D. Merry 		if (vap->va_mode != (mode_t)VNOVAL) {
348fb8e9eadSBoris Popov 			if (vap->va_mode & S_IWUSR)
349fb8e9eadSBoris Popov 				np->n_dosattr &= ~SMB_FA_RDONLY;
350fb8e9eadSBoris Popov 			else
351fb8e9eadSBoris Popov 				np->n_dosattr |= SMB_FA_RDONLY;
3527da1a731SKenneth D. Merry 		}
3537da1a731SKenneth D. Merry 
3547da1a731SKenneth D. Merry 		if (vap->va_flags != VNOVAL) {
3557da1a731SKenneth D. Merry 			if (vap->va_flags & UF_HIDDEN)
3567da1a731SKenneth D. Merry 				np->n_dosattr |= SMB_FA_HIDDEN;
3577da1a731SKenneth D. Merry 			else
3587da1a731SKenneth D. Merry 				np->n_dosattr &= ~SMB_FA_HIDDEN;
3597da1a731SKenneth D. Merry 
3607da1a731SKenneth D. Merry 			if (vap->va_flags & UF_SYSTEM)
3617da1a731SKenneth D. Merry 				np->n_dosattr |= SMB_FA_SYSTEM;
3627da1a731SKenneth D. Merry 			else
3637da1a731SKenneth D. Merry 				np->n_dosattr &= ~SMB_FA_SYSTEM;
3647da1a731SKenneth D. Merry 
3657da1a731SKenneth D. Merry 			if (vap->va_flags & UF_ARCHIVE)
3667da1a731SKenneth D. Merry 				np->n_dosattr |= SMB_FA_ARCHIVE;
3677da1a731SKenneth D. Merry 			else
3687da1a731SKenneth D. Merry 				np->n_dosattr &= ~SMB_FA_ARCHIVE;
3697da1a731SKenneth D. Merry 
3707da1a731SKenneth D. Merry 			/*
3717da1a731SKenneth D. Merry 			 * We only support setting the immutable / readonly
3727da1a731SKenneth D. Merry 			 * bit for regular files.  According to comments in
3737da1a731SKenneth D. Merry 			 * the MacOS X version of this code, supporting the
3747da1a731SKenneth D. Merry 			 * readonly bit on directories doesn't do the same
3757da1a731SKenneth D. Merry 			 * thing in Windows as in Unix.
3767da1a731SKenneth D. Merry 			 */
3777da1a731SKenneth D. Merry 			if (vp->v_type == VREG) {
3787da1a731SKenneth D. Merry 				if (vap->va_flags & UF_READONLY)
3797da1a731SKenneth D. Merry 					np->n_dosattr |= SMB_FA_RDONLY;
3807da1a731SKenneth D. Merry 				else
3817da1a731SKenneth D. Merry 					np->n_dosattr &= ~SMB_FA_RDONLY;
3827da1a731SKenneth D. Merry 			}
3837da1a731SKenneth D. Merry 		}
3847da1a731SKenneth D. Merry 
385fb8e9eadSBoris Popov 		if (np->n_dosattr != old_n_dosattr) {
386afe09751SDavide Italiano 			error = smbfs_smb_setpattr(np, np->n_dosattr, NULL, scred);
387fb8e9eadSBoris Popov 			if (error)
388afe09751SDavide Italiano 				goto out;
389fb8e9eadSBoris Popov 		}
390fb8e9eadSBoris Popov 	}
391681a5bbeSBoris Popov 	mtime = atime = NULL;
392681a5bbeSBoris Popov 	if (vap->va_mtime.tv_sec != VNOVAL)
393681a5bbeSBoris Popov 		mtime = &vap->va_mtime;
394681a5bbeSBoris Popov 	if (vap->va_atime.tv_sec != VNOVAL)
395681a5bbeSBoris Popov 		atime = &vap->va_atime;
396681a5bbeSBoris Popov 	if (mtime != atime) {
397acd3428bSRobert Watson 		if (vap->va_vaflags & VA_UTIMES_NULL) {
3980359a12eSAttilio Rao 			error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td);
399acd3428bSRobert Watson 			if (error)
4000359a12eSAttilio Rao 				error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td);
401acd3428bSRobert Watson 		} else
4020359a12eSAttilio Rao 			error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td);
403681a5bbeSBoris Popov #if 0
404681a5bbeSBoris Popov 		if (mtime == NULL)
405681a5bbeSBoris Popov 			mtime = &np->n_mtime;
406681a5bbeSBoris Popov 		if (atime == NULL)
407681a5bbeSBoris Popov 			atime = &np->n_atime;
408681a5bbeSBoris Popov #endif
409681a5bbeSBoris Popov 		/*
410681a5bbeSBoris Popov 		 * If file is opened, then we can use handle based calls.
411681a5bbeSBoris Popov 		 * If not, use path based ones.
412681a5bbeSBoris Popov 		 */
4132a4ad258STim J. Robbins 		if ((np->n_flag & NOPEN) == 0) {
414681a5bbeSBoris Popov 			if (vcp->vc_flags & SMBV_WIN95) {
4150359a12eSAttilio Rao 				error = VOP_OPEN(vp, FWRITE, ap->a_cred, td,
4160359a12eSAttilio Rao 				    NULL);
417681a5bbeSBoris Popov 				if (!error) {
4180359a12eSAttilio Rao /*					error = smbfs_smb_setfattrNT(np, 0,
419afe09751SDavide Italiano 					    mtime, atime, scred);
4200359a12eSAttilio Rao 					VOP_GETATTR(vp, &vattr, ap->a_cred); */
421681a5bbeSBoris Popov 					if (mtime)
422681a5bbeSBoris Popov 						np->n_mtime = *mtime;
4230359a12eSAttilio Rao 					VOP_CLOSE(vp, FWRITE, ap->a_cred, td);
424681a5bbeSBoris Popov 				}
425681a5bbeSBoris Popov 			} else if ((vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS)) {
426afe09751SDavide Italiano 				error = smbfs_smb_setptime2(np, mtime, atime, 0, scred);
427afe09751SDavide Italiano /*				error = smbfs_smb_setpattrNT(np, 0, mtime, atime, scred);*/
428681a5bbeSBoris Popov 			} else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN2_0) {
429afe09751SDavide Italiano 				error = smbfs_smb_setptime2(np, mtime, atime, 0, scred);
430681a5bbeSBoris Popov 			} else {
431afe09751SDavide Italiano 				error = smbfs_smb_setpattr(np, 0, mtime, scred);
432681a5bbeSBoris Popov 			}
433681a5bbeSBoris Popov 		} else {
434681a5bbeSBoris Popov 			if (vcp->vc_sopt.sv_caps & SMB_CAP_NT_SMBS) {
435afe09751SDavide Italiano 				error = smbfs_smb_setfattrNT(np, 0, mtime, atime, scred);
436681a5bbeSBoris Popov 			} else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN1_0) {
437afe09751SDavide Italiano 				error = smbfs_smb_setftime(np, mtime, atime, scred);
438681a5bbeSBoris Popov 			} else {
439681a5bbeSBoris Popov 				/*
440681a5bbeSBoris Popov 				 * I have no idea how to handle this for core
441681a5bbeSBoris Popov 				 * level servers. The possible solution is to
442681a5bbeSBoris Popov 				 * update mtime after file is closed.
443681a5bbeSBoris Popov 				 */
444681a5bbeSBoris Popov 				 SMBERROR("can't update times on an opened file\n");
445681a5bbeSBoris Popov 			}
446681a5bbeSBoris Popov 		}
447681a5bbeSBoris Popov 	}
448681a5bbeSBoris Popov 	/*
449681a5bbeSBoris Popov 	 * Invalidate attribute cache in case if server doesn't set
450681a5bbeSBoris Popov 	 * required attributes.
451681a5bbeSBoris Popov 	 */
452681a5bbeSBoris Popov 	smbfs_attr_cacheremove(vp);	/* invalidate cache */
4530359a12eSAttilio Rao 	VOP_GETATTR(vp, vap, ap->a_cred);
454681a5bbeSBoris Popov 	np->n_mtime.tv_sec = vap->va_mtime.tv_sec;
455afe09751SDavide Italiano out:
456afe09751SDavide Italiano 	smbfs_free_scred(scred);
457681a5bbeSBoris Popov 	return error;
458681a5bbeSBoris Popov }
459681a5bbeSBoris Popov /*
460681a5bbeSBoris Popov  * smbfs_read call.
461681a5bbeSBoris Popov  */
462681a5bbeSBoris Popov static int
463*b09b03a1SMateusz Guzik smbfs_read(struct vop_read_args *ap)
464681a5bbeSBoris Popov {
465681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
466681a5bbeSBoris Popov 	struct uio *uio = ap->a_uio;
467681a5bbeSBoris Popov 
468681a5bbeSBoris Popov 	SMBVDEBUG("\n");
469681a5bbeSBoris Popov 	if (vp->v_type != VREG && vp->v_type != VDIR)
470681a5bbeSBoris Popov 		return EPERM;
471681a5bbeSBoris Popov 	return smbfs_readvnode(vp, uio, ap->a_cred);
472681a5bbeSBoris Popov }
473681a5bbeSBoris Popov 
474681a5bbeSBoris Popov static int
475*b09b03a1SMateusz Guzik smbfs_write(struct vop_write_args *ap)
476681a5bbeSBoris Popov {
477681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
478681a5bbeSBoris Popov 	struct uio *uio = ap->a_uio;
479681a5bbeSBoris Popov 
480994f027fSDavide Italiano 	SMBVDEBUG("%d,ofs=%jd,sz=%zd\n",vp->v_type, (intmax_t)uio->uio_offset,
481469cb18fSDavide Italiano 	    uio->uio_resid);
482681a5bbeSBoris Popov 	if (vp->v_type != VREG)
483681a5bbeSBoris Popov 		return (EPERM);
484681a5bbeSBoris Popov 	return smbfs_writevnode(vp, uio, ap->a_cred,ap->a_ioflag);
485681a5bbeSBoris Popov }
486681a5bbeSBoris Popov /*
487681a5bbeSBoris Popov  * smbfs_create call
488681a5bbeSBoris Popov  * Create a regular file. On entry the directory to contain the file being
489681a5bbeSBoris Popov  * created is locked.  We must release before we return. We must also free
4908f7859e8SMateusz Guzik  * the pathname buffer pointed at by cnp->cn_pnbuf, always on error.
491681a5bbeSBoris Popov  */
492681a5bbeSBoris Popov static int
493*b09b03a1SMateusz Guzik smbfs_create(struct vop_create_args *ap)
494681a5bbeSBoris Popov {
495681a5bbeSBoris Popov 	struct vnode *dvp = ap->a_dvp;
496681a5bbeSBoris Popov 	struct vattr *vap = ap->a_vap;
497681a5bbeSBoris Popov 	struct vnode **vpp=ap->a_vpp;
498681a5bbeSBoris Popov 	struct componentname *cnp = ap->a_cnp;
499681a5bbeSBoris Popov 	struct smbnode *dnp = VTOSMB(dvp);
500681a5bbeSBoris Popov 	struct vnode *vp;
501681a5bbeSBoris Popov 	struct vattr vattr;
502681a5bbeSBoris Popov 	struct smbfattr fattr;
503afe09751SDavide Italiano 	struct smb_cred *scred;
504681a5bbeSBoris Popov 	char *name = cnp->cn_nameptr;
505681a5bbeSBoris Popov 	int nmlen = cnp->cn_namelen;
506681a5bbeSBoris Popov 	int error;
507681a5bbeSBoris Popov 
508681a5bbeSBoris Popov 	SMBVDEBUG("\n");
509681a5bbeSBoris Popov 	*vpp = NULL;
510681a5bbeSBoris Popov 	if (vap->va_type != VREG)
511681a5bbeSBoris Popov 		return EOPNOTSUPP;
5120359a12eSAttilio Rao 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
513681a5bbeSBoris Popov 		return error;
514afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
515b4a58fbfSMateusz Guzik 	smb_makescred(scred, curthread, cnp->cn_cred);
516681a5bbeSBoris Popov 
517afe09751SDavide Italiano 	error = smbfs_smb_create(dnp, name, nmlen, scred);
518681a5bbeSBoris Popov 	if (error)
519afe09751SDavide Italiano 		goto out;
520afe09751SDavide Italiano 	error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, scred);
521681a5bbeSBoris Popov 	if (error)
522afe09751SDavide Italiano 		goto out;
523681a5bbeSBoris Popov 	error = smbfs_nget(VTOVFS(dvp), dvp, name, nmlen, &fattr, &vp);
524681a5bbeSBoris Popov 	if (error)
525afe09751SDavide Italiano 		goto out;
526681a5bbeSBoris Popov 	*vpp = vp;
527681a5bbeSBoris Popov 	if (cnp->cn_flags & MAKEENTRY)
528681a5bbeSBoris Popov 		cache_enter(dvp, vp, cnp);
529afe09751SDavide Italiano out:
530afe09751SDavide Italiano 	smbfs_free_scred(scred);
531681a5bbeSBoris Popov 	return error;
532681a5bbeSBoris Popov }
533681a5bbeSBoris Popov 
534681a5bbeSBoris Popov static int
535*b09b03a1SMateusz Guzik smbfs_remove(struct vop_remove_args *ap)
536681a5bbeSBoris Popov {
537681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
538681a5bbeSBoris Popov /*	struct vnode *dvp = ap->a_dvp;*/
539681a5bbeSBoris Popov 	struct componentname *cnp = ap->a_cnp;
540681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
541afe09751SDavide Italiano 	struct smb_cred *scred;
542681a5bbeSBoris Popov 	int error;
543681a5bbeSBoris Popov 
5442a4ad258STim J. Robbins 	if (vp->v_type == VDIR || (np->n_flag & NOPEN) != 0 || vrefcnt(vp) != 1)
545681a5bbeSBoris Popov 		return EPERM;
546afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
547b4a58fbfSMateusz Guzik 	smb_makescred(scred, curthread, cnp->cn_cred);
548afe09751SDavide Italiano 	error = smbfs_smb_delete(np, scred);
549b4484bf0STim J. Robbins 	if (error == 0)
550b4484bf0STim J. Robbins 		np->n_flag |= NGONE;
551681a5bbeSBoris Popov 	cache_purge(vp);
552afe09751SDavide Italiano 	smbfs_free_scred(scred);
553681a5bbeSBoris Popov 	return error;
554681a5bbeSBoris Popov }
555681a5bbeSBoris Popov 
556681a5bbeSBoris Popov /*
557681a5bbeSBoris Popov  * smbfs_file rename call
558681a5bbeSBoris Popov  */
559681a5bbeSBoris Popov static int
560*b09b03a1SMateusz Guzik smbfs_rename(struct vop_rename_args *ap)
561681a5bbeSBoris Popov {
562681a5bbeSBoris Popov 	struct vnode *fvp = ap->a_fvp;
563681a5bbeSBoris Popov 	struct vnode *tvp = ap->a_tvp;
564681a5bbeSBoris Popov 	struct vnode *fdvp = ap->a_fdvp;
565681a5bbeSBoris Popov 	struct vnode *tdvp = ap->a_tdvp;
566681a5bbeSBoris Popov 	struct componentname *tcnp = ap->a_tcnp;
567681a5bbeSBoris Popov /*	struct componentname *fcnp = ap->a_fcnp;*/
568afe09751SDavide Italiano 	struct smb_cred *scred;
5699fe2867cSJohn Baldwin #ifdef notnow
570681a5bbeSBoris Popov 	u_int16_t flags = 6;
5719fe2867cSJohn Baldwin #endif
572681a5bbeSBoris Popov 	int error=0;
573681a5bbeSBoris Popov 
574e346bd81SDavide Italiano 	scred = NULL;
575681a5bbeSBoris Popov 	/* Check for cross-device rename */
576681a5bbeSBoris Popov 	if ((fvp->v_mount != tdvp->v_mount) ||
577681a5bbeSBoris Popov 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
57842039c5bSDavide Italiano 		error = EXDEV;
579681a5bbeSBoris Popov 		goto out;
580681a5bbeSBoris Popov 	}
581681a5bbeSBoris Popov 
5824d93c0beSJeff Roberson 	if (tvp && vrefcnt(tvp) > 1) {
58342039c5bSDavide Italiano 		error = EBUSY;
584681a5bbeSBoris Popov 		goto out;
585681a5bbeSBoris Popov 	}
5869fe2867cSJohn Baldwin #ifdef notnow
587681a5bbeSBoris Popov 	flags = 0x10;			/* verify all writes */
5889fe2867cSJohn Baldwin #endif
589681a5bbeSBoris Popov 	if (fvp->v_type == VDIR) {
5909fe2867cSJohn Baldwin #ifdef notnow
591681a5bbeSBoris Popov 		flags |= 2;
5929fe2867cSJohn Baldwin #endif
593681a5bbeSBoris Popov 	} else if (fvp->v_type == VREG) {
5949fe2867cSJohn Baldwin #ifdef notnow
595681a5bbeSBoris Popov 		flags |= 1;
5969fe2867cSJohn Baldwin #endif
5976dae0c1eSTim J. Robbins 	} else {
598afe09751SDavide Italiano 		return EINVAL;
5996dae0c1eSTim J. Robbins 	}
600afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
601b4a58fbfSMateusz Guzik 	smb_makescred(scred, curthread, tcnp->cn_cred);
602681a5bbeSBoris Popov 	/*
603681a5bbeSBoris Popov 	 * It seems that Samba doesn't implement SMB_COM_MOVE call...
604681a5bbeSBoris Popov 	 */
605681a5bbeSBoris Popov #ifdef notnow
606681a5bbeSBoris Popov 	if (SMB_DIALECT(SSTOCN(smp->sm_share)) >= SMB_DIALECT_LANMAN1_0) {
607681a5bbeSBoris Popov 		error = smbfs_smb_move(VTOSMB(fvp), VTOSMB(tdvp),
608afe09751SDavide Italiano 		    tcnp->cn_nameptr, tcnp->cn_namelen, flags, scred);
609681a5bbeSBoris Popov 	} else
610681a5bbeSBoris Popov #endif
611681a5bbeSBoris Popov 	{
612681a5bbeSBoris Popov 		/*
613681a5bbeSBoris Popov 		 * We have to do the work atomicaly
614681a5bbeSBoris Popov 		 */
615681a5bbeSBoris Popov 		if (tvp && tvp != fvp) {
616afe09751SDavide Italiano 			error = smbfs_smb_delete(VTOSMB(tvp), scred);
617681a5bbeSBoris Popov 			if (error)
6186dae0c1eSTim J. Robbins 				goto out_cacherem;
619b4484bf0STim J. Robbins 			VTOSMB(fvp)->n_flag |= NGONE;
620681a5bbeSBoris Popov 		}
621681a5bbeSBoris Popov 		error = smbfs_smb_rename(VTOSMB(fvp), VTOSMB(tdvp),
622afe09751SDavide Italiano 		    tcnp->cn_nameptr, tcnp->cn_namelen, scred);
623681a5bbeSBoris Popov 	}
624681a5bbeSBoris Popov 
625681a5bbeSBoris Popov 	if (fvp->v_type == VDIR) {
626681a5bbeSBoris Popov 		if (tvp != NULL && tvp->v_type == VDIR)
627681a5bbeSBoris Popov 			cache_purge(tdvp);
628681a5bbeSBoris Popov 		cache_purge(fdvp);
629681a5bbeSBoris Popov 	}
6306dae0c1eSTim J. Robbins 
6316dae0c1eSTim J. Robbins out_cacherem:
6326dae0c1eSTim J. Robbins 	smbfs_attr_cacheremove(fdvp);
6336dae0c1eSTim J. Robbins 	smbfs_attr_cacheremove(tdvp);
634681a5bbeSBoris Popov out:
635afe09751SDavide Italiano 	smbfs_free_scred(scred);
636681a5bbeSBoris Popov 	if (tdvp == tvp)
637681a5bbeSBoris Popov 		vrele(tdvp);
638681a5bbeSBoris Popov 	else
639681a5bbeSBoris Popov 		vput(tdvp);
640681a5bbeSBoris Popov 	if (tvp)
641681a5bbeSBoris Popov 		vput(tvp);
642681a5bbeSBoris Popov 	vrele(fdvp);
643681a5bbeSBoris Popov 	vrele(fvp);
644681a5bbeSBoris Popov #ifdef possible_mistake
645681a5bbeSBoris Popov 	vgone(fvp);
646681a5bbeSBoris Popov 	if (tvp)
647681a5bbeSBoris Popov 		vgone(tvp);
648681a5bbeSBoris Popov #endif
649681a5bbeSBoris Popov 	return error;
650681a5bbeSBoris Popov }
651681a5bbeSBoris Popov 
6528e67c454STim J. Robbins /*
6538e67c454STim J. Robbins  * somtime it will come true...
6548e67c454STim J. Robbins  */
6558e67c454STim J. Robbins static int
656*b09b03a1SMateusz Guzik smbfs_link(struct vop_link_args *ap)
6578e67c454STim J. Robbins {
6588e67c454STim J. Robbins 	return EOPNOTSUPP;
6598e67c454STim J. Robbins }
6608e67c454STim J. Robbins 
6618e67c454STim J. Robbins /*
6628e67c454STim J. Robbins  * smbfs_symlink link create call.
6638e67c454STim J. Robbins  * Sometime it will be functional...
6648e67c454STim J. Robbins  */
6658e67c454STim J. Robbins static int
666*b09b03a1SMateusz Guzik smbfs_symlink(struct vop_symlink_args *ap)
6678e67c454STim J. Robbins {
6688e67c454STim J. Robbins 	return EOPNOTSUPP;
6698e67c454STim J. Robbins }
6708e67c454STim J. Robbins 
6718e67c454STim J. Robbins static int
672*b09b03a1SMateusz Guzik smbfs_mknod(struct vop_mknod_args *ap)
6738e67c454STim J. Robbins {
6748e67c454STim J. Robbins 	return EOPNOTSUPP;
6758e67c454STim J. Robbins }
6768e67c454STim J. Robbins 
677681a5bbeSBoris Popov static int
678*b09b03a1SMateusz Guzik smbfs_mkdir(struct vop_mkdir_args *ap)
679681a5bbeSBoris Popov {
680681a5bbeSBoris Popov 	struct vnode *dvp = ap->a_dvp;
681681a5bbeSBoris Popov /*	struct vattr *vap = ap->a_vap;*/
682681a5bbeSBoris Popov 	struct vnode *vp;
683681a5bbeSBoris Popov 	struct componentname *cnp = ap->a_cnp;
684681a5bbeSBoris Popov 	struct smbnode *dnp = VTOSMB(dvp);
685681a5bbeSBoris Popov 	struct vattr vattr;
686afe09751SDavide Italiano 	struct smb_cred *scred;
687681a5bbeSBoris Popov 	struct smbfattr fattr;
688681a5bbeSBoris Popov 	char *name = cnp->cn_nameptr;
689681a5bbeSBoris Popov 	int len = cnp->cn_namelen;
690681a5bbeSBoris Popov 	int error;
691681a5bbeSBoris Popov 
6920359a12eSAttilio Rao 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred))) {
693681a5bbeSBoris Popov 		return error;
694681a5bbeSBoris Popov 	}
695681a5bbeSBoris Popov 	if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.'))))
696681a5bbeSBoris Popov 		return EEXIST;
697afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
698b4a58fbfSMateusz Guzik 	smb_makescred(scred, curthread, cnp->cn_cred);
699afe09751SDavide Italiano 	error = smbfs_smb_mkdir(dnp, name, len, scred);
700681a5bbeSBoris Popov 	if (error)
701afe09751SDavide Italiano 		goto out;
702afe09751SDavide Italiano 	error = smbfs_smb_lookup(dnp, name, len, &fattr, scred);
703681a5bbeSBoris Popov 	if (error)
704afe09751SDavide Italiano 		goto out;
705681a5bbeSBoris Popov 	error = smbfs_nget(VTOVFS(dvp), dvp, name, len, &fattr, &vp);
706681a5bbeSBoris Popov 	if (error)
707afe09751SDavide Italiano 		goto out;
708681a5bbeSBoris Popov 	*ap->a_vpp = vp;
709afe09751SDavide Italiano out:
710afe09751SDavide Italiano 	smbfs_free_scred(scred);
711e346bd81SDavide Italiano 	return error;
712681a5bbeSBoris Popov }
713681a5bbeSBoris Popov 
714681a5bbeSBoris Popov /*
715681a5bbeSBoris Popov  * smbfs_remove directory call
716681a5bbeSBoris Popov  */
717681a5bbeSBoris Popov static int
718*b09b03a1SMateusz Guzik smbfs_rmdir(struct vop_rmdir_args *ap)
719681a5bbeSBoris Popov {
720681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
721681a5bbeSBoris Popov 	struct vnode *dvp = ap->a_dvp;
722681a5bbeSBoris Popov 	struct componentname *cnp = ap->a_cnp;
723681a5bbeSBoris Popov /*	struct smbmount *smp = VTOSMBFS(vp);*/
724681a5bbeSBoris Popov 	struct smbnode *dnp = VTOSMB(dvp);
725681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
726afe09751SDavide Italiano 	struct smb_cred *scred;
727681a5bbeSBoris Popov 	int error;
728681a5bbeSBoris Popov 
729681a5bbeSBoris Popov 	if (dvp == vp)
730681a5bbeSBoris Popov 		return EINVAL;
731681a5bbeSBoris Popov 
732afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
733b4a58fbfSMateusz Guzik 	smb_makescred(scred, curthread, cnp->cn_cred);
734afe09751SDavide Italiano 	error = smbfs_smb_rmdir(np, scred);
735b4484bf0STim J. Robbins 	if (error == 0)
736b4484bf0STim J. Robbins 		np->n_flag |= NGONE;
737681a5bbeSBoris Popov 	dnp->n_flag |= NMODIFIED;
738681a5bbeSBoris Popov 	smbfs_attr_cacheremove(dvp);
739681a5bbeSBoris Popov /*	cache_purge(dvp);*/
740681a5bbeSBoris Popov 	cache_purge(vp);
741afe09751SDavide Italiano 	smbfs_free_scred(scred);
742681a5bbeSBoris Popov 	return error;
743681a5bbeSBoris Popov }
744681a5bbeSBoris Popov 
745681a5bbeSBoris Popov /*
746681a5bbeSBoris Popov  * smbfs_readdir call
747681a5bbeSBoris Popov  */
748681a5bbeSBoris Popov static int
749*b09b03a1SMateusz Guzik smbfs_readdir(struct vop_readdir_args *ap)
750681a5bbeSBoris Popov {
751681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
752681a5bbeSBoris Popov 	struct uio *uio = ap->a_uio;
753681a5bbeSBoris Popov 	int error;
754681a5bbeSBoris Popov 
755681a5bbeSBoris Popov 	if (vp->v_type != VDIR)
756681a5bbeSBoris Popov 		return (EPERM);
757681a5bbeSBoris Popov #ifdef notnow
758681a5bbeSBoris Popov 	if (ap->a_ncookies) {
759681a5bbeSBoris Popov 		printf("smbfs_readdir: no support for cookies now...");
760681a5bbeSBoris Popov 		return (EOPNOTSUPP);
761681a5bbeSBoris Popov 	}
762681a5bbeSBoris Popov #endif
763681a5bbeSBoris Popov 	error = smbfs_readvnode(vp, uio, ap->a_cred);
764681a5bbeSBoris Popov 	return error;
765681a5bbeSBoris Popov }
766681a5bbeSBoris Popov 
767681a5bbeSBoris Popov /* ARGSUSED */
768681a5bbeSBoris Popov static int
769*b09b03a1SMateusz Guzik smbfs_fsync(struct vop_fsync_args *ap)
770681a5bbeSBoris Popov {
771b1c996c4SBoris Popov /*	return (smb_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));*/
772681a5bbeSBoris Popov     return (0);
773681a5bbeSBoris Popov }
774681a5bbeSBoris Popov 
775681a5bbeSBoris Popov static
776*b09b03a1SMateusz Guzik int smbfs_print(struct vop_print_args *ap)
777681a5bbeSBoris Popov {
778681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
779681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
780681a5bbeSBoris Popov 
781681a5bbeSBoris Popov 	if (np == NULL) {
782681a5bbeSBoris Popov 		printf("no smbnode data\n");
783681a5bbeSBoris Popov 		return (0);
784681a5bbeSBoris Popov 	}
7852a4ad258STim J. Robbins 	printf("\tname = %s, parent = %p, open = %d\n", np->n_name,
7862a4ad258STim J. Robbins 	    np->n_parent ? np->n_parent : NULL, (np->n_flag & NOPEN) != 0);
787681a5bbeSBoris Popov 	return (0);
788681a5bbeSBoris Popov }
789681a5bbeSBoris Popov 
790681a5bbeSBoris Popov static int
791*b09b03a1SMateusz Guzik smbfs_pathconf(struct vop_pathconf_args *ap)
792681a5bbeSBoris Popov {
793681a5bbeSBoris Popov 	struct smbmount *smp = VFSTOSMBFS(VTOVFS(ap->a_vp));
794681a5bbeSBoris Popov 	struct smb_vc *vcp = SSTOVC(smp->sm_share);
795b1288166SJohn Baldwin 	long *retval = ap->a_retval;
796681a5bbeSBoris Popov 	int error = 0;
797681a5bbeSBoris Popov 
798681a5bbeSBoris Popov 	switch (ap->a_name) {
7994a627952SJohn Baldwin 	    case _PC_FILESIZEBITS:
8004a627952SJohn Baldwin 		if (vcp->vc_sopt.sv_caps & (SMB_CAP_LARGE_READX |
8014a627952SJohn Baldwin 		    SMB_CAP_LARGE_WRITEX))
8024a627952SJohn Baldwin 		    *retval = 64;
8034a627952SJohn Baldwin 		else
8044a627952SJohn Baldwin 		    *retval = 32;
805681a5bbeSBoris Popov 		break;
806681a5bbeSBoris Popov 	    case _PC_NAME_MAX:
8073419dc99SBoris Popov 		*retval = (vcp->vc_hflags2 & SMB_FLAGS2_KNOWS_LONG_NAMES) ? 255 : 12;
808681a5bbeSBoris Popov 		break;
809681a5bbeSBoris Popov 	    case _PC_PATH_MAX:
810681a5bbeSBoris Popov 		*retval = 800;	/* XXX: a correct one ? */
811681a5bbeSBoris Popov 		break;
8124a627952SJohn Baldwin 	    case _PC_NO_TRUNC:
8134a627952SJohn Baldwin 		*retval = 1;
8144a627952SJohn Baldwin 		break;
815681a5bbeSBoris Popov 	    default:
81615a88f81SJohn Baldwin 		error = vop_stdpathconf(ap);
817681a5bbeSBoris Popov 	}
818681a5bbeSBoris Popov 	return error;
819681a5bbeSBoris Popov }
820681a5bbeSBoris Popov 
821681a5bbeSBoris Popov static int
822*b09b03a1SMateusz Guzik smbfs_strategy(struct vop_strategy_args *ap)
823681a5bbeSBoris Popov {
824681a5bbeSBoris Popov 	struct buf *bp=ap->a_bp;
825681a5bbeSBoris Popov 	struct ucred *cr;
826b1c996c4SBoris Popov 	struct thread *td;
827681a5bbeSBoris Popov 
828681a5bbeSBoris Popov 	SMBVDEBUG("\n");
829681a5bbeSBoris Popov 	if (bp->b_flags & B_ASYNC)
830b1c996c4SBoris Popov 		td = (struct thread *)0;
831681a5bbeSBoris Popov 	else
832b1c996c4SBoris Popov 		td = curthread;	/* XXX */
833681a5bbeSBoris Popov 	if (bp->b_iocmd == BIO_READ)
834681a5bbeSBoris Popov 		cr = bp->b_rcred;
835681a5bbeSBoris Popov 	else
836681a5bbeSBoris Popov 		cr = bp->b_wcred;
837681a5bbeSBoris Popov 
838681a5bbeSBoris Popov 	if ((bp->b_flags & B_ASYNC) == 0 )
83913fd4d21SBjoern A. Zeeb 		(void)smbfs_doio(ap->a_vp, bp, cr, td);
8400da50f6eSEdward Tomasz Napierala 	return (0);
841681a5bbeSBoris Popov }
842681a5bbeSBoris Popov 
8438e67c454STim J. Robbins int
844*b09b03a1SMateusz Guzik smbfs_ioctl(struct vop_ioctl_args *ap)
8458e67c454STim J. Robbins {
8468e67c454STim J. Robbins 	return ENOTTY;
8478e67c454STim J. Robbins }
8488e67c454STim J. Robbins 
849681a5bbeSBoris Popov static char smbfs_atl[] = "rhsvda";
850681a5bbeSBoris Popov static int
851681a5bbeSBoris Popov smbfs_getextattr(struct vop_getextattr_args *ap)
852681a5bbeSBoris Popov /* {
853681a5bbeSBoris Popov         IN struct vnode *a_vp;
854681a5bbeSBoris Popov         IN char *a_name;
855681a5bbeSBoris Popov         INOUT struct uio *a_uio;
856681a5bbeSBoris Popov         IN struct ucred *a_cred;
857b1c996c4SBoris Popov         IN struct thread *a_td;
858681a5bbeSBoris Popov };
859681a5bbeSBoris Popov */
860681a5bbeSBoris Popov {
861681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
862b1c996c4SBoris Popov 	struct thread *td = ap->a_td;
863681a5bbeSBoris Popov 	struct ucred *cred = ap->a_cred;
864681a5bbeSBoris Popov 	struct uio *uio = ap->a_uio;
865681a5bbeSBoris Popov 	const char *name = ap->a_name;
866681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
867681a5bbeSBoris Popov 	struct vattr vattr;
868681a5bbeSBoris Popov 	char buf[10];
869681a5bbeSBoris Popov 	int i, attr, error;
870681a5bbeSBoris Popov 
871b1c996c4SBoris Popov 	error = VOP_ACCESS(vp, VREAD, cred, td);
872681a5bbeSBoris Popov 	if (error)
873681a5bbeSBoris Popov 		return error;
8740359a12eSAttilio Rao 	error = VOP_GETATTR(vp, &vattr, cred);
875681a5bbeSBoris Popov 	if (error)
876681a5bbeSBoris Popov 		return error;
877681a5bbeSBoris Popov 	if (strcmp(name, "dosattr") == 0) {
878681a5bbeSBoris Popov 		attr = np->n_dosattr;
879681a5bbeSBoris Popov 		for (i = 0; i < 6; i++, attr >>= 1)
880681a5bbeSBoris Popov 			buf[i] = (attr & 1) ? smbfs_atl[i] : '-';
881681a5bbeSBoris Popov 		buf[i] = 0;
882681a5bbeSBoris Popov 		error = uiomove(buf, i, uio);
883681a5bbeSBoris Popov 	} else
884681a5bbeSBoris Popov 		error = EINVAL;
885681a5bbeSBoris Popov 	return error;
886681a5bbeSBoris Popov }
887681a5bbeSBoris Popov 
888681a5bbeSBoris Popov /*
889681a5bbeSBoris Popov  * Since we expected to support F_GETLK (and SMB protocol has no such function),
890681a5bbeSBoris Popov  * it is necessary to use lf_advlock(). It would be nice if this function had
891681a5bbeSBoris Popov  * a callback mechanism because it will help to improve a level of consistency.
892681a5bbeSBoris Popov  */
893681a5bbeSBoris Popov int
894*b09b03a1SMateusz Guzik smbfs_advlock(struct vop_advlock_args *ap)
895681a5bbeSBoris Popov {
896681a5bbeSBoris Popov 	struct vnode *vp = ap->a_vp;
897681a5bbeSBoris Popov 	struct smbnode *np = VTOSMB(vp);
898681a5bbeSBoris Popov 	struct flock *fl = ap->a_fl;
899681a5bbeSBoris Popov 	caddr_t id = (caddr_t)1 /* ap->a_id */;
900681a5bbeSBoris Popov /*	int flags = ap->a_flags;*/
901b1c996c4SBoris Popov 	struct thread *td = curthread;
902afe09751SDavide Italiano 	struct smb_cred *scred;
90347790174SAndrey A. Chernov 	u_quad_t size;
904fcbe9614SAndrey A. Chernov 	off_t start, end, oadd;
905681a5bbeSBoris Popov 	int error, lkop;
906681a5bbeSBoris Popov 
907681a5bbeSBoris Popov 	if (vp->v_type == VDIR) {
908681a5bbeSBoris Popov 		/*
909681a5bbeSBoris Popov 		 * SMB protocol have no support for directory locking.
910681a5bbeSBoris Popov 		 * Although locks can be processed on local machine, I don't
911681a5bbeSBoris Popov 		 * think that this is a good idea, because some programs
912681a5bbeSBoris Popov 		 * can work wrong assuming directory is locked. So, we just
913681a5bbeSBoris Popov 		 * return 'operation not supported
914681a5bbeSBoris Popov 		 */
915681a5bbeSBoris Popov 		 return EOPNOTSUPP;
916681a5bbeSBoris Popov 	}
917681a5bbeSBoris Popov 	size = np->n_size;
918681a5bbeSBoris Popov 	switch (fl->l_whence) {
919681a5bbeSBoris Popov 	case SEEK_SET:
920681a5bbeSBoris Popov 	case SEEK_CUR:
921681a5bbeSBoris Popov 		start = fl->l_start;
922681a5bbeSBoris Popov 		break;
92315924778SAndrey A. Chernov 
924681a5bbeSBoris Popov 	case SEEK_END:
92515924778SAndrey A. Chernov 		if (size > OFF_MAX ||
92615924778SAndrey A. Chernov 		    (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
92747790174SAndrey A. Chernov 			return EOVERFLOW;
92815924778SAndrey A. Chernov 		start = size + fl->l_start;
929bbf6984cSAndrey A. Chernov 		break;
93015924778SAndrey A. Chernov 
931681a5bbeSBoris Popov 	default:
932681a5bbeSBoris Popov 		return EINVAL;
933681a5bbeSBoris Popov 	}
934681a5bbeSBoris Popov 	if (start < 0)
935681a5bbeSBoris Popov 		return EINVAL;
936e3e2c03dSAndrey A. Chernov 	if (fl->l_len < 0) {
937ea4313e3SAndrey A. Chernov 		if (start == 0)
938e3e2c03dSAndrey A. Chernov 			return EINVAL;
939e3e2c03dSAndrey A. Chernov 		end = start - 1;
940ea4313e3SAndrey A. Chernov 		start += fl->l_len;
941ea4313e3SAndrey A. Chernov 		if (start < 0)
942ea4313e3SAndrey A. Chernov 			return EINVAL;
943e3e2c03dSAndrey A. Chernov 	} else if (fl->l_len == 0)
944681a5bbeSBoris Popov 		end = -1;
945681a5bbeSBoris Popov 	else {
946fcbe9614SAndrey A. Chernov 		oadd = fl->l_len - 1;
94747790174SAndrey A. Chernov 		if (oadd > OFF_MAX - start)
94847790174SAndrey A. Chernov 			return EOVERFLOW;
94947790174SAndrey A. Chernov 		end = start + oadd;
950681a5bbeSBoris Popov 	}
951afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
952afe09751SDavide Italiano 	smb_makescred(scred, td, td->td_ucred);
953681a5bbeSBoris Popov 	switch (ap->a_op) {
954681a5bbeSBoris Popov 	    case F_SETLK:
955681a5bbeSBoris Popov 		switch (fl->l_type) {
956681a5bbeSBoris Popov 		    case F_WRLCK:
957681a5bbeSBoris Popov 			lkop = SMB_LOCK_EXCL;
958681a5bbeSBoris Popov 			break;
959681a5bbeSBoris Popov 		    case F_RDLCK:
960681a5bbeSBoris Popov 			lkop = SMB_LOCK_SHARED;
961681a5bbeSBoris Popov 			break;
962681a5bbeSBoris Popov 		    case F_UNLCK:
963681a5bbeSBoris Popov 			lkop = SMB_LOCK_RELEASE;
964681a5bbeSBoris Popov 			break;
965681a5bbeSBoris Popov 		    default:
966afe09751SDavide Italiano 			smbfs_free_scred(scred);
967681a5bbeSBoris Popov 			return EINVAL;
968681a5bbeSBoris Popov 		}
969eab626f1SKonstantin Belousov 		error = lf_advlock(ap, &vp->v_lockf, size);
970681a5bbeSBoris Popov 		if (error)
971681a5bbeSBoris Popov 			break;
972681a5bbeSBoris Popov 		lkop = SMB_LOCK_EXCL;
973afe09751SDavide Italiano 		error = smbfs_smb_lock(np, lkop, id, start, end, scred);
974681a5bbeSBoris Popov 		if (error) {
97518121c17SDoug Rabson 			int oldtype = fl->l_type;
97618121c17SDoug Rabson 			fl->l_type = F_UNLCK;
977681a5bbeSBoris Popov 			ap->a_op = F_UNLCK;
978eab626f1SKonstantin Belousov 			lf_advlock(ap, &vp->v_lockf, size);
97918121c17SDoug Rabson 			fl->l_type = oldtype;
980681a5bbeSBoris Popov 		}
981681a5bbeSBoris Popov 		break;
982681a5bbeSBoris Popov 	    case F_UNLCK:
983eab626f1SKonstantin Belousov 		lf_advlock(ap, &vp->v_lockf, size);
984afe09751SDavide Italiano 		error = smbfs_smb_lock(np, SMB_LOCK_RELEASE, id, start, end, scred);
985681a5bbeSBoris Popov 		break;
986681a5bbeSBoris Popov 	    case F_GETLK:
987eab626f1SKonstantin Belousov 		error = lf_advlock(ap, &vp->v_lockf, size);
988681a5bbeSBoris Popov 		break;
989681a5bbeSBoris Popov 	    default:
990afe09751SDavide Italiano 		smbfs_free_scred(scred);
991681a5bbeSBoris Popov 		return EINVAL;
992681a5bbeSBoris Popov 	}
993afe09751SDavide Italiano 	smbfs_free_scred(scred);
994681a5bbeSBoris Popov 	return error;
995681a5bbeSBoris Popov }
996681a5bbeSBoris Popov 
997681a5bbeSBoris Popov static int
998681a5bbeSBoris Popov smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop)
999681a5bbeSBoris Popov {
100052b2c8e2SOleksandr Tymoshenko 	static const char *badchars = "*/:<>?";
100152b2c8e2SOleksandr Tymoshenko 	static const char *badchars83 = " +|,[]=;";
1002681a5bbeSBoris Popov 	const char *cp;
1003681a5bbeSBoris Popov 	int i, error;
1004681a5bbeSBoris Popov 
100572f6a0faSColin Percival 	/*
100672f6a0faSColin Percival 	 * Backslash characters, being a path delimiter, are prohibited
100772f6a0faSColin Percival 	 * within a path component even for LOOKUP operations.
100872f6a0faSColin Percival 	 */
1009dc15eac0SEd Schouten 	if (strchr(name, '\\') != NULL)
101072f6a0faSColin Percival 		return ENOENT;
101172f6a0faSColin Percival 
1012681a5bbeSBoris Popov 	if (nameiop == LOOKUP)
1013681a5bbeSBoris Popov 		return 0;
1014681a5bbeSBoris Popov 	error = ENOENT;
1015681a5bbeSBoris Popov 	if (SMB_DIALECT(SSTOVC(smp->sm_share)) < SMB_DIALECT_LANMAN2_0) {
1016681a5bbeSBoris Popov 		/*
1017681a5bbeSBoris Popov 		 * Name should conform 8.3 format
1018681a5bbeSBoris Popov 		 */
1019681a5bbeSBoris Popov 		if (nmlen > 12)
1020681a5bbeSBoris Popov 			return ENAMETOOLONG;
1021dc15eac0SEd Schouten 		cp = strchr(name, '.');
1022681a5bbeSBoris Popov 		if (cp == NULL)
1023681a5bbeSBoris Popov 			return error;
1024681a5bbeSBoris Popov 		if (cp == name || (cp - name) > 8)
1025681a5bbeSBoris Popov 			return error;
1026dc15eac0SEd Schouten 		cp = strchr(cp + 1, '.');
1027681a5bbeSBoris Popov 		if (cp != NULL)
1028681a5bbeSBoris Popov 			return error;
1029681a5bbeSBoris Popov 		for (cp = name, i = 0; i < nmlen; i++, cp++)
1030dc15eac0SEd Schouten 			if (strchr(badchars83, *cp) != NULL)
1031681a5bbeSBoris Popov 				return error;
1032681a5bbeSBoris Popov 	}
1033681a5bbeSBoris Popov 	for (cp = name, i = 0; i < nmlen; i++, cp++)
1034dc15eac0SEd Schouten 		if (strchr(badchars, *cp) != NULL)
1035681a5bbeSBoris Popov 			return error;
1036681a5bbeSBoris Popov 	return 0;
1037681a5bbeSBoris Popov }
1038681a5bbeSBoris Popov 
1039681a5bbeSBoris Popov /*
1040681a5bbeSBoris Popov  * Things go even weird without fixed inode numbers...
1041681a5bbeSBoris Popov  */
1042681a5bbeSBoris Popov int
1043*b09b03a1SMateusz Guzik smbfs_lookup(struct vop_lookup_args *ap)
1044681a5bbeSBoris Popov {
1045681a5bbeSBoris Popov 	struct componentname *cnp = ap->a_cnp;
1046b4a58fbfSMateusz Guzik 	struct thread *td = curthread;
1047681a5bbeSBoris Popov 	struct vnode *dvp = ap->a_dvp;
1048681a5bbeSBoris Popov 	struct vnode **vpp = ap->a_vpp;
1049681a5bbeSBoris Popov 	struct vnode *vp;
1050681a5bbeSBoris Popov 	struct smbmount *smp;
1051681a5bbeSBoris Popov 	struct mount *mp = dvp->v_mount;
1052681a5bbeSBoris Popov 	struct smbnode *dnp;
1053681a5bbeSBoris Popov 	struct smbfattr fattr, *fap;
1054afe09751SDavide Italiano 	struct smb_cred *scred;
1055681a5bbeSBoris Popov 	char *name = cnp->cn_nameptr;
1056681a5bbeSBoris Popov 	int flags = cnp->cn_flags;
1057681a5bbeSBoris Popov 	int nameiop = cnp->cn_nameiop;
1058681a5bbeSBoris Popov 	int nmlen = cnp->cn_namelen;
1059da1c9cb2SJeff Roberson 	int error, islastcn, isdot;
1060b4484bf0STim J. Robbins 	int killit;
1061681a5bbeSBoris Popov 
1062681a5bbeSBoris Popov 	SMBVDEBUG("\n");
1063681a5bbeSBoris Popov 	if (dvp->v_type != VDIR)
1064681a5bbeSBoris Popov 		return ENOTDIR;
1065e6e370a7SJeff Roberson 	if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) {
1066681a5bbeSBoris Popov 		SMBFSERR("invalid '..'\n");
1067681a5bbeSBoris Popov 		return EIO;
1068681a5bbeSBoris Popov 	}
1069681a5bbeSBoris Popov 	islastcn = flags & ISLASTCN;
1070681a5bbeSBoris Popov 	if (islastcn && (mp->mnt_flag & MNT_RDONLY) && (nameiop != LOOKUP))
1071681a5bbeSBoris Popov 		return EROFS;
10726a5abb1eSKyle Evans 	error = vn_dir_check_exec(dvp, cnp);
10736a5abb1eSKyle Evans 	if (error != 0)
1074681a5bbeSBoris Popov 		return error;
1075681a5bbeSBoris Popov 	smp = VFSTOSMBFS(mp);
1076681a5bbeSBoris Popov 	dnp = VTOSMB(dvp);
1077681a5bbeSBoris Popov 	isdot = (nmlen == 1 && name[0] == '.');
1078681a5bbeSBoris Popov 
1079681a5bbeSBoris Popov 	error = smbfs_pathcheck(smp, cnp->cn_nameptr, cnp->cn_namelen, nameiop);
1080681a5bbeSBoris Popov 
1081681a5bbeSBoris Popov 	if (error)
1082681a5bbeSBoris Popov 		return ENOENT;
1083681a5bbeSBoris Popov 
1084bf40d24aSJohn Baldwin 	error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
1085681a5bbeSBoris Popov 	SMBVDEBUG("cache_lookup returned %d\n", error);
1086681a5bbeSBoris Popov 	if (error > 0)
1087681a5bbeSBoris Popov 		return error;
1088681a5bbeSBoris Popov 	if (error) {		/* name was found */
1089681a5bbeSBoris Popov 		struct vattr vattr;
1090681a5bbeSBoris Popov 
1091b4484bf0STim J. Robbins 		killit = 0;
1092e8943128SXin LI 		vp = *vpp;
10930359a12eSAttilio Rao 		error = VOP_GETATTR(vp, &vattr, cnp->cn_cred);
1094b4484bf0STim J. Robbins 		/*
1095b4484bf0STim J. Robbins 		 * If the file type on the server is inconsistent
1096b4484bf0STim J. Robbins 		 * with what it was when we created the vnode,
1097b4484bf0STim J. Robbins 		 * kill the bogus vnode now and fall through to
1098b4484bf0STim J. Robbins 		 * the code below to create a new one with the
1099b4484bf0STim J. Robbins 		 * right type.
1100b4484bf0STim J. Robbins 		 */
1101b4484bf0STim J. Robbins 		if (error == 0 &&
1102b4484bf0STim J. Robbins 		   ((vp->v_type == VDIR &&
1103b4484bf0STim J. Robbins 		   (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) == 0) ||
1104b4484bf0STim J. Robbins 		   (vp->v_type == VREG &&
1105b4484bf0STim J. Robbins 		   (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) != 0)))
1106b4484bf0STim J. Robbins 		   killit = 1;
1107b4484bf0STim J. Robbins 		else if (error == 0
1108681a5bbeSBoris Popov 	     /*    && vattr.va_ctime.tv_sec == VTOSMB(vp)->n_ctime*/) {
1109681a5bbeSBoris Popov 		     SMBVDEBUG("use cached vnode\n");
1110681a5bbeSBoris Popov 		     return (0);
1111681a5bbeSBoris Popov 		}
1112681a5bbeSBoris Popov 		cache_purge(vp);
1113f6576f19SJeff Roberson 		/*
1114f6576f19SJeff Roberson 		 * XXX This is not quite right, if '.' is
1115f6576f19SJeff Roberson 		 * inconsistent, we really need to start the lookup
1116f6576f19SJeff Roberson 		 * all over again.  Hopefully there is some other
1117f6576f19SJeff Roberson 		 * guarantee that prevents this case from happening.
1118f6576f19SJeff Roberson 		 */
1119f6576f19SJeff Roberson 		if (killit && vp != dvp)
1120b4484bf0STim J. Robbins 			vgone(vp);
1121f6576f19SJeff Roberson 		if (vp != dvp)
11228da00465SJeff Roberson 			vput(vp);
1123f6576f19SJeff Roberson 		else
1124f6576f19SJeff Roberson 			vrele(vp);
1125681a5bbeSBoris Popov 		*vpp = NULLVP;
1126681a5bbeSBoris Popov 	}
1127681a5bbeSBoris Popov 	/*
1128681a5bbeSBoris Popov 	 * entry is not in the cache or has been expired
1129681a5bbeSBoris Popov 	 */
1130681a5bbeSBoris Popov 	error = 0;
1131681a5bbeSBoris Popov 	*vpp = NULLVP;
1132afe09751SDavide Italiano 	scred = smbfs_malloc_scred();
1133afe09751SDavide Italiano 	smb_makescred(scred, td, cnp->cn_cred);
1134681a5bbeSBoris Popov 	fap = &fattr;
1135681a5bbeSBoris Popov 	if (flags & ISDOTDOT) {
1136ce589ae2SDavide Italiano 		/*
1137ce589ae2SDavide Italiano 		 * In the DOTDOT case, don't go over-the-wire
1138ce589ae2SDavide Italiano 		 * in order to request attributes. We already
1139ce589ae2SDavide Italiano 		 * know it's a directory and subsequent call to
1140ce589ae2SDavide Italiano 		 * smbfs_getattr() will restore consistency.
1141ce589ae2SDavide Italiano 		 *
1142ce589ae2SDavide Italiano 		 */
1143ce589ae2SDavide Italiano 		SMBVDEBUG("smbfs_smb_lookup: dotdot\n");
1144ce589ae2SDavide Italiano 	} else if (isdot) {
1145ce589ae2SDavide Italiano 		error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred);
1146ce589ae2SDavide Italiano 		SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1147ce589ae2SDavide Italiano 	}
1148ce589ae2SDavide Italiano 	else {
1149afe09751SDavide Italiano 		error = smbfs_smb_lookup(dnp, name, nmlen, fap, scred);
1150681a5bbeSBoris Popov 		SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1151681a5bbeSBoris Popov 	}
1152681a5bbeSBoris Popov 	if (error && error != ENOENT)
1153afe09751SDavide Italiano 		goto out;
1154681a5bbeSBoris Popov 	if (error) {			/* entry not found */
1155681a5bbeSBoris Popov 		/*
1156681a5bbeSBoris Popov 		 * Handle RENAME or CREATE case...
1157681a5bbeSBoris Popov 		 */
1158da1c9cb2SJeff Roberson 		if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
11596e8681aaSBoris Popov 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
11606e8681aaSBoris Popov 			if (error)
1161afe09751SDavide Italiano 				goto out;
1162afe09751SDavide Italiano 			error = EJUSTRETURN;
1163afe09751SDavide Italiano 			goto out;
1164681a5bbeSBoris Popov 		}
1165afe09751SDavide Italiano 		error = ENOENT;
1166afe09751SDavide Italiano 		goto out;
1167681a5bbeSBoris Popov 	}/* else {
1168681a5bbeSBoris Popov 		SMBVDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
1169681a5bbeSBoris Popov 	}*/
1170681a5bbeSBoris Popov 	/*
1171681a5bbeSBoris Popov 	 * handle DELETE case ...
1172681a5bbeSBoris Popov 	 */
1173681a5bbeSBoris Popov 	if (nameiop == DELETE && islastcn) { 	/* delete last component */
1174b1c996c4SBoris Popov 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1175681a5bbeSBoris Popov 		if (error)
1176afe09751SDavide Italiano 			goto out;
1177681a5bbeSBoris Popov 		if (isdot) {
1178681a5bbeSBoris Popov 			VREF(dvp);
1179681a5bbeSBoris Popov 			*vpp = dvp;
1180afe09751SDavide Italiano 			goto out;
1181681a5bbeSBoris Popov 		}
1182681a5bbeSBoris Popov 		error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1183681a5bbeSBoris Popov 		if (error)
1184afe09751SDavide Italiano 			goto out;
1185681a5bbeSBoris Popov 		*vpp = vp;
1186afe09751SDavide Italiano 		goto out;
1187681a5bbeSBoris Popov 	}
1188da1c9cb2SJeff Roberson 	if (nameiop == RENAME && islastcn) {
1189b1c996c4SBoris Popov 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1190681a5bbeSBoris Popov 		if (error)
1191afe09751SDavide Italiano 			goto out;
1192afe09751SDavide Italiano 		if (isdot) {
1193afe09751SDavide Italiano 			error = EISDIR;
1194afe09751SDavide Italiano 			goto out;
1195afe09751SDavide Italiano 		}
1196681a5bbeSBoris Popov 		error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1197681a5bbeSBoris Popov 		if (error)
1198afe09751SDavide Italiano 			goto out;
1199681a5bbeSBoris Popov 		*vpp = vp;
1200afe09751SDavide Italiano 		goto out;
1201681a5bbeSBoris Popov 	}
1202681a5bbeSBoris Popov 	if (flags & ISDOTDOT) {
12039dbe0b12SDavide Italiano 		mp = dvp->v_mount;
12049dbe0b12SDavide Italiano 		error = vfs_busy(mp, MBF_NOWAIT);
12059dbe0b12SDavide Italiano 		if (error != 0) {
12069dbe0b12SDavide Italiano 			vfs_ref(mp);
1207b249ce48SMateusz Guzik 			VOP_UNLOCK(dvp);
12089dbe0b12SDavide Italiano 			error = vfs_busy(mp, 0);
12099dbe0b12SDavide Italiano 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
12109dbe0b12SDavide Italiano 			vfs_rel(mp);
1211c7d2e4cfSDavide Italiano 			if (error) {
1212c7d2e4cfSDavide Italiano 				error = ENOENT;
1213c7d2e4cfSDavide Italiano 				goto out;
1214c7d2e4cfSDavide Italiano 			}
1215abd80ddbSMateusz Guzik 			if (VN_IS_DOOMED(dvp)) {
12169dbe0b12SDavide Italiano 				vfs_unbusy(mp);
1217c7d2e4cfSDavide Italiano 				error = ENOENT;
1218c7d2e4cfSDavide Italiano 				goto out;
12199dbe0b12SDavide Italiano 			}
12209dbe0b12SDavide Italiano 		}
1221b249ce48SMateusz Guzik 		VOP_UNLOCK(dvp);
1222681a5bbeSBoris Popov 		error = smbfs_nget(mp, dvp, name, nmlen, NULL, &vp);
12239dbe0b12SDavide Italiano 		vfs_unbusy(mp);
1224cb05b60aSAttilio Rao 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1225abd80ddbSMateusz Guzik 		if (VN_IS_DOOMED(dvp)) {
12269dbe0b12SDavide Italiano 			if (error == 0)
12279dbe0b12SDavide Italiano 				vput(vp);
12289dbe0b12SDavide Italiano 			error = ENOENT;
12299dbe0b12SDavide Italiano 		}
12304585e3acSJeff Roberson 		if (error)
1231afe09751SDavide Italiano 			goto out;
1232681a5bbeSBoris Popov 		*vpp = vp;
1233681a5bbeSBoris Popov 	} else if (isdot) {
1234681a5bbeSBoris Popov 		vref(dvp);
1235681a5bbeSBoris Popov 		*vpp = dvp;
1236681a5bbeSBoris Popov 	} else {
1237681a5bbeSBoris Popov 		error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1238681a5bbeSBoris Popov 		if (error)
1239afe09751SDavide Italiano 			goto out;
1240681a5bbeSBoris Popov 		*vpp = vp;
1241681a5bbeSBoris Popov 		SMBVDEBUG("lookup: getnewvp!\n");
1242681a5bbeSBoris Popov 	}
1243681a5bbeSBoris Popov 	if ((cnp->cn_flags & MAKEENTRY)/* && !islastcn*/) {
1244681a5bbeSBoris Popov /*		VTOSMB(*vpp)->n_ctime = VTOSMB(*vpp)->n_vattr.va_ctime.tv_sec;*/
1245681a5bbeSBoris Popov 		cache_enter(dvp, *vpp, cnp);
1246681a5bbeSBoris Popov 	}
1247afe09751SDavide Italiano out:
1248afe09751SDavide Italiano 	smbfs_free_scred(scred);
1249afe09751SDavide Italiano 	return (error);
1250681a5bbeSBoris Popov }
1251