Home | History | Annotate | Line # | Download | only in tmpfs
tmpfs.h revision 1.33.4.3
      1  1.33.4.3    simonb /*	$NetBSD: tmpfs.h,v 1.33.4.3 2008/07/29 07:00:30 simonb Exp $	*/
      2       1.1      jmmv 
      3       1.1      jmmv /*
      4      1.30        ad  * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
      5       1.1      jmmv  * All rights reserved.
      6       1.1      jmmv  *
      7       1.1      jmmv  * This code is derived from software contributed to The NetBSD Foundation
      8       1.6      jmmv  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
      9       1.6      jmmv  * 2005 program.
     10       1.1      jmmv  *
     11       1.1      jmmv  * Redistribution and use in source and binary forms, with or without
     12       1.1      jmmv  * modification, are permitted provided that the following conditions
     13       1.1      jmmv  * are met:
     14       1.1      jmmv  * 1. Redistributions of source code must retain the above copyright
     15       1.1      jmmv  *    notice, this list of conditions and the following disclaimer.
     16       1.1      jmmv  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1      jmmv  *    notice, this list of conditions and the following disclaimer in the
     18       1.1      jmmv  *    documentation and/or other materials provided with the distribution.
     19       1.1      jmmv  *
     20       1.1      jmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1      jmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1      jmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1      jmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1      jmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1      jmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1      jmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1      jmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1      jmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1      jmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1      jmmv  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1      jmmv  */
     32       1.1      jmmv 
     33      1.10  christos #ifndef _FS_TMPFS_TMPFS_H_
     34      1.10  christos #define _FS_TMPFS_TMPFS_H_
     35       1.1      jmmv 
     36       1.1      jmmv #include <sys/dirent.h>
     37       1.1      jmmv #include <sys/mount.h>
     38       1.1      jmmv #include <sys/queue.h>
     39       1.1      jmmv #include <sys/vnode.h>
     40       1.1      jmmv 
     41       1.1      jmmv #include <fs/tmpfs/tmpfs_pool.h>
     42  1.33.4.3    simonb #include <fs/tmpfs/tmpfs_args.h>
     43       1.1      jmmv 
     44       1.1      jmmv /* --------------------------------------------------------------------- */
     45       1.1      jmmv 
     46      1.22      jmmv /* Each entry in a directory has a cookie that identifies it.  Cookies
     47      1.22      jmmv  * supersede offsets within directories because, given how tmpfs stores
     48      1.22      jmmv  * directories in memory, there is no such thing as an offset.  (Emulating
     49      1.22      jmmv  * a real offset could be very difficult.)
     50      1.31      jmmv  *
     51      1.22      jmmv  * The '.', '..' and the end of directory markers have fixed cookies which
     52      1.22      jmmv  * cannot collide with the cookies generated by other entries.  The cookies
     53      1.22      jmmv  * fot the other entries are generated based on the memory address on which
     54      1.22      jmmv  * stores their information is stored.
     55      1.22      jmmv  *
     56      1.22      jmmv  * Ideally, using the entry's memory pointer as the cookie would be enough
     57      1.22      jmmv  * to represent it and it wouldn't cause collisions in any system.
     58      1.22      jmmv  * Unfortunately, this results in "offsets" with very large values which
     59      1.22      jmmv  * later raise problems in the Linux compatibility layer (and maybe in other
     60      1.22      jmmv  * places) as described in PR kern/32034.  Hence we need to workaround this
     61      1.22      jmmv  * with a rather ugly hack.
     62      1.22      jmmv  *
     63      1.22      jmmv  * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t
     64      1.22      jmmv  * set to 'long', which is a 32-bit *signed* long integer.  Regardless of
     65      1.22      jmmv  * the macro value, GLIBC (2.3 at least) always uses the getdents64
     66      1.22      jmmv  * system call (when calling readdir) which internally returns off64_t
     67      1.22      jmmv  * offsets.  In order to make 32-bit binaries work, *GLIBC* converts the
     68      1.22      jmmv  * 64-bit values returned by the kernel to 32-bit ones and aborts with
     69      1.22      jmmv  * EOVERFLOW if the conversion results in values that won't fit in 32-bit
     70      1.22      jmmv  * integers (which it assumes is because the directory is extremely large).
     71      1.22      jmmv  * This wouldn't cause problems if we were dealing with unsigned integers,
     72      1.22      jmmv  * but as we have signed integers, this check fails due to sign expansion.
     73      1.22      jmmv  *
     74      1.22      jmmv  * For example, consider that the kernel returns the 0xc1234567 cookie to
     75      1.22      jmmv  * userspace in a off64_t integer.  Later on, GLIBC casts this value to
     76      1.22      jmmv  * off_t (remember, signed) with code similar to:
     77      1.22      jmmv  *     system call returns the offset in kernel_value;
     78      1.22      jmmv  *     off_t casted_value = kernel_value;
     79      1.22      jmmv  *     if (sizeof(off_t) != sizeof(off64_t) &&
     80      1.22      jmmv  *         kernel_value != casted_value)
     81      1.22      jmmv  *             error!
     82      1.22      jmmv  * In this case, casted_value still has 0xc1234567, but when it is compared
     83      1.22      jmmv  * for equality against kernel_value, it is promoted to a 64-bit integer and
     84      1.22      jmmv  * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567.
     85      1.22      jmmv  * Then, GLIBC assumes this is because the directory is very large.
     86      1.22      jmmv  *
     87      1.22      jmmv  * Given that all the above happens in user-space, we have no control over
     88      1.22      jmmv  * it; therefore we must workaround the issue here.  We do this by
     89      1.22      jmmv  * truncating the pointer value to a 32-bit integer and hope that there
     90      1.22      jmmv  * won't be collisions.  In fact, this will not cause any problems in
     91      1.22      jmmv  * 32-bit platforms but some might arise in 64-bit machines (I'm not sure
     92      1.22      jmmv  * if they can happen at all in practice).
     93      1.22      jmmv  *
     94      1.22      jmmv  * XXX A nicer solution shall be attempted. */
     95       1.4      yamt #define	TMPFS_DIRCOOKIE_DOT	0
     96       1.4      yamt #define	TMPFS_DIRCOOKIE_DOTDOT	1
     97       1.4      yamt #define	TMPFS_DIRCOOKIE_EOF	2
     98      1.22      jmmv static __inline
     99      1.22      jmmv off_t
    100      1.22      jmmv tmpfs_dircookie(struct tmpfs_dirent *de)
    101      1.22      jmmv {
    102      1.22      jmmv 	off_t cookie;
    103      1.22      jmmv 
    104      1.22      jmmv 	cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF;
    105      1.22      jmmv 	KASSERT(cookie != TMPFS_DIRCOOKIE_DOT);
    106      1.22      jmmv 	KASSERT(cookie != TMPFS_DIRCOOKIE_DOTDOT);
    107      1.22      jmmv 	KASSERT(cookie != TMPFS_DIRCOOKIE_EOF);
    108      1.22      jmmv 
    109      1.22      jmmv 	return cookie;
    110      1.22      jmmv }
    111       1.4      yamt 
    112       1.1      jmmv /* --------------------------------------------------------------------- */
    113       1.1      jmmv 
    114       1.1      jmmv LIST_HEAD(tmpfs_node_list, tmpfs_node);
    115       1.1      jmmv 
    116       1.1      jmmv /* --------------------------------------------------------------------- */
    117       1.1      jmmv 
    118       1.1      jmmv /*
    119       1.6      jmmv  * Internal representation of a tmpfs mount point.
    120       1.1      jmmv  */
    121       1.1      jmmv struct tmpfs_mount {
    122       1.6      jmmv 	/* Maximum number of memory pages available for use by the file
    123       1.6      jmmv 	 * system, set during mount time.  This variable must never be
    124      1.24      jmmv 	 * used directly as it may be bigger than the current amount of
    125       1.6      jmmv 	 * free memory; in the extreme case, it will hold the SIZE_MAX
    126       1.6      jmmv 	 * value.  Instead, use the TMPFS_PAGES_MAX macro. */
    127      1.32      jmmv 	unsigned int		tm_pages_max;
    128       1.6      jmmv 
    129       1.6      jmmv 	/* Number of pages in use by the file system.  Cannot be bigger
    130       1.6      jmmv 	 * than the value returned by TMPFS_PAGES_MAX in any case. */
    131      1.32      jmmv 	unsigned int		tm_pages_used;
    132       1.1      jmmv 
    133       1.6      jmmv 	/* Pointer to the node representing the root directory of this
    134       1.6      jmmv 	 * file system. */
    135       1.1      jmmv 	struct tmpfs_node *	tm_root;
    136       1.1      jmmv 
    137       1.6      jmmv 	/* Maximum number of possible nodes for this file system; set
    138       1.6      jmmv 	 * during mount time.  We need a hard limit on the maximum number
    139       1.6      jmmv 	 * of nodes to avoid allocating too much of them; their objects
    140       1.6      jmmv 	 * cannot be released until the file system is unmounted.
    141       1.6      jmmv 	 * Otherwise, we could easily run out of memory by creating lots
    142       1.6      jmmv 	 * of empty files and then simply removing them. */
    143      1.32      jmmv 	unsigned int		tm_nodes_max;
    144       1.6      jmmv 
    145       1.6      jmmv 	/* Number of nodes currently allocated.  This number only grows.
    146       1.6      jmmv 	 * When it reaches tm_nodes_max, no more new nodes can be allocated.
    147       1.6      jmmv 	 * Of course, the old, unused ones can be reused. */
    148      1.32      jmmv 	unsigned int		tm_nodes_cnt;
    149       1.6      jmmv 
    150      1.30        ad 	/* Node list. */
    151      1.30        ad 	kmutex_t		tm_lock;
    152      1.30        ad 	struct tmpfs_node_list	tm_nodes;
    153       1.1      jmmv 
    154       1.6      jmmv 	/* Pools used to store file system meta data.  These are not shared
    155       1.6      jmmv 	 * across several instances of tmpfs for the reasons described in
    156       1.6      jmmv 	 * tmpfs_pool.c. */
    157       1.1      jmmv 	struct tmpfs_pool	tm_dirent_pool;
    158       1.1      jmmv 	struct tmpfs_pool	tm_node_pool;
    159       1.1      jmmv 	struct tmpfs_str_pool	tm_str_pool;
    160       1.1      jmmv };
    161       1.1      jmmv 
    162       1.1      jmmv /* --------------------------------------------------------------------- */
    163       1.1      jmmv 
    164       1.1      jmmv /*
    165       1.1      jmmv  * This structure maps a file identifier to a tmpfs node.  Used by the
    166       1.1      jmmv  * NFS code.
    167       1.1      jmmv  */
    168       1.1      jmmv struct tmpfs_fid {
    169       1.1      jmmv 	uint16_t		tf_len;
    170       1.1      jmmv 	uint16_t		tf_pad;
    171      1.18       riz 	uint32_t		tf_gen;
    172       1.1      jmmv 	ino_t			tf_id;
    173       1.1      jmmv };
    174       1.1      jmmv 
    175       1.1      jmmv /* --------------------------------------------------------------------- */
    176       1.1      jmmv 
    177       1.1      jmmv /*
    178       1.1      jmmv  * Prototypes for tmpfs_subr.c.
    179       1.1      jmmv  */
    180       1.1      jmmv 
    181       1.1      jmmv int	tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
    182       1.1      jmmv 	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
    183      1.29     pooka 	    char *, dev_t, struct tmpfs_node **);
    184       1.1      jmmv void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
    185       1.1      jmmv int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
    186       1.1      jmmv 	    const char *, uint16_t, struct tmpfs_dirent **);
    187       1.1      jmmv void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
    188      1.25   thorpej 	    bool);
    189       1.1      jmmv int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **);
    190       1.1      jmmv void	tmpfs_free_vp(struct vnode *);
    191       1.1      jmmv int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
    192       1.1      jmmv 	    struct componentname *, char *);
    193       1.1      jmmv void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
    194       1.1      jmmv void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
    195       1.1      jmmv struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
    196       1.1      jmmv 			    struct componentname *cnp);
    197       1.1      jmmv int	tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
    198       1.1      jmmv int	tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
    199       1.4      yamt struct tmpfs_dirent *	tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
    200       1.4      yamt int	tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
    201       1.1      jmmv int	tmpfs_reg_resize(struct vnode *, off_t);
    202      1.25   thorpej size_t	tmpfs_mem_info(bool);
    203      1.21        ad int	tmpfs_chflags(struct vnode *, int, kauth_cred_t, struct lwp *);
    204      1.21        ad int	tmpfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct lwp *);
    205      1.21        ad int	tmpfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, struct lwp *);
    206      1.21        ad int	tmpfs_chsize(struct vnode *, u_quad_t, kauth_cred_t, struct lwp *);
    207  1.33.4.1    simonb int	tmpfs_chtimes(struct vnode *, const struct timespec *,
    208  1.33.4.1    simonb     const struct timespec *, const struct timespec *, int, kauth_cred_t,
    209  1.33.4.1    simonb     struct lwp *);
    210       1.7      yamt void	tmpfs_itimes(struct vnode *, const struct timespec *,
    211  1.33.4.1    simonb 	    const struct timespec *, const struct timespec *);
    212       1.1      jmmv 
    213       1.9      yamt void	tmpfs_update(struct vnode *, const struct timespec *,
    214  1.33.4.1    simonb 	    const struct timespec *, const struct timespec *, int);
    215       1.9      yamt int	tmpfs_truncate(struct vnode *, off_t);
    216       1.9      yamt 
    217       1.1      jmmv /* --------------------------------------------------------------------- */
    218       1.1      jmmv 
    219       1.1      jmmv /*
    220       1.1      jmmv  * Convenience macros to simplify some logical expressions.
    221       1.1      jmmv  */
    222       1.1      jmmv #define IMPLIES(a, b) (!(a) || (b))
    223       1.1      jmmv #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
    224       1.1      jmmv 
    225       1.1      jmmv /* --------------------------------------------------------------------- */
    226       1.1      jmmv 
    227       1.1      jmmv /*
    228       1.1      jmmv  * Checks that the directory entry pointed by 'de' matches the name 'name'
    229       1.1      jmmv  * with a length of 'len'.
    230       1.1      jmmv  */
    231       1.1      jmmv #define TMPFS_DIRENT_MATCHES(de, name, len) \
    232       1.1      jmmv     (de->td_namelen == (uint16_t)len && \
    233       1.1      jmmv     memcmp((de)->td_name, (name), (de)->td_namelen) == 0)
    234       1.1      jmmv 
    235       1.1      jmmv /* --------------------------------------------------------------------- */
    236       1.1      jmmv 
    237       1.1      jmmv /*
    238       1.1      jmmv  * Ensures that the node pointed by 'node' is a directory and that its
    239       1.1      jmmv  * contents are consistent with respect to directories.
    240       1.1      jmmv  */
    241       1.1      jmmv #define TMPFS_VALIDATE_DIR(node) \
    242       1.1      jmmv     KASSERT((node)->tn_type == VDIR); \
    243       1.4      yamt     KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
    244      1.15      jmmv     KASSERT((node)->tn_spec.tn_dir.tn_readdir_lastp == NULL || \
    245      1.22      jmmv         tmpfs_dircookie((node)->tn_spec.tn_dir.tn_readdir_lastp) == \
    246      1.15      jmmv         (node)->tn_spec.tn_dir.tn_readdir_lastn);
    247       1.1      jmmv 
    248       1.1      jmmv /* --------------------------------------------------------------------- */
    249       1.1      jmmv 
    250       1.1      jmmv /*
    251       1.1      jmmv  * Memory management stuff.
    252       1.1      jmmv  */
    253       1.1      jmmv 
    254       1.1      jmmv /* Amount of memory pages to reserve for the system (e.g., to not use by
    255       1.1      jmmv  * tmpfs).
    256       1.1      jmmv  * XXX: Should this be tunable through sysctl, for instance? */
    257       1.1      jmmv #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
    258       1.1      jmmv 
    259       1.6      jmmv /* Returns the maximum size allowed for a tmpfs file system.  This macro
    260       1.6      jmmv  * must be used instead of directly retrieving the value from tm_pages_max.
    261       1.6      jmmv  * The reason is that the size of a tmpfs file system is dynamic: it lets
    262       1.6      jmmv  * the user store files as long as there is enough free memory (including
    263       1.6      jmmv  * physical memory and swap space).  Therefore, the amount of memory to be
    264       1.6      jmmv  * used is either the limit imposed by the user during mount time or the
    265       1.6      jmmv  * amount of available memory, whichever is lower.  To avoid consuming all
    266       1.6      jmmv  * the memory for a given mount point, the system will always reserve a
    267       1.6      jmmv  * minimum of TMPFS_PAGES_RESERVED pages, which is also taken into account
    268       1.6      jmmv  * by this macro (see above). */
    269      1.16     perry static __inline size_t
    270       1.1      jmmv TMPFS_PAGES_MAX(struct tmpfs_mount *tmp)
    271       1.1      jmmv {
    272       1.1      jmmv 	size_t freepages;
    273       1.1      jmmv 
    274      1.26   thorpej 	freepages = tmpfs_mem_info(false);
    275       1.1      jmmv 	if (freepages < TMPFS_PAGES_RESERVED)
    276       1.1      jmmv 		freepages = 0;
    277       1.1      jmmv 	else
    278       1.1      jmmv 		freepages -= TMPFS_PAGES_RESERVED;
    279       1.1      jmmv 
    280       1.1      jmmv 	return MIN(tmp->tm_pages_max, freepages + tmp->tm_pages_used);
    281       1.1      jmmv }
    282       1.1      jmmv 
    283       1.6      jmmv /* Returns the available space for the given file system. */
    284      1.30        ad #define TMPFS_PAGES_AVAIL(tmp)		\
    285      1.30        ad     ((ssize_t)(TMPFS_PAGES_MAX(tmp) - (tmp)->tm_pages_used))
    286       1.1      jmmv 
    287       1.1      jmmv /* --------------------------------------------------------------------- */
    288       1.1      jmmv 
    289       1.1      jmmv /*
    290       1.1      jmmv  * Macros/functions to convert from generic data structures to tmpfs
    291       1.1      jmmv  * specific ones.
    292       1.1      jmmv  */
    293       1.1      jmmv 
    294      1.16     perry static __inline
    295       1.1      jmmv struct tmpfs_mount *
    296       1.1      jmmv VFS_TO_TMPFS(struct mount *mp)
    297       1.1      jmmv {
    298       1.1      jmmv 	struct tmpfs_mount *tmp;
    299       1.1      jmmv 
    300      1.14  christos #ifdef KASSERT
    301       1.1      jmmv 	KASSERT((mp) != NULL && (mp)->mnt_data != NULL);
    302      1.14  christos #endif
    303       1.1      jmmv 	tmp = (struct tmpfs_mount *)(mp)->mnt_data;
    304       1.1      jmmv 	return tmp;
    305       1.1      jmmv }
    306       1.1      jmmv 
    307      1.16     perry static __inline
    308       1.1      jmmv struct tmpfs_node *
    309       1.1      jmmv VP_TO_TMPFS_DIR(struct vnode *vp)
    310       1.1      jmmv {
    311       1.1      jmmv 	struct tmpfs_node *node;
    312       1.1      jmmv 
    313       1.1      jmmv 	node = VP_TO_TMPFS_NODE(vp);
    314      1.14  christos #ifdef KASSERT
    315       1.1      jmmv 	TMPFS_VALIDATE_DIR(node);
    316      1.14  christos #endif
    317       1.1      jmmv 	return node;
    318       1.1      jmmv }
    319      1.10  christos #endif /* _FS_TMPFS_TMPFS_H_ */
    320