tmpfs_vfsops.c revision 1.65 1 /* $NetBSD: tmpfs_vfsops.c,v 1.65 2015/07/06 10:07:12 hannken 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 /*
34 * Efficient memory file system.
35 *
36 * tmpfs is a file system that uses NetBSD's virtual memory sub-system
37 * (the well-known UVM) to store file data and metadata in an efficient
38 * way. This means that it does not follow the structure of an on-disk
39 * file system because it simply does not need to. Instead, it uses
40 * memory-specific data structures and algorithms to automatically
41 * allocate and release resources.
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vfsops.c,v 1.65 2015/07/06 10:07:12 hannken Exp $");
46
47 #include <sys/param.h>
48 #include <sys/atomic.h>
49 #include <sys/types.h>
50 #include <sys/kmem.h>
51 #include <sys/mount.h>
52 #include <sys/stat.h>
53 #include <sys/systm.h>
54 #include <sys/vnode.h>
55 #include <sys/kauth.h>
56 #include <sys/module.h>
57
58 #include <miscfs/genfs/genfs.h>
59 #include <fs/tmpfs/tmpfs.h>
60 #include <fs/tmpfs/tmpfs_args.h>
61
62 MODULE(MODULE_CLASS_VFS, tmpfs, NULL);
63
64 struct pool tmpfs_dirent_pool;
65 struct pool tmpfs_node_pool;
66
67 void
68 tmpfs_init(void)
69 {
70
71 pool_init(&tmpfs_dirent_pool, sizeof(tmpfs_dirent_t), 0, 0, 0,
72 "tmpfs_dirent", &pool_allocator_nointr, IPL_NONE);
73 pool_init(&tmpfs_node_pool, sizeof(tmpfs_node_t), 0, 0, 0,
74 "tmpfs_node", &pool_allocator_nointr, IPL_NONE);
75 }
76
77 void
78 tmpfs_done(void)
79 {
80
81 pool_destroy(&tmpfs_dirent_pool);
82 pool_destroy(&tmpfs_node_pool);
83 }
84
85 int
86 tmpfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
87 {
88 struct tmpfs_args *args = data;
89 tmpfs_mount_t *tmp;
90 tmpfs_node_t *root;
91 struct vattr va;
92 struct vnode *vp;
93 uint64_t memlimit;
94 ino_t nodes;
95 int error;
96
97 if (args == NULL)
98 return EINVAL;
99
100 /* Validate the version. */
101 if (*data_len < sizeof(*args) ||
102 args->ta_version != TMPFS_ARGS_VERSION)
103 return EINVAL;
104
105 /* Handle retrieval of mount point arguments. */
106 if (mp->mnt_flag & MNT_GETARGS) {
107 if (mp->mnt_data == NULL)
108 return EIO;
109 tmp = VFS_TO_TMPFS(mp);
110
111 args->ta_version = TMPFS_ARGS_VERSION;
112 args->ta_nodes_max = tmp->tm_nodes_max;
113 args->ta_size_max = tmp->tm_mem_limit;
114
115 root = tmp->tm_root;
116 args->ta_root_uid = root->tn_uid;
117 args->ta_root_gid = root->tn_gid;
118 args->ta_root_mode = root->tn_mode;
119
120 *data_len = sizeof(*args);
121 return 0;
122 }
123
124
125 /* Prohibit mounts if there is not enough memory. */
126 if (tmpfs_mem_info(true) < uvmexp.freetarg)
127 return EINVAL;
128
129 /* Check for invalid uid and gid arguments */
130 if (args->ta_root_uid == VNOVAL || args->ta_root_gid == VNOVAL)
131 return EINVAL;
132
133 /* This can never happen? */
134 if ((args->ta_root_mode & ALLPERMS) == VNOVAL)
135 return EINVAL;
136
137 /* Get the memory usage limit for this file-system. */
138 if (args->ta_size_max < PAGE_SIZE) {
139 memlimit = UINT64_MAX;
140 } else {
141 memlimit = args->ta_size_max;
142 }
143 KASSERT(memlimit > 0);
144
145 if (args->ta_nodes_max <= 3) {
146 nodes = 3 + (memlimit / 1024);
147 } else {
148 nodes = args->ta_nodes_max;
149 }
150 nodes = MIN(nodes, INT_MAX);
151 KASSERT(nodes >= 3);
152
153 if (mp->mnt_flag & MNT_UPDATE) {
154 tmp = VFS_TO_TMPFS(mp);
155 if (nodes < tmp->tm_nodes_cnt)
156 return EBUSY;
157 if ((error = tmpfs_mntmem_set(tmp, memlimit)) != 0)
158 return error;
159 tmp->tm_nodes_max = nodes;
160 root = tmp->tm_root;
161 root->tn_uid = args->ta_root_uid;
162 root->tn_gid = args->ta_root_gid;
163 root->tn_mode = args->ta_root_mode;
164 return 0;
165 }
166
167 /* Allocate the tmpfs mount structure and fill it. */
168 tmp = kmem_zalloc(sizeof(tmpfs_mount_t), KM_SLEEP);
169 if (tmp == NULL)
170 return ENOMEM;
171
172 tmp->tm_nodes_max = nodes;
173 tmp->tm_nodes_cnt = 0;
174 LIST_INIT(&tmp->tm_nodes);
175
176 mutex_init(&tmp->tm_lock, MUTEX_DEFAULT, IPL_NONE);
177 tmpfs_mntmem_init(tmp, memlimit);
178 mp->mnt_data = tmp;
179
180 /* Allocate the root node. */
181 vattr_null(&va);
182 va.va_type = VDIR;
183 va.va_mode = args->ta_root_mode & ALLPERMS;
184 va.va_uid = args->ta_root_uid;
185 va.va_gid = args->ta_root_gid;
186 error = vcache_new(mp, NULL, &va, NOCRED, &vp);
187 root = VP_TO_TMPFS_NODE(vp);
188 KASSERT(error == 0 && root != NULL);
189
190 /*
191 * Parent of the root inode is itself. Also, root inode has no
192 * directory entry (i.e. is never attached), thus hold an extra
193 * reference (link) for it.
194 */
195 root->tn_links++;
196 root->tn_spec.tn_dir.tn_parent = root;
197 tmp->tm_root = root;
198 vrele(vp);
199
200 mp->mnt_flag |= MNT_LOCAL;
201 mp->mnt_stat.f_namemax = TMPFS_MAXNAMLEN;
202 mp->mnt_fs_bshift = PAGE_SHIFT;
203 mp->mnt_dev_bshift = DEV_BSHIFT;
204 mp->mnt_iflag |= IMNT_MPSAFE;
205 vfs_getnewfsid(mp);
206
207 error = set_statvfs_info(path, UIO_USERSPACE, "tmpfs", UIO_SYSSPACE,
208 mp->mnt_op->vfs_name, mp, curlwp);
209 if (error) {
210 (void)tmpfs_unmount(mp, MNT_FORCE);
211 }
212 return error;
213 }
214
215 int
216 tmpfs_start(struct mount *mp, int flags)
217 {
218
219 return 0;
220 }
221
222 int
223 tmpfs_unmount(struct mount *mp, int mntflags)
224 {
225 tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
226 tmpfs_node_t *node, *cnode;
227 int error, flags = 0;
228
229 /* Handle forced unmounts. */
230 if (mntflags & MNT_FORCE)
231 flags |= FORCECLOSE;
232
233 /* Finalize all pending I/O. */
234 error = vflush(mp, NULL, flags);
235 if (error != 0)
236 return error;
237
238 /*
239 * First round, detach and destroy all directory entries.
240 * Also, clear the pointers to the vnodes - they are gone.
241 */
242 LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
243 tmpfs_dirent_t *de;
244
245 node->tn_vnode = NULL;
246 if (node->tn_type != VDIR) {
247 continue;
248 }
249 while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
250 cnode = de->td_node;
251 if (cnode && cnode != TMPFS_NODE_WHITEOUT) {
252 cnode->tn_vnode = NULL;
253 }
254 tmpfs_dir_detach(node, de);
255 tmpfs_free_dirent(tmp, de);
256 }
257 /* Extra virtual entry (itself for the root). */
258 node->tn_links--;
259 }
260
261 /* Release the reference on root (diagnostic). */
262 node = tmp->tm_root;
263 node->tn_links--;
264
265 /* Second round, destroy all inodes. */
266 while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) {
267 tmpfs_free_node(tmp, node);
268 }
269
270 /* Throw away the tmpfs_mount structure. */
271 tmpfs_mntmem_destroy(tmp);
272 mutex_destroy(&tmp->tm_lock);
273 kmem_free(tmp, sizeof(*tmp));
274 mp->mnt_data = NULL;
275
276 return 0;
277 }
278
279 int
280 tmpfs_root(struct mount *mp, vnode_t **vpp)
281 {
282 tmpfs_node_t *node = VFS_TO_TMPFS(mp)->tm_root;
283 int error;
284
285 error = vcache_get(mp, &node, sizeof(node), vpp);
286 if (error)
287 return error;
288 error = vn_lock(*vpp, LK_EXCLUSIVE);
289 if (error) {
290 vrele(*vpp);
291 *vpp = NULL;
292 return error;
293 }
294
295 return 0;
296 }
297
298 int
299 tmpfs_vget(struct mount *mp, ino_t ino, vnode_t **vpp)
300 {
301
302 return EOPNOTSUPP;
303 }
304
305 int
306 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, vnode_t **vpp)
307 {
308 tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
309 tmpfs_node_t *node;
310 tmpfs_fid_t tfh;
311 int error;
312
313 if (fhp->fid_len != sizeof(tmpfs_fid_t)) {
314 return EINVAL;
315 }
316 memcpy(&tfh, fhp, sizeof(tmpfs_fid_t));
317
318 mutex_enter(&tmp->tm_lock);
319 LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
320 if (node->tn_id == tfh.tf_id) {
321 /* Prevent this node from disappearing. */
322 atomic_inc_32(&node->tn_holdcount);
323 break;
324 }
325 }
326 mutex_exit(&tmp->tm_lock);
327 if (node == NULL)
328 return ESTALE;
329
330 error = vcache_get(mp, &node, sizeof(node), vpp);
331 /* If this node has been reclaimed free it now. */
332 if (atomic_dec_32_nv(&node->tn_holdcount) == TMPFS_NODE_RECLAIMED) {
333 KASSERT(error != 0);
334 tmpfs_free_node(tmp, node);
335 }
336 if (error)
337 return (error == ENOENT ? ESTALE : error);
338 error = vn_lock(*vpp, LK_EXCLUSIVE);
339 if (error) {
340 vrele(*vpp);
341 *vpp = NULL;
342 return error;
343 }
344 if (TMPFS_NODE_GEN(node) != tfh.tf_gen) {
345 vput(*vpp);
346 *vpp = NULL;
347 return ESTALE;
348 }
349
350 return 0;
351 }
352
353 int
354 tmpfs_vptofh(vnode_t *vp, struct fid *fhp, size_t *fh_size)
355 {
356 tmpfs_fid_t tfh;
357 tmpfs_node_t *node;
358
359 if (*fh_size < sizeof(tmpfs_fid_t)) {
360 *fh_size = sizeof(tmpfs_fid_t);
361 return E2BIG;
362 }
363 *fh_size = sizeof(tmpfs_fid_t);
364 node = VP_TO_TMPFS_NODE(vp);
365
366 memset(&tfh, 0, sizeof(tfh));
367 tfh.tf_len = sizeof(tmpfs_fid_t);
368 tfh.tf_gen = TMPFS_NODE_GEN(node);
369 tfh.tf_id = node->tn_id;
370 memcpy(fhp, &tfh, sizeof(tfh));
371
372 return 0;
373 }
374
375 int
376 tmpfs_statvfs(struct mount *mp, struct statvfs *sbp)
377 {
378 tmpfs_mount_t *tmp;
379 fsfilcnt_t freenodes;
380 size_t avail;
381
382 tmp = VFS_TO_TMPFS(mp);
383
384 sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
385
386 mutex_enter(&tmp->tm_acc_lock);
387 avail = tmpfs_pages_avail(tmp);
388 sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
389 sbp->f_bavail = sbp->f_bfree = avail;
390 sbp->f_bresvd = 0;
391
392 freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
393 avail * PAGE_SIZE / sizeof(tmpfs_node_t));
394
395 sbp->f_files = tmp->tm_nodes_cnt + freenodes;
396 sbp->f_favail = sbp->f_ffree = freenodes;
397 sbp->f_fresvd = 0;
398 mutex_exit(&tmp->tm_acc_lock);
399
400 copy_statvfs_info(sbp, mp);
401
402 return 0;
403 }
404
405 int
406 tmpfs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
407 {
408
409 return 0;
410 }
411
412 int
413 tmpfs_snapshot(struct mount *mp, vnode_t *vp, struct timespec *ctime)
414 {
415
416 return EOPNOTSUPP;
417 }
418
419 /*
420 * tmpfs vfs operations.
421 */
422
423 extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
424 extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
425 extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
426
427 const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
428 &tmpfs_fifoop_opv_desc,
429 &tmpfs_specop_opv_desc,
430 &tmpfs_vnodeop_opv_desc,
431 NULL,
432 };
433
434 struct vfsops tmpfs_vfsops = {
435 .vfs_name = MOUNT_TMPFS,
436 .vfs_min_mount_data = sizeof (struct tmpfs_args),
437 .vfs_mount = tmpfs_mount,
438 .vfs_start = tmpfs_start,
439 .vfs_unmount = tmpfs_unmount,
440 .vfs_root = tmpfs_root,
441 .vfs_quotactl = (void *)eopnotsupp,
442 .vfs_statvfs = tmpfs_statvfs,
443 .vfs_sync = tmpfs_sync,
444 .vfs_vget = tmpfs_vget,
445 .vfs_loadvnode = tmpfs_loadvnode,
446 .vfs_newvnode = tmpfs_newvnode,
447 .vfs_fhtovp = tmpfs_fhtovp,
448 .vfs_vptofh = tmpfs_vptofh,
449 .vfs_init = tmpfs_init,
450 .vfs_done = tmpfs_done,
451 .vfs_snapshot = tmpfs_snapshot,
452 .vfs_extattrctl = vfs_stdextattrctl,
453 .vfs_suspendctl = (void *)eopnotsupp,
454 .vfs_renamelock_enter = genfs_renamelock_enter,
455 .vfs_renamelock_exit = genfs_renamelock_exit,
456 .vfs_fsync = (void *)eopnotsupp,
457 .vfs_opv_descs = tmpfs_vnodeopv_descs
458 };
459
460 static int
461 tmpfs_modcmd(modcmd_t cmd, void *arg)
462 {
463
464 switch (cmd) {
465 case MODULE_CMD_INIT:
466 return vfs_attach(&tmpfs_vfsops);
467 case MODULE_CMD_FINI:
468 return vfs_detach(&tmpfs_vfsops);
469 default:
470 return ENOTTY;
471 }
472 }
473