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