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/nwfs/nwfs_node.c

Version: -  FREEBSD  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 1999, 2000 Boris Popov
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *    This product includes software developed by Boris Popov.
   16  * 4. Neither the name of the author nor the names of any co-contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  * $FreeBSD: src/sys/nwfs/nwfs_node.c,v 1.3.2.8 2001/12/25 01:44:45 dillon Exp $
   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 <netncp/ncp.h>
   50 #include <netncp/ncp_conn.h>
   51 #include <netncp/ncp_subr.h>
   52 
   53 #include <nwfs/nwfs.h>
   54 #include <nwfs/nwfs_mount.h>
   55 #include <nwfs/nwfs_node.h>
   56 #include <nwfs/nwfs_subr.h>
   57 
   58 #define NWNOHASH(fhsum) (&nwhashtbl[(fhsum.f_id) & nwnodehash])
   59 
   60 extern vop_t **nwfs_vnodeop_p;
   61 
   62 static LIST_HEAD(nwnode_hash_head,nwnode) *nwhashtbl;
   63 static u_long nwnodehash;
   64 static struct lock nwhashlock;
   65 
   66 MALLOC_DEFINE(M_NWNODE, "NWFS node", "NWFS vnode private part");
   67 MALLOC_DEFINE(M_NWFSHASH, "NWFS hash", "NWFS has table");
   68 
   69 static int nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS);
   70 
   71 extern struct linker_set sysctl_vfs_nwfs;
   72 
   73 SYSCTL_DECL(_vfs_nwfs);
   74 
   75 SYSCTL_PROC(_vfs_nwfs, OID_AUTO, vnprint, CTLFLAG_WR|CTLTYPE_OPAQUE,
   76             NULL, 0, nwfs_sysctl_vnprint, "S,vnlist", "vnode hash");
   77 
   78 void
   79 nwfs_hash_init(void) {
   80         nwhashtbl = hashinit(desiredvnodes, M_NWFSHASH, &nwnodehash);
   81         lockinit(&nwhashlock, PVFS, "nwfshl", 0, 0);
   82 }
   83 
   84 void
   85 nwfs_hash_free(void) {
   86         free(nwhashtbl, M_NWFSHASH);
   87 }
   88 
   89 int
   90 nwfs_sysctl_vnprint(SYSCTL_HANDLER_ARGS) {
   91         struct nwnode *np;
   92         struct nwnode_hash_head *nhpp;
   93         struct vnode *vp;
   94         int i;
   95 
   96         if (nwfs_debuglevel == 0)
   97                 return 0;
   98         printf("Name:uc:hc:fid:pfid\n");
   99         for(i = 0; i <= nwnodehash; i++) {
  100                 nhpp = &nwhashtbl[i];
  101                 LIST_FOREACH(np, nhpp, n_hash) {
  102                         vp = NWTOV(np);
  103                         vprint(NULL, vp);
  104                         printf("%s:%d:%d:%d:%d\n",np->n_name,vp->v_usecount,vp->v_holdcnt,
  105                             np->n_fid.f_id, np->n_fid.f_parent);
  106                 }
  107         }
  108         return 0;
  109 }
  110 
  111 /*
  112  * Search nwnode with given fid.
  113  * Hash list should be locked by caller.
  114  */
  115 static int
  116 nwfs_hashlookup(struct nwmount *nmp, ncpfid fid, struct nwnode **npp)
  117 {
  118         struct nwnode *np;
  119         struct nwnode_hash_head *nhpp;
  120 
  121         nhpp = NWNOHASH(fid);
  122         LIST_FOREACH(np, nhpp, n_hash) {
  123                 if (nmp != np->n_mount || !NWCMPF(&fid, &np->n_fid))
  124                         continue;
  125                 if (npp)
  126                         *npp = np;
  127                 return 0;
  128         }
  129         return ENOENT;
  130 }
  131 
  132 /*
  133  * Allocate new nwfsnode/vnode from given nwnode. 
  134  * Vnode referenced and not locked.
  135  */
  136 int
  137 nwfs_allocvp(struct mount *mp, ncpfid fid, struct vnode **vpp)
  138 {
  139         struct proc *p = curproc;       /* XXX */
  140         struct nwnode *np;
  141         struct nwnode_hash_head *nhpp;
  142         struct nwmount *nmp = VFSTONWFS(mp);
  143         struct vnode *vp;
  144         int error;
  145 
  146 loop:
  147         lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, p);
  148 rescan:
  149         if (nwfs_hashlookup(nmp, fid, &np) == 0) {
  150                 vp = NWTOV(np);
  151                 simple_lock(&vp->v_interlock);
  152                 lockmgr(&nwhashlock, LK_RELEASE, NULL, p);
  153                 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p))
  154                         goto loop;
  155                 *vpp = vp;
  156                 return(0);
  157         }
  158         lockmgr(&nwhashlock, LK_RELEASE, NULL, p);
  159 
  160         /*
  161          * Do the MALLOC before the getnewvnode since doing so afterward
  162          * might cause a bogus v_data pointer to get dereferenced
  163          * elsewhere if MALLOC should block.
  164          */
  165         MALLOC(np, struct nwnode *, sizeof *np, M_NWNODE, M_WAITOK | M_ZERO);
  166         error = getnewvnode(VT_NWFS, mp, nwfs_vnodeop_p, &vp);
  167         if (error) {
  168                 *vpp = NULL;
  169                 FREE(np, M_NWNODE);
  170                 return (error);
  171         }
  172         vp->v_data = np;
  173         np->n_vnode = vp;
  174         np->n_mount = nmp;
  175         lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, p);
  176         /*
  177          * Another process can create vnode while we blocked in malloc() or
  178          * getnewvnode(). Rescan list again.
  179          */
  180         if (nwfs_hashlookup(nmp, fid, NULL) == 0) {
  181                 vp->v_data = NULL;
  182                 np->n_vnode = NULL;
  183                 vrele(vp);
  184                 FREE(np, M_NWNODE);
  185                 goto rescan;
  186         }
  187         *vpp = vp;
  188         np->n_fid = fid;
  189         np->n_flag |= NNEW;
  190         lockinit(&np->n_lock, PINOD, "nwnode", VLKTIMEOUT, LK_CANRECURSE);
  191         nhpp = NWNOHASH(fid);
  192         LIST_INSERT_HEAD(nhpp, np, n_hash);
  193         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
  194         lockmgr(&nwhashlock, LK_RELEASE, NULL, p);
  195         return 0;
  196 }
  197 
  198 int
  199 nwfs_lookupnp(struct nwmount *nmp, ncpfid fid, struct proc *p,
  200         struct nwnode **npp)
  201 {
  202         int error;
  203 
  204         lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, p);
  205         error = nwfs_hashlookup(nmp, fid, npp);
  206         lockmgr(&nwhashlock, LK_RELEASE, NULL, p);
  207         return error;
  208 }
  209 
  210 /*
  211  * Free nwnode, and give vnode back to system
  212  */
  213 int
  214 nwfs_reclaim(ap)                     
  215         struct vop_reclaim_args /* {
  216                 struct vnode *a_vp;
  217                 struct proc *a_p;
  218         } */ *ap;
  219 {
  220         struct vnode *dvp = NULL, *vp = ap->a_vp;
  221         struct nwnode *dnp, *np = VTONW(vp);
  222         struct nwmount *nmp = VTONWFS(vp);
  223         struct proc *p = ap->a_p;
  224         
  225         NCPVNDEBUG("%s,%d\n", np->n_name, vp->v_usecount);
  226         if (np->n_refparent) {
  227                 np->n_refparent = 0;
  228                 if (nwfs_lookupnp(nmp, np->n_parent, p, &dnp) == 0) {
  229                         dvp = dnp->n_vnode;
  230                 } else {
  231                         NCPVNDEBUG("%s: has no parent ?\n",np->n_name);
  232                 }
  233         }
  234         lockmgr(&nwhashlock, LK_EXCLUSIVE, NULL, p);
  235         LIST_REMOVE(np, n_hash);
  236         lockmgr(&nwhashlock, LK_RELEASE, NULL, p);
  237         cache_purge(vp);
  238         if (nmp->n_root == np) {
  239                 nmp->n_root = NULL;
  240         }
  241         vp->v_data = NULL;
  242         FREE(np, M_NWNODE);
  243         if (dvp) {
  244                 vrele(dvp);
  245         }
  246         return (0);
  247 }
  248 
  249 int
  250 nwfs_inactive(ap)
  251         struct vop_inactive_args /* {
  252                 struct vnode *a_vp;
  253                 struct proc *a_p;
  254         } */ *ap;
  255 {
  256         struct proc *p = ap->a_p;
  257         struct ucred *cred = p->p_ucred;
  258         struct vnode *vp = ap->a_vp;
  259         struct nwnode *np = VTONW(vp);
  260         int error;
  261 
  262         NCPVNDEBUG("%s: %d\n", VTONW(vp)->n_name, vp->v_usecount);
  263         if (np->opened) {
  264                 error = nwfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
  265                 error = ncp_close_file(NWFSTOCONN(VTONWFS(vp)), &np->n_fh, p, cred);
  266                 np->opened = 0;
  267         }
  268         VOP_UNLOCK(vp, 0, p);
  269         if (np->n_flag & NSHOULDFREE) {
  270                 cache_purge(vp);
  271                 vgone(vp);
  272         }
  273         return (0);
  274 }
  275 /*
  276  * routines to maintain vnode attributes cache
  277  * nwfs_attr_cacheenter: unpack np.i to va structure
  278  */
  279 void
  280 nwfs_attr_cacheenter(struct vnode *vp, struct nw_entry_info *fi)
  281 {
  282         struct nwnode *np = VTONW(vp);
  283         struct nwmount *nmp = VTONWFS(vp);
  284         struct vattr *va = &np->n_vattr;
  285 
  286         va->va_type = vp->v_type;               /* vnode type (for create) */
  287         np->n_nmlen = fi->nameLen;
  288         bcopy(fi->entryName, np->n_name, np->n_nmlen);
  289         np->n_name[fi->nameLen] = 0;
  290         if (vp->v_type == VREG) {
  291                 if (va->va_size != fi->dataStreamSize) {
  292                         va->va_size = fi->dataStreamSize;
  293                         vnode_pager_setsize(vp, va->va_size);
  294                 }
  295                 va->va_mode = nmp->m.file_mode; /* files access mode and type */
  296         } else if (vp->v_type == VDIR) {
  297                 va->va_size = 16384;            /* should be a better way ... */
  298                 va->va_mode = nmp->m.dir_mode;  /* files access mode and type */
  299         } else
  300                 return;
  301         np->n_size = va->va_size;
  302         va->va_nlink = 1;               /* number of references to file */
  303         va->va_uid = nmp->m.uid;        /* owner user id */
  304         va->va_gid = nmp->m.gid;        /* owner group id */
  305         va->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
  306         va->va_fileid = np->n_fid.f_id; /* file id */
  307         if (va->va_fileid == 0)
  308                 va->va_fileid = NWFS_ROOT_INO;
  309         va->va_blocksize=nmp->connh->nh_conn->buffer_size;/* blocksize preferred for i/o */
  310         /* time of last modification */
  311         ncp_dos2unixtime(fi->modifyDate, fi->modifyTime, 0, nmp->m.tz, &va->va_mtime);
  312         /* time of last access */
  313         ncp_dos2unixtime(fi->lastAccessDate, 0, 0, nmp->m.tz, &va->va_atime);
  314         va->va_ctime = va->va_mtime;    /* time file changed */
  315         va->va_gen = VNOVAL;            /* generation number of file */
  316         va->va_flags = 0;               /* flags defined for file */
  317         va->va_rdev = VNOVAL;           /* device the special file represents */
  318         va->va_bytes = va->va_size;     /* bytes of disk space held by file */
  319         va->va_filerev = 0;             /* file modification number */
  320         va->va_vaflags = 0;             /* operations flags */
  321         np->n_vattr = *va;
  322         if (np->n_mtime == 0) {
  323                 np->n_mtime = va->va_mtime.tv_sec;
  324         }
  325         np->n_atime = time_second;
  326         np->n_dosfid = fi->DosDirNum;
  327         return;
  328 }
  329 
  330 int
  331 nwfs_attr_cachelookup(struct vnode *vp, struct vattr *va)
  332 {
  333         struct nwnode *np = VTONW(vp);
  334         int diff;
  335 
  336         diff = time_second - np->n_atime;
  337         if (diff > 2) { /* XXX should be configurable */
  338                 return ENOENT;
  339         }
  340         *va = np->n_vattr;
  341         return 0;
  342 }

Cache object: 849461838023b2958550cb3602370701


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