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.
35 #ifndef _FS_TMPFS_TMPFS_H_
36 #define _FS_TMPFS_TMPFS_H_
38 #include <sys/dirent.h>
39 #include <sys/mount.h>
40 #include <sys/queue.h>
41 #include <sys/vnode.h>
44 #include <sys/mutex.h>
46 #include <sys/malloc.h>
47 #include <sys/systm.h>
49 #include <sys/vmmeter.h>
50 #include <vm/swap_pager.h>
52 MALLOC_DECLARE(M_TMPFSMNT);
53 MALLOC_DECLARE(M_TMPFSNAME);
56 * Internal representation of a tmpfs directory entry.
59 LIST_HEAD(tmpfs_dir_duphead, tmpfs_dirent);
63 * Depending on td_cookie flag entry can be of 3 types:
64 * - regular -- no hash collisions, stored in RB-Tree
65 * - duphead -- synthetic linked list head for dup entries
66 * - dup -- stored in linked list instead of RB-Tree
69 /* regular and duphead entry types */
70 RB_ENTRY(tmpfs_dirent) td_entries;
74 LIST_ENTRY(tmpfs_dirent) entries;
75 LIST_ENTRY(tmpfs_dirent) index_entries;
84 * Pointer to the node this entry refers to. In case this field
85 * is NULL, the node is a whiteout.
87 struct tmpfs_node * td_node;
91 * The name of the entry, allocated from a string pool. This
92 * string is not required to be zero-terminated.
94 char * td_name; /* regular, dup */
95 struct tmpfs_dir_duphead td_duphead; /* duphead */
100 * A directory in tmpfs holds a collection of directory entries, which
101 * in turn point to other files (which can be directories themselves).
103 * In tmpfs, this collection is managed by a RB-Tree, whose head is
104 * defined by the struct tmpfs_dir type.
106 * It is important to notice that directories do not have entries for . and
107 * .. as other file systems do. These can be generated when requested
108 * based on information available by other means, such as the pointer to
109 * the node itself in the former case or the pointer to the parent directory
110 * in the latter case. This is done to simplify tmpfs's code and, more
111 * importantly, to remove redundancy.
113 RB_HEAD(tmpfs_dir, tmpfs_dirent);
116 * Each entry in a directory has a cookie that identifies it. Cookies
117 * supersede offsets within directories because, given how tmpfs stores
118 * directories in memory, there is no such thing as an offset.
120 * The '.', '..' and the end of directory markers have fixed cookies which
121 * cannot collide with the cookies generated by other entries. The cookies
122 * for the other entries are generated based on the file name hash value or
123 * unique number in case of name hash collision.
125 * To preserve compatibility cookies are limited to 31 bits.
128 #define TMPFS_DIRCOOKIE_DOT 0
129 #define TMPFS_DIRCOOKIE_DOTDOT 1
130 #define TMPFS_DIRCOOKIE_EOF 2
131 #define TMPFS_DIRCOOKIE_MASK ((off_t)0x3fffffffU)
132 #define TMPFS_DIRCOOKIE_MIN ((off_t)0x00000004U)
133 #define TMPFS_DIRCOOKIE_DUP ((off_t)0x40000000U)
134 #define TMPFS_DIRCOOKIE_DUPHEAD ((off_t)0x80000000U)
135 #define TMPFS_DIRCOOKIE_DUP_MIN TMPFS_DIRCOOKIE_DUP
136 #define TMPFS_DIRCOOKIE_DUP_MAX \
137 (TMPFS_DIRCOOKIE_DUP | TMPFS_DIRCOOKIE_MASK)
140 * Internal representation of a tmpfs file system node.
142 * This structure is splitted in two parts: one holds attributes common
143 * to all file types and the other holds data that is only applicable to
144 * a particular type. The code must be careful to only access those
145 * attributes that are actually allowed by the node's type.
147 * Below is the key of locks used to protected the fields in the following
149 * (v) vnode lock in exclusive mode
150 * (vi) vnode lock in exclusive mode, or vnode lock in shared vnode and
153 * (m) tmpfs_mount tm_allnode_lock
154 * (c) stable after creation
158 * Doubly-linked list entry which links all existing nodes for
159 * a single file system. This is provided to ease the removal
160 * of all nodes during the unmount operation, and to support
161 * the implementation of VOP_VNTOCNP().
163 LIST_ENTRY(tmpfs_node) tn_entries; /* (m) */
166 * The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
167 * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode
168 * types instead of a custom enumeration is to make things simpler
169 * and faster, as we do not need to convert between two types.
171 enum vtype tn_type; /* (c) */
173 /* Node identifier. */
174 ino_t tn_id; /* (c) */
177 * Node's internal status. This is used by several file system
178 * operations to do modifications to the node in a delayed
181 int tn_status; /* (vi) */
182 #define TMPFS_NODE_ACCESSED (1 << 1)
183 #define TMPFS_NODE_MODIFIED (1 << 2)
184 #define TMPFS_NODE_CHANGED (1 << 3)
187 * The node size. It does not necessarily match the real amount
188 * of memory consumed by it.
190 off_t tn_size; /* (v) */
192 /* Generic node attributes. */
193 uid_t tn_uid; /* (v) */
194 gid_t tn_gid; /* (v) */
195 mode_t tn_mode; /* (v) */
196 u_long tn_flags; /* (v) */
197 nlink_t tn_links; /* (v) */
198 struct timespec tn_atime; /* (vi) */
199 struct timespec tn_mtime; /* (vi) */
200 struct timespec tn_ctime; /* (vi) */
201 struct timespec tn_birthtime; /* (v) */
202 unsigned long tn_gen; /* (c) */
205 * As there is a single vnode for each active file within the
206 * system, care has to be taken to avoid allocating more than one
207 * vnode per file. In order to do this, a bidirectional association
208 * is kept between vnodes and nodes.
210 * Whenever a vnode is allocated, its v_data field is updated to
211 * point to the node it references. At the same time, the node's
212 * tn_vnode field is modified to point to the new vnode representing
213 * it. Further attempts to allocate a vnode for this same node will
214 * result in returning a new reference to the value stored in
217 * May be NULL when the node is unused (that is, no vnode has been
218 * allocated for it or it has been reclaimed).
220 struct vnode * tn_vnode; /* (i) */
223 * Interlock to protect tn_vpstate, and tn_status under shared
226 struct mtx tn_interlock;
229 * Identify if current node has vnode assiocate with
230 * or allocating vnode.
232 int tn_vpstate; /* (i) */
234 /* misc data field for different tn_type node */
236 /* Valid when tn_type == VBLK || tn_type == VCHR. */
237 dev_t tn_rdev; /* (c) */
239 /* Valid when tn_type == VDIR. */
242 * Pointer to the parent directory. The root
243 * directory has a pointer to itself in this field;
244 * this property identifies the root node.
246 struct tmpfs_node * tn_parent;
249 * Head of a tree that links the contents of
250 * the directory together.
252 struct tmpfs_dir tn_dirhead;
255 * Head of a list the contains fake directory entries
256 * heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD
259 struct tmpfs_dir_duphead tn_dupindex;
262 * Number and pointer of the first directory entry
263 * returned by the readdir operation if it were
264 * called again to continue reading data from the
265 * same directory as before. This is used to speed
266 * up reads of long directories, assuming that no
267 * more than one read is in progress at a given time.
268 * Otherwise, these values are discarded.
270 off_t tn_readdir_lastn;
271 struct tmpfs_dirent * tn_readdir_lastp;
274 /* Valid when tn_type == VLNK. */
275 /* The link's target, allocated from a string pool. */
276 char * tn_link; /* (c) */
278 /* Valid when tn_type == VREG. */
281 * The contents of regular files stored in a
282 * tmpfs file system are represented by a
283 * single anonymous memory object (aobj, for
284 * short). The aobj provides direct access to
285 * any position within the file. It is a task
286 * of the memory management subsystem to issue
287 * the required page ins or page outs whenever
288 * a position within the file is accessed.
290 vm_object_t tn_aobj; /* (c) */
294 LIST_HEAD(tmpfs_node_list, tmpfs_node);
296 #define tn_rdev tn_spec.tn_rdev
297 #define tn_dir tn_spec.tn_dir
298 #define tn_link tn_spec.tn_link
299 #define tn_reg tn_spec.tn_reg
300 #define tn_fifo tn_spec.tn_fifo
302 #define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock)
303 #define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock)
304 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
305 #define TMPFS_NODE_ASSERT_LOCKED(node) mtx_assert(TMPFS_NODE_MTX(node), \
309 #define TMPFS_ASSERT_LOCKED(node) do { \
310 MPASS((node) != NULL); \
311 MPASS((node)->tn_vnode != NULL); \
312 ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs assert"); \
315 #define TMPFS_ASSERT_LOCKED(node) (void)0
318 #define TMPFS_VNODE_ALLOCATING 1
319 #define TMPFS_VNODE_WANT 2
320 #define TMPFS_VNODE_DOOMED 4
321 #define TMPFS_VNODE_WRECLAIM 8
324 * Internal representation of a tmpfs mount point.
328 * Maximum number of memory pages available for use by the file
329 * system, set during mount time. This variable must never be
330 * used directly as it may be bigger than the current amount of
331 * free memory; in the extreme case, it will hold the ULONG_MAX
336 /* Number of pages in use by the file system. */
337 u_long tm_pages_used;
340 * Pointer to the node representing the root directory of this
343 struct tmpfs_node * tm_root;
346 * Maximum number of possible nodes for this file system; set
347 * during mount time. We need a hard limit on the maximum number
348 * of nodes to avoid allocating too much of them; their objects
349 * cannot be released until the file system is unmounted.
350 * Otherwise, we could easily run out of memory by creating lots
351 * of empty files and then simply removing them.
355 /* unrhdr used to allocate inode numbers */
356 struct unrhdr * tm_ino_unr;
358 /* Number of nodes currently that are in use. */
359 ino_t tm_nodes_inuse;
361 /* maximum representable file size */
362 u_int64_t tm_maxfilesize;
365 * The used list contains all nodes that are currently used by
366 * the file system; i.e., they refer to existing files.
368 struct tmpfs_node_list tm_nodes_used;
370 /* All node lock to protect the node list and tmp_pages_used. */
371 struct mtx tm_allnode_lock;
373 /* Zones used to store file system meta data, per tmpfs mount. */
374 uma_zone_t tm_dirent_pool;
375 uma_zone_t tm_node_pool;
377 /* Read-only status. */
380 #define TMPFS_LOCK(tm) mtx_lock(&(tm)->tm_allnode_lock)
381 #define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->tm_allnode_lock)
382 #define TMPFS_MP_ASSERT_LOCKED(tm) mtx_assert(&(tm)->tm_allnode_lock, MA_OWNED)
385 * This structure maps a file identifier to a tmpfs node. Used by the
392 unsigned long tf_gen;
395 struct tmpfs_dir_cursor {
396 struct tmpfs_dirent *tdc_current;
397 struct tmpfs_dirent *tdc_tree;
402 * Prototypes for tmpfs_subr.c.
405 int tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *, enum vtype,
406 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
407 char *, dev_t, struct tmpfs_node **);
408 void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
409 int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
410 const char *, u_int, struct tmpfs_dirent **);
411 void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
412 void tmpfs_dirent_init(struct tmpfs_dirent *, const char *, u_int);
413 void tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj);
414 int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
416 void tmpfs_free_vp(struct vnode *);
417 int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
418 struct componentname *, char *);
419 void tmpfs_check_mtime(struct vnode *);
420 void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
421 void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
422 void tmpfs_dir_destroy(struct tmpfs_mount *, struct tmpfs_node *);
423 struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node,
424 struct tmpfs_node *f,
425 struct componentname *cnp);
426 int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, int,
428 int tmpfs_dir_whiteout_add(struct vnode *, struct componentname *);
429 void tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *);
430 int tmpfs_reg_resize(struct vnode *, off_t, boolean_t);
431 int tmpfs_chflags(struct vnode *, u_long, struct ucred *, struct thread *);
432 int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *);
433 int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
435 int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *);
436 int tmpfs_chtimes(struct vnode *, struct vattr *, struct ucred *cred,
438 void tmpfs_itimes(struct vnode *, const struct timespec *,
439 const struct timespec *);
441 void tmpfs_set_status(struct tmpfs_node *node, int status);
442 void tmpfs_update(struct vnode *);
443 int tmpfs_truncate(struct vnode *, off_t);
444 struct tmpfs_dirent *tmpfs_dir_first(struct tmpfs_node *dnode,
445 struct tmpfs_dir_cursor *dc);
446 struct tmpfs_dirent *tmpfs_dir_next(struct tmpfs_node *dnode,
447 struct tmpfs_dir_cursor *dc);
450 * Convenience macros to simplify some logical expressions.
452 #define IMPLIES(a, b) (!(a) || (b))
453 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
456 * Checks that the directory entry pointed by 'de' matches the name 'name'
457 * with a length of 'len'.
459 #define TMPFS_DIRENT_MATCHES(de, name, len) \
460 (de->td_namelen == len && \
461 bcmp((de)->ud.td_name, (name), (de)->td_namelen) == 0)
464 * Ensures that the node pointed by 'node' is a directory and that its
465 * contents are consistent with respect to directories.
467 #define TMPFS_VALIDATE_DIR(node) do { \
468 MPASS((node)->tn_type == VDIR); \
469 MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
473 * Amount of memory pages to reserve for the system (e.g., to not use by
476 #define TMPFS_PAGES_MINRESERVED (4 * 1024 * 1024 / PAGE_SIZE)
478 size_t tmpfs_mem_avail(void);
480 size_t tmpfs_pages_used(struct tmpfs_mount *tmp);
485 * Macros/functions to convert from generic data structures to tmpfs
489 static inline struct tmpfs_mount *
490 VFS_TO_TMPFS(struct mount *mp)
492 struct tmpfs_mount *tmp;
494 MPASS(mp != NULL && mp->mnt_data != NULL);
495 tmp = (struct tmpfs_mount *)mp->mnt_data;
499 static inline struct tmpfs_node *
500 VP_TO_TMPFS_NODE(struct vnode *vp)
502 struct tmpfs_node *node;
504 MPASS(vp != NULL && vp->v_data != NULL);
505 node = (struct tmpfs_node *)vp->v_data;
509 static inline struct tmpfs_node *
510 VP_TO_TMPFS_DIR(struct vnode *vp)
512 struct tmpfs_node *node;
514 node = VP_TO_TMPFS_NODE(vp);
515 TMPFS_VALIDATE_DIR(node);
519 #endif /* _FS_TMPFS_TMPFS_H_ */