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