1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _BCACHEFS_JOURNAL_H 3 #define _BCACHEFS_JOURNAL_H 4 5 /* 6 * THE JOURNAL: 7 * 8 * The primary purpose of the journal is to log updates (insertions) to the 9 * b-tree, to avoid having to do synchronous updates to the b-tree on disk. 10 * 11 * Without the journal, the b-tree is always internally consistent on 12 * disk - and in fact, in the earliest incarnations bcache didn't have a journal 13 * but did handle unclean shutdowns by doing all index updates synchronously 14 * (with coalescing). 15 * 16 * Updates to interior nodes still happen synchronously and without the journal 17 * (for simplicity) - this may change eventually but updates to interior nodes 18 * are rare enough it's not a huge priority. 19 * 20 * This means the journal is relatively separate from the b-tree; it consists of 21 * just a list of keys and journal replay consists of just redoing those 22 * insertions in same order that they appear in the journal. 23 * 24 * PERSISTENCE: 25 * 26 * For synchronous updates (where we're waiting on the index update to hit 27 * disk), the journal entry will be written out immediately (or as soon as 28 * possible, if the write for the previous journal entry was still in flight). 29 * 30 * Synchronous updates are specified by passing a closure (@flush_cl) to 31 * bch2_btree_insert() or bch_btree_insert_node(), which then pass that parameter 32 * down to the journalling code. That closure will wait on the journal write to 33 * complete (via closure_wait()). 34 * 35 * If the index update wasn't synchronous, the journal entry will be 36 * written out after 10 ms have elapsed, by default (the delay_ms field 37 * in struct journal). 38 * 39 * JOURNAL ENTRIES: 40 * 41 * A journal entry is variable size (struct jset), it's got a fixed length 42 * header and then a variable number of struct jset_entry entries. 43 * 44 * Journal entries are identified by monotonically increasing 64 bit sequence 45 * numbers - jset->seq; other places in the code refer to this sequence number. 46 * 47 * A jset_entry entry contains one or more bkeys (which is what gets inserted 48 * into the b-tree). We need a container to indicate which b-tree the key is 49 * for; also, the roots of the various b-trees are stored in jset_entry entries 50 * (one for each b-tree) - this lets us add new b-tree types without changing 51 * the on disk format. 52 * 53 * We also keep some things in the journal header that are logically part of the 54 * superblock - all the things that are frequently updated. This is for future 55 * bcache on raw flash support; the superblock (which will become another 56 * journal) can't be moved or wear leveled, so it contains just enough 57 * information to find the main journal, and the superblock only has to be 58 * rewritten when we want to move/wear level the main journal. 59 * 60 * JOURNAL LAYOUT ON DISK: 61 * 62 * The journal is written to a ringbuffer of buckets (which is kept in the 63 * superblock); the individual buckets are not necessarily contiguous on disk 64 * which means that journal entries are not allowed to span buckets, but also 65 * that we can resize the journal at runtime if desired (unimplemented). 66 * 67 * The journal buckets exist in the same pool as all the other buckets that are 68 * managed by the allocator and garbage collection - garbage collection marks 69 * the journal buckets as metadata buckets. 70 * 71 * OPEN/DIRTY JOURNAL ENTRIES: 72 * 73 * Open/dirty journal entries are journal entries that contain b-tree updates 74 * that have not yet been written out to the b-tree on disk. We have to track 75 * which journal entries are dirty, and we also have to avoid wrapping around 76 * the journal and overwriting old but still dirty journal entries with new 77 * journal entries. 78 * 79 * On disk, this is represented with the "last_seq" field of struct jset; 80 * last_seq is the first sequence number that journal replay has to replay. 81 * 82 * To avoid overwriting dirty journal entries on disk, we keep a mapping (in 83 * journal_device->seq) of for each journal bucket, the highest sequence number 84 * any journal entry it contains. Then, by comparing that against last_seq we 85 * can determine whether that journal bucket contains dirty journal entries or 86 * not. 87 * 88 * To track which journal entries are dirty, we maintain a fifo of refcounts 89 * (where each entry corresponds to a specific sequence number) - when a ref 90 * goes to 0, that journal entry is no longer dirty. 91 * 92 * Journalling of index updates is done at the same time as the b-tree itself is 93 * being modified (see btree_insert_key()); when we add the key to the journal 94 * the pending b-tree write takes a ref on the journal entry the key was added 95 * to. If a pending b-tree write would need to take refs on multiple dirty 96 * journal entries, it only keeps the ref on the oldest one (since a newer 97 * journal entry will still be replayed if an older entry was dirty). 98 * 99 * JOURNAL FILLING UP: 100 * 101 * There are two ways the journal could fill up; either we could run out of 102 * space to write to, or we could have too many open journal entries and run out 103 * of room in the fifo of refcounts. Since those refcounts are decremented 104 * without any locking we can't safely resize that fifo, so we handle it the 105 * same way. 106 * 107 * If the journal fills up, we start flushing dirty btree nodes until we can 108 * allocate space for a journal write again - preferentially flushing btree 109 * nodes that are pinning the oldest journal entries first. 110 */ 111 112 #include <linux/hash.h> 113 114 #include "journal_types.h" 115 116 struct bch_fs; 117 118 static inline void journal_wake(struct journal *j) 119 { 120 wake_up(&j->wait); 121 closure_wake_up(&j->async_wait); 122 } 123 124 /* Sequence number of oldest dirty journal entry */ 125 126 static inline u64 journal_last_seq(struct journal *j) 127 { 128 return j->pin.front; 129 } 130 131 static inline u64 journal_cur_seq(struct journal *j) 132 { 133 return atomic64_read(&j->seq); 134 } 135 136 static inline u64 journal_last_unwritten_seq(struct journal *j) 137 { 138 return j->seq_ondisk + 1; 139 } 140 141 static inline struct journal_buf *journal_cur_buf(struct journal *j) 142 { 143 unsigned idx = (journal_cur_seq(j) & 144 JOURNAL_BUF_MASK & 145 ~JOURNAL_STATE_BUF_MASK) + j->reservations.idx; 146 147 return j->buf + idx; 148 } 149 150 static inline int journal_state_count(union journal_res_state s, int idx) 151 { 152 switch (idx) { 153 case 0: return s.buf0_count; 154 case 1: return s.buf1_count; 155 case 2: return s.buf2_count; 156 case 3: return s.buf3_count; 157 } 158 BUG(); 159 } 160 161 static inline int journal_state_seq_count(struct journal *j, 162 union journal_res_state s, u64 seq) 163 { 164 if (journal_cur_seq(j) - seq < JOURNAL_STATE_BUF_NR) 165 return journal_state_count(s, seq & JOURNAL_STATE_BUF_MASK); 166 else 167 return 0; 168 } 169 170 static inline void journal_state_inc(union journal_res_state *s) 171 { 172 s->buf0_count += s->idx == 0; 173 s->buf1_count += s->idx == 1; 174 s->buf2_count += s->idx == 2; 175 s->buf3_count += s->idx == 3; 176 } 177 178 /* 179 * Amount of space that will be taken up by some keys in the journal (i.e. 180 * including the jset header) 181 */ 182 static inline unsigned jset_u64s(unsigned u64s) 183 { 184 return u64s + sizeof(struct jset_entry) / sizeof(u64); 185 } 186 187 static inline int journal_entry_overhead(struct journal *j) 188 { 189 return sizeof(struct jset) / sizeof(u64) + j->entry_u64s_reserved; 190 } 191 192 static inline struct jset_entry * 193 bch2_journal_add_entry_noreservation(struct journal_buf *buf, size_t u64s) 194 { 195 struct jset *jset = buf->data; 196 struct jset_entry *entry = vstruct_idx(jset, le32_to_cpu(jset->u64s)); 197 198 memset(entry, 0, sizeof(*entry)); 199 entry->u64s = cpu_to_le16(u64s); 200 201 le32_add_cpu(&jset->u64s, jset_u64s(u64s)); 202 203 return entry; 204 } 205 206 static inline struct jset_entry * 207 journal_res_entry(struct journal *j, struct journal_res *res) 208 { 209 return vstruct_idx(j->buf[res->seq & JOURNAL_BUF_MASK].data, res->offset); 210 } 211 212 static inline unsigned journal_entry_init(struct jset_entry *entry, unsigned type, 213 enum btree_id id, unsigned level, 214 unsigned u64s) 215 { 216 entry->u64s = cpu_to_le16(u64s); 217 entry->btree_id = id; 218 entry->level = level; 219 entry->type = type; 220 entry->pad[0] = 0; 221 entry->pad[1] = 0; 222 entry->pad[2] = 0; 223 return jset_u64s(u64s); 224 } 225 226 static inline unsigned journal_entry_set(struct jset_entry *entry, unsigned type, 227 enum btree_id id, unsigned level, 228 const void *data, unsigned u64s) 229 { 230 unsigned ret = journal_entry_init(entry, type, id, level, u64s); 231 232 memcpy_u64s_small(entry->_data, data, u64s); 233 return ret; 234 } 235 236 static inline struct jset_entry * 237 bch2_journal_add_entry(struct journal *j, struct journal_res *res, 238 unsigned type, enum btree_id id, 239 unsigned level, unsigned u64s) 240 { 241 struct jset_entry *entry = journal_res_entry(j, res); 242 unsigned actual = journal_entry_init(entry, type, id, level, u64s); 243 244 EBUG_ON(!res->ref); 245 EBUG_ON(actual > res->u64s); 246 247 res->offset += actual; 248 res->u64s -= actual; 249 return entry; 250 } 251 252 static inline bool journal_entry_empty(struct jset *j) 253 { 254 if (j->seq != j->last_seq) 255 return false; 256 257 vstruct_for_each(j, i) 258 if (i->type == BCH_JSET_ENTRY_btree_keys && i->u64s) 259 return false; 260 return true; 261 } 262 263 /* 264 * Drop reference on a buffer index and return true if the count has hit zero. 265 */ 266 static inline union journal_res_state journal_state_buf_put(struct journal *j, unsigned idx) 267 { 268 union journal_res_state s; 269 270 s.v = atomic64_sub_return(((union journal_res_state) { 271 .buf0_count = idx == 0, 272 .buf1_count = idx == 1, 273 .buf2_count = idx == 2, 274 .buf3_count = idx == 3, 275 }).v, &j->reservations.counter); 276 return s; 277 } 278 279 bool bch2_journal_entry_close(struct journal *); 280 void bch2_journal_do_writes(struct journal *); 281 void bch2_journal_buf_put_final(struct journal *, u64); 282 283 static inline void __bch2_journal_buf_put(struct journal *j, u64 seq) 284 { 285 unsigned idx = seq & JOURNAL_STATE_BUF_MASK; 286 union journal_res_state s; 287 288 s = journal_state_buf_put(j, idx); 289 if (!journal_state_count(s, idx)) 290 bch2_journal_buf_put_final(j, seq); 291 } 292 293 static inline void bch2_journal_buf_put(struct journal *j, u64 seq) 294 { 295 unsigned idx = seq & JOURNAL_STATE_BUF_MASK; 296 union journal_res_state s; 297 298 s = journal_state_buf_put(j, idx); 299 if (!journal_state_count(s, idx)) { 300 spin_lock(&j->lock); 301 bch2_journal_buf_put_final(j, seq); 302 spin_unlock(&j->lock); 303 } else if (unlikely(s.cur_entry_offset == JOURNAL_ENTRY_BLOCKED_VAL)) 304 wake_up(&j->wait); 305 } 306 307 /* 308 * This function releases the journal write structure so other threads can 309 * then proceed to add their keys as well. 310 */ 311 static inline void bch2_journal_res_put(struct journal *j, 312 struct journal_res *res) 313 { 314 if (!res->ref) 315 return; 316 317 lock_release(&j->res_map, _THIS_IP_); 318 319 while (res->u64s) 320 bch2_journal_add_entry(j, res, 321 BCH_JSET_ENTRY_btree_keys, 322 0, 0, 0); 323 324 bch2_journal_buf_put(j, res->seq); 325 326 res->ref = 0; 327 } 328 329 int bch2_journal_res_get_slowpath(struct journal *, struct journal_res *, 330 unsigned, struct btree_trans *); 331 332 /* First bits for BCH_WATERMARK: */ 333 enum journal_res_flags { 334 __JOURNAL_RES_GET_NONBLOCK = BCH_WATERMARK_BITS, 335 __JOURNAL_RES_GET_CHECK, 336 }; 337 338 #define JOURNAL_RES_GET_NONBLOCK (1 << __JOURNAL_RES_GET_NONBLOCK) 339 #define JOURNAL_RES_GET_CHECK (1 << __JOURNAL_RES_GET_CHECK) 340 341 static inline int journal_res_get_fast(struct journal *j, 342 struct journal_res *res, 343 unsigned flags) 344 { 345 union journal_res_state old, new; 346 347 old.v = atomic64_read(&j->reservations.counter); 348 do { 349 new.v = old.v; 350 351 /* 352 * Check if there is still room in the current journal 353 * entry, smp_rmb() guarantees that reads from reservations.counter 354 * occur before accessing cur_entry_u64s: 355 */ 356 smp_rmb(); 357 if (new.cur_entry_offset + res->u64s > j->cur_entry_u64s) 358 return 0; 359 360 EBUG_ON(!journal_state_count(new, new.idx)); 361 362 if ((flags & BCH_WATERMARK_MASK) < j->watermark) 363 return 0; 364 365 new.cur_entry_offset += res->u64s; 366 journal_state_inc(&new); 367 368 /* 369 * If the refcount would overflow, we have to wait: 370 * XXX - tracepoint this: 371 */ 372 if (!journal_state_count(new, new.idx)) 373 return 0; 374 375 if (flags & JOURNAL_RES_GET_CHECK) 376 return 1; 377 } while (!atomic64_try_cmpxchg(&j->reservations.counter, 378 &old.v, new.v)); 379 380 res->ref = true; 381 res->offset = old.cur_entry_offset; 382 res->seq = journal_cur_seq(j); 383 res->seq -= (res->seq - old.idx) & JOURNAL_STATE_BUF_MASK; 384 return 1; 385 } 386 387 static inline int bch2_journal_res_get(struct journal *j, struct journal_res *res, 388 unsigned u64s, unsigned flags, 389 struct btree_trans *trans) 390 { 391 int ret; 392 393 EBUG_ON(res->ref); 394 EBUG_ON(!test_bit(JOURNAL_running, &j->flags)); 395 396 res->u64s = u64s; 397 398 if (journal_res_get_fast(j, res, flags)) 399 goto out; 400 401 ret = bch2_journal_res_get_slowpath(j, res, flags, trans); 402 if (ret) 403 return ret; 404 out: 405 if (!(flags & JOURNAL_RES_GET_CHECK)) { 406 lock_acquire_shared(&j->res_map, 0, 407 (flags & JOURNAL_RES_GET_NONBLOCK) != 0, 408 NULL, _THIS_IP_); 409 EBUG_ON(!res->ref); 410 BUG_ON(!res->seq); 411 } 412 return 0; 413 } 414 415 /* journal_entry_res: */ 416 417 void bch2_journal_entry_res_resize(struct journal *, 418 struct journal_entry_res *, 419 unsigned); 420 421 int bch2_journal_flush_seq_async(struct journal *, u64, struct closure *); 422 void bch2_journal_flush_async(struct journal *, struct closure *); 423 424 int bch2_journal_flush_seq(struct journal *, u64, unsigned); 425 int bch2_journal_flush(struct journal *); 426 bool bch2_journal_noflush_seq(struct journal *, u64, u64); 427 int bch2_journal_meta(struct journal *); 428 429 void bch2_journal_halt_locked(struct journal *); 430 void bch2_journal_halt(struct journal *); 431 432 static inline int bch2_journal_error(struct journal *j) 433 { 434 return j->reservations.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL 435 ? -BCH_ERR_journal_shutdown : 0; 436 } 437 438 struct bch_dev; 439 440 void bch2_journal_unblock(struct journal *); 441 void bch2_journal_block(struct journal *); 442 struct journal_buf *bch2_next_write_buffer_flush_journal_buf(struct journal *, u64, bool *); 443 444 void __bch2_journal_debug_to_text(struct printbuf *, struct journal *); 445 void bch2_journal_debug_to_text(struct printbuf *, struct journal *); 446 447 int bch2_set_nr_journal_buckets(struct bch_fs *, struct bch_dev *, unsigned); 448 int bch2_dev_journal_bucket_delete(struct bch_dev *, u64); 449 450 int bch2_dev_journal_alloc(struct bch_dev *, bool); 451 int bch2_fs_journal_alloc(struct bch_fs *); 452 453 void bch2_dev_journal_stop(struct journal *, struct bch_dev *); 454 455 void bch2_fs_journal_stop(struct journal *); 456 int bch2_fs_journal_start(struct journal *, u64, u64); 457 void bch2_journal_set_replay_done(struct journal *); 458 459 void bch2_dev_journal_exit(struct bch_dev *); 460 int bch2_dev_journal_init(struct bch_dev *, struct bch_sb *); 461 void bch2_fs_journal_exit(struct journal *); 462 void bch2_fs_journal_init_early(struct journal *); 463 int bch2_fs_journal_init(struct journal *); 464 465 #endif /* _BCACHEFS_JOURNAL_H */ 466