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