]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/dmu_objset.c
Merge tag 'upstream/0.7.5'
[mirror_zfs-debian.git] / module / zfs / dmu_objset.c
CommitLineData
34dc7c2f
BB
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 */
cae5b340 21
34dc7c2f 22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
cae5b340 24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
c06d4368 25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
e10b0808
AX
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
e10b0808 28 * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
4e820b5a 29 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
cae5b340 30 * Copyright 2017 Nexenta Systems, Inc.
34dc7c2f
BB
31 */
32
428870ff
BB
33/* Portions Copyright 2010 Robert Milkowski */
34
cae5b340 35#include <sys/zfeature.h>
34dc7c2f
BB
36#include <sys/cred.h>
37#include <sys/zfs_context.h>
38#include <sys/dmu_objset.h>
39#include <sys/dsl_dir.h>
40#include <sys/dsl_dataset.h>
41#include <sys/dsl_prop.h>
42#include <sys/dsl_pool.h>
43#include <sys/dsl_synctask.h>
44#include <sys/dsl_deleg.h>
45#include <sys/dnode.h>
46#include <sys/dbuf.h>
47#include <sys/zvol.h>
48#include <sys/dmu_tx.h>
34dc7c2f
BB
49#include <sys/zap.h>
50#include <sys/zil.h>
51#include <sys/dmu_impl.h>
52#include <sys/zfs_ioctl.h>
428870ff 53#include <sys/sa.h>
572e2857 54#include <sys/zfs_onexit.h>
a08ee875 55#include <sys/dsl_destroy.h>
e10b0808 56#include <sys/vdev.h>
cae5b340
AX
57#include <sys/policy.h>
58#include <sys/spa_impl.h>
572e2857
BB
59
60/*
61 * Needed to close a window in dnode_move() that allows the objset to be freed
62 * before it can be safely accessed.
63 */
64krwlock_t os_lock;
65
e10b0808 66/*
cae5b340 67 * Tunable to overwrite the maximum number of threads for the parallelization
e10b0808
AX
68 * of dmu_objset_find_dp, needed to speed up the import of pools with many
69 * datasets.
70 * Default is 4 times the number of leaf vdevs.
71 */
72int dmu_find_threads = 0;
73
cae5b340
AX
74/*
75 * Backfill lower metadnode objects after this many have been freed.
76 * Backfilling negatively impacts object creation rates, so only do it
77 * if there are enough holes to fill.
78 */
79int dmu_rescan_dnode_threshold = 1 << DN_MAX_INDBLKSHIFT;
80
e10b0808
AX
81static void dmu_objset_find_dp_cb(void *arg);
82
cae5b340
AX
83static void dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb);
84static void dmu_objset_upgrade_stop(objset_t *os);
85
572e2857
BB
86void
87dmu_objset_init(void)
88{
89 rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
90}
91
92void
93dmu_objset_fini(void)
94{
95 rw_destroy(&os_lock);
96}
34dc7c2f
BB
97
98spa_t *
99dmu_objset_spa(objset_t *os)
100{
428870ff 101 return (os->os_spa);
34dc7c2f
BB
102}
103
104zilog_t *
105dmu_objset_zil(objset_t *os)
106{
428870ff 107 return (os->os_zil);
34dc7c2f
BB
108}
109
110dsl_pool_t *
111dmu_objset_pool(objset_t *os)
112{
113 dsl_dataset_t *ds;
114
428870ff 115 if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
34dc7c2f
BB
116 return (ds->ds_dir->dd_pool);
117 else
428870ff 118 return (spa_get_dsl(os->os_spa));
34dc7c2f
BB
119}
120
121dsl_dataset_t *
122dmu_objset_ds(objset_t *os)
123{
428870ff 124 return (os->os_dsl_dataset);
34dc7c2f
BB
125}
126
127dmu_objset_type_t
128dmu_objset_type(objset_t *os)
129{
428870ff 130 return (os->os_phys->os_type);
34dc7c2f
BB
131}
132
133void
134dmu_objset_name(objset_t *os, char *buf)
135{
428870ff 136 dsl_dataset_name(os->os_dsl_dataset, buf);
34dc7c2f
BB
137}
138
139uint64_t
140dmu_objset_id(objset_t *os)
141{
428870ff 142 dsl_dataset_t *ds = os->os_dsl_dataset;
34dc7c2f
BB
143
144 return (ds ? ds->ds_object : 0);
145}
146
cae5b340
AX
147uint64_t
148dmu_objset_dnodesize(objset_t *os)
149{
150 return (os->os_dnodesize);
151}
152
ea04106b 153zfs_sync_type_t
428870ff
BB
154dmu_objset_syncprop(objset_t *os)
155{
156 return (os->os_sync);
157}
158
ea04106b 159zfs_logbias_op_t
428870ff
BB
160dmu_objset_logbias(objset_t *os)
161{
162 return (os->os_logbias);
163}
164
34dc7c2f
BB
165static void
166checksum_changed_cb(void *arg, uint64_t newval)
167{
428870ff 168 objset_t *os = arg;
34dc7c2f
BB
169
170 /*
171 * Inheritance should have been done by now.
172 */
173 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
174
428870ff 175 os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
34dc7c2f
BB
176}
177
178static void
179compression_changed_cb(void *arg, uint64_t newval)
180{
428870ff 181 objset_t *os = arg;
34dc7c2f
BB
182
183 /*
184 * Inheritance and range checking should have been done by now.
185 */
186 ASSERT(newval != ZIO_COMPRESS_INHERIT);
187
e10b0808
AX
188 os->os_compress = zio_compress_select(os->os_spa, newval,
189 ZIO_COMPRESS_ON);
34dc7c2f
BB
190}
191
192static void
193copies_changed_cb(void *arg, uint64_t newval)
194{
428870ff 195 objset_t *os = arg;
34dc7c2f
BB
196
197 /*
198 * Inheritance and range checking should have been done by now.
199 */
200 ASSERT(newval > 0);
428870ff 201 ASSERT(newval <= spa_max_replication(os->os_spa));
34dc7c2f 202
428870ff
BB
203 os->os_copies = newval;
204}
205
206static void
207dedup_changed_cb(void *arg, uint64_t newval)
208{
209 objset_t *os = arg;
210 spa_t *spa = os->os_spa;
211 enum zio_checksum checksum;
212
213 /*
214 * Inheritance should have been done by now.
215 */
216 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
217
218 checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
219
220 os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
221 os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
34dc7c2f
BB
222}
223
b128c09f
BB
224static void
225primary_cache_changed_cb(void *arg, uint64_t newval)
226{
428870ff 227 objset_t *os = arg;
b128c09f
BB
228
229 /*
230 * Inheritance and range checking should have been done by now.
231 */
232 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
233 newval == ZFS_CACHE_METADATA);
234
428870ff 235 os->os_primary_cache = newval;
b128c09f
BB
236}
237
238static void
239secondary_cache_changed_cb(void *arg, uint64_t newval)
240{
428870ff 241 objset_t *os = arg;
b128c09f
BB
242
243 /*
244 * Inheritance and range checking should have been done by now.
245 */
246 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
247 newval == ZFS_CACHE_METADATA);
248
428870ff
BB
249 os->os_secondary_cache = newval;
250}
251
252static void
253sync_changed_cb(void *arg, uint64_t newval)
254{
255 objset_t *os = arg;
256
257 /*
258 * Inheritance and range checking should have been done by now.
259 */
260 ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
261 newval == ZFS_SYNC_DISABLED);
262
263 os->os_sync = newval;
264 if (os->os_zil)
265 zil_set_sync(os->os_zil, newval);
266}
267
ea04106b
AX
268static void
269redundant_metadata_changed_cb(void *arg, uint64_t newval)
270{
271 objset_t *os = arg;
272
273 /*
274 * Inheritance and range checking should have been done by now.
275 */
276 ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
277 newval == ZFS_REDUNDANT_METADATA_MOST);
278
279 os->os_redundant_metadata = newval;
280}
281
cae5b340
AX
282static void
283dnodesize_changed_cb(void *arg, uint64_t newval)
284{
285 objset_t *os = arg;
286
287 switch (newval) {
288 case ZFS_DNSIZE_LEGACY:
289 os->os_dnodesize = DNODE_MIN_SIZE;
290 break;
291 case ZFS_DNSIZE_AUTO:
292 /*
293 * Choose a dnode size that will work well for most
294 * workloads if the user specified "auto". Future code
295 * improvements could dynamically select a dnode size
296 * based on observed workload patterns.
297 */
298 os->os_dnodesize = DNODE_MIN_SIZE * 2;
299 break;
300 case ZFS_DNSIZE_1K:
301 case ZFS_DNSIZE_2K:
302 case ZFS_DNSIZE_4K:
303 case ZFS_DNSIZE_8K:
304 case ZFS_DNSIZE_16K:
305 os->os_dnodesize = newval;
306 break;
307 }
308}
309
428870ff
BB
310static void
311logbias_changed_cb(void *arg, uint64_t newval)
312{
313 objset_t *os = arg;
314
315 ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
316 newval == ZFS_LOGBIAS_THROUGHPUT);
317 os->os_logbias = newval;
318 if (os->os_zil)
319 zil_set_logbias(os->os_zil, newval);
b128c09f
BB
320}
321
e10b0808
AX
322static void
323recordsize_changed_cb(void *arg, uint64_t newval)
324{
325 objset_t *os = arg;
326
327 os->os_recordsize = newval;
328}
329
34dc7c2f
BB
330void
331dmu_objset_byteswap(void *buf, size_t size)
332{
333 objset_phys_t *osp = buf;
334
9babb374 335 ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
34dc7c2f
BB
336 dnode_byteswap(&osp->os_meta_dnode);
337 byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
338 osp->os_type = BSWAP_64(osp->os_type);
9babb374
BB
339 osp->os_flags = BSWAP_64(osp->os_flags);
340 if (size == sizeof (objset_phys_t)) {
341 dnode_byteswap(&osp->os_userused_dnode);
342 dnode_byteswap(&osp->os_groupused_dnode);
343 }
34dc7c2f
BB
344}
345
cae5b340
AX
346/*
347 * The hash is a CRC-based hash of the objset_t pointer and the object number.
348 */
349static uint64_t
350dnode_hash(const objset_t *os, uint64_t obj)
351{
352 uintptr_t osv = (uintptr_t)os;
353 uint64_t crc = -1ULL;
354
355 ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
356 /*
357 * The low 6 bits of the pointer don't have much entropy, because
358 * the objset_t is larger than 2^6 bytes long.
359 */
360 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
361 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
362 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
363 crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 16)) & 0xFF];
364
365 crc ^= (osv>>14) ^ (obj>>24);
366
367 return (crc);
368}
369
370unsigned int
371dnode_multilist_index_func(multilist_t *ml, void *obj)
372{
373 dnode_t *dn = obj;
374 return (dnode_hash(dn->dn_objset, dn->dn_object) %
375 multilist_get_num_sublists(ml));
376}
377
34dc7c2f
BB
378int
379dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
428870ff 380 objset_t **osp)
34dc7c2f 381{
428870ff 382 objset_t *os;
b128c09f 383 int i, err;
34dc7c2f
BB
384
385 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
386
ea04106b 387 os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
428870ff
BB
388 os->os_dsl_dataset = ds;
389 os->os_spa = spa;
390 os->os_rootbp = bp;
391 if (!BP_IS_HOLE(os->os_rootbp)) {
e10b0808 392 arc_flags_t aflags = ARC_FLAG_WAIT;
ea04106b 393 zbookmark_phys_t zb;
428870ff
BB
394 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
395 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
396
397 if (DMU_OS_IS_L2CACHEABLE(os))
e10b0808 398 aflags |= ARC_FLAG_L2CACHE;
34dc7c2f 399
428870ff 400 dprintf_bp(os->os_rootbp, "reading %s", "");
c06d4368 401 err = arc_read(NULL, spa, os->os_rootbp,
428870ff 402 arc_getbuf_func, &os->os_phys_buf,
34dc7c2f 403 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
a08ee875 404 if (err != 0) {
428870ff 405 kmem_free(os, sizeof (objset_t));
b128c09f
BB
406 /* convert checksum errors into IO errors */
407 if (err == ECKSUM)
a08ee875 408 err = SET_ERROR(EIO);
34dc7c2f
BB
409 return (err);
410 }
9babb374
BB
411
412 /* Increase the blocksize if we are permitted. */
413 if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
428870ff 414 arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
cae5b340
AX
415 arc_buf_t *buf = arc_alloc_buf(spa, &os->os_phys_buf,
416 ARC_BUFC_METADATA, sizeof (objset_phys_t));
9babb374 417 bzero(buf->b_data, sizeof (objset_phys_t));
428870ff
BB
418 bcopy(os->os_phys_buf->b_data, buf->b_data,
419 arc_buf_size(os->os_phys_buf));
cae5b340 420 arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
428870ff 421 os->os_phys_buf = buf;
9babb374
BB
422 }
423
428870ff
BB
424 os->os_phys = os->os_phys_buf->b_data;
425 os->os_flags = os->os_phys->os_flags;
34dc7c2f 426 } else {
9babb374
BB
427 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
428 sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
cae5b340
AX
429 os->os_phys_buf = arc_alloc_buf(spa, &os->os_phys_buf,
430 ARC_BUFC_METADATA, size);
428870ff
BB
431 os->os_phys = os->os_phys_buf->b_data;
432 bzero(os->os_phys, size);
34dc7c2f
BB
433 }
434
435 /*
436 * Note: the changed_cb will be called once before the register
437 * func returns, thus changing the checksum/compression from the
b128c09f
BB
438 * default (fletcher2/off). Snapshots don't need to know about
439 * checksum/compression/copies.
34dc7c2f 440 */
ea04106b 441 if (ds != NULL) {
cae5b340
AX
442 boolean_t needlock = B_FALSE;
443
444 /*
445 * Note: it's valid to open the objset if the dataset is
446 * long-held, in which case the pool_config lock will not
447 * be held.
448 */
449 if (!dsl_pool_config_held(dmu_objset_pool(os))) {
450 needlock = B_TRUE;
451 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
452 }
a08ee875
LG
453 err = dsl_prop_register(ds,
454 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
428870ff 455 primary_cache_changed_cb, os);
a08ee875
LG
456 if (err == 0) {
457 err = dsl_prop_register(ds,
458 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
428870ff 459 secondary_cache_changed_cb, os);
a08ee875 460 }
e10b0808 461 if (!ds->ds_is_snapshot) {
a08ee875
LG
462 if (err == 0) {
463 err = dsl_prop_register(ds,
464 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
428870ff 465 checksum_changed_cb, os);
a08ee875
LG
466 }
467 if (err == 0) {
468 err = dsl_prop_register(ds,
469 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
428870ff 470 compression_changed_cb, os);
a08ee875
LG
471 }
472 if (err == 0) {
473 err = dsl_prop_register(ds,
474 zfs_prop_to_name(ZFS_PROP_COPIES),
428870ff 475 copies_changed_cb, os);
a08ee875
LG
476 }
477 if (err == 0) {
478 err = dsl_prop_register(ds,
479 zfs_prop_to_name(ZFS_PROP_DEDUP),
428870ff 480 dedup_changed_cb, os);
a08ee875
LG
481 }
482 if (err == 0) {
483 err = dsl_prop_register(ds,
484 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
428870ff 485 logbias_changed_cb, os);
a08ee875
LG
486 }
487 if (err == 0) {
488 err = dsl_prop_register(ds,
489 zfs_prop_to_name(ZFS_PROP_SYNC),
428870ff 490 sync_changed_cb, os);
a08ee875 491 }
ea04106b
AX
492 if (err == 0) {
493 err = dsl_prop_register(ds,
494 zfs_prop_to_name(
495 ZFS_PROP_REDUNDANT_METADATA),
496 redundant_metadata_changed_cb, os);
497 }
e10b0808
AX
498 if (err == 0) {
499 err = dsl_prop_register(ds,
500 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
501 recordsize_changed_cb, os);
502 }
cae5b340
AX
503 if (err == 0) {
504 err = dsl_prop_register(ds,
505 zfs_prop_to_name(ZFS_PROP_DNODESIZE),
506 dnodesize_changed_cb, os);
507 }
b128c09f 508 }
cae5b340
AX
509 if (needlock)
510 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
a08ee875 511 if (err != 0) {
cae5b340 512 arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
428870ff 513 kmem_free(os, sizeof (objset_t));
34dc7c2f
BB
514 return (err);
515 }
ea04106b 516 } else {
34dc7c2f 517 /* It's the meta-objset. */
428870ff 518 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
e10b0808 519 os->os_compress = ZIO_COMPRESS_ON;
428870ff
BB
520 os->os_copies = spa_max_replication(spa);
521 os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
ea04106b
AX
522 os->os_dedup_verify = B_FALSE;
523 os->os_logbias = ZFS_LOGBIAS_LATENCY;
524 os->os_sync = ZFS_SYNC_STANDARD;
428870ff
BB
525 os->os_primary_cache = ZFS_CACHE_ALL;
526 os->os_secondary_cache = ZFS_CACHE_ALL;
cae5b340 527 os->os_dnodesize = DNODE_MIN_SIZE;
34dc7c2f
BB
528 }
529
e10b0808 530 if (ds == NULL || !ds->ds_is_snapshot)
572e2857 531 os->os_zil_header = os->os_phys->os_zil_header;
428870ff 532 os->os_zil = zil_alloc(os, &os->os_zil_header);
34dc7c2f
BB
533
534 for (i = 0; i < TXG_SIZE; i++) {
cae5b340
AX
535 os->os_dirty_dnodes[i] = multilist_create(sizeof (dnode_t),
536 offsetof(dnode_t, dn_dirty_link[i]),
537 dnode_multilist_index_func);
34dc7c2f 538 }
428870ff 539 list_create(&os->os_dnodes, sizeof (dnode_t),
34dc7c2f 540 offsetof(dnode_t, dn_link));
428870ff 541 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
34dc7c2f
BB
542 offsetof(dmu_buf_impl_t, db_link));
543
e10b0808
AX
544 list_link_init(&os->os_evicting_node);
545
428870ff 546 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
cae5b340 547 mutex_init(&os->os_userused_lock, NULL, MUTEX_DEFAULT, NULL);
428870ff
BB
548 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
549 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
cae5b340
AX
550 os->os_obj_next_percpu_len = boot_ncpus;
551 os->os_obj_next_percpu = kmem_zalloc(os->os_obj_next_percpu_len *
552 sizeof (os->os_obj_next_percpu[0]), KM_SLEEP);
428870ff 553
e10b0808
AX
554 dnode_special_open(os, &os->os_phys->os_meta_dnode,
555 DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
428870ff 556 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
e10b0808
AX
557 dnode_special_open(os, &os->os_phys->os_userused_dnode,
558 DMU_USERUSED_OBJECT, &os->os_userused_dnode);
559 dnode_special_open(os, &os->os_phys->os_groupused_dnode,
560 DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
9babb374 561 }
34dc7c2f 562
cae5b340
AX
563 mutex_init(&os->os_upgrade_lock, NULL, MUTEX_DEFAULT, NULL);
564
428870ff 565 *osp = os;
34dc7c2f
BB
566 return (0);
567}
568
428870ff
BB
569int
570dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
34dc7c2f 571{
428870ff 572 int err = 0;
34dc7c2f 573
cae5b340
AX
574 /*
575 * We shouldn't be doing anything with dsl_dataset_t's unless the
576 * pool_config lock is held, or the dataset is long-held.
577 */
578 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool) ||
579 dsl_dataset_long_held(ds));
580
34dc7c2f 581 mutex_enter(&ds->ds_opening_lock);
ea04106b
AX
582 if (ds->ds_objset == NULL) {
583 objset_t *os;
cae5b340 584 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
34dc7c2f 585 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
ea04106b 586 ds, dsl_dataset_get_blkptr(ds), &os);
cae5b340 587 rrw_exit(&ds->ds_bp_rwlock, FTAG);
ea04106b
AX
588
589 if (err == 0) {
590 mutex_enter(&ds->ds_lock);
591 ASSERT(ds->ds_objset == NULL);
592 ds->ds_objset = os;
593 mutex_exit(&ds->ds_lock);
594 }
34dc7c2f 595 }
ea04106b 596 *osp = ds->ds_objset;
34dc7c2f 597 mutex_exit(&ds->ds_opening_lock);
428870ff 598 return (err);
34dc7c2f
BB
599}
600
a08ee875
LG
601/*
602 * Holds the pool while the objset is held. Therefore only one objset
603 * can be held at a time.
604 */
34dc7c2f 605int
428870ff 606dmu_objset_hold(const char *name, void *tag, objset_t **osp)
34dc7c2f 607{
a08ee875 608 dsl_pool_t *dp;
428870ff 609 dsl_dataset_t *ds;
34dc7c2f
BB
610 int err;
611
a08ee875
LG
612 err = dsl_pool_hold(name, tag, &dp);
613 if (err != 0)
614 return (err);
615 err = dsl_dataset_hold(dp, name, tag, &ds);
616 if (err != 0) {
617 dsl_pool_rele(dp, tag);
428870ff 618 return (err);
a08ee875 619 }
428870ff
BB
620
621 err = dmu_objset_from_ds(ds, osp);
a08ee875 622 if (err != 0) {
428870ff 623 dsl_dataset_rele(ds, tag);
a08ee875
LG
624 dsl_pool_rele(dp, tag);
625 }
428870ff 626
34dc7c2f
BB
627 return (err);
628}
629
e10b0808
AX
630static int
631dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
632 boolean_t readonly, void *tag, objset_t **osp)
633{
634 int err;
635
636 err = dmu_objset_from_ds(ds, osp);
637 if (err != 0) {
638 dsl_dataset_disown(ds, tag);
639 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
640 dsl_dataset_disown(ds, tag);
641 return (SET_ERROR(EINVAL));
642 } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
643 dsl_dataset_disown(ds, tag);
644 return (SET_ERROR(EROFS));
645 }
646 return (err);
647}
648
a08ee875
LG
649/*
650 * dsl_pool must not be held when this is called.
651 * Upon successful return, there will be a longhold on the dataset,
652 * and the dsl_pool will not be held.
653 */
34dc7c2f 654int
428870ff
BB
655dmu_objset_own(const char *name, dmu_objset_type_t type,
656 boolean_t readonly, void *tag, objset_t **osp)
34dc7c2f 657{
a08ee875 658 dsl_pool_t *dp;
34dc7c2f
BB
659 dsl_dataset_t *ds;
660 int err;
661
a08ee875
LG
662 err = dsl_pool_hold(name, FTAG, &dp);
663 if (err != 0)
664 return (err);
665 err = dsl_dataset_own(dp, name, tag, &ds);
666 if (err != 0) {
667 dsl_pool_rele(dp, FTAG);
34dc7c2f 668 return (err);
a08ee875 669 }
e10b0808 670 err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
a08ee875 671 dsl_pool_rele(dp, FTAG);
e10b0808 672
cae5b340
AX
673 if (err == 0 && dmu_objset_userobjspace_upgradable(*osp))
674 dmu_objset_userobjspace_upgrade(*osp);
675
34dc7c2f
BB
676 return (err);
677}
678
e10b0808
AX
679int
680dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
681 boolean_t readonly, void *tag, objset_t **osp)
682{
683 dsl_dataset_t *ds;
684 int err;
685
686 err = dsl_dataset_own_obj(dp, obj, tag, &ds);
687 if (err != 0)
688 return (err);
689
690 return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
691}
692
34dc7c2f 693void
428870ff 694dmu_objset_rele(objset_t *os, void *tag)
34dc7c2f 695{
a08ee875 696 dsl_pool_t *dp = dmu_objset_pool(os);
428870ff 697 dsl_dataset_rele(os->os_dsl_dataset, tag);
a08ee875
LG
698 dsl_pool_rele(dp, tag);
699}
700
701/*
702 * When we are called, os MUST refer to an objset associated with a dataset
703 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
704 * == tag. We will then release and reacquire ownership of the dataset while
705 * holding the pool config_rwlock to avoid intervening namespace or ownership
706 * changes may occur.
707 *
708 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
709 * release the hold on its dataset and acquire a new one on the dataset of the
710 * same name so that it can be partially torn down and reconstructed.
711 */
712void
713dmu_objset_refresh_ownership(objset_t *os, void *tag)
714{
715 dsl_pool_t *dp;
716 dsl_dataset_t *ds, *newds;
cae5b340 717 char name[ZFS_MAX_DATASET_NAME_LEN];
a08ee875
LG
718
719 ds = os->os_dsl_dataset;
720 VERIFY3P(ds, !=, NULL);
721 VERIFY3P(ds->ds_owner, ==, tag);
722 VERIFY(dsl_dataset_long_held(ds));
723
724 dsl_dataset_name(ds, name);
725 dp = dmu_objset_pool(os);
726 dsl_pool_config_enter(dp, FTAG);
727 dmu_objset_disown(os, tag);
728 VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
729 VERIFY3P(newds, ==, os->os_dsl_dataset);
730 dsl_pool_config_exit(dp, FTAG);
428870ff 731}
b128c09f 732
428870ff
BB
733void
734dmu_objset_disown(objset_t *os, void *tag)
735{
cae5b340
AX
736 /*
737 * Stop upgrading thread
738 */
739 dmu_objset_upgrade_stop(os);
428870ff 740 dsl_dataset_disown(os->os_dsl_dataset, tag);
34dc7c2f
BB
741}
742
a08ee875 743void
34dc7c2f
BB
744dmu_objset_evict_dbufs(objset_t *os)
745{
e10b0808 746 dnode_t *dn_marker;
34dc7c2f
BB
747 dnode_t *dn;
748
e10b0808 749 dn_marker = kmem_alloc(sizeof (dnode_t), KM_SLEEP);
34dc7c2f 750
e10b0808
AX
751 mutex_enter(&os->os_lock);
752 dn = list_head(&os->os_dnodes);
753 while (dn != NULL) {
754 /*
755 * Skip dnodes without holds. We have to do this dance
756 * because dnode_add_ref() only works if there is already a
757 * hold. If the dnode has no holds, then it has no dbufs.
758 */
759 if (dnode_add_ref(dn, FTAG)) {
760 list_insert_after(&os->os_dnodes, dn, dn_marker);
761 mutex_exit(&os->os_lock);
34dc7c2f 762
e10b0808
AX
763 dnode_evict_dbufs(dn);
764 dnode_rele(dn, FTAG);
34dc7c2f 765
e10b0808
AX
766 mutex_enter(&os->os_lock);
767 dn = list_next(&os->os_dnodes, dn_marker);
768 list_remove(&os->os_dnodes, dn_marker);
769 } else {
770 dn = list_next(&os->os_dnodes, dn);
771 }
772 }
773 mutex_exit(&os->os_lock);
34dc7c2f 774
e10b0808 775 kmem_free(dn_marker, sizeof (dnode_t));
34dc7c2f 776
e10b0808
AX
777 if (DMU_USERUSED_DNODE(os) != NULL) {
778 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
779 dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
34dc7c2f 780 }
e10b0808 781 dnode_evict_dbufs(DMU_META_DNODE(os));
34dc7c2f
BB
782}
783
e10b0808
AX
784/*
785 * Objset eviction processing is split into into two pieces.
786 * The first marks the objset as evicting, evicts any dbufs that
787 * have a refcount of zero, and then queues up the objset for the
788 * second phase of eviction. Once os->os_dnodes has been cleared by
789 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
790 * The second phase closes the special dnodes, dequeues the objset from
791 * the list of those undergoing eviction, and finally frees the objset.
792 *
793 * NOTE: Due to asynchronous eviction processing (invocation of
794 * dnode_buf_pageout()), it is possible for the meta dnode for the
795 * objset to have no holds even though os->os_dnodes is not empty.
796 */
34dc7c2f 797void
428870ff 798dmu_objset_evict(objset_t *os)
34dc7c2f 799{
d6320ddb 800 int t;
34dc7c2f 801
a08ee875
LG
802 dsl_dataset_t *ds = os->os_dsl_dataset;
803
d6320ddb 804 for (t = 0; t < TXG_SIZE; t++)
428870ff 805 ASSERT(!dmu_objset_is_dirty(os, t));
34dc7c2f 806
cae5b340
AX
807 if (ds)
808 dsl_prop_unregister_all(ds, os);
34dc7c2f 809
428870ff
BB
810 if (os->os_sa)
811 sa_tear_down(os);
812
a08ee875 813 dmu_objset_evict_dbufs(os);
34dc7c2f 814
e10b0808
AX
815 mutex_enter(&os->os_lock);
816 spa_evicting_os_register(os->os_spa, os);
817 if (list_is_empty(&os->os_dnodes)) {
818 mutex_exit(&os->os_lock);
819 dmu_objset_evict_done(os);
820 } else {
821 mutex_exit(&os->os_lock);
822 }
823}
824
825void
826dmu_objset_evict_done(objset_t *os)
827{
828 ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
829
572e2857
BB
830 dnode_special_close(&os->os_meta_dnode);
831 if (DMU_USERUSED_DNODE(os)) {
832 dnode_special_close(&os->os_userused_dnode);
833 dnode_special_close(&os->os_groupused_dnode);
9babb374 834 }
428870ff
BB
835 zil_free(os->os_zil);
836
cae5b340 837 arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
572e2857
BB
838
839 /*
840 * This is a barrier to prevent the objset from going away in
841 * dnode_move() until we can safely ensure that the objset is still in
842 * use. We consider the objset valid before the barrier and invalid
843 * after the barrier.
844 */
845 rw_enter(&os_lock, RW_READER);
846 rw_exit(&os_lock);
847
cae5b340
AX
848 kmem_free(os->os_obj_next_percpu,
849 os->os_obj_next_percpu_len * sizeof (os->os_obj_next_percpu[0]));
850
428870ff 851 mutex_destroy(&os->os_lock);
cae5b340 852 mutex_destroy(&os->os_userused_lock);
428870ff
BB
853 mutex_destroy(&os->os_obj_lock);
854 mutex_destroy(&os->os_user_ptr_lock);
cae5b340
AX
855 mutex_destroy(&os->os_upgrade_lock);
856 for (int i = 0; i < TXG_SIZE; i++) {
857 multilist_destroy(os->os_dirty_dnodes[i]);
858 }
e10b0808 859 spa_evicting_os_deregister(os->os_spa, os);
428870ff
BB
860 kmem_free(os, sizeof (objset_t));
861}
9babb374 862
428870ff
BB
863timestruc_t
864dmu_objset_snap_cmtime(objset_t *os)
865{
866 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
34dc7c2f
BB
867}
868
869/* called from dsl for meta-objset */
428870ff 870objset_t *
34dc7c2f
BB
871dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
872 dmu_objset_type_t type, dmu_tx_t *tx)
873{
428870ff 874 objset_t *os;
34dc7c2f
BB
875 dnode_t *mdn;
876
877 ASSERT(dmu_tx_is_syncing(tx));
a08ee875 878
572e2857 879 if (ds != NULL)
a08ee875 880 VERIFY0(dmu_objset_from_ds(ds, &os));
572e2857 881 else
a08ee875 882 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
572e2857
BB
883
884 mdn = DMU_META_DNODE(os);
34dc7c2f 885
cae5b340
AX
886 dnode_allocate(mdn, DMU_OT_DNODE, DNODE_BLOCK_SIZE, DN_MAX_INDBLKSHIFT,
887 DMU_OT_NONE, 0, DNODE_MIN_SLOTS, tx);
34dc7c2f
BB
888
889 /*
890 * We don't want to have to increase the meta-dnode's nlevels
891 * later, because then we could do it in quescing context while
892 * we are also accessing it in open context.
893 *
894 * This precaution is not necessary for the MOS (ds == NULL),
895 * because the MOS is only updated in syncing context.
896 * This is most fortunate: the MOS is the only objset that
897 * needs to be synced multiple times as spa_sync() iterates
898 * to convergence, so minimizing its dn_nlevels matters.
899 */
900 if (ds != NULL) {
901 int levels = 1;
902
903 /*
904 * Determine the number of levels necessary for the meta-dnode
cae5b340
AX
905 * to contain DN_MAX_OBJECT dnodes. Note that in order to
906 * ensure that we do not overflow 64 bits, there has to be
907 * a nlevels that gives us a number of blocks > DN_MAX_OBJECT
908 * but < 2^64. Therefore,
909 * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT) (10) must be
910 * less than (64 - log2(DN_MAX_OBJECT)) (16).
34dc7c2f 911 */
cae5b340
AX
912 while ((uint64_t)mdn->dn_nblkptr <<
913 (mdn->dn_datablkshift - DNODE_SHIFT +
34dc7c2f 914 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
cae5b340 915 DN_MAX_OBJECT)
34dc7c2f
BB
916 levels++;
917
918 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
919 mdn->dn_nlevels = levels;
920 }
921
922 ASSERT(type != DMU_OST_NONE);
923 ASSERT(type != DMU_OST_ANY);
924 ASSERT(type < DMU_OST_NUMTYPES);
428870ff
BB
925 os->os_phys->os_type = type;
926 if (dmu_objset_userused_enabled(os)) {
927 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
cae5b340
AX
928 if (dmu_objset_userobjused_enabled(os)) {
929 ds->ds_feature_activation_needed[
930 SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE;
931 os->os_phys->os_flags |=
932 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
933 }
428870ff 934 os->os_flags = os->os_phys->os_flags;
9babb374 935 }
34dc7c2f
BB
936
937 dsl_dataset_dirty(ds, tx);
938
428870ff 939 return (os);
34dc7c2f
BB
940}
941
a08ee875
LG
942typedef struct dmu_objset_create_arg {
943 const char *doca_name;
944 cred_t *doca_cred;
945 void (*doca_userfunc)(objset_t *os, void *arg,
946 cred_t *cr, dmu_tx_t *tx);
947 void *doca_userarg;
948 dmu_objset_type_t doca_type;
949 uint64_t doca_flags;
950} dmu_objset_create_arg_t;
34dc7c2f
BB
951
952/*ARGSUSED*/
953static int
a08ee875 954dmu_objset_create_check(void *arg, dmu_tx_t *tx)
34dc7c2f 955{
a08ee875
LG
956 dmu_objset_create_arg_t *doca = arg;
957 dsl_pool_t *dp = dmu_tx_pool(tx);
958 dsl_dir_t *pdd;
959 const char *tail;
960 int error;
34dc7c2f 961
a08ee875
LG
962 if (strchr(doca->doca_name, '@') != NULL)
963 return (SET_ERROR(EINVAL));
34dc7c2f 964
cae5b340
AX
965 if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
966 return (SET_ERROR(ENAMETOOLONG));
967
a08ee875
LG
968 error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
969 if (error != 0)
970 return (error);
971 if (tail == NULL) {
972 dsl_dir_rele(pdd, FTAG);
973 return (SET_ERROR(EEXIST));
34dc7c2f 974 }
e10b0808
AX
975 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
976 doca->doca_cred);
a08ee875 977 dsl_dir_rele(pdd, FTAG);
34dc7c2f 978
e10b0808 979 return (error);
34dc7c2f
BB
980}
981
982static void
a08ee875 983dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 984{
a08ee875
LG
985 dmu_objset_create_arg_t *doca = arg;
986 dsl_pool_t *dp = dmu_tx_pool(tx);
987 dsl_dir_t *pdd;
988 const char *tail;
989 dsl_dataset_t *ds;
572e2857 990 uint64_t obj;
a08ee875
LG
991 blkptr_t *bp;
992 objset_t *os;
34dc7c2f 993
a08ee875 994 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
34dc7c2f 995
a08ee875
LG
996 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
997 doca->doca_cred, tx);
428870ff 998
a08ee875 999 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
cae5b340 1000 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
a08ee875
LG
1001 bp = dsl_dataset_get_blkptr(ds);
1002 os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
1003 ds, bp, doca->doca_type, tx);
cae5b340 1004 rrw_exit(&ds->ds_bp_rwlock, FTAG);
34dc7c2f 1005
a08ee875
LG
1006 if (doca->doca_userfunc != NULL) {
1007 doca->doca_userfunc(os, doca->doca_userarg,
1008 doca->doca_cred, tx);
34dc7c2f
BB
1009 }
1010
a08ee875 1011 spa_history_log_internal_ds(ds, "create", tx, "");
4e820b5a
AX
1012 zvol_create_minors(dp->dp_spa, doca->doca_name, B_TRUE);
1013
a08ee875
LG
1014 dsl_dataset_rele(ds, FTAG);
1015 dsl_dir_rele(pdd, FTAG);
34dc7c2f
BB
1016}
1017
1018int
428870ff 1019dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
34dc7c2f
BB
1020 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
1021{
a08ee875 1022 dmu_objset_create_arg_t doca;
34dc7c2f 1023
a08ee875
LG
1024 doca.doca_name = name;
1025 doca.doca_cred = CRED();
1026 doca.doca_flags = flags;
1027 doca.doca_userfunc = func;
1028 doca.doca_userarg = arg;
1029 doca.doca_type = type;
34dc7c2f 1030
a08ee875 1031 return (dsl_sync_task(name,
e10b0808
AX
1032 dmu_objset_create_check, dmu_objset_create_sync, &doca,
1033 5, ZFS_SPACE_CHECK_NORMAL));
34dc7c2f
BB
1034}
1035
a08ee875
LG
1036typedef struct dmu_objset_clone_arg {
1037 const char *doca_clone;
1038 const char *doca_origin;
1039 cred_t *doca_cred;
1040} dmu_objset_clone_arg_t;
1041
1042/*ARGSUSED*/
1043static int
1044dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1045{
a08ee875 1046 dmu_objset_clone_arg_t *doca = arg;
428870ff
BB
1047 dsl_dir_t *pdd;
1048 const char *tail;
a08ee875
LG
1049 int error;
1050 dsl_dataset_t *origin;
1051 dsl_pool_t *dp = dmu_tx_pool(tx);
34dc7c2f 1052
a08ee875
LG
1053 if (strchr(doca->doca_clone, '@') != NULL)
1054 return (SET_ERROR(EINVAL));
1055
cae5b340
AX
1056 if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
1057 return (SET_ERROR(ENAMETOOLONG));
1058
a08ee875
LG
1059 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
1060 if (error != 0)
1061 return (error);
428870ff 1062 if (tail == NULL) {
a08ee875
LG
1063 dsl_dir_rele(pdd, FTAG);
1064 return (SET_ERROR(EEXIST));
34dc7c2f 1065 }
e10b0808
AX
1066
1067 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1068 doca->doca_cred);
1069 if (error != 0) {
a08ee875 1070 dsl_dir_rele(pdd, FTAG);
e10b0808 1071 return (SET_ERROR(EDQUOT));
428870ff 1072 }
a08ee875 1073 dsl_dir_rele(pdd, FTAG);
428870ff 1074
a08ee875
LG
1075 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
1076 if (error != 0)
572e2857
BB
1077 return (error);
1078
a08ee875 1079 /* You can only clone snapshots, not the head datasets. */
e10b0808 1080 if (!origin->ds_is_snapshot) {
a08ee875
LG
1081 dsl_dataset_rele(origin, FTAG);
1082 return (SET_ERROR(EINVAL));
572e2857 1083 }
a08ee875
LG
1084 dsl_dataset_rele(origin, FTAG);
1085
1086 return (0);
9babb374
BB
1087}
1088
1089static void
a08ee875 1090dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
9babb374 1091{
a08ee875
LG
1092 dmu_objset_clone_arg_t *doca = arg;
1093 dsl_pool_t *dp = dmu_tx_pool(tx);
1094 dsl_dir_t *pdd;
1095 const char *tail;
1096 dsl_dataset_t *origin, *ds;
1097 uint64_t obj;
cae5b340 1098 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
572e2857 1099
a08ee875
LG
1100 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
1101 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
572e2857 1102
a08ee875
LG
1103 obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
1104 doca->doca_cred, tx);
572e2857 1105
a08ee875
LG
1106 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
1107 dsl_dataset_name(origin, namebuf);
1108 spa_history_log_internal_ds(ds, "clone", tx,
1109 "origin=%s (%llu)", namebuf, origin->ds_object);
4e820b5a 1110 zvol_create_minors(dp->dp_spa, doca->doca_clone, B_TRUE);
a08ee875
LG
1111 dsl_dataset_rele(ds, FTAG);
1112 dsl_dataset_rele(origin, FTAG);
1113 dsl_dir_rele(pdd, FTAG);
9babb374 1114}
34dc7c2f 1115
a08ee875
LG
1116int
1117dmu_objset_clone(const char *clone, const char *origin)
34dc7c2f 1118{
a08ee875 1119 dmu_objset_clone_arg_t doca;
34dc7c2f 1120
a08ee875
LG
1121 doca.doca_clone = clone;
1122 doca.doca_origin = origin;
1123 doca.doca_cred = CRED();
34dc7c2f 1124
a08ee875 1125 return (dsl_sync_task(clone,
e10b0808
AX
1126 dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
1127 5, ZFS_SPACE_CHECK_NORMAL));
34dc7c2f
BB
1128}
1129
1130int
a08ee875 1131dmu_objset_snapshot_one(const char *fsname, const char *snapname)
34dc7c2f 1132{
34dc7c2f 1133 int err;
a08ee875
LG
1134 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1135 nvlist_t *snaps = fnvlist_alloc();
34dc7c2f 1136
a08ee875
LG
1137 fnvlist_add_boolean(snaps, longsnap);
1138 strfree(longsnap);
1139 err = dsl_dataset_snapshot(snaps, NULL, NULL);
1140 fnvlist_free(snaps);
34dc7c2f
BB
1141 return (err);
1142}
1143
1144static void
cae5b340
AX
1145dmu_objset_upgrade_task_cb(void *data)
1146{
1147 objset_t *os = data;
1148
1149 mutex_enter(&os->os_upgrade_lock);
1150 os->os_upgrade_status = EINTR;
1151 if (!os->os_upgrade_exit) {
1152 mutex_exit(&os->os_upgrade_lock);
1153
1154 os->os_upgrade_status = os->os_upgrade_cb(os);
1155 mutex_enter(&os->os_upgrade_lock);
1156 }
1157 os->os_upgrade_exit = B_TRUE;
1158 os->os_upgrade_id = 0;
1159 mutex_exit(&os->os_upgrade_lock);
1160}
1161
1162static void
1163dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb)
1164{
1165 if (os->os_upgrade_id != 0)
1166 return;
1167
1168 mutex_enter(&os->os_upgrade_lock);
1169 if (os->os_upgrade_id == 0 && os->os_upgrade_status == 0) {
1170 os->os_upgrade_exit = B_FALSE;
1171 os->os_upgrade_cb = cb;
1172 os->os_upgrade_id = taskq_dispatch(
1173 os->os_spa->spa_upgrade_taskq,
1174 dmu_objset_upgrade_task_cb, os, TQ_SLEEP);
1175 if (os->os_upgrade_id == TASKQID_INVALID)
1176 os->os_upgrade_status = ENOMEM;
1177 }
1178 mutex_exit(&os->os_upgrade_lock);
1179}
1180
1181static void
1182dmu_objset_upgrade_stop(objset_t *os)
1183{
1184 mutex_enter(&os->os_upgrade_lock);
1185 os->os_upgrade_exit = B_TRUE;
1186 if (os->os_upgrade_id != 0) {
1187 taskqid_t id = os->os_upgrade_id;
1188
1189 os->os_upgrade_id = 0;
1190 mutex_exit(&os->os_upgrade_lock);
1191
1192 taskq_cancel_id(os->os_spa->spa_upgrade_taskq, id);
1193 } else {
1194 mutex_exit(&os->os_upgrade_lock);
1195 }
1196}
1197
1198static void
1199dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx)
34dc7c2f
BB
1200{
1201 dnode_t *dn;
1202
cae5b340 1203 while ((dn = multilist_sublist_head(list)) != NULL) {
34dc7c2f
BB
1204 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1205 ASSERT(dn->dn_dbuf->db_data_pending);
1206 /*
9babb374
BB
1207 * Initialize dn_zio outside dnode_sync() because the
1208 * meta-dnode needs to set it ouside dnode_sync().
34dc7c2f
BB
1209 */
1210 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1211 ASSERT(dn->dn_zio);
1212
1213 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
cae5b340 1214 multilist_sublist_remove(list, dn);
9babb374 1215
cae5b340
AX
1216 multilist_t *newlist = dn->dn_objset->os_synced_dnodes;
1217 if (newlist != NULL) {
9babb374 1218 (void) dnode_add_ref(dn, newlist);
cae5b340 1219 multilist_insert(newlist, dn);
9babb374
BB
1220 }
1221
34dc7c2f
BB
1222 dnode_sync(dn, tx);
1223 }
1224}
1225
1226/* ARGSUSED */
1227static void
428870ff 1228dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
34dc7c2f 1229{
d6320ddb
BB
1230 int i;
1231
b128c09f 1232 blkptr_t *bp = zio->io_bp;
428870ff 1233 objset_t *os = arg;
34dc7c2f 1234 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
34dc7c2f 1235
ea04106b 1236 ASSERT(!BP_IS_EMBEDDED(bp));
a08ee875
LG
1237 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1238 ASSERT0(BP_GET_LEVEL(bp));
34dc7c2f
BB
1239
1240 /*
9babb374
BB
1241 * Update rootbp fill count: it should be the number of objects
1242 * allocated in the object set (not counting the "special"
1243 * objects that are stored in the objset_phys_t -- the meta
1244 * dnode and user/group accounting objects).
34dc7c2f 1245 */
9babb374 1246 bp->blk_fill = 0;
d6320ddb 1247 for (i = 0; i < dnp->dn_nblkptr; i++)
ea04106b 1248 bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
cae5b340
AX
1249 if (os->os_dsl_dataset != NULL)
1250 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1251 *os->os_rootbp = *bp;
1252 if (os->os_dsl_dataset != NULL)
1253 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
428870ff
BB
1254}
1255
1256/* ARGSUSED */
1257static void
1258dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1259{
1260 blkptr_t *bp = zio->io_bp;
1261 blkptr_t *bp_orig = &zio->io_bp_orig;
1262 objset_t *os = arg;
34dc7c2f 1263
b128c09f 1264 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
428870ff 1265 ASSERT(BP_EQUAL(bp, bp_orig));
b128c09f 1266 } else {
428870ff
BB
1267 dsl_dataset_t *ds = os->os_dsl_dataset;
1268 dmu_tx_t *tx = os->os_synctx;
1269
1270 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1271 dsl_dataset_block_born(ds, bp, tx);
34dc7c2f 1272 }
cae5b340 1273 kmem_free(bp, sizeof (*bp));
34dc7c2f
BB
1274}
1275
cae5b340
AX
1276typedef struct sync_dnodes_arg {
1277 multilist_t *sda_list;
1278 int sda_sublist_idx;
1279 multilist_t *sda_newlist;
1280 dmu_tx_t *sda_tx;
1281} sync_dnodes_arg_t;
1282
1283static void
1284sync_dnodes_task(void *arg)
1285{
1286 sync_dnodes_arg_t *sda = arg;
1287
1288 multilist_sublist_t *ms =
1289 multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
1290
1291 dmu_objset_sync_dnodes(ms, sda->sda_tx);
1292
1293 multilist_sublist_unlock(ms);
1294
1295 kmem_free(sda, sizeof (*sda));
1296}
1297
1298
34dc7c2f
BB
1299/* called from dsl */
1300void
428870ff 1301dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
34dc7c2f
BB
1302{
1303 int txgoff;
ea04106b 1304 zbookmark_phys_t zb;
428870ff 1305 zio_prop_t zp;
34dc7c2f
BB
1306 zio_t *zio;
1307 list_t *list;
1308 dbuf_dirty_record_t *dr;
cae5b340
AX
1309 blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1310 *blkptr_copy = *os->os_rootbp;
34dc7c2f
BB
1311
1312 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1313
1314 ASSERT(dmu_tx_is_syncing(tx));
1315 /* XXX the write_done callback should really give us the tx... */
1316 os->os_synctx = tx;
1317
1318 if (os->os_dsl_dataset == NULL) {
1319 /*
1320 * This is the MOS. If we have upgraded,
1321 * spa_max_replication() could change, so reset
1322 * os_copies here.
1323 */
1324 os->os_copies = spa_max_replication(os->os_spa);
1325 }
1326
1327 /*
1328 * Create the root block IO
1329 */
428870ff
BB
1330 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1331 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1332 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
c06d4368 1333 arc_release(os->os_phys_buf, &os->os_phys_buf);
b128c09f 1334
428870ff 1335 dmu_write_policy(os, NULL, 0, 0, &zp);
9babb374 1336
428870ff 1337 zio = arc_write(pio, os->os_spa, tx->tx_txg,
cae5b340
AX
1338 blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1339 &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
1340 os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
34dc7c2f
BB
1341
1342 /*
9babb374 1343 * Sync special dnodes - the parent IO for the sync is the root block
34dc7c2f 1344 */
572e2857
BB
1345 DMU_META_DNODE(os)->dn_zio = zio;
1346 dnode_sync(DMU_META_DNODE(os), tx);
34dc7c2f 1347
9babb374
BB
1348 os->os_phys->os_flags = os->os_flags;
1349
572e2857
BB
1350 if (DMU_USERUSED_DNODE(os) &&
1351 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1352 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1353 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1354 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1355 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
9babb374
BB
1356 }
1357
34dc7c2f
BB
1358 txgoff = tx->tx_txg & TXG_MASK;
1359
9babb374 1360 if (dmu_objset_userused_enabled(os)) {
9babb374
BB
1361 /*
1362 * We must create the list here because it uses the
cae5b340
AX
1363 * dn_dirty_link[] of this txg. But it may already
1364 * exist because we call dsl_dataset_sync() twice per txg.
9babb374 1365 */
cae5b340
AX
1366 if (os->os_synced_dnodes == NULL) {
1367 os->os_synced_dnodes =
1368 multilist_create(sizeof (dnode_t),
1369 offsetof(dnode_t, dn_dirty_link[txgoff]),
1370 dnode_multilist_index_func);
1371 } else {
1372 ASSERT3U(os->os_synced_dnodes->ml_offset, ==,
1373 offsetof(dnode_t, dn_dirty_link[txgoff]));
1374 }
9babb374
BB
1375 }
1376
cae5b340
AX
1377 for (int i = 0;
1378 i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) {
1379 sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
1380 sda->sda_list = os->os_dirty_dnodes[txgoff];
1381 sda->sda_sublist_idx = i;
1382 sda->sda_tx = tx;
1383 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1384 sync_dnodes_task, sda, 0);
1385 /* callback frees sda */
1386 }
1387 taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
34dc7c2f 1388
572e2857 1389 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
cae5b340 1390 while ((dr = list_head(list)) != NULL) {
a08ee875 1391 ASSERT0(dr->dr_dbuf->db_level);
34dc7c2f
BB
1392 list_remove(list, dr);
1393 if (dr->dr_zio)
1394 zio_nowait(dr->dr_zio);
1395 }
cae5b340
AX
1396
1397 /* Enable dnode backfill if enough objects have been freed. */
1398 if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1399 os->os_rescan_dnodes = B_TRUE;
1400 os->os_freed_dnodes = 0;
1401 }
1402
34dc7c2f
BB
1403 /*
1404 * Free intent log blocks up to this tx.
1405 */
1406 zil_sync(os->os_zil, tx);
b128c09f 1407 os->os_phys->os_zil_header = os->os_zil_header;
34dc7c2f
BB
1408 zio_nowait(zio);
1409}
1410
428870ff
BB
1411boolean_t
1412dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1413{
cae5b340 1414 return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK]));
428870ff
BB
1415}
1416
572e2857 1417static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
9babb374
BB
1418
1419void
1420dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1421{
1422 used_cbs[ost] = cb;
1423}
1424
1425boolean_t
428870ff 1426dmu_objset_userused_enabled(objset_t *os)
9babb374
BB
1427{
1428 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
572e2857
BB
1429 used_cbs[os->os_phys->os_type] != NULL &&
1430 DMU_USERUSED_DNODE(os) != NULL);
9babb374
BB
1431}
1432
cae5b340
AX
1433boolean_t
1434dmu_objset_userobjused_enabled(objset_t *os)
1435{
1436 return (dmu_objset_userused_enabled(os) &&
1437 spa_feature_is_enabled(os->os_spa, SPA_FEATURE_USEROBJ_ACCOUNTING));
1438}
1439
1440typedef struct userquota_node {
1441 /* must be in the first filed, see userquota_update_cache() */
1442 char uqn_id[20 + DMU_OBJACCT_PREFIX_LEN];
1443 int64_t uqn_delta;
1444 avl_node_t uqn_node;
1445} userquota_node_t;
1446
1447typedef struct userquota_cache {
1448 avl_tree_t uqc_user_deltas;
1449 avl_tree_t uqc_group_deltas;
1450} userquota_cache_t;
1451
1452static int
1453userquota_compare(const void *l, const void *r)
1454{
1455 const userquota_node_t *luqn = l;
1456 const userquota_node_t *ruqn = r;
1457 int rv;
1458
1459 /*
1460 * NB: can only access uqn_id because userquota_update_cache() doesn't
1461 * pass in an entire userquota_node_t.
1462 */
1463 rv = strcmp(luqn->uqn_id, ruqn->uqn_id);
1464
1465 return (AVL_ISIGN(rv));
1466}
1467
428870ff 1468static void
cae5b340
AX
1469do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
1470{
1471 void *cookie;
1472 userquota_node_t *uqn;
1473
1474 ASSERT(dmu_tx_is_syncing(tx));
1475
1476 cookie = NULL;
1477 while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
1478 &cookie)) != NULL) {
1479 /*
1480 * os_userused_lock protects against concurrent calls to
1481 * zap_increment_int(). It's needed because zap_increment_int()
1482 * is not thread-safe (i.e. not atomic).
1483 */
1484 mutex_enter(&os->os_userused_lock);
1485 VERIFY0(zap_increment(os, DMU_USERUSED_OBJECT,
1486 uqn->uqn_id, uqn->uqn_delta, tx));
1487 mutex_exit(&os->os_userused_lock);
1488 kmem_free(uqn, sizeof (*uqn));
1489 }
1490 avl_destroy(&cache->uqc_user_deltas);
1491
1492 cookie = NULL;
1493 while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
1494 &cookie)) != NULL) {
1495 mutex_enter(&os->os_userused_lock);
1496 VERIFY0(zap_increment(os, DMU_GROUPUSED_OBJECT,
1497 uqn->uqn_id, uqn->uqn_delta, tx));
1498 mutex_exit(&os->os_userused_lock);
1499 kmem_free(uqn, sizeof (*uqn));
1500 }
1501 avl_destroy(&cache->uqc_group_deltas);
1502}
1503
1504static void
1505userquota_update_cache(avl_tree_t *avl, const char *id, int64_t delta)
1506{
1507 userquota_node_t *uqn;
1508 avl_index_t idx;
1509
1510 ASSERT(strlen(id) < sizeof (uqn->uqn_id));
1511 /*
1512 * Use id directly for searching because uqn_id is the first field of
1513 * userquota_node_t and fields after uqn_id won't be accessed in
1514 * avl_find().
1515 */
1516 uqn = avl_find(avl, (const void *)id, &idx);
1517 if (uqn == NULL) {
1518 uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
1519 strlcpy(uqn->uqn_id, id, sizeof (uqn->uqn_id));
1520 avl_insert(avl, uqn, idx);
1521 }
1522 uqn->uqn_delta += delta;
1523}
1524
1525static void
1526do_userquota_update(userquota_cache_t *cache, uint64_t used, uint64_t flags,
1527 uint64_t user, uint64_t group, boolean_t subtract)
428870ff
BB
1528{
1529 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
cae5b340
AX
1530 int64_t delta = DNODE_MIN_SIZE + used;
1531 char name[20];
1532
428870ff
BB
1533 if (subtract)
1534 delta = -delta;
cae5b340
AX
1535
1536 (void) sprintf(name, "%llx", (longlong_t)user);
1537 userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1538
1539 (void) sprintf(name, "%llx", (longlong_t)group);
1540 userquota_update_cache(&cache->uqc_group_deltas, name, delta);
428870ff
BB
1541 }
1542}
1543
cae5b340
AX
1544static void
1545do_userobjquota_update(userquota_cache_t *cache, uint64_t flags,
1546 uint64_t user, uint64_t group, boolean_t subtract)
9babb374 1547{
cae5b340
AX
1548 if (flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) {
1549 char name[20 + DMU_OBJACCT_PREFIX_LEN];
1550 int delta = subtract ? -1 : 1;
1551
1552 (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1553 (longlong_t)user);
1554 userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1555
1556 (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1557 (longlong_t)group);
1558 userquota_update_cache(&cache->uqc_group_deltas, name, delta);
1559 }
1560}
1561
1562typedef struct userquota_updates_arg {
1563 objset_t *uua_os;
1564 int uua_sublist_idx;
1565 dmu_tx_t *uua_tx;
1566} userquota_updates_arg_t;
1567
1568static void
1569userquota_updates_task(void *arg)
1570{
1571 userquota_updates_arg_t *uua = arg;
1572 objset_t *os = uua->uua_os;
1573 dmu_tx_t *tx = uua->uua_tx;
9babb374 1574 dnode_t *dn;
cae5b340 1575 userquota_cache_t cache = { { 0 } };
9babb374 1576
cae5b340
AX
1577 multilist_sublist_t *list =
1578 multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx);
9babb374 1579
cae5b340
AX
1580 ASSERT(multilist_sublist_head(list) == NULL ||
1581 dmu_objset_userused_enabled(os));
1582 avl_create(&cache.uqc_user_deltas, userquota_compare,
1583 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1584 avl_create(&cache.uqc_group_deltas, userquota_compare,
1585 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1586
1587 while ((dn = multilist_sublist_head(list)) != NULL) {
572e2857 1588 int flags;
9babb374 1589 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
9babb374
BB
1590 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1591 dn->dn_phys->dn_flags &
1592 DNODE_FLAG_USERUSED_ACCOUNTED);
1593
572e2857
BB
1594 flags = dn->dn_id_flags;
1595 ASSERT(flags);
1596 if (flags & DN_ID_OLD_EXIST) {
cae5b340
AX
1597 do_userquota_update(&cache,
1598 dn->dn_oldused, dn->dn_oldflags,
1599 dn->dn_olduid, dn->dn_oldgid, B_TRUE);
1600 do_userobjquota_update(&cache, dn->dn_oldflags,
1601 dn->dn_olduid, dn->dn_oldgid, B_TRUE);
428870ff 1602 }
572e2857 1603 if (flags & DN_ID_NEW_EXIST) {
cae5b340
AX
1604 do_userquota_update(&cache,
1605 DN_USED_BYTES(dn->dn_phys), dn->dn_phys->dn_flags,
1606 dn->dn_newuid, dn->dn_newgid, B_FALSE);
1607 do_userobjquota_update(&cache, dn->dn_phys->dn_flags,
1608 dn->dn_newuid, dn->dn_newgid, B_FALSE);
428870ff
BB
1609 }
1610
572e2857 1611 mutex_enter(&dn->dn_mtx);
428870ff
BB
1612 dn->dn_oldused = 0;
1613 dn->dn_oldflags = 0;
1614 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1615 dn->dn_olduid = dn->dn_newuid;
1616 dn->dn_oldgid = dn->dn_newgid;
1617 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1618 if (dn->dn_bonuslen == 0)
1619 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1620 else
1621 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1622 }
1623 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
9babb374
BB
1624 mutex_exit(&dn->dn_mtx);
1625
cae5b340
AX
1626 multilist_sublist_remove(list, dn);
1627 dnode_rele(dn, os->os_synced_dnodes);
1628 }
1629 do_userquota_cacheflush(os, &cache, tx);
1630 multilist_sublist_unlock(list);
1631 kmem_free(uua, sizeof (*uua));
1632}
1633
1634void
1635dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1636{
1637 if (!dmu_objset_userused_enabled(os))
1638 return;
1639
1640 /* Allocate the user/groupused objects if necessary. */
1641 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1642 VERIFY0(zap_create_claim(os,
1643 DMU_USERUSED_OBJECT,
1644 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1645 VERIFY0(zap_create_claim(os,
1646 DMU_GROUPUSED_OBJECT,
1647 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1648 }
1649
1650 for (int i = 0;
1651 i < multilist_get_num_sublists(os->os_synced_dnodes); i++) {
1652 userquota_updates_arg_t *uua =
1653 kmem_alloc(sizeof (*uua), KM_SLEEP);
1654 uua->uua_os = os;
1655 uua->uua_sublist_idx = i;
1656 uua->uua_tx = tx;
1657 /* note: caller does taskq_wait() */
1658 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1659 userquota_updates_task, uua, 0);
1660 /* callback frees uua */
9babb374
BB
1661 }
1662}
1663
428870ff
BB
1664/*
1665 * Returns a pointer to data to find uid/gid from
1666 *
1667 * If a dirty record for transaction group that is syncing can't
1668 * be found then NULL is returned. In the NULL case it is assumed
1669 * the uid/gid aren't changing.
1670 */
1671static void *
1672dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1673{
1674 dbuf_dirty_record_t *dr, **drp;
1675 void *data;
1676
1677 if (db->db_dirtycnt == 0)
1678 return (db->db.db_data); /* Nothing is changing */
1679
1680 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1681 if (dr->dr_txg == tx->tx_txg)
1682 break;
1683
572e2857 1684 if (dr == NULL) {
428870ff 1685 data = NULL;
572e2857
BB
1686 } else {
1687 dnode_t *dn;
1688
1689 DB_DNODE_ENTER(dr->dr_dbuf);
1690 dn = DB_DNODE(dr->dr_dbuf);
1691
1692 if (dn->dn_bonuslen == 0 &&
1693 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1694 data = dr->dt.dl.dr_data->b_data;
1695 else
1696 data = dr->dt.dl.dr_data;
1697
1698 DB_DNODE_EXIT(dr->dr_dbuf);
1699 }
1700
428870ff
BB
1701 return (data);
1702}
1703
1704void
1705dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1706{
1707 objset_t *os = dn->dn_objset;
1708 void *data = NULL;
1709 dmu_buf_impl_t *db = NULL;
a08ee875
LG
1710 uint64_t *user = NULL;
1711 uint64_t *group = NULL;
428870ff
BB
1712 int flags = dn->dn_id_flags;
1713 int error;
1714 boolean_t have_spill = B_FALSE;
1715
1716 if (!dmu_objset_userused_enabled(dn->dn_objset))
1717 return;
1718
1719 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1720 DN_ID_CHKED_SPILL)))
1721 return;
1722
1723 if (before && dn->dn_bonuslen != 0)
1724 data = DN_BONUS(dn->dn_phys);
1725 else if (!before && dn->dn_bonuslen != 0) {
1726 if (dn->dn_bonus) {
1727 db = dn->dn_bonus;
1728 mutex_enter(&db->db_mtx);
1729 data = dmu_objset_userquota_find_data(db, tx);
1730 } else {
1731 data = DN_BONUS(dn->dn_phys);
1732 }
1733 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1734 int rf = 0;
1735
1736 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1737 rf |= DB_RF_HAVESTRUCT;
572e2857
BB
1738 error = dmu_spill_hold_by_dnode(dn,
1739 rf | DB_RF_MUST_SUCCEED,
428870ff
BB
1740 FTAG, (dmu_buf_t **)&db);
1741 ASSERT(error == 0);
1742 mutex_enter(&db->db_mtx);
1743 data = (before) ? db->db.db_data :
1744 dmu_objset_userquota_find_data(db, tx);
1745 have_spill = B_TRUE;
1746 } else {
1747 mutex_enter(&dn->dn_mtx);
1748 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1749 mutex_exit(&dn->dn_mtx);
1750 return;
1751 }
1752
1753 if (before) {
1754 ASSERT(data);
1755 user = &dn->dn_olduid;
1756 group = &dn->dn_oldgid;
1757 } else if (data) {
1758 user = &dn->dn_newuid;
1759 group = &dn->dn_newgid;
1760 }
1761
1762 /*
1763 * Must always call the callback in case the object
1764 * type has changed and that type isn't an object type to track
1765 */
1766 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1767 user, group);
1768
1769 /*
1770 * Preserve existing uid/gid when the callback can't determine
1771 * what the new uid/gid are and the callback returned EEXIST.
1772 * The EEXIST error tells us to just use the existing uid/gid.
1773 * If we don't know what the old values are then just assign
1774 * them to 0, since that is a new file being created.
1775 */
1776 if (!before && data == NULL && error == EEXIST) {
1777 if (flags & DN_ID_OLD_EXIST) {
1778 dn->dn_newuid = dn->dn_olduid;
1779 dn->dn_newgid = dn->dn_oldgid;
1780 } else {
1781 dn->dn_newuid = 0;
1782 dn->dn_newgid = 0;
1783 }
1784 error = 0;
1785 }
1786
1787 if (db)
1788 mutex_exit(&db->db_mtx);
1789
1790 mutex_enter(&dn->dn_mtx);
1791 if (error == 0 && before)
1792 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1793 if (error == 0 && !before)
1794 dn->dn_id_flags |= DN_ID_NEW_EXIST;
1795
1796 if (have_spill) {
1797 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1798 } else {
1799 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1800 }
1801 mutex_exit(&dn->dn_mtx);
1802 if (have_spill)
1803 dmu_buf_rele((dmu_buf_t *)db, FTAG);
1804}
1805
9babb374
BB
1806boolean_t
1807dmu_objset_userspace_present(objset_t *os)
1808{
428870ff 1809 return (os->os_phys->os_flags &
9babb374
BB
1810 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1811}
1812
cae5b340
AX
1813boolean_t
1814dmu_objset_userobjspace_present(objset_t *os)
1815{
1816 return (os->os_phys->os_flags &
1817 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE);
1818}
1819
1820static int
1821dmu_objset_space_upgrade(objset_t *os)
9babb374
BB
1822{
1823 uint64_t obj;
1824 int err = 0;
1825
9babb374
BB
1826 /*
1827 * We simply need to mark every object dirty, so that it will be
1828 * synced out and now accounted. If this is called
1829 * concurrently, or if we already did some work before crashing,
1830 * that's fine, since we track each object's accounted state
1831 * independently.
1832 */
1833
1834 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
45d1cae3 1835 dmu_tx_t *tx;
9babb374
BB
1836 dmu_buf_t *db;
1837 int objerr;
1838
cae5b340
AX
1839 mutex_enter(&os->os_upgrade_lock);
1840 if (os->os_upgrade_exit)
1841 err = SET_ERROR(EINTR);
1842 mutex_exit(&os->os_upgrade_lock);
1843 if (err != 0)
1844 return (err);
1845
9babb374 1846 if (issig(JUSTLOOKING) && issig(FORREAL))
a08ee875 1847 return (SET_ERROR(EINTR));
9babb374
BB
1848
1849 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
a08ee875 1850 if (objerr != 0)
9babb374 1851 continue;
45d1cae3 1852 tx = dmu_tx_create(os);
9babb374
BB
1853 dmu_tx_hold_bonus(tx, obj);
1854 objerr = dmu_tx_assign(tx, TXG_WAIT);
a08ee875 1855 if (objerr != 0) {
9babb374
BB
1856 dmu_tx_abort(tx);
1857 continue;
1858 }
1859 dmu_buf_will_dirty(db, tx);
1860 dmu_buf_rele(db, FTAG);
1861 dmu_tx_commit(tx);
1862 }
cae5b340
AX
1863 return (0);
1864}
1865
1866int
1867dmu_objset_userspace_upgrade(objset_t *os)
1868{
1869 int err = 0;
1870
1871 if (dmu_objset_userspace_present(os))
1872 return (0);
1873 if (dmu_objset_is_snapshot(os))
1874 return (SET_ERROR(EINVAL));
1875 if (!dmu_objset_userused_enabled(os))
1876 return (SET_ERROR(ENOTSUP));
1877
1878 err = dmu_objset_space_upgrade(os);
1879 if (err)
1880 return (err);
9babb374 1881
428870ff 1882 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
9babb374
BB
1883 txg_wait_synced(dmu_objset_pool(os), 0);
1884 return (0);
1885}
1886
cae5b340
AX
1887static int
1888dmu_objset_userobjspace_upgrade_cb(objset_t *os)
1889{
1890 int err = 0;
1891
1892 if (dmu_objset_userobjspace_present(os))
1893 return (0);
1894 if (dmu_objset_is_snapshot(os))
1895 return (SET_ERROR(EINVAL));
1896 if (!dmu_objset_userobjused_enabled(os))
1897 return (SET_ERROR(ENOTSUP));
1898
1899 dmu_objset_ds(os)->ds_feature_activation_needed[
1900 SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE;
1901
1902 err = dmu_objset_space_upgrade(os);
1903 if (err)
1904 return (err);
1905
1906 os->os_flags |= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
1907 txg_wait_synced(dmu_objset_pool(os), 0);
1908 return (0);
1909}
1910
1911void
1912dmu_objset_userobjspace_upgrade(objset_t *os)
1913{
1914 dmu_objset_upgrade(os, dmu_objset_userobjspace_upgrade_cb);
1915}
1916
1917boolean_t
1918dmu_objset_userobjspace_upgradable(objset_t *os)
1919{
1920 return (dmu_objset_type(os) == DMU_OST_ZFS &&
1921 !dmu_objset_is_snapshot(os) &&
1922 dmu_objset_userobjused_enabled(os) &&
1923 !dmu_objset_userobjspace_present(os));
1924}
1925
34dc7c2f
BB
1926void
1927dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1928 uint64_t *usedobjsp, uint64_t *availobjsp)
1929{
428870ff 1930 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
34dc7c2f
BB
1931 usedobjsp, availobjsp);
1932}
1933
1934uint64_t
1935dmu_objset_fsid_guid(objset_t *os)
1936{
428870ff 1937 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
34dc7c2f
BB
1938}
1939
1940void
1941dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1942{
428870ff
BB
1943 stat->dds_type = os->os_phys->os_type;
1944 if (os->os_dsl_dataset)
1945 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
34dc7c2f
BB
1946}
1947
1948void
1949dmu_objset_stats(objset_t *os, nvlist_t *nv)
1950{
428870ff
BB
1951 ASSERT(os->os_dsl_dataset ||
1952 os->os_phys->os_type == DMU_OST_META);
34dc7c2f 1953
428870ff
BB
1954 if (os->os_dsl_dataset != NULL)
1955 dsl_dataset_stats(os->os_dsl_dataset, nv);
34dc7c2f
BB
1956
1957 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
428870ff 1958 os->os_phys->os_type);
9babb374
BB
1959 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1960 dmu_objset_userspace_present(os));
34dc7c2f
BB
1961}
1962
1963int
1964dmu_objset_is_snapshot(objset_t *os)
1965{
428870ff 1966 if (os->os_dsl_dataset != NULL)
e10b0808 1967 return (os->os_dsl_dataset->ds_is_snapshot);
34dc7c2f
BB
1968 else
1969 return (B_FALSE);
1970}
1971
1972int
1973dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1974 boolean_t *conflict)
1975{
428870ff 1976 dsl_dataset_t *ds = os->os_dsl_dataset;
34dc7c2f
BB
1977 uint64_t ignored;
1978
e10b0808 1979 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
a08ee875 1980 return (SET_ERROR(ENOENT));
34dc7c2f
BB
1981
1982 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
e10b0808 1983 dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
cae5b340 1984 MT_NORMALIZE, real, maxlen, conflict));
34dc7c2f
BB
1985}
1986
1987int
1988dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1989 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1990{
428870ff 1991 dsl_dataset_t *ds = os->os_dsl_dataset;
34dc7c2f
BB
1992 zap_cursor_t cursor;
1993 zap_attribute_t attr;
1994
a08ee875
LG
1995 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1996
e10b0808 1997 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
a08ee875 1998 return (SET_ERROR(ENOENT));
34dc7c2f
BB
1999
2000 zap_cursor_init_serialized(&cursor,
2001 ds->ds_dir->dd_pool->dp_meta_objset,
e10b0808 2002 dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
34dc7c2f
BB
2003
2004 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2005 zap_cursor_fini(&cursor);
a08ee875 2006 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2007 }
2008
2009 if (strlen(attr.za_name) + 1 > namelen) {
2010 zap_cursor_fini(&cursor);
a08ee875 2011 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f
BB
2012 }
2013
2014 (void) strcpy(name, attr.za_name);
2015 if (idp)
2016 *idp = attr.za_first_integer;
2017 if (case_conflict)
2018 *case_conflict = attr.za_normalization_conflict;
2019 zap_cursor_advance(&cursor);
2020 *offp = zap_cursor_serialize(&cursor);
2021 zap_cursor_fini(&cursor);
2022
2023 return (0);
2024}
2025
ebe7e575 2026int
6772fb67 2027dmu_snapshot_lookup(objset_t *os, const char *name, uint64_t *value)
ebe7e575 2028{
a08ee875 2029 return (dsl_dataset_snap_lookup(os->os_dsl_dataset, name, value));
ebe7e575
BB
2030}
2031
34dc7c2f
BB
2032int
2033dmu_dir_list_next(objset_t *os, int namelen, char *name,
2034 uint64_t *idp, uint64_t *offp)
2035{
428870ff 2036 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
34dc7c2f
BB
2037 zap_cursor_t cursor;
2038 zap_attribute_t attr;
2039
2040 /* there is no next dir on a snapshot! */
428870ff 2041 if (os->os_dsl_dataset->ds_object !=
e10b0808 2042 dsl_dir_phys(dd)->dd_head_dataset_obj)
a08ee875 2043 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2044
2045 zap_cursor_init_serialized(&cursor,
2046 dd->dd_pool->dp_meta_objset,
e10b0808 2047 dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
34dc7c2f
BB
2048
2049 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2050 zap_cursor_fini(&cursor);
a08ee875 2051 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2052 }
2053
2054 if (strlen(attr.za_name) + 1 > namelen) {
2055 zap_cursor_fini(&cursor);
a08ee875 2056 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f
BB
2057 }
2058
2059 (void) strcpy(name, attr.za_name);
2060 if (idp)
2061 *idp = attr.za_first_integer;
2062 zap_cursor_advance(&cursor);
2063 *offp = zap_cursor_serialize(&cursor);
2064 zap_cursor_fini(&cursor);
2065
2066 return (0);
2067}
2068
e10b0808
AX
2069typedef struct dmu_objset_find_ctx {
2070 taskq_t *dc_tq;
2071 dsl_pool_t *dc_dp;
2072 uint64_t dc_ddobj;
cae5b340 2073 char *dc_ddname; /* last component of ddobj's name */
e10b0808
AX
2074 int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
2075 void *dc_arg;
2076 int dc_flags;
2077 kmutex_t *dc_error_lock;
2078 int *dc_error;
2079} dmu_objset_find_ctx_t;
2080
2081static void
2082dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
b128c09f 2083{
e10b0808
AX
2084 dsl_pool_t *dp = dcp->dc_dp;
2085 dmu_objset_find_ctx_t *child_dcp;
a08ee875
LG
2086 dsl_dir_t *dd;
2087 dsl_dataset_t *ds;
2088 zap_cursor_t zc;
2089 zap_attribute_t *attr;
2090 uint64_t thisobj;
e10b0808 2091 int err = 0;
a08ee875 2092
e10b0808
AX
2093 /* don't process if there already was an error */
2094 if (*dcp->dc_error != 0)
2095 goto out;
a08ee875 2096
cae5b340
AX
2097 /*
2098 * Note: passing the name (dc_ddname) here is optional, but it
2099 * improves performance because we don't need to call
2100 * zap_value_search() to determine the name.
2101 */
2102 err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
a08ee875 2103 if (err != 0)
e10b0808 2104 goto out;
a08ee875
LG
2105
2106 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2107 if (dd->dd_myname[0] == '$') {
2108 dsl_dir_rele(dd, FTAG);
e10b0808 2109 goto out;
a08ee875
LG
2110 }
2111
e10b0808 2112 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
ea04106b 2113 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
a08ee875
LG
2114
2115 /*
2116 * Iterate over all children.
2117 */
e10b0808 2118 if (dcp->dc_flags & DS_FIND_CHILDREN) {
a08ee875 2119 for (zap_cursor_init(&zc, dp->dp_meta_objset,
e10b0808 2120 dsl_dir_phys(dd)->dd_child_dir_zapobj);
a08ee875
LG
2121 zap_cursor_retrieve(&zc, attr) == 0;
2122 (void) zap_cursor_advance(&zc)) {
2123 ASSERT3U(attr->za_integer_length, ==,
2124 sizeof (uint64_t));
2125 ASSERT3U(attr->za_num_integers, ==, 1);
2126
cae5b340
AX
2127 child_dcp =
2128 kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
e10b0808
AX
2129 *child_dcp = *dcp;
2130 child_dcp->dc_ddobj = attr->za_first_integer;
cae5b340 2131 child_dcp->dc_ddname = spa_strdup(attr->za_name);
e10b0808
AX
2132 if (dcp->dc_tq != NULL)
2133 (void) taskq_dispatch(dcp->dc_tq,
2134 dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
2135 else
2136 dmu_objset_find_dp_impl(child_dcp);
a08ee875
LG
2137 }
2138 zap_cursor_fini(&zc);
a08ee875
LG
2139 }
2140
2141 /*
2142 * Iterate over all snapshots.
2143 */
e10b0808 2144 if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
a08ee875
LG
2145 dsl_dataset_t *ds;
2146 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2147
2148 if (err == 0) {
e10b0808
AX
2149 uint64_t snapobj;
2150
2151 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
a08ee875
LG
2152 dsl_dataset_rele(ds, FTAG);
2153
2154 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2155 zap_cursor_retrieve(&zc, attr) == 0;
2156 (void) zap_cursor_advance(&zc)) {
2157 ASSERT3U(attr->za_integer_length, ==,
2158 sizeof (uint64_t));
2159 ASSERT3U(attr->za_num_integers, ==, 1);
2160
2161 err = dsl_dataset_hold_obj(dp,
2162 attr->za_first_integer, FTAG, &ds);
2163 if (err != 0)
2164 break;
e10b0808 2165 err = dcp->dc_func(dp, ds, dcp->dc_arg);
a08ee875
LG
2166 dsl_dataset_rele(ds, FTAG);
2167 if (err != 0)
2168 break;
2169 }
2170 zap_cursor_fini(&zc);
2171 }
2172 }
2173
a08ee875
LG
2174 kmem_free(attr, sizeof (zap_attribute_t));
2175
cae5b340
AX
2176 if (err != 0) {
2177 dsl_dir_rele(dd, FTAG);
e10b0808 2178 goto out;
cae5b340 2179 }
a08ee875
LG
2180
2181 /*
2182 * Apply to self.
2183 */
2184 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
cae5b340
AX
2185
2186 /*
2187 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2188 * that the dir will remain cached, and we won't have to re-instantiate
2189 * it (which could be expensive due to finding its name via
2190 * zap_value_search()).
2191 */
2192 dsl_dir_rele(dd, FTAG);
a08ee875 2193 if (err != 0)
e10b0808
AX
2194 goto out;
2195 err = dcp->dc_func(dp, ds, dcp->dc_arg);
a08ee875 2196 dsl_dataset_rele(ds, FTAG);
e10b0808
AX
2197
2198out:
2199 if (err != 0) {
2200 mutex_enter(dcp->dc_error_lock);
2201 /* only keep first error */
2202 if (*dcp->dc_error == 0)
2203 *dcp->dc_error = err;
2204 mutex_exit(dcp->dc_error_lock);
2205 }
2206
cae5b340
AX
2207 if (dcp->dc_ddname != NULL)
2208 spa_strfree(dcp->dc_ddname);
e10b0808
AX
2209 kmem_free(dcp, sizeof (*dcp));
2210}
2211
2212static void
2213dmu_objset_find_dp_cb(void *arg)
2214{
2215 dmu_objset_find_ctx_t *dcp = arg;
2216 dsl_pool_t *dp = dcp->dc_dp;
2217
2218 /*
2219 * We need to get a pool_config_lock here, as there are several
2220 * asssert(pool_config_held) down the stack. Getting a lock via
2221 * dsl_pool_config_enter is risky, as it might be stalled by a
2222 * pending writer. This would deadlock, as the write lock can
2223 * only be granted when our parent thread gives up the lock.
2224 * The _prio interface gives us priority over a pending writer.
2225 */
2226 dsl_pool_config_enter_prio(dp, FTAG);
2227
2228 dmu_objset_find_dp_impl(dcp);
2229
2230 dsl_pool_config_exit(dp, FTAG);
2231}
2232
2233/*
2234 * Find objsets under and including ddobj, call func(ds) on each.
2235 * The order for the enumeration is completely undefined.
2236 * func is called with dsl_pool_config held.
2237 */
2238int
2239dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
2240 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
2241{
2242 int error = 0;
2243 taskq_t *tq = NULL;
2244 int ntasks;
2245 dmu_objset_find_ctx_t *dcp;
2246 kmutex_t err_lock;
2247
2248 mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
2249 dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
2250 dcp->dc_tq = NULL;
2251 dcp->dc_dp = dp;
2252 dcp->dc_ddobj = ddobj;
cae5b340 2253 dcp->dc_ddname = NULL;
e10b0808
AX
2254 dcp->dc_func = func;
2255 dcp->dc_arg = arg;
2256 dcp->dc_flags = flags;
2257 dcp->dc_error_lock = &err_lock;
2258 dcp->dc_error = &error;
2259
2260 if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
2261 /*
2262 * In case a write lock is held we can't make use of
2263 * parallelism, as down the stack of the worker threads
2264 * the lock is asserted via dsl_pool_config_held.
2265 * In case of a read lock this is solved by getting a read
2266 * lock in each worker thread, which isn't possible in case
2267 * of a writer lock. So we fall back to the synchronous path
2268 * here.
2269 * In the future it might be possible to get some magic into
2270 * dsl_pool_config_held in a way that it returns true for
2271 * the worker threads so that a single lock held from this
2272 * thread suffices. For now, stay single threaded.
2273 */
2274 dmu_objset_find_dp_impl(dcp);
cae5b340 2275 mutex_destroy(&err_lock);
e10b0808
AX
2276
2277 return (error);
2278 }
2279
2280 ntasks = dmu_find_threads;
2281 if (ntasks == 0)
2282 ntasks = vdev_count_leaves(dp->dp_spa) * 4;
2283 tq = taskq_create("dmu_objset_find", ntasks, maxclsyspri, ntasks,
2284 INT_MAX, 0);
2285 if (tq == NULL) {
2286 kmem_free(dcp, sizeof (*dcp));
cae5b340
AX
2287 mutex_destroy(&err_lock);
2288
e10b0808
AX
2289 return (SET_ERROR(ENOMEM));
2290 }
2291 dcp->dc_tq = tq;
2292
2293 /* dcp will be freed by task */
2294 (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
2295
2296 /*
2297 * PORTING: this code relies on the property of taskq_wait to wait
2298 * until no more tasks are queued and no more tasks are active. As
2299 * we always queue new tasks from within other tasks, task_wait
2300 * reliably waits for the full recursion to finish, even though we
2301 * enqueue new tasks after taskq_wait has been called.
2302 * On platforms other than illumos, taskq_wait may not have this
2303 * property.
2304 */
2305 taskq_wait(tq);
2306 taskq_destroy(tq);
2307 mutex_destroy(&err_lock);
2308
2309 return (error);
b128c09f
BB
2310}
2311
2312/*
a08ee875
LG
2313 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2314 * The dp_config_rwlock must not be held when this is called, and it
2315 * will not be held when the callback is called.
2316 * Therefore this function should only be used when the pool is not changing
2317 * (e.g. in syncing context), or the callback can deal with the possible races.
b128c09f 2318 */
a08ee875
LG
2319static int
2320dmu_objset_find_impl(spa_t *spa, const char *name,
2321 int func(const char *, void *), void *arg, int flags)
34dc7c2f
BB
2322{
2323 dsl_dir_t *dd;
a08ee875 2324 dsl_pool_t *dp = spa_get_dsl(spa);
b128c09f 2325 dsl_dataset_t *ds;
34dc7c2f
BB
2326 zap_cursor_t zc;
2327 zap_attribute_t *attr;
2328 char *child;
b128c09f
BB
2329 uint64_t thisobj;
2330 int err;
34dc7c2f 2331
a08ee875
LG
2332 dsl_pool_config_enter(dp, FTAG);
2333
2334 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
2335 if (err != 0) {
2336 dsl_pool_config_exit(dp, FTAG);
34dc7c2f 2337 return (err);
a08ee875 2338 }
34dc7c2f 2339
b128c09f
BB
2340 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2341 if (dd->dd_myname[0] == '$') {
a08ee875
LG
2342 dsl_dir_rele(dd, FTAG);
2343 dsl_pool_config_exit(dp, FTAG);
b128c09f
BB
2344 return (0);
2345 }
2346
e10b0808 2347 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
ea04106b 2348 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
34dc7c2f
BB
2349
2350 /*
2351 * Iterate over all children.
2352 */
2353 if (flags & DS_FIND_CHILDREN) {
b128c09f 2354 for (zap_cursor_init(&zc, dp->dp_meta_objset,
e10b0808 2355 dsl_dir_phys(dd)->dd_child_dir_zapobj);
34dc7c2f
BB
2356 zap_cursor_retrieve(&zc, attr) == 0;
2357 (void) zap_cursor_advance(&zc)) {
a08ee875
LG
2358 ASSERT3U(attr->za_integer_length, ==,
2359 sizeof (uint64_t));
2360 ASSERT3U(attr->za_num_integers, ==, 1);
34dc7c2f 2361
428870ff 2362 child = kmem_asprintf("%s/%s", name, attr->za_name);
a08ee875
LG
2363 dsl_pool_config_exit(dp, FTAG);
2364 err = dmu_objset_find_impl(spa, child,
2365 func, arg, flags);
2366 dsl_pool_config_enter(dp, FTAG);
428870ff 2367 strfree(child);
a08ee875 2368 if (err != 0)
34dc7c2f
BB
2369 break;
2370 }
2371 zap_cursor_fini(&zc);
2372
a08ee875
LG
2373 if (err != 0) {
2374 dsl_dir_rele(dd, FTAG);
2375 dsl_pool_config_exit(dp, FTAG);
34dc7c2f
BB
2376 kmem_free(attr, sizeof (zap_attribute_t));
2377 return (err);
2378 }
2379 }
2380
2381 /*
2382 * Iterate over all snapshots.
2383 */
b128c09f 2384 if (flags & DS_FIND_SNAPSHOTS) {
b128c09f 2385 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
b128c09f
BB
2386
2387 if (err == 0) {
e10b0808
AX
2388 uint64_t snapobj;
2389
2390 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
b128c09f
BB
2391 dsl_dataset_rele(ds, FTAG);
2392
2393 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2394 zap_cursor_retrieve(&zc, attr) == 0;
2395 (void) zap_cursor_advance(&zc)) {
a08ee875 2396 ASSERT3U(attr->za_integer_length, ==,
b128c09f 2397 sizeof (uint64_t));
a08ee875 2398 ASSERT3U(attr->za_num_integers, ==, 1);
b128c09f 2399
428870ff
BB
2400 child = kmem_asprintf("%s@%s",
2401 name, attr->za_name);
a08ee875
LG
2402 dsl_pool_config_exit(dp, FTAG);
2403 err = func(child, arg);
2404 dsl_pool_config_enter(dp, FTAG);
428870ff 2405 strfree(child);
a08ee875 2406 if (err != 0)
b128c09f
BB
2407 break;
2408 }
2409 zap_cursor_fini(&zc);
34dc7c2f 2410 }
34dc7c2f
BB
2411 }
2412
a08ee875 2413 dsl_dir_rele(dd, FTAG);
34dc7c2f 2414 kmem_free(attr, sizeof (zap_attribute_t));
a08ee875 2415 dsl_pool_config_exit(dp, FTAG);
34dc7c2f 2416
a08ee875 2417 if (err != 0)
34dc7c2f
BB
2418 return (err);
2419
a08ee875
LG
2420 /* Apply to self. */
2421 return (func(name, arg));
34dc7c2f
BB
2422}
2423
a08ee875
LG
2424/*
2425 * See comment above dmu_objset_find_impl().
2426 */
d164b209 2427int
a08ee875
LG
2428dmu_objset_find(char *name, int func(const char *, void *), void *arg,
2429 int flags)
d164b209 2430{
a08ee875
LG
2431 spa_t *spa;
2432 int error;
d164b209 2433
a08ee875
LG
2434 error = spa_open(name, &spa, FTAG);
2435 if (error != 0)
2436 return (error);
2437 error = dmu_objset_find_impl(spa, name, func, arg, flags);
2438 spa_close(spa, FTAG);
2439 return (error);
d164b209
BB
2440}
2441
34dc7c2f
BB
2442void
2443dmu_objset_set_user(objset_t *os, void *user_ptr)
2444{
428870ff
BB
2445 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2446 os->os_user_ptr = user_ptr;
34dc7c2f
BB
2447}
2448
2449void *
2450dmu_objset_get_user(objset_t *os)
2451{
428870ff
BB
2452 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2453 return (os->os_user_ptr);
34dc7c2f 2454}
c28b2279 2455
a08ee875
LG
2456/*
2457 * Determine name of filesystem, given name of snapshot.
cae5b340 2458 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
a08ee875
LG
2459 */
2460int
2461dmu_fsname(const char *snapname, char *buf)
2462{
2463 char *atp = strchr(snapname, '@');
2464 if (atp == NULL)
2465 return (SET_ERROR(EINVAL));
cae5b340 2466 if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
a08ee875
LG
2467 return (SET_ERROR(ENAMETOOLONG));
2468 (void) strlcpy(buf, snapname, atp - snapname + 1);
2469 return (0);
2470}
2471
cae5b340
AX
2472/*
2473 * Call when we think we're going to write/free space in open context to track
2474 * the amount of dirty data in the open txg, which is also the amount
2475 * of memory that can not be evicted until this txg syncs.
2476 */
2477void
2478dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
2479{
2480 dsl_dataset_t *ds = os->os_dsl_dataset;
2481 int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
2482
2483 if (ds != NULL) {
2484 dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
2485 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
2486 }
2487}
2488
c28b2279 2489#if defined(_KERNEL) && defined(HAVE_SPL)
f0fd83be 2490EXPORT_SYMBOL(dmu_objset_zil);
c28b2279 2491EXPORT_SYMBOL(dmu_objset_pool);
f0fd83be
BB
2492EXPORT_SYMBOL(dmu_objset_ds);
2493EXPORT_SYMBOL(dmu_objset_type);
c28b2279
BB
2494EXPORT_SYMBOL(dmu_objset_name);
2495EXPORT_SYMBOL(dmu_objset_hold);
2496EXPORT_SYMBOL(dmu_objset_own);
2497EXPORT_SYMBOL(dmu_objset_rele);
2498EXPORT_SYMBOL(dmu_objset_disown);
2499EXPORT_SYMBOL(dmu_objset_from_ds);
2500EXPORT_SYMBOL(dmu_objset_create);
2501EXPORT_SYMBOL(dmu_objset_clone);
c28b2279
BB
2502EXPORT_SYMBOL(dmu_objset_stats);
2503EXPORT_SYMBOL(dmu_objset_fast_stat);
54a179e7 2504EXPORT_SYMBOL(dmu_objset_spa);
c28b2279
BB
2505EXPORT_SYMBOL(dmu_objset_space);
2506EXPORT_SYMBOL(dmu_objset_fsid_guid);
2507EXPORT_SYMBOL(dmu_objset_find);
c28b2279
BB
2508EXPORT_SYMBOL(dmu_objset_byteswap);
2509EXPORT_SYMBOL(dmu_objset_evict_dbufs);
2510EXPORT_SYMBOL(dmu_objset_snap_cmtime);
cae5b340 2511EXPORT_SYMBOL(dmu_objset_dnodesize);
c28b2279
BB
2512
2513EXPORT_SYMBOL(dmu_objset_sync);
2514EXPORT_SYMBOL(dmu_objset_is_dirty);
2515EXPORT_SYMBOL(dmu_objset_create_impl);
2516EXPORT_SYMBOL(dmu_objset_open_impl);
2517EXPORT_SYMBOL(dmu_objset_evict);
2518EXPORT_SYMBOL(dmu_objset_register_type);
2519EXPORT_SYMBOL(dmu_objset_do_userquota_updates);
2520EXPORT_SYMBOL(dmu_objset_userquota_get_ids);
2521EXPORT_SYMBOL(dmu_objset_userused_enabled);
2522EXPORT_SYMBOL(dmu_objset_userspace_upgrade);
2523EXPORT_SYMBOL(dmu_objset_userspace_present);
cae5b340
AX
2524EXPORT_SYMBOL(dmu_objset_userobjused_enabled);
2525EXPORT_SYMBOL(dmu_objset_userobjspace_upgrade);
2526EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable);
2527EXPORT_SYMBOL(dmu_objset_userobjspace_present);
c28b2279 2528#endif