Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs.h revision 1.9.2.2
      1  1.9.2.2  skrll /*	$NetBSD: tmpfs.h,v 1.9.2.2 2005/11/10 14:09:44 skrll Exp $	*/
      2  1.9.2.2  skrll 
      3  1.9.2.2  skrll /*
      4  1.9.2.2  skrll  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  1.9.2.2  skrll  * All rights reserved.
      6  1.9.2.2  skrll  *
      7  1.9.2.2  skrll  * This code is derived from software contributed to The NetBSD Foundation
      8  1.9.2.2  skrll  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
      9  1.9.2.2  skrll  * 2005 program.
     10  1.9.2.2  skrll  *
     11  1.9.2.2  skrll  * Redistribution and use in source and binary forms, with or without
     12  1.9.2.2  skrll  * modification, are permitted provided that the following conditions
     13  1.9.2.2  skrll  * are met:
     14  1.9.2.2  skrll  * 1. Redistributions of source code must retain the above copyright
     15  1.9.2.2  skrll  *    notice, this list of conditions and the following disclaimer.
     16  1.9.2.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.9.2.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     18  1.9.2.2  skrll  *    documentation and/or other materials provided with the distribution.
     19  1.9.2.2  skrll  * 3. All advertising materials mentioning features or use of this software
     20  1.9.2.2  skrll  *    must display the following acknowledgement:
     21  1.9.2.2  skrll  *        This product includes software developed by the NetBSD
     22  1.9.2.2  skrll  *        Foundation, Inc. and its contributors.
     23  1.9.2.2  skrll  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.9.2.2  skrll  *    contributors may be used to endorse or promote products derived
     25  1.9.2.2  skrll  *    from this software without specific prior written permission.
     26  1.9.2.2  skrll  *
     27  1.9.2.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.9.2.2  skrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.9.2.2  skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.9.2.2  skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.9.2.2  skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.9.2.2  skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.9.2.2  skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.9.2.2  skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.9.2.2  skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.9.2.2  skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.9.2.2  skrll  * POSSIBILITY OF SUCH DAMAGE.
     38  1.9.2.2  skrll  */
     39  1.9.2.2  skrll 
     40  1.9.2.2  skrll #if !defined(_TMPFS_H_)
     41  1.9.2.2  skrll #  define _TMPFS_H_
     42  1.9.2.2  skrll #else
     43  1.9.2.2  skrll #  error "tmpfs.h cannot be included multiple times."
     44  1.9.2.2  skrll #endif
     45  1.9.2.2  skrll 
     46  1.9.2.2  skrll /* ---------------------------------------------------------------------
     47  1.9.2.2  skrll  * KERNEL-SPECIFIC DEFINITIONS
     48  1.9.2.2  skrll  * --------------------------------------------------------------------- */
     49  1.9.2.2  skrll 
     50  1.9.2.2  skrll #if defined(_KERNEL)
     51  1.9.2.2  skrll 
     52  1.9.2.2  skrll #include <sys/dirent.h>
     53  1.9.2.2  skrll #include <sys/mount.h>
     54  1.9.2.2  skrll #include <sys/queue.h>
     55  1.9.2.2  skrll #include <sys/vnode.h>
     56  1.9.2.2  skrll 
     57  1.9.2.2  skrll #include <fs/tmpfs/tmpfs_pool.h>
     58  1.9.2.2  skrll 
     59  1.9.2.2  skrll /* --------------------------------------------------------------------- */
     60  1.9.2.2  skrll 
     61  1.9.2.2  skrll /*
     62  1.9.2.2  skrll  * Internal representation of a tmpfs directory entry.
     63  1.9.2.2  skrll  */
     64  1.9.2.2  skrll struct tmpfs_dirent {
     65  1.9.2.2  skrll 	TAILQ_ENTRY(tmpfs_dirent)	td_entries;
     66  1.9.2.2  skrll 
     67  1.9.2.2  skrll 	/* Length of the name stored in this directory entry.  This avoids
     68  1.9.2.2  skrll 	 * the need to recalculate it every time the name is used. */
     69  1.9.2.2  skrll 	uint16_t			td_namelen;
     70  1.9.2.2  skrll 
     71  1.9.2.2  skrll 	/* The name of the entry, allocated from a string pool.  This
     72  1.9.2.2  skrll 	* string is not required to be zero-terminated; therefore, the
     73  1.9.2.2  skrll 	* td_namelen field must always be used when accessing its value. */
     74  1.9.2.2  skrll 	char *				td_name;
     75  1.9.2.2  skrll 
     76  1.9.2.2  skrll 	/* Pointer to the node this entry refers to. */
     77  1.9.2.2  skrll 	struct tmpfs_node *		td_node;
     78  1.9.2.2  skrll };
     79  1.9.2.2  skrll 
     80  1.9.2.2  skrll /* A directory in tmpfs holds a sorted list of directory entries, which in
     81  1.9.2.2  skrll  * turn point to other files (which can be directories themselves).
     82  1.9.2.2  skrll  *
     83  1.9.2.2  skrll  * In tmpfs, this list is managed by a tail queue, whose head is defined by
     84  1.9.2.2  skrll  * the struct tmpfs_dir type.
     85  1.9.2.2  skrll  *
     86  1.9.2.2  skrll  * It is imporant to notice that directories do not have entries for . and
     87  1.9.2.2  skrll  * .. as other file systems do.  These can be generated when requested
     88  1.9.2.2  skrll  * based on information available by other means, such as the pointer to
     89  1.9.2.2  skrll  * the node itself in the former case or the pointer to the parent directory
     90  1.9.2.2  skrll  * in the latter case.  This is done to simplify tmpfs's code and, more
     91  1.9.2.2  skrll  * importantly, to remove redundancy. */
     92  1.9.2.2  skrll TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
     93  1.9.2.2  skrll 
     94  1.9.2.2  skrll #define	TMPFS_DIRCOOKIE(dirent)	((off_t)(uintptr_t)(dirent))
     95  1.9.2.2  skrll #define	TMPFS_DIRCOOKIE_DOT	0
     96  1.9.2.2  skrll #define	TMPFS_DIRCOOKIE_DOTDOT	1
     97  1.9.2.2  skrll #define	TMPFS_DIRCOOKIE_EOF	2
     98  1.9.2.2  skrll 
     99  1.9.2.2  skrll /* --------------------------------------------------------------------- */
    100  1.9.2.2  skrll 
    101  1.9.2.2  skrll /*
    102  1.9.2.2  skrll  * Internal representation of a tmpfs file system node.
    103  1.9.2.2  skrll  *
    104  1.9.2.2  skrll  * This structure is splitted in two parts: one holds attributes common
    105  1.9.2.2  skrll  * to all file types and the other holds data that is only applicable to
    106  1.9.2.2  skrll  * a particular type.  The code must be careful to only access those
    107  1.9.2.2  skrll  * attributes that are actually allowed by the node's type.
    108  1.9.2.2  skrll  */
    109  1.9.2.2  skrll struct tmpfs_node {
    110  1.9.2.2  skrll 	/* Doubly-linked list entry which links all existing nodes for a
    111  1.9.2.2  skrll 	 * single file system.  This is provided to ease the removal of
    112  1.9.2.2  skrll 	 * all nodes during the unmount operation. */
    113  1.9.2.2  skrll 	LIST_ENTRY(tmpfs_node)	tn_entries;
    114  1.9.2.2  skrll 
    115  1.9.2.2  skrll 	/* The node's type.  Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
    116  1.9.2.2  skrll 	 * 'VLNK', 'VREG' and 'VSOCK' is allowed.  The usage of vnode
    117  1.9.2.2  skrll 	 * types instead of a custom enumeration is to make things simpler
    118  1.9.2.2  skrll 	 * and faster, as we do not need to convert between two types. */
    119  1.9.2.2  skrll 	enum vtype		tn_type;
    120  1.9.2.2  skrll 
    121  1.9.2.2  skrll 	/* Node identifier. */
    122  1.9.2.2  skrll 	ino_t			tn_id;
    123  1.9.2.2  skrll 
    124  1.9.2.2  skrll 	/* Node's internal status.  This is used by several file system
    125  1.9.2.2  skrll 	 * operations to do modifications to the node in a delayed
    126  1.9.2.2  skrll 	 * fashion. */
    127  1.9.2.2  skrll 	int			tn_status;
    128  1.9.2.2  skrll #define	TMPFS_NODE_ACCESSED	(1 << 1)
    129  1.9.2.2  skrll #define	TMPFS_NODE_MODIFIED	(1 << 2)
    130  1.9.2.2  skrll #define	TMPFS_NODE_CHANGED	(1 << 3)
    131  1.9.2.2  skrll 
    132  1.9.2.2  skrll 	/* The node size.  It does not necessarily match the real amount
    133  1.9.2.2  skrll 	 * of memory consumed by it. */
    134  1.9.2.2  skrll 	off_t			tn_size;
    135  1.9.2.2  skrll 
    136  1.9.2.2  skrll 	/* Generic node attributes. */
    137  1.9.2.2  skrll 	uid_t			tn_uid;
    138  1.9.2.2  skrll 	gid_t			tn_gid;
    139  1.9.2.2  skrll 	mode_t			tn_mode;
    140  1.9.2.2  skrll 	int			tn_flags;
    141  1.9.2.2  skrll 	nlink_t			tn_links;
    142  1.9.2.2  skrll 	struct timespec		tn_atime;
    143  1.9.2.2  skrll 	struct timespec		tn_mtime;
    144  1.9.2.2  skrll 	struct timespec		tn_ctime;
    145  1.9.2.2  skrll 	struct timespec		tn_birthtime;
    146  1.9.2.2  skrll 	unsigned long		tn_gen;
    147  1.9.2.2  skrll 
    148  1.9.2.2  skrll 	/* Head of byte-level lock list (used by tmpfs_advlock). */
    149  1.9.2.2  skrll 	struct lockf *		tn_lockf;
    150  1.9.2.2  skrll 
    151  1.9.2.2  skrll 	/* As there is a single vnode for each active file within the
    152  1.9.2.2  skrll 	 * system, care has to be taken to avoid allocating more than one
    153  1.9.2.2  skrll 	 * vnode per file.  In order to do this, a bidirectional association
    154  1.9.2.2  skrll 	 * is kept between vnodes and nodes.
    155  1.9.2.2  skrll 	 *
    156  1.9.2.2  skrll 	 * Whenever a vnode is allocated, its v_data field is updated to
    157  1.9.2.2  skrll 	 * point to the node it references.  At the same time, the node's
    158  1.9.2.2  skrll 	 * tn_vnode field is modified to point to the new vnode representing
    159  1.9.2.2  skrll 	 * it.  Further attempts to allocate a vnode for this same node will
    160  1.9.2.2  skrll 	 * result in returning a new reference to the value stored in
    161  1.9.2.2  skrll 	 * tn_vnode.
    162  1.9.2.2  skrll 	 *
    163  1.9.2.2  skrll 	 * May be NULL when the node is unused (that is, no vnode has been
    164  1.9.2.2  skrll 	 * allocated for it or it has been reclaimed). */
    165  1.9.2.2  skrll 	struct vnode *		tn_vnode;
    166  1.9.2.2  skrll 
    167  1.9.2.2  skrll 	/* Pointer to the node returned by tmpfs_lookup() after doing a
    168  1.9.2.2  skrll 	 * delete or a rename lookup; its value is only valid in these two
    169  1.9.2.2  skrll 	 * situations.  In case we were looking up . or .., it holds a null
    170  1.9.2.2  skrll 	 * pointer. */
    171  1.9.2.2  skrll 	struct tmpfs_dirent *	tn_lookup_dirent;
    172  1.9.2.2  skrll 
    173  1.9.2.2  skrll 	union {
    174  1.9.2.2  skrll 		/* Valid when tn_type == VBLK || tn_type == VCHR. */
    175  1.9.2.2  skrll 		struct {
    176  1.9.2.2  skrll 			dev_t			tn_rdev;
    177  1.9.2.2  skrll 		};
    178  1.9.2.2  skrll 
    179  1.9.2.2  skrll 		/* Valid when tn_type == VDIR. */
    180  1.9.2.2  skrll 		struct {
    181  1.9.2.2  skrll 			/* Pointer to the parent directory.  The root
    182  1.9.2.2  skrll 			 * directory has a pointer to itself in this field;
    183  1.9.2.2  skrll 			 * this property identifies the root node. */
    184  1.9.2.2  skrll 			struct tmpfs_node *	tn_parent;
    185  1.9.2.2  skrll 
    186  1.9.2.2  skrll 			/* Head of a tail-queue that links the contents of
    187  1.9.2.2  skrll 			 * the directory together.  See above for a
    188  1.9.2.2  skrll 			 * description of its contents. */
    189  1.9.2.2  skrll 			struct tmpfs_dir	tn_dir;
    190  1.9.2.2  skrll 
    191  1.9.2.2  skrll 			/* Number and pointer of the first directory entry
    192  1.9.2.2  skrll 			 * returned by the readdir operation if it were
    193  1.9.2.2  skrll 			 * called again to continue reading data from the
    194  1.9.2.2  skrll 			 * same directory as before.  This is used to speed
    195  1.9.2.2  skrll 			 * up reads of long directories, assuming that no
    196  1.9.2.2  skrll 			 * more than one read is in progress at a given time.
    197  1.9.2.2  skrll 			 * Otherwise, these values are discarded and a linear
    198  1.9.2.2  skrll 			 * scan is performed from the beginning up to the
    199  1.9.2.2  skrll 			 * point where readdir starts returning values. */
    200  1.9.2.2  skrll 			off_t			tn_readdir_lastn;
    201  1.9.2.2  skrll 			struct tmpfs_dirent *	tn_readdir_lastp;
    202  1.9.2.2  skrll 		};
    203  1.9.2.2  skrll 
    204  1.9.2.2  skrll 		/* Valid when tn_type == VLNK. */
    205  1.9.2.2  skrll 		struct {
    206  1.9.2.2  skrll 			/* The link's target, allocated from a string pool. */
    207  1.9.2.2  skrll 			char *			tn_link;
    208  1.9.2.2  skrll 		};
    209  1.9.2.2  skrll 
    210  1.9.2.2  skrll 		/* Valid when tn_type == VREG. */
    211  1.9.2.2  skrll 		struct {
    212  1.9.2.2  skrll 			/* The contents of regular files stored in a tmpfs
    213  1.9.2.2  skrll 			 * file system are represented by a single anonymous
    214  1.9.2.2  skrll 			 * memory object (aobj, for short).  The aobj provides
    215  1.9.2.2  skrll 			 * direct access to any position within the file,
    216  1.9.2.2  skrll 			 * because its contents are always mapped in a
    217  1.9.2.2  skrll 			 * contiguous region of virtual memory.  It is a task
    218  1.9.2.2  skrll 			 * of the memory management subsystem (see uvm(9)) to
    219  1.9.2.2  skrll 			 * issue the required page ins or page outs whenever
    220  1.9.2.2  skrll 			 * a position within the file is accessed. */
    221  1.9.2.2  skrll 			struct uvm_object *	tn_aobj;
    222  1.9.2.2  skrll 			size_t			tn_aobj_pages;
    223  1.9.2.2  skrll 		};
    224  1.9.2.2  skrll 	};
    225  1.9.2.2  skrll };
    226  1.9.2.2  skrll LIST_HEAD(tmpfs_node_list, tmpfs_node);
    227  1.9.2.2  skrll 
    228  1.9.2.2  skrll /* --------------------------------------------------------------------- */
    229  1.9.2.2  skrll 
    230  1.9.2.2  skrll /*
    231  1.9.2.2  skrll  * Internal representation of a tmpfs mount point.
    232  1.9.2.2  skrll  */
    233  1.9.2.2  skrll struct tmpfs_mount {
    234  1.9.2.2  skrll 	/* Maximum number of memory pages available for use by the file
    235  1.9.2.2  skrll 	 * system, set during mount time.  This variable must never be
    236  1.9.2.2  skrll 	 * used directly as it may be bigger that the current amount of
    237  1.9.2.2  skrll 	 * free memory; in the extreme case, it will hold the SIZE_MAX
    238  1.9.2.2  skrll 	 * value.  Instead, use the TMPFS_PAGES_MAX macro. */
    239  1.9.2.2  skrll 	size_t			tm_pages_max;
    240  1.9.2.2  skrll 
    241  1.9.2.2  skrll 	/* Number of pages in use by the file system.  Cannot be bigger
    242  1.9.2.2  skrll 	 * than the value returned by TMPFS_PAGES_MAX in any case. */
    243  1.9.2.2  skrll 	size_t			tm_pages_used;
    244  1.9.2.2  skrll 
    245  1.9.2.2  skrll 	/* Pointer to the node representing the root directory of this
    246  1.9.2.2  skrll 	 * file system. */
    247  1.9.2.2  skrll 	struct tmpfs_node *	tm_root;
    248  1.9.2.2  skrll 
    249  1.9.2.2  skrll 	/* Maximum number of possible nodes for this file system; set
    250  1.9.2.2  skrll 	 * during mount time.  We need a hard limit on the maximum number
    251  1.9.2.2  skrll 	 * of nodes to avoid allocating too much of them; their objects
    252  1.9.2.2  skrll 	 * cannot be released until the file system is unmounted.
    253  1.9.2.2  skrll 	 * Otherwise, we could easily run out of memory by creating lots
    254  1.9.2.2  skrll 	 * of empty files and then simply removing them. */
    255  1.9.2.2  skrll 	ino_t			tm_nodes_max;
    256  1.9.2.2  skrll 
    257  1.9.2.2  skrll 	/* Number of nodes currently allocated.  This number only grows.
    258  1.9.2.2  skrll 	 * When it reaches tm_nodes_max, no more new nodes can be allocated.
    259  1.9.2.2  skrll 	 * Of course, the old, unused ones can be reused. */
    260  1.9.2.2  skrll 	ino_t			tm_nodes_last;
    261  1.9.2.2  skrll 
    262  1.9.2.2  skrll 	/* Nodes are organized in two different lists.  The used list
    263  1.9.2.2  skrll 	 * contains all nodes that are currently used by the file system;
    264  1.9.2.2  skrll 	 * i.e., they refer to existing files.  The available list contains
    265  1.9.2.2  skrll 	 * all nodes that are currently available for use by new files.
    266  1.9.2.2  skrll 	 * Nodes must be kept in this list (instead of deleting them)
    267  1.9.2.2  skrll 	 * because we need to keep track of their generation number (tn_gen
    268  1.9.2.2  skrll 	 * field).
    269  1.9.2.2  skrll 	 *
    270  1.9.2.2  skrll 	 * Note that nodes are lazily allocated: if the available list is
    271  1.9.2.2  skrll 	 * empty and we have enough space to create more nodes, they will be
    272  1.9.2.2  skrll 	 * created and inserted in the used list.  Once these are released,
    273  1.9.2.2  skrll 	 * they will go into the available list, remaining alive until the
    274  1.9.2.2  skrll 	 * file system is unmounted. */
    275  1.9.2.2  skrll 	struct tmpfs_node_list	tm_nodes_used;
    276  1.9.2.2  skrll 	struct tmpfs_node_list	tm_nodes_avail;
    277  1.9.2.2  skrll 
    278  1.9.2.2  skrll 	/* Pools used to store file system meta data.  These are not shared
    279  1.9.2.2  skrll 	 * across several instances of tmpfs for the reasons described in
    280  1.9.2.2  skrll 	 * tmpfs_pool.c. */
    281  1.9.2.2  skrll 	struct tmpfs_pool	tm_dirent_pool;
    282  1.9.2.2  skrll 	struct tmpfs_pool	tm_node_pool;
    283  1.9.2.2  skrll 	struct tmpfs_str_pool	tm_str_pool;
    284  1.9.2.2  skrll };
    285  1.9.2.2  skrll 
    286  1.9.2.2  skrll /* --------------------------------------------------------------------- */
    287  1.9.2.2  skrll 
    288  1.9.2.2  skrll /*
    289  1.9.2.2  skrll  * This structure maps a file identifier to a tmpfs node.  Used by the
    290  1.9.2.2  skrll  * NFS code.
    291  1.9.2.2  skrll  */
    292  1.9.2.2  skrll struct tmpfs_fid {
    293  1.9.2.2  skrll 	uint16_t		tf_len;
    294  1.9.2.2  skrll 	uint16_t		tf_pad;
    295  1.9.2.2  skrll 	ino_t			tf_id;
    296  1.9.2.2  skrll 	unsigned long		tf_gen;
    297  1.9.2.2  skrll };
    298  1.9.2.2  skrll 
    299  1.9.2.2  skrll /* --------------------------------------------------------------------- */
    300  1.9.2.2  skrll 
    301  1.9.2.2  skrll /*
    302  1.9.2.2  skrll  * Prototypes for tmpfs_subr.c.
    303  1.9.2.2  skrll  */
    304  1.9.2.2  skrll 
    305  1.9.2.2  skrll int	tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
    306  1.9.2.2  skrll 	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
    307  1.9.2.2  skrll 	    char *, dev_t, struct proc *, struct tmpfs_node **);
    308  1.9.2.2  skrll void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
    309  1.9.2.2  skrll int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
    310  1.9.2.2  skrll 	    const char *, uint16_t, struct tmpfs_dirent **);
    311  1.9.2.2  skrll void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
    312  1.9.2.2  skrll 	    boolean_t);
    313  1.9.2.2  skrll int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **);
    314  1.9.2.2  skrll void	tmpfs_free_vp(struct vnode *);
    315  1.9.2.2  skrll int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
    316  1.9.2.2  skrll 	    struct componentname *, char *);
    317  1.9.2.2  skrll void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
    318  1.9.2.2  skrll void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
    319  1.9.2.2  skrll struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
    320  1.9.2.2  skrll 			    struct componentname *cnp);
    321  1.9.2.2  skrll int	tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
    322  1.9.2.2  skrll int	tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
    323  1.9.2.2  skrll struct tmpfs_dirent *	tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
    324  1.9.2.2  skrll int	tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
    325  1.9.2.2  skrll int	tmpfs_reg_resize(struct vnode *, off_t);
    326  1.9.2.2  skrll size_t	tmpfs_mem_info(boolean_t);
    327  1.9.2.2  skrll int	tmpfs_chflags(struct vnode *, int, struct ucred *, struct proc *);
    328  1.9.2.2  skrll int	tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct proc *);
    329  1.9.2.2  skrll int	tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
    330  1.9.2.2  skrll 	    struct proc *);
    331  1.9.2.2  skrll int	tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct proc *);
    332  1.9.2.2  skrll int	tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
    333  1.9.2.2  skrll 	    int, struct ucred *, struct proc *);
    334  1.9.2.2  skrll void	tmpfs_itimes(struct vnode *, const struct timespec *,
    335  1.9.2.2  skrll 	    const struct timespec *);
    336  1.9.2.2  skrll 
    337  1.9.2.2  skrll void	tmpfs_update(struct vnode *, const struct timespec *,
    338  1.9.2.2  skrll 	    const struct timespec *, int);
    339  1.9.2.2  skrll int	tmpfs_truncate(struct vnode *, off_t);
    340  1.9.2.2  skrll 
    341  1.9.2.2  skrll /* --------------------------------------------------------------------- */
    342  1.9.2.2  skrll 
    343  1.9.2.2  skrll /*
    344  1.9.2.2  skrll  * Convenience macros to simplify some logical expressions.
    345  1.9.2.2  skrll  */
    346  1.9.2.2  skrll #define IMPLIES(a, b) (!(a) || (b))
    347  1.9.2.2  skrll #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
    348  1.9.2.2  skrll 
    349  1.9.2.2  skrll /* --------------------------------------------------------------------- */
    350  1.9.2.2  skrll 
    351  1.9.2.2  skrll /*
    352  1.9.2.2  skrll  * Checks that the directory entry pointed by 'de' matches the name 'name'
    353  1.9.2.2  skrll  * with a length of 'len'.
    354  1.9.2.2  skrll  */
    355  1.9.2.2  skrll #define TMPFS_DIRENT_MATCHES(de, name, len) \
    356  1.9.2.2  skrll     (de->td_namelen == (uint16_t)len && \
    357  1.9.2.2  skrll     memcmp((de)->td_name, (name), (de)->td_namelen) == 0)
    358  1.9.2.2  skrll 
    359  1.9.2.2  skrll /* --------------------------------------------------------------------- */
    360  1.9.2.2  skrll 
    361  1.9.2.2  skrll /*
    362  1.9.2.2  skrll  * Ensures that the node pointed by 'node' is a directory and that its
    363  1.9.2.2  skrll  * contents are consistent with respect to directories.
    364  1.9.2.2  skrll  */
    365  1.9.2.2  skrll #define TMPFS_VALIDATE_DIR(node) \
    366  1.9.2.2  skrll     KASSERT((node)->tn_type == VDIR); \
    367  1.9.2.2  skrll     KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
    368  1.9.2.2  skrll     KASSERT((node)->tn_readdir_lastp == NULL || \
    369  1.9.2.2  skrll 	TMPFS_DIRCOOKIE((node)->tn_readdir_lastp) == (node)->tn_readdir_lastn);
    370  1.9.2.2  skrll 
    371  1.9.2.2  skrll /* --------------------------------------------------------------------- */
    372  1.9.2.2  skrll 
    373  1.9.2.2  skrll /*
    374  1.9.2.2  skrll  * Memory management stuff.
    375  1.9.2.2  skrll  */
    376  1.9.2.2  skrll 
    377  1.9.2.2  skrll /* Amount of memory pages to reserve for the system (e.g., to not use by
    378  1.9.2.2  skrll  * tmpfs).
    379  1.9.2.2  skrll  * XXX: Should this be tunable through sysctl, for instance? */
    380  1.9.2.2  skrll #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
    381  1.9.2.2  skrll 
    382  1.9.2.2  skrll /* Returns the maximum size allowed for a tmpfs file system.  This macro
    383  1.9.2.2  skrll  * must be used instead of directly retrieving the value from tm_pages_max.
    384  1.9.2.2  skrll  * The reason is that the size of a tmpfs file system is dynamic: it lets
    385  1.9.2.2  skrll  * the user store files as long as there is enough free memory (including
    386  1.9.2.2  skrll  * physical memory and swap space).  Therefore, the amount of memory to be
    387  1.9.2.2  skrll  * used is either the limit imposed by the user during mount time or the
    388  1.9.2.2  skrll  * amount of available memory, whichever is lower.  To avoid consuming all
    389  1.9.2.2  skrll  * the memory for a given mount point, the system will always reserve a
    390  1.9.2.2  skrll  * minimum of TMPFS_PAGES_RESERVED pages, which is also taken into account
    391  1.9.2.2  skrll  * by this macro (see above). */
    392  1.9.2.2  skrll static inline size_t
    393  1.9.2.2  skrll TMPFS_PAGES_MAX(struct tmpfs_mount *tmp)
    394  1.9.2.2  skrll {
    395  1.9.2.2  skrll 	size_t freepages;
    396  1.9.2.2  skrll 
    397  1.9.2.2  skrll 	freepages = tmpfs_mem_info(FALSE);
    398  1.9.2.2  skrll 	if (freepages < TMPFS_PAGES_RESERVED)
    399  1.9.2.2  skrll 		freepages = 0;
    400  1.9.2.2  skrll 	else
    401  1.9.2.2  skrll 		freepages -= TMPFS_PAGES_RESERVED;
    402  1.9.2.2  skrll 
    403  1.9.2.2  skrll 	return MIN(tmp->tm_pages_max, freepages + tmp->tm_pages_used);
    404  1.9.2.2  skrll }
    405  1.9.2.2  skrll 
    406  1.9.2.2  skrll /* Returns the available space for the given file system. */
    407  1.9.2.2  skrll #define TMPFS_PAGES_AVAIL(tmp) (TMPFS_PAGES_MAX(tmp) - (tmp)->tm_pages_used)
    408  1.9.2.2  skrll 
    409  1.9.2.2  skrll /* --------------------------------------------------------------------- */
    410  1.9.2.2  skrll 
    411  1.9.2.2  skrll /*
    412  1.9.2.2  skrll  * Macros/functions to convert from generic data structures to tmpfs
    413  1.9.2.2  skrll  * specific ones.
    414  1.9.2.2  skrll  *
    415  1.9.2.2  skrll  * Macros are used when no sanity checks have to be done, as they provide
    416  1.9.2.2  skrll  * the fastest conversion.  On the other hand, inlined functions are used
    417  1.9.2.2  skrll  * when expensive sanity checks are enabled, mostly because the checks
    418  1.9.2.2  skrll  * have to be done separately from the return value.
    419  1.9.2.2  skrll  */
    420  1.9.2.2  skrll 
    421  1.9.2.2  skrll #if defined(DIAGNOSTIC)
    422  1.9.2.2  skrll static inline
    423  1.9.2.2  skrll struct tmpfs_mount *
    424  1.9.2.2  skrll VFS_TO_TMPFS(struct mount *mp)
    425  1.9.2.2  skrll {
    426  1.9.2.2  skrll 	struct tmpfs_mount *tmp;
    427  1.9.2.2  skrll 
    428  1.9.2.2  skrll 	KASSERT((mp) != NULL && (mp)->mnt_data != NULL);
    429  1.9.2.2  skrll 	tmp = (struct tmpfs_mount *)(mp)->mnt_data;
    430  1.9.2.2  skrll 	KASSERT(TMPFS_PAGES_MAX(tmp) >= tmp->tm_pages_used);
    431  1.9.2.2  skrll 	return tmp;
    432  1.9.2.2  skrll }
    433  1.9.2.2  skrll 
    434  1.9.2.2  skrll static inline
    435  1.9.2.2  skrll struct tmpfs_node *
    436  1.9.2.2  skrll VP_TO_TMPFS_NODE(struct vnode *vp)
    437  1.9.2.2  skrll {
    438  1.9.2.2  skrll 	struct tmpfs_node *node;
    439  1.9.2.2  skrll 
    440  1.9.2.2  skrll 	KASSERT((vp) != NULL && (vp)->v_data != NULL);
    441  1.9.2.2  skrll 	node = (struct tmpfs_node *)vp->v_data;
    442  1.9.2.2  skrll 	return node;
    443  1.9.2.2  skrll }
    444  1.9.2.2  skrll 
    445  1.9.2.2  skrll static inline
    446  1.9.2.2  skrll struct tmpfs_node *
    447  1.9.2.2  skrll VP_TO_TMPFS_DIR(struct vnode *vp)
    448  1.9.2.2  skrll {
    449  1.9.2.2  skrll 	struct tmpfs_node *node;
    450  1.9.2.2  skrll 
    451  1.9.2.2  skrll 	node = VP_TO_TMPFS_NODE(vp);
    452  1.9.2.2  skrll 	TMPFS_VALIDATE_DIR(node);
    453  1.9.2.2  skrll 	return node;
    454  1.9.2.2  skrll }
    455  1.9.2.2  skrll #else
    456  1.9.2.2  skrll #	define VFS_TO_TMPFS(mp) ((struct tmpfs_mount *)mp->mnt_data)
    457  1.9.2.2  skrll #	define VP_TO_TMPFS_NODE(vp) ((struct tmpfs_node *)vp->v_data)
    458  1.9.2.2  skrll #	define VP_TO_TMPFS_DIR(vp) VP_TO_TMPFS_NODE(vp)
    459  1.9.2.2  skrll #endif
    460  1.9.2.2  skrll 
    461  1.9.2.2  skrll #endif /* _KERNEL */
    462  1.9.2.2  skrll 
    463  1.9.2.2  skrll /* ---------------------------------------------------------------------
    464  1.9.2.2  skrll  * USER AND KERNEL DEFINITIONS
    465  1.9.2.2  skrll  * --------------------------------------------------------------------- */
    466  1.9.2.2  skrll 
    467  1.9.2.2  skrll /*
    468  1.9.2.2  skrll  * This structure is used to communicate mount parameters between userland
    469  1.9.2.2  skrll  * and kernel space.
    470  1.9.2.2  skrll  */
    471  1.9.2.2  skrll #define TMPFS_ARGS_VERSION	1
    472  1.9.2.2  skrll struct tmpfs_args {
    473  1.9.2.2  skrll 	int			ta_version;
    474  1.9.2.2  skrll 
    475  1.9.2.2  skrll 	/* Size counters. */
    476  1.9.2.2  skrll 	ino_t			ta_nodes_max;
    477  1.9.2.2  skrll 	off_t			ta_size_max;
    478  1.9.2.2  skrll 
    479  1.9.2.2  skrll 	/* Root node attributes. */
    480  1.9.2.2  skrll 	uid_t			ta_root_uid;
    481  1.9.2.2  skrll 	gid_t			ta_root_gid;
    482  1.9.2.2  skrll 	mode_t			ta_root_mode;
    483  1.9.2.2  skrll };
    484