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