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