]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/dmu_objset.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / cddl / contrib / opensolaris / uts / common / fs / 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  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
25  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27  */
28
29 /* Portions Copyright 2010 Robert Milkowski */
30
31 #include <sys/cred.h>
32 #include <sys/zfs_context.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dsl_dir.h>
35 #include <sys/dsl_dataset.h>
36 #include <sys/dsl_prop.h>
37 #include <sys/dsl_pool.h>
38 #include <sys/dsl_synctask.h>
39 #include <sys/dsl_deleg.h>
40 #include <sys/dnode.h>
41 #include <sys/dbuf.h>
42 #include <sys/zvol.h>
43 #include <sys/dmu_tx.h>
44 #include <sys/zap.h>
45 #include <sys/zil.h>
46 #include <sys/dmu_impl.h>
47 #include <sys/zfs_ioctl.h>
48 #include <sys/sa.h>
49 #include <sys/zfs_onexit.h>
50 #include <sys/dsl_destroy.h>
51
52 /*
53  * Needed to close a window in dnode_move() that allows the objset to be freed
54  * before it can be safely accessed.
55  */
56 krwlock_t os_lock;
57
58 void
59 dmu_objset_init(void)
60 {
61         rw_init(&os_lock, NULL, RW_DEFAULT, NULL);
62 }
63
64 void
65 dmu_objset_fini(void)
66 {
67         rw_destroy(&os_lock);
68 }
69
70 spa_t *
71 dmu_objset_spa(objset_t *os)
72 {
73         return (os->os_spa);
74 }
75
76 zilog_t *
77 dmu_objset_zil(objset_t *os)
78 {
79         return (os->os_zil);
80 }
81
82 dsl_pool_t *
83 dmu_objset_pool(objset_t *os)
84 {
85         dsl_dataset_t *ds;
86
87         if ((ds = os->os_dsl_dataset) != NULL && ds->ds_dir)
88                 return (ds->ds_dir->dd_pool);
89         else
90                 return (spa_get_dsl(os->os_spa));
91 }
92
93 dsl_dataset_t *
94 dmu_objset_ds(objset_t *os)
95 {
96         return (os->os_dsl_dataset);
97 }
98
99 dmu_objset_type_t
100 dmu_objset_type(objset_t *os)
101 {
102         return (os->os_phys->os_type);
103 }
104
105 void
106 dmu_objset_name(objset_t *os, char *buf)
107 {
108         dsl_dataset_name(os->os_dsl_dataset, buf);
109 }
110
111 uint64_t
112 dmu_objset_id(objset_t *os)
113 {
114         dsl_dataset_t *ds = os->os_dsl_dataset;
115
116         return (ds ? ds->ds_object : 0);
117 }
118
119 zfs_sync_type_t
120 dmu_objset_syncprop(objset_t *os)
121 {
122         return (os->os_sync);
123 }
124
125 zfs_logbias_op_t
126 dmu_objset_logbias(objset_t *os)
127 {
128         return (os->os_logbias);
129 }
130
131 static void
132 checksum_changed_cb(void *arg, uint64_t newval)
133 {
134         objset_t *os = arg;
135
136         /*
137          * Inheritance should have been done by now.
138          */
139         ASSERT(newval != ZIO_CHECKSUM_INHERIT);
140
141         os->os_checksum = zio_checksum_select(newval, ZIO_CHECKSUM_ON_VALUE);
142 }
143
144 static void
145 compression_changed_cb(void *arg, uint64_t newval)
146 {
147         objset_t *os = arg;
148
149         /*
150          * Inheritance and range checking should have been done by now.
151          */
152         ASSERT(newval != ZIO_COMPRESS_INHERIT);
153
154         os->os_compress = zio_compress_select(newval, ZIO_COMPRESS_ON_VALUE);
155 }
156
157 static void
158 copies_changed_cb(void *arg, uint64_t newval)
159 {
160         objset_t *os = arg;
161
162         /*
163          * Inheritance and range checking should have been done by now.
164          */
165         ASSERT(newval > 0);
166         ASSERT(newval <= spa_max_replication(os->os_spa));
167
168         os->os_copies = newval;
169 }
170
171 static void
172 dedup_changed_cb(void *arg, uint64_t newval)
173 {
174         objset_t *os = arg;
175         spa_t *spa = os->os_spa;
176         enum zio_checksum checksum;
177
178         /*
179          * Inheritance should have been done by now.
180          */
181         ASSERT(newval != ZIO_CHECKSUM_INHERIT);
182
183         checksum = zio_checksum_dedup_select(spa, newval, ZIO_CHECKSUM_OFF);
184
185         os->os_dedup_checksum = checksum & ZIO_CHECKSUM_MASK;
186         os->os_dedup_verify = !!(checksum & ZIO_CHECKSUM_VERIFY);
187 }
188
189 static void
190 primary_cache_changed_cb(void *arg, uint64_t newval)
191 {
192         objset_t *os = arg;
193
194         /*
195          * Inheritance and range checking should have been done by now.
196          */
197         ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
198             newval == ZFS_CACHE_METADATA);
199
200         os->os_primary_cache = newval;
201 }
202
203 static void
204 secondary_cache_changed_cb(void *arg, uint64_t newval)
205 {
206         objset_t *os = arg;
207
208         /*
209          * Inheritance and range checking should have been done by now.
210          */
211         ASSERT(newval == ZFS_CACHE_ALL || newval == ZFS_CACHE_NONE ||
212             newval == ZFS_CACHE_METADATA);
213
214         os->os_secondary_cache = newval;
215 }
216
217 static void
218 sync_changed_cb(void *arg, uint64_t newval)
219 {
220         objset_t *os = arg;
221
222         /*
223          * Inheritance and range checking should have been done by now.
224          */
225         ASSERT(newval == ZFS_SYNC_STANDARD || newval == ZFS_SYNC_ALWAYS ||
226             newval == ZFS_SYNC_DISABLED);
227
228         os->os_sync = newval;
229         if (os->os_zil)
230                 zil_set_sync(os->os_zil, newval);
231 }
232
233 static void
234 redundant_metadata_changed_cb(void *arg, uint64_t newval)
235 {
236         objset_t *os = arg;
237
238         /*
239          * Inheritance and range checking should have been done by now.
240          */
241         ASSERT(newval == ZFS_REDUNDANT_METADATA_ALL ||
242             newval == ZFS_REDUNDANT_METADATA_MOST);
243
244         os->os_redundant_metadata = newval;
245 }
246
247 static void
248 logbias_changed_cb(void *arg, uint64_t newval)
249 {
250         objset_t *os = arg;
251
252         ASSERT(newval == ZFS_LOGBIAS_LATENCY ||
253             newval == ZFS_LOGBIAS_THROUGHPUT);
254         os->os_logbias = newval;
255         if (os->os_zil)
256                 zil_set_logbias(os->os_zil, newval);
257 }
258
259 static void
260 recordsize_changed_cb(void *arg, uint64_t newval)
261 {
262         objset_t *os = arg;
263
264         os->os_recordsize = newval;
265 }
266
267 void
268 dmu_objset_byteswap(void *buf, size_t size)
269 {
270         objset_phys_t *osp = buf;
271
272         ASSERT(size == OBJSET_OLD_PHYS_SIZE || size == sizeof (objset_phys_t));
273         dnode_byteswap(&osp->os_meta_dnode);
274         byteswap_uint64_array(&osp->os_zil_header, sizeof (zil_header_t));
275         osp->os_type = BSWAP_64(osp->os_type);
276         osp->os_flags = BSWAP_64(osp->os_flags);
277         if (size == sizeof (objset_phys_t)) {
278                 dnode_byteswap(&osp->os_userused_dnode);
279                 dnode_byteswap(&osp->os_groupused_dnode);
280         }
281 }
282
283 int
284 dmu_objset_open_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
285     objset_t **osp)
286 {
287         objset_t *os;
288         int i, err;
289
290         ASSERT(ds == NULL || MUTEX_HELD(&ds->ds_opening_lock));
291
292         os = kmem_zalloc(sizeof (objset_t), KM_SLEEP);
293         os->os_dsl_dataset = ds;
294         os->os_spa = spa;
295         os->os_rootbp = bp;
296         if (!BP_IS_HOLE(os->os_rootbp)) {
297                 arc_flags_t aflags = ARC_FLAG_WAIT;
298                 zbookmark_phys_t zb;
299                 SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
300                     ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
301
302                 if (DMU_OS_IS_L2CACHEABLE(os))
303                         aflags |= ARC_FLAG_L2CACHE;
304                 if (DMU_OS_IS_L2COMPRESSIBLE(os))
305                         aflags |= ARC_FLAG_L2COMPRESS;
306
307                 dprintf_bp(os->os_rootbp, "reading %s", "");
308                 err = arc_read(NULL, spa, os->os_rootbp,
309                     arc_getbuf_func, &os->os_phys_buf,
310                     ZIO_PRIORITY_SYNC_READ, ZIO_FLAG_CANFAIL, &aflags, &zb);
311                 if (err != 0) {
312                         kmem_free(os, sizeof (objset_t));
313                         /* convert checksum errors into IO errors */
314                         if (err == ECKSUM)
315                                 err = SET_ERROR(EIO);
316                         return (err);
317                 }
318
319                 /* Increase the blocksize if we are permitted. */
320                 if (spa_version(spa) >= SPA_VERSION_USERSPACE &&
321                     arc_buf_size(os->os_phys_buf) < sizeof (objset_phys_t)) {
322                         arc_buf_t *buf = arc_buf_alloc(spa,
323                             sizeof (objset_phys_t), &os->os_phys_buf,
324                             ARC_BUFC_METADATA);
325                         bzero(buf->b_data, sizeof (objset_phys_t));
326                         bcopy(os->os_phys_buf->b_data, buf->b_data,
327                             arc_buf_size(os->os_phys_buf));
328                         (void) arc_buf_remove_ref(os->os_phys_buf,
329                             &os->os_phys_buf);
330                         os->os_phys_buf = buf;
331                 }
332
333                 os->os_phys = os->os_phys_buf->b_data;
334                 os->os_flags = os->os_phys->os_flags;
335         } else {
336                 int size = spa_version(spa) >= SPA_VERSION_USERSPACE ?
337                     sizeof (objset_phys_t) : OBJSET_OLD_PHYS_SIZE;
338                 os->os_phys_buf = arc_buf_alloc(spa, size,
339                     &os->os_phys_buf, ARC_BUFC_METADATA);
340                 os->os_phys = os->os_phys_buf->b_data;
341                 bzero(os->os_phys, size);
342         }
343
344         /*
345          * Note: the changed_cb will be called once before the register
346          * func returns, thus changing the checksum/compression from the
347          * default (fletcher2/off).  Snapshots don't need to know about
348          * checksum/compression/copies.
349          */
350         if (ds != NULL) {
351                 err = dsl_prop_register(ds,
352                     zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
353                     primary_cache_changed_cb, os);
354                 if (err == 0) {
355                         err = dsl_prop_register(ds,
356                             zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
357                             secondary_cache_changed_cb, os);
358                 }
359                 if (!dsl_dataset_is_snapshot(ds)) {
360                         if (err == 0) {
361                                 err = dsl_prop_register(ds,
362                                     zfs_prop_to_name(ZFS_PROP_CHECKSUM),
363                                     checksum_changed_cb, os);
364                         }
365                         if (err == 0) {
366                                 err = dsl_prop_register(ds,
367                                     zfs_prop_to_name(ZFS_PROP_COMPRESSION),
368                                     compression_changed_cb, os);
369                         }
370                         if (err == 0) {
371                                 err = dsl_prop_register(ds,
372                                     zfs_prop_to_name(ZFS_PROP_COPIES),
373                                     copies_changed_cb, os);
374                         }
375                         if (err == 0) {
376                                 err = dsl_prop_register(ds,
377                                     zfs_prop_to_name(ZFS_PROP_DEDUP),
378                                     dedup_changed_cb, os);
379                         }
380                         if (err == 0) {
381                                 err = dsl_prop_register(ds,
382                                     zfs_prop_to_name(ZFS_PROP_LOGBIAS),
383                                     logbias_changed_cb, os);
384                         }
385                         if (err == 0) {
386                                 err = dsl_prop_register(ds,
387                                     zfs_prop_to_name(ZFS_PROP_SYNC),
388                                     sync_changed_cb, os);
389                         }
390                         if (err == 0) {
391                                 err = dsl_prop_register(ds,
392                                     zfs_prop_to_name(
393                                     ZFS_PROP_REDUNDANT_METADATA),
394                                     redundant_metadata_changed_cb, os);
395                         }
396                         if (err == 0) {
397                                 err = dsl_prop_register(ds,
398                                     zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
399                                     recordsize_changed_cb, os);
400                         }
401                 }
402                 if (err != 0) {
403                         VERIFY(arc_buf_remove_ref(os->os_phys_buf,
404                             &os->os_phys_buf));
405                         kmem_free(os, sizeof (objset_t));
406                         return (err);
407                 }
408         } else {
409                 /* It's the meta-objset. */
410                 os->os_checksum = ZIO_CHECKSUM_FLETCHER_4;
411                 os->os_compress = ZIO_COMPRESS_LZJB;
412                 os->os_copies = spa_max_replication(spa);
413                 os->os_dedup_checksum = ZIO_CHECKSUM_OFF;
414                 os->os_dedup_verify = B_FALSE;
415                 os->os_logbias = ZFS_LOGBIAS_LATENCY;
416                 os->os_sync = ZFS_SYNC_STANDARD;
417                 os->os_primary_cache = ZFS_CACHE_ALL;
418                 os->os_secondary_cache = ZFS_CACHE_ALL;
419         }
420
421         if (ds == NULL || !dsl_dataset_is_snapshot(ds))
422                 os->os_zil_header = os->os_phys->os_zil_header;
423         os->os_zil = zil_alloc(os, &os->os_zil_header);
424
425         for (i = 0; i < TXG_SIZE; i++) {
426                 list_create(&os->os_dirty_dnodes[i], sizeof (dnode_t),
427                     offsetof(dnode_t, dn_dirty_link[i]));
428                 list_create(&os->os_free_dnodes[i], sizeof (dnode_t),
429                     offsetof(dnode_t, dn_dirty_link[i]));
430         }
431         list_create(&os->os_dnodes, sizeof (dnode_t),
432             offsetof(dnode_t, dn_link));
433         list_create(&os->os_downgraded_dbufs, sizeof (dmu_buf_impl_t),
434             offsetof(dmu_buf_impl_t, db_link));
435
436         mutex_init(&os->os_lock, NULL, MUTEX_DEFAULT, NULL);
437         mutex_init(&os->os_obj_lock, NULL, MUTEX_DEFAULT, NULL);
438         mutex_init(&os->os_user_ptr_lock, NULL, MUTEX_DEFAULT, NULL);
439
440         DMU_META_DNODE(os) = dnode_special_open(os,
441             &os->os_phys->os_meta_dnode, DMU_META_DNODE_OBJECT,
442             &os->os_meta_dnode);
443         if (arc_buf_size(os->os_phys_buf) >= sizeof (objset_phys_t)) {
444                 DMU_USERUSED_DNODE(os) = dnode_special_open(os,
445                     &os->os_phys->os_userused_dnode, DMU_USERUSED_OBJECT,
446                     &os->os_userused_dnode);
447                 DMU_GROUPUSED_DNODE(os) = dnode_special_open(os,
448                     &os->os_phys->os_groupused_dnode, DMU_GROUPUSED_OBJECT,
449                     &os->os_groupused_dnode);
450         }
451
452         *osp = os;
453         return (0);
454 }
455
456 int
457 dmu_objset_from_ds(dsl_dataset_t *ds, objset_t **osp)
458 {
459         int err = 0;
460
461         mutex_enter(&ds->ds_opening_lock);
462         if (ds->ds_objset == NULL) {
463                 objset_t *os;
464                 err = dmu_objset_open_impl(dsl_dataset_get_spa(ds),
465                     ds, dsl_dataset_get_blkptr(ds), &os);
466
467                 if (err == 0) {
468                         mutex_enter(&ds->ds_lock);
469                         ASSERT(ds->ds_objset == NULL);
470                         ds->ds_objset = os;
471                         mutex_exit(&ds->ds_lock);
472                 }
473         }
474         *osp = ds->ds_objset;
475         mutex_exit(&ds->ds_opening_lock);
476         return (err);
477 }
478
479 /*
480  * Holds the pool while the objset is held.  Therefore only one objset
481  * can be held at a time.
482  */
483 int
484 dmu_objset_hold(const char *name, void *tag, objset_t **osp)
485 {
486         dsl_pool_t *dp;
487         dsl_dataset_t *ds;
488         int err;
489
490         err = dsl_pool_hold(name, tag, &dp);
491         if (err != 0)
492                 return (err);
493         err = dsl_dataset_hold(dp, name, tag, &ds);
494         if (err != 0) {
495                 dsl_pool_rele(dp, tag);
496                 return (err);
497         }
498
499         err = dmu_objset_from_ds(ds, osp);
500         if (err != 0) {
501                 dsl_dataset_rele(ds, tag);
502                 dsl_pool_rele(dp, tag);
503         }
504
505         return (err);
506 }
507
508 /*
509  * dsl_pool must not be held when this is called.
510  * Upon successful return, there will be a longhold on the dataset,
511  * and the dsl_pool will not be held.
512  */
513 int
514 dmu_objset_own(const char *name, dmu_objset_type_t type,
515     boolean_t readonly, void *tag, objset_t **osp)
516 {
517         dsl_pool_t *dp;
518         dsl_dataset_t *ds;
519         int err;
520
521         err = dsl_pool_hold(name, FTAG, &dp);
522         if (err != 0)
523                 return (err);
524         err = dsl_dataset_own(dp, name, tag, &ds);
525         if (err != 0) {
526                 dsl_pool_rele(dp, FTAG);
527                 return (err);
528         }
529
530         err = dmu_objset_from_ds(ds, osp);
531         dsl_pool_rele(dp, FTAG);
532         if (err != 0) {
533                 dsl_dataset_disown(ds, tag);
534         } else if (type != DMU_OST_ANY && type != (*osp)->os_phys->os_type) {
535                 dsl_dataset_disown(ds, tag);
536                 return (SET_ERROR(EINVAL));
537         } else if (!readonly && dsl_dataset_is_snapshot(ds)) {
538                 dsl_dataset_disown(ds, tag);
539                 return (SET_ERROR(EROFS));
540         }
541         return (err);
542 }
543
544 void
545 dmu_objset_rele(objset_t *os, void *tag)
546 {
547         dsl_pool_t *dp = dmu_objset_pool(os);
548         dsl_dataset_rele(os->os_dsl_dataset, tag);
549         dsl_pool_rele(dp, tag);
550 }
551
552 /*
553  * When we are called, os MUST refer to an objset associated with a dataset
554  * that is owned by 'tag'; that is, is held and long held by 'tag' and ds_owner
555  * == tag.  We will then release and reacquire ownership of the dataset while
556  * holding the pool config_rwlock to avoid intervening namespace or ownership
557  * changes may occur.
558  *
559  * This exists solely to accommodate zfs_ioc_userspace_upgrade()'s desire to
560  * release the hold on its dataset and acquire a new one on the dataset of the
561  * same name so that it can be partially torn down and reconstructed.
562  */
563 void
564 dmu_objset_refresh_ownership(objset_t *os, void *tag)
565 {
566         dsl_pool_t *dp;
567         dsl_dataset_t *ds, *newds;
568         char name[MAXNAMELEN];
569
570         ds = os->os_dsl_dataset;
571         VERIFY3P(ds, !=, NULL);
572         VERIFY3P(ds->ds_owner, ==, tag);
573         VERIFY(dsl_dataset_long_held(ds));
574
575         dsl_dataset_name(ds, name);
576         dp = dmu_objset_pool(os);
577         dsl_pool_config_enter(dp, FTAG);
578         dmu_objset_disown(os, tag);
579         VERIFY0(dsl_dataset_own(dp, name, tag, &newds));
580         VERIFY3P(newds, ==, os->os_dsl_dataset);
581         dsl_pool_config_exit(dp, FTAG);
582 }
583
584 void
585 dmu_objset_disown(objset_t *os, void *tag)
586 {
587         dsl_dataset_disown(os->os_dsl_dataset, tag);
588 }
589
590 void
591 dmu_objset_evict_dbufs(objset_t *os)
592 {
593         dnode_t *dn;
594
595         mutex_enter(&os->os_lock);
596
597         /* process the mdn last, since the other dnodes have holds on it */
598         list_remove(&os->os_dnodes, DMU_META_DNODE(os));
599         list_insert_tail(&os->os_dnodes, DMU_META_DNODE(os));
600
601         /*
602          * Find the first dnode with holds.  We have to do this dance
603          * because dnode_add_ref() only works if you already have a
604          * hold.  If there are no holds then it has no dbufs so OK to
605          * skip.
606          */
607         for (dn = list_head(&os->os_dnodes);
608             dn && !dnode_add_ref(dn, FTAG);
609             dn = list_next(&os->os_dnodes, dn))
610                 continue;
611
612         while (dn) {
613                 dnode_t *next_dn = dn;
614
615                 do {
616                         next_dn = list_next(&os->os_dnodes, next_dn);
617                 } while (next_dn && !dnode_add_ref(next_dn, FTAG));
618
619                 mutex_exit(&os->os_lock);
620                 dnode_evict_dbufs(dn);
621                 dnode_rele(dn, FTAG);
622                 mutex_enter(&os->os_lock);
623                 dn = next_dn;
624         }
625         mutex_exit(&os->os_lock);
626 }
627
628 void
629 dmu_objset_evict(objset_t *os)
630 {
631         dsl_dataset_t *ds = os->os_dsl_dataset;
632
633         for (int t = 0; t < TXG_SIZE; t++)
634                 ASSERT(!dmu_objset_is_dirty(os, t));
635
636         if (ds) {
637                 if (!dsl_dataset_is_snapshot(ds)) {
638                         VERIFY0(dsl_prop_unregister(ds,
639                             zfs_prop_to_name(ZFS_PROP_CHECKSUM),
640                             checksum_changed_cb, os));
641                         VERIFY0(dsl_prop_unregister(ds,
642                             zfs_prop_to_name(ZFS_PROP_COMPRESSION),
643                             compression_changed_cb, os));
644                         VERIFY0(dsl_prop_unregister(ds,
645                             zfs_prop_to_name(ZFS_PROP_COPIES),
646                             copies_changed_cb, os));
647                         VERIFY0(dsl_prop_unregister(ds,
648                             zfs_prop_to_name(ZFS_PROP_DEDUP),
649                             dedup_changed_cb, os));
650                         VERIFY0(dsl_prop_unregister(ds,
651                             zfs_prop_to_name(ZFS_PROP_LOGBIAS),
652                             logbias_changed_cb, os));
653                         VERIFY0(dsl_prop_unregister(ds,
654                             zfs_prop_to_name(ZFS_PROP_SYNC),
655                             sync_changed_cb, os));
656                         VERIFY0(dsl_prop_unregister(ds,
657                             zfs_prop_to_name(ZFS_PROP_REDUNDANT_METADATA),
658                             redundant_metadata_changed_cb, os));
659                         VERIFY0(dsl_prop_unregister(ds,
660                             zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
661                             recordsize_changed_cb, os));
662                 }
663                 VERIFY0(dsl_prop_unregister(ds,
664                     zfs_prop_to_name(ZFS_PROP_PRIMARYCACHE),
665                     primary_cache_changed_cb, os));
666                 VERIFY0(dsl_prop_unregister(ds,
667                     zfs_prop_to_name(ZFS_PROP_SECONDARYCACHE),
668                     secondary_cache_changed_cb, os));
669         }
670
671         if (os->os_sa)
672                 sa_tear_down(os);
673
674         dmu_objset_evict_dbufs(os);
675
676         dnode_special_close(&os->os_meta_dnode);
677         if (DMU_USERUSED_DNODE(os)) {
678                 dnode_special_close(&os->os_userused_dnode);
679                 dnode_special_close(&os->os_groupused_dnode);
680         }
681         zil_free(os->os_zil);
682
683         ASSERT3P(list_head(&os->os_dnodes), ==, NULL);
684
685         VERIFY(arc_buf_remove_ref(os->os_phys_buf, &os->os_phys_buf));
686
687         /*
688          * This is a barrier to prevent the objset from going away in
689          * dnode_move() until we can safely ensure that the objset is still in
690          * use. We consider the objset valid before the barrier and invalid
691          * after the barrier.
692          */
693         rw_enter(&os_lock, RW_READER);
694         rw_exit(&os_lock);
695
696         mutex_destroy(&os->os_lock);
697         mutex_destroy(&os->os_obj_lock);
698         mutex_destroy(&os->os_user_ptr_lock);
699         kmem_free(os, sizeof (objset_t));
700 }
701
702 timestruc_t
703 dmu_objset_snap_cmtime(objset_t *os)
704 {
705         return (dsl_dir_snap_cmtime(os->os_dsl_dataset->ds_dir));
706 }
707
708 /* called from dsl for meta-objset */
709 objset_t *
710 dmu_objset_create_impl(spa_t *spa, dsl_dataset_t *ds, blkptr_t *bp,
711     dmu_objset_type_t type, dmu_tx_t *tx)
712 {
713         objset_t *os;
714         dnode_t *mdn;
715
716         ASSERT(dmu_tx_is_syncing(tx));
717
718         if (ds != NULL)
719                 VERIFY0(dmu_objset_from_ds(ds, &os));
720         else
721                 VERIFY0(dmu_objset_open_impl(spa, NULL, bp, &os));
722
723         mdn = DMU_META_DNODE(os);
724
725         dnode_allocate(mdn, DMU_OT_DNODE, 1 << DNODE_BLOCK_SHIFT,
726             DN_MAX_INDBLKSHIFT, DMU_OT_NONE, 0, tx);
727
728         /*
729          * We don't want to have to increase the meta-dnode's nlevels
730          * later, because then we could do it in quescing context while
731          * we are also accessing it in open context.
732          *
733          * This precaution is not necessary for the MOS (ds == NULL),
734          * because the MOS is only updated in syncing context.
735          * This is most fortunate: the MOS is the only objset that
736          * needs to be synced multiple times as spa_sync() iterates
737          * to convergence, so minimizing its dn_nlevels matters.
738          */
739         if (ds != NULL) {
740                 int levels = 1;
741
742                 /*
743                  * Determine the number of levels necessary for the meta-dnode
744                  * to contain DN_MAX_OBJECT dnodes.
745                  */
746                 while ((uint64_t)mdn->dn_nblkptr << (mdn->dn_datablkshift +
747                     (levels - 1) * (mdn->dn_indblkshift - SPA_BLKPTRSHIFT)) <
748                     DN_MAX_OBJECT * sizeof (dnode_phys_t))
749                         levels++;
750
751                 mdn->dn_next_nlevels[tx->tx_txg & TXG_MASK] =
752                     mdn->dn_nlevels = levels;
753         }
754
755         ASSERT(type != DMU_OST_NONE);
756         ASSERT(type != DMU_OST_ANY);
757         ASSERT(type < DMU_OST_NUMTYPES);
758         os->os_phys->os_type = type;
759         if (dmu_objset_userused_enabled(os)) {
760                 os->os_phys->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
761                 os->os_flags = os->os_phys->os_flags;
762         }
763
764         dsl_dataset_dirty(ds, tx);
765
766         return (os);
767 }
768
769 typedef struct dmu_objset_create_arg {
770         const char *doca_name;
771         cred_t *doca_cred;
772         void (*doca_userfunc)(objset_t *os, void *arg,
773             cred_t *cr, dmu_tx_t *tx);
774         void *doca_userarg;
775         dmu_objset_type_t doca_type;
776         uint64_t doca_flags;
777 } dmu_objset_create_arg_t;
778
779 /*ARGSUSED*/
780 static int
781 dmu_objset_create_check(void *arg, dmu_tx_t *tx)
782 {
783         dmu_objset_create_arg_t *doca = arg;
784         dsl_pool_t *dp = dmu_tx_pool(tx);
785         dsl_dir_t *pdd;
786         const char *tail;
787         int error;
788
789         if (strchr(doca->doca_name, '@') != NULL)
790                 return (SET_ERROR(EINVAL));
791
792         error = dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail);
793         if (error != 0)
794                 return (error);
795         if (tail == NULL) {
796                 dsl_dir_rele(pdd, FTAG);
797                 return (SET_ERROR(EEXIST));
798         }
799         error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
800             doca->doca_cred);
801         dsl_dir_rele(pdd, FTAG);
802
803         return (error);
804 }
805
806 static void
807 dmu_objset_create_sync(void *arg, dmu_tx_t *tx)
808 {
809         dmu_objset_create_arg_t *doca = arg;
810         dsl_pool_t *dp = dmu_tx_pool(tx);
811         dsl_dir_t *pdd;
812         const char *tail;
813         dsl_dataset_t *ds;
814         uint64_t obj;
815         blkptr_t *bp;
816         objset_t *os;
817
818         VERIFY0(dsl_dir_hold(dp, doca->doca_name, FTAG, &pdd, &tail));
819
820         obj = dsl_dataset_create_sync(pdd, tail, NULL, doca->doca_flags,
821             doca->doca_cred, tx);
822
823         VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
824         bp = dsl_dataset_get_blkptr(ds);
825         os = dmu_objset_create_impl(pdd->dd_pool->dp_spa,
826             ds, bp, doca->doca_type, tx);
827
828         if (doca->doca_userfunc != NULL) {
829                 doca->doca_userfunc(os, doca->doca_userarg,
830                     doca->doca_cred, tx);
831         }
832
833         spa_history_log_internal_ds(ds, "create", tx, "");
834         dsl_dataset_rele(ds, FTAG);
835         dsl_dir_rele(pdd, FTAG);
836 }
837
838 int
839 dmu_objset_create(const char *name, dmu_objset_type_t type, uint64_t flags,
840     void (*func)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx), void *arg)
841 {
842         dmu_objset_create_arg_t doca;
843
844         doca.doca_name = name;
845         doca.doca_cred = CRED();
846         doca.doca_flags = flags;
847         doca.doca_userfunc = func;
848         doca.doca_userarg = arg;
849         doca.doca_type = type;
850
851         return (dsl_sync_task(name,
852             dmu_objset_create_check, dmu_objset_create_sync, &doca,
853             5, ZFS_SPACE_CHECK_NORMAL));
854 }
855
856 typedef struct dmu_objset_clone_arg {
857         const char *doca_clone;
858         const char *doca_origin;
859         cred_t *doca_cred;
860 } dmu_objset_clone_arg_t;
861
862 /*ARGSUSED*/
863 static int
864 dmu_objset_clone_check(void *arg, dmu_tx_t *tx)
865 {
866         dmu_objset_clone_arg_t *doca = arg;
867         dsl_dir_t *pdd;
868         const char *tail;
869         int error;
870         dsl_dataset_t *origin;
871         dsl_pool_t *dp = dmu_tx_pool(tx);
872
873         if (strchr(doca->doca_clone, '@') != NULL)
874                 return (SET_ERROR(EINVAL));
875
876         error = dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail);
877         if (error != 0)
878                 return (error);
879         if (tail == NULL) {
880                 dsl_dir_rele(pdd, FTAG);
881                 return (SET_ERROR(EEXIST));
882         }
883
884         error = dsl_fs_ss_limit_check(pdd, 1, ZFS_PROP_FILESYSTEM_LIMIT, NULL,
885             doca->doca_cred);
886         if (error != 0) {
887                 dsl_dir_rele(pdd, FTAG);
888                 return (SET_ERROR(EDQUOT));
889         }
890         dsl_dir_rele(pdd, FTAG);
891
892         error = dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin);
893         if (error != 0)
894                 return (error);
895
896         /* You can only clone snapshots, not the head datasets. */
897         if (!dsl_dataset_is_snapshot(origin)) {
898                 dsl_dataset_rele(origin, FTAG);
899                 return (SET_ERROR(EINVAL));
900         }
901         dsl_dataset_rele(origin, FTAG);
902
903         return (0);
904 }
905
906 static void
907 dmu_objset_clone_sync(void *arg, dmu_tx_t *tx)
908 {
909         dmu_objset_clone_arg_t *doca = arg;
910         dsl_pool_t *dp = dmu_tx_pool(tx);
911         dsl_dir_t *pdd;
912         const char *tail;
913         dsl_dataset_t *origin, *ds;
914         uint64_t obj;
915         char namebuf[MAXNAMELEN];
916
917         VERIFY0(dsl_dir_hold(dp, doca->doca_clone, FTAG, &pdd, &tail));
918         VERIFY0(dsl_dataset_hold(dp, doca->doca_origin, FTAG, &origin));
919
920         obj = dsl_dataset_create_sync(pdd, tail, origin, 0,
921             doca->doca_cred, tx);
922
923         VERIFY0(dsl_dataset_hold_obj(pdd->dd_pool, obj, FTAG, &ds));
924         dsl_dataset_name(origin, namebuf);
925         spa_history_log_internal_ds(ds, "clone", tx,
926             "origin=%s (%llu)", namebuf, origin->ds_object);
927         dsl_dataset_rele(ds, FTAG);
928         dsl_dataset_rele(origin, FTAG);
929         dsl_dir_rele(pdd, FTAG);
930 }
931
932 int
933 dmu_objset_clone(const char *clone, const char *origin)
934 {
935         dmu_objset_clone_arg_t doca;
936
937         doca.doca_clone = clone;
938         doca.doca_origin = origin;
939         doca.doca_cred = CRED();
940
941         return (dsl_sync_task(clone,
942             dmu_objset_clone_check, dmu_objset_clone_sync, &doca,
943             5, ZFS_SPACE_CHECK_NORMAL));
944 }
945
946 int
947 dmu_objset_snapshot_one(const char *fsname, const char *snapname)
948 {
949         int err;
950         char *longsnap = kmem_asprintf("%s@%s", fsname, snapname);
951         nvlist_t *snaps = fnvlist_alloc();
952
953         fnvlist_add_boolean(snaps, longsnap);
954         strfree(longsnap);
955         err = dsl_dataset_snapshot(snaps, NULL, NULL);
956         fnvlist_free(snaps);
957         return (err);
958 }
959
960 static void
961 dmu_objset_sync_dnodes(list_t *list, list_t *newlist, dmu_tx_t *tx)
962 {
963         dnode_t *dn;
964
965         while (dn = list_head(list)) {
966                 ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT);
967                 ASSERT(dn->dn_dbuf->db_data_pending);
968                 /*
969                  * Initialize dn_zio outside dnode_sync() because the
970                  * meta-dnode needs to set it ouside dnode_sync().
971                  */
972                 dn->dn_zio = dn->dn_dbuf->db_data_pending->dr_zio;
973                 ASSERT(dn->dn_zio);
974
975                 ASSERT3U(dn->dn_nlevels, <=, DN_MAX_LEVELS);
976                 list_remove(list, dn);
977
978                 if (newlist) {
979                         (void) dnode_add_ref(dn, newlist);
980                         list_insert_tail(newlist, dn);
981                 }
982
983                 dnode_sync(dn, tx);
984         }
985 }
986
987 /* ARGSUSED */
988 static void
989 dmu_objset_write_ready(zio_t *zio, arc_buf_t *abuf, void *arg)
990 {
991         blkptr_t *bp = zio->io_bp;
992         objset_t *os = arg;
993         dnode_phys_t *dnp = &os->os_phys->os_meta_dnode;
994
995         ASSERT(!BP_IS_EMBEDDED(bp));
996         ASSERT3P(bp, ==, os->os_rootbp);
997         ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_OBJSET);
998         ASSERT0(BP_GET_LEVEL(bp));
999
1000         /*
1001          * Update rootbp fill count: it should be the number of objects
1002          * allocated in the object set (not counting the "special"
1003          * objects that are stored in the objset_phys_t -- the meta
1004          * dnode and user/group accounting objects).
1005          */
1006         bp->blk_fill = 0;
1007         for (int i = 0; i < dnp->dn_nblkptr; i++)
1008                 bp->blk_fill += BP_GET_FILL(&dnp->dn_blkptr[i]);
1009 }
1010
1011 /* ARGSUSED */
1012 static void
1013 dmu_objset_write_done(zio_t *zio, arc_buf_t *abuf, void *arg)
1014 {
1015         blkptr_t *bp = zio->io_bp;
1016         blkptr_t *bp_orig = &zio->io_bp_orig;
1017         objset_t *os = arg;
1018
1019         if (zio->io_flags & ZIO_FLAG_IO_REWRITE) {
1020                 ASSERT(BP_EQUAL(bp, bp_orig));
1021         } else {
1022                 dsl_dataset_t *ds = os->os_dsl_dataset;
1023                 dmu_tx_t *tx = os->os_synctx;
1024
1025                 (void) dsl_dataset_block_kill(ds, bp_orig, tx, B_TRUE);
1026                 dsl_dataset_block_born(ds, bp, tx);
1027         }
1028 }
1029
1030 /* called from dsl */
1031 void
1032 dmu_objset_sync(objset_t *os, zio_t *pio, dmu_tx_t *tx)
1033 {
1034         int txgoff;
1035         zbookmark_phys_t zb;
1036         zio_prop_t zp;
1037         zio_t *zio;
1038         list_t *list;
1039         list_t *newlist = NULL;
1040         dbuf_dirty_record_t *dr;
1041
1042         dprintf_ds(os->os_dsl_dataset, "txg=%llu\n", tx->tx_txg);
1043
1044         ASSERT(dmu_tx_is_syncing(tx));
1045         /* XXX the write_done callback should really give us the tx... */
1046         os->os_synctx = tx;
1047
1048         if (os->os_dsl_dataset == NULL) {
1049                 /*
1050                  * This is the MOS.  If we have upgraded,
1051                  * spa_max_replication() could change, so reset
1052                  * os_copies here.
1053                  */
1054                 os->os_copies = spa_max_replication(os->os_spa);
1055         }
1056
1057         /*
1058          * Create the root block IO
1059          */
1060         SET_BOOKMARK(&zb, os->os_dsl_dataset ?
1061             os->os_dsl_dataset->ds_object : DMU_META_OBJSET,
1062             ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
1063         arc_release(os->os_phys_buf, &os->os_phys_buf);
1064
1065         dmu_write_policy(os, NULL, 0, 0, &zp);
1066
1067         zio = arc_write(pio, os->os_spa, tx->tx_txg,
1068             os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os),
1069             DMU_OS_IS_L2COMPRESSIBLE(os), &zp, dmu_objset_write_ready,
1070             NULL, dmu_objset_write_done, os, ZIO_PRIORITY_ASYNC_WRITE,
1071             ZIO_FLAG_MUSTSUCCEED, &zb);
1072
1073         /*
1074          * Sync special dnodes - the parent IO for the sync is the root block
1075          */
1076         DMU_META_DNODE(os)->dn_zio = zio;
1077         dnode_sync(DMU_META_DNODE(os), tx);
1078
1079         os->os_phys->os_flags = os->os_flags;
1080
1081         if (DMU_USERUSED_DNODE(os) &&
1082             DMU_USERUSED_DNODE(os)->dn_type != DMU_OT_NONE) {
1083                 DMU_USERUSED_DNODE(os)->dn_zio = zio;
1084                 dnode_sync(DMU_USERUSED_DNODE(os), tx);
1085                 DMU_GROUPUSED_DNODE(os)->dn_zio = zio;
1086                 dnode_sync(DMU_GROUPUSED_DNODE(os), tx);
1087         }
1088
1089         txgoff = tx->tx_txg & TXG_MASK;
1090
1091         if (dmu_objset_userused_enabled(os)) {
1092                 newlist = &os->os_synced_dnodes;
1093                 /*
1094                  * We must create the list here because it uses the
1095                  * dn_dirty_link[] of this txg.
1096                  */
1097                 list_create(newlist, sizeof (dnode_t),
1098                     offsetof(dnode_t, dn_dirty_link[txgoff]));
1099         }
1100
1101         dmu_objset_sync_dnodes(&os->os_free_dnodes[txgoff], newlist, tx);
1102         dmu_objset_sync_dnodes(&os->os_dirty_dnodes[txgoff], newlist, tx);
1103
1104         list = &DMU_META_DNODE(os)->dn_dirty_records[txgoff];
1105         while (dr = list_head(list)) {
1106                 ASSERT0(dr->dr_dbuf->db_level);
1107                 list_remove(list, dr);
1108                 if (dr->dr_zio)
1109                         zio_nowait(dr->dr_zio);
1110         }
1111         /*
1112          * Free intent log blocks up to this tx.
1113          */
1114         zil_sync(os->os_zil, tx);
1115         os->os_phys->os_zil_header = os->os_zil_header;
1116         zio_nowait(zio);
1117 }
1118
1119 boolean_t
1120 dmu_objset_is_dirty(objset_t *os, uint64_t txg)
1121 {
1122         return (!list_is_empty(&os->os_dirty_dnodes[txg & TXG_MASK]) ||
1123             !list_is_empty(&os->os_free_dnodes[txg & TXG_MASK]));
1124 }
1125
1126 static objset_used_cb_t *used_cbs[DMU_OST_NUMTYPES];
1127
1128 void
1129 dmu_objset_register_type(dmu_objset_type_t ost, objset_used_cb_t *cb)
1130 {
1131         used_cbs[ost] = cb;
1132 }
1133
1134 boolean_t
1135 dmu_objset_userused_enabled(objset_t *os)
1136 {
1137         return (spa_version(os->os_spa) >= SPA_VERSION_USERSPACE &&
1138             used_cbs[os->os_phys->os_type] != NULL &&
1139             DMU_USERUSED_DNODE(os) != NULL);
1140 }
1141
1142 static void
1143 do_userquota_update(objset_t *os, uint64_t used, uint64_t flags,
1144     uint64_t user, uint64_t group, boolean_t subtract, dmu_tx_t *tx)
1145 {
1146         if ((flags & DNODE_FLAG_USERUSED_ACCOUNTED)) {
1147                 int64_t delta = DNODE_SIZE + used;
1148                 if (subtract)
1149                         delta = -delta;
1150                 VERIFY3U(0, ==, zap_increment_int(os, DMU_USERUSED_OBJECT,
1151                     user, delta, tx));
1152                 VERIFY3U(0, ==, zap_increment_int(os, DMU_GROUPUSED_OBJECT,
1153                     group, delta, tx));
1154         }
1155 }
1156
1157 void
1158 dmu_objset_do_userquota_updates(objset_t *os, dmu_tx_t *tx)
1159 {
1160         dnode_t *dn;
1161         list_t *list = &os->os_synced_dnodes;
1162
1163         ASSERT(list_head(list) == NULL || dmu_objset_userused_enabled(os));
1164
1165         while (dn = list_head(list)) {
1166                 int flags;
1167                 ASSERT(!DMU_OBJECT_IS_SPECIAL(dn->dn_object));
1168                 ASSERT(dn->dn_phys->dn_type == DMU_OT_NONE ||
1169                     dn->dn_phys->dn_flags &
1170                     DNODE_FLAG_USERUSED_ACCOUNTED);
1171
1172                 /* Allocate the user/groupused objects if necessary. */
1173                 if (DMU_USERUSED_DNODE(os)->dn_type == DMU_OT_NONE) {
1174                         VERIFY(0 == zap_create_claim(os,
1175                             DMU_USERUSED_OBJECT,
1176                             DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1177                         VERIFY(0 == zap_create_claim(os,
1178                             DMU_GROUPUSED_OBJECT,
1179                             DMU_OT_USERGROUP_USED, DMU_OT_NONE, 0, tx));
1180                 }
1181
1182                 /*
1183                  * We intentionally modify the zap object even if the
1184                  * net delta is zero.  Otherwise
1185                  * the block of the zap obj could be shared between
1186                  * datasets but need to be different between them after
1187                  * a bprewrite.
1188                  */
1189
1190                 flags = dn->dn_id_flags;
1191                 ASSERT(flags);
1192                 if (flags & DN_ID_OLD_EXIST)  {
1193                         do_userquota_update(os, dn->dn_oldused, dn->dn_oldflags,
1194                             dn->dn_olduid, dn->dn_oldgid, B_TRUE, tx);
1195                 }
1196                 if (flags & DN_ID_NEW_EXIST) {
1197                         do_userquota_update(os, DN_USED_BYTES(dn->dn_phys),
1198                             dn->dn_phys->dn_flags,  dn->dn_newuid,
1199                             dn->dn_newgid, B_FALSE, tx);
1200                 }
1201
1202                 mutex_enter(&dn->dn_mtx);
1203                 dn->dn_oldused = 0;
1204                 dn->dn_oldflags = 0;
1205                 if (dn->dn_id_flags & DN_ID_NEW_EXIST) {
1206                         dn->dn_olduid = dn->dn_newuid;
1207                         dn->dn_oldgid = dn->dn_newgid;
1208                         dn->dn_id_flags |= DN_ID_OLD_EXIST;
1209                         if (dn->dn_bonuslen == 0)
1210                                 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1211                         else
1212                                 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1213                 }
1214                 dn->dn_id_flags &= ~(DN_ID_NEW_EXIST);
1215                 mutex_exit(&dn->dn_mtx);
1216
1217                 list_remove(list, dn);
1218                 dnode_rele(dn, list);
1219         }
1220 }
1221
1222 /*
1223  * Returns a pointer to data to find uid/gid from
1224  *
1225  * If a dirty record for transaction group that is syncing can't
1226  * be found then NULL is returned.  In the NULL case it is assumed
1227  * the uid/gid aren't changing.
1228  */
1229 static void *
1230 dmu_objset_userquota_find_data(dmu_buf_impl_t *db, dmu_tx_t *tx)
1231 {
1232         dbuf_dirty_record_t *dr, **drp;
1233         void *data;
1234
1235         if (db->db_dirtycnt == 0)
1236                 return (db->db.db_data);  /* Nothing is changing */
1237
1238         for (drp = &db->db_last_dirty; (dr = *drp) != NULL; drp = &dr->dr_next)
1239                 if (dr->dr_txg == tx->tx_txg)
1240                         break;
1241
1242         if (dr == NULL) {
1243                 data = NULL;
1244         } else {
1245                 dnode_t *dn;
1246
1247                 DB_DNODE_ENTER(dr->dr_dbuf);
1248                 dn = DB_DNODE(dr->dr_dbuf);
1249
1250                 if (dn->dn_bonuslen == 0 &&
1251                     dr->dr_dbuf->db_blkid == DMU_SPILL_BLKID)
1252                         data = dr->dt.dl.dr_data->b_data;
1253                 else
1254                         data = dr->dt.dl.dr_data;
1255
1256                 DB_DNODE_EXIT(dr->dr_dbuf);
1257         }
1258
1259         return (data);
1260 }
1261
1262 void
1263 dmu_objset_userquota_get_ids(dnode_t *dn, boolean_t before, dmu_tx_t *tx)
1264 {
1265         objset_t *os = dn->dn_objset;
1266         void *data = NULL;
1267         dmu_buf_impl_t *db = NULL;
1268         uint64_t *user = NULL;
1269         uint64_t *group = NULL;
1270         int flags = dn->dn_id_flags;
1271         int error;
1272         boolean_t have_spill = B_FALSE;
1273
1274         if (!dmu_objset_userused_enabled(dn->dn_objset))
1275                 return;
1276
1277         if (before && (flags & (DN_ID_CHKED_BONUS|DN_ID_OLD_EXIST|
1278             DN_ID_CHKED_SPILL)))
1279                 return;
1280
1281         if (before && dn->dn_bonuslen != 0)
1282                 data = DN_BONUS(dn->dn_phys);
1283         else if (!before && dn->dn_bonuslen != 0) {
1284                 if (dn->dn_bonus) {
1285                         db = dn->dn_bonus;
1286                         mutex_enter(&db->db_mtx);
1287                         data = dmu_objset_userquota_find_data(db, tx);
1288                 } else {
1289                         data = DN_BONUS(dn->dn_phys);
1290                 }
1291         } else if (dn->dn_bonuslen == 0 && dn->dn_bonustype == DMU_OT_SA) {
1292                         int rf = 0;
1293
1294                         if (RW_WRITE_HELD(&dn->dn_struct_rwlock))
1295                                 rf |= DB_RF_HAVESTRUCT;
1296                         error = dmu_spill_hold_by_dnode(dn,
1297                             rf | DB_RF_MUST_SUCCEED,
1298                             FTAG, (dmu_buf_t **)&db);
1299                         ASSERT(error == 0);
1300                         mutex_enter(&db->db_mtx);
1301                         data = (before) ? db->db.db_data :
1302                             dmu_objset_userquota_find_data(db, tx);
1303                         have_spill = B_TRUE;
1304         } else {
1305                 mutex_enter(&dn->dn_mtx);
1306                 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1307                 mutex_exit(&dn->dn_mtx);
1308                 return;
1309         }
1310
1311         if (before) {
1312                 ASSERT(data);
1313                 user = &dn->dn_olduid;
1314                 group = &dn->dn_oldgid;
1315         } else if (data) {
1316                 user = &dn->dn_newuid;
1317                 group = &dn->dn_newgid;
1318         }
1319
1320         /*
1321          * Must always call the callback in case the object
1322          * type has changed and that type isn't an object type to track
1323          */
1324         error = used_cbs[os->os_phys->os_type](dn->dn_bonustype, data,
1325             user, group);
1326
1327         /*
1328          * Preserve existing uid/gid when the callback can't determine
1329          * what the new uid/gid are and the callback returned EEXIST.
1330          * The EEXIST error tells us to just use the existing uid/gid.
1331          * If we don't know what the old values are then just assign
1332          * them to 0, since that is a new file  being created.
1333          */
1334         if (!before && data == NULL && error == EEXIST) {
1335                 if (flags & DN_ID_OLD_EXIST) {
1336                         dn->dn_newuid = dn->dn_olduid;
1337                         dn->dn_newgid = dn->dn_oldgid;
1338                 } else {
1339                         dn->dn_newuid = 0;
1340                         dn->dn_newgid = 0;
1341                 }
1342                 error = 0;
1343         }
1344
1345         if (db)
1346                 mutex_exit(&db->db_mtx);
1347
1348         mutex_enter(&dn->dn_mtx);
1349         if (error == 0 && before)
1350                 dn->dn_id_flags |= DN_ID_OLD_EXIST;
1351         if (error == 0 && !before)
1352                 dn->dn_id_flags |= DN_ID_NEW_EXIST;
1353
1354         if (have_spill) {
1355                 dn->dn_id_flags |= DN_ID_CHKED_SPILL;
1356         } else {
1357                 dn->dn_id_flags |= DN_ID_CHKED_BONUS;
1358         }
1359         mutex_exit(&dn->dn_mtx);
1360         if (have_spill)
1361                 dmu_buf_rele((dmu_buf_t *)db, FTAG);
1362 }
1363
1364 boolean_t
1365 dmu_objset_userspace_present(objset_t *os)
1366 {
1367         return (os->os_phys->os_flags &
1368             OBJSET_FLAG_USERACCOUNTING_COMPLETE);
1369 }
1370
1371 int
1372 dmu_objset_userspace_upgrade(objset_t *os)
1373 {
1374         uint64_t obj;
1375         int err = 0;
1376
1377         if (dmu_objset_userspace_present(os))
1378                 return (0);
1379         if (!dmu_objset_userused_enabled(os))
1380                 return (SET_ERROR(ENOTSUP));
1381         if (dmu_objset_is_snapshot(os))
1382                 return (SET_ERROR(EINVAL));
1383
1384         /*
1385          * We simply need to mark every object dirty, so that it will be
1386          * synced out and now accounted.  If this is called
1387          * concurrently, or if we already did some work before crashing,
1388          * that's fine, since we track each object's accounted state
1389          * independently.
1390          */
1391
1392         for (obj = 0; err == 0; err = dmu_object_next(os, &obj, FALSE, 0)) {
1393                 dmu_tx_t *tx;
1394                 dmu_buf_t *db;
1395                 int objerr;
1396
1397                 if (issig(JUSTLOOKING) && issig(FORREAL))
1398                         return (SET_ERROR(EINTR));
1399
1400                 objerr = dmu_bonus_hold(os, obj, FTAG, &db);
1401                 if (objerr != 0)
1402                         continue;
1403                 tx = dmu_tx_create(os);
1404                 dmu_tx_hold_bonus(tx, obj);
1405                 objerr = dmu_tx_assign(tx, TXG_WAIT);
1406                 if (objerr != 0) {
1407                         dmu_tx_abort(tx);
1408                         continue;
1409                 }
1410                 dmu_buf_will_dirty(db, tx);
1411                 dmu_buf_rele(db, FTAG);
1412                 dmu_tx_commit(tx);
1413         }
1414
1415         os->os_flags |= OBJSET_FLAG_USERACCOUNTING_COMPLETE;
1416         txg_wait_synced(dmu_objset_pool(os), 0);
1417         return (0);
1418 }
1419
1420 void
1421 dmu_objset_space(objset_t *os, uint64_t *refdbytesp, uint64_t *availbytesp,
1422     uint64_t *usedobjsp, uint64_t *availobjsp)
1423 {
1424         dsl_dataset_space(os->os_dsl_dataset, refdbytesp, availbytesp,
1425             usedobjsp, availobjsp);
1426 }
1427
1428 uint64_t
1429 dmu_objset_fsid_guid(objset_t *os)
1430 {
1431         return (dsl_dataset_fsid_guid(os->os_dsl_dataset));
1432 }
1433
1434 void
1435 dmu_objset_fast_stat(objset_t *os, dmu_objset_stats_t *stat)
1436 {
1437         stat->dds_type = os->os_phys->os_type;
1438         if (os->os_dsl_dataset)
1439                 dsl_dataset_fast_stat(os->os_dsl_dataset, stat);
1440 }
1441
1442 void
1443 dmu_objset_stats(objset_t *os, nvlist_t *nv)
1444 {
1445         ASSERT(os->os_dsl_dataset ||
1446             os->os_phys->os_type == DMU_OST_META);
1447
1448         if (os->os_dsl_dataset != NULL)
1449                 dsl_dataset_stats(os->os_dsl_dataset, nv);
1450
1451         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_TYPE,
1452             os->os_phys->os_type);
1453         dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERACCOUNTING,
1454             dmu_objset_userspace_present(os));
1455 }
1456
1457 int
1458 dmu_objset_is_snapshot(objset_t *os)
1459 {
1460         if (os->os_dsl_dataset != NULL)
1461                 return (dsl_dataset_is_snapshot(os->os_dsl_dataset));
1462         else
1463                 return (B_FALSE);
1464 }
1465
1466 int
1467 dmu_snapshot_realname(objset_t *os, char *name, char *real, int maxlen,
1468     boolean_t *conflict)
1469 {
1470         dsl_dataset_t *ds = os->os_dsl_dataset;
1471         uint64_t ignored;
1472
1473         if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1474                 return (SET_ERROR(ENOENT));
1475
1476         return (zap_lookup_norm(ds->ds_dir->dd_pool->dp_meta_objset,
1477             dsl_dataset_phys(ds)->ds_snapnames_zapobj, name, 8, 1, &ignored,
1478             MT_FIRST, real, maxlen, conflict));
1479 }
1480
1481 int
1482 dmu_snapshot_list_next(objset_t *os, int namelen, char *name,
1483     uint64_t *idp, uint64_t *offp, boolean_t *case_conflict)
1484 {
1485         dsl_dataset_t *ds = os->os_dsl_dataset;
1486         zap_cursor_t cursor;
1487         zap_attribute_t attr;
1488
1489         ASSERT(dsl_pool_config_held(dmu_objset_pool(os)));
1490
1491         if (dsl_dataset_phys(ds)->ds_snapnames_zapobj == 0)
1492                 return (SET_ERROR(ENOENT));
1493
1494         zap_cursor_init_serialized(&cursor,
1495             ds->ds_dir->dd_pool->dp_meta_objset,
1496             dsl_dataset_phys(ds)->ds_snapnames_zapobj, *offp);
1497
1498         if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1499                 zap_cursor_fini(&cursor);
1500                 return (SET_ERROR(ENOENT));
1501         }
1502
1503         if (strlen(attr.za_name) + 1 > namelen) {
1504                 zap_cursor_fini(&cursor);
1505                 return (SET_ERROR(ENAMETOOLONG));
1506         }
1507
1508         (void) strcpy(name, attr.za_name);
1509         if (idp)
1510                 *idp = attr.za_first_integer;
1511         if (case_conflict)
1512                 *case_conflict = attr.za_normalization_conflict;
1513         zap_cursor_advance(&cursor);
1514         *offp = zap_cursor_serialize(&cursor);
1515         zap_cursor_fini(&cursor);
1516
1517         return (0);
1518 }
1519
1520 int
1521 dmu_dir_list_next(objset_t *os, int namelen, char *name,
1522     uint64_t *idp, uint64_t *offp)
1523 {
1524         dsl_dir_t *dd = os->os_dsl_dataset->ds_dir;
1525         zap_cursor_t cursor;
1526         zap_attribute_t attr;
1527
1528         /* there is no next dir on a snapshot! */
1529         if (os->os_dsl_dataset->ds_object !=
1530             dsl_dir_phys(dd)->dd_head_dataset_obj)
1531                 return (SET_ERROR(ENOENT));
1532
1533         zap_cursor_init_serialized(&cursor,
1534             dd->dd_pool->dp_meta_objset,
1535             dsl_dir_phys(dd)->dd_child_dir_zapobj, *offp);
1536
1537         if (zap_cursor_retrieve(&cursor, &attr) != 0) {
1538                 zap_cursor_fini(&cursor);
1539                 return (SET_ERROR(ENOENT));
1540         }
1541
1542         if (strlen(attr.za_name) + 1 > namelen) {
1543                 zap_cursor_fini(&cursor);
1544                 return (SET_ERROR(ENAMETOOLONG));
1545         }
1546
1547         (void) strcpy(name, attr.za_name);
1548         if (idp)
1549                 *idp = attr.za_first_integer;
1550         zap_cursor_advance(&cursor);
1551         *offp = zap_cursor_serialize(&cursor);
1552         zap_cursor_fini(&cursor);
1553
1554         return (0);
1555 }
1556
1557 /*
1558  * Find objsets under and including ddobj, call func(ds) on each.
1559  */
1560 int
1561 dmu_objset_find_dp(dsl_pool_t *dp, uint64_t ddobj,
1562     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg, int flags)
1563 {
1564         dsl_dir_t *dd;
1565         dsl_dataset_t *ds;
1566         zap_cursor_t zc;
1567         zap_attribute_t *attr;
1568         uint64_t thisobj;
1569         int err;
1570
1571         ASSERT(dsl_pool_config_held(dp));
1572
1573         err = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
1574         if (err != 0)
1575                 return (err);
1576
1577         /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1578         if (dd->dd_myname[0] == '$') {
1579                 dsl_dir_rele(dd, FTAG);
1580                 return (0);
1581         }
1582
1583         thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1584         attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1585
1586         /*
1587          * Iterate over all children.
1588          */
1589         if (flags & DS_FIND_CHILDREN) {
1590                 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1591                     dsl_dir_phys(dd)->dd_child_dir_zapobj);
1592                     zap_cursor_retrieve(&zc, attr) == 0;
1593                     (void) zap_cursor_advance(&zc)) {
1594                         ASSERT3U(attr->za_integer_length, ==,
1595                             sizeof (uint64_t));
1596                         ASSERT3U(attr->za_num_integers, ==, 1);
1597
1598                         err = dmu_objset_find_dp(dp, attr->za_first_integer,
1599                             func, arg, flags);
1600                         if (err != 0)
1601                                 break;
1602                 }
1603                 zap_cursor_fini(&zc);
1604
1605                 if (err != 0) {
1606                         dsl_dir_rele(dd, FTAG);
1607                         kmem_free(attr, sizeof (zap_attribute_t));
1608                         return (err);
1609                 }
1610         }
1611
1612         /*
1613          * Iterate over all snapshots.
1614          */
1615         if (flags & DS_FIND_SNAPSHOTS) {
1616                 dsl_dataset_t *ds;
1617                 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1618
1619                 if (err == 0) {
1620                         uint64_t snapobj;
1621
1622                         snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1623                         dsl_dataset_rele(ds, FTAG);
1624
1625                         for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1626                             zap_cursor_retrieve(&zc, attr) == 0;
1627                             (void) zap_cursor_advance(&zc)) {
1628                                 ASSERT3U(attr->za_integer_length, ==,
1629                                     sizeof (uint64_t));
1630                                 ASSERT3U(attr->za_num_integers, ==, 1);
1631
1632                                 err = dsl_dataset_hold_obj(dp,
1633                                     attr->za_first_integer, FTAG, &ds);
1634                                 if (err != 0)
1635                                         break;
1636                                 err = func(dp, ds, arg);
1637                                 dsl_dataset_rele(ds, FTAG);
1638                                 if (err != 0)
1639                                         break;
1640                         }
1641                         zap_cursor_fini(&zc);
1642                 }
1643         }
1644
1645         dsl_dir_rele(dd, FTAG);
1646         kmem_free(attr, sizeof (zap_attribute_t));
1647
1648         if (err != 0)
1649                 return (err);
1650
1651         /*
1652          * Apply to self.
1653          */
1654         err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1655         if (err != 0)
1656                 return (err);
1657         err = func(dp, ds, arg);
1658         dsl_dataset_rele(ds, FTAG);
1659         return (err);
1660 }
1661
1662 /*
1663  * Find all objsets under name, and for each, call 'func(child_name, arg)'.
1664  * The dp_config_rwlock must not be held when this is called, and it
1665  * will not be held when the callback is called.
1666  * Therefore this function should only be used when the pool is not changing
1667  * (e.g. in syncing context), or the callback can deal with the possible races.
1668  */
1669 static int
1670 dmu_objset_find_impl(spa_t *spa, const char *name,
1671     int func(const char *, void *), void *arg, int flags)
1672 {
1673         dsl_dir_t *dd;
1674         dsl_pool_t *dp = spa_get_dsl(spa);
1675         dsl_dataset_t *ds;
1676         zap_cursor_t zc;
1677         zap_attribute_t *attr;
1678         char *child;
1679         uint64_t thisobj;
1680         int err;
1681
1682         dsl_pool_config_enter(dp, FTAG);
1683
1684         err = dsl_dir_hold(dp, name, FTAG, &dd, NULL);
1685         if (err != 0) {
1686                 dsl_pool_config_exit(dp, FTAG);
1687                 return (err);
1688         }
1689
1690         /* Don't visit hidden ($MOS & $ORIGIN) objsets. */
1691         if (dd->dd_myname[0] == '$') {
1692                 dsl_dir_rele(dd, FTAG);
1693                 dsl_pool_config_exit(dp, FTAG);
1694                 return (0);
1695         }
1696
1697         thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
1698         attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1699
1700         /*
1701          * Iterate over all children.
1702          */
1703         if (flags & DS_FIND_CHILDREN) {
1704                 for (zap_cursor_init(&zc, dp->dp_meta_objset,
1705                     dsl_dir_phys(dd)->dd_child_dir_zapobj);
1706                     zap_cursor_retrieve(&zc, attr) == 0;
1707                     (void) zap_cursor_advance(&zc)) {
1708                         ASSERT3U(attr->za_integer_length, ==,
1709                             sizeof (uint64_t));
1710                         ASSERT3U(attr->za_num_integers, ==, 1);
1711
1712                         child = kmem_asprintf("%s/%s", name, attr->za_name);
1713                         dsl_pool_config_exit(dp, FTAG);
1714                         err = dmu_objset_find_impl(spa, child,
1715                             func, arg, flags);
1716                         dsl_pool_config_enter(dp, FTAG);
1717                         strfree(child);
1718                         if (err != 0)
1719                                 break;
1720                 }
1721                 zap_cursor_fini(&zc);
1722
1723                 if (err != 0) {
1724                         dsl_dir_rele(dd, FTAG);
1725                         dsl_pool_config_exit(dp, FTAG);
1726                         kmem_free(attr, sizeof (zap_attribute_t));
1727                         return (err);
1728                 }
1729         }
1730
1731         /*
1732          * Iterate over all snapshots.
1733          */
1734         if (flags & DS_FIND_SNAPSHOTS) {
1735                 err = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
1736
1737                 if (err == 0) {
1738                         uint64_t snapobj;
1739
1740                         snapobj = dsl_dataset_phys(ds)->ds_snapnames_zapobj;
1741                         dsl_dataset_rele(ds, FTAG);
1742
1743                         for (zap_cursor_init(&zc, dp->dp_meta_objset, snapobj);
1744                             zap_cursor_retrieve(&zc, attr) == 0;
1745                             (void) zap_cursor_advance(&zc)) {
1746                                 ASSERT3U(attr->za_integer_length, ==,
1747                                     sizeof (uint64_t));
1748                                 ASSERT3U(attr->za_num_integers, ==, 1);
1749
1750                                 child = kmem_asprintf("%s@%s",
1751                                     name, attr->za_name);
1752                                 dsl_pool_config_exit(dp, FTAG);
1753                                 err = func(child, arg);
1754                                 dsl_pool_config_enter(dp, FTAG);
1755                                 strfree(child);
1756                                 if (err != 0)
1757                                         break;
1758                         }
1759                         zap_cursor_fini(&zc);
1760                 }
1761         }
1762
1763         dsl_dir_rele(dd, FTAG);
1764         kmem_free(attr, sizeof (zap_attribute_t));
1765         dsl_pool_config_exit(dp, FTAG);
1766
1767         if (err != 0)
1768                 return (err);
1769
1770         /* Apply to self. */
1771         return (func(name, arg));
1772 }
1773
1774 /*
1775  * See comment above dmu_objset_find_impl().
1776  */
1777 int
1778 dmu_objset_find(char *name, int func(const char *, void *), void *arg,
1779     int flags)
1780 {
1781         spa_t *spa;
1782         int error;
1783
1784         error = spa_open(name, &spa, FTAG);
1785         if (error != 0)
1786                 return (error);
1787         error = dmu_objset_find_impl(spa, name, func, arg, flags);
1788         spa_close(spa, FTAG);
1789         return (error);
1790 }
1791
1792 void
1793 dmu_objset_set_user(objset_t *os, void *user_ptr)
1794 {
1795         ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1796         os->os_user_ptr = user_ptr;
1797 }
1798
1799 void *
1800 dmu_objset_get_user(objset_t *os)
1801 {
1802         ASSERT(MUTEX_HELD(&os->os_user_ptr_lock));
1803         return (os->os_user_ptr);
1804 }
1805
1806 /*
1807  * Determine name of filesystem, given name of snapshot.
1808  * buf must be at least MAXNAMELEN bytes
1809  */
1810 int
1811 dmu_fsname(const char *snapname, char *buf)
1812 {
1813         char *atp = strchr(snapname, '@');
1814         if (atp == NULL)
1815                 return (SET_ERROR(EINVAL));
1816         if (atp - snapname >= MAXNAMELEN)
1817                 return (SET_ERROR(ENAMETOOLONG));
1818         (void) strlcpy(buf, snapname, atp - snapname + 1);
1819         return (0);
1820 }