xref: /freebsd/sys/fs/tmpfs/tmpfs.h (revision 2e68c5a4)
1 /*	$NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
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 
35 #ifndef _FS_TMPFS_TMPFS_H_
36 #define _FS_TMPFS_TMPFS_H_
37 
38 #include <sys/cdefs.h>
39 #include <sys/queue.h>
40 #include <sys/tree.h>
41 
42 #ifdef	_SYS_MALLOC_H_
43 MALLOC_DECLARE(M_TMPFSNAME);
44 #endif
45 
46 #define	OBJ_TMPFS	OBJ_PAGERPRIV1	/* has tmpfs vnode allocated */
47 #define	OBJ_TMPFS_VREF	OBJ_PAGERPRIV2	/* vnode is referenced */
48 
49 /*
50  * Internal representation of a tmpfs directory entry.
51  */
52 
53 LIST_HEAD(tmpfs_dir_duphead, tmpfs_dirent);
54 
55 struct tmpfs_dirent {
56 	/*
57 	 * Depending on td_cookie flag entry can be of 3 types:
58 	 * - regular -- no hash collisions, stored in RB-Tree
59 	 * - duphead -- synthetic linked list head for dup entries
60 	 * - dup -- stored in linked list instead of RB-Tree
61 	 */
62 	union {
63 		/* regular and duphead entry types */
64 		RB_ENTRY(tmpfs_dirent)		td_entries;
65 
66 		/* dup entry type */
67 		struct {
68 			LIST_ENTRY(tmpfs_dirent) entries;
69 			LIST_ENTRY(tmpfs_dirent) index_entries;
70 		} td_dup;
71 	} uh;
72 
73 	uint32_t			td_cookie;
74 	uint32_t			td_hash;
75 	u_int				td_namelen;
76 
77 	/*
78 	 * Pointer to the node this entry refers to.  In case this field
79 	 * is NULL, the node is a whiteout.
80 	 */
81 	struct tmpfs_node *		td_node;
82 
83 	union {
84 		/*
85 		 * The name of the entry, allocated from a string pool.  This
86 		 * string is not required to be zero-terminated.
87 		 */
88 		char *			td_name;	/* regular, dup */
89 		struct tmpfs_dir_duphead td_duphead;	/* duphead */
90 	} ud;
91 };
92 
93 /*
94  * A directory in tmpfs holds a collection of directory entries, which
95  * in turn point to other files (which can be directories themselves).
96  *
97  * In tmpfs, this collection is managed by a RB-Tree, whose head is
98  * defined by the struct tmpfs_dir type.
99  *
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.
106  */
107 RB_HEAD(tmpfs_dir, tmpfs_dirent);
108 
109 /*
110  * Each entry in a directory has a cookie that identifies it.  Cookies
111  * supersede offsets within directories because, given how tmpfs stores
112  * directories in memory, there is no such thing as an offset.
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 file name hash value or
117  * unique number in case of name hash collision.
118  *
119  * To preserve compatibility cookies are limited to 31 bits.
120  */
121 
122 #define	TMPFS_DIRCOOKIE_DOT		0
123 #define	TMPFS_DIRCOOKIE_DOTDOT		1
124 #define	TMPFS_DIRCOOKIE_EOF		2
125 #define	TMPFS_DIRCOOKIE_MASK		((off_t)0x3fffffffU)
126 #define	TMPFS_DIRCOOKIE_MIN		((off_t)0x00000004U)
127 #define	TMPFS_DIRCOOKIE_DUP		((off_t)0x40000000U)
128 #define	TMPFS_DIRCOOKIE_DUPHEAD		((off_t)0x80000000U)
129 #define	TMPFS_DIRCOOKIE_DUP_MIN		TMPFS_DIRCOOKIE_DUP
130 #define	TMPFS_DIRCOOKIE_DUP_MAX		\
131 	(TMPFS_DIRCOOKIE_DUP | TMPFS_DIRCOOKIE_MASK)
132 
133 /*
134  * Internal representation of a tmpfs extended attribute entry.
135  */
136 LIST_HEAD(tmpfs_extattr_list, tmpfs_extattr);
137 
138 struct tmpfs_extattr {
139 	LIST_ENTRY(tmpfs_extattr)	ea_extattrs;
140 	int			ea_namespace;	/* attr namespace */
141 	char			*ea_name;	/* attr name */
142 	unsigned char		ea_namelen;	/* attr name length */
143 	char			*ea_value;	/* attr value buffer */
144 	ssize_t			ea_size;	/* attr value size */
145 };
146 
147 /*
148  * Internal representation of a tmpfs file system node.
149  *
150  * This structure is splitted in two parts: one holds attributes common
151  * to all file types and the other holds data that is only applicable to
152  * a particular type.  The code must be careful to only access those
153  * attributes that are actually allowed by the node's type.
154  *
155  * Below is the key of locks used to protected the fields in the following
156  * structures.
157  * (v)  vnode lock in exclusive mode
158  * (vi) vnode lock in exclusive mode, or vnode lock in shared vnode and
159  *	tn_interlock
160  * (i)  tn_interlock
161  * (m)  tmpfs_mount tm_allnode_lock
162  * (c)  stable after creation
163  * (v)  tn_reg.tn_aobj vm_object lock
164  */
165 struct tmpfs_node {
166 	/*
167 	 * Doubly-linked list entry which links all existing nodes for
168 	 * a single file system.  This is provided to ease the removal
169 	 * of all nodes during the unmount operation, and to support
170 	 * the implementation of VOP_VNTOCNP().  tn_attached is false
171 	 * when the node is removed from list and unlocked.
172 	 */
173 	LIST_ENTRY(tmpfs_node)	tn_entries;	/* (m) */
174 
175 	/* Node identifier. */
176 	ino_t			tn_id;		/* (c) */
177 
178 	/*
179 	 * The node's type.  Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
180 	 * 'VLNK', 'VREG' and 'VSOCK' is allowed.  The usage of vnode
181 	 * types instead of a custom enumeration is to make things simpler
182 	 * and faster, as we do not need to convert between two types.
183 	 */
184 	__enum_uint8(vtype)	tn_type;	/* (c) */
185 
186 	/*
187 	 * See the top comment. Reordered here to fill LP64 hole.
188 	 */
189 	bool			tn_attached;	/* (m) */
190 
191 	/*
192 	 * Node's internal status.  This is used by several file system
193 	 * operations to do modifications to the node in a delayed
194 	 * fashion.
195 	 *
196 	 * tn_accessed has a dedicated byte to allow update by store without
197 	 * using atomics.  This provides a micro-optimization to e.g.
198 	 * tmpfs_read_pgcache().
199 	 */
200 	uint8_t			tn_status;	/* (vi) */
201 	uint8_t			tn_accessed;	/* unlocked */
202 
203 	/*
204 	 * The node size.  It does not necessarily match the real amount
205 	 * of memory consumed by it.
206 	 */
207 	off_t			tn_size;	/* (v) */
208 
209 	/* Generic node attributes. */
210 	uid_t			tn_uid;		/* (v) */
211 	gid_t			tn_gid;		/* (v) */
212 	mode_t			tn_mode;	/* (v) */
213 	int			tn_links;	/* (v) */
214 	u_long			tn_flags;	/* (v) */
215 	struct timespec		tn_atime;	/* (vi) */
216 	struct timespec		tn_mtime;	/* (vi) */
217 	struct timespec		tn_ctime;	/* (vi) */
218 	struct timespec		tn_birthtime;	/* (v) */
219 	unsigned long		tn_gen;		/* (c) */
220 
221 	/*
222 	 * As there is a single vnode for each active file within the
223 	 * system, care has to be taken to avoid allocating more than one
224 	 * vnode per file.  In order to do this, a bidirectional association
225 	 * is kept between vnodes and nodes.
226 	 *
227 	 * Whenever a vnode is allocated, its v_data field is updated to
228 	 * point to the node it references.  At the same time, the node's
229 	 * tn_vnode field is modified to point to the new vnode representing
230 	 * it.  Further attempts to allocate a vnode for this same node will
231 	 * result in returning a new reference to the value stored in
232 	 * tn_vnode.
233 	 *
234 	 * May be NULL when the node is unused (that is, no vnode has been
235 	 * allocated for it or it has been reclaimed).
236 	 */
237 	struct vnode *		tn_vnode;	/* (i) */
238 
239 	/*
240 	 * Interlock to protect tn_vpstate, and tn_status under shared
241 	 * vnode lock.
242 	 */
243 	struct mtx	tn_interlock;
244 
245 	/*
246 	 * Identify if current node has vnode assiocate with
247 	 * or allocating vnode.
248 	 */
249 	int		tn_vpstate;		/* (i) */
250 
251 	/* Transient refcounter on this node. */
252 	u_int		tn_refcount;		/* 0<->1 (m) + (i) */
253 
254 	/* Extended attributes of this node. */
255 	struct tmpfs_extattr_list	tn_extattrs;	/* (v) */
256 
257 	/* misc data field for different tn_type node */
258 	union {
259 		/* Valid when tn_type == VBLK || tn_type == VCHR. */
260 		dev_t			tn_rdev;	/* (c) */
261 
262 		/* Valid when tn_type == VDIR. */
263 		struct tn_dir {
264 			/*
265 			 * Pointer to the parent directory.  The root
266 			 * directory has a pointer to itself in this field;
267 			 * this property identifies the root node.
268 			 */
269 			struct tmpfs_node *	tn_parent;
270 
271 			/*
272 			 * Head of a tree that links the contents of
273 			 * the directory together.
274 			 */
275 			struct tmpfs_dir	tn_dirhead;
276 
277 			/*
278 			 * Head of a list the contains fake directory entries
279 			 * heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD
280 			 * flag.
281 			 */
282 			struct tmpfs_dir_duphead tn_dupindex;
283 
284 			/*
285 			 * Number and pointer of the first directory entry
286 			 * returned by the readdir operation if it were
287 			 * called again to continue reading data from the
288 			 * same directory as before.  This is used to speed
289 			 * up reads of long directories, assuming that no
290 			 * more than one read is in progress at a given time.
291 			 * Otherwise, these values are discarded.
292 			 */
293 			off_t			tn_readdir_lastn;
294 			struct tmpfs_dirent *	tn_readdir_lastp;
295 		} tn_dir;
296 
297 		/* Valid when tn_type == VLNK. */
298 		/* The link's target, allocated from a string pool. */
299 		struct tn_link {
300 			char *			tn_link_target;	/* (c) */
301 			char 			tn_link_smr;	/* (c) */
302 		} tn_link;
303 
304 		/* Valid when tn_type == VREG. */
305 		struct tn_reg {
306 			/*
307 			 * The contents of regular files stored in a
308 			 * tmpfs file system are represented by a
309 			 * single anonymous memory object (aobj, for
310 			 * short).  The aobj provides direct access to
311 			 * any position within the file.  It is a task
312 			 * of the memory management subsystem to issue
313 			 * the required page ins or page outs whenever
314 			 * a position within the file is accessed.
315 			 */
316 			vm_object_t		tn_aobj;	/* (c) */
317 			struct tmpfs_mount	*tn_tmp;	/* (c) */
318 			vm_pindex_t		tn_pages;	/* (v) */
319 		} tn_reg;
320 	} tn_spec;	/* (v) */
321 };
322 LIST_HEAD(tmpfs_node_list, tmpfs_node);
323 
324 #define tn_rdev tn_spec.tn_rdev
325 #define tn_dir tn_spec.tn_dir
326 #define tn_link_target tn_spec.tn_link.tn_link_target
327 #define tn_link_smr tn_spec.tn_link.tn_link_smr
328 #define tn_reg tn_spec.tn_reg
329 #define tn_fifo tn_spec.tn_fifo
330 
331 #define	TMPFS_LINK_MAX INT_MAX
332 
333 #define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock)
334 #define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock)
335 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
336 #define	TMPFS_NODE_ASSERT_LOCKED(node) mtx_assert(TMPFS_NODE_MTX(node), \
337     MA_OWNED)
338 
339 #ifdef INVARIANTS
340 #define TMPFS_ASSERT_LOCKED(node) do {					\
341 		MPASS((node) != NULL);					\
342 		MPASS((node)->tn_vnode != NULL);			\
343 		ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs assert");	\
344 	} while (0)
345 #else
346 #define TMPFS_ASSERT_LOCKED(node) (void)0
347 #endif
348 
349 /* tn_vpstate */
350 #define TMPFS_VNODE_ALLOCATING	1
351 #define TMPFS_VNODE_WANT	2
352 #define TMPFS_VNODE_DOOMED	4
353 #define	TMPFS_VNODE_WRECLAIM	8
354 
355 /* tn_status */
356 #define	TMPFS_NODE_MODIFIED	0x01
357 #define	TMPFS_NODE_CHANGED	0x02
358 
359 /*
360  * Internal representation of a tmpfs mount point.
361  */
362 struct tmpfs_mount {
363 	/*
364 	 * Original value of the "size" parameter, for reference purposes,
365 	 * mostly.
366 	 */
367 	off_t			tm_size_max;
368 	/*
369 	 * Maximum number of memory pages available for use by the file
370 	 * system, set during mount time.  This variable must never be
371 	 * used directly as it may be bigger than the current amount of
372 	 * free memory; in the extreme case, it will hold the ULONG_MAX
373 	 * value.
374 	 */
375 	u_long			tm_pages_max;
376 
377 	/* Number of pages in use by the file system. */
378 	u_long			tm_pages_used;
379 
380 	/*
381 	 * Pointer to the node representing the root directory of this
382 	 * file system.
383 	 */
384 	struct tmpfs_node *	tm_root;
385 
386 	/*
387 	 * Maximum number of possible nodes for this file system; set
388 	 * during mount time.  We need a hard limit on the maximum number
389 	 * of nodes to avoid allocating too much of them; their objects
390 	 * cannot be released until the file system is unmounted.
391 	 * Otherwise, we could easily run out of memory by creating lots
392 	 * of empty files and then simply removing them.
393 	 */
394 	ino_t			tm_nodes_max;
395 
396 	/* unrhdr used to allocate inode numbers */
397 	struct unrhdr64		tm_ino_unr;
398 
399 	/* Number of nodes currently that are in use. */
400 	ino_t			tm_nodes_inuse;
401 
402 	/* Memory used by extended attributes */
403 	uint64_t		tm_ea_memory_inuse;
404 
405 	/* Maximum memory available for extended attributes */
406 	uint64_t		tm_ea_memory_max;
407 
408 	/* Refcounter on this struct tmpfs_mount. */
409 	uint64_t		tm_refcount;
410 
411 	/* maximum representable file size */
412 	u_int64_t		tm_maxfilesize;
413 
414 	/*
415 	 * The used list contains all nodes that are currently used by
416 	 * the file system; i.e., they refer to existing files.
417 	 */
418 	struct tmpfs_node_list	tm_nodes_used;
419 
420 	/* All node lock to protect the node list and tmp_pages_used. */
421 	struct mtx		tm_allnode_lock;
422 
423 	/* Read-only status. */
424 	bool			tm_ronly;
425 	/* Do not use namecache. */
426 	bool			tm_nonc;
427 	/* Do not update mtime on writes through mmaped areas. */
428 	bool			tm_nomtime;
429 
430 	/* Read from page cache directly. */
431 	bool			tm_pgread;
432 };
433 #define	TMPFS_LOCK(tm) mtx_lock(&(tm)->tm_allnode_lock)
434 #define	TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->tm_allnode_lock)
435 #define	TMPFS_MP_ASSERT_LOCKED(tm) mtx_assert(&(tm)->tm_allnode_lock, MA_OWNED)
436 
437 /*
438  * This structure maps a file identifier to a tmpfs node.  Used by the
439  * NFS code.
440  */
441 struct tmpfs_fid_data {
442 	ino_t			tfd_id;
443 	unsigned long		tfd_gen;
444 };
445 _Static_assert(sizeof(struct tmpfs_fid_data) <= MAXFIDSZ,
446     "(struct tmpfs_fid_data) is larger than (struct fid).fid_data");
447 
448 struct tmpfs_dir_cursor {
449 	struct tmpfs_dirent	*tdc_current;
450 	struct tmpfs_dirent	*tdc_tree;
451 };
452 
453 #ifdef _KERNEL
454 /*
455  * Prototypes for tmpfs_subr.c.
456  */
457 
458 void	tmpfs_ref_node(struct tmpfs_node *node);
459 int	tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *, __enum_uint8(vtype),
460 	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
461 	    const char *, dev_t, struct tmpfs_node **);
462 int	tmpfs_fo_close(struct file *fp, struct thread *td);
463 void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
464 bool	tmpfs_free_node_locked(struct tmpfs_mount *, struct tmpfs_node *, bool);
465 void	tmpfs_free_tmp(struct tmpfs_mount *);
466 int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
467 	    const char *, u_int, struct tmpfs_dirent **);
468 void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
469 void	tmpfs_dirent_init(struct tmpfs_dirent *, const char *, u_int);
470 void	tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj);
471 int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
472 	    struct vnode **);
473 void	tmpfs_free_vp(struct vnode *);
474 int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
475 	    struct componentname *, const char *);
476 void	tmpfs_check_mtime(struct vnode *);
477 void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
478 void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
479 void	tmpfs_dir_destroy(struct tmpfs_mount *, struct tmpfs_node *);
480 struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
481 			    struct tmpfs_node *f,
482 			    struct componentname *cnp);
483 int	tmpfs_dir_getdents(struct tmpfs_mount *, struct tmpfs_node *,
484 	    struct uio *, int, uint64_t *, int *);
485 int	tmpfs_dir_whiteout_add(struct vnode *, struct componentname *);
486 void	tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *);
487 int	tmpfs_reg_resize(struct vnode *, off_t, boolean_t);
488 int	tmpfs_reg_punch_hole(struct vnode *vp, off_t *, off_t *);
489 int	tmpfs_chflags(struct vnode *, u_long, struct ucred *, struct thread *);
490 int	tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *);
491 int	tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
492 	    struct thread *);
493 int	tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *);
494 int	tmpfs_chtimes(struct vnode *, struct vattr *, struct ucred *cred,
495 	    struct thread *);
496 void	tmpfs_itimes(struct vnode *, const struct timespec *,
497 	    const struct timespec *);
498 
499 void	tmpfs_set_accessed(struct tmpfs_mount *tm, struct tmpfs_node *node);
500 void	tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node,
501 	    int status);
502 int	tmpfs_truncate(struct vnode *, off_t);
503 struct tmpfs_dirent *tmpfs_dir_first(struct tmpfs_node *dnode,
504 	    struct tmpfs_dir_cursor *dc);
505 struct tmpfs_dirent *tmpfs_dir_next(struct tmpfs_node *dnode,
506 	    struct tmpfs_dir_cursor *dc);
507 bool	tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages);
508 void	tmpfs_extattr_free(struct tmpfs_extattr* ea);
509 static __inline void
tmpfs_update(struct vnode * vp)510 tmpfs_update(struct vnode *vp)
511 {
512 
513 	tmpfs_itimes(vp, NULL, NULL);
514 }
515 
516 /*
517  * Convenience macros to simplify some logical expressions.
518  */
519 #define IMPLIES(a, b) (!(a) || (b))
520 
521 /*
522  * Checks that the directory entry pointed by 'de' matches the name 'name'
523  * with a length of 'len'.
524  */
525 #define TMPFS_DIRENT_MATCHES(de, name, len) \
526     (de->td_namelen == len && \
527     bcmp((de)->ud.td_name, (name), (de)->td_namelen) == 0)
528 
529 /*
530  * Ensures that the node pointed by 'node' is a directory and that its
531  * contents are consistent with respect to directories.
532  */
533 #define TMPFS_VALIDATE_DIR(node) do { \
534 	MPASS((node)->tn_type == VDIR); \
535 	MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
536 } while (0)
537 
538 /*
539  * Amount of memory pages to reserve for the system (e.g., to not use by
540  * tmpfs).
541  */
542 #if !defined(TMPFS_PAGES_MINRESERVED)
543 #define TMPFS_PAGES_MINRESERVED		(4 * 1024 * 1024 / PAGE_SIZE)
544 #endif
545 
546 /*
547  * Percent of available memory + swap available to use by tmpfs file systems
548  * without a size limit.
549  */
550 #if !defined(TMPFS_MEM_PERCENT)
551 #define TMPFS_MEM_PERCENT		100
552 #endif
553 
554 /*
555  * Amount of memory to reserve for extended attributes.
556  */
557 #if !defined(TMPFS_EA_MEMORY_RESERVED)
558 #define TMPFS_EA_MEMORY_RESERVED	(16 * 1024 * 1024)
559 #endif
560 
561 size_t tmpfs_mem_avail(void);
562 size_t tmpfs_pages_used(struct tmpfs_mount *tmp);
563 int tmpfs_subr_init(void);
564 void tmpfs_subr_uninit(void);
565 
566 extern int tmpfs_pager_type;
567 
568 /*
569  * Macros/functions to convert from generic data structures to tmpfs
570  * specific ones.
571  */
572 
573 static inline struct vnode *
VM_TO_TMPFS_VP(vm_object_t obj)574 VM_TO_TMPFS_VP(vm_object_t obj)
575 {
576 	struct tmpfs_node *node;
577 
578 	if ((obj->flags & OBJ_TMPFS) == 0)
579 		return (NULL);
580 
581 	/*
582 	 * swp_priv is the back-pointer to the tmpfs node, if any,
583 	 * which uses the vm object as backing store.  The object
584 	 * handle is not used to avoid locking sw_alloc_sx on tmpfs
585 	 * node instantiation/destroy.
586 	 */
587 	node = obj->un_pager.swp.swp_priv;
588 	return (node->tn_vnode);
589 }
590 
591 static inline struct tmpfs_mount *
VM_TO_TMPFS_MP(vm_object_t obj)592 VM_TO_TMPFS_MP(vm_object_t obj)
593 {
594 	struct tmpfs_node *node;
595 
596 	if ((obj->flags & OBJ_TMPFS) == 0)
597 		return (NULL);
598 
599 	node = obj->un_pager.swp.swp_priv;
600 	MPASS(node->tn_type == VREG);
601 	return (node->tn_reg.tn_tmp);
602 }
603 
604 static inline struct tmpfs_mount *
VFS_TO_TMPFS(struct mount * mp)605 VFS_TO_TMPFS(struct mount *mp)
606 {
607 	struct tmpfs_mount *tmp;
608 
609 	MPASS(mp != NULL && mp->mnt_data != NULL);
610 	tmp = (struct tmpfs_mount *)mp->mnt_data;
611 	return (tmp);
612 }
613 
614 static inline struct tmpfs_node *
VP_TO_TMPFS_NODE(struct vnode * vp)615 VP_TO_TMPFS_NODE(struct vnode *vp)
616 {
617 	struct tmpfs_node *node;
618 
619 	MPASS(vp != NULL && vp->v_data != NULL);
620 	node = (struct tmpfs_node *)vp->v_data;
621 	return (node);
622 }
623 
624 #define	VP_TO_TMPFS_NODE_SMR(vp)	\
625 	((struct tmpfs_node *)vn_load_v_data_smr(vp))
626 
627 static inline struct tmpfs_node *
VP_TO_TMPFS_DIR(struct vnode * vp)628 VP_TO_TMPFS_DIR(struct vnode *vp)
629 {
630 	struct tmpfs_node *node;
631 
632 	node = VP_TO_TMPFS_NODE(vp);
633 	TMPFS_VALIDATE_DIR(node);
634 	return (node);
635 }
636 
637 static inline bool
tmpfs_use_nc(struct vnode * vp)638 tmpfs_use_nc(struct vnode *vp)
639 {
640 
641 	return (!(VFS_TO_TMPFS(vp->v_mount)->tm_nonc));
642 }
643 
644 static inline void
tmpfs_update_getattr(struct vnode * vp)645 tmpfs_update_getattr(struct vnode *vp)
646 {
647 	struct tmpfs_node *node;
648 
649 	node = VP_TO_TMPFS_NODE(vp);
650 	if (__predict_false((node->tn_status & (TMPFS_NODE_MODIFIED |
651 	    TMPFS_NODE_CHANGED)) != 0 || node->tn_accessed))
652 		tmpfs_update(vp);
653 }
654 
655 extern struct fileops tmpfs_fnops;
656 
657 #endif /* _KERNEL */
658 
659 #endif /* _FS_TMPFS_TMPFS_H_ */
660