1258945Sroberto/*	$NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $	*/
2258945Sroberto
3258945Sroberto/*-
4258945Sroberto * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5258945Sroberto * All rights reserved.
6258945Sroberto *
7258945Sroberto * This code is derived from software contributed to The NetBSD Foundation
8258945Sroberto * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9258945Sroberto * 2005 program.
10280849Scy *
11280849Scy * Redistribution and use in source and binary forms, with or without
12258945Sroberto * modification, are permitted provided that the following conditions
13258945Sroberto * are met:
14258945Sroberto * 1. Redistributions of source code must retain the above copyright
15258945Sroberto *    notice, this list of conditions and the following disclaimer.
16258945Sroberto * 2. Redistributions in binary form must reproduce the above copyright
17258945Sroberto *    notice, this list of conditions and the following disclaimer in the
18258945Sroberto *    documentation and/or other materials provided with the distribution.
19258945Sroberto *
20258945Sroberto * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21258945Sroberto * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22280849Scy * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23280849Scy * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24280849Scy * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25280849Scy * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26258945Sroberto * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27258945Sroberto * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28258945Sroberto * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29258945Sroberto * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30258945Sroberto * POSSIBILITY OF SUCH DAMAGE.
31258945Sroberto *
32258945Sroberto * $FreeBSD$
33258945Sroberto */
34258945Sroberto
35258945Sroberto#ifndef _FS_TMPFS_TMPFS_H_
36258945Sroberto#define _FS_TMPFS_TMPFS_H_
37258945Sroberto
38290000Sglebius/* ---------------------------------------------------------------------
39258945Sroberto * KERNEL-SPECIFIC DEFINITIONS
40258945Sroberto * --------------------------------------------------------------------- */
41258945Sroberto#include <sys/dirent.h>
42258945Sroberto#include <sys/mount.h>
43258945Sroberto#include <sys/queue.h>
44258945Sroberto#include <sys/vnode.h>
45258945Sroberto#include <sys/file.h>
46258945Sroberto#include <sys/lock.h>
47258945Sroberto#include <sys/mutex.h>
48258945Sroberto
49258945Sroberto/* --------------------------------------------------------------------- */
50258945Sroberto#include <sys/malloc.h>
51258945Sroberto#include <sys/systm.h>
52258945Sroberto#include <sys/tree.h>
53280849Scy#include <sys/vmmeter.h>
54280849Scy#include <vm/swap_pager.h>
55258945Sroberto
56258945SrobertoMALLOC_DECLARE(M_TMPFSMNT);
57258945SrobertoMALLOC_DECLARE(M_TMPFSNAME);
58258945Sroberto
59258945Sroberto/* --------------------------------------------------------------------- */
60280849Scy
61258945Sroberto/*
62258945Sroberto * Internal representation of a tmpfs directory entry.
63258945Sroberto */
64280849Scy
65258945SrobertoLIST_HEAD(tmpfs_dir_duphead, tmpfs_dirent);
66258945Sroberto
67258945Srobertostruct tmpfs_dirent {
68258945Sroberto	/*
69258945Sroberto	 * Depending on td_cookie flag entry can be of 3 types:
70258945Sroberto	 * - regular -- no hash collisions, stored in RB-Tree
71258945Sroberto	 * - duphead -- synthetic linked list head for dup entries
72258945Sroberto	 * - dup -- stored in linked list instead of RB-Tree
73258945Sroberto	 */
74258945Sroberto	union {
75258945Sroberto		/* regular and duphead entry types */
76258945Sroberto		RB_ENTRY(tmpfs_dirent)		td_entries;
77258945Sroberto
78258945Sroberto		/* dup entry type */
79258945Sroberto		struct {
80258945Sroberto			LIST_ENTRY(tmpfs_dirent) entries;
81258945Sroberto			LIST_ENTRY(tmpfs_dirent) index_entries;
82258945Sroberto		} td_dup;
83258945Sroberto	} uh;
84258945Sroberto
85258945Sroberto	uint32_t			td_cookie;
86258945Sroberto	uint32_t			td_hash;
87258945Sroberto	u_int				td_namelen;
88258945Sroberto
89258945Sroberto	/* Pointer to the node this entry refers to.  In case this field
90258945Sroberto	 * is NULL, the node is a whiteout. */
91280849Scy	struct tmpfs_node *		td_node;
92280849Scy
93258945Sroberto	union {
94258945Sroberto		/*
95280849Scy		 * The name of the entry, allocated from a string pool.  This
96258945Sroberto		 * string is not required to be zero-terminated.
97258945Sroberto		 */
98258945Sroberto		char *			td_name;	/* regular, dup */
99258945Sroberto		struct tmpfs_dir_duphead td_duphead;	/* duphead */
100258945Sroberto	} ud;
101258945Sroberto};
102258945Sroberto
103258945Sroberto/* A directory in tmpfs holds a list of directory entries, which in
104258945Sroberto * turn point to other files (which can be directories themselves).
105258945Sroberto *
106258945Sroberto * In tmpfs, this list is managed by a RB-Tree, whose head is defined by
107258945Sroberto * the struct tmpfs_dir type.
108258945Sroberto *
109258945Sroberto * It is important to notice that directories do not have entries for . and
110258945Sroberto * .. as other file systems do.  These can be generated when requested
111258945Sroberto * based on information available by other means, such as the pointer to
112258945Sroberto * the node itself in the former case or the pointer to the parent directory
113258945Sroberto * in the latter case.  This is done to simplify tmpfs's code and, more
114258945Sroberto * importantly, to remove redundancy. */
115280849ScyRB_HEAD(tmpfs_dir, tmpfs_dirent);
116280849Scy
117280849Scy/* Each entry in a directory has a cookie that identifies it.  Cookies
118280849Scy * supersede offsets within directories because, given how tmpfs stores
119280849Scy * directories in memory, there is no such thing as an offset.
120280849Scy *
121280849Scy * The '.', '..' and the end of directory markers have fixed cookies which
122280849Scy * cannot collide with the cookies generated by other entries.  The cookies
123280849Scy * for the other entries are generated based on the file name hash value or
124280849Scy * unique number in case of name hash collision.
125280849Scy *
126258945Sroberto * To preserve compatibility cookies are limited to 31 bits.
127258945Sroberto */
128258945Sroberto
129258945Sroberto#define	TMPFS_DIRCOOKIE_DOT		0
130258945Sroberto#define	TMPFS_DIRCOOKIE_DOTDOT		1
131258945Sroberto#define	TMPFS_DIRCOOKIE_EOF		2
132258945Sroberto#define	TMPFS_DIRCOOKIE_MASK		((off_t)0x3fffffffU)
133258945Sroberto#define	TMPFS_DIRCOOKIE_MIN		((off_t)0x00000004U)
134258945Sroberto#define	TMPFS_DIRCOOKIE_DUP		((off_t)0x40000000U)
135258945Sroberto#define	TMPFS_DIRCOOKIE_DUPHEAD		((off_t)0x80000000U)
136258945Sroberto#define	TMPFS_DIRCOOKIE_DUP_MIN		TMPFS_DIRCOOKIE_DUP
137258945Sroberto#define	TMPFS_DIRCOOKIE_DUP_MAX		\
138258945Sroberto	(TMPFS_DIRCOOKIE_DUP | TMPFS_DIRCOOKIE_MASK)
139258945Sroberto
140258945Sroberto/* --------------------------------------------------------------------- */
141258945Sroberto
142258945Sroberto/*
143258945Sroberto * Internal representation of a tmpfs file system node.
144258945Sroberto *
145258945Sroberto * This structure is splitted in two parts: one holds attributes common
146280849Scy * to all file types and the other holds data that is only applicable to
147280849Scy * a particular type.  The code must be careful to only access those
148280849Scy * attributes that are actually allowed by the node's type.
149280849Scy *
150280849Scy *
151280849Scy * Below is the key of locks used to protected the fields in the following
152258945Sroberto * structures.
153258945Sroberto *
154258945Sroberto */
155258945Srobertostruct tmpfs_node {
156258945Sroberto	/* Doubly-linked list entry which links all existing nodes for a
157258945Sroberto	 * single file system.  This is provided to ease the removal of
158258945Sroberto	 * all nodes during the unmount operation. */
159280849Scy	LIST_ENTRY(tmpfs_node)	tn_entries;
160258945Sroberto
161258945Sroberto	/* The node's type.  Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
162280849Scy	 * 'VLNK', 'VREG' and 'VSOCK' is allowed.  The usage of vnode
163258945Sroberto	 * types instead of a custom enumeration is to make things simpler
164258945Sroberto	 * and faster, as we do not need to convert between two types. */
165258945Sroberto	enum vtype		tn_type;
166258945Sroberto
167258945Sroberto	/* Node identifier. */
168258945Sroberto	ino_t			tn_id;
169258945Sroberto
170258945Sroberto	/* Node's internal status.  This is used by several file system
171258945Sroberto	 * operations to do modifications to the node in a delayed
172258945Sroberto	 * fashion. */
173280849Scy	int			tn_status;
174280849Scy#define	TMPFS_NODE_ACCESSED	(1 << 1)
175258945Sroberto#define	TMPFS_NODE_MODIFIED	(1 << 2)
176258945Sroberto#define	TMPFS_NODE_CHANGED	(1 << 3)
177258945Sroberto
178258945Sroberto	/* The node size.  It does not necessarily match the real amount
179258945Sroberto	 * of memory consumed by it. */
180280849Scy	off_t			tn_size;
181280849Scy
182258945Sroberto	/* Generic node attributes. */
183258945Sroberto	uid_t			tn_uid;
184258945Sroberto	gid_t			tn_gid;
185258945Sroberto	mode_t			tn_mode;
186258945Sroberto	u_long			tn_flags;
187258945Sroberto	nlink_t			tn_links;
188258945Sroberto	struct timespec		tn_atime;
189258945Sroberto	struct timespec		tn_mtime;
190258945Sroberto	struct timespec		tn_ctime;
191258945Sroberto	struct timespec		tn_birthtime;
192258945Sroberto	unsigned long		tn_gen;
193258945Sroberto
194258945Sroberto	/* As there is a single vnode for each active file within the
195280849Scy	 * system, care has to be taken to avoid allocating more than one
196258945Sroberto	 * vnode per file.  In order to do this, a bidirectional association
197258945Sroberto	 * is kept between vnodes and nodes.
198258945Sroberto	 *
199258945Sroberto	 * Whenever a vnode is allocated, its v_data field is updated to
200258945Sroberto	 * point to the node it references.  At the same time, the node's
201258945Sroberto	 * tn_vnode field is modified to point to the new vnode representing
202280849Scy	 * it.  Further attempts to allocate a vnode for this same node will
203258945Sroberto	 * result in returning a new reference to the value stored in
204258945Sroberto	 * tn_vnode.
205258945Sroberto	 *
206258945Sroberto	 * May be NULL when the node is unused (that is, no vnode has been
207258945Sroberto	 * allocated for it or it has been reclaimed). */
208280849Scy	struct vnode *		tn_vnode;
209258945Sroberto
210258945Sroberto	/* interlock to protect tn_vpstate */
211258945Sroberto	struct mtx	tn_interlock;
212258945Sroberto
213258945Sroberto	/* Identify if current node has vnode assiocate with
214258945Sroberto	 * or allocating vnode.
215258945Sroberto	 */
216258945Sroberto	int		tn_vpstate;
217258945Sroberto
218258945Sroberto	/* misc data field for different tn_type node */
219258945Sroberto	union {
220258945Sroberto		/* Valid when tn_type == VBLK || tn_type == VCHR. */
221258945Sroberto		dev_t			tn_rdev;
222258945Sroberto
223258945Sroberto		/* Valid when tn_type == VDIR. */
224258945Sroberto		struct tn_dir {
225258945Sroberto			/* Pointer to the parent directory.  The root
226258945Sroberto			 * directory has a pointer to itself in this field;
227258945Sroberto			 * this property identifies the root node. */
228258945Sroberto			struct tmpfs_node *	tn_parent;
229258945Sroberto
230258945Sroberto			/* Head of a tree that links the contents of
231258945Sroberto			 * the directory together. */
232258945Sroberto			struct tmpfs_dir	tn_dirhead;
233258945Sroberto
234258945Sroberto			/* Head of a list the contains fake directory entries
235258945Sroberto			 * heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD
236258945Sroberto			 * flag. */
237258945Sroberto			struct tmpfs_dir_duphead tn_dupindex;
238258945Sroberto
239258945Sroberto			/* Number and pointer of the first directory entry
240258945Sroberto			 * returned by the readdir operation if it were
241280849Scy			 * called again to continue reading data from the
242280849Scy			 * same directory as before.  This is used to speed
243258945Sroberto			 * up reads of long directories, assuming that no
244258945Sroberto			 * more than one read is in progress at a given time.
245280849Scy			 * Otherwise, these values are discarded. */
246258945Sroberto			off_t			tn_readdir_lastn;
247258945Sroberto			struct tmpfs_dirent *	tn_readdir_lastp;
248258945Sroberto		} tn_dir;
249290000Sglebius
250258945Sroberto		/* Valid when tn_type == VLNK. */
251258945Sroberto		/* The link's target, allocated from a string pool. */
252258945Sroberto		char *			tn_link;
253258945Sroberto
254258945Sroberto		/* Valid when tn_type == VREG. */
255258945Sroberto		struct tn_reg {
256258945Sroberto			/* The contents of regular files stored in a tmpfs
257258945Sroberto			 * file system are represented by a single anonymous
258258945Sroberto			 * memory object (aobj, for short).  The aobj provides
259258945Sroberto			 * direct access to any position within the file,
260258945Sroberto			 * because its contents are always mapped in a
261258945Sroberto			 * contiguous region of virtual memory.  It is a task
262258945Sroberto			 * of the memory management subsystem (see uvm(9)) to
263258945Sroberto			 * issue the required page ins or page outs whenever
264258945Sroberto			 * a position within the file is accessed. */
265258945Sroberto			vm_object_t		tn_aobj;
266258945Sroberto
267258945Sroberto		}tn_reg;
268258945Sroberto
269258945Sroberto		/* Valid when tn_type = VFIFO */
270258945Sroberto		struct tn_fifo {
271258945Sroberto			fo_rdwr_t		*tn_fo_read;
272258945Sroberto			fo_rdwr_t		*tn_fo_write;
273258945Sroberto		}tn_fifo;
274258945Sroberto	}tn_spec;
275258945Sroberto};
276258945SrobertoLIST_HEAD(tmpfs_node_list, tmpfs_node);
277258945Sroberto
278258945Sroberto#define tn_rdev tn_spec.tn_rdev
279258945Sroberto#define tn_dir tn_spec.tn_dir
280258945Sroberto#define tn_link tn_spec.tn_link
281258945Sroberto#define tn_reg tn_spec.tn_reg
282258945Sroberto#define tn_fifo tn_spec.tn_fifo
283258945Sroberto
284258945Sroberto#define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock)
285258945Sroberto#define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock)
286258945Sroberto#define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
287258945Sroberto
288258945Sroberto#ifdef INVARIANTS
289258945Sroberto#define TMPFS_ASSERT_LOCKED(node) do {					\
290258945Sroberto		MPASS(node != NULL);					\
291280849Scy		MPASS(node->tn_vnode != NULL);				\
292258945Sroberto		if (!VOP_ISLOCKED(node->tn_vnode) &&			\
293258945Sroberto		    !mtx_owned(TMPFS_NODE_MTX(node)))			\
294280849Scy			panic("tmpfs: node is not locked: %p", node);	\
295280849Scy	} while (0)
296280849Scy#define TMPFS_ASSERT_ELOCKED(node) do {					\
297280849Scy		MPASS((node) != NULL);					\
298280849Scy		MPASS((node)->tn_vnode != NULL);			\
299280849Scy		mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED);		\
300280849Scy		ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs");		\
301258945Sroberto	} while (0)
302#else
303#define TMPFS_ASSERT_LOCKED(node) (void)0
304#define TMPFS_ASSERT_ELOCKED(node) (void)0
305#endif
306
307#define TMPFS_VNODE_ALLOCATING	1
308#define TMPFS_VNODE_WANT	2
309#define TMPFS_VNODE_DOOMED	4
310#define	TMPFS_VNODE_WRECLAIM	8
311/* --------------------------------------------------------------------- */
312
313/*
314 * Internal representation of a tmpfs mount point.
315 */
316struct tmpfs_mount {
317	/* Maximum number of memory pages available for use by the file
318	 * system, set during mount time.  This variable must never be
319	 * used directly as it may be bigger than the current amount of
320	 * free memory; in the extreme case, it will hold the SIZE_MAX
321	 * value. */
322	size_t			tm_pages_max;
323
324	/* Number of pages in use by the file system. */
325	size_t			tm_pages_used;
326
327	/* Pointer to the node representing the root directory of this
328	 * file system. */
329	struct tmpfs_node *	tm_root;
330
331	/* Maximum number of possible nodes for this file system; set
332	 * during mount time.  We need a hard limit on the maximum number
333	 * of nodes to avoid allocating too much of them; their objects
334	 * cannot be released until the file system is unmounted.
335	 * Otherwise, we could easily run out of memory by creating lots
336	 * of empty files and then simply removing them. */
337	ino_t			tm_nodes_max;
338
339	/* unrhdr used to allocate inode numbers */
340	struct unrhdr *		tm_ino_unr;
341
342	/* Number of nodes currently that are in use. */
343	ino_t			tm_nodes_inuse;
344
345	/* maximum representable file size */
346	u_int64_t		tm_maxfilesize;
347
348	/* Nodes are organized in two different lists.  The used list
349	 * contains all nodes that are currently used by the file system;
350	 * i.e., they refer to existing files.  The available list contains
351	 * all nodes that are currently available for use by new files.
352	 * Nodes must be kept in this list (instead of deleting them)
353	 * because we need to keep track of their generation number (tn_gen
354	 * field).
355	 *
356	 * Note that nodes are lazily allocated: if the available list is
357	 * empty and we have enough space to create more nodes, they will be
358	 * created and inserted in the used list.  Once these are released,
359	 * they will go into the available list, remaining alive until the
360	 * file system is unmounted. */
361	struct tmpfs_node_list	tm_nodes_used;
362
363	/* All node lock to protect the node list and tmp_pages_used */
364	struct mtx allnode_lock;
365
366	/* Pools used to store file system meta data.  These are not shared
367	 * across several instances of tmpfs for the reasons described in
368	 * tmpfs_pool.c. */
369	uma_zone_t		tm_dirent_pool;
370	uma_zone_t		tm_node_pool;
371
372	/* Read-only status. */
373	int			tm_ronly;
374};
375#define TMPFS_LOCK(tm) mtx_lock(&(tm)->allnode_lock)
376#define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->allnode_lock)
377
378/* --------------------------------------------------------------------- */
379
380/*
381 * This structure maps a file identifier to a tmpfs node.  Used by the
382 * NFS code.
383 */
384struct tmpfs_fid {
385	uint16_t		tf_len;
386	uint16_t		tf_pad;
387	ino_t			tf_id;
388	unsigned long		tf_gen;
389};
390
391/* --------------------------------------------------------------------- */
392
393#ifdef _KERNEL
394/*
395 * Prototypes for tmpfs_subr.c.
396 */
397
398int	tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
399	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
400	    char *, dev_t, struct tmpfs_node **);
401void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
402int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
403	    const char *, u_int, struct tmpfs_dirent **);
404void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
405void	tmpfs_dirent_init(struct tmpfs_dirent *, const char *, u_int);
406void	tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj);
407int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
408	    struct vnode **);
409void	tmpfs_free_vp(struct vnode *);
410int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
411	    struct componentname *, char *);
412void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
413void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
414void	tmpfs_dir_destroy(struct tmpfs_mount *, struct tmpfs_node *);
415struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
416			    struct tmpfs_node *f,
417			    struct componentname *cnp);
418int	tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, int,
419	    u_long *, int *);
420int	tmpfs_dir_whiteout_add(struct vnode *, struct componentname *);
421void	tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *);
422int	tmpfs_reg_resize(struct vnode *, off_t, boolean_t);
423int	tmpfs_chflags(struct vnode *, u_long, struct ucred *, struct thread *);
424int	tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *);
425int	tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
426	    struct thread *);
427int	tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *);
428int	tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
429	    struct timespec *, int, struct ucred *, struct thread *);
430void	tmpfs_itimes(struct vnode *, const struct timespec *,
431	    const struct timespec *);
432
433void	tmpfs_update(struct vnode *);
434int	tmpfs_truncate(struct vnode *, off_t);
435
436/* --------------------------------------------------------------------- */
437
438/*
439 * Convenience macros to simplify some logical expressions.
440 */
441#define IMPLIES(a, b) (!(a) || (b))
442#define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
443
444/* --------------------------------------------------------------------- */
445
446/*
447 * Checks that the directory entry pointed by 'de' matches the name 'name'
448 * with a length of 'len'.
449 */
450#define TMPFS_DIRENT_MATCHES(de, name, len) \
451    (de->td_namelen == len && \
452    bcmp((de)->ud.td_name, (name), (de)->td_namelen) == 0)
453
454/* --------------------------------------------------------------------- */
455
456/*
457 * Ensures that the node pointed by 'node' is a directory and that its
458 * contents are consistent with respect to directories.
459 */
460#define TMPFS_VALIDATE_DIR(node) do { \
461	MPASS((node)->tn_type == VDIR); \
462	MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
463} while (0)
464
465/* --------------------------------------------------------------------- */
466
467/*
468 * Memory management stuff.
469 */
470
471/*
472 * Amount of memory pages to reserve for the system (e.g., to not use by
473 * tmpfs).
474 */
475#define TMPFS_PAGES_MINRESERVED		(4 * 1024 * 1024 / PAGE_SIZE)
476
477size_t tmpfs_mem_avail(void);
478
479size_t tmpfs_pages_used(struct tmpfs_mount *tmp);
480
481#endif
482
483/* --------------------------------------------------------------------- */
484
485/*
486 * Macros/functions to convert from generic data structures to tmpfs
487 * specific ones.
488 */
489
490static inline
491struct tmpfs_mount *
492VFS_TO_TMPFS(struct mount *mp)
493{
494	struct tmpfs_mount *tmp;
495
496	MPASS((mp) != NULL && (mp)->mnt_data != NULL);
497	tmp = (struct tmpfs_mount *)(mp)->mnt_data;
498	return tmp;
499}
500
501static inline
502struct tmpfs_node *
503VP_TO_TMPFS_NODE(struct vnode *vp)
504{
505	struct tmpfs_node *node;
506
507	MPASS((vp) != NULL && (vp)->v_data != NULL);
508	node = (struct tmpfs_node *)vp->v_data;
509	return node;
510}
511
512static inline
513struct tmpfs_node *
514VP_TO_TMPFS_DIR(struct vnode *vp)
515{
516	struct tmpfs_node *node;
517
518	node = VP_TO_TMPFS_NODE(vp);
519	TMPFS_VALIDATE_DIR(node);
520	return node;
521}
522
523#endif /* _FS_TMPFS_TMPFS_H_ */
524