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