xref: /freebsd/sys/fs/tmpfs/tmpfs.h (revision 5b9c547c)
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$
33  */
34 
35 #ifndef _FS_TMPFS_TMPFS_H_
36 #define _FS_TMPFS_TMPFS_H_
37 
38 #include <sys/dirent.h>
39 #include <sys/mount.h>
40 #include <sys/queue.h>
41 #include <sys/vnode.h>
42 #include <sys/file.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 
46 #include <sys/malloc.h>
47 #include <sys/systm.h>
48 #include <sys/tree.h>
49 #include <sys/vmmeter.h>
50 #include <vm/swap_pager.h>
51 
52 MALLOC_DECLARE(M_TMPFSMNT);
53 MALLOC_DECLARE(M_TMPFSNAME);
54 
55 /*
56  * Internal representation of a tmpfs directory entry.
57  */
58 
59 LIST_HEAD(tmpfs_dir_duphead, tmpfs_dirent);
60 
61 struct tmpfs_dirent {
62 	/*
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
67 	 */
68 	union {
69 		/* regular and duphead entry types */
70 		RB_ENTRY(tmpfs_dirent)		td_entries;
71 
72 		/* dup entry type */
73 		struct {
74 			LIST_ENTRY(tmpfs_dirent) entries;
75 			LIST_ENTRY(tmpfs_dirent) index_entries;
76 		} td_dup;
77 	} uh;
78 
79 	uint32_t			td_cookie;
80 	uint32_t			td_hash;
81 	u_int				td_namelen;
82 
83 	/* Pointer to the node this entry refers to.  In case this field
84 	 * is NULL, the node is a whiteout. */
85 	struct tmpfs_node *		td_node;
86 
87 	union {
88 		/*
89 		 * The name of the entry, allocated from a string pool.  This
90 		 * string is not required to be zero-terminated.
91 		 */
92 		char *			td_name;	/* regular, dup */
93 		struct tmpfs_dir_duphead td_duphead;	/* duphead */
94 	} ud;
95 };
96 
97 /* A directory in tmpfs holds a list of directory entries, which in
98  * turn point to other files (which can be directories themselves).
99  *
100  * In tmpfs, this list is managed by a RB-Tree, whose head is defined by
101  * the struct tmpfs_dir type.
102  *
103  * It is important to notice that directories do not have entries for . and
104  * .. as other file systems do.  These can be generated when requested
105  * based on information available by other means, such as the pointer to
106  * the node itself in the former case or the pointer to the parent directory
107  * in the latter case.  This is done to simplify tmpfs's code and, more
108  * importantly, to remove redundancy. */
109 RB_HEAD(tmpfs_dir, tmpfs_dirent);
110 
111 /* Each entry in a directory has a cookie that identifies it.  Cookies
112  * supersede offsets within directories because, given how tmpfs stores
113  * directories in memory, there is no such thing as an offset.
114  *
115  * The '.', '..' and the end of directory markers have fixed cookies which
116  * cannot collide with the cookies generated by other entries.  The cookies
117  * for the other entries are generated based on the file name hash value or
118  * unique number in case of name hash collision.
119  *
120  * To preserve compatibility cookies are limited to 31 bits.
121  */
122 
123 #define	TMPFS_DIRCOOKIE_DOT		0
124 #define	TMPFS_DIRCOOKIE_DOTDOT		1
125 #define	TMPFS_DIRCOOKIE_EOF		2
126 #define	TMPFS_DIRCOOKIE_MASK		((off_t)0x3fffffffU)
127 #define	TMPFS_DIRCOOKIE_MIN		((off_t)0x00000004U)
128 #define	TMPFS_DIRCOOKIE_DUP		((off_t)0x40000000U)
129 #define	TMPFS_DIRCOOKIE_DUPHEAD		((off_t)0x80000000U)
130 #define	TMPFS_DIRCOOKIE_DUP_MIN		TMPFS_DIRCOOKIE_DUP
131 #define	TMPFS_DIRCOOKIE_DUP_MAX		\
132 	(TMPFS_DIRCOOKIE_DUP | TMPFS_DIRCOOKIE_MASK)
133 
134 /*
135  * Internal representation of a tmpfs file system node.
136  *
137  * This structure is splitted in two parts: one holds attributes common
138  * to all file types and the other holds data that is only applicable to
139  * a particular type.  The code must be careful to only access those
140  * attributes that are actually allowed by the node's type.
141  *
142  *
143  * Below is the key of locks used to protected the fields in the following
144  * structures.
145  *
146  */
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;
152 
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;
158 
159 	/* Node identifier. */
160 	ino_t			tn_id;
161 
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)
169 
170 	/* The node size.  It does not necessarily match the real amount
171 	 * of memory consumed by it. */
172 	off_t			tn_size;
173 
174 	/* Generic node attributes. */
175 	uid_t			tn_uid;
176 	gid_t			tn_gid;
177 	mode_t			tn_mode;
178 	u_long			tn_flags;
179 	nlink_t			tn_links;
180 	struct timespec		tn_atime;
181 	struct timespec		tn_mtime;
182 	struct timespec		tn_ctime;
183 	struct timespec		tn_birthtime;
184 	unsigned long		tn_gen;
185 
186 	/* As there is a single vnode for each active file within the
187 	 * system, care has to be taken to avoid allocating more than one
188 	 * vnode per file.  In order to do this, a bidirectional association
189 	 * is kept between vnodes and nodes.
190 	 *
191 	 * Whenever a vnode is allocated, its v_data field is updated to
192 	 * point to the node it references.  At the same time, the node's
193 	 * tn_vnode field is modified to point to the new vnode representing
194 	 * it.  Further attempts to allocate a vnode for this same node will
195 	 * result in returning a new reference to the value stored in
196 	 * tn_vnode.
197 	 *
198 	 * May be NULL when the node is unused (that is, no vnode has been
199 	 * allocated for it or it has been reclaimed). */
200 	struct vnode *		tn_vnode;
201 
202 	/* interlock to protect tn_vpstate */
203 	struct mtx	tn_interlock;
204 
205 	/* Identify if current node has vnode assiocate with
206 	 * or allocating vnode.
207 	 */
208 	int		tn_vpstate;
209 
210 	/* misc data field for different tn_type node */
211 	union {
212 		/* Valid when tn_type == VBLK || tn_type == VCHR. */
213 		dev_t			tn_rdev;
214 
215 		/* Valid when tn_type == VDIR. */
216 		struct tn_dir {
217 			/* Pointer to the parent directory.  The root
218 			 * directory has a pointer to itself in this field;
219 			 * this property identifies the root node. */
220 			struct tmpfs_node *	tn_parent;
221 
222 			/* Head of a tree that links the contents of
223 			 * the directory together. */
224 			struct tmpfs_dir	tn_dirhead;
225 
226 			/* Head of a list the contains fake directory entries
227 			 * heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD
228 			 * flag. */
229 			struct tmpfs_dir_duphead tn_dupindex;
230 
231 			/* Number and pointer of the first directory entry
232 			 * returned by the readdir operation if it were
233 			 * called again to continue reading data from the
234 			 * same directory as before.  This is used to speed
235 			 * up reads of long directories, assuming that no
236 			 * more than one read is in progress at a given time.
237 			 * Otherwise, these values are discarded. */
238 			off_t			tn_readdir_lastn;
239 			struct tmpfs_dirent *	tn_readdir_lastp;
240 		} tn_dir;
241 
242 		/* Valid when tn_type == VLNK. */
243 		/* The link's target, allocated from a string pool. */
244 		char *			tn_link;
245 
246 		/* Valid when tn_type == VREG. */
247 		struct tn_reg {
248 			/* The contents of regular files stored in a tmpfs
249 			 * file system are represented by a single anonymous
250 			 * memory object (aobj, for short).  The aobj provides
251 			 * direct access to any position within the file,
252 			 * because its contents are always mapped in a
253 			 * contiguous region of virtual memory.  It is a task
254 			 * of the memory management subsystem (see uvm(9)) to
255 			 * issue the required page ins or page outs whenever
256 			 * a position within the file is accessed. */
257 			vm_object_t		tn_aobj;
258 
259 		}tn_reg;
260 
261 		/* Valid when tn_type = VFIFO */
262 		struct tn_fifo {
263 			fo_rdwr_t		*tn_fo_read;
264 			fo_rdwr_t		*tn_fo_write;
265 		}tn_fifo;
266 	}tn_spec;
267 };
268 LIST_HEAD(tmpfs_node_list, tmpfs_node);
269 
270 #define tn_rdev tn_spec.tn_rdev
271 #define tn_dir tn_spec.tn_dir
272 #define tn_link tn_spec.tn_link
273 #define tn_reg tn_spec.tn_reg
274 #define tn_fifo tn_spec.tn_fifo
275 
276 #define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock)
277 #define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock)
278 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
279 #define	TMPFS_NODE_ASSERT_LOCKED(node) mtx_assert(TMPFS_NODE_MTX(node), \
280     MA_OWNED)
281 
282 #ifdef INVARIANTS
283 #define TMPFS_ASSERT_LOCKED(node) do {					\
284 		MPASS(node != NULL);					\
285 		MPASS(node->tn_vnode != NULL);				\
286 		if (!VOP_ISLOCKED(node->tn_vnode) &&			\
287 		    !mtx_owned(TMPFS_NODE_MTX(node)))			\
288 			panic("tmpfs: node is not locked: %p", node);	\
289 	} while (0)
290 #define TMPFS_ASSERT_ELOCKED(node) do {					\
291 		MPASS((node) != NULL);					\
292 		MPASS((node)->tn_vnode != NULL);			\
293 		mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED);		\
294 		ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs");		\
295 	} while (0)
296 #else
297 #define TMPFS_ASSERT_LOCKED(node) (void)0
298 #define TMPFS_ASSERT_ELOCKED(node) (void)0
299 #endif
300 
301 #define TMPFS_VNODE_ALLOCATING	1
302 #define TMPFS_VNODE_WANT	2
303 #define TMPFS_VNODE_DOOMED	4
304 #define	TMPFS_VNODE_WRECLAIM	8
305 
306 /*
307  * Internal representation of a tmpfs mount point.
308  */
309 struct tmpfs_mount {
310 	/* Maximum number of memory pages available for use by the file
311 	 * system, set during mount time.  This variable must never be
312 	 * used directly as it may be bigger than the current amount of
313 	 * free memory; in the extreme case, it will hold the SIZE_MAX
314 	 * value. */
315 	size_t			tm_pages_max;
316 
317 	/* Number of pages in use by the file system. */
318 	size_t			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 	/* unrhdr used to allocate inode numbers */
333 	struct unrhdr *		tm_ino_unr;
334 
335 	/* Number of nodes currently that are in use. */
336 	ino_t			tm_nodes_inuse;
337 
338 	/* maximum representable file size */
339 	u_int64_t		tm_maxfilesize;
340 
341 	/* Nodes are organized in two different lists.  The used list
342 	 * contains all nodes that are currently used by the file system;
343 	 * i.e., they refer to existing files.  The available list contains
344 	 * all nodes that are currently available for use by new files.
345 	 * Nodes must be kept in this list (instead of deleting them)
346 	 * because we need to keep track of their generation number (tn_gen
347 	 * field).
348 	 *
349 	 * Note that nodes are lazily allocated: if the available list is
350 	 * empty and we have enough space to create more nodes, they will be
351 	 * created and inserted in the used list.  Once these are released,
352 	 * they will go into the available list, remaining alive until the
353 	 * file system is unmounted. */
354 	struct tmpfs_node_list	tm_nodes_used;
355 
356 	/* All node lock to protect the node list and tmp_pages_used */
357 	struct mtx allnode_lock;
358 
359 	/* Pools used to store file system meta data.  These are not shared
360 	 * across several instances of tmpfs for the reasons described in
361 	 * tmpfs_pool.c. */
362 	uma_zone_t		tm_dirent_pool;
363 	uma_zone_t		tm_node_pool;
364 
365 	/* Read-only status. */
366 	int			tm_ronly;
367 };
368 #define TMPFS_LOCK(tm) mtx_lock(&(tm)->allnode_lock)
369 #define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->allnode_lock)
370 
371 /*
372  * This structure maps a file identifier to a tmpfs node.  Used by the
373  * NFS code.
374  */
375 struct tmpfs_fid {
376 	uint16_t		tf_len;
377 	uint16_t		tf_pad;
378 	ino_t			tf_id;
379 	unsigned long		tf_gen;
380 };
381 
382 #ifdef _KERNEL
383 /*
384  * Prototypes for tmpfs_subr.c.
385  */
386 
387 int	tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *, enum vtype,
388 	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
389 	    char *, dev_t, struct tmpfs_node **);
390 void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
391 int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
392 	    const char *, u_int, struct tmpfs_dirent **);
393 void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
394 void	tmpfs_dirent_init(struct tmpfs_dirent *, const char *, u_int);
395 void	tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj);
396 int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
397 	    struct vnode **);
398 void	tmpfs_free_vp(struct vnode *);
399 int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
400 	    struct componentname *, char *);
401 void	tmpfs_check_mtime(struct vnode *);
402 void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
403 void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
404 void	tmpfs_dir_destroy(struct tmpfs_mount *, struct tmpfs_node *);
405 struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
406 			    struct tmpfs_node *f,
407 			    struct componentname *cnp);
408 int	tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, int,
409 	    u_long *, int *);
410 int	tmpfs_dir_whiteout_add(struct vnode *, struct componentname *);
411 void	tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *);
412 int	tmpfs_reg_resize(struct vnode *, off_t, boolean_t);
413 int	tmpfs_chflags(struct vnode *, u_long, struct ucred *, struct thread *);
414 int	tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *);
415 int	tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
416 	    struct thread *);
417 int	tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *);
418 int	tmpfs_chtimes(struct vnode *, struct vattr *, struct ucred *cred,
419 	    struct thread *);
420 void	tmpfs_itimes(struct vnode *, const struct timespec *,
421 	    const struct timespec *);
422 
423 void	tmpfs_update(struct vnode *);
424 int	tmpfs_truncate(struct vnode *, off_t);
425 
426 /*
427  * Convenience macros to simplify some logical expressions.
428  */
429 #define IMPLIES(a, b) (!(a) || (b))
430 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
431 
432 /*
433  * Checks that the directory entry pointed by 'de' matches the name 'name'
434  * with a length of 'len'.
435  */
436 #define TMPFS_DIRENT_MATCHES(de, name, len) \
437     (de->td_namelen == len && \
438     bcmp((de)->ud.td_name, (name), (de)->td_namelen) == 0)
439 
440 /*
441  * Ensures that the node pointed by 'node' is a directory and that its
442  * contents are consistent with respect to directories.
443  */
444 #define TMPFS_VALIDATE_DIR(node) do { \
445 	MPASS((node)->tn_type == VDIR); \
446 	MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
447 } while (0)
448 
449 /*
450  * Memory management stuff.
451  */
452 
453 /*
454  * Amount of memory pages to reserve for the system (e.g., to not use by
455  * tmpfs).
456  */
457 #define TMPFS_PAGES_MINRESERVED		(4 * 1024 * 1024 / PAGE_SIZE)
458 
459 size_t tmpfs_mem_avail(void);
460 
461 size_t tmpfs_pages_used(struct tmpfs_mount *tmp);
462 
463 #endif
464 
465 /*
466  * Macros/functions to convert from generic data structures to tmpfs
467  * specific ones.
468  */
469 
470 static inline
471 struct tmpfs_mount *
472 VFS_TO_TMPFS(struct mount *mp)
473 {
474 	struct tmpfs_mount *tmp;
475 
476 	MPASS((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 	MPASS((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 #endif /* _FS_TMPFS_TMPFS_H_ */
504