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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
24 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28 #include <sys/dmu_impl.h>
29 #include <sys/dmu_tx.h>
31 #include <sys/dnode.h>
32 #include <sys/zfs_context.h>
33 #include <sys/dmu_objset.h>
34 #include <sys/dmu_traverse.h>
35 #include <sys/dsl_dataset.h>
36 #include <sys/dsl_dir.h>
37 #include <sys/dsl_pool.h>
38 #include <sys/dsl_synctask.h>
39 #include <sys/dsl_prop.h>
40 #include <sys/dmu_zfetch.h>
41 #include <sys/zfs_ioctl.h>
43 #include <sys/zio_checksum.h>
46 #include <sys/vmsystm.h>
47 #include <sys/zfs_znode.h>
50 const dmu_object_type_info_t dmu_ot
[DMU_OT_NUMTYPES
] = {
51 { DMU_BSWAP_UINT8
, TRUE
, "unallocated" },
52 { DMU_BSWAP_ZAP
, TRUE
, "object directory" },
53 { DMU_BSWAP_UINT64
, TRUE
, "object array" },
54 { DMU_BSWAP_UINT8
, TRUE
, "packed nvlist" },
55 { DMU_BSWAP_UINT64
, TRUE
, "packed nvlist size" },
56 { DMU_BSWAP_UINT64
, TRUE
, "bpobj" },
57 { DMU_BSWAP_UINT64
, TRUE
, "bpobj header" },
58 { DMU_BSWAP_UINT64
, TRUE
, "SPA space map header" },
59 { DMU_BSWAP_UINT64
, TRUE
, "SPA space map" },
60 { DMU_BSWAP_UINT64
, TRUE
, "ZIL intent log" },
61 { DMU_BSWAP_DNODE
, TRUE
, "DMU dnode" },
62 { DMU_BSWAP_OBJSET
, TRUE
, "DMU objset" },
63 { DMU_BSWAP_UINT64
, TRUE
, "DSL directory" },
64 { DMU_BSWAP_ZAP
, TRUE
, "DSL directory child map"},
65 { DMU_BSWAP_ZAP
, TRUE
, "DSL dataset snap map" },
66 { DMU_BSWAP_ZAP
, TRUE
, "DSL props" },
67 { DMU_BSWAP_UINT64
, TRUE
, "DSL dataset" },
68 { DMU_BSWAP_ZNODE
, TRUE
, "ZFS znode" },
69 { DMU_BSWAP_OLDACL
, TRUE
, "ZFS V0 ACL" },
70 { DMU_BSWAP_UINT8
, FALSE
, "ZFS plain file" },
71 { DMU_BSWAP_ZAP
, TRUE
, "ZFS directory" },
72 { DMU_BSWAP_ZAP
, TRUE
, "ZFS master node" },
73 { DMU_BSWAP_ZAP
, TRUE
, "ZFS delete queue" },
74 { DMU_BSWAP_UINT8
, FALSE
, "zvol object" },
75 { DMU_BSWAP_ZAP
, TRUE
, "zvol prop" },
76 { DMU_BSWAP_UINT8
, FALSE
, "other uint8[]" },
77 { DMU_BSWAP_UINT64
, FALSE
, "other uint64[]" },
78 { DMU_BSWAP_ZAP
, TRUE
, "other ZAP" },
79 { DMU_BSWAP_ZAP
, TRUE
, "persistent error log" },
80 { DMU_BSWAP_UINT8
, TRUE
, "SPA history" },
81 { DMU_BSWAP_UINT64
, TRUE
, "SPA history offsets" },
82 { DMU_BSWAP_ZAP
, TRUE
, "Pool properties" },
83 { DMU_BSWAP_ZAP
, TRUE
, "DSL permissions" },
84 { DMU_BSWAP_ACL
, TRUE
, "ZFS ACL" },
85 { DMU_BSWAP_UINT8
, TRUE
, "ZFS SYSACL" },
86 { DMU_BSWAP_UINT8
, TRUE
, "FUID table" },
87 { DMU_BSWAP_UINT64
, TRUE
, "FUID table size" },
88 { DMU_BSWAP_ZAP
, TRUE
, "DSL dataset next clones"},
89 { DMU_BSWAP_ZAP
, TRUE
, "scan work queue" },
90 { DMU_BSWAP_ZAP
, TRUE
, "ZFS user/group used" },
91 { DMU_BSWAP_ZAP
, TRUE
, "ZFS user/group quota" },
92 { DMU_BSWAP_ZAP
, TRUE
, "snapshot refcount tags"},
93 { DMU_BSWAP_ZAP
, TRUE
, "DDT ZAP algorithm" },
94 { DMU_BSWAP_ZAP
, TRUE
, "DDT statistics" },
95 { DMU_BSWAP_UINT8
, TRUE
, "System attributes" },
96 { DMU_BSWAP_ZAP
, TRUE
, "SA master node" },
97 { DMU_BSWAP_ZAP
, TRUE
, "SA attr registration" },
98 { DMU_BSWAP_ZAP
, TRUE
, "SA attr layouts" },
99 { DMU_BSWAP_ZAP
, TRUE
, "scan translations" },
100 { DMU_BSWAP_UINT8
, FALSE
, "deduplicated block" },
101 { DMU_BSWAP_ZAP
, TRUE
, "DSL deadlist map" },
102 { DMU_BSWAP_UINT64
, TRUE
, "DSL deadlist map hdr" },
103 { DMU_BSWAP_ZAP
, TRUE
, "DSL dir clones" },
104 { DMU_BSWAP_UINT64
, TRUE
, "bpobj subobj" }
107 const dmu_object_byteswap_info_t dmu_ot_byteswap
[DMU_BSWAP_NUMFUNCS
] = {
108 { byteswap_uint8_array
, "uint8" },
109 { byteswap_uint16_array
, "uint16" },
110 { byteswap_uint32_array
, "uint32" },
111 { byteswap_uint64_array
, "uint64" },
112 { zap_byteswap
, "zap" },
113 { dnode_buf_byteswap
, "dnode" },
114 { dmu_objset_byteswap
, "objset" },
115 { zfs_znode_byteswap
, "znode" },
116 { zfs_oldacl_byteswap
, "oldacl" },
117 { zfs_acl_byteswap
, "acl" }
121 dmu_buf_hold(objset_t
*os
, uint64_t object
, uint64_t offset
,
122 void *tag
, dmu_buf_t
**dbp
, int flags
)
128 int db_flags
= DB_RF_CANFAIL
;
130 if (flags
& DMU_READ_NO_PREFETCH
)
131 db_flags
|= DB_RF_NOPREFETCH
;
133 err
= dnode_hold(os
, object
, FTAG
, &dn
);
136 blkid
= dbuf_whichblock(dn
, offset
);
137 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
138 db
= dbuf_hold(dn
, blkid
, tag
);
139 rw_exit(&dn
->dn_struct_rwlock
);
143 err
= dbuf_read(db
, NULL
, db_flags
);
150 dnode_rele(dn
, FTAG
);
151 *dbp
= &db
->db
; /* NULL db plus first field offset is NULL */
158 return (DN_MAX_BONUSLEN
);
162 dmu_set_bonus(dmu_buf_t
*db_fake
, int newsize
, dmu_tx_t
*tx
)
164 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
171 if (dn
->dn_bonus
!= db
) {
173 } else if (newsize
< 0 || newsize
> db_fake
->db_size
) {
176 dnode_setbonuslen(dn
, newsize
, tx
);
185 dmu_set_bonustype(dmu_buf_t
*db_fake
, dmu_object_type_t type
, dmu_tx_t
*tx
)
187 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
194 if (!DMU_OT_IS_VALID(type
)) {
196 } else if (dn
->dn_bonus
!= db
) {
199 dnode_setbonus_type(dn
, type
, tx
);
208 dmu_get_bonustype(dmu_buf_t
*db_fake
)
210 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
212 dmu_object_type_t type
;
216 type
= dn
->dn_bonustype
;
223 dmu_rm_spill(objset_t
*os
, uint64_t object
, dmu_tx_t
*tx
)
228 error
= dnode_hold(os
, object
, FTAG
, &dn
);
229 dbuf_rm_spill(dn
, tx
);
230 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
231 dnode_rm_spill(dn
, tx
);
232 rw_exit(&dn
->dn_struct_rwlock
);
233 dnode_rele(dn
, FTAG
);
238 * returns ENOENT, EIO, or 0.
241 dmu_bonus_hold(objset_t
*os
, uint64_t object
, void *tag
, dmu_buf_t
**dbp
)
247 error
= dnode_hold(os
, object
, FTAG
, &dn
);
251 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
252 if (dn
->dn_bonus
== NULL
) {
253 rw_exit(&dn
->dn_struct_rwlock
);
254 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
255 if (dn
->dn_bonus
== NULL
)
256 dbuf_create_bonus(dn
);
260 /* as long as the bonus buf is held, the dnode will be held */
261 if (refcount_add(&db
->db_holds
, tag
) == 1) {
262 VERIFY(dnode_add_ref(dn
, db
));
263 (void) atomic_inc_32_nv(&dn
->dn_dbufs_count
);
267 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's
268 * hold and incrementing the dbuf count to ensure that dnode_move() sees
269 * a dnode hold for every dbuf.
271 rw_exit(&dn
->dn_struct_rwlock
);
273 dnode_rele(dn
, FTAG
);
275 VERIFY(0 == dbuf_read(db
, NULL
, DB_RF_MUST_SUCCEED
| DB_RF_NOPREFETCH
));
282 * returns ENOENT, EIO, or 0.
284 * This interface will allocate a blank spill dbuf when a spill blk
285 * doesn't already exist on the dnode.
287 * if you only want to find an already existing spill db, then
288 * dmu_spill_hold_existing() should be used.
291 dmu_spill_hold_by_dnode(dnode_t
*dn
, uint32_t flags
, void *tag
, dmu_buf_t
**dbp
)
293 dmu_buf_impl_t
*db
= NULL
;
296 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
297 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
299 db
= dbuf_hold(dn
, DMU_SPILL_BLKID
, tag
);
301 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
302 rw_exit(&dn
->dn_struct_rwlock
);
305 err
= dbuf_read(db
, NULL
, flags
);
314 dmu_spill_hold_existing(dmu_buf_t
*bonus
, void *tag
, dmu_buf_t
**dbp
)
316 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)bonus
;
323 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_SA
) {
326 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
328 if (!dn
->dn_have_spill
) {
331 err
= dmu_spill_hold_by_dnode(dn
,
332 DB_RF_HAVESTRUCT
| DB_RF_CANFAIL
, tag
, dbp
);
335 rw_exit(&dn
->dn_struct_rwlock
);
343 dmu_spill_hold_by_bonus(dmu_buf_t
*bonus
, void *tag
, dmu_buf_t
**dbp
)
345 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)bonus
;
351 err
= dmu_spill_hold_by_dnode(dn
, DB_RF_CANFAIL
, tag
, dbp
);
358 * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
359 * to take a held dnode rather than <os, object> -- the lookup is wasteful,
360 * and can induce severe lock contention when writing to several files
361 * whose dnodes are in the same block.
364 dmu_buf_hold_array_by_dnode(dnode_t
*dn
, uint64_t offset
, uint64_t length
,
365 int read
, void *tag
, int *numbufsp
, dmu_buf_t
***dbpp
, uint32_t flags
)
367 dsl_pool_t
*dp
= NULL
;
369 uint64_t blkid
, nblks
, i
;
375 ASSERT(length
<= DMU_MAX_ACCESS
);
377 dbuf_flags
= DB_RF_CANFAIL
| DB_RF_NEVERWAIT
| DB_RF_HAVESTRUCT
;
378 if (flags
& DMU_READ_NO_PREFETCH
|| length
> zfetch_array_rd_sz
)
379 dbuf_flags
|= DB_RF_NOPREFETCH
;
381 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
382 if (dn
->dn_datablkshift
) {
383 int blkshift
= dn
->dn_datablkshift
;
384 nblks
= (P2ROUNDUP(offset
+length
, 1ULL<<blkshift
) -
385 P2ALIGN(offset
, 1ULL<<blkshift
)) >> blkshift
;
387 if (offset
+ length
> dn
->dn_datablksz
) {
388 zfs_panic_recover("zfs: accessing past end of object "
389 "%llx/%llx (size=%u access=%llu+%llu)",
390 (longlong_t
)dn
->dn_objset
->
391 os_dsl_dataset
->ds_object
,
392 (longlong_t
)dn
->dn_object
, dn
->dn_datablksz
,
393 (longlong_t
)offset
, (longlong_t
)length
);
394 rw_exit(&dn
->dn_struct_rwlock
);
399 dbp
= kmem_zalloc(sizeof (dmu_buf_t
*) * nblks
, KM_PUSHPAGE
| KM_NODEBUG
);
401 if (dn
->dn_objset
->os_dsl_dataset
)
402 dp
= dn
->dn_objset
->os_dsl_dataset
->ds_dir
->dd_pool
;
403 if (dp
&& dsl_pool_sync_context(dp
))
405 zio
= zio_root(dn
->dn_objset
->os_spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
406 blkid
= dbuf_whichblock(dn
, offset
);
407 for (i
= 0; i
< nblks
; i
++) {
408 dmu_buf_impl_t
*db
= dbuf_hold(dn
, blkid
+i
, tag
);
410 rw_exit(&dn
->dn_struct_rwlock
);
411 dmu_buf_rele_array(dbp
, nblks
, tag
);
415 /* initiate async i/o */
417 (void) dbuf_read(db
, zio
, dbuf_flags
);
421 rw_exit(&dn
->dn_struct_rwlock
);
423 /* wait for async i/o */
425 /* track read overhead when we are in sync context */
426 if (dp
&& dsl_pool_sync_context(dp
))
427 dp
->dp_read_overhead
+= gethrtime() - start
;
429 dmu_buf_rele_array(dbp
, nblks
, tag
);
433 /* wait for other io to complete */
435 for (i
= 0; i
< nblks
; i
++) {
436 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbp
[i
];
437 mutex_enter(&db
->db_mtx
);
438 while (db
->db_state
== DB_READ
||
439 db
->db_state
== DB_FILL
)
440 cv_wait(&db
->db_changed
, &db
->db_mtx
);
441 if (db
->db_state
== DB_UNCACHED
)
443 mutex_exit(&db
->db_mtx
);
445 dmu_buf_rele_array(dbp
, nblks
, tag
);
457 dmu_buf_hold_array(objset_t
*os
, uint64_t object
, uint64_t offset
,
458 uint64_t length
, int read
, void *tag
, int *numbufsp
, dmu_buf_t
***dbpp
)
463 err
= dnode_hold(os
, object
, FTAG
, &dn
);
467 err
= dmu_buf_hold_array_by_dnode(dn
, offset
, length
, read
, tag
,
468 numbufsp
, dbpp
, DMU_READ_PREFETCH
);
470 dnode_rele(dn
, FTAG
);
476 dmu_buf_hold_array_by_bonus(dmu_buf_t
*db_fake
, uint64_t offset
,
477 uint64_t length
, int read
, void *tag
, int *numbufsp
, dmu_buf_t
***dbpp
)
479 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
485 err
= dmu_buf_hold_array_by_dnode(dn
, offset
, length
, read
, tag
,
486 numbufsp
, dbpp
, DMU_READ_PREFETCH
);
493 dmu_buf_rele_array(dmu_buf_t
**dbp_fake
, int numbufs
, void *tag
)
496 dmu_buf_impl_t
**dbp
= (dmu_buf_impl_t
**)dbp_fake
;
501 for (i
= 0; i
< numbufs
; i
++) {
503 dbuf_rele(dbp
[i
], tag
);
506 kmem_free(dbp
, sizeof (dmu_buf_t
*) * numbufs
);
510 dmu_prefetch(objset_t
*os
, uint64_t object
, uint64_t offset
, uint64_t len
)
516 if (zfs_prefetch_disable
)
519 if (len
== 0) { /* they're interested in the bonus buffer */
520 dn
= DMU_META_DNODE(os
);
522 if (object
== 0 || object
>= DN_MAX_OBJECT
)
525 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
526 blkid
= dbuf_whichblock(dn
, object
* sizeof (dnode_phys_t
));
527 dbuf_prefetch(dn
, blkid
);
528 rw_exit(&dn
->dn_struct_rwlock
);
533 * XXX - Note, if the dnode for the requested object is not
534 * already cached, we will do a *synchronous* read in the
535 * dnode_hold() call. The same is true for any indirects.
537 err
= dnode_hold(os
, object
, FTAG
, &dn
);
541 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
542 if (dn
->dn_datablkshift
) {
543 int blkshift
= dn
->dn_datablkshift
;
544 nblks
= (P2ROUNDUP(offset
+len
, 1<<blkshift
) -
545 P2ALIGN(offset
, 1<<blkshift
)) >> blkshift
;
547 nblks
= (offset
< dn
->dn_datablksz
);
551 blkid
= dbuf_whichblock(dn
, offset
);
552 for (i
= 0; i
< nblks
; i
++)
553 dbuf_prefetch(dn
, blkid
+i
);
556 rw_exit(&dn
->dn_struct_rwlock
);
558 dnode_rele(dn
, FTAG
);
562 * Get the next "chunk" of file data to free. We traverse the file from
563 * the end so that the file gets shorter over time (if we crashes in the
564 * middle, this will leave us in a better state). We find allocated file
565 * data by simply searching the allocated level 1 indirects.
568 get_next_chunk(dnode_t
*dn
, uint64_t *start
, uint64_t limit
)
570 uint64_t len
= *start
- limit
;
572 uint64_t maxblks
= DMU_MAX_ACCESS
/ (1ULL << (dn
->dn_indblkshift
+ 1));
574 dn
->dn_datablksz
* EPB(dn
->dn_indblkshift
, SPA_BLKPTRSHIFT
);
576 ASSERT(limit
<= *start
);
578 if (len
<= iblkrange
* maxblks
) {
582 ASSERT(ISP2(iblkrange
));
584 while (*start
> limit
&& blkcnt
< maxblks
) {
587 /* find next allocated L1 indirect */
588 err
= dnode_next_offset(dn
,
589 DNODE_FIND_BACKWARDS
, start
, 2, 1, 0);
591 /* if there are no more, then we are done */
600 /* reset offset to end of "next" block back */
601 *start
= P2ALIGN(*start
, iblkrange
);
611 dmu_free_long_range_impl(objset_t
*os
, dnode_t
*dn
, uint64_t offset
,
612 uint64_t length
, boolean_t free_dnode
)
615 uint64_t object_size
, start
, end
, len
;
616 boolean_t trunc
= (length
== DMU_OBJECT_END
);
619 align
= 1 << dn
->dn_datablkshift
;
621 object_size
= align
== 1 ? dn
->dn_datablksz
:
622 (dn
->dn_maxblkid
+ 1) << dn
->dn_datablkshift
;
624 end
= offset
+ length
;
625 if (trunc
|| end
> object_size
)
629 length
= end
- offset
;
633 /* assert(offset <= start) */
634 err
= get_next_chunk(dn
, &start
, offset
);
637 len
= trunc
? DMU_OBJECT_END
: end
- start
;
639 tx
= dmu_tx_create(os
);
640 dmu_tx_hold_free(tx
, dn
->dn_object
, start
, len
);
641 err
= dmu_tx_assign(tx
, TXG_WAIT
);
647 dnode_free_range(dn
, start
, trunc
? -1 : len
, tx
);
649 if (start
== 0 && free_dnode
) {
654 length
-= end
- start
;
663 dmu_free_long_range(objset_t
*os
, uint64_t object
,
664 uint64_t offset
, uint64_t length
)
669 err
= dnode_hold(os
, object
, FTAG
, &dn
);
672 err
= dmu_free_long_range_impl(os
, dn
, offset
, length
, FALSE
);
673 dnode_rele(dn
, FTAG
);
678 dmu_free_object(objset_t
*os
, uint64_t object
)
684 err
= dnode_hold_impl(os
, object
, DNODE_MUST_BE_ALLOCATED
,
688 if (dn
->dn_nlevels
== 1) {
689 tx
= dmu_tx_create(os
);
690 dmu_tx_hold_bonus(tx
, object
);
691 dmu_tx_hold_free(tx
, dn
->dn_object
, 0, DMU_OBJECT_END
);
692 err
= dmu_tx_assign(tx
, TXG_WAIT
);
694 dnode_free_range(dn
, 0, DMU_OBJECT_END
, tx
);
701 err
= dmu_free_long_range_impl(os
, dn
, 0, DMU_OBJECT_END
, TRUE
);
703 dnode_rele(dn
, FTAG
);
708 dmu_free_range(objset_t
*os
, uint64_t object
, uint64_t offset
,
709 uint64_t size
, dmu_tx_t
*tx
)
712 int err
= dnode_hold(os
, object
, FTAG
, &dn
);
715 ASSERT(offset
< UINT64_MAX
);
716 ASSERT(size
== -1ULL || size
<= UINT64_MAX
- offset
);
717 dnode_free_range(dn
, offset
, size
, tx
);
718 dnode_rele(dn
, FTAG
);
723 dmu_read(objset_t
*os
, uint64_t object
, uint64_t offset
, uint64_t size
,
724 void *buf
, uint32_t flags
)
730 err
= dnode_hold(os
, object
, FTAG
, &dn
);
735 * Deal with odd block sizes, where there can't be data past the first
736 * block. If we ever do the tail block optimization, we will need to
737 * handle that here as well.
739 if (dn
->dn_maxblkid
== 0) {
740 int newsz
= offset
> dn
->dn_datablksz
? 0 :
741 MIN(size
, dn
->dn_datablksz
- offset
);
742 bzero((char *)buf
+ newsz
, size
- newsz
);
747 uint64_t mylen
= MIN(size
, DMU_MAX_ACCESS
/ 2);
751 * NB: we could do this block-at-a-time, but it's nice
752 * to be reading in parallel.
754 err
= dmu_buf_hold_array_by_dnode(dn
, offset
, mylen
,
755 TRUE
, FTAG
, &numbufs
, &dbp
, flags
);
759 for (i
= 0; i
< numbufs
; i
++) {
762 dmu_buf_t
*db
= dbp
[i
];
766 bufoff
= offset
- db
->db_offset
;
767 tocpy
= (int)MIN(db
->db_size
- bufoff
, size
);
769 bcopy((char *)db
->db_data
+ bufoff
, buf
, tocpy
);
773 buf
= (char *)buf
+ tocpy
;
775 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
777 dnode_rele(dn
, FTAG
);
782 dmu_write(objset_t
*os
, uint64_t object
, uint64_t offset
, uint64_t size
,
783 const void *buf
, dmu_tx_t
*tx
)
791 VERIFY(0 == dmu_buf_hold_array(os
, object
, offset
, size
,
792 FALSE
, FTAG
, &numbufs
, &dbp
));
794 for (i
= 0; i
< numbufs
; i
++) {
797 dmu_buf_t
*db
= dbp
[i
];
801 bufoff
= offset
- db
->db_offset
;
802 tocpy
= (int)MIN(db
->db_size
- bufoff
, size
);
804 ASSERT(i
== 0 || i
== numbufs
-1 || tocpy
== db
->db_size
);
806 if (tocpy
== db
->db_size
)
807 dmu_buf_will_fill(db
, tx
);
809 dmu_buf_will_dirty(db
, tx
);
811 (void) memcpy((char *)db
->db_data
+ bufoff
, buf
, tocpy
);
813 if (tocpy
== db
->db_size
)
814 dmu_buf_fill_done(db
, tx
);
818 buf
= (char *)buf
+ tocpy
;
820 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
824 dmu_prealloc(objset_t
*os
, uint64_t object
, uint64_t offset
, uint64_t size
,
833 VERIFY(0 == dmu_buf_hold_array(os
, object
, offset
, size
,
834 FALSE
, FTAG
, &numbufs
, &dbp
));
836 for (i
= 0; i
< numbufs
; i
++) {
837 dmu_buf_t
*db
= dbp
[i
];
839 dmu_buf_will_not_fill(db
, tx
);
841 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
845 * DMU support for xuio
847 kstat_t
*xuio_ksp
= NULL
;
849 typedef struct xuio_stats
{
850 /* loaned yet not returned arc_buf */
851 kstat_named_t xuiostat_onloan_rbuf
;
852 kstat_named_t xuiostat_onloan_wbuf
;
853 /* whether a copy is made when loaning out a read buffer */
854 kstat_named_t xuiostat_rbuf_copied
;
855 kstat_named_t xuiostat_rbuf_nocopy
;
856 /* whether a copy is made when assigning a write buffer */
857 kstat_named_t xuiostat_wbuf_copied
;
858 kstat_named_t xuiostat_wbuf_nocopy
;
861 static xuio_stats_t xuio_stats
= {
862 { "onloan_read_buf", KSTAT_DATA_UINT64
},
863 { "onloan_write_buf", KSTAT_DATA_UINT64
},
864 { "read_buf_copied", KSTAT_DATA_UINT64
},
865 { "read_buf_nocopy", KSTAT_DATA_UINT64
},
866 { "write_buf_copied", KSTAT_DATA_UINT64
},
867 { "write_buf_nocopy", KSTAT_DATA_UINT64
}
870 #define XUIOSTAT_INCR(stat, val) \
871 atomic_add_64(&xuio_stats.stat.value.ui64, (val))
872 #define XUIOSTAT_BUMP(stat) XUIOSTAT_INCR(stat, 1)
875 dmu_xuio_init(xuio_t
*xuio
, int nblk
)
878 uio_t
*uio
= &xuio
->xu_uio
;
880 uio
->uio_iovcnt
= nblk
;
881 uio
->uio_iov
= kmem_zalloc(nblk
* sizeof (iovec_t
), KM_PUSHPAGE
);
883 priv
= kmem_zalloc(sizeof (dmu_xuio_t
), KM_PUSHPAGE
);
885 priv
->bufs
= kmem_zalloc(nblk
* sizeof (arc_buf_t
*), KM_PUSHPAGE
);
886 priv
->iovp
= uio
->uio_iov
;
887 XUIO_XUZC_PRIV(xuio
) = priv
;
889 if (XUIO_XUZC_RW(xuio
) == UIO_READ
)
890 XUIOSTAT_INCR(xuiostat_onloan_rbuf
, nblk
);
892 XUIOSTAT_INCR(xuiostat_onloan_wbuf
, nblk
);
898 dmu_xuio_fini(xuio_t
*xuio
)
900 dmu_xuio_t
*priv
= XUIO_XUZC_PRIV(xuio
);
901 int nblk
= priv
->cnt
;
903 kmem_free(priv
->iovp
, nblk
* sizeof (iovec_t
));
904 kmem_free(priv
->bufs
, nblk
* sizeof (arc_buf_t
*));
905 kmem_free(priv
, sizeof (dmu_xuio_t
));
907 if (XUIO_XUZC_RW(xuio
) == UIO_READ
)
908 XUIOSTAT_INCR(xuiostat_onloan_rbuf
, -nblk
);
910 XUIOSTAT_INCR(xuiostat_onloan_wbuf
, -nblk
);
914 * Initialize iov[priv->next] and priv->bufs[priv->next] with { off, n, abuf }
915 * and increase priv->next by 1.
918 dmu_xuio_add(xuio_t
*xuio
, arc_buf_t
*abuf
, offset_t off
, size_t n
)
921 uio_t
*uio
= &xuio
->xu_uio
;
922 dmu_xuio_t
*priv
= XUIO_XUZC_PRIV(xuio
);
923 int i
= priv
->next
++;
925 ASSERT(i
< priv
->cnt
);
926 ASSERT(off
+ n
<= arc_buf_size(abuf
));
927 iov
= uio
->uio_iov
+ i
;
928 iov
->iov_base
= (char *)abuf
->b_data
+ off
;
930 priv
->bufs
[i
] = abuf
;
935 dmu_xuio_cnt(xuio_t
*xuio
)
937 dmu_xuio_t
*priv
= XUIO_XUZC_PRIV(xuio
);
942 dmu_xuio_arcbuf(xuio_t
*xuio
, int i
)
944 dmu_xuio_t
*priv
= XUIO_XUZC_PRIV(xuio
);
946 ASSERT(i
< priv
->cnt
);
947 return (priv
->bufs
[i
]);
951 dmu_xuio_clear(xuio_t
*xuio
, int i
)
953 dmu_xuio_t
*priv
= XUIO_XUZC_PRIV(xuio
);
955 ASSERT(i
< priv
->cnt
);
956 priv
->bufs
[i
] = NULL
;
962 xuio_ksp
= kstat_create("zfs", 0, "xuio_stats", "misc",
963 KSTAT_TYPE_NAMED
, sizeof (xuio_stats
) / sizeof (kstat_named_t
),
965 if (xuio_ksp
!= NULL
) {
966 xuio_ksp
->ks_data
= &xuio_stats
;
967 kstat_install(xuio_ksp
);
974 if (xuio_ksp
!= NULL
) {
975 kstat_delete(xuio_ksp
);
981 xuio_stat_wbuf_copied()
983 XUIOSTAT_BUMP(xuiostat_wbuf_copied
);
987 xuio_stat_wbuf_nocopy()
989 XUIOSTAT_BUMP(xuiostat_wbuf_nocopy
);
995 * Copy up to size bytes between arg_buf and req based on the data direction
996 * described by the req. If an entire req's data cannot be transfered the
997 * req's is updated such that it's current index and bv offsets correctly
998 * reference any residual data which could not be copied. The return value
999 * is the number of bytes successfully copied to arg_buf.
1002 dmu_req_copy(void *arg_buf
, int size
, int *offset
, struct request
*req
)
1005 struct req_iterator iter
;
1010 rq_for_each_segment(bv
, req
, iter
) {
1012 /* Fully consumed the passed arg_buf */
1013 ASSERT3S(*offset
, <=, size
);
1014 if (size
== *offset
)
1017 /* Skip fully consumed bv's */
1018 if (bv
->bv_len
== 0)
1021 tocpy
= MIN(bv
->bv_len
, size
- *offset
);
1022 ASSERT3S(tocpy
, >=, 0);
1024 bv_buf
= page_address(bv
->bv_page
) + bv
->bv_offset
;
1025 ASSERT3P(bv_buf
, !=, NULL
);
1027 if (rq_data_dir(req
) == WRITE
)
1028 memcpy(arg_buf
+ *offset
, bv_buf
, tocpy
);
1030 memcpy(bv_buf
, arg_buf
+ *offset
, tocpy
);
1033 bv
->bv_offset
+= tocpy
;
1034 bv
->bv_len
-= tocpy
;
1041 dmu_bio_put(struct bio
*bio
)
1043 struct bio
*bio_next
;
1046 bio_next
= bio
->bi_next
;
1053 dmu_bio_clone(struct bio
*bio
, struct bio
**bio_copy
)
1055 struct bio
*bio_root
= NULL
;
1056 struct bio
*bio_last
= NULL
;
1057 struct bio
*bio_new
;
1063 bio_new
= bio_clone(bio
, GFP_NOIO
);
1064 if (bio_new
== NULL
) {
1065 dmu_bio_put(bio_root
);
1070 bio_last
->bi_next
= bio_new
;
1080 *bio_copy
= bio_root
;
1086 dmu_read_req(objset_t
*os
, uint64_t object
, struct request
*req
)
1088 uint64_t size
= blk_rq_bytes(req
);
1089 uint64_t offset
= blk_rq_pos(req
) << 9;
1090 struct bio
*bio_saved
= req
->bio
;
1092 int numbufs
, i
, err
;
1095 * NB: we could do this block-at-a-time, but it's nice
1096 * to be reading in parallel.
1098 err
= dmu_buf_hold_array(os
, object
, offset
, size
, TRUE
, FTAG
,
1104 * Clone the bio list so the bv->bv_offset and bv->bv_len members
1105 * can be safely modified. The original bio list is relinked in to
1106 * the request when the function exits. This is required because
1107 * some file systems blindly assume that these values will remain
1108 * constant between bio_submit() and the IO completion callback.
1110 err
= dmu_bio_clone(bio_saved
, &req
->bio
);
1114 for (i
= 0; i
< numbufs
; i
++) {
1115 int tocpy
, didcpy
, bufoff
;
1116 dmu_buf_t
*db
= dbp
[i
];
1118 bufoff
= offset
- db
->db_offset
;
1119 ASSERT3S(bufoff
, >=, 0);
1121 tocpy
= (int)MIN(db
->db_size
- bufoff
, size
);
1125 err
= dmu_req_copy(db
->db_data
+ bufoff
, tocpy
, &didcpy
, req
);
1138 dmu_bio_put(req
->bio
);
1139 req
->bio
= bio_saved
;
1141 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
1147 dmu_write_req(objset_t
*os
, uint64_t object
, struct request
*req
, dmu_tx_t
*tx
)
1149 uint64_t size
= blk_rq_bytes(req
);
1150 uint64_t offset
= blk_rq_pos(req
) << 9;
1151 struct bio
*bio_saved
= req
->bio
;
1160 err
= dmu_buf_hold_array(os
, object
, offset
, size
, FALSE
, FTAG
,
1166 * Clone the bio list so the bv->bv_offset and bv->bv_len members
1167 * can be safely modified. The original bio list is relinked in to
1168 * the request when the function exits. This is required because
1169 * some file systems blindly assume that these values will remain
1170 * constant between bio_submit() and the IO completion callback.
1172 err
= dmu_bio_clone(bio_saved
, &req
->bio
);
1176 for (i
= 0; i
< numbufs
; i
++) {
1177 int tocpy
, didcpy
, bufoff
;
1178 dmu_buf_t
*db
= dbp
[i
];
1180 bufoff
= offset
- db
->db_offset
;
1181 ASSERT3S(bufoff
, >=, 0);
1183 tocpy
= (int)MIN(db
->db_size
- bufoff
, size
);
1187 ASSERT(i
== 0 || i
== numbufs
-1 || tocpy
== db
->db_size
);
1189 if (tocpy
== db
->db_size
)
1190 dmu_buf_will_fill(db
, tx
);
1192 dmu_buf_will_dirty(db
, tx
);
1194 err
= dmu_req_copy(db
->db_data
+ bufoff
, tocpy
, &didcpy
, req
);
1196 if (tocpy
== db
->db_size
)
1197 dmu_buf_fill_done(db
, tx
);
1210 dmu_bio_put(req
->bio
);
1211 req
->bio
= bio_saved
;
1213 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
1219 dmu_read_uio(objset_t
*os
, uint64_t object
, uio_t
*uio
, uint64_t size
)
1222 int numbufs
, i
, err
;
1223 xuio_t
*xuio
= NULL
;
1226 * NB: we could do this block-at-a-time, but it's nice
1227 * to be reading in parallel.
1229 err
= dmu_buf_hold_array(os
, object
, uio
->uio_loffset
, size
, TRUE
, FTAG
,
1234 for (i
= 0; i
< numbufs
; i
++) {
1237 dmu_buf_t
*db
= dbp
[i
];
1241 bufoff
= uio
->uio_loffset
- db
->db_offset
;
1242 tocpy
= (int)MIN(db
->db_size
- bufoff
, size
);
1245 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
1246 arc_buf_t
*dbuf_abuf
= dbi
->db_buf
;
1247 arc_buf_t
*abuf
= dbuf_loan_arcbuf(dbi
);
1248 err
= dmu_xuio_add(xuio
, abuf
, bufoff
, tocpy
);
1250 uio
->uio_resid
-= tocpy
;
1251 uio
->uio_loffset
+= tocpy
;
1254 if (abuf
== dbuf_abuf
)
1255 XUIOSTAT_BUMP(xuiostat_rbuf_nocopy
);
1257 XUIOSTAT_BUMP(xuiostat_rbuf_copied
);
1259 err
= uiomove((char *)db
->db_data
+ bufoff
, tocpy
,
1267 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
1273 dmu_write_uio_dnode(dnode_t
*dn
, uio_t
*uio
, uint64_t size
, dmu_tx_t
*tx
)
1280 err
= dmu_buf_hold_array_by_dnode(dn
, uio
->uio_loffset
, size
,
1281 FALSE
, FTAG
, &numbufs
, &dbp
, DMU_READ_PREFETCH
);
1285 for (i
= 0; i
< numbufs
; i
++) {
1288 dmu_buf_t
*db
= dbp
[i
];
1292 bufoff
= uio
->uio_loffset
- db
->db_offset
;
1293 tocpy
= (int)MIN(db
->db_size
- bufoff
, size
);
1295 ASSERT(i
== 0 || i
== numbufs
-1 || tocpy
== db
->db_size
);
1297 if (tocpy
== db
->db_size
)
1298 dmu_buf_will_fill(db
, tx
);
1300 dmu_buf_will_dirty(db
, tx
);
1303 * XXX uiomove could block forever (eg.nfs-backed
1304 * pages). There needs to be a uiolockdown() function
1305 * to lock the pages in memory, so that uiomove won't
1308 err
= uiomove((char *)db
->db_data
+ bufoff
, tocpy
,
1311 if (tocpy
== db
->db_size
)
1312 dmu_buf_fill_done(db
, tx
);
1320 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
1325 dmu_write_uio_dbuf(dmu_buf_t
*zdb
, uio_t
*uio
, uint64_t size
,
1328 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)zdb
;
1337 err
= dmu_write_uio_dnode(dn
, uio
, size
, tx
);
1344 dmu_write_uio(objset_t
*os
, uint64_t object
, uio_t
*uio
, uint64_t size
,
1353 err
= dnode_hold(os
, object
, FTAG
, &dn
);
1357 err
= dmu_write_uio_dnode(dn
, uio
, size
, tx
);
1359 dnode_rele(dn
, FTAG
);
1363 #endif /* _KERNEL */
1366 * Allocate a loaned anonymous arc buffer.
1369 dmu_request_arcbuf(dmu_buf_t
*handle
, int size
)
1371 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)handle
;
1374 DB_GET_SPA(&spa
, db
);
1375 return (arc_loan_buf(spa
, size
));
1379 * Free a loaned arc buffer.
1382 dmu_return_arcbuf(arc_buf_t
*buf
)
1384 arc_return_buf(buf
, FTAG
);
1385 VERIFY(arc_buf_remove_ref(buf
, FTAG
));
1389 * When possible directly assign passed loaned arc buffer to a dbuf.
1390 * If this is not possible copy the contents of passed arc buf via
1394 dmu_assign_arcbuf(dmu_buf_t
*handle
, uint64_t offset
, arc_buf_t
*buf
,
1397 dmu_buf_impl_t
*dbuf
= (dmu_buf_impl_t
*)handle
;
1400 uint32_t blksz
= (uint32_t)arc_buf_size(buf
);
1403 DB_DNODE_ENTER(dbuf
);
1404 dn
= DB_DNODE(dbuf
);
1405 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1406 blkid
= dbuf_whichblock(dn
, offset
);
1407 VERIFY((db
= dbuf_hold(dn
, blkid
, FTAG
)) != NULL
);
1408 rw_exit(&dn
->dn_struct_rwlock
);
1409 DB_DNODE_EXIT(dbuf
);
1411 if (offset
== db
->db
.db_offset
&& blksz
== db
->db
.db_size
) {
1412 dbuf_assign_arcbuf(db
, buf
, tx
);
1413 dbuf_rele(db
, FTAG
);
1418 DB_DNODE_ENTER(dbuf
);
1419 dn
= DB_DNODE(dbuf
);
1421 object
= dn
->dn_object
;
1422 DB_DNODE_EXIT(dbuf
);
1424 dbuf_rele(db
, FTAG
);
1425 dmu_write(os
, object
, offset
, blksz
, buf
->b_data
, tx
);
1426 dmu_return_arcbuf(buf
);
1427 XUIOSTAT_BUMP(xuiostat_wbuf_copied
);
1432 dbuf_dirty_record_t
*dsa_dr
;
1433 dmu_sync_cb_t
*dsa_done
;
1440 dmu_sync_ready(zio_t
*zio
, arc_buf_t
*buf
, void *varg
)
1442 dmu_sync_arg_t
*dsa
= varg
;
1443 dmu_buf_t
*db
= dsa
->dsa_zgd
->zgd_db
;
1444 blkptr_t
*bp
= zio
->io_bp
;
1446 if (zio
->io_error
== 0) {
1447 if (BP_IS_HOLE(bp
)) {
1449 * A block of zeros may compress to a hole, but the
1450 * block size still needs to be known for replay.
1452 BP_SET_LSIZE(bp
, db
->db_size
);
1454 ASSERT(BP_GET_LEVEL(bp
) == 0);
1461 dmu_sync_late_arrival_ready(zio_t
*zio
)
1463 dmu_sync_ready(zio
, NULL
, zio
->io_private
);
1468 dmu_sync_done(zio_t
*zio
, arc_buf_t
*buf
, void *varg
)
1470 dmu_sync_arg_t
*dsa
= varg
;
1471 dbuf_dirty_record_t
*dr
= dsa
->dsa_dr
;
1472 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1474 mutex_enter(&db
->db_mtx
);
1475 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
);
1476 if (zio
->io_error
== 0) {
1477 dr
->dt
.dl
.dr_overridden_by
= *zio
->io_bp
;
1478 dr
->dt
.dl
.dr_override_state
= DR_OVERRIDDEN
;
1479 dr
->dt
.dl
.dr_copies
= zio
->io_prop
.zp_copies
;
1480 if (BP_IS_HOLE(&dr
->dt
.dl
.dr_overridden_by
))
1481 BP_ZERO(&dr
->dt
.dl
.dr_overridden_by
);
1483 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1485 cv_broadcast(&db
->db_changed
);
1486 mutex_exit(&db
->db_mtx
);
1488 dsa
->dsa_done(dsa
->dsa_zgd
, zio
->io_error
);
1490 kmem_free(dsa
, sizeof (*dsa
));
1494 dmu_sync_late_arrival_done(zio_t
*zio
)
1496 blkptr_t
*bp
= zio
->io_bp
;
1497 dmu_sync_arg_t
*dsa
= zio
->io_private
;
1499 if (zio
->io_error
== 0 && !BP_IS_HOLE(bp
)) {
1500 ASSERT(zio
->io_bp
->blk_birth
== zio
->io_txg
);
1501 ASSERT(zio
->io_txg
> spa_syncing_txg(zio
->io_spa
));
1502 zio_free(zio
->io_spa
, zio
->io_txg
, zio
->io_bp
);
1505 dmu_tx_commit(dsa
->dsa_tx
);
1507 dsa
->dsa_done(dsa
->dsa_zgd
, zio
->io_error
);
1509 kmem_free(dsa
, sizeof (*dsa
));
1513 dmu_sync_late_arrival(zio_t
*pio
, objset_t
*os
, dmu_sync_cb_t
*done
, zgd_t
*zgd
,
1514 zio_prop_t
*zp
, zbookmark_t
*zb
)
1516 dmu_sync_arg_t
*dsa
;
1519 tx
= dmu_tx_create(os
);
1520 dmu_tx_hold_space(tx
, zgd
->zgd_db
->db_size
);
1521 if (dmu_tx_assign(tx
, TXG_WAIT
) != 0) {
1523 return (EIO
); /* Make zl_get_data do txg_waited_synced() */
1526 dsa
= kmem_alloc(sizeof (dmu_sync_arg_t
), KM_PUSHPAGE
);
1528 dsa
->dsa_done
= done
;
1532 zio_nowait(zio_write(pio
, os
->os_spa
, dmu_tx_get_txg(tx
), zgd
->zgd_bp
,
1533 zgd
->zgd_db
->db_data
, zgd
->zgd_db
->db_size
, zp
,
1534 dmu_sync_late_arrival_ready
, dmu_sync_late_arrival_done
, dsa
,
1535 ZIO_PRIORITY_SYNC_WRITE
, ZIO_FLAG_CANFAIL
| ZIO_FLAG_FASTWRITE
, zb
));
1541 * Intent log support: sync the block associated with db to disk.
1542 * N.B. and XXX: the caller is responsible for making sure that the
1543 * data isn't changing while dmu_sync() is writing it.
1547 * EEXIST: this txg has already been synced, so there's nothing to to.
1548 * The caller should not log the write.
1550 * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
1551 * The caller should not log the write.
1553 * EALREADY: this block is already in the process of being synced.
1554 * The caller should track its progress (somehow).
1556 * EIO: could not do the I/O.
1557 * The caller should do a txg_wait_synced().
1559 * 0: the I/O has been initiated.
1560 * The caller should log this blkptr in the done callback.
1561 * It is possible that the I/O will fail, in which case
1562 * the error will be reported to the done callback and
1563 * propagated to pio from zio_done().
1566 dmu_sync(zio_t
*pio
, uint64_t txg
, dmu_sync_cb_t
*done
, zgd_t
*zgd
)
1568 blkptr_t
*bp
= zgd
->zgd_bp
;
1569 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)zgd
->zgd_db
;
1570 objset_t
*os
= db
->db_objset
;
1571 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
1572 dbuf_dirty_record_t
*dr
;
1573 dmu_sync_arg_t
*dsa
;
1578 ASSERT(pio
!= NULL
);
1579 ASSERT(BP_IS_HOLE(bp
));
1582 SET_BOOKMARK(&zb
, ds
->ds_object
,
1583 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
1587 dmu_write_policy(os
, dn
, db
->db_level
, WP_DMU_SYNC
, &zp
);
1591 * If we're frozen (running ziltest), we always need to generate a bp.
1593 if (txg
> spa_freeze_txg(os
->os_spa
))
1594 return (dmu_sync_late_arrival(pio
, os
, done
, zgd
, &zp
, &zb
));
1597 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
1598 * and us. If we determine that this txg is not yet syncing,
1599 * but it begins to sync a moment later, that's OK because the
1600 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
1602 mutex_enter(&db
->db_mtx
);
1604 if (txg
<= spa_last_synced_txg(os
->os_spa
)) {
1606 * This txg has already synced. There's nothing to do.
1608 mutex_exit(&db
->db_mtx
);
1612 if (txg
<= spa_syncing_txg(os
->os_spa
)) {
1614 * This txg is currently syncing, so we can't mess with
1615 * the dirty record anymore; just write a new log block.
1617 mutex_exit(&db
->db_mtx
);
1618 return (dmu_sync_late_arrival(pio
, os
, done
, zgd
, &zp
, &zb
));
1621 dr
= db
->db_last_dirty
;
1622 while (dr
&& dr
->dr_txg
!= txg
)
1627 * There's no dr for this dbuf, so it must have been freed.
1628 * There's no need to log writes to freed blocks, so we're done.
1630 mutex_exit(&db
->db_mtx
);
1634 ASSERT(dr
->dr_txg
== txg
);
1635 if (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
||
1636 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
1638 * We have already issued a sync write for this buffer,
1639 * or this buffer has already been synced. It could not
1640 * have been dirtied since, or we would have cleared the state.
1642 mutex_exit(&db
->db_mtx
);
1646 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
1647 dr
->dt
.dl
.dr_override_state
= DR_IN_DMU_SYNC
;
1648 mutex_exit(&db
->db_mtx
);
1650 dsa
= kmem_alloc(sizeof (dmu_sync_arg_t
), KM_PUSHPAGE
);
1652 dsa
->dsa_done
= done
;
1656 zio_nowait(arc_write(pio
, os
->os_spa
, txg
,
1657 bp
, dr
->dt
.dl
.dr_data
, DBUF_IS_L2CACHEABLE(db
),
1658 DBUF_IS_L2COMPRESSIBLE(db
), &zp
, dmu_sync_ready
, dmu_sync_done
,
1659 dsa
, ZIO_PRIORITY_SYNC_WRITE
, ZIO_FLAG_CANFAIL
| ZIO_FLAG_FASTWRITE
, &zb
));
1665 dmu_object_set_blocksize(objset_t
*os
, uint64_t object
, uint64_t size
, int ibs
,
1671 err
= dnode_hold(os
, object
, FTAG
, &dn
);
1674 err
= dnode_set_blksz(dn
, size
, ibs
, tx
);
1675 dnode_rele(dn
, FTAG
);
1680 dmu_object_set_checksum(objset_t
*os
, uint64_t object
, uint8_t checksum
,
1685 /* XXX assumes dnode_hold will not get an i/o error */
1686 (void) dnode_hold(os
, object
, FTAG
, &dn
);
1687 ASSERT(checksum
< ZIO_CHECKSUM_FUNCTIONS
);
1688 dn
->dn_checksum
= checksum
;
1689 dnode_setdirty(dn
, tx
);
1690 dnode_rele(dn
, FTAG
);
1694 dmu_object_set_compress(objset_t
*os
, uint64_t object
, uint8_t compress
,
1699 /* XXX assumes dnode_hold will not get an i/o error */
1700 (void) dnode_hold(os
, object
, FTAG
, &dn
);
1701 ASSERT(compress
< ZIO_COMPRESS_FUNCTIONS
);
1702 dn
->dn_compress
= compress
;
1703 dnode_setdirty(dn
, tx
);
1704 dnode_rele(dn
, FTAG
);
1707 int zfs_mdcomp_disable
= 0;
1710 dmu_write_policy(objset_t
*os
, dnode_t
*dn
, int level
, int wp
, zio_prop_t
*zp
)
1712 dmu_object_type_t type
= dn
? dn
->dn_type
: DMU_OT_OBJSET
;
1713 boolean_t ismd
= (level
> 0 || DMU_OT_IS_METADATA(type
) ||
1715 enum zio_checksum checksum
= os
->os_checksum
;
1716 enum zio_compress compress
= os
->os_compress
;
1717 enum zio_checksum dedup_checksum
= os
->os_dedup_checksum
;
1719 boolean_t dedup_verify
= os
->os_dedup_verify
;
1720 int copies
= os
->os_copies
;
1723 * Determine checksum setting.
1727 * Metadata always gets checksummed. If the data
1728 * checksum is multi-bit correctable, and it's not a
1729 * ZBT-style checksum, then it's suitable for metadata
1730 * as well. Otherwise, the metadata checksum defaults
1733 if (zio_checksum_table
[checksum
].ci_correctable
< 1 ||
1734 zio_checksum_table
[checksum
].ci_eck
)
1735 checksum
= ZIO_CHECKSUM_FLETCHER_4
;
1737 checksum
= zio_checksum_select(dn
->dn_checksum
, checksum
);
1741 * Determine compression setting.
1745 * XXX -- we should design a compression algorithm
1746 * that specializes in arrays of bps.
1748 compress
= zfs_mdcomp_disable
? ZIO_COMPRESS_EMPTY
:
1751 compress
= zio_compress_select(dn
->dn_compress
, compress
);
1755 * Determine dedup setting. If we are in dmu_sync(), we won't
1756 * actually dedup now because that's all done in syncing context;
1757 * but we do want to use the dedup checkum. If the checksum is not
1758 * strong enough to ensure unique signatures, force dedup_verify.
1760 dedup
= (!ismd
&& dedup_checksum
!= ZIO_CHECKSUM_OFF
);
1762 checksum
= dedup_checksum
;
1763 if (!zio_checksum_table
[checksum
].ci_dedup
)
1767 if (wp
& WP_DMU_SYNC
)
1770 if (wp
& WP_NOFILL
) {
1771 ASSERT(!ismd
&& level
== 0);
1772 checksum
= ZIO_CHECKSUM_OFF
;
1773 compress
= ZIO_COMPRESS_OFF
;
1777 zp
->zp_checksum
= checksum
;
1778 zp
->zp_compress
= compress
;
1779 zp
->zp_type
= (wp
& WP_SPILL
) ? dn
->dn_bonustype
: type
;
1780 zp
->zp_level
= level
;
1781 zp
->zp_copies
= MIN(copies
+ ismd
, spa_max_replication(os
->os_spa
));
1782 zp
->zp_dedup
= dedup
;
1783 zp
->zp_dedup_verify
= dedup
&& dedup_verify
;
1787 dmu_offset_next(objset_t
*os
, uint64_t object
, boolean_t hole
, uint64_t *off
)
1792 err
= dnode_hold(os
, object
, FTAG
, &dn
);
1796 * Sync any current changes before
1797 * we go trundling through the block pointers.
1799 for (i
= 0; i
< TXG_SIZE
; i
++) {
1800 if (list_link_active(&dn
->dn_dirty_link
[i
]))
1803 if (i
!= TXG_SIZE
) {
1804 dnode_rele(dn
, FTAG
);
1805 txg_wait_synced(dmu_objset_pool(os
), 0);
1806 err
= dnode_hold(os
, object
, FTAG
, &dn
);
1811 err
= dnode_next_offset(dn
, (hole
? DNODE_FIND_HOLE
: 0), off
, 1, 1, 0);
1812 dnode_rele(dn
, FTAG
);
1818 dmu_object_info_from_dnode(dnode_t
*dn
, dmu_object_info_t
*doi
)
1823 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1824 mutex_enter(&dn
->dn_mtx
);
1828 doi
->doi_data_block_size
= dn
->dn_datablksz
;
1829 doi
->doi_metadata_block_size
= dn
->dn_indblkshift
?
1830 1ULL << dn
->dn_indblkshift
: 0;
1831 doi
->doi_type
= dn
->dn_type
;
1832 doi
->doi_bonus_type
= dn
->dn_bonustype
;
1833 doi
->doi_bonus_size
= dn
->dn_bonuslen
;
1834 doi
->doi_indirection
= dn
->dn_nlevels
;
1835 doi
->doi_checksum
= dn
->dn_checksum
;
1836 doi
->doi_compress
= dn
->dn_compress
;
1837 doi
->doi_physical_blocks_512
= (DN_USED_BYTES(dnp
) + 256) >> 9;
1838 doi
->doi_max_offset
= (dnp
->dn_maxblkid
+ 1) * dn
->dn_datablksz
;
1839 doi
->doi_fill_count
= 0;
1840 for (i
= 0; i
< dnp
->dn_nblkptr
; i
++)
1841 doi
->doi_fill_count
+= dnp
->dn_blkptr
[i
].blk_fill
;
1843 mutex_exit(&dn
->dn_mtx
);
1844 rw_exit(&dn
->dn_struct_rwlock
);
1848 * Get information on a DMU object.
1849 * If doi is NULL, just indicates whether the object exists.
1852 dmu_object_info(objset_t
*os
, uint64_t object
, dmu_object_info_t
*doi
)
1855 int err
= dnode_hold(os
, object
, FTAG
, &dn
);
1861 dmu_object_info_from_dnode(dn
, doi
);
1863 dnode_rele(dn
, FTAG
);
1868 * As above, but faster; can be used when you have a held dbuf in hand.
1871 dmu_object_info_from_db(dmu_buf_t
*db_fake
, dmu_object_info_t
*doi
)
1873 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1876 dmu_object_info_from_dnode(DB_DNODE(db
), doi
);
1881 * Faster still when you only care about the size.
1882 * This is specifically optimized for zfs_getattr().
1885 dmu_object_size_from_db(dmu_buf_t
*db_fake
, uint32_t *blksize
,
1886 u_longlong_t
*nblk512
)
1888 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
1894 *blksize
= dn
->dn_datablksz
;
1895 /* add 1 for dnode space */
1896 *nblk512
= ((DN_USED_BYTES(dn
->dn_phys
) + SPA_MINBLOCKSIZE
/2) >>
1897 SPA_MINBLOCKSHIFT
) + 1;
1902 byteswap_uint64_array(void *vbuf
, size_t size
)
1904 uint64_t *buf
= vbuf
;
1905 size_t count
= size
>> 3;
1908 ASSERT((size
& 7) == 0);
1910 for (i
= 0; i
< count
; i
++)
1911 buf
[i
] = BSWAP_64(buf
[i
]);
1915 byteswap_uint32_array(void *vbuf
, size_t size
)
1917 uint32_t *buf
= vbuf
;
1918 size_t count
= size
>> 2;
1921 ASSERT((size
& 3) == 0);
1923 for (i
= 0; i
< count
; i
++)
1924 buf
[i
] = BSWAP_32(buf
[i
]);
1928 byteswap_uint16_array(void *vbuf
, size_t size
)
1930 uint16_t *buf
= vbuf
;
1931 size_t count
= size
>> 1;
1934 ASSERT((size
& 1) == 0);
1936 for (i
= 0; i
< count
; i
++)
1937 buf
[i
] = BSWAP_16(buf
[i
]);
1942 byteswap_uint8_array(void *vbuf
, size_t size
)
1976 #if defined(_KERNEL) && defined(HAVE_SPL)
1977 EXPORT_SYMBOL(dmu_bonus_hold
);
1978 EXPORT_SYMBOL(dmu_buf_hold_array_by_bonus
);
1979 EXPORT_SYMBOL(dmu_buf_rele_array
);
1980 EXPORT_SYMBOL(dmu_prefetch
);
1981 EXPORT_SYMBOL(dmu_free_range
);
1982 EXPORT_SYMBOL(dmu_free_long_range
);
1983 EXPORT_SYMBOL(dmu_free_object
);
1984 EXPORT_SYMBOL(dmu_read
);
1985 EXPORT_SYMBOL(dmu_write
);
1986 EXPORT_SYMBOL(dmu_prealloc
);
1987 EXPORT_SYMBOL(dmu_object_info
);
1988 EXPORT_SYMBOL(dmu_object_info_from_dnode
);
1989 EXPORT_SYMBOL(dmu_object_info_from_db
);
1990 EXPORT_SYMBOL(dmu_object_size_from_db
);
1991 EXPORT_SYMBOL(dmu_object_set_blocksize
);
1992 EXPORT_SYMBOL(dmu_object_set_checksum
);
1993 EXPORT_SYMBOL(dmu_object_set_compress
);
1994 EXPORT_SYMBOL(dmu_write_policy
);
1995 EXPORT_SYMBOL(dmu_sync
);
1996 EXPORT_SYMBOL(dmu_request_arcbuf
);
1997 EXPORT_SYMBOL(dmu_return_arcbuf
);
1998 EXPORT_SYMBOL(dmu_assign_arcbuf
);
1999 EXPORT_SYMBOL(dmu_buf_hold
);
2000 EXPORT_SYMBOL(dmu_ot
);
2002 module_param(zfs_mdcomp_disable
, int, 0644);
2003 MODULE_PARM_DESC(zfs_mdcomp_disable
, "Disable meta data compression");