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 #include <sys/dirent.h> 39d1fa59e9SXin LI #include <sys/mount.h> 40d1fa59e9SXin LI #include <sys/queue.h> 41d1fa59e9SXin LI #include <sys/vnode.h> 42d1fa59e9SXin LI #include <sys/file.h> 43d1fa59e9SXin LI #include <sys/lock.h> 44d1fa59e9SXin LI #include <sys/mutex.h> 45d1fa59e9SXin LI 46d1fa59e9SXin LI #include <sys/malloc.h> 47d1fa59e9SXin LI #include <sys/systm.h> 484fd5efe7SGleb Kurtsou #include <sys/tree.h> 49d1fa59e9SXin LI #include <sys/vmmeter.h> 50d1fa59e9SXin LI #include <vm/swap_pager.h> 51d1fa59e9SXin LI 52d1fa59e9SXin LI MALLOC_DECLARE(M_TMPFSMNT); 539b258fcaSXin LI MALLOC_DECLARE(M_TMPFSNAME); 54d1fa59e9SXin LI 55d1fa59e9SXin LI /* 56d1fa59e9SXin LI * Internal representation of a tmpfs directory entry. 57d1fa59e9SXin LI */ 584fd5efe7SGleb Kurtsou 594fd5efe7SGleb Kurtsou LIST_HEAD(tmpfs_dir_duphead, tmpfs_dirent); 604fd5efe7SGleb Kurtsou 61d1fa59e9SXin LI struct tmpfs_dirent { 624fd5efe7SGleb Kurtsou /* 634fd5efe7SGleb Kurtsou * Depending on td_cookie flag entry can be of 3 types: 644fd5efe7SGleb Kurtsou * - regular -- no hash collisions, stored in RB-Tree 654fd5efe7SGleb Kurtsou * - duphead -- synthetic linked list head for dup entries 664fd5efe7SGleb Kurtsou * - dup -- stored in linked list instead of RB-Tree 674fd5efe7SGleb Kurtsou */ 684fd5efe7SGleb Kurtsou union { 694fd5efe7SGleb Kurtsou /* regular and duphead entry types */ 704fd5efe7SGleb Kurtsou RB_ENTRY(tmpfs_dirent) td_entries; 71d1fa59e9SXin LI 724fd5efe7SGleb Kurtsou /* dup entry type */ 734fd5efe7SGleb Kurtsou struct { 744fd5efe7SGleb Kurtsou LIST_ENTRY(tmpfs_dirent) entries; 754fd5efe7SGleb Kurtsou LIST_ENTRY(tmpfs_dirent) index_entries; 764fd5efe7SGleb Kurtsou } td_dup; 774fd5efe7SGleb Kurtsou } uh; 78d1fa59e9SXin LI 794fd5efe7SGleb Kurtsou uint32_t td_cookie; 804fd5efe7SGleb Kurtsou uint32_t td_hash; 814fd5efe7SGleb Kurtsou u_int td_namelen; 82d1fa59e9SXin LI 83*bba7ed20SKonstantin Belousov /* 84*bba7ed20SKonstantin Belousov * Pointer to the node this entry refers to. In case this field 85*bba7ed20SKonstantin Belousov * is NULL, the node is a whiteout. 86*bba7ed20SKonstantin Belousov */ 87d1fa59e9SXin LI struct tmpfs_node * td_node; 884fd5efe7SGleb Kurtsou 894fd5efe7SGleb Kurtsou union { 904fd5efe7SGleb Kurtsou /* 914fd5efe7SGleb Kurtsou * The name of the entry, allocated from a string pool. This 924fd5efe7SGleb Kurtsou * string is not required to be zero-terminated. 934fd5efe7SGleb Kurtsou */ 944fd5efe7SGleb Kurtsou char * td_name; /* regular, dup */ 954fd5efe7SGleb Kurtsou struct tmpfs_dir_duphead td_duphead; /* duphead */ 964fd5efe7SGleb Kurtsou } ud; 97d1fa59e9SXin LI }; 98d1fa59e9SXin LI 99*bba7ed20SKonstantin Belousov /* 100*bba7ed20SKonstantin Belousov * A directory in tmpfs holds a collection of directory entries, which 101*bba7ed20SKonstantin Belousov * in turn point to other files (which can be directories themselves). 102d1fa59e9SXin LI * 103*bba7ed20SKonstantin Belousov * In tmpfs, this collection is managed by a RB-Tree, whose head is 104*bba7ed20SKonstantin Belousov * defined by the struct tmpfs_dir type. 105d1fa59e9SXin LI * 1064fd5efe7SGleb Kurtsou * It is important to notice that directories do not have entries for . and 107d1fa59e9SXin LI * .. as other file systems do. These can be generated when requested 108d1fa59e9SXin LI * based on information available by other means, such as the pointer to 109d1fa59e9SXin LI * the node itself in the former case or the pointer to the parent directory 110d1fa59e9SXin LI * in the latter case. This is done to simplify tmpfs's code and, more 111*bba7ed20SKonstantin Belousov * importantly, to remove redundancy. 112*bba7ed20SKonstantin Belousov */ 1134fd5efe7SGleb Kurtsou RB_HEAD(tmpfs_dir, tmpfs_dirent); 114d1fa59e9SXin LI 115*bba7ed20SKonstantin Belousov /* 116*bba7ed20SKonstantin Belousov * Each entry in a directory has a cookie that identifies it. Cookies 117ad3638eeSXin LI * supersede offsets within directories because, given how tmpfs stores 1184fd5efe7SGleb Kurtsou * directories in memory, there is no such thing as an offset. 119ad3638eeSXin LI * 120ad3638eeSXin LI * The '.', '..' and the end of directory markers have fixed cookies which 121ad3638eeSXin LI * cannot collide with the cookies generated by other entries. The cookies 1224fd5efe7SGleb Kurtsou * for the other entries are generated based on the file name hash value or 1234fd5efe7SGleb Kurtsou * unique number in case of name hash collision. 124ad3638eeSXin LI * 1254fd5efe7SGleb Kurtsou * To preserve compatibility cookies are limited to 31 bits. 1264fd5efe7SGleb Kurtsou */ 1274fd5efe7SGleb Kurtsou 128d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_DOT 0 129d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_DOTDOT 1 130d1fa59e9SXin LI #define TMPFS_DIRCOOKIE_EOF 2 1314fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_MASK ((off_t)0x3fffffffU) 1324fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_MIN ((off_t)0x00000004U) 1334fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUP ((off_t)0x40000000U) 1344fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUPHEAD ((off_t)0x80000000U) 1354fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUP_MIN TMPFS_DIRCOOKIE_DUP 1364fd5efe7SGleb Kurtsou #define TMPFS_DIRCOOKIE_DUP_MAX \ 1374fd5efe7SGleb Kurtsou (TMPFS_DIRCOOKIE_DUP | TMPFS_DIRCOOKIE_MASK) 138d1fa59e9SXin LI 139d1fa59e9SXin LI /* 140d1fa59e9SXin LI * Internal representation of a tmpfs file system node. 141d1fa59e9SXin LI * 142d1fa59e9SXin LI * This structure is splitted in two parts: one holds attributes common 143d1fa59e9SXin LI * to all file types and the other holds data that is only applicable to 144d1fa59e9SXin LI * a particular type. The code must be careful to only access those 145d1fa59e9SXin LI * attributes that are actually allowed by the node's type. 146d1fa59e9SXin LI * 147d1fa59e9SXin LI * Below is the key of locks used to protected the fields in the following 148d1fa59e9SXin LI * structures. 149*bba7ed20SKonstantin Belousov * (v) vnode lock in exclusive mode 150*bba7ed20SKonstantin Belousov * (vi) vnode lock in exclusive mode, or vnode lock in shared vnode and 151*bba7ed20SKonstantin Belousov * tn_interlock 152*bba7ed20SKonstantin Belousov * (i) tn_interlock 153*bba7ed20SKonstantin Belousov * (m) tmpfs_mount allnode_lock 154*bba7ed20SKonstantin Belousov * (c) stable after creation 155d1fa59e9SXin LI */ 156d1fa59e9SXin LI struct tmpfs_node { 157*bba7ed20SKonstantin Belousov /* 158*bba7ed20SKonstantin Belousov * Doubly-linked list entry which links all existing nodes for 159*bba7ed20SKonstantin Belousov * a single file system. This is provided to ease the removal 160*bba7ed20SKonstantin Belousov * of all nodes during the unmount operation, and to support 161*bba7ed20SKonstantin Belousov * the implementation of VOP_VNTOCNP(). 162*bba7ed20SKonstantin Belousov */ 163*bba7ed20SKonstantin Belousov LIST_ENTRY(tmpfs_node) tn_entries; /* (m) */ 164d1fa59e9SXin LI 165*bba7ed20SKonstantin Belousov /* 166*bba7ed20SKonstantin Belousov * The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO', 167d1fa59e9SXin LI * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode 168d1fa59e9SXin LI * types instead of a custom enumeration is to make things simpler 169*bba7ed20SKonstantin Belousov * and faster, as we do not need to convert between two types. 170*bba7ed20SKonstantin Belousov */ 171*bba7ed20SKonstantin Belousov enum vtype tn_type; /* (c) */ 172d1fa59e9SXin LI 173d1fa59e9SXin LI /* Node identifier. */ 174*bba7ed20SKonstantin Belousov ino_t tn_id; /* (c) */ 175d1fa59e9SXin LI 176*bba7ed20SKonstantin Belousov /* 177*bba7ed20SKonstantin Belousov * Node's internal status. This is used by several file system 178d1fa59e9SXin LI * operations to do modifications to the node in a delayed 179*bba7ed20SKonstantin Belousov * fashion. 180*bba7ed20SKonstantin Belousov */ 181*bba7ed20SKonstantin Belousov int tn_status; /* (vi) */ 182d1fa59e9SXin LI #define TMPFS_NODE_ACCESSED (1 << 1) 183d1fa59e9SXin LI #define TMPFS_NODE_MODIFIED (1 << 2) 184d1fa59e9SXin LI #define TMPFS_NODE_CHANGED (1 << 3) 185d1fa59e9SXin LI 186*bba7ed20SKonstantin Belousov /* 187*bba7ed20SKonstantin Belousov * The node size. It does not necessarily match the real amount 188*bba7ed20SKonstantin Belousov * of memory consumed by it. 189*bba7ed20SKonstantin Belousov */ 190*bba7ed20SKonstantin Belousov off_t tn_size; /* (v) */ 191d1fa59e9SXin LI 192d1fa59e9SXin LI /* Generic node attributes. */ 193*bba7ed20SKonstantin Belousov uid_t tn_uid; /* (v) */ 194*bba7ed20SKonstantin Belousov gid_t tn_gid; /* (v) */ 195*bba7ed20SKonstantin Belousov mode_t tn_mode; /* (v) */ 196*bba7ed20SKonstantin Belousov u_long tn_flags; /* (v) */ 197*bba7ed20SKonstantin Belousov nlink_t tn_links; /* (v) */ 198*bba7ed20SKonstantin Belousov struct timespec tn_atime; /* (vi) */ 199*bba7ed20SKonstantin Belousov struct timespec tn_mtime; /* (vi) */ 200*bba7ed20SKonstantin Belousov struct timespec tn_ctime; /* (vi) */ 201*bba7ed20SKonstantin Belousov struct timespec tn_birthtime; /* (v) */ 202*bba7ed20SKonstantin Belousov unsigned long tn_gen; /* (c) */ 203d1fa59e9SXin LI 204*bba7ed20SKonstantin Belousov /* 205*bba7ed20SKonstantin Belousov * As there is a single vnode for each active file within the 206d1fa59e9SXin LI * system, care has to be taken to avoid allocating more than one 207d1fa59e9SXin LI * vnode per file. In order to do this, a bidirectional association 208d1fa59e9SXin LI * is kept between vnodes and nodes. 209d1fa59e9SXin LI * 210d1fa59e9SXin LI * Whenever a vnode is allocated, its v_data field is updated to 211d1fa59e9SXin LI * point to the node it references. At the same time, the node's 212d1fa59e9SXin LI * tn_vnode field is modified to point to the new vnode representing 213d1fa59e9SXin LI * it. Further attempts to allocate a vnode for this same node will 214d1fa59e9SXin LI * result in returning a new reference to the value stored in 215d1fa59e9SXin LI * tn_vnode. 216d1fa59e9SXin LI * 217d1fa59e9SXin LI * May be NULL when the node is unused (that is, no vnode has been 218*bba7ed20SKonstantin Belousov * allocated for it or it has been reclaimed). 219*bba7ed20SKonstantin Belousov */ 220*bba7ed20SKonstantin Belousov struct vnode * tn_vnode; /* (i) */ 221d1fa59e9SXin LI 222*bba7ed20SKonstantin Belousov /* 223*bba7ed20SKonstantin Belousov * Interlock to protect tn_vpstate, and tn_status under shared 2245dc11286SKonstantin Belousov * vnode lock. 2255dc11286SKonstantin Belousov */ 226d1fa59e9SXin LI struct mtx tn_interlock; 227d1fa59e9SXin LI 228*bba7ed20SKonstantin Belousov /* 229*bba7ed20SKonstantin Belousov * Identify if current node has vnode assiocate with 230d1fa59e9SXin LI * or allocating vnode. 231d1fa59e9SXin LI */ 232*bba7ed20SKonstantin Belousov int tn_vpstate; /* (i) */ 233d1fa59e9SXin LI 234d1fa59e9SXin LI /* misc data field for different tn_type node */ 235d1fa59e9SXin LI union { 236d1fa59e9SXin LI /* Valid when tn_type == VBLK || tn_type == VCHR. */ 237*bba7ed20SKonstantin Belousov dev_t tn_rdev; /* (c) */ 238d1fa59e9SXin LI 239d1fa59e9SXin LI /* Valid when tn_type == VDIR. */ 240d1fa59e9SXin LI struct tn_dir { 241*bba7ed20SKonstantin Belousov /* 242*bba7ed20SKonstantin Belousov * Pointer to the parent directory. The root 243d1fa59e9SXin LI * directory has a pointer to itself in this field; 244*bba7ed20SKonstantin Belousov * this property identifies the root node. 245*bba7ed20SKonstantin Belousov */ 246d1fa59e9SXin LI struct tmpfs_node * tn_parent; 247d1fa59e9SXin LI 248*bba7ed20SKonstantin Belousov /* 249*bba7ed20SKonstantin Belousov * Head of a tree that links the contents of 250*bba7ed20SKonstantin Belousov * the directory together. 251*bba7ed20SKonstantin Belousov */ 252d1fa59e9SXin LI struct tmpfs_dir tn_dirhead; 253d1fa59e9SXin LI 254*bba7ed20SKonstantin Belousov /* 255*bba7ed20SKonstantin Belousov * Head of a list the contains fake directory entries 2564fd5efe7SGleb Kurtsou * heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD 257*bba7ed20SKonstantin Belousov * flag. 258*bba7ed20SKonstantin Belousov */ 2594fd5efe7SGleb Kurtsou struct tmpfs_dir_duphead tn_dupindex; 2604fd5efe7SGleb Kurtsou 261*bba7ed20SKonstantin Belousov /* 262*bba7ed20SKonstantin Belousov * Number and pointer of the first directory entry 263d1fa59e9SXin LI * returned by the readdir operation if it were 264d1fa59e9SXin LI * called again to continue reading data from the 265d1fa59e9SXin LI * same directory as before. This is used to speed 266d1fa59e9SXin LI * up reads of long directories, assuming that no 267d1fa59e9SXin LI * more than one read is in progress at a given time. 268*bba7ed20SKonstantin Belousov * Otherwise, these values are discarded. 269*bba7ed20SKonstantin Belousov */ 270d1fa59e9SXin LI off_t tn_readdir_lastn; 271d1fa59e9SXin LI struct tmpfs_dirent * tn_readdir_lastp; 272d1fa59e9SXin LI } tn_dir; 273d1fa59e9SXin LI 274d1fa59e9SXin LI /* Valid when tn_type == VLNK. */ 275d1fa59e9SXin LI /* The link's target, allocated from a string pool. */ 276*bba7ed20SKonstantin Belousov char * tn_link; /* (c) */ 277d1fa59e9SXin LI 278d1fa59e9SXin LI /* Valid when tn_type == VREG. */ 279d1fa59e9SXin LI struct tn_reg { 280*bba7ed20SKonstantin Belousov /* 281*bba7ed20SKonstantin Belousov * The contents of regular files stored in a 282*bba7ed20SKonstantin Belousov * tmpfs file system are represented by a 283*bba7ed20SKonstantin Belousov * single anonymous memory object (aobj, for 284*bba7ed20SKonstantin Belousov * short). The aobj provides direct access to 285*bba7ed20SKonstantin Belousov * any position within the file. It is a task 286*bba7ed20SKonstantin Belousov * of the memory management subsystem to issue 287*bba7ed20SKonstantin Belousov * the required page ins or page outs whenever 288*bba7ed20SKonstantin Belousov * a position within the file is accessed. 289*bba7ed20SKonstantin Belousov */ 290*bba7ed20SKonstantin Belousov vm_object_t tn_aobj; /* (c) */ 291d1fa59e9SXin LI } tn_reg; 292*bba7ed20SKonstantin Belousov } tn_spec; /* (v) */ 293d1fa59e9SXin LI }; 294d1fa59e9SXin LI LIST_HEAD(tmpfs_node_list, tmpfs_node); 295d1fa59e9SXin LI 296d1fa59e9SXin LI #define tn_rdev tn_spec.tn_rdev 297d1fa59e9SXin LI #define tn_dir tn_spec.tn_dir 298d1fa59e9SXin LI #define tn_link tn_spec.tn_link 299d1fa59e9SXin LI #define tn_reg tn_spec.tn_reg 300d1fa59e9SXin LI #define tn_fifo tn_spec.tn_fifo 301d1fa59e9SXin LI 302d1fa59e9SXin LI #define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock) 303d1fa59e9SXin LI #define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock) 304fb755714SXin LI #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock) 305d2ca06cdSKonstantin Belousov #define TMPFS_NODE_ASSERT_LOCKED(node) mtx_assert(TMPFS_NODE_MTX(node), \ 306d2ca06cdSKonstantin Belousov MA_OWNED) 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 3308239a7a8SKonstantin Belousov #define TMPFS_VNODE_WRECLAIM 8 331d1fa59e9SXin LI 332d1fa59e9SXin LI /* 333d1fa59e9SXin LI * Internal representation of a tmpfs mount point. 334d1fa59e9SXin LI */ 335d1fa59e9SXin LI struct tmpfs_mount { 336*bba7ed20SKonstantin Belousov /* 337*bba7ed20SKonstantin Belousov * Maximum number of memory pages available for use by the file 338d1fa59e9SXin LI * system, set during mount time. This variable must never be 339974fd8c6SXin LI * used directly as it may be bigger than the current amount of 340ed2159c9SMateusz Guzik * free memory; in the extreme case, it will hold the ULONG_MAX 341*bba7ed20SKonstantin Belousov * value. 342*bba7ed20SKonstantin Belousov */ 343ed2159c9SMateusz Guzik u_long tm_pages_max; 344d1fa59e9SXin LI 345da7aa277SGleb Kurtsou /* Number of pages in use by the file system. */ 346ed2159c9SMateusz Guzik u_long tm_pages_used; 347d1fa59e9SXin LI 348*bba7ed20SKonstantin Belousov /* 349*bba7ed20SKonstantin Belousov * Pointer to the node representing the root directory of this 350*bba7ed20SKonstantin Belousov * file system. 351*bba7ed20SKonstantin Belousov */ 352d1fa59e9SXin LI struct tmpfs_node * tm_root; 353d1fa59e9SXin LI 354*bba7ed20SKonstantin Belousov struct mount * tm_mnt; 355*bba7ed20SKonstantin Belousov 356*bba7ed20SKonstantin Belousov /* 357*bba7ed20SKonstantin Belousov * Maximum number of possible nodes for this file system; set 358d1fa59e9SXin LI * during mount time. We need a hard limit on the maximum number 359d1fa59e9SXin LI * of nodes to avoid allocating too much of them; their objects 360d1fa59e9SXin LI * cannot be released until the file system is unmounted. 361d1fa59e9SXin LI * Otherwise, we could easily run out of memory by creating lots 362*bba7ed20SKonstantin Belousov * of empty files and then simply removing them. 363*bba7ed20SKonstantin Belousov */ 364d1fa59e9SXin LI ino_t tm_nodes_max; 365d1fa59e9SXin LI 3668d9a89a3SXin LI /* unrhdr used to allocate inode numbers */ 3678d9a89a3SXin LI struct unrhdr * tm_ino_unr; 368d1fa59e9SXin LI 369d1fa59e9SXin LI /* Number of nodes currently that are in use. */ 370d1fa59e9SXin LI ino_t tm_nodes_inuse; 371d1fa59e9SXin LI 372d1fa59e9SXin LI /* maximum representable file size */ 373d1fa59e9SXin LI u_int64_t tm_maxfilesize; 374d1fa59e9SXin LI 375*bba7ed20SKonstantin Belousov /* 376*bba7ed20SKonstantin Belousov * The used list contains all nodes that are currently used by 377*bba7ed20SKonstantin Belousov * the file system; i.e., they refer to existing files. 378*bba7ed20SKonstantin Belousov */ 379d1fa59e9SXin LI struct tmpfs_node_list tm_nodes_used; 380d1fa59e9SXin LI 381*bba7ed20SKonstantin Belousov /* All node lock to protect the node list and tmp_pages_used. */ 382d1fa59e9SXin LI struct mtx allnode_lock; 383d1fa59e9SXin LI 384*bba7ed20SKonstantin Belousov /* Zones used to store file system meta data, per tmpfs mount. */ 385d1fa59e9SXin LI uma_zone_t tm_dirent_pool; 386d1fa59e9SXin LI uma_zone_t tm_node_pool; 387c5ab5ce3SJaakko Heinonen 388c5ab5ce3SJaakko Heinonen /* Read-only status. */ 389c5ab5ce3SJaakko Heinonen int tm_ronly; 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 * This structure maps a file identifier to a tmpfs node. Used by the 396d1fa59e9SXin LI * NFS code. 397d1fa59e9SXin LI */ 398d1fa59e9SXin LI struct tmpfs_fid { 399d1fa59e9SXin LI uint16_t tf_len; 400d1fa59e9SXin LI uint16_t tf_pad; 401d1fa59e9SXin LI ino_t tf_id; 4025ff9b915SXin LI unsigned long tf_gen; 403d1fa59e9SXin LI }; 404d1fa59e9SXin LI 405d1fa59e9SXin LI #ifdef _KERNEL 406d1fa59e9SXin LI /* 407d1fa59e9SXin LI * Prototypes for tmpfs_subr.c. 408d1fa59e9SXin LI */ 409d1fa59e9SXin LI 4104cda7f7eSKonstantin Belousov int tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *, enum vtype, 411d1fa59e9SXin LI uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *, 412dfd233edSAttilio Rao char *, dev_t, struct tmpfs_node **); 413d1fa59e9SXin LI void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *); 414d1fa59e9SXin LI int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *, 4154fd5efe7SGleb Kurtsou const char *, u_int, struct tmpfs_dirent **); 4164fd5efe7SGleb Kurtsou void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *); 4174fd5efe7SGleb Kurtsou void tmpfs_dirent_init(struct tmpfs_dirent *, const char *, u_int); 418158cc900SKonstantin Belousov void tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj); 4190ae6383dSXin LI int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int, 420dfd233edSAttilio Rao struct vnode **); 421d1fa59e9SXin LI void tmpfs_free_vp(struct vnode *); 422d1fa59e9SXin LI int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *, 423d1fa59e9SXin LI struct componentname *, char *); 424f40cb1c6SKonstantin Belousov void tmpfs_check_mtime(struct vnode *); 425d1fa59e9SXin LI void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *); 426d1fa59e9SXin LI void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *); 4274fd5efe7SGleb Kurtsou void tmpfs_dir_destroy(struct tmpfs_mount *, struct tmpfs_node *); 428d1fa59e9SXin LI struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node, 429e3c7e753SKonstantin Belousov struct tmpfs_node *f, 430d1fa59e9SXin LI struct componentname *cnp); 4314fd5efe7SGleb Kurtsou int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, int, 4324fd5efe7SGleb Kurtsou u_long *, int *); 43399d57a6bSEd Schouten int tmpfs_dir_whiteout_add(struct vnode *, struct componentname *); 43499d57a6bSEd Schouten void tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *); 4350b05cac3SAlan Cox int tmpfs_reg_resize(struct vnode *, off_t, boolean_t); 436b4b2596bSPawel Jakub Dawidek int tmpfs_chflags(struct vnode *, u_long, struct ucred *, struct thread *); 437d1fa59e9SXin LI int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *); 438d1fa59e9SXin LI int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *, 439d1fa59e9SXin LI struct thread *); 440d1fa59e9SXin LI int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *); 4417b81a399SKonstantin Belousov int tmpfs_chtimes(struct vnode *, struct vattr *, struct ucred *cred, 4427b81a399SKonstantin Belousov struct thread *); 443d1fa59e9SXin LI void tmpfs_itimes(struct vnode *, const struct timespec *, 444d1fa59e9SXin LI const struct timespec *); 445d1fa59e9SXin LI 4465dc11286SKonstantin Belousov void tmpfs_set_status(struct tmpfs_node *node, int status); 447d1fa59e9SXin LI void tmpfs_update(struct vnode *); 448d1fa59e9SXin LI int tmpfs_truncate(struct vnode *, off_t); 449d1fa59e9SXin LI 450d1fa59e9SXin LI /* 451d1fa59e9SXin LI * Convenience macros to simplify some logical expressions. 452d1fa59e9SXin LI */ 453d1fa59e9SXin LI #define IMPLIES(a, b) (!(a) || (b)) 454d1fa59e9SXin LI #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a)) 455d1fa59e9SXin LI 456d1fa59e9SXin LI /* 457d1fa59e9SXin LI * Checks that the directory entry pointed by 'de' matches the name 'name' 458d1fa59e9SXin LI * with a length of 'len'. 459d1fa59e9SXin LI */ 460d1fa59e9SXin LI #define TMPFS_DIRENT_MATCHES(de, name, len) \ 4614fd5efe7SGleb Kurtsou (de->td_namelen == len && \ 4624fd5efe7SGleb Kurtsou bcmp((de)->ud.td_name, (name), (de)->td_namelen) == 0) 463d1fa59e9SXin LI 464d1fa59e9SXin LI /* 465d1fa59e9SXin LI * Ensures that the node pointed by 'node' is a directory and that its 466d1fa59e9SXin LI * contents are consistent with respect to directories. 467d1fa59e9SXin LI */ 4684fd5efe7SGleb Kurtsou #define TMPFS_VALIDATE_DIR(node) do { \ 469d1fa59e9SXin LI MPASS((node)->tn_type == VDIR); \ 470d1fa59e9SXin LI MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \ 4714fd5efe7SGleb Kurtsou } while (0) 472d1fa59e9SXin LI 473d1fa59e9SXin LI /* 474da7aa277SGleb Kurtsou * Amount of memory pages to reserve for the system (e.g., to not use by 475da7aa277SGleb Kurtsou * tmpfs). 476d1fa59e9SXin LI */ 477da7aa277SGleb Kurtsou #define TMPFS_PAGES_MINRESERVED (4 * 1024 * 1024 / PAGE_SIZE) 478d1fa59e9SXin LI 479da7aa277SGleb Kurtsou size_t tmpfs_mem_avail(void); 480d1fa59e9SXin LI 481da7aa277SGleb Kurtsou size_t tmpfs_pages_used(struct tmpfs_mount *tmp); 482d1fa59e9SXin LI 483d1fa59e9SXin LI #endif 484d1fa59e9SXin LI 485d1fa59e9SXin LI /* 486d1fa59e9SXin LI * Macros/functions to convert from generic data structures to tmpfs 487d1fa59e9SXin LI * specific ones. 488d1fa59e9SXin LI */ 489d1fa59e9SXin LI 490*bba7ed20SKonstantin Belousov static inline struct tmpfs_mount * 491d1fa59e9SXin LI VFS_TO_TMPFS(struct mount *mp) 492d1fa59e9SXin LI { 493d1fa59e9SXin LI struct tmpfs_mount *tmp; 494d1fa59e9SXin LI 495*bba7ed20SKonstantin Belousov MPASS(mp != NULL && mp->mnt_data != NULL); 496*bba7ed20SKonstantin Belousov tmp = (struct tmpfs_mount *)mp->mnt_data; 497*bba7ed20SKonstantin Belousov return (tmp); 498d1fa59e9SXin LI } 499d1fa59e9SXin LI 500*bba7ed20SKonstantin Belousov static inline struct tmpfs_node * 501d1fa59e9SXin LI VP_TO_TMPFS_NODE(struct vnode *vp) 502d1fa59e9SXin LI { 503d1fa59e9SXin LI struct tmpfs_node *node; 504d1fa59e9SXin LI 505*bba7ed20SKonstantin Belousov MPASS(vp != NULL && vp->v_data != NULL); 506d1fa59e9SXin LI node = (struct tmpfs_node *)vp->v_data; 507*bba7ed20SKonstantin Belousov return (node); 508d1fa59e9SXin LI } 509d1fa59e9SXin LI 510*bba7ed20SKonstantin Belousov static inline struct tmpfs_node * 511d1fa59e9SXin LI VP_TO_TMPFS_DIR(struct vnode *vp) 512d1fa59e9SXin LI { 513d1fa59e9SXin LI struct tmpfs_node *node; 514d1fa59e9SXin LI 515d1fa59e9SXin LI node = VP_TO_TMPFS_NODE(vp); 516d1fa59e9SXin LI TMPFS_VALIDATE_DIR(node); 517*bba7ed20SKonstantin Belousov return (node); 518d1fa59e9SXin LI } 519d1fa59e9SXin LI 520d1fa59e9SXin LI #endif /* _FS_TMPFS_TMPFS_H_ */ 521