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