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