#ifndef _FS_TMPFS_TMPFS_H_
#define _FS_TMPFS_TMPFS_H_
#if !defined(_KERNEL) && !defined(_KMEMUSER)
#error "not supposed to be exposed to userland"
#endif
#include <sys/dirent.h>
#include <sys/mount.h>
#include <sys/pool.h>
#include <sys/queue.h>
#include <sys/vnode.h>
typedef struct tmpfs_dirent {
TAILQ_ENTRY(tmpfs_dirent) td_entries;
struct tmpfs_node * td_node;
uint32_t td_seq;
char * td_name;
uint16_t td_namelen;
} tmpfs_dirent_t;
TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
typedef struct tmpfs_node {
LIST_ENTRY(tmpfs_node) tn_entries;
vnode_t * tn_vnode;
uint32_t tn_holdcount;
tmpfs_dirent_t * tn_dirent_hint;
enum vtype tn_type;
ino_t tn_id;
uint32_t tn_gen;
off_t tn_size;
uid_t tn_uid;
gid_t tn_gid;
mode_t tn_mode;
int tn_flags;
nlink_t tn_links;
unsigned tn_tflags;
struct timespec tn_atime;
struct timespec tn_mtime;
struct timespec tn_ctime;
struct timespec tn_birthtime;
kmutex_t tn_timelock;
struct lockf * tn_lockf;
union {
struct {
dev_t tn_rdev;
} tn_dev;
struct {
struct tmpfs_node * tn_parent;
struct tmpfs_dir tn_dir;
uint32_t tn_next_seq;
void * tn_seq_arena;
struct tmpfs_dirent * tn_readdir_lastp;
} tn_dir;
struct tn_lnk {
char * tn_link;
} tn_lnk;
struct tn_reg {
struct uvm_object * tn_aobj;
size_t tn_aobj_pages;
} tn_reg;
} tn_spec;
} tmpfs_node_t;
#if defined(_KERNEL)
VFS_PROTOS(tmpfs);
LIST_HEAD(tmpfs_node_list, tmpfs_node);
#define TMPFS_MAXNAMLEN 255
CTASSERT(TMPFS_MAXNAMLEN < UINT16_MAX);
#define TMPFS_DIRSEQ_DOT 0
#define TMPFS_DIRSEQ_DOTDOT 1
#define TMPFS_DIRSEQ_EOF 2
#define TMPFS_DIRSEQ_START 3
#define TMPFS_DIRSEQ_END (1U << 30)
#define TMPFS_DIRSEQ_NONE (1U << 31)
#define TMPFS_UPDATE_ATIME 0x01
#define TMPFS_UPDATE_MTIME 0x02
#define TMPFS_UPDATE_CTIME 0x04
#define TMPFS_WHITEOUT_BIT (1U << 31)
#define TMPFS_NODE_GEN_MASK (TMPFS_WHITEOUT_BIT - 1)
#define TMPFS_NODE_GEN(node) \
((node)->tn_gen & TMPFS_NODE_GEN_MASK)
#define TMPFS_NODE_WHITEOUT ((tmpfs_node_t *)-1)
#define TMPFS_NODE_RECLAIMED (1U << 30)
typedef struct tmpfs_mount {
uint64_t tm_mem_limit;
uint64_t tm_bytes_used;
kmutex_t tm_acc_lock;
tmpfs_node_t * tm_root;
unsigned int tm_nodes_max;
unsigned int tm_nodes_cnt;
kmutex_t tm_lock;
struct tmpfs_node_list tm_nodes;
} tmpfs_mount_t;
typedef struct tmpfs_fid {
uint16_t tf_len;
uint16_t tf_pad;
uint32_t tf_gen;
ino_t tf_id;
} tmpfs_fid_t;
void tmpfs_free_node(tmpfs_mount_t *, tmpfs_node_t *);
int tmpfs_construct_node(vnode_t *, vnode_t **, struct vattr *,
struct componentname *, char *);
int tmpfs_alloc_dirent(tmpfs_mount_t *, const char *, uint16_t,
tmpfs_dirent_t **);
void tmpfs_free_dirent(tmpfs_mount_t *, tmpfs_dirent_t *);
void tmpfs_dir_attach(tmpfs_node_t *, tmpfs_dirent_t *, tmpfs_node_t *);
void tmpfs_dir_detach(tmpfs_node_t *, tmpfs_dirent_t *);
tmpfs_dirent_t *tmpfs_dir_lookup(tmpfs_node_t *, struct componentname *);
tmpfs_dirent_t *tmpfs_dir_cached(tmpfs_node_t *);
uint32_t tmpfs_dir_getseq(tmpfs_node_t *, tmpfs_dirent_t *);
tmpfs_dirent_t *tmpfs_dir_lookupbyseq(tmpfs_node_t *, off_t);
int tmpfs_dir_getdents(tmpfs_node_t *, struct uio *, off_t *);
int tmpfs_reg_resize(vnode_t *, off_t);
int tmpfs_chflags(vnode_t *, int, kauth_cred_t, lwp_t *);
int tmpfs_chmod(vnode_t *, mode_t, kauth_cred_t, lwp_t *);
int tmpfs_chown(vnode_t *, uid_t, gid_t, kauth_cred_t, lwp_t *);
int tmpfs_chsize(vnode_t *, u_quad_t, kauth_cred_t, lwp_t *);
int tmpfs_chtimes(vnode_t *, const struct timespec *,
const struct timespec *, const struct timespec *, int,
kauth_cred_t, lwp_t *);
void tmpfs_update(vnode_t *, unsigned);
void tmpfs_update_locked(vnode_t *, unsigned);
void tmpfs_update_lazily(vnode_t *, unsigned);
void tmpfs_mntmem_init(tmpfs_mount_t *, uint64_t);
void tmpfs_mntmem_destroy(tmpfs_mount_t *);
int tmpfs_mntmem_set(tmpfs_mount_t *, uint64_t);
size_t tmpfs_mem_info(bool);
uint64_t tmpfs_bytes_max(tmpfs_mount_t *);
size_t tmpfs_pages_avail(tmpfs_mount_t *);
bool tmpfs_mem_incr(tmpfs_mount_t *, size_t);
void tmpfs_mem_decr(tmpfs_mount_t *, size_t);
tmpfs_dirent_t *tmpfs_dirent_get(tmpfs_mount_t *);
void tmpfs_dirent_put(tmpfs_mount_t *, tmpfs_dirent_t *);
tmpfs_node_t * tmpfs_node_get(tmpfs_mount_t *);
void tmpfs_node_put(tmpfs_mount_t *, tmpfs_node_t *);
char * tmpfs_strname_alloc(tmpfs_mount_t *, size_t);
void tmpfs_strname_free(tmpfs_mount_t *, char *, size_t);
bool tmpfs_strname_neqlen(struct componentname *, struct componentname *);
#define TMPFS_VALIDATE_DIR(node) \
KASSERT((node)->tn_vnode == NULL || VOP_ISLOCKED((node)->tn_vnode)); \
KASSERT((node)->tn_type == VDIR); \
KASSERT((node)->tn_size % sizeof(tmpfs_dirent_t) == 0);
static __inline tmpfs_mount_t *
VFS_TO_TMPFS(struct mount *mp)
{
tmpfs_mount_t *tmp = mp->mnt_data;
KASSERT(tmp != NULL);
return tmp;
}
static __inline tmpfs_node_t *
VP_TO_TMPFS_DIR(vnode_t *vp)
{
tmpfs_node_t *node = vp->v_data;
KASSERT(node != NULL);
TMPFS_VALIDATE_DIR(node);
return node;
}
#endif
static __inline tmpfs_node_t *
VP_TO_TMPFS_NODE(vnode_t *vp)
{
tmpfs_node_t *node = vp->v_data;
#ifdef KASSERT
KASSERT(node != NULL);
#endif
return node;
}
#endif