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