1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
27 * Copyright (c) 2016, Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
29 * Copyright (c) 2019 Datto Inc.
30 * Copyright (c) 2019, 2023, Klara Inc.
31 * Copyright (c) 2019, Allan Jude
32 * Copyright (c) 2022 Hewlett Packard Enterprise Development LP.
33 * Copyright (c) 2021, 2022 by Pawel Jakub Dawidek
34 */
35
36 #include <sys/dmu.h>
37 #include <sys/dmu_impl.h>
38 #include <sys/dmu_tx.h>
39 #include <sys/dbuf.h>
40 #include <sys/dnode.h>
41 #include <sys/zfs_context.h>
42 #include <sys/dmu_objset.h>
43 #include <sys/dmu_traverse.h>
44 #include <sys/dsl_dataset.h>
45 #include <sys/dsl_dir.h>
46 #include <sys/dsl_pool.h>
47 #include <sys/dsl_synctask.h>
48 #include <sys/dsl_prop.h>
49 #include <sys/dmu_zfetch.h>
50 #include <sys/zfs_ioctl.h>
51 #include <sys/zap.h>
52 #include <sys/zio_checksum.h>
53 #include <sys/zio_compress.h>
54 #include <sys/sa.h>
55 #include <sys/zfeature.h>
56 #include <sys/abd.h>
57 #include <sys/brt.h>
58 #include <sys/trace_zfs.h>
59 #include <sys/zfs_racct.h>
60 #include <sys/zfs_rlock.h>
61 #ifdef _KERNEL
62 #include <sys/vmsystm.h>
63 #include <sys/zfs_znode.h>
64 #endif
65
66 /*
67 * Enable/disable nopwrite feature.
68 */
69 static int zfs_nopwrite_enabled = 1;
70
71 /*
72 * Tunable to control percentage of dirtied L1 blocks from frees allowed into
73 * one TXG. After this threshold is crossed, additional dirty blocks from frees
74 * will wait until the next TXG.
75 * A value of zero will disable this throttle.
76 */
77 static uint_t zfs_per_txg_dirty_frees_percent = 30;
78
79 /*
80 * Enable/disable forcing txg sync when dirty checking for holes with lseek().
81 * By default this is enabled to ensure accurate hole reporting, it can result
82 * in a significant performance penalty for lseek(SEEK_HOLE) heavy workloads.
83 * Disabling this option will result in holes never being reported in dirty
84 * files which is always safe.
85 */
86 static int zfs_dmu_offset_next_sync = 1;
87
88 /*
89 * Limit the amount we can prefetch with one call to this amount. This
90 * helps to limit the amount of memory that can be used by prefetching.
91 * Larger objects should be prefetched a bit at a time.
92 */
93 #ifdef _ILP32
94 uint_t dmu_prefetch_max = 8 * 1024 * 1024;
95 #else
96 uint_t dmu_prefetch_max = 8 * SPA_MAXBLOCKSIZE;
97 #endif
98
99 /*
100 * Override copies= for dedup state objects. 0 means the traditional behaviour
101 * (ie the default for the containing objset ie 3 for the MOS).
102 */
103 uint_t dmu_ddt_copies = 0;
104
105 const dmu_object_type_info_t dmu_ot[DMU_OT_NUMTYPES] = {
106 {DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "unallocated" },
107 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "object directory" },
108 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "object array" },
109 {DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "packed nvlist" },
110 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "packed nvlist size" },
111 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj" },
112 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj header" },
113 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA space map header" },
114 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA space map" },
115 {DMU_BSWAP_UINT64, TRUE, FALSE, TRUE, "ZIL intent log" },
116 {DMU_BSWAP_DNODE, TRUE, FALSE, TRUE, "DMU dnode" },
117 {DMU_BSWAP_OBJSET, TRUE, TRUE, FALSE, "DMU objset" },
118 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL directory" },
119 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL directory child map"},
120 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dataset snap map" },
121 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL props" },
122 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL dataset" },
123 {DMU_BSWAP_ZNODE, TRUE, FALSE, FALSE, "ZFS znode" },
124 {DMU_BSWAP_OLDACL, TRUE, FALSE, TRUE, "ZFS V0 ACL" },
125 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "ZFS plain file" },
126 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS directory" },
127 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "ZFS master node" },
128 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS delete queue" },
129 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "zvol object" },
130 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "zvol prop" },
131 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "other uint8[]" },
132 {DMU_BSWAP_UINT64, FALSE, FALSE, TRUE, "other uint64[]" },
133 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "other ZAP" },
134 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "persistent error log" },
135 {DMU_BSWAP_UINT8, TRUE, FALSE, FALSE, "SPA history" },
136 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "SPA history offsets" },
137 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "Pool properties" },
138 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL permissions" },
139 {DMU_BSWAP_ACL, TRUE, FALSE, TRUE, "ZFS ACL" },
140 {DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "ZFS SYSACL" },
141 {DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "FUID table" },
142 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "FUID table size" },
143 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dataset next clones"},
144 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "scan work queue" },
145 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS user/group/project used" },
146 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "ZFS user/group/project quota"},
147 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "snapshot refcount tags"},
148 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "DDT ZAP algorithm" },
149 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "DDT statistics" },
150 {DMU_BSWAP_UINT8, TRUE, FALSE, TRUE, "System attributes" },
151 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA master node" },
152 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA attr registration" },
153 {DMU_BSWAP_ZAP, TRUE, FALSE, TRUE, "SA attr layouts" },
154 {DMU_BSWAP_ZAP, TRUE, FALSE, FALSE, "scan translations" },
155 {DMU_BSWAP_UINT8, FALSE, FALSE, TRUE, "deduplicated block" },
156 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL deadlist map" },
157 {DMU_BSWAP_UINT64, TRUE, TRUE, FALSE, "DSL deadlist map hdr" },
158 {DMU_BSWAP_ZAP, TRUE, TRUE, FALSE, "DSL dir clones" },
159 {DMU_BSWAP_UINT64, TRUE, FALSE, FALSE, "bpobj subobj" }
160 };
161
162 dmu_object_byteswap_info_t dmu_ot_byteswap[DMU_BSWAP_NUMFUNCS] = {
163 { byteswap_uint8_array, "uint8" },
164 { byteswap_uint16_array, "uint16" },
165 { byteswap_uint32_array, "uint32" },
166 { byteswap_uint64_array, "uint64" },
167 { zap_byteswap, "zap" },
168 { dnode_buf_byteswap, "dnode" },
169 { dmu_objset_byteswap, "objset" },
170 { zfs_znode_byteswap, "znode" },
171 { zfs_oldacl_byteswap, "oldacl" },
172 { zfs_acl_byteswap, "acl" }
173 };
174
175 int
dmu_buf_hold_noread_by_dnode(dnode_t * dn,uint64_t offset,const void * tag,dmu_buf_t ** dbp)176 dmu_buf_hold_noread_by_dnode(dnode_t *dn, uint64_t offset,
177 const void *tag, dmu_buf_t **dbp)
178 {
179 uint64_t blkid;
180 dmu_buf_impl_t *db;
181
182 rw_enter(&dn->dn_struct_rwlock, RW_READER);
183 blkid = dbuf_whichblock(dn, 0, offset);
184 db = dbuf_hold(dn, blkid, tag);
185 rw_exit(&dn->dn_struct_rwlock);
186
187 if (db == NULL) {
188 *dbp = NULL;
189 return (SET_ERROR(EIO));
190 }
191
192 *dbp = &db->db;
193 return (0);
194 }
195
196 int
dmu_buf_hold_noread(objset_t * os,uint64_t object,uint64_t offset,const void * tag,dmu_buf_t ** dbp)197 dmu_buf_hold_noread(objset_t *os, uint64_t object, uint64_t offset,
198 const void *tag, dmu_buf_t **dbp)
199 {
200 dnode_t *dn;
201 uint64_t blkid;
202 dmu_buf_impl_t *db;
203 int err;
204
205 err = dnode_hold(os, object, FTAG, &dn);
206 if (err)
207 return (err);
208 rw_enter(&dn->dn_struct_rwlock, RW_READER);
209 blkid = dbuf_whichblock(dn, 0, offset);
210 db = dbuf_hold(dn, blkid, tag);
211 rw_exit(&dn->dn_struct_rwlock);
212 dnode_rele(dn, FTAG);
213
214 if (db == NULL) {
215 *dbp = NULL;
216 return (SET_ERROR(EIO));
217 }
218
219 *dbp = &db->db;
220 return (err);
221 }
222
223 int
dmu_buf_hold_by_dnode(dnode_t * dn,uint64_t offset,const void * tag,dmu_buf_t ** dbp,dmu_flags_t flags)224 dmu_buf_hold_by_dnode(dnode_t *dn, uint64_t offset,
225 const void *tag, dmu_buf_t **dbp, dmu_flags_t flags)
226 {
227 int err;
228
229 err = dmu_buf_hold_noread_by_dnode(dn, offset, tag, dbp);
230 if (err == 0) {
231 dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp);
232 err = dbuf_read(db, NULL, flags | DB_RF_CANFAIL);
233 if (err != 0) {
234 dbuf_rele(db, tag);
235 *dbp = NULL;
236 }
237 }
238
239 return (err);
240 }
241
242 int
dmu_buf_hold(objset_t * os,uint64_t object,uint64_t offset,const void * tag,dmu_buf_t ** dbp,dmu_flags_t flags)243 dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
244 const void *tag, dmu_buf_t **dbp, dmu_flags_t flags)
245 {
246 int err;
247
248 err = dmu_buf_hold_noread(os, object, offset, tag, dbp);
249 if (err == 0) {
250 dmu_buf_impl_t *db = (dmu_buf_impl_t *)(*dbp);
251 err = dbuf_read(db, NULL, flags | DB_RF_CANFAIL);
252 if (err != 0) {
253 dbuf_rele(db, tag);
254 *dbp = NULL;
255 }
256 }
257
258 return (err);
259 }
260
261 int
dmu_bonus_max(void)262 dmu_bonus_max(void)
263 {
264 return (DN_OLD_MAX_BONUSLEN);
265 }
266
267 int
dmu_set_bonus(dmu_buf_t * db_fake,int newsize,dmu_tx_t * tx)268 dmu_set_bonus(dmu_buf_t *db_fake, int newsize, dmu_tx_t *tx)
269 {
270 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
271 dnode_t *dn;
272 int error;
273
274 if (newsize < 0 || newsize > db_fake->db_size)
275 return (SET_ERROR(EINVAL));
276
277 DB_DNODE_ENTER(db);
278 dn = DB_DNODE(db);
279
280 if (dn->dn_bonus != db) {
281 error = SET_ERROR(EINVAL);
282 } else {
283 dnode_setbonuslen(dn, newsize, tx);
284 error = 0;
285 }
286
287 DB_DNODE_EXIT(db);
288 return (error);
289 }
290
291 int
dmu_set_bonustype(dmu_buf_t * db_fake,dmu_object_type_t type,dmu_tx_t * tx)292 dmu_set_bonustype(dmu_buf_t *db_fake, dmu_object_type_t type, dmu_tx_t *tx)
293 {
294 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
295 dnode_t *dn;
296 int error;
297
298 if (!DMU_OT_IS_VALID(type))
299 return (SET_ERROR(EINVAL));
300
301 DB_DNODE_ENTER(db);
302 dn = DB_DNODE(db);
303
304 if (dn->dn_bonus != db) {
305 error = SET_ERROR(EINVAL);
306 } else {
307 dnode_setbonus_type(dn, type, tx);
308 error = 0;
309 }
310
311 DB_DNODE_EXIT(db);
312 return (error);
313 }
314
315 dmu_object_type_t
dmu_get_bonustype(dmu_buf_t * db_fake)316 dmu_get_bonustype(dmu_buf_t *db_fake)
317 {
318 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
319 dmu_object_type_t type;
320
321 DB_DNODE_ENTER(db);
322 type = DB_DNODE(db)->dn_bonustype;
323 DB_DNODE_EXIT(db);
324
325 return (type);
326 }
327
328 int
dmu_rm_spill(objset_t * os,uint64_t object,dmu_tx_t * tx)329 dmu_rm_spill(objset_t *os, uint64_t object, dmu_tx_t *tx)
330 {
331 dnode_t *dn;
332 int error;
333
334 error = dnode_hold(os, object, FTAG, &dn);
335 dbuf_rm_spill(dn, tx);
336 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
337 dnode_rm_spill(dn, tx);
338 rw_exit(&dn->dn_struct_rwlock);
339 dnode_rele(dn, FTAG);
340 return (error);
341 }
342
343 /*
344 * Lookup and hold the bonus buffer for the provided dnode. If the dnode
345 * has not yet been allocated a new bonus dbuf a will be allocated.
346 * Returns ENOENT, EIO, or 0.
347 */
dmu_bonus_hold_by_dnode(dnode_t * dn,const void * tag,dmu_buf_t ** dbp,dmu_flags_t flags)348 int dmu_bonus_hold_by_dnode(dnode_t *dn, const void *tag, dmu_buf_t **dbp,
349 dmu_flags_t flags)
350 {
351 dmu_buf_impl_t *db;
352 int error;
353
354 rw_enter(&dn->dn_struct_rwlock, RW_READER);
355 if (dn->dn_bonus == NULL) {
356 if (!rw_tryupgrade(&dn->dn_struct_rwlock)) {
357 rw_exit(&dn->dn_struct_rwlock);
358 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
359 }
360 if (dn->dn_bonus == NULL)
361 dbuf_create_bonus(dn);
362 }
363 db = dn->dn_bonus;
364
365 /* as long as the bonus buf is held, the dnode will be held */
366 if (zfs_refcount_add(&db->db_holds, tag) == 1) {
367 VERIFY(dnode_add_ref(dn, db));
368 atomic_inc_32(&dn->dn_dbufs_count);
369 }
370
371 /*
372 * Wait to drop dn_struct_rwlock until after adding the bonus dbuf's
373 * hold and incrementing the dbuf count to ensure that dnode_move() sees
374 * a dnode hold for every dbuf.
375 */
376 rw_exit(&dn->dn_struct_rwlock);
377
378 error = dbuf_read(db, NULL, flags | DB_RF_CANFAIL);
379 if (error) {
380 dnode_evict_bonus(dn);
381 dbuf_rele(db, tag);
382 *dbp = NULL;
383 return (error);
384 }
385
386 *dbp = &db->db;
387 return (0);
388 }
389
390 int
dmu_bonus_hold(objset_t * os,uint64_t object,const void * tag,dmu_buf_t ** dbp)391 dmu_bonus_hold(objset_t *os, uint64_t object, const void *tag, dmu_buf_t **dbp)
392 {
393 dnode_t *dn;
394 int error;
395
396 error = dnode_hold(os, object, FTAG, &dn);
397 if (error)
398 return (error);
399
400 error = dmu_bonus_hold_by_dnode(dn, tag, dbp, DMU_READ_NO_PREFETCH);
401 dnode_rele(dn, FTAG);
402
403 return (error);
404 }
405
406 /*
407 * returns ENOENT, EIO, or 0.
408 *
409 * This interface will allocate a blank spill dbuf when a spill blk
410 * doesn't already exist on the dnode.
411 *
412 * if you only want to find an already existing spill db, then
413 * dmu_spill_hold_existing() should be used.
414 */
415 int
dmu_spill_hold_by_dnode(dnode_t * dn,dmu_flags_t flags,const void * tag,dmu_buf_t ** dbp)416 dmu_spill_hold_by_dnode(dnode_t *dn, dmu_flags_t flags, const void *tag,
417 dmu_buf_t **dbp)
418 {
419 dmu_buf_impl_t *db = NULL;
420 int err;
421
422 if ((flags & DB_RF_HAVESTRUCT) == 0)
423 rw_enter(&dn->dn_struct_rwlock, RW_READER);
424
425 db = dbuf_hold(dn, DMU_SPILL_BLKID, tag);
426
427 if ((flags & DB_RF_HAVESTRUCT) == 0)
428 rw_exit(&dn->dn_struct_rwlock);
429
430 if (db == NULL) {
431 *dbp = NULL;
432 return (SET_ERROR(EIO));
433 }
434 err = dbuf_read(db, NULL, flags);
435 if (err == 0)
436 *dbp = &db->db;
437 else {
438 dbuf_rele(db, tag);
439 *dbp = NULL;
440 }
441 return (err);
442 }
443
444 int
dmu_spill_hold_existing(dmu_buf_t * bonus,const void * tag,dmu_buf_t ** dbp)445 dmu_spill_hold_existing(dmu_buf_t *bonus, const void *tag, dmu_buf_t **dbp)
446 {
447 dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
448 dnode_t *dn;
449 int err;
450
451 DB_DNODE_ENTER(db);
452 dn = DB_DNODE(db);
453
454 if (spa_version(dn->dn_objset->os_spa) < SPA_VERSION_SA) {
455 err = SET_ERROR(EINVAL);
456 } else {
457 rw_enter(&dn->dn_struct_rwlock, RW_READER);
458
459 if (!dn->dn_have_spill) {
460 err = SET_ERROR(ENOENT);
461 } else {
462 err = dmu_spill_hold_by_dnode(dn,
463 DB_RF_HAVESTRUCT | DB_RF_CANFAIL, tag, dbp);
464 }
465
466 rw_exit(&dn->dn_struct_rwlock);
467 }
468
469 DB_DNODE_EXIT(db);
470 return (err);
471 }
472
473 int
dmu_spill_hold_by_bonus(dmu_buf_t * bonus,dmu_flags_t flags,const void * tag,dmu_buf_t ** dbp)474 dmu_spill_hold_by_bonus(dmu_buf_t *bonus, dmu_flags_t flags, const void *tag,
475 dmu_buf_t **dbp)
476 {
477 dmu_buf_impl_t *db = (dmu_buf_impl_t *)bonus;
478 int err;
479
480 DB_DNODE_ENTER(db);
481 err = dmu_spill_hold_by_dnode(DB_DNODE(db), flags, tag, dbp);
482 DB_DNODE_EXIT(db);
483
484 return (err);
485 }
486
487 /*
488 * Note: longer-term, we should modify all of the dmu_buf_*() interfaces
489 * to take a held dnode rather than <os, object> -- the lookup is wasteful,
490 * and can induce severe lock contention when writing to several files
491 * whose dnodes are in the same block.
492 */
493 int
dmu_buf_hold_array_by_dnode(dnode_t * dn,uint64_t offset,uint64_t length,boolean_t read,const void * tag,int * numbufsp,dmu_buf_t *** dbpp,dmu_flags_t flags)494 dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length,
495 boolean_t read, const void *tag, int *numbufsp, dmu_buf_t ***dbpp,
496 dmu_flags_t flags)
497 {
498 dmu_buf_t **dbp;
499 zstream_t *zs = NULL;
500 uint64_t blkid, nblks, i;
501 dmu_flags_t dbuf_flags;
502 int err;
503 zio_t *zio = NULL;
504 boolean_t missed = B_FALSE;
505
506 ASSERT(!read || length <= DMU_MAX_ACCESS);
507
508 /*
509 * Note: We directly notify the prefetch code of this read, so that
510 * we can tell it about the multi-block read. dbuf_read() only knows
511 * about the one block it is accessing.
512 */
513 dbuf_flags = (flags & ~DMU_READ_PREFETCH) | DMU_READ_NO_PREFETCH |
514 DB_RF_CANFAIL | DB_RF_NEVERWAIT | DB_RF_HAVESTRUCT;
515
516 rw_enter(&dn->dn_struct_rwlock, RW_READER);
517 if (dn->dn_datablkshift) {
518 int blkshift = dn->dn_datablkshift;
519 nblks = (P2ROUNDUP(offset + length, 1ULL << blkshift) -
520 P2ALIGN_TYPED(offset, 1ULL << blkshift, uint64_t))
521 >> blkshift;
522 } else {
523 if (offset + length > dn->dn_datablksz) {
524 zfs_panic_recover("zfs: accessing past end of object "
525 "%llx/%llx (size=%u access=%llu+%llu)",
526 (longlong_t)dn->dn_objset->
527 os_dsl_dataset->ds_object,
528 (longlong_t)dn->dn_object, dn->dn_datablksz,
529 (longlong_t)offset, (longlong_t)length);
530 rw_exit(&dn->dn_struct_rwlock);
531 return (SET_ERROR(EIO));
532 }
533 nblks = 1;
534 }
535 dbp = kmem_zalloc(sizeof (dmu_buf_t *) * nblks, KM_SLEEP);
536
537 if (read)
538 zio = zio_root(dn->dn_objset->os_spa, NULL, NULL,
539 ZIO_FLAG_CANFAIL);
540 blkid = dbuf_whichblock(dn, 0, offset);
541 if ((flags & DMU_READ_NO_PREFETCH) == 0) {
542 /*
543 * Prepare the zfetch before initiating the demand reads, so
544 * that if multiple threads block on same indirect block, we
545 * base predictions on the original less racy request order.
546 */
547 zs = dmu_zfetch_prepare(&dn->dn_zfetch, blkid, nblks,
548 read && !(flags & DMU_DIRECTIO), B_TRUE);
549 }
550 for (i = 0; i < nblks; i++) {
551 dmu_buf_impl_t *db = dbuf_hold(dn, blkid + i, tag);
552 if (db == NULL) {
553 if (zs) {
554 dmu_zfetch_run(&dn->dn_zfetch, zs, missed,
555 B_TRUE, (flags & DMU_UNCACHEDIO));
556 }
557 rw_exit(&dn->dn_struct_rwlock);
558 dmu_buf_rele_array(dbp, nblks, tag);
559 if (read)
560 zio_nowait(zio);
561 return (SET_ERROR(EIO));
562 }
563
564 /*
565 * Initiate async demand data read.
566 * We check the db_state after calling dbuf_read() because
567 * (1) dbuf_read() may change the state to CACHED due to a
568 * hit in the ARC, and (2) on a cache miss, a child will
569 * have been added to "zio" but not yet completed, so the
570 * state will not yet be CACHED.
571 */
572 if (read) {
573 if (i == nblks - 1 && blkid + i < dn->dn_maxblkid &&
574 offset + length < db->db.db_offset +
575 db->db.db_size) {
576 if (offset <= db->db.db_offset)
577 dbuf_flags |= DMU_PARTIAL_FIRST;
578 else
579 dbuf_flags |= DMU_PARTIAL_MORE;
580 }
581 (void) dbuf_read(db, zio, dbuf_flags);
582 if (db->db_state != DB_CACHED)
583 missed = B_TRUE;
584 }
585 dbp[i] = &db->db;
586 }
587
588 /*
589 * If we are doing O_DIRECT we still hold the dbufs, even for reads,
590 * but we do not issue any reads here. We do not want to account for
591 * writes in this case.
592 *
593 * O_DIRECT write/read accounting takes place in
594 * dmu_{write/read}_abd().
595 */
596 if (!read && ((flags & DMU_DIRECTIO) == 0))
597 zfs_racct_write(dn->dn_objset->os_spa, length, nblks, flags);
598
599 if (zs) {
600 dmu_zfetch_run(&dn->dn_zfetch, zs, missed, B_TRUE,
601 (flags & DMU_UNCACHEDIO));
602 }
603 rw_exit(&dn->dn_struct_rwlock);
604
605 if (read) {
606 /* wait for async read i/o */
607 err = zio_wait(zio);
608 if (err) {
609 dmu_buf_rele_array(dbp, nblks, tag);
610 return (err);
611 }
612
613 /* wait for other io to complete */
614 for (i = 0; i < nblks; i++) {
615 dmu_buf_impl_t *db = (dmu_buf_impl_t *)dbp[i];
616 mutex_enter(&db->db_mtx);
617 while (db->db_state == DB_READ ||
618 db->db_state == DB_FILL)
619 cv_wait(&db->db_changed, &db->db_mtx);
620 if (db->db_state == DB_UNCACHED)
621 err = SET_ERROR(EIO);
622 mutex_exit(&db->db_mtx);
623 if (err) {
624 dmu_buf_rele_array(dbp, nblks, tag);
625 return (err);
626 }
627 }
628 }
629
630 *numbufsp = nblks;
631 *dbpp = dbp;
632 return (0);
633 }
634
635 int
dmu_buf_hold_array(objset_t * os,uint64_t object,uint64_t offset,uint64_t length,int read,const void * tag,int * numbufsp,dmu_buf_t *** dbpp)636 dmu_buf_hold_array(objset_t *os, uint64_t object, uint64_t offset,
637 uint64_t length, int read, const void *tag, int *numbufsp,
638 dmu_buf_t ***dbpp)
639 {
640 dnode_t *dn;
641 int err;
642
643 err = dnode_hold(os, object, FTAG, &dn);
644 if (err)
645 return (err);
646
647 err = dmu_buf_hold_array_by_dnode(dn, offset, length, read, tag,
648 numbufsp, dbpp, DMU_READ_PREFETCH);
649
650 dnode_rele(dn, FTAG);
651
652 return (err);
653 }
654
655 int
dmu_buf_hold_array_by_bonus(dmu_buf_t * db_fake,uint64_t offset,uint64_t length,boolean_t read,const void * tag,int * numbufsp,dmu_buf_t *** dbpp)656 dmu_buf_hold_array_by_bonus(dmu_buf_t *db_fake, uint64_t offset,
657 uint64_t length, boolean_t read, const void *tag, int *numbufsp,
658 dmu_buf_t ***dbpp)
659 {
660 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
661 int err;
662
663 DB_DNODE_ENTER(db);
664 err = dmu_buf_hold_array_by_dnode(DB_DNODE(db), offset, length, read,
665 tag, numbufsp, dbpp, DMU_READ_PREFETCH);
666 DB_DNODE_EXIT(db);
667
668 return (err);
669 }
670
671 void
dmu_buf_rele_array(dmu_buf_t ** dbp_fake,int numbufs,const void * tag)672 dmu_buf_rele_array(dmu_buf_t **dbp_fake, int numbufs, const void *tag)
673 {
674 int i;
675 dmu_buf_impl_t **dbp = (dmu_buf_impl_t **)dbp_fake;
676
677 if (numbufs == 0)
678 return;
679
680 for (i = 0; i < numbufs; i++) {
681 if (dbp[i])
682 dbuf_rele(dbp[i], tag);
683 }
684
685 kmem_free(dbp, sizeof (dmu_buf_t *) * numbufs);
686 }
687
688 /*
689 * Issue prefetch I/Os for the given blocks. If level is greater than 0, the
690 * indirect blocks prefetched will be those that point to the blocks containing
691 * the data starting at offset, and continuing to offset + len. If the range
692 * is too long, prefetch the first dmu_prefetch_max bytes as requested, while
693 * for the rest only a higher level, also fitting within dmu_prefetch_max. It
694 * should primarily help random reads, since for long sequential reads there is
695 * a speculative prefetcher.
696 *
697 * Note that if the indirect blocks above the blocks being prefetched are not
698 * in cache, they will be asynchronously read in. Dnode read by dnode_hold()
699 * is currently synchronous.
700 */
701 void
dmu_prefetch(objset_t * os,uint64_t object,int64_t level,uint64_t offset,uint64_t len,zio_priority_t pri)702 dmu_prefetch(objset_t *os, uint64_t object, int64_t level, uint64_t offset,
703 uint64_t len, zio_priority_t pri)
704 {
705 dnode_t *dn;
706
707 if (dmu_prefetch_max == 0 || len == 0) {
708 dmu_prefetch_dnode(os, object, pri);
709 return;
710 }
711
712 if (dnode_hold(os, object, FTAG, &dn) != 0)
713 return;
714
715 dmu_prefetch_by_dnode(dn, level, offset, len, pri);
716
717 dnode_rele(dn, FTAG);
718 }
719
720 void
dmu_prefetch_by_dnode(dnode_t * dn,int64_t level,uint64_t offset,uint64_t len,zio_priority_t pri)721 dmu_prefetch_by_dnode(dnode_t *dn, int64_t level, uint64_t offset,
722 uint64_t len, zio_priority_t pri)
723 {
724 int64_t level2 = level;
725 uint64_t start, end, start2, end2;
726
727 /*
728 * Depending on len we may do two prefetches: blocks [start, end) at
729 * level, and following blocks [start2, end2) at higher level2.
730 */
731 rw_enter(&dn->dn_struct_rwlock, RW_READER);
732 if (dn->dn_datablkshift != 0) {
733
734 /*
735 * Limit prefetch to present blocks.
736 */
737 uint64_t size = (dn->dn_maxblkid + 1) << dn->dn_datablkshift;
738 if (offset >= size) {
739 rw_exit(&dn->dn_struct_rwlock);
740 return;
741 }
742 if (offset + len < offset || offset + len > size)
743 len = size - offset;
744
745 /*
746 * The object has multiple blocks. Calculate the full range
747 * of blocks [start, end2) and then split it into two parts,
748 * so that the first [start, end) fits into dmu_prefetch_max.
749 */
750 start = dbuf_whichblock(dn, level, offset);
751 end2 = dbuf_whichblock(dn, level, offset + len - 1) + 1;
752 uint8_t ibs = dn->dn_indblkshift;
753 uint8_t bs = (level == 0) ? dn->dn_datablkshift : ibs;
754 uint_t limit = P2ROUNDUP(dmu_prefetch_max, 1 << bs) >> bs;
755 start2 = end = MIN(end2, start + limit);
756
757 /*
758 * Find level2 where [start2, end2) fits into dmu_prefetch_max.
759 */
760 uint8_t ibps = ibs - SPA_BLKPTRSHIFT;
761 limit = P2ROUNDUP(dmu_prefetch_max, 1 << ibs) >> ibs;
762 if (limit == 0)
763 end2 = start2;
764 do {
765 level2++;
766 start2 = P2ROUNDUP(start2, 1 << ibps) >> ibps;
767 end2 = P2ROUNDUP(end2, 1 << ibps) >> ibps;
768 } while (end2 - start2 > limit);
769 } else {
770 /* There is only one block. Prefetch it or nothing. */
771 start = start2 = end2 = 0;
772 end = start + (level == 0 && offset < dn->dn_datablksz);
773 }
774
775 for (uint64_t i = start; i < end; i++)
776 dbuf_prefetch(dn, level, i, pri, 0);
777 for (uint64_t i = start2; i < end2; i++)
778 dbuf_prefetch(dn, level2, i, pri, 0);
779 rw_exit(&dn->dn_struct_rwlock);
780 }
781
782 typedef struct {
783 kmutex_t dpa_lock;
784 kcondvar_t dpa_cv;
785 uint64_t dpa_pending_io;
786 } dmu_prefetch_arg_t;
787
788 static void
dmu_prefetch_done(void * arg,uint64_t level,uint64_t blkid,boolean_t issued)789 dmu_prefetch_done(void *arg, uint64_t level, uint64_t blkid, boolean_t issued)
790 {
791 (void) level; (void) blkid; (void)issued;
792 dmu_prefetch_arg_t *dpa = arg;
793
794 ASSERT0(level);
795
796 mutex_enter(&dpa->dpa_lock);
797 ASSERT3U(dpa->dpa_pending_io, >, 0);
798 if (--dpa->dpa_pending_io == 0)
799 cv_broadcast(&dpa->dpa_cv);
800 mutex_exit(&dpa->dpa_lock);
801 }
802
803 static void
dmu_prefetch_wait_by_dnode(dnode_t * dn,uint64_t offset,uint64_t len)804 dmu_prefetch_wait_by_dnode(dnode_t *dn, uint64_t offset, uint64_t len)
805 {
806 dmu_prefetch_arg_t dpa;
807
808 mutex_init(&dpa.dpa_lock, NULL, MUTEX_DEFAULT, NULL);
809 cv_init(&dpa.dpa_cv, NULL, CV_DEFAULT, NULL);
810
811 rw_enter(&dn->dn_struct_rwlock, RW_READER);
812
813 uint64_t start = dbuf_whichblock(dn, 0, offset);
814 uint64_t end = dbuf_whichblock(dn, 0, offset + len - 1) + 1;
815 dpa.dpa_pending_io = end - start;
816
817 for (uint64_t blk = start; blk < end; blk++) {
818 (void) dbuf_prefetch_impl(dn, 0, blk, ZIO_PRIORITY_ASYNC_READ,
819 0, dmu_prefetch_done, &dpa);
820 }
821
822 rw_exit(&dn->dn_struct_rwlock);
823
824 /* wait for prefetch L0 reads to finish */
825 mutex_enter(&dpa.dpa_lock);
826 while (dpa.dpa_pending_io > 0) {
827 cv_wait(&dpa.dpa_cv, &dpa.dpa_lock);
828
829 }
830 mutex_exit(&dpa.dpa_lock);
831
832 mutex_destroy(&dpa.dpa_lock);
833 cv_destroy(&dpa.dpa_cv);
834 }
835
836 /*
837 * Issue prefetch I/Os for the given L0 block range and wait for the I/O
838 * to complete. This does not enforce dmu_prefetch_max and will prefetch
839 * the entire range. The blocks are read from disk into the ARC but no
840 * decompression occurs (i.e., the dbuf cache is not required).
841 */
842 int
dmu_prefetch_wait(objset_t * os,uint64_t object,uint64_t offset,uint64_t size)843 dmu_prefetch_wait(objset_t *os, uint64_t object, uint64_t offset, uint64_t size)
844 {
845 dnode_t *dn;
846 int err = 0;
847
848 err = dnode_hold(os, object, FTAG, &dn);
849 if (err != 0)
850 return (err);
851
852 /*
853 * Chunk the requests (16 indirects worth) so that we can be interrupted
854 */
855 uint64_t chunksize;
856 if (dn->dn_indblkshift) {
857 uint64_t nbps = bp_span_in_blocks(dn->dn_indblkshift, 1);
858 chunksize = (nbps * 16) << dn->dn_datablkshift;
859 } else {
860 chunksize = dn->dn_datablksz;
861 }
862
863 while (size > 0) {
864 uint64_t mylen = MIN(size, chunksize);
865
866 dmu_prefetch_wait_by_dnode(dn, offset, mylen);
867
868 offset += mylen;
869 size -= mylen;
870
871 if (issig()) {
872 err = SET_ERROR(EINTR);
873 break;
874 }
875 }
876
877 dnode_rele(dn, FTAG);
878
879 return (err);
880 }
881
882 /*
883 * Issue prefetch I/Os for the given object's dnode.
884 */
885 void
dmu_prefetch_dnode(objset_t * os,uint64_t object,zio_priority_t pri)886 dmu_prefetch_dnode(objset_t *os, uint64_t object, zio_priority_t pri)
887 {
888 if (object == 0 || object >= DN_MAX_OBJECT)
889 return;
890
891 dnode_t *dn = DMU_META_DNODE(os);
892 rw_enter(&dn->dn_struct_rwlock, RW_READER);
893 uint64_t blkid = dbuf_whichblock(dn, 0, object * sizeof (dnode_phys_t));
894 dbuf_prefetch(dn, 0, blkid, pri, 0);
895 rw_exit(&dn->dn_struct_rwlock);
896 }
897
898 /*
899 * Get the next "chunk" of file data to free. We traverse the file from
900 * the end so that the file gets shorter over time (if we crash in the
901 * middle, this will leave us in a better state). We find allocated file
902 * data by simply searching the allocated level 1 indirects.
903 *
904 * On input, *start should be the first offset that does not need to be
905 * freed (e.g. "offset + length"). On return, *start will be the first
906 * offset that should be freed and l1blks is set to the number of level 1
907 * indirect blocks found within the chunk.
908 */
909 static int
get_next_chunk(dnode_t * dn,uint64_t * start,uint64_t minimum,uint64_t * l1blks)910 get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t minimum, uint64_t *l1blks)
911 {
912 uint64_t blks;
913 uint64_t maxblks = DMU_MAX_ACCESS >> (dn->dn_indblkshift + 1);
914 /* bytes of data covered by a level-1 indirect block */
915 uint64_t iblkrange = (uint64_t)dn->dn_datablksz *
916 EPB(dn->dn_indblkshift, SPA_BLKPTRSHIFT);
917
918 ASSERT3U(minimum, <=, *start);
919
920 /* dn_nlevels == 1 means we don't have any L1 blocks */
921 if (dn->dn_nlevels <= 1) {
922 *l1blks = 0;
923 *start = minimum;
924 return (0);
925 }
926
927 /*
928 * Check if we can free the entire range assuming that all of the
929 * L1 blocks in this range have data. If we can, we use this
930 * worst case value as an estimate so we can avoid having to look
931 * at the object's actual data.
932 */
933 uint64_t total_l1blks =
934 (roundup(*start, iblkrange) - (minimum / iblkrange * iblkrange)) /
935 iblkrange;
936 if (total_l1blks <= maxblks) {
937 *l1blks = total_l1blks;
938 *start = minimum;
939 return (0);
940 }
941 ASSERT(ISP2(iblkrange));
942
943 for (blks = 0; *start > minimum && blks < maxblks; blks++) {
944 int err;
945
946 /*
947 * dnode_next_offset(BACKWARDS) will find an allocated L1
948 * indirect block at or before the input offset. We must
949 * decrement *start so that it is at the end of the region
950 * to search.
951 */
952 (*start)--;
953
954 err = dnode_next_offset(dn,
955 DNODE_FIND_BACKWARDS, start, 2, 1, 0);
956
957 /* if there are no indirect blocks before start, we are done */
958 if (err == ESRCH) {
959 *start = minimum;
960 break;
961 } else if (err != 0) {
962 *l1blks = blks;
963 return (err);
964 }
965
966 /* set start to the beginning of this L1 indirect */
967 *start = P2ALIGN_TYPED(*start, iblkrange, uint64_t);
968 }
969 if (*start < minimum)
970 *start = minimum;
971 *l1blks = blks;
972
973 return (0);
974 }
975
976 /*
977 * If this objset is of type OST_ZFS return true if vfs's unmounted flag is set,
978 * otherwise return false.
979 * Used below in dmu_free_long_range_impl() to enable abort when unmounting
980 */
981 static boolean_t
dmu_objset_zfs_unmounting(objset_t * os)982 dmu_objset_zfs_unmounting(objset_t *os)
983 {
984 #ifdef _KERNEL
985 if (dmu_objset_type(os) == DMU_OST_ZFS)
986 return (zfs_get_vfs_flag_unmounted(os));
987 #else
988 (void) os;
989 #endif
990 return (B_FALSE);
991 }
992
993 static int
dmu_free_long_range_impl(objset_t * os,dnode_t * dn,uint64_t offset,uint64_t length)994 dmu_free_long_range_impl(objset_t *os, dnode_t *dn, uint64_t offset,
995 uint64_t length)
996 {
997 uint64_t object_size;
998 int err;
999 uint64_t dirty_frees_threshold;
1000 dsl_pool_t *dp = dmu_objset_pool(os);
1001
1002 if (dn == NULL)
1003 return (SET_ERROR(EINVAL));
1004
1005 object_size = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
1006 if (offset >= object_size)
1007 return (0);
1008
1009 if (zfs_per_txg_dirty_frees_percent <= 100)
1010 dirty_frees_threshold =
1011 zfs_per_txg_dirty_frees_percent * zfs_dirty_data_max / 100;
1012 else
1013 dirty_frees_threshold = zfs_dirty_data_max / 20;
1014
1015 if (length == DMU_OBJECT_END || offset + length > object_size)
1016 length = object_size - offset;
1017
1018 while (length != 0) {
1019 uint64_t chunk_end, chunk_begin, chunk_len;
1020 uint64_t l1blks;
1021 dmu_tx_t *tx;
1022
1023 if (dmu_objset_zfs_unmounting(dn->dn_objset))
1024 return (SET_ERROR(EINTR));
1025
1026 chunk_end = chunk_begin = offset + length;
1027
1028 /* move chunk_begin backwards to the beginning of this chunk */
1029 err = get_next_chunk(dn, &chunk_begin, offset, &l1blks);
1030 if (err)
1031 return (err);
1032 ASSERT3U(chunk_begin, >=, offset);
1033 ASSERT3U(chunk_begin, <=, chunk_end);
1034
1035 chunk_len = chunk_end - chunk_begin;
1036
1037 tx = dmu_tx_create(os);
1038 dmu_tx_hold_free(tx, dn->dn_object, chunk_begin, chunk_len);
1039
1040 /*
1041 * Mark this transaction as typically resulting in a net
1042 * reduction in space used.
1043 */
1044 dmu_tx_mark_netfree(tx);
1045 err = dmu_tx_assign(tx, DMU_TX_WAIT);
1046 if (err) {
1047 dmu_tx_abort(tx);
1048 return (err);
1049 }
1050
1051 uint64_t txg = dmu_tx_get_txg(tx);
1052
1053 mutex_enter(&dp->dp_lock);
1054 uint64_t long_free_dirty =
1055 dp->dp_long_free_dirty_pertxg[txg & TXG_MASK];
1056 mutex_exit(&dp->dp_lock);
1057
1058 /*
1059 * To avoid filling up a TXG with just frees, wait for
1060 * the next TXG to open before freeing more chunks if
1061 * we have reached the threshold of frees.
1062 */
1063 if (dirty_frees_threshold != 0 &&
1064 long_free_dirty >= dirty_frees_threshold) {
1065 DMU_TX_STAT_BUMP(dmu_tx_dirty_frees_delay);
1066 dmu_tx_commit(tx);
1067 txg_wait_open(dp, 0, B_TRUE);
1068 continue;
1069 }
1070
1071 /*
1072 * In order to prevent unnecessary write throttling, for each
1073 * TXG, we track the cumulative size of L1 blocks being dirtied
1074 * in dnode_free_range() below. We compare this number to a
1075 * tunable threshold, past which we prevent new L1 dirty freeing
1076 * blocks from being added into the open TXG. See
1077 * dmu_free_long_range_impl() for details. The threshold
1078 * prevents write throttle activation due to dirty freeing L1
1079 * blocks taking up a large percentage of zfs_dirty_data_max.
1080 */
1081 mutex_enter(&dp->dp_lock);
1082 dp->dp_long_free_dirty_pertxg[txg & TXG_MASK] +=
1083 l1blks << dn->dn_indblkshift;
1084 mutex_exit(&dp->dp_lock);
1085 DTRACE_PROBE3(free__long__range,
1086 uint64_t, long_free_dirty, uint64_t, chunk_len,
1087 uint64_t, txg);
1088 dnode_free_range(dn, chunk_begin, chunk_len, tx);
1089
1090 dmu_tx_commit(tx);
1091
1092 length -= chunk_len;
1093 }
1094 return (0);
1095 }
1096
1097 int
dmu_free_long_range(objset_t * os,uint64_t object,uint64_t offset,uint64_t length)1098 dmu_free_long_range(objset_t *os, uint64_t object,
1099 uint64_t offset, uint64_t length)
1100 {
1101 dnode_t *dn;
1102 int err;
1103
1104 err = dnode_hold(os, object, FTAG, &dn);
1105 if (err != 0)
1106 return (err);
1107 err = dmu_free_long_range_impl(os, dn, offset, length);
1108
1109 /*
1110 * It is important to zero out the maxblkid when freeing the entire
1111 * file, so that (a) subsequent calls to dmu_free_long_range_impl()
1112 * will take the fast path, and (b) dnode_reallocate() can verify
1113 * that the entire file has been freed.
1114 */
1115 if (err == 0 && offset == 0 && length == DMU_OBJECT_END)
1116 dn->dn_maxblkid = 0;
1117
1118 dnode_rele(dn, FTAG);
1119 return (err);
1120 }
1121
1122 int
dmu_free_long_object(objset_t * os,uint64_t object)1123 dmu_free_long_object(objset_t *os, uint64_t object)
1124 {
1125 dmu_tx_t *tx;
1126 int err;
1127
1128 err = dmu_free_long_range(os, object, 0, DMU_OBJECT_END);
1129 if (err != 0)
1130 return (err);
1131
1132 tx = dmu_tx_create(os);
1133 dmu_tx_hold_bonus(tx, object);
1134 dmu_tx_hold_free(tx, object, 0, DMU_OBJECT_END);
1135 dmu_tx_mark_netfree(tx);
1136 err = dmu_tx_assign(tx, DMU_TX_WAIT);
1137 if (err == 0) {
1138 err = dmu_object_free(os, object, tx);
1139 dmu_tx_commit(tx);
1140 } else {
1141 dmu_tx_abort(tx);
1142 }
1143
1144 return (err);
1145 }
1146
1147 int
dmu_free_range(objset_t * os,uint64_t object,uint64_t offset,uint64_t size,dmu_tx_t * tx)1148 dmu_free_range(objset_t *os, uint64_t object, uint64_t offset,
1149 uint64_t size, dmu_tx_t *tx)
1150 {
1151 dnode_t *dn;
1152 int err = dnode_hold(os, object, FTAG, &dn);
1153 if (err)
1154 return (err);
1155 ASSERT(offset < UINT64_MAX);
1156 ASSERT(size == DMU_OBJECT_END || size <= UINT64_MAX - offset);
1157 dnode_free_range(dn, offset, size, tx);
1158 dnode_rele(dn, FTAG);
1159 return (0);
1160 }
1161
1162 static int
dmu_read_impl(dnode_t * dn,uint64_t offset,uint64_t size,void * buf,dmu_flags_t flags)1163 dmu_read_impl(dnode_t *dn, uint64_t offset, uint64_t size,
1164 void *buf, dmu_flags_t flags)
1165 {
1166 dmu_buf_t **dbp;
1167 int numbufs, err = 0;
1168
1169 /*
1170 * Deal with odd block sizes, where there can't be data past the first
1171 * block. If we ever do the tail block optimization, we will need to
1172 * handle that here as well.
1173 */
1174 if (dn->dn_maxblkid == 0) {
1175 uint64_t newsz = offset > dn->dn_datablksz ? 0 :
1176 MIN(size, dn->dn_datablksz - offset);
1177 memset((char *)buf + newsz, 0, size - newsz);
1178 size = newsz;
1179 }
1180
1181 if (size == 0)
1182 return (0);
1183
1184 /* Allow Direct I/O when requested and properly aligned */
1185 if ((flags & DMU_DIRECTIO) && zfs_dio_page_aligned(buf) &&
1186 zfs_dio_aligned(offset, size, PAGESIZE)) {
1187 abd_t *data = abd_get_from_buf(buf, size);
1188 err = dmu_read_abd(dn, offset, size, data, flags);
1189 abd_free(data);
1190 return (err);
1191 }
1192 flags &= ~DMU_DIRECTIO;
1193
1194 while (size > 0) {
1195 uint64_t mylen = MIN(size, DMU_MAX_ACCESS / 2);
1196 int i;
1197
1198 /*
1199 * NB: we could do this block-at-a-time, but it's nice
1200 * to be reading in parallel.
1201 */
1202 err = dmu_buf_hold_array_by_dnode(dn, offset, mylen,
1203 TRUE, FTAG, &numbufs, &dbp, flags);
1204 if (err)
1205 break;
1206
1207 for (i = 0; i < numbufs; i++) {
1208 uint64_t tocpy;
1209 int64_t bufoff;
1210 dmu_buf_t *db = dbp[i];
1211
1212 ASSERT(size > 0);
1213
1214 bufoff = offset - db->db_offset;
1215 tocpy = MIN(db->db_size - bufoff, size);
1216
1217 ASSERT(db->db_data != NULL);
1218 (void) memcpy(buf, (char *)db->db_data + bufoff, tocpy);
1219
1220 offset += tocpy;
1221 size -= tocpy;
1222 buf = (char *)buf + tocpy;
1223 }
1224 dmu_buf_rele_array(dbp, numbufs, FTAG);
1225 }
1226 return (err);
1227 }
1228
1229 int
dmu_read(objset_t * os,uint64_t object,uint64_t offset,uint64_t size,void * buf,dmu_flags_t flags)1230 dmu_read(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1231 void *buf, dmu_flags_t flags)
1232 {
1233 dnode_t *dn;
1234 int err;
1235
1236 err = dnode_hold(os, object, FTAG, &dn);
1237 if (err != 0)
1238 return (err);
1239
1240 err = dmu_read_impl(dn, offset, size, buf, flags);
1241 dnode_rele(dn, FTAG);
1242 return (err);
1243 }
1244
1245 int
dmu_read_by_dnode(dnode_t * dn,uint64_t offset,uint64_t size,void * buf,dmu_flags_t flags)1246 dmu_read_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size, void *buf,
1247 dmu_flags_t flags)
1248 {
1249 return (dmu_read_impl(dn, offset, size, buf, flags));
1250 }
1251
1252 static void
dmu_write_impl(dmu_buf_t ** dbp,int numbufs,uint64_t offset,uint64_t size,const void * buf,dmu_tx_t * tx,dmu_flags_t flags)1253 dmu_write_impl(dmu_buf_t **dbp, int numbufs, uint64_t offset, uint64_t size,
1254 const void *buf, dmu_tx_t *tx, dmu_flags_t flags)
1255 {
1256 int i;
1257
1258 for (i = 0; i < numbufs; i++) {
1259 uint64_t tocpy;
1260 int64_t bufoff;
1261 dmu_buf_t *db = dbp[i];
1262
1263 ASSERT(size > 0);
1264
1265 bufoff = offset - db->db_offset;
1266 tocpy = MIN(db->db_size - bufoff, size);
1267
1268 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1269
1270 if (tocpy == db->db_size) {
1271 dmu_buf_will_fill_flags(db, tx, B_FALSE, flags);
1272 } else {
1273 if (i == numbufs - 1 && bufoff + tocpy < db->db_size) {
1274 if (bufoff == 0)
1275 flags |= DMU_PARTIAL_FIRST;
1276 else
1277 flags |= DMU_PARTIAL_MORE;
1278 }
1279 dmu_buf_will_dirty_flags(db, tx, flags);
1280 }
1281
1282 ASSERT(db->db_data != NULL);
1283 (void) memcpy((char *)db->db_data + bufoff, buf, tocpy);
1284
1285 if (tocpy == db->db_size)
1286 dmu_buf_fill_done(db, tx, B_FALSE);
1287
1288 offset += tocpy;
1289 size -= tocpy;
1290 buf = (char *)buf + tocpy;
1291 }
1292 }
1293
1294 void
dmu_write(objset_t * os,uint64_t object,uint64_t offset,uint64_t size,const void * buf,dmu_tx_t * tx)1295 dmu_write(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1296 const void *buf, dmu_tx_t *tx)
1297 {
1298 dmu_buf_t **dbp;
1299 int numbufs;
1300
1301 if (size == 0)
1302 return;
1303
1304 VERIFY0(dmu_buf_hold_array(os, object, offset, size,
1305 FALSE, FTAG, &numbufs, &dbp));
1306 dmu_write_impl(dbp, numbufs, offset, size, buf, tx, DMU_READ_PREFETCH);
1307 dmu_buf_rele_array(dbp, numbufs, FTAG);
1308 }
1309
1310 int
dmu_write_by_dnode(dnode_t * dn,uint64_t offset,uint64_t size,const void * buf,dmu_tx_t * tx,dmu_flags_t flags)1311 dmu_write_by_dnode(dnode_t *dn, uint64_t offset, uint64_t size,
1312 const void *buf, dmu_tx_t *tx, dmu_flags_t flags)
1313 {
1314 dmu_buf_t **dbp;
1315 int numbufs;
1316 int error;
1317
1318 if (size == 0)
1319 return (0);
1320
1321 /* Allow Direct I/O when requested and properly aligned */
1322 if ((flags & DMU_DIRECTIO) && zfs_dio_page_aligned((void *)buf) &&
1323 zfs_dio_aligned(offset, size, dn->dn_datablksz)) {
1324 abd_t *data = abd_get_from_buf((void *)buf, size);
1325 error = dmu_write_abd(dn, offset, size, data, flags, tx);
1326 abd_free(data);
1327 return (error);
1328 }
1329 flags &= ~DMU_DIRECTIO;
1330
1331 VERIFY0(dmu_buf_hold_array_by_dnode(dn, offset, size,
1332 FALSE, FTAG, &numbufs, &dbp, flags));
1333 dmu_write_impl(dbp, numbufs, offset, size, buf, tx, flags);
1334 dmu_buf_rele_array(dbp, numbufs, FTAG);
1335 return (0);
1336 }
1337
1338 void
dmu_prealloc(objset_t * os,uint64_t object,uint64_t offset,uint64_t size,dmu_tx_t * tx)1339 dmu_prealloc(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1340 dmu_tx_t *tx)
1341 {
1342 dmu_buf_t **dbp;
1343 int numbufs, i;
1344
1345 if (size == 0)
1346 return;
1347
1348 VERIFY0(dmu_buf_hold_array(os, object, offset, size,
1349 FALSE, FTAG, &numbufs, &dbp));
1350
1351 for (i = 0; i < numbufs; i++) {
1352 dmu_buf_t *db = dbp[i];
1353
1354 dmu_buf_will_not_fill(db, tx);
1355 }
1356 dmu_buf_rele_array(dbp, numbufs, FTAG);
1357 }
1358
1359 void
dmu_write_embedded(objset_t * os,uint64_t object,uint64_t offset,void * data,uint8_t etype,uint8_t comp,int uncompressed_size,int compressed_size,int byteorder,dmu_tx_t * tx)1360 dmu_write_embedded(objset_t *os, uint64_t object, uint64_t offset,
1361 void *data, uint8_t etype, uint8_t comp, int uncompressed_size,
1362 int compressed_size, int byteorder, dmu_tx_t *tx)
1363 {
1364 dmu_buf_t *db;
1365
1366 ASSERT3U(etype, <, NUM_BP_EMBEDDED_TYPES);
1367 ASSERT3U(comp, <, ZIO_COMPRESS_FUNCTIONS);
1368 VERIFY0(dmu_buf_hold_noread(os, object, offset,
1369 FTAG, &db));
1370
1371 dmu_buf_write_embedded(db,
1372 data, (bp_embedded_type_t)etype, (enum zio_compress)comp,
1373 uncompressed_size, compressed_size, byteorder, tx);
1374
1375 dmu_buf_rele(db, FTAG);
1376 }
1377
1378 void
dmu_redact(objset_t * os,uint64_t object,uint64_t offset,uint64_t size,dmu_tx_t * tx)1379 dmu_redact(objset_t *os, uint64_t object, uint64_t offset, uint64_t size,
1380 dmu_tx_t *tx)
1381 {
1382 int numbufs, i;
1383 dmu_buf_t **dbp;
1384
1385 VERIFY0(dmu_buf_hold_array(os, object, offset, size, FALSE, FTAG,
1386 &numbufs, &dbp));
1387 for (i = 0; i < numbufs; i++)
1388 dmu_buf_redact(dbp[i], tx);
1389 dmu_buf_rele_array(dbp, numbufs, FTAG);
1390 }
1391
1392 #ifdef _KERNEL
1393 int
dmu_read_uio_dnode(dnode_t * dn,zfs_uio_t * uio,uint64_t size,dmu_flags_t flags)1394 dmu_read_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size,
1395 dmu_flags_t flags)
1396 {
1397 dmu_buf_t **dbp;
1398 int numbufs, i, err;
1399
1400 if ((flags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT))
1401 return (dmu_read_uio_direct(dn, uio, size, flags));
1402 flags &= ~DMU_DIRECTIO;
1403
1404 /*
1405 * NB: we could do this block-at-a-time, but it's nice
1406 * to be reading in parallel.
1407 */
1408 err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), size,
1409 TRUE, FTAG, &numbufs, &dbp, flags);
1410 if (err)
1411 return (err);
1412
1413 for (i = 0; i < numbufs; i++) {
1414 uint64_t tocpy;
1415 int64_t bufoff;
1416 dmu_buf_t *db = dbp[i];
1417
1418 ASSERT(size > 0);
1419
1420 bufoff = zfs_uio_offset(uio) - db->db_offset;
1421 tocpy = MIN(db->db_size - bufoff, size);
1422
1423 ASSERT(db->db_data != NULL);
1424 err = zfs_uio_fault_move((char *)db->db_data + bufoff, tocpy,
1425 UIO_READ, uio);
1426
1427 if (err)
1428 break;
1429
1430 size -= tocpy;
1431 }
1432 dmu_buf_rele_array(dbp, numbufs, FTAG);
1433
1434 return (err);
1435 }
1436
1437 /*
1438 * Read 'size' bytes into the uio buffer.
1439 * From object zdb->db_object.
1440 * Starting at zfs_uio_offset(uio).
1441 *
1442 * If the caller already has a dbuf in the target object
1443 * (e.g. its bonus buffer), this routine is faster than dmu_read_uio(),
1444 * because we don't have to find the dnode_t for the object.
1445 */
1446 int
dmu_read_uio_dbuf(dmu_buf_t * zdb,zfs_uio_t * uio,uint64_t size,dmu_flags_t flags)1447 dmu_read_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size,
1448 dmu_flags_t flags)
1449 {
1450 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1451 int err;
1452
1453 if (size == 0)
1454 return (0);
1455
1456 DB_DNODE_ENTER(db);
1457 err = dmu_read_uio_dnode(DB_DNODE(db), uio, size, flags);
1458 DB_DNODE_EXIT(db);
1459
1460 return (err);
1461 }
1462
1463 /*
1464 * Read 'size' bytes into the uio buffer.
1465 * From the specified object
1466 * Starting at offset zfs_uio_offset(uio).
1467 */
1468 int
dmu_read_uio(objset_t * os,uint64_t object,zfs_uio_t * uio,uint64_t size,dmu_flags_t flags)1469 dmu_read_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size,
1470 dmu_flags_t flags)
1471 {
1472 dnode_t *dn;
1473 int err;
1474
1475 if (size == 0)
1476 return (0);
1477
1478 err = dnode_hold(os, object, FTAG, &dn);
1479 if (err)
1480 return (err);
1481
1482 err = dmu_read_uio_dnode(dn, uio, size, flags);
1483
1484 dnode_rele(dn, FTAG);
1485
1486 return (err);
1487 }
1488
1489 int
dmu_write_uio_dnode(dnode_t * dn,zfs_uio_t * uio,uint64_t size,dmu_tx_t * tx,dmu_flags_t flags)1490 dmu_write_uio_dnode(dnode_t *dn, zfs_uio_t *uio, uint64_t size, dmu_tx_t *tx,
1491 dmu_flags_t flags)
1492 {
1493 dmu_buf_t **dbp;
1494 int numbufs;
1495 int err = 0;
1496 uint64_t write_size;
1497 dmu_flags_t oflags = flags;
1498
1499 top:
1500 write_size = size;
1501
1502 /*
1503 * We only allow Direct I/O writes to happen if we are block
1504 * sized aligned. Otherwise, we pass the write off to the ARC.
1505 */
1506 if ((flags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT) &&
1507 (write_size >= dn->dn_datablksz)) {
1508 if (zfs_dio_aligned(zfs_uio_offset(uio), write_size,
1509 dn->dn_datablksz)) {
1510 return (dmu_write_uio_direct(dn, uio, size, flags, tx));
1511 } else if (write_size > dn->dn_datablksz &&
1512 zfs_dio_offset_aligned(zfs_uio_offset(uio),
1513 dn->dn_datablksz)) {
1514 write_size =
1515 dn->dn_datablksz * (write_size / dn->dn_datablksz);
1516 err = dmu_write_uio_direct(dn, uio, write_size, flags,
1517 tx);
1518 if (err == 0) {
1519 size -= write_size;
1520 goto top;
1521 } else {
1522 return (err);
1523 }
1524 } else {
1525 write_size =
1526 P2PHASE(zfs_uio_offset(uio), dn->dn_datablksz);
1527 }
1528 }
1529 flags &= ~DMU_DIRECTIO;
1530
1531 err = dmu_buf_hold_array_by_dnode(dn, zfs_uio_offset(uio), write_size,
1532 FALSE, FTAG, &numbufs, &dbp, flags);
1533 if (err)
1534 return (err);
1535
1536 for (int i = 0; i < numbufs; i++) {
1537 uint64_t tocpy;
1538 int64_t bufoff;
1539 dmu_buf_t *db = dbp[i];
1540
1541 ASSERT(write_size > 0);
1542
1543 offset_t off = zfs_uio_offset(uio);
1544 bufoff = off - db->db_offset;
1545 tocpy = MIN(db->db_size - bufoff, write_size);
1546
1547 ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size);
1548
1549 if (tocpy == db->db_size) {
1550 dmu_buf_will_fill_flags(db, tx, B_TRUE, flags);
1551 } else {
1552 if (i == numbufs - 1 && bufoff + tocpy < db->db_size) {
1553 if (bufoff == 0)
1554 flags |= DMU_PARTIAL_FIRST;
1555 else
1556 flags |= DMU_PARTIAL_MORE;
1557 }
1558 dmu_buf_will_dirty_flags(db, tx, flags);
1559 }
1560
1561 ASSERT(db->db_data != NULL);
1562 err = zfs_uio_fault_move((char *)db->db_data + bufoff,
1563 tocpy, UIO_WRITE, uio);
1564
1565 if (tocpy == db->db_size && dmu_buf_fill_done(db, tx, err)) {
1566 /* The fill was reverted. Undo any uio progress. */
1567 zfs_uio_advance(uio, off - zfs_uio_offset(uio));
1568 }
1569
1570 if (err)
1571 break;
1572
1573 write_size -= tocpy;
1574 size -= tocpy;
1575 }
1576
1577 IMPLY(err == 0, write_size == 0);
1578
1579 dmu_buf_rele_array(dbp, numbufs, FTAG);
1580
1581 if ((oflags & DMU_DIRECTIO) && (uio->uio_extflg & UIO_DIRECT) &&
1582 err == 0 && size > 0) {
1583 flags = oflags;
1584 goto top;
1585 }
1586 IMPLY(err == 0, size == 0);
1587
1588 return (err);
1589 }
1590
1591 /*
1592 * Write 'size' bytes from the uio buffer.
1593 * To object zdb->db_object.
1594 * Starting at offset zfs_uio_offset(uio).
1595 *
1596 * If the caller already has a dbuf in the target object
1597 * (e.g. its bonus buffer), this routine is faster than dmu_write_uio(),
1598 * because we don't have to find the dnode_t for the object.
1599 */
1600 int
dmu_write_uio_dbuf(dmu_buf_t * zdb,zfs_uio_t * uio,uint64_t size,dmu_tx_t * tx,dmu_flags_t flags)1601 dmu_write_uio_dbuf(dmu_buf_t *zdb, zfs_uio_t *uio, uint64_t size,
1602 dmu_tx_t *tx, dmu_flags_t flags)
1603 {
1604 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zdb;
1605 int err;
1606
1607 if (size == 0)
1608 return (0);
1609
1610 DB_DNODE_ENTER(db);
1611 err = dmu_write_uio_dnode(DB_DNODE(db), uio, size, tx, flags);
1612 DB_DNODE_EXIT(db);
1613
1614 return (err);
1615 }
1616
1617 /*
1618 * Write 'size' bytes from the uio buffer.
1619 * To the specified object.
1620 * Starting at offset zfs_uio_offset(uio).
1621 */
1622 int
dmu_write_uio(objset_t * os,uint64_t object,zfs_uio_t * uio,uint64_t size,dmu_tx_t * tx,dmu_flags_t flags)1623 dmu_write_uio(objset_t *os, uint64_t object, zfs_uio_t *uio, uint64_t size,
1624 dmu_tx_t *tx, dmu_flags_t flags)
1625 {
1626 dnode_t *dn;
1627 int err;
1628
1629 if (size == 0)
1630 return (0);
1631
1632 err = dnode_hold(os, object, FTAG, &dn);
1633 if (err)
1634 return (err);
1635
1636 err = dmu_write_uio_dnode(dn, uio, size, tx, flags);
1637
1638 dnode_rele(dn, FTAG);
1639
1640 return (err);
1641 }
1642 #endif /* _KERNEL */
1643
1644 static void
dmu_cached_bps(spa_t * spa,blkptr_t * bps,uint_t nbps,uint64_t * l1sz,uint64_t * l2sz)1645 dmu_cached_bps(spa_t *spa, blkptr_t *bps, uint_t nbps,
1646 uint64_t *l1sz, uint64_t *l2sz)
1647 {
1648 int cached_flags;
1649
1650 if (bps == NULL)
1651 return;
1652
1653 for (size_t blk_off = 0; blk_off < nbps; blk_off++) {
1654 blkptr_t *bp = &bps[blk_off];
1655
1656 if (BP_IS_HOLE(bp))
1657 continue;
1658
1659 cached_flags = arc_cached(spa, bp);
1660 if (cached_flags == 0)
1661 continue;
1662
1663 if ((cached_flags & (ARC_CACHED_IN_L1 | ARC_CACHED_IN_L2)) ==
1664 ARC_CACHED_IN_L2)
1665 *l2sz += BP_GET_LSIZE(bp);
1666 else
1667 *l1sz += BP_GET_LSIZE(bp);
1668 }
1669 }
1670
1671 /*
1672 * Estimate DMU object cached size.
1673 */
1674 int
dmu_object_cached_size(objset_t * os,uint64_t object,uint64_t * l1sz,uint64_t * l2sz)1675 dmu_object_cached_size(objset_t *os, uint64_t object,
1676 uint64_t *l1sz, uint64_t *l2sz)
1677 {
1678 dnode_t *dn;
1679 dmu_object_info_t doi;
1680 int err = 0;
1681
1682 *l1sz = *l2sz = 0;
1683
1684 if (dnode_hold(os, object, FTAG, &dn) != 0)
1685 return (0);
1686
1687 if (dn->dn_nlevels < 2) {
1688 dnode_rele(dn, FTAG);
1689 return (0);
1690 }
1691
1692 dmu_object_info_from_dnode(dn, &doi);
1693
1694 for (uint64_t off = 0; off < doi.doi_max_offset &&
1695 dmu_prefetch_max > 0; off += dmu_prefetch_max) {
1696 /* dbuf_read doesn't prefetch L1 blocks. */
1697 dmu_prefetch_by_dnode(dn, 1, off,
1698 dmu_prefetch_max, ZIO_PRIORITY_SYNC_READ);
1699 }
1700
1701 /*
1702 * Hold all valid L1 blocks, asking ARC the status of each BP
1703 * contained in each such L1 block.
1704 */
1705 uint_t nbps = bp_span_in_blocks(dn->dn_indblkshift, 1);
1706 uint64_t l1blks = 1 + (dn->dn_maxblkid / nbps);
1707
1708 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1709 for (uint64_t blk = 0; blk < l1blks; blk++) {
1710 dmu_buf_impl_t *db = NULL;
1711
1712 if (issig()) {
1713 /*
1714 * On interrupt, get out, and bubble up EINTR
1715 */
1716 err = EINTR;
1717 break;
1718 }
1719
1720 /*
1721 * If we get an i/o error here, the L1 can't be read,
1722 * and nothing under it could be cached, so we just
1723 * continue. Ignoring the error from dbuf_hold_impl
1724 * or from dbuf_read is then a reasonable choice.
1725 */
1726 err = dbuf_hold_impl(dn, 1, blk, B_TRUE, B_FALSE, FTAG, &db);
1727 if (err != 0) {
1728 /*
1729 * ignore error and continue
1730 */
1731 err = 0;
1732 continue;
1733 }
1734
1735 err = dbuf_read(db, NULL, DB_RF_CANFAIL);
1736 if (err == 0) {
1737 dmu_cached_bps(dmu_objset_spa(os), db->db.db_data,
1738 nbps, l1sz, l2sz);
1739 }
1740 /*
1741 * error may be ignored, and we continue
1742 */
1743 err = 0;
1744 dbuf_rele(db, FTAG);
1745 }
1746 rw_exit(&dn->dn_struct_rwlock);
1747
1748 dnode_rele(dn, FTAG);
1749 return (err);
1750 }
1751
1752 /*
1753 * Allocate a loaned anonymous arc buffer.
1754 */
1755 arc_buf_t *
dmu_request_arcbuf(dmu_buf_t * handle,int size)1756 dmu_request_arcbuf(dmu_buf_t *handle, int size)
1757 {
1758 dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
1759
1760 return (arc_loan_buf(db->db_objset->os_spa, B_FALSE, size));
1761 }
1762
1763 /*
1764 * Free a loaned arc buffer.
1765 */
1766 void
dmu_return_arcbuf(arc_buf_t * buf)1767 dmu_return_arcbuf(arc_buf_t *buf)
1768 {
1769 arc_return_buf(buf, FTAG);
1770 arc_buf_destroy(buf, FTAG);
1771 }
1772
1773 /*
1774 * A "lightweight" write is faster than a regular write (e.g.
1775 * dmu_write_by_dnode() or dmu_assign_arcbuf_by_dnode()), because it avoids the
1776 * CPU cost of creating a dmu_buf_impl_t and arc_buf_[hdr_]_t. However, the
1777 * data can not be read or overwritten until the transaction's txg has been
1778 * synced. This makes it appropriate for workloads that are known to be
1779 * (temporarily) write-only, like "zfs receive".
1780 *
1781 * A single block is written, starting at the specified offset in bytes. If
1782 * the call is successful, it returns 0 and the provided abd has been
1783 * consumed (the caller should not free it).
1784 */
1785 int
dmu_lightweight_write_by_dnode(dnode_t * dn,uint64_t offset,abd_t * abd,const zio_prop_t * zp,zio_flag_t flags,dmu_tx_t * tx)1786 dmu_lightweight_write_by_dnode(dnode_t *dn, uint64_t offset, abd_t *abd,
1787 const zio_prop_t *zp, zio_flag_t flags, dmu_tx_t *tx)
1788 {
1789 dbuf_dirty_record_t *dr =
1790 dbuf_dirty_lightweight(dn, dbuf_whichblock(dn, 0, offset), tx);
1791 if (dr == NULL)
1792 return (SET_ERROR(EIO));
1793 dr->dt.dll.dr_abd = abd;
1794 dr->dt.dll.dr_props = *zp;
1795 dr->dt.dll.dr_flags = flags;
1796 return (0);
1797 }
1798
1799 /*
1800 * When possible directly assign passed loaned arc buffer to a dbuf.
1801 * If this is not possible copy the contents of passed arc buf via
1802 * dmu_write().
1803 */
1804 int
dmu_assign_arcbuf_by_dnode(dnode_t * dn,uint64_t offset,arc_buf_t * buf,dmu_tx_t * tx,dmu_flags_t flags)1805 dmu_assign_arcbuf_by_dnode(dnode_t *dn, uint64_t offset, arc_buf_t *buf,
1806 dmu_tx_t *tx, dmu_flags_t flags)
1807 {
1808 dmu_buf_impl_t *db;
1809 objset_t *os = dn->dn_objset;
1810 uint32_t blksz = (uint32_t)arc_buf_lsize(buf);
1811 uint64_t blkid;
1812
1813 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1814 blkid = dbuf_whichblock(dn, 0, offset);
1815 db = dbuf_hold(dn, blkid, FTAG);
1816 rw_exit(&dn->dn_struct_rwlock);
1817 if (db == NULL)
1818 return (SET_ERROR(EIO));
1819
1820 /*
1821 * We can only assign if the offset is aligned and the arc buf is the
1822 * same size as the dbuf.
1823 */
1824 if (offset == db->db.db_offset && blksz == db->db.db_size) {
1825 zfs_racct_write(os->os_spa, blksz, 1, flags);
1826 dbuf_assign_arcbuf(db, buf, tx, flags);
1827 dbuf_rele(db, FTAG);
1828 } else {
1829 /* compressed bufs must always be assignable to their dbuf */
1830 ASSERT3U(arc_get_compression(buf), ==, ZIO_COMPRESS_OFF);
1831 ASSERT(!(buf->b_flags & ARC_BUF_FLAG_COMPRESSED));
1832
1833 dbuf_rele(db, FTAG);
1834 dmu_write_by_dnode(dn, offset, blksz, buf->b_data, tx, flags);
1835 dmu_return_arcbuf(buf);
1836 }
1837
1838 return (0);
1839 }
1840
1841 int
dmu_assign_arcbuf_by_dbuf(dmu_buf_t * handle,uint64_t offset,arc_buf_t * buf,dmu_tx_t * tx,dmu_flags_t flags)1842 dmu_assign_arcbuf_by_dbuf(dmu_buf_t *handle, uint64_t offset, arc_buf_t *buf,
1843 dmu_tx_t *tx, dmu_flags_t flags)
1844 {
1845 int err;
1846 dmu_buf_impl_t *db = (dmu_buf_impl_t *)handle;
1847
1848 DB_DNODE_ENTER(db);
1849 err = dmu_assign_arcbuf_by_dnode(DB_DNODE(db), offset, buf, tx, flags);
1850 DB_DNODE_EXIT(db);
1851
1852 return (err);
1853 }
1854
1855 void
dmu_sync_ready(zio_t * zio,arc_buf_t * buf,void * varg)1856 dmu_sync_ready(zio_t *zio, arc_buf_t *buf, void *varg)
1857 {
1858 (void) buf;
1859 dmu_sync_arg_t *dsa = varg;
1860
1861 if (zio->io_error == 0) {
1862 dbuf_dirty_record_t *dr = dsa->dsa_dr;
1863 blkptr_t *bp = zio->io_bp;
1864
1865 if (BP_IS_HOLE(bp)) {
1866 dmu_buf_t *db = NULL;
1867 if (dr)
1868 db = &(dr->dr_dbuf->db);
1869 else
1870 db = dsa->dsa_zgd->zgd_db;
1871 /*
1872 * A block of zeros may compress to a hole, but the
1873 * block size still needs to be known for replay.
1874 */
1875 BP_SET_LSIZE(bp, db->db_size);
1876 } else if (!BP_IS_EMBEDDED(bp)) {
1877 ASSERT0(BP_GET_LEVEL(bp));
1878 BP_SET_FILL(bp, 1);
1879 }
1880 }
1881 }
1882
1883 static void
dmu_sync_late_arrival_ready(zio_t * zio)1884 dmu_sync_late_arrival_ready(zio_t *zio)
1885 {
1886 dmu_sync_ready(zio, NULL, zio->io_private);
1887 }
1888
1889 void
dmu_sync_done(zio_t * zio,arc_buf_t * buf,void * varg)1890 dmu_sync_done(zio_t *zio, arc_buf_t *buf, void *varg)
1891 {
1892 (void) buf;
1893 dmu_sync_arg_t *dsa = varg;
1894 dbuf_dirty_record_t *dr = dsa->dsa_dr;
1895 dmu_buf_impl_t *db = dr->dr_dbuf;
1896 zgd_t *zgd = dsa->dsa_zgd;
1897
1898 /*
1899 * Record the vdev(s) backing this blkptr so they can be flushed after
1900 * the writes for the lwb have completed.
1901 */
1902 if (zgd && zio->io_error == 0) {
1903 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1904 }
1905
1906 mutex_enter(&db->db_mtx);
1907 ASSERT(dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC);
1908 if (zio->io_error == 0) {
1909 ASSERT0(dr->dt.dl.dr_has_raw_params);
1910 dr->dt.dl.dr_nopwrite = !!(zio->io_flags & ZIO_FLAG_NOPWRITE);
1911 if (dr->dt.dl.dr_nopwrite) {
1912 blkptr_t *bp = zio->io_bp;
1913 blkptr_t *bp_orig = &zio->io_bp_orig;
1914 uint8_t chksum = BP_GET_CHECKSUM(bp_orig);
1915
1916 ASSERT(BP_EQUAL(bp, bp_orig));
1917 VERIFY(BP_EQUAL(bp, db->db_blkptr));
1918 ASSERT(zio->io_prop.zp_compress != ZIO_COMPRESS_OFF);
1919 VERIFY(zio_checksum_table[chksum].ci_flags &
1920 ZCHECKSUM_FLAG_NOPWRITE);
1921 }
1922 dr->dt.dl.dr_overridden_by = *zio->io_bp;
1923 dr->dt.dl.dr_override_state = DR_OVERRIDDEN;
1924 dr->dt.dl.dr_copies = zio->io_prop.zp_copies;
1925 dr->dt.dl.dr_gang_copies = zio->io_prop.zp_gang_copies;
1926
1927 /*
1928 * Old style holes are filled with all zeros, whereas
1929 * new-style holes maintain their lsize, type, level,
1930 * and birth time (see zio_write_compress). While we
1931 * need to reset the BP_SET_LSIZE() call that happened
1932 * in dmu_sync_ready for old style holes, we do *not*
1933 * want to wipe out the information contained in new
1934 * style holes. Thus, only zero out the block pointer if
1935 * it's an old style hole.
1936 */
1937 if (BP_IS_HOLE(&dr->dt.dl.dr_overridden_by) &&
1938 BP_GET_LOGICAL_BIRTH(&dr->dt.dl.dr_overridden_by) == 0)
1939 BP_ZERO(&dr->dt.dl.dr_overridden_by);
1940 } else {
1941 dr->dt.dl.dr_override_state = DR_NOT_OVERRIDDEN;
1942 }
1943
1944 cv_broadcast(&db->db_changed);
1945 mutex_exit(&db->db_mtx);
1946
1947 if (dsa->dsa_done)
1948 dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1949
1950 kmem_free(dsa, sizeof (*dsa));
1951 }
1952
1953 static void
dmu_sync_late_arrival_done(zio_t * zio)1954 dmu_sync_late_arrival_done(zio_t *zio)
1955 {
1956 blkptr_t *bp = zio->io_bp;
1957 dmu_sync_arg_t *dsa = zio->io_private;
1958 zgd_t *zgd = dsa->dsa_zgd;
1959
1960 if (zio->io_error == 0) {
1961 /*
1962 * Record the vdev(s) backing this blkptr so they can be
1963 * flushed after the writes for the lwb have completed.
1964 */
1965 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1966
1967 if (!BP_IS_HOLE(bp)) {
1968 blkptr_t *bp_orig __maybe_unused = &zio->io_bp_orig;
1969 ASSERT(!(zio->io_flags & ZIO_FLAG_NOPWRITE));
1970 ASSERT(BP_IS_HOLE(bp_orig) || !BP_EQUAL(bp, bp_orig));
1971 ASSERT(BP_GET_BIRTH(zio->io_bp) == zio->io_txg);
1972 ASSERT(zio->io_txg > spa_syncing_txg(zio->io_spa));
1973 zio_free(zio->io_spa, zio->io_txg, zio->io_bp);
1974 }
1975 }
1976
1977 dmu_tx_commit(dsa->dsa_tx);
1978
1979 dsa->dsa_done(dsa->dsa_zgd, zio->io_error);
1980
1981 abd_free(zio->io_abd);
1982 kmem_free(dsa, sizeof (*dsa));
1983 }
1984
1985 static int
dmu_sync_late_arrival(zio_t * pio,objset_t * os,dmu_sync_cb_t * done,zgd_t * zgd,zio_prop_t * zp,zbookmark_phys_t * zb)1986 dmu_sync_late_arrival(zio_t *pio, objset_t *os, dmu_sync_cb_t *done, zgd_t *zgd,
1987 zio_prop_t *zp, zbookmark_phys_t *zb)
1988 {
1989 dmu_sync_arg_t *dsa;
1990 dmu_tx_t *tx;
1991 int error;
1992
1993 error = dbuf_read((dmu_buf_impl_t *)zgd->zgd_db, NULL,
1994 DB_RF_CANFAIL | DMU_READ_NO_PREFETCH | DMU_KEEP_CACHING);
1995 if (error != 0)
1996 return (error);
1997
1998 tx = dmu_tx_create(os);
1999 dmu_tx_hold_space(tx, zgd->zgd_db->db_size);
2000 /*
2001 * This transaction does not produce any dirty data or log blocks, so
2002 * it should not be throttled. All other cases wait for TXG sync, by
2003 * which time the log block we are writing will be obsolete, so we can
2004 * skip waiting and just return error here instead.
2005 */
2006 if (dmu_tx_assign(tx, DMU_TX_NOWAIT | DMU_TX_NOTHROTTLE) != 0) {
2007 dmu_tx_abort(tx);
2008 /* Make zl_get_data do txg_waited_synced() */
2009 return (SET_ERROR(EIO));
2010 }
2011
2012 /*
2013 * In order to prevent the zgd's lwb from being free'd prior to
2014 * dmu_sync_late_arrival_done() being called, we have to ensure
2015 * the lwb's "max txg" takes this tx's txg into account.
2016 */
2017 zil_lwb_add_txg(zgd->zgd_lwb, dmu_tx_get_txg(tx));
2018
2019 dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
2020 dsa->dsa_dr = NULL;
2021 dsa->dsa_done = done;
2022 dsa->dsa_zgd = zgd;
2023 dsa->dsa_tx = tx;
2024
2025 /*
2026 * Since we are currently syncing this txg, it's nontrivial to
2027 * determine what BP to nopwrite against, so we disable nopwrite.
2028 *
2029 * When syncing, the db_blkptr is initially the BP of the previous
2030 * txg. We can not nopwrite against it because it will be changed
2031 * (this is similar to the non-late-arrival case where the dbuf is
2032 * dirty in a future txg).
2033 *
2034 * Then dbuf_write_ready() sets bp_blkptr to the location we will write.
2035 * We can not nopwrite against it because although the BP will not
2036 * (typically) be changed, the data has not yet been persisted to this
2037 * location.
2038 *
2039 * Finally, when dbuf_write_done() is called, it is theoretically
2040 * possible to always nopwrite, because the data that was written in
2041 * this txg is the same data that we are trying to write. However we
2042 * would need to check that this dbuf is not dirty in any future
2043 * txg's (as we do in the normal dmu_sync() path). For simplicity, we
2044 * don't nopwrite in this case.
2045 */
2046 zp->zp_nopwrite = B_FALSE;
2047
2048 zio_nowait(zio_write(pio, os->os_spa, dmu_tx_get_txg(tx), zgd->zgd_bp,
2049 abd_get_from_buf(zgd->zgd_db->db_data, zgd->zgd_db->db_size),
2050 zgd->zgd_db->db_size, zgd->zgd_db->db_size, zp,
2051 dmu_sync_late_arrival_ready, NULL, dmu_sync_late_arrival_done,
2052 dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL, zb));
2053
2054 return (0);
2055 }
2056
2057 /*
2058 * Intent log support: sync the block associated with db to disk.
2059 * N.B. and XXX: the caller is responsible for making sure that the
2060 * data isn't changing while dmu_sync() is writing it.
2061 *
2062 * Return values:
2063 *
2064 * EEXIST: this txg has already been synced, so there's nothing to do.
2065 * The caller should not log the write.
2066 *
2067 * ENOENT: the block was dbuf_free_range()'d, so there's nothing to do.
2068 * The caller should not log the write.
2069 *
2070 * EALREADY: this block is already in the process of being synced.
2071 * The caller should track its progress (somehow).
2072 *
2073 * EIO: could not do the I/O.
2074 * The caller should do a txg_wait_synced().
2075 *
2076 * 0: the I/O has been initiated.
2077 * The caller should log this blkptr in the done callback.
2078 * It is possible that the I/O will fail, in which case
2079 * the error will be reported to the done callback and
2080 * propagated to pio from zio_done().
2081 */
2082 int
dmu_sync(zio_t * pio,uint64_t txg,dmu_sync_cb_t * done,zgd_t * zgd)2083 dmu_sync(zio_t *pio, uint64_t txg, dmu_sync_cb_t *done, zgd_t *zgd)
2084 {
2085 dmu_buf_impl_t *db = (dmu_buf_impl_t *)zgd->zgd_db;
2086 objset_t *os = db->db_objset;
2087 dsl_dataset_t *ds = os->os_dsl_dataset;
2088 dbuf_dirty_record_t *dr, *dr_next;
2089 dmu_sync_arg_t *dsa;
2090 zbookmark_phys_t zb;
2091 zio_prop_t zp;
2092
2093 ASSERT(pio != NULL);
2094 ASSERT(txg != 0);
2095
2096 SET_BOOKMARK(&zb, ds->ds_object,
2097 db->db.db_object, db->db_level, db->db_blkid);
2098
2099 DB_DNODE_ENTER(db);
2100 dmu_write_policy(os, DB_DNODE(db), db->db_level, WP_DMU_SYNC, &zp);
2101 DB_DNODE_EXIT(db);
2102
2103 /*
2104 * If we're frozen (running ziltest), we always need to generate a bp.
2105 */
2106 if (txg > spa_freeze_txg(os->os_spa))
2107 return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
2108
2109 /*
2110 * Grabbing db_mtx now provides a barrier between dbuf_sync_leaf()
2111 * and us. If we determine that this txg is not yet syncing,
2112 * but it begins to sync a moment later, that's OK because the
2113 * sync thread will block in dbuf_sync_leaf() until we drop db_mtx.
2114 */
2115 mutex_enter(&db->db_mtx);
2116
2117 if (txg <= spa_last_synced_txg(os->os_spa)) {
2118 /*
2119 * This txg has already synced. There's nothing to do.
2120 */
2121 mutex_exit(&db->db_mtx);
2122 return (SET_ERROR(EEXIST));
2123 }
2124
2125 if (txg <= spa_syncing_txg(os->os_spa)) {
2126 /*
2127 * This txg is currently syncing, so we can't mess with
2128 * the dirty record anymore; just write a new log block.
2129 */
2130 mutex_exit(&db->db_mtx);
2131 return (dmu_sync_late_arrival(pio, os, done, zgd, &zp, &zb));
2132 }
2133
2134 dr = dbuf_find_dirty_eq(db, txg);
2135
2136 if (dr == NULL) {
2137 /*
2138 * There's no dr for this dbuf, so it must have been freed.
2139 * There's no need to log writes to freed blocks, so we're done.
2140 */
2141 mutex_exit(&db->db_mtx);
2142 return (SET_ERROR(ENOENT));
2143 }
2144
2145 dr_next = list_next(&db->db_dirty_records, dr);
2146 ASSERT(dr_next == NULL || dr_next->dr_txg < txg);
2147
2148 if (db->db_blkptr != NULL) {
2149 /*
2150 * We need to fill in zgd_bp with the current blkptr so that
2151 * the nopwrite code can check if we're writing the same
2152 * data that's already on disk. We can only nopwrite if we
2153 * are sure that after making the copy, db_blkptr will not
2154 * change until our i/o completes. We ensure this by
2155 * holding the db_mtx, and only allowing nopwrite if the
2156 * block is not already dirty (see below). This is verified
2157 * by dmu_sync_done(), which VERIFYs that the db_blkptr has
2158 * not changed.
2159 */
2160 *zgd->zgd_bp = *db->db_blkptr;
2161 }
2162
2163 /*
2164 * Assume the on-disk data is X, the current syncing data (in
2165 * txg - 1) is Y, and the current in-memory data is Z (currently
2166 * in dmu_sync).
2167 *
2168 * We usually want to perform a nopwrite if X and Z are the
2169 * same. However, if Y is different (i.e. the BP is going to
2170 * change before this write takes effect), then a nopwrite will
2171 * be incorrect - we would override with X, which could have
2172 * been freed when Y was written.
2173 *
2174 * (Note that this is not a concern when we are nop-writing from
2175 * syncing context, because X and Y must be identical, because
2176 * all previous txgs have been synced.)
2177 *
2178 * Therefore, we disable nopwrite if the current BP could change
2179 * before this TXG. There are two ways it could change: by
2180 * being dirty (dr_next is non-NULL), or by being freed
2181 * (dnode_block_freed()). This behavior is verified by
2182 * zio_done(), which VERIFYs that the override BP is identical
2183 * to the on-disk BP.
2184 */
2185 if (dr_next != NULL) {
2186 zp.zp_nopwrite = B_FALSE;
2187 } else {
2188 DB_DNODE_ENTER(db);
2189 if (dnode_block_freed(DB_DNODE(db), db->db_blkid))
2190 zp.zp_nopwrite = B_FALSE;
2191 DB_DNODE_EXIT(db);
2192 }
2193
2194 ASSERT(dr->dr_txg == txg);
2195 if (dr->dt.dl.dr_override_state == DR_IN_DMU_SYNC ||
2196 dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
2197 /*
2198 * We have already issued a sync write for this buffer,
2199 * or this buffer has already been synced. It could not
2200 * have been dirtied since, or we would have cleared the state.
2201 */
2202 mutex_exit(&db->db_mtx);
2203 return (SET_ERROR(EALREADY));
2204 }
2205
2206 ASSERT0(dr->dt.dl.dr_has_raw_params);
2207 ASSERT(dr->dt.dl.dr_override_state == DR_NOT_OVERRIDDEN);
2208 dr->dt.dl.dr_override_state = DR_IN_DMU_SYNC;
2209 mutex_exit(&db->db_mtx);
2210
2211 dsa = kmem_alloc(sizeof (dmu_sync_arg_t), KM_SLEEP);
2212 dsa->dsa_dr = dr;
2213 dsa->dsa_done = done;
2214 dsa->dsa_zgd = zgd;
2215 dsa->dsa_tx = NULL;
2216
2217 zio_nowait(arc_write(pio, os->os_spa, txg, zgd->zgd_bp,
2218 dr->dt.dl.dr_data, !DBUF_IS_CACHEABLE(db),
2219 dbuf_is_l2cacheable(db, NULL), &zp, dmu_sync_ready, NULL,
2220 dmu_sync_done, dsa, ZIO_PRIORITY_SYNC_WRITE, ZIO_FLAG_CANFAIL,
2221 &zb));
2222
2223 return (0);
2224 }
2225
2226 int
dmu_object_set_nlevels(objset_t * os,uint64_t object,int nlevels,dmu_tx_t * tx)2227 dmu_object_set_nlevels(objset_t *os, uint64_t object, int nlevels, dmu_tx_t *tx)
2228 {
2229 dnode_t *dn;
2230 int err;
2231
2232 err = dnode_hold(os, object, FTAG, &dn);
2233 if (err)
2234 return (err);
2235 err = dnode_set_nlevels(dn, nlevels, tx);
2236 dnode_rele(dn, FTAG);
2237 return (err);
2238 }
2239
2240 int
dmu_object_set_blocksize(objset_t * os,uint64_t object,uint64_t size,int ibs,dmu_tx_t * tx)2241 dmu_object_set_blocksize(objset_t *os, uint64_t object, uint64_t size, int ibs,
2242 dmu_tx_t *tx)
2243 {
2244 dnode_t *dn;
2245 int err;
2246
2247 err = dnode_hold(os, object, FTAG, &dn);
2248 if (err)
2249 return (err);
2250 err = dnode_set_blksz(dn, size, ibs, tx);
2251 dnode_rele(dn, FTAG);
2252 return (err);
2253 }
2254
2255 int
dmu_object_set_maxblkid(objset_t * os,uint64_t object,uint64_t maxblkid,dmu_tx_t * tx)2256 dmu_object_set_maxblkid(objset_t *os, uint64_t object, uint64_t maxblkid,
2257 dmu_tx_t *tx)
2258 {
2259 dnode_t *dn;
2260 int err;
2261
2262 err = dnode_hold(os, object, FTAG, &dn);
2263 if (err)
2264 return (err);
2265 rw_enter(&dn->dn_struct_rwlock, RW_WRITER);
2266 dnode_new_blkid(dn, maxblkid, tx, B_FALSE, B_TRUE);
2267 rw_exit(&dn->dn_struct_rwlock);
2268 dnode_rele(dn, FTAG);
2269 return (0);
2270 }
2271
2272 void
dmu_object_set_checksum(objset_t * os,uint64_t object,uint8_t checksum,dmu_tx_t * tx)2273 dmu_object_set_checksum(objset_t *os, uint64_t object, uint8_t checksum,
2274 dmu_tx_t *tx)
2275 {
2276 dnode_t *dn;
2277
2278 /*
2279 * Send streams include each object's checksum function. This
2280 * check ensures that the receiving system can understand the
2281 * checksum function transmitted.
2282 */
2283 ASSERT3U(checksum, <, ZIO_CHECKSUM_LEGACY_FUNCTIONS);
2284
2285 VERIFY0(dnode_hold(os, object, FTAG, &dn));
2286 ASSERT3U(checksum, <, ZIO_CHECKSUM_FUNCTIONS);
2287 dn->dn_checksum = checksum;
2288 dnode_setdirty(dn, tx);
2289 dnode_rele(dn, FTAG);
2290 }
2291
2292 void
dmu_object_set_compress(objset_t * os,uint64_t object,uint8_t compress,dmu_tx_t * tx)2293 dmu_object_set_compress(objset_t *os, uint64_t object, uint8_t compress,
2294 dmu_tx_t *tx)
2295 {
2296 dnode_t *dn;
2297
2298 /*
2299 * Send streams include each object's compression function. This
2300 * check ensures that the receiving system can understand the
2301 * compression function transmitted.
2302 */
2303 ASSERT3U(compress, <, ZIO_COMPRESS_LEGACY_FUNCTIONS);
2304
2305 VERIFY0(dnode_hold(os, object, FTAG, &dn));
2306 dn->dn_compress = compress;
2307 dnode_setdirty(dn, tx);
2308 dnode_rele(dn, FTAG);
2309 }
2310
2311 /*
2312 * When the "redundant_metadata" property is set to "most", only indirect
2313 * blocks of this level and higher will have an additional ditto block.
2314 */
2315 static const int zfs_redundant_metadata_most_ditto_level = 2;
2316
2317 void
dmu_write_policy(objset_t * os,dnode_t * dn,int level,int wp,zio_prop_t * zp)2318 dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
2319 {
2320 dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;
2321 boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||
2322 (wp & WP_SPILL));
2323 enum zio_checksum checksum = os->os_checksum;
2324 enum zio_compress compress = os->os_compress;
2325 uint8_t complevel = os->os_complevel;
2326 enum zio_checksum dedup_checksum = os->os_dedup_checksum;
2327 boolean_t dedup = B_FALSE;
2328 boolean_t nopwrite = B_FALSE;
2329 boolean_t dedup_verify = os->os_dedup_verify;
2330 boolean_t encrypt = B_FALSE;
2331 int copies = os->os_copies;
2332 int gang_copies = os->os_copies;
2333
2334 /*
2335 * We maintain different write policies for each of the following
2336 * types of data:
2337 * 1. metadata
2338 * 2. preallocated blocks (i.e. level-0 blocks of a dump device)
2339 * 3. all other level 0 blocks
2340 */
2341 if (ismd) {
2342 /*
2343 * XXX -- we should design a compression algorithm
2344 * that specializes in arrays of bps.
2345 */
2346 compress = zio_compress_select(os->os_spa,
2347 ZIO_COMPRESS_ON, ZIO_COMPRESS_ON);
2348
2349 /*
2350 * Metadata always gets checksummed. If the data
2351 * checksum is multi-bit correctable, and it's not a
2352 * ZBT-style checksum, then it's suitable for metadata
2353 * as well. Otherwise, the metadata checksum defaults
2354 * to fletcher4.
2355 */
2356 if (!(zio_checksum_table[checksum].ci_flags &
2357 ZCHECKSUM_FLAG_METADATA) ||
2358 (zio_checksum_table[checksum].ci_flags &
2359 ZCHECKSUM_FLAG_EMBEDDED))
2360 checksum = ZIO_CHECKSUM_FLETCHER_4;
2361
2362 switch (os->os_redundant_metadata) {
2363 case ZFS_REDUNDANT_METADATA_ALL:
2364 copies++;
2365 gang_copies++;
2366 break;
2367 case ZFS_REDUNDANT_METADATA_MOST:
2368 if (level >= zfs_redundant_metadata_most_ditto_level ||
2369 DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))
2370 copies++;
2371 if (level + 1 >=
2372 zfs_redundant_metadata_most_ditto_level ||
2373 DMU_OT_IS_METADATA(type) || (wp & WP_SPILL))
2374 gang_copies++;
2375 break;
2376 case ZFS_REDUNDANT_METADATA_SOME:
2377 if (DMU_OT_IS_CRITICAL(type, level)) {
2378 copies++;
2379 gang_copies++;
2380 } else if (DMU_OT_IS_METADATA(type)) {
2381 gang_copies++;
2382 }
2383 break;
2384 case ZFS_REDUNDANT_METADATA_NONE:
2385 break;
2386 }
2387
2388 if (dmu_ddt_copies > 0) {
2389 /*
2390 * If this tunable is set, and this is a write for a
2391 * dedup entry store (zap or log), then we treat it
2392 * something like ZFS_REDUNDANT_METADATA_MOST on a
2393 * regular dataset: this many copies, and one more for
2394 * "higher" indirect blocks. This specific exception is
2395 * necessary because dedup objects are stored in the
2396 * MOS, which always has the highest possible copies.
2397 */
2398 dmu_object_type_t stype =
2399 dn ? dn->dn_storage_type : DMU_OT_NONE;
2400 if (stype == DMU_OT_NONE)
2401 stype = type;
2402 if (stype == DMU_OT_DDT_ZAP) {
2403 copies = dmu_ddt_copies;
2404 if (level >=
2405 zfs_redundant_metadata_most_ditto_level)
2406 copies++;
2407 }
2408 }
2409 } else if (wp & WP_NOFILL) {
2410 ASSERT0(level);
2411
2412 /*
2413 * If we're writing preallocated blocks, we aren't actually
2414 * writing them so don't set any policy properties. These
2415 * blocks are currently only used by an external subsystem
2416 * outside of zfs (i.e. dump) and not written by the zio
2417 * pipeline.
2418 */
2419 compress = ZIO_COMPRESS_OFF;
2420 checksum = ZIO_CHECKSUM_OFF;
2421 } else {
2422 compress = zio_compress_select(os->os_spa, dn->dn_compress,
2423 compress);
2424 complevel = zio_complevel_select(os->os_spa, compress,
2425 complevel, complevel);
2426
2427 /*
2428 * Storing many references to an all zeros block in the dedup
2429 * table would be expensive. Instead, if dedup is enabled,
2430 * store them as holes even if compression is not enabled.
2431 */
2432 if (compress == ZIO_COMPRESS_OFF &&
2433 dedup_checksum != ZIO_CHECKSUM_OFF)
2434 compress = ZIO_COMPRESS_EMPTY;
2435
2436 checksum = (dedup_checksum == ZIO_CHECKSUM_OFF) ?
2437 zio_checksum_select(dn->dn_checksum, checksum) :
2438 dedup_checksum;
2439
2440 /*
2441 * Determine dedup setting. If we are in dmu_sync(),
2442 * we won't actually dedup now because that's all
2443 * done in syncing context; but we do want to use the
2444 * dedup checksum. If the checksum is not strong
2445 * enough to ensure unique signatures, force
2446 * dedup_verify.
2447 */
2448 if (dedup_checksum != ZIO_CHECKSUM_OFF) {
2449 dedup = (wp & WP_DMU_SYNC) ? B_FALSE : B_TRUE;
2450 if (!(zio_checksum_table[checksum].ci_flags &
2451 ZCHECKSUM_FLAG_DEDUP))
2452 dedup_verify = B_TRUE;
2453 }
2454
2455 /*
2456 * Enable nopwrite if we have secure enough checksum
2457 * algorithm (see comment in zio_nop_write) and
2458 * compression is enabled. We don't enable nopwrite if
2459 * dedup is enabled as the two features are mutually
2460 * exclusive.
2461 */
2462 nopwrite = (!dedup && (zio_checksum_table[checksum].ci_flags &
2463 ZCHECKSUM_FLAG_NOPWRITE) &&
2464 compress != ZIO_COMPRESS_OFF && zfs_nopwrite_enabled);
2465
2466 if (os->os_redundant_metadata == ZFS_REDUNDANT_METADATA_ALL ||
2467 (os->os_redundant_metadata ==
2468 ZFS_REDUNDANT_METADATA_MOST &&
2469 zfs_redundant_metadata_most_ditto_level <= 1))
2470 gang_copies++;
2471 }
2472
2473 /*
2474 * All objects in an encrypted objset are protected from modification
2475 * via a MAC. Encrypted objects store their IV and salt in the last DVA
2476 * in the bp, so we cannot use all copies. Encrypted objects are also
2477 * not subject to nopwrite since writing the same data will still
2478 * result in a new ciphertext. Only encrypted blocks can be dedup'd
2479 * to avoid ambiguity in the dedup code since the DDT does not store
2480 * object types.
2481 */
2482 if (os->os_encrypted && (wp & WP_NOFILL) == 0) {
2483 encrypt = B_TRUE;
2484
2485 if (DMU_OT_IS_ENCRYPTED(type)) {
2486 copies = MIN(copies, SPA_DVAS_PER_BP - 1);
2487 gang_copies = MIN(gang_copies, SPA_DVAS_PER_BP - 1);
2488 nopwrite = B_FALSE;
2489 } else {
2490 dedup = B_FALSE;
2491 }
2492
2493 if (level <= 0 &&
2494 (type == DMU_OT_DNODE || type == DMU_OT_OBJSET)) {
2495 compress = ZIO_COMPRESS_EMPTY;
2496 }
2497 }
2498
2499 zp->zp_compress = compress;
2500 zp->zp_complevel = complevel;
2501 zp->zp_checksum = checksum;
2502 zp->zp_type = (wp & WP_SPILL) ? dn->dn_bonustype : type;
2503 zp->zp_level = level;
2504 zp->zp_copies = MIN(copies, spa_max_replication(os->os_spa));
2505 zp->zp_gang_copies = MIN(MAX(gang_copies, copies),
2506 spa_max_replication(os->os_spa));
2507 zp->zp_dedup = dedup;
2508 zp->zp_dedup_verify = dedup && dedup_verify;
2509 zp->zp_nopwrite = nopwrite;
2510 zp->zp_encrypt = encrypt;
2511 zp->zp_byteorder = ZFS_HOST_BYTEORDER;
2512 zp->zp_direct_write = (wp & WP_DIRECT_WR) ? B_TRUE : B_FALSE;
2513 zp->zp_rewrite = B_FALSE;
2514 memset(zp->zp_salt, 0, ZIO_DATA_SALT_LEN);
2515 memset(zp->zp_iv, 0, ZIO_DATA_IV_LEN);
2516 memset(zp->zp_mac, 0, ZIO_DATA_MAC_LEN);
2517 zp->zp_zpl_smallblk = (DMU_OT_IS_FILE(zp->zp_type) ||
2518 zp->zp_type == DMU_OT_ZVOL) ?
2519 os->os_zpl_special_smallblock : 0;
2520 zp->zp_storage_type = dn ? dn->dn_storage_type : DMU_OT_NONE;
2521
2522 ASSERT3U(zp->zp_compress, !=, ZIO_COMPRESS_INHERIT);
2523 }
2524
2525 /*
2526 * Reports the location of data and holes in an object. In order to
2527 * accurately report holes all dirty data must be synced to disk. This
2528 * causes extremely poor performance when seeking for holes in a dirty file.
2529 * As a compromise, only provide hole data when the dnode is clean. When
2530 * a dnode is dirty report the dnode as having no holes by returning EBUSY
2531 * which is always safe to do.
2532 */
2533 int
dmu_offset_next(objset_t * os,uint64_t object,boolean_t hole,uint64_t * off)2534 dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole, uint64_t *off)
2535 {
2536 dnode_t *dn;
2537 uint64_t txg, maxtxg = 0;
2538 int err;
2539
2540 restart:
2541 err = dnode_hold(os, object, FTAG, &dn);
2542 if (err)
2543 return (err);
2544
2545 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2546
2547 if (dnode_is_dirty(dn)) {
2548 /*
2549 * If the zfs_dmu_offset_next_sync module option is enabled
2550 * then hole reporting has been requested. Dirty dnodes
2551 * must be synced to disk to accurately report holes.
2552 *
2553 * Provided a RL_READER rangelock spanning 0-UINT64_MAX is
2554 * held by the caller only limited restarts will be required.
2555 * We tolerate callers which do not hold the rangelock by
2556 * returning EBUSY and not reporting holes after at most
2557 * TXG_CONCURRENT_STATES (3) restarts.
2558 */
2559 if (zfs_dmu_offset_next_sync) {
2560 rw_exit(&dn->dn_struct_rwlock);
2561 dnode_rele(dn, FTAG);
2562
2563 if (maxtxg == 0) {
2564 txg = spa_last_synced_txg(dmu_objset_spa(os));
2565 maxtxg = txg + TXG_CONCURRENT_STATES;
2566 } else if (txg >= maxtxg)
2567 return (SET_ERROR(EBUSY));
2568
2569 txg_wait_synced(dmu_objset_pool(os), ++txg);
2570 goto restart;
2571 }
2572
2573 err = SET_ERROR(EBUSY);
2574 } else {
2575 err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK |
2576 (hole ? DNODE_FIND_HOLE : 0), off, 1, 1, 0);
2577 }
2578
2579 rw_exit(&dn->dn_struct_rwlock);
2580 dnode_rele(dn, FTAG);
2581
2582 return (err);
2583 }
2584
2585 int
dmu_read_l0_bps(objset_t * os,uint64_t object,uint64_t offset,uint64_t length,blkptr_t * bps,size_t * nbpsp)2586 dmu_read_l0_bps(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,
2587 blkptr_t *bps, size_t *nbpsp)
2588 {
2589 dmu_buf_t **dbp, *dbuf;
2590 dmu_buf_impl_t *db;
2591 blkptr_t *bp;
2592 int error, numbufs;
2593
2594 error = dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG,
2595 &numbufs, &dbp);
2596 if (error != 0) {
2597 if (error == ESRCH) {
2598 error = SET_ERROR(ENXIO);
2599 }
2600 return (error);
2601 }
2602
2603 ASSERT3U(numbufs, <=, *nbpsp);
2604
2605 for (int i = 0; i < numbufs; i++) {
2606 dbuf = dbp[i];
2607 db = (dmu_buf_impl_t *)dbuf;
2608
2609 mutex_enter(&db->db_mtx);
2610
2611 if (!list_is_empty(&db->db_dirty_records)) {
2612 dbuf_dirty_record_t *dr;
2613
2614 dr = list_head(&db->db_dirty_records);
2615 if (dr->dt.dl.dr_brtwrite) {
2616 /*
2617 * This is very special case where we clone a
2618 * block and in the same transaction group we
2619 * read its BP (most likely to clone the clone).
2620 */
2621 bp = &dr->dt.dl.dr_overridden_by;
2622 } else {
2623 /*
2624 * The block was modified in the same
2625 * transaction group.
2626 */
2627 mutex_exit(&db->db_mtx);
2628 error = SET_ERROR(EAGAIN);
2629 goto out;
2630 }
2631 } else {
2632 bp = db->db_blkptr;
2633 }
2634
2635 mutex_exit(&db->db_mtx);
2636
2637 if (bp == NULL) {
2638 /*
2639 * The file size was increased, but the block was never
2640 * written, otherwise we would either have the block
2641 * pointer or the dirty record and would not get here.
2642 * It is effectively a hole, so report it as such.
2643 */
2644 BP_ZERO(&bps[i]);
2645 continue;
2646 }
2647 /*
2648 * Make sure we clone only data blocks.
2649 */
2650 if (BP_IS_METADATA(bp) && !BP_IS_HOLE(bp)) {
2651 error = SET_ERROR(EINVAL);
2652 goto out;
2653 }
2654
2655 /*
2656 * If the block was allocated in transaction group that is not
2657 * yet synced, we could clone it, but we couldn't write this
2658 * operation into ZIL, or it may be impossible to replay, since
2659 * the block may appear not yet allocated at that point.
2660 */
2661 if (BP_GET_PHYSICAL_BIRTH(bp) > spa_freeze_txg(os->os_spa)) {
2662 error = SET_ERROR(EINVAL);
2663 goto out;
2664 }
2665 if (BP_GET_PHYSICAL_BIRTH(bp) >
2666 spa_last_synced_txg(os->os_spa)) {
2667 error = SET_ERROR(EAGAIN);
2668 goto out;
2669 }
2670
2671 bps[i] = *bp;
2672 }
2673
2674 *nbpsp = numbufs;
2675 out:
2676 dmu_buf_rele_array(dbp, numbufs, FTAG);
2677
2678 return (error);
2679 }
2680
2681 int
dmu_brt_clone(objset_t * os,uint64_t object,uint64_t offset,uint64_t length,dmu_tx_t * tx,const blkptr_t * bps,size_t nbps)2682 dmu_brt_clone(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,
2683 dmu_tx_t *tx, const blkptr_t *bps, size_t nbps)
2684 {
2685 spa_t *spa;
2686 dmu_buf_t **dbp, *dbuf;
2687 dmu_buf_impl_t *db;
2688 struct dirty_leaf *dl;
2689 dbuf_dirty_record_t *dr;
2690 const blkptr_t *bp;
2691 int error = 0, i, numbufs;
2692
2693 spa = os->os_spa;
2694
2695 VERIFY0(dmu_buf_hold_array(os, object, offset, length, FALSE, FTAG,
2696 &numbufs, &dbp));
2697 ASSERT3U(nbps, ==, numbufs);
2698
2699 /*
2700 * Before we start cloning make sure that the dbufs sizes match new BPs
2701 * sizes. If they don't, that's a no-go, as we are not able to shrink
2702 * dbufs.
2703 */
2704 for (i = 0; i < numbufs; i++) {
2705 dbuf = dbp[i];
2706 db = (dmu_buf_impl_t *)dbuf;
2707 bp = &bps[i];
2708
2709 ASSERT3U(db->db.db_object, !=, DMU_META_DNODE_OBJECT);
2710 ASSERT0(db->db_level);
2711 ASSERT(db->db_blkid != DMU_BONUS_BLKID);
2712 ASSERT(db->db_blkid != DMU_SPILL_BLKID);
2713
2714 if (!BP_IS_HOLE(bp) && BP_GET_LSIZE(bp) != dbuf->db_size) {
2715 error = SET_ERROR(EXDEV);
2716 goto out;
2717 }
2718 }
2719
2720 for (i = 0; i < numbufs; i++) {
2721 dbuf = dbp[i];
2722 db = (dmu_buf_impl_t *)dbuf;
2723 bp = &bps[i];
2724
2725 dmu_buf_will_clone_or_dio(dbuf, tx);
2726
2727 mutex_enter(&db->db_mtx);
2728
2729 dr = list_head(&db->db_dirty_records);
2730 VERIFY(dr != NULL);
2731 ASSERT3U(dr->dr_txg, ==, tx->tx_txg);
2732 dl = &dr->dt.dl;
2733 ASSERT0(dl->dr_has_raw_params);
2734 dl->dr_overridden_by = *bp;
2735 if (!BP_IS_HOLE(bp) || BP_GET_LOGICAL_BIRTH(bp) != 0) {
2736 if (!BP_IS_EMBEDDED(bp)) {
2737 BP_SET_BIRTH(&dl->dr_overridden_by, dr->dr_txg,
2738 BP_GET_PHYSICAL_BIRTH(bp));
2739 BP_SET_REWRITE(&dl->dr_overridden_by, 0);
2740 } else {
2741 BP_SET_LOGICAL_BIRTH(&dl->dr_overridden_by,
2742 dr->dr_txg);
2743 }
2744 }
2745 dl->dr_brtwrite = B_TRUE;
2746 dl->dr_override_state = DR_OVERRIDDEN;
2747
2748 mutex_exit(&db->db_mtx);
2749
2750 /*
2751 * When data in embedded into BP there is no need to create
2752 * BRT entry as there is no data block. Just copy the BP as
2753 * it contains the data.
2754 */
2755 if (!BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) {
2756 brt_pending_add(spa, bp, tx);
2757 }
2758 }
2759 out:
2760 dmu_buf_rele_array(dbp, numbufs, FTAG);
2761
2762 return (error);
2763 }
2764
2765 void
__dmu_object_info_from_dnode(dnode_t * dn,dmu_object_info_t * doi)2766 __dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
2767 {
2768 dnode_phys_t *dnp = dn->dn_phys;
2769
2770 doi->doi_data_block_size = dn->dn_datablksz;
2771 doi->doi_metadata_block_size = dn->dn_indblkshift ?
2772 1ULL << dn->dn_indblkshift : 0;
2773 doi->doi_type = dn->dn_type;
2774 doi->doi_bonus_type = dn->dn_bonustype;
2775 doi->doi_bonus_size = dn->dn_bonuslen;
2776 doi->doi_dnodesize = dn->dn_num_slots << DNODE_SHIFT;
2777 doi->doi_indirection = dn->dn_nlevels;
2778 doi->doi_checksum = dn->dn_checksum;
2779 doi->doi_compress = dn->dn_compress;
2780 doi->doi_nblkptr = dn->dn_nblkptr;
2781 doi->doi_physical_blocks_512 = (DN_USED_BYTES(dnp) + 256) >> 9;
2782 doi->doi_max_offset = (dn->dn_maxblkid + 1) * dn->dn_datablksz;
2783 doi->doi_fill_count = 0;
2784 for (int i = 0; i < dnp->dn_nblkptr; i++)
2785 doi->doi_fill_count += BP_GET_FILL(&dnp->dn_blkptr[i]);
2786 }
2787
2788 void
dmu_object_info_from_dnode(dnode_t * dn,dmu_object_info_t * doi)2789 dmu_object_info_from_dnode(dnode_t *dn, dmu_object_info_t *doi)
2790 {
2791 rw_enter(&dn->dn_struct_rwlock, RW_READER);
2792 mutex_enter(&dn->dn_mtx);
2793
2794 __dmu_object_info_from_dnode(dn, doi);
2795
2796 mutex_exit(&dn->dn_mtx);
2797 rw_exit(&dn->dn_struct_rwlock);
2798 }
2799
2800 /*
2801 * Get information on a DMU object.
2802 * If doi is NULL, just indicates whether the object exists.
2803 */
2804 int
dmu_object_info(objset_t * os,uint64_t object,dmu_object_info_t * doi)2805 dmu_object_info(objset_t *os, uint64_t object, dmu_object_info_t *doi)
2806 {
2807 dnode_t *dn;
2808 int err = dnode_hold(os, object, FTAG, &dn);
2809
2810 if (err)
2811 return (err);
2812
2813 if (doi != NULL)
2814 dmu_object_info_from_dnode(dn, doi);
2815
2816 dnode_rele(dn, FTAG);
2817 return (0);
2818 }
2819
2820 /*
2821 * As above, but faster; can be used when you have a held dbuf in hand.
2822 */
2823 void
dmu_object_info_from_db(dmu_buf_t * db_fake,dmu_object_info_t * doi)2824 dmu_object_info_from_db(dmu_buf_t *db_fake, dmu_object_info_t *doi)
2825 {
2826 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2827
2828 DB_DNODE_ENTER(db);
2829 dmu_object_info_from_dnode(DB_DNODE(db), doi);
2830 DB_DNODE_EXIT(db);
2831 }
2832
2833 /*
2834 * Faster still when you only care about the size.
2835 */
2836 void
dmu_object_size_from_db(dmu_buf_t * db_fake,uint32_t * blksize,u_longlong_t * nblk512)2837 dmu_object_size_from_db(dmu_buf_t *db_fake, uint32_t *blksize,
2838 u_longlong_t *nblk512)
2839 {
2840 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2841 dnode_t *dn;
2842
2843 DB_DNODE_ENTER(db);
2844 dn = DB_DNODE(db);
2845
2846 *blksize = dn->dn_datablksz;
2847 /* add in number of slots used for the dnode itself */
2848 *nblk512 = ((DN_USED_BYTES(dn->dn_phys) + SPA_MINBLOCKSIZE/2) >>
2849 SPA_MINBLOCKSHIFT) + dn->dn_num_slots;
2850 DB_DNODE_EXIT(db);
2851 }
2852
2853 void
dmu_object_dnsize_from_db(dmu_buf_t * db_fake,int * dnsize)2854 dmu_object_dnsize_from_db(dmu_buf_t *db_fake, int *dnsize)
2855 {
2856 dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2857
2858 DB_DNODE_ENTER(db);
2859 *dnsize = DB_DNODE(db)->dn_num_slots << DNODE_SHIFT;
2860 DB_DNODE_EXIT(db);
2861 }
2862
2863 void
byteswap_uint64_array(void * vbuf,size_t size)2864 byteswap_uint64_array(void *vbuf, size_t size)
2865 {
2866 uint64_t *buf = vbuf;
2867 size_t count = size >> 3;
2868 int i;
2869
2870 ASSERT0((size & 7));
2871
2872 for (i = 0; i < count; i++)
2873 buf[i] = BSWAP_64(buf[i]);
2874 }
2875
2876 void
byteswap_uint32_array(void * vbuf,size_t size)2877 byteswap_uint32_array(void *vbuf, size_t size)
2878 {
2879 uint32_t *buf = vbuf;
2880 size_t count = size >> 2;
2881 int i;
2882
2883 ASSERT0((size & 3));
2884
2885 for (i = 0; i < count; i++)
2886 buf[i] = BSWAP_32(buf[i]);
2887 }
2888
2889 void
byteswap_uint16_array(void * vbuf,size_t size)2890 byteswap_uint16_array(void *vbuf, size_t size)
2891 {
2892 uint16_t *buf = vbuf;
2893 size_t count = size >> 1;
2894 int i;
2895
2896 ASSERT0((size & 1));
2897
2898 for (i = 0; i < count; i++)
2899 buf[i] = BSWAP_16(buf[i]);
2900 }
2901
2902 void
byteswap_uint8_array(void * vbuf,size_t size)2903 byteswap_uint8_array(void *vbuf, size_t size)
2904 {
2905 (void) vbuf, (void) size;
2906 }
2907
2908 void
dmu_init(void)2909 dmu_init(void)
2910 {
2911 abd_init();
2912 zfs_dbgmsg_init();
2913 sa_cache_init();
2914 dmu_objset_init();
2915 dnode_init();
2916 zfetch_init();
2917 dmu_tx_init();
2918 l2arc_init();
2919 arc_init();
2920 dbuf_init();
2921 }
2922
2923 void
dmu_fini(void)2924 dmu_fini(void)
2925 {
2926 arc_fini(); /* arc depends on l2arc, so arc must go first */
2927 l2arc_fini();
2928 dmu_tx_fini();
2929 zfetch_fini();
2930 dbuf_fini();
2931 dnode_fini();
2932 dmu_objset_fini();
2933 sa_cache_fini();
2934 zfs_dbgmsg_fini();
2935 abd_fini();
2936 }
2937
2938 EXPORT_SYMBOL(dmu_bonus_hold);
2939 EXPORT_SYMBOL(dmu_bonus_hold_by_dnode);
2940 EXPORT_SYMBOL(dmu_buf_hold_array_by_bonus);
2941 EXPORT_SYMBOL(dmu_buf_rele_array);
2942 EXPORT_SYMBOL(dmu_prefetch);
2943 EXPORT_SYMBOL(dmu_prefetch_by_dnode);
2944 EXPORT_SYMBOL(dmu_prefetch_dnode);
2945 EXPORT_SYMBOL(dmu_free_range);
2946 EXPORT_SYMBOL(dmu_free_long_range);
2947 EXPORT_SYMBOL(dmu_free_long_object);
2948 EXPORT_SYMBOL(dmu_read);
2949 EXPORT_SYMBOL(dmu_read_by_dnode);
2950 EXPORT_SYMBOL(dmu_read_uio);
2951 EXPORT_SYMBOL(dmu_read_uio_dbuf);
2952 EXPORT_SYMBOL(dmu_read_uio_dnode);
2953 EXPORT_SYMBOL(dmu_write);
2954 EXPORT_SYMBOL(dmu_write_by_dnode);
2955 EXPORT_SYMBOL(dmu_write_uio);
2956 EXPORT_SYMBOL(dmu_write_uio_dbuf);
2957 EXPORT_SYMBOL(dmu_write_uio_dnode);
2958 EXPORT_SYMBOL(dmu_prealloc);
2959 EXPORT_SYMBOL(dmu_object_info);
2960 EXPORT_SYMBOL(dmu_object_info_from_dnode);
2961 EXPORT_SYMBOL(dmu_object_info_from_db);
2962 EXPORT_SYMBOL(dmu_object_size_from_db);
2963 EXPORT_SYMBOL(dmu_object_dnsize_from_db);
2964 EXPORT_SYMBOL(dmu_object_set_nlevels);
2965 EXPORT_SYMBOL(dmu_object_set_blocksize);
2966 EXPORT_SYMBOL(dmu_object_set_maxblkid);
2967 EXPORT_SYMBOL(dmu_object_set_checksum);
2968 EXPORT_SYMBOL(dmu_object_set_compress);
2969 EXPORT_SYMBOL(dmu_offset_next);
2970 EXPORT_SYMBOL(dmu_write_policy);
2971 EXPORT_SYMBOL(dmu_sync);
2972 EXPORT_SYMBOL(dmu_request_arcbuf);
2973 EXPORT_SYMBOL(dmu_return_arcbuf);
2974 EXPORT_SYMBOL(dmu_assign_arcbuf_by_dnode);
2975 EXPORT_SYMBOL(dmu_assign_arcbuf_by_dbuf);
2976 EXPORT_SYMBOL(dmu_buf_hold);
2977 EXPORT_SYMBOL(dmu_ot);
2978
2979 ZFS_MODULE_PARAM(zfs, zfs_, nopwrite_enabled, INT, ZMOD_RW,
2980 "Enable NOP writes");
2981
2982 ZFS_MODULE_PARAM(zfs, zfs_, per_txg_dirty_frees_percent, UINT, ZMOD_RW,
2983 "Percentage of dirtied blocks from frees in one TXG");
2984
2985 ZFS_MODULE_PARAM(zfs, zfs_, dmu_offset_next_sync, INT, ZMOD_RW,
2986 "Enable forcing txg sync to find holes");
2987
2988 ZFS_MODULE_PARAM(zfs, , dmu_prefetch_max, UINT, ZMOD_RW,
2989 "Limit one prefetch call to this size");
2990
2991 ZFS_MODULE_PARAM(zfs, , dmu_ddt_copies, UINT, ZMOD_RW,
2992 "Override copies= for dedup objects");
2993