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