]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/dmu_objset.c
Add support for asynchronous zvol minor operations
[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 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright (c) 2015 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2015, STRATO AG, Inc. All rights reserved.
29 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
30 */
31
32 /* Portions Copyright 2010 Robert Milkowski */
33
34 #include <sys/cred.h>
35 #include <sys/zfs_context.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dsl_dataset.h>
39 #include <sys/dsl_prop.h>
40 #include <sys/dsl_pool.h>
41 #include <sys/dsl_synctask.h>
42 #include <sys/dsl_deleg.h>
43 #include <sys/dnode.h>
44 #include <sys/dbuf.h>
45 #include <sys/zvol.h>
46 #include <sys/dmu_tx.h>
47 #include <sys/zap.h>
48 #include <sys/zil.h>
49 #include <sys/dmu_impl.h>
50 #include <sys/zfs_ioctl.h>
51 #include <sys/sa.h>
52 #include <sys/zfs_onexit.h>
53 #include <sys/dsl_destroy.h>
54 #include <sys/vdev.h>
55
56 /*
57 * Needed to close a window in dnode_move() that allows the objset to be freed
58 * before it can be safely accessed.
59 */
60 krwlock_t os_lock;
61
62 /*
63 * Tunable to overwrite the maximum number of threads for the parallization
64 * of dmu_objset_find_dp, needed to speed up the import of pools with many
65 * datasets.
66 * Default is 4 times the number of leaf vdevs.
67 */
68 int dmu_find_threads = 0;
69
70 static void dmu_objset_find_dp_cb(void *arg);
71
72 void
73 dmu_objset_init(void)
74 {
75 rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
76 }
77
78 void
79 dmu_objset_fini(void)
80 {
81 rw_destroy(&os_lock);
82 }
83
84 spa_t *
85 dmu_objset_spa(objset_t *os)
86 {
87 return (os->os_spa);
88 }
89
90 zilog_t *
91 dmu_objset_zil(objset_t *os)
92 {
93 return (os->os_zil);
94 }
95
96 dsl_pool_t *
97 dmu_objset_pool(objset_t *os)
98 {
99 dsl_dataset_t *ds;
100
101 if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
102 return (ds->ds_dir->dd_pool);
103 else
104 return (spa_get_dsl(os->os_spa));
105 }
106
107 dsl_dataset_t *
108 dmu_objset_ds(objset_t *os)
109 {
110 return (os->os_dsl_dataset);
111 }
112
113 dmu_objset_type_t
114 dmu_objset_type(objset_t *os)
115 {
116 return (os->os_phys->os_type);
117 }
118
119 void
120 dmu_objset_name(objset_t *os, char *buf)
121 {
122 dsl_dataset_name(os->os_dsl_dataset, buf);
123 }
124
125 uint64_t
126 dmu_objset_id(objset_t *os)
127 {
128 dsl_dataset_t *ds = os->os_dsl_dataset;
129
130 return (ds ? ds->ds_object : 0);
131 }
132
133 zfs_sync_type_t
134 dmu_objset_syncprop(objset_t *os)
135 {
136 return (os->os_sync);
137 }
138
139 zfs_logbias_op_t
140 dmu_objset_logbias(objset_t *os)
141 {
142 return (os->os_logbias);
143 }
144
145 static void
146 checksum_changed_cb(void *arg, uint64_t newval)
147 {
148 objset_t *os = arg;
149
150 /*
151 * Inheritance should have been done by now.
152 */
153 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
154
155 os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
156 }
157
158 static void
159 compression_changed_cb(void *arg, uint64_t newval)
160 {
161 objset_t *os = arg;
162
163 /*
164 * Inheritance and range checking should have been done by now.
165 */
166 ASSERT(newval != ZIO_COMPRESS_INHERIT);
167
168 os->os_compress = zio_compress_select(os->os_spa, newval,
169 ZIO_COMPRESS_ON);
170 }
171
172 static void
173 copies_changed_cb(void *arg, uint64_t newval)
174 {
175 objset_t *os = arg;
176
177 /*
178 * Inheritance and range checking should have been done by now.
179 */
180 ASSERT(newval > 0);
181 ASSERT(newval <= spa_max_replication(os->os_spa));
182
183 os->os_copies = newval;
184 }
185
186 static void
187 dedup_changed_cb(void *arg, uint64_t newval)
188 {
189 objset_t *os = arg;
190 spa_t *spa = os->os_spa;
191 enum zio_checksum checksum;
192
193 /*
194 * Inheritance should have been done by now.
195 */
196 ASSERT(newval != ZIO_CHECKSUM_INHERIT);
197
198 checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
199
200 os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
201 os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
202 }
203
204 static void
205 primary_cache_changed_cb(void *arg, uint64_t newval)
206 {
207 objset_t *os = arg;
208
209 /*
210 * Inheritance and range checking should have been done by now.
211 */
212 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
213 newval == ZFS_CACHE_METADATA);
214
215 os->os_primary_cache = newval;
216 }
217
218 static void
219 secondary_cache_changed_cb(void *arg, uint64_t newval)
220 {
221 objset_t *os = arg;
222
223 /*
224 * Inheritance and range checking should have been done by now.
225 */
226 ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
227 newval == ZFS_CACHE_METADATA);
228
229 os->os_secondary_cache = newval;
230 }
231
232 static void
233 sync_changed_cb(void *arg, uint64_t newval)
234 {
235 objset_t *os = arg;
236
237 /*
238 * Inheritance and range checking should have been done by now.
239 */
240 ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
241 newval == ZFS_SYNC_DISABLED);
242
243 os->os_sync = newval;
244 if (os->os_zil)
245 zil_set_sync(os->os_zil, newval);
246 }
247
248 static void
249 redundant_metadata_changed_cb(void *arg, uint64_t newval)
250 {
251 objset_t *os = arg;
252
253 /*
254 * Inheritance and range checking should have been done by now.
255 */
256 ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
257 newval == ZFS_REDUNDANT_METADATA_MOST);
258
259 os->os_redundant_metadata = newval;
260 }
261
262 static void
263 logbias_changed_cb(void *arg, uint64_t newval)
264 {
265 objset_t *os = arg;
266
267 ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
268 newval == ZFS_LOGBIAS_THROUGHPUT);
269 os->os_logbias = newval;
270 if (os->os_zil)
271 zil_set_logbias(os->os_zil, newval);
272 }
273
274 static void
275 recordsize_changed_cb(void *arg, uint64_t newval)
276 {
277 objset_t *os = arg;
278
279 os->os_recordsize = newval;
280 }
281
282 void
283 dmu_objset_byteswap(void *buf, size_t size)
284 {
285 objset_phys_t *osp = buf;
286
287 ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
288 dnode_byteswap(&osp->os_meta_dnode);
289 byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
290 osp->os_type = BSWAP_64(osp->os_type);
291 osp->os_flags = BSWAP_64(osp->os_flags);
292 if (size == sizeof (objset_phys_t)) {
293 dnode_byteswap(&osp->os_userused_dnode);
294 dnode_byteswap(&osp->os_groupused_dnode);
295 }
296 }
297
298 int
299 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
300 objset_t **osp)
301 {
302 objset_t *os;
303 int i, err;
304
305 ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
306
307 os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
308 os->os_dsl_dataset = ds;
309 os->os_spa = spa;
310 os->os_rootbp = bp;
311 if (!BP_IS_HOLE(os->os_rootbp)) {
312 arc_flags_t aflags = ARC_FLAG_WAIT;
313 zbookmark_phys_t zb;
314 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
315 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
316
317 if (DMU_OS_IS_L2CACHEABLE(os))
318 aflags |= ARC_FLAG_L2CACHE;
319 if (DMU_OS_IS_L2COMPRESSIBLE(os))
320 aflags |= ARC_FLAG_L2COMPRESS;
321
322 dprintf_bp(os->os_rootbp, "reading %s", "");
323 err = arc_read(NULL, spa, os->os_rootbp,
324 arc_getbuf_func, &os->os_phys_buf,
325 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
326 if (err != 0) {
327 kmem_free(os, sizeof (objset_t));
328 /* convert checksum errors into IO errors */
329 if (err == ECKSUM)
330 err = SET_ERROR(EIO);
331 return (err);
332 }
333
334 /* Increase the blocksize if we are permitted. */
335 if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
336 arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
337 arc_buf_t *buf = arc_buf_alloc(spa,
338 sizeof (objset_phys_t), &os->os_phys_buf,
339 ARC_BUFC_METADATA);
340 bzero(buf->b_data, sizeof (objset_phys_t));
341 bcopy(os->os_phys_buf->b_data, buf->b_data,
342 arc_buf_size(os->os_phys_buf));
343 (void) arc_buf_remove_ref(os->os_phys_buf,
344 &os->os_phys_buf);
345 os->os_phys_buf = buf;
346 }
347
348 os->os_phys = os->os_phys_buf->b_data;
349 os->os_flags = os->os_phys->os_flags;
350 } else {
351 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
352 sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
353 os->os_phys_buf = arc_buf_alloc(spa, size,
354 &os->os_phys_buf, ARC_BUFC_METADATA);
355 os->os_phys = os->os_phys_buf->b_data;
356 bzero(os->os_phys, size);
357 }
358
359 /*
360 * Note: the changed_cb will be called once before the register
361 * func returns, thus changing the checksum/compression from the
362 * default (fletcher2/off). Snapshots don't need to know about
363 * checksum/compression/copies.
364 */
365 if (ds != NULL) {
366 err = dsl_prop_register(ds,
367 zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
368 primary_cache_changed_cb, os);
369 if (err == 0) {
370 err = dsl_prop_register(ds,
371 zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
372 secondary_cache_changed_cb, os);
373 }
374 if (!ds->ds_is_snapshot) {
375 if (err == 0) {
376 err = dsl_prop_register(ds,
377 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
378 checksum_changed_cb, os);
379 }
380 if (err == 0) {
381 err = dsl_prop_register(ds,
382 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
383 compression_changed_cb, os);
384 }
385 if (err == 0) {
386 err = dsl_prop_register(ds,
387 zfs_prop_to_name(ZFS_PROP_COPIES),
388 copies_changed_cb, os);
389 }
390 if (err == 0) {
391 err = dsl_prop_register(ds,
392 zfs_prop_to_name(ZFS_PROP_DEDUP),
393 dedup_changed_cb, os);
394 }
395 if (err == 0) {
396 err = dsl_prop_register(ds,
397 zfs_prop_to_name(ZFS_PROP_LOGBIAS),
398 logbias_changed_cb, os);
399 }
400 if (err == 0) {
401 err = dsl_prop_register(ds,
402 zfs_prop_to_name(ZFS_PROP_SYNC),
403 sync_changed_cb, os);
404 }
405 if (err == 0) {
406 err = dsl_prop_register(ds,
407 zfs_prop_to_name(
408 ZFS_PROP_REDUNDANT_METADATA),
409 redundant_metadata_changed_cb, os);
410 }
411 if (err == 0) {
412 err = dsl_prop_register(ds,
413 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
414 recordsize_changed_cb, os);
415 }
416 }
417 if (err != 0) {
418 VERIFY(arc_buf_remove_ref(os->os_phys_buf,
419 &os->os_phys_buf));
420 kmem_free(os, sizeof (objset_t));
421 return (err);
422 }
423 } else {
424 /* It's the meta-objset. */
425 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
426 os->os_compress = ZIO_COMPRESS_ON;
427 os->os_copies = spa_max_replication(spa);
428 os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
429 os->os_dedup_verify = B_FALSE;
430 os->os_logbias = ZFS_LOGBIAS_LATENCY;
431 os->os_sync = ZFS_SYNC_STANDARD;
432 os->os_primary_cache = ZFS_CACHE_ALL;
433 os->os_secondary_cache = ZFS_CACHE_ALL;
434 }
435
436 if (ds == NULL || !ds->ds_is_snapshot)
437 os->os_zil_header = os->os_phys->os_zil_header;
438 os->os_zil = zil_alloc(os, &os->os_zil_header);
439
440 for (i = 0; i < TXG_SIZE; i++) {
441 list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
442 offsetof(dnode_t, dn_dirty_link[i]));
443 list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
444 offsetof(dnode_t, dn_dirty_link[i]));
445 }
446 list_create(&os->os_dnodes, sizeof (dnode_t),
447 offsetof(dnode_t, dn_link));
448 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
449 offsetof(dmu_buf_impl_t, db_link));
450
451 list_link_init(&os->os_evicting_node);
452
453 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
454 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
455 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
456
457 dnode_special_open(os, &os->os_phys->os_meta_dnode,
458 DMU_META_DNODE_OBJECT, &os->os_meta_dnode);
459 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
460 dnode_special_open(os, &os->os_phys->os_userused_dnode,
461 DMU_USERUSED_OBJECT, &os->os_userused_dnode);
462 dnode_special_open(os, &os->os_phys->os_groupused_dnode,
463 DMU_GROUPUSED_OBJECT, &os->os_groupused_dnode);
464 }
465
466 *osp = os;
467 return (0);
468 }
469
470 int
471 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
472 {
473 int err = 0;
474
475 mutex_enter(&ds->ds_opening_lock);
476 if (ds->ds_objset == NULL) {
477 objset_t *os;
478 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
479 ds, dsl_dataset_get_blkptr(ds), &os);
480
481 if (err == 0) {
482 mutex_enter(&ds->ds_lock);
483 ASSERT(ds->ds_objset == NULL);
484 ds->ds_objset = os;
485 mutex_exit(&ds->ds_lock);
486 }
487 }
488 *osp = ds->ds_objset;
489 mutex_exit(&ds->ds_opening_lock);
490 return (err);
491 }
492
493 /*
494 * Holds the pool while the objset is held. Therefore only one objset
495 * can be held at a time.
496 */
497 int
498 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
499 {
500 dsl_pool_t *dp;
501 dsl_dataset_t *ds;
502 int err;
503
504 err = dsl_pool_hold(name, tag, &dp);
505 if (err != 0)
506 return (err);
507 err = dsl_dataset_hold(dp, name, tag, &ds);
508 if (err != 0) {
509 dsl_pool_rele(dp, tag);
510 return (err);
511 }
512
513 err = dmu_objset_from_ds(ds, osp);
514 if (err != 0) {
515 dsl_dataset_rele(ds, tag);
516 dsl_pool_rele(dp, tag);
517 }
518
519 return (err);
520 }
521
522 static int
523 dmu_objset_own_impl(dsl_dataset_t *ds, dmu_objset_type_t type,
524 boolean_t readonly, void *tag, objset_t **osp)
525 {
526 int err;
527
528 err = dmu_objset_from_ds(ds, osp);
529 if (err != 0) {
530 dsl_dataset_disown(ds, tag);
531 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
532 dsl_dataset_disown(ds, tag);
533 return (SET_ERROR(EINVAL));
534 } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
535 dsl_dataset_disown(ds, tag);
536 return (SET_ERROR(EROFS));
537 }
538 return (err);
539 }
540
541 /*
542 * dsl_pool must not be held when this is called.
543 * Upon successful return, there will be a longhold on the dataset,
544 * and the dsl_pool will not be held.
545 */
546 int
547 dmu_objset_own(const char *name, dmu_objset_type_t type,
548 boolean_t readonly, void *tag, objset_t **osp)
549 {
550 dsl_pool_t *dp;
551 dsl_dataset_t *ds;
552 int err;
553
554 err = dsl_pool_hold(name, FTAG, &dp);
555 if (err != 0)
556 return (err);
557 err = dsl_dataset_own(dp, name, tag, &ds);
558 if (err != 0) {
559 dsl_pool_rele(dp, FTAG);
560 return (err);
561 }
562 err = dmu_objset_own_impl(ds, type, readonly, tag, osp);
563 dsl_pool_rele(dp, FTAG);
564
565 return (err);
566 }
567
568 int
569 dmu_objset_own_obj(dsl_pool_t *dp, uint64_t obj, dmu_objset_type_t type,
570 boolean_t readonly, void *tag, objset_t **osp)
571 {
572 dsl_dataset_t *ds;
573 int err;
574
575 err = dsl_dataset_own_obj(dp, obj, tag, &ds);
576 if (err != 0)
577 return (err);
578
579 return (dmu_objset_own_impl(ds, type, readonly, tag, osp));
580 }
581
582 void
583 dmu_objset_rele(objset_t *os, void *tag)
584 {
585 dsl_pool_t *dp = dmu_objset_pool(os);
586 dsl_dataset_rele(os->os_dsl_dataset, tag);
587 dsl_pool_rele(dp, tag);
588 }
589
590 /*
591 * When we are called, os MUST refer to an objset associated with a dataset
592 * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
593 * == tag. We will then release and reacquire ownership of the dataset while
594 * holding the pool config_rwlock to avoid intervening namespace or ownership
595 * changes may occur.
596 *
597 * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
598 * release the hold on its dataset and acquire a new one on the dataset of the
599 * same name so that it can be partially torn down and reconstructed.
600 */
601 void
602 dmu_objset_refresh_ownership(objset_t *os, void *tag)
603 {
604 dsl_pool_t *dp;
605 dsl_dataset_t *ds, *newds;
606 char name[MAXNAMELEN];
607
608 ds = os->os_dsl_dataset;
609 VERIFY3P(ds, !=, NULL);
610 VERIFY3P(ds->ds_owner, ==, tag);
611 VERIFY(dsl_dataset_long_held(ds));
612
613 dsl_dataset_name(ds, name);
614 dp = dmu_objset_pool(os);
615 dsl_pool_config_enter(dp, FTAG);
616 dmu_objset_disown(os, tag);
617 VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
618 VERIFY3P(newds, ==, os->os_dsl_dataset);
619 dsl_pool_config_exit(dp, FTAG);
620 }
621
622 void
623 dmu_objset_disown(objset_t *os, void *tag)
624 {
625 dsl_dataset_disown(os->os_dsl_dataset, tag);
626 }
627
628 void
629 dmu_objset_evict_dbufs(objset_t *os)
630 {
631 dnode_t *dn_marker;
632 dnode_t *dn;
633
634 dn_marker = kmem_alloc(sizeof (dnode_t), KM_SLEEP);
635
636 mutex_enter(&os->os_lock);
637 dn = list_head(&os->os_dnodes);
638 while (dn != NULL) {
639 /*
640 * Skip dnodes without holds. We have to do this dance
641 * because dnode_add_ref() only works if there is already a
642 * hold. If the dnode has no holds, then it has no dbufs.
643 */
644 if (dnode_add_ref(dn, FTAG)) {
645 list_insert_after(&os->os_dnodes, dn, dn_marker);
646 mutex_exit(&os->os_lock);
647
648 dnode_evict_dbufs(dn);
649 dnode_rele(dn, FTAG);
650
651 mutex_enter(&os->os_lock);
652 dn = list_next(&os->os_dnodes, dn_marker);
653 list_remove(&os->os_dnodes, dn_marker);
654 } else {
655 dn = list_next(&os->os_dnodes, dn);
656 }
657 }
658 mutex_exit(&os->os_lock);
659
660 kmem_free(dn_marker, sizeof (dnode_t));
661
662 if (DMU_USERUSED_DNODE(os) != NULL) {
663 dnode_evict_dbufs(DMU_GROUPUSED_DNODE(os));
664 dnode_evict_dbufs(DMU_USERUSED_DNODE(os));
665 }
666 dnode_evict_dbufs(DMU_META_DNODE(os));
667 }
668
669 /*
670 * Objset eviction processing is split into into two pieces.
671 * The first marks the objset as evicting, evicts any dbufs that
672 * have a refcount of zero, and then queues up the objset for the
673 * second phase of eviction. Once os->os_dnodes has been cleared by
674 * dnode_buf_pageout()->dnode_destroy(), the second phase is executed.
675 * The second phase closes the special dnodes, dequeues the objset from
676 * the list of those undergoing eviction, and finally frees the objset.
677 *
678 * NOTE: Due to asynchronous eviction processing (invocation of
679 * dnode_buf_pageout()), it is possible for the meta dnode for the
680 * objset to have no holds even though os->os_dnodes is not empty.
681 */
682 void
683 dmu_objset_evict(objset_t *os)
684 {
685 int t;
686
687 dsl_dataset_t *ds = os->os_dsl_dataset;
688
689 for (t = 0; t < TXG_SIZE; t++)
690 ASSERT(!dmu_objset_is_dirty(os, t));
691
692 if (ds)
693 dsl_prop_unregister_all(ds, os);
694
695 if (os->os_sa)
696 sa_tear_down(os);
697
698 dmu_objset_evict_dbufs(os);
699
700 mutex_enter(&os->os_lock);
701 spa_evicting_os_register(os->os_spa, os);
702 if (list_is_empty(&os->os_dnodes)) {
703 mutex_exit(&os->os_lock);
704 dmu_objset_evict_done(os);
705 } else {
706 mutex_exit(&os->os_lock);
707 }
708 }
709
710 void
711 dmu_objset_evict_done(objset_t *os)
712 {
713 ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
714
715 dnode_special_close(&os->os_meta_dnode);
716 if (DMU_USERUSED_DNODE(os)) {
717 dnode_special_close(&os->os_userused_dnode);
718 dnode_special_close(&os->os_groupused_dnode);
719 }
720 zil_free(os->os_zil);
721
722 VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
723
724 /*
725 * This is a barrier to prevent the objset from going away in
726 * dnode_move() until we can safely ensure that the objset is still in
727 * use. We consider the objset valid before the barrier and invalid
728 * after the barrier.
729 */
730 rw_enter(&os_lock, RW_READER);
731 rw_exit(&os_lock);
732
733 mutex_destroy(&os->os_lock);
734 mutex_destroy(&os->os_obj_lock);
735 mutex_destroy(&os->os_user_ptr_lock);
736 spa_evicting_os_deregister(os->os_spa, os);
737 kmem_free(os, sizeof (objset_t));
738 }
739
740 timestruc_t
741 dmu_objset_snap_cmtime(objset_t *os)
742 {
743 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
744 }
745
746 /* called from dsl for meta-objset */
747 objset_t *
748 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
749 dmu_objset_type_t type, dmu_tx_t *tx)
750 {
751 objset_t *os;
752 dnode_t *mdn;
753
754 ASSERT(dmu_tx_is_syncing(tx));
755
756 if (ds != NULL)
757 VERIFY0(dmu_objset_from_ds(ds, &os));
758 else
759 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
760
761 mdn = DMU_META_DNODE(os);
762
763 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
764 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
765
766 /*
767 * We don't want to have to increase the meta-dnode's nlevels
768 * later, because then we could do it in quescing context while
769 * we are also accessing it in open context.
770 *
771 * This precaution is not necessary for the MOS (ds == NULL),
772 * because the MOS is only updated in syncing context.
773 * This is most fortunate: the MOS is the only objset that
774 * needs to be synced multiple times as spa_sync() iterates
775 * to convergence, so minimizing its dn_nlevels matters.
776 */
777 if (ds != NULL) {
778 int levels = 1;
779
780 /*
781 * Determine the number of levels necessary for the meta-dnode
782 * to contain DN_MAX_OBJECT dnodes.
783 */
784 while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
785 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
786 DN_MAX_OBJECT * sizeof (dnode_phys_t))
787 levels++;
788
789 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
790 mdn->dn_nlevels = levels;
791 }
792
793 ASSERT(type != DMU_OST_NONE);
794 ASSERT(type != DMU_OST_ANY);
795 ASSERT(type < DMU_OST_NUMTYPES);
796 os->os_phys->os_type = type;
797 if (dmu_objset_userused_enabled(os)) {
798 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
799 os->os_flags = os->os_phys->os_flags;
800 }
801
802 dsl_dataset_dirty(ds, tx);
803
804 return (os);
805 }
806
807 typedef struct dmu_objset_create_arg {
808 const char *doca_name;
809 cred_t *doca_cred;
810 void (*doca_userfunc)(objset_t *os, void *arg,
811 cred_t *cr, dmu_tx_t *tx);
812 void *doca_userarg;
813 dmu_objset_type_t doca_type;
814 uint64_t doca_flags;
815 } dmu_objset_create_arg_t;
816
817 /*ARGSUSED*/
818 static int
819 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
820 {
821 dmu_objset_create_arg_t *doca = arg;
822 dsl_pool_t *dp = dmu_tx_pool(tx);
823 dsl_dir_t *pdd;
824 const char *tail;
825 int error;
826
827 if (strchr(doca->doca_name, '@') != NULL)
828 return (SET_ERROR(EINVAL));
829
830 error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
831 if (error != 0)
832 return (error);
833 if (tail == NULL) {
834 dsl_dir_rele(pdd, FTAG);
835 return (SET_ERROR(EEXIST));
836 }
837 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
838 doca->doca_cred);
839 dsl_dir_rele(pdd, FTAG);
840
841 return (error);
842 }
843
844 static void
845 dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
846 {
847 dmu_objset_create_arg_t *doca = arg;
848 dsl_pool_t *dp = dmu_tx_pool(tx);
849 dsl_dir_t *pdd;
850 const char *tail;
851 dsl_dataset_t *ds;
852 uint64_t obj;
853 blkptr_t *bp;
854 objset_t *os;
855
856 VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
857
858 obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
859 doca->doca_cred, tx);
860
861 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
862 bp = dsl_dataset_get_blkptr(ds);
863 os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
864 ds, bp, doca->doca_type, tx);
865
866 if (doca->doca_userfunc != NULL) {
867 doca->doca_userfunc(os, doca->doca_userarg,
868 doca->doca_cred, tx);
869 }
870
871 spa_history_log_internal_ds(ds, "create", tx, "");
872 zvol_create_minors(dp->dp_spa, doca->doca_name, B_TRUE);
873
874 dsl_dataset_rele(ds, FTAG);
875 dsl_dir_rele(pdd, FTAG);
876 }
877
878 int
879 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
880 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
881 {
882 dmu_objset_create_arg_t doca;
883
884 doca.doca_name = name;
885 doca.doca_cred = CRED();
886 doca.doca_flags = flags;
887 doca.doca_userfunc = func;
888 doca.doca_userarg = arg;
889 doca.doca_type = type;
890
891 return (dsl_sync_task(name,
892 dmu_objset_create_check, dmu_objset_create_sync, &doca,
893 5, ZFS_SPACE_CHECK_NORMAL));
894 }
895
896 typedef struct dmu_objset_clone_arg {
897 const char *doca_clone;
898 const char *doca_origin;
899 cred_t *doca_cred;
900 } dmu_objset_clone_arg_t;
901
902 /*ARGSUSED*/
903 static int
904 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
905 {
906 dmu_objset_clone_arg_t *doca = arg;
907 dsl_dir_t *pdd;
908 const char *tail;
909 int error;
910 dsl_dataset_t *origin;
911 dsl_pool_t *dp = dmu_tx_pool(tx);
912
913 if (strchr(doca->doca_clone, '@') != NULL)
914 return (SET_ERROR(EINVAL));
915
916 error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
917 if (error != 0)
918 return (error);
919 if (tail == NULL) {
920 dsl_dir_rele(pdd, FTAG);
921 return (SET_ERROR(EEXIST));
922 }
923
924 error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
925 doca->doca_cred);
926 if (error != 0) {
927 dsl_dir_rele(pdd, FTAG);
928 return (SET_ERROR(EDQUOT));
929 }
930 dsl_dir_rele(pdd, FTAG);
931
932 error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
933 if (error != 0)
934 return (error);
935
936 /* You can only clone snapshots, not the head datasets. */
937 if (!origin->ds_is_snapshot) {
938 dsl_dataset_rele(origin, FTAG);
939 return (SET_ERROR(EINVAL));
940 }
941 dsl_dataset_rele(origin, FTAG);
942
943 return (0);
944 }
945
946 static void
947 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
948 {
949 dmu_objset_clone_arg_t *doca = arg;
950 dsl_pool_t *dp = dmu_tx_pool(tx);
951 dsl_dir_t *pdd;
952 const char *tail;
953 dsl_dataset_t *origin, *ds;
954 uint64_t obj;
955 char namebuf[MAXNAMELEN];
956
957 VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
958 VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
959
960 obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
961 doca->doca_cred, tx);
962
963 VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
964 dsl_dataset_name(origin, namebuf);
965 spa_history_log_internal_ds(ds, "clone", tx,
966 "origin=%s (%llu)", namebuf, origin->ds_object);
967 zvol_create_minors(dp->dp_spa, doca->doca_clone, B_TRUE);
968 dsl_dataset_rele(ds, FTAG);
969 dsl_dataset_rele(origin, FTAG);
970 dsl_dir_rele(pdd, FTAG);
971 }
972
973 int
974 dmu_objset_clone(const char *clone, const char *origin)
975 {
976 dmu_objset_clone_arg_t doca;
977
978 doca.doca_clone = clone;
979 doca.doca_origin = origin;
980 doca.doca_cred = CRED();
981
982 return (dsl_sync_task(clone,
983 dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
984 5, ZFS_SPACE_CHECK_NORMAL));
985 }
986
987 int
988 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
989 {
990 int err;
991 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
992 nvlist_t *snaps = fnvlist_alloc();
993
994 fnvlist_add_boolean(snaps, longsnap);
995 strfree(longsnap);
996 err = dsl_dataset_snapshot(snaps, NULL, NULL);
997 fnvlist_free(snaps);
998 return (err);
999 }
1000
1001 static void
1002 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
1003 {
1004 dnode_t *dn;
1005
1006 while ((dn = list_head(list))) {
1007 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1008 ASSERT(dn->dn_dbuf->db_data_pending);
1009 /*
1010 * Initialize dn_zio outside dnode_sync() because the
1011 * meta-dnode needs to set it ouside dnode_sync().
1012 */
1013 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1014 ASSERT(dn->dn_zio);
1015
1016 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1017 list_remove(list, dn);
1018
1019 if (newlist) {
1020 (void) dnode_add_ref(dn, newlist);
1021 list_insert_tail(newlist, dn);
1022 }
1023
1024 dnode_sync(dn, tx);
1025 }
1026 }
1027
1028 /* ARGSUSED */
1029 static void
1030 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1031 {
1032 int i;
1033
1034 blkptr_t *bp = zio->io_bp;
1035 objset_t *os = arg;
1036 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1037
1038 ASSERT(!BP_IS_EMBEDDED(bp));
1039 ASSERT3P(bp, ==, os->os_rootbp);
1040 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
1041 ASSERT0(BP_GET_LEVEL(bp));
1042
1043 /*
1044 * Update rootbp fill count: it should be the number of objects
1045 * allocated in the object set (not counting the "special"
1046 * objects that are stored in the objset_phys_t -- the meta
1047 * dnode and user/group accounting objects).
1048 */
1049 bp->blk_fill = 0;
1050 for (i = 0; i < dnp->dn_nblkptr; i++)
1051 bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1052 }
1053
1054 /* ARGSUSED */
1055 static void
1056 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1057 {
1058 blkptr_t *bp = zio->io_bp;
1059 blkptr_t *bp_orig = &zio->io_bp_orig;
1060 objset_t *os = arg;
1061
1062 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1063 ASSERT(BP_EQUAL(bp, bp_orig));
1064 } else {
1065 dsl_dataset_t *ds = os->os_dsl_dataset;
1066 dmu_tx_t *tx = os->os_synctx;
1067
1068 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1069 dsl_dataset_block_born(ds, bp, tx);
1070 }
1071 }
1072
1073 /* called from dsl */
1074 void
1075 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1076 {
1077 int txgoff;
1078 zbookmark_phys_t zb;
1079 zio_prop_t zp;
1080 zio_t *zio;
1081 list_t *list;
1082 list_t *newlist = NULL;
1083 dbuf_dirty_record_t *dr;
1084
1085 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1086
1087 ASSERT(dmu_tx_is_syncing(tx));
1088 /* XXX the write_done callback should really give us the tx... */
1089 os->os_synctx = tx;
1090
1091 if (os->os_dsl_dataset == NULL) {
1092 /*
1093 * This is the MOS. If we have upgraded,
1094 * spa_max_replication() could change, so reset
1095 * os_copies here.
1096 */
1097 os->os_copies = spa_max_replication(os->os_spa);
1098 }
1099
1100 /*
1101 * Create the root block IO
1102 */
1103 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1104 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1105 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1106 arc_release(os->os_phys_buf, &os->os_phys_buf);
1107
1108 dmu_write_policy(os, NULL, 0, 0, &zp);
1109
1110 zio = arc_write(pio, os->os_spa, tx->tx_txg,
1111 os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1112 DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
1113 NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1114 ZIO_FLAG_MUSTSUCCEED, &zb);
1115
1116 /*
1117 * Sync special dnodes - the parent IO for the sync is the root block
1118 */
1119 DMU_META_DNODE(os)->dn_zio = zio;
1120 dnode_sync(DMU_META_DNODE(os), tx);
1121
1122 os->os_phys->os_flags = os->os_flags;
1123
1124 if (DMU_USERUSED_DNODE(os) &&
1125 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1126 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1127 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1128 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1129 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1130 }
1131
1132 txgoff = tx->tx_txg & TXG_MASK;
1133
1134 if (dmu_objset_userused_enabled(os)) {
1135 newlist = &os->os_synced_dnodes;
1136 /*
1137 * We must create the list here because it uses the
1138 * dn_dirty_link[] of this txg.
1139 */
1140 list_create(newlist, sizeof (dnode_t),
1141 offsetof(dnode_t, dn_dirty_link[txgoff]));
1142 }
1143
1144 dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1145 dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1146
1147 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1148 while ((dr = list_head(list))) {
1149 ASSERT0(dr->dr_dbuf->db_level);
1150 list_remove(list, dr);
1151 if (dr->dr_zio)
1152 zio_nowait(dr->dr_zio);
1153 }
1154 /*
1155 * Free intent log blocks up to this tx.
1156 */
1157 zil_sync(os->os_zil, tx);
1158 os->os_phys->os_zil_header = os->os_zil_header;
1159 zio_nowait(zio);
1160 }
1161
1162 boolean_t
1163 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1164 {
1165 return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1166 !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1167 }
1168
1169 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1170
1171 void
1172 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1173 {
1174 used_cbs[ost] = cb;
1175 }
1176
1177 boolean_t
1178 dmu_objset_userused_enabled(objset_t *os)
1179 {
1180 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1181 used_cbs[os->os_phys->os_type] != NULL &&
1182 DMU_USERUSED_DNODE(os) != NULL);
1183 }
1184
1185 static void
1186 do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1187 uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
1188 {
1189 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1190 int64_t delta = DNODE_SIZE + used;
1191 if (subtract)
1192 delta = -delta;
1193 VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
1194 user, delta, tx));
1195 VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1196 group, delta, tx));
1197 }
1198 }
1199
1200 void
1201 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1202 {
1203 dnode_t *dn;
1204 list_t *list = &os->os_synced_dnodes;
1205
1206 ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1207
1208 while ((dn = list_head(list))) {
1209 int flags;
1210 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1211 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1212 dn->dn_phys->dn_flags &
1213 DNODE_FLAG_USERUSED_ACCOUNTED);
1214
1215 /* Allocate the user/groupused objects if necessary. */
1216 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1217 VERIFY(0 == zap_create_claim(os,
1218 DMU_USERUSED_OBJECT,
1219 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1220 VERIFY(0 == zap_create_claim(os,
1221 DMU_GROUPUSED_OBJECT,
1222 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1223 }
1224
1225 /*
1226 * We intentionally modify the zap object even if the
1227 * net delta is zero. Otherwise
1228 * the block of the zap obj could be shared between
1229 * datasets but need to be different between them after
1230 * a bprewrite.
1231 */
1232
1233 flags = dn->dn_id_flags;
1234 ASSERT(flags);
1235 if (flags & DN_ID_OLD_EXIST) {
1236 do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1237 dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1238 }
1239 if (flags & DN_ID_NEW_EXIST) {
1240 do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1241 dn->dn_phys->dn_flags, dn->dn_newuid,
1242 dn->dn_newgid, B_FALSE, tx);
1243 }
1244
1245 mutex_enter(&dn->dn_mtx);
1246 dn->dn_oldused = 0;
1247 dn->dn_oldflags = 0;
1248 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1249 dn->dn_olduid = dn->dn_newuid;
1250 dn->dn_oldgid = dn->dn_newgid;
1251 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1252 if (dn->dn_bonuslen == 0)
1253 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1254 else
1255 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1256 }
1257 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1258 mutex_exit(&dn->dn_mtx);
1259
1260 list_remove(list, dn);
1261 dnode_rele(dn, list);
1262 }
1263 }
1264
1265 /*
1266 * Returns a pointer to data to find uid/gid from
1267 *
1268 * If a dirty record for transaction group that is syncing can't
1269 * be found then NULL is returned. In the NULL case it is assumed
1270 * the uid/gid aren't changing.
1271 */
1272 static void *
1273 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1274 {
1275 dbuf_dirty_record_t *dr, **drp;
1276 void *data;
1277
1278 if (db->db_dirtycnt == 0)
1279 return (db->db.db_data); /* Nothing is changing */
1280
1281 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1282 if (dr->dr_txg == tx->tx_txg)
1283 break;
1284
1285 if (dr == NULL) {
1286 data = NULL;
1287 } else {
1288 dnode_t *dn;
1289
1290 DB_DNODE_ENTER(dr->dr_dbuf);
1291 dn = DB_DNODE(dr->dr_dbuf);
1292
1293 if (dn->dn_bonuslen == 0 &&
1294 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1295 data = dr->dt.dl.dr_data->b_data;
1296 else
1297 data = dr->dt.dl.dr_data;
1298
1299 DB_DNODE_EXIT(dr->dr_dbuf);
1300 }
1301
1302 return (data);
1303 }
1304
1305 void
1306 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1307 {
1308 objset_t *os = dn->dn_objset;
1309 void *data = NULL;
1310 dmu_buf_impl_t *db = NULL;
1311 uint64_t *user = NULL;
1312 uint64_t *group = NULL;
1313 int flags = dn->dn_id_flags;
1314 int error;
1315 boolean_t have_spill = B_FALSE;
1316
1317 if (!dmu_objset_userused_enabled(dn->dn_objset))
1318 return;
1319
1320 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1321 DN_ID_CHKED_SPILL)))
1322 return;
1323
1324 if (before && dn->dn_bonuslen != 0)
1325 data = DN_BONUS(dn->dn_phys);
1326 else if (!before && dn->dn_bonuslen != 0) {
1327 if (dn->dn_bonus) {
1328 db = dn->dn_bonus;
1329 mutex_enter(&db->db_mtx);
1330 data = dmu_objset_userquota_find_data(db, tx);
1331 } else {
1332 data = DN_BONUS(dn->dn_phys);
1333 }
1334 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1335 int rf = 0;
1336
1337 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1338 rf |= DB_RF_HAVESTRUCT;
1339 error = dmu_spill_hold_by_dnode(dn,
1340 rf | DB_RF_MUST_SUCCEED,
1341 FTAG, (dmu_buf_t **)&db);
1342 ASSERT(error == 0);
1343 mutex_enter(&db->db_mtx);
1344 data = (before) ? db->db.db_data :
1345 dmu_objset_userquota_find_data(db, tx);
1346 have_spill = B_TRUE;
1347 } else {
1348 mutex_enter(&dn->dn_mtx);
1349 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1350 mutex_exit(&dn->dn_mtx);
1351 return;
1352 }
1353
1354 if (before) {
1355 ASSERT(data);
1356 user = &dn->dn_olduid;
1357 group = &dn->dn_oldgid;
1358 } else if (data) {
1359 user = &dn->dn_newuid;
1360 group = &dn->dn_newgid;
1361 }
1362
1363 /*
1364 * Must always call the callback in case the object
1365 * type has changed and that type isn't an object type to track
1366 */
1367 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1368 user, group);
1369
1370 /*
1371 * Preserve existing uid/gid when the callback can't determine
1372 * what the new uid/gid are and the callback returned EEXIST.
1373 * The EEXIST error tells us to just use the existing uid/gid.
1374 * If we don't know what the old values are then just assign
1375 * them to 0, since that is a new file being created.
1376 */
1377 if (!before && data == NULL && error == EEXIST) {
1378 if (flags & DN_ID_OLD_EXIST) {
1379 dn->dn_newuid = dn->dn_olduid;
1380 dn->dn_newgid = dn->dn_oldgid;
1381 } else {
1382 dn->dn_newuid = 0;
1383 dn->dn_newgid = 0;
1384 }
1385 error = 0;
1386 }
1387
1388 if (db)
1389 mutex_exit(&db->db_mtx);
1390
1391 mutex_enter(&dn->dn_mtx);
1392 if (error == 0 && before)
1393 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1394 if (error == 0 && !before)
1395 dn->dn_id_flags |= DN_ID_NEW_EXIST;
1396
1397 if (have_spill) {
1398 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1399 } else {
1400 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1401 }
1402 mutex_exit(&dn->dn_mtx);
1403 if (have_spill)
1404 dmu_buf_rele((dmu_buf_t *)db, FTAG);
1405 }
1406
1407 boolean_t
1408 dmu_objset_userspace_present(objset_t *os)
1409 {
1410 return (os->os_phys->os_flags &
1411 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1412 }
1413
1414 int
1415 dmu_objset_userspace_upgrade(objset_t *os)
1416 {
1417 uint64_t obj;
1418 int err = 0;
1419
1420 if (dmu_objset_userspace_present(os))
1421 return (0);
1422 if (!dmu_objset_userused_enabled(os))
1423 return (SET_ERROR(ENOTSUP));
1424 if (dmu_objset_is_snapshot(os))
1425 return (SET_ERROR(EINVAL));
1426
1427 /*
1428 * We simply need to mark every object dirty, so that it will be
1429 * synced out and now accounted. If this is called
1430 * concurrently, or if we already did some work before crashing,
1431 * that's fine, since we track each object's accounted state
1432 * independently.
1433 */
1434
1435 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1436 dmu_tx_t *tx;
1437 dmu_buf_t *db;
1438 int objerr;
1439
1440 if (issig(JUSTLOOKING) && issig(FORREAL))
1441 return (SET_ERROR(EINTR));
1442
1443 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1444 if (objerr != 0)
1445 continue;
1446 tx = dmu_tx_create(os);
1447 dmu_tx_hold_bonus(tx, obj);
1448 objerr = dmu_tx_assign(tx, TXG_WAIT);
1449 if (objerr != 0) {
1450 dmu_tx_abort(tx);
1451 continue;
1452 }
1453 dmu_buf_will_dirty(db, tx);
1454 dmu_buf_rele(db, FTAG);
1455 dmu_tx_commit(tx);
1456 }
1457
1458 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1459 txg_wait_synced(dmu_objset_pool(os), 0);
1460 return (0);
1461 }
1462
1463 void
1464 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1465 uint64_t *usedobjsp, uint64_t *availobjsp)
1466 {
1467 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1468 usedobjsp, availobjsp);
1469 }
1470
1471 uint64_t
1472 dmu_objset_fsid_guid(objset_t *os)
1473 {
1474 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1475 }
1476
1477 void
1478 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1479 {
1480 stat->dds_type = os->os_phys->os_type;
1481 if (os->os_dsl_dataset)
1482 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1483 }
1484
1485 void
1486 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1487 {
1488 ASSERT(os->os_dsl_dataset ||
1489 os->os_phys->os_type == DMU_OST_META);
1490
1491 if (os->os_dsl_dataset != NULL)
1492 dsl_dataset_stats(os->os_dsl_dataset, nv);
1493
1494 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1495 os->os_phys->os_type);
1496 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1497 dmu_objset_userspace_present(os));
1498 }
1499
1500 int
1501 dmu_objset_is_snapshot(objset_t *os)
1502 {
1503 if (os->os_dsl_dataset != NULL)
1504 return (os->os_dsl_dataset->ds_is_snapshot);
1505 else
1506 return (B_FALSE);
1507 }
1508
1509 int
1510 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1511 boolean_t *conflict)
1512 {
1513 dsl_dataset_t *ds = os->os_dsl_dataset;
1514 uint64_t ignored;
1515
1516 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1517 return (SET_ERROR(ENOENT));
1518
1519 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1520 dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1521 MT_FIRST, real, maxlen, conflict));
1522 }
1523
1524 int
1525 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1526 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1527 {
1528 dsl_dataset_t *ds = os->os_dsl_dataset;
1529 zap_cursor_t cursor;
1530 zap_attribute_t attr;
1531
1532 ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1533
1534 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1535 return (SET_ERROR(ENOENT));
1536
1537 zap_cursor_init_serialized(&cursor,
1538 ds->ds_dir->dd_pool->dp_meta_objset,
1539 dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1540
1541 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1542 zap_cursor_fini(&cursor);
1543 return (SET_ERROR(ENOENT));
1544 }
1545
1546 if (strlen(attr.za_name) + 1 > namelen) {
1547 zap_cursor_fini(&cursor);
1548 return (SET_ERROR(ENAMETOOLONG));
1549 }
1550
1551 (void) strcpy(name, attr.za_name);
1552 if (idp)
1553 *idp = attr.za_first_integer;
1554 if (case_conflict)
1555 *case_conflict = attr.za_normalization_conflict;
1556 zap_cursor_advance(&cursor);
1557 *offp = zap_cursor_serialize(&cursor);
1558 zap_cursor_fini(&cursor);
1559
1560 return (0);
1561 }
1562
1563 int
1564 dmu_snapshot_lookup(objset_t *os, const char *name, uint64_t *value)
1565 {
1566 return (dsl_dataset_snap_lookup(os->os_dsl_dataset, name, value));
1567 }
1568
1569 int
1570 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1571 uint64_t *idp, uint64_t *offp)
1572 {
1573 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1574 zap_cursor_t cursor;
1575 zap_attribute_t attr;
1576
1577 /* there is no next dir on a snapshot! */
1578 if (os->os_dsl_dataset->ds_object !=
1579 dsl_dir_phys(dd)->dd_head_dataset_obj)
1580 return (SET_ERROR(ENOENT));
1581
1582 zap_cursor_init_serialized(&cursor,
1583 dd->dd_pool->dp_meta_objset,
1584 dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
1585
1586 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1587 zap_cursor_fini(&cursor);
1588 return (SET_ERROR(ENOENT));
1589 }
1590
1591 if (strlen(attr.za_name) + 1 > namelen) {
1592 zap_cursor_fini(&cursor);
1593 return (SET_ERROR(ENAMETOOLONG));
1594 }
1595
1596 (void) strcpy(name, attr.za_name);
1597 if (idp)
1598 *idp = attr.za_first_integer;
1599 zap_cursor_advance(&cursor);
1600 *offp = zap_cursor_serialize(&cursor);
1601 zap_cursor_fini(&cursor);
1602
1603 return (0);
1604 }
1605
1606 typedef struct dmu_objset_find_ctx {
1607 taskq_t *dc_tq;
1608 dsl_pool_t *dc_dp;
1609 uint64_t dc_ddobj;
1610 int (*dc_func)(dsl_pool_t *, dsl_dataset_t *, void *);
1611 void *dc_arg;
1612 int dc_flags;
1613 kmutex_t *dc_error_lock;
1614 int *dc_error;
1615 } dmu_objset_find_ctx_t;
1616
1617 static void
1618 dmu_objset_find_dp_impl(dmu_objset_find_ctx_t *dcp)
1619 {
1620 dsl_pool_t *dp = dcp->dc_dp;
1621 dmu_objset_find_ctx_t *child_dcp;
1622 dsl_dir_t *dd;
1623 dsl_dataset_t *ds;
1624 zap_cursor_t zc;
1625 zap_attribute_t *attr;
1626 uint64_t thisobj;
1627 int err = 0;
1628
1629 /* don't process if there already was an error */
1630 if (*dcp->dc_error != 0)
1631 goto out;
1632
1633 err = dsl_dir_hold_obj(dp, dcp->dc_ddobj, NULL, FTAG, &dd);
1634 if (err != 0)
1635 goto out;
1636
1637 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1638 if (dd->dd_myname[0] == '$') {
1639 dsl_dir_rele(dd, FTAG);
1640 goto out;
1641 }
1642
1643 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1644 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1645
1646 /*
1647 * Iterate over all children.
1648 */
1649 if (dcp->dc_flags & DS_FIND_CHILDREN) {
1650 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1651 dsl_dir_phys(dd)->dd_child_dir_zapobj);
1652 zap_cursor_retrieve(&zc, attr) == 0;
1653 (void) zap_cursor_advance(&zc)) {
1654 ASSERT3U(attr->za_integer_length, ==,
1655 sizeof (uint64_t));
1656 ASSERT3U(attr->za_num_integers, ==, 1);
1657
1658 child_dcp = kmem_alloc(sizeof (*child_dcp), KM_SLEEP);
1659 *child_dcp = *dcp;
1660 child_dcp->dc_ddobj = attr->za_first_integer;
1661 if (dcp->dc_tq != NULL)
1662 (void) taskq_dispatch(dcp->dc_tq,
1663 dmu_objset_find_dp_cb, child_dcp, TQ_SLEEP);
1664 else
1665 dmu_objset_find_dp_impl(child_dcp);
1666 }
1667 zap_cursor_fini(&zc);
1668 }
1669
1670 /*
1671 * Iterate over all snapshots.
1672 */
1673 if (dcp->dc_flags & DS_FIND_SNAPSHOTS) {
1674 dsl_dataset_t *ds;
1675 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1676
1677 if (err == 0) {
1678 uint64_t snapobj;
1679
1680 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1681 dsl_dataset_rele(ds, FTAG);
1682
1683 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1684 zap_cursor_retrieve(&zc, attr) == 0;
1685 (void) zap_cursor_advance(&zc)) {
1686 ASSERT3U(attr->za_integer_length, ==,
1687 sizeof (uint64_t));
1688 ASSERT3U(attr->za_num_integers, ==, 1);
1689
1690 err = dsl_dataset_hold_obj(dp,
1691 attr->za_first_integer, FTAG, &ds);
1692 if (err != 0)
1693 break;
1694 err = dcp->dc_func(dp, ds, dcp->dc_arg);
1695 dsl_dataset_rele(ds, FTAG);
1696 if (err != 0)
1697 break;
1698 }
1699 zap_cursor_fini(&zc);
1700 }
1701 }
1702
1703 dsl_dir_rele(dd, FTAG);
1704 kmem_free(attr, sizeof (zap_attribute_t));
1705
1706 if (err != 0)
1707 goto out;
1708
1709 /*
1710 * Apply to self.
1711 */
1712 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1713 if (err != 0)
1714 goto out;
1715 err = dcp->dc_func(dp, ds, dcp->dc_arg);
1716 dsl_dataset_rele(ds, FTAG);
1717
1718 out:
1719 if (err != 0) {
1720 mutex_enter(dcp->dc_error_lock);
1721 /* only keep first error */
1722 if (*dcp->dc_error == 0)
1723 *dcp->dc_error = err;
1724 mutex_exit(dcp->dc_error_lock);
1725 }
1726
1727 kmem_free(dcp, sizeof (*dcp));
1728 }
1729
1730 static void
1731 dmu_objset_find_dp_cb(void *arg)
1732 {
1733 dmu_objset_find_ctx_t *dcp = arg;
1734 dsl_pool_t *dp = dcp->dc_dp;
1735
1736 /*
1737 * We need to get a pool_config_lock here, as there are several
1738 * asssert(pool_config_held) down the stack. Getting a lock via
1739 * dsl_pool_config_enter is risky, as it might be stalled by a
1740 * pending writer. This would deadlock, as the write lock can
1741 * only be granted when our parent thread gives up the lock.
1742 * The _prio interface gives us priority over a pending writer.
1743 */
1744 dsl_pool_config_enter_prio(dp, FTAG);
1745
1746 dmu_objset_find_dp_impl(dcp);
1747
1748 dsl_pool_config_exit(dp, FTAG);
1749 }
1750
1751 /*
1752 * Find objsets under and including ddobj, call func(ds) on each.
1753 * The order for the enumeration is completely undefined.
1754 * func is called with dsl_pool_config held.
1755 */
1756 int
1757 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
1758 int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1759 {
1760 int error = 0;
1761 taskq_t *tq = NULL;
1762 int ntasks;
1763 dmu_objset_find_ctx_t *dcp;
1764 kmutex_t err_lock;
1765
1766 mutex_init(&err_lock, NULL, MUTEX_DEFAULT, NULL);
1767 dcp = kmem_alloc(sizeof (*dcp), KM_SLEEP);
1768 dcp->dc_tq = NULL;
1769 dcp->dc_dp = dp;
1770 dcp->dc_ddobj = ddobj;
1771 dcp->dc_func = func;
1772 dcp->dc_arg = arg;
1773 dcp->dc_flags = flags;
1774 dcp->dc_error_lock = &err_lock;
1775 dcp->dc_error = &error;
1776
1777 if ((flags & DS_FIND_SERIALIZE) || dsl_pool_config_held_writer(dp)) {
1778 /*
1779 * In case a write lock is held we can't make use of
1780 * parallelism, as down the stack of the worker threads
1781 * the lock is asserted via dsl_pool_config_held.
1782 * In case of a read lock this is solved by getting a read
1783 * lock in each worker thread, which isn't possible in case
1784 * of a writer lock. So we fall back to the synchronous path
1785 * here.
1786 * In the future it might be possible to get some magic into
1787 * dsl_pool_config_held in a way that it returns true for
1788 * the worker threads so that a single lock held from this
1789 * thread suffices. For now, stay single threaded.
1790 */
1791 dmu_objset_find_dp_impl(dcp);
1792 mutex_destroy(&err_lock);
1793
1794 return (error);
1795 }
1796
1797 ntasks = dmu_find_threads;
1798 if (ntasks == 0)
1799 ntasks = vdev_count_leaves(dp->dp_spa) * 4;
1800 tq = taskq_create("dmu_objset_find", ntasks, maxclsyspri, ntasks,
1801 INT_MAX, 0);
1802 if (tq == NULL) {
1803 kmem_free(dcp, sizeof (*dcp));
1804 mutex_destroy(&err_lock);
1805
1806 return (SET_ERROR(ENOMEM));
1807 }
1808 dcp->dc_tq = tq;
1809
1810 /* dcp will be freed by task */
1811 (void) taskq_dispatch(tq, dmu_objset_find_dp_cb, dcp, TQ_SLEEP);
1812
1813 /*
1814 * PORTING: this code relies on the property of taskq_wait to wait
1815 * until no more tasks are queued and no more tasks are active. As
1816 * we always queue new tasks from within other tasks, task_wait
1817 * reliably waits for the full recursion to finish, even though we
1818 * enqueue new tasks after taskq_wait has been called.
1819 * On platforms other than illumos, taskq_wait may not have this
1820 * property.
1821 */
1822 taskq_wait(tq);
1823 taskq_destroy(tq);
1824 mutex_destroy(&err_lock);
1825
1826 return (error);
1827 }
1828
1829 /*
1830 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1831 * The dp_config_rwlock must not be held when this is called, and it
1832 * will not be held when the callback is called.
1833 * Therefore this function should only be used when the pool is not changing
1834 * (e.g. in syncing context), or the callback can deal with the possible races.
1835 */
1836 static int
1837 dmu_objset_find_impl(spa_t *spa, const char *name,
1838 int func(const char *, void *), void *arg, int flags)
1839 {
1840 dsl_dir_t *dd;
1841 dsl_pool_t *dp = spa_get_dsl(spa);
1842 dsl_dataset_t *ds;
1843 zap_cursor_t zc;
1844 zap_attribute_t *attr;
1845 char *child;
1846 uint64_t thisobj;
1847 int err;
1848
1849 dsl_pool_config_enter(dp, FTAG);
1850
1851 err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
1852 if (err != 0) {
1853 dsl_pool_config_exit(dp, FTAG);
1854 return (err);
1855 }
1856
1857 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1858 if (dd->dd_myname[0] == '$') {
1859 dsl_dir_rele(dd, FTAG);
1860 dsl_pool_config_exit(dp, FTAG);
1861 return (0);
1862 }
1863
1864 thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1865 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1866
1867 /*
1868 * Iterate over all children.
1869 */
1870 if (flags & DS_FIND_CHILDREN) {
1871 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1872 dsl_dir_phys(dd)->dd_child_dir_zapobj);
1873 zap_cursor_retrieve(&zc, attr) == 0;
1874 (void) zap_cursor_advance(&zc)) {
1875 ASSERT3U(attr->za_integer_length, ==,
1876 sizeof (uint64_t));
1877 ASSERT3U(attr->za_num_integers, ==, 1);
1878
1879 child = kmem_asprintf("%s/%s", name, attr->za_name);
1880 dsl_pool_config_exit(dp, FTAG);
1881 err = dmu_objset_find_impl(spa, child,
1882 func, arg, flags);
1883 dsl_pool_config_enter(dp, FTAG);
1884 strfree(child);
1885 if (err != 0)
1886 break;
1887 }
1888 zap_cursor_fini(&zc);
1889
1890 if (err != 0) {
1891 dsl_dir_rele(dd, FTAG);
1892 dsl_pool_config_exit(dp, FTAG);
1893 kmem_free(attr, sizeof (zap_attribute_t));
1894 return (err);
1895 }
1896 }
1897
1898 /*
1899 * Iterate over all snapshots.
1900 */
1901 if (flags & DS_FIND_SNAPSHOTS) {
1902 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1903
1904 if (err == 0) {
1905 uint64_t snapobj;
1906
1907 snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1908 dsl_dataset_rele(ds, FTAG);
1909
1910 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1911 zap_cursor_retrieve(&zc, attr) == 0;
1912 (void) zap_cursor_advance(&zc)) {
1913 ASSERT3U(attr->za_integer_length, ==,
1914 sizeof (uint64_t));
1915 ASSERT3U(attr->za_num_integers, ==, 1);
1916
1917 child = kmem_asprintf("%s@%s",
1918 name, attr->za_name);
1919 dsl_pool_config_exit(dp, FTAG);
1920 err = func(child, arg);
1921 dsl_pool_config_enter(dp, FTAG);
1922 strfree(child);
1923 if (err != 0)
1924 break;
1925 }
1926 zap_cursor_fini(&zc);
1927 }
1928 }
1929
1930 dsl_dir_rele(dd, FTAG);
1931 kmem_free(attr, sizeof (zap_attribute_t));
1932 dsl_pool_config_exit(dp, FTAG);
1933
1934 if (err != 0)
1935 return (err);
1936
1937 /* Apply to self. */
1938 return (func(name, arg));
1939 }
1940
1941 /*
1942 * See comment above dmu_objset_find_impl().
1943 */
1944 int
1945 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
1946 int flags)
1947 {
1948 spa_t *spa;
1949 int error;
1950
1951 error = spa_open(name, &spa, FTAG);
1952 if (error != 0)
1953 return (error);
1954 error = dmu_objset_find_impl(spa, name, func, arg, flags);
1955 spa_close(spa, FTAG);
1956 return (error);
1957 }
1958
1959 void
1960 dmu_objset_set_user(objset_t *os, void *user_ptr)
1961 {
1962 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1963 os->os_user_ptr = user_ptr;
1964 }
1965
1966 void *
1967 dmu_objset_get_user(objset_t *os)
1968 {
1969 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1970 return (os->os_user_ptr);
1971 }
1972
1973 /*
1974 * Determine name of filesystem, given name of snapshot.
1975 * buf must be at least MAXNAMELEN bytes
1976 */
1977 int
1978 dmu_fsname(const char *snapname, char *buf)
1979 {
1980 char *atp = strchr(snapname, '@');
1981 if (atp == NULL)
1982 return (SET_ERROR(EINVAL));
1983 if (atp - snapname >= MAXNAMELEN)
1984 return (SET_ERROR(ENAMETOOLONG));
1985 (void) strlcpy(buf, snapname, atp - snapname + 1);
1986 return (0);
1987 }
1988
1989 #if defined(_KERNEL) && defined(HAVE_SPL)
1990 EXPORT_SYMBOL(dmu_objset_zil);
1991 EXPORT_SYMBOL(dmu_objset_pool);
1992 EXPORT_SYMBOL(dmu_objset_ds);
1993 EXPORT_SYMBOL(dmu_objset_type);
1994 EXPORT_SYMBOL(dmu_objset_name);
1995 EXPORT_SYMBOL(dmu_objset_hold);
1996 EXPORT_SYMBOL(dmu_objset_own);
1997 EXPORT_SYMBOL(dmu_objset_rele);
1998 EXPORT_SYMBOL(dmu_objset_disown);
1999 EXPORT_SYMBOL(dmu_objset_from_ds);
2000 EXPORT_SYMBOL(dmu_objset_create);
2001 EXPORT_SYMBOL(dmu_objset_clone);
2002 EXPORT_SYMBOL(dmu_objset_stats);
2003 EXPORT_SYMBOL(dmu_objset_fast_stat);
2004 EXPORT_SYMBOL(dmu_objset_spa);
2005 EXPORT_SYMBOL(dmu_objset_space);
2006 EXPORT_SYMBOL(dmu_objset_fsid_guid);
2007 EXPORT_SYMBOL(dmu_objset_find);
2008 EXPORT_SYMBOL(dmu_objset_byteswap);
2009 EXPORT_SYMBOL(dmu_objset_evict_dbufs);
2010 EXPORT_SYMBOL(dmu_objset_snap_cmtime);
2011
2012 EXPORT_SYMBOL(dmu_objset_sync);
2013 EXPORT_SYMBOL(dmu_objset_is_dirty);
2014 EXPORT_SYMBOL(dmu_objset_create_impl);
2015 EXPORT_SYMBOL(dmu_objset_open_impl);
2016 EXPORT_SYMBOL(dmu_objset_evict);
2017 EXPORT_SYMBOL(dmu_objset_register_type);
2018 EXPORT_SYMBOL(dmu_objset_do_userquota_updates);
2019 EXPORT_SYMBOL(dmu_objset_userquota_get_ids);
2020 EXPORT_SYMBOL(dmu_objset_userused_enabled);
2021 EXPORT_SYMBOL(dmu_objset_userspace_upgrade);
2022 EXPORT_SYMBOL(dmu_objset_userspace_present);
2023 #endif