1 /* $OpenBSD: tmpfs_vfsops.c,v 1.19 2021/10/24 15:41:47 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 int tmpfs_mount_update(struct mount *); 71 72 int 73 tmpfs_init(struct vfsconf *vfsp) 74 { 75 76 pool_init(&tmpfs_dirent_pool, sizeof(tmpfs_dirent_t), 0, IPL_NONE, 77 PR_WAITOK, "tmpfs_dirent", NULL); 78 pool_init(&tmpfs_node_pool, sizeof(tmpfs_node_t), 0, IPL_NONE, 79 PR_WAITOK, "tmpfs_node", NULL); 80 81 return 0; 82 } 83 84 int 85 tmpfs_mount_update(struct mount *mp) 86 { 87 tmpfs_mount_t *tmp; 88 struct vnode *rootvp; 89 int error; 90 91 if ((mp->mnt_flag & MNT_RDONLY) == 0) 92 return EOPNOTSUPP; 93 94 /* ro->rw transition: nothing to do? */ 95 if (mp->mnt_flag & MNT_WANTRDWR) 96 return 0; 97 98 tmp = mp->mnt_data; 99 rootvp = tmp->tm_root->tn_vnode; 100 101 /* Lock root to prevent lookups. */ 102 error = vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY); 103 if (error) 104 return error; 105 106 /* Lock mount point to prevent nodes from being added/removed. */ 107 rw_enter_write(&tmp->tm_lock); 108 109 /* Flush files opened for writing; skip rootvp. */ 110 error = vflush(mp, rootvp, WRITECLOSE); 111 112 rw_exit_write(&tmp->tm_lock); 113 VOP_UNLOCK(rootvp); 114 115 return error; 116 } 117 118 int 119 tmpfs_mount(struct mount *mp, const char *path, void *data, 120 struct nameidata *ndp, struct proc *p) 121 { 122 struct tmpfs_args *args = data; 123 tmpfs_mount_t *tmp; 124 tmpfs_node_t *root; 125 uint64_t memlimit; 126 uint64_t nodes; 127 int error; 128 129 if (mp->mnt_flag & MNT_UPDATE) 130 return (tmpfs_mount_update(mp)); 131 132 /* Prohibit mounts if there is not enough memory. */ 133 if (tmpfs_mem_info(1) < TMPFS_PAGES_RESERVED) 134 return EINVAL; 135 136 if (args->ta_root_uid == VNOVAL || args->ta_root_gid == VNOVAL || 137 args->ta_root_mode == VNOVAL) 138 return EINVAL; 139 140 /* Get the memory usage limit for this file-system. */ 141 if (args->ta_size_max < PAGE_SIZE) { 142 memlimit = UINT64_MAX; 143 } else { 144 memlimit = args->ta_size_max; 145 } 146 KASSERT(memlimit > 0); 147 148 if (args->ta_nodes_max <= 3) { 149 nodes = 3 + (memlimit / 1024); 150 } else { 151 nodes = args->ta_nodes_max; 152 } 153 nodes = MIN(nodes, INT_MAX); 154 KASSERT(nodes >= 3); 155 156 /* Allocate the tmpfs mount structure and fill it. */ 157 tmp = malloc(sizeof(tmpfs_mount_t), M_MISCFSMNT, M_WAITOK); 158 159 tmp->tm_nodes_max = (ino_t)nodes; 160 tmp->tm_nodes_cnt = 0; 161 tmp->tm_highest_inode = 1; 162 LIST_INIT(&tmp->tm_nodes); 163 164 rw_init(&tmp->tm_lock, "tmplk"); 165 tmpfs_mntmem_init(tmp, memlimit); 166 167 /* Allocate the root node. */ 168 error = tmpfs_alloc_node(tmp, VDIR, args->ta_root_uid, 169 args->ta_root_gid, args->ta_root_mode & ALLPERMS, NULL, 170 VNOVAL, &root); 171 KASSERT(error == 0 && root != NULL); 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 */ 178 root->tn_links++; 179 root->tn_spec.tn_dir.tn_parent = root; 180 tmp->tm_root = root; 181 182 mp->mnt_data = tmp; 183 mp->mnt_flag |= MNT_LOCAL; 184 mp->mnt_stat.f_namemax = TMPFS_MAXNAMLEN; 185 vfs_getnewfsid(mp); 186 187 mp->mnt_stat.mount_info.tmpfs_args = *args; 188 189 bzero(&mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname)); 190 bzero(&mp->mnt_stat.f_mntfromname, sizeof(mp->mnt_stat.f_mntfromname)); 191 bzero(&mp->mnt_stat.f_mntfromspec, sizeof(mp->mnt_stat.f_mntfromspec)); 192 193 strlcpy(mp->mnt_stat.f_mntonname, path, 194 sizeof(mp->mnt_stat.f_mntonname) - 1); 195 strlcpy(mp->mnt_stat.f_mntfromname, "tmpfs", 196 sizeof(mp->mnt_stat.f_mntfromname) - 1); 197 strlcpy(mp->mnt_stat.f_mntfromspec, "tmpfs", 198 sizeof(mp->mnt_stat.f_mntfromspec) - 1); 199 200 return error; 201 } 202 203 int 204 tmpfs_start(struct mount *mp, int flags, struct proc *p) 205 { 206 return 0; 207 } 208 209 int 210 tmpfs_unmount(struct mount *mp, int mntflags, struct proc *p) 211 { 212 tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp); 213 tmpfs_node_t *node, *cnode; 214 int error, flags = 0; 215 216 /* Handle forced unmounts. */ 217 if (mntflags & MNT_FORCE) 218 flags |= FORCECLOSE; 219 220 /* Finalize all pending I/O. */ 221 error = vflush(mp, NULL, flags); 222 if (error != 0) 223 return error; 224 225 /* 226 * First round, detach and destroy all directory entries. 227 * Also, clear the pointers to the vnodes - they are gone. 228 */ 229 LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) { 230 tmpfs_dirent_t *de; 231 232 node->tn_vnode = NULL; 233 if (node->tn_type != VDIR) { 234 continue; 235 } 236 while ((de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir)) != NULL) { 237 cnode = de->td_node; 238 if (cnode) 239 cnode->tn_vnode = NULL; 240 tmpfs_dir_detach(node, de); 241 tmpfs_free_dirent(tmp, de); 242 } 243 } 244 245 /* Second round, destroy all inodes. */ 246 while ((node = LIST_FIRST(&tmp->tm_nodes)) != NULL) { 247 tmpfs_free_node(tmp, node); 248 } 249 250 /* Throw away the tmpfs_mount structure. */ 251 tmpfs_mntmem_destroy(tmp); 252 /* mutex_destroy(&tmp->tm_lock); */ 253 /* kmem_free(tmp, sizeof(*tmp)); */ 254 free(tmp, M_MISCFSMNT, sizeof(tmpfs_mount_t)); 255 mp->mnt_data = NULL; 256 257 return 0; 258 } 259 260 int 261 tmpfs_root(struct mount *mp, struct vnode **vpp) 262 { 263 tmpfs_node_t *node = VFS_TO_TMPFS(mp)->tm_root; 264 265 rw_enter_write(&node->tn_nlock); 266 return tmpfs_vnode_get(mp, node, vpp); 267 } 268 269 int 270 tmpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp) 271 { 272 273 printf("tmpfs_vget called; need for it unknown yet\n"); 274 return EOPNOTSUPP; 275 } 276 277 int 278 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 279 { 280 tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp); 281 tmpfs_node_t *node; 282 tmpfs_fid_t tfh; 283 284 if (fhp->fid_len != sizeof(tmpfs_fid_t)) { 285 return EINVAL; 286 } 287 memcpy(&tfh, fhp, sizeof(tmpfs_fid_t)); 288 289 rw_enter_write(&tmp->tm_lock); 290 LIST_FOREACH(node, &tmp->tm_nodes, tn_entries) { 291 if (node->tn_id != tfh.tf_id) { 292 continue; 293 } 294 if (TMPFS_NODE_GEN(node) != tfh.tf_gen) { 295 continue; 296 } 297 rw_enter_write(&node->tn_nlock); 298 break; 299 } 300 rw_exit_write(&tmp->tm_lock); 301 302 /* Will release the tn_nlock. */ 303 return node ? tmpfs_vnode_get(mp, node, vpp) : ESTALE; 304 } 305 306 int 307 tmpfs_vptofh(struct vnode *vp, struct fid *fhp) 308 { 309 tmpfs_fid_t tfh; 310 tmpfs_node_t *node; 311 312 node = VP_TO_TMPFS_NODE(vp); 313 314 memset(&tfh, 0, sizeof(tfh)); 315 tfh.tf_len = sizeof(tmpfs_fid_t); 316 tfh.tf_gen = TMPFS_NODE_GEN(node); 317 tfh.tf_id = node->tn_id; 318 memcpy(fhp, &tfh, sizeof(tfh)); 319 320 return 0; 321 } 322 323 int 324 tmpfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p) 325 { 326 tmpfs_mount_t *tmp; 327 fsfilcnt_t freenodes; 328 uint64_t avail; 329 330 tmp = VFS_TO_TMPFS(mp); 331 332 sbp->f_iosize = sbp->f_bsize = PAGE_SIZE; 333 334 rw_enter_write(&tmp->tm_acc_lock); 335 avail = tmpfs_pages_avail(tmp); 336 sbp->f_blocks = (tmpfs_bytes_max(tmp) >> PAGE_SHIFT); 337 sbp->f_bfree = avail; 338 sbp->f_bavail = avail & INT64_MAX; /* f_bavail is int64_t */ 339 340 freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_cnt, 341 avail * PAGE_SIZE / sizeof(tmpfs_node_t)); 342 343 sbp->f_files = tmp->tm_nodes_cnt + freenodes; 344 sbp->f_ffree = freenodes; 345 sbp->f_favail = freenodes & INT64_MAX; /* f_favail is int64_t */ 346 rw_exit_write(&tmp->tm_acc_lock); 347 348 copy_statfs_info(sbp, mp); 349 350 return 0; 351 } 352 353 int 354 tmpfs_sync(struct mount *mp, int waitfor, int stall, struct ucred *cred, 355 struct proc *p) 356 { 357 358 return 0; 359 } 360 361 /* 362 * tmpfs vfs operations. 363 */ 364 365 const struct vfsops tmpfs_vfsops = { 366 .vfs_mount = tmpfs_mount, 367 .vfs_start = tmpfs_start, 368 .vfs_unmount = tmpfs_unmount, 369 .vfs_root = tmpfs_root, 370 .vfs_quotactl = (void *)eopnotsupp, 371 .vfs_statfs = tmpfs_statfs, 372 .vfs_sync = tmpfs_sync, 373 .vfs_vget = tmpfs_vget, 374 .vfs_fhtovp = tmpfs_fhtovp, 375 .vfs_vptofh = tmpfs_vptofh, 376 .vfs_init = tmpfs_init, 377 .vfs_sysctl = (void *)eopnotsupp, 378 .vfs_checkexp = (void *)eopnotsupp, 379 }; 380