1 /* $NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $ */
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 * $FreeBSD: src/sys/fs/tmpfs/tmpfs.h,v 1.18 2009/10/11 07:03:56 delphij Exp $
35 #ifndef _VFS_TMPFS_TMPFS_H_
36 #define _VFS_TMPFS_TMPFS_H_
38 /* ---------------------------------------------------------------------
39 * KERNEL-SPECIFIC DEFINITIONS
40 * --------------------------------------------------------------------- */
41 #include <sys/dirent.h>
42 #include <sys/mount.h>
44 #include <sys/vnode.h>
47 #include <sys/lockf.h>
48 #include <sys/mutex.h>
49 #include <sys/objcache.h>
51 /* --------------------------------------------------------------------- */
52 #include <sys/malloc.h>
53 #include <sys/systm.h>
54 #include <sys/vmmeter.h>
55 #include <vm/swap_pager.h>
57 MALLOC_DECLARE(M_TMPFSMNT);
59 /* --------------------------------------------------------------------- */
62 * Internal representation of a tmpfs directory entry.
65 RB_ENTRY(tmpfs_dirent) rb_node;
67 /* Length of the name stored in this directory entry. This avoids
68 * the need to recalculate it every time the name is used. */
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. */
76 /* Pointer to the node this entry refers to. */
77 struct tmpfs_node * td_node;
81 RB_HEAD(tmpfs_dirtree, tmpfs_dirent);
82 RB_PROTOTYPE(tmpfs_dirtree, tmpfs_dirent, rb_node,
83 tmpfs_dirtree_compare);
86 /* A directory in tmpfs holds a set of directory entries, which in
87 * turn point to other files (which can be directories themselves).
89 * In tmpfs, this set is managed by a red-black tree, whose root is defined
90 * by the struct tmpfs_dirtree type.
92 * It is important to notice that directories do not have entries for . and
93 * .. as other file systems do. These can be generated when requested
94 * based on information available by other means, such as the pointer to
95 * the node itself in the former case or the pointer to the parent directory
96 * in the latter case. This is done to simplify tmpfs's code and, more
97 * importantly, to remove redundancy. */
99 /* Each entry in a directory has a cookie that identifies it. Cookies
100 * supersede offsets within directories because, given how tmpfs stores
101 * directories in memory, there is no such thing as an offset. (Emulating
102 * a real offset could be very difficult.)
104 * The '.', '..' and the end of directory markers have fixed cookies which
105 * cannot collide with the cookies generated by other entries. The cookies
106 * for the other entries are generated based on the memory address on which
107 * stores their information is stored.
109 * Ideally, using the entry's memory pointer as the cookie would be enough
110 * to represent it and it wouldn't cause collisions in any system.
111 * Unfortunately, this results in "offsets" with very large values which
112 * later raise problems in the Linux compatibility layer (and maybe in other
113 * places) as described in PR kern/32034. Hence we need to workaround this
114 * with a rather ugly hack.
116 * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t
117 * set to 'long', which is a 32-bit *signed* long integer. Regardless of
118 * the macro value, GLIBC (2.3 at least) always uses the getdents64
119 * system call (when calling readdir) which internally returns off64_t
120 * offsets. In order to make 32-bit binaries work, *GLIBC* converts the
121 * 64-bit values returned by the kernel to 32-bit ones and aborts with
122 * EOVERFLOW if the conversion results in values that won't fit in 32-bit
123 * integers (which it assumes is because the directory is extremely large).
124 * This wouldn't cause problems if we were dealing with unsigned integers,
125 * but as we have signed integers, this check fails due to sign expansion.
127 * For example, consider that the kernel returns the 0xc1234567 cookie to
128 * userspace in a off64_t integer. Later on, GLIBC casts this value to
129 * off_t (remember, signed) with code similar to:
130 * system call returns the offset in kernel_value;
131 * off_t casted_value = kernel_value;
132 * if (sizeof(off_t) != sizeof(off64_t) &&
133 * kernel_value != casted_value)
135 * In this case, casted_value still has 0xc1234567, but when it is compared
136 * for equality against kernel_value, it is promoted to a 64-bit integer and
137 * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567.
138 * Then, GLIBC assumes this is because the directory is very large.
140 * Given that all the above happens in user-space, we have no control over
141 * it; therefore we must workaround the issue here. We do this by
142 * truncating the pointer value to a 32-bit integer and hope that there
143 * won't be collisions. In fact, this will not cause any problems in
144 * 32-bit platforms but some might arise in 64-bit machines (I'm not sure
145 * if they can happen at all in practice).
147 * XXX A nicer solution shall be attempted. */
149 #define TMPFS_DIRCOOKIE_DOT 0
150 #define TMPFS_DIRCOOKIE_DOTDOT 1
151 #define TMPFS_DIRCOOKIE_EOF 2
154 tmpfs_dircookie(struct tmpfs_dirent *de)
158 cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF;
159 KKASSERT(cookie != TMPFS_DIRCOOKIE_DOT);
160 KKASSERT(cookie != TMPFS_DIRCOOKIE_DOTDOT);
161 KKASSERT(cookie != TMPFS_DIRCOOKIE_EOF);
167 /* --------------------------------------------------------------------- */
170 * Internal representation of a tmpfs file system node.
172 * This structure is splitted in two parts: one holds attributes common
173 * to all file types and the other holds data that is only applicable to
174 * a particular type. The code must be careful to only access those
175 * attributes that are actually allowed by the node's type.
178 * Below is the key of locks used to protected the fields in the following
183 /* Doubly-linked list entry which links all existing nodes for a
184 * single file system. This is provided to ease the removal of
185 * all nodes during the unmount operation. */
186 LIST_ENTRY(tmpfs_node) tn_entries;
188 /* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
189 * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode
190 * types instead of a custom enumeration is to make things simpler
191 * and faster, as we do not need to convert between two types. */
194 /* Node identifier. */
197 /* Node's internal status. This is used by several file system
198 * operations to do modifications to the node in a delayed
201 #define TMPFS_NODE_ACCESSED (1 << 1)
202 #define TMPFS_NODE_MODIFIED (1 << 2)
203 #define TMPFS_NODE_CHANGED (1 << 3)
205 /* The node size. It does not necessarily match the real amount
206 * of memory consumed by it. */
209 /* Generic node attributes. */
216 int32_t tn_atimensec;
218 int32_t tn_mtimensec;
220 int32_t tn_ctimensec;
221 unsigned long tn_gen;
222 struct lockf tn_advlock;
224 /* As there is a single vnode for each active file within the
225 * system, care has to be taken to avoid allocating more than one
226 * vnode per file. In order to do this, a bidirectional association
227 * is kept between vnodes and nodes.
229 * Whenever a vnode is allocated, its v_data field is updated to
230 * point to the node it references. At the same time, the node's
231 * tn_vnode field is modified to point to the new vnode representing
232 * it. Further attempts to allocate a vnode for this same node will
233 * result in returning a new reference to the value stored in
236 * May be NULL when the node is unused (that is, no vnode has been
237 * allocated for it or it has been reclaimed). */
238 struct vnode * tn_vnode;
240 /* interlock to protect tn_vpstate */
241 struct lock tn_interlock;
243 /* Identify if current node has vnode assiocate with
244 * or allocating vnode.
248 /* misc data field for different tn_type node */
250 /* Valid when tn_type == VBLK || tn_type == VCHR. */
251 dev_t tn_rdev; /*int32_t ?*/
253 /* Valid when tn_type == VDIR. */
255 /* Pointer to the parent directory. The root
256 * directory has a pointer to itself in this field;
257 * this property identifies the root node. */
258 struct tmpfs_node * tn_parent;
260 /* Root of a red-black tree that links the contents of
261 * the directory together. See above for a
262 * description of its contents. */
263 struct tmpfs_dirtree tn_dirtree;
265 /* Number and pointer of the first directory entry
266 * returned by the readdir operation if it were
267 * called again to continue reading data from the
268 * same directory as before. This is used to speed
269 * up reads of long directories, assuming that no
270 * more than one read is in progress at a given time.
271 * Otherwise, these values are discarded and a linear
272 * scan is performed from the beginning up to the
273 * point where readdir starts returning values. */
274 off_t tn_readdir_lastn;
275 struct tmpfs_dirent * tn_readdir_lastp;
278 /* Valid when tn_type == VLNK. */
279 /* The link's target, allocated from a string pool. */
282 /* Valid when tn_type == VREG. */
284 /* The contents of regular files stored in a tmpfs
285 * file system are represented by a single anonymous
286 * memory object (aobj, for short). The aobj provides
287 * direct access to any position within the file,
288 * because its contents are always mapped in a
289 * contiguous region of virtual memory. It is a task
290 * of the memory management subsystem (see uvm(9)) to
291 * issue the required page ins or page outs whenever
292 * a position within the file is accessed. */
294 size_t tn_aobj_pages;
298 /* Valid when tn_type = VFIFO */
300 int (*tn_fo_read) (struct file *fp, struct uio *uio,
301 struct ucred *cred, int flags);
302 int (*tn_fo_write) (struct file *fp, struct uio *uio,
303 struct ucred *cred, int flags);
307 LIST_HEAD(tmpfs_node_list, tmpfs_node);
309 #define tn_rdev tn_spec.tn_rdev
310 #define tn_dir tn_spec.tn_dir
311 #define tn_link tn_spec.tn_link
312 #define tn_reg tn_spec.tn_reg
313 #define tn_fifo tn_spec.tn_fifo
315 #define TMPFS_NODE_LOCK(node) lockmgr(&(node)->tn_interlock, LK_EXCLUSIVE|LK_RETRY)
316 #define TMPFS_NODE_UNLOCK(node) lockmgr(&(node)->tn_interlock, LK_RELEASE)
317 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
320 #define TMPFS_ASSERT_LOCKED(node) do { \
321 KKASSERT(node != NULL); \
322 KKASSERT(node->tn_vnode != NULL); \
323 if (!vn_islocked(node->tn_vnode) && \
324 (lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE )) \
325 panic("tmpfs: node is not locked: %p", node); \
327 #define TMPFS_ASSERT_ELOCKED(node) do { \
328 KKASSERT((node) != NULL); \
329 KKASSERT(lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE); \
332 #define TMPFS_ASSERT_LOCKED(node) (void)0
333 #define TMPFS_ASSERT_ELOCKED(node) (void)0
336 #define TMPFS_VNODE_ALLOCATING 1
337 #define TMPFS_VNODE_WANT 2
338 #define TMPFS_VNODE_DOOMED 4
339 /* --------------------------------------------------------------------- */
342 * Internal representation of a tmpfs mount point.
345 /* Maximum number of memory pages available for use by the file
346 * system, set during mount time. This variable must never be
347 * used directly as it may be bigger than the current amount of
348 * free memory; in the extreme case, it will hold the SIZE_MAX
349 * value. Instead, use the TMPFS_PAGES_MAX macro. */
350 vm_pindex_t tm_pages_max;
352 /* Number of pages in use by the file system. Cannot be bigger
353 * than the value returned by TMPFS_PAGES_MAX in any case. */
354 vm_pindex_t tm_pages_used;
356 /* Pointer to the node representing the root directory of this
358 struct tmpfs_node * tm_root;
360 /* Maximum number of possible nodes for this file system; set
361 * during mount time. We need a hard limit on the maximum number
362 * of nodes to avoid allocating too much of them; their objects
363 * cannot be released until the file system is unmounted.
364 * Otherwise, we could easily run out of memory by creating lots
365 * of empty files and then simply removing them. */
368 /* Number of nodes currently that are in use. */
369 ino_t tm_nodes_inuse;
371 /* maximum representable file size */
372 u_int64_t tm_maxfilesize;
374 /* Nodes are organized in two different lists. The used list
375 * contains all nodes that are currently used by the file system;
376 * i.e., they refer to existing files. The available list contains
377 * all nodes that are currently available for use by new files.
378 * Nodes must be kept in this list (instead of deleting them)
379 * because we need to keep track of their generation number (tn_gen
382 * Note that nodes are lazily allocated: if the available list is
383 * empty and we have enough space to create more nodes, they will be
384 * created and inserted in the used list. Once these are released,
385 * they will go into the available list, remaining alive until the
386 * file system is unmounted. */
387 struct tmpfs_node_list tm_nodes_used;
389 /* All node lock to protect the node list and tmp_pages_used */
390 struct lock allnode_lock;
392 /* Per-mount malloc zones for tmpfs nodes, names, and dirents */
393 struct malloc_type *tm_node_zone;
394 struct malloc_type *tm_dirent_zone;
395 struct malloc_type *tm_name_zone;
397 struct objcache_malloc_args tm_node_zone_malloc_args;
398 struct objcache_malloc_args tm_dirent_zone_malloc_args;
400 /* Pools used to store file system meta data. These are not shared
401 * across several instances of tmpfs for the reasons described in
403 struct objcache *tm_dirent_pool;
404 struct objcache *tm_node_pool;
409 struct netexport tm_export;
412 #define TMPFS_LOCK(tm) lockmgr(&(tm)->allnode_lock, LK_EXCLUSIVE|LK_RETRY)
413 #define TMPFS_UNLOCK(tm) lockmgr(&(tm)->allnode_lock, LK_RELEASE)
415 /* --------------------------------------------------------------------- */
418 * This structure maps a file identifier to a tmpfs node. Used by the
425 unsigned long tf_gen;
428 /* --------------------------------------------------------------------- */
432 * Prototypes for tmpfs_subr.c.
435 int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
436 uid_t uid, gid_t gid, mode_t mode, char *, int, int,
437 struct tmpfs_node **);
438 void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
439 int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
440 const char *, uint16_t, struct tmpfs_dirent **);
441 void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
442 int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
444 void tmpfs_free_vp(struct vnode *);
445 int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
446 struct namecache *, struct ucred *, char *);
447 void tmpfs_dir_attach(struct tmpfs_node *, struct tmpfs_dirent *);
448 void tmpfs_dir_detach(struct tmpfs_node *, struct tmpfs_dirent *);
449 struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node,
450 struct tmpfs_node *f,
451 struct namecache *ncp);
452 int tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
453 int tmpfs_dir_getdotdotdent(struct tmpfs_mount *,
454 struct tmpfs_node *, struct uio *);
455 struct tmpfs_dirent * tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
456 int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
457 int tmpfs_reg_resize(struct vnode *, off_t, int);
458 int tmpfs_chflags(struct vnode *, int, struct ucred *);
459 int tmpfs_chmod(struct vnode *, mode_t, struct ucred *);
460 int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *);
461 int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *);
462 int tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
463 int, struct ucred *);
464 void tmpfs_itimes(struct vnode *, const struct timespec *,
465 const struct timespec *);
467 void tmpfs_update(struct vnode *);
468 int tmpfs_truncate(struct vnode *, off_t);
469 int tmpfs_node_ctor(void *obj, void *privdata, int flags);
471 /* --------------------------------------------------------------------- */
474 * Convenience macros to simplify some logical expressions.
476 #define IMPLIES(a, b) (!(a) || (b))
477 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
479 /* --------------------------------------------------------------------- */
482 * Checks that the directory entry pointed by 'de' matches the name 'name'
483 * with a length of 'len'.
485 #define TMPFS_DIRENT_MATCHES(de, name, len) \
486 (de->td_namelen == (uint16_t)len && \
487 bcmp((de)->td_name, (name), (de)->td_namelen) == 0)
489 /* --------------------------------------------------------------------- */
492 * Ensures that the node pointed by 'node' is a directory and that its
493 * contents are consistent with respect to directories.
495 #define TMPFS_VALIDATE_DIR(node) \
496 KKASSERT((node)->tn_type == VDIR); \
497 KKASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
498 KKASSERT((node)->tn_dir.tn_readdir_lastp == NULL || \
499 tmpfs_dircookie((node)->tn_dir.tn_readdir_lastp) == (node)->tn_dir.tn_readdir_lastn);
503 /* --------------------------------------------------------------------- */
506 * Macros/functions to convert from generic data structures to tmpfs
512 VFS_TO_TMPFS(struct mount *mp)
514 struct tmpfs_mount *tmp;
516 KKASSERT((mp) != NULL && (mp)->mnt_data != NULL);
517 tmp = (struct tmpfs_mount *)(mp)->mnt_data;
523 VP_TO_TMPFS_NODE(struct vnode *vp)
525 struct tmpfs_node *node;
527 KKASSERT((vp) != NULL && (vp)->v_data != NULL);
528 node = (struct tmpfs_node *)vp->v_data;
534 VP_TO_TMPFS_DIR(struct vnode *vp)
536 struct tmpfs_node *node;
538 node = VP_TO_TMPFS_NODE(vp);
539 TMPFS_VALIDATE_DIR(node);
543 /* --------------------------------------------------------------------- */
547 #define BSIZE (off_t)16384 /* buffer cache size*/
548 #define BMASK (off_t)(BSIZE - 1)
550 #endif /* _VFS_TMPFS_TMPFS_H_ */