dmu_objset.c revision 290754
120253Sjoerg/* 220302Sjoerg * CDDL HEADER START 320302Sjoerg * 420253Sjoerg * The contents of this file are subject to the terms of the 520253Sjoerg * Common Development and Distribution License (the "License"). 620253Sjoerg * You may not use this file except in compliance with the License. 720253Sjoerg * 820253Sjoerg * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 920302Sjoerg * or http://www.opensolaris.org/os/licensing. 1020253Sjoerg * See the License for the specific language governing permissions 1120253Sjoerg * and limitations under the License. 1220253Sjoerg * 1320253Sjoerg * When distributing Covered Code, include this CDDL HEADER in each 1420302Sjoerg * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1520253Sjoerg * If applicable, add the following below this CDDL HEADER, with the 1620253Sjoerg * fields enclosed by brackets "[]" replaced with your own identifying 1720302Sjoerg * information: Portions Copyright [yyyy] [name of copyright owner] 1820253Sjoerg * 1920253Sjoerg * CDDL HEADER END 2020253Sjoerg */ 2120253Sjoerg/* 2220253Sjoerg * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 2320253Sjoerg * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 2420253Sjoerg * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. 2520253Sjoerg * Copyright (c) 2013, Joyent, Inc. All rights reserved. 2620253Sjoerg * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. 2730259Scharnier * Copyright 2015 Nexenta Systems, Inc. All rights reserved. 2830259Scharnier * Copyright (c) 2015, STRATO AG, Inc. All rights reserved. 2950479Speter */ 3030259Scharnier 3130259Scharnier/* Portions Copyright 2010 Robert Milkowski */ 3220253Sjoerg 3330259Scharnier#include <sys/cred.h> 3420253Sjoerg#include <sys/zfs_context.h> 3530259Scharnier#include <sys/dmu_objset.h> 3620253Sjoerg#include <sys/dsl_dir.h> 3720253Sjoerg#include <sys/dsl_dataset.h> 3820253Sjoerg#include <sys/dsl_prop.h> 3920253Sjoerg#include <sys/dsl_pool.h> 4020253Sjoerg#include <sys/dsl_synctask.h> 4120253Sjoerg#include <sys/dsl_deleg.h> 4220253Sjoerg#include <sys/dnode.h> 4320253Sjoerg#include <sys/dbuf.h> 4420253Sjoerg#include <sys/zvol.h> 4520253Sjoerg#include <sys/dmu_tx.h> 4620253Sjoerg#include <sys/zap.h> 4752502Sdavidn#include <sys/zil.h> 4820253Sjoerg#include <sys/dmu_impl.h> 4920253Sjoerg#include <sys/zfs_ioctl.h> 5020253Sjoerg#include <sys/sa.h> 5120253Sjoerg#include <sys/zfs_onexit.h> 5220747Sdavidn#include <sys/dsl_destroy.h> 5352502Sdavidn#include <sys/vdev.h> 5420253Sjoerg 5520253Sjoerg/* 5620253Sjoerg * Needed to close a window in dnode_move() that allows the objset to be freed 5720253Sjoerg * before it can be safely accessed. 5820253Sjoerg */ 5920253Sjoergkrwlock_t os_lock; 6020253Sjoerg 6120253Sjoerg/* 6220253Sjoerg * Tunable to overwrite the maximum number of threads for the parallization 6352512Sdavidn * of dmu_objset_find_dp, needed to speed up the import of pools with many 6452512Sdavidn * datasets. 6552512Sdavidn * Default is 4 times the number of leaf vdevs. 6620267Sjoerg */ 6720267Sjoergint dmu_find_threads = 0; 6820267Sjoerg 6920267Sjoergstatic void dmu_objset_find_dp_cb(void *arg); 7052512Sdavidn 7120267Sjoergvoid 7220267Sjoergdmu_objset_init(void) 7320267Sjoerg{ 7420267Sjoerg rw_init(&os_lock, NULL, RW_DEFAULT, NULL); 7520267Sjoerg} 7620267Sjoerg 7720267Sjoergvoid 7820253Sjoergdmu_objset_fini(void) 7920267Sjoerg{ 8020253Sjoerg rw_destroy(&os_lock); 8144229Sdavidn} 8244229Sdavidn 8320253Sjoergspa_t * 8444229Sdavidndmu_objset_spa(objset_t *os) 8520267Sjoerg{ 8620253Sjoerg return (os->os_spa); 8720253Sjoerg} 8820253Sjoerg 8930259Scharnierzilog_t * 9020253Sjoergdmu_objset_zil(objset_t *os) 9161957Sache{ 9220253Sjoerg return (os->os_zil); 9320253Sjoerg} 9420253Sjoerg 9520253Sjoergdsl_pool_t * 9644229Sdavidndmu_objset_pool(objset_t *os) 9720253Sjoerg{ 9820253Sjoerg dsl_dataset_t *ds; 9920253Sjoerg 10044229Sdavidn if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir) 10120253Sjoerg return (ds->ds_dir->dd_pool); 10220253Sjoerg else 10320253Sjoerg return (spa_get_dsl(os->os_spa)); 10420747Sdavidn} 10520747Sdavidn 10620253Sjoergdsl_dataset_t * 10720253Sjoergdmu_objset_ds(objset_t *os) 10820747Sdavidn{ 10920267Sjoerg return (os->os_dsl_dataset); 11020253Sjoerg} 11130259Scharnier 11220253Sjoergdmu_objset_type_t 11320253Sjoergdmu_objset_type(objset_t *os) 11420253Sjoerg{ 11520253Sjoerg return (os->os_phys->os_type); 11620253Sjoerg} 11720253Sjoerg 11820253Sjoergvoid 11920253Sjoergdmu_objset_name(objset_t *os, char *buf) 12020253Sjoerg{ 12120253Sjoerg dsl_dataset_name(os->os_dsl_dataset, buf); 12252502Sdavidn} 12352502Sdavidn 12452502Sdavidnuint64_t 12552502Sdavidndmu_objset_id(objset_t *os) 12656000Sdavidn{ 12752502Sdavidn dsl_dataset_t *ds = os->os_dsl_dataset; 12852502Sdavidn 12920253Sjoerg return (ds ? ds->ds_object : 0); 13020267Sjoerg} 13120253Sjoerg 13220267Sjoergzfs_sync_type_t 13320253Sjoergdmu_objset_syncprop(objset_t *os) 13420253Sjoerg{ 13520253Sjoerg return (os->os_sync); 13620253Sjoerg} 13720253Sjoerg 13820679Sdavidnzfs_logbias_op_t 13920253Sjoergdmu_objset_logbias(objset_t *os) 14020253Sjoerg{ 14130259Scharnier return (os->os_logbias); 14220253Sjoerg} 14330259Scharnier 14420253Sjoergstatic void 14520747Sdavidnchecksum_changed_cb(void *arg, uint64_t newval) 14620747Sdavidn{ 14720253Sjoerg objset_t *os = arg; 14820679Sdavidn 14920253Sjoerg /* 15020253Sjoerg * Inheritance should have been done by now. 15120253Sjoerg */ 15220253Sjoerg ASSERT(newval != ZIO_CHECKSUM_INHERIT); 15320253Sjoerg 15420253Sjoerg os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE); 15520253Sjoerg} 15620253Sjoerg 15720253Sjoergstatic void 15820253Sjoergcompression_changed_cb(void *arg, uint64_t newval) 15920253Sjoerg{ 16020253Sjoerg objset_t *os = arg; 161124382Siedowse 162124382Siedowse /* 16320253Sjoerg * Inheritance and range checking should have been done by now. 16420253Sjoerg */ 16520253Sjoerg ASSERT(newval != ZIO_COMPRESS_INHERIT); 16620253Sjoerg 167124382Siedowse os->os_compress = zio_compress_select(os->os_spa, newval, 16820253Sjoerg ZIO_COMPRESS_ON); 16920253Sjoerg} 17020253Sjoerg 17120253Sjoergstatic void 17220253Sjoergcopies_changed_cb(void *arg, uint64_t newval) 17320253Sjoerg{ 17420253Sjoerg objset_t *os = arg; 17520253Sjoerg 17620253Sjoerg /* 17720253Sjoerg * Inheritance and range checking should have been done by now. 17820253Sjoerg */ 17920253Sjoerg ASSERT(newval > 0); 18020253Sjoerg ASSERT(newval <= spa_max_replication(os->os_spa)); 18120253Sjoerg 18220253Sjoerg os->os_copies = newval; 18320253Sjoerg} 18420253Sjoerg 18520253Sjoergstatic void 18620253Sjoergdedup_changed_cb(void *arg, uint64_t newval) 18720253Sjoerg{ 18820253Sjoerg objset_t *os = arg; 18920253Sjoerg spa_t *spa = os->os_spa; 19020253Sjoerg enum zio_checksum checksum; 19120253Sjoerg 19220253Sjoerg /* 19330259Scharnier * Inheritance should have been done by now. 19420267Sjoerg */ 19520253Sjoerg ASSERT(newval != ZIO_CHECKSUM_INHERIT); 19620253Sjoerg 19720253Sjoerg checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF); 19820253Sjoerg 19920253Sjoerg os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK; 20030259Scharnier os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY); 201124382Siedowse} 202124382Siedowse 203124382Siedowsestatic void 204124382Siedowseprimary_cache_changed_cb(void *arg, uint64_t newval) 205124382Siedowse{ 206124382Siedowse objset_t *os = arg; 20720253Sjoerg 20820253Sjoerg /* 20920267Sjoerg * Inheritance and range checking should have been done by now. 21020267Sjoerg */ 21120267Sjoerg ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 21220267Sjoerg newval == ZFS_CACHE_METADATA); 21320267Sjoerg 21420267Sjoerg os->os_primary_cache = newval; 21520747Sdavidn} 21620747Sdavidn 21720267Sjoergstatic void 21820747Sdavidnsecondary_cache_changed_cb(void *arg, uint64_t newval) 21920747Sdavidn{ 22020747Sdavidn objset_t *os = arg; 22120747Sdavidn 22220747Sdavidn /* 22320747Sdavidn * Inheritance and range checking should have been done by now. 22420747Sdavidn */ 22520267Sjoerg ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE || 22620267Sjoerg newval == ZFS_CACHE_METADATA); 22720747Sdavidn 22820267Sjoerg os->os_secondary_cache = newval; 22944229Sdavidn} 23061957Sache 23130259Scharnierstatic void 23220267Sjoergsync_changed_cb(void *arg, uint64_t newval) 23320267Sjoerg{ 23420267Sjoerg objset_t *os = arg; 23520267Sjoerg 23620267Sjoerg /* 23720267Sjoerg * Inheritance and range checking should have been done by now. 23820747Sdavidn */ 23920267Sjoerg ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS || 24020267Sjoerg newval == ZFS_SYNC_DISABLED); 24120747Sdavidn 24220267Sjoerg os->os_sync = newval; 24320267Sjoerg if (os->os_zil) 24420267Sjoerg zil_set_sync(os->os_zil, newval); 24520267Sjoerg} 24620267Sjoerg 24720267Sjoergstatic void 24820267Sjoergredundant_metadata_changed_cb(void *arg, uint64_t newval) 24952502Sdavidn{ 25052502Sdavidn objset_t *os = arg; 25152502Sdavidn 25252502Sdavidn /* 25352502Sdavidn * Inheritance and range checking should have been done by now. 25420267Sjoerg */ 25552502Sdavidn ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL || 25652502Sdavidn newval == ZFS_REDUNDANT_METADATA_MOST); 25752502Sdavidn 25852502Sdavidn os->os_redundant_metadata = newval; 25956000Sdavidn} 26052502Sdavidn 26120253Sjoergstatic void 26220253Sjoerglogbias_changed_cb(void *arg, uint64_t newval) 26344229Sdavidn{ 26430259Scharnier objset_t *os = arg; 26520253Sjoerg 26620253Sjoerg ASSERT(newval == ZFS_LOGBIAS_LATENCY || 26720253Sjoerg newval == ZFS_LOGBIAS_THROUGHPUT); 26820747Sdavidn os->os_logbias = newval; 26920747Sdavidn if (os->os_zil) 27020747Sdavidn zil_set_logbias(os->os_zil, newval); 27120267Sjoerg} 27220253Sjoerg 27320253Sjoergstatic void 27420253Sjoergrecordsize_changed_cb(void *arg, uint64_t newval) 27520253Sjoerg{ 27620253Sjoerg objset_t *os = arg; 27720253Sjoerg 27820253Sjoerg os->os_recordsize = newval; 27920253Sjoerg} 28020253Sjoerg 28120253Sjoergvoid 28220253Sjoergdmu_objset_byteswap(void *buf, size_t size) 28320253Sjoerg{ 28420253Sjoerg objset_phys_t *osp = buf; 28520253Sjoerg 28620253Sjoerg ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t)); 28720253Sjoerg dnode_byteswap(&osp->os_meta_dnode); 28844229Sdavidn byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t)); 28930259Scharnier osp->os_type = BSWAP_64(osp->os_type); 29020253Sjoerg osp->os_flags = BSWAP_64(osp->os_flags); 29120253Sjoerg if (size == sizeof (objset_phys_t)) { 29220253Sjoerg dnode_byteswap(&osp->os_userused_dnode); 29320253Sjoerg dnode_byteswap(&osp->os_groupused_dnode); 29420253Sjoerg } 29520253Sjoerg} 29620253Sjoerg 29720253Sjoergint 29820253Sjoergdmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 29920253Sjoerg objset_t **osp) 30020253Sjoerg{ 30120253Sjoerg objset_t *os; 30220253Sjoerg int i, err; 30320253Sjoerg 30420253Sjoerg ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock)); 30520253Sjoerg 30620253Sjoerg os = kmem_zalloc(sizeof (objset_t), KM_SLEEP); 30744229Sdavidn os->os_dsl_dataset = ds; 30844229Sdavidn os->os_spa = spa; 30956000Sdavidn os->os_rootbp = bp; 31056000Sdavidn if (!BP_IS_HOLE(os->os_rootbp)) { 31120253Sjoerg arc_flags_t aflags = ARC_FLAG_WAIT; 31244229Sdavidn zbookmark_phys_t zb; 31320253Sjoerg SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET, 31420253Sjoerg ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 31520253Sjoerg 31620253Sjoerg if (DMU_OS_IS_L2CACHEABLE(os)) 31720253Sjoerg aflags |= ARC_FLAG_L2CACHE; 31820253Sjoerg if (DMU_OS_IS_L2COMPRESSIBLE(os)) 31920253Sjoerg aflags |= ARC_FLAG_L2COMPRESS; 32020253Sjoerg 32120253Sjoerg dprintf_bp(os->os_rootbp, "reading %s", ""); 32220253Sjoerg err = arc_read(NULL, spa, os->os_rootbp, 32320253Sjoerg arc_getbuf_func, &os->os_phys_buf, 32420253Sjoerg ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb); 32520253Sjoerg if (err != 0) { 32620253Sjoerg kmem_free(os, sizeof (objset_t)); 32720253Sjoerg /* convert checksum errors into IO errors */ 32820253Sjoerg if (err == ECKSUM) 32920253Sjoerg err = SET_ERROR(EIO); 33020253Sjoerg return (err); 33130259Scharnier } 33220253Sjoerg 33320253Sjoerg /* Increase the blocksize if we are permitted. */ 33420253Sjoerg if (spa_version(spa) >= SPA_VERSION_USERSPACE && 33520253Sjoerg arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) { 33620253Sjoerg arc_buf_t *buf = arc_buf_alloc(spa, 33720253Sjoerg sizeof (objset_phys_t), &os->os_phys_buf, 33820253Sjoerg ARC_BUFC_METADATA); 33920253Sjoerg bzero(buf->b_data, sizeof (objset_phys_t)); 34020253Sjoerg bcopy(os->os_phys_buf->b_data, buf->b_data, 34120253Sjoerg arc_buf_size(os->os_phys_buf)); 34220747Sdavidn (void) arc_buf_remove_ref(os->os_phys_buf, 34320747Sdavidn &os->os_phys_buf); 34420253Sjoerg os->os_phys_buf = buf; 34520747Sdavidn } 34620253Sjoerg 34720747Sdavidn os->os_phys = os->os_phys_buf->b_data; 34820253Sjoerg os->os_flags = os->os_phys->os_flags; 34920253Sjoerg } else { 35020253Sjoerg int size = spa_version(spa) >= SPA_VERSION_USERSPACE ? 35122398Sdavidn sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE; 35220747Sdavidn os->os_phys_buf = arc_buf_alloc(spa, size, 35320253Sjoerg &os->os_phys_buf, ARC_BUFC_METADATA); 35420747Sdavidn os->os_phys = os->os_phys_buf->b_data; 35520253Sjoerg bzero(os->os_phys, size); 35620253Sjoerg } 35720253Sjoerg 35820267Sjoerg /* 35920253Sjoerg * Note: the changed_cb will be called once before the register 360 * func returns, thus changing the checksum/compression from the 361 * default (fletcher2/off). Snapshots don't need to know about 362 * checksum/compression/copies. 363 */ 364 if (ds != NULL) { 365 err = dsl_prop_register(ds, 366 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE), 367 primary_cache_changed_cb, os); 368 if (err == 0) { 369 err = dsl_prop_register(ds, 370 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE), 371 secondary_cache_changed_cb, os); 372 } 373 if (!ds->ds_is_snapshot) { 374 if (err == 0) { 375 err = dsl_prop_register(ds, 376 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 377 checksum_changed_cb, os); 378 } 379 if (err == 0) { 380 err = dsl_prop_register(ds, 381 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 382 compression_changed_cb, os); 383 } 384 if (err == 0) { 385 err = dsl_prop_register(ds, 386 zfs_prop_to_name(ZFS_PROP_COPIES), 387 copies_changed_cb, os); 388 } 389 if (err == 0) { 390 err = dsl_prop_register(ds, 391 zfs_prop_to_name(ZFS_PROP_DEDUP), 392 dedup_changed_cb, os); 393 } 394 if (err == 0) { 395 err = dsl_prop_register(ds, 396 zfs_prop_to_name(ZFS_PROP_LOGBIAS), 397 logbias_changed_cb, os); 398 } 399 if (err == 0) { 400 err = dsl_prop_register(ds, 401 zfs_prop_to_name(ZFS_PROP_SYNC), 402 sync_changed_cb, os); 403 } 404 if (err == 0) { 405 err = dsl_prop_register(ds, 406 zfs_prop_to_name( 407 ZFS_PROP_REDUNDANT_METADATA), 408 redundant_metadata_changed_cb, os); 409 } 410 if (err == 0) { 411 err = dsl_prop_register(ds, 412 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), 413 recordsize_changed_cb, os); 414 } 415 } 416 if (err != 0) { 417 VERIFY(arc_buf_remove_ref(os->os_phys_buf, 418 &os->os_phys_buf)); 419 kmem_free(os, sizeof (objset_t)); 420 return (err); 421 } 422 } else { 423 /* It's the meta-objset. */ 424 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4; 425 os->os_compress = ZIO_COMPRESS_ON; 426 os->os_copies = spa_max_replication(spa); 427 os->os_dedup_checksum = ZIO_CHECKSUM_OFF; 428 os->os_dedup_verify = B_FALSE; 429 os->os_logbias = ZFS_LOGBIAS_LATENCY; 430 os->os_sync = ZFS_SYNC_STANDARD; 431 os->os_primary_cache = ZFS_CACHE_ALL; 432 os->os_secondary_cache = ZFS_CACHE_ALL; 433 } 434 435 if (ds == NULL || !ds->ds_is_snapshot) 436 os->os_zil_header = os->os_phys->os_zil_header; 437 os->os_zil = zil_alloc(os, &os->os_zil_header); 438 439 for (i = 0; i < TXG_SIZE; i++) { 440 list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t), 441 offsetof(dnode_t, dn_dirty_link[i])); 442 list_create(&os->os_free_dnodes[i], sizeof (dnode_t), 443 offsetof(dnode_t, dn_dirty_link[i])); 444 } 445 list_create(&os->os_dnodes, sizeof (dnode_t), 446 offsetof(dnode_t, dn_link)); 447 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t), 448 offsetof(dmu_buf_impl_t, db_link)); 449 450 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL); 451 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL); 452 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL); 453 454 dnode_special_open(os, &os->os_phys->os_meta_dnode, 455 DMU_META_DNODE_OBJECT, &os->os_meta_dnode); 456 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) { 457 dnode_special_open(os, &os->os_phys->os_userused_dnode, 458 DMU_USERUSED_OBJECT, &os->os_userused_dnode); 459 dnode_special_open(os, &os->os_phys->os_groupused_dnode, 460 DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode); 461 } 462 463 *osp = os; 464 return (0); 465} 466 467int 468dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp) 469{ 470 int err = 0; 471 472 mutex_enter(&ds->ds_opening_lock); 473 if (ds->ds_objset == NULL) { 474 objset_t *os; 475 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds), 476 ds, dsl_dataset_get_blkptr(ds), &os); 477 478 if (err == 0) { 479 mutex_enter(&ds->ds_lock); 480 ASSERT(ds->ds_objset == NULL); 481 ds->ds_objset = os; 482 mutex_exit(&ds->ds_lock); 483 } 484 } 485 *osp = ds->ds_objset; 486 mutex_exit(&ds->ds_opening_lock); 487 return (err); 488} 489 490/* 491 * Holds the pool while the objset is held. Therefore only one objset 492 * can be held at a time. 493 */ 494int 495dmu_objset_hold(const char *name, void *tag, objset_t **osp) 496{ 497 dsl_pool_t *dp; 498 dsl_dataset_t *ds; 499 int err; 500 501 err = dsl_pool_hold(name, tag, &dp); 502 if (err != 0) 503 return (err); 504 err = dsl_dataset_hold(dp, name, tag, &ds); 505 if (err != 0) { 506 dsl_pool_rele(dp, tag); 507 return (err); 508 } 509 510 err = dmu_objset_from_ds(ds, osp); 511 if (err != 0) { 512 dsl_dataset_rele(ds, tag); 513 dsl_pool_rele(dp, tag); 514 } 515 516 return (err); 517} 518 519static int 520dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type, 521 boolean_t readonly, void *tag, objset_t **osp) 522{ 523 int err; 524 525 err = dmu_objset_from_ds(ds, osp); 526 if (err != 0) { 527 dsl_dataset_disown(ds, tag); 528 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) { 529 dsl_dataset_disown(ds, tag); 530 return (SET_ERROR(EINVAL)); 531 } else if (!readonly && dsl_dataset_is_snapshot(ds)) { 532 dsl_dataset_disown(ds, tag); 533 return (SET_ERROR(EROFS)); 534 } 535 return (err); 536} 537 538/* 539 * dsl_pool must not be held when this is called. 540 * Upon successful return, there will be a longhold on the dataset, 541 * and the dsl_pool will not be held. 542 */ 543int 544dmu_objset_own(const char *name, dmu_objset_type_t type, 545 boolean_t readonly, void *tag, objset_t **osp) 546{ 547 dsl_pool_t *dp; 548 dsl_dataset_t *ds; 549 int err; 550 551 err = dsl_pool_hold(name, FTAG, &dp); 552 if (err != 0) 553 return (err); 554 err = dsl_dataset_own(dp, name, tag, &ds); 555 if (err != 0) { 556 dsl_pool_rele(dp, FTAG); 557 return (err); 558 } 559 err = dmu_objset_own_impl(ds, type, readonly, tag, osp); 560 dsl_pool_rele(dp, FTAG); 561 562 return (err); 563} 564 565int 566dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type, 567 boolean_t readonly, void *tag, objset_t **osp) 568{ 569 dsl_dataset_t *ds; 570 int err; 571 572 err = dsl_dataset_own_obj(dp, obj, tag, &ds); 573 if (err != 0) 574 return (err); 575 576 return (dmu_objset_own_impl(ds, type, readonly, tag, osp)); 577} 578 579void 580dmu_objset_rele(objset_t *os, void *tag) 581{ 582 dsl_pool_t *dp = dmu_objset_pool(os); 583 dsl_dataset_rele(os->os_dsl_dataset, tag); 584 dsl_pool_rele(dp, tag); 585} 586 587/* 588 * When we are called, os MUST refer to an objset associated with a dataset 589 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner 590 * == tag. We will then release and reacquire ownership of the dataset while 591 * holding the pool config_rwlock to avoid intervening namespace or ownership 592 * changes may occur. 593 * 594 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to 595 * release the hold on its dataset and acquire a new one on the dataset of the 596 * same name so that it can be partially torn down and reconstructed. 597 */ 598void 599dmu_objset_refresh_ownership(objset_t *os, void *tag) 600{ 601 dsl_pool_t *dp; 602 dsl_dataset_t *ds, *newds; 603 char name[MAXNAMELEN]; 604 605 ds = os->os_dsl_dataset; 606 VERIFY3P(ds, !=, NULL); 607 VERIFY3P(ds->ds_owner, ==, tag); 608 VERIFY(dsl_dataset_long_held(ds)); 609 610 dsl_dataset_name(ds, name); 611 dp = dmu_objset_pool(os); 612 dsl_pool_config_enter(dp, FTAG); 613 dmu_objset_disown(os, tag); 614 VERIFY0(dsl_dataset_own(dp, name, tag, &newds)); 615 VERIFY3P(newds, ==, os->os_dsl_dataset); 616 dsl_pool_config_exit(dp, FTAG); 617} 618 619void 620dmu_objset_disown(objset_t *os, void *tag) 621{ 622 dsl_dataset_disown(os->os_dsl_dataset, tag); 623} 624 625void 626dmu_objset_evict_dbufs(objset_t *os) 627{ 628 dnode_t dn_marker; 629 dnode_t *dn; 630 631 mutex_enter(&os->os_lock); 632 dn = list_head(&os->os_dnodes); 633 while (dn != NULL) { 634 /* 635 * Skip dnodes without holds. We have to do this dance 636 * because dnode_add_ref() only works if there is already a 637 * hold. If the dnode has no holds, then it has no dbufs. 638 */ 639 if (dnode_add_ref(dn, FTAG)) { 640 list_insert_after(&os->os_dnodes, dn, &dn_marker); 641 mutex_exit(&os->os_lock); 642 643 dnode_evict_dbufs(dn); 644 dnode_rele(dn, FTAG); 645 646 mutex_enter(&os->os_lock); 647 dn = list_next(&os->os_dnodes, &dn_marker); 648 list_remove(&os->os_dnodes, &dn_marker); 649 } else { 650 dn = list_next(&os->os_dnodes, dn); 651 } 652 } 653 mutex_exit(&os->os_lock); 654 655 if (DMU_USERUSED_DNODE(os) != NULL) { 656 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os)); 657 dnode_evict_dbufs(DMU_USERUSED_DNODE(os)); 658 } 659 dnode_evict_dbufs(DMU_META_DNODE(os)); 660} 661 662/* 663 * Objset eviction processing is split into into two pieces. 664 * The first marks the objset as evicting, evicts any dbufs that 665 * have a refcount of zero, and then queues up the objset for the 666 * second phase of eviction. Once os->os_dnodes has been cleared by 667 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed. 668 * The second phase closes the special dnodes, dequeues the objset from 669 * the list of those undergoing eviction, and finally frees the objset. 670 * 671 * NOTE: Due to asynchronous eviction processing (invocation of 672 * dnode_buf_pageout()), it is possible for the meta dnode for the 673 * objset to have no holds even though os->os_dnodes is not empty. 674 */ 675void 676dmu_objset_evict(objset_t *os) 677{ 678 dsl_dataset_t *ds = os->os_dsl_dataset; 679 680 for (int t = 0; t < TXG_SIZE; t++) 681 ASSERT(!dmu_objset_is_dirty(os, t)); 682 683 if (ds) 684 dsl_prop_unregister_all(ds, os); 685 686 if (os->os_sa) 687 sa_tear_down(os); 688 689 dmu_objset_evict_dbufs(os); 690 691 mutex_enter(&os->os_lock); 692 spa_evicting_os_register(os->os_spa, os); 693 if (list_is_empty(&os->os_dnodes)) { 694 mutex_exit(&os->os_lock); 695 dmu_objset_evict_done(os); 696 } else { 697 mutex_exit(&os->os_lock); 698 } 699} 700 701void 702dmu_objset_evict_done(objset_t *os) 703{ 704 ASSERT3P(list_head(&os->os_dnodes), ==, NULL); 705 706 dnode_special_close(&os->os_meta_dnode); 707 if (DMU_USERUSED_DNODE(os)) { 708 dnode_special_close(&os->os_userused_dnode); 709 dnode_special_close(&os->os_groupused_dnode); 710 } 711 zil_free(os->os_zil); 712 713 VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf)); 714 715 /* 716 * This is a barrier to prevent the objset from going away in 717 * dnode_move() until we can safely ensure that the objset is still in 718 * use. We consider the objset valid before the barrier and invalid 719 * after the barrier. 720 */ 721 rw_enter(&os_lock, RW_READER); 722 rw_exit(&os_lock); 723 724 mutex_destroy(&os->os_lock); 725 mutex_destroy(&os->os_obj_lock); 726 mutex_destroy(&os->os_user_ptr_lock); 727 spa_evicting_os_deregister(os->os_spa, os); 728 kmem_free(os, sizeof (objset_t)); 729} 730 731timestruc_t 732dmu_objset_snap_cmtime(objset_t *os) 733{ 734 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir)); 735} 736 737/* called from dsl for meta-objset */ 738objset_t * 739dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp, 740 dmu_objset_type_t type, dmu_tx_t *tx) 741{ 742 objset_t *os; 743 dnode_t *mdn; 744 745 ASSERT(dmu_tx_is_syncing(tx)); 746 747 if (ds != NULL) 748 VERIFY0(dmu_objset_from_ds(ds, &os)); 749 else 750 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os)); 751 752 mdn = DMU_META_DNODE(os); 753 754 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT, 755 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx); 756 757 /* 758 * We don't want to have to increase the meta-dnode's nlevels 759 * later, because then we could do it in quescing context while 760 * we are also accessing it in open context. 761 * 762 * This precaution is not necessary for the MOS (ds == NULL), 763 * because the MOS is only updated in syncing context. 764 * This is most fortunate: the MOS is the only objset that 765 * needs to be synced multiple times as spa_sync() iterates 766 * to convergence, so minimizing its dn_nlevels matters. 767 */ 768 if (ds != NULL) { 769 int levels = 1; 770 771 /* 772 * Determine the number of levels necessary for the meta-dnode 773 * to contain DN_MAX_OBJECT dnodes. 774 */ 775 while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift + 776 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) < 777 DN_MAX_OBJECT * sizeof (dnode_phys_t)) 778 levels++; 779 780 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] = 781 mdn->dn_nlevels = levels; 782 } 783 784 ASSERT(type != DMU_OST_NONE); 785 ASSERT(type != DMU_OST_ANY); 786 ASSERT(type < DMU_OST_NUMTYPES); 787 os->os_phys->os_type = type; 788 if (dmu_objset_userused_enabled(os)) { 789 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 790 os->os_flags = os->os_phys->os_flags; 791 } 792 793 dsl_dataset_dirty(ds, tx); 794 795 return (os); 796} 797 798typedef struct dmu_objset_create_arg { 799 const char *doca_name; 800 cred_t *doca_cred; 801 void (*doca_userfunc)(objset_t *os, void *arg, 802 cred_t *cr, dmu_tx_t *tx); 803 void *doca_userarg; 804 dmu_objset_type_t doca_type; 805 uint64_t doca_flags; 806} dmu_objset_create_arg_t; 807 808/*ARGSUSED*/ 809static int 810dmu_objset_create_check(void *arg, dmu_tx_t *tx) 811{ 812 dmu_objset_create_arg_t *doca = arg; 813 dsl_pool_t *dp = dmu_tx_pool(tx); 814 dsl_dir_t *pdd; 815 const char *tail; 816 int error; 817 818 if (strchr(doca->doca_name, '@') != NULL) 819 return (SET_ERROR(EINVAL)); 820 821 error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail); 822 if (error != 0) 823 return (error); 824 if (tail == NULL) { 825 dsl_dir_rele(pdd, FTAG); 826 return (SET_ERROR(EEXIST)); 827 } 828 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, 829 doca->doca_cred); 830 dsl_dir_rele(pdd, FTAG); 831 832 return (error); 833} 834 835static void 836dmu_objset_create_sync(void *arg, dmu_tx_t *tx) 837{ 838 dmu_objset_create_arg_t *doca = arg; 839 dsl_pool_t *dp = dmu_tx_pool(tx); 840 dsl_dir_t *pdd; 841 const char *tail; 842 dsl_dataset_t *ds; 843 uint64_t obj; 844 blkptr_t *bp; 845 objset_t *os; 846 847 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail)); 848 849 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags, 850 doca->doca_cred, tx); 851 852 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds)); 853 bp = dsl_dataset_get_blkptr(ds); 854 os = dmu_objset_create_impl(pdd->dd_pool->dp_spa, 855 ds, bp, doca->doca_type, tx); 856 857 if (doca->doca_userfunc != NULL) { 858 doca->doca_userfunc(os, doca->doca_userarg, 859 doca->doca_cred, tx); 860 } 861 862 spa_history_log_internal_ds(ds, "create", tx, ""); 863 dsl_dataset_rele(ds, FTAG); 864 dsl_dir_rele(pdd, FTAG); 865} 866 867int 868dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags, 869 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg) 870{ 871 dmu_objset_create_arg_t doca; 872 873 doca.doca_name = name; 874 doca.doca_cred = CRED(); 875 doca.doca_flags = flags; 876 doca.doca_userfunc = func; 877 doca.doca_userarg = arg; 878 doca.doca_type = type; 879 880 return (dsl_sync_task(name, 881 dmu_objset_create_check, dmu_objset_create_sync, &doca, 882 5, ZFS_SPACE_CHECK_NORMAL)); 883} 884 885typedef struct dmu_objset_clone_arg { 886 const char *doca_clone; 887 const char *doca_origin; 888 cred_t *doca_cred; 889} dmu_objset_clone_arg_t; 890 891/*ARGSUSED*/ 892static int 893dmu_objset_clone_check(void *arg, dmu_tx_t *tx) 894{ 895 dmu_objset_clone_arg_t *doca = arg; 896 dsl_dir_t *pdd; 897 const char *tail; 898 int error; 899 dsl_dataset_t *origin; 900 dsl_pool_t *dp = dmu_tx_pool(tx); 901 902 if (strchr(doca->doca_clone, '@') != NULL) 903 return (SET_ERROR(EINVAL)); 904 905 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail); 906 if (error != 0) 907 return (error); 908 if (tail == NULL) { 909 dsl_dir_rele(pdd, FTAG); 910 return (SET_ERROR(EEXIST)); 911 } 912 913 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL, 914 doca->doca_cred); 915 if (error != 0) { 916 dsl_dir_rele(pdd, FTAG); 917 return (SET_ERROR(EDQUOT)); 918 } 919 dsl_dir_rele(pdd, FTAG); 920 921 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin); 922 if (error != 0) 923 return (error); 924 925 /* You can only clone snapshots, not the head datasets. */ 926 if (!origin->ds_is_snapshot) { 927 dsl_dataset_rele(origin, FTAG); 928 return (SET_ERROR(EINVAL)); 929 } 930 dsl_dataset_rele(origin, FTAG); 931 932 return (0); 933} 934 935static void 936dmu_objset_clone_sync(void *arg, dmu_tx_t *tx) 937{ 938 dmu_objset_clone_arg_t *doca = arg; 939 dsl_pool_t *dp = dmu_tx_pool(tx); 940 dsl_dir_t *pdd; 941 const char *tail; 942 dsl_dataset_t *origin, *ds; 943 uint64_t obj; 944 char namebuf[MAXNAMELEN]; 945 946 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail)); 947 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin)); 948 949 obj = dsl_dataset_create_sync(pdd, tail, origin, 0, 950 doca->doca_cred, tx); 951 952 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds)); 953 dsl_dataset_name(origin, namebuf); 954 spa_history_log_internal_ds(ds, "clone", tx, 955 "origin=%s (%llu)", namebuf, origin->ds_object); 956 dsl_dataset_rele(ds, FTAG); 957 dsl_dataset_rele(origin, FTAG); 958 dsl_dir_rele(pdd, FTAG); 959} 960 961int 962dmu_objset_clone(const char *clone, const char *origin) 963{ 964 dmu_objset_clone_arg_t doca; 965 966 doca.doca_clone = clone; 967 doca.doca_origin = origin; 968 doca.doca_cred = CRED(); 969 970 return (dsl_sync_task(clone, 971 dmu_objset_clone_check, dmu_objset_clone_sync, &doca, 972 5, ZFS_SPACE_CHECK_NORMAL)); 973} 974 975int 976dmu_objset_snapshot_one(const char *fsname, const char *snapname) 977{ 978 int err; 979 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname); 980 nvlist_t *snaps = fnvlist_alloc(); 981 982 fnvlist_add_boolean(snaps, longsnap); 983 strfree(longsnap); 984 err = dsl_dataset_snapshot(snaps, NULL, NULL); 985 fnvlist_free(snaps); 986 return (err); 987} 988 989static void 990dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx) 991{ 992 dnode_t *dn; 993 994 while (dn = list_head(list)) { 995 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); 996 ASSERT(dn->dn_dbuf->db_data_pending); 997 /* 998 * Initialize dn_zio outside dnode_sync() because the 999 * meta-dnode needs to set it ouside dnode_sync(). 1000 */ 1001 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio; 1002 ASSERT(dn->dn_zio); 1003 1004 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS); 1005 list_remove(list, dn); 1006 1007 if (newlist) { 1008 (void) dnode_add_ref(dn, newlist); 1009 list_insert_tail(newlist, dn); 1010 } 1011 1012 dnode_sync(dn, tx); 1013 } 1014} 1015 1016/* ARGSUSED */ 1017static void 1018dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg) 1019{ 1020 blkptr_t *bp = zio->io_bp; 1021 objset_t *os = arg; 1022 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode; 1023 1024 ASSERT(!BP_IS_EMBEDDED(bp)); 1025 ASSERT3P(bp, ==, os->os_rootbp); 1026 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET); 1027 ASSERT0(BP_GET_LEVEL(bp)); 1028 1029 /* 1030 * Update rootbp fill count: it should be the number of objects 1031 * allocated in the object set (not counting the "special" 1032 * objects that are stored in the objset_phys_t -- the meta 1033 * dnode and user/group accounting objects). 1034 */ 1035 bp->blk_fill = 0; 1036 for (int i = 0; i < dnp->dn_nblkptr; i++) 1037 bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]); 1038} 1039 1040/* ARGSUSED */ 1041static void 1042dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg) 1043{ 1044 blkptr_t *bp = zio->io_bp; 1045 blkptr_t *bp_orig = &zio->io_bp_orig; 1046 objset_t *os = arg; 1047 1048 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) { 1049 ASSERT(BP_EQUAL(bp, bp_orig)); 1050 } else { 1051 dsl_dataset_t *ds = os->os_dsl_dataset; 1052 dmu_tx_t *tx = os->os_synctx; 1053 1054 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE); 1055 dsl_dataset_block_born(ds, bp, tx); 1056 } 1057} 1058 1059/* called from dsl */ 1060void 1061dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx) 1062{ 1063 int txgoff; 1064 zbookmark_phys_t zb; 1065 zio_prop_t zp; 1066 zio_t *zio; 1067 list_t *list; 1068 list_t *newlist = NULL; 1069 dbuf_dirty_record_t *dr; 1070 1071 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg); 1072 1073 ASSERT(dmu_tx_is_syncing(tx)); 1074 /* XXX the write_done callback should really give us the tx... */ 1075 os->os_synctx = tx; 1076 1077 if (os->os_dsl_dataset == NULL) { 1078 /* 1079 * This is the MOS. If we have upgraded, 1080 * spa_max_replication() could change, so reset 1081 * os_copies here. 1082 */ 1083 os->os_copies = spa_max_replication(os->os_spa); 1084 } 1085 1086 /* 1087 * Create the root block IO 1088 */ 1089 SET_BOOKMARK(&zb, os->os_dsl_dataset ? 1090 os->os_dsl_dataset->ds_object : DMU_META_OBJSET, 1091 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID); 1092 arc_release(os->os_phys_buf, &os->os_phys_buf); 1093 1094 dmu_write_policy(os, NULL, 0, 0, &zp); 1095 1096 zio = arc_write(pio, os->os_spa, tx->tx_txg, 1097 os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), 1098 DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready, 1099 NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE, 1100 ZIO_FLAG_MUSTSUCCEED, &zb); 1101 1102 /* 1103 * Sync special dnodes - the parent IO for the sync is the root block 1104 */ 1105 DMU_META_DNODE(os)->dn_zio = zio; 1106 dnode_sync(DMU_META_DNODE(os), tx); 1107 1108 os->os_phys->os_flags = os->os_flags; 1109 1110 if (DMU_USERUSED_DNODE(os) && 1111 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) { 1112 DMU_USERUSED_DNODE(os)->dn_zio = zio; 1113 dnode_sync(DMU_USERUSED_DNODE(os), tx); 1114 DMU_GROUPUSED_DNODE(os)->dn_zio = zio; 1115 dnode_sync(DMU_GROUPUSED_DNODE(os), tx); 1116 } 1117 1118 txgoff = tx->tx_txg & TXG_MASK; 1119 1120 if (dmu_objset_userused_enabled(os)) { 1121 newlist = &os->os_synced_dnodes; 1122 /* 1123 * We must create the list here because it uses the 1124 * dn_dirty_link[] of this txg. 1125 */ 1126 list_create(newlist, sizeof (dnode_t), 1127 offsetof(dnode_t, dn_dirty_link[txgoff])); 1128 } 1129 1130 dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx); 1131 dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx); 1132 1133 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff]; 1134 while (dr = list_head(list)) { 1135 ASSERT0(dr->dr_dbuf->db_level); 1136 list_remove(list, dr); 1137 if (dr->dr_zio) 1138 zio_nowait(dr->dr_zio); 1139 } 1140 /* 1141 * Free intent log blocks up to this tx. 1142 */ 1143 zil_sync(os->os_zil, tx); 1144 os->os_phys->os_zil_header = os->os_zil_header; 1145 zio_nowait(zio); 1146} 1147 1148boolean_t 1149dmu_objset_is_dirty(objset_t *os, uint64_t txg) 1150{ 1151 return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) || 1152 !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK])); 1153} 1154 1155static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES]; 1156 1157void 1158dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb) 1159{ 1160 used_cbs[ost] = cb; 1161} 1162 1163boolean_t 1164dmu_objset_userused_enabled(objset_t *os) 1165{ 1166 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE && 1167 used_cbs[os->os_phys->os_type] != NULL && 1168 DMU_USERUSED_DNODE(os) != NULL); 1169} 1170 1171static void 1172do_userquota_update(objset_t *os, uint64_t used, uint64_t flags, 1173 uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx) 1174{ 1175 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) { 1176 int64_t delta = DNODE_SIZE + used; 1177 if (subtract) 1178 delta = -delta; 1179 VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT, 1180 user, delta, tx)); 1181 VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT, 1182 group, delta, tx)); 1183 } 1184} 1185 1186void 1187dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx) 1188{ 1189 dnode_t *dn; 1190 list_t *list = &os->os_synced_dnodes; 1191 1192 ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os)); 1193 1194 while (dn = list_head(list)) { 1195 int flags; 1196 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object)); 1197 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE || 1198 dn->dn_phys->dn_flags & 1199 DNODE_FLAG_USERUSED_ACCOUNTED); 1200 1201 /* Allocate the user/groupused objects if necessary. */ 1202 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) { 1203 VERIFY(0 == zap_create_claim(os, 1204 DMU_USERUSED_OBJECT, 1205 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1206 VERIFY(0 == zap_create_claim(os, 1207 DMU_GROUPUSED_OBJECT, 1208 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx)); 1209 } 1210 1211 /* 1212 * We intentionally modify the zap object even if the 1213 * net delta is zero. Otherwise 1214 * the block of the zap obj could be shared between 1215 * datasets but need to be different between them after 1216 * a bprewrite. 1217 */ 1218 1219 flags = dn->dn_id_flags; 1220 ASSERT(flags); 1221 if (flags & DN_ID_OLD_EXIST) { 1222 do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags, 1223 dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx); 1224 } 1225 if (flags & DN_ID_NEW_EXIST) { 1226 do_userquota_update(os, DN_USED_BYTES(dn->dn_phys), 1227 dn->dn_phys->dn_flags, dn->dn_newuid, 1228 dn->dn_newgid, B_FALSE, tx); 1229 } 1230 1231 mutex_enter(&dn->dn_mtx); 1232 dn->dn_oldused = 0; 1233 dn->dn_oldflags = 0; 1234 if (dn->dn_id_flags & DN_ID_NEW_EXIST) { 1235 dn->dn_olduid = dn->dn_newuid; 1236 dn->dn_oldgid = dn->dn_newgid; 1237 dn->dn_id_flags |= DN_ID_OLD_EXIST; 1238 if (dn->dn_bonuslen == 0) 1239 dn->dn_id_flags |= DN_ID_CHKED_SPILL; 1240 else 1241 dn->dn_id_flags |= DN_ID_CHKED_BONUS; 1242 } 1243 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST); 1244 mutex_exit(&dn->dn_mtx); 1245 1246 list_remove(list, dn); 1247 dnode_rele(dn, list); 1248 } 1249} 1250 1251/* 1252 * Returns a pointer to data to find uid/gid from 1253 * 1254 * If a dirty record for transaction group that is syncing can't 1255 * be found then NULL is returned. In the NULL case it is assumed 1256 * the uid/gid aren't changing. 1257 */ 1258static void * 1259dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx) 1260{ 1261 dbuf_dirty_record_t *dr, **drp; 1262 void *data; 1263 1264 if (db->db_dirtycnt == 0) 1265 return (db->db.db_data); /* Nothing is changing */ 1266 1267 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next) 1268 if (dr->dr_txg == tx->tx_txg) 1269 break; 1270 1271 if (dr == NULL) { 1272 data = NULL; 1273 } else { 1274 dnode_t *dn; 1275 1276 DB_DNODE_ENTER(dr->dr_dbuf); 1277 dn = DB_DNODE(dr->dr_dbuf); 1278 1279 if (dn->dn_bonuslen == 0 && 1280 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID) 1281 data = dr->dt.dl.dr_data->b_data; 1282 else 1283 data = dr->dt.dl.dr_data; 1284 1285 DB_DNODE_EXIT(dr->dr_dbuf); 1286 } 1287 1288 return (data); 1289} 1290 1291void 1292dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx) 1293{ 1294 objset_t *os = dn->dn_objset; 1295 void *data = NULL; 1296 dmu_buf_impl_t *db = NULL; 1297 uint64_t *user = NULL; 1298 uint64_t *group = NULL; 1299 int flags = dn->dn_id_flags; 1300 int error; 1301 boolean_t have_spill = B_FALSE; 1302 1303 if (!dmu_objset_userused_enabled(dn->dn_objset)) 1304 return; 1305 1306 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST| 1307 DN_ID_CHKED_SPILL))) 1308 return; 1309 1310 if (before && dn->dn_bonuslen != 0) 1311 data = DN_BONUS(dn->dn_phys); 1312 else if (!before && dn->dn_bonuslen != 0) { 1313 if (dn->dn_bonus) { 1314 db = dn->dn_bonus; 1315 mutex_enter(&db->db_mtx); 1316 data = dmu_objset_userquota_find_data(db, tx); 1317 } else { 1318 data = DN_BONUS(dn->dn_phys); 1319 } 1320 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) { 1321 int rf = 0; 1322 1323 if (RW_WRITE_HELD(&dn->dn_struct_rwlock)) 1324 rf |= DB_RF_HAVESTRUCT; 1325 error = dmu_spill_hold_by_dnode(dn, 1326 rf | DB_RF_MUST_SUCCEED, 1327 FTAG, (dmu_buf_t **)&db); 1328 ASSERT(error == 0); 1329 mutex_enter(&db->db_mtx); 1330 data = (before) ? db->db.db_data : 1331 dmu_objset_userquota_find_data(db, tx); 1332 have_spill = B_TRUE; 1333 } else { 1334 mutex_enter(&dn->dn_mtx); 1335 dn->dn_id_flags |= DN_ID_CHKED_BONUS; 1336 mutex_exit(&dn->dn_mtx); 1337 return; 1338 } 1339 1340 if (before) { 1341 ASSERT(data); 1342 user = &dn->dn_olduid; 1343 group = &dn->dn_oldgid; 1344 } else if (data) { 1345 user = &dn->dn_newuid; 1346 group = &dn->dn_newgid; 1347 } 1348 1349 /* 1350 * Must always call the callback in case the object 1351 * type has changed and that type isn't an object type to track 1352 */ 1353 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data, 1354 user, group); 1355 1356 /* 1357 * Preserve existing uid/gid when the callback can't determine 1358 * what the new uid/gid are and the callback returned EEXIST. 1359 * The EEXIST error tells us to just use the existing uid/gid. 1360 * If we don't know what the old values are then just assign 1361 * them to 0, since that is a new file being created. 1362 */ 1363 if (!before && data == NULL && error == EEXIST) { 1364 if (flags & DN_ID_OLD_EXIST) { 1365 dn->dn_newuid = dn->dn_olduid; 1366 dn->dn_newgid = dn->dn_oldgid; 1367 } else { 1368 dn->dn_newuid = 0; 1369 dn->dn_newgid = 0; 1370 } 1371 error = 0; 1372 } 1373 1374 if (db) 1375 mutex_exit(&db->db_mtx); 1376 1377 mutex_enter(&dn->dn_mtx); 1378 if (error == 0 && before) 1379 dn->dn_id_flags |= DN_ID_OLD_EXIST; 1380 if (error == 0 && !before) 1381 dn->dn_id_flags |= DN_ID_NEW_EXIST; 1382 1383 if (have_spill) { 1384 dn->dn_id_flags |= DN_ID_CHKED_SPILL; 1385 } else { 1386 dn->dn_id_flags |= DN_ID_CHKED_BONUS; 1387 } 1388 mutex_exit(&dn->dn_mtx); 1389 if (have_spill) 1390 dmu_buf_rele((dmu_buf_t *)db, FTAG); 1391} 1392 1393boolean_t 1394dmu_objset_userspace_present(objset_t *os) 1395{ 1396 return (os->os_phys->os_flags & 1397 OBJSET_FLAG_USERACCOUNTING_COMPLETE); 1398} 1399 1400int 1401dmu_objset_userspace_upgrade(objset_t *os) 1402{ 1403 uint64_t obj; 1404 int err = 0; 1405 1406 if (dmu_objset_userspace_present(os)) 1407 return (0); 1408 if (!dmu_objset_userused_enabled(os)) 1409 return (SET_ERROR(ENOTSUP)); 1410 if (dmu_objset_is_snapshot(os)) 1411 return (SET_ERROR(EINVAL)); 1412 1413 /* 1414 * We simply need to mark every object dirty, so that it will be 1415 * synced out and now accounted. If this is called 1416 * concurrently, or if we already did some work before crashing, 1417 * that's fine, since we track each object's accounted state 1418 * independently. 1419 */ 1420 1421 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) { 1422 dmu_tx_t *tx; 1423 dmu_buf_t *db; 1424 int objerr; 1425 1426 if (issig(JUSTLOOKING) && issig(FORREAL)) 1427 return (SET_ERROR(EINTR)); 1428 1429 objerr = dmu_bonus_hold(os, obj, FTAG, &db); 1430 if (objerr != 0) 1431 continue; 1432 tx = dmu_tx_create(os); 1433 dmu_tx_hold_bonus(tx, obj); 1434 objerr = dmu_tx_assign(tx, TXG_WAIT); 1435 if (objerr != 0) { 1436 dmu_tx_abort(tx); 1437 continue; 1438 } 1439 dmu_buf_will_dirty(db, tx); 1440 dmu_buf_rele(db, FTAG); 1441 dmu_tx_commit(tx); 1442 } 1443 1444 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE; 1445 txg_wait_synced(dmu_objset_pool(os), 0); 1446 return (0); 1447} 1448 1449void 1450dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp, 1451 uint64_t *usedobjsp, uint64_t *availobjsp) 1452{ 1453 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp, 1454 usedobjsp, availobjsp); 1455} 1456 1457uint64_t 1458dmu_objset_fsid_guid(objset_t *os) 1459{ 1460 return (dsl_dataset_fsid_guid(os->os_dsl_dataset)); 1461} 1462 1463void 1464dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat) 1465{ 1466 stat->dds_type = os->os_phys->os_type; 1467 if (os->os_dsl_dataset) 1468 dsl_dataset_fast_stat(os->os_dsl_dataset, stat); 1469} 1470 1471void 1472dmu_objset_stats(objset_t *os, nvlist_t *nv) 1473{ 1474 ASSERT(os->os_dsl_dataset || 1475 os->os_phys->os_type == DMU_OST_META); 1476 1477 if (os->os_dsl_dataset != NULL) 1478 dsl_dataset_stats(os->os_dsl_dataset, nv); 1479 1480 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE, 1481 os->os_phys->os_type); 1482 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING, 1483 dmu_objset_userspace_present(os)); 1484} 1485 1486int 1487dmu_objset_is_snapshot(objset_t *os) 1488{ 1489 if (os->os_dsl_dataset != NULL) 1490 return (os->os_dsl_dataset->ds_is_snapshot); 1491 else 1492 return (B_FALSE); 1493} 1494 1495int 1496dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen, 1497 boolean_t *conflict) 1498{ 1499 dsl_dataset_t *ds = os->os_dsl_dataset; 1500 uint64_t ignored; 1501 1502 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) 1503 return (SET_ERROR(ENOENT)); 1504 1505 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset, 1506 dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored, 1507 MT_FIRST, real, maxlen, conflict)); 1508} 1509 1510int 1511dmu_snapshot_list_next(objset_t *os, int namelen, char *name, 1512 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict) 1513{ 1514 dsl_dataset_t *ds = os->os_dsl_dataset; 1515 zap_cursor_t cursor; 1516 zap_attribute_t attr; 1517 1518 ASSERT(dsl_pool_config_held(dmu_objset_pool(os))); 1519 1520 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0) 1521 return (SET_ERROR(ENOENT)); 1522 1523 zap_cursor_init_serialized(&cursor, 1524 ds->ds_dir->dd_pool->dp_meta_objset, 1525 dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp); 1526 1527 if (zap_cursor_retrieve(&cursor, &attr) != 0) { 1528 zap_cursor_fini(&cursor); 1529 return (SET_ERROR(ENOENT)); 1530 } 1531 1532 if (strlen(attr.za_name) + 1 > namelen) { 1533 zap_cursor_fini(&cursor); 1534 return (SET_ERROR(ENAMETOOLONG)); 1535 } 1536 1537 (void) strcpy(name, attr.za_name); 1538 if (idp) 1539 *idp = attr.za_first_integer; 1540 if (case_conflict) 1541 *case_conflict = attr.za_normalization_conflict; 1542 zap_cursor_advance(&cursor); 1543 *offp = zap_cursor_serialize(&cursor); 1544 zap_cursor_fini(&cursor); 1545 1546 return (0); 1547} 1548 1549int 1550dmu_dir_list_next(objset_t *os, int namelen, char *name, 1551 uint64_t *idp, uint64_t *offp) 1552{ 1553 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir; 1554 zap_cursor_t cursor; 1555 zap_attribute_t attr; 1556 1557 /* there is no next dir on a snapshot! */ 1558 if (os->os_dsl_dataset->ds_object != 1559 dsl_dir_phys(dd)->dd_head_dataset_obj) 1560 return (SET_ERROR(ENOENT)); 1561 1562 zap_cursor_init_serialized(&cursor, 1563 dd->dd_pool->dp_meta_objset, 1564 dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp); 1565 1566 if (zap_cursor_retrieve(&cursor, &attr) != 0) { 1567 zap_cursor_fini(&cursor); 1568 return (SET_ERROR(ENOENT)); 1569 } 1570 1571 if (strlen(attr.za_name) + 1 > namelen) { 1572 zap_cursor_fini(&cursor); 1573 return (SET_ERROR(ENAMETOOLONG)); 1574 } 1575 1576 (void) strcpy(name, attr.za_name); 1577 if (idp) 1578 *idp = attr.za_first_integer; 1579 zap_cursor_advance(&cursor); 1580 *offp = zap_cursor_serialize(&cursor); 1581 zap_cursor_fini(&cursor); 1582 1583 return (0); 1584} 1585 1586typedef struct dmu_objset_find_ctx { 1587 taskq_t *dc_tq; 1588 dsl_pool_t *dc_dp; 1589 uint64_t dc_ddobj; 1590 int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *); 1591 void *dc_arg; 1592 int dc_flags; 1593 kmutex_t *dc_error_lock; 1594 int *dc_error; 1595} dmu_objset_find_ctx_t; 1596 1597static void 1598dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp) 1599{ 1600 dsl_pool_t *dp = dcp->dc_dp; 1601 dmu_objset_find_ctx_t *child_dcp; 1602 dsl_dir_t *dd; 1603 dsl_dataset_t *ds; 1604 zap_cursor_t zc; 1605 zap_attribute_t *attr; 1606 uint64_t thisobj; 1607 int err = 0; 1608 1609 /* don't process if there already was an error */ 1610 if (*dcp->dc_error != 0) 1611 goto out; 1612 1613 err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd); 1614 if (err != 0) 1615 goto out; 1616 1617 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 1618 if (dd->dd_myname[0] == '$') { 1619 dsl_dir_rele(dd, FTAG); 1620 goto out; 1621 } 1622 1623 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; 1624 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1625 1626 /* 1627 * Iterate over all children. 1628 */ 1629 if (dcp->dc_flags & DS_FIND_CHILDREN) { 1630 for (zap_cursor_init(&zc, dp->dp_meta_objset, 1631 dsl_dir_phys(dd)->dd_child_dir_zapobj); 1632 zap_cursor_retrieve(&zc, attr) == 0; 1633 (void) zap_cursor_advance(&zc)) { 1634 ASSERT3U(attr->za_integer_length, ==, 1635 sizeof (uint64_t)); 1636 ASSERT3U(attr->za_num_integers, ==, 1); 1637 1638 child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP); 1639 *child_dcp = *dcp; 1640 child_dcp->dc_ddobj = attr->za_first_integer; 1641 if (dcp->dc_tq != NULL) 1642 (void) taskq_dispatch(dcp->dc_tq, 1643 dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP); 1644 else 1645 dmu_objset_find_dp_impl(child_dcp); 1646 } 1647 zap_cursor_fini(&zc); 1648 } 1649 1650 /* 1651 * Iterate over all snapshots. 1652 */ 1653 if (dcp->dc_flags & DS_FIND_SNAPSHOTS) { 1654 dsl_dataset_t *ds; 1655 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1656 1657 if (err == 0) { 1658 uint64_t snapobj; 1659 1660 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 1661 dsl_dataset_rele(ds, FTAG); 1662 1663 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 1664 zap_cursor_retrieve(&zc, attr) == 0; 1665 (void) zap_cursor_advance(&zc)) { 1666 ASSERT3U(attr->za_integer_length, ==, 1667 sizeof (uint64_t)); 1668 ASSERT3U(attr->za_num_integers, ==, 1); 1669 1670 err = dsl_dataset_hold_obj(dp, 1671 attr->za_first_integer, FTAG, &ds); 1672 if (err != 0) 1673 break; 1674 err = dcp->dc_func(dp, ds, dcp->dc_arg); 1675 dsl_dataset_rele(ds, FTAG); 1676 if (err != 0) 1677 break; 1678 } 1679 zap_cursor_fini(&zc); 1680 } 1681 } 1682 1683 dsl_dir_rele(dd, FTAG); 1684 kmem_free(attr, sizeof (zap_attribute_t)); 1685 1686 if (err != 0) 1687 goto out; 1688 1689 /* 1690 * Apply to self. 1691 */ 1692 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1693 if (err != 0) 1694 goto out; 1695 err = dcp->dc_func(dp, ds, dcp->dc_arg); 1696 dsl_dataset_rele(ds, FTAG); 1697 1698out: 1699 if (err != 0) { 1700 mutex_enter(dcp->dc_error_lock); 1701 /* only keep first error */ 1702 if (*dcp->dc_error == 0) 1703 *dcp->dc_error = err; 1704 mutex_exit(dcp->dc_error_lock); 1705 } 1706 1707 kmem_free(dcp, sizeof (*dcp)); 1708} 1709 1710static void 1711dmu_objset_find_dp_cb(void *arg) 1712{ 1713 dmu_objset_find_ctx_t *dcp = arg; 1714 dsl_pool_t *dp = dcp->dc_dp; 1715 1716 /* 1717 * We need to get a pool_config_lock here, as there are several 1718 * asssert(pool_config_held) down the stack. Getting a lock via 1719 * dsl_pool_config_enter is risky, as it might be stalled by a 1720 * pending writer. This would deadlock, as the write lock can 1721 * only be granted when our parent thread gives up the lock. 1722 * The _prio interface gives us priority over a pending writer. 1723 */ 1724 dsl_pool_config_enter_prio(dp, FTAG); 1725 1726 dmu_objset_find_dp_impl(dcp); 1727 1728 dsl_pool_config_exit(dp, FTAG); 1729} 1730 1731/* 1732 * Find objsets under and including ddobj, call func(ds) on each. 1733 * The order for the enumeration is completely undefined. 1734 * func is called with dsl_pool_config held. 1735 */ 1736int 1737dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj, 1738 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags) 1739{ 1740 int error = 0; 1741 taskq_t *tq = NULL; 1742 int ntasks; 1743 dmu_objset_find_ctx_t *dcp; 1744 kmutex_t err_lock; 1745 1746 mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL); 1747 dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP); 1748 dcp->dc_tq = NULL; 1749 dcp->dc_dp = dp; 1750 dcp->dc_ddobj = ddobj; 1751 dcp->dc_func = func; 1752 dcp->dc_arg = arg; 1753 dcp->dc_flags = flags; 1754 dcp->dc_error_lock = &err_lock; 1755 dcp->dc_error = &error; 1756 1757 if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) { 1758 /* 1759 * In case a write lock is held we can't make use of 1760 * parallelism, as down the stack of the worker threads 1761 * the lock is asserted via dsl_pool_config_held. 1762 * In case of a read lock this is solved by getting a read 1763 * lock in each worker thread, which isn't possible in case 1764 * of a writer lock. So we fall back to the synchronous path 1765 * here. 1766 * In the future it might be possible to get some magic into 1767 * dsl_pool_config_held in a way that it returns true for 1768 * the worker threads so that a single lock held from this 1769 * thread suffices. For now, stay single threaded. 1770 */ 1771 dmu_objset_find_dp_impl(dcp); 1772 1773 return (error); 1774 } 1775 1776 ntasks = dmu_find_threads; 1777 if (ntasks == 0) 1778 ntasks = vdev_count_leaves(dp->dp_spa) * 4; 1779 tq = taskq_create("dmu_objset_find", ntasks, minclsyspri, ntasks, 1780 INT_MAX, 0); 1781 if (tq == NULL) { 1782 kmem_free(dcp, sizeof (*dcp)); 1783 return (SET_ERROR(ENOMEM)); 1784 } 1785 dcp->dc_tq = tq; 1786 1787 /* dcp will be freed by task */ 1788 (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP); 1789 1790 /* 1791 * PORTING: this code relies on the property of taskq_wait to wait 1792 * until no more tasks are queued and no more tasks are active. As 1793 * we always queue new tasks from within other tasks, task_wait 1794 * reliably waits for the full recursion to finish, even though we 1795 * enqueue new tasks after taskq_wait has been called. 1796 * On platforms other than illumos, taskq_wait may not have this 1797 * property. 1798 */ 1799 taskq_wait(tq); 1800 taskq_destroy(tq); 1801 mutex_destroy(&err_lock); 1802 1803 return (error); 1804} 1805 1806/* 1807 * Find all objsets under name, and for each, call 'func(child_name, arg)'. 1808 * The dp_config_rwlock must not be held when this is called, and it 1809 * will not be held when the callback is called. 1810 * Therefore this function should only be used when the pool is not changing 1811 * (e.g. in syncing context), or the callback can deal with the possible races. 1812 */ 1813static int 1814dmu_objset_find_impl(spa_t *spa, const char *name, 1815 int func(const char *, void *), void *arg, int flags) 1816{ 1817 dsl_dir_t *dd; 1818 dsl_pool_t *dp = spa_get_dsl(spa); 1819 dsl_dataset_t *ds; 1820 zap_cursor_t zc; 1821 zap_attribute_t *attr; 1822 char *child; 1823 uint64_t thisobj; 1824 int err; 1825 1826 dsl_pool_config_enter(dp, FTAG); 1827 1828 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL); 1829 if (err != 0) { 1830 dsl_pool_config_exit(dp, FTAG); 1831 return (err); 1832 } 1833 1834 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */ 1835 if (dd->dd_myname[0] == '$') { 1836 dsl_dir_rele(dd, FTAG); 1837 dsl_pool_config_exit(dp, FTAG); 1838 return (0); 1839 } 1840 1841 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj; 1842 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); 1843 1844 /* 1845 * Iterate over all children. 1846 */ 1847 if (flags & DS_FIND_CHILDREN) { 1848 for (zap_cursor_init(&zc, dp->dp_meta_objset, 1849 dsl_dir_phys(dd)->dd_child_dir_zapobj); 1850 zap_cursor_retrieve(&zc, attr) == 0; 1851 (void) zap_cursor_advance(&zc)) { 1852 ASSERT3U(attr->za_integer_length, ==, 1853 sizeof (uint64_t)); 1854 ASSERT3U(attr->za_num_integers, ==, 1); 1855 1856 child = kmem_asprintf("%s/%s", name, attr->za_name); 1857 dsl_pool_config_exit(dp, FTAG); 1858 err = dmu_objset_find_impl(spa, child, 1859 func, arg, flags); 1860 dsl_pool_config_enter(dp, FTAG); 1861 strfree(child); 1862 if (err != 0) 1863 break; 1864 } 1865 zap_cursor_fini(&zc); 1866 1867 if (err != 0) { 1868 dsl_dir_rele(dd, FTAG); 1869 dsl_pool_config_exit(dp, FTAG); 1870 kmem_free(attr, sizeof (zap_attribute_t)); 1871 return (err); 1872 } 1873 } 1874 1875 /* 1876 * Iterate over all snapshots. 1877 */ 1878 if (flags & DS_FIND_SNAPSHOTS) { 1879 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds); 1880 1881 if (err == 0) { 1882 uint64_t snapobj; 1883 1884 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj; 1885 dsl_dataset_rele(ds, FTAG); 1886 1887 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj); 1888 zap_cursor_retrieve(&zc, attr) == 0; 1889 (void) zap_cursor_advance(&zc)) { 1890 ASSERT3U(attr->za_integer_length, ==, 1891 sizeof (uint64_t)); 1892 ASSERT3U(attr->za_num_integers, ==, 1); 1893 1894 child = kmem_asprintf("%s@%s", 1895 name, attr->za_name); 1896 dsl_pool_config_exit(dp, FTAG); 1897 err = func(child, arg); 1898 dsl_pool_config_enter(dp, FTAG); 1899 strfree(child); 1900 if (err != 0) 1901 break; 1902 } 1903 zap_cursor_fini(&zc); 1904 } 1905 } 1906 1907 dsl_dir_rele(dd, FTAG); 1908 kmem_free(attr, sizeof (zap_attribute_t)); 1909 dsl_pool_config_exit(dp, FTAG); 1910 1911 if (err != 0) 1912 return (err); 1913 1914 /* Apply to self. */ 1915 return (func(name, arg)); 1916} 1917 1918/* 1919 * See comment above dmu_objset_find_impl(). 1920 */ 1921int 1922dmu_objset_find(char *name, int func(const char *, void *), void *arg, 1923 int flags) 1924{ 1925 spa_t *spa; 1926 int error; 1927 1928 error = spa_open(name, &spa, FTAG); 1929 if (error != 0) 1930 return (error); 1931 error = dmu_objset_find_impl(spa, name, func, arg, flags); 1932 spa_close(spa, FTAG); 1933 return (error); 1934} 1935 1936void 1937dmu_objset_set_user(objset_t *os, void *user_ptr) 1938{ 1939 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1940 os->os_user_ptr = user_ptr; 1941} 1942 1943void * 1944dmu_objset_get_user(objset_t *os) 1945{ 1946 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock)); 1947 return (os->os_user_ptr); 1948} 1949 1950/* 1951 * Determine name of filesystem, given name of snapshot. 1952 * buf must be at least MAXNAMELEN bytes 1953 */ 1954int 1955dmu_fsname(const char *snapname, char *buf) 1956{ 1957 char *atp = strchr(snapname, '@'); 1958 if (atp == NULL) 1959 return (SET_ERROR(EINVAL)); 1960 if (atp - snapname >= MAXNAMELEN) 1961 return (SET_ERROR(ENAMETOOLONG)); 1962 (void) strlcpy(buf, snapname, atp - snapname + 1); 1963 return (0); 1964} 1965