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