blob: 76c0e5063f1b3d7278697078810261a3c813af91 [file] [log] [blame]
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001/*
2 * Copyright (C) 2015 Shaohua Li <shli@fb.com>
Song Liub4c625c2016-11-17 15:24:43 -08003 * Copyright (C) 2016 Song Liu <songliubraving@fb.com>
Shaohua Lif6bed0e2015-08-13 14:31:59 -07004 *
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>
Shaohua Li5cb2fbd2015-10-28 08:41:25 -070020#include <linux/crc32c.h>
Shaohua Lif6bed0e2015-08-13 14:31:59 -070021#include <linux/random.h>
Shaohua Lice1ccd02016-11-21 10:29:18 -080022#include <linux/kthread.h>
Song Liu03b047f2017-01-11 13:39:14 -080023#include <linux/types.h>
Shaohua Lif6bed0e2015-08-13 14:31:59 -070024#include "md.h"
25#include "raid5.h"
Song Liu1e6d6902016-11-17 15:24:39 -080026#include "bitmap.h"
Shaohua Lif6bed0e2015-08-13 14:31:59 -070027
28/*
29 * metadata/data stored in disk with 4k size unit (a block) regardless
30 * underneath hardware sector size. only works with PAGE_SIZE == 4096
31 */
32#define BLOCK_SECTORS (8)
33
Shaohua Li0576b1c2015-08-13 14:32:00 -070034/*
Song Liua39f7af2016-11-17 15:24:40 -080035 * log->max_free_space is min(1/4 disk size, 10G reclaimable space).
36 *
37 * In write through mode, the reclaim runs every log->max_free_space.
38 * This can prevent the recovery scans for too long
Shaohua Li0576b1c2015-08-13 14:32:00 -070039 */
40#define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
41#define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
42
Song Liua39f7af2016-11-17 15:24:40 -080043/* wake up reclaim thread periodically */
44#define R5C_RECLAIM_WAKEUP_INTERVAL (30 * HZ)
45/* start flush with these full stripes */
46#define R5C_FULL_STRIPE_FLUSH_BATCH 256
47/* reclaim stripes in groups */
48#define R5C_RECLAIM_STRIPE_GROUP (NR_STRIPE_HASH_LOCKS * 2)
49
Christoph Hellwigc38d29b2015-12-21 10:51:02 +110050/*
51 * We only need 2 bios per I/O unit to make progress, but ensure we
52 * have a few more available to not get too tight.
53 */
54#define R5L_POOL_SIZE 4
55
Song Liu2ded3702016-11-17 15:24:38 -080056/*
57 * r5c journal modes of the array: write-back or write-through.
58 * write-through mode has identical behavior as existing log only
59 * implementation.
60 */
61enum r5c_journal_mode {
62 R5C_JOURNAL_MODE_WRITE_THROUGH = 0,
63 R5C_JOURNAL_MODE_WRITE_BACK = 1,
64};
65
Song Liu2c7da142016-11-17 15:24:41 -080066static char *r5c_journal_mode_str[] = {"write-through",
67 "write-back"};
Song Liu2ded3702016-11-17 15:24:38 -080068/*
69 * raid5 cache state machine
70 *
JackieLiu9b691732016-11-28 16:19:18 +080071 * With the RAID cache, each stripe works in two phases:
Song Liu2ded3702016-11-17 15:24:38 -080072 * - caching phase
73 * - writing-out phase
74 *
75 * These two phases are controlled by bit STRIPE_R5C_CACHING:
76 * if STRIPE_R5C_CACHING == 0, the stripe is in writing-out phase
77 * if STRIPE_R5C_CACHING == 1, the stripe is in caching phase
78 *
79 * When there is no journal, or the journal is in write-through mode,
80 * the stripe is always in writing-out phase.
81 *
82 * For write-back journal, the stripe is sent to caching phase on write
83 * (r5c_try_caching_write). r5c_make_stripe_write_out() kicks off
84 * the write-out phase by clearing STRIPE_R5C_CACHING.
85 *
86 * Stripes in caching phase do not write the raid disks. Instead, all
87 * writes are committed from the log device. Therefore, a stripe in
88 * caching phase handles writes as:
89 * - write to log device
90 * - return IO
91 *
92 * Stripes in writing-out phase handle writes as:
93 * - calculate parity
94 * - write pending data and parity to journal
95 * - write data and parity to raid disks
96 * - return IO for pending writes
97 */
98
Shaohua Lif6bed0e2015-08-13 14:31:59 -070099struct r5l_log {
100 struct md_rdev *rdev;
101
102 u32 uuid_checksum;
103
104 sector_t device_size; /* log device size, round to
105 * BLOCK_SECTORS */
Shaohua Li0576b1c2015-08-13 14:32:00 -0700106 sector_t max_free_space; /* reclaim run if free space is at
107 * this size */
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700108
109 sector_t last_checkpoint; /* log tail. where recovery scan
110 * starts from */
111 u64 last_cp_seq; /* log tail sequence */
112
113 sector_t log_start; /* log head. where new data appends */
114 u64 seq; /* log head sequence */
115
Christoph Hellwig17036462015-10-05 09:31:06 +0200116 sector_t next_checkpoint;
Christoph Hellwig17036462015-10-05 09:31:06 +0200117
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700118 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 */
Shaohua Lia8c34f92015-09-02 13:49:46 -0700128 struct list_head flushing_ios; /* io_units which are waiting for log
129 * cache flush */
Christoph Hellwig04732f72015-10-05 09:31:07 +0200130 struct list_head finished_ios; /* io_units which settle down in log disk */
Shaohua Lia8c34f92015-09-02 13:49:46 -0700131 struct bio flush_bio;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700132
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100133 struct list_head no_mem_stripes; /* pending stripes, -ENOMEM */
134
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700135 struct kmem_cache *io_kc;
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100136 mempool_t *io_pool;
Christoph Hellwigc38d29b2015-12-21 10:51:02 +1100137 struct bio_set *bs;
Christoph Hellwige8deb632015-12-21 10:51:02 +1100138 mempool_t *meta_pool;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700139
Shaohua Li0576b1c2015-08-13 14:32:00 -0700140 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) */
Shaohua Li0fd22b42015-09-02 13:49:47 -0700148 wait_queue_head_t iounit_wait;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700149
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700150 struct list_head no_space_stripes; /* pending stripes, log has no space */
151 spinlock_t no_space_stripes_lock;
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200152
153 bool need_cache_flush;
Song Liu2ded3702016-11-17 15:24:38 -0800154
155 /* for r5c_cache */
156 enum r5c_journal_mode r5c_journal_mode;
Song Liua39f7af2016-11-17 15:24:40 -0800157
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;
Song Liu3bddb7f2016-11-18 16:46:50 -0800163
164 /* to submit async io_units, to fulfill ordering of flush */
165 struct work_struct deferred_io_work;
Song Liu2e38a372017-01-24 10:45:30 -0800166 /* to disable write back during in degraded mode */
167 struct work_struct disable_writeback_work;
Song Liu03b047f2017-01-11 13:39:14 -0800168
169 /* to for chunk_aligned_read in writeback mode, details below */
170 spinlock_t tree_lock;
171 struct radix_tree_root big_stripe_tree;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700172};
173
174/*
Song Liu03b047f2017-01-11 13:39:14 -0800175 * Enable chunk_aligned_read() with write back cache.
176 *
177 * Each chunk may contain more than one stripe (for example, a 256kB
178 * chunk contains 64 4kB-page, so this chunk contain 64 stripes). For
179 * chunk_aligned_read, these stripes are grouped into one "big_stripe".
180 * For each big_stripe, we count how many stripes of this big_stripe
181 * are in the write back cache. These data are tracked in a radix tree
182 * (big_stripe_tree). We use radix_tree item pointer as the counter.
183 * r5c_tree_index() is used to calculate keys for the radix tree.
184 *
185 * chunk_aligned_read() calls r5c_big_stripe_cached() to look up
186 * big_stripe of each chunk in the tree. If this big_stripe is in the
187 * tree, chunk_aligned_read() aborts. This look up is protected by
188 * rcu_read_lock().
189 *
190 * It is necessary to remember whether a stripe is counted in
191 * big_stripe_tree. Instead of adding new flag, we reuses existing flags:
192 * STRIPE_R5C_PARTIAL_STRIPE and STRIPE_R5C_FULL_STRIPE. If either of these
193 * two flags are set, the stripe is counted in big_stripe_tree. This
194 * requires moving set_bit(STRIPE_R5C_PARTIAL_STRIPE) to
195 * r5c_try_caching_write(); and moving clear_bit of
196 * STRIPE_R5C_PARTIAL_STRIPE and STRIPE_R5C_FULL_STRIPE to
197 * r5c_finish_stripe_write_out().
198 */
199
200/*
201 * radix tree requests lowest 2 bits of data pointer to be 2b'00.
202 * So it is necessary to left shift the counter by 2 bits before using it
203 * as data pointer of the tree.
204 */
205#define R5C_RADIX_COUNT_SHIFT 2
206
207/*
208 * calculate key for big_stripe_tree
209 *
210 * sect: align_bi->bi_iter.bi_sector or sh->sector
211 */
212static inline sector_t r5c_tree_index(struct r5conf *conf,
213 sector_t sect)
214{
215 sector_t offset;
216
217 offset = sector_div(sect, conf->chunk_sectors);
218 return sect;
219}
220
221/*
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700222 * an IO range starts from a meta data block and end at the next meta data
223 * block. The io unit's the meta data block tracks data/parity followed it. io
224 * unit is written to log disk with normal write, as we always flush log disk
225 * first and then start move data to raid disks, there is no requirement to
226 * write io unit with FLUSH/FUA
227 */
228struct r5l_io_unit {
229 struct r5l_log *log;
230
231 struct page *meta_page; /* store meta block */
232 int meta_offset; /* current offset in meta_page */
233
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700234 struct bio *current_bio;/* current_bio accepting new data */
235
236 atomic_t pending_stripe;/* how many stripes not flushed to raid */
237 u64 seq; /* seq number of the metablock */
238 sector_t log_start; /* where the io_unit starts */
239 sector_t log_end; /* where the io_unit ends */
240 struct list_head log_sibling; /* log->running_ios */
241 struct list_head stripe_list; /* stripes added to the io_unit */
242
243 int state;
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200244 bool need_split_bio;
Song Liu3bddb7f2016-11-18 16:46:50 -0800245 struct bio *split_bio;
246
247 unsigned int has_flush:1; /* include flush request */
248 unsigned int has_fua:1; /* include fua request */
249 unsigned int has_null_flush:1; /* include empty flush request */
250 /*
251 * io isn't sent yet, flush/fua request can only be submitted till it's
252 * the first IO in running_ios list
253 */
254 unsigned int io_deferred:1;
255
256 struct bio_list flush_barriers; /* size == 0 flush bios */
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700257};
258
259/* r5l_io_unit state */
260enum r5l_io_unit_state {
261 IO_UNIT_RUNNING = 0, /* accepting new IO */
262 IO_UNIT_IO_START = 1, /* io_unit bio start writing to log,
263 * don't accepting new bio */
264 IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */
Shaohua Lia8c34f92015-09-02 13:49:46 -0700265 IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700266};
267
Song Liu2ded3702016-11-17 15:24:38 -0800268bool r5c_is_writeback(struct r5l_log *log)
269{
270 return (log != NULL &&
271 log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK);
272}
273
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700274static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
275{
276 start += inc;
277 if (start >= log->device_size)
278 start = start - log->device_size;
279 return start;
280}
281
282static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
283 sector_t end)
284{
285 if (end >= start)
286 return end - start;
287 else
288 return end + log->device_size - start;
289}
290
291static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
292{
293 sector_t used_size;
294
295 used_size = r5l_ring_distance(log, log->last_checkpoint,
296 log->log_start);
297
298 return log->device_size > used_size + size;
299}
300
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700301static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
302 enum r5l_io_unit_state state)
303{
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700304 if (WARN_ON(io->state >= state))
305 return;
306 io->state = state;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700307}
308
Song Liu1e6d6902016-11-17 15:24:39 -0800309static void
310r5c_return_dev_pending_writes(struct r5conf *conf, struct r5dev *dev,
311 struct bio_list *return_bi)
312{
313 struct bio *wbi, *wbi2;
314
315 wbi = dev->written;
316 dev->written = NULL;
317 while (wbi && wbi->bi_iter.bi_sector <
318 dev->sector + STRIPE_SECTORS) {
319 wbi2 = r5_next_bio(wbi, dev->sector);
320 if (!raid5_dec_bi_active_stripes(wbi)) {
321 md_write_end(conf->mddev);
322 bio_list_add(return_bi, wbi);
323 }
324 wbi = wbi2;
325 }
326}
327
328void r5c_handle_cached_data_endio(struct r5conf *conf,
329 struct stripe_head *sh, int disks, struct bio_list *return_bi)
330{
331 int i;
332
333 for (i = sh->disks; i--; ) {
334 if (sh->dev[i].written) {
335 set_bit(R5_UPTODATE, &sh->dev[i].flags);
336 r5c_return_dev_pending_writes(conf, &sh->dev[i],
337 return_bi);
338 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
339 STRIPE_SECTORS,
340 !test_bit(STRIPE_DEGRADED, &sh->state),
341 0);
342 }
343 }
344}
345
Song Liua39f7af2016-11-17 15:24:40 -0800346/* Check whether we should flush some stripes to free up stripe cache */
347void r5c_check_stripe_cache_usage(struct r5conf *conf)
348{
349 int total_cached;
350
351 if (!r5c_is_writeback(conf->log))
352 return;
353
354 total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
355 atomic_read(&conf->r5c_cached_full_stripes);
356
357 /*
358 * The following condition is true for either of the following:
359 * - stripe cache pressure high:
360 * total_cached > 3/4 min_nr_stripes ||
361 * empty_inactive_list_nr > 0
362 * - stripe cache pressure moderate:
363 * total_cached > 1/2 min_nr_stripes
364 */
365 if (total_cached > conf->min_nr_stripes * 1 / 2 ||
366 atomic_read(&conf->empty_inactive_list_nr) > 0)
367 r5l_wake_reclaim(conf->log, 0);
368}
369
370/*
371 * flush cache when there are R5C_FULL_STRIPE_FLUSH_BATCH or more full
372 * stripes in the cache
373 */
374void r5c_check_cached_full_stripe(struct r5conf *conf)
375{
376 if (!r5c_is_writeback(conf->log))
377 return;
378
379 /*
380 * wake up reclaim for R5C_FULL_STRIPE_FLUSH_BATCH cached stripes
381 * or a full stripe (chunk size / 4k stripes).
382 */
383 if (atomic_read(&conf->r5c_cached_full_stripes) >=
384 min(R5C_FULL_STRIPE_FLUSH_BATCH,
385 conf->chunk_sectors >> STRIPE_SHIFT))
386 r5l_wake_reclaim(conf->log, 0);
387}
388
389/*
390 * Total log space (in sectors) needed to flush all data in cache
391 *
392 * Currently, writing-out phase automatically includes all pending writes
393 * to the same sector. So the reclaim of each stripe takes up to
394 * (conf->raid_disks + 1) pages of log space.
395 *
396 * To totally avoid deadlock due to log space, the code reserves
397 * (conf->raid_disks + 1) pages for each stripe in cache, which is not
398 * necessary in most cases.
399 *
400 * To improve this, we will need writing-out phase to be able to NOT include
401 * pending writes, which will reduce the requirement to
402 * (conf->max_degraded + 1) pages per stripe in cache.
403 */
404static sector_t r5c_log_required_to_flush_cache(struct r5conf *conf)
405{
406 struct r5l_log *log = conf->log;
407
408 if (!r5c_is_writeback(log))
409 return 0;
410
411 return BLOCK_SECTORS * (conf->raid_disks + 1) *
412 atomic_read(&log->stripe_in_journal_count);
413}
414
415/*
416 * evaluate log space usage and update R5C_LOG_TIGHT and R5C_LOG_CRITICAL
417 *
418 * R5C_LOG_TIGHT is set when free space on the log device is less than 3x of
419 * reclaim_required_space. R5C_LOG_CRITICAL is set when free space on the log
420 * device is less than 2x of reclaim_required_space.
421 */
422static inline void r5c_update_log_state(struct r5l_log *log)
423{
424 struct r5conf *conf = log->rdev->mddev->private;
425 sector_t free_space;
426 sector_t reclaim_space;
Song Liuf687a332016-11-30 16:57:54 -0800427 bool wake_reclaim = false;
Song Liua39f7af2016-11-17 15:24:40 -0800428
429 if (!r5c_is_writeback(log))
430 return;
431
432 free_space = r5l_ring_distance(log, log->log_start,
433 log->last_checkpoint);
434 reclaim_space = r5c_log_required_to_flush_cache(conf);
435 if (free_space < 2 * reclaim_space)
436 set_bit(R5C_LOG_CRITICAL, &conf->cache_state);
Song Liuf687a332016-11-30 16:57:54 -0800437 else {
438 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state))
439 wake_reclaim = true;
Song Liua39f7af2016-11-17 15:24:40 -0800440 clear_bit(R5C_LOG_CRITICAL, &conf->cache_state);
Song Liuf687a332016-11-30 16:57:54 -0800441 }
Song Liua39f7af2016-11-17 15:24:40 -0800442 if (free_space < 3 * reclaim_space)
443 set_bit(R5C_LOG_TIGHT, &conf->cache_state);
444 else
445 clear_bit(R5C_LOG_TIGHT, &conf->cache_state);
Song Liuf687a332016-11-30 16:57:54 -0800446
447 if (wake_reclaim)
448 r5l_wake_reclaim(log, 0);
Song Liua39f7af2016-11-17 15:24:40 -0800449}
450
Song Liu2ded3702016-11-17 15:24:38 -0800451/*
452 * Put the stripe into writing-out phase by clearing STRIPE_R5C_CACHING.
453 * This function should only be called in write-back mode.
454 */
Song Liua39f7af2016-11-17 15:24:40 -0800455void r5c_make_stripe_write_out(struct stripe_head *sh)
Song Liu2ded3702016-11-17 15:24:38 -0800456{
457 struct r5conf *conf = sh->raid_conf;
458 struct r5l_log *log = conf->log;
459
460 BUG_ON(!r5c_is_writeback(log));
461
462 WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
463 clear_bit(STRIPE_R5C_CACHING, &sh->state);
Song Liu1e6d6902016-11-17 15:24:39 -0800464
465 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
466 atomic_inc(&conf->preread_active_stripes);
Song Liu1e6d6902016-11-17 15:24:39 -0800467}
468
469static void r5c_handle_data_cached(struct stripe_head *sh)
470{
471 int i;
472
473 for (i = sh->disks; i--; )
474 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
475 set_bit(R5_InJournal, &sh->dev[i].flags);
476 clear_bit(R5_LOCKED, &sh->dev[i].flags);
477 }
478 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
479}
480
481/*
482 * this journal write must contain full parity,
483 * it may also contain some data pages
484 */
485static void r5c_handle_parity_cached(struct stripe_head *sh)
486{
487 int i;
488
489 for (i = sh->disks; i--; )
490 if (test_bit(R5_InJournal, &sh->dev[i].flags))
491 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Song Liu2ded3702016-11-17 15:24:38 -0800492}
493
494/*
495 * Setting proper flags after writing (or flushing) data and/or parity to the
496 * log device. This is called from r5l_log_endio() or r5l_log_flush_endio().
497 */
498static void r5c_finish_cache_stripe(struct stripe_head *sh)
499{
500 struct r5l_log *log = sh->raid_conf->log;
501
502 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
503 BUG_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
504 /*
505 * Set R5_InJournal for parity dev[pd_idx]. This means
506 * all data AND parity in the journal. For RAID 6, it is
507 * NOT necessary to set the flag for dev[qd_idx], as the
508 * two parities are written out together.
509 */
510 set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
Song Liu1e6d6902016-11-17 15:24:39 -0800511 } else if (test_bit(STRIPE_R5C_CACHING, &sh->state)) {
512 r5c_handle_data_cached(sh);
513 } else {
514 r5c_handle_parity_cached(sh);
515 set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
516 }
Song Liu2ded3702016-11-17 15:24:38 -0800517}
518
Christoph Hellwigd8858f42015-10-05 09:31:08 +0200519static void r5l_io_run_stripes(struct r5l_io_unit *io)
520{
521 struct stripe_head *sh, *next;
522
523 list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
524 list_del_init(&sh->log_list);
Song Liu2ded3702016-11-17 15:24:38 -0800525
526 r5c_finish_cache_stripe(sh);
527
Christoph Hellwigd8858f42015-10-05 09:31:08 +0200528 set_bit(STRIPE_HANDLE, &sh->state);
529 raid5_release_stripe(sh);
530 }
531}
532
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200533static void r5l_log_run_stripes(struct r5l_log *log)
534{
535 struct r5l_io_unit *io, *next;
536
537 assert_spin_locked(&log->io_list_lock);
538
539 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
540 /* don't change list order */
541 if (io->state < IO_UNIT_IO_END)
542 break;
543
544 list_move_tail(&io->log_sibling, &log->finished_ios);
545 r5l_io_run_stripes(io);
546 }
547}
548
Christoph Hellwig3848c0b2015-12-21 10:51:01 +1100549static void r5l_move_to_end_ios(struct r5l_log *log)
550{
551 struct r5l_io_unit *io, *next;
552
553 assert_spin_locked(&log->io_list_lock);
554
555 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
556 /* don't change list order */
557 if (io->state < IO_UNIT_IO_END)
558 break;
559 list_move_tail(&io->log_sibling, &log->io_end_ios);
560 }
561}
562
Song Liu3bddb7f2016-11-18 16:46:50 -0800563static void __r5l_stripe_write_finished(struct r5l_io_unit *io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700564static void r5l_log_endio(struct bio *bio)
565{
566 struct r5l_io_unit *io = bio->bi_private;
Song Liu3bddb7f2016-11-18 16:46:50 -0800567 struct r5l_io_unit *io_deferred;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700568 struct r5l_log *log = io->log;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700569 unsigned long flags;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700570
Shaohua Li6e74a9c2015-10-08 21:54:08 -0700571 if (bio->bi_error)
572 md_error(log->rdev->mddev, log->rdev);
573
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700574 bio_put(bio);
Christoph Hellwige8deb632015-12-21 10:51:02 +1100575 mempool_free(io->meta_page, log->meta_pool);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700576
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700577 spin_lock_irqsave(&log->io_list_lock, flags);
578 __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200579 if (log->need_cache_flush)
Christoph Hellwig3848c0b2015-12-21 10:51:01 +1100580 r5l_move_to_end_ios(log);
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200581 else
582 r5l_log_run_stripes(log);
Song Liu3bddb7f2016-11-18 16:46:50 -0800583 if (!list_empty(&log->running_ios)) {
584 /*
585 * FLUSH/FUA io_unit is deferred because of ordering, now we
586 * can dispatch it
587 */
588 io_deferred = list_first_entry(&log->running_ios,
589 struct r5l_io_unit, log_sibling);
590 if (io_deferred->io_deferred)
591 schedule_work(&log->deferred_io_work);
592 }
593
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700594 spin_unlock_irqrestore(&log->io_list_lock, flags);
595
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200596 if (log->need_cache_flush)
597 md_wakeup_thread(log->rdev->mddev->thread);
Song Liu3bddb7f2016-11-18 16:46:50 -0800598
599 if (io->has_null_flush) {
600 struct bio *bi;
601
602 WARN_ON(bio_list_empty(&io->flush_barriers));
603 while ((bi = bio_list_pop(&io->flush_barriers)) != NULL) {
604 bio_endio(bi);
605 atomic_dec(&io->pending_stripe);
606 }
607 if (atomic_read(&io->pending_stripe) == 0)
608 __r5l_stripe_write_finished(io);
609 }
610}
611
612static void r5l_do_submit_io(struct r5l_log *log, struct r5l_io_unit *io)
613{
614 unsigned long flags;
615
616 spin_lock_irqsave(&log->io_list_lock, flags);
617 __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
618 spin_unlock_irqrestore(&log->io_list_lock, flags);
619
620 if (io->has_flush)
Shaohua Li20737732016-12-13 12:40:15 -0800621 io->current_bio->bi_opf |= REQ_PREFLUSH;
Song Liu3bddb7f2016-11-18 16:46:50 -0800622 if (io->has_fua)
Shaohua Li20737732016-12-13 12:40:15 -0800623 io->current_bio->bi_opf |= REQ_FUA;
Song Liu3bddb7f2016-11-18 16:46:50 -0800624 submit_bio(io->current_bio);
625
626 if (!io->split_bio)
627 return;
628
629 if (io->has_flush)
Shaohua Li20737732016-12-13 12:40:15 -0800630 io->split_bio->bi_opf |= REQ_PREFLUSH;
Song Liu3bddb7f2016-11-18 16:46:50 -0800631 if (io->has_fua)
Shaohua Li20737732016-12-13 12:40:15 -0800632 io->split_bio->bi_opf |= REQ_FUA;
Song Liu3bddb7f2016-11-18 16:46:50 -0800633 submit_bio(io->split_bio);
634}
635
636/* deferred io_unit will be dispatched here */
637static void r5l_submit_io_async(struct work_struct *work)
638{
639 struct r5l_log *log = container_of(work, struct r5l_log,
640 deferred_io_work);
641 struct r5l_io_unit *io = NULL;
642 unsigned long flags;
643
644 spin_lock_irqsave(&log->io_list_lock, flags);
645 if (!list_empty(&log->running_ios)) {
646 io = list_first_entry(&log->running_ios, struct r5l_io_unit,
647 log_sibling);
648 if (!io->io_deferred)
649 io = NULL;
650 else
651 io->io_deferred = 0;
652 }
653 spin_unlock_irqrestore(&log->io_list_lock, flags);
654 if (io)
655 r5l_do_submit_io(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700656}
657
Song Liu2e38a372017-01-24 10:45:30 -0800658static void r5c_disable_writeback_async(struct work_struct *work)
659{
660 struct r5l_log *log = container_of(work, struct r5l_log,
661 disable_writeback_work);
662 struct mddev *mddev = log->rdev->mddev;
663
664 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
665 return;
666 pr_info("md/raid:%s: Disabling writeback cache for degraded array.\n",
667 mdname(mddev));
668 mddev_suspend(mddev);
669 log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
670 mddev_resume(mddev);
671}
672
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700673static void r5l_submit_current_io(struct r5l_log *log)
674{
675 struct r5l_io_unit *io = log->current_io;
Song Liu3bddb7f2016-11-18 16:46:50 -0800676 struct bio *bio;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700677 struct r5l_meta_block *block;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700678 unsigned long flags;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700679 u32 crc;
Song Liu3bddb7f2016-11-18 16:46:50 -0800680 bool do_submit = true;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700681
682 if (!io)
683 return;
684
685 block = page_address(io->meta_page);
686 block->meta_size = cpu_to_le32(io->meta_offset);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700687 crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700688 block->checksum = cpu_to_le32(crc);
Song Liu3bddb7f2016-11-18 16:46:50 -0800689 bio = io->current_bio;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700690
691 log->current_io = NULL;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700692 spin_lock_irqsave(&log->io_list_lock, flags);
Song Liu3bddb7f2016-11-18 16:46:50 -0800693 if (io->has_flush || io->has_fua) {
694 if (io != list_first_entry(&log->running_ios,
695 struct r5l_io_unit, log_sibling)) {
696 io->io_deferred = 1;
697 do_submit = false;
698 }
699 }
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700700 spin_unlock_irqrestore(&log->io_list_lock, flags);
Song Liu3bddb7f2016-11-18 16:46:50 -0800701 if (do_submit)
702 r5l_do_submit_io(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700703}
704
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200705static struct bio *r5l_bio_alloc(struct r5l_log *log)
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200706{
Christoph Hellwigc38d29b2015-12-21 10:51:02 +1100707 struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200708
Mike Christie796a5cf2016-06-05 14:32:07 -0500709 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200710 bio->bi_bdev = log->rdev->bdev;
Christoph Hellwig1e932a32015-10-05 09:31:12 +0200711 bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200712
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200713 return bio;
714}
715
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200716static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
717{
718 log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
719
Song Liua39f7af2016-11-17 15:24:40 -0800720 r5c_update_log_state(log);
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200721 /*
722 * If we filled up the log device start from the beginning again,
723 * which will require a new bio.
724 *
725 * Note: for this to work properly the log size needs to me a multiple
726 * of BLOCK_SECTORS.
727 */
728 if (log->log_start == 0)
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200729 io->need_split_bio = true;
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200730
731 io->log_end = log->log_start;
732}
733
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700734static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
735{
736 struct r5l_io_unit *io;
737 struct r5l_meta_block *block;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700738
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100739 io = mempool_alloc(log->io_pool, GFP_ATOMIC);
740 if (!io)
741 return NULL;
742 memset(io, 0, sizeof(*io));
743
Christoph Hellwig51039cd2015-10-05 09:31:13 +0200744 io->log = log;
Christoph Hellwig51039cd2015-10-05 09:31:13 +0200745 INIT_LIST_HEAD(&io->log_sibling);
746 INIT_LIST_HEAD(&io->stripe_list);
Song Liu3bddb7f2016-11-18 16:46:50 -0800747 bio_list_init(&io->flush_barriers);
Christoph Hellwig51039cd2015-10-05 09:31:13 +0200748 io->state = IO_UNIT_RUNNING;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700749
Christoph Hellwige8deb632015-12-21 10:51:02 +1100750 io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700751 block = page_address(io->meta_page);
Christoph Hellwige8deb632015-12-21 10:51:02 +1100752 clear_page(block);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700753 block->magic = cpu_to_le32(R5LOG_MAGIC);
754 block->version = R5LOG_VERSION;
755 block->seq = cpu_to_le64(log->seq);
756 block->position = cpu_to_le64(log->log_start);
757
758 io->log_start = log->log_start;
759 io->meta_offset = sizeof(struct r5l_meta_block);
Christoph Hellwig2b8ef162015-10-05 09:31:15 +0200760 io->seq = log->seq++;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700761
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200762 io->current_bio = r5l_bio_alloc(log);
763 io->current_bio->bi_end_io = r5l_log_endio;
764 io->current_bio->bi_private = io;
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200765 bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700766
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200767 r5_reserve_log_entry(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700768
769 spin_lock_irq(&log->io_list_lock);
770 list_add_tail(&io->log_sibling, &log->running_ios);
771 spin_unlock_irq(&log->io_list_lock);
772
773 return io;
774}
775
776static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
777{
Christoph Hellwig22581f52015-10-05 09:31:10 +0200778 if (log->current_io &&
779 log->current_io->meta_offset + payload_size > PAGE_SIZE)
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700780 r5l_submit_current_io(log);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700781
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100782 if (!log->current_io) {
Christoph Hellwig22581f52015-10-05 09:31:10 +0200783 log->current_io = r5l_new_meta(log);
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100784 if (!log->current_io)
785 return -ENOMEM;
786 }
787
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700788 return 0;
789}
790
791static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
792 sector_t location,
793 u32 checksum1, u32 checksum2,
794 bool checksum2_valid)
795{
796 struct r5l_io_unit *io = log->current_io;
797 struct r5l_payload_data_parity *payload;
798
799 payload = page_address(io->meta_page) + io->meta_offset;
800 payload->header.type = cpu_to_le16(type);
801 payload->header.flags = cpu_to_le16(0);
802 payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
803 (PAGE_SHIFT - 9));
804 payload->location = cpu_to_le64(location);
805 payload->checksum[0] = cpu_to_le32(checksum1);
806 if (checksum2_valid)
807 payload->checksum[1] = cpu_to_le32(checksum2);
808
809 io->meta_offset += sizeof(struct r5l_payload_data_parity) +
810 sizeof(__le32) * (1 + !!checksum2_valid);
811}
812
813static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
814{
815 struct r5l_io_unit *io = log->current_io;
816
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200817 if (io->need_split_bio) {
Song Liu3bddb7f2016-11-18 16:46:50 -0800818 BUG_ON(io->split_bio);
819 io->split_bio = io->current_bio;
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200820 io->current_bio = r5l_bio_alloc(log);
Song Liu3bddb7f2016-11-18 16:46:50 -0800821 bio_chain(io->current_bio, io->split_bio);
822 io->need_split_bio = false;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700823 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700824
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200825 if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
826 BUG();
827
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200828 r5_reserve_log_entry(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700829}
830
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100831static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700832 int data_pages, int parity_pages)
833{
834 int i;
835 int meta_size;
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100836 int ret;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700837 struct r5l_io_unit *io;
838
839 meta_size =
840 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
841 * data_pages) +
842 sizeof(struct r5l_payload_data_parity) +
843 sizeof(__le32) * parity_pages;
844
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100845 ret = r5l_get_meta(log, meta_size);
846 if (ret)
847 return ret;
848
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700849 io = log->current_io;
850
Song Liu3bddb7f2016-11-18 16:46:50 -0800851 if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state))
852 io->has_flush = 1;
853
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700854 for (i = 0; i < sh->disks; i++) {
Song Liu1e6d6902016-11-17 15:24:39 -0800855 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
856 test_bit(R5_InJournal, &sh->dev[i].flags))
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700857 continue;
858 if (i == sh->pd_idx || i == sh->qd_idx)
859 continue;
Song Liu3bddb7f2016-11-18 16:46:50 -0800860 if (test_bit(R5_WantFUA, &sh->dev[i].flags) &&
861 log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) {
862 io->has_fua = 1;
863 /*
864 * we need to flush journal to make sure recovery can
865 * reach the data with fua flag
866 */
867 io->has_flush = 1;
868 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700869 r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
870 raid5_compute_blocknr(sh, i, 0),
871 sh->dev[i].log_checksum, 0, false);
872 r5l_append_payload_page(log, sh->dev[i].page);
873 }
874
Song Liu2ded3702016-11-17 15:24:38 -0800875 if (parity_pages == 2) {
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700876 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
877 sh->sector, sh->dev[sh->pd_idx].log_checksum,
878 sh->dev[sh->qd_idx].log_checksum, true);
879 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
880 r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
Song Liu2ded3702016-11-17 15:24:38 -0800881 } else if (parity_pages == 1) {
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700882 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
883 sh->sector, sh->dev[sh->pd_idx].log_checksum,
884 0, false);
885 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
Song Liu2ded3702016-11-17 15:24:38 -0800886 } else /* Just writing data, not parity, in caching phase */
887 BUG_ON(parity_pages != 0);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700888
889 list_add_tail(&sh->log_list, &io->stripe_list);
890 atomic_inc(&io->pending_stripe);
891 sh->log_io = io;
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100892
Song Liua39f7af2016-11-17 15:24:40 -0800893 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
894 return 0;
895
896 if (sh->log_start == MaxSector) {
897 BUG_ON(!list_empty(&sh->r5c));
898 sh->log_start = io->log_start;
899 spin_lock_irq(&log->stripe_in_journal_lock);
900 list_add_tail(&sh->r5c,
901 &log->stripe_in_journal_list);
902 spin_unlock_irq(&log->stripe_in_journal_lock);
903 atomic_inc(&log->stripe_in_journal_count);
904 }
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100905 return 0;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700906}
907
Song Liua39f7af2016-11-17 15:24:40 -0800908/* add stripe to no_space_stripes, and then wake up reclaim */
909static inline void r5l_add_no_space_stripe(struct r5l_log *log,
910 struct stripe_head *sh)
911{
912 spin_lock(&log->no_space_stripes_lock);
913 list_add_tail(&sh->log_list, &log->no_space_stripes);
914 spin_unlock(&log->no_space_stripes_lock);
915}
916
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700917/*
918 * running in raid5d, where reclaim could wait for raid5d too (when it flushes
919 * data from log to raid disks), so we shouldn't wait for reclaim here
920 */
921int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
922{
Song Liua39f7af2016-11-17 15:24:40 -0800923 struct r5conf *conf = sh->raid_conf;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700924 int write_disks = 0;
925 int data_pages, parity_pages;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700926 int reserve;
927 int i;
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100928 int ret = 0;
Song Liua39f7af2016-11-17 15:24:40 -0800929 bool wake_reclaim = false;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700930
931 if (!log)
932 return -EAGAIN;
933 /* Don't support stripe batch */
934 if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
935 test_bit(STRIPE_SYNCING, &sh->state)) {
936 /* the stripe is written to log, we start writing it to raid */
937 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
938 return -EAGAIN;
939 }
940
Song Liu2ded3702016-11-17 15:24:38 -0800941 WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
942
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700943 for (i = 0; i < sh->disks; i++) {
944 void *addr;
945
Song Liu1e6d6902016-11-17 15:24:39 -0800946 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
947 test_bit(R5_InJournal, &sh->dev[i].flags))
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700948 continue;
Song Liu1e6d6902016-11-17 15:24:39 -0800949
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700950 write_disks++;
951 /* checksum is already calculated in last run */
952 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
953 continue;
954 addr = kmap_atomic(sh->dev[i].page);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700955 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
956 addr, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700957 kunmap_atomic(addr);
958 }
959 parity_pages = 1 + !!(sh->qd_idx >= 0);
960 data_pages = write_disks - parity_pages;
961
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700962 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
Shaohua Li253f9fd42015-09-04 14:14:16 -0700963 /*
964 * The stripe must enter state machine again to finish the write, so
965 * don't delay.
966 */
967 clear_bit(STRIPE_DELAYED, &sh->state);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700968 atomic_inc(&sh->count);
969
970 mutex_lock(&log->io_mutex);
971 /* meta + data */
972 reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700973
Song Liua39f7af2016-11-17 15:24:40 -0800974 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
975 if (!r5l_has_free_space(log, reserve)) {
976 r5l_add_no_space_stripe(log, sh);
977 wake_reclaim = true;
978 } else {
979 ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
980 if (ret) {
981 spin_lock_irq(&log->io_list_lock);
982 list_add_tail(&sh->log_list,
983 &log->no_mem_stripes);
984 spin_unlock_irq(&log->io_list_lock);
985 }
986 }
987 } else { /* R5C_JOURNAL_MODE_WRITE_BACK */
988 /*
989 * log space critical, do not process stripes that are
990 * not in cache yet (sh->log_start == MaxSector).
991 */
992 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
993 sh->log_start == MaxSector) {
994 r5l_add_no_space_stripe(log, sh);
995 wake_reclaim = true;
996 reserve = 0;
997 } else if (!r5l_has_free_space(log, reserve)) {
998 if (sh->log_start == log->last_checkpoint)
999 BUG();
1000 else
1001 r5l_add_no_space_stripe(log, sh);
1002 } else {
1003 ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
1004 if (ret) {
1005 spin_lock_irq(&log->io_list_lock);
1006 list_add_tail(&sh->log_list,
1007 &log->no_mem_stripes);
1008 spin_unlock_irq(&log->io_list_lock);
1009 }
Christoph Hellwig5036c3902015-12-21 10:51:02 +11001010 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001011 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001012
Christoph Hellwig5036c3902015-12-21 10:51:02 +11001013 mutex_unlock(&log->io_mutex);
Song Liua39f7af2016-11-17 15:24:40 -08001014 if (wake_reclaim)
1015 r5l_wake_reclaim(log, reserve);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001016 return 0;
1017}
1018
1019void r5l_write_stripe_run(struct r5l_log *log)
1020{
1021 if (!log)
1022 return;
1023 mutex_lock(&log->io_mutex);
1024 r5l_submit_current_io(log);
1025 mutex_unlock(&log->io_mutex);
1026}
1027
Shaohua Li828cbe92015-09-02 13:49:49 -07001028int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
1029{
1030 if (!log)
1031 return -ENODEV;
Song Liu3bddb7f2016-11-18 16:46:50 -08001032
1033 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
1034 /*
1035 * in write through (journal only)
1036 * we flush log disk cache first, then write stripe data to
1037 * raid disks. So if bio is finished, the log disk cache is
1038 * flushed already. The recovery guarantees we can recovery
1039 * the bio from log disk, so we don't need to flush again
1040 */
1041 if (bio->bi_iter.bi_size == 0) {
1042 bio_endio(bio);
1043 return 0;
1044 }
1045 bio->bi_opf &= ~REQ_PREFLUSH;
1046 } else {
1047 /* write back (with cache) */
1048 if (bio->bi_iter.bi_size == 0) {
1049 mutex_lock(&log->io_mutex);
1050 r5l_get_meta(log, 0);
1051 bio_list_add(&log->current_io->flush_barriers, bio);
1052 log->current_io->has_flush = 1;
1053 log->current_io->has_null_flush = 1;
1054 atomic_inc(&log->current_io->pending_stripe);
1055 r5l_submit_current_io(log);
1056 mutex_unlock(&log->io_mutex);
1057 return 0;
1058 }
Shaohua Li828cbe92015-09-02 13:49:49 -07001059 }
Shaohua Li828cbe92015-09-02 13:49:49 -07001060 return -EAGAIN;
1061}
1062
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001063/* This will run after log space is reclaimed */
1064static void r5l_run_no_space_stripes(struct r5l_log *log)
1065{
1066 struct stripe_head *sh;
1067
1068 spin_lock(&log->no_space_stripes_lock);
1069 while (!list_empty(&log->no_space_stripes)) {
1070 sh = list_first_entry(&log->no_space_stripes,
1071 struct stripe_head, log_list);
1072 list_del_init(&sh->log_list);
1073 set_bit(STRIPE_HANDLE, &sh->state);
1074 raid5_release_stripe(sh);
1075 }
1076 spin_unlock(&log->no_space_stripes_lock);
1077}
1078
Song Liua39f7af2016-11-17 15:24:40 -08001079/*
1080 * calculate new last_checkpoint
1081 * for write through mode, returns log->next_checkpoint
1082 * for write back, returns log_start of first sh in stripe_in_journal_list
1083 */
1084static sector_t r5c_calculate_new_cp(struct r5conf *conf)
1085{
1086 struct stripe_head *sh;
1087 struct r5l_log *log = conf->log;
1088 sector_t new_cp;
1089 unsigned long flags;
1090
1091 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
1092 return log->next_checkpoint;
1093
1094 spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
1095 if (list_empty(&conf->log->stripe_in_journal_list)) {
1096 /* all stripes flushed */
Dan Carpenterd3014e22016-11-24 14:13:04 +03001097 spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
Song Liua39f7af2016-11-17 15:24:40 -08001098 return log->next_checkpoint;
1099 }
1100 sh = list_first_entry(&conf->log->stripe_in_journal_list,
1101 struct stripe_head, r5c);
1102 new_cp = sh->log_start;
1103 spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
1104 return new_cp;
1105}
1106
Christoph Hellwig17036462015-10-05 09:31:06 +02001107static sector_t r5l_reclaimable_space(struct r5l_log *log)
1108{
Song Liua39f7af2016-11-17 15:24:40 -08001109 struct r5conf *conf = log->rdev->mddev->private;
1110
Christoph Hellwig17036462015-10-05 09:31:06 +02001111 return r5l_ring_distance(log, log->last_checkpoint,
Song Liua39f7af2016-11-17 15:24:40 -08001112 r5c_calculate_new_cp(conf));
Christoph Hellwig17036462015-10-05 09:31:06 +02001113}
1114
Christoph Hellwig5036c3902015-12-21 10:51:02 +11001115static void r5l_run_no_mem_stripe(struct r5l_log *log)
1116{
1117 struct stripe_head *sh;
1118
1119 assert_spin_locked(&log->io_list_lock);
1120
1121 if (!list_empty(&log->no_mem_stripes)) {
1122 sh = list_first_entry(&log->no_mem_stripes,
1123 struct stripe_head, log_list);
1124 list_del_init(&sh->log_list);
1125 set_bit(STRIPE_HANDLE, &sh->state);
1126 raid5_release_stripe(sh);
1127 }
1128}
1129
Christoph Hellwig04732f72015-10-05 09:31:07 +02001130static bool r5l_complete_finished_ios(struct r5l_log *log)
Christoph Hellwig17036462015-10-05 09:31:06 +02001131{
1132 struct r5l_io_unit *io, *next;
1133 bool found = false;
1134
1135 assert_spin_locked(&log->io_list_lock);
1136
Christoph Hellwig04732f72015-10-05 09:31:07 +02001137 list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
Christoph Hellwig17036462015-10-05 09:31:06 +02001138 /* don't change list order */
1139 if (io->state < IO_UNIT_STRIPE_END)
1140 break;
1141
1142 log->next_checkpoint = io->log_start;
Christoph Hellwig17036462015-10-05 09:31:06 +02001143
1144 list_del(&io->log_sibling);
Christoph Hellwig5036c3902015-12-21 10:51:02 +11001145 mempool_free(io, log->io_pool);
1146 r5l_run_no_mem_stripe(log);
Christoph Hellwig17036462015-10-05 09:31:06 +02001147
1148 found = true;
1149 }
1150
1151 return found;
1152}
1153
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001154static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
1155{
1156 struct r5l_log *log = io->log;
Song Liua39f7af2016-11-17 15:24:40 -08001157 struct r5conf *conf = log->rdev->mddev->private;
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001158 unsigned long flags;
1159
1160 spin_lock_irqsave(&log->io_list_lock, flags);
1161 __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
Christoph Hellwig17036462015-10-05 09:31:06 +02001162
Christoph Hellwig04732f72015-10-05 09:31:07 +02001163 if (!r5l_complete_finished_ios(log)) {
Shaohua Li85f2f9a2015-09-04 14:14:05 -07001164 spin_unlock_irqrestore(&log->io_list_lock, flags);
1165 return;
1166 }
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001167
Song Liua39f7af2016-11-17 15:24:40 -08001168 if (r5l_reclaimable_space(log) > log->max_free_space ||
1169 test_bit(R5C_LOG_TIGHT, &conf->cache_state))
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001170 r5l_wake_reclaim(log, 0);
1171
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001172 spin_unlock_irqrestore(&log->io_list_lock, flags);
1173 wake_up(&log->iounit_wait);
1174}
1175
Shaohua Li0576b1c2015-08-13 14:32:00 -07001176void r5l_stripe_write_finished(struct stripe_head *sh)
1177{
1178 struct r5l_io_unit *io;
1179
Shaohua Li0576b1c2015-08-13 14:32:00 -07001180 io = sh->log_io;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001181 sh->log_io = NULL;
1182
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001183 if (io && atomic_dec_and_test(&io->pending_stripe))
1184 __r5l_stripe_write_finished(io);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001185}
1186
Shaohua Lia8c34f92015-09-02 13:49:46 -07001187static void r5l_log_flush_endio(struct bio *bio)
1188{
1189 struct r5l_log *log = container_of(bio, struct r5l_log,
1190 flush_bio);
1191 unsigned long flags;
1192 struct r5l_io_unit *io;
Shaohua Lia8c34f92015-09-02 13:49:46 -07001193
Shaohua Li6e74a9c2015-10-08 21:54:08 -07001194 if (bio->bi_error)
1195 md_error(log->rdev->mddev, log->rdev);
1196
Shaohua Lia8c34f92015-09-02 13:49:46 -07001197 spin_lock_irqsave(&log->io_list_lock, flags);
Christoph Hellwigd8858f42015-10-05 09:31:08 +02001198 list_for_each_entry(io, &log->flushing_ios, log_sibling)
1199 r5l_io_run_stripes(io);
Christoph Hellwig04732f72015-10-05 09:31:07 +02001200 list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07001201 spin_unlock_irqrestore(&log->io_list_lock, flags);
1202}
1203
Shaohua Li0576b1c2015-08-13 14:32:00 -07001204/*
1205 * Starting dispatch IO to raid.
1206 * io_unit(meta) consists of a log. There is one situation we want to avoid. A
1207 * broken meta in the middle of a log causes recovery can't find meta at the
1208 * head of log. If operations require meta at the head persistent in log, we
1209 * must make sure meta before it persistent in log too. A case is:
1210 *
1211 * stripe data/parity is in log, we start write stripe to raid disks. stripe
1212 * data/parity must be persistent in log before we do the write to raid disks.
1213 *
1214 * The solution is we restrictly maintain io_unit list order. In this case, we
1215 * only write stripes of an io_unit to raid disks till the io_unit is the first
1216 * one whose data/parity is in log.
1217 */
1218void r5l_flush_stripe_to_raid(struct r5l_log *log)
1219{
Shaohua Lia8c34f92015-09-02 13:49:46 -07001220 bool do_flush;
Christoph Hellwig56fef7c2015-10-05 09:31:09 +02001221
1222 if (!log || !log->need_cache_flush)
Shaohua Li0576b1c2015-08-13 14:32:00 -07001223 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001224
Shaohua Lia8c34f92015-09-02 13:49:46 -07001225 spin_lock_irq(&log->io_list_lock);
1226 /* flush bio is running */
1227 if (!list_empty(&log->flushing_ios)) {
1228 spin_unlock_irq(&log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001229 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001230 }
Shaohua Lia8c34f92015-09-02 13:49:46 -07001231 list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
1232 do_flush = !list_empty(&log->flushing_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001233 spin_unlock_irq(&log->io_list_lock);
Shaohua Lia8c34f92015-09-02 13:49:46 -07001234
1235 if (!do_flush)
1236 return;
1237 bio_reset(&log->flush_bio);
1238 log->flush_bio.bi_bdev = log->rdev->bdev;
1239 log->flush_bio.bi_end_io = r5l_log_flush_endio;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001240 log->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
Mike Christie4e49ea42016-06-05 14:31:41 -05001241 submit_bio(&log->flush_bio);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001242}
1243
Shaohua Li0576b1c2015-08-13 14:32:00 -07001244static void r5l_write_super(struct r5l_log *log, sector_t cp);
Shaohua Li4b482042015-10-08 21:54:06 -07001245static void r5l_write_super_and_discard_space(struct r5l_log *log,
1246 sector_t end)
1247{
1248 struct block_device *bdev = log->rdev->bdev;
1249 struct mddev *mddev;
1250
1251 r5l_write_super(log, end);
1252
1253 if (!blk_queue_discard(bdev_get_queue(bdev)))
1254 return;
1255
1256 mddev = log->rdev->mddev;
1257 /*
Shaohua Li8e018c22016-08-25 10:09:39 -07001258 * Discard could zero data, so before discard we must make sure
1259 * superblock is updated to new log tail. Updating superblock (either
1260 * directly call md_update_sb() or depend on md thread) must hold
1261 * reconfig mutex. On the other hand, raid5_quiesce is called with
1262 * reconfig_mutex hold. The first step of raid5_quiesce() is waitting
1263 * for all IO finish, hence waitting for reclaim thread, while reclaim
1264 * thread is calling this function and waitting for reconfig mutex. So
1265 * there is a deadlock. We workaround this issue with a trylock.
1266 * FIXME: we could miss discard if we can't take reconfig mutex
Shaohua Li4b482042015-10-08 21:54:06 -07001267 */
Shaohua Li29530792016-12-08 15:48:19 -08001268 set_mask_bits(&mddev->sb_flags, 0,
1269 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
Shaohua Li8e018c22016-08-25 10:09:39 -07001270 if (!mddev_trylock(mddev))
1271 return;
1272 md_update_sb(mddev, 1);
1273 mddev_unlock(mddev);
Shaohua Li4b482042015-10-08 21:54:06 -07001274
Shaohua Li6e74a9c2015-10-08 21:54:08 -07001275 /* discard IO error really doesn't matter, ignore it */
Shaohua Li4b482042015-10-08 21:54:06 -07001276 if (log->last_checkpoint < end) {
1277 blkdev_issue_discard(bdev,
1278 log->last_checkpoint + log->rdev->data_offset,
1279 end - log->last_checkpoint, GFP_NOIO, 0);
1280 } else {
1281 blkdev_issue_discard(bdev,
1282 log->last_checkpoint + log->rdev->data_offset,
1283 log->device_size - log->last_checkpoint,
1284 GFP_NOIO, 0);
1285 blkdev_issue_discard(bdev, log->rdev->data_offset, end,
1286 GFP_NOIO, 0);
1287 }
1288}
1289
Song Liua39f7af2016-11-17 15:24:40 -08001290/*
1291 * r5c_flush_stripe moves stripe from cached list to handle_list. When called,
1292 * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes.
1293 *
1294 * must hold conf->device_lock
1295 */
1296static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh)
1297{
1298 BUG_ON(list_empty(&sh->lru));
1299 BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
1300 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
1301
1302 /*
1303 * The stripe is not ON_RELEASE_LIST, so it is safe to call
1304 * raid5_release_stripe() while holding conf->device_lock
1305 */
1306 BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
1307 assert_spin_locked(&conf->device_lock);
1308
1309 list_del_init(&sh->lru);
1310 atomic_inc(&sh->count);
1311
1312 set_bit(STRIPE_HANDLE, &sh->state);
1313 atomic_inc(&conf->active_stripes);
1314 r5c_make_stripe_write_out(sh);
1315
Song Liua39f7af2016-11-17 15:24:40 -08001316 raid5_release_stripe(sh);
1317}
1318
1319/*
1320 * if num == 0, flush all full stripes
1321 * if num > 0, flush all full stripes. If less than num full stripes are
1322 * flushed, flush some partial stripes until totally num stripes are
1323 * flushed or there is no more cached stripes.
1324 */
1325void r5c_flush_cache(struct r5conf *conf, int num)
1326{
1327 int count;
1328 struct stripe_head *sh, *next;
1329
1330 assert_spin_locked(&conf->device_lock);
1331 if (!conf->log)
1332 return;
1333
1334 count = 0;
1335 list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) {
1336 r5c_flush_stripe(conf, sh);
1337 count++;
1338 }
1339
1340 if (count >= num)
1341 return;
1342 list_for_each_entry_safe(sh, next,
1343 &conf->r5c_partial_stripe_list, lru) {
1344 r5c_flush_stripe(conf, sh);
1345 if (++count >= num)
1346 break;
1347 }
1348}
1349
1350static void r5c_do_reclaim(struct r5conf *conf)
1351{
1352 struct r5l_log *log = conf->log;
1353 struct stripe_head *sh;
1354 int count = 0;
1355 unsigned long flags;
1356 int total_cached;
1357 int stripes_to_flush;
1358
1359 if (!r5c_is_writeback(log))
1360 return;
1361
1362 total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
1363 atomic_read(&conf->r5c_cached_full_stripes);
1364
1365 if (total_cached > conf->min_nr_stripes * 3 / 4 ||
1366 atomic_read(&conf->empty_inactive_list_nr) > 0)
1367 /*
1368 * if stripe cache pressure high, flush all full stripes and
1369 * some partial stripes
1370 */
1371 stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP;
1372 else if (total_cached > conf->min_nr_stripes * 1 / 2 ||
1373 atomic_read(&conf->r5c_cached_full_stripes) >
1374 R5C_FULL_STRIPE_FLUSH_BATCH)
1375 /*
1376 * if stripe cache pressure moderate, or if there is many full
1377 * stripes,flush all full stripes
1378 */
1379 stripes_to_flush = 0;
1380 else
1381 /* no need to flush */
1382 stripes_to_flush = -1;
1383
1384 if (stripes_to_flush >= 0) {
1385 spin_lock_irqsave(&conf->device_lock, flags);
1386 r5c_flush_cache(conf, stripes_to_flush);
1387 spin_unlock_irqrestore(&conf->device_lock, flags);
1388 }
1389
1390 /* if log space is tight, flush stripes on stripe_in_journal_list */
1391 if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) {
1392 spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
1393 spin_lock(&conf->device_lock);
1394 list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) {
1395 /*
1396 * stripes on stripe_in_journal_list could be in any
1397 * state of the stripe_cache state machine. In this
1398 * case, we only want to flush stripe on
1399 * r5c_cached_full/partial_stripes. The following
1400 * condition makes sure the stripe is on one of the
1401 * two lists.
1402 */
1403 if (!list_empty(&sh->lru) &&
1404 !test_bit(STRIPE_HANDLE, &sh->state) &&
1405 atomic_read(&sh->count) == 0) {
1406 r5c_flush_stripe(conf, sh);
1407 }
1408 if (count++ >= R5C_RECLAIM_STRIPE_GROUP)
1409 break;
1410 }
1411 spin_unlock(&conf->device_lock);
1412 spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
1413 }
Song Liuf687a332016-11-30 16:57:54 -08001414
1415 if (!test_bit(R5C_LOG_CRITICAL, &conf->cache_state))
1416 r5l_run_no_space_stripes(log);
1417
Song Liua39f7af2016-11-17 15:24:40 -08001418 md_wakeup_thread(conf->mddev->thread);
1419}
Shaohua Li4b482042015-10-08 21:54:06 -07001420
Shaohua Li0576b1c2015-08-13 14:32:00 -07001421static void r5l_do_reclaim(struct r5l_log *log)
1422{
Song Liua39f7af2016-11-17 15:24:40 -08001423 struct r5conf *conf = log->rdev->mddev->private;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001424 sector_t reclaim_target = xchg(&log->reclaim_target, 0);
Christoph Hellwig17036462015-10-05 09:31:06 +02001425 sector_t reclaimable;
1426 sector_t next_checkpoint;
Song Liua39f7af2016-11-17 15:24:40 -08001427 bool write_super;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001428
1429 spin_lock_irq(&log->io_list_lock);
Song Liua39f7af2016-11-17 15:24:40 -08001430 write_super = r5l_reclaimable_space(log) > log->max_free_space ||
1431 reclaim_target != 0 || !list_empty(&log->no_space_stripes);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001432 /*
1433 * move proper io_unit to reclaim list. We should not change the order.
1434 * reclaimable/unreclaimable io_unit can be mixed in the list, we
1435 * shouldn't reuse space of an unreclaimable io_unit
1436 */
1437 while (1) {
Christoph Hellwig17036462015-10-05 09:31:06 +02001438 reclaimable = r5l_reclaimable_space(log);
1439 if (reclaimable >= reclaim_target ||
Shaohua Li0576b1c2015-08-13 14:32:00 -07001440 (list_empty(&log->running_ios) &&
1441 list_empty(&log->io_end_ios) &&
Shaohua Lia8c34f92015-09-02 13:49:46 -07001442 list_empty(&log->flushing_ios) &&
Christoph Hellwig04732f72015-10-05 09:31:07 +02001443 list_empty(&log->finished_ios)))
Shaohua Li0576b1c2015-08-13 14:32:00 -07001444 break;
1445
Christoph Hellwig17036462015-10-05 09:31:06 +02001446 md_wakeup_thread(log->rdev->mddev->thread);
1447 wait_event_lock_irq(log->iounit_wait,
1448 r5l_reclaimable_space(log) > reclaimable,
1449 log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001450 }
Christoph Hellwig17036462015-10-05 09:31:06 +02001451
Song Liua39f7af2016-11-17 15:24:40 -08001452 next_checkpoint = r5c_calculate_new_cp(conf);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001453 spin_unlock_irq(&log->io_list_lock);
1454
Song Liua39f7af2016-11-17 15:24:40 -08001455 if (reclaimable == 0 || !write_super)
Shaohua Li0576b1c2015-08-13 14:32:00 -07001456 return;
1457
Shaohua Li0576b1c2015-08-13 14:32:00 -07001458 /*
1459 * write_super will flush cache of each raid disk. We must write super
1460 * here, because the log area might be reused soon and we don't want to
1461 * confuse recovery
1462 */
Shaohua Li4b482042015-10-08 21:54:06 -07001463 r5l_write_super_and_discard_space(log, next_checkpoint);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001464
1465 mutex_lock(&log->io_mutex);
Christoph Hellwig17036462015-10-05 09:31:06 +02001466 log->last_checkpoint = next_checkpoint;
Song Liua39f7af2016-11-17 15:24:40 -08001467 r5c_update_log_state(log);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001468 mutex_unlock(&log->io_mutex);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001469
Christoph Hellwig17036462015-10-05 09:31:06 +02001470 r5l_run_no_space_stripes(log);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001471}
1472
1473static void r5l_reclaim_thread(struct md_thread *thread)
1474{
1475 struct mddev *mddev = thread->mddev;
1476 struct r5conf *conf = mddev->private;
1477 struct r5l_log *log = conf->log;
1478
1479 if (!log)
1480 return;
Song Liua39f7af2016-11-17 15:24:40 -08001481 r5c_do_reclaim(conf);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001482 r5l_do_reclaim(log);
1483}
1484
Song Liua39f7af2016-11-17 15:24:40 -08001485void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001486{
Shaohua Li0576b1c2015-08-13 14:32:00 -07001487 unsigned long target;
1488 unsigned long new = (unsigned long)space; /* overflow in theory */
1489
Song Liua39f7af2016-11-17 15:24:40 -08001490 if (!log)
1491 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001492 do {
1493 target = log->reclaim_target;
1494 if (new < target)
1495 return;
1496 } while (cmpxchg(&log->reclaim_target, target, new) != target);
1497 md_wakeup_thread(log->reclaim_thread);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001498}
1499
Shaohua Lie6c033f2015-10-04 09:20:12 -07001500void r5l_quiesce(struct r5l_log *log, int state)
1501{
Shaohua Li4b482042015-10-08 21:54:06 -07001502 struct mddev *mddev;
Shaohua Lie6c033f2015-10-04 09:20:12 -07001503 if (!log || state == 2)
1504 return;
Shaohua Lice1ccd02016-11-21 10:29:18 -08001505 if (state == 0)
1506 kthread_unpark(log->reclaim_thread->tsk);
1507 else if (state == 1) {
Shaohua Li4b482042015-10-08 21:54:06 -07001508 /* make sure r5l_write_super_and_discard_space exits */
1509 mddev = log->rdev->mddev;
1510 wake_up(&mddev->sb_wait);
Shaohua Lice1ccd02016-11-21 10:29:18 -08001511 kthread_park(log->reclaim_thread->tsk);
Song Liua39f7af2016-11-17 15:24:40 -08001512 r5l_wake_reclaim(log, MaxSector);
Shaohua Lie6c033f2015-10-04 09:20:12 -07001513 r5l_do_reclaim(log);
1514 }
1515}
1516
Shaohua Li6e74a9c2015-10-08 21:54:08 -07001517bool r5l_log_disk_error(struct r5conf *conf)
1518{
Shaohua Lif6b6ec52015-12-21 10:51:02 +11001519 struct r5l_log *log;
1520 bool ret;
Shaohua Li7dde2ad2015-10-08 21:54:10 -07001521 /* don't allow write if journal disk is missing */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11001522 rcu_read_lock();
1523 log = rcu_dereference(conf->log);
1524
1525 if (!log)
1526 ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
1527 else
1528 ret = test_bit(Faulty, &log->rdev->flags);
1529 rcu_read_unlock();
1530 return ret;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07001531}
1532
Shaohua Li355810d2015-08-13 14:32:01 -07001533struct r5l_recovery_ctx {
1534 struct page *meta_page; /* current meta */
1535 sector_t meta_total_blocks; /* total size of current meta and data */
1536 sector_t pos; /* recovery position */
1537 u64 seq; /* recovery position seq */
Song Liub4c625c2016-11-17 15:24:43 -08001538 int data_parity_stripes; /* number of data_parity stripes */
1539 int data_only_stripes; /* number of data_only stripes */
1540 struct list_head cached_list;
Shaohua Li355810d2015-08-13 14:32:01 -07001541};
1542
Song Liu9ed988f52016-11-17 15:24:42 -08001543static int r5l_recovery_read_meta_block(struct r5l_log *log,
1544 struct r5l_recovery_ctx *ctx)
Shaohua Li355810d2015-08-13 14:32:01 -07001545{
1546 struct page *page = ctx->meta_page;
1547 struct r5l_meta_block *mb;
1548 u32 crc, stored_crc;
1549
Mike Christie796a5cf2016-06-05 14:32:07 -05001550 if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, REQ_OP_READ, 0,
1551 false))
Shaohua Li355810d2015-08-13 14:32:01 -07001552 return -EIO;
1553
1554 mb = page_address(page);
1555 stored_crc = le32_to_cpu(mb->checksum);
1556 mb->checksum = 0;
1557
1558 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
1559 le64_to_cpu(mb->seq) != ctx->seq ||
1560 mb->version != R5LOG_VERSION ||
1561 le64_to_cpu(mb->position) != ctx->pos)
1562 return -EINVAL;
1563
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001564 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -07001565 if (stored_crc != crc)
1566 return -EINVAL;
1567
1568 if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
1569 return -EINVAL;
1570
1571 ctx->meta_total_blocks = BLOCK_SECTORS;
1572
1573 return 0;
1574}
1575
Song Liu9ed988f52016-11-17 15:24:42 -08001576static void
1577r5l_recovery_create_empty_meta_block(struct r5l_log *log,
1578 struct page *page,
1579 sector_t pos, u64 seq)
Shaohua Li355810d2015-08-13 14:32:01 -07001580{
Shaohua Li355810d2015-08-13 14:32:01 -07001581 struct r5l_meta_block *mb;
Shaohua Li355810d2015-08-13 14:32:01 -07001582
Shaohua Li355810d2015-08-13 14:32:01 -07001583 mb = page_address(page);
Song Liu9ed988f52016-11-17 15:24:42 -08001584 clear_page(mb);
Shaohua Li355810d2015-08-13 14:32:01 -07001585 mb->magic = cpu_to_le32(R5LOG_MAGIC);
1586 mb->version = R5LOG_VERSION;
1587 mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
1588 mb->seq = cpu_to_le64(seq);
1589 mb->position = cpu_to_le64(pos);
Shaohua Li355810d2015-08-13 14:32:01 -07001590}
1591
1592static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
1593 u64 seq)
1594{
1595 struct page *page;
1596 struct r5l_meta_block *mb;
Shaohua Li355810d2015-08-13 14:32:01 -07001597
Song Liu9ed988f52016-11-17 15:24:42 -08001598 page = alloc_page(GFP_KERNEL);
Shaohua Li355810d2015-08-13 14:32:01 -07001599 if (!page)
1600 return -ENOMEM;
Song Liu9ed988f52016-11-17 15:24:42 -08001601 r5l_recovery_create_empty_meta_block(log, page, pos, seq);
Shaohua Li355810d2015-08-13 14:32:01 -07001602 mb = page_address(page);
Song Liu5c88f402016-12-07 09:42:05 -08001603 mb->checksum = cpu_to_le32(crc32c_le(log->uuid_checksum,
1604 mb, PAGE_SIZE));
Mike Christie796a5cf2016-06-05 14:32:07 -05001605 if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE,
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001606 REQ_FUA, false)) {
Shaohua Li355810d2015-08-13 14:32:01 -07001607 __free_page(page);
1608 return -EIO;
1609 }
1610 __free_page(page);
1611 return 0;
1612}
1613
Song Liub4c625c2016-11-17 15:24:43 -08001614/*
1615 * r5l_recovery_load_data and r5l_recovery_load_parity uses flag R5_Wantwrite
1616 * to mark valid (potentially not flushed) data in the journal.
1617 *
1618 * We already verified checksum in r5l_recovery_verify_data_checksum_for_mb,
1619 * so there should not be any mismatch here.
1620 */
1621static void r5l_recovery_load_data(struct r5l_log *log,
1622 struct stripe_head *sh,
1623 struct r5l_recovery_ctx *ctx,
1624 struct r5l_payload_data_parity *payload,
1625 sector_t log_offset)
1626{
1627 struct mddev *mddev = log->rdev->mddev;
1628 struct r5conf *conf = mddev->private;
1629 int dd_idx;
1630
1631 raid5_compute_sector(conf,
1632 le64_to_cpu(payload->location), 0,
1633 &dd_idx, sh);
1634 sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1635 sh->dev[dd_idx].page, REQ_OP_READ, 0, false);
1636 sh->dev[dd_idx].log_checksum =
1637 le32_to_cpu(payload->checksum[0]);
1638 ctx->meta_total_blocks += BLOCK_SECTORS;
1639
1640 set_bit(R5_Wantwrite, &sh->dev[dd_idx].flags);
1641 set_bit(STRIPE_R5C_CACHING, &sh->state);
1642}
1643
1644static void r5l_recovery_load_parity(struct r5l_log *log,
1645 struct stripe_head *sh,
1646 struct r5l_recovery_ctx *ctx,
1647 struct r5l_payload_data_parity *payload,
1648 sector_t log_offset)
1649{
1650 struct mddev *mddev = log->rdev->mddev;
1651 struct r5conf *conf = mddev->private;
1652
1653 ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
1654 sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1655 sh->dev[sh->pd_idx].page, REQ_OP_READ, 0, false);
1656 sh->dev[sh->pd_idx].log_checksum =
1657 le32_to_cpu(payload->checksum[0]);
1658 set_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags);
1659
1660 if (sh->qd_idx >= 0) {
1661 sync_page_io(log->rdev,
1662 r5l_ring_add(log, log_offset, BLOCK_SECTORS),
1663 PAGE_SIZE, sh->dev[sh->qd_idx].page,
1664 REQ_OP_READ, 0, false);
1665 sh->dev[sh->qd_idx].log_checksum =
1666 le32_to_cpu(payload->checksum[1]);
1667 set_bit(R5_Wantwrite, &sh->dev[sh->qd_idx].flags);
1668 }
1669 clear_bit(STRIPE_R5C_CACHING, &sh->state);
1670}
1671
1672static void r5l_recovery_reset_stripe(struct stripe_head *sh)
1673{
1674 int i;
1675
1676 sh->state = 0;
1677 sh->log_start = MaxSector;
1678 for (i = sh->disks; i--; )
1679 sh->dev[i].flags = 0;
1680}
1681
1682static void
1683r5l_recovery_replay_one_stripe(struct r5conf *conf,
1684 struct stripe_head *sh,
1685 struct r5l_recovery_ctx *ctx)
1686{
1687 struct md_rdev *rdev, *rrdev;
1688 int disk_index;
1689 int data_count = 0;
1690
1691 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
1692 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
1693 continue;
1694 if (disk_index == sh->qd_idx || disk_index == sh->pd_idx)
1695 continue;
1696 data_count++;
1697 }
1698
1699 /*
1700 * stripes that only have parity must have been flushed
1701 * before the crash that we are now recovering from, so
1702 * there is nothing more to recovery.
1703 */
1704 if (data_count == 0)
1705 goto out;
1706
1707 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
1708 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
1709 continue;
1710
1711 /* in case device is broken */
1712 rcu_read_lock();
1713 rdev = rcu_dereference(conf->disks[disk_index].rdev);
1714 if (rdev) {
1715 atomic_inc(&rdev->nr_pending);
1716 rcu_read_unlock();
1717 sync_page_io(rdev, sh->sector, PAGE_SIZE,
1718 sh->dev[disk_index].page, REQ_OP_WRITE, 0,
1719 false);
1720 rdev_dec_pending(rdev, rdev->mddev);
1721 rcu_read_lock();
1722 }
1723 rrdev = rcu_dereference(conf->disks[disk_index].replacement);
1724 if (rrdev) {
1725 atomic_inc(&rrdev->nr_pending);
1726 rcu_read_unlock();
1727 sync_page_io(rrdev, sh->sector, PAGE_SIZE,
1728 sh->dev[disk_index].page, REQ_OP_WRITE, 0,
1729 false);
1730 rdev_dec_pending(rrdev, rrdev->mddev);
1731 rcu_read_lock();
1732 }
1733 rcu_read_unlock();
1734 }
1735 ctx->data_parity_stripes++;
1736out:
1737 r5l_recovery_reset_stripe(sh);
1738}
1739
1740static struct stripe_head *
1741r5c_recovery_alloc_stripe(struct r5conf *conf,
Song Liu3c66abb2016-12-14 15:38:01 -08001742 sector_t stripe_sect)
Song Liub4c625c2016-11-17 15:24:43 -08001743{
1744 struct stripe_head *sh;
1745
1746 sh = raid5_get_active_stripe(conf, stripe_sect, 0, 1, 0);
1747 if (!sh)
1748 return NULL; /* no more stripe available */
1749
1750 r5l_recovery_reset_stripe(sh);
Song Liub4c625c2016-11-17 15:24:43 -08001751
1752 return sh;
1753}
1754
1755static struct stripe_head *
1756r5c_recovery_lookup_stripe(struct list_head *list, sector_t sect)
1757{
1758 struct stripe_head *sh;
1759
1760 list_for_each_entry(sh, list, lru)
1761 if (sh->sector == sect)
1762 return sh;
1763 return NULL;
1764}
1765
1766static void
1767r5c_recovery_drop_stripes(struct list_head *cached_stripe_list,
1768 struct r5l_recovery_ctx *ctx)
1769{
1770 struct stripe_head *sh, *next;
1771
1772 list_for_each_entry_safe(sh, next, cached_stripe_list, lru) {
1773 r5l_recovery_reset_stripe(sh);
1774 list_del_init(&sh->lru);
1775 raid5_release_stripe(sh);
1776 }
1777}
1778
1779static void
1780r5c_recovery_replay_stripes(struct list_head *cached_stripe_list,
1781 struct r5l_recovery_ctx *ctx)
1782{
1783 struct stripe_head *sh, *next;
1784
1785 list_for_each_entry_safe(sh, next, cached_stripe_list, lru)
1786 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
1787 r5l_recovery_replay_one_stripe(sh->raid_conf, sh, ctx);
1788 list_del_init(&sh->lru);
1789 raid5_release_stripe(sh);
1790 }
1791}
1792
1793/* if matches return 0; otherwise return -EINVAL */
1794static int
1795r5l_recovery_verify_data_checksum(struct r5l_log *log, struct page *page,
1796 sector_t log_offset, __le32 log_checksum)
1797{
1798 void *addr;
1799 u32 checksum;
1800
1801 sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1802 page, REQ_OP_READ, 0, false);
1803 addr = kmap_atomic(page);
1804 checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
1805 kunmap_atomic(addr);
1806 return (le32_to_cpu(log_checksum) == checksum) ? 0 : -EINVAL;
1807}
1808
1809/*
1810 * before loading data to stripe cache, we need verify checksum for all data,
1811 * if there is mismatch for any data page, we drop all data in the mata block
1812 */
1813static int
1814r5l_recovery_verify_data_checksum_for_mb(struct r5l_log *log,
1815 struct r5l_recovery_ctx *ctx)
1816{
1817 struct mddev *mddev = log->rdev->mddev;
1818 struct r5conf *conf = mddev->private;
1819 struct r5l_meta_block *mb = page_address(ctx->meta_page);
1820 sector_t mb_offset = sizeof(struct r5l_meta_block);
1821 sector_t log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
1822 struct page *page;
1823 struct r5l_payload_data_parity *payload;
1824
1825 page = alloc_page(GFP_KERNEL);
1826 if (!page)
1827 return -ENOMEM;
1828
1829 while (mb_offset < le32_to_cpu(mb->meta_size)) {
1830 payload = (void *)mb + mb_offset;
1831
1832 if (payload->header.type == R5LOG_PAYLOAD_DATA) {
1833 if (r5l_recovery_verify_data_checksum(
1834 log, page, log_offset,
1835 payload->checksum[0]) < 0)
1836 goto mismatch;
1837 } else if (payload->header.type == R5LOG_PAYLOAD_PARITY) {
1838 if (r5l_recovery_verify_data_checksum(
1839 log, page, log_offset,
1840 payload->checksum[0]) < 0)
1841 goto mismatch;
1842 if (conf->max_degraded == 2 && /* q for RAID 6 */
1843 r5l_recovery_verify_data_checksum(
1844 log, page,
1845 r5l_ring_add(log, log_offset,
1846 BLOCK_SECTORS),
1847 payload->checksum[1]) < 0)
1848 goto mismatch;
1849 } else /* not R5LOG_PAYLOAD_DATA or R5LOG_PAYLOAD_PARITY */
1850 goto mismatch;
1851
1852 log_offset = r5l_ring_add(log, log_offset,
1853 le32_to_cpu(payload->size));
1854
1855 mb_offset += sizeof(struct r5l_payload_data_parity) +
1856 sizeof(__le32) *
1857 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
1858 }
1859
1860 put_page(page);
1861 return 0;
1862
1863mismatch:
1864 put_page(page);
1865 return -EINVAL;
1866}
1867
1868/*
1869 * Analyze all data/parity pages in one meta block
1870 * Returns:
1871 * 0 for success
1872 * -EINVAL for unknown playload type
1873 * -EAGAIN for checksum mismatch of data page
1874 * -ENOMEM for run out of memory (alloc_page failed or run out of stripes)
1875 */
1876static int
1877r5c_recovery_analyze_meta_block(struct r5l_log *log,
1878 struct r5l_recovery_ctx *ctx,
1879 struct list_head *cached_stripe_list)
1880{
1881 struct mddev *mddev = log->rdev->mddev;
1882 struct r5conf *conf = mddev->private;
1883 struct r5l_meta_block *mb;
1884 struct r5l_payload_data_parity *payload;
1885 int mb_offset;
1886 sector_t log_offset;
1887 sector_t stripe_sect;
1888 struct stripe_head *sh;
1889 int ret;
1890
1891 /*
1892 * for mismatch in data blocks, we will drop all data in this mb, but
1893 * we will still read next mb for other data with FLUSH flag, as
1894 * io_unit could finish out of order.
1895 */
1896 ret = r5l_recovery_verify_data_checksum_for_mb(log, ctx);
1897 if (ret == -EINVAL)
1898 return -EAGAIN;
1899 else if (ret)
1900 return ret; /* -ENOMEM duo to alloc_page() failed */
1901
1902 mb = page_address(ctx->meta_page);
1903 mb_offset = sizeof(struct r5l_meta_block);
1904 log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
1905
1906 while (mb_offset < le32_to_cpu(mb->meta_size)) {
1907 int dd;
1908
1909 payload = (void *)mb + mb_offset;
1910 stripe_sect = (payload->header.type == R5LOG_PAYLOAD_DATA) ?
1911 raid5_compute_sector(
1912 conf, le64_to_cpu(payload->location), 0, &dd,
1913 NULL)
1914 : le64_to_cpu(payload->location);
1915
1916 sh = r5c_recovery_lookup_stripe(cached_stripe_list,
1917 stripe_sect);
1918
1919 if (!sh) {
Song Liu3c66abb2016-12-14 15:38:01 -08001920 sh = r5c_recovery_alloc_stripe(conf, stripe_sect);
Song Liub4c625c2016-11-17 15:24:43 -08001921 /*
1922 * cannot get stripe from raid5_get_active_stripe
1923 * try replay some stripes
1924 */
1925 if (!sh) {
1926 r5c_recovery_replay_stripes(
1927 cached_stripe_list, ctx);
1928 sh = r5c_recovery_alloc_stripe(
Song Liu3c66abb2016-12-14 15:38:01 -08001929 conf, stripe_sect);
Song Liub4c625c2016-11-17 15:24:43 -08001930 }
1931 if (!sh) {
1932 pr_debug("md/raid:%s: Increasing stripe cache size to %d to recovery data on journal.\n",
1933 mdname(mddev),
1934 conf->min_nr_stripes * 2);
1935 raid5_set_cache_size(mddev,
1936 conf->min_nr_stripes * 2);
Song Liu3c66abb2016-12-14 15:38:01 -08001937 sh = r5c_recovery_alloc_stripe(conf,
1938 stripe_sect);
Song Liub4c625c2016-11-17 15:24:43 -08001939 }
1940 if (!sh) {
1941 pr_err("md/raid:%s: Cannot get enough stripes due to memory pressure. Recovery failed.\n",
1942 mdname(mddev));
1943 return -ENOMEM;
1944 }
1945 list_add_tail(&sh->lru, cached_stripe_list);
1946 }
1947
1948 if (payload->header.type == R5LOG_PAYLOAD_DATA) {
Zhengyuan Liuf7b7bee2016-11-26 10:57:13 +08001949 if (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
1950 test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags)) {
Song Liub4c625c2016-11-17 15:24:43 -08001951 r5l_recovery_replay_one_stripe(conf, sh, ctx);
Song Liub4c625c2016-11-17 15:24:43 -08001952 list_move_tail(&sh->lru, cached_stripe_list);
1953 }
1954 r5l_recovery_load_data(log, sh, ctx, payload,
1955 log_offset);
1956 } else if (payload->header.type == R5LOG_PAYLOAD_PARITY)
1957 r5l_recovery_load_parity(log, sh, ctx, payload,
1958 log_offset);
1959 else
1960 return -EINVAL;
1961
1962 log_offset = r5l_ring_add(log, log_offset,
1963 le32_to_cpu(payload->size));
1964
1965 mb_offset += sizeof(struct r5l_payload_data_parity) +
1966 sizeof(__le32) *
1967 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
1968 }
1969
1970 return 0;
1971}
1972
1973/*
1974 * Load the stripe into cache. The stripe will be written out later by
1975 * the stripe cache state machine.
1976 */
1977static void r5c_recovery_load_one_stripe(struct r5l_log *log,
1978 struct stripe_head *sh)
1979{
Song Liub4c625c2016-11-17 15:24:43 -08001980 struct r5dev *dev;
1981 int i;
1982
1983 for (i = sh->disks; i--; ) {
1984 dev = sh->dev + i;
1985 if (test_and_clear_bit(R5_Wantwrite, &dev->flags)) {
1986 set_bit(R5_InJournal, &dev->flags);
1987 set_bit(R5_UPTODATE, &dev->flags);
1988 }
1989 }
Song Liub4c625c2016-11-17 15:24:43 -08001990}
1991
1992/*
1993 * Scan through the log for all to-be-flushed data
1994 *
1995 * For stripes with data and parity, namely Data-Parity stripe
1996 * (STRIPE_R5C_CACHING == 0), we simply replay all the writes.
1997 *
1998 * For stripes with only data, namely Data-Only stripe
1999 * (STRIPE_R5C_CACHING == 1), we load them to stripe cache state machine.
2000 *
2001 * For a stripe, if we see data after parity, we should discard all previous
2002 * data and parity for this stripe, as these data are already flushed to
2003 * the array.
2004 *
2005 * At the end of the scan, we return the new journal_tail, which points to
2006 * first data-only stripe on the journal device, or next invalid meta block.
2007 */
2008static int r5c_recovery_flush_log(struct r5l_log *log,
2009 struct r5l_recovery_ctx *ctx)
2010{
JackieLiubc8f1672016-11-28 16:19:20 +08002011 struct stripe_head *sh;
Song Liub4c625c2016-11-17 15:24:43 -08002012 int ret = 0;
2013
2014 /* scan through the log */
2015 while (1) {
2016 if (r5l_recovery_read_meta_block(log, ctx))
2017 break;
2018
2019 ret = r5c_recovery_analyze_meta_block(log, ctx,
2020 &ctx->cached_list);
2021 /*
2022 * -EAGAIN means mismatch in data block, in this case, we still
2023 * try scan the next metablock
2024 */
2025 if (ret && ret != -EAGAIN)
2026 break; /* ret == -EINVAL or -ENOMEM */
2027 ctx->seq++;
2028 ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
2029 }
2030
2031 if (ret == -ENOMEM) {
2032 r5c_recovery_drop_stripes(&ctx->cached_list, ctx);
2033 return ret;
2034 }
2035
2036 /* replay data-parity stripes */
2037 r5c_recovery_replay_stripes(&ctx->cached_list, ctx);
2038
2039 /* load data-only stripes to stripe cache */
JackieLiubc8f1672016-11-28 16:19:20 +08002040 list_for_each_entry(sh, &ctx->cached_list, lru) {
Song Liub4c625c2016-11-17 15:24:43 -08002041 WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
2042 r5c_recovery_load_one_stripe(log, sh);
Song Liub4c625c2016-11-17 15:24:43 -08002043 ctx->data_only_stripes++;
2044 }
2045
2046 return 0;
2047}
2048
2049/*
2050 * we did a recovery. Now ctx.pos points to an invalid meta block. New
2051 * log will start here. but we can't let superblock point to last valid
2052 * meta block. The log might looks like:
2053 * | meta 1| meta 2| meta 3|
2054 * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
2055 * superblock points to meta 1, we write a new valid meta 2n. if crash
2056 * happens again, new recovery will start from meta 1. Since meta 2n is
2057 * valid now, recovery will think meta 3 is valid, which is wrong.
2058 * The solution is we create a new meta in meta2 with its seq == meta
Song Liu3c6edc62016-12-07 09:42:06 -08002059 * 1's seq + 10000 and let superblock points to meta2. The same recovery
2060 * will not think meta 3 is a valid meta, because its seq doesn't match
Song Liub4c625c2016-11-17 15:24:43 -08002061 */
2062
2063/*
2064 * Before recovery, the log looks like the following
2065 *
2066 * ---------------------------------------------
2067 * | valid log | invalid log |
2068 * ---------------------------------------------
2069 * ^
2070 * |- log->last_checkpoint
2071 * |- log->last_cp_seq
2072 *
2073 * Now we scan through the log until we see invalid entry
2074 *
2075 * ---------------------------------------------
2076 * | valid log | invalid log |
2077 * ---------------------------------------------
2078 * ^ ^
2079 * |- log->last_checkpoint |- ctx->pos
2080 * |- log->last_cp_seq |- ctx->seq
2081 *
2082 * From this point, we need to increase seq number by 10 to avoid
2083 * confusing next recovery.
2084 *
2085 * ---------------------------------------------
2086 * | valid log | invalid log |
2087 * ---------------------------------------------
2088 * ^ ^
2089 * |- log->last_checkpoint |- ctx->pos+1
Song Liu3c6edc62016-12-07 09:42:06 -08002090 * |- log->last_cp_seq |- ctx->seq+10001
Song Liub4c625c2016-11-17 15:24:43 -08002091 *
2092 * However, it is not safe to start the state machine yet, because data only
2093 * parities are not yet secured in RAID. To save these data only parities, we
2094 * rewrite them from seq+11.
2095 *
2096 * -----------------------------------------------------------------
2097 * | valid log | data only stripes | invalid log |
2098 * -----------------------------------------------------------------
2099 * ^ ^
2100 * |- log->last_checkpoint |- ctx->pos+n
Song Liu3c6edc62016-12-07 09:42:06 -08002101 * |- log->last_cp_seq |- ctx->seq+10000+n
Song Liub4c625c2016-11-17 15:24:43 -08002102 *
2103 * If failure happens again during this process, the recovery can safe start
2104 * again from log->last_checkpoint.
2105 *
2106 * Once data only stripes are rewritten to journal, we move log_tail
2107 *
2108 * -----------------------------------------------------------------
2109 * | old log | data only stripes | invalid log |
2110 * -----------------------------------------------------------------
2111 * ^ ^
2112 * |- log->last_checkpoint |- ctx->pos+n
Song Liu3c6edc62016-12-07 09:42:06 -08002113 * |- log->last_cp_seq |- ctx->seq+10000+n
Song Liub4c625c2016-11-17 15:24:43 -08002114 *
2115 * Then we can safely start the state machine. If failure happens from this
2116 * point on, the recovery will start from new log->last_checkpoint.
2117 */
2118static int
2119r5c_recovery_rewrite_data_only_stripes(struct r5l_log *log,
2120 struct r5l_recovery_ctx *ctx)
2121{
Song Liua85dd7b2017-01-23 17:12:57 -08002122 struct stripe_head *sh;
Song Liub4c625c2016-11-17 15:24:43 -08002123 struct mddev *mddev = log->rdev->mddev;
2124 struct page *page;
Song Liu3c66abb2016-12-14 15:38:01 -08002125 sector_t next_checkpoint = MaxSector;
Song Liub4c625c2016-11-17 15:24:43 -08002126
2127 page = alloc_page(GFP_KERNEL);
2128 if (!page) {
2129 pr_err("md/raid:%s: cannot allocate memory to rewrite data only stripes\n",
2130 mdname(mddev));
2131 return -ENOMEM;
2132 }
2133
Song Liu3c66abb2016-12-14 15:38:01 -08002134 WARN_ON(list_empty(&ctx->cached_list));
2135
Song Liua85dd7b2017-01-23 17:12:57 -08002136 list_for_each_entry(sh, &ctx->cached_list, lru) {
Song Liub4c625c2016-11-17 15:24:43 -08002137 struct r5l_meta_block *mb;
2138 int i;
2139 int offset;
2140 sector_t write_pos;
2141
2142 WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
2143 r5l_recovery_create_empty_meta_block(log, page,
2144 ctx->pos, ctx->seq);
2145 mb = page_address(page);
2146 offset = le32_to_cpu(mb->meta_size);
JackieLiufc833c22016-11-28 16:19:19 +08002147 write_pos = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
Song Liub4c625c2016-11-17 15:24:43 -08002148
2149 for (i = sh->disks; i--; ) {
2150 struct r5dev *dev = &sh->dev[i];
2151 struct r5l_payload_data_parity *payload;
2152 void *addr;
2153
2154 if (test_bit(R5_InJournal, &dev->flags)) {
2155 payload = (void *)mb + offset;
2156 payload->header.type = cpu_to_le16(
2157 R5LOG_PAYLOAD_DATA);
2158 payload->size = BLOCK_SECTORS;
2159 payload->location = cpu_to_le64(
2160 raid5_compute_blocknr(sh, i, 0));
2161 addr = kmap_atomic(dev->page);
2162 payload->checksum[0] = cpu_to_le32(
2163 crc32c_le(log->uuid_checksum, addr,
2164 PAGE_SIZE));
2165 kunmap_atomic(addr);
2166 sync_page_io(log->rdev, write_pos, PAGE_SIZE,
2167 dev->page, REQ_OP_WRITE, 0, false);
2168 write_pos = r5l_ring_add(log, write_pos,
2169 BLOCK_SECTORS);
2170 offset += sizeof(__le32) +
2171 sizeof(struct r5l_payload_data_parity);
2172
2173 }
2174 }
2175 mb->meta_size = cpu_to_le32(offset);
Song Liu5c88f402016-12-07 09:42:05 -08002176 mb->checksum = cpu_to_le32(crc32c_le(log->uuid_checksum,
2177 mb, PAGE_SIZE));
Song Liub4c625c2016-11-17 15:24:43 -08002178 sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page,
Shaohua Li20737732016-12-13 12:40:15 -08002179 REQ_OP_WRITE, REQ_FUA, false);
Song Liub4c625c2016-11-17 15:24:43 -08002180 sh->log_start = ctx->pos;
Song Liu3c66abb2016-12-14 15:38:01 -08002181 list_add_tail(&sh->r5c, &log->stripe_in_journal_list);
2182 atomic_inc(&log->stripe_in_journal_count);
Song Liub4c625c2016-11-17 15:24:43 -08002183 ctx->pos = write_pos;
2184 ctx->seq += 1;
Song Liu3c66abb2016-12-14 15:38:01 -08002185 next_checkpoint = sh->log_start;
Song Liub4c625c2016-11-17 15:24:43 -08002186 }
Song Liu3c66abb2016-12-14 15:38:01 -08002187 log->next_checkpoint = next_checkpoint;
Song Liub4c625c2016-11-17 15:24:43 -08002188 __free_page(page);
2189 return 0;
2190}
2191
Song Liua85dd7b2017-01-23 17:12:57 -08002192static void r5c_recovery_flush_data_only_stripes(struct r5l_log *log,
2193 struct r5l_recovery_ctx *ctx)
2194{
2195 struct mddev *mddev = log->rdev->mddev;
2196 struct r5conf *conf = mddev->private;
2197 struct stripe_head *sh, *next;
2198
2199 if (ctx->data_only_stripes == 0)
2200 return;
2201
2202 log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_BACK;
2203
2204 list_for_each_entry_safe(sh, next, &ctx->cached_list, lru) {
2205 r5c_make_stripe_write_out(sh);
2206 set_bit(STRIPE_HANDLE, &sh->state);
2207 list_del_init(&sh->lru);
2208 raid5_release_stripe(sh);
2209 }
2210
2211 md_wakeup_thread(conf->mddev->thread);
2212 /* reuse conf->wait_for_quiescent in recovery */
2213 wait_event(conf->wait_for_quiescent,
2214 atomic_read(&conf->active_stripes) == 0);
2215
2216 log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
2217}
2218
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002219static int r5l_recovery_log(struct r5l_log *log)
2220{
Song Liu5aabf7c2016-11-17 15:24:44 -08002221 struct mddev *mddev = log->rdev->mddev;
Shaohua Li355810d2015-08-13 14:32:01 -07002222 struct r5l_recovery_ctx ctx;
Song Liu5aabf7c2016-11-17 15:24:44 -08002223 int ret;
JackieLiu43b96742016-12-05 11:58:53 +08002224 sector_t pos;
Shaohua Li355810d2015-08-13 14:32:01 -07002225
2226 ctx.pos = log->last_checkpoint;
2227 ctx.seq = log->last_cp_seq;
2228 ctx.meta_page = alloc_page(GFP_KERNEL);
Song Liub4c625c2016-11-17 15:24:43 -08002229 ctx.data_only_stripes = 0;
2230 ctx.data_parity_stripes = 0;
2231 INIT_LIST_HEAD(&ctx.cached_list);
2232
Shaohua Li355810d2015-08-13 14:32:01 -07002233 if (!ctx.meta_page)
2234 return -ENOMEM;
2235
Song Liu5aabf7c2016-11-17 15:24:44 -08002236 ret = r5c_recovery_flush_log(log, &ctx);
Shaohua Li355810d2015-08-13 14:32:01 -07002237 __free_page(ctx.meta_page);
2238
Song Liu5aabf7c2016-11-17 15:24:44 -08002239 if (ret)
2240 return ret;
Shaohua Li355810d2015-08-13 14:32:01 -07002241
Song Liu3c6edc62016-12-07 09:42:06 -08002242 pos = ctx.pos;
2243 ctx.seq += 10000;
JackieLiu43b96742016-12-05 11:58:53 +08002244
JackieLiu43b96742016-12-05 11:58:53 +08002245
Song Liu5aabf7c2016-11-17 15:24:44 -08002246 if ((ctx.data_only_stripes == 0) && (ctx.data_parity_stripes == 0))
2247 pr_debug("md/raid:%s: starting from clean shutdown\n",
2248 mdname(mddev));
Song Liua85dd7b2017-01-23 17:12:57 -08002249 else
Colin Ian King99f17892016-12-23 00:52:30 +00002250 pr_debug("md/raid:%s: recovering %d data-only stripes and %d data-parity stripes\n",
Song Liu5aabf7c2016-11-17 15:24:44 -08002251 mdname(mddev), ctx.data_only_stripes,
2252 ctx.data_parity_stripes);
2253
Song Liua85dd7b2017-01-23 17:12:57 -08002254 if (ctx.data_only_stripes == 0) {
2255 log->next_checkpoint = ctx.pos;
2256 r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq++);
2257 ctx.pos = r5l_ring_add(log, ctx.pos, BLOCK_SECTORS);
2258 } else if (r5c_recovery_rewrite_data_only_stripes(log, &ctx)) {
2259 pr_err("md/raid:%s: failed to rewrite stripes to journal\n",
2260 mdname(mddev));
2261 return -EIO;
Shaohua Li355810d2015-08-13 14:32:01 -07002262 }
Song Liub4c625c2016-11-17 15:24:43 -08002263
Song Liu5aabf7c2016-11-17 15:24:44 -08002264 log->log_start = ctx.pos;
Song Liu5aabf7c2016-11-17 15:24:44 -08002265 log->seq = ctx.seq;
JackieLiu43b96742016-12-05 11:58:53 +08002266 log->last_checkpoint = pos;
2267 r5l_write_super(log, pos);
Song Liua85dd7b2017-01-23 17:12:57 -08002268
2269 r5c_recovery_flush_data_only_stripes(log, &ctx);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002270 return 0;
2271}
2272
2273static void r5l_write_super(struct r5l_log *log, sector_t cp)
2274{
2275 struct mddev *mddev = log->rdev->mddev;
2276
2277 log->rdev->journal_tail = cp;
Shaohua Li29530792016-12-08 15:48:19 -08002278 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002279}
2280
Song Liu2c7da142016-11-17 15:24:41 -08002281static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page)
2282{
2283 struct r5conf *conf = mddev->private;
2284 int ret;
2285
2286 if (!conf->log)
2287 return 0;
2288
2289 switch (conf->log->r5c_journal_mode) {
2290 case R5C_JOURNAL_MODE_WRITE_THROUGH:
2291 ret = snprintf(
2292 page, PAGE_SIZE, "[%s] %s\n",
2293 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
2294 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
2295 break;
2296 case R5C_JOURNAL_MODE_WRITE_BACK:
2297 ret = snprintf(
2298 page, PAGE_SIZE, "%s [%s]\n",
2299 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
2300 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
2301 break;
2302 default:
2303 ret = 0;
2304 }
2305 return ret;
2306}
2307
2308static ssize_t r5c_journal_mode_store(struct mddev *mddev,
2309 const char *page, size_t length)
2310{
2311 struct r5conf *conf = mddev->private;
2312 struct r5l_log *log = conf->log;
2313 int val = -1, i;
2314 int len = length;
2315
2316 if (!log)
2317 return -ENODEV;
2318
2319 if (len && page[len - 1] == '\n')
2320 len -= 1;
2321 for (i = 0; i < ARRAY_SIZE(r5c_journal_mode_str); i++)
2322 if (strlen(r5c_journal_mode_str[i]) == len &&
2323 strncmp(page, r5c_journal_mode_str[i], len) == 0) {
2324 val = i;
2325 break;
2326 }
2327 if (val < R5C_JOURNAL_MODE_WRITE_THROUGH ||
2328 val > R5C_JOURNAL_MODE_WRITE_BACK)
2329 return -EINVAL;
2330
Song Liu2e38a372017-01-24 10:45:30 -08002331 if (raid5_calc_degraded(conf) > 0 &&
2332 val == R5C_JOURNAL_MODE_WRITE_BACK)
2333 return -EINVAL;
2334
Song Liu2c7da142016-11-17 15:24:41 -08002335 mddev_suspend(mddev);
2336 conf->log->r5c_journal_mode = val;
2337 mddev_resume(mddev);
2338
2339 pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n",
2340 mdname(mddev), val, r5c_journal_mode_str[val]);
2341 return length;
2342}
2343
2344struct md_sysfs_entry
2345r5c_journal_mode = __ATTR(journal_mode, 0644,
2346 r5c_journal_mode_show, r5c_journal_mode_store);
2347
Song Liu2ded3702016-11-17 15:24:38 -08002348/*
2349 * Try handle write operation in caching phase. This function should only
2350 * be called in write-back mode.
2351 *
2352 * If all outstanding writes can be handled in caching phase, returns 0
2353 * If writes requires write-out phase, call r5c_make_stripe_write_out()
2354 * and returns -EAGAIN
2355 */
2356int r5c_try_caching_write(struct r5conf *conf,
2357 struct stripe_head *sh,
2358 struct stripe_head_state *s,
2359 int disks)
2360{
2361 struct r5l_log *log = conf->log;
Song Liu1e6d6902016-11-17 15:24:39 -08002362 int i;
2363 struct r5dev *dev;
2364 int to_cache = 0;
Song Liu03b047f2017-01-11 13:39:14 -08002365 void **pslot;
2366 sector_t tree_index;
2367 int ret;
2368 uintptr_t refcount;
Song Liu2ded3702016-11-17 15:24:38 -08002369
2370 BUG_ON(!r5c_is_writeback(log));
2371
Song Liu1e6d6902016-11-17 15:24:39 -08002372 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
2373 /*
2374 * There are two different scenarios here:
2375 * 1. The stripe has some data cached, and it is sent to
2376 * write-out phase for reclaim
2377 * 2. The stripe is clean, and this is the first write
2378 *
2379 * For 1, return -EAGAIN, so we continue with
2380 * handle_stripe_dirtying().
2381 *
2382 * For 2, set STRIPE_R5C_CACHING and continue with caching
2383 * write.
2384 */
2385
2386 /* case 1: anything injournal or anything in written */
2387 if (s->injournal > 0 || s->written > 0)
2388 return -EAGAIN;
2389 /* case 2 */
2390 set_bit(STRIPE_R5C_CACHING, &sh->state);
2391 }
2392
Song Liu2e38a372017-01-24 10:45:30 -08002393 /*
2394 * When run in degraded mode, array is set to write-through mode.
2395 * This check helps drain pending write safely in the transition to
2396 * write-through mode.
2397 */
2398 if (s->failed) {
2399 r5c_make_stripe_write_out(sh);
2400 return -EAGAIN;
2401 }
2402
Song Liu1e6d6902016-11-17 15:24:39 -08002403 for (i = disks; i--; ) {
2404 dev = &sh->dev[i];
2405 /* if non-overwrite, use writing-out phase */
2406 if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) &&
2407 !test_bit(R5_InJournal, &dev->flags)) {
2408 r5c_make_stripe_write_out(sh);
2409 return -EAGAIN;
2410 }
2411 }
2412
Song Liu03b047f2017-01-11 13:39:14 -08002413 /* if the stripe is not counted in big_stripe_tree, add it now */
2414 if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) &&
2415 !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
2416 tree_index = r5c_tree_index(conf, sh->sector);
2417 spin_lock(&log->tree_lock);
2418 pslot = radix_tree_lookup_slot(&log->big_stripe_tree,
2419 tree_index);
2420 if (pslot) {
2421 refcount = (uintptr_t)radix_tree_deref_slot_protected(
2422 pslot, &log->tree_lock) >>
2423 R5C_RADIX_COUNT_SHIFT;
2424 radix_tree_replace_slot(
2425 &log->big_stripe_tree, pslot,
2426 (void *)((refcount + 1) << R5C_RADIX_COUNT_SHIFT));
2427 } else {
2428 /*
2429 * this radix_tree_insert can fail safely, so no
2430 * need to call radix_tree_preload()
2431 */
2432 ret = radix_tree_insert(
2433 &log->big_stripe_tree, tree_index,
2434 (void *)(1 << R5C_RADIX_COUNT_SHIFT));
2435 if (ret) {
2436 spin_unlock(&log->tree_lock);
2437 r5c_make_stripe_write_out(sh);
2438 return -EAGAIN;
2439 }
2440 }
2441 spin_unlock(&log->tree_lock);
2442
2443 /*
2444 * set STRIPE_R5C_PARTIAL_STRIPE, this shows the stripe is
2445 * counted in the radix tree
2446 */
2447 set_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state);
2448 atomic_inc(&conf->r5c_cached_partial_stripes);
2449 }
2450
Song Liu1e6d6902016-11-17 15:24:39 -08002451 for (i = disks; i--; ) {
2452 dev = &sh->dev[i];
2453 if (dev->towrite) {
2454 set_bit(R5_Wantwrite, &dev->flags);
2455 set_bit(R5_Wantdrain, &dev->flags);
2456 set_bit(R5_LOCKED, &dev->flags);
2457 to_cache++;
2458 }
2459 }
2460
2461 if (to_cache) {
2462 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2463 /*
2464 * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data()
2465 * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in
2466 * r5c_handle_data_cached()
2467 */
2468 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
2469 }
2470
2471 return 0;
2472}
2473
2474/*
2475 * free extra pages (orig_page) we allocated for prexor
2476 */
2477void r5c_release_extra_page(struct stripe_head *sh)
2478{
Song Liud7bd3982016-11-23 22:50:39 -08002479 struct r5conf *conf = sh->raid_conf;
Song Liu1e6d6902016-11-17 15:24:39 -08002480 int i;
Song Liud7bd3982016-11-23 22:50:39 -08002481 bool using_disk_info_extra_page;
2482
2483 using_disk_info_extra_page =
2484 sh->dev[0].orig_page == conf->disks[0].extra_page;
Song Liu1e6d6902016-11-17 15:24:39 -08002485
2486 for (i = sh->disks; i--; )
2487 if (sh->dev[i].page != sh->dev[i].orig_page) {
2488 struct page *p = sh->dev[i].orig_page;
2489
2490 sh->dev[i].orig_page = sh->dev[i].page;
Song Liu86aa1392017-01-12 17:22:41 -08002491 clear_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2492
Song Liud7bd3982016-11-23 22:50:39 -08002493 if (!using_disk_info_extra_page)
2494 put_page(p);
Song Liu1e6d6902016-11-17 15:24:39 -08002495 }
Song Liud7bd3982016-11-23 22:50:39 -08002496
2497 if (using_disk_info_extra_page) {
2498 clear_bit(R5C_EXTRA_PAGE_IN_USE, &conf->cache_state);
2499 md_wakeup_thread(conf->mddev->thread);
2500 }
2501}
2502
2503void r5c_use_extra_page(struct stripe_head *sh)
2504{
2505 struct r5conf *conf = sh->raid_conf;
2506 int i;
2507 struct r5dev *dev;
2508
2509 for (i = sh->disks; i--; ) {
2510 dev = &sh->dev[i];
2511 if (dev->orig_page != dev->page)
2512 put_page(dev->orig_page);
2513 dev->orig_page = conf->disks[i].extra_page;
2514 }
Song Liu2ded3702016-11-17 15:24:38 -08002515}
2516
2517/*
2518 * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the
2519 * stripe is committed to RAID disks.
2520 */
2521void r5c_finish_stripe_write_out(struct r5conf *conf,
2522 struct stripe_head *sh,
2523 struct stripe_head_state *s)
2524{
Song Liu03b047f2017-01-11 13:39:14 -08002525 struct r5l_log *log = conf->log;
Song Liu1e6d6902016-11-17 15:24:39 -08002526 int i;
2527 int do_wakeup = 0;
Song Liu03b047f2017-01-11 13:39:14 -08002528 sector_t tree_index;
2529 void **pslot;
2530 uintptr_t refcount;
Song Liu1e6d6902016-11-17 15:24:39 -08002531
Song Liu03b047f2017-01-11 13:39:14 -08002532 if (!log || !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags))
Song Liu2ded3702016-11-17 15:24:38 -08002533 return;
2534
2535 WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
2536 clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
2537
Song Liu03b047f2017-01-11 13:39:14 -08002538 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
Song Liu2ded3702016-11-17 15:24:38 -08002539 return;
Song Liu1e6d6902016-11-17 15:24:39 -08002540
2541 for (i = sh->disks; i--; ) {
2542 clear_bit(R5_InJournal, &sh->dev[i].flags);
2543 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2544 do_wakeup = 1;
2545 }
2546
2547 /*
2548 * analyse_stripe() runs before r5c_finish_stripe_write_out(),
2549 * We updated R5_InJournal, so we also update s->injournal.
2550 */
2551 s->injournal = 0;
2552
2553 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2554 if (atomic_dec_and_test(&conf->pending_full_writes))
2555 md_wakeup_thread(conf->mddev->thread);
2556
2557 if (do_wakeup)
2558 wake_up(&conf->wait_for_overlap);
Song Liua39f7af2016-11-17 15:24:40 -08002559
Song Liu03b047f2017-01-11 13:39:14 -08002560 spin_lock_irq(&log->stripe_in_journal_lock);
Song Liua39f7af2016-11-17 15:24:40 -08002561 list_del_init(&sh->r5c);
Song Liu03b047f2017-01-11 13:39:14 -08002562 spin_unlock_irq(&log->stripe_in_journal_lock);
Song Liua39f7af2016-11-17 15:24:40 -08002563 sh->log_start = MaxSector;
Song Liu03b047f2017-01-11 13:39:14 -08002564
2565 atomic_dec(&log->stripe_in_journal_count);
2566 r5c_update_log_state(log);
2567
2568 /* stop counting this stripe in big_stripe_tree */
2569 if (test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) ||
2570 test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
2571 tree_index = r5c_tree_index(conf, sh->sector);
2572 spin_lock(&log->tree_lock);
2573 pslot = radix_tree_lookup_slot(&log->big_stripe_tree,
2574 tree_index);
2575 BUG_ON(pslot == NULL);
2576 refcount = (uintptr_t)radix_tree_deref_slot_protected(
2577 pslot, &log->tree_lock) >>
2578 R5C_RADIX_COUNT_SHIFT;
2579 if (refcount == 1)
2580 radix_tree_delete(&log->big_stripe_tree, tree_index);
2581 else
2582 radix_tree_replace_slot(
2583 &log->big_stripe_tree, pslot,
2584 (void *)((refcount - 1) << R5C_RADIX_COUNT_SHIFT));
2585 spin_unlock(&log->tree_lock);
2586 }
2587
2588 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) {
2589 BUG_ON(atomic_read(&conf->r5c_cached_partial_stripes) == 0);
2590 atomic_dec(&conf->r5c_cached_partial_stripes);
2591 }
2592
2593 if (test_and_clear_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
2594 BUG_ON(atomic_read(&conf->r5c_cached_full_stripes) == 0);
2595 atomic_dec(&conf->r5c_cached_full_stripes);
2596 }
Song Liu1e6d6902016-11-17 15:24:39 -08002597}
2598
2599int
2600r5c_cache_data(struct r5l_log *log, struct stripe_head *sh,
2601 struct stripe_head_state *s)
2602{
Song Liua39f7af2016-11-17 15:24:40 -08002603 struct r5conf *conf = sh->raid_conf;
Song Liu1e6d6902016-11-17 15:24:39 -08002604 int pages = 0;
2605 int reserve;
2606 int i;
2607 int ret = 0;
2608
2609 BUG_ON(!log);
2610
2611 for (i = 0; i < sh->disks; i++) {
2612 void *addr;
2613
2614 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
2615 continue;
2616 addr = kmap_atomic(sh->dev[i].page);
2617 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
2618 addr, PAGE_SIZE);
2619 kunmap_atomic(addr);
2620 pages++;
2621 }
2622 WARN_ON(pages == 0);
2623
2624 /*
2625 * The stripe must enter state machine again to call endio, so
2626 * don't delay.
2627 */
2628 clear_bit(STRIPE_DELAYED, &sh->state);
2629 atomic_inc(&sh->count);
2630
2631 mutex_lock(&log->io_mutex);
2632 /* meta + data */
2633 reserve = (1 + pages) << (PAGE_SHIFT - 9);
Song Liu1e6d6902016-11-17 15:24:39 -08002634
Song Liua39f7af2016-11-17 15:24:40 -08002635 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
2636 sh->log_start == MaxSector)
2637 r5l_add_no_space_stripe(log, sh);
2638 else if (!r5l_has_free_space(log, reserve)) {
2639 if (sh->log_start == log->last_checkpoint)
2640 BUG();
2641 else
2642 r5l_add_no_space_stripe(log, sh);
Song Liu1e6d6902016-11-17 15:24:39 -08002643 } else {
2644 ret = r5l_log_stripe(log, sh, pages, 0);
2645 if (ret) {
2646 spin_lock_irq(&log->io_list_lock);
2647 list_add_tail(&sh->log_list, &log->no_mem_stripes);
2648 spin_unlock_irq(&log->io_list_lock);
2649 }
2650 }
2651
2652 mutex_unlock(&log->io_mutex);
2653 return 0;
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002654}
2655
Song Liu03b047f2017-01-11 13:39:14 -08002656/* check whether this big stripe is in write back cache. */
2657bool r5c_big_stripe_cached(struct r5conf *conf, sector_t sect)
2658{
2659 struct r5l_log *log = conf->log;
2660 sector_t tree_index;
2661 void *slot;
2662
2663 if (!log)
2664 return false;
2665
2666 WARN_ON_ONCE(!rcu_read_lock_held());
2667 tree_index = r5c_tree_index(conf, sect);
2668 slot = radix_tree_lookup(&log->big_stripe_tree, tree_index);
2669 return slot != NULL;
2670}
2671
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002672static int r5l_load_log(struct r5l_log *log)
2673{
2674 struct md_rdev *rdev = log->rdev;
2675 struct page *page;
2676 struct r5l_meta_block *mb;
2677 sector_t cp = log->rdev->journal_tail;
2678 u32 stored_crc, expected_crc;
2679 bool create_super = false;
JackieLiud30dfeb2016-12-08 08:47:39 +08002680 int ret = 0;
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002681
2682 /* Make sure it's valid */
2683 if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
2684 cp = 0;
2685 page = alloc_page(GFP_KERNEL);
2686 if (!page)
2687 return -ENOMEM;
2688
Mike Christie796a5cf2016-06-05 14:32:07 -05002689 if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) {
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002690 ret = -EIO;
2691 goto ioerr;
2692 }
2693 mb = page_address(page);
2694
2695 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
2696 mb->version != R5LOG_VERSION) {
2697 create_super = true;
2698 goto create;
2699 }
2700 stored_crc = le32_to_cpu(mb->checksum);
2701 mb->checksum = 0;
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07002702 expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002703 if (stored_crc != expected_crc) {
2704 create_super = true;
2705 goto create;
2706 }
2707 if (le64_to_cpu(mb->position) != cp) {
2708 create_super = true;
2709 goto create;
2710 }
2711create:
2712 if (create_super) {
2713 log->last_cp_seq = prandom_u32();
2714 cp = 0;
Zhengyuan Liu56056c22016-10-24 16:15:59 +08002715 r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002716 /*
2717 * Make sure super points to correct address. Log might have
2718 * data very soon. If super hasn't correct log tail address,
2719 * recovery can't find the log
2720 */
2721 r5l_write_super(log, cp);
2722 } else
2723 log->last_cp_seq = le64_to_cpu(mb->seq);
2724
2725 log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
Shaohua Li0576b1c2015-08-13 14:32:00 -07002726 log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
2727 if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
2728 log->max_free_space = RECLAIM_MAX_FREE_SPACE;
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002729 log->last_checkpoint = cp;
2730
2731 __free_page(page);
2732
JackieLiud30dfeb2016-12-08 08:47:39 +08002733 if (create_super) {
2734 log->log_start = r5l_ring_add(log, cp, BLOCK_SECTORS);
2735 log->seq = log->last_cp_seq + 1;
2736 log->next_checkpoint = cp;
2737 } else
2738 ret = r5l_recovery_log(log);
2739
Zhengyuan Liu3d7e7e12016-12-04 16:49:44 +08002740 r5c_update_log_state(log);
2741 return ret;
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002742ioerr:
2743 __free_page(page);
2744 return ret;
2745}
2746
Song Liu2e38a372017-01-24 10:45:30 -08002747void r5c_update_on_rdev_error(struct mddev *mddev)
2748{
2749 struct r5conf *conf = mddev->private;
2750 struct r5l_log *log = conf->log;
2751
2752 if (!log)
2753 return;
2754
2755 if (raid5_calc_degraded(conf) > 0 &&
2756 conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK)
2757 schedule_work(&log->disable_writeback_work);
2758}
2759
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002760int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
2761{
Jens Axboec888a8f2016-04-13 13:33:19 -06002762 struct request_queue *q = bdev_get_queue(rdev->bdev);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002763 struct r5l_log *log;
2764
2765 if (PAGE_SIZE != 4096)
2766 return -EINVAL;
Song Liuc757ec92016-11-17 15:24:36 -08002767
2768 /*
2769 * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and
2770 * raid_disks r5l_payload_data_parity.
2771 *
2772 * Write journal and cache does not work for very big array
2773 * (raid_disks > 203)
2774 */
2775 if (sizeof(struct r5l_meta_block) +
2776 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) *
2777 conf->raid_disks) > PAGE_SIZE) {
2778 pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n",
2779 mdname(conf->mddev), conf->raid_disks);
2780 return -EINVAL;
2781 }
2782
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002783 log = kzalloc(sizeof(*log), GFP_KERNEL);
2784 if (!log)
2785 return -ENOMEM;
2786 log->rdev = rdev;
2787
Jens Axboec888a8f2016-04-13 13:33:19 -06002788 log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0;
Christoph Hellwig56fef7c2015-10-05 09:31:09 +02002789
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07002790 log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
2791 sizeof(rdev->mddev->uuid));
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002792
2793 mutex_init(&log->io_mutex);
2794
2795 spin_lock_init(&log->io_list_lock);
2796 INIT_LIST_HEAD(&log->running_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -07002797 INIT_LIST_HEAD(&log->io_end_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07002798 INIT_LIST_HEAD(&log->flushing_ios);
Christoph Hellwig04732f72015-10-05 09:31:07 +02002799 INIT_LIST_HEAD(&log->finished_ios);
Ming Lei3a83f462016-11-22 08:57:21 -07002800 bio_init(&log->flush_bio, NULL, 0);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002801
2802 log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
2803 if (!log->io_kc)
2804 goto io_kc;
2805
Christoph Hellwig5036c3902015-12-21 10:51:02 +11002806 log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc);
2807 if (!log->io_pool)
2808 goto io_pool;
2809
Christoph Hellwigc38d29b2015-12-21 10:51:02 +11002810 log->bs = bioset_create(R5L_POOL_SIZE, 0);
2811 if (!log->bs)
2812 goto io_bs;
2813
Christoph Hellwige8deb632015-12-21 10:51:02 +11002814 log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
2815 if (!log->meta_pool)
2816 goto out_mempool;
2817
Song Liu03b047f2017-01-11 13:39:14 -08002818 spin_lock_init(&log->tree_lock);
2819 INIT_RADIX_TREE(&log->big_stripe_tree, GFP_NOWAIT | __GFP_NOWARN);
2820
Shaohua Li0576b1c2015-08-13 14:32:00 -07002821 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
2822 log->rdev->mddev, "reclaim");
2823 if (!log->reclaim_thread)
2824 goto reclaim_thread;
Song Liua39f7af2016-11-17 15:24:40 -08002825 log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
2826
Shaohua Li0fd22b42015-09-02 13:49:47 -07002827 init_waitqueue_head(&log->iounit_wait);
Shaohua Li0576b1c2015-08-13 14:32:00 -07002828
Christoph Hellwig5036c3902015-12-21 10:51:02 +11002829 INIT_LIST_HEAD(&log->no_mem_stripes);
2830
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002831 INIT_LIST_HEAD(&log->no_space_stripes);
2832 spin_lock_init(&log->no_space_stripes_lock);
2833
Song Liu3bddb7f2016-11-18 16:46:50 -08002834 INIT_WORK(&log->deferred_io_work, r5l_submit_io_async);
Song Liu2e38a372017-01-24 10:45:30 -08002835 INIT_WORK(&log->disable_writeback_work, r5c_disable_writeback_async);
Song Liu3bddb7f2016-11-18 16:46:50 -08002836
Song Liu2ded3702016-11-17 15:24:38 -08002837 log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
Song Liua39f7af2016-11-17 15:24:40 -08002838 INIT_LIST_HEAD(&log->stripe_in_journal_list);
2839 spin_lock_init(&log->stripe_in_journal_lock);
2840 atomic_set(&log->stripe_in_journal_count, 0);
Song Liu2ded3702016-11-17 15:24:38 -08002841
Song Liud2250f12016-12-14 15:38:02 -08002842 rcu_assign_pointer(conf->log, log);
2843
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002844 if (r5l_load_log(log))
2845 goto error;
2846
Shaohua Lia62ab492016-01-06 14:37:13 -08002847 set_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002848 return 0;
Christoph Hellwige8deb632015-12-21 10:51:02 +11002849
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002850error:
Song Liud2250f12016-12-14 15:38:02 -08002851 rcu_assign_pointer(conf->log, NULL);
Shaohua Li0576b1c2015-08-13 14:32:00 -07002852 md_unregister_thread(&log->reclaim_thread);
2853reclaim_thread:
Christoph Hellwige8deb632015-12-21 10:51:02 +11002854 mempool_destroy(log->meta_pool);
2855out_mempool:
Christoph Hellwigc38d29b2015-12-21 10:51:02 +11002856 bioset_free(log->bs);
2857io_bs:
Christoph Hellwig5036c3902015-12-21 10:51:02 +11002858 mempool_destroy(log->io_pool);
2859io_pool:
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002860 kmem_cache_destroy(log->io_kc);
2861io_kc:
2862 kfree(log);
2863 return -EINVAL;
2864}
2865
2866void r5l_exit_log(struct r5l_log *log)
2867{
Song Liu2e38a372017-01-24 10:45:30 -08002868 flush_work(&log->disable_writeback_work);
Shaohua Li0576b1c2015-08-13 14:32:00 -07002869 md_unregister_thread(&log->reclaim_thread);
Christoph Hellwige8deb632015-12-21 10:51:02 +11002870 mempool_destroy(log->meta_pool);
Christoph Hellwigc38d29b2015-12-21 10:51:02 +11002871 bioset_free(log->bs);
Christoph Hellwig5036c3902015-12-21 10:51:02 +11002872 mempool_destroy(log->io_pool);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002873 kmem_cache_destroy(log->io_kc);
2874 kfree(log);
2875}