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