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