FreeBSD/Linux Kernel Cross Reference
sys/fs/tmpfs/tmpfs.h
1 /* $NetBSD: tmpfs.h,v 1.26 2007/02/22 06:37:00 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2005, 2006 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 * $FreeBSD: releng/10.0/sys/fs/tmpfs/tmpfs.h 253967 2013-08-05 18:53:59Z kib $
33 */
34
35 #ifndef _FS_TMPFS_TMPFS_H_
36 #define _FS_TMPFS_TMPFS_H_
37
38 /* ---------------------------------------------------------------------
39 * KERNEL-SPECIFIC DEFINITIONS
40 * --------------------------------------------------------------------- */
41 #include <sys/dirent.h>
42 #include <sys/mount.h>
43 #include <sys/queue.h>
44 #include <sys/vnode.h>
45 #include <sys/file.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48
49 /* --------------------------------------------------------------------- */
50 #include <sys/malloc.h>
51 #include <sys/systm.h>
52 #include <sys/tree.h>
53 #include <sys/vmmeter.h>
54 #include <vm/swap_pager.h>
55
56 MALLOC_DECLARE(M_TMPFSMNT);
57 MALLOC_DECLARE(M_TMPFSNAME);
58
59 /* --------------------------------------------------------------------- */
60
61 /*
62 * Internal representation of a tmpfs directory entry.
63 */
64
65 LIST_HEAD(tmpfs_dir_duphead, tmpfs_dirent);
66
67 struct tmpfs_dirent {
68 /*
69 * Depending on td_cookie flag entry can be of 3 types:
70 * - regular -- no hash collisions, stored in RB-Tree
71 * - duphead -- synthetic linked list head for dup entries
72 * - dup -- stored in linked list instead of RB-Tree
73 */
74 union {
75 /* regular and duphead entry types */
76 RB_ENTRY(tmpfs_dirent) td_entries;
77
78 /* dup entry type */
79 struct {
80 LIST_ENTRY(tmpfs_dirent) entries;
81 LIST_ENTRY(tmpfs_dirent) index_entries;
82 } td_dup;
83 } uh;
84
85 uint32_t td_cookie;
86 uint32_t td_hash;
87 u_int td_namelen;
88
89 /* Pointer to the node this entry refers to. In case this field
90 * is NULL, the node is a whiteout. */
91 struct tmpfs_node * td_node;
92
93 union {
94 /*
95 * The name of the entry, allocated from a string pool. This
96 * string is not required to be zero-terminated.
97 */
98 char * td_name; /* regular, dup */
99 struct tmpfs_dir_duphead td_duphead; /* duphead */
100 } ud;
101 };
102
103 /* A directory in tmpfs holds a list of directory entries, which in
104 * turn point to other files (which can be directories themselves).
105 *
106 * In tmpfs, this list is managed by a RB-Tree, whose head is defined by
107 * the struct tmpfs_dir type.
108 *
109 * It is important to notice that directories do not have entries for . and
110 * .. as other file systems do. These can be generated when requested
111 * based on information available by other means, such as the pointer to
112 * the node itself in the former case or the pointer to the parent directory
113 * in the latter case. This is done to simplify tmpfs's code and, more
114 * importantly, to remove redundancy. */
115 RB_HEAD(tmpfs_dir, tmpfs_dirent);
116
117 /* Each entry in a directory has a cookie that identifies it. Cookies
118 * supersede offsets within directories because, given how tmpfs stores
119 * directories in memory, there is no such thing as an offset.
120 *
121 * The '.', '..' and the end of directory markers have fixed cookies which
122 * cannot collide with the cookies generated by other entries. The cookies
123 * for the other entries are generated based on the file name hash value or
124 * unique number in case of name hash collision.
125 *
126 * To preserve compatibility cookies are limited to 31 bits.
127 */
128
129 #define TMPFS_DIRCOOKIE_DOT 0
130 #define TMPFS_DIRCOOKIE_DOTDOT 1
131 #define TMPFS_DIRCOOKIE_EOF 2
132 #define TMPFS_DIRCOOKIE_MASK ((off_t)0x3fffffffU)
133 #define TMPFS_DIRCOOKIE_MIN ((off_t)0x00000004U)
134 #define TMPFS_DIRCOOKIE_DUP ((off_t)0x40000000U)
135 #define TMPFS_DIRCOOKIE_DUPHEAD ((off_t)0x80000000U)
136 #define TMPFS_DIRCOOKIE_DUP_MIN TMPFS_DIRCOOKIE_DUP
137 #define TMPFS_DIRCOOKIE_DUP_MAX \
138 (TMPFS_DIRCOOKIE_DUP | TMPFS_DIRCOOKIE_MASK)
139
140 /* --------------------------------------------------------------------- */
141
142 /*
143 * Internal representation of a tmpfs file system node.
144 *
145 * This structure is splitted in two parts: one holds attributes common
146 * to all file types and the other holds data that is only applicable to
147 * a particular type. The code must be careful to only access those
148 * attributes that are actually allowed by the node's type.
149 *
150 *
151 * Below is the key of locks used to protected the fields in the following
152 * structures.
153 *
154 */
155 struct tmpfs_node {
156 /* Doubly-linked list entry which links all existing nodes for a
157 * single file system. This is provided to ease the removal of
158 * all nodes during the unmount operation. */
159 LIST_ENTRY(tmpfs_node) tn_entries;
160
161 /* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
162 * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode
163 * types instead of a custom enumeration is to make things simpler
164 * and faster, as we do not need to convert between two types. */
165 enum vtype tn_type;
166
167 /* Node identifier. */
168 ino_t tn_id;
169
170 /* Node's internal status. This is used by several file system
171 * operations to do modifications to the node in a delayed
172 * fashion. */
173 int tn_status;
174 #define TMPFS_NODE_ACCESSED (1 << 1)
175 #define TMPFS_NODE_MODIFIED (1 << 2)
176 #define TMPFS_NODE_CHANGED (1 << 3)
177
178 /* The node size. It does not necessarily match the real amount
179 * of memory consumed by it. */
180 off_t tn_size;
181
182 /* Generic node attributes. */
183 uid_t tn_uid;
184 gid_t tn_gid;
185 mode_t tn_mode;
186 u_long tn_flags;
187 nlink_t tn_links;
188 struct timespec tn_atime;
189 struct timespec tn_mtime;
190 struct timespec tn_ctime;
191 struct timespec tn_birthtime;
192 unsigned long tn_gen;
193
194 /* As there is a single vnode for each active file within the
195 * system, care has to be taken to avoid allocating more than one
196 * vnode per file. In order to do this, a bidirectional association
197 * is kept between vnodes and nodes.
198 *
199 * Whenever a vnode is allocated, its v_data field is updated to
200 * point to the node it references. At the same time, the node's
201 * tn_vnode field is modified to point to the new vnode representing
202 * it. Further attempts to allocate a vnode for this same node will
203 * result in returning a new reference to the value stored in
204 * tn_vnode.
205 *
206 * May be NULL when the node is unused (that is, no vnode has been
207 * allocated for it or it has been reclaimed). */
208 struct vnode * tn_vnode;
209
210 /* interlock to protect tn_vpstate */
211 struct mtx tn_interlock;
212
213 /* Identify if current node has vnode assiocate with
214 * or allocating vnode.
215 */
216 int tn_vpstate;
217
218 /* misc data field for different tn_type node */
219 union {
220 /* Valid when tn_type == VBLK || tn_type == VCHR. */
221 dev_t tn_rdev;
222
223 /* Valid when tn_type == VDIR. */
224 struct tn_dir {
225 /* Pointer to the parent directory. The root
226 * directory has a pointer to itself in this field;
227 * this property identifies the root node. */
228 struct tmpfs_node * tn_parent;
229
230 /* Head of a tree that links the contents of
231 * the directory together. */
232 struct tmpfs_dir tn_dirhead;
233
234 /* Head of a list the contains fake directory entries
235 * heads, i.e. entries with TMPFS_DIRCOOKIE_DUPHEAD
236 * flag. */
237 struct tmpfs_dir_duphead tn_dupindex;
238
239 /* Number and pointer of the first directory entry
240 * returned by the readdir operation if it were
241 * called again to continue reading data from the
242 * same directory as before. This is used to speed
243 * up reads of long directories, assuming that no
244 * more than one read is in progress at a given time.
245 * Otherwise, these values are discarded. */
246 off_t tn_readdir_lastn;
247 struct tmpfs_dirent * tn_readdir_lastp;
248 } tn_dir;
249
250 /* Valid when tn_type == VLNK. */
251 /* The link's target, allocated from a string pool. */
252 char * tn_link;
253
254 /* Valid when tn_type == VREG. */
255 struct tn_reg {
256 /* The contents of regular files stored in a tmpfs
257 * file system are represented by a single anonymous
258 * memory object (aobj, for short). The aobj provides
259 * direct access to any position within the file,
260 * because its contents are always mapped in a
261 * contiguous region of virtual memory. It is a task
262 * of the memory management subsystem (see uvm(9)) to
263 * issue the required page ins or page outs whenever
264 * a position within the file is accessed. */
265 vm_object_t tn_aobj;
266
267 }tn_reg;
268
269 /* Valid when tn_type = VFIFO */
270 struct tn_fifo {
271 fo_rdwr_t *tn_fo_read;
272 fo_rdwr_t *tn_fo_write;
273 }tn_fifo;
274 }tn_spec;
275 };
276 LIST_HEAD(tmpfs_node_list, tmpfs_node);
277
278 #define tn_rdev tn_spec.tn_rdev
279 #define tn_dir tn_spec.tn_dir
280 #define tn_link tn_spec.tn_link
281 #define tn_reg tn_spec.tn_reg
282 #define tn_fifo tn_spec.tn_fifo
283
284 #define TMPFS_NODE_LOCK(node) mtx_lock(&(node)->tn_interlock)
285 #define TMPFS_NODE_UNLOCK(node) mtx_unlock(&(node)->tn_interlock)
286 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
287
288 #ifdef INVARIANTS
289 #define TMPFS_ASSERT_LOCKED(node) do { \
290 MPASS(node != NULL); \
291 MPASS(node->tn_vnode != NULL); \
292 if (!VOP_ISLOCKED(node->tn_vnode) && \
293 !mtx_owned(TMPFS_NODE_MTX(node))) \
294 panic("tmpfs: node is not locked: %p", node); \
295 } while (0)
296 #define TMPFS_ASSERT_ELOCKED(node) do { \
297 MPASS((node) != NULL); \
298 MPASS((node)->tn_vnode != NULL); \
299 mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED); \
300 ASSERT_VOP_LOCKED((node)->tn_vnode, "tmpfs"); \
301 } while (0)
302 #else
303 #define TMPFS_ASSERT_LOCKED(node) (void)0
304 #define TMPFS_ASSERT_ELOCKED(node) (void)0
305 #endif
306
307 #define TMPFS_VNODE_ALLOCATING 1
308 #define TMPFS_VNODE_WANT 2
309 #define TMPFS_VNODE_DOOMED 4
310 #define TMPFS_VNODE_WRECLAIM 8
311 /* --------------------------------------------------------------------- */
312
313 /*
314 * Internal representation of a tmpfs mount point.
315 */
316 struct tmpfs_mount {
317 /* Maximum number of memory pages available for use by the file
318 * system, set during mount time. This variable must never be
319 * used directly as it may be bigger than the current amount of
320 * free memory; in the extreme case, it will hold the SIZE_MAX
321 * value. */
322 size_t tm_pages_max;
323
324 /* Number of pages in use by the file system. */
325 size_t tm_pages_used;
326
327 /* Pointer to the node representing the root directory of this
328 * file system. */
329 struct tmpfs_node * tm_root;
330
331 /* Maximum number of possible nodes for this file system; set
332 * during mount time. We need a hard limit on the maximum number
333 * of nodes to avoid allocating too much of them; their objects
334 * cannot be released until the file system is unmounted.
335 * Otherwise, we could easily run out of memory by creating lots
336 * of empty files and then simply removing them. */
337 ino_t tm_nodes_max;
338
339 /* unrhdr used to allocate inode numbers */
340 struct unrhdr * tm_ino_unr;
341
342 /* Number of nodes currently that are in use. */
343 ino_t tm_nodes_inuse;
344
345 /* maximum representable file size */
346 u_int64_t tm_maxfilesize;
347
348 /* Nodes are organized in two different lists. The used list
349 * contains all nodes that are currently used by the file system;
350 * i.e., they refer to existing files. The available list contains
351 * all nodes that are currently available for use by new files.
352 * Nodes must be kept in this list (instead of deleting them)
353 * because we need to keep track of their generation number (tn_gen
354 * field).
355 *
356 * Note that nodes are lazily allocated: if the available list is
357 * empty and we have enough space to create more nodes, they will be
358 * created and inserted in the used list. Once these are released,
359 * they will go into the available list, remaining alive until the
360 * file system is unmounted. */
361 struct tmpfs_node_list tm_nodes_used;
362
363 /* All node lock to protect the node list and tmp_pages_used */
364 struct mtx allnode_lock;
365
366 /* Pools used to store file system meta data. These are not shared
367 * across several instances of tmpfs for the reasons described in
368 * tmpfs_pool.c. */
369 uma_zone_t tm_dirent_pool;
370 uma_zone_t tm_node_pool;
371
372 /* Read-only status. */
373 int tm_ronly;
374 };
375 #define TMPFS_LOCK(tm) mtx_lock(&(tm)->allnode_lock)
376 #define TMPFS_UNLOCK(tm) mtx_unlock(&(tm)->allnode_lock)
377
378 /* --------------------------------------------------------------------- */
379
380 /*
381 * This structure maps a file identifier to a tmpfs node. Used by the
382 * NFS code.
383 */
384 struct tmpfs_fid {
385 uint16_t tf_len;
386 uint16_t tf_pad;
387 ino_t tf_id;
388 unsigned long tf_gen;
389 };
390
391 /* --------------------------------------------------------------------- */
392
393 #ifdef _KERNEL
394 /*
395 * Prototypes for tmpfs_subr.c.
396 */
397
398 int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
399 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
400 char *, dev_t, struct tmpfs_node **);
401 void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
402 int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
403 const char *, u_int, struct tmpfs_dirent **);
404 void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
405 void tmpfs_dirent_init(struct tmpfs_dirent *, const char *, u_int);
406 void tmpfs_destroy_vobject(struct vnode *vp, vm_object_t obj);
407 int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
408 struct vnode **);
409 void tmpfs_free_vp(struct vnode *);
410 int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
411 struct componentname *, char *);
412 void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
413 void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
414 void tmpfs_dir_destroy(struct tmpfs_mount *, struct tmpfs_node *);
415 struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node,
416 struct tmpfs_node *f,
417 struct componentname *cnp);
418 int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, int,
419 u_long *, int *);
420 int tmpfs_dir_whiteout_add(struct vnode *, struct componentname *);
421 void tmpfs_dir_whiteout_remove(struct vnode *, struct componentname *);
422 int tmpfs_reg_resize(struct vnode *, off_t, boolean_t);
423 int tmpfs_chflags(struct vnode *, u_long, struct ucred *, struct thread *);
424 int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct thread *);
425 int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
426 struct thread *);
427 int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct thread *);
428 int tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
429 struct timespec *, int, struct ucred *, struct thread *);
430 void tmpfs_itimes(struct vnode *, const struct timespec *,
431 const struct timespec *);
432
433 void tmpfs_update(struct vnode *);
434 int tmpfs_truncate(struct vnode *, off_t);
435
436 /* --------------------------------------------------------------------- */
437
438 /*
439 * Convenience macros to simplify some logical expressions.
440 */
441 #define IMPLIES(a, b) (!(a) || (b))
442 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
443
444 /* --------------------------------------------------------------------- */
445
446 /*
447 * Checks that the directory entry pointed by 'de' matches the name 'name'
448 * with a length of 'len'.
449 */
450 #define TMPFS_DIRENT_MATCHES(de, name, len) \
451 (de->td_namelen == len && \
452 bcmp((de)->ud.td_name, (name), (de)->td_namelen) == 0)
453
454 /* --------------------------------------------------------------------- */
455
456 /*
457 * Ensures that the node pointed by 'node' is a directory and that its
458 * contents are consistent with respect to directories.
459 */
460 #define TMPFS_VALIDATE_DIR(node) do { \
461 MPASS((node)->tn_type == VDIR); \
462 MPASS((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
463 } while (0)
464
465 /* --------------------------------------------------------------------- */
466
467 /*
468 * Memory management stuff.
469 */
470
471 /*
472 * Amount of memory pages to reserve for the system (e.g., to not use by
473 * tmpfs).
474 */
475 #define TMPFS_PAGES_MINRESERVED (4 * 1024 * 1024 / PAGE_SIZE)
476
477 size_t tmpfs_mem_avail(void);
478
479 size_t tmpfs_pages_used(struct tmpfs_mount *tmp);
480
481 #endif
482
483 /* --------------------------------------------------------------------- */
484
485 /*
486 * Macros/functions to convert from generic data structures to tmpfs
487 * specific ones.
488 */
489
490 static inline
491 struct tmpfs_mount *
492 VFS_TO_TMPFS(struct mount *mp)
493 {
494 struct tmpfs_mount *tmp;
495
496 MPASS((mp) != NULL && (mp)->mnt_data != NULL);
497 tmp = (struct tmpfs_mount *)(mp)->mnt_data;
498 return tmp;
499 }
500
501 static inline
502 struct tmpfs_node *
503 VP_TO_TMPFS_NODE(struct vnode *vp)
504 {
505 struct tmpfs_node *node;
506
507 MPASS((vp) != NULL && (vp)->v_data != NULL);
508 node = (struct tmpfs_node *)vp->v_data;
509 return node;
510 }
511
512 static inline
513 struct tmpfs_node *
514 VP_TO_TMPFS_DIR(struct vnode *vp)
515 {
516 struct tmpfs_node *node;
517
518 node = VP_TO_TMPFS_NODE(vp);
519 TMPFS_VALIDATE_DIR(node);
520 return node;
521 }
522
523 #endif /* _FS_TMPFS_TMPFS_H_ */
Cache object: fbfa5daf37bccdfb93ecd66ed216d650
|