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

Cache object: bcc3bdf360ab9f7e4843b5af4a4cf367


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