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  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  * $FreeBSD: releng/9.0/sys/fs/smbfs/smbfs_node.c 211531 2010-08-20 19:46:50Z jhb $
   27  */
   28 #include <sys/param.h>
   29 #include <sys/systm.h>
   30 #include <sys/kernel.h>
   31 #include <sys/lock.h>
   32 #include <sys/malloc.h>
   33 #include <sys/mount.h>
   34 #include <sys/proc.h>
   35 #include <sys/queue.h>
   36 #include <sys/stat.h>
   37 #include <sys/sx.h>
   38 #include <sys/sysctl.h>
   39 #include <sys/time.h>
   40 #include <sys/vnode.h>
   41 
   42 #include <netsmb/smb.h>
   43 #include <netsmb/smb_conn.h>
   44 #include <netsmb/smb_subr.h>
   45 
   46 #include <vm/vm.h>
   47 #include <vm/vm_extern.h>
   48 /*#include <vm/vm_page.h>
   49 #include <vm/vm_object.h>*/
   50 
   51 #include <fs/smbfs/smbfs.h>
   52 #include <fs/smbfs/smbfs_node.h>
   53 #include <fs/smbfs/smbfs_subr.h>
   54 
   55 #define SMBFS_NOHASH(smp, hval) (&(smp)->sm_hash[(hval) & (smp)->sm_hashlen])
   56 #define smbfs_hash_lock(smp)    sx_xlock(&smp->sm_hashlock)
   57 #define smbfs_hash_unlock(smp)  sx_xunlock(&smp->sm_hashlock)
   58 
   59 extern struct vop_vector smbfs_vnodeops;        /* XXX -> .h file */
   60 
   61 MALLOC_DEFINE(M_SMBNODE, "smbufs_node", "SMBFS vnode private part");
   62 static MALLOC_DEFINE(M_SMBNODENAME, "smbufs_nname", "SMBFS node name");
   63 
   64 int smbfs_hashprint(struct mount *mp);
   65 
   66 #if 0
   67 #ifdef SYSCTL_DECL
   68 SYSCTL_DECL(_vfs_smbfs);
   69 #endif
   70 SYSCTL_PROC(_vfs_smbfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
   71             NULL, 0, smbfs_hashprint, "S,vnlist", "vnode hash");
   72 #endif
   73 
   74 #define FNV_32_PRIME ((u_int32_t) 0x01000193UL)
   75 #define FNV1_32_INIT ((u_int32_t) 33554467UL)
   76 
   77 u_int32_t
   78 smbfs_hash(const u_char *name, int nmlen)
   79 {
   80         u_int32_t v;
   81 
   82         for (v = FNV1_32_INIT; nmlen; name++, nmlen--) {
   83                 v *= FNV_32_PRIME;
   84                 v ^= (u_int32_t)*name;
   85         }
   86         return v;
   87 }
   88 
   89 int
   90 smbfs_hashprint(struct mount *mp)
   91 {
   92         struct smbmount *smp = VFSTOSMBFS(mp);
   93         struct smbnode_hashhead *nhpp;
   94         struct smbnode *np;
   95         int i;
   96 
   97         for(i = 0; i <= smp->sm_hashlen; i++) {
   98                 nhpp = &smp->sm_hash[i];
   99                 LIST_FOREACH(np, nhpp, n_hash)
  100                         vprint("", SMBTOV(np));
  101         }
  102         return 0;
  103 }
  104 
  105 static char *
  106 smbfs_name_alloc(const u_char *name, int nmlen)
  107 {
  108         u_char *cp;
  109 
  110         nmlen++;
  111 #ifdef SMBFS_NAME_DEBUG
  112         cp = malloc(nmlen + 2 + sizeof(int), M_SMBNODENAME, M_WAITOK);
  113         *(int*)cp = nmlen;
  114         cp += sizeof(int);
  115         cp[0] = 0xfc;
  116         cp++;
  117         bcopy(name, cp, nmlen - 1);
  118         cp[nmlen] = 0xfe;
  119 #else
  120         cp = malloc(nmlen, M_SMBNODENAME, M_WAITOK);
  121         bcopy(name, cp, nmlen - 1);
  122 #endif
  123         cp[nmlen - 1] = 0;
  124         return cp;
  125 }
  126 
  127 static void
  128 smbfs_name_free(u_char *name)
  129 {
  130 #ifdef SMBFS_NAME_DEBUG
  131         int nmlen, slen;
  132         u_char *cp;
  133 
  134         cp = name;
  135         cp--;
  136         if (*cp != 0xfc)
  137                 panic("First byte of name entry '%s' corrupted", name);
  138         cp -= sizeof(int);
  139         nmlen = *(int*)cp;
  140         slen = strlen(name) + 1;
  141         if (nmlen != slen)
  142                 panic("Name length mismatch: was %d, now %d name '%s'",
  143                     nmlen, slen, name);
  144         if (name[nmlen] != 0xfe)
  145                 panic("Last byte of name entry '%s' corrupted\n", name);
  146         free(cp, M_SMBNODENAME);
  147 #else
  148         free(name, M_SMBNODENAME);
  149 #endif
  150 }
  151 
  152 static int
  153 smbfs_node_alloc(struct mount *mp, struct vnode *dvp,
  154         const char *name, int nmlen, struct smbfattr *fap, struct vnode **vpp)
  155 {
  156         struct vattr vattr;
  157         struct thread *td = curthread;  /* XXX */
  158         struct smbmount *smp = VFSTOSMBFS(mp);
  159         struct smbnode_hashhead *nhpp;
  160         struct smbnode *np, *np2, *dnp;
  161         struct vnode *vp;
  162         u_long hashval;
  163         int error;
  164 
  165         *vpp = NULL;
  166         if (smp->sm_root != NULL && dvp == NULL) {
  167                 SMBERROR("do not allocate root vnode twice!\n");
  168                 return EINVAL;
  169         }
  170         if (nmlen == 2 && bcmp(name, "..", 2) == 0) {
  171                 if (dvp == NULL)
  172                         return EINVAL;
  173                 vp = VTOSMB(VTOSMB(dvp)->n_parent)->n_vnode;
  174                 error = vget(vp, LK_EXCLUSIVE, td);
  175                 if (error == 0)
  176                         *vpp = vp;
  177                 return error;
  178         } else if (nmlen == 1 && name[0] == '.') {
  179                 SMBERROR("do not call me with dot!\n");
  180                 return EINVAL;
  181         }
  182         dnp = dvp ? VTOSMB(dvp) : NULL;
  183         if (dnp == NULL && dvp != NULL) {
  184                 vprint("smbfs_node_alloc: dead parent vnode", dvp);
  185                 return EINVAL;
  186         }
  187         hashval = smbfs_hash(name, nmlen);
  188 retry:
  189         smbfs_hash_lock(smp);
  190 loop:
  191         nhpp = SMBFS_NOHASH(smp, hashval);
  192         LIST_FOREACH(np, nhpp, n_hash) {
  193                 vp = SMBTOV(np);
  194                 if (np->n_parent != dvp ||
  195                     np->n_nmlen != nmlen || bcmp(name, np->n_name, nmlen) != 0)
  196                         continue;
  197                 VI_LOCK(vp);
  198                 smbfs_hash_unlock(smp);
  199                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) != 0)
  200                         goto retry;
  201                 /* Force cached attributes to be refreshed if stale. */
  202                 (void)VOP_GETATTR(vp, &vattr, td->td_ucred);
  203                 /*
  204                  * If the file type on the server is inconsistent with
  205                  * what it was when we created the vnode, kill the
  206                  * bogus vnode now and fall through to the code below
  207                  * to create a new one with the right type.
  208                  */
  209                 if ((vp->v_type == VDIR && (np->n_dosattr & SMB_FA_DIR) == 0) ||
  210                     (vp->v_type == VREG && (np->n_dosattr & SMB_FA_DIR) != 0)) {
  211                         vgone(vp);
  212                         vput(vp);
  213                         break;
  214                 }
  215                 *vpp = vp;
  216                 return 0;
  217         }
  218         smbfs_hash_unlock(smp);
  219         /*
  220          * If we don't have node attributes, then it is an explicit lookup
  221          * for an existing vnode.
  222          */
  223         if (fap == NULL)
  224                 return ENOENT;
  225 
  226         np = malloc(sizeof *np, M_SMBNODE, M_WAITOK);
  227         error = getnewvnode("smbfs", mp, &smbfs_vnodeops, &vp);
  228         if (error) {
  229                 free(np, M_SMBNODE);
  230                 return error;
  231         }
  232         error = insmntque(vp, mp);      /* XXX: Too early for mpsafe fs */
  233         if (error != 0) {
  234                 free(np, M_SMBNODE);
  235                 return (error);
  236         }
  237         vp->v_type = fap->fa_attr & SMB_FA_DIR ? VDIR : VREG;
  238         bzero(np, sizeof(*np));
  239         vp->v_data = np;
  240         np->n_vnode = vp;
  241         np->n_mount = VFSTOSMBFS(mp);
  242         np->n_nmlen = nmlen;
  243         np->n_name = smbfs_name_alloc(name, nmlen);
  244         np->n_ino = fap->fa_ino;
  245 
  246         if (dvp) {
  247                 ASSERT_VOP_LOCKED(dvp, "smbfs_node_alloc");
  248                 np->n_parent = dvp;
  249                 if (/*vp->v_type == VDIR &&*/ (dvp->v_vflag & VV_ROOT) == 0) {
  250                         vref(dvp);
  251                         np->n_flag |= NREFPARENT;
  252                 }
  253         } else if (vp->v_type == VREG)
  254                 SMBERROR("new vnode '%s' born without parent ?\n", np->n_name);
  255 
  256         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  257         VN_LOCK_AREC(vp);
  258 
  259         smbfs_hash_lock(smp);
  260         LIST_FOREACH(np2, nhpp, n_hash) {
  261                 if (np2->n_parent != dvp ||
  262                     np2->n_nmlen != nmlen || bcmp(name, np2->n_name, nmlen) != 0)
  263                         continue;
  264                 vput(vp);
  265 /*              smb_name_free(np->n_name);
  266                 free(np, M_SMBNODE);*/
  267                 goto loop;
  268         }
  269         LIST_INSERT_HEAD(nhpp, np, n_hash);
  270         smbfs_hash_unlock(smp);
  271         *vpp = vp;
  272         return 0;
  273 }
  274 
  275 int
  276 smbfs_nget(struct mount *mp, struct vnode *dvp, const char *name, int nmlen,
  277         struct smbfattr *fap, struct vnode **vpp)
  278 {
  279         struct smbnode *np;
  280         struct vnode *vp;
  281         int error;
  282 
  283         *vpp = NULL;
  284         error = smbfs_node_alloc(mp, dvp, name, nmlen, fap, &vp);
  285         if (error)
  286                 return error;
  287         np = VTOSMB(vp);
  288         if (fap)
  289                 smbfs_attr_cacheenter(vp, fap);
  290         *vpp = vp;
  291         return 0;
  292 }
  293 
  294 /*
  295  * Free smbnode, and give vnode back to system
  296  */
  297 int
  298 smbfs_reclaim(ap)                     
  299         struct vop_reclaim_args /* {
  300                 struct vnode *a_vp;
  301                 struct thread *a_p;
  302         } */ *ap;
  303 {
  304         struct vnode *vp = ap->a_vp;
  305         struct vnode *dvp;
  306         struct smbnode *np = VTOSMB(vp);
  307         struct smbmount *smp = VTOSMBFS(vp);
  308         
  309         SMBVDEBUG("%s,%d\n", np->n_name, vrefcnt(vp));
  310 
  311         KASSERT((np->n_flag & NOPEN) == 0, ("file not closed before reclaim"));
  312 
  313         smbfs_hash_lock(smp);
  314         /*
  315          * Destroy the vm object and flush associated pages.
  316          */
  317         vnode_destroy_vobject(vp);
  318 
  319         dvp = (np->n_parent && (np->n_flag & NREFPARENT)) ?
  320             np->n_parent : NULL;
  321 
  322         if (np->n_hash.le_prev)
  323                 LIST_REMOVE(np, n_hash);
  324         if (smp->sm_root == np) {
  325                 SMBVDEBUG("root vnode\n");
  326                 smp->sm_root = NULL;
  327         }
  328         vp->v_data = NULL;
  329         smbfs_hash_unlock(smp);
  330         if (np->n_name)
  331                 smbfs_name_free(np->n_name);
  332         free(np, M_SMBNODE);
  333         if (dvp != NULL) {
  334                 vrele(dvp);
  335                 /*
  336                  * Indicate that we released something; see comment
  337                  * in smbfs_unmount().
  338                  */
  339                 smp->sm_didrele = 1;
  340         }
  341         return 0;
  342 }
  343 
  344 int
  345 smbfs_inactive(ap)
  346         struct vop_inactive_args /* {
  347                 struct vnode *a_vp;
  348                 struct thread *a_td;
  349         } */ *ap;
  350 {
  351         struct thread *td = ap->a_td;
  352         struct ucred *cred = td->td_ucred;
  353         struct vnode *vp = ap->a_vp;
  354         struct smbnode *np = VTOSMB(vp);
  355         struct smb_cred scred;
  356         struct vattr va;
  357 
  358         SMBVDEBUG("%s: %d\n", VTOSMB(vp)->n_name, vrefcnt(vp));
  359         if ((np->n_flag & NOPEN) != 0) {
  360                 smb_makescred(&scred, td, cred);
  361                 smbfs_vinvalbuf(vp, td);
  362                 if (vp->v_type == VREG) {
  363                         VOP_GETATTR(vp, &va, cred);
  364                         smbfs_smb_close(np->n_mount->sm_share, np->n_fid,
  365                             &np->n_mtime, &scred);
  366                 } else if (vp->v_type == VDIR) {
  367                         if (np->n_dirseq != NULL) {
  368                                 smbfs_findclose(np->n_dirseq, &scred);
  369                                 np->n_dirseq = NULL;
  370                         }
  371                 }
  372                 np->n_flag &= ~NOPEN;
  373                 smbfs_attr_cacheremove(vp);
  374         }
  375         if (np->n_flag & NGONE)
  376                 vrecycle(vp, td);
  377         return (0);
  378 }
  379 /*
  380  * routines to maintain vnode attributes cache
  381  * smbfs_attr_cacheenter: unpack np.i to vattr structure
  382  */
  383 void
  384 smbfs_attr_cacheenter(struct vnode *vp, struct smbfattr *fap)
  385 {
  386         struct smbnode *np = VTOSMB(vp);
  387 
  388         if (vp->v_type == VREG) {
  389                 if (np->n_size != fap->fa_size) {
  390                         np->n_size = fap->fa_size;
  391                         vnode_pager_setsize(vp, np->n_size);
  392                 }
  393         } else if (vp->v_type == VDIR) {
  394                 np->n_size = 16384;             /* should be a better way ... */
  395         } else
  396                 return;
  397         np->n_mtime = fap->fa_mtime;
  398         np->n_dosattr = fap->fa_attr;
  399         np->n_attrage = time_second;
  400         return;
  401 }
  402 
  403 int
  404 smbfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
  405 {
  406         struct smbnode *np = VTOSMB(vp);
  407         struct smbmount *smp = VTOSMBFS(vp);
  408         int diff;
  409 
  410         diff = time_second - np->n_attrage;
  411         if (diff > 2)   /* XXX should be configurable */
  412                 return ENOENT;
  413         va->va_type = vp->v_type;               /* vnode type (for create) */
  414         if (vp->v_type == VREG) {
  415                 va->va_mode = smp->sm_file_mode; /* files access mode and type */
  416                 if (np->n_dosattr & SMB_FA_RDONLY)
  417                         va->va_mode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
  418         } else if (vp->v_type == VDIR) {
  419                 va->va_mode = smp->sm_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_uid;       /* owner user id */
  425         va->va_gid = smp->sm_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 = NODEV;            /* 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: 68446d2d6f9bcbfdd298495e9a844886


[ 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.