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