#ifndef _TMPFS_TMPFS_H_
#define _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/stdint.h>
#include <sys/rwlock.h>
#include <sys/lock.h>
#include <uvm/uvm_extern.h>
typedef struct tmpfs_dirent {
TAILQ_ENTRY(tmpfs_dirent) td_entries;
struct tmpfs_node * td_node;
uint64_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;
struct rwlock tn_nlock;
struct rrwlock tn_vlock;
struct vnode * tn_vnode;
tmpfs_dirent_t * tn_dirent_hint;
enum vtype tn_type;
ino_t tn_id;
unsigned long 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;
struct timespec tn_atime;
struct timespec tn_mtime;
struct timespec tn_ctime;
struct timespec tn_birthtime;
struct lockf_state * tn_lockf;
union {
struct {
dev_t tn_rdev;
} tn_dev;
struct {
struct tmpfs_node * tn_parent;
struct tmpfs_dir tn_dir;
uint64_t tn_next_seq;
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;
vaddr_t tn_aobj_pgptr;
voff_t tn_aobj_pgnum;
} tn_reg;
} tn_spec;
#define tn_uobj tn_spec.tn_reg.tn_aobj
#define tn_pgptr tn_spec.tn_reg.tn_aobj_pgptr
#define tn_pgnum tn_spec.tn_reg.tn_aobj_pgnum
} tmpfs_node_t;
#if defined(_KERNEL)
#include <lib/libkern/libkern.h>
LIST_HEAD(tmpfs_node_list, tmpfs_node);
#define TMPFS_MAXNAMLEN 255
#define TMPFS_DIRSEQ_DOT 0
#define TMPFS_DIRSEQ_DOTDOT 1
#define TMPFS_DIRSEQ_EOF 2
#define TMPFS_DIRSEQ_START 3
#define TMPFS_DIRSEQ_END UINT64_MAX
#define TMPFS_DIRSEQ_NONE UINT64_MAX
#define TMPFS_DIRSEQ_FULL(dnode) \
((dnode)->tn_spec.tn_dir.tn_next_seq == TMPFS_DIRSEQ_END)
#define TMPFS_NODE_ACCESSED 0x01
#define TMPFS_NODE_MODIFIED 0x02
#define TMPFS_NODE_CHANGED 0x04
#define TMPFS_NODE_STATUSALL \
(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED)
#define TMPFS_NODE_GEN_MASK (~0UL >> 1)
#define TMPFS_RECLAIMING_BIT (~TMPFS_NODE_GEN_MASK)
#define TMPFS_NODE_RECLAIMING(node) \
(((node)->tn_gen & TMPFS_RECLAIMING_BIT) != 0)
#define TMPFS_NODE_GEN(node) \
((node)->tn_gen & TMPFS_NODE_GEN_MASK)
typedef struct tmpfs_mount {
uint64_t tm_mem_limit;
uint64_t tm_bytes_used;
uint64_t tm_highest_inode;
struct rwlock tm_acc_lock;
tmpfs_node_t * tm_root;
unsigned int tm_nodes_max;
unsigned int tm_nodes_cnt;
struct rwlock 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;
int tmpfs_alloc_node(tmpfs_mount_t *, enum vtype, uid_t, gid_t,
mode_t, char *, dev_t, tmpfs_node_t **);
void tmpfs_free_node(tmpfs_mount_t *, tmpfs_node_t *);
int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
struct componentname *, char *);
int tmpfs_vnode_get(struct mount *, tmpfs_node_t *, struct vnode **);
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 *);
uint64_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 *);
int tmpfs_reg_resize(struct vnode *, off_t);
int tmpfs_truncate(struct vnode *, off_t);
int tmpfs_chflags(struct vnode *, int, struct ucred *, struct proc *);
int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct proc *);
int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *, struct proc *);
int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct proc *);
int tmpfs_chtimes(struct vnode *, const struct timespec *,
const struct timespec *, int, struct ucred *,
struct proc *);
void tmpfs_update(tmpfs_node_t *, int);
int tmpfs_zeropg(tmpfs_node_t *, voff_t, vaddr_t);
int tmpfs_uio_cached(tmpfs_node_t *);
int tmpfs_uiomove(tmpfs_node_t *, struct uio *, vsize_t);
void tmpfs_uio_uncache(tmpfs_node_t *);
void tmpfs_uio_cache(tmpfs_node_t *, voff_t, vaddr_t);
vaddr_t tmpfs_uio_lookup(tmpfs_node_t *, voff_t);
void tmpfs_mntmem_init(tmpfs_mount_t *, uint64_t);
void tmpfs_mntmem_destroy(tmpfs_mount_t *);
uint64_t tmpfs_pages_total(tmpfs_mount_t *);
uint64_t tmpfs_pages_avail(tmpfs_mount_t *);
int 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);
int 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(struct vnode *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(struct vnode *vp)
{
tmpfs_node_t *node = vp->v_data;
#ifdef KASSERT
KASSERT(node != NULL);
#endif
return node;
}
#endif