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: releng/5.0/sys/fs/pseudofs/pseudofs.c 103314 2002-09-14 09:02:28Z njl $
   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, 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, 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, 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  * Destroy a node or a tree of nodes
  197  */
  198 int
  199 pfs_destroy(struct pfs_node *node)
  200 {
  201         struct pfs_node *parent, *rover;
  202 
  203         KASSERT(node != NULL,
  204             ("%s(): node is NULL", __func__));
  205         KASSERT(node->pn_info != NULL,
  206             ("%s(): node has no pn_info", __func__));
  207 
  208         /* destroy children */
  209         if (node->pn_type == pfstype_dir ||
  210             node->pn_type == pfstype_procdir ||
  211             node->pn_type == pfstype_root)
  212                 while (node->pn_nodes != NULL)
  213                         pfs_destroy(node->pn_nodes);
  214 
  215         /* unlink from parent */
  216         if ((parent = node->pn_parent) != NULL) {
  217                 KASSERT(parent->pn_info == node->pn_info,
  218                     ("%s(): parent has different pn_info", __func__));
  219                 mtx_lock(&node->pn_info->pi_mutex);
  220                 if (parent->pn_nodes == node) {
  221                         parent->pn_nodes = node->pn_next;
  222                 } else {
  223                         rover = parent->pn_nodes;
  224                         while (rover->pn_next != NULL) {
  225                                 if (rover->pn_next == node) {
  226                                         rover->pn_next = node->pn_next;
  227                                         break;
  228                                 }
  229                                 rover = rover->pn_next;
  230                         }
  231                 }
  232                 mtx_unlock(&node->pn_info->pi_mutex);
  233         }
  234 
  235         /* revoke vnodes and release memory */
  236         pfs_disable(node);
  237         FREE(node, M_PFSNODES);
  238 
  239         return (0);
  240 }
  241 
  242 /*
  243  * Mount a pseudofs instance
  244  */
  245 int
  246 pfs_mount(struct pfs_info *pi, struct mount *mp, struct nameidata *ndp,
  247           struct thread *td)
  248 {
  249         struct statfs *sbp;
  250 
  251         if (mp->mnt_flag & MNT_UPDATE)
  252                 return (EOPNOTSUPP);
  253 
  254         mp->mnt_flag |= MNT_LOCAL;
  255         mp->mnt_data = (qaddr_t)pi;
  256         vfs_getnewfsid(mp);
  257 
  258         sbp = &mp->mnt_stat;
  259         bcopy(pi->pi_name, sbp->f_mntfromname, sizeof pi->pi_name);
  260         sbp->f_bsize = PAGE_SIZE;
  261         sbp->f_iosize = PAGE_SIZE;
  262         sbp->f_blocks = 1;
  263         sbp->f_bfree = 0;
  264         sbp->f_bavail = 0;
  265         sbp->f_files = 1;
  266         sbp->f_ffree = 0;
  267 
  268         return (0);
  269 }
  270 
  271 /*
  272  * Unmount a pseudofs instance
  273  */
  274 int
  275 pfs_unmount(struct mount *mp, int mntflags, struct thread *td)
  276 {
  277         struct pfs_info *pi;
  278         int error;
  279 
  280         pi = (struct pfs_info *)mp->mnt_data;
  281 
  282         /* XXX do stuff with pi... */
  283 
  284         error = vflush(mp, 0, (mntflags & MNT_FORCE) ?  FORCECLOSE : 0);
  285         return (error);
  286 }
  287 
  288 /*
  289  * Return a root vnode
  290  */
  291 int
  292 pfs_root(struct mount *mp, struct vnode **vpp)
  293 {
  294         struct pfs_info *pi;
  295 
  296         pi = (struct pfs_info *)mp->mnt_data;
  297         return pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID);
  298 }
  299 
  300 /*
  301  * Return filesystem stats
  302  */
  303 int
  304 pfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
  305 {
  306         bcopy(&mp->mnt_stat, sbp, sizeof *sbp);
  307         return (0);
  308 }
  309 
  310 /*
  311  * Initialize a pseudofs instance
  312  */
  313 int
  314 pfs_init(struct pfs_info *pi, struct vfsconf *vfc)
  315 {
  316         struct pfs_node *root;
  317         int error;
  318 
  319         mtx_init(&pi->pi_mutex, "pseudofs", NULL, MTX_DEF);
  320 
  321         /* set up the root diretory */
  322         MALLOC(root, struct pfs_node *, sizeof *root,
  323             M_PFSNODES, M_WAITOK|M_ZERO);
  324         root->pn_type = pfstype_root;
  325         root->pn_name[0] = '/';
  326         root->pn_info = pi;
  327         if (_pfs_fixup_dir(root) != 0) {
  328                 FREE(root, M_PFSNODES);
  329                 return (ENODEV); /* XXX not really the right errno */
  330         }
  331         pi->pi_root = root;
  332 
  333         /* construct file hierarchy */
  334         error = (pi->pi_init)(pi, vfc);
  335         if (error) {
  336                 pfs_destroy(root);
  337                 pi->pi_root = NULL;
  338                 mtx_destroy(&pi->pi_mutex);
  339                 return (error);
  340         }
  341 
  342         pfs_fileno_init(pi);
  343         if (bootverbose)
  344                 printf("%s registered\n", pi->pi_name);
  345         return (0);
  346 }
  347 
  348 /*
  349  * Destroy a pseudofs instance
  350  */
  351 int
  352 pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc)
  353 {
  354         int error;
  355 
  356         pfs_fileno_uninit(pi);
  357         pfs_destroy(pi->pi_root);
  358         pi->pi_root = NULL;
  359         mtx_destroy(&pi->pi_mutex);
  360         if (bootverbose)
  361                 printf("%s unregistered\n", pi->pi_name);
  362         error = (pi->pi_uninit)(pi, vfc);
  363         return (error);
  364 }
  365 
  366 /*
  367  * Handle load / unload events
  368  */
  369 static int
  370 pfs_modevent(module_t mod, int evt, void *arg)
  371 {
  372         switch (evt) {
  373         case MOD_LOAD:
  374                 pfs_fileno_load();
  375                 pfs_vncache_load();
  376                 break;
  377         case MOD_UNLOAD:
  378         case MOD_SHUTDOWN:
  379                 pfs_vncache_unload();
  380                 pfs_fileno_unload();
  381                 break;
  382         default:
  383                 printf("pseudofs: unexpected event type %d\n", evt);
  384                 break;
  385         }
  386         return 0;
  387 }
  388 
  389 /*
  390  * Module declaration
  391  */
  392 static moduledata_t pseudofs_data = {
  393         "pseudofs",
  394         pfs_modevent,
  395         NULL
  396 };
  397 DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST);
  398 MODULE_VERSION(pseudofs, 1);

Cache object: 0c9ac75c1ea68f26762cbaef551b176b


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