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