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