1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
24 * Copyright (c) 2012, Joyent, Inc. 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
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 /*
282 * XXX when bprewrite scrub can change the bp,
283 * and this is called from dmu_objset_open_ds_os, the bp
284 * could change, and we'll need a lock.
285 */
286 err = dsl_read_nolock(NULL, spa, os->os_rootbp,
287 arc_getbuf_func, &os->os_phys_buf,
288 ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
289 if (err) {
290 kmem_free(os, sizeof (objset_t));
291 /* convert checksum errors into IO errors */
292 if (err == ECKSUM)
293 err = EIO;
294 return (err);
295 }
296
297 /* Increase the blocksize if we are permitted. */
298 if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
299 arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
300 arc_buf_t *buf = arc_buf_alloc(spa,
301 sizeof (objset_phys_t), &os->os_phys_buf,
302 ARC_BUFC_METADATA);
303 bzero(buf->b_data, sizeof (objset_phys_t));
304 bcopy(os->os_phys_buf->b_data, buf->b_data,
305 arc_buf_size(os->os_phys_buf));
306 (void) arc_buf_remove_ref(os->os_phys_buf,
307 &os->os_phys_buf);
308 os->os_phys_buf = buf;
309 }
310
311 os->os_phys = os->os_phys_buf->b_data;
312 os->os_flags = os->os_phys->os_flags;
313 } else {
314 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
315 sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
316 os->os_phys_buf = arc_buf_alloc(spa, size,
317 &os->os_phys_buf, ARC_BUFC_METADATA);
318 os->os_phys = os->os_phys_buf->b_data;
319 bzero(os->os_phys, size);
320 }
321
322 /*
323 * Note: the changed_cb will be called once before the register
324 * func returns, thus changing the checksum/compression from the
325 * default (fletcher2/off). Snapshots don't need to know about
326 * checksum/compression/copies.
327 */
328 if (ds) {
329 err = dsl_prop_register(ds, "primarycache",
330 primary_cache_changed_cb, os);
331 if (err == 0)
332 err = dsl_prop_register(ds, "secondarycache",
333 secondary_cache_changed_cb, os);
334 if (!dsl_dataset_is_snapshot(ds)) {
335 if (err == 0)
336 err = dsl_prop_register(ds, "checksum",
337 checksum_changed_cb, os);
338 if (err == 0)
339 err = dsl_prop_register(ds, "compression",
340 compression_changed_cb, os);
341 if (err == 0)
342 err = dsl_prop_register(ds, "copies",
343 copies_changed_cb, os);
344 if (err == 0)
345 err = dsl_prop_register(ds, "dedup",
346 dedup_changed_cb, os);
347 if (err == 0)
348 err = dsl_prop_register(ds, "logbias",
349 logbias_changed_cb, os);
350 if (err == 0)
351 err = dsl_prop_register(ds, "sync",
352 sync_changed_cb, os);
353 }
354 if (err) {
355 VERIFY(arc_buf_remove_ref(os->os_phys_buf,
356 &os->os_phys_buf) == 1);
357 kmem_free(os, sizeof (objset_t));
358 return (err);
359 }
360 } else if (ds == NULL) {
361 /* It's the meta-objset. */
362 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
363 os->os_compress = ZIO_COMPRESS_LZJB;
364 os->os_copies = spa_max_replication(spa);
365 os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
366 os->os_dedup_verify = 0;
367 os->os_logbias = 0;
368 os->os_sync = 0;
369 os->os_primary_cache = ZFS_CACHE_ALL;
370 os->os_secondary_cache = ZFS_CACHE_ALL;
371 }
372
373 if (ds == NULL || !dsl_dataset_is_snapshot(ds))
374 os->os_zil_header = os->os_phys->os_zil_header;
375 os->os_zil = zil_alloc(os, &os->os_zil_header);
376
377 for (i = 0; i < TXG_SIZE; i++) {
378 list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
379 offsetof(dnode_t, dn_dirty_link[i]));
380 list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
381 offsetof(dnode_t, dn_dirty_link[i]));
382 }
383 list_create(&os->os_dnodes, sizeof (dnode_t),
384 offsetof(dnode_t, dn_link));
385 list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
386 offsetof(dmu_buf_impl_t, db_link));
387
388 mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
389 mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
390 mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
391
392 DMU_META_DNODE(os) = dnode_special_open(os,
393 &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
394 &os->os_meta_dnode);
395 if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
396 DMU_USERUSED_DNODE(os) = dnode_special_open(os,
397 &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
398 &os->os_userused_dnode);
399 DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
400 &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
401 &os->os_groupused_dnode);
402 }
403
404 /*
405 * We should be the only thread trying to do this because we
406 * have ds_opening_lock
407 */
408 if (ds) {
409 mutex_enter(&ds->ds_lock);
410 ASSERT(ds->ds_objset == NULL);
411 ds->ds_objset = os;
412 mutex_exit(&ds->ds_lock);
413 }
414
415 *osp = os;
416 return (0);
417 }
418
419 int
420 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
421 {
422 int err = 0;
423
424 mutex_enter(&ds->ds_opening_lock);
425 *osp = ds->ds_objset;
426 if (*osp == NULL) {
427 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
428 ds, dsl_dataset_get_blkptr(ds), osp);
429 }
430 mutex_exit(&ds->ds_opening_lock);
431 return (err);
432 }
433
434 /* called from zpl */
435 int
436 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
437 {
438 dsl_dataset_t *ds;
439 int err;
440
441 err = dsl_dataset_hold(name, tag, &ds);
442 if (err)
443 return (err);
444
445 err = dmu_objset_from_ds(ds, osp);
446 if (err)
447 dsl_dataset_rele(ds, tag);
448
449 return (err);
450 }
451
452 /* called from zpl */
453 int
454 dmu_objset_own(const char *name, dmu_objset_type_t type,
455 boolean_t readonly, void *tag, objset_t **osp)
456 {
457 dsl_dataset_t *ds;
458 int err;
459
460 err = dsl_dataset_own(name, B_FALSE, tag, &ds);
461 if (err)
462 return (err);
463
464 err = dmu_objset_from_ds(ds, osp);
465 if (err) {
466 dsl_dataset_disown(ds, tag);
467 } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
468 dmu_objset_disown(*osp, tag);
469 return (EINVAL);
470 } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
471 dmu_objset_disown(*osp, tag);
472 return (EROFS);
473 }
474 return (err);
475 }
476
477 void
478 dmu_objset_rele(objset_t *os, void *tag)
479 {
480 dsl_dataset_rele(os->os_dsl_dataset, tag);
481 }
482
483 void
484 dmu_objset_disown(objset_t *os, void *tag)
485 {
486 dsl_dataset_disown(os->os_dsl_dataset, tag);
487 }
488
489 int
490 dmu_objset_evict_dbufs(objset_t *os)
491 {
492 dnode_t *dn;
493
494 mutex_enter(&os->os_lock);
495
496 /* process the mdn last, since the other dnodes have holds on it */
497 list_remove(&os->os_dnodes, DMU_META_DNODE(os));
498 list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
499
500 /*
501 * Find the first dnode with holds. We have to do this dance
502 * because dnode_add_ref() only works if you already have a
503 * hold. If there are no holds then it has no dbufs so OK to
504 * skip.
505 */
506 for (dn = list_head(&os->os_dnodes);
507 dn && !dnode_add_ref(dn, FTAG);
508 dn = list_next(&os->os_dnodes, dn))
509 continue;
510
511 while (dn) {
512 dnode_t *next_dn = dn;
513
514 do {
515 next_dn = list_next(&os->os_dnodes, next_dn);
516 } while (next_dn && !dnode_add_ref(next_dn, FTAG));
517
518 mutex_exit(&os->os_lock);
519 dnode_evict_dbufs(dn);
520 dnode_rele(dn, FTAG);
521 mutex_enter(&os->os_lock);
522 dn = next_dn;
523 }
524 dn = list_head(&os->os_dnodes);
525 mutex_exit(&os->os_lock);
526 return (dn != DMU_META_DNODE(os));
527 }
528
529 void
530 dmu_objset_evict(objset_t *os)
531 {
532 dsl_dataset_t *ds = os->os_dsl_dataset;
533
534 for (int t = 0; t < TXG_SIZE; t++)
535 ASSERT(!dmu_objset_is_dirty(os, t));
536
537 if (ds) {
538 if (!dsl_dataset_is_snapshot(ds)) {
539 VERIFY(0 == dsl_prop_unregister(ds, "checksum",
540 checksum_changed_cb, os));
541 VERIFY(0 == dsl_prop_unregister(ds, "compression",
542 compression_changed_cb, os));
543 VERIFY(0 == dsl_prop_unregister(ds, "copies",
544 copies_changed_cb, os));
545 VERIFY(0 == dsl_prop_unregister(ds, "dedup",
546 dedup_changed_cb, os));
547 VERIFY(0 == dsl_prop_unregister(ds, "logbias",
548 logbias_changed_cb, os));
549 VERIFY(0 == dsl_prop_unregister(ds, "sync",
550 sync_changed_cb, os));
551 }
552 VERIFY(0 == dsl_prop_unregister(ds, "primarycache",
553 primary_cache_changed_cb, os));
554 VERIFY(0 == dsl_prop_unregister(ds, "secondarycache",
555 secondary_cache_changed_cb, os));
556 }
557
558 if (os->os_sa)
559 sa_tear_down(os);
560
561 /*
562 * We should need only a single pass over the dnode list, since
563 * nothing can be added to the list at this point.
564 */
565 (void) dmu_objset_evict_dbufs(os);
566
567 dnode_special_close(&os->os_meta_dnode);
568 if (DMU_USERUSED_DNODE(os)) {
569 dnode_special_close(&os->os_userused_dnode);
570 dnode_special_close(&os->os_groupused_dnode);
571 }
572 zil_free(os->os_zil);
573
574 ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
575
576 VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf) == 1);
577
578 /*
579 * This is a barrier to prevent the objset from going away in
580 * dnode_move() until we can safely ensure that the objset is still in
581 * use. We consider the objset valid before the barrier and invalid
582 * after the barrier.
583 */
584 rw_enter(&os_lock, RW_READER);
585 rw_exit(&os_lock);
586
587 mutex_destroy(&os->os_lock);
588 mutex_destroy(&os->os_obj_lock);
589 mutex_destroy(&os->os_user_ptr_lock);
590 kmem_free(os, sizeof (objset_t));
591 }
592
593 timestruc_t
594 dmu_objset_snap_cmtime(objset_t *os)
595 {
596 return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
597 }
598
599 /* called from dsl for meta-objset */
600 objset_t *
601 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
602 dmu_objset_type_t type, dmu_tx_t *tx)
603 {
604 objset_t *os;
605 dnode_t *mdn;
606
607 ASSERT(dmu_tx_is_syncing(tx));
608 if (ds != NULL)
609 VERIFY(0 == dmu_objset_from_ds(ds, &os));
610 else
611 VERIFY(0 == dmu_objset_open_impl(spa, NULL, bp, &os));
612
613 mdn = DMU_META_DNODE(os);
614
615 dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
616 DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
617
618 /*
619 * We don't want to have to increase the meta-dnode's nlevels
620 * later, because then we could do it in quescing context while
621 * we are also accessing it in open context.
622 *
623 * This precaution is not necessary for the MOS (ds == NULL),
624 * because the MOS is only updated in syncing context.
625 * This is most fortunate: the MOS is the only objset that
626 * needs to be synced multiple times as spa_sync() iterates
627 * to convergence, so minimizing its dn_nlevels matters.
628 */
629 if (ds != NULL) {
630 int levels = 1;
631
632 /*
633 * Determine the number of levels necessary for the meta-dnode
634 * to contain DN_MAX_OBJECT dnodes.
635 */
636 while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
637 (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
638 DN_MAX_OBJECT * sizeof (dnode_phys_t))
639 levels++;
640
641 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
642 mdn->dn_nlevels = levels;
643 }
644
645 ASSERT(type != DMU_OST_NONE);
646 ASSERT(type != DMU_OST_ANY);
647 ASSERT(type < DMU_OST_NUMTYPES);
648 os->os_phys->os_type = type;
649 if (dmu_objset_userused_enabled(os)) {
650 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
651 os->os_flags = os->os_phys->os_flags;
652 }
653
654 dsl_dataset_dirty(ds, tx);
655
656 return (os);
657 }
658
659 struct oscarg {
660 void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
661 void *userarg;
662 dsl_dataset_t *clone_origin;
663 const char *lastname;
664 dmu_objset_type_t type;
665 uint64_t flags;
666 cred_t *cr;
667 };
668
669 /*ARGSUSED*/
670 static int
671 dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
672 {
673 dsl_dir_t *dd = arg1;
674 struct oscarg *oa = arg2;
675 objset_t *mos = dd->dd_pool->dp_meta_objset;
676 int err;
677 uint64_t ddobj;
678
679 err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
680 oa->lastname, sizeof (uint64_t), 1, &ddobj);
681 if (err != ENOENT)
682 return (err ? err : EEXIST);
683
684 if (oa->clone_origin != NULL) {
685 /* You can't clone across pools. */
686 if (oa->clone_origin->ds_dir->dd_pool != dd->dd_pool)
687 return (EXDEV);
688
689 /* You can only clone snapshots, not the head datasets. */
690 if (!dsl_dataset_is_snapshot(oa->clone_origin))
691 return (EINVAL);
692 }
693
694 return (dsl_dir_fscount_check(dd, 1, NULL, oa->cr));
695 }
696
697 static void
698 dmu_objset_create_sync(void *arg1, void *arg2, dmu_tx_t *tx)
699 {
700 dsl_dir_t *dd = arg1;
701 spa_t *spa = dd->dd_pool->dp_spa;
702 struct oscarg *oa = arg2;
703 uint64_t obj;
704 dsl_dataset_t *ds;
705 blkptr_t *bp;
706
707 ASSERT(dmu_tx_is_syncing(tx));
708
709 dsl_dir_fscount_adjust(dd, tx, 1, B_TRUE);
710
711 obj = dsl_dataset_create_sync(dd, oa->lastname,
712 oa->clone_origin, oa->flags, oa->cr, tx);
713
714 VERIFY3U(0, ==, dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds));
715 bp = dsl_dataset_get_blkptr(ds);
716 if (BP_IS_HOLE(bp)) {
717 objset_t *os =
718 dmu_objset_create_impl(spa, ds, bp, oa->type, tx);
719
720 if (oa->userfunc)
721 oa->userfunc(os, oa->userarg, oa->cr, tx);
722 }
723
724 if (oa->clone_origin == NULL) {
725 spa_history_log_internal_ds(ds, "create", tx, "");
726 } else {
727 char namebuf[MAXNAMELEN];
728 dsl_dataset_name(oa->clone_origin, namebuf);
729 spa_history_log_internal_ds(ds, "clone", tx,
730 "origin=%s (%llu)", namebuf, oa->clone_origin->ds_object);
731 }
732 dsl_dataset_rele(ds, FTAG);
733 }
734
735 int
736 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
737 void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
738 {
739 dsl_dir_t *pdd;
740 const char *tail;
741 int err = 0;
742 struct oscarg oa = { 0 };
743
744 ASSERT(strchr(name, '@') == NULL);
745 err = dsl_dir_open(name, FTAG, &pdd, &tail);
746 if (err)
747 return (err);
748 if (tail == NULL) {
749 dsl_dir_close(pdd, FTAG);
750 return (EEXIST);
751 }
752
753 oa.userfunc = func;
754 oa.userarg = arg;
755 oa.lastname = tail;
756 oa.type = type;
757 oa.flags = flags;
758 oa.cr = CRED();
759
760 err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
761 dmu_objset_create_sync, pdd, &oa, 5);
762 dsl_dir_close(pdd, FTAG);
763 return (err);
764 }
765
766 int
767 dmu_objset_clone(const char *name, dsl_dataset_t *clone_origin, uint64_t flags)
768 {
769 dsl_dir_t *pdd;
770 const char *tail;
771 int err = 0;
772 struct oscarg oa = { 0 };
773
774 ASSERT(strchr(name, '@') == NULL);
775 err = dsl_dir_open(name, FTAG, &pdd, &tail);
776 if (err)
777 return (err);
778 if (tail == NULL) {
779 dsl_dir_close(pdd, FTAG);
780 return (EEXIST);
781 }
782
783 oa.lastname = tail;
784 oa.clone_origin = clone_origin;
785 oa.flags = flags;
786 oa.cr = CRED();
787
788 err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
789 dmu_objset_create_sync, pdd, &oa, 5);
790 dsl_dir_close(pdd, FTAG);
791 return (err);
792 }
793
794 int
795 dmu_objset_destroy(const char *name, boolean_t defer)
796 {
797 dsl_dataset_t *ds;
798 int error;
799
800 error = dsl_dataset_own(name, B_TRUE, FTAG, &ds);
801 if (error == 0) {
802 error = dsl_dataset_destroy(ds, FTAG, defer);
803 /* dsl_dataset_destroy() closes the ds. */
804 }
805
806 return (error);
807 }
808
809 typedef struct snapallarg {
810 dsl_sync_task_group_t *saa_dstg;
811 boolean_t saa_needsuspend;
812 nvlist_t *saa_props;
813 cred_t *saa_cr;
814
815 /* the following are used only if 'temporary' is set: */
816 boolean_t saa_temporary;
817 const char *saa_htag;
818 struct dsl_ds_holdarg *saa_ha;
819 dsl_dataset_t *saa_newds;
820 } snapallarg_t;
821
822 typedef struct snaponearg {
823 const char *soa_longname; /* long snap name */
824 const char *soa_snapname; /* short snap name */
825 uint64_t soa_tot_cnt;
826 snapallarg_t *soa_saa;
827 } snaponearg_t;
828
829 static int
830 snapshot_check(void *arg1, void *arg2, dmu_tx_t *tx)
831 {
832 objset_t *os = arg1;
833 snaponearg_t *soa = arg2;
834 snapallarg_t *saa = soa->soa_saa;
835 int error;
836
837 /* The props have already been checked by zfs_check_userprops(). */
838
839 error = dsl_dataset_snapshot_check(os->os_dsl_dataset,
840 soa->soa_snapname, soa->soa_tot_cnt, tx, saa->saa_cr);
841 if (error)
842 return (error);
843
844 if (saa->saa_temporary) {
845 /*
846 * Ideally we would just call
847 * dsl_dataset_user_hold_check() and
848 * dsl_dataset_destroy_check() here. However the
849 * dataset we want to hold and destroy is the snapshot
850 * that we just confirmed we can create, but it won't
851 * exist until after these checks are run. Do any
852 * checks we can here and if more checks are added to
853 * those routines in the future, similar checks may be
854 * necessary here.
855 */
856 if (spa_version(os->os_spa) < SPA_VERSION_USERREFS)
857 return (ENOTSUP);
858 /*
859 * Not checking number of tags because the tag will be
860 * unique, as it will be the only tag.
861 */
862 if (strlen(saa->saa_htag) + MAX_TAG_PREFIX_LEN >= MAXNAMELEN)
863 return (E2BIG);
864
865 saa->saa_ha = kmem_alloc(sizeof (struct dsl_ds_holdarg),
866 KM_SLEEP);
867 saa->saa_ha->temphold = B_TRUE;
868 saa->saa_ha->htag = saa->saa_htag;
869 }
870 return (error);
871 }
872
873 static void
874 snapshot_sync(void *arg1, void *arg2, dmu_tx_t *tx)
875 {
876 objset_t *os = arg1;
877 dsl_dataset_t *ds = os->os_dsl_dataset;
878 snaponearg_t *soa = arg2;
879 snapallarg_t *saa = soa->soa_saa;
880
881 dsl_dataset_snapshot_sync(ds, soa->soa_snapname, tx);
882
883 if (saa->saa_props != NULL) {
884 dsl_props_arg_t pa;
885 pa.pa_props = saa->saa_props;
886 pa.pa_source = ZPROP_SRC_LOCAL;
887 dsl_props_set_sync(ds->ds_prev, &pa, tx);
888 }
889
890 if (saa->saa_temporary) {
891 struct dsl_ds_destroyarg da;
892
893 dsl_dataset_user_hold_sync(ds->ds_prev, saa->saa_ha, tx);
894 kmem_free(saa->saa_ha, sizeof (struct dsl_ds_holdarg));
895 saa->saa_ha = NULL;
896 saa->saa_newds = ds->ds_prev;
897
898 da.ds = ds->ds_prev;
899 da.defer = B_TRUE;
900 dsl_dataset_destroy_sync(&da, FTAG, tx);
901 }
902 }
903
904 static int
905 snapshot_one_impl(const char *snapname, void *arg, uint64_t cnt)
906 {
907 char fsname[MAXPATHLEN];
908 snapallarg_t *saa = arg;
909 snaponearg_t *soa;
910 objset_t *os;
911 int err;
912
913 (void) strlcpy(fsname, snapname, sizeof (fsname));
914 strchr(fsname, '@')[0] = '\0';
915
916 err = dmu_objset_hold(fsname, saa, &os);
917 if (err != 0)
918 return (err);
919
920 /*
921 * If the objset is in an inconsistent state (eg, in the process
922 * of being destroyed), don't snapshot it.
923 */
924 if (os->os_dsl_dataset->ds_phys->ds_flags & DS_FLAG_INCONSISTENT) {
925 dmu_objset_rele(os, saa);
926 return (EBUSY);
927 }
928
929 if (saa->saa_needsuspend) {
930 err = zil_suspend(dmu_objset_zil(os));
931 if (err) {
932 dmu_objset_rele(os, saa);
933 return (err);
934 }
935 }
936
937 soa = kmem_zalloc(sizeof (*soa), KM_SLEEP);
938 soa->soa_saa = saa;
939 soa->soa_longname = snapname;
940 soa->soa_snapname = strchr(snapname, '@') + 1;
941 soa->soa_tot_cnt = cnt;
942
943 dsl_sync_task_create(saa->saa_dstg, snapshot_check, snapshot_sync,
944 os, soa, 3);
945
946 return (0);
947 }
948
949 /*
950 * The snapshots must all be in the same pool.
951 */
952 int
953 dmu_objset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
954 {
955 dsl_sync_task_t *dst;
956 snapallarg_t saa = { 0 };
957 spa_t *spa;
958 int rv = 0;
959 int err;
960 nvpair_t *pair;
961 nvlist_t *cnt_track = NULL;
962 char *pdelim;
963 uint64_t val;
964 char nm[MAXPATHLEN];
965
966 pair = nvlist_next_nvpair(snaps, NULL);
967 if (pair == NULL)
968 return (0);
969
970 err = spa_open(nvpair_name(pair), &spa, FTAG);
971 if (err)
972 return (err);
973 saa.saa_dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
974 saa.saa_props = props;
975 saa.saa_needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
976 saa.saa_cr = CRED();
977
978 /*
979 * Pre-compute how many total new snapshots will be created for each
980 * level in the tree and below. This is needed for validating the
981 * snapshot limit when taking a recursive snapshot.
982 *
983 * The problem is that the counts are not actually adjusted when
984 * we are checking, only when we finally sync. For a single snapshot,
985 * this is easy, the count will increase by 1 at each node up the tree,
986 * but its more complicated for recursive snapshots. Since we are
987 * validating each snapshot independently we need to be sure that we
988 * are validating the complete count for the entire set of snapshots.
989 * We do this by rolling up the counts for each component of the name
990 * into an nvlist then we'll use that count in the validation of each
991 * individual snapshot.
992 *
993 * We validated the snapshot names in zfs_ioc_snapshot so we know they
994 * have a '@'.
995 */
996 cnt_track = fnvlist_alloc();
997
998 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
999 pair = nvlist_next_nvpair(snaps, pair)) {
1000 (void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1001 pdelim = strchr(nm, '@');
1002 *pdelim = '\0';
1003
1004 do {
1005 if (nvlist_lookup_uint64(cnt_track, nm, &val) == 0) {
1006 /* update existing entry */
1007 fnvlist_add_uint64(cnt_track, nm, val + 1);
1008 } else {
1009 /* add to list */
1010 fnvlist_add_uint64(cnt_track, nm, 1);
1011 }
1012
1013 pdelim = strrchr(nm, '/');
1014 if (pdelim != NULL)
1015 *pdelim = '\0';
1016 } while (pdelim != NULL);
1017 }
1018
1019 /*
1020 * We've calculated the counts, now validate.
1021 */
1022 for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
1023 pair = nvlist_next_nvpair(snaps, pair)) {
1024 (void) strlcpy(nm, nvpair_name(pair), sizeof (nm));
1025 pdelim = strchr(nm, '@');
1026 *pdelim = '\0';
1027
1028 val = fnvlist_lookup_uint64(cnt_track, nm);
1029 err = snapshot_one_impl(nvpair_name(pair), &saa, val);
1030 if (err != 0) {
1031 if (errors != NULL) {
1032 fnvlist_add_int32(errors,
1033 nvpair_name(pair), err);
1034 }
1035 rv = err;
1036 }
1037 }
1038
1039 nvlist_free(cnt_track);
1040
1041 /*
1042 * If any call to snapshot_one_impl() failed, don't execute the
1043 * sync task. The error handling code below will clean up the
1044 * snaponearg_t from any successful calls to
1045 * snapshot_one_impl().
1046 */
1047 if (rv == 0)
1048 err = dsl_sync_task_group_wait(saa.saa_dstg);
1049 if (err != 0)
1050 rv = err;
1051
1052 for (dst = list_head(&saa.saa_dstg->dstg_tasks); dst;
1053 dst = list_next(&saa.saa_dstg->dstg_tasks, dst)) {
1054 objset_t *os = dst->dst_arg1;
1055 snaponearg_t *soa = dst->dst_arg2;
1056 if (dst->dst_err != 0) {
1057 if (errors != NULL) {
1058 fnvlist_add_int32(errors,
1059 soa->soa_longname, dst->dst_err);
1060 }
1061 rv = dst->dst_err;
1062 }
1063
1064 if (saa.saa_needsuspend)
1065 zil_resume(dmu_objset_zil(os));
1066 dmu_objset_rele(os, &saa);
1067 kmem_free(soa, sizeof (*soa));
1068 }
1069
1070 dsl_sync_task_group_destroy(saa.saa_dstg);
1071 spa_close(spa, FTAG);
1072 return (rv);
1073 }
1074
1075 int
1076 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
1077 {
1078 int err;
1079 char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
1080 nvlist_t *snaps = fnvlist_alloc();
1081
1082 fnvlist_add_boolean(snaps, longsnap);
1083 err = dmu_objset_snapshot(snaps, NULL, NULL);
1084 fnvlist_free(snaps);
1085 strfree(longsnap);
1086 return (err);
1087 }
1088
1089 int
1090 dmu_objset_snapshot_tmp(const char *snapname, const char *tag, int cleanup_fd)
1091 {
1092 dsl_sync_task_t *dst;
1093 snapallarg_t saa = { 0 };
1094 spa_t *spa;
1095 minor_t minor;
1096 int err;
1097
1098 err = spa_open(snapname, &spa, FTAG);
1099 if (err)
1100 return (err);
1101 saa.saa_dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
1102 saa.saa_htag = tag;
1103 saa.saa_needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
1104 saa.saa_temporary = B_TRUE;
1105
1106 if (cleanup_fd < 0) {
1107 spa_close(spa, FTAG);
1108 return (EINVAL);
1109 }
1110 if ((err = zfs_onexit_fd_hold(cleanup_fd, &minor)) != 0) {
1111 spa_close(spa, FTAG);
1112 return (err);
1113 }
1114
1115 err = snapshot_one_impl(snapname, &saa, 1);
1116
1117 if (err == 0)
1118 err = dsl_sync_task_group_wait(saa.saa_dstg);
1119
1120 for (dst = list_head(&saa.saa_dstg->dstg_tasks); dst;
1121 dst = list_next(&saa.saa_dstg->dstg_tasks, dst)) {
1122 objset_t *os = dst->dst_arg1;
1123 dsl_register_onexit_hold_cleanup(saa.saa_newds, tag, minor);
1124 if (saa.saa_needsuspend)
1125 zil_resume(dmu_objset_zil(os));
1126 dmu_objset_rele(os, &saa);
1127 }
1128
1129 zfs_onexit_fd_rele(cleanup_fd);
1130 dsl_sync_task_group_destroy(saa.saa_dstg);
1131 spa_close(spa, FTAG);
1132 return (err);
1133 }
1134
1135
1136 static void
1137 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
1138 {
1139 dnode_t *dn;
1140
1141 while (dn = list_head(list)) {
1142 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
1143 ASSERT(dn->dn_dbuf->db_data_pending);
1144 /*
1145 * Initialize dn_zio outside dnode_sync() because the
1146 * meta-dnode needs to set it ouside dnode_sync().
1147 */
1148 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
1149 ASSERT(dn->dn_zio);
1150
1151 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
1152 list_remove(list, dn);
1153
1154 if (newlist) {
1155 (void) dnode_add_ref(dn, newlist);
1156 list_insert_tail(newlist, dn);
1157 }
1158
1159 dnode_sync(dn, tx);
1160 }
1161 }
1162
1163 /* ARGSUSED */
1164 static void
1165 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
1166 {
1167 blkptr_t *bp = zio->io_bp;
1168 objset_t *os = arg;
1169 dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
1170
1171 ASSERT(bp == os->os_rootbp);
1172 ASSERT(BP_GET_TYPE(bp) == DMU_OT_OBJSET);
1173 ASSERT(BP_GET_LEVEL(bp) == 0);
1174
1175 /*
1176 * Update rootbp fill count: it should be the number of objects
1177 * allocated in the object set (not counting the "special"
1178 * objects that are stored in the objset_phys_t -- the meta
1179 * dnode and user/group accounting objects).
1180 */
1181 bp->blk_fill = 0;
1182 for (int i = 0; i < dnp->dn_nblkptr; i++)
1183 bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
1184 }
1185
1186 /* ARGSUSED */
1187 static void
1188 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1189 {
1190 blkptr_t *bp = zio->io_bp;
1191 blkptr_t *bp_orig = &zio->io_bp_orig;
1192 objset_t *os = arg;
1193
1194 if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1195 ASSERT(BP_EQUAL(bp, bp_orig));
1196 } else {
1197 dsl_dataset_t *ds = os->os_dsl_dataset;
1198 dmu_tx_t *tx = os->os_synctx;
1199
1200 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1201 dsl_dataset_block_born(ds, bp, tx);
1202 }
1203 }
1204
1205 /* called from dsl */
1206 void
1207 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1208 {
1209 int txgoff;
1210 zbookmark_t zb;
1211 zio_prop_t zp;
1212 zio_t *zio;
1213 list_t *list;
1214 list_t *newlist = NULL;
1215 dbuf_dirty_record_t *dr;
1216
1217 dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1218
1219 ASSERT(dmu_tx_is_syncing(tx));
1220 /* XXX the write_done callback should really give us the tx... */
1221 os->os_synctx = tx;
1222
1223 if (os->os_dsl_dataset == NULL) {
1224 /*
1225 * This is the MOS. If we have upgraded,
1226 * spa_max_replication() could change, so reset
1227 * os_copies here.
1228 */
1229 os->os_copies = spa_max_replication(os->os_spa);
1230 }
1231
1232 /*
1233 * Create the root block IO
1234 */
1235 SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1236 os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1237 ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1238 VERIFY3U(0, ==, arc_release_bp(os->os_phys_buf, &os->os_phys_buf,
1239 os->os_rootbp, os->os_spa, &zb));
1240
1241 dmu_write_policy(os, NULL, 0, 0, &zp);
1242
1243 zio = arc_write(pio, os->os_spa, tx->tx_txg,
1244 os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
1245 dmu_objset_write_ready, dmu_objset_write_done, os,
1246 ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, &zb);
1247
1248 /*
1249 * Sync special dnodes - the parent IO for the sync is the root block
1250 */
1251 DMU_META_DNODE(os)->dn_zio = zio;
1252 dnode_sync(DMU_META_DNODE(os), tx);
1253
1254 os->os_phys->os_flags = os->os_flags;
1255
1256 if (DMU_USERUSED_DNODE(os) &&
1257 DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1258 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1259 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1260 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1261 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1262 }
1263
1264 txgoff = tx->tx_txg & TXG_MASK;
1265
1266 if (dmu_objset_userused_enabled(os)) {
1267 newlist = &os->os_synced_dnodes;
1268 /*
1269 * We must create the list here because it uses the
1270 * dn_dirty_link[] of this txg.
1271 */
1272 list_create(newlist, sizeof (dnode_t),
1273 offsetof(dnode_t, dn_dirty_link[txgoff]));
1274 }
1275
1276 dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1277 dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1278
1279 list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1280 while (dr = list_head(list)) {
1281 ASSERT(dr->dr_dbuf->db_level == 0);
1282 list_remove(list, dr);
1283 if (dr->dr_zio)
1284 zio_nowait(dr->dr_zio);
1285 }
1286 /*
1287 * Free intent log blocks up to this tx.
1288 */
1289 zil_sync(os->os_zil, tx);
1290 os->os_phys->os_zil_header = os->os_zil_header;
1291 zio_nowait(zio);
1292 }
1293
1294 boolean_t
1295 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1296 {
1297 return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1298 !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1299 }
1300
1301 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1302
1303 void
1304 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1305 {
1306 used_cbs[ost] = cb;
1307 }
1308
1309 boolean_t
1310 dmu_objset_userused_enabled(objset_t *os)
1311 {
1312 return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1313 used_cbs[os->os_phys->os_type] != NULL &&
1314 DMU_USERUSED_DNODE(os) != NULL);
1315 }
1316
1317 static void
1318 do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1319 uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
1320 {
1321 if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1322 int64_t delta = DNODE_SIZE + used;
1323 if (subtract)
1324 delta = -delta;
1325 VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
1326 user, delta, tx));
1327 VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1328 group, delta, tx));
1329 }
1330 }
1331
1332 void
1333 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1334 {
1335 dnode_t *dn;
1336 list_t *list = &os->os_synced_dnodes;
1337
1338 ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1339
1340 while (dn = list_head(list)) {
1341 int flags;
1342 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1343 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1344 dn->dn_phys->dn_flags &
1345 DNODE_FLAG_USERUSED_ACCOUNTED);
1346
1347 /* Allocate the user/groupused objects if necessary. */
1348 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1349 VERIFY(0 == zap_create_claim(os,
1350 DMU_USERUSED_OBJECT,
1351 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1352 VERIFY(0 == zap_create_claim(os,
1353 DMU_GROUPUSED_OBJECT,
1354 DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1355 }
1356
1357 /*
1358 * We intentionally modify the zap object even if the
1359 * net delta is zero. Otherwise
1360 * the block of the zap obj could be shared between
1361 * datasets but need to be different between them after
1362 * a bprewrite.
1363 */
1364
1365 flags = dn->dn_id_flags;
1366 ASSERT(flags);
1367 if (flags & DN_ID_OLD_EXIST) {
1368 do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1369 dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1370 }
1371 if (flags & DN_ID_NEW_EXIST) {
1372 do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1373 dn->dn_phys->dn_flags, dn->dn_newuid,
1374 dn->dn_newgid, B_FALSE, tx);
1375 }
1376
1377 mutex_enter(&dn->dn_mtx);
1378 dn->dn_oldused = 0;
1379 dn->dn_oldflags = 0;
1380 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1381 dn->dn_olduid = dn->dn_newuid;
1382 dn->dn_oldgid = dn->dn_newgid;
1383 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1384 if (dn->dn_bonuslen == 0)
1385 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1386 else
1387 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1388 }
1389 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1390 mutex_exit(&dn->dn_mtx);
1391
1392 list_remove(list, dn);
1393 dnode_rele(dn, list);
1394 }
1395 }
1396
1397 /*
1398 * Returns a pointer to data to find uid/gid from
1399 *
1400 * If a dirty record for transaction group that is syncing can't
1401 * be found then NULL is returned. In the NULL case it is assumed
1402 * the uid/gid aren't changing.
1403 */
1404 static void *
1405 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1406 {
1407 dbuf_dirty_record_t *dr, **drp;
1408 void *data;
1409
1410 if (db->db_dirtycnt == 0)
1411 return (db->db.db_data); /* Nothing is changing */
1412
1413 for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1414 if (dr->dr_txg == tx->tx_txg)
1415 break;
1416
1417 if (dr == NULL) {
1418 data = NULL;
1419 } else {
1420 dnode_t *dn;
1421
1422 DB_DNODE_ENTER(dr->dr_dbuf);
1423 dn = DB_DNODE(dr->dr_dbuf);
1424
1425 if (dn->dn_bonuslen == 0 &&
1426 dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1427 data = dr->dt.dl.dr_data->b_data;
1428 else
1429 data = dr->dt.dl.dr_data;
1430
1431 DB_DNODE_EXIT(dr->dr_dbuf);
1432 }
1433
1434 return (data);
1435 }
1436
1437 void
1438 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1439 {
1440 objset_t *os = dn->dn_objset;
1441 void *data = NULL;
1442 dmu_buf_impl_t *db = NULL;
1443 uint64_t *user, *group;
1444 int flags = dn->dn_id_flags;
1445 int error;
1446 boolean_t have_spill = B_FALSE;
1447
1448 if (!dmu_objset_userused_enabled(dn->dn_objset))
1449 return;
1450
1451 if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1452 DN_ID_CHKED_SPILL)))
1453 return;
1454
1455 if (before && dn->dn_bonuslen != 0)
1456 data = DN_BONUS(dn->dn_phys);
1457 else if (!before && dn->dn_bonuslen != 0) {
1458 if (dn->dn_bonus) {
1459 db = dn->dn_bonus;
1460 mutex_enter(&db->db_mtx);
1461 data = dmu_objset_userquota_find_data(db, tx);
1462 } else {
1463 data = DN_BONUS(dn->dn_phys);
1464 }
1465 } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1466 int rf = 0;
1467
1468 if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1469 rf |= DB_RF_HAVESTRUCT;
1470 error = dmu_spill_hold_by_dnode(dn,
1471 rf | DB_RF_MUST_SUCCEED,
1472 FTAG, (dmu_buf_t **)&db);
1473 ASSERT(error == 0);
1474 mutex_enter(&db->db_mtx);
1475 data = (before) ? db->db.db_data :
1476 dmu_objset_userquota_find_data(db, tx);
1477 have_spill = B_TRUE;
1478 } else {
1479 mutex_enter(&dn->dn_mtx);
1480 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1481 mutex_exit(&dn->dn_mtx);
1482 return;
1483 }
1484
1485 if (before) {
1486 ASSERT(data);
1487 user = &dn->dn_olduid;
1488 group = &dn->dn_oldgid;
1489 } else if (data) {
1490 user = &dn->dn_newuid;
1491 group = &dn->dn_newgid;
1492 }
1493
1494 /*
1495 * Must always call the callback in case the object
1496 * type has changed and that type isn't an object type to track
1497 */
1498 error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1499 user, group);
1500
1501 /*
1502 * Preserve existing uid/gid when the callback can't determine
1503 * what the new uid/gid are and the callback returned EEXIST.
1504 * The EEXIST error tells us to just use the existing uid/gid.
1505 * If we don't know what the old values are then just assign
1506 * them to 0, since that is a new file being created.
1507 */
1508 if (!before && data == NULL && error == EEXIST) {
1509 if (flags & DN_ID_OLD_EXIST) {
1510 dn->dn_newuid = dn->dn_olduid;
1511 dn->dn_newgid = dn->dn_oldgid;
1512 } else {
1513 dn->dn_newuid = 0;
1514 dn->dn_newgid = 0;
1515 }
1516 error = 0;
1517 }
1518
1519 if (db)
1520 mutex_exit(&db->db_mtx);
1521
1522 mutex_enter(&dn->dn_mtx);
1523 if (error == 0 && before)
1524 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1525 if (error == 0 && !before)
1526 dn->dn_id_flags |= DN_ID_NEW_EXIST;
1527
1528 if (have_spill) {
1529 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1530 } else {
1531 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1532 }
1533 mutex_exit(&dn->dn_mtx);
1534 if (have_spill)
1535 dmu_buf_rele((dmu_buf_t *)db, FTAG);
1536 }
1537
1538 boolean_t
1539 dmu_objset_userspace_present(objset_t *os)
1540 {
1541 return (os->os_phys->os_flags &
1542 OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1543 }
1544
1545 int
1546 dmu_objset_userspace_upgrade(objset_t *os)
1547 {
1548 uint64_t obj;
1549 int err = 0;
1550
1551 if (dmu_objset_userspace_present(os))
1552 return (0);
1553 if (!dmu_objset_userused_enabled(os))
1554 return (ENOTSUP);
1555 if (dmu_objset_is_snapshot(os))
1556 return (EINVAL);
1557
1558 /*
1559 * We simply need to mark every object dirty, so that it will be
1560 * synced out and now accounted. If this is called
1561 * concurrently, or if we already did some work before crashing,
1562 * that's fine, since we track each object's accounted state
1563 * independently.
1564 */
1565
1566 for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1567 dmu_tx_t *tx;
1568 dmu_buf_t *db;
1569 int objerr;
1570
1571 if (issig(JUSTLOOKING) && issig(FORREAL))
1572 return (EINTR);
1573
1574 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1575 if (objerr)
1576 continue;
1577 tx = dmu_tx_create(os);
1578 dmu_tx_hold_bonus(tx, obj);
1579 objerr = dmu_tx_assign(tx, TXG_WAIT);
1580 if (objerr) {
1581 dmu_tx_abort(tx);
1582 continue;
1583 }
1584 dmu_buf_will_dirty(db, tx);
1585 dmu_buf_rele(db, FTAG);
1586 dmu_tx_commit(tx);
1587 }
1588
1589 os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1590 txg_wait_synced(dmu_objset_pool(os), 0);
1591 return (0);
1592 }
1593
1594 void
1595 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1596 uint64_t *usedobjsp, uint64_t *availobjsp)
1597 {
1598 dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1599 usedobjsp, availobjsp);
1600 }
1601
1602 uint64_t
1603 dmu_objset_fsid_guid(objset_t *os)
1604 {
1605 return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1606 }
1607
1608 void
1609 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1610 {
1611 stat->dds_type = os->os_phys->os_type;
1612 if (os->os_dsl_dataset)
1613 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1614 }
1615
1616 void
1617 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1618 {
1619 ASSERT(os->os_dsl_dataset ||
1620 os->os_phys->os_type == DMU_OST_META);
1621
1622 if (os->os_dsl_dataset != NULL)
1623 dsl_dataset_stats(os->os_dsl_dataset, nv);
1624
1625 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1626 os->os_phys->os_type);
1627 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1628 dmu_objset_userspace_present(os));
1629 }
1630
1631 int
1632 dmu_objset_is_snapshot(objset_t *os)
1633 {
1634 if (os->os_dsl_dataset != NULL)
1635 return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1636 else
1637 return (B_FALSE);
1638 }
1639
1640 int
1641 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1642 boolean_t *conflict)
1643 {
1644 dsl_dataset_t *ds = os->os_dsl_dataset;
1645 uint64_t ignored;
1646
1647 if (ds->ds_phys->ds_snapnames_zapobj == 0)
1648 return (ENOENT);
1649
1650 return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1651 ds->ds_phys->ds_snapnames_zapobj, name, 8, 1, &ignored, MT_FIRST,
1652 real, maxlen, conflict));
1653 }
1654
1655 int
1656 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1657 uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1658 {
1659 dsl_dataset_t *ds = os->os_dsl_dataset;
1660 zap_cursor_t cursor;
1661 zap_attribute_t attr;
1662
1663 if (ds->ds_phys->ds_snapnames_zapobj == 0)
1664 return (ENOENT);
1665
1666 zap_cursor_init_serialized(&cursor,
1667 ds->ds_dir->dd_pool->dp_meta_objset,
1668 ds->ds_phys->ds_snapnames_zapobj, *offp);
1669
1670 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1671 zap_cursor_fini(&cursor);
1672 return (ENOENT);
1673 }
1674
1675 if (strlen(attr.za_name) + 1 > namelen) {
1676 zap_cursor_fini(&cursor);
1677 return (ENAMETOOLONG);
1678 }
1679
1680 (void) strcpy(name, attr.za_name);
1681 if (idp)
1682 *idp = attr.za_first_integer;
1683 if (case_conflict)
1684 *case_conflict = attr.za_normalization_conflict;
1685 zap_cursor_advance(&cursor);
1686 *offp = zap_cursor_serialize(&cursor);
1687 zap_cursor_fini(&cursor);
1688
1689 return (0);
1690 }
1691
1692 int
1693 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1694 uint64_t *idp, uint64_t *offp)
1695 {
1696 dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1697 zap_cursor_t cursor;
1698 zap_attribute_t attr;
1699
1700 /* there is no next dir on a snapshot! */
1701 if (os->os_dsl_dataset->ds_object !=
1702 dd->dd_phys->dd_head_dataset_obj)
1703 return (ENOENT);
1704
1705 zap_cursor_init_serialized(&cursor,
1706 dd->dd_pool->dp_meta_objset,
1707 dd->dd_phys->dd_child_dir_zapobj, *offp);
1708
1709 if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1710 zap_cursor_fini(&cursor);
1711 return (ENOENT);
1712 }
1713
1714 if (strlen(attr.za_name) + 1 > namelen) {
1715 zap_cursor_fini(&cursor);
1716 return (ENAMETOOLONG);
1717 }
1718
1719 (void) strcpy(name, attr.za_name);
1720 if (idp)
1721 *idp = attr.za_first_integer;
1722 zap_cursor_advance(&cursor);
1723 *offp = zap_cursor_serialize(&cursor);
1724 zap_cursor_fini(&cursor);
1725
1726 return (0);
1727 }
1728
1729 struct findarg {
1730 int (*func)(const char *, void *);
1731 void *arg;
1732 };
1733
1734 /* ARGSUSED */
1735 static int
1736 findfunc(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1737 {
1738 struct findarg *fa = arg;
1739 return (fa->func(dsname, fa->arg));
1740 }
1741
1742 /*
1743 * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1744 * Perhaps change all callers to use dmu_objset_find_spa()?
1745 */
1746 int
1747 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
1748 int flags)
1749 {
1750 struct findarg fa;
1751 fa.func = func;
1752 fa.arg = arg;
1753 return (dmu_objset_find_spa(NULL, name, findfunc, &fa, flags));
1754 }
1755
1756 /*
1757 * Find all objsets under name, call func on each
1758 */
1759 int
1760 dmu_objset_find_spa(spa_t *spa, const char *name,
1761 int func(spa_t *, uint64_t, const char *, void *), void *arg, int flags)
1762 {
1763 dsl_dir_t *dd;
1764 dsl_pool_t *dp;
1765 dsl_dataset_t *ds;
1766 zap_cursor_t zc;
1767 zap_attribute_t *attr;
1768 char *child;
1769 uint64_t thisobj;
1770 int err;
1771
1772 if (name == NULL)
1773 name = spa_name(spa);
1774 err = dsl_dir_open_spa(spa, name, FTAG, &dd, NULL);
1775 if (err)
1776 return (err);
1777
1778 /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1779 if (dd->dd_myname[0] == '$') {
1780 dsl_dir_close(dd, FTAG);
1781 return (0);
1782 }
1783
1784 thisobj = dd->dd_phys->dd_head_dataset_obj;
1785 attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1786 dp = dd->dd_pool;
1787
1788 /*
1789 * Iterate over all children.
1790 */
1791 if (flags & DS_FIND_CHILDREN) {
1792 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1793 dd->dd_phys->dd_child_dir_zapobj);
1794 zap_cursor_retrieve(&zc, attr) == 0;
1795 (void) zap_cursor_advance(&zc)) {
1796 ASSERT(attr->za_integer_length == sizeof (uint64_t));
1797 ASSERT(attr->za_num_integers == 1);
1798
1799 child = kmem_asprintf("%s/%s", name, attr->za_name);
1800 err = dmu_objset_find_spa(spa, child, func, arg, flags);
1801 strfree(child);
1802 if (err)
1803 break;
1804 }
1805 zap_cursor_fini(&zc);
1806
1807 if (err) {
1808 dsl_dir_close(dd, FTAG);
1809 kmem_free(attr, sizeof (zap_attribute_t));
1810 return (err);
1811 }
1812 }
1813
1814 /*
1815 * Iterate over all snapshots.
1816 */
1817 if (flags & DS_FIND_SNAPSHOTS) {
1818 if (!dsl_pool_sync_context(dp))
1819 rw_enter(&dp->dp_config_rwlock, RW_READER);
1820 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1821 if (!dsl_pool_sync_context(dp))
1822 rw_exit(&dp->dp_config_rwlock);
1823
1824 if (err == 0) {
1825 uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
1826 dsl_dataset_rele(ds, FTAG);
1827
1828 for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1829 zap_cursor_retrieve(&zc, attr) == 0;
1830 (void) zap_cursor_advance(&zc)) {
1831 ASSERT(attr->za_integer_length ==
1832 sizeof (uint64_t));
1833 ASSERT(attr->za_num_integers == 1);
1834
1835 child = kmem_asprintf("%s@%s",
1836 name, attr->za_name);
1837 err = func(spa, attr->za_first_integer,
1838 child, arg);
1839 strfree(child);
1840 if (err)
1841 break;
1842 }
1843 zap_cursor_fini(&zc);
1844 }
1845 }
1846
1847 dsl_dir_close(dd, FTAG);
1848 kmem_free(attr, sizeof (zap_attribute_t));
1849
1850 if (err)
1851 return (err);
1852
1853 /*
1854 * Apply to self if appropriate.
1855 */
1856 err = func(spa, thisobj, name, arg);
1857 return (err);
1858 }
1859
1860 /* ARGSUSED */
1861 int
1862 dmu_objset_prefetch(const char *name, void *arg)
1863 {
1864 dsl_dataset_t *ds;
1865
1866 if (dsl_dataset_hold(name, FTAG, &ds))
1867 return (0);
1868
1869 if (!BP_IS_HOLE(&ds->ds_phys->ds_bp)) {
1870 mutex_enter(&ds->ds_opening_lock);
1871 if (ds->ds_objset == NULL) {
1872 uint32_t aflags = ARC_NOWAIT | ARC_PREFETCH;
1873 zbookmark_t zb;
1874
1875 SET_BOOKMARK(&zb, ds->ds_object, ZB_ROOT_OBJECT,
1876 ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1877
1878 (void) dsl_read_nolock(NULL, dsl_dataset_get_spa(ds),
1879 &ds->ds_phys->ds_bp, NULL, NULL,
1880 ZIO_PRIORITY_ASYNC_READ,
1881 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
1882 &aflags, &zb);
1883 }
1884 mutex_exit(&ds->ds_opening_lock);
1885 }
1886
1887 dsl_dataset_rele(ds, FTAG);
1888 return (0);
1889 }
1890
1891 void
1892 dmu_objset_set_user(objset_t *os, void *user_ptr)
1893 {
1894 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1895 os->os_user_ptr = user_ptr;
1896 }
1897
1898 void *
1899 dmu_objset_get_user(objset_t *os)
1900 {
1901 ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1902 return (os->os_user_ptr);
1903 }