xref: /dragonfly/sys/vfs/tmpfs/tmpfs.h (revision ff038999)
1 /*	$NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005, 2006 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  *
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.
31  *
32  * $FreeBSD: src/sys/fs/tmpfs/tmpfs.h,v 1.18 2009/10/11 07:03:56 delphij Exp $
33  */
34 
35 #ifndef _VFS_TMPFS_TMPFS_H_
36 #define _VFS_TMPFS_TMPFS_H_
37 
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>
50 
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>
58 
59 #ifdef MALLOC_DECLARE
60 MALLOC_DECLARE(M_TMPFSMNT);
61 #endif
62 
63 /* --------------------------------------------------------------------- */
64 
65 /*
66  * Internal representation of a tmpfs directory entry.
67  */
68 struct tmpfs_dirent {
69 	RB_ENTRY(tmpfs_dirent)	rb_node;
70 	RB_ENTRY(tmpfs_dirent)	rb_cookienode;
71 
72 	/* Length of the name stored in this directory entry.  This avoids
73 	 * the need to recalculate it every time the name is used. */
74 	uint16_t		td_namelen;
75 
76 	/* The name of the entry, allocated from a string pool.  This
77 	* string is not required to be zero-terminated; therefore, the
78 	* td_namelen field must always be used when accessing its value. */
79 	char 			*td_name;
80 
81 	/* Pointer to the node this entry refers to. */
82 	struct tmpfs_node 	*td_node;
83 };
84 
85 struct tmpfs_dirtree;
86 RB_HEAD(tmpfs_dirtree, tmpfs_dirent);
87 RB_PROTOTYPE(tmpfs_dirtree, tmpfs_dirent, rb_node,
88 	tmpfs_dirtree_compare);
89 
90 RB_HEAD(tmpfs_dirtree_cookie, tmpfs_dirent);
91 RB_PROTOTYPE(tmpfs_dirtree_cookie, tmpfs_dirent, rb_cookienode,
92 	tmpfs_dirtree_cookie_compare);
93 
94 
95 /*
96  * A directory in tmpfs holds a set of directory entries, which in
97  * turn point to other files (which can be directories themselves).
98  *
99  * In tmpfs, this set is managed by a red-black tree, whose root is defined
100  * by the struct tmpfs_dirtree type.
101  *
102  * It is important to notice that directories do not have entries for . and
103  * .. as other file systems do.  These can be generated when requested
104  * based on information available by other means, such as the pointer to
105  * the node itself in the former case or the pointer to the parent directory
106  * in the latter case.  This is done to simplify tmpfs's code and, more
107  * importantly, to remove redundancy.
108  *
109  * Each entry in a directory has a cookie that identifies it.  Cookies
110  * supersede offsets within directories because, given how tmpfs stores
111  * directories in memory, there is no such thing as an offset.  (Emulating
112  * a real offset could be very difficult.)
113  *
114  * The '.', '..' and the end of directory markers have fixed cookies which
115  * cannot collide with the cookies generated by other entries.  The cookies
116  * for the other entries are generated based on the memory address on which
117  * stores their information is stored.
118  *
119  * DragonFly binaries use 64-bit cookies.  We mask-off the signed bit to
120  * ensure that cookie 'offsets' are positive.
121  */
122 #ifdef _KERNEL
123 
124 #define	TMPFS_ROOTINO	((ino_t)2)
125 
126 #define	TMPFS_DIRCOOKIE_DOT	0
127 #define	TMPFS_DIRCOOKIE_DOTDOT	1
128 #define	TMPFS_DIRCOOKIE_EOF	2
129 
130 static __inline
131 off_t
132 tmpfs_dircookie(struct tmpfs_dirent *de)
133 {
134 	return (((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFFFFFFFFFFLLU);
135 }
136 
137 #endif  /* _KERNEL */
138 
139 /* --------------------------------------------------------------------- */
140 
141 /*
142  * Internal representation of a tmpfs file system node.
143  *
144  * This structure is splitted in two parts: one holds attributes common
145  * to all file types and the other holds data that is only applicable to
146  * a particular type.  The code must be careful to only access those
147  * attributes that are actually allowed by the node's type.
148  */
149 struct tmpfs_node {
150 	/* Doubly-linked list entry which links all existing nodes for a
151 	 * single file system.  This is provided to ease the removal of
152 	 * all nodes during the unmount operation. */
153 	LIST_ENTRY(tmpfs_node)	tn_entries;
154 
155 	/* The node's type.  Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
156 	 * 'VLNK', 'VREG' and 'VSOCK' is allowed.  The usage of vnode
157 	 * types instead of a custom enumeration is to make things simpler
158 	 * and faster, as we do not need to convert between two types. */
159 	enum vtype		tn_type;
160 
161 	/* Node identifier. */
162 	ino_t			tn_id;
163 
164 	/* Node's internal status.  This is used by several file system
165 	 * operations to do modifications to the node in a delayed
166 	 * fashion. */
167 	int			tn_blksize;	/* small file optimization */
168 	int			tn_status;
169 #define	TMPFS_NODE_ACCESSED	(1 << 1)
170 #define	TMPFS_NODE_MODIFIED	(1 << 2)
171 #define	TMPFS_NODE_CHANGED	(1 << 3)
172 
173 	/* The node size.  It does not necessarily match the real amount
174 	 * of memory consumed by it. */
175 	off_t			tn_size;
176 
177 	/* Generic node attributes. */
178 	uid_t			tn_uid;
179 	gid_t			tn_gid;
180 	mode_t			tn_mode;
181 	u_int			tn_flags;
182 	nlink_t			tn_links;	/* atomic ops req */
183 	long			tn_atime;
184 	long			tn_atimensec;
185 	long			tn_mtime;
186 	long			tn_mtimensec;
187 	long			tn_ctime;
188 	long			tn_ctimensec;
189 	unsigned long		tn_gen;
190 	struct lockf		tn_advlock;
191 
192 	/* As there is a single vnode for each active file within the
193 	 * system, care has to be taken to avoid allocating more than one
194 	 * vnode per file.  In order to do this, a bidirectional association
195 	 * is kept between vnodes and nodes.
196 	 *
197 	 * Whenever a vnode is allocated, its v_data field is updated to
198 	 * point to the node it references.  At the same time, the node's
199 	 * tn_vnode field is modified to point to the new vnode representing
200 	 * it.  Further attempts to allocate a vnode for this same node will
201 	 * result in returning a new reference to the value stored in
202 	 * tn_vnode.
203 	 *
204 	 * May be NULL when the node is unused (that is, no vnode has been
205 	 * allocated for it or it has been reclaimed). */
206 	struct vnode *		tn_vnode;
207 
208 	/* interlock to protect structure */
209 	struct lock		tn_interlock;
210 
211 	/*
212 	 * tmpfs vnode state, may specify an allocation in-progress.
213 	 */
214 	int		tn_vpstate;
215 
216 	/* misc data field for different tn_type node */
217 	union {
218 		/* Valid when tn_type == VBLK || tn_type == VCHR. */
219 		dev_t			tn_rdev; /*int32_t ?*/
220 
221 		/* Valid when tn_type == VDIR. */
222 		struct tn_dir {
223 			/*
224 			 * Pointer to the parent directory.  The root
225 			 * directory has a pointer to itself in this field;
226 			 * this property identifies the root node.
227 			 */
228 			struct tmpfs_node *	tn_parent;
229 
230 			/*
231 			 * Directory entries are indexed by name and also
232 			 * indexed by cookie.
233 			 */
234 			struct tmpfs_dirtree		tn_dirtree;
235 			struct tmpfs_dirtree_cookie	tn_cookietree;
236 		} tn_dir;
237 
238 		/* Valid when tn_type == VLNK. */
239 		/* The link's target, allocated from a string pool. */
240 		char *			tn_link;
241 
242 		/*
243 		 * Valid when tn_type == VREG.
244 		 *
245 		 * aobj is used as backing store for the vnode object.  It
246 		 * typically only contains swap assignments, but we also use
247 		 * it to save the vnode object's vm_page's when the vnode
248 		 * becomes inactive.
249 		 */
250 		struct tn_reg {
251 			vm_object_t		tn_aobj;
252 			size_t			tn_aobj_pages;
253 			int			tn_pages_in_aobj;
254 		} tn_reg;
255 
256 		/* Valid when tn_type = VFIFO */
257 		struct tn_fifo {
258 			int (*tn_fo_read)  (struct file *fp, struct uio *uio,
259 			        struct ucred *cred, int flags);
260 			int (*tn_fo_write) (struct file *fp, struct uio *uio,
261 			        struct ucred *cred, int flags);
262 		} tn_fifo;
263 	} tn_spec;
264 };
265 
266 /* Only userspace needs this */
267 #define VTOI(vp)	((struct tmpfs_node *)(vp)->v_data)
268 
269 #ifdef _KERNEL
270 LIST_HEAD(tmpfs_node_list, tmpfs_node);
271 
272 #define tn_rdev tn_spec.tn_rdev
273 #define tn_dir tn_spec.tn_dir
274 #define tn_link tn_spec.tn_link
275 #define tn_reg tn_spec.tn_reg
276 #define tn_fifo tn_spec.tn_fifo
277 
278 #define TMPFS_NODE_LOCK(node) lockmgr(&(node)->tn_interlock, LK_EXCLUSIVE|LK_RETRY)
279 #define TMPFS_NODE_LOCK_SH(node) lockmgr(&(node)->tn_interlock, LK_SHARED|LK_RETRY)
280 #define TMPFS_NODE_UNLOCK(node) lockmgr(&(node)->tn_interlock, LK_RELEASE)
281 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
282 
283 #ifdef INVARIANTS
284 #define TMPFS_ASSERT_LOCKED(node) do {					\
285 		KKASSERT(node != NULL);					\
286 		KKASSERT(node->tn_vnode != NULL);			\
287 		if (!vn_islocked(node->tn_vnode) &&			\
288 		    (lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE ))		\
289 			panic("tmpfs: node is not locked: %p", node);	\
290 	} while (0)
291 #define TMPFS_ASSERT_ELOCKED(node) do {					\
292 		KKASSERT((node) != NULL);				\
293 		KKASSERT(lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE);		\
294 	} while (0)
295 #else
296 #define TMPFS_ASSERT_LOCKED(node) (void)0
297 #define TMPFS_ASSERT_ELOCKED(node) (void)0
298 #endif  /* INVARIANTS */
299 
300 #define TMPFS_VNODE_DOOMED	0x0001
301 /* --------------------------------------------------------------------- */
302 
303 /*
304  * Internal representation of a tmpfs mount point.
305  */
306 struct tmpfs_mount {
307 	struct mount		*tm_mount;
308 
309 	/* Maximum number of memory pages available for use by the file
310 	 * system, set during mount time.  This variable must never be
311 	 * used directly as it may be bigger than the current amount of
312 	 * free memory; in the extreme case, it will hold the SIZE_MAX
313 	 * value.  Instead, use the TMPFS_PAGES_MAX macro. */
314 	long			tm_pages_max;
315 
316 	/* Number of pages in use by the file system.  Cannot be bigger
317 	 * than the value returned by TMPFS_PAGES_MAX in any case. */
318 	long			tm_pages_used;
319 
320 	/* Pointer to the node representing the root directory of this
321 	 * file system. */
322 	struct tmpfs_node *	tm_root;
323 
324 	/* Maximum number of possible nodes for this file system; set
325 	 * during mount time.  We need a hard limit on the maximum number
326 	 * of nodes to avoid allocating too much of them; their objects
327 	 * cannot be released until the file system is unmounted.
328 	 * Otherwise, we could easily run out of memory by creating lots
329 	 * of empty files and then simply removing them. */
330 	ino_t			tm_nodes_max;
331 
332 	/* Number of nodes currently that are in use. */
333 	ino_t			tm_nodes_inuse;
334 
335 	/* maximum representable file size */
336 	u_int64_t		tm_maxfilesize;
337 
338 	/* Nodes are organized in two different lists.  The used list
339 	 * contains all nodes that are currently used by the file system;
340 	 * i.e., they refer to existing files.  The available list contains
341 	 * all nodes that are currently available for use by new files.
342 	 * Nodes must be kept in this list (instead of deleting them)
343 	 * because we need to keep track of their generation number (tn_gen
344 	 * field).
345 	 *
346 	 * Note that nodes are lazily allocated: if the available list is
347 	 * empty and we have enough space to create more nodes, they will be
348 	 * created and inserted in the used list.  Once these are released,
349 	 * they will go into the available list, remaining alive until the
350 	 * file system is unmounted. */
351 	struct tmpfs_node_list	tm_nodes_used;
352 
353 	/* Per-mount malloc zones for tmpfs nodes, names, and dirents */
354 	struct malloc_type	*tm_node_zone;
355 	struct malloc_type	*tm_dirent_zone;
356 	struct malloc_type	*tm_name_zone;
357 
358 	struct objcache_malloc_args tm_node_zone_malloc_args;
359 	struct objcache_malloc_args tm_dirent_zone_malloc_args;
360 
361 	/* Pools used to store file system meta data. */
362 	struct objcache		*tm_dirent_pool;
363 	struct objcache		*tm_node_pool;
364 
365 	ino_t			tm_ino;
366 	int			tm_flags;
367 
368 	struct netexport	tm_export;
369 
370 	struct mount		*tm_mnt;
371 };
372 
373 #define TMPFS_LOCK(tm) lwkt_gettoken(&(tm)->tm_mount->mnt_token)
374 #define TMPFS_UNLOCK(tm) lwkt_reltoken(&(tm)->tm_mount->mnt_token)
375 
376 /* --------------------------------------------------------------------- */
377 
378 /*
379  * This structure maps a file identifier to a tmpfs node.  Used by the
380  * NFS code.
381  */
382 struct tmpfs_fid {
383 	uint16_t		tf_len;
384 	uint16_t		tf_pad;
385 	ino_t			tf_id;
386 	unsigned long		tf_gen;
387 } __packed;
388 
389 /* --------------------------------------------------------------------- */
390 
391 /*
392  * Prototypes for tmpfs_subr.c.
393  */
394 
395 int	tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
396 	    uid_t uid, gid_t gid, mode_t mode, char *, int, int,
397 	    struct tmpfs_node **);
398 void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
399 int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
400 	    const char *, uint16_t, struct tmpfs_dirent **);
401 void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
402 int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *,
403 	    struct tmpfs_node *, int, 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 *, u_long, 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 *);
425 
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);
429 void	tmpfs_lock4(struct tmpfs_node *node1, struct tmpfs_node *node2,
430 		struct tmpfs_node *node3, struct tmpfs_node *node4);
431 void	tmpfs_unlock4(struct tmpfs_node *node1, struct tmpfs_node *node2,
432 		struct tmpfs_node *node3, struct tmpfs_node *node4);
433 
434 /* --------------------------------------------------------------------- */
435 
436 /*
437  * Convenience macros to simplify some logical expressions.
438  */
439 #define IMPLIES(a, b) (!(a) || (b))
440 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
441 
442 /* --------------------------------------------------------------------- */
443 
444 /*
445  * Checks that the directory entry pointed by 'de' matches the name 'name'
446  * with a length of 'len'.
447  */
448 #define TMPFS_DIRENT_MATCHES(de, name, len) \
449     (de->td_namelen == (uint16_t)len && \
450     bcmp((de)->td_name, (name), (de)->td_namelen) == 0)
451 
452 /* --------------------------------------------------------------------- */
453 
454 /*
455  * Ensures that the node pointed by 'node' is a directory and that its
456  * contents are consistent with respect to directories.
457  */
458 #define TMPFS_VALIDATE_DIR(node) do {	\
459     KKASSERT((node)->tn_type == VDIR);	\
460     KKASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
461 } while(0)
462 
463 /* --------------------------------------------------------------------- */
464 
465 /*
466  * Macros/functions to convert from generic data structures to tmpfs
467  * specific ones.  Kernel code use VP_TO_TMPFS_NODE() instead of VTOI().
468  */
469 
470 static inline
471 struct tmpfs_mount *
472 VFS_TO_TMPFS(struct mount *mp)
473 {
474 	struct tmpfs_mount *tmp;
475 
476 	KKASSERT((mp) != NULL && (mp)->mnt_data != NULL);
477 	tmp = (struct tmpfs_mount *)(mp)->mnt_data;
478 	return tmp;
479 }
480 
481 static inline
482 struct tmpfs_node *
483 VP_TO_TMPFS_NODE(struct vnode *vp)
484 {
485 	struct tmpfs_node *node;
486 
487 	KKASSERT((vp) != NULL && (vp)->v_data != NULL);
488 	node = (struct tmpfs_node *)vp->v_data;
489 	return node;
490 }
491 
492 static inline
493 struct tmpfs_node *
494 VP_TO_TMPFS_DIR(struct vnode *vp)
495 {
496 	struct tmpfs_node *node;
497 
498 	node = VP_TO_TMPFS_NODE(vp);
499 	TMPFS_VALIDATE_DIR(node);
500 	return node;
501 }
502 
503 /* --------------------------------------------------------------------- */
504 /*
505  * buffer cache size
506  */
507 #define TMPFS_BLKSIZE	16384			/* buffer cache size*/
508 #define TMPFS_BLKMASK	(TMPFS_BLKSIZE - 1)
509 #define TMPFS_BLKMASK64	((off_t)(TMPFS_BLKSIZE - 1))
510 #endif /* _KERNEL */
511 
512 #endif /* _VFS_TMPFS_TMPFS_H_ */
513