/* $NetBSD: tmpfs.h,v 1.36 2008/07/28 18:00:20 pooka Exp $ */ /* * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Julio M. Merino Vidal, developed as part of Google's Summer of Code * 2005 program. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef _FS_TMPFS_TMPFS_H_ #define _FS_TMPFS_TMPFS_H_ #include #include #include #include #include #include /* --------------------------------------------------------------------- */ /* Each entry in a directory has a cookie that identifies it. Cookies * supersede offsets within directories because, given how tmpfs stores * directories in memory, there is no such thing as an offset. (Emulating * a real offset could be very difficult.) * * The '.', '..' and the end of directory markers have fixed cookies which * cannot collide with the cookies generated by other entries. The cookies * fot the other entries are generated based on the memory address on which * stores their information is stored. * * Ideally, using the entry's memory pointer as the cookie would be enough * to represent it and it wouldn't cause collisions in any system. * Unfortunately, this results in "offsets" with very large values which * later raise problems in the Linux compatibility layer (and maybe in other * places) as described in PR kern/32034. Hence we need to workaround this * with a rather ugly hack. * * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t * set to 'long', which is a 32-bit *signed* long integer. Regardless of * the macro value, GLIBC (2.3 at least) always uses the getdents64 * system call (when calling readdir) which internally returns off64_t * offsets. In order to make 32-bit binaries work, *GLIBC* converts the * 64-bit values returned by the kernel to 32-bit ones and aborts with * EOVERFLOW if the conversion results in values that won't fit in 32-bit * integers (which it assumes is because the directory is extremely large). * This wouldn't cause problems if we were dealing with unsigned integers, * but as we have signed integers, this check fails due to sign expansion. * * For example, consider that the kernel returns the 0xc1234567 cookie to * userspace in a off64_t integer. Later on, GLIBC casts this value to * off_t (remember, signed) with code similar to: * system call returns the offset in kernel_value; * off_t casted_value = kernel_value; * if (sizeof(off_t) != sizeof(off64_t) && * kernel_value != casted_value) * error! * In this case, casted_value still has 0xc1234567, but when it is compared * for equality against kernel_value, it is promoted to a 64-bit integer and * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567. * Then, GLIBC assumes this is because the directory is very large. * * Given that all the above happens in user-space, we have no control over * it; therefore we must workaround the issue here. We do this by * truncating the pointer value to a 32-bit integer and hope that there * won't be collisions. In fact, this will not cause any problems in * 32-bit platforms but some might arise in 64-bit machines (I'm not sure * if they can happen at all in practice). * * XXX A nicer solution shall be attempted. */ #define TMPFS_DIRCOOKIE_DOT 0 #define TMPFS_DIRCOOKIE_DOTDOT 1 #define TMPFS_DIRCOOKIE_EOF 2 static __inline off_t tmpfs_dircookie(struct tmpfs_dirent *de) { off_t cookie; cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF; KASSERT(cookie != TMPFS_DIRCOOKIE_DOT); KASSERT(cookie != TMPFS_DIRCOOKIE_DOTDOT); KASSERT(cookie != TMPFS_DIRCOOKIE_EOF); return cookie; } /* --------------------------------------------------------------------- */ LIST_HEAD(tmpfs_node_list, tmpfs_node); /* --------------------------------------------------------------------- */ /* * Internal representation of a tmpfs mount point. */ struct tmpfs_mount { /* Maximum number of memory pages available for use by the file * system, set during mount time. This variable must never be * used directly as it may be bigger than the current amount of * free memory; in the extreme case, it will hold the SIZE_MAX * value. Instead, use the TMPFS_PAGES_MAX macro. */ unsigned int tm_pages_max; /* Number of pages in use by the file system. Cannot be bigger * than the value returned by TMPFS_PAGES_MAX in any case. */ unsigned int tm_pages_used; /* Pointer to the node representing the root directory of this * file system. */ struct tmpfs_node * tm_root; /* Maximum number of possible nodes for this file system; set * during mount time. We need a hard limit on the maximum number * of nodes to avoid allocating too much of them; their objects * cannot be released until the file system is unmounted. * Otherwise, we could easily run out of memory by creating lots * of empty files and then simply removing them. */ unsigned int tm_nodes_max; /* Number of nodes currently allocated. This number only grows. * When it reaches tm_nodes_max, no more new nodes can be allocated. * Of course, the old, unused ones can be reused. */ unsigned int tm_nodes_cnt; /* Node list. */ kmutex_t tm_lock; struct tmpfs_node_list tm_nodes; /* Pools used to store file system meta data. These are not shared * across several instances of tmpfs for the reasons described in * tmpfs_pool.c. */ struct tmpfs_pool tm_dirent_pool; struct tmpfs_pool tm_node_pool; struct tmpfs_str_pool tm_str_pool; }; /* --------------------------------------------------------------------- */ /* * This structure maps a file identifier to a tmpfs node. Used by the * NFS code. */ struct tmpfs_fid { uint16_t tf_len; uint16_t tf_pad; uint32_t tf_gen; ino_t tf_id; }; /* --------------------------------------------------------------------- */ /* * Prototypes for tmpfs_subr.c. */ int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype, uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *, char *, dev_t, struct tmpfs_node **); void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *); int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *, const char *, uint16_t, struct tmpfs_dirent **); void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *, bool); int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **); void tmpfs_free_vp(struct vnode *); int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *, struct componentname *, char *); void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *); void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *); struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node, struct componentname *cnp); int tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *); int tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *); struct tmpfs_dirent * tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t); int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *); int tmpfs_reg_resize(struct vnode *, off_t); size_t tmpfs_mem_info(bool); int tmpfs_chflags(struct vnode *, int, kauth_cred_t, struct lwp *); int tmpfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct lwp *); int tmpfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, struct lwp *); int tmpfs_chsize(struct vnode *, u_quad_t, kauth_cred_t, struct lwp *); int tmpfs_chtimes(struct vnode *, const struct timespec *, const struct timespec *, const struct timespec *, int, kauth_cred_t, struct lwp *); void tmpfs_itimes(struct vnode *, const struct timespec *, const struct timespec *, const struct timespec *); void tmpfs_update(struct vnode *, const struct timespec *, const struct timespec *, const struct timespec *, int); int tmpfs_truncate(struct vnode *, off_t); /* --------------------------------------------------------------------- */ /* * Convenience macros to simplify some logical expressions. */ #define IMPLIES(a, b) (!(a) || (b)) #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a)) /* --------------------------------------------------------------------- */ /* * Checks that the directory entry pointed by 'de' matches the name 'name' * with a length of 'len'. */ #define TMPFS_DIRENT_MATCHES(de, name, len) \ (de->td_namelen == (uint16_t)len && \ memcmp((de)->td_name, (name), (de)->td_namelen) == 0) /* --------------------------------------------------------------------- */ /* * Ensures that the node pointed by 'node' is a directory and that its * contents are consistent with respect to directories. */ #define TMPFS_VALIDATE_DIR(node) \ KASSERT((node)->tn_type == VDIR); \ KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \ KASSERT((node)->tn_spec.tn_dir.tn_readdir_lastp == NULL || \ tmpfs_dircookie((node)->tn_spec.tn_dir.tn_readdir_lastp) == \ (node)->tn_spec.tn_dir.tn_readdir_lastn); /* --------------------------------------------------------------------- */ /* * Memory management stuff. */ /* Amount of memory pages to reserve for the system (e.g., to not use by * tmpfs). * XXX: Should this be tunable through sysctl, for instance? */ #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE) /* Returns the maximum size allowed for a tmpfs file system. This macro * must be used instead of directly retrieving the value from tm_pages_max. * The reason is that the size of a tmpfs file system is dynamic: it lets * the user store files as long as there is enough free memory (including * physical memory and swap space). Therefore, the amount of memory to be * used is either the limit imposed by the user during mount time or the * amount of available memory, whichever is lower. To avoid consuming all * the memory for a given mount point, the system will always reserve a * minimum of TMPFS_PAGES_RESERVED pages, which is also taken into account * by this macro (see above). */ static __inline size_t TMPFS_PAGES_MAX(struct tmpfs_mount *tmp) { size_t freepages; freepages = tmpfs_mem_info(false); if (freepages < TMPFS_PAGES_RESERVED) freepages = 0; else freepages -= TMPFS_PAGES_RESERVED; return MIN(tmp->tm_pages_max, freepages + tmp->tm_pages_used); } /* Returns the available space for the given file system. */ #define TMPFS_PAGES_AVAIL(tmp) \ ((ssize_t)(TMPFS_PAGES_MAX(tmp) - (tmp)->tm_pages_used)) /* --------------------------------------------------------------------- */ /* * Macros/functions to convert from generic data structures to tmpfs * specific ones. */ static __inline struct tmpfs_mount * VFS_TO_TMPFS(struct mount *mp) { struct tmpfs_mount *tmp; #ifdef KASSERT KASSERT((mp) != NULL && (mp)->mnt_data != NULL); #endif tmp = (struct tmpfs_mount *)(mp)->mnt_data; return tmp; } static __inline struct tmpfs_node * VP_TO_TMPFS_DIR(struct vnode *vp) { struct tmpfs_node *node; node = VP_TO_TMPFS_NODE(vp); #ifdef KASSERT TMPFS_VALIDATE_DIR(node); #endif return node; } #endif /* _FS_TMPFS_TMPFS_H_ */