1171802Sdelphij/*	$NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $	*/
2170808Sdelphij
3182739Sdelphij/*-
4171802Sdelphij * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5170808Sdelphij * All rights reserved.
6170808Sdelphij *
7170808Sdelphij * This code is derived from software contributed to The NetBSD Foundation
8170808Sdelphij * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9170808Sdelphij * 2005 program.
10170808Sdelphij *
11170808Sdelphij * Redistribution and use in source and binary forms, with or without
12170808Sdelphij * modification, are permitted provided that the following conditions
13170808Sdelphij * are met:
14170808Sdelphij * 1. Redistributions of source code must retain the above copyright
15170808Sdelphij *    notice, this list of conditions and the following disclaimer.
16170808Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
17170808Sdelphij *    notice, this list of conditions and the following disclaimer in the
18170808Sdelphij *    documentation and/or other materials provided with the distribution.
19170808Sdelphij *
20170808Sdelphij * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21170808Sdelphij * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22170808Sdelphij * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23170808Sdelphij * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24170808Sdelphij * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25170808Sdelphij * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26170808Sdelphij * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27170808Sdelphij * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28170808Sdelphij * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29170808Sdelphij * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30170808Sdelphij * POSSIBILITY OF SUCH DAMAGE.
31170808Sdelphij *
32170808Sdelphij * $FreeBSD$
33170808Sdelphij */
34170808Sdelphij
35170808Sdelphij#ifndef _FS_TMPFS_TMPFS_H_
36170808Sdelphij#define _FS_TMPFS_TMPFS_H_
37170808Sdelphij
38170808Sdelphij/* ---------------------------------------------------------------------
39170808Sdelphij * KERNEL-SPECIFIC DEFINITIONS
40170808Sdelphij * --------------------------------------------------------------------- */
41170808Sdelphij#include <sys/dirent.h>
42170808Sdelphij#include <sys/mount.h>
43170808Sdelphij#include <sys/queue.h>
44170808Sdelphij#include <sys/vnode.h>
45170808Sdelphij#include <sys/file.h>
46170808Sdelphij#include <sys/lock.h>
47170808Sdelphij#include <sys/mutex.h>
48170808Sdelphij
49170808Sdelphij/* --------------------------------------------------------------------- */
50170808Sdelphij#include <sys/malloc.h>
51170808Sdelphij#include <sys/systm.h>
52170808Sdelphij#include <sys/vmmeter.h>
53170808Sdelphij#include <vm/swap_pager.h>
54170808Sdelphij
55170808SdelphijMALLOC_DECLARE(M_TMPFSMNT);
56171087SdelphijMALLOC_DECLARE(M_TMPFSNAME);
57170808Sdelphij
58170808Sdelphij/* --------------------------------------------------------------------- */
59170808Sdelphij
60170808Sdelphij/*
61170808Sdelphij * Internal representation of a tmpfs directory entry.
62170808Sdelphij */
63170808Sdelphijstruct tmpfs_dirent {
64170808Sdelphij	TAILQ_ENTRY(tmpfs_dirent)	td_entries;
65170808Sdelphij
66170808Sdelphij	/* Length of the name stored in this directory entry.  This avoids
67170808Sdelphij	 * the need to recalculate it every time the name is used. */
68170808Sdelphij	uint16_t			td_namelen;
69170808Sdelphij
70170808Sdelphij	/* The name of the entry, allocated from a string pool.  This
71170808Sdelphij	* string is not required to be zero-terminated; therefore, the
72170808Sdelphij	* td_namelen field must always be used when accessing its value. */
73170808Sdelphij	char *				td_name;
74170808Sdelphij
75211598Sed	/* Pointer to the node this entry refers to.  In case this field
76211598Sed	 * is NULL, the node is a whiteout. */
77170808Sdelphij	struct tmpfs_node *		td_node;
78170808Sdelphij};
79170808Sdelphij
80170808Sdelphij/* A directory in tmpfs holds a sorted list of directory entries, which in
81170808Sdelphij * turn point to other files (which can be directories themselves).
82170808Sdelphij *
83170808Sdelphij * In tmpfs, this list is managed by a tail queue, whose head is defined by
84170808Sdelphij * the struct tmpfs_dir type.
85170808Sdelphij *
86170808Sdelphij * It is imporant to notice that directories do not have entries for . and
87170808Sdelphij * .. as other file systems do.  These can be generated when requested
88170808Sdelphij * based on information available by other means, such as the pointer to
89170808Sdelphij * the node itself in the former case or the pointer to the parent directory
90170808Sdelphij * in the latter case.  This is done to simplify tmpfs's code and, more
91170808Sdelphij * importantly, to remove redundancy. */
92170808SdelphijTAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
93170808Sdelphij
94171802Sdelphij/* Each entry in a directory has a cookie that identifies it.  Cookies
95171802Sdelphij * supersede offsets within directories because, given how tmpfs stores
96171802Sdelphij * directories in memory, there is no such thing as an offset.  (Emulating
97171802Sdelphij * a real offset could be very difficult.)
98171802Sdelphij *
99171802Sdelphij * The '.', '..' and the end of directory markers have fixed cookies which
100171802Sdelphij * cannot collide with the cookies generated by other entries.  The cookies
101171802Sdelphij * fot the other entries are generated based on the memory address on which
102171802Sdelphij * stores their information is stored.
103171802Sdelphij *
104171802Sdelphij * Ideally, using the entry's memory pointer as the cookie would be enough
105171802Sdelphij * to represent it and it wouldn't cause collisions in any system.
106171802Sdelphij * Unfortunately, this results in "offsets" with very large values which
107171802Sdelphij * later raise problems in the Linux compatibility layer (and maybe in other
108171802Sdelphij * places) as described in PR kern/32034.  Hence we need to workaround this
109171802Sdelphij * with a rather ugly hack.
110171802Sdelphij *
111171802Sdelphij * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t
112171802Sdelphij * set to 'long', which is a 32-bit *signed* long integer.  Regardless of
113171802Sdelphij * the macro value, GLIBC (2.3 at least) always uses the getdents64
114171802Sdelphij * system call (when calling readdir) which internally returns off64_t
115171802Sdelphij * offsets.  In order to make 32-bit binaries work, *GLIBC* converts the
116171802Sdelphij * 64-bit values returned by the kernel to 32-bit ones and aborts with
117171802Sdelphij * EOVERFLOW if the conversion results in values that won't fit in 32-bit
118171802Sdelphij * integers (which it assumes is because the directory is extremely large).
119171802Sdelphij * This wouldn't cause problems if we were dealing with unsigned integers,
120171802Sdelphij * but as we have signed integers, this check fails due to sign expansion.
121171802Sdelphij *
122171802Sdelphij * For example, consider that the kernel returns the 0xc1234567 cookie to
123171802Sdelphij * userspace in a off64_t integer.  Later on, GLIBC casts this value to
124171802Sdelphij * off_t (remember, signed) with code similar to:
125171802Sdelphij *     system call returns the offset in kernel_value;
126171802Sdelphij *     off_t casted_value = kernel_value;
127171802Sdelphij *     if (sizeof(off_t) != sizeof(off64_t) &&
128171802Sdelphij *         kernel_value != casted_value)
129171802Sdelphij *             error!
130171802Sdelphij * In this case, casted_value still has 0xc1234567, but when it is compared
131171802Sdelphij * for equality against kernel_value, it is promoted to a 64-bit integer and
132171802Sdelphij * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567.
133171802Sdelphij * Then, GLIBC assumes this is because the directory is very large.
134171802Sdelphij *
135171802Sdelphij * Given that all the above happens in user-space, we have no control over
136171802Sdelphij * it; therefore we must workaround the issue here.  We do this by
137171802Sdelphij * truncating the pointer value to a 32-bit integer and hope that there
138171802Sdelphij * won't be collisions.  In fact, this will not cause any problems in
139171802Sdelphij * 32-bit platforms but some might arise in 64-bit machines (I'm not sure
140171802Sdelphij * if they can happen at all in practice).
141171802Sdelphij *
142171802Sdelphij * XXX A nicer solution shall be attempted. */
143171802Sdelphij#ifdef _KERNEL
144170808Sdelphij#define	TMPFS_DIRCOOKIE_DOT	0
145170808Sdelphij#define	TMPFS_DIRCOOKIE_DOTDOT	1
146170808Sdelphij#define	TMPFS_DIRCOOKIE_EOF	2
147171802Sdelphijstatic __inline
148171802Sdelphijoff_t
149171802Sdelphijtmpfs_dircookie(struct tmpfs_dirent *de)
150171802Sdelphij{
151171802Sdelphij	off_t cookie;
152170808Sdelphij
153171802Sdelphij	cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF;
154171802Sdelphij	MPASS(cookie != TMPFS_DIRCOOKIE_DOT);
155171802Sdelphij	MPASS(cookie != TMPFS_DIRCOOKIE_DOTDOT);
156171802Sdelphij	MPASS(cookie != TMPFS_DIRCOOKIE_EOF);
157171802Sdelphij
158171802Sdelphij	return cookie;
159171802Sdelphij}
160171802Sdelphij#endif
161171802Sdelphij
162170808Sdelphij/* --------------------------------------------------------------------- */
163170808Sdelphij
164170808Sdelphij/*
165170808Sdelphij * Internal representation of a tmpfs file system node.
166170808Sdelphij *
167170808Sdelphij * This structure is splitted in two parts: one holds attributes common
168170808Sdelphij * to all file types and the other holds data that is only applicable to
169170808Sdelphij * a particular type.  The code must be careful to only access those
170170808Sdelphij * attributes that are actually allowed by the node's type.
171170808Sdelphij *
172170808Sdelphij *
173170808Sdelphij * Below is the key of locks used to protected the fields in the following
174170808Sdelphij * structures.
175170808Sdelphij *
176170808Sdelphij */
177170808Sdelphijstruct tmpfs_node {
178170808Sdelphij	/* Doubly-linked list entry which links all existing nodes for a
179170808Sdelphij	 * single file system.  This is provided to ease the removal of
180170808Sdelphij	 * all nodes during the unmount operation. */
181170808Sdelphij	LIST_ENTRY(tmpfs_node)	tn_entries;
182170808Sdelphij
183170808Sdelphij	/* The node's type.  Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
184170808Sdelphij	 * 'VLNK', 'VREG' and 'VSOCK' is allowed.  The usage of vnode
185170808Sdelphij	 * types instead of a custom enumeration is to make things simpler
186170808Sdelphij	 * and faster, as we do not need to convert between two types. */
187170808Sdelphij	enum vtype		tn_type;
188170808Sdelphij
189170808Sdelphij	/* Node identifier. */
190170808Sdelphij	ino_t			tn_id;
191170808Sdelphij
192170808Sdelphij	/* Node's internal status.  This is used by several file system
193170808Sdelphij	 * operations to do modifications to the node in a delayed
194170808Sdelphij	 * fashion. */
195170808Sdelphij	int			tn_status;
196170808Sdelphij#define	TMPFS_NODE_ACCESSED	(1 << 1)
197170808Sdelphij#define	TMPFS_NODE_MODIFIED	(1 << 2)
198170808Sdelphij#define	TMPFS_NODE_CHANGED	(1 << 3)
199170808Sdelphij
200170808Sdelphij	/* The node size.  It does not necessarily match the real amount
201170808Sdelphij	 * of memory consumed by it. */
202170808Sdelphij	off_t			tn_size;
203170808Sdelphij
204170808Sdelphij	/* Generic node attributes. */
205170808Sdelphij	uid_t			tn_uid;
206170808Sdelphij	gid_t			tn_gid;
207170808Sdelphij	mode_t			tn_mode;
208170808Sdelphij	int			tn_flags;
209170808Sdelphij	nlink_t			tn_links;
210170808Sdelphij	struct timespec		tn_atime;
211170808Sdelphij	struct timespec		tn_mtime;
212170808Sdelphij	struct timespec		tn_ctime;
213170808Sdelphij	struct timespec		tn_birthtime;
214170808Sdelphij	unsigned long		tn_gen;
215170808Sdelphij
216170808Sdelphij	/* As there is a single vnode for each active file within the
217170808Sdelphij	 * system, care has to be taken to avoid allocating more than one
218170808Sdelphij	 * vnode per file.  In order to do this, a bidirectional association
219170808Sdelphij	 * is kept between vnodes and nodes.
220170808Sdelphij	 *
221170808Sdelphij	 * Whenever a vnode is allocated, its v_data field is updated to
222170808Sdelphij	 * point to the node it references.  At the same time, the node's
223170808Sdelphij	 * tn_vnode field is modified to point to the new vnode representing
224170808Sdelphij	 * it.  Further attempts to allocate a vnode for this same node will
225170808Sdelphij	 * result in returning a new reference to the value stored in
226170808Sdelphij	 * tn_vnode.
227170808Sdelphij	 *
228170808Sdelphij	 * May be NULL when the node is unused (that is, no vnode has been
229170808Sdelphij	 * allocated for it or it has been reclaimed). */
230170808Sdelphij	struct vnode *		tn_vnode;
231170808Sdelphij
232170808Sdelphij	/* interlock to protect tn_vpstate */
233170808Sdelphij	struct mtx	tn_interlock;
234170808Sdelphij
235170808Sdelphij	/* Identify if current node has vnode assiocate with
236170808Sdelphij	 * or allocating vnode.
237170808Sdelphij	 */
238170808Sdelphij	int		tn_vpstate;
239170808Sdelphij
240170808Sdelphij	/* misc data field for different tn_type node */
241170808Sdelphij	union {
242170808Sdelphij		/* Valid when tn_type == VBLK || tn_type == VCHR. */
243170808Sdelphij		dev_t			tn_rdev;
244170808Sdelphij
245170808Sdelphij		/* Valid when tn_type == VDIR. */
246170808Sdelphij		struct tn_dir{
247170808Sdelphij			/* Pointer to the parent directory.  The root
248170808Sdelphij			 * directory has a pointer to itself in this field;
249170808Sdelphij			 * this property identifies the root node. */
250170808Sdelphij			struct tmpfs_node *	tn_parent;
251170808Sdelphij
252170808Sdelphij			/* Head of a tail-queue that links the contents of
253170808Sdelphij			 * the directory together.  See above for a
254170808Sdelphij			 * description of its contents. */
255170808Sdelphij			struct tmpfs_dir	tn_dirhead;
256170808Sdelphij
257170808Sdelphij			/* Number and pointer of the first directory entry
258170808Sdelphij			 * returned by the readdir operation if it were
259170808Sdelphij			 * called again to continue reading data from the
260170808Sdelphij			 * same directory as before.  This is used to speed
261170808Sdelphij			 * up reads of long directories, assuming that no
262170808Sdelphij			 * more than one read is in progress at a given time.
263170808Sdelphij			 * Otherwise, these values are discarded and a linear
264170808Sdelphij			 * scan is performed from the beginning up to the
265170808Sdelphij			 * point where readdir starts returning values. */
266170808Sdelphij			off_t			tn_readdir_lastn;
267170808Sdelphij			struct tmpfs_dirent *	tn_readdir_lastp;
268170808Sdelphij		}tn_dir;
269170808Sdelphij
270170808Sdelphij		/* Valid when tn_type == VLNK. */
271170808Sdelphij		/* The link's target, allocated from a string pool. */
272170808Sdelphij		char *			tn_link;
273170808Sdelphij
274170808Sdelphij		/* Valid when tn_type == VREG. */
275170808Sdelphij		struct tn_reg {
276170808Sdelphij			/* The contents of regular files stored in a tmpfs
277170808Sdelphij			 * file system are represented by a single anonymous
278170808Sdelphij			 * memory object (aobj, for short).  The aobj provides
279170808Sdelphij			 * direct access to any position within the file,
280170808Sdelphij			 * because its contents are always mapped in a
281170808Sdelphij			 * contiguous region of virtual memory.  It is a task
282170808Sdelphij			 * of the memory management subsystem (see uvm(9)) to
283170808Sdelphij			 * issue the required page ins or page outs whenever
284170808Sdelphij			 * a position within the file is accessed. */
285170808Sdelphij			vm_object_t		tn_aobj;
286170808Sdelphij
287170808Sdelphij		}tn_reg;
288170808Sdelphij
289170808Sdelphij		/* Valid when tn_type = VFIFO */
290170808Sdelphij		struct tn_fifo {
291170808Sdelphij			fo_rdwr_t		*tn_fo_read;
292170808Sdelphij			fo_rdwr_t		*tn_fo_write;
293170808Sdelphij		}tn_fifo;
294170808Sdelphij	}tn_spec;
295170808Sdelphij};
296170808SdelphijLIST_HEAD(tmpfs_node_list, tmpfs_node);
297170808Sdelphij
298170808Sdelphij#define tn_rdev tn_spec.tn_rdev
299170808Sdelphij#define tn_dir tn_spec.tn_dir
300170808Sdelphij#define tn_link tn_spec.tn_link
301170808Sdelphij#define tn_reg tn_spec.tn_reg
302170808Sdelphij#define tn_fifo tn_spec.tn_fifo
303170808Sdelphij
304170808Sdelphij#define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock)
305170808Sdelphij#define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock)
306197953Sdelphij#define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
307170808Sdelphij
308197953Sdelphij#ifdef INVARIANTS
309197953Sdelphij#define TMPFS_ASSERT_LOCKED(node) do {					\
310197953Sdelphij		MPASS(node != NULL);					\
311197953Sdelphij		MPASS(node->tn_vnode != NULL);				\
312197953Sdelphij		if (!VOP_ISLOCKED(node->tn_vnode) &&			\
313197953Sdelphij		    !mtx_owned(TMPFS_NODE_MTX(node)))			\
314197953Sdelphij			panic("tmpfs: node is not locked: %p", node);	\
315197953Sdelphij	} while (0)
316197953Sdelphij#define TMPFS_ASSERT_ELOCKED(node) do {					\
317197953Sdelphij		MPASS((node) != NULL);					\
318197953Sdelphij		MPASS((node)->tn_vnode != NULL);			\
319197953Sdelphij		mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED);		\
320197953Sdelphij		ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs");		\
321197953Sdelphij	} while (0)
322197953Sdelphij#else
323197953Sdelphij#define TMPFS_ASSERT_LOCKED(node) (void)0
324197953Sdelphij#define TMPFS_ASSERT_ELOCKED(node) (void)0
325197953Sdelphij#endif
326197953Sdelphij
327170808Sdelphij#define TMPFS_VNODE_ALLOCATING	1
328170808Sdelphij#define TMPFS_VNODE_WANT	2
329197953Sdelphij#define TMPFS_VNODE_DOOMED	4
330254550Skib#define	TMPFS_VNODE_WRECLAIM	8
331170808Sdelphij/* --------------------------------------------------------------------- */
332170808Sdelphij
333170808Sdelphij/*
334170808Sdelphij * Internal representation of a tmpfs mount point.
335170808Sdelphij */
336170808Sdelphijstruct tmpfs_mount {
337170808Sdelphij	/* Maximum number of memory pages available for use by the file
338170808Sdelphij	 * system, set during mount time.  This variable must never be
339171038Sdelphij	 * used directly as it may be bigger than the current amount of
340170808Sdelphij	 * free memory; in the extreme case, it will hold the SIZE_MAX
341234849Sgleb	 * value. */
342170808Sdelphij	size_t			tm_pages_max;
343170808Sdelphij
344234849Sgleb	/* Number of pages in use by the file system. */
345170808Sdelphij	size_t			tm_pages_used;
346170808Sdelphij
347170808Sdelphij	/* Pointer to the node representing the root directory of this
348170808Sdelphij	 * file system. */
349170808Sdelphij	struct tmpfs_node *	tm_root;
350170808Sdelphij
351170808Sdelphij	/* Maximum number of possible nodes for this file system; set
352170808Sdelphij	 * during mount time.  We need a hard limit on the maximum number
353170808Sdelphij	 * of nodes to avoid allocating too much of them; their objects
354170808Sdelphij	 * cannot be released until the file system is unmounted.
355170808Sdelphij	 * Otherwise, we could easily run out of memory by creating lots
356170808Sdelphij	 * of empty files and then simply removing them. */
357170808Sdelphij	ino_t			tm_nodes_max;
358170808Sdelphij
359171362Sdelphij	/* unrhdr used to allocate inode numbers */
360171362Sdelphij	struct unrhdr *		tm_ino_unr;
361170808Sdelphij
362170808Sdelphij	/* Number of nodes currently that are in use. */
363170808Sdelphij	ino_t			tm_nodes_inuse;
364170808Sdelphij
365170808Sdelphij	/* maximum representable file size */
366170808Sdelphij	u_int64_t		tm_maxfilesize;
367171070Sdelphij
368170808Sdelphij	/* Nodes are organized in two different lists.  The used list
369170808Sdelphij	 * contains all nodes that are currently used by the file system;
370170808Sdelphij	 * i.e., they refer to existing files.  The available list contains
371170808Sdelphij	 * all nodes that are currently available for use by new files.
372170808Sdelphij	 * Nodes must be kept in this list (instead of deleting them)
373170808Sdelphij	 * because we need to keep track of their generation number (tn_gen
374170808Sdelphij	 * field).
375170808Sdelphij	 *
376170808Sdelphij	 * Note that nodes are lazily allocated: if the available list is
377170808Sdelphij	 * empty and we have enough space to create more nodes, they will be
378170808Sdelphij	 * created and inserted in the used list.  Once these are released,
379170808Sdelphij	 * they will go into the available list, remaining alive until the
380170808Sdelphij	 * file system is unmounted. */
381170808Sdelphij	struct tmpfs_node_list	tm_nodes_used;
382170808Sdelphij
383170808Sdelphij	/* All node lock to protect the node list and tmp_pages_used */
384170808Sdelphij	struct mtx allnode_lock;
385170808Sdelphij
386170808Sdelphij	/* Pools used to store file system meta data.  These are not shared
387170808Sdelphij	 * across several instances of tmpfs for the reasons described in
388170808Sdelphij	 * tmpfs_pool.c. */
389170808Sdelphij	uma_zone_t		tm_dirent_pool;
390170808Sdelphij	uma_zone_t		tm_node_pool;
391242368Salfred
392242368Salfred	/* Read-only status. */
393242368Salfred	int			tm_ronly;
394170808Sdelphij};
395170808Sdelphij#define TMPFS_LOCK(tm) mtx_lock(&(tm)->allnode_lock)
396170808Sdelphij#define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->allnode_lock)
397170808Sdelphij
398170808Sdelphij/* --------------------------------------------------------------------- */
399170808Sdelphij
400170808Sdelphij/*
401170808Sdelphij * This structure maps a file identifier to a tmpfs node.  Used by the
402170808Sdelphij * NFS code.
403170808Sdelphij */
404170808Sdelphijstruct tmpfs_fid {
405170808Sdelphij	uint16_t		tf_len;
406170808Sdelphij	uint16_t		tf_pad;
407171067Sdelphij	ino_t			tf_id;
408170808Sdelphij	unsigned long		tf_gen;
409170808Sdelphij};
410170808Sdelphij
411170808Sdelphij/* --------------------------------------------------------------------- */
412170808Sdelphij
413170808Sdelphij#ifdef _KERNEL
414170808Sdelphij/*
415170808Sdelphij * Prototypes for tmpfs_subr.c.
416170808Sdelphij */
417170808Sdelphij
418170808Sdelphijint	tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
419170808Sdelphij	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
420191990Sattilio	    char *, dev_t, struct tmpfs_node **);
421170808Sdelphijvoid	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
422170808Sdelphijint	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
423170808Sdelphij	    const char *, uint16_t, struct tmpfs_dirent **);
424170808Sdelphijvoid	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
425170808Sdelphij	    boolean_t);
426171799Sdelphijint	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
427191990Sattilio	    struct vnode **);
428170808Sdelphijvoid	tmpfs_free_vp(struct vnode *);
429170808Sdelphijint	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
430170808Sdelphij	    struct componentname *, char *);
431170808Sdelphijvoid	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
432170808Sdelphijvoid	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
433170808Sdelphijstruct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
434188318Skib			    struct tmpfs_node *f,
435170808Sdelphij			    struct componentname *cnp);
436170808Sdelphijint	tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
437170808Sdelphijint	tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
438170808Sdelphijstruct tmpfs_dirent *	tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
439170808Sdelphijint	tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
440211598Sedint	tmpfs_dir_whiteout_add(struct vnode *, struct componentname *);
441211598Sedvoid	tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *);
442236208Salcint	tmpfs_reg_resize(struct vnode *, off_t, boolean_t);
443170808Sdelphijint	tmpfs_chflags(struct vnode *, int, struct ucred *, struct thread *);
444170808Sdelphijint	tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *);
445170808Sdelphijint	tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
446170808Sdelphij	    struct thread *);
447170808Sdelphijint	tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *);
448171070Sdelphijint	tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
449170808Sdelphij	    struct timespec *, int, struct ucred *, struct thread *);
450170808Sdelphijvoid	tmpfs_itimes(struct vnode *, const struct timespec *,
451170808Sdelphij	    const struct timespec *);
452170808Sdelphij
453170808Sdelphijvoid	tmpfs_update(struct vnode *);
454170808Sdelphijint	tmpfs_truncate(struct vnode *, off_t);
455170808Sdelphij
456170808Sdelphij/* --------------------------------------------------------------------- */
457170808Sdelphij
458170808Sdelphij/*
459170808Sdelphij * Convenience macros to simplify some logical expressions.
460170808Sdelphij */
461170808Sdelphij#define IMPLIES(a, b) (!(a) || (b))
462170808Sdelphij#define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
463170808Sdelphij
464170808Sdelphij/* --------------------------------------------------------------------- */
465170808Sdelphij
466170808Sdelphij/*
467170808Sdelphij * Checks that the directory entry pointed by 'de' matches the name 'name'
468170808Sdelphij * with a length of 'len'.
469170808Sdelphij */
470170808Sdelphij#define TMPFS_DIRENT_MATCHES(de, name, len) \
471170808Sdelphij    (de->td_namelen == (uint16_t)len && \
472183299Sobrien    bcmp((de)->td_name, (name), (de)->td_namelen) == 0)
473170808Sdelphij
474170808Sdelphij/* --------------------------------------------------------------------- */
475170808Sdelphij
476170808Sdelphij/*
477170808Sdelphij * Ensures that the node pointed by 'node' is a directory and that its
478170808Sdelphij * contents are consistent with respect to directories.
479170808Sdelphij */
480170808Sdelphij#define TMPFS_VALIDATE_DIR(node) \
481170808Sdelphij    MPASS((node)->tn_type == VDIR); \
482170808Sdelphij    MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
483170808Sdelphij    MPASS((node)->tn_dir.tn_readdir_lastp == NULL || \
484171802Sdelphij	tmpfs_dircookie((node)->tn_dir.tn_readdir_lastp) == (node)->tn_dir.tn_readdir_lastn);
485170808Sdelphij
486170808Sdelphij/* --------------------------------------------------------------------- */
487170808Sdelphij
488170808Sdelphij/*
489170808Sdelphij * Memory management stuff.
490170808Sdelphij */
491170808Sdelphij
492234849Sgleb/*
493234849Sgleb * Amount of memory pages to reserve for the system (e.g., to not use by
494170808Sdelphij * tmpfs).
495170808Sdelphij */
496234849Sgleb#define TMPFS_PAGES_MINRESERVED		(4 * 1024 * 1024 / PAGE_SIZE)
497170808Sdelphij
498234849Sglebsize_t tmpfs_mem_avail(void);
499170808Sdelphij
500234849Sglebsize_t tmpfs_pages_used(struct tmpfs_mount *tmp);
501170808Sdelphij
502170808Sdelphij#endif
503170808Sdelphij
504170808Sdelphij/* --------------------------------------------------------------------- */
505170808Sdelphij
506170808Sdelphij/*
507170808Sdelphij * Macros/functions to convert from generic data structures to tmpfs
508170808Sdelphij * specific ones.
509170808Sdelphij */
510170808Sdelphij
511170808Sdelphijstatic inline
512170808Sdelphijstruct tmpfs_mount *
513170808SdelphijVFS_TO_TMPFS(struct mount *mp)
514170808Sdelphij{
515170808Sdelphij	struct tmpfs_mount *tmp;
516170808Sdelphij
517170808Sdelphij	MPASS((mp) != NULL && (mp)->mnt_data != NULL);
518170808Sdelphij	tmp = (struct tmpfs_mount *)(mp)->mnt_data;
519170808Sdelphij	return tmp;
520170808Sdelphij}
521170808Sdelphij
522170808Sdelphijstatic inline
523170808Sdelphijstruct tmpfs_node *
524170808SdelphijVP_TO_TMPFS_NODE(struct vnode *vp)
525170808Sdelphij{
526170808Sdelphij	struct tmpfs_node *node;
527170808Sdelphij
528170808Sdelphij	MPASS((vp) != NULL && (vp)->v_data != NULL);
529170808Sdelphij	node = (struct tmpfs_node *)vp->v_data;
530170808Sdelphij	return node;
531170808Sdelphij}
532170808Sdelphij
533170808Sdelphijstatic inline
534170808Sdelphijstruct tmpfs_node *
535170808SdelphijVP_TO_TMPFS_DIR(struct vnode *vp)
536170808Sdelphij{
537170808Sdelphij	struct tmpfs_node *node;
538170808Sdelphij
539170808Sdelphij	node = VP_TO_TMPFS_NODE(vp);
540170808Sdelphij	TMPFS_VALIDATE_DIR(node);
541170808Sdelphij	return node;
542170808Sdelphij}
543170808Sdelphij
544170808Sdelphij#endif /* _FS_TMPFS_TMPFS_H_ */
545