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