Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs.h revision 1.3
      1 /*	$NetBSD: tmpfs.h,v 1.3 2005/09/13 14:29:18 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 /* --------------------------------------------------------------------- */
     72 
     73 /*
     74  * This structure represents a node within tmpfs.
     75  */
     76 struct tmpfs_node {
     77 	LIST_ENTRY(tmpfs_node)	tn_entries;
     78 
     79 	enum vtype		tn_type;
     80 	ino_t			tn_id;
     81 
     82 #define	TMPFS_NODE_ACCESSED	(1 << 1)
     83 #define	TMPFS_NODE_MODIFIED	(1 << 2)
     84 #define	TMPFS_NODE_CHANGED	(1 << 3)
     85 	int			tn_status;
     86 
     87 	off_t			tn_size;
     88 
     89 	/* Attributes. */
     90 	uid_t			tn_uid;
     91 	gid_t			tn_gid;
     92 	mode_t			tn_mode;
     93 	int			tn_flags;
     94 	nlink_t			tn_links;
     95 	struct timespec		tn_atime;
     96 	struct timespec		tn_mtime;
     97 	struct timespec		tn_ctime;
     98 	struct timespec		tn_birthtime;
     99 	unsigned long		tn_gen;
    100 
    101 	struct vnode *		tn_vnode;
    102 
    103 	/* Used by tmpfs_lookup to store the affected directory entry during
    104 	 * DELETE and RENAME operations. */
    105 	struct tmpfs_dirent *	tn_lookup_dirent;
    106 
    107 	union {
    108 		/* Valid when tn_type == VBLK || tn_type == VCHR. */
    109 		struct {
    110 			dev_t			tn_rdev;
    111 		};
    112 
    113 		/* Valid when tn_type == VDIR. */
    114 		struct {
    115 			struct tmpfs_node *	tn_parent;
    116 			struct tmpfs_dir	tn_dir;
    117 
    118 			/* Used by tmpfs_readdir to speed up lookups. */
    119 			long			tn_readdir_lastn;
    120 			struct tmpfs_dirent *	tn_readdir_lastp;
    121 		};
    122 
    123 		/* Valid when tn_type == VLNK. */
    124 		struct {
    125 			char *			tn_link;
    126 		};
    127 
    128 		/* Valid when tn_type == VREG. */
    129 		struct {
    130 			struct uvm_object *	tn_aobj;
    131 			size_t			tn_aobj_pages;
    132 		};
    133 	};
    134 };
    135 LIST_HEAD(tmpfs_node_list, tmpfs_node);
    136 
    137 /* --------------------------------------------------------------------- */
    138 
    139 /*
    140  * This structure holds the information relative to a tmpfs instance.
    141  */
    142 struct tmpfs_mount {
    143 	size_t			tm_pages_max;
    144 	size_t			tm_pages_used;
    145 
    146 	struct tmpfs_node *	tm_root;
    147 	struct netexport	tm_export;
    148 
    149 	ino_t			tm_nodes_max;
    150 	ino_t			tm_nodes_last;
    151 	struct tmpfs_node_list	tm_nodes_used;
    152 	struct tmpfs_node_list	tm_nodes_avail;
    153 
    154 	struct tmpfs_pool	tm_dirent_pool;
    155 	struct tmpfs_pool	tm_node_pool;
    156 	struct tmpfs_str_pool	tm_str_pool;
    157 };
    158 
    159 /* --------------------------------------------------------------------- */
    160 
    161 /*
    162  * This structure maps a file identifier to a tmpfs node.  Used by the
    163  * NFS code.
    164  */
    165 struct tmpfs_fid {
    166 	uint16_t		tf_len;
    167 	uint16_t		tf_pad;
    168 	ino_t			tf_id;
    169 	unsigned long		tf_gen;
    170 };
    171 
    172 /* --------------------------------------------------------------------- */
    173 
    174 /*
    175  * Prototypes for tmpfs_subr.c.
    176  */
    177 
    178 int	tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
    179 	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
    180 	    char *, dev_t, struct proc *, struct tmpfs_node **);
    181 void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
    182 int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
    183 	    const char *, uint16_t, struct tmpfs_dirent **);
    184 void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
    185 	    boolean_t);
    186 int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **);
    187 void	tmpfs_free_vp(struct vnode *);
    188 int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
    189 	    struct componentname *, char *);
    190 void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
    191 void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
    192 struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
    193 			    struct componentname *cnp);
    194 int	tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
    195 int	tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
    196 int	tmpfs_dir_getdents(struct tmpfs_node *, struct uio *);
    197 int	tmpfs_reg_resize(struct vnode *, off_t);
    198 size_t	tmpfs_mem_info(boolean_t);
    199 int	tmpfs_chflags(struct vnode *, int, struct ucred *, struct proc *);
    200 int	tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct proc *);
    201 int	tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
    202 	    struct proc *);
    203 int	tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct proc *);
    204 int	tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
    205 	    int, struct ucred *, struct proc *);
    206 
    207 /* --------------------------------------------------------------------- */
    208 
    209 /*
    210  * Convenience macros to simplify some logical expressions.
    211  */
    212 #define IMPLIES(a, b) (!(a) || (b))
    213 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
    214 
    215 /* --------------------------------------------------------------------- */
    216 
    217 /*
    218  * Checks that the directory entry pointed by 'de' matches the name 'name'
    219  * with a length of 'len'.
    220  */
    221 #define TMPFS_DIRENT_MATCHES(de, name, len) \
    222     (de->td_namelen == (uint16_t)len && \
    223     memcmp((de)->td_name, (name), (de)->td_namelen) == 0)
    224 
    225 /* --------------------------------------------------------------------- */
    226 
    227 /*
    228  * Ensures that the node pointed by 'node' is a directory and that its
    229  * contents are consistent with respect to directories.
    230  */
    231 #define TMPFS_VALIDATE_DIR(node) \
    232     KASSERT((node)->tn_type == VDIR); \
    233     KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0);
    234 
    235 /* --------------------------------------------------------------------- */
    236 
    237 /*
    238  * Memory management stuff.
    239  */
    240 
    241 /* Amount of memory pages to reserve for the system (e.g., to not use by
    242  * tmpfs).
    243  * XXX: Should this be tunable through sysctl, for instance? */
    244 #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
    245 
    246 /* Returns the available memory for the given file system, which can be
    247  * its limit (set during mount time) or the amount of free memory, whichever
    248  * is lower. */
    249 static inline size_t
    250 TMPFS_PAGES_MAX(struct tmpfs_mount *tmp)
    251 {
    252 	size_t freepages;
    253 
    254 	freepages = tmpfs_mem_info(FALSE);
    255 	if (freepages < TMPFS_PAGES_RESERVED)
    256 		freepages = 0;
    257 	else
    258 		freepages -= TMPFS_PAGES_RESERVED;
    259 
    260 	return MIN(tmp->tm_pages_max, freepages + tmp->tm_pages_used);
    261 }
    262 
    263 #define TMPFS_PAGES_AVAIL(tmp) (TMPFS_PAGES_MAX(tmp) - (tmp)->tm_pages_used)
    264 
    265 /* --------------------------------------------------------------------- */
    266 
    267 /*
    268  * Macros/functions to convert from generic data structures to tmpfs
    269  * specific ones.
    270  *
    271  * Macros are used when no sanity checks have to be done, as they provide
    272  * the fastest conversion.  On the other hand, inlined functions are used
    273  * when expensive sanity checks are enabled, mostly because the checks
    274  * have to be done separately from the return value.
    275  */
    276 
    277 #if defined(DIAGNOSTIC)
    278 static inline
    279 struct tmpfs_mount *
    280 VFS_TO_TMPFS(struct mount *mp)
    281 {
    282 	struct tmpfs_mount *tmp;
    283 
    284 	KASSERT((mp) != NULL && (mp)->mnt_data != NULL);
    285 	tmp = (struct tmpfs_mount *)(mp)->mnt_data;
    286 	KASSERT(TMPFS_PAGES_MAX(tmp) >= tmp->tm_pages_used);
    287 	return tmp;
    288 }
    289 
    290 static inline
    291 struct tmpfs_node *
    292 VP_TO_TMPFS_NODE(struct vnode *vp)
    293 {
    294 	struct tmpfs_node *node;
    295 
    296 	KASSERT((vp) != NULL && (vp)->v_data != NULL);
    297 	node = (struct tmpfs_node *)vp->v_data;
    298 	return node;
    299 }
    300 
    301 static inline
    302 struct tmpfs_node *
    303 VP_TO_TMPFS_DIR(struct vnode *vp)
    304 {
    305 	struct tmpfs_node *node;
    306 
    307 	node = VP_TO_TMPFS_NODE(vp);
    308 	TMPFS_VALIDATE_DIR(node);
    309 	return node;
    310 }
    311 #else
    312 #	define VFS_TO_TMPFS(mp) ((struct tmpfs_mount *)mp->mnt_data)
    313 #	define VP_TO_TMPFS_NODE(vp) ((struct tmpfs_node *)vp->v_data)
    314 #	define VP_TO_TMPFS_DIR(vp) VP_TO_TMPFS_NODE(vp)
    315 #endif
    316 
    317 #endif /* _KERNEL */
    318 
    319 /* ---------------------------------------------------------------------
    320  * USER AND KERNEL DEFINITIONS
    321  * --------------------------------------------------------------------- */
    322 
    323 /*
    324  * This structure is used to communicate mount parameters between userland
    325  * and kernel space.
    326  */
    327 #define TMPFS_ARGS_VERSION	1
    328 struct tmpfs_args {
    329 	int			ta_version;
    330 
    331 	/* Size counters. */
    332 	ino_t			ta_nodes_max;
    333 	off_t			ta_size_max;
    334 
    335 	/* Root node attributes. */
    336 	uid_t			ta_root_uid;
    337 	gid_t			ta_root_gid;
    338 	mode_t			ta_root_mode;
    339 
    340 	/* Used to update NFS export properties.  Due to the way these
    341 	 * fields are used by mountd(8), they must come first, but as
    342 	 * I would like to remove this restriction, I'm placing them
    343 	 * here (there is no problem in doing so because, ATM, mountd(8)
    344 	 * does not recognize tmpfs).  XXX */
    345 	const char *		ta_fspec;
    346 	struct export_args	ta_export;
    347 };
    348