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