tmpfs.h revision 1.51 1 /* $NetBSD: tmpfs.h,v 1.51 2015/07/06 10:05:50 hannken 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 /* The inode size. */
105 off_t tn_size;
106
107 /* Generic node attributes. */
108 uid_t tn_uid;
109 gid_t tn_gid;
110 mode_t tn_mode;
111 int tn_flags;
112 nlink_t tn_links;
113 struct timespec tn_atime;
114 struct timespec tn_mtime;
115 struct timespec tn_ctime;
116 struct timespec tn_birthtime;
117
118 /* Head of byte-level lock list (used by tmpfs_advlock). */
119 struct lockf * tn_lockf;
120
121 union {
122 /* Type case: VBLK or VCHR. */
123 struct {
124 dev_t tn_rdev;
125 } tn_dev;
126
127 /* Type case: VDIR. */
128 struct {
129 /* Parent directory (root inode points to itself). */
130 struct tmpfs_node * tn_parent;
131
132 /* List of directory entries. */
133 struct tmpfs_dir tn_dir;
134
135 /* Last given sequence number and their arena. */
136 uint32_t tn_next_seq;
137 void * tn_seq_arena;
138
139 /*
140 * Pointer of the last directory entry returned
141 * by the readdir(3) operation.
142 */
143 struct tmpfs_dirent * tn_readdir_lastp;
144 } tn_dir;
145
146 /* Type case: VLNK. */
147 struct tn_lnk {
148 /* The link's target. */
149 char * tn_link;
150 } tn_lnk;
151
152 /* Type case: VREG. */
153 struct tn_reg {
154 /* Underlying UVM object to store contents. */
155 struct uvm_object * tn_aobj;
156 size_t tn_aobj_pages;
157 } tn_reg;
158 } tn_spec;
159 } tmpfs_node_t;
160
161 #if defined(_KERNEL)
162
163 VFS_PROTOS(tmpfs);
164
165 LIST_HEAD(tmpfs_node_list, tmpfs_node);
166
167 #define TMPFS_MAXNAMLEN 255
168 /* Validate maximum td_namelen length. */
169 CTASSERT(TMPFS_MAXNAMLEN < UINT16_MAX);
170
171 /*
172 * Reserved values for the virtual entries (the first must be 0) and EOF.
173 * The start/end of the incremental range, see tmpfs_dir_getseq().
174 */
175 #define TMPFS_DIRSEQ_DOT 0
176 #define TMPFS_DIRSEQ_DOTDOT 1
177 #define TMPFS_DIRSEQ_EOF 2
178
179 #define TMPFS_DIRSEQ_START 3 /* inclusive */
180 #define TMPFS_DIRSEQ_END (1U << 30) /* exclusive */
181
182 /* Mark to indicate that the number is not set. */
183 #define TMPFS_DIRSEQ_NONE (1U << 31)
184
185 /* Flags: time update requests. */
186 #define TMPFS_UPDATE_ATIME 0x01
187 #define TMPFS_UPDATE_MTIME 0x02
188 #define TMPFS_UPDATE_CTIME 0x04
189
190 /*
191 * Bits indicating vnode reclamation and whiteout use for the directory.
192 * We abuse tmpfs_node_t::tn_gen for that.
193 */
194 #define TMPFS_RECLAIMING_BIT (1U << 31)
195 #define TMPFS_WHITEOUT_BIT (1U << 30)
196 #define TMPFS_NODE_GEN_MASK (TMPFS_WHITEOUT_BIT - 1)
197
198 #define TMPFS_NODE_RECLAIMING(node) \
199 (((node)->tn_gen & TMPFS_RECLAIMING_BIT) != 0)
200
201 #define TMPFS_NODE_GEN(node) \
202 ((node)->tn_gen & TMPFS_NODE_GEN_MASK)
203
204 /* White-out inode indicator. */
205 #define TMPFS_NODE_WHITEOUT ((tmpfs_node_t *)-1)
206
207 /*
208 * Internal representation of a tmpfs mount point.
209 */
210 typedef struct tmpfs_mount {
211 /* Limit and number of bytes in use by the file system. */
212 uint64_t tm_mem_limit;
213 uint64_t tm_bytes_used;
214 kmutex_t tm_acc_lock;
215
216 /* Pointer to the root inode. */
217 tmpfs_node_t * tm_root;
218
219 /* Maximum number of possible nodes for this file system. */
220 unsigned int tm_nodes_max;
221
222 /* Number of nodes currently allocated. */
223 unsigned int tm_nodes_cnt;
224
225 /* List of inodes and the lock protecting it. */
226 kmutex_t tm_lock;
227 struct tmpfs_node_list tm_nodes;
228 } tmpfs_mount_t;
229
230 /*
231 * This structure maps a file identifier to a tmpfs node. Used by the
232 * NFS code.
233 */
234 typedef struct tmpfs_fid {
235 uint16_t tf_len;
236 uint16_t tf_pad;
237 uint32_t tf_gen;
238 ino_t tf_id;
239 } tmpfs_fid_t;
240
241 /*
242 * Prototypes for tmpfs_subr.c.
243 */
244
245 int tmpfs_alloc_node(tmpfs_mount_t *, enum vtype, uid_t, gid_t,
246 mode_t, char *, dev_t, tmpfs_node_t **);
247 void tmpfs_free_node(tmpfs_mount_t *, tmpfs_node_t *);
248
249 int tmpfs_construct_node(vnode_t *, vnode_t **, struct vattr *,
250 struct componentname *, char *);
251
252 int tmpfs_vnode_get(struct mount *, tmpfs_node_t *, vnode_t **);
253
254 int tmpfs_alloc_dirent(tmpfs_mount_t *, const char *, uint16_t,
255 tmpfs_dirent_t **);
256 void tmpfs_free_dirent(tmpfs_mount_t *, tmpfs_dirent_t *);
257 void tmpfs_dir_attach(tmpfs_node_t *, tmpfs_dirent_t *, tmpfs_node_t *);
258 void tmpfs_dir_detach(tmpfs_node_t *, tmpfs_dirent_t *);
259
260 tmpfs_dirent_t *tmpfs_dir_lookup(tmpfs_node_t *, struct componentname *);
261 tmpfs_dirent_t *tmpfs_dir_cached(tmpfs_node_t *);
262
263 uint32_t tmpfs_dir_getseq(tmpfs_node_t *, tmpfs_dirent_t *);
264 tmpfs_dirent_t *tmpfs_dir_lookupbyseq(tmpfs_node_t *, off_t);
265 int tmpfs_dir_getdents(tmpfs_node_t *, struct uio *, off_t *);
266
267 int tmpfs_reg_resize(vnode_t *, off_t);
268
269 int tmpfs_chflags(vnode_t *, int, kauth_cred_t, lwp_t *);
270 int tmpfs_chmod(vnode_t *, mode_t, kauth_cred_t, lwp_t *);
271 int tmpfs_chown(vnode_t *, uid_t, gid_t, kauth_cred_t, lwp_t *);
272 int tmpfs_chsize(vnode_t *, u_quad_t, kauth_cred_t, lwp_t *);
273 int tmpfs_chtimes(vnode_t *, const struct timespec *,
274 const struct timespec *, const struct timespec *, int,
275 kauth_cred_t, lwp_t *);
276 void tmpfs_update(vnode_t *, unsigned);
277
278 /*
279 * Prototypes for tmpfs_mem.c.
280 */
281
282 void tmpfs_mntmem_init(tmpfs_mount_t *, uint64_t);
283 void tmpfs_mntmem_destroy(tmpfs_mount_t *);
284 int tmpfs_mntmem_set(tmpfs_mount_t *, uint64_t);
285
286 size_t tmpfs_mem_info(bool);
287 uint64_t tmpfs_bytes_max(tmpfs_mount_t *);
288 size_t tmpfs_pages_avail(tmpfs_mount_t *);
289 bool tmpfs_mem_incr(tmpfs_mount_t *, size_t);
290 void tmpfs_mem_decr(tmpfs_mount_t *, size_t);
291
292 tmpfs_dirent_t *tmpfs_dirent_get(tmpfs_mount_t *);
293 void tmpfs_dirent_put(tmpfs_mount_t *, tmpfs_dirent_t *);
294
295 tmpfs_node_t * tmpfs_node_get(tmpfs_mount_t *);
296 void tmpfs_node_put(tmpfs_mount_t *, tmpfs_node_t *);
297
298 char * tmpfs_strname_alloc(tmpfs_mount_t *, size_t);
299 void tmpfs_strname_free(tmpfs_mount_t *, char *, size_t);
300 bool tmpfs_strname_neqlen(struct componentname *, struct componentname *);
301
302 /*
303 * Ensures that the node pointed by 'node' is a directory and that its
304 * contents are consistent with respect to directories.
305 */
306 #define TMPFS_VALIDATE_DIR(node) \
307 KASSERT((node)->tn_vnode == NULL || VOP_ISLOCKED((node)->tn_vnode)); \
308 KASSERT((node)->tn_type == VDIR); \
309 KASSERT((node)->tn_size % sizeof(tmpfs_dirent_t) == 0);
310
311 /*
312 * Routines to convert VFS structures to tmpfs internal ones.
313 */
314
315 static inline tmpfs_mount_t *
316 VFS_TO_TMPFS(struct mount *mp)
317 {
318 tmpfs_mount_t *tmp = mp->mnt_data;
319
320 KASSERT(tmp != NULL);
321 return tmp;
322 }
323
324 static inline tmpfs_node_t *
325 VP_TO_TMPFS_DIR(vnode_t *vp)
326 {
327 tmpfs_node_t *node = vp->v_data;
328
329 KASSERT(node != NULL);
330 TMPFS_VALIDATE_DIR(node);
331 return node;
332 }
333
334 #endif /* defined(_KERNEL) */
335
336 static __inline tmpfs_node_t *
337 VP_TO_TMPFS_NODE(vnode_t *vp)
338 {
339 tmpfs_node_t *node = vp->v_data;
340 #ifdef KASSERT
341 KASSERT(node != NULL);
342 #endif
343 return node;
344 }
345
346 #endif /* _FS_TMPFS_TMPFS_H_ */
347