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