xref: /freebsd-11-stable/sys/fs/tmpfs/tmpfs.h (revision 5e40dae4f247c68cdd5d31b9de0f171e0e6bab1d)
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 	/*
84 	 * Pointer to the node this entry refers to.  In case this field
85 	 * is NULL, the node is a whiteout.
86 	 */
87 	struct tmpfs_node *		td_node;
88 
89 	union {
90 		/*
91 		 * The name of the entry, allocated from a string pool.  This
92 		 * string is not required to be zero-terminated.
93 		 */
94 		char *			td_name;	/* regular, dup */
95 		struct tmpfs_dir_duphead td_duphead;	/* duphead */
96 	} ud;
97 };
98 
99 /*
100  * A directory in tmpfs holds a collection of directory entries, which
101  * in turn point to other files (which can be directories themselves).
102  *
103  * In tmpfs, this collection is managed by a RB-Tree, whose head is
104  * defined by the struct tmpfs_dir type.
105  *
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.
112  */
113 RB_HEAD(tmpfs_dir, tmpfs_dirent);
114 
115 /*
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.
119  *
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.
124  *
125  * To preserve compatibility cookies are limited to 31 bits.
126  */
127 
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)
138 
139 /*
140  * Internal representation of a tmpfs file system node.
141  *
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.
146  *
147  * Below is the key of locks used to protected the fields in the following
148  * structures.
149  * (v)  vnode lock in exclusive mode
150  * (vi) vnode lock in exclusive mode, or vnode lock in shared vnode and
151  *	tn_interlock
152  * (i)  tn_interlock
153  * (m)  tmpfs_mount tm_allnode_lock
154  * (c)  stable after creation
155  */
156 struct tmpfs_node {
157 	/*
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().  tn_attached is false
162 	 * when the node is removed from list and unlocked.
163 	 */
164 	LIST_ENTRY(tmpfs_node)	tn_entries;	/* (m) */
165 	bool			tn_attached;	/* (m) */
166 
167 	/*
168 	 * The node's type.  Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
169 	 * 'VLNK', 'VREG' and 'VSOCK' is allowed.  The usage of vnode
170 	 * types instead of a custom enumeration is to make things simpler
171 	 * and faster, as we do not need to convert between two types.
172 	 */
173 	enum vtype		tn_type;	/* (c) */
174 
175 	/* Node identifier. */
176 	ino_t			tn_id;		/* (c) */
177 
178 	/*
179 	 * Node's internal status.  This is used by several file system
180 	 * operations to do modifications to the node in a delayed
181 	 * fashion.
182 	 */
183 	int			tn_status;	/* (vi) */
184 #define	TMPFS_NODE_ACCESSED	(1 << 1)
185 #define	TMPFS_NODE_MODIFIED	(1 << 2)
186 #define	TMPFS_NODE_CHANGED	(1 << 3)
187 
188 	/*
189 	 * The node size.  It does not necessarily match the real amount
190 	 * of memory consumed by it.
191 	 */
192 	off_t			tn_size;	/* (v) */
193 
194 	/* Generic node attributes. */
195 	uid_t			tn_uid;		/* (v) */
196 	gid_t			tn_gid;		/* (v) */
197 	mode_t			tn_mode;	/* (v) */
198 	u_long			tn_flags;	/* (v) */
199 	nlink_t			tn_links;	/* (v) */
200 	struct timespec		tn_atime;	/* (vi) */
201 	struct timespec		tn_mtime;	/* (vi) */
202 	struct timespec		tn_ctime;	/* (vi) */
203 	struct timespec		tn_birthtime;	/* (v) */
204 	unsigned long		tn_gen;		/* (c) */
205 
206 	/*
207 	 * As there is a single vnode for each active file within the
208 	 * system, care has to be taken to avoid allocating more than one
209 	 * vnode per file.  In order to do this, a bidirectional association
210 	 * is kept between vnodes and nodes.
211 	 *
212 	 * Whenever a vnode is allocated, its v_data field is updated to
213 	 * point to the node it references.  At the same time, the node's
214 	 * tn_vnode field is modified to point to the new vnode representing
215 	 * it.  Further attempts to allocate a vnode for this same node will
216 	 * result in returning a new reference to the value stored in
217 	 * tn_vnode.
218 	 *
219 	 * May be NULL when the node is unused (that is, no vnode has been
220 	 * allocated for it or it has been reclaimed).
221 	 */
222 	struct vnode *		tn_vnode;	/* (i) */
223 
224 	/*
225 	 * Interlock to protect tn_vpstate, and tn_status under shared
226 	 * vnode lock.
227 	 */
228 	struct mtx	tn_interlock;
229 
230 	/*
231 	 * Identify if current node has vnode assiocate with
232 	 * or allocating vnode.
233 	 */
234 	int		tn_vpstate;		/* (i) */
235 
236 	/* Transient refcounter on this node. */
237 	u_int		tn_refcount;		/* (m) + (i) */
238 
239 	/* misc data field for different tn_type node */
240 	union {
241 		/* Valid when tn_type == VBLK || tn_type == VCHR. */
242 		dev_t			tn_rdev;	/* (c) */
243 
244 		/* Valid when tn_type == VDIR. */
245 		struct tn_dir {
246 			/*
247 			 * Pointer to the parent directory.  The root
248 			 * directory has a pointer to itself in this field;
249 			 * this property identifies the root node.
250 			 */
251 			struct tmpfs_node *	tn_parent;
252 
253 			/*
254 			 * Head of a tree that links the contents of
255 			 * the directory together.
256 			 */
257 			struct tmpfs_dir	tn_dirhead;
258 
259 			/*
260 			 * Head of a list the contains fake directory entries
261 			 * heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD
262 			 * flag.
263 			 */
264 			struct tmpfs_dir_duphead tn_dupindex;
265 
266 			/*
267 			 * Number and pointer of the first directory entry
268 			 * returned by the readdir operation if it were
269 			 * called again to continue reading data from the
270 			 * same directory as before.  This is used to speed
271 			 * up reads of long directories, assuming that no
272 			 * more than one read is in progress at a given time.
273 			 * Otherwise, these values are discarded.
274 			 */
275 			off_t			tn_readdir_lastn;
276 			struct tmpfs_dirent *	tn_readdir_lastp;
277 		} tn_dir;
278 
279 		/* Valid when tn_type == VLNK. */
280 		/* The link's target, allocated from a string pool. */
281 		char *			tn_link;	/* (c) */
282 
283 		/* Valid when tn_type == VREG. */
284 		struct tn_reg {
285 			/*
286 			 * The contents of regular files stored in a
287 			 * tmpfs file system are represented by a
288 			 * single anonymous memory object (aobj, for
289 			 * short).  The aobj provides direct access to
290 			 * any position within the file.  It is a task
291 			 * of the memory management subsystem to issue
292 			 * the required page ins or page outs whenever
293 			 * a position within the file is accessed.
294 			 */
295 			vm_object_t		tn_aobj;	/* (c) */
296 		} tn_reg;
297 	} tn_spec;	/* (v) */
298 };
299 LIST_HEAD(tmpfs_node_list, tmpfs_node);
300 
301 #define tn_rdev tn_spec.tn_rdev
302 #define tn_dir tn_spec.tn_dir
303 #define tn_link tn_spec.tn_link
304 #define tn_reg tn_spec.tn_reg
305 #define tn_fifo tn_spec.tn_fifo
306 
307 #define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock)
308 #define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock)
309 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
310 #define	TMPFS_NODE_ASSERT_LOCKED(node) mtx_assert(TMPFS_NODE_MTX(node), \
311     MA_OWNED)
312 
313 #ifdef INVARIANTS
314 #define TMPFS_ASSERT_LOCKED(node) do {					\
315 		MPASS((node) != NULL);					\
316 		MPASS((node)->tn_vnode != NULL);			\
317 		ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs assert");	\
318 	} while (0)
319 #else
320 #define TMPFS_ASSERT_LOCKED(node) (void)0
321 #endif
322 
323 #define TMPFS_VNODE_ALLOCATING	1
324 #define TMPFS_VNODE_WANT	2
325 #define TMPFS_VNODE_DOOMED	4
326 #define	TMPFS_VNODE_WRECLAIM	8
327 
328 /*
329  * Internal representation of a tmpfs mount point.
330  */
331 struct tmpfs_mount {
332 	/*
333 	 * Original value of the "size" parameter, for reference purposes,
334 	 * mostly.
335 	 */
336 	off_t			tm_size_max;
337 	/*
338 	 * Maximum number of memory pages available for use by the file
339 	 * system, set during mount time.  This variable must never be
340 	 * used directly as it may be bigger than the current amount of
341 	 * free memory; in the extreme case, it will hold the ULONG_MAX
342 	 * value.
343 	 */
344 	u_long			tm_pages_max;
345 
346 	/* Number of pages in use by the file system. */
347 	u_long			tm_pages_used;
348 
349 	/*
350 	 * Pointer to the node representing the root directory of this
351 	 * file system.
352 	 */
353 	struct tmpfs_node *	tm_root;
354 
355 	/*
356 	 * Maximum number of possible nodes for this file system; set
357 	 * during mount time.  We need a hard limit on the maximum number
358 	 * of nodes to avoid allocating too much of them; their objects
359 	 * cannot be released until the file system is unmounted.
360 	 * Otherwise, we could easily run out of memory by creating lots
361 	 * of empty files and then simply removing them.
362 	 */
363 	ino_t			tm_nodes_max;
364 
365 	/* unrhdr used to allocate inode numbers */
366 	struct unrhdr *		tm_ino_unr;
367 
368 	/* Number of nodes currently that are in use. */
369 	ino_t			tm_nodes_inuse;
370 
371 	/* Refcounter on this struct tmpfs_mount. */
372 	uint64_t		tm_refcount;
373 
374 	/* maximum representable file size */
375 	u_int64_t		tm_maxfilesize;
376 
377 	/*
378 	 * The used list contains all nodes that are currently used by
379 	 * the file system; i.e., they refer to existing files.
380 	 */
381 	struct tmpfs_node_list	tm_nodes_used;
382 
383 	/* All node lock to protect the node list and tmp_pages_used. */
384 	struct mtx		tm_allnode_lock;
385 
386 	/* Zones used to store file system meta data, per tmpfs mount. */
387 	uma_zone_t		tm_dirent_pool;
388 	uma_zone_t		tm_node_pool;
389 
390 	/* Read-only status. */
391 	bool			tm_ronly;
392 	/* Do not use namecache. */
393 	bool			tm_nonc;
394 };
395 #define	TMPFS_LOCK(tm) mtx_lock(&(tm)->tm_allnode_lock)
396 #define	TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->tm_allnode_lock)
397 #define	TMPFS_MP_ASSERT_LOCKED(tm) mtx_assert(&(tm)->tm_allnode_lock, MA_OWNED)
398 
399 /*
400  * This structure maps a file identifier to a tmpfs node.  Used by the
401  * NFS code.
402  */
403 struct tmpfs_fid {
404 	uint16_t		tf_len;
405 	uint16_t		tf_pad;
406 	ino_t			tf_id;
407 	unsigned long		tf_gen;
408 };
409 
410 struct tmpfs_dir_cursor {
411 	struct tmpfs_dirent	*tdc_current;
412 	struct tmpfs_dirent	*tdc_tree;
413 };
414 
415 #ifdef _KERNEL
416 /*
417  * Prototypes for tmpfs_subr.c.
418  */
419 
420 void	tmpfs_ref_node(struct tmpfs_node *node);
421 void	tmpfs_ref_node_locked(struct tmpfs_node *node);
422 int	tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *, enum vtype,
423 	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
424 	    char *, dev_t, struct tmpfs_node **);
425 void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
426 bool	tmpfs_free_node_locked(struct tmpfs_mount *, struct tmpfs_node *, bool);
427 void	tmpfs_free_tmp(struct tmpfs_mount *);
428 int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
429 	    const char *, u_int, struct tmpfs_dirent **);
430 void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
431 void	tmpfs_dirent_init(struct tmpfs_dirent *, const char *, u_int);
432 void	tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj);
433 int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
434 	    struct vnode **);
435 void	tmpfs_free_vp(struct vnode *);
436 int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
437 	    struct componentname *, char *);
438 void	tmpfs_check_mtime(struct vnode *);
439 void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
440 void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
441 void	tmpfs_dir_destroy(struct tmpfs_mount *, struct tmpfs_node *);
442 struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
443 			    struct tmpfs_node *f,
444 			    struct componentname *cnp);
445 int	tmpfs_dir_getdents(struct tmpfs_mount *, struct tmpfs_node *,
446 	    struct uio *, int, u_long *, int *);
447 int	tmpfs_dir_whiteout_add(struct vnode *, struct componentname *);
448 void	tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *);
449 int	tmpfs_reg_resize(struct vnode *, off_t, boolean_t);
450 int	tmpfs_chflags(struct vnode *, u_long, struct ucred *, struct thread *);
451 int	tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *);
452 int	tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
453 	    struct thread *);
454 int	tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *);
455 int	tmpfs_chtimes(struct vnode *, struct vattr *, struct ucred *cred,
456 	    struct thread *);
457 void	tmpfs_itimes(struct vnode *, const struct timespec *,
458 	    const struct timespec *);
459 
460 void	tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node,
461 	    int status);
462 void	tmpfs_update(struct vnode *);
463 int	tmpfs_truncate(struct vnode *, off_t);
464 struct tmpfs_dirent *tmpfs_dir_first(struct tmpfs_node *dnode,
465 	    struct tmpfs_dir_cursor *dc);
466 struct tmpfs_dirent *tmpfs_dir_next(struct tmpfs_node *dnode,
467 	    struct tmpfs_dir_cursor *dc);
468 
469 /*
470  * Convenience macros to simplify some logical expressions.
471  */
472 #define IMPLIES(a, b) (!(a) || (b))
473 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
474 
475 /*
476  * Checks that the directory entry pointed by 'de' matches the name 'name'
477  * with a length of 'len'.
478  */
479 #define TMPFS_DIRENT_MATCHES(de, name, len) \
480     (de->td_namelen == len && \
481     bcmp((de)->ud.td_name, (name), (de)->td_namelen) == 0)
482 
483 /*
484  * Ensures that the node pointed by 'node' is a directory and that its
485  * contents are consistent with respect to directories.
486  */
487 #define TMPFS_VALIDATE_DIR(node) do { \
488 	MPASS((node)->tn_type == VDIR); \
489 	MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
490 } while (0)
491 
492 /*
493  * Amount of memory pages to reserve for the system (e.g., to not use by
494  * tmpfs).
495  */
496 #define TMPFS_PAGES_MINRESERVED		(4 * 1024 * 1024 / PAGE_SIZE)
497 
498 size_t tmpfs_mem_avail(void);
499 
500 size_t tmpfs_pages_used(struct tmpfs_mount *tmp);
501 
502 /*
503  * Macros/functions to convert from generic data structures to tmpfs
504  * specific ones.
505  */
506 
507 static inline struct tmpfs_mount *
VFS_TO_TMPFS(struct mount * mp)508 VFS_TO_TMPFS(struct mount *mp)
509 {
510 	struct tmpfs_mount *tmp;
511 
512 	MPASS(mp != NULL && mp->mnt_data != NULL);
513 	tmp = (struct tmpfs_mount *)mp->mnt_data;
514 	return (tmp);
515 }
516 
517 static inline struct tmpfs_node *
VP_TO_TMPFS_NODE(struct vnode * vp)518 VP_TO_TMPFS_NODE(struct vnode *vp)
519 {
520 	struct tmpfs_node *node;
521 
522 	MPASS(vp != NULL && vp->v_data != NULL);
523 	node = (struct tmpfs_node *)vp->v_data;
524 	return (node);
525 }
526 
527 static inline struct tmpfs_node *
VP_TO_TMPFS_DIR(struct vnode * vp)528 VP_TO_TMPFS_DIR(struct vnode *vp)
529 {
530 	struct tmpfs_node *node;
531 
532 	node = VP_TO_TMPFS_NODE(vp);
533 	TMPFS_VALIDATE_DIR(node);
534 	return (node);
535 }
536 
537 static inline bool
tmpfs_use_nc(struct vnode * vp)538 tmpfs_use_nc(struct vnode *vp)
539 {
540 
541 	return (!(VFS_TO_TMPFS(vp->v_mount)->tm_nonc));
542 }
543 #endif /* _KERNEL */
544 
545 #endif /* _FS_TMPFS_TMPFS_H_ */
546