]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/md/raid5-cache.c
7b9149ef685ce1b09e73dded0db23d07d4c3d7b9
[mirror_ubuntu-bionic-kernel.git] / drivers / md / raid5-cache.c
1 /*
2 * Copyright (C) 2015 Shaohua Li <shli@fb.com>
3 * Copyright (C) 2016 Song Liu <songliubraving@fb.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15 #include <linux/kernel.h>
16 #include <linux/wait.h>
17 #include <linux/blkdev.h>
18 #include <linux/slab.h>
19 #include <linux/raid/md_p.h>
20 #include <linux/crc32c.h>
21 #include <linux/random.h>
22 #include <linux/kthread.h>
23 #include "md.h"
24 #include "raid5.h"
25 #include "bitmap.h"
26
27 /*
28 * metadata/data stored in disk with 4k size unit (a block) regardless
29 * underneath hardware sector size. only works with PAGE_SIZE == 4096
30 */
31 #define BLOCK_SECTORS (8)
32
33 /*
34 * log->max_free_space is min(1/4 disk size, 10G reclaimable space).
35 *
36 * In write through mode, the reclaim runs every log->max_free_space.
37 * This can prevent the recovery scans for too long
38 */
39 #define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
40 #define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
41
42 /* wake up reclaim thread periodically */
43 #define R5C_RECLAIM_WAKEUP_INTERVAL (30 * HZ)
44 /* start flush with these full stripes */
45 #define R5C_FULL_STRIPE_FLUSH_BATCH 256
46 /* reclaim stripes in groups */
47 #define R5C_RECLAIM_STRIPE_GROUP (NR_STRIPE_HASH_LOCKS * 2)
48
49 /*
50 * We only need 2 bios per I/O unit to make progress, but ensure we
51 * have a few more available to not get too tight.
52 */
53 #define R5L_POOL_SIZE 4
54
55 /*
56 * r5c journal modes of the array: write-back or write-through.
57 * write-through mode has identical behavior as existing log only
58 * implementation.
59 */
60 enum r5c_journal_mode {
61 R5C_JOURNAL_MODE_WRITE_THROUGH = 0,
62 R5C_JOURNAL_MODE_WRITE_BACK = 1,
63 };
64
65 static char *r5c_journal_mode_str[] = {"write-through",
66 "write-back"};
67 /*
68 * raid5 cache state machine
69 *
70 * With the RAID cache, each stripe works in two phases:
71 * - caching phase
72 * - writing-out phase
73 *
74 * These two phases are controlled by bit STRIPE_R5C_CACHING:
75 * if STRIPE_R5C_CACHING == 0, the stripe is in writing-out phase
76 * if STRIPE_R5C_CACHING == 1, the stripe is in caching phase
77 *
78 * When there is no journal, or the journal is in write-through mode,
79 * the stripe is always in writing-out phase.
80 *
81 * For write-back journal, the stripe is sent to caching phase on write
82 * (r5c_try_caching_write). r5c_make_stripe_write_out() kicks off
83 * the write-out phase by clearing STRIPE_R5C_CACHING.
84 *
85 * Stripes in caching phase do not write the raid disks. Instead, all
86 * writes are committed from the log device. Therefore, a stripe in
87 * caching phase handles writes as:
88 * - write to log device
89 * - return IO
90 *
91 * Stripes in writing-out phase handle writes as:
92 * - calculate parity
93 * - write pending data and parity to journal
94 * - write data and parity to raid disks
95 * - return IO for pending writes
96 */
97
98 struct r5l_log {
99 struct md_rdev *rdev;
100
101 u32 uuid_checksum;
102
103 sector_t device_size; /* log device size, round to
104 * BLOCK_SECTORS */
105 sector_t max_free_space; /* reclaim run if free space is at
106 * this size */
107
108 sector_t last_checkpoint; /* log tail. where recovery scan
109 * starts from */
110 u64 last_cp_seq; /* log tail sequence */
111
112 sector_t log_start; /* log head. where new data appends */
113 u64 seq; /* log head sequence */
114
115 sector_t next_checkpoint;
116 u64 next_cp_seq;
117
118 struct mutex io_mutex;
119 struct r5l_io_unit *current_io; /* current io_unit accepting new data */
120
121 spinlock_t io_list_lock;
122 struct list_head running_ios; /* io_units which are still running,
123 * and have not yet been completely
124 * written to the log */
125 struct list_head io_end_ios; /* io_units which have been completely
126 * written to the log but not yet written
127 * to the RAID */
128 struct list_head flushing_ios; /* io_units which are waiting for log
129 * cache flush */
130 struct list_head finished_ios; /* io_units which settle down in log disk */
131 struct bio flush_bio;
132
133 struct list_head no_mem_stripes; /* pending stripes, -ENOMEM */
134
135 struct kmem_cache *io_kc;
136 mempool_t *io_pool;
137 struct bio_set *bs;
138 mempool_t *meta_pool;
139
140 struct md_thread *reclaim_thread;
141 unsigned long reclaim_target; /* number of space that need to be
142 * reclaimed. if it's 0, reclaim spaces
143 * used by io_units which are in
144 * IO_UNIT_STRIPE_END state (eg, reclaim
145 * dones't wait for specific io_unit
146 * switching to IO_UNIT_STRIPE_END
147 * state) */
148 wait_queue_head_t iounit_wait;
149
150 struct list_head no_space_stripes; /* pending stripes, log has no space */
151 spinlock_t no_space_stripes_lock;
152
153 bool need_cache_flush;
154
155 /* for r5c_cache */
156 enum r5c_journal_mode r5c_journal_mode;
157
158 /* all stripes in r5cache, in the order of seq at sh->log_start */
159 struct list_head stripe_in_journal_list;
160
161 spinlock_t stripe_in_journal_lock;
162 atomic_t stripe_in_journal_count;
163
164 /* to submit async io_units, to fulfill ordering of flush */
165 struct work_struct deferred_io_work;
166 };
167
168 /*
169 * an IO range starts from a meta data block and end at the next meta data
170 * block. The io unit's the meta data block tracks data/parity followed it. io
171 * unit is written to log disk with normal write, as we always flush log disk
172 * first and then start move data to raid disks, there is no requirement to
173 * write io unit with FLUSH/FUA
174 */
175 struct r5l_io_unit {
176 struct r5l_log *log;
177
178 struct page *meta_page; /* store meta block */
179 int meta_offset; /* current offset in meta_page */
180
181 struct bio *current_bio;/* current_bio accepting new data */
182
183 atomic_t pending_stripe;/* how many stripes not flushed to raid */
184 u64 seq; /* seq number of the metablock */
185 sector_t log_start; /* where the io_unit starts */
186 sector_t log_end; /* where the io_unit ends */
187 struct list_head log_sibling; /* log->running_ios */
188 struct list_head stripe_list; /* stripes added to the io_unit */
189
190 int state;
191 bool need_split_bio;
192 struct bio *split_bio;
193
194 unsigned int has_flush:1; /* include flush request */
195 unsigned int has_fua:1; /* include fua request */
196 unsigned int has_null_flush:1; /* include empty flush request */
197 /*
198 * io isn't sent yet, flush/fua request can only be submitted till it's
199 * the first IO in running_ios list
200 */
201 unsigned int io_deferred:1;
202
203 struct bio_list flush_barriers; /* size == 0 flush bios */
204 };
205
206 /* r5l_io_unit state */
207 enum r5l_io_unit_state {
208 IO_UNIT_RUNNING = 0, /* accepting new IO */
209 IO_UNIT_IO_START = 1, /* io_unit bio start writing to log,
210 * don't accepting new bio */
211 IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */
212 IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
213 };
214
215 bool r5c_is_writeback(struct r5l_log *log)
216 {
217 return (log != NULL &&
218 log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK);
219 }
220
221 static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
222 {
223 start += inc;
224 if (start >= log->device_size)
225 start = start - log->device_size;
226 return start;
227 }
228
229 static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
230 sector_t end)
231 {
232 if (end >= start)
233 return end - start;
234 else
235 return end + log->device_size - start;
236 }
237
238 static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
239 {
240 sector_t used_size;
241
242 used_size = r5l_ring_distance(log, log->last_checkpoint,
243 log->log_start);
244
245 return log->device_size > used_size + size;
246 }
247
248 static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
249 enum r5l_io_unit_state state)
250 {
251 if (WARN_ON(io->state >= state))
252 return;
253 io->state = state;
254 }
255
256 static void
257 r5c_return_dev_pending_writes(struct r5conf *conf, struct r5dev *dev,
258 struct bio_list *return_bi)
259 {
260 struct bio *wbi, *wbi2;
261
262 wbi = dev->written;
263 dev->written = NULL;
264 while (wbi && wbi->bi_iter.bi_sector <
265 dev->sector + STRIPE_SECTORS) {
266 wbi2 = r5_next_bio(wbi, dev->sector);
267 if (!raid5_dec_bi_active_stripes(wbi)) {
268 md_write_end(conf->mddev);
269 bio_list_add(return_bi, wbi);
270 }
271 wbi = wbi2;
272 }
273 }
274
275 void r5c_handle_cached_data_endio(struct r5conf *conf,
276 struct stripe_head *sh, int disks, struct bio_list *return_bi)
277 {
278 int i;
279
280 for (i = sh->disks; i--; ) {
281 if (sh->dev[i].written) {
282 set_bit(R5_UPTODATE, &sh->dev[i].flags);
283 r5c_return_dev_pending_writes(conf, &sh->dev[i],
284 return_bi);
285 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
286 STRIPE_SECTORS,
287 !test_bit(STRIPE_DEGRADED, &sh->state),
288 0);
289 }
290 }
291 }
292
293 /* Check whether we should flush some stripes to free up stripe cache */
294 void r5c_check_stripe_cache_usage(struct r5conf *conf)
295 {
296 int total_cached;
297
298 if (!r5c_is_writeback(conf->log))
299 return;
300
301 total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
302 atomic_read(&conf->r5c_cached_full_stripes);
303
304 /*
305 * The following condition is true for either of the following:
306 * - stripe cache pressure high:
307 * total_cached > 3/4 min_nr_stripes ||
308 * empty_inactive_list_nr > 0
309 * - stripe cache pressure moderate:
310 * total_cached > 1/2 min_nr_stripes
311 */
312 if (total_cached > conf->min_nr_stripes * 1 / 2 ||
313 atomic_read(&conf->empty_inactive_list_nr) > 0)
314 r5l_wake_reclaim(conf->log, 0);
315 }
316
317 /*
318 * flush cache when there are R5C_FULL_STRIPE_FLUSH_BATCH or more full
319 * stripes in the cache
320 */
321 void r5c_check_cached_full_stripe(struct r5conf *conf)
322 {
323 if (!r5c_is_writeback(conf->log))
324 return;
325
326 /*
327 * wake up reclaim for R5C_FULL_STRIPE_FLUSH_BATCH cached stripes
328 * or a full stripe (chunk size / 4k stripes).
329 */
330 if (atomic_read(&conf->r5c_cached_full_stripes) >=
331 min(R5C_FULL_STRIPE_FLUSH_BATCH,
332 conf->chunk_sectors >> STRIPE_SHIFT))
333 r5l_wake_reclaim(conf->log, 0);
334 }
335
336 /*
337 * Total log space (in sectors) needed to flush all data in cache
338 *
339 * Currently, writing-out phase automatically includes all pending writes
340 * to the same sector. So the reclaim of each stripe takes up to
341 * (conf->raid_disks + 1) pages of log space.
342 *
343 * To totally avoid deadlock due to log space, the code reserves
344 * (conf->raid_disks + 1) pages for each stripe in cache, which is not
345 * necessary in most cases.
346 *
347 * To improve this, we will need writing-out phase to be able to NOT include
348 * pending writes, which will reduce the requirement to
349 * (conf->max_degraded + 1) pages per stripe in cache.
350 */
351 static sector_t r5c_log_required_to_flush_cache(struct r5conf *conf)
352 {
353 struct r5l_log *log = conf->log;
354
355 if (!r5c_is_writeback(log))
356 return 0;
357
358 return BLOCK_SECTORS * (conf->raid_disks + 1) *
359 atomic_read(&log->stripe_in_journal_count);
360 }
361
362 /*
363 * evaluate log space usage and update R5C_LOG_TIGHT and R5C_LOG_CRITICAL
364 *
365 * R5C_LOG_TIGHT is set when free space on the log device is less than 3x of
366 * reclaim_required_space. R5C_LOG_CRITICAL is set when free space on the log
367 * device is less than 2x of reclaim_required_space.
368 */
369 static inline void r5c_update_log_state(struct r5l_log *log)
370 {
371 struct r5conf *conf = log->rdev->mddev->private;
372 sector_t free_space;
373 sector_t reclaim_space;
374
375 if (!r5c_is_writeback(log))
376 return;
377
378 free_space = r5l_ring_distance(log, log->log_start,
379 log->last_checkpoint);
380 reclaim_space = r5c_log_required_to_flush_cache(conf);
381 if (free_space < 2 * reclaim_space)
382 set_bit(R5C_LOG_CRITICAL, &conf->cache_state);
383 else
384 clear_bit(R5C_LOG_CRITICAL, &conf->cache_state);
385 if (free_space < 3 * reclaim_space)
386 set_bit(R5C_LOG_TIGHT, &conf->cache_state);
387 else
388 clear_bit(R5C_LOG_TIGHT, &conf->cache_state);
389 }
390
391 /*
392 * Put the stripe into writing-out phase by clearing STRIPE_R5C_CACHING.
393 * This function should only be called in write-back mode.
394 */
395 void r5c_make_stripe_write_out(struct stripe_head *sh)
396 {
397 struct r5conf *conf = sh->raid_conf;
398 struct r5l_log *log = conf->log;
399
400 BUG_ON(!r5c_is_writeback(log));
401
402 WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
403 clear_bit(STRIPE_R5C_CACHING, &sh->state);
404
405 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
406 atomic_inc(&conf->preread_active_stripes);
407
408 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) {
409 BUG_ON(atomic_read(&conf->r5c_cached_partial_stripes) == 0);
410 atomic_dec(&conf->r5c_cached_partial_stripes);
411 }
412
413 if (test_and_clear_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
414 BUG_ON(atomic_read(&conf->r5c_cached_full_stripes) == 0);
415 atomic_dec(&conf->r5c_cached_full_stripes);
416 }
417 }
418
419 static void r5c_handle_data_cached(struct stripe_head *sh)
420 {
421 int i;
422
423 for (i = sh->disks; i--; )
424 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
425 set_bit(R5_InJournal, &sh->dev[i].flags);
426 clear_bit(R5_LOCKED, &sh->dev[i].flags);
427 }
428 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
429 }
430
431 /*
432 * this journal write must contain full parity,
433 * it may also contain some data pages
434 */
435 static void r5c_handle_parity_cached(struct stripe_head *sh)
436 {
437 int i;
438
439 for (i = sh->disks; i--; )
440 if (test_bit(R5_InJournal, &sh->dev[i].flags))
441 set_bit(R5_Wantwrite, &sh->dev[i].flags);
442 }
443
444 /*
445 * Setting proper flags after writing (or flushing) data and/or parity to the
446 * log device. This is called from r5l_log_endio() or r5l_log_flush_endio().
447 */
448 static void r5c_finish_cache_stripe(struct stripe_head *sh)
449 {
450 struct r5l_log *log = sh->raid_conf->log;
451
452 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
453 BUG_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
454 /*
455 * Set R5_InJournal for parity dev[pd_idx]. This means
456 * all data AND parity in the journal. For RAID 6, it is
457 * NOT necessary to set the flag for dev[qd_idx], as the
458 * two parities are written out together.
459 */
460 set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
461 } else if (test_bit(STRIPE_R5C_CACHING, &sh->state)) {
462 r5c_handle_data_cached(sh);
463 } else {
464 r5c_handle_parity_cached(sh);
465 set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
466 }
467 }
468
469 static void r5l_io_run_stripes(struct r5l_io_unit *io)
470 {
471 struct stripe_head *sh, *next;
472
473 list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
474 list_del_init(&sh->log_list);
475
476 r5c_finish_cache_stripe(sh);
477
478 set_bit(STRIPE_HANDLE, &sh->state);
479 raid5_release_stripe(sh);
480 }
481 }
482
483 static void r5l_log_run_stripes(struct r5l_log *log)
484 {
485 struct r5l_io_unit *io, *next;
486
487 assert_spin_locked(&log->io_list_lock);
488
489 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
490 /* don't change list order */
491 if (io->state < IO_UNIT_IO_END)
492 break;
493
494 list_move_tail(&io->log_sibling, &log->finished_ios);
495 r5l_io_run_stripes(io);
496 }
497 }
498
499 static void r5l_move_to_end_ios(struct r5l_log *log)
500 {
501 struct r5l_io_unit *io, *next;
502
503 assert_spin_locked(&log->io_list_lock);
504
505 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
506 /* don't change list order */
507 if (io->state < IO_UNIT_IO_END)
508 break;
509 list_move_tail(&io->log_sibling, &log->io_end_ios);
510 }
511 }
512
513 static void __r5l_stripe_write_finished(struct r5l_io_unit *io);
514 static void r5l_log_endio(struct bio *bio)
515 {
516 struct r5l_io_unit *io = bio->bi_private;
517 struct r5l_io_unit *io_deferred;
518 struct r5l_log *log = io->log;
519 unsigned long flags;
520
521 if (bio->bi_error)
522 md_error(log->rdev->mddev, log->rdev);
523
524 bio_put(bio);
525 mempool_free(io->meta_page, log->meta_pool);
526
527 spin_lock_irqsave(&log->io_list_lock, flags);
528 __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
529 if (log->need_cache_flush)
530 r5l_move_to_end_ios(log);
531 else
532 r5l_log_run_stripes(log);
533 if (!list_empty(&log->running_ios)) {
534 /*
535 * FLUSH/FUA io_unit is deferred because of ordering, now we
536 * can dispatch it
537 */
538 io_deferred = list_first_entry(&log->running_ios,
539 struct r5l_io_unit, log_sibling);
540 if (io_deferred->io_deferred)
541 schedule_work(&log->deferred_io_work);
542 }
543
544 spin_unlock_irqrestore(&log->io_list_lock, flags);
545
546 if (log->need_cache_flush)
547 md_wakeup_thread(log->rdev->mddev->thread);
548
549 if (io->has_null_flush) {
550 struct bio *bi;
551
552 WARN_ON(bio_list_empty(&io->flush_barriers));
553 while ((bi = bio_list_pop(&io->flush_barriers)) != NULL) {
554 bio_endio(bi);
555 atomic_dec(&io->pending_stripe);
556 }
557 if (atomic_read(&io->pending_stripe) == 0)
558 __r5l_stripe_write_finished(io);
559 }
560 }
561
562 static void r5l_do_submit_io(struct r5l_log *log, struct r5l_io_unit *io)
563 {
564 unsigned long flags;
565
566 spin_lock_irqsave(&log->io_list_lock, flags);
567 __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
568 spin_unlock_irqrestore(&log->io_list_lock, flags);
569
570 if (io->has_flush)
571 bio_set_op_attrs(io->current_bio, REQ_OP_WRITE, WRITE_FLUSH);
572 if (io->has_fua)
573 bio_set_op_attrs(io->current_bio, REQ_OP_WRITE, WRITE_FUA);
574 submit_bio(io->current_bio);
575
576 if (!io->split_bio)
577 return;
578
579 if (io->has_flush)
580 bio_set_op_attrs(io->split_bio, REQ_OP_WRITE, WRITE_FLUSH);
581 if (io->has_fua)
582 bio_set_op_attrs(io->split_bio, REQ_OP_WRITE, WRITE_FUA);
583 submit_bio(io->split_bio);
584 }
585
586 /* deferred io_unit will be dispatched here */
587 static void r5l_submit_io_async(struct work_struct *work)
588 {
589 struct r5l_log *log = container_of(work, struct r5l_log,
590 deferred_io_work);
591 struct r5l_io_unit *io = NULL;
592 unsigned long flags;
593
594 spin_lock_irqsave(&log->io_list_lock, flags);
595 if (!list_empty(&log->running_ios)) {
596 io = list_first_entry(&log->running_ios, struct r5l_io_unit,
597 log_sibling);
598 if (!io->io_deferred)
599 io = NULL;
600 else
601 io->io_deferred = 0;
602 }
603 spin_unlock_irqrestore(&log->io_list_lock, flags);
604 if (io)
605 r5l_do_submit_io(log, io);
606 }
607
608 static void r5l_submit_current_io(struct r5l_log *log)
609 {
610 struct r5l_io_unit *io = log->current_io;
611 struct bio *bio;
612 struct r5l_meta_block *block;
613 unsigned long flags;
614 u32 crc;
615 bool do_submit = true;
616
617 if (!io)
618 return;
619
620 block = page_address(io->meta_page);
621 block->meta_size = cpu_to_le32(io->meta_offset);
622 crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
623 block->checksum = cpu_to_le32(crc);
624 bio = io->current_bio;
625
626 log->current_io = NULL;
627 spin_lock_irqsave(&log->io_list_lock, flags);
628 if (io->has_flush || io->has_fua) {
629 if (io != list_first_entry(&log->running_ios,
630 struct r5l_io_unit, log_sibling)) {
631 io->io_deferred = 1;
632 do_submit = false;
633 }
634 }
635 spin_unlock_irqrestore(&log->io_list_lock, flags);
636 if (do_submit)
637 r5l_do_submit_io(log, io);
638 }
639
640 static struct bio *r5l_bio_alloc(struct r5l_log *log)
641 {
642 struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
643
644 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
645 bio->bi_bdev = log->rdev->bdev;
646 bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
647
648 return bio;
649 }
650
651 static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
652 {
653 log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
654
655 r5c_update_log_state(log);
656 /*
657 * If we filled up the log device start from the beginning again,
658 * which will require a new bio.
659 *
660 * Note: for this to work properly the log size needs to me a multiple
661 * of BLOCK_SECTORS.
662 */
663 if (log->log_start == 0)
664 io->need_split_bio = true;
665
666 io->log_end = log->log_start;
667 }
668
669 static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
670 {
671 struct r5l_io_unit *io;
672 struct r5l_meta_block *block;
673
674 io = mempool_alloc(log->io_pool, GFP_ATOMIC);
675 if (!io)
676 return NULL;
677 memset(io, 0, sizeof(*io));
678
679 io->log = log;
680 INIT_LIST_HEAD(&io->log_sibling);
681 INIT_LIST_HEAD(&io->stripe_list);
682 bio_list_init(&io->flush_barriers);
683 io->state = IO_UNIT_RUNNING;
684
685 io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
686 block = page_address(io->meta_page);
687 clear_page(block);
688 block->magic = cpu_to_le32(R5LOG_MAGIC);
689 block->version = R5LOG_VERSION;
690 block->seq = cpu_to_le64(log->seq);
691 block->position = cpu_to_le64(log->log_start);
692
693 io->log_start = log->log_start;
694 io->meta_offset = sizeof(struct r5l_meta_block);
695 io->seq = log->seq++;
696
697 io->current_bio = r5l_bio_alloc(log);
698 io->current_bio->bi_end_io = r5l_log_endio;
699 io->current_bio->bi_private = io;
700 bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
701
702 r5_reserve_log_entry(log, io);
703
704 spin_lock_irq(&log->io_list_lock);
705 list_add_tail(&io->log_sibling, &log->running_ios);
706 spin_unlock_irq(&log->io_list_lock);
707
708 return io;
709 }
710
711 static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
712 {
713 if (log->current_io &&
714 log->current_io->meta_offset + payload_size > PAGE_SIZE)
715 r5l_submit_current_io(log);
716
717 if (!log->current_io) {
718 log->current_io = r5l_new_meta(log);
719 if (!log->current_io)
720 return -ENOMEM;
721 }
722
723 return 0;
724 }
725
726 static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
727 sector_t location,
728 u32 checksum1, u32 checksum2,
729 bool checksum2_valid)
730 {
731 struct r5l_io_unit *io = log->current_io;
732 struct r5l_payload_data_parity *payload;
733
734 payload = page_address(io->meta_page) + io->meta_offset;
735 payload->header.type = cpu_to_le16(type);
736 payload->header.flags = cpu_to_le16(0);
737 payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
738 (PAGE_SHIFT - 9));
739 payload->location = cpu_to_le64(location);
740 payload->checksum[0] = cpu_to_le32(checksum1);
741 if (checksum2_valid)
742 payload->checksum[1] = cpu_to_le32(checksum2);
743
744 io->meta_offset += sizeof(struct r5l_payload_data_parity) +
745 sizeof(__le32) * (1 + !!checksum2_valid);
746 }
747
748 static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
749 {
750 struct r5l_io_unit *io = log->current_io;
751
752 if (io->need_split_bio) {
753 BUG_ON(io->split_bio);
754 io->split_bio = io->current_bio;
755 io->current_bio = r5l_bio_alloc(log);
756 bio_chain(io->current_bio, io->split_bio);
757 io->need_split_bio = false;
758 }
759
760 if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
761 BUG();
762
763 r5_reserve_log_entry(log, io);
764 }
765
766 static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
767 int data_pages, int parity_pages)
768 {
769 int i;
770 int meta_size;
771 int ret;
772 struct r5l_io_unit *io;
773
774 meta_size =
775 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
776 * data_pages) +
777 sizeof(struct r5l_payload_data_parity) +
778 sizeof(__le32) * parity_pages;
779
780 ret = r5l_get_meta(log, meta_size);
781 if (ret)
782 return ret;
783
784 io = log->current_io;
785
786 if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state))
787 io->has_flush = 1;
788
789 for (i = 0; i < sh->disks; i++) {
790 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
791 test_bit(R5_InJournal, &sh->dev[i].flags))
792 continue;
793 if (i == sh->pd_idx || i == sh->qd_idx)
794 continue;
795 if (test_bit(R5_WantFUA, &sh->dev[i].flags) &&
796 log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) {
797 io->has_fua = 1;
798 /*
799 * we need to flush journal to make sure recovery can
800 * reach the data with fua flag
801 */
802 io->has_flush = 1;
803 }
804 r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
805 raid5_compute_blocknr(sh, i, 0),
806 sh->dev[i].log_checksum, 0, false);
807 r5l_append_payload_page(log, sh->dev[i].page);
808 }
809
810 if (parity_pages == 2) {
811 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
812 sh->sector, sh->dev[sh->pd_idx].log_checksum,
813 sh->dev[sh->qd_idx].log_checksum, true);
814 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
815 r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
816 } else if (parity_pages == 1) {
817 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
818 sh->sector, sh->dev[sh->pd_idx].log_checksum,
819 0, false);
820 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
821 } else /* Just writing data, not parity, in caching phase */
822 BUG_ON(parity_pages != 0);
823
824 list_add_tail(&sh->log_list, &io->stripe_list);
825 atomic_inc(&io->pending_stripe);
826 sh->log_io = io;
827
828 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
829 return 0;
830
831 if (sh->log_start == MaxSector) {
832 BUG_ON(!list_empty(&sh->r5c));
833 sh->log_start = io->log_start;
834 spin_lock_irq(&log->stripe_in_journal_lock);
835 list_add_tail(&sh->r5c,
836 &log->stripe_in_journal_list);
837 spin_unlock_irq(&log->stripe_in_journal_lock);
838 atomic_inc(&log->stripe_in_journal_count);
839 }
840 return 0;
841 }
842
843 /* add stripe to no_space_stripes, and then wake up reclaim */
844 static inline void r5l_add_no_space_stripe(struct r5l_log *log,
845 struct stripe_head *sh)
846 {
847 spin_lock(&log->no_space_stripes_lock);
848 list_add_tail(&sh->log_list, &log->no_space_stripes);
849 spin_unlock(&log->no_space_stripes_lock);
850 }
851
852 /*
853 * running in raid5d, where reclaim could wait for raid5d too (when it flushes
854 * data from log to raid disks), so we shouldn't wait for reclaim here
855 */
856 int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
857 {
858 struct r5conf *conf = sh->raid_conf;
859 int write_disks = 0;
860 int data_pages, parity_pages;
861 int reserve;
862 int i;
863 int ret = 0;
864 bool wake_reclaim = false;
865
866 if (!log)
867 return -EAGAIN;
868 /* Don't support stripe batch */
869 if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
870 test_bit(STRIPE_SYNCING, &sh->state)) {
871 /* the stripe is written to log, we start writing it to raid */
872 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
873 return -EAGAIN;
874 }
875
876 WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
877
878 for (i = 0; i < sh->disks; i++) {
879 void *addr;
880
881 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
882 test_bit(R5_InJournal, &sh->dev[i].flags))
883 continue;
884
885 write_disks++;
886 /* checksum is already calculated in last run */
887 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
888 continue;
889 addr = kmap_atomic(sh->dev[i].page);
890 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
891 addr, PAGE_SIZE);
892 kunmap_atomic(addr);
893 }
894 parity_pages = 1 + !!(sh->qd_idx >= 0);
895 data_pages = write_disks - parity_pages;
896
897 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
898 /*
899 * The stripe must enter state machine again to finish the write, so
900 * don't delay.
901 */
902 clear_bit(STRIPE_DELAYED, &sh->state);
903 atomic_inc(&sh->count);
904
905 mutex_lock(&log->io_mutex);
906 /* meta + data */
907 reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
908
909 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
910 if (!r5l_has_free_space(log, reserve)) {
911 r5l_add_no_space_stripe(log, sh);
912 wake_reclaim = true;
913 } else {
914 ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
915 if (ret) {
916 spin_lock_irq(&log->io_list_lock);
917 list_add_tail(&sh->log_list,
918 &log->no_mem_stripes);
919 spin_unlock_irq(&log->io_list_lock);
920 }
921 }
922 } else { /* R5C_JOURNAL_MODE_WRITE_BACK */
923 /*
924 * log space critical, do not process stripes that are
925 * not in cache yet (sh->log_start == MaxSector).
926 */
927 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
928 sh->log_start == MaxSector) {
929 r5l_add_no_space_stripe(log, sh);
930 wake_reclaim = true;
931 reserve = 0;
932 } else if (!r5l_has_free_space(log, reserve)) {
933 if (sh->log_start == log->last_checkpoint)
934 BUG();
935 else
936 r5l_add_no_space_stripe(log, sh);
937 } else {
938 ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
939 if (ret) {
940 spin_lock_irq(&log->io_list_lock);
941 list_add_tail(&sh->log_list,
942 &log->no_mem_stripes);
943 spin_unlock_irq(&log->io_list_lock);
944 }
945 }
946 }
947
948 mutex_unlock(&log->io_mutex);
949 if (wake_reclaim)
950 r5l_wake_reclaim(log, reserve);
951 return 0;
952 }
953
954 void r5l_write_stripe_run(struct r5l_log *log)
955 {
956 if (!log)
957 return;
958 mutex_lock(&log->io_mutex);
959 r5l_submit_current_io(log);
960 mutex_unlock(&log->io_mutex);
961 }
962
963 int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
964 {
965 if (!log)
966 return -ENODEV;
967
968 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
969 /*
970 * in write through (journal only)
971 * we flush log disk cache first, then write stripe data to
972 * raid disks. So if bio is finished, the log disk cache is
973 * flushed already. The recovery guarantees we can recovery
974 * the bio from log disk, so we don't need to flush again
975 */
976 if (bio->bi_iter.bi_size == 0) {
977 bio_endio(bio);
978 return 0;
979 }
980 bio->bi_opf &= ~REQ_PREFLUSH;
981 } else {
982 /* write back (with cache) */
983 if (bio->bi_iter.bi_size == 0) {
984 mutex_lock(&log->io_mutex);
985 r5l_get_meta(log, 0);
986 bio_list_add(&log->current_io->flush_barriers, bio);
987 log->current_io->has_flush = 1;
988 log->current_io->has_null_flush = 1;
989 atomic_inc(&log->current_io->pending_stripe);
990 r5l_submit_current_io(log);
991 mutex_unlock(&log->io_mutex);
992 return 0;
993 }
994 }
995 return -EAGAIN;
996 }
997
998 /* This will run after log space is reclaimed */
999 static void r5l_run_no_space_stripes(struct r5l_log *log)
1000 {
1001 struct stripe_head *sh;
1002
1003 spin_lock(&log->no_space_stripes_lock);
1004 while (!list_empty(&log->no_space_stripes)) {
1005 sh = list_first_entry(&log->no_space_stripes,
1006 struct stripe_head, log_list);
1007 list_del_init(&sh->log_list);
1008 set_bit(STRIPE_HANDLE, &sh->state);
1009 raid5_release_stripe(sh);
1010 }
1011 spin_unlock(&log->no_space_stripes_lock);
1012 }
1013
1014 /*
1015 * calculate new last_checkpoint
1016 * for write through mode, returns log->next_checkpoint
1017 * for write back, returns log_start of first sh in stripe_in_journal_list
1018 */
1019 static sector_t r5c_calculate_new_cp(struct r5conf *conf)
1020 {
1021 struct stripe_head *sh;
1022 struct r5l_log *log = conf->log;
1023 sector_t new_cp;
1024 unsigned long flags;
1025
1026 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
1027 return log->next_checkpoint;
1028
1029 spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
1030 if (list_empty(&conf->log->stripe_in_journal_list)) {
1031 /* all stripes flushed */
1032 spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
1033 return log->next_checkpoint;
1034 }
1035 sh = list_first_entry(&conf->log->stripe_in_journal_list,
1036 struct stripe_head, r5c);
1037 new_cp = sh->log_start;
1038 spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
1039 return new_cp;
1040 }
1041
1042 static sector_t r5l_reclaimable_space(struct r5l_log *log)
1043 {
1044 struct r5conf *conf = log->rdev->mddev->private;
1045
1046 return r5l_ring_distance(log, log->last_checkpoint,
1047 r5c_calculate_new_cp(conf));
1048 }
1049
1050 static void r5l_run_no_mem_stripe(struct r5l_log *log)
1051 {
1052 struct stripe_head *sh;
1053
1054 assert_spin_locked(&log->io_list_lock);
1055
1056 if (!list_empty(&log->no_mem_stripes)) {
1057 sh = list_first_entry(&log->no_mem_stripes,
1058 struct stripe_head, log_list);
1059 list_del_init(&sh->log_list);
1060 set_bit(STRIPE_HANDLE, &sh->state);
1061 raid5_release_stripe(sh);
1062 }
1063 }
1064
1065 static bool r5l_complete_finished_ios(struct r5l_log *log)
1066 {
1067 struct r5l_io_unit *io, *next;
1068 bool found = false;
1069
1070 assert_spin_locked(&log->io_list_lock);
1071
1072 list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
1073 /* don't change list order */
1074 if (io->state < IO_UNIT_STRIPE_END)
1075 break;
1076
1077 log->next_checkpoint = io->log_start;
1078 log->next_cp_seq = io->seq;
1079
1080 list_del(&io->log_sibling);
1081 mempool_free(io, log->io_pool);
1082 r5l_run_no_mem_stripe(log);
1083
1084 found = true;
1085 }
1086
1087 return found;
1088 }
1089
1090 static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
1091 {
1092 struct r5l_log *log = io->log;
1093 struct r5conf *conf = log->rdev->mddev->private;
1094 unsigned long flags;
1095
1096 spin_lock_irqsave(&log->io_list_lock, flags);
1097 __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
1098
1099 if (!r5l_complete_finished_ios(log)) {
1100 spin_unlock_irqrestore(&log->io_list_lock, flags);
1101 return;
1102 }
1103
1104 if (r5l_reclaimable_space(log) > log->max_free_space ||
1105 test_bit(R5C_LOG_TIGHT, &conf->cache_state))
1106 r5l_wake_reclaim(log, 0);
1107
1108 spin_unlock_irqrestore(&log->io_list_lock, flags);
1109 wake_up(&log->iounit_wait);
1110 }
1111
1112 void r5l_stripe_write_finished(struct stripe_head *sh)
1113 {
1114 struct r5l_io_unit *io;
1115
1116 io = sh->log_io;
1117 sh->log_io = NULL;
1118
1119 if (io && atomic_dec_and_test(&io->pending_stripe))
1120 __r5l_stripe_write_finished(io);
1121 }
1122
1123 static void r5l_log_flush_endio(struct bio *bio)
1124 {
1125 struct r5l_log *log = container_of(bio, struct r5l_log,
1126 flush_bio);
1127 unsigned long flags;
1128 struct r5l_io_unit *io;
1129
1130 if (bio->bi_error)
1131 md_error(log->rdev->mddev, log->rdev);
1132
1133 spin_lock_irqsave(&log->io_list_lock, flags);
1134 list_for_each_entry(io, &log->flushing_ios, log_sibling)
1135 r5l_io_run_stripes(io);
1136 list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
1137 spin_unlock_irqrestore(&log->io_list_lock, flags);
1138 }
1139
1140 /*
1141 * Starting dispatch IO to raid.
1142 * io_unit(meta) consists of a log. There is one situation we want to avoid. A
1143 * broken meta in the middle of a log causes recovery can't find meta at the
1144 * head of log. If operations require meta at the head persistent in log, we
1145 * must make sure meta before it persistent in log too. A case is:
1146 *
1147 * stripe data/parity is in log, we start write stripe to raid disks. stripe
1148 * data/parity must be persistent in log before we do the write to raid disks.
1149 *
1150 * The solution is we restrictly maintain io_unit list order. In this case, we
1151 * only write stripes of an io_unit to raid disks till the io_unit is the first
1152 * one whose data/parity is in log.
1153 */
1154 void r5l_flush_stripe_to_raid(struct r5l_log *log)
1155 {
1156 bool do_flush;
1157
1158 if (!log || !log->need_cache_flush)
1159 return;
1160
1161 spin_lock_irq(&log->io_list_lock);
1162 /* flush bio is running */
1163 if (!list_empty(&log->flushing_ios)) {
1164 spin_unlock_irq(&log->io_list_lock);
1165 return;
1166 }
1167 list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
1168 do_flush = !list_empty(&log->flushing_ios);
1169 spin_unlock_irq(&log->io_list_lock);
1170
1171 if (!do_flush)
1172 return;
1173 bio_reset(&log->flush_bio);
1174 log->flush_bio.bi_bdev = log->rdev->bdev;
1175 log->flush_bio.bi_end_io = r5l_log_flush_endio;
1176 bio_set_op_attrs(&log->flush_bio, REQ_OP_WRITE, WRITE_FLUSH);
1177 submit_bio(&log->flush_bio);
1178 }
1179
1180 static void r5l_write_super(struct r5l_log *log, sector_t cp);
1181 static void r5l_write_super_and_discard_space(struct r5l_log *log,
1182 sector_t end)
1183 {
1184 struct block_device *bdev = log->rdev->bdev;
1185 struct mddev *mddev;
1186
1187 r5l_write_super(log, end);
1188
1189 if (!blk_queue_discard(bdev_get_queue(bdev)))
1190 return;
1191
1192 mddev = log->rdev->mddev;
1193 /*
1194 * Discard could zero data, so before discard we must make sure
1195 * superblock is updated to new log tail. Updating superblock (either
1196 * directly call md_update_sb() or depend on md thread) must hold
1197 * reconfig mutex. On the other hand, raid5_quiesce is called with
1198 * reconfig_mutex hold. The first step of raid5_quiesce() is waitting
1199 * for all IO finish, hence waitting for reclaim thread, while reclaim
1200 * thread is calling this function and waitting for reconfig mutex. So
1201 * there is a deadlock. We workaround this issue with a trylock.
1202 * FIXME: we could miss discard if we can't take reconfig mutex
1203 */
1204 set_mask_bits(&mddev->flags, 0,
1205 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
1206 if (!mddev_trylock(mddev))
1207 return;
1208 md_update_sb(mddev, 1);
1209 mddev_unlock(mddev);
1210
1211 /* discard IO error really doesn't matter, ignore it */
1212 if (log->last_checkpoint < end) {
1213 blkdev_issue_discard(bdev,
1214 log->last_checkpoint + log->rdev->data_offset,
1215 end - log->last_checkpoint, GFP_NOIO, 0);
1216 } else {
1217 blkdev_issue_discard(bdev,
1218 log->last_checkpoint + log->rdev->data_offset,
1219 log->device_size - log->last_checkpoint,
1220 GFP_NOIO, 0);
1221 blkdev_issue_discard(bdev, log->rdev->data_offset, end,
1222 GFP_NOIO, 0);
1223 }
1224 }
1225
1226 /*
1227 * r5c_flush_stripe moves stripe from cached list to handle_list. When called,
1228 * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes.
1229 *
1230 * must hold conf->device_lock
1231 */
1232 static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh)
1233 {
1234 BUG_ON(list_empty(&sh->lru));
1235 BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
1236 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
1237
1238 /*
1239 * The stripe is not ON_RELEASE_LIST, so it is safe to call
1240 * raid5_release_stripe() while holding conf->device_lock
1241 */
1242 BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
1243 assert_spin_locked(&conf->device_lock);
1244
1245 list_del_init(&sh->lru);
1246 atomic_inc(&sh->count);
1247
1248 set_bit(STRIPE_HANDLE, &sh->state);
1249 atomic_inc(&conf->active_stripes);
1250 r5c_make_stripe_write_out(sh);
1251
1252 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1253 atomic_inc(&conf->preread_active_stripes);
1254 raid5_release_stripe(sh);
1255 }
1256
1257 /*
1258 * if num == 0, flush all full stripes
1259 * if num > 0, flush all full stripes. If less than num full stripes are
1260 * flushed, flush some partial stripes until totally num stripes are
1261 * flushed or there is no more cached stripes.
1262 */
1263 void r5c_flush_cache(struct r5conf *conf, int num)
1264 {
1265 int count;
1266 struct stripe_head *sh, *next;
1267
1268 assert_spin_locked(&conf->device_lock);
1269 if (!conf->log)
1270 return;
1271
1272 count = 0;
1273 list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) {
1274 r5c_flush_stripe(conf, sh);
1275 count++;
1276 }
1277
1278 if (count >= num)
1279 return;
1280 list_for_each_entry_safe(sh, next,
1281 &conf->r5c_partial_stripe_list, lru) {
1282 r5c_flush_stripe(conf, sh);
1283 if (++count >= num)
1284 break;
1285 }
1286 }
1287
1288 static void r5c_do_reclaim(struct r5conf *conf)
1289 {
1290 struct r5l_log *log = conf->log;
1291 struct stripe_head *sh;
1292 int count = 0;
1293 unsigned long flags;
1294 int total_cached;
1295 int stripes_to_flush;
1296
1297 if (!r5c_is_writeback(log))
1298 return;
1299
1300 total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
1301 atomic_read(&conf->r5c_cached_full_stripes);
1302
1303 if (total_cached > conf->min_nr_stripes * 3 / 4 ||
1304 atomic_read(&conf->empty_inactive_list_nr) > 0)
1305 /*
1306 * if stripe cache pressure high, flush all full stripes and
1307 * some partial stripes
1308 */
1309 stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP;
1310 else if (total_cached > conf->min_nr_stripes * 1 / 2 ||
1311 atomic_read(&conf->r5c_cached_full_stripes) >
1312 R5C_FULL_STRIPE_FLUSH_BATCH)
1313 /*
1314 * if stripe cache pressure moderate, or if there is many full
1315 * stripes,flush all full stripes
1316 */
1317 stripes_to_flush = 0;
1318 else
1319 /* no need to flush */
1320 stripes_to_flush = -1;
1321
1322 if (stripes_to_flush >= 0) {
1323 spin_lock_irqsave(&conf->device_lock, flags);
1324 r5c_flush_cache(conf, stripes_to_flush);
1325 spin_unlock_irqrestore(&conf->device_lock, flags);
1326 }
1327
1328 /* if log space is tight, flush stripes on stripe_in_journal_list */
1329 if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) {
1330 spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
1331 spin_lock(&conf->device_lock);
1332 list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) {
1333 /*
1334 * stripes on stripe_in_journal_list could be in any
1335 * state of the stripe_cache state machine. In this
1336 * case, we only want to flush stripe on
1337 * r5c_cached_full/partial_stripes. The following
1338 * condition makes sure the stripe is on one of the
1339 * two lists.
1340 */
1341 if (!list_empty(&sh->lru) &&
1342 !test_bit(STRIPE_HANDLE, &sh->state) &&
1343 atomic_read(&sh->count) == 0) {
1344 r5c_flush_stripe(conf, sh);
1345 }
1346 if (count++ >= R5C_RECLAIM_STRIPE_GROUP)
1347 break;
1348 }
1349 spin_unlock(&conf->device_lock);
1350 spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
1351 }
1352 md_wakeup_thread(conf->mddev->thread);
1353 }
1354
1355 static void r5l_do_reclaim(struct r5l_log *log)
1356 {
1357 struct r5conf *conf = log->rdev->mddev->private;
1358 sector_t reclaim_target = xchg(&log->reclaim_target, 0);
1359 sector_t reclaimable;
1360 sector_t next_checkpoint;
1361 bool write_super;
1362
1363 spin_lock_irq(&log->io_list_lock);
1364 write_super = r5l_reclaimable_space(log) > log->max_free_space ||
1365 reclaim_target != 0 || !list_empty(&log->no_space_stripes);
1366 /*
1367 * move proper io_unit to reclaim list. We should not change the order.
1368 * reclaimable/unreclaimable io_unit can be mixed in the list, we
1369 * shouldn't reuse space of an unreclaimable io_unit
1370 */
1371 while (1) {
1372 reclaimable = r5l_reclaimable_space(log);
1373 if (reclaimable >= reclaim_target ||
1374 (list_empty(&log->running_ios) &&
1375 list_empty(&log->io_end_ios) &&
1376 list_empty(&log->flushing_ios) &&
1377 list_empty(&log->finished_ios)))
1378 break;
1379
1380 md_wakeup_thread(log->rdev->mddev->thread);
1381 wait_event_lock_irq(log->iounit_wait,
1382 r5l_reclaimable_space(log) > reclaimable,
1383 log->io_list_lock);
1384 }
1385
1386 next_checkpoint = r5c_calculate_new_cp(conf);
1387 spin_unlock_irq(&log->io_list_lock);
1388
1389 BUG_ON(reclaimable < 0);
1390
1391 if (reclaimable == 0 || !write_super)
1392 return;
1393
1394 /*
1395 * write_super will flush cache of each raid disk. We must write super
1396 * here, because the log area might be reused soon and we don't want to
1397 * confuse recovery
1398 */
1399 r5l_write_super_and_discard_space(log, next_checkpoint);
1400
1401 mutex_lock(&log->io_mutex);
1402 log->last_checkpoint = next_checkpoint;
1403 r5c_update_log_state(log);
1404 mutex_unlock(&log->io_mutex);
1405
1406 r5l_run_no_space_stripes(log);
1407 }
1408
1409 static void r5l_reclaim_thread(struct md_thread *thread)
1410 {
1411 struct mddev *mddev = thread->mddev;
1412 struct r5conf *conf = mddev->private;
1413 struct r5l_log *log = conf->log;
1414
1415 if (!log)
1416 return;
1417 r5c_do_reclaim(conf);
1418 r5l_do_reclaim(log);
1419 }
1420
1421 void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
1422 {
1423 unsigned long target;
1424 unsigned long new = (unsigned long)space; /* overflow in theory */
1425
1426 if (!log)
1427 return;
1428 do {
1429 target = log->reclaim_target;
1430 if (new < target)
1431 return;
1432 } while (cmpxchg(&log->reclaim_target, target, new) != target);
1433 md_wakeup_thread(log->reclaim_thread);
1434 }
1435
1436 void r5l_quiesce(struct r5l_log *log, int state)
1437 {
1438 struct mddev *mddev;
1439 if (!log || state == 2)
1440 return;
1441 if (state == 0)
1442 kthread_unpark(log->reclaim_thread->tsk);
1443 else if (state == 1) {
1444 /* make sure r5l_write_super_and_discard_space exits */
1445 mddev = log->rdev->mddev;
1446 wake_up(&mddev->sb_wait);
1447 kthread_park(log->reclaim_thread->tsk);
1448 r5l_wake_reclaim(log, MaxSector);
1449 r5l_do_reclaim(log);
1450 }
1451 }
1452
1453 bool r5l_log_disk_error(struct r5conf *conf)
1454 {
1455 struct r5l_log *log;
1456 bool ret;
1457 /* don't allow write if journal disk is missing */
1458 rcu_read_lock();
1459 log = rcu_dereference(conf->log);
1460
1461 if (!log)
1462 ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
1463 else
1464 ret = test_bit(Faulty, &log->rdev->flags);
1465 rcu_read_unlock();
1466 return ret;
1467 }
1468
1469 struct r5l_recovery_ctx {
1470 struct page *meta_page; /* current meta */
1471 sector_t meta_total_blocks; /* total size of current meta and data */
1472 sector_t pos; /* recovery position */
1473 u64 seq; /* recovery position seq */
1474 int data_parity_stripes; /* number of data_parity stripes */
1475 int data_only_stripes; /* number of data_only stripes */
1476 struct list_head cached_list;
1477 };
1478
1479 static int r5l_recovery_read_meta_block(struct r5l_log *log,
1480 struct r5l_recovery_ctx *ctx)
1481 {
1482 struct page *page = ctx->meta_page;
1483 struct r5l_meta_block *mb;
1484 u32 crc, stored_crc;
1485
1486 if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, REQ_OP_READ, 0,
1487 false))
1488 return -EIO;
1489
1490 mb = page_address(page);
1491 stored_crc = le32_to_cpu(mb->checksum);
1492 mb->checksum = 0;
1493
1494 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
1495 le64_to_cpu(mb->seq) != ctx->seq ||
1496 mb->version != R5LOG_VERSION ||
1497 le64_to_cpu(mb->position) != ctx->pos)
1498 return -EINVAL;
1499
1500 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
1501 if (stored_crc != crc)
1502 return -EINVAL;
1503
1504 if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
1505 return -EINVAL;
1506
1507 ctx->meta_total_blocks = BLOCK_SECTORS;
1508
1509 return 0;
1510 }
1511
1512 static void
1513 r5l_recovery_create_empty_meta_block(struct r5l_log *log,
1514 struct page *page,
1515 sector_t pos, u64 seq)
1516 {
1517 struct r5l_meta_block *mb;
1518 u32 crc;
1519
1520 mb = page_address(page);
1521 clear_page(mb);
1522 mb->magic = cpu_to_le32(R5LOG_MAGIC);
1523 mb->version = R5LOG_VERSION;
1524 mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
1525 mb->seq = cpu_to_le64(seq);
1526 mb->position = cpu_to_le64(pos);
1527 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
1528 mb->checksum = cpu_to_le32(crc);
1529 }
1530
1531 static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
1532 u64 seq)
1533 {
1534 struct page *page;
1535
1536 page = alloc_page(GFP_KERNEL);
1537 if (!page)
1538 return -ENOMEM;
1539 r5l_recovery_create_empty_meta_block(log, page, pos, seq);
1540 if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE,
1541 WRITE_FUA, false)) {
1542 __free_page(page);
1543 return -EIO;
1544 }
1545 __free_page(page);
1546 return 0;
1547 }
1548
1549 /*
1550 * r5l_recovery_load_data and r5l_recovery_load_parity uses flag R5_Wantwrite
1551 * to mark valid (potentially not flushed) data in the journal.
1552 *
1553 * We already verified checksum in r5l_recovery_verify_data_checksum_for_mb,
1554 * so there should not be any mismatch here.
1555 */
1556 static void r5l_recovery_load_data(struct r5l_log *log,
1557 struct stripe_head *sh,
1558 struct r5l_recovery_ctx *ctx,
1559 struct r5l_payload_data_parity *payload,
1560 sector_t log_offset)
1561 {
1562 struct mddev *mddev = log->rdev->mddev;
1563 struct r5conf *conf = mddev->private;
1564 int dd_idx;
1565
1566 raid5_compute_sector(conf,
1567 le64_to_cpu(payload->location), 0,
1568 &dd_idx, sh);
1569 sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1570 sh->dev[dd_idx].page, REQ_OP_READ, 0, false);
1571 sh->dev[dd_idx].log_checksum =
1572 le32_to_cpu(payload->checksum[0]);
1573 ctx->meta_total_blocks += BLOCK_SECTORS;
1574
1575 set_bit(R5_Wantwrite, &sh->dev[dd_idx].flags);
1576 set_bit(STRIPE_R5C_CACHING, &sh->state);
1577 }
1578
1579 static void r5l_recovery_load_parity(struct r5l_log *log,
1580 struct stripe_head *sh,
1581 struct r5l_recovery_ctx *ctx,
1582 struct r5l_payload_data_parity *payload,
1583 sector_t log_offset)
1584 {
1585 struct mddev *mddev = log->rdev->mddev;
1586 struct r5conf *conf = mddev->private;
1587
1588 ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
1589 sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1590 sh->dev[sh->pd_idx].page, REQ_OP_READ, 0, false);
1591 sh->dev[sh->pd_idx].log_checksum =
1592 le32_to_cpu(payload->checksum[0]);
1593 set_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags);
1594
1595 if (sh->qd_idx >= 0) {
1596 sync_page_io(log->rdev,
1597 r5l_ring_add(log, log_offset, BLOCK_SECTORS),
1598 PAGE_SIZE, sh->dev[sh->qd_idx].page,
1599 REQ_OP_READ, 0, false);
1600 sh->dev[sh->qd_idx].log_checksum =
1601 le32_to_cpu(payload->checksum[1]);
1602 set_bit(R5_Wantwrite, &sh->dev[sh->qd_idx].flags);
1603 }
1604 clear_bit(STRIPE_R5C_CACHING, &sh->state);
1605 }
1606
1607 static void r5l_recovery_reset_stripe(struct stripe_head *sh)
1608 {
1609 int i;
1610
1611 sh->state = 0;
1612 sh->log_start = MaxSector;
1613 for (i = sh->disks; i--; )
1614 sh->dev[i].flags = 0;
1615 }
1616
1617 static void
1618 r5l_recovery_replay_one_stripe(struct r5conf *conf,
1619 struct stripe_head *sh,
1620 struct r5l_recovery_ctx *ctx)
1621 {
1622 struct md_rdev *rdev, *rrdev;
1623 int disk_index;
1624 int data_count = 0;
1625
1626 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
1627 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
1628 continue;
1629 if (disk_index == sh->qd_idx || disk_index == sh->pd_idx)
1630 continue;
1631 data_count++;
1632 }
1633
1634 /*
1635 * stripes that only have parity must have been flushed
1636 * before the crash that we are now recovering from, so
1637 * there is nothing more to recovery.
1638 */
1639 if (data_count == 0)
1640 goto out;
1641
1642 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
1643 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
1644 continue;
1645
1646 /* in case device is broken */
1647 rcu_read_lock();
1648 rdev = rcu_dereference(conf->disks[disk_index].rdev);
1649 if (rdev) {
1650 atomic_inc(&rdev->nr_pending);
1651 rcu_read_unlock();
1652 sync_page_io(rdev, sh->sector, PAGE_SIZE,
1653 sh->dev[disk_index].page, REQ_OP_WRITE, 0,
1654 false);
1655 rdev_dec_pending(rdev, rdev->mddev);
1656 rcu_read_lock();
1657 }
1658 rrdev = rcu_dereference(conf->disks[disk_index].replacement);
1659 if (rrdev) {
1660 atomic_inc(&rrdev->nr_pending);
1661 rcu_read_unlock();
1662 sync_page_io(rrdev, sh->sector, PAGE_SIZE,
1663 sh->dev[disk_index].page, REQ_OP_WRITE, 0,
1664 false);
1665 rdev_dec_pending(rrdev, rrdev->mddev);
1666 rcu_read_lock();
1667 }
1668 rcu_read_unlock();
1669 }
1670 ctx->data_parity_stripes++;
1671 out:
1672 r5l_recovery_reset_stripe(sh);
1673 }
1674
1675 static struct stripe_head *
1676 r5c_recovery_alloc_stripe(struct r5conf *conf,
1677 sector_t stripe_sect,
1678 sector_t log_start)
1679 {
1680 struct stripe_head *sh;
1681
1682 sh = raid5_get_active_stripe(conf, stripe_sect, 0, 1, 0);
1683 if (!sh)
1684 return NULL; /* no more stripe available */
1685
1686 r5l_recovery_reset_stripe(sh);
1687 sh->log_start = log_start;
1688
1689 return sh;
1690 }
1691
1692 static struct stripe_head *
1693 r5c_recovery_lookup_stripe(struct list_head *list, sector_t sect)
1694 {
1695 struct stripe_head *sh;
1696
1697 list_for_each_entry(sh, list, lru)
1698 if (sh->sector == sect)
1699 return sh;
1700 return NULL;
1701 }
1702
1703 static void
1704 r5c_recovery_drop_stripes(struct list_head *cached_stripe_list,
1705 struct r5l_recovery_ctx *ctx)
1706 {
1707 struct stripe_head *sh, *next;
1708
1709 list_for_each_entry_safe(sh, next, cached_stripe_list, lru) {
1710 r5l_recovery_reset_stripe(sh);
1711 list_del_init(&sh->lru);
1712 raid5_release_stripe(sh);
1713 }
1714 }
1715
1716 static void
1717 r5c_recovery_replay_stripes(struct list_head *cached_stripe_list,
1718 struct r5l_recovery_ctx *ctx)
1719 {
1720 struct stripe_head *sh, *next;
1721
1722 list_for_each_entry_safe(sh, next, cached_stripe_list, lru)
1723 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
1724 r5l_recovery_replay_one_stripe(sh->raid_conf, sh, ctx);
1725 list_del_init(&sh->lru);
1726 raid5_release_stripe(sh);
1727 }
1728 }
1729
1730 /* if matches return 0; otherwise return -EINVAL */
1731 static int
1732 r5l_recovery_verify_data_checksum(struct r5l_log *log, struct page *page,
1733 sector_t log_offset, __le32 log_checksum)
1734 {
1735 void *addr;
1736 u32 checksum;
1737
1738 sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1739 page, REQ_OP_READ, 0, false);
1740 addr = kmap_atomic(page);
1741 checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
1742 kunmap_atomic(addr);
1743 return (le32_to_cpu(log_checksum) == checksum) ? 0 : -EINVAL;
1744 }
1745
1746 /*
1747 * before loading data to stripe cache, we need verify checksum for all data,
1748 * if there is mismatch for any data page, we drop all data in the mata block
1749 */
1750 static int
1751 r5l_recovery_verify_data_checksum_for_mb(struct r5l_log *log,
1752 struct r5l_recovery_ctx *ctx)
1753 {
1754 struct mddev *mddev = log->rdev->mddev;
1755 struct r5conf *conf = mddev->private;
1756 struct r5l_meta_block *mb = page_address(ctx->meta_page);
1757 sector_t mb_offset = sizeof(struct r5l_meta_block);
1758 sector_t log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
1759 struct page *page;
1760 struct r5l_payload_data_parity *payload;
1761
1762 page = alloc_page(GFP_KERNEL);
1763 if (!page)
1764 return -ENOMEM;
1765
1766 while (mb_offset < le32_to_cpu(mb->meta_size)) {
1767 payload = (void *)mb + mb_offset;
1768
1769 if (payload->header.type == R5LOG_PAYLOAD_DATA) {
1770 if (r5l_recovery_verify_data_checksum(
1771 log, page, log_offset,
1772 payload->checksum[0]) < 0)
1773 goto mismatch;
1774 } else if (payload->header.type == R5LOG_PAYLOAD_PARITY) {
1775 if (r5l_recovery_verify_data_checksum(
1776 log, page, log_offset,
1777 payload->checksum[0]) < 0)
1778 goto mismatch;
1779 if (conf->max_degraded == 2 && /* q for RAID 6 */
1780 r5l_recovery_verify_data_checksum(
1781 log, page,
1782 r5l_ring_add(log, log_offset,
1783 BLOCK_SECTORS),
1784 payload->checksum[1]) < 0)
1785 goto mismatch;
1786 } else /* not R5LOG_PAYLOAD_DATA or R5LOG_PAYLOAD_PARITY */
1787 goto mismatch;
1788
1789 log_offset = r5l_ring_add(log, log_offset,
1790 le32_to_cpu(payload->size));
1791
1792 mb_offset += sizeof(struct r5l_payload_data_parity) +
1793 sizeof(__le32) *
1794 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
1795 }
1796
1797 put_page(page);
1798 return 0;
1799
1800 mismatch:
1801 put_page(page);
1802 return -EINVAL;
1803 }
1804
1805 /*
1806 * Analyze all data/parity pages in one meta block
1807 * Returns:
1808 * 0 for success
1809 * -EINVAL for unknown playload type
1810 * -EAGAIN for checksum mismatch of data page
1811 * -ENOMEM for run out of memory (alloc_page failed or run out of stripes)
1812 */
1813 static int
1814 r5c_recovery_analyze_meta_block(struct r5l_log *log,
1815 struct r5l_recovery_ctx *ctx,
1816 struct list_head *cached_stripe_list)
1817 {
1818 struct mddev *mddev = log->rdev->mddev;
1819 struct r5conf *conf = mddev->private;
1820 struct r5l_meta_block *mb;
1821 struct r5l_payload_data_parity *payload;
1822 int mb_offset;
1823 sector_t log_offset;
1824 sector_t stripe_sect;
1825 struct stripe_head *sh;
1826 int ret;
1827
1828 /*
1829 * for mismatch in data blocks, we will drop all data in this mb, but
1830 * we will still read next mb for other data with FLUSH flag, as
1831 * io_unit could finish out of order.
1832 */
1833 ret = r5l_recovery_verify_data_checksum_for_mb(log, ctx);
1834 if (ret == -EINVAL)
1835 return -EAGAIN;
1836 else if (ret)
1837 return ret; /* -ENOMEM duo to alloc_page() failed */
1838
1839 mb = page_address(ctx->meta_page);
1840 mb_offset = sizeof(struct r5l_meta_block);
1841 log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
1842
1843 while (mb_offset < le32_to_cpu(mb->meta_size)) {
1844 int dd;
1845
1846 payload = (void *)mb + mb_offset;
1847 stripe_sect = (payload->header.type == R5LOG_PAYLOAD_DATA) ?
1848 raid5_compute_sector(
1849 conf, le64_to_cpu(payload->location), 0, &dd,
1850 NULL)
1851 : le64_to_cpu(payload->location);
1852
1853 sh = r5c_recovery_lookup_stripe(cached_stripe_list,
1854 stripe_sect);
1855
1856 if (!sh) {
1857 sh = r5c_recovery_alloc_stripe(conf, stripe_sect, ctx->pos);
1858 /*
1859 * cannot get stripe from raid5_get_active_stripe
1860 * try replay some stripes
1861 */
1862 if (!sh) {
1863 r5c_recovery_replay_stripes(
1864 cached_stripe_list, ctx);
1865 sh = r5c_recovery_alloc_stripe(
1866 conf, stripe_sect, ctx->pos);
1867 }
1868 if (!sh) {
1869 pr_debug("md/raid:%s: Increasing stripe cache size to %d to recovery data on journal.\n",
1870 mdname(mddev),
1871 conf->min_nr_stripes * 2);
1872 raid5_set_cache_size(mddev,
1873 conf->min_nr_stripes * 2);
1874 sh = r5c_recovery_alloc_stripe(
1875 conf, stripe_sect, ctx->pos);
1876 }
1877 if (!sh) {
1878 pr_err("md/raid:%s: Cannot get enough stripes due to memory pressure. Recovery failed.\n",
1879 mdname(mddev));
1880 return -ENOMEM;
1881 }
1882 list_add_tail(&sh->lru, cached_stripe_list);
1883 }
1884
1885 if (payload->header.type == R5LOG_PAYLOAD_DATA) {
1886 if (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
1887 test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags)) {
1888 r5l_recovery_replay_one_stripe(conf, sh, ctx);
1889 sh->log_start = ctx->pos;
1890 list_move_tail(&sh->lru, cached_stripe_list);
1891 }
1892 r5l_recovery_load_data(log, sh, ctx, payload,
1893 log_offset);
1894 } else if (payload->header.type == R5LOG_PAYLOAD_PARITY)
1895 r5l_recovery_load_parity(log, sh, ctx, payload,
1896 log_offset);
1897 else
1898 return -EINVAL;
1899
1900 log_offset = r5l_ring_add(log, log_offset,
1901 le32_to_cpu(payload->size));
1902
1903 mb_offset += sizeof(struct r5l_payload_data_parity) +
1904 sizeof(__le32) *
1905 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
1906 }
1907
1908 return 0;
1909 }
1910
1911 /*
1912 * Load the stripe into cache. The stripe will be written out later by
1913 * the stripe cache state machine.
1914 */
1915 static void r5c_recovery_load_one_stripe(struct r5l_log *log,
1916 struct stripe_head *sh)
1917 {
1918 struct r5dev *dev;
1919 int i;
1920
1921 for (i = sh->disks; i--; ) {
1922 dev = sh->dev + i;
1923 if (test_and_clear_bit(R5_Wantwrite, &dev->flags)) {
1924 set_bit(R5_InJournal, &dev->flags);
1925 set_bit(R5_UPTODATE, &dev->flags);
1926 }
1927 }
1928 list_add_tail(&sh->r5c, &log->stripe_in_journal_list);
1929 atomic_inc(&log->stripe_in_journal_count);
1930 }
1931
1932 /*
1933 * Scan through the log for all to-be-flushed data
1934 *
1935 * For stripes with data and parity, namely Data-Parity stripe
1936 * (STRIPE_R5C_CACHING == 0), we simply replay all the writes.
1937 *
1938 * For stripes with only data, namely Data-Only stripe
1939 * (STRIPE_R5C_CACHING == 1), we load them to stripe cache state machine.
1940 *
1941 * For a stripe, if we see data after parity, we should discard all previous
1942 * data and parity for this stripe, as these data are already flushed to
1943 * the array.
1944 *
1945 * At the end of the scan, we return the new journal_tail, which points to
1946 * first data-only stripe on the journal device, or next invalid meta block.
1947 */
1948 static int r5c_recovery_flush_log(struct r5l_log *log,
1949 struct r5l_recovery_ctx *ctx)
1950 {
1951 struct stripe_head *sh;
1952 int ret = 0;
1953
1954 /* scan through the log */
1955 while (1) {
1956 if (r5l_recovery_read_meta_block(log, ctx))
1957 break;
1958
1959 ret = r5c_recovery_analyze_meta_block(log, ctx,
1960 &ctx->cached_list);
1961 /*
1962 * -EAGAIN means mismatch in data block, in this case, we still
1963 * try scan the next metablock
1964 */
1965 if (ret && ret != -EAGAIN)
1966 break; /* ret == -EINVAL or -ENOMEM */
1967 ctx->seq++;
1968 ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
1969 }
1970
1971 if (ret == -ENOMEM) {
1972 r5c_recovery_drop_stripes(&ctx->cached_list, ctx);
1973 return ret;
1974 }
1975
1976 /* replay data-parity stripes */
1977 r5c_recovery_replay_stripes(&ctx->cached_list, ctx);
1978
1979 /* load data-only stripes to stripe cache */
1980 list_for_each_entry(sh, &ctx->cached_list, lru) {
1981 WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
1982 r5c_recovery_load_one_stripe(log, sh);
1983 ctx->data_only_stripes++;
1984 }
1985
1986 return 0;
1987 }
1988
1989 /*
1990 * we did a recovery. Now ctx.pos points to an invalid meta block. New
1991 * log will start here. but we can't let superblock point to last valid
1992 * meta block. The log might looks like:
1993 * | meta 1| meta 2| meta 3|
1994 * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
1995 * superblock points to meta 1, we write a new valid meta 2n. if crash
1996 * happens again, new recovery will start from meta 1. Since meta 2n is
1997 * valid now, recovery will think meta 3 is valid, which is wrong.
1998 * The solution is we create a new meta in meta2 with its seq == meta
1999 * 1's seq + 10 and let superblock points to meta2. The same recovery will
2000 * not think meta 3 is a valid meta, because its seq doesn't match
2001 */
2002
2003 /*
2004 * Before recovery, the log looks like the following
2005 *
2006 * ---------------------------------------------
2007 * | valid log | invalid log |
2008 * ---------------------------------------------
2009 * ^
2010 * |- log->last_checkpoint
2011 * |- log->last_cp_seq
2012 *
2013 * Now we scan through the log until we see invalid entry
2014 *
2015 * ---------------------------------------------
2016 * | valid log | invalid log |
2017 * ---------------------------------------------
2018 * ^ ^
2019 * |- log->last_checkpoint |- ctx->pos
2020 * |- log->last_cp_seq |- ctx->seq
2021 *
2022 * From this point, we need to increase seq number by 10 to avoid
2023 * confusing next recovery.
2024 *
2025 * ---------------------------------------------
2026 * | valid log | invalid log |
2027 * ---------------------------------------------
2028 * ^ ^
2029 * |- log->last_checkpoint |- ctx->pos+1
2030 * |- log->last_cp_seq |- ctx->seq+11
2031 *
2032 * However, it is not safe to start the state machine yet, because data only
2033 * parities are not yet secured in RAID. To save these data only parities, we
2034 * rewrite them from seq+11.
2035 *
2036 * -----------------------------------------------------------------
2037 * | valid log | data only stripes | invalid log |
2038 * -----------------------------------------------------------------
2039 * ^ ^
2040 * |- log->last_checkpoint |- ctx->pos+n
2041 * |- log->last_cp_seq |- ctx->seq+10+n
2042 *
2043 * If failure happens again during this process, the recovery can safe start
2044 * again from log->last_checkpoint.
2045 *
2046 * Once data only stripes are rewritten to journal, we move log_tail
2047 *
2048 * -----------------------------------------------------------------
2049 * | old log | data only stripes | invalid log |
2050 * -----------------------------------------------------------------
2051 * ^ ^
2052 * |- log->last_checkpoint |- ctx->pos+n
2053 * |- log->last_cp_seq |- ctx->seq+10+n
2054 *
2055 * Then we can safely start the state machine. If failure happens from this
2056 * point on, the recovery will start from new log->last_checkpoint.
2057 */
2058 static int
2059 r5c_recovery_rewrite_data_only_stripes(struct r5l_log *log,
2060 struct r5l_recovery_ctx *ctx)
2061 {
2062 struct stripe_head *sh, *next;
2063 struct mddev *mddev = log->rdev->mddev;
2064 struct page *page;
2065
2066 page = alloc_page(GFP_KERNEL);
2067 if (!page) {
2068 pr_err("md/raid:%s: cannot allocate memory to rewrite data only stripes\n",
2069 mdname(mddev));
2070 return -ENOMEM;
2071 }
2072
2073 ctx->seq += 10;
2074 list_for_each_entry_safe(sh, next, &ctx->cached_list, lru) {
2075 struct r5l_meta_block *mb;
2076 int i;
2077 int offset;
2078 sector_t write_pos;
2079
2080 WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
2081 r5l_recovery_create_empty_meta_block(log, page,
2082 ctx->pos, ctx->seq);
2083 mb = page_address(page);
2084 offset = le32_to_cpu(mb->meta_size);
2085 write_pos = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
2086
2087 for (i = sh->disks; i--; ) {
2088 struct r5dev *dev = &sh->dev[i];
2089 struct r5l_payload_data_parity *payload;
2090 void *addr;
2091
2092 if (test_bit(R5_InJournal, &dev->flags)) {
2093 payload = (void *)mb + offset;
2094 payload->header.type = cpu_to_le16(
2095 R5LOG_PAYLOAD_DATA);
2096 payload->size = BLOCK_SECTORS;
2097 payload->location = cpu_to_le64(
2098 raid5_compute_blocknr(sh, i, 0));
2099 addr = kmap_atomic(dev->page);
2100 payload->checksum[0] = cpu_to_le32(
2101 crc32c_le(log->uuid_checksum, addr,
2102 PAGE_SIZE));
2103 kunmap_atomic(addr);
2104 sync_page_io(log->rdev, write_pos, PAGE_SIZE,
2105 dev->page, REQ_OP_WRITE, 0, false);
2106 write_pos = r5l_ring_add(log, write_pos,
2107 BLOCK_SECTORS);
2108 offset += sizeof(__le32) +
2109 sizeof(struct r5l_payload_data_parity);
2110
2111 }
2112 }
2113 mb->meta_size = cpu_to_le32(offset);
2114 mb->checksum = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
2115 sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page,
2116 REQ_OP_WRITE, WRITE_FUA, false);
2117 sh->log_start = ctx->pos;
2118 ctx->pos = write_pos;
2119 ctx->seq += 1;
2120
2121 list_del_init(&sh->lru);
2122 raid5_release_stripe(sh);
2123 }
2124 __free_page(page);
2125 return 0;
2126 }
2127
2128 static int r5l_recovery_log(struct r5l_log *log)
2129 {
2130 struct mddev *mddev = log->rdev->mddev;
2131 struct r5l_recovery_ctx ctx;
2132 int ret;
2133
2134 ctx.pos = log->last_checkpoint;
2135 ctx.seq = log->last_cp_seq;
2136 ctx.meta_page = alloc_page(GFP_KERNEL);
2137 ctx.data_only_stripes = 0;
2138 ctx.data_parity_stripes = 0;
2139 INIT_LIST_HEAD(&ctx.cached_list);
2140
2141 if (!ctx.meta_page)
2142 return -ENOMEM;
2143
2144 ret = r5c_recovery_flush_log(log, &ctx);
2145 __free_page(ctx.meta_page);
2146
2147 if (ret)
2148 return ret;
2149
2150 if ((ctx.data_only_stripes == 0) && (ctx.data_parity_stripes == 0))
2151 pr_debug("md/raid:%s: starting from clean shutdown\n",
2152 mdname(mddev));
2153 else {
2154 pr_debug("md/raid:%s: recoverying %d data-only stripes and %d data-parity stripes\n",
2155 mdname(mddev), ctx.data_only_stripes,
2156 ctx.data_parity_stripes);
2157
2158 if (ctx.data_only_stripes > 0)
2159 if (r5c_recovery_rewrite_data_only_stripes(log, &ctx)) {
2160 pr_err("md/raid:%s: failed to rewrite stripes to journal\n",
2161 mdname(mddev));
2162 return -EIO;
2163 }
2164 }
2165
2166 log->log_start = ctx.pos;
2167 log->next_checkpoint = ctx.pos;
2168 log->seq = ctx.seq;
2169 r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq);
2170 r5l_write_super(log, ctx.pos);
2171 return 0;
2172 }
2173
2174 static void r5l_write_super(struct r5l_log *log, sector_t cp)
2175 {
2176 struct mddev *mddev = log->rdev->mddev;
2177
2178 log->rdev->journal_tail = cp;
2179 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2180 }
2181
2182 static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page)
2183 {
2184 struct r5conf *conf = mddev->private;
2185 int ret;
2186
2187 if (!conf->log)
2188 return 0;
2189
2190 switch (conf->log->r5c_journal_mode) {
2191 case R5C_JOURNAL_MODE_WRITE_THROUGH:
2192 ret = snprintf(
2193 page, PAGE_SIZE, "[%s] %s\n",
2194 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
2195 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
2196 break;
2197 case R5C_JOURNAL_MODE_WRITE_BACK:
2198 ret = snprintf(
2199 page, PAGE_SIZE, "%s [%s]\n",
2200 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
2201 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
2202 break;
2203 default:
2204 ret = 0;
2205 }
2206 return ret;
2207 }
2208
2209 static ssize_t r5c_journal_mode_store(struct mddev *mddev,
2210 const char *page, size_t length)
2211 {
2212 struct r5conf *conf = mddev->private;
2213 struct r5l_log *log = conf->log;
2214 int val = -1, i;
2215 int len = length;
2216
2217 if (!log)
2218 return -ENODEV;
2219
2220 if (len && page[len - 1] == '\n')
2221 len -= 1;
2222 for (i = 0; i < ARRAY_SIZE(r5c_journal_mode_str); i++)
2223 if (strlen(r5c_journal_mode_str[i]) == len &&
2224 strncmp(page, r5c_journal_mode_str[i], len) == 0) {
2225 val = i;
2226 break;
2227 }
2228 if (val < R5C_JOURNAL_MODE_WRITE_THROUGH ||
2229 val > R5C_JOURNAL_MODE_WRITE_BACK)
2230 return -EINVAL;
2231
2232 mddev_suspend(mddev);
2233 conf->log->r5c_journal_mode = val;
2234 mddev_resume(mddev);
2235
2236 pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n",
2237 mdname(mddev), val, r5c_journal_mode_str[val]);
2238 return length;
2239 }
2240
2241 struct md_sysfs_entry
2242 r5c_journal_mode = __ATTR(journal_mode, 0644,
2243 r5c_journal_mode_show, r5c_journal_mode_store);
2244
2245 /*
2246 * Try handle write operation in caching phase. This function should only
2247 * be called in write-back mode.
2248 *
2249 * If all outstanding writes can be handled in caching phase, returns 0
2250 * If writes requires write-out phase, call r5c_make_stripe_write_out()
2251 * and returns -EAGAIN
2252 */
2253 int r5c_try_caching_write(struct r5conf *conf,
2254 struct stripe_head *sh,
2255 struct stripe_head_state *s,
2256 int disks)
2257 {
2258 struct r5l_log *log = conf->log;
2259 int i;
2260 struct r5dev *dev;
2261 int to_cache = 0;
2262
2263 BUG_ON(!r5c_is_writeback(log));
2264
2265 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
2266 /*
2267 * There are two different scenarios here:
2268 * 1. The stripe has some data cached, and it is sent to
2269 * write-out phase for reclaim
2270 * 2. The stripe is clean, and this is the first write
2271 *
2272 * For 1, return -EAGAIN, so we continue with
2273 * handle_stripe_dirtying().
2274 *
2275 * For 2, set STRIPE_R5C_CACHING and continue with caching
2276 * write.
2277 */
2278
2279 /* case 1: anything injournal or anything in written */
2280 if (s->injournal > 0 || s->written > 0)
2281 return -EAGAIN;
2282 /* case 2 */
2283 set_bit(STRIPE_R5C_CACHING, &sh->state);
2284 }
2285
2286 for (i = disks; i--; ) {
2287 dev = &sh->dev[i];
2288 /* if non-overwrite, use writing-out phase */
2289 if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) &&
2290 !test_bit(R5_InJournal, &dev->flags)) {
2291 r5c_make_stripe_write_out(sh);
2292 return -EAGAIN;
2293 }
2294 }
2295
2296 for (i = disks; i--; ) {
2297 dev = &sh->dev[i];
2298 if (dev->towrite) {
2299 set_bit(R5_Wantwrite, &dev->flags);
2300 set_bit(R5_Wantdrain, &dev->flags);
2301 set_bit(R5_LOCKED, &dev->flags);
2302 to_cache++;
2303 }
2304 }
2305
2306 if (to_cache) {
2307 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2308 /*
2309 * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data()
2310 * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in
2311 * r5c_handle_data_cached()
2312 */
2313 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
2314 }
2315
2316 return 0;
2317 }
2318
2319 /*
2320 * free extra pages (orig_page) we allocated for prexor
2321 */
2322 void r5c_release_extra_page(struct stripe_head *sh)
2323 {
2324 struct r5conf *conf = sh->raid_conf;
2325 int i;
2326 bool using_disk_info_extra_page;
2327
2328 using_disk_info_extra_page =
2329 sh->dev[0].orig_page == conf->disks[0].extra_page;
2330
2331 for (i = sh->disks; i--; )
2332 if (sh->dev[i].page != sh->dev[i].orig_page) {
2333 struct page *p = sh->dev[i].orig_page;
2334
2335 sh->dev[i].orig_page = sh->dev[i].page;
2336 if (!using_disk_info_extra_page)
2337 put_page(p);
2338 }
2339
2340 if (using_disk_info_extra_page) {
2341 clear_bit(R5C_EXTRA_PAGE_IN_USE, &conf->cache_state);
2342 md_wakeup_thread(conf->mddev->thread);
2343 }
2344 }
2345
2346 void r5c_use_extra_page(struct stripe_head *sh)
2347 {
2348 struct r5conf *conf = sh->raid_conf;
2349 int i;
2350 struct r5dev *dev;
2351
2352 for (i = sh->disks; i--; ) {
2353 dev = &sh->dev[i];
2354 if (dev->orig_page != dev->page)
2355 put_page(dev->orig_page);
2356 dev->orig_page = conf->disks[i].extra_page;
2357 }
2358 }
2359
2360 /*
2361 * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the
2362 * stripe is committed to RAID disks.
2363 */
2364 void r5c_finish_stripe_write_out(struct r5conf *conf,
2365 struct stripe_head *sh,
2366 struct stripe_head_state *s)
2367 {
2368 int i;
2369 int do_wakeup = 0;
2370
2371 if (!conf->log ||
2372 !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags))
2373 return;
2374
2375 WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
2376 clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
2377
2378 if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
2379 return;
2380
2381 for (i = sh->disks; i--; ) {
2382 clear_bit(R5_InJournal, &sh->dev[i].flags);
2383 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2384 do_wakeup = 1;
2385 }
2386
2387 /*
2388 * analyse_stripe() runs before r5c_finish_stripe_write_out(),
2389 * We updated R5_InJournal, so we also update s->injournal.
2390 */
2391 s->injournal = 0;
2392
2393 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2394 if (atomic_dec_and_test(&conf->pending_full_writes))
2395 md_wakeup_thread(conf->mddev->thread);
2396
2397 if (do_wakeup)
2398 wake_up(&conf->wait_for_overlap);
2399
2400 if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
2401 return;
2402
2403 spin_lock_irq(&conf->log->stripe_in_journal_lock);
2404 list_del_init(&sh->r5c);
2405 spin_unlock_irq(&conf->log->stripe_in_journal_lock);
2406 sh->log_start = MaxSector;
2407 atomic_dec(&conf->log->stripe_in_journal_count);
2408 }
2409
2410 int
2411 r5c_cache_data(struct r5l_log *log, struct stripe_head *sh,
2412 struct stripe_head_state *s)
2413 {
2414 struct r5conf *conf = sh->raid_conf;
2415 int pages = 0;
2416 int reserve;
2417 int i;
2418 int ret = 0;
2419
2420 BUG_ON(!log);
2421
2422 for (i = 0; i < sh->disks; i++) {
2423 void *addr;
2424
2425 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
2426 continue;
2427 addr = kmap_atomic(sh->dev[i].page);
2428 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
2429 addr, PAGE_SIZE);
2430 kunmap_atomic(addr);
2431 pages++;
2432 }
2433 WARN_ON(pages == 0);
2434
2435 /*
2436 * The stripe must enter state machine again to call endio, so
2437 * don't delay.
2438 */
2439 clear_bit(STRIPE_DELAYED, &sh->state);
2440 atomic_inc(&sh->count);
2441
2442 mutex_lock(&log->io_mutex);
2443 /* meta + data */
2444 reserve = (1 + pages) << (PAGE_SHIFT - 9);
2445
2446 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
2447 sh->log_start == MaxSector)
2448 r5l_add_no_space_stripe(log, sh);
2449 else if (!r5l_has_free_space(log, reserve)) {
2450 if (sh->log_start == log->last_checkpoint)
2451 BUG();
2452 else
2453 r5l_add_no_space_stripe(log, sh);
2454 } else {
2455 ret = r5l_log_stripe(log, sh, pages, 0);
2456 if (ret) {
2457 spin_lock_irq(&log->io_list_lock);
2458 list_add_tail(&sh->log_list, &log->no_mem_stripes);
2459 spin_unlock_irq(&log->io_list_lock);
2460 }
2461 }
2462
2463 mutex_unlock(&log->io_mutex);
2464 return 0;
2465 }
2466
2467 static int r5l_load_log(struct r5l_log *log)
2468 {
2469 struct md_rdev *rdev = log->rdev;
2470 struct page *page;
2471 struct r5l_meta_block *mb;
2472 sector_t cp = log->rdev->journal_tail;
2473 u32 stored_crc, expected_crc;
2474 bool create_super = false;
2475 int ret;
2476
2477 /* Make sure it's valid */
2478 if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
2479 cp = 0;
2480 page = alloc_page(GFP_KERNEL);
2481 if (!page)
2482 return -ENOMEM;
2483
2484 if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) {
2485 ret = -EIO;
2486 goto ioerr;
2487 }
2488 mb = page_address(page);
2489
2490 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
2491 mb->version != R5LOG_VERSION) {
2492 create_super = true;
2493 goto create;
2494 }
2495 stored_crc = le32_to_cpu(mb->checksum);
2496 mb->checksum = 0;
2497 expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
2498 if (stored_crc != expected_crc) {
2499 create_super = true;
2500 goto create;
2501 }
2502 if (le64_to_cpu(mb->position) != cp) {
2503 create_super = true;
2504 goto create;
2505 }
2506 create:
2507 if (create_super) {
2508 log->last_cp_seq = prandom_u32();
2509 cp = 0;
2510 r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq);
2511 /*
2512 * Make sure super points to correct address. Log might have
2513 * data very soon. If super hasn't correct log tail address,
2514 * recovery can't find the log
2515 */
2516 r5l_write_super(log, cp);
2517 } else
2518 log->last_cp_seq = le64_to_cpu(mb->seq);
2519
2520 log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
2521 log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
2522 if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
2523 log->max_free_space = RECLAIM_MAX_FREE_SPACE;
2524 log->last_checkpoint = cp;
2525 log->next_checkpoint = cp;
2526 mutex_lock(&log->io_mutex);
2527 r5c_update_log_state(log);
2528 mutex_unlock(&log->io_mutex);
2529
2530 __free_page(page);
2531
2532 return r5l_recovery_log(log);
2533 ioerr:
2534 __free_page(page);
2535 return ret;
2536 }
2537
2538 int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
2539 {
2540 struct request_queue *q = bdev_get_queue(rdev->bdev);
2541 struct r5l_log *log;
2542
2543 if (PAGE_SIZE != 4096)
2544 return -EINVAL;
2545
2546 /*
2547 * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and
2548 * raid_disks r5l_payload_data_parity.
2549 *
2550 * Write journal and cache does not work for very big array
2551 * (raid_disks > 203)
2552 */
2553 if (sizeof(struct r5l_meta_block) +
2554 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) *
2555 conf->raid_disks) > PAGE_SIZE) {
2556 pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n",
2557 mdname(conf->mddev), conf->raid_disks);
2558 return -EINVAL;
2559 }
2560
2561 log = kzalloc(sizeof(*log), GFP_KERNEL);
2562 if (!log)
2563 return -ENOMEM;
2564 log->rdev = rdev;
2565
2566 log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0;
2567
2568 log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
2569 sizeof(rdev->mddev->uuid));
2570
2571 mutex_init(&log->io_mutex);
2572
2573 spin_lock_init(&log->io_list_lock);
2574 INIT_LIST_HEAD(&log->running_ios);
2575 INIT_LIST_HEAD(&log->io_end_ios);
2576 INIT_LIST_HEAD(&log->flushing_ios);
2577 INIT_LIST_HEAD(&log->finished_ios);
2578 bio_init(&log->flush_bio);
2579
2580 log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
2581 if (!log->io_kc)
2582 goto io_kc;
2583
2584 log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc);
2585 if (!log->io_pool)
2586 goto io_pool;
2587
2588 log->bs = bioset_create(R5L_POOL_SIZE, 0);
2589 if (!log->bs)
2590 goto io_bs;
2591
2592 log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
2593 if (!log->meta_pool)
2594 goto out_mempool;
2595
2596 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
2597 log->rdev->mddev, "reclaim");
2598 if (!log->reclaim_thread)
2599 goto reclaim_thread;
2600 log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
2601
2602 init_waitqueue_head(&log->iounit_wait);
2603
2604 INIT_LIST_HEAD(&log->no_mem_stripes);
2605
2606 INIT_LIST_HEAD(&log->no_space_stripes);
2607 spin_lock_init(&log->no_space_stripes_lock);
2608
2609 INIT_WORK(&log->deferred_io_work, r5l_submit_io_async);
2610
2611 log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
2612 INIT_LIST_HEAD(&log->stripe_in_journal_list);
2613 spin_lock_init(&log->stripe_in_journal_lock);
2614 atomic_set(&log->stripe_in_journal_count, 0);
2615
2616 if (r5l_load_log(log))
2617 goto error;
2618
2619 rcu_assign_pointer(conf->log, log);
2620 set_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
2621 return 0;
2622
2623 error:
2624 md_unregister_thread(&log->reclaim_thread);
2625 reclaim_thread:
2626 mempool_destroy(log->meta_pool);
2627 out_mempool:
2628 bioset_free(log->bs);
2629 io_bs:
2630 mempool_destroy(log->io_pool);
2631 io_pool:
2632 kmem_cache_destroy(log->io_kc);
2633 io_kc:
2634 kfree(log);
2635 return -EINVAL;
2636 }
2637
2638 void r5l_exit_log(struct r5l_log *log)
2639 {
2640 md_unregister_thread(&log->reclaim_thread);
2641 mempool_destroy(log->meta_pool);
2642 bioset_free(log->bs);
2643 mempool_destroy(log->io_pool);
2644 kmem_cache_destroy(log->io_kc);
2645 kfree(log);
2646 }