tmpfs.h revision 1.37.14.1 1 1.37.14.1 uebayasi /* $NetBSD: tmpfs.h,v 1.37.14.1 2010/08/17 06:47:21 uebayasi Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.30 ad * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * This code is derived from software contributed to The NetBSD Foundation
8 1.6 jmmv * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 1.6 jmmv * 2005 program.
10 1.1 jmmv *
11 1.1 jmmv * Redistribution and use in source and binary forms, with or without
12 1.1 jmmv * modification, are permitted provided that the following conditions
13 1.1 jmmv * are met:
14 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
15 1.1 jmmv * notice, this list of conditions and the following disclaimer.
16 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
18 1.1 jmmv * documentation and/or other materials provided with the distribution.
19 1.1 jmmv *
20 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
31 1.1 jmmv */
32 1.1 jmmv
33 1.10 christos #ifndef _FS_TMPFS_TMPFS_H_
34 1.10 christos #define _FS_TMPFS_TMPFS_H_
35 1.1 jmmv
36 1.1 jmmv #include <sys/dirent.h>
37 1.1 jmmv #include <sys/mount.h>
38 1.37.14.1 uebayasi #include <sys/pool.h>
39 1.1 jmmv #include <sys/queue.h>
40 1.1 jmmv #include <sys/vnode.h>
41 1.1 jmmv
42 1.37 pooka /*
43 1.37 pooka * Internal representation of a tmpfs directory entry.
44 1.37 pooka */
45 1.37 pooka struct tmpfs_dirent {
46 1.37 pooka TAILQ_ENTRY(tmpfs_dirent) td_entries;
47 1.37 pooka
48 1.37 pooka /* Length of the name stored in this directory entry. This avoids
49 1.37 pooka * the need to recalculate it every time the name is used. */
50 1.37 pooka uint16_t td_namelen;
51 1.37 pooka
52 1.37 pooka /* The name of the entry, allocated from a string pool. This
53 1.37 pooka * string is not required to be zero-terminated; therefore, the
54 1.37 pooka * td_namelen field must always be used when accessing its value. */
55 1.37 pooka char * td_name;
56 1.37 pooka
57 1.37 pooka /* Pointer to the node this entry refers to. */
58 1.37 pooka struct tmpfs_node * td_node;
59 1.37 pooka };
60 1.37 pooka
61 1.37 pooka /* A directory in tmpfs holds a sorted list of directory entries, which in
62 1.37 pooka * turn point to other files (which can be directories themselves).
63 1.37 pooka *
64 1.37 pooka * In tmpfs, this list is managed by a tail queue, whose head is defined by
65 1.37 pooka * the struct tmpfs_dir type.
66 1.37 pooka *
67 1.37 pooka * It is imporant to notice that directories do not have entries for . and
68 1.37 pooka * .. as other file systems do. These can be generated when requested
69 1.37 pooka * based on information available by other means, such as the pointer to
70 1.37 pooka * the node itself in the former case or the pointer to the parent directory
71 1.37 pooka * in the latter case. This is done to simplify tmpfs's code and, more
72 1.37 pooka * importantly, to remove redundancy. */
73 1.37 pooka TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
74 1.37 pooka
75 1.22 jmmv /* Each entry in a directory has a cookie that identifies it. Cookies
76 1.22 jmmv * supersede offsets within directories because, given how tmpfs stores
77 1.22 jmmv * directories in memory, there is no such thing as an offset. (Emulating
78 1.22 jmmv * a real offset could be very difficult.)
79 1.31 jmmv *
80 1.22 jmmv * The '.', '..' and the end of directory markers have fixed cookies which
81 1.22 jmmv * cannot collide with the cookies generated by other entries. The cookies
82 1.22 jmmv * fot the other entries are generated based on the memory address on which
83 1.22 jmmv * stores their information is stored.
84 1.22 jmmv *
85 1.22 jmmv * Ideally, using the entry's memory pointer as the cookie would be enough
86 1.22 jmmv * to represent it and it wouldn't cause collisions in any system.
87 1.22 jmmv * Unfortunately, this results in "offsets" with very large values which
88 1.22 jmmv * later raise problems in the Linux compatibility layer (and maybe in other
89 1.22 jmmv * places) as described in PR kern/32034. Hence we need to workaround this
90 1.22 jmmv * with a rather ugly hack.
91 1.22 jmmv *
92 1.22 jmmv * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t
93 1.22 jmmv * set to 'long', which is a 32-bit *signed* long integer. Regardless of
94 1.22 jmmv * the macro value, GLIBC (2.3 at least) always uses the getdents64
95 1.22 jmmv * system call (when calling readdir) which internally returns off64_t
96 1.22 jmmv * offsets. In order to make 32-bit binaries work, *GLIBC* converts the
97 1.22 jmmv * 64-bit values returned by the kernel to 32-bit ones and aborts with
98 1.22 jmmv * EOVERFLOW if the conversion results in values that won't fit in 32-bit
99 1.22 jmmv * integers (which it assumes is because the directory is extremely large).
100 1.22 jmmv * This wouldn't cause problems if we were dealing with unsigned integers,
101 1.22 jmmv * but as we have signed integers, this check fails due to sign expansion.
102 1.22 jmmv *
103 1.22 jmmv * For example, consider that the kernel returns the 0xc1234567 cookie to
104 1.22 jmmv * userspace in a off64_t integer. Later on, GLIBC casts this value to
105 1.22 jmmv * off_t (remember, signed) with code similar to:
106 1.22 jmmv * system call returns the offset in kernel_value;
107 1.22 jmmv * off_t casted_value = kernel_value;
108 1.22 jmmv * if (sizeof(off_t) != sizeof(off64_t) &&
109 1.22 jmmv * kernel_value != casted_value)
110 1.22 jmmv * error!
111 1.22 jmmv * In this case, casted_value still has 0xc1234567, but when it is compared
112 1.22 jmmv * for equality against kernel_value, it is promoted to a 64-bit integer and
113 1.22 jmmv * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567.
114 1.22 jmmv * Then, GLIBC assumes this is because the directory is very large.
115 1.22 jmmv *
116 1.22 jmmv * Given that all the above happens in user-space, we have no control over
117 1.22 jmmv * it; therefore we must workaround the issue here. We do this by
118 1.22 jmmv * truncating the pointer value to a 32-bit integer and hope that there
119 1.22 jmmv * won't be collisions. In fact, this will not cause any problems in
120 1.22 jmmv * 32-bit platforms but some might arise in 64-bit machines (I'm not sure
121 1.22 jmmv * if they can happen at all in practice).
122 1.22 jmmv *
123 1.22 jmmv * XXX A nicer solution shall be attempted. */
124 1.37 pooka #if defined(_KERNEL)
125 1.4 yamt #define TMPFS_DIRCOOKIE_DOT 0
126 1.4 yamt #define TMPFS_DIRCOOKIE_DOTDOT 1
127 1.4 yamt #define TMPFS_DIRCOOKIE_EOF 2
128 1.22 jmmv static __inline
129 1.22 jmmv off_t
130 1.22 jmmv tmpfs_dircookie(struct tmpfs_dirent *de)
131 1.22 jmmv {
132 1.22 jmmv off_t cookie;
133 1.22 jmmv
134 1.22 jmmv cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF;
135 1.22 jmmv KASSERT(cookie != TMPFS_DIRCOOKIE_DOT);
136 1.22 jmmv KASSERT(cookie != TMPFS_DIRCOOKIE_DOTDOT);
137 1.22 jmmv KASSERT(cookie != TMPFS_DIRCOOKIE_EOF);
138 1.22 jmmv
139 1.22 jmmv return cookie;
140 1.22 jmmv }
141 1.37 pooka #endif /* defined(_KERNEL) */
142 1.4 yamt
143 1.1 jmmv /* --------------------------------------------------------------------- */
144 1.1 jmmv
145 1.37 pooka /*
146 1.37 pooka * Internal representation of a tmpfs file system node.
147 1.37 pooka *
148 1.37 pooka * This structure is splitted in two parts: one holds attributes common
149 1.37 pooka * to all file types and the other holds data that is only applicable to
150 1.37 pooka * a particular type. The code must be careful to only access those
151 1.37 pooka * attributes that are actually allowed by the node's type.
152 1.37 pooka */
153 1.37 pooka struct tmpfs_node {
154 1.37 pooka /* Doubly-linked list entry which links all existing nodes for a
155 1.37 pooka * single file system. This is provided to ease the removal of
156 1.37 pooka * all nodes during the unmount operation. */
157 1.37 pooka LIST_ENTRY(tmpfs_node) tn_entries;
158 1.37 pooka
159 1.37 pooka /* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
160 1.37 pooka * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode
161 1.37 pooka * types instead of a custom enumeration is to make things simpler
162 1.37 pooka * and faster, as we do not need to convert between two types. */
163 1.37 pooka enum vtype tn_type;
164 1.37 pooka
165 1.37 pooka /* Node identifier. */
166 1.37 pooka ino_t tn_id;
167 1.37 pooka
168 1.37 pooka /* Node's internal status. This is used by several file system
169 1.37 pooka * operations to do modifications to the node in a delayed
170 1.37 pooka * fashion. */
171 1.37 pooka int tn_status;
172 1.37 pooka #define TMPFS_NODE_ACCESSED (1 << 1)
173 1.37 pooka #define TMPFS_NODE_MODIFIED (1 << 2)
174 1.37 pooka #define TMPFS_NODE_CHANGED (1 << 3)
175 1.37 pooka
176 1.37 pooka /* The node size. It does not necessarily match the real amount
177 1.37 pooka * of memory consumed by it. */
178 1.37 pooka off_t tn_size;
179 1.37 pooka
180 1.37 pooka /* Generic node attributes. */
181 1.37 pooka uid_t tn_uid;
182 1.37 pooka gid_t tn_gid;
183 1.37 pooka mode_t tn_mode;
184 1.37 pooka int tn_flags;
185 1.37 pooka nlink_t tn_links;
186 1.37 pooka struct timespec tn_atime;
187 1.37 pooka struct timespec tn_mtime;
188 1.37 pooka struct timespec tn_ctime;
189 1.37 pooka struct timespec tn_birthtime;
190 1.37 pooka unsigned long tn_gen;
191 1.37 pooka
192 1.37 pooka /* Head of byte-level lock list (used by tmpfs_advlock). */
193 1.37 pooka struct lockf * tn_lockf;
194 1.37 pooka
195 1.37 pooka /* As there is a single vnode for each active file within the
196 1.37 pooka * system, care has to be taken to avoid allocating more than one
197 1.37 pooka * vnode per file. In order to do this, a bidirectional association
198 1.37 pooka * is kept between vnodes and nodes.
199 1.37 pooka *
200 1.37 pooka * Whenever a vnode is allocated, its v_data field is updated to
201 1.37 pooka * point to the node it references. At the same time, the node's
202 1.37 pooka * tn_vnode field is modified to point to the new vnode representing
203 1.37 pooka * it. Further attempts to allocate a vnode for this same node will
204 1.37 pooka * result in returning a new reference to the value stored in
205 1.37 pooka * tn_vnode.
206 1.37 pooka *
207 1.37 pooka * May be NULL when the node is unused (that is, no vnode has been
208 1.37 pooka * allocated for it or it has been reclaimed). */
209 1.37 pooka kmutex_t tn_vlock;
210 1.37 pooka struct vnode * tn_vnode;
211 1.37 pooka
212 1.37 pooka union {
213 1.37 pooka /* Valid when tn_type == VBLK || tn_type == VCHR. */
214 1.37 pooka struct {
215 1.37 pooka dev_t tn_rdev;
216 1.37 pooka } tn_dev;
217 1.37 pooka
218 1.37 pooka /* Valid when tn_type == VDIR. */
219 1.37 pooka struct {
220 1.37 pooka /* Pointer to the parent directory. The root
221 1.37 pooka * directory has a pointer to itself in this field;
222 1.37 pooka * this property identifies the root node. */
223 1.37 pooka struct tmpfs_node * tn_parent;
224 1.37 pooka
225 1.37 pooka /* Head of a tail-queue that links the contents of
226 1.37 pooka * the directory together. See above for a
227 1.37 pooka * description of its contents. */
228 1.37 pooka struct tmpfs_dir tn_dir;
229 1.37 pooka
230 1.37 pooka /* Number and pointer of the first directory entry
231 1.37 pooka * returned by the readdir operation if it were
232 1.37 pooka * called again to continue reading data from the
233 1.37 pooka * same directory as before. This is used to speed
234 1.37 pooka * up reads of long directories, assuming that no
235 1.37 pooka * more than one read is in progress at a given time.
236 1.37 pooka * Otherwise, these values are discarded and a linear
237 1.37 pooka * scan is performed from the beginning up to the
238 1.37 pooka * point where readdir starts returning values. */
239 1.37 pooka off_t tn_readdir_lastn;
240 1.37 pooka struct tmpfs_dirent * tn_readdir_lastp;
241 1.37 pooka } tn_dir;
242 1.37 pooka
243 1.37 pooka /* Valid when tn_type == VLNK. */
244 1.37 pooka struct tn_lnk {
245 1.37 pooka /* The link's target, allocated from a string pool. */
246 1.37 pooka char * tn_link;
247 1.37 pooka } tn_lnk;
248 1.37 pooka
249 1.37 pooka /* Valid when tn_type == VREG. */
250 1.37 pooka struct tn_reg {
251 1.37 pooka /* The contents of regular files stored in a tmpfs
252 1.37 pooka * file system are represented by a single anonymous
253 1.37 pooka * memory object (aobj, for short). The aobj provides
254 1.37 pooka * direct access to any position within the file,
255 1.37 pooka * because its contents are always mapped in a
256 1.37 pooka * contiguous region of virtual memory. It is a task
257 1.37 pooka * of the memory management subsystem (see uvm(9)) to
258 1.37 pooka * issue the required page ins or page outs whenever
259 1.37 pooka * a position within the file is accessed. */
260 1.37 pooka struct uvm_object * tn_aobj;
261 1.37 pooka size_t tn_aobj_pages;
262 1.37 pooka } tn_reg;
263 1.37 pooka } tn_spec;
264 1.37 pooka };
265 1.37 pooka
266 1.37 pooka #if defined(_KERNEL)
267 1.1 jmmv LIST_HEAD(tmpfs_node_list, tmpfs_node);
268 1.1 jmmv
269 1.1 jmmv /* --------------------------------------------------------------------- */
270 1.1 jmmv
271 1.1 jmmv /*
272 1.6 jmmv * Internal representation of a tmpfs mount point.
273 1.1 jmmv */
274 1.1 jmmv struct tmpfs_mount {
275 1.37.14.1 uebayasi /* Limit and number of bytes in use by the file system. */
276 1.37.14.1 uebayasi uint64_t tm_mem_limit;
277 1.37.14.1 uebayasi uint64_t tm_bytes_used;
278 1.37.14.1 uebayasi kmutex_t tm_acc_lock;
279 1.1 jmmv
280 1.6 jmmv /* Pointer to the node representing the root directory of this
281 1.6 jmmv * file system. */
282 1.1 jmmv struct tmpfs_node * tm_root;
283 1.1 jmmv
284 1.6 jmmv /* Maximum number of possible nodes for this file system; set
285 1.6 jmmv * during mount time. We need a hard limit on the maximum number
286 1.6 jmmv * of nodes to avoid allocating too much of them; their objects
287 1.6 jmmv * cannot be released until the file system is unmounted.
288 1.6 jmmv * Otherwise, we could easily run out of memory by creating lots
289 1.6 jmmv * of empty files and then simply removing them. */
290 1.32 jmmv unsigned int tm_nodes_max;
291 1.6 jmmv
292 1.6 jmmv /* Number of nodes currently allocated. This number only grows.
293 1.6 jmmv * When it reaches tm_nodes_max, no more new nodes can be allocated.
294 1.6 jmmv * Of course, the old, unused ones can be reused. */
295 1.32 jmmv unsigned int tm_nodes_cnt;
296 1.6 jmmv
297 1.30 ad /* Node list. */
298 1.30 ad kmutex_t tm_lock;
299 1.30 ad struct tmpfs_node_list tm_nodes;
300 1.1 jmmv
301 1.37.14.1 uebayasi char tm_dwchan[32];
302 1.37.14.1 uebayasi struct pool tm_dirent_pool;
303 1.37.14.1 uebayasi char tm_nwchan[32];
304 1.37.14.1 uebayasi struct pool tm_node_pool;
305 1.1 jmmv };
306 1.1 jmmv
307 1.1 jmmv /* --------------------------------------------------------------------- */
308 1.1 jmmv
309 1.1 jmmv /*
310 1.1 jmmv * This structure maps a file identifier to a tmpfs node. Used by the
311 1.1 jmmv * NFS code.
312 1.1 jmmv */
313 1.1 jmmv struct tmpfs_fid {
314 1.1 jmmv uint16_t tf_len;
315 1.1 jmmv uint16_t tf_pad;
316 1.18 riz uint32_t tf_gen;
317 1.1 jmmv ino_t tf_id;
318 1.1 jmmv };
319 1.1 jmmv
320 1.1 jmmv /* --------------------------------------------------------------------- */
321 1.1 jmmv
322 1.1 jmmv /*
323 1.1 jmmv * Prototypes for tmpfs_subr.c.
324 1.1 jmmv */
325 1.1 jmmv
326 1.1 jmmv int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
327 1.1 jmmv uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
328 1.29 pooka char *, dev_t, struct tmpfs_node **);
329 1.1 jmmv void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
330 1.1 jmmv int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
331 1.1 jmmv const char *, uint16_t, struct tmpfs_dirent **);
332 1.1 jmmv void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
333 1.25 thorpej bool);
334 1.1 jmmv int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **);
335 1.1 jmmv void tmpfs_free_vp(struct vnode *);
336 1.1 jmmv int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
337 1.1 jmmv struct componentname *, char *);
338 1.1 jmmv void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
339 1.1 jmmv void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
340 1.1 jmmv struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node,
341 1.1 jmmv struct componentname *cnp);
342 1.1 jmmv int tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
343 1.1 jmmv int tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
344 1.4 yamt struct tmpfs_dirent * tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
345 1.4 yamt int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
346 1.1 jmmv int tmpfs_reg_resize(struct vnode *, off_t);
347 1.21 ad int tmpfs_chflags(struct vnode *, int, kauth_cred_t, struct lwp *);
348 1.21 ad int tmpfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct lwp *);
349 1.21 ad int tmpfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, struct lwp *);
350 1.21 ad int tmpfs_chsize(struct vnode *, u_quad_t, kauth_cred_t, struct lwp *);
351 1.34 christos int tmpfs_chtimes(struct vnode *, const struct timespec *,
352 1.34 christos const struct timespec *, const struct timespec *, int, kauth_cred_t,
353 1.34 christos struct lwp *);
354 1.7 yamt void tmpfs_itimes(struct vnode *, const struct timespec *,
355 1.34 christos const struct timespec *, const struct timespec *);
356 1.1 jmmv
357 1.9 yamt void tmpfs_update(struct vnode *, const struct timespec *,
358 1.34 christos const struct timespec *, const struct timespec *, int);
359 1.9 yamt int tmpfs_truncate(struct vnode *, off_t);
360 1.9 yamt
361 1.37.14.1 uebayasi /*
362 1.37.14.1 uebayasi * Prototypes for tmpfs_mem.c.
363 1.37.14.1 uebayasi */
364 1.37.14.1 uebayasi
365 1.37.14.1 uebayasi void tmpfs_mntmem_init(struct tmpfs_mount *, uint64_t);
366 1.37.14.1 uebayasi void tmpfs_mntmem_destroy(struct tmpfs_mount *);
367 1.37.14.1 uebayasi
368 1.37.14.1 uebayasi size_t tmpfs_mem_info(bool);
369 1.37.14.1 uebayasi uint64_t tmpfs_bytes_max(struct tmpfs_mount *);
370 1.37.14.1 uebayasi size_t tmpfs_pages_avail(struct tmpfs_mount *);
371 1.37.14.1 uebayasi bool tmpfs_mem_incr(struct tmpfs_mount *, size_t);
372 1.37.14.1 uebayasi void tmpfs_mem_decr(struct tmpfs_mount *, size_t);
373 1.37.14.1 uebayasi
374 1.37.14.1 uebayasi struct tmpfs_dirent *tmpfs_dirent_get(struct tmpfs_mount *);
375 1.37.14.1 uebayasi void tmpfs_dirent_put(struct tmpfs_mount *, struct tmpfs_dirent *);
376 1.37.14.1 uebayasi
377 1.37.14.1 uebayasi struct tmpfs_node *tmpfs_node_get(struct tmpfs_mount *);
378 1.37.14.1 uebayasi void tmpfs_node_put(struct tmpfs_mount *, struct tmpfs_node *);
379 1.37.14.1 uebayasi
380 1.37.14.1 uebayasi char * tmpfs_strname_alloc(struct tmpfs_mount *, size_t);
381 1.37.14.1 uebayasi void tmpfs_strname_free(struct tmpfs_mount *, char *, size_t);
382 1.37.14.1 uebayasi bool tmpfs_strname_neqlen(struct componentname *, struct componentname *);
383 1.37.14.1 uebayasi
384 1.1 jmmv /* --------------------------------------------------------------------- */
385 1.1 jmmv
386 1.1 jmmv /*
387 1.1 jmmv * Convenience macros to simplify some logical expressions.
388 1.1 jmmv */
389 1.1 jmmv #define IMPLIES(a, b) (!(a) || (b))
390 1.1 jmmv #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
391 1.1 jmmv
392 1.1 jmmv /* --------------------------------------------------------------------- */
393 1.1 jmmv
394 1.1 jmmv /*
395 1.1 jmmv * Checks that the directory entry pointed by 'de' matches the name 'name'
396 1.1 jmmv * with a length of 'len'.
397 1.1 jmmv */
398 1.1 jmmv #define TMPFS_DIRENT_MATCHES(de, name, len) \
399 1.1 jmmv (de->td_namelen == (uint16_t)len && \
400 1.1 jmmv memcmp((de)->td_name, (name), (de)->td_namelen) == 0)
401 1.1 jmmv
402 1.1 jmmv /* --------------------------------------------------------------------- */
403 1.1 jmmv
404 1.1 jmmv /*
405 1.1 jmmv * Ensures that the node pointed by 'node' is a directory and that its
406 1.1 jmmv * contents are consistent with respect to directories.
407 1.1 jmmv */
408 1.1 jmmv #define TMPFS_VALIDATE_DIR(node) \
409 1.1 jmmv KASSERT((node)->tn_type == VDIR); \
410 1.4 yamt KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
411 1.15 jmmv KASSERT((node)->tn_spec.tn_dir.tn_readdir_lastp == NULL || \
412 1.22 jmmv tmpfs_dircookie((node)->tn_spec.tn_dir.tn_readdir_lastp) == \
413 1.15 jmmv (node)->tn_spec.tn_dir.tn_readdir_lastn);
414 1.1 jmmv
415 1.1 jmmv /* --------------------------------------------------------------------- */
416 1.1 jmmv
417 1.1 jmmv /*
418 1.1 jmmv * Memory management stuff.
419 1.1 jmmv */
420 1.1 jmmv
421 1.1 jmmv /* Amount of memory pages to reserve for the system (e.g., to not use by
422 1.1 jmmv * tmpfs).
423 1.1 jmmv * XXX: Should this be tunable through sysctl, for instance? */
424 1.1 jmmv #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
425 1.1 jmmv
426 1.1 jmmv /*
427 1.1 jmmv * Macros/functions to convert from generic data structures to tmpfs
428 1.1 jmmv * specific ones.
429 1.1 jmmv */
430 1.1 jmmv
431 1.16 perry static __inline
432 1.1 jmmv struct tmpfs_mount *
433 1.1 jmmv VFS_TO_TMPFS(struct mount *mp)
434 1.1 jmmv {
435 1.1 jmmv struct tmpfs_mount *tmp;
436 1.1 jmmv
437 1.14 christos #ifdef KASSERT
438 1.1 jmmv KASSERT((mp) != NULL && (mp)->mnt_data != NULL);
439 1.14 christos #endif
440 1.1 jmmv tmp = (struct tmpfs_mount *)(mp)->mnt_data;
441 1.1 jmmv return tmp;
442 1.1 jmmv }
443 1.1 jmmv
444 1.37 pooka #endif /* defined(_KERNEL) */
445 1.37 pooka
446 1.37 pooka static __inline
447 1.37 pooka struct tmpfs_node *
448 1.37 pooka VP_TO_TMPFS_NODE(struct vnode *vp)
449 1.37 pooka {
450 1.37 pooka struct tmpfs_node *node;
451 1.37 pooka
452 1.37 pooka #ifdef KASSERT
453 1.37 pooka KASSERT((vp) != NULL && (vp)->v_data != NULL);
454 1.37 pooka #endif
455 1.37 pooka node = (struct tmpfs_node *)vp->v_data;
456 1.37 pooka return node;
457 1.37 pooka }
458 1.37 pooka
459 1.37 pooka #if defined(_KERNEL)
460 1.37 pooka
461 1.16 perry static __inline
462 1.1 jmmv struct tmpfs_node *
463 1.1 jmmv VP_TO_TMPFS_DIR(struct vnode *vp)
464 1.1 jmmv {
465 1.1 jmmv struct tmpfs_node *node;
466 1.1 jmmv
467 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
468 1.14 christos #ifdef KASSERT
469 1.1 jmmv TMPFS_VALIDATE_DIR(node);
470 1.14 christos #endif
471 1.1 jmmv return node;
472 1.1 jmmv }
473 1.37 pooka
474 1.37 pooka #endif /* defined(_KERNEL) */
475 1.10 christos #endif /* _FS_TMPFS_TMPFS_H_ */
476