xref: /titanic_44/usr/src/uts/common/fs/zfs/dmu_objset.c (revision 1d03c31e0733adea0edef54f0d5d2ea9639ecd2a)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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/zio_checksum.h>
42 #include <sys/zap.h>
43 #include <sys/zil.h>
44 #include <sys/dmu_impl.h>
45 #include <sys/zfs_ioctl.h>
46 
47 
48 spa_t *
49 dmu_objset_spa(objset_t *os)
50 {
51 	return (os->os->os_spa);
52 }
53 
54 zilog_t *
55 dmu_objset_zil(objset_t *os)
56 {
57 	return (os->os->os_zil);
58 }
59 
60 dsl_pool_t *
61 dmu_objset_pool(objset_t *os)
62 {
63 	dsl_dataset_t *ds;
64 
65 	if ((ds = os->os->os_dsl_dataset) != NULL && ds->ds_dir)
66 		return (ds->ds_dir->dd_pool);
67 	else
68 		return (spa_get_dsl(os->os->os_spa));
69 }
70 
71 dsl_dataset_t *
72 dmu_objset_ds(objset_t *os)
73 {
74 	return (os->os->os_dsl_dataset);
75 }
76 
77 dmu_objset_type_t
78 dmu_objset_type(objset_t *os)
79 {
80 	return (os->os->os_phys->os_type);
81 }
82 
83 void
84 dmu_objset_name(objset_t *os, char *buf)
85 {
86 	dsl_dataset_name(os->os->os_dsl_dataset, buf);
87 }
88 
89 uint64_t
90 dmu_objset_id(objset_t *os)
91 {
92 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
93 
94 	return (ds ? ds->ds_object : 0);
95 }
96 
97 static void
98 checksum_changed_cb(void *arg, uint64_t newval)
99 {
100 	objset_impl_t *osi = arg;
101 
102 	/*
103 	 * Inheritance should have been done by now.
104 	 */
105 	ASSERT(newval != ZIO_CHECKSUM_INHERIT);
106 
107 	osi->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
108 }
109 
110 static void
111 compression_changed_cb(void *arg, uint64_t newval)
112 {
113 	objset_impl_t *osi = arg;
114 
115 	/*
116 	 * Inheritance and range checking should have been done by now.
117 	 */
118 	ASSERT(newval != ZIO_COMPRESS_INHERIT);
119 
120 	osi->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
121 }
122 
123 static void
124 copies_changed_cb(void *arg, uint64_t newval)
125 {
126 	objset_impl_t *osi = arg;
127 
128 	/*
129 	 * Inheritance and range checking should have been done by now.
130 	 */
131 	ASSERT(newval > 0);
132 	ASSERT(newval <= spa_max_replication(osi->os_spa));
133 
134 	osi->os_copies = newval;
135 }
136 
137 void
138 dmu_objset_byteswap(void *buf, size_t size)
139 {
140 	objset_phys_t *osp = buf;
141 
142 	ASSERT(size == sizeof (objset_phys_t));
143 	dnode_byteswap(&osp->os_meta_dnode);
144 	byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
145 	osp->os_type = BSWAP_64(osp->os_type);
146 }
147 
148 int
149 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
150     objset_impl_t **osip)
151 {
152 	objset_impl_t *osi;
153 	int i, err, checksum;
154 
155 	ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
156 
157 	osi = kmem_zalloc(sizeof (objset_impl_t), KM_SLEEP);
158 	osi->os.os = osi;
159 	osi->os_dsl_dataset = ds;
160 	osi->os_spa = spa;
161 	osi->os_rootbp = bp;
162 	if (!BP_IS_HOLE(osi->os_rootbp)) {
163 		uint32_t aflags = ARC_WAIT;
164 		zbookmark_t zb;
165 		zb.zb_objset = ds ? ds->ds_object : 0;
166 		zb.zb_object = 0;
167 		zb.zb_level = -1;
168 		zb.zb_blkid = 0;
169 
170 		dprintf_bp(osi->os_rootbp, "reading %s", "");
171 		err = arc_read(NULL, spa, osi->os_rootbp,
172 		    dmu_ot[DMU_OT_OBJSET].ot_byteswap,
173 		    arc_getbuf_func, &osi->os_phys_buf,
174 		    ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
175 		if (err) {
176 			kmem_free(osi, sizeof (objset_impl_t));
177 			return (err);
178 		}
179 		osi->os_phys = osi->os_phys_buf->b_data;
180 		if (ds == NULL || dsl_dataset_is_snapshot(ds) == 0)
181 			arc_release(osi->os_phys_buf, &osi->os_phys_buf);
182 	} else {
183 		osi->os_phys_buf = arc_buf_alloc(spa, sizeof (objset_phys_t),
184 		    &osi->os_phys_buf, ARC_BUFC_METADATA);
185 		osi->os_phys = osi->os_phys_buf->b_data;
186 		bzero(osi->os_phys, sizeof (objset_phys_t));
187 	}
188 
189 	/*
190 	 * Note: the changed_cb will be called once before the register
191 	 * func returns, thus changing the checksum/compression from the
192 	 * default (fletcher2/off).  Snapshots don't need to know, and
193 	 * registering would complicate clone promotion.
194 	 */
195 	if (ds && ds->ds_phys->ds_num_children == 0) {
196 		err = dsl_prop_register(ds, "checksum",
197 		    checksum_changed_cb, osi);
198 		if (err == 0)
199 			err = dsl_prop_register(ds, "compression",
200 			    compression_changed_cb, osi);
201 		if (err == 0)
202 			err = dsl_prop_register(ds, "copies",
203 			    copies_changed_cb, osi);
204 		if (err) {
205 			VERIFY(arc_buf_remove_ref(osi->os_phys_buf,
206 			    &osi->os_phys_buf) == 1);
207 			kmem_free(osi, sizeof (objset_impl_t));
208 			return (err);
209 		}
210 	} else if (ds == NULL) {
211 		/* It's the meta-objset. */
212 		osi->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
213 		osi->os_compress = ZIO_COMPRESS_LZJB;
214 		osi->os_copies = spa_max_replication(spa);
215 	}
216 
217 	osi->os_zil = zil_alloc(&osi->os, &osi->os_phys->os_zil_header);
218 
219 	/*
220 	 * Metadata always gets compressed and checksummed.
221 	 * If the data checksum is multi-bit correctable, and it's not
222 	 * a ZBT-style checksum, then it's suitable for metadata as well.
223 	 * Otherwise, the metadata checksum defaults to fletcher4.
224 	 */
225 	checksum = osi->os_checksum;
226 
227 	if (zio_checksum_table[checksum].ci_correctable &&
228 	    !zio_checksum_table[checksum].ci_zbt)
229 		osi->os_md_checksum = checksum;
230 	else
231 		osi->os_md_checksum = ZIO_CHECKSUM_FLETCHER_4;
232 	osi->os_md_compress = ZIO_COMPRESS_LZJB;
233 
234 	for (i = 0; i < TXG_SIZE; i++) {
235 		list_create(&osi->os_dirty_dnodes[i], sizeof (dnode_t),
236 		    offsetof(dnode_t, dn_dirty_link[i]));
237 		list_create(&osi->os_free_dnodes[i], sizeof (dnode_t),
238 		    offsetof(dnode_t, dn_dirty_link[i]));
239 	}
240 	list_create(&osi->os_dnodes, sizeof (dnode_t),
241 	    offsetof(dnode_t, dn_link));
242 	list_create(&osi->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
243 	    offsetof(dmu_buf_impl_t, db_link));
244 
245 	mutex_init(&osi->os_lock, NULL, MUTEX_DEFAULT, NULL);
246 	mutex_init(&osi->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
247 
248 	osi->os_meta_dnode = dnode_special_open(osi,
249 	    &osi->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT);
250 
251 	/*
252 	 * We should be the only thread trying to do this because we
253 	 * have ds_opening_lock
254 	 */
255 	if (ds) {
256 		VERIFY(NULL == dsl_dataset_set_user_ptr(ds, osi,
257 		    dmu_objset_evict));
258 	}
259 
260 	*osip = osi;
261 	return (0);
262 }
263 
264 /* called from zpl */
265 int
266 dmu_objset_open(const char *name, dmu_objset_type_t type, int mode,
267     objset_t **osp)
268 {
269 	dsl_dataset_t *ds;
270 	int err;
271 	objset_t *os;
272 	objset_impl_t *osi;
273 
274 	os = kmem_alloc(sizeof (objset_t), KM_SLEEP);
275 	err = dsl_dataset_open(name, mode, os, &ds);
276 	if (err) {
277 		kmem_free(os, sizeof (objset_t));
278 		return (err);
279 	}
280 
281 	mutex_enter(&ds->ds_opening_lock);
282 	osi = dsl_dataset_get_user_ptr(ds);
283 	if (osi == NULL) {
284 		err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
285 		    ds, &ds->ds_phys->ds_bp, &osi);
286 		if (err) {
287 			dsl_dataset_close(ds, mode, os);
288 			kmem_free(os, sizeof (objset_t));
289 			return (err);
290 		}
291 	}
292 	mutex_exit(&ds->ds_opening_lock);
293 
294 	os->os = osi;
295 	os->os_mode = mode;
296 
297 	if (type != DMU_OST_ANY && type != os->os->os_phys->os_type) {
298 		dmu_objset_close(os);
299 		return (EINVAL);
300 	}
301 	*osp = os;
302 	return (0);
303 }
304 
305 void
306 dmu_objset_close(objset_t *os)
307 {
308 	dsl_dataset_close(os->os->os_dsl_dataset, os->os_mode, os);
309 	kmem_free(os, sizeof (objset_t));
310 }
311 
312 int
313 dmu_objset_evict_dbufs(objset_t *os)
314 {
315 	objset_impl_t *osi = os->os;
316 	dnode_t *dn;
317 
318 	mutex_enter(&osi->os_lock);
319 
320 	/* process the mdn last, since the other dnodes have holds on it */
321 	list_remove(&osi->os_dnodes, osi->os_meta_dnode);
322 	list_insert_tail(&osi->os_dnodes, osi->os_meta_dnode);
323 
324 	/*
325 	 * Find the first dnode with holds.  We have to do this dance
326 	 * because dnode_add_ref() only works if you already have a
327 	 * hold.  If there are no holds then it has no dbufs so OK to
328 	 * skip.
329 	 */
330 	for (dn = list_head(&osi->os_dnodes);
331 	    dn && !dnode_add_ref(dn, FTAG);
332 	    dn = list_next(&osi->os_dnodes, dn))
333 		continue;
334 
335 	while (dn) {
336 		dnode_t *next_dn = dn;
337 
338 		do {
339 			next_dn = list_next(&osi->os_dnodes, next_dn);
340 		} while (next_dn && !dnode_add_ref(next_dn, FTAG));
341 
342 		mutex_exit(&osi->os_lock);
343 		dnode_evict_dbufs(dn);
344 		dnode_rele(dn, FTAG);
345 		mutex_enter(&osi->os_lock);
346 		dn = next_dn;
347 	}
348 	mutex_exit(&osi->os_lock);
349 	return (list_head(&osi->os_dnodes) != osi->os_meta_dnode);
350 }
351 
352 void
353 dmu_objset_evict(dsl_dataset_t *ds, void *arg)
354 {
355 	objset_impl_t *osi = arg;
356 	objset_t os;
357 	int i;
358 
359 	for (i = 0; i < TXG_SIZE; i++) {
360 		ASSERT(list_head(&osi->os_dirty_dnodes[i]) == NULL);
361 		ASSERT(list_head(&osi->os_free_dnodes[i]) == NULL);
362 	}
363 
364 	if (ds && ds->ds_phys->ds_num_children == 0) {
365 		VERIFY(0 == dsl_prop_unregister(ds, "checksum",
366 		    checksum_changed_cb, osi));
367 		VERIFY(0 == dsl_prop_unregister(ds, "compression",
368 		    compression_changed_cb, osi));
369 		VERIFY(0 == dsl_prop_unregister(ds, "copies",
370 		    copies_changed_cb, osi));
371 	}
372 
373 	/*
374 	 * We should need only a single pass over the dnode list, since
375 	 * nothing can be added to the list at this point.
376 	 */
377 	os.os = osi;
378 	(void) dmu_objset_evict_dbufs(&os);
379 
380 	ASSERT3P(list_head(&osi->os_dnodes), ==, osi->os_meta_dnode);
381 	ASSERT3P(list_tail(&osi->os_dnodes), ==, osi->os_meta_dnode);
382 	ASSERT3P(list_head(&osi->os_meta_dnode->dn_dbufs), ==, NULL);
383 
384 	dnode_special_close(osi->os_meta_dnode);
385 	zil_free(osi->os_zil);
386 
387 	VERIFY(arc_buf_remove_ref(osi->os_phys_buf, &osi->os_phys_buf) == 1);
388 	mutex_destroy(&osi->os_lock);
389 	mutex_destroy(&osi->os_obj_lock);
390 	kmem_free(osi, sizeof (objset_impl_t));
391 }
392 
393 /* called from dsl for meta-objset */
394 objset_impl_t *
395 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
396     dmu_objset_type_t type, dmu_tx_t *tx)
397 {
398 	objset_impl_t *osi;
399 	dnode_t *mdn;
400 
401 	ASSERT(dmu_tx_is_syncing(tx));
402 	if (ds)
403 		mutex_enter(&ds->ds_opening_lock);
404 	VERIFY(0 == dmu_objset_open_impl(spa, ds, bp, &osi));
405 	if (ds)
406 		mutex_exit(&ds->ds_opening_lock);
407 	mdn = osi->os_meta_dnode;
408 
409 	dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
410 	    DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
411 
412 	/*
413 	 * We don't want to have to increase the meta-dnode's nlevels
414 	 * later, because then we could do it in quescing context while
415 	 * we are also accessing it in open context.
416 	 *
417 	 * This precaution is not necessary for the MOS (ds == NULL),
418 	 * because the MOS is only updated in syncing context.
419 	 * This is most fortunate: the MOS is the only objset that
420 	 * needs to be synced multiple times as spa_sync() iterates
421 	 * to convergence, so minimizing its dn_nlevels matters.
422 	 */
423 	if (ds != NULL) {
424 		int levels = 1;
425 
426 		/*
427 		 * Determine the number of levels necessary for the meta-dnode
428 		 * to contain DN_MAX_OBJECT dnodes.
429 		 */
430 		while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
431 		    (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
432 		    DN_MAX_OBJECT * sizeof (dnode_phys_t))
433 			levels++;
434 
435 		mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
436 		    mdn->dn_nlevels = levels;
437 	}
438 
439 	ASSERT(type != DMU_OST_NONE);
440 	ASSERT(type != DMU_OST_ANY);
441 	ASSERT(type < DMU_OST_NUMTYPES);
442 	osi->os_phys->os_type = type;
443 
444 	dsl_dataset_dirty(ds, tx);
445 
446 	return (osi);
447 }
448 
449 struct oscarg {
450 	void (*userfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
451 	void *userarg;
452 	dsl_dataset_t *clone_parent;
453 	const char *lastname;
454 	dmu_objset_type_t type;
455 };
456 
457 /*ARGSUSED*/
458 static int
459 dmu_objset_create_check(void *arg1, void *arg2, dmu_tx_t *tx)
460 {
461 	dsl_dir_t *dd = arg1;
462 	struct oscarg *oa = arg2;
463 	objset_t *mos = dd->dd_pool->dp_meta_objset;
464 	int err;
465 	uint64_t ddobj;
466 
467 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
468 	    oa->lastname, sizeof (uint64_t), 1, &ddobj);
469 	if (err != ENOENT)
470 		return (err ? err : EEXIST);
471 
472 	if (oa->clone_parent != NULL) {
473 		/*
474 		 * You can't clone across pools.
475 		 */
476 		if (oa->clone_parent->ds_dir->dd_pool != dd->dd_pool)
477 			return (EXDEV);
478 
479 		/*
480 		 * You can only clone snapshots, not the head datasets.
481 		 */
482 		if (oa->clone_parent->ds_phys->ds_num_children == 0)
483 			return (EINVAL);
484 	}
485 
486 	return (0);
487 }
488 
489 static void
490 dmu_objset_create_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
491 {
492 	dsl_dir_t *dd = arg1;
493 	struct oscarg *oa = arg2;
494 	dsl_dataset_t *ds;
495 	blkptr_t *bp;
496 	uint64_t dsobj;
497 
498 	ASSERT(dmu_tx_is_syncing(tx));
499 
500 	dsobj = dsl_dataset_create_sync(dd, oa->lastname,
501 	    oa->clone_parent, tx);
502 
503 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, dsobj, NULL,
504 	    DS_MODE_STANDARD | DS_MODE_READONLY, FTAG, &ds));
505 	bp = dsl_dataset_get_blkptr(ds);
506 	if (BP_IS_HOLE(bp)) {
507 		objset_impl_t *osi;
508 
509 		/* This is an empty dmu_objset; not a clone. */
510 		osi = dmu_objset_create_impl(dsl_dataset_get_spa(ds),
511 		    ds, bp, oa->type, tx);
512 
513 		if (oa->userfunc)
514 			oa->userfunc(&osi->os, oa->userarg, cr, tx);
515 	}
516 
517 	/*
518 	 * Create create time permission if any?
519 	 */
520 	dsl_deleg_set_create_perms(ds->ds_dir, tx, cr);
521 
522 	spa_history_internal_log(LOG_DS_CREATE, dd->dd_pool->dp_spa,
523 	    tx, cr, "dataset = %llu", dsobj);
524 
525 	dsl_dataset_close(ds, DS_MODE_STANDARD | DS_MODE_READONLY, FTAG);
526 }
527 
528 int
529 dmu_objset_create(const char *name, dmu_objset_type_t type,
530     objset_t *clone_parent,
531     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
532 {
533 	dsl_dir_t *pdd;
534 	const char *tail;
535 	int err = 0;
536 	struct oscarg oa = { 0 };
537 
538 	ASSERT(strchr(name, '@') == NULL);
539 	err = dsl_dir_open(name, FTAG, &pdd, &tail);
540 	if (err)
541 		return (err);
542 	if (tail == NULL) {
543 		dsl_dir_close(pdd, FTAG);
544 		return (EEXIST);
545 	}
546 
547 	dprintf("name=%s\n", name);
548 
549 	oa.userfunc = func;
550 	oa.userarg = arg;
551 	oa.lastname = tail;
552 	oa.type = type;
553 
554 	if (clone_parent != NULL) {
555 		/*
556 		 * You can't clone to a different type.
557 		 */
558 		if (clone_parent->os->os_phys->os_type != type) {
559 			dsl_dir_close(pdd, FTAG);
560 			return (EINVAL);
561 		}
562 		oa.clone_parent = clone_parent->os->os_dsl_dataset;
563 	}
564 	err = dsl_sync_task_do(pdd->dd_pool, dmu_objset_create_check,
565 	    dmu_objset_create_sync, pdd, &oa, 5);
566 	dsl_dir_close(pdd, FTAG);
567 	return (err);
568 }
569 
570 int
571 dmu_objset_destroy(const char *name)
572 {
573 	objset_t *os;
574 	int error;
575 
576 	/*
577 	 * If it looks like we'll be able to destroy it, and there's
578 	 * an unplayed replay log sitting around, destroy the log.
579 	 * It would be nicer to do this in dsl_dataset_destroy_sync(),
580 	 * but the replay log objset is modified in open context.
581 	 */
582 	error = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_EXCLUSIVE, &os);
583 	if (error == 0) {
584 		zil_destroy(dmu_objset_zil(os), B_FALSE);
585 		dmu_objset_close(os);
586 	}
587 
588 	return (dsl_dataset_destroy(name));
589 }
590 
591 int
592 dmu_objset_rollback(const char *name)
593 {
594 	int err;
595 	objset_t *os;
596 
597 	err = dmu_objset_open(name, DMU_OST_ANY,
598 	    DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT, &os);
599 	if (err)
600 		return (err);
601 
602 	/* XXX uncache everything? */
603 	err = dsl_dataset_rollback(os->os->os_dsl_dataset);
604 
605 	dmu_objset_close(os);
606 	return (err);
607 }
608 
609 struct snaparg {
610 	dsl_sync_task_group_t *dstg;
611 	char *snapname;
612 	char failed[MAXPATHLEN];
613 	boolean_t checkperms;
614 };
615 
616 static int
617 dmu_objset_snapshot_one(char *name, void *arg)
618 {
619 	struct snaparg *sn = arg;
620 	objset_t *os;
621 	dmu_objset_stats_t stat;
622 	int err;
623 
624 	(void) strcpy(sn->failed, name);
625 
626 	/*
627 	 * Check permissions only when requested.  This only applies when
628 	 * doing a recursive snapshot.  The permission checks for the starting
629 	 * dataset have already been performed in zfs_secpolicy_snapshot()
630 	 */
631 	if (sn->checkperms == B_TRUE &&
632 	    (err = zfs_secpolicy_snapshot_perms(name, CRED())))
633 		return (err);
634 
635 	err = dmu_objset_open(name, DMU_OST_ANY, DS_MODE_STANDARD, &os);
636 	if (err != 0)
637 		return (err);
638 
639 	/*
640 	 * If the objset is in an inconsistent state, return busy.
641 	 */
642 	dmu_objset_fast_stat(os, &stat);
643 	if (stat.dds_inconsistent) {
644 		dmu_objset_close(os);
645 		return (EBUSY);
646 	}
647 
648 	/*
649 	 * NB: we need to wait for all in-flight changes to get to disk,
650 	 * so that we snapshot those changes.  zil_suspend does this as
651 	 * a side effect.
652 	 */
653 	err = zil_suspend(dmu_objset_zil(os));
654 	if (err == 0) {
655 		dsl_sync_task_create(sn->dstg, dsl_dataset_snapshot_check,
656 		    dsl_dataset_snapshot_sync, os, sn->snapname, 3);
657 	} else {
658 		dmu_objset_close(os);
659 	}
660 
661 	return (err);
662 }
663 
664 int
665 dmu_objset_snapshot(char *fsname, char *snapname, boolean_t recursive)
666 {
667 	dsl_sync_task_t *dst;
668 	struct snaparg sn = { 0 };
669 	spa_t *spa;
670 	int err;
671 
672 	(void) strcpy(sn.failed, fsname);
673 
674 	err = spa_open(fsname, &spa, FTAG);
675 	if (err)
676 		return (err);
677 
678 	sn.dstg = dsl_sync_task_group_create(spa_get_dsl(spa));
679 	sn.snapname = snapname;
680 
681 	if (recursive) {
682 		sn.checkperms = B_TRUE;
683 		err = dmu_objset_find(fsname,
684 		    dmu_objset_snapshot_one, &sn, DS_FIND_CHILDREN);
685 	} else {
686 		sn.checkperms = B_FALSE;
687 		err = dmu_objset_snapshot_one(fsname, &sn);
688 	}
689 
690 	if (err)
691 		goto out;
692 
693 	err = dsl_sync_task_group_wait(sn.dstg);
694 
695 	for (dst = list_head(&sn.dstg->dstg_tasks); dst;
696 	    dst = list_next(&sn.dstg->dstg_tasks, dst)) {
697 		objset_t *os = dst->dst_arg1;
698 		if (dst->dst_err)
699 			dmu_objset_name(os, sn.failed);
700 		zil_resume(dmu_objset_zil(os));
701 		dmu_objset_close(os);
702 	}
703 out:
704 	if (err)
705 		(void) strcpy(fsname, sn.failed);
706 	dsl_sync_task_group_destroy(sn.dstg);
707 	spa_close(spa, FTAG);
708 	return (err);
709 }
710 
711 static void
712 dmu_objset_sync_dnodes(list_t *list, dmu_tx_t *tx)
713 {
714 	dnode_t *dn;
715 
716 	while (dn = list_head(list)) {
717 		ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
718 		ASSERT(dn->dn_dbuf->db_data_pending);
719 		/*
720 		 * Initialize dn_zio outside dnode_sync()
721 		 * to accomodate meta-dnode
722 		 */
723 		dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
724 		ASSERT(dn->dn_zio);
725 
726 		ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
727 		list_remove(list, dn);
728 		dnode_sync(dn, tx);
729 	}
730 }
731 
732 /* ARGSUSED */
733 static void
734 ready(zio_t *zio, arc_buf_t *abuf, void *arg)
735 {
736 	objset_impl_t *os = arg;
737 	blkptr_t *bp = os->os_rootbp;
738 	dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
739 	int i;
740 
741 	/*
742 	 * Update rootbp fill count.
743 	 */
744 	bp->blk_fill = 1;	/* count the meta-dnode */
745 	for (i = 0; i < dnp->dn_nblkptr; i++)
746 		bp->blk_fill += dnp->dn_blkptr[i].blk_fill;
747 }
748 
749 /* ARGSUSED */
750 static void
751 killer(zio_t *zio, arc_buf_t *abuf, void *arg)
752 {
753 	objset_impl_t *os = arg;
754 
755 	ASSERT3U(zio->io_error, ==, 0);
756 
757 	BP_SET_TYPE(zio->io_bp, DMU_OT_OBJSET);
758 	BP_SET_LEVEL(zio->io_bp, 0);
759 
760 	if (!DVA_EQUAL(BP_IDENTITY(zio->io_bp),
761 	    BP_IDENTITY(&zio->io_bp_orig))) {
762 		if (zio->io_bp_orig.blk_birth == os->os_synctx->tx_txg)
763 			dsl_dataset_block_kill(os->os_dsl_dataset,
764 			    &zio->io_bp_orig, NULL, os->os_synctx);
765 		dsl_dataset_block_born(os->os_dsl_dataset, zio->io_bp,
766 		    os->os_synctx);
767 	}
768 	arc_release(os->os_phys_buf, &os->os_phys_buf);
769 }
770 
771 /* called from dsl */
772 void
773 dmu_objset_sync(objset_impl_t *os, zio_t *pio, dmu_tx_t *tx)
774 {
775 	int txgoff;
776 	zbookmark_t zb;
777 	zio_t *zio;
778 	list_t *list;
779 	dbuf_dirty_record_t *dr;
780 
781 	dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
782 
783 	ASSERT(dmu_tx_is_syncing(tx));
784 	/* XXX the write_done callback should really give us the tx... */
785 	os->os_synctx = tx;
786 
787 	if (os->os_dsl_dataset == NULL) {
788 		/*
789 		 * This is the MOS.  If we have upgraded,
790 		 * spa_max_replication() could change, so reset
791 		 * os_copies here.
792 		 */
793 		os->os_copies = spa_max_replication(os->os_spa);
794 	}
795 
796 	/*
797 	 * Create the root block IO
798 	 */
799 	zb.zb_objset = os->os_dsl_dataset ? os->os_dsl_dataset->ds_object : 0;
800 	zb.zb_object = 0;
801 	zb.zb_level = -1;
802 	zb.zb_blkid = 0;
803 	if (BP_IS_OLDER(os->os_rootbp, tx->tx_txg)) {
804 		dsl_dataset_block_kill(os->os_dsl_dataset,
805 		    os->os_rootbp, pio, tx);
806 	}
807 	zio = arc_write(pio, os->os_spa, os->os_md_checksum,
808 	    os->os_md_compress,
809 	    dmu_get_replication_level(os, &zb, DMU_OT_OBJSET),
810 	    tx->tx_txg, os->os_rootbp, os->os_phys_buf, ready, killer, os,
811 	    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED | ZIO_FLAG_METADATA,
812 	    &zb);
813 
814 	/*
815 	 * Sync meta-dnode - the parent IO for the sync is the root block
816 	 */
817 	os->os_meta_dnode->dn_zio = zio;
818 	dnode_sync(os->os_meta_dnode, tx);
819 
820 	txgoff = tx->tx_txg & TXG_MASK;
821 
822 	dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], tx);
823 	dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], tx);
824 
825 	list = &os->os_meta_dnode->dn_dirty_records[txgoff];
826 	while (dr = list_head(list)) {
827 		ASSERT(dr->dr_dbuf->db_level == 0);
828 		list_remove(list, dr);
829 		if (dr->dr_zio)
830 			zio_nowait(dr->dr_zio);
831 	}
832 	/*
833 	 * Free intent log blocks up to this tx.
834 	 */
835 	zil_sync(os->os_zil, tx);
836 	zio_nowait(zio);
837 }
838 
839 void
840 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
841     uint64_t *usedobjsp, uint64_t *availobjsp)
842 {
843 	dsl_dataset_space(os->os->os_dsl_dataset, refdbytesp, availbytesp,
844 	    usedobjsp, availobjsp);
845 }
846 
847 uint64_t
848 dmu_objset_fsid_guid(objset_t *os)
849 {
850 	return (dsl_dataset_fsid_guid(os->os->os_dsl_dataset));
851 }
852 
853 void
854 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
855 {
856 	stat->dds_type = os->os->os_phys->os_type;
857 	if (os->os->os_dsl_dataset)
858 		dsl_dataset_fast_stat(os->os->os_dsl_dataset, stat);
859 }
860 
861 void
862 dmu_objset_stats(objset_t *os, nvlist_t *nv)
863 {
864 	ASSERT(os->os->os_dsl_dataset ||
865 	    os->os->os_phys->os_type == DMU_OST_META);
866 
867 	if (os->os->os_dsl_dataset != NULL)
868 		dsl_dataset_stats(os->os->os_dsl_dataset, nv);
869 
870 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
871 	    os->os->os_phys->os_type);
872 }
873 
874 int
875 dmu_objset_is_snapshot(objset_t *os)
876 {
877 	if (os->os->os_dsl_dataset != NULL)
878 		return (dsl_dataset_is_snapshot(os->os->os_dsl_dataset));
879 	else
880 		return (B_FALSE);
881 }
882 
883 int
884 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
885     uint64_t *idp, uint64_t *offp)
886 {
887 	dsl_dataset_t *ds = os->os->os_dsl_dataset;
888 	zap_cursor_t cursor;
889 	zap_attribute_t attr;
890 
891 	if (ds->ds_phys->ds_snapnames_zapobj == 0)
892 		return (ENOENT);
893 
894 	zap_cursor_init_serialized(&cursor,
895 	    ds->ds_dir->dd_pool->dp_meta_objset,
896 	    ds->ds_phys->ds_snapnames_zapobj, *offp);
897 
898 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
899 		zap_cursor_fini(&cursor);
900 		return (ENOENT);
901 	}
902 
903 	if (strlen(attr.za_name) + 1 > namelen) {
904 		zap_cursor_fini(&cursor);
905 		return (ENAMETOOLONG);
906 	}
907 
908 	(void) strcpy(name, attr.za_name);
909 	if (idp)
910 		*idp = attr.za_first_integer;
911 	zap_cursor_advance(&cursor);
912 	*offp = zap_cursor_serialize(&cursor);
913 	zap_cursor_fini(&cursor);
914 
915 	return (0);
916 }
917 
918 int
919 dmu_dir_list_next(objset_t *os, int namelen, char *name,
920     uint64_t *idp, uint64_t *offp)
921 {
922 	dsl_dir_t *dd = os->os->os_dsl_dataset->ds_dir;
923 	zap_cursor_t cursor;
924 	zap_attribute_t attr;
925 
926 	/* there is no next dir on a snapshot! */
927 	if (os->os->os_dsl_dataset->ds_object !=
928 	    dd->dd_phys->dd_head_dataset_obj)
929 		return (ENOENT);
930 
931 	zap_cursor_init_serialized(&cursor,
932 	    dd->dd_pool->dp_meta_objset,
933 	    dd->dd_phys->dd_child_dir_zapobj, *offp);
934 
935 	if (zap_cursor_retrieve(&cursor, &attr) != 0) {
936 		zap_cursor_fini(&cursor);
937 		return (ENOENT);
938 	}
939 
940 	if (strlen(attr.za_name) + 1 > namelen) {
941 		zap_cursor_fini(&cursor);
942 		return (ENAMETOOLONG);
943 	}
944 
945 	(void) strcpy(name, attr.za_name);
946 	if (idp)
947 		*idp = attr.za_first_integer;
948 	zap_cursor_advance(&cursor);
949 	*offp = zap_cursor_serialize(&cursor);
950 	zap_cursor_fini(&cursor);
951 
952 	return (0);
953 }
954 
955 /*
956  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
957  */
958 int
959 dmu_objset_find(char *name, int func(char *, void *), void *arg, int flags)
960 {
961 	dsl_dir_t *dd;
962 	objset_t *os;
963 	uint64_t snapobj;
964 	zap_cursor_t zc;
965 	zap_attribute_t *attr;
966 	char *child;
967 	int do_self, err;
968 
969 	err = dsl_dir_open(name, FTAG, &dd, NULL);
970 	if (err)
971 		return (err);
972 
973 	/* NB: the $MOS dir doesn't have a head dataset */
974 	do_self = (dd->dd_phys->dd_head_dataset_obj != 0);
975 	attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
976 
977 	/*
978 	 * Iterate over all children.
979 	 */
980 	if (flags & DS_FIND_CHILDREN) {
981 		for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset,
982 		    dd->dd_phys->dd_child_dir_zapobj);
983 		    zap_cursor_retrieve(&zc, attr) == 0;
984 		    (void) zap_cursor_advance(&zc)) {
985 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
986 			ASSERT(attr->za_num_integers == 1);
987 
988 			/*
989 			 * No separating '/' because parent's name ends in /.
990 			 */
991 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
992 			/* XXX could probably just use name here */
993 			dsl_dir_name(dd, child);
994 			(void) strcat(child, "/");
995 			(void) strcat(child, attr->za_name);
996 			err = dmu_objset_find(child, func, arg, flags);
997 			kmem_free(child, MAXPATHLEN);
998 			if (err)
999 				break;
1000 		}
1001 		zap_cursor_fini(&zc);
1002 
1003 		if (err) {
1004 			dsl_dir_close(dd, FTAG);
1005 			kmem_free(attr, sizeof (zap_attribute_t));
1006 			return (err);
1007 		}
1008 	}
1009 
1010 	/*
1011 	 * Iterate over all snapshots.
1012 	 */
1013 	if ((flags & DS_FIND_SNAPSHOTS) &&
1014 	    dmu_objset_open(name, DMU_OST_ANY,
1015 	    DS_MODE_STANDARD | DS_MODE_READONLY, &os) == 0) {
1016 
1017 		snapobj = os->os->os_dsl_dataset->ds_phys->ds_snapnames_zapobj;
1018 		dmu_objset_close(os);
1019 
1020 		for (zap_cursor_init(&zc, dd->dd_pool->dp_meta_objset, snapobj);
1021 		    zap_cursor_retrieve(&zc, attr) == 0;
1022 		    (void) zap_cursor_advance(&zc)) {
1023 			ASSERT(attr->za_integer_length == sizeof (uint64_t));
1024 			ASSERT(attr->za_num_integers == 1);
1025 
1026 			child = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1027 			/* XXX could probably just use name here */
1028 			dsl_dir_name(dd, child);
1029 			(void) strcat(child, "@");
1030 			(void) strcat(child, attr->za_name);
1031 			err = func(child, arg);
1032 			kmem_free(child, MAXPATHLEN);
1033 			if (err)
1034 				break;
1035 		}
1036 		zap_cursor_fini(&zc);
1037 	}
1038 
1039 	dsl_dir_close(dd, FTAG);
1040 	kmem_free(attr, sizeof (zap_attribute_t));
1041 
1042 	if (err)
1043 		return (err);
1044 
1045 	/*
1046 	 * Apply to self if appropriate.
1047 	 */
1048 	if (do_self)
1049 		err = func(name, arg);
1050 	return (err);
1051 }
1052