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/tmpfs/tmpfs.h

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 /*      $NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $       */
    2 
    3 /*-
    4  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
    9  * 2005 program.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   30  * POSSIBILITY OF SUCH DAMAGE.
   31  *
   32  * $FreeBSD: releng/8.4/sys/fs/tmpfs/tmpfs.h 238553 2012-07-17 17:34:48Z delphij $
   33  */
   34 
   35 #ifndef _FS_TMPFS_TMPFS_H_
   36 #define _FS_TMPFS_TMPFS_H_
   37 
   38 /* ---------------------------------------------------------------------
   39  * KERNEL-SPECIFIC DEFINITIONS
   40  * --------------------------------------------------------------------- */
   41 #include <sys/dirent.h>
   42 #include <sys/mount.h>
   43 #include <sys/queue.h>
   44 #include <sys/vnode.h>
   45 #include <sys/file.h>
   46 #include <sys/lock.h>
   47 #include <sys/mutex.h>
   48 
   49 /* --------------------------------------------------------------------- */
   50 #include <sys/malloc.h>
   51 #include <sys/systm.h>
   52 #include <sys/vmmeter.h>
   53 #include <vm/swap_pager.h>
   54 
   55 MALLOC_DECLARE(M_TMPFSMNT);
   56 MALLOC_DECLARE(M_TMPFSNAME);
   57 
   58 /* --------------------------------------------------------------------- */
   59 
   60 /*
   61  * Internal representation of a tmpfs directory entry.
   62  */
   63 struct tmpfs_dirent {
   64         TAILQ_ENTRY(tmpfs_dirent)       td_entries;
   65 
   66         /* Length of the name stored in this directory entry.  This avoids
   67          * the need to recalculate it every time the name is used. */
   68         uint16_t                        td_namelen;
   69 
   70         /* The name of the entry, allocated from a string pool.  This
   71         * string is not required to be zero-terminated; therefore, the
   72         * td_namelen field must always be used when accessing its value. */
   73         char *                          td_name;
   74 
   75         /* Pointer to the node this entry refers to.  In case this field
   76          * is NULL, the node is a whiteout. */
   77         struct tmpfs_node *             td_node;
   78 };
   79 
   80 /* A directory in tmpfs holds a sorted list of directory entries, which in
   81  * turn point to other files (which can be directories themselves).
   82  *
   83  * In tmpfs, this list is managed by a tail queue, whose head is defined by
   84  * the struct tmpfs_dir type.
   85  *
   86  * It is imporant to notice that directories do not have entries for . and
   87  * .. as other file systems do.  These can be generated when requested
   88  * based on information available by other means, such as the pointer to
   89  * the node itself in the former case or the pointer to the parent directory
   90  * in the latter case.  This is done to simplify tmpfs's code and, more
   91  * importantly, to remove redundancy. */
   92 TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
   93 
   94 /* Each entry in a directory has a cookie that identifies it.  Cookies
   95  * supersede offsets within directories because, given how tmpfs stores
   96  * directories in memory, there is no such thing as an offset.  (Emulating
   97  * a real offset could be very difficult.)
   98  * 
   99  * The '.', '..' and the end of directory markers have fixed cookies which
  100  * cannot collide with the cookies generated by other entries.  The cookies
  101  * fot the other entries are generated based on the memory address on which
  102  * stores their information is stored.
  103  *
  104  * Ideally, using the entry's memory pointer as the cookie would be enough
  105  * to represent it and it wouldn't cause collisions in any system.
  106  * Unfortunately, this results in "offsets" with very large values which
  107  * later raise problems in the Linux compatibility layer (and maybe in other
  108  * places) as described in PR kern/32034.  Hence we need to workaround this
  109  * with a rather ugly hack.
  110  *
  111  * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t
  112  * set to 'long', which is a 32-bit *signed* long integer.  Regardless of
  113  * the macro value, GLIBC (2.3 at least) always uses the getdents64
  114  * system call (when calling readdir) which internally returns off64_t
  115  * offsets.  In order to make 32-bit binaries work, *GLIBC* converts the
  116  * 64-bit values returned by the kernel to 32-bit ones and aborts with
  117  * EOVERFLOW if the conversion results in values that won't fit in 32-bit
  118  * integers (which it assumes is because the directory is extremely large).
  119  * This wouldn't cause problems if we were dealing with unsigned integers,
  120  * but as we have signed integers, this check fails due to sign expansion.
  121  *
  122  * For example, consider that the kernel returns the 0xc1234567 cookie to
  123  * userspace in a off64_t integer.  Later on, GLIBC casts this value to
  124  * off_t (remember, signed) with code similar to:
  125  *     system call returns the offset in kernel_value;
  126  *     off_t casted_value = kernel_value;
  127  *     if (sizeof(off_t) != sizeof(off64_t) &&
  128  *         kernel_value != casted_value)
  129  *             error!
  130  * In this case, casted_value still has 0xc1234567, but when it is compared
  131  * for equality against kernel_value, it is promoted to a 64-bit integer and
  132  * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567.
  133  * Then, GLIBC assumes this is because the directory is very large.
  134  *
  135  * Given that all the above happens in user-space, we have no control over
  136  * it; therefore we must workaround the issue here.  We do this by
  137  * truncating the pointer value to a 32-bit integer and hope that there
  138  * won't be collisions.  In fact, this will not cause any problems in
  139  * 32-bit platforms but some might arise in 64-bit machines (I'm not sure
  140  * if they can happen at all in practice).
  141  *
  142  * XXX A nicer solution shall be attempted. */
  143 #ifdef _KERNEL
  144 #define TMPFS_DIRCOOKIE_DOT     0
  145 #define TMPFS_DIRCOOKIE_DOTDOT  1
  146 #define TMPFS_DIRCOOKIE_EOF     2
  147 static __inline
  148 off_t
  149 tmpfs_dircookie(struct tmpfs_dirent *de)
  150 {
  151         off_t cookie;
  152 
  153         cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF;
  154         MPASS(cookie != TMPFS_DIRCOOKIE_DOT);
  155         MPASS(cookie != TMPFS_DIRCOOKIE_DOTDOT);
  156         MPASS(cookie != TMPFS_DIRCOOKIE_EOF);
  157 
  158         return cookie;
  159 }
  160 #endif
  161 
  162 /* --------------------------------------------------------------------- */
  163 
  164 /*
  165  * Internal representation of a tmpfs file system node.
  166  *
  167  * This structure is splitted in two parts: one holds attributes common
  168  * to all file types and the other holds data that is only applicable to
  169  * a particular type.  The code must be careful to only access those
  170  * attributes that are actually allowed by the node's type.
  171  *
  172  *
  173  * Below is the key of locks used to protected the fields in the following
  174  * structures.
  175  *
  176  */
  177 struct tmpfs_node {
  178         /* Doubly-linked list entry which links all existing nodes for a
  179          * single file system.  This is provided to ease the removal of
  180          * all nodes during the unmount operation. */
  181         LIST_ENTRY(tmpfs_node)  tn_entries;
  182 
  183         /* The node's type.  Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
  184          * 'VLNK', 'VREG' and 'VSOCK' is allowed.  The usage of vnode
  185          * types instead of a custom enumeration is to make things simpler
  186          * and faster, as we do not need to convert between two types. */
  187         enum vtype              tn_type;
  188 
  189         /* Node identifier. */
  190         ino_t                   tn_id;
  191 
  192         /* Node's internal status.  This is used by several file system
  193          * operations to do modifications to the node in a delayed
  194          * fashion. */
  195         int                     tn_status;
  196 #define TMPFS_NODE_ACCESSED     (1 << 1)
  197 #define TMPFS_NODE_MODIFIED     (1 << 2)
  198 #define TMPFS_NODE_CHANGED      (1 << 3)
  199 
  200         /* The node size.  It does not necessarily match the real amount
  201          * of memory consumed by it. */
  202         off_t                   tn_size;
  203 
  204         /* Generic node attributes. */
  205         uid_t                   tn_uid;
  206         gid_t                   tn_gid;
  207         mode_t                  tn_mode;
  208         int                     tn_flags;
  209         nlink_t                 tn_links;
  210         struct timespec         tn_atime;
  211         struct timespec         tn_mtime;
  212         struct timespec         tn_ctime;
  213         struct timespec         tn_birthtime;
  214         unsigned long           tn_gen;
  215 
  216         /* As there is a single vnode for each active file within the
  217          * system, care has to be taken to avoid allocating more than one
  218          * vnode per file.  In order to do this, a bidirectional association
  219          * is kept between vnodes and nodes.
  220          *
  221          * Whenever a vnode is allocated, its v_data field is updated to
  222          * point to the node it references.  At the same time, the node's
  223          * tn_vnode field is modified to point to the new vnode representing
  224          * it.  Further attempts to allocate a vnode for this same node will
  225          * result in returning a new reference to the value stored in
  226          * tn_vnode.
  227          *
  228          * May be NULL when the node is unused (that is, no vnode has been
  229          * allocated for it or it has been reclaimed). */
  230         struct vnode *          tn_vnode;
  231 
  232         /* interlock to protect tn_vpstate */
  233         struct mtx      tn_interlock;
  234 
  235         /* Identify if current node has vnode assiocate with
  236          * or allocating vnode.
  237          */
  238         int             tn_vpstate;
  239 
  240         /* misc data field for different tn_type node */
  241         union {
  242                 /* Valid when tn_type == VBLK || tn_type == VCHR. */
  243                 dev_t                   tn_rdev;
  244 
  245                 /* Valid when tn_type == VDIR. */
  246                 struct tn_dir{
  247                         /* Pointer to the parent directory.  The root
  248                          * directory has a pointer to itself in this field;
  249                          * this property identifies the root node. */
  250                         struct tmpfs_node *     tn_parent;
  251 
  252                         /* Head of a tail-queue that links the contents of
  253                          * the directory together.  See above for a
  254                          * description of its contents. */
  255                         struct tmpfs_dir        tn_dirhead;
  256 
  257                         /* Number and pointer of the first directory entry
  258                          * returned by the readdir operation if it were
  259                          * called again to continue reading data from the
  260                          * same directory as before.  This is used to speed
  261                          * up reads of long directories, assuming that no
  262                          * more than one read is in progress at a given time.
  263                          * Otherwise, these values are discarded and a linear
  264                          * scan is performed from the beginning up to the
  265                          * point where readdir starts returning values. */
  266                         off_t                   tn_readdir_lastn;
  267                         struct tmpfs_dirent *   tn_readdir_lastp;
  268                 }tn_dir;
  269 
  270                 /* Valid when tn_type == VLNK. */
  271                 /* The link's target, allocated from a string pool. */
  272                 char *                  tn_link;
  273 
  274                 /* Valid when tn_type == VREG. */
  275                 struct tn_reg {
  276                         /* The contents of regular files stored in a tmpfs
  277                          * file system are represented by a single anonymous
  278                          * memory object (aobj, for short).  The aobj provides
  279                          * direct access to any position within the file,
  280                          * because its contents are always mapped in a
  281                          * contiguous region of virtual memory.  It is a task
  282                          * of the memory management subsystem (see uvm(9)) to
  283                          * issue the required page ins or page outs whenever
  284                          * a position within the file is accessed. */
  285                         vm_object_t             tn_aobj;
  286                         size_t                  tn_aobj_pages;
  287 
  288                 }tn_reg;
  289 
  290                 /* Valid when tn_type = VFIFO */
  291                 struct tn_fifo {
  292                         fo_rdwr_t               *tn_fo_read;
  293                         fo_rdwr_t               *tn_fo_write;
  294                 }tn_fifo;
  295         }tn_spec;
  296 };
  297 LIST_HEAD(tmpfs_node_list, tmpfs_node);
  298 
  299 #define tn_rdev tn_spec.tn_rdev
  300 #define tn_dir tn_spec.tn_dir
  301 #define tn_link tn_spec.tn_link
  302 #define tn_reg tn_spec.tn_reg
  303 #define tn_fifo tn_spec.tn_fifo
  304 
  305 #define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock)
  306 #define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock)
  307 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
  308 
  309 #ifdef INVARIANTS
  310 #define TMPFS_ASSERT_LOCKED(node) do {                                  \
  311                 MPASS(node != NULL);                                    \
  312                 MPASS(node->tn_vnode != NULL);                          \
  313                 if (!VOP_ISLOCKED(node->tn_vnode) &&                    \
  314                     !mtx_owned(TMPFS_NODE_MTX(node)))                   \
  315                         panic("tmpfs: node is not locked: %p", node);   \
  316         } while (0)
  317 #define TMPFS_ASSERT_ELOCKED(node) do {                                 \
  318                 MPASS((node) != NULL);                                  \
  319                 MPASS((node)->tn_vnode != NULL);                        \
  320                 mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED);             \
  321                 ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs");           \
  322         } while (0)
  323 #else
  324 #define TMPFS_ASSERT_LOCKED(node) (void)0
  325 #define TMPFS_ASSERT_ELOCKED(node) (void)0
  326 #endif
  327 
  328 #define TMPFS_VNODE_ALLOCATING  1
  329 #define TMPFS_VNODE_WANT        2
  330 #define TMPFS_VNODE_DOOMED      4
  331 /* --------------------------------------------------------------------- */
  332 
  333 /*
  334  * Internal representation of a tmpfs mount point.
  335  */
  336 struct tmpfs_mount {
  337         /* Maximum number of memory pages available for use by the file
  338          * system, set during mount time.  This variable must never be
  339          * used directly as it may be bigger than the current amount of
  340          * free memory; in the extreme case, it will hold the SIZE_MAX
  341          * value.  Instead, use the TMPFS_PAGES_MAX macro. */
  342         size_t                  tm_pages_max;
  343 
  344         /* Number of pages in use by the file system.  Cannot be bigger
  345          * than the value returned by TMPFS_PAGES_MAX in any case. */
  346         size_t                  tm_pages_used;
  347 
  348         /* Pointer to the node representing the root directory of this
  349          * file system. */
  350         struct tmpfs_node *     tm_root;
  351 
  352         /* Maximum number of possible nodes for this file system; set
  353          * during mount time.  We need a hard limit on the maximum number
  354          * of nodes to avoid allocating too much of them; their objects
  355          * cannot be released until the file system is unmounted.
  356          * Otherwise, we could easily run out of memory by creating lots
  357          * of empty files and then simply removing them. */
  358         ino_t                   tm_nodes_max;
  359 
  360         /* unrhdr used to allocate inode numbers */
  361         struct unrhdr *         tm_ino_unr;
  362 
  363         /* Number of nodes currently that are in use. */
  364         ino_t                   tm_nodes_inuse;
  365 
  366         /* maximum representable file size */
  367         u_int64_t               tm_maxfilesize;
  368 
  369         /* Nodes are organized in two different lists.  The used list
  370          * contains all nodes that are currently used by the file system;
  371          * i.e., they refer to existing files.  The available list contains
  372          * all nodes that are currently available for use by new files.
  373          * Nodes must be kept in this list (instead of deleting them)
  374          * because we need to keep track of their generation number (tn_gen
  375          * field).
  376          *
  377          * Note that nodes are lazily allocated: if the available list is
  378          * empty and we have enough space to create more nodes, they will be
  379          * created and inserted in the used list.  Once these are released,
  380          * they will go into the available list, remaining alive until the
  381          * file system is unmounted. */
  382         struct tmpfs_node_list  tm_nodes_used;
  383 
  384         /* All node lock to protect the node list and tmp_pages_used */
  385         struct mtx allnode_lock;
  386 
  387         /* Pools used to store file system meta data.  These are not shared
  388          * across several instances of tmpfs for the reasons described in
  389          * tmpfs_pool.c. */
  390         uma_zone_t              tm_dirent_pool;
  391         uma_zone_t              tm_node_pool;
  392 };
  393 #define TMPFS_LOCK(tm) mtx_lock(&(tm)->allnode_lock)
  394 #define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->allnode_lock)
  395 
  396 /* --------------------------------------------------------------------- */
  397 
  398 /*
  399  * This structure maps a file identifier to a tmpfs node.  Used by the
  400  * NFS code.
  401  */
  402 struct tmpfs_fid {
  403         uint16_t                tf_len;
  404         uint16_t                tf_pad;
  405         ino_t                   tf_id;
  406         unsigned long           tf_gen;
  407 };
  408 
  409 /* --------------------------------------------------------------------- */
  410 
  411 #ifdef _KERNEL
  412 /*
  413  * Prototypes for tmpfs_subr.c.
  414  */
  415 
  416 int     tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
  417             uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
  418             char *, dev_t, struct tmpfs_node **);
  419 void    tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
  420 int     tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
  421             const char *, uint16_t, struct tmpfs_dirent **);
  422 void    tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
  423             boolean_t);
  424 int     tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
  425             struct vnode **);
  426 void    tmpfs_free_vp(struct vnode *);
  427 int     tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
  428             struct componentname *, char *);
  429 void    tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
  430 void    tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
  431 struct tmpfs_dirent *   tmpfs_dir_lookup(struct tmpfs_node *node,
  432                             struct tmpfs_node *f,
  433                             struct componentname *cnp);
  434 int     tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
  435 int     tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
  436 struct tmpfs_dirent *   tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
  437 int     tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
  438 int     tmpfs_dir_whiteout_add(struct vnode *, struct componentname *);
  439 void    tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *);
  440 int     tmpfs_reg_resize(struct vnode *, off_t);
  441 int     tmpfs_chflags(struct vnode *, int, struct ucred *, struct thread *);
  442 int     tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *);
  443 int     tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
  444             struct thread *);
  445 int     tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *);
  446 int     tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
  447             struct timespec *, int, struct ucred *, struct thread *);
  448 void    tmpfs_itimes(struct vnode *, const struct timespec *,
  449             const struct timespec *);
  450 
  451 void    tmpfs_update(struct vnode *);
  452 int     tmpfs_truncate(struct vnode *, off_t);
  453 
  454 /* --------------------------------------------------------------------- */
  455 
  456 /*
  457  * Convenience macros to simplify some logical expressions.
  458  */
  459 #define IMPLIES(a, b) (!(a) || (b))
  460 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
  461 
  462 /* --------------------------------------------------------------------- */
  463 
  464 /*
  465  * Checks that the directory entry pointed by 'de' matches the name 'name'
  466  * with a length of 'len'.
  467  */
  468 #define TMPFS_DIRENT_MATCHES(de, name, len) \
  469     (de->td_namelen == (uint16_t)len && \
  470     bcmp((de)->td_name, (name), (de)->td_namelen) == 0)
  471 
  472 /* --------------------------------------------------------------------- */
  473 
  474 /*
  475  * Ensures that the node pointed by 'node' is a directory and that its
  476  * contents are consistent with respect to directories.
  477  */
  478 #define TMPFS_VALIDATE_DIR(node) \
  479     MPASS((node)->tn_type == VDIR); \
  480     MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
  481     MPASS((node)->tn_dir.tn_readdir_lastp == NULL || \
  482         tmpfs_dircookie((node)->tn_dir.tn_readdir_lastp) == (node)->tn_dir.tn_readdir_lastn);
  483 
  484 /* --------------------------------------------------------------------- */
  485 
  486 /*
  487  * Memory management stuff.
  488  */
  489 
  490 /* Amount of memory pages to reserve for the system (e.g., to not use by
  491  * tmpfs).
  492  * XXX: Should this be tunable through sysctl, for instance? */
  493 #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
  494 
  495 /*
  496  * Returns information about the number of available memory pages,
  497  * including physical and virtual ones.
  498  *
  499  * Remember to remove TMPFS_PAGES_RESERVED from the returned value to avoid
  500  * excessive memory usage.
  501  *
  502  */
  503 static __inline size_t
  504 tmpfs_mem_info(void)
  505 {
  506 
  507         return (swap_pager_avail + cnt.v_free_count + cnt.v_cache_count);
  508 }
  509 
  510 /* Returns the maximum size allowed for a tmpfs file system.  This macro
  511  * must be used instead of directly retrieving the value from tm_pages_max.
  512  * The reason is that the size of a tmpfs file system is dynamic: it lets
  513  * the user store files as long as there is enough free memory (including
  514  * physical memory and swap space).  Therefore, the amount of memory to be
  515  * used is either the limit imposed by the user during mount time or the
  516  * amount of available memory, whichever is lower.  To avoid consuming all
  517  * the memory for a given mount point, the system will always reserve a
  518  * minimum of TMPFS_PAGES_RESERVED pages, which is also taken into account
  519  * by this macro (see above). */
  520 static __inline size_t
  521 TMPFS_PAGES_MAX(struct tmpfs_mount *tmp)
  522 {
  523         size_t freepages;
  524 
  525         freepages = tmpfs_mem_info();
  526         freepages -= freepages < TMPFS_PAGES_RESERVED ?
  527             freepages : TMPFS_PAGES_RESERVED;
  528 
  529         return MIN(tmp->tm_pages_max, freepages + tmp->tm_pages_used);
  530 }
  531 
  532 /* Returns the available space for the given file system. */
  533 #define TMPFS_META_PAGES(tmp) (howmany((tmp)->tm_nodes_inuse * (sizeof(struct tmpfs_node) \
  534                                 + sizeof(struct tmpfs_dirent)), PAGE_SIZE))
  535 #define TMPFS_FILE_PAGES(tmp) ((tmp)->tm_pages_used)
  536 
  537 #define TMPFS_PAGES_AVAIL(tmp) (TMPFS_PAGES_MAX(tmp) > \
  538                         TMPFS_META_PAGES(tmp)+TMPFS_FILE_PAGES(tmp)? \
  539                         TMPFS_PAGES_MAX(tmp) - TMPFS_META_PAGES(tmp) \
  540                         - TMPFS_FILE_PAGES(tmp):0)
  541 
  542 #endif
  543 
  544 /* --------------------------------------------------------------------- */
  545 
  546 /*
  547  * Macros/functions to convert from generic data structures to tmpfs
  548  * specific ones.
  549  */
  550 
  551 static inline
  552 struct tmpfs_mount *
  553 VFS_TO_TMPFS(struct mount *mp)
  554 {
  555         struct tmpfs_mount *tmp;
  556 
  557         MPASS((mp) != NULL && (mp)->mnt_data != NULL);
  558         tmp = (struct tmpfs_mount *)(mp)->mnt_data;
  559         return tmp;
  560 }
  561 
  562 static inline
  563 struct tmpfs_node *
  564 VP_TO_TMPFS_NODE(struct vnode *vp)
  565 {
  566         struct tmpfs_node *node;
  567 
  568         MPASS((vp) != NULL && (vp)->v_data != NULL);
  569         node = (struct tmpfs_node *)vp->v_data;
  570         return node;
  571 }
  572 
  573 static inline
  574 struct tmpfs_node *
  575 VP_TO_TMPFS_DIR(struct vnode *vp)
  576 {
  577         struct tmpfs_node *node;
  578 
  579         node = VP_TO_TMPFS_NODE(vp);
  580         TMPFS_VALIDATE_DIR(node);
  581         return node;
  582 }
  583 
  584 #endif /* _FS_TMPFS_TMPFS_H_ */

Cache object: 8eb7e7ec3f6f48c57c9dc21ac3e19c92


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