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