tmpfs.h revision 183299
1171802Sdelphij/* $NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $ */ 2170808Sdelphij 3182739Sdelphij/*- 4171802Sdelphij * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. 5170808Sdelphij * All rights reserved. 6170808Sdelphij * 7170808Sdelphij * This code is derived from software contributed to The NetBSD Foundation 8170808Sdelphij * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 9170808Sdelphij * 2005 program. 10170808Sdelphij * 11170808Sdelphij * Redistribution and use in source and binary forms, with or without 12170808Sdelphij * modification, are permitted provided that the following conditions 13170808Sdelphij * are met: 14170808Sdelphij * 1. Redistributions of source code must retain the above copyright 15170808Sdelphij * notice, this list of conditions and the following disclaimer. 16170808Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 17170808Sdelphij * notice, this list of conditions and the following disclaimer in the 18170808Sdelphij * documentation and/or other materials provided with the distribution. 19170808Sdelphij * 20170808Sdelphij * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21170808Sdelphij * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22170808Sdelphij * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23170808Sdelphij * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24170808Sdelphij * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25170808Sdelphij * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26170808Sdelphij * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27170808Sdelphij * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28170808Sdelphij * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29170808Sdelphij * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30170808Sdelphij * POSSIBILITY OF SUCH DAMAGE. 31170808Sdelphij * 32170808Sdelphij * $FreeBSD: head/sys/fs/tmpfs/tmpfs.h 183299 2008-09-23 14:45:10Z obrien $ 33170808Sdelphij */ 34170808Sdelphij 35170808Sdelphij#ifndef _FS_TMPFS_TMPFS_H_ 36170808Sdelphij#define _FS_TMPFS_TMPFS_H_ 37170808Sdelphij 38170808Sdelphij/* --------------------------------------------------------------------- 39170808Sdelphij * KERNEL-SPECIFIC DEFINITIONS 40170808Sdelphij * --------------------------------------------------------------------- */ 41170808Sdelphij#include <sys/dirent.h> 42170808Sdelphij#include <sys/mount.h> 43170808Sdelphij#include <sys/queue.h> 44170808Sdelphij#include <sys/vnode.h> 45170808Sdelphij#include <sys/file.h> 46170808Sdelphij#include <sys/lock.h> 47170808Sdelphij#include <sys/mutex.h> 48170808Sdelphij 49170808Sdelphij/* --------------------------------------------------------------------- */ 50170808Sdelphij#include <sys/malloc.h> 51170808Sdelphij#include <sys/systm.h> 52170808Sdelphij#include <sys/vmmeter.h> 53170808Sdelphij#include <vm/swap_pager.h> 54170808Sdelphij 55170808SdelphijMALLOC_DECLARE(M_TMPFSMNT); 56171087SdelphijMALLOC_DECLARE(M_TMPFSNAME); 57170808Sdelphij 58170808Sdelphij/* --------------------------------------------------------------------- */ 59170808Sdelphij 60170808Sdelphij/* 61170808Sdelphij * Internal representation of a tmpfs directory entry. 62170808Sdelphij */ 63170808Sdelphijstruct tmpfs_dirent { 64170808Sdelphij TAILQ_ENTRY(tmpfs_dirent) td_entries; 65170808Sdelphij 66170808Sdelphij /* Length of the name stored in this directory entry. This avoids 67170808Sdelphij * the need to recalculate it every time the name is used. */ 68170808Sdelphij uint16_t td_namelen; 69170808Sdelphij 70170808Sdelphij /* The name of the entry, allocated from a string pool. This 71170808Sdelphij * string is not required to be zero-terminated; therefore, the 72170808Sdelphij * td_namelen field must always be used when accessing its value. */ 73170808Sdelphij char * td_name; 74170808Sdelphij 75170808Sdelphij /* Pointer to the node this entry refers to. */ 76170808Sdelphij struct tmpfs_node * td_node; 77170808Sdelphij}; 78170808Sdelphij 79170808Sdelphij/* A directory in tmpfs holds a sorted list of directory entries, which in 80170808Sdelphij * turn point to other files (which can be directories themselves). 81170808Sdelphij * 82170808Sdelphij * In tmpfs, this list is managed by a tail queue, whose head is defined by 83170808Sdelphij * the struct tmpfs_dir type. 84170808Sdelphij * 85170808Sdelphij * It is imporant to notice that directories do not have entries for . and 86170808Sdelphij * .. as other file systems do. These can be generated when requested 87170808Sdelphij * based on information available by other means, such as the pointer to 88170808Sdelphij * the node itself in the former case or the pointer to the parent directory 89170808Sdelphij * in the latter case. This is done to simplify tmpfs's code and, more 90170808Sdelphij * importantly, to remove redundancy. */ 91170808SdelphijTAILQ_HEAD(tmpfs_dir, tmpfs_dirent); 92170808Sdelphij 93171802Sdelphij/* Each entry in a directory has a cookie that identifies it. Cookies 94171802Sdelphij * supersede offsets within directories because, given how tmpfs stores 95171802Sdelphij * directories in memory, there is no such thing as an offset. (Emulating 96171802Sdelphij * a real offset could be very difficult.) 97171802Sdelphij * 98171802Sdelphij * The '.', '..' and the end of directory markers have fixed cookies which 99171802Sdelphij * cannot collide with the cookies generated by other entries. The cookies 100171802Sdelphij * fot the other entries are generated based on the memory address on which 101171802Sdelphij * stores their information is stored. 102171802Sdelphij * 103171802Sdelphij * Ideally, using the entry's memory pointer as the cookie would be enough 104171802Sdelphij * to represent it and it wouldn't cause collisions in any system. 105171802Sdelphij * Unfortunately, this results in "offsets" with very large values which 106171802Sdelphij * later raise problems in the Linux compatibility layer (and maybe in other 107171802Sdelphij * places) as described in PR kern/32034. Hence we need to workaround this 108171802Sdelphij * with a rather ugly hack. 109171802Sdelphij * 110171802Sdelphij * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t 111171802Sdelphij * set to 'long', which is a 32-bit *signed* long integer. Regardless of 112171802Sdelphij * the macro value, GLIBC (2.3 at least) always uses the getdents64 113171802Sdelphij * system call (when calling readdir) which internally returns off64_t 114171802Sdelphij * offsets. In order to make 32-bit binaries work, *GLIBC* converts the 115171802Sdelphij * 64-bit values returned by the kernel to 32-bit ones and aborts with 116171802Sdelphij * EOVERFLOW if the conversion results in values that won't fit in 32-bit 117171802Sdelphij * integers (which it assumes is because the directory is extremely large). 118171802Sdelphij * This wouldn't cause problems if we were dealing with unsigned integers, 119171802Sdelphij * but as we have signed integers, this check fails due to sign expansion. 120171802Sdelphij * 121171802Sdelphij * For example, consider that the kernel returns the 0xc1234567 cookie to 122171802Sdelphij * userspace in a off64_t integer. Later on, GLIBC casts this value to 123171802Sdelphij * off_t (remember, signed) with code similar to: 124171802Sdelphij * system call returns the offset in kernel_value; 125171802Sdelphij * off_t casted_value = kernel_value; 126171802Sdelphij * if (sizeof(off_t) != sizeof(off64_t) && 127171802Sdelphij * kernel_value != casted_value) 128171802Sdelphij * error! 129171802Sdelphij * In this case, casted_value still has 0xc1234567, but when it is compared 130171802Sdelphij * for equality against kernel_value, it is promoted to a 64-bit integer and 131171802Sdelphij * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567. 132171802Sdelphij * Then, GLIBC assumes this is because the directory is very large. 133171802Sdelphij * 134171802Sdelphij * Given that all the above happens in user-space, we have no control over 135171802Sdelphij * it; therefore we must workaround the issue here. We do this by 136171802Sdelphij * truncating the pointer value to a 32-bit integer and hope that there 137171802Sdelphij * won't be collisions. In fact, this will not cause any problems in 138171802Sdelphij * 32-bit platforms but some might arise in 64-bit machines (I'm not sure 139171802Sdelphij * if they can happen at all in practice). 140171802Sdelphij * 141171802Sdelphij * XXX A nicer solution shall be attempted. */ 142171802Sdelphij#ifdef _KERNEL 143170808Sdelphij#define TMPFS_DIRCOOKIE_DOT 0 144170808Sdelphij#define TMPFS_DIRCOOKIE_DOTDOT 1 145170808Sdelphij#define TMPFS_DIRCOOKIE_EOF 2 146171802Sdelphijstatic __inline 147171802Sdelphijoff_t 148171802Sdelphijtmpfs_dircookie(struct tmpfs_dirent *de) 149171802Sdelphij{ 150171802Sdelphij off_t cookie; 151170808Sdelphij 152171802Sdelphij cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF; 153171802Sdelphij MPASS(cookie != TMPFS_DIRCOOKIE_DOT); 154171802Sdelphij MPASS(cookie != TMPFS_DIRCOOKIE_DOTDOT); 155171802Sdelphij MPASS(cookie != TMPFS_DIRCOOKIE_EOF); 156171802Sdelphij 157171802Sdelphij return cookie; 158171802Sdelphij} 159171802Sdelphij#endif 160171802Sdelphij 161170808Sdelphij/* --------------------------------------------------------------------- */ 162170808Sdelphij 163170808Sdelphij/* 164170808Sdelphij * Internal representation of a tmpfs file system node. 165170808Sdelphij * 166170808Sdelphij * This structure is splitted in two parts: one holds attributes common 167170808Sdelphij * to all file types and the other holds data that is only applicable to 168170808Sdelphij * a particular type. The code must be careful to only access those 169170808Sdelphij * attributes that are actually allowed by the node's type. 170170808Sdelphij * 171170808Sdelphij * 172170808Sdelphij * Below is the key of locks used to protected the fields in the following 173170808Sdelphij * structures. 174170808Sdelphij * 175170808Sdelphij */ 176170808Sdelphijstruct tmpfs_node { 177170808Sdelphij /* Doubly-linked list entry which links all existing nodes for a 178170808Sdelphij * single file system. This is provided to ease the removal of 179170808Sdelphij * all nodes during the unmount operation. */ 180170808Sdelphij LIST_ENTRY(tmpfs_node) tn_entries; 181170808Sdelphij 182170808Sdelphij /* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO', 183170808Sdelphij * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode 184170808Sdelphij * types instead of a custom enumeration is to make things simpler 185170808Sdelphij * and faster, as we do not need to convert between two types. */ 186170808Sdelphij enum vtype tn_type; 187170808Sdelphij 188170808Sdelphij /* Node identifier. */ 189170808Sdelphij ino_t tn_id; 190170808Sdelphij 191170808Sdelphij /* Node's internal status. This is used by several file system 192170808Sdelphij * operations to do modifications to the node in a delayed 193170808Sdelphij * fashion. */ 194170808Sdelphij int tn_status; 195170808Sdelphij#define TMPFS_NODE_ACCESSED (1 << 1) 196170808Sdelphij#define TMPFS_NODE_MODIFIED (1 << 2) 197170808Sdelphij#define TMPFS_NODE_CHANGED (1 << 3) 198170808Sdelphij 199170808Sdelphij /* The node size. It does not necessarily match the real amount 200170808Sdelphij * of memory consumed by it. */ 201170808Sdelphij off_t tn_size; 202170808Sdelphij 203170808Sdelphij /* Generic node attributes. */ 204170808Sdelphij uid_t tn_uid; 205170808Sdelphij gid_t tn_gid; 206170808Sdelphij mode_t tn_mode; 207170808Sdelphij int tn_flags; 208170808Sdelphij nlink_t tn_links; 209170808Sdelphij struct timespec tn_atime; 210170808Sdelphij struct timespec tn_mtime; 211170808Sdelphij struct timespec tn_ctime; 212170808Sdelphij struct timespec tn_birthtime; 213170808Sdelphij unsigned long tn_gen; 214170808Sdelphij 215170808Sdelphij /* As there is a single vnode for each active file within the 216170808Sdelphij * system, care has to be taken to avoid allocating more than one 217170808Sdelphij * vnode per file. In order to do this, a bidirectional association 218170808Sdelphij * is kept between vnodes and nodes. 219170808Sdelphij * 220170808Sdelphij * Whenever a vnode is allocated, its v_data field is updated to 221170808Sdelphij * point to the node it references. At the same time, the node's 222170808Sdelphij * tn_vnode field is modified to point to the new vnode representing 223170808Sdelphij * it. Further attempts to allocate a vnode for this same node will 224170808Sdelphij * result in returning a new reference to the value stored in 225170808Sdelphij * tn_vnode. 226170808Sdelphij * 227170808Sdelphij * May be NULL when the node is unused (that is, no vnode has been 228170808Sdelphij * allocated for it or it has been reclaimed). */ 229170808Sdelphij struct vnode * tn_vnode; 230170808Sdelphij 231170808Sdelphij /* interlock to protect tn_vpstate */ 232170808Sdelphij struct mtx tn_interlock; 233170808Sdelphij 234170808Sdelphij /* Identify if current node has vnode assiocate with 235170808Sdelphij * or allocating vnode. 236170808Sdelphij */ 237170808Sdelphij int tn_vpstate; 238170808Sdelphij 239170808Sdelphij /* misc data field for different tn_type node */ 240170808Sdelphij union { 241170808Sdelphij /* Valid when tn_type == VBLK || tn_type == VCHR. */ 242170808Sdelphij dev_t tn_rdev; 243170808Sdelphij 244170808Sdelphij /* Valid when tn_type == VDIR. */ 245170808Sdelphij struct tn_dir{ 246170808Sdelphij /* Pointer to the parent directory. The root 247170808Sdelphij * directory has a pointer to itself in this field; 248170808Sdelphij * this property identifies the root node. */ 249170808Sdelphij struct tmpfs_node * tn_parent; 250170808Sdelphij 251170808Sdelphij /* Head of a tail-queue that links the contents of 252170808Sdelphij * the directory together. See above for a 253170808Sdelphij * description of its contents. */ 254170808Sdelphij struct tmpfs_dir tn_dirhead; 255170808Sdelphij 256170808Sdelphij /* Number and pointer of the first directory entry 257170808Sdelphij * returned by the readdir operation if it were 258170808Sdelphij * called again to continue reading data from the 259170808Sdelphij * same directory as before. This is used to speed 260170808Sdelphij * up reads of long directories, assuming that no 261170808Sdelphij * more than one read is in progress at a given time. 262170808Sdelphij * Otherwise, these values are discarded and a linear 263170808Sdelphij * scan is performed from the beginning up to the 264170808Sdelphij * point where readdir starts returning values. */ 265170808Sdelphij off_t tn_readdir_lastn; 266170808Sdelphij struct tmpfs_dirent * tn_readdir_lastp; 267170808Sdelphij }tn_dir; 268170808Sdelphij 269170808Sdelphij /* Valid when tn_type == VLNK. */ 270170808Sdelphij /* The link's target, allocated from a string pool. */ 271170808Sdelphij char * tn_link; 272170808Sdelphij 273170808Sdelphij /* Valid when tn_type == VREG. */ 274170808Sdelphij struct tn_reg { 275170808Sdelphij /* The contents of regular files stored in a tmpfs 276170808Sdelphij * file system are represented by a single anonymous 277170808Sdelphij * memory object (aobj, for short). The aobj provides 278170808Sdelphij * direct access to any position within the file, 279170808Sdelphij * because its contents are always mapped in a 280170808Sdelphij * contiguous region of virtual memory. It is a task 281170808Sdelphij * of the memory management subsystem (see uvm(9)) to 282170808Sdelphij * issue the required page ins or page outs whenever 283170808Sdelphij * a position within the file is accessed. */ 284170808Sdelphij vm_object_t tn_aobj; 285170808Sdelphij size_t tn_aobj_pages; 286170808Sdelphij 287170808Sdelphij }tn_reg; 288170808Sdelphij 289170808Sdelphij /* Valid when tn_type = VFIFO */ 290170808Sdelphij struct tn_fifo { 291170808Sdelphij fo_rdwr_t *tn_fo_read; 292170808Sdelphij fo_rdwr_t *tn_fo_write; 293170808Sdelphij }tn_fifo; 294170808Sdelphij }tn_spec; 295170808Sdelphij}; 296170808SdelphijLIST_HEAD(tmpfs_node_list, tmpfs_node); 297170808Sdelphij 298170808Sdelphij#define tn_rdev tn_spec.tn_rdev 299170808Sdelphij#define tn_dir tn_spec.tn_dir 300170808Sdelphij#define tn_link tn_spec.tn_link 301170808Sdelphij#define tn_reg tn_spec.tn_reg 302170808Sdelphij#define tn_fifo tn_spec.tn_fifo 303170808Sdelphij 304170808Sdelphij#define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock) 305170808Sdelphij#define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock) 306171704Sdelphij#define TMPFS_NODE_MTX(node) (&(node)->tn_interlock) 307170808Sdelphij 308170808Sdelphij#define TMPFS_VNODE_ALLOCATING 1 309170808Sdelphij#define TMPFS_VNODE_WANT 2 310170808Sdelphij/* --------------------------------------------------------------------- */ 311170808Sdelphij 312170808Sdelphij/* 313170808Sdelphij * Internal representation of a tmpfs mount point. 314170808Sdelphij */ 315170808Sdelphijstruct tmpfs_mount { 316170808Sdelphij /* Maximum number of memory pages available for use by the file 317170808Sdelphij * system, set during mount time. This variable must never be 318171038Sdelphij * used directly as it may be bigger than the current amount of 319170808Sdelphij * free memory; in the extreme case, it will hold the SIZE_MAX 320170808Sdelphij * value. Instead, use the TMPFS_PAGES_MAX macro. */ 321170808Sdelphij size_t tm_pages_max; 322170808Sdelphij 323170808Sdelphij /* Number of pages in use by the file system. Cannot be bigger 324170808Sdelphij * than the value returned by TMPFS_PAGES_MAX in any case. */ 325170808Sdelphij size_t tm_pages_used; 326170808Sdelphij 327170808Sdelphij /* Pointer to the node representing the root directory of this 328170808Sdelphij * file system. */ 329170808Sdelphij struct tmpfs_node * tm_root; 330170808Sdelphij 331170808Sdelphij /* Maximum number of possible nodes for this file system; set 332170808Sdelphij * during mount time. We need a hard limit on the maximum number 333170808Sdelphij * of nodes to avoid allocating too much of them; their objects 334170808Sdelphij * cannot be released until the file system is unmounted. 335170808Sdelphij * Otherwise, we could easily run out of memory by creating lots 336170808Sdelphij * of empty files and then simply removing them. */ 337170808Sdelphij ino_t tm_nodes_max; 338170808Sdelphij 339171362Sdelphij /* unrhdr used to allocate inode numbers */ 340171362Sdelphij struct unrhdr * tm_ino_unr; 341170808Sdelphij 342170808Sdelphij /* Number of nodes currently that are in use. */ 343170808Sdelphij ino_t tm_nodes_inuse; 344170808Sdelphij 345170808Sdelphij /* maximum representable file size */ 346170808Sdelphij u_int64_t tm_maxfilesize; 347171070Sdelphij 348170808Sdelphij /* Nodes are organized in two different lists. The used list 349170808Sdelphij * contains all nodes that are currently used by the file system; 350170808Sdelphij * i.e., they refer to existing files. The available list contains 351170808Sdelphij * all nodes that are currently available for use by new files. 352170808Sdelphij * Nodes must be kept in this list (instead of deleting them) 353170808Sdelphij * because we need to keep track of their generation number (tn_gen 354170808Sdelphij * field). 355170808Sdelphij * 356170808Sdelphij * Note that nodes are lazily allocated: if the available list is 357170808Sdelphij * empty and we have enough space to create more nodes, they will be 358170808Sdelphij * created and inserted in the used list. Once these are released, 359170808Sdelphij * they will go into the available list, remaining alive until the 360170808Sdelphij * file system is unmounted. */ 361170808Sdelphij struct tmpfs_node_list tm_nodes_used; 362170808Sdelphij 363170808Sdelphij /* All node lock to protect the node list and tmp_pages_used */ 364170808Sdelphij struct mtx allnode_lock; 365170808Sdelphij 366170808Sdelphij /* Pools used to store file system meta data. These are not shared 367170808Sdelphij * across several instances of tmpfs for the reasons described in 368170808Sdelphij * tmpfs_pool.c. */ 369170808Sdelphij uma_zone_t tm_dirent_pool; 370170808Sdelphij uma_zone_t tm_node_pool; 371170808Sdelphij}; 372170808Sdelphij#define TMPFS_LOCK(tm) mtx_lock(&(tm)->allnode_lock) 373170808Sdelphij#define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->allnode_lock) 374170808Sdelphij 375170808Sdelphij/* --------------------------------------------------------------------- */ 376170808Sdelphij 377170808Sdelphij/* 378170808Sdelphij * This structure maps a file identifier to a tmpfs node. Used by the 379170808Sdelphij * NFS code. 380170808Sdelphij */ 381170808Sdelphijstruct tmpfs_fid { 382170808Sdelphij uint16_t tf_len; 383170808Sdelphij uint16_t tf_pad; 384171067Sdelphij ino_t tf_id; 385170808Sdelphij unsigned long tf_gen; 386170808Sdelphij}; 387170808Sdelphij 388170808Sdelphij/* --------------------------------------------------------------------- */ 389170808Sdelphij 390170808Sdelphij#ifdef _KERNEL 391170808Sdelphij/* 392170808Sdelphij * Prototypes for tmpfs_subr.c. 393170808Sdelphij */ 394170808Sdelphij 395170808Sdelphijint tmpfs_alloc_node(struct tmpfs_mount *, enum vtype, 396170808Sdelphij uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *, 397170808Sdelphij char *, dev_t, struct thread *, struct tmpfs_node **); 398170808Sdelphijvoid tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *); 399170808Sdelphijint tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *, 400170808Sdelphij const char *, uint16_t, struct tmpfs_dirent **); 401170808Sdelphijvoid tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *, 402170808Sdelphij boolean_t); 403171799Sdelphijint tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int, 404171799Sdelphij struct vnode **, struct thread *); 405170808Sdelphijvoid tmpfs_free_vp(struct vnode *); 406170808Sdelphijint tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *, 407170808Sdelphij struct componentname *, char *); 408170808Sdelphijvoid tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *); 409170808Sdelphijvoid tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *); 410170808Sdelphijstruct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node, 411170808Sdelphij struct componentname *cnp); 412173725Sdelphijstruct tmpfs_dirent *tmpfs_dir_search(struct tmpfs_node *node, 413173725Sdelphij struct tmpfs_node *f); 414170808Sdelphijint tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *); 415170808Sdelphijint tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *); 416170808Sdelphijstruct tmpfs_dirent * tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t); 417170808Sdelphijint tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *); 418170808Sdelphijint tmpfs_reg_resize(struct vnode *, off_t); 419170808Sdelphijint tmpfs_chflags(struct vnode *, int, struct ucred *, struct thread *); 420170808Sdelphijint tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *); 421170808Sdelphijint tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *, 422170808Sdelphij struct thread *); 423170808Sdelphijint tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *); 424171070Sdelphijint tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *, 425170808Sdelphij struct timespec *, int, struct ucred *, struct thread *); 426170808Sdelphijvoid tmpfs_itimes(struct vnode *, const struct timespec *, 427170808Sdelphij const struct timespec *); 428170808Sdelphij 429170808Sdelphijvoid tmpfs_update(struct vnode *); 430170808Sdelphijint tmpfs_truncate(struct vnode *, off_t); 431170808Sdelphij 432170808Sdelphij/* --------------------------------------------------------------------- */ 433170808Sdelphij 434170808Sdelphij/* 435170808Sdelphij * Convenience macros to simplify some logical expressions. 436170808Sdelphij */ 437170808Sdelphij#define IMPLIES(a, b) (!(a) || (b)) 438170808Sdelphij#define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a)) 439170808Sdelphij 440170808Sdelphij/* --------------------------------------------------------------------- */ 441170808Sdelphij 442170808Sdelphij/* 443170808Sdelphij * Checks that the directory entry pointed by 'de' matches the name 'name' 444170808Sdelphij * with a length of 'len'. 445170808Sdelphij */ 446170808Sdelphij#define TMPFS_DIRENT_MATCHES(de, name, len) \ 447170808Sdelphij (de->td_namelen == (uint16_t)len && \ 448183299Sobrien bcmp((de)->td_name, (name), (de)->td_namelen) == 0) 449170808Sdelphij 450170808Sdelphij/* --------------------------------------------------------------------- */ 451170808Sdelphij 452170808Sdelphij/* 453170808Sdelphij * Ensures that the node pointed by 'node' is a directory and that its 454170808Sdelphij * contents are consistent with respect to directories. 455170808Sdelphij */ 456170808Sdelphij#define TMPFS_VALIDATE_DIR(node) \ 457170808Sdelphij MPASS((node)->tn_type == VDIR); \ 458170808Sdelphij MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \ 459170808Sdelphij MPASS((node)->tn_dir.tn_readdir_lastp == NULL || \ 460171802Sdelphij tmpfs_dircookie((node)->tn_dir.tn_readdir_lastp) == (node)->tn_dir.tn_readdir_lastn); 461170808Sdelphij 462170808Sdelphij/* --------------------------------------------------------------------- */ 463170808Sdelphij 464170808Sdelphij/* 465170808Sdelphij * Memory management stuff. 466170808Sdelphij */ 467170808Sdelphij 468170808Sdelphij/* Amount of memory pages to reserve for the system (e.g., to not use by 469170808Sdelphij * tmpfs). 470170808Sdelphij * XXX: Should this be tunable through sysctl, for instance? */ 471170808Sdelphij#define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE) 472170808Sdelphij 473170808Sdelphij/* 474170808Sdelphij * Returns information about the number of available memory pages, 475170808Sdelphij * including physical and virtual ones. 476170808Sdelphij * 477171070Sdelphij * If 'total' is TRUE, the value returned is the total amount of memory 478170808Sdelphij * pages configured for the system (either in use or free). 479170808Sdelphij * If it is FALSE, the value returned is the amount of free memory pages. 480170808Sdelphij * 481170808Sdelphij * Remember to remove TMPFS_PAGES_RESERVED from the returned value to avoid 482170808Sdelphij * excessive memory usage. 483170808Sdelphij * 484170808Sdelphij */ 485170808Sdelphijstatic __inline size_t 486170808Sdelphijtmpfs_mem_info(void) 487170808Sdelphij{ 488170808Sdelphij size_t size; 489170808Sdelphij 490170808Sdelphij size = swap_pager_avail + cnt.v_free_count + cnt.v_inactive_count; 491170808Sdelphij size -= size > cnt.v_wire_count ? cnt.v_wire_count : size; 492170808Sdelphij return size; 493170808Sdelphij} 494170808Sdelphij 495170808Sdelphij/* Returns the maximum size allowed for a tmpfs file system. This macro 496170808Sdelphij * must be used instead of directly retrieving the value from tm_pages_max. 497170808Sdelphij * The reason is that the size of a tmpfs file system is dynamic: it lets 498170808Sdelphij * the user store files as long as there is enough free memory (including 499170808Sdelphij * physical memory and swap space). Therefore, the amount of memory to be 500170808Sdelphij * used is either the limit imposed by the user during mount time or the 501170808Sdelphij * amount of available memory, whichever is lower. To avoid consuming all 502170808Sdelphij * the memory for a given mount point, the system will always reserve a 503170808Sdelphij * minimum of TMPFS_PAGES_RESERVED pages, which is also taken into account 504170808Sdelphij * by this macro (see above). */ 505170808Sdelphijstatic __inline size_t 506170808SdelphijTMPFS_PAGES_MAX(struct tmpfs_mount *tmp) 507170808Sdelphij{ 508170808Sdelphij size_t freepages; 509170808Sdelphij 510170808Sdelphij freepages = tmpfs_mem_info(); 511171070Sdelphij freepages -= freepages < TMPFS_PAGES_RESERVED ? 512170808Sdelphij freepages : TMPFS_PAGES_RESERVED; 513170808Sdelphij 514170808Sdelphij return MIN(tmp->tm_pages_max, freepages + tmp->tm_pages_used); 515170808Sdelphij} 516170808Sdelphij 517170808Sdelphij/* Returns the available space for the given file system. */ 518171308Sdelphij#define TMPFS_META_PAGES(tmp) (howmany((tmp)->tm_nodes_inuse * (sizeof(struct tmpfs_node) \ 519171308Sdelphij + sizeof(struct tmpfs_dirent)), PAGE_SIZE)) 520171029Sdelphij#define TMPFS_FILE_PAGES(tmp) ((tmp)->tm_pages_used) 521170808Sdelphij 522171029Sdelphij#define TMPFS_PAGES_AVAIL(tmp) (TMPFS_PAGES_MAX(tmp) > \ 523171029Sdelphij TMPFS_META_PAGES(tmp)+TMPFS_FILE_PAGES(tmp)? \ 524171029Sdelphij TMPFS_PAGES_MAX(tmp) - TMPFS_META_PAGES(tmp) \ 525171029Sdelphij - TMPFS_FILE_PAGES(tmp):0) 526171029Sdelphij 527170808Sdelphij#endif 528170808Sdelphij 529170808Sdelphij/* --------------------------------------------------------------------- */ 530170808Sdelphij 531170808Sdelphij/* 532170808Sdelphij * Macros/functions to convert from generic data structures to tmpfs 533170808Sdelphij * specific ones. 534170808Sdelphij */ 535170808Sdelphij 536170808Sdelphijstatic inline 537170808Sdelphijstruct tmpfs_mount * 538170808SdelphijVFS_TO_TMPFS(struct mount *mp) 539170808Sdelphij{ 540170808Sdelphij struct tmpfs_mount *tmp; 541170808Sdelphij 542170808Sdelphij MPASS((mp) != NULL && (mp)->mnt_data != NULL); 543170808Sdelphij tmp = (struct tmpfs_mount *)(mp)->mnt_data; 544170808Sdelphij return tmp; 545170808Sdelphij} 546170808Sdelphij 547170808Sdelphijstatic inline 548170808Sdelphijstruct tmpfs_node * 549170808SdelphijVP_TO_TMPFS_NODE(struct vnode *vp) 550170808Sdelphij{ 551170808Sdelphij struct tmpfs_node *node; 552170808Sdelphij 553170808Sdelphij MPASS((vp) != NULL && (vp)->v_data != NULL); 554170808Sdelphij node = (struct tmpfs_node *)vp->v_data; 555170808Sdelphij return node; 556170808Sdelphij} 557170808Sdelphij 558170808Sdelphijstatic inline 559170808Sdelphijstruct tmpfs_node * 560170808SdelphijVP_TO_TMPFS_DIR(struct vnode *vp) 561170808Sdelphij{ 562170808Sdelphij struct tmpfs_node *node; 563170808Sdelphij 564170808Sdelphij node = VP_TO_TMPFS_NODE(vp); 565170808Sdelphij TMPFS_VALIDATE_DIR(node); 566170808Sdelphij return node; 567170808Sdelphij} 568170808Sdelphij 569170808Sdelphij#endif /* _FS_TMPFS_TMPFS_H_ */ 570