1 /* $NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD 5 * 6 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 11 * 2005 program. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 * 34 * $FreeBSD$ 35 */ 36 37 #ifndef _FS_TMPFS_TMPFS_H_ 38 #define _FS_TMPFS_TMPFS_H_ 39 40 #include <sys/cdefs.h> 41 #include <sys/queue.h> 42 #include <sys/tree.h> 43 44 #ifdef _SYS_MALLOC_H_ 45 MALLOC_DECLARE(M_TMPFSMNT); 46 MALLOC_DECLARE(M_TMPFSNAME); 47 #endif 48 49 /* 50 * Internal representation of a tmpfs directory entry. 51 */ 52 53 LIST_HEAD(tmpfs_dir_duphead, tmpfs_dirent); 54 55 struct tmpfs_dirent { 56 /* 57 * Depending on td_cookie flag entry can be of 3 types: 58 * - regular -- no hash collisions, stored in RB-Tree 59 * - duphead -- synthetic linked list head for dup entries 60 * - dup -- stored in linked list instead of RB-Tree 61 */ 62 union { 63 /* regular and duphead entry types */ 64 RB_ENTRY(tmpfs_dirent) td_entries; 65 66 /* dup entry type */ 67 struct { 68 LIST_ENTRY(tmpfs_dirent) entries; 69 LIST_ENTRY(tmpfs_dirent) index_entries; 70 } td_dup; 71 } uh; 72 73 uint32_t td_cookie; 74 uint32_t td_hash; 75 u_int td_namelen; 76 77 /* 78 * Pointer to the node this entry refers to. In case this field 79 * is NULL, the node is a whiteout. 80 */ 81 struct tmpfs_node * td_node; 82 83 union { 84 /* 85 * The name of the entry, allocated from a string pool. This 86 * string is not required to be zero-terminated. 87 */ 88 char * td_name; /* regular, dup */ 89 struct tmpfs_dir_duphead td_duphead; /* duphead */ 90 } ud; 91 }; 92 93 /* 94 * A directory in tmpfs holds a collection of directory entries, which 95 * in turn point to other files (which can be directories themselves). 96 * 97 * In tmpfs, this collection is managed by a RB-Tree, whose head is 98 * defined by the struct tmpfs_dir type. 99 * 100 * It is important to notice that directories do not have entries for . and 101 * .. as other file systems do. These can be generated when requested 102 * based on information available by other means, such as the pointer to 103 * the node itself in the former case or the pointer to the parent directory 104 * in the latter case. This is done to simplify tmpfs's code and, more 105 * importantly, to remove redundancy. 106 */ 107 RB_HEAD(tmpfs_dir, tmpfs_dirent); 108 109 /* 110 * Each entry in a directory has a cookie that identifies it. Cookies 111 * supersede offsets within directories because, given how tmpfs stores 112 * directories in memory, there is no such thing as an offset. 113 * 114 * The '.', '..' and the end of directory markers have fixed cookies which 115 * cannot collide with the cookies generated by other entries. The cookies 116 * for the other entries are generated based on the file name hash value or 117 * unique number in case of name hash collision. 118 * 119 * To preserve compatibility cookies are limited to 31 bits. 120 */ 121 122 #define TMPFS_DIRCOOKIE_DOT 0 123 #define TMPFS_DIRCOOKIE_DOTDOT 1 124 #define TMPFS_DIRCOOKIE_EOF 2 125 #define TMPFS_DIRCOOKIE_MASK ((off_t)0x3fffffffU) 126 #define TMPFS_DIRCOOKIE_MIN ((off_t)0x00000004U) 127 #define TMPFS_DIRCOOKIE_DUP ((off_t)0x40000000U) 128 #define TMPFS_DIRCOOKIE_DUPHEAD ((off_t)0x80000000U) 129 #define TMPFS_DIRCOOKIE_DUP_MIN TMPFS_DIRCOOKIE_DUP 130 #define TMPFS_DIRCOOKIE_DUP_MAX \ 131 (TMPFS_DIRCOOKIE_DUP | TMPFS_DIRCOOKIE_MASK) 132 133 /* 134 * Internal representation of a tmpfs file system node. 135 * 136 * This structure is splitted in two parts: one holds attributes common 137 * to all file types and the other holds data that is only applicable to 138 * a particular type. The code must be careful to only access those 139 * attributes that are actually allowed by the node's type. 140 * 141 * Below is the key of locks used to protected the fields in the following 142 * structures. 143 * (v) vnode lock in exclusive mode 144 * (vi) vnode lock in exclusive mode, or vnode lock in shared vnode and 145 * tn_interlock 146 * (i) tn_interlock 147 * (m) tmpfs_mount tm_allnode_lock 148 * (c) stable after creation 149 */ 150 struct tmpfs_node { 151 /* 152 * Doubly-linked list entry which links all existing nodes for 153 * a single file system. This is provided to ease the removal 154 * of all nodes during the unmount operation, and to support 155 * the implementation of VOP_VNTOCNP(). tn_attached is false 156 * when the node is removed from list and unlocked. 157 */ 158 LIST_ENTRY(tmpfs_node) tn_entries; /* (m) */ 159 160 /* Node identifier. */ 161 ino_t tn_id; /* (c) */ 162 163 /* 164 * The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO', 165 * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode 166 * types instead of a custom enumeration is to make things simpler 167 * and faster, as we do not need to convert between two types. 168 */ 169 enum vtype tn_type; /* (c) */ 170 171 /* 172 * See the top comment. Reordered here to fill LP64 hole. 173 */ 174 bool tn_attached; /* (m) */ 175 176 /* 177 * Node's internal status. This is used by several file system 178 * operations to do modifications to the node in a delayed 179 * fashion. 180 * 181 * tn_accessed has a dedicated byte to allow update by store without 182 * using atomics. This provides a micro-optimization to e.g. 183 * tmpfs_read_pgcache(). 184 */ 185 uint8_t tn_status; /* (vi) */ 186 uint8_t tn_accessed; /* unlocked */ 187 188 /* 189 * The node size. It does not necessarily match the real amount 190 * of memory consumed by it. 191 */ 192 off_t tn_size; /* (v) */ 193 194 /* Generic node attributes. */ 195 uid_t tn_uid; /* (v) */ 196 gid_t tn_gid; /* (v) */ 197 mode_t tn_mode; /* (v) */ 198 int tn_links; /* (v) */ 199 u_long tn_flags; /* (v) */ 200 struct timespec tn_atime; /* (vi) */ 201 struct timespec tn_mtime; /* (vi) */ 202 struct timespec tn_ctime; /* (vi) */ 203 struct timespec tn_birthtime; /* (v) */ 204 unsigned long tn_gen; /* (c) */ 205 206 /* 207 * As there is a single vnode for each active file within the 208 * system, care has to be taken to avoid allocating more than one 209 * vnode per file. In order to do this, a bidirectional association 210 * is kept between vnodes and nodes. 211 * 212 * Whenever a vnode is allocated, its v_data field is updated to 213 * point to the node it references. At the same time, the node's 214 * tn_vnode field is modified to point to the new vnode representing 215 * it. Further attempts to allocate a vnode for this same node will 216 * result in returning a new reference to the value stored in 217 * tn_vnode. 218 * 219 * May be NULL when the node is unused (that is, no vnode has been 220 * allocated for it or it has been reclaimed). 221 */ 222 struct vnode * tn_vnode; /* (i) */ 223 224 /* 225 * Interlock to protect tn_vpstate, and tn_status under shared 226 * vnode lock. 227 */ 228 struct mtx tn_interlock; 229 230 /* 231 * Identify if current node has vnode assiocate with 232 * or allocating vnode. 233 */ 234 int tn_vpstate; /* (i) */ 235 236 /* Transient refcounter on this node. */ 237 u_int tn_refcount; /* 0<->1 (m) + (i) */ 238 239 /* misc data field for different tn_type node */ 240 union { 241 /* Valid when tn_type == VBLK || tn_type == VCHR. */ 242 dev_t tn_rdev; /* (c) */ 243 244 /* Valid when tn_type == VDIR. */ 245 struct tn_dir { 246 /* 247 * Pointer to the parent directory. The root 248 * directory has a pointer to itself in this field; 249 * this property identifies the root node. 250 */ 251 struct tmpfs_node * tn_parent; 252 253 /* 254 * Head of a tree that links the contents of 255 * the directory together. 256 */ 257 struct tmpfs_dir tn_dirhead; 258 259 /* 260 * Head of a list the contains fake directory entries 261 * heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD 262 * flag. 263 */ 264 struct tmpfs_dir_duphead tn_dupindex; 265 266 /* 267 * Number and pointer of the first directory entry 268 * returned by the readdir operation if it were 269 * called again to continue reading data from the 270 * same directory as before. This is used to speed 271 * up reads of long directories, assuming that no 272 * more than one read is in progress at a given time. 273 * Otherwise, these values are discarded. 274 */ 275 off_t tn_readdir_lastn; 276 struct tmpfs_dirent * tn_readdir_lastp; 277 } tn_dir; 278 279 /* Valid when tn_type == VLNK. */ 280 /* The link's target, allocated from a string pool. */ 281 char * tn_link; /* (c) */ 282 283 /* Valid when tn_type == VREG. */ 284 struct tn_reg { 285 /* 286 * The contents of regular files stored in a 287 * tmpfs file system are represented by a 288 * single anonymous memory object (aobj, for 289 * short). The aobj provides direct access to 290 * any position within the file. It is a task 291 * of the memory management subsystem to issue 292 * the required page ins or page outs whenever 293 * a position within the file is accessed. 294 */ 295 vm_object_t tn_aobj; /* (c) */ 296 struct tmpfs_mount *tn_tmp; /* (c) */ 297 } tn_reg; 298 } tn_spec; /* (v) */ 299 }; 300 LIST_HEAD(tmpfs_node_list, tmpfs_node); 301 302 #define tn_rdev tn_spec.tn_rdev 303 #define tn_dir tn_spec.tn_dir 304 #define tn_link tn_spec.tn_link 305 #define tn_reg tn_spec.tn_reg 306 #define tn_fifo tn_spec.tn_fifo 307 308 #define TMPFS_LINK_MAX INT_MAX 309 310 #define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock) 311 #define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock) 312 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock) 313 #define TMPFS_NODE_ASSERT_LOCKED(node) mtx_assert(TMPFS_NODE_MTX(node), \ 314 MA_OWNED) 315 316 #ifdef INVARIANTS 317 #define TMPFS_ASSERT_LOCKED(node) do { \ 318 MPASS((node) != NULL); \ 319 MPASS((node)->tn_vnode != NULL); \ 320 ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs assert"); \ 321 } while (0) 322 #else 323 #define TMPFS_ASSERT_LOCKED(node) (void)0 324 #endif 325 326 /* tn_vpstate */ 327 #define TMPFS_VNODE_ALLOCATING 1 328 #define TMPFS_VNODE_WANT 2 329 #define TMPFS_VNODE_DOOMED 4 330 #define TMPFS_VNODE_WRECLAIM 8 331 332 /* tn_status */ 333 #define TMPFS_NODE_MODIFIED 0x01 334 #define TMPFS_NODE_CHANGED 0x02 335 336 /* 337 * Internal representation of a tmpfs mount point. 338 */ 339 struct tmpfs_mount { 340 /* 341 * Original value of the "size" parameter, for reference purposes, 342 * mostly. 343 */ 344 off_t tm_size_max; 345 /* 346 * Maximum number of memory pages available for use by the file 347 * system, set during mount time. This variable must never be 348 * used directly as it may be bigger than the current amount of 349 * free memory; in the extreme case, it will hold the ULONG_MAX 350 * value. 351 */ 352 u_long tm_pages_max; 353 354 /* Number of pages in use by the file system. */ 355 u_long tm_pages_used; 356 357 /* 358 * Pointer to the node representing the root directory of this 359 * file system. 360 */ 361 struct tmpfs_node * tm_root; 362 363 /* 364 * Maximum number of possible nodes for this file system; set 365 * during mount time. We need a hard limit on the maximum number 366 * of nodes to avoid allocating too much of them; their objects 367 * cannot be released until the file system is unmounted. 368 * Otherwise, we could easily run out of memory by creating lots 369 * of empty files and then simply removing them. 370 */ 371 ino_t tm_nodes_max; 372 373 /* unrhdr used to allocate inode numbers */ 374 struct unrhdr64 tm_ino_unr; 375 376 /* Number of nodes currently that are in use. */ 377 ino_t tm_nodes_inuse; 378 379 /* Refcounter on this struct tmpfs_mount. */ 380 uint64_t tm_refcount; 381 382 /* maximum representable file size */ 383 u_int64_t tm_maxfilesize; 384 385 /* 386 * The used list contains all nodes that are currently used by 387 * the file system; i.e., they refer to existing files. 388 */ 389 struct tmpfs_node_list tm_nodes_used; 390 391 /* All node lock to protect the node list and tmp_pages_used. */ 392 struct mtx tm_allnode_lock; 393 394 /* Read-only status. */ 395 bool tm_ronly; 396 /* Do not use namecache. */ 397 bool tm_nonc; 398 /* Do not update mtime on writes through mmaped areas. */ 399 bool tm_nomtime; 400 }; 401 #define TMPFS_LOCK(tm) mtx_lock(&(tm)->tm_allnode_lock) 402 #define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->tm_allnode_lock) 403 #define TMPFS_MP_ASSERT_LOCKED(tm) mtx_assert(&(tm)->tm_allnode_lock, MA_OWNED) 404 405 /* 406 * This structure maps a file identifier to a tmpfs node. Used by the 407 * NFS code. 408 */ 409 struct tmpfs_fid_data { 410 ino_t tfd_id; 411 unsigned long tfd_gen; 412 }; 413 _Static_assert(sizeof(struct tmpfs_fid_data) <= MAXFIDSZ, 414 "(struct tmpfs_fid_data) is larger than (struct fid).fid_data"); 415 416 struct tmpfs_dir_cursor { 417 struct tmpfs_dirent *tdc_current; 418 struct tmpfs_dirent *tdc_tree; 419 }; 420 421 #ifdef _KERNEL 422 /* 423 * Prototypes for tmpfs_subr.c. 424 */ 425 426 void tmpfs_ref_node(struct tmpfs_node *node); 427 int tmpfs_alloc_node(struct mount *mp, struct tmpfs_mount *, enum vtype, 428 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *, 429 const char *, dev_t, struct tmpfs_node **); 430 int tmpfs_fo_close(struct file *fp, struct thread *td); 431 void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *); 432 bool tmpfs_free_node_locked(struct tmpfs_mount *, struct tmpfs_node *, bool); 433 void tmpfs_free_tmp(struct tmpfs_mount *); 434 int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *, 435 const char *, u_int, struct tmpfs_dirent **); 436 void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *); 437 void tmpfs_dirent_init(struct tmpfs_dirent *, const char *, u_int); 438 void tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj); 439 int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int, 440 struct vnode **); 441 void tmpfs_free_vp(struct vnode *); 442 int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *, 443 struct componentname *, const char *); 444 void tmpfs_check_mtime(struct vnode *); 445 void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *); 446 void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *); 447 void tmpfs_dir_destroy(struct tmpfs_mount *, struct tmpfs_node *); 448 struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node, 449 struct tmpfs_node *f, 450 struct componentname *cnp); 451 int tmpfs_dir_getdents(struct tmpfs_mount *, struct tmpfs_node *, 452 struct uio *, int, u_long *, int *); 453 int tmpfs_dir_whiteout_add(struct vnode *, struct componentname *); 454 void tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *); 455 int tmpfs_reg_resize(struct vnode *, off_t, boolean_t); 456 int tmpfs_chflags(struct vnode *, u_long, struct ucred *, struct thread *); 457 int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *); 458 int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *, 459 struct thread *); 460 int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *); 461 int tmpfs_chtimes(struct vnode *, struct vattr *, struct ucred *cred, 462 struct thread *); 463 void tmpfs_itimes(struct vnode *, const struct timespec *, 464 const struct timespec *); 465 466 void tmpfs_set_accessed(struct tmpfs_mount *tm, struct tmpfs_node *node); 467 void tmpfs_set_status(struct tmpfs_mount *tm, struct tmpfs_node *node, 468 int status); 469 int tmpfs_truncate(struct vnode *, off_t); 470 struct tmpfs_dirent *tmpfs_dir_first(struct tmpfs_node *dnode, 471 struct tmpfs_dir_cursor *dc); 472 struct tmpfs_dirent *tmpfs_dir_next(struct tmpfs_node *dnode, 473 struct tmpfs_dir_cursor *dc); 474 static __inline void 475 tmpfs_update(struct vnode *vp) 476 { 477 478 tmpfs_itimes(vp, NULL, NULL); 479 } 480 481 /* 482 * Convenience macros to simplify some logical expressions. 483 */ 484 #define IMPLIES(a, b) (!(a) || (b)) 485 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a)) 486 487 /* 488 * Checks that the directory entry pointed by 'de' matches the name 'name' 489 * with a length of 'len'. 490 */ 491 #define TMPFS_DIRENT_MATCHES(de, name, len) \ 492 (de->td_namelen == len && \ 493 bcmp((de)->ud.td_name, (name), (de)->td_namelen) == 0) 494 495 /* 496 * Ensures that the node pointed by 'node' is a directory and that its 497 * contents are consistent with respect to directories. 498 */ 499 #define TMPFS_VALIDATE_DIR(node) do { \ 500 MPASS((node)->tn_type == VDIR); \ 501 MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \ 502 } while (0) 503 504 /* 505 * Amount of memory pages to reserve for the system (e.g., to not use by 506 * tmpfs). 507 */ 508 #if !defined(TMPFS_PAGES_MINRESERVED) 509 #define TMPFS_PAGES_MINRESERVED (4 * 1024 * 1024 / PAGE_SIZE) 510 #endif 511 512 size_t tmpfs_mem_avail(void); 513 size_t tmpfs_pages_used(struct tmpfs_mount *tmp); 514 void tmpfs_subr_init(void); 515 void tmpfs_subr_uninit(void); 516 517 /* 518 * Macros/functions to convert from generic data structures to tmpfs 519 * specific ones. 520 */ 521 522 static inline struct tmpfs_mount * 523 VFS_TO_TMPFS(struct mount *mp) 524 { 525 struct tmpfs_mount *tmp; 526 527 MPASS(mp != NULL && mp->mnt_data != NULL); 528 tmp = (struct tmpfs_mount *)mp->mnt_data; 529 return (tmp); 530 } 531 532 static inline struct tmpfs_node * 533 VP_TO_TMPFS_NODE(struct vnode *vp) 534 { 535 struct tmpfs_node *node; 536 537 MPASS(vp != NULL && vp->v_data != NULL); 538 node = (struct tmpfs_node *)vp->v_data; 539 return (node); 540 } 541 542 #define VP_TO_TMPFS_NODE_SMR(vp) \ 543 ((struct tmpfs_node *)vn_load_v_data_smr(vp)) 544 545 static inline struct tmpfs_node * 546 VP_TO_TMPFS_DIR(struct vnode *vp) 547 { 548 struct tmpfs_node *node; 549 550 node = VP_TO_TMPFS_NODE(vp); 551 TMPFS_VALIDATE_DIR(node); 552 return (node); 553 } 554 555 static inline bool 556 tmpfs_use_nc(struct vnode *vp) 557 { 558 559 return (!(VFS_TO_TMPFS(vp->v_mount)->tm_nonc)); 560 } 561 562 static inline void 563 tmpfs_update_getattr(struct vnode *vp) 564 { 565 struct tmpfs_node *node; 566 567 node = VP_TO_TMPFS_NODE(vp); 568 if (__predict_false((node->tn_status & (TMPFS_NODE_MODIFIED | 569 TMPFS_NODE_CHANGED)) != 0 || node->tn_accessed)) 570 tmpfs_update(vp); 571 } 572 573 extern struct fileops tmpfs_fnops; 574 575 #endif /* _KERNEL */ 576 577 #endif /* _FS_TMPFS_TMPFS_H_ */ 578