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