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> 52*4fd5efe7SGleb Kurtsou #include <sys/tree.h> 53d1fa59e9SXin LI #include <sys/vmmeter.h> 54d1fa59e9SXin LI #include <vm/swap_pager.h> 55d1fa59e9SXin LI 56d1fa59e9SXin LI MALLOC_DECLARE(M_TMPFSMNT); 579b258fcaSXin LI MALLOC_DECLARE(M_TMPFSNAME); 58d1fa59e9SXin LI 59d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 60d1fa59e9SXin LI 61d1fa59e9SXin LI /* 62d1fa59e9SXin LI * Internal representation of a tmpfs directory entry. 63d1fa59e9SXin LI */ 64*4fd5efe7SGleb Kurtsou 65*4fd5efe7SGleb Kurtsou LIST_HEAD(tmpfs_dir_duphead, tmpfs_dirent); 66*4fd5efe7SGleb Kurtsou 67d1fa59e9SXin LI struct tmpfs_dirent { 68*4fd5efe7SGleb Kurtsou /* 69*4fd5efe7SGleb Kurtsou * Depending on td_cookie flag entry can be of 3 types: 70*4fd5efe7SGleb Kurtsou * - regular -- no hash collisions, stored in RB-Tree 71*4fd5efe7SGleb Kurtsou * - duphead -- synthetic linked list head for dup entries 72*4fd5efe7SGleb Kurtsou * - dup -- stored in linked list instead of RB-Tree 73*4fd5efe7SGleb Kurtsou */ 74*4fd5efe7SGleb Kurtsou union { 75*4fd5efe7SGleb Kurtsou /* regular and duphead entry types */ 76*4fd5efe7SGleb Kurtsou RB_ENTRY(tmpfs_dirent) td_entries; 77d1fa59e9SXin LI 78*4fd5efe7SGleb Kurtsou /* dup entry type */ 79*4fd5efe7SGleb Kurtsou struct { 80*4fd5efe7SGleb Kurtsou LIST_ENTRY(tmpfs_dirent) entries; 81*4fd5efe7SGleb Kurtsou LIST_ENTRY(tmpfs_dirent) index_entries; 82*4fd5efe7SGleb Kurtsou } td_dup; 83*4fd5efe7SGleb Kurtsou } uh; 84d1fa59e9SXin LI 85*4fd5efe7SGleb Kurtsou uint32_t td_cookie; 86*4fd5efe7SGleb Kurtsou uint32_t td_hash; 87*4fd5efe7SGleb Kurtsou u_int td_namelen; 88d1fa59e9SXin LI 8999d57a6bSEd Schouten /* Pointer to the node this entry refers to. In case this field 9099d57a6bSEd Schouten * is NULL, the node is a whiteout. */ 91d1fa59e9SXin LI struct tmpfs_node * td_node; 92*4fd5efe7SGleb Kurtsou 93*4fd5efe7SGleb Kurtsou union { 94*4fd5efe7SGleb Kurtsou /* 95*4fd5efe7SGleb Kurtsou * The name of the entry, allocated from a string pool. This 96*4fd5efe7SGleb Kurtsou * string is not required to be zero-terminated. 97*4fd5efe7SGleb Kurtsou */ 98*4fd5efe7SGleb Kurtsou char * td_name; /* regular, dup */ 99*4fd5efe7SGleb Kurtsou struct tmpfs_dir_duphead td_duphead; /* duphead */ 100*4fd5efe7SGleb Kurtsou } ud; 101d1fa59e9SXin LI }; 102d1fa59e9SXin LI 103*4fd5efe7SGleb Kurtsou /* A directory in tmpfs holds a list of directory entries, which in 104d1fa59e9SXin LI * turn point to other files (which can be directories themselves). 105d1fa59e9SXin LI * 106*4fd5efe7SGleb Kurtsou * In tmpfs, this list is managed by a RB-Tree, whose head is defined by 107d1fa59e9SXin LI * the struct tmpfs_dir type. 108d1fa59e9SXin LI * 109*4fd5efe7SGleb Kurtsou * It is important to notice that directories do not have entries for . and 110d1fa59e9SXin LI * .. as other file systems do. These can be generated when requested 111d1fa59e9SXin LI * based on information available by other means, such as the pointer to 112d1fa59e9SXin LI * the node itself in the former case or the pointer to the parent directory 113d1fa59e9SXin LI * in the latter case. This is done to simplify tmpfs's code and, more 114d1fa59e9SXin LI * importantly, to remove redundancy. */ 115*4fd5efe7SGleb Kurtsou RB_HEAD(tmpfs_dir, tmpfs_dirent); 116d1fa59e9SXin LI 117ad3638eeSXin LI /* Each entry in a directory has a cookie that identifies it. Cookies 118ad3638eeSXin LI * supersede offsets within directories because, given how tmpfs stores 119*4fd5efe7SGleb Kurtsou * directories in memory, there is no such thing as an offset. 120ad3638eeSXin LI * 121ad3638eeSXin LI * The '.', '..' and the end of directory markers have fixed cookies which 122ad3638eeSXin LI * cannot collide with the cookies generated by other entries. The cookies 123*4fd5efe7SGleb Kurtsou * for the other entries are generated based on the file name hash value or 124*4fd5efe7SGleb Kurtsou * unique number in case of name hash collision. 125ad3638eeSXin LI * 126*4fd5efe7SGleb Kurtsou * To preserve compatibility cookies are limited to 31 bits. 127*4fd5efe7SGleb Kurtsou */ 128*4fd5efe7SGleb Kurtsou 129d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_DOT 0 130d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_DOTDOT 1 131d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_EOF 2 132*4fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_MASK ((off_t)0x3fffffffU) 133*4fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_MIN ((off_t)0x00000004U) 134*4fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUP ((off_t)0x40000000U) 135*4fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUPHEAD ((off_t)0x80000000U) 136*4fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUP_MIN TMPFS_DIRCOOKIE_DUP 137*4fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUP_MAX \ 138*4fd5efe7SGleb Kurtsou (TMPFS_DIRCOOKIE_DUP | TMPFS_DIRCOOKIE_MASK) 139d1fa59e9SXin LI 140d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 141d1fa59e9SXin LI 142d1fa59e9SXin LI /* 143d1fa59e9SXin LI * Internal representation of a tmpfs file system node. 144d1fa59e9SXin LI * 145d1fa59e9SXin LI * This structure is splitted in two parts: one holds attributes common 146d1fa59e9SXin LI * to all file types and the other holds data that is only applicable to 147d1fa59e9SXin LI * a particular type. The code must be careful to only access those 148d1fa59e9SXin LI * attributes that are actually allowed by the node's type. 149d1fa59e9SXin LI * 150d1fa59e9SXin LI * 151d1fa59e9SXin LI * Below is the key of locks used to protected the fields in the following 152d1fa59e9SXin LI * structures. 153d1fa59e9SXin LI * 154d1fa59e9SXin LI */ 155d1fa59e9SXin LI struct tmpfs_node { 156d1fa59e9SXin LI /* Doubly-linked list entry which links all existing nodes for a 157d1fa59e9SXin LI * single file system. This is provided to ease the removal of 158d1fa59e9SXin LI * all nodes during the unmount operation. */ 159d1fa59e9SXin LI LIST_ENTRY(tmpfs_node) tn_entries; 160d1fa59e9SXin LI 161d1fa59e9SXin LI /* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO', 162d1fa59e9SXin LI * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode 163d1fa59e9SXin LI * types instead of a custom enumeration is to make things simpler 164d1fa59e9SXin LI * and faster, as we do not need to convert between two types. */ 165d1fa59e9SXin LI enum vtype tn_type; 166d1fa59e9SXin LI 167d1fa59e9SXin LI /* Node identifier. */ 168d1fa59e9SXin LI ino_t tn_id; 169d1fa59e9SXin LI 170d1fa59e9SXin LI /* Node's internal status. This is used by several file system 171d1fa59e9SXin LI * operations to do modifications to the node in a delayed 172d1fa59e9SXin LI * fashion. */ 173d1fa59e9SXin LI int tn_status; 174d1fa59e9SXin LI #define TMPFS_NODE_ACCESSED (1 << 1) 175d1fa59e9SXin LI #define TMPFS_NODE_MODIFIED (1 << 2) 176d1fa59e9SXin LI #define TMPFS_NODE_CHANGED (1 << 3) 177d1fa59e9SXin LI 178d1fa59e9SXin LI /* The node size. It does not necessarily match the real amount 179d1fa59e9SXin LI * of memory consumed by it. */ 180d1fa59e9SXin LI off_t tn_size; 181d1fa59e9SXin LI 182d1fa59e9SXin LI /* Generic node attributes. */ 183d1fa59e9SXin LI uid_t tn_uid; 184d1fa59e9SXin LI gid_t tn_gid; 185d1fa59e9SXin LI mode_t tn_mode; 186d1fa59e9SXin LI int tn_flags; 187d1fa59e9SXin LI nlink_t tn_links; 188d1fa59e9SXin LI struct timespec tn_atime; 189d1fa59e9SXin LI struct timespec tn_mtime; 190d1fa59e9SXin LI struct timespec tn_ctime; 191d1fa59e9SXin LI struct timespec tn_birthtime; 192d1fa59e9SXin LI unsigned long tn_gen; 193d1fa59e9SXin LI 194d1fa59e9SXin LI /* As there is a single vnode for each active file within the 195d1fa59e9SXin LI * system, care has to be taken to avoid allocating more than one 196d1fa59e9SXin LI * vnode per file. In order to do this, a bidirectional association 197d1fa59e9SXin LI * is kept between vnodes and nodes. 198d1fa59e9SXin LI * 199d1fa59e9SXin LI * Whenever a vnode is allocated, its v_data field is updated to 200d1fa59e9SXin LI * point to the node it references. At the same time, the node's 201d1fa59e9SXin LI * tn_vnode field is modified to point to the new vnode representing 202d1fa59e9SXin LI * it. Further attempts to allocate a vnode for this same node will 203d1fa59e9SXin LI * result in returning a new reference to the value stored in 204d1fa59e9SXin LI * tn_vnode. 205d1fa59e9SXin LI * 206d1fa59e9SXin LI * May be NULL when the node is unused (that is, no vnode has been 207d1fa59e9SXin LI * allocated for it or it has been reclaimed). */ 208d1fa59e9SXin LI struct vnode * tn_vnode; 209d1fa59e9SXin LI 210d1fa59e9SXin LI /* interlock to protect tn_vpstate */ 211d1fa59e9SXin LI struct mtx tn_interlock; 212d1fa59e9SXin LI 213d1fa59e9SXin LI /* Identify if current node has vnode assiocate with 214d1fa59e9SXin LI * or allocating vnode. 215d1fa59e9SXin LI */ 216d1fa59e9SXin LI int tn_vpstate; 217d1fa59e9SXin LI 218d1fa59e9SXin LI /* misc data field for different tn_type node */ 219d1fa59e9SXin LI union { 220d1fa59e9SXin LI /* Valid when tn_type == VBLK || tn_type == VCHR. */ 221d1fa59e9SXin LI dev_t tn_rdev; 222d1fa59e9SXin LI 223d1fa59e9SXin LI /* Valid when tn_type == VDIR. */ 224d1fa59e9SXin LI struct tn_dir { 225d1fa59e9SXin LI /* Pointer to the parent directory. The root 226d1fa59e9SXin LI * directory has a pointer to itself in this field; 227d1fa59e9SXin LI * this property identifies the root node. */ 228d1fa59e9SXin LI struct tmpfs_node * tn_parent; 229d1fa59e9SXin LI 230*4fd5efe7SGleb Kurtsou /* Head of a tree that links the contents of 231*4fd5efe7SGleb Kurtsou * the directory together. */ 232d1fa59e9SXin LI struct tmpfs_dir tn_dirhead; 233d1fa59e9SXin LI 234*4fd5efe7SGleb Kurtsou /* Head of a list the contains fake directory entries 235*4fd5efe7SGleb Kurtsou * heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD 236*4fd5efe7SGleb Kurtsou * flag. */ 237*4fd5efe7SGleb Kurtsou struct tmpfs_dir_duphead tn_dupindex; 238*4fd5efe7SGleb Kurtsou 239d1fa59e9SXin LI /* Number and pointer of the first directory entry 240d1fa59e9SXin LI * returned by the readdir operation if it were 241d1fa59e9SXin LI * called again to continue reading data from the 242d1fa59e9SXin LI * same directory as before. This is used to speed 243d1fa59e9SXin LI * up reads of long directories, assuming that no 244d1fa59e9SXin LI * more than one read is in progress at a given time. 245*4fd5efe7SGleb Kurtsou * Otherwise, these values are discarded. */ 246d1fa59e9SXin LI off_t tn_readdir_lastn; 247d1fa59e9SXin LI struct tmpfs_dirent * tn_readdir_lastp; 248d1fa59e9SXin LI } tn_dir; 249d1fa59e9SXin LI 250d1fa59e9SXin LI /* Valid when tn_type == VLNK. */ 251d1fa59e9SXin LI /* The link's target, allocated from a string pool. */ 252d1fa59e9SXin LI char * tn_link; 253d1fa59e9SXin LI 254d1fa59e9SXin LI /* Valid when tn_type == VREG. */ 255d1fa59e9SXin LI struct tn_reg { 256d1fa59e9SXin LI /* The contents of regular files stored in a tmpfs 257d1fa59e9SXin LI * file system are represented by a single anonymous 258d1fa59e9SXin LI * memory object (aobj, for short). The aobj provides 259d1fa59e9SXin LI * direct access to any position within the file, 260d1fa59e9SXin LI * because its contents are always mapped in a 261d1fa59e9SXin LI * contiguous region of virtual memory. It is a task 262d1fa59e9SXin LI * of the memory management subsystem (see uvm(9)) to 263d1fa59e9SXin LI * issue the required page ins or page outs whenever 264d1fa59e9SXin LI * a position within the file is accessed. */ 265d1fa59e9SXin LI vm_object_t tn_aobj; 266d1fa59e9SXin LI 267d1fa59e9SXin LI }tn_reg; 268d1fa59e9SXin LI 269d1fa59e9SXin LI /* Valid when tn_type = VFIFO */ 270d1fa59e9SXin LI struct tn_fifo { 271d1fa59e9SXin LI fo_rdwr_t *tn_fo_read; 272d1fa59e9SXin LI fo_rdwr_t *tn_fo_write; 273d1fa59e9SXin LI }tn_fifo; 274d1fa59e9SXin LI }tn_spec; 275d1fa59e9SXin LI }; 276d1fa59e9SXin LI LIST_HEAD(tmpfs_node_list, tmpfs_node); 277d1fa59e9SXin LI 278d1fa59e9SXin LI #define tn_rdev tn_spec.tn_rdev 279d1fa59e9SXin LI #define tn_dir tn_spec.tn_dir 280d1fa59e9SXin LI #define tn_link tn_spec.tn_link 281d1fa59e9SXin LI #define tn_reg tn_spec.tn_reg 282d1fa59e9SXin LI #define tn_fifo tn_spec.tn_fifo 283d1fa59e9SXin LI 284d1fa59e9SXin LI #define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock) 285d1fa59e9SXin LI #define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock) 286fb755714SXin LI #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock) 287d1fa59e9SXin LI 28882cf92d4SXin LI #ifdef INVARIANTS 28982cf92d4SXin LI #define TMPFS_ASSERT_LOCKED(node) do { \ 29082cf92d4SXin LI MPASS(node != NULL); \ 29182cf92d4SXin LI MPASS(node->tn_vnode != NULL); \ 29282cf92d4SXin LI if (!VOP_ISLOCKED(node->tn_vnode) && \ 29382cf92d4SXin LI !mtx_owned(TMPFS_NODE_MTX(node))) \ 29482cf92d4SXin LI panic("tmpfs: node is not locked: %p", node); \ 29582cf92d4SXin LI } while (0) 29682cf92d4SXin LI #define TMPFS_ASSERT_ELOCKED(node) do { \ 29782cf92d4SXin LI MPASS((node) != NULL); \ 29882cf92d4SXin LI MPASS((node)->tn_vnode != NULL); \ 29982cf92d4SXin LI mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED); \ 30082cf92d4SXin LI ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs"); \ 30182cf92d4SXin LI } while (0) 30282cf92d4SXin LI #else 30382cf92d4SXin LI #define TMPFS_ASSERT_LOCKED(node) (void)0 30482cf92d4SXin LI #define TMPFS_ASSERT_ELOCKED(node) (void)0 30582cf92d4SXin LI #endif 30682cf92d4SXin LI 307d1fa59e9SXin LI #define TMPFS_VNODE_ALLOCATING 1 308d1fa59e9SXin LI #define TMPFS_VNODE_WANT 2 30982cf92d4SXin LI #define TMPFS_VNODE_DOOMED 4 310d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 311d1fa59e9SXin LI 312d1fa59e9SXin LI /* 313d1fa59e9SXin LI * Internal representation of a tmpfs mount point. 314d1fa59e9SXin LI */ 315d1fa59e9SXin LI struct tmpfs_mount { 316d1fa59e9SXin LI /* Maximum number of memory pages available for use by the file 317d1fa59e9SXin LI * system, set during mount time. This variable must never be 318974fd8c6SXin LI * used directly as it may be bigger than the current amount of 319d1fa59e9SXin LI * free memory; in the extreme case, it will hold the SIZE_MAX 320da7aa277SGleb Kurtsou * value. */ 321d1fa59e9SXin LI size_t tm_pages_max; 322d1fa59e9SXin LI 323da7aa277SGleb Kurtsou /* Number of pages in use by the file system. */ 324d1fa59e9SXin LI size_t tm_pages_used; 325d1fa59e9SXin LI 326d1fa59e9SXin LI /* Pointer to the node representing the root directory of this 327d1fa59e9SXin LI * file system. */ 328d1fa59e9SXin LI struct tmpfs_node * tm_root; 329d1fa59e9SXin LI 330d1fa59e9SXin LI /* Maximum number of possible nodes for this file system; set 331d1fa59e9SXin LI * during mount time. We need a hard limit on the maximum number 332d1fa59e9SXin LI * of nodes to avoid allocating too much of them; their objects 333d1fa59e9SXin LI * cannot be released until the file system is unmounted. 334d1fa59e9SXin LI * Otherwise, we could easily run out of memory by creating lots 335d1fa59e9SXin LI * of empty files and then simply removing them. */ 336d1fa59e9SXin LI ino_t tm_nodes_max; 337d1fa59e9SXin LI 3388d9a89a3SXin LI /* unrhdr used to allocate inode numbers */ 3398d9a89a3SXin LI struct unrhdr * tm_ino_unr; 340d1fa59e9SXin LI 341d1fa59e9SXin LI /* Number of nodes currently that are in use. */ 342d1fa59e9SXin LI ino_t tm_nodes_inuse; 343d1fa59e9SXin LI 344d1fa59e9SXin LI /* maximum representable file size */ 345d1fa59e9SXin LI u_int64_t tm_maxfilesize; 346d1fa59e9SXin LI 347d1fa59e9SXin LI /* Nodes are organized in two different lists. The used list 348d1fa59e9SXin LI * contains all nodes that are currently used by the file system; 349d1fa59e9SXin LI * i.e., they refer to existing files. The available list contains 350d1fa59e9SXin LI * all nodes that are currently available for use by new files. 351d1fa59e9SXin LI * Nodes must be kept in this list (instead of deleting them) 352d1fa59e9SXin LI * because we need to keep track of their generation number (tn_gen 353d1fa59e9SXin LI * field). 354d1fa59e9SXin LI * 355d1fa59e9SXin LI * Note that nodes are lazily allocated: if the available list is 356d1fa59e9SXin LI * empty and we have enough space to create more nodes, they will be 357d1fa59e9SXin LI * created and inserted in the used list. Once these are released, 358d1fa59e9SXin LI * they will go into the available list, remaining alive until the 359d1fa59e9SXin LI * file system is unmounted. */ 360d1fa59e9SXin LI struct tmpfs_node_list tm_nodes_used; 361d1fa59e9SXin LI 362d1fa59e9SXin LI /* All node lock to protect the node list and tmp_pages_used */ 363d1fa59e9SXin LI struct mtx allnode_lock; 364d1fa59e9SXin LI 365d1fa59e9SXin LI /* Pools used to store file system meta data. These are not shared 366d1fa59e9SXin LI * across several instances of tmpfs for the reasons described in 367d1fa59e9SXin LI * tmpfs_pool.c. */ 368d1fa59e9SXin LI uma_zone_t tm_dirent_pool; 369d1fa59e9SXin LI uma_zone_t tm_node_pool; 370c5ab5ce3SJaakko Heinonen 371c5ab5ce3SJaakko Heinonen /* Read-only status. */ 372c5ab5ce3SJaakko Heinonen int tm_ronly; 373d1fa59e9SXin LI }; 374d1fa59e9SXin LI #define TMPFS_LOCK(tm) mtx_lock(&(tm)->allnode_lock) 375d1fa59e9SXin LI #define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->allnode_lock) 376d1fa59e9SXin LI 377d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 378d1fa59e9SXin LI 379d1fa59e9SXin LI /* 380d1fa59e9SXin LI * This structure maps a file identifier to a tmpfs node. Used by the 381d1fa59e9SXin LI * NFS code. 382d1fa59e9SXin LI */ 383d1fa59e9SXin LI struct tmpfs_fid { 384d1fa59e9SXin LI uint16_t tf_len; 385d1fa59e9SXin LI uint16_t tf_pad; 386d1fa59e9SXin LI ino_t tf_id; 3875ff9b915SXin LI unsigned long tf_gen; 388d1fa59e9SXin LI }; 389d1fa59e9SXin LI 390d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 391d1fa59e9SXin LI 392d1fa59e9SXin LI #ifdef _KERNEL 393d1fa59e9SXin LI /* 394d1fa59e9SXin LI * Prototypes for tmpfs_subr.c. 395d1fa59e9SXin LI */ 396d1fa59e9SXin LI 397d1fa59e9SXin LI int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype, 398d1fa59e9SXin LI uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *, 399dfd233edSAttilio Rao char *, dev_t, struct tmpfs_node **); 400d1fa59e9SXin LI void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *); 401d1fa59e9SXin LI int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *, 402*4fd5efe7SGleb Kurtsou const char *, u_int, struct tmpfs_dirent **); 403*4fd5efe7SGleb Kurtsou void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *); 404*4fd5efe7SGleb Kurtsou void tmpfs_dirent_init(struct tmpfs_dirent *, const char *, u_int); 4050ae6383dSXin LI int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int, 406dfd233edSAttilio Rao struct vnode **); 407d1fa59e9SXin LI void tmpfs_free_vp(struct vnode *); 408d1fa59e9SXin LI int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *, 409d1fa59e9SXin LI struct componentname *, char *); 410d1fa59e9SXin LI void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *); 411d1fa59e9SXin LI void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *); 412*4fd5efe7SGleb Kurtsou void tmpfs_dir_destroy(struct tmpfs_mount *, struct tmpfs_node *); 413d1fa59e9SXin LI struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node, 414e3c7e753SKonstantin Belousov struct tmpfs_node *f, 415d1fa59e9SXin LI struct componentname *cnp); 416*4fd5efe7SGleb Kurtsou int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, int, 417*4fd5efe7SGleb Kurtsou u_long *, int *); 41899d57a6bSEd Schouten int tmpfs_dir_whiteout_add(struct vnode *, struct componentname *); 41999d57a6bSEd Schouten void tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *); 4200b05cac3SAlan Cox int tmpfs_reg_resize(struct vnode *, off_t, boolean_t); 421d1fa59e9SXin LI int tmpfs_chflags(struct vnode *, int, struct ucred *, struct thread *); 422d1fa59e9SXin LI int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *); 423d1fa59e9SXin LI int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *, 424d1fa59e9SXin LI struct thread *); 425d1fa59e9SXin LI int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *); 426d1fa59e9SXin LI int tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *, 427d1fa59e9SXin LI struct timespec *, int, struct ucred *, struct thread *); 428d1fa59e9SXin LI void tmpfs_itimes(struct vnode *, const struct timespec *, 429d1fa59e9SXin LI const struct timespec *); 430d1fa59e9SXin LI 431d1fa59e9SXin LI void tmpfs_update(struct vnode *); 432d1fa59e9SXin LI int tmpfs_truncate(struct vnode *, off_t); 433d1fa59e9SXin LI 434d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 435d1fa59e9SXin LI 436d1fa59e9SXin LI /* 437d1fa59e9SXin LI * Convenience macros to simplify some logical expressions. 438d1fa59e9SXin LI */ 439d1fa59e9SXin LI #define IMPLIES(a, b) (!(a) || (b)) 440d1fa59e9SXin LI #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a)) 441d1fa59e9SXin LI 442d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 443d1fa59e9SXin LI 444d1fa59e9SXin LI /* 445d1fa59e9SXin LI * Checks that the directory entry pointed by 'de' matches the name 'name' 446d1fa59e9SXin LI * with a length of 'len'. 447d1fa59e9SXin LI */ 448d1fa59e9SXin LI #define TMPFS_DIRENT_MATCHES(de, name, len) \ 449*4fd5efe7SGleb Kurtsou (de->td_namelen == len && \ 450*4fd5efe7SGleb Kurtsou bcmp((de)->ud.td_name, (name), (de)->td_namelen) == 0) 451d1fa59e9SXin LI 452d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 453d1fa59e9SXin LI 454d1fa59e9SXin LI /* 455d1fa59e9SXin LI * Ensures that the node pointed by 'node' is a directory and that its 456d1fa59e9SXin LI * contents are consistent with respect to directories. 457d1fa59e9SXin LI */ 458*4fd5efe7SGleb Kurtsou #define TMPFS_VALIDATE_DIR(node) do { \ 459d1fa59e9SXin LI MPASS((node)->tn_type == VDIR); \ 460d1fa59e9SXin LI MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \ 461*4fd5efe7SGleb Kurtsou } while (0) 462d1fa59e9SXin LI 463d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 464d1fa59e9SXin LI 465d1fa59e9SXin LI /* 466d1fa59e9SXin LI * Memory management stuff. 467d1fa59e9SXin LI */ 468d1fa59e9SXin LI 469d1fa59e9SXin LI /* 470da7aa277SGleb Kurtsou * Amount of memory pages to reserve for the system (e.g., to not use by 471da7aa277SGleb Kurtsou * tmpfs). 472d1fa59e9SXin LI */ 473da7aa277SGleb Kurtsou #define TMPFS_PAGES_MINRESERVED (4 * 1024 * 1024 / PAGE_SIZE) 474d1fa59e9SXin LI 475da7aa277SGleb Kurtsou size_t tmpfs_mem_avail(void); 476d1fa59e9SXin LI 477da7aa277SGleb Kurtsou size_t tmpfs_pages_used(struct tmpfs_mount *tmp); 478d1fa59e9SXin LI 479d1fa59e9SXin LI #endif 480d1fa59e9SXin LI 481d1fa59e9SXin LI /* --------------------------------------------------------------------- */ 482d1fa59e9SXin LI 483d1fa59e9SXin LI /* 484d1fa59e9SXin LI * Macros/functions to convert from generic data structures to tmpfs 485d1fa59e9SXin LI * specific ones. 486d1fa59e9SXin LI */ 487d1fa59e9SXin LI 488d1fa59e9SXin LI static inline 489d1fa59e9SXin LI struct tmpfs_mount * 490d1fa59e9SXin LI VFS_TO_TMPFS(struct mount *mp) 491d1fa59e9SXin LI { 492d1fa59e9SXin LI struct tmpfs_mount *tmp; 493d1fa59e9SXin LI 494d1fa59e9SXin LI MPASS((mp) != NULL && (mp)->mnt_data != NULL); 495d1fa59e9SXin LI tmp = (struct tmpfs_mount *)(mp)->mnt_data; 496d1fa59e9SXin LI return tmp; 497d1fa59e9SXin LI } 498d1fa59e9SXin LI 499d1fa59e9SXin LI static inline 500d1fa59e9SXin LI struct tmpfs_node * 501d1fa59e9SXin LI VP_TO_TMPFS_NODE(struct vnode *vp) 502d1fa59e9SXin LI { 503d1fa59e9SXin LI struct tmpfs_node *node; 504d1fa59e9SXin LI 505d1fa59e9SXin LI MPASS((vp) != NULL && (vp)->v_data != NULL); 506d1fa59e9SXin LI node = (struct tmpfs_node *)vp->v_data; 507d1fa59e9SXin LI return node; 508d1fa59e9SXin LI } 509d1fa59e9SXin LI 510d1fa59e9SXin LI static inline 511d1fa59e9SXin LI struct tmpfs_node * 512d1fa59e9SXin LI VP_TO_TMPFS_DIR(struct vnode *vp) 513d1fa59e9SXin LI { 514d1fa59e9SXin LI struct tmpfs_node *node; 515d1fa59e9SXin LI 516d1fa59e9SXin LI node = VP_TO_TMPFS_NODE(vp); 517d1fa59e9SXin LI TMPFS_VALIDATE_DIR(node); 518d1fa59e9SXin LI return node; 519d1fa59e9SXin LI } 520d1fa59e9SXin LI 521d1fa59e9SXin LI #endif /* _FS_TMPFS_TMPFS_H_ */ 522