xref: /netbsd/sys/fs/tmpfs/tmpfs.h (revision 6550d01e)
1 /*	$NetBSD: tmpfs.h,v 1.39 2011/01/13 13:35:11 pooka 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 	char			tm_dwchan[32];
303 	struct pool		tm_dirent_pool;
304 	char			tm_nwchan[32];
305 	struct pool		tm_node_pool;
306 };
307 
308 /* --------------------------------------------------------------------- */
309 
310 /*
311  * This structure maps a file identifier to a tmpfs node.  Used by the
312  * NFS code.
313  */
314 struct tmpfs_fid {
315 	uint16_t		tf_len;
316 	uint16_t		tf_pad;
317 	uint32_t		tf_gen;
318 	ino_t			tf_id;
319 };
320 
321 /* --------------------------------------------------------------------- */
322 
323 /*
324  * Prototypes for tmpfs_subr.c.
325  */
326 
327 int	tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
328 	    uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
329 	    char *, dev_t, struct tmpfs_node **);
330 void	tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
331 int	tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
332 	    const char *, uint16_t, struct tmpfs_dirent **);
333 void	tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
334 	    bool);
335 int	tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **);
336 void	tmpfs_free_vp(struct vnode *);
337 int	tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
338 	    struct componentname *, char *);
339 void	tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
340 void	tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
341 struct tmpfs_dirent *	tmpfs_dir_lookup(struct tmpfs_node *node,
342 			    struct componentname *cnp);
343 int	tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
344 int	tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
345 struct tmpfs_dirent *	tmpfs_dir_lookupbycookie(struct tmpfs_node *, off_t);
346 int	tmpfs_dir_getdents(struct tmpfs_node *, struct uio *, off_t *);
347 int	tmpfs_reg_resize(struct vnode *, off_t);
348 int	tmpfs_chflags(struct vnode *, int, kauth_cred_t, struct lwp *);
349 int	tmpfs_chmod(struct vnode *, mode_t, kauth_cred_t, struct lwp *);
350 int	tmpfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, struct lwp *);
351 int	tmpfs_chsize(struct vnode *, u_quad_t, kauth_cred_t, struct lwp *);
352 int	tmpfs_chtimes(struct vnode *, const struct timespec *,
353     const struct timespec *, const struct timespec *, int, kauth_cred_t,
354     struct lwp *);
355 void	tmpfs_itimes(struct vnode *, const struct timespec *,
356 	    const struct timespec *, const struct timespec *);
357 
358 void	tmpfs_update(struct vnode *, const struct timespec *,
359 	    const struct timespec *, const struct timespec *, int);
360 int	tmpfs_truncate(struct vnode *, off_t);
361 
362 /*
363  * Prototypes for tmpfs_mem.c.
364  */
365 
366 void		tmpfs_mntmem_init(struct tmpfs_mount *, uint64_t);
367 void		tmpfs_mntmem_destroy(struct tmpfs_mount *);
368 
369 size_t		tmpfs_mem_info(bool);
370 uint64_t	tmpfs_bytes_max(struct tmpfs_mount *);
371 size_t		tmpfs_pages_avail(struct tmpfs_mount *);
372 bool		tmpfs_mem_incr(struct tmpfs_mount *, size_t);
373 void		tmpfs_mem_decr(struct tmpfs_mount *, size_t);
374 
375 struct tmpfs_dirent *tmpfs_dirent_get(struct tmpfs_mount *);
376 void		tmpfs_dirent_put(struct tmpfs_mount *, struct tmpfs_dirent *);
377 
378 struct tmpfs_node *tmpfs_node_get(struct tmpfs_mount *);
379 void		tmpfs_node_put(struct tmpfs_mount *, struct tmpfs_node *);
380 
381 char *		tmpfs_strname_alloc(struct tmpfs_mount *, size_t);
382 void		tmpfs_strname_free(struct tmpfs_mount *, char *, size_t);
383 bool		tmpfs_strname_neqlen(struct componentname *, struct componentname *);
384 
385 /* --------------------------------------------------------------------- */
386 
387 /*
388  * Convenience macros to simplify some logical expressions.
389  */
390 #define IMPLIES(a, b) (!(a) || (b))
391 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
392 
393 /* --------------------------------------------------------------------- */
394 
395 /*
396  * Checks that the directory entry pointed by 'de' matches the name 'name'
397  * with a length of 'len'.
398  */
399 #define TMPFS_DIRENT_MATCHES(de, name, len) \
400     (de->td_namelen == (uint16_t)len && \
401     memcmp((de)->td_name, (name), (de)->td_namelen) == 0)
402 
403 /* --------------------------------------------------------------------- */
404 
405 /*
406  * Ensures that the node pointed by 'node' is a directory and that its
407  * contents are consistent with respect to directories.
408  */
409 #define TMPFS_VALIDATE_DIR(node) \
410     KASSERT((node)->tn_type == VDIR); \
411     KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0); \
412     KASSERT((node)->tn_spec.tn_dir.tn_readdir_lastp == NULL || \
413         tmpfs_dircookie((node)->tn_spec.tn_dir.tn_readdir_lastp) == \
414         (node)->tn_spec.tn_dir.tn_readdir_lastn);
415 
416 /* --------------------------------------------------------------------- */
417 
418 /*
419  * Memory management stuff.
420  */
421 
422 /* Amount of memory pages to reserve for the system (e.g., to not use by
423  * tmpfs).
424  * XXX: Should this be tunable through sysctl, for instance? */
425 #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
426 
427 /*
428  * Macros/functions to convert from generic data structures to tmpfs
429  * specific ones.
430  */
431 
432 static __inline
433 struct tmpfs_mount *
434 VFS_TO_TMPFS(struct mount *mp)
435 {
436 	struct tmpfs_mount *tmp;
437 
438 #ifdef KASSERT
439 	KASSERT((mp) != NULL && (mp)->mnt_data != NULL);
440 #endif
441 	tmp = (struct tmpfs_mount *)(mp)->mnt_data;
442 	return tmp;
443 }
444 
445 #endif /* defined(_KERNEL) */
446 
447 static __inline
448 struct tmpfs_node *
449 VP_TO_TMPFS_NODE(struct vnode *vp)
450 {
451 	struct tmpfs_node *node;
452 
453 #ifdef KASSERT
454 	KASSERT((vp) != NULL && (vp)->v_data != NULL);
455 #endif
456 	node = (struct tmpfs_node *)vp->v_data;
457 	return node;
458 }
459 
460 #if defined(_KERNEL)
461 
462 static __inline
463 struct tmpfs_node *
464 VP_TO_TMPFS_DIR(struct vnode *vp)
465 {
466 	struct tmpfs_node *node;
467 
468 	node = VP_TO_TMPFS_NODE(vp);
469 #ifdef KASSERT
470 	TMPFS_VALIDATE_DIR(node);
471 #endif
472 	return node;
473 }
474 
475 #endif /* defined(_KERNEL) */
476 #endif /* _FS_TMPFS_TMPFS_H_ */
477