1ad3638eeSXin LI /* $NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $ */ 2d1fa59e9SXin LI 3e08d5567SXin LI /*- 4ad3638eeSXin LI * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. 5d1fa59e9SXin LI * All rights reserved. 6d1fa59e9SXin LI * 7d1fa59e9SXin LI * This code is derived from software contributed to The NetBSD Foundation 8d1fa59e9SXin LI * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 9d1fa59e9SXin LI * 2005 program. 10d1fa59e9SXin LI * 11d1fa59e9SXin LI * Redistribution and use in source and binary forms, with or without 12d1fa59e9SXin LI * modification, are permitted provided that the following conditions 13d1fa59e9SXin LI * are met: 14d1fa59e9SXin LI * 1. Redistributions of source code must retain the above copyright 15d1fa59e9SXin LI * notice, this list of conditions and the following disclaimer. 16d1fa59e9SXin LI * 2. Redistributions in binary form must reproduce the above copyright 17d1fa59e9SXin LI * notice, this list of conditions and the following disclaimer in the 18d1fa59e9SXin LI * documentation and/or other materials provided with the distribution. 19d1fa59e9SXin LI * 20d1fa59e9SXin LI * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21d1fa59e9SXin LI * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22d1fa59e9SXin LI * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23d1fa59e9SXin LI * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24d1fa59e9SXin LI * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25d1fa59e9SXin LI * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26d1fa59e9SXin LI * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27d1fa59e9SXin LI * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28d1fa59e9SXin LI * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29d1fa59e9SXin LI * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30d1fa59e9SXin LI * POSSIBILITY OF SUCH DAMAGE. 31d1fa59e9SXin LI * 32d1fa59e9SXin LI * $FreeBSD$ 33d1fa59e9SXin LI */ 34d1fa59e9SXin LI 35d1fa59e9SXin LI #ifndef _FS_TMPFS_TMPFS_H_ 36d1fa59e9SXin LI #define _FS_TMPFS_TMPFS_H_ 37d1fa59e9SXin LI 38d1fa59e9SXin LI /* --------------------------------------------------------------------- 39d1fa59e9SXin LI * KERNEL-SPECIFIC DEFINITIONS 40d1fa59e9SXin LI * --------------------------------------------------------------------- */ 41d1fa59e9SXin LI #include <sys/dirent.h> 42d1fa59e9SXin LI #include <sys/mount.h> 43d1fa59e9SXin LI #include <sys/queue.h> 44d1fa59e9SXin LI #include <sys/vnode.h> 45d1fa59e9SXin LI #include <sys/file.h> 46d1fa59e9SXin LI #include <sys/lock.h> 47d1fa59e9SXin LI #include <sys/mutex.h> 48d1fa59e9SXin LI 49d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 50d1fa59e9SXin LI #include <sys/malloc.h> 51d1fa59e9SXin LI #include <sys/systm.h> 52d1fa59e9SXin LI #include <sys/vmmeter.h> 53d1fa59e9SXin LI #include <vm/swap_pager.h> 54d1fa59e9SXin LI 55d1fa59e9SXin LI MALLOC_DECLARE(M_TMPFSMNT); 569b258fcaSXin LI MALLOC_DECLARE(M_TMPFSNAME); 57d1fa59e9SXin LI 58d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 59d1fa59e9SXin LI 60d1fa59e9SXin LI /* 61d1fa59e9SXin LI * Internal representation of a tmpfs directory entry. 62d1fa59e9SXin LI */ 63d1fa59e9SXin LI struct tmpfs_dirent { 64d1fa59e9SXin LI TAILQ_ENTRY(tmpfs_dirent) td_entries; 65d1fa59e9SXin LI 66d1fa59e9SXin LI /* Length of the name stored in this directory entry. This avoids 67d1fa59e9SXin LI * the need to recalculate it every time the name is used. */ 68d1fa59e9SXin LI uint16_t td_namelen; 69d1fa59e9SXin LI 70d1fa59e9SXin LI /* The name of the entry, allocated from a string pool. This 71d1fa59e9SXin LI * string is not required to be zero-terminated; therefore, the 72d1fa59e9SXin LI * td_namelen field must always be used when accessing its value. */ 73d1fa59e9SXin LI char * td_name; 74d1fa59e9SXin LI 7599d57a6bSEd Schouten /* Pointer to the node this entry refers to. In case this field 7699d57a6bSEd Schouten * is NULL, the node is a whiteout. */ 77d1fa59e9SXin LI struct tmpfs_node * td_node; 78d1fa59e9SXin LI }; 79d1fa59e9SXin LI 80d1fa59e9SXin LI /* A directory in tmpfs holds a sorted list of directory entries, which in 81d1fa59e9SXin LI * turn point to other files (which can be directories themselves). 82d1fa59e9SXin LI * 83d1fa59e9SXin LI * In tmpfs, this list is managed by a tail queue, whose head is defined by 84d1fa59e9SXin LI * the struct tmpfs_dir type. 85d1fa59e9SXin LI * 86d1fa59e9SXin LI * It is imporant to notice that directories do not have entries for . and 87d1fa59e9SXin LI * .. as other file systems do. These can be generated when requested 88d1fa59e9SXin LI * based on information available by other means, such as the pointer to 89d1fa59e9SXin LI * the node itself in the former case or the pointer to the parent directory 90d1fa59e9SXin LI * in the latter case. This is done to simplify tmpfs's code and, more 91d1fa59e9SXin LI * importantly, to remove redundancy. */ 92d1fa59e9SXin LI TAILQ_HEAD(tmpfs_dir, tmpfs_dirent); 93d1fa59e9SXin LI 94ad3638eeSXin LI /* Each entry in a directory has a cookie that identifies it. Cookies 95ad3638eeSXin LI * supersede offsets within directories because, given how tmpfs stores 96ad3638eeSXin LI * directories in memory, there is no such thing as an offset. (Emulating 97ad3638eeSXin LI * a real offset could be very difficult.) 98ad3638eeSXin LI * 99ad3638eeSXin LI * The '.', '..' and the end of directory markers have fixed cookies which 100ad3638eeSXin LI * cannot collide with the cookies generated by other entries. The cookies 101ad3638eeSXin LI * fot the other entries are generated based on the memory address on which 102ad3638eeSXin LI * stores their information is stored. 103ad3638eeSXin LI * 104ad3638eeSXin LI * Ideally, using the entry's memory pointer as the cookie would be enough 105ad3638eeSXin LI * to represent it and it wouldn't cause collisions in any system. 106ad3638eeSXin LI * Unfortunately, this results in "offsets" with very large values which 107ad3638eeSXin LI * later raise problems in the Linux compatibility layer (and maybe in other 108ad3638eeSXin LI * places) as described in PR kern/32034. Hence we need to workaround this 109ad3638eeSXin LI * with a rather ugly hack. 110ad3638eeSXin LI * 111ad3638eeSXin LI * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t 112ad3638eeSXin LI * set to 'long', which is a 32-bit *signed* long integer. Regardless of 113ad3638eeSXin LI * the macro value, GLIBC (2.3 at least) always uses the getdents64 114ad3638eeSXin LI * system call (when calling readdir) which internally returns off64_t 115ad3638eeSXin LI * offsets. In order to make 32-bit binaries work, *GLIBC* converts the 116ad3638eeSXin LI * 64-bit values returned by the kernel to 32-bit ones and aborts with 117ad3638eeSXin LI * EOVERFLOW if the conversion results in values that won't fit in 32-bit 118ad3638eeSXin LI * integers (which it assumes is because the directory is extremely large). 119ad3638eeSXin LI * This wouldn't cause problems if we were dealing with unsigned integers, 120ad3638eeSXin LI * but as we have signed integers, this check fails due to sign expansion. 121ad3638eeSXin LI * 122ad3638eeSXin LI * For example, consider that the kernel returns the 0xc1234567 cookie to 123ad3638eeSXin LI * userspace in a off64_t integer. Later on, GLIBC casts this value to 124ad3638eeSXin LI * off_t (remember, signed) with code similar to: 125ad3638eeSXin LI * system call returns the offset in kernel_value; 126ad3638eeSXin LI * off_t casted_value = kernel_value; 127ad3638eeSXin LI * if (sizeof(off_t) != sizeof(off64_t) && 128ad3638eeSXin LI * kernel_value != casted_value) 129ad3638eeSXin LI * error! 130ad3638eeSXin LI * In this case, casted_value still has 0xc1234567, but when it is compared 131ad3638eeSXin LI * for equality against kernel_value, it is promoted to a 64-bit integer and 132ad3638eeSXin LI * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567. 133ad3638eeSXin LI * Then, GLIBC assumes this is because the directory is very large. 134ad3638eeSXin LI * 135ad3638eeSXin LI * Given that all the above happens in user-space, we have no control over 136ad3638eeSXin LI * it; therefore we must workaround the issue here. We do this by 137ad3638eeSXin LI * truncating the pointer value to a 32-bit integer and hope that there 138ad3638eeSXin LI * won't be collisions. In fact, this will not cause any problems in 139ad3638eeSXin LI * 32-bit platforms but some might arise in 64-bit machines (I'm not sure 140ad3638eeSXin LI * if they can happen at all in practice). 141ad3638eeSXin LI * 142ad3638eeSXin LI * XXX A nicer solution shall be attempted. */ 143ad3638eeSXin LI #ifdef _KERNEL 144d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_DOT 0 145d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_DOTDOT 1 146d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_EOF 2 147ad3638eeSXin LI static __inline 148ad3638eeSXin LI off_t 149ad3638eeSXin LI tmpfs_dircookie(struct tmpfs_dirent *de) 150ad3638eeSXin LI { 151ad3638eeSXin LI off_t cookie; 152ad3638eeSXin LI 153ad3638eeSXin LI cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF; 154ad3638eeSXin LI MPASS(cookie != TMPFS_DIRCOOKIE_DOT); 155ad3638eeSXin LI MPASS(cookie != TMPFS_DIRCOOKIE_DOTDOT); 156ad3638eeSXin LI MPASS(cookie != TMPFS_DIRCOOKIE_EOF); 157ad3638eeSXin LI 158ad3638eeSXin LI return cookie; 159ad3638eeSXin LI } 160ad3638eeSXin LI #endif 161d1fa59e9SXin LI 162d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 163d1fa59e9SXin LI 164d1fa59e9SXin LI /* 165d1fa59e9SXin LI * Internal representation of a tmpfs file system node. 166d1fa59e9SXin LI * 167d1fa59e9SXin LI * This structure is splitted in two parts: one holds attributes common 168d1fa59e9SXin LI * to all file types and the other holds data that is only applicable to 169d1fa59e9SXin LI * a particular type. The code must be careful to only access those 170d1fa59e9SXin LI * attributes that are actually allowed by the node's type. 171d1fa59e9SXin LI * 172d1fa59e9SXin LI * 173d1fa59e9SXin LI * Below is the key of locks used to protected the fields in the following 174d1fa59e9SXin LI * structures. 175d1fa59e9SXin LI * 176d1fa59e9SXin LI */ 177d1fa59e9SXin LI struct tmpfs_node { 178d1fa59e9SXin LI /* Doubly-linked list entry which links all existing nodes for a 179d1fa59e9SXin LI * single file system. This is provided to ease the removal of 180d1fa59e9SXin LI * all nodes during the unmount operation. */ 181d1fa59e9SXin LI LIST_ENTRY(tmpfs_node) tn_entries; 182d1fa59e9SXin LI 183d1fa59e9SXin LI /* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO', 184d1fa59e9SXin LI * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode 185d1fa59e9SXin LI * types instead of a custom enumeration is to make things simpler 186d1fa59e9SXin LI * and faster, as we do not need to convert between two types. */ 187d1fa59e9SXin LI enum vtype tn_type; 188d1fa59e9SXin LI 189d1fa59e9SXin LI /* Node identifier. */ 190d1fa59e9SXin LI ino_t tn_id; 191d1fa59e9SXin LI 192d1fa59e9SXin LI /* Node's internal status. This is used by several file system 193d1fa59e9SXin LI * operations to do modifications to the node in a delayed 194d1fa59e9SXin LI * fashion. */ 195d1fa59e9SXin LI int tn_status; 196d1fa59e9SXin LI #define TMPFS_NODE_ACCESSED (1 << 1) 197d1fa59e9SXin LI #define TMPFS_NODE_MODIFIED (1 << 2) 198d1fa59e9SXin LI #define TMPFS_NODE_CHANGED (1 << 3) 199d1fa59e9SXin LI 200d1fa59e9SXin LI /* The node size. It does not necessarily match the real amount 201d1fa59e9SXin LI * of memory consumed by it. */ 202d1fa59e9SXin LI off_t tn_size; 203d1fa59e9SXin LI 204d1fa59e9SXin LI /* Generic node attributes. */ 205d1fa59e9SXin LI uid_t tn_uid; 206d1fa59e9SXin LI gid_t tn_gid; 207d1fa59e9SXin LI mode_t tn_mode; 208d1fa59e9SXin LI int tn_flags; 209d1fa59e9SXin LI nlink_t tn_links; 210d1fa59e9SXin LI struct timespec tn_atime; 211d1fa59e9SXin LI struct timespec tn_mtime; 212d1fa59e9SXin LI struct timespec tn_ctime; 213d1fa59e9SXin LI struct timespec tn_birthtime; 214d1fa59e9SXin LI unsigned long tn_gen; 215d1fa59e9SXin LI 216d1fa59e9SXin LI /* As there is a single vnode for each active file within the 217d1fa59e9SXin LI * system, care has to be taken to avoid allocating more than one 218d1fa59e9SXin LI * vnode per file. In order to do this, a bidirectional association 219d1fa59e9SXin LI * is kept between vnodes and nodes. 220d1fa59e9SXin LI * 221d1fa59e9SXin LI * Whenever a vnode is allocated, its v_data field is updated to 222d1fa59e9SXin LI * point to the node it references. At the same time, the node's 223d1fa59e9SXin LI * tn_vnode field is modified to point to the new vnode representing 224d1fa59e9SXin LI * it. Further attempts to allocate a vnode for this same node will 225d1fa59e9SXin LI * result in returning a new reference to the value stored in 226d1fa59e9SXin LI * tn_vnode. 227d1fa59e9SXin LI * 228d1fa59e9SXin LI * May be NULL when the node is unused (that is, no vnode has been 229d1fa59e9SXin LI * allocated for it or it has been reclaimed). */ 230d1fa59e9SXin LI struct vnode * tn_vnode; 231d1fa59e9SXin LI 232d1fa59e9SXin LI /* interlock to protect tn_vpstate */ 233d1fa59e9SXin LI struct mtx tn_interlock; 234d1fa59e9SXin LI 235d1fa59e9SXin LI /* Identify if current node has vnode assiocate with 236d1fa59e9SXin LI * or allocating vnode. 237d1fa59e9SXin LI */ 238d1fa59e9SXin LI int tn_vpstate; 239d1fa59e9SXin LI 240d1fa59e9SXin LI /* misc data field for different tn_type node */ 241d1fa59e9SXin LI union { 242d1fa59e9SXin LI /* Valid when tn_type == VBLK || tn_type == VCHR. */ 243d1fa59e9SXin LI dev_t tn_rdev; 244d1fa59e9SXin LI 245d1fa59e9SXin LI /* Valid when tn_type == VDIR. */ 246d1fa59e9SXin LI struct tn_dir{ 247d1fa59e9SXin LI /* Pointer to the parent directory. The root 248d1fa59e9SXin LI * directory has a pointer to itself in this field; 249d1fa59e9SXin LI * this property identifies the root node. */ 250d1fa59e9SXin LI struct tmpfs_node * tn_parent; 251d1fa59e9SXin LI 252d1fa59e9SXin LI /* Head of a tail-queue that links the contents of 253d1fa59e9SXin LI * the directory together. See above for a 254d1fa59e9SXin LI * description of its contents. */ 255d1fa59e9SXin LI struct tmpfs_dir tn_dirhead; 256d1fa59e9SXin LI 257d1fa59e9SXin LI /* Number and pointer of the first directory entry 258d1fa59e9SXin LI * returned by the readdir operation if it were 259d1fa59e9SXin LI * called again to continue reading data from the 260d1fa59e9SXin LI * same directory as before. This is used to speed 261d1fa59e9SXin LI * up reads of long directories, assuming that no 262d1fa59e9SXin LI * more than one read is in progress at a given time. 263d1fa59e9SXin LI * Otherwise, these values are discarded and a linear 264d1fa59e9SXin LI * scan is performed from the beginning up to the 265d1fa59e9SXin LI * point where readdir starts returning values. */ 266d1fa59e9SXin LI off_t tn_readdir_lastn; 267d1fa59e9SXin LI struct tmpfs_dirent * tn_readdir_lastp; 268d1fa59e9SXin LI }tn_dir; 269d1fa59e9SXin LI 270d1fa59e9SXin LI /* Valid when tn_type == VLNK. */ 271d1fa59e9SXin LI /* The link's target, allocated from a string pool. */ 272d1fa59e9SXin LI char * tn_link; 273d1fa59e9SXin LI 274d1fa59e9SXin LI /* Valid when tn_type == VREG. */ 275d1fa59e9SXin LI struct tn_reg { 276d1fa59e9SXin LI /* The contents of regular files stored in a tmpfs 277d1fa59e9SXin LI * file system are represented by a single anonymous 278d1fa59e9SXin LI * memory object (aobj, for short). The aobj provides 279d1fa59e9SXin LI * direct access to any position within the file, 280d1fa59e9SXin LI * because its contents are always mapped in a 281d1fa59e9SXin LI * contiguous region of virtual memory. It is a task 282d1fa59e9SXin LI * of the memory management subsystem (see uvm(9)) to 283d1fa59e9SXin LI * issue the required page ins or page outs whenever 284d1fa59e9SXin LI * a position within the file is accessed. */ 285d1fa59e9SXin LI vm_object_t tn_aobj; 286d1fa59e9SXin LI 287d1fa59e9SXin LI }tn_reg; 288d1fa59e9SXin LI 289d1fa59e9SXin LI /* Valid when tn_type = VFIFO */ 290d1fa59e9SXin LI struct tn_fifo { 291d1fa59e9SXin LI fo_rdwr_t *tn_fo_read; 292d1fa59e9SXin LI fo_rdwr_t *tn_fo_write; 293d1fa59e9SXin LI }tn_fifo; 294d1fa59e9SXin LI }tn_spec; 295d1fa59e9SXin LI }; 296d1fa59e9SXin LI LIST_HEAD(tmpfs_node_list, tmpfs_node); 297d1fa59e9SXin LI 298d1fa59e9SXin LI #define tn_rdev tn_spec.tn_rdev 299d1fa59e9SXin LI #define tn_dir tn_spec.tn_dir 300d1fa59e9SXin LI #define tn_link tn_spec.tn_link 301d1fa59e9SXin LI #define tn_reg tn_spec.tn_reg 302d1fa59e9SXin LI #define tn_fifo tn_spec.tn_fifo 303d1fa59e9SXin LI 304d1fa59e9SXin LI #define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock) 305d1fa59e9SXin LI #define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock) 306fb755714SXin LI #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock) 307d1fa59e9SXin LI 30882cf92d4SXin LI #ifdef INVARIANTS 30982cf92d4SXin LI #define TMPFS_ASSERT_LOCKED(node) do { \ 31082cf92d4SXin LI MPASS(node != NULL); \ 31182cf92d4SXin LI MPASS(node->tn_vnode != NULL); \ 31282cf92d4SXin LI if (!VOP_ISLOCKED(node->tn_vnode) && \ 31382cf92d4SXin LI !mtx_owned(TMPFS_NODE_MTX(node))) \ 31482cf92d4SXin LI panic("tmpfs: node is not locked: %p", node); \ 31582cf92d4SXin LI } while (0) 31682cf92d4SXin LI #define TMPFS_ASSERT_ELOCKED(node) do { \ 31782cf92d4SXin LI MPASS((node) != NULL); \ 31882cf92d4SXin LI MPASS((node)->tn_vnode != NULL); \ 31982cf92d4SXin LI mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED); \ 32082cf92d4SXin LI ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs"); \ 32182cf92d4SXin LI } while (0) 32282cf92d4SXin LI #else 32382cf92d4SXin LI #define TMPFS_ASSERT_LOCKED(node) (void)0 32482cf92d4SXin LI #define TMPFS_ASSERT_ELOCKED(node) (void)0 32582cf92d4SXin LI #endif 32682cf92d4SXin LI 327d1fa59e9SXin LI #define TMPFS_VNODE_ALLOCATING 1 328d1fa59e9SXin LI #define TMPFS_VNODE_WANT 2 32982cf92d4SXin LI #define TMPFS_VNODE_DOOMED 4 330d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 331d1fa59e9SXin LI 332d1fa59e9SXin LI /* 333d1fa59e9SXin LI * Internal representation of a tmpfs mount point. 334d1fa59e9SXin LI */ 335d1fa59e9SXin LI struct tmpfs_mount { 336d1fa59e9SXin LI /* Maximum number of memory pages available for use by the file 337d1fa59e9SXin LI * system, set during mount time. This variable must never be 338974fd8c6SXin LI * used directly as it may be bigger than the current amount of 339d1fa59e9SXin LI * free memory; in the extreme case, it will hold the SIZE_MAX 340*da7aa277SGleb Kurtsou * value. */ 341d1fa59e9SXin LI size_t tm_pages_max; 342d1fa59e9SXin LI 343*da7aa277SGleb Kurtsou /* Number of pages in use by the file system. */ 344d1fa59e9SXin LI size_t tm_pages_used; 345d1fa59e9SXin LI 346d1fa59e9SXin LI /* Pointer to the node representing the root directory of this 347d1fa59e9SXin LI * file system. */ 348d1fa59e9SXin LI struct tmpfs_node * tm_root; 349d1fa59e9SXin LI 350d1fa59e9SXin LI /* Maximum number of possible nodes for this file system; set 351d1fa59e9SXin LI * during mount time. We need a hard limit on the maximum number 352d1fa59e9SXin LI * of nodes to avoid allocating too much of them; their objects 353d1fa59e9SXin LI * cannot be released until the file system is unmounted. 354d1fa59e9SXin LI * Otherwise, we could easily run out of memory by creating lots 355d1fa59e9SXin LI * of empty files and then simply removing them. */ 356d1fa59e9SXin LI ino_t tm_nodes_max; 357d1fa59e9SXin LI 3588d9a89a3SXin LI /* unrhdr used to allocate inode numbers */ 3598d9a89a3SXin LI struct unrhdr * tm_ino_unr; 360d1fa59e9SXin LI 361d1fa59e9SXin LI /* Number of nodes currently that are in use. */ 362d1fa59e9SXin LI ino_t tm_nodes_inuse; 363d1fa59e9SXin LI 364d1fa59e9SXin LI /* maximum representable file size */ 365d1fa59e9SXin LI u_int64_t tm_maxfilesize; 366d1fa59e9SXin LI 367d1fa59e9SXin LI /* Nodes are organized in two different lists. The used list 368d1fa59e9SXin LI * contains all nodes that are currently used by the file system; 369d1fa59e9SXin LI * i.e., they refer to existing files. The available list contains 370d1fa59e9SXin LI * all nodes that are currently available for use by new files. 371d1fa59e9SXin LI * Nodes must be kept in this list (instead of deleting them) 372d1fa59e9SXin LI * because we need to keep track of their generation number (tn_gen 373d1fa59e9SXin LI * field). 374d1fa59e9SXin LI * 375d1fa59e9SXin LI * Note that nodes are lazily allocated: if the available list is 376d1fa59e9SXin LI * empty and we have enough space to create more nodes, they will be 377d1fa59e9SXin LI * created and inserted in the used list. Once these are released, 378d1fa59e9SXin LI * they will go into the available list, remaining alive until the 379d1fa59e9SXin LI * file system is unmounted. */ 380d1fa59e9SXin LI struct tmpfs_node_list tm_nodes_used; 381d1fa59e9SXin LI 382d1fa59e9SXin LI /* All node lock to protect the node list and tmp_pages_used */ 383d1fa59e9SXin LI struct mtx allnode_lock; 384d1fa59e9SXin LI 385d1fa59e9SXin LI /* Pools used to store file system meta data. These are not shared 386d1fa59e9SXin LI * across several instances of tmpfs for the reasons described in 387d1fa59e9SXin LI * tmpfs_pool.c. */ 388d1fa59e9SXin LI uma_zone_t tm_dirent_pool; 389d1fa59e9SXin LI uma_zone_t tm_node_pool; 390d1fa59e9SXin LI }; 391d1fa59e9SXin LI #define TMPFS_LOCK(tm) mtx_lock(&(tm)->allnode_lock) 392d1fa59e9SXin LI #define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->allnode_lock) 393d1fa59e9SXin LI 394d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 395d1fa59e9SXin LI 396d1fa59e9SXin LI /* 397d1fa59e9SXin LI * This structure maps a file identifier to a tmpfs node. Used by the 398d1fa59e9SXin LI * NFS code. 399d1fa59e9SXin LI */ 400d1fa59e9SXin LI struct tmpfs_fid { 401d1fa59e9SXin LI uint16_t tf_len; 402d1fa59e9SXin LI uint16_t tf_pad; 403d1fa59e9SXin LI ino_t tf_id; 4045ff9b915SXin LI unsigned long tf_gen; 405d1fa59e9SXin LI }; 406d1fa59e9SXin LI 407d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 408d1fa59e9SXin LI 409d1fa59e9SXin LI #ifdef _KERNEL 410d1fa59e9SXin LI /* 411d1fa59e9SXin LI * Prototypes for tmpfs_subr.c. 412d1fa59e9SXin LI */ 413d1fa59e9SXin LI 414d1fa59e9SXin LI int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype, 415d1fa59e9SXin LI uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *, 416dfd233edSAttilio Rao char *, dev_t, struct tmpfs_node **); 417d1fa59e9SXin LI void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *); 418d1fa59e9SXin LI int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *, 419d1fa59e9SXin LI const char *, uint16_t, struct tmpfs_dirent **); 420d1fa59e9SXin LI void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *, 421d1fa59e9SXin LI boolean_t); 4220ae6383dSXin LI int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int, 423dfd233edSAttilio Rao struct vnode **); 424d1fa59e9SXin LI void tmpfs_free_vp(struct vnode *); 425d1fa59e9SXin LI int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *, 426d1fa59e9SXin LI struct componentname *, char *); 427d1fa59e9SXin LI void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *); 428d1fa59e9SXin LI void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *); 429d1fa59e9SXin LI struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node, 430e3c7e753SKonstantin Belousov struct tmpfs_node *f, 431d1fa59e9SXin LI struct componentname *cnp); 432d1fa59e9SXin LI int tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *); 433d1fa59e9SXin LI int tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *); 434d1fa59e9SXin LI struct tmpfs_dirent * tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t); 435d1fa59e9SXin LI int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *); 43699d57a6bSEd Schouten int tmpfs_dir_whiteout_add(struct vnode *, struct componentname *); 43799d57a6bSEd Schouten void tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *); 4380b05cac3SAlan Cox int tmpfs_reg_resize(struct vnode *, off_t, boolean_t); 439d1fa59e9SXin LI int tmpfs_chflags(struct vnode *, int, struct ucred *, struct thread *); 440d1fa59e9SXin LI int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *); 441d1fa59e9SXin LI int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *, 442d1fa59e9SXin LI struct thread *); 443d1fa59e9SXin LI int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *); 444d1fa59e9SXin LI int tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *, 445d1fa59e9SXin LI struct timespec *, int, struct ucred *, struct thread *); 446d1fa59e9SXin LI void tmpfs_itimes(struct vnode *, const struct timespec *, 447d1fa59e9SXin LI const struct timespec *); 448d1fa59e9SXin LI 449d1fa59e9SXin LI void tmpfs_update(struct vnode *); 450d1fa59e9SXin LI int tmpfs_truncate(struct vnode *, off_t); 451d1fa59e9SXin LI 452d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 453d1fa59e9SXin LI 454d1fa59e9SXin LI /* 455d1fa59e9SXin LI * Convenience macros to simplify some logical expressions. 456d1fa59e9SXin LI */ 457d1fa59e9SXin LI #define IMPLIES(a, b) (!(a) || (b)) 458d1fa59e9SXin LI #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a)) 459d1fa59e9SXin LI 460d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 461d1fa59e9SXin LI 462d1fa59e9SXin LI /* 463d1fa59e9SXin LI * Checks that the directory entry pointed by 'de' matches the name 'name' 464d1fa59e9SXin LI * with a length of 'len'. 465d1fa59e9SXin LI */ 466d1fa59e9SXin LI #define TMPFS_DIRENT_MATCHES(de, name, len) \ 467d1fa59e9SXin LI (de->td_namelen == (uint16_t)len && \ 468ae72afe0SDavid E. O'Brien bcmp((de)->td_name, (name), (de)->td_namelen) == 0) 469d1fa59e9SXin LI 470d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 471d1fa59e9SXin LI 472d1fa59e9SXin LI /* 473d1fa59e9SXin LI * Ensures that the node pointed by 'node' is a directory and that its 474d1fa59e9SXin LI * contents are consistent with respect to directories. 475d1fa59e9SXin LI */ 476d1fa59e9SXin LI #define TMPFS_VALIDATE_DIR(node) \ 477d1fa59e9SXin LI MPASS((node)->tn_type == VDIR); \ 478d1fa59e9SXin LI MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \ 479d1fa59e9SXin LI MPASS((node)->tn_dir.tn_readdir_lastp == NULL || \ 480ad3638eeSXin LI tmpfs_dircookie((node)->tn_dir.tn_readdir_lastp) == (node)->tn_dir.tn_readdir_lastn); 481d1fa59e9SXin LI 482d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 483d1fa59e9SXin LI 484d1fa59e9SXin LI /* 485d1fa59e9SXin LI * Memory management stuff. 486d1fa59e9SXin LI */ 487d1fa59e9SXin LI 488d1fa59e9SXin LI /* 489*da7aa277SGleb Kurtsou * Amount of memory pages to reserve for the system (e.g., to not use by 490*da7aa277SGleb Kurtsou * tmpfs). 491d1fa59e9SXin LI */ 492*da7aa277SGleb Kurtsou #define TMPFS_PAGES_MINRESERVED (4 * 1024 * 1024 / PAGE_SIZE) 493d1fa59e9SXin LI 494*da7aa277SGleb Kurtsou size_t tmpfs_mem_avail(void); 495d1fa59e9SXin LI 496*da7aa277SGleb Kurtsou size_t tmpfs_pages_used(struct tmpfs_mount *tmp); 497d1fa59e9SXin LI 498d1fa59e9SXin LI #endif 499d1fa59e9SXin LI 500d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 501d1fa59e9SXin LI 502d1fa59e9SXin LI /* 503d1fa59e9SXin LI * Macros/functions to convert from generic data structures to tmpfs 504d1fa59e9SXin LI * specific ones. 505d1fa59e9SXin LI */ 506d1fa59e9SXin LI 507d1fa59e9SXin LI static inline 508d1fa59e9SXin LI struct tmpfs_mount * 509d1fa59e9SXin LI VFS_TO_TMPFS(struct mount *mp) 510d1fa59e9SXin LI { 511d1fa59e9SXin LI struct tmpfs_mount *tmp; 512d1fa59e9SXin LI 513d1fa59e9SXin LI MPASS((mp) != NULL && (mp)->mnt_data != NULL); 514d1fa59e9SXin LI tmp = (struct tmpfs_mount *)(mp)->mnt_data; 515d1fa59e9SXin LI return tmp; 516d1fa59e9SXin LI } 517d1fa59e9SXin LI 518d1fa59e9SXin LI static inline 519d1fa59e9SXin LI struct tmpfs_node * 520d1fa59e9SXin LI VP_TO_TMPFS_NODE(struct vnode *vp) 521d1fa59e9SXin LI { 522d1fa59e9SXin LI struct tmpfs_node *node; 523d1fa59e9SXin LI 524d1fa59e9SXin LI MPASS((vp) != NULL && (vp)->v_data != NULL); 525d1fa59e9SXin LI node = (struct tmpfs_node *)vp->v_data; 526d1fa59e9SXin LI return node; 527d1fa59e9SXin LI } 528d1fa59e9SXin LI 529d1fa59e9SXin LI static inline 530d1fa59e9SXin LI struct tmpfs_node * 531d1fa59e9SXin LI VP_TO_TMPFS_DIR(struct vnode *vp) 532d1fa59e9SXin LI { 533d1fa59e9SXin LI struct tmpfs_node *node; 534d1fa59e9SXin LI 535d1fa59e9SXin LI node = VP_TO_TMPFS_NODE(vp); 536d1fa59e9SXin LI TMPFS_VALIDATE_DIR(node); 537d1fa59e9SXin LI return node; 538d1fa59e9SXin LI } 539d1fa59e9SXin LI 540d1fa59e9SXin LI #endif /* _FS_TMPFS_TMPFS_H_ */ 541