1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/sysmacros.h> 33 #include <sys/kmem.h> 34 #include <sys/pathname.h> 35 #include <sys/acl.h> 36 #include <sys/vnode.h> 37 #include <sys/vfs.h> 38 #include <sys/mntent.h> 39 #include <sys/mount.h> 40 #include <sys/cmn_err.h> 41 #include "fs/fs_subr.h" 42 #include <sys/zfs_znode.h> 43 #include <sys/zil.h> 44 #include <sys/fs/zfs.h> 45 #include <sys/dmu.h> 46 #include <sys/dsl_prop.h> 47 #include <sys/spa.h> 48 #include <sys/zap.h> 49 #include <sys/varargs.h> 50 #include <sys/policy.h> 51 #include <sys/atomic.h> 52 #include <sys/mkdev.h> 53 #include <sys/modctl.h> 54 #include <sys/zfs_ioctl.h> 55 #include <sys/zfs_ctldir.h> 56 #include <sys/sunddi.h> 57 58 int zfsfstype; 59 vfsops_t *zfs_vfsops = NULL; 60 static major_t zfs_major; 61 static minor_t zfs_minor; 62 static kmutex_t zfs_dev_mtx; 63 64 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr); 65 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr); 66 static int zfs_root(vfs_t *vfsp, vnode_t **vpp); 67 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp); 68 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp); 69 static void zfs_freevfs(vfs_t *vfsp); 70 static void zfs_objset_close(zfsvfs_t *zfsvfs); 71 72 static const fs_operation_def_t zfs_vfsops_template[] = { 73 VFSNAME_MOUNT, zfs_mount, 74 VFSNAME_UNMOUNT, zfs_umount, 75 VFSNAME_ROOT, zfs_root, 76 VFSNAME_STATVFS, zfs_statvfs, 77 VFSNAME_SYNC, (fs_generic_func_p) zfs_sync, 78 VFSNAME_VGET, zfs_vget, 79 VFSNAME_FREEVFS, (fs_generic_func_p) zfs_freevfs, 80 NULL, NULL 81 }; 82 83 static const fs_operation_def_t zfs_vfsops_eio_template[] = { 84 VFSNAME_FREEVFS, (fs_generic_func_p) zfs_freevfs, 85 NULL, NULL 86 }; 87 88 /* 89 * We need to keep a count of active fs's. 90 * This is necessary to prevent our module 91 * from being unloaded after a umount -f 92 */ 93 static uint32_t zfs_active_fs_count = 0; 94 95 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL }; 96 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL }; 97 98 static mntopt_t mntopts[] = { 99 { MNTOPT_XATTR, NULL, NULL, MO_NODISPLAY|MO_DEFAULT, NULL }, 100 { MNTOPT_NOATIME, noatime_cancel, NULL, MO_DEFAULT, NULL }, 101 { MNTOPT_ATIME, atime_cancel, NULL, 0, NULL } 102 }; 103 104 static mntopts_t zfs_mntopts = { 105 sizeof (mntopts) / sizeof (mntopt_t), 106 mntopts 107 }; 108 109 /*ARGSUSED*/ 110 int 111 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr) 112 { 113 /* 114 * Data integrity is job one. We don't want a compromised kernel 115 * writing to the storage pool, so we never sync during panic. 116 */ 117 if (panicstr) 118 return (0); 119 120 /* 121 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS 122 * to sync metadata, which they would otherwise cache indefinitely. 123 * Semantically, the only requirement is that the sync be initiated. 124 * The DMU syncs out txgs frequently, so there's nothing to do. 125 */ 126 if (flag & SYNC_ATTR) 127 return (0); 128 129 if (vfsp != NULL) { 130 /* 131 * Sync a specific filesystem. 132 */ 133 zfsvfs_t *zfsvfs = vfsp->vfs_data; 134 135 ZFS_ENTER(zfsvfs); 136 if (zfsvfs->z_log != NULL) 137 zil_commit(zfsvfs->z_log, UINT64_MAX, FSYNC); 138 else 139 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0); 140 ZFS_EXIT(zfsvfs); 141 } else { 142 /* 143 * Sync all ZFS filesystems. This is what happens when you 144 * run sync(1M). Unlike other filesystems, ZFS honors the 145 * request by waiting for all pools to commit all dirty data. 146 */ 147 spa_sync_allpools(); 148 } 149 150 return (0); 151 } 152 153 static void 154 atime_changed_cb(void *arg, uint64_t newval) 155 { 156 zfsvfs_t *zfsvfs = arg; 157 158 if (newval == TRUE) { 159 zfsvfs->z_atime = TRUE; 160 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME); 161 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0); 162 } else { 163 zfsvfs->z_atime = FALSE; 164 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME); 165 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0); 166 } 167 } 168 169 static void 170 blksz_changed_cb(void *arg, uint64_t newval) 171 { 172 zfsvfs_t *zfsvfs = arg; 173 174 if (newval < SPA_MINBLOCKSIZE || 175 newval > SPA_MAXBLOCKSIZE || !ISP2(newval)) 176 newval = SPA_MAXBLOCKSIZE; 177 178 zfsvfs->z_max_blksz = newval; 179 zfsvfs->z_vfs->vfs_bsize = newval; 180 } 181 182 static void 183 readonly_changed_cb(void *arg, uint64_t newval) 184 { 185 zfsvfs_t *zfsvfs = arg; 186 187 if (newval) { 188 /* XXX locking on vfs_flag? */ 189 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY; 190 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW); 191 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0); 192 (void) zfs_delete_thread_target(zfsvfs, 0); 193 } else { 194 /* XXX locking on vfs_flag? */ 195 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY; 196 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO); 197 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0); 198 (void) zfs_delete_thread_target(zfsvfs, 1); 199 } 200 } 201 202 static void 203 devices_changed_cb(void *arg, uint64_t newval) 204 { 205 zfsvfs_t *zfsvfs = arg; 206 207 if (newval == FALSE) { 208 zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES; 209 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES); 210 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0); 211 } else { 212 zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES; 213 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES); 214 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0); 215 } 216 } 217 218 static void 219 setuid_changed_cb(void *arg, uint64_t newval) 220 { 221 zfsvfs_t *zfsvfs = arg; 222 223 if (newval == FALSE) { 224 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID; 225 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID); 226 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0); 227 } else { 228 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID; 229 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID); 230 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0); 231 } 232 } 233 234 static void 235 exec_changed_cb(void *arg, uint64_t newval) 236 { 237 zfsvfs_t *zfsvfs = arg; 238 239 if (newval == FALSE) { 240 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC; 241 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC); 242 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0); 243 } else { 244 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC; 245 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC); 246 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0); 247 } 248 } 249 250 static void 251 snapdir_changed_cb(void *arg, uint64_t newval) 252 { 253 zfsvfs_t *zfsvfs = arg; 254 255 zfsvfs->z_show_ctldir = newval; 256 } 257 258 static void 259 acl_mode_changed_cb(void *arg, uint64_t newval) 260 { 261 zfsvfs_t *zfsvfs = arg; 262 263 zfsvfs->z_acl_mode = newval; 264 } 265 266 static void 267 acl_inherit_changed_cb(void *arg, uint64_t newval) 268 { 269 zfsvfs_t *zfsvfs = arg; 270 271 zfsvfs->z_acl_inherit = newval; 272 } 273 274 /*ARGSUSED*/ 275 static int 276 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr) 277 { 278 zfsvfs_t *zfsvfs = NULL; 279 znode_t *zp = NULL; 280 vnode_t *vp = NULL; 281 objset_t *os = NULL; 282 struct dsl_dataset *ds; 283 char *osname; 284 uint64_t readonly, recordsize; 285 pathname_t spn; 286 dev_t mount_dev; 287 major_t new_major; 288 int mode; 289 int error = 0; 290 uio_seg_t fromspace = (uap->flags & MS_SYSSPACE) ? 291 UIO_SYSSPACE : UIO_USERSPACE; 292 int canwrite; 293 294 if (mvp->v_type != VDIR) 295 return (ENOTDIR); 296 297 mutex_enter(&mvp->v_lock); 298 if ((uap->flags & MS_REMOUNT) == 0 && 299 (uap->flags & MS_OVERLAY) == 0 && 300 (mvp->v_count != 1 || (mvp->v_flag & VROOT))) { 301 mutex_exit(&mvp->v_lock); 302 return (EBUSY); 303 } 304 mutex_exit(&mvp->v_lock); 305 306 /* 307 * ZFS does not support passing unparsed data in via MS_DATA. 308 * Users should use the MS_OPTIONSTR interface; this means 309 * that all option parsing is already done and the options struct 310 * can be interrogated. 311 */ 312 if ((uap->flags & MS_DATA) && uap->datalen > 0) 313 return (EINVAL); 314 315 /* 316 * When doing a remount, we simply refresh our temporary properties 317 * according to those options set in the current VFS options. 318 */ 319 if (uap->flags & MS_REMOUNT) { 320 zfsvfs = vfsp->vfs_data; 321 322 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) 323 readonly_changed_cb(zfsvfs, B_TRUE); 324 else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) { 325 if (dmu_objset_is_snapshot(zfsvfs->z_os)) 326 return (EROFS); 327 readonly_changed_cb(zfsvfs, B_FALSE); 328 } 329 330 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) { 331 devices_changed_cb(zfsvfs, B_FALSE); 332 setuid_changed_cb(zfsvfs, B_FALSE); 333 } else { 334 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) 335 devices_changed_cb(zfsvfs, B_FALSE); 336 else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) 337 devices_changed_cb(zfsvfs, B_TRUE); 338 339 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) 340 setuid_changed_cb(zfsvfs, B_FALSE); 341 else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) 342 setuid_changed_cb(zfsvfs, B_TRUE); 343 } 344 345 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) 346 exec_changed_cb(zfsvfs, B_FALSE); 347 else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) 348 exec_changed_cb(zfsvfs, B_TRUE); 349 350 return (0); 351 } 352 353 /* 354 * Get the objset name (the "special" mount argument). 355 */ 356 if (error = pn_get(uap->spec, fromspace, &spn)) 357 return (error); 358 359 osname = spn.pn_path; 360 361 if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0) 362 goto out; 363 364 /* 365 * Refuse to mount a filesystem if we are in a local zone and the 366 * dataset is not visible. 367 */ 368 if (!INGLOBALZONE(curproc) && 369 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) { 370 error = EPERM; 371 goto out; 372 } 373 374 /* 375 * Initialize the zfs-specific filesystem structure. 376 * Should probably make this a kmem cache, shuffle fields, 377 * and just bzero upto z_hold_mtx[]. 378 */ 379 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP); 380 zfsvfs->z_vfs = vfsp; 381 zfsvfs->z_parent = zfsvfs; 382 zfsvfs->z_assign = TXG_NOWAIT; 383 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE; 384 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE; 385 386 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 387 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t), 388 offsetof(znode_t, z_link_node)); 389 rw_init(&zfsvfs->z_um_lock, NULL, RW_DEFAULT, NULL); 390 391 /* 392 * Initialize the generic filesystem structure. 393 */ 394 vfsp->vfs_bcount = 0; 395 vfsp->vfs_data = NULL; 396 397 /* 398 * Create a unique device for the mount. 399 */ 400 do { 401 ASSERT3U(zfs_minor, <=, MAXMIN32); 402 minor_t start = zfs_minor; 403 do { 404 mutex_enter(&zfs_dev_mtx); 405 if (zfs_minor >= MAXMIN32) { 406 /* 407 * If we're still using the real major number, 408 * keep out of /dev/zfs and /dev/zvol minor 409 * number space. If we're using a getudev()'ed 410 * major number, we can use all of its minors. 411 */ 412 if (zfs_major == ddi_name_to_major(ZFS_DRIVER)) 413 zfs_minor = ZFS_MIN_MINOR; 414 else 415 zfs_minor = 0; 416 } else { 417 zfs_minor++; 418 } 419 mount_dev = makedevice(zfs_major, zfs_minor); 420 mutex_exit(&zfs_dev_mtx); 421 } while (vfs_devismounted(mount_dev) && zfs_minor != start); 422 if (zfs_minor == start) { 423 /* 424 * We are using all ~262,000 minor numbers 425 * for the current major number. Create a 426 * new major number. 427 */ 428 if ((new_major = getudev()) == (major_t)-1) { 429 cmn_err(CE_WARN, 430 "zfs_mount: Can't get unique" 431 " major device number."); 432 goto out; 433 } 434 mutex_enter(&zfs_dev_mtx); 435 zfs_major = new_major; 436 zfs_minor = 0; 437 mutex_exit(&zfs_dev_mtx); 438 } else { 439 break; 440 } 441 /* CONSTANTCONDITION */ 442 } while (1); 443 444 ASSERT(vfs_devismounted(mount_dev) == 0); 445 446 if (dsl_prop_get_integer(osname, "recordsize", &recordsize, NULL) != 0) 447 recordsize = SPA_MAXBLOCKSIZE; 448 449 vfsp->vfs_dev = mount_dev; 450 vfsp->vfs_fstype = zfsfstype; 451 vfsp->vfs_bsize = recordsize; 452 vfsp->vfs_flag |= VFS_NOTRUNC; 453 vfsp->vfs_data = zfsvfs; 454 455 error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL); 456 if (error) 457 goto out; 458 459 if (readonly) 460 mode = DS_MODE_PRIMARY | DS_MODE_READONLY; 461 else 462 mode = DS_MODE_PRIMARY; 463 464 error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os); 465 if (error == EROFS) { 466 mode = DS_MODE_PRIMARY | DS_MODE_READONLY; 467 error = dmu_objset_open(osname, DMU_OST_ZFS, mode, 468 &zfsvfs->z_os); 469 } 470 os = zfsvfs->z_os; 471 472 if (error) 473 goto out; 474 475 if (error = zfs_init_fs(zfsvfs, &zp, cr)) 476 goto out; 477 478 if (dmu_objset_is_snapshot(os)) { 479 ASSERT(mode & DS_MODE_READONLY); 480 atime_changed_cb(zfsvfs, B_FALSE); 481 readonly_changed_cb(zfsvfs, B_TRUE); 482 zfsvfs->z_issnap = B_TRUE; 483 } else { 484 int do_readonly = FALSE, readonly; 485 int do_setuid = FALSE, setuid; 486 int do_exec = FALSE, exec; 487 int do_devices = FALSE, devices; 488 489 /* 490 * Start a delete thread running. 491 */ 492 (void) zfs_delete_thread_target(zfsvfs, 1); 493 494 /* 495 * Parse and replay the intent log. 496 */ 497 zil_replay(os, zfsvfs, &zfsvfs->z_assign, zfs_replay_vector, 498 (void (*)(void *))zfs_delete_wait_empty); 499 500 if (!zil_disable) 501 zfsvfs->z_log = zil_open(os, zfs_get_data); 502 503 /* 504 * The act of registering our callbacks will destroy any mount 505 * options we may have. In order to enable temporary overrides 506 * of mount options, we stash away the current values and 507 * restore them after we register the callbacks. 508 */ 509 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) { 510 readonly = B_TRUE; 511 do_readonly = B_TRUE; 512 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) { 513 readonly = B_FALSE; 514 do_readonly = B_TRUE; 515 } 516 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) { 517 devices = B_FALSE; 518 setuid = B_FALSE; 519 do_devices = B_TRUE; 520 do_setuid = B_TRUE; 521 } else { 522 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) { 523 devices = B_FALSE; 524 do_devices = B_TRUE; 525 } else if (vfs_optionisset(vfsp, 526 MNTOPT_DEVICES, NULL)) { 527 devices = B_TRUE; 528 do_devices = B_TRUE; 529 } 530 531 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) { 532 setuid = B_FALSE; 533 do_setuid = B_TRUE; 534 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) { 535 setuid = B_TRUE; 536 do_setuid = B_TRUE; 537 } 538 } 539 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) { 540 exec = B_FALSE; 541 do_exec = B_TRUE; 542 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) { 543 exec = B_TRUE; 544 do_exec = B_TRUE; 545 } 546 547 /* 548 * Register property callbacks. 549 */ 550 ds = dmu_objset_ds(os); 551 VERIFY(dsl_prop_register(ds, "atime", atime_changed_cb, 552 zfsvfs) == 0); 553 554 VERIFY(dsl_prop_register(ds, "recordsize", blksz_changed_cb, 555 zfsvfs) == 0); 556 557 VERIFY(dsl_prop_register(ds, "readonly", readonly_changed_cb, 558 zfsvfs) == 0); 559 560 VERIFY(dsl_prop_register(ds, "devices", devices_changed_cb, 561 zfsvfs) == 0); 562 563 VERIFY(dsl_prop_register(ds, "setuid", setuid_changed_cb, 564 zfsvfs) == 0); 565 566 VERIFY(dsl_prop_register(ds, "exec", exec_changed_cb, 567 zfsvfs) == 0); 568 569 VERIFY(dsl_prop_register(ds, "snapdir", snapdir_changed_cb, 570 zfsvfs) == 0); 571 572 VERIFY(dsl_prop_register(ds, "aclmode", acl_mode_changed_cb, 573 zfsvfs) == 0); 574 575 VERIFY(dsl_prop_register(ds, "aclinherit", 576 acl_inherit_changed_cb, zfsvfs) == 0); 577 578 579 /* 580 * Invoke our callbacks to restore temporary mount options. 581 */ 582 if (do_readonly) 583 readonly_changed_cb(zfsvfs, readonly); 584 if (do_setuid) 585 setuid_changed_cb(zfsvfs, setuid); 586 if (do_exec) 587 exec_changed_cb(zfsvfs, exec); 588 if (do_devices) 589 devices_changed_cb(zfsvfs, devices); 590 } 591 592 vp = ZTOV(zp); 593 if (!zfsvfs->z_issnap) 594 zfsctl_create(zfsvfs); 595 out: 596 if (error) { 597 if (zp) 598 VN_RELE(vp); 599 600 if (zfsvfs) { 601 if (os) 602 dmu_objset_close(os); 603 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 604 } 605 } else { 606 atomic_add_32(&zfs_active_fs_count, 1); 607 VN_RELE(vp); 608 } 609 610 pn_free(&spn); 611 return (error); 612 } 613 614 static int 615 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp) 616 { 617 zfsvfs_t *zfsvfs = vfsp->vfs_data; 618 dmu_objset_stats_t dstats; 619 dev32_t d32; 620 621 ZFS_ENTER(zfsvfs); 622 623 dmu_objset_stats(zfsvfs->z_os, &dstats); 624 625 /* 626 * The underlying storage pool actually uses multiple block sizes. 627 * We report the fragsize as the smallest block size we support, 628 * and we report our blocksize as the filesystem's maximum blocksize. 629 */ 630 statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT; 631 statp->f_bsize = zfsvfs->z_max_blksz; 632 633 /* 634 * The following report "total" blocks of various kinds in the 635 * file system, but reported in terms of f_frsize - the 636 * "fragment" size. 637 */ 638 639 statp->f_blocks = 640 (dstats.dds_space_refd + dstats.dds_available) >> SPA_MINBLOCKSHIFT; 641 statp->f_bfree = dstats.dds_available >> SPA_MINBLOCKSHIFT; 642 statp->f_bavail = statp->f_bfree; /* no root reservation */ 643 644 /* 645 * statvfs() should really be called statufs(), because it assumes 646 * static metadata. ZFS doesn't preallocate files, so the best 647 * we can do is report the max that could possibly fit in f_files, 648 * and that minus the number actually used in f_ffree. 649 * For f_ffree, report the smaller of the number of object available 650 * and the number of blocks (each object will take at least a block). 651 */ 652 statp->f_ffree = MIN(dstats.dds_objects_avail, statp->f_bfree); 653 statp->f_favail = statp->f_ffree; /* no "root reservation" */ 654 statp->f_files = statp->f_ffree + dstats.dds_objects_used; 655 656 (void) cmpldev(&d32, vfsp->vfs_dev); 657 statp->f_fsid = d32; 658 659 /* 660 * We're a zfs filesystem. 661 */ 662 (void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name); 663 664 statp->f_flag = vf_to_stf(vfsp->vfs_flag); 665 666 statp->f_namemax = ZFS_MAXNAMELEN; 667 668 /* 669 * We have all of 32 characters to stuff a string here. 670 * Is there anything useful we could/should provide? 671 */ 672 bzero(statp->f_fstr, sizeof (statp->f_fstr)); 673 674 ZFS_EXIT(zfsvfs); 675 return (0); 676 } 677 678 static int 679 zfs_root(vfs_t *vfsp, vnode_t **vpp) 680 { 681 zfsvfs_t *zfsvfs = vfsp->vfs_data; 682 znode_t *rootzp; 683 int error; 684 685 ZFS_ENTER(zfsvfs); 686 687 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp); 688 if (error == 0) 689 *vpp = ZTOV(rootzp); 690 691 ZFS_EXIT(zfsvfs); 692 return (error); 693 } 694 695 /*ARGSUSED*/ 696 static int 697 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr) 698 { 699 zfsvfs_t *zfsvfs = vfsp->vfs_data; 700 int ret; 701 702 if ((ret = secpolicy_fs_unmount(cr, vfsp)) != 0) 703 return (ret); 704 705 /* 706 * Unmount any snapshots mounted under .zfs before unmounting the 707 * dataset itself. 708 */ 709 if (zfsvfs->z_ctldir != NULL && 710 (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) 711 return (ret); 712 713 if (fflag & MS_FORCE) { 714 vfsp->vfs_flag |= VFS_UNMOUNTED; 715 zfsvfs->z_unmounted1 = B_TRUE; 716 717 /* 718 * Wait for all zfs threads to leave zfs. 719 * Grabbing a rwlock as reader in all vops and 720 * as writer here doesn't work because it too easy to get 721 * multiple reader enters as zfs can re-enter itself. 722 * This can lead to deadlock if there is an intervening 723 * rw_enter as writer. 724 * So a file system threads ref count (z_op_cnt) is used. 725 * A polling loop on z_op_cnt may seem inefficient, but 726 * - this saves all threads on exit from having to grab a 727 * mutex in order to cv_signal 728 * - only occurs on forced unmount in the rare case when 729 * there are outstanding threads within the file system. 730 */ 731 while (zfsvfs->z_op_cnt) { 732 delay(1); 733 } 734 735 zfs_objset_close(zfsvfs); 736 737 return (0); 738 } 739 740 zfs_zcache_flush(zfsvfs); 741 742 /* 743 * Stop all delete threads. 744 */ 745 (void) zfs_delete_thread_target(zfsvfs, 0); 746 747 /* 748 * Check the number of active vnodes in the file system. 749 * Our count is maintained in the vfs structure, but the number 750 * is off by 1 to indicate a hold on the vfs structure itself. 751 * 752 * The '.zfs' directory maintains a reference of its own, and any active 753 * references underneath are reflected in the vnode count. 754 */ 755 if (zfsvfs->z_ctldir == NULL) { 756 if (vfsp->vfs_count > 1) { 757 if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0) 758 (void) zfs_delete_thread_target(zfsvfs, 1); 759 return (EBUSY); 760 } 761 } else { 762 if (vfsp->vfs_count > 2 || 763 (zfsvfs->z_ctldir->v_count > 1 && !(fflag & MS_FORCE))) { 764 if ((zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) == 0) 765 (void) zfs_delete_thread_target(zfsvfs, 1); 766 return (EBUSY); 767 } 768 } 769 770 vfsp->vfs_flag |= VFS_UNMOUNTED; 771 zfs_objset_close(zfsvfs); 772 773 return (0); 774 } 775 776 static int 777 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp) 778 { 779 zfsvfs_t *zfsvfs = vfsp->vfs_data; 780 znode_t *zp; 781 uint64_t object = 0; 782 uint64_t fid_gen = 0; 783 uint64_t gen_mask; 784 uint64_t zp_gen; 785 int i, err; 786 787 *vpp = NULL; 788 789 ZFS_ENTER(zfsvfs); 790 791 if (fidp->fid_len == LONG_FID_LEN) { 792 zfid_long_t *zlfid = (zfid_long_t *)fidp; 793 uint64_t objsetid = 0; 794 uint64_t setgen = 0; 795 796 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 797 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i); 798 799 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 800 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i); 801 802 ZFS_EXIT(zfsvfs); 803 804 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs); 805 if (err) 806 return (EINVAL); 807 ZFS_ENTER(zfsvfs); 808 } 809 810 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) { 811 zfid_short_t *zfid = (zfid_short_t *)fidp; 812 813 for (i = 0; i < sizeof (zfid->zf_object); i++) 814 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i); 815 816 for (i = 0; i < sizeof (zfid->zf_gen); i++) 817 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i); 818 } else { 819 ZFS_EXIT(zfsvfs); 820 return (EINVAL); 821 } 822 823 /* A zero fid_gen means we are in the .zfs control directories */ 824 if (fid_gen == 0 && 825 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) { 826 *vpp = zfsvfs->z_ctldir; 827 ASSERT(*vpp != NULL); 828 if (object == ZFSCTL_INO_SNAPDIR) { 829 VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL, 830 0, NULL, NULL) == 0); 831 } else { 832 VN_HOLD(*vpp); 833 } 834 ZFS_EXIT(zfsvfs); 835 return (0); 836 } 837 838 gen_mask = -1ULL >> (64 - 8 * i); 839 840 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask); 841 if (err = zfs_zget(zfsvfs, object, &zp)) { 842 ZFS_EXIT(zfsvfs); 843 return (err); 844 } 845 zp_gen = zp->z_phys->zp_gen & gen_mask; 846 if (zp_gen == 0) 847 zp_gen = 1; 848 if (zp->z_reap || zp_gen != fid_gen) { 849 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen); 850 VN_RELE(ZTOV(zp)); 851 ZFS_EXIT(zfsvfs); 852 return (EINVAL); 853 } 854 855 *vpp = ZTOV(zp); 856 ZFS_EXIT(zfsvfs); 857 return (0); 858 } 859 860 static void 861 zfs_objset_close(zfsvfs_t *zfsvfs) 862 { 863 zfs_delete_t *zd = &zfsvfs->z_delete_head; 864 znode_t *zp, *nextzp; 865 objset_t *os = zfsvfs->z_os; 866 struct dsl_dataset *ds; 867 868 /* 869 * Stop all delete threads. 870 */ 871 (void) zfs_delete_thread_target(zfsvfs, 0); 872 873 /* 874 * For forced unmount, at this point all vops except zfs_inactive 875 * are erroring EIO. We need to now suspend zfs_inactive threads 876 * while we are freeing dbufs before switching zfs_inactive 877 * to use behaviour without a objset. 878 */ 879 rw_enter(&zfsvfs->z_um_lock, RW_WRITER); 880 881 zfs_zcache_flush(zfsvfs); 882 883 /* 884 * Release all delete in progress znodes 885 * They will be processed when the file system remounts. 886 */ 887 mutex_enter(&zd->z_mutex); 888 while (zp = list_head(&zd->z_znodes)) { 889 list_remove(&zd->z_znodes, zp); 890 zp->z_dbuf_held = 0; 891 dmu_buf_rele(zp->z_dbuf); 892 } 893 mutex_exit(&zd->z_mutex); 894 895 /* 896 * Release all holds on dbufs 897 * Note, although we have stopped all other vop threads and 898 * zfs_inactive(), the dmu can callback via znode_pageout_func() 899 * which can zfs_znode_free() the znode. 900 * So we lock z_all_znodes; search the list for a held 901 * dbuf; drop the lock (we know zp can't disappear if we hold 902 * a dbuf lock; then regrab the lock and restart. 903 */ 904 mutex_enter(&zfsvfs->z_znodes_lock); 905 for (zp = list_head(&zfsvfs->z_all_znodes); zp; zp = nextzp) { 906 nextzp = list_next(&zfsvfs->z_all_znodes, zp); 907 if (zp->z_dbuf_held) { 908 /* dbufs should only be held when force unmounting */ 909 zp->z_dbuf_held = 0; 910 mutex_exit(&zfsvfs->z_znodes_lock); 911 dmu_buf_rele(zp->z_dbuf); 912 /* Start again */ 913 mutex_enter(&zfsvfs->z_znodes_lock); 914 nextzp = list_head(&zfsvfs->z_all_znodes); 915 } 916 } 917 mutex_exit(&zfsvfs->z_znodes_lock); 918 919 /* 920 * Unregister properties. 921 */ 922 if (!dmu_objset_is_snapshot(os)) { 923 ds = dmu_objset_ds(os); 924 925 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb, 926 zfsvfs) == 0); 927 928 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, 929 zfsvfs) == 0); 930 931 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb, 932 zfsvfs) == 0); 933 934 VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb, 935 zfsvfs) == 0); 936 937 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb, 938 zfsvfs) == 0); 939 940 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb, 941 zfsvfs) == 0); 942 943 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, 944 zfsvfs) == 0); 945 946 VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, 947 zfsvfs) == 0); 948 949 VERIFY(dsl_prop_unregister(ds, "aclinherit", 950 acl_inherit_changed_cb, zfsvfs) == 0); 951 } 952 953 /* 954 * Make the dmu drop all it dbuf holds so that zfs_inactive 955 * can then safely free znode/vnodes. 956 */ 957 txg_wait_synced(dmu_objset_pool(os), 0); 958 959 /* 960 * Switch zfs_inactive to behaviour without an objset. 961 * It just tosses cached pages and frees the znode & vnode. 962 * Then re-enable zfs_inactive threads in that new behaviour. 963 */ 964 zfsvfs->z_unmounted2 = B_TRUE; 965 rw_exit(&zfsvfs->z_um_lock); /* re-enable any zfs_inactive threads */ 966 967 /* 968 * Close the zil. Can't close the zil while zfs_inactive 969 * threads are blocked as zil_close can call zfs_inactive. 970 */ 971 if (zfsvfs->z_log) { 972 zil_close(zfsvfs->z_log); 973 zfsvfs->z_log = NULL; 974 } 975 976 /* 977 * Finally close the objset 978 */ 979 dmu_objset_close(os); 980 981 /* 982 * We can now safely destroy the '.zfs' directory node. 983 */ 984 if (zfsvfs->z_ctldir != NULL) 985 zfsctl_destroy(zfsvfs); 986 987 } 988 989 static void 990 zfs_freevfs(vfs_t *vfsp) 991 { 992 zfsvfs_t *zfsvfs = vfsp->vfs_data; 993 994 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 995 996 atomic_add_32(&zfs_active_fs_count, -1); 997 } 998 999 /* 1000 * VFS_INIT() initialization. Note that there is no VFS_FINI(), 1001 * so we can't safely do any non-idempotent initialization here. 1002 * Leave that to zfs_init() and zfs_fini(), which are called 1003 * from the module's _init() and _fini() entry points. 1004 */ 1005 /*ARGSUSED*/ 1006 static int 1007 zfs_vfsinit(int fstype, char *name) 1008 { 1009 int error; 1010 1011 zfsfstype = fstype; 1012 1013 /* 1014 * Setup vfsops and vnodeops tables. 1015 */ 1016 error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops); 1017 if (error != 0) { 1018 cmn_err(CE_WARN, "zfs: bad vfs ops template"); 1019 } 1020 1021 error = zfs_create_op_tables(); 1022 if (error) { 1023 zfs_remove_op_tables(); 1024 cmn_err(CE_WARN, "zfs: bad vnode ops template"); 1025 (void) vfs_freevfsops_by_type(zfsfstype); 1026 return (error); 1027 } 1028 1029 mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 1030 1031 /* 1032 * Unique major number for all zfs mounts. 1033 * If we run out of 32-bit minors, we'll getudev() another major. 1034 */ 1035 zfs_major = ddi_name_to_major(ZFS_DRIVER); 1036 zfs_minor = ZFS_MIN_MINOR; 1037 1038 return (0); 1039 } 1040 1041 void 1042 zfs_init(void) 1043 { 1044 /* 1045 * Initialize .zfs directory structures 1046 */ 1047 zfsctl_init(); 1048 1049 /* 1050 * Initialize znode cache, vnode ops, etc... 1051 */ 1052 zfs_znode_init(); 1053 } 1054 1055 void 1056 zfs_fini(void) 1057 { 1058 zfsctl_fini(); 1059 zfs_znode_fini(); 1060 } 1061 1062 int 1063 zfs_busy(void) 1064 { 1065 return (zfs_active_fs_count != 0); 1066 } 1067 1068 static vfsdef_t vfw = { 1069 VFSDEF_VERSION, 1070 MNTTYPE_ZFS, 1071 zfs_vfsinit, 1072 VSW_HASPROTO | VSW_CANRWRO | VSW_CANREMOUNT | VSW_VOLATILEDEV, 1073 &zfs_mntopts 1074 }; 1075 1076 struct modlfs zfs_modlfs = { 1077 &mod_fsops, "ZFS filesystem version 1", &vfw 1078 }; 1079