Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs.h revision 1.42
      1 /*	$NetBSD: tmpfs.h,v 1.42 2011/05/29 01:00:50 rmind Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006, 2007 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, developed as part of Google's Summer of Code
      9  * 2005 program.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #ifndef _FS_TMPFS_H_
     34 #define _FS_TMPFS_H_
     35 
     36 #if !defined(_KERNEL) && !defined(_KMEMUSER)
     37 #error "not supposed to be exposed to userland"
     38 #endif
     39 
     40 #include <sys/dirent.h>
     41 #include <sys/mount.h>
     42 #include <sys/pool.h>
     43 #include <sys/queue.h>
     44 #include <sys/vnode.h>
     45 
     46 /*
     47  * Internal representation of a tmpfs directory entry.
     48  *
     49  * All fields are protected by vnode lock.
     50  */
     51 typedef struct tmpfs_dirent {
     52 	TAILQ_ENTRY(tmpfs_dirent)	td_entries;
     53 
     54 	/* Pointer to the inode this entry refers to. */
     55 	struct tmpfs_node *		td_node;
     56 
     57 	/* Name and its length. */
     58 	char *				td_name;
     59 	uint16_t			td_namelen;
     60 } tmpfs_dirent_t;
     61 
     62 TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
     63 
     64 #if defined(_KERNEL)
     65 
     66 /* Validate maximum td_namelen length. */
     67 CTASSERT(MAXNAMLEN < UINT16_MAX);
     68 
     69 #define	TMPFS_DIRCOOKIE_DOT	0
     70 #define	TMPFS_DIRCOOKIE_DOTDOT	1
     71 #define	TMPFS_DIRCOOKIE_EOF	2
     72 
     73 /*
     74  * Each entry in a directory has a cookie that identifies it.  Cookies
     75  * supersede offsets within directories, as tmpfs has no offsets as such.
     76  *
     77  * The '.', '..' and the end of directory markers have fixed cookies,
     78  * which cannot collide with the cookies generated by other entries.
     79  *
     80  * The cookies for the other entries are generated based on the memory
     81  * address of their representative meta-data structure.
     82  *
     83  * XXX: Truncating directory cookies to 31 bits now - workaround for
     84  * problem with Linux compat, see PR/32034.
     85  */
     86 static inline off_t
     87 tmpfs_dircookie(tmpfs_dirent_t *de)
     88 {
     89 	off_t cookie;
     90 
     91 	cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF;
     92 	KASSERT(cookie != TMPFS_DIRCOOKIE_DOT);
     93 	KASSERT(cookie != TMPFS_DIRCOOKIE_DOTDOT);
     94 	KASSERT(cookie != TMPFS_DIRCOOKIE_EOF);
     95 
     96 	return cookie;
     97 }
     98 #endif
     99 
    100 /*
    101  * Internal representation of a tmpfs file system node -- inode.
    102  *
    103  * This structure is splitted in two parts: one holds attributes common
    104  * to all file types and the other holds data that is only applicable to
    105  * a particular type.
    106  *
    107  * All fields are protected by vnode lock.  The vnode association itself
    108  * is protected by tmpfs_node_t::tn_vlock.
    109  */
    110 typedef struct tmpfs_node {
    111 	LIST_ENTRY(tmpfs_node)	tn_entries;
    112 
    113 	/* The inode type: VBLK, VCHR, VDIR, VFIFO, VLNK, VREG or VSOCK. */
    114 	enum vtype		tn_type;
    115 
    116 	/* Inode identifier. */
    117 	ino_t			tn_id;
    118 
    119 	/* Inode status flags (for operations in delayed manner). */
    120 	int			tn_status;
    121 
    122 	/* The inode size. */
    123 	off_t			tn_size;
    124 
    125 	/* Generic node attributes. */
    126 	uid_t			tn_uid;
    127 	gid_t			tn_gid;
    128 	mode_t			tn_mode;
    129 	int			tn_flags;
    130 	nlink_t			tn_links;
    131 	struct timespec		tn_atime;
    132 	struct timespec		tn_mtime;
    133 	struct timespec		tn_ctime;
    134 	struct timespec		tn_birthtime;
    135 	unsigned long		tn_gen;
    136 
    137 	/* Head of byte-level lock list (used by tmpfs_advlock). */
    138 	struct lockf *		tn_lockf;
    139 
    140 	/*
    141 	 * Each inode has a corresponding vnode.  It is a bi-directional
    142 	 * association.  Whenever vnode is allocated, its v_data field is
    143 	 * set to the inode it reference, and tmpfs_node_t::tn_vnode is
    144 	 * set to point to the said vnode.
    145 	 *
    146 	 * Further attempts to allocate a vnode for this same node will
    147 	 * result in returning a new reference to the value stored in
    148 	 * tn_vnode.  It may be NULL when the node is unused (that is,
    149 	 * no vnode has been allocated or it has been reclaimed).
    150 	 */
    151 	kmutex_t		tn_vlock;
    152 	vnode_t *		tn_vnode;
    153 
    154 	union {
    155 		/* Type case: VBLK or VCHR. */
    156 		struct {
    157 			dev_t			tn_rdev;
    158 		} tn_dev;
    159 
    160 		/* Type case: VDIR. */
    161 		struct {
    162 			/* Parent directory (root inode points to itself). */
    163 			struct tmpfs_node *	tn_parent;
    164 
    165 			/* List of directory entries. */
    166 			struct tmpfs_dir	tn_dir;
    167 
    168 			/*
    169 			 * Number and pointer of the last directory entry
    170 			 * returned by the readdir(3) operation.
    171 			 */
    172 			off_t			tn_readdir_lastn;
    173 			struct tmpfs_dirent *	tn_readdir_lastp;
    174 		} tn_dir;
    175 
    176 		/* Type case: VLNK. */
    177 		struct tn_lnk {
    178 			/* The link's target. */
    179 			char *			tn_link;
    180 		} tn_lnk;
    181 
    182 		/* Type case: VREG. */
    183 		struct tn_reg {
    184 			/* Underlying UVM object to store contents. */
    185 			struct uvm_object *	tn_aobj;
    186 			size_t			tn_aobj_pages;
    187 		} tn_reg;
    188 	} tn_spec;
    189 } tmpfs_node_t;
    190 
    191 #if defined(_KERNEL)
    192 
    193 LIST_HEAD(tmpfs_node_list, tmpfs_node);
    194 
    195 /* Status flags. */
    196 #define	TMPFS_NODE_ACCESSED	0x01
    197 #define	TMPFS_NODE_MODIFIED	0x02
    198 #define	TMPFS_NODE_CHANGED	0x04
    199 
    200 #define	TMPFS_NODE_STATUSALL	\
    201     (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED)
    202 
    203 /* White-out inode indicator. */
    204 #define	TMPFS_NODE_WHITEOUT	((tmpfs_node_t *)-1)
    205 
    206 /*
    207  * Internal representation of a tmpfs mount point.
    208  */
    209 typedef struct tmpfs_mount {
    210 	/* Limit and number of bytes in use by the file system. */
    211 	uint64_t		tm_mem_limit;
    212 	uint64_t		tm_bytes_used;
    213 	kmutex_t		tm_acc_lock;
    214 
    215 	/* Pointer to the root inode. */
    216 	tmpfs_node_t *		tm_root;
    217 
    218 	/* Maximum number of possible nodes for this file system. */
    219 	unsigned int		tm_nodes_max;
    220 
    221 	/* Number of nodes currently allocated. */
    222 	unsigned int		tm_nodes_cnt;
    223 
    224 	/* List of inodes and the lock protecting it. */
    225 	kmutex_t		tm_lock;
    226 	struct tmpfs_node_list	tm_nodes;
    227 } tmpfs_mount_t;
    228 
    229 /*
    230  * This structure maps a file identifier to a tmpfs node.  Used by the
    231  * NFS code.
    232  */
    233 typedef struct tmpfs_fid {
    234 	uint16_t		tf_len;
    235 	uint16_t		tf_pad;
    236 	uint32_t		tf_gen;
    237 	ino_t			tf_id;
    238 } tmpfs_fid_t;
    239 
    240 /*
    241  * Prototypes for tmpfs_subr.c.
    242  */
    243 
    244 int		tmpfs_alloc_node(tmpfs_mount_t *, enum vtype, uid_t, gid_t,
    245 		    mode_t, tmpfs_node_t *, char *, dev_t, tmpfs_node_t **);
    246 void		tmpfs_free_node(tmpfs_mount_t *, tmpfs_node_t *);
    247 
    248 int		tmpfs_alloc_file(vnode_t *, vnode_t **, struct vattr *,
    249 		    struct componentname *, char *);
    250 
    251 int		tmpfs_alloc_vp(struct mount *, tmpfs_node_t *, vnode_t **);
    252 void		tmpfs_free_vp(vnode_t *);
    253 
    254 int		tmpfs_alloc_dirent(tmpfs_mount_t *, tmpfs_node_t *,
    255 		    const char *, uint16_t, tmpfs_dirent_t **);
    256 void		tmpfs_free_dirent(tmpfs_mount_t *, tmpfs_dirent_t *, bool);
    257 void		tmpfs_dir_attach(vnode_t *, tmpfs_dirent_t *);
    258 void		tmpfs_dir_detach(vnode_t *, tmpfs_dirent_t *);
    259 
    260 tmpfs_dirent_t *tmpfs_dir_lookup(tmpfs_node_t *, struct componentname *);
    261 int		tmpfs_dir_getdotdent(tmpfs_node_t *, struct uio *);
    262 int		tmpfs_dir_getdotdotdent(tmpfs_node_t *, struct uio *);
    263 tmpfs_dirent_t *tmpfs_dir_lookupbycookie(tmpfs_node_t *, off_t);
    264 int		tmpfs_dir_getdents(tmpfs_node_t *, struct uio *, off_t *);
    265 
    266 int		tmpfs_reg_resize(vnode_t *, off_t);
    267 int		tmpfs_truncate(vnode_t *, off_t);
    268 
    269 int		tmpfs_chflags(vnode_t *, int, kauth_cred_t, lwp_t *);
    270 int		tmpfs_chmod(vnode_t *, mode_t, kauth_cred_t, lwp_t *);
    271 int		tmpfs_chown(vnode_t *, uid_t, gid_t, kauth_cred_t, lwp_t *);
    272 int		tmpfs_chsize(vnode_t *, u_quad_t, kauth_cred_t, lwp_t *);
    273 int		tmpfs_chtimes(vnode_t *, const struct timespec *,
    274 		    const struct timespec *, const struct timespec *, int,
    275 		    kauth_cred_t, lwp_t *);
    276 void		tmpfs_update(vnode_t *, const struct timespec *,
    277 		    const struct timespec *, const struct timespec *, int);
    278 
    279 /*
    280  * Prototypes for tmpfs_mem.c.
    281  */
    282 
    283 void		tmpfs_mntmem_init(tmpfs_mount_t *, uint64_t);
    284 void		tmpfs_mntmem_destroy(tmpfs_mount_t *);
    285 
    286 size_t		tmpfs_mem_info(bool);
    287 uint64_t	tmpfs_bytes_max(tmpfs_mount_t *);
    288 size_t		tmpfs_pages_avail(tmpfs_mount_t *);
    289 bool		tmpfs_mem_incr(tmpfs_mount_t *, size_t);
    290 void		tmpfs_mem_decr(tmpfs_mount_t *, size_t);
    291 
    292 tmpfs_dirent_t *tmpfs_dirent_get(tmpfs_mount_t *);
    293 void		tmpfs_dirent_put(tmpfs_mount_t *, tmpfs_dirent_t *);
    294 
    295 tmpfs_node_t *	tmpfs_node_get(tmpfs_mount_t *);
    296 void		tmpfs_node_put(tmpfs_mount_t *, tmpfs_node_t *);
    297 
    298 char *		tmpfs_strname_alloc(tmpfs_mount_t *, size_t);
    299 void		tmpfs_strname_free(tmpfs_mount_t *, char *, size_t);
    300 bool		tmpfs_strname_neqlen(struct componentname *, struct componentname *);
    301 
    302 /*
    303  * Ensures that the node pointed by 'node' is a directory and that its
    304  * contents are consistent with respect to directories.
    305  */
    306 #define TMPFS_VALIDATE_DIR(node) \
    307     KASSERT((node)->tn_type == VDIR); \
    308     KASSERT((node)->tn_size % sizeof(tmpfs_dirent_t) == 0); \
    309     KASSERT((node)->tn_spec.tn_dir.tn_readdir_lastp == NULL || \
    310         tmpfs_dircookie((node)->tn_spec.tn_dir.tn_readdir_lastp) == \
    311         (node)->tn_spec.tn_dir.tn_readdir_lastn);
    312 
    313 /*
    314  * Memory management stuff.
    315  */
    316 
    317 /* Amount of memory pages to reserve for the system. */
    318 #define	TMPFS_PAGES_RESERVED	(4 * 1024 * 1024 / PAGE_SIZE)
    319 
    320 /*
    321  * Routines to convert VFS structures to tmpfs internal ones.
    322  */
    323 
    324 static inline tmpfs_mount_t *
    325 VFS_TO_TMPFS(struct mount *mp)
    326 {
    327 	tmpfs_mount_t *tmp = mp->mnt_data;
    328 
    329 	KASSERT(tmp != NULL);
    330 	return tmp;
    331 }
    332 
    333 static inline tmpfs_node_t *
    334 VP_TO_TMPFS_DIR(vnode_t *vp)
    335 {
    336 	tmpfs_node_t *node = vp->v_data;
    337 
    338 	KASSERT(node != NULL);
    339 	TMPFS_VALIDATE_DIR(node);
    340 	return node;
    341 }
    342 
    343 #endif /* defined(_KERNEL) */
    344 
    345 static __inline tmpfs_node_t *
    346 VP_TO_TMPFS_NODE(vnode_t *vp)
    347 {
    348 	tmpfs_node_t *node = vp->v_data;
    349 #ifdef KASSERT
    350 	KASSERT(node != NULL);
    351 #endif
    352 	return node;
    353 }
    354 
    355 #endif /* _FS_TMPFS_H_ */
    356