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