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