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