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