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