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