1ad3638eeSXin LI /* $NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $ */
2d1fa59e9SXin LI
3e08d5567SXin LI /*-
4b61a5730SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
5d63027b6SPedro F. Giffuni *
6ad3638eeSXin LI * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
7d1fa59e9SXin LI * All rights reserved.
8d1fa59e9SXin LI *
9d1fa59e9SXin LI * This code is derived from software contributed to The NetBSD Foundation
10d1fa59e9SXin LI * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11d1fa59e9SXin LI * 2005 program.
12d1fa59e9SXin LI *
13d1fa59e9SXin LI * Redistribution and use in source and binary forms, with or without
14d1fa59e9SXin LI * modification, are permitted provided that the following conditions
15d1fa59e9SXin LI * are met:
16d1fa59e9SXin LI * 1. Redistributions of source code must retain the above copyright
17d1fa59e9SXin LI * notice, this list of conditions and the following disclaimer.
18d1fa59e9SXin LI * 2. Redistributions in binary form must reproduce the above copyright
19d1fa59e9SXin LI * notice, this list of conditions and the following disclaimer in the
20d1fa59e9SXin LI * documentation and/or other materials provided with the distribution.
21d1fa59e9SXin LI *
22d1fa59e9SXin LI * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23d1fa59e9SXin LI * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24d1fa59e9SXin LI * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25d1fa59e9SXin LI * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26d1fa59e9SXin LI * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27d1fa59e9SXin LI * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28d1fa59e9SXin LI * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29d1fa59e9SXin LI * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30d1fa59e9SXin LI * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31d1fa59e9SXin LI * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32d1fa59e9SXin LI * POSSIBILITY OF SUCH DAMAGE.
33d1fa59e9SXin LI */
34d1fa59e9SXin LI
35d1fa59e9SXin LI #ifndef _FS_TMPFS_TMPFS_H_
36d1fa59e9SXin LI #define _FS_TMPFS_TMPFS_H_
37d1fa59e9SXin LI
38693d10a2SRyan Moeller #include <sys/cdefs.h>
39d1fa59e9SXin LI #include <sys/queue.h>
404fd5efe7SGleb Kurtsou #include <sys/tree.h>
41d1fa59e9SXin LI
42135beaf6SGleb Smirnoff #ifdef _SYS_MALLOC_H_
439b258fcaSXin LI MALLOC_DECLARE(M_TMPFSNAME);
44135beaf6SGleb Smirnoff #endif
45d1fa59e9SXin LI
46128e2584SMateusz Guzik #define OBJ_TMPFS OBJ_PAGERPRIV1 /* has tmpfs vnode allocated */
47eec2e4efSMateusz Guzik #define OBJ_TMPFS_VREF OBJ_PAGERPRIV2 /* vnode is referenced */
4828bc23abSKonstantin Belousov
49d1fa59e9SXin LI /*
50d1fa59e9SXin LI * Internal representation of a tmpfs directory entry.
51d1fa59e9SXin LI */
524fd5efe7SGleb Kurtsou
534fd5efe7SGleb Kurtsou LIST_HEAD(tmpfs_dir_duphead, tmpfs_dirent);
544fd5efe7SGleb Kurtsou
55d1fa59e9SXin LI struct tmpfs_dirent {
564fd5efe7SGleb Kurtsou /*
574fd5efe7SGleb Kurtsou * Depending on td_cookie flag entry can be of 3 types:
584fd5efe7SGleb Kurtsou * - regular -- no hash collisions, stored in RB-Tree
594fd5efe7SGleb Kurtsou * - duphead -- synthetic linked list head for dup entries
604fd5efe7SGleb Kurtsou * - dup -- stored in linked list instead of RB-Tree
614fd5efe7SGleb Kurtsou */
624fd5efe7SGleb Kurtsou union {
634fd5efe7SGleb Kurtsou /* regular and duphead entry types */
644fd5efe7SGleb Kurtsou RB_ENTRY(tmpfs_dirent) td_entries;
65d1fa59e9SXin LI
664fd5efe7SGleb Kurtsou /* dup entry type */
674fd5efe7SGleb Kurtsou struct {
684fd5efe7SGleb Kurtsou LIST_ENTRY(tmpfs_dirent) entries;
694fd5efe7SGleb Kurtsou LIST_ENTRY(tmpfs_dirent) index_entries;
704fd5efe7SGleb Kurtsou } td_dup;
714fd5efe7SGleb Kurtsou } uh;
72d1fa59e9SXin LI
734fd5efe7SGleb Kurtsou uint32_t td_cookie;
744fd5efe7SGleb Kurtsou uint32_t td_hash;
754fd5efe7SGleb Kurtsou u_int td_namelen;
76d1fa59e9SXin LI
77bba7ed20SKonstantin Belousov /*
78bba7ed20SKonstantin Belousov * Pointer to the node this entry refers to. In case this field
79bba7ed20SKonstantin Belousov * is NULL, the node is a whiteout.
80bba7ed20SKonstantin Belousov */
81d1fa59e9SXin LI struct tmpfs_node * td_node;
824fd5efe7SGleb Kurtsou
834fd5efe7SGleb Kurtsou union {
844fd5efe7SGleb Kurtsou /*
854fd5efe7SGleb Kurtsou * The name of the entry, allocated from a string pool. This
864fd5efe7SGleb Kurtsou * string is not required to be zero-terminated.
874fd5efe7SGleb Kurtsou */
884fd5efe7SGleb Kurtsou char * td_name; /* regular, dup */
894fd5efe7SGleb Kurtsou struct tmpfs_dir_duphead td_duphead; /* duphead */
904fd5efe7SGleb Kurtsou } ud;
91d1fa59e9SXin LI };
92d1fa59e9SXin LI
93bba7ed20SKonstantin Belousov /*
94bba7ed20SKonstantin Belousov * A directory in tmpfs holds a collection of directory entries, which
95bba7ed20SKonstantin Belousov * in turn point to other files (which can be directories themselves).
96d1fa59e9SXin LI *
97bba7ed20SKonstantin Belousov * In tmpfs, this collection is managed by a RB-Tree, whose head is
98bba7ed20SKonstantin Belousov * defined by the struct tmpfs_dir type.
99d1fa59e9SXin LI *
1004fd5efe7SGleb Kurtsou * It is important to notice that directories do not have entries for . and
101d1fa59e9SXin LI * .. as other file systems do. These can be generated when requested
102d1fa59e9SXin LI * based on information available by other means, such as the pointer to
103d1fa59e9SXin LI * the node itself in the former case or the pointer to the parent directory
104d1fa59e9SXin LI * in the latter case. This is done to simplify tmpfs's code and, more
105bba7ed20SKonstantin Belousov * importantly, to remove redundancy.
106bba7ed20SKonstantin Belousov */
1074fd5efe7SGleb Kurtsou RB_HEAD(tmpfs_dir, tmpfs_dirent);
108d1fa59e9SXin LI
109bba7ed20SKonstantin Belousov /*
110bba7ed20SKonstantin Belousov * Each entry in a directory has a cookie that identifies it. Cookies
111ad3638eeSXin LI * supersede offsets within directories because, given how tmpfs stores
1124fd5efe7SGleb Kurtsou * directories in memory, there is no such thing as an offset.
113ad3638eeSXin LI *
114ad3638eeSXin LI * The '.', '..' and the end of directory markers have fixed cookies which
115ad3638eeSXin LI * cannot collide with the cookies generated by other entries. The cookies
1164fd5efe7SGleb Kurtsou * for the other entries are generated based on the file name hash value or
1174fd5efe7SGleb Kurtsou * unique number in case of name hash collision.
118ad3638eeSXin LI *
1194fd5efe7SGleb Kurtsou * To preserve compatibility cookies are limited to 31 bits.
1204fd5efe7SGleb Kurtsou */
1214fd5efe7SGleb Kurtsou
122d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_DOT 0
123d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_DOTDOT 1
124d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_EOF 2
1254fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_MASK ((off_t)0x3fffffffU)
1264fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_MIN ((off_t)0x00000004U)
1274fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUP ((off_t)0x40000000U)
1284fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUPHEAD ((off_t)0x80000000U)
1294fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUP_MIN TMPFS_DIRCOOKIE_DUP
1304fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUP_MAX \
1314fd5efe7SGleb Kurtsou (TMPFS_DIRCOOKIE_DUP | TMPFS_DIRCOOKIE_MASK)
132d1fa59e9SXin LI
133d1fa59e9SXin LI /*
13456242a4cSFedor Uporov * Internal representation of a tmpfs extended attribute entry.
13556242a4cSFedor Uporov */
13656242a4cSFedor Uporov LIST_HEAD(tmpfs_extattr_list, tmpfs_extattr);
13756242a4cSFedor Uporov
13856242a4cSFedor Uporov struct tmpfs_extattr {
13956242a4cSFedor Uporov LIST_ENTRY(tmpfs_extattr) ea_extattrs;
14056242a4cSFedor Uporov int ea_namespace; /* attr namespace */
14156242a4cSFedor Uporov char *ea_name; /* attr name */
14256242a4cSFedor Uporov unsigned char ea_namelen; /* attr name length */
14356242a4cSFedor Uporov char *ea_value; /* attr value buffer */
14456242a4cSFedor Uporov ssize_t ea_size; /* attr value size */
14556242a4cSFedor Uporov };
14656242a4cSFedor Uporov
14756242a4cSFedor Uporov /*
148d1fa59e9SXin LI * Internal representation of a tmpfs file system node.
149d1fa59e9SXin LI *
150d1fa59e9SXin LI * This structure is splitted in two parts: one holds attributes common
151d1fa59e9SXin LI * to all file types and the other holds data that is only applicable to
152d1fa59e9SXin LI * a particular type. The code must be careful to only access those
153d1fa59e9SXin LI * attributes that are actually allowed by the node's type.
154d1fa59e9SXin LI *
155d1fa59e9SXin LI * Below is the key of locks used to protected the fields in the following
156d1fa59e9SXin LI * structures.
157bba7ed20SKonstantin Belousov * (v) vnode lock in exclusive mode
158bba7ed20SKonstantin Belousov * (vi) vnode lock in exclusive mode, or vnode lock in shared vnode and
159bba7ed20SKonstantin Belousov * tn_interlock
160bba7ed20SKonstantin Belousov * (i) tn_interlock
161280ffa5eSKonstantin Belousov * (m) tmpfs_mount tm_allnode_lock
162bba7ed20SKonstantin Belousov * (c) stable after creation
16337aea264SKonstantin Belousov * (v) tn_reg.tn_aobj vm_object lock
164d1fa59e9SXin LI */
165d1fa59e9SXin LI struct tmpfs_node {
166bba7ed20SKonstantin Belousov /*
167bba7ed20SKonstantin Belousov * Doubly-linked list entry which links all existing nodes for
168bba7ed20SKonstantin Belousov * a single file system. This is provided to ease the removal
169bba7ed20SKonstantin Belousov * of all nodes during the unmount operation, and to support
17064c25043SKonstantin Belousov * the implementation of VOP_VNTOCNP(). tn_attached is false
17164c25043SKonstantin Belousov * when the node is removed from list and unlocked.
172bba7ed20SKonstantin Belousov */
173bba7ed20SKonstantin Belousov LIST_ENTRY(tmpfs_node) tn_entries; /* (m) */
174f24aa01fSMateusz Guzik
175f24aa01fSMateusz Guzik /* Node identifier. */
176f24aa01fSMateusz Guzik ino_t tn_id; /* (c) */
177d1fa59e9SXin LI
178bba7ed20SKonstantin Belousov /*
179bba7ed20SKonstantin Belousov * The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
180d1fa59e9SXin LI * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode
181d1fa59e9SXin LI * types instead of a custom enumeration is to make things simpler
182bba7ed20SKonstantin Belousov * and faster, as we do not need to convert between two types.
183bba7ed20SKonstantin Belousov */
184ba8cc6d7SMateusz Guzik __enum_uint8(vtype) tn_type; /* (c) */
185d1fa59e9SXin LI
186f24aa01fSMateusz Guzik /*
187f24aa01fSMateusz Guzik * See the top comment. Reordered here to fill LP64 hole.
188f24aa01fSMateusz Guzik */
189f24aa01fSMateusz Guzik bool tn_attached; /* (m) */
190d1fa59e9SXin LI
191bba7ed20SKonstantin Belousov /*
192bba7ed20SKonstantin Belousov * Node's internal status. This is used by several file system
193d1fa59e9SXin LI * operations to do modifications to the node in a delayed
194bba7ed20SKonstantin Belousov * fashion.
195016b7c7eSKonstantin Belousov *
196016b7c7eSKonstantin Belousov * tn_accessed has a dedicated byte to allow update by store without
197016b7c7eSKonstantin Belousov * using atomics. This provides a micro-optimization to e.g.
198016b7c7eSKonstantin Belousov * tmpfs_read_pgcache().
199bba7ed20SKonstantin Belousov */
200016b7c7eSKonstantin Belousov uint8_t tn_status; /* (vi) */
201016b7c7eSKonstantin Belousov uint8_t tn_accessed; /* unlocked */
202d1fa59e9SXin LI
203bba7ed20SKonstantin Belousov /*
204bba7ed20SKonstantin Belousov * The node size. It does not necessarily match the real amount
205bba7ed20SKonstantin Belousov * of memory consumed by it.
206bba7ed20SKonstantin Belousov */
207bba7ed20SKonstantin Belousov off_t tn_size; /* (v) */
208d1fa59e9SXin LI
209d1fa59e9SXin LI /* Generic node attributes. */
210bba7ed20SKonstantin Belousov uid_t tn_uid; /* (v) */
211bba7ed20SKonstantin Belousov gid_t tn_gid; /* (v) */
212bba7ed20SKonstantin Belousov mode_t tn_mode; /* (v) */
21335b1a3abSJohn Baldwin int tn_links; /* (v) */
214bba7ed20SKonstantin Belousov u_long tn_flags; /* (v) */
215bba7ed20SKonstantin Belousov struct timespec tn_atime; /* (vi) */
216bba7ed20SKonstantin Belousov struct timespec tn_mtime; /* (vi) */
217bba7ed20SKonstantin Belousov struct timespec tn_ctime; /* (vi) */
218bba7ed20SKonstantin Belousov struct timespec tn_birthtime; /* (v) */
219bba7ed20SKonstantin Belousov unsigned long tn_gen; /* (c) */
220d1fa59e9SXin LI
221bba7ed20SKonstantin Belousov /*
222bba7ed20SKonstantin Belousov * As there is a single vnode for each active file within the
223d1fa59e9SXin LI * system, care has to be taken to avoid allocating more than one
224d1fa59e9SXin LI * vnode per file. In order to do this, a bidirectional association
225d1fa59e9SXin LI * is kept between vnodes and nodes.
226d1fa59e9SXin LI *
227d1fa59e9SXin LI * Whenever a vnode is allocated, its v_data field is updated to
228d1fa59e9SXin LI * point to the node it references. At the same time, the node's
229d1fa59e9SXin LI * tn_vnode field is modified to point to the new vnode representing
230d1fa59e9SXin LI * it. Further attempts to allocate a vnode for this same node will
231d1fa59e9SXin LI * result in returning a new reference to the value stored in
232d1fa59e9SXin LI * tn_vnode.
233d1fa59e9SXin LI *
234d1fa59e9SXin LI * May be NULL when the node is unused (that is, no vnode has been
235bba7ed20SKonstantin Belousov * allocated for it or it has been reclaimed).
236bba7ed20SKonstantin Belousov */
237bba7ed20SKonstantin Belousov struct vnode * tn_vnode; /* (i) */
238d1fa59e9SXin LI
239bba7ed20SKonstantin Belousov /*
240bba7ed20SKonstantin Belousov * Interlock to protect tn_vpstate, and tn_status under shared
2415dc11286SKonstantin Belousov * vnode lock.
2425dc11286SKonstantin Belousov */
243d1fa59e9SXin LI struct mtx tn_interlock;
244d1fa59e9SXin LI
245bba7ed20SKonstantin Belousov /*
246bba7ed20SKonstantin Belousov * Identify if current node has vnode assiocate with
247d1fa59e9SXin LI * or allocating vnode.
248d1fa59e9SXin LI */
249bba7ed20SKonstantin Belousov int tn_vpstate; /* (i) */
250d1fa59e9SXin LI
25164c25043SKonstantin Belousov /* Transient refcounter on this node. */
2524601f5f5SKonstantin Belousov u_int tn_refcount; /* 0<->1 (m) + (i) */
25364c25043SKonstantin Belousov
25456242a4cSFedor Uporov /* Extended attributes of this node. */
25556242a4cSFedor Uporov struct tmpfs_extattr_list tn_extattrs; /* (v) */
25656242a4cSFedor Uporov
257d1fa59e9SXin LI /* misc data field for different tn_type node */
258d1fa59e9SXin LI union {
259d1fa59e9SXin LI /* Valid when tn_type == VBLK || tn_type == VCHR. */
260bba7ed20SKonstantin Belousov dev_t tn_rdev; /* (c) */
261d1fa59e9SXin LI
262d1fa59e9SXin LI /* Valid when tn_type == VDIR. */
263d1fa59e9SXin LI struct tn_dir {
264bba7ed20SKonstantin Belousov /*
265bba7ed20SKonstantin Belousov * Pointer to the parent directory. The root
266d1fa59e9SXin LI * directory has a pointer to itself in this field;
267bba7ed20SKonstantin Belousov * this property identifies the root node.
268bba7ed20SKonstantin Belousov */
269d1fa59e9SXin LI struct tmpfs_node * tn_parent;
270d1fa59e9SXin LI
271bba7ed20SKonstantin Belousov /*
272bba7ed20SKonstantin Belousov * Head of a tree that links the contents of
273bba7ed20SKonstantin Belousov * the directory together.
274bba7ed20SKonstantin Belousov */
275d1fa59e9SXin LI struct tmpfs_dir tn_dirhead;
276d1fa59e9SXin LI
277bba7ed20SKonstantin Belousov /*
278bba7ed20SKonstantin Belousov * Head of a list the contains fake directory entries
2794fd5efe7SGleb Kurtsou * heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD
280bba7ed20SKonstantin Belousov * flag.
281bba7ed20SKonstantin Belousov */
2824fd5efe7SGleb Kurtsou struct tmpfs_dir_duphead tn_dupindex;
2834fd5efe7SGleb Kurtsou
284bba7ed20SKonstantin Belousov /*
285bba7ed20SKonstantin Belousov * Number and pointer of the first directory entry
286d1fa59e9SXin LI * returned by the readdir operation if it were
287d1fa59e9SXin LI * called again to continue reading data from the
288d1fa59e9SXin LI * same directory as before. This is used to speed
289d1fa59e9SXin LI * up reads of long directories, assuming that no
290d1fa59e9SXin LI * more than one read is in progress at a given time.
291bba7ed20SKonstantin Belousov * Otherwise, these values are discarded.
292bba7ed20SKonstantin Belousov */
293d1fa59e9SXin LI off_t tn_readdir_lastn;
294d1fa59e9SXin LI struct tmpfs_dirent * tn_readdir_lastp;
2958fa5e0f2SJason A. Harmening
2968fa5e0f2SJason A. Harmening /*
2978fa5e0f2SJason A. Harmening * Total size of whiteout directory entries. This
2988fa5e0f2SJason A. Harmening * must be a multiple of sizeof(struct tmpfs_dirent)
2998fa5e0f2SJason A. Harmening * and is used to determine whether a directory is
3008fa5e0f2SJason A. Harmening * empty (excluding whiteout entries) during rename/
3018fa5e0f2SJason A. Harmening * rmdir operations.
3028fa5e0f2SJason A. Harmening */
3038fa5e0f2SJason A. Harmening off_t tn_wht_size; /* (v) */
304d1fa59e9SXin LI } tn_dir;
305d1fa59e9SXin LI
306d1fa59e9SXin LI /* Valid when tn_type == VLNK. */
307d1fa59e9SXin LI /* The link's target, allocated from a string pool. */
308618029afSMateusz Guzik struct tn_link {
309618029afSMateusz Guzik char * tn_link_target; /* (c) */
310618029afSMateusz Guzik char tn_link_smr; /* (c) */
311618029afSMateusz Guzik } tn_link;
312d1fa59e9SXin LI
313d1fa59e9SXin LI /* Valid when tn_type == VREG. */
314d1fa59e9SXin LI struct tn_reg {
315bba7ed20SKonstantin Belousov /*
316bba7ed20SKonstantin Belousov * The contents of regular files stored in a
317bba7ed20SKonstantin Belousov * tmpfs file system are represented by a
318bba7ed20SKonstantin Belousov * single anonymous memory object (aobj, for
319bba7ed20SKonstantin Belousov * short). The aobj provides direct access to
320bba7ed20SKonstantin Belousov * any position within the file. It is a task
321bba7ed20SKonstantin Belousov * of the memory management subsystem to issue
322bba7ed20SKonstantin Belousov * the required page ins or page outs whenever
323bba7ed20SKonstantin Belousov * a position within the file is accessed.
324bba7ed20SKonstantin Belousov */
325bba7ed20SKonstantin Belousov vm_object_t tn_aobj; /* (c) */
326081e36e7SKonstantin Belousov struct tmpfs_mount *tn_tmp; /* (c) */
32737aea264SKonstantin Belousov vm_pindex_t tn_pages; /* (v) */
328d1fa59e9SXin LI } tn_reg;
329bba7ed20SKonstantin Belousov } tn_spec; /* (v) */
330d1fa59e9SXin LI };
331d1fa59e9SXin LI LIST_HEAD(tmpfs_node_list, tmpfs_node);
332d1fa59e9SXin LI
333d1fa59e9SXin LI #define tn_rdev tn_spec.tn_rdev
334d1fa59e9SXin LI #define tn_dir tn_spec.tn_dir
335618029afSMateusz Guzik #define tn_link_target tn_spec.tn_link.tn_link_target
336618029afSMateusz Guzik #define tn_link_smr tn_spec.tn_link.tn_link_smr
337d1fa59e9SXin LI #define tn_reg tn_spec.tn_reg
338d1fa59e9SXin LI #define tn_fifo tn_spec.tn_fifo
339d1fa59e9SXin LI
34035b1a3abSJohn Baldwin #define TMPFS_LINK_MAX INT_MAX
34135b1a3abSJohn Baldwin
342d1fa59e9SXin LI #define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock)
343d1fa59e9SXin LI #define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock)
344fb755714SXin LI #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
345d2ca06cdSKonstantin Belousov #define TMPFS_NODE_ASSERT_LOCKED(node) mtx_assert(TMPFS_NODE_MTX(node), \
346d2ca06cdSKonstantin Belousov MA_OWNED)
347d1fa59e9SXin LI
34882cf92d4SXin LI #ifdef INVARIANTS
34982cf92d4SXin LI #define TMPFS_ASSERT_LOCKED(node) do { \
3504960d0d4SKonstantin Belousov MPASS((node) != NULL); \
3514960d0d4SKonstantin Belousov MPASS((node)->tn_vnode != NULL); \
3524960d0d4SKonstantin Belousov ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs assert"); \
35382cf92d4SXin LI } while (0)
35482cf92d4SXin LI #else
35582cf92d4SXin LI #define TMPFS_ASSERT_LOCKED(node) (void)0
35682cf92d4SXin LI #endif
35782cf92d4SXin LI
358016b7c7eSKonstantin Belousov /* tn_vpstate */
359d1fa59e9SXin LI #define TMPFS_VNODE_ALLOCATING 1
360d1fa59e9SXin LI #define TMPFS_VNODE_WANT 2
36182cf92d4SXin LI #define TMPFS_VNODE_DOOMED 4
3628239a7a8SKonstantin Belousov #define TMPFS_VNODE_WRECLAIM 8
363d1fa59e9SXin LI
364016b7c7eSKonstantin Belousov /* tn_status */
365016b7c7eSKonstantin Belousov #define TMPFS_NODE_MODIFIED 0x01
366016b7c7eSKonstantin Belousov #define TMPFS_NODE_CHANGED 0x02
367016b7c7eSKonstantin Belousov
368d1fa59e9SXin LI /*
369d1fa59e9SXin LI * Internal representation of a tmpfs mount point.
370d1fa59e9SXin LI */
371d1fa59e9SXin LI struct tmpfs_mount {
372bba7ed20SKonstantin Belousov /*
373ac1a10efSMaxim Sobolev * Original value of the "size" parameter, for reference purposes,
374ac1a10efSMaxim Sobolev * mostly.
375ac1a10efSMaxim Sobolev */
376ac1a10efSMaxim Sobolev off_t tm_size_max;
377ac1a10efSMaxim Sobolev /*
378bba7ed20SKonstantin Belousov * Maximum number of memory pages available for use by the file
379d1fa59e9SXin LI * system, set during mount time. This variable must never be
380974fd8c6SXin LI * used directly as it may be bigger than the current amount of
381ed2159c9SMateusz Guzik * free memory; in the extreme case, it will hold the ULONG_MAX
382bba7ed20SKonstantin Belousov * value.
383bba7ed20SKonstantin Belousov */
384ed2159c9SMateusz Guzik u_long tm_pages_max;
385d1fa59e9SXin LI
386da7aa277SGleb Kurtsou /* Number of pages in use by the file system. */
387ed2159c9SMateusz Guzik u_long tm_pages_used;
388d1fa59e9SXin LI
389bba7ed20SKonstantin Belousov /*
390bba7ed20SKonstantin Belousov * Pointer to the node representing the root directory of this
391bba7ed20SKonstantin Belousov * file system.
392bba7ed20SKonstantin Belousov */
393d1fa59e9SXin LI struct tmpfs_node * tm_root;
394d1fa59e9SXin LI
395bba7ed20SKonstantin Belousov /*
396bba7ed20SKonstantin Belousov * Maximum number of possible nodes for this file system; set
397d1fa59e9SXin LI * during mount time. We need a hard limit on the maximum number
398d1fa59e9SXin LI * of nodes to avoid allocating too much of them; their objects
399d1fa59e9SXin LI * cannot be released until the file system is unmounted.
400d1fa59e9SXin LI * Otherwise, we could easily run out of memory by creating lots
401bba7ed20SKonstantin Belousov * of empty files and then simply removing them.
402bba7ed20SKonstantin Belousov */
403d1fa59e9SXin LI ino_t tm_nodes_max;
404d1fa59e9SXin LI
4058d9a89a3SXin LI /* unrhdr used to allocate inode numbers */
40630e0cf49SMateusz Guzik struct unrhdr64 tm_ino_unr;
407d1fa59e9SXin LI
408d1fa59e9SXin LI /* Number of nodes currently that are in use. */
409d1fa59e9SXin LI ino_t tm_nodes_inuse;
410d1fa59e9SXin LI
41156242a4cSFedor Uporov /* Memory used by extended attributes */
41256242a4cSFedor Uporov uint64_t tm_ea_memory_inuse;
41356242a4cSFedor Uporov
41456242a4cSFedor Uporov /* Maximum memory available for extended attributes */
41556242a4cSFedor Uporov uint64_t tm_ea_memory_max;
41656242a4cSFedor Uporov
41764c25043SKonstantin Belousov /* Refcounter on this struct tmpfs_mount. */
41864c25043SKonstantin Belousov uint64_t tm_refcount;
41964c25043SKonstantin Belousov
420d1fa59e9SXin LI /* maximum representable file size */
421d1fa59e9SXin LI u_int64_t tm_maxfilesize;
422d1fa59e9SXin LI
423bba7ed20SKonstantin Belousov /*
424bba7ed20SKonstantin Belousov * The used list contains all nodes that are currently used by
425bba7ed20SKonstantin Belousov * the file system; i.e., they refer to existing files.
426bba7ed20SKonstantin Belousov */
427d1fa59e9SXin LI struct tmpfs_node_list tm_nodes_used;
428d1fa59e9SXin LI
429bba7ed20SKonstantin Belousov /* All node lock to protect the node list and tmp_pages_used. */
430280ffa5eSKonstantin Belousov struct mtx tm_allnode_lock;
431d1fa59e9SXin LI
432c5ab5ce3SJaakko Heinonen /* Read-only status. */
43300ac6a98SKonstantin Belousov bool tm_ronly;
43400ac6a98SKonstantin Belousov /* Do not use namecache. */
43500ac6a98SKonstantin Belousov bool tm_nonc;
436c1e84733SKonstantin Belousov /* Do not update mtime on writes through mmaped areas. */
437c1e84733SKonstantin Belousov bool tm_nomtime;
4380f613ab8SKonstantin Belousov
4390f613ab8SKonstantin Belousov /* Read from page cache directly. */
4400f613ab8SKonstantin Belousov bool tm_pgread;
441d1fa59e9SXin LI };
442280ffa5eSKonstantin Belousov #define TMPFS_LOCK(tm) mtx_lock(&(tm)->tm_allnode_lock)
443280ffa5eSKonstantin Belousov #define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->tm_allnode_lock)
444280ffa5eSKonstantin Belousov #define TMPFS_MP_ASSERT_LOCKED(tm) mtx_assert(&(tm)->tm_allnode_lock, MA_OWNED)
445d1fa59e9SXin LI
446d1fa59e9SXin LI /*
447d1fa59e9SXin LI * This structure maps a file identifier to a tmpfs node. Used by the
448d1fa59e9SXin LI * NFS code.
449d1fa59e9SXin LI */
450693d10a2SRyan Moeller struct tmpfs_fid_data {
451*1ccbdf56SOlivier Certner unsigned short tfd_len;
452693d10a2SRyan Moeller ino_t tfd_id;
453693d10a2SRyan Moeller unsigned long tfd_gen;
454*1ccbdf56SOlivier Certner } __packed;
455d1fa59e9SXin LI
4561c07d69bSKonstantin Belousov struct tmpfs_dir_cursor {
4571c07d69bSKonstantin Belousov struct tmpfs_dirent *tdc_current;
4581c07d69bSKonstantin Belousov struct tmpfs_dirent *tdc_tree;
4591c07d69bSKonstantin Belousov };
4601c07d69bSKonstantin Belousov
461d1fa59e9SXin LI #ifdef _KERNEL
462d1fa59e9SXin LI /*
463d1fa59e9SXin LI * Prototypes for tmpfs_subr.c.
464d1fa59e9SXin LI */
465d1fa59e9SXin LI
46664c25043SKonstantin Belousov void tmpfs_ref_node(struct tmpfs_node *node);
467ba8cc6d7SMateusz Guzik int tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *, __enum_uint8(vtype),
468d1fa59e9SXin LI uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
4691493c2eeSBrooks Davis const char *, dev_t, struct tmpfs_node **);
470081e36e7SKonstantin Belousov int tmpfs_fo_close(struct file *fp, struct thread *td);
471d1fa59e9SXin LI void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
47264c25043SKonstantin Belousov bool tmpfs_free_node_locked(struct tmpfs_mount *, struct tmpfs_node *, bool);
47364c25043SKonstantin Belousov void tmpfs_free_tmp(struct tmpfs_mount *);
474d1fa59e9SXin LI int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
4754fd5efe7SGleb Kurtsou const char *, u_int, struct tmpfs_dirent **);
4764fd5efe7SGleb Kurtsou void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
4774fd5efe7SGleb Kurtsou void tmpfs_dirent_init(struct tmpfs_dirent *, const char *, u_int);
478158cc900SKonstantin Belousov void tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj);
4790ae6383dSXin LI int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
480dfd233edSAttilio Rao struct vnode **);
481d1fa59e9SXin LI void tmpfs_free_vp(struct vnode *);
482d1fa59e9SXin LI int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
4831493c2eeSBrooks Davis struct componentname *, const char *);
484f40cb1c6SKonstantin Belousov void tmpfs_check_mtime(struct vnode *);
485d1fa59e9SXin LI void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
486d1fa59e9SXin LI void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
4874fd5efe7SGleb Kurtsou void tmpfs_dir_destroy(struct tmpfs_mount *, struct tmpfs_node *);
488d1fa59e9SXin LI struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node,
489e3c7e753SKonstantin Belousov struct tmpfs_node *f,
490d1fa59e9SXin LI struct componentname *cnp);
491e1cdc30fSKonstantin Belousov int tmpfs_dir_getdents(struct tmpfs_mount *, struct tmpfs_node *,
492b214fcceSAlan Somers struct uio *, int, uint64_t *, int *);
49399d57a6bSEd Schouten int tmpfs_dir_whiteout_add(struct vnode *, struct componentname *);
49499d57a6bSEd Schouten void tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *);
4958fa5e0f2SJason A. Harmening void tmpfs_dir_clear_whiteouts(struct vnode *);
4960b05cac3SAlan Cox int tmpfs_reg_resize(struct vnode *, off_t, boolean_t);
4978d7cd10bSKa Ho Ng int tmpfs_reg_punch_hole(struct vnode *vp, off_t *, off_t *);
498b4b2596bSPawel Jakub Dawidek int tmpfs_chflags(struct vnode *, u_long, struct ucred *, struct thread *);
499d1fa59e9SXin LI int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *);
500d1fa59e9SXin LI int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
501d1fa59e9SXin LI struct thread *);
502d1fa59e9SXin LI int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *);
5037b81a399SKonstantin Belousov int tmpfs_chtimes(struct vnode *, struct vattr *, struct ucred *cred,
5047b81a399SKonstantin Belousov struct thread *);
505d1fa59e9SXin LI void tmpfs_itimes(struct vnode *, const struct timespec *,
506d1fa59e9SXin LI const struct timespec *);
507d1fa59e9SXin LI
508016b7c7eSKonstantin Belousov void tmpfs_set_accessed(struct tmpfs_mount *tm, struct tmpfs_node *node);
509e1cdc30fSKonstantin Belousov void tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node,
510e1cdc30fSKonstantin Belousov int status);
511d1fa59e9SXin LI int tmpfs_truncate(struct vnode *, off_t);
5121c07d69bSKonstantin Belousov struct tmpfs_dirent *tmpfs_dir_first(struct tmpfs_node *dnode,
5131c07d69bSKonstantin Belousov struct tmpfs_dir_cursor *dc);
5141c07d69bSKonstantin Belousov struct tmpfs_dirent *tmpfs_dir_next(struct tmpfs_node *dnode,
5151c07d69bSKonstantin Belousov struct tmpfs_dir_cursor *dc);
51656242a4cSFedor Uporov bool tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages);
51756242a4cSFedor Uporov void tmpfs_extattr_free(struct tmpfs_extattr* ea);
5182abdae33SMateusz Guzik static __inline void
tmpfs_update(struct vnode * vp)5192abdae33SMateusz Guzik tmpfs_update(struct vnode *vp)
5202abdae33SMateusz Guzik {
5212abdae33SMateusz Guzik
5222abdae33SMateusz Guzik tmpfs_itimes(vp, NULL, NULL);
5232abdae33SMateusz Guzik }
524d1fa59e9SXin LI
525d1fa59e9SXin LI /*
526d1fa59e9SXin LI * Convenience macros to simplify some logical expressions.
527d1fa59e9SXin LI */
528d1fa59e9SXin LI #define IMPLIES(a, b) (!(a) || (b))
529d1fa59e9SXin LI
530d1fa59e9SXin LI /*
531d1fa59e9SXin LI * Checks that the directory entry pointed by 'de' matches the name 'name'
532d1fa59e9SXin LI * with a length of 'len'.
533d1fa59e9SXin LI */
534d1fa59e9SXin LI #define TMPFS_DIRENT_MATCHES(de, name, len) \
5354fd5efe7SGleb Kurtsou (de->td_namelen == len && \
5364fd5efe7SGleb Kurtsou bcmp((de)->ud.td_name, (name), (de)->td_namelen) == 0)
537d1fa59e9SXin LI
538d1fa59e9SXin LI /*
539d1fa59e9SXin LI * Ensures that the node pointed by 'node' is a directory and that its
540d1fa59e9SXin LI * contents are consistent with respect to directories.
541d1fa59e9SXin LI */
5424fd5efe7SGleb Kurtsou #define TMPFS_VALIDATE_DIR(node) do { \
543d1fa59e9SXin LI MPASS((node)->tn_type == VDIR); \
544d1fa59e9SXin LI MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
5458fa5e0f2SJason A. Harmening MPASS((node)->tn_dir.tn_wht_size % sizeof(struct tmpfs_dirent) == 0); \
5468fa5e0f2SJason A. Harmening MPASS((node)->tn_dir.tn_wht_size <= (node)->tn_size); \
5474fd5efe7SGleb Kurtsou } while (0)
548d1fa59e9SXin LI
549d1fa59e9SXin LI /*
550da7aa277SGleb Kurtsou * Amount of memory pages to reserve for the system (e.g., to not use by
551da7aa277SGleb Kurtsou * tmpfs).
552d1fa59e9SXin LI */
553b4b3e349SAllan Jude #if !defined(TMPFS_PAGES_MINRESERVED)
554da7aa277SGleb Kurtsou #define TMPFS_PAGES_MINRESERVED (4 * 1024 * 1024 / PAGE_SIZE)
555b4b3e349SAllan Jude #endif
556d1fa59e9SXin LI
55756242a4cSFedor Uporov /*
55863659234SMike Karels * Percent of available memory + swap available to use by tmpfs file systems
55963659234SMike Karels * without a size limit.
56063659234SMike Karels */
56163659234SMike Karels #if !defined(TMPFS_MEM_PERCENT)
5622e68c5a4SMike Karels #define TMPFS_MEM_PERCENT 100
56363659234SMike Karels #endif
56463659234SMike Karels
56563659234SMike Karels /*
56656242a4cSFedor Uporov * Amount of memory to reserve for extended attributes.
56756242a4cSFedor Uporov */
56856242a4cSFedor Uporov #if !defined(TMPFS_EA_MEMORY_RESERVED)
56956242a4cSFedor Uporov #define TMPFS_EA_MEMORY_RESERVED (16 * 1024 * 1024)
57056242a4cSFedor Uporov #endif
57156242a4cSFedor Uporov
572da7aa277SGleb Kurtsou size_t tmpfs_mem_avail(void);
573da7aa277SGleb Kurtsou size_t tmpfs_pages_used(struct tmpfs_mount *tmp);
57428bc23abSKonstantin Belousov int tmpfs_subr_init(void);
575a51c8071SKonstantin Belousov void tmpfs_subr_uninit(void);
576d1fa59e9SXin LI
57728bc23abSKonstantin Belousov extern int tmpfs_pager_type;
57828bc23abSKonstantin Belousov
579d1fa59e9SXin LI /*
580d1fa59e9SXin LI * Macros/functions to convert from generic data structures to tmpfs
581d1fa59e9SXin LI * specific ones.
582d1fa59e9SXin LI */
583d1fa59e9SXin LI
584d9dc64f1SKonstantin Belousov static inline struct vnode *
VM_TO_TMPFS_VP(vm_object_t obj)585d9dc64f1SKonstantin Belousov VM_TO_TMPFS_VP(vm_object_t obj)
586d9dc64f1SKonstantin Belousov {
587d9dc64f1SKonstantin Belousov struct tmpfs_node *node;
588d9dc64f1SKonstantin Belousov
58937aea264SKonstantin Belousov if ((obj->flags & OBJ_TMPFS) == 0)
59037aea264SKonstantin Belousov return (NULL);
591d9dc64f1SKonstantin Belousov
592d9dc64f1SKonstantin Belousov /*
593d9dc64f1SKonstantin Belousov * swp_priv is the back-pointer to the tmpfs node, if any,
594d9dc64f1SKonstantin Belousov * which uses the vm object as backing store. The object
595d9dc64f1SKonstantin Belousov * handle is not used to avoid locking sw_alloc_sx on tmpfs
596d9dc64f1SKonstantin Belousov * node instantiation/destroy.
597d9dc64f1SKonstantin Belousov */
598d9dc64f1SKonstantin Belousov node = obj->un_pager.swp.swp_priv;
599d9dc64f1SKonstantin Belousov return (node->tn_vnode);
600d9dc64f1SKonstantin Belousov }
601d9dc64f1SKonstantin Belousov
602bba7ed20SKonstantin Belousov static inline struct tmpfs_mount *
VM_TO_TMPFS_MP(vm_object_t obj)60337aea264SKonstantin Belousov VM_TO_TMPFS_MP(vm_object_t obj)
60437aea264SKonstantin Belousov {
60537aea264SKonstantin Belousov struct tmpfs_node *node;
60637aea264SKonstantin Belousov
60737aea264SKonstantin Belousov if ((obj->flags & OBJ_TMPFS) == 0)
60837aea264SKonstantin Belousov return (NULL);
60937aea264SKonstantin Belousov
61037aea264SKonstantin Belousov node = obj->un_pager.swp.swp_priv;
61137aea264SKonstantin Belousov MPASS(node->tn_type == VREG);
61237aea264SKonstantin Belousov return (node->tn_reg.tn_tmp);
61337aea264SKonstantin Belousov }
61437aea264SKonstantin Belousov
61537aea264SKonstantin Belousov static inline struct tmpfs_mount *
VFS_TO_TMPFS(struct mount * mp)616d1fa59e9SXin LI VFS_TO_TMPFS(struct mount *mp)
617d1fa59e9SXin LI {
618d1fa59e9SXin LI struct tmpfs_mount *tmp;
619d1fa59e9SXin LI
620bba7ed20SKonstantin Belousov MPASS(mp != NULL && mp->mnt_data != NULL);
621bba7ed20SKonstantin Belousov tmp = (struct tmpfs_mount *)mp->mnt_data;
622bba7ed20SKonstantin Belousov return (tmp);
623d1fa59e9SXin LI }
624d1fa59e9SXin LI
625bba7ed20SKonstantin Belousov static inline struct tmpfs_node *
VP_TO_TMPFS_NODE(struct vnode * vp)626d1fa59e9SXin LI VP_TO_TMPFS_NODE(struct vnode *vp)
627d1fa59e9SXin LI {
628d1fa59e9SXin LI struct tmpfs_node *node;
629d1fa59e9SXin LI
630bba7ed20SKonstantin Belousov MPASS(vp != NULL && vp->v_data != NULL);
631d1fa59e9SXin LI node = (struct tmpfs_node *)vp->v_data;
632bba7ed20SKonstantin Belousov return (node);
633d1fa59e9SXin LI }
634d1fa59e9SXin LI
635172ffe70SMateusz Guzik #define VP_TO_TMPFS_NODE_SMR(vp) \
636172ffe70SMateusz Guzik ((struct tmpfs_node *)vn_load_v_data_smr(vp))
637172ffe70SMateusz Guzik
638bba7ed20SKonstantin Belousov static inline struct tmpfs_node *
VP_TO_TMPFS_DIR(struct vnode * vp)639d1fa59e9SXin LI VP_TO_TMPFS_DIR(struct vnode *vp)
640d1fa59e9SXin LI {
641d1fa59e9SXin LI struct tmpfs_node *node;
642d1fa59e9SXin LI
643d1fa59e9SXin LI node = VP_TO_TMPFS_NODE(vp);
644d1fa59e9SXin LI TMPFS_VALIDATE_DIR(node);
645bba7ed20SKonstantin Belousov return (node);
646d1fa59e9SXin LI }
647d1fa59e9SXin LI
64800ac6a98SKonstantin Belousov static inline bool
tmpfs_use_nc(struct vnode * vp)64900ac6a98SKonstantin Belousov tmpfs_use_nc(struct vnode *vp)
65000ac6a98SKonstantin Belousov {
65100ac6a98SKonstantin Belousov
65200ac6a98SKonstantin Belousov return (!(VFS_TO_TMPFS(vp->v_mount)->tm_nonc));
65300ac6a98SKonstantin Belousov }
6542abdae33SMateusz Guzik
6552abdae33SMateusz Guzik static inline void
tmpfs_update_getattr(struct vnode * vp)6562abdae33SMateusz Guzik tmpfs_update_getattr(struct vnode *vp)
6572abdae33SMateusz Guzik {
6582abdae33SMateusz Guzik struct tmpfs_node *node;
6592abdae33SMateusz Guzik
6602abdae33SMateusz Guzik node = VP_TO_TMPFS_NODE(vp);
661016b7c7eSKonstantin Belousov if (__predict_false((node->tn_status & (TMPFS_NODE_MODIFIED |
662016b7c7eSKonstantin Belousov TMPFS_NODE_CHANGED)) != 0 || node->tn_accessed))
6632abdae33SMateusz Guzik tmpfs_update(vp);
6642abdae33SMateusz Guzik }
6652abdae33SMateusz Guzik
666081e36e7SKonstantin Belousov extern struct fileops tmpfs_fnops;
667081e36e7SKonstantin Belousov
668f9f4c60aSDoug Moore #endif /* _KERNEL */
66900ac6a98SKonstantin Belousov
670d1fa59e9SXin LI #endif /* _FS_TMPFS_TMPFS_H_ */
671