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