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