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