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