xref: /dragonfly/sys/vfs/tmpfs/tmpfs.h (revision e91c5b2636e1fdd0d9e5170f24e5e39987211a49)
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: src/sys/fs/tmpfs/tmpfs.h,v 1.18 2009/10/11 07:03:56 delphij Exp $
33  */
34 
35 #ifndef _VFS_TMPFS_TMPFS_H_
36 #define _VFS_TMPFS_TMPFS_H_
37 
38 /* ---------------------------------------------------------------------
39  * KERNEL-SPECIFIC DEFINITIONS
40  * --------------------------------------------------------------------- */
41 #include <sys/dirent.h>
42 #include <sys/mount.h>
43 #include <sys/tree.h>
44 #include <sys/vnode.h>
45 #include <sys/file.h>
46 #include <sys/lock.h>
47 #include <sys/lockf.h>
48 #include <sys/mutex.h>
49 
50 /* --------------------------------------------------------------------- */
51 #include <sys/malloc.h>
52 #ifdef _KERNEL
53 #include <sys/systm.h>
54 #endif
55 #include <sys/vmmeter.h>
56 #include <vm/swap_pager.h>
57 
58 #ifdef MALLOC_DECLARE
59 MALLOC_DECLARE(M_TMPFSMNT);
60 #endif
61 
62 /* --------------------------------------------------------------------- */
63 
64 /*
65  * Internal representation of a tmpfs directory entry.
66  */
67 struct tmpfs_dirent {
68           RB_ENTRY(tmpfs_dirent)        rb_node;
69           RB_ENTRY(tmpfs_dirent)        rb_cookienode;
70 
71           /* Length of the name stored in this directory entry.  This avoids
72            * the need to recalculate it every time the name is used. */
73           uint16_t            td_namelen;
74 
75           /* The name of the entry, allocated from a string pool.  This
76           * string is not required to be zero-terminated; therefore, the
77           * td_namelen field must always be used when accessing its value. */
78           char                          *td_name;
79 
80           /* Pointer to the node this entry refers to. */
81           struct tmpfs_node   *td_node;
82 };
83 
84 struct tmpfs_dirtree;
85 RB_HEAD(tmpfs_dirtree, tmpfs_dirent);
86 RB_PROTOTYPE(tmpfs_dirtree, tmpfs_dirent, rb_node,
87           tmpfs_dirtree_compare);
88 
89 RB_HEAD(tmpfs_dirtree_cookie, tmpfs_dirent);
90 RB_PROTOTYPE(tmpfs_dirtree_cookie, tmpfs_dirent, rb_cookienode,
91           tmpfs_dirtree_cookie_compare);
92 
93 
94 /*
95  * A directory in tmpfs holds a set of directory entries, which in
96  * turn point to other files (which can be directories themselves).
97  *
98  * In tmpfs, this set is managed by a red-black tree, whose root is defined
99  * by the struct tmpfs_dirtree 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  * Each entry in a directory has a cookie that identifies it.  Cookies
109  * supersede offsets within directories because, given how tmpfs stores
110  * directories in memory, there is no such thing as an offset.  (Emulating
111  * a real offset could be very difficult.)
112  *
113  * The '.', '..' and the end of directory markers have fixed cookies which
114  * cannot collide with the cookies generated by other entries.  The cookies
115  * for the other entries are generated based on the memory address on which
116  * stores their information is stored.
117  *
118  * DragonFly binaries use 64-bit cookies.  We mask-off the signed bit to
119  * ensure that cookie 'offsets' are positive.
120  */
121 #ifdef _KERNEL
122 
123 #define   TMPFS_ROOTINO       ((ino_t)2)
124 
125 #define   TMPFS_DIRCOOKIE_DOT 0
126 #define   TMPFS_DIRCOOKIE_DOTDOT        1
127 #define   TMPFS_DIRCOOKIE_EOF ((off_t)0x7FFFFFFFFFFFFFFFLLU)
128 
129 static __inline
130 off_t
tmpfs_dircookie(struct tmpfs_dirent * de)131 tmpfs_dircookie(struct tmpfs_dirent *de)
132 {
133           return ((off_t)((uintptr_t)de >> 1) & 0x7FFFFFFFFFFFFFFFLLU);
134 }
135 
136 /*
137  * WARNING!  Caller should never try to actually access a tmpfs dirent
138  *             structure from a cookiedir() conversion.  It is used strictly
139  *             for RBTREE operations.
140  */
141 static __inline
142 void *
tmpfs_cookiedir(off_t off)143 tmpfs_cookiedir(off_t off)
144 {
145           return ((void *)((uintptr_t)off << 1));
146 }
147 
148 #endif  /* _KERNEL */
149 
150 /* --------------------------------------------------------------------- */
151 
152 /*
153  * Internal representation of a tmpfs file system node.
154  *
155  * This structure is splitted in two parts: one holds attributes common
156  * to all file types and the other holds data that is only applicable to
157  * a particular type.  The code must be careful to only access those
158  * attributes that are actually allowed by the node's type.
159  */
160 struct tmpfs_node {
161           /* Doubly-linked list entry which links all existing nodes for a
162            * single file system.  This is provided to ease the removal of
163            * all nodes during the unmount operation. */
164           LIST_ENTRY(tmpfs_node)        tn_entries;
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           enum vtype                    tn_type;
171 
172           /* Node identifier. */
173           ino_t                         tn_id;
174 
175           /* Node's internal status.  This is used by several file system
176            * operations to do modifications to the node in a delayed
177            * fashion. */
178           int                           tn_blksize;         /* small file optimization */
179           int                           tn_status;
180 #define   TMPFS_NODE_ACCESSED (1 << 1)
181 #define   TMPFS_NODE_MODIFIED (1 << 2)
182 #define   TMPFS_NODE_CHANGED  (1 << 3)
183 
184           /* The node size.  It does not necessarily match the real amount
185            * of memory consumed by it. */
186           off_t                         tn_size;
187 
188           /* Generic node attributes. */
189           uid_t                         tn_uid;
190           gid_t                         tn_gid;
191           mode_t                        tn_mode;
192           u_int                         tn_flags;
193           nlink_t                       tn_links; /* atomic ops req */
194           long                          tn_atime;
195           long                          tn_atimensec;
196           long                          tn_mtime;
197           long                          tn_mtimensec;
198           long                          tn_ctime;
199           long                          tn_ctimensec;
200           unsigned long                 tn_gen;
201           struct lockf                  tn_advlock;
202 
203           /* As there is a single vnode for each active file within the
204            * system, care has to be taken to avoid allocating more than one
205            * vnode per file.  In order to do this, a bidirectional association
206            * is kept between vnodes and nodes.
207            *
208            * Whenever a vnode is allocated, its v_data field is updated to
209            * point to the node it references.  At the same time, the node's
210            * tn_vnode field is modified to point to the new vnode representing
211            * it.  Further attempts to allocate a vnode for this same node will
212            * result in returning a new reference to the value stored in
213            * tn_vnode.
214            *
215            * May be NULL when the node is unused (that is, no vnode has been
216            * allocated for it or it has been reclaimed). */
217           struct vnode *                tn_vnode;
218 
219           /* interlock to protect structure */
220           struct lock                   tn_interlock;
221 
222           /*
223            * tmpfs vnode state, may specify an allocation in-progress.
224            */
225           int                 tn_vpstate;
226 
227           /* misc data field for different tn_type node */
228           union {
229                     /* Valid when tn_type == VBLK || tn_type == VCHR. */
230                     dev_t                         tn_rdev; /*int32_t ?*/
231 
232                     /* Valid when tn_type == VDIR. */
233                     struct tn_dir {
234                               /*
235                                * Pointer to the parent directory.  The root
236                                * directory has a pointer to itself in this field;
237                                * this property identifies the root node.
238                                */
239                               struct tmpfs_node * tn_parent;
240 
241                               /*
242                                * Directory entries are indexed by name and also
243                                * indexed by cookie.
244                                */
245                               struct tmpfs_dirtree                    tn_dirtree;
246                               struct tmpfs_dirtree_cookie   tn_cookietree;
247                     } tn_dir;
248 
249                     /* Valid when tn_type == VLNK. */
250                     /* The link's target, allocated from a string pool. */
251                     char *                        tn_link;
252 
253                     /*
254                      * Valid when tn_type == VREG.
255                      *
256                      * aobj is used as backing store for the vnode object.  It
257                      * typically only contains swap assignments, but we also use
258                      * it to save the vnode object's vm_page's when the vnode
259                      * becomes inactive.
260                      */
261                     struct tn_reg {
262                               vm_object_t                   tn_aobj;
263                               size_t                        tn_aobj_pages;
264                               int                           tn_pages_in_aobj;
265                     } tn_reg;
266 
267                     /* Valid when tn_type = VFIFO */
268                     struct tn_fifo {
269                               int (*tn_fo_read)  (struct file *fp, struct uio *uio,
270                                       struct ucred *cred, int flags);
271                               int (*tn_fo_write) (struct file *fp, struct uio *uio,
272                                       struct ucred *cred, int flags);
273                     } tn_fifo;
274           } tn_spec;
275 };
276 
277 /* Only userspace needs this */
278 #define VTOI(vp)    ((struct tmpfs_node *)(vp)->v_data)
279 
280 #ifdef _KERNEL
281 LIST_HEAD(tmpfs_node_list, tmpfs_node);
282 
283 #define tn_rdev tn_spec.tn_rdev
284 #define tn_dir tn_spec.tn_dir
285 #define tn_link tn_spec.tn_link
286 #define tn_reg tn_spec.tn_reg
287 #define tn_fifo tn_spec.tn_fifo
288 
289 #define TMPFS_NODE_LOCK(node) lockmgr(&(node)->tn_interlock, LK_EXCLUSIVE|LK_RETRY)
290 #define TMPFS_NODE_LOCK_SH(node) lockmgr(&(node)->tn_interlock, LK_SHARED|LK_RETRY)
291 #define TMPFS_NODE_UNLOCK(node) lockmgr(&(node)->tn_interlock, LK_RELEASE)
292 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
293 
294 #ifdef INVARIANTS
295 #define TMPFS_ASSERT_LOCKED(node) do {                                          \
296                     KKASSERT(node != NULL);                                               \
297                     KKASSERT(node->tn_vnode != NULL);                           \
298                     if (!vn_islocked(node->tn_vnode) &&                         \
299                         (lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE ))             \
300                               panic("tmpfs: node is not locked: %p", node);     \
301           } while (0)
302 #define TMPFS_ASSERT_ELOCKED(node) do {                                         \
303                     KKASSERT((node) != NULL);                                   \
304                     KKASSERT(lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE);                    \
305           } while (0)
306 #else
307 #define TMPFS_ASSERT_LOCKED(node) (void)0
308 #define TMPFS_ASSERT_ELOCKED(node) (void)0
309 #endif  /* INVARIANTS */
310 
311 #define TMPFS_VNODE_DOOMED    0x0001
312 /* --------------------------------------------------------------------- */
313 
314 /*
315  * Internal representation of a tmpfs mount point.
316  */
317 struct tmpfs_mount {
318           struct mount                  *tm_mount;
319 
320           /* Maximum number of memory pages available for use by the file
321            * system, set during mount time.  This variable must never be
322            * used directly as it may be bigger than the current amount of
323            * free memory; in the extreme case, it will hold the SIZE_MAX
324            * value.  Instead, use the TMPFS_PAGES_MAX macro. */
325           long                          tm_pages_max;
326 
327           /* Number of pages in use by the file system.  Cannot be bigger
328            * than the value returned by TMPFS_PAGES_MAX in any case. */
329           long                          tm_pages_used;
330 
331           /* Pointer to the node representing the root directory of this
332            * file system. */
333           struct tmpfs_node * tm_root;
334 
335           /* Maximum number of possible nodes for this file system; set
336            * during mount time.  We need a hard limit on the maximum number
337            * of nodes to avoid allocating too much of them; their objects
338            * cannot be released until the file system is unmounted.
339            * Otherwise, we could easily run out of memory by creating lots
340            * of empty files and then simply removing them. */
341           ino_t                         tm_nodes_max;
342 
343           /* Number of nodes currently that are in use. */
344           ino_t                         tm_nodes_inuse;
345 
346           /* maximum representable file size */
347           u_int64_t           tm_maxfilesize;
348 
349           /* Nodes are organized in two different lists.  The used list
350            * contains all nodes that are currently used by the file system;
351            * i.e., they refer to existing files.  The available list contains
352            * all nodes that are currently available for use by new files.
353            * Nodes must be kept in this list (instead of deleting them)
354            * because we need to keep track of their generation number (tn_gen
355            * field).
356            *
357            * Note that nodes are lazily allocated: if the available list is
358            * empty and we have enough space to create more nodes, they will be
359            * created and inserted in the used list.  Once these are released,
360            * they will go into the available list, remaining alive until the
361            * file system is unmounted. */
362           struct tmpfs_node_list        tm_nodes_used;
363 
364           /* Per-mount malloc zones for tmpfs nodes, names, and dirents */
365           struct malloc_type  *tm_node_zone_obj;
366           struct malloc_type  *tm_dirent_zone_obj;
367           struct malloc_type  *tm_name_zone;
368 
369           ino_t                         tm_ino;
370           int                           tm_flags;
371 
372           struct netexport    tm_export;
373 
374           struct mount                  *tm_mnt;
375 };
376 
377 #define TMPFS_LOCK(tm) lwkt_gettoken(&(tm)->tm_mount->mnt_token)
378 #define TMPFS_UNLOCK(tm) lwkt_reltoken(&(tm)->tm_mount->mnt_token)
379 
380 /* --------------------------------------------------------------------- */
381 
382 /*
383  * This structure maps a file identifier to a tmpfs node.  Used by the
384  * NFS code.
385  */
386 struct tmpfs_fid {
387           uint16_t            tf_len;
388           uint16_t            tf_pad;
389           ino_t                         tf_id;
390           unsigned long                 tf_gen;
391 } __packed;
392 
393 /* --------------------------------------------------------------------- */
394 
395 /*
396  * Prototypes for tmpfs_subr.c.
397  */
398 
399 int       tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
400               uid_t uid, gid_t gid, mode_t mode, char *, int, int,
401               struct tmpfs_node **);
402 void      tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
403 int       tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
404               const char *, uint16_t, struct tmpfs_dirent **);
405 void      tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
406 int       tmpfs_alloc_vp(struct mount *, struct tmpfs_node *,
407               struct tmpfs_node *, int, struct vnode **);
408 int       tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
409               struct namecache *, struct ucred *, char *);
410 void      tmpfs_dir_attach_locked(struct tmpfs_node *, struct tmpfs_dirent *);
411 void      tmpfs_dir_detach_locked(struct tmpfs_node *, struct tmpfs_dirent *);
412 struct tmpfs_dirent *         tmpfs_dir_lookup(struct tmpfs_node *node,
413                                   struct tmpfs_node *f,
414                                   struct namecache *ncp);
415 int       tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
416 int       tmpfs_dir_getdotdotdent(struct tmpfs_mount *,
417                                   struct tmpfs_node *, struct uio *);
418 struct tmpfs_dirent *         tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t,
419                                   int);
420 int       tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
421 int       tmpfs_reg_resize(struct vnode *, off_t, int);
422 int       tmpfs_chflags(struct vnode *, u_long, struct ucred *);
423 int       tmpfs_chmod(struct vnode *, mode_t, struct ucred *);
424 int       tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *);
425 int       tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *);
426 int       tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
427               int, struct ucred *);
428 void      tmpfs_itimes(struct vnode *, const struct timespec *,
429               const struct timespec *);
430 
431 void      tmpfs_node_init(struct tmpfs_node *node);
432 void      tmpfs_node_uninit(struct tmpfs_node *node);
433 void      tmpfs_update(struct vnode *);
434 int       tmpfs_truncate(struct vnode *, off_t);
435 void      tmpfs_lock4(struct tmpfs_node *node1, struct tmpfs_node *node2,
436                     struct tmpfs_node *node3, struct tmpfs_node *node4);
437 void      tmpfs_unlock4(struct tmpfs_node *node1, struct tmpfs_node *node2,
438                     struct tmpfs_node *node3, struct tmpfs_node *node4);
439 
440 /* --------------------------------------------------------------------- */
441 
442 /*
443  * Convenience macros to simplify some logical expressions.
444  */
445 #define IMPLIES(a, b) (!(a) || (b))
446 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
447 
448 /* --------------------------------------------------------------------- */
449 
450 /*
451  * Checks that the directory entry pointed by 'de' matches the name 'name'
452  * with a length of 'len'.
453  */
454 #define TMPFS_DIRENT_MATCHES(de, name, len) \
455     (de->td_namelen == (uint16_t)len && \
456     bcmp((de)->td_name, (name), (de)->td_namelen) == 0)
457 
458 /* --------------------------------------------------------------------- */
459 
460 /*
461  * Ensures that the node pointed by 'node' is a directory and that its
462  * contents are consistent with respect to directories.
463  */
464 #define TMPFS_VALIDATE_DIR(node) do {   \
465     KKASSERT((node)->tn_type == VDIR);  \
466     KKASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
467 } while(0)
468 
469 /* --------------------------------------------------------------------- */
470 
471 /*
472  * Macros/functions to convert from generic data structures to tmpfs
473  * specific ones.  Kernel code use VP_TO_TMPFS_NODE() instead of VTOI().
474  */
475 
476 static inline
477 struct tmpfs_mount *
VFS_TO_TMPFS(struct mount * mp)478 VFS_TO_TMPFS(struct mount *mp)
479 {
480           struct tmpfs_mount *tmp;
481 
482           KKASSERT((mp) != NULL && (mp)->mnt_data != NULL);
483           tmp = (struct tmpfs_mount *)(mp)->mnt_data;
484           return tmp;
485 }
486 
487 static inline
488 struct tmpfs_node *
VP_TO_TMPFS_NODE(struct vnode * vp)489 VP_TO_TMPFS_NODE(struct vnode *vp)
490 {
491           struct tmpfs_node *node;
492 
493           KKASSERT((vp) != NULL && (vp)->v_data != NULL);
494           node = (struct tmpfs_node *)vp->v_data;
495           return node;
496 }
497 
498 static inline
499 struct tmpfs_node *
VP_TO_TMPFS_DIR(struct vnode * vp)500 VP_TO_TMPFS_DIR(struct vnode *vp)
501 {
502           struct tmpfs_node *node;
503 
504           node = VP_TO_TMPFS_NODE(vp);
505           TMPFS_VALIDATE_DIR(node);
506           return node;
507 }
508 
509 /* --------------------------------------------------------------------- */
510 /*
511  * buffer cache size
512  */
513 #define TMPFS_BLKSIZE         16384                         /* buffer cache size*/
514 #define TMPFS_BLKMASK         (TMPFS_BLKSIZE - 1)
515 #define TMPFS_BLKMASK64       ((off_t)(TMPFS_BLKSIZE - 1))
516 #endif /* _KERNEL */
517 
518 #endif /* _VFS_TMPFS_TMPFS_H_ */
519