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