Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs.h revision 1.2
      1  1.2  jmmv /*	$NetBSD: tmpfs.h,v 1.2 2005/09/10 22:28:57 jmmv Exp $	*/
      2  1.1  jmmv 
      3  1.1  jmmv /*
      4  1.1  jmmv  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  1.1  jmmv  * All rights reserved.
      6  1.1  jmmv  *
      7  1.1  jmmv  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  jmmv  * by Julio M. Merino Vidal.
      9  1.1  jmmv  *
     10  1.1  jmmv  * Redistribution and use in source and binary forms, with or without
     11  1.1  jmmv  * modification, are permitted provided that the following conditions
     12  1.1  jmmv  * are met:
     13  1.1  jmmv  * 1. Redistributions of source code must retain the above copyright
     14  1.1  jmmv  *    notice, this list of conditions and the following disclaimer.
     15  1.1  jmmv  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  jmmv  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  jmmv  *    documentation and/or other materials provided with the distribution.
     18  1.1  jmmv  * 3. All advertising materials mentioning features or use of this software
     19  1.1  jmmv  *    must display the following acknowledgement:
     20  1.1  jmmv  *        This product includes software developed by the NetBSD
     21  1.1  jmmv  *        Foundation, Inc. and its contributors.
     22  1.1  jmmv  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  jmmv  *    contributors may be used to endorse or promote products derived
     24  1.1  jmmv  *    from this software without specific prior written permission.
     25  1.1  jmmv  *
     26  1.1  jmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  jmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  jmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  jmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  jmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  jmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  jmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  jmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  jmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  jmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  jmmv  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  jmmv  */
     38  1.1  jmmv 
     39  1.1  jmmv #if !defined(_TMPFS_H_)
     40  1.1  jmmv #  define _TMPFS_H_
     41  1.1  jmmv #else
     42  1.1  jmmv #  error "tmpfs.h cannot be included multiple times."
     43  1.1  jmmv #endif
     44  1.1  jmmv 
     45  1.1  jmmv /* ---------------------------------------------------------------------
     46  1.1  jmmv  * KERNEL-SPECIFIC DEFINITIONS
     47  1.1  jmmv  * --------------------------------------------------------------------- */
     48  1.1  jmmv 
     49  1.1  jmmv #if defined(_KERNEL)
     50  1.1  jmmv 
     51  1.1  jmmv #include <sys/dirent.h>
     52  1.1  jmmv #include <sys/mount.h>
     53  1.1  jmmv #include <sys/queue.h>
     54  1.1  jmmv #include <sys/vnode.h>
     55  1.1  jmmv 
     56  1.1  jmmv #include <fs/tmpfs/tmpfs_pool.h>
     57  1.1  jmmv 
     58  1.1  jmmv /* --------------------------------------------------------------------- */
     59  1.1  jmmv 
     60  1.1  jmmv /*
     61  1.1  jmmv  * This structure holds a directory entry.
     62  1.1  jmmv  */
     63  1.1  jmmv struct tmpfs_dirent {
     64  1.1  jmmv 	TAILQ_ENTRY(tmpfs_dirent)	td_entries;
     65  1.1  jmmv 	uint16_t			td_namelen;
     66  1.1  jmmv 	char *				td_name;
     67  1.1  jmmv 	struct tmpfs_node *		td_node;
     68  1.1  jmmv };
     69  1.1  jmmv TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
     70  1.1  jmmv 
     71  1.1  jmmv /* --------------------------------------------------------------------- */
     72  1.1  jmmv 
     73  1.1  jmmv /*
     74  1.1  jmmv  * This structure represents a node within tmpfs.
     75  1.1  jmmv  */
     76  1.1  jmmv struct tmpfs_node {
     77  1.1  jmmv 	LIST_ENTRY(tmpfs_node)	tn_entries;
     78  1.1  jmmv 
     79  1.1  jmmv 	enum vtype		tn_type;
     80  1.1  jmmv 	ino_t			tn_id;
     81  1.1  jmmv 
     82  1.1  jmmv #define	TMPFS_NODE_ACCESSED	(1 << 1)
     83  1.1  jmmv #define	TMPFS_NODE_MODIFIED	(1 << 2)
     84  1.1  jmmv #define	TMPFS_NODE_CHANGED	(1 << 3)
     85  1.1  jmmv 	int			tn_status;
     86  1.1  jmmv 
     87  1.1  jmmv 	off_t			tn_size;
     88  1.1  jmmv 
     89  1.1  jmmv 	/* Attributes. */
     90  1.1  jmmv 	uid_t			tn_uid;
     91  1.1  jmmv 	gid_t			tn_gid;
     92  1.1  jmmv 	mode_t			tn_mode;
     93  1.1  jmmv 	int			tn_flags;
     94  1.1  jmmv 	nlink_t			tn_links;
     95  1.1  jmmv 	struct timespec		tn_atime;
     96  1.1  jmmv 	struct timespec		tn_mtime;
     97  1.1  jmmv 	struct timespec		tn_ctime;
     98  1.1  jmmv 	struct timespec		tn_birthtime;
     99  1.1  jmmv 	unsigned long		tn_gen;
    100  1.1  jmmv 
    101  1.1  jmmv 	struct vnode *		tn_vnode;
    102  1.1  jmmv 
    103  1.1  jmmv 	/* Used by tmpfs_lookup to store the affected directory entry during
    104  1.1  jmmv 	 * DELETE and RENAME operations. */
    105  1.1  jmmv 	struct tmpfs_dirent *	tn_lookup_dirent;
    106  1.1  jmmv 
    107  1.1  jmmv 	union {
    108  1.1  jmmv 		/* Valid when tn_type == VBLK || tn_type == VCHR. */
    109  1.1  jmmv 		struct {
    110  1.1  jmmv 			dev_t			tn_rdev;
    111  1.1  jmmv 		};
    112  1.1  jmmv 
    113  1.1  jmmv 		/* Valid when tn_type == VDIR. */
    114  1.1  jmmv 		struct {
    115  1.1  jmmv 			struct tmpfs_node *	tn_parent;
    116  1.1  jmmv 			struct tmpfs_dir	tn_dir;
    117  1.1  jmmv 
    118  1.1  jmmv 			/* Used by tmpfs_readdir to speed up lookups. */
    119  1.1  jmmv 			long			tn_readdir_lastn;
    120  1.1  jmmv 			struct tmpfs_dirent *	tn_readdir_lastp;
    121  1.1  jmmv 		};
    122  1.1  jmmv 
    123  1.1  jmmv 		/* Valid when tn_type == VLNK. */
    124  1.1  jmmv 		struct {
    125  1.1  jmmv 			char *			tn_link;
    126  1.1  jmmv 		};
    127  1.1  jmmv 
    128  1.1  jmmv 		/* Valid when tn_type == VREG. */
    129  1.1  jmmv 		struct {
    130  1.1  jmmv 			struct uvm_object *	tn_aobj;
    131  1.1  jmmv 			size_t			tn_aobj_pages;
    132  1.1  jmmv 			vaddr_t			tn_va;
    133  1.1  jmmv 		};
    134  1.1  jmmv 	};
    135  1.1  jmmv };
    136  1.1  jmmv LIST_HEAD(tmpfs_node_list, tmpfs_node);
    137  1.1  jmmv 
    138  1.1  jmmv /* --------------------------------------------------------------------- */
    139  1.1  jmmv 
    140  1.1  jmmv /*
    141  1.1  jmmv  * This structure holds the information relative to a tmpfs instance.
    142  1.1  jmmv  */
    143  1.1  jmmv struct tmpfs_mount {
    144  1.1  jmmv 	size_t			tm_pages_max;
    145  1.1  jmmv 	size_t			tm_pages_used;
    146  1.1  jmmv 
    147  1.1  jmmv 	struct tmpfs_node *	tm_root;
    148  1.1  jmmv 	struct netexport	tm_export;
    149  1.1  jmmv 
    150  1.1  jmmv 	ino_t			tm_nodes_max;
    151  1.1  jmmv 	ino_t			tm_nodes_last;
    152  1.1  jmmv 	struct tmpfs_node_list	tm_nodes_used;
    153  1.1  jmmv 	struct tmpfs_node_list	tm_nodes_avail;
    154  1.1  jmmv 
    155  1.1  jmmv 	struct tmpfs_pool	tm_dirent_pool;
    156  1.1  jmmv 	struct tmpfs_pool	tm_node_pool;
    157  1.1  jmmv 	struct tmpfs_str_pool	tm_str_pool;
    158  1.1  jmmv };
    159  1.1  jmmv 
    160  1.1  jmmv /* --------------------------------------------------------------------- */
    161  1.1  jmmv 
    162  1.1  jmmv /*
    163  1.1  jmmv  * This structure maps a file identifier to a tmpfs node.  Used by the
    164  1.1  jmmv  * NFS code.
    165  1.1  jmmv  */
    166  1.1  jmmv struct tmpfs_fid {
    167  1.1  jmmv 	uint16_t		tf_len;
    168  1.1  jmmv 	uint16_t		tf_pad;
    169  1.1  jmmv 	ino_t			tf_id;
    170  1.1  jmmv 	unsigned long		tf_gen;
    171  1.1  jmmv };
    172  1.1  jmmv 
    173  1.1  jmmv /* --------------------------------------------------------------------- */
    174  1.1  jmmv 
    175  1.1  jmmv /*
    176  1.1  jmmv  * Prototypes for tmpfs_subr.c.
    177  1.1  jmmv  */
    178  1.1  jmmv 
    179  1.1  jmmv int	tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
    180  1.1  jmmv 	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
    181  1.1  jmmv 	    char *, dev_t, struct proc *, struct tmpfs_node **);
    182  1.1  jmmv void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
    183  1.1  jmmv int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
    184  1.1  jmmv 	    const char *, uint16_t, struct tmpfs_dirent **);
    185  1.1  jmmv void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
    186  1.1  jmmv 	    boolean_t);
    187  1.1  jmmv int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **);
    188  1.1  jmmv void	tmpfs_free_vp(struct vnode *);
    189  1.1  jmmv int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
    190  1.1  jmmv 	    struct componentname *, char *);
    191  1.1  jmmv void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
    192  1.1  jmmv void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
    193  1.1  jmmv struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
    194  1.1  jmmv 			    struct componentname *cnp);
    195  1.1  jmmv int	tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
    196  1.1  jmmv int	tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
    197  1.1  jmmv int	tmpfs_dir_getdents(struct tmpfs_node *, struct uio *);
    198  1.1  jmmv int	tmpfs_reg_resize(struct vnode *, off_t);
    199  1.1  jmmv size_t	tmpfs_mem_info(boolean_t);
    200  1.1  jmmv int	tmpfs_chflags(struct vnode *, int, struct ucred *, struct proc *);
    201  1.1  jmmv int	tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct proc *);
    202  1.1  jmmv int	tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
    203  1.1  jmmv 	    struct proc *);
    204  1.1  jmmv int	tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct proc *);
    205  1.1  jmmv int	tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
    206  1.1  jmmv 	    int, struct ucred *, struct proc *);
    207  1.1  jmmv 
    208  1.1  jmmv /* --------------------------------------------------------------------- */
    209  1.1  jmmv 
    210  1.1  jmmv /*
    211  1.1  jmmv  * Convenience macros to simplify some logical expressions.
    212  1.1  jmmv  */
    213  1.1  jmmv #define IMPLIES(a, b) (!(a) || (b))
    214  1.1  jmmv #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
    215  1.1  jmmv 
    216  1.1  jmmv /* --------------------------------------------------------------------- */
    217  1.1  jmmv 
    218  1.1  jmmv /*
    219  1.1  jmmv  * Checks that the directory entry pointed by 'de' matches the name 'name'
    220  1.1  jmmv  * with a length of 'len'.
    221  1.1  jmmv  */
    222  1.1  jmmv #define TMPFS_DIRENT_MATCHES(de, name, len) \
    223  1.1  jmmv     (de->td_namelen == (uint16_t)len && \
    224  1.1  jmmv     memcmp((de)->td_name, (name), (de)->td_namelen) == 0)
    225  1.1  jmmv 
    226  1.1  jmmv /* --------------------------------------------------------------------- */
    227  1.1  jmmv 
    228  1.1  jmmv /*
    229  1.1  jmmv  * Ensures that the node pointed by 'node' is a directory and that its
    230  1.1  jmmv  * contents are consistent with respect to directories.
    231  1.1  jmmv  */
    232  1.1  jmmv #define TMPFS_VALIDATE_DIR(node) \
    233  1.1  jmmv     KASSERT((node)->tn_type == VDIR); \
    234  1.1  jmmv     KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0);
    235  1.1  jmmv 
    236  1.1  jmmv /* --------------------------------------------------------------------- */
    237  1.1  jmmv 
    238  1.1  jmmv /*
    239  1.1  jmmv  * Memory management stuff.
    240  1.1  jmmv  */
    241  1.1  jmmv 
    242  1.1  jmmv /* Amount of memory pages to reserve for the system (e.g., to not use by
    243  1.1  jmmv  * tmpfs).
    244  1.1  jmmv  * XXX: Should this be tunable through sysctl, for instance? */
    245  1.1  jmmv #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
    246  1.1  jmmv 
    247  1.2  jmmv /* Returns the available memory for the given file system, which can be
    248  1.1  jmmv  * its limit (set during mount time) or the amount of free memory, whichever
    249  1.1  jmmv  * is lower. */
    250  1.1  jmmv static inline size_t
    251  1.1  jmmv TMPFS_PAGES_MAX(struct tmpfs_mount *tmp)
    252  1.1  jmmv {
    253  1.1  jmmv 	size_t freepages;
    254  1.1  jmmv 
    255  1.1  jmmv 	freepages = tmpfs_mem_info(FALSE);
    256  1.1  jmmv 	if (freepages < TMPFS_PAGES_RESERVED)
    257  1.1  jmmv 		freepages = 0;
    258  1.1  jmmv 	else
    259  1.1  jmmv 		freepages -= TMPFS_PAGES_RESERVED;
    260  1.1  jmmv 
    261  1.1  jmmv 	return MIN(tmp->tm_pages_max, freepages + tmp->tm_pages_used);
    262  1.1  jmmv }
    263  1.1  jmmv 
    264  1.1  jmmv #define TMPFS_PAGES_AVAIL(tmp) (TMPFS_PAGES_MAX(tmp) - (tmp)->tm_pages_used)
    265  1.1  jmmv 
    266  1.1  jmmv /* --------------------------------------------------------------------- */
    267  1.1  jmmv 
    268  1.1  jmmv /*
    269  1.1  jmmv  * Macros/functions to convert from generic data structures to tmpfs
    270  1.1  jmmv  * specific ones.
    271  1.1  jmmv  *
    272  1.1  jmmv  * Macros are used when no sanity checks have to be done, as they provide
    273  1.1  jmmv  * the fastest conversion.  On the other hand, inlined functions are used
    274  1.1  jmmv  * when expensive sanity checks are enabled, mostly because the checks
    275  1.1  jmmv  * have to be done separately from the return value.
    276  1.1  jmmv  */
    277  1.1  jmmv 
    278  1.1  jmmv #if defined(DIAGNOSTIC)
    279  1.1  jmmv static inline
    280  1.1  jmmv struct tmpfs_mount *
    281  1.1  jmmv VFS_TO_TMPFS(struct mount *mp)
    282  1.1  jmmv {
    283  1.1  jmmv 	struct tmpfs_mount *tmp;
    284  1.1  jmmv 
    285  1.1  jmmv 	KASSERT((mp) != NULL && (mp)->mnt_data != NULL);
    286  1.1  jmmv 	tmp = (struct tmpfs_mount *)(mp)->mnt_data;
    287  1.1  jmmv 	KASSERT(TMPFS_PAGES_MAX(tmp) >= tmp->tm_pages_used);
    288  1.1  jmmv 	return tmp;
    289  1.1  jmmv }
    290  1.1  jmmv 
    291  1.1  jmmv static inline
    292  1.1  jmmv struct tmpfs_node *
    293  1.1  jmmv VP_TO_TMPFS_NODE(struct vnode *vp)
    294  1.1  jmmv {
    295  1.1  jmmv 	struct tmpfs_node *node;
    296  1.1  jmmv 
    297  1.1  jmmv 	KASSERT((vp) != NULL && (vp)->v_data != NULL);
    298  1.1  jmmv 	node = (struct tmpfs_node *)vp->v_data;
    299  1.1  jmmv 	KASSERT(node->tn_size == vp->v_size);
    300  1.1  jmmv 	return node;
    301  1.1  jmmv }
    302  1.1  jmmv 
    303  1.1  jmmv static inline
    304  1.1  jmmv struct tmpfs_node *
    305  1.1  jmmv VP_TO_TMPFS_DIR(struct vnode *vp)
    306  1.1  jmmv {
    307  1.1  jmmv 	struct tmpfs_node *node;
    308  1.1  jmmv 
    309  1.1  jmmv 	node = VP_TO_TMPFS_NODE(vp);
    310  1.1  jmmv 	TMPFS_VALIDATE_DIR(node);
    311  1.1  jmmv 	return node;
    312  1.1  jmmv }
    313  1.1  jmmv #else
    314  1.1  jmmv #	define VFS_TO_TMPFS(mp) ((struct tmpfs_mount *)mp->mnt_data)
    315  1.1  jmmv #	define VP_TO_TMPFS_NODE(vp) ((struct tmpfs_node *)vp->v_data)
    316  1.1  jmmv #	define VP_TO_TMPFS_DIR(vp) VP_TO_TMPFS_NODE(vp)
    317  1.1  jmmv #endif
    318  1.1  jmmv 
    319  1.1  jmmv #endif /* _KERNEL */
    320  1.1  jmmv 
    321  1.1  jmmv /* ---------------------------------------------------------------------
    322  1.1  jmmv  * USER AND KERNEL DEFINITIONS
    323  1.1  jmmv  * --------------------------------------------------------------------- */
    324  1.1  jmmv 
    325  1.1  jmmv /*
    326  1.1  jmmv  * This structure is used to communicate mount parameters between userland
    327  1.1  jmmv  * and kernel space.
    328  1.1  jmmv  */
    329  1.1  jmmv #define TMPFS_ARGS_VERSION	1
    330  1.1  jmmv struct tmpfs_args {
    331  1.1  jmmv 	int			ta_version;
    332  1.1  jmmv 
    333  1.1  jmmv 	/* Size counters. */
    334  1.1  jmmv 	ino_t			ta_nodes_max;
    335  1.1  jmmv 	off_t			ta_size_max;
    336  1.1  jmmv 
    337  1.1  jmmv 	/* Root node attributes. */
    338  1.1  jmmv 	uid_t			ta_root_uid;
    339  1.1  jmmv 	gid_t			ta_root_gid;
    340  1.1  jmmv 	mode_t			ta_root_mode;
    341  1.1  jmmv 
    342  1.1  jmmv 	/* Used to update NFS export properties.  Due to the way these
    343  1.1  jmmv 	 * fields are used by mountd(8), they must come first, but as
    344  1.1  jmmv 	 * I would like to remove this restriction, I'm placing them
    345  1.1  jmmv 	 * here (there is no problem in doing so because, ATM, mountd(8)
    346  1.1  jmmv 	 * does not recognize tmpfs).  XXX */
    347  1.1  jmmv 	const char *		ta_fspec;
    348  1.1  jmmv 	struct export_args	ta_export;
    349  1.1  jmmv };
    350