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