tmpfs_vfsops.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* $OpenBSD: tmpfs_vfsops.c,v 1.7 2015/01/21 22:26:52 deraadt Exp $ */
  2. /* $NetBSD: tmpfs_vfsops.c,v 1.52 2011/09/27 01:10:43 christos Exp $ */
  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. * Efficient memory file system.
  34. *
  35. * tmpfs is a file system that uses NetBSD's virtual memory sub-system
  36. * (the well-known UVM) to store file data and metadata in an efficient
  37. * way. This means that it does not follow the structure of an on-disk
  38. * file system because it simply does not need to. Instead, it uses
  39. * memory-specific data structures and algorithms to automatically
  40. * allocate and release resources.
  41. */
  42. #include <sys/param.h>
  43. #include <sys/types.h>
  44. #include <sys/mount.h>
  45. #include <sys/stat.h>
  46. #include <sys/systm.h>
  47. #include <sys/vnode.h>
  48. #include <sys/malloc.h>
  49. #include <tmpfs/tmpfs.h>
  50. /* MODULE(MODULE_CLASS_VFS, tmpfs, NULL); */
  51. struct pool tmpfs_dirent_pool;
  52. struct pool tmpfs_node_pool;
  53. int tmpfs_mount(struct mount *, const char *, void *, struct nameidata *,
  54. struct proc *);
  55. int tmpfs_start(struct mount *, int, struct proc *);
  56. int tmpfs_unmount(struct mount *, int, struct proc *);
  57. int tmpfs_root(struct mount *, struct vnode **);
  58. int tmpfs_vget(struct mount *, ino_t, struct vnode **);
  59. int tmpfs_fhtovp(struct mount *, struct fid *, struct vnode **);
  60. int tmpfs_vptofh(struct vnode *, struct fid *);
  61. int tmpfs_statfs(struct mount *, struct statfs *, struct proc *);
  62. int tmpfs_sync(struct mount *, int, struct ucred *, struct proc *);
  63. int tmpfs_init(struct vfsconf *);
  64. int
  65. tmpfs_init(struct vfsconf *vfsp)
  66. {
  67. pool_init(&tmpfs_dirent_pool, sizeof(tmpfs_dirent_t), 0, 0, PR_WAITOK,
  68. "tmpfs_dirent", NULL);
  69. pool_init(&tmpfs_node_pool, sizeof(tmpfs_node_t), 0, 0, PR_WAITOK,
  70. "tmpfs_node", NULL);
  71. return 0;
  72. }
  73. int
  74. tmpfs_mount(struct mount *mp, const char *path, void *data,
  75. struct nameidata *ndp, struct proc *p)
  76. {
  77. struct tmpfs_args args;
  78. tmpfs_mount_t *tmp;
  79. tmpfs_node_t *root;
  80. uint64_t memlimit;
  81. uint64_t nodes;
  82. int error;
  83. #if 0
  84. /* Handle retrieval of mount point arguments. */
  85. if (mp->mnt_flag & MNT_GETARGS) {
  86. if (mp->mnt_data == NULL)
  87. return EIO;
  88. tmp = VFS_TO_TMPFS(mp);
  89. args->ta_version = TMPFS_ARGS_VERSION;
  90. args->ta_nodes_max = tmp->tm_nodes_max;
  91. args->ta_size_max = tmp->tm_mem_limit;
  92. root = tmp->tm_root;
  93. args->ta_root_uid = root->tn_uid;
  94. args->ta_root_gid = root->tn_gid;
  95. args->ta_root_mode = root->tn_mode;
  96. *data_len = sizeof(*args);
  97. return 0;
  98. }
  99. #endif
  100. if (mp->mnt_flag & MNT_UPDATE) {
  101. /* TODO */
  102. return EOPNOTSUPP;
  103. }
  104. /* Prohibit mounts if there is not enough memory. */
  105. if (tmpfs_mem_info(1) < TMPFS_PAGES_RESERVED)
  106. return EINVAL;
  107. error = copyin(data, &args, sizeof(struct tmpfs_args));
  108. if (error)
  109. return error;
  110. /* Get the memory usage limit for this file-system. */
  111. if (args.ta_size_max < PAGE_SIZE) {
  112. memlimit = UINT64_MAX;
  113. } else {
  114. memlimit = args.ta_size_max;
  115. }
  116. KASSERT(memlimit > 0);
  117. if (args.ta_nodes_max <= 3) {
  118. nodes = 3 + (memlimit / 1024);
  119. } else {
  120. nodes = args.ta_nodes_max;
  121. }
  122. nodes = MIN(nodes, INT_MAX);
  123. KASSERT(nodes >= 3);
  124. /* Allocate the tmpfs mount structure and fill it. */
  125. tmp = malloc(sizeof(tmpfs_mount_t), M_MISCFSMNT, M_WAITOK);
  126. if (tmp == NULL)
  127. return ENOMEM;
  128. tmp->tm_nodes_max = (ino_t)nodes;
  129. tmp->tm_nodes_cnt = 0;
  130. tmp->tm_highest_inode = 1;
  131. LIST_INIT(&tmp->tm_nodes);
  132. rw_init(&tmp->tm_lock, "tmplk");
  133. tmpfs_mntmem_init(tmp, memlimit);
  134. /* Allocate the root node. */
  135. error = tmpfs_alloc_node(tmp, VDIR, args.ta_root_uid,
  136. args.ta_root_gid, args.ta_root_mode & ALLPERMS, NULL,
  137. VNOVAL, &root);
  138. KASSERT(error == 0 && root != NULL);
  139. /*
  140. * Parent of the root inode is itself. Also, root inode has no
  141. * directory entry (i.e. is never attached), thus hold an extra
  142. * reference (link) for it.
  143. */
  144. root->tn_links++;
  145. root->tn_spec.tn_dir.tn_parent = root;
  146. tmp->tm_root = root;
  147. mp->mnt_data = tmp;
  148. mp->mnt_flag |= MNT_LOCAL;
  149. mp->mnt_stat.f_namemax = TMPFS_MAXNAMLEN;
  150. #if 0
  151. mp->mnt_fs_bshift = PAGE_SHIFT;
  152. mp->mnt_dev_bshift = DEV_BSHIFT;
  153. mp->mnt_iflag |= IMNT_MPSAFE;
  154. #endif
  155. vfs_getnewfsid(mp);
  156. mp->mnt_stat.mount_info.tmpfs_args = args;
  157. bzero(&mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
  158. bzero(&mp->mnt_stat.f_mntfromname, sizeof(mp->mnt_stat.f_mntfromname));
  159. bzero(&mp->mnt_stat.f_mntfromspec, sizeof(mp->mnt_stat.f_mntfromspec));
  160. strlcpy(mp->mnt_stat.f_mntonname, path,
  161. sizeof(mp->mnt_stat.f_mntonname) - 1);
  162. strlcpy(mp->mnt_stat.f_mntfromname, "tmpfs",
  163. sizeof(mp->mnt_stat.f_mntfromname) - 1);
  164. strlcpy(mp->mnt_stat.f_mntfromspec, "tmpfs",
  165. sizeof(mp->mnt_stat.f_mntfromspec) - 1);
  166. return error;
  167. }
  168. int
  169. tmpfs_start(struct mount *mp, int flags, struct proc *p)
  170. {
  171. return 0;
  172. }
  173. int
  174. tmpfs_unmount(struct mount *mp, int mntflags, struct proc *p)
  175. {
  176. tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
  177. tmpfs_node_t *node, *cnode;
  178. int error, flags = 0;
  179. /* Handle forced unmounts. */
  180. if (mntflags & MNT_FORCE)
  181. flags |= FORCECLOSE;
  182. /* Finalize all pending I/O. */
  183. error = vflush(mp, NULL, flags);
  184. if (error != 0)
  185. return error;
  186. /*
  187. * First round, detach and destroy all directory entries.
  188. * Also, clear the pointers to the vnodes - they are gone.
  189. */
  190. LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
  191. tmpfs_dirent_t *de;
  192. node->tn_vnode = NULL;
  193. if (node->tn_type != VDIR) {
  194. continue;
  195. }
  196. while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) {
  197. cnode = de->td_node;
  198. if (cnode) {
  199. cnode->tn_vnode = NULL;
  200. }
  201. tmpfs_dir_detach(node, de);
  202. tmpfs_free_dirent(tmp, de);
  203. }
  204. }
  205. /* Second round, destroy all inodes. */
  206. while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) {
  207. tmpfs_free_node(tmp, node);
  208. }
  209. /* Throw away the tmpfs_mount structure. */
  210. tmpfs_mntmem_destroy(tmp);
  211. /* mutex_destroy(&tmp->tm_lock); */
  212. /* kmem_free(tmp, sizeof(*tmp)); */
  213. free(tmp, M_MISCFSMNT, sizeof(tmpfs_mount_t));
  214. mp->mnt_data = NULL;
  215. return 0;
  216. }
  217. int
  218. tmpfs_root(struct mount *mp, struct vnode **vpp)
  219. {
  220. tmpfs_node_t *node = VFS_TO_TMPFS(mp)->tm_root;
  221. rw_enter_write(&node->tn_nlock);
  222. return tmpfs_vnode_get(mp, node, vpp);
  223. }
  224. int
  225. tmpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
  226. {
  227. printf("tmpfs_vget called; need for it unknown yet\n");
  228. return EOPNOTSUPP;
  229. }
  230. int
  231. tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
  232. {
  233. tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
  234. tmpfs_node_t *node;
  235. tmpfs_fid_t tfh;
  236. if (fhp->fid_len != sizeof(tmpfs_fid_t)) {
  237. return EINVAL;
  238. }
  239. memcpy(&tfh, fhp, sizeof(tmpfs_fid_t));
  240. rw_enter_write(&tmp->tm_lock);
  241. LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) {
  242. if (node->tn_id != tfh.tf_id) {
  243. continue;
  244. }
  245. if (TMPFS_NODE_GEN(node) != tfh.tf_gen) {
  246. continue;
  247. }
  248. rw_enter_write(&node->tn_nlock);
  249. break;
  250. }
  251. rw_exit_write(&tmp->tm_lock);
  252. /* Will release the tn_nlock. */
  253. return node ? tmpfs_vnode_get(mp, node, vpp) : ESTALE;
  254. }
  255. int
  256. tmpfs_vptofh(struct vnode *vp, struct fid *fhp)
  257. {
  258. tmpfs_fid_t tfh;
  259. tmpfs_node_t *node;
  260. node = VP_TO_TMPFS_NODE(vp);
  261. memset(&tfh, 0, sizeof(tfh));
  262. tfh.tf_len = sizeof(tmpfs_fid_t);
  263. tfh.tf_gen = TMPFS_NODE_GEN(node);
  264. tfh.tf_id = node->tn_id;
  265. memcpy(fhp, &tfh, sizeof(tfh));
  266. return 0;
  267. }
  268. int
  269. tmpfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
  270. {
  271. tmpfs_mount_t *tmp;
  272. fsfilcnt_t freenodes;
  273. uint64_t avail;
  274. tmp = VFS_TO_TMPFS(mp);
  275. sbp->f_iosize = sbp->f_bsize = PAGE_SIZE;
  276. rw_enter_write(&tmp->tm_acc_lock);
  277. avail = tmpfs_pages_avail(tmp);
  278. sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT);
  279. sbp->f_bfree = avail;
  280. sbp->f_bavail = avail & INT64_MAX; /* f_bavail is int64_t */
  281. freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt,
  282. avail * PAGE_SIZE / sizeof(tmpfs_node_t));
  283. sbp->f_files = tmp->tm_nodes_cnt + freenodes;
  284. sbp->f_ffree = freenodes;
  285. sbp->f_favail = freenodes & INT64_MAX; /* f_favail is int64_t */
  286. rw_exit_write(&tmp->tm_acc_lock);
  287. copy_statfs_info(sbp, mp);
  288. return 0;
  289. }
  290. int
  291. tmpfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
  292. {
  293. return 0;
  294. }
  295. /*
  296. * tmpfs vfs operations.
  297. */
  298. struct vfsops tmpfs_vfsops = {
  299. tmpfs_mount, /* vfs_mount */
  300. tmpfs_start, /* vfs_start */
  301. tmpfs_unmount, /* vfs_unmount */
  302. tmpfs_root, /* vfs_root */
  303. (void *)eopnotsupp, /* vfs_quotactl */
  304. tmpfs_statfs, /* vfs_statfs */
  305. tmpfs_sync, /* vfs_sync */
  306. tmpfs_vget, /* vfs_vget */
  307. tmpfs_fhtovp, /* vfs_fhtovp */
  308. tmpfs_vptofh, /* vfs_vptofh */
  309. tmpfs_init, /* vfs_init */
  310. NULL, /* vfs_sysctl */
  311. (void *)eopnotsupp,
  312. };