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