]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dmu_objset.c
Project Quota on ZFS
[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)) {
689 err = arc_untransform((*osp)->os_phys_buf, (*osp)->os_spa,
690 ds->ds_object, B_FALSE);
691 if (err != 0)
692 return (err);
693
694 ASSERT0(arc_is_unauthenticated((*osp)->os_phys_buf));
695 }
696
697 return (0);
9c43027b
AJ
698}
699
13fe0198
MA
700/*
701 * dsl_pool must not be held when this is called.
702 * Upon successful return, there will be a longhold on the dataset,
703 * and the dsl_pool will not be held.
704 */
34dc7c2f 705int
428870ff 706dmu_objset_own(const char *name, dmu_objset_type_t type,
b5256303 707 boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
34dc7c2f 708{
13fe0198 709 dsl_pool_t *dp;
34dc7c2f
BB
710 dsl_dataset_t *ds;
711 int err;
b5256303 712 ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0;
34dc7c2f 713
13fe0198
MA
714 err = dsl_pool_hold(name, FTAG, &dp);
715 if (err != 0)
716 return (err);
b5256303 717 err = dsl_dataset_own(dp, name, flags, tag, &ds);
13fe0198
MA
718 if (err != 0) {
719 dsl_pool_rele(dp, FTAG);
34dc7c2f 720 return (err);
13fe0198 721 }
b5256303
TC
722 err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp);
723 if (err != 0) {
724 dsl_dataset_disown(ds, flags, tag);
725 dsl_pool_rele(dp, FTAG);
726 return (err);
727 }
728
4807c0ba 729 /* user accounting requires the dataset to be decrypted */
9c5167d1
NF
730 if ((dmu_objset_userobjspace_upgradable(*osp) ||
731 dmu_objset_projectquota_upgradable(*osp)) &&
4807c0ba 732 (ds->ds_dir->dd_crypto_obj == 0 || decrypt))
9c5167d1 733 dmu_objset_id_quota_upgrade(*osp);
1de321e6 734
c0daec32 735 dsl_pool_rele(dp, FTAG);
b5256303 736 return (0);
34dc7c2f
BB
737}
738
9c43027b
AJ
739int
740dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
b5256303 741 boolean_t readonly, boolean_t decrypt, void *tag, objset_t **osp)
9c43027b
AJ
742{
743 dsl_dataset_t *ds;
744 int err;
b5256303 745 ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0;
9c43027b 746
b5256303 747 err = dsl_dataset_own_obj(dp, obj, flags, tag, &ds);
9c43027b
AJ
748 if (err != 0)
749 return (err);
750
b5256303
TC
751 err = dmu_objset_own_impl(ds, type, readonly, decrypt, tag, osp);
752 if (err != 0) {
753 dsl_dataset_disown(ds, flags, tag);
754 return (err);
755 }
756
757 return (0);
9c43027b
AJ
758}
759
34dc7c2f 760void
b5256303 761dmu_objset_rele_flags(objset_t *os, boolean_t decrypt, void *tag)
34dc7c2f 762{
b5256303
TC
763 ds_hold_flags_t flags = (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0;
764
13fe0198 765 dsl_pool_t *dp = dmu_objset_pool(os);
b5256303 766 dsl_dataset_rele_flags(os->os_dsl_dataset, flags, tag);
13fe0198 767 dsl_pool_rele(dp, tag);
428870ff 768}
b128c09f 769
b5256303
TC
770void
771dmu_objset_rele(objset_t *os, void *tag)
772{
773 dmu_objset_rele_flags(os, B_FALSE, tag);
774}
775
831baf06
KW
776/*
777 * When we are called, os MUST refer to an objset associated with a dataset
778 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
779 * == tag. We will then release and reacquire ownership of the dataset while
780 * holding the pool config_rwlock to avoid intervening namespace or ownership
781 * changes may occur.
782 *
783 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
784 * release the hold on its dataset and acquire a new one on the dataset of the
785 * same name so that it can be partially torn down and reconstructed.
786 */
787void
b5256303 788dmu_objset_refresh_ownership(objset_t *os, boolean_t decrypt, void *tag)
831baf06
KW
789{
790 dsl_pool_t *dp;
791 dsl_dataset_t *ds, *newds;
eca7b760 792 char name[ZFS_MAX_DATASET_NAME_LEN];
831baf06
KW
793
794 ds = os->os_dsl_dataset;
795 VERIFY3P(ds, !=, NULL);
796 VERIFY3P(ds->ds_owner, ==, tag);
797 VERIFY(dsl_dataset_long_held(ds));
798
799 dsl_dataset_name(ds, name);
800 dp = dmu_objset_pool(os);
801 dsl_pool_config_enter(dp, FTAG);
b5256303
TC
802 dmu_objset_disown(os, decrypt, tag);
803 VERIFY0(dsl_dataset_own(dp, name,
804 (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0, tag, &newds));
831baf06
KW
805 VERIFY3P(newds, ==, os->os_dsl_dataset);
806 dsl_pool_config_exit(dp, FTAG);
807}
808
428870ff 809void
b5256303 810dmu_objset_disown(objset_t *os, boolean_t decrypt, void *tag)
428870ff 811{
1de321e6
JX
812 /*
813 * Stop upgrading thread
814 */
815 dmu_objset_upgrade_stop(os);
b5256303
TC
816 dsl_dataset_disown(os->os_dsl_dataset,
817 (decrypt) ? DS_HOLD_FLAG_DECRYPT : 0, tag);
34dc7c2f
BB
818}
819
13fe0198 820void
34dc7c2f
BB
821dmu_objset_evict_dbufs(objset_t *os)
822{
0c66c32d 823 dnode_t *dn_marker;
34dc7c2f
BB
824 dnode_t *dn;
825
0c66c32d 826 dn_marker = kmem_alloc(sizeof (dnode_t), KM_SLEEP);
34dc7c2f 827
0c66c32d
JG
828 mutex_enter(&os->os_lock);
829 dn = list_head(&os->os_dnodes);
830 while (dn != NULL) {
831 /*
832 * Skip dnodes without holds. We have to do this dance
833 * because dnode_add_ref() only works if there is already a
834 * hold. If the dnode has no holds, then it has no dbufs.
835 */
836 if (dnode_add_ref(dn, FTAG)) {
837 list_insert_after(&os->os_dnodes, dn, dn_marker);
838 mutex_exit(&os->os_lock);
34dc7c2f 839
0c66c32d
JG
840 dnode_evict_dbufs(dn);
841 dnode_rele(dn, FTAG);
34dc7c2f 842
0c66c32d
JG
843 mutex_enter(&os->os_lock);
844 dn = list_next(&os->os_dnodes, dn_marker);
845 list_remove(&os->os_dnodes, dn_marker);
846 } else {
847 dn = list_next(&os->os_dnodes, dn);
848 }
849 }
850 mutex_exit(&os->os_lock);
34dc7c2f 851
0c66c32d 852 kmem_free(dn_marker, sizeof (dnode_t));
34dc7c2f 853
0c66c32d 854 if (DMU_USERUSED_DNODE(os) != NULL) {
9c5167d1
NF
855 if (DMU_PROJECTUSED_DNODE(os) != NULL)
856 dnode_evict_dbufs(DMU_PROJECTUSED_DNODE(os));
0c66c32d
JG
857 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
858 dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
34dc7c2f 859 }
0c66c32d 860 dnode_evict_dbufs(DMU_META_DNODE(os));
34dc7c2f
BB
861}
862
0c66c32d
JG
863/*
864 * Objset eviction processing is split into into two pieces.
865 * The first marks the objset as evicting, evicts any dbufs that
866 * have a refcount of zero, and then queues up the objset for the
867 * second phase of eviction. Once os->os_dnodes has been cleared by
868 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
869 * The second phase closes the special dnodes, dequeues the objset from
870 * the list of those undergoing eviction, and finally frees the objset.
871 *
872 * NOTE: Due to asynchronous eviction processing (invocation of
873 * dnode_buf_pageout()), it is possible for the meta dnode for the
874 * objset to have no holds even though os->os_dnodes is not empty.
875 */
34dc7c2f 876void
428870ff 877dmu_objset_evict(objset_t *os)
34dc7c2f 878{
6f1ffb06
MA
879 dsl_dataset_t *ds = os->os_dsl_dataset;
880
1c27024e 881 for (int t = 0; t < TXG_SIZE; t++)
428870ff 882 ASSERT(!dmu_objset_is_dirty(os, t));
34dc7c2f 883
0eb21616
JG
884 if (ds)
885 dsl_prop_unregister_all(ds, os);
34dc7c2f 886
428870ff
BB
887 if (os->os_sa)
888 sa_tear_down(os);
889
13fe0198 890 dmu_objset_evict_dbufs(os);
34dc7c2f 891
0c66c32d
JG
892 mutex_enter(&os->os_lock);
893 spa_evicting_os_register(os->os_spa, os);
894 if (list_is_empty(&os->os_dnodes)) {
895 mutex_exit(&os->os_lock);
896 dmu_objset_evict_done(os);
897 } else {
898 mutex_exit(&os->os_lock);
899 }
b5256303
TC
900
901
0c66c32d
JG
902}
903
904void
905dmu_objset_evict_done(objset_t *os)
906{
907 ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
908
572e2857
BB
909 dnode_special_close(&os->os_meta_dnode);
910 if (DMU_USERUSED_DNODE(os)) {
9c5167d1
NF
911 if (DMU_PROJECTUSED_DNODE(os))
912 dnode_special_close(&os->os_projectused_dnode);
572e2857
BB
913 dnode_special_close(&os->os_userused_dnode);
914 dnode_special_close(&os->os_groupused_dnode);
9babb374 915 }
428870ff
BB
916 zil_free(os->os_zil);
917
d3c2ae1c 918 arc_buf_destroy(os->os_phys_buf, &os->os_phys_buf);
572e2857
BB
919
920 /*
921 * This is a barrier to prevent the objset from going away in
922 * dnode_move() until we can safely ensure that the objset is still in
923 * use. We consider the objset valid before the barrier and invalid
924 * after the barrier.
925 */
926 rw_enter(&os_lock, RW_READER);
927 rw_exit(&os_lock);
928
dbeb8796
MA
929 kmem_free(os->os_obj_next_percpu,
930 os->os_obj_next_percpu_len * sizeof (os->os_obj_next_percpu[0]));
931
428870ff 932 mutex_destroy(&os->os_lock);
64fc7762 933 mutex_destroy(&os->os_userused_lock);
428870ff
BB
934 mutex_destroy(&os->os_obj_lock);
935 mutex_destroy(&os->os_user_ptr_lock);
c17486b2 936 mutex_destroy(&os->os_upgrade_lock);
64fc7762
MA
937 for (int i = 0; i < TXG_SIZE; i++) {
938 multilist_destroy(os->os_dirty_dnodes[i]);
939 }
0c66c32d 940 spa_evicting_os_deregister(os->os_spa, os);
428870ff
BB
941 kmem_free(os, sizeof (objset_t));
942}
9babb374 943
428870ff
BB
944timestruc_t
945dmu_objset_snap_cmtime(objset_t *os)
946{
947 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
34dc7c2f
BB
948}
949
428870ff 950objset_t *
b5256303
TC
951dmu_objset_create_impl_dnstats(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
952 dmu_objset_type_t type, int levels, int blksz, int ibs, dmu_tx_t *tx)
34dc7c2f 953{
428870ff 954 objset_t *os;
34dc7c2f
BB
955 dnode_t *mdn;
956
957 ASSERT(dmu_tx_is_syncing(tx));
13fe0198 958
b5256303
TC
959 if (blksz == 0)
960 blksz = DNODE_BLOCK_SIZE;
4807c0ba 961 if (ibs == 0)
b5256303
TC
962 ibs = DN_MAX_INDBLKSHIFT;
963
572e2857 964 if (ds != NULL)
13fe0198 965 VERIFY0(dmu_objset_from_ds(ds, &os));
572e2857 966 else
13fe0198 967 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
572e2857
BB
968
969 mdn = DMU_META_DNODE(os);
34dc7c2f 970
b5256303
TC
971 dnode_allocate(mdn, DMU_OT_DNODE, blksz, ibs, DMU_OT_NONE, 0,
972 DNODE_MIN_SLOTS, tx);
34dc7c2f
BB
973
974 /*
975 * We don't want to have to increase the meta-dnode's nlevels
976 * later, because then we could do it in quescing context while
977 * we are also accessing it in open context.
978 *
979 * This precaution is not necessary for the MOS (ds == NULL),
980 * because the MOS is only updated in syncing context.
981 * This is most fortunate: the MOS is the only objset that
982 * needs to be synced multiple times as spa_sync() iterates
983 * to convergence, so minimizing its dn_nlevels matters.
984 */
985 if (ds != NULL) {
b5256303
TC
986 if (levels == 0) {
987 levels = 1;
988
989 /*
990 * Determine the number of levels necessary for the
991 * meta-dnode to contain DN_MAX_OBJECT dnodes. Note
992 * that in order to ensure that we do not overflow
993 * 64 bits, there has to be a nlevels that gives us a
994 * number of blocks > DN_MAX_OBJECT but < 2^64.
995 * Therefore, (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)
996 * (10) must be less than (64 - log2(DN_MAX_OBJECT))
997 * (16).
998 */
999 while ((uint64_t)mdn->dn_nblkptr <<
1000 (mdn->dn_datablkshift - DNODE_SHIFT + (levels - 1) *
1001 (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
1002 DN_MAX_OBJECT)
1003 levels++;
1004 }
34dc7c2f
BB
1005
1006 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
1007 mdn->dn_nlevels = levels;
1008 }
1009
1010 ASSERT(type != DMU_OST_NONE);
1011 ASSERT(type != DMU_OST_ANY);
1012 ASSERT(type < DMU_OST_NUMTYPES);
428870ff 1013 os->os_phys->os_type = type;
b5256303
TC
1014
1015 /*
1016 * Enable user accounting if it is enabled and this is not an
1017 * encrypted receive.
1018 */
1019 if (dmu_objset_userused_enabled(os) &&
1020 (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
428870ff 1021 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1de321e6
JX
1022 if (dmu_objset_userobjused_enabled(os)) {
1023 ds->ds_feature_activation_needed[
1024 SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE;
1025 os->os_phys->os_flags |=
1026 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
1027 }
9c5167d1
NF
1028 if (dmu_objset_projectquota_enabled(os)) {
1029 ds->ds_feature_activation_needed[
1030 SPA_FEATURE_PROJECT_QUOTA] = B_TRUE;
1031 os->os_phys->os_flags |=
1032 OBJSET_FLAG_PROJECTQUOTA_COMPLETE;
1033 }
428870ff 1034 os->os_flags = os->os_phys->os_flags;
9babb374 1035 }
34dc7c2f
BB
1036
1037 dsl_dataset_dirty(ds, tx);
1038
428870ff 1039 return (os);
34dc7c2f
BB
1040}
1041
b5256303
TC
1042/* called from dsl for meta-objset */
1043objset_t *
1044dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
1045 dmu_objset_type_t type, dmu_tx_t *tx)
1046{
1047 return (dmu_objset_create_impl_dnstats(spa, ds, bp, type, 0, 0, 0, tx));
1048}
1049
13fe0198
MA
1050typedef struct dmu_objset_create_arg {
1051 const char *doca_name;
1052 cred_t *doca_cred;
1053 void (*doca_userfunc)(objset_t *os, void *arg,
1054 cred_t *cr, dmu_tx_t *tx);
1055 void *doca_userarg;
1056 dmu_objset_type_t doca_type;
1057 uint64_t doca_flags;
b5256303 1058 dsl_crypto_params_t *doca_dcp;
13fe0198 1059} dmu_objset_create_arg_t;
34dc7c2f
BB
1060
1061/*ARGSUSED*/
1062static int
13fe0198 1063dmu_objset_create_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1064{
13fe0198
MA
1065 dmu_objset_create_arg_t *doca = arg;
1066 dsl_pool_t *dp = dmu_tx_pool(tx);
1067 dsl_dir_t *pdd;
1068 const char *tail;
1069 int error;
34dc7c2f 1070
13fe0198 1071 if (strchr(doca->doca_name, '@') != NULL)
2e528b49 1072 return (SET_ERROR(EINVAL));
34dc7c2f 1073
eca7b760
IK
1074 if (strlen(doca->doca_name) >= ZFS_MAX_DATASET_NAME_LEN)
1075 return (SET_ERROR(ENAMETOOLONG));
1076
13fe0198
MA
1077 error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
1078 if (error != 0)
1079 return (error);
1080 if (tail == NULL) {
1081 dsl_dir_rele(pdd, FTAG);
2e528b49 1082 return (SET_ERROR(EEXIST));
34dc7c2f 1083 }
b5256303
TC
1084
1085 error = dmu_objset_create_crypt_check(pdd, doca->doca_dcp);
1086 if (error != 0) {
1087 dsl_dir_rele(pdd, FTAG);
1088 return (error);
1089 }
1090
788eb90c
JJ
1091 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1092 doca->doca_cred);
b5256303 1093
13fe0198 1094 dsl_dir_rele(pdd, FTAG);
34dc7c2f 1095
788eb90c 1096 return (error);
34dc7c2f
BB
1097}
1098
1099static void
13fe0198 1100dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 1101{
13fe0198
MA
1102 dmu_objset_create_arg_t *doca = arg;
1103 dsl_pool_t *dp = dmu_tx_pool(tx);
1104 dsl_dir_t *pdd;
1105 const char *tail;
6f1ffb06 1106 dsl_dataset_t *ds;
13fe0198 1107 uint64_t obj;
6f1ffb06 1108 blkptr_t *bp;
13fe0198 1109 objset_t *os;
b5256303 1110 zio_t *rzio;
34dc7c2f 1111
13fe0198 1112 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
34dc7c2f 1113
13fe0198 1114 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
b5256303 1115 doca->doca_cred, doca->doca_dcp, tx);
34dc7c2f 1116
b5256303
TC
1117 VERIFY0(dsl_dataset_hold_obj_flags(pdd->dd_pool, obj,
1118 DS_HOLD_FLAG_DECRYPT, FTAG, &ds));
cc9bb3e5 1119 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
6f1ffb06 1120 bp = dsl_dataset_get_blkptr(ds);
13fe0198
MA
1121 os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
1122 ds, bp, doca->doca_type, tx);
cc9bb3e5 1123 rrw_exit(&ds->ds_bp_rwlock, FTAG);
34dc7c2f 1124
13fe0198
MA
1125 if (doca->doca_userfunc != NULL) {
1126 doca->doca_userfunc(os, doca->doca_userarg,
1127 doca->doca_cred, tx);
34dc7c2f
BB
1128 }
1129
b5256303 1130 /*
4807c0ba 1131 * The doca_userfunc() may write out some data that needs to be
b5256303
TC
1132 * encrypted if the dataset is encrypted (specifically the root
1133 * directory). This data must be written out before the encryption
1134 * key mapping is removed by dsl_dataset_rele_flags(). Force the
1135 * I/O to occur immediately by invoking the relevant sections of
1136 * dsl_pool_sync().
1137 */
1138 if (os->os_encrypted) {
1139 dsl_dataset_t *tmpds = NULL;
1140 boolean_t need_sync_done = B_FALSE;
1141
4807c0ba
TC
1142 mutex_enter(&ds->ds_lock);
1143 ds->ds_owner = FTAG;
1144 mutex_exit(&ds->ds_lock);
1145
b5256303 1146 rzio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
4807c0ba
TC
1147 tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds,
1148 tx->tx_txg);
b5256303 1149 if (tmpds != NULL) {
b5256303
TC
1150 dsl_dataset_sync(ds, rzio, tx);
1151 need_sync_done = B_TRUE;
1152 }
1153 VERIFY0(zio_wait(rzio));
1154
1155 dmu_objset_do_userquota_updates(os, tx);
1156 taskq_wait(dp->dp_sync_taskq);
1157
1158 rzio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
4807c0ba
TC
1159 tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds,
1160 tx->tx_txg);
b5256303 1161 if (tmpds != NULL) {
b5256303
TC
1162 dmu_buf_rele(ds->ds_dbuf, ds);
1163 dsl_dataset_sync(ds, rzio, tx);
1164 }
1165 VERIFY0(zio_wait(rzio));
1166
1167 if (need_sync_done)
1168 dsl_dataset_sync_done(ds, tx);
4807c0ba
TC
1169
1170 mutex_enter(&ds->ds_lock);
1171 ds->ds_owner = NULL;
1172 mutex_exit(&ds->ds_lock);
b5256303
TC
1173 }
1174
13fe0198 1175 spa_history_log_internal_ds(ds, "create", tx, "");
a0bd735a
BP
1176 zvol_create_minors(dp->dp_spa, doca->doca_name, B_TRUE);
1177
b5256303 1178 dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
13fe0198 1179 dsl_dir_rele(pdd, FTAG);
34dc7c2f
BB
1180}
1181
1182int
428870ff 1183dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
b5256303 1184 dsl_crypto_params_t *dcp, dmu_objset_create_sync_func_t func, void *arg)
34dc7c2f 1185{
13fe0198 1186 dmu_objset_create_arg_t doca;
b5256303 1187 dsl_crypto_params_t tmp_dcp = { 0 };
34dc7c2f 1188
13fe0198
MA
1189 doca.doca_name = name;
1190 doca.doca_cred = CRED();
1191 doca.doca_flags = flags;
1192 doca.doca_userfunc = func;
1193 doca.doca_userarg = arg;
1194 doca.doca_type = type;
34dc7c2f 1195
b5256303
TC
1196 /*
1197 * Some callers (mostly for testing) do not provide a dcp on their
1198 * own but various code inside the sync task will require it to be
1199 * allocated. Rather than adding NULL checks throughout this code
1200 * or adding dummy dcp's to all of the callers we simply create a
1201 * dummy one here and use that. This zero dcp will have the same
1202 * effect as asking for inheritence of all encryption params.
1203 */
1204 doca.doca_dcp = (dcp != NULL) ? dcp : &tmp_dcp;
1205
13fe0198 1206 return (dsl_sync_task(name,
3d45fdd6 1207 dmu_objset_create_check, dmu_objset_create_sync, &doca,
b5256303 1208 6, ZFS_SPACE_CHECK_NORMAL));
34dc7c2f
BB
1209}
1210
13fe0198
MA
1211typedef struct dmu_objset_clone_arg {
1212 const char *doca_clone;
1213 const char *doca_origin;
1214 cred_t *doca_cred;
1215} dmu_objset_clone_arg_t;
1216
1217/*ARGSUSED*/
1218static int
1219dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1220{
13fe0198 1221 dmu_objset_clone_arg_t *doca = arg;
428870ff
BB
1222 dsl_dir_t *pdd;
1223 const char *tail;
13fe0198
MA
1224 int error;
1225 dsl_dataset_t *origin;
1226 dsl_pool_t *dp = dmu_tx_pool(tx);
34dc7c2f 1227
13fe0198 1228 if (strchr(doca->doca_clone, '@') != NULL)
2e528b49 1229 return (SET_ERROR(EINVAL));
13fe0198 1230
eca7b760
IK
1231 if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
1232 return (SET_ERROR(ENAMETOOLONG));
1233
13fe0198
MA
1234 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
1235 if (error != 0)
1236 return (error);
428870ff 1237 if (tail == NULL) {
13fe0198 1238 dsl_dir_rele(pdd, FTAG);
2e528b49 1239 return (SET_ERROR(EEXIST));
34dc7c2f 1240 }
1cddb8c9 1241
788eb90c
JJ
1242 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1243 doca->doca_cred);
1244 if (error != 0) {
1245 dsl_dir_rele(pdd, FTAG);
1246 return (SET_ERROR(EDQUOT));
1247 }
428870ff 1248
13fe0198 1249 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
b5256303
TC
1250 if (error != 0) {
1251 dsl_dir_rele(pdd, FTAG);
572e2857 1252 return (error);
b5256303 1253 }
572e2857 1254
13fe0198 1255 /* You can only clone snapshots, not the head datasets. */
0c66c32d 1256 if (!origin->ds_is_snapshot) {
13fe0198 1257 dsl_dataset_rele(origin, FTAG);
b5256303 1258 dsl_dir_rele(pdd, FTAG);
2e528b49 1259 return (SET_ERROR(EINVAL));
428870ff 1260 }
b5256303
TC
1261
1262 error = dmu_objset_clone_crypt_check(pdd, origin->ds_dir);
1263 if (error != 0) {
1264 dsl_dataset_rele(origin, FTAG);
1265 dsl_dir_rele(pdd, FTAG);
1266 return (error);
1267 }
1268
13fe0198 1269 dsl_dataset_rele(origin, FTAG);
b5256303 1270 dsl_dir_rele(pdd, FTAG);
572e2857 1271
13fe0198 1272 return (0);
9babb374 1273}
34dc7c2f 1274
13fe0198
MA
1275static void
1276dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 1277{
13fe0198
MA
1278 dmu_objset_clone_arg_t *doca = arg;
1279 dsl_pool_t *dp = dmu_tx_pool(tx);
1280 dsl_dir_t *pdd;
1281 const char *tail;
1282 dsl_dataset_t *origin, *ds;
1283 uint64_t obj;
eca7b760 1284 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
6f1ffb06 1285
13fe0198
MA
1286 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
1287 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
6f1ffb06 1288
13fe0198 1289 obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
b5256303 1290 doca->doca_cred, NULL, tx);
34dc7c2f 1291
13fe0198
MA
1292 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
1293 dsl_dataset_name(origin, namebuf);
1294 spa_history_log_internal_ds(ds, "clone", tx,
1295 "origin=%s (%llu)", namebuf, origin->ds_object);
a0bd735a 1296 zvol_create_minors(dp->dp_spa, doca->doca_clone, B_TRUE);
13fe0198
MA
1297 dsl_dataset_rele(ds, FTAG);
1298 dsl_dataset_rele(origin, FTAG);
1299 dsl_dir_rele(pdd, FTAG);
34dc7c2f
BB
1300}
1301
1302int
13fe0198 1303dmu_objset_clone(const char *clone, const char *origin)
34dc7c2f 1304{
13fe0198 1305 dmu_objset_clone_arg_t doca;
34dc7c2f 1306
13fe0198
MA
1307 doca.doca_clone = clone;
1308 doca.doca_origin = origin;
1309 doca.doca_cred = CRED();
572e2857 1310
13fe0198 1311 return (dsl_sync_task(clone,
3d45fdd6 1312 dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
b5256303 1313 6, ZFS_SPACE_CHECK_NORMAL));
6f1ffb06
MA
1314}
1315
1316int
1317dmu_objset_snapshot_one(const char *fsname, const char *snapname)
1318{
1319 int err;
1320 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1321 nvlist_t *snaps = fnvlist_alloc();
1322
1323 fnvlist_add_boolean(snaps, longsnap);
6f1ffb06 1324 strfree(longsnap);
13fe0198
MA
1325 err = dsl_dataset_snapshot(snaps, NULL, NULL);
1326 fnvlist_free(snaps);
6f1ffb06
MA
1327 return (err);
1328}
1329
1de321e6
JX
1330static void
1331dmu_objset_upgrade_task_cb(void *data)
1332{
1333 objset_t *os = data;
1334
1335 mutex_enter(&os->os_upgrade_lock);
1336 os->os_upgrade_status = EINTR;
1337 if (!os->os_upgrade_exit) {
1338 mutex_exit(&os->os_upgrade_lock);
1339
1340 os->os_upgrade_status = os->os_upgrade_cb(os);
1341 mutex_enter(&os->os_upgrade_lock);
1342 }
1343 os->os_upgrade_exit = B_TRUE;
1344 os->os_upgrade_id = 0;
1345 mutex_exit(&os->os_upgrade_lock);
c0daec32 1346 dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1de321e6
JX
1347}
1348
1349static void
1350dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb)
1351{
1352 if (os->os_upgrade_id != 0)
1353 return;
1354
c0daec32
AB
1355 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1356 dsl_dataset_long_hold(dmu_objset_ds(os), upgrade_tag);
1357
1de321e6
JX
1358 mutex_enter(&os->os_upgrade_lock);
1359 if (os->os_upgrade_id == 0 && os->os_upgrade_status == 0) {
1360 os->os_upgrade_exit = B_FALSE;
1361 os->os_upgrade_cb = cb;
1362 os->os_upgrade_id = taskq_dispatch(
1363 os->os_spa->spa_upgrade_taskq,
1364 dmu_objset_upgrade_task_cb, os, TQ_SLEEP);
c0daec32
AB
1365 if (os->os_upgrade_id == TASKQID_INVALID) {
1366 dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1de321e6 1367 os->os_upgrade_status = ENOMEM;
c0daec32 1368 }
1de321e6
JX
1369 }
1370 mutex_exit(&os->os_upgrade_lock);
1371}
1372
1373static void
1374dmu_objset_upgrade_stop(objset_t *os)
1375{
1376 mutex_enter(&os->os_upgrade_lock);
1377 os->os_upgrade_exit = B_TRUE;
1378 if (os->os_upgrade_id != 0) {
1379 taskqid_t id = os->os_upgrade_id;
1380
1381 os->os_upgrade_id = 0;
1382 mutex_exit(&os->os_upgrade_lock);
1383
c0daec32
AB
1384 if ((taskq_cancel_id(os->os_spa->spa_upgrade_taskq, id)) == 0) {
1385 dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1386 }
4807c0ba 1387 txg_wait_synced(os->os_spa->spa_dsl_pool, 0);
1de321e6
JX
1388 } else {
1389 mutex_exit(&os->os_upgrade_lock);
1390 }
1391}
1392
34dc7c2f 1393static void
64fc7762 1394dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx)
34dc7c2f
BB
1395{
1396 dnode_t *dn;
1397
64fc7762 1398 while ((dn = multilist_sublist_head(list)) != NULL) {
34dc7c2f
BB
1399 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1400 ASSERT(dn->dn_dbuf->db_data_pending);
1401 /*
9babb374
BB
1402 * Initialize dn_zio outside dnode_sync() because the
1403 * meta-dnode needs to set it ouside dnode_sync().
34dc7c2f
BB
1404 */
1405 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1406 ASSERT(dn->dn_zio);
1407
1408 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
64fc7762 1409 multilist_sublist_remove(list, dn);
9babb374 1410
64fc7762
MA
1411 multilist_t *newlist = dn->dn_objset->os_synced_dnodes;
1412 if (newlist != NULL) {
9babb374 1413 (void) dnode_add_ref(dn, newlist);
64fc7762 1414 multilist_insert(newlist, dn);
9babb374
BB
1415 }
1416
34dc7c2f
BB
1417 dnode_sync(dn, tx);
1418 }
1419}
1420
1421/* ARGSUSED */
1422static void
428870ff 1423dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
34dc7c2f 1424{
b128c09f 1425 blkptr_t *bp = zio->io_bp;
428870ff 1426 objset_t *os = arg;
34dc7c2f 1427 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
b5256303 1428 uint64_t fill = 0;
34dc7c2f 1429
9b67f605 1430 ASSERT(!BP_IS_EMBEDDED(bp));
13fe0198
MA
1431 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1432 ASSERT0(BP_GET_LEVEL(bp));
34dc7c2f
BB
1433
1434 /*
9babb374
BB
1435 * Update rootbp fill count: it should be the number of objects
1436 * allocated in the object set (not counting the "special"
1437 * objects that are stored in the objset_phys_t -- the meta
9c5167d1 1438 * dnode and user/group/project accounting objects).
34dc7c2f 1439 */
1c27024e 1440 for (int i = 0; i < dnp->dn_nblkptr; i++)
b5256303
TC
1441 fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1442
1443 BP_SET_FILL(bp, fill);
1444
cc9bb3e5
GM
1445 if (os->os_dsl_dataset != NULL)
1446 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1447 *os->os_rootbp = *bp;
1448 if (os->os_dsl_dataset != NULL)
1449 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
428870ff
BB
1450}
1451
1452/* ARGSUSED */
1453static void
1454dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1455{
1456 blkptr_t *bp = zio->io_bp;
1457 blkptr_t *bp_orig = &zio->io_bp_orig;
1458 objset_t *os = arg;
34dc7c2f 1459
b128c09f 1460 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
428870ff 1461 ASSERT(BP_EQUAL(bp, bp_orig));
b128c09f 1462 } else {
428870ff
BB
1463 dsl_dataset_t *ds = os->os_dsl_dataset;
1464 dmu_tx_t *tx = os->os_synctx;
1465
1466 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1467 dsl_dataset_block_born(ds, bp, tx);
34dc7c2f 1468 }
cc9bb3e5 1469 kmem_free(bp, sizeof (*bp));
34dc7c2f
BB
1470}
1471
64fc7762
MA
1472typedef struct sync_dnodes_arg {
1473 multilist_t *sda_list;
1474 int sda_sublist_idx;
1475 multilist_t *sda_newlist;
1476 dmu_tx_t *sda_tx;
1477} sync_dnodes_arg_t;
1478
1479static void
1480sync_dnodes_task(void *arg)
1481{
1482 sync_dnodes_arg_t *sda = arg;
1483
1484 multilist_sublist_t *ms =
1485 multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
1486
1487 dmu_objset_sync_dnodes(ms, sda->sda_tx);
1488
1489 multilist_sublist_unlock(ms);
1490
1491 kmem_free(sda, sizeof (*sda));
1492}
1493
1494
34dc7c2f
BB
1495/* called from dsl */
1496void
428870ff 1497dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
34dc7c2f
BB
1498{
1499 int txgoff;
5dbd68a3 1500 zbookmark_phys_t zb;
428870ff 1501 zio_prop_t zp;
34dc7c2f
BB
1502 zio_t *zio;
1503 list_t *list;
1504 dbuf_dirty_record_t *dr;
cc9bb3e5
GM
1505 blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1506 *blkptr_copy = *os->os_rootbp;
34dc7c2f
BB
1507
1508 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1509
1510 ASSERT(dmu_tx_is_syncing(tx));
1511 /* XXX the write_done callback should really give us the tx... */
1512 os->os_synctx = tx;
1513
1514 if (os->os_dsl_dataset == NULL) {
1515 /*
1516 * This is the MOS. If we have upgraded,
1517 * spa_max_replication() could change, so reset
1518 * os_copies here.
1519 */
1520 os->os_copies = spa_max_replication(os->os_spa);
1521 }
1522
1523 /*
1524 * Create the root block IO
1525 */
428870ff
BB
1526 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1527 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1528 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
294f6806 1529 arc_release(os->os_phys_buf, &os->os_phys_buf);
b128c09f 1530
82644107 1531 dmu_write_policy(os, NULL, 0, 0, &zp);
9babb374 1532
b5256303
TC
1533 /*
1534 * If we are either claiming the ZIL or doing a raw receive write out
1535 * the os_phys_buf raw. Neither of these actions will effect the MAC
1536 * at this point.
1537 */
1b66810b 1538 if (os->os_next_write_raw[tx->tx_txg & TXG_MASK]) {
b5256303 1539 ASSERT(os->os_encrypted);
1b66810b 1540 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_FALSE;
b5256303
TC
1541 arc_convert_to_raw(os->os_phys_buf,
1542 os->os_dsl_dataset->ds_object, ZFS_HOST_BYTEORDER,
1543 DMU_OT_OBJSET, NULL, NULL, NULL);
1544 }
1545
428870ff 1546 zio = arc_write(pio, os->os_spa, tx->tx_txg,
cc9bb3e5 1547 blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
bc77ba73
PD
1548 &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
1549 os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
34dc7c2f
BB
1550
1551 /*
9babb374 1552 * Sync special dnodes - the parent IO for the sync is the root block
34dc7c2f 1553 */
572e2857
BB
1554 DMU_META_DNODE(os)->dn_zio = zio;
1555 dnode_sync(DMU_META_DNODE(os), tx);
34dc7c2f 1556
9babb374
BB
1557 os->os_phys->os_flags = os->os_flags;
1558
572e2857
BB
1559 if (DMU_USERUSED_DNODE(os) &&
1560 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1561 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1562 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1563 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1564 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
9babb374
BB
1565 }
1566
9c5167d1
NF
1567 if (DMU_PROJECTUSED_DNODE(os) &&
1568 DMU_PROJECTUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1569 DMU_PROJECTUSED_DNODE(os)->dn_zio = zio;
1570 dnode_sync(DMU_PROJECTUSED_DNODE(os), tx);
1571 }
1572
34dc7c2f
BB
1573 txgoff = tx->tx_txg & TXG_MASK;
1574
b5256303
TC
1575 if (dmu_objset_userused_enabled(os) &&
1576 (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
9babb374
BB
1577 /*
1578 * We must create the list here because it uses the
64fc7762
MA
1579 * dn_dirty_link[] of this txg. But it may already
1580 * exist because we call dsl_dataset_sync() twice per txg.
9babb374 1581 */
64fc7762
MA
1582 if (os->os_synced_dnodes == NULL) {
1583 os->os_synced_dnodes =
1584 multilist_create(sizeof (dnode_t),
1585 offsetof(dnode_t, dn_dirty_link[txgoff]),
1586 dnode_multilist_index_func);
1587 } else {
1588 ASSERT3U(os->os_synced_dnodes->ml_offset, ==,
1589 offsetof(dnode_t, dn_dirty_link[txgoff]));
1590 }
9babb374
BB
1591 }
1592
64fc7762
MA
1593 for (int i = 0;
1594 i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) {
1595 sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
1596 sda->sda_list = os->os_dirty_dnodes[txgoff];
1597 sda->sda_sublist_idx = i;
1598 sda->sda_tx = tx;
1599 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1600 sync_dnodes_task, sda, 0);
1601 /* callback frees sda */
1602 }
1603 taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
34dc7c2f 1604
572e2857 1605 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
64fc7762 1606 while ((dr = list_head(list)) != NULL) {
13fe0198 1607 ASSERT0(dr->dr_dbuf->db_level);
34dc7c2f
BB
1608 list_remove(list, dr);
1609 if (dr->dr_zio)
1610 zio_nowait(dr->dr_zio);
1611 }
68cbd56e
NB
1612
1613 /* Enable dnode backfill if enough objects have been freed. */
1614 if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1615 os->os_rescan_dnodes = B_TRUE;
1616 os->os_freed_dnodes = 0;
1617 }
1618
34dc7c2f
BB
1619 /*
1620 * Free intent log blocks up to this tx.
1621 */
1622 zil_sync(os->os_zil, tx);
b128c09f 1623 os->os_phys->os_zil_header = os->os_zil_header;
34dc7c2f
BB
1624 zio_nowait(zio);
1625}
1626
428870ff
BB
1627boolean_t
1628dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1629{
64fc7762 1630 return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK]));
428870ff
BB
1631}
1632
572e2857 1633static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
9babb374
BB
1634
1635void
1636dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1637{
1638 used_cbs[ost] = cb;
1639}
1640
1641boolean_t
428870ff 1642dmu_objset_userused_enabled(objset_t *os)
9babb374
BB
1643{
1644 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
572e2857
BB
1645 used_cbs[os->os_phys->os_type] != NULL &&
1646 DMU_USERUSED_DNODE(os) != NULL);
9babb374
BB
1647}
1648
1de321e6
JX
1649boolean_t
1650dmu_objset_userobjused_enabled(objset_t *os)
1651{
1652 return (dmu_objset_userused_enabled(os) &&
1653 spa_feature_is_enabled(os->os_spa, SPA_FEATURE_USEROBJ_ACCOUNTING));
1654}
1655
9c5167d1
NF
1656boolean_t
1657dmu_objset_projectquota_enabled(objset_t *os)
1658{
1659 return (used_cbs[os->os_phys->os_type] != NULL &&
1660 DMU_PROJECTUSED_DNODE(os) != NULL &&
1661 spa_feature_is_enabled(os->os_spa, SPA_FEATURE_PROJECT_QUOTA));
1662}
1663
9b7a83cb
JX
1664typedef struct userquota_node {
1665 /* must be in the first filed, see userquota_update_cache() */
1666 char uqn_id[20 + DMU_OBJACCT_PREFIX_LEN];
1667 int64_t uqn_delta;
1668 avl_node_t uqn_node;
1669} userquota_node_t;
1670
1671typedef struct userquota_cache {
1672 avl_tree_t uqc_user_deltas;
1673 avl_tree_t uqc_group_deltas;
9c5167d1 1674 avl_tree_t uqc_project_deltas;
9b7a83cb
JX
1675} userquota_cache_t;
1676
1677static int
1678userquota_compare(const void *l, const void *r)
1679{
1680 const userquota_node_t *luqn = l;
1681 const userquota_node_t *ruqn = r;
e4ffa98d 1682 int rv;
9b7a83cb
JX
1683
1684 /*
1685 * NB: can only access uqn_id because userquota_update_cache() doesn't
1686 * pass in an entire userquota_node_t.
1687 */
e4ffa98d
BB
1688 rv = strcmp(luqn->uqn_id, ruqn->uqn_id);
1689
1690 return (AVL_ISIGN(rv));
9b7a83cb
JX
1691}
1692
1693static void
1694do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
1695{
1696 void *cookie;
1697 userquota_node_t *uqn;
1698
1699 ASSERT(dmu_tx_is_syncing(tx));
1700
1701 cookie = NULL;
1702 while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
1703 &cookie)) != NULL) {
64fc7762
MA
1704 /*
1705 * os_userused_lock protects against concurrent calls to
1706 * zap_increment_int(). It's needed because zap_increment_int()
1707 * is not thread-safe (i.e. not atomic).
1708 */
1709 mutex_enter(&os->os_userused_lock);
9b7a83cb
JX
1710 VERIFY0(zap_increment(os, DMU_USERUSED_OBJECT,
1711 uqn->uqn_id, uqn->uqn_delta, tx));
64fc7762 1712 mutex_exit(&os->os_userused_lock);
9b7a83cb
JX
1713 kmem_free(uqn, sizeof (*uqn));
1714 }
1715 avl_destroy(&cache->uqc_user_deltas);
1716
1717 cookie = NULL;
1718 while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
1719 &cookie)) != NULL) {
64fc7762 1720 mutex_enter(&os->os_userused_lock);
9b7a83cb
JX
1721 VERIFY0(zap_increment(os, DMU_GROUPUSED_OBJECT,
1722 uqn->uqn_id, uqn->uqn_delta, tx));
64fc7762 1723 mutex_exit(&os->os_userused_lock);
9b7a83cb
JX
1724 kmem_free(uqn, sizeof (*uqn));
1725 }
1726 avl_destroy(&cache->uqc_group_deltas);
9c5167d1
NF
1727
1728 if (dmu_objset_projectquota_enabled(os)) {
1729 cookie = NULL;
1730 while ((uqn = avl_destroy_nodes(&cache->uqc_project_deltas,
1731 &cookie)) != NULL) {
1732 mutex_enter(&os->os_userused_lock);
1733 VERIFY0(zap_increment(os, DMU_PROJECTUSED_OBJECT,
1734 uqn->uqn_id, uqn->uqn_delta, tx));
1735 mutex_exit(&os->os_userused_lock);
1736 kmem_free(uqn, sizeof (*uqn));
1737 }
1738 avl_destroy(&cache->uqc_project_deltas);
1739 }
9b7a83cb
JX
1740}
1741
1742static void
1743userquota_update_cache(avl_tree_t *avl, const char *id, int64_t delta)
1744{
1745 userquota_node_t *uqn;
1746 avl_index_t idx;
1747
1748 ASSERT(strlen(id) < sizeof (uqn->uqn_id));
1749 /*
1750 * Use id directly for searching because uqn_id is the first field of
1751 * userquota_node_t and fields after uqn_id won't be accessed in
1752 * avl_find().
1753 */
1754 uqn = avl_find(avl, (const void *)id, &idx);
1755 if (uqn == NULL) {
1756 uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
1f51b525 1757 strlcpy(uqn->uqn_id, id, sizeof (uqn->uqn_id));
9b7a83cb
JX
1758 avl_insert(avl, uqn, idx);
1759 }
1760 uqn->uqn_delta += delta;
1761}
1762
428870ff 1763static void
9c5167d1
NF
1764do_userquota_update(objset_t *os, userquota_cache_t *cache, uint64_t used,
1765 uint64_t flags, uint64_t user, uint64_t group, uint64_t project,
1766 boolean_t subtract)
428870ff 1767{
9c5167d1 1768 if (flags & DNODE_FLAG_USERUSED_ACCOUNTED) {
50c957f7 1769 int64_t delta = DNODE_MIN_SIZE + used;
9b7a83cb
JX
1770 char name[20];
1771
428870ff
BB
1772 if (subtract)
1773 delta = -delta;
9b7a83cb
JX
1774
1775 (void) sprintf(name, "%llx", (longlong_t)user);
1776 userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1777
1778 (void) sprintf(name, "%llx", (longlong_t)group);
1779 userquota_update_cache(&cache->uqc_group_deltas, name, delta);
9c5167d1
NF
1780
1781 if (dmu_objset_projectquota_enabled(os)) {
1782 (void) sprintf(name, "%llx", (longlong_t)project);
1783 userquota_update_cache(&cache->uqc_project_deltas,
1784 name, delta);
1785 }
428870ff
BB
1786 }
1787}
1788
1de321e6 1789static void
9c5167d1
NF
1790do_userobjquota_update(objset_t *os, userquota_cache_t *cache, uint64_t flags,
1791 uint64_t user, uint64_t group, uint64_t project, boolean_t subtract)
1de321e6
JX
1792{
1793 if (flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) {
1794 char name[20 + DMU_OBJACCT_PREFIX_LEN];
9b7a83cb 1795 int delta = subtract ? -1 : 1;
1de321e6
JX
1796
1797 (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1798 (longlong_t)user);
9b7a83cb 1799 userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1de321e6
JX
1800
1801 (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1802 (longlong_t)group);
9b7a83cb 1803 userquota_update_cache(&cache->uqc_group_deltas, name, delta);
9c5167d1
NF
1804
1805 if (dmu_objset_projectquota_enabled(os)) {
1806 (void) snprintf(name, sizeof (name),
1807 DMU_OBJACCT_PREFIX "%llx", (longlong_t)project);
1808 userquota_update_cache(&cache->uqc_project_deltas,
1809 name, delta);
1810 }
1de321e6
JX
1811 }
1812}
1813
64fc7762
MA
1814typedef struct userquota_updates_arg {
1815 objset_t *uua_os;
1816 int uua_sublist_idx;
1817 dmu_tx_t *uua_tx;
1818} userquota_updates_arg_t;
1819
1820static void
1821userquota_updates_task(void *arg)
9babb374 1822{
64fc7762
MA
1823 userquota_updates_arg_t *uua = arg;
1824 objset_t *os = uua->uua_os;
1825 dmu_tx_t *tx = uua->uua_tx;
9babb374 1826 dnode_t *dn;
9b7a83cb 1827 userquota_cache_t cache = { { 0 } };
9babb374 1828
64fc7762
MA
1829 multilist_sublist_t *list =
1830 multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx);
9babb374 1831
64fc7762
MA
1832 ASSERT(multilist_sublist_head(list) == NULL ||
1833 dmu_objset_userused_enabled(os));
9b7a83cb
JX
1834 avl_create(&cache.uqc_user_deltas, userquota_compare,
1835 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
1836 avl_create(&cache.uqc_group_deltas, userquota_compare,
1837 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
9c5167d1
NF
1838 if (dmu_objset_projectquota_enabled(os))
1839 avl_create(&cache.uqc_project_deltas, userquota_compare,
1840 sizeof (userquota_node_t), offsetof(userquota_node_t,
1841 uqn_node));
9b7a83cb 1842
64fc7762 1843 while ((dn = multilist_sublist_head(list)) != NULL) {
572e2857 1844 int flags;
9babb374 1845 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
9babb374
BB
1846 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1847 dn->dn_phys->dn_flags &
1848 DNODE_FLAG_USERUSED_ACCOUNTED);
1849
572e2857
BB
1850 flags = dn->dn_id_flags;
1851 ASSERT(flags);
1852 if (flags & DN_ID_OLD_EXIST) {
9c5167d1
NF
1853 do_userquota_update(os, &cache, dn->dn_oldused,
1854 dn->dn_oldflags, dn->dn_olduid, dn->dn_oldgid,
1855 dn->dn_oldprojid, B_TRUE);
1856 do_userobjquota_update(os, &cache, dn->dn_oldflags,
1857 dn->dn_olduid, dn->dn_oldgid,
1858 dn->dn_oldprojid, B_TRUE);
428870ff 1859 }
572e2857 1860 if (flags & DN_ID_NEW_EXIST) {
9c5167d1 1861 do_userquota_update(os, &cache,
9b7a83cb 1862 DN_USED_BYTES(dn->dn_phys), dn->dn_phys->dn_flags,
9c5167d1
NF
1863 dn->dn_newuid, dn->dn_newgid,
1864 dn->dn_newprojid, B_FALSE);
1865 do_userobjquota_update(os, &cache,
1866 dn->dn_phys->dn_flags, dn->dn_newuid, dn->dn_newgid,
1867 dn->dn_newprojid, B_FALSE);
428870ff
BB
1868 }
1869
572e2857 1870 mutex_enter(&dn->dn_mtx);
428870ff
BB
1871 dn->dn_oldused = 0;
1872 dn->dn_oldflags = 0;
1873 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1874 dn->dn_olduid = dn->dn_newuid;
1875 dn->dn_oldgid = dn->dn_newgid;
9c5167d1 1876 dn->dn_oldprojid = dn->dn_newprojid;
428870ff
BB
1877 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1878 if (dn->dn_bonuslen == 0)
1879 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1880 else
1881 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1882 }
1883 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
9babb374
BB
1884 mutex_exit(&dn->dn_mtx);
1885
64fc7762
MA
1886 multilist_sublist_remove(list, dn);
1887 dnode_rele(dn, os->os_synced_dnodes);
9babb374 1888 }
9b7a83cb 1889 do_userquota_cacheflush(os, &cache, tx);
64fc7762
MA
1890 multilist_sublist_unlock(list);
1891 kmem_free(uua, sizeof (*uua));
1892}
1893
1894void
1895dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1896{
1897 if (!dmu_objset_userused_enabled(os))
1898 return;
1899
b5256303
TC
1900 /* if this is a raw receive just return and handle accounting later */
1901 if (os->os_encrypted && dmu_objset_is_receiving(os))
1902 return;
1903
9c5167d1 1904 /* Allocate the user/group/project used objects if necessary. */
64fc7762
MA
1905 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1906 VERIFY0(zap_create_claim(os,
1907 DMU_USERUSED_OBJECT,
1908 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1909 VERIFY0(zap_create_claim(os,
1910 DMU_GROUPUSED_OBJECT,
1911 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1912 }
1913
9c5167d1
NF
1914 if (dmu_objset_projectquota_enabled(os) &&
1915 DMU_PROJECTUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1916 VERIFY0(zap_create_claim(os, DMU_PROJECTUSED_OBJECT,
1917 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1918 }
1919
64fc7762
MA
1920 for (int i = 0;
1921 i < multilist_get_num_sublists(os->os_synced_dnodes); i++) {
1922 userquota_updates_arg_t *uua =
1923 kmem_alloc(sizeof (*uua), KM_SLEEP);
1924 uua->uua_os = os;
1925 uua->uua_sublist_idx = i;
1926 uua->uua_tx = tx;
1927 /* note: caller does taskq_wait() */
1928 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1929 userquota_updates_task, uua, 0);
1930 /* callback frees uua */
1931 }
9babb374
BB
1932}
1933
428870ff
BB
1934/*
1935 * Returns a pointer to data to find uid/gid from
1936 *
1937 * If a dirty record for transaction group that is syncing can't
1938 * be found then NULL is returned. In the NULL case it is assumed
1939 * the uid/gid aren't changing.
1940 */
1941static void *
1942dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1943{
1944 dbuf_dirty_record_t *dr, **drp;
1945 void *data;
1946
1947 if (db->db_dirtycnt == 0)
1948 return (db->db.db_data); /* Nothing is changing */
1949
1950 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1951 if (dr->dr_txg == tx->tx_txg)
1952 break;
1953
572e2857 1954 if (dr == NULL) {
428870ff 1955 data = NULL;
572e2857
BB
1956 } else {
1957 dnode_t *dn;
1958
1959 DB_DNODE_ENTER(dr->dr_dbuf);
1960 dn = DB_DNODE(dr->dr_dbuf);
1961
1962 if (dn->dn_bonuslen == 0 &&
1963 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1964 data = dr->dt.dl.dr_data->b_data;
1965 else
1966 data = dr->dt.dl.dr_data;
1967
1968 DB_DNODE_EXIT(dr->dr_dbuf);
1969 }
1970
428870ff
BB
1971 return (data);
1972}
1973
1974void
1975dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1976{
1977 objset_t *os = dn->dn_objset;
1978 void *data = NULL;
1979 dmu_buf_impl_t *db = NULL;
a117a6d6
GW
1980 uint64_t *user = NULL;
1981 uint64_t *group = NULL;
9c5167d1 1982 uint64_t *project = NULL;
428870ff
BB
1983 int flags = dn->dn_id_flags;
1984 int error;
1985 boolean_t have_spill = B_FALSE;
1986
1987 if (!dmu_objset_userused_enabled(dn->dn_objset))
1988 return;
1989
b5256303
TC
1990 /*
1991 * Raw receives introduce a problem with user accounting. Raw
1992 * receives cannot update the user accounting info because the
1993 * user ids and the sizes are encrypted. To guarantee that we
1994 * never end up with bad user accounting, we simply disable it
1995 * during raw receives. We also disable this for normal receives
1996 * so that an incremental raw receive may be done on top of an
1997 * existing non-raw receive.
1998 */
1999 if (os->os_encrypted && dmu_objset_is_receiving(os))
2000 return;
2001
428870ff
BB
2002 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
2003 DN_ID_CHKED_SPILL)))
2004 return;
2005
2006 if (before && dn->dn_bonuslen != 0)
2007 data = DN_BONUS(dn->dn_phys);
2008 else if (!before && dn->dn_bonuslen != 0) {
a3000f93
BB
2009 if (dn->dn_bonus) {
2010 db = dn->dn_bonus;
428870ff
BB
2011 mutex_enter(&db->db_mtx);
2012 data = dmu_objset_userquota_find_data(db, tx);
2013 } else {
2014 data = DN_BONUS(dn->dn_phys);
2015 }
2016 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
2017 int rf = 0;
2018
2019 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
2020 rf |= DB_RF_HAVESTRUCT;
572e2857
BB
2021 error = dmu_spill_hold_by_dnode(dn,
2022 rf | DB_RF_MUST_SUCCEED,
428870ff
BB
2023 FTAG, (dmu_buf_t **)&db);
2024 ASSERT(error == 0);
2025 mutex_enter(&db->db_mtx);
2026 data = (before) ? db->db.db_data :
2027 dmu_objset_userquota_find_data(db, tx);
2028 have_spill = B_TRUE;
2029 } else {
2030 mutex_enter(&dn->dn_mtx);
2031 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2032 mutex_exit(&dn->dn_mtx);
2033 return;
2034 }
2035
2036 if (before) {
2037 ASSERT(data);
2038 user = &dn->dn_olduid;
2039 group = &dn->dn_oldgid;
9c5167d1 2040 project = &dn->dn_oldprojid;
428870ff
BB
2041 } else if (data) {
2042 user = &dn->dn_newuid;
2043 group = &dn->dn_newgid;
9c5167d1 2044 project = &dn->dn_newprojid;
428870ff
BB
2045 }
2046
2047 /*
2048 * Must always call the callback in case the object
2049 * type has changed and that type isn't an object type to track
2050 */
2051 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
9c5167d1 2052 user, group, project);
428870ff
BB
2053
2054 /*
2055 * Preserve existing uid/gid when the callback can't determine
2056 * what the new uid/gid are and the callback returned EEXIST.
2057 * The EEXIST error tells us to just use the existing uid/gid.
2058 * If we don't know what the old values are then just assign
2059 * them to 0, since that is a new file being created.
2060 */
2061 if (!before && data == NULL && error == EEXIST) {
2062 if (flags & DN_ID_OLD_EXIST) {
2063 dn->dn_newuid = dn->dn_olduid;
2064 dn->dn_newgid = dn->dn_oldgid;
9c5167d1 2065 dn->dn_newgid = dn->dn_oldprojid;
428870ff
BB
2066 } else {
2067 dn->dn_newuid = 0;
2068 dn->dn_newgid = 0;
9c5167d1 2069 dn->dn_newprojid = ZFS_DEFAULT_PROJID;
428870ff
BB
2070 }
2071 error = 0;
2072 }
2073
2074 if (db)
2075 mutex_exit(&db->db_mtx);
2076
2077 mutex_enter(&dn->dn_mtx);
2078 if (error == 0 && before)
2079 dn->dn_id_flags |= DN_ID_OLD_EXIST;
2080 if (error == 0 && !before)
2081 dn->dn_id_flags |= DN_ID_NEW_EXIST;
2082
2083 if (have_spill) {
2084 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
2085 } else {
2086 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2087 }
2088 mutex_exit(&dn->dn_mtx);
a3000f93 2089 if (have_spill)
428870ff
BB
2090 dmu_buf_rele((dmu_buf_t *)db, FTAG);
2091}
2092
9babb374
BB
2093boolean_t
2094dmu_objset_userspace_present(objset_t *os)
2095{
428870ff 2096 return (os->os_phys->os_flags &
9babb374
BB
2097 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
2098}
2099
1de321e6
JX
2100boolean_t
2101dmu_objset_userobjspace_present(objset_t *os)
2102{
2103 return (os->os_phys->os_flags &
2104 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE);
2105}
2106
9c5167d1
NF
2107boolean_t
2108dmu_objset_projectquota_present(objset_t *os)
2109{
2110 return (os->os_phys->os_flags &
2111 OBJSET_FLAG_PROJECTQUOTA_COMPLETE);
2112}
2113
1de321e6
JX
2114static int
2115dmu_objset_space_upgrade(objset_t *os)
9babb374
BB
2116{
2117 uint64_t obj;
2118 int err = 0;
2119
9babb374
BB
2120 /*
2121 * We simply need to mark every object dirty, so that it will be
2122 * synced out and now accounted. If this is called
2123 * concurrently, or if we already did some work before crashing,
2124 * that's fine, since we track each object's accounted state
2125 * independently.
2126 */
2127
2128 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
45d1cae3 2129 dmu_tx_t *tx;
9babb374
BB
2130 dmu_buf_t *db;
2131 int objerr;
2132
1de321e6
JX
2133 mutex_enter(&os->os_upgrade_lock);
2134 if (os->os_upgrade_exit)
2135 err = SET_ERROR(EINTR);
2136 mutex_exit(&os->os_upgrade_lock);
2137 if (err != 0)
2138 return (err);
2139
9babb374 2140 if (issig(JUSTLOOKING) && issig(FORREAL))
2e528b49 2141 return (SET_ERROR(EINTR));
9babb374
BB
2142
2143 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
13fe0198 2144 if (objerr != 0)
9babb374 2145 continue;
45d1cae3 2146 tx = dmu_tx_create(os);
9babb374
BB
2147 dmu_tx_hold_bonus(tx, obj);
2148 objerr = dmu_tx_assign(tx, TXG_WAIT);
13fe0198 2149 if (objerr != 0) {
d22323e8 2150 dmu_buf_rele(db, FTAG);
9babb374
BB
2151 dmu_tx_abort(tx);
2152 continue;
2153 }
2154 dmu_buf_will_dirty(db, tx);
2155 dmu_buf_rele(db, FTAG);
2156 dmu_tx_commit(tx);
2157 }
1de321e6
JX
2158 return (0);
2159}
2160
2161int
2162dmu_objset_userspace_upgrade(objset_t *os)
2163{
2164 int err = 0;
2165
2166 if (dmu_objset_userspace_present(os))
2167 return (0);
2168 if (dmu_objset_is_snapshot(os))
2169 return (SET_ERROR(EINVAL));
2170 if (!dmu_objset_userused_enabled(os))
2171 return (SET_ERROR(ENOTSUP));
2172
2173 err = dmu_objset_space_upgrade(os);
2174 if (err)
2175 return (err);
9babb374 2176
428870ff 2177 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
9babb374
BB
2178 txg_wait_synced(dmu_objset_pool(os), 0);
2179 return (0);
2180}
2181
1de321e6 2182static int
9c5167d1 2183dmu_objset_id_quota_upgrade_cb(objset_t *os)
1de321e6
JX
2184{
2185 int err = 0;
2186
9c5167d1
NF
2187 if (dmu_objset_userobjspace_present(os) &&
2188 dmu_objset_projectquota_present(os))
1de321e6
JX
2189 return (0);
2190 if (dmu_objset_is_snapshot(os))
2191 return (SET_ERROR(EINVAL));
2192 if (!dmu_objset_userobjused_enabled(os))
2193 return (SET_ERROR(ENOTSUP));
9c5167d1
NF
2194 if (!dmu_objset_projectquota_enabled(os) &&
2195 dmu_objset_userobjspace_present(os))
2196 return (SET_ERROR(ENOTSUP));
1de321e6
JX
2197
2198 dmu_objset_ds(os)->ds_feature_activation_needed[
2199 SPA_FEATURE_USEROBJ_ACCOUNTING] = B_TRUE;
9c5167d1
NF
2200 if (dmu_objset_projectquota_enabled(os))
2201 dmu_objset_ds(os)->ds_feature_activation_needed[
2202 SPA_FEATURE_PROJECT_QUOTA] = B_TRUE;
1de321e6
JX
2203
2204 err = dmu_objset_space_upgrade(os);
2205 if (err)
2206 return (err);
2207
2208 os->os_flags |= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
9c5167d1
NF
2209 if (dmu_objset_projectquota_enabled(os))
2210 os->os_flags |= OBJSET_FLAG_PROJECTQUOTA_COMPLETE;
2211
1de321e6
JX
2212 txg_wait_synced(dmu_objset_pool(os), 0);
2213 return (0);
2214}
2215
2216void
9c5167d1 2217dmu_objset_id_quota_upgrade(objset_t *os)
1de321e6 2218{
9c5167d1 2219 dmu_objset_upgrade(os, dmu_objset_id_quota_upgrade_cb);
1de321e6
JX
2220}
2221
126ae9f4
JX
2222boolean_t
2223dmu_objset_userobjspace_upgradable(objset_t *os)
2224{
2225 return (dmu_objset_type(os) == DMU_OST_ZFS &&
2226 !dmu_objset_is_snapshot(os) &&
2227 dmu_objset_userobjused_enabled(os) &&
2228 !dmu_objset_userobjspace_present(os));
2229}
2230
9c5167d1
NF
2231boolean_t
2232dmu_objset_projectquota_upgradable(objset_t *os)
2233{
2234 return (dmu_objset_type(os) == DMU_OST_ZFS &&
2235 !dmu_objset_is_snapshot(os) &&
2236 dmu_objset_projectquota_enabled(os) &&
2237 !dmu_objset_projectquota_present(os));
2238}
2239
34dc7c2f
BB
2240void
2241dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
2242 uint64_t *usedobjsp, uint64_t *availobjsp)
2243{
428870ff 2244 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
34dc7c2f
BB
2245 usedobjsp, availobjsp);
2246}
2247
2248uint64_t
2249dmu_objset_fsid_guid(objset_t *os)
2250{
428870ff 2251 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
34dc7c2f
BB
2252}
2253
2254void
2255dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
2256{
428870ff
BB
2257 stat->dds_type = os->os_phys->os_type;
2258 if (os->os_dsl_dataset)
2259 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
34dc7c2f
BB
2260}
2261
2262void
2263dmu_objset_stats(objset_t *os, nvlist_t *nv)
2264{
428870ff
BB
2265 ASSERT(os->os_dsl_dataset ||
2266 os->os_phys->os_type == DMU_OST_META);
34dc7c2f 2267
428870ff
BB
2268 if (os->os_dsl_dataset != NULL)
2269 dsl_dataset_stats(os->os_dsl_dataset, nv);
34dc7c2f
BB
2270
2271 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
428870ff 2272 os->os_phys->os_type);
9babb374
BB
2273 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
2274 dmu_objset_userspace_present(os));
34dc7c2f
BB
2275}
2276
2277int
2278dmu_objset_is_snapshot(objset_t *os)
2279{
428870ff 2280 if (os->os_dsl_dataset != NULL)
0c66c32d 2281 return (os->os_dsl_dataset->ds_is_snapshot);
34dc7c2f
BB
2282 else
2283 return (B_FALSE);
2284}
2285
2286int
2287dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
2288 boolean_t *conflict)
2289{
428870ff 2290 dsl_dataset_t *ds = os->os_dsl_dataset;
34dc7c2f
BB
2291 uint64_t ignored;
2292
d683ddbb 2293 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2e528b49 2294 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2295
2296 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
d683ddbb 2297 dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
9b7b9cd3 2298 MT_NORMALIZE, real, maxlen, conflict));
34dc7c2f
BB
2299}
2300
2301int
2302dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
2303 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
2304{
428870ff 2305 dsl_dataset_t *ds = os->os_dsl_dataset;
34dc7c2f
BB
2306 zap_cursor_t cursor;
2307 zap_attribute_t attr;
2308
13fe0198
MA
2309 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
2310
d683ddbb 2311 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2e528b49 2312 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2313
2314 zap_cursor_init_serialized(&cursor,
2315 ds->ds_dir->dd_pool->dp_meta_objset,
d683ddbb 2316 dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
34dc7c2f
BB
2317
2318 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2319 zap_cursor_fini(&cursor);
2e528b49 2320 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2321 }
2322
2323 if (strlen(attr.za_name) + 1 > namelen) {
2324 zap_cursor_fini(&cursor);
2e528b49 2325 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f
BB
2326 }
2327
2328 (void) strcpy(name, attr.za_name);
2329 if (idp)
2330 *idp = attr.za_first_integer;
2331 if (case_conflict)
2332 *case_conflict = attr.za_normalization_conflict;
2333 zap_cursor_advance(&cursor);
2334 *offp = zap_cursor_serialize(&cursor);
2335 zap_cursor_fini(&cursor);
2336
2337 return (0);
2338}
2339
ebe7e575 2340int
6772fb67 2341dmu_snapshot_lookup(objset_t *os, const char *name, uint64_t *value)
ebe7e575 2342{
d1d7e268 2343 return (dsl_dataset_snap_lookup(os->os_dsl_dataset, name, value));
ebe7e575
BB
2344}
2345
34dc7c2f
BB
2346int
2347dmu_dir_list_next(objset_t *os, int namelen, char *name,
2348 uint64_t *idp, uint64_t *offp)
2349{
428870ff 2350 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
34dc7c2f
BB
2351 zap_cursor_t cursor;
2352 zap_attribute_t attr;
2353
2354 /* there is no next dir on a snapshot! */
428870ff 2355 if (os->os_dsl_dataset->ds_object !=
d683ddbb 2356 dsl_dir_phys(dd)->dd_head_dataset_obj)
2e528b49 2357 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2358
2359 zap_cursor_init_serialized(&cursor,
2360 dd->dd_pool->dp_meta_objset,
d683ddbb 2361 dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
34dc7c2f
BB
2362
2363 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2364 zap_cursor_fini(&cursor);
2e528b49 2365 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2366 }
2367
2368 if (strlen(attr.za_name) + 1 > namelen) {
2369 zap_cursor_fini(&cursor);
2e528b49 2370 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f
BB
2371 }
2372
2373 (void) strcpy(name, attr.za_name);
2374 if (idp)
2375 *idp = attr.za_first_integer;
2376 zap_cursor_advance(&cursor);
2377 *offp = zap_cursor_serialize(&cursor);
2378 zap_cursor_fini(&cursor);
2379
2380 return (0);
2381}
2382
9c43027b
AJ
2383typedef struct dmu_objset_find_ctx {
2384 taskq_t *dc_tq;
2385 dsl_pool_t *dc_dp;
2386 uint64_t dc_ddobj;
1149ba64 2387 char *dc_ddname; /* last component of ddobj's name */
9c43027b
AJ
2388 int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
2389 void *dc_arg;
2390 int dc_flags;
2391 kmutex_t *dc_error_lock;
2392 int *dc_error;
2393} dmu_objset_find_ctx_t;
2394
2395static void
2396dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
b128c09f 2397{
9c43027b 2398 dsl_pool_t *dp = dcp->dc_dp;
13fe0198
MA
2399 dsl_dir_t *dd;
2400 dsl_dataset_t *ds;
2401 zap_cursor_t zc;
2402 zap_attribute_t *attr;
2403 uint64_t thisobj;
9c43027b 2404 int err = 0;
13fe0198 2405
9c43027b
AJ
2406 /* don't process if there already was an error */
2407 if (*dcp->dc_error != 0)
2408 goto out;
13fe0198 2409
1149ba64
GM
2410 /*
2411 * Note: passing the name (dc_ddname) here is optional, but it
2412 * improves performance because we don't need to call
2413 * zap_value_search() to determine the name.
2414 */
2415 err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
13fe0198 2416 if (err != 0)
9c43027b 2417 goto out;
13fe0198
MA
2418
2419 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2420 if (dd->dd_myname[0] == '$') {
2421 dsl_dir_rele(dd, FTAG);
9c43027b 2422 goto out;
13fe0198
MA
2423 }
2424
d683ddbb 2425 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
79c76d5b 2426 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
13fe0198
MA
2427
2428 /*
2429 * Iterate over all children.
2430 */
9c43027b 2431 if (dcp->dc_flags & DS_FIND_CHILDREN) {
13fe0198 2432 for (zap_cursor_init(&zc, dp->dp_meta_objset,
d683ddbb 2433 dsl_dir_phys(dd)->dd_child_dir_zapobj);
13fe0198
MA
2434 zap_cursor_retrieve(&zc, attr) == 0;
2435 (void) zap_cursor_advance(&zc)) {
2436 ASSERT3U(attr->za_integer_length, ==,
2437 sizeof (uint64_t));
2438 ASSERT3U(attr->za_num_integers, ==, 1);
2439
1c27024e 2440 dmu_objset_find_ctx_t *child_dcp =
1149ba64 2441 kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
9c43027b
AJ
2442 *child_dcp = *dcp;
2443 child_dcp->dc_ddobj = attr->za_first_integer;
1149ba64 2444 child_dcp->dc_ddname = spa_strdup(attr->za_name);
9c43027b
AJ
2445 if (dcp->dc_tq != NULL)
2446 (void) taskq_dispatch(dcp->dc_tq,
2447 dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
2448 else
2449 dmu_objset_find_dp_impl(child_dcp);
13fe0198
MA
2450 }
2451 zap_cursor_fini(&zc);
13fe0198
MA
2452 }
2453
2454 /*
2455 * Iterate over all snapshots.
2456 */
9c43027b 2457 if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
13fe0198
MA
2458 dsl_dataset_t *ds;
2459 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2460
2461 if (err == 0) {
d683ddbb
JG
2462 uint64_t snapobj;
2463
2464 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
13fe0198
MA
2465 dsl_dataset_rele(ds, FTAG);
2466
2467 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2468 zap_cursor_retrieve(&zc, attr) == 0;
2469 (void) zap_cursor_advance(&zc)) {
2470 ASSERT3U(attr->za_integer_length, ==,
2471 sizeof (uint64_t));
2472 ASSERT3U(attr->za_num_integers, ==, 1);
2473
2474 err = dsl_dataset_hold_obj(dp,
2475 attr->za_first_integer, FTAG, &ds);
2476 if (err != 0)
2477 break;
9c43027b 2478 err = dcp->dc_func(dp, ds, dcp->dc_arg);
13fe0198
MA
2479 dsl_dataset_rele(ds, FTAG);
2480 if (err != 0)
2481 break;
2482 }
2483 zap_cursor_fini(&zc);
2484 }
2485 }
2486
13fe0198
MA
2487 kmem_free(attr, sizeof (zap_attribute_t));
2488
1149ba64
GM
2489 if (err != 0) {
2490 dsl_dir_rele(dd, FTAG);
9c43027b 2491 goto out;
1149ba64 2492 }
13fe0198
MA
2493
2494 /*
2495 * Apply to self.
2496 */
2497 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1149ba64
GM
2498
2499 /*
2500 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2501 * that the dir will remain cached, and we won't have to re-instantiate
2502 * it (which could be expensive due to finding its name via
2503 * zap_value_search()).
2504 */
2505 dsl_dir_rele(dd, FTAG);
13fe0198 2506 if (err != 0)
9c43027b
AJ
2507 goto out;
2508 err = dcp->dc_func(dp, ds, dcp->dc_arg);
13fe0198 2509 dsl_dataset_rele(ds, FTAG);
9c43027b
AJ
2510
2511out:
2512 if (err != 0) {
2513 mutex_enter(dcp->dc_error_lock);
2514 /* only keep first error */
2515 if (*dcp->dc_error == 0)
2516 *dcp->dc_error = err;
2517 mutex_exit(dcp->dc_error_lock);
2518 }
2519
1149ba64
GM
2520 if (dcp->dc_ddname != NULL)
2521 spa_strfree(dcp->dc_ddname);
9c43027b
AJ
2522 kmem_free(dcp, sizeof (*dcp));
2523}
2524
2525static void
2526dmu_objset_find_dp_cb(void *arg)
2527{
2528 dmu_objset_find_ctx_t *dcp = arg;
2529 dsl_pool_t *dp = dcp->dc_dp;
2530
5e8cd5d1
AJ
2531 /*
2532 * We need to get a pool_config_lock here, as there are several
2533 * asssert(pool_config_held) down the stack. Getting a lock via
2534 * dsl_pool_config_enter is risky, as it might be stalled by a
2535 * pending writer. This would deadlock, as the write lock can
2536 * only be granted when our parent thread gives up the lock.
2537 * The _prio interface gives us priority over a pending writer.
2538 */
2539 dsl_pool_config_enter_prio(dp, FTAG);
9c43027b
AJ
2540
2541 dmu_objset_find_dp_impl(dcp);
2542
2543 dsl_pool_config_exit(dp, FTAG);
2544}
2545
2546/*
2547 * Find objsets under and including ddobj, call func(ds) on each.
2548 * The order for the enumeration is completely undefined.
2549 * func is called with dsl_pool_config held.
2550 */
2551int
2552dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
2553 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
2554{
2555 int error = 0;
2556 taskq_t *tq = NULL;
2557 int ntasks;
2558 dmu_objset_find_ctx_t *dcp;
2559 kmutex_t err_lock;
2560
2561 mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
2562 dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
2563 dcp->dc_tq = NULL;
2564 dcp->dc_dp = dp;
2565 dcp->dc_ddobj = ddobj;
1149ba64 2566 dcp->dc_ddname = NULL;
9c43027b
AJ
2567 dcp->dc_func = func;
2568 dcp->dc_arg = arg;
2569 dcp->dc_flags = flags;
2570 dcp->dc_error_lock = &err_lock;
2571 dcp->dc_error = &error;
2572
2573 if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
2574 /*
2575 * In case a write lock is held we can't make use of
2576 * parallelism, as down the stack of the worker threads
2577 * the lock is asserted via dsl_pool_config_held.
2578 * In case of a read lock this is solved by getting a read
2579 * lock in each worker thread, which isn't possible in case
2580 * of a writer lock. So we fall back to the synchronous path
2581 * here.
2582 * In the future it might be possible to get some magic into
2583 * dsl_pool_config_held in a way that it returns true for
2584 * the worker threads so that a single lock held from this
2585 * thread suffices. For now, stay single threaded.
2586 */
2587 dmu_objset_find_dp_impl(dcp);
e5676636 2588 mutex_destroy(&err_lock);
9c43027b
AJ
2589
2590 return (error);
2591 }
2592
2593 ntasks = dmu_find_threads;
2594 if (ntasks == 0)
2595 ntasks = vdev_count_leaves(dp->dp_spa) * 4;
1229323d 2596 tq = taskq_create("dmu_objset_find", ntasks, maxclsyspri, ntasks,
9c43027b
AJ
2597 INT_MAX, 0);
2598 if (tq == NULL) {
2599 kmem_free(dcp, sizeof (*dcp));
e5676636
BB
2600 mutex_destroy(&err_lock);
2601
9c43027b
AJ
2602 return (SET_ERROR(ENOMEM));
2603 }
2604 dcp->dc_tq = tq;
2605
2606 /* dcp will be freed by task */
2607 (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
2608
2609 /*
2610 * PORTING: this code relies on the property of taskq_wait to wait
2611 * until no more tasks are queued and no more tasks are active. As
2612 * we always queue new tasks from within other tasks, task_wait
2613 * reliably waits for the full recursion to finish, even though we
2614 * enqueue new tasks after taskq_wait has been called.
2615 * On platforms other than illumos, taskq_wait may not have this
2616 * property.
2617 */
2618 taskq_wait(tq);
2619 taskq_destroy(tq);
2620 mutex_destroy(&err_lock);
2621
2622 return (error);
b128c09f
BB
2623}
2624
2625/*
13fe0198
MA
2626 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2627 * The dp_config_rwlock must not be held when this is called, and it
2628 * will not be held when the callback is called.
2629 * Therefore this function should only be used when the pool is not changing
2630 * (e.g. in syncing context), or the callback can deal with the possible races.
b128c09f 2631 */
13fe0198
MA
2632static int
2633dmu_objset_find_impl(spa_t *spa, const char *name,
2634 int func(const char *, void *), void *arg, int flags)
34dc7c2f
BB
2635{
2636 dsl_dir_t *dd;
13fe0198 2637 dsl_pool_t *dp = spa_get_dsl(spa);
b128c09f 2638 dsl_dataset_t *ds;
34dc7c2f
BB
2639 zap_cursor_t zc;
2640 zap_attribute_t *attr;
2641 char *child;
b128c09f
BB
2642 uint64_t thisobj;
2643 int err;
34dc7c2f 2644
13fe0198
MA
2645 dsl_pool_config_enter(dp, FTAG);
2646
2647 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
2648 if (err != 0) {
2649 dsl_pool_config_exit(dp, FTAG);
34dc7c2f 2650 return (err);
13fe0198 2651 }
34dc7c2f 2652
b128c09f
BB
2653 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2654 if (dd->dd_myname[0] == '$') {
13fe0198
MA
2655 dsl_dir_rele(dd, FTAG);
2656 dsl_pool_config_exit(dp, FTAG);
b128c09f
BB
2657 return (0);
2658 }
2659
d683ddbb 2660 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
79c76d5b 2661 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
34dc7c2f
BB
2662
2663 /*
2664 * Iterate over all children.
2665 */
2666 if (flags & DS_FIND_CHILDREN) {
b128c09f 2667 for (zap_cursor_init(&zc, dp->dp_meta_objset,
d683ddbb 2668 dsl_dir_phys(dd)->dd_child_dir_zapobj);
34dc7c2f
BB
2669 zap_cursor_retrieve(&zc, attr) == 0;
2670 (void) zap_cursor_advance(&zc)) {
13fe0198
MA
2671 ASSERT3U(attr->za_integer_length, ==,
2672 sizeof (uint64_t));
2673 ASSERT3U(attr->za_num_integers, ==, 1);
34dc7c2f 2674
428870ff 2675 child = kmem_asprintf("%s/%s", name, attr->za_name);
13fe0198
MA
2676 dsl_pool_config_exit(dp, FTAG);
2677 err = dmu_objset_find_impl(spa, child,
2678 func, arg, flags);
2679 dsl_pool_config_enter(dp, FTAG);
428870ff 2680 strfree(child);
13fe0198 2681 if (err != 0)
34dc7c2f
BB
2682 break;
2683 }
2684 zap_cursor_fini(&zc);
2685
13fe0198
MA
2686 if (err != 0) {
2687 dsl_dir_rele(dd, FTAG);
2688 dsl_pool_config_exit(dp, FTAG);
34dc7c2f
BB
2689 kmem_free(attr, sizeof (zap_attribute_t));
2690 return (err);
2691 }
2692 }
2693
2694 /*
2695 * Iterate over all snapshots.
2696 */
b128c09f 2697 if (flags & DS_FIND_SNAPSHOTS) {
b128c09f 2698 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
b128c09f
BB
2699
2700 if (err == 0) {
d683ddbb
JG
2701 uint64_t snapobj;
2702
2703 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
b128c09f
BB
2704 dsl_dataset_rele(ds, FTAG);
2705
2706 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2707 zap_cursor_retrieve(&zc, attr) == 0;
2708 (void) zap_cursor_advance(&zc)) {
13fe0198 2709 ASSERT3U(attr->za_integer_length, ==,
b128c09f 2710 sizeof (uint64_t));
13fe0198 2711 ASSERT3U(attr->za_num_integers, ==, 1);
b128c09f 2712
428870ff
BB
2713 child = kmem_asprintf("%s@%s",
2714 name, attr->za_name);
13fe0198
MA
2715 dsl_pool_config_exit(dp, FTAG);
2716 err = func(child, arg);
2717 dsl_pool_config_enter(dp, FTAG);
428870ff 2718 strfree(child);
13fe0198 2719 if (err != 0)
b128c09f
BB
2720 break;
2721 }
2722 zap_cursor_fini(&zc);
34dc7c2f 2723 }
34dc7c2f
BB
2724 }
2725
13fe0198 2726 dsl_dir_rele(dd, FTAG);
34dc7c2f 2727 kmem_free(attr, sizeof (zap_attribute_t));
13fe0198 2728 dsl_pool_config_exit(dp, FTAG);
34dc7c2f 2729
13fe0198 2730 if (err != 0)
34dc7c2f
BB
2731 return (err);
2732
13fe0198
MA
2733 /* Apply to self. */
2734 return (func(name, arg));
34dc7c2f
BB
2735}
2736
13fe0198
MA
2737/*
2738 * See comment above dmu_objset_find_impl().
2739 */
d164b209 2740int
13fe0198
MA
2741dmu_objset_find(char *name, int func(const char *, void *), void *arg,
2742 int flags)
d164b209 2743{
13fe0198
MA
2744 spa_t *spa;
2745 int error;
d164b209 2746
13fe0198
MA
2747 error = spa_open(name, &spa, FTAG);
2748 if (error != 0)
2749 return (error);
2750 error = dmu_objset_find_impl(spa, name, func, arg, flags);
2751 spa_close(spa, FTAG);
2752 return (error);
d164b209
BB
2753}
2754
ae76f45c
TC
2755boolean_t
2756dmu_objset_incompatible_encryption_version(objset_t *os)
2757{
2758 return (dsl_dir_incompatible_encryption_version(
2759 os->os_dsl_dataset->ds_dir));
2760}
2761
34dc7c2f
BB
2762void
2763dmu_objset_set_user(objset_t *os, void *user_ptr)
2764{
428870ff
BB
2765 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2766 os->os_user_ptr = user_ptr;
34dc7c2f
BB
2767}
2768
2769void *
2770dmu_objset_get_user(objset_t *os)
2771{
428870ff
BB
2772 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2773 return (os->os_user_ptr);
34dc7c2f 2774}
c28b2279 2775
13fe0198
MA
2776/*
2777 * Determine name of filesystem, given name of snapshot.
eca7b760 2778 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
13fe0198
MA
2779 */
2780int
2781dmu_fsname(const char *snapname, char *buf)
2782{
2783 char *atp = strchr(snapname, '@');
2784 if (atp == NULL)
2e528b49 2785 return (SET_ERROR(EINVAL));
eca7b760 2786 if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2e528b49 2787 return (SET_ERROR(ENAMETOOLONG));
13fe0198
MA
2788 (void) strlcpy(buf, snapname, atp - snapname + 1);
2789 return (0);
2790}
2791
3ec3bc21
BB
2792/*
2793 * Call when we think we're going to write/free space in open context to track
2794 * the amount of dirty data in the open txg, which is also the amount
2795 * of memory that can not be evicted until this txg syncs.
2796 */
2797void
2798dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
2799{
2800 dsl_dataset_t *ds = os->os_dsl_dataset;
2801 int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
2802
2803 if (ds != NULL) {
2804 dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
2805 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
2806 }
2807}
2808
c28b2279 2809#if defined(_KERNEL) && defined(HAVE_SPL)
f0fd83be 2810EXPORT_SYMBOL(dmu_objset_zil);
c28b2279 2811EXPORT_SYMBOL(dmu_objset_pool);
f0fd83be
BB
2812EXPORT_SYMBOL(dmu_objset_ds);
2813EXPORT_SYMBOL(dmu_objset_type);
c28b2279
BB
2814EXPORT_SYMBOL(dmu_objset_name);
2815EXPORT_SYMBOL(dmu_objset_hold);
b5256303 2816EXPORT_SYMBOL(dmu_objset_hold_flags);
c28b2279
BB
2817EXPORT_SYMBOL(dmu_objset_own);
2818EXPORT_SYMBOL(dmu_objset_rele);
b5256303 2819EXPORT_SYMBOL(dmu_objset_rele_flags);
c28b2279
BB
2820EXPORT_SYMBOL(dmu_objset_disown);
2821EXPORT_SYMBOL(dmu_objset_from_ds);
2822EXPORT_SYMBOL(dmu_objset_create);
2823EXPORT_SYMBOL(dmu_objset_clone);
c28b2279
BB
2824EXPORT_SYMBOL(dmu_objset_stats);
2825EXPORT_SYMBOL(dmu_objset_fast_stat);
54a179e7 2826EXPORT_SYMBOL(dmu_objset_spa);
c28b2279
BB
2827EXPORT_SYMBOL(dmu_objset_space);
2828EXPORT_SYMBOL(dmu_objset_fsid_guid);
2829EXPORT_SYMBOL(dmu_objset_find);
c28b2279
BB
2830EXPORT_SYMBOL(dmu_objset_byteswap);
2831EXPORT_SYMBOL(dmu_objset_evict_dbufs);
2832EXPORT_SYMBOL(dmu_objset_snap_cmtime);
50c957f7 2833EXPORT_SYMBOL(dmu_objset_dnodesize);
c28b2279
BB
2834
2835EXPORT_SYMBOL(dmu_objset_sync);
2836EXPORT_SYMBOL(dmu_objset_is_dirty);
b5256303 2837EXPORT_SYMBOL(dmu_objset_create_impl_dnstats);
c28b2279
BB
2838EXPORT_SYMBOL(dmu_objset_create_impl);
2839EXPORT_SYMBOL(dmu_objset_open_impl);
2840EXPORT_SYMBOL(dmu_objset_evict);
2841EXPORT_SYMBOL(dmu_objset_register_type);
2842EXPORT_SYMBOL(dmu_objset_do_userquota_updates);
2843EXPORT_SYMBOL(dmu_objset_userquota_get_ids);
2844EXPORT_SYMBOL(dmu_objset_userused_enabled);
2845EXPORT_SYMBOL(dmu_objset_userspace_upgrade);
2846EXPORT_SYMBOL(dmu_objset_userspace_present);
1de321e6 2847EXPORT_SYMBOL(dmu_objset_userobjused_enabled);
126ae9f4 2848EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable);
1de321e6 2849EXPORT_SYMBOL(dmu_objset_userobjspace_present);
9c5167d1
NF
2850EXPORT_SYMBOL(dmu_objset_projectquota_enabled);
2851EXPORT_SYMBOL(dmu_objset_projectquota_present);
2852EXPORT_SYMBOL(dmu_objset_projectquota_upgradable);
2853EXPORT_SYMBOL(dmu_objset_id_quota_upgrade);
c28b2279 2854#endif