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