zfs_vfsops.c revision 248369
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>. 24 * All rights reserved. 25 */ 26 27/* Portions Copyright 2010 Robert Milkowski */ 28 29#include <sys/types.h> 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/kernel.h> 33#include <sys/sysmacros.h> 34#include <sys/kmem.h> 35#include <sys/acl.h> 36#include <sys/vnode.h> 37#include <sys/vfs.h> 38#include <sys/mntent.h> 39#include <sys/mount.h> 40#include <sys/cmn_err.h> 41#include <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/sa.h> 52#include <sys/sa_impl.h> 53#include <sys/varargs.h> 54#include <sys/policy.h> 55#include <sys/atomic.h> 56#include <sys/zfs_ioctl.h> 57#include <sys/zfs_ctldir.h> 58#include <sys/zfs_fuid.h> 59#include <sys/sunddi.h> 60#include <sys/dnlc.h> 61#include <sys/dmu_objset.h> 62#include <sys/spa_boot.h> 63#include <sys/jail.h> 64#include "zfs_comutil.h" 65 66struct mtx zfs_debug_mtx; 67MTX_SYSINIT(zfs_debug_mtx, &zfs_debug_mtx, "zfs_debug", MTX_DEF); 68 69SYSCTL_NODE(_vfs, OID_AUTO, zfs, CTLFLAG_RW, 0, "ZFS file system"); 70 71int zfs_super_owner; 72SYSCTL_INT(_vfs_zfs, OID_AUTO, super_owner, CTLFLAG_RW, &zfs_super_owner, 0, 73 "File system owner can perform privileged operation on his file systems"); 74 75int zfs_debug_level; 76TUNABLE_INT("vfs.zfs.debug", &zfs_debug_level); 77SYSCTL_INT(_vfs_zfs, OID_AUTO, debug, CTLFLAG_RW, &zfs_debug_level, 0, 78 "Debug level"); 79 80SYSCTL_NODE(_vfs_zfs, OID_AUTO, version, CTLFLAG_RD, 0, "ZFS versions"); 81static int zfs_version_acl = ZFS_ACL_VERSION; 82SYSCTL_INT(_vfs_zfs_version, OID_AUTO, acl, CTLFLAG_RD, &zfs_version_acl, 0, 83 "ZFS_ACL_VERSION"); 84static int zfs_version_spa = SPA_VERSION; 85SYSCTL_INT(_vfs_zfs_version, OID_AUTO, spa, CTLFLAG_RD, &zfs_version_spa, 0, 86 "SPA_VERSION"); 87static int zfs_version_zpl = ZPL_VERSION; 88SYSCTL_INT(_vfs_zfs_version, OID_AUTO, zpl, CTLFLAG_RD, &zfs_version_zpl, 0, 89 "ZPL_VERSION"); 90 91static int zfs_mount(vfs_t *vfsp); 92static int zfs_umount(vfs_t *vfsp, int fflag); 93static int zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp); 94static int zfs_statfs(vfs_t *vfsp, struct statfs *statp); 95static int zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp); 96static int zfs_sync(vfs_t *vfsp, int waitfor); 97static int zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp, 98 struct ucred **credanonp, int *numsecflavors, int **secflavors); 99static int zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, int flags, vnode_t **vpp); 100static void zfs_objset_close(zfsvfs_t *zfsvfs); 101static void zfs_freevfs(vfs_t *vfsp); 102 103static struct vfsops zfs_vfsops = { 104 .vfs_mount = zfs_mount, 105 .vfs_unmount = zfs_umount, 106 .vfs_root = zfs_root, 107 .vfs_statfs = zfs_statfs, 108 .vfs_vget = zfs_vget, 109 .vfs_sync = zfs_sync, 110 .vfs_checkexp = zfs_checkexp, 111 .vfs_fhtovp = zfs_fhtovp, 112}; 113 114VFS_SET(zfs_vfsops, zfs, VFCF_JAIL | VFCF_DELEGADMIN); 115 116/* 117 * We need to keep a count of active fs's. 118 * This is necessary to prevent our module 119 * from being unloaded after a umount -f 120 */ 121static uint32_t zfs_active_fs_count = 0; 122 123/*ARGSUSED*/ 124static int 125zfs_sync(vfs_t *vfsp, int waitfor) 126{ 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 if (vfsp != NULL) { 136 /* 137 * Sync a specific filesystem. 138 */ 139 zfsvfs_t *zfsvfs = vfsp->vfs_data; 140 dsl_pool_t *dp; 141 int error; 142 143 error = vfs_stdsync(vfsp, waitfor); 144 if (error != 0) 145 return (error); 146 147 ZFS_ENTER(zfsvfs); 148 dp = dmu_objset_pool(zfsvfs->z_os); 149 150 /* 151 * If the system is shutting down, then skip any 152 * filesystems which may exist on a suspended pool. 153 */ 154 if (sys_shutdown && spa_suspended(dp->dp_spa)) { 155 ZFS_EXIT(zfsvfs); 156 return (0); 157 } 158 159 if (zfsvfs->z_log != NULL) 160 zil_commit(zfsvfs->z_log, 0); 161 162 ZFS_EXIT(zfsvfs); 163 } else { 164 /* 165 * Sync all ZFS filesystems. This is what happens when you 166 * run sync(1M). Unlike other filesystems, ZFS honors the 167 * request by waiting for all pools to commit all dirty data. 168 */ 169 spa_sync_allpools(); 170 } 171 172 return (0); 173} 174 175#ifndef __FreeBSD__ 176static int 177zfs_create_unique_device(dev_t *dev) 178{ 179 major_t new_major; 180 181 do { 182 ASSERT3U(zfs_minor, <=, MAXMIN32); 183 minor_t start = zfs_minor; 184 do { 185 mutex_enter(&zfs_dev_mtx); 186 if (zfs_minor >= MAXMIN32) { 187 /* 188 * If we're still using the real major 189 * keep out of /dev/zfs and /dev/zvol minor 190 * number space. If we're using a getudev()'ed 191 * major number, we can use all of its minors. 192 */ 193 if (zfs_major == ddi_name_to_major(ZFS_DRIVER)) 194 zfs_minor = ZFS_MIN_MINOR; 195 else 196 zfs_minor = 0; 197 } else { 198 zfs_minor++; 199 } 200 *dev = makedevice(zfs_major, zfs_minor); 201 mutex_exit(&zfs_dev_mtx); 202 } while (vfs_devismounted(*dev) && zfs_minor != start); 203 if (zfs_minor == start) { 204 /* 205 * We are using all ~262,000 minor numbers for the 206 * current major number. Create a new major number. 207 */ 208 if ((new_major = getudev()) == (major_t)-1) { 209 cmn_err(CE_WARN, 210 "zfs_mount: Can't get unique major " 211 "device number."); 212 return (-1); 213 } 214 mutex_enter(&zfs_dev_mtx); 215 zfs_major = new_major; 216 zfs_minor = 0; 217 218 mutex_exit(&zfs_dev_mtx); 219 } else { 220 break; 221 } 222 /* CONSTANTCONDITION */ 223 } while (1); 224 225 return (0); 226} 227#endif /* !__FreeBSD__ */ 228 229static void 230atime_changed_cb(void *arg, uint64_t newval) 231{ 232 zfsvfs_t *zfsvfs = arg; 233 234 if (newval == TRUE) { 235 zfsvfs->z_atime = TRUE; 236 zfsvfs->z_vfs->vfs_flag &= ~MNT_NOATIME; 237 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME); 238 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0); 239 } else { 240 zfsvfs->z_atime = FALSE; 241 zfsvfs->z_vfs->vfs_flag |= MNT_NOATIME; 242 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME); 243 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0); 244 } 245} 246 247static void 248xattr_changed_cb(void *arg, uint64_t newval) 249{ 250 zfsvfs_t *zfsvfs = arg; 251 252 if (newval == TRUE) { 253 /* XXX locking on vfs_flag? */ 254#ifdef TODO 255 zfsvfs->z_vfs->vfs_flag |= VFS_XATTR; 256#endif 257 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR); 258 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0); 259 } else { 260 /* XXX locking on vfs_flag? */ 261#ifdef TODO 262 zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR; 263#endif 264 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR); 265 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0); 266 } 267} 268 269static void 270blksz_changed_cb(void *arg, uint64_t newval) 271{ 272 zfsvfs_t *zfsvfs = arg; 273 274 if (newval < SPA_MINBLOCKSIZE || 275 newval > SPA_MAXBLOCKSIZE || !ISP2(newval)) 276 newval = SPA_MAXBLOCKSIZE; 277 278 zfsvfs->z_max_blksz = newval; 279 zfsvfs->z_vfs->mnt_stat.f_iosize = newval; 280} 281 282static void 283readonly_changed_cb(void *arg, uint64_t newval) 284{ 285 zfsvfs_t *zfsvfs = arg; 286 287 if (newval) { 288 /* XXX locking on vfs_flag? */ 289 zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY; 290 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW); 291 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0); 292 } else { 293 /* XXX locking on vfs_flag? */ 294 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY; 295 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO); 296 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0); 297 } 298} 299 300static void 301setuid_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 316static void 317exec_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/* 333 * The nbmand mount option can be changed at mount time. 334 * We can't allow it to be toggled on live file systems or incorrect 335 * behavior may be seen from cifs clients 336 * 337 * This property isn't registered via dsl_prop_register(), but this callback 338 * will be called when a file system is first mounted 339 */ 340static void 341nbmand_changed_cb(void *arg, uint64_t newval) 342{ 343 zfsvfs_t *zfsvfs = arg; 344 if (newval == FALSE) { 345 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND); 346 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0); 347 } else { 348 vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND); 349 vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0); 350 } 351} 352 353static void 354snapdir_changed_cb(void *arg, uint64_t newval) 355{ 356 zfsvfs_t *zfsvfs = arg; 357 358 zfsvfs->z_show_ctldir = newval; 359} 360 361static void 362vscan_changed_cb(void *arg, uint64_t newval) 363{ 364 zfsvfs_t *zfsvfs = arg; 365 366 zfsvfs->z_vscan = newval; 367} 368 369static void 370acl_mode_changed_cb(void *arg, uint64_t newval) 371{ 372 zfsvfs_t *zfsvfs = arg; 373 374 zfsvfs->z_acl_mode = newval; 375} 376 377static void 378acl_inherit_changed_cb(void *arg, uint64_t newval) 379{ 380 zfsvfs_t *zfsvfs = arg; 381 382 zfsvfs->z_acl_inherit = newval; 383} 384 385static int 386zfs_register_callbacks(vfs_t *vfsp) 387{ 388 struct dsl_dataset *ds = NULL; 389 objset_t *os = NULL; 390 zfsvfs_t *zfsvfs = NULL; 391 uint64_t nbmand; 392 boolean_t readonly = B_FALSE; 393 boolean_t do_readonly = B_FALSE; 394 boolean_t setuid = B_FALSE; 395 boolean_t do_setuid = B_FALSE; 396 boolean_t exec = B_FALSE; 397 boolean_t do_exec = B_FALSE; 398 boolean_t devices = B_FALSE; 399 boolean_t do_devices = B_FALSE; 400 boolean_t xattr = B_FALSE; 401 boolean_t do_xattr = B_FALSE; 402 boolean_t atime = B_FALSE; 403 boolean_t do_atime = B_FALSE; 404 int error = 0; 405 406 ASSERT(vfsp); 407 zfsvfs = vfsp->vfs_data; 408 ASSERT(zfsvfs); 409 os = zfsvfs->z_os; 410 411 /* 412 * This function can be called for a snapshot when we update snapshot's 413 * mount point, which isn't really supported. 414 */ 415 if (dmu_objset_is_snapshot(os)) 416 return (EOPNOTSUPP); 417 418 /* 419 * The act of registering our callbacks will destroy any mount 420 * options we may have. In order to enable temporary overrides 421 * of mount options, we stash away the current values and 422 * restore them after we register the callbacks. 423 */ 424 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) || 425 !spa_writeable(dmu_objset_spa(os))) { 426 readonly = B_TRUE; 427 do_readonly = B_TRUE; 428 } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) { 429 readonly = B_FALSE; 430 do_readonly = B_TRUE; 431 } 432 if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) { 433 setuid = B_FALSE; 434 do_setuid = B_TRUE; 435 } else { 436 if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) { 437 setuid = B_FALSE; 438 do_setuid = B_TRUE; 439 } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) { 440 setuid = B_TRUE; 441 do_setuid = B_TRUE; 442 } 443 } 444 if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) { 445 exec = B_FALSE; 446 do_exec = B_TRUE; 447 } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) { 448 exec = B_TRUE; 449 do_exec = B_TRUE; 450 } 451 if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) { 452 xattr = B_FALSE; 453 do_xattr = B_TRUE; 454 } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) { 455 xattr = B_TRUE; 456 do_xattr = B_TRUE; 457 } 458 if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) { 459 atime = B_FALSE; 460 do_atime = B_TRUE; 461 } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) { 462 atime = B_TRUE; 463 do_atime = B_TRUE; 464 } 465 466 /* 467 * nbmand is a special property. It can only be changed at 468 * mount time. 469 * 470 * This is weird, but it is documented to only be changeable 471 * at mount time. 472 */ 473 if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) { 474 nbmand = B_FALSE; 475 } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) { 476 nbmand = B_TRUE; 477 } else { 478 char osname[MAXNAMELEN]; 479 480 dmu_objset_name(os, osname); 481 if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand, 482 NULL)) { 483 return (error); 484 } 485 } 486 487 /* 488 * Register property callbacks. 489 * 490 * It would probably be fine to just check for i/o error from 491 * the first prop_register(), but I guess I like to go 492 * overboard... 493 */ 494 ds = dmu_objset_ds(os); 495 error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs); 496 error = error ? error : dsl_prop_register(ds, 497 "xattr", xattr_changed_cb, zfsvfs); 498 error = error ? error : dsl_prop_register(ds, 499 "recordsize", blksz_changed_cb, zfsvfs); 500 error = error ? error : dsl_prop_register(ds, 501 "readonly", readonly_changed_cb, zfsvfs); 502 error = error ? error : dsl_prop_register(ds, 503 "setuid", setuid_changed_cb, zfsvfs); 504 error = error ? error : dsl_prop_register(ds, 505 "exec", exec_changed_cb, zfsvfs); 506 error = error ? error : dsl_prop_register(ds, 507 "snapdir", snapdir_changed_cb, zfsvfs); 508 error = error ? error : dsl_prop_register(ds, 509 "aclmode", acl_mode_changed_cb, zfsvfs); 510 error = error ? error : dsl_prop_register(ds, 511 "aclinherit", acl_inherit_changed_cb, zfsvfs); 512 error = error ? error : dsl_prop_register(ds, 513 "vscan", vscan_changed_cb, zfsvfs); 514 if (error) 515 goto unregister; 516 517 /* 518 * Invoke our callbacks to restore temporary mount options. 519 */ 520 if (do_readonly) 521 readonly_changed_cb(zfsvfs, readonly); 522 if (do_setuid) 523 setuid_changed_cb(zfsvfs, setuid); 524 if (do_exec) 525 exec_changed_cb(zfsvfs, exec); 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 535unregister: 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, "setuid", setuid_changed_cb, zfsvfs); 546 (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs); 547 (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs); 548 (void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs); 549 (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb, 550 zfsvfs); 551 (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs); 552 return (error); 553 554} 555 556static int 557zfs_space_delta_cb(dmu_object_type_t bonustype, void *data, 558 uint64_t *userp, uint64_t *groupp) 559{ 560 int error = 0; 561 562 /* 563 * Is it a valid type of object to track? 564 */ 565 if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA) 566 return (ENOENT); 567 568 /* 569 * If we have a NULL data pointer 570 * then assume the id's aren't changing and 571 * return EEXIST to the dmu to let it know to 572 * use the same ids 573 */ 574 if (data == NULL) 575 return (EEXIST); 576 577 if (bonustype == DMU_OT_ZNODE) { 578 znode_phys_t *znp = data; 579 *userp = znp->zp_uid; 580 *groupp = znp->zp_gid; 581 } else { 582 int hdrsize; 583 sa_hdr_phys_t *sap = data; 584 sa_hdr_phys_t sa = *sap; 585 boolean_t swap = B_FALSE; 586 587 ASSERT(bonustype == DMU_OT_SA); 588 589 if (sa.sa_magic == 0) { 590 /* 591 * This should only happen for newly created 592 * files that haven't had the znode data filled 593 * in yet. 594 */ 595 *userp = 0; 596 *groupp = 0; 597 return (0); 598 } 599 if (sa.sa_magic == BSWAP_32(SA_MAGIC)) { 600 sa.sa_magic = SA_MAGIC; 601 sa.sa_layout_info = BSWAP_16(sa.sa_layout_info); 602 swap = B_TRUE; 603 } else { 604 VERIFY3U(sa.sa_magic, ==, SA_MAGIC); 605 } 606 607 hdrsize = sa_hdrsize(&sa); 608 VERIFY3U(hdrsize, >=, sizeof (sa_hdr_phys_t)); 609 *userp = *((uint64_t *)((uintptr_t)data + hdrsize + 610 SA_UID_OFFSET)); 611 *groupp = *((uint64_t *)((uintptr_t)data + hdrsize + 612 SA_GID_OFFSET)); 613 if (swap) { 614 *userp = BSWAP_64(*userp); 615 *groupp = BSWAP_64(*groupp); 616 } 617 } 618 return (error); 619} 620 621static void 622fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr, 623 char *domainbuf, int buflen, uid_t *ridp) 624{ 625 uint64_t fuid; 626 const char *domain; 627 628 fuid = strtonum(fuidstr, NULL); 629 630 domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid)); 631 if (domain) 632 (void) strlcpy(domainbuf, domain, buflen); 633 else 634 domainbuf[0] = '\0'; 635 *ridp = FUID_RID(fuid); 636} 637 638static uint64_t 639zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type) 640{ 641 switch (type) { 642 case ZFS_PROP_USERUSED: 643 return (DMU_USERUSED_OBJECT); 644 case ZFS_PROP_GROUPUSED: 645 return (DMU_GROUPUSED_OBJECT); 646 case ZFS_PROP_USERQUOTA: 647 return (zfsvfs->z_userquota_obj); 648 case ZFS_PROP_GROUPQUOTA: 649 return (zfsvfs->z_groupquota_obj); 650 } 651 return (0); 652} 653 654int 655zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type, 656 uint64_t *cookiep, void *vbuf, uint64_t *bufsizep) 657{ 658 int error; 659 zap_cursor_t zc; 660 zap_attribute_t za; 661 zfs_useracct_t *buf = vbuf; 662 uint64_t obj; 663 664 if (!dmu_objset_userspace_present(zfsvfs->z_os)) 665 return (ENOTSUP); 666 667 obj = zfs_userquota_prop_to_obj(zfsvfs, type); 668 if (obj == 0) { 669 *bufsizep = 0; 670 return (0); 671 } 672 673 for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep); 674 (error = zap_cursor_retrieve(&zc, &za)) == 0; 675 zap_cursor_advance(&zc)) { 676 if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) > 677 *bufsizep) 678 break; 679 680 fuidstr_to_sid(zfsvfs, za.za_name, 681 buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid); 682 683 buf->zu_space = za.za_first_integer; 684 buf++; 685 } 686 if (error == ENOENT) 687 error = 0; 688 689 ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep); 690 *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf; 691 *cookiep = zap_cursor_serialize(&zc); 692 zap_cursor_fini(&zc); 693 return (error); 694} 695 696/* 697 * buf must be big enough (eg, 32 bytes) 698 */ 699static int 700id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid, 701 char *buf, boolean_t addok) 702{ 703 uint64_t fuid; 704 int domainid = 0; 705 706 if (domain && domain[0]) { 707 domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok); 708 if (domainid == -1) 709 return (ENOENT); 710 } 711 fuid = FUID_ENCODE(domainid, rid); 712 (void) sprintf(buf, "%llx", (longlong_t)fuid); 713 return (0); 714} 715 716int 717zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type, 718 const char *domain, uint64_t rid, uint64_t *valp) 719{ 720 char buf[32]; 721 int err; 722 uint64_t obj; 723 724 *valp = 0; 725 726 if (!dmu_objset_userspace_present(zfsvfs->z_os)) 727 return (ENOTSUP); 728 729 obj = zfs_userquota_prop_to_obj(zfsvfs, type); 730 if (obj == 0) 731 return (0); 732 733 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE); 734 if (err) 735 return (err); 736 737 err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp); 738 if (err == ENOENT) 739 err = 0; 740 return (err); 741} 742 743int 744zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type, 745 const char *domain, uint64_t rid, uint64_t quota) 746{ 747 char buf[32]; 748 int err; 749 dmu_tx_t *tx; 750 uint64_t *objp; 751 boolean_t fuid_dirtied; 752 753 if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA) 754 return (EINVAL); 755 756 if (zfsvfs->z_version < ZPL_VERSION_USERSPACE) 757 return (ENOTSUP); 758 759 objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj : 760 &zfsvfs->z_groupquota_obj; 761 762 err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE); 763 if (err) 764 return (err); 765 fuid_dirtied = zfsvfs->z_fuid_dirty; 766 767 tx = dmu_tx_create(zfsvfs->z_os); 768 dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL); 769 if (*objp == 0) { 770 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE, 771 zfs_userquota_prop_prefixes[type]); 772 } 773 if (fuid_dirtied) 774 zfs_fuid_txhold(zfsvfs, tx); 775 err = dmu_tx_assign(tx, TXG_WAIT); 776 if (err) { 777 dmu_tx_abort(tx); 778 return (err); 779 } 780 781 mutex_enter(&zfsvfs->z_lock); 782 if (*objp == 0) { 783 *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA, 784 DMU_OT_NONE, 0, tx); 785 VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ, 786 zfs_userquota_prop_prefixes[type], 8, 1, objp, tx)); 787 } 788 mutex_exit(&zfsvfs->z_lock); 789 790 if (quota == 0) { 791 err = zap_remove(zfsvfs->z_os, *objp, buf, tx); 792 if (err == ENOENT) 793 err = 0; 794 } else { 795 err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, "a, tx); 796 } 797 ASSERT(err == 0); 798 if (fuid_dirtied) 799 zfs_fuid_sync(zfsvfs, tx); 800 dmu_tx_commit(tx); 801 return (err); 802} 803 804boolean_t 805zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid) 806{ 807 char buf[32]; 808 uint64_t used, quota, usedobj, quotaobj; 809 int err; 810 811 usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT; 812 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj; 813 814 if (quotaobj == 0 || zfsvfs->z_replay) 815 return (B_FALSE); 816 817 (void) sprintf(buf, "%llx", (longlong_t)fuid); 818 err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, "a); 819 if (err != 0) 820 return (B_FALSE); 821 822 err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used); 823 if (err != 0) 824 return (B_FALSE); 825 return (used >= quota); 826} 827 828boolean_t 829zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup) 830{ 831 uint64_t fuid; 832 uint64_t quotaobj; 833 834 quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj; 835 836 fuid = isgroup ? zp->z_gid : zp->z_uid; 837 838 if (quotaobj == 0 || zfsvfs->z_replay) 839 return (B_FALSE); 840 841 return (zfs_fuid_overquota(zfsvfs, isgroup, fuid)); 842} 843 844int 845zfsvfs_create(const char *osname, zfsvfs_t **zfvp) 846{ 847 objset_t *os; 848 zfsvfs_t *zfsvfs; 849 uint64_t zval; 850 int i, error; 851 uint64_t sa_obj; 852 853 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP); 854 855 /* 856 * We claim to always be readonly so we can open snapshots; 857 * other ZPL code will prevent us from writing to snapshots. 858 */ 859 error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os); 860 if (error) { 861 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 862 return (error); 863 } 864 865 /* 866 * Initialize the zfs-specific filesystem structure. 867 * Should probably make this a kmem cache, shuffle fields, 868 * and just bzero up to z_hold_mtx[]. 869 */ 870 zfsvfs->z_vfs = NULL; 871 zfsvfs->z_parent = zfsvfs; 872 zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE; 873 zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE; 874 zfsvfs->z_os = os; 875 876 error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version); 877 if (error) { 878 goto out; 879 } else if (zfsvfs->z_version > 880 zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) { 881 (void) printf("Can't mount a version %lld file system " 882 "on a version %lld pool\n. Pool must be upgraded to mount " 883 "this file system.", (u_longlong_t)zfsvfs->z_version, 884 (u_longlong_t)spa_version(dmu_objset_spa(os))); 885 error = ENOTSUP; 886 goto out; 887 } 888 if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0) 889 goto out; 890 zfsvfs->z_norm = (int)zval; 891 892 if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0) 893 goto out; 894 zfsvfs->z_utf8 = (zval != 0); 895 896 if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0) 897 goto out; 898 zfsvfs->z_case = (uint_t)zval; 899 900 /* 901 * Fold case on file systems that are always or sometimes case 902 * insensitive. 903 */ 904 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 905 zfsvfs->z_case == ZFS_CASE_MIXED) 906 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER; 907 908 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os); 909 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os); 910 911 if (zfsvfs->z_use_sa) { 912 /* should either have both of these objects or none */ 913 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, 914 &sa_obj); 915 if (error) 916 return (error); 917 } else { 918 /* 919 * Pre SA versions file systems should never touch 920 * either the attribute registration or layout objects. 921 */ 922 sa_obj = 0; 923 } 924 925 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END, 926 &zfsvfs->z_attr_table); 927 if (error) 928 goto out; 929 930 if (zfsvfs->z_version >= ZPL_VERSION_SA) 931 sa_register_update_callback(os, zfs_sa_upgrade); 932 933 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1, 934 &zfsvfs->z_root); 935 if (error) 936 goto out; 937 ASSERT(zfsvfs->z_root != 0); 938 939 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1, 940 &zfsvfs->z_unlinkedobj); 941 if (error) 942 goto out; 943 944 error = zap_lookup(os, MASTER_NODE_OBJ, 945 zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA], 946 8, 1, &zfsvfs->z_userquota_obj); 947 if (error && error != ENOENT) 948 goto out; 949 950 error = zap_lookup(os, MASTER_NODE_OBJ, 951 zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA], 952 8, 1, &zfsvfs->z_groupquota_obj); 953 if (error && error != ENOENT) 954 goto out; 955 956 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1, 957 &zfsvfs->z_fuid_obj); 958 if (error && error != ENOENT) 959 goto out; 960 961 error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1, 962 &zfsvfs->z_shares_dir); 963 if (error && error != ENOENT) 964 goto out; 965 966 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 967 mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL); 968 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t), 969 offsetof(znode_t, z_link_node)); 970 rrw_init(&zfsvfs->z_teardown_lock); 971 rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL); 972 rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL); 973 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 974 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL); 975 976 *zfvp = zfsvfs; 977 return (0); 978 979out: 980 dmu_objset_disown(os, zfsvfs); 981 *zfvp = NULL; 982 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 983 return (error); 984} 985 986static int 987zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting) 988{ 989 int error; 990 991 error = zfs_register_callbacks(zfsvfs->z_vfs); 992 if (error) 993 return (error); 994 995 /* 996 * Set the objset user_ptr to track its zfsvfs. 997 */ 998 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock); 999 dmu_objset_set_user(zfsvfs->z_os, zfsvfs); 1000 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock); 1001 1002 zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data); 1003 1004 /* 1005 * If we are not mounting (ie: online recv), then we don't 1006 * have to worry about replaying the log as we blocked all 1007 * operations out since we closed the ZIL. 1008 */ 1009 if (mounting) { 1010 boolean_t readonly; 1011 1012 /* 1013 * During replay we remove the read only flag to 1014 * allow replays to succeed. 1015 */ 1016 readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY; 1017 if (readonly != 0) 1018 zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY; 1019 else 1020 zfs_unlinked_drain(zfsvfs); 1021 1022 /* 1023 * Parse and replay the intent log. 1024 * 1025 * Because of ziltest, this must be done after 1026 * zfs_unlinked_drain(). (Further note: ziltest 1027 * doesn't use readonly mounts, where 1028 * zfs_unlinked_drain() isn't called.) This is because 1029 * ziltest causes spa_sync() to think it's committed, 1030 * but actually it is not, so the intent log contains 1031 * many txg's worth of changes. 1032 * 1033 * In particular, if object N is in the unlinked set in 1034 * the last txg to actually sync, then it could be 1035 * actually freed in a later txg and then reallocated 1036 * in a yet later txg. This would write a "create 1037 * object N" record to the intent log. Normally, this 1038 * would be fine because the spa_sync() would have 1039 * written out the fact that object N is free, before 1040 * we could write the "create object N" intent log 1041 * record. 1042 * 1043 * But when we are in ziltest mode, we advance the "open 1044 * txg" without actually spa_sync()-ing the changes to 1045 * disk. So we would see that object N is still 1046 * allocated and in the unlinked set, and there is an 1047 * intent log record saying to allocate it. 1048 */ 1049 if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) { 1050 if (zil_replay_disable) { 1051 zil_destroy(zfsvfs->z_log, B_FALSE); 1052 } else { 1053 zfsvfs->z_replay = B_TRUE; 1054 zil_replay(zfsvfs->z_os, zfsvfs, 1055 zfs_replay_vector); 1056 zfsvfs->z_replay = B_FALSE; 1057 } 1058 } 1059 zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */ 1060 } 1061 1062 return (0); 1063} 1064 1065extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */ 1066 1067void 1068zfsvfs_free(zfsvfs_t *zfsvfs) 1069{ 1070 int i; 1071 1072 /* 1073 * This is a barrier to prevent the filesystem from going away in 1074 * zfs_znode_move() until we can safely ensure that the filesystem is 1075 * not unmounted. We consider the filesystem valid before the barrier 1076 * and invalid after the barrier. 1077 */ 1078 rw_enter(&zfsvfs_lock, RW_READER); 1079 rw_exit(&zfsvfs_lock); 1080 1081 zfs_fuid_destroy(zfsvfs); 1082 1083 mutex_destroy(&zfsvfs->z_znodes_lock); 1084 mutex_destroy(&zfsvfs->z_lock); 1085 list_destroy(&zfsvfs->z_all_znodes); 1086 rrw_destroy(&zfsvfs->z_teardown_lock); 1087 rw_destroy(&zfsvfs->z_teardown_inactive_lock); 1088 rw_destroy(&zfsvfs->z_fuid_lock); 1089 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 1090 mutex_destroy(&zfsvfs->z_hold_mtx[i]); 1091 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 1092} 1093 1094static void 1095zfs_set_fuid_feature(zfsvfs_t *zfsvfs) 1096{ 1097 zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os); 1098 if (zfsvfs->z_vfs) { 1099 if (zfsvfs->z_use_fuids) { 1100 vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR); 1101 vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS); 1102 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS); 1103 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE); 1104 vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER); 1105 vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE); 1106 } else { 1107 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR); 1108 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS); 1109 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS); 1110 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE); 1111 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER); 1112 vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE); 1113 } 1114 } 1115 zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os); 1116} 1117 1118static int 1119zfs_domount(vfs_t *vfsp, char *osname) 1120{ 1121 uint64_t recordsize, fsid_guid; 1122 int error = 0; 1123 zfsvfs_t *zfsvfs; 1124 vnode_t *vp; 1125 1126 ASSERT(vfsp); 1127 ASSERT(osname); 1128 1129 error = zfsvfs_create(osname, &zfsvfs); 1130 if (error) 1131 return (error); 1132 zfsvfs->z_vfs = vfsp; 1133 1134 if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize, 1135 NULL)) 1136 goto out; 1137 zfsvfs->z_vfs->vfs_bsize = SPA_MINBLOCKSIZE; 1138 zfsvfs->z_vfs->mnt_stat.f_iosize = recordsize; 1139 1140 vfsp->vfs_data = zfsvfs; 1141 vfsp->mnt_flag |= MNT_LOCAL; 1142 vfsp->mnt_kern_flag |= MNTK_MPSAFE; 1143 vfsp->mnt_kern_flag |= MNTK_LOOKUP_SHARED; 1144 vfsp->mnt_kern_flag |= MNTK_SHARED_WRITES; 1145 vfsp->mnt_kern_flag |= MNTK_EXTENDED_SHARED; 1146 1147 /* 1148 * The fsid is 64 bits, composed of an 8-bit fs type, which 1149 * separates our fsid from any other filesystem types, and a 1150 * 56-bit objset unique ID. The objset unique ID is unique to 1151 * all objsets open on this system, provided by unique_create(). 1152 * The 8-bit fs type must be put in the low bits of fsid[1] 1153 * because that's where other Solaris filesystems put it. 1154 */ 1155 fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os); 1156 ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0); 1157 vfsp->vfs_fsid.val[0] = fsid_guid; 1158 vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) | 1159 vfsp->mnt_vfc->vfc_typenum & 0xFF; 1160 1161 /* 1162 * Set features for file system. 1163 */ 1164 zfs_set_fuid_feature(zfsvfs); 1165 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) { 1166 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS); 1167 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE); 1168 vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE); 1169 } else if (zfsvfs->z_case == ZFS_CASE_MIXED) { 1170 vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS); 1171 vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE); 1172 } 1173 vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED); 1174 1175 if (dmu_objset_is_snapshot(zfsvfs->z_os)) { 1176 uint64_t pval; 1177 1178 atime_changed_cb(zfsvfs, B_FALSE); 1179 readonly_changed_cb(zfsvfs, B_TRUE); 1180 if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL)) 1181 goto out; 1182 xattr_changed_cb(zfsvfs, pval); 1183 zfsvfs->z_issnap = B_TRUE; 1184 zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED; 1185 1186 mutex_enter(&zfsvfs->z_os->os_user_ptr_lock); 1187 dmu_objset_set_user(zfsvfs->z_os, zfsvfs); 1188 mutex_exit(&zfsvfs->z_os->os_user_ptr_lock); 1189 } else { 1190 error = zfsvfs_setup(zfsvfs, B_TRUE); 1191 } 1192 1193 vfs_mountedfrom(vfsp, osname); 1194 /* Grab extra reference. */ 1195 VERIFY(VFS_ROOT(vfsp, LK_EXCLUSIVE, &vp) == 0); 1196 VOP_UNLOCK(vp, 0); 1197 1198 if (!zfsvfs->z_issnap) 1199 zfsctl_create(zfsvfs); 1200out: 1201 if (error) { 1202 dmu_objset_disown(zfsvfs->z_os, zfsvfs); 1203 zfsvfs_free(zfsvfs); 1204 } else { 1205 atomic_add_32(&zfs_active_fs_count, 1); 1206 } 1207 1208 return (error); 1209} 1210 1211void 1212zfs_unregister_callbacks(zfsvfs_t *zfsvfs) 1213{ 1214 objset_t *os = zfsvfs->z_os; 1215 struct dsl_dataset *ds; 1216 1217 /* 1218 * Unregister properties. 1219 */ 1220 if (!dmu_objset_is_snapshot(os)) { 1221 ds = dmu_objset_ds(os); 1222 VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb, 1223 zfsvfs) == 0); 1224 1225 VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb, 1226 zfsvfs) == 0); 1227 1228 VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, 1229 zfsvfs) == 0); 1230 1231 VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb, 1232 zfsvfs) == 0); 1233 1234 VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb, 1235 zfsvfs) == 0); 1236 1237 VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb, 1238 zfsvfs) == 0); 1239 1240 VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, 1241 zfsvfs) == 0); 1242 1243 VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, 1244 zfsvfs) == 0); 1245 1246 VERIFY(dsl_prop_unregister(ds, "aclinherit", 1247 acl_inherit_changed_cb, zfsvfs) == 0); 1248 1249 VERIFY(dsl_prop_unregister(ds, "vscan", 1250 vscan_changed_cb, zfsvfs) == 0); 1251 } 1252} 1253 1254#ifdef SECLABEL 1255/* 1256 * Convert a decimal digit string to a uint64_t integer. 1257 */ 1258static int 1259str_to_uint64(char *str, uint64_t *objnum) 1260{ 1261 uint64_t num = 0; 1262 1263 while (*str) { 1264 if (*str < '0' || *str > '9') 1265 return (EINVAL); 1266 1267 num = num*10 + *str++ - '0'; 1268 } 1269 1270 *objnum = num; 1271 return (0); 1272} 1273 1274/* 1275 * The boot path passed from the boot loader is in the form of 1276 * "rootpool-name/root-filesystem-object-number'. Convert this 1277 * string to a dataset name: "rootpool-name/root-filesystem-name". 1278 */ 1279static int 1280zfs_parse_bootfs(char *bpath, char *outpath) 1281{ 1282 char *slashp; 1283 uint64_t objnum; 1284 int error; 1285 1286 if (*bpath == 0 || *bpath == '/') 1287 return (EINVAL); 1288 1289 (void) strcpy(outpath, bpath); 1290 1291 slashp = strchr(bpath, '/'); 1292 1293 /* if no '/', just return the pool name */ 1294 if (slashp == NULL) { 1295 return (0); 1296 } 1297 1298 /* if not a number, just return the root dataset name */ 1299 if (str_to_uint64(slashp+1, &objnum)) { 1300 return (0); 1301 } 1302 1303 *slashp = '\0'; 1304 error = dsl_dsobj_to_dsname(bpath, objnum, outpath); 1305 *slashp = '/'; 1306 1307 return (error); 1308} 1309 1310/* 1311 * zfs_check_global_label: 1312 * Check that the hex label string is appropriate for the dataset 1313 * being mounted into the global_zone proper. 1314 * 1315 * Return an error if the hex label string is not default or 1316 * admin_low/admin_high. For admin_low labels, the corresponding 1317 * dataset must be readonly. 1318 */ 1319int 1320zfs_check_global_label(const char *dsname, const char *hexsl) 1321{ 1322 if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0) 1323 return (0); 1324 if (strcasecmp(hexsl, ADMIN_HIGH) == 0) 1325 return (0); 1326 if (strcasecmp(hexsl, ADMIN_LOW) == 0) { 1327 /* must be readonly */ 1328 uint64_t rdonly; 1329 1330 if (dsl_prop_get_integer(dsname, 1331 zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL)) 1332 return (EACCES); 1333 return (rdonly ? 0 : EACCES); 1334 } 1335 return (EACCES); 1336} 1337 1338/* 1339 * zfs_mount_label_policy: 1340 * Determine whether the mount is allowed according to MAC check. 1341 * by comparing (where appropriate) label of the dataset against 1342 * the label of the zone being mounted into. If the dataset has 1343 * no label, create one. 1344 * 1345 * Returns: 1346 * 0 : access allowed 1347 * >0 : error code, such as EACCES 1348 */ 1349static int 1350zfs_mount_label_policy(vfs_t *vfsp, char *osname) 1351{ 1352 int error, retv; 1353 zone_t *mntzone = NULL; 1354 ts_label_t *mnt_tsl; 1355 bslabel_t *mnt_sl; 1356 bslabel_t ds_sl; 1357 char ds_hexsl[MAXNAMELEN]; 1358 1359 retv = EACCES; /* assume the worst */ 1360 1361 /* 1362 * Start by getting the dataset label if it exists. 1363 */ 1364 error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 1365 1, sizeof (ds_hexsl), &ds_hexsl, NULL); 1366 if (error) 1367 return (EACCES); 1368 1369 /* 1370 * If labeling is NOT enabled, then disallow the mount of datasets 1371 * which have a non-default label already. No other label checks 1372 * are needed. 1373 */ 1374 if (!is_system_labeled()) { 1375 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) 1376 return (0); 1377 return (EACCES); 1378 } 1379 1380 /* 1381 * Get the label of the mountpoint. If mounting into the global 1382 * zone (i.e. mountpoint is not within an active zone and the 1383 * zoned property is off), the label must be default or 1384 * admin_low/admin_high only; no other checks are needed. 1385 */ 1386 mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE); 1387 if (mntzone->zone_id == GLOBAL_ZONEID) { 1388 uint64_t zoned; 1389 1390 zone_rele(mntzone); 1391 1392 if (dsl_prop_get_integer(osname, 1393 zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) 1394 return (EACCES); 1395 if (!zoned) 1396 return (zfs_check_global_label(osname, ds_hexsl)); 1397 else 1398 /* 1399 * This is the case of a zone dataset being mounted 1400 * initially, before the zone has been fully created; 1401 * allow this mount into global zone. 1402 */ 1403 return (0); 1404 } 1405 1406 mnt_tsl = mntzone->zone_slabel; 1407 ASSERT(mnt_tsl != NULL); 1408 label_hold(mnt_tsl); 1409 mnt_sl = label2bslabel(mnt_tsl); 1410 1411 if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) { 1412 /* 1413 * The dataset doesn't have a real label, so fabricate one. 1414 */ 1415 char *str = NULL; 1416 1417 if (l_to_str_internal(mnt_sl, &str) == 0 && 1418 dsl_prop_set(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL), 1419 ZPROP_SRC_LOCAL, 1, strlen(str) + 1, str) == 0) 1420 retv = 0; 1421 if (str != NULL) 1422 kmem_free(str, strlen(str) + 1); 1423 } else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) { 1424 /* 1425 * Now compare labels to complete the MAC check. If the 1426 * labels are equal then allow access. If the mountpoint 1427 * label dominates the dataset label, allow readonly access. 1428 * Otherwise, access is denied. 1429 */ 1430 if (blequal(mnt_sl, &ds_sl)) 1431 retv = 0; 1432 else if (bldominates(mnt_sl, &ds_sl)) { 1433 vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0); 1434 retv = 0; 1435 } 1436 } 1437 1438 label_rele(mnt_tsl); 1439 zone_rele(mntzone); 1440 return (retv); 1441} 1442#endif /* SECLABEL */ 1443 1444#ifdef OPENSOLARIS_MOUNTROOT 1445static int 1446zfs_mountroot(vfs_t *vfsp, enum whymountroot why) 1447{ 1448 int error = 0; 1449 static int zfsrootdone = 0; 1450 zfsvfs_t *zfsvfs = NULL; 1451 znode_t *zp = NULL; 1452 vnode_t *vp = NULL; 1453 char *zfs_bootfs; 1454 char *zfs_devid; 1455 1456 ASSERT(vfsp); 1457 1458 /* 1459 * The filesystem that we mount as root is defined in the 1460 * boot property "zfs-bootfs" with a format of 1461 * "poolname/root-dataset-objnum". 1462 */ 1463 if (why == ROOT_INIT) { 1464 if (zfsrootdone++) 1465 return (EBUSY); 1466 /* 1467 * the process of doing a spa_load will require the 1468 * clock to be set before we could (for example) do 1469 * something better by looking at the timestamp on 1470 * an uberblock, so just set it to -1. 1471 */ 1472 clkset(-1); 1473 1474 if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) { 1475 cmn_err(CE_NOTE, "spa_get_bootfs: can not get " 1476 "bootfs name"); 1477 return (EINVAL); 1478 } 1479 zfs_devid = spa_get_bootprop("diskdevid"); 1480 error = spa_import_rootpool(rootfs.bo_name, zfs_devid); 1481 if (zfs_devid) 1482 spa_free_bootprop(zfs_devid); 1483 if (error) { 1484 spa_free_bootprop(zfs_bootfs); 1485 cmn_err(CE_NOTE, "spa_import_rootpool: error %d", 1486 error); 1487 return (error); 1488 } 1489 if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) { 1490 spa_free_bootprop(zfs_bootfs); 1491 cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d", 1492 error); 1493 return (error); 1494 } 1495 1496 spa_free_bootprop(zfs_bootfs); 1497 1498 if (error = vfs_lock(vfsp)) 1499 return (error); 1500 1501 if (error = zfs_domount(vfsp, rootfs.bo_name)) { 1502 cmn_err(CE_NOTE, "zfs_domount: error %d", error); 1503 goto out; 1504 } 1505 1506 zfsvfs = (zfsvfs_t *)vfsp->vfs_data; 1507 ASSERT(zfsvfs); 1508 if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) { 1509 cmn_err(CE_NOTE, "zfs_zget: error %d", error); 1510 goto out; 1511 } 1512 1513 vp = ZTOV(zp); 1514 mutex_enter(&vp->v_lock); 1515 vp->v_flag |= VROOT; 1516 mutex_exit(&vp->v_lock); 1517 rootvp = vp; 1518 1519 /* 1520 * Leave rootvp held. The root file system is never unmounted. 1521 */ 1522 1523 vfs_add((struct vnode *)0, vfsp, 1524 (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0); 1525out: 1526 vfs_unlock(vfsp); 1527 return (error); 1528 } else if (why == ROOT_REMOUNT) { 1529 readonly_changed_cb(vfsp->vfs_data, B_FALSE); 1530 vfsp->vfs_flag |= VFS_REMOUNT; 1531 1532 /* refresh mount options */ 1533 zfs_unregister_callbacks(vfsp->vfs_data); 1534 return (zfs_register_callbacks(vfsp)); 1535 1536 } else if (why == ROOT_UNMOUNT) { 1537 zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data); 1538 (void) zfs_sync(vfsp, 0, 0); 1539 return (0); 1540 } 1541 1542 /* 1543 * if "why" is equal to anything else other than ROOT_INIT, 1544 * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it. 1545 */ 1546 return (ENOTSUP); 1547} 1548#endif /* OPENSOLARIS_MOUNTROOT */ 1549 1550static int 1551getpoolname(const char *osname, char *poolname) 1552{ 1553 char *p; 1554 1555 p = strchr(osname, '/'); 1556 if (p == NULL) { 1557 if (strlen(osname) >= MAXNAMELEN) 1558 return (ENAMETOOLONG); 1559 (void) strcpy(poolname, osname); 1560 } else { 1561 if (p - osname >= MAXNAMELEN) 1562 return (ENAMETOOLONG); 1563 (void) strncpy(poolname, osname, p - osname); 1564 poolname[p - osname] = '\0'; 1565 } 1566 return (0); 1567} 1568 1569/*ARGSUSED*/ 1570static int 1571zfs_mount(vfs_t *vfsp) 1572{ 1573 kthread_t *td = curthread; 1574 vnode_t *mvp = vfsp->mnt_vnodecovered; 1575 cred_t *cr = td->td_ucred; 1576 char *osname; 1577 int error = 0; 1578 int canwrite; 1579 1580 if (!prison_allow(td->td_ucred, PR_ALLOW_MOUNT_ZFS)) 1581 return (EPERM); 1582 1583 if (vfs_getopt(vfsp->mnt_optnew, "from", (void **)&osname, NULL)) 1584 return (EINVAL); 1585 1586 /* 1587 * If full-owner-access is enabled and delegated administration is 1588 * turned on, we must set nosuid. 1589 */ 1590 if (zfs_super_owner && 1591 dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != ECANCELED) { 1592 secpolicy_fs_mount_clearopts(cr, vfsp); 1593 } 1594 1595 /* 1596 * Check for mount privilege? 1597 * 1598 * If we don't have privilege then see if 1599 * we have local permission to allow it 1600 */ 1601 error = secpolicy_fs_mount(cr, mvp, vfsp); 1602 if (error) { 1603 if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) != 0) 1604 goto out; 1605 1606 if (!(vfsp->vfs_flag & MS_REMOUNT)) { 1607 vattr_t vattr; 1608 1609 /* 1610 * Make sure user is the owner of the mount point 1611 * or has sufficient privileges. 1612 */ 1613 1614 vattr.va_mask = AT_UID; 1615 1616 vn_lock(mvp, LK_SHARED | LK_RETRY); 1617 if (VOP_GETATTR(mvp, &vattr, cr)) { 1618 VOP_UNLOCK(mvp, 0); 1619 goto out; 1620 } 1621 1622 if (secpolicy_vnode_owner(mvp, cr, vattr.va_uid) != 0 && 1623 VOP_ACCESS(mvp, VWRITE, cr, td) != 0) { 1624 VOP_UNLOCK(mvp, 0); 1625 goto out; 1626 } 1627 VOP_UNLOCK(mvp, 0); 1628 } 1629 1630 secpolicy_fs_mount_clearopts(cr, vfsp); 1631 } 1632 1633 /* 1634 * Refuse to mount a filesystem if we are in a local zone and the 1635 * dataset is not visible. 1636 */ 1637 if (!INGLOBALZONE(curthread) && 1638 (!zone_dataset_visible(osname, &canwrite) || !canwrite)) { 1639 error = EPERM; 1640 goto out; 1641 } 1642 1643#ifdef SECLABEL 1644 error = zfs_mount_label_policy(vfsp, osname); 1645 if (error) 1646 goto out; 1647#endif 1648 1649 vfsp->vfs_flag |= MNT_NFS4ACLS; 1650 1651 /* 1652 * When doing a remount, we simply refresh our temporary properties 1653 * according to those options set in the current VFS options. 1654 */ 1655 if (vfsp->vfs_flag & MS_REMOUNT) { 1656 /* refresh mount options */ 1657 zfs_unregister_callbacks(vfsp->vfs_data); 1658 error = zfs_register_callbacks(vfsp); 1659 goto out; 1660 } 1661 1662 /* Initial root mount: try hard to import the requested root pool. */ 1663 if ((vfsp->vfs_flag & MNT_ROOTFS) != 0 && 1664 (vfsp->vfs_flag & MNT_UPDATE) == 0) { 1665 char pname[MAXNAMELEN]; 1666 1667 error = getpoolname(osname, pname); 1668 if (error == 0) 1669 error = spa_import_rootpool(pname); 1670 if (error) 1671 goto out; 1672 } 1673 DROP_GIANT(); 1674 error = zfs_domount(vfsp, osname); 1675 PICKUP_GIANT(); 1676 1677#ifdef sun 1678 /* 1679 * Add an extra VFS_HOLD on our parent vfs so that it can't 1680 * disappear due to a forced unmount. 1681 */ 1682 if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap) 1683 VFS_HOLD(mvp->v_vfsp); 1684#endif /* sun */ 1685 1686out: 1687 return (error); 1688} 1689 1690static int 1691zfs_statfs(vfs_t *vfsp, struct statfs *statp) 1692{ 1693 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1694 uint64_t refdbytes, availbytes, usedobjs, availobjs; 1695 1696 statp->f_version = STATFS_VERSION; 1697 1698 ZFS_ENTER(zfsvfs); 1699 1700 dmu_objset_space(zfsvfs->z_os, 1701 &refdbytes, &availbytes, &usedobjs, &availobjs); 1702 1703 /* 1704 * The underlying storage pool actually uses multiple block sizes. 1705 * We report the fragsize as the smallest block size we support, 1706 * and we report our blocksize as the filesystem's maximum blocksize. 1707 */ 1708 statp->f_bsize = SPA_MINBLOCKSIZE; 1709 statp->f_iosize = zfsvfs->z_vfs->mnt_stat.f_iosize; 1710 1711 /* 1712 * The following report "total" blocks of various kinds in the 1713 * file system, but reported in terms of f_frsize - the 1714 * "fragment" size. 1715 */ 1716 1717 statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT; 1718 statp->f_bfree = availbytes / statp->f_bsize; 1719 statp->f_bavail = statp->f_bfree; /* no root reservation */ 1720 1721 /* 1722 * statvfs() should really be called statufs(), because it assumes 1723 * static metadata. ZFS doesn't preallocate files, so the best 1724 * we can do is report the max that could possibly fit in f_files, 1725 * and that minus the number actually used in f_ffree. 1726 * For f_ffree, report the smaller of the number of object available 1727 * and the number of blocks (each object will take at least a block). 1728 */ 1729 statp->f_ffree = MIN(availobjs, statp->f_bfree); 1730 statp->f_files = statp->f_ffree + usedobjs; 1731 1732 /* 1733 * We're a zfs filesystem. 1734 */ 1735 (void) strlcpy(statp->f_fstypename, "zfs", sizeof(statp->f_fstypename)); 1736 1737 strlcpy(statp->f_mntfromname, vfsp->mnt_stat.f_mntfromname, 1738 sizeof(statp->f_mntfromname)); 1739 strlcpy(statp->f_mntonname, vfsp->mnt_stat.f_mntonname, 1740 sizeof(statp->f_mntonname)); 1741 1742 statp->f_namemax = ZFS_MAXNAMELEN; 1743 1744 ZFS_EXIT(zfsvfs); 1745 return (0); 1746} 1747 1748int 1749zfs_vnode_lock(vnode_t *vp, int flags) 1750{ 1751 int error; 1752 1753 ASSERT(vp != NULL); 1754 1755 error = vn_lock(vp, flags); 1756 return (error); 1757} 1758 1759static int 1760zfs_root(vfs_t *vfsp, int flags, vnode_t **vpp) 1761{ 1762 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1763 znode_t *rootzp; 1764 int error; 1765 1766 ZFS_ENTER_NOERROR(zfsvfs); 1767 1768 error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp); 1769 if (error == 0) 1770 *vpp = ZTOV(rootzp); 1771 1772 ZFS_EXIT(zfsvfs); 1773 1774 if (error == 0) { 1775 error = zfs_vnode_lock(*vpp, flags); 1776 if (error == 0) 1777 (*vpp)->v_vflag |= VV_ROOT; 1778 } 1779 if (error != 0) 1780 *vpp = NULL; 1781 1782 return (error); 1783} 1784 1785/* 1786 * Teardown the zfsvfs::z_os. 1787 * 1788 * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock' 1789 * and 'z_teardown_inactive_lock' held. 1790 */ 1791static int 1792zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting) 1793{ 1794 znode_t *zp; 1795 1796 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG); 1797 1798 if (!unmounting) { 1799 /* 1800 * We purge the parent filesystem's vfsp as the parent 1801 * filesystem and all of its snapshots have their vnode's 1802 * v_vfsp set to the parent's filesystem's vfsp. Note, 1803 * 'z_parent' is self referential for non-snapshots. 1804 */ 1805 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0); 1806#ifdef FREEBSD_NAMECACHE 1807 cache_purgevfs(zfsvfs->z_parent->z_vfs); 1808#endif 1809 } 1810 1811 /* 1812 * Close the zil. NB: Can't close the zil while zfs_inactive 1813 * threads are blocked as zil_close can call zfs_inactive. 1814 */ 1815 if (zfsvfs->z_log) { 1816 zil_close(zfsvfs->z_log); 1817 zfsvfs->z_log = NULL; 1818 } 1819 1820 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER); 1821 1822 /* 1823 * If we are not unmounting (ie: online recv) and someone already 1824 * unmounted this file system while we were doing the switcheroo, 1825 * or a reopen of z_os failed then just bail out now. 1826 */ 1827 if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) { 1828 rw_exit(&zfsvfs->z_teardown_inactive_lock); 1829 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 1830 return (EIO); 1831 } 1832 1833 /* 1834 * At this point there are no vops active, and any new vops will 1835 * fail with EIO since we have z_teardown_lock for writer (only 1836 * relavent for forced unmount). 1837 * 1838 * Release all holds on dbufs. 1839 */ 1840 mutex_enter(&zfsvfs->z_znodes_lock); 1841 for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL; 1842 zp = list_next(&zfsvfs->z_all_znodes, zp)) 1843 if (zp->z_sa_hdl) { 1844 ASSERT(ZTOV(zp)->v_count >= 0); 1845 zfs_znode_dmu_fini(zp); 1846 } 1847 mutex_exit(&zfsvfs->z_znodes_lock); 1848 1849 /* 1850 * If we are unmounting, set the unmounted flag and let new vops 1851 * unblock. zfs_inactive will have the unmounted behavior, and all 1852 * other vops will fail with EIO. 1853 */ 1854 if (unmounting) { 1855 zfsvfs->z_unmounted = B_TRUE; 1856 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 1857 rw_exit(&zfsvfs->z_teardown_inactive_lock); 1858 } 1859 1860 /* 1861 * z_os will be NULL if there was an error in attempting to reopen 1862 * zfsvfs, so just return as the properties had already been 1863 * unregistered and cached data had been evicted before. 1864 */ 1865 if (zfsvfs->z_os == NULL) 1866 return (0); 1867 1868 /* 1869 * Unregister properties. 1870 */ 1871 zfs_unregister_callbacks(zfsvfs); 1872 1873 /* 1874 * Evict cached data 1875 */ 1876 if (dsl_dataset_is_dirty(dmu_objset_ds(zfsvfs->z_os)) && 1877 !(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY)) 1878 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0); 1879 (void) dmu_objset_evict_dbufs(zfsvfs->z_os); 1880 1881 return (0); 1882} 1883 1884/*ARGSUSED*/ 1885static int 1886zfs_umount(vfs_t *vfsp, int fflag) 1887{ 1888 kthread_t *td = curthread; 1889 zfsvfs_t *zfsvfs = vfsp->vfs_data; 1890 objset_t *os; 1891 cred_t *cr = td->td_ucred; 1892 int ret; 1893 1894 ret = secpolicy_fs_unmount(cr, vfsp); 1895 if (ret) { 1896 if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource), 1897 ZFS_DELEG_PERM_MOUNT, cr)) 1898 return (ret); 1899 } 1900 1901 /* 1902 * We purge the parent filesystem's vfsp as the parent filesystem 1903 * and all of its snapshots have their vnode's v_vfsp set to the 1904 * parent's filesystem's vfsp. Note, 'z_parent' is self 1905 * referential for non-snapshots. 1906 */ 1907 (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0); 1908 1909 /* 1910 * Unmount any snapshots mounted under .zfs before unmounting the 1911 * dataset itself. 1912 */ 1913 if (zfsvfs->z_ctldir != NULL) { 1914 if ((ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) 1915 return (ret); 1916 ret = vflush(vfsp, 0, 0, td); 1917 ASSERT(ret == EBUSY); 1918 if (!(fflag & MS_FORCE)) { 1919 if (zfsvfs->z_ctldir->v_count > 1) 1920 return (EBUSY); 1921 ASSERT(zfsvfs->z_ctldir->v_count == 1); 1922 } 1923 zfsctl_destroy(zfsvfs); 1924 ASSERT(zfsvfs->z_ctldir == NULL); 1925 } 1926 1927 if (fflag & MS_FORCE) { 1928 /* 1929 * Mark file system as unmounted before calling 1930 * vflush(FORCECLOSE). This way we ensure no future vnops 1931 * will be called and risk operating on DOOMED vnodes. 1932 */ 1933 rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG); 1934 zfsvfs->z_unmounted = B_TRUE; 1935 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 1936 } 1937 1938 /* 1939 * Flush all the files. 1940 */ 1941 ret = vflush(vfsp, 1, (fflag & MS_FORCE) ? FORCECLOSE : 0, td); 1942 if (ret != 0) { 1943 if (!zfsvfs->z_issnap) { 1944 zfsctl_create(zfsvfs); 1945 ASSERT(zfsvfs->z_ctldir != NULL); 1946 } 1947 return (ret); 1948 } 1949 1950 if (!(fflag & MS_FORCE)) { 1951 /* 1952 * Check the number of active vnodes in the file system. 1953 * Our count is maintained in the vfs structure, but the 1954 * number is off by 1 to indicate a hold on the vfs 1955 * structure itself. 1956 * 1957 * The '.zfs' directory maintains a reference of its 1958 * own, and any active references underneath are 1959 * reflected in the vnode count. 1960 */ 1961 if (zfsvfs->z_ctldir == NULL) { 1962 if (vfsp->vfs_count > 1) 1963 return (EBUSY); 1964 } else { 1965 if (vfsp->vfs_count > 2 || 1966 zfsvfs->z_ctldir->v_count > 1) 1967 return (EBUSY); 1968 } 1969 } 1970 1971 VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0); 1972 os = zfsvfs->z_os; 1973 1974 /* 1975 * z_os will be NULL if there was an error in 1976 * attempting to reopen zfsvfs. 1977 */ 1978 if (os != NULL) { 1979 /* 1980 * Unset the objset user_ptr. 1981 */ 1982 mutex_enter(&os->os_user_ptr_lock); 1983 dmu_objset_set_user(os, NULL); 1984 mutex_exit(&os->os_user_ptr_lock); 1985 1986 /* 1987 * Finally release the objset 1988 */ 1989 dmu_objset_disown(os, zfsvfs); 1990 } 1991 1992 /* 1993 * We can now safely destroy the '.zfs' directory node. 1994 */ 1995 if (zfsvfs->z_ctldir != NULL) 1996 zfsctl_destroy(zfsvfs); 1997 if (zfsvfs->z_issnap) { 1998 vnode_t *svp = vfsp->mnt_vnodecovered; 1999 2000 if (svp->v_count >= 2) 2001 VN_RELE(svp); 2002 } 2003 zfs_freevfs(vfsp); 2004 2005 return (0); 2006} 2007 2008static int 2009zfs_vget(vfs_t *vfsp, ino_t ino, int flags, vnode_t **vpp) 2010{ 2011 zfsvfs_t *zfsvfs = vfsp->vfs_data; 2012 znode_t *zp; 2013 int err; 2014 2015 /* 2016 * zfs_zget() can't operate on virtual entries like .zfs/ or 2017 * .zfs/snapshot/ directories, that's why we return EOPNOTSUPP. 2018 * This will make NFS to switch to LOOKUP instead of using VGET. 2019 */ 2020 if (ino == ZFSCTL_INO_ROOT || ino == ZFSCTL_INO_SNAPDIR || 2021 (zfsvfs->z_shares_dir != 0 && ino == zfsvfs->z_shares_dir)) 2022 return (EOPNOTSUPP); 2023 2024 ZFS_ENTER(zfsvfs); 2025 err = zfs_zget(zfsvfs, ino, &zp); 2026 if (err == 0 && zp->z_unlinked) { 2027 VN_RELE(ZTOV(zp)); 2028 err = EINVAL; 2029 } 2030 if (err == 0) 2031 *vpp = ZTOV(zp); 2032 ZFS_EXIT(zfsvfs); 2033 if (err == 0) 2034 err = zfs_vnode_lock(*vpp, flags); 2035 if (err != 0) 2036 *vpp = NULL; 2037 else 2038 (*vpp)->v_hash = ino; 2039 return (err); 2040} 2041 2042static int 2043zfs_checkexp(vfs_t *vfsp, struct sockaddr *nam, int *extflagsp, 2044 struct ucred **credanonp, int *numsecflavors, int **secflavors) 2045{ 2046 zfsvfs_t *zfsvfs = vfsp->vfs_data; 2047 2048 /* 2049 * If this is regular file system vfsp is the same as 2050 * zfsvfs->z_parent->z_vfs, but if it is snapshot, 2051 * zfsvfs->z_parent->z_vfs represents parent file system 2052 * which we have to use here, because only this file system 2053 * has mnt_export configured. 2054 */ 2055 return (vfs_stdcheckexp(zfsvfs->z_parent->z_vfs, nam, extflagsp, 2056 credanonp, numsecflavors, secflavors)); 2057} 2058 2059CTASSERT(SHORT_FID_LEN <= sizeof(struct fid)); 2060CTASSERT(LONG_FID_LEN <= sizeof(struct fid)); 2061 2062static int 2063zfs_fhtovp(vfs_t *vfsp, fid_t *fidp, int flags, vnode_t **vpp) 2064{ 2065 zfsvfs_t *zfsvfs = vfsp->vfs_data; 2066 znode_t *zp; 2067 uint64_t object = 0; 2068 uint64_t fid_gen = 0; 2069 uint64_t gen_mask; 2070 uint64_t zp_gen; 2071 int i, err; 2072 2073 *vpp = NULL; 2074 2075 ZFS_ENTER(zfsvfs); 2076 2077 /* 2078 * On FreeBSD we can get snapshot's mount point or its parent file 2079 * system mount point depending if snapshot is already mounted or not. 2080 */ 2081 if (zfsvfs->z_parent == zfsvfs && fidp->fid_len == LONG_FID_LEN) { 2082 zfid_long_t *zlfid = (zfid_long_t *)fidp; 2083 uint64_t objsetid = 0; 2084 uint64_t setgen = 0; 2085 2086 for (i = 0; i < sizeof (zlfid->zf_setid); i++) 2087 objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i); 2088 2089 for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 2090 setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i); 2091 2092 ZFS_EXIT(zfsvfs); 2093 2094 err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs); 2095 if (err) 2096 return (EINVAL); 2097 ZFS_ENTER(zfsvfs); 2098 } 2099 2100 if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) { 2101 zfid_short_t *zfid = (zfid_short_t *)fidp; 2102 2103 for (i = 0; i < sizeof (zfid->zf_object); i++) 2104 object |= ((uint64_t)zfid->zf_object[i]) << (8 * i); 2105 2106 for (i = 0; i < sizeof (zfid->zf_gen); i++) 2107 fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i); 2108 } else { 2109 ZFS_EXIT(zfsvfs); 2110 return (EINVAL); 2111 } 2112 2113 /* 2114 * A zero fid_gen means we are in .zfs or the .zfs/snapshot 2115 * directory tree. If the object == zfsvfs->z_shares_dir, then 2116 * we are in the .zfs/shares directory tree. 2117 */ 2118 if ((fid_gen == 0 && 2119 (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) || 2120 (zfsvfs->z_shares_dir != 0 && object == zfsvfs->z_shares_dir)) { 2121 *vpp = zfsvfs->z_ctldir; 2122 ASSERT(*vpp != NULL); 2123 if (object == ZFSCTL_INO_SNAPDIR) { 2124 VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL, 2125 0, NULL, NULL, NULL, NULL, NULL) == 0); 2126 } else if (object == zfsvfs->z_shares_dir) { 2127 VERIFY(zfsctl_root_lookup(*vpp, "shares", vpp, NULL, 2128 0, NULL, NULL, NULL, NULL, NULL) == 0); 2129 } else { 2130 VN_HOLD(*vpp); 2131 } 2132 ZFS_EXIT(zfsvfs); 2133 err = zfs_vnode_lock(*vpp, flags); 2134 if (err != 0) 2135 *vpp = NULL; 2136 return (err); 2137 } 2138 2139 gen_mask = -1ULL >> (64 - 8 * i); 2140 2141 dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask); 2142 if (err = zfs_zget(zfsvfs, object, &zp)) { 2143 ZFS_EXIT(zfsvfs); 2144 return (err); 2145 } 2146 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen, 2147 sizeof (uint64_t)); 2148 zp_gen = zp_gen & gen_mask; 2149 if (zp_gen == 0) 2150 zp_gen = 1; 2151 if (zp->z_unlinked || zp_gen != fid_gen) { 2152 dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen); 2153 VN_RELE(ZTOV(zp)); 2154 ZFS_EXIT(zfsvfs); 2155 return (EINVAL); 2156 } 2157 2158 *vpp = ZTOV(zp); 2159 ZFS_EXIT(zfsvfs); 2160 err = zfs_vnode_lock(*vpp, flags | LK_RETRY); 2161 if (err == 0) 2162 vnode_create_vobject(*vpp, zp->z_size, curthread); 2163 else 2164 *vpp = NULL; 2165 return (err); 2166} 2167 2168/* 2169 * Block out VOPs and close zfsvfs_t::z_os 2170 * 2171 * Note, if successful, then we return with the 'z_teardown_lock' and 2172 * 'z_teardown_inactive_lock' write held. 2173 */ 2174int 2175zfs_suspend_fs(zfsvfs_t *zfsvfs) 2176{ 2177 int error; 2178 2179 if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0) 2180 return (error); 2181 dmu_objset_disown(zfsvfs->z_os, zfsvfs); 2182 2183 return (0); 2184} 2185 2186/* 2187 * Reopen zfsvfs_t::z_os and release VOPs. 2188 */ 2189int 2190zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname) 2191{ 2192 int err; 2193 2194 ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock)); 2195 ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)); 2196 2197 err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs, 2198 &zfsvfs->z_os); 2199 if (err) { 2200 zfsvfs->z_os = NULL; 2201 } else { 2202 znode_t *zp; 2203 uint64_t sa_obj = 0; 2204 2205 /* 2206 * Make sure version hasn't changed 2207 */ 2208 2209 err = zfs_get_zplprop(zfsvfs->z_os, ZFS_PROP_VERSION, 2210 &zfsvfs->z_version); 2211 2212 if (err) 2213 goto bail; 2214 2215 err = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ, 2216 ZFS_SA_ATTRS, 8, 1, &sa_obj); 2217 2218 if (err && zfsvfs->z_version >= ZPL_VERSION_SA) 2219 goto bail; 2220 2221 if ((err = sa_setup(zfsvfs->z_os, sa_obj, 2222 zfs_attr_table, ZPL_END, &zfsvfs->z_attr_table)) != 0) 2223 goto bail; 2224 2225 if (zfsvfs->z_version >= ZPL_VERSION_SA) 2226 sa_register_update_callback(zfsvfs->z_os, 2227 zfs_sa_upgrade); 2228 2229 VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0); 2230 2231 zfs_set_fuid_feature(zfsvfs); 2232 2233 /* 2234 * Attempt to re-establish all the active znodes with 2235 * their dbufs. If a zfs_rezget() fails, then we'll let 2236 * any potential callers discover that via ZFS_ENTER_VERIFY_VP 2237 * when they try to use their znode. 2238 */ 2239 mutex_enter(&zfsvfs->z_znodes_lock); 2240 for (zp = list_head(&zfsvfs->z_all_znodes); zp; 2241 zp = list_next(&zfsvfs->z_all_znodes, zp)) { 2242 (void) zfs_rezget(zp); 2243 } 2244 mutex_exit(&zfsvfs->z_znodes_lock); 2245 } 2246 2247bail: 2248 /* release the VOPs */ 2249 rw_exit(&zfsvfs->z_teardown_inactive_lock); 2250 rrw_exit(&zfsvfs->z_teardown_lock, FTAG); 2251 2252 if (err) { 2253 /* 2254 * Since we couldn't reopen zfsvfs::z_os, or 2255 * setup the sa framework force unmount this file system. 2256 */ 2257 if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0) 2258 (void) dounmount(zfsvfs->z_vfs, MS_FORCE, curthread); 2259 } 2260 return (err); 2261} 2262 2263static void 2264zfs_freevfs(vfs_t *vfsp) 2265{ 2266 zfsvfs_t *zfsvfs = vfsp->vfs_data; 2267 2268#ifdef sun 2269 /* 2270 * If this is a snapshot, we have an extra VFS_HOLD on our parent 2271 * from zfs_mount(). Release it here. If we came through 2272 * zfs_mountroot() instead, we didn't grab an extra hold, so 2273 * skip the VFS_RELE for rootvfs. 2274 */ 2275 if (zfsvfs->z_issnap && (vfsp != rootvfs)) 2276 VFS_RELE(zfsvfs->z_parent->z_vfs); 2277#endif /* sun */ 2278 2279 zfsvfs_free(zfsvfs); 2280 2281 atomic_add_32(&zfs_active_fs_count, -1); 2282} 2283 2284#ifdef __i386__ 2285static int desiredvnodes_backup; 2286#endif 2287 2288static void 2289zfs_vnodes_adjust(void) 2290{ 2291#ifdef __i386__ 2292 int newdesiredvnodes; 2293 2294 desiredvnodes_backup = desiredvnodes; 2295 2296 /* 2297 * We calculate newdesiredvnodes the same way it is done in 2298 * vntblinit(). If it is equal to desiredvnodes, it means that 2299 * it wasn't tuned by the administrator and we can tune it down. 2300 */ 2301 newdesiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 * 2302 vm_kmem_size / (5 * (sizeof(struct vm_object) + 2303 sizeof(struct vnode)))); 2304 if (newdesiredvnodes == desiredvnodes) 2305 desiredvnodes = (3 * newdesiredvnodes) / 4; 2306#endif 2307} 2308 2309static void 2310zfs_vnodes_adjust_back(void) 2311{ 2312 2313#ifdef __i386__ 2314 desiredvnodes = desiredvnodes_backup; 2315#endif 2316} 2317 2318void 2319zfs_init(void) 2320{ 2321 2322 printf("ZFS filesystem version: " ZPL_VERSION_STRING "\n"); 2323 2324 /* 2325 * Initialize .zfs directory structures 2326 */ 2327 zfsctl_init(); 2328 2329 /* 2330 * Initialize znode cache, vnode ops, etc... 2331 */ 2332 zfs_znode_init(); 2333 2334 /* 2335 * Reduce number of vnodes. Originally number of vnodes is calculated 2336 * with UFS inode in mind. We reduce it here, because it's too big for 2337 * ZFS/i386. 2338 */ 2339 zfs_vnodes_adjust(); 2340 2341 dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb); 2342} 2343 2344void 2345zfs_fini(void) 2346{ 2347 zfsctl_fini(); 2348 zfs_znode_fini(); 2349 zfs_vnodes_adjust_back(); 2350} 2351 2352int 2353zfs_busy(void) 2354{ 2355 return (zfs_active_fs_count != 0); 2356} 2357 2358int 2359zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers) 2360{ 2361 int error; 2362 objset_t *os = zfsvfs->z_os; 2363 dmu_tx_t *tx; 2364 2365 if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION) 2366 return (EINVAL); 2367 2368 if (newvers < zfsvfs->z_version) 2369 return (EINVAL); 2370 2371 if (zfs_spa_version_map(newvers) > 2372 spa_version(dmu_objset_spa(zfsvfs->z_os))) 2373 return (ENOTSUP); 2374 2375 tx = dmu_tx_create(os); 2376 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR); 2377 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) { 2378 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE, 2379 ZFS_SA_ATTRS); 2380 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 2381 } 2382 error = dmu_tx_assign(tx, TXG_WAIT); 2383 if (error) { 2384 dmu_tx_abort(tx); 2385 return (error); 2386 } 2387 2388 error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR, 2389 8, 1, &newvers, tx); 2390 2391 if (error) { 2392 dmu_tx_commit(tx); 2393 return (error); 2394 } 2395 2396 if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) { 2397 uint64_t sa_obj; 2398 2399 ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=, 2400 SPA_VERSION_SA); 2401 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE, 2402 DMU_OT_NONE, 0, tx); 2403 2404 error = zap_add(os, MASTER_NODE_OBJ, 2405 ZFS_SA_ATTRS, 8, 1, &sa_obj, tx); 2406 ASSERT0(error); 2407 2408 VERIFY(0 == sa_set_sa_object(os, sa_obj)); 2409 sa_register_update_callback(os, zfs_sa_upgrade); 2410 } 2411 2412 spa_history_log_internal(LOG_DS_UPGRADE, 2413 dmu_objset_spa(os), tx, "oldver=%llu newver=%llu dataset = %llu", 2414 zfsvfs->z_version, newvers, dmu_objset_id(os)); 2415 2416 dmu_tx_commit(tx); 2417 2418 zfsvfs->z_version = newvers; 2419 2420 zfs_set_fuid_feature(zfsvfs); 2421 2422 return (0); 2423} 2424 2425/* 2426 * Read a property stored within the master node. 2427 */ 2428int 2429zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value) 2430{ 2431 const char *pname; 2432 int error = ENOENT; 2433 2434 /* 2435 * Look up the file system's value for the property. For the 2436 * version property, we look up a slightly different string. 2437 */ 2438 if (prop == ZFS_PROP_VERSION) 2439 pname = ZPL_VERSION_STR; 2440 else 2441 pname = zfs_prop_to_name(prop); 2442 2443 if (os != NULL) 2444 error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value); 2445 2446 if (error == ENOENT) { 2447 /* No value set, use the default value */ 2448 switch (prop) { 2449 case ZFS_PROP_VERSION: 2450 *value = ZPL_VERSION; 2451 break; 2452 case ZFS_PROP_NORMALIZE: 2453 case ZFS_PROP_UTF8ONLY: 2454 *value = 0; 2455 break; 2456 case ZFS_PROP_CASE: 2457 *value = ZFS_CASE_SENSITIVE; 2458 break; 2459 default: 2460 return (error); 2461 } 2462 error = 0; 2463 } 2464 return (error); 2465} 2466 2467#ifdef _KERNEL 2468void 2469zfsvfs_update_fromname(const char *oldname, const char *newname) 2470{ 2471 char tmpbuf[MAXPATHLEN]; 2472 struct mount *mp; 2473 char *fromname; 2474 size_t oldlen; 2475 2476 oldlen = strlen(oldname); 2477 2478 mtx_lock(&mountlist_mtx); 2479 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 2480 fromname = mp->mnt_stat.f_mntfromname; 2481 if (strcmp(fromname, oldname) == 0) { 2482 (void)strlcpy(fromname, newname, 2483 sizeof(mp->mnt_stat.f_mntfromname)); 2484 continue; 2485 } 2486 if (strncmp(fromname, oldname, oldlen) == 0 && 2487 (fromname[oldlen] == '/' || fromname[oldlen] == '@')) { 2488 (void)snprintf(tmpbuf, sizeof(tmpbuf), "%s%s", 2489 newname, fromname + oldlen); 2490 (void)strlcpy(fromname, tmpbuf, 2491 sizeof(mp->mnt_stat.f_mntfromname)); 2492 continue; 2493 } 2494 } 2495 mtx_unlock(&mountlist_mtx); 2496} 2497#endif 2498