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