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