tmpfs - Fix tmpfs_fid, fix NFS exports
[dragonfly.git] / sys / vfs / tmpfs / tmpfs.h
blob14499a6a442a08e22f9a609f21d3b1e66286f7f6
1 /* $NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $ */
3 /*-
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
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/tree.h>
44 #include <sys/vnode.h>
45 #include <sys/file.h>
46 #include <sys/lock.h>
47 #include <sys/lockf.h>
48 #include <sys/mutex.h>
49 #include <sys/objcache.h>
51 /* --------------------------------------------------------------------- */
52 #include <sys/malloc.h>
53 #ifdef _KERNEL
54 #include <sys/systm.h>
55 #endif
56 #include <sys/vmmeter.h>
57 #include <vm/swap_pager.h>
59 MALLOC_DECLARE(M_TMPFSMNT);
61 /* --------------------------------------------------------------------- */
64 * Internal representation of a tmpfs directory entry.
66 struct tmpfs_dirent {
67 RB_ENTRY(tmpfs_dirent) rb_node;
68 RB_ENTRY(tmpfs_dirent) rb_cookienode;
70 /* Length of the name stored in this directory entry. This avoids
71 * the need to recalculate it every time the name is used. */
72 uint16_t td_namelen;
74 /* The name of the entry, allocated from a string pool. This
75 * string is not required to be zero-terminated; therefore, the
76 * td_namelen field must always be used when accessing its value. */
77 char *td_name;
79 /* Pointer to the node this entry refers to. */
80 struct tmpfs_node *td_node;
83 struct tmpfs_dirtree;
84 RB_HEAD(tmpfs_dirtree, tmpfs_dirent);
85 RB_PROTOTYPE(tmpfs_dirtree, tmpfs_dirent, rb_node,
86 tmpfs_dirtree_compare);
88 RB_HEAD(tmpfs_dirtree_cookie, tmpfs_dirent);
89 RB_PROTOTYPE(tmpfs_dirtree_cookie, tmpfs_dirent, rb_cookienode,
90 tmpfs_dirtree_cookie_compare);
94 * A directory in tmpfs holds a set of directory entries, which in
95 * turn point to other files (which can be directories themselves).
97 * In tmpfs, this set is managed by a red-black tree, whose root is defined
98 * by the struct tmpfs_dirtree type.
100 * It is important to notice that directories do not have entries for . and
101 * .. as other file systems do. These can be generated when requested
102 * based on information available by other means, such as the pointer to
103 * the node itself in the former case or the pointer to the parent directory
104 * in the latter case. This is done to simplify tmpfs's code and, more
105 * importantly, to remove redundancy.
107 * Each entry in a directory has a cookie that identifies it. Cookies
108 * supersede offsets within directories because, given how tmpfs stores
109 * directories in memory, there is no such thing as an offset. (Emulating
110 * a real offset could be very difficult.)
112 * The '.', '..' and the end of directory markers have fixed cookies which
113 * cannot collide with the cookies generated by other entries. The cookies
114 * for the other entries are generated based on the memory address on which
115 * stores their information is stored.
117 * DragonFly binaries use 64-bit cookies. We mask-off the signed bit to
118 * ensure that cookie 'offsets' are positive.
120 #ifdef _KERNEL
122 #define TMPFS_ROOTINO ((ino_t)2)
124 #define TMPFS_DIRCOOKIE_DOT 0
125 #define TMPFS_DIRCOOKIE_DOTDOT 1
126 #define TMPFS_DIRCOOKIE_EOF 2
128 static __inline
129 off_t
130 tmpfs_dircookie(struct tmpfs_dirent *de)
132 return (((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFFFFFFFFFFLLU);
135 #endif /* _KERNEL */
137 /* --------------------------------------------------------------------- */
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 struct tmpfs_node {
148 /* Doubly-linked list entry which links all existing nodes for a
149 * single file system. This is provided to ease the removal of
150 * all nodes during the unmount operation. */
151 LIST_ENTRY(tmpfs_node) tn_entries;
153 /* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
154 * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode
155 * types instead of a custom enumeration is to make things simpler
156 * and faster, as we do not need to convert between two types. */
157 enum vtype tn_type;
159 /* Node identifier. */
160 ino_t tn_id;
162 /* Node's internal status. This is used by several file system
163 * operations to do modifications to the node in a delayed
164 * fashion. */
165 int tn_status;
166 #define TMPFS_NODE_ACCESSED (1 << 1)
167 #define TMPFS_NODE_MODIFIED (1 << 2)
168 #define TMPFS_NODE_CHANGED (1 << 3)
170 /* The node size. It does not necessarily match the real amount
171 * of memory consumed by it. */
172 off_t tn_size;
174 /* Generic node attributes. */
175 uid_t tn_uid;
176 gid_t tn_gid;
177 mode_t tn_mode;
178 int tn_flags;
179 nlink_t tn_links;
180 long tn_atime;
181 long tn_atimensec;
182 long tn_mtime;
183 long tn_mtimensec;
184 long tn_ctime;
185 long tn_ctimensec;
186 unsigned long tn_gen;
187 struct lockf tn_advlock;
189 /* As there is a single vnode for each active file within the
190 * system, care has to be taken to avoid allocating more than one
191 * vnode per file. In order to do this, a bidirectional association
192 * is kept between vnodes and nodes.
194 * Whenever a vnode is allocated, its v_data field is updated to
195 * point to the node it references. At the same time, the node's
196 * tn_vnode field is modified to point to the new vnode representing
197 * it. Further attempts to allocate a vnode for this same node will
198 * result in returning a new reference to the value stored in
199 * tn_vnode.
201 * May be NULL when the node is unused (that is, no vnode has been
202 * allocated for it or it has been reclaimed). */
203 struct vnode * tn_vnode;
205 /* interlock to protect tn_vpstate */
206 struct lock tn_interlock;
208 /* Identify if current node has vnode assiocate with
209 * or allocating vnode.
211 int tn_vpstate;
213 /* misc data field for different tn_type node */
214 union {
215 /* Valid when tn_type == VBLK || tn_type == VCHR. */
216 dev_t tn_rdev; /*int32_t ?*/
218 /* Valid when tn_type == VDIR. */
219 struct tn_dir {
221 * Pointer to the parent directory. The root
222 * directory has a pointer to itself in this field;
223 * this property identifies the root node.
225 struct tmpfs_node * tn_parent;
228 * Directory entries are indexed by name and also
229 * indexed by cookie.
231 struct tmpfs_dirtree tn_dirtree;
232 struct tmpfs_dirtree_cookie tn_cookietree;
233 } tn_dir;
235 /* Valid when tn_type == VLNK. */
236 /* The link's target, allocated from a string pool. */
237 char * tn_link;
240 * Valid when tn_type == VREG.
242 * aobj is used as backing store for the vnode object. It
243 * typically only contains swap assignments, but we also use
244 * it to save the vnode object's vm_page's when the vnode
245 * becomes inactive.
247 struct tn_reg {
248 vm_object_t tn_aobj;
249 size_t tn_aobj_pages;
250 int tn_pages_in_aobj;
251 } tn_reg;
253 /* Valid when tn_type = VFIFO */
254 struct tn_fifo {
255 int (*tn_fo_read) (struct file *fp, struct uio *uio,
256 struct ucred *cred, int flags);
257 int (*tn_fo_write) (struct file *fp, struct uio *uio,
258 struct ucred *cred, int flags);
259 } tn_fifo;
260 } tn_spec;
263 /* Only userspace needs this */
264 #define VTOI(vp) ((struct tmpfs_node *)(vp)->v_data)
266 #ifdef _KERNEL
267 LIST_HEAD(tmpfs_node_list, tmpfs_node);
269 #define tn_rdev tn_spec.tn_rdev
270 #define tn_dir tn_spec.tn_dir
271 #define tn_link tn_spec.tn_link
272 #define tn_reg tn_spec.tn_reg
273 #define tn_fifo tn_spec.tn_fifo
275 #define TMPFS_NODE_LOCK(node) lockmgr(&(node)->tn_interlock, LK_EXCLUSIVE|LK_RETRY)
276 #define TMPFS_NODE_LOCK_SH(node) lockmgr(&(node)->tn_interlock, LK_SHARED|LK_RETRY)
277 #define TMPFS_NODE_UNLOCK(node) lockmgr(&(node)->tn_interlock, LK_RELEASE)
278 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
280 #ifdef INVARIANTS
281 #define TMPFS_ASSERT_LOCKED(node) do { \
282 KKASSERT(node != NULL); \
283 KKASSERT(node->tn_vnode != NULL); \
284 if (!vn_islocked(node->tn_vnode) && \
285 (lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE )) \
286 panic("tmpfs: node is not locked: %p", node); \
287 } while (0)
288 #define TMPFS_ASSERT_ELOCKED(node) do { \
289 KKASSERT((node) != NULL); \
290 KKASSERT(lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE); \
291 } while (0)
292 #else
293 #define TMPFS_ASSERT_LOCKED(node) (void)0
294 #define TMPFS_ASSERT_ELOCKED(node) (void)0
295 #endif /* INVARIANTS */
297 #define TMPFS_VNODE_ALLOCATING 1
298 #define TMPFS_VNODE_WANT 2
299 #define TMPFS_VNODE_DOOMED 4
300 /* --------------------------------------------------------------------- */
303 * Internal representation of a tmpfs mount point.
305 struct tmpfs_mount {
306 struct mount *tm_mount;
308 /* Maximum number of memory pages available for use by the file
309 * system, set during mount time. This variable must never be
310 * used directly as it may be bigger than the current amount of
311 * free memory; in the extreme case, it will hold the SIZE_MAX
312 * value. Instead, use the TMPFS_PAGES_MAX macro. */
313 long tm_pages_max;
315 /* Number of pages in use by the file system. Cannot be bigger
316 * than the value returned by TMPFS_PAGES_MAX in any case. */
317 long tm_pages_used;
319 /* Pointer to the node representing the root directory of this
320 * file system. */
321 struct tmpfs_node * tm_root;
323 /* Maximum number of possible nodes for this file system; set
324 * during mount time. We need a hard limit on the maximum number
325 * of nodes to avoid allocating too much of them; their objects
326 * cannot be released until the file system is unmounted.
327 * Otherwise, we could easily run out of memory by creating lots
328 * of empty files and then simply removing them. */
329 ino_t tm_nodes_max;
331 /* Number of nodes currently that are in use. */
332 ino_t tm_nodes_inuse;
334 /* maximum representable file size */
335 u_int64_t tm_maxfilesize;
337 /* Nodes are organized in two different lists. The used list
338 * contains all nodes that are currently used by the file system;
339 * i.e., they refer to existing files. The available list contains
340 * all nodes that are currently available for use by new files.
341 * Nodes must be kept in this list (instead of deleting them)
342 * because we need to keep track of their generation number (tn_gen
343 * field).
345 * Note that nodes are lazily allocated: if the available list is
346 * empty and we have enough space to create more nodes, they will be
347 * created and inserted in the used list. Once these are released,
348 * they will go into the available list, remaining alive until the
349 * file system is unmounted. */
350 struct tmpfs_node_list tm_nodes_used;
352 /* Per-mount malloc zones for tmpfs nodes, names, and dirents */
353 struct malloc_type *tm_node_zone;
354 struct malloc_type *tm_dirent_zone;
355 struct malloc_type *tm_name_zone;
357 struct objcache_malloc_args tm_node_zone_malloc_args;
358 struct objcache_malloc_args tm_dirent_zone_malloc_args;
360 /* Pools used to store file system meta data. */
361 struct objcache *tm_dirent_pool;
362 struct objcache *tm_node_pool;
364 ino_t tm_ino;
365 int tm_flags;
367 struct netexport tm_export;
369 struct mount *tm_mnt;
372 #define TMPFS_LOCK(tm) lwkt_gettoken(&(tm)->tm_mount->mnt_token)
373 #define TMPFS_UNLOCK(tm) lwkt_reltoken(&(tm)->tm_mount->mnt_token)
375 /* --------------------------------------------------------------------- */
378 * This structure maps a file identifier to a tmpfs node. Used by the
379 * NFS code.
381 struct tmpfs_fid {
382 uint16_t tf_len;
383 uint16_t tf_pad;
384 ino_t tf_id;
385 unsigned long tf_gen;
386 } __packed;
388 /* --------------------------------------------------------------------- */
391 * Prototypes for tmpfs_subr.c.
394 int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
395 uid_t uid, gid_t gid, mode_t mode, char *, int, int,
396 struct tmpfs_node **);
397 void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
398 int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
399 const char *, uint16_t, struct tmpfs_dirent **);
400 void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
401 int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
402 struct vnode **);
403 void tmpfs_free_vp(struct vnode *);
404 int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
405 struct namecache *, struct ucred *, char *);
406 void tmpfs_dir_attach(struct tmpfs_node *, struct tmpfs_dirent *);
407 void tmpfs_dir_detach(struct tmpfs_node *, struct tmpfs_dirent *);
408 struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node,
409 struct tmpfs_node *f,
410 struct namecache *ncp);
411 int tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
412 int tmpfs_dir_getdotdotdent(struct tmpfs_mount *,
413 struct tmpfs_node *, struct uio *);
414 struct tmpfs_dirent * tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
415 int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
416 int tmpfs_reg_resize(struct vnode *, off_t, int);
417 int tmpfs_chflags(struct vnode *, int, struct ucred *);
418 int tmpfs_chmod(struct vnode *, mode_t, struct ucred *);
419 int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *);
420 int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *);
421 int tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
422 int, struct ucred *);
423 void tmpfs_itimes(struct vnode *, const struct timespec *,
424 const struct timespec *);
426 void tmpfs_update(struct vnode *);
427 int tmpfs_truncate(struct vnode *, off_t);
428 boolean_t tmpfs_node_ctor(void *obj, void *privdata, int flags);
430 /* --------------------------------------------------------------------- */
433 * Convenience macros to simplify some logical expressions.
435 #define IMPLIES(a, b) (!(a) || (b))
436 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
438 /* --------------------------------------------------------------------- */
441 * Checks that the directory entry pointed by 'de' matches the name 'name'
442 * with a length of 'len'.
444 #define TMPFS_DIRENT_MATCHES(de, name, len) \
445 (de->td_namelen == (uint16_t)len && \
446 bcmp((de)->td_name, (name), (de)->td_namelen) == 0)
448 /* --------------------------------------------------------------------- */
451 * Ensures that the node pointed by 'node' is a directory and that its
452 * contents are consistent with respect to directories.
454 #define TMPFS_VALIDATE_DIR(node) do { \
455 KKASSERT((node)->tn_type == VDIR); \
456 KKASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
457 } while(0)
459 /* --------------------------------------------------------------------- */
462 * Macros/functions to convert from generic data structures to tmpfs
463 * specific ones. Kernel code use VP_TO_TMPFS_NODE() instead of VTOI().
466 static inline
467 struct tmpfs_mount *
468 VFS_TO_TMPFS(struct mount *mp)
470 struct tmpfs_mount *tmp;
472 KKASSERT((mp) != NULL && (mp)->mnt_data != NULL);
473 tmp = (struct tmpfs_mount *)(mp)->mnt_data;
474 return tmp;
477 static inline
478 struct tmpfs_node *
479 VP_TO_TMPFS_NODE(struct vnode *vp)
481 struct tmpfs_node *node;
483 KKASSERT((vp) != NULL && (vp)->v_data != NULL);
484 node = (struct tmpfs_node *)vp->v_data;
485 return node;
488 static inline
489 struct tmpfs_node *
490 VP_TO_TMPFS_DIR(struct vnode *vp)
492 struct tmpfs_node *node;
494 node = VP_TO_TMPFS_NODE(vp);
495 TMPFS_VALIDATE_DIR(node);
496 return node;
499 /* --------------------------------------------------------------------- */
501 * buffer cache size
503 #define TMPFS_BLKSIZE 16384 /* buffer cache size*/
504 #define TMPFS_BLKMASK (TMPFS_BLKSIZE - 1)
505 #define TMPFS_BLKMASK64 ((off_t)(TMPFS_BLKSIZE - 1))
506 #endif /* _KERNEL */
508 #endif /* _VFS_TMPFS_TMPFS_H_ */