Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs.h revision 1.39.2.1
      1 /*	$NetBSD: tmpfs.h,v 1.39.2.1 2011/06/06 09:09:24 jruoho 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_TMPFS_H_
     34 #define _FS_TMPFS_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 	/*
    114 	 * Each inode has a corresponding vnode.  It is a bi-directional
    115 	 * association.  Whenever vnode is allocated, its v_data field is
    116 	 * set to the inode it reference, and tmpfs_node_t::tn_vnode is
    117 	 * set to point to the said vnode.
    118 	 *
    119 	 * Further attempts to allocate a vnode for this same node will
    120 	 * result in returning a new reference to the value stored in
    121 	 * tn_vnode.  It may be NULL when the node is unused (that is,
    122 	 * no vnode has been allocated or it has been reclaimed).
    123 	 */
    124 	kmutex_t		tn_vlock;
    125 	vnode_t *		tn_vnode;
    126 
    127 	/* Directory entry.  Only a hint, since hard link can have multiple. */
    128 	tmpfs_dirent_t *	tn_dirent_hint;
    129 
    130 	/* The inode type: VBLK, VCHR, VDIR, VFIFO, VLNK, VREG or VSOCK. */
    131 	enum vtype		tn_type;
    132 
    133 	/* Inode identifier and generation number. */
    134 	ino_t			tn_id;
    135 	unsigned long		tn_gen;
    136 
    137 	/* Inode status flags (for operations in delayed manner). */
    138 	int			tn_status;
    139 
    140 	/* The inode size. */
    141 	off_t			tn_size;
    142 
    143 	/* Generic node attributes. */
    144 	uid_t			tn_uid;
    145 	gid_t			tn_gid;
    146 	mode_t			tn_mode;
    147 	int			tn_flags;
    148 	nlink_t			tn_links;
    149 	struct timespec		tn_atime;
    150 	struct timespec		tn_mtime;
    151 	struct timespec		tn_ctime;
    152 	struct timespec		tn_birthtime;
    153 
    154 	/* Head of byte-level lock list (used by tmpfs_advlock). */
    155 	struct lockf *		tn_lockf;
    156 
    157 	union {
    158 		/* Type case: VBLK or VCHR. */
    159 		struct {
    160 			dev_t			tn_rdev;
    161 		} tn_dev;
    162 
    163 		/* Type case: VDIR. */
    164 		struct {
    165 			/* Parent directory (root inode points to itself). */
    166 			struct tmpfs_node *	tn_parent;
    167 
    168 			/* List of directory entries. */
    169 			struct tmpfs_dir	tn_dir;
    170 
    171 			/*
    172 			 * Number and pointer of the last directory entry
    173 			 * returned by the readdir(3) operation.
    174 			 */
    175 			off_t			tn_readdir_lastn;
    176 			struct tmpfs_dirent *	tn_readdir_lastp;
    177 		} tn_dir;
    178 
    179 		/* Type case: VLNK. */
    180 		struct tn_lnk {
    181 			/* The link's target. */
    182 			char *			tn_link;
    183 		} tn_lnk;
    184 
    185 		/* Type case: VREG. */
    186 		struct tn_reg {
    187 			/* Underlying UVM object to store contents. */
    188 			struct uvm_object *	tn_aobj;
    189 			size_t			tn_aobj_pages;
    190 		} tn_reg;
    191 	} tn_spec;
    192 } tmpfs_node_t;
    193 
    194 #if defined(_KERNEL)
    195 
    196 LIST_HEAD(tmpfs_node_list, tmpfs_node);
    197 
    198 /* Status flags. */
    199 #define	TMPFS_NODE_ACCESSED	0x01
    200 #define	TMPFS_NODE_MODIFIED	0x02
    201 #define	TMPFS_NODE_CHANGED	0x04
    202 
    203 #define	TMPFS_NODE_STATUSALL	\
    204     (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED)
    205 
    206 /*
    207  * Bit indicating vnode reclamation.
    208  * We abuse tmpfs_node_t::tn_gen for that.
    209  */
    210 #define	TMPFS_NODE_GEN_MASK	(~0UL >> 1)
    211 #define	TMPFS_RECLAIMING_BIT	(~TMPFS_NODE_GEN_MASK)
    212 
    213 #define	TMPFS_NODE_RECLAIMING(node) \
    214     (((node)->tn_gen & TMPFS_RECLAIMING_BIT) != 0)
    215 
    216 #define	TMPFS_NODE_GEN(node) \
    217     ((node)->tn_gen & TMPFS_NODE_GEN_MASK)
    218 
    219 /* White-out inode indicator. */
    220 #define	TMPFS_NODE_WHITEOUT	((tmpfs_node_t *)-1)
    221 
    222 /*
    223  * Internal representation of a tmpfs mount point.
    224  */
    225 typedef struct tmpfs_mount {
    226 	/* Limit and number of bytes in use by the file system. */
    227 	uint64_t		tm_mem_limit;
    228 	uint64_t		tm_bytes_used;
    229 	kmutex_t		tm_acc_lock;
    230 
    231 	/* Pointer to the root inode. */
    232 	tmpfs_node_t *		tm_root;
    233 
    234 	/* Maximum number of possible nodes for this file system. */
    235 	unsigned int		tm_nodes_max;
    236 
    237 	/* Number of nodes currently allocated. */
    238 	unsigned int		tm_nodes_cnt;
    239 
    240 	/* List of inodes and the lock protecting it. */
    241 	kmutex_t		tm_lock;
    242 	struct tmpfs_node_list	tm_nodes;
    243 } tmpfs_mount_t;
    244 
    245 /*
    246  * This structure maps a file identifier to a tmpfs node.  Used by the
    247  * NFS code.
    248  */
    249 typedef struct tmpfs_fid {
    250 	uint16_t		tf_len;
    251 	uint16_t		tf_pad;
    252 	uint32_t		tf_gen;
    253 	ino_t			tf_id;
    254 } tmpfs_fid_t;
    255 
    256 /*
    257  * Prototypes for tmpfs_subr.c.
    258  */
    259 
    260 int		tmpfs_alloc_node(tmpfs_mount_t *, enum vtype, uid_t, gid_t,
    261 		    mode_t, char *, dev_t, tmpfs_node_t **);
    262 void		tmpfs_free_node(tmpfs_mount_t *, tmpfs_node_t *);
    263 
    264 int		tmpfs_alloc_file(vnode_t *, vnode_t **, struct vattr *,
    265 		    struct componentname *, char *);
    266 
    267 int		tmpfs_vnode_get(struct mount *, tmpfs_node_t *, vnode_t **);
    268 
    269 int		tmpfs_alloc_dirent(tmpfs_mount_t *, const char *, uint16_t,
    270 		    tmpfs_dirent_t **);
    271 void		tmpfs_free_dirent(tmpfs_mount_t *, tmpfs_dirent_t *);
    272 void		tmpfs_dir_attach(vnode_t *, tmpfs_dirent_t *, tmpfs_node_t *);
    273 void		tmpfs_dir_detach(vnode_t *, tmpfs_dirent_t *);
    274 
    275 tmpfs_dirent_t *tmpfs_dir_lookup(tmpfs_node_t *, struct componentname *);
    276 tmpfs_dirent_t *tmpfs_dir_cached(tmpfs_node_t *);
    277 
    278 int		tmpfs_dir_getdotdent(tmpfs_node_t *, struct uio *);
    279 int		tmpfs_dir_getdotdotdent(tmpfs_node_t *, struct uio *);
    280 tmpfs_dirent_t *tmpfs_dir_lookupbycookie(tmpfs_node_t *, off_t);
    281 int		tmpfs_dir_getdents(tmpfs_node_t *, struct uio *, off_t *);
    282 
    283 int		tmpfs_reg_resize(vnode_t *, off_t);
    284 int		tmpfs_truncate(vnode_t *, off_t);
    285 
    286 int		tmpfs_chflags(vnode_t *, int, kauth_cred_t, lwp_t *);
    287 int		tmpfs_chmod(vnode_t *, mode_t, kauth_cred_t, lwp_t *);
    288 int		tmpfs_chown(vnode_t *, uid_t, gid_t, kauth_cred_t, lwp_t *);
    289 int		tmpfs_chsize(vnode_t *, u_quad_t, kauth_cred_t, lwp_t *);
    290 int		tmpfs_chtimes(vnode_t *, const struct timespec *,
    291 		    const struct timespec *, const struct timespec *, int,
    292 		    kauth_cred_t, lwp_t *);
    293 void		tmpfs_update(vnode_t *, const struct timespec *,
    294 		    const struct timespec *, const struct timespec *, int);
    295 
    296 /*
    297  * Prototypes for tmpfs_mem.c.
    298  */
    299 
    300 void		tmpfs_mntmem_init(tmpfs_mount_t *, uint64_t);
    301 void		tmpfs_mntmem_destroy(tmpfs_mount_t *);
    302 
    303 size_t		tmpfs_mem_info(bool);
    304 uint64_t	tmpfs_bytes_max(tmpfs_mount_t *);
    305 size_t		tmpfs_pages_avail(tmpfs_mount_t *);
    306 bool		tmpfs_mem_incr(tmpfs_mount_t *, size_t);
    307 void		tmpfs_mem_decr(tmpfs_mount_t *, size_t);
    308 
    309 tmpfs_dirent_t *tmpfs_dirent_get(tmpfs_mount_t *);
    310 void		tmpfs_dirent_put(tmpfs_mount_t *, tmpfs_dirent_t *);
    311 
    312 tmpfs_node_t *	tmpfs_node_get(tmpfs_mount_t *);
    313 void		tmpfs_node_put(tmpfs_mount_t *, tmpfs_node_t *);
    314 
    315 char *		tmpfs_strname_alloc(tmpfs_mount_t *, size_t);
    316 void		tmpfs_strname_free(tmpfs_mount_t *, char *, size_t);
    317 bool		tmpfs_strname_neqlen(struct componentname *, struct componentname *);
    318 
    319 /*
    320  * Ensures that the node pointed by 'node' is a directory and that its
    321  * contents are consistent with respect to directories.
    322  */
    323 #define TMPFS_VALIDATE_DIR(node) \
    324     KASSERT((node)->tn_type == VDIR); \
    325     KASSERT((node)->tn_size % sizeof(tmpfs_dirent_t) == 0); \
    326     KASSERT((node)->tn_spec.tn_dir.tn_readdir_lastp == NULL || \
    327         tmpfs_dircookie((node)->tn_spec.tn_dir.tn_readdir_lastp) == \
    328         (node)->tn_spec.tn_dir.tn_readdir_lastn);
    329 
    330 /*
    331  * Memory management stuff.
    332  */
    333 
    334 /* Amount of memory pages to reserve for the system. */
    335 #define	TMPFS_PAGES_RESERVED	(4 * 1024 * 1024 / PAGE_SIZE)
    336 
    337 /*
    338  * Routines to convert VFS structures to tmpfs internal ones.
    339  */
    340 
    341 static inline tmpfs_mount_t *
    342 VFS_TO_TMPFS(struct mount *mp)
    343 {
    344 	tmpfs_mount_t *tmp = mp->mnt_data;
    345 
    346 	KASSERT(tmp != NULL);
    347 	return tmp;
    348 }
    349 
    350 static inline tmpfs_node_t *
    351 VP_TO_TMPFS_DIR(vnode_t *vp)
    352 {
    353 	tmpfs_node_t *node = vp->v_data;
    354 
    355 	KASSERT(node != NULL);
    356 	TMPFS_VALIDATE_DIR(node);
    357 	return node;
    358 }
    359 
    360 #endif /* defined(_KERNEL) */
    361 
    362 static __inline tmpfs_node_t *
    363 VP_TO_TMPFS_NODE(vnode_t *vp)
    364 {
    365 	tmpfs_node_t *node = vp->v_data;
    366 #ifdef KASSERT
    367 	KASSERT(node != NULL);
    368 #endif
    369 	return node;
    370 }
    371 
    372 #endif /* _FS_TMPFS_TMPFS_H_ */
    373