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