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