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