Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs.h revision 1.33.4.2
      1 /*	$NetBSD: tmpfs.h,v 1.33.4.2 2008/07/28 14:37:35 simonb 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 #include <sys/dirent.h>
     37 #include <sys/mount.h>
     38 #include <sys/queue.h>
     39 #include <sys/vnode.h>
     40 
     41 #include <fs/tmpfs/tmpfs_pool.h>
     42 
     43 /* --------------------------------------------------------------------- */
     44 
     45 /*
     46  * Internal representation of a tmpfs directory entry.
     47  */
     48 struct tmpfs_dirent {
     49 	TAILQ_ENTRY(tmpfs_dirent)	td_entries;
     50 
     51 	/* Length of the name stored in this directory entry.  This avoids
     52 	 * the need to recalculate it every time the name is used. */
     53 	uint16_t			td_namelen;
     54 
     55 	/* The name of the entry, allocated from a string pool.  This
     56 	* string is not required to be zero-terminated; therefore, the
     57 	* td_namelen field must always be used when accessing its value. */
     58 	char *				td_name;
     59 
     60 	/* Pointer to the node this entry refers to. */
     61 	struct tmpfs_node *		td_node;
     62 };
     63 
     64 /* A directory in tmpfs holds a sorted list of directory entries, which in
     65  * turn point to other files (which can be directories themselves).
     66  *
     67  * In tmpfs, this list is managed by a tail queue, whose head is defined by
     68  * the struct tmpfs_dir type.
     69  *
     70  * It is imporant to notice that directories do not have entries for . and
     71  * .. as other file systems do.  These can be generated when requested
     72  * based on information available by other means, such as the pointer to
     73  * the node itself in the former case or the pointer to the parent directory
     74  * in the latter case.  This is done to simplify tmpfs's code and, more
     75  * importantly, to remove redundancy. */
     76 TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
     77 
     78 /* Each entry in a directory has a cookie that identifies it.  Cookies
     79  * supersede offsets within directories because, given how tmpfs stores
     80  * directories in memory, there is no such thing as an offset.  (Emulating
     81  * a real offset could be very difficult.)
     82  *
     83  * The '.', '..' and the end of directory markers have fixed cookies which
     84  * cannot collide with the cookies generated by other entries.  The cookies
     85  * fot the other entries are generated based on the memory address on which
     86  * stores their information is stored.
     87  *
     88  * Ideally, using the entry's memory pointer as the cookie would be enough
     89  * to represent it and it wouldn't cause collisions in any system.
     90  * Unfortunately, this results in "offsets" with very large values which
     91  * later raise problems in the Linux compatibility layer (and maybe in other
     92  * places) as described in PR kern/32034.  Hence we need to workaround this
     93  * with a rather ugly hack.
     94  *
     95  * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t
     96  * set to 'long', which is a 32-bit *signed* long integer.  Regardless of
     97  * the macro value, GLIBC (2.3 at least) always uses the getdents64
     98  * system call (when calling readdir) which internally returns off64_t
     99  * offsets.  In order to make 32-bit binaries work, *GLIBC* converts the
    100  * 64-bit values returned by the kernel to 32-bit ones and aborts with
    101  * EOVERFLOW if the conversion results in values that won't fit in 32-bit
    102  * integers (which it assumes is because the directory is extremely large).
    103  * This wouldn't cause problems if we were dealing with unsigned integers,
    104  * but as we have signed integers, this check fails due to sign expansion.
    105  *
    106  * For example, consider that the kernel returns the 0xc1234567 cookie to
    107  * userspace in a off64_t integer.  Later on, GLIBC casts this value to
    108  * off_t (remember, signed) with code similar to:
    109  *     system call returns the offset in kernel_value;
    110  *     off_t casted_value = kernel_value;
    111  *     if (sizeof(off_t) != sizeof(off64_t) &&
    112  *         kernel_value != casted_value)
    113  *             error!
    114  * In this case, casted_value still has 0xc1234567, but when it is compared
    115  * for equality against kernel_value, it is promoted to a 64-bit integer and
    116  * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567.
    117  * Then, GLIBC assumes this is because the directory is very large.
    118  *
    119  * Given that all the above happens in user-space, we have no control over
    120  * it; therefore we must workaround the issue here.  We do this by
    121  * truncating the pointer value to a 32-bit integer and hope that there
    122  * won't be collisions.  In fact, this will not cause any problems in
    123  * 32-bit platforms but some might arise in 64-bit machines (I'm not sure
    124  * if they can happen at all in practice).
    125  *
    126  * XXX A nicer solution shall be attempted. */
    127 #define	TMPFS_DIRCOOKIE_DOT	0
    128 #define	TMPFS_DIRCOOKIE_DOTDOT	1
    129 #define	TMPFS_DIRCOOKIE_EOF	2
    130 static __inline
    131 off_t
    132 tmpfs_dircookie(struct tmpfs_dirent *de)
    133 {
    134 	off_t cookie;
    135 
    136 	cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF;
    137 	KASSERT(cookie != TMPFS_DIRCOOKIE_DOT);
    138 	KASSERT(cookie != TMPFS_DIRCOOKIE_DOTDOT);
    139 	KASSERT(cookie != TMPFS_DIRCOOKIE_EOF);
    140 
    141 	return cookie;
    142 }
    143 
    144 /* --------------------------------------------------------------------- */
    145 
    146 /*
    147  * Internal representation of a tmpfs file system node.
    148  *
    149  * This structure is splitted in two parts: one holds attributes common
    150  * to all file types and the other holds data that is only applicable to
    151  * a particular type.  The code must be careful to only access those
    152  * attributes that are actually allowed by the node's type.
    153  */
    154 struct tmpfs_node {
    155 	/* Doubly-linked list entry which links all existing nodes for a
    156 	 * single file system.  This is provided to ease the removal of
    157 	 * all nodes during the unmount operation. */
    158 	LIST_ENTRY(tmpfs_node)	tn_entries;
    159 
    160 	/* The node's type.  Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
    161 	 * 'VLNK', 'VREG' and 'VSOCK' is allowed.  The usage of vnode
    162 	 * types instead of a custom enumeration is to make things simpler
    163 	 * and faster, as we do not need to convert between two types. */
    164 	enum vtype		tn_type;
    165 
    166 	/* Node identifier. */
    167 	ino_t			tn_id;
    168 
    169 	/* Node's internal status.  This is used by several file system
    170 	 * operations to do modifications to the node in a delayed
    171 	 * fashion. */
    172 	int			tn_status;
    173 #define	TMPFS_NODE_ACCESSED	(1 << 1)
    174 #define	TMPFS_NODE_MODIFIED	(1 << 2)
    175 #define	TMPFS_NODE_CHANGED	(1 << 3)
    176 
    177 	/* The node size.  It does not necessarily match the real amount
    178 	 * of memory consumed by it. */
    179 	off_t			tn_size;
    180 
    181 	/* Generic node attributes. */
    182 	uid_t			tn_uid;
    183 	gid_t			tn_gid;
    184 	mode_t			tn_mode;
    185 	int			tn_flags;
    186 	nlink_t			tn_links;
    187 	struct timespec		tn_atime;
    188 	struct timespec		tn_mtime;
    189 	struct timespec		tn_ctime;
    190 	struct timespec		tn_birthtime;
    191 	unsigned long		tn_gen;
    192 
    193 	/* Head of byte-level lock list (used by tmpfs_advlock). */
    194 	struct lockf *		tn_lockf;
    195 
    196 	/* As there is a single vnode for each active file within the
    197 	 * system, care has to be taken to avoid allocating more than one
    198 	 * vnode per file.  In order to do this, a bidirectional association
    199 	 * is kept between vnodes and nodes.
    200 	 *
    201 	 * Whenever a vnode is allocated, its v_data field is updated to
    202 	 * point to the node it references.  At the same time, the node's
    203 	 * tn_vnode field is modified to point to the new vnode representing
    204 	 * it.  Further attempts to allocate a vnode for this same node will
    205 	 * result in returning a new reference to the value stored in
    206 	 * tn_vnode.
    207 	 *
    208 	 * May be NULL when the node is unused (that is, no vnode has been
    209 	 * allocated for it or it has been reclaimed). */
    210 	kmutex_t		tn_vlock;
    211 	struct vnode *		tn_vnode;
    212 
    213 	union {
    214 		/* Valid when tn_type == VBLK || tn_type == VCHR. */
    215 		struct {
    216 			dev_t			tn_rdev;
    217 		} tn_dev;
    218 
    219 		/* Valid when tn_type == VDIR. */
    220 		struct {
    221 			/* Pointer to the parent directory.  The root
    222 			 * directory has a pointer to itself in this field;
    223 			 * this property identifies the root node. */
    224 			struct tmpfs_node *	tn_parent;
    225 
    226 			/* Head of a tail-queue that links the contents of
    227 			 * the directory together.  See above for a
    228 			 * description of its contents. */
    229 			struct tmpfs_dir	tn_dir;
    230 
    231 			/* Number and pointer of the first directory entry
    232 			 * returned by the readdir operation if it were
    233 			 * called again to continue reading data from the
    234 			 * same directory as before.  This is used to speed
    235 			 * up reads of long directories, assuming that no
    236 			 * more than one read is in progress at a given time.
    237 			 * Otherwise, these values are discarded and a linear
    238 			 * scan is performed from the beginning up to the
    239 			 * point where readdir starts returning values. */
    240 			off_t			tn_readdir_lastn;
    241 			struct tmpfs_dirent *	tn_readdir_lastp;
    242 		} tn_dir;
    243 
    244 		/* Valid when tn_type == VLNK. */
    245 		struct tn_lnk {
    246 			/* The link's target, allocated from a string pool. */
    247 			char *			tn_link;
    248 		} tn_lnk;
    249 
    250 		/* Valid when tn_type == VREG. */
    251 		struct tn_reg {
    252 			/* The contents of regular files stored in a tmpfs
    253 			 * file system are represented by a single anonymous
    254 			 * memory object (aobj, for short).  The aobj provides
    255 			 * direct access to any position within the file,
    256 			 * because its contents are always mapped in a
    257 			 * contiguous region of virtual memory.  It is a task
    258 			 * of the memory management subsystem (see uvm(9)) to
    259 			 * issue the required page ins or page outs whenever
    260 			 * a position within the file is accessed. */
    261 			struct uvm_object *	tn_aobj;
    262 			size_t			tn_aobj_pages;
    263 		} tn_reg;
    264 	} tn_spec;
    265 };
    266 
    267 LIST_HEAD(tmpfs_node_list, tmpfs_node);
    268 
    269 /* --------------------------------------------------------------------- */
    270 
    271 /*
    272  * Internal representation of a tmpfs mount point.
    273  */
    274 struct tmpfs_mount {
    275 	/* Maximum number of memory pages available for use by the file
    276 	 * system, set during mount time.  This variable must never be
    277 	 * used directly as it may be bigger than the current amount of
    278 	 * free memory; in the extreme case, it will hold the SIZE_MAX
    279 	 * value.  Instead, use the TMPFS_PAGES_MAX macro. */
    280 	unsigned int		tm_pages_max;
    281 
    282 	/* Number of pages in use by the file system.  Cannot be bigger
    283 	 * than the value returned by TMPFS_PAGES_MAX in any case. */
    284 	unsigned int		tm_pages_used;
    285 
    286 	/* Pointer to the node representing the root directory of this
    287 	 * file system. */
    288 	struct tmpfs_node *	tm_root;
    289 
    290 	/* Maximum number of possible nodes for this file system; set
    291 	 * during mount time.  We need a hard limit on the maximum number
    292 	 * of nodes to avoid allocating too much of them; their objects
    293 	 * cannot be released until the file system is unmounted.
    294 	 * Otherwise, we could easily run out of memory by creating lots
    295 	 * of empty files and then simply removing them. */
    296 	unsigned int		tm_nodes_max;
    297 
    298 	/* Number of nodes currently allocated.  This number only grows.
    299 	 * When it reaches tm_nodes_max, no more new nodes can be allocated.
    300 	 * Of course, the old, unused ones can be reused. */
    301 	unsigned int		tm_nodes_cnt;
    302 
    303 	/* Node list. */
    304 	kmutex_t		tm_lock;
    305 	struct tmpfs_node_list	tm_nodes;
    306 
    307 	/* Pools used to store file system meta data.  These are not shared
    308 	 * across several instances of tmpfs for the reasons described in
    309 	 * tmpfs_pool.c. */
    310 	struct tmpfs_pool	tm_dirent_pool;
    311 	struct tmpfs_pool	tm_node_pool;
    312 	struct tmpfs_str_pool	tm_str_pool;
    313 };
    314 
    315 /* --------------------------------------------------------------------- */
    316 
    317 /*
    318  * This structure maps a file identifier to a tmpfs node.  Used by the
    319  * NFS code.
    320  */
    321 struct tmpfs_fid {
    322 	uint16_t		tf_len;
    323 	uint16_t		tf_pad;
    324 	uint32_t		tf_gen;
    325 	ino_t			tf_id;
    326 };
    327 
    328 /* --------------------------------------------------------------------- */
    329 
    330 /*
    331  * Prototypes for tmpfs_subr.c.
    332  */
    333 
    334 int	tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
    335 	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
    336 	    char *, dev_t, struct tmpfs_node **);
    337 void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
    338 int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
    339 	    const char *, uint16_t, struct tmpfs_dirent **);
    340 void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
    341 	    bool);
    342 int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **);
    343 void	tmpfs_free_vp(struct vnode *);
    344 int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
    345 	    struct componentname *, char *);
    346 void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
    347 void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
    348 struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
    349 			    struct componentname *cnp);
    350 int	tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
    351 int	tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
    352 struct tmpfs_dirent *	tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
    353 int	tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
    354 int	tmpfs_reg_resize(struct vnode *, off_t);
    355 size_t	tmpfs_mem_info(bool);
    356 int	tmpfs_chflags(struct vnode *, int, kauth_cred_t, struct lwp *);
    357 int	tmpfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct lwp *);
    358 int	tmpfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, struct lwp *);
    359 int	tmpfs_chsize(struct vnode *, u_quad_t, kauth_cred_t, struct lwp *);
    360 int	tmpfs_chtimes(struct vnode *, const struct timespec *,
    361     const struct timespec *, const struct timespec *, int, kauth_cred_t,
    362     struct lwp *);
    363 void	tmpfs_itimes(struct vnode *, const struct timespec *,
    364 	    const struct timespec *, const struct timespec *);
    365 
    366 void	tmpfs_update(struct vnode *, const struct timespec *,
    367 	    const struct timespec *, const struct timespec *, int);
    368 int	tmpfs_truncate(struct vnode *, off_t);
    369 
    370 /* --------------------------------------------------------------------- */
    371 
    372 /*
    373  * Convenience macros to simplify some logical expressions.
    374  */
    375 #define IMPLIES(a, b) (!(a) || (b))
    376 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
    377 
    378 /* --------------------------------------------------------------------- */
    379 
    380 /*
    381  * Checks that the directory entry pointed by 'de' matches the name 'name'
    382  * with a length of 'len'.
    383  */
    384 #define TMPFS_DIRENT_MATCHES(de, name, len) \
    385     (de->td_namelen == (uint16_t)len && \
    386     memcmp((de)->td_name, (name), (de)->td_namelen) == 0)
    387 
    388 /* --------------------------------------------------------------------- */
    389 
    390 /*
    391  * Ensures that the node pointed by 'node' is a directory and that its
    392  * contents are consistent with respect to directories.
    393  */
    394 #define TMPFS_VALIDATE_DIR(node) \
    395     KASSERT((node)->tn_type == VDIR); \
    396     KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
    397     KASSERT((node)->tn_spec.tn_dir.tn_readdir_lastp == NULL || \
    398         tmpfs_dircookie((node)->tn_spec.tn_dir.tn_readdir_lastp) == \
    399         (node)->tn_spec.tn_dir.tn_readdir_lastn);
    400 
    401 /* --------------------------------------------------------------------- */
    402 
    403 /*
    404  * Memory management stuff.
    405  */
    406 
    407 /* Amount of memory pages to reserve for the system (e.g., to not use by
    408  * tmpfs).
    409  * XXX: Should this be tunable through sysctl, for instance? */
    410 #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
    411 
    412 /* Returns the maximum size allowed for a tmpfs file system.  This macro
    413  * must be used instead of directly retrieving the value from tm_pages_max.
    414  * The reason is that the size of a tmpfs file system is dynamic: it lets
    415  * the user store files as long as there is enough free memory (including
    416  * physical memory and swap space).  Therefore, the amount of memory to be
    417  * used is either the limit imposed by the user during mount time or the
    418  * amount of available memory, whichever is lower.  To avoid consuming all
    419  * the memory for a given mount point, the system will always reserve a
    420  * minimum of TMPFS_PAGES_RESERVED pages, which is also taken into account
    421  * by this macro (see above). */
    422 static __inline size_t
    423 TMPFS_PAGES_MAX(struct tmpfs_mount *tmp)
    424 {
    425 	size_t freepages;
    426 
    427 	freepages = tmpfs_mem_info(false);
    428 	if (freepages < TMPFS_PAGES_RESERVED)
    429 		freepages = 0;
    430 	else
    431 		freepages -= TMPFS_PAGES_RESERVED;
    432 
    433 	return MIN(tmp->tm_pages_max, freepages + tmp->tm_pages_used);
    434 }
    435 
    436 /* Returns the available space for the given file system. */
    437 #define TMPFS_PAGES_AVAIL(tmp)		\
    438     ((ssize_t)(TMPFS_PAGES_MAX(tmp) - (tmp)->tm_pages_used))
    439 
    440 /* --------------------------------------------------------------------- */
    441 
    442 /*
    443  * Macros/functions to convert from generic data structures to tmpfs
    444  * specific ones.
    445  */
    446 
    447 static __inline
    448 struct tmpfs_mount *
    449 VFS_TO_TMPFS(struct mount *mp)
    450 {
    451 	struct tmpfs_mount *tmp;
    452 
    453 #ifdef KASSERT
    454 	KASSERT((mp) != NULL && (mp)->mnt_data != NULL);
    455 #endif
    456 	tmp = (struct tmpfs_mount *)(mp)->mnt_data;
    457 	return tmp;
    458 }
    459 
    460 static __inline
    461 struct tmpfs_node *
    462 VP_TO_TMPFS_NODE(struct vnode *vp)
    463 {
    464 	struct tmpfs_node *node;
    465 
    466 #ifdef KASSERT
    467 	KASSERT((vp) != NULL && (vp)->v_data != NULL);
    468 #endif
    469 	node = (struct tmpfs_node *)vp->v_data;
    470 	return node;
    471 }
    472 
    473 static __inline
    474 struct tmpfs_node *
    475 VP_TO_TMPFS_DIR(struct vnode *vp)
    476 {
    477 	struct tmpfs_node *node;
    478 
    479 	node = VP_TO_TMPFS_NODE(vp);
    480 #ifdef KASSERT
    481 	TMPFS_VALIDATE_DIR(node);
    482 #endif
    483 	return node;
    484 }
    485 #endif /* _FS_TMPFS_TMPFS_H_ */
    486