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 2008 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/vnode.h> 35 #include <sys/vfs.h> 36 #include <sys/vfs_opreg.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/zfs_dir.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/dsl_dataset.h> 48 #include <sys/dsl_deleg.h> 49 #include <sys/spa.h> 50 #include <sys/zap.h> 51 #include <sys/varargs.h> 52 #include <sys/policy.h> 53 #include <sys/atomic.h> 54 #include <sys/mkdev.h> 55 #include <sys/modctl.h> 56 #include <sys/refstr.h> 57 #include <sys/zfs_ioctl.h> 58 #include <sys/zfs_ctldir.h> 59 #include <sys/zfs_fuid.h> 60 #include <sys/bootconf.h> 61 #include <sys/sunddi.h> 62 #include <sys/dnlc.h> 63 #include <sys/dmu_objset.h> 64 65 int zfsfstype; 66 vfsops_t *zfs_vfsops = NULL; 67 static major_t zfs_major; 68 static minor_t zfs_minor; 69 static kmutex_t zfs_dev_mtx; 70 71 static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr); 72 static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr); 73 static int zfs_mountroot(vfs_t *vfsp, enum whymountroot); 74 static int zfs_root(vfs_t *vfsp, vnode_t **vpp); 75 static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp); 76 static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp); 77 static void zfs_freevfs(vfs_t *vfsp); 78 79 static const fs_operation_def_t zfs_vfsops_template[] = { 80 VFSNAME_MOUNT, { .vfs_mount = zfs_mount }, 81 VFSNAME_MOUNTROOT, { .vfs_mountroot = zfs_mountroot }, 82 VFSNAME_UNMOUNT, { .vfs_unmount = zfs_umount }, 83 VFSNAME_ROOT, { .vfs_root = zfs_root }, 84 VFSNAME_STATVFS, { .vfs_statvfs = zfs_statvfs }, 85 VFSNAME_SYNC, { .vfs_sync = zfs_sync }, 86 VFSNAME_VGET, { .vfs_vget = zfs_vget }, 87 VFSNAME_FREEVFS, { .vfs_freevfs = zfs_freevfs }, 88 NULL, NULL 89 }; 90 91 static const fs_operation_def_t zfs_vfsops_eio_template[] = { 92 VFSNAME_FREEVFS, { .vfs_freevfs = zfs_freevfs }, 93 NULL, NULL 94 }; 95 96 /* 97 * We need to keep a count of active fs's. 98 * This is necessary to prevent our module 99 * from being unloaded after a umount -f 100 */ 101 static uint32_t zfs_active_fs_count = 0; 102 103 static char *noatime_cancel[] = { MNTOPT_ATIME, NULL }; 104 static char *atime_cancel[] = { MNTOPT_NOATIME, NULL }; 105 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL }; 106 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL }; 107 108 /* 109 * MO_DEFAULT is not used since the default value is determined 110 * by the equivalent property. 111 */ 112 static mntopt_t mntopts[] = { 113 { MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL }, 114 { MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL }, 115 { MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL }, 116 { MNTOPT_ATIME, atime_cancel, NULL, 0, NULL } 117 }; 118 119 static mntopts_t zfs_mntopts = { 120 sizeof (mntopts) / sizeof (mntopt_t), 121 mntopts 122 }; 123 124 /*ARGSUSED*/ 125 int 126 zfs_sync(vfs_t *vfsp, short flag, cred_t *cr) 127 { 128 /* 129 * Data integrity is job one. We don't want a compromised kernel 130 * writing to the storage pool, so we never sync during panic. 131 */ 132 if (panicstr) 133 return (0); 134 135 /* 136 * SYNC_ATTR is used by fsflush() to force old filesystems like UFS 137 * to sync metadata, which they would otherwise cache indefinitely. 138 * Semantically, the only requirement is that the sync be initiated. 139 * The DMU syncs out txgs frequently, so there's nothing to do. 140 */ 141 if (flag & SYNC_ATTR) 142 return (0); 143 144 if (vfsp != NULL) { 145 /* 146 * Sync a specific filesystem. 147 */ 148 zfsvfs_t *zfsvfs = vfsp->vfs_data; 149 150 ZFS_ENTER(zfsvfs); 151 if (zfsvfs->z_log != NULL) 152 zil_commit(zfsvfs->z_log, UINT64_MAX, 0); 153 else 154 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0); 155 ZFS_EXIT(zfsvfs); 156 } else { 157 /* 158 * Sync all ZFS filesystems. This is what happens when you 159 * run sync(1M). Unlike other filesystems, ZFS honors the 160 * request by waiting for all pools to commit all dirty data. 161 */ 162 spa_sync_allpools(); 163 } 164 165 return (0); 166 } 167 168 static int 169 zfs_create_unique_device(dev_t *dev) 170 { 171 major_t new_major; 172 173 do { 174 ASSERT3U(zfs_minor, <=, MAXMIN32); 175 minor_t start = zfs_minor; 176 do { 177 mutex_enter(&zfs_dev_mtx); 178 if (zfs_minor >= MAXMIN32) { 179 /* 180 * If we're still using the real major 181 * keep out of /dev/zfs and /dev/zvol minor 182 * number space. If we're using a getudev()'ed 183 * major number, we can use all of its minors. 184 */ 185 if (zfs_major == ddi_name_to_major(ZFS_DRIVER)) 186 zfs_minor = ZFS_MIN_MINOR; 187 else 188 zfs_minor = 0; 189 } else { 190 zfs_minor++; 191 } 192 *dev = makedevice(zfs_major, zfs_minor); 193 mutex_exit(&zfs_dev_mtx); 194 } while (vfs_devismounted(*dev) && zfs_minor != start); 195 if (zfs_minor == start) { 196 /* 197 * We are using all ~262,000 minor numbers for the 198 * current major number. Create a new major number. 199 */ 200 if ((new_major = getudev()) == (major_t)-1) { 201 cmn_err(CE_WARN, 202 "zfs_mount: Can't get unique major " 203 "device number."); 204 return (-1); 205 } 206 mutex_enter(&zfs_dev_mtx); 207 zfs_major = new_major; 208 zfs_minor = 0; 209 210 mutex_exit(&zfs_dev_mtx); 211 } else { 212 break; 213 } 214 /* CONSTANTCONDITION */ 215 } while (1); 216 217 return (0); 218 } 219 220 static void 221 atime_changed_cb(void *arg, uint64_t newval) 222 { 223 zfsvfs_t *zfsvfs = arg; 224 225 if (newval == TRUE) { 226 zfsvfs->z_atime = TRUE; 227 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME); 228 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0); 229 } else { 230 zfsvfs->z_atime = FALSE; 231 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME); 232 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0); 233 } 234 } 235 236 static void 237 xattr_changed_cb(void *arg, uint64_t newval) 238 { 239 zfsvfs_t *zfsvfs = arg; 240 241 if (newval == TRUE) { 242 /* XXX locking on vfs_flag? */ 243 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR; 244 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR); 245 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0); 246 } else { 247 /* XXX locking on vfs_flag? */ 248 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR; 249 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR); 250 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0); 251 } 252 } 253 254 static void 255 blksz_changed_cb(void *arg, uint64_t newval) 256 { 257 zfsvfs_t *zfsvfs = arg; 258 259 if (newval < SPA_MINBLOCKSIZE || 260 newval > SPA_MAXBLOCKSIZE || !ISP2(newval)) 261 newval = SPA_MAXBLOCKSIZE; 262 263 zfsvfs->z_max_blksz = newval; 264 zfsvfs->z_vfs->vfs_bsize = newval; 265 } 266 267 static void 268 readonly_changed_cb(void *arg, uint64_t newval) 269 { 270 zfsvfs_t *zfsvfs = arg; 271 272 if (newval) { 273 /* XXX locking on vfs_flag? */ 274 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY; 275 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW); 276 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0); 277 } else { 278 /* XXX locking on vfs_flag? */ 279 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY; 280 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO); 281 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0); 282 } 283 } 284 285 static void 286 devices_changed_cb(void *arg, uint64_t newval) 287 { 288 zfsvfs_t *zfsvfs = arg; 289 290 if (newval == FALSE) { 291 zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES; 292 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES); 293 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0); 294 } else { 295 zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES; 296 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES); 297 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0); 298 } 299 } 300 301 static void 302 setuid_changed_cb(void *arg, uint64_t newval) 303 { 304 zfsvfs_t *zfsvfs = arg; 305 306 if (newval == FALSE) { 307 zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID; 308 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID); 309 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0); 310 } else { 311 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID; 312 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID); 313 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0); 314 } 315 } 316 317 static void 318 exec_changed_cb(void *arg, uint64_t newval) 319 { 320 zfsvfs_t *zfsvfs = arg; 321 322 if (newval == FALSE) { 323 zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC; 324 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC); 325 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0); 326 } else { 327 zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC; 328 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC); 329 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0); 330 } 331 } 332 333 /* 334 * The nbmand mount option can be changed at mount time. 335 * We can't allow it to be toggled on live file systems or incorrect 336 * behavior may be seen from cifs clients 337 * 338 * This property isn't registered via dsl_prop_register(), but this callback 339 * will be called when a file system is first mounted 340 */ 341 static void 342 nbmand_changed_cb(void *arg, uint64_t newval) 343 { 344 zfsvfs_t *zfsvfs = arg; 345 if (newval == FALSE) { 346 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND); 347 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0); 348 } else { 349 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND); 350 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0); 351 } 352 } 353 354 static void 355 snapdir_changed_cb(void *arg, uint64_t newval) 356 { 357 zfsvfs_t *zfsvfs = arg; 358 359 zfsvfs->z_show_ctldir = newval; 360 } 361 362 static void 363 vscan_changed_cb(void *arg, uint64_t newval) 364 { 365 zfsvfs_t *zfsvfs = arg; 366 367 zfsvfs->z_vscan = newval; 368 } 369 370 static void 371 acl_mode_changed_cb(void *arg, uint64_t newval) 372 { 373 zfsvfs_t *zfsvfs = arg; 374 375 zfsvfs->z_acl_mode = newval; 376 } 377 378 static void 379 acl_inherit_changed_cb(void *arg, uint64_t newval) 380 { 381 zfsvfs_t *zfsvfs = arg; 382 383 zfsvfs->z_acl_inherit = newval; 384 } 385 386 static int 387 zfs_register_callbacks(vfs_t *vfsp) 388 { 389 struct dsl_dataset *ds = NULL; 390 objset_t *os = NULL; 391 zfsvfs_t *zfsvfs = NULL; 392 uint64_t nbmand; 393 int readonly, do_readonly = B_FALSE; 394 int setuid, do_setuid = B_FALSE; 395 int exec, do_exec = B_FALSE; 396 int devices, do_devices = B_FALSE; 397 int xattr, do_xattr = B_FALSE; 398 int atime, do_atime = B_FALSE; 399 int error = 0; 400 401 ASSERT(vfsp); 402 zfsvfs = vfsp->vfs_data; 403 ASSERT(zfsvfs); 404 os = zfsvfs->z_os; 405 406 /* 407 * The act of registering our callbacks will destroy any mount 408 * options we may have. In order to enable temporary overrides 409 * of mount options, we stash away the current values and 410 * restore them after we register the callbacks. 411 */ 412 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) { 413 readonly = B_TRUE; 414 do_readonly = B_TRUE; 415 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) { 416 readonly = B_FALSE; 417 do_readonly = B_TRUE; 418 } 419 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) { 420 devices = B_FALSE; 421 setuid = B_FALSE; 422 do_devices = B_TRUE; 423 do_setuid = B_TRUE; 424 } else { 425 if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) { 426 devices = B_FALSE; 427 do_devices = B_TRUE; 428 } else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) { 429 devices = B_TRUE; 430 do_devices = B_TRUE; 431 } 432 433 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) { 434 setuid = B_FALSE; 435 do_setuid = B_TRUE; 436 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) { 437 setuid = B_TRUE; 438 do_setuid = B_TRUE; 439 } 440 } 441 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) { 442 exec = B_FALSE; 443 do_exec = B_TRUE; 444 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) { 445 exec = B_TRUE; 446 do_exec = B_TRUE; 447 } 448 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) { 449 xattr = B_FALSE; 450 do_xattr = B_TRUE; 451 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) { 452 xattr = B_TRUE; 453 do_xattr = B_TRUE; 454 } 455 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) { 456 atime = B_FALSE; 457 do_atime = B_TRUE; 458 } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) { 459 atime = B_TRUE; 460 do_atime = B_TRUE; 461 } 462 463 /* 464 * nbmand is a special property. It can only be changed at 465 * mount time. 466 * 467 * This is weird, but it is documented to only be changeable 468 * at mount time. 469 */ 470 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) { 471 nbmand = B_FALSE; 472 } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) { 473 nbmand = B_TRUE; 474 } else { 475 char osname[MAXNAMELEN]; 476 477 dmu_objset_name(os, osname); 478 if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand, 479 NULL)) 480 return (error); 481 } 482 483 /* 484 * Register property callbacks. 485 * 486 * It would probably be fine to just check for i/o error from 487 * the first prop_register(), but I guess I like to go 488 * overboard... 489 */ 490 ds = dmu_objset_ds(os); 491 error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs); 492 error = error ? error : dsl_prop_register(ds, 493 "xattr", xattr_changed_cb, zfsvfs); 494 error = error ? error : dsl_prop_register(ds, 495 "recordsize", blksz_changed_cb, zfsvfs); 496 error = error ? error : dsl_prop_register(ds, 497 "readonly", readonly_changed_cb, zfsvfs); 498 error = error ? error : dsl_prop_register(ds, 499 "devices", devices_changed_cb, zfsvfs); 500 error = error ? error : dsl_prop_register(ds, 501 "setuid", setuid_changed_cb, zfsvfs); 502 error = error ? error : dsl_prop_register(ds, 503 "exec", exec_changed_cb, zfsvfs); 504 error = error ? error : dsl_prop_register(ds, 505 "snapdir", snapdir_changed_cb, zfsvfs); 506 error = error ? error : dsl_prop_register(ds, 507 "aclmode", acl_mode_changed_cb, zfsvfs); 508 error = error ? error : dsl_prop_register(ds, 509 "aclinherit", acl_inherit_changed_cb, zfsvfs); 510 error = error ? error : dsl_prop_register(ds, 511 "vscan", vscan_changed_cb, zfsvfs); 512 if (error) 513 goto unregister; 514 515 /* 516 * Invoke our callbacks to restore temporary mount options. 517 */ 518 if (do_readonly) 519 readonly_changed_cb(zfsvfs, readonly); 520 if (do_setuid) 521 setuid_changed_cb(zfsvfs, setuid); 522 if (do_exec) 523 exec_changed_cb(zfsvfs, exec); 524 if (do_devices) 525 devices_changed_cb(zfsvfs, devices); 526 if (do_xattr) 527 xattr_changed_cb(zfsvfs, xattr); 528 if (do_atime) 529 atime_changed_cb(zfsvfs, atime); 530 531 nbmand_changed_cb(zfsvfs, nbmand); 532 533 return (0); 534 535 unregister: 536 /* 537 * We may attempt to unregister some callbacks that are not 538 * registered, but this is OK; it will simply return ENOMSG, 539 * which we will ignore. 540 */ 541 (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs); 542 (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs); 543 (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs); 544 (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs); 545 (void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs); 546 (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs); 547 (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs); 548 (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs); 549 (void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs); 550 (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb, 551 zfsvfs); 552 (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs); 553 return (error); 554 555 } 556 557 static int 558 zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting) 559 { 560 uint_t readonly; 561 int error; 562 563 error = zfs_register_callbacks(zfsvfs->z_vfs); 564 if (error) 565 return (error); 566 567 /* 568 * Set the objset user_ptr to track its zfsvfs. 569 */ 570 mutex_enter(&zfsvfs->z_os->os->os_user_ptr_lock); 571 dmu_objset_set_user(zfsvfs->z_os, zfsvfs); 572 mutex_exit(&zfsvfs->z_os->os->os_user_ptr_lock); 573 574 /* 575 * If we are not mounting (ie: online recv), then we don't 576 * have to worry about replaying the log as we blocked all 577 * operations out since we closed the ZIL. 578 */ 579 if (mounting) { 580 /* 581 * During replay we remove the read only flag to 582 * allow replays to succeed. 583 */ 584 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY; 585 if (readonly != 0) 586 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY; 587 else 588 zfs_unlinked_drain(zfsvfs); 589 590 /* 591 * Parse and replay the intent log. 592 * 593 * Because of ziltest, this must be done after 594 * zfs_unlinked_drain(). (Further note: ziltest doesn't 595 * use readonly mounts, where zfs_unlinked_drain() isn't 596 * called.) This is because ziltest causes spa_sync() 597 * to think it's committed, but actually it is not, so 598 * the intent log contains many txg's worth of changes. 599 * 600 * In particular, if object N is in the unlinked set in 601 * the last txg to actually sync, then it could be 602 * actually freed in a later txg and then reallocated in 603 * a yet later txg. This would write a "create object 604 * N" record to the intent log. Normally, this would be 605 * fine because the spa_sync() would have written out 606 * the fact that object N is free, before we could write 607 * the "create object N" intent log record. 608 * 609 * But when we are in ziltest mode, we advance the "open 610 * txg" without actually spa_sync()-ing the changes to 611 * disk. So we would see that object N is still 612 * allocated and in the unlinked set, and there is an 613 * intent log record saying to allocate it. 614 */ 615 zil_replay(zfsvfs->z_os, zfsvfs, &zfsvfs->z_assign, 616 zfs_replay_vector); 617 618 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */ 619 } 620 621 if (!zil_disable) 622 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data); 623 624 return (0); 625 } 626 627 static void 628 zfs_freezfsvfs(zfsvfs_t *zfsvfs) 629 { 630 mutex_destroy(&zfsvfs->z_znodes_lock); 631 mutex_destroy(&zfsvfs->z_online_recv_lock); 632 list_destroy(&zfsvfs->z_all_znodes); 633 rrw_destroy(&zfsvfs->z_teardown_lock); 634 rw_destroy(&zfsvfs->z_teardown_inactive_lock); 635 rw_destroy(&zfsvfs->z_fuid_lock); 636 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 637 } 638 639 static int 640 zfs_domount(vfs_t *vfsp, char *osname, cred_t *cr) 641 { 642 dev_t mount_dev; 643 uint64_t recordsize, readonly; 644 int error = 0; 645 int mode; 646 zfsvfs_t *zfsvfs; 647 znode_t *zp = NULL; 648 649 ASSERT(vfsp); 650 ASSERT(osname); 651 652 /* 653 * Initialize the zfs-specific filesystem structure. 654 * Should probably make this a kmem cache, shuffle fields, 655 * and just bzero up to z_hold_mtx[]. 656 */ 657 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP); 658 zfsvfs->z_vfs = vfsp; 659 zfsvfs->z_parent = zfsvfs; 660 zfsvfs->z_assign = TXG_NOWAIT; 661 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE; 662 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE; 663 664 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 665 mutex_init(&zfsvfs->z_online_recv_lock, NULL, MUTEX_DEFAULT, NULL); 666 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t), 667 offsetof(znode_t, z_link_node)); 668 rrw_init(&zfsvfs->z_teardown_lock); 669 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL); 670 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL); 671 672 /* Initialize the generic filesystem structure. */ 673 vfsp->vfs_bcount = 0; 674 vfsp->vfs_data = NULL; 675 676 if (zfs_create_unique_device(&mount_dev) == -1) { 677 error = ENODEV; 678 goto out; 679 } 680 ASSERT(vfs_devismounted(mount_dev) == 0); 681 682 if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize, 683 NULL)) 684 goto out; 685 686 vfsp->vfs_dev = mount_dev; 687 vfsp->vfs_fstype = zfsfstype; 688 vfsp->vfs_bsize = recordsize; 689 vfsp->vfs_flag |= VFS_NOTRUNC; 690 vfsp->vfs_data = zfsvfs; 691 692 if (error = dsl_prop_get_integer(osname, "readonly", &readonly, NULL)) 693 goto out; 694 695 if (readonly) 696 mode = DS_MODE_PRIMARY | DS_MODE_READONLY; 697 else 698 mode = DS_MODE_PRIMARY; 699 700 error = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os); 701 if (error == EROFS) { 702 mode = DS_MODE_PRIMARY | DS_MODE_READONLY; 703 error = dmu_objset_open(osname, DMU_OST_ZFS, mode, 704 &zfsvfs->z_os); 705 } 706 707 if (error) 708 goto out; 709 710 if (error = zfs_init_fs(zfsvfs, &zp, cr)) 711 goto out; 712 713 /* The call to zfs_init_fs leaves the vnode held, release it here. */ 714 VN_RELE(ZTOV(zp)); 715 716 /* 717 * Set features for file system. 718 */ 719 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os); 720 if (zfsvfs->z_use_fuids) { 721 vfs_set_feature(vfsp, VFSFT_XVATTR); 722 vfs_set_feature(vfsp, VFSFT_ACEMASKONACCESS); 723 vfs_set_feature(vfsp, VFSFT_ACLONCREATE); 724 } 725 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 726 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS); 727 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE); 728 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE); 729 } else if (zfsvfs->z_case == ZFS_CASE_MIXED) { 730 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS); 731 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE); 732 } 733 734 if (dmu_objset_is_snapshot(zfsvfs->z_os)) { 735 uint64_t pval; 736 737 ASSERT(mode & DS_MODE_READONLY); 738 atime_changed_cb(zfsvfs, B_FALSE); 739 readonly_changed_cb(zfsvfs, B_TRUE); 740 if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL)) 741 goto out; 742 xattr_changed_cb(zfsvfs, pval); 743 zfsvfs->z_issnap = B_TRUE; 744 } else { 745 error = zfsvfs_setup(zfsvfs, B_TRUE); 746 } 747 748 if (!zfsvfs->z_issnap) 749 zfsctl_create(zfsvfs); 750 out: 751 if (error) { 752 if (zfsvfs->z_os) 753 dmu_objset_close(zfsvfs->z_os); 754 zfs_freezfsvfs(zfsvfs); 755 } else { 756 atomic_add_32(&zfs_active_fs_count, 1); 757 } 758 759 return (error); 760 } 761 762 void 763 zfs_unregister_callbacks(zfsvfs_t *zfsvfs) 764 { 765 objset_t *os = zfsvfs->z_os; 766 struct dsl_dataset *ds; 767 768 /* 769 * Unregister properties. 770 */ 771 if (!dmu_objset_is_snapshot(os)) { 772 ds = dmu_objset_ds(os); 773 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb, 774 zfsvfs) == 0); 775 776 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb, 777 zfsvfs) == 0); 778 779 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, 780 zfsvfs) == 0); 781 782 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb, 783 zfsvfs) == 0); 784 785 VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb, 786 zfsvfs) == 0); 787 788 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb, 789 zfsvfs) == 0); 790 791 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb, 792 zfsvfs) == 0); 793 794 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, 795 zfsvfs) == 0); 796 797 VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, 798 zfsvfs) == 0); 799 800 VERIFY(dsl_prop_unregister(ds, "aclinherit", 801 acl_inherit_changed_cb, zfsvfs) == 0); 802 803 VERIFY(dsl_prop_unregister(ds, "vscan", 804 vscan_changed_cb, zfsvfs) == 0); 805 } 806 } 807 808 /* 809 * Convert a decimal digit string to a uint64_t integer. 810 */ 811 static int 812 str_to_uint64(char *str, uint64_t *objnum) 813 { 814 uint64_t num = 0; 815 816 while (*str) { 817 if (*str < '0' || *str > '9') 818 return (EINVAL); 819 820 num = num*10 + *str++ - '0'; 821 } 822 823 *objnum = num; 824 return (0); 825 } 826 827 /* 828 * The boot path passed from the boot loader is in the form of 829 * "rootpool-name/root-filesystem-object-number'. Convert this 830 * string to a dataset name: "rootpool-name/root-filesystem-name". 831 */ 832 static int 833 parse_bootpath(char *bpath, char *outpath) 834 { 835 char *slashp; 836 uint64_t objnum; 837 int error; 838 839 if (*bpath == 0 || *bpath == '/') 840 return (EINVAL); 841 842 slashp = strchr(bpath, '/'); 843 844 /* if no '/', just return the pool name */ 845 if (slashp == NULL) { 846 (void) strcpy(outpath, bpath); 847 return (0); 848 } 849 850 if (error = str_to_uint64(slashp+1, &objnum)) 851 return (error); 852 853 *slashp = '\0'; 854 error = dsl_dsobj_to_dsname(bpath, objnum, outpath); 855 *slashp = '/'; 856 857 return (error); 858 } 859 860 static int 861 zfs_mountroot(vfs_t *vfsp, enum whymountroot why) 862 { 863 int error = 0; 864 int ret = 0; 865 static int zfsrootdone = 0; 866 zfsvfs_t *zfsvfs = NULL; 867 znode_t *zp = NULL; 868 vnode_t *vp = NULL; 869 char *zfs_bootpath; 870 #if defined(_OBP) 871 int proplen; 872 #endif 873 874 ASSERT(vfsp); 875 876 /* 877 * The filesystem that we mount as root is defined in the 878 * "zfs-bootfs" property. 879 */ 880 if (why == ROOT_INIT) { 881 if (zfsrootdone++) 882 return (EBUSY); 883 884 #if defined(_OBP) 885 proplen = BOP_GETPROPLEN(bootops, "zfs-bootfs"); 886 if (proplen == 0) 887 return (EIO); 888 zfs_bootpath = kmem_zalloc(proplen, KM_SLEEP); 889 if (BOP_GETPROP(bootops, "zfs-bootfs", zfs_bootpath) == -1) { 890 kmem_free(zfs_bootpath, proplen); 891 return (EIO); 892 } 893 error = parse_bootpath(zfs_bootpath, rootfs.bo_name); 894 kmem_free(zfs_bootpath, proplen); 895 #else 896 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(), 897 DDI_PROP_DONTPASS, "zfs-bootfs", &zfs_bootpath) != 898 DDI_SUCCESS) 899 return (EIO); 900 901 error = parse_bootpath(zfs_bootpath, rootfs.bo_name); 902 ddi_prop_free(zfs_bootpath); 903 #endif 904 905 if (error) 906 return (error); 907 908 if (error = vfs_lock(vfsp)) 909 return (error); 910 911 if (error = zfs_domount(vfsp, rootfs.bo_name, CRED())) 912 goto out; 913 914 zfsvfs = (zfsvfs_t *)vfsp->vfs_data; 915 ASSERT(zfsvfs); 916 if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) 917 goto out; 918 919 vp = ZTOV(zp); 920 mutex_enter(&vp->v_lock); 921 vp->v_flag |= VROOT; 922 mutex_exit(&vp->v_lock); 923 rootvp = vp; 924 925 /* 926 * The zfs_zget call above returns with a hold on vp, we release 927 * it here. 928 */ 929 VN_RELE(vp); 930 931 /* 932 * Mount root as readonly initially, it will be remouted 933 * read/write by /lib/svc/method/fs-usr. 934 */ 935 readonly_changed_cb(vfsp->vfs_data, B_TRUE); 936 vfs_add((struct vnode *)0, vfsp, 937 (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0); 938 out: 939 vfs_unlock(vfsp); 940 ret = (error) ? error : 0; 941 return (ret); 942 } else if (why == ROOT_REMOUNT) { 943 readonly_changed_cb(vfsp->vfs_data, B_FALSE); 944 vfsp->vfs_flag |= VFS_REMOUNT; 945 946 /* refresh mount options */ 947 zfs_unregister_callbacks(vfsp->vfs_data); 948 return (zfs_register_callbacks(vfsp)); 949 950 } else if (why == ROOT_UNMOUNT) { 951 zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data); 952 (void) zfs_sync(vfsp, 0, 0); 953 return (0); 954 } 955 956 /* 957 * if "why" is equal to anything else other than ROOT_INIT, 958 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it. 959 */ 960 return (ENOTSUP); 961 } 962 963 /*ARGSUSED*/ 964 static int 965 zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr) 966 { 967 char *osname; 968 pathname_t spn; 969 int error = 0; 970 uio_seg_t fromspace = (uap->flags & MS_SYSSPACE) ? 971 UIO_SYSSPACE : UIO_USERSPACE; 972 int canwrite; 973 974 if (mvp->v_type != VDIR) 975 return (ENOTDIR); 976 977 mutex_enter(&mvp->v_lock); 978 if ((uap->flags & MS_REMOUNT) == 0 && 979 (uap->flags & MS_OVERLAY) == 0 && 980 (mvp->v_count != 1 || (mvp->v_flag & VROOT))) { 981 mutex_exit(&mvp->v_lock); 982 return (EBUSY); 983 } 984 mutex_exit(&mvp->v_lock); 985 986 /* 987 * ZFS does not support passing unparsed data in via MS_DATA. 988 * Users should use the MS_OPTIONSTR interface; this means 989 * that all option parsing is already done and the options struct 990 * can be interrogated. 991 */ 992 if ((uap->flags & MS_DATA) && uap->datalen > 0) 993 return (EINVAL); 994 995 /* 996 * Get the objset name (the "special" mount argument). 997 */ 998 if (error = pn_get(uap->spec, fromspace, &spn)) 999 return (error); 1000 1001 osname = spn.pn_path; 1002 1003 /* 1004 * Check for mount privilege? 1005 * 1006 * If we don't have privilege then see if 1007 * we have local permission to allow it 1008 */ 1009 error = secpolicy_fs_mount(cr, mvp, vfsp); 1010 if (error) { 1011 error = dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr); 1012 if (error == 0) { 1013 vattr_t vattr; 1014 1015 /* 1016 * Make sure user is the owner of the mount point 1017 * or has sufficient privileges. 1018 */ 1019 1020 vattr.va_mask = AT_UID; 1021 1022 if (error = VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) { 1023 goto out; 1024 } 1025 1026 if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 && 1027 VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) { 1028 error = EPERM; 1029 goto out; 1030 } 1031 1032 secpolicy_fs_mount_clearopts(cr, vfsp); 1033 } else { 1034 goto out; 1035 } 1036 } 1037 1038 /* 1039 * Refuse to mount a filesystem if we are in a local zone and the 1040 * dataset is not visible. 1041 */ 1042 if (!INGLOBALZONE(curproc) && 1043 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) { 1044 error = EPERM; 1045 goto out; 1046 } 1047 1048 /* 1049 * When doing a remount, we simply refresh our temporary properties 1050 * according to those options set in the current VFS options. 1051 */ 1052 if (uap->flags & MS_REMOUNT) { 1053 /* refresh mount options */ 1054 zfs_unregister_callbacks(vfsp->vfs_data); 1055 error = zfs_register_callbacks(vfsp); 1056 goto out; 1057 } 1058 1059 error = zfs_domount(vfsp, osname, cr); 1060 1061 out: 1062 pn_free(&spn); 1063 return (error); 1064 } 1065 1066 static int 1067 zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp) 1068 { 1069 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1070 dev32_t d32; 1071 uint64_t refdbytes, availbytes, usedobjs, availobjs; 1072 1073 ZFS_ENTER(zfsvfs); 1074 1075 dmu_objset_space(zfsvfs->z_os, 1076 &refdbytes, &availbytes, &usedobjs, &availobjs); 1077 1078 /* 1079 * The underlying storage pool actually uses multiple block sizes. 1080 * We report the fragsize as the smallest block size we support, 1081 * and we report our blocksize as the filesystem's maximum blocksize. 1082 */ 1083 statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT; 1084 statp->f_bsize = zfsvfs->z_max_blksz; 1085 1086 /* 1087 * The following report "total" blocks of various kinds in the 1088 * file system, but reported in terms of f_frsize - the 1089 * "fragment" size. 1090 */ 1091 1092 statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT; 1093 statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT; 1094 statp->f_bavail = statp->f_bfree; /* no root reservation */ 1095 1096 /* 1097 * statvfs() should really be called statufs(), because it assumes 1098 * static metadata. ZFS doesn't preallocate files, so the best 1099 * we can do is report the max that could possibly fit in f_files, 1100 * and that minus the number actually used in f_ffree. 1101 * For f_ffree, report the smaller of the number of object available 1102 * and the number of blocks (each object will take at least a block). 1103 */ 1104 statp->f_ffree = MIN(availobjs, statp->f_bfree); 1105 statp->f_favail = statp->f_ffree; /* no "root reservation" */ 1106 statp->f_files = statp->f_ffree + usedobjs; 1107 1108 (void) cmpldev(&d32, vfsp->vfs_dev); 1109 statp->f_fsid = d32; 1110 1111 /* 1112 * We're a zfs filesystem. 1113 */ 1114 (void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name); 1115 1116 statp->f_flag = vf_to_stf(vfsp->vfs_flag); 1117 1118 statp->f_namemax = ZFS_MAXNAMELEN; 1119 1120 /* 1121 * We have all of 32 characters to stuff a string here. 1122 * Is there anything useful we could/should provide? 1123 */ 1124 bzero(statp->f_fstr, sizeof (statp->f_fstr)); 1125 1126 ZFS_EXIT(zfsvfs); 1127 return (0); 1128 } 1129 1130 static int 1131 zfs_root(vfs_t *vfsp, vnode_t **vpp) 1132 { 1133 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1134 znode_t *rootzp; 1135 int error; 1136 1137 ZFS_ENTER(zfsvfs); 1138 1139 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp); 1140 if (error == 0) 1141 *vpp = ZTOV(rootzp); 1142 1143 ZFS_EXIT(zfsvfs); 1144 return (error); 1145 } 1146 1147 /* 1148 * Teardown the zfsvfs::z_os. 1149 * 1150 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock' 1151 * and 'z_teardown_inactive_lock' held. 1152 */ 1153 static int 1154 zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting) 1155 { 1156 znode_t *zp; 1157 1158 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG); 1159 1160 if (!unmounting) { 1161 /* 1162 * We purge the parent filesystem's vfsp as the parent 1163 * filesystem and all of its snapshots have their vnode's 1164 * v_vfsp set to the parent's filesystem's vfsp. Note, 1165 * 'z_parent' is self referential for non-snapshots. 1166 */ 1167 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0); 1168 } 1169 1170 /* 1171 * Close the zil. NB: Can't close the zil while zfs_inactive 1172 * threads are blocked as zil_close can call zfs_inactive. 1173 */ 1174 if (zfsvfs->z_log) { 1175 zil_close(zfsvfs->z_log); 1176 zfsvfs->z_log = NULL; 1177 } 1178 1179 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER); 1180 1181 /* 1182 * If we are not unmounting (ie: online recv) and someone already 1183 * unmounted this file system while we were doing the switcheroo, 1184 * or a reopen of z_os failed then just bail out now. 1185 */ 1186 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) { 1187 rw_exit(&zfsvfs->z_teardown_inactive_lock); 1188 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 1189 return (EIO); 1190 } 1191 1192 /* 1193 * At this point there are no vops active, and any new vops will 1194 * fail with EIO since we have z_teardown_lock for writer (only 1195 * relavent for forced unmount). 1196 * 1197 * Release all holds on dbufs. 1198 */ 1199 mutex_enter(&zfsvfs->z_znodes_lock); 1200 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL; 1201 zp = list_next(&zfsvfs->z_all_znodes, zp)) 1202 if (zp->z_dbuf) { 1203 ASSERT(ZTOV(zp)->v_count > 0); 1204 zfs_znode_dmu_fini(zp); 1205 } 1206 mutex_exit(&zfsvfs->z_znodes_lock); 1207 1208 /* 1209 * If we are unmounting, set the unmounted flag and let new vops 1210 * unblock. zfs_inactive will have the unmounted behavior, and all 1211 * other vops will fail with EIO. 1212 */ 1213 if (unmounting) { 1214 zfsvfs->z_unmounted = B_TRUE; 1215 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 1216 rw_exit(&zfsvfs->z_teardown_inactive_lock); 1217 } 1218 1219 /* 1220 * z_os will be NULL if there was an error in attempting to reopen 1221 * zfsvfs, so just return as the properties had already been 1222 * unregistered and cached data had been evicted before. 1223 */ 1224 if (zfsvfs->z_os == NULL) 1225 return (0); 1226 1227 /* 1228 * Unregister properties. 1229 */ 1230 zfs_unregister_callbacks(zfsvfs); 1231 1232 /* 1233 * Evict cached data 1234 */ 1235 if (dmu_objset_evict_dbufs(zfsvfs->z_os)) { 1236 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0); 1237 (void) dmu_objset_evict_dbufs(zfsvfs->z_os); 1238 } 1239 1240 return (0); 1241 } 1242 1243 /*ARGSUSED*/ 1244 static int 1245 zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr) 1246 { 1247 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1248 objset_t *os; 1249 int ret; 1250 1251 ret = secpolicy_fs_unmount(cr, vfsp); 1252 if (ret) { 1253 ret = dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource), 1254 ZFS_DELEG_PERM_MOUNT, cr); 1255 if (ret) 1256 return (ret); 1257 } 1258 1259 /* 1260 * We purge the parent filesystem's vfsp as the parent filesystem 1261 * and all of its snapshots have their vnode's v_vfsp set to the 1262 * parent's filesystem's vfsp. Note, 'z_parent' is self 1263 * referential for non-snapshots. 1264 */ 1265 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0); 1266 1267 /* 1268 * Unmount any snapshots mounted under .zfs before unmounting the 1269 * dataset itself. 1270 */ 1271 if (zfsvfs->z_ctldir != NULL && 1272 (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) { 1273 return (ret); 1274 } 1275 1276 if (!(fflag & MS_FORCE)) { 1277 /* 1278 * Check the number of active vnodes in the file system. 1279 * Our count is maintained in the vfs structure, but the 1280 * number is off by 1 to indicate a hold on the vfs 1281 * structure itself. 1282 * 1283 * The '.zfs' directory maintains a reference of its 1284 * own, and any active references underneath are 1285 * reflected in the vnode count. 1286 */ 1287 if (zfsvfs->z_ctldir == NULL) { 1288 if (vfsp->vfs_count > 1) 1289 return (EBUSY); 1290 } else { 1291 if (vfsp->vfs_count > 2 || 1292 zfsvfs->z_ctldir->v_count > 1) 1293 return (EBUSY); 1294 } 1295 } 1296 1297 vfsp->vfs_flag |= VFS_UNMOUNTED; 1298 1299 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0); 1300 os = zfsvfs->z_os; 1301 1302 /* 1303 * z_os will be NULL if there was an error in 1304 * attempting to reopen zfsvfs. 1305 */ 1306 if (os != NULL) { 1307 /* 1308 * Unset the objset user_ptr. 1309 */ 1310 mutex_enter(&os->os->os_user_ptr_lock); 1311 dmu_objset_set_user(os, NULL); 1312 mutex_exit(&os->os->os_user_ptr_lock); 1313 1314 /* 1315 * Finally close the objset 1316 */ 1317 dmu_objset_close(os); 1318 } 1319 1320 /* 1321 * We can now safely destroy the '.zfs' directory node. 1322 */ 1323 if (zfsvfs->z_ctldir != NULL) 1324 zfsctl_destroy(zfsvfs); 1325 1326 return (0); 1327 } 1328 1329 static int 1330 zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp) 1331 { 1332 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1333 znode_t *zp; 1334 uint64_t object = 0; 1335 uint64_t fid_gen = 0; 1336 uint64_t gen_mask; 1337 uint64_t zp_gen; 1338 int i, err; 1339 1340 *vpp = NULL; 1341 1342 ZFS_ENTER(zfsvfs); 1343 1344 if (fidp->fid_len == LONG_FID_LEN) { 1345 zfid_long_t *zlfid = (zfid_long_t *)fidp; 1346 uint64_t objsetid = 0; 1347 uint64_t setgen = 0; 1348 1349 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 1350 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i); 1351 1352 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 1353 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i); 1354 1355 ZFS_EXIT(zfsvfs); 1356 1357 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs); 1358 if (err) 1359 return (EINVAL); 1360 ZFS_ENTER(zfsvfs); 1361 } 1362 1363 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) { 1364 zfid_short_t *zfid = (zfid_short_t *)fidp; 1365 1366 for (i = 0; i < sizeof (zfid->zf_object); i++) 1367 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i); 1368 1369 for (i = 0; i < sizeof (zfid->zf_gen); i++) 1370 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i); 1371 } else { 1372 ZFS_EXIT(zfsvfs); 1373 return (EINVAL); 1374 } 1375 1376 /* A zero fid_gen means we are in the .zfs control directories */ 1377 if (fid_gen == 0 && 1378 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) { 1379 *vpp = zfsvfs->z_ctldir; 1380 ASSERT(*vpp != NULL); 1381 if (object == ZFSCTL_INO_SNAPDIR) { 1382 VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL, 1383 0, NULL, NULL, NULL, NULL, NULL) == 0); 1384 } else { 1385 VN_HOLD(*vpp); 1386 } 1387 ZFS_EXIT(zfsvfs); 1388 return (0); 1389 } 1390 1391 gen_mask = -1ULL >> (64 - 8 * i); 1392 1393 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask); 1394 if (err = zfs_zget(zfsvfs, object, &zp)) { 1395 ZFS_EXIT(zfsvfs); 1396 return (err); 1397 } 1398 zp_gen = zp->z_phys->zp_gen & gen_mask; 1399 if (zp_gen == 0) 1400 zp_gen = 1; 1401 if (zp->z_unlinked || zp_gen != fid_gen) { 1402 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen); 1403 VN_RELE(ZTOV(zp)); 1404 ZFS_EXIT(zfsvfs); 1405 return (EINVAL); 1406 } 1407 1408 *vpp = ZTOV(zp); 1409 ZFS_EXIT(zfsvfs); 1410 return (0); 1411 } 1412 1413 /* 1414 * Block out VOPs and close zfsvfs_t::z_os 1415 * 1416 * Note, if successful, then we return with the 'z_teardown_lock' and 1417 * 'z_teardown_inactive_lock' write held. 1418 */ 1419 int 1420 zfs_suspend_fs(zfsvfs_t *zfsvfs, char *name, int *mode) 1421 { 1422 int error; 1423 1424 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0) 1425 return (error); 1426 1427 *mode = zfsvfs->z_os->os_mode; 1428 dmu_objset_name(zfsvfs->z_os, name); 1429 dmu_objset_close(zfsvfs->z_os); 1430 1431 return (0); 1432 } 1433 1434 /* 1435 * Reopen zfsvfs_t::z_os and release VOPs. 1436 */ 1437 int 1438 zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname, int mode) 1439 { 1440 int err; 1441 1442 ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock)); 1443 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)); 1444 1445 err = dmu_objset_open(osname, DMU_OST_ZFS, mode, &zfsvfs->z_os); 1446 if (err) { 1447 zfsvfs->z_os = NULL; 1448 } else { 1449 znode_t *zp; 1450 1451 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0); 1452 1453 /* 1454 * Attempt to re-establish all the active znodes with 1455 * their dbufs. If a zfs_rezget() fails, then we'll let 1456 * any potential callers discover that via ZFS_ENTER_VERIFY_VP 1457 * when they try to use their znode. 1458 */ 1459 mutex_enter(&zfsvfs->z_znodes_lock); 1460 for (zp = list_head(&zfsvfs->z_all_znodes); zp; 1461 zp = list_next(&zfsvfs->z_all_znodes, zp)) { 1462 (void) zfs_rezget(zp); 1463 } 1464 mutex_exit(&zfsvfs->z_znodes_lock); 1465 1466 } 1467 1468 /* release the VOPs */ 1469 rw_exit(&zfsvfs->z_teardown_inactive_lock); 1470 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 1471 1472 if (err) { 1473 /* 1474 * Since we couldn't reopen zfsvfs::z_os, force 1475 * unmount this file system. 1476 */ 1477 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0) 1478 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED()); 1479 } 1480 return (err); 1481 } 1482 1483 static void 1484 zfs_freevfs(vfs_t *vfsp) 1485 { 1486 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1487 int i; 1488 1489 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 1490 mutex_destroy(&zfsvfs->z_hold_mtx[i]); 1491 1492 zfs_fuid_destroy(zfsvfs); 1493 zfs_freezfsvfs(zfsvfs); 1494 1495 atomic_add_32(&zfs_active_fs_count, -1); 1496 } 1497 1498 /* 1499 * VFS_INIT() initialization. Note that there is no VFS_FINI(), 1500 * so we can't safely do any non-idempotent initialization here. 1501 * Leave that to zfs_init() and zfs_fini(), which are called 1502 * from the module's _init() and _fini() entry points. 1503 */ 1504 /*ARGSUSED*/ 1505 static int 1506 zfs_vfsinit(int fstype, char *name) 1507 { 1508 int error; 1509 1510 zfsfstype = fstype; 1511 1512 /* 1513 * Setup vfsops and vnodeops tables. 1514 */ 1515 error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops); 1516 if (error != 0) { 1517 cmn_err(CE_WARN, "zfs: bad vfs ops template"); 1518 } 1519 1520 error = zfs_create_op_tables(); 1521 if (error) { 1522 zfs_remove_op_tables(); 1523 cmn_err(CE_WARN, "zfs: bad vnode ops template"); 1524 (void) vfs_freevfsops_by_type(zfsfstype); 1525 return (error); 1526 } 1527 1528 mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL); 1529 1530 /* 1531 * Unique major number for all zfs mounts. 1532 * If we run out of 32-bit minors, we'll getudev() another major. 1533 */ 1534 zfs_major = ddi_name_to_major(ZFS_DRIVER); 1535 zfs_minor = ZFS_MIN_MINOR; 1536 1537 return (0); 1538 } 1539 1540 void 1541 zfs_init(void) 1542 { 1543 /* 1544 * Initialize .zfs directory structures 1545 */ 1546 zfsctl_init(); 1547 1548 /* 1549 * Initialize znode cache, vnode ops, etc... 1550 */ 1551 zfs_znode_init(); 1552 } 1553 1554 void 1555 zfs_fini(void) 1556 { 1557 zfsctl_fini(); 1558 zfs_znode_fini(); 1559 } 1560 1561 int 1562 zfs_busy(void) 1563 { 1564 return (zfs_active_fs_count != 0); 1565 } 1566 1567 int 1568 zfs_set_version(const char *name, uint64_t newvers) 1569 { 1570 int error; 1571 objset_t *os; 1572 dmu_tx_t *tx; 1573 uint64_t curvers; 1574 1575 /* 1576 * XXX for now, require that the filesystem be unmounted. Would 1577 * be nice to find the zfsvfs_t and just update that if 1578 * possible. 1579 */ 1580 1581 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION) 1582 return (EINVAL); 1583 1584 error = dmu_objset_open(name, DMU_OST_ZFS, DS_MODE_PRIMARY, &os); 1585 if (error) 1586 return (error); 1587 1588 error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 1589 8, 1, &curvers); 1590 if (error) 1591 goto out; 1592 if (newvers < curvers) { 1593 error = EINVAL; 1594 goto out; 1595 } 1596 1597 tx = dmu_tx_create(os); 1598 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 0, ZPL_VERSION_STR); 1599 error = dmu_tx_assign(tx, TXG_WAIT); 1600 if (error) { 1601 dmu_tx_abort(tx); 1602 goto out; 1603 } 1604 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 8, 1, 1605 &newvers, tx); 1606 1607 spa_history_internal_log(LOG_DS_UPGRADE, 1608 dmu_objset_spa(os), tx, CRED(), 1609 "oldver=%llu newver=%llu dataset = %llu", curvers, newvers, 1610 dmu_objset_id(os)); 1611 dmu_tx_commit(tx); 1612 1613 out: 1614 dmu_objset_close(os); 1615 return (error); 1616 } 1617 1618 /* 1619 * Read a property stored within the master node. 1620 */ 1621 int 1622 zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value) 1623 { 1624 const char *pname; 1625 int error; 1626 1627 /* 1628 * Look up the file system's value for the property. For the 1629 * version property, we look up a slightly different string. 1630 * Also, there is no default VERSION value, so if we don't 1631 * find it, return the error. 1632 */ 1633 if (prop == ZFS_PROP_VERSION) 1634 pname = ZPL_VERSION_STR; 1635 else 1636 pname = zfs_prop_to_name(prop); 1637 1638 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value); 1639 1640 if (!error) { 1641 return (0); 1642 } else if (prop == ZFS_PROP_VERSION || error != ENOENT) { 1643 return (error); 1644 } else { 1645 /* No value set, use the default value */ 1646 switch (prop) { 1647 case ZFS_PROP_NORMALIZE: 1648 case ZFS_PROP_UTF8ONLY: 1649 *value = 0; 1650 break; 1651 case ZFS_PROP_CASE: 1652 *value = ZFS_CASE_SENSITIVE; 1653 break; 1654 default: 1655 return (ENOENT); 1656 } 1657 } 1658 return (0); 1659 } 1660 1661 static vfsdef_t vfw = { 1662 VFSDEF_VERSION, 1663 MNTTYPE_ZFS, 1664 zfs_vfsinit, 1665 VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS| 1666 VSW_XID, 1667 &zfs_mntopts 1668 }; 1669 1670 struct modlfs zfs_modlfs = { 1671 &mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw 1672 }; 1673