tmpfs.h revision 1.45.2.1 1 /* $NetBSD: tmpfs.h,v 1.45.2.1 2014/05/22 11:41:02 yamt 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 LIST_HEAD(tmpfs_node_list, tmpfs_node);
164
165 #define TMPFS_MAXNAMLEN 255
166 /* Validate maximum td_namelen length. */
167 CTASSERT(TMPFS_MAXNAMLEN < UINT16_MAX);
168
169 /*
170 * Reserved values for the virtual entries (the first must be 0) and EOF.
171 * The start/end of the incremental range, see tmpfs_dir_getseq().
172 */
173 #define TMPFS_DIRSEQ_DOT 0
174 #define TMPFS_DIRSEQ_DOTDOT 1
175 #define TMPFS_DIRSEQ_EOF 2
176
177 #define TMPFS_DIRSEQ_START 3 /* inclusive */
178 #define TMPFS_DIRSEQ_END (1U << 30) /* exclusive */
179
180 /* Mark to indicate that the number is not set. */
181 #define TMPFS_DIRSEQ_NONE (1U << 31)
182
183 /* Flags: time update requests. */
184 #define TMPFS_UPDATE_ATIME 0x01
185 #define TMPFS_UPDATE_MTIME 0x02
186 #define TMPFS_UPDATE_CTIME 0x04
187
188 /*
189 * Bits indicating vnode reclamation and whiteout use for the directory.
190 * We abuse tmpfs_node_t::tn_gen for that.
191 */
192 #define TMPFS_RECLAIMING_BIT (1U << 31)
193 #define TMPFS_WHITEOUT_BIT (1U << 30)
194 #define TMPFS_NODE_GEN_MASK (TMPFS_WHITEOUT_BIT - 1)
195
196 #define TMPFS_NODE_RECLAIMING(node) \
197 (((node)->tn_gen & TMPFS_RECLAIMING_BIT) != 0)
198
199 #define TMPFS_NODE_GEN(node) \
200 ((node)->tn_gen & TMPFS_NODE_GEN_MASK)
201
202 /* White-out inode indicator. */
203 #define TMPFS_NODE_WHITEOUT ((tmpfs_node_t *)-1)
204
205 /*
206 * Internal representation of a tmpfs mount point.
207 */
208 typedef struct tmpfs_mount {
209 /* Limit and number of bytes in use by the file system. */
210 uint64_t tm_mem_limit;
211 uint64_t tm_bytes_used;
212 kmutex_t tm_acc_lock;
213
214 /* Pointer to the root inode. */
215 tmpfs_node_t * tm_root;
216
217 /* Maximum number of possible nodes for this file system. */
218 unsigned int tm_nodes_max;
219
220 /* Number of nodes currently allocated. */
221 unsigned int tm_nodes_cnt;
222
223 /* List of inodes and the lock protecting it. */
224 kmutex_t tm_lock;
225 struct tmpfs_node_list tm_nodes;
226 } tmpfs_mount_t;
227
228 /*
229 * This structure maps a file identifier to a tmpfs node. Used by the
230 * NFS code.
231 */
232 typedef struct tmpfs_fid {
233 uint16_t tf_len;
234 uint16_t tf_pad;
235 uint32_t tf_gen;
236 ino_t tf_id;
237 } tmpfs_fid_t;
238
239 /*
240 * Prototypes for tmpfs_subr.c.
241 */
242
243 int tmpfs_alloc_node(tmpfs_mount_t *, enum vtype, uid_t, gid_t,
244 mode_t, char *, dev_t, tmpfs_node_t **);
245 void tmpfs_free_node(tmpfs_mount_t *, tmpfs_node_t *);
246
247 int tmpfs_construct_node(vnode_t *, vnode_t **, struct vattr *,
248 struct componentname *, char *);
249
250 int tmpfs_vnode_get(struct mount *, tmpfs_node_t *, vnode_t **);
251
252 int tmpfs_alloc_dirent(tmpfs_mount_t *, const char *, uint16_t,
253 tmpfs_dirent_t **);
254 void tmpfs_free_dirent(tmpfs_mount_t *, tmpfs_dirent_t *);
255 void tmpfs_dir_attach(tmpfs_node_t *, tmpfs_dirent_t *, tmpfs_node_t *);
256 void tmpfs_dir_detach(tmpfs_node_t *, tmpfs_dirent_t *);
257
258 tmpfs_dirent_t *tmpfs_dir_lookup(tmpfs_node_t *, struct componentname *);
259 tmpfs_dirent_t *tmpfs_dir_cached(tmpfs_node_t *);
260
261 uint32_t tmpfs_dir_getseq(tmpfs_node_t *, tmpfs_dirent_t *);
262 tmpfs_dirent_t *tmpfs_dir_lookupbyseq(tmpfs_node_t *, off_t);
263 int tmpfs_dir_getdents(tmpfs_node_t *, struct uio *, off_t *);
264
265 int tmpfs_reg_resize(vnode_t *, off_t);
266
267 int tmpfs_chflags(vnode_t *, int, kauth_cred_t, lwp_t *);
268 int tmpfs_chmod(vnode_t *, mode_t, kauth_cred_t, lwp_t *);
269 int tmpfs_chown(vnode_t *, uid_t, gid_t, kauth_cred_t, lwp_t *);
270 int tmpfs_chsize(vnode_t *, u_quad_t, kauth_cred_t, lwp_t *);
271 int tmpfs_chtimes(vnode_t *, const struct timespec *,
272 const struct timespec *, const struct timespec *, int,
273 kauth_cred_t, lwp_t *);
274 void tmpfs_update(vnode_t *, unsigned);
275
276 /*
277 * Prototypes for tmpfs_mem.c.
278 */
279
280 void tmpfs_mntmem_init(tmpfs_mount_t *, uint64_t);
281 void tmpfs_mntmem_destroy(tmpfs_mount_t *);
282 int tmpfs_mntmem_set(tmpfs_mount_t *, uint64_t);
283
284 size_t tmpfs_mem_info(bool);
285 uint64_t tmpfs_bytes_max(tmpfs_mount_t *);
286 size_t tmpfs_pages_avail(tmpfs_mount_t *);
287 bool tmpfs_mem_incr(tmpfs_mount_t *, size_t);
288 void tmpfs_mem_decr(tmpfs_mount_t *, size_t);
289
290 tmpfs_dirent_t *tmpfs_dirent_get(tmpfs_mount_t *);
291 void tmpfs_dirent_put(tmpfs_mount_t *, tmpfs_dirent_t *);
292
293 tmpfs_node_t * tmpfs_node_get(tmpfs_mount_t *);
294 void tmpfs_node_put(tmpfs_mount_t *, tmpfs_node_t *);
295
296 char * tmpfs_strname_alloc(tmpfs_mount_t *, size_t);
297 void tmpfs_strname_free(tmpfs_mount_t *, char *, size_t);
298 bool tmpfs_strname_neqlen(struct componentname *, struct componentname *);
299
300 /*
301 * Ensures that the node pointed by 'node' is a directory and that its
302 * contents are consistent with respect to directories.
303 */
304 #define TMPFS_VALIDATE_DIR(node) \
305 KASSERT((node)->tn_vnode == NULL || VOP_ISLOCKED((node)->tn_vnode)); \
306 KASSERT((node)->tn_type == VDIR); \
307 KASSERT((node)->tn_size % sizeof(tmpfs_dirent_t) == 0);
308
309 /*
310 * Memory management stuff.
311 */
312
313 /* Amount of memory pages to reserve for the system. */
314 #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
315
316 /*
317 * Routines to convert VFS structures to tmpfs internal ones.
318 */
319
320 static inline tmpfs_mount_t *
321 VFS_TO_TMPFS(struct mount *mp)
322 {
323 tmpfs_mount_t *tmp = mp->mnt_data;
324
325 KASSERT(tmp != NULL);
326 return tmp;
327 }
328
329 static inline tmpfs_node_t *
330 VP_TO_TMPFS_DIR(vnode_t *vp)
331 {
332 tmpfs_node_t *node = vp->v_data;
333
334 KASSERT(node != NULL);
335 TMPFS_VALIDATE_DIR(node);
336 return node;
337 }
338
339 #endif /* defined(_KERNEL) */
340
341 static __inline tmpfs_node_t *
342 VP_TO_TMPFS_NODE(vnode_t *vp)
343 {
344 tmpfs_node_t *node = vp->v_data;
345 #ifdef KASSERT
346 KASSERT(node != NULL);
347 #endif
348 return node;
349 }
350
351 #endif /* _FS_TMPFS_TMPFS_H_ */
352