Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs.h revision 1.4
      1 /*	$NetBSD: tmpfs.h,v 1.4 2005/09/15 12:34:35 yamt 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 	struct netexport	tm_export;
    153 
    154 	ino_t			tm_nodes_max;
    155 	ino_t			tm_nodes_last;
    156 	struct tmpfs_node_list	tm_nodes_used;
    157 	struct tmpfs_node_list	tm_nodes_avail;
    158 
    159 	struct tmpfs_pool	tm_dirent_pool;
    160 	struct tmpfs_pool	tm_node_pool;
    161 	struct tmpfs_str_pool	tm_str_pool;
    162 };
    163 
    164 /* --------------------------------------------------------------------- */
    165 
    166 /*
    167  * This structure maps a file identifier to a tmpfs node.  Used by the
    168  * NFS code.
    169  */
    170 struct tmpfs_fid {
    171 	uint16_t		tf_len;
    172 	uint16_t		tf_pad;
    173 	ino_t			tf_id;
    174 	unsigned long		tf_gen;
    175 };
    176 
    177 /* --------------------------------------------------------------------- */
    178 
    179 /*
    180  * Prototypes for tmpfs_subr.c.
    181  */
    182 
    183 int	tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
    184 	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
    185 	    char *, dev_t, struct proc *, struct tmpfs_node **);
    186 void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
    187 int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
    188 	    const char *, uint16_t, struct tmpfs_dirent **);
    189 void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
    190 	    boolean_t);
    191 int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **);
    192 void	tmpfs_free_vp(struct vnode *);
    193 int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
    194 	    struct componentname *, char *);
    195 void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
    196 void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
    197 struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
    198 			    struct componentname *cnp);
    199 int	tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
    200 int	tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
    201 struct tmpfs_dirent *	tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
    202 int	tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
    203 int	tmpfs_reg_resize(struct vnode *, off_t);
    204 size_t	tmpfs_mem_info(boolean_t);
    205 int	tmpfs_chflags(struct vnode *, int, struct ucred *, struct proc *);
    206 int	tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct proc *);
    207 int	tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
    208 	    struct proc *);
    209 int	tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct proc *);
    210 int	tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
    211 	    int, struct ucred *, struct proc *);
    212 
    213 /* --------------------------------------------------------------------- */
    214 
    215 /*
    216  * Convenience macros to simplify some logical expressions.
    217  */
    218 #define IMPLIES(a, b) (!(a) || (b))
    219 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
    220 
    221 /* --------------------------------------------------------------------- */
    222 
    223 /*
    224  * Checks that the directory entry pointed by 'de' matches the name 'name'
    225  * with a length of 'len'.
    226  */
    227 #define TMPFS_DIRENT_MATCHES(de, name, len) \
    228     (de->td_namelen == (uint16_t)len && \
    229     memcmp((de)->td_name, (name), (de)->td_namelen) == 0)
    230 
    231 /* --------------------------------------------------------------------- */
    232 
    233 /*
    234  * Ensures that the node pointed by 'node' is a directory and that its
    235  * contents are consistent with respect to directories.
    236  */
    237 #define TMPFS_VALIDATE_DIR(node) \
    238     KASSERT((node)->tn_type == VDIR); \
    239     KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
    240     KASSERT((node)->tn_readdir_lastp == NULL || \
    241 	TMPFS_DIRCOOKIE((node)->tn_readdir_lastp) == (node)->tn_readdir_lastn);
    242 
    243 /* --------------------------------------------------------------------- */
    244 
    245 /*
    246  * Memory management stuff.
    247  */
    248 
    249 /* Amount of memory pages to reserve for the system (e.g., to not use by
    250  * tmpfs).
    251  * XXX: Should this be tunable through sysctl, for instance? */
    252 #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
    253 
    254 /* Returns the available memory for the given file system, which can be
    255  * its limit (set during mount time) or the amount of free memory, whichever
    256  * is lower. */
    257 static inline size_t
    258 TMPFS_PAGES_MAX(struct tmpfs_mount *tmp)
    259 {
    260 	size_t freepages;
    261 
    262 	freepages = tmpfs_mem_info(FALSE);
    263 	if (freepages < TMPFS_PAGES_RESERVED)
    264 		freepages = 0;
    265 	else
    266 		freepages -= TMPFS_PAGES_RESERVED;
    267 
    268 	return MIN(tmp->tm_pages_max, freepages + tmp->tm_pages_used);
    269 }
    270 
    271 #define TMPFS_PAGES_AVAIL(tmp) (TMPFS_PAGES_MAX(tmp) - (tmp)->tm_pages_used)
    272 
    273 /* --------------------------------------------------------------------- */
    274 
    275 /*
    276  * Macros/functions to convert from generic data structures to tmpfs
    277  * specific ones.
    278  *
    279  * Macros are used when no sanity checks have to be done, as they provide
    280  * the fastest conversion.  On the other hand, inlined functions are used
    281  * when expensive sanity checks are enabled, mostly because the checks
    282  * have to be done separately from the return value.
    283  */
    284 
    285 #if defined(DIAGNOSTIC)
    286 static inline
    287 struct tmpfs_mount *
    288 VFS_TO_TMPFS(struct mount *mp)
    289 {
    290 	struct tmpfs_mount *tmp;
    291 
    292 	KASSERT((mp) != NULL && (mp)->mnt_data != NULL);
    293 	tmp = (struct tmpfs_mount *)(mp)->mnt_data;
    294 	KASSERT(TMPFS_PAGES_MAX(tmp) >= tmp->tm_pages_used);
    295 	return tmp;
    296 }
    297 
    298 static inline
    299 struct tmpfs_node *
    300 VP_TO_TMPFS_NODE(struct vnode *vp)
    301 {
    302 	struct tmpfs_node *node;
    303 
    304 	KASSERT((vp) != NULL && (vp)->v_data != NULL);
    305 	node = (struct tmpfs_node *)vp->v_data;
    306 	return node;
    307 }
    308 
    309 static inline
    310 struct tmpfs_node *
    311 VP_TO_TMPFS_DIR(struct vnode *vp)
    312 {
    313 	struct tmpfs_node *node;
    314 
    315 	node = VP_TO_TMPFS_NODE(vp);
    316 	TMPFS_VALIDATE_DIR(node);
    317 	return node;
    318 }
    319 #else
    320 #	define VFS_TO_TMPFS(mp) ((struct tmpfs_mount *)mp->mnt_data)
    321 #	define VP_TO_TMPFS_NODE(vp) ((struct tmpfs_node *)vp->v_data)
    322 #	define VP_TO_TMPFS_DIR(vp) VP_TO_TMPFS_NODE(vp)
    323 #endif
    324 
    325 #endif /* _KERNEL */
    326 
    327 /* ---------------------------------------------------------------------
    328  * USER AND KERNEL DEFINITIONS
    329  * --------------------------------------------------------------------- */
    330 
    331 /*
    332  * This structure is used to communicate mount parameters between userland
    333  * and kernel space.
    334  */
    335 #define TMPFS_ARGS_VERSION	1
    336 struct tmpfs_args {
    337 	int			ta_version;
    338 
    339 	/* Size counters. */
    340 	ino_t			ta_nodes_max;
    341 	off_t			ta_size_max;
    342 
    343 	/* Root node attributes. */
    344 	uid_t			ta_root_uid;
    345 	gid_t			ta_root_gid;
    346 	mode_t			ta_root_mode;
    347 
    348 	/* Used to update NFS export properties.  Due to the way these
    349 	 * fields are used by mountd(8), they must come first, but as
    350 	 * I would like to remove this restriction, I'm placing them
    351 	 * here (there is no problem in doing so because, ATM, mountd(8)
    352 	 * does not recognize tmpfs).  XXX */
    353 	const char *		ta_fspec;
    354 	struct export_args	ta_export;
    355 };
    356