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