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