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