xref: /freebsd/sys/fs/smbfs/smbfs_vnops.c (revision c07d6445eb89d9dd3950361b065b7bd110e3a043)
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.
532  */
533 static int
534 smbfs_create(ap)
535 	struct vop_create_args /* {
536 		struct vnode *a_dvp;
537 		struct vnode **a_vpp;
538 		struct componentname *a_cnp;
539 		struct vattr *a_vap;
540 	} */ *ap;
541 {
542 	struct vnode *dvp = ap->a_dvp;
543 	struct vattr *vap = ap->a_vap;
544 	struct vnode **vpp=ap->a_vpp;
545 	struct componentname *cnp = ap->a_cnp;
546 	struct smbnode *dnp = VTOSMB(dvp);
547 	struct vnode *vp;
548 	struct vattr vattr;
549 	struct smbfattr fattr;
550 	struct smb_cred *scred;
551 	char *name = cnp->cn_nameptr;
552 	int nmlen = cnp->cn_namelen;
553 	int error;
554 
555 	SMBVDEBUG("\n");
556 	*vpp = NULL;
557 	if (vap->va_type != VREG)
558 		return EOPNOTSUPP;
559 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
560 		return error;
561 	scred = smbfs_malloc_scred();
562 	smb_makescred(scred, curthread, cnp->cn_cred);
563 
564 	error = smbfs_smb_create(dnp, name, nmlen, scred);
565 	if (error)
566 		goto out;
567 	error = smbfs_smb_lookup(dnp, name, nmlen, &fattr, scred);
568 	if (error)
569 		goto out;
570 	error = smbfs_nget(VTOVFS(dvp), dvp, name, nmlen, &fattr, &vp);
571 	if (error)
572 		goto out;
573 	*vpp = vp;
574 	if (cnp->cn_flags & MAKEENTRY)
575 		cache_enter(dvp, vp, cnp);
576 out:
577 	smbfs_free_scred(scred);
578 	return error;
579 }
580 
581 static int
582 smbfs_remove(ap)
583 	struct vop_remove_args /* {
584 		struct vnodeop_desc *a_desc;
585 		struct vnode * a_dvp;
586 		struct vnode * a_vp;
587 		struct componentname * a_cnp;
588 	} */ *ap;
589 {
590 	struct vnode *vp = ap->a_vp;
591 /*	struct vnode *dvp = ap->a_dvp;*/
592 	struct componentname *cnp = ap->a_cnp;
593 	struct smbnode *np = VTOSMB(vp);
594 	struct smb_cred *scred;
595 	int error;
596 
597 	if (vp->v_type == VDIR || (np->n_flag & NOPEN) != 0 || vrefcnt(vp) != 1)
598 		return EPERM;
599 	scred = smbfs_malloc_scred();
600 	smb_makescred(scred, curthread, cnp->cn_cred);
601 	error = smbfs_smb_delete(np, scred);
602 	if (error == 0)
603 		np->n_flag |= NGONE;
604 	cache_purge(vp);
605 	smbfs_free_scred(scred);
606 	return error;
607 }
608 
609 /*
610  * smbfs_file rename call
611  */
612 static int
613 smbfs_rename(ap)
614 	struct vop_rename_args  /* {
615 		struct vnode *a_fdvp;
616 		struct vnode *a_fvp;
617 		struct componentname *a_fcnp;
618 		struct vnode *a_tdvp;
619 		struct vnode *a_tvp;
620 		struct componentname *a_tcnp;
621 	} */ *ap;
622 {
623 	struct vnode *fvp = ap->a_fvp;
624 	struct vnode *tvp = ap->a_tvp;
625 	struct vnode *fdvp = ap->a_fdvp;
626 	struct vnode *tdvp = ap->a_tdvp;
627 	struct componentname *tcnp = ap->a_tcnp;
628 /*	struct componentname *fcnp = ap->a_fcnp;*/
629 	struct smb_cred *scred;
630 #ifdef notnow
631 	u_int16_t flags = 6;
632 #endif
633 	int error=0;
634 
635 	scred = NULL;
636 	/* Check for cross-device rename */
637 	if ((fvp->v_mount != tdvp->v_mount) ||
638 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
639 		error = EXDEV;
640 		goto out;
641 	}
642 
643 	if (tvp && vrefcnt(tvp) > 1) {
644 		error = EBUSY;
645 		goto out;
646 	}
647 #ifdef notnow
648 	flags = 0x10;			/* verify all writes */
649 #endif
650 	if (fvp->v_type == VDIR) {
651 #ifdef notnow
652 		flags |= 2;
653 #endif
654 	} else if (fvp->v_type == VREG) {
655 #ifdef notnow
656 		flags |= 1;
657 #endif
658 	} else {
659 		return EINVAL;
660 	}
661 	scred = smbfs_malloc_scred();
662 	smb_makescred(scred, curthread, tcnp->cn_cred);
663 	/*
664 	 * It seems that Samba doesn't implement SMB_COM_MOVE call...
665 	 */
666 #ifdef notnow
667 	if (SMB_DIALECT(SSTOCN(smp->sm_share)) >= SMB_DIALECT_LANMAN1_0) {
668 		error = smbfs_smb_move(VTOSMB(fvp), VTOSMB(tdvp),
669 		    tcnp->cn_nameptr, tcnp->cn_namelen, flags, scred);
670 	} else
671 #endif
672 	{
673 		/*
674 		 * We have to do the work atomicaly
675 		 */
676 		if (tvp && tvp != fvp) {
677 			error = smbfs_smb_delete(VTOSMB(tvp), scred);
678 			if (error)
679 				goto out_cacherem;
680 			VTOSMB(fvp)->n_flag |= NGONE;
681 		}
682 		error = smbfs_smb_rename(VTOSMB(fvp), VTOSMB(tdvp),
683 		    tcnp->cn_nameptr, tcnp->cn_namelen, scred);
684 	}
685 
686 	if (fvp->v_type == VDIR) {
687 		if (tvp != NULL && tvp->v_type == VDIR)
688 			cache_purge(tdvp);
689 		cache_purge(fdvp);
690 	}
691 
692 out_cacherem:
693 	smbfs_attr_cacheremove(fdvp);
694 	smbfs_attr_cacheremove(tdvp);
695 out:
696 	smbfs_free_scred(scred);
697 	if (tdvp == tvp)
698 		vrele(tdvp);
699 	else
700 		vput(tdvp);
701 	if (tvp)
702 		vput(tvp);
703 	vrele(fdvp);
704 	vrele(fvp);
705 #ifdef possible_mistake
706 	vgone(fvp);
707 	if (tvp)
708 		vgone(tvp);
709 #endif
710 	return error;
711 }
712 
713 /*
714  * somtime it will come true...
715  */
716 static int
717 smbfs_link(ap)
718 	struct vop_link_args /* {
719 		struct vnode *a_tdvp;
720 		struct vnode *a_vp;
721 		struct componentname *a_cnp;
722 	} */ *ap;
723 {
724 	return EOPNOTSUPP;
725 }
726 
727 /*
728  * smbfs_symlink link create call.
729  * Sometime it will be functional...
730  */
731 static int
732 smbfs_symlink(ap)
733 	struct vop_symlink_args /* {
734 		struct vnode *a_dvp;
735 		struct vnode **a_vpp;
736 		struct componentname *a_cnp;
737 		struct vattr *a_vap;
738 		char *a_target;
739 	} */ *ap;
740 {
741 	return EOPNOTSUPP;
742 }
743 
744 static int
745 smbfs_mknod(ap)
746 	struct vop_mknod_args /* {
747 	} */ *ap;
748 {
749 	return EOPNOTSUPP;
750 }
751 
752 static int
753 smbfs_mkdir(ap)
754 	struct vop_mkdir_args /* {
755 		struct vnode *a_dvp;
756 		struct vnode **a_vpp;
757 		struct componentname *a_cnp;
758 		struct vattr *a_vap;
759 	} */ *ap;
760 {
761 	struct vnode *dvp = ap->a_dvp;
762 /*	struct vattr *vap = ap->a_vap;*/
763 	struct vnode *vp;
764 	struct componentname *cnp = ap->a_cnp;
765 	struct smbnode *dnp = VTOSMB(dvp);
766 	struct vattr vattr;
767 	struct smb_cred *scred;
768 	struct smbfattr fattr;
769 	char *name = cnp->cn_nameptr;
770 	int len = cnp->cn_namelen;
771 	int error;
772 
773 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred))) {
774 		return error;
775 	}
776 	if ((name[0] == '.') && ((len == 1) || ((len == 2) && (name[1] == '.'))))
777 		return EEXIST;
778 	scred = smbfs_malloc_scred();
779 	smb_makescred(scred, curthread, cnp->cn_cred);
780 	error = smbfs_smb_mkdir(dnp, name, len, scred);
781 	if (error)
782 		goto out;
783 	error = smbfs_smb_lookup(dnp, name, len, &fattr, scred);
784 	if (error)
785 		goto out;
786 	error = smbfs_nget(VTOVFS(dvp), dvp, name, len, &fattr, &vp);
787 	if (error)
788 		goto out;
789 	*ap->a_vpp = vp;
790 out:
791 	smbfs_free_scred(scred);
792 	return error;
793 }
794 
795 /*
796  * smbfs_remove directory call
797  */
798 static int
799 smbfs_rmdir(ap)
800 	struct vop_rmdir_args /* {
801 		struct vnode *a_dvp;
802 		struct vnode *a_vp;
803 		struct componentname *a_cnp;
804 	} */ *ap;
805 {
806 	struct vnode *vp = ap->a_vp;
807 	struct vnode *dvp = ap->a_dvp;
808 	struct componentname *cnp = ap->a_cnp;
809 /*	struct smbmount *smp = VTOSMBFS(vp);*/
810 	struct smbnode *dnp = VTOSMB(dvp);
811 	struct smbnode *np = VTOSMB(vp);
812 	struct smb_cred *scred;
813 	int error;
814 
815 	if (dvp == vp)
816 		return EINVAL;
817 
818 	scred = smbfs_malloc_scred();
819 	smb_makescred(scred, curthread, cnp->cn_cred);
820 	error = smbfs_smb_rmdir(np, scred);
821 	if (error == 0)
822 		np->n_flag |= NGONE;
823 	dnp->n_flag |= NMODIFIED;
824 	smbfs_attr_cacheremove(dvp);
825 /*	cache_purge(dvp);*/
826 	cache_purge(vp);
827 	smbfs_free_scred(scred);
828 	return error;
829 }
830 
831 /*
832  * smbfs_readdir call
833  */
834 static int
835 smbfs_readdir(ap)
836 	struct vop_readdir_args /* {
837 		struct vnode *a_vp;
838 		struct uio *a_uio;
839 		struct ucred *a_cred;
840 		int *a_eofflag;
841 		uint64_t *a_cookies;
842 		int a_ncookies;
843 	} */ *ap;
844 {
845 	struct vnode *vp = ap->a_vp;
846 	struct uio *uio = ap->a_uio;
847 	int error;
848 
849 	if (vp->v_type != VDIR)
850 		return (EPERM);
851 #ifdef notnow
852 	if (ap->a_ncookies) {
853 		printf("smbfs_readdir: no support for cookies now...");
854 		return (EOPNOTSUPP);
855 	}
856 #endif
857 	error = smbfs_readvnode(vp, uio, ap->a_cred);
858 	return error;
859 }
860 
861 /* ARGSUSED */
862 static int
863 smbfs_fsync(ap)
864 	struct vop_fsync_args /* {
865 		struct vnodeop_desc *a_desc;
866 		struct vnode * a_vp;
867 		struct ucred * a_cred;
868 		int  a_waitfor;
869 		struct thread * a_td;
870 	} */ *ap;
871 {
872 /*	return (smb_flush(ap->a_vp, ap->a_cred, ap->a_waitfor, ap->a_td, 1));*/
873     return (0);
874 }
875 
876 static
877 int smbfs_print (ap)
878 	struct vop_print_args /* {
879 	struct vnode *a_vp;
880 	} */ *ap;
881 {
882 	struct vnode *vp = ap->a_vp;
883 	struct smbnode *np = VTOSMB(vp);
884 
885 	if (np == NULL) {
886 		printf("no smbnode data\n");
887 		return (0);
888 	}
889 	printf("\tname = %s, parent = %p, open = %d\n", np->n_name,
890 	    np->n_parent ? np->n_parent : NULL, (np->n_flag & NOPEN) != 0);
891 	return (0);
892 }
893 
894 static int
895 smbfs_pathconf (ap)
896 	struct vop_pathconf_args  /* {
897 	struct vnode *vp;
898 	int name;
899 	register_t *retval;
900 	} */ *ap;
901 {
902 	struct smbmount *smp = VFSTOSMBFS(VTOVFS(ap->a_vp));
903 	struct smb_vc *vcp = SSTOVC(smp->sm_share);
904 	long *retval = ap->a_retval;
905 	int error = 0;
906 
907 	switch (ap->a_name) {
908 	    case _PC_FILESIZEBITS:
909 		if (vcp->vc_sopt.sv_caps & (SMB_CAP_LARGE_READX |
910 		    SMB_CAP_LARGE_WRITEX))
911 		    *retval = 64;
912 		else
913 		    *retval = 32;
914 		break;
915 	    case _PC_NAME_MAX:
916 		*retval = (vcp->vc_hflags2 & SMB_FLAGS2_KNOWS_LONG_NAMES) ? 255 : 12;
917 		break;
918 	    case _PC_PATH_MAX:
919 		*retval = 800;	/* XXX: a correct one ? */
920 		break;
921 	    case _PC_NO_TRUNC:
922 		*retval = 1;
923 		break;
924 	    default:
925 		error = vop_stdpathconf(ap);
926 	}
927 	return error;
928 }
929 
930 static int
931 smbfs_strategy (ap)
932 	struct vop_strategy_args /* {
933 	struct buf *a_bp
934 	} */ *ap;
935 {
936 	struct buf *bp=ap->a_bp;
937 	struct ucred *cr;
938 	struct thread *td;
939 
940 	SMBVDEBUG("\n");
941 	if (bp->b_flags & B_ASYNC)
942 		td = (struct thread *)0;
943 	else
944 		td = curthread;	/* XXX */
945 	if (bp->b_iocmd == BIO_READ)
946 		cr = bp->b_rcred;
947 	else
948 		cr = bp->b_wcred;
949 
950 	if ((bp->b_flags & B_ASYNC) == 0 )
951 		(void)smbfs_doio(ap->a_vp, bp, cr, td);
952 	return (0);
953 }
954 
955 int
956 smbfs_ioctl(ap)
957 	struct vop_ioctl_args /* {
958 		struct vnode *a_vp;
959 		u_long a_command;
960 		caddr_t a_data;
961 		int fflag;
962 		struct ucred *cred;
963 		struct thread *td;
964 	} */ *ap;
965 {
966 	return ENOTTY;
967 }
968 
969 static char smbfs_atl[] = "rhsvda";
970 static int
971 smbfs_getextattr(struct vop_getextattr_args *ap)
972 /* {
973         IN struct vnode *a_vp;
974         IN char *a_name;
975         INOUT struct uio *a_uio;
976         IN struct ucred *a_cred;
977         IN struct thread *a_td;
978 };
979 */
980 {
981 	struct vnode *vp = ap->a_vp;
982 	struct thread *td = ap->a_td;
983 	struct ucred *cred = ap->a_cred;
984 	struct uio *uio = ap->a_uio;
985 	const char *name = ap->a_name;
986 	struct smbnode *np = VTOSMB(vp);
987 	struct vattr vattr;
988 	char buf[10];
989 	int i, attr, error;
990 
991 	error = VOP_ACCESS(vp, VREAD, cred, td);
992 	if (error)
993 		return error;
994 	error = VOP_GETATTR(vp, &vattr, cred);
995 	if (error)
996 		return error;
997 	if (strcmp(name, "dosattr") == 0) {
998 		attr = np->n_dosattr;
999 		for (i = 0; i < 6; i++, attr >>= 1)
1000 			buf[i] = (attr & 1) ? smbfs_atl[i] : '-';
1001 		buf[i] = 0;
1002 		error = uiomove(buf, i, uio);
1003 
1004 	} else
1005 		error = EINVAL;
1006 	return error;
1007 }
1008 
1009 /*
1010  * Since we expected to support F_GETLK (and SMB protocol has no such function),
1011  * it is necessary to use lf_advlock(). It would be nice if this function had
1012  * a callback mechanism because it will help to improve a level of consistency.
1013  */
1014 int
1015 smbfs_advlock(ap)
1016 	struct vop_advlock_args /* {
1017 		struct vnode *a_vp;
1018 		caddr_t  a_id;
1019 		int  a_op;
1020 		struct flock *a_fl;
1021 		int  a_flags;
1022 	} */ *ap;
1023 {
1024 	struct vnode *vp = ap->a_vp;
1025 	struct smbnode *np = VTOSMB(vp);
1026 	struct flock *fl = ap->a_fl;
1027 	caddr_t id = (caddr_t)1 /* ap->a_id */;
1028 /*	int flags = ap->a_flags;*/
1029 	struct thread *td = curthread;
1030 	struct smb_cred *scred;
1031 	u_quad_t size;
1032 	off_t start, end, oadd;
1033 	int error, lkop;
1034 
1035 	if (vp->v_type == VDIR) {
1036 		/*
1037 		 * SMB protocol have no support for directory locking.
1038 		 * Although locks can be processed on local machine, I don't
1039 		 * think that this is a good idea, because some programs
1040 		 * can work wrong assuming directory is locked. So, we just
1041 		 * return 'operation not supported
1042 		 */
1043 		 return EOPNOTSUPP;
1044 	}
1045 	size = np->n_size;
1046 	switch (fl->l_whence) {
1047 	case SEEK_SET:
1048 	case SEEK_CUR:
1049 		start = fl->l_start;
1050 		break;
1051 
1052 	case SEEK_END:
1053 		if (size > OFF_MAX ||
1054 		    (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
1055 			return EOVERFLOW;
1056 		start = size + fl->l_start;
1057 		break;
1058 
1059 	default:
1060 		return EINVAL;
1061 	}
1062 	if (start < 0)
1063 		return EINVAL;
1064 	if (fl->l_len < 0) {
1065 		if (start == 0)
1066 			return EINVAL;
1067 		end = start - 1;
1068 		start += fl->l_len;
1069 		if (start < 0)
1070 			return EINVAL;
1071 	} else if (fl->l_len == 0)
1072 		end = -1;
1073 	else {
1074 		oadd = fl->l_len - 1;
1075 		if (oadd > OFF_MAX - start)
1076 			return EOVERFLOW;
1077 		end = start + oadd;
1078 	}
1079 	scred = smbfs_malloc_scred();
1080 	smb_makescred(scred, td, td->td_ucred);
1081 	switch (ap->a_op) {
1082 	    case F_SETLK:
1083 		switch (fl->l_type) {
1084 		    case F_WRLCK:
1085 			lkop = SMB_LOCK_EXCL;
1086 			break;
1087 		    case F_RDLCK:
1088 			lkop = SMB_LOCK_SHARED;
1089 			break;
1090 		    case F_UNLCK:
1091 			lkop = SMB_LOCK_RELEASE;
1092 			break;
1093 		    default:
1094 			smbfs_free_scred(scred);
1095 			return EINVAL;
1096 		}
1097 		error = lf_advlock(ap, &vp->v_lockf, size);
1098 		if (error)
1099 			break;
1100 		lkop = SMB_LOCK_EXCL;
1101 		error = smbfs_smb_lock(np, lkop, id, start, end, scred);
1102 		if (error) {
1103 			int oldtype = fl->l_type;
1104 			fl->l_type = F_UNLCK;
1105 			ap->a_op = F_UNLCK;
1106 			lf_advlock(ap, &vp->v_lockf, size);
1107 			fl->l_type = oldtype;
1108 		}
1109 		break;
1110 	    case F_UNLCK:
1111 		lf_advlock(ap, &vp->v_lockf, size);
1112 		error = smbfs_smb_lock(np, SMB_LOCK_RELEASE, id, start, end, scred);
1113 		break;
1114 	    case F_GETLK:
1115 		error = lf_advlock(ap, &vp->v_lockf, size);
1116 		break;
1117 	    default:
1118 		smbfs_free_scred(scred);
1119 		return EINVAL;
1120 	}
1121 	smbfs_free_scred(scred);
1122 	return error;
1123 }
1124 
1125 static int
1126 smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop)
1127 {
1128 	static const char *badchars = "*/:<>?";
1129 	static const char *badchars83 = " +|,[]=;";
1130 	const char *cp;
1131 	int i, error;
1132 
1133 	/*
1134 	 * Backslash characters, being a path delimiter, are prohibited
1135 	 * within a path component even for LOOKUP operations.
1136 	 */
1137 	if (strchr(name, '\\') != NULL)
1138 		return ENOENT;
1139 
1140 	if (nameiop == LOOKUP)
1141 		return 0;
1142 	error = ENOENT;
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 		cp = strchr(name, '.');
1150 		if (cp == NULL)
1151 			return error;
1152 		if (cp == name || (cp - name) > 8)
1153 			return error;
1154 		cp = strchr(cp + 1, '.');
1155 		if (cp != NULL)
1156 			return error;
1157 		for (cp = name, i = 0; i < nmlen; i++, cp++)
1158 			if (strchr(badchars83, *cp) != NULL)
1159 				return error;
1160 	}
1161 	for (cp = name, i = 0; i < nmlen; i++, cp++)
1162 		if (strchr(badchars, *cp) != NULL)
1163 			return error;
1164 	return 0;
1165 }
1166 
1167 /*
1168  * Things go even weird without fixed inode numbers...
1169  */
1170 int
1171 smbfs_lookup(ap)
1172 	struct vop_lookup_args /* {
1173 		struct vnodeop_desc *a_desc;
1174 		struct vnode *a_dvp;
1175 		struct vnode **a_vpp;
1176 		struct componentname *a_cnp;
1177 	} */ *ap;
1178 {
1179 	struct componentname *cnp = ap->a_cnp;
1180 	struct thread *td = curthread;
1181 	struct vnode *dvp = ap->a_dvp;
1182 	struct vnode **vpp = ap->a_vpp;
1183 	struct vnode *vp;
1184 	struct smbmount *smp;
1185 	struct mount *mp = dvp->v_mount;
1186 	struct smbnode *dnp;
1187 	struct smbfattr fattr, *fap;
1188 	struct smb_cred *scred;
1189 	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 	int killit;
1195 
1196 	SMBVDEBUG("\n");
1197 	if (dvp->v_type != VDIR)
1198 		return ENOTDIR;
1199 	if ((flags & ISDOTDOT) && (dvp->v_vflag & VV_ROOT)) {
1200 		SMBFSERR("invalid '..'\n");
1201 		return EIO;
1202 	}
1203 	islastcn = flags & ISLASTCN;
1204 	if (islastcn && (mp->mnt_flag & MNT_RDONLY) && (nameiop != LOOKUP))
1205 		return EROFS;
1206 	error = vn_dir_check_exec(dvp, cnp);
1207 	if (error != 0)
1208 		return error;
1209 	smp = VFSTOSMBFS(mp);
1210 	dnp = VTOSMB(dvp);
1211 	isdot = (nmlen == 1 && name[0] == '.');
1212 
1213 	error = smbfs_pathcheck(smp, cnp->cn_nameptr, cnp->cn_namelen, nameiop);
1214 
1215 	if (error)
1216 		return ENOENT;
1217 
1218 	error = cache_lookup(dvp, vpp, cnp, NULL, NULL);
1219 	SMBVDEBUG("cache_lookup returned %d\n", error);
1220 	if (error > 0)
1221 		return error;
1222 	if (error) {		/* name was found */
1223 		struct vattr vattr;
1224 
1225 		killit = 0;
1226 		vp = *vpp;
1227 		error = VOP_GETATTR(vp, &vattr, cnp->cn_cred);
1228 		/*
1229 		 * If the file type on the server is inconsistent
1230 		 * with what it was when we created the vnode,
1231 		 * kill the bogus vnode now and fall through to
1232 		 * the code below to create a new one with the
1233 		 * right type.
1234 		 */
1235 		if (error == 0 &&
1236 		   ((vp->v_type == VDIR &&
1237 		   (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) == 0) ||
1238 		   (vp->v_type == VREG &&
1239 		   (VTOSMB(vp)->n_dosattr & SMB_FA_DIR) != 0)))
1240 		   killit = 1;
1241 		else if (error == 0
1242 	     /*    && vattr.va_ctime.tv_sec == VTOSMB(vp)->n_ctime*/) {
1243 		     SMBVDEBUG("use cached vnode\n");
1244 		     return (0);
1245 		}
1246 		cache_purge(vp);
1247 		/*
1248 		 * XXX This is not quite right, if '.' is
1249 		 * inconsistent, we really need to start the lookup
1250 		 * all over again.  Hopefully there is some other
1251 		 * guarantee that prevents this case from happening.
1252 		 */
1253 		if (killit && vp != dvp)
1254 			vgone(vp);
1255 		if (vp != dvp)
1256 			vput(vp);
1257 		else
1258 			vrele(vp);
1259 		*vpp = NULLVP;
1260 	}
1261 	/*
1262 	 * entry is not in the cache or has been expired
1263 	 */
1264 	error = 0;
1265 	*vpp = NULLVP;
1266 	scred = smbfs_malloc_scred();
1267 	smb_makescred(scred, td, cnp->cn_cred);
1268 	fap = &fattr;
1269 	if (flags & ISDOTDOT) {
1270 		/*
1271 		 * In the DOTDOT case, don't go over-the-wire
1272 		 * in order to request attributes. We already
1273 		 * know it's a directory and subsequent call to
1274 		 * smbfs_getattr() will restore consistency.
1275 		 *
1276 		 */
1277 		SMBVDEBUG("smbfs_smb_lookup: dotdot\n");
1278 	} else if (isdot) {
1279 		error = smbfs_smb_lookup(dnp, NULL, 0, fap, scred);
1280 		SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1281 	}
1282 	else {
1283 		error = smbfs_smb_lookup(dnp, name, nmlen, fap, scred);
1284 		SMBVDEBUG("result of smbfs_smb_lookup: %d\n", error);
1285 	}
1286 	if (error && error != ENOENT)
1287 		goto out;
1288 	if (error) {			/* entry not found */
1289 		/*
1290 		 * Handle RENAME or CREATE case...
1291 		 */
1292 		if ((nameiop == CREATE || nameiop == RENAME) && islastcn) {
1293 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1294 			if (error)
1295 				goto out;
1296 			error = EJUSTRETURN;
1297 			goto out;
1298 		}
1299 		error = ENOENT;
1300 		goto out;
1301 	}/* else {
1302 		SMBVDEBUG("Found entry %s with id=%d\n", fap->entryName, fap->dirEntNum);
1303 	}*/
1304 	/*
1305 	 * handle DELETE case ...
1306 	 */
1307 	if (nameiop == DELETE && islastcn) { 	/* delete last component */
1308 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1309 		if (error)
1310 			goto out;
1311 		if (isdot) {
1312 			VREF(dvp);
1313 			*vpp = dvp;
1314 			goto out;
1315 		}
1316 		error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1317 		if (error)
1318 			goto out;
1319 		*vpp = vp;
1320 		goto out;
1321 	}
1322 	if (nameiop == RENAME && islastcn) {
1323 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
1324 		if (error)
1325 			goto out;
1326 		if (isdot) {
1327 			error = EISDIR;
1328 			goto out;
1329 		}
1330 		error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1331 		if (error)
1332 			goto out;
1333 		*vpp = vp;
1334 		goto out;
1335 	}
1336 	if (flags & ISDOTDOT) {
1337 		mp = dvp->v_mount;
1338 		error = vfs_busy(mp, MBF_NOWAIT);
1339 		if (error != 0) {
1340 			vfs_ref(mp);
1341 			VOP_UNLOCK(dvp);
1342 			error = vfs_busy(mp, 0);
1343 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1344 			vfs_rel(mp);
1345 			if (error) {
1346 				error = ENOENT;
1347 				goto out;
1348 			}
1349 			if (VN_IS_DOOMED(dvp)) {
1350 				vfs_unbusy(mp);
1351 				error = ENOENT;
1352 				goto out;
1353 			}
1354 		}
1355 		VOP_UNLOCK(dvp);
1356 		error = smbfs_nget(mp, dvp, name, nmlen, NULL, &vp);
1357 		vfs_unbusy(mp);
1358 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1359 		if (VN_IS_DOOMED(dvp)) {
1360 			if (error == 0)
1361 				vput(vp);
1362 			error = ENOENT;
1363 		}
1364 		if (error)
1365 			goto out;
1366 		*vpp = vp;
1367 	} else if (isdot) {
1368 		vref(dvp);
1369 		*vpp = dvp;
1370 	} else {
1371 		error = smbfs_nget(mp, dvp, name, nmlen, fap, &vp);
1372 		if (error)
1373 			goto out;
1374 		*vpp = vp;
1375 		SMBVDEBUG("lookup: getnewvp!\n");
1376 	}
1377 	if ((cnp->cn_flags & MAKEENTRY)/* && !islastcn*/) {
1378 /*		VTOSMB(*vpp)->n_ctime = VTOSMB(*vpp)->n_vattr.va_ctime.tv_sec;*/
1379 		cache_enter(dvp, *vpp, cnp);
1380 	}
1381 out:
1382 	smbfs_free_scred(scred);
1383 	return (error);
1384 }
1385