]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dmu_objset.c
Import pyzfs source code from ClusterHQ
[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 1665 /*
0c03d21a
MA
1666 * If we are either claiming the ZIL or doing a raw receive, write
1667 * out the os_phys_buf raw. Neither of these actions will effect the
1668 * MAC at this point.
b5256303 1669 */
0c03d21a
MA
1670 if (os->os_raw_receive ||
1671 os->os_next_write_raw[tx->tx_txg & TXG_MASK]) {
b5256303 1672 ASSERT(os->os_encrypted);
1b66810b 1673 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_FALSE;
b5256303
TC
1674 arc_convert_to_raw(os->os_phys_buf,
1675 os->os_dsl_dataset->ds_object, ZFS_HOST_BYTEORDER,
1676 DMU_OT_OBJSET, NULL, NULL, NULL);
1677 }
1678
428870ff 1679 zio = arc_write(pio, os->os_spa, tx->tx_txg,
cc9bb3e5 1680 blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
bc77ba73
PD
1681 &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
1682 os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
34dc7c2f
BB
1683
1684 /*
9babb374 1685 * Sync special dnodes - the parent IO for the sync is the root block
34dc7c2f 1686 */
572e2857
BB
1687 DMU_META_DNODE(os)->dn_zio = zio;
1688 dnode_sync(DMU_META_DNODE(os), tx);
34dc7c2f 1689
9babb374
BB
1690 os->os_phys->os_flags = os->os_flags;
1691
572e2857
BB
1692 if (DMU_USERUSED_DNODE(os) &&
1693 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1694 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1695 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1696 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1697 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
9babb374
BB
1698 }
1699
9c5167d1
NF
1700 if (DMU_PROJECTUSED_DNODE(os) &&
1701 DMU_PROJECTUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1702 DMU_PROJECTUSED_DNODE(os)->dn_zio = zio;
1703 dnode_sync(DMU_PROJECTUSED_DNODE(os), tx);
1704 }
1705
34dc7c2f
BB
1706 txgoff = tx->tx_txg & TXG_MASK;
1707
b5256303
TC
1708 if (dmu_objset_userused_enabled(os) &&
1709 (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
9babb374
BB
1710 /*
1711 * We must create the list here because it uses the
64fc7762
MA
1712 * dn_dirty_link[] of this txg. But it may already
1713 * exist because we call dsl_dataset_sync() twice per txg.
9babb374 1714 */
64fc7762
MA
1715 if (os->os_synced_dnodes == NULL) {
1716 os->os_synced_dnodes =
1717 multilist_create(sizeof (dnode_t),
1718 offsetof(dnode_t, dn_dirty_link[txgoff]),
1719 dnode_multilist_index_func);
1720 } else {
1721 ASSERT3U(os->os_synced_dnodes->ml_offset, ==,
1722 offsetof(dnode_t, dn_dirty_link[txgoff]));
1723 }
9babb374
BB
1724 }
1725
64fc7762
MA
1726 for (int i = 0;
1727 i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) {
1728 sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
1729 sda->sda_list = os->os_dirty_dnodes[txgoff];
1730 sda->sda_sublist_idx = i;
1731 sda->sda_tx = tx;
1732 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1733 sync_dnodes_task, sda, 0);
1734 /* callback frees sda */
1735 }
1736 taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
34dc7c2f 1737
572e2857 1738 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
64fc7762 1739 while ((dr = list_head(list)) != NULL) {
13fe0198 1740 ASSERT0(dr->dr_dbuf->db_level);
34dc7c2f
BB
1741 list_remove(list, dr);
1742 if (dr->dr_zio)
1743 zio_nowait(dr->dr_zio);
1744 }
68cbd56e
NB
1745
1746 /* Enable dnode backfill if enough objects have been freed. */
1747 if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1748 os->os_rescan_dnodes = B_TRUE;
1749 os->os_freed_dnodes = 0;
1750 }
1751
34dc7c2f
BB
1752 /*
1753 * Free intent log blocks up to this tx.
1754 */
1755 zil_sync(os->os_zil, tx);
b128c09f 1756 os->os_phys->os_zil_header = os->os_zil_header;
34dc7c2f
BB
1757 zio_nowait(zio);
1758}
1759
428870ff
BB
1760boolean_t
1761dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1762{
64fc7762 1763 return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK]));
428870ff
BB
1764}
1765
572e2857 1766static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
9babb374
BB
1767
1768void
1769dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1770{
1771 used_cbs[ost] = cb;
1772}
1773
1774boolean_t
428870ff 1775dmu_objset_userused_enabled(objset_t *os)
9babb374
BB
1776{
1777 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
572e2857
BB
1778 used_cbs[os->os_phys->os_type] != NULL &&
1779 DMU_USERUSED_DNODE(os) != NULL);
9babb374
BB
1780}
1781
1de321e6
JX
1782boolean_t
1783dmu_objset_userobjused_enabled(objset_t *os)
1784{
1785 return (dmu_objset_userused_enabled(os) &&
1786 spa_feature_is_enabled(os->os_spa, SPA_FEATURE_USEROBJ_ACCOUNTING));
1787}
1788
9c5167d1
NF
1789boolean_t
1790dmu_objset_projectquota_enabled(objset_t *os)
1791{
1792 return (used_cbs[os->os_phys->os_type] != NULL &&
1793 DMU_PROJECTUSED_DNODE(os) != NULL &&
1794 spa_feature_is_enabled(os->os_spa, SPA_FEATURE_PROJECT_QUOTA));
1795}
1796
9b7a83cb
JX
1797typedef struct userquota_node {
1798 /* must be in the first filed, see userquota_update_cache() */
1799 char uqn_id[20 + DMU_OBJACCT_PREFIX_LEN];
1800 int64_t uqn_delta;
1801 avl_node_t uqn_node;
1802} userquota_node_t;
1803
1804typedef struct userquota_cache {
1805 avl_tree_t uqc_user_deltas;
1806 avl_tree_t uqc_group_deltas;
9c5167d1 1807 avl_tree_t uqc_project_deltas;
9b7a83cb
JX
1808} userquota_cache_t;
1809
1810static int
1811userquota_compare(const void *l, const void *r)
1812{
1813 const userquota_node_t *luqn = l;
1814 const userquota_node_t *ruqn = r;
e4ffa98d 1815 int rv;
9b7a83cb
JX
1816
1817 /*
1818 * NB: can only access uqn_id because userquota_update_cache() doesn't
1819 * pass in an entire userquota_node_t.
1820 */
e4ffa98d
BB
1821 rv = strcmp(luqn->uqn_id, ruqn->uqn_id);
1822
1823 return (AVL_ISIGN(rv));
9b7a83cb
JX
1824}
1825
1826static void
1827do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
1828{
1829 void *cookie;
1830 userquota_node_t *uqn;
1831
1832 ASSERT(dmu_tx_is_syncing(tx));
1833
1834 cookie = NULL;
1835 while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
1836 &cookie)) != NULL) {
64fc7762
MA
1837 /*
1838 * os_userused_lock protects against concurrent calls to
1839 * zap_increment_int(). It's needed because zap_increment_int()
1840 * is not thread-safe (i.e. not atomic).
1841 */
1842 mutex_enter(&os->os_userused_lock);
9b7a83cb
JX
1843 VERIFY0(zap_increment(os, DMU_USERUSED_OBJECT,
1844 uqn->uqn_id, uqn->uqn_delta, tx));
64fc7762 1845 mutex_exit(&os->os_userused_lock);
9b7a83cb
JX
1846 kmem_free(uqn, sizeof (*uqn));
1847 }
1848 avl_destroy(&cache->uqc_user_deltas);
1849
1850 cookie = NULL;
1851 while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
1852 &cookie)) != NULL) {
64fc7762 1853 mutex_enter(&os->os_userused_lock);
9b7a83cb
JX
1854 VERIFY0(zap_increment(os, DMU_GROUPUSED_OBJECT,
1855 uqn->uqn_id, uqn->uqn_delta, tx));
64fc7762 1856 mutex_exit(&os->os_userused_lock);
9b7a83cb
JX
1857 kmem_free(uqn, sizeof (*uqn));
1858 }
1859 avl_destroy(&cache->uqc_group_deltas);
9c5167d1
NF
1860
1861 if (dmu_objset_projectquota_enabled(os)) {
1862 cookie = NULL;
1863 while ((uqn = avl_destroy_nodes(&cache->uqc_project_deltas,
1864 &cookie)) != NULL) {
1865 mutex_enter(&os->os_userused_lock);
1866 VERIFY0(zap_increment(os, DMU_PROJECTUSED_OBJECT,
1867 uqn->uqn_id, uqn->uqn_delta, tx));
1868 mutex_exit(&os->os_userused_lock);
1869 kmem_free(uqn, sizeof (*uqn));
1870 }
1871 avl_destroy(&cache->uqc_project_deltas);
1872 }
9b7a83cb
JX
1873}
1874
1875static void
1876userquota_update_cache(avl_tree_t *avl, const char *id, int64_t delta)
1877{
1878 userquota_node_t *uqn;
1879 avl_index_t idx;
1880
1881 ASSERT(strlen(id) < sizeof (uqn->uqn_id));
1882 /*
1883 * Use id directly for searching because uqn_id is the first field of
1884 * userquota_node_t and fields after uqn_id won't be accessed in
1885 * avl_find().
1886 */
1887 uqn = avl_find(avl, (const void *)id, &idx);
1888 if (uqn == NULL) {
1889 uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
1f51b525 1890 strlcpy(uqn->uqn_id, id, sizeof (uqn->uqn_id));
9b7a83cb
JX
1891 avl_insert(avl, uqn, idx);
1892 }
1893 uqn->uqn_delta += delta;
1894}
1895
428870ff 1896static void
9c5167d1
NF
1897do_userquota_update(objset_t *os, userquota_cache_t *cache, uint64_t used,
1898 uint64_t flags, uint64_t user, uint64_t group, uint64_t project,
1899 boolean_t subtract)
428870ff 1900{
9c5167d1 1901 if (flags & DNODE_FLAG_USERUSED_ACCOUNTED) {
50c957f7 1902 int64_t delta = DNODE_MIN_SIZE + used;
9b7a83cb
JX
1903 char name[20];
1904
428870ff
BB
1905 if (subtract)
1906 delta = -delta;
9b7a83cb
JX
1907
1908 (void) sprintf(name, "%llx", (longlong_t)user);
1909 userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1910
1911 (void) sprintf(name, "%llx", (longlong_t)group);
1912 userquota_update_cache(&cache->uqc_group_deltas, name, delta);
9c5167d1
NF
1913
1914 if (dmu_objset_projectquota_enabled(os)) {
1915 (void) sprintf(name, "%llx", (longlong_t)project);
1916 userquota_update_cache(&cache->uqc_project_deltas,
1917 name, delta);
1918 }
428870ff
BB
1919 }
1920}
1921
1de321e6 1922static void
9c5167d1
NF
1923do_userobjquota_update(objset_t *os, userquota_cache_t *cache, uint64_t flags,
1924 uint64_t user, uint64_t group, uint64_t project, boolean_t subtract)
1de321e6
JX
1925{
1926 if (flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) {
1927 char name[20 + DMU_OBJACCT_PREFIX_LEN];
9b7a83cb 1928 int delta = subtract ? -1 : 1;
1de321e6
JX
1929
1930 (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1931 (longlong_t)user);
9b7a83cb 1932 userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1de321e6
JX
1933
1934 (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1935 (longlong_t)group);
9b7a83cb 1936 userquota_update_cache(&cache->uqc_group_deltas, name, delta);
9c5167d1
NF
1937
1938 if (dmu_objset_projectquota_enabled(os)) {
1939 (void) snprintf(name, sizeof (name),
1940 DMU_OBJACCT_PREFIX "%llx", (longlong_t)project);
1941 userquota_update_cache(&cache->uqc_project_deltas,
1942 name, delta);
1943 }
1de321e6
JX
1944 }
1945}
1946
64fc7762
MA
1947typedef struct userquota_updates_arg {
1948 objset_t *uua_os;
1949 int uua_sublist_idx;
1950 dmu_tx_t *uua_tx;
1951} userquota_updates_arg_t;
1952
1953static void
1954userquota_updates_task(void *arg)
9babb374 1955{
64fc7762
MA
1956 userquota_updates_arg_t *uua = arg;
1957 objset_t *os = uua->uua_os;
1958 dmu_tx_t *tx = uua->uua_tx;
9babb374 1959 dnode_t *dn;
9b7a83cb 1960 userquota_cache_t cache = { { 0 } };
9babb374 1961
64fc7762
MA
1962 multilist_sublist_t *list =
1963 multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx);
9babb374 1964
64fc7762
MA
1965 ASSERT(multilist_sublist_head(list) == NULL ||
1966 dmu_objset_userused_enabled(os));
9b7a83cb
JX
1967 avl_create(&cache.uqc_user_deltas, userquota_compare,
1968 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1969 avl_create(&cache.uqc_group_deltas, userquota_compare,
1970 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
9c5167d1
NF
1971 if (dmu_objset_projectquota_enabled(os))
1972 avl_create(&cache.uqc_project_deltas, userquota_compare,
1973 sizeof (userquota_node_t), offsetof(userquota_node_t,
1974 uqn_node));
9b7a83cb 1975
64fc7762 1976 while ((dn = multilist_sublist_head(list)) != NULL) {
572e2857 1977 int flags;
9babb374 1978 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
9babb374
BB
1979 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1980 dn->dn_phys->dn_flags &
1981 DNODE_FLAG_USERUSED_ACCOUNTED);
1982
572e2857
BB
1983 flags = dn->dn_id_flags;
1984 ASSERT(flags);
1985 if (flags & DN_ID_OLD_EXIST) {
9c5167d1
NF
1986 do_userquota_update(os, &cache, dn->dn_oldused,
1987 dn->dn_oldflags, dn->dn_olduid, dn->dn_oldgid,
1988 dn->dn_oldprojid, B_TRUE);
1989 do_userobjquota_update(os, &cache, dn->dn_oldflags,
1990 dn->dn_olduid, dn->dn_oldgid,
1991 dn->dn_oldprojid, B_TRUE);
428870ff 1992 }
572e2857 1993 if (flags & DN_ID_NEW_EXIST) {
9c5167d1 1994 do_userquota_update(os, &cache,
9b7a83cb 1995 DN_USED_BYTES(dn->dn_phys), dn->dn_phys->dn_flags,
9c5167d1
NF
1996 dn->dn_newuid, dn->dn_newgid,
1997 dn->dn_newprojid, B_FALSE);
1998 do_userobjquota_update(os, &cache,
1999 dn->dn_phys->dn_flags, dn->dn_newuid, dn->dn_newgid,
2000 dn->dn_newprojid, B_FALSE);
428870ff
BB
2001 }
2002
572e2857 2003 mutex_enter(&dn->dn_mtx);
428870ff
BB
2004 dn->dn_oldused = 0;
2005 dn->dn_oldflags = 0;
2006 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
2007 dn->dn_olduid = dn->dn_newuid;
2008 dn->dn_oldgid = dn->dn_newgid;
9c5167d1 2009 dn->dn_oldprojid = dn->dn_newprojid;
428870ff
BB
2010 dn->dn_id_flags |= DN_ID_OLD_EXIST;
2011 if (dn->dn_bonuslen == 0)
2012 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
2013 else
2014 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2015 }
2016 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
edc1e713
TC
2017 if (dn->dn_dirty_txg == spa_syncing_txg(os->os_spa))
2018 dn->dn_dirty_txg = 0;
9babb374
BB
2019 mutex_exit(&dn->dn_mtx);
2020
64fc7762
MA
2021 multilist_sublist_remove(list, dn);
2022 dnode_rele(dn, os->os_synced_dnodes);
9babb374 2023 }
9b7a83cb 2024 do_userquota_cacheflush(os, &cache, tx);
64fc7762
MA
2025 multilist_sublist_unlock(list);
2026 kmem_free(uua, sizeof (*uua));
2027}
2028
2029void
2030dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
2031{
2032 if (!dmu_objset_userused_enabled(os))
2033 return;
2034
163a8c28
TC
2035 /*
2036 * If this is a raw receive just return and handle accounting
2037 * later when we have the keys loaded. We also don't do user
2038 * accounting during claiming since the datasets are not owned
2039 * for the duration of claiming and this txg should only be
2040 * used for recovery.
2041 */
b5256303
TC
2042 if (os->os_encrypted && dmu_objset_is_receiving(os))
2043 return;
2044
163a8c28
TC
2045 if (tx->tx_txg <= os->os_spa->spa_claim_max_txg)
2046 return;
2047
9c5167d1 2048 /* Allocate the user/group/project used objects if necessary. */
64fc7762
MA
2049 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
2050 VERIFY0(zap_create_claim(os,
2051 DMU_USERUSED_OBJECT,
2052 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2053 VERIFY0(zap_create_claim(os,
2054 DMU_GROUPUSED_OBJECT,
2055 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2056 }
2057
9c5167d1
NF
2058 if (dmu_objset_projectquota_enabled(os) &&
2059 DMU_PROJECTUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
2060 VERIFY0(zap_create_claim(os, DMU_PROJECTUSED_OBJECT,
2061 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2062 }
2063
64fc7762
MA
2064 for (int i = 0;
2065 i < multilist_get_num_sublists(os->os_synced_dnodes); i++) {
2066 userquota_updates_arg_t *uua =
2067 kmem_alloc(sizeof (*uua), KM_SLEEP);
2068 uua->uua_os = os;
2069 uua->uua_sublist_idx = i;
2070 uua->uua_tx = tx;
2071 /* note: caller does taskq_wait() */
2072 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
2073 userquota_updates_task, uua, 0);
2074 /* callback frees uua */
2075 }
9babb374
BB
2076}
2077
428870ff
BB
2078/*
2079 * Returns a pointer to data to find uid/gid from
2080 *
2081 * If a dirty record for transaction group that is syncing can't
2082 * be found then NULL is returned. In the NULL case it is assumed
2083 * the uid/gid aren't changing.
2084 */
2085static void *
2086dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
2087{
2088 dbuf_dirty_record_t *dr, **drp;
2089 void *data;
2090
2091 if (db->db_dirtycnt == 0)
2092 return (db->db.db_data); /* Nothing is changing */
2093
2094 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
2095 if (dr->dr_txg == tx->tx_txg)
2096 break;
2097
572e2857 2098 if (dr == NULL) {
428870ff 2099 data = NULL;
572e2857
BB
2100 } else {
2101 dnode_t *dn;
2102
2103 DB_DNODE_ENTER(dr->dr_dbuf);
2104 dn = DB_DNODE(dr->dr_dbuf);
2105
2106 if (dn->dn_bonuslen == 0 &&
2107 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
2108 data = dr->dt.dl.dr_data->b_data;
2109 else
2110 data = dr->dt.dl.dr_data;
2111
2112 DB_DNODE_EXIT(dr->dr_dbuf);
2113 }
2114
428870ff
BB
2115 return (data);
2116}
2117
2118void
2119dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
2120{
2121 objset_t *os = dn->dn_objset;
2122 void *data = NULL;
2123 dmu_buf_impl_t *db = NULL;
a117a6d6
GW
2124 uint64_t *user = NULL;
2125 uint64_t *group = NULL;
9c5167d1 2126 uint64_t *project = NULL;
428870ff
BB
2127 int flags = dn->dn_id_flags;
2128 int error;
2129 boolean_t have_spill = B_FALSE;
2130
2131 if (!dmu_objset_userused_enabled(dn->dn_objset))
2132 return;
2133
b5256303
TC
2134 /*
2135 * Raw receives introduce a problem with user accounting. Raw
2136 * receives cannot update the user accounting info because the
2137 * user ids and the sizes are encrypted. To guarantee that we
2138 * never end up with bad user accounting, we simply disable it
2139 * during raw receives. We also disable this for normal receives
2140 * so that an incremental raw receive may be done on top of an
2141 * existing non-raw receive.
2142 */
2143 if (os->os_encrypted && dmu_objset_is_receiving(os))
2144 return;
2145
428870ff
BB
2146 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
2147 DN_ID_CHKED_SPILL)))
2148 return;
2149
2150 if (before && dn->dn_bonuslen != 0)
2151 data = DN_BONUS(dn->dn_phys);
2152 else if (!before && dn->dn_bonuslen != 0) {
a3000f93
BB
2153 if (dn->dn_bonus) {
2154 db = dn->dn_bonus;
428870ff
BB
2155 mutex_enter(&db->db_mtx);
2156 data = dmu_objset_userquota_find_data(db, tx);
2157 } else {
2158 data = DN_BONUS(dn->dn_phys);
2159 }
2160 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
2161 int rf = 0;
2162
2163 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
2164 rf |= DB_RF_HAVESTRUCT;
572e2857
BB
2165 error = dmu_spill_hold_by_dnode(dn,
2166 rf | DB_RF_MUST_SUCCEED,
428870ff
BB
2167 FTAG, (dmu_buf_t **)&db);
2168 ASSERT(error == 0);
2169 mutex_enter(&db->db_mtx);
2170 data = (before) ? db->db.db_data :
2171 dmu_objset_userquota_find_data(db, tx);
2172 have_spill = B_TRUE;
2173 } else {
2174 mutex_enter(&dn->dn_mtx);
2175 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2176 mutex_exit(&dn->dn_mtx);
2177 return;
2178 }
2179
2180 if (before) {
2181 ASSERT(data);
2182 user = &dn->dn_olduid;
2183 group = &dn->dn_oldgid;
9c5167d1 2184 project = &dn->dn_oldprojid;
428870ff
BB
2185 } else if (data) {
2186 user = &dn->dn_newuid;
2187 group = &dn->dn_newgid;
9c5167d1 2188 project = &dn->dn_newprojid;
428870ff
BB
2189 }
2190
2191 /*
2192 * Must always call the callback in case the object
2193 * type has changed and that type isn't an object type to track
2194 */
2195 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
9c5167d1 2196 user, group, project);
428870ff
BB
2197
2198 /*
2199 * Preserve existing uid/gid when the callback can't determine
2200 * what the new uid/gid are and the callback returned EEXIST.
2201 * The EEXIST error tells us to just use the existing uid/gid.
2202 * If we don't know what the old values are then just assign
2203 * them to 0, since that is a new file being created.
2204 */
2205 if (!before && data == NULL && error == EEXIST) {
2206 if (flags & DN_ID_OLD_EXIST) {
2207 dn->dn_newuid = dn->dn_olduid;
2208 dn->dn_newgid = dn->dn_oldgid;
2705ebf0 2209 dn->dn_newprojid = dn->dn_oldprojid;
428870ff
BB
2210 } else {
2211 dn->dn_newuid = 0;
2212 dn->dn_newgid = 0;
9c5167d1 2213 dn->dn_newprojid = ZFS_DEFAULT_PROJID;
428870ff
BB
2214 }
2215 error = 0;
2216 }
2217
2218 if (db)
2219 mutex_exit(&db->db_mtx);
2220
2221 mutex_enter(&dn->dn_mtx);
2222 if (error == 0 && before)
2223 dn->dn_id_flags |= DN_ID_OLD_EXIST;
2224 if (error == 0 && !before)
2225 dn->dn_id_flags |= DN_ID_NEW_EXIST;
2226
2227 if (have_spill) {
2228 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
2229 } else {
2230 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2231 }
2232 mutex_exit(&dn->dn_mtx);
a3000f93 2233 if (have_spill)
428870ff
BB
2234 dmu_buf_rele((dmu_buf_t *)db, FTAG);
2235}
2236
9babb374
BB
2237boolean_t
2238dmu_objset_userspace_present(objset_t *os)
2239{
428870ff 2240 return (os->os_phys->os_flags &
9babb374
BB
2241 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
2242}
2243
1de321e6
JX
2244boolean_t
2245dmu_objset_userobjspace_present(objset_t *os)
2246{
2247 return (os->os_phys->os_flags &
2248 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE);
2249}
2250
9c5167d1
NF
2251boolean_t
2252dmu_objset_projectquota_present(objset_t *os)
2253{
2254 return (os->os_phys->os_flags &
2255 OBJSET_FLAG_PROJECTQUOTA_COMPLETE);
2256}
2257
1de321e6
JX
2258static int
2259dmu_objset_space_upgrade(objset_t *os)
9babb374
BB
2260{
2261 uint64_t obj;
2262 int err = 0;
2263
9babb374
BB
2264 /*
2265 * We simply need to mark every object dirty, so that it will be
2266 * synced out and now accounted. If this is called
2267 * concurrently, or if we already did some work before crashing,
2268 * that's fine, since we track each object's accounted state
2269 * independently.
2270 */
2271
2272 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
45d1cae3 2273 dmu_tx_t *tx;
9babb374
BB
2274 dmu_buf_t *db;
2275 int objerr;
2276
1de321e6
JX
2277 mutex_enter(&os->os_upgrade_lock);
2278 if (os->os_upgrade_exit)
2279 err = SET_ERROR(EINTR);
2280 mutex_exit(&os->os_upgrade_lock);
2281 if (err != 0)
2282 return (err);
2283
9babb374 2284 if (issig(JUSTLOOKING) && issig(FORREAL))
2e528b49 2285 return (SET_ERROR(EINTR));
9babb374
BB
2286
2287 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
13fe0198 2288 if (objerr != 0)
9babb374 2289 continue;
45d1cae3 2290 tx = dmu_tx_create(os);
9babb374
BB
2291 dmu_tx_hold_bonus(tx, obj);
2292 objerr = dmu_tx_assign(tx, TXG_WAIT);
13fe0198 2293 if (objerr != 0) {
d22323e8 2294 dmu_buf_rele(db, FTAG);
9babb374
BB
2295 dmu_tx_abort(tx);
2296 continue;
2297 }
2298 dmu_buf_will_dirty(db, tx);
2299 dmu_buf_rele(db, FTAG);
2300 dmu_tx_commit(tx);
2301 }
1de321e6
JX
2302 return (0);
2303}
2304
2305int
2306dmu_objset_userspace_upgrade(objset_t *os)
2307{
2308 int err = 0;
2309
2310 if (dmu_objset_userspace_present(os))
2311 return (0);
2312 if (dmu_objset_is_snapshot(os))
2313 return (SET_ERROR(EINVAL));
2314 if (!dmu_objset_userused_enabled(os))
2315 return (SET_ERROR(ENOTSUP));
2316
2317 err = dmu_objset_space_upgrade(os);
2318 if (err)
2319 return (err);
9babb374 2320
428870ff 2321 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
9babb374
BB
2322 txg_wait_synced(dmu_objset_pool(os), 0);
2323 return (0);
2324}
2325
1de321e6 2326static int
9c5167d1 2327dmu_objset_id_quota_upgrade_cb(objset_t *os)
1de321e6
JX
2328{
2329 int err = 0;
2330
9c5167d1
NF
2331 if (dmu_objset_userobjspace_present(os) &&
2332 dmu_objset_projectquota_present(os))
1de321e6
JX
2333 return (0);
2334 if (dmu_objset_is_snapshot(os))
2335 return (SET_ERROR(EINVAL));
2336 if (!dmu_objset_userobjused_enabled(os))
2337 return (SET_ERROR(ENOTSUP));
9c5167d1
NF
2338 if (!dmu_objset_projectquota_enabled(os) &&
2339 dmu_objset_userobjspace_present(os))
2340 return (SET_ERROR(ENOTSUP));
1de321e6
JX
2341
2342 dmu_objset_ds(os)->ds_feature_activation_needed[
2343 SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE;
9c5167d1
NF
2344 if (dmu_objset_projectquota_enabled(os))
2345 dmu_objset_ds(os)->ds_feature_activation_needed[
2346 SPA_FEATURE_PROJECT_QUOTA] = B_TRUE;
1de321e6
JX
2347
2348 err = dmu_objset_space_upgrade(os);
2349 if (err)
2350 return (err);
2351
2352 os->os_flags |= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
9c5167d1
NF
2353 if (dmu_objset_projectquota_enabled(os))
2354 os->os_flags |= OBJSET_FLAG_PROJECTQUOTA_COMPLETE;
2355
1de321e6
JX
2356 txg_wait_synced(dmu_objset_pool(os), 0);
2357 return (0);
2358}
2359
2360void
9c5167d1 2361dmu_objset_id_quota_upgrade(objset_t *os)
1de321e6 2362{
9c5167d1 2363 dmu_objset_upgrade(os, dmu_objset_id_quota_upgrade_cb);
1de321e6
JX
2364}
2365
126ae9f4
JX
2366boolean_t
2367dmu_objset_userobjspace_upgradable(objset_t *os)
2368{
2369 return (dmu_objset_type(os) == DMU_OST_ZFS &&
2370 !dmu_objset_is_snapshot(os) &&
2371 dmu_objset_userobjused_enabled(os) &&
2372 !dmu_objset_userobjspace_present(os));
2373}
2374
9c5167d1
NF
2375boolean_t
2376dmu_objset_projectquota_upgradable(objset_t *os)
2377{
2378 return (dmu_objset_type(os) == DMU_OST_ZFS &&
2379 !dmu_objset_is_snapshot(os) &&
2380 dmu_objset_projectquota_enabled(os) &&
2381 !dmu_objset_projectquota_present(os));
2382}
2383
34dc7c2f
BB
2384void
2385dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
2386 uint64_t *usedobjsp, uint64_t *availobjsp)
2387{
428870ff 2388 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
34dc7c2f
BB
2389 usedobjsp, availobjsp);
2390}
2391
2392uint64_t
2393dmu_objset_fsid_guid(objset_t *os)
2394{
428870ff 2395 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
34dc7c2f
BB
2396}
2397
2398void
2399dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
2400{
428870ff
BB
2401 stat->dds_type = os->os_phys->os_type;
2402 if (os->os_dsl_dataset)
2403 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
34dc7c2f
BB
2404}
2405
2406void
2407dmu_objset_stats(objset_t *os, nvlist_t *nv)
2408{
428870ff
BB
2409 ASSERT(os->os_dsl_dataset ||
2410 os->os_phys->os_type == DMU_OST_META);
34dc7c2f 2411
428870ff
BB
2412 if (os->os_dsl_dataset != NULL)
2413 dsl_dataset_stats(os->os_dsl_dataset, nv);
34dc7c2f
BB
2414
2415 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
428870ff 2416 os->os_phys->os_type);
9babb374
BB
2417 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
2418 dmu_objset_userspace_present(os));
34dc7c2f
BB
2419}
2420
2421int
2422dmu_objset_is_snapshot(objset_t *os)
2423{
428870ff 2424 if (os->os_dsl_dataset != NULL)
0c66c32d 2425 return (os->os_dsl_dataset->ds_is_snapshot);
34dc7c2f
BB
2426 else
2427 return (B_FALSE);
2428}
2429
2430int
2431dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
2432 boolean_t *conflict)
2433{
428870ff 2434 dsl_dataset_t *ds = os->os_dsl_dataset;
34dc7c2f
BB
2435 uint64_t ignored;
2436
d683ddbb 2437 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2e528b49 2438 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2439
2440 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
d683ddbb 2441 dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
9b7b9cd3 2442 MT_NORMALIZE, real, maxlen, conflict));
34dc7c2f
BB
2443}
2444
2445int
2446dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
2447 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
2448{
428870ff 2449 dsl_dataset_t *ds = os->os_dsl_dataset;
34dc7c2f
BB
2450 zap_cursor_t cursor;
2451 zap_attribute_t attr;
2452
13fe0198
MA
2453 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
2454
d683ddbb 2455 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2e528b49 2456 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2457
2458 zap_cursor_init_serialized(&cursor,
2459 ds->ds_dir->dd_pool->dp_meta_objset,
d683ddbb 2460 dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
34dc7c2f
BB
2461
2462 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2463 zap_cursor_fini(&cursor);
2e528b49 2464 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2465 }
2466
2467 if (strlen(attr.za_name) + 1 > namelen) {
2468 zap_cursor_fini(&cursor);
2e528b49 2469 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f
BB
2470 }
2471
2472 (void) strcpy(name, attr.za_name);
2473 if (idp)
2474 *idp = attr.za_first_integer;
2475 if (case_conflict)
2476 *case_conflict = attr.za_normalization_conflict;
2477 zap_cursor_advance(&cursor);
2478 *offp = zap_cursor_serialize(&cursor);
2479 zap_cursor_fini(&cursor);
2480
2481 return (0);
2482}
2483
ebe7e575 2484int
6772fb67 2485dmu_snapshot_lookup(objset_t *os, const char *name, uint64_t *value)
ebe7e575 2486{
d1d7e268 2487 return (dsl_dataset_snap_lookup(os->os_dsl_dataset, name, value));
ebe7e575
BB
2488}
2489
34dc7c2f
BB
2490int
2491dmu_dir_list_next(objset_t *os, int namelen, char *name,
2492 uint64_t *idp, uint64_t *offp)
2493{
428870ff 2494 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
34dc7c2f
BB
2495 zap_cursor_t cursor;
2496 zap_attribute_t attr;
2497
2498 /* there is no next dir on a snapshot! */
428870ff 2499 if (os->os_dsl_dataset->ds_object !=
d683ddbb 2500 dsl_dir_phys(dd)->dd_head_dataset_obj)
2e528b49 2501 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2502
2503 zap_cursor_init_serialized(&cursor,
2504 dd->dd_pool->dp_meta_objset,
d683ddbb 2505 dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
34dc7c2f
BB
2506
2507 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2508 zap_cursor_fini(&cursor);
2e528b49 2509 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2510 }
2511
2512 if (strlen(attr.za_name) + 1 > namelen) {
2513 zap_cursor_fini(&cursor);
2e528b49 2514 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f
BB
2515 }
2516
2517 (void) strcpy(name, attr.za_name);
2518 if (idp)
2519 *idp = attr.za_first_integer;
2520 zap_cursor_advance(&cursor);
2521 *offp = zap_cursor_serialize(&cursor);
2522 zap_cursor_fini(&cursor);
2523
2524 return (0);
2525}
2526
9c43027b
AJ
2527typedef struct dmu_objset_find_ctx {
2528 taskq_t *dc_tq;
2529 dsl_pool_t *dc_dp;
2530 uint64_t dc_ddobj;
1149ba64 2531 char *dc_ddname; /* last component of ddobj's name */
9c43027b
AJ
2532 int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
2533 void *dc_arg;
2534 int dc_flags;
2535 kmutex_t *dc_error_lock;
2536 int *dc_error;
2537} dmu_objset_find_ctx_t;
2538
2539static void
2540dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
b128c09f 2541{
9c43027b 2542 dsl_pool_t *dp = dcp->dc_dp;
13fe0198
MA
2543 dsl_dir_t *dd;
2544 dsl_dataset_t *ds;
2545 zap_cursor_t zc;
2546 zap_attribute_t *attr;
2547 uint64_t thisobj;
9c43027b 2548 int err = 0;
13fe0198 2549
9c43027b
AJ
2550 /* don't process if there already was an error */
2551 if (*dcp->dc_error != 0)
2552 goto out;
13fe0198 2553
1149ba64
GM
2554 /*
2555 * Note: passing the name (dc_ddname) here is optional, but it
2556 * improves performance because we don't need to call
2557 * zap_value_search() to determine the name.
2558 */
2559 err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
13fe0198 2560 if (err != 0)
9c43027b 2561 goto out;
13fe0198
MA
2562
2563 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2564 if (dd->dd_myname[0] == '$') {
2565 dsl_dir_rele(dd, FTAG);
9c43027b 2566 goto out;
13fe0198
MA
2567 }
2568
d683ddbb 2569 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
79c76d5b 2570 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
13fe0198
MA
2571
2572 /*
2573 * Iterate over all children.
2574 */
9c43027b 2575 if (dcp->dc_flags & DS_FIND_CHILDREN) {
13fe0198 2576 for (zap_cursor_init(&zc, dp->dp_meta_objset,
d683ddbb 2577 dsl_dir_phys(dd)->dd_child_dir_zapobj);
13fe0198
MA
2578 zap_cursor_retrieve(&zc, attr) == 0;
2579 (void) zap_cursor_advance(&zc)) {
2580 ASSERT3U(attr->za_integer_length, ==,
2581 sizeof (uint64_t));
2582 ASSERT3U(attr->za_num_integers, ==, 1);
2583
1c27024e 2584 dmu_objset_find_ctx_t *child_dcp =
1149ba64 2585 kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
9c43027b
AJ
2586 *child_dcp = *dcp;
2587 child_dcp->dc_ddobj = attr->za_first_integer;
1149ba64 2588 child_dcp->dc_ddname = spa_strdup(attr->za_name);
9c43027b
AJ
2589 if (dcp->dc_tq != NULL)
2590 (void) taskq_dispatch(dcp->dc_tq,
2591 dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
2592 else
2593 dmu_objset_find_dp_impl(child_dcp);
13fe0198
MA
2594 }
2595 zap_cursor_fini(&zc);
13fe0198
MA
2596 }
2597
2598 /*
2599 * Iterate over all snapshots.
2600 */
9c43027b 2601 if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
13fe0198
MA
2602 dsl_dataset_t *ds;
2603 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2604
2605 if (err == 0) {
d683ddbb
JG
2606 uint64_t snapobj;
2607
2608 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
13fe0198
MA
2609 dsl_dataset_rele(ds, FTAG);
2610
2611 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2612 zap_cursor_retrieve(&zc, attr) == 0;
2613 (void) zap_cursor_advance(&zc)) {
2614 ASSERT3U(attr->za_integer_length, ==,
2615 sizeof (uint64_t));
2616 ASSERT3U(attr->za_num_integers, ==, 1);
2617
2618 err = dsl_dataset_hold_obj(dp,
2619 attr->za_first_integer, FTAG, &ds);
2620 if (err != 0)
2621 break;
9c43027b 2622 err = dcp->dc_func(dp, ds, dcp->dc_arg);
13fe0198
MA
2623 dsl_dataset_rele(ds, FTAG);
2624 if (err != 0)
2625 break;
2626 }
2627 zap_cursor_fini(&zc);
2628 }
2629 }
2630
13fe0198
MA
2631 kmem_free(attr, sizeof (zap_attribute_t));
2632
1149ba64
GM
2633 if (err != 0) {
2634 dsl_dir_rele(dd, FTAG);
9c43027b 2635 goto out;
1149ba64 2636 }
13fe0198
MA
2637
2638 /*
2639 * Apply to self.
2640 */
2641 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1149ba64
GM
2642
2643 /*
2644 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2645 * that the dir will remain cached, and we won't have to re-instantiate
2646 * it (which could be expensive due to finding its name via
2647 * zap_value_search()).
2648 */
2649 dsl_dir_rele(dd, FTAG);
13fe0198 2650 if (err != 0)
9c43027b
AJ
2651 goto out;
2652 err = dcp->dc_func(dp, ds, dcp->dc_arg);
13fe0198 2653 dsl_dataset_rele(ds, FTAG);
9c43027b
AJ
2654
2655out:
2656 if (err != 0) {
2657 mutex_enter(dcp->dc_error_lock);
2658 /* only keep first error */
2659 if (*dcp->dc_error == 0)
2660 *dcp->dc_error = err;
2661 mutex_exit(dcp->dc_error_lock);
2662 }
2663
1149ba64
GM
2664 if (dcp->dc_ddname != NULL)
2665 spa_strfree(dcp->dc_ddname);
9c43027b
AJ
2666 kmem_free(dcp, sizeof (*dcp));
2667}
2668
2669static void
2670dmu_objset_find_dp_cb(void *arg)
2671{
2672 dmu_objset_find_ctx_t *dcp = arg;
2673 dsl_pool_t *dp = dcp->dc_dp;
2674
5e8cd5d1
AJ
2675 /*
2676 * We need to get a pool_config_lock here, as there are several
2677 * asssert(pool_config_held) down the stack. Getting a lock via
2678 * dsl_pool_config_enter is risky, as it might be stalled by a
2679 * pending writer. This would deadlock, as the write lock can
2680 * only be granted when our parent thread gives up the lock.
2681 * The _prio interface gives us priority over a pending writer.
2682 */
2683 dsl_pool_config_enter_prio(dp, FTAG);
9c43027b
AJ
2684
2685 dmu_objset_find_dp_impl(dcp);
2686
2687 dsl_pool_config_exit(dp, FTAG);
2688}
2689
2690/*
2691 * Find objsets under and including ddobj, call func(ds) on each.
2692 * The order for the enumeration is completely undefined.
2693 * func is called with dsl_pool_config held.
2694 */
2695int
2696dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
2697 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
2698{
2699 int error = 0;
2700 taskq_t *tq = NULL;
2701 int ntasks;
2702 dmu_objset_find_ctx_t *dcp;
2703 kmutex_t err_lock;
2704
2705 mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
2706 dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
2707 dcp->dc_tq = NULL;
2708 dcp->dc_dp = dp;
2709 dcp->dc_ddobj = ddobj;
1149ba64 2710 dcp->dc_ddname = NULL;
9c43027b
AJ
2711 dcp->dc_func = func;
2712 dcp->dc_arg = arg;
2713 dcp->dc_flags = flags;
2714 dcp->dc_error_lock = &err_lock;
2715 dcp->dc_error = &error;
2716
2717 if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
2718 /*
2719 * In case a write lock is held we can't make use of
2720 * parallelism, as down the stack of the worker threads
2721 * the lock is asserted via dsl_pool_config_held.
2722 * In case of a read lock this is solved by getting a read
2723 * lock in each worker thread, which isn't possible in case
2724 * of a writer lock. So we fall back to the synchronous path
2725 * here.
2726 * In the future it might be possible to get some magic into
2727 * dsl_pool_config_held in a way that it returns true for
2728 * the worker threads so that a single lock held from this
2729 * thread suffices. For now, stay single threaded.
2730 */
2731 dmu_objset_find_dp_impl(dcp);
e5676636 2732 mutex_destroy(&err_lock);
9c43027b
AJ
2733
2734 return (error);
2735 }
2736
2737 ntasks = dmu_find_threads;
2738 if (ntasks == 0)
2739 ntasks = vdev_count_leaves(dp->dp_spa) * 4;
1229323d 2740 tq = taskq_create("dmu_objset_find", ntasks, maxclsyspri, ntasks,
9c43027b
AJ
2741 INT_MAX, 0);
2742 if (tq == NULL) {
2743 kmem_free(dcp, sizeof (*dcp));
e5676636
BB
2744 mutex_destroy(&err_lock);
2745
9c43027b
AJ
2746 return (SET_ERROR(ENOMEM));
2747 }
2748 dcp->dc_tq = tq;
2749
2750 /* dcp will be freed by task */
2751 (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
2752
2753 /*
2754 * PORTING: this code relies on the property of taskq_wait to wait
2755 * until no more tasks are queued and no more tasks are active. As
2756 * we always queue new tasks from within other tasks, task_wait
2757 * reliably waits for the full recursion to finish, even though we
2758 * enqueue new tasks after taskq_wait has been called.
2759 * On platforms other than illumos, taskq_wait may not have this
2760 * property.
2761 */
2762 taskq_wait(tq);
2763 taskq_destroy(tq);
2764 mutex_destroy(&err_lock);
2765
2766 return (error);
b128c09f
BB
2767}
2768
2769/*
13fe0198
MA
2770 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2771 * The dp_config_rwlock must not be held when this is called, and it
2772 * will not be held when the callback is called.
2773 * Therefore this function should only be used when the pool is not changing
2774 * (e.g. in syncing context), or the callback can deal with the possible races.
b128c09f 2775 */
13fe0198
MA
2776static int
2777dmu_objset_find_impl(spa_t *spa, const char *name,
2778 int func(const char *, void *), void *arg, int flags)
34dc7c2f
BB
2779{
2780 dsl_dir_t *dd;
13fe0198 2781 dsl_pool_t *dp = spa_get_dsl(spa);
b128c09f 2782 dsl_dataset_t *ds;
34dc7c2f
BB
2783 zap_cursor_t zc;
2784 zap_attribute_t *attr;
2785 char *child;
b128c09f
BB
2786 uint64_t thisobj;
2787 int err;
34dc7c2f 2788
13fe0198
MA
2789 dsl_pool_config_enter(dp, FTAG);
2790
2791 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
2792 if (err != 0) {
2793 dsl_pool_config_exit(dp, FTAG);
34dc7c2f 2794 return (err);
13fe0198 2795 }
34dc7c2f 2796
b128c09f
BB
2797 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2798 if (dd->dd_myname[0] == '$') {
13fe0198
MA
2799 dsl_dir_rele(dd, FTAG);
2800 dsl_pool_config_exit(dp, FTAG);
b128c09f
BB
2801 return (0);
2802 }
2803
d683ddbb 2804 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
79c76d5b 2805 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
34dc7c2f
BB
2806
2807 /*
2808 * Iterate over all children.
2809 */
2810 if (flags & DS_FIND_CHILDREN) {
b128c09f 2811 for (zap_cursor_init(&zc, dp->dp_meta_objset,
d683ddbb 2812 dsl_dir_phys(dd)->dd_child_dir_zapobj);
34dc7c2f
BB
2813 zap_cursor_retrieve(&zc, attr) == 0;
2814 (void) zap_cursor_advance(&zc)) {
13fe0198
MA
2815 ASSERT3U(attr->za_integer_length, ==,
2816 sizeof (uint64_t));
2817 ASSERT3U(attr->za_num_integers, ==, 1);
34dc7c2f 2818
428870ff 2819 child = kmem_asprintf("%s/%s", name, attr->za_name);
13fe0198
MA
2820 dsl_pool_config_exit(dp, FTAG);
2821 err = dmu_objset_find_impl(spa, child,
2822 func, arg, flags);
2823 dsl_pool_config_enter(dp, FTAG);
428870ff 2824 strfree(child);
13fe0198 2825 if (err != 0)
34dc7c2f
BB
2826 break;
2827 }
2828 zap_cursor_fini(&zc);
2829
13fe0198
MA
2830 if (err != 0) {
2831 dsl_dir_rele(dd, FTAG);
2832 dsl_pool_config_exit(dp, FTAG);
34dc7c2f
BB
2833 kmem_free(attr, sizeof (zap_attribute_t));
2834 return (err);
2835 }
2836 }
2837
2838 /*
2839 * Iterate over all snapshots.
2840 */
b128c09f 2841 if (flags & DS_FIND_SNAPSHOTS) {
b128c09f 2842 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
b128c09f
BB
2843
2844 if (err == 0) {
d683ddbb
JG
2845 uint64_t snapobj;
2846
2847 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
b128c09f
BB
2848 dsl_dataset_rele(ds, FTAG);
2849
2850 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2851 zap_cursor_retrieve(&zc, attr) == 0;
2852 (void) zap_cursor_advance(&zc)) {
13fe0198 2853 ASSERT3U(attr->za_integer_length, ==,
b128c09f 2854 sizeof (uint64_t));
13fe0198 2855 ASSERT3U(attr->za_num_integers, ==, 1);
b128c09f 2856
428870ff
BB
2857 child = kmem_asprintf("%s@%s",
2858 name, attr->za_name);
13fe0198
MA
2859 dsl_pool_config_exit(dp, FTAG);
2860 err = func(child, arg);
2861 dsl_pool_config_enter(dp, FTAG);
428870ff 2862 strfree(child);
13fe0198 2863 if (err != 0)
b128c09f
BB
2864 break;
2865 }
2866 zap_cursor_fini(&zc);
34dc7c2f 2867 }
34dc7c2f
BB
2868 }
2869
13fe0198 2870 dsl_dir_rele(dd, FTAG);
34dc7c2f 2871 kmem_free(attr, sizeof (zap_attribute_t));
13fe0198 2872 dsl_pool_config_exit(dp, FTAG);
34dc7c2f 2873
13fe0198 2874 if (err != 0)
34dc7c2f
BB
2875 return (err);
2876
13fe0198
MA
2877 /* Apply to self. */
2878 return (func(name, arg));
34dc7c2f
BB
2879}
2880
13fe0198
MA
2881/*
2882 * See comment above dmu_objset_find_impl().
2883 */
d164b209 2884int
13fe0198
MA
2885dmu_objset_find(char *name, int func(const char *, void *), void *arg,
2886 int flags)
d164b209 2887{
13fe0198
MA
2888 spa_t *spa;
2889 int error;
d164b209 2890
13fe0198
MA
2891 error = spa_open(name, &spa, FTAG);
2892 if (error != 0)
2893 return (error);
2894 error = dmu_objset_find_impl(spa, name, func, arg, flags);
2895 spa_close(spa, FTAG);
2896 return (error);
d164b209
BB
2897}
2898
ae76f45c
TC
2899boolean_t
2900dmu_objset_incompatible_encryption_version(objset_t *os)
2901{
2902 return (dsl_dir_incompatible_encryption_version(
2903 os->os_dsl_dataset->ds_dir));
2904}
2905
34dc7c2f
BB
2906void
2907dmu_objset_set_user(objset_t *os, void *user_ptr)
2908{
428870ff
BB
2909 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2910 os->os_user_ptr = user_ptr;
34dc7c2f
BB
2911}
2912
2913void *
2914dmu_objset_get_user(objset_t *os)
2915{
428870ff
BB
2916 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2917 return (os->os_user_ptr);
34dc7c2f 2918}
c28b2279 2919
13fe0198
MA
2920/*
2921 * Determine name of filesystem, given name of snapshot.
eca7b760 2922 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
13fe0198
MA
2923 */
2924int
2925dmu_fsname(const char *snapname, char *buf)
2926{
2927 char *atp = strchr(snapname, '@');
2928 if (atp == NULL)
2e528b49 2929 return (SET_ERROR(EINVAL));
eca7b760 2930 if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2e528b49 2931 return (SET_ERROR(ENAMETOOLONG));
13fe0198
MA
2932 (void) strlcpy(buf, snapname, atp - snapname + 1);
2933 return (0);
2934}
2935
3ec3bc21
BB
2936/*
2937 * Call when we think we're going to write/free space in open context to track
2938 * the amount of dirty data in the open txg, which is also the amount
2939 * of memory that can not be evicted until this txg syncs.
2940 */
2941void
2942dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
2943{
2944 dsl_dataset_t *ds = os->os_dsl_dataset;
2945 int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
2946
2947 if (ds != NULL) {
2948 dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
2949 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
2950 }
2951}
2952
c28b2279 2953#if defined(_KERNEL) && defined(HAVE_SPL)
f0fd83be 2954EXPORT_SYMBOL(dmu_objset_zil);
c28b2279 2955EXPORT_SYMBOL(dmu_objset_pool);
f0fd83be
BB
2956EXPORT_SYMBOL(dmu_objset_ds);
2957EXPORT_SYMBOL(dmu_objset_type);
c28b2279
BB
2958EXPORT_SYMBOL(dmu_objset_name);
2959EXPORT_SYMBOL(dmu_objset_hold);
b5256303 2960EXPORT_SYMBOL(dmu_objset_hold_flags);
c28b2279
BB
2961EXPORT_SYMBOL(dmu_objset_own);
2962EXPORT_SYMBOL(dmu_objset_rele);
b5256303 2963EXPORT_SYMBOL(dmu_objset_rele_flags);
c28b2279
BB
2964EXPORT_SYMBOL(dmu_objset_disown);
2965EXPORT_SYMBOL(dmu_objset_from_ds);
2966EXPORT_SYMBOL(dmu_objset_create);
2967EXPORT_SYMBOL(dmu_objset_clone);
c28b2279
BB
2968EXPORT_SYMBOL(dmu_objset_stats);
2969EXPORT_SYMBOL(dmu_objset_fast_stat);
54a179e7 2970EXPORT_SYMBOL(dmu_objset_spa);
c28b2279
BB
2971EXPORT_SYMBOL(dmu_objset_space);
2972EXPORT_SYMBOL(dmu_objset_fsid_guid);
2973EXPORT_SYMBOL(dmu_objset_find);
c28b2279
BB
2974EXPORT_SYMBOL(dmu_objset_byteswap);
2975EXPORT_SYMBOL(dmu_objset_evict_dbufs);
2976EXPORT_SYMBOL(dmu_objset_snap_cmtime);
50c957f7 2977EXPORT_SYMBOL(dmu_objset_dnodesize);
c28b2279
BB
2978
2979EXPORT_SYMBOL(dmu_objset_sync);
2980EXPORT_SYMBOL(dmu_objset_is_dirty);
b5256303 2981EXPORT_SYMBOL(dmu_objset_create_impl_dnstats);
c28b2279
BB
2982EXPORT_SYMBOL(dmu_objset_create_impl);
2983EXPORT_SYMBOL(dmu_objset_open_impl);
2984EXPORT_SYMBOL(dmu_objset_evict);
2985EXPORT_SYMBOL(dmu_objset_register_type);
2986EXPORT_SYMBOL(dmu_objset_do_userquota_updates);
2987EXPORT_SYMBOL(dmu_objset_userquota_get_ids);
2988EXPORT_SYMBOL(dmu_objset_userused_enabled);
2989EXPORT_SYMBOL(dmu_objset_userspace_upgrade);
2990EXPORT_SYMBOL(dmu_objset_userspace_present);
1de321e6 2991EXPORT_SYMBOL(dmu_objset_userobjused_enabled);
126ae9f4 2992EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable);
1de321e6 2993EXPORT_SYMBOL(dmu_objset_userobjspace_present);
9c5167d1
NF
2994EXPORT_SYMBOL(dmu_objset_projectquota_enabled);
2995EXPORT_SYMBOL(dmu_objset_projectquota_present);
2996EXPORT_SYMBOL(dmu_objset_projectquota_upgradable);
2997EXPORT_SYMBOL(dmu_objset_id_quota_upgrade);
c28b2279 2998#endif