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