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