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