1 /* $OpenBSD: tmpfs_vfsops.c,v 1.16 2018/04/06 15:14:27 patrick Exp $ */ 2 /* $NetBSD: tmpfs_vfsops.c,v 1.52 2011/09/27 01:10:43 christos Exp $ */ 3 4 /* 5 * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code 10 * 2005 program. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Efficient memory file system. 36 * 37 * tmpfs is a file system that uses NetBSD's virtual memory sub-system 38 * (the well-known UVM) to store file data and metadata in an efficient 39 * way. This means that it does not follow the structure of an on-disk 40 * file system because it simply does not need to. Instead, it uses 41 * memory-specific data structures and algorithms to automatically 42 * allocate and release resources. 43 */ 44 45 #include <sys/param.h> 46 #include <sys/mount.h> 47 #include <sys/stat.h> 48 #include <sys/systm.h> 49 #include <sys/vnode.h> 50 #include <sys/malloc.h> 51 52 #include <tmpfs/tmpfs.h> 53 54 /* MODULE(MODULE_CLASS_VFS, tmpfs, NULL); */ 55 56 struct pool tmpfs_dirent_pool; 57 struct pool tmpfs_node_pool; 58 59 int tmpfs_mount(struct mount *, const char *, void *, struct nameidata *, 60 struct proc *); 61 int tmpfs_start(struct mount *, int, struct proc *); 62 int tmpfs_unmount(struct mount *, int, struct proc *); 63 int tmpfs_root(struct mount *, struct vnode **); 64 int tmpfs_vget(struct mount *, ino_t, struct vnode **); 65 int tmpfs_fhtovp(struct mount *, struct fid *, struct vnode **); 66 int tmpfs_vptofh(struct vnode *, struct fid *); 67 int tmpfs_statfs(struct mount *, struct statfs *, struct proc *); 68 int tmpfs_sync(struct mount *, int, int, struct ucred *, struct proc *); 69 int tmpfs_init(struct vfsconf *); 70 71 int 72 tmpfs_init(struct vfsconf *vfsp) 73 { 74 75 pool_init(&tmpfs_dirent_pool, sizeof(tmpfs_dirent_t), 0, IPL_NONE, 76 PR_WAITOK, "tmpfs_dirent", NULL); 77 pool_init(&tmpfs_node_pool, sizeof(tmpfs_node_t), 0, IPL_NONE, 78 PR_WAITOK, "tmpfs_node", NULL); 79 80 return 0; 81 } 82 83 int 84 tmpfs_mount(struct mount *mp, const char *path, void *data, 85 struct nameidata *ndp, struct proc *p) 86 { 87 struct tmpfs_args *args = data; 88 tmpfs_mount_t *tmp; 89 tmpfs_node_t *root; 90 uint64_t memlimit; 91 uint64_t nodes; 92 int error; 93 94 #if 0 95 /* Handle retrieval of mount point arguments. */ 96 if (mp->mnt_flag & MNT_GETARGS) { 97 if (mp->mnt_data == NULL) 98 return EIO; 99 tmp = VFS_TO_TMPFS(mp); 100 101 args->ta_version = TMPFS_ARGS_VERSION; 102 args->ta_nodes_max = tmp->tm_nodes_max; 103 args->ta_size_max = tmp->tm_mem_limit; 104 105 root = tmp->tm_root; 106 args->ta_root_uid = root->tn_uid; 107 args->ta_root_gid = root->tn_gid; 108 args->ta_root_mode = root->tn_mode; 109 110 *data_len = sizeof(*args); 111 return 0; 112 } 113 #endif 114 115 if (mp->mnt_flag & MNT_UPDATE) { 116 /* TODO */ 117 return EOPNOTSUPP; 118 } 119 120 /* Prohibit mounts if there is not enough memory. */ 121 if (tmpfs_mem_info(1) < TMPFS_PAGES_RESERVED) 122 return EINVAL; 123 124 if (args->ta_root_uid == VNOVAL || args->ta_root_gid == VNOVAL || 125 args->ta_root_mode == VNOVAL) 126 return EINVAL; 127 128 /* Get the memory usage limit for this file-system. */ 129 if (args->ta_size_max < PAGE_SIZE) { 130 memlimit = UINT64_MAX; 131 } else { 132 memlimit = args->ta_size_max; 133 } 134 KASSERT(memlimit > 0); 135 136 if (args->ta_nodes_max <= 3) { 137 nodes = 3 + (memlimit / 1024); 138 } else { 139 nodes = args->ta_nodes_max; 140 } 141 nodes = MIN(nodes, INT_MAX); 142 KASSERT(nodes >= 3); 143 144 /* Allocate the tmpfs mount structure and fill it. */ 145 tmp = malloc(sizeof(tmpfs_mount_t), M_MISCFSMNT, M_WAITOK); 146 147 tmp->tm_nodes_max = (ino_t)nodes; 148 tmp->tm_nodes_cnt = 0; 149 tmp->tm_highest_inode = 1; 150 LIST_INIT(&tmp->tm_nodes); 151 152 rw_init(&tmp->tm_lock, "tmplk"); 153 tmpfs_mntmem_init(tmp, memlimit); 154 155 /* Allocate the root node. */ 156 error = tmpfs_alloc_node(tmp, VDIR, args->ta_root_uid, 157 args->ta_root_gid, args->ta_root_mode & ALLPERMS, NULL, 158 VNOVAL, &root); 159 KASSERT(error == 0 && root != NULL); 160 161 /* 162 * Parent of the root inode is itself. Also, root inode has no 163 * directory entry (i.e. is never attached), thus hold an extra 164 * reference (link) for it. 165 */ 166 root->tn_links++; 167 root->tn_spec.tn_dir.tn_parent = root; 168 tmp->tm_root = root; 169 170 mp->mnt_data = tmp; 171 mp->mnt_flag |= MNT_LOCAL; 172 mp->mnt_stat.f_namemax = TMPFS_MAXNAMLEN; 173 #if 0 174 mp->mnt_fs_bshift = PAGE_SHIFT; 175 mp->mnt_dev_bshift = DEV_BSHIFT; 176 mp->mnt_iflag |= IMNT_MPSAFE; 177 #endif 178 vfs_getnewfsid(mp); 179 180 mp->mnt_stat.mount_info.tmpfs_args = *args; 181 182 bzero(&mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname)); 183 bzero(&mp->mnt_stat.f_mntfromname, sizeof(mp->mnt_stat.f_mntfromname)); 184 bzero(&mp->mnt_stat.f_mntfromspec, sizeof(mp->mnt_stat.f_mntfromspec)); 185 186 strlcpy(mp->mnt_stat.f_mntonname, path, 187 sizeof(mp->mnt_stat.f_mntonname) - 1); 188 strlcpy(mp->mnt_stat.f_mntfromname, "tmpfs", 189 sizeof(mp->mnt_stat.f_mntfromname) - 1); 190 strlcpy(mp->mnt_stat.f_mntfromspec, "tmpfs", 191 sizeof(mp->mnt_stat.f_mntfromspec) - 1); 192 193 return error; 194 } 195 196 int 197 tmpfs_start(struct mount *mp, int flags, struct proc *p) 198 { 199 200 return 0; 201 } 202 203 int 204 tmpfs_unmount(struct mount *mp, int mntflags, struct proc *p) 205 { 206 tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp); 207 tmpfs_node_t *node, *cnode; 208 int error, flags = 0; 209 210 /* Handle forced unmounts. */ 211 if (mntflags & MNT_FORCE) 212 flags |= FORCECLOSE; 213 214 /* Finalize all pending I/O. */ 215 error = vflush(mp, NULL, flags); 216 if (error != 0) 217 return error; 218 219 220 /* 221 * First round, detach and destroy all directory entries. 222 * Also, clear the pointers to the vnodes - they are gone. 223 */ 224 LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) { 225 tmpfs_dirent_t *de; 226 227 node->tn_vnode = NULL; 228 if (node->tn_type != VDIR) { 229 continue; 230 } 231 while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) { 232 cnode = de->td_node; 233 if (cnode) { 234 cnode->tn_vnode = NULL; 235 } 236 tmpfs_dir_detach(node, de); 237 tmpfs_free_dirent(tmp, de); 238 } 239 } 240 241 /* Second round, destroy all inodes. */ 242 while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) { 243 tmpfs_free_node(tmp, node); 244 } 245 246 /* Throw away the tmpfs_mount structure. */ 247 tmpfs_mntmem_destroy(tmp); 248 /* mutex_destroy(&tmp->tm_lock); */ 249 /* kmem_free(tmp, sizeof(*tmp)); */ 250 free(tmp, M_MISCFSMNT, sizeof(tmpfs_mount_t)); 251 mp->mnt_data = NULL; 252 253 return 0; 254 } 255 256 int 257 tmpfs_root(struct mount *mp, struct vnode **vpp) 258 { 259 tmpfs_node_t *node = VFS_TO_TMPFS(mp)->tm_root; 260 261 rw_enter_write(&node->tn_nlock); 262 return tmpfs_vnode_get(mp, node, vpp); 263 } 264 265 int 266 tmpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 267 { 268 269 printf("tmpfs_vget called; need for it unknown yet\n"); 270 return EOPNOTSUPP; 271 } 272 273 int 274 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 275 { 276 tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp); 277 tmpfs_node_t *node; 278 tmpfs_fid_t tfh; 279 280 if (fhp->fid_len != sizeof(tmpfs_fid_t)) { 281 return EINVAL; 282 } 283 memcpy(&tfh, fhp, sizeof(tmpfs_fid_t)); 284 285 rw_enter_write(&tmp->tm_lock); 286 LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) { 287 if (node->tn_id != tfh.tf_id) { 288 continue; 289 } 290 if (TMPFS_NODE_GEN(node) != tfh.tf_gen) { 291 continue; 292 } 293 rw_enter_write(&node->tn_nlock); 294 break; 295 } 296 rw_exit_write(&tmp->tm_lock); 297 298 /* Will release the tn_nlock. */ 299 return node ? tmpfs_vnode_get(mp, node, vpp) : ESTALE; 300 } 301 302 int 303 tmpfs_vptofh(struct vnode *vp, struct fid *fhp) 304 { 305 tmpfs_fid_t tfh; 306 tmpfs_node_t *node; 307 308 node = VP_TO_TMPFS_NODE(vp); 309 310 memset(&tfh, 0, sizeof(tfh)); 311 tfh.tf_len = sizeof(tmpfs_fid_t); 312 tfh.tf_gen = TMPFS_NODE_GEN(node); 313 tfh.tf_id = node->tn_id; 314 memcpy(fhp, &tfh, sizeof(tfh)); 315 316 return 0; 317 } 318 319 int 320 tmpfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p) 321 { 322 tmpfs_mount_t *tmp; 323 fsfilcnt_t freenodes; 324 uint64_t avail; 325 326 tmp = VFS_TO_TMPFS(mp); 327 328 sbp->f_iosize = sbp->f_bsize = PAGE_SIZE; 329 330 rw_enter_write(&tmp->tm_acc_lock); 331 avail = tmpfs_pages_avail(tmp); 332 sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT); 333 sbp->f_bfree = avail; 334 sbp->f_bavail = avail & INT64_MAX; /* f_bavail is int64_t */ 335 336 freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt, 337 avail * PAGE_SIZE / sizeof(tmpfs_node_t)); 338 339 sbp->f_files = tmp->tm_nodes_cnt + freenodes; 340 sbp->f_ffree = freenodes; 341 sbp->f_favail = freenodes & INT64_MAX; /* f_favail is int64_t */ 342 rw_exit_write(&tmp->tm_acc_lock); 343 344 copy_statfs_info(sbp, mp); 345 346 return 0; 347 } 348 349 int 350 tmpfs_sync(struct mount *mp, int waitfor, int stall, struct ucred *cred, 351 struct proc *p) 352 { 353 354 return 0; 355 } 356 357 /* 358 * tmpfs vfs operations. 359 */ 360 361 struct vfsops tmpfs_vfsops = { 362 tmpfs_mount, /* vfs_mount */ 363 tmpfs_start, /* vfs_start */ 364 tmpfs_unmount, /* vfs_unmount */ 365 tmpfs_root, /* vfs_root */ 366 (void *)eopnotsupp, /* vfs_quotactl */ 367 tmpfs_statfs, /* vfs_statfs */ 368 tmpfs_sync, /* vfs_sync */ 369 tmpfs_vget, /* vfs_vget */ 370 tmpfs_fhtovp, /* vfs_fhtovp */ 371 tmpfs_vptofh, /* vfs_vptofh */ 372 tmpfs_init, /* vfs_init */ 373 (void *)eopnotsupp, /* vfs_sysctl */ 374 (void *)eopnotsupp, 375 }; 376