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