1*99ef5f49Svisa /* $OpenBSD: tmpfs.h,v 1.10 2020/10/12 13:08:03 visa Exp $ */
27013b092Sespie /* $NetBSD: tmpfs.h,v 1.45 2011/09/27 01:10:43 christos Exp $ */
37013b092Sespie
47013b092Sespie /*
57013b092Sespie * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
67013b092Sespie * All rights reserved.
77013b092Sespie *
87013b092Sespie * This code is derived from software contributed to The NetBSD Foundation
97013b092Sespie * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
107013b092Sespie * 2005 program.
117013b092Sespie *
127013b092Sespie * Redistribution and use in source and binary forms, with or without
137013b092Sespie * modification, are permitted provided that the following conditions
147013b092Sespie * are met:
157013b092Sespie * 1. Redistributions of source code must retain the above copyright
167013b092Sespie * notice, this list of conditions and the following disclaimer.
177013b092Sespie * 2. Redistributions in binary form must reproduce the above copyright
187013b092Sespie * notice, this list of conditions and the following disclaimer in the
197013b092Sespie * documentation and/or other materials provided with the distribution.
207013b092Sespie *
217013b092Sespie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
227013b092Sespie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
237013b092Sespie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
247013b092Sespie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
257013b092Sespie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
267013b092Sespie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
277013b092Sespie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
287013b092Sespie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
297013b092Sespie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
307013b092Sespie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
317013b092Sespie * POSSIBILITY OF SUCH DAMAGE.
327013b092Sespie */
337013b092Sespie
349d2ad5b0Sespie #ifndef _TMPFS_TMPFS_H_
359d2ad5b0Sespie #define _TMPFS_TMPFS_H_
367013b092Sespie
377013b092Sespie #if !defined(_KERNEL) && !defined(_KMEMUSER)
387013b092Sespie #error "not supposed to be exposed to userland"
397013b092Sespie #endif
407013b092Sespie
417013b092Sespie #include <sys/dirent.h>
427013b092Sespie #include <sys/mount.h>
437013b092Sespie #include <sys/pool.h>
447013b092Sespie #include <sys/queue.h>
457013b092Sespie #include <sys/stdint.h>
467013b092Sespie #include <sys/rwlock.h>
4703d1830dStedu #include <sys/lock.h>
487013b092Sespie
49fde894e5Stedu #include <uvm/uvm_extern.h>
50fde894e5Stedu
517013b092Sespie /*
527013b092Sespie * Internal representation of a tmpfs directory entry.
537013b092Sespie *
547013b092Sespie * All fields are protected by vnode lock.
557013b092Sespie */
567013b092Sespie typedef struct tmpfs_dirent {
577013b092Sespie TAILQ_ENTRY(tmpfs_dirent) td_entries;
587013b092Sespie
597013b092Sespie /* Pointer to the inode this entry refers to. */
607013b092Sespie struct tmpfs_node * td_node;
617013b092Sespie
6257331246Sespie /* Sequence number, see tmpfs_dir_getseq(). */
6357331246Sespie uint64_t td_seq;
6457331246Sespie
657013b092Sespie /* Name and its length. */
667013b092Sespie char * td_name;
677013b092Sespie uint16_t td_namelen;
687013b092Sespie } tmpfs_dirent_t;
697013b092Sespie
707013b092Sespie TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
717013b092Sespie
727013b092Sespie /*
737013b092Sespie * Internal representation of a tmpfs file system node -- inode.
747013b092Sespie *
7557331246Sespie * This structure is split in two parts: one holds attributes common
767013b092Sespie * to all file types and the other holds data that is only applicable to
777013b092Sespie * a particular type.
787013b092Sespie *
797013b092Sespie * All fields are protected by vnode lock. The vnode association itself
807013b092Sespie * is protected by tmpfs_node_t::tn_nlock.
817013b092Sespie */
827013b092Sespie typedef struct tmpfs_node {
837013b092Sespie LIST_ENTRY(tmpfs_node) tn_entries;
847013b092Sespie
857013b092Sespie /*
867013b092Sespie * Each inode has a corresponding vnode. It is a bi-directional
877013b092Sespie * association. Whenever vnode is allocated, its v_data field is
887013b092Sespie * set to the inode it reference, and tmpfs_node_t::tn_vnode is
897013b092Sespie * set to point to the said vnode.
907013b092Sespie *
917013b092Sespie * Further attempts to allocate a vnode for this same node will
927013b092Sespie * result in returning a new reference to the value stored in
937013b092Sespie * tn_vnode. It may be NULL when the node is unused (that is,
947013b092Sespie * no vnode has been allocated or it has been reclaimed).
957013b092Sespie */
967013b092Sespie struct rwlock tn_nlock; /* node lock */
9726b8ec94Snatano struct rrwlock tn_vlock; /* vnode lock */
987013b092Sespie struct vnode * tn_vnode;
997013b092Sespie
1007013b092Sespie /* Directory entry. Only a hint, since hard link can have multiple. */
1017013b092Sespie tmpfs_dirent_t * tn_dirent_hint;
1027013b092Sespie
1037013b092Sespie /* The inode type: VBLK, VCHR, VDIR, VFIFO, VLNK, VREG or VSOCK. */
1047013b092Sespie enum vtype tn_type;
1057013b092Sespie
1067013b092Sespie /* Inode identifier and generation number. */
1077013b092Sespie ino_t tn_id;
1087013b092Sespie unsigned long tn_gen;
1097013b092Sespie
1107013b092Sespie /* The inode size. */
1117013b092Sespie off_t tn_size;
1127013b092Sespie
1137013b092Sespie /* Generic node attributes. */
1147013b092Sespie uid_t tn_uid;
1157013b092Sespie gid_t tn_gid;
1167013b092Sespie mode_t tn_mode;
1177013b092Sespie int tn_flags;
1187013b092Sespie nlink_t tn_links;
1197013b092Sespie struct timespec tn_atime;
1207013b092Sespie struct timespec tn_mtime;
1217013b092Sespie struct timespec tn_ctime;
1227013b092Sespie struct timespec tn_birthtime;
1237013b092Sespie
1247013b092Sespie /* Head of byte-level lock list (used by tmpfs_advlock). */
1256bd4f7caSanton struct lockf_state * tn_lockf;
1267013b092Sespie
1277013b092Sespie union {
1287013b092Sespie /* Type case: VBLK or VCHR. */
1297013b092Sespie struct {
1307013b092Sespie dev_t tn_rdev;
1317013b092Sespie } tn_dev;
1327013b092Sespie
1337013b092Sespie /* Type case: VDIR. */
1347013b092Sespie struct {
1357013b092Sespie /* Parent directory (root inode points to itself). */
1367013b092Sespie struct tmpfs_node * tn_parent;
1377013b092Sespie
1387013b092Sespie /* List of directory entries. */
1397013b092Sespie struct tmpfs_dir tn_dir;
1407013b092Sespie
14157331246Sespie /* Last given sequence number. */
14257331246Sespie uint64_t tn_next_seq;
14357331246Sespie
1447013b092Sespie /*
14557331246Sespie * Pointer of the last directory entry returned
14657331246Sespie * by the readdir(3) operation.
1477013b092Sespie */
1487013b092Sespie struct tmpfs_dirent * tn_readdir_lastp;
1497013b092Sespie } tn_dir;
1507013b092Sespie
1517013b092Sespie /* Type case: VLNK. */
1527013b092Sespie struct tn_lnk {
1537013b092Sespie /* The link's target. */
1547013b092Sespie char * tn_link;
1557013b092Sespie } tn_lnk;
1567013b092Sespie
1577013b092Sespie /* Type case: VREG. */
1587013b092Sespie struct tn_reg {
1597013b092Sespie /* Underlying UVM object to store contents. */
1607013b092Sespie struct uvm_object * tn_aobj;
1617013b092Sespie size_t tn_aobj_pages;
1627013b092Sespie vaddr_t tn_aobj_pgptr;
1637013b092Sespie voff_t tn_aobj_pgnum;
1647013b092Sespie } tn_reg;
1657013b092Sespie } tn_spec;
1667013b092Sespie
1677013b092Sespie #define tn_uobj tn_spec.tn_reg.tn_aobj
1687013b092Sespie #define tn_pgptr tn_spec.tn_reg.tn_aobj_pgptr
1697013b092Sespie #define tn_pgnum tn_spec.tn_reg.tn_aobj_pgnum
1707013b092Sespie
1717013b092Sespie } tmpfs_node_t;
1727013b092Sespie
1737013b092Sespie #if defined(_KERNEL)
1747013b092Sespie
175*99ef5f49Svisa #include <lib/libkern/libkern.h> /* for KASSERT() */
176*99ef5f49Svisa
1777013b092Sespie LIST_HEAD(tmpfs_node_list, tmpfs_node);
1787013b092Sespie
17957331246Sespie
18057331246Sespie #define TMPFS_MAXNAMLEN 255
18157331246Sespie /* Validate maximum td_namelen length. */
18257331246Sespie /* CTASSERT(TMPFS_MAXNAMLEN < UINT16_MAX); */
18357331246Sespie
18457331246Sespie /*
18557331246Sespie * Reserved values for the virtual entries (the first must be 0) and EOF.
18657331246Sespie * The start/end of the incremental range, see tmpfs_dir_getseq().
18757331246Sespie */
18857331246Sespie #define TMPFS_DIRSEQ_DOT 0
18957331246Sespie #define TMPFS_DIRSEQ_DOTDOT 1
19057331246Sespie #define TMPFS_DIRSEQ_EOF 2
19157331246Sespie
19257331246Sespie #define TMPFS_DIRSEQ_START 3 /* inclusive */
19357331246Sespie #define TMPFS_DIRSEQ_END UINT64_MAX /* exclusive */
19457331246Sespie
19557331246Sespie /* Mark to indicate that the number is not set. */
19657331246Sespie #define TMPFS_DIRSEQ_NONE UINT64_MAX
19757331246Sespie
19857331246Sespie /* Can we still append entries to a directory? */
19957331246Sespie #define TMPFS_DIRSEQ_FULL(dnode) \
20057331246Sespie ((dnode)->tn_spec.tn_dir.tn_next_seq == TMPFS_DIRSEQ_END)
20157331246Sespie
2027013b092Sespie /* Status flags. */
2037013b092Sespie #define TMPFS_NODE_ACCESSED 0x01
2047013b092Sespie #define TMPFS_NODE_MODIFIED 0x02
2057013b092Sespie #define TMPFS_NODE_CHANGED 0x04
2067013b092Sespie
2077013b092Sespie #define TMPFS_NODE_STATUSALL \
2087013b092Sespie (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED)
2097013b092Sespie
2107013b092Sespie /*
2117013b092Sespie * Bit indicating vnode reclamation.
2127013b092Sespie * We abuse tmpfs_node_t::tn_gen for that.
2137013b092Sespie */
2147013b092Sespie #define TMPFS_NODE_GEN_MASK (~0UL >> 1)
2157013b092Sespie #define TMPFS_RECLAIMING_BIT (~TMPFS_NODE_GEN_MASK)
2167013b092Sespie
2177013b092Sespie #define TMPFS_NODE_RECLAIMING(node) \
2187013b092Sespie (((node)->tn_gen & TMPFS_RECLAIMING_BIT) != 0)
2197013b092Sespie
2207013b092Sespie #define TMPFS_NODE_GEN(node) \
2217013b092Sespie ((node)->tn_gen & TMPFS_NODE_GEN_MASK)
2227013b092Sespie
2237013b092Sespie /*
2247013b092Sespie * Internal representation of a tmpfs mount point.
2257013b092Sespie */
2267013b092Sespie typedef struct tmpfs_mount {
2277013b092Sespie /* Limit and number of bytes in use by the file system. */
2287013b092Sespie uint64_t tm_mem_limit;
2297013b092Sespie uint64_t tm_bytes_used;
23057331246Sespie /* Highest allocated inode number. */
23157331246Sespie uint64_t tm_highest_inode;
2327013b092Sespie struct rwlock tm_acc_lock;
2337013b092Sespie
2347013b092Sespie /* Pointer to the root inode. */
2357013b092Sespie tmpfs_node_t * tm_root;
2367013b092Sespie
2377013b092Sespie /* Maximum number of possible nodes for this file system. */
2387013b092Sespie unsigned int tm_nodes_max;
2397013b092Sespie
2407013b092Sespie /* Number of nodes currently allocated. */
2417013b092Sespie unsigned int tm_nodes_cnt;
2427013b092Sespie
2437013b092Sespie /* List of inodes and the lock protecting it. */
2447013b092Sespie struct rwlock tm_lock;
2457013b092Sespie struct tmpfs_node_list tm_nodes;
2467013b092Sespie } tmpfs_mount_t;
2477013b092Sespie
2487013b092Sespie /*
2497013b092Sespie * This structure maps a file identifier to a tmpfs node. Used by the
2507013b092Sespie * NFS code.
2517013b092Sespie */
2527013b092Sespie typedef struct tmpfs_fid {
2537013b092Sespie uint16_t tf_len;
2547013b092Sespie uint16_t tf_pad;
2557013b092Sespie uint32_t tf_gen;
2567013b092Sespie ino_t tf_id;
2577013b092Sespie } tmpfs_fid_t;
2587013b092Sespie
2597013b092Sespie /*
2607013b092Sespie * Prototypes for tmpfs_subr.c.
2617013b092Sespie */
2627013b092Sespie
2637013b092Sespie int tmpfs_alloc_node(tmpfs_mount_t *, enum vtype, uid_t, gid_t,
2647013b092Sespie mode_t, char *, dev_t, tmpfs_node_t **);
2657013b092Sespie void tmpfs_free_node(tmpfs_mount_t *, tmpfs_node_t *);
2667013b092Sespie
2677013b092Sespie int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
2687013b092Sespie struct componentname *, char *);
2697013b092Sespie
2707013b092Sespie int tmpfs_vnode_get(struct mount *, tmpfs_node_t *, struct vnode **);
2717013b092Sespie
2727013b092Sespie int tmpfs_alloc_dirent(tmpfs_mount_t *, const char *, uint16_t,
2737013b092Sespie tmpfs_dirent_t **);
2747013b092Sespie void tmpfs_free_dirent(tmpfs_mount_t *, tmpfs_dirent_t *);
27557331246Sespie void tmpfs_dir_attach(tmpfs_node_t *, tmpfs_dirent_t *,
27657331246Sespie tmpfs_node_t *);
27757331246Sespie void tmpfs_dir_detach(tmpfs_node_t *, tmpfs_dirent_t *);
2787013b092Sespie
2797013b092Sespie tmpfs_dirent_t *tmpfs_dir_lookup(tmpfs_node_t *, struct componentname *);
2807013b092Sespie tmpfs_dirent_t *tmpfs_dir_cached(tmpfs_node_t *);
2817013b092Sespie
28257331246Sespie uint64_t tmpfs_dir_getseq(tmpfs_node_t *, tmpfs_dirent_t *);
28357331246Sespie tmpfs_dirent_t *tmpfs_dir_lookupbyseq(tmpfs_node_t *, off_t);
2843544b002Sguenther int tmpfs_dir_getdents(tmpfs_node_t *, struct uio *);
2857013b092Sespie
2867013b092Sespie int tmpfs_reg_resize(struct vnode *, off_t);
2877013b092Sespie int tmpfs_truncate(struct vnode *, off_t);
2887013b092Sespie
2897013b092Sespie int tmpfs_chflags(struct vnode *, int, struct ucred *, struct proc *);
2907013b092Sespie int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct proc *);
2917013b092Sespie int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct proc *);
2927013b092Sespie int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct proc *);
2937013b092Sespie int tmpfs_chtimes(struct vnode *, const struct timespec *,
2947013b092Sespie const struct timespec *, int, struct ucred *,
2957013b092Sespie struct proc *);
2967013b092Sespie void tmpfs_update(tmpfs_node_t *, int);
2977013b092Sespie int tmpfs_zeropg(tmpfs_node_t *, voff_t, vaddr_t);
2987013b092Sespie int tmpfs_uio_cached(tmpfs_node_t *);
2997013b092Sespie int tmpfs_uiomove(tmpfs_node_t *, struct uio *, vsize_t);
3007013b092Sespie void tmpfs_uio_uncache(tmpfs_node_t *);
3017013b092Sespie void tmpfs_uio_cache(tmpfs_node_t *, voff_t, vaddr_t);
3027013b092Sespie vaddr_t tmpfs_uio_lookup(tmpfs_node_t *, voff_t);
3037013b092Sespie
3047013b092Sespie /*
3057013b092Sespie * Prototypes for tmpfs_mem.c.
3067013b092Sespie */
3077013b092Sespie
3087013b092Sespie void tmpfs_mntmem_init(tmpfs_mount_t *, uint64_t);
3097013b092Sespie void tmpfs_mntmem_destroy(tmpfs_mount_t *);
3107013b092Sespie
3117013b092Sespie size_t tmpfs_mem_info(int);
3127013b092Sespie uint64_t tmpfs_bytes_max(tmpfs_mount_t *);
3137013b092Sespie uint64_t tmpfs_pages_avail(tmpfs_mount_t *);
3147013b092Sespie int tmpfs_mem_incr(tmpfs_mount_t *, size_t);
3157013b092Sespie void tmpfs_mem_decr(tmpfs_mount_t *, size_t);
3167013b092Sespie
3177013b092Sespie tmpfs_dirent_t *tmpfs_dirent_get(tmpfs_mount_t *);
3187013b092Sespie void tmpfs_dirent_put(tmpfs_mount_t *, tmpfs_dirent_t *);
3197013b092Sespie
3207013b092Sespie tmpfs_node_t * tmpfs_node_get(tmpfs_mount_t *);
3217013b092Sespie void tmpfs_node_put(tmpfs_mount_t *, tmpfs_node_t *);
3227013b092Sespie
3237013b092Sespie char * tmpfs_strname_alloc(tmpfs_mount_t *, size_t);
3247013b092Sespie void tmpfs_strname_free(tmpfs_mount_t *, char *, size_t);
3257013b092Sespie int tmpfs_strname_neqlen(struct componentname *, struct componentname *);
3267013b092Sespie
3277013b092Sespie /*
3287013b092Sespie * Ensures that the node pointed by 'node' is a directory and that its
3297013b092Sespie * contents are consistent with respect to directories.
3307013b092Sespie */
3317013b092Sespie #define TMPFS_VALIDATE_DIR(node) \
33257331246Sespie KASSERT((node)->tn_vnode == NULL || VOP_ISLOCKED((node)->tn_vnode)); \
3337013b092Sespie KASSERT((node)->tn_type == VDIR); \
33457331246Sespie KASSERT((node)->tn_size % sizeof(tmpfs_dirent_t) == 0);
3357013b092Sespie
3367013b092Sespie /*
3377013b092Sespie * Memory management stuff.
3387013b092Sespie */
3397013b092Sespie
3407013b092Sespie /* Amount of memory pages to reserve for the system. */
3417013b092Sespie #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
3427013b092Sespie
3437013b092Sespie /*
3447013b092Sespie * Routines to convert VFS structures to tmpfs internal ones.
3457013b092Sespie */
3467013b092Sespie
3477013b092Sespie static inline tmpfs_mount_t *
VFS_TO_TMPFS(struct mount * mp)3487013b092Sespie VFS_TO_TMPFS(struct mount *mp)
3497013b092Sespie {
3507013b092Sespie tmpfs_mount_t *tmp = mp->mnt_data;
3517013b092Sespie
3527013b092Sespie KASSERT(tmp != NULL);
3537013b092Sespie return tmp;
3547013b092Sespie }
3557013b092Sespie
3567013b092Sespie static inline tmpfs_node_t *
VP_TO_TMPFS_DIR(struct vnode * vp)3577013b092Sespie VP_TO_TMPFS_DIR(struct vnode *vp)
3587013b092Sespie {
3597013b092Sespie tmpfs_node_t *node = vp->v_data;
3607013b092Sespie
3617013b092Sespie KASSERT(node != NULL);
3627013b092Sespie TMPFS_VALIDATE_DIR(node);
3637013b092Sespie return node;
3647013b092Sespie }
3657013b092Sespie
3667013b092Sespie #endif /* defined(_KERNEL) */
3677013b092Sespie
3687013b092Sespie static __inline tmpfs_node_t *
VP_TO_TMPFS_NODE(struct vnode * vp)3697013b092Sespie VP_TO_TMPFS_NODE(struct vnode *vp)
3707013b092Sespie {
3717013b092Sespie tmpfs_node_t *node = vp->v_data;
3727013b092Sespie #ifdef KASSERT
3737013b092Sespie KASSERT(node != NULL);
3747013b092Sespie #endif
3757013b092Sespie return node;
3767013b092Sespie }
3777013b092Sespie
3789d2ad5b0Sespie #endif /* _TMPFS_TMPFS_H_ */
379