]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dmu_objset.c
a8304c18df28dbf7c400cd6e13263d13c3b9cbbb
[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 */
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_recv.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[
1077 SPA_FEATURE_USEROBJ_ACCOUNTING] = (void *)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[
1083 SPA_FEATURE_PROJECT_QUOTA] = (void *)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 spa_t *spa = dp->dp_spa;
1161 dsl_dir_t *pdd;
1162 const char *tail;
1163 dsl_dataset_t *ds;
1164 uint64_t obj;
1165 blkptr_t *bp;
1166 objset_t *os;
1167 zio_t *rzio;
1168
1169 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
1170
1171 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
1172 doca->doca_cred, doca->doca_dcp, tx);
1173
1174 VERIFY0(dsl_dataset_hold_obj_flags(pdd->dd_pool, obj,
1175 DS_HOLD_FLAG_DECRYPT, FTAG, &ds));
1176 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1177 bp = dsl_dataset_get_blkptr(ds);
1178 os = dmu_objset_create_impl(spa, 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(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 if (txg_list_member(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
1214 ASSERT3P(ds->ds_key_mapping, !=, NULL);
1215 key_mapping_rele(spa, ds->ds_key_mapping, ds);
1216 }
1217
1218 rzio = zio_root(spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
1219 tmpds = txg_list_remove_this(&dp->dp_dirty_datasets, ds,
1220 tx->tx_txg);
1221 if (tmpds != NULL) {
1222 dmu_buf_rele(ds->ds_dbuf, ds);
1223 dsl_dataset_sync(ds, rzio, tx);
1224 }
1225 VERIFY0(zio_wait(rzio));
1226
1227 if (need_sync_done) {
1228 ASSERT3P(ds->ds_key_mapping, !=, NULL);
1229 key_mapping_rele(spa, ds->ds_key_mapping, ds);
1230 dsl_dataset_sync_done(ds, tx);
1231 }
1232
1233 mutex_enter(&ds->ds_lock);
1234 ds->ds_owner = NULL;
1235 mutex_exit(&ds->ds_lock);
1236 }
1237
1238 spa_history_log_internal_ds(ds, "create", tx, "");
1239 zvol_create_minors(spa, doca->doca_name, B_TRUE);
1240
1241 dsl_dataset_rele_flags(ds, DS_HOLD_FLAG_DECRYPT, FTAG);
1242 dsl_dir_rele(pdd, FTAG);
1243 }
1244
1245 int
1246 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
1247 dsl_crypto_params_t *dcp, dmu_objset_create_sync_func_t func, void *arg)
1248 {
1249 dmu_objset_create_arg_t doca;
1250 dsl_crypto_params_t tmp_dcp = { 0 };
1251
1252 doca.doca_name = name;
1253 doca.doca_cred = CRED();
1254 doca.doca_flags = flags;
1255 doca.doca_userfunc = func;
1256 doca.doca_userarg = arg;
1257 doca.doca_type = type;
1258
1259 /*
1260 * Some callers (mostly for testing) do not provide a dcp on their
1261 * own but various code inside the sync task will require it to be
1262 * allocated. Rather than adding NULL checks throughout this code
1263 * or adding dummy dcp's to all of the callers we simply create a
1264 * dummy one here and use that. This zero dcp will have the same
1265 * effect as asking for inheritance of all encryption params.
1266 */
1267 doca.doca_dcp = (dcp != NULL) ? dcp : &tmp_dcp;
1268
1269 return (dsl_sync_task(name,
1270 dmu_objset_create_check, dmu_objset_create_sync, &doca,
1271 6, ZFS_SPACE_CHECK_NORMAL));
1272 }
1273
1274 typedef struct dmu_objset_clone_arg {
1275 const char *doca_clone;
1276 const char *doca_origin;
1277 cred_t *doca_cred;
1278 } dmu_objset_clone_arg_t;
1279
1280 /*ARGSUSED*/
1281 static int
1282 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
1283 {
1284 dmu_objset_clone_arg_t *doca = arg;
1285 dsl_dir_t *pdd;
1286 const char *tail;
1287 int error;
1288 dsl_dataset_t *origin;
1289 dsl_pool_t *dp = dmu_tx_pool(tx);
1290
1291 if (strchr(doca->doca_clone, '@') != NULL)
1292 return (SET_ERROR(EINVAL));
1293
1294 if (strlen(doca->doca_clone) >= ZFS_MAX_DATASET_NAME_LEN)
1295 return (SET_ERROR(ENAMETOOLONG));
1296
1297 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
1298 if (error != 0)
1299 return (error);
1300 if (tail == NULL) {
1301 dsl_dir_rele(pdd, FTAG);
1302 return (SET_ERROR(EEXIST));
1303 }
1304
1305 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
1306 doca->doca_cred);
1307 if (error != 0) {
1308 dsl_dir_rele(pdd, FTAG);
1309 return (SET_ERROR(EDQUOT));
1310 }
1311
1312 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
1313 if (error != 0) {
1314 dsl_dir_rele(pdd, FTAG);
1315 return (error);
1316 }
1317
1318 /* You can only clone snapshots, not the head datasets. */
1319 if (!origin->ds_is_snapshot) {
1320 dsl_dataset_rele(origin, FTAG);
1321 dsl_dir_rele(pdd, FTAG);
1322 return (SET_ERROR(EINVAL));
1323 }
1324
1325 error = dmu_objset_clone_crypt_check(pdd, origin->ds_dir);
1326 if (error != 0) {
1327 dsl_dataset_rele(origin, FTAG);
1328 dsl_dir_rele(pdd, FTAG);
1329 return (error);
1330 }
1331
1332 dsl_dataset_rele(origin, FTAG);
1333 dsl_dir_rele(pdd, FTAG);
1334
1335 return (0);
1336 }
1337
1338 static void
1339 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
1340 {
1341 dmu_objset_clone_arg_t *doca = arg;
1342 dsl_pool_t *dp = dmu_tx_pool(tx);
1343 dsl_dir_t *pdd;
1344 const char *tail;
1345 dsl_dataset_t *origin, *ds;
1346 uint64_t obj;
1347 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
1348
1349 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
1350 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
1351
1352 obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
1353 doca->doca_cred, NULL, tx);
1354
1355 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
1356 dsl_dataset_name(origin, namebuf);
1357 spa_history_log_internal_ds(ds, "clone", tx,
1358 "origin=%s (%llu)", namebuf, origin->ds_object);
1359 zvol_create_minors(dp->dp_spa, doca->doca_clone, B_TRUE);
1360 dsl_dataset_rele(ds, FTAG);
1361 dsl_dataset_rele(origin, FTAG);
1362 dsl_dir_rele(pdd, FTAG);
1363 }
1364
1365 int
1366 dmu_objset_clone(const char *clone, const char *origin)
1367 {
1368 dmu_objset_clone_arg_t doca;
1369
1370 doca.doca_clone = clone;
1371 doca.doca_origin = origin;
1372 doca.doca_cred = CRED();
1373
1374 return (dsl_sync_task(clone,
1375 dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
1376 6, ZFS_SPACE_CHECK_NORMAL));
1377 }
1378
1379 static int
1380 dmu_objset_remap_indirects_impl(objset_t *os, uint64_t last_removed_txg)
1381 {
1382 int error = 0;
1383 uint64_t object = 0;
1384 while ((error = dmu_object_next(os, &object, B_FALSE, 0)) == 0) {
1385 error = dmu_object_remap_indirects(os, object,
1386 last_removed_txg);
1387 /*
1388 * If the ZPL removed the object before we managed to dnode_hold
1389 * it, we would get an ENOENT. If the ZPL declares its intent
1390 * to remove the object (dnode_free) before we manage to
1391 * dnode_hold it, we would get an EEXIST. In either case, we
1392 * want to continue remapping the other objects in the objset;
1393 * in all other cases, we want to break early.
1394 */
1395 if (error != 0 && error != ENOENT && error != EEXIST) {
1396 break;
1397 }
1398 }
1399 if (error == ESRCH) {
1400 error = 0;
1401 }
1402 return (error);
1403 }
1404
1405 int
1406 dmu_objset_remap_indirects(const char *fsname)
1407 {
1408 int error = 0;
1409 objset_t *os = NULL;
1410 uint64_t last_removed_txg;
1411 uint64_t remap_start_txg;
1412 dsl_dir_t *dd;
1413
1414 error = dmu_objset_hold(fsname, FTAG, &os);
1415 if (error != 0) {
1416 return (error);
1417 }
1418 dd = dmu_objset_ds(os)->ds_dir;
1419
1420 if (!spa_feature_is_enabled(dmu_objset_spa(os),
1421 SPA_FEATURE_OBSOLETE_COUNTS)) {
1422 dmu_objset_rele(os, FTAG);
1423 return (SET_ERROR(ENOTSUP));
1424 }
1425
1426 if (dsl_dataset_is_snapshot(dmu_objset_ds(os))) {
1427 dmu_objset_rele(os, FTAG);
1428 return (SET_ERROR(EINVAL));
1429 }
1430
1431 /*
1432 * If there has not been a removal, we're done.
1433 */
1434 last_removed_txg = spa_get_last_removal_txg(dmu_objset_spa(os));
1435 if (last_removed_txg == -1ULL) {
1436 dmu_objset_rele(os, FTAG);
1437 return (0);
1438 }
1439
1440 /*
1441 * If we have remapped since the last removal, we're done.
1442 */
1443 if (dsl_dir_is_zapified(dd)) {
1444 uint64_t last_remap_txg;
1445 if (zap_lookup(spa_meta_objset(dmu_objset_spa(os)),
1446 dd->dd_object, DD_FIELD_LAST_REMAP_TXG,
1447 sizeof (last_remap_txg), 1, &last_remap_txg) == 0 &&
1448 last_remap_txg > last_removed_txg) {
1449 dmu_objset_rele(os, FTAG);
1450 return (0);
1451 }
1452 }
1453
1454 dsl_dataset_long_hold(dmu_objset_ds(os), FTAG);
1455 dsl_pool_rele(dmu_objset_pool(os), FTAG);
1456
1457 remap_start_txg = spa_last_synced_txg(dmu_objset_spa(os));
1458 error = dmu_objset_remap_indirects_impl(os, last_removed_txg);
1459 if (error == 0) {
1460 /*
1461 * We update the last_remap_txg to be the start txg so that
1462 * we can guarantee that every block older than last_remap_txg
1463 * that can be remapped has been remapped.
1464 */
1465 error = dsl_dir_update_last_remap_txg(dd, remap_start_txg);
1466 }
1467
1468 dsl_dataset_long_rele(dmu_objset_ds(os), FTAG);
1469 dsl_dataset_rele(dmu_objset_ds(os), FTAG);
1470
1471 return (error);
1472 }
1473
1474 int
1475 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
1476 {
1477 int err;
1478 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1479 nvlist_t *snaps = fnvlist_alloc();
1480
1481 fnvlist_add_boolean(snaps, longsnap);
1482 strfree(longsnap);
1483 err = dsl_dataset_snapshot(snaps, NULL, NULL);
1484 fnvlist_free(snaps);
1485 return (err);
1486 }
1487
1488 static void
1489 dmu_objset_upgrade_task_cb(void *data)
1490 {
1491 objset_t *os = data;
1492
1493 mutex_enter(&os->os_upgrade_lock);
1494 os->os_upgrade_status = EINTR;
1495 if (!os->os_upgrade_exit) {
1496 mutex_exit(&os->os_upgrade_lock);
1497
1498 os->os_upgrade_status = os->os_upgrade_cb(os);
1499 mutex_enter(&os->os_upgrade_lock);
1500 }
1501 os->os_upgrade_exit = B_TRUE;
1502 os->os_upgrade_id = 0;
1503 mutex_exit(&os->os_upgrade_lock);
1504 dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1505 }
1506
1507 static void
1508 dmu_objset_upgrade(objset_t *os, dmu_objset_upgrade_cb_t cb)
1509 {
1510 if (os->os_upgrade_id != 0)
1511 return;
1512
1513 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1514 dsl_dataset_long_hold(dmu_objset_ds(os), upgrade_tag);
1515
1516 mutex_enter(&os->os_upgrade_lock);
1517 if (os->os_upgrade_id == 0 && os->os_upgrade_status == 0) {
1518 os->os_upgrade_exit = B_FALSE;
1519 os->os_upgrade_cb = cb;
1520 os->os_upgrade_id = taskq_dispatch(
1521 os->os_spa->spa_upgrade_taskq,
1522 dmu_objset_upgrade_task_cb, os, TQ_SLEEP);
1523 if (os->os_upgrade_id == TASKQID_INVALID) {
1524 dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1525 os->os_upgrade_status = ENOMEM;
1526 }
1527 }
1528 mutex_exit(&os->os_upgrade_lock);
1529 }
1530
1531 static void
1532 dmu_objset_upgrade_stop(objset_t *os)
1533 {
1534 mutex_enter(&os->os_upgrade_lock);
1535 os->os_upgrade_exit = B_TRUE;
1536 if (os->os_upgrade_id != 0) {
1537 taskqid_t id = os->os_upgrade_id;
1538
1539 os->os_upgrade_id = 0;
1540 mutex_exit(&os->os_upgrade_lock);
1541
1542 if ((taskq_cancel_id(os->os_spa->spa_upgrade_taskq, id)) == 0) {
1543 dsl_dataset_long_rele(dmu_objset_ds(os), upgrade_tag);
1544 }
1545 txg_wait_synced(os->os_spa->spa_dsl_pool, 0);
1546 } else {
1547 mutex_exit(&os->os_upgrade_lock);
1548 }
1549 }
1550
1551 static void
1552 dmu_objset_sync_dnodes(multilist_sublist_t *list, dmu_tx_t *tx)
1553 {
1554 dnode_t *dn;
1555
1556 while ((dn = multilist_sublist_head(list)) != NULL) {
1557 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1558 ASSERT(dn->dn_dbuf->db_data_pending);
1559 /*
1560 * Initialize dn_zio outside dnode_sync() because the
1561 * meta-dnode needs to set it ouside dnode_sync().
1562 */
1563 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1564 ASSERT(dn->dn_zio);
1565
1566 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1567 multilist_sublist_remove(list, dn);
1568
1569 /*
1570 * If we are not doing useraccounting (os_synced_dnodes == NULL)
1571 * we are done with this dnode for this txg. Unset dn_dirty_txg
1572 * if later txgs aren't dirtying it so that future holders do
1573 * not get a stale value. Otherwise, we will do this in
1574 * userquota_updates_task() when processing has completely
1575 * finished for this txg.
1576 */
1577 multilist_t *newlist = dn->dn_objset->os_synced_dnodes;
1578 if (newlist != NULL) {
1579 (void) dnode_add_ref(dn, newlist);
1580 multilist_insert(newlist, dn);
1581 } else {
1582 mutex_enter(&dn->dn_mtx);
1583 if (dn->dn_dirty_txg == tx->tx_txg)
1584 dn->dn_dirty_txg = 0;
1585 mutex_exit(&dn->dn_mtx);
1586 }
1587
1588 dnode_sync(dn, tx);
1589 }
1590 }
1591
1592 /* ARGSUSED */
1593 static void
1594 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1595 {
1596 blkptr_t *bp = zio->io_bp;
1597 objset_t *os = arg;
1598 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1599 uint64_t fill = 0;
1600
1601 ASSERT(!BP_IS_EMBEDDED(bp));
1602 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1603 ASSERT0(BP_GET_LEVEL(bp));
1604
1605 /*
1606 * Update rootbp fill count: it should be the number of objects
1607 * allocated in the object set (not counting the "special"
1608 * objects that are stored in the objset_phys_t -- the meta
1609 * dnode and user/group/project accounting objects).
1610 */
1611 for (int i = 0; i < dnp->dn_nblkptr; i++)
1612 fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1613
1614 BP_SET_FILL(bp, fill);
1615
1616 if (os->os_dsl_dataset != NULL)
1617 rrw_enter(&os->os_dsl_dataset->ds_bp_rwlock, RW_WRITER, FTAG);
1618 *os->os_rootbp = *bp;
1619 if (os->os_dsl_dataset != NULL)
1620 rrw_exit(&os->os_dsl_dataset->ds_bp_rwlock, FTAG);
1621 }
1622
1623 /* ARGSUSED */
1624 static void
1625 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1626 {
1627 blkptr_t *bp = zio->io_bp;
1628 blkptr_t *bp_orig = &zio->io_bp_orig;
1629 objset_t *os = arg;
1630
1631 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1632 ASSERT(BP_EQUAL(bp, bp_orig));
1633 } else {
1634 dsl_dataset_t *ds = os->os_dsl_dataset;
1635 dmu_tx_t *tx = os->os_synctx;
1636
1637 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1638 dsl_dataset_block_born(ds, bp, tx);
1639 }
1640 kmem_free(bp, sizeof (*bp));
1641 }
1642
1643 typedef struct sync_dnodes_arg {
1644 multilist_t *sda_list;
1645 int sda_sublist_idx;
1646 multilist_t *sda_newlist;
1647 dmu_tx_t *sda_tx;
1648 } sync_dnodes_arg_t;
1649
1650 static void
1651 sync_dnodes_task(void *arg)
1652 {
1653 sync_dnodes_arg_t *sda = arg;
1654
1655 multilist_sublist_t *ms =
1656 multilist_sublist_lock(sda->sda_list, sda->sda_sublist_idx);
1657
1658 dmu_objset_sync_dnodes(ms, sda->sda_tx);
1659
1660 multilist_sublist_unlock(ms);
1661
1662 kmem_free(sda, sizeof (*sda));
1663 }
1664
1665
1666 /* called from dsl */
1667 void
1668 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1669 {
1670 int txgoff;
1671 zbookmark_phys_t zb;
1672 zio_prop_t zp;
1673 zio_t *zio;
1674 list_t *list;
1675 dbuf_dirty_record_t *dr;
1676 blkptr_t *blkptr_copy = kmem_alloc(sizeof (*os->os_rootbp), KM_SLEEP);
1677 *blkptr_copy = *os->os_rootbp;
1678
1679 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1680
1681 ASSERT(dmu_tx_is_syncing(tx));
1682 /* XXX the write_done callback should really give us the tx... */
1683 os->os_synctx = tx;
1684
1685 if (os->os_dsl_dataset == NULL) {
1686 /*
1687 * This is the MOS. If we have upgraded,
1688 * spa_max_replication() could change, so reset
1689 * os_copies here.
1690 */
1691 os->os_copies = spa_max_replication(os->os_spa);
1692 }
1693
1694 /*
1695 * Create the root block IO
1696 */
1697 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1698 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1699 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1700 arc_release(os->os_phys_buf, &os->os_phys_buf);
1701
1702 dmu_write_policy(os, NULL, 0, 0, &zp);
1703
1704 /*
1705 * If we are either claiming the ZIL or doing a raw receive, write
1706 * out the os_phys_buf raw. Neither of these actions will effect the
1707 * MAC at this point.
1708 */
1709 if (os->os_raw_receive ||
1710 os->os_next_write_raw[tx->tx_txg & TXG_MASK]) {
1711 ASSERT(os->os_encrypted);
1712 arc_convert_to_raw(os->os_phys_buf,
1713 os->os_dsl_dataset->ds_object, ZFS_HOST_BYTEORDER,
1714 DMU_OT_OBJSET, NULL, NULL, NULL);
1715 }
1716
1717 zio = arc_write(pio, os->os_spa, tx->tx_txg,
1718 blkptr_copy, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1719 &zp, dmu_objset_write_ready, NULL, NULL, dmu_objset_write_done,
1720 os, ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1721
1722 /*
1723 * Sync special dnodes - the parent IO for the sync is the root block
1724 */
1725 DMU_META_DNODE(os)->dn_zio = zio;
1726 dnode_sync(DMU_META_DNODE(os), tx);
1727
1728 os->os_phys->os_flags = os->os_flags;
1729
1730 if (DMU_USERUSED_DNODE(os) &&
1731 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1732 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1733 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1734 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1735 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1736 }
1737
1738 if (DMU_PROJECTUSED_DNODE(os) &&
1739 DMU_PROJECTUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1740 DMU_PROJECTUSED_DNODE(os)->dn_zio = zio;
1741 dnode_sync(DMU_PROJECTUSED_DNODE(os), tx);
1742 }
1743
1744 txgoff = tx->tx_txg & TXG_MASK;
1745
1746 if (dmu_objset_userused_enabled(os) &&
1747 (!os->os_encrypted || !dmu_objset_is_receiving(os))) {
1748 /*
1749 * We must create the list here because it uses the
1750 * dn_dirty_link[] of this txg. But it may already
1751 * exist because we call dsl_dataset_sync() twice per txg.
1752 */
1753 if (os->os_synced_dnodes == NULL) {
1754 os->os_synced_dnodes =
1755 multilist_create(sizeof (dnode_t),
1756 offsetof(dnode_t, dn_dirty_link[txgoff]),
1757 dnode_multilist_index_func);
1758 } else {
1759 ASSERT3U(os->os_synced_dnodes->ml_offset, ==,
1760 offsetof(dnode_t, dn_dirty_link[txgoff]));
1761 }
1762 }
1763
1764 for (int i = 0;
1765 i < multilist_get_num_sublists(os->os_dirty_dnodes[txgoff]); i++) {
1766 sync_dnodes_arg_t *sda = kmem_alloc(sizeof (*sda), KM_SLEEP);
1767 sda->sda_list = os->os_dirty_dnodes[txgoff];
1768 sda->sda_sublist_idx = i;
1769 sda->sda_tx = tx;
1770 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
1771 sync_dnodes_task, sda, 0);
1772 /* callback frees sda */
1773 }
1774 taskq_wait(dmu_objset_pool(os)->dp_sync_taskq);
1775
1776 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1777 while ((dr = list_head(list)) != NULL) {
1778 ASSERT0(dr->dr_dbuf->db_level);
1779 list_remove(list, dr);
1780 if (dr->dr_zio)
1781 zio_nowait(dr->dr_zio);
1782 }
1783
1784 /* Enable dnode backfill if enough objects have been freed. */
1785 if (os->os_freed_dnodes >= dmu_rescan_dnode_threshold) {
1786 os->os_rescan_dnodes = B_TRUE;
1787 os->os_freed_dnodes = 0;
1788 }
1789
1790 /*
1791 * Free intent log blocks up to this tx.
1792 */
1793 zil_sync(os->os_zil, tx);
1794 os->os_phys->os_zil_header = os->os_zil_header;
1795 zio_nowait(zio);
1796 }
1797
1798 boolean_t
1799 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1800 {
1801 return (!multilist_is_empty(os->os_dirty_dnodes[txg & TXG_MASK]));
1802 }
1803
1804 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1805
1806 void
1807 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1808 {
1809 used_cbs[ost] = cb;
1810 }
1811
1812 boolean_t
1813 dmu_objset_userused_enabled(objset_t *os)
1814 {
1815 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1816 used_cbs[os->os_phys->os_type] != NULL &&
1817 DMU_USERUSED_DNODE(os) != NULL);
1818 }
1819
1820 boolean_t
1821 dmu_objset_userobjused_enabled(objset_t *os)
1822 {
1823 return (dmu_objset_userused_enabled(os) &&
1824 spa_feature_is_enabled(os->os_spa, SPA_FEATURE_USEROBJ_ACCOUNTING));
1825 }
1826
1827 boolean_t
1828 dmu_objset_projectquota_enabled(objset_t *os)
1829 {
1830 return (used_cbs[os->os_phys->os_type] != NULL &&
1831 DMU_PROJECTUSED_DNODE(os) != NULL &&
1832 spa_feature_is_enabled(os->os_spa, SPA_FEATURE_PROJECT_QUOTA));
1833 }
1834
1835 typedef struct userquota_node {
1836 /* must be in the first filed, see userquota_update_cache() */
1837 char uqn_id[20 + DMU_OBJACCT_PREFIX_LEN];
1838 int64_t uqn_delta;
1839 avl_node_t uqn_node;
1840 } userquota_node_t;
1841
1842 typedef struct userquota_cache {
1843 avl_tree_t uqc_user_deltas;
1844 avl_tree_t uqc_group_deltas;
1845 avl_tree_t uqc_project_deltas;
1846 } userquota_cache_t;
1847
1848 static int
1849 userquota_compare(const void *l, const void *r)
1850 {
1851 const userquota_node_t *luqn = l;
1852 const userquota_node_t *ruqn = r;
1853 int rv;
1854
1855 /*
1856 * NB: can only access uqn_id because userquota_update_cache() doesn't
1857 * pass in an entire userquota_node_t.
1858 */
1859 rv = strcmp(luqn->uqn_id, ruqn->uqn_id);
1860
1861 return (AVL_ISIGN(rv));
1862 }
1863
1864 static void
1865 do_userquota_cacheflush(objset_t *os, userquota_cache_t *cache, dmu_tx_t *tx)
1866 {
1867 void *cookie;
1868 userquota_node_t *uqn;
1869
1870 ASSERT(dmu_tx_is_syncing(tx));
1871
1872 cookie = NULL;
1873 while ((uqn = avl_destroy_nodes(&cache->uqc_user_deltas,
1874 &cookie)) != NULL) {
1875 /*
1876 * os_userused_lock protects against concurrent calls to
1877 * zap_increment_int(). It's needed because zap_increment_int()
1878 * is not thread-safe (i.e. not atomic).
1879 */
1880 mutex_enter(&os->os_userused_lock);
1881 VERIFY0(zap_increment(os, DMU_USERUSED_OBJECT,
1882 uqn->uqn_id, uqn->uqn_delta, tx));
1883 mutex_exit(&os->os_userused_lock);
1884 kmem_free(uqn, sizeof (*uqn));
1885 }
1886 avl_destroy(&cache->uqc_user_deltas);
1887
1888 cookie = NULL;
1889 while ((uqn = avl_destroy_nodes(&cache->uqc_group_deltas,
1890 &cookie)) != NULL) {
1891 mutex_enter(&os->os_userused_lock);
1892 VERIFY0(zap_increment(os, DMU_GROUPUSED_OBJECT,
1893 uqn->uqn_id, uqn->uqn_delta, tx));
1894 mutex_exit(&os->os_userused_lock);
1895 kmem_free(uqn, sizeof (*uqn));
1896 }
1897 avl_destroy(&cache->uqc_group_deltas);
1898
1899 if (dmu_objset_projectquota_enabled(os)) {
1900 cookie = NULL;
1901 while ((uqn = avl_destroy_nodes(&cache->uqc_project_deltas,
1902 &cookie)) != NULL) {
1903 mutex_enter(&os->os_userused_lock);
1904 VERIFY0(zap_increment(os, DMU_PROJECTUSED_OBJECT,
1905 uqn->uqn_id, uqn->uqn_delta, tx));
1906 mutex_exit(&os->os_userused_lock);
1907 kmem_free(uqn, sizeof (*uqn));
1908 }
1909 avl_destroy(&cache->uqc_project_deltas);
1910 }
1911 }
1912
1913 static void
1914 userquota_update_cache(avl_tree_t *avl, const char *id, int64_t delta)
1915 {
1916 userquota_node_t *uqn;
1917 avl_index_t idx;
1918
1919 ASSERT(strlen(id) < sizeof (uqn->uqn_id));
1920 /*
1921 * Use id directly for searching because uqn_id is the first field of
1922 * userquota_node_t and fields after uqn_id won't be accessed in
1923 * avl_find().
1924 */
1925 uqn = avl_find(avl, (const void *)id, &idx);
1926 if (uqn == NULL) {
1927 uqn = kmem_zalloc(sizeof (*uqn), KM_SLEEP);
1928 strlcpy(uqn->uqn_id, id, sizeof (uqn->uqn_id));
1929 avl_insert(avl, uqn, idx);
1930 }
1931 uqn->uqn_delta += delta;
1932 }
1933
1934 static void
1935 do_userquota_update(objset_t *os, userquota_cache_t *cache, uint64_t used,
1936 uint64_t flags, uint64_t user, uint64_t group, uint64_t project,
1937 boolean_t subtract)
1938 {
1939 if (flags & DNODE_FLAG_USERUSED_ACCOUNTED) {
1940 int64_t delta = DNODE_MIN_SIZE + used;
1941 char name[20];
1942
1943 if (subtract)
1944 delta = -delta;
1945
1946 (void) sprintf(name, "%llx", (longlong_t)user);
1947 userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1948
1949 (void) sprintf(name, "%llx", (longlong_t)group);
1950 userquota_update_cache(&cache->uqc_group_deltas, name, delta);
1951
1952 if (dmu_objset_projectquota_enabled(os)) {
1953 (void) sprintf(name, "%llx", (longlong_t)project);
1954 userquota_update_cache(&cache->uqc_project_deltas,
1955 name, delta);
1956 }
1957 }
1958 }
1959
1960 static void
1961 do_userobjquota_update(objset_t *os, userquota_cache_t *cache, uint64_t flags,
1962 uint64_t user, uint64_t group, uint64_t project, boolean_t subtract)
1963 {
1964 if (flags & DNODE_FLAG_USEROBJUSED_ACCOUNTED) {
1965 char name[20 + DMU_OBJACCT_PREFIX_LEN];
1966 int delta = subtract ? -1 : 1;
1967
1968 (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1969 (longlong_t)user);
1970 userquota_update_cache(&cache->uqc_user_deltas, name, delta);
1971
1972 (void) snprintf(name, sizeof (name), DMU_OBJACCT_PREFIX "%llx",
1973 (longlong_t)group);
1974 userquota_update_cache(&cache->uqc_group_deltas, name, delta);
1975
1976 if (dmu_objset_projectquota_enabled(os)) {
1977 (void) snprintf(name, sizeof (name),
1978 DMU_OBJACCT_PREFIX "%llx", (longlong_t)project);
1979 userquota_update_cache(&cache->uqc_project_deltas,
1980 name, delta);
1981 }
1982 }
1983 }
1984
1985 typedef struct userquota_updates_arg {
1986 objset_t *uua_os;
1987 int uua_sublist_idx;
1988 dmu_tx_t *uua_tx;
1989 } userquota_updates_arg_t;
1990
1991 static void
1992 userquota_updates_task(void *arg)
1993 {
1994 userquota_updates_arg_t *uua = arg;
1995 objset_t *os = uua->uua_os;
1996 dmu_tx_t *tx = uua->uua_tx;
1997 dnode_t *dn;
1998 userquota_cache_t cache = { { 0 } };
1999
2000 multilist_sublist_t *list =
2001 multilist_sublist_lock(os->os_synced_dnodes, uua->uua_sublist_idx);
2002
2003 ASSERT(multilist_sublist_head(list) == NULL ||
2004 dmu_objset_userused_enabled(os));
2005 avl_create(&cache.uqc_user_deltas, userquota_compare,
2006 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
2007 avl_create(&cache.uqc_group_deltas, userquota_compare,
2008 sizeof (userquota_node_t), offsetof(userquota_node_t, uqn_node));
2009 if (dmu_objset_projectquota_enabled(os))
2010 avl_create(&cache.uqc_project_deltas, userquota_compare,
2011 sizeof (userquota_node_t), offsetof(userquota_node_t,
2012 uqn_node));
2013
2014 while ((dn = multilist_sublist_head(list)) != NULL) {
2015 int flags;
2016 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
2017 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
2018 dn->dn_phys->dn_flags &
2019 DNODE_FLAG_USERUSED_ACCOUNTED);
2020
2021 flags = dn->dn_id_flags;
2022 ASSERT(flags);
2023 if (flags & DN_ID_OLD_EXIST) {
2024 do_userquota_update(os, &cache, dn->dn_oldused,
2025 dn->dn_oldflags, dn->dn_olduid, dn->dn_oldgid,
2026 dn->dn_oldprojid, B_TRUE);
2027 do_userobjquota_update(os, &cache, dn->dn_oldflags,
2028 dn->dn_olduid, dn->dn_oldgid,
2029 dn->dn_oldprojid, B_TRUE);
2030 }
2031 if (flags & DN_ID_NEW_EXIST) {
2032 do_userquota_update(os, &cache,
2033 DN_USED_BYTES(dn->dn_phys), dn->dn_phys->dn_flags,
2034 dn->dn_newuid, dn->dn_newgid,
2035 dn->dn_newprojid, B_FALSE);
2036 do_userobjquota_update(os, &cache,
2037 dn->dn_phys->dn_flags, dn->dn_newuid, dn->dn_newgid,
2038 dn->dn_newprojid, B_FALSE);
2039 }
2040
2041 mutex_enter(&dn->dn_mtx);
2042 dn->dn_oldused = 0;
2043 dn->dn_oldflags = 0;
2044 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
2045 dn->dn_olduid = dn->dn_newuid;
2046 dn->dn_oldgid = dn->dn_newgid;
2047 dn->dn_oldprojid = dn->dn_newprojid;
2048 dn->dn_id_flags |= DN_ID_OLD_EXIST;
2049 if (dn->dn_bonuslen == 0)
2050 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
2051 else
2052 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2053 }
2054 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
2055 if (dn->dn_dirty_txg == spa_syncing_txg(os->os_spa))
2056 dn->dn_dirty_txg = 0;
2057 mutex_exit(&dn->dn_mtx);
2058
2059 multilist_sublist_remove(list, dn);
2060 dnode_rele(dn, os->os_synced_dnodes);
2061 }
2062 do_userquota_cacheflush(os, &cache, tx);
2063 multilist_sublist_unlock(list);
2064 kmem_free(uua, sizeof (*uua));
2065 }
2066
2067 void
2068 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
2069 {
2070 if (!dmu_objset_userused_enabled(os))
2071 return;
2072
2073 /*
2074 * If this is a raw receive just return and handle accounting
2075 * later when we have the keys loaded. We also don't do user
2076 * accounting during claiming since the datasets are not owned
2077 * for the duration of claiming and this txg should only be
2078 * used for recovery.
2079 */
2080 if (os->os_encrypted && dmu_objset_is_receiving(os))
2081 return;
2082
2083 if (tx->tx_txg <= os->os_spa->spa_claim_max_txg)
2084 return;
2085
2086 /* Allocate the user/group/project used objects if necessary. */
2087 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
2088 VERIFY0(zap_create_claim(os,
2089 DMU_USERUSED_OBJECT,
2090 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2091 VERIFY0(zap_create_claim(os,
2092 DMU_GROUPUSED_OBJECT,
2093 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2094 }
2095
2096 if (dmu_objset_projectquota_enabled(os) &&
2097 DMU_PROJECTUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
2098 VERIFY0(zap_create_claim(os, DMU_PROJECTUSED_OBJECT,
2099 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
2100 }
2101
2102 for (int i = 0;
2103 i < multilist_get_num_sublists(os->os_synced_dnodes); i++) {
2104 userquota_updates_arg_t *uua =
2105 kmem_alloc(sizeof (*uua), KM_SLEEP);
2106 uua->uua_os = os;
2107 uua->uua_sublist_idx = i;
2108 uua->uua_tx = tx;
2109 /* note: caller does taskq_wait() */
2110 (void) taskq_dispatch(dmu_objset_pool(os)->dp_sync_taskq,
2111 userquota_updates_task, uua, 0);
2112 /* callback frees uua */
2113 }
2114 }
2115
2116 /*
2117 * Returns a pointer to data to find uid/gid from
2118 *
2119 * If a dirty record for transaction group that is syncing can't
2120 * be found then NULL is returned. In the NULL case it is assumed
2121 * the uid/gid aren't changing.
2122 */
2123 static void *
2124 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
2125 {
2126 dbuf_dirty_record_t *dr, **drp;
2127 void *data;
2128
2129 if (db->db_dirtycnt == 0)
2130 return (db->db.db_data); /* Nothing is changing */
2131
2132 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
2133 if (dr->dr_txg == tx->tx_txg)
2134 break;
2135
2136 if (dr == NULL) {
2137 data = NULL;
2138 } else {
2139 dnode_t *dn;
2140
2141 DB_DNODE_ENTER(dr->dr_dbuf);
2142 dn = DB_DNODE(dr->dr_dbuf);
2143
2144 if (dn->dn_bonuslen == 0 &&
2145 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
2146 data = dr->dt.dl.dr_data->b_data;
2147 else
2148 data = dr->dt.dl.dr_data;
2149
2150 DB_DNODE_EXIT(dr->dr_dbuf);
2151 }
2152
2153 return (data);
2154 }
2155
2156 void
2157 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
2158 {
2159 objset_t *os = dn->dn_objset;
2160 void *data = NULL;
2161 dmu_buf_impl_t *db = NULL;
2162 uint64_t *user = NULL;
2163 uint64_t *group = NULL;
2164 uint64_t *project = NULL;
2165 int flags = dn->dn_id_flags;
2166 int error;
2167 boolean_t have_spill = B_FALSE;
2168
2169 if (!dmu_objset_userused_enabled(dn->dn_objset))
2170 return;
2171
2172 /*
2173 * Raw receives introduce a problem with user accounting. Raw
2174 * receives cannot update the user accounting info because the
2175 * user ids and the sizes are encrypted. To guarantee that we
2176 * never end up with bad user accounting, we simply disable it
2177 * during raw receives. We also disable this for normal receives
2178 * so that an incremental raw receive may be done on top of an
2179 * existing non-raw receive.
2180 */
2181 if (os->os_encrypted && dmu_objset_is_receiving(os))
2182 return;
2183
2184 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
2185 DN_ID_CHKED_SPILL)))
2186 return;
2187
2188 if (before && dn->dn_bonuslen != 0)
2189 data = DN_BONUS(dn->dn_phys);
2190 else if (!before && dn->dn_bonuslen != 0) {
2191 if (dn->dn_bonus) {
2192 db = dn->dn_bonus;
2193 mutex_enter(&db->db_mtx);
2194 data = dmu_objset_userquota_find_data(db, tx);
2195 } else {
2196 data = DN_BONUS(dn->dn_phys);
2197 }
2198 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
2199 int rf = 0;
2200
2201 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
2202 rf |= DB_RF_HAVESTRUCT;
2203 error = dmu_spill_hold_by_dnode(dn,
2204 rf | DB_RF_MUST_SUCCEED,
2205 FTAG, (dmu_buf_t **)&db);
2206 ASSERT(error == 0);
2207 mutex_enter(&db->db_mtx);
2208 data = (before) ? db->db.db_data :
2209 dmu_objset_userquota_find_data(db, tx);
2210 have_spill = B_TRUE;
2211 } else {
2212 mutex_enter(&dn->dn_mtx);
2213 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2214 mutex_exit(&dn->dn_mtx);
2215 return;
2216 }
2217
2218 if (before) {
2219 ASSERT(data);
2220 user = &dn->dn_olduid;
2221 group = &dn->dn_oldgid;
2222 project = &dn->dn_oldprojid;
2223 } else if (data) {
2224 user = &dn->dn_newuid;
2225 group = &dn->dn_newgid;
2226 project = &dn->dn_newprojid;
2227 }
2228
2229 /*
2230 * Must always call the callback in case the object
2231 * type has changed and that type isn't an object type to track
2232 */
2233 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
2234 user, group, project);
2235
2236 /*
2237 * Preserve existing uid/gid when the callback can't determine
2238 * what the new uid/gid are and the callback returned EEXIST.
2239 * The EEXIST error tells us to just use the existing uid/gid.
2240 * If we don't know what the old values are then just assign
2241 * them to 0, since that is a new file being created.
2242 */
2243 if (!before && data == NULL && error == EEXIST) {
2244 if (flags & DN_ID_OLD_EXIST) {
2245 dn->dn_newuid = dn->dn_olduid;
2246 dn->dn_newgid = dn->dn_oldgid;
2247 dn->dn_newprojid = dn->dn_oldprojid;
2248 } else {
2249 dn->dn_newuid = 0;
2250 dn->dn_newgid = 0;
2251 dn->dn_newprojid = ZFS_DEFAULT_PROJID;
2252 }
2253 error = 0;
2254 }
2255
2256 if (db)
2257 mutex_exit(&db->db_mtx);
2258
2259 mutex_enter(&dn->dn_mtx);
2260 if (error == 0 && before)
2261 dn->dn_id_flags |= DN_ID_OLD_EXIST;
2262 if (error == 0 && !before)
2263 dn->dn_id_flags |= DN_ID_NEW_EXIST;
2264
2265 if (have_spill) {
2266 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
2267 } else {
2268 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
2269 }
2270 mutex_exit(&dn->dn_mtx);
2271 if (have_spill)
2272 dmu_buf_rele((dmu_buf_t *)db, FTAG);
2273 }
2274
2275 boolean_t
2276 dmu_objset_userspace_present(objset_t *os)
2277 {
2278 return (os->os_phys->os_flags &
2279 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
2280 }
2281
2282 boolean_t
2283 dmu_objset_userobjspace_present(objset_t *os)
2284 {
2285 return (os->os_phys->os_flags &
2286 OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE);
2287 }
2288
2289 boolean_t
2290 dmu_objset_projectquota_present(objset_t *os)
2291 {
2292 return (os->os_phys->os_flags &
2293 OBJSET_FLAG_PROJECTQUOTA_COMPLETE);
2294 }
2295
2296 static int
2297 dmu_objset_space_upgrade(objset_t *os)
2298 {
2299 uint64_t obj;
2300 int err = 0;
2301
2302 /*
2303 * We simply need to mark every object dirty, so that it will be
2304 * synced out and now accounted. If this is called
2305 * concurrently, or if we already did some work before crashing,
2306 * that's fine, since we track each object's accounted state
2307 * independently.
2308 */
2309
2310 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
2311 dmu_tx_t *tx;
2312 dmu_buf_t *db;
2313 int objerr;
2314
2315 mutex_enter(&os->os_upgrade_lock);
2316 if (os->os_upgrade_exit)
2317 err = SET_ERROR(EINTR);
2318 mutex_exit(&os->os_upgrade_lock);
2319 if (err != 0)
2320 return (err);
2321
2322 if (issig(JUSTLOOKING) && issig(FORREAL))
2323 return (SET_ERROR(EINTR));
2324
2325 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
2326 if (objerr != 0)
2327 continue;
2328 tx = dmu_tx_create(os);
2329 dmu_tx_hold_bonus(tx, obj);
2330 objerr = dmu_tx_assign(tx, TXG_WAIT);
2331 if (objerr != 0) {
2332 dmu_buf_rele(db, FTAG);
2333 dmu_tx_abort(tx);
2334 continue;
2335 }
2336 dmu_buf_will_dirty(db, tx);
2337 dmu_buf_rele(db, FTAG);
2338 dmu_tx_commit(tx);
2339 }
2340 return (0);
2341 }
2342
2343 int
2344 dmu_objset_userspace_upgrade(objset_t *os)
2345 {
2346 int err = 0;
2347
2348 if (dmu_objset_userspace_present(os))
2349 return (0);
2350 if (dmu_objset_is_snapshot(os))
2351 return (SET_ERROR(EINVAL));
2352 if (!dmu_objset_userused_enabled(os))
2353 return (SET_ERROR(ENOTSUP));
2354
2355 err = dmu_objset_space_upgrade(os);
2356 if (err)
2357 return (err);
2358
2359 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
2360 txg_wait_synced(dmu_objset_pool(os), 0);
2361 return (0);
2362 }
2363
2364 static int
2365 dmu_objset_id_quota_upgrade_cb(objset_t *os)
2366 {
2367 int err = 0;
2368
2369 if (dmu_objset_userobjspace_present(os) &&
2370 dmu_objset_projectquota_present(os))
2371 return (0);
2372 if (dmu_objset_is_snapshot(os))
2373 return (SET_ERROR(EINVAL));
2374 if (!dmu_objset_userobjused_enabled(os))
2375 return (SET_ERROR(ENOTSUP));
2376 if (!dmu_objset_projectquota_enabled(os) &&
2377 dmu_objset_userobjspace_present(os))
2378 return (SET_ERROR(ENOTSUP));
2379
2380 dmu_objset_ds(os)->ds_feature_activation[
2381 SPA_FEATURE_USEROBJ_ACCOUNTING] = (void *)B_TRUE;
2382 if (dmu_objset_projectquota_enabled(os))
2383 dmu_objset_ds(os)->ds_feature_activation[
2384 SPA_FEATURE_PROJECT_QUOTA] = (void *)B_TRUE;
2385
2386 err = dmu_objset_space_upgrade(os);
2387 if (err)
2388 return (err);
2389
2390 os->os_flags |= OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE;
2391 if (dmu_objset_projectquota_enabled(os))
2392 os->os_flags |= OBJSET_FLAG_PROJECTQUOTA_COMPLETE;
2393
2394 txg_wait_synced(dmu_objset_pool(os), 0);
2395 return (0);
2396 }
2397
2398 void
2399 dmu_objset_id_quota_upgrade(objset_t *os)
2400 {
2401 dmu_objset_upgrade(os, dmu_objset_id_quota_upgrade_cb);
2402 }
2403
2404 boolean_t
2405 dmu_objset_userobjspace_upgradable(objset_t *os)
2406 {
2407 return (dmu_objset_type(os) == DMU_OST_ZFS &&
2408 !dmu_objset_is_snapshot(os) &&
2409 dmu_objset_userobjused_enabled(os) &&
2410 !dmu_objset_userobjspace_present(os));
2411 }
2412
2413 boolean_t
2414 dmu_objset_projectquota_upgradable(objset_t *os)
2415 {
2416 return (dmu_objset_type(os) == DMU_OST_ZFS &&
2417 !dmu_objset_is_snapshot(os) &&
2418 dmu_objset_projectquota_enabled(os) &&
2419 !dmu_objset_projectquota_present(os));
2420 }
2421
2422 void
2423 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
2424 uint64_t *usedobjsp, uint64_t *availobjsp)
2425 {
2426 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
2427 usedobjsp, availobjsp);
2428 }
2429
2430 uint64_t
2431 dmu_objset_fsid_guid(objset_t *os)
2432 {
2433 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
2434 }
2435
2436 void
2437 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
2438 {
2439 stat->dds_type = os->os_phys->os_type;
2440 if (os->os_dsl_dataset)
2441 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
2442 }
2443
2444 void
2445 dmu_objset_stats(objset_t *os, nvlist_t *nv)
2446 {
2447 ASSERT(os->os_dsl_dataset ||
2448 os->os_phys->os_type == DMU_OST_META);
2449
2450 if (os->os_dsl_dataset != NULL)
2451 dsl_dataset_stats(os->os_dsl_dataset, nv);
2452
2453 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
2454 os->os_phys->os_type);
2455 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
2456 dmu_objset_userspace_present(os));
2457 }
2458
2459 int
2460 dmu_objset_is_snapshot(objset_t *os)
2461 {
2462 if (os->os_dsl_dataset != NULL)
2463 return (os->os_dsl_dataset->ds_is_snapshot);
2464 else
2465 return (B_FALSE);
2466 }
2467
2468 int
2469 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
2470 boolean_t *conflict)
2471 {
2472 dsl_dataset_t *ds = os->os_dsl_dataset;
2473 uint64_t ignored;
2474
2475 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2476 return (SET_ERROR(ENOENT));
2477
2478 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
2479 dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
2480 MT_NORMALIZE, real, maxlen, conflict));
2481 }
2482
2483 int
2484 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
2485 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
2486 {
2487 dsl_dataset_t *ds = os->os_dsl_dataset;
2488 zap_cursor_t cursor;
2489 zap_attribute_t attr;
2490
2491 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
2492
2493 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
2494 return (SET_ERROR(ENOENT));
2495
2496 zap_cursor_init_serialized(&cursor,
2497 ds->ds_dir->dd_pool->dp_meta_objset,
2498 dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
2499
2500 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2501 zap_cursor_fini(&cursor);
2502 return (SET_ERROR(ENOENT));
2503 }
2504
2505 if (strlen(attr.za_name) + 1 > namelen) {
2506 zap_cursor_fini(&cursor);
2507 return (SET_ERROR(ENAMETOOLONG));
2508 }
2509
2510 (void) strcpy(name, attr.za_name);
2511 if (idp)
2512 *idp = attr.za_first_integer;
2513 if (case_conflict)
2514 *case_conflict = attr.za_normalization_conflict;
2515 zap_cursor_advance(&cursor);
2516 *offp = zap_cursor_serialize(&cursor);
2517 zap_cursor_fini(&cursor);
2518
2519 return (0);
2520 }
2521
2522 int
2523 dmu_snapshot_lookup(objset_t *os, const char *name, uint64_t *value)
2524 {
2525 return (dsl_dataset_snap_lookup(os->os_dsl_dataset, name, value));
2526 }
2527
2528 int
2529 dmu_dir_list_next(objset_t *os, int namelen, char *name,
2530 uint64_t *idp, uint64_t *offp)
2531 {
2532 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
2533 zap_cursor_t cursor;
2534 zap_attribute_t attr;
2535
2536 /* there is no next dir on a snapshot! */
2537 if (os->os_dsl_dataset->ds_object !=
2538 dsl_dir_phys(dd)->dd_head_dataset_obj)
2539 return (SET_ERROR(ENOENT));
2540
2541 zap_cursor_init_serialized(&cursor,
2542 dd->dd_pool->dp_meta_objset,
2543 dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
2544
2545 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
2546 zap_cursor_fini(&cursor);
2547 return (SET_ERROR(ENOENT));
2548 }
2549
2550 if (strlen(attr.za_name) + 1 > namelen) {
2551 zap_cursor_fini(&cursor);
2552 return (SET_ERROR(ENAMETOOLONG));
2553 }
2554
2555 (void) strcpy(name, attr.za_name);
2556 if (idp)
2557 *idp = attr.za_first_integer;
2558 zap_cursor_advance(&cursor);
2559 *offp = zap_cursor_serialize(&cursor);
2560 zap_cursor_fini(&cursor);
2561
2562 return (0);
2563 }
2564
2565 typedef struct dmu_objset_find_ctx {
2566 taskq_t *dc_tq;
2567 dsl_pool_t *dc_dp;
2568 uint64_t dc_ddobj;
2569 char *dc_ddname; /* last component of ddobj's name */
2570 int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
2571 void *dc_arg;
2572 int dc_flags;
2573 kmutex_t *dc_error_lock;
2574 int *dc_error;
2575 } dmu_objset_find_ctx_t;
2576
2577 static void
2578 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
2579 {
2580 dsl_pool_t *dp = dcp->dc_dp;
2581 dsl_dir_t *dd;
2582 dsl_dataset_t *ds;
2583 zap_cursor_t zc;
2584 zap_attribute_t *attr;
2585 uint64_t thisobj;
2586 int err = 0;
2587
2588 /* don't process if there already was an error */
2589 if (*dcp->dc_error != 0)
2590 goto out;
2591
2592 /*
2593 * Note: passing the name (dc_ddname) here is optional, but it
2594 * improves performance because we don't need to call
2595 * zap_value_search() to determine the name.
2596 */
2597 err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, dcp->dc_ddname, FTAG, &dd);
2598 if (err != 0)
2599 goto out;
2600
2601 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2602 if (dd->dd_myname[0] == '$') {
2603 dsl_dir_rele(dd, FTAG);
2604 goto out;
2605 }
2606
2607 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2608 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2609
2610 /*
2611 * Iterate over all children.
2612 */
2613 if (dcp->dc_flags & DS_FIND_CHILDREN) {
2614 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2615 dsl_dir_phys(dd)->dd_child_dir_zapobj);
2616 zap_cursor_retrieve(&zc, attr) == 0;
2617 (void) zap_cursor_advance(&zc)) {
2618 ASSERT3U(attr->za_integer_length, ==,
2619 sizeof (uint64_t));
2620 ASSERT3U(attr->za_num_integers, ==, 1);
2621
2622 dmu_objset_find_ctx_t *child_dcp =
2623 kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
2624 *child_dcp = *dcp;
2625 child_dcp->dc_ddobj = attr->za_first_integer;
2626 child_dcp->dc_ddname = spa_strdup(attr->za_name);
2627 if (dcp->dc_tq != NULL)
2628 (void) taskq_dispatch(dcp->dc_tq,
2629 dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
2630 else
2631 dmu_objset_find_dp_impl(child_dcp);
2632 }
2633 zap_cursor_fini(&zc);
2634 }
2635
2636 /*
2637 * Iterate over all snapshots.
2638 */
2639 if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
2640 dsl_dataset_t *ds;
2641 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2642
2643 if (err == 0) {
2644 uint64_t snapobj;
2645
2646 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2647 dsl_dataset_rele(ds, FTAG);
2648
2649 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2650 zap_cursor_retrieve(&zc, attr) == 0;
2651 (void) zap_cursor_advance(&zc)) {
2652 ASSERT3U(attr->za_integer_length, ==,
2653 sizeof (uint64_t));
2654 ASSERT3U(attr->za_num_integers, ==, 1);
2655
2656 err = dsl_dataset_hold_obj(dp,
2657 attr->za_first_integer, FTAG, &ds);
2658 if (err != 0)
2659 break;
2660 err = dcp->dc_func(dp, ds, dcp->dc_arg);
2661 dsl_dataset_rele(ds, FTAG);
2662 if (err != 0)
2663 break;
2664 }
2665 zap_cursor_fini(&zc);
2666 }
2667 }
2668
2669 kmem_free(attr, sizeof (zap_attribute_t));
2670
2671 if (err != 0) {
2672 dsl_dir_rele(dd, FTAG);
2673 goto out;
2674 }
2675
2676 /*
2677 * Apply to self.
2678 */
2679 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2680
2681 /*
2682 * Note: we hold the dir while calling dsl_dataset_hold_obj() so
2683 * that the dir will remain cached, and we won't have to re-instantiate
2684 * it (which could be expensive due to finding its name via
2685 * zap_value_search()).
2686 */
2687 dsl_dir_rele(dd, FTAG);
2688 if (err != 0)
2689 goto out;
2690 err = dcp->dc_func(dp, ds, dcp->dc_arg);
2691 dsl_dataset_rele(ds, FTAG);
2692
2693 out:
2694 if (err != 0) {
2695 mutex_enter(dcp->dc_error_lock);
2696 /* only keep first error */
2697 if (*dcp->dc_error == 0)
2698 *dcp->dc_error = err;
2699 mutex_exit(dcp->dc_error_lock);
2700 }
2701
2702 if (dcp->dc_ddname != NULL)
2703 spa_strfree(dcp->dc_ddname);
2704 kmem_free(dcp, sizeof (*dcp));
2705 }
2706
2707 static void
2708 dmu_objset_find_dp_cb(void *arg)
2709 {
2710 dmu_objset_find_ctx_t *dcp = arg;
2711 dsl_pool_t *dp = dcp->dc_dp;
2712
2713 /*
2714 * We need to get a pool_config_lock here, as there are several
2715 * asssert(pool_config_held) down the stack. Getting a lock via
2716 * dsl_pool_config_enter is risky, as it might be stalled by a
2717 * pending writer. This would deadlock, as the write lock can
2718 * only be granted when our parent thread gives up the lock.
2719 * The _prio interface gives us priority over a pending writer.
2720 */
2721 dsl_pool_config_enter_prio(dp, FTAG);
2722
2723 dmu_objset_find_dp_impl(dcp);
2724
2725 dsl_pool_config_exit(dp, FTAG);
2726 }
2727
2728 /*
2729 * Find objsets under and including ddobj, call func(ds) on each.
2730 * The order for the enumeration is completely undefined.
2731 * func is called with dsl_pool_config held.
2732 */
2733 int
2734 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
2735 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
2736 {
2737 int error = 0;
2738 taskq_t *tq = NULL;
2739 int ntasks;
2740 dmu_objset_find_ctx_t *dcp;
2741 kmutex_t err_lock;
2742
2743 mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
2744 dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
2745 dcp->dc_tq = NULL;
2746 dcp->dc_dp = dp;
2747 dcp->dc_ddobj = ddobj;
2748 dcp->dc_ddname = NULL;
2749 dcp->dc_func = func;
2750 dcp->dc_arg = arg;
2751 dcp->dc_flags = flags;
2752 dcp->dc_error_lock = &err_lock;
2753 dcp->dc_error = &error;
2754
2755 if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
2756 /*
2757 * In case a write lock is held we can't make use of
2758 * parallelism, as down the stack of the worker threads
2759 * the lock is asserted via dsl_pool_config_held.
2760 * In case of a read lock this is solved by getting a read
2761 * lock in each worker thread, which isn't possible in case
2762 * of a writer lock. So we fall back to the synchronous path
2763 * here.
2764 * In the future it might be possible to get some magic into
2765 * dsl_pool_config_held in a way that it returns true for
2766 * the worker threads so that a single lock held from this
2767 * thread suffices. For now, stay single threaded.
2768 */
2769 dmu_objset_find_dp_impl(dcp);
2770 mutex_destroy(&err_lock);
2771
2772 return (error);
2773 }
2774
2775 ntasks = dmu_find_threads;
2776 if (ntasks == 0)
2777 ntasks = vdev_count_leaves(dp->dp_spa) * 4;
2778 tq = taskq_create("dmu_objset_find", ntasks, maxclsyspri, ntasks,
2779 INT_MAX, 0);
2780 if (tq == NULL) {
2781 kmem_free(dcp, sizeof (*dcp));
2782 mutex_destroy(&err_lock);
2783
2784 return (SET_ERROR(ENOMEM));
2785 }
2786 dcp->dc_tq = tq;
2787
2788 /* dcp will be freed by task */
2789 (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
2790
2791 /*
2792 * PORTING: this code relies on the property of taskq_wait to wait
2793 * until no more tasks are queued and no more tasks are active. As
2794 * we always queue new tasks from within other tasks, task_wait
2795 * reliably waits for the full recursion to finish, even though we
2796 * enqueue new tasks after taskq_wait has been called.
2797 * On platforms other than illumos, taskq_wait may not have this
2798 * property.
2799 */
2800 taskq_wait(tq);
2801 taskq_destroy(tq);
2802 mutex_destroy(&err_lock);
2803
2804 return (error);
2805 }
2806
2807 /*
2808 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
2809 * The dp_config_rwlock must not be held when this is called, and it
2810 * will not be held when the callback is called.
2811 * Therefore this function should only be used when the pool is not changing
2812 * (e.g. in syncing context), or the callback can deal with the possible races.
2813 */
2814 static int
2815 dmu_objset_find_impl(spa_t *spa, const char *name,
2816 int func(const char *, void *), void *arg, int flags)
2817 {
2818 dsl_dir_t *dd;
2819 dsl_pool_t *dp = spa_get_dsl(spa);
2820 dsl_dataset_t *ds;
2821 zap_cursor_t zc;
2822 zap_attribute_t *attr;
2823 char *child;
2824 uint64_t thisobj;
2825 int err;
2826
2827 dsl_pool_config_enter(dp, FTAG);
2828
2829 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
2830 if (err != 0) {
2831 dsl_pool_config_exit(dp, FTAG);
2832 return (err);
2833 }
2834
2835 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
2836 if (dd->dd_myname[0] == '$') {
2837 dsl_dir_rele(dd, FTAG);
2838 dsl_pool_config_exit(dp, FTAG);
2839 return (0);
2840 }
2841
2842 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
2843 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
2844
2845 /*
2846 * Iterate over all children.
2847 */
2848 if (flags & DS_FIND_CHILDREN) {
2849 for (zap_cursor_init(&zc, dp->dp_meta_objset,
2850 dsl_dir_phys(dd)->dd_child_dir_zapobj);
2851 zap_cursor_retrieve(&zc, attr) == 0;
2852 (void) zap_cursor_advance(&zc)) {
2853 ASSERT3U(attr->za_integer_length, ==,
2854 sizeof (uint64_t));
2855 ASSERT3U(attr->za_num_integers, ==, 1);
2856
2857 child = kmem_asprintf("%s/%s", name, attr->za_name);
2858 dsl_pool_config_exit(dp, FTAG);
2859 err = dmu_objset_find_impl(spa, child,
2860 func, arg, flags);
2861 dsl_pool_config_enter(dp, FTAG);
2862 strfree(child);
2863 if (err != 0)
2864 break;
2865 }
2866 zap_cursor_fini(&zc);
2867
2868 if (err != 0) {
2869 dsl_dir_rele(dd, FTAG);
2870 dsl_pool_config_exit(dp, FTAG);
2871 kmem_free(attr, sizeof (zap_attribute_t));
2872 return (err);
2873 }
2874 }
2875
2876 /*
2877 * Iterate over all snapshots.
2878 */
2879 if (flags & DS_FIND_SNAPSHOTS) {
2880 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
2881
2882 if (err == 0) {
2883 uint64_t snapobj;
2884
2885 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
2886 dsl_dataset_rele(ds, FTAG);
2887
2888 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
2889 zap_cursor_retrieve(&zc, attr) == 0;
2890 (void) zap_cursor_advance(&zc)) {
2891 ASSERT3U(attr->za_integer_length, ==,
2892 sizeof (uint64_t));
2893 ASSERT3U(attr->za_num_integers, ==, 1);
2894
2895 child = kmem_asprintf("%s@%s",
2896 name, attr->za_name);
2897 dsl_pool_config_exit(dp, FTAG);
2898 err = func(child, arg);
2899 dsl_pool_config_enter(dp, FTAG);
2900 strfree(child);
2901 if (err != 0)
2902 break;
2903 }
2904 zap_cursor_fini(&zc);
2905 }
2906 }
2907
2908 dsl_dir_rele(dd, FTAG);
2909 kmem_free(attr, sizeof (zap_attribute_t));
2910 dsl_pool_config_exit(dp, FTAG);
2911
2912 if (err != 0)
2913 return (err);
2914
2915 /* Apply to self. */
2916 return (func(name, arg));
2917 }
2918
2919 /*
2920 * See comment above dmu_objset_find_impl().
2921 */
2922 int
2923 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
2924 int flags)
2925 {
2926 spa_t *spa;
2927 int error;
2928
2929 error = spa_open(name, &spa, FTAG);
2930 if (error != 0)
2931 return (error);
2932 error = dmu_objset_find_impl(spa, name, func, arg, flags);
2933 spa_close(spa, FTAG);
2934 return (error);
2935 }
2936
2937 boolean_t
2938 dmu_objset_incompatible_encryption_version(objset_t *os)
2939 {
2940 return (dsl_dir_incompatible_encryption_version(
2941 os->os_dsl_dataset->ds_dir));
2942 }
2943
2944 void
2945 dmu_objset_set_user(objset_t *os, void *user_ptr)
2946 {
2947 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2948 os->os_user_ptr = user_ptr;
2949 }
2950
2951 void *
2952 dmu_objset_get_user(objset_t *os)
2953 {
2954 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
2955 return (os->os_user_ptr);
2956 }
2957
2958 /*
2959 * Determine name of filesystem, given name of snapshot.
2960 * buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes
2961 */
2962 int
2963 dmu_fsname(const char *snapname, char *buf)
2964 {
2965 char *atp = strchr(snapname, '@');
2966 if (atp == NULL)
2967 return (SET_ERROR(EINVAL));
2968 if (atp - snapname >= ZFS_MAX_DATASET_NAME_LEN)
2969 return (SET_ERROR(ENAMETOOLONG));
2970 (void) strlcpy(buf, snapname, atp - snapname + 1);
2971 return (0);
2972 }
2973
2974 /*
2975 * Call when we think we're going to write/free space in open context to track
2976 * the amount of dirty data in the open txg, which is also the amount
2977 * of memory that can not be evicted until this txg syncs.
2978 */
2979 void
2980 dmu_objset_willuse_space(objset_t *os, int64_t space, dmu_tx_t *tx)
2981 {
2982 dsl_dataset_t *ds = os->os_dsl_dataset;
2983 int64_t aspace = spa_get_worst_case_asize(os->os_spa, space);
2984
2985 if (ds != NULL) {
2986 dsl_dir_willuse_space(ds->ds_dir, aspace, tx);
2987 dsl_pool_dirty_space(dmu_tx_pool(tx), space, tx);
2988 }
2989 }
2990
2991 #if defined(_KERNEL)
2992 EXPORT_SYMBOL(dmu_objset_zil);
2993 EXPORT_SYMBOL(dmu_objset_pool);
2994 EXPORT_SYMBOL(dmu_objset_ds);
2995 EXPORT_SYMBOL(dmu_objset_type);
2996 EXPORT_SYMBOL(dmu_objset_name);
2997 EXPORT_SYMBOL(dmu_objset_hold);
2998 EXPORT_SYMBOL(dmu_objset_hold_flags);
2999 EXPORT_SYMBOL(dmu_objset_own);
3000 EXPORT_SYMBOL(dmu_objset_rele);
3001 EXPORT_SYMBOL(dmu_objset_rele_flags);
3002 EXPORT_SYMBOL(dmu_objset_disown);
3003 EXPORT_SYMBOL(dmu_objset_from_ds);
3004 EXPORT_SYMBOL(dmu_objset_create);
3005 EXPORT_SYMBOL(dmu_objset_clone);
3006 EXPORT_SYMBOL(dmu_objset_stats);
3007 EXPORT_SYMBOL(dmu_objset_fast_stat);
3008 EXPORT_SYMBOL(dmu_objset_spa);
3009 EXPORT_SYMBOL(dmu_objset_space);
3010 EXPORT_SYMBOL(dmu_objset_fsid_guid);
3011 EXPORT_SYMBOL(dmu_objset_find);
3012 EXPORT_SYMBOL(dmu_objset_byteswap);
3013 EXPORT_SYMBOL(dmu_objset_evict_dbufs);
3014 EXPORT_SYMBOL(dmu_objset_snap_cmtime);
3015 EXPORT_SYMBOL(dmu_objset_dnodesize);
3016
3017 EXPORT_SYMBOL(dmu_objset_sync);
3018 EXPORT_SYMBOL(dmu_objset_is_dirty);
3019 EXPORT_SYMBOL(dmu_objset_create_impl_dnstats);
3020 EXPORT_SYMBOL(dmu_objset_create_impl);
3021 EXPORT_SYMBOL(dmu_objset_open_impl);
3022 EXPORT_SYMBOL(dmu_objset_evict);
3023 EXPORT_SYMBOL(dmu_objset_register_type);
3024 EXPORT_SYMBOL(dmu_objset_do_userquota_updates);
3025 EXPORT_SYMBOL(dmu_objset_userquota_get_ids);
3026 EXPORT_SYMBOL(dmu_objset_userused_enabled);
3027 EXPORT_SYMBOL(dmu_objset_userspace_upgrade);
3028 EXPORT_SYMBOL(dmu_objset_userspace_present);
3029 EXPORT_SYMBOL(dmu_objset_userobjused_enabled);
3030 EXPORT_SYMBOL(dmu_objset_userobjspace_upgradable);
3031 EXPORT_SYMBOL(dmu_objset_userobjspace_present);
3032 EXPORT_SYMBOL(dmu_objset_projectquota_enabled);
3033 EXPORT_SYMBOL(dmu_objset_projectquota_present);
3034 EXPORT_SYMBOL(dmu_objset_projectquota_upgradable);
3035 EXPORT_SYMBOL(dmu_objset_id_quota_upgrade);
3036 #endif