1 /*	$NetBSD: smbfs_vnops.c,v 1.93 2014/12/21 10:48:53 hannken Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jaromir Dolecek.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 2000-2001 Boris Popov
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *    This product includes software developed by Boris Popov.
47  * 4. Neither the name of the author nor the names of any co-contributors
48  *    may be used to endorse or promote products derived from this software
49  *    without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  *
63  * FreeBSD: src/sys/fs/smbfs/smbfs_vnops.c,v 1.15 2001/12/20 15:56:45 bp Exp
64  */
65 
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: smbfs_vnops.c,v 1.93 2014/12/21 10:48:53 hannken Exp $");
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/namei.h>
72 #include <sys/kernel.h>
73 #include <sys/proc.h>
74 #include <sys/buf.h>
75 #include <sys/fcntl.h>
76 #include <sys/mount.h>
77 #include <sys/unistd.h>
78 #include <sys/vnode.h>
79 #include <sys/lockf.h>
80 #include <sys/kauth.h>
81 #include <sys/mallocvar.h>
82 
83 #include <machine/limits.h>
84 
85 #include <uvm/uvm.h>
86 #include <uvm/uvm_extern.h>
87 
88 #include <netsmb/smb.h>
89 #include <netsmb/smb_conn.h>
90 #include <netsmb/smb_subr.h>
91 
92 #include <fs/smbfs/smbfs.h>
93 #include <fs/smbfs/smbfs_node.h>
94 #include <fs/smbfs/smbfs_subr.h>
95 
96 #include <miscfs/genfs/genfs.h>
97 
98 /*
99  * Prototypes for SMBFS vnode operations
100  */
101 int smbfs_create(void *);
102 int smbfs_open(void *);
103 int smbfs_close(void *);
104 int smbfs_access(void *);
105 int smbfs_getattr(void *);
106 int smbfs_setattr(void *);
107 int smbfs_read(void *);
108 int smbfs_write(void *);
109 int smbfs_fsync(void *);
110 int smbfs_remove(void *);
111 int smbfs_link(void *);
112 int smbfs_lookup(void *);
113 int smbfs_rename(void *);
114 int smbfs_mkdir(void *);
115 int smbfs_rmdir(void *);
116 int smbfs_symlink(void *);
117 int smbfs_readdir(void *);
118 int smbfs_strategy(void *);
119 int smbfs_print(void *);
120 int smbfs_pathconf(void *ap);
121 int smbfs_advlock(void *);
122 
123 int (**smbfs_vnodeop_p)(void *);
124 static struct vnodeopv_entry_desc smbfs_vnodeop_entries[] = {
125 	{ &vop_default_desc,		vn_default_error },
126 	{ &vop_access_desc,		smbfs_access },
127 	{ &vop_advlock_desc,		smbfs_advlock },
128 	{ &vop_close_desc,		smbfs_close },
129 	{ &vop_create_desc,		smbfs_create },
130 	{ &vop_fallocate_desc,		genfs_eopnotsupp },
131 	{ &vop_fdiscard_desc,		genfs_eopnotsupp },
132 	{ &vop_fsync_desc,		smbfs_fsync },
133 	{ &vop_getattr_desc,		smbfs_getattr },
134 	{ &vop_getpages_desc,		genfs_compat_getpages },
135 	{ &vop_inactive_desc,		smbfs_inactive },
136 	{ &vop_ioctl_desc,		genfs_enoioctl },
137 	{ &vop_islocked_desc,		genfs_islocked },
138 	{ &vop_link_desc,		smbfs_link },
139 	{ &vop_lock_desc,		genfs_lock },
140 	{ &vop_lookup_desc,		smbfs_lookup },
141 	{ &vop_mkdir_desc,		smbfs_mkdir },
142 	{ &vop_mknod_desc,		genfs_eopnotsupp },
143 	{ &vop_open_desc,		smbfs_open },
144 	{ &vop_pathconf_desc,		smbfs_pathconf },
145 	{ &vop_print_desc,		smbfs_print },
146 	{ &vop_putpages_desc,		genfs_putpages },
147 	{ &vop_read_desc,		smbfs_read },
148 	{ &vop_readdir_desc,		smbfs_readdir },
149 	{ &vop_reclaim_desc,		smbfs_reclaim },
150 	{ &vop_remove_desc,		smbfs_remove },
151 	{ &vop_rename_desc,		smbfs_rename },
152 	{ &vop_rmdir_desc,		smbfs_rmdir },
153 	{ &vop_setattr_desc,		smbfs_setattr },
154 	{ &vop_strategy_desc,		smbfs_strategy },
155 	{ &vop_symlink_desc,		smbfs_symlink },
156 	{ &vop_unlock_desc,		genfs_unlock },
157 	{ &vop_write_desc,		smbfs_write },
158 	{ &vop_mmap_desc,		genfs_mmap },		/* mmap */
159 	{ &vop_seek_desc,		genfs_seek },		/* seek */
160 	{ &vop_kqfilter_desc,		smbfs_kqfilter},	/* kqfilter */
161 	{ NULL, NULL }
162 };
163 const struct vnodeopv_desc smbfs_vnodeop_opv_desc =
164 	{ &smbfs_vnodeop_p, smbfs_vnodeop_entries };
165 
166 static int
smbfs_check_possible(struct vnode * vp,struct smbnode * np,mode_t mode)167 smbfs_check_possible(struct vnode *vp, struct smbnode *np, mode_t mode)
168 {
169 
170         /*
171          * Disallow write attempts on read-only file systems;
172          * unless the file is a socket, fifo, or a block or
173          * character device resident on the file system.
174          */
175 	if (mode & VWRITE) {
176 		switch (vp->v_type) {
177 		case VREG:
178 		case VDIR:
179 		case VLNK:
180  			if (vp->v_mount->mnt_flag & MNT_RDONLY)
181 				return EROFS;
182 		default:
183 			break;
184 		}
185 	}
186 
187 	return 0;
188 }
189 
190 static int
smbfs_check_permitted(struct vnode * vp,struct smbnode * np,mode_t mode,kauth_cred_t cred)191 smbfs_check_permitted(struct vnode *vp, struct smbnode *np, mode_t mode,
192     kauth_cred_t cred)
193 {
194 	struct smbmount *smp = VTOSMBFS(vp);
195 	mode_t file_mode = (vp->v_type == VDIR) ? smp->sm_args.dir_mode :
196 	    smp->sm_args.file_mode;
197 
198 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode,
199 	    vp->v_type, file_mode), vp, NULL, genfs_can_access(vp->v_type,
200 	    file_mode, smp->sm_args.uid, smp->sm_args.gid, mode, cred));
201 }
202 
203 int
smbfs_access(void * v)204 smbfs_access(void *v)
205 {
206 	struct vop_access_args /* {
207 		struct vnode *a_vp;
208 		int  a_mode;
209 		kauth_cred_t a_cred;
210 	} */ *ap = v;
211 	struct vnode *vp = ap->a_vp;
212 	struct smbnode *np = VTOSMB(vp);
213 	u_int acc_mode = ap->a_mode;
214 	int error;
215 #ifdef SMB_VNODE_DEBUG
216 	struct smbmount *smp = VTOSMBFS(vp);
217 #endif
218 
219         SMBVDEBUG("file '%.*s', node mode=%o, acc mode=%x\n",
220 	    (int) np->n_nmlen, np->n_name,
221 	    (vp->v_type == VDIR) ? smp->sm_args.dir_mode : smp->sm_args.file_mode,
222 	    acc_mode);
223 
224 	error = smbfs_check_possible(vp, np, acc_mode);
225 	if (error)
226 		return error;
227 
228 	error = smbfs_check_permitted(vp, np, acc_mode, ap->a_cred);
229 
230 	return error;
231 }
232 
233 /* ARGSUSED */
234 int
smbfs_open(void * v)235 smbfs_open(void *v)
236 {
237 	struct vop_open_args /* {
238 		struct vnode *a_vp;
239 		int  a_mode;
240 		kauth_cred_t a_cred;
241 	} */ *ap = v;
242 	struct lwp *l = curlwp;
243 	struct vnode *vp = ap->a_vp;
244 	struct smbnode *np = VTOSMB(vp);
245 	struct smb_cred scred;
246 	struct vattr vattr;
247 	u_int32_t sv_caps = SMB_CAPS(SSTOVC(np->n_mount->sm_share));
248 	int error, accmode;
249 
250 	SMBVDEBUG("%.*s,%d\n", (int) np->n_nmlen, np->n_name,
251 	    (np->n_flag & NOPEN) != 0);
252 	if (vp->v_type != VREG && vp->v_type != VDIR) {
253 		SMBFSERR("open eacces vtype=%d\n", vp->v_type);
254 		return EACCES;
255 	}
256 	if (vp->v_type == VDIR) {
257 		if ((sv_caps & SMB_CAP_NT_SMBS) == 0) {
258 			np->n_flag |= NOPEN;
259 			return 0;
260 		}
261 		goto do_open;	/* skip 'modified' check */
262 	}
263 
264 	if (np->n_flag & NMODIFIED) {
265 		if ((error = smbfs_vinvalbuf(vp, V_SAVE, ap->a_cred, l, 1)) == EINTR)
266 			return error;
267 		smbfs_attr_cacheremove(vp);
268 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
269 		if (error)
270 			return error;
271 		np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
272 	} else {
273 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
274 		if (error)
275 			return error;
276 		if (np->n_mtime.tv_sec != vattr.va_mtime.tv_sec) {
277 			error = smbfs_vinvalbuf(vp, V_SAVE, ap->a_cred, l, 1);
278 			if (error == EINTR)
279 				return error;
280 			np->n_mtime.tv_sec = vattr.va_mtime.tv_sec;
281 		}
282 	}
283 
284 do_open:
285 	if ((np->n_flag & NOPEN) != 0)
286 		return 0;
287 
288 	smb_makescred(&scred, l, ap->a_cred);
289 	if (vp->v_type == VDIR)
290 		error = smbfs_smb_ntcreatex(np,
291 		    SMB_SM_DENYNONE|SMB_AM_OPENREAD, &scred);
292 	else {
293 		/*
294 		 * Use DENYNONE to give unixy semantics of permitting
295 		 * everything not forbidden by permissions.  Ie denial
296 		 * is up to server with clients/openers needing to use
297 		 * advisory locks for further control.
298 		 */
299 		accmode = SMB_SM_DENYNONE|SMB_AM_OPENREAD;
300 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
301 			accmode = SMB_SM_DENYNONE|SMB_AM_OPENRW;
302 		error = smbfs_smb_open(np, accmode, &scred);
303 		if (error) {
304 			if (ap->a_mode & FWRITE)
305 				return EACCES;
306 
307 			error = smbfs_smb_open(np,
308 			    SMB_SM_DENYNONE|SMB_AM_OPENREAD, &scred);
309 		}
310 	}
311 	if (!error)
312 		np->n_flag |= NOPEN;
313 	smbfs_attr_cacheremove(vp);
314 	return error;
315 }
316 
317 /*
318  * Close called.
319  */
320 int
smbfs_close(void * v)321 smbfs_close(void *v)
322 {
323 	struct vop_close_args /* {
324 		struct vnodeop_desc *a_desc;
325 		struct vnode *a_vp;
326 		int  a_fflag;
327 		kauth_cred_t a_cred;
328 	} */ *ap = v;
329 	int error;
330 	struct lwp *l = curlwp;
331 	struct vnode *vp = ap->a_vp;
332 	struct smbnode *np = VTOSMB(vp);
333 
334 	/* Flush all file data */
335 	error = smbfs_vinvalbuf(vp, V_SAVE, ap->a_cred, l, 1);
336 	if (error)
337 		return (error);
338 
339 	/*
340 	 * We must close the directory lookup context now, so that
341 	 * later directory changes would be properly detected.
342 	 * Ideally, the lookup routines should handle such case, and
343 	 * the context would be removed only in smbfs_inactive().
344 	 */
345 	if (vp->v_type == VDIR && (np->n_flag & NOPEN) != 0) {
346 		struct smb_share *ssp = np->n_mount->sm_share;
347 		struct smb_cred scred;
348 
349 		smb_makescred(&scred, l, ap->a_cred);
350 
351 		if (np->n_dirseq != NULL) {
352 			smbfs_findclose(np->n_dirseq, &scred);
353 			np->n_dirseq = NULL;
354 		}
355 
356 		if (SMB_CAPS(SSTOVC(ssp)) & SMB_CAP_NT_SMBS)
357 			smbfs_smb_close(ssp, np->n_fid, &np->n_mtime, &scred);
358 
359 		np->n_flag &= ~NOPEN;
360 		smbfs_attr_cacheremove(vp);
361 	}
362 
363 	return (0);
364 }
365 
366 /*
367  * smbfs_getattr call from vfs.
368  */
369 int
smbfs_getattr(void * v)370 smbfs_getattr(void *v)
371 {
372 	struct vop_getattr_args /* {
373 		struct vnode *a_vp;
374 		struct vattr *a_vap;
375 		kauth_cred_t a_cred;
376 	} */ *ap = v;
377 	struct vnode *vp = ap->a_vp;
378 	struct smbnode *np = VTOSMB(vp);
379 	struct vattr *va=ap->a_vap;
380 	struct smbfattr fattr;
381 	struct smb_cred scred;
382 	u_quad_t oldsize;
383 	int error;
384 
385 	SMBVDEBUG("%p: '%.*s' isroot %d\n", vp,
386 		(int) np->n_nmlen, np->n_name, (vp->v_vflag & VV_ROOT) != 0);
387 
388 	if ((error = smbfs_attr_cachelookup(vp, va)) == 0)
389 		return (0);
390 
391 	SMBVDEBUG0("not in the cache\n");
392 	smb_makescred(&scred, curlwp, ap->a_cred);
393 	oldsize = np->n_size;
394 	error = smbfs_smb_lookup(np, NULL, 0, &fattr, &scred);
395 	if (error) {
396 		SMBVDEBUG("error %d\n", error);
397 		return error;
398 	}
399 	smbfs_attr_cacheenter(vp, &fattr);
400 	smbfs_attr_cachelookup(vp, va);
401 	if ((np->n_flag & NOPEN) != 0)
402 		np->n_size = oldsize;
403 	return 0;
404 }
405 
406 int
smbfs_setattr(void * v)407 smbfs_setattr(void *v)
408 {
409 	struct vop_setattr_args /* {
410 		struct vnode *a_vp;
411 		struct vattr *a_vap;
412 		kauth_cred_t a_cred;
413 	} */ *ap = v;
414 	struct lwp *l = curlwp;
415 	struct vnode *vp = ap->a_vp;
416 	struct smbnode *np = VTOSMB(vp);
417 	struct vattr *vap = ap->a_vap;
418 	struct timespec *mtime, *atime;
419 	struct smb_cred scred;
420 	struct smb_share *ssp = np->n_mount->sm_share;
421 	struct smb_vc *vcp = SSTOVC(ssp);
422 	u_quad_t tsize = 0;
423 	int isreadonly, doclose, error = 0;
424 
425 	SMBVDEBUG0("\n");
426 	if (vap->va_flags != VNOVAL)
427 		return EOPNOTSUPP;
428 	isreadonly = (vp->v_mount->mnt_flag & MNT_RDONLY);
429 	/*
430 	 * Disallow write attempts if the filesystem is mounted read-only.
431 	 */
432   	if ((vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL ||
433 	     vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL ||
434 	     vap->va_mode != (mode_t)VNOVAL) && isreadonly)
435 		return EROFS;
436 	smb_makescred(&scred, l, ap->a_cred);
437 	if (vap->va_size != VNOVAL) {
438  		switch (vp->v_type) {
439  		case VDIR:
440  			return EISDIR;
441  		case VREG:
442 			break;
443  		default:
444 			return EINVAL;
445   		};
446 		if (isreadonly)
447 			return EROFS;
448 		doclose = 0;
449  		tsize = np->n_size;
450  		np->n_size = vap->va_size;
451 		uvm_vnp_setsize(vp, vap->va_size);
452 		if ((np->n_flag & NOPEN) == 0) {
453 			error = smbfs_smb_open(np,
454 			    SMB_SM_DENYNONE|SMB_AM_OPENRW, &scred);
455 			if (error == 0)
456 				doclose = 1;
457 		}
458 		if (error == 0)
459 			error = smbfs_smb_setfsize(np, vap->va_size, &scred);
460 		if (doclose)
461 			smbfs_smb_close(ssp, np->n_fid, NULL, &scred);
462 		if (error) {
463 			np->n_size = tsize;
464 			uvm_vnp_setsize(vp, tsize);
465 			return (error);
466 		}
467   	}
468 	mtime = atime = NULL;
469 	if (vap->va_mtime.tv_sec != VNOVAL)
470 		mtime = &vap->va_mtime;
471 	if (vap->va_atime.tv_sec != VNOVAL)
472 		atime = &vap->va_atime;
473 	if (mtime != atime) {
474 		error = kauth_authorize_vnode(ap->a_cred,
475 		    KAUTH_VNODE_WRITE_TIMES, ap->a_vp, NULL,
476 		    genfs_can_chtimes(ap->a_vp, vap->va_vaflags,
477 		    VTOSMBFS(vp)->sm_args.uid, ap->a_cred));
478 		if (error)
479 			return (error);
480 
481 #if 0
482 		if (mtime == NULL)
483 			mtime = &np->n_mtime;
484 		if (atime == NULL)
485 			atime = &np->n_atime;
486 #endif
487 		/*
488 		 * If file is opened, then we can use handle based calls.
489 		 * If not, use path based ones.
490 		 */
491 		if ((np->n_flag & NOPEN) == 0) {
492 			if (vcp->vc_flags & SMBV_WIN95) {
493 				error = VOP_OPEN(vp, FWRITE, ap->a_cred);
494 				if (!error) {
495 /*				error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
496 				VOP_GETATTR(vp, &vattr, ap->a_cred);*/
497 				if (mtime)
498 					np->n_mtime = *mtime;
499 				VOP_CLOSE(vp, FWRITE, ap->a_cred);
500 				}
501 			} else if (SMB_CAPS(vcp) & SMB_CAP_NT_SMBS) {
502 				error = smbfs_smb_setpattrNT(np, 0, mtime, atime, &scred);
503 			} else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN2_0) {
504 				error = smbfs_smb_setptime2(np, mtime, atime, 0, &scred);
505 			} else {
506 				error = smbfs_smb_setpattr(np, 0, mtime, &scred);
507 			}
508 		} else {
509 			if (SMB_CAPS(vcp) & SMB_CAP_NT_SMBS) {
510 				error = smbfs_smb_setfattrNT(np, 0, mtime, atime, &scred);
511 			} else if (SMB_DIALECT(vcp) >= SMB_DIALECT_LANMAN1_0) {
512 				error = smbfs_smb_setftime(np, mtime, atime, &scred);
513 			} else {
514 				/*
515 				 * XXX I have no idea how to handle this for core
516 				 * level servers. The possible solution is to
517 				 * update mtime after file is closed.
518 				 */
519 			}
520 		}
521 	}
522 	/*
523 	 * Invalidate attribute cache in case if server doesn't set
524 	 * required attributes.
525 	 */
526 	smbfs_attr_cacheremove(vp);	/* invalidate cache */
527 	VOP_GETATTR(vp, vap, ap->a_cred);
528 	np->n_mtime.tv_sec = vap->va_mtime.tv_sec;
529 	VN_KNOTE(vp, NOTE_ATTRIB);
530 	return error;
531 }
532 /*
533  * smbfs_read call.
534  */
535 int
smbfs_read(void * v)536 smbfs_read(void *v)
537 {
538 	struct vop_read_args /* {
539 		struct vnode *a_vp;
540 		struct uio *a_uio;
541 		int  a_ioflag;
542 		kauth_cred_t a_cred;
543 	} */ *ap = v;
544 	struct vnode *vp = ap->a_vp;
545 
546 	if (vp->v_type != VREG && vp->v_type != VDIR)
547 		return EPERM;
548 
549 	return smbfs_readvnode(vp, ap->a_uio, ap->a_cred);
550 }
551 
552 int
smbfs_write(void * v)553 smbfs_write(void *v)
554 {
555 	struct vop_write_args /* {
556 		struct vnode *a_vp;
557 		struct uio *a_uio;
558 		int  a_ioflag;
559 		kauth_cred_t a_cred;
560 	} */ *ap = v;
561 	struct vnode *vp = ap->a_vp;
562 	struct uio *uio = ap->a_uio;
563 
564 	SMBVDEBUG("%d,ofs=%lld,sz=%zu\n",vp->v_type,
565 	    (long long int)uio->uio_offset, uio->uio_resid);
566 	if (vp->v_type != VREG)
567 		return (EPERM);
568 	return smbfs_writevnode(vp, uio, ap->a_cred, ap->a_ioflag);
569 }
570 /*
571  * smbfs_create call
572  * Create a regular file. On entry the directory to contain the file being
573  * created is locked.  We must release before we return.
574  */
575 int
smbfs_create(void * v)576 smbfs_create(void *v)
577 {
578 	struct vop_create_v3_args /* {
579 		struct vnode *a_dvp;
580 		struct vnode **a_vpp;
581 		struct componentname *a_cnp;
582 		struct vattr *a_vap;
583 	} */ *ap = v;
584 	struct vnode *dvp = ap->a_dvp;
585 	struct vattr *vap = ap->a_vap;
586 	struct componentname *cnp = ap->a_cnp;
587 	struct smbnode *dnp = VTOSMB(dvp);
588 	struct smbfattr fattr;
589 	struct smb_cred scred;
590 	const char *name = cnp->cn_nameptr;
591 	int nmlen = cnp->cn_namelen;
592 	int error = EINVAL;
593 
594 
595 	if (vap->va_type != VREG)
596 		goto out;
597 
598 	smb_makescred(&scred, curlwp, cnp->cn_cred);
599 	error = smbfs_smb_create(dnp, name, nmlen, &scred);
600 	if (error)
601 		goto out;
602 
603 	error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, &scred);
604 	if (error)
605 		goto out;
606 	error = smbfs_nget(VTOVFS(dvp), dvp, name, nmlen, &fattr, ap->a_vpp);
607 	if (error)
608 		goto out;
609 
610 	cache_enter(dvp, *ap->a_vpp, cnp->cn_nameptr, cnp->cn_namelen,
611 		    cnp->cn_flags);
612 
613   out:
614 	VN_KNOTE(dvp, NOTE_WRITE);
615 	return (error);
616 }
617 
618 int
smbfs_remove(void * v)619 smbfs_remove(void *v)
620 {
621 	struct vop_remove_args /* {
622 		struct vnodeop_desc *a_desc;
623 		struct vnode * a_dvp;
624 		struct vnode * a_vp;
625 		struct componentname * a_cnp;
626 	} */ *ap = v;
627 	struct vnode *vp = ap->a_vp;
628 	struct vnode *dvp = ap->a_dvp;
629 	struct componentname *cnp = ap->a_cnp;
630 	struct smbnode *np = VTOSMB(vp);
631 	struct smb_cred scred;
632 	int error;
633 
634 	if (vp->v_type == VDIR || (np->n_flag & NOPEN) != 0
635 	    || vp->v_usecount != 1) {
636 		/* XXX Eventually should do something along NFS sillyrename */
637 		error = EPERM;
638 	} else {
639 		smb_makescred(&scred, curlwp, cnp->cn_cred);
640 		error = smbfs_smb_delete(np, &scred);
641 	}
642 	if (error == 0)
643 		np->n_flag |= NGONE;
644 
645 	VN_KNOTE(ap->a_vp, NOTE_DELETE);
646 	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
647 	if (dvp == vp)
648 		vrele(vp);
649 	else
650 		vput(vp);
651 	vput(dvp);
652 	return (error);
653 }
654 
655 /*
656  * smbfs_file rename call
657  */
658 int
smbfs_rename(void * v)659 smbfs_rename(void *v)
660 {
661 	struct vop_rename_args  /* {
662 		struct vnode *a_fdvp;
663 		struct vnode *a_fvp;
664 		struct componentname *a_fcnp;
665 		struct vnode *a_tdvp;
666 		struct vnode *a_tvp;
667 		struct componentname *a_tcnp;
668 	} */ *ap = v;
669 	struct vnode *fvp = ap->a_fvp;
670 	struct vnode *tvp = ap->a_tvp;
671 	struct vnode *fdvp = ap->a_fdvp;
672 	struct vnode *tdvp = ap->a_tdvp;
673 	struct componentname *tcnp = ap->a_tcnp;
674 /*	struct componentname *fcnp = ap->a_fcnp;*/
675 	struct smb_cred scred;
676 #ifdef notyet
677 	u_int16_t flags = 6;
678 #endif
679 	int error=0;
680 
681 	/* Check for cross-device rename */
682 	if ((fvp->v_mount != tdvp->v_mount) ||
683 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
684 		error = EXDEV;
685 		goto out;
686 	}
687 
688 	if (tvp && tvp->v_usecount > 1) {
689 		error = EBUSY;
690 		goto out;
691 	}
692 #ifdef notnow
693 	flags = 0x10;			/* verify all writes */
694 	if (fvp->v_type == VDIR) {
695 		flags |= 2;
696 	} else if (fvp->v_type == VREG) {
697 		flags |= 1;
698 	} else {
699 		error = EINVAL;
700 		goto out;
701 	}
702 #endif
703 	smb_makescred(&scred, curlwp, tcnp->cn_cred);
704 	/*
705 	 * It seems that Samba doesn't implement SMB_COM_MOVE call...
706 	 */
707 #ifdef notnow
708 	if (SMB_DIALECT(SSTOCN(smp->sm_share)) >= SMB_DIALECT_LANMAN1_0) {
709 		error = smbfs_smb_move(VTOSMB(fvp), VTOSMB(tdvp),
710 		    tcnp->cn_nameptr, tcnp->cn_namelen, flags, &scred);
711 	} else
712 #endif
713 	{
714 		/*
715 		 * We have to do the work atomicaly
716 		 */
717 		if (tvp && tvp != fvp) {
718 			error = smbfs_smb_delete(VTOSMB(tvp), &scred);
719 			if (error)
720 				goto out;
721 			VTOSMB(tvp)->n_flag |= NGONE;
722 			VN_KNOTE(tdvp, NOTE_WRITE);
723 			VN_KNOTE(tvp, NOTE_DELETE);
724 			cache_purge(tvp);
725 		}
726 		error = smbfs_smb_rename(VTOSMB(fvp), VTOSMB(tdvp),
727 		    tcnp->cn_nameptr, tcnp->cn_namelen, &scred);
728 		VTOSMB(fvp)->n_flag |= NGONE;
729 		VN_KNOTE(fdvp, NOTE_WRITE);
730 		VN_KNOTE(fvp, NOTE_RENAME);
731 	}
732 
733 	if (fvp->v_type == VDIR) {
734 		if (tvp != NULL && tvp->v_type == VDIR)
735 			cache_purge(tdvp);
736 		cache_purge(fdvp);
737 	}
738 
739 	smbfs_attr_cacheremove(fdvp);
740 	smbfs_attr_cacheremove(tdvp);
741 
742 out:
743 
744 	if (tdvp == tvp)
745 		vrele(tdvp);
746 	else
747 		vput(tdvp);
748 	if (tvp)
749 		vput(tvp);
750 
751 	vrele(fdvp);
752 	vrele(fvp);
753 
754 	return (error);
755 }
756 
757 /*
758  * somtime it will come true...
759  */
760 int
smbfs_link(void * v)761 smbfs_link(void *v)
762 {
763 	return genfs_eopnotsupp(v);
764 }
765 
766 /*
767  * smbfs_symlink link create call.
768  * Sometime it will be functional...
769  */
770 int
smbfs_symlink(void * v)771 smbfs_symlink(void *v)
772 {
773 	return genfs_eopnotsupp(v);
774 }
775 
776 int
smbfs_mkdir(void * v)777 smbfs_mkdir(void *v)
778 {
779 	struct vop_mkdir_v3_args /* {
780 		struct vnode *a_dvp;
781 		struct vnode **a_vpp;
782 		struct componentname *a_cnp;
783 		struct vattr *a_vap;
784 	} */ *ap = v;
785 	struct vnode *dvp = ap->a_dvp;
786 /*	struct vattr *vap = ap->a_vap;*/
787 	struct vnode *vp;
788 	struct componentname *cnp = ap->a_cnp;
789 	struct smbnode *dnp = VTOSMB(dvp);
790 	struct smb_cred scred;
791 	struct smbfattr fattr;
792 	const char *name = cnp->cn_nameptr;
793 	int len = cnp->cn_namelen;
794 	int error;
795 
796 	if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.')))){
797 		error = EEXIST;
798 		goto out;
799 	}
800 
801 	smb_makescred(&scred, curlwp, cnp->cn_cred);
802 	error = smbfs_smb_mkdir(dnp, name, len, &scred);
803 	if (error)
804 		goto out;
805 	error = smbfs_smb_lookup(dnp, name, len, &fattr, &scred);
806 	if (error)
807 		goto out;
808 	error = smbfs_nget(VTOVFS(dvp), dvp, name, len, &fattr, &vp);
809 	if (error)
810 		goto out;
811 	*ap->a_vpp = vp;
812 
813  out:
814 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
815 
816 	return (error);
817 }
818 
819 /*
820  * smbfs_remove directory call
821  */
822 int
smbfs_rmdir(void * v)823 smbfs_rmdir(void *v)
824 {
825 	struct vop_rmdir_args /* {
826 		struct vnode *a_dvp;
827 		struct vnode *a_vp;
828 		struct componentname *a_cnp;
829 	} */ *ap = v;
830 	struct vnode *vp = ap->a_vp;
831 	struct vnode *dvp = ap->a_dvp;
832 	struct componentname *cnp = ap->a_cnp;
833 /*	struct smbmount *smp = VTOSMBFS(vp);*/
834 	struct smbnode *dnp = VTOSMB(dvp);
835 	struct smbnode *np = VTOSMB(vp);
836 	struct smb_cred scred;
837 	int error;
838 
839 	if (dvp == vp) {
840 		vrele(dvp);
841 		vput(dvp);
842 		return (EINVAL);
843 	}
844 
845 	smb_makescred(&scred, curlwp, cnp->cn_cred);
846 	error = smbfs_smb_rmdir(np, &scred);
847 	if (error == 0)
848 		np->n_flag |= NGONE;
849 	dnp->n_flag |= NMODIFIED;
850 	smbfs_attr_cacheremove(dvp);
851 	VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK);
852 	VN_KNOTE(vp, NOTE_DELETE);
853 	cache_purge(dvp);
854 	cache_purge(vp);
855 	vput(vp);
856 	vput(dvp);
857 
858 	return (error);
859 }
860 
861 /*
862  * smbfs_readdir call
863  */
864 int
smbfs_readdir(void * v)865 smbfs_readdir(void *v)
866 {
867 	struct vop_readdir_args /* {
868 		struct vnode *a_vp;
869 		struct uio *a_uio;
870 		kauth_cred_t a_cred;
871 		int *a_eofflag;
872 		u_long *a_cookies;
873 		int a_ncookies;
874 	} */ *ap = v;
875 	struct vnode *vp = ap->a_vp;
876 
877 	if (vp->v_type != VDIR)
878 		return (EPERM);
879 #ifdef notnow
880 	if (ap->a_ncookies) {
881 		printf("smbfs_readdir: no support for cookies now...");
882 		return (EOPNOTSUPP);
883 	}
884 #endif
885 	return (smbfs_readvnode(vp, ap->a_uio, ap->a_cred));
886 }
887 
888 /* ARGSUSED */
889 int
smbfs_fsync(void * v)890 smbfs_fsync(void *v)
891 {
892 	/*return (smb_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_l, 1));*/
893     return (0);
894 }
895 
896 int
smbfs_print(void * v)897 smbfs_print(void *v)
898 {
899 	struct vop_print_args /* {
900 	struct vnode *a_vp;
901 	} */ *ap = v;
902 	struct vnode *vp = ap->a_vp;
903 	struct smbnode *np = VTOSMB(vp);
904 
905 	printf("tag VT_SMBFS, name = %.*s, parent = %p, open = %d\n",
906 	    (int)np->n_nmlen, np->n_name,
907 	    np->n_parent ? np->n_parent : NULL,
908 	    (np->n_flag & NOPEN) != 0);
909 	return (0);
910 }
911 
912 int
smbfs_pathconf(void * v)913 smbfs_pathconf(void *v)
914 {
915 	struct vop_pathconf_args  /* {
916 		struct vnode *vp;
917 		int a_name;
918 		register_t *a_retval;
919 	} */ *ap = v;
920 	register_t *retval = ap->a_retval;
921 	int error = 0;
922 
923 	switch (ap->a_name) {
924 	case _PC_PIPE_BUF:
925 		*retval = PIPE_BUF;
926 		break;
927 	case _PC_SYNC_IO:
928 		*retval = 1;
929 		break;
930 	case _PC_LINK_MAX:
931 		*retval = 0;
932 		break;
933 	case _PC_NAME_MAX:
934 		*retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
935 		break;
936 	case _PC_PATH_MAX:
937 		*retval = 800;	/* XXX: a correct one ? */
938 		break;
939 	default:
940 		error = EINVAL;
941 		break;
942 	}
943 
944 	return (error);
945 }
946 
947 int
smbfs_strategy(void * v)948 smbfs_strategy(void *v)
949 {
950 	struct vop_strategy_args /* {
951 		struct vnode *a_vp;
952 		struct buf *a_bp;
953 	} */ *ap = v;
954 	struct buf *bp = ap->a_bp;
955 	kauth_cred_t cr;
956 	struct lwp *l;
957 	int error = 0;
958 
959 	SMBVDEBUG0("\n");
960 	if ((bp->b_flags & (B_PHYS|B_ASYNC)) == (B_PHYS|B_ASYNC))
961 		panic("smbfs physio/async");
962 	if (bp->b_flags & B_ASYNC) {
963 		l = NULL;
964 		cr = NULL;
965 	} else {
966 		l = curlwp;	/* XXX */
967 		cr = l->l_cred;
968 	}
969 
970 	if ((bp->b_flags & B_ASYNC) == 0)
971 		error = smbfs_doio(bp, cr, l);
972 
973 	return (error);
974 }
975 
976 #ifndef __NetBSD__
977 static char smbfs_atl[] = "rhsvda";
978 static int
smbfs_getextattr(struct vop_getextattr_args * ap)979 smbfs_getextattr(struct vop_getextattr_args *ap)
980 /* {
981         IN struct vnode *a_vp;
982         IN char *a_name;
983         INOUT struct uio *a_uio;
984         IN kauth_cred_t a_cred;
985 };
986 */
987 {
988 	struct vnode *vp = ap->a_vp;
989 	struct lwp *l = ap->a_l;
990 	kauth_cred_t cred = ap->a_cred;
991 	struct uio *uio = ap->a_uio;
992 	const char *name = ap->a_name;
993 	struct smbnode *np = VTOSMB(vp);
994 	struct vattr vattr;
995 	char buf[10];
996 	int i, attr, error;
997 
998 	error = VOP_ACCESS(vp, VREAD, cred, td);
999 	if (error)
1000 		return error;
1001 	error = VOP_GETATTR(vp, &vattr, cred, td);
1002 	if (error)
1003 		return error;
1004 	if (strcmp(name, "dosattr") == 0) {
1005 		attr = np->n_dosattr;
1006 		for (i = 0; i < 6; i++, attr >>= 1)
1007 			buf[i] = (attr & 1) ? smbfs_atl[i] : '-';
1008 		buf[i] = 0;
1009 		error = uiomove(buf, i, uio);
1010 
1011 	} else
1012 		error = EINVAL;
1013 	return error;
1014 }
1015 #endif /* !__NetBSD__ */
1016 
1017 /*
1018  * Since we expected to support F_GETLK (and SMB protocol has no such function),
1019  * it is necessary to use lf_advlock(). It would be nice if this function had
1020  * a callback mechanism because it will help to improve a level of consistency.
1021  */
1022 int
smbfs_advlock(void * v)1023 smbfs_advlock(void *v)
1024 {
1025 	struct vop_advlock_args /* {
1026 		struct vnode *a_vp;
1027 		void *a_id;
1028 		int  a_op;
1029 		struct flock *a_fl;
1030 		int  a_flags;
1031 	} */ *ap = v;
1032 	struct vnode *vp = ap->a_vp;
1033 	struct smbnode *np = VTOSMB(vp);
1034 	struct flock *fl = ap->a_fl;
1035 	struct lwp *l = curlwp;
1036 	struct smb_cred scred;
1037 	u_quad_t size;
1038 	off_t start, end, oadd;
1039 	int error, lkop;
1040 
1041 	if (vp->v_type == VDIR) {
1042 		/*
1043 		 * SMB protocol have no support for directory locking.
1044 		 * Although locks can be processed on local machine, I don't
1045 		 * think that this is a good idea, because some programs
1046 		 * can work wrong assuming directory is locked. So, we just
1047 		 * return 'operation not supported'.
1048 		 */
1049 		 return EOPNOTSUPP;
1050 	}
1051 	size = np->n_size;
1052 	switch (fl->l_whence) {
1053 
1054 	case SEEK_SET:
1055 	case SEEK_CUR:
1056 		start = fl->l_start;
1057 		break;
1058 
1059 	case SEEK_END:
1060 #ifndef __NetBSD__
1061 		if (size > OFF_MAX ||
1062 		    (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
1063 			return EOVERFLOW;
1064 #endif
1065 		start = size + fl->l_start;
1066 		break;
1067 
1068 	default:
1069 		return EINVAL;
1070 	}
1071 	if (start < 0)
1072 		return EINVAL;
1073 	if (fl->l_len < 0) {
1074 		if (start == 0)
1075 			return EINVAL;
1076 		end = start - 1;
1077 		start += fl->l_len;
1078 		if (start < 0)
1079 			return EINVAL;
1080 	} else if (fl->l_len == 0)
1081 		end = -1;
1082 	else {
1083 		oadd = fl->l_len - 1;
1084 #ifndef __NetBSD__
1085 		if (oadd > OFF_MAX - start)
1086 			return EOVERFLOW;
1087 #endif
1088 		end = start + oadd;
1089 	}
1090 	smb_makescred(&scred, l, l ? l->l_cred : NULL);
1091 	switch (ap->a_op) {
1092 	case F_SETLK:
1093 		switch (fl->l_type) {
1094 		case F_WRLCK:
1095 			lkop = SMB_LOCK_EXCL;
1096 			break;
1097 		case F_RDLCK:
1098 			lkop = SMB_LOCK_SHARED;
1099 			break;
1100 		case F_UNLCK:
1101 			lkop = SMB_LOCK_RELEASE;
1102 			break;
1103 		default:
1104 			return EINVAL;
1105 		}
1106 		error = lf_advlock(ap, &np->n_lockf, size);
1107 		if (error)
1108 			break;
1109 		/*
1110 		 * The ID we use for smb_lock is passed as PID to SMB
1111 		 * server. It MUST agree with PID as setup in basic
1112 		 * SMB header in later write requests, otherwise SMB server
1113 		 * returns EDEADLK. See also smb_rq_new() on SMB header setup.
1114 		 */
1115 		error = smbfs_smb_lock(np, lkop,(void *)1, start, end, &scred);
1116 		if (error) {
1117 			ap->a_op = F_UNLCK;
1118 			lf_advlock(ap, &np->n_lockf, size);
1119 		}
1120 		break;
1121 	case F_UNLCK:
1122 		lf_advlock(ap, &np->n_lockf, size);
1123 		error = smbfs_smb_lock(np, SMB_LOCK_RELEASE,  ap->a_id, start, end, &scred);
1124 		break;
1125 	case F_GETLK:
1126 		error = lf_advlock(ap, &np->n_lockf, size);
1127 		break;
1128 	default:
1129 		return EINVAL;
1130 	}
1131 
1132 	return error;
1133 }
1134 
1135 static int
smbfs_pathcheck(struct smbmount * smp,const char * name,int nmlen)1136 smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen)
1137 {
1138 	static const char * const badchars = "*/\\:<>;?";
1139 	static const char * const badchars83 = " +|,[]=";
1140 	const char *cp;
1141 	int i;
1142 
1143 	if (SMB_DIALECT(SSTOVC(smp->sm_share)) < SMB_DIALECT_LANMAN2_0) {
1144 		/*
1145 		 * Name should conform 8.3 format
1146 		 */
1147 		if (nmlen > 12)
1148 			return (ENAMETOOLONG);
1149 
1150 		if ((cp = memchr(name, '.', nmlen)) == NULL
1151 		    || cp == name || (cp - name) > 8
1152 		    || (cp = memchr(cp + 1, '.', nmlen - (cp - name))) != NULL)
1153 			goto bad;
1154 
1155 		for (cp = name, i = 0; i < nmlen; i++, cp++)
1156 			if (strchr(badchars83, *cp) != NULL)
1157 				goto bad;
1158 	}
1159 
1160 	for (cp = name, i = 0; i < nmlen; i++, cp++)
1161 		if (strchr(badchars, *cp) != NULL)
1162 			goto bad;
1163 
1164 	/* name is fine */
1165 	return (0);
1166 
1167  bad:
1168 	return (ENOENT);
1169 }
1170 
1171 /*
1172  * Things go even weird without fixed inode numbers...
1173  */
1174 int
smbfs_lookup(void * v)1175 smbfs_lookup(void *v)
1176 {
1177 	struct vop_lookup_v2_args /* {
1178 		struct vnode *a_dvp;
1179 		struct vnode **a_vpp;
1180 		struct componentname *a_cnp;
1181 	} */ *ap = v;
1182 	struct componentname *cnp = ap->a_cnp;
1183 	struct vnode *dvp = ap->a_dvp;
1184 	struct vnode **vpp = ap->a_vpp;
1185 	struct mount *mp = dvp->v_mount;
1186 	struct smbnode *dnp;
1187 	struct smbfattr fattr;
1188 	struct smb_cred scred;
1189 	const char *name = cnp->cn_nameptr;
1190 	int flags = cnp->cn_flags;
1191 	int nameiop = cnp->cn_nameiop;
1192 	int nmlen = cnp->cn_namelen;
1193 	int error, islastcn, isdot;
1194 
1195 	/*
1196 	 * Check accessiblity of directory.
1197 	 */
1198 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
1199 	if (error)
1200 		return (error);
1201 
1202 	if ((cnp->cn_flags & ISLASTCN) &&
1203 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1204 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1205 		return (EROFS);
1206 
1207 	SMBVDEBUG("%d '%.*s' in '%.*s'\n", nameiop, nmlen, name,
1208 	    (int) VTOSMB(dvp)->n_nmlen, VTOSMB(dvp)->n_name);
1209 
1210 	islastcn = flags & ISLASTCN;
1211 
1212 	/*
1213 	 * Before tediously performing a linear scan of the directory,
1214 	 * check the name cache to see if the directory/name pair
1215 	 * we are looking for is known already.
1216 	 * If the directory/name pair is found in the name cache,
1217 	 * we have to ensure the directory has not changed from
1218 	 * the time the cache entry has been created. If it has,
1219 	 * the cache entry has to be ignored.
1220 	 */
1221 	if (cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1222 			 cnp->cn_nameiop, cnp->cn_flags,
1223 			 NULL, vpp)) {
1224 		struct vattr vattr;
1225 		struct vnode *newvp;
1226 		bool killit = false;
1227 
1228 		error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
1229 		if (error != 0) {
1230 			if (*vpp != NULLVP) {
1231 				vrele(*vpp);
1232 			}
1233 			*vpp = NULLVP;
1234 			return error;
1235 		}
1236 
1237 		if (*vpp == NULLVP) {
1238 			if (!VOP_GETATTR(dvp, &vattr, cnp->cn_cred)
1239 			    && vattr.va_mtime.tv_sec == VTOSMB(dvp)->n_nctime)
1240 				return ENOENT;
1241 			cache_purge(dvp);
1242 			VTOSMB(dvp)->n_nctime = 0;
1243 			goto dolookup;
1244 		}
1245 
1246 		newvp = *vpp;
1247 		if (newvp != dvp)
1248 			vn_lock(newvp, LK_SHARED | LK_RETRY);
1249 		error = VOP_GETATTR(newvp, &vattr, cnp->cn_cred);
1250 		/*
1251 		 * If the file type on the server is inconsistent
1252 		 * with what it was when we created the vnode,
1253 		 * kill the bogus vnode now and fall through to
1254 		 * the code below to create a new one with the
1255 		 * right type.
1256 		 */
1257 		if (error == 0 &&
1258 		    ((newvp->v_type == VDIR &&
1259 		    (VTOSMB(newvp)->n_dosattr & SMB_FA_DIR) == 0) ||
1260 		    (newvp->v_type == VREG &&
1261 		    (VTOSMB(newvp)->n_dosattr & SMB_FA_DIR) != 0)))
1262 			killit = true;
1263 		else if (error == 0
1264 			&& vattr.va_ctime.tv_sec == VTOSMB(newvp)->n_ctime)
1265 		{
1266 			if (newvp != dvp)
1267 				VOP_UNLOCK(newvp);
1268 			/* nfsstats.lookupcache_hits++; */
1269 			return (0);
1270 		}
1271 
1272 		cache_purge(newvp);
1273 		if (newvp != dvp) {
1274 			if (killit) {
1275 				VOP_UNLOCK(newvp);
1276 				vgone(newvp);
1277 			} else
1278 				vput(newvp);
1279 		} else
1280 			vrele(newvp);
1281 		*vpp = NULLVP;
1282 	}
1283 
1284  dolookup:
1285 
1286 	/* ensure the name is sane */
1287 	if (nameiop != LOOKUP) {
1288 		error = smbfs_pathcheck(VFSTOSMBFS(mp), cnp->cn_nameptr,
1289 					cnp->cn_namelen);
1290 		if (error)
1291 			return (error);
1292 	}
1293 
1294 	dnp = VTOSMB(dvp);
1295 	isdot = (nmlen == 1 && name[0] == '.');
1296 
1297 	/*
1298 	 * entry is not in the cache or has been expired
1299 	 */
1300 	smb_makescred(&scred, curlwp, cnp->cn_cred);
1301 	if (flags & ISDOTDOT)
1302 		error = smbfs_smb_lookup(VTOSMB(dnp->n_parent), NULL, 0,
1303 		    &fattr, &scred);
1304 	else
1305 		error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, &scred);
1306 
1307 	if (error) {
1308 		/* Not found */
1309 
1310 		if (error != ENOENT)
1311 			return (error);
1312 
1313 		/*
1314 		 * Handle RENAME or CREATE case...
1315 		 */
1316 		if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1317 			/*
1318 			 * Access for write is interpreted as allowing
1319 			 * creation of files in the directory.
1320 			 */
1321 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
1322 			if (error)
1323 				return (error);
1324 
1325 			return (EJUSTRETURN);
1326 		}
1327 
1328 		/*
1329 		 * Insert name into cache (as non-existent) if appropriate.
1330 		 */
1331 		if (nameiop != CREATE)
1332 			cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
1333 				    cnp->cn_flags);
1334 
1335 		return (ENOENT);
1336 	}
1337 
1338 	/* Found */
1339 
1340 	/* Handle RENAME case... */
1341 	if (nameiop == RENAME && islastcn) {
1342 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
1343 		if (error)
1344 			return (error);
1345 
1346 		if (isdot)
1347 			return (EISDIR);
1348 		error = smbfs_nget(mp, dvp, name, nmlen, &fattr, vpp);
1349 		if (error)
1350 			return (error);
1351 		return (0);
1352 	}
1353 
1354 	if (isdot) {
1355 		vref(dvp);
1356 		*vpp = dvp;
1357 		error = 0;
1358 	} else {
1359 		error = smbfs_nget(mp, dvp, name, nmlen,
1360 		    ((flags & ISDOTDOT) ? NULL : &fattr), vpp);
1361 	}
1362 	if (error)
1363 		return error;
1364 
1365 	if (cnp->cn_nameiop != DELETE || !islastcn) {
1366 		VTOSMB(*vpp)->n_ctime = VTOSMB(*vpp)->n_mtime.tv_sec;
1367 		cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
1368 			    cnp->cn_flags);
1369 #ifdef notdef
1370 	} else if (error == ENOENT && cnp->cn_nameiop != CREATE) {
1371 		VTOSMB(*vpp)->n_nctime = VTOSMB(*vpp)->n_mtime.tv_sec;
1372 		cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
1373 			    cnp->cn_flags);
1374 #endif
1375 	}
1376 
1377 	return (0);
1378 }
1379