tmpfs_vfsops.c revision 1.50 1 1.50 rmind /* $NetBSD: tmpfs_vfsops.c,v 1.50 2011/05/24 20:17:49 rmind Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.34 ad * Copyright (c) 2005, 2006, 2007 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.6 jmmv * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 1.6 jmmv * 2005 program.
10 1.1 jmmv *
11 1.1 jmmv * Redistribution and use in source and binary forms, with or without
12 1.1 jmmv * modification, are permitted provided that the following conditions
13 1.1 jmmv * are met:
14 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
15 1.1 jmmv * notice, this list of conditions and the following disclaimer.
16 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
18 1.1 jmmv * documentation and/or other materials provided with the distribution.
19 1.1 jmmv *
20 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
31 1.1 jmmv */
32 1.1 jmmv
33 1.1 jmmv /*
34 1.2 jmmv * Efficient memory file system.
35 1.6 jmmv *
36 1.6 jmmv * tmpfs is a file system that uses NetBSD's virtual memory sub-system
37 1.6 jmmv * (the well-known UVM) to store file data and metadata in an efficient
38 1.6 jmmv * way. This means that it does not follow the structure of an on-disk
39 1.6 jmmv * file system because it simply does not need to. Instead, it uses
40 1.6 jmmv * memory-specific data structures and algorithms to automatically
41 1.6 jmmv * allocate and release resources.
42 1.1 jmmv */
43 1.1 jmmv
44 1.1 jmmv #include <sys/cdefs.h>
45 1.50 rmind __KERNEL_RCSID(0, "$NetBSD: tmpfs_vfsops.c,v 1.50 2011/05/24 20:17:49 rmind Exp $");
46 1.1 jmmv
47 1.1 jmmv #include <sys/param.h>
48 1.1 jmmv #include <sys/types.h>
49 1.34 ad #include <sys/kmem.h>
50 1.1 jmmv #include <sys/mount.h>
51 1.8 jmmv #include <sys/stat.h>
52 1.1 jmmv #include <sys/systm.h>
53 1.1 jmmv #include <sys/vnode.h>
54 1.41 rumble #include <sys/module.h>
55 1.1 jmmv
56 1.36 dholland #include <miscfs/genfs/genfs.h>
57 1.1 jmmv #include <fs/tmpfs/tmpfs.h>
58 1.44 pooka #include <fs/tmpfs/tmpfs_args.h>
59 1.1 jmmv
60 1.41 rumble MODULE(MODULE_CLASS_VFS, tmpfs, NULL);
61 1.41 rumble
62 1.48 rmind struct pool tmpfs_dirent_pool;
63 1.48 rmind struct pool tmpfs_node_pool;
64 1.1 jmmv
65 1.32 pooka static int tmpfs_mount(struct mount *, const char *, void *, size_t *);
66 1.32 pooka static int tmpfs_start(struct mount *, int);
67 1.32 pooka static int tmpfs_unmount(struct mount *, int);
68 1.50 rmind static int tmpfs_root(struct mount *, vnode_t **);
69 1.50 rmind static int tmpfs_vget(struct mount *, ino_t, vnode_t **);
70 1.50 rmind static int tmpfs_fhtovp(struct mount *, struct fid *, vnode_t **);
71 1.13 martin static int tmpfs_vptofh(struct vnode *, struct fid *, size_t *);
72 1.32 pooka static int tmpfs_statvfs(struct mount *, struct statvfs *);
73 1.32 pooka static int tmpfs_sync(struct mount *, int, kauth_cred_t);
74 1.1 jmmv static void tmpfs_init(void);
75 1.1 jmmv static void tmpfs_done(void);
76 1.50 rmind static int tmpfs_snapshot(struct mount *, vnode_t *, struct timespec *);
77 1.1 jmmv
78 1.48 rmind static void
79 1.48 rmind tmpfs_init(void)
80 1.48 rmind {
81 1.48 rmind
82 1.50 rmind pool_init(&tmpfs_dirent_pool, sizeof(tmpfs_dirent_t), 0, 0, 0,
83 1.48 rmind "tmpfs_dirent", &pool_allocator_nointr, IPL_NONE);
84 1.50 rmind pool_init(&tmpfs_node_pool, sizeof(tmpfs_node_t), 0, 0, 0,
85 1.48 rmind "tmpfs_node", &pool_allocator_nointr, IPL_NONE);
86 1.48 rmind }
87 1.48 rmind
88 1.48 rmind static void
89 1.48 rmind tmpfs_done(void)
90 1.48 rmind {
91 1.48 rmind
92 1.48 rmind pool_destroy(&tmpfs_dirent_pool);
93 1.48 rmind pool_destroy(&tmpfs_node_pool);
94 1.48 rmind }
95 1.1 jmmv
96 1.1 jmmv static int
97 1.32 pooka tmpfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
98 1.1 jmmv {
99 1.23 dsl struct tmpfs_args *args = data;
100 1.50 rmind tmpfs_mount_t *tmp;
101 1.50 rmind tmpfs_node_t *root;
102 1.45 rmind uint64_t memlimit;
103 1.45 rmind ino_t nodes;
104 1.45 rmind int error;
105 1.23 dsl
106 1.49 rmind /* Validate the version. */
107 1.49 rmind if (*data_len < sizeof(*args) ||
108 1.49 rmind args->ta_version != TMPFS_ARGS_VERSION)
109 1.23 dsl return EINVAL;
110 1.1 jmmv
111 1.1 jmmv /* Handle retrieval of mount point arguments. */
112 1.1 jmmv if (mp->mnt_flag & MNT_GETARGS) {
113 1.1 jmmv if (mp->mnt_data == NULL)
114 1.1 jmmv return EIO;
115 1.1 jmmv tmp = VFS_TO_TMPFS(mp);
116 1.1 jmmv
117 1.23 dsl args->ta_version = TMPFS_ARGS_VERSION;
118 1.23 dsl args->ta_nodes_max = tmp->tm_nodes_max;
119 1.45 rmind args->ta_size_max = tmp->tm_mem_limit;
120 1.31 ad
121 1.30 ad root = tmp->tm_root;
122 1.23 dsl args->ta_root_uid = root->tn_uid;
123 1.23 dsl args->ta_root_gid = root->tn_gid;
124 1.23 dsl args->ta_root_mode = root->tn_mode;
125 1.1 jmmv
126 1.45 rmind *data_len = sizeof(*args);
127 1.23 dsl return 0;
128 1.1 jmmv }
129 1.1 jmmv
130 1.1 jmmv if (mp->mnt_flag & MNT_UPDATE) {
131 1.49 rmind /* TODO */
132 1.1 jmmv return EOPNOTSUPP;
133 1.1 jmmv }
134 1.1 jmmv
135 1.49 rmind /* Prohibit mounts if there is not enough memory. */
136 1.20 thorpej if (tmpfs_mem_info(true) < TMPFS_PAGES_RESERVED)
137 1.1 jmmv return EINVAL;
138 1.1 jmmv
139 1.45 rmind /* Get the memory usage limit for this file-system. */
140 1.45 rmind if (args->ta_size_max < PAGE_SIZE) {
141 1.45 rmind memlimit = UINT64_MAX;
142 1.45 rmind } else {
143 1.45 rmind memlimit = args->ta_size_max;
144 1.45 rmind }
145 1.45 rmind KASSERT(memlimit > 0);
146 1.45 rmind
147 1.45 rmind if (args->ta_nodes_max <= 3) {
148 1.45 rmind nodes = 3 + (memlimit / 1024);
149 1.45 rmind } else {
150 1.23 dsl nodes = args->ta_nodes_max;
151 1.45 rmind }
152 1.45 rmind nodes = MIN(nodes, INT_MAX);
153 1.7 jmmv KASSERT(nodes >= 3);
154 1.1 jmmv
155 1.1 jmmv /* Allocate the tmpfs mount structure and fill it. */
156 1.50 rmind tmp = kmem_zalloc(sizeof(tmpfs_mount_t), KM_SLEEP);
157 1.34 ad if (tmp == NULL)
158 1.34 ad return ENOMEM;
159 1.1 jmmv
160 1.1 jmmv tmp->tm_nodes_max = nodes;
161 1.34 ad tmp->tm_nodes_cnt = 0;
162 1.34 ad LIST_INIT(&tmp->tm_nodes);
163 1.34 ad
164 1.34 ad mutex_init(&tmp->tm_lock, MUTEX_DEFAULT, IPL_NONE);
165 1.45 rmind tmpfs_mntmem_init(tmp, memlimit);
166 1.1 jmmv
167 1.1 jmmv /* Allocate the root node. */
168 1.23 dsl error = tmpfs_alloc_node(tmp, VDIR, args->ta_root_uid,
169 1.23 dsl args->ta_root_gid, args->ta_root_mode & ALLPERMS, NULL, NULL,
170 1.33 pooka VNOVAL, &root);
171 1.1 jmmv KASSERT(error == 0 && root != NULL);
172 1.34 ad root->tn_links++;
173 1.1 jmmv tmp->tm_root = root;
174 1.1 jmmv
175 1.1 jmmv mp->mnt_data = tmp;
176 1.1 jmmv mp->mnt_flag |= MNT_LOCAL;
177 1.1 jmmv mp->mnt_stat.f_namemax = MAXNAMLEN;
178 1.27 pooka mp->mnt_fs_bshift = PAGE_SHIFT;
179 1.27 pooka mp->mnt_dev_bshift = DEV_BSHIFT;
180 1.34 ad mp->mnt_iflag |= IMNT_MPSAFE;
181 1.1 jmmv vfs_getnewfsid(mp);
182 1.1 jmmv
183 1.48 rmind error = set_statvfs_info(path, UIO_USERSPACE, "tmpfs", UIO_SYSSPACE,
184 1.45 rmind mp->mnt_op->vfs_name, mp, curlwp);
185 1.48 rmind if (error) {
186 1.48 rmind (void)tmpfs_unmount(mp, MNT_FORCE);
187 1.48 rmind }
188 1.48 rmind return error;
189 1.1 jmmv }
190 1.1 jmmv
191 1.1 jmmv static int
192 1.32 pooka tmpfs_start(struct mount *mp, int flags)
193 1.1 jmmv {
194 1.1 jmmv
195 1.1 jmmv return 0;
196 1.1 jmmv }
197 1.1 jmmv
198 1.1 jmmv static int
199 1.32 pooka tmpfs_unmount(struct mount *mp, int mntflags)
200 1.1 jmmv {
201 1.50 rmind tmpfs_mount_t *tmp;
202 1.50 rmind tmpfs_node_t *node;
203 1.48 rmind int error, flags = 0;
204 1.1 jmmv
205 1.1 jmmv /* Handle forced unmounts. */
206 1.1 jmmv if (mntflags & MNT_FORCE)
207 1.1 jmmv flags |= FORCECLOSE;
208 1.1 jmmv
209 1.1 jmmv /* Finalize all pending I/O. */
210 1.1 jmmv error = vflush(mp, NULL, flags);
211 1.1 jmmv if (error != 0)
212 1.1 jmmv return error;
213 1.1 jmmv
214 1.1 jmmv tmp = VFS_TO_TMPFS(mp);
215 1.1 jmmv
216 1.50 rmind /* Destroy any existing inodes. */
217 1.50 rmind while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) {
218 1.1 jmmv if (node->tn_type == VDIR) {
219 1.50 rmind tmpfs_dirent_t *de;
220 1.1 jmmv
221 1.50 rmind /* Destroy any directory entries. */
222 1.11 jmmv de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
223 1.1 jmmv while (de != NULL) {
224 1.50 rmind tmpfs_dirent_t *nde;
225 1.1 jmmv
226 1.1 jmmv nde = TAILQ_NEXT(de, td_entries);
227 1.20 thorpej tmpfs_free_dirent(tmp, de, false);
228 1.50 rmind node->tn_size -= sizeof(tmpfs_dirent_t);
229 1.1 jmmv de = nde;
230 1.1 jmmv }
231 1.1 jmmv }
232 1.50 rmind /* Removes inode from the list. */
233 1.1 jmmv tmpfs_free_node(tmp, node);
234 1.31 ad }
235 1.1 jmmv
236 1.1 jmmv /* Throw away the tmpfs_mount structure. */
237 1.45 rmind tmpfs_mntmem_destroy(tmp);
238 1.34 ad mutex_destroy(&tmp->tm_lock);
239 1.34 ad kmem_free(tmp, sizeof(*tmp));
240 1.1 jmmv mp->mnt_data = NULL;
241 1.1 jmmv
242 1.1 jmmv return 0;
243 1.1 jmmv }
244 1.1 jmmv
245 1.1 jmmv static int
246 1.50 rmind tmpfs_root(struct mount *mp, vnode_t **vpp)
247 1.1 jmmv {
248 1.1 jmmv
249 1.1 jmmv return tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, vpp);
250 1.1 jmmv }
251 1.1 jmmv
252 1.1 jmmv static int
253 1.50 rmind tmpfs_vget(struct mount *mp, ino_t ino, vnode_t **vpp)
254 1.1 jmmv {
255 1.1 jmmv
256 1.1 jmmv printf("tmpfs_vget called; need for it unknown yet\n");
257 1.1 jmmv return EOPNOTSUPP;
258 1.1 jmmv }
259 1.1 jmmv
260 1.1 jmmv static int
261 1.50 rmind tmpfs_fhtovp(struct mount *mp, struct fid *fhp, vnode_t **vpp)
262 1.1 jmmv {
263 1.50 rmind tmpfs_mount_t *tmp;
264 1.50 rmind tmpfs_node_t *node;
265 1.50 rmind tmpfs_fid_t tfh;
266 1.19 thorpej bool found;
267 1.1 jmmv
268 1.1 jmmv tmp = VFS_TO_TMPFS(mp);
269 1.1 jmmv
270 1.50 rmind if (fhp->fid_len != sizeof(tmpfs_fid_t))
271 1.1 jmmv return EINVAL;
272 1.1 jmmv
273 1.50 rmind memcpy(&tfh, fhp, sizeof(tmpfs_fid_t));
274 1.13 martin
275 1.20 thorpej found = false;
276 1.34 ad mutex_enter(&tmp->tm_lock);
277 1.34 ad LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
278 1.13 martin if (node->tn_id == tfh.tf_id &&
279 1.13 martin node->tn_gen == tfh.tf_gen) {
280 1.20 thorpej found = true;
281 1.1 jmmv break;
282 1.1 jmmv }
283 1.1 jmmv }
284 1.34 ad mutex_exit(&tmp->tm_lock);
285 1.1 jmmv
286 1.34 ad /* XXXAD nothing to prevent 'node' from being removed. */
287 1.47 hannken return found ? tmpfs_alloc_vp(mp, node, vpp) : ESTALE;
288 1.1 jmmv }
289 1.1 jmmv
290 1.1 jmmv static int
291 1.50 rmind tmpfs_vptofh(vnode_t *vp, struct fid *fhp, size_t *fh_size)
292 1.1 jmmv {
293 1.50 rmind tmpfs_fid_t tfh;
294 1.50 rmind tmpfs_node_t *node;
295 1.1 jmmv
296 1.50 rmind if (*fh_size < sizeof(tmpfs_fid_t)) {
297 1.50 rmind *fh_size = sizeof(tmpfs_fid_t);
298 1.13 martin return E2BIG;
299 1.13 martin }
300 1.50 rmind *fh_size = sizeof(tmpfs_fid_t);
301 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
302 1.1 jmmv
303 1.13 martin memset(&tfh, 0, sizeof(tfh));
304 1.50 rmind tfh.tf_len = sizeof(tmpfs_fid_t);
305 1.13 martin tfh.tf_gen = node->tn_gen;
306 1.13 martin tfh.tf_id = node->tn_id;
307 1.13 martin memcpy(fhp, &tfh, sizeof(tfh));
308 1.1 jmmv
309 1.1 jmmv return 0;
310 1.1 jmmv }
311 1.1 jmmv
312 1.1 jmmv static int
313 1.32 pooka tmpfs_statvfs(struct mount *mp, struct statvfs *sbp)
314 1.1 jmmv {
315 1.50 rmind tmpfs_mount_t *tmp;
316 1.34 ad fsfilcnt_t freenodes;
317 1.46 rmind size_t avail;
318 1.1 jmmv
319 1.1 jmmv tmp = VFS_TO_TMPFS(mp);
320 1.1 jmmv
321 1.1 jmmv sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
322 1.1 jmmv
323 1.46 rmind mutex_enter(&tmp->tm_acc_lock);
324 1.46 rmind avail = tmpfs_pages_avail(tmp);
325 1.45 rmind sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
326 1.46 rmind sbp->f_bavail = sbp->f_bfree = avail;
327 1.1 jmmv sbp->f_bresvd = 0;
328 1.1 jmmv
329 1.34 ad freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
330 1.50 rmind avail * PAGE_SIZE / sizeof(tmpfs_node_t));
331 1.31 ad
332 1.34 ad sbp->f_files = tmp->tm_nodes_cnt + freenodes;
333 1.1 jmmv sbp->f_favail = sbp->f_ffree = freenodes;
334 1.1 jmmv sbp->f_fresvd = 0;
335 1.46 rmind mutex_exit(&tmp->tm_acc_lock);
336 1.1 jmmv
337 1.1 jmmv copy_statvfs_info(sbp, mp);
338 1.1 jmmv
339 1.1 jmmv return 0;
340 1.1 jmmv }
341 1.1 jmmv
342 1.1 jmmv static int
343 1.50 rmind tmpfs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
344 1.1 jmmv {
345 1.1 jmmv
346 1.1 jmmv return 0;
347 1.1 jmmv }
348 1.1 jmmv
349 1.1 jmmv static int
350 1.50 rmind tmpfs_snapshot(struct mount *mp, vnode_t *vp, struct timespec *ctime)
351 1.1 jmmv {
352 1.1 jmmv
353 1.1 jmmv return EOPNOTSUPP;
354 1.1 jmmv }
355 1.1 jmmv
356 1.1 jmmv /*
357 1.1 jmmv * tmpfs vfs operations.
358 1.1 jmmv */
359 1.1 jmmv
360 1.1 jmmv extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
361 1.1 jmmv extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
362 1.1 jmmv extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
363 1.1 jmmv
364 1.1 jmmv const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
365 1.1 jmmv &tmpfs_fifoop_opv_desc,
366 1.1 jmmv &tmpfs_specop_opv_desc,
367 1.1 jmmv &tmpfs_vnodeop_opv_desc,
368 1.1 jmmv NULL,
369 1.1 jmmv };
370 1.1 jmmv
371 1.1 jmmv struct vfsops tmpfs_vfsops = {
372 1.1 jmmv MOUNT_TMPFS, /* vfs_name */
373 1.23 dsl sizeof (struct tmpfs_args),
374 1.1 jmmv tmpfs_mount, /* vfs_mount */
375 1.1 jmmv tmpfs_start, /* vfs_start */
376 1.1 jmmv tmpfs_unmount, /* vfs_unmount */
377 1.1 jmmv tmpfs_root, /* vfs_root */
378 1.32 pooka (void *)eopnotsupp, /* vfs_quotactl */
379 1.1 jmmv tmpfs_statvfs, /* vfs_statvfs */
380 1.1 jmmv tmpfs_sync, /* vfs_sync */
381 1.1 jmmv tmpfs_vget, /* vfs_vget */
382 1.1 jmmv tmpfs_fhtovp, /* vfs_fhtovp */
383 1.1 jmmv tmpfs_vptofh, /* vfs_vptofh */
384 1.1 jmmv tmpfs_init, /* vfs_init */
385 1.1 jmmv NULL, /* vfs_reinit */
386 1.1 jmmv tmpfs_done, /* vfs_done */
387 1.1 jmmv NULL, /* vfs_mountroot */
388 1.1 jmmv tmpfs_snapshot, /* vfs_snapshot */
389 1.1 jmmv vfs_stdextattrctl, /* vfs_extattrctl */
390 1.25 pooka (void *)eopnotsupp, /* vfs_suspendctl */
391 1.36 dholland genfs_renamelock_enter,
392 1.36 dholland genfs_renamelock_exit,
393 1.40 ad (void *)eopnotsupp,
394 1.1 jmmv tmpfs_vnodeopv_descs,
395 1.14 christos 0, /* vfs_refcount */
396 1.14 christos { NULL, NULL },
397 1.1 jmmv };
398 1.41 rumble
399 1.41 rumble static int
400 1.41 rumble tmpfs_modcmd(modcmd_t cmd, void *arg)
401 1.41 rumble {
402 1.41 rumble
403 1.41 rumble switch (cmd) {
404 1.41 rumble case MODULE_CMD_INIT:
405 1.41 rumble return vfs_attach(&tmpfs_vfsops);
406 1.41 rumble case MODULE_CMD_FINI:
407 1.41 rumble return vfs_detach(&tmpfs_vfsops);
408 1.41 rumble default:
409 1.41 rumble return ENOTTY;
410 1.41 rumble }
411 1.41 rumble }
412