tmpfs.h revision 1.2 1 /* $NetBSD: tmpfs.h,v 1.2 2005/09/10 22:28:57 jmmv Exp $ */
2
3 /*
4 * Copyright (c) 2005 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.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #if !defined(_TMPFS_H_)
40 # define _TMPFS_H_
41 #else
42 # error "tmpfs.h cannot be included multiple times."
43 #endif
44
45 /* ---------------------------------------------------------------------
46 * KERNEL-SPECIFIC DEFINITIONS
47 * --------------------------------------------------------------------- */
48
49 #if defined(_KERNEL)
50
51 #include <sys/dirent.h>
52 #include <sys/mount.h>
53 #include <sys/queue.h>
54 #include <sys/vnode.h>
55
56 #include <fs/tmpfs/tmpfs_pool.h>
57
58 /* --------------------------------------------------------------------- */
59
60 /*
61 * This structure holds a directory entry.
62 */
63 struct tmpfs_dirent {
64 TAILQ_ENTRY(tmpfs_dirent) td_entries;
65 uint16_t td_namelen;
66 char * td_name;
67 struct tmpfs_node * td_node;
68 };
69 TAILQ_HEAD(tmpfs_dir, tmpfs_dirent);
70
71 /* --------------------------------------------------------------------- */
72
73 /*
74 * This structure represents a node within tmpfs.
75 */
76 struct tmpfs_node {
77 LIST_ENTRY(tmpfs_node) tn_entries;
78
79 enum vtype tn_type;
80 ino_t tn_id;
81
82 #define TMPFS_NODE_ACCESSED (1 << 1)
83 #define TMPFS_NODE_MODIFIED (1 << 2)
84 #define TMPFS_NODE_CHANGED (1 << 3)
85 int tn_status;
86
87 off_t tn_size;
88
89 /* Attributes. */
90 uid_t tn_uid;
91 gid_t tn_gid;
92 mode_t tn_mode;
93 int tn_flags;
94 nlink_t tn_links;
95 struct timespec tn_atime;
96 struct timespec tn_mtime;
97 struct timespec tn_ctime;
98 struct timespec tn_birthtime;
99 unsigned long tn_gen;
100
101 struct vnode * tn_vnode;
102
103 /* Used by tmpfs_lookup to store the affected directory entry during
104 * DELETE and RENAME operations. */
105 struct tmpfs_dirent * tn_lookup_dirent;
106
107 union {
108 /* Valid when tn_type == VBLK || tn_type == VCHR. */
109 struct {
110 dev_t tn_rdev;
111 };
112
113 /* Valid when tn_type == VDIR. */
114 struct {
115 struct tmpfs_node * tn_parent;
116 struct tmpfs_dir tn_dir;
117
118 /* Used by tmpfs_readdir to speed up lookups. */
119 long tn_readdir_lastn;
120 struct tmpfs_dirent * tn_readdir_lastp;
121 };
122
123 /* Valid when tn_type == VLNK. */
124 struct {
125 char * tn_link;
126 };
127
128 /* Valid when tn_type == VREG. */
129 struct {
130 struct uvm_object * tn_aobj;
131 size_t tn_aobj_pages;
132 vaddr_t tn_va;
133 };
134 };
135 };
136 LIST_HEAD(tmpfs_node_list, tmpfs_node);
137
138 /* --------------------------------------------------------------------- */
139
140 /*
141 * This structure holds the information relative to a tmpfs instance.
142 */
143 struct tmpfs_mount {
144 size_t tm_pages_max;
145 size_t tm_pages_used;
146
147 struct tmpfs_node * tm_root;
148 struct netexport tm_export;
149
150 ino_t tm_nodes_max;
151 ino_t tm_nodes_last;
152 struct tmpfs_node_list tm_nodes_used;
153 struct tmpfs_node_list tm_nodes_avail;
154
155 struct tmpfs_pool tm_dirent_pool;
156 struct tmpfs_pool tm_node_pool;
157 struct tmpfs_str_pool tm_str_pool;
158 };
159
160 /* --------------------------------------------------------------------- */
161
162 /*
163 * This structure maps a file identifier to a tmpfs node. Used by the
164 * NFS code.
165 */
166 struct tmpfs_fid {
167 uint16_t tf_len;
168 uint16_t tf_pad;
169 ino_t tf_id;
170 unsigned long tf_gen;
171 };
172
173 /* --------------------------------------------------------------------- */
174
175 /*
176 * Prototypes for tmpfs_subr.c.
177 */
178
179 int tmpfs_alloc_node(struct tmpfs_mount *, enum vtype,
180 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *,
181 char *, dev_t, struct proc *, struct tmpfs_node **);
182 void tmpfs_free_node(struct tmpfs_mount *, struct tmpfs_node *);
183 int tmpfs_alloc_dirent(struct tmpfs_mount *, struct tmpfs_node *,
184 const char *, uint16_t, struct tmpfs_dirent **);
185 void tmpfs_free_dirent(struct tmpfs_mount *, struct tmpfs_dirent *,
186 boolean_t);
187 int tmpfs_alloc_vp(struct mount *, struct tmpfs_node *, struct vnode **);
188 void tmpfs_free_vp(struct vnode *);
189 int tmpfs_alloc_file(struct vnode *, struct vnode **, struct vattr *,
190 struct componentname *, char *);
191 void tmpfs_dir_attach(struct vnode *, struct tmpfs_dirent *);
192 void tmpfs_dir_detach(struct vnode *, struct tmpfs_dirent *);
193 struct tmpfs_dirent * tmpfs_dir_lookup(struct tmpfs_node *node,
194 struct componentname *cnp);
195 int tmpfs_dir_getdotdent(struct tmpfs_node *, struct uio *);
196 int tmpfs_dir_getdotdotdent(struct tmpfs_node *, struct uio *);
197 int tmpfs_dir_getdents(struct tmpfs_node *, struct uio *);
198 int tmpfs_reg_resize(struct vnode *, off_t);
199 size_t tmpfs_mem_info(boolean_t);
200 int tmpfs_chflags(struct vnode *, int, struct ucred *, struct proc *);
201 int tmpfs_chmod(struct vnode *, mode_t, struct ucred *, struct proc *);
202 int tmpfs_chown(struct vnode *, uid_t, gid_t, struct ucred *,
203 struct proc *);
204 int tmpfs_chsize(struct vnode *, u_quad_t, struct ucred *, struct proc *);
205 int tmpfs_chtimes(struct vnode *, struct timespec *, struct timespec *,
206 int, struct ucred *, struct proc *);
207
208 /* --------------------------------------------------------------------- */
209
210 /*
211 * Convenience macros to simplify some logical expressions.
212 */
213 #define IMPLIES(a, b) (!(a) || (b))
214 #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
215
216 /* --------------------------------------------------------------------- */
217
218 /*
219 * Checks that the directory entry pointed by 'de' matches the name 'name'
220 * with a length of 'len'.
221 */
222 #define TMPFS_DIRENT_MATCHES(de, name, len) \
223 (de->td_namelen == (uint16_t)len && \
224 memcmp((de)->td_name, (name), (de)->td_namelen) == 0)
225
226 /* --------------------------------------------------------------------- */
227
228 /*
229 * Ensures that the node pointed by 'node' is a directory and that its
230 * contents are consistent with respect to directories.
231 */
232 #define TMPFS_VALIDATE_DIR(node) \
233 KASSERT((node)->tn_type == VDIR); \
234 KASSERT((node)->tn_size % sizeof(struct tmpfs_dirent) == 0);
235
236 /* --------------------------------------------------------------------- */
237
238 /*
239 * Memory management stuff.
240 */
241
242 /* Amount of memory pages to reserve for the system (e.g., to not use by
243 * tmpfs).
244 * XXX: Should this be tunable through sysctl, for instance? */
245 #define TMPFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
246
247 /* Returns the available memory for the given file system, which can be
248 * its limit (set during mount time) or the amount of free memory, whichever
249 * is lower. */
250 static inline size_t
251 TMPFS_PAGES_MAX(struct tmpfs_mount *tmp)
252 {
253 size_t freepages;
254
255 freepages = tmpfs_mem_info(FALSE);
256 if (freepages < TMPFS_PAGES_RESERVED)
257 freepages = 0;
258 else
259 freepages -= TMPFS_PAGES_RESERVED;
260
261 return MIN(tmp->tm_pages_max, freepages + tmp->tm_pages_used);
262 }
263
264 #define TMPFS_PAGES_AVAIL(tmp) (TMPFS_PAGES_MAX(tmp) - (tmp)->tm_pages_used)
265
266 /* --------------------------------------------------------------------- */
267
268 /*
269 * Macros/functions to convert from generic data structures to tmpfs
270 * specific ones.
271 *
272 * Macros are used when no sanity checks have to be done, as they provide
273 * the fastest conversion. On the other hand, inlined functions are used
274 * when expensive sanity checks are enabled, mostly because the checks
275 * have to be done separately from the return value.
276 */
277
278 #if defined(DIAGNOSTIC)
279 static inline
280 struct tmpfs_mount *
281 VFS_TO_TMPFS(struct mount *mp)
282 {
283 struct tmpfs_mount *tmp;
284
285 KASSERT((mp) != NULL && (mp)->mnt_data != NULL);
286 tmp = (struct tmpfs_mount *)(mp)->mnt_data;
287 KASSERT(TMPFS_PAGES_MAX(tmp) >= tmp->tm_pages_used);
288 return tmp;
289 }
290
291 static inline
292 struct tmpfs_node *
293 VP_TO_TMPFS_NODE(struct vnode *vp)
294 {
295 struct tmpfs_node *node;
296
297 KASSERT((vp) != NULL && (vp)->v_data != NULL);
298 node = (struct tmpfs_node *)vp->v_data;
299 KASSERT(node->tn_size == vp->v_size);
300 return node;
301 }
302
303 static inline
304 struct tmpfs_node *
305 VP_TO_TMPFS_DIR(struct vnode *vp)
306 {
307 struct tmpfs_node *node;
308
309 node = VP_TO_TMPFS_NODE(vp);
310 TMPFS_VALIDATE_DIR(node);
311 return node;
312 }
313 #else
314 # define VFS_TO_TMPFS(mp) ((struct tmpfs_mount *)mp->mnt_data)
315 # define VP_TO_TMPFS_NODE(vp) ((struct tmpfs_node *)vp->v_data)
316 # define VP_TO_TMPFS_DIR(vp) VP_TO_TMPFS_NODE(vp)
317 #endif
318
319 #endif /* _KERNEL */
320
321 /* ---------------------------------------------------------------------
322 * USER AND KERNEL DEFINITIONS
323 * --------------------------------------------------------------------- */
324
325 /*
326 * This structure is used to communicate mount parameters between userland
327 * and kernel space.
328 */
329 #define TMPFS_ARGS_VERSION 1
330 struct tmpfs_args {
331 int ta_version;
332
333 /* Size counters. */
334 ino_t ta_nodes_max;
335 off_t ta_size_max;
336
337 /* Root node attributes. */
338 uid_t ta_root_uid;
339 gid_t ta_root_gid;
340 mode_t ta_root_mode;
341
342 /* Used to update NFS export properties. Due to the way these
343 * fields are used by mountd(8), they must come first, but as
344 * I would like to remove this restriction, I'm placing them
345 * here (there is no problem in doing so because, ATM, mountd(8)
346 * does not recognize tmpfs). XXX */
347 const char * ta_fspec;
348 struct export_args ta_export;
349 };
350