xref: /freebsd/sys/fs/tmpfs/tmpfs.h (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*	$NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11  * 2005 program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $FreeBSD$
35  */
36 
37 #ifndef _FS_TMPFS_TMPFS_H_
38 #define _FS_TMPFS_TMPFS_H_
39 
40 #include <sys/queue.h>
41 #include <sys/tree.h>
42 
43 #ifdef	_SYS_MALLOC_H_
44 MALLOC_DECLARE(M_TMPFSMNT);
45 MALLOC_DECLARE(M_TMPFSNAME);
46 #endif
47 
48 /*
49  * Internal representation of a tmpfs directory entry.
50  */
51 
52 LIST_HEAD(tmpfs_dir_duphead, tmpfs_dirent);
53 
54 struct tmpfs_dirent {
55 	/*
56 	 * Depending on td_cookie flag entry can be of 3 types:
57 	 * - regular -- no hash collisions, stored in RB-Tree
58 	 * - duphead -- synthetic linked list head for dup entries
59 	 * - dup -- stored in linked list instead of RB-Tree
60 	 */
61 	union {
62 		/* regular and duphead entry types */
63 		RB_ENTRY(tmpfs_dirent)		td_entries;
64 
65 		/* dup entry type */
66 		struct {
67 			LIST_ENTRY(tmpfs_dirent) entries;
68 			LIST_ENTRY(tmpfs_dirent) index_entries;
69 		} td_dup;
70 	} uh;
71 
72 	uint32_t			td_cookie;
73 	uint32_t			td_hash;
74 	u_int				td_namelen;
75 
76 	/*
77 	 * Pointer to the node this entry refers to.  In case this field
78 	 * is NULL, the node is a whiteout.
79 	 */
80 	struct tmpfs_node *		td_node;
81 
82 	union {
83 		/*
84 		 * The name of the entry, allocated from a string pool.  This
85 		 * string is not required to be zero-terminated.
86 		 */
87 		char *			td_name;	/* regular, dup */
88 		struct tmpfs_dir_duphead td_duphead;	/* duphead */
89 	} ud;
90 };
91 
92 /*
93  * A directory in tmpfs holds a collection of directory entries, which
94  * in turn point to other files (which can be directories themselves).
95  *
96  * In tmpfs, this collection is managed by a RB-Tree, whose head is
97  * defined by the struct tmpfs_dir type.
98  *
99  * It is important to notice that directories do not have entries for . and
100  * .. as other file systems do.  These can be generated when requested
101  * based on information available by other means, such as the pointer to
102  * the node itself in the former case or the pointer to the parent directory
103  * in the latter case.  This is done to simplify tmpfs's code and, more
104  * importantly, to remove redundancy.
105  */
106 RB_HEAD(tmpfs_dir, tmpfs_dirent);
107 
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.
112  *
113  * The '.', '..' and the end of directory markers have fixed cookies which
114  * cannot collide with the cookies generated by other entries.  The cookies
115  * for the other entries are generated based on the file name hash value or
116  * unique number in case of name hash collision.
117  *
118  * To preserve compatibility cookies are limited to 31 bits.
119  */
120 
121 #define	TMPFS_DIRCOOKIE_DOT		0
122 #define	TMPFS_DIRCOOKIE_DOTDOT		1
123 #define	TMPFS_DIRCOOKIE_EOF		2
124 #define	TMPFS_DIRCOOKIE_MASK		((off_t)0x3fffffffU)
125 #define	TMPFS_DIRCOOKIE_MIN		((off_t)0x00000004U)
126 #define	TMPFS_DIRCOOKIE_DUP		((off_t)0x40000000U)
127 #define	TMPFS_DIRCOOKIE_DUPHEAD		((off_t)0x80000000U)
128 #define	TMPFS_DIRCOOKIE_DUP_MIN		TMPFS_DIRCOOKIE_DUP
129 #define	TMPFS_DIRCOOKIE_DUP_MAX		\
130 	(TMPFS_DIRCOOKIE_DUP | TMPFS_DIRCOOKIE_MASK)
131 
132 /*
133  * Internal representation of a tmpfs file system node.
134  *
135  * This structure is splitted in two parts: one holds attributes common
136  * to all file types and the other holds data that is only applicable to
137  * a particular type.  The code must be careful to only access those
138  * attributes that are actually allowed by the node's type.
139  *
140  * Below is the key of locks used to protected the fields in the following
141  * structures.
142  * (v)  vnode lock in exclusive mode
143  * (vi) vnode lock in exclusive mode, or vnode lock in shared vnode and
144  *	tn_interlock
145  * (i)  tn_interlock
146  * (m)  tmpfs_mount tm_allnode_lock
147  * (c)  stable after creation
148  */
149 struct tmpfs_node {
150 	/*
151 	 * Doubly-linked list entry which links all existing nodes for
152 	 * a single file system.  This is provided to ease the removal
153 	 * of all nodes during the unmount operation, and to support
154 	 * the implementation of VOP_VNTOCNP().  tn_attached is false
155 	 * when the node is removed from list and unlocked.
156 	 */
157 	LIST_ENTRY(tmpfs_node)	tn_entries;	/* (m) */
158 	bool			tn_attached;	/* (m) */
159 
160 	/*
161 	 * The node's type.  Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
162 	 * 'VLNK', 'VREG' and 'VSOCK' is allowed.  The usage of vnode
163 	 * types instead of a custom enumeration is to make things simpler
164 	 * and faster, as we do not need to convert between two types.
165 	 */
166 	enum vtype		tn_type;	/* (c) */
167 
168 	/* Node identifier. */
169 	ino_t			tn_id;		/* (c) */
170 
171 	/*
172 	 * Node's internal status.  This is used by several file system
173 	 * operations to do modifications to the node in a delayed
174 	 * fashion.
175 	 */
176 	int			tn_status;	/* (vi) */
177 #define	TMPFS_NODE_ACCESSED	(1 << 1)
178 #define	TMPFS_NODE_MODIFIED	(1 << 2)
179 #define	TMPFS_NODE_CHANGED	(1 << 3)
180 
181 	/*
182 	 * The node size.  It does not necessarily match the real amount
183 	 * of memory consumed by it.
184 	 */
185 	off_t			tn_size;	/* (v) */
186 
187 	/* Generic node attributes. */
188 	uid_t			tn_uid;		/* (v) */
189 	gid_t			tn_gid;		/* (v) */
190 	mode_t			tn_mode;	/* (v) */
191 	int			tn_links;	/* (v) */
192 	u_long			tn_flags;	/* (v) */
193 	struct timespec		tn_atime;	/* (vi) */
194 	struct timespec		tn_mtime;	/* (vi) */
195 	struct timespec		tn_ctime;	/* (vi) */
196 	struct timespec		tn_birthtime;	/* (v) */
197 	unsigned long		tn_gen;		/* (c) */
198 
199 	/*
200 	 * As there is a single vnode for each active file within the
201 	 * system, care has to be taken to avoid allocating more than one
202 	 * vnode per file.  In order to do this, a bidirectional association
203 	 * is kept between vnodes and nodes.
204 	 *
205 	 * Whenever a vnode is allocated, its v_data field is updated to
206 	 * point to the node it references.  At the same time, the node's
207 	 * tn_vnode field is modified to point to the new vnode representing
208 	 * it.  Further attempts to allocate a vnode for this same node will
209 	 * result in returning a new reference to the value stored in
210 	 * tn_vnode.
211 	 *
212 	 * May be NULL when the node is unused (that is, no vnode has been
213 	 * allocated for it or it has been reclaimed).
214 	 */
215 	struct vnode *		tn_vnode;	/* (i) */
216 
217 	/*
218 	 * Interlock to protect tn_vpstate, and tn_status under shared
219 	 * vnode lock.
220 	 */
221 	struct mtx	tn_interlock;
222 
223 	/*
224 	 * Identify if current node has vnode assiocate with
225 	 * or allocating vnode.
226 	 */
227 	int		tn_vpstate;		/* (i) */
228 
229 	/* Transient refcounter on this node. */
230 	u_int		tn_refcount;		/* (m) + (i) */
231 
232 	/* misc data field for different tn_type node */
233 	union {
234 		/* Valid when tn_type == VBLK || tn_type == VCHR. */
235 		dev_t			tn_rdev;	/* (c) */
236 
237 		/* Valid when tn_type == VDIR. */
238 		struct tn_dir {
239 			/*
240 			 * Pointer to the parent directory.  The root
241 			 * directory has a pointer to itself in this field;
242 			 * this property identifies the root node.
243 			 */
244 			struct tmpfs_node *	tn_parent;
245 
246 			/*
247 			 * Head of a tree that links the contents of
248 			 * the directory together.
249 			 */
250 			struct tmpfs_dir	tn_dirhead;
251 
252 			/*
253 			 * Head of a list the contains fake directory entries
254 			 * heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD
255 			 * flag.
256 			 */
257 			struct tmpfs_dir_duphead tn_dupindex;
258 
259 			/*
260 			 * Number and pointer of the first directory entry
261 			 * returned by the readdir operation if it were
262 			 * called again to continue reading data from the
263 			 * same directory as before.  This is used to speed
264 			 * up reads of long directories, assuming that no
265 			 * more than one read is in progress at a given time.
266 			 * Otherwise, these values are discarded.
267 			 */
268 			off_t			tn_readdir_lastn;
269 			struct tmpfs_dirent *	tn_readdir_lastp;
270 		} tn_dir;
271 
272 		/* Valid when tn_type == VLNK. */
273 		/* The link's target, allocated from a string pool. */
274 		char *			tn_link;	/* (c) */
275 
276 		/* Valid when tn_type == VREG. */
277 		struct tn_reg {
278 			/*
279 			 * The contents of regular files stored in a
280 			 * tmpfs file system are represented by a
281 			 * single anonymous memory object (aobj, for
282 			 * short).  The aobj provides direct access to
283 			 * any position within the file.  It is a task
284 			 * of the memory management subsystem to issue
285 			 * the required page ins or page outs whenever
286 			 * a position within the file is accessed.
287 			 */
288 			vm_object_t		tn_aobj;	/* (c) */
289 		} tn_reg;
290 	} tn_spec;	/* (v) */
291 };
292 LIST_HEAD(tmpfs_node_list, tmpfs_node);
293 
294 #define tn_rdev tn_spec.tn_rdev
295 #define tn_dir tn_spec.tn_dir
296 #define tn_link tn_spec.tn_link
297 #define tn_reg tn_spec.tn_reg
298 #define tn_fifo tn_spec.tn_fifo
299 
300 #define	TMPFS_LINK_MAX INT_MAX
301 
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), \
306     MA_OWNED)
307 
308 #ifdef INVARIANTS
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");	\
313 	} while (0)
314 #else
315 #define TMPFS_ASSERT_LOCKED(node) (void)0
316 #endif
317 
318 #define TMPFS_VNODE_ALLOCATING	1
319 #define TMPFS_VNODE_WANT	2
320 #define TMPFS_VNODE_DOOMED	4
321 #define	TMPFS_VNODE_WRECLAIM	8
322 
323 /*
324  * Internal representation of a tmpfs mount point.
325  */
326 struct tmpfs_mount {
327 	/*
328 	 * Original value of the "size" parameter, for reference purposes,
329 	 * mostly.
330 	 */
331 	off_t			tm_size_max;
332 	/*
333 	 * Maximum number of memory pages available for use by the file
334 	 * system, set during mount time.  This variable must never be
335 	 * used directly as it may be bigger than the current amount of
336 	 * free memory; in the extreme case, it will hold the ULONG_MAX
337 	 * value.
338 	 */
339 	u_long			tm_pages_max;
340 
341 	/* Number of pages in use by the file system. */
342 	u_long			tm_pages_used;
343 
344 	/*
345 	 * Pointer to the node representing the root directory of this
346 	 * file system.
347 	 */
348 	struct tmpfs_node *	tm_root;
349 
350 	/*
351 	 * Maximum number of possible nodes for this file system; set
352 	 * during mount time.  We need a hard limit on the maximum number
353 	 * of nodes to avoid allocating too much of them; their objects
354 	 * cannot be released until the file system is unmounted.
355 	 * Otherwise, we could easily run out of memory by creating lots
356 	 * of empty files and then simply removing them.
357 	 */
358 	ino_t			tm_nodes_max;
359 
360 	/* unrhdr used to allocate inode numbers */
361 	struct unrhdr64		tm_ino_unr;
362 
363 	/* Number of nodes currently that are in use. */
364 	ino_t			tm_nodes_inuse;
365 
366 	/* Refcounter on this struct tmpfs_mount. */
367 	uint64_t		tm_refcount;
368 
369 	/* maximum representable file size */
370 	u_int64_t		tm_maxfilesize;
371 
372 	/*
373 	 * The used list contains all nodes that are currently used by
374 	 * the file system; i.e., they refer to existing files.
375 	 */
376 	struct tmpfs_node_list	tm_nodes_used;
377 
378 	/* All node lock to protect the node list and tmp_pages_used. */
379 	struct mtx		tm_allnode_lock;
380 
381 	/* Read-only status. */
382 	bool			tm_ronly;
383 	/* Do not use namecache. */
384 	bool			tm_nonc;
385 	/* Do not update mtime on writes through mmaped areas. */
386 	bool			tm_nomtime;
387 };
388 #define	TMPFS_LOCK(tm) mtx_lock(&(tm)->tm_allnode_lock)
389 #define	TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->tm_allnode_lock)
390 #define	TMPFS_MP_ASSERT_LOCKED(tm) mtx_assert(&(tm)->tm_allnode_lock, MA_OWNED)
391 
392 /*
393  * This structure maps a file identifier to a tmpfs node.  Used by the
394  * NFS code.
395  */
396 struct tmpfs_fid {
397 	uint16_t		tf_len;
398 	uint16_t		tf_pad;
399 	ino_t			tf_id;
400 	unsigned long		tf_gen;
401 };
402 
403 struct tmpfs_dir_cursor {
404 	struct tmpfs_dirent	*tdc_current;
405 	struct tmpfs_dirent	*tdc_tree;
406 };
407 
408 #ifdef _KERNEL
409 /*
410  * Prototypes for tmpfs_subr.c.
411  */
412 
413 void	tmpfs_ref_node(struct tmpfs_node *node);
414 void	tmpfs_ref_node_locked(struct tmpfs_node *node);
415 int	tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *, enum vtype,
416 	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
417 	    const char *, dev_t, struct tmpfs_node **);
418 void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
419 bool	tmpfs_free_node_locked(struct tmpfs_mount *, struct tmpfs_node *, bool);
420 void	tmpfs_free_tmp(struct tmpfs_mount *);
421 int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
422 	    const char *, u_int, struct tmpfs_dirent **);
423 void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
424 void	tmpfs_dirent_init(struct tmpfs_dirent *, const char *, u_int);
425 void	tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj);
426 int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
427 	    struct vnode **);
428 void	tmpfs_free_vp(struct vnode *);
429 int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
430 	    struct componentname *, const char *);
431 void	tmpfs_check_mtime(struct vnode *);
432 void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
433 void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
434 void	tmpfs_dir_destroy(struct tmpfs_mount *, struct tmpfs_node *);
435 struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
436 			    struct tmpfs_node *f,
437 			    struct componentname *cnp);
438 int	tmpfs_dir_getdents(struct tmpfs_mount *, struct tmpfs_node *,
439 	    struct uio *, int, u_long *, int *);
440 int	tmpfs_dir_whiteout_add(struct vnode *, struct componentname *);
441 void	tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *);
442 int	tmpfs_reg_resize(struct vnode *, off_t, boolean_t);
443 int	tmpfs_chflags(struct vnode *, u_long, struct ucred *, struct thread *);
444 int	tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *);
445 int	tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
446 	    struct thread *);
447 int	tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *);
448 int	tmpfs_chtimes(struct vnode *, struct vattr *, struct ucred *cred,
449 	    struct thread *);
450 void	tmpfs_itimes(struct vnode *, const struct timespec *,
451 	    const struct timespec *);
452 
453 void	tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node,
454 	    int status);
455 int	tmpfs_truncate(struct vnode *, off_t);
456 struct tmpfs_dirent *tmpfs_dir_first(struct tmpfs_node *dnode,
457 	    struct tmpfs_dir_cursor *dc);
458 struct tmpfs_dirent *tmpfs_dir_next(struct tmpfs_node *dnode,
459 	    struct tmpfs_dir_cursor *dc);
460 static __inline void
461 tmpfs_update(struct vnode *vp)
462 {
463 
464 	tmpfs_itimes(vp, NULL, NULL);
465 }
466 
467 /*
468  * Convenience macros to simplify some logical expressions.
469  */
470 #define IMPLIES(a, b) (!(a) || (b))
471 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
472 
473 /*
474  * Checks that the directory entry pointed by 'de' matches the name 'name'
475  * with a length of 'len'.
476  */
477 #define TMPFS_DIRENT_MATCHES(de, name, len) \
478     (de->td_namelen == len && \
479     bcmp((de)->ud.td_name, (name), (de)->td_namelen) == 0)
480 
481 /*
482  * Ensures that the node pointed by 'node' is a directory and that its
483  * contents are consistent with respect to directories.
484  */
485 #define TMPFS_VALIDATE_DIR(node) do { \
486 	MPASS((node)->tn_type == VDIR); \
487 	MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
488 } while (0)
489 
490 /*
491  * Amount of memory pages to reserve for the system (e.g., to not use by
492  * tmpfs).
493  */
494 #if !defined(TMPFS_PAGES_MINRESERVED)
495 #define TMPFS_PAGES_MINRESERVED		(4 * 1024 * 1024 / PAGE_SIZE)
496 #endif
497 
498 size_t tmpfs_mem_avail(void);
499 size_t tmpfs_pages_used(struct tmpfs_mount *tmp);
500 void tmpfs_subr_init(void);
501 void tmpfs_subr_uninit(void);
502 
503 /*
504  * Macros/functions to convert from generic data structures to tmpfs
505  * specific ones.
506  */
507 
508 static inline struct tmpfs_mount *
509 VFS_TO_TMPFS(struct mount *mp)
510 {
511 	struct tmpfs_mount *tmp;
512 
513 	MPASS(mp != NULL && mp->mnt_data != NULL);
514 	tmp = (struct tmpfs_mount *)mp->mnt_data;
515 	return (tmp);
516 }
517 
518 static inline struct tmpfs_node *
519 VP_TO_TMPFS_NODE(struct vnode *vp)
520 {
521 	struct tmpfs_node *node;
522 
523 	MPASS(vp != NULL && vp->v_data != NULL);
524 	node = (struct tmpfs_node *)vp->v_data;
525 	return (node);
526 }
527 
528 static inline struct tmpfs_node *
529 VP_TO_TMPFS_DIR(struct vnode *vp)
530 {
531 	struct tmpfs_node *node;
532 
533 	node = VP_TO_TMPFS_NODE(vp);
534 	TMPFS_VALIDATE_DIR(node);
535 	return (node);
536 }
537 
538 static inline bool
539 tmpfs_use_nc(struct vnode *vp)
540 {
541 
542 	return (!(VFS_TO_TMPFS(vp->v_mount)->tm_nonc));
543 }
544 
545 static inline void
546 tmpfs_update_getattr(struct vnode *vp)
547 {
548 	struct tmpfs_node *node;
549 	int update_flags;
550 
551 	update_flags = TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED;
552 
553 	node = VP_TO_TMPFS_NODE(vp);
554 	if (__predict_false(node->tn_status & update_flags) != 0)
555 		tmpfs_update(vp);
556 }
557 
558 #endif /* _KERNEL */
559 
560 #endif /* _FS_TMPFS_TMPFS_H_ */
561