Annotation of src/sys/fs/tmpfs/tmpfs_vfsops.c, Revision 1.57
1.57 ! hannken 1: /* $NetBSD: tmpfs_vfsops.c,v 1.56 2014/01/04 12:36:49 hannken Exp $ */
1.1 jmmv 2:
3: /*
1.34 ad 4: * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
1.1 jmmv 5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
1.6 jmmv 8: * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9: * 2005 program.
1.1 jmmv 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: /*
1.2 jmmv 34: * Efficient memory file system.
1.6 jmmv 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.
1.1 jmmv 42: */
43:
44: #include <sys/cdefs.h>
1.57 ! hannken 45: __KERNEL_RCSID(0, "$NetBSD: tmpfs_vfsops.c,v 1.56 2014/01/04 12:36:49 hannken Exp $");
1.1 jmmv 46:
47: #include <sys/param.h>
48: #include <sys/types.h>
1.34 ad 49: #include <sys/kmem.h>
1.1 jmmv 50: #include <sys/mount.h>
1.8 jmmv 51: #include <sys/stat.h>
1.1 jmmv 52: #include <sys/systm.h>
53: #include <sys/vnode.h>
1.41 rumble 54: #include <sys/module.h>
1.1 jmmv 55:
1.36 dholland 56: #include <miscfs/genfs/genfs.h>
1.1 jmmv 57: #include <fs/tmpfs/tmpfs.h>
1.44 pooka 58: #include <fs/tmpfs/tmpfs_args.h>
1.1 jmmv 59:
1.41 rumble 60: MODULE(MODULE_CLASS_VFS, tmpfs, NULL);
61:
1.48 rmind 62: struct pool tmpfs_dirent_pool;
63: struct pool tmpfs_node_pool;
1.1 jmmv 64:
1.32 pooka 65: static int tmpfs_mount(struct mount *, const char *, void *, size_t *);
66: static int tmpfs_start(struct mount *, int);
67: static int tmpfs_unmount(struct mount *, int);
1.50 rmind 68: static int tmpfs_root(struct mount *, vnode_t **);
69: static int tmpfs_vget(struct mount *, ino_t, vnode_t **);
70: static int tmpfs_fhtovp(struct mount *, struct fid *, vnode_t **);
1.13 martin 71: static int tmpfs_vptofh(struct vnode *, struct fid *, size_t *);
1.32 pooka 72: static int tmpfs_statvfs(struct mount *, struct statvfs *);
73: static int tmpfs_sync(struct mount *, int, kauth_cred_t);
1.1 jmmv 74: static void tmpfs_init(void);
75: static void tmpfs_done(void);
1.50 rmind 76: static int tmpfs_snapshot(struct mount *, vnode_t *, struct timespec *);
1.1 jmmv 77:
1.48 rmind 78: static void
79: tmpfs_init(void)
80: {
81:
1.50 rmind 82: pool_init(&tmpfs_dirent_pool, sizeof(tmpfs_dirent_t), 0, 0, 0,
1.48 rmind 83: "tmpfs_dirent", &pool_allocator_nointr, IPL_NONE);
1.50 rmind 84: pool_init(&tmpfs_node_pool, sizeof(tmpfs_node_t), 0, 0, 0,
1.48 rmind 85: "tmpfs_node", &pool_allocator_nointr, IPL_NONE);
86: }
87:
88: static void
89: tmpfs_done(void)
90: {
91:
92: pool_destroy(&tmpfs_dirent_pool);
93: pool_destroy(&tmpfs_node_pool);
94: }
1.1 jmmv 95:
96: static int
1.32 pooka 97: tmpfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
1.1 jmmv 98: {
1.23 dsl 99: struct tmpfs_args *args = data;
1.50 rmind 100: tmpfs_mount_t *tmp;
101: tmpfs_node_t *root;
1.45 rmind 102: uint64_t memlimit;
103: ino_t nodes;
104: int error;
1.23 dsl 105:
1.49 rmind 106: /* Validate the version. */
107: if (*data_len < sizeof(*args) ||
108: args->ta_version != TMPFS_ARGS_VERSION)
1.23 dsl 109: return EINVAL;
1.1 jmmv 110:
111: /* Handle retrieval of mount point arguments. */
112: if (mp->mnt_flag & MNT_GETARGS) {
113: if (mp->mnt_data == NULL)
114: return EIO;
115: tmp = VFS_TO_TMPFS(mp);
116:
1.23 dsl 117: args->ta_version = TMPFS_ARGS_VERSION;
118: args->ta_nodes_max = tmp->tm_nodes_max;
1.45 rmind 119: args->ta_size_max = tmp->tm_mem_limit;
1.31 ad 120:
1.30 ad 121: root = tmp->tm_root;
1.23 dsl 122: args->ta_root_uid = root->tn_uid;
123: args->ta_root_gid = root->tn_gid;
124: args->ta_root_mode = root->tn_mode;
1.1 jmmv 125:
1.45 rmind 126: *data_len = sizeof(*args);
1.23 dsl 127: return 0;
1.1 jmmv 128: }
129:
130: if (mp->mnt_flag & MNT_UPDATE) {
1.49 rmind 131: /* TODO */
1.1 jmmv 132: return EOPNOTSUPP;
133: }
134:
1.49 rmind 135: /* Prohibit mounts if there is not enough memory. */
1.20 thorpej 136: if (tmpfs_mem_info(true) < TMPFS_PAGES_RESERVED)
1.1 jmmv 137: return EINVAL;
138:
1.45 rmind 139: /* Get the memory usage limit for this file-system. */
140: if (args->ta_size_max < PAGE_SIZE) {
141: memlimit = UINT64_MAX;
142: } else {
143: memlimit = args->ta_size_max;
144: }
145: KASSERT(memlimit > 0);
146:
147: if (args->ta_nodes_max <= 3) {
148: nodes = 3 + (memlimit / 1024);
149: } else {
1.23 dsl 150: nodes = args->ta_nodes_max;
1.45 rmind 151: }
152: nodes = MIN(nodes, INT_MAX);
1.7 jmmv 153: KASSERT(nodes >= 3);
1.1 jmmv 154:
155: /* Allocate the tmpfs mount structure and fill it. */
1.50 rmind 156: tmp = kmem_zalloc(sizeof(tmpfs_mount_t), KM_SLEEP);
1.34 ad 157: if (tmp == NULL)
158: return ENOMEM;
1.1 jmmv 159:
160: tmp->tm_nodes_max = nodes;
1.34 ad 161: tmp->tm_nodes_cnt = 0;
162: LIST_INIT(&tmp->tm_nodes);
163:
164: mutex_init(&tmp->tm_lock, MUTEX_DEFAULT, IPL_NONE);
1.45 rmind 165: tmpfs_mntmem_init(tmp, memlimit);
1.1 jmmv 166:
167: /* Allocate the root node. */
1.23 dsl 168: error = tmpfs_alloc_node(tmp, VDIR, args->ta_root_uid,
1.51 rmind 169: args->ta_root_gid, args->ta_root_mode & ALLPERMS, NULL,
1.33 pooka 170: VNOVAL, &root);
1.1 jmmv 171: KASSERT(error == 0 && root != NULL);
1.51 rmind 172:
173: /*
174: * Parent of the root inode is itself. Also, root inode has no
175: * directory entry (i.e. is never attached), thus hold an extra
176: * reference (link) for it.
177: */
1.34 ad 178: root->tn_links++;
1.51 rmind 179: root->tn_spec.tn_dir.tn_parent = root;
1.1 jmmv 180: tmp->tm_root = root;
181:
182: mp->mnt_data = tmp;
183: mp->mnt_flag |= MNT_LOCAL;
1.52 christos 184: mp->mnt_stat.f_namemax = TMPFS_MAXNAMLEN;
1.27 pooka 185: mp->mnt_fs_bshift = PAGE_SHIFT;
186: mp->mnt_dev_bshift = DEV_BSHIFT;
1.34 ad 187: mp->mnt_iflag |= IMNT_MPSAFE;
1.1 jmmv 188: vfs_getnewfsid(mp);
189:
1.48 rmind 190: error = set_statvfs_info(path, UIO_USERSPACE, "tmpfs", UIO_SYSSPACE,
1.45 rmind 191: mp->mnt_op->vfs_name, mp, curlwp);
1.48 rmind 192: if (error) {
193: (void)tmpfs_unmount(mp, MNT_FORCE);
194: }
195: return error;
1.1 jmmv 196: }
197:
198: static int
1.32 pooka 199: tmpfs_start(struct mount *mp, int flags)
1.1 jmmv 200: {
201:
202: return 0;
203: }
204:
205: static int
1.32 pooka 206: tmpfs_unmount(struct mount *mp, int mntflags)
1.1 jmmv 207: {
1.53 rmind 208: tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
209: tmpfs_node_t *node, *cnode;
1.48 rmind 210: int error, flags = 0;
1.1 jmmv 211:
212: /* Handle forced unmounts. */
213: if (mntflags & MNT_FORCE)
214: flags |= FORCECLOSE;
215:
216: /* Finalize all pending I/O. */
217: error = vflush(mp, NULL, flags);
218: if (error != 0)
219: return error;
220:
1.53 rmind 221: /*
222: * First round, detach and destroy all directory entries.
223: * Also, clear the pointers to the vnodes - they are gone.
224: */
225: LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
226: tmpfs_dirent_t *de;
1.1 jmmv 227:
1.53 rmind 228: node->tn_vnode = NULL;
229: if (node->tn_type != VDIR) {
230: continue;
231: }
232: while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
1.54 rmind 233: cnode = de->td_node;
234: if (cnode && cnode != TMPFS_NODE_WHITEOUT) {
1.53 rmind 235: cnode->tn_vnode = NULL;
1.1 jmmv 236: }
1.53 rmind 237: tmpfs_dir_detach(node, de);
238: tmpfs_free_dirent(tmp, de);
1.1 jmmv 239: }
1.55 rmind 240: /* Extra virtual entry (itself for the root). */
241: node->tn_links--;
1.53 rmind 242: }
243:
1.55 rmind 244: /* Release the reference on root (diagnostic). */
245: node = tmp->tm_root;
246: node->tn_links--;
247:
1.53 rmind 248: /* Second round, destroy all inodes. */
249: while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) {
1.1 jmmv 250: tmpfs_free_node(tmp, node);
1.31 ad 251: }
1.1 jmmv 252:
253: /* Throw away the tmpfs_mount structure. */
1.45 rmind 254: tmpfs_mntmem_destroy(tmp);
1.34 ad 255: mutex_destroy(&tmp->tm_lock);
256: kmem_free(tmp, sizeof(*tmp));
1.1 jmmv 257: mp->mnt_data = NULL;
258:
259: return 0;
260: }
261:
262: static int
1.50 rmind 263: tmpfs_root(struct mount *mp, vnode_t **vpp)
1.1 jmmv 264: {
1.51 rmind 265: tmpfs_node_t *node = VFS_TO_TMPFS(mp)->tm_root;
1.1 jmmv 266:
1.51 rmind 267: mutex_enter(&node->tn_vlock);
268: return tmpfs_vnode_get(mp, node, vpp);
1.1 jmmv 269: }
270:
271: static int
1.50 rmind 272: tmpfs_vget(struct mount *mp, ino_t ino, vnode_t **vpp)
1.1 jmmv 273: {
274:
275: return EOPNOTSUPP;
276: }
277:
278: static int
1.50 rmind 279: tmpfs_fhtovp(struct mount *mp, struct fid *fhp, vnode_t **vpp)
1.1 jmmv 280: {
1.51 rmind 281: tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
1.50 rmind 282: tmpfs_node_t *node;
283: tmpfs_fid_t tfh;
1.56 hannken 284: int error;
1.1 jmmv 285:
1.51 rmind 286: if (fhp->fid_len != sizeof(tmpfs_fid_t)) {
1.1 jmmv 287: return EINVAL;
1.51 rmind 288: }
1.50 rmind 289: memcpy(&tfh, fhp, sizeof(tmpfs_fid_t));
1.13 martin 290:
1.34 ad 291: mutex_enter(&tmp->tm_lock);
292: LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
1.56 hannken 293: if (node->tn_id == tfh.tf_id) {
294: mutex_enter(&node->tn_vlock);
295: break;
1.1 jmmv 296: }
297: }
1.34 ad 298: mutex_exit(&tmp->tm_lock);
1.1 jmmv 299:
1.56 hannken 300: if (node == NULL)
301: return ESTALE;
1.51 rmind 302: /* Will release the tn_vlock. */
1.56 hannken 303: if ((error = tmpfs_vnode_get(mp, node, vpp)) != 0)
304: return error;
305: if (TMPFS_NODE_GEN(node) != tfh.tf_gen) {
306: vput(*vpp);
307: *vpp = NULL;
308: return ESTALE;
309: }
310:
311: return 0;
1.1 jmmv 312: }
313:
314: static int
1.50 rmind 315: tmpfs_vptofh(vnode_t *vp, struct fid *fhp, size_t *fh_size)
1.1 jmmv 316: {
1.50 rmind 317: tmpfs_fid_t tfh;
318: tmpfs_node_t *node;
1.1 jmmv 319:
1.50 rmind 320: if (*fh_size < sizeof(tmpfs_fid_t)) {
321: *fh_size = sizeof(tmpfs_fid_t);
1.13 martin 322: return E2BIG;
323: }
1.50 rmind 324: *fh_size = sizeof(tmpfs_fid_t);
1.1 jmmv 325: node = VP_TO_TMPFS_NODE(vp);
326:
1.13 martin 327: memset(&tfh, 0, sizeof(tfh));
1.50 rmind 328: tfh.tf_len = sizeof(tmpfs_fid_t);
1.51 rmind 329: tfh.tf_gen = TMPFS_NODE_GEN(node);
1.13 martin 330: tfh.tf_id = node->tn_id;
331: memcpy(fhp, &tfh, sizeof(tfh));
1.1 jmmv 332:
333: return 0;
334: }
335:
336: static int
1.32 pooka 337: tmpfs_statvfs(struct mount *mp, struct statvfs *sbp)
1.1 jmmv 338: {
1.50 rmind 339: tmpfs_mount_t *tmp;
1.34 ad 340: fsfilcnt_t freenodes;
1.46 rmind 341: size_t avail;
1.1 jmmv 342:
343: tmp = VFS_TO_TMPFS(mp);
344:
345: sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
346:
1.46 rmind 347: mutex_enter(&tmp->tm_acc_lock);
348: avail = tmpfs_pages_avail(tmp);
1.45 rmind 349: sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
1.46 rmind 350: sbp->f_bavail = sbp->f_bfree = avail;
1.1 jmmv 351: sbp->f_bresvd = 0;
352:
1.34 ad 353: freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
1.50 rmind 354: avail * PAGE_SIZE / sizeof(tmpfs_node_t));
1.31 ad 355:
1.34 ad 356: sbp->f_files = tmp->tm_nodes_cnt + freenodes;
1.1 jmmv 357: sbp->f_favail = sbp->f_ffree = freenodes;
358: sbp->f_fresvd = 0;
1.46 rmind 359: mutex_exit(&tmp->tm_acc_lock);
1.1 jmmv 360:
361: copy_statvfs_info(sbp, mp);
362:
363: return 0;
364: }
365:
366: static int
1.50 rmind 367: tmpfs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
1.1 jmmv 368: {
369:
370: return 0;
371: }
372:
373: static int
1.50 rmind 374: tmpfs_snapshot(struct mount *mp, vnode_t *vp, struct timespec *ctime)
1.1 jmmv 375: {
376:
377: return EOPNOTSUPP;
378: }
379:
380: /*
381: * tmpfs vfs operations.
382: */
383:
384: extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
385: extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
386: extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
387:
388: const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
389: &tmpfs_fifoop_opv_desc,
390: &tmpfs_specop_opv_desc,
391: &tmpfs_vnodeop_opv_desc,
392: NULL,
393: };
394:
395: struct vfsops tmpfs_vfsops = {
396: MOUNT_TMPFS, /* vfs_name */
1.23 dsl 397: sizeof (struct tmpfs_args),
1.1 jmmv 398: tmpfs_mount, /* vfs_mount */
399: tmpfs_start, /* vfs_start */
400: tmpfs_unmount, /* vfs_unmount */
401: tmpfs_root, /* vfs_root */
1.32 pooka 402: (void *)eopnotsupp, /* vfs_quotactl */
1.1 jmmv 403: tmpfs_statvfs, /* vfs_statvfs */
404: tmpfs_sync, /* vfs_sync */
405: tmpfs_vget, /* vfs_vget */
406: tmpfs_fhtovp, /* vfs_fhtovp */
407: tmpfs_vptofh, /* vfs_vptofh */
408: tmpfs_init, /* vfs_init */
409: NULL, /* vfs_reinit */
410: tmpfs_done, /* vfs_done */
411: NULL, /* vfs_mountroot */
412: tmpfs_snapshot, /* vfs_snapshot */
413: vfs_stdextattrctl, /* vfs_extattrctl */
1.25 pooka 414: (void *)eopnotsupp, /* vfs_suspendctl */
1.36 dholland 415: genfs_renamelock_enter,
416: genfs_renamelock_exit,
1.40 ad 417: (void *)eopnotsupp,
1.1 jmmv 418: tmpfs_vnodeopv_descs,
1.14 christos 419: 0, /* vfs_refcount */
420: { NULL, NULL },
1.1 jmmv 421: };
1.41 rumble 422:
423: static int
424: tmpfs_modcmd(modcmd_t cmd, void *arg)
425: {
426:
427: switch (cmd) {
428: case MODULE_CMD_INIT:
429: return vfs_attach(&tmpfs_vfsops);
430: case MODULE_CMD_FINI:
431: return vfs_detach(&tmpfs_vfsops);
432: default:
433: return ENOTTY;
434: }
435: }
CVSweb <webmaster@jp.NetBSD.org>