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