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