1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _BCACHEFS_JOURNAL_TYPES_H 3 #define _BCACHEFS_JOURNAL_TYPES_H 4 5 #include <linux/cache.h> 6 #include <linux/workqueue.h> 7 8 #include "alloc_types.h" 9 #include "super_types.h" 10 #include "fifo.h" 11 12 /* btree write buffer steals 8 bits for its own purposes: */ 13 #define JOURNAL_SEQ_MAX ((1ULL << 56) - 1) 14 15 #define JOURNAL_STATE_BUF_BITS 2 16 #define JOURNAL_STATE_BUF_NR (1U << JOURNAL_STATE_BUF_BITS) 17 #define JOURNAL_STATE_BUF_MASK (JOURNAL_STATE_BUF_NR - 1) 18 19 #define JOURNAL_BUF_BITS 4 20 #define JOURNAL_BUF_NR (1U << JOURNAL_BUF_BITS) 21 #define JOURNAL_BUF_MASK (JOURNAL_BUF_NR - 1) 22 23 /* 24 * We put JOURNAL_BUF_NR of these in struct journal; we used them for writes to 25 * the journal that are being staged or in flight. 26 */ 27 struct journal_buf { 28 struct closure io; 29 struct jset *data; 30 31 __BKEY_PADDED(key, BCH_REPLICAS_MAX); 32 struct bch_devs_list devs_written; 33 34 struct closure_waitlist wait; 35 u64 last_seq; /* copy of data->last_seq */ 36 long expires; 37 u64 flush_time; 38 39 unsigned buf_size; /* size in bytes of @data */ 40 unsigned sectors; /* maximum size for current entry */ 41 unsigned disk_sectors; /* maximum size entry could have been, if 42 buf_size was bigger */ 43 unsigned u64s_reserved; 44 bool noflush:1; /* write has already been kicked off, and was noflush */ 45 bool must_flush:1; /* something wants a flush */ 46 bool separate_flush:1; 47 bool need_flush_to_write_buffer:1; 48 bool write_started:1; 49 bool write_allocated:1; 50 bool write_done:1; 51 u8 idx; 52 }; 53 54 /* 55 * Something that makes a journal entry dirty - i.e. a btree node that has to be 56 * flushed: 57 */ 58 59 enum journal_pin_type { 60 JOURNAL_PIN_TYPE_btree3, 61 JOURNAL_PIN_TYPE_btree2, 62 JOURNAL_PIN_TYPE_btree1, 63 JOURNAL_PIN_TYPE_btree0, 64 JOURNAL_PIN_TYPE_key_cache, 65 JOURNAL_PIN_TYPE_other, 66 JOURNAL_PIN_TYPE_NR, 67 }; 68 69 struct journal_entry_pin_list { 70 struct list_head unflushed[JOURNAL_PIN_TYPE_NR]; 71 struct list_head flushed[JOURNAL_PIN_TYPE_NR]; 72 atomic_t count; 73 struct bch_devs_list devs; 74 }; 75 76 struct journal; 77 struct journal_entry_pin; 78 typedef int (*journal_pin_flush_fn)(struct journal *j, 79 struct journal_entry_pin *, u64); 80 81 struct journal_entry_pin { 82 struct list_head list; 83 journal_pin_flush_fn flush; 84 u64 seq; 85 }; 86 87 struct journal_res { 88 bool ref; 89 u16 u64s; 90 u32 offset; 91 u64 seq; 92 }; 93 94 union journal_res_state { 95 struct { 96 atomic64_t counter; 97 }; 98 99 struct { 100 u64 v; 101 }; 102 103 struct { 104 u64 cur_entry_offset:22, 105 idx:2, 106 buf0_count:10, 107 buf1_count:10, 108 buf2_count:10, 109 buf3_count:10; 110 }; 111 }; 112 113 /* bytes: */ 114 #define JOURNAL_ENTRY_SIZE_MIN (64U << 10) /* 64k */ 115 #define JOURNAL_ENTRY_SIZE_MAX (4U << 22) /* 16M */ 116 117 /* 118 * We stash some journal state as sentinal values in cur_entry_offset: 119 * note - cur_entry_offset is in units of u64s 120 */ 121 #define JOURNAL_ENTRY_OFFSET_MAX ((1U << 22) - 1) 122 123 #define JOURNAL_ENTRY_BLOCKED_VAL (JOURNAL_ENTRY_OFFSET_MAX - 2) 124 #define JOURNAL_ENTRY_CLOSED_VAL (JOURNAL_ENTRY_OFFSET_MAX - 1) 125 #define JOURNAL_ENTRY_ERROR_VAL (JOURNAL_ENTRY_OFFSET_MAX) 126 127 struct journal_space { 128 /* Units of 512 bytes sectors: */ 129 unsigned next_entry; /* How big the next journal entry can be */ 130 unsigned total; 131 }; 132 133 enum journal_space_from { 134 journal_space_discarded, 135 journal_space_clean_ondisk, 136 journal_space_clean, 137 journal_space_total, 138 journal_space_nr, 139 }; 140 141 #define JOURNAL_FLAGS() \ 142 x(replay_done) \ 143 x(running) \ 144 x(may_skip_flush) \ 145 x(need_flush_write) \ 146 x(space_low) 147 148 enum journal_flags { 149 #define x(n) JOURNAL_##n, 150 JOURNAL_FLAGS() 151 #undef x 152 }; 153 154 struct journal_bio { 155 struct bch_dev *ca; 156 unsigned buf_idx; 157 u64 submit_time; 158 159 struct bio bio; 160 }; 161 162 /* Embedded in struct bch_fs */ 163 struct journal { 164 /* Fastpath stuff up front: */ 165 struct { 166 167 union journal_res_state reservations; 168 enum bch_watermark watermark; 169 170 } __aligned(SMP_CACHE_BYTES); 171 172 unsigned long flags; 173 174 /* Max size of current journal entry */ 175 unsigned cur_entry_u64s; 176 unsigned cur_entry_sectors; 177 178 /* Reserved space in journal entry to be used just prior to write */ 179 unsigned entry_u64s_reserved; 180 181 182 /* 183 * 0, or -ENOSPC if waiting on journal reclaim, or -EROFS if 184 * insufficient devices: 185 */ 186 int cur_entry_error; 187 unsigned cur_entry_offset_if_blocked; 188 189 unsigned buf_size_want; 190 /* 191 * We may queue up some things to be journalled (log messages) before 192 * the journal has actually started - stash them here: 193 */ 194 darray_u64 early_journal_entries; 195 196 /* 197 * Protects journal_buf->data, when accessing without a jorunal 198 * reservation: for synchronization between the btree write buffer code 199 * and the journal write path: 200 */ 201 struct mutex buf_lock; 202 /* 203 * Two journal entries -- one is currently open for new entries, the 204 * other is possibly being written out. 205 */ 206 struct journal_buf buf[JOURNAL_BUF_NR]; 207 void *free_buf; 208 unsigned free_buf_size; 209 210 spinlock_t lock; 211 212 /* if nonzero, we may not open a new journal entry: */ 213 unsigned blocked; 214 215 /* Used when waiting because the journal was full */ 216 wait_queue_head_t wait; 217 struct closure_waitlist async_wait; 218 struct closure_waitlist reclaim_flush_wait; 219 220 struct delayed_work write_work; 221 struct workqueue_struct *wq; 222 223 /* Sequence number of most recent journal entry (last entry in @pin) */ 224 atomic64_t seq; 225 226 u64 seq_write_started; 227 /* seq, last_seq from the most recent journal entry successfully written */ 228 u64 seq_ondisk; 229 u64 flushed_seq_ondisk; 230 u64 flushing_seq; 231 u64 last_seq_ondisk; 232 u64 err_seq; 233 u64 last_empty_seq; 234 u64 oldest_seq_found_ondisk; 235 236 /* 237 * FIFO of journal entries whose btree updates have not yet been 238 * written out. 239 * 240 * Each entry is a reference count. The position in the FIFO is the 241 * entry's sequence number relative to @seq. 242 * 243 * The journal entry itself holds a reference count, put when the 244 * journal entry is written out. Each btree node modified by the journal 245 * entry also holds a reference count, put when the btree node is 246 * written. 247 * 248 * When a reference count reaches zero, the journal entry is no longer 249 * needed. When all journal entries in the oldest journal bucket are no 250 * longer needed, the bucket can be discarded and reused. 251 */ 252 struct { 253 u64 front, back, size, mask; 254 struct journal_entry_pin_list *data; 255 } pin; 256 257 struct journal_space space[journal_space_nr]; 258 259 u64 replay_journal_seq; 260 u64 replay_journal_seq_end; 261 262 struct write_point wp; 263 spinlock_t err_lock; 264 265 struct mutex reclaim_lock; 266 /* 267 * Used for waiting until journal reclaim has freed up space in the 268 * journal: 269 */ 270 wait_queue_head_t reclaim_wait; 271 struct task_struct *reclaim_thread; 272 bool reclaim_kicked; 273 unsigned long next_reclaim; 274 u64 nr_direct_reclaim; 275 u64 nr_background_reclaim; 276 277 unsigned long last_flushed; 278 struct journal_entry_pin *flush_in_progress; 279 bool flush_in_progress_dropped; 280 wait_queue_head_t pin_flush_wait; 281 282 /* protects advancing ja->discard_idx: */ 283 struct mutex discard_lock; 284 bool can_discard; 285 286 unsigned long last_flush_write; 287 288 u64 write_start_time; 289 290 u64 nr_flush_writes; 291 u64 nr_noflush_writes; 292 u64 entry_bytes_written; 293 294 struct bch2_time_stats *flush_write_time; 295 struct bch2_time_stats *noflush_write_time; 296 struct bch2_time_stats *flush_seq_time; 297 298 #ifdef CONFIG_DEBUG_LOCK_ALLOC 299 struct lockdep_map res_map; 300 #endif 301 } __aligned(SMP_CACHE_BYTES); 302 303 /* 304 * Embedded in struct bch_dev. First three fields refer to the array of journal 305 * buckets, in bch_sb. 306 */ 307 struct journal_device { 308 /* 309 * For each journal bucket, contains the max sequence number of the 310 * journal writes it contains - so we know when a bucket can be reused. 311 */ 312 u64 *bucket_seq; 313 314 unsigned sectors_free; 315 316 /* 317 * discard_idx <= dirty_idx_ondisk <= dirty_idx <= cur_idx: 318 */ 319 unsigned discard_idx; /* Next bucket to discard */ 320 unsigned dirty_idx_ondisk; 321 unsigned dirty_idx; 322 unsigned cur_idx; /* Journal bucket we're currently writing to */ 323 unsigned nr; 324 325 u64 *buckets; 326 327 /* Bio for journal reads/writes to this device */ 328 struct journal_bio *bio[JOURNAL_BUF_NR]; 329 330 /* for bch_journal_read_device */ 331 struct closure read; 332 u64 highest_seq_found; 333 }; 334 335 /* 336 * journal_entry_res - reserve space in every journal entry: 337 */ 338 struct journal_entry_res { 339 unsigned u64s; 340 }; 341 342 #endif /* _BCACHEFS_JOURNAL_TYPES_H */ 343