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