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