1 /* $NetBSD: tmpfs.h,v 1.38 2010/06/22 18:32:07 rmind Exp $ */ 2 3 /* 4 * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 9 * 2005 program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #ifndef _FS_TMPFS_TMPFS_H_ 34 #define _FS_TMPFS_TMPFS_H_ 35 36 #include <sys/dirent.h> 37 #include <sys/mount.h> 38 #include <sys/pool.h> 39 #include <sys/queue.h> 40 #include <sys/vnode.h> 41 42 /* 43 * Internal representation of a tmpfs directory entry. 44 */ 45 struct tmpfs_dirent { 46 TAILQ_ENTRY(tmpfs_dirent) td_entries; 47 48 /* Length of the name stored in this directory entry. This avoids 49 * the need to recalculate it every time the name is used. */ 50 uint16_t td_namelen; 51 52 /* The name of the entry, allocated from a string pool. This 53 * string is not required to be zero-terminated; therefore, the 54 * td_namelen field must always be used when accessing its value. */ 55 char * td_name; 56 57 /* Pointer to the node this entry refers to. */ 58 struct tmpfs_node * td_node; 59 }; 60 61 /* A directory in tmpfs holds a sorted list of directory entries, which in 62 * turn point to other files (which can be directories themselves). 63 * 64 * In tmpfs, this list is managed by a tail queue, whose head is defined by 65 * the struct tmpfs_dir type. 66 * 67 * It is imporant to notice that directories do not have entries for . and 68 * .. as other file systems do. These can be generated when requested 69 * based on information available by other means, such as the pointer to 70 * the node itself in the former case or the pointer to the parent directory 71 * in the latter case. This is done to simplify tmpfs's code and, more 72 * importantly, to remove redundancy. */ 73 TAILQ_HEAD(tmpfs_dir, tmpfs_dirent); 74 75 /* Each entry in a directory has a cookie that identifies it. Cookies 76 * supersede offsets within directories because, given how tmpfs stores 77 * directories in memory, there is no such thing as an offset. (Emulating 78 * a real offset could be very difficult.) 79 * 80 * The '.', '..' and the end of directory markers have fixed cookies which 81 * cannot collide with the cookies generated by other entries. The cookies 82 * fot the other entries are generated based on the memory address on which 83 * stores their information is stored. 84 * 85 * Ideally, using the entry's memory pointer as the cookie would be enough 86 * to represent it and it wouldn't cause collisions in any system. 87 * Unfortunately, this results in "offsets" with very large values which 88 * later raise problems in the Linux compatibility layer (and maybe in other 89 * places) as described in PR kern/32034. Hence we need to workaround this 90 * with a rather ugly hack. 91 * 92 * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t 93 * set to 'long', which is a 32-bit *signed* long integer. Regardless of 94 * the macro value, GLIBC (2.3 at least) always uses the getdents64 95 * system call (when calling readdir) which internally returns off64_t 96 * offsets. In order to make 32-bit binaries work, *GLIBC* converts the 97 * 64-bit values returned by the kernel to 32-bit ones and aborts with 98 * EOVERFLOW if the conversion results in values that won't fit in 32-bit 99 * integers (which it assumes is because the directory is extremely large). 100 * This wouldn't cause problems if we were dealing with unsigned integers, 101 * but as we have signed integers, this check fails due to sign expansion. 102 * 103 * For example, consider that the kernel returns the 0xc1234567 cookie to 104 * userspace in a off64_t integer. Later on, GLIBC casts this value to 105 * off_t (remember, signed) with code similar to: 106 * system call returns the offset in kernel_value; 107 * off_t casted_value = kernel_value; 108 * if (sizeof(off_t) != sizeof(off64_t) && 109 * kernel_value != casted_value) 110 * error! 111 * In this case, casted_value still has 0xc1234567, but when it is compared 112 * for equality against kernel_value, it is promoted to a 64-bit integer and 113 * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567. 114 * Then, GLIBC assumes this is because the directory is very large. 115 * 116 * Given that all the above happens in user-space, we have no control over 117 * it; therefore we must workaround the issue here. We do this by 118 * truncating the pointer value to a 32-bit integer and hope that there 119 * won't be collisions. In fact, this will not cause any problems in 120 * 32-bit platforms but some might arise in 64-bit machines (I'm not sure 121 * if they can happen at all in practice). 122 * 123 * XXX A nicer solution shall be attempted. */ 124 #if defined(_KERNEL) 125 #define TMPFS_DIRCOOKIE_DOT 0 126 #define TMPFS_DIRCOOKIE_DOTDOT 1 127 #define TMPFS_DIRCOOKIE_EOF 2 128 static __inline 129 off_t 130 tmpfs_dircookie(struct tmpfs_dirent *de) 131 { 132 off_t cookie; 133 134 cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF; 135 KASSERT(cookie != TMPFS_DIRCOOKIE_DOT); 136 KASSERT(cookie != TMPFS_DIRCOOKIE_DOTDOT); 137 KASSERT(cookie != TMPFS_DIRCOOKIE_EOF); 138 139 return cookie; 140 } 141 #endif /* defined(_KERNEL) */ 142 143 /* --------------------------------------------------------------------- */ 144 145 /* 146 * Internal representation of a tmpfs file system node. 147 * 148 * This structure is splitted in two parts: one holds attributes common 149 * to all file types and the other holds data that is only applicable to 150 * a particular type. The code must be careful to only access those 151 * attributes that are actually allowed by the node's type. 152 */ 153 struct tmpfs_node { 154 /* Doubly-linked list entry which links all existing nodes for a 155 * single file system. This is provided to ease the removal of 156 * all nodes during the unmount operation. */ 157 LIST_ENTRY(tmpfs_node) tn_entries; 158 159 /* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO', 160 * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode 161 * types instead of a custom enumeration is to make things simpler 162 * and faster, as we do not need to convert between two types. */ 163 enum vtype tn_type; 164 165 /* Node identifier. */ 166 ino_t tn_id; 167 168 /* Node's internal status. This is used by several file system 169 * operations to do modifications to the node in a delayed 170 * fashion. */ 171 int tn_status; 172 #define TMPFS_NODE_ACCESSED (1 << 1) 173 #define TMPFS_NODE_MODIFIED (1 << 2) 174 #define TMPFS_NODE_CHANGED (1 << 3) 175 176 /* The node size. It does not necessarily match the real amount 177 * of memory consumed by it. */ 178 off_t tn_size; 179 180 /* Generic node attributes. */ 181 uid_t tn_uid; 182 gid_t tn_gid; 183 mode_t tn_mode; 184 int tn_flags; 185 nlink_t tn_links; 186 struct timespec tn_atime; 187 struct timespec tn_mtime; 188 struct timespec tn_ctime; 189 struct timespec tn_birthtime; 190 unsigned long tn_gen; 191 192 /* Head of byte-level lock list (used by tmpfs_advlock). */ 193 struct lockf * tn_lockf; 194 195 /* As there is a single vnode for each active file within the 196 * system, care has to be taken to avoid allocating more than one 197 * vnode per file. In order to do this, a bidirectional association 198 * is kept between vnodes and nodes. 199 * 200 * Whenever a vnode is allocated, its v_data field is updated to 201 * point to the node it references. At the same time, the node's 202 * tn_vnode field is modified to point to the new vnode representing 203 * it. Further attempts to allocate a vnode for this same node will 204 * result in returning a new reference to the value stored in 205 * tn_vnode. 206 * 207 * May be NULL when the node is unused (that is, no vnode has been 208 * allocated for it or it has been reclaimed). */ 209 kmutex_t tn_vlock; 210 struct vnode * tn_vnode; 211 212 union { 213 /* Valid when tn_type == VBLK || tn_type == VCHR. */ 214 struct { 215 dev_t tn_rdev; 216 } tn_dev; 217 218 /* Valid when tn_type == VDIR. */ 219 struct { 220 /* Pointer to the parent directory. The root 221 * directory has a pointer to itself in this field; 222 * this property identifies the root node. */ 223 struct tmpfs_node * tn_parent; 224 225 /* Head of a tail-queue that links the contents of 226 * the directory together. See above for a 227 * description of its contents. */ 228 struct tmpfs_dir tn_dir; 229 230 /* Number and pointer of the first directory entry 231 * returned by the readdir operation if it were 232 * called again to continue reading data from the 233 * same directory as before. This is used to speed 234 * up reads of long directories, assuming that no 235 * more than one read is in progress at a given time. 236 * Otherwise, these values are discarded and a linear 237 * scan is performed from the beginning up to the 238 * point where readdir starts returning values. */ 239 off_t tn_readdir_lastn; 240 struct tmpfs_dirent * tn_readdir_lastp; 241 } tn_dir; 242 243 /* Valid when tn_type == VLNK. */ 244 struct tn_lnk { 245 /* The link's target, allocated from a string pool. */ 246 char * tn_link; 247 } tn_lnk; 248 249 /* Valid when tn_type == VREG. */ 250 struct tn_reg { 251 /* The contents of regular files stored in a tmpfs 252 * file system are represented by a single anonymous 253 * memory object (aobj, for short). The aobj provides 254 * direct access to any position within the file, 255 * because its contents are always mapped in a 256 * contiguous region of virtual memory. It is a task 257 * of the memory management subsystem (see uvm(9)) to 258 * issue the required page ins or page outs whenever 259 * a position within the file is accessed. */ 260 struct uvm_object * tn_aobj; 261 size_t tn_aobj_pages; 262 } tn_reg; 263 } tn_spec; 264 }; 265 266 #if defined(_KERNEL) 267 LIST_HEAD(tmpfs_node_list, tmpfs_node); 268 269 /* --------------------------------------------------------------------- */ 270 271 /* 272 * Internal representation of a tmpfs mount point. 273 */ 274 struct tmpfs_mount { 275 /* Limit and number of bytes in use by the file system. */ 276 uint64_t tm_mem_limit; 277 uint64_t tm_bytes_used; 278 kmutex_t tm_acc_lock; 279 280 /* Pointer to the node representing the root directory of this 281 * file system. */ 282 struct tmpfs_node * tm_root; 283 284 /* Maximum number of possible nodes for this file system; set 285 * during mount time. We need a hard limit on the maximum number 286 * of nodes to avoid allocating too much of them; their objects 287 * cannot be released until the file system is unmounted. 288 * Otherwise, we could easily run out of memory by creating lots 289 * of empty files and then simply removing them. */ 290 unsigned int tm_nodes_max; 291 292 /* Number of nodes currently allocated. This number only grows. 293 * When it reaches tm_nodes_max, no more new nodes can be allocated. 294 * Of course, the old, unused ones can be reused. */ 295 unsigned int tm_nodes_cnt; 296 297 /* Node list. */ 298 kmutex_t tm_lock; 299 struct tmpfs_node_list tm_nodes; 300 301 char tm_dwchan[32]; 302 struct pool tm_dirent_pool; 303 char tm_nwchan[32]; 304 struct pool tm_node_pool; 305 }; 306 307 /* --------------------------------------------------------------------- */ 308 309 /* 310 * This structure maps a file identifier to a tmpfs node. Used by the 311 * NFS code. 312 */ 313 struct tmpfs_fid { 314 uint16_t tf_len; 315 uint16_t tf_pad; 316 uint32_t tf_gen; 317 ino_t tf_id; 318 }; 319 320 /* --------------------------------------------------------------------- */ 321 322 /* 323 * Prototypes for tmpfs_subr.c. 324 */ 325 326 int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype, 327 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *, 328 char *, dev_t, struct tmpfs_node **); 329 void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *); 330 int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *, 331 const char *, uint16_t, struct tmpfs_dirent **); 332 void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *, 333 bool); 334 int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **); 335 void tmpfs_free_vp(struct vnode *); 336 int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *, 337 struct componentname *, char *); 338 void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *); 339 void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *); 340 struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node, 341 struct componentname *cnp); 342 int tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *); 343 int tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *); 344 struct tmpfs_dirent * tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t); 345 int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *); 346 int tmpfs_reg_resize(struct vnode *, off_t); 347 int tmpfs_chflags(struct vnode *, int, kauth_cred_t, struct lwp *); 348 int tmpfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct lwp *); 349 int tmpfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, struct lwp *); 350 int tmpfs_chsize(struct vnode *, u_quad_t, kauth_cred_t, struct lwp *); 351 int tmpfs_chtimes(struct vnode *, const struct timespec *, 352 const struct timespec *, const struct timespec *, int, kauth_cred_t, 353 struct lwp *); 354 void tmpfs_itimes(struct vnode *, const struct timespec *, 355 const struct timespec *, const struct timespec *); 356 357 void tmpfs_update(struct vnode *, const struct timespec *, 358 const struct timespec *, const struct timespec *, int); 359 int tmpfs_truncate(struct vnode *, off_t); 360 361 /* 362 * Prototypes for tmpfs_mem.c. 363 */ 364 365 void tmpfs_mntmem_init(struct tmpfs_mount *, uint64_t); 366 void tmpfs_mntmem_destroy(struct tmpfs_mount *); 367 368 size_t tmpfs_mem_info(bool); 369 uint64_t tmpfs_bytes_max(struct tmpfs_mount *); 370 size_t tmpfs_pages_avail(struct tmpfs_mount *); 371 bool tmpfs_mem_incr(struct tmpfs_mount *, size_t); 372 void tmpfs_mem_decr(struct tmpfs_mount *, size_t); 373 374 struct tmpfs_dirent *tmpfs_dirent_get(struct tmpfs_mount *); 375 void tmpfs_dirent_put(struct tmpfs_mount *, struct tmpfs_dirent *); 376 377 struct tmpfs_node *tmpfs_node_get(struct tmpfs_mount *); 378 void tmpfs_node_put(struct tmpfs_mount *, struct tmpfs_node *); 379 380 char * tmpfs_strname_alloc(struct tmpfs_mount *, size_t); 381 void tmpfs_strname_free(struct tmpfs_mount *, char *, size_t); 382 bool tmpfs_strname_neqlen(struct componentname *, struct componentname *); 383 384 /* --------------------------------------------------------------------- */ 385 386 /* 387 * Convenience macros to simplify some logical expressions. 388 */ 389 #define IMPLIES(a, b) (!(a) || (b)) 390 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a)) 391 392 /* --------------------------------------------------------------------- */ 393 394 /* 395 * Checks that the directory entry pointed by 'de' matches the name 'name' 396 * with a length of 'len'. 397 */ 398 #define TMPFS_DIRENT_MATCHES(de, name, len) \ 399 (de->td_namelen == (uint16_t)len && \ 400 memcmp((de)->td_name, (name), (de)->td_namelen) == 0) 401 402 /* --------------------------------------------------------------------- */ 403 404 /* 405 * Ensures that the node pointed by 'node' is a directory and that its 406 * contents are consistent with respect to directories. 407 */ 408 #define TMPFS_VALIDATE_DIR(node) \ 409 KASSERT((node)->tn_type == VDIR); \ 410 KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \ 411 KASSERT((node)->tn_spec.tn_dir.tn_readdir_lastp == NULL || \ 412 tmpfs_dircookie((node)->tn_spec.tn_dir.tn_readdir_lastp) == \ 413 (node)->tn_spec.tn_dir.tn_readdir_lastn); 414 415 /* --------------------------------------------------------------------- */ 416 417 /* 418 * Memory management stuff. 419 */ 420 421 /* Amount of memory pages to reserve for the system (e.g., to not use by 422 * tmpfs). 423 * XXX: Should this be tunable through sysctl, for instance? */ 424 #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE) 425 426 /* 427 * Macros/functions to convert from generic data structures to tmpfs 428 * specific ones. 429 */ 430 431 static __inline 432 struct tmpfs_mount * 433 VFS_TO_TMPFS(struct mount *mp) 434 { 435 struct tmpfs_mount *tmp; 436 437 #ifdef KASSERT 438 KASSERT((mp) != NULL && (mp)->mnt_data != NULL); 439 #endif 440 tmp = (struct tmpfs_mount *)(mp)->mnt_data; 441 return tmp; 442 } 443 444 #endif /* defined(_KERNEL) */ 445 446 static __inline 447 struct tmpfs_node * 448 VP_TO_TMPFS_NODE(struct vnode *vp) 449 { 450 struct tmpfs_node *node; 451 452 #ifdef KASSERT 453 KASSERT((vp) != NULL && (vp)->v_data != NULL); 454 #endif 455 node = (struct tmpfs_node *)vp->v_data; 456 return node; 457 } 458 459 #if defined(_KERNEL) 460 461 static __inline 462 struct tmpfs_node * 463 VP_TO_TMPFS_DIR(struct vnode *vp) 464 { 465 struct tmpfs_node *node; 466 467 node = VP_TO_TMPFS_NODE(vp); 468 #ifdef KASSERT 469 TMPFS_VALIDATE_DIR(node); 470 #endif 471 return node; 472 } 473 474 #endif /* defined(_KERNEL) */ 475 #endif /* _FS_TMPFS_TMPFS_H_ */ 476