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