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