The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/smbfs/smbfs_node.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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$
   33  */
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/kernel.h>
   37 #include <sys/time.h>
   38 #include <sys/proc.h>
   39 #include <sys/mount.h>
   40 #include <sys/vnode.h>
   41 #include <sys/malloc.h>
   42 #include <sys/sysctl.h>
   43 #include <vm/vm.h>
   44 #include <vm/vm_extern.h>
   45 /*#include <vm/vm_page.h>
   46 #include <vm/vm_object.h>*/
   47 #include <sys/queue.h>
   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 #define SMBFS_NOHASH(smp, hval) (&(smp)->sm_hash[(hval) & (smp)->sm_hashlen])
   58 #define smbfs_hash_lock(smp, p)         lockmgr(&smp->sm_hashlock, LK_EXCLUSIVE, NULL, p)
   59 #define smbfs_hash_unlock(smp, p)       lockmgr(&smp->sm_hashlock, LK_RELEASE, NULL, p)
   60 
   61 
   62 extern vop_t **smbfs_vnodeop_p;
   63 
   64 MALLOC_DEFINE(M_SMBNODE, "SMBFS node", "SMBFS vnode private part");
   65 static MALLOC_DEFINE(M_SMBNODENAME, "SMBFS nname", "SMBFS node name");
   66 
   67 int smbfs_hashprint(struct mount *mp);
   68 
   69 #if 0
   70 extern struct linker_set sysctl_vfs_smbfs;
   71 #ifdef SYSCTL_DECL
   72 SYSCTL_DECL(_vfs_smbfs);
   73 #endif
   74 SYSCTL_PROC(_vfs_smbfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
   75             NULL, 0, smbfs_hashprint, "S,vnlist", "vnode hash");
   76 #endif
   77 
   78 #define FNV_32_PRIME ((u_int32_t) 0x01000193UL)
   79 #define FNV1_32_INIT ((u_int32_t) 33554467UL)
   80 
   81 u_int32_t
   82 smbfs_hash(const u_char *name, int nmlen)
   83 {
   84         u_int32_t v;
   85 
   86         for (v = FNV1_32_INIT; nmlen; name++, nmlen--) {
   87                 v *= FNV_32_PRIME;
   88                 v ^= (u_int32_t)*name;
   89         }
   90         return v;
   91 }
   92 
   93 int
   94 smbfs_hashprint(struct mount *mp)
   95 {
   96         struct smbmount *smp = VFSTOSMBFS(mp);
   97         struct smbnode_hashhead *nhpp;
   98         struct smbnode *np;
   99         int i;
  100 
  101         for(i = 0; i <= smp->sm_hashlen; i++) {
  102                 nhpp = &smp->sm_hash[i];
  103                 LIST_FOREACH(np, nhpp, n_hash)
  104                         vprint(NULL, SMBTOV(np));
  105         }
  106         return 0;
  107 }
  108 
  109 static char *
  110 smbfs_name_alloc(const u_char *name, int nmlen)
  111 {
  112         u_char *cp;
  113 
  114         nmlen++;
  115 #ifdef SMBFS_NAME_DEBUG
  116         cp = malloc(nmlen + 2 + sizeof(int), M_SMBNODENAME, M_WAITOK);
  117         *(int*)cp = nmlen;
  118         cp += sizeof(int);
  119         cp[0] = 0xfc;
  120         cp++;
  121         bcopy(name, cp, nmlen - 1);
  122         cp[nmlen] = 0xfe;
  123 #else
  124         cp = malloc(nmlen, M_SMBNODENAME, M_WAITOK);
  125         bcopy(name, cp, nmlen - 1);
  126 #endif
  127         cp[nmlen - 1] = 0;
  128         return cp;
  129 }
  130 
  131 static void
  132 smbfs_name_free(u_char *name)
  133 {
  134 #ifdef SMBFS_NAME_DEBUG
  135         int nmlen, slen;
  136         u_char *cp;
  137 
  138         cp = name;
  139         cp--;
  140         if (*cp != 0xfc) {
  141                 printf("First byte of name entry '%s' corrupted\n", name);
  142                 Debugger("ditto");
  143         }
  144         cp -= sizeof(int);
  145         nmlen = *(int*)cp;
  146         slen = strlen(name) + 1;
  147         if (nmlen != slen) {
  148                 printf("Name length mismatch: was %d, now %d name '%s'\n",
  149                     nmlen, slen, name);
  150                 Debugger("ditto");
  151         }
  152         if (name[nmlen] != 0xfe) {
  153                 printf("Last byte of name entry '%s' corrupted\n", name);
  154                 Debugger("ditto");
  155         }
  156         free(cp, M_SMBNODENAME);
  157 #else
  158         free(name, M_SMBNODENAME);
  159 #endif
  160 }
  161 
  162 static int
  163 smbfs_node_alloc(struct mount *mp, struct vnode *dvp,
  164         const char *name, int nmlen, struct smbfattr *fap, struct vnode **vpp)
  165 {
  166         struct vattr vattr;
  167         struct proc *p = curproc;       /* XXX */
  168         struct smbmount *smp = VFSTOSMBFS(mp);
  169         struct smbnode_hashhead *nhpp;
  170         struct smbnode *np, *np2, *dnp;
  171         struct vnode *vp;
  172         u_long hashval;
  173         int error;
  174 
  175         *vpp = NULL;
  176         if (smp->sm_root != NULL && dvp == NULL) {
  177                 SMBERROR("do not allocate root vnode twice!\n");
  178                 return EINVAL;
  179         }
  180         if (nmlen == 2 && bcmp(name, "..", 2) == 0) {
  181                 if (dvp == NULL)
  182                         return EINVAL;
  183                 vp = VTOSMB(VTOSMB(dvp)->n_parent)->n_vnode;
  184                 error = vget(vp, LK_EXCLUSIVE, p);
  185                 if (error == 0)
  186                         *vpp = vp;
  187                 return error;
  188         } else if (nmlen == 1 && name[0] == '.') {
  189                 SMBERROR("do not call me with dot!\n");
  190                 return EINVAL;
  191         }
  192         dnp = dvp ? VTOSMB(dvp) : NULL;
  193         if (dnp == NULL && dvp != NULL) {
  194                 vprint("smbfs_node_alloc: dead parent vnode", dvp);
  195                 return EINVAL;
  196         }
  197         hashval = smbfs_hash(name, nmlen);
  198 retry:
  199         smbfs_hash_lock(smp, p);
  200 loop:
  201         nhpp = SMBFS_NOHASH(smp, hashval);
  202         LIST_FOREACH(np, nhpp, n_hash) {
  203                 vp = SMBTOV(np);
  204                 if (np->n_parent != dvp ||
  205                     np->n_nmlen != nmlen || bcmp(name, np->n_name, nmlen) != 0)
  206                         continue;
  207                 VI_LOCK(vp);
  208                 smbfs_hash_unlock(smp, p);
  209                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) != 0)
  210                         goto retry;
  211                 /* Force cached attributes to be refreshed if stale. */
  212                 (void)VOP_GETATTR(vp, &vattr, p->p_ucred, p);
  213                 /*
  214                  * If the file type on the server is inconsistent with
  215                  * what it was when we created the vnode, kill the
  216                  * bogus vnode now and fall through to the code below
  217                  * to create a new one with the right type.
  218                  */
  219                 if ((vp->v_type == VDIR && (np->n_dosattr & SMB_FA_DIR) == 0) ||
  220                     (vp->v_type == VREG && (np->n_dosattr & SMB_FA_DIR) != 0)) {
  221                         vput(vp);
  222                         vgone(vp);
  223                         break;
  224                 }
  225                 *vpp = vp;
  226                 return 0;
  227         }
  228         smbfs_hash_unlock(smp, p);
  229         /*
  230          * If we don't have node attributes, then it is an explicit lookup
  231          * for an existing vnode.
  232          */
  233         if (fap == NULL)
  234                 return ENOENT;
  235 
  236         MALLOC(np, struct smbnode *, sizeof *np, M_SMBNODE, M_WAITOK);
  237         error = getnewvnode(VT_SMBFS, mp, smbfs_vnodeop_p, &vp);
  238         if (error) {
  239                 FREE(np, M_SMBNODE);
  240                 return error;
  241         }
  242         vp->v_type = fap->fa_attr & SMB_FA_DIR ? VDIR : VREG;
  243         bzero(np, sizeof(*np));
  244         vp->v_data = np;
  245         np->n_vnode = vp;
  246         np->n_mount = VFSTOSMBFS(mp);
  247         np->n_nmlen = nmlen;
  248         np->n_name = smbfs_name_alloc(name, nmlen);
  249         np->n_ino = fap->fa_ino;
  250 
  251         if (dvp) {
  252                 np->n_parent = dvp;
  253                 if (/*vp->v_type == VDIR &&*/ (dvp->v_flag & VROOT) == 0) {
  254                         vref(dvp);
  255                         np->n_flag |= NREFPARENT;
  256                 }
  257         } else if (vp->v_type == VREG)
  258                 SMBERROR("new vnode '%s' born without parent ?\n", np->n_name);
  259 
  260         lockinit(&np->n_lock, PINOD, "smbnode", VLKTIMEOUT, LK_CANRECURSE);
  261         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
  262 
  263         smbfs_hash_lock(smp, p);
  264         LIST_FOREACH(np2, nhpp, n_hash) {
  265                 if (np2->n_parent != dvp ||
  266                     np2->n_nmlen != nmlen || bcmp(name, np2->n_name, nmlen) != 0)
  267                         continue;
  268                 vput(vp);
  269 /*              smb_name_free(np->n_name);
  270                 FREE(np, M_SMBNODE);*/
  271                 goto loop;
  272         }
  273         LIST_INSERT_HEAD(nhpp, np, n_hash);
  274         smbfs_hash_unlock(smp, p);
  275         *vpp = vp;
  276         return 0;
  277 }
  278 
  279 int
  280 smbfs_nget(struct mount *mp, struct vnode *dvp, const char *name, int nmlen,
  281         struct smbfattr *fap, struct vnode **vpp)
  282 {
  283         struct smbnode *np;
  284         struct vnode *vp;
  285         int error;
  286 
  287         *vpp = NULL;
  288         error = smbfs_node_alloc(mp, dvp, name, nmlen, fap, &vp);
  289         if (error)
  290                 return error;
  291         np = VTOSMB(vp);
  292         if (fap)
  293                 smbfs_attr_cacheenter(vp, fap);
  294         *vpp = vp;
  295         return 0;
  296 }
  297 
  298 /*
  299  * Free smbnode, and give vnode back to system
  300  */
  301 int
  302 smbfs_reclaim(ap)                     
  303         struct vop_reclaim_args /* {
  304                 struct vnode *a_vp;
  305                 struct proc *a_p;
  306         } */ *ap;
  307 {
  308         struct vnode *vp = ap->a_vp;
  309         struct proc *p = ap->a_p;
  310         struct vnode *dvp;
  311         struct smbnode *np = VTOSMB(vp);
  312         struct smbmount *smp = VTOSMBFS(vp);
  313         
  314         SMBVDEBUG("%s,%d\n", np->n_name, vp->v_usecount);
  315 
  316         KASSERT((np->n_flag & NOPEN) == 0, ("file not closed before reclaim"));
  317 
  318         smbfs_hash_lock(smp, p);
  319 
  320         dvp = (np->n_parent && (np->n_flag & NREFPARENT)) ?
  321             np->n_parent : NULL;
  322 
  323         if (np->n_hash.le_prev)
  324                 LIST_REMOVE(np, n_hash);
  325         if (smp->sm_root == np) {
  326                 SMBVDEBUG("root vnode\n");
  327                 smp->sm_root = NULL;
  328         }
  329         vp->v_data = NULL;
  330         smbfs_hash_unlock(smp, p);
  331         if (np->n_name)
  332                 smbfs_name_free(np->n_name);
  333         FREE(np, M_SMBNODE);
  334         if (dvp != NULL) {
  335                 vrele(dvp);
  336                 /*
  337                  * Indicate that we released something; see comment
  338                  * in smbfs_unmount().
  339                  */
  340                 smp->sm_didrele = 1;
  341         }
  342         return 0;
  343 }
  344 
  345 int
  346 smbfs_inactive(ap)
  347         struct vop_inactive_args /* {
  348                 struct vnode *a_vp;
  349                 struct proc *a_p;
  350         } */ *ap;
  351 {
  352         struct proc *p = ap->a_p;
  353         struct ucred *cred = p->p_ucred;
  354         struct vnode *vp = ap->a_vp;
  355         struct smbnode *np = VTOSMB(vp);
  356         struct smb_cred scred;
  357         struct vattr va;
  358 
  359         SMBVDEBUG("%s: %d\n", VTOSMB(vp)->n_name, vp->v_usecount);
  360         if ((np->n_flag & NOPEN) != 0) {
  361                 smb_makescred(&scred, p, cred);
  362                 smbfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
  363                 if (vp->v_type == VREG) {
  364                         VOP_GETATTR(vp, &va, cred, p);
  365                         smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
  366                             &np->n_mtime, &scred);
  367                 } else if (vp->v_type == VDIR) {
  368                         if (np->n_dirseq != NULL) {
  369                                 smbfs_findclose(np->n_dirseq, &scred);
  370                                 np->n_dirseq = NULL;
  371                         }
  372                 }
  373                 np->n_flag &= ~NOPEN;
  374                 smbfs_attr_cacheremove(vp);
  375         }
  376         VOP_UNLOCK(vp, 0, p);
  377         if (np->n_flag & NGONE)
  378                 vrecycle(vp, NULL, p);
  379         return (0);
  380 }
  381 /*
  382  * routines to maintain vnode attributes cache
  383  * smbfs_attr_cacheenter: unpack np.i to vattr structure
  384  */
  385 void
  386 smbfs_attr_cacheenter(struct vnode *vp, struct smbfattr *fap)
  387 {
  388         struct smbnode *np = VTOSMB(vp);
  389 
  390         if (vp->v_type == VREG) {
  391                 if (np->n_size != fap->fa_size) {
  392                         np->n_size = fap->fa_size;
  393                         vnode_pager_setsize(vp, np->n_size);
  394                 }
  395         } else if (vp->v_type == VDIR) {
  396                 np->n_size = 16384;             /* should be a better way ... */
  397         } else
  398                 return;
  399         np->n_mtime = fap->fa_mtime;
  400         np->n_dosattr = fap->fa_attr;
  401         np->n_attrage = time_second;
  402         return;
  403 }
  404 
  405 int
  406 smbfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
  407 {
  408         struct smbnode *np = VTOSMB(vp);
  409         struct smbmount *smp = VTOSMBFS(vp);
  410         int diff;
  411 
  412         diff = time_second - np->n_attrage;
  413         if (diff > 2)   /* XXX should be configurable */
  414                 return ENOENT;
  415         va->va_type = vp->v_type;               /* vnode type (for create) */
  416         if (vp->v_type == VREG) {
  417                 va->va_mode = smp->sm_args.file_mode;   /* files access mode and type */
  418         } else if (vp->v_type == VDIR) {
  419                 va->va_mode = smp->sm_args.dir_mode;    /* files access mode and type */
  420         } else
  421                 return EINVAL;
  422         va->va_size = np->n_size;
  423         va->va_nlink = 1;               /* number of references to file */
  424         va->va_uid = smp->sm_args.uid;  /* owner user id */
  425         va->va_gid = smp->sm_args.gid;  /* owner group id */
  426         va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
  427         va->va_fileid = np->n_ino;      /* file id */
  428         if (va->va_fileid == 0)
  429                 va->va_fileid = 2;
  430         va->va_blocksize = SSTOVC(smp->sm_share)->vc_txmax;
  431         va->va_mtime = np->n_mtime;
  432         va->va_atime = va->va_ctime = va->va_mtime;     /* time file changed */
  433         va->va_gen = VNOVAL;            /* generation number of file */
  434         va->va_flags = 0;               /* flags defined for file */
  435         va->va_rdev = VNOVAL;           /* device the special file represents */
  436         va->va_bytes = va->va_size;     /* bytes of disk space held by file */
  437         va->va_filerev = 0;             /* file modification number */
  438         va->va_vaflags = 0;             /* operations flags */
  439         return 0;
  440 }

Cache object: c13b7342716be9af55e7d223d61190fb


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.