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