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