Merge branch 'vendor/MDOCML'
[dragonfly.git] / sys / vfs / 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: 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 #include <sys/objcache.h>
50
51 /* --------------------------------------------------------------------- */
52 #include <sys/malloc.h>
53 #ifdef _KERNEL
54 #include <sys/systm.h>
55 #endif
56 #include <sys/vmmeter.h>
57 #include <vm/swap_pager.h>
58
59 MALLOC_DECLARE(M_TMPFSMNT);
60
61 /* --------------------------------------------------------------------- */
62
63 /*
64  * Internal representation of a tmpfs directory entry.
65  */
66 struct tmpfs_dirent {
67         RB_ENTRY(tmpfs_dirent)  rb_node;
68
69         /* Length of the name stored in this directory entry.  This avoids
70          * the need to recalculate it every time the name is used. */
71         uint16_t                td_namelen;
72
73         /* The name of the entry, allocated from a string pool.  This
74         * string is not required to be zero-terminated; therefore, the
75         * td_namelen field must always be used when accessing its value. */
76         char                    *td_name;
77
78         /* Pointer to the node this entry refers to. */
79         struct tmpfs_node       *td_node;
80 };
81
82 struct tmpfs_dirtree;
83 RB_HEAD(tmpfs_dirtree, tmpfs_dirent);
84 RB_PROTOTYPE(tmpfs_dirtree, tmpfs_dirent, rb_node,
85         tmpfs_dirtree_compare);
86
87
88 /* A directory in tmpfs holds a set of directory entries, which in
89  * turn point to other files (which can be directories themselves).
90  *
91  * In tmpfs, this set is managed by a red-black tree, whose root is defined
92  * by the struct tmpfs_dirtree type.
93  *
94  * It is important to notice that directories do not have entries for . and
95  * .. as other file systems do.  These can be generated when requested
96  * based on information available by other means, such as the pointer to
97  * the node itself in the former case or the pointer to the parent directory
98  * in the latter case.  This is done to simplify tmpfs's code and, more
99  * importantly, to remove redundancy. */
100
101 /* Each entry in a directory has a cookie that identifies it.  Cookies
102  * supersede offsets within directories because, given how tmpfs stores
103  * directories in memory, there is no such thing as an offset.  (Emulating
104  * a real offset could be very difficult.)
105  *
106  * The '.', '..' and the end of directory markers have fixed cookies which
107  * cannot collide with the cookies generated by other entries.  The cookies
108  * for the other entries are generated based on the memory address on which
109  * stores their information is stored.
110  *
111  * Ideally, using the entry's memory pointer as the cookie would be enough
112  * to represent it and it wouldn't cause collisions in any system.
113  * Unfortunately, this results in "offsets" with very large values which
114  * later raise problems in the Linux compatibility layer (and maybe in other
115  * places) as described in PR kern/32034.  Hence we need to workaround this
116  * with a rather ugly hack.
117  *
118  * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t
119  * set to 'long', which is a 32-bit *signed* long integer.  Regardless of
120  * the macro value, GLIBC (2.3 at least) always uses the getdents64
121  * system call (when calling readdir) which internally returns off64_t
122  * offsets.  In order to make 32-bit binaries work, *GLIBC* converts the
123  * 64-bit values returned by the kernel to 32-bit ones and aborts with
124  * EOVERFLOW if the conversion results in values that won't fit in 32-bit
125  * integers (which it assumes is because the directory is extremely large).
126  * This wouldn't cause problems if we were dealing with unsigned integers,
127  * but as we have signed integers, this check fails due to sign expansion.
128  *
129  * For example, consider that the kernel returns the 0xc1234567 cookie to
130  * userspace in a off64_t integer.  Later on, GLIBC casts this value to
131  * off_t (remember, signed) with code similar to:
132  *     system call returns the offset in kernel_value;
133  *     off_t casted_value = kernel_value;
134  *     if (sizeof(off_t) != sizeof(off64_t) &&
135  *         kernel_value != casted_value)
136  *             error!
137  * In this case, casted_value still has 0xc1234567, but when it is compared
138  * for equality against kernel_value, it is promoted to a 64-bit integer and
139  * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567.
140  * Then, GLIBC assumes this is because the directory is very large.
141  *
142  * Given that all the above happens in user-space, we have no control over
143  * it; therefore we must workaround the issue here.  We do this by
144  * truncating the pointer value to a 32-bit integer and hope that there
145  * won't be collisions.  In fact, this will not cause any problems in
146  * 32-bit platforms but some might arise in 64-bit machines (I'm not sure
147  * if they can happen at all in practice).
148  *
149  * XXX A nicer solution shall be attempted. */
150 #ifdef _KERNEL
151 #define TMPFS_DIRCOOKIE_DOT     0
152 #define TMPFS_DIRCOOKIE_DOTDOT  1
153 #define TMPFS_DIRCOOKIE_EOF     2
154 static __inline
155 off_t
156 tmpfs_dircookie(struct tmpfs_dirent *de)
157 {
158         off_t cookie;
159
160         cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF;
161         KKASSERT(cookie != TMPFS_DIRCOOKIE_DOT);
162         KKASSERT(cookie != TMPFS_DIRCOOKIE_DOTDOT);
163         KKASSERT(cookie != TMPFS_DIRCOOKIE_EOF);
164
165         return cookie;
166 }
167 #endif
168
169 /* --------------------------------------------------------------------- */
170
171 /*
172  * Internal representation of a tmpfs file system node.
173  *
174  * This structure is splitted in two parts: one holds attributes common
175  * to all file types and the other holds data that is only applicable to
176  * a particular type.  The code must be careful to only access those
177  * attributes that are actually allowed by the node's type.
178  */
179 struct tmpfs_node {
180         /* Doubly-linked list entry which links all existing nodes for a
181          * single file system.  This is provided to ease the removal of
182          * all nodes during the unmount operation. */
183         LIST_ENTRY(tmpfs_node)  tn_entries;
184
185         /* The node's type.  Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
186          * 'VLNK', 'VREG' and 'VSOCK' is allowed.  The usage of vnode
187          * types instead of a custom enumeration is to make things simpler
188          * and faster, as we do not need to convert between two types. */
189         enum vtype              tn_type;
190
191         /* Node identifier. */
192         ino_t                   tn_id;
193
194         /* Node's internal status.  This is used by several file system
195          * operations to do modifications to the node in a delayed
196          * fashion. */
197         int                     tn_status;
198 #define TMPFS_NODE_ACCESSED     (1 << 1)
199 #define TMPFS_NODE_MODIFIED     (1 << 2)
200 #define TMPFS_NODE_CHANGED      (1 << 3)
201
202         /* The node size.  It does not necessarily match the real amount
203          * of memory consumed by it. */
204         off_t                   tn_size;
205
206         /* Generic node attributes. */
207         uid_t                   tn_uid;
208         gid_t                   tn_gid;
209         mode_t                  tn_mode;
210         int                     tn_flags;
211         nlink_t                 tn_links;   /* requires mnt_token protection */
212         int32_t                 tn_atime;
213         int32_t                 tn_atimensec;
214         int32_t                 tn_mtime;
215         int32_t                 tn_mtimensec;
216         int32_t                 tn_ctime;
217         int32_t                 tn_ctimensec;
218         unsigned long           tn_gen;
219         struct lockf            tn_advlock;
220
221         /* As there is a single vnode for each active file within the
222          * system, care has to be taken to avoid allocating more than one
223          * vnode per file.  In order to do this, a bidirectional association
224          * is kept between vnodes and nodes.
225          *
226          * Whenever a vnode is allocated, its v_data field is updated to
227          * point to the node it references.  At the same time, the node's
228          * tn_vnode field is modified to point to the new vnode representing
229          * it.  Further attempts to allocate a vnode for this same node will
230          * result in returning a new reference to the value stored in
231          * tn_vnode.
232          *
233          * May be NULL when the node is unused (that is, no vnode has been
234          * allocated for it or it has been reclaimed). */
235         struct vnode *          tn_vnode;
236
237         /* interlock to protect tn_vpstate */
238         struct lock             tn_interlock;
239
240         /* Identify if current node has vnode assiocate with
241          * or allocating vnode.
242          */
243         int             tn_vpstate;
244
245         /* misc data field for different tn_type node */
246         union {
247                 /* Valid when tn_type == VBLK || tn_type == VCHR. */
248                 dev_t                   tn_rdev; /*int32_t ?*/
249
250                 /* Valid when tn_type == VDIR. */
251                 struct tn_dir {
252                         /* Pointer to the parent directory.  The root
253                          * directory has a pointer to itself in this field;
254                          * this property identifies the root node. */
255                         struct tmpfs_node *     tn_parent;
256
257                         /* Root of a red-black tree that links the contents of
258                          * the directory together.  See above for a
259                          * description of its contents. */
260                         struct tmpfs_dirtree    tn_dirtree;
261
262                         /* Number and pointer of the first directory entry
263                          * returned by the readdir operation if it were
264                          * called again to continue reading data from the
265                          * same directory as before.  This is used to speed
266                          * up reads of long directories, assuming that no
267                          * more than one read is in progress at a given time.
268                          * Otherwise, these values are discarded and a linear
269                          * scan is performed from the beginning up to the
270                          * point where readdir starts returning values. */
271                         off_t                   tn_readdir_lastn;
272                         struct tmpfs_dirent *   tn_readdir_lastp;
273                 } tn_dir;
274
275                 /* Valid when tn_type == VLNK. */
276                 /* The link's target, allocated from a string pool. */
277                 char *                  tn_link;
278
279                 /* Valid when tn_type == VREG. */
280                 struct tn_reg {
281                         /* The contents of regular files stored in a tmpfs
282                          * file system are represented by a single anonymous
283                          * memory object (aobj, for short).  The aobj provides
284                          * direct access to any position within the file,
285                          * because its contents are always mapped in a
286                          * contiguous region of virtual memory.  It is a task
287                          * of the memory management subsystem (see uvm(9)) to
288                          * issue the required page ins or page outs whenever
289                          * a position within the file is accessed. */
290                         vm_object_t             tn_aobj;
291                         size_t                  tn_aobj_pages;
292
293                 } tn_reg;
294
295                 /* Valid when tn_type = VFIFO */
296                 struct tn_fifo {
297                         int (*tn_fo_read)  (struct file *fp, struct uio *uio,
298                                 struct ucred *cred, int flags);
299                         int (*tn_fo_write) (struct file *fp, struct uio *uio,
300                                 struct ucred *cred, int flags);
301                 } tn_fifo;
302         } tn_spec;
303 };
304
305 #define VTOI(vp)        ((struct tmpfs_node *)(vp)->v_data)
306
307 #ifdef _KERNEL
308 LIST_HEAD(tmpfs_node_list, tmpfs_node);
309
310 #define tn_rdev tn_spec.tn_rdev
311 #define tn_dir tn_spec.tn_dir
312 #define tn_link tn_spec.tn_link
313 #define tn_reg tn_spec.tn_reg
314 #define tn_fifo tn_spec.tn_fifo
315
316 #define TMPFS_NODE_LOCK(node) lockmgr(&(node)->tn_interlock, LK_EXCLUSIVE|LK_RETRY)
317 #define TMPFS_NODE_UNLOCK(node) lockmgr(&(node)->tn_interlock, LK_RELEASE)
318 #define TMPFS_NODE_MTX(node) (&(node)->tn_interlock)
319
320 #ifdef INVARIANTS
321 #define TMPFS_ASSERT_LOCKED(node) do {                                  \
322                 KKASSERT(node != NULL);                                 \
323                 KKASSERT(node->tn_vnode != NULL);                       \
324                 if (!vn_islocked(node->tn_vnode) &&                     \
325                     (lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE ))             \
326                         panic("tmpfs: node is not locked: %p", node);   \
327         } while (0)
328 #define TMPFS_ASSERT_ELOCKED(node) do {                                 \
329                 KKASSERT((node) != NULL);                               \
330                 KKASSERT(lockstatus(TMPFS_NODE_MTX(node), curthread) == LK_EXCLUSIVE);          \
331         } while (0)
332 #else
333 #define TMPFS_ASSERT_LOCKED(node) (void)0
334 #define TMPFS_ASSERT_ELOCKED(node) (void)0
335 #endif
336
337 #define TMPFS_VNODE_ALLOCATING  1
338 #define TMPFS_VNODE_WANT        2
339 #define TMPFS_VNODE_DOOMED      4
340 /* --------------------------------------------------------------------- */
341
342 /*
343  * Internal representation of a tmpfs mount point.
344  */
345 struct tmpfs_mount {
346         struct mount            *tm_mount;
347
348         /* Maximum number of memory pages available for use by the file
349          * system, set during mount time.  This variable must never be
350          * used directly as it may be bigger than the current amount of
351          * free memory; in the extreme case, it will hold the SIZE_MAX
352          * value.  Instead, use the TMPFS_PAGES_MAX macro. */
353         vm_pindex_t             tm_pages_max;
354
355         /* Number of pages in use by the file system.  Cannot be bigger
356          * than the value returned by TMPFS_PAGES_MAX in any case. */
357         vm_pindex_t             tm_pages_used;
358
359         /* Pointer to the node representing the root directory of this
360          * file system. */
361         struct tmpfs_node *     tm_root;
362
363         /* Maximum number of possible nodes for this file system; set
364          * during mount time.  We need a hard limit on the maximum number
365          * of nodes to avoid allocating too much of them; their objects
366          * cannot be released until the file system is unmounted.
367          * Otherwise, we could easily run out of memory by creating lots
368          * of empty files and then simply removing them. */
369         ino_t                   tm_nodes_max;
370
371         /* Number of nodes currently that are in use. */
372         ino_t                   tm_nodes_inuse;
373
374         /* maximum representable file size */
375         u_int64_t               tm_maxfilesize;
376
377         /* Nodes are organized in two different lists.  The used list
378          * contains all nodes that are currently used by the file system;
379          * i.e., they refer to existing files.  The available list contains
380          * all nodes that are currently available for use by new files.
381          * Nodes must be kept in this list (instead of deleting them)
382          * because we need to keep track of their generation number (tn_gen
383          * field).
384          *
385          * Note that nodes are lazily allocated: if the available list is
386          * empty and we have enough space to create more nodes, they will be
387          * created and inserted in the used list.  Once these are released,
388          * they will go into the available list, remaining alive until the
389          * file system is unmounted. */
390         struct tmpfs_node_list  tm_nodes_used;
391
392         /* Per-mount malloc zones for tmpfs nodes, names, and dirents */
393         struct malloc_type      *tm_node_zone;
394         struct malloc_type      *tm_dirent_zone;
395         struct malloc_type      *tm_name_zone;
396
397         struct objcache_malloc_args tm_node_zone_malloc_args;
398         struct objcache_malloc_args tm_dirent_zone_malloc_args;
399
400         /* Pools used to store file system meta data. */
401         struct objcache         *tm_dirent_pool;
402         struct objcache         *tm_node_pool;
403
404         int                     tm_ino;
405         int                     tm_flags;
406
407         struct netexport        tm_export;
408
409         struct mount            *tm_mnt;
410 };
411
412 #define TMPFS_LOCK(tm) lwkt_gettoken(&(tm)->tm_mount->mnt_token)
413 #define TMPFS_UNLOCK(tm) lwkt_reltoken(&(tm)->tm_mount->mnt_token)
414
415 /* --------------------------------------------------------------------- */
416
417 /*
418  * This structure maps a file identifier to a tmpfs node.  Used by the
419  * NFS code.
420  */
421 struct tmpfs_fid {
422         uint16_t                tf_len;
423         uint16_t                tf_pad;
424         ino_t                   tf_id;
425         unsigned long           tf_gen;
426 };
427
428 /* --------------------------------------------------------------------- */
429
430 #ifdef _KERNEL
431 /*
432  * Prototypes for tmpfs_subr.c.
433  */
434
435 int     tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
436             uid_t uid, gid_t gid, mode_t mode, char *, int, int,
437             struct tmpfs_node **);
438 void    tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
439 int     tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
440             const char *, uint16_t, struct tmpfs_dirent **);
441 void    tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *);
442 int     tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, int,
443             struct vnode **);
444 void    tmpfs_free_vp(struct vnode *);
445 int     tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
446             struct namecache *, struct ucred *, char *);
447 void    tmpfs_dir_attach(struct tmpfs_node *, struct tmpfs_dirent *);
448 void    tmpfs_dir_detach(struct tmpfs_node *, struct tmpfs_dirent *);
449 struct tmpfs_dirent *   tmpfs_dir_lookup(struct tmpfs_node *node,
450                             struct tmpfs_node *f,
451                             struct namecache *ncp);
452 int     tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
453 int     tmpfs_dir_getdotdotdent(struct tmpfs_mount *,
454                             struct tmpfs_node *, struct uio *);
455 struct tmpfs_dirent *   tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
456 int     tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
457 int     tmpfs_reg_resize(struct vnode *, off_t, int);
458 int     tmpfs_chflags(struct vnode *, int, struct ucred *);
459 int     tmpfs_chmod(struct vnode *, mode_t, struct ucred *);
460 int     tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *);
461 int     tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *);
462 int     tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
463             int, struct ucred *);
464 void    tmpfs_itimes(struct vnode *, const struct timespec *,
465             const struct timespec *);
466
467 void    tmpfs_update(struct vnode *);
468 int     tmpfs_truncate(struct vnode *, off_t);
469 int     tmpfs_node_ctor(void *obj, void *privdata, int flags);
470
471 /* --------------------------------------------------------------------- */
472
473 /*
474  * Convenience macros to simplify some logical expressions.
475  */
476 #define IMPLIES(a, b) (!(a) || (b))
477 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
478
479 /* --------------------------------------------------------------------- */
480
481 /*
482  * Checks that the directory entry pointed by 'de' matches the name 'name'
483  * with a length of 'len'.
484  */
485 #define TMPFS_DIRENT_MATCHES(de, name, len) \
486     (de->td_namelen == (uint16_t)len && \
487     bcmp((de)->td_name, (name), (de)->td_namelen) == 0)
488
489 /* --------------------------------------------------------------------- */
490
491 /*
492  * Ensures that the node pointed by 'node' is a directory and that its
493  * contents are consistent with respect to directories.
494  */
495 #define TMPFS_VALIDATE_DIR(node) \
496     KKASSERT((node)->tn_type == VDIR); \
497     KKASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
498     KKASSERT((node)->tn_dir.tn_readdir_lastp == NULL || \
499         tmpfs_dircookie((node)->tn_dir.tn_readdir_lastp) == (node)->tn_dir.tn_readdir_lastn);
500
501 #endif
502
503 /* --------------------------------------------------------------------- */
504
505 /*
506  * Macros/functions to convert from generic data structures to tmpfs
507  * specific ones.
508  */
509
510 static inline
511 struct tmpfs_mount *
512 VFS_TO_TMPFS(struct mount *mp)
513 {
514         struct tmpfs_mount *tmp;
515
516         KKASSERT((mp) != NULL && (mp)->mnt_data != NULL);
517         tmp = (struct tmpfs_mount *)(mp)->mnt_data;
518         return tmp;
519 }
520
521 static inline
522 struct tmpfs_node *
523 VP_TO_TMPFS_NODE(struct vnode *vp)
524 {
525         struct tmpfs_node *node;
526
527         KKASSERT((vp) != NULL && (vp)->v_data != NULL);
528         node = (struct tmpfs_node *)vp->v_data;
529         return node;
530 }
531
532 static inline
533 struct tmpfs_node *
534 VP_TO_TMPFS_DIR(struct vnode *vp)
535 {
536         struct tmpfs_node *node;
537
538         node = VP_TO_TMPFS_NODE(vp);
539         TMPFS_VALIDATE_DIR(node);
540         return node;
541 }
542
543 /* --------------------------------------------------------------------- */
544 /*
545  * buffer cache size
546  */
547 #define TMPFS_BLKSIZE   16384                   /* buffer cache size*/
548 #define TMPFS_BLKMASK   (TMPFS_BLKSIZE - 1)
549 #define TMPFS_BLKMASK64 ((off_t)(TMPFS_BLKSIZE - 1))
550 #endif /* _KERNEL */
551
552 #endif /* _VFS_TMPFS_TMPFS_H_ */