1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14 * Copyright (c) 2012, 2020 by Delphix. All rights reserved.
15 * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
16 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
17 */
18
19 #ifndef _SYS_DBUF_H
20 #define _SYS_DBUF_H
21
22 #include <sys/dmu.h>
23 #include <sys/spa.h>
24 #include <sys/txg.h>
25 #include <sys/zio.h>
26 #include <sys/arc.h>
27 #include <sys/zfs_context.h>
28 #include <sys/zfs_refcount.h>
29 #include <sys/zrlock.h>
30 #include <sys/multilist.h>
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 #define IN_DMU_SYNC 2
37
38 /*
39 * The simplified state transition diagram for dbufs looks like:
40 *
41 * +-------> READ ------+
42 * | |
43 * | V
44 * (alloc)-->UNCACHED CACHED-->EVICTING-->(free)
45 * ^ | ^ ^
46 * | | | |
47 * | +-------> FILL ------+ |
48 * | | | |
49 * | | | |
50 * | +------> NOFILL -----+-----> UNCACHED
51 * | | (Direct I/O)
52 * +---------------+
53 *
54 * DB_SEARCH is an invalid state for a dbuf. It is used by dbuf_free_range
55 * to find all dbufs in a range of a dnode and must be less than any other
56 * dbuf_states_t (see comment on dn_dbufs in dnode.h).
57 */
58 typedef enum dbuf_states {
59 DB_MARKER = -2,
60 DB_SEARCH = -1,
61 DB_UNCACHED,
62 DB_FILL,
63 DB_NOFILL,
64 DB_READ,
65 DB_CACHED,
66 DB_EVICTING
67 } dbuf_states_t;
68
69 typedef enum dbuf_cached_state {
70 DB_NO_CACHE = -1,
71 DB_DBUF_CACHE,
72 DB_DBUF_METADATA_CACHE,
73 DB_CACHE_MAX
74 } dbuf_cached_state_t;
75
76 struct dnode;
77 struct dmu_tx;
78
79 /*
80 * level = 0 means the user data
81 * level = 1 means the single indirect block
82 * etc.
83 */
84
85 struct dmu_buf_impl;
86
87 typedef enum override_states {
88 DR_NOT_OVERRIDDEN,
89 DR_IN_DMU_SYNC,
90 DR_OVERRIDDEN
91 } override_states_t;
92
93 typedef enum db_lock_type {
94 DLT_NONE,
95 DLT_PARENT,
96 DLT_OBJSET
97 } db_lock_type_t;
98
99 typedef struct dbuf_dirty_record {
100 /* link on our parents dirty list */
101 list_node_t dr_dirty_node;
102
103 /* transaction group this data will sync in */
104 uint64_t dr_txg;
105
106 /* zio of outstanding write IO */
107 zio_t *dr_zio;
108
109 /* pointer back to our dbuf */
110 struct dmu_buf_impl *dr_dbuf;
111
112 /* list link for dbuf dirty records */
113 list_node_t dr_dbuf_node;
114
115 /*
116 * The dnode we are part of. Note that the dnode can not be moved or
117 * evicted due to the hold that's added by dnode_setdirty() or
118 * dmu_objset_sync_dnodes(), and released by dnode_rele_task() or
119 * userquota_updates_task(). This hold is necessary for
120 * dirty_lightweight_leaf-type dirty records, which don't have a hold
121 * on a dbuf.
122 */
123 dnode_t *dr_dnode;
124
125 /* pointer to parent dirty record */
126 struct dbuf_dirty_record *dr_parent;
127
128 /* How much space was changed to dsl_pool_dirty_space() for this? */
129 unsigned int dr_accounted;
130
131 /* A copy of the bp that points to us */
132 blkptr_t dr_bp_copy;
133
134 union dirty_types {
135 struct dirty_indirect {
136
137 /* protect access to list */
138 kmutex_t dr_mtx;
139
140 /* Our list of dirty children */
141 list_t dr_children;
142 } di;
143 struct dirty_leaf {
144
145 /*
146 * dr_data is set when we dirty the buffer
147 * so that we can retain the pointer even if it
148 * gets COW'd in a subsequent transaction group.
149 */
150 arc_buf_t *dr_data;
151 override_states_t dr_override_state;
152 uint8_t dr_copies;
153 uint8_t dr_gang_copies;
154 boolean_t dr_nopwrite;
155 boolean_t dr_brtwrite;
156 boolean_t dr_diowrite;
157 boolean_t dr_rewrite;
158 boolean_t dr_has_raw_params;
159
160 /* Override and raw params are mutually exclusive. */
161 union {
162 blkptr_t dr_overridden_by;
163 struct {
164 /*
165 * If dr_has_raw_params is set, the
166 * following crypt params will be set
167 * on the BP that's written.
168 */
169 boolean_t dr_byteorder;
170 uint8_t dr_salt[ZIO_DATA_SALT_LEN];
171 uint8_t dr_iv[ZIO_DATA_IV_LEN];
172 uint8_t dr_mac[ZIO_DATA_MAC_LEN];
173 };
174 };
175 } dl;
176 struct dirty_lightweight_leaf {
177 /*
178 * This dirty record refers to a leaf (level=0)
179 * block, whose dbuf has not been instantiated for
180 * performance reasons.
181 */
182 uint64_t dr_blkid;
183 abd_t *dr_abd;
184 zio_prop_t dr_props;
185 zio_flag_t dr_flags;
186 } dll;
187 } dt;
188 } dbuf_dirty_record_t;
189
190 typedef struct dmu_buf_impl {
191 /*
192 * The following members are immutable, with the exception of
193 * db.db_data, which is protected by db_mtx.
194 */
195
196 /* the publicly visible structure */
197 dmu_buf_t db;
198
199 /* the objset we belong to */
200 struct objset *db_objset;
201
202 /*
203 * Handle to safely access the dnode we belong to (NULL when evicted)
204 * if dnode_move() is used on the platform, or just dnode otherwise.
205 */
206 #if !defined(__linux__) && !defined(__FreeBSD__)
207 #define USE_DNODE_HANDLE 1
208 struct dnode_handle *db_dnode_handle;
209 #else
210 struct dnode *db_dnode;
211 #endif
212
213 /*
214 * our parent buffer; if the dnode points to us directly,
215 * db_parent == db_dnode_handle->dnh_dnode->dn_dbuf
216 * only accessed by sync thread ???
217 * (NULL when evicted)
218 * May change from NULL to non-NULL under the protection of db_mtx
219 * (see dbuf_check_blkptr())
220 */
221 struct dmu_buf_impl *db_parent;
222
223 /*
224 * link for hash table of all dmu_buf_impl_t's
225 */
226 struct dmu_buf_impl *db_hash_next;
227
228 /*
229 * Our link on the owner dnodes's dn_dbufs list.
230 * Protected by its dn_dbufs_mtx. Should be on the same cache line
231 * as db_level and db_blkid for the best avl_add() performance.
232 */
233 avl_node_t db_link;
234
235 /* our block number */
236 uint64_t db_blkid;
237
238 /*
239 * Pointer to the blkptr_t which points to us. May be NULL if we
240 * don't have one yet. (NULL when evicted)
241 */
242 blkptr_t *db_blkptr;
243
244 /*
245 * Our indirection level. Data buffers have db_level==0.
246 * Indirect buffers which point to data buffers have
247 * db_level==1. etc. Buffers which contain dnodes have
248 * db_level==0, since the dnodes are stored in a file.
249 */
250 uint8_t db_level;
251
252 /* This block was freed while a read or write was active. */
253 uint8_t db_freed_in_flight;
254
255 /*
256 * Evict user data as soon as the dirty and reference counts are equal.
257 */
258 uint8_t db_user_immediate_evict;
259
260 /*
261 * dnode_evict_dbufs() or dnode_evict_bonus() tried to evict this dbuf,
262 * but couldn't due to outstanding references. Evict once the refcount
263 * drops to 0.
264 */
265 uint8_t db_pending_evict;
266
267 /* Number of TXGs in which this buffer is dirty. */
268 uint8_t db_dirtycnt;
269
270 /* The buffer was partially read. More reads may follow. */
271 uint8_t db_partial_read;
272
273 /*
274 * Protects db_buf's contents if they contain an indirect block or data
275 * block of the meta-dnode. We use this lock to protect the structure of
276 * the block tree. This means that when modifying this dbuf's data, we
277 * grab its rwlock. When modifying its parent's data (including the
278 * blkptr to this dbuf), we grab the parent's rwlock. The lock ordering
279 * for this lock is:
280 * 1) dn_struct_rwlock
281 * 2) db_rwlock
282 * We don't currently grab multiple dbufs' db_rwlocks at once.
283 */
284 krwlock_t db_rwlock;
285
286 /* buffer holding our data */
287 arc_buf_t *db_buf;
288
289 /* db_mtx protects the members below */
290 kmutex_t db_mtx;
291
292 /*
293 * Current state of the buffer
294 */
295 dbuf_states_t db_state;
296
297 /* In which dbuf cache this dbuf is, if any. */
298 dbuf_cached_state_t db_caching_status;
299
300 /*
301 * Refcount accessed by dmu_buf_{hold,rele}.
302 * If nonzero, the buffer can't be destroyed.
303 * Protected by db_mtx.
304 */
305 zfs_refcount_t db_holds;
306
307 kcondvar_t db_changed;
308 dbuf_dirty_record_t *db_data_pending;
309
310 /* List of dirty records for the buffer sorted newest to oldest. */
311 list_t db_dirty_records;
312
313 /* Link in dbuf_cache or dbuf_metadata_cache */
314 multilist_node_t db_cache_link;
315
316 uint64_t db_hash;
317
318 /* User callback information. */
319 dmu_buf_user_t *db_user;
320 } dmu_buf_impl_t;
321
322 #define DBUF_HASH_MUTEX(h, idx) \
323 (&(h)->hash_mutexes[(idx) & ((h)->hash_mutex_mask)])
324
325 typedef struct dbuf_hash_table {
326 uint64_t hash_table_mask;
327 uint64_t hash_mutex_mask;
328 dmu_buf_impl_t **hash_table;
329 kmutex_t *hash_mutexes;
330 } dbuf_hash_table_t;
331
332 typedef void (*dbuf_prefetch_fn)(void *, uint64_t, uint64_t, boolean_t);
333
334 extern kmem_cache_t *dbuf_dirty_kmem_cache;
335
336 uint64_t dbuf_whichblock(const struct dnode *di, const int64_t level,
337 const uint64_t offset);
338
339 void dbuf_create_bonus(struct dnode *dn);
340 int dbuf_spill_set_blksz(dmu_buf_t *db, uint64_t blksz, dmu_tx_t *tx);
341
342 void dbuf_rm_spill(struct dnode *dn, dmu_tx_t *tx);
343
344 dmu_buf_impl_t *dbuf_hold(struct dnode *dn, uint64_t blkid, const void *tag);
345 dmu_buf_impl_t *dbuf_hold_level(struct dnode *dn, int level, uint64_t blkid,
346 const void *tag);
347 int dbuf_hold_impl(struct dnode *dn, uint8_t level, uint64_t blkid,
348 boolean_t fail_sparse, boolean_t fail_uncached,
349 const void *tag, dmu_buf_impl_t **dbp);
350
351 int dbuf_prefetch_impl(struct dnode *dn, int64_t level, uint64_t blkid,
352 zio_priority_t prio, arc_flags_t aflags, dbuf_prefetch_fn cb,
353 void *arg);
354 int dbuf_prefetch(struct dnode *dn, int64_t level, uint64_t blkid,
355 zio_priority_t prio, arc_flags_t aflags);
356
357 void dbuf_add_ref(dmu_buf_impl_t *db, const void *tag);
358 boolean_t dbuf_try_add_ref(dmu_buf_t *db, objset_t *os, uint64_t obj,
359 uint64_t blkid, const void *tag);
360 uint64_t dbuf_refcount(dmu_buf_impl_t *db);
361
362 void dbuf_rele(dmu_buf_impl_t *db, const void *tag);
363 void dbuf_rele_and_unlock(dmu_buf_impl_t *db, const void *tag,
364 boolean_t evicting);
365
366 dmu_buf_impl_t *dbuf_find(struct objset *os, uint64_t object, uint8_t level,
367 uint64_t blkid, uint64_t *hash_out);
368
369 int dbuf_read(dmu_buf_impl_t *db, zio_t *zio, dmu_flags_t flags);
370 void dmu_buf_will_clone_or_dio(dmu_buf_t *db, dmu_tx_t *tx);
371 void dmu_buf_will_not_fill(dmu_buf_t *db, dmu_tx_t *tx);
372 void dmu_buf_will_fill(dmu_buf_t *db, dmu_tx_t *tx, boolean_t canfail);
373 void dmu_buf_will_fill_flags(dmu_buf_t *db, dmu_tx_t *tx, boolean_t canfail,
374 dmu_flags_t flags);
375 boolean_t dmu_buf_fill_done(dmu_buf_t *db, dmu_tx_t *tx, boolean_t failed);
376 void dbuf_assign_arcbuf(dmu_buf_impl_t *db, arc_buf_t *buf, dmu_tx_t *tx,
377 dmu_flags_t flags);
378 dbuf_dirty_record_t *dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
379 dbuf_dirty_record_t *dbuf_dirty_lightweight(dnode_t *dn, uint64_t blkid,
380 dmu_tx_t *tx);
381 boolean_t dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
382 int dmu_buf_get_bp_from_dbuf(dmu_buf_impl_t *db, blkptr_t **bp);
383 int dmu_buf_untransform_direct(dmu_buf_impl_t *db, spa_t *spa);
384 void dmu_buf_write_embedded(dmu_buf_t *dbuf, void *data,
385 bp_embedded_type_t etype, enum zio_compress comp,
386 int uncompressed_size, int compressed_size, int byteorder, dmu_tx_t *tx);
387
388 int dmu_lightweight_write_by_dnode(dnode_t *dn, uint64_t offset, abd_t *abd,
389 const struct zio_prop *zp, zio_flag_t flags, dmu_tx_t *tx);
390
391 void dmu_buf_redact(dmu_buf_t *dbuf, dmu_tx_t *tx);
392 void dbuf_destroy(dmu_buf_impl_t *db);
393
394 void dbuf_unoverride(dbuf_dirty_record_t *dr);
395 void dbuf_sync_list(list_t *list, int level, dmu_tx_t *tx);
396 void dbuf_release_bp(dmu_buf_impl_t *db);
397 db_lock_type_t dmu_buf_lock_parent(dmu_buf_impl_t *db, krw_t rw,
398 const void *tag);
399 void dmu_buf_unlock_parent(dmu_buf_impl_t *db, db_lock_type_t type,
400 const void *tag);
401
402 void dbuf_free_range(struct dnode *dn, uint64_t start, uint64_t end,
403 struct dmu_tx *);
404 void dbuf_evict_range(struct dnode *dn, uint64_t start_blkid,
405 uint64_t end_blkid);
406
407 void dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx);
408
409 void dbuf_stats_init(dbuf_hash_table_t *hash);
410 void dbuf_stats_destroy(void);
411
412 int dbuf_dnode_findbp(dnode_t *dn, uint64_t level, uint64_t blkid,
413 blkptr_t *bp, uint16_t *datablkszsec, uint8_t *indblkshift);
414
415 #ifdef USE_DNODE_HANDLE
416 #define DB_DNODE(_db) ((_db)->db_dnode_handle->dnh_dnode)
417 #define DB_DNODE_LOCK(_db) ((_db)->db_dnode_handle->dnh_zrlock)
418 #define DB_DNODE_ENTER(_db) (zrl_add(&DB_DNODE_LOCK(_db)))
419 #define DB_DNODE_EXIT(_db) (zrl_remove(&DB_DNODE_LOCK(_db)))
420 #define DB_DNODE_HELD(_db) (!zrl_is_zero(&DB_DNODE_LOCK(_db)))
421 #else
422 #define DB_DNODE(_db) ((_db)->db_dnode)
423 #define DB_DNODE_LOCK(_db)
424 #define DB_DNODE_ENTER(_db)
425 #define DB_DNODE_EXIT(_db)
426 #define DB_DNODE_HELD(_db) (B_TRUE)
427 #endif
428
429 void dbuf_init(void);
430 void dbuf_fini(void);
431 void dbuf_cache_reduce_target_size(void);
432
433 boolean_t dbuf_is_metadata(dmu_buf_impl_t *db);
434
435 static inline dbuf_dirty_record_t *
dbuf_find_dirty_lte(dmu_buf_impl_t * db,uint64_t txg)436 dbuf_find_dirty_lte(dmu_buf_impl_t *db, uint64_t txg)
437 {
438 dbuf_dirty_record_t *dr;
439
440 for (dr = list_head(&db->db_dirty_records);
441 dr != NULL && dr->dr_txg > txg;
442 dr = list_next(&db->db_dirty_records, dr))
443 continue;
444 return (dr);
445 }
446
447 static inline dbuf_dirty_record_t *
dbuf_find_dirty_eq(dmu_buf_impl_t * db,uint64_t txg)448 dbuf_find_dirty_eq(dmu_buf_impl_t *db, uint64_t txg)
449 {
450 dbuf_dirty_record_t *dr;
451
452 dr = dbuf_find_dirty_lte(db, txg);
453 if (dr && dr->dr_txg == txg)
454 return (dr);
455 return (NULL);
456 }
457
458 #define DBUF_GET_BUFC_TYPE(_db) \
459 (dbuf_is_metadata(_db) ? ARC_BUFC_METADATA : ARC_BUFC_DATA)
460
461 #define DBUF_IS_CACHEABLE(_db) (!(_db)->db_pending_evict && \
462 ((_db)->db_objset->os_primary_cache == ZFS_CACHE_ALL || \
463 (dbuf_is_metadata(_db) && \
464 ((_db)->db_objset->os_primary_cache == ZFS_CACHE_METADATA))))
465
466 boolean_t dbuf_is_l2cacheable(dmu_buf_impl_t *db, blkptr_t *db_bp);
467
468 #ifdef ZFS_DEBUG
469
470 /*
471 * There should be a ## between the string literal and fmt, to make it
472 * clear that we're joining two strings together, but gcc does not
473 * support that preprocessor token.
474 */
475 #define dprintf_dbuf(dbuf, fmt, ...) do { \
476 if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
477 char __db_buf[32]; \
478 uint64_t __db_obj = (dbuf)->db.db_object; \
479 if (__db_obj == DMU_META_DNODE_OBJECT) \
480 (void) strlcpy(__db_buf, "mdn", sizeof (__db_buf)); \
481 else \
482 (void) snprintf(__db_buf, sizeof (__db_buf), "%lld", \
483 (u_longlong_t)__db_obj); \
484 dprintf_ds((dbuf)->db_objset->os_dsl_dataset, \
485 "obj=%s lvl=%u blkid=%lld " fmt, \
486 __db_buf, (dbuf)->db_level, \
487 (u_longlong_t)(dbuf)->db_blkid, __VA_ARGS__); \
488 } \
489 } while (0)
490
491 #define dprintf_dbuf_bp(db, bp, fmt, ...) do { \
492 if (zfs_flags & ZFS_DEBUG_DPRINTF) { \
493 char *__blkbuf = kmem_alloc(BP_SPRINTF_LEN, KM_SLEEP); \
494 snprintf_blkptr(__blkbuf, BP_SPRINTF_LEN, bp); \
495 dprintf_dbuf(db, fmt " %s\n", __VA_ARGS__, __blkbuf); \
496 kmem_free(__blkbuf, BP_SPRINTF_LEN); \
497 } \
498 } while (0)
499
500 #define DBUF_VERIFY(db) dbuf_verify(db)
501
502 #else
503
504 #define dprintf_dbuf(db, fmt, ...)
505 #define dprintf_dbuf_bp(db, bp, fmt, ...)
506 #define DBUF_VERIFY(db)
507
508 #endif
509
510
511 #ifdef __cplusplus
512 }
513 #endif
514
515 #endif /* _SYS_DBUF_H */
516