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