1 /* $NetBSD: tmpfs.h,v 1.47 2013/11/18 01:39:34 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 #if !defined(_KERNEL) && !defined(_KMEMUSER) 37 #error "not supposed to be exposed to userland" 38 #endif 39 40 #include <sys/dirent.h> 41 #include <sys/mount.h> 42 #include <sys/pool.h> 43 #include <sys/queue.h> 44 #include <sys/vnode.h> 45 46 /* 47 * Internal representation of a tmpfs directory entry. 48 * 49 * All fields are protected by vnode lock. 50 */ 51 typedef struct tmpfs_dirent { 52 TAILQ_ENTRY(tmpfs_dirent) td_entries; 53 54 /* Pointer to the inode this entry refers to. */ 55 struct tmpfs_node * td_node; 56 57 /* Sequence number, see tmpfs_dir_getseq(). */ 58 uint32_t td_seq; 59 60 /* Name and its length. */ 61 char * td_name; 62 uint16_t td_namelen; 63 } tmpfs_dirent_t; 64 65 TAILQ_HEAD(tmpfs_dir, tmpfs_dirent); 66 67 /* 68 * Internal representation of a tmpfs file system node -- inode. 69 * 70 * This structure is split in two parts: one holds attributes common 71 * to all file types and the other holds data that is only applicable to 72 * a particular type. 73 * 74 * All fields are protected by vnode lock. The vnode association itself 75 * is protected by tmpfs_node_t::tn_vlock. 76 */ 77 typedef struct tmpfs_node { 78 LIST_ENTRY(tmpfs_node) tn_entries; 79 80 /* 81 * Each inode has a corresponding vnode. It is a bi-directional 82 * association. Whenever vnode is allocated, its v_data field is 83 * set to the inode it reference, and tmpfs_node_t::tn_vnode is 84 * set to point to the said vnode. 85 * 86 * Further attempts to allocate a vnode for this same node will 87 * result in returning a new reference to the value stored in 88 * tn_vnode. It may be NULL when the node is unused (that is, 89 * no vnode has been allocated or it has been reclaimed). 90 */ 91 kmutex_t tn_vlock; 92 vnode_t * tn_vnode; 93 94 /* Directory entry. Only a hint, since hard link can have multiple. */ 95 tmpfs_dirent_t * tn_dirent_hint; 96 97 /* The inode type: VBLK, VCHR, VDIR, VFIFO, VLNK, VREG or VSOCK. */ 98 enum vtype tn_type; 99 100 /* Inode identifier and generation number. */ 101 ino_t tn_id; 102 uint32_t tn_gen; 103 104 /* Inode status flags (for operations in delayed manner). */ 105 unsigned tn_status; 106 107 /* The inode size. */ 108 off_t tn_size; 109 110 /* Generic node attributes. */ 111 uid_t tn_uid; 112 gid_t tn_gid; 113 mode_t tn_mode; 114 int tn_flags; 115 nlink_t tn_links; 116 struct timespec tn_atime; 117 struct timespec tn_mtime; 118 struct timespec tn_ctime; 119 struct timespec tn_birthtime; 120 121 /* Head of byte-level lock list (used by tmpfs_advlock). */ 122 struct lockf * tn_lockf; 123 124 union { 125 /* Type case: VBLK or VCHR. */ 126 struct { 127 dev_t tn_rdev; 128 } tn_dev; 129 130 /* Type case: VDIR. */ 131 struct { 132 /* Parent directory (root inode points to itself). */ 133 struct tmpfs_node * tn_parent; 134 135 /* List of directory entries. */ 136 struct tmpfs_dir tn_dir; 137 138 /* Last given sequence number and their arena. */ 139 uint32_t tn_next_seq; 140 void * tn_seq_arena; 141 142 /* 143 * Pointer of the last directory entry returned 144 * by the readdir(3) operation. 145 */ 146 struct tmpfs_dirent * tn_readdir_lastp; 147 } tn_dir; 148 149 /* Type case: VLNK. */ 150 struct tn_lnk { 151 /* The link's target. */ 152 char * tn_link; 153 } tn_lnk; 154 155 /* Type case: VREG. */ 156 struct tn_reg { 157 /* Underlying UVM object to store contents. */ 158 struct uvm_object * tn_aobj; 159 size_t tn_aobj_pages; 160 } tn_reg; 161 } tn_spec; 162 } tmpfs_node_t; 163 164 #if defined(_KERNEL) 165 166 LIST_HEAD(tmpfs_node_list, tmpfs_node); 167 168 #define TMPFS_MAXNAMLEN 255 169 /* Validate maximum td_namelen length. */ 170 CTASSERT(TMPFS_MAXNAMLEN < UINT16_MAX); 171 172 /* 173 * Reserved values for the virtual entries (the first must be 0) and EOF. 174 * The start/end of the incremental range, see tmpfs_dir_getseq(). 175 */ 176 #define TMPFS_DIRSEQ_DOT 0 177 #define TMPFS_DIRSEQ_DOTDOT 1 178 #define TMPFS_DIRSEQ_EOF 2 179 180 #define TMPFS_DIRSEQ_START 3 /* inclusive */ 181 #define TMPFS_DIRSEQ_END (1U << 30) /* exclusive */ 182 183 /* Mark to indicate that the number is not set. */ 184 #define TMPFS_DIRSEQ_NONE (1U << 31) 185 186 /* Status flags. */ 187 #define TMPFS_NODE_ACCESSED 0x01 188 #define TMPFS_NODE_MODIFIED 0x02 189 #define TMPFS_NODE_CHANGED 0x04 190 191 #define TMPFS_NODE_STATUSALL \ 192 (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED) 193 194 /* 195 * Bit indicating vnode reclamation. 196 * We abuse tmpfs_node_t::tn_gen for that. 197 */ 198 #define TMPFS_RECLAIMING_BIT (1U << 31) 199 #define TMPFS_NODE_GEN_MASK (TMPFS_RECLAIMING_BIT - 1) 200 201 #define TMPFS_NODE_RECLAIMING(node) \ 202 (((node)->tn_gen & TMPFS_RECLAIMING_BIT) != 0) 203 204 #define TMPFS_NODE_GEN(node) \ 205 ((node)->tn_gen & TMPFS_NODE_GEN_MASK) 206 207 /* White-out inode indicator. */ 208 #define TMPFS_NODE_WHITEOUT ((tmpfs_node_t *)-1) 209 210 /* 211 * Internal representation of a tmpfs mount point. 212 */ 213 typedef struct tmpfs_mount { 214 /* Limit and number of bytes in use by the file system. */ 215 uint64_t tm_mem_limit; 216 uint64_t tm_bytes_used; 217 kmutex_t tm_acc_lock; 218 219 /* Pointer to the root inode. */ 220 tmpfs_node_t * tm_root; 221 222 /* Maximum number of possible nodes for this file system. */ 223 unsigned int tm_nodes_max; 224 225 /* Number of nodes currently allocated. */ 226 unsigned int tm_nodes_cnt; 227 228 /* List of inodes and the lock protecting it. */ 229 kmutex_t tm_lock; 230 struct tmpfs_node_list tm_nodes; 231 } tmpfs_mount_t; 232 233 /* 234 * This structure maps a file identifier to a tmpfs node. Used by the 235 * NFS code. 236 */ 237 typedef struct tmpfs_fid { 238 uint16_t tf_len; 239 uint16_t tf_pad; 240 uint32_t tf_gen; 241 ino_t tf_id; 242 } tmpfs_fid_t; 243 244 /* 245 * Prototypes for tmpfs_subr.c. 246 */ 247 248 int tmpfs_alloc_node(tmpfs_mount_t *, enum vtype, uid_t, gid_t, 249 mode_t, char *, dev_t, tmpfs_node_t **); 250 void tmpfs_free_node(tmpfs_mount_t *, tmpfs_node_t *); 251 252 int tmpfs_alloc_file(vnode_t *, vnode_t **, struct vattr *, 253 struct componentname *, char *); 254 255 int tmpfs_vnode_get(struct mount *, tmpfs_node_t *, vnode_t **); 256 257 int tmpfs_alloc_dirent(tmpfs_mount_t *, const char *, uint16_t, 258 tmpfs_dirent_t **); 259 void tmpfs_free_dirent(tmpfs_mount_t *, tmpfs_dirent_t *); 260 void tmpfs_dir_attach(tmpfs_node_t *, tmpfs_dirent_t *, tmpfs_node_t *); 261 void tmpfs_dir_detach(tmpfs_node_t *, tmpfs_dirent_t *); 262 263 tmpfs_dirent_t *tmpfs_dir_lookup(tmpfs_node_t *, struct componentname *); 264 tmpfs_dirent_t *tmpfs_dir_cached(tmpfs_node_t *); 265 266 uint32_t tmpfs_dir_getseq(tmpfs_node_t *, tmpfs_dirent_t *); 267 tmpfs_dirent_t *tmpfs_dir_lookupbyseq(tmpfs_node_t *, off_t); 268 int tmpfs_dir_getdents(tmpfs_node_t *, struct uio *, off_t *); 269 270 int tmpfs_reg_resize(vnode_t *, off_t); 271 int tmpfs_truncate(vnode_t *, off_t); 272 273 int tmpfs_chflags(vnode_t *, int, kauth_cred_t, lwp_t *); 274 int tmpfs_chmod(vnode_t *, mode_t, kauth_cred_t, lwp_t *); 275 int tmpfs_chown(vnode_t *, uid_t, gid_t, kauth_cred_t, lwp_t *); 276 int tmpfs_chsize(vnode_t *, u_quad_t, kauth_cred_t, lwp_t *); 277 int tmpfs_chtimes(vnode_t *, const struct timespec *, 278 const struct timespec *, const struct timespec *, int, 279 kauth_cred_t, lwp_t *); 280 void tmpfs_update(vnode_t *, const struct timespec *, 281 const struct timespec *, const struct timespec *, int); 282 283 /* 284 * Prototypes for tmpfs_mem.c. 285 */ 286 287 void tmpfs_mntmem_init(tmpfs_mount_t *, uint64_t); 288 void tmpfs_mntmem_destroy(tmpfs_mount_t *); 289 290 size_t tmpfs_mem_info(bool); 291 uint64_t tmpfs_bytes_max(tmpfs_mount_t *); 292 size_t tmpfs_pages_avail(tmpfs_mount_t *); 293 bool tmpfs_mem_incr(tmpfs_mount_t *, size_t); 294 void tmpfs_mem_decr(tmpfs_mount_t *, size_t); 295 296 tmpfs_dirent_t *tmpfs_dirent_get(tmpfs_mount_t *); 297 void tmpfs_dirent_put(tmpfs_mount_t *, tmpfs_dirent_t *); 298 299 tmpfs_node_t * tmpfs_node_get(tmpfs_mount_t *); 300 void tmpfs_node_put(tmpfs_mount_t *, tmpfs_node_t *); 301 302 char * tmpfs_strname_alloc(tmpfs_mount_t *, size_t); 303 void tmpfs_strname_free(tmpfs_mount_t *, char *, size_t); 304 bool tmpfs_strname_neqlen(struct componentname *, struct componentname *); 305 306 /* 307 * Ensures that the node pointed by 'node' is a directory and that its 308 * contents are consistent with respect to directories. 309 */ 310 #define TMPFS_VALIDATE_DIR(node) \ 311 KASSERT((node)->tn_vnode == NULL || VOP_ISLOCKED((node)->tn_vnode)); \ 312 KASSERT((node)->tn_type == VDIR); \ 313 KASSERT((node)->tn_size % sizeof(tmpfs_dirent_t) == 0); 314 315 /* 316 * Memory management stuff. 317 */ 318 319 /* Amount of memory pages to reserve for the system. */ 320 #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE) 321 322 /* 323 * Routines to convert VFS structures to tmpfs internal ones. 324 */ 325 326 static inline tmpfs_mount_t * 327 VFS_TO_TMPFS(struct mount *mp) 328 { 329 tmpfs_mount_t *tmp = mp->mnt_data; 330 331 KASSERT(tmp != NULL); 332 return tmp; 333 } 334 335 static inline tmpfs_node_t * 336 VP_TO_TMPFS_DIR(vnode_t *vp) 337 { 338 tmpfs_node_t *node = vp->v_data; 339 340 KASSERT(node != NULL); 341 TMPFS_VALIDATE_DIR(node); 342 return node; 343 } 344 345 #endif /* defined(_KERNEL) */ 346 347 static __inline tmpfs_node_t * 348 VP_TO_TMPFS_NODE(vnode_t *vp) 349 { 350 tmpfs_node_t *node = vp->v_data; 351 #ifdef KASSERT 352 KASSERT(node != NULL); 353 #endif 354 return node; 355 } 356 357 #endif /* _FS_TMPFS_TMPFS_H_ */ 358