tmpfs.h revision 1.40 1 /* $NetBSD: tmpfs.h,v 1.40 2011/05/19 03:21:23 rmind 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 #include <sys/dirent.h>
37 #include <sys/mount.h>
38 #include <sys/pool.h>
39 #include <sys/queue.h>
40 #include <sys/vnode.h>
41
42 /*
43 * Internal representation of a tmpfs directory entry.
44 */
45 struct tmpfs_dirent {
46 TAILQ_ENTRY(tmpfs_dirent) td_entries;
47
48 /* Length of the name stored in this directory entry. This avoids
49 * the need to recalculate it every time the name is used. */
50 uint16_t td_namelen;
51
52 /* The name of the entry, allocated from a string pool. This
53 * string is not required to be zero-terminated; therefore, the
54 * td_namelen field must always be used when accessing its value. */
55 char * td_name;
56
57 /* Pointer to the node this entry refers to. */
58 struct tmpfs_node * td_node;
59 };
60
61 /* A directory in tmpfs holds a sorted list of directory entries, which in
62 * turn point to other files (which can be directories themselves).
63 *
64 * In tmpfs, this list is managed by a tail queue, whose head is defined by
65 * the struct tmpfs_dir type.
66 *
67 * It is imporant to notice that directories do not have entries for . and
68 * .. as other file systems do. These can be generated when requested
69 * based on information available by other means, such as the pointer to
70 * the node itself in the former case or the pointer to the parent directory
71 * in the latter case. This is done to simplify tmpfs's code and, more
72 * importantly, to remove redundancy. */
73 TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
74
75 /* Each entry in a directory has a cookie that identifies it. Cookies
76 * supersede offsets within directories because, given how tmpfs stores
77 * directories in memory, there is no such thing as an offset. (Emulating
78 * a real offset could be very difficult.)
79 *
80 * The '.', '..' and the end of directory markers have fixed cookies which
81 * cannot collide with the cookies generated by other entries. The cookies
82 * fot the other entries are generated based on the memory address on which
83 * stores their information is stored.
84 *
85 * Ideally, using the entry's memory pointer as the cookie would be enough
86 * to represent it and it wouldn't cause collisions in any system.
87 * Unfortunately, this results in "offsets" with very large values which
88 * later raise problems in the Linux compatibility layer (and maybe in other
89 * places) as described in PR kern/32034. Hence we need to workaround this
90 * with a rather ugly hack.
91 *
92 * Linux 32-bit binaries, unless built with _FILE_OFFSET_BITS=64, have off_t
93 * set to 'long', which is a 32-bit *signed* long integer. Regardless of
94 * the macro value, GLIBC (2.3 at least) always uses the getdents64
95 * system call (when calling readdir) which internally returns off64_t
96 * offsets. In order to make 32-bit binaries work, *GLIBC* converts the
97 * 64-bit values returned by the kernel to 32-bit ones and aborts with
98 * EOVERFLOW if the conversion results in values that won't fit in 32-bit
99 * integers (which it assumes is because the directory is extremely large).
100 * This wouldn't cause problems if we were dealing with unsigned integers,
101 * but as we have signed integers, this check fails due to sign expansion.
102 *
103 * For example, consider that the kernel returns the 0xc1234567 cookie to
104 * userspace in a off64_t integer. Later on, GLIBC casts this value to
105 * off_t (remember, signed) with code similar to:
106 * system call returns the offset in kernel_value;
107 * off_t casted_value = kernel_value;
108 * if (sizeof(off_t) != sizeof(off64_t) &&
109 * kernel_value != casted_value)
110 * error!
111 * In this case, casted_value still has 0xc1234567, but when it is compared
112 * for equality against kernel_value, it is promoted to a 64-bit integer and
113 * becomes 0xffffffffc1234567, which is different than 0x00000000c1234567.
114 * Then, GLIBC assumes this is because the directory is very large.
115 *
116 * Given that all the above happens in user-space, we have no control over
117 * it; therefore we must workaround the issue here. We do this by
118 * truncating the pointer value to a 32-bit integer and hope that there
119 * won't be collisions. In fact, this will not cause any problems in
120 * 32-bit platforms but some might arise in 64-bit machines (I'm not sure
121 * if they can happen at all in practice).
122 *
123 * XXX A nicer solution shall be attempted. */
124 #if defined(_KERNEL)
125 #define TMPFS_DIRCOOKIE_DOT 0
126 #define TMPFS_DIRCOOKIE_DOTDOT 1
127 #define TMPFS_DIRCOOKIE_EOF 2
128 static __inline
129 off_t
130 tmpfs_dircookie(struct tmpfs_dirent *de)
131 {
132 off_t cookie;
133
134 cookie = ((off_t)(uintptr_t)de >> 1) & 0x7FFFFFFF;
135 KASSERT(cookie != TMPFS_DIRCOOKIE_DOT);
136 KASSERT(cookie != TMPFS_DIRCOOKIE_DOTDOT);
137 KASSERT(cookie != TMPFS_DIRCOOKIE_EOF);
138
139 return cookie;
140 }
141 #endif /* defined(_KERNEL) */
142
143 /* --------------------------------------------------------------------- */
144
145 /*
146 * Internal representation of a tmpfs file system node.
147 *
148 * This structure is splitted in two parts: one holds attributes common
149 * to all file types and the other holds data that is only applicable to
150 * a particular type. The code must be careful to only access those
151 * attributes that are actually allowed by the node's type.
152 */
153 struct tmpfs_node {
154 /* Doubly-linked list entry which links all existing nodes for a
155 * single file system. This is provided to ease the removal of
156 * all nodes during the unmount operation. */
157 LIST_ENTRY(tmpfs_node) tn_entries;
158
159 /* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
160 * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode
161 * types instead of a custom enumeration is to make things simpler
162 * and faster, as we do not need to convert between two types. */
163 enum vtype tn_type;
164
165 /* Node identifier. */
166 ino_t tn_id;
167
168 /* Node's internal status. This is used by several file system
169 * operations to do modifications to the node in a delayed
170 * fashion. */
171 int tn_status;
172 #define TMPFS_NODE_ACCESSED (1 << 1)
173 #define TMPFS_NODE_MODIFIED (1 << 2)
174 #define TMPFS_NODE_CHANGED (1 << 3)
175
176 /* The node size. It does not necessarily match the real amount
177 * of memory consumed by it. */
178 off_t tn_size;
179
180 /* Generic node attributes. */
181 uid_t tn_uid;
182 gid_t tn_gid;
183 mode_t tn_mode;
184 int tn_flags;
185 nlink_t tn_links;
186 struct timespec tn_atime;
187 struct timespec tn_mtime;
188 struct timespec tn_ctime;
189 struct timespec tn_birthtime;
190 unsigned long tn_gen;
191
192 /* Head of byte-level lock list (used by tmpfs_advlock). */
193 struct lockf * tn_lockf;
194
195 /* As there is a single vnode for each active file within the
196 * system, care has to be taken to avoid allocating more than one
197 * vnode per file. In order to do this, a bidirectional association
198 * is kept between vnodes and nodes.
199 *
200 * Whenever a vnode is allocated, its v_data field is updated to
201 * point to the node it references. At the same time, the node's
202 * tn_vnode field is modified to point to the new vnode representing
203 * it. Further attempts to allocate a vnode for this same node will
204 * result in returning a new reference to the value stored in
205 * tn_vnode.
206 *
207 * May be NULL when the node is unused (that is, no vnode has been
208 * allocated for it or it has been reclaimed). */
209 kmutex_t tn_vlock;
210 struct vnode * tn_vnode;
211
212 union {
213 /* Valid when tn_type == VBLK || tn_type == VCHR. */
214 struct {
215 dev_t tn_rdev;
216 } tn_dev;
217
218 /* Valid when tn_type == VDIR. */
219 struct {
220 /* Pointer to the parent directory. The root
221 * directory has a pointer to itself in this field;
222 * this property identifies the root node. */
223 struct tmpfs_node * tn_parent;
224
225 /* Head of a tail-queue that links the contents of
226 * the directory together. See above for a
227 * description of its contents. */
228 struct tmpfs_dir tn_dir;
229
230 /* Number and pointer of the first directory entry
231 * returned by the readdir operation if it were
232 * called again to continue reading data from the
233 * same directory as before. This is used to speed
234 * up reads of long directories, assuming that no
235 * more than one read is in progress at a given time.
236 * Otherwise, these values are discarded and a linear
237 * scan is performed from the beginning up to the
238 * point where readdir starts returning values. */
239 off_t tn_readdir_lastn;
240 struct tmpfs_dirent * tn_readdir_lastp;
241 } tn_dir;
242
243 /* Valid when tn_type == VLNK. */
244 struct tn_lnk {
245 /* The link's target, allocated from a string pool. */
246 char * tn_link;
247 } tn_lnk;
248
249 /* Valid when tn_type == VREG. */
250 struct tn_reg {
251 /* The contents of regular files stored in a tmpfs
252 * file system are represented by a single anonymous
253 * memory object (aobj, for short). The aobj provides
254 * direct access to any position within the file,
255 * because its contents are always mapped in a
256 * contiguous region of virtual memory. It is a task
257 * of the memory management subsystem (see uvm(9)) to
258 * issue the required page ins or page outs whenever
259 * a position within the file is accessed. */
260 struct uvm_object * tn_aobj;
261 size_t tn_aobj_pages;
262 } tn_reg;
263 } tn_spec;
264 };
265 #define TMPFS_NODE_WHITEOUT ((struct tmpfs_node *)-1)
266
267 #if defined(_KERNEL)
268 LIST_HEAD(tmpfs_node_list, tmpfs_node);
269
270 /* --------------------------------------------------------------------- */
271
272 /*
273 * Internal representation of a tmpfs mount point.
274 */
275 struct tmpfs_mount {
276 /* Limit and number of bytes in use by the file system. */
277 uint64_t tm_mem_limit;
278 uint64_t tm_bytes_used;
279 kmutex_t tm_acc_lock;
280
281 /* Pointer to the node representing the root directory of this
282 * file system. */
283 struct tmpfs_node * tm_root;
284
285 /* Maximum number of possible nodes for this file system; set
286 * during mount time. We need a hard limit on the maximum number
287 * of nodes to avoid allocating too much of them; their objects
288 * cannot be released until the file system is unmounted.
289 * Otherwise, we could easily run out of memory by creating lots
290 * of empty files and then simply removing them. */
291 unsigned int tm_nodes_max;
292
293 /* Number of nodes currently allocated. This number only grows.
294 * When it reaches tm_nodes_max, no more new nodes can be allocated.
295 * Of course, the old, unused ones can be reused. */
296 unsigned int tm_nodes_cnt;
297
298 /* Node list. */
299 kmutex_t tm_lock;
300 struct tmpfs_node_list tm_nodes;
301 };
302
303 /* --------------------------------------------------------------------- */
304
305 /*
306 * This structure maps a file identifier to a tmpfs node. Used by the
307 * NFS code.
308 */
309 struct tmpfs_fid {
310 uint16_t tf_len;
311 uint16_t tf_pad;
312 uint32_t tf_gen;
313 ino_t tf_id;
314 };
315
316 /* --------------------------------------------------------------------- */
317
318 /*
319 * Prototypes for tmpfs_subr.c.
320 */
321
322 int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
323 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
324 char *, dev_t, struct tmpfs_node **);
325 void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
326 int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
327 const char *, uint16_t, struct tmpfs_dirent **);
328 void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
329 bool);
330 int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **);
331 void tmpfs_free_vp(struct vnode *);
332 int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
333 struct componentname *, char *);
334 void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
335 void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
336 struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node,
337 struct componentname *cnp);
338 int tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
339 int tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
340 struct tmpfs_dirent * tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
341 int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
342 int tmpfs_reg_resize(struct vnode *, off_t);
343 int tmpfs_chflags(struct vnode *, int, kauth_cred_t, struct lwp *);
344 int tmpfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct lwp *);
345 int tmpfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, struct lwp *);
346 int tmpfs_chsize(struct vnode *, u_quad_t, kauth_cred_t, struct lwp *);
347 int tmpfs_chtimes(struct vnode *, const struct timespec *,
348 const struct timespec *, const struct timespec *, int, kauth_cred_t,
349 struct lwp *);
350 void tmpfs_itimes(struct vnode *, const struct timespec *,
351 const struct timespec *, const struct timespec *);
352
353 void tmpfs_update(struct vnode *, const struct timespec *,
354 const struct timespec *, const struct timespec *, int);
355 int tmpfs_truncate(struct vnode *, off_t);
356
357 /*
358 * Prototypes for tmpfs_mem.c.
359 */
360
361 void tmpfs_mntmem_init(struct tmpfs_mount *, uint64_t);
362 void tmpfs_mntmem_destroy(struct tmpfs_mount *);
363
364 size_t tmpfs_mem_info(bool);
365 uint64_t tmpfs_bytes_max(struct tmpfs_mount *);
366 size_t tmpfs_pages_avail(struct tmpfs_mount *);
367 bool tmpfs_mem_incr(struct tmpfs_mount *, size_t);
368 void tmpfs_mem_decr(struct tmpfs_mount *, size_t);
369
370 struct tmpfs_dirent *tmpfs_dirent_get(struct tmpfs_mount *);
371 void tmpfs_dirent_put(struct tmpfs_mount *, struct tmpfs_dirent *);
372
373 struct tmpfs_node *tmpfs_node_get(struct tmpfs_mount *);
374 void tmpfs_node_put(struct tmpfs_mount *, struct tmpfs_node *);
375
376 char * tmpfs_strname_alloc(struct tmpfs_mount *, size_t);
377 void tmpfs_strname_free(struct tmpfs_mount *, char *, size_t);
378 bool tmpfs_strname_neqlen(struct componentname *, struct componentname *);
379
380 /* --------------------------------------------------------------------- */
381
382 /*
383 * Convenience macros to simplify some logical expressions.
384 */
385 #define IMPLIES(a, b) (!(a) || (b))
386 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
387
388 /* --------------------------------------------------------------------- */
389
390 /*
391 * Checks that the directory entry pointed by 'de' matches the name 'name'
392 * with a length of 'len'.
393 */
394 #define TMPFS_DIRENT_MATCHES(de, name, len) \
395 (de->td_namelen == (uint16_t)len && \
396 memcmp((de)->td_name, (name), (de)->td_namelen) == 0)
397
398 /* --------------------------------------------------------------------- */
399
400 /*
401 * Ensures that the node pointed by 'node' is a directory and that its
402 * contents are consistent with respect to directories.
403 */
404 #define TMPFS_VALIDATE_DIR(node) \
405 KASSERT((node)->tn_type == VDIR); \
406 KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
407 KASSERT((node)->tn_spec.tn_dir.tn_readdir_lastp == NULL || \
408 tmpfs_dircookie((node)->tn_spec.tn_dir.tn_readdir_lastp) == \
409 (node)->tn_spec.tn_dir.tn_readdir_lastn);
410
411 /* --------------------------------------------------------------------- */
412
413 /*
414 * Memory management stuff.
415 */
416
417 /* Amount of memory pages to reserve for the system (e.g., to not use by
418 * tmpfs).
419 * XXX: Should this be tunable through sysctl, for instance? */
420 #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
421
422 /*
423 * Macros/functions to convert from generic data structures to tmpfs
424 * specific ones.
425 */
426
427 static __inline
428 struct tmpfs_mount *
429 VFS_TO_TMPFS(struct mount *mp)
430 {
431 struct tmpfs_mount *tmp;
432
433 #ifdef KASSERT
434 KASSERT((mp) != NULL && (mp)->mnt_data != NULL);
435 #endif
436 tmp = (struct tmpfs_mount *)(mp)->mnt_data;
437 return tmp;
438 }
439
440 #endif /* defined(_KERNEL) */
441
442 static __inline
443 struct tmpfs_node *
444 VP_TO_TMPFS_NODE(struct vnode *vp)
445 {
446 struct tmpfs_node *node;
447
448 #ifdef KASSERT
449 KASSERT((vp) != NULL && (vp)->v_data != NULL);
450 #endif
451 node = (struct tmpfs_node *)vp->v_data;
452 return node;
453 }
454
455 #if defined(_KERNEL)
456
457 static __inline
458 struct tmpfs_node *
459 VP_TO_TMPFS_DIR(struct vnode *vp)
460 {
461 struct tmpfs_node *node;
462
463 node = VP_TO_TMPFS_NODE(vp);
464 #ifdef KASSERT
465 TMPFS_VALIDATE_DIR(node);
466 #endif
467 return node;
468 }
469
470 #endif /* defined(_KERNEL) */
471 #endif /* _FS_TMPFS_TMPFS_H_ */
472