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