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.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright 2019 Joyent, Inc.
27 * Copyright 2016 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
29 * Copyright (c) 2018 DilOS
33 #include <sys/dmu_impl.h>
34 #include <sys/dmu_tx.h>
36 #include <sys/dnode.h>
37 #include <sys/zfs_context.h>
38 #include <sys/dmu_objset.h>
39 #include <sys/dmu_traverse.h>
40 #include <sys/dsl_dataset.h>
41 #include <sys/dsl_dir.h>
42 #include <sys/dsl_pool.h>
43 #include <sys/dsl_synctask.h>
44 #include <sys/dsl_prop.h>
45 #include <sys/dmu_zfetch.h>
46 #include <sys/zfs_ioctl.h>
48 #include <sys/zio_checksum.h>
49 #include <sys/zio_compress.h>
51 #include <sys/zfeature.h>
54 #include <sys/vmsystm.h>
55 #include <sys/zfs_znode.h>
58 static xuio_stats_t xuio_stats
= {
59 { "onloan_read_buf", KSTAT_DATA_UINT64
},
60 { "onloan_write_buf", KSTAT_DATA_UINT64
},
61 { "read_buf_copied", KSTAT_DATA_UINT64
},
62 { "read_buf_nocopy", KSTAT_DATA_UINT64
},
63 { "write_buf_copied", KSTAT_DATA_UINT64
},
64 { "write_buf_nocopy", KSTAT_DATA_UINT64
}
67 #define XUIOSTAT_INCR(stat, val) \
68 atomic_add_64(&xuio_stats.stat.value.ui64, (val))
69 #define XUIOSTAT_BUMP(stat) XUIOSTAT_INCR(stat, 1)
72 * Enable/disable nopwrite feature.
74 int zfs_nopwrite_enabled
= 1;
77 * Tunable to control percentage of dirtied blocks from frees in one TXG.
78 * After this threshold is crossed, additional dirty blocks from frees
79 * wait until the next TXG.
80 * A value of zero will disable this throttle.
82 uint32_t zfs_per_txg_dirty_frees_percent
= 30;
85 * This can be used for testing, to ensure that certain actions happen
86 * while in the middle of a remap (which might otherwise complete too
89 int zfs_object_remap_one_indirect_delay_ticks
= 0;
92 * Limit the amount we can prefetch with one call to this amount. This
93 * helps to limit the amount of memory that can be used by prefetching.
94 * Larger objects should be prefetched a bit at a time.
96 uint64_t dmu_prefetch_max
= 8 * SPA_MAXBLOCKSIZE
;
98 const dmu_object_type_info_t dmu_ot
[DMU_OT_NUMTYPES
] = {
99 { DMU_BSWAP_UINT8
, TRUE
, FALSE
, FALSE
, "unallocated" },
100 { DMU_BSWAP_ZAP
, TRUE
, TRUE
, FALSE
, "object directory" },
101 { DMU_BSWAP_UINT64
, TRUE
, TRUE
, FALSE
, "object array" },
102 { DMU_BSWAP_UINT8
, TRUE
, FALSE
, FALSE
, "packed nvlist" },
103 { DMU_BSWAP_UINT64
, TRUE
, FALSE
, FALSE
, "packed nvlist size" },
104 { DMU_BSWAP_UINT64
, TRUE
, FALSE
, FALSE
, "bpobj" },
105 { DMU_BSWAP_UINT64
, TRUE
, FALSE
, FALSE
, "bpobj header" },
106 { DMU_BSWAP_UINT64
, TRUE
, FALSE
, FALSE
, "SPA space map header" },
107 { DMU_BSWAP_UINT64
, TRUE
, FALSE
, FALSE
, "SPA space map" },
108 { DMU_BSWAP_UINT64
, TRUE
, FALSE
, TRUE
, "ZIL intent log" },
109 { DMU_BSWAP_DNODE
, TRUE
, FALSE
, TRUE
, "DMU dnode" },
110 { DMU_BSWAP_OBJSET
, TRUE
, TRUE
, FALSE
, "DMU objset" },
111 { DMU_BSWAP_UINT64
, TRUE
, TRUE
, FALSE
, "DSL directory" },
112 { DMU_BSWAP_ZAP
, TRUE
, TRUE
, FALSE
, "DSL directory child map" },
113 { DMU_BSWAP_ZAP
, TRUE
, TRUE
, FALSE
, "DSL dataset snap map" },
114 { DMU_BSWAP_ZAP
, TRUE
, TRUE
, FALSE
, "DSL props" },
115 { DMU_BSWAP_UINT64
, TRUE
, TRUE
, FALSE
, "DSL dataset" },
116 { DMU_BSWAP_ZNODE
, TRUE
, FALSE
, FALSE
, "ZFS znode" },
117 { DMU_BSWAP_OLDACL
, TRUE
, FALSE
, TRUE
, "ZFS V0 ACL" },
118 { DMU_BSWAP_UINT8
, FALSE
, FALSE
, TRUE
, "ZFS plain file" },
119 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, TRUE
, "ZFS directory" },
120 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, FALSE
, "ZFS master node" },
121 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, TRUE
, "ZFS delete queue" },
122 { DMU_BSWAP_UINT8
, FALSE
, FALSE
, TRUE
, "zvol object" },
123 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, FALSE
, "zvol prop" },
124 { DMU_BSWAP_UINT8
, FALSE
, FALSE
, TRUE
, "other uint8[]" },
125 { DMU_BSWAP_UINT64
, FALSE
, FALSE
, TRUE
, "other uint64[]" },
126 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, FALSE
, "other ZAP" },
127 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, FALSE
, "persistent error log" },
128 { DMU_BSWAP_UINT8
, TRUE
, FALSE
, FALSE
, "SPA history" },
129 { DMU_BSWAP_UINT64
, TRUE
, FALSE
, FALSE
, "SPA history offsets" },
130 { DMU_BSWAP_ZAP
, TRUE
, TRUE
, FALSE
, "Pool properties" },
131 { DMU_BSWAP_ZAP
, TRUE
, TRUE
, FALSE
, "DSL permissions" },
132 { DMU_BSWAP_ACL
, TRUE
, FALSE
, TRUE
, "ZFS ACL" },
133 { DMU_BSWAP_UINT8
, TRUE
, FALSE
, TRUE
, "ZFS SYSACL" },
134 { DMU_BSWAP_UINT8
, TRUE
, FALSE
, TRUE
, "FUID table" },
135 { DMU_BSWAP_UINT64
, TRUE
, FALSE
, FALSE
, "FUID table size" },
136 { DMU_BSWAP_ZAP
, TRUE
, TRUE
, FALSE
, "DSL dataset next clones" },
137 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, FALSE
, "scan work queue" },
138 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, TRUE
, "ZFS user/group/project used"},
139 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, TRUE
, "ZFS user/group/proj quota"},
140 { DMU_BSWAP_ZAP
, TRUE
, TRUE
, FALSE
, "snapshot refcount tags" },
141 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, FALSE
, "DDT ZAP algorithm" },
142 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, FALSE
, "DDT statistics" },
143 { DMU_BSWAP_UINT8
, TRUE
, FALSE
, TRUE
, "System attributes" },
144 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, TRUE
, "SA master node" },
145 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, TRUE
, "SA attr registration" },
146 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, TRUE
, "SA attr layouts" },
147 { DMU_BSWAP_ZAP
, TRUE
, FALSE
, FALSE
, "scan translations" },
148 { DMU_BSWAP_UINT8
, FALSE
, FALSE
, TRUE
, "deduplicated block" },
149 { DMU_BSWAP_ZAP
, TRUE
, TRUE
, FALSE
, "DSL deadlist map" },
150 { DMU_BSWAP_UINT64
, TRUE
, TRUE
, FALSE
, "DSL deadlist map hdr" },
151 { DMU_BSWAP_ZAP
, TRUE
, TRUE
, FALSE
, "DSL dir clones" },
152 { DMU_BSWAP_UINT64
, TRUE
, FALSE
, FALSE
, "bpobj subobj" }
155 const dmu_object_byteswap_info_t dmu_ot_byteswap
[DMU_BSWAP_NUMFUNCS
] = {
156 { byteswap_uint8_array
, "uint8" },
157 { byteswap_uint16_array
, "uint16" },
158 { byteswap_uint32_array
, "uint32" },
159 { byteswap_uint64_array
, "uint64" },
160 { zap_byteswap
, "zap" },
161 { dnode_buf_byteswap
, "dnode" },
162 { dmu_objset_byteswap
, "objset" },
163 { zfs_znode_byteswap
, "znode" },
164 { zfs_oldacl_byteswap
, "oldacl" },
165 { zfs_acl_byteswap
, "acl" }
169 dmu_buf_hold_noread_by_dnode(dnode_t
*dn
, uint64_t offset
,
170 void *tag
, dmu_buf_t
**dbp
)
175 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
176 blkid
= dbuf_whichblock(dn
, 0, offset
);
177 db
= dbuf_hold(dn
, blkid
, tag
);
178 rw_exit(&dn
->dn_struct_rwlock
);
182 return (SET_ERROR(EIO
));
189 dmu_buf_hold_noread(objset_t
*os
, uint64_t object
, uint64_t offset
,
190 void *tag
, dmu_buf_t
**dbp
)
197 err
= dnode_hold(os
, object
, FTAG
, &dn
);
200 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
201 blkid
= dbuf_whichblock(dn
, 0, offset
);
202 db
= dbuf_hold(dn
, blkid
, tag
);
203 rw_exit(&dn
->dn_struct_rwlock
);
204 dnode_rele(dn
, FTAG
);
208 return (SET_ERROR(EIO
));
216 dmu_buf_hold_by_dnode(dnode_t
*dn
, uint64_t offset
,
217 void *tag
, dmu_buf_t
**dbp
, int flags
)
220 int db_flags
= DB_RF_CANFAIL
;
222 if (flags
& DMU_READ_NO_PREFETCH
)
223 db_flags
|= DB_RF_NOPREFETCH
;
224 if (flags
& DMU_READ_NO_DECRYPT
)
225 db_flags
|= DB_RF_NO_DECRYPT
;
227 err
= dmu_buf_hold_noread_by_dnode(dn
, offset
, tag
, dbp
);
229 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)(*dbp
);
230 err
= dbuf_read(db
, NULL
, db_flags
);
241 dmu_buf_hold(objset_t
*os
, uint64_t object
, uint64_t offset
,
242 void *tag
, dmu_buf_t
**dbp
, int flags
)
245 int db_flags
= DB_RF_CANFAIL
;
247 if (flags
& DMU_READ_NO_PREFETCH
)
248 db_flags
|= DB_RF_NOPREFETCH
;
249 if (flags
& DMU_READ_NO_DECRYPT
)
250 db_flags
|= DB_RF_NO_DECRYPT
;
252 err
= dmu_buf_hold_noread(os
, object
, offset
, tag
, dbp
);
254 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)(*dbp
);
255 err
= dbuf_read(db
, NULL
, db_flags
);
268 return (DN_OLD_MAX_BONUSLEN
);
272 dmu_set_bonus(dmu_buf_t
*db_fake
, int newsize
, dmu_tx_t
*tx
)
274 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
281 if (dn
->dn_bonus
!= db
) {
282 error
= SET_ERROR(EINVAL
);
283 } else if (newsize
< 0 || newsize
> db_fake
->db_size
) {
284 error
= SET_ERROR(EINVAL
);
286 dnode_setbonuslen(dn
, newsize
, tx
);
295 dmu_set_bonustype(dmu_buf_t
*db_fake
, dmu_object_type_t type
, dmu_tx_t
*tx
)
297 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
304 if (!DMU_OT_IS_VALID(type
)) {
305 error
= SET_ERROR(EINVAL
);
306 } else if (dn
->dn_bonus
!= db
) {
307 error
= SET_ERROR(EINVAL
);
309 dnode_setbonus_type(dn
, type
, tx
);
318 dmu_get_bonustype(dmu_buf_t
*db_fake
)
320 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
322 dmu_object_type_t type
;
326 type
= dn
->dn_bonustype
;
333 dmu_rm_spill(objset_t
*os
, uint64_t object
, dmu_tx_t
*tx
)
338 error
= dnode_hold(os
, object
, FTAG
, &dn
);
339 dbuf_rm_spill(dn
, tx
);
340 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
341 dnode_rm_spill(dn
, tx
);
342 rw_exit(&dn
->dn_struct_rwlock
);
343 dnode_rele(dn
, FTAG
);
348 * Lookup and hold the bonus buffer for the provided dnode. If the dnode
349 * has not yet been allocated a new bonus dbuf a will be allocated.
350 * Returns ENOENT, EIO, or 0.
352 int dmu_bonus_hold_by_dnode(dnode_t
*dn
, void *tag
, dmu_buf_t
**dbp
,
357 uint32_t db_flags
= DB_RF_MUST_SUCCEED
;
359 if (flags
& DMU_READ_NO_PREFETCH
)
360 db_flags
|= DB_RF_NOPREFETCH
;
361 if (flags
& DMU_READ_NO_DECRYPT
)
362 db_flags
|= DB_RF_NO_DECRYPT
;
364 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
365 if (dn
->dn_bonus
== NULL
) {
366 rw_exit(&dn
->dn_struct_rwlock
);
367 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
368 if (dn
->dn_bonus
== NULL
)
369 dbuf_create_bonus(dn
);
373 /* as long as the bonus buf is held, the dnode will be held */
374 if (zfs_refcount_add(&db
->db_holds
, tag
) == 1) {
375 VERIFY(dnode_add_ref(dn
, db
));
376 atomic_inc_32(&dn
->dn_dbufs_count
);
380 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's
381 * hold and incrementing the dbuf count to ensure that dnode_move() sees
382 * a dnode hold for every dbuf.
384 rw_exit(&dn
->dn_struct_rwlock
);
386 error
= dbuf_read(db
, NULL
, db_flags
);
388 dnode_evict_bonus(dn
);
399 * returns ENOENT, EIO, or 0.
402 dmu_bonus_hold_impl(objset_t
*os
, uint64_t object
, void *tag
, uint32_t flags
,
408 uint32_t db_flags
= DB_RF_MUST_SUCCEED
;
410 if (flags
& DMU_READ_NO_PREFETCH
)
411 db_flags
|= DB_RF_NOPREFETCH
;
412 if (flags
& DMU_READ_NO_DECRYPT
)
413 db_flags
|= DB_RF_NO_DECRYPT
;
415 error
= dnode_hold(os
, object
, FTAG
, &dn
);
419 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
420 if (dn
->dn_bonus
== NULL
) {
421 rw_exit(&dn
->dn_struct_rwlock
);
422 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
423 if (dn
->dn_bonus
== NULL
)
424 dbuf_create_bonus(dn
);
428 /* as long as the bonus buf is held, the dnode will be held */
429 if (zfs_refcount_add(&db
->db_holds
, tag
) == 1) {
430 VERIFY(dnode_add_ref(dn
, db
));
431 atomic_inc_32(&dn
->dn_dbufs_count
);
435 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's
436 * hold and incrementing the dbuf count to ensure that dnode_move() sees
437 * a dnode hold for every dbuf.
439 rw_exit(&dn
->dn_struct_rwlock
);
441 dnode_rele(dn
, FTAG
);
443 error
= dbuf_read(db
, NULL
, db_flags
);
445 dnode_evict_bonus(dn
);
456 dmu_bonus_hold(objset_t
*os
, uint64_t obj
, void *tag
, dmu_buf_t
**dbp
)
458 return (dmu_bonus_hold_impl(os
, obj
, tag
, DMU_READ_NO_PREFETCH
, dbp
));
462 * returns ENOENT, EIO, or 0.
464 * This interface will allocate a blank spill dbuf when a spill blk
465 * doesn't already exist on the dnode.
467 * if you only want to find an already existing spill db, then
468 * dmu_spill_hold_existing() should be used.
471 dmu_spill_hold_by_dnode(dnode_t
*dn
, uint32_t flags
, void *tag
, dmu_buf_t
**dbp
)
473 dmu_buf_impl_t
*db
= NULL
;
476 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
477 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
479 db
= dbuf_hold(dn
, DMU_SPILL_BLKID
, tag
);
481 if ((flags
& DB_RF_HAVESTRUCT
) == 0)
482 rw_exit(&dn
->dn_struct_rwlock
);
485 err
= dbuf_read(db
, NULL
, flags
);
494 dmu_spill_hold_existing(dmu_buf_t
*bonus
, void *tag
, dmu_buf_t
**dbp
)
496 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)bonus
;
503 if (spa_version(dn
->dn_objset
->os_spa
) < SPA_VERSION_SA
) {
504 err
= SET_ERROR(EINVAL
);
506 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
508 if (!dn
->dn_have_spill
) {
509 err
= SET_ERROR(ENOENT
);
511 err
= dmu_spill_hold_by_dnode(dn
,
512 DB_RF_HAVESTRUCT
| DB_RF_CANFAIL
, tag
, dbp
);
515 rw_exit(&dn
->dn_struct_rwlock
);
523 dmu_spill_hold_by_bonus(dmu_buf_t
*bonus
, uint32_t flags
, void *tag
,
526 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)bonus
;
529 uint32_t db_flags
= DB_RF_CANFAIL
;
531 if (flags
& DMU_READ_NO_DECRYPT
)
532 db_flags
|= DB_RF_NO_DECRYPT
;
536 err
= dmu_spill_hold_by_dnode(dn
, db_flags
, tag
, dbp
);
543 * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
544 * to take a held dnode rather than <os, object> -- the lookup is wasteful,
545 * and can induce severe lock contention when writing to several files
546 * whose dnodes are in the same block.
549 dmu_buf_hold_array_by_dnode(dnode_t
*dn
, uint64_t offset
, uint64_t length
,
550 boolean_t read
, void *tag
, int *numbufsp
, dmu_buf_t
***dbpp
, uint32_t flags
)
553 uint64_t blkid
, nblks
, i
;
558 ASSERT(length
<= DMU_MAX_ACCESS
);
561 * Note: We directly notify the prefetch code of this read, so that
562 * we can tell it about the multi-block read. dbuf_read() only knows
563 * about the one block it is accessing.
565 dbuf_flags
= DB_RF_CANFAIL
| DB_RF_NEVERWAIT
| DB_RF_HAVESTRUCT
|
568 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
569 if (dn
->dn_datablkshift
) {
570 int blkshift
= dn
->dn_datablkshift
;
571 nblks
= (P2ROUNDUP(offset
+ length
, 1ULL << blkshift
) -
572 P2ALIGN(offset
, 1ULL << blkshift
)) >> blkshift
;
574 if (offset
+ length
> dn
->dn_datablksz
) {
575 zfs_panic_recover("zfs: accessing past end of object "
576 "%llx/%llx (size=%u access=%llu+%llu)",
577 (longlong_t
)dn
->dn_objset
->
578 os_dsl_dataset
->ds_object
,
579 (longlong_t
)dn
->dn_object
, dn
->dn_datablksz
,
580 (longlong_t
)offset
, (longlong_t
)length
);
581 rw_exit(&dn
->dn_struct_rwlock
);
582 return (SET_ERROR(EIO
));
586 dbp
= kmem_zalloc(sizeof (dmu_buf_t
*) * nblks
, KM_SLEEP
);
588 zio
= zio_root(dn
->dn_objset
->os_spa
, NULL
, NULL
, ZIO_FLAG_CANFAIL
);
589 blkid
= dbuf_whichblock(dn
, 0, offset
);
590 for (i
= 0; i
< nblks
; i
++) {
591 dmu_buf_impl_t
*db
= dbuf_hold(dn
, blkid
+ i
, tag
);
593 rw_exit(&dn
->dn_struct_rwlock
);
594 dmu_buf_rele_array(dbp
, nblks
, tag
);
596 return (SET_ERROR(EIO
));
599 /* initiate async i/o */
601 (void) dbuf_read(db
, zio
, dbuf_flags
);
605 if ((flags
& DMU_READ_NO_PREFETCH
) == 0 &&
606 DNODE_META_IS_CACHEABLE(dn
) && length
<= zfetch_array_rd_sz
) {
607 dmu_zfetch(&dn
->dn_zfetch
, blkid
, nblks
,
608 read
&& DNODE_IS_CACHEABLE(dn
), B_TRUE
);
610 rw_exit(&dn
->dn_struct_rwlock
);
612 /* wait for async i/o */
615 dmu_buf_rele_array(dbp
, nblks
, tag
);
619 /* wait for other io to complete */
621 for (i
= 0; i
< nblks
; i
++) {
622 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)dbp
[i
];
623 mutex_enter(&db
->db_mtx
);
624 while (db
->db_state
== DB_READ
||
625 db
->db_state
== DB_FILL
)
626 cv_wait(&db
->db_changed
, &db
->db_mtx
);
627 if (db
->db_state
== DB_UNCACHED
)
628 err
= SET_ERROR(EIO
);
629 mutex_exit(&db
->db_mtx
);
631 dmu_buf_rele_array(dbp
, nblks
, tag
);
643 dmu_buf_hold_array(objset_t
*os
, uint64_t object
, uint64_t offset
,
644 uint64_t length
, int read
, void *tag
, int *numbufsp
, dmu_buf_t
***dbpp
)
649 err
= dnode_hold(os
, object
, FTAG
, &dn
);
653 err
= dmu_buf_hold_array_by_dnode(dn
, offset
, length
, read
, tag
,
654 numbufsp
, dbpp
, DMU_READ_PREFETCH
);
656 dnode_rele(dn
, FTAG
);
662 dmu_buf_hold_array_by_bonus(dmu_buf_t
*db_fake
, uint64_t offset
,
663 uint64_t length
, boolean_t read
, void *tag
, int *numbufsp
,
666 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
672 err
= dmu_buf_hold_array_by_dnode(dn
, offset
, length
, read
, tag
,
673 numbufsp
, dbpp
, DMU_READ_PREFETCH
);
680 dmu_buf_rele_array(dmu_buf_t
**dbp_fake
, int numbufs
, void *tag
)
683 dmu_buf_impl_t
**dbp
= (dmu_buf_impl_t
**)dbp_fake
;
688 for (i
= 0; i
< numbufs
; i
++) {
690 dbuf_rele(dbp
[i
], tag
);
693 kmem_free(dbp
, sizeof (dmu_buf_t
*) * numbufs
);
697 * Issue prefetch i/os for the given blocks. If level is greater than 0, the
698 * indirect blocks prefeteched will be those that point to the blocks containing
699 * the data starting at offset, and continuing to offset + len.
701 * Note that if the indirect blocks above the blocks being prefetched are not
702 * in cache, they will be asychronously read in.
705 dmu_prefetch(objset_t
*os
, uint64_t object
, int64_t level
, uint64_t offset
,
706 uint64_t len
, zio_priority_t pri
)
712 if (len
== 0) { /* they're interested in the bonus buffer */
713 dn
= DMU_META_DNODE(os
);
715 if (object
== 0 || object
>= DN_MAX_OBJECT
)
718 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
719 blkid
= dbuf_whichblock(dn
, level
,
720 object
* sizeof (dnode_phys_t
));
721 dbuf_prefetch(dn
, level
, blkid
, pri
, 0);
722 rw_exit(&dn
->dn_struct_rwlock
);
727 * See comment before the definition of dmu_prefetch_max.
729 len
= MIN(len
, dmu_prefetch_max
);
732 * XXX - Note, if the dnode for the requested object is not
733 * already cached, we will do a *synchronous* read in the
734 * dnode_hold() call. The same is true for any indirects.
736 err
= dnode_hold(os
, object
, FTAG
, &dn
);
741 * offset + len - 1 is the last byte we want to prefetch for, and offset
742 * is the first. Then dbuf_whichblk(dn, level, off + len - 1) is the
743 * last block we want to prefetch, and dbuf_whichblock(dn, level,
744 * offset) is the first. Then the number we need to prefetch is the
747 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
748 if (level
> 0 || dn
->dn_datablkshift
!= 0) {
749 nblks
= dbuf_whichblock(dn
, level
, offset
+ len
- 1) -
750 dbuf_whichblock(dn
, level
, offset
) + 1;
752 nblks
= (offset
< dn
->dn_datablksz
);
756 blkid
= dbuf_whichblock(dn
, level
, offset
);
757 for (int i
= 0; i
< nblks
; i
++)
758 dbuf_prefetch(dn
, level
, blkid
+ i
, pri
, 0);
760 rw_exit(&dn
->dn_struct_rwlock
);
762 dnode_rele(dn
, FTAG
);
766 * Get the next "chunk" of file data to free. We traverse the file from
767 * the end so that the file gets shorter over time (if we crashes in the
768 * middle, this will leave us in a better state). We find allocated file
769 * data by simply searching the allocated level 1 indirects.
771 * On input, *start should be the first offset that does not need to be
772 * freed (e.g. "offset + length"). On return, *start will be the first
773 * offset that should be freed.
776 get_next_chunk(dnode_t
*dn
, uint64_t *start
, uint64_t minimum
)
778 uint64_t maxblks
= DMU_MAX_ACCESS
>> (dn
->dn_indblkshift
+ 1);
779 /* bytes of data covered by a level-1 indirect block */
781 dn
->dn_datablksz
* EPB(dn
->dn_indblkshift
, SPA_BLKPTRSHIFT
);
783 ASSERT3U(minimum
, <=, *start
);
785 if (*start
- minimum
<= iblkrange
* maxblks
) {
789 ASSERT(ISP2(iblkrange
));
791 for (uint64_t blks
= 0; *start
> minimum
&& blks
< maxblks
; blks
++) {
795 * dnode_next_offset(BACKWARDS) will find an allocated L1
796 * indirect block at or before the input offset. We must
797 * decrement *start so that it is at the end of the region
801 err
= dnode_next_offset(dn
,
802 DNODE_FIND_BACKWARDS
, start
, 2, 1, 0);
804 /* if there are no indirect blocks before start, we are done */
808 } else if (err
!= 0) {
812 /* set start to the beginning of this L1 indirect */
813 *start
= P2ALIGN(*start
, iblkrange
);
815 if (*start
< minimum
)
821 * If this objset is of type OST_ZFS return true if vfs's unmounted flag is set,
822 * otherwise return false.
823 * Used below in dmu_free_long_range_impl() to enable abort when unmounting
827 dmu_objset_zfs_unmounting(objset_t
*os
)
830 if (dmu_objset_type(os
) == DMU_OST_ZFS
)
831 return (zfs_get_vfs_flag_unmounted(os
));
837 dmu_free_long_range_impl(objset_t
*os
, dnode_t
*dn
, uint64_t offset
,
840 uint64_t object_size
= (dn
->dn_maxblkid
+ 1) * dn
->dn_datablksz
;
842 uint64_t dirty_frees_threshold
;
843 dsl_pool_t
*dp
= dmu_objset_pool(os
);
845 if (offset
>= object_size
)
848 if (zfs_per_txg_dirty_frees_percent
<= 100)
849 dirty_frees_threshold
=
850 zfs_per_txg_dirty_frees_percent
* zfs_dirty_data_max
/ 100;
852 dirty_frees_threshold
= zfs_dirty_data_max
/ 4;
854 if (length
== DMU_OBJECT_END
|| offset
+ length
> object_size
)
855 length
= object_size
- offset
;
857 while (length
!= 0) {
858 uint64_t chunk_end
, chunk_begin
, chunk_len
;
859 uint64_t long_free_dirty_all_txgs
= 0;
862 if (dmu_objset_zfs_unmounting(dn
->dn_objset
))
863 return (SET_ERROR(EINTR
));
865 chunk_end
= chunk_begin
= offset
+ length
;
867 /* move chunk_begin backwards to the beginning of this chunk */
868 err
= get_next_chunk(dn
, &chunk_begin
, offset
);
871 ASSERT3U(chunk_begin
, >=, offset
);
872 ASSERT3U(chunk_begin
, <=, chunk_end
);
874 chunk_len
= chunk_end
- chunk_begin
;
876 mutex_enter(&dp
->dp_lock
);
877 for (int t
= 0; t
< TXG_SIZE
; t
++) {
878 long_free_dirty_all_txgs
+=
879 dp
->dp_long_free_dirty_pertxg
[t
];
881 mutex_exit(&dp
->dp_lock
);
884 * To avoid filling up a TXG with just frees wait for
885 * the next TXG to open before freeing more chunks if
886 * we have reached the threshold of frees
888 if (dirty_frees_threshold
!= 0 &&
889 long_free_dirty_all_txgs
>= dirty_frees_threshold
) {
890 txg_wait_open(dp
, 0, B_TRUE
);
894 tx
= dmu_tx_create(os
);
895 dmu_tx_hold_free(tx
, dn
->dn_object
, chunk_begin
, chunk_len
);
898 * Mark this transaction as typically resulting in a net
899 * reduction in space used.
901 dmu_tx_mark_netfree(tx
);
902 err
= dmu_tx_assign(tx
, TXG_WAIT
);
908 mutex_enter(&dp
->dp_lock
);
909 dp
->dp_long_free_dirty_pertxg
[dmu_tx_get_txg(tx
) & TXG_MASK
] +=
911 mutex_exit(&dp
->dp_lock
);
912 DTRACE_PROBE3(free__long__range
,
913 uint64_t, long_free_dirty_all_txgs
, uint64_t, chunk_len
,
914 uint64_t, dmu_tx_get_txg(tx
));
915 dnode_free_range(dn
, chunk_begin
, chunk_len
, tx
);
925 dmu_free_long_range(objset_t
*os
, uint64_t object
,
926 uint64_t offset
, uint64_t length
)
931 err
= dnode_hold(os
, object
, FTAG
, &dn
);
934 err
= dmu_free_long_range_impl(os
, dn
, offset
, length
);
937 * It is important to zero out the maxblkid when freeing the entire
938 * file, so that (a) subsequent calls to dmu_free_long_range_impl()
939 * will take the fast path, and (b) dnode_reallocate() can verify
940 * that the entire file has been freed.
942 if (err
== 0 && offset
== 0 && length
== DMU_OBJECT_END
)
945 dnode_rele(dn
, FTAG
);
950 dmu_free_long_object(objset_t
*os
, uint64_t object
)
955 err
= dmu_free_long_range(os
, object
, 0, DMU_OBJECT_END
);
959 tx
= dmu_tx_create(os
);
960 dmu_tx_hold_bonus(tx
, object
);
961 dmu_tx_hold_free(tx
, object
, 0, DMU_OBJECT_END
);
962 dmu_tx_mark_netfree(tx
);
963 err
= dmu_tx_assign(tx
, TXG_WAIT
);
966 err
= dmu_object_free(os
, object
, tx
);
977 dmu_free_range(objset_t
*os
, uint64_t object
, uint64_t offset
,
978 uint64_t size
, dmu_tx_t
*tx
)
981 int err
= dnode_hold(os
, object
, FTAG
, &dn
);
984 ASSERT(offset
< UINT64_MAX
);
985 ASSERT(size
== DMU_OBJECT_END
|| size
<= UINT64_MAX
- offset
);
986 dnode_free_range(dn
, offset
, size
, tx
);
987 dnode_rele(dn
, FTAG
);
992 dmu_read_impl(dnode_t
*dn
, uint64_t offset
, uint64_t size
,
993 void *buf
, uint32_t flags
)
996 int numbufs
, err
= 0;
999 * Deal with odd block sizes, where there can't be data past the first
1000 * block. If we ever do the tail block optimization, we will need to
1001 * handle that here as well.
1003 if (dn
->dn_maxblkid
== 0) {
1004 int newsz
= offset
> dn
->dn_datablksz
? 0 :
1005 MIN(size
, dn
->dn_datablksz
- offset
);
1006 bzero((char *)buf
+ newsz
, size
- newsz
);
1011 uint64_t mylen
= MIN(size
, DMU_MAX_ACCESS
/ 2);
1015 * NB: we could do this block-at-a-time, but it's nice
1016 * to be reading in parallel.
1018 err
= dmu_buf_hold_array_by_dnode(dn
, offset
, mylen
,
1019 TRUE
, FTAG
, &numbufs
, &dbp
, flags
);
1023 for (i
= 0; i
< numbufs
; i
++) {
1026 dmu_buf_t
*db
= dbp
[i
];
1030 bufoff
= offset
- db
->db_offset
;
1031 tocpy
= (int)MIN(db
->db_size
- bufoff
, size
);
1033 bcopy((char *)db
->db_data
+ bufoff
, buf
, tocpy
);
1037 buf
= (char *)buf
+ tocpy
;
1039 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
1045 dmu_read(objset_t
*os
, uint64_t object
, uint64_t offset
, uint64_t size
,
1046 void *buf
, uint32_t flags
)
1051 err
= dnode_hold(os
, object
, FTAG
, &dn
);
1055 err
= dmu_read_impl(dn
, offset
, size
, buf
, flags
);
1056 dnode_rele(dn
, FTAG
);
1061 dmu_read_by_dnode(dnode_t
*dn
, uint64_t offset
, uint64_t size
, void *buf
,
1064 return (dmu_read_impl(dn
, offset
, size
, buf
, flags
));
1068 dmu_write_impl(dmu_buf_t
**dbp
, int numbufs
, uint64_t offset
, uint64_t size
,
1069 const void *buf
, dmu_tx_t
*tx
)
1073 for (i
= 0; i
< numbufs
; i
++) {
1076 dmu_buf_t
*db
= dbp
[i
];
1080 bufoff
= offset
- db
->db_offset
;
1081 tocpy
= (int)MIN(db
->db_size
- bufoff
, size
);
1083 ASSERT(i
== 0 || i
== numbufs
-1 || tocpy
== db
->db_size
);
1085 if (tocpy
== db
->db_size
)
1086 dmu_buf_will_fill(db
, tx
);
1088 dmu_buf_will_dirty(db
, tx
);
1090 bcopy(buf
, (char *)db
->db_data
+ bufoff
, tocpy
);
1092 if (tocpy
== db
->db_size
)
1093 dmu_buf_fill_done(db
, tx
);
1097 buf
= (char *)buf
+ tocpy
;
1102 dmu_write(objset_t
*os
, uint64_t object
, uint64_t offset
, uint64_t size
,
1103 const void *buf
, dmu_tx_t
*tx
)
1111 VERIFY0(dmu_buf_hold_array(os
, object
, offset
, size
,
1112 FALSE
, FTAG
, &numbufs
, &dbp
));
1113 dmu_write_impl(dbp
, numbufs
, offset
, size
, buf
, tx
);
1114 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
1118 dmu_write_by_dnode(dnode_t
*dn
, uint64_t offset
, uint64_t size
,
1119 const void *buf
, dmu_tx_t
*tx
)
1127 VERIFY0(dmu_buf_hold_array_by_dnode(dn
, offset
, size
,
1128 FALSE
, FTAG
, &numbufs
, &dbp
, DMU_READ_PREFETCH
));
1129 dmu_write_impl(dbp
, numbufs
, offset
, size
, buf
, tx
);
1130 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
1134 dmu_object_remap_one_indirect(objset_t
*os
, dnode_t
*dn
,
1135 uint64_t last_removal_txg
, uint64_t offset
)
1137 uint64_t l1blkid
= dbuf_whichblock(dn
, 1, offset
);
1140 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1141 dmu_buf_impl_t
*dbuf
= dbuf_hold_level(dn
, 1, l1blkid
, FTAG
);
1142 ASSERT3P(dbuf
, !=, NULL
);
1145 * If the block hasn't been written yet, this default will ensure
1146 * we don't try to remap it.
1148 uint64_t birth
= UINT64_MAX
;
1149 ASSERT3U(last_removal_txg
, !=, UINT64_MAX
);
1150 if (dbuf
->db_blkptr
!= NULL
)
1151 birth
= dbuf
->db_blkptr
->blk_birth
;
1152 rw_exit(&dn
->dn_struct_rwlock
);
1155 * If this L1 was already written after the last removal, then we've
1156 * already tried to remap it.
1158 if (birth
<= last_removal_txg
&&
1159 dbuf_read(dbuf
, NULL
, DB_RF_MUST_SUCCEED
) == 0 &&
1160 dbuf_can_remap(dbuf
)) {
1161 dmu_tx_t
*tx
= dmu_tx_create(os
);
1162 dmu_tx_hold_remap_l1indirect(tx
, dn
->dn_object
);
1163 err
= dmu_tx_assign(tx
, TXG_WAIT
);
1165 (void) dbuf_dirty(dbuf
, tx
);
1172 dbuf_rele(dbuf
, FTAG
);
1174 delay(zfs_object_remap_one_indirect_delay_ticks
);
1180 * Remap all blockpointers in the object, if possible, so that they reference
1181 * only concrete vdevs.
1183 * To do this, iterate over the L0 blockpointers and remap any that reference
1184 * an indirect vdev. Note that we only examine L0 blockpointers; since we
1185 * cannot guarantee that we can remap all blockpointer anyways (due to split
1186 * blocks), we do not want to make the code unnecessarily complicated to
1187 * catch the unlikely case that there is an L1 block on an indirect vdev that
1188 * contains no indirect blockpointers.
1191 dmu_object_remap_indirects(objset_t
*os
, uint64_t object
,
1192 uint64_t last_removal_txg
)
1194 uint64_t offset
, l1span
;
1198 err
= dnode_hold(os
, object
, FTAG
, &dn
);
1203 if (dn
->dn_nlevels
<= 1) {
1204 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
1205 err
= SET_ERROR(EINTR
);
1209 * If the dnode has no indirect blocks, we cannot dirty them.
1210 * We still want to remap the blkptr(s) in the dnode if
1211 * appropriate, so mark it as dirty.
1213 if (err
== 0 && dnode_needs_remap(dn
)) {
1214 dmu_tx_t
*tx
= dmu_tx_create(os
);
1215 dmu_tx_hold_bonus(tx
, dn
->dn_object
);
1216 if ((err
= dmu_tx_assign(tx
, TXG_WAIT
)) == 0) {
1217 dnode_setdirty(dn
, tx
);
1224 dnode_rele(dn
, FTAG
);
1229 l1span
= 1ULL << (dn
->dn_indblkshift
- SPA_BLKPTRSHIFT
+
1230 dn
->dn_datablkshift
);
1232 * Find the next L1 indirect that is not a hole.
1234 while (dnode_next_offset(dn
, 0, &offset
, 2, 1, 0) == 0) {
1235 if (issig(JUSTLOOKING
) && issig(FORREAL
)) {
1236 err
= SET_ERROR(EINTR
);
1239 if ((err
= dmu_object_remap_one_indirect(os
, dn
,
1240 last_removal_txg
, offset
)) != 0) {
1246 dnode_rele(dn
, FTAG
);
1251 dmu_prealloc(objset_t
*os
, uint64_t object
, uint64_t offset
, uint64_t size
,
1260 VERIFY(0 == dmu_buf_hold_array(os
, object
, offset
, size
,
1261 FALSE
, FTAG
, &numbufs
, &dbp
));
1263 for (i
= 0; i
< numbufs
; i
++) {
1264 dmu_buf_t
*db
= dbp
[i
];
1266 dmu_buf_will_not_fill(db
, tx
);
1268 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
1272 dmu_write_embedded(objset_t
*os
, uint64_t object
, uint64_t offset
,
1273 void *data
, uint8_t etype
, uint8_t comp
, int uncompressed_size
,
1274 int compressed_size
, int byteorder
, dmu_tx_t
*tx
)
1278 ASSERT3U(etype
, <, NUM_BP_EMBEDDED_TYPES
);
1279 ASSERT3U(comp
, <, ZIO_COMPRESS_FUNCTIONS
);
1280 VERIFY0(dmu_buf_hold_noread(os
, object
, offset
,
1283 dmu_buf_write_embedded(db
,
1284 data
, (bp_embedded_type_t
)etype
, (enum zio_compress
)comp
,
1285 uncompressed_size
, compressed_size
, byteorder
, tx
);
1287 dmu_buf_rele(db
, FTAG
);
1291 * DMU support for xuio
1293 kstat_t
*xuio_ksp
= NULL
;
1296 dmu_xuio_init(xuio_t
*xuio
, int nblk
)
1299 uio_t
*uio
= &xuio
->xu_uio
;
1301 uio
->uio_iovcnt
= nblk
;
1302 uio
->uio_iov
= kmem_zalloc(nblk
* sizeof (iovec_t
), KM_SLEEP
);
1304 priv
= kmem_zalloc(sizeof (dmu_xuio_t
), KM_SLEEP
);
1306 priv
->bufs
= kmem_zalloc(nblk
* sizeof (arc_buf_t
*), KM_SLEEP
);
1307 priv
->iovp
= uio
->uio_iov
;
1308 XUIO_XUZC_PRIV(xuio
) = priv
;
1310 if (XUIO_XUZC_RW(xuio
) == UIO_READ
)
1311 XUIOSTAT_INCR(xuiostat_onloan_rbuf
, nblk
);
1313 XUIOSTAT_INCR(xuiostat_onloan_wbuf
, nblk
);
1319 dmu_xuio_fini(xuio_t
*xuio
)
1321 dmu_xuio_t
*priv
= XUIO_XUZC_PRIV(xuio
);
1322 int nblk
= priv
->cnt
;
1324 kmem_free(priv
->iovp
, nblk
* sizeof (iovec_t
));
1325 kmem_free(priv
->bufs
, nblk
* sizeof (arc_buf_t
*));
1326 kmem_free(priv
, sizeof (dmu_xuio_t
));
1328 if (XUIO_XUZC_RW(xuio
) == UIO_READ
)
1329 XUIOSTAT_INCR(xuiostat_onloan_rbuf
, -nblk
);
1331 XUIOSTAT_INCR(xuiostat_onloan_wbuf
, -nblk
);
1335 * Initialize iov[priv->next] and priv->bufs[priv->next] with { off, n, abuf }
1336 * and increase priv->next by 1.
1339 dmu_xuio_add(xuio_t
*xuio
, arc_buf_t
*abuf
, offset_t off
, size_t n
)
1342 uio_t
*uio
= &xuio
->xu_uio
;
1343 dmu_xuio_t
*priv
= XUIO_XUZC_PRIV(xuio
);
1344 int i
= priv
->next
++;
1346 ASSERT(i
< priv
->cnt
);
1347 ASSERT(off
+ n
<= arc_buf_lsize(abuf
));
1348 iov
= uio
->uio_iov
+ i
;
1349 iov
->iov_base
= (char *)abuf
->b_data
+ off
;
1351 priv
->bufs
[i
] = abuf
;
1356 dmu_xuio_cnt(xuio_t
*xuio
)
1358 dmu_xuio_t
*priv
= XUIO_XUZC_PRIV(xuio
);
1363 dmu_xuio_arcbuf(xuio_t
*xuio
, int i
)
1365 dmu_xuio_t
*priv
= XUIO_XUZC_PRIV(xuio
);
1367 ASSERT(i
< priv
->cnt
);
1368 return (priv
->bufs
[i
]);
1372 dmu_xuio_clear(xuio_t
*xuio
, int i
)
1374 dmu_xuio_t
*priv
= XUIO_XUZC_PRIV(xuio
);
1376 ASSERT(i
< priv
->cnt
);
1377 priv
->bufs
[i
] = NULL
;
1381 xuio_stat_init(void)
1383 xuio_ksp
= kstat_create("zfs", 0, "xuio_stats", "misc",
1384 KSTAT_TYPE_NAMED
, sizeof (xuio_stats
) / sizeof (kstat_named_t
),
1385 KSTAT_FLAG_VIRTUAL
);
1386 if (xuio_ksp
!= NULL
) {
1387 xuio_ksp
->ks_data
= &xuio_stats
;
1388 kstat_install(xuio_ksp
);
1393 xuio_stat_fini(void)
1395 if (xuio_ksp
!= NULL
) {
1396 kstat_delete(xuio_ksp
);
1402 xuio_stat_wbuf_copied(void)
1404 XUIOSTAT_BUMP(xuiostat_wbuf_copied
);
1408 xuio_stat_wbuf_nocopy(void)
1410 XUIOSTAT_BUMP(xuiostat_wbuf_nocopy
);
1415 dmu_read_uio_dnode(dnode_t
*dn
, uio_t
*uio
, uint64_t size
)
1418 int numbufs
, i
, err
;
1419 xuio_t
*xuio
= NULL
;
1422 * NB: we could do this block-at-a-time, but it's nice
1423 * to be reading in parallel.
1425 err
= dmu_buf_hold_array_by_dnode(dn
, uio
->uio_loffset
, size
,
1426 TRUE
, FTAG
, &numbufs
, &dbp
, 0);
1430 if (uio
->uio_extflg
== UIO_XUIO
)
1431 xuio
= (xuio_t
*)uio
;
1433 for (i
= 0; i
< numbufs
; i
++) {
1436 dmu_buf_t
*db
= dbp
[i
];
1440 bufoff
= uio
->uio_loffset
- db
->db_offset
;
1441 tocpy
= (int)MIN(db
->db_size
- bufoff
, size
);
1444 dmu_buf_impl_t
*dbi
= (dmu_buf_impl_t
*)db
;
1445 arc_buf_t
*dbuf_abuf
= dbi
->db_buf
;
1446 arc_buf_t
*abuf
= dbuf_loan_arcbuf(dbi
);
1447 err
= dmu_xuio_add(xuio
, abuf
, bufoff
, tocpy
);
1449 uio
->uio_resid
-= tocpy
;
1450 uio
->uio_loffset
+= tocpy
;
1453 if (abuf
== dbuf_abuf
)
1454 XUIOSTAT_BUMP(xuiostat_rbuf_nocopy
);
1456 XUIOSTAT_BUMP(xuiostat_rbuf_copied
);
1458 err
= uiomove((char *)db
->db_data
+ bufoff
, tocpy
,
1466 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
1472 * Read 'size' bytes into the uio buffer.
1473 * From object zdb->db_object.
1474 * Starting at offset uio->uio_loffset.
1476 * If the caller already has a dbuf in the target object
1477 * (e.g. its bonus buffer), this routine is faster than dmu_read_uio(),
1478 * because we don't have to find the dnode_t for the object.
1481 dmu_read_uio_dbuf(dmu_buf_t
*zdb
, uio_t
*uio
, uint64_t size
)
1483 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)zdb
;
1492 err
= dmu_read_uio_dnode(dn
, uio
, size
);
1499 * Read 'size' bytes into the uio buffer.
1500 * From the specified object
1501 * Starting at offset uio->uio_loffset.
1504 dmu_read_uio(objset_t
*os
, uint64_t object
, uio_t
*uio
, uint64_t size
)
1512 err
= dnode_hold(os
, object
, FTAG
, &dn
);
1516 err
= dmu_read_uio_dnode(dn
, uio
, size
);
1518 dnode_rele(dn
, FTAG
);
1524 dmu_write_uio_dnode(dnode_t
*dn
, uio_t
*uio
, uint64_t size
, dmu_tx_t
*tx
)
1531 err
= dmu_buf_hold_array_by_dnode(dn
, uio
->uio_loffset
, size
,
1532 FALSE
, FTAG
, &numbufs
, &dbp
, DMU_READ_PREFETCH
);
1536 for (i
= 0; i
< numbufs
; i
++) {
1539 dmu_buf_t
*db
= dbp
[i
];
1543 bufoff
= uio
->uio_loffset
- db
->db_offset
;
1544 tocpy
= (int)MIN(db
->db_size
- bufoff
, size
);
1546 ASSERT(i
== 0 || i
== numbufs
-1 || tocpy
== db
->db_size
);
1548 if (tocpy
== db
->db_size
)
1549 dmu_buf_will_fill(db
, tx
);
1551 dmu_buf_will_dirty(db
, tx
);
1554 * XXX uiomove could block forever (eg. nfs-backed
1555 * pages). There needs to be a uiolockdown() function
1556 * to lock the pages in memory, so that uiomove won't
1559 err
= uiomove((char *)db
->db_data
+ bufoff
, tocpy
,
1562 if (tocpy
== db
->db_size
)
1563 dmu_buf_fill_done(db
, tx
);
1571 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
1576 * Write 'size' bytes from the uio buffer.
1577 * To object zdb->db_object.
1578 * Starting at offset uio->uio_loffset.
1580 * If the caller already has a dbuf in the target object
1581 * (e.g. its bonus buffer), this routine is faster than dmu_write_uio(),
1582 * because we don't have to find the dnode_t for the object.
1585 dmu_write_uio_dbuf(dmu_buf_t
*zdb
, uio_t
*uio
, uint64_t size
,
1588 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)zdb
;
1597 err
= dmu_write_uio_dnode(dn
, uio
, size
, tx
);
1604 * Write 'size' bytes from the uio buffer.
1605 * To the specified object.
1606 * Starting at offset uio->uio_loffset.
1609 dmu_write_uio(objset_t
*os
, uint64_t object
, uio_t
*uio
, uint64_t size
,
1618 err
= dnode_hold(os
, object
, FTAG
, &dn
);
1622 err
= dmu_write_uio_dnode(dn
, uio
, size
, tx
);
1624 dnode_rele(dn
, FTAG
);
1630 dmu_write_pages(objset_t
*os
, uint64_t object
, uint64_t offset
, uint64_t size
,
1631 page_t
*pp
, dmu_tx_t
*tx
)
1640 err
= dmu_buf_hold_array(os
, object
, offset
, size
,
1641 FALSE
, FTAG
, &numbufs
, &dbp
);
1645 for (i
= 0; i
< numbufs
; i
++) {
1646 int tocpy
, copied
, thiscpy
;
1648 dmu_buf_t
*db
= dbp
[i
];
1652 ASSERT3U(db
->db_size
, >=, PAGESIZE
);
1654 bufoff
= offset
- db
->db_offset
;
1655 tocpy
= (int)MIN(db
->db_size
- bufoff
, size
);
1657 ASSERT(i
== 0 || i
== numbufs
-1 || tocpy
== db
->db_size
);
1659 if (tocpy
== db
->db_size
)
1660 dmu_buf_will_fill(db
, tx
);
1662 dmu_buf_will_dirty(db
, tx
);
1664 for (copied
= 0; copied
< tocpy
; copied
+= PAGESIZE
) {
1665 ASSERT3U(pp
->p_offset
, ==, db
->db_offset
+ bufoff
);
1666 thiscpy
= MIN(PAGESIZE
, tocpy
- copied
);
1667 va
= zfs_map_page(pp
, S_READ
);
1668 bcopy(va
, (char *)db
->db_data
+ bufoff
, thiscpy
);
1669 zfs_unmap_page(pp
, va
);
1674 if (tocpy
== db
->db_size
)
1675 dmu_buf_fill_done(db
, tx
);
1680 dmu_buf_rele_array(dbp
, numbufs
, FTAG
);
1686 * Allocate a loaned anonymous arc buffer.
1689 dmu_request_arcbuf(dmu_buf_t
*handle
, int size
)
1691 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)handle
;
1693 return (arc_loan_buf(db
->db_objset
->os_spa
, B_FALSE
, size
));
1697 * Free a loaned arc buffer.
1700 dmu_return_arcbuf(arc_buf_t
*buf
)
1702 arc_return_buf(buf
, FTAG
);
1703 arc_buf_destroy(buf
, FTAG
);
1707 dmu_copy_from_buf(objset_t
*os
, uint64_t object
, uint64_t offset
,
1708 dmu_buf_t
*handle
, dmu_tx_t
*tx
)
1710 dmu_buf_t
*dst_handle
;
1711 dmu_buf_impl_t
*dstdb
;
1712 dmu_buf_impl_t
*srcdb
= (dmu_buf_impl_t
*)handle
;
1713 dmu_object_type_t type
;
1716 boolean_t byteorder
;
1717 uint8_t salt
[ZIO_DATA_SALT_LEN
];
1718 uint8_t iv
[ZIO_DATA_IV_LEN
];
1719 uint8_t mac
[ZIO_DATA_MAC_LEN
];
1721 ASSERT3P(srcdb
->db_buf
, !=, NULL
);
1723 /* hold the db that we want to write to */
1724 VERIFY0(dmu_buf_hold(os
, object
, offset
, FTAG
, &dst_handle
,
1725 DMU_READ_NO_DECRYPT
));
1726 dstdb
= (dmu_buf_impl_t
*)dst_handle
;
1727 datalen
= arc_buf_size(srcdb
->db_buf
);
1729 DB_DNODE_ENTER(dstdb
);
1730 type
= DB_DNODE(dstdb
)->dn_type
;
1731 DB_DNODE_EXIT(dstdb
);
1733 /* allocated an arc buffer that matches the type of srcdb->db_buf */
1734 if (arc_is_encrypted(srcdb
->db_buf
)) {
1735 arc_get_raw_params(srcdb
->db_buf
, &byteorder
, salt
, iv
, mac
);
1736 abuf
= arc_loan_raw_buf(os
->os_spa
, dmu_objset_id(os
),
1737 byteorder
, salt
, iv
, mac
, type
,
1738 datalen
, arc_buf_lsize(srcdb
->db_buf
),
1739 arc_get_compression(srcdb
->db_buf
));
1741 /* we won't get a compressed db back from dmu_buf_hold() */
1742 ASSERT3U(arc_get_compression(srcdb
->db_buf
),
1743 ==, ZIO_COMPRESS_OFF
);
1744 abuf
= arc_loan_buf(os
->os_spa
,
1745 DMU_OT_IS_METADATA(type
), datalen
);
1748 ASSERT3U(datalen
, ==, arc_buf_size(abuf
));
1750 /* copy the data to the new buffer and assign it to the dstdb */
1751 bcopy(srcdb
->db_buf
->b_data
, abuf
->b_data
, datalen
);
1752 dbuf_assign_arcbuf(dstdb
, abuf
, tx
);
1753 dmu_buf_rele(dst_handle
, FTAG
);
1757 * When possible directly assign passed loaned arc buffer to a dbuf.
1758 * If this is not possible copy the contents of passed arc buf via
1762 dmu_assign_arcbuf_by_dnode(dnode_t
*dn
, uint64_t offset
, arc_buf_t
*buf
,
1766 objset_t
*os
= dn
->dn_objset
;
1767 uint64_t object
= dn
->dn_object
;
1768 uint32_t blksz
= (uint32_t)arc_buf_lsize(buf
);
1771 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
1772 blkid
= dbuf_whichblock(dn
, 0, offset
);
1773 db
= dbuf_hold(dn
, blkid
, FTAG
);
1775 return (SET_ERROR(EIO
));
1776 rw_exit(&dn
->dn_struct_rwlock
);
1779 * We can only assign if the offset is aligned, the arc buf is the
1780 * same size as the dbuf, and the dbuf is not metadata.
1782 if (offset
== db
->db
.db_offset
&& blksz
== db
->db
.db_size
) {
1783 dbuf_assign_arcbuf(db
, buf
, tx
);
1784 dbuf_rele(db
, FTAG
);
1786 /* compressed bufs must always be assignable to their dbuf */
1787 ASSERT3U(arc_get_compression(buf
), ==, ZIO_COMPRESS_OFF
);
1788 ASSERT(!(buf
->b_flags
& ARC_BUF_FLAG_COMPRESSED
));
1791 object
= dn
->dn_object
;
1792 dbuf_rele(db
, FTAG
);
1793 dmu_write(os
, object
, offset
, blksz
, buf
->b_data
, tx
);
1794 dmu_return_arcbuf(buf
);
1795 XUIOSTAT_BUMP(xuiostat_wbuf_copied
);
1802 dmu_assign_arcbuf_by_dbuf(dmu_buf_t
*handle
, uint64_t offset
, arc_buf_t
*buf
,
1806 dmu_buf_impl_t
*dbuf
= (dmu_buf_impl_t
*)handle
;
1808 DB_DNODE_ENTER(dbuf
);
1809 err
= dmu_assign_arcbuf_by_dnode(DB_DNODE(dbuf
), offset
, buf
, tx
);
1810 DB_DNODE_EXIT(dbuf
);
1816 dbuf_dirty_record_t
*dsa_dr
;
1817 dmu_sync_cb_t
*dsa_done
;
1824 dmu_sync_ready(zio_t
*zio
, arc_buf_t
*buf
, void *varg
)
1826 dmu_sync_arg_t
*dsa
= varg
;
1827 dmu_buf_t
*db
= dsa
->dsa_zgd
->zgd_db
;
1828 blkptr_t
*bp
= zio
->io_bp
;
1830 if (zio
->io_error
== 0) {
1831 if (BP_IS_HOLE(bp
)) {
1833 * A block of zeros may compress to a hole, but the
1834 * block size still needs to be known for replay.
1836 BP_SET_LSIZE(bp
, db
->db_size
);
1837 } else if (!BP_IS_EMBEDDED(bp
)) {
1838 ASSERT(BP_GET_LEVEL(bp
) == 0);
1845 dmu_sync_late_arrival_ready(zio_t
*zio
)
1847 dmu_sync_ready(zio
, NULL
, zio
->io_private
);
1852 dmu_sync_done(zio_t
*zio
, arc_buf_t
*buf
, void *varg
)
1854 dmu_sync_arg_t
*dsa
= varg
;
1855 dbuf_dirty_record_t
*dr
= dsa
->dsa_dr
;
1856 dmu_buf_impl_t
*db
= dr
->dr_dbuf
;
1857 zgd_t
*zgd
= dsa
->dsa_zgd
;
1860 * Record the vdev(s) backing this blkptr so they can be flushed after
1861 * the writes for the lwb have completed.
1863 if (zio
->io_error
== 0) {
1864 zil_lwb_add_block(zgd
->zgd_lwb
, zgd
->zgd_bp
);
1867 mutex_enter(&db
->db_mtx
);
1868 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
);
1869 if (zio
->io_error
== 0) {
1870 dr
->dt
.dl
.dr_nopwrite
= !!(zio
->io_flags
& ZIO_FLAG_NOPWRITE
);
1871 if (dr
->dt
.dl
.dr_nopwrite
) {
1872 blkptr_t
*bp
= zio
->io_bp
;
1873 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1874 uint8_t chksum
= BP_GET_CHECKSUM(bp_orig
);
1876 ASSERT(BP_EQUAL(bp
, bp_orig
));
1877 VERIFY(BP_EQUAL(bp
, db
->db_blkptr
));
1878 ASSERT(zio
->io_prop
.zp_compress
!= ZIO_COMPRESS_OFF
);
1879 ASSERT(zio_checksum_table
[chksum
].ci_flags
&
1880 ZCHECKSUM_FLAG_NOPWRITE
);
1882 dr
->dt
.dl
.dr_overridden_by
= *zio
->io_bp
;
1883 dr
->dt
.dl
.dr_override_state
= DR_OVERRIDDEN
;
1884 dr
->dt
.dl
.dr_copies
= zio
->io_prop
.zp_copies
;
1887 * Old style holes are filled with all zeros, whereas
1888 * new-style holes maintain their lsize, type, level,
1889 * and birth time (see zio_write_compress). While we
1890 * need to reset the BP_SET_LSIZE() call that happened
1891 * in dmu_sync_ready for old style holes, we do *not*
1892 * want to wipe out the information contained in new
1893 * style holes. Thus, only zero out the block pointer if
1894 * it's an old style hole.
1896 if (BP_IS_HOLE(&dr
->dt
.dl
.dr_overridden_by
) &&
1897 dr
->dt
.dl
.dr_overridden_by
.blk_birth
== 0)
1898 BP_ZERO(&dr
->dt
.dl
.dr_overridden_by
);
1900 dr
->dt
.dl
.dr_override_state
= DR_NOT_OVERRIDDEN
;
1902 cv_broadcast(&db
->db_changed
);
1903 mutex_exit(&db
->db_mtx
);
1905 dsa
->dsa_done(dsa
->dsa_zgd
, zio
->io_error
);
1907 kmem_free(dsa
, sizeof (*dsa
));
1911 dmu_sync_late_arrival_done(zio_t
*zio
)
1913 blkptr_t
*bp
= zio
->io_bp
;
1914 dmu_sync_arg_t
*dsa
= zio
->io_private
;
1915 blkptr_t
*bp_orig
= &zio
->io_bp_orig
;
1916 zgd_t
*zgd
= dsa
->dsa_zgd
;
1918 if (zio
->io_error
== 0) {
1920 * Record the vdev(s) backing this blkptr so they can be
1921 * flushed after the writes for the lwb have completed.
1923 zil_lwb_add_block(zgd
->zgd_lwb
, zgd
->zgd_bp
);
1925 if (!BP_IS_HOLE(bp
)) {
1926 ASSERT(!(zio
->io_flags
& ZIO_FLAG_NOPWRITE
));
1927 ASSERT(BP_IS_HOLE(bp_orig
) || !BP_EQUAL(bp
, bp_orig
));
1928 ASSERT(zio
->io_bp
->blk_birth
== zio
->io_txg
);
1929 ASSERT(zio
->io_txg
> spa_syncing_txg(zio
->io_spa
));
1930 zio_free(zio
->io_spa
, zio
->io_txg
, zio
->io_bp
);
1934 dmu_tx_commit(dsa
->dsa_tx
);
1936 dsa
->dsa_done(dsa
->dsa_zgd
, zio
->io_error
);
1938 abd_put(zio
->io_abd
);
1939 kmem_free(dsa
, sizeof (*dsa
));
1943 dmu_sync_late_arrival(zio_t
*pio
, objset_t
*os
, dmu_sync_cb_t
*done
, zgd_t
*zgd
,
1944 zio_prop_t
*zp
, zbookmark_phys_t
*zb
)
1946 dmu_sync_arg_t
*dsa
;
1949 tx
= dmu_tx_create(os
);
1950 dmu_tx_hold_space(tx
, zgd
->zgd_db
->db_size
);
1951 if (dmu_tx_assign(tx
, TXG_WAIT
) != 0) {
1953 /* Make zl_get_data do txg_waited_synced() */
1954 return (SET_ERROR(EIO
));
1958 * In order to prevent the zgd's lwb from being free'd prior to
1959 * dmu_sync_late_arrival_done() being called, we have to ensure
1960 * the lwb's "max txg" takes this tx's txg into account.
1962 zil_lwb_add_txg(zgd
->zgd_lwb
, dmu_tx_get_txg(tx
));
1964 dsa
= kmem_alloc(sizeof (dmu_sync_arg_t
), KM_SLEEP
);
1966 dsa
->dsa_done
= done
;
1971 * Since we are currently syncing this txg, it's nontrivial to
1972 * determine what BP to nopwrite against, so we disable nopwrite.
1974 * When syncing, the db_blkptr is initially the BP of the previous
1975 * txg. We can not nopwrite against it because it will be changed
1976 * (this is similar to the non-late-arrival case where the dbuf is
1977 * dirty in a future txg).
1979 * Then dbuf_write_ready() sets bp_blkptr to the location we will write.
1980 * We can not nopwrite against it because although the BP will not
1981 * (typically) be changed, the data has not yet been persisted to this
1984 * Finally, when dbuf_write_done() is called, it is theoretically
1985 * possible to always nopwrite, because the data that was written in
1986 * this txg is the same data that we are trying to write. However we
1987 * would need to check that this dbuf is not dirty in any future
1988 * txg's (as we do in the normal dmu_sync() path). For simplicity, we
1989 * don't nopwrite in this case.
1991 zp
->zp_nopwrite
= B_FALSE
;
1993 zio_nowait(zio_write(pio
, os
->os_spa
, dmu_tx_get_txg(tx
), zgd
->zgd_bp
,
1994 abd_get_from_buf(zgd
->zgd_db
->db_data
, zgd
->zgd_db
->db_size
),
1995 zgd
->zgd_db
->db_size
, zgd
->zgd_db
->db_size
, zp
,
1996 dmu_sync_late_arrival_ready
, NULL
, NULL
, dmu_sync_late_arrival_done
,
1997 dsa
, ZIO_PRIORITY_SYNC_WRITE
, ZIO_FLAG_CANFAIL
, zb
));
2003 * Intent log support: sync the block associated with db to disk.
2004 * N.B. and XXX: the caller is responsible for making sure that the
2005 * data isn't changing while dmu_sync() is writing it.
2009 * EEXIST: this txg has already been synced, so there's nothing to do.
2010 * The caller should not log the write.
2012 * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
2013 * The caller should not log the write.
2015 * EALREADY: this block is already in the process of being synced.
2016 * The caller should track its progress (somehow).
2018 * EIO: could not do the I/O.
2019 * The caller should do a txg_wait_synced().
2021 * 0: the I/O has been initiated.
2022 * The caller should log this blkptr in the done callback.
2023 * It is possible that the I/O will fail, in which case
2024 * the error will be reported to the done callback and
2025 * propagated to pio from zio_done().
2028 dmu_sync(zio_t
*pio
, uint64_t txg
, dmu_sync_cb_t
*done
, zgd_t
*zgd
)
2030 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)zgd
->zgd_db
;
2031 objset_t
*os
= db
->db_objset
;
2032 dsl_dataset_t
*ds
= os
->os_dsl_dataset
;
2033 dbuf_dirty_record_t
*dr
;
2034 dmu_sync_arg_t
*dsa
;
2035 zbookmark_phys_t zb
;
2039 ASSERT(pio
!= NULL
);
2042 SET_BOOKMARK(&zb
, ds
->ds_object
,
2043 db
->db
.db_object
, db
->db_level
, db
->db_blkid
);
2047 dmu_write_policy(os
, dn
, db
->db_level
, WP_DMU_SYNC
, &zp
);
2051 * If we're frozen (running ziltest), we always need to generate a bp.
2053 if (txg
> spa_freeze_txg(os
->os_spa
))
2054 return (dmu_sync_late_arrival(pio
, os
, done
, zgd
, &zp
, &zb
));
2057 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
2058 * and us. If we determine that this txg is not yet syncing,
2059 * but it begins to sync a moment later, that's OK because the
2060 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
2062 mutex_enter(&db
->db_mtx
);
2064 if (txg
<= spa_last_synced_txg(os
->os_spa
)) {
2066 * This txg has already synced. There's nothing to do.
2068 mutex_exit(&db
->db_mtx
);
2069 return (SET_ERROR(EEXIST
));
2072 if (txg
<= spa_syncing_txg(os
->os_spa
)) {
2074 * This txg is currently syncing, so we can't mess with
2075 * the dirty record anymore; just write a new log block.
2077 mutex_exit(&db
->db_mtx
);
2078 return (dmu_sync_late_arrival(pio
, os
, done
, zgd
, &zp
, &zb
));
2081 dr
= db
->db_last_dirty
;
2082 while (dr
&& dr
->dr_txg
!= txg
)
2087 * There's no dr for this dbuf, so it must have been freed.
2088 * There's no need to log writes to freed blocks, so we're done.
2090 mutex_exit(&db
->db_mtx
);
2091 return (SET_ERROR(ENOENT
));
2094 ASSERT(dr
->dr_next
== NULL
|| dr
->dr_next
->dr_txg
< txg
);
2096 if (db
->db_blkptr
!= NULL
) {
2098 * We need to fill in zgd_bp with the current blkptr so that
2099 * the nopwrite code can check if we're writing the same
2100 * data that's already on disk. We can only nopwrite if we
2101 * are sure that after making the copy, db_blkptr will not
2102 * change until our i/o completes. We ensure this by
2103 * holding the db_mtx, and only allowing nopwrite if the
2104 * block is not already dirty (see below). This is verified
2105 * by dmu_sync_done(), which VERIFYs that the db_blkptr has
2108 *zgd
->zgd_bp
= *db
->db_blkptr
;
2112 * Assume the on-disk data is X, the current syncing data (in
2113 * txg - 1) is Y, and the current in-memory data is Z (currently
2116 * We usually want to perform a nopwrite if X and Z are the
2117 * same. However, if Y is different (i.e. the BP is going to
2118 * change before this write takes effect), then a nopwrite will
2119 * be incorrect - we would override with X, which could have
2120 * been freed when Y was written.
2122 * (Note that this is not a concern when we are nop-writing from
2123 * syncing context, because X and Y must be identical, because
2124 * all previous txgs have been synced.)
2126 * Therefore, we disable nopwrite if the current BP could change
2127 * before this TXG. There are two ways it could change: by
2128 * being dirty (dr_next is non-NULL), or by being freed
2129 * (dnode_block_freed()). This behavior is verified by
2130 * zio_done(), which VERIFYs that the override BP is identical
2131 * to the on-disk BP.
2135 if (dr
->dr_next
!= NULL
|| dnode_block_freed(dn
, db
->db_blkid
))
2136 zp
.zp_nopwrite
= B_FALSE
;
2139 ASSERT(dr
->dr_txg
== txg
);
2140 if (dr
->dt
.dl
.dr_override_state
== DR_IN_DMU_SYNC
||
2141 dr
->dt
.dl
.dr_override_state
== DR_OVERRIDDEN
) {
2143 * We have already issued a sync write for this buffer,
2144 * or this buffer has already been synced. It could not
2145 * have been dirtied since, or we would have cleared the state.
2147 mutex_exit(&db
->db_mtx
);
2148 return (SET_ERROR(EALREADY
));
2151 ASSERT(dr
->dt
.dl
.dr_override_state
== DR_NOT_OVERRIDDEN
);
2152 dr
->dt
.dl
.dr_override_state
= DR_IN_DMU_SYNC
;
2153 mutex_exit(&db
->db_mtx
);
2155 dsa
= kmem_alloc(sizeof (dmu_sync_arg_t
), KM_SLEEP
);
2157 dsa
->dsa_done
= done
;
2161 zio_nowait(arc_write(pio
, os
->os_spa
, txg
,
2162 zgd
->zgd_bp
, dr
->dt
.dl
.dr_data
, DBUF_IS_L2CACHEABLE(db
),
2163 &zp
, dmu_sync_ready
, NULL
, NULL
, dmu_sync_done
, dsa
,
2164 ZIO_PRIORITY_SYNC_WRITE
, ZIO_FLAG_CANFAIL
, &zb
));
2170 dmu_object_set_nlevels(objset_t
*os
, uint64_t object
, int nlevels
, dmu_tx_t
*tx
)
2175 err
= dnode_hold(os
, object
, FTAG
, &dn
);
2178 err
= dnode_set_nlevels(dn
, nlevels
, tx
);
2179 dnode_rele(dn
, FTAG
);
2184 dmu_object_set_blocksize(objset_t
*os
, uint64_t object
, uint64_t size
, int ibs
,
2190 err
= dnode_hold(os
, object
, FTAG
, &dn
);
2193 err
= dnode_set_blksz(dn
, size
, ibs
, tx
);
2194 dnode_rele(dn
, FTAG
);
2199 dmu_object_set_maxblkid(objset_t
*os
, uint64_t object
, uint64_t maxblkid
,
2205 err
= dnode_hold(os
, object
, FTAG
, &dn
);
2208 rw_enter(&dn
->dn_struct_rwlock
, RW_WRITER
);
2209 dnode_new_blkid(dn
, maxblkid
, tx
, B_FALSE
, B_TRUE
);
2210 rw_exit(&dn
->dn_struct_rwlock
);
2211 dnode_rele(dn
, FTAG
);
2216 dmu_object_set_checksum(objset_t
*os
, uint64_t object
, uint8_t checksum
,
2222 * Send streams include each object's checksum function. This
2223 * check ensures that the receiving system can understand the
2224 * checksum function transmitted.
2226 ASSERT3U(checksum
, <, ZIO_CHECKSUM_LEGACY_FUNCTIONS
);
2228 VERIFY0(dnode_hold(os
, object
, FTAG
, &dn
));
2229 ASSERT3U(checksum
, <, ZIO_CHECKSUM_FUNCTIONS
);
2230 dn
->dn_checksum
= checksum
;
2231 dnode_setdirty(dn
, tx
);
2232 dnode_rele(dn
, FTAG
);
2236 dmu_object_set_compress(objset_t
*os
, uint64_t object
, uint8_t compress
,
2242 * Send streams include each object's compression function. This
2243 * check ensures that the receiving system can understand the
2244 * compression function transmitted.
2246 ASSERT3U(compress
, <, ZIO_COMPRESS_LEGACY_FUNCTIONS
);
2248 VERIFY0(dnode_hold(os
, object
, FTAG
, &dn
));
2249 dn
->dn_compress
= compress
;
2250 dnode_setdirty(dn
, tx
);
2251 dnode_rele(dn
, FTAG
);
2255 * When the "redundant_metadata" property is set to "most", only indirect
2256 * blocks of this level and higher will have an additional ditto block.
2258 int zfs_redundant_metadata_most_ditto_level
= 2;
2261 dmu_write_policy(objset_t
*os
, dnode_t
*dn
, int level
, int wp
, zio_prop_t
*zp
)
2263 dmu_object_type_t type
= dn
? dn
->dn_type
: DMU_OT_OBJSET
;
2264 boolean_t ismd
= (level
> 0 || DMU_OT_IS_METADATA(type
) ||
2266 enum zio_checksum checksum
= os
->os_checksum
;
2267 enum zio_compress compress
= os
->os_compress
;
2268 enum zio_checksum dedup_checksum
= os
->os_dedup_checksum
;
2269 boolean_t dedup
= B_FALSE
;
2270 boolean_t nopwrite
= B_FALSE
;
2271 boolean_t dedup_verify
= os
->os_dedup_verify
;
2272 boolean_t encrypt
= B_FALSE
;
2273 int copies
= os
->os_copies
;
2276 * We maintain different write policies for each of the following
2279 * 2. preallocated blocks (i.e. level-0 blocks of a dump device)
2280 * 3. all other level 0 blocks
2284 * XXX -- we should design a compression algorithm
2285 * that specializes in arrays of bps.
2287 compress
= zio_compress_select(os
->os_spa
,
2288 ZIO_COMPRESS_ON
, ZIO_COMPRESS_ON
);
2291 * Metadata always gets checksummed. If the data
2292 * checksum is multi-bit correctable, and it's not a
2293 * ZBT-style checksum, then it's suitable for metadata
2294 * as well. Otherwise, the metadata checksum defaults
2297 if (!(zio_checksum_table
[checksum
].ci_flags
&
2298 ZCHECKSUM_FLAG_METADATA
) ||
2299 (zio_checksum_table
[checksum
].ci_flags
&
2300 ZCHECKSUM_FLAG_EMBEDDED
))
2301 checksum
= ZIO_CHECKSUM_FLETCHER_4
;
2303 if (os
->os_redundant_metadata
== ZFS_REDUNDANT_METADATA_ALL
||
2304 (os
->os_redundant_metadata
==
2305 ZFS_REDUNDANT_METADATA_MOST
&&
2306 (level
>= zfs_redundant_metadata_most_ditto_level
||
2307 DMU_OT_IS_METADATA(type
) || (wp
& WP_SPILL
))))
2309 } else if (wp
& WP_NOFILL
) {
2313 * If we're writing preallocated blocks, we aren't actually
2314 * writing them so don't set any policy properties. These
2315 * blocks are currently only used by an external subsystem
2316 * outside of zfs (i.e. dump) and not written by the zio
2319 compress
= ZIO_COMPRESS_OFF
;
2320 checksum
= ZIO_CHECKSUM_NOPARITY
;
2322 compress
= zio_compress_select(os
->os_spa
, dn
->dn_compress
,
2325 checksum
= (dedup_checksum
== ZIO_CHECKSUM_OFF
) ?
2326 zio_checksum_select(dn
->dn_checksum
, checksum
) :
2330 * Determine dedup setting. If we are in dmu_sync(),
2331 * we won't actually dedup now because that's all
2332 * done in syncing context; but we do want to use the
2333 * dedup checkum. If the checksum is not strong
2334 * enough to ensure unique signatures, force
2337 if (dedup_checksum
!= ZIO_CHECKSUM_OFF
) {
2338 dedup
= (wp
& WP_DMU_SYNC
) ? B_FALSE
: B_TRUE
;
2339 if (!(zio_checksum_table
[checksum
].ci_flags
&
2340 ZCHECKSUM_FLAG_DEDUP
))
2341 dedup_verify
= B_TRUE
;
2345 * Enable nopwrite if we have secure enough checksum
2346 * algorithm (see comment in zio_nop_write) and
2347 * compression is enabled. We don't enable nopwrite if
2348 * dedup is enabled as the two features are mutually
2351 nopwrite
= (!dedup
&& (zio_checksum_table
[checksum
].ci_flags
&
2352 ZCHECKSUM_FLAG_NOPWRITE
) &&
2353 compress
!= ZIO_COMPRESS_OFF
&& zfs_nopwrite_enabled
);
2357 * All objects in an encrypted objset are protected from modification
2358 * via a MAC. Encrypted objects store their IV and salt in the last DVA
2359 * in the bp, so we cannot use all copies. Encrypted objects are also
2360 * not subject to nopwrite since writing the same data will still
2361 * result in a new ciphertext. Only encrypted blocks can be dedup'd
2362 * to avoid ambiguity in the dedup code since the DDT does not store
2365 if (os
->os_encrypted
&& (wp
& WP_NOFILL
) == 0) {
2368 if (DMU_OT_IS_ENCRYPTED(type
)) {
2369 copies
= MIN(copies
, SPA_DVAS_PER_BP
- 1);
2376 (type
== DMU_OT_DNODE
|| type
== DMU_OT_OBJSET
)) {
2377 compress
= ZIO_COMPRESS_EMPTY
;
2381 zp
->zp_compress
= compress
;
2382 zp
->zp_checksum
= checksum
;
2383 zp
->zp_type
= (wp
& WP_SPILL
) ? dn
->dn_bonustype
: type
;
2384 zp
->zp_level
= level
;
2385 zp
->zp_copies
= MIN(copies
, spa_max_replication(os
->os_spa
));
2386 zp
->zp_dedup
= dedup
;
2387 zp
->zp_dedup_verify
= dedup
&& dedup_verify
;
2388 zp
->zp_nopwrite
= nopwrite
;
2389 zp
->zp_zpl_smallblk
= DMU_OT_IS_FILE(zp
->zp_type
) ?
2390 os
->os_zpl_special_smallblock
: 0;
2391 zp
->zp_encrypt
= encrypt
;
2392 zp
->zp_byteorder
= ZFS_HOST_BYTEORDER
;
2393 bzero(zp
->zp_salt
, ZIO_DATA_SALT_LEN
);
2394 bzero(zp
->zp_iv
, ZIO_DATA_IV_LEN
);
2395 bzero(zp
->zp_mac
, ZIO_DATA_MAC_LEN
);
2399 dmu_offset_next(objset_t
*os
, uint64_t object
, boolean_t hole
, uint64_t *off
)
2405 * Sync any current changes before
2406 * we go trundling through the block pointers.
2408 err
= dmu_object_wait_synced(os
, object
);
2413 err
= dnode_hold(os
, object
, FTAG
, &dn
);
2418 err
= dnode_next_offset(dn
, (hole
? DNODE_FIND_HOLE
: 0), off
, 1, 1, 0);
2419 dnode_rele(dn
, FTAG
);
2425 * Given the ZFS object, if it contains any dirty nodes
2426 * this function flushes all dirty blocks to disk. This
2427 * ensures the DMU object info is updated. A more efficient
2428 * future version might just find the TXG with the maximum
2429 * ID and wait for that to be synced.
2432 dmu_object_wait_synced(objset_t
*os
, uint64_t object
)
2437 error
= dnode_hold(os
, object
, FTAG
, &dn
);
2442 mutex_enter(&dn
->dn_mtx
);
2443 for (i
= 0; i
< TXG_SIZE
; i
++) {
2444 if (list_link_active(&dn
->dn_dirty_link
[i
]) ||
2445 !list_is_empty(&dn
->dn_dirty_records
[i
])) {
2449 mutex_exit(&dn
->dn_mtx
);
2451 dnode_rele(dn
, FTAG
);
2452 if (i
!= TXG_SIZE
) {
2453 txg_wait_synced(dmu_objset_pool(os
), 0);
2460 dmu_object_info_from_dnode(dnode_t
*dn
, dmu_object_info_t
*doi
)
2464 rw_enter(&dn
->dn_struct_rwlock
, RW_READER
);
2465 mutex_enter(&dn
->dn_mtx
);
2469 doi
->doi_data_block_size
= dn
->dn_datablksz
;
2470 doi
->doi_metadata_block_size
= dn
->dn_indblkshift
?
2471 1ULL << dn
->dn_indblkshift
: 0;
2472 doi
->doi_type
= dn
->dn_type
;
2473 doi
->doi_bonus_type
= dn
->dn_bonustype
;
2474 doi
->doi_bonus_size
= dn
->dn_bonuslen
;
2475 doi
->doi_dnodesize
= dn
->dn_num_slots
<< DNODE_SHIFT
;
2476 doi
->doi_indirection
= dn
->dn_nlevels
;
2477 doi
->doi_checksum
= dn
->dn_checksum
;
2478 doi
->doi_compress
= dn
->dn_compress
;
2479 doi
->doi_nblkptr
= dn
->dn_nblkptr
;
2480 doi
->doi_physical_blocks_512
= (DN_USED_BYTES(dnp
) + 256) >> 9;
2481 doi
->doi_max_offset
= (dn
->dn_maxblkid
+ 1) * dn
->dn_datablksz
;
2482 doi
->doi_fill_count
= 0;
2483 for (int i
= 0; i
< dnp
->dn_nblkptr
; i
++)
2484 doi
->doi_fill_count
+= BP_GET_FILL(&dnp
->dn_blkptr
[i
]);
2486 mutex_exit(&dn
->dn_mtx
);
2487 rw_exit(&dn
->dn_struct_rwlock
);
2491 * Get information on a DMU object.
2492 * If doi is NULL, just indicates whether the object exists.
2495 dmu_object_info(objset_t
*os
, uint64_t object
, dmu_object_info_t
*doi
)
2498 int err
= dnode_hold(os
, object
, FTAG
, &dn
);
2504 dmu_object_info_from_dnode(dn
, doi
);
2506 dnode_rele(dn
, FTAG
);
2511 * As above, but faster; can be used when you have a held dbuf in hand.
2514 dmu_object_info_from_db(dmu_buf_t
*db_fake
, dmu_object_info_t
*doi
)
2516 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2519 dmu_object_info_from_dnode(DB_DNODE(db
), doi
);
2524 * Faster still when you only care about the size.
2525 * This is specifically optimized for zfs_getattr().
2528 dmu_object_size_from_db(dmu_buf_t
*db_fake
, uint32_t *blksize
,
2529 u_longlong_t
*nblk512
)
2531 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2537 *blksize
= dn
->dn_datablksz
;
2538 /* add in number of slots used for the dnode itself */
2539 *nblk512
= ((DN_USED_BYTES(dn
->dn_phys
) + SPA_MINBLOCKSIZE
/2) >>
2540 SPA_MINBLOCKSHIFT
) + dn
->dn_num_slots
;
2545 dmu_object_dnsize_from_db(dmu_buf_t
*db_fake
, int *dnsize
)
2547 dmu_buf_impl_t
*db
= (dmu_buf_impl_t
*)db_fake
;
2552 *dnsize
= dn
->dn_num_slots
<< DNODE_SHIFT
;
2557 byteswap_uint64_array(void *vbuf
, size_t size
)
2559 uint64_t *buf
= vbuf
;
2560 size_t count
= size
>> 3;
2563 ASSERT((size
& 7) == 0);
2565 for (i
= 0; i
< count
; i
++)
2566 buf
[i
] = BSWAP_64(buf
[i
]);
2570 byteswap_uint32_array(void *vbuf
, size_t size
)
2572 uint32_t *buf
= vbuf
;
2573 size_t count
= size
>> 2;
2576 ASSERT((size
& 3) == 0);
2578 for (i
= 0; i
< count
; i
++)
2579 buf
[i
] = BSWAP_32(buf
[i
]);
2583 byteswap_uint16_array(void *vbuf
, size_t size
)
2585 uint16_t *buf
= vbuf
;
2586 size_t count
= size
>> 1;
2589 ASSERT((size
& 1) == 0);
2591 for (i
= 0; i
< count
; i
++)
2592 buf
[i
] = BSWAP_16(buf
[i
]);
2597 byteswap_uint8_array(void *vbuf
, size_t size
)
2619 arc_fini(); /* arc depends on l2arc, so arc must go first */