tmpfs.h revision 1.16.4.3 1 /* $NetBSD: tmpfs.h,v 1.16.4.3 2006/05/06 23:31:30 christos Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006 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 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #ifndef _FS_TMPFS_TMPFS_H_
41 #define _FS_TMPFS_TMPFS_H_
42
43 /* ---------------------------------------------------------------------
44 * KERNEL-SPECIFIC DEFINITIONS
45 * --------------------------------------------------------------------- */
46 #include <sys/dirent.h>
47 #include <sys/mount.h>
48 #include <sys/queue.h>
49 #include <sys/vnode.h>
50
51 #include <fs/tmpfs/tmpfs_pool.h>
52
53 /* --------------------------------------------------------------------- */
54
55 /*
56 * Internal representation of a tmpfs directory entry.
57 */
58 struct tmpfs_dirent {
59 TAILQ_ENTRY(tmpfs_dirent) td_entries;
60
61 /* Length of the name stored in this directory entry. This avoids
62 * the need to recalculate it every time the name is used. */
63 uint16_t td_namelen;
64
65 /* The name of the entry, allocated from a string pool. This
66 * string is not required to be zero-terminated; therefore, the
67 * td_namelen field must always be used when accessing its value. */
68 char * td_name;
69
70 /* Pointer to the node this entry refers to. */
71 struct tmpfs_node * td_node;
72 };
73
74 /* A directory in tmpfs holds a sorted list of directory entries, which in
75 * turn point to other files (which can be directories themselves).
76 *
77 * In tmpfs, this list is managed by a tail queue, whose head is defined by
78 * the struct tmpfs_dir type.
79 *
80 * It is imporant to notice that directories do not have entries for . and
81 * .. as other file systems do. These can be generated when requested
82 * based on information available by other means, such as the pointer to
83 * the node itself in the former case or the pointer to the parent directory
84 * in the latter case. This is done to simplify tmpfs's code and, more
85 * importantly, to remove redundancy. */
86 TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
87
88 #define TMPFS_DIRCOOKIE(dirent) ((off_t)(uintptr_t)(dirent))
89 #define TMPFS_DIRCOOKIE_DOT 0
90 #define TMPFS_DIRCOOKIE_DOTDOT 1
91 #define TMPFS_DIRCOOKIE_EOF 2
92
93 /* --------------------------------------------------------------------- */
94
95 /*
96 * Internal representation of a tmpfs file system node.
97 *
98 * This structure is splitted in two parts: one holds attributes common
99 * to all file types and the other holds data that is only applicable to
100 * a particular type. The code must be careful to only access those
101 * attributes that are actually allowed by the node's type.
102 */
103 struct tmpfs_node {
104 /* Doubly-linked list entry which links all existing nodes for a
105 * single file system. This is provided to ease the removal of
106 * all nodes during the unmount operation. */
107 LIST_ENTRY(tmpfs_node) tn_entries;
108
109 /* The node's type. Any of 'VBLK', 'VCHR', 'VDIR', 'VFIFO',
110 * 'VLNK', 'VREG' and 'VSOCK' is allowed. The usage of vnode
111 * types instead of a custom enumeration is to make things simpler
112 * and faster, as we do not need to convert between two types. */
113 enum vtype tn_type;
114
115 /* Node identifier. */
116 ino_t tn_id;
117
118 /* Node's internal status. This is used by several file system
119 * operations to do modifications to the node in a delayed
120 * fashion. */
121 int tn_status;
122 #define TMPFS_NODE_ACCESSED (1 << 1)
123 #define TMPFS_NODE_MODIFIED (1 << 2)
124 #define TMPFS_NODE_CHANGED (1 << 3)
125
126 /* The node size. It does not necessarily match the real amount
127 * of memory consumed by it. */
128 off_t tn_size;
129
130 /* Generic node attributes. */
131 uid_t tn_uid;
132 gid_t tn_gid;
133 mode_t tn_mode;
134 int tn_flags;
135 nlink_t tn_links;
136 struct timespec tn_atime;
137 struct timespec tn_mtime;
138 struct timespec tn_ctime;
139 struct timespec tn_birthtime;
140 unsigned long tn_gen;
141
142 /* Head of byte-level lock list (used by tmpfs_advlock). */
143 struct lockf * tn_lockf;
144
145 /* As there is a single vnode for each active file within the
146 * system, care has to be taken to avoid allocating more than one
147 * vnode per file. In order to do this, a bidirectional association
148 * is kept between vnodes and nodes.
149 *
150 * Whenever a vnode is allocated, its v_data field is updated to
151 * point to the node it references. At the same time, the node's
152 * tn_vnode field is modified to point to the new vnode representing
153 * it. Further attempts to allocate a vnode for this same node will
154 * result in returning a new reference to the value stored in
155 * tn_vnode.
156 *
157 * May be NULL when the node is unused (that is, no vnode has been
158 * allocated for it or it has been reclaimed). */
159 struct vnode * tn_vnode;
160
161 /* Pointer to the node returned by tmpfs_lookup() after doing a
162 * delete or a rename lookup; its value is only valid in these two
163 * situations. In case we were looking up . or .., it holds a null
164 * pointer. */
165 struct tmpfs_dirent * tn_lookup_dirent;
166
167 union {
168 /* Valid when tn_type == VBLK || tn_type == VCHR. */
169 struct {
170 dev_t tn_rdev;
171 } tn_dev;
172
173 /* Valid when tn_type == VDIR. */
174 struct {
175 /* Pointer to the parent directory. The root
176 * directory has a pointer to itself in this field;
177 * this property identifies the root node. */
178 struct tmpfs_node * tn_parent;
179
180 /* Head of a tail-queue that links the contents of
181 * the directory together. See above for a
182 * description of its contents. */
183 struct tmpfs_dir tn_dir;
184
185 /* Number and pointer of the first directory entry
186 * returned by the readdir operation if it were
187 * called again to continue reading data from the
188 * same directory as before. This is used to speed
189 * up reads of long directories, assuming that no
190 * more than one read is in progress at a given time.
191 * Otherwise, these values are discarded and a linear
192 * scan is performed from the beginning up to the
193 * point where readdir starts returning values. */
194 off_t tn_readdir_lastn;
195 struct tmpfs_dirent * tn_readdir_lastp;
196 } tn_dir;
197
198 /* Valid when tn_type == VLNK. */
199 struct tn_lnk {
200 /* The link's target, allocated from a string pool. */
201 char * tn_link;
202 } tn_lnk;
203
204 /* Valid when tn_type == VREG. */
205 struct tn_reg {
206 /* The contents of regular files stored in a tmpfs
207 * file system are represented by a single anonymous
208 * memory object (aobj, for short). The aobj provides
209 * direct access to any position within the file,
210 * because its contents are always mapped in a
211 * contiguous region of virtual memory. It is a task
212 * of the memory management subsystem (see uvm(9)) to
213 * issue the required page ins or page outs whenever
214 * a position within the file is accessed. */
215 struct uvm_object * tn_aobj;
216 size_t tn_aobj_pages;
217 } tn_reg;
218 } tn_spec;
219 };
220 LIST_HEAD(tmpfs_node_list, tmpfs_node);
221
222 /* --------------------------------------------------------------------- */
223
224 /*
225 * Internal representation of a tmpfs mount point.
226 */
227 struct tmpfs_mount {
228 /* Maximum number of memory pages available for use by the file
229 * system, set during mount time. This variable must never be
230 * used directly as it may be bigger that the current amount of
231 * free memory; in the extreme case, it will hold the SIZE_MAX
232 * value. Instead, use the TMPFS_PAGES_MAX macro. */
233 size_t tm_pages_max;
234
235 /* Number of pages in use by the file system. Cannot be bigger
236 * than the value returned by TMPFS_PAGES_MAX in any case. */
237 size_t tm_pages_used;
238
239 /* Pointer to the node representing the root directory of this
240 * file system. */
241 struct tmpfs_node * tm_root;
242
243 /* Maximum number of possible nodes for this file system; set
244 * during mount time. We need a hard limit on the maximum number
245 * of nodes to avoid allocating too much of them; their objects
246 * cannot be released until the file system is unmounted.
247 * Otherwise, we could easily run out of memory by creating lots
248 * of empty files and then simply removing them. */
249 ino_t tm_nodes_max;
250
251 /* Number of nodes currently allocated. This number only grows.
252 * When it reaches tm_nodes_max, no more new nodes can be allocated.
253 * Of course, the old, unused ones can be reused. */
254 ino_t tm_nodes_last;
255
256 /* Nodes are organized in two different lists. The used list
257 * contains all nodes that are currently used by the file system;
258 * i.e., they refer to existing files. The available list contains
259 * all nodes that are currently available for use by new files.
260 * Nodes must be kept in this list (instead of deleting them)
261 * because we need to keep track of their generation number (tn_gen
262 * field).
263 *
264 * Note that nodes are lazily allocated: if the available list is
265 * empty and we have enough space to create more nodes, they will be
266 * created and inserted in the used list. Once these are released,
267 * they will go into the available list, remaining alive until the
268 * file system is unmounted. */
269 struct tmpfs_node_list tm_nodes_used;
270 struct tmpfs_node_list tm_nodes_avail;
271
272 /* Pools used to store file system meta data. These are not shared
273 * across several instances of tmpfs for the reasons described in
274 * tmpfs_pool.c. */
275 struct tmpfs_pool tm_dirent_pool;
276 struct tmpfs_pool tm_node_pool;
277 struct tmpfs_str_pool tm_str_pool;
278 };
279
280 /* --------------------------------------------------------------------- */
281
282 /*
283 * This structure maps a file identifier to a tmpfs node. Used by the
284 * NFS code.
285 */
286 struct tmpfs_fid {
287 uint16_t tf_len;
288 uint16_t tf_pad;
289 uint32_t tf_gen;
290 ino_t tf_id;
291 };
292
293 /* --------------------------------------------------------------------- */
294
295 #ifdef _KERNEL
296 /*
297 * Prototypes for tmpfs_subr.c.
298 */
299
300 int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
301 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
302 char *, dev_t, struct proc *, struct tmpfs_node **);
303 void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
304 int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
305 const char *, uint16_t, struct tmpfs_dirent **);
306 void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
307 boolean_t);
308 int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **);
309 void tmpfs_free_vp(struct vnode *);
310 int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
311 struct componentname *, char *);
312 void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
313 void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
314 struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node,
315 struct componentname *cnp);
316 int tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
317 int tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
318 struct tmpfs_dirent * tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
319 int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
320 int tmpfs_reg_resize(struct vnode *, off_t);
321 size_t tmpfs_mem_info(boolean_t);
322 int tmpfs_chflags(struct vnode *, int, kauth_cred_t, struct proc *);
323 int tmpfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct proc *);
324 int tmpfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, struct proc *);
325 int tmpfs_chsize(struct vnode *, u_quad_t, kauth_cred_t, struct proc *);
326 int tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
327 int, kauth_cred_t, struct lwp *);
328 void tmpfs_itimes(struct vnode *, const struct timespec *,
329 const struct timespec *);
330
331 void tmpfs_update(struct vnode *, const struct timespec *,
332 const struct timespec *, int);
333 int tmpfs_truncate(struct vnode *, off_t);
334
335 /* --------------------------------------------------------------------- */
336
337 /*
338 * Convenience macros to simplify some logical expressions.
339 */
340 #define IMPLIES(a, b) (!(a) || (b))
341 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
342
343 /* --------------------------------------------------------------------- */
344
345 /*
346 * Checks that the directory entry pointed by 'de' matches the name 'name'
347 * with a length of 'len'.
348 */
349 #define TMPFS_DIRENT_MATCHES(de, name, len) \
350 (de->td_namelen == (uint16_t)len && \
351 memcmp((de)->td_name, (name), (de)->td_namelen) == 0)
352
353 /* --------------------------------------------------------------------- */
354
355 /*
356 * Ensures that the node pointed by 'node' is a directory and that its
357 * contents are consistent with respect to directories.
358 */
359 #define TMPFS_VALIDATE_DIR(node) \
360 KASSERT((node)->tn_type == VDIR); \
361 KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
362 KASSERT((node)->tn_spec.tn_dir.tn_readdir_lastp == NULL || \
363 TMPFS_DIRCOOKIE((node)->tn_spec.tn_dir.tn_readdir_lastp) == \
364 (node)->tn_spec.tn_dir.tn_readdir_lastn);
365
366 /* --------------------------------------------------------------------- */
367
368 /*
369 * Memory management stuff.
370 */
371
372 /* Amount of memory pages to reserve for the system (e.g., to not use by
373 * tmpfs).
374 * XXX: Should this be tunable through sysctl, for instance? */
375 #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
376
377 /* Returns the maximum size allowed for a tmpfs file system. This macro
378 * must be used instead of directly retrieving the value from tm_pages_max.
379 * The reason is that the size of a tmpfs file system is dynamic: it lets
380 * the user store files as long as there is enough free memory (including
381 * physical memory and swap space). Therefore, the amount of memory to be
382 * used is either the limit imposed by the user during mount time or the
383 * amount of available memory, whichever is lower. To avoid consuming all
384 * the memory for a given mount point, the system will always reserve a
385 * minimum of TMPFS_PAGES_RESERVED pages, which is also taken into account
386 * by this macro (see above). */
387 static __inline size_t
388 TMPFS_PAGES_MAX(struct tmpfs_mount *tmp)
389 {
390 size_t freepages;
391
392 freepages = tmpfs_mem_info(FALSE);
393 if (freepages < TMPFS_PAGES_RESERVED)
394 freepages = 0;
395 else
396 freepages -= TMPFS_PAGES_RESERVED;
397
398 return MIN(tmp->tm_pages_max, freepages + tmp->tm_pages_used);
399 }
400
401 /* Returns the available space for the given file system. */
402 #define TMPFS_PAGES_AVAIL(tmp) (TMPFS_PAGES_MAX(tmp) - (tmp)->tm_pages_used)
403
404 #endif
405
406 /* --------------------------------------------------------------------- */
407
408 /*
409 * Macros/functions to convert from generic data structures to tmpfs
410 * specific ones.
411 */
412
413 static __inline
414 struct tmpfs_mount *
415 VFS_TO_TMPFS(struct mount *mp)
416 {
417 struct tmpfs_mount *tmp;
418
419 #ifdef KASSERT
420 KASSERT((mp) != NULL && (mp)->mnt_data != NULL);
421 #endif
422 tmp = (struct tmpfs_mount *)(mp)->mnt_data;
423 return tmp;
424 }
425
426 static __inline
427 struct tmpfs_node *
428 VP_TO_TMPFS_NODE(struct vnode *vp)
429 {
430 struct tmpfs_node *node;
431
432 #ifdef KASSERT
433 KASSERT((vp) != NULL && (vp)->v_data != NULL);
434 #endif
435 node = (struct tmpfs_node *)vp->v_data;
436 return node;
437 }
438
439 static __inline
440 struct tmpfs_node *
441 VP_TO_TMPFS_DIR(struct vnode *vp)
442 {
443 struct tmpfs_node *node;
444
445 node = VP_TO_TMPFS_NODE(vp);
446 #ifdef KASSERT
447 TMPFS_VALIDATE_DIR(node);
448 #endif
449 return node;
450 }
451
452 /* ---------------------------------------------------------------------
453 * USER AND KERNEL DEFINITIONS
454 * --------------------------------------------------------------------- */
455
456 /*
457 * This structure is used to communicate mount parameters between userland
458 * and kernel space.
459 */
460 #define TMPFS_ARGS_VERSION 1
461 struct tmpfs_args {
462 int ta_version;
463
464 /* Size counters. */
465 ino_t ta_nodes_max;
466 off_t ta_size_max;
467
468 /* Root node attributes. */
469 uid_t ta_root_uid;
470 gid_t ta_root_gid;
471 mode_t ta_root_mode;
472 };
473 #endif /* _FS_TMPFS_TMPFS_H_ */
474