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