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