]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/md/raid5-cache.c
md/raid5-cache: use ring add to prevent overflow
[mirror_ubuntu-artful-kernel.git] / drivers / md / raid5-cache.c
CommitLineData
f6bed0ef
SL
1/*
2 * Copyright (C) 2015 Shaohua Li <shli@fb.com>
b4c625c6 3 * Copyright (C) 2016 Song Liu <songliubraving@fb.com>
f6bed0ef
SL
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>
5cb2fbd6 20#include <linux/crc32c.h>
f6bed0ef 21#include <linux/random.h>
ce1ccd07 22#include <linux/kthread.h>
f6bed0ef
SL
23#include "md.h"
24#include "raid5.h"
1e6d690b 25#include "bitmap.h"
f6bed0ef
SL
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
0576b1c6 33/*
a39f7afd
SL
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
0576b1c6
SL
38 */
39#define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
40#define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
41
a39f7afd
SL
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
c38d29b3
CH
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
2ded3703
SL
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 */
60enum r5c_journal_mode {
61 R5C_JOURNAL_MODE_WRITE_THROUGH = 0,
62 R5C_JOURNAL_MODE_WRITE_BACK = 1,
63};
64
2c7da14b
SL
65static char *r5c_journal_mode_str[] = {"write-through",
66 "write-back"};
2ded3703
SL
67/*
68 * raid5 cache state machine
69 *
9b69173e 70 * With the RAID cache, each stripe works in two phases:
2ded3703
SL
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
f6bed0ef
SL
98struct 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 */
0576b1c6
SL
105 sector_t max_free_space; /* reclaim run if free space is at
106 * this size */
f6bed0ef
SL
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
17036461
CH
115 sector_t next_checkpoint;
116 u64 next_cp_seq;
117
f6bed0ef
SL
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 */
a8c34f91
SL
128 struct list_head flushing_ios; /* io_units which are waiting for log
129 * cache flush */
04732f74 130 struct list_head finished_ios; /* io_units which settle down in log disk */
a8c34f91 131 struct bio flush_bio;
f6bed0ef 132
5036c390
CH
133 struct list_head no_mem_stripes; /* pending stripes, -ENOMEM */
134
f6bed0ef 135 struct kmem_cache *io_kc;
5036c390 136 mempool_t *io_pool;
c38d29b3 137 struct bio_set *bs;
e8deb638 138 mempool_t *meta_pool;
f6bed0ef 139
0576b1c6
SL
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) */
0fd22b45 148 wait_queue_head_t iounit_wait;
0576b1c6 149
f6bed0ef
SL
150 struct list_head no_space_stripes; /* pending stripes, log has no space */
151 spinlock_t no_space_stripes_lock;
56fef7c6
CH
152
153 bool need_cache_flush;
2ded3703
SL
154
155 /* for r5c_cache */
156 enum r5c_journal_mode r5c_journal_mode;
a39f7afd
SL
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;
3bddb7f8
SL
163
164 /* to submit async io_units, to fulfill ordering of flush */
165 struct work_struct deferred_io_work;
f6bed0ef
SL
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 */
175struct 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
f6bed0ef
SL
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;
6143e2ce 191 bool need_split_bio;
3bddb7f8
SL
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 */
f6bed0ef
SL
204};
205
206/* r5l_io_unit state */
207enum 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 */
a8c34f91 212 IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
f6bed0ef
SL
213};
214
2ded3703
SL
215bool r5c_is_writeback(struct r5l_log *log)
216{
217 return (log != NULL &&
218 log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK);
219}
220
f6bed0ef
SL
221static 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
229static 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
238static 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
f6bed0ef
SL
248static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
249 enum r5l_io_unit_state state)
250{
f6bed0ef
SL
251 if (WARN_ON(io->state >= state))
252 return;
253 io->state = state;
f6bed0ef
SL
254}
255
1e6d690b
SL
256static void
257r5c_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
275void 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
a39f7afd
SL
293/* Check whether we should flush some stripes to free up stripe cache */
294void 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 */
321void 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 */
351static 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 */
369static 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
2ded3703
SL
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 */
a39f7afd 395void r5c_make_stripe_write_out(struct stripe_head *sh)
2ded3703
SL
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);
1e6d690b
SL
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
419static 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 */
435static 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);
2ded3703
SL
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 */
448static 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);
1e6d690b
SL
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 }
2ded3703
SL
467}
468
d8858f43
CH
469static 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);
2ded3703
SL
475
476 r5c_finish_cache_stripe(sh);
477
d8858f43
CH
478 set_bit(STRIPE_HANDLE, &sh->state);
479 raid5_release_stripe(sh);
480 }
481}
482
56fef7c6
CH
483static 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
3848c0bc
CH
499static 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
3bddb7f8 513static void __r5l_stripe_write_finished(struct r5l_io_unit *io);
f6bed0ef
SL
514static void r5l_log_endio(struct bio *bio)
515{
516 struct r5l_io_unit *io = bio->bi_private;
3bddb7f8 517 struct r5l_io_unit *io_deferred;
f6bed0ef 518 struct r5l_log *log = io->log;
509ffec7 519 unsigned long flags;
f6bed0ef 520
6e74a9cf
SL
521 if (bio->bi_error)
522 md_error(log->rdev->mddev, log->rdev);
523
f6bed0ef 524 bio_put(bio);
e8deb638 525 mempool_free(io->meta_page, log->meta_pool);
f6bed0ef 526
509ffec7
CH
527 spin_lock_irqsave(&log->io_list_lock, flags);
528 __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
56fef7c6 529 if (log->need_cache_flush)
3848c0bc 530 r5l_move_to_end_ios(log);
56fef7c6
CH
531 else
532 r5l_log_run_stripes(log);
3bddb7f8
SL
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
509ffec7
CH
544 spin_unlock_irqrestore(&log->io_list_lock, flags);
545
56fef7c6
CH
546 if (log->need_cache_flush)
547 md_wakeup_thread(log->rdev->mddev->thread);
3bddb7f8
SL
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
562static 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 */
587static 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);
f6bed0ef
SL
606}
607
608static void r5l_submit_current_io(struct r5l_log *log)
609{
610 struct r5l_io_unit *io = log->current_io;
3bddb7f8 611 struct bio *bio;
f6bed0ef 612 struct r5l_meta_block *block;
509ffec7 613 unsigned long flags;
f6bed0ef 614 u32 crc;
3bddb7f8 615 bool do_submit = true;
f6bed0ef
SL
616
617 if (!io)
618 return;
619
620 block = page_address(io->meta_page);
621 block->meta_size = cpu_to_le32(io->meta_offset);
5cb2fbd6 622 crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
f6bed0ef 623 block->checksum = cpu_to_le32(crc);
3bddb7f8 624 bio = io->current_bio;
f6bed0ef
SL
625
626 log->current_io = NULL;
509ffec7 627 spin_lock_irqsave(&log->io_list_lock, flags);
3bddb7f8
SL
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 }
509ffec7 635 spin_unlock_irqrestore(&log->io_list_lock, flags);
3bddb7f8
SL
636 if (do_submit)
637 r5l_do_submit_io(log, io);
f6bed0ef
SL
638}
639
6143e2ce 640static struct bio *r5l_bio_alloc(struct r5l_log *log)
b349feb3 641{
c38d29b3 642 struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
b349feb3 643
796a5cf0 644 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
b349feb3 645 bio->bi_bdev = log->rdev->bdev;
1e932a37 646 bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
b349feb3 647
b349feb3
CH
648 return bio;
649}
650
c1b99198
CH
651static 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
a39f7afd 655 r5c_update_log_state(log);
c1b99198
CH
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)
6143e2ce 664 io->need_split_bio = true;
c1b99198
CH
665
666 io->log_end = log->log_start;
667}
668
f6bed0ef
SL
669static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
670{
671 struct r5l_io_unit *io;
672 struct r5l_meta_block *block;
f6bed0ef 673
5036c390
CH
674 io = mempool_alloc(log->io_pool, GFP_ATOMIC);
675 if (!io)
676 return NULL;
677 memset(io, 0, sizeof(*io));
678
51039cd0 679 io->log = log;
51039cd0
CH
680 INIT_LIST_HEAD(&io->log_sibling);
681 INIT_LIST_HEAD(&io->stripe_list);
3bddb7f8 682 bio_list_init(&io->flush_barriers);
51039cd0 683 io->state = IO_UNIT_RUNNING;
f6bed0ef 684
e8deb638 685 io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
f6bed0ef 686 block = page_address(io->meta_page);
e8deb638 687 clear_page(block);
f6bed0ef
SL
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);
2b8ef16e 695 io->seq = log->seq++;
f6bed0ef 696
6143e2ce
CH
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;
b349feb3 700 bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
f6bed0ef 701
c1b99198 702 r5_reserve_log_entry(log, io);
f6bed0ef
SL
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
711static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
712{
22581f58
CH
713 if (log->current_io &&
714 log->current_io->meta_offset + payload_size > PAGE_SIZE)
f6bed0ef 715 r5l_submit_current_io(log);
f6bed0ef 716
5036c390 717 if (!log->current_io) {
22581f58 718 log->current_io = r5l_new_meta(log);
5036c390
CH
719 if (!log->current_io)
720 return -ENOMEM;
721 }
722
f6bed0ef
SL
723 return 0;
724}
725
726static 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
748static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
749{
750 struct r5l_io_unit *io = log->current_io;
751
6143e2ce 752 if (io->need_split_bio) {
3bddb7f8
SL
753 BUG_ON(io->split_bio);
754 io->split_bio = io->current_bio;
6143e2ce 755 io->current_bio = r5l_bio_alloc(log);
3bddb7f8
SL
756 bio_chain(io->current_bio, io->split_bio);
757 io->need_split_bio = false;
f6bed0ef 758 }
f6bed0ef 759
6143e2ce
CH
760 if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
761 BUG();
762
c1b99198 763 r5_reserve_log_entry(log, io);
f6bed0ef
SL
764}
765
5036c390 766static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
f6bed0ef
SL
767 int data_pages, int parity_pages)
768{
769 int i;
770 int meta_size;
5036c390 771 int ret;
f6bed0ef
SL
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
5036c390
CH
780 ret = r5l_get_meta(log, meta_size);
781 if (ret)
782 return ret;
783
f6bed0ef
SL
784 io = log->current_io;
785
3bddb7f8
SL
786 if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state))
787 io->has_flush = 1;
788
f6bed0ef 789 for (i = 0; i < sh->disks; i++) {
1e6d690b
SL
790 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
791 test_bit(R5_InJournal, &sh->dev[i].flags))
f6bed0ef
SL
792 continue;
793 if (i == sh->pd_idx || i == sh->qd_idx)
794 continue;
3bddb7f8
SL
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 }
f6bed0ef
SL
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
2ded3703 810 if (parity_pages == 2) {
f6bed0ef
SL
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);
2ded3703 816 } else if (parity_pages == 1) {
f6bed0ef
SL
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);
2ded3703
SL
821 } else /* Just writing data, not parity, in caching phase */
822 BUG_ON(parity_pages != 0);
f6bed0ef
SL
823
824 list_add_tail(&sh->log_list, &io->stripe_list);
825 atomic_inc(&io->pending_stripe);
826 sh->log_io = io;
5036c390 827
a39f7afd
SL
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 }
5036c390 840 return 0;
f6bed0ef
SL
841}
842
a39f7afd
SL
843/* add stripe to no_space_stripes, and then wake up reclaim */
844static 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
f6bed0ef
SL
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 */
856int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
857{
a39f7afd 858 struct r5conf *conf = sh->raid_conf;
f6bed0ef
SL
859 int write_disks = 0;
860 int data_pages, parity_pages;
f6bed0ef
SL
861 int reserve;
862 int i;
5036c390 863 int ret = 0;
a39f7afd 864 bool wake_reclaim = false;
f6bed0ef
SL
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
2ded3703
SL
876 WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
877
f6bed0ef
SL
878 for (i = 0; i < sh->disks; i++) {
879 void *addr;
880
1e6d690b
SL
881 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
882 test_bit(R5_InJournal, &sh->dev[i].flags))
f6bed0ef 883 continue;
1e6d690b 884
f6bed0ef
SL
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);
5cb2fbd6
SL
890 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
891 addr, PAGE_SIZE);
f6bed0ef
SL
892 kunmap_atomic(addr);
893 }
894 parity_pages = 1 + !!(sh->qd_idx >= 0);
895 data_pages = write_disks - parity_pages;
896
f6bed0ef 897 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
253f9fd4
SL
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);
f6bed0ef
SL
903 atomic_inc(&sh->count);
904
905 mutex_lock(&log->io_mutex);
906 /* meta + data */
907 reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
f6bed0ef 908
a39f7afd
SL
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 }
5036c390 945 }
f6bed0ef 946 }
f6bed0ef 947
5036c390 948 mutex_unlock(&log->io_mutex);
a39f7afd
SL
949 if (wake_reclaim)
950 r5l_wake_reclaim(log, reserve);
f6bed0ef
SL
951 return 0;
952}
953
954void 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
828cbe98
SL
963int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
964{
965 if (!log)
966 return -ENODEV;
3bddb7f8
SL
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 }
828cbe98 994 }
828cbe98
SL
995 return -EAGAIN;
996}
997
f6bed0ef
SL
998/* This will run after log space is reclaimed */
999static 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
a39f7afd
SL
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 */
1019static 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 */
d3014e21 1032 spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
a39f7afd
SL
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
17036461
CH
1042static sector_t r5l_reclaimable_space(struct r5l_log *log)
1043{
a39f7afd
SL
1044 struct r5conf *conf = log->rdev->mddev->private;
1045
17036461 1046 return r5l_ring_distance(log, log->last_checkpoint,
a39f7afd 1047 r5c_calculate_new_cp(conf));
17036461
CH
1048}
1049
5036c390
CH
1050static 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
04732f74 1065static bool r5l_complete_finished_ios(struct r5l_log *log)
17036461
CH
1066{
1067 struct r5l_io_unit *io, *next;
1068 bool found = false;
1069
1070 assert_spin_locked(&log->io_list_lock);
1071
04732f74 1072 list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
17036461
CH
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);
5036c390
CH
1081 mempool_free(io, log->io_pool);
1082 r5l_run_no_mem_stripe(log);
17036461
CH
1083
1084 found = true;
1085 }
1086
1087 return found;
1088}
1089
509ffec7
CH
1090static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
1091{
1092 struct r5l_log *log = io->log;
a39f7afd 1093 struct r5conf *conf = log->rdev->mddev->private;
509ffec7
CH
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);
17036461 1098
04732f74 1099 if (!r5l_complete_finished_ios(log)) {
85f2f9a4
SL
1100 spin_unlock_irqrestore(&log->io_list_lock, flags);
1101 return;
1102 }
509ffec7 1103
a39f7afd
SL
1104 if (r5l_reclaimable_space(log) > log->max_free_space ||
1105 test_bit(R5C_LOG_TIGHT, &conf->cache_state))
509ffec7
CH
1106 r5l_wake_reclaim(log, 0);
1107
509ffec7
CH
1108 spin_unlock_irqrestore(&log->io_list_lock, flags);
1109 wake_up(&log->iounit_wait);
1110}
1111
0576b1c6
SL
1112void r5l_stripe_write_finished(struct stripe_head *sh)
1113{
1114 struct r5l_io_unit *io;
1115
0576b1c6 1116 io = sh->log_io;
0576b1c6
SL
1117 sh->log_io = NULL;
1118
509ffec7
CH
1119 if (io && atomic_dec_and_test(&io->pending_stripe))
1120 __r5l_stripe_write_finished(io);
0576b1c6
SL
1121}
1122
a8c34f91
SL
1123static 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;
a8c34f91 1129
6e74a9cf
SL
1130 if (bio->bi_error)
1131 md_error(log->rdev->mddev, log->rdev);
1132
a8c34f91 1133 spin_lock_irqsave(&log->io_list_lock, flags);
d8858f43
CH
1134 list_for_each_entry(io, &log->flushing_ios, log_sibling)
1135 r5l_io_run_stripes(io);
04732f74 1136 list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
a8c34f91
SL
1137 spin_unlock_irqrestore(&log->io_list_lock, flags);
1138}
1139
0576b1c6
SL
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 */
1154void r5l_flush_stripe_to_raid(struct r5l_log *log)
1155{
a8c34f91 1156 bool do_flush;
56fef7c6
CH
1157
1158 if (!log || !log->need_cache_flush)
0576b1c6 1159 return;
0576b1c6
SL
1160
1161 spin_lock_irq(&log->io_list_lock);
a8c34f91
SL
1162 /* flush bio is running */
1163 if (!list_empty(&log->flushing_ios)) {
1164 spin_unlock_irq(&log->io_list_lock);
1165 return;
0576b1c6 1166 }
a8c34f91
SL
1167 list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
1168 do_flush = !list_empty(&log->flushing_ios);
0576b1c6 1169 spin_unlock_irq(&log->io_list_lock);
a8c34f91
SL
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;
796a5cf0 1176 bio_set_op_attrs(&log->flush_bio, REQ_OP_WRITE, WRITE_FLUSH);
4e49ea4a 1177 submit_bio(&log->flush_bio);
0576b1c6
SL
1178}
1179
0576b1c6 1180static void r5l_write_super(struct r5l_log *log, sector_t cp);
4b482044
SL
1181static 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 /*
8e018c21
SL
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
4b482044 1203 */
8e018c21
SL
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);
4b482044 1210
6e74a9cf 1211 /* discard IO error really doesn't matter, ignore it */
4b482044
SL
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
a39f7afd
SL
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 */
1232static 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 */
1263void 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
1288static 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
0576b1c6
SL
1355static void r5l_do_reclaim(struct r5l_log *log)
1356{
a39f7afd 1357 struct r5conf *conf = log->rdev->mddev->private;
0576b1c6 1358 sector_t reclaim_target = xchg(&log->reclaim_target, 0);
17036461
CH
1359 sector_t reclaimable;
1360 sector_t next_checkpoint;
a39f7afd 1361 bool write_super;
0576b1c6
SL
1362
1363 spin_lock_irq(&log->io_list_lock);
a39f7afd
SL
1364 write_super = r5l_reclaimable_space(log) > log->max_free_space ||
1365 reclaim_target != 0 || !list_empty(&log->no_space_stripes);
0576b1c6
SL
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) {
17036461
CH
1372 reclaimable = r5l_reclaimable_space(log);
1373 if (reclaimable >= reclaim_target ||
0576b1c6
SL
1374 (list_empty(&log->running_ios) &&
1375 list_empty(&log->io_end_ios) &&
a8c34f91 1376 list_empty(&log->flushing_ios) &&
04732f74 1377 list_empty(&log->finished_ios)))
0576b1c6
SL
1378 break;
1379
17036461
CH
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);
0576b1c6 1384 }
17036461 1385
a39f7afd 1386 next_checkpoint = r5c_calculate_new_cp(conf);
0576b1c6
SL
1387 spin_unlock_irq(&log->io_list_lock);
1388
17036461 1389 BUG_ON(reclaimable < 0);
a39f7afd
SL
1390
1391 if (reclaimable == 0 || !write_super)
0576b1c6
SL
1392 return;
1393
0576b1c6
SL
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 */
4b482044 1399 r5l_write_super_and_discard_space(log, next_checkpoint);
0576b1c6
SL
1400
1401 mutex_lock(&log->io_mutex);
17036461 1402 log->last_checkpoint = next_checkpoint;
a39f7afd 1403 r5c_update_log_state(log);
0576b1c6 1404 mutex_unlock(&log->io_mutex);
0576b1c6 1405
17036461 1406 r5l_run_no_space_stripes(log);
0576b1c6
SL
1407}
1408
1409static 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;
a39f7afd 1417 r5c_do_reclaim(conf);
0576b1c6
SL
1418 r5l_do_reclaim(log);
1419}
1420
a39f7afd 1421void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
f6bed0ef 1422{
0576b1c6
SL
1423 unsigned long target;
1424 unsigned long new = (unsigned long)space; /* overflow in theory */
1425
a39f7afd
SL
1426 if (!log)
1427 return;
0576b1c6
SL
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);
f6bed0ef
SL
1434}
1435
e6c033f7
SL
1436void r5l_quiesce(struct r5l_log *log, int state)
1437{
4b482044 1438 struct mddev *mddev;
e6c033f7
SL
1439 if (!log || state == 2)
1440 return;
ce1ccd07
SL
1441 if (state == 0)
1442 kthread_unpark(log->reclaim_thread->tsk);
1443 else if (state == 1) {
4b482044
SL
1444 /* make sure r5l_write_super_and_discard_space exits */
1445 mddev = log->rdev->mddev;
1446 wake_up(&mddev->sb_wait);
ce1ccd07 1447 kthread_park(log->reclaim_thread->tsk);
a39f7afd 1448 r5l_wake_reclaim(log, MaxSector);
e6c033f7
SL
1449 r5l_do_reclaim(log);
1450 }
1451}
1452
6e74a9cf
SL
1453bool r5l_log_disk_error(struct r5conf *conf)
1454{
f6b6ec5c
SL
1455 struct r5l_log *log;
1456 bool ret;
7dde2ad3 1457 /* don't allow write if journal disk is missing */
f6b6ec5c
SL
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;
6e74a9cf
SL
1467}
1468
355810d1
SL
1469struct 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 */
b4c625c6
SL
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;
355810d1
SL
1477};
1478
9ed988f5
SL
1479static int r5l_recovery_read_meta_block(struct r5l_log *log,
1480 struct r5l_recovery_ctx *ctx)
355810d1
SL
1481{
1482 struct page *page = ctx->meta_page;
1483 struct r5l_meta_block *mb;
1484 u32 crc, stored_crc;
1485
796a5cf0
MC
1486 if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, REQ_OP_READ, 0,
1487 false))
355810d1
SL
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
5cb2fbd6 1500 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
355810d1
SL
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
9ed988f5
SL
1512static void
1513r5l_recovery_create_empty_meta_block(struct r5l_log *log,
1514 struct page *page,
1515 sector_t pos, u64 seq)
355810d1 1516{
355810d1
SL
1517 struct r5l_meta_block *mb;
1518 u32 crc;
1519
355810d1 1520 mb = page_address(page);
9ed988f5 1521 clear_page(mb);
355810d1
SL
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);
5cb2fbd6 1527 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
355810d1 1528 mb->checksum = cpu_to_le32(crc);
9ed988f5 1529}
355810d1 1530
9ed988f5
SL
1531static 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);
796a5cf0
MC
1540 if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE,
1541 WRITE_FUA, false)) {
355810d1
SL
1542 __free_page(page);
1543 return -EIO;
1544 }
1545 __free_page(page);
1546 return 0;
1547}
1548
b4c625c6
SL
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 */
1556static 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
1579static 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
1607static 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
1617static void
1618r5l_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++;
1671out:
1672 r5l_recovery_reset_stripe(sh);
1673}
1674
1675static struct stripe_head *
1676r5c_recovery_alloc_stripe(struct r5conf *conf,
b4c625c6
SL
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
1692static struct stripe_head *
1693r5c_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
1703static void
1704r5c_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
1716static void
1717r5c_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 */
1731static int
1732r5l_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 */
1750static int
1751r5l_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
1800mismatch:
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 */
1813static int
1814r5c_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) {
9b69173e 1857 sh = r5c_recovery_alloc_stripe(conf, stripe_sect, ctx->pos);
b4c625c6
SL
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(
9b69173e 1866 conf, stripe_sect, ctx->pos);
b4c625c6
SL
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(
9b69173e 1875 conf, stripe_sect, ctx->pos);
b4c625c6
SL
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) {
f7b7bee7
ZL
1886 if (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
1887 test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags)) {
b4c625c6 1888 r5l_recovery_replay_one_stripe(conf, sh, ctx);
b4c625c6
SL
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 */
1915static void r5c_recovery_load_one_stripe(struct r5l_log *log,
1916 struct stripe_head *sh)
1917{
b4c625c6
SL
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 }
b4c625c6 1928 list_add_tail(&sh->r5c, &log->stripe_in_journal_list);
462eb7d8 1929 atomic_inc(&log->stripe_in_journal_count);
b4c625c6
SL
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 */
1948static int r5c_recovery_flush_log(struct r5l_log *log,
1949 struct r5l_recovery_ctx *ctx)
1950{
1951 struct stripe_head *sh, *next;
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_safe(sh, next, &ctx->cached_list, lru) {
1981 WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
1982 r5c_recovery_load_one_stripe(log, sh);
1983 list_del_init(&sh->lru);
1984 raid5_release_stripe(sh);
1985 ctx->data_only_stripes++;
1986 }
1987
1988 return 0;
1989}
1990
1991/*
1992 * we did a recovery. Now ctx.pos points to an invalid meta block. New
1993 * log will start here. but we can't let superblock point to last valid
1994 * meta block. The log might looks like:
1995 * | meta 1| meta 2| meta 3|
1996 * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
1997 * superblock points to meta 1, we write a new valid meta 2n. if crash
1998 * happens again, new recovery will start from meta 1. Since meta 2n is
1999 * valid now, recovery will think meta 3 is valid, which is wrong.
2000 * The solution is we create a new meta in meta2 with its seq == meta
2001 * 1's seq + 10 and let superblock points to meta2. The same recovery will
2002 * not think meta 3 is a valid meta, because its seq doesn't match
2003 */
2004
2005/*
2006 * Before recovery, the log looks like the following
2007 *
2008 * ---------------------------------------------
2009 * | valid log | invalid log |
2010 * ---------------------------------------------
2011 * ^
2012 * |- log->last_checkpoint
2013 * |- log->last_cp_seq
2014 *
2015 * Now we scan through the log until we see invalid entry
2016 *
2017 * ---------------------------------------------
2018 * | valid log | invalid log |
2019 * ---------------------------------------------
2020 * ^ ^
2021 * |- log->last_checkpoint |- ctx->pos
2022 * |- log->last_cp_seq |- ctx->seq
2023 *
2024 * From this point, we need to increase seq number by 10 to avoid
2025 * confusing next recovery.
2026 *
2027 * ---------------------------------------------
2028 * | valid log | invalid log |
2029 * ---------------------------------------------
2030 * ^ ^
2031 * |- log->last_checkpoint |- ctx->pos+1
2032 * |- log->last_cp_seq |- ctx->seq+11
2033 *
2034 * However, it is not safe to start the state machine yet, because data only
2035 * parities are not yet secured in RAID. To save these data only parities, we
2036 * rewrite them from seq+11.
2037 *
2038 * -----------------------------------------------------------------
2039 * | valid log | data only stripes | invalid log |
2040 * -----------------------------------------------------------------
2041 * ^ ^
2042 * |- log->last_checkpoint |- ctx->pos+n
2043 * |- log->last_cp_seq |- ctx->seq+10+n
2044 *
2045 * If failure happens again during this process, the recovery can safe start
2046 * again from log->last_checkpoint.
2047 *
2048 * Once data only stripes are rewritten to journal, we move log_tail
2049 *
2050 * -----------------------------------------------------------------
2051 * | old log | data only stripes | invalid log |
2052 * -----------------------------------------------------------------
2053 * ^ ^
2054 * |- log->last_checkpoint |- ctx->pos+n
2055 * |- log->last_cp_seq |- ctx->seq+10+n
2056 *
2057 * Then we can safely start the state machine. If failure happens from this
2058 * point on, the recovery will start from new log->last_checkpoint.
2059 */
2060static int
2061r5c_recovery_rewrite_data_only_stripes(struct r5l_log *log,
2062 struct r5l_recovery_ctx *ctx)
2063{
2064 struct stripe_head *sh;
2065 struct mddev *mddev = log->rdev->mddev;
2066 struct page *page;
2067
2068 page = alloc_page(GFP_KERNEL);
2069 if (!page) {
2070 pr_err("md/raid:%s: cannot allocate memory to rewrite data only stripes\n",
2071 mdname(mddev));
2072 return -ENOMEM;
2073 }
2074
2075 ctx->seq += 10;
2076 list_for_each_entry(sh, &ctx->cached_list, lru) {
2077 struct r5l_meta_block *mb;
2078 int i;
2079 int offset;
2080 sector_t write_pos;
2081
2082 WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
2083 r5l_recovery_create_empty_meta_block(log, page,
2084 ctx->pos, ctx->seq);
2085 mb = page_address(page);
2086 offset = le32_to_cpu(mb->meta_size);
fc833c2a 2087 write_pos = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
b4c625c6
SL
2088
2089 for (i = sh->disks; i--; ) {
2090 struct r5dev *dev = &sh->dev[i];
2091 struct r5l_payload_data_parity *payload;
2092 void *addr;
2093
2094 if (test_bit(R5_InJournal, &dev->flags)) {
2095 payload = (void *)mb + offset;
2096 payload->header.type = cpu_to_le16(
2097 R5LOG_PAYLOAD_DATA);
2098 payload->size = BLOCK_SECTORS;
2099 payload->location = cpu_to_le64(
2100 raid5_compute_blocknr(sh, i, 0));
2101 addr = kmap_atomic(dev->page);
2102 payload->checksum[0] = cpu_to_le32(
2103 crc32c_le(log->uuid_checksum, addr,
2104 PAGE_SIZE));
2105 kunmap_atomic(addr);
2106 sync_page_io(log->rdev, write_pos, PAGE_SIZE,
2107 dev->page, REQ_OP_WRITE, 0, false);
2108 write_pos = r5l_ring_add(log, write_pos,
2109 BLOCK_SECTORS);
2110 offset += sizeof(__le32) +
2111 sizeof(struct r5l_payload_data_parity);
2112
2113 }
2114 }
2115 mb->meta_size = cpu_to_le32(offset);
2116 mb->checksum = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
2117 sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page,
2118 REQ_OP_WRITE, WRITE_FUA, false);
2119 sh->log_start = ctx->pos;
2120 ctx->pos = write_pos;
2121 ctx->seq += 1;
2122 }
2123 __free_page(page);
2124 return 0;
2125}
2126
f6bed0ef
SL
2127static int r5l_recovery_log(struct r5l_log *log)
2128{
5aabf7c4 2129 struct mddev *mddev = log->rdev->mddev;
355810d1 2130 struct r5l_recovery_ctx ctx;
5aabf7c4 2131 int ret;
355810d1
SL
2132
2133 ctx.pos = log->last_checkpoint;
2134 ctx.seq = log->last_cp_seq;
2135 ctx.meta_page = alloc_page(GFP_KERNEL);
b4c625c6
SL
2136 ctx.data_only_stripes = 0;
2137 ctx.data_parity_stripes = 0;
2138 INIT_LIST_HEAD(&ctx.cached_list);
2139
355810d1
SL
2140 if (!ctx.meta_page)
2141 return -ENOMEM;
2142
5aabf7c4 2143 ret = r5c_recovery_flush_log(log, &ctx);
355810d1
SL
2144 __free_page(ctx.meta_page);
2145
5aabf7c4
SL
2146 if (ret)
2147 return ret;
b4c625c6 2148
5aabf7c4
SL
2149 if ((ctx.data_only_stripes == 0) && (ctx.data_parity_stripes == 0))
2150 pr_debug("md/raid:%s: starting from clean shutdown\n",
2151 mdname(mddev));
2152 else {
2153 pr_debug("md/raid:%s: recoverying %d data-only stripes and %d data-parity stripes\n",
2154 mdname(mddev), ctx.data_only_stripes,
2155 ctx.data_parity_stripes);
2156
2157 if (ctx.data_only_stripes > 0)
2158 if (r5c_recovery_rewrite_data_only_stripes(log, &ctx)) {
2159 pr_err("md/raid:%s: failed to rewrite stripes to journal\n",
2160 mdname(mddev));
2161 return -EIO;
2162 }
b4c625c6
SL
2163 }
2164
5aabf7c4
SL
2165 log->log_start = ctx.pos;
2166 log->next_checkpoint = ctx.pos;
2167 log->seq = ctx.seq;
2168 r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq);
2169 r5l_write_super(log, ctx.pos);
f6bed0ef
SL
2170 return 0;
2171}
2172
2173static void r5l_write_super(struct r5l_log *log, sector_t cp)
2174{
2175 struct mddev *mddev = log->rdev->mddev;
2176
2177 log->rdev->journal_tail = cp;
2178 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2179}
2180
2c7da14b
SL
2181static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page)
2182{
2183 struct r5conf *conf = mddev->private;
2184 int ret;
2185
2186 if (!conf->log)
2187 return 0;
2188
2189 switch (conf->log->r5c_journal_mode) {
2190 case R5C_JOURNAL_MODE_WRITE_THROUGH:
2191 ret = snprintf(
2192 page, PAGE_SIZE, "[%s] %s\n",
2193 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
2194 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
2195 break;
2196 case R5C_JOURNAL_MODE_WRITE_BACK:
2197 ret = snprintf(
2198 page, PAGE_SIZE, "%s [%s]\n",
2199 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
2200 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
2201 break;
2202 default:
2203 ret = 0;
2204 }
2205 return ret;
2206}
2207
2208static ssize_t r5c_journal_mode_store(struct mddev *mddev,
2209 const char *page, size_t length)
2210{
2211 struct r5conf *conf = mddev->private;
2212 struct r5l_log *log = conf->log;
2213 int val = -1, i;
2214 int len = length;
2215
2216 if (!log)
2217 return -ENODEV;
2218
2219 if (len && page[len - 1] == '\n')
2220 len -= 1;
2221 for (i = 0; i < ARRAY_SIZE(r5c_journal_mode_str); i++)
2222 if (strlen(r5c_journal_mode_str[i]) == len &&
2223 strncmp(page, r5c_journal_mode_str[i], len) == 0) {
2224 val = i;
2225 break;
2226 }
2227 if (val < R5C_JOURNAL_MODE_WRITE_THROUGH ||
2228 val > R5C_JOURNAL_MODE_WRITE_BACK)
2229 return -EINVAL;
2230
2231 mddev_suspend(mddev);
2232 conf->log->r5c_journal_mode = val;
2233 mddev_resume(mddev);
2234
2235 pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n",
2236 mdname(mddev), val, r5c_journal_mode_str[val]);
2237 return length;
2238}
2239
2240struct md_sysfs_entry
2241r5c_journal_mode = __ATTR(journal_mode, 0644,
2242 r5c_journal_mode_show, r5c_journal_mode_store);
2243
2ded3703
SL
2244/*
2245 * Try handle write operation in caching phase. This function should only
2246 * be called in write-back mode.
2247 *
2248 * If all outstanding writes can be handled in caching phase, returns 0
2249 * If writes requires write-out phase, call r5c_make_stripe_write_out()
2250 * and returns -EAGAIN
2251 */
2252int r5c_try_caching_write(struct r5conf *conf,
2253 struct stripe_head *sh,
2254 struct stripe_head_state *s,
2255 int disks)
2256{
2257 struct r5l_log *log = conf->log;
1e6d690b
SL
2258 int i;
2259 struct r5dev *dev;
2260 int to_cache = 0;
2ded3703
SL
2261
2262 BUG_ON(!r5c_is_writeback(log));
2263
1e6d690b
SL
2264 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
2265 /*
2266 * There are two different scenarios here:
2267 * 1. The stripe has some data cached, and it is sent to
2268 * write-out phase for reclaim
2269 * 2. The stripe is clean, and this is the first write
2270 *
2271 * For 1, return -EAGAIN, so we continue with
2272 * handle_stripe_dirtying().
2273 *
2274 * For 2, set STRIPE_R5C_CACHING and continue with caching
2275 * write.
2276 */
2277
2278 /* case 1: anything injournal or anything in written */
2279 if (s->injournal > 0 || s->written > 0)
2280 return -EAGAIN;
2281 /* case 2 */
2282 set_bit(STRIPE_R5C_CACHING, &sh->state);
2283 }
2284
2285 for (i = disks; i--; ) {
2286 dev = &sh->dev[i];
2287 /* if non-overwrite, use writing-out phase */
2288 if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) &&
2289 !test_bit(R5_InJournal, &dev->flags)) {
2290 r5c_make_stripe_write_out(sh);
2291 return -EAGAIN;
2292 }
2293 }
2294
2295 for (i = disks; i--; ) {
2296 dev = &sh->dev[i];
2297 if (dev->towrite) {
2298 set_bit(R5_Wantwrite, &dev->flags);
2299 set_bit(R5_Wantdrain, &dev->flags);
2300 set_bit(R5_LOCKED, &dev->flags);
2301 to_cache++;
2302 }
2303 }
2304
2305 if (to_cache) {
2306 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2307 /*
2308 * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data()
2309 * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in
2310 * r5c_handle_data_cached()
2311 */
2312 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
2313 }
2314
2315 return 0;
2316}
2317
2318/*
2319 * free extra pages (orig_page) we allocated for prexor
2320 */
2321void r5c_release_extra_page(struct stripe_head *sh)
2322{
d7bd398e 2323 struct r5conf *conf = sh->raid_conf;
1e6d690b 2324 int i;
d7bd398e
SL
2325 bool using_disk_info_extra_page;
2326
2327 using_disk_info_extra_page =
2328 sh->dev[0].orig_page == conf->disks[0].extra_page;
1e6d690b
SL
2329
2330 for (i = sh->disks; i--; )
2331 if (sh->dev[i].page != sh->dev[i].orig_page) {
2332 struct page *p = sh->dev[i].orig_page;
2333
2334 sh->dev[i].orig_page = sh->dev[i].page;
d7bd398e
SL
2335 if (!using_disk_info_extra_page)
2336 put_page(p);
1e6d690b 2337 }
d7bd398e
SL
2338
2339 if (using_disk_info_extra_page) {
2340 clear_bit(R5C_EXTRA_PAGE_IN_USE, &conf->cache_state);
2341 md_wakeup_thread(conf->mddev->thread);
2342 }
2343}
2344
2345void r5c_use_extra_page(struct stripe_head *sh)
2346{
2347 struct r5conf *conf = sh->raid_conf;
2348 int i;
2349 struct r5dev *dev;
2350
2351 for (i = sh->disks; i--; ) {
2352 dev = &sh->dev[i];
2353 if (dev->orig_page != dev->page)
2354 put_page(dev->orig_page);
2355 dev->orig_page = conf->disks[i].extra_page;
2356 }
2ded3703
SL
2357}
2358
2359/*
2360 * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the
2361 * stripe is committed to RAID disks.
2362 */
2363void r5c_finish_stripe_write_out(struct r5conf *conf,
2364 struct stripe_head *sh,
2365 struct stripe_head_state *s)
2366{
1e6d690b
SL
2367 int i;
2368 int do_wakeup = 0;
2369
2ded3703
SL
2370 if (!conf->log ||
2371 !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags))
2372 return;
2373
2374 WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
2375 clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
2376
2377 if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
2378 return;
1e6d690b
SL
2379
2380 for (i = sh->disks; i--; ) {
2381 clear_bit(R5_InJournal, &sh->dev[i].flags);
2382 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2383 do_wakeup = 1;
2384 }
2385
2386 /*
2387 * analyse_stripe() runs before r5c_finish_stripe_write_out(),
2388 * We updated R5_InJournal, so we also update s->injournal.
2389 */
2390 s->injournal = 0;
2391
2392 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2393 if (atomic_dec_and_test(&conf->pending_full_writes))
2394 md_wakeup_thread(conf->mddev->thread);
2395
2396 if (do_wakeup)
2397 wake_up(&conf->wait_for_overlap);
a39f7afd
SL
2398
2399 if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
2400 return;
2401
2402 spin_lock_irq(&conf->log->stripe_in_journal_lock);
2403 list_del_init(&sh->r5c);
2404 spin_unlock_irq(&conf->log->stripe_in_journal_lock);
2405 sh->log_start = MaxSector;
2406 atomic_dec(&conf->log->stripe_in_journal_count);
1e6d690b
SL
2407}
2408
2409int
2410r5c_cache_data(struct r5l_log *log, struct stripe_head *sh,
2411 struct stripe_head_state *s)
2412{
a39f7afd 2413 struct r5conf *conf = sh->raid_conf;
1e6d690b
SL
2414 int pages = 0;
2415 int reserve;
2416 int i;
2417 int ret = 0;
2418
2419 BUG_ON(!log);
2420
2421 for (i = 0; i < sh->disks; i++) {
2422 void *addr;
2423
2424 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
2425 continue;
2426 addr = kmap_atomic(sh->dev[i].page);
2427 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
2428 addr, PAGE_SIZE);
2429 kunmap_atomic(addr);
2430 pages++;
2431 }
2432 WARN_ON(pages == 0);
2433
2434 /*
2435 * The stripe must enter state machine again to call endio, so
2436 * don't delay.
2437 */
2438 clear_bit(STRIPE_DELAYED, &sh->state);
2439 atomic_inc(&sh->count);
2440
2441 mutex_lock(&log->io_mutex);
2442 /* meta + data */
2443 reserve = (1 + pages) << (PAGE_SHIFT - 9);
1e6d690b 2444
a39f7afd
SL
2445 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
2446 sh->log_start == MaxSector)
2447 r5l_add_no_space_stripe(log, sh);
2448 else if (!r5l_has_free_space(log, reserve)) {
2449 if (sh->log_start == log->last_checkpoint)
2450 BUG();
2451 else
2452 r5l_add_no_space_stripe(log, sh);
1e6d690b
SL
2453 } else {
2454 ret = r5l_log_stripe(log, sh, pages, 0);
2455 if (ret) {
2456 spin_lock_irq(&log->io_list_lock);
2457 list_add_tail(&sh->log_list, &log->no_mem_stripes);
2458 spin_unlock_irq(&log->io_list_lock);
2459 }
2460 }
2461
2462 mutex_unlock(&log->io_mutex);
2463 return 0;
2ded3703
SL
2464}
2465
f6bed0ef
SL
2466static int r5l_load_log(struct r5l_log *log)
2467{
2468 struct md_rdev *rdev = log->rdev;
2469 struct page *page;
2470 struct r5l_meta_block *mb;
2471 sector_t cp = log->rdev->journal_tail;
2472 u32 stored_crc, expected_crc;
2473 bool create_super = false;
2474 int ret;
2475
2476 /* Make sure it's valid */
2477 if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
2478 cp = 0;
2479 page = alloc_page(GFP_KERNEL);
2480 if (!page)
2481 return -ENOMEM;
2482
796a5cf0 2483 if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) {
f6bed0ef
SL
2484 ret = -EIO;
2485 goto ioerr;
2486 }
2487 mb = page_address(page);
2488
2489 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
2490 mb->version != R5LOG_VERSION) {
2491 create_super = true;
2492 goto create;
2493 }
2494 stored_crc = le32_to_cpu(mb->checksum);
2495 mb->checksum = 0;
5cb2fbd6 2496 expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
f6bed0ef
SL
2497 if (stored_crc != expected_crc) {
2498 create_super = true;
2499 goto create;
2500 }
2501 if (le64_to_cpu(mb->position) != cp) {
2502 create_super = true;
2503 goto create;
2504 }
2505create:
2506 if (create_super) {
2507 log->last_cp_seq = prandom_u32();
2508 cp = 0;
56056c2e 2509 r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq);
f6bed0ef
SL
2510 /*
2511 * Make sure super points to correct address. Log might have
2512 * data very soon. If super hasn't correct log tail address,
2513 * recovery can't find the log
2514 */
2515 r5l_write_super(log, cp);
2516 } else
2517 log->last_cp_seq = le64_to_cpu(mb->seq);
2518
2519 log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
0576b1c6
SL
2520 log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
2521 if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
2522 log->max_free_space = RECLAIM_MAX_FREE_SPACE;
f6bed0ef 2523 log->last_checkpoint = cp;
28cd88e2 2524 log->next_checkpoint = cp;
a39f7afd
SL
2525 mutex_lock(&log->io_mutex);
2526 r5c_update_log_state(log);
2527 mutex_unlock(&log->io_mutex);
f6bed0ef
SL
2528
2529 __free_page(page);
2530
2531 return r5l_recovery_log(log);
2532ioerr:
2533 __free_page(page);
2534 return ret;
2535}
2536
2537int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
2538{
c888a8f9 2539 struct request_queue *q = bdev_get_queue(rdev->bdev);
f6bed0ef
SL
2540 struct r5l_log *log;
2541
2542 if (PAGE_SIZE != 4096)
2543 return -EINVAL;
c757ec95
SL
2544
2545 /*
2546 * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and
2547 * raid_disks r5l_payload_data_parity.
2548 *
2549 * Write journal and cache does not work for very big array
2550 * (raid_disks > 203)
2551 */
2552 if (sizeof(struct r5l_meta_block) +
2553 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) *
2554 conf->raid_disks) > PAGE_SIZE) {
2555 pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n",
2556 mdname(conf->mddev), conf->raid_disks);
2557 return -EINVAL;
2558 }
2559
f6bed0ef
SL
2560 log = kzalloc(sizeof(*log), GFP_KERNEL);
2561 if (!log)
2562 return -ENOMEM;
2563 log->rdev = rdev;
2564
c888a8f9 2565 log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0;
56fef7c6 2566
5cb2fbd6
SL
2567 log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
2568 sizeof(rdev->mddev->uuid));
f6bed0ef
SL
2569
2570 mutex_init(&log->io_mutex);
2571
2572 spin_lock_init(&log->io_list_lock);
2573 INIT_LIST_HEAD(&log->running_ios);
0576b1c6 2574 INIT_LIST_HEAD(&log->io_end_ios);
a8c34f91 2575 INIT_LIST_HEAD(&log->flushing_ios);
04732f74 2576 INIT_LIST_HEAD(&log->finished_ios);
a8c34f91 2577 bio_init(&log->flush_bio);
f6bed0ef
SL
2578
2579 log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
2580 if (!log->io_kc)
2581 goto io_kc;
2582
5036c390
CH
2583 log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc);
2584 if (!log->io_pool)
2585 goto io_pool;
2586
c38d29b3
CH
2587 log->bs = bioset_create(R5L_POOL_SIZE, 0);
2588 if (!log->bs)
2589 goto io_bs;
2590
e8deb638
CH
2591 log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
2592 if (!log->meta_pool)
2593 goto out_mempool;
2594
0576b1c6
SL
2595 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
2596 log->rdev->mddev, "reclaim");
2597 if (!log->reclaim_thread)
2598 goto reclaim_thread;
a39f7afd
SL
2599 log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
2600
0fd22b45 2601 init_waitqueue_head(&log->iounit_wait);
0576b1c6 2602
5036c390
CH
2603 INIT_LIST_HEAD(&log->no_mem_stripes);
2604
f6bed0ef
SL
2605 INIT_LIST_HEAD(&log->no_space_stripes);
2606 spin_lock_init(&log->no_space_stripes_lock);
2607
3bddb7f8
SL
2608 INIT_WORK(&log->deferred_io_work, r5l_submit_io_async);
2609
2ded3703 2610 log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
a39f7afd
SL
2611 INIT_LIST_HEAD(&log->stripe_in_journal_list);
2612 spin_lock_init(&log->stripe_in_journal_lock);
2613 atomic_set(&log->stripe_in_journal_count, 0);
2ded3703 2614
f6bed0ef
SL
2615 if (r5l_load_log(log))
2616 goto error;
2617
f6b6ec5c 2618 rcu_assign_pointer(conf->log, log);
a62ab49e 2619 set_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
f6bed0ef 2620 return 0;
e8deb638 2621
f6bed0ef 2622error:
0576b1c6
SL
2623 md_unregister_thread(&log->reclaim_thread);
2624reclaim_thread:
e8deb638
CH
2625 mempool_destroy(log->meta_pool);
2626out_mempool:
c38d29b3
CH
2627 bioset_free(log->bs);
2628io_bs:
5036c390
CH
2629 mempool_destroy(log->io_pool);
2630io_pool:
f6bed0ef
SL
2631 kmem_cache_destroy(log->io_kc);
2632io_kc:
2633 kfree(log);
2634 return -EINVAL;
2635}
2636
2637void r5l_exit_log(struct r5l_log *log)
2638{
0576b1c6 2639 md_unregister_thread(&log->reclaim_thread);
e8deb638 2640 mempool_destroy(log->meta_pool);
c38d29b3 2641 bioset_free(log->bs);
5036c390 2642 mempool_destroy(log->io_pool);
f6bed0ef
SL
2643 kmem_cache_destroy(log->io_kc);
2644 kfree(log);
2645}