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/pseudofs/pseudofs.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) 2001 Dag-Erling Coïdan Smørgrav
    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  *    in this position and unchanged.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  *
   28  *      $FreeBSD: src/sys/fs/pseudofs/pseudofs.c,v 1.22 2004/07/30 22:08:50 phk Exp $
   29  */
   30 
   31 #include <sys/param.h>
   32 #include <sys/kernel.h>
   33 #include <sys/systm.h>
   34 #include <sys/lock.h>
   35 #include <sys/malloc.h>
   36 #include <sys/module.h>
   37 #include <sys/mount.h>
   38 #include <sys/mutex.h>
   39 #include <sys/proc.h>
   40 #include <sys/sbuf.h>
   41 #include <sys/sysctl.h>
   42 #include <sys/vnode.h>
   43 
   44 #include <fs/pseudofs/pseudofs.h>
   45 #include <fs/pseudofs/pseudofs_internal.h>
   46 
   47 static MALLOC_DEFINE(M_PFSNODES, "pfs_nodes", "pseudofs nodes");
   48 
   49 SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW, 0,
   50     "pseudofs");
   51 
   52 #if PFS_FSNAMELEN != MFSNAMELEN
   53 #error "PFS_FSNAMELEN is not equal to MFSNAMELEN"
   54 #endif
   55 
   56 /*
   57  * Add a node to a directory
   58  */
   59 static int
   60 _pfs_add_node(struct pfs_node *parent, struct pfs_node *node)
   61 {
   62         KASSERT(parent != NULL,
   63             ("%s(): parent is NULL", __func__));
   64         KASSERT(parent->pn_info != NULL,
   65             ("%s(): parent has no pn_info", __func__));
   66         KASSERT(parent->pn_type == pfstype_dir ||
   67             parent->pn_type == pfstype_procdir ||
   68             parent->pn_type == pfstype_root,
   69             ("%s(): parent is not a directory", __func__));
   70 
   71         /* XXX should check for duplicate names etc. */
   72 
   73         mtx_lock(&parent->pn_info->pi_mutex);
   74         node->pn_info = parent->pn_info;
   75         node->pn_parent = parent;
   76         node->pn_next = parent->pn_nodes;
   77         parent->pn_nodes = node;
   78         /* Propagate flag to all child nodes (and thus their vnodes) */
   79         if ((parent->pn_flags & PFS_PROCDEP) != 0)
   80                 node->pn_flags |= PFS_PROCDEP;
   81         mtx_unlock(&parent->pn_info->pi_mutex);
   82 
   83         return (0);
   84 }
   85 
   86 /*
   87  * Add . and .. to a directory
   88  */
   89 static int
   90 _pfs_fixup_dir(struct pfs_node *parent)
   91 {
   92         struct pfs_node *dir;
   93 
   94         MALLOC(dir, struct pfs_node *, sizeof *dir,
   95             M_PFSNODES, M_WAITOK|M_ZERO);
   96         dir->pn_name[0] = '.';
   97         dir->pn_type = pfstype_this;
   98 
   99         if (_pfs_add_node(parent, dir) != 0) {
  100                 FREE(dir, M_PFSNODES);
  101                 return (-1);
  102         }
  103 
  104         MALLOC(dir, struct pfs_node *, sizeof *dir,
  105             M_PFSNODES, M_WAITOK|M_ZERO);
  106         dir->pn_name[0] = dir->pn_name[1] = '.';
  107         dir->pn_type = pfstype_parent;
  108 
  109         if (_pfs_add_node(parent, dir) != 0) {
  110                 FREE(dir, M_PFSNODES);
  111                 return (-1);
  112         }
  113 
  114         return (0);
  115 }
  116 
  117 /*
  118  * Create a directory
  119  */
  120 struct pfs_node *
  121 pfs_create_dir(struct pfs_node *parent, const char *name,
  122                pfs_attr_t attr, pfs_vis_t vis, int flags)
  123 {
  124         struct pfs_node *dir;
  125 
  126         KASSERT(strlen(name) < PFS_NAMELEN,
  127             ("%s(): node name is too long", __func__));
  128 
  129         MALLOC(dir, struct pfs_node *, sizeof *dir,
  130             M_PFSNODES, M_WAITOK|M_ZERO);
  131         strcpy(dir->pn_name, name);
  132         dir->pn_type = (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir;
  133         dir->pn_attr = attr;
  134         dir->pn_vis = vis;
  135         dir->pn_flags = flags;
  136 
  137         if (_pfs_add_node(parent, dir) != 0) {
  138                 FREE(dir, M_PFSNODES);
  139                 return (NULL);
  140         }
  141 
  142         if (_pfs_fixup_dir(dir) != 0) {
  143                 pfs_destroy(dir);
  144                 return (NULL);
  145         }
  146 
  147         return (dir);
  148 }
  149 
  150 /*
  151  * Create a file
  152  */
  153 struct pfs_node *
  154 pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill,
  155                 pfs_attr_t attr, pfs_vis_t vis, int flags)
  156 {
  157         struct pfs_node *node;
  158 
  159         KASSERT(strlen(name) < PFS_NAMELEN,
  160             ("%s(): node name is too long", __func__));
  161 
  162         MALLOC(node, struct pfs_node *, sizeof *node,
  163             M_PFSNODES, M_WAITOK|M_ZERO);
  164         strcpy(node->pn_name, name);
  165         node->pn_type = pfstype_file;
  166         node->pn_func = fill;
  167         node->pn_attr = attr;
  168         node->pn_vis = vis;
  169         node->pn_flags = flags;
  170 
  171         if (_pfs_add_node(parent, node) != 0) {
  172                 FREE(node, M_PFSNODES);
  173                 return (NULL);
  174         }
  175 
  176         return (node);
  177 }
  178 
  179 /*
  180  * Create a symlink
  181  */
  182 struct pfs_node *
  183 pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill,
  184                 pfs_attr_t attr, pfs_vis_t vis, int flags)
  185 {
  186         struct pfs_node *node;
  187 
  188         node = pfs_create_file(parent, name, fill, attr, vis, flags);
  189         if (node == NULL)
  190                 return (NULL);
  191         node->pn_type = pfstype_symlink;
  192         return (node);
  193 }
  194 
  195 /*
  196  * Locate a node by name
  197  */
  198 struct pfs_node *
  199 pfs_find_node(struct pfs_node *parent, const char *name)
  200 {
  201         struct pfs_node *node;
  202 
  203         for (node = parent->pn_nodes; node != NULL; node = node->pn_next)
  204                 if (strcmp(node->pn_name, name) == 0)
  205                         return (node);
  206         return (NULL);
  207 }
  208 
  209 /*
  210  * Destroy a node or a tree of nodes
  211  */
  212 int
  213 pfs_destroy(struct pfs_node *node)
  214 {
  215         struct pfs_node *parent, *rover;
  216 
  217         KASSERT(node != NULL,
  218             ("%s(): node is NULL", __func__));
  219         KASSERT(node->pn_info != NULL,
  220             ("%s(): node has no pn_info", __func__));
  221 
  222         /* destroy children */
  223         if (node->pn_type == pfstype_dir ||
  224             node->pn_type == pfstype_procdir ||
  225             node->pn_type == pfstype_root)
  226                 while (node->pn_nodes != NULL)
  227                         pfs_destroy(node->pn_nodes);
  228 
  229         /* unlink from parent */
  230         if ((parent = node->pn_parent) != NULL) {
  231                 KASSERT(parent->pn_info == node->pn_info,
  232                     ("%s(): parent has different pn_info", __func__));
  233                 mtx_lock(&node->pn_info->pi_mutex);
  234                 if (parent->pn_nodes == node) {
  235                         parent->pn_nodes = node->pn_next;
  236                 } else {
  237                         rover = parent->pn_nodes;
  238                         while (rover->pn_next != NULL) {
  239                                 if (rover->pn_next == node) {
  240                                         rover->pn_next = node->pn_next;
  241                                         break;
  242                                 }
  243                                 rover = rover->pn_next;
  244                         }
  245                 }
  246                 mtx_unlock(&node->pn_info->pi_mutex);
  247         }
  248 
  249         /* revoke vnodes and release memory */
  250         pfs_disable(node);
  251         FREE(node, M_PFSNODES);
  252 
  253         return (0);
  254 }
  255 
  256 /*
  257  * Mount a pseudofs instance
  258  */
  259 int
  260 pfs_mount(struct pfs_info *pi, struct mount *mp, struct thread *td)
  261 {
  262         struct statfs *sbp;
  263 
  264         if (mp->mnt_flag & MNT_UPDATE)
  265                 return (EOPNOTSUPP);
  266 
  267         mp->mnt_flag |= MNT_LOCAL;
  268         mp->mnt_data = (qaddr_t)pi;
  269         vfs_getnewfsid(mp);
  270 
  271         sbp = &mp->mnt_stat;
  272         bcopy(pi->pi_name, sbp->f_mntfromname, sizeof pi->pi_name);
  273         sbp->f_bsize = PAGE_SIZE;
  274         sbp->f_iosize = PAGE_SIZE;
  275         sbp->f_blocks = 1;
  276         sbp->f_bfree = 0;
  277         sbp->f_bavail = 0;
  278         sbp->f_files = 1;
  279         sbp->f_ffree = 0;
  280 
  281         return (0);
  282 }
  283 
  284 /*
  285  * Unmount a pseudofs instance
  286  */
  287 int
  288 pfs_unmount(struct mount *mp, int mntflags, struct thread *td)
  289 {
  290         struct pfs_info *pi;
  291         int error;
  292 
  293         pi = (struct pfs_info *)mp->mnt_data;
  294 
  295         /* XXX do stuff with pi... */
  296 
  297         error = vflush(mp, 0, (mntflags & MNT_FORCE) ?  FORCECLOSE : 0, td);
  298         return (error);
  299 }
  300 
  301 /*
  302  * Return a root vnode
  303  */
  304 int
  305 pfs_root(struct mount *mp, struct vnode **vpp, struct thread *td)
  306 {
  307         struct pfs_info *pi;
  308 
  309         pi = (struct pfs_info *)mp->mnt_data;
  310         return pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID);
  311 }
  312 
  313 /*
  314  * Return filesystem stats
  315  */
  316 int
  317 pfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
  318 {
  319         bcopy(&mp->mnt_stat, sbp, sizeof *sbp);
  320         return (0);
  321 }
  322 
  323 /*
  324  * Initialize a pseudofs instance
  325  */
  326 int
  327 pfs_init(struct pfs_info *pi, struct vfsconf *vfc)
  328 {
  329         struct pfs_node *root;
  330         int error;
  331 
  332         mtx_init(&pi->pi_mutex, "pseudofs", NULL, MTX_DEF);
  333 
  334         /* set up the root diretory */
  335         MALLOC(root, struct pfs_node *, sizeof *root,
  336             M_PFSNODES, M_WAITOK|M_ZERO);
  337         root->pn_type = pfstype_root;
  338         root->pn_name[0] = '/';
  339         root->pn_info = pi;
  340         if (_pfs_fixup_dir(root) != 0) {
  341                 FREE(root, M_PFSNODES);
  342                 return (ENODEV); /* XXX not really the right errno */
  343         }
  344         pi->pi_root = root;
  345 
  346         /* construct file hierarchy */
  347         error = (pi->pi_init)(pi, vfc);
  348         if (error) {
  349                 pfs_destroy(root);
  350                 pi->pi_root = NULL;
  351                 mtx_destroy(&pi->pi_mutex);
  352                 return (error);
  353         }
  354 
  355         pfs_fileno_init(pi);
  356         if (bootverbose)
  357                 printf("%s registered\n", pi->pi_name);
  358         return (0);
  359 }
  360 
  361 /*
  362  * Destroy a pseudofs instance
  363  */
  364 int
  365 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc)
  366 {
  367         int error;
  368 
  369         pfs_fileno_uninit(pi);
  370         pfs_destroy(pi->pi_root);
  371         pi->pi_root = NULL;
  372         mtx_destroy(&pi->pi_mutex);
  373         if (bootverbose)
  374                 printf("%s unregistered\n", pi->pi_name);
  375         error = (pi->pi_uninit)(pi, vfc);
  376         return (error);
  377 }
  378 
  379 /*
  380  * Handle load / unload events
  381  */
  382 static int
  383 pfs_modevent(module_t mod, int evt, void *arg)
  384 {
  385         switch (evt) {
  386         case MOD_LOAD:
  387                 pfs_fileno_load();
  388                 pfs_vncache_load();
  389                 break;
  390         case MOD_UNLOAD:
  391         case MOD_SHUTDOWN:
  392                 pfs_vncache_unload();
  393                 pfs_fileno_unload();
  394                 break;
  395         default:
  396                 return EOPNOTSUPP;
  397                 break;
  398         }
  399         return 0;
  400 }
  401 
  402 /*
  403  * Module declaration
  404  */
  405 static moduledata_t pseudofs_data = {
  406         "pseudofs",
  407         pfs_modevent,
  408         NULL
  409 };
  410 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST);
  411 MODULE_VERSION(pseudofs, 1);

Cache object: 05673212d25c9f90e589fde95ae0b618


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