]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dmu_objset.c
deadlock between mm_sem and tx assign in zfs_write() and page fault
[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>
03916905 61#include <sys/dmu_recv.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);
52ce99dd 1160 spa_t *spa = dp->dp_spa;
13fe0198
MA
1161 dsl_dir_t *pdd;
1162 const char *tail;
6f1ffb06 1163 dsl_dataset_t *ds;
13fe0198 1164 uint64_t obj;
6f1ffb06 1165 blkptr_t *bp;
13fe0198 1166 objset_t *os;
b5256303 1167 zio_t *rzio;
34dc7c2f 1168
13fe0198 1169 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
34dc7c2f 1170
13fe0198 1171 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
b5256303 1172 doca->doca_cred, doca->doca_dcp, tx);
34dc7c2f 1173
b5256303
TC
1174 VERIFY0(dsl_dataset_hold_obj_flags(pdd->dd_pool, obj,
1175 DS_HOLD_FLAG_DECRYPT, FTAG, &ds));
cc9bb3e5 1176 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
6f1ffb06 1177 bp = dsl_dataset_get_blkptr(ds);
52ce99dd 1178 os = dmu_objset_create_impl(spa, 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
52ce99dd 1202 rzio = zio_root(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);
52ce99dd
TC
1213 if (txg_list_member(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1214 ASSERT3P(ds->ds_key_mapping, !=, NULL);
1215 key_mapping_rele(spa, ds->ds_key_mapping, ds);
1216 }
b5256303 1217
52ce99dd 1218 rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
4807c0ba
TC
1219 tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds,
1220 tx->tx_txg);
b5256303 1221 if (tmpds != NULL) {
b5256303
TC
1222 dmu_buf_rele(ds->ds_dbuf, ds);
1223 dsl_dataset_sync(ds, rzio, tx);
1224 }
1225 VERIFY0(zio_wait(rzio));
1226
52ce99dd
TC
1227 if (need_sync_done) {
1228 ASSERT3P(ds->ds_key_mapping, !=, NULL);
1229 key_mapping_rele(spa, ds->ds_key_mapping, ds);
b5256303 1230 dsl_dataset_sync_done(ds, tx);
52ce99dd 1231 }
4807c0ba
TC
1232
1233 mutex_enter(&ds->ds_lock);
1234 ds->ds_owner = NULL;
1235 mutex_exit(&ds->ds_lock);
b5256303
TC
1236 }
1237
13fe0198 1238 spa_history_log_internal_ds(ds, "create", tx, "");
52ce99dd 1239 zvol_create_minors(spa, doca->doca_name, B_TRUE);
a0bd735a 1240
b5256303 1241 dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
13fe0198 1242 dsl_dir_rele(pdd, FTAG);
34dc7c2f
BB
1243}
1244
1245int
428870ff 1246dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
b5256303 1247 dsl_crypto_params_t *dcp, dmu_objset_create_sync_func_t func, void *arg)
34dc7c2f 1248{
13fe0198 1249 dmu_objset_create_arg_t doca;
b5256303 1250 dsl_crypto_params_t tmp_dcp = { 0 };
34dc7c2f 1251
13fe0198
MA
1252 doca.doca_name = name;
1253 doca.doca_cred = CRED();
1254 doca.doca_flags = flags;
1255 doca.doca_userfunc = func;
1256 doca.doca_userarg = arg;
1257 doca.doca_type = type;
34dc7c2f 1258
b5256303
TC
1259 /*
1260 * Some callers (mostly for testing) do not provide a dcp on their
1261 * own but various code inside the sync task will require it to be
1262 * allocated. Rather than adding NULL checks throughout this code
1263 * or adding dummy dcp's to all of the callers we simply create a
1264 * dummy one here and use that. This zero dcp will have the same
85ce3f4f 1265 * effect as asking for inheritance of all encryption params.
b5256303
TC
1266 */
1267 doca.doca_dcp = (dcp != NULL) ? dcp : &tmp_dcp;
1268
13fe0198 1269 return (dsl_sync_task(name,
3d45fdd6 1270 dmu_objset_create_check, dmu_objset_create_sync, &doca,
b5256303 1271 6, ZFS_SPACE_CHECK_NORMAL));
34dc7c2f
BB
1272}
1273
13fe0198
MA
1274typedef struct dmu_objset_clone_arg {
1275 const char *doca_clone;
1276 const char *doca_origin;
1277 cred_t *doca_cred;
1278} dmu_objset_clone_arg_t;
1279
1280/*ARGSUSED*/
1281static int
1282dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1283{
13fe0198 1284 dmu_objset_clone_arg_t *doca = arg;
428870ff
BB
1285 dsl_dir_t *pdd;
1286 const char *tail;
13fe0198
MA
1287 int error;
1288 dsl_dataset_t *origin;
1289 dsl_pool_t *dp = dmu_tx_pool(tx);
34dc7c2f 1290
13fe0198 1291 if (strchr(doca->doca_clone, '@') != NULL)
2e528b49 1292 return (SET_ERROR(EINVAL));
13fe0198 1293
eca7b760
IK
1294 if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
1295 return (SET_ERROR(ENAMETOOLONG));
1296
13fe0198
MA
1297 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
1298 if (error != 0)
1299 return (error);
428870ff 1300 if (tail == NULL) {
13fe0198 1301 dsl_dir_rele(pdd, FTAG);
2e528b49 1302 return (SET_ERROR(EEXIST));
34dc7c2f 1303 }
1cddb8c9 1304
788eb90c
JJ
1305 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1306 doca->doca_cred);
1307 if (error != 0) {
1308 dsl_dir_rele(pdd, FTAG);
1309 return (SET_ERROR(EDQUOT));
1310 }
428870ff 1311
13fe0198 1312 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
b5256303
TC
1313 if (error != 0) {
1314 dsl_dir_rele(pdd, FTAG);
572e2857 1315 return (error);
b5256303 1316 }
572e2857 1317
13fe0198 1318 /* You can only clone snapshots, not the head datasets. */
0c66c32d 1319 if (!origin->ds_is_snapshot) {
13fe0198 1320 dsl_dataset_rele(origin, FTAG);
b5256303 1321 dsl_dir_rele(pdd, FTAG);
2e528b49 1322 return (SET_ERROR(EINVAL));
428870ff 1323 }
b5256303
TC
1324
1325 error = dmu_objset_clone_crypt_check(pdd, origin->ds_dir);
1326 if (error != 0) {
1327 dsl_dataset_rele(origin, FTAG);
1328 dsl_dir_rele(pdd, FTAG);
1329 return (error);
1330 }
1331
13fe0198 1332 dsl_dataset_rele(origin, FTAG);
b5256303 1333 dsl_dir_rele(pdd, FTAG);
572e2857 1334
13fe0198 1335 return (0);
9babb374 1336}
34dc7c2f 1337
13fe0198
MA
1338static void
1339dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 1340{
13fe0198
MA
1341 dmu_objset_clone_arg_t *doca = arg;
1342 dsl_pool_t *dp = dmu_tx_pool(tx);
1343 dsl_dir_t *pdd;
1344 const char *tail;
1345 dsl_dataset_t *origin, *ds;
1346 uint64_t obj;
eca7b760 1347 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
6f1ffb06 1348
13fe0198
MA
1349 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
1350 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
6f1ffb06 1351
13fe0198 1352 obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
b5256303 1353 doca->doca_cred, NULL, tx);
34dc7c2f 1354
13fe0198
MA
1355 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
1356 dsl_dataset_name(origin, namebuf);
1357 spa_history_log_internal_ds(ds, "clone", tx,
1358 "origin=%s (%llu)", namebuf, origin->ds_object);
a0bd735a 1359 zvol_create_minors(dp->dp_spa, doca->doca_clone, B_TRUE);
13fe0198
MA
1360 dsl_dataset_rele(ds, FTAG);
1361 dsl_dataset_rele(origin, FTAG);
1362 dsl_dir_rele(pdd, FTAG);
34dc7c2f
BB
1363}
1364
1365int
13fe0198 1366dmu_objset_clone(const char *clone, const char *origin)
34dc7c2f 1367{
13fe0198 1368 dmu_objset_clone_arg_t doca;
34dc7c2f 1369
13fe0198
MA
1370 doca.doca_clone = clone;
1371 doca.doca_origin = origin;
1372 doca.doca_cred = CRED();
572e2857 1373
13fe0198 1374 return (dsl_sync_task(clone,
3d45fdd6 1375 dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
b5256303 1376 6, ZFS_SPACE_CHECK_NORMAL));
6f1ffb06
MA
1377}
1378
a1d477c2
MA
1379static int
1380dmu_objset_remap_indirects_impl(objset_t *os, uint64_t last_removed_txg)
1381{
1382 int error = 0;
1383 uint64_t object = 0;
1384 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1385 error = dmu_object_remap_indirects(os, object,
1386 last_removed_txg);
1387 /*
1388 * If the ZPL removed the object before we managed to dnode_hold
1389 * it, we would get an ENOENT. If the ZPL declares its intent
1390 * to remove the object (dnode_free) before we manage to
1391 * dnode_hold it, we would get an EEXIST. In either case, we
1392 * want to continue remapping the other objects in the objset;
1393 * in all other cases, we want to break early.
1394 */
1395 if (error != 0 && error != ENOENT && error != EEXIST) {
1396 break;
1397 }
1398 }
1399 if (error == ESRCH) {
1400 error = 0;
1401 }
1402 return (error);
1403}
1404
1405int
1406dmu_objset_remap_indirects(const char *fsname)
1407{
1408 int error = 0;
1409 objset_t *os = NULL;
1410 uint64_t last_removed_txg;
1411 uint64_t remap_start_txg;
1412 dsl_dir_t *dd;
1413
1414 error = dmu_objset_hold(fsname, FTAG, &os);
1415 if (error != 0) {
1416 return (error);
1417 }
1418 dd = dmu_objset_ds(os)->ds_dir;
1419
1420 if (!spa_feature_is_enabled(dmu_objset_spa(os),
1421 SPA_FEATURE_OBSOLETE_COUNTS)) {
1422 dmu_objset_rele(os, FTAG);
1423 return (SET_ERROR(ENOTSUP));
1424 }
1425
1426 if (dsl_dataset_is_snapshot(dmu_objset_ds(os))) {
1427 dmu_objset_rele(os, FTAG);
1428 return (SET_ERROR(EINVAL));
1429 }
1430
1431 /*
1432 * If there has not been a removal, we're done.
1433 */
1434 last_removed_txg = spa_get_last_removal_txg(dmu_objset_spa(os));
1435 if (last_removed_txg == -1ULL) {
1436 dmu_objset_rele(os, FTAG);
1437 return (0);
1438 }
1439
1440 /*
1441 * If we have remapped since the last removal, we're done.
1442 */
1443 if (dsl_dir_is_zapified(dd)) {
1444 uint64_t last_remap_txg;
1445 if (zap_lookup(spa_meta_objset(dmu_objset_spa(os)),
1446 dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
1447 sizeof (last_remap_txg), 1, &last_remap_txg) == 0 &&
1448 last_remap_txg > last_removed_txg) {
1449 dmu_objset_rele(os, FTAG);
1450 return (0);
1451 }
1452 }
1453
1454 dsl_dataset_long_hold(dmu_objset_ds(os), FTAG);
1455 dsl_pool_rele(dmu_objset_pool(os), FTAG);
1456
1457 remap_start_txg = spa_last_synced_txg(dmu_objset_spa(os));
1458 error = dmu_objset_remap_indirects_impl(os, last_removed_txg);
1459 if (error == 0) {
1460 /*
1461 * We update the last_remap_txg to be the start txg so that
1462 * we can guarantee that every block older than last_remap_txg
1463 * that can be remapped has been remapped.
1464 */
1465 error = dsl_dir_update_last_remap_txg(dd, remap_start_txg);
1466 }
1467
1468 dsl_dataset_long_rele(dmu_objset_ds(os), FTAG);
1469 dsl_dataset_rele(dmu_objset_ds(os), FTAG);
1470
1471 return (error);
1472}
1473
6f1ffb06
MA
1474int
1475dmu_objset_snapshot_one(const char *fsname, const char *snapname)
1476{
1477 int err;
1478 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1479 nvlist_t *snaps = fnvlist_alloc();
1480
1481 fnvlist_add_boolean(snaps, longsnap);
6f1ffb06 1482 strfree(longsnap);
13fe0198
MA
1483 err = dsl_dataset_snapshot(snaps, NULL, NULL);
1484 fnvlist_free(snaps);
6f1ffb06
MA
1485 return (err);
1486}
1487
1de321e6
JX
1488static void
1489dmu_objset_upgrade_task_cb(void *data)
1490{
1491 objset_t *os = data;
1492
1493 mutex_enter(&os->os_upgrade_lock);
1494 os->os_upgrade_status = EINTR;
1495 if (!os->os_upgrade_exit) {
1496 mutex_exit(&os->os_upgrade_lock);
1497
1498 os->os_upgrade_status = os->os_upgrade_cb(os);
1499 mutex_enter(&os->os_upgrade_lock);
1500 }
1501 os->os_upgrade_exit = B_TRUE;
1502 os->os_upgrade_id = 0;
1503 mutex_exit(&os->os_upgrade_lock);
c0daec32 1504 dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1de321e6
JX
1505}
1506
1507static void
1508dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb)
1509{
1510 if (os->os_upgrade_id != 0)
1511 return;
1512
c0daec32
AB
1513 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1514 dsl_dataset_long_hold(dmu_objset_ds(os), upgrade_tag);
1515
1de321e6
JX
1516 mutex_enter(&os->os_upgrade_lock);
1517 if (os->os_upgrade_id == 0 && os->os_upgrade_status == 0) {
1518 os->os_upgrade_exit = B_FALSE;
1519 os->os_upgrade_cb = cb;
1520 os->os_upgrade_id = taskq_dispatch(
1521 os->os_spa->spa_upgrade_taskq,
1522 dmu_objset_upgrade_task_cb, os, TQ_SLEEP);
c0daec32
AB
1523 if (os->os_upgrade_id == TASKQID_INVALID) {
1524 dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1de321e6 1525 os->os_upgrade_status = ENOMEM;
c0daec32 1526 }
1de321e6
JX
1527 }
1528 mutex_exit(&os->os_upgrade_lock);
1529}
1530
1531static void
1532dmu_objset_upgrade_stop(objset_t *os)
1533{
1534 mutex_enter(&os->os_upgrade_lock);
1535 os->os_upgrade_exit = B_TRUE;
1536 if (os->os_upgrade_id != 0) {
1537 taskqid_t id = os->os_upgrade_id;
1538
1539 os->os_upgrade_id = 0;
1540 mutex_exit(&os->os_upgrade_lock);
1541
c0daec32
AB
1542 if ((taskq_cancel_id(os->os_spa->spa_upgrade_taskq, id)) == 0) {
1543 dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1544 }
4807c0ba 1545 txg_wait_synced(os->os_spa->spa_dsl_pool, 0);
1de321e6
JX
1546 } else {
1547 mutex_exit(&os->os_upgrade_lock);
1548 }
1549}
1550
34dc7c2f 1551static void
64fc7762 1552dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx)
34dc7c2f
BB
1553{
1554 dnode_t *dn;
1555
64fc7762 1556 while ((dn = multilist_sublist_head(list)) != NULL) {
34dc7c2f
BB
1557 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1558 ASSERT(dn->dn_dbuf->db_data_pending);
1559 /*
9babb374
BB
1560 * Initialize dn_zio outside dnode_sync() because the
1561 * meta-dnode needs to set it ouside dnode_sync().
34dc7c2f
BB
1562 */
1563 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1564 ASSERT(dn->dn_zio);
1565
1566 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
64fc7762 1567 multilist_sublist_remove(list, dn);
9babb374 1568
edc1e713
TC
1569 /*
1570 * If we are not doing useraccounting (os_synced_dnodes == NULL)
1571 * we are done with this dnode for this txg. Unset dn_dirty_txg
1572 * if later txgs aren't dirtying it so that future holders do
1573 * not get a stale value. Otherwise, we will do this in
1574 * userquota_updates_task() when processing has completely
1575 * finished for this txg.
1576 */
64fc7762
MA
1577 multilist_t *newlist = dn->dn_objset->os_synced_dnodes;
1578 if (newlist != NULL) {
9babb374 1579 (void) dnode_add_ref(dn, newlist);
64fc7762 1580 multilist_insert(newlist, dn);
edc1e713
TC
1581 } else {
1582 mutex_enter(&dn->dn_mtx);
1583 if (dn->dn_dirty_txg == tx->tx_txg)
1584 dn->dn_dirty_txg = 0;
1585 mutex_exit(&dn->dn_mtx);
9babb374
BB
1586 }
1587
34dc7c2f
BB
1588 dnode_sync(dn, tx);
1589 }
1590}
1591
1592/* ARGSUSED */
1593static void
428870ff 1594dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
34dc7c2f 1595{
b128c09f 1596 blkptr_t *bp = zio->io_bp;
428870ff 1597 objset_t *os = arg;
34dc7c2f 1598 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
b5256303 1599 uint64_t fill = 0;
34dc7c2f 1600
9b67f605 1601 ASSERT(!BP_IS_EMBEDDED(bp));
13fe0198
MA
1602 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1603 ASSERT0(BP_GET_LEVEL(bp));
34dc7c2f
BB
1604
1605 /*
9babb374
BB
1606 * Update rootbp fill count: it should be the number of objects
1607 * allocated in the object set (not counting the "special"
1608 * objects that are stored in the objset_phys_t -- the meta
9c5167d1 1609 * dnode and user/group/project accounting objects).
34dc7c2f 1610 */
1c27024e 1611 for (int i = 0; i < dnp->dn_nblkptr; i++)
b5256303
TC
1612 fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1613
1614 BP_SET_FILL(bp, fill);
1615
cc9bb3e5
GM
1616 if (os->os_dsl_dataset != NULL)
1617 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1618 *os->os_rootbp = *bp;
1619 if (os->os_dsl_dataset != NULL)
1620 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
428870ff
BB
1621}
1622
1623/* ARGSUSED */
1624static void
1625dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1626{
1627 blkptr_t *bp = zio->io_bp;
1628 blkptr_t *bp_orig = &zio->io_bp_orig;
1629 objset_t *os = arg;
34dc7c2f 1630
b128c09f 1631 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
428870ff 1632 ASSERT(BP_EQUAL(bp, bp_orig));
b128c09f 1633 } else {
428870ff
BB
1634 dsl_dataset_t *ds = os->os_dsl_dataset;
1635 dmu_tx_t *tx = os->os_synctx;
1636
1637 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1638 dsl_dataset_block_born(ds, bp, tx);
34dc7c2f 1639 }
cc9bb3e5 1640 kmem_free(bp, sizeof (*bp));
34dc7c2f
BB
1641}
1642
64fc7762
MA
1643typedef struct sync_dnodes_arg {
1644 multilist_t *sda_list;
1645 int sda_sublist_idx;
1646 multilist_t *sda_newlist;
1647 dmu_tx_t *sda_tx;
1648} sync_dnodes_arg_t;
1649
1650static void
1651sync_dnodes_task(void *arg)
1652{
1653 sync_dnodes_arg_t *sda = arg;
1654
1655 multilist_sublist_t *ms =
1656 multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
1657
1658 dmu_objset_sync_dnodes(ms, sda->sda_tx);
1659
1660 multilist_sublist_unlock(ms);
1661
1662 kmem_free(sda, sizeof (*sda));
1663}
1664
1665
34dc7c2f
BB
1666/* called from dsl */
1667void
428870ff 1668dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
34dc7c2f
BB
1669{
1670 int txgoff;
5dbd68a3 1671 zbookmark_phys_t zb;
428870ff 1672 zio_prop_t zp;
34dc7c2f
BB
1673 zio_t *zio;
1674 list_t *list;
1675 dbuf_dirty_record_t *dr;
cc9bb3e5
GM
1676 blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1677 *blkptr_copy = *os->os_rootbp;
34dc7c2f
BB
1678
1679 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1680
1681 ASSERT(dmu_tx_is_syncing(tx));
1682 /* XXX the write_done callback should really give us the tx... */
1683 os->os_synctx = tx;
1684
1685 if (os->os_dsl_dataset == NULL) {
1686 /*
1687 * This is the MOS. If we have upgraded,
1688 * spa_max_replication() could change, so reset
1689 * os_copies here.
1690 */
1691 os->os_copies = spa_max_replication(os->os_spa);
1692 }
1693
1694 /*
1695 * Create the root block IO
1696 */
428870ff
BB
1697 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1698 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1699 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
294f6806 1700 arc_release(os->os_phys_buf, &os->os_phys_buf);
b128c09f 1701
82644107 1702 dmu_write_policy(os, NULL, 0, 0, &zp);
9babb374 1703
b5256303 1704 /*
0c03d21a
MA
1705 * If we are either claiming the ZIL or doing a raw receive, write
1706 * out the os_phys_buf raw. Neither of these actions will effect the
1707 * MAC at this point.
b5256303 1708 */
0c03d21a
MA
1709 if (os->os_raw_receive ||
1710 os->os_next_write_raw[tx->tx_txg & TXG_MASK]) {
b5256303 1711 ASSERT(os->os_encrypted);
b5256303
TC
1712 arc_convert_to_raw(os->os_phys_buf,
1713 os->os_dsl_dataset->ds_object, ZFS_HOST_BYTEORDER,
1714 DMU_OT_OBJSET, NULL, NULL, NULL);
1715 }
1716
428870ff 1717 zio = arc_write(pio, os->os_spa, tx->tx_txg,
cc9bb3e5 1718 blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
bc77ba73
PD
1719 &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
1720 os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
34dc7c2f
BB
1721
1722 /*
9babb374 1723 * Sync special dnodes - the parent IO for the sync is the root block
34dc7c2f 1724 */
572e2857
BB
1725 DMU_META_DNODE(os)->dn_zio = zio;
1726 dnode_sync(DMU_META_DNODE(os), tx);
34dc7c2f 1727
9babb374
BB
1728 os->os_phys->os_flags = os->os_flags;
1729
572e2857
BB
1730 if (DMU_USERUSED_DNODE(os) &&
1731 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1732 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1733 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1734 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1735 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
9babb374
BB
1736 }
1737
9c5167d1
NF
1738 if (DMU_PROJECTUSED_DNODE(os) &&
1739 DMU_PROJECTUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1740 DMU_PROJECTUSED_DNODE(os)->dn_zio = zio;
1741 dnode_sync(DMU_PROJECTUSED_DNODE(os), tx);
1742 }
1743
34dc7c2f
BB
1744 txgoff = tx->tx_txg & TXG_MASK;
1745
b5256303
TC
1746 if (dmu_objset_userused_enabled(os) &&
1747 (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
9babb374
BB
1748 /*
1749 * We must create the list here because it uses the
64fc7762
MA
1750 * dn_dirty_link[] of this txg. But it may already
1751 * exist because we call dsl_dataset_sync() twice per txg.
9babb374 1752 */
64fc7762
MA
1753 if (os->os_synced_dnodes == NULL) {
1754 os->os_synced_dnodes =
1755 multilist_create(sizeof (dnode_t),
1756 offsetof(dnode_t, dn_dirty_link[txgoff]),
1757 dnode_multilist_index_func);
1758 } else {
1759 ASSERT3U(os->os_synced_dnodes->ml_offset, ==,
1760 offsetof(dnode_t, dn_dirty_link[txgoff]));
1761 }
9babb374
BB
1762 }
1763
64fc7762
MA
1764 for (int i = 0;
1765 i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) {
1766 sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
1767 sda->sda_list = os->os_dirty_dnodes[txgoff];
1768 sda->sda_sublist_idx = i;
1769 sda->sda_tx = tx;
1770 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1771 sync_dnodes_task, sda, 0);
1772 /* callback frees sda */
1773 }
1774 taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
34dc7c2f 1775
572e2857 1776 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
64fc7762 1777 while ((dr = list_head(list)) != NULL) {
13fe0198 1778 ASSERT0(dr->dr_dbuf->db_level);
34dc7c2f
BB
1779 list_remove(list, dr);
1780 if (dr->dr_zio)
1781 zio_nowait(dr->dr_zio);
1782 }
68cbd56e
NB
1783
1784 /* Enable dnode backfill if enough objects have been freed. */
1785 if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1786 os->os_rescan_dnodes = B_TRUE;
1787 os->os_freed_dnodes = 0;
1788 }
1789
34dc7c2f
BB
1790 /*
1791 * Free intent log blocks up to this tx.
1792 */
1793 zil_sync(os->os_zil, tx);
b128c09f 1794 os->os_phys->os_zil_header = os->os_zil_header;
34dc7c2f
BB
1795 zio_nowait(zio);
1796}
1797
428870ff
BB
1798boolean_t
1799dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1800{
64fc7762 1801 return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK]));
428870ff
BB
1802}
1803
572e2857 1804static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
9babb374
BB
1805
1806void
1807dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1808{
1809 used_cbs[ost] = cb;
1810}
1811
1812boolean_t
428870ff 1813dmu_objset_userused_enabled(objset_t *os)
9babb374
BB
1814{
1815 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
572e2857
BB
1816 used_cbs[os->os_phys->os_type] != NULL &&
1817 DMU_USERUSED_DNODE(os) != NULL);
9babb374
BB
1818}
1819
1de321e6
JX
1820boolean_t
1821dmu_objset_userobjused_enabled(objset_t *os)
1822{
1823 return (dmu_objset_userused_enabled(os) &&
1824 spa_feature_is_enabled(os->os_spa, SPA_FEATURE_USEROBJ_ACCOUNTING));
1825}
1826
9c5167d1
NF
1827boolean_t
1828dmu_objset_projectquota_enabled(objset_t *os)
1829{
1830 return (used_cbs[os->os_phys->os_type] != NULL &&
1831 DMU_PROJECTUSED_DNODE(os) != NULL &&
1832 spa_feature_is_enabled(os->os_spa, SPA_FEATURE_PROJECT_QUOTA));
1833}
1834
9b7a83cb
JX
1835typedef struct userquota_node {
1836 /* must be in the first filed, see userquota_update_cache() */
1837 char uqn_id[20 + DMU_OBJACCT_PREFIX_LEN];
1838 int64_t uqn_delta;
1839 avl_node_t uqn_node;
1840} userquota_node_t;
1841
1842typedef struct userquota_cache {
1843 avl_tree_t uqc_user_deltas;
1844 avl_tree_t uqc_group_deltas;
9c5167d1 1845 avl_tree_t uqc_project_deltas;
9b7a83cb
JX
1846} userquota_cache_t;
1847
1848static int
1849userquota_compare(const void *l, const void *r)
1850{
1851 const userquota_node_t *luqn = l;
1852 const userquota_node_t *ruqn = r;
e4ffa98d 1853 int rv;
9b7a83cb
JX
1854
1855 /*
1856 * NB: can only access uqn_id because userquota_update_cache() doesn't
1857 * pass in an entire userquota_node_t.
1858 */
e4ffa98d
BB
1859 rv = strcmp(luqn->uqn_id, ruqn->uqn_id);
1860
1861 return (AVL_ISIGN(rv));
9b7a83cb
JX
1862}
1863
1864static void
1865do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
1866{
1867 void *cookie;
1868 userquota_node_t *uqn;
1869
1870 ASSERT(dmu_tx_is_syncing(tx));
1871
1872 cookie = NULL;
1873 while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
1874 &cookie)) != NULL) {
64fc7762
MA
1875 /*
1876 * os_userused_lock protects against concurrent calls to
1877 * zap_increment_int(). It's needed because zap_increment_int()
1878 * is not thread-safe (i.e. not atomic).
1879 */
1880 mutex_enter(&os->os_userused_lock);
9b7a83cb
JX
1881 VERIFY0(zap_increment(os, DMU_USERUSED_OBJECT,
1882 uqn->uqn_id, uqn->uqn_delta, tx));
64fc7762 1883 mutex_exit(&os->os_userused_lock);
9b7a83cb
JX
1884 kmem_free(uqn, sizeof (*uqn));
1885 }
1886 avl_destroy(&cache->uqc_user_deltas);
1887
1888 cookie = NULL;
1889 while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
1890 &cookie)) != NULL) {
64fc7762 1891 mutex_enter(&os->os_userused_lock);
9b7a83cb
JX
1892 VERIFY0(zap_increment(os, DMU_GROUPUSED_OBJECT,
1893 uqn->uqn_id, uqn->uqn_delta, tx));
64fc7762 1894 mutex_exit(&os->os_userused_lock);
9b7a83cb
JX
1895 kmem_free(uqn, sizeof (*uqn));
1896 }
1897 avl_destroy(&cache->uqc_group_deltas);
9c5167d1
NF
1898
1899 if (dmu_objset_projectquota_enabled(os)) {
1900 cookie = NULL;
1901 while ((uqn = avl_destroy_nodes(&cache->uqc_project_deltas,
1902 &cookie)) != NULL) {
1903 mutex_enter(&os->os_userused_lock);
1904 VERIFY0(zap_increment(os, DMU_PROJECTUSED_OBJECT,
1905 uqn->uqn_id, uqn->uqn_delta, tx));
1906 mutex_exit(&os->os_userused_lock);
1907 kmem_free(uqn, sizeof (*uqn));
1908 }
1909 avl_destroy(&cache->uqc_project_deltas);
1910 }
9b7a83cb
JX
1911}
1912
1913static void
1914userquota_update_cache(avl_tree_t *avl, const char *id, int64_t delta)
1915{
1916 userquota_node_t *uqn;
1917 avl_index_t idx;
1918
1919 ASSERT(strlen(id) < sizeof (uqn->uqn_id));
1920 /*
1921 * Use id directly for searching because uqn_id is the first field of
1922 * userquota_node_t and fields after uqn_id won't be accessed in
1923 * avl_find().
1924 */
1925 uqn = avl_find(avl, (const void *)id, &idx);
1926 if (uqn == NULL) {
1927 uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
1f51b525 1928 strlcpy(uqn->uqn_id, id, sizeof (uqn->uqn_id));
9b7a83cb
JX
1929 avl_insert(avl, uqn, idx);
1930 }
1931 uqn->uqn_delta += delta;
1932}
1933
428870ff 1934static void
9c5167d1
NF
1935do_userquota_update(objset_t *os, userquota_cache_t *cache, uint64_t used,
1936 uint64_t flags, uint64_t user, uint64_t group, uint64_t project,
1937 boolean_t subtract)
428870ff 1938{
9c5167d1 1939 if (flags & DNODE_FLAG_USERUSED_ACCOUNTED) {
50c957f7 1940 int64_t delta = DNODE_MIN_SIZE + used;
9b7a83cb
JX
1941 char name[20];
1942
428870ff
BB
1943 if (subtract)
1944 delta = -delta;
9b7a83cb
JX
1945
1946 (void) sprintf(name, "%llx", (longlong_t)user);
1947 userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1948
1949 (void) sprintf(name, "%llx", (longlong_t)group);
1950 userquota_update_cache(&cache->uqc_group_deltas, name, delta);
9c5167d1
NF
1951
1952 if (dmu_objset_projectquota_enabled(os)) {
1953 (void) sprintf(name, "%llx", (longlong_t)project);
1954 userquota_update_cache(&cache->uqc_project_deltas,
1955 name, delta);
1956 }
428870ff
BB
1957 }
1958}
1959
1de321e6 1960static void
9c5167d1
NF
1961do_userobjquota_update(objset_t *os, userquota_cache_t *cache, uint64_t flags,
1962 uint64_t user, uint64_t group, uint64_t project, boolean_t subtract)
1de321e6
JX
1963{
1964 if (flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) {
1965 char name[20 + DMU_OBJACCT_PREFIX_LEN];
9b7a83cb 1966 int delta = subtract ? -1 : 1;
1de321e6
JX
1967
1968 (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1969 (longlong_t)user);
9b7a83cb 1970 userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1de321e6
JX
1971
1972 (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1973 (longlong_t)group);
9b7a83cb 1974 userquota_update_cache(&cache->uqc_group_deltas, name, delta);
9c5167d1
NF
1975
1976 if (dmu_objset_projectquota_enabled(os)) {
1977 (void) snprintf(name, sizeof (name),
1978 DMU_OBJACCT_PREFIX "%llx", (longlong_t)project);
1979 userquota_update_cache(&cache->uqc_project_deltas,
1980 name, delta);
1981 }
1de321e6
JX
1982 }
1983}
1984
64fc7762
MA
1985typedef struct userquota_updates_arg {
1986 objset_t *uua_os;
1987 int uua_sublist_idx;
1988 dmu_tx_t *uua_tx;
1989} userquota_updates_arg_t;
1990
1991static void
1992userquota_updates_task(void *arg)
9babb374 1993{
64fc7762
MA
1994 userquota_updates_arg_t *uua = arg;
1995 objset_t *os = uua->uua_os;
1996 dmu_tx_t *tx = uua->uua_tx;
9babb374 1997 dnode_t *dn;
9b7a83cb 1998 userquota_cache_t cache = { { 0 } };
9babb374 1999
64fc7762
MA
2000 multilist_sublist_t *list =
2001 multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx);
9babb374 2002
64fc7762
MA
2003 ASSERT(multilist_sublist_head(list) == NULL ||
2004 dmu_objset_userused_enabled(os));
9b7a83cb
JX
2005 avl_create(&cache.uqc_user_deltas, userquota_compare,
2006 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
2007 avl_create(&cache.uqc_group_deltas, userquota_compare,
2008 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
9c5167d1
NF
2009 if (dmu_objset_projectquota_enabled(os))
2010 avl_create(&cache.uqc_project_deltas, userquota_compare,
2011 sizeof (userquota_node_t), offsetof(userquota_node_t,
2012 uqn_node));
9b7a83cb 2013
64fc7762 2014 while ((dn = multilist_sublist_head(list)) != NULL) {
572e2857 2015 int flags;
9babb374 2016 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
9babb374
BB
2017 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
2018 dn->dn_phys->dn_flags &
2019 DNODE_FLAG_USERUSED_ACCOUNTED);
2020
572e2857
BB
2021 flags = dn->dn_id_flags;
2022 ASSERT(flags);
2023 if (flags & DN_ID_OLD_EXIST) {
9c5167d1
NF
2024 do_userquota_update(os, &cache, dn->dn_oldused,
2025 dn->dn_oldflags, dn->dn_olduid, dn->dn_oldgid,
2026 dn->dn_oldprojid, B_TRUE);
2027 do_userobjquota_update(os, &cache, dn->dn_oldflags,
2028 dn->dn_olduid, dn->dn_oldgid,
2029 dn->dn_oldprojid, B_TRUE);
428870ff 2030 }
572e2857 2031 if (flags & DN_ID_NEW_EXIST) {
9c5167d1 2032 do_userquota_update(os, &cache,
9b7a83cb 2033 DN_USED_BYTES(dn->dn_phys), dn->dn_phys->dn_flags,
9c5167d1
NF
2034 dn->dn_newuid, dn->dn_newgid,
2035 dn->dn_newprojid, B_FALSE);
2036 do_userobjquota_update(os, &cache,
2037 dn->dn_phys->dn_flags, dn->dn_newuid, dn->dn_newgid,
2038 dn->dn_newprojid, B_FALSE);
428870ff
BB
2039 }
2040
572e2857 2041 mutex_enter(&dn->dn_mtx);
428870ff
BB
2042 dn->dn_oldused = 0;
2043 dn->dn_oldflags = 0;
2044 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
2045 dn->dn_olduid = dn->dn_newuid;
2046 dn->dn_oldgid = dn->dn_newgid;
9c5167d1 2047 dn->dn_oldprojid = dn->dn_newprojid;
428870ff
BB
2048 dn->dn_id_flags |= DN_ID_OLD_EXIST;
2049 if (dn->dn_bonuslen == 0)
2050 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
2051 else
2052 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2053 }
2054 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
edc1e713
TC
2055 if (dn->dn_dirty_txg == spa_syncing_txg(os->os_spa))
2056 dn->dn_dirty_txg = 0;
9babb374
BB
2057 mutex_exit(&dn->dn_mtx);
2058
64fc7762
MA
2059 multilist_sublist_remove(list, dn);
2060 dnode_rele(dn, os->os_synced_dnodes);
9babb374 2061 }
9b7a83cb 2062 do_userquota_cacheflush(os, &cache, tx);
64fc7762
MA
2063 multilist_sublist_unlock(list);
2064 kmem_free(uua, sizeof (*uua));
2065}
2066
2067void
2068dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
2069{
2070 if (!dmu_objset_userused_enabled(os))
2071 return;
2072
163a8c28
TC
2073 /*
2074 * If this is a raw receive just return and handle accounting
2075 * later when we have the keys loaded. We also don't do user
2076 * accounting during claiming since the datasets are not owned
2077 * for the duration of claiming and this txg should only be
2078 * used for recovery.
2079 */
b5256303
TC
2080 if (os->os_encrypted && dmu_objset_is_receiving(os))
2081 return;
2082
163a8c28
TC
2083 if (tx->tx_txg <= os->os_spa->spa_claim_max_txg)
2084 return;
2085
9c5167d1 2086 /* Allocate the user/group/project used objects if necessary. */
64fc7762
MA
2087 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
2088 VERIFY0(zap_create_claim(os,
2089 DMU_USERUSED_OBJECT,
2090 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2091 VERIFY0(zap_create_claim(os,
2092 DMU_GROUPUSED_OBJECT,
2093 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2094 }
2095
9c5167d1
NF
2096 if (dmu_objset_projectquota_enabled(os) &&
2097 DMU_PROJECTUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
2098 VERIFY0(zap_create_claim(os, DMU_PROJECTUSED_OBJECT,
2099 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2100 }
2101
64fc7762
MA
2102 for (int i = 0;
2103 i < multilist_get_num_sublists(os->os_synced_dnodes); i++) {
2104 userquota_updates_arg_t *uua =
2105 kmem_alloc(sizeof (*uua), KM_SLEEP);
2106 uua->uua_os = os;
2107 uua->uua_sublist_idx = i;
2108 uua->uua_tx = tx;
2109 /* note: caller does taskq_wait() */
2110 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
2111 userquota_updates_task, uua, 0);
2112 /* callback frees uua */
2113 }
9babb374
BB
2114}
2115
428870ff
BB
2116/*
2117 * Returns a pointer to data to find uid/gid from
2118 *
2119 * If a dirty record for transaction group that is syncing can't
2120 * be found then NULL is returned. In the NULL case it is assumed
2121 * the uid/gid aren't changing.
2122 */
2123static void *
2124dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
2125{
2126 dbuf_dirty_record_t *dr, **drp;
2127 void *data;
2128
2129 if (db->db_dirtycnt == 0)
2130 return (db->db.db_data); /* Nothing is changing */
2131
2132 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
2133 if (dr->dr_txg == tx->tx_txg)
2134 break;
2135
572e2857 2136 if (dr == NULL) {
428870ff 2137 data = NULL;
572e2857
BB
2138 } else {
2139 dnode_t *dn;
2140
2141 DB_DNODE_ENTER(dr->dr_dbuf);
2142 dn = DB_DNODE(dr->dr_dbuf);
2143
2144 if (dn->dn_bonuslen == 0 &&
2145 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
2146 data = dr->dt.dl.dr_data->b_data;
2147 else
2148 data = dr->dt.dl.dr_data;
2149
2150 DB_DNODE_EXIT(dr->dr_dbuf);
2151 }
2152
428870ff
BB
2153 return (data);
2154}
2155
2156void
2157dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
2158{
2159 objset_t *os = dn->dn_objset;
2160 void *data = NULL;
2161 dmu_buf_impl_t *db = NULL;
a117a6d6
GW
2162 uint64_t *user = NULL;
2163 uint64_t *group = NULL;
9c5167d1 2164 uint64_t *project = NULL;
428870ff
BB
2165 int flags = dn->dn_id_flags;
2166 int error;
2167 boolean_t have_spill = B_FALSE;
2168
2169 if (!dmu_objset_userused_enabled(dn->dn_objset))
2170 return;
2171
b5256303
TC
2172 /*
2173 * Raw receives introduce a problem with user accounting. Raw
2174 * receives cannot update the user accounting info because the
2175 * user ids and the sizes are encrypted. To guarantee that we
2176 * never end up with bad user accounting, we simply disable it
2177 * during raw receives. We also disable this for normal receives
2178 * so that an incremental raw receive may be done on top of an
2179 * existing non-raw receive.
2180 */
2181 if (os->os_encrypted && dmu_objset_is_receiving(os))
2182 return;
2183
428870ff
BB
2184 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
2185 DN_ID_CHKED_SPILL)))
2186 return;
2187
2188 if (before && dn->dn_bonuslen != 0)
2189 data = DN_BONUS(dn->dn_phys);
2190 else if (!before && dn->dn_bonuslen != 0) {
a3000f93
BB
2191 if (dn->dn_bonus) {
2192 db = dn->dn_bonus;
428870ff
BB
2193 mutex_enter(&db->db_mtx);
2194 data = dmu_objset_userquota_find_data(db, tx);
2195 } else {
2196 data = DN_BONUS(dn->dn_phys);
2197 }
2198 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
2199 int rf = 0;
2200
2201 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
2202 rf |= DB_RF_HAVESTRUCT;
572e2857
BB
2203 error = dmu_spill_hold_by_dnode(dn,
2204 rf | DB_RF_MUST_SUCCEED,
428870ff
BB
2205 FTAG, (dmu_buf_t **)&db);
2206 ASSERT(error == 0);
2207 mutex_enter(&db->db_mtx);
2208 data = (before) ? db->db.db_data :
2209 dmu_objset_userquota_find_data(db, tx);
2210 have_spill = B_TRUE;
2211 } else {
2212 mutex_enter(&dn->dn_mtx);
2213 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2214 mutex_exit(&dn->dn_mtx);
2215 return;
2216 }
2217
2218 if (before) {
2219 ASSERT(data);
2220 user = &dn->dn_olduid;
2221 group = &dn->dn_oldgid;
9c5167d1 2222 project = &dn->dn_oldprojid;
428870ff
BB
2223 } else if (data) {
2224 user = &dn->dn_newuid;
2225 group = &dn->dn_newgid;
9c5167d1 2226 project = &dn->dn_newprojid;
428870ff
BB
2227 }
2228
2229 /*
2230 * Must always call the callback in case the object
2231 * type has changed and that type isn't an object type to track
2232 */
2233 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
9c5167d1 2234 user, group, project);
428870ff
BB
2235
2236 /*
2237 * Preserve existing uid/gid when the callback can't determine
2238 * what the new uid/gid are and the callback returned EEXIST.
2239 * The EEXIST error tells us to just use the existing uid/gid.
2240 * If we don't know what the old values are then just assign
2241 * them to 0, since that is a new file being created.
2242 */
2243 if (!before && data == NULL && error == EEXIST) {
2244 if (flags & DN_ID_OLD_EXIST) {
2245 dn->dn_newuid = dn->dn_olduid;
2246 dn->dn_newgid = dn->dn_oldgid;
2705ebf0 2247 dn->dn_newprojid = dn->dn_oldprojid;
428870ff
BB
2248 } else {
2249 dn->dn_newuid = 0;
2250 dn->dn_newgid = 0;
9c5167d1 2251 dn->dn_newprojid = ZFS_DEFAULT_PROJID;
428870ff
BB
2252 }
2253 error = 0;
2254 }
2255
2256 if (db)
2257 mutex_exit(&db->db_mtx);
2258
2259 mutex_enter(&dn->dn_mtx);
2260 if (error == 0 && before)
2261 dn->dn_id_flags |= DN_ID_OLD_EXIST;
2262 if (error == 0 && !before)
2263 dn->dn_id_flags |= DN_ID_NEW_EXIST;
2264
2265 if (have_spill) {
2266 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
2267 } else {
2268 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2269 }
2270 mutex_exit(&dn->dn_mtx);
a3000f93 2271 if (have_spill)
428870ff
BB
2272 dmu_buf_rele((dmu_buf_t *)db, FTAG);
2273}
2274
9babb374
BB
2275boolean_t
2276dmu_objset_userspace_present(objset_t *os)
2277{
428870ff 2278 return (os->os_phys->os_flags &
9babb374
BB
2279 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
2280}
2281
1de321e6
JX
2282boolean_t
2283dmu_objset_userobjspace_present(objset_t *os)
2284{
2285 return (os->os_phys->os_flags &
2286 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE);
2287}
2288
9c5167d1
NF
2289boolean_t
2290dmu_objset_projectquota_present(objset_t *os)
2291{
2292 return (os->os_phys->os_flags &
2293 OBJSET_FLAG_PROJECTQUOTA_COMPLETE);
2294}
2295
1de321e6
JX
2296static int
2297dmu_objset_space_upgrade(objset_t *os)
9babb374
BB
2298{
2299 uint64_t obj;
2300 int err = 0;
2301
9babb374
BB
2302 /*
2303 * We simply need to mark every object dirty, so that it will be
2304 * synced out and now accounted. If this is called
2305 * concurrently, or if we already did some work before crashing,
2306 * that's fine, since we track each object's accounted state
2307 * independently.
2308 */
2309
2310 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
45d1cae3 2311 dmu_tx_t *tx;
9babb374
BB
2312 dmu_buf_t *db;
2313 int objerr;
2314
1de321e6
JX
2315 mutex_enter(&os->os_upgrade_lock);
2316 if (os->os_upgrade_exit)
2317 err = SET_ERROR(EINTR);
2318 mutex_exit(&os->os_upgrade_lock);
2319 if (err != 0)
2320 return (err);
2321
9babb374 2322 if (issig(JUSTLOOKING) && issig(FORREAL))
2e528b49 2323 return (SET_ERROR(EINTR));
9babb374
BB
2324
2325 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
13fe0198 2326 if (objerr != 0)
9babb374 2327 continue;
45d1cae3 2328 tx = dmu_tx_create(os);
9babb374
BB
2329 dmu_tx_hold_bonus(tx, obj);
2330 objerr = dmu_tx_assign(tx, TXG_WAIT);
13fe0198 2331 if (objerr != 0) {
d22323e8 2332 dmu_buf_rele(db, FTAG);
9babb374
BB
2333 dmu_tx_abort(tx);
2334 continue;
2335 }
2336 dmu_buf_will_dirty(db, tx);
2337 dmu_buf_rele(db, FTAG);
2338 dmu_tx_commit(tx);
2339 }
1de321e6
JX
2340 return (0);
2341}
2342
2343int
2344dmu_objset_userspace_upgrade(objset_t *os)
2345{
2346 int err = 0;
2347
2348 if (dmu_objset_userspace_present(os))
2349 return (0);
2350 if (dmu_objset_is_snapshot(os))
2351 return (SET_ERROR(EINVAL));
2352 if (!dmu_objset_userused_enabled(os))
2353 return (SET_ERROR(ENOTSUP));
2354
2355 err = dmu_objset_space_upgrade(os);
2356 if (err)
2357 return (err);
9babb374 2358
428870ff 2359 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
9babb374
BB
2360 txg_wait_synced(dmu_objset_pool(os), 0);
2361 return (0);
2362}
2363
1de321e6 2364static int
9c5167d1 2365dmu_objset_id_quota_upgrade_cb(objset_t *os)
1de321e6
JX
2366{
2367 int err = 0;
2368
9c5167d1
NF
2369 if (dmu_objset_userobjspace_present(os) &&
2370 dmu_objset_projectquota_present(os))
1de321e6
JX
2371 return (0);
2372 if (dmu_objset_is_snapshot(os))
2373 return (SET_ERROR(EINVAL));
2374 if (!dmu_objset_userobjused_enabled(os))
2375 return (SET_ERROR(ENOTSUP));
9c5167d1
NF
2376 if (!dmu_objset_projectquota_enabled(os) &&
2377 dmu_objset_userobjspace_present(os))
2378 return (SET_ERROR(ENOTSUP));
1de321e6
JX
2379
2380 dmu_objset_ds(os)->ds_feature_activation_needed[
2381 SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE;
9c5167d1
NF
2382 if (dmu_objset_projectquota_enabled(os))
2383 dmu_objset_ds(os)->ds_feature_activation_needed[
2384 SPA_FEATURE_PROJECT_QUOTA] = B_TRUE;
1de321e6
JX
2385
2386 err = dmu_objset_space_upgrade(os);
2387 if (err)
2388 return (err);
2389
2390 os->os_flags |= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
9c5167d1
NF
2391 if (dmu_objset_projectquota_enabled(os))
2392 os->os_flags |= OBJSET_FLAG_PROJECTQUOTA_COMPLETE;
2393
1de321e6
JX
2394 txg_wait_synced(dmu_objset_pool(os), 0);
2395 return (0);
2396}
2397
2398void
9c5167d1 2399dmu_objset_id_quota_upgrade(objset_t *os)
1de321e6 2400{
9c5167d1 2401 dmu_objset_upgrade(os, dmu_objset_id_quota_upgrade_cb);
1de321e6
JX
2402}
2403
126ae9f4
JX
2404boolean_t
2405dmu_objset_userobjspace_upgradable(objset_t *os)
2406{
2407 return (dmu_objset_type(os) == DMU_OST_ZFS &&
2408 !dmu_objset_is_snapshot(os) &&
2409 dmu_objset_userobjused_enabled(os) &&
2410 !dmu_objset_userobjspace_present(os));
2411}
2412
9c5167d1
NF
2413boolean_t
2414dmu_objset_projectquota_upgradable(objset_t *os)
2415{
2416 return (dmu_objset_type(os) == DMU_OST_ZFS &&
2417 !dmu_objset_is_snapshot(os) &&
2418 dmu_objset_projectquota_enabled(os) &&
2419 !dmu_objset_projectquota_present(os));
2420}
2421
34dc7c2f
BB
2422void
2423dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
2424 uint64_t *usedobjsp, uint64_t *availobjsp)
2425{
428870ff 2426 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
34dc7c2f
BB
2427 usedobjsp, availobjsp);
2428}
2429
2430uint64_t
2431dmu_objset_fsid_guid(objset_t *os)
2432{
428870ff 2433 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
34dc7c2f
BB
2434}
2435
2436void
2437dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
2438{
428870ff
BB
2439 stat->dds_type = os->os_phys->os_type;
2440 if (os->os_dsl_dataset)
2441 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
34dc7c2f
BB
2442}
2443
2444void
2445dmu_objset_stats(objset_t *os, nvlist_t *nv)
2446{
428870ff
BB
2447 ASSERT(os->os_dsl_dataset ||
2448 os->os_phys->os_type == DMU_OST_META);
34dc7c2f 2449
428870ff
BB
2450 if (os->os_dsl_dataset != NULL)
2451 dsl_dataset_stats(os->os_dsl_dataset, nv);
34dc7c2f
BB
2452
2453 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
428870ff 2454 os->os_phys->os_type);
9babb374
BB
2455 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
2456 dmu_objset_userspace_present(os));
34dc7c2f
BB
2457}
2458
2459int
2460dmu_objset_is_snapshot(objset_t *os)
2461{
428870ff 2462 if (os->os_dsl_dataset != NULL)
0c66c32d 2463 return (os->os_dsl_dataset->ds_is_snapshot);
34dc7c2f
BB
2464 else
2465 return (B_FALSE);
2466}
2467
2468int
2469dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
2470 boolean_t *conflict)
2471{
428870ff 2472 dsl_dataset_t *ds = os->os_dsl_dataset;
34dc7c2f
BB
2473 uint64_t ignored;
2474
d683ddbb 2475 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2e528b49 2476 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2477
2478 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
d683ddbb 2479 dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
9b7b9cd3 2480 MT_NORMALIZE, real, maxlen, conflict));
34dc7c2f
BB
2481}
2482
2483int
2484dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
2485 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
2486{
428870ff 2487 dsl_dataset_t *ds = os->os_dsl_dataset;
34dc7c2f
BB
2488 zap_cursor_t cursor;
2489 zap_attribute_t attr;
2490
13fe0198
MA
2491 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
2492
d683ddbb 2493 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2e528b49 2494 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2495
2496 zap_cursor_init_serialized(&cursor,
2497 ds->ds_dir->dd_pool->dp_meta_objset,
d683ddbb 2498 dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
34dc7c2f
BB
2499
2500 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2501 zap_cursor_fini(&cursor);
2e528b49 2502 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2503 }
2504
2505 if (strlen(attr.za_name) + 1 > namelen) {
2506 zap_cursor_fini(&cursor);
2e528b49 2507 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f
BB
2508 }
2509
2510 (void) strcpy(name, attr.za_name);
2511 if (idp)
2512 *idp = attr.za_first_integer;
2513 if (case_conflict)
2514 *case_conflict = attr.za_normalization_conflict;
2515 zap_cursor_advance(&cursor);
2516 *offp = zap_cursor_serialize(&cursor);
2517 zap_cursor_fini(&cursor);
2518
2519 return (0);
2520}
2521
ebe7e575 2522int
6772fb67 2523dmu_snapshot_lookup(objset_t *os, const char *name, uint64_t *value)
ebe7e575 2524{
d1d7e268 2525 return (dsl_dataset_snap_lookup(os->os_dsl_dataset, name, value));
ebe7e575
BB
2526}
2527
34dc7c2f
BB
2528int
2529dmu_dir_list_next(objset_t *os, int namelen, char *name,
2530 uint64_t *idp, uint64_t *offp)
2531{
428870ff 2532 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
34dc7c2f
BB
2533 zap_cursor_t cursor;
2534 zap_attribute_t attr;
2535
2536 /* there is no next dir on a snapshot! */
428870ff 2537 if (os->os_dsl_dataset->ds_object !=
d683ddbb 2538 dsl_dir_phys(dd)->dd_head_dataset_obj)
2e528b49 2539 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2540
2541 zap_cursor_init_serialized(&cursor,
2542 dd->dd_pool->dp_meta_objset,
d683ddbb 2543 dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
34dc7c2f
BB
2544
2545 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2546 zap_cursor_fini(&cursor);
2e528b49 2547 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2548 }
2549
2550 if (strlen(attr.za_name) + 1 > namelen) {
2551 zap_cursor_fini(&cursor);
2e528b49 2552 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f
BB
2553 }
2554
2555 (void) strcpy(name, attr.za_name);
2556 if (idp)
2557 *idp = attr.za_first_integer;
2558 zap_cursor_advance(&cursor);
2559 *offp = zap_cursor_serialize(&cursor);
2560 zap_cursor_fini(&cursor);
2561
2562 return (0);
2563}
2564
9c43027b
AJ
2565typedef struct dmu_objset_find_ctx {
2566 taskq_t *dc_tq;
2567 dsl_pool_t *dc_dp;
2568 uint64_t dc_ddobj;
1149ba64 2569 char *dc_ddname; /* last component of ddobj's name */
9c43027b
AJ
2570 int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
2571 void *dc_arg;
2572 int dc_flags;
2573 kmutex_t *dc_error_lock;
2574 int *dc_error;
2575} dmu_objset_find_ctx_t;
2576
2577static void
2578dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
b128c09f 2579{
9c43027b 2580 dsl_pool_t *dp = dcp->dc_dp;
13fe0198
MA
2581 dsl_dir_t *dd;
2582 dsl_dataset_t *ds;
2583 zap_cursor_t zc;
2584 zap_attribute_t *attr;
2585 uint64_t thisobj;
9c43027b 2586 int err = 0;
13fe0198 2587
9c43027b
AJ
2588 /* don't process if there already was an error */
2589 if (*dcp->dc_error != 0)
2590 goto out;
13fe0198 2591
1149ba64
GM
2592 /*
2593 * Note: passing the name (dc_ddname) here is optional, but it
2594 * improves performance because we don't need to call
2595 * zap_value_search() to determine the name.
2596 */
2597 err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
13fe0198 2598 if (err != 0)
9c43027b 2599 goto out;
13fe0198
MA
2600
2601 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2602 if (dd->dd_myname[0] == '$') {
2603 dsl_dir_rele(dd, FTAG);
9c43027b 2604 goto out;
13fe0198
MA
2605 }
2606
d683ddbb 2607 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
79c76d5b 2608 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
13fe0198
MA
2609
2610 /*
2611 * Iterate over all children.
2612 */
9c43027b 2613 if (dcp->dc_flags & DS_FIND_CHILDREN) {
13fe0198 2614 for (zap_cursor_init(&zc, dp->dp_meta_objset,
d683ddbb 2615 dsl_dir_phys(dd)->dd_child_dir_zapobj);
13fe0198
MA
2616 zap_cursor_retrieve(&zc, attr) == 0;
2617 (void) zap_cursor_advance(&zc)) {
2618 ASSERT3U(attr->za_integer_length, ==,
2619 sizeof (uint64_t));
2620 ASSERT3U(attr->za_num_integers, ==, 1);
2621
1c27024e 2622 dmu_objset_find_ctx_t *child_dcp =
1149ba64 2623 kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
9c43027b
AJ
2624 *child_dcp = *dcp;
2625 child_dcp->dc_ddobj = attr->za_first_integer;
1149ba64 2626 child_dcp->dc_ddname = spa_strdup(attr->za_name);
9c43027b
AJ
2627 if (dcp->dc_tq != NULL)
2628 (void) taskq_dispatch(dcp->dc_tq,
2629 dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
2630 else
2631 dmu_objset_find_dp_impl(child_dcp);
13fe0198
MA
2632 }
2633 zap_cursor_fini(&zc);
13fe0198
MA
2634 }
2635
2636 /*
2637 * Iterate over all snapshots.
2638 */
9c43027b 2639 if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
13fe0198
MA
2640 dsl_dataset_t *ds;
2641 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2642
2643 if (err == 0) {
d683ddbb
JG
2644 uint64_t snapobj;
2645
2646 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
13fe0198
MA
2647 dsl_dataset_rele(ds, FTAG);
2648
2649 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2650 zap_cursor_retrieve(&zc, attr) == 0;
2651 (void) zap_cursor_advance(&zc)) {
2652 ASSERT3U(attr->za_integer_length, ==,
2653 sizeof (uint64_t));
2654 ASSERT3U(attr->za_num_integers, ==, 1);
2655
2656 err = dsl_dataset_hold_obj(dp,
2657 attr->za_first_integer, FTAG, &ds);
2658 if (err != 0)
2659 break;
9c43027b 2660 err = dcp->dc_func(dp, ds, dcp->dc_arg);
13fe0198
MA
2661 dsl_dataset_rele(ds, FTAG);
2662 if (err != 0)
2663 break;
2664 }
2665 zap_cursor_fini(&zc);
2666 }
2667 }
2668
13fe0198
MA
2669 kmem_free(attr, sizeof (zap_attribute_t));
2670
1149ba64
GM
2671 if (err != 0) {
2672 dsl_dir_rele(dd, FTAG);
9c43027b 2673 goto out;
1149ba64 2674 }
13fe0198
MA
2675
2676 /*
2677 * Apply to self.
2678 */
2679 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1149ba64
GM
2680
2681 /*
2682 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2683 * that the dir will remain cached, and we won't have to re-instantiate
2684 * it (which could be expensive due to finding its name via
2685 * zap_value_search()).
2686 */
2687 dsl_dir_rele(dd, FTAG);
13fe0198 2688 if (err != 0)
9c43027b
AJ
2689 goto out;
2690 err = dcp->dc_func(dp, ds, dcp->dc_arg);
13fe0198 2691 dsl_dataset_rele(ds, FTAG);
9c43027b
AJ
2692
2693out:
2694 if (err != 0) {
2695 mutex_enter(dcp->dc_error_lock);
2696 /* only keep first error */
2697 if (*dcp->dc_error == 0)
2698 *dcp->dc_error = err;
2699 mutex_exit(dcp->dc_error_lock);
2700 }
2701
1149ba64
GM
2702 if (dcp->dc_ddname != NULL)
2703 spa_strfree(dcp->dc_ddname);
9c43027b
AJ
2704 kmem_free(dcp, sizeof (*dcp));
2705}
2706
2707static void
2708dmu_objset_find_dp_cb(void *arg)
2709{
2710 dmu_objset_find_ctx_t *dcp = arg;
2711 dsl_pool_t *dp = dcp->dc_dp;
2712
5e8cd5d1
AJ
2713 /*
2714 * We need to get a pool_config_lock here, as there are several
2715 * asssert(pool_config_held) down the stack. Getting a lock via
2716 * dsl_pool_config_enter is risky, as it might be stalled by a
2717 * pending writer. This would deadlock, as the write lock can
2718 * only be granted when our parent thread gives up the lock.
2719 * The _prio interface gives us priority over a pending writer.
2720 */
2721 dsl_pool_config_enter_prio(dp, FTAG);
9c43027b
AJ
2722
2723 dmu_objset_find_dp_impl(dcp);
2724
2725 dsl_pool_config_exit(dp, FTAG);
2726}
2727
2728/*
2729 * Find objsets under and including ddobj, call func(ds) on each.
2730 * The order for the enumeration is completely undefined.
2731 * func is called with dsl_pool_config held.
2732 */
2733int
2734dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
2735 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
2736{
2737 int error = 0;
2738 taskq_t *tq = NULL;
2739 int ntasks;
2740 dmu_objset_find_ctx_t *dcp;
2741 kmutex_t err_lock;
2742
2743 mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
2744 dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
2745 dcp->dc_tq = NULL;
2746 dcp->dc_dp = dp;
2747 dcp->dc_ddobj = ddobj;
1149ba64 2748 dcp->dc_ddname = NULL;
9c43027b
AJ
2749 dcp->dc_func = func;
2750 dcp->dc_arg = arg;
2751 dcp->dc_flags = flags;
2752 dcp->dc_error_lock = &err_lock;
2753 dcp->dc_error = &error;
2754
2755 if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
2756 /*
2757 * In case a write lock is held we can't make use of
2758 * parallelism, as down the stack of the worker threads
2759 * the lock is asserted via dsl_pool_config_held.
2760 * In case of a read lock this is solved by getting a read
2761 * lock in each worker thread, which isn't possible in case
2762 * of a writer lock. So we fall back to the synchronous path
2763 * here.
2764 * In the future it might be possible to get some magic into
2765 * dsl_pool_config_held in a way that it returns true for
2766 * the worker threads so that a single lock held from this
2767 * thread suffices. For now, stay single threaded.
2768 */
2769 dmu_objset_find_dp_impl(dcp);
e5676636 2770 mutex_destroy(&err_lock);
9c43027b
AJ
2771
2772 return (error);
2773 }
2774
2775 ntasks = dmu_find_threads;
2776 if (ntasks == 0)
2777 ntasks = vdev_count_leaves(dp->dp_spa) * 4;
1229323d 2778 tq = taskq_create("dmu_objset_find", ntasks, maxclsyspri, ntasks,
9c43027b
AJ
2779 INT_MAX, 0);
2780 if (tq == NULL) {
2781 kmem_free(dcp, sizeof (*dcp));
e5676636
BB
2782 mutex_destroy(&err_lock);
2783
9c43027b
AJ
2784 return (SET_ERROR(ENOMEM));
2785 }
2786 dcp->dc_tq = tq;
2787
2788 /* dcp will be freed by task */
2789 (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
2790
2791 /*
2792 * PORTING: this code relies on the property of taskq_wait to wait
2793 * until no more tasks are queued and no more tasks are active. As
2794 * we always queue new tasks from within other tasks, task_wait
2795 * reliably waits for the full recursion to finish, even though we
2796 * enqueue new tasks after taskq_wait has been called.
2797 * On platforms other than illumos, taskq_wait may not have this
2798 * property.
2799 */
2800 taskq_wait(tq);
2801 taskq_destroy(tq);
2802 mutex_destroy(&err_lock);
2803
2804 return (error);
b128c09f
BB
2805}
2806
2807/*
13fe0198
MA
2808 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2809 * The dp_config_rwlock must not be held when this is called, and it
2810 * will not be held when the callback is called.
2811 * Therefore this function should only be used when the pool is not changing
2812 * (e.g. in syncing context), or the callback can deal with the possible races.
b128c09f 2813 */
13fe0198
MA
2814static int
2815dmu_objset_find_impl(spa_t *spa, const char *name,
2816 int func(const char *, void *), void *arg, int flags)
34dc7c2f
BB
2817{
2818 dsl_dir_t *dd;
13fe0198 2819 dsl_pool_t *dp = spa_get_dsl(spa);
b128c09f 2820 dsl_dataset_t *ds;
34dc7c2f
BB
2821 zap_cursor_t zc;
2822 zap_attribute_t *attr;
2823 char *child;
b128c09f
BB
2824 uint64_t thisobj;
2825 int err;
34dc7c2f 2826
13fe0198
MA
2827 dsl_pool_config_enter(dp, FTAG);
2828
2829 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
2830 if (err != 0) {
2831 dsl_pool_config_exit(dp, FTAG);
34dc7c2f 2832 return (err);
13fe0198 2833 }
34dc7c2f 2834
b128c09f
BB
2835 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2836 if (dd->dd_myname[0] == '$') {
13fe0198
MA
2837 dsl_dir_rele(dd, FTAG);
2838 dsl_pool_config_exit(dp, FTAG);
b128c09f
BB
2839 return (0);
2840 }
2841
d683ddbb 2842 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
79c76d5b 2843 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
34dc7c2f
BB
2844
2845 /*
2846 * Iterate over all children.
2847 */
2848 if (flags & DS_FIND_CHILDREN) {
b128c09f 2849 for (zap_cursor_init(&zc, dp->dp_meta_objset,
d683ddbb 2850 dsl_dir_phys(dd)->dd_child_dir_zapobj);
34dc7c2f
BB
2851 zap_cursor_retrieve(&zc, attr) == 0;
2852 (void) zap_cursor_advance(&zc)) {
13fe0198
MA
2853 ASSERT3U(attr->za_integer_length, ==,
2854 sizeof (uint64_t));
2855 ASSERT3U(attr->za_num_integers, ==, 1);
34dc7c2f 2856
428870ff 2857 child = kmem_asprintf("%s/%s", name, attr->za_name);
13fe0198
MA
2858 dsl_pool_config_exit(dp, FTAG);
2859 err = dmu_objset_find_impl(spa, child,
2860 func, arg, flags);
2861 dsl_pool_config_enter(dp, FTAG);
428870ff 2862 strfree(child);
13fe0198 2863 if (err != 0)
34dc7c2f
BB
2864 break;
2865 }
2866 zap_cursor_fini(&zc);
2867
13fe0198
MA
2868 if (err != 0) {
2869 dsl_dir_rele(dd, FTAG);
2870 dsl_pool_config_exit(dp, FTAG);
34dc7c2f
BB
2871 kmem_free(attr, sizeof (zap_attribute_t));
2872 return (err);
2873 }
2874 }
2875
2876 /*
2877 * Iterate over all snapshots.
2878 */
b128c09f 2879 if (flags & DS_FIND_SNAPSHOTS) {
b128c09f 2880 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
b128c09f
BB
2881
2882 if (err == 0) {
d683ddbb
JG
2883 uint64_t snapobj;
2884
2885 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
b128c09f
BB
2886 dsl_dataset_rele(ds, FTAG);
2887
2888 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2889 zap_cursor_retrieve(&zc, attr) == 0;
2890 (void) zap_cursor_advance(&zc)) {
13fe0198 2891 ASSERT3U(attr->za_integer_length, ==,
b128c09f 2892 sizeof (uint64_t));
13fe0198 2893 ASSERT3U(attr->za_num_integers, ==, 1);
b128c09f 2894
428870ff
BB
2895 child = kmem_asprintf("%s@%s",
2896 name, attr->za_name);
13fe0198
MA
2897 dsl_pool_config_exit(dp, FTAG);
2898 err = func(child, arg);
2899 dsl_pool_config_enter(dp, FTAG);
428870ff 2900 strfree(child);
13fe0198 2901 if (err != 0)
b128c09f
BB
2902 break;
2903 }
2904 zap_cursor_fini(&zc);
34dc7c2f 2905 }
34dc7c2f
BB
2906 }
2907
13fe0198 2908 dsl_dir_rele(dd, FTAG);
34dc7c2f 2909 kmem_free(attr, sizeof (zap_attribute_t));
13fe0198 2910 dsl_pool_config_exit(dp, FTAG);
34dc7c2f 2911
13fe0198 2912 if (err != 0)
34dc7c2f
BB
2913 return (err);
2914
13fe0198
MA
2915 /* Apply to self. */
2916 return (func(name, arg));
34dc7c2f
BB
2917}
2918
13fe0198
MA
2919/*
2920 * See comment above dmu_objset_find_impl().
2921 */
d164b209 2922int
13fe0198
MA
2923dmu_objset_find(char *name, int func(const char *, void *), void *arg,
2924 int flags)
d164b209 2925{
13fe0198
MA
2926 spa_t *spa;
2927 int error;
d164b209 2928
13fe0198
MA
2929 error = spa_open(name, &spa, FTAG);
2930 if (error != 0)
2931 return (error);
2932 error = dmu_objset_find_impl(spa, name, func, arg, flags);
2933 spa_close(spa, FTAG);
2934 return (error);
d164b209
BB
2935}
2936
ae76f45c
TC
2937boolean_t
2938dmu_objset_incompatible_encryption_version(objset_t *os)
2939{
2940 return (dsl_dir_incompatible_encryption_version(
2941 os->os_dsl_dataset->ds_dir));
2942}
2943
34dc7c2f
BB
2944void
2945dmu_objset_set_user(objset_t *os, void *user_ptr)
2946{
428870ff
BB
2947 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2948 os->os_user_ptr = user_ptr;
34dc7c2f
BB
2949}
2950
2951void *
2952dmu_objset_get_user(objset_t *os)
2953{
428870ff
BB
2954 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2955 return (os->os_user_ptr);
34dc7c2f 2956}
c28b2279 2957
13fe0198
MA
2958/*
2959 * Determine name of filesystem, given name of snapshot.
eca7b760 2960 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
13fe0198
MA
2961 */
2962int
2963dmu_fsname(const char *snapname, char *buf)
2964{
2965 char *atp = strchr(snapname, '@');
2966 if (atp == NULL)
2e528b49 2967 return (SET_ERROR(EINVAL));
eca7b760 2968 if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2e528b49 2969 return (SET_ERROR(ENAMETOOLONG));
13fe0198
MA
2970 (void) strlcpy(buf, snapname, atp - snapname + 1);
2971 return (0);
2972}
2973
3ec3bc21
BB
2974/*
2975 * Call when we think we're going to write/free space in open context to track
2976 * the amount of dirty data in the open txg, which is also the amount
2977 * of memory that can not be evicted until this txg syncs.
2978 */
2979void
2980dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
2981{
2982 dsl_dataset_t *ds = os->os_dsl_dataset;
2983 int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
2984
2985 if (ds != NULL) {
2986 dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
2987 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
2988 }
2989}
2990
93ce2b4c 2991#if defined(_KERNEL)
f0fd83be 2992EXPORT_SYMBOL(dmu_objset_zil);
c28b2279 2993EXPORT_SYMBOL(dmu_objset_pool);
f0fd83be
BB
2994EXPORT_SYMBOL(dmu_objset_ds);
2995EXPORT_SYMBOL(dmu_objset_type);
c28b2279
BB
2996EXPORT_SYMBOL(dmu_objset_name);
2997EXPORT_SYMBOL(dmu_objset_hold);
b5256303 2998EXPORT_SYMBOL(dmu_objset_hold_flags);
c28b2279
BB
2999EXPORT_SYMBOL(dmu_objset_own);
3000EXPORT_SYMBOL(dmu_objset_rele);
b5256303 3001EXPORT_SYMBOL(dmu_objset_rele_flags);
c28b2279
BB
3002EXPORT_SYMBOL(dmu_objset_disown);
3003EXPORT_SYMBOL(dmu_objset_from_ds);
3004EXPORT_SYMBOL(dmu_objset_create);
3005EXPORT_SYMBOL(dmu_objset_clone);
c28b2279
BB
3006EXPORT_SYMBOL(dmu_objset_stats);
3007EXPORT_SYMBOL(dmu_objset_fast_stat);
54a179e7 3008EXPORT_SYMBOL(dmu_objset_spa);
c28b2279
BB
3009EXPORT_SYMBOL(dmu_objset_space);
3010EXPORT_SYMBOL(dmu_objset_fsid_guid);
3011EXPORT_SYMBOL(dmu_objset_find);
c28b2279
BB
3012EXPORT_SYMBOL(dmu_objset_byteswap);
3013EXPORT_SYMBOL(dmu_objset_evict_dbufs);
3014EXPORT_SYMBOL(dmu_objset_snap_cmtime);
50c957f7 3015EXPORT_SYMBOL(dmu_objset_dnodesize);
c28b2279
BB
3016
3017EXPORT_SYMBOL(dmu_objset_sync);
3018EXPORT_SYMBOL(dmu_objset_is_dirty);
b5256303 3019EXPORT_SYMBOL(dmu_objset_create_impl_dnstats);
c28b2279
BB
3020EXPORT_SYMBOL(dmu_objset_create_impl);
3021EXPORT_SYMBOL(dmu_objset_open_impl);
3022EXPORT_SYMBOL(dmu_objset_evict);
3023EXPORT_SYMBOL(dmu_objset_register_type);
3024EXPORT_SYMBOL(dmu_objset_do_userquota_updates);
3025EXPORT_SYMBOL(dmu_objset_userquota_get_ids);
3026EXPORT_SYMBOL(dmu_objset_userused_enabled);
3027EXPORT_SYMBOL(dmu_objset_userspace_upgrade);
3028EXPORT_SYMBOL(dmu_objset_userspace_present);
1de321e6 3029EXPORT_SYMBOL(dmu_objset_userobjused_enabled);
126ae9f4 3030EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable);
1de321e6 3031EXPORT_SYMBOL(dmu_objset_userobjspace_present);
9c5167d1
NF
3032EXPORT_SYMBOL(dmu_objset_projectquota_enabled);
3033EXPORT_SYMBOL(dmu_objset_projectquota_present);
3034EXPORT_SYMBOL(dmu_objset_projectquota_upgradable);
3035EXPORT_SYMBOL(dmu_objset_id_quota_upgrade);
c28b2279 3036#endif