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>
43 #include <sys/queue.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 TAILQ_ENTRY(tmpfs_dirent) td_entries;
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;
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).
83 * In tmpfs, this list is managed by a tail queue, whose head is defined by
84 * the struct tmpfs_dir type.
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);
94 /* Each entry in a directory has a cookie that identifies it. Cookies
95 * supersede offsets within directories because, given how tmpfs stores
96 * directories in memory, there is no such thing as an offset. (Emulating
97 * a real offset could be very difficult.)
99 * The '.', '..' and the end of directory markers have fixed cookies which
100 * cannot collide with the cookies generated by other entries. The cookies
101 * for the other entries are generated based on the memory address on which
102 * stores their information is stored.
104 * Ideally, using the entry's memory pointer as the cookie would be enough
105 * to represent it and it wouldn't cause collisions in any system.
106 * Unfortunately, this results in "offsets" with very large values which
107 * later raise problems in the Linux compatibility layer (and maybe in other
108 * places) as described in PR kern/32034. Hence we need to workaround this
109 * with a rather ugly hack.
111 * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t
112 * set to 'long', which is a 32-bit *signed* long integer. Regardless of
113 * the macro value, GLIBC (2.3 at least) always uses the getdents64
114 * system call (when calling readdir) which internally returns off64_t
115 * offsets. In order to make 32-bit binaries work, *GLIBC* converts the
116 * 64-bit values returned by the kernel to 32-bit ones and aborts with
117 * EOVERFLOW if the conversion results in values that won't fit in 32-bit
118 * integers (which it assumes is because the directory is extremely large).
119 * This wouldn't cause problems if we were dealing with unsigned integers,
120 * but as we have signed integers, this check fails due to sign expansion.
122 * For example, consider that the kernel returns the 0xc1234567 cookie to
123 * userspace in a off64_t integer. Later on, GLIBC casts this value to
124 * off_t (remember, signed) with code similar to:
125 * system call returns the offset in kernel_value;
126 * off_t casted_value = kernel_value;
127 * if (sizeof(off_t) != sizeof(off64_t) &&
128 * kernel_value != casted_value)
130 * In this case, casted_value still has 0xc1234567, but when it is compared
131 * for equality against kernel_value, it is promoted to a 64-bit integer and
132 * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567.
133 * Then, GLIBC assumes this is because the directory is very large.
135 * Given that all the above happens in user-space, we have no control over
136 * it; therefore we must workaround the issue here. We do this by
137 * truncating the pointer value to a 32-bit integer and hope that there
138 * won't be collisions. In fact, this will not cause any problems in
139 * 32-bit platforms but some might arise in 64-bit machines (I'm not sure
140 * if they can happen at all in practice).
142 * XXX A nicer solution shall be attempted. */
144 #define TMPFS_DIRCOOKIE_DOT 0
145 #define TMPFS_DIRCOOKIE_DOTDOT 1
146 #define TMPFS_DIRCOOKIE_EOF 2
149 tmpfs_dircookie(struct tmpfs_dirent *de)
153 cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF;
154 KKASSERT(cookie != TMPFS_DIRCOOKIE_DOT);
155 KKASSERT(cookie != TMPFS_DIRCOOKIE_DOTDOT);
156 KKASSERT(cookie != TMPFS_DIRCOOKIE_EOF);
162 /* --------------------------------------------------------------------- */
165 * Internal representation of a tmpfs file system node.
167 * This structure is splitted in two parts: one holds attributes common
168 * to all file types and the other holds data that is only applicable to
169 * a particular type. The code must be careful to only access those
170 * attributes that are actually allowed by the node's type.
173 * Below is the key of locks used to protected the fields in the following
178 /* Doubly-linked list entry which links all existing nodes for a
179 * single file system. This is provided to ease the removal of
180 * all nodes during the unmount operation. */
181 LIST_ENTRY(tmpfs_node) tn_entries;
183 /* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
184 * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode
185 * types instead of a custom enumeration is to make things simpler
186 * and faster, as we do not need to convert between two types. */
189 /* Node identifier. */
192 /* Node's internal status. This is used by several file system
193 * operations to do modifications to the node in a delayed
196 #define TMPFS_NODE_ACCESSED (1 << 1)
197 #define TMPFS_NODE_MODIFIED (1 << 2)
198 #define TMPFS_NODE_CHANGED (1 << 3)
200 /* The node size. It does not necessarily match the real amount
201 * of memory consumed by it. */
204 /* Generic node attributes. */
211 int32_t tn_atimensec;
213 int32_t tn_mtimensec;
215 int32_t tn_ctimensec;
216 unsigned long tn_gen;
217 struct lockf tn_advlock;
219 /* As there is a single vnode for each active file within the
220 * system, care has to be taken to avoid allocating more than one
221 * vnode per file. In order to do this, a bidirectional association
222 * is kept between vnodes and nodes.
224 * Whenever a vnode is allocated, its v_data field is updated to
225 * point to the node it references. At the same time, the node's
226 * tn_vnode field is modified to point to the new vnode representing
227 * it. Further attempts to allocate a vnode for this same node will
228 * result in returning a new reference to the value stored in
231 * May be NULL when the node is unused (that is, no vnode has been
232 * allocated for it or it has been reclaimed). */
233 struct vnode * tn_vnode;
235 /* interlock to protect tn_vpstate */
236 struct lock tn_interlock;
238 /* Identify if current node has vnode assiocate with
239 * or allocating vnode.
243 /* misc data field for different tn_type node */
245 /* Valid when tn_type == VBLK || tn_type == VCHR. */
246 dev_t tn_rdev; /*int32_t ?*/
248 /* Valid when tn_type == VDIR. */
250 /* Pointer to the parent directory. The root
251 * directory has a pointer to itself in this field;
252 * this property identifies the root node. */
253 struct tmpfs_node * tn_parent;
255 /* Head of a tail-queue that links the contents of
256 * the directory together. See above for a
257 * description of its contents. */
258 struct tmpfs_dir tn_dirhead;
260 /* Number and pointer of the first directory entry
261 * returned by the readdir operation if it were
262 * called again to continue reading data from the
263 * same directory as before. This is used to speed
264 * up reads of long directories, assuming that no
265 * more than one read is in progress at a given time.
266 * Otherwise, these values are discarded and a linear
267 * scan is performed from the beginning up to the
268 * point where readdir starts returning values. */
269 off_t tn_readdir_lastn;
270 struct tmpfs_dirent * tn_readdir_lastp;
273 /* Valid when tn_type == VLNK. */
274 /* The link's target, allocated from a string pool. */
277 /* Valid when tn_type == VREG. */
279 /* The contents of regular files stored in a tmpfs
280 * file system are represented by a single anonymous
281 * memory object (aobj, for short). The aobj provides
282 * direct access to any position within the file,
283 * because its contents are always mapped in a
284 * contiguous region of virtual memory. It is a task
285 * of the memory management subsystem (see uvm(9)) to
286 * issue the required page ins or page outs whenever
287 * a position within the file is accessed. */
289 size_t tn_aobj_pages;
293 /* Valid when tn_type = VFIFO */
295 int (*tn_fo_read) (struct file *fp, struct uio *uio,
296 struct ucred *cred, int flags);
297 int (*tn_fo_write) (struct file *fp, struct uio *uio,
298 struct ucred *cred, int flags);
302 LIST_HEAD(tmpfs_node_list, tmpfs_node);
304 #define tn_rdev tn_spec.tn_rdev
305 #define tn_dir tn_spec.tn_dir
306 #define tn_link tn_spec.tn_link
307 #define tn_reg tn_spec.tn_reg
308 #define tn_fifo tn_spec.tn_fifo
310 #define TMPFS_NODE_LOCK(node) lockmgr(&(node)->tn_interlock, LK_EXCLUSIVE|LK_RETRY)
311 #define TMPFS_NODE_UNLOCK(node) lockmgr(&(node)->tn_interlock, LK_RELEASE)
312 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
315 #define TMPFS_ASSERT_LOCKED(node) do { \
316 KKASSERT(node != NULL); \
317 KKASSERT(node->tn_vnode != NULL); \
318 if (!vn_islocked(node->tn_vnode) && \
319 (lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE )) \
320 panic("tmpfs: node is not locked: %p", node); \
322 #define TMPFS_ASSERT_ELOCKED(node) do { \
323 KKASSERT((node) != NULL); \
324 KKASSERT(lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE); \
327 #define TMPFS_ASSERT_LOCKED(node) (void)0
328 #define TMPFS_ASSERT_ELOCKED(node) (void)0
331 #define TMPFS_VNODE_ALLOCATING 1
332 #define TMPFS_VNODE_WANT 2
333 #define TMPFS_VNODE_DOOMED 4
334 /* --------------------------------------------------------------------- */
337 * Internal representation of a tmpfs mount point.
340 /* Maximum number of memory pages available for use by the file
341 * system, set during mount time. This variable must never be
342 * used directly as it may be bigger than the current amount of
343 * free memory; in the extreme case, it will hold the SIZE_MAX
344 * value. Instead, use the TMPFS_PAGES_MAX macro. */
345 vm_pindex_t tm_pages_max;
347 /* Number of pages in use by the file system. Cannot be bigger
348 * than the value returned by TMPFS_PAGES_MAX in any case. */
349 vm_pindex_t tm_pages_used;
351 /* Pointer to the node representing the root directory of this
353 struct tmpfs_node * tm_root;
355 /* Maximum number of possible nodes for this file system; set
356 * during mount time. We need a hard limit on the maximum number
357 * of nodes to avoid allocating too much of them; their objects
358 * cannot be released until the file system is unmounted.
359 * Otherwise, we could easily run out of memory by creating lots
360 * of empty files and then simply removing them. */
363 /* Number of nodes currently that are in use. */
364 ino_t tm_nodes_inuse;
366 /* maximum representable file size */
367 u_int64_t tm_maxfilesize;
369 /* Nodes are organized in two different lists. The used list
370 * contains all nodes that are currently used by the file system;
371 * i.e., they refer to existing files. The available list contains
372 * all nodes that are currently available for use by new files.
373 * Nodes must be kept in this list (instead of deleting them)
374 * because we need to keep track of their generation number (tn_gen
377 * Note that nodes are lazily allocated: if the available list is
378 * empty and we have enough space to create more nodes, they will be
379 * created and inserted in the used list. Once these are released,
380 * they will go into the available list, remaining alive until the
381 * file system is unmounted. */
382 struct tmpfs_node_list tm_nodes_used;
384 /* All node lock to protect the node list and tmp_pages_used */
385 struct lock allnode_lock;
387 /* Per-mount malloc zones for tmpfs nodes, names, and dirents */
388 struct malloc_type *tm_node_zone;
389 struct malloc_type *tm_dirent_zone;
390 struct malloc_type *tm_name_zone;
392 struct objcache_malloc_args tm_node_zone_malloc_args;
393 struct objcache_malloc_args tm_dirent_zone_malloc_args;
395 /* Pools used to store file system meta data. These are not shared
396 * across several instances of tmpfs for the reasons described in
398 struct objcache *tm_dirent_pool;
399 struct objcache *tm_node_pool;
404 #define TMPFS_LOCK(tm) lockmgr(&(tm)->allnode_lock, LK_EXCLUSIVE|LK_RETRY)
405 #define TMPFS_UNLOCK(tm) lockmgr(&(tm)->allnode_lock, LK_RELEASE)
407 /* --------------------------------------------------------------------- */
410 * This structure maps a file identifier to a tmpfs node. Used by the
417 unsigned long tf_gen;
420 /* --------------------------------------------------------------------- */
424 * Prototypes for tmpfs_subr.c.
427 int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
428 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
429 char *, int, int, struct tmpfs_node **);
430 void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
431 int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
432 const char *, uint16_t, struct tmpfs_dirent **);
433 void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
434 int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
436 void tmpfs_free_vp(struct vnode *);
437 int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
438 struct namecache *, struct ucred *, char *);
439 void tmpfs_dir_attach(struct tmpfs_node *, struct tmpfs_dirent *);
440 void tmpfs_dir_detach(struct tmpfs_node *, struct tmpfs_dirent *);
441 struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node,
442 struct tmpfs_node *f,
443 struct namecache *ncp);
444 int tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
445 int tmpfs_dir_getdotdotdent(struct tmpfs_mount *,
446 struct tmpfs_node *, struct uio *);
447 struct tmpfs_dirent * tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
448 int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
449 int tmpfs_reg_resize(struct vnode *, off_t, int);
450 int tmpfs_chflags(struct vnode *, int, struct ucred *);
451 int tmpfs_chmod(struct vnode *, mode_t, struct ucred *);
452 int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *);
453 int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *);
454 int tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
455 int, struct ucred *);
456 void tmpfs_itimes(struct vnode *, const struct timespec *,
457 const struct timespec *);
459 void tmpfs_update(struct vnode *);
460 int tmpfs_truncate(struct vnode *, off_t);
461 int tmpfs_node_ctor(void *obj, void *privdata, int flags);
463 /* --------------------------------------------------------------------- */
466 * Convenience macros to simplify some logical expressions.
468 #define IMPLIES(a, b) (!(a) || (b))
469 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
471 /* --------------------------------------------------------------------- */
474 * Checks that the directory entry pointed by 'de' matches the name 'name'
475 * with a length of 'len'.
477 #define TMPFS_DIRENT_MATCHES(de, name, len) \
478 (de->td_namelen == (uint16_t)len && \
479 bcmp((de)->td_name, (name), (de)->td_namelen) == 0)
481 /* --------------------------------------------------------------------- */
484 * Ensures that the node pointed by 'node' is a directory and that its
485 * contents are consistent with respect to directories.
487 #define TMPFS_VALIDATE_DIR(node) \
488 KKASSERT((node)->tn_type == VDIR); \
489 KKASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
490 KKASSERT((node)->tn_dir.tn_readdir_lastp == NULL || \
491 tmpfs_dircookie((node)->tn_dir.tn_readdir_lastp) == (node)->tn_dir.tn_readdir_lastn);
495 /* --------------------------------------------------------------------- */
498 * Macros/functions to convert from generic data structures to tmpfs
504 VFS_TO_TMPFS(struct mount *mp)
506 struct tmpfs_mount *tmp;
508 KKASSERT((mp) != NULL && (mp)->mnt_data != NULL);
509 tmp = (struct tmpfs_mount *)(mp)->mnt_data;
515 VP_TO_TMPFS_NODE(struct vnode *vp)
517 struct tmpfs_node *node;
519 KKASSERT((vp) != NULL && (vp)->v_data != NULL);
520 node = (struct tmpfs_node *)vp->v_data;
526 VP_TO_TMPFS_DIR(struct vnode *vp)
528 struct tmpfs_node *node;
530 node = VP_TO_TMPFS_NODE(vp);
531 TMPFS_VALIDATE_DIR(node);
535 /* --------------------------------------------------------------------- */
539 #define BSIZE (off_t)16384 /* buffer cache size*/
540 #define BMASK (off_t)(BSIZE - 1)
542 #endif /* _VFS_TMPFS_TMPFS_H_ */