blob: 5ef20c57731fea83240417f37722483de8cc232e [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>
Shaohua Lif6bed0e2015-08-13 14:31:59 -070023#include "md.h"
24#include "raid5.h"
Song Liu1e6d6902016-11-17 15:24:39 -080025#include "bitmap.h"
Shaohua Lif6bed0e2015-08-13 14:31:59 -070026
27/*
28 * metadata/data stored in disk with 4k size unit (a block) regardless
29 * underneath hardware sector size. only works with PAGE_SIZE == 4096
30 */
31#define BLOCK_SECTORS (8)
32
Shaohua Li0576b1c2015-08-13 14:32:00 -070033/*
Song Liua39f7af2016-11-17 15:24:40 -080034 * log->max_free_space is min(1/4 disk size, 10G reclaimable space).
35 *
36 * In write through mode, the reclaim runs every log->max_free_space.
37 * This can prevent the recovery scans for too long
Shaohua Li0576b1c2015-08-13 14:32:00 -070038 */
39#define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
40#define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
41
Song Liua39f7af2016-11-17 15:24:40 -080042/* wake up reclaim thread periodically */
43#define R5C_RECLAIM_WAKEUP_INTERVAL (30 * HZ)
44/* start flush with these full stripes */
45#define R5C_FULL_STRIPE_FLUSH_BATCH 256
46/* reclaim stripes in groups */
47#define R5C_RECLAIM_STRIPE_GROUP (NR_STRIPE_HASH_LOCKS * 2)
48
Christoph Hellwigc38d29b2015-12-21 10:51:02 +110049/*
50 * We only need 2 bios per I/O unit to make progress, but ensure we
51 * have a few more available to not get too tight.
52 */
53#define R5L_POOL_SIZE 4
54
Song Liu2ded3702016-11-17 15:24:38 -080055/*
56 * r5c journal modes of the array: write-back or write-through.
57 * write-through mode has identical behavior as existing log only
58 * implementation.
59 */
60enum r5c_journal_mode {
61 R5C_JOURNAL_MODE_WRITE_THROUGH = 0,
62 R5C_JOURNAL_MODE_WRITE_BACK = 1,
63};
64
Song Liu2c7da142016-11-17 15:24:41 -080065static char *r5c_journal_mode_str[] = {"write-through",
66 "write-back"};
Song Liu2ded3702016-11-17 15:24:38 -080067/*
68 * raid5 cache state machine
69 *
JackieLiu9b691732016-11-28 16:19:18 +080070 * With the RAID cache, each stripe works in two phases:
Song Liu2ded3702016-11-17 15:24:38 -080071 * - caching phase
72 * - writing-out phase
73 *
74 * These two phases are controlled by bit STRIPE_R5C_CACHING:
75 * if STRIPE_R5C_CACHING == 0, the stripe is in writing-out phase
76 * if STRIPE_R5C_CACHING == 1, the stripe is in caching phase
77 *
78 * When there is no journal, or the journal is in write-through mode,
79 * the stripe is always in writing-out phase.
80 *
81 * For write-back journal, the stripe is sent to caching phase on write
82 * (r5c_try_caching_write). r5c_make_stripe_write_out() kicks off
83 * the write-out phase by clearing STRIPE_R5C_CACHING.
84 *
85 * Stripes in caching phase do not write the raid disks. Instead, all
86 * writes are committed from the log device. Therefore, a stripe in
87 * caching phase handles writes as:
88 * - write to log device
89 * - return IO
90 *
91 * Stripes in writing-out phase handle writes as:
92 * - calculate parity
93 * - write pending data and parity to journal
94 * - write data and parity to raid disks
95 * - return IO for pending writes
96 */
97
Shaohua Lif6bed0e2015-08-13 14:31:59 -070098struct r5l_log {
99 struct md_rdev *rdev;
100
101 u32 uuid_checksum;
102
103 sector_t device_size; /* log device size, round to
104 * BLOCK_SECTORS */
Shaohua Li0576b1c2015-08-13 14:32:00 -0700105 sector_t max_free_space; /* reclaim run if free space is at
106 * this size */
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700107
108 sector_t last_checkpoint; /* log tail. where recovery scan
109 * starts from */
110 u64 last_cp_seq; /* log tail sequence */
111
112 sector_t log_start; /* log head. where new data appends */
113 u64 seq; /* log head sequence */
114
Christoph Hellwig17036462015-10-05 09:31:06 +0200115 sector_t next_checkpoint;
116 u64 next_cp_seq;
117
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;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700166};
167
168/*
169 * an IO range starts from a meta data block and end at the next meta data
170 * block. The io unit's the meta data block tracks data/parity followed it. io
171 * unit is written to log disk with normal write, as we always flush log disk
172 * first and then start move data to raid disks, there is no requirement to
173 * write io unit with FLUSH/FUA
174 */
175struct r5l_io_unit {
176 struct r5l_log *log;
177
178 struct page *meta_page; /* store meta block */
179 int meta_offset; /* current offset in meta_page */
180
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700181 struct bio *current_bio;/* current_bio accepting new data */
182
183 atomic_t pending_stripe;/* how many stripes not flushed to raid */
184 u64 seq; /* seq number of the metablock */
185 sector_t log_start; /* where the io_unit starts */
186 sector_t log_end; /* where the io_unit ends */
187 struct list_head log_sibling; /* log->running_ios */
188 struct list_head stripe_list; /* stripes added to the io_unit */
189
190 int state;
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200191 bool need_split_bio;
Song Liu3bddb7f2016-11-18 16:46:50 -0800192 struct bio *split_bio;
193
194 unsigned int has_flush:1; /* include flush request */
195 unsigned int has_fua:1; /* include fua request */
196 unsigned int has_null_flush:1; /* include empty flush request */
197 /*
198 * io isn't sent yet, flush/fua request can only be submitted till it's
199 * the first IO in running_ios list
200 */
201 unsigned int io_deferred:1;
202
203 struct bio_list flush_barriers; /* size == 0 flush bios */
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700204};
205
206/* r5l_io_unit state */
207enum r5l_io_unit_state {
208 IO_UNIT_RUNNING = 0, /* accepting new IO */
209 IO_UNIT_IO_START = 1, /* io_unit bio start writing to log,
210 * don't accepting new bio */
211 IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */
Shaohua Lia8c34f92015-09-02 13:49:46 -0700212 IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700213};
214
Song Liu2ded3702016-11-17 15:24:38 -0800215bool r5c_is_writeback(struct r5l_log *log)
216{
217 return (log != NULL &&
218 log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK);
219}
220
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700221static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
222{
223 start += inc;
224 if (start >= log->device_size)
225 start = start - log->device_size;
226 return start;
227}
228
229static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
230 sector_t end)
231{
232 if (end >= start)
233 return end - start;
234 else
235 return end + log->device_size - start;
236}
237
238static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
239{
240 sector_t used_size;
241
242 used_size = r5l_ring_distance(log, log->last_checkpoint,
243 log->log_start);
244
245 return log->device_size > used_size + size;
246}
247
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700248static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
249 enum r5l_io_unit_state state)
250{
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700251 if (WARN_ON(io->state >= state))
252 return;
253 io->state = state;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700254}
255
Song Liu1e6d6902016-11-17 15:24:39 -0800256static void
257r5c_return_dev_pending_writes(struct r5conf *conf, struct r5dev *dev,
258 struct bio_list *return_bi)
259{
260 struct bio *wbi, *wbi2;
261
262 wbi = dev->written;
263 dev->written = NULL;
264 while (wbi && wbi->bi_iter.bi_sector <
265 dev->sector + STRIPE_SECTORS) {
266 wbi2 = r5_next_bio(wbi, dev->sector);
267 if (!raid5_dec_bi_active_stripes(wbi)) {
268 md_write_end(conf->mddev);
269 bio_list_add(return_bi, wbi);
270 }
271 wbi = wbi2;
272 }
273}
274
275void r5c_handle_cached_data_endio(struct r5conf *conf,
276 struct stripe_head *sh, int disks, struct bio_list *return_bi)
277{
278 int i;
279
280 for (i = sh->disks; i--; ) {
281 if (sh->dev[i].written) {
282 set_bit(R5_UPTODATE, &sh->dev[i].flags);
283 r5c_return_dev_pending_writes(conf, &sh->dev[i],
284 return_bi);
285 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
286 STRIPE_SECTORS,
287 !test_bit(STRIPE_DEGRADED, &sh->state),
288 0);
289 }
290 }
291}
292
Song Liua39f7af2016-11-17 15:24:40 -0800293/* Check whether we should flush some stripes to free up stripe cache */
294void r5c_check_stripe_cache_usage(struct r5conf *conf)
295{
296 int total_cached;
297
298 if (!r5c_is_writeback(conf->log))
299 return;
300
301 total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
302 atomic_read(&conf->r5c_cached_full_stripes);
303
304 /*
305 * The following condition is true for either of the following:
306 * - stripe cache pressure high:
307 * total_cached > 3/4 min_nr_stripes ||
308 * empty_inactive_list_nr > 0
309 * - stripe cache pressure moderate:
310 * total_cached > 1/2 min_nr_stripes
311 */
312 if (total_cached > conf->min_nr_stripes * 1 / 2 ||
313 atomic_read(&conf->empty_inactive_list_nr) > 0)
314 r5l_wake_reclaim(conf->log, 0);
315}
316
317/*
318 * flush cache when there are R5C_FULL_STRIPE_FLUSH_BATCH or more full
319 * stripes in the cache
320 */
321void r5c_check_cached_full_stripe(struct r5conf *conf)
322{
323 if (!r5c_is_writeback(conf->log))
324 return;
325
326 /*
327 * wake up reclaim for R5C_FULL_STRIPE_FLUSH_BATCH cached stripes
328 * or a full stripe (chunk size / 4k stripes).
329 */
330 if (atomic_read(&conf->r5c_cached_full_stripes) >=
331 min(R5C_FULL_STRIPE_FLUSH_BATCH,
332 conf->chunk_sectors >> STRIPE_SHIFT))
333 r5l_wake_reclaim(conf->log, 0);
334}
335
336/*
337 * Total log space (in sectors) needed to flush all data in cache
338 *
339 * Currently, writing-out phase automatically includes all pending writes
340 * to the same sector. So the reclaim of each stripe takes up to
341 * (conf->raid_disks + 1) pages of log space.
342 *
343 * To totally avoid deadlock due to log space, the code reserves
344 * (conf->raid_disks + 1) pages for each stripe in cache, which is not
345 * necessary in most cases.
346 *
347 * To improve this, we will need writing-out phase to be able to NOT include
348 * pending writes, which will reduce the requirement to
349 * (conf->max_degraded + 1) pages per stripe in cache.
350 */
351static sector_t r5c_log_required_to_flush_cache(struct r5conf *conf)
352{
353 struct r5l_log *log = conf->log;
354
355 if (!r5c_is_writeback(log))
356 return 0;
357
358 return BLOCK_SECTORS * (conf->raid_disks + 1) *
359 atomic_read(&log->stripe_in_journal_count);
360}
361
362/*
363 * evaluate log space usage and update R5C_LOG_TIGHT and R5C_LOG_CRITICAL
364 *
365 * R5C_LOG_TIGHT is set when free space on the log device is less than 3x of
366 * reclaim_required_space. R5C_LOG_CRITICAL is set when free space on the log
367 * device is less than 2x of reclaim_required_space.
368 */
369static inline void r5c_update_log_state(struct r5l_log *log)
370{
371 struct r5conf *conf = log->rdev->mddev->private;
372 sector_t free_space;
373 sector_t reclaim_space;
374
375 if (!r5c_is_writeback(log))
376 return;
377
378 free_space = r5l_ring_distance(log, log->log_start,
379 log->last_checkpoint);
380 reclaim_space = r5c_log_required_to_flush_cache(conf);
381 if (free_space < 2 * reclaim_space)
382 set_bit(R5C_LOG_CRITICAL, &conf->cache_state);
383 else
384 clear_bit(R5C_LOG_CRITICAL, &conf->cache_state);
385 if (free_space < 3 * reclaim_space)
386 set_bit(R5C_LOG_TIGHT, &conf->cache_state);
387 else
388 clear_bit(R5C_LOG_TIGHT, &conf->cache_state);
389}
390
Song Liu2ded3702016-11-17 15:24:38 -0800391/*
392 * Put the stripe into writing-out phase by clearing STRIPE_R5C_CACHING.
393 * This function should only be called in write-back mode.
394 */
Song Liua39f7af2016-11-17 15:24:40 -0800395void r5c_make_stripe_write_out(struct stripe_head *sh)
Song Liu2ded3702016-11-17 15:24:38 -0800396{
397 struct r5conf *conf = sh->raid_conf;
398 struct r5l_log *log = conf->log;
399
400 BUG_ON(!r5c_is_writeback(log));
401
402 WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
403 clear_bit(STRIPE_R5C_CACHING, &sh->state);
Song Liu1e6d6902016-11-17 15:24:39 -0800404
405 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
406 atomic_inc(&conf->preread_active_stripes);
407
408 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) {
409 BUG_ON(atomic_read(&conf->r5c_cached_partial_stripes) == 0);
410 atomic_dec(&conf->r5c_cached_partial_stripes);
411 }
412
413 if (test_and_clear_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
414 BUG_ON(atomic_read(&conf->r5c_cached_full_stripes) == 0);
415 atomic_dec(&conf->r5c_cached_full_stripes);
416 }
417}
418
419static void r5c_handle_data_cached(struct stripe_head *sh)
420{
421 int i;
422
423 for (i = sh->disks; i--; )
424 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
425 set_bit(R5_InJournal, &sh->dev[i].flags);
426 clear_bit(R5_LOCKED, &sh->dev[i].flags);
427 }
428 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
429}
430
431/*
432 * this journal write must contain full parity,
433 * it may also contain some data pages
434 */
435static void r5c_handle_parity_cached(struct stripe_head *sh)
436{
437 int i;
438
439 for (i = sh->disks; i--; )
440 if (test_bit(R5_InJournal, &sh->dev[i].flags))
441 set_bit(R5_Wantwrite, &sh->dev[i].flags);
Song Liu2ded3702016-11-17 15:24:38 -0800442}
443
444/*
445 * Setting proper flags after writing (or flushing) data and/or parity to the
446 * log device. This is called from r5l_log_endio() or r5l_log_flush_endio().
447 */
448static void r5c_finish_cache_stripe(struct stripe_head *sh)
449{
450 struct r5l_log *log = sh->raid_conf->log;
451
452 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
453 BUG_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
454 /*
455 * Set R5_InJournal for parity dev[pd_idx]. This means
456 * all data AND parity in the journal. For RAID 6, it is
457 * NOT necessary to set the flag for dev[qd_idx], as the
458 * two parities are written out together.
459 */
460 set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
Song Liu1e6d6902016-11-17 15:24:39 -0800461 } else if (test_bit(STRIPE_R5C_CACHING, &sh->state)) {
462 r5c_handle_data_cached(sh);
463 } else {
464 r5c_handle_parity_cached(sh);
465 set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
466 }
Song Liu2ded3702016-11-17 15:24:38 -0800467}
468
Christoph Hellwigd8858f42015-10-05 09:31:08 +0200469static void r5l_io_run_stripes(struct r5l_io_unit *io)
470{
471 struct stripe_head *sh, *next;
472
473 list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
474 list_del_init(&sh->log_list);
Song Liu2ded3702016-11-17 15:24:38 -0800475
476 r5c_finish_cache_stripe(sh);
477
Christoph Hellwigd8858f42015-10-05 09:31:08 +0200478 set_bit(STRIPE_HANDLE, &sh->state);
479 raid5_release_stripe(sh);
480 }
481}
482
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200483static void r5l_log_run_stripes(struct r5l_log *log)
484{
485 struct r5l_io_unit *io, *next;
486
487 assert_spin_locked(&log->io_list_lock);
488
489 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
490 /* don't change list order */
491 if (io->state < IO_UNIT_IO_END)
492 break;
493
494 list_move_tail(&io->log_sibling, &log->finished_ios);
495 r5l_io_run_stripes(io);
496 }
497}
498
Christoph Hellwig3848c0b2015-12-21 10:51:01 +1100499static void r5l_move_to_end_ios(struct r5l_log *log)
500{
501 struct r5l_io_unit *io, *next;
502
503 assert_spin_locked(&log->io_list_lock);
504
505 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
506 /* don't change list order */
507 if (io->state < IO_UNIT_IO_END)
508 break;
509 list_move_tail(&io->log_sibling, &log->io_end_ios);
510 }
511}
512
Song Liu3bddb7f2016-11-18 16:46:50 -0800513static void __r5l_stripe_write_finished(struct r5l_io_unit *io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700514static void r5l_log_endio(struct bio *bio)
515{
516 struct r5l_io_unit *io = bio->bi_private;
Song Liu3bddb7f2016-11-18 16:46:50 -0800517 struct r5l_io_unit *io_deferred;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700518 struct r5l_log *log = io->log;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700519 unsigned long flags;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700520
Shaohua Li6e74a9c2015-10-08 21:54:08 -0700521 if (bio->bi_error)
522 md_error(log->rdev->mddev, log->rdev);
523
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700524 bio_put(bio);
Christoph Hellwige8deb632015-12-21 10:51:02 +1100525 mempool_free(io->meta_page, log->meta_pool);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700526
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700527 spin_lock_irqsave(&log->io_list_lock, flags);
528 __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200529 if (log->need_cache_flush)
Christoph Hellwig3848c0b2015-12-21 10:51:01 +1100530 r5l_move_to_end_ios(log);
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200531 else
532 r5l_log_run_stripes(log);
Song Liu3bddb7f2016-11-18 16:46:50 -0800533 if (!list_empty(&log->running_ios)) {
534 /*
535 * FLUSH/FUA io_unit is deferred because of ordering, now we
536 * can dispatch it
537 */
538 io_deferred = list_first_entry(&log->running_ios,
539 struct r5l_io_unit, log_sibling);
540 if (io_deferred->io_deferred)
541 schedule_work(&log->deferred_io_work);
542 }
543
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700544 spin_unlock_irqrestore(&log->io_list_lock, flags);
545
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200546 if (log->need_cache_flush)
547 md_wakeup_thread(log->rdev->mddev->thread);
Song Liu3bddb7f2016-11-18 16:46:50 -0800548
549 if (io->has_null_flush) {
550 struct bio *bi;
551
552 WARN_ON(bio_list_empty(&io->flush_barriers));
553 while ((bi = bio_list_pop(&io->flush_barriers)) != NULL) {
554 bio_endio(bi);
555 atomic_dec(&io->pending_stripe);
556 }
557 if (atomic_read(&io->pending_stripe) == 0)
558 __r5l_stripe_write_finished(io);
559 }
560}
561
562static void r5l_do_submit_io(struct r5l_log *log, struct r5l_io_unit *io)
563{
564 unsigned long flags;
565
566 spin_lock_irqsave(&log->io_list_lock, flags);
567 __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
568 spin_unlock_irqrestore(&log->io_list_lock, flags);
569
570 if (io->has_flush)
571 bio_set_op_attrs(io->current_bio, REQ_OP_WRITE, WRITE_FLUSH);
572 if (io->has_fua)
573 bio_set_op_attrs(io->current_bio, REQ_OP_WRITE, WRITE_FUA);
574 submit_bio(io->current_bio);
575
576 if (!io->split_bio)
577 return;
578
579 if (io->has_flush)
580 bio_set_op_attrs(io->split_bio, REQ_OP_WRITE, WRITE_FLUSH);
581 if (io->has_fua)
582 bio_set_op_attrs(io->split_bio, REQ_OP_WRITE, WRITE_FUA);
583 submit_bio(io->split_bio);
584}
585
586/* deferred io_unit will be dispatched here */
587static void r5l_submit_io_async(struct work_struct *work)
588{
589 struct r5l_log *log = container_of(work, struct r5l_log,
590 deferred_io_work);
591 struct r5l_io_unit *io = NULL;
592 unsigned long flags;
593
594 spin_lock_irqsave(&log->io_list_lock, flags);
595 if (!list_empty(&log->running_ios)) {
596 io = list_first_entry(&log->running_ios, struct r5l_io_unit,
597 log_sibling);
598 if (!io->io_deferred)
599 io = NULL;
600 else
601 io->io_deferred = 0;
602 }
603 spin_unlock_irqrestore(&log->io_list_lock, flags);
604 if (io)
605 r5l_do_submit_io(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700606}
607
608static void r5l_submit_current_io(struct r5l_log *log)
609{
610 struct r5l_io_unit *io = log->current_io;
Song Liu3bddb7f2016-11-18 16:46:50 -0800611 struct bio *bio;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700612 struct r5l_meta_block *block;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700613 unsigned long flags;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700614 u32 crc;
Song Liu3bddb7f2016-11-18 16:46:50 -0800615 bool do_submit = true;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700616
617 if (!io)
618 return;
619
620 block = page_address(io->meta_page);
621 block->meta_size = cpu_to_le32(io->meta_offset);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700622 crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700623 block->checksum = cpu_to_le32(crc);
Song Liu3bddb7f2016-11-18 16:46:50 -0800624 bio = io->current_bio;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700625
626 log->current_io = NULL;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700627 spin_lock_irqsave(&log->io_list_lock, flags);
Song Liu3bddb7f2016-11-18 16:46:50 -0800628 if (io->has_flush || io->has_fua) {
629 if (io != list_first_entry(&log->running_ios,
630 struct r5l_io_unit, log_sibling)) {
631 io->io_deferred = 1;
632 do_submit = false;
633 }
634 }
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700635 spin_unlock_irqrestore(&log->io_list_lock, flags);
Song Liu3bddb7f2016-11-18 16:46:50 -0800636 if (do_submit)
637 r5l_do_submit_io(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700638}
639
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200640static struct bio *r5l_bio_alloc(struct r5l_log *log)
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200641{
Christoph Hellwigc38d29b2015-12-21 10:51:02 +1100642 struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200643
Mike Christie796a5cf2016-06-05 14:32:07 -0500644 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200645 bio->bi_bdev = log->rdev->bdev;
Christoph Hellwig1e932a32015-10-05 09:31:12 +0200646 bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200647
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200648 return bio;
649}
650
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200651static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
652{
653 log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
654
Song Liua39f7af2016-11-17 15:24:40 -0800655 r5c_update_log_state(log);
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200656 /*
657 * If we filled up the log device start from the beginning again,
658 * which will require a new bio.
659 *
660 * Note: for this to work properly the log size needs to me a multiple
661 * of BLOCK_SECTORS.
662 */
663 if (log->log_start == 0)
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200664 io->need_split_bio = true;
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200665
666 io->log_end = log->log_start;
667}
668
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700669static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
670{
671 struct r5l_io_unit *io;
672 struct r5l_meta_block *block;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700673
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100674 io = mempool_alloc(log->io_pool, GFP_ATOMIC);
675 if (!io)
676 return NULL;
677 memset(io, 0, sizeof(*io));
678
Christoph Hellwig51039cd2015-10-05 09:31:13 +0200679 io->log = log;
Christoph Hellwig51039cd2015-10-05 09:31:13 +0200680 INIT_LIST_HEAD(&io->log_sibling);
681 INIT_LIST_HEAD(&io->stripe_list);
Song Liu3bddb7f2016-11-18 16:46:50 -0800682 bio_list_init(&io->flush_barriers);
Christoph Hellwig51039cd2015-10-05 09:31:13 +0200683 io->state = IO_UNIT_RUNNING;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700684
Christoph Hellwige8deb632015-12-21 10:51:02 +1100685 io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700686 block = page_address(io->meta_page);
Christoph Hellwige8deb632015-12-21 10:51:02 +1100687 clear_page(block);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700688 block->magic = cpu_to_le32(R5LOG_MAGIC);
689 block->version = R5LOG_VERSION;
690 block->seq = cpu_to_le64(log->seq);
691 block->position = cpu_to_le64(log->log_start);
692
693 io->log_start = log->log_start;
694 io->meta_offset = sizeof(struct r5l_meta_block);
Christoph Hellwig2b8ef162015-10-05 09:31:15 +0200695 io->seq = log->seq++;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700696
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200697 io->current_bio = r5l_bio_alloc(log);
698 io->current_bio->bi_end_io = r5l_log_endio;
699 io->current_bio->bi_private = io;
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200700 bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700701
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200702 r5_reserve_log_entry(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700703
704 spin_lock_irq(&log->io_list_lock);
705 list_add_tail(&io->log_sibling, &log->running_ios);
706 spin_unlock_irq(&log->io_list_lock);
707
708 return io;
709}
710
711static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
712{
Christoph Hellwig22581f52015-10-05 09:31:10 +0200713 if (log->current_io &&
714 log->current_io->meta_offset + payload_size > PAGE_SIZE)
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700715 r5l_submit_current_io(log);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700716
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100717 if (!log->current_io) {
Christoph Hellwig22581f52015-10-05 09:31:10 +0200718 log->current_io = r5l_new_meta(log);
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100719 if (!log->current_io)
720 return -ENOMEM;
721 }
722
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700723 return 0;
724}
725
726static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
727 sector_t location,
728 u32 checksum1, u32 checksum2,
729 bool checksum2_valid)
730{
731 struct r5l_io_unit *io = log->current_io;
732 struct r5l_payload_data_parity *payload;
733
734 payload = page_address(io->meta_page) + io->meta_offset;
735 payload->header.type = cpu_to_le16(type);
736 payload->header.flags = cpu_to_le16(0);
737 payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
738 (PAGE_SHIFT - 9));
739 payload->location = cpu_to_le64(location);
740 payload->checksum[0] = cpu_to_le32(checksum1);
741 if (checksum2_valid)
742 payload->checksum[1] = cpu_to_le32(checksum2);
743
744 io->meta_offset += sizeof(struct r5l_payload_data_parity) +
745 sizeof(__le32) * (1 + !!checksum2_valid);
746}
747
748static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
749{
750 struct r5l_io_unit *io = log->current_io;
751
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200752 if (io->need_split_bio) {
Song Liu3bddb7f2016-11-18 16:46:50 -0800753 BUG_ON(io->split_bio);
754 io->split_bio = io->current_bio;
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200755 io->current_bio = r5l_bio_alloc(log);
Song Liu3bddb7f2016-11-18 16:46:50 -0800756 bio_chain(io->current_bio, io->split_bio);
757 io->need_split_bio = false;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700758 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700759
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200760 if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
761 BUG();
762
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200763 r5_reserve_log_entry(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700764}
765
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100766static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700767 int data_pages, int parity_pages)
768{
769 int i;
770 int meta_size;
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100771 int ret;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700772 struct r5l_io_unit *io;
773
774 meta_size =
775 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
776 * data_pages) +
777 sizeof(struct r5l_payload_data_parity) +
778 sizeof(__le32) * parity_pages;
779
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100780 ret = r5l_get_meta(log, meta_size);
781 if (ret)
782 return ret;
783
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700784 io = log->current_io;
785
Song Liu3bddb7f2016-11-18 16:46:50 -0800786 if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state))
787 io->has_flush = 1;
788
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700789 for (i = 0; i < sh->disks; i++) {
Song Liu1e6d6902016-11-17 15:24:39 -0800790 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
791 test_bit(R5_InJournal, &sh->dev[i].flags))
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700792 continue;
793 if (i == sh->pd_idx || i == sh->qd_idx)
794 continue;
Song Liu3bddb7f2016-11-18 16:46:50 -0800795 if (test_bit(R5_WantFUA, &sh->dev[i].flags) &&
796 log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) {
797 io->has_fua = 1;
798 /*
799 * we need to flush journal to make sure recovery can
800 * reach the data with fua flag
801 */
802 io->has_flush = 1;
803 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700804 r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
805 raid5_compute_blocknr(sh, i, 0),
806 sh->dev[i].log_checksum, 0, false);
807 r5l_append_payload_page(log, sh->dev[i].page);
808 }
809
Song Liu2ded3702016-11-17 15:24:38 -0800810 if (parity_pages == 2) {
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700811 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
812 sh->sector, sh->dev[sh->pd_idx].log_checksum,
813 sh->dev[sh->qd_idx].log_checksum, true);
814 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
815 r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
Song Liu2ded3702016-11-17 15:24:38 -0800816 } else if (parity_pages == 1) {
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700817 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
818 sh->sector, sh->dev[sh->pd_idx].log_checksum,
819 0, false);
820 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
Song Liu2ded3702016-11-17 15:24:38 -0800821 } else /* Just writing data, not parity, in caching phase */
822 BUG_ON(parity_pages != 0);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700823
824 list_add_tail(&sh->log_list, &io->stripe_list);
825 atomic_inc(&io->pending_stripe);
826 sh->log_io = io;
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100827
Song Liua39f7af2016-11-17 15:24:40 -0800828 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
829 return 0;
830
831 if (sh->log_start == MaxSector) {
832 BUG_ON(!list_empty(&sh->r5c));
833 sh->log_start = io->log_start;
834 spin_lock_irq(&log->stripe_in_journal_lock);
835 list_add_tail(&sh->r5c,
836 &log->stripe_in_journal_list);
837 spin_unlock_irq(&log->stripe_in_journal_lock);
838 atomic_inc(&log->stripe_in_journal_count);
839 }
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100840 return 0;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700841}
842
Song Liua39f7af2016-11-17 15:24:40 -0800843/* add stripe to no_space_stripes, and then wake up reclaim */
844static inline void r5l_add_no_space_stripe(struct r5l_log *log,
845 struct stripe_head *sh)
846{
847 spin_lock(&log->no_space_stripes_lock);
848 list_add_tail(&sh->log_list, &log->no_space_stripes);
849 spin_unlock(&log->no_space_stripes_lock);
850}
851
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700852/*
853 * running in raid5d, where reclaim could wait for raid5d too (when it flushes
854 * data from log to raid disks), so we shouldn't wait for reclaim here
855 */
856int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
857{
Song Liua39f7af2016-11-17 15:24:40 -0800858 struct r5conf *conf = sh->raid_conf;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700859 int write_disks = 0;
860 int data_pages, parity_pages;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700861 int reserve;
862 int i;
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100863 int ret = 0;
Song Liua39f7af2016-11-17 15:24:40 -0800864 bool wake_reclaim = false;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700865
866 if (!log)
867 return -EAGAIN;
868 /* Don't support stripe batch */
869 if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
870 test_bit(STRIPE_SYNCING, &sh->state)) {
871 /* the stripe is written to log, we start writing it to raid */
872 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
873 return -EAGAIN;
874 }
875
Song Liu2ded3702016-11-17 15:24:38 -0800876 WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
877
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700878 for (i = 0; i < sh->disks; i++) {
879 void *addr;
880
Song Liu1e6d6902016-11-17 15:24:39 -0800881 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
882 test_bit(R5_InJournal, &sh->dev[i].flags))
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700883 continue;
Song Liu1e6d6902016-11-17 15:24:39 -0800884
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700885 write_disks++;
886 /* checksum is already calculated in last run */
887 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
888 continue;
889 addr = kmap_atomic(sh->dev[i].page);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700890 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
891 addr, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700892 kunmap_atomic(addr);
893 }
894 parity_pages = 1 + !!(sh->qd_idx >= 0);
895 data_pages = write_disks - parity_pages;
896
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700897 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
Shaohua Li253f9fd42015-09-04 14:14:16 -0700898 /*
899 * The stripe must enter state machine again to finish the write, so
900 * don't delay.
901 */
902 clear_bit(STRIPE_DELAYED, &sh->state);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700903 atomic_inc(&sh->count);
904
905 mutex_lock(&log->io_mutex);
906 /* meta + data */
907 reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700908
Song Liua39f7af2016-11-17 15:24:40 -0800909 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
910 if (!r5l_has_free_space(log, reserve)) {
911 r5l_add_no_space_stripe(log, sh);
912 wake_reclaim = true;
913 } else {
914 ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
915 if (ret) {
916 spin_lock_irq(&log->io_list_lock);
917 list_add_tail(&sh->log_list,
918 &log->no_mem_stripes);
919 spin_unlock_irq(&log->io_list_lock);
920 }
921 }
922 } else { /* R5C_JOURNAL_MODE_WRITE_BACK */
923 /*
924 * log space critical, do not process stripes that are
925 * not in cache yet (sh->log_start == MaxSector).
926 */
927 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
928 sh->log_start == MaxSector) {
929 r5l_add_no_space_stripe(log, sh);
930 wake_reclaim = true;
931 reserve = 0;
932 } else if (!r5l_has_free_space(log, reserve)) {
933 if (sh->log_start == log->last_checkpoint)
934 BUG();
935 else
936 r5l_add_no_space_stripe(log, sh);
937 } else {
938 ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
939 if (ret) {
940 spin_lock_irq(&log->io_list_lock);
941 list_add_tail(&sh->log_list,
942 &log->no_mem_stripes);
943 spin_unlock_irq(&log->io_list_lock);
944 }
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100945 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700946 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700947
Christoph Hellwig5036c3902015-12-21 10:51:02 +1100948 mutex_unlock(&log->io_mutex);
Song Liua39f7af2016-11-17 15:24:40 -0800949 if (wake_reclaim)
950 r5l_wake_reclaim(log, reserve);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700951 return 0;
952}
953
954void r5l_write_stripe_run(struct r5l_log *log)
955{
956 if (!log)
957 return;
958 mutex_lock(&log->io_mutex);
959 r5l_submit_current_io(log);
960 mutex_unlock(&log->io_mutex);
961}
962
Shaohua Li828cbe92015-09-02 13:49:49 -0700963int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
964{
965 if (!log)
966 return -ENODEV;
Song Liu3bddb7f2016-11-18 16:46:50 -0800967
968 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
969 /*
970 * in write through (journal only)
971 * we flush log disk cache first, then write stripe data to
972 * raid disks. So if bio is finished, the log disk cache is
973 * flushed already. The recovery guarantees we can recovery
974 * the bio from log disk, so we don't need to flush again
975 */
976 if (bio->bi_iter.bi_size == 0) {
977 bio_endio(bio);
978 return 0;
979 }
980 bio->bi_opf &= ~REQ_PREFLUSH;
981 } else {
982 /* write back (with cache) */
983 if (bio->bi_iter.bi_size == 0) {
984 mutex_lock(&log->io_mutex);
985 r5l_get_meta(log, 0);
986 bio_list_add(&log->current_io->flush_barriers, bio);
987 log->current_io->has_flush = 1;
988 log->current_io->has_null_flush = 1;
989 atomic_inc(&log->current_io->pending_stripe);
990 r5l_submit_current_io(log);
991 mutex_unlock(&log->io_mutex);
992 return 0;
993 }
Shaohua Li828cbe92015-09-02 13:49:49 -0700994 }
Shaohua Li828cbe92015-09-02 13:49:49 -0700995 return -EAGAIN;
996}
997
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700998/* This will run after log space is reclaimed */
999static void r5l_run_no_space_stripes(struct r5l_log *log)
1000{
1001 struct stripe_head *sh;
1002
1003 spin_lock(&log->no_space_stripes_lock);
1004 while (!list_empty(&log->no_space_stripes)) {
1005 sh = list_first_entry(&log->no_space_stripes,
1006 struct stripe_head, log_list);
1007 list_del_init(&sh->log_list);
1008 set_bit(STRIPE_HANDLE, &sh->state);
1009 raid5_release_stripe(sh);
1010 }
1011 spin_unlock(&log->no_space_stripes_lock);
1012}
1013
Song Liua39f7af2016-11-17 15:24:40 -08001014/*
1015 * calculate new last_checkpoint
1016 * for write through mode, returns log->next_checkpoint
1017 * for write back, returns log_start of first sh in stripe_in_journal_list
1018 */
1019static sector_t r5c_calculate_new_cp(struct r5conf *conf)
1020{
1021 struct stripe_head *sh;
1022 struct r5l_log *log = conf->log;
1023 sector_t new_cp;
1024 unsigned long flags;
1025
1026 if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
1027 return log->next_checkpoint;
1028
1029 spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
1030 if (list_empty(&conf->log->stripe_in_journal_list)) {
1031 /* all stripes flushed */
Dan Carpenterd3014e22016-11-24 14:13:04 +03001032 spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
Song Liua39f7af2016-11-17 15:24:40 -08001033 return log->next_checkpoint;
1034 }
1035 sh = list_first_entry(&conf->log->stripe_in_journal_list,
1036 struct stripe_head, r5c);
1037 new_cp = sh->log_start;
1038 spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
1039 return new_cp;
1040}
1041
Christoph Hellwig17036462015-10-05 09:31:06 +02001042static sector_t r5l_reclaimable_space(struct r5l_log *log)
1043{
Song Liua39f7af2016-11-17 15:24:40 -08001044 struct r5conf *conf = log->rdev->mddev->private;
1045
Christoph Hellwig17036462015-10-05 09:31:06 +02001046 return r5l_ring_distance(log, log->last_checkpoint,
Song Liua39f7af2016-11-17 15:24:40 -08001047 r5c_calculate_new_cp(conf));
Christoph Hellwig17036462015-10-05 09:31:06 +02001048}
1049
Christoph Hellwig5036c3902015-12-21 10:51:02 +11001050static void r5l_run_no_mem_stripe(struct r5l_log *log)
1051{
1052 struct stripe_head *sh;
1053
1054 assert_spin_locked(&log->io_list_lock);
1055
1056 if (!list_empty(&log->no_mem_stripes)) {
1057 sh = list_first_entry(&log->no_mem_stripes,
1058 struct stripe_head, log_list);
1059 list_del_init(&sh->log_list);
1060 set_bit(STRIPE_HANDLE, &sh->state);
1061 raid5_release_stripe(sh);
1062 }
1063}
1064
Christoph Hellwig04732f72015-10-05 09:31:07 +02001065static bool r5l_complete_finished_ios(struct r5l_log *log)
Christoph Hellwig17036462015-10-05 09:31:06 +02001066{
1067 struct r5l_io_unit *io, *next;
1068 bool found = false;
1069
1070 assert_spin_locked(&log->io_list_lock);
1071
Christoph Hellwig04732f72015-10-05 09:31:07 +02001072 list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
Christoph Hellwig17036462015-10-05 09:31:06 +02001073 /* don't change list order */
1074 if (io->state < IO_UNIT_STRIPE_END)
1075 break;
1076
1077 log->next_checkpoint = io->log_start;
1078 log->next_cp_seq = io->seq;
1079
1080 list_del(&io->log_sibling);
Christoph Hellwig5036c3902015-12-21 10:51:02 +11001081 mempool_free(io, log->io_pool);
1082 r5l_run_no_mem_stripe(log);
Christoph Hellwig17036462015-10-05 09:31:06 +02001083
1084 found = true;
1085 }
1086
1087 return found;
1088}
1089
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001090static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
1091{
1092 struct r5l_log *log = io->log;
Song Liua39f7af2016-11-17 15:24:40 -08001093 struct r5conf *conf = log->rdev->mddev->private;
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001094 unsigned long flags;
1095
1096 spin_lock_irqsave(&log->io_list_lock, flags);
1097 __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
Christoph Hellwig17036462015-10-05 09:31:06 +02001098
Christoph Hellwig04732f72015-10-05 09:31:07 +02001099 if (!r5l_complete_finished_ios(log)) {
Shaohua Li85f2f9a2015-09-04 14:14:05 -07001100 spin_unlock_irqrestore(&log->io_list_lock, flags);
1101 return;
1102 }
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001103
Song Liua39f7af2016-11-17 15:24:40 -08001104 if (r5l_reclaimable_space(log) > log->max_free_space ||
1105 test_bit(R5C_LOG_TIGHT, &conf->cache_state))
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001106 r5l_wake_reclaim(log, 0);
1107
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001108 spin_unlock_irqrestore(&log->io_list_lock, flags);
1109 wake_up(&log->iounit_wait);
1110}
1111
Shaohua Li0576b1c2015-08-13 14:32:00 -07001112void r5l_stripe_write_finished(struct stripe_head *sh)
1113{
1114 struct r5l_io_unit *io;
1115
Shaohua Li0576b1c2015-08-13 14:32:00 -07001116 io = sh->log_io;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001117 sh->log_io = NULL;
1118
Christoph Hellwig509ffec2015-09-02 13:49:48 -07001119 if (io && atomic_dec_and_test(&io->pending_stripe))
1120 __r5l_stripe_write_finished(io);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001121}
1122
Shaohua Lia8c34f92015-09-02 13:49:46 -07001123static void r5l_log_flush_endio(struct bio *bio)
1124{
1125 struct r5l_log *log = container_of(bio, struct r5l_log,
1126 flush_bio);
1127 unsigned long flags;
1128 struct r5l_io_unit *io;
Shaohua Lia8c34f92015-09-02 13:49:46 -07001129
Shaohua Li6e74a9c2015-10-08 21:54:08 -07001130 if (bio->bi_error)
1131 md_error(log->rdev->mddev, log->rdev);
1132
Shaohua Lia8c34f92015-09-02 13:49:46 -07001133 spin_lock_irqsave(&log->io_list_lock, flags);
Christoph Hellwigd8858f42015-10-05 09:31:08 +02001134 list_for_each_entry(io, &log->flushing_ios, log_sibling)
1135 r5l_io_run_stripes(io);
Christoph Hellwig04732f72015-10-05 09:31:07 +02001136 list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07001137 spin_unlock_irqrestore(&log->io_list_lock, flags);
1138}
1139
Shaohua Li0576b1c2015-08-13 14:32:00 -07001140/*
1141 * Starting dispatch IO to raid.
1142 * io_unit(meta) consists of a log. There is one situation we want to avoid. A
1143 * broken meta in the middle of a log causes recovery can't find meta at the
1144 * head of log. If operations require meta at the head persistent in log, we
1145 * must make sure meta before it persistent in log too. A case is:
1146 *
1147 * stripe data/parity is in log, we start write stripe to raid disks. stripe
1148 * data/parity must be persistent in log before we do the write to raid disks.
1149 *
1150 * The solution is we restrictly maintain io_unit list order. In this case, we
1151 * only write stripes of an io_unit to raid disks till the io_unit is the first
1152 * one whose data/parity is in log.
1153 */
1154void r5l_flush_stripe_to_raid(struct r5l_log *log)
1155{
Shaohua Lia8c34f92015-09-02 13:49:46 -07001156 bool do_flush;
Christoph Hellwig56fef7c2015-10-05 09:31:09 +02001157
1158 if (!log || !log->need_cache_flush)
Shaohua Li0576b1c2015-08-13 14:32:00 -07001159 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001160
Shaohua Lia8c34f92015-09-02 13:49:46 -07001161 spin_lock_irq(&log->io_list_lock);
1162 /* flush bio is running */
1163 if (!list_empty(&log->flushing_ios)) {
1164 spin_unlock_irq(&log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001165 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001166 }
Shaohua Lia8c34f92015-09-02 13:49:46 -07001167 list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
1168 do_flush = !list_empty(&log->flushing_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001169 spin_unlock_irq(&log->io_list_lock);
Shaohua Lia8c34f92015-09-02 13:49:46 -07001170
1171 if (!do_flush)
1172 return;
1173 bio_reset(&log->flush_bio);
1174 log->flush_bio.bi_bdev = log->rdev->bdev;
1175 log->flush_bio.bi_end_io = r5l_log_flush_endio;
Mike Christie796a5cf2016-06-05 14:32:07 -05001176 bio_set_op_attrs(&log->flush_bio, REQ_OP_WRITE, WRITE_FLUSH);
Mike Christie4e49ea42016-06-05 14:31:41 -05001177 submit_bio(&log->flush_bio);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001178}
1179
Shaohua Li0576b1c2015-08-13 14:32:00 -07001180static void r5l_write_super(struct r5l_log *log, sector_t cp);
Shaohua Li4b482042015-10-08 21:54:06 -07001181static void r5l_write_super_and_discard_space(struct r5l_log *log,
1182 sector_t end)
1183{
1184 struct block_device *bdev = log->rdev->bdev;
1185 struct mddev *mddev;
1186
1187 r5l_write_super(log, end);
1188
1189 if (!blk_queue_discard(bdev_get_queue(bdev)))
1190 return;
1191
1192 mddev = log->rdev->mddev;
1193 /*
Shaohua Li8e018c22016-08-25 10:09:39 -07001194 * Discard could zero data, so before discard we must make sure
1195 * superblock is updated to new log tail. Updating superblock (either
1196 * directly call md_update_sb() or depend on md thread) must hold
1197 * reconfig mutex. On the other hand, raid5_quiesce is called with
1198 * reconfig_mutex hold. The first step of raid5_quiesce() is waitting
1199 * for all IO finish, hence waitting for reclaim thread, while reclaim
1200 * thread is calling this function and waitting for reconfig mutex. So
1201 * there is a deadlock. We workaround this issue with a trylock.
1202 * FIXME: we could miss discard if we can't take reconfig mutex
Shaohua Li4b482042015-10-08 21:54:06 -07001203 */
Shaohua Li8e018c22016-08-25 10:09:39 -07001204 set_mask_bits(&mddev->flags, 0,
1205 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
1206 if (!mddev_trylock(mddev))
1207 return;
1208 md_update_sb(mddev, 1);
1209 mddev_unlock(mddev);
Shaohua Li4b482042015-10-08 21:54:06 -07001210
Shaohua Li6e74a9c2015-10-08 21:54:08 -07001211 /* discard IO error really doesn't matter, ignore it */
Shaohua Li4b482042015-10-08 21:54:06 -07001212 if (log->last_checkpoint < end) {
1213 blkdev_issue_discard(bdev,
1214 log->last_checkpoint + log->rdev->data_offset,
1215 end - log->last_checkpoint, GFP_NOIO, 0);
1216 } else {
1217 blkdev_issue_discard(bdev,
1218 log->last_checkpoint + log->rdev->data_offset,
1219 log->device_size - log->last_checkpoint,
1220 GFP_NOIO, 0);
1221 blkdev_issue_discard(bdev, log->rdev->data_offset, end,
1222 GFP_NOIO, 0);
1223 }
1224}
1225
Song Liua39f7af2016-11-17 15:24:40 -08001226/*
1227 * r5c_flush_stripe moves stripe from cached list to handle_list. When called,
1228 * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes.
1229 *
1230 * must hold conf->device_lock
1231 */
1232static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh)
1233{
1234 BUG_ON(list_empty(&sh->lru));
1235 BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
1236 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
1237
1238 /*
1239 * The stripe is not ON_RELEASE_LIST, so it is safe to call
1240 * raid5_release_stripe() while holding conf->device_lock
1241 */
1242 BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
1243 assert_spin_locked(&conf->device_lock);
1244
1245 list_del_init(&sh->lru);
1246 atomic_inc(&sh->count);
1247
1248 set_bit(STRIPE_HANDLE, &sh->state);
1249 atomic_inc(&conf->active_stripes);
1250 r5c_make_stripe_write_out(sh);
1251
1252 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1253 atomic_inc(&conf->preread_active_stripes);
1254 raid5_release_stripe(sh);
1255}
1256
1257/*
1258 * if num == 0, flush all full stripes
1259 * if num > 0, flush all full stripes. If less than num full stripes are
1260 * flushed, flush some partial stripes until totally num stripes are
1261 * flushed or there is no more cached stripes.
1262 */
1263void r5c_flush_cache(struct r5conf *conf, int num)
1264{
1265 int count;
1266 struct stripe_head *sh, *next;
1267
1268 assert_spin_locked(&conf->device_lock);
1269 if (!conf->log)
1270 return;
1271
1272 count = 0;
1273 list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) {
1274 r5c_flush_stripe(conf, sh);
1275 count++;
1276 }
1277
1278 if (count >= num)
1279 return;
1280 list_for_each_entry_safe(sh, next,
1281 &conf->r5c_partial_stripe_list, lru) {
1282 r5c_flush_stripe(conf, sh);
1283 if (++count >= num)
1284 break;
1285 }
1286}
1287
1288static void r5c_do_reclaim(struct r5conf *conf)
1289{
1290 struct r5l_log *log = conf->log;
1291 struct stripe_head *sh;
1292 int count = 0;
1293 unsigned long flags;
1294 int total_cached;
1295 int stripes_to_flush;
1296
1297 if (!r5c_is_writeback(log))
1298 return;
1299
1300 total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
1301 atomic_read(&conf->r5c_cached_full_stripes);
1302
1303 if (total_cached > conf->min_nr_stripes * 3 / 4 ||
1304 atomic_read(&conf->empty_inactive_list_nr) > 0)
1305 /*
1306 * if stripe cache pressure high, flush all full stripes and
1307 * some partial stripes
1308 */
1309 stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP;
1310 else if (total_cached > conf->min_nr_stripes * 1 / 2 ||
1311 atomic_read(&conf->r5c_cached_full_stripes) >
1312 R5C_FULL_STRIPE_FLUSH_BATCH)
1313 /*
1314 * if stripe cache pressure moderate, or if there is many full
1315 * stripes,flush all full stripes
1316 */
1317 stripes_to_flush = 0;
1318 else
1319 /* no need to flush */
1320 stripes_to_flush = -1;
1321
1322 if (stripes_to_flush >= 0) {
1323 spin_lock_irqsave(&conf->device_lock, flags);
1324 r5c_flush_cache(conf, stripes_to_flush);
1325 spin_unlock_irqrestore(&conf->device_lock, flags);
1326 }
1327
1328 /* if log space is tight, flush stripes on stripe_in_journal_list */
1329 if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) {
1330 spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
1331 spin_lock(&conf->device_lock);
1332 list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) {
1333 /*
1334 * stripes on stripe_in_journal_list could be in any
1335 * state of the stripe_cache state machine. In this
1336 * case, we only want to flush stripe on
1337 * r5c_cached_full/partial_stripes. The following
1338 * condition makes sure the stripe is on one of the
1339 * two lists.
1340 */
1341 if (!list_empty(&sh->lru) &&
1342 !test_bit(STRIPE_HANDLE, &sh->state) &&
1343 atomic_read(&sh->count) == 0) {
1344 r5c_flush_stripe(conf, sh);
1345 }
1346 if (count++ >= R5C_RECLAIM_STRIPE_GROUP)
1347 break;
1348 }
1349 spin_unlock(&conf->device_lock);
1350 spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
1351 }
1352 md_wakeup_thread(conf->mddev->thread);
1353}
1354
Shaohua Li0576b1c2015-08-13 14:32:00 -07001355static void r5l_do_reclaim(struct r5l_log *log)
1356{
Song Liua39f7af2016-11-17 15:24:40 -08001357 struct r5conf *conf = log->rdev->mddev->private;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001358 sector_t reclaim_target = xchg(&log->reclaim_target, 0);
Christoph Hellwig17036462015-10-05 09:31:06 +02001359 sector_t reclaimable;
1360 sector_t next_checkpoint;
Song Liua39f7af2016-11-17 15:24:40 -08001361 bool write_super;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001362
1363 spin_lock_irq(&log->io_list_lock);
Song Liua39f7af2016-11-17 15:24:40 -08001364 write_super = r5l_reclaimable_space(log) > log->max_free_space ||
1365 reclaim_target != 0 || !list_empty(&log->no_space_stripes);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001366 /*
1367 * move proper io_unit to reclaim list. We should not change the order.
1368 * reclaimable/unreclaimable io_unit can be mixed in the list, we
1369 * shouldn't reuse space of an unreclaimable io_unit
1370 */
1371 while (1) {
Christoph Hellwig17036462015-10-05 09:31:06 +02001372 reclaimable = r5l_reclaimable_space(log);
1373 if (reclaimable >= reclaim_target ||
Shaohua Li0576b1c2015-08-13 14:32:00 -07001374 (list_empty(&log->running_ios) &&
1375 list_empty(&log->io_end_ios) &&
Shaohua Lia8c34f92015-09-02 13:49:46 -07001376 list_empty(&log->flushing_ios) &&
Christoph Hellwig04732f72015-10-05 09:31:07 +02001377 list_empty(&log->finished_ios)))
Shaohua Li0576b1c2015-08-13 14:32:00 -07001378 break;
1379
Christoph Hellwig17036462015-10-05 09:31:06 +02001380 md_wakeup_thread(log->rdev->mddev->thread);
1381 wait_event_lock_irq(log->iounit_wait,
1382 r5l_reclaimable_space(log) > reclaimable,
1383 log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001384 }
Christoph Hellwig17036462015-10-05 09:31:06 +02001385
Song Liua39f7af2016-11-17 15:24:40 -08001386 next_checkpoint = r5c_calculate_new_cp(conf);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001387 spin_unlock_irq(&log->io_list_lock);
1388
Christoph Hellwig17036462015-10-05 09:31:06 +02001389 BUG_ON(reclaimable < 0);
Song Liua39f7af2016-11-17 15:24:40 -08001390
1391 if (reclaimable == 0 || !write_super)
Shaohua Li0576b1c2015-08-13 14:32:00 -07001392 return;
1393
Shaohua Li0576b1c2015-08-13 14:32:00 -07001394 /*
1395 * write_super will flush cache of each raid disk. We must write super
1396 * here, because the log area might be reused soon and we don't want to
1397 * confuse recovery
1398 */
Shaohua Li4b482042015-10-08 21:54:06 -07001399 r5l_write_super_and_discard_space(log, next_checkpoint);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001400
1401 mutex_lock(&log->io_mutex);
Christoph Hellwig17036462015-10-05 09:31:06 +02001402 log->last_checkpoint = next_checkpoint;
Song Liua39f7af2016-11-17 15:24:40 -08001403 r5c_update_log_state(log);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001404 mutex_unlock(&log->io_mutex);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001405
Christoph Hellwig17036462015-10-05 09:31:06 +02001406 r5l_run_no_space_stripes(log);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001407}
1408
1409static void r5l_reclaim_thread(struct md_thread *thread)
1410{
1411 struct mddev *mddev = thread->mddev;
1412 struct r5conf *conf = mddev->private;
1413 struct r5l_log *log = conf->log;
1414
1415 if (!log)
1416 return;
Song Liua39f7af2016-11-17 15:24:40 -08001417 r5c_do_reclaim(conf);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001418 r5l_do_reclaim(log);
1419}
1420
Song Liua39f7af2016-11-17 15:24:40 -08001421void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001422{
Shaohua Li0576b1c2015-08-13 14:32:00 -07001423 unsigned long target;
1424 unsigned long new = (unsigned long)space; /* overflow in theory */
1425
Song Liua39f7af2016-11-17 15:24:40 -08001426 if (!log)
1427 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -07001428 do {
1429 target = log->reclaim_target;
1430 if (new < target)
1431 return;
1432 } while (cmpxchg(&log->reclaim_target, target, new) != target);
1433 md_wakeup_thread(log->reclaim_thread);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001434}
1435
Shaohua Lie6c033f2015-10-04 09:20:12 -07001436void r5l_quiesce(struct r5l_log *log, int state)
1437{
Shaohua Li4b482042015-10-08 21:54:06 -07001438 struct mddev *mddev;
Shaohua Lie6c033f2015-10-04 09:20:12 -07001439 if (!log || state == 2)
1440 return;
Shaohua Lice1ccd02016-11-21 10:29:18 -08001441 if (state == 0)
1442 kthread_unpark(log->reclaim_thread->tsk);
1443 else if (state == 1) {
Shaohua Li4b482042015-10-08 21:54:06 -07001444 /* make sure r5l_write_super_and_discard_space exits */
1445 mddev = log->rdev->mddev;
1446 wake_up(&mddev->sb_wait);
Shaohua Lice1ccd02016-11-21 10:29:18 -08001447 kthread_park(log->reclaim_thread->tsk);
Song Liua39f7af2016-11-17 15:24:40 -08001448 r5l_wake_reclaim(log, MaxSector);
Shaohua Lie6c033f2015-10-04 09:20:12 -07001449 r5l_do_reclaim(log);
1450 }
1451}
1452
Shaohua Li6e74a9c2015-10-08 21:54:08 -07001453bool r5l_log_disk_error(struct r5conf *conf)
1454{
Shaohua Lif6b6ec52015-12-21 10:51:02 +11001455 struct r5l_log *log;
1456 bool ret;
Shaohua Li7dde2ad2015-10-08 21:54:10 -07001457 /* don't allow write if journal disk is missing */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11001458 rcu_read_lock();
1459 log = rcu_dereference(conf->log);
1460
1461 if (!log)
1462 ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
1463 else
1464 ret = test_bit(Faulty, &log->rdev->flags);
1465 rcu_read_unlock();
1466 return ret;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07001467}
1468
Shaohua Li355810d2015-08-13 14:32:01 -07001469struct r5l_recovery_ctx {
1470 struct page *meta_page; /* current meta */
1471 sector_t meta_total_blocks; /* total size of current meta and data */
1472 sector_t pos; /* recovery position */
1473 u64 seq; /* recovery position seq */
Song Liub4c625c2016-11-17 15:24:43 -08001474 int data_parity_stripes; /* number of data_parity stripes */
1475 int data_only_stripes; /* number of data_only stripes */
1476 struct list_head cached_list;
Shaohua Li355810d2015-08-13 14:32:01 -07001477};
1478
Song Liu9ed988f52016-11-17 15:24:42 -08001479static int r5l_recovery_read_meta_block(struct r5l_log *log,
1480 struct r5l_recovery_ctx *ctx)
Shaohua Li355810d2015-08-13 14:32:01 -07001481{
1482 struct page *page = ctx->meta_page;
1483 struct r5l_meta_block *mb;
1484 u32 crc, stored_crc;
1485
Mike Christie796a5cf2016-06-05 14:32:07 -05001486 if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, REQ_OP_READ, 0,
1487 false))
Shaohua Li355810d2015-08-13 14:32:01 -07001488 return -EIO;
1489
1490 mb = page_address(page);
1491 stored_crc = le32_to_cpu(mb->checksum);
1492 mb->checksum = 0;
1493
1494 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
1495 le64_to_cpu(mb->seq) != ctx->seq ||
1496 mb->version != R5LOG_VERSION ||
1497 le64_to_cpu(mb->position) != ctx->pos)
1498 return -EINVAL;
1499
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001500 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -07001501 if (stored_crc != crc)
1502 return -EINVAL;
1503
1504 if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
1505 return -EINVAL;
1506
1507 ctx->meta_total_blocks = BLOCK_SECTORS;
1508
1509 return 0;
1510}
1511
Song Liu9ed988f52016-11-17 15:24:42 -08001512static void
1513r5l_recovery_create_empty_meta_block(struct r5l_log *log,
1514 struct page *page,
1515 sector_t pos, u64 seq)
Shaohua Li355810d2015-08-13 14:32:01 -07001516{
Shaohua Li355810d2015-08-13 14:32:01 -07001517 struct r5l_meta_block *mb;
1518 u32 crc;
1519
Shaohua Li355810d2015-08-13 14:32:01 -07001520 mb = page_address(page);
Song Liu9ed988f52016-11-17 15:24:42 -08001521 clear_page(mb);
Shaohua Li355810d2015-08-13 14:32:01 -07001522 mb->magic = cpu_to_le32(R5LOG_MAGIC);
1523 mb->version = R5LOG_VERSION;
1524 mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
1525 mb->seq = cpu_to_le64(seq);
1526 mb->position = cpu_to_le64(pos);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001527 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -07001528 mb->checksum = cpu_to_le32(crc);
Song Liu9ed988f52016-11-17 15:24:42 -08001529}
Shaohua Li355810d2015-08-13 14:32:01 -07001530
Song Liu9ed988f52016-11-17 15:24:42 -08001531static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
1532 u64 seq)
1533{
1534 struct page *page;
1535
1536 page = alloc_page(GFP_KERNEL);
1537 if (!page)
1538 return -ENOMEM;
1539 r5l_recovery_create_empty_meta_block(log, page, pos, seq);
Mike Christie796a5cf2016-06-05 14:32:07 -05001540 if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE,
1541 WRITE_FUA, false)) {
Shaohua Li355810d2015-08-13 14:32:01 -07001542 __free_page(page);
1543 return -EIO;
1544 }
1545 __free_page(page);
1546 return 0;
1547}
1548
Song Liub4c625c2016-11-17 15:24:43 -08001549/*
1550 * r5l_recovery_load_data and r5l_recovery_load_parity uses flag R5_Wantwrite
1551 * to mark valid (potentially not flushed) data in the journal.
1552 *
1553 * We already verified checksum in r5l_recovery_verify_data_checksum_for_mb,
1554 * so there should not be any mismatch here.
1555 */
1556static void r5l_recovery_load_data(struct r5l_log *log,
1557 struct stripe_head *sh,
1558 struct r5l_recovery_ctx *ctx,
1559 struct r5l_payload_data_parity *payload,
1560 sector_t log_offset)
1561{
1562 struct mddev *mddev = log->rdev->mddev;
1563 struct r5conf *conf = mddev->private;
1564 int dd_idx;
1565
1566 raid5_compute_sector(conf,
1567 le64_to_cpu(payload->location), 0,
1568 &dd_idx, sh);
1569 sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1570 sh->dev[dd_idx].page, REQ_OP_READ, 0, false);
1571 sh->dev[dd_idx].log_checksum =
1572 le32_to_cpu(payload->checksum[0]);
1573 ctx->meta_total_blocks += BLOCK_SECTORS;
1574
1575 set_bit(R5_Wantwrite, &sh->dev[dd_idx].flags);
1576 set_bit(STRIPE_R5C_CACHING, &sh->state);
1577}
1578
1579static void r5l_recovery_load_parity(struct r5l_log *log,
1580 struct stripe_head *sh,
1581 struct r5l_recovery_ctx *ctx,
1582 struct r5l_payload_data_parity *payload,
1583 sector_t log_offset)
1584{
1585 struct mddev *mddev = log->rdev->mddev;
1586 struct r5conf *conf = mddev->private;
1587
1588 ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
1589 sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1590 sh->dev[sh->pd_idx].page, REQ_OP_READ, 0, false);
1591 sh->dev[sh->pd_idx].log_checksum =
1592 le32_to_cpu(payload->checksum[0]);
1593 set_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags);
1594
1595 if (sh->qd_idx >= 0) {
1596 sync_page_io(log->rdev,
1597 r5l_ring_add(log, log_offset, BLOCK_SECTORS),
1598 PAGE_SIZE, sh->dev[sh->qd_idx].page,
1599 REQ_OP_READ, 0, false);
1600 sh->dev[sh->qd_idx].log_checksum =
1601 le32_to_cpu(payload->checksum[1]);
1602 set_bit(R5_Wantwrite, &sh->dev[sh->qd_idx].flags);
1603 }
1604 clear_bit(STRIPE_R5C_CACHING, &sh->state);
1605}
1606
1607static void r5l_recovery_reset_stripe(struct stripe_head *sh)
1608{
1609 int i;
1610
1611 sh->state = 0;
1612 sh->log_start = MaxSector;
1613 for (i = sh->disks; i--; )
1614 sh->dev[i].flags = 0;
1615}
1616
1617static void
1618r5l_recovery_replay_one_stripe(struct r5conf *conf,
1619 struct stripe_head *sh,
1620 struct r5l_recovery_ctx *ctx)
1621{
1622 struct md_rdev *rdev, *rrdev;
1623 int disk_index;
1624 int data_count = 0;
1625
1626 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
1627 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
1628 continue;
1629 if (disk_index == sh->qd_idx || disk_index == sh->pd_idx)
1630 continue;
1631 data_count++;
1632 }
1633
1634 /*
1635 * stripes that only have parity must have been flushed
1636 * before the crash that we are now recovering from, so
1637 * there is nothing more to recovery.
1638 */
1639 if (data_count == 0)
1640 goto out;
1641
1642 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
1643 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
1644 continue;
1645
1646 /* in case device is broken */
1647 rcu_read_lock();
1648 rdev = rcu_dereference(conf->disks[disk_index].rdev);
1649 if (rdev) {
1650 atomic_inc(&rdev->nr_pending);
1651 rcu_read_unlock();
1652 sync_page_io(rdev, sh->sector, PAGE_SIZE,
1653 sh->dev[disk_index].page, REQ_OP_WRITE, 0,
1654 false);
1655 rdev_dec_pending(rdev, rdev->mddev);
1656 rcu_read_lock();
1657 }
1658 rrdev = rcu_dereference(conf->disks[disk_index].replacement);
1659 if (rrdev) {
1660 atomic_inc(&rrdev->nr_pending);
1661 rcu_read_unlock();
1662 sync_page_io(rrdev, sh->sector, PAGE_SIZE,
1663 sh->dev[disk_index].page, REQ_OP_WRITE, 0,
1664 false);
1665 rdev_dec_pending(rrdev, rrdev->mddev);
1666 rcu_read_lock();
1667 }
1668 rcu_read_unlock();
1669 }
1670 ctx->data_parity_stripes++;
1671out:
1672 r5l_recovery_reset_stripe(sh);
1673}
1674
1675static struct stripe_head *
1676r5c_recovery_alloc_stripe(struct r5conf *conf,
Song Liub4c625c2016-11-17 15:24:43 -08001677 sector_t stripe_sect,
1678 sector_t log_start)
1679{
1680 struct stripe_head *sh;
1681
1682 sh = raid5_get_active_stripe(conf, stripe_sect, 0, 1, 0);
1683 if (!sh)
1684 return NULL; /* no more stripe available */
1685
1686 r5l_recovery_reset_stripe(sh);
1687 sh->log_start = log_start;
1688
1689 return sh;
1690}
1691
1692static struct stripe_head *
1693r5c_recovery_lookup_stripe(struct list_head *list, sector_t sect)
1694{
1695 struct stripe_head *sh;
1696
1697 list_for_each_entry(sh, list, lru)
1698 if (sh->sector == sect)
1699 return sh;
1700 return NULL;
1701}
1702
1703static void
1704r5c_recovery_drop_stripes(struct list_head *cached_stripe_list,
1705 struct r5l_recovery_ctx *ctx)
1706{
1707 struct stripe_head *sh, *next;
1708
1709 list_for_each_entry_safe(sh, next, cached_stripe_list, lru) {
1710 r5l_recovery_reset_stripe(sh);
1711 list_del_init(&sh->lru);
1712 raid5_release_stripe(sh);
1713 }
1714}
1715
1716static void
1717r5c_recovery_replay_stripes(struct list_head *cached_stripe_list,
1718 struct r5l_recovery_ctx *ctx)
1719{
1720 struct stripe_head *sh, *next;
1721
1722 list_for_each_entry_safe(sh, next, cached_stripe_list, lru)
1723 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
1724 r5l_recovery_replay_one_stripe(sh->raid_conf, sh, ctx);
1725 list_del_init(&sh->lru);
1726 raid5_release_stripe(sh);
1727 }
1728}
1729
1730/* if matches return 0; otherwise return -EINVAL */
1731static int
1732r5l_recovery_verify_data_checksum(struct r5l_log *log, struct page *page,
1733 sector_t log_offset, __le32 log_checksum)
1734{
1735 void *addr;
1736 u32 checksum;
1737
1738 sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1739 page, REQ_OP_READ, 0, false);
1740 addr = kmap_atomic(page);
1741 checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
1742 kunmap_atomic(addr);
1743 return (le32_to_cpu(log_checksum) == checksum) ? 0 : -EINVAL;
1744}
1745
1746/*
1747 * before loading data to stripe cache, we need verify checksum for all data,
1748 * if there is mismatch for any data page, we drop all data in the mata block
1749 */
1750static int
1751r5l_recovery_verify_data_checksum_for_mb(struct r5l_log *log,
1752 struct r5l_recovery_ctx *ctx)
1753{
1754 struct mddev *mddev = log->rdev->mddev;
1755 struct r5conf *conf = mddev->private;
1756 struct r5l_meta_block *mb = page_address(ctx->meta_page);
1757 sector_t mb_offset = sizeof(struct r5l_meta_block);
1758 sector_t log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
1759 struct page *page;
1760 struct r5l_payload_data_parity *payload;
1761
1762 page = alloc_page(GFP_KERNEL);
1763 if (!page)
1764 return -ENOMEM;
1765
1766 while (mb_offset < le32_to_cpu(mb->meta_size)) {
1767 payload = (void *)mb + mb_offset;
1768
1769 if (payload->header.type == R5LOG_PAYLOAD_DATA) {
1770 if (r5l_recovery_verify_data_checksum(
1771 log, page, log_offset,
1772 payload->checksum[0]) < 0)
1773 goto mismatch;
1774 } else if (payload->header.type == R5LOG_PAYLOAD_PARITY) {
1775 if (r5l_recovery_verify_data_checksum(
1776 log, page, log_offset,
1777 payload->checksum[0]) < 0)
1778 goto mismatch;
1779 if (conf->max_degraded == 2 && /* q for RAID 6 */
1780 r5l_recovery_verify_data_checksum(
1781 log, page,
1782 r5l_ring_add(log, log_offset,
1783 BLOCK_SECTORS),
1784 payload->checksum[1]) < 0)
1785 goto mismatch;
1786 } else /* not R5LOG_PAYLOAD_DATA or R5LOG_PAYLOAD_PARITY */
1787 goto mismatch;
1788
1789 log_offset = r5l_ring_add(log, log_offset,
1790 le32_to_cpu(payload->size));
1791
1792 mb_offset += sizeof(struct r5l_payload_data_parity) +
1793 sizeof(__le32) *
1794 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
1795 }
1796
1797 put_page(page);
1798 return 0;
1799
1800mismatch:
1801 put_page(page);
1802 return -EINVAL;
1803}
1804
1805/*
1806 * Analyze all data/parity pages in one meta block
1807 * Returns:
1808 * 0 for success
1809 * -EINVAL for unknown playload type
1810 * -EAGAIN for checksum mismatch of data page
1811 * -ENOMEM for run out of memory (alloc_page failed or run out of stripes)
1812 */
1813static int
1814r5c_recovery_analyze_meta_block(struct r5l_log *log,
1815 struct r5l_recovery_ctx *ctx,
1816 struct list_head *cached_stripe_list)
1817{
1818 struct mddev *mddev = log->rdev->mddev;
1819 struct r5conf *conf = mddev->private;
1820 struct r5l_meta_block *mb;
1821 struct r5l_payload_data_parity *payload;
1822 int mb_offset;
1823 sector_t log_offset;
1824 sector_t stripe_sect;
1825 struct stripe_head *sh;
1826 int ret;
1827
1828 /*
1829 * for mismatch in data blocks, we will drop all data in this mb, but
1830 * we will still read next mb for other data with FLUSH flag, as
1831 * io_unit could finish out of order.
1832 */
1833 ret = r5l_recovery_verify_data_checksum_for_mb(log, ctx);
1834 if (ret == -EINVAL)
1835 return -EAGAIN;
1836 else if (ret)
1837 return ret; /* -ENOMEM duo to alloc_page() failed */
1838
1839 mb = page_address(ctx->meta_page);
1840 mb_offset = sizeof(struct r5l_meta_block);
1841 log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
1842
1843 while (mb_offset < le32_to_cpu(mb->meta_size)) {
1844 int dd;
1845
1846 payload = (void *)mb + mb_offset;
1847 stripe_sect = (payload->header.type == R5LOG_PAYLOAD_DATA) ?
1848 raid5_compute_sector(
1849 conf, le64_to_cpu(payload->location), 0, &dd,
1850 NULL)
1851 : le64_to_cpu(payload->location);
1852
1853 sh = r5c_recovery_lookup_stripe(cached_stripe_list,
1854 stripe_sect);
1855
1856 if (!sh) {
JackieLiu9b691732016-11-28 16:19:18 +08001857 sh = r5c_recovery_alloc_stripe(conf, stripe_sect, ctx->pos);
Song Liub4c625c2016-11-17 15:24:43 -08001858 /*
1859 * cannot get stripe from raid5_get_active_stripe
1860 * try replay some stripes
1861 */
1862 if (!sh) {
1863 r5c_recovery_replay_stripes(
1864 cached_stripe_list, ctx);
1865 sh = r5c_recovery_alloc_stripe(
JackieLiu9b691732016-11-28 16:19:18 +08001866 conf, stripe_sect, ctx->pos);
Song Liub4c625c2016-11-17 15:24:43 -08001867 }
1868 if (!sh) {
1869 pr_debug("md/raid:%s: Increasing stripe cache size to %d to recovery data on journal.\n",
1870 mdname(mddev),
1871 conf->min_nr_stripes * 2);
1872 raid5_set_cache_size(mddev,
1873 conf->min_nr_stripes * 2);
1874 sh = r5c_recovery_alloc_stripe(
JackieLiu9b691732016-11-28 16:19:18 +08001875 conf, stripe_sect, ctx->pos);
Song Liub4c625c2016-11-17 15:24:43 -08001876 }
1877 if (!sh) {
1878 pr_err("md/raid:%s: Cannot get enough stripes due to memory pressure. Recovery failed.\n",
1879 mdname(mddev));
1880 return -ENOMEM;
1881 }
1882 list_add_tail(&sh->lru, cached_stripe_list);
1883 }
1884
1885 if (payload->header.type == R5LOG_PAYLOAD_DATA) {
Zhengyuan Liuf7b7bee2016-11-26 10:57:13 +08001886 if (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
1887 test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags)) {
Song Liub4c625c2016-11-17 15:24:43 -08001888 r5l_recovery_replay_one_stripe(conf, sh, ctx);
Song Liub4c625c2016-11-17 15:24:43 -08001889 sh->log_start = ctx->pos;
1890 list_move_tail(&sh->lru, cached_stripe_list);
1891 }
1892 r5l_recovery_load_data(log, sh, ctx, payload,
1893 log_offset);
1894 } else if (payload->header.type == R5LOG_PAYLOAD_PARITY)
1895 r5l_recovery_load_parity(log, sh, ctx, payload,
1896 log_offset);
1897 else
1898 return -EINVAL;
1899
1900 log_offset = r5l_ring_add(log, log_offset,
1901 le32_to_cpu(payload->size));
1902
1903 mb_offset += sizeof(struct r5l_payload_data_parity) +
1904 sizeof(__le32) *
1905 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
1906 }
1907
1908 return 0;
1909}
1910
1911/*
1912 * Load the stripe into cache. The stripe will be written out later by
1913 * the stripe cache state machine.
1914 */
1915static void r5c_recovery_load_one_stripe(struct r5l_log *log,
1916 struct stripe_head *sh)
1917{
Song Liub4c625c2016-11-17 15:24:43 -08001918 struct r5dev *dev;
1919 int i;
1920
1921 for (i = sh->disks; i--; ) {
1922 dev = sh->dev + i;
1923 if (test_and_clear_bit(R5_Wantwrite, &dev->flags)) {
1924 set_bit(R5_InJournal, &dev->flags);
1925 set_bit(R5_UPTODATE, &dev->flags);
1926 }
1927 }
Song Liub4c625c2016-11-17 15:24:43 -08001928 list_add_tail(&sh->r5c, &log->stripe_in_journal_list);
Zhengyuan Liu462eb7d2016-11-26 10:57:14 +08001929 atomic_inc(&log->stripe_in_journal_count);
Song Liub4c625c2016-11-17 15:24:43 -08001930}
1931
1932/*
1933 * Scan through the log for all to-be-flushed data
1934 *
1935 * For stripes with data and parity, namely Data-Parity stripe
1936 * (STRIPE_R5C_CACHING == 0), we simply replay all the writes.
1937 *
1938 * For stripes with only data, namely Data-Only stripe
1939 * (STRIPE_R5C_CACHING == 1), we load them to stripe cache state machine.
1940 *
1941 * For a stripe, if we see data after parity, we should discard all previous
1942 * data and parity for this stripe, as these data are already flushed to
1943 * the array.
1944 *
1945 * At the end of the scan, we return the new journal_tail, which points to
1946 * first data-only stripe on the journal device, or next invalid meta block.
1947 */
1948static int r5c_recovery_flush_log(struct r5l_log *log,
1949 struct r5l_recovery_ctx *ctx)
1950{
1951 struct stripe_head *sh, *next;
1952 int ret = 0;
1953
1954 /* scan through the log */
1955 while (1) {
1956 if (r5l_recovery_read_meta_block(log, ctx))
1957 break;
1958
1959 ret = r5c_recovery_analyze_meta_block(log, ctx,
1960 &ctx->cached_list);
1961 /*
1962 * -EAGAIN means mismatch in data block, in this case, we still
1963 * try scan the next metablock
1964 */
1965 if (ret && ret != -EAGAIN)
1966 break; /* ret == -EINVAL or -ENOMEM */
1967 ctx->seq++;
1968 ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
1969 }
1970
1971 if (ret == -ENOMEM) {
1972 r5c_recovery_drop_stripes(&ctx->cached_list, ctx);
1973 return ret;
1974 }
1975
1976 /* replay data-parity stripes */
1977 r5c_recovery_replay_stripes(&ctx->cached_list, ctx);
1978
1979 /* load data-only stripes to stripe cache */
1980 list_for_each_entry_safe(sh, next, &ctx->cached_list, lru) {
1981 WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
1982 r5c_recovery_load_one_stripe(log, sh);
1983 list_del_init(&sh->lru);
1984 raid5_release_stripe(sh);
1985 ctx->data_only_stripes++;
1986 }
1987
1988 return 0;
1989}
1990
1991/*
1992 * we did a recovery. Now ctx.pos points to an invalid meta block. New
1993 * log will start here. but we can't let superblock point to last valid
1994 * meta block. The log might looks like:
1995 * | meta 1| meta 2| meta 3|
1996 * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
1997 * superblock points to meta 1, we write a new valid meta 2n. if crash
1998 * happens again, new recovery will start from meta 1. Since meta 2n is
1999 * valid now, recovery will think meta 3 is valid, which is wrong.
2000 * The solution is we create a new meta in meta2 with its seq == meta
2001 * 1's seq + 10 and let superblock points to meta2. The same recovery will
2002 * not think meta 3 is a valid meta, because its seq doesn't match
2003 */
2004
2005/*
2006 * Before recovery, the log looks like the following
2007 *
2008 * ---------------------------------------------
2009 * | valid log | invalid log |
2010 * ---------------------------------------------
2011 * ^
2012 * |- log->last_checkpoint
2013 * |- log->last_cp_seq
2014 *
2015 * Now we scan through the log until we see invalid entry
2016 *
2017 * ---------------------------------------------
2018 * | valid log | invalid log |
2019 * ---------------------------------------------
2020 * ^ ^
2021 * |- log->last_checkpoint |- ctx->pos
2022 * |- log->last_cp_seq |- ctx->seq
2023 *
2024 * From this point, we need to increase seq number by 10 to avoid
2025 * confusing next recovery.
2026 *
2027 * ---------------------------------------------
2028 * | valid log | invalid log |
2029 * ---------------------------------------------
2030 * ^ ^
2031 * |- log->last_checkpoint |- ctx->pos+1
2032 * |- log->last_cp_seq |- ctx->seq+11
2033 *
2034 * However, it is not safe to start the state machine yet, because data only
2035 * parities are not yet secured in RAID. To save these data only parities, we
2036 * rewrite them from seq+11.
2037 *
2038 * -----------------------------------------------------------------
2039 * | valid log | data only stripes | invalid log |
2040 * -----------------------------------------------------------------
2041 * ^ ^
2042 * |- log->last_checkpoint |- ctx->pos+n
2043 * |- log->last_cp_seq |- ctx->seq+10+n
2044 *
2045 * If failure happens again during this process, the recovery can safe start
2046 * again from log->last_checkpoint.
2047 *
2048 * Once data only stripes are rewritten to journal, we move log_tail
2049 *
2050 * -----------------------------------------------------------------
2051 * | old log | data only stripes | invalid log |
2052 * -----------------------------------------------------------------
2053 * ^ ^
2054 * |- log->last_checkpoint |- ctx->pos+n
2055 * |- log->last_cp_seq |- ctx->seq+10+n
2056 *
2057 * Then we can safely start the state machine. If failure happens from this
2058 * point on, the recovery will start from new log->last_checkpoint.
2059 */
2060static int
2061r5c_recovery_rewrite_data_only_stripes(struct r5l_log *log,
2062 struct r5l_recovery_ctx *ctx)
2063{
2064 struct stripe_head *sh;
2065 struct mddev *mddev = log->rdev->mddev;
2066 struct page *page;
2067
2068 page = alloc_page(GFP_KERNEL);
2069 if (!page) {
2070 pr_err("md/raid:%s: cannot allocate memory to rewrite data only stripes\n",
2071 mdname(mddev));
2072 return -ENOMEM;
2073 }
2074
2075 ctx->seq += 10;
2076 list_for_each_entry(sh, &ctx->cached_list, lru) {
2077 struct r5l_meta_block *mb;
2078 int i;
2079 int offset;
2080 sector_t write_pos;
2081
2082 WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
2083 r5l_recovery_create_empty_meta_block(log, page,
2084 ctx->pos, ctx->seq);
2085 mb = page_address(page);
2086 offset = le32_to_cpu(mb->meta_size);
JackieLiufc833c22016-11-28 16:19:19 +08002087 write_pos = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
Song Liub4c625c2016-11-17 15:24:43 -08002088
2089 for (i = sh->disks; i--; ) {
2090 struct r5dev *dev = &sh->dev[i];
2091 struct r5l_payload_data_parity *payload;
2092 void *addr;
2093
2094 if (test_bit(R5_InJournal, &dev->flags)) {
2095 payload = (void *)mb + offset;
2096 payload->header.type = cpu_to_le16(
2097 R5LOG_PAYLOAD_DATA);
2098 payload->size = BLOCK_SECTORS;
2099 payload->location = cpu_to_le64(
2100 raid5_compute_blocknr(sh, i, 0));
2101 addr = kmap_atomic(dev->page);
2102 payload->checksum[0] = cpu_to_le32(
2103 crc32c_le(log->uuid_checksum, addr,
2104 PAGE_SIZE));
2105 kunmap_atomic(addr);
2106 sync_page_io(log->rdev, write_pos, PAGE_SIZE,
2107 dev->page, REQ_OP_WRITE, 0, false);
2108 write_pos = r5l_ring_add(log, write_pos,
2109 BLOCK_SECTORS);
2110 offset += sizeof(__le32) +
2111 sizeof(struct r5l_payload_data_parity);
2112
2113 }
2114 }
2115 mb->meta_size = cpu_to_le32(offset);
2116 mb->checksum = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
2117 sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page,
2118 REQ_OP_WRITE, WRITE_FUA, false);
2119 sh->log_start = ctx->pos;
2120 ctx->pos = write_pos;
2121 ctx->seq += 1;
2122 }
2123 __free_page(page);
2124 return 0;
2125}
2126
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002127static int r5l_recovery_log(struct r5l_log *log)
2128{
Song Liu5aabf7c2016-11-17 15:24:44 -08002129 struct mddev *mddev = log->rdev->mddev;
Shaohua Li355810d2015-08-13 14:32:01 -07002130 struct r5l_recovery_ctx ctx;
Song Liu5aabf7c2016-11-17 15:24:44 -08002131 int ret;
Shaohua Li355810d2015-08-13 14:32:01 -07002132
2133 ctx.pos = log->last_checkpoint;
2134 ctx.seq = log->last_cp_seq;
2135 ctx.meta_page = alloc_page(GFP_KERNEL);
Song Liub4c625c2016-11-17 15:24:43 -08002136 ctx.data_only_stripes = 0;
2137 ctx.data_parity_stripes = 0;
2138 INIT_LIST_HEAD(&ctx.cached_list);
2139
Shaohua Li355810d2015-08-13 14:32:01 -07002140 if (!ctx.meta_page)
2141 return -ENOMEM;
2142
Song Liu5aabf7c2016-11-17 15:24:44 -08002143 ret = r5c_recovery_flush_log(log, &ctx);
Shaohua Li355810d2015-08-13 14:32:01 -07002144 __free_page(ctx.meta_page);
2145
Song Liu5aabf7c2016-11-17 15:24:44 -08002146 if (ret)
2147 return ret;
Shaohua Li355810d2015-08-13 14:32:01 -07002148
Song Liu5aabf7c2016-11-17 15:24:44 -08002149 if ((ctx.data_only_stripes == 0) && (ctx.data_parity_stripes == 0))
2150 pr_debug("md/raid:%s: starting from clean shutdown\n",
2151 mdname(mddev));
2152 else {
2153 pr_debug("md/raid:%s: recoverying %d data-only stripes and %d data-parity stripes\n",
2154 mdname(mddev), ctx.data_only_stripes,
2155 ctx.data_parity_stripes);
2156
2157 if (ctx.data_only_stripes > 0)
2158 if (r5c_recovery_rewrite_data_only_stripes(log, &ctx)) {
2159 pr_err("md/raid:%s: failed to rewrite stripes to journal\n",
2160 mdname(mddev));
2161 return -EIO;
2162 }
Shaohua Li355810d2015-08-13 14:32:01 -07002163 }
Song Liub4c625c2016-11-17 15:24:43 -08002164
Song Liu5aabf7c2016-11-17 15:24:44 -08002165 log->log_start = ctx.pos;
2166 log->next_checkpoint = ctx.pos;
2167 log->seq = ctx.seq;
2168 r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq);
2169 r5l_write_super(log, ctx.pos);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002170 return 0;
2171}
2172
2173static void r5l_write_super(struct r5l_log *log, sector_t cp)
2174{
2175 struct mddev *mddev = log->rdev->mddev;
2176
2177 log->rdev->journal_tail = cp;
2178 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2179}
2180
Song Liu2c7da142016-11-17 15:24:41 -08002181static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page)
2182{
2183 struct r5conf *conf = mddev->private;
2184 int ret;
2185
2186 if (!conf->log)
2187 return 0;
2188
2189 switch (conf->log->r5c_journal_mode) {
2190 case R5C_JOURNAL_MODE_WRITE_THROUGH:
2191 ret = snprintf(
2192 page, PAGE_SIZE, "[%s] %s\n",
2193 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
2194 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
2195 break;
2196 case R5C_JOURNAL_MODE_WRITE_BACK:
2197 ret = snprintf(
2198 page, PAGE_SIZE, "%s [%s]\n",
2199 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
2200 r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
2201 break;
2202 default:
2203 ret = 0;
2204 }
2205 return ret;
2206}
2207
2208static ssize_t r5c_journal_mode_store(struct mddev *mddev,
2209 const char *page, size_t length)
2210{
2211 struct r5conf *conf = mddev->private;
2212 struct r5l_log *log = conf->log;
2213 int val = -1, i;
2214 int len = length;
2215
2216 if (!log)
2217 return -ENODEV;
2218
2219 if (len && page[len - 1] == '\n')
2220 len -= 1;
2221 for (i = 0; i < ARRAY_SIZE(r5c_journal_mode_str); i++)
2222 if (strlen(r5c_journal_mode_str[i]) == len &&
2223 strncmp(page, r5c_journal_mode_str[i], len) == 0) {
2224 val = i;
2225 break;
2226 }
2227 if (val < R5C_JOURNAL_MODE_WRITE_THROUGH ||
2228 val > R5C_JOURNAL_MODE_WRITE_BACK)
2229 return -EINVAL;
2230
2231 mddev_suspend(mddev);
2232 conf->log->r5c_journal_mode = val;
2233 mddev_resume(mddev);
2234
2235 pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n",
2236 mdname(mddev), val, r5c_journal_mode_str[val]);
2237 return length;
2238}
2239
2240struct md_sysfs_entry
2241r5c_journal_mode = __ATTR(journal_mode, 0644,
2242 r5c_journal_mode_show, r5c_journal_mode_store);
2243
Song Liu2ded3702016-11-17 15:24:38 -08002244/*
2245 * Try handle write operation in caching phase. This function should only
2246 * be called in write-back mode.
2247 *
2248 * If all outstanding writes can be handled in caching phase, returns 0
2249 * If writes requires write-out phase, call r5c_make_stripe_write_out()
2250 * and returns -EAGAIN
2251 */
2252int r5c_try_caching_write(struct r5conf *conf,
2253 struct stripe_head *sh,
2254 struct stripe_head_state *s,
2255 int disks)
2256{
2257 struct r5l_log *log = conf->log;
Song Liu1e6d6902016-11-17 15:24:39 -08002258 int i;
2259 struct r5dev *dev;
2260 int to_cache = 0;
Song Liu2ded3702016-11-17 15:24:38 -08002261
2262 BUG_ON(!r5c_is_writeback(log));
2263
Song Liu1e6d6902016-11-17 15:24:39 -08002264 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
2265 /*
2266 * There are two different scenarios here:
2267 * 1. The stripe has some data cached, and it is sent to
2268 * write-out phase for reclaim
2269 * 2. The stripe is clean, and this is the first write
2270 *
2271 * For 1, return -EAGAIN, so we continue with
2272 * handle_stripe_dirtying().
2273 *
2274 * For 2, set STRIPE_R5C_CACHING and continue with caching
2275 * write.
2276 */
2277
2278 /* case 1: anything injournal or anything in written */
2279 if (s->injournal > 0 || s->written > 0)
2280 return -EAGAIN;
2281 /* case 2 */
2282 set_bit(STRIPE_R5C_CACHING, &sh->state);
2283 }
2284
2285 for (i = disks; i--; ) {
2286 dev = &sh->dev[i];
2287 /* if non-overwrite, use writing-out phase */
2288 if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) &&
2289 !test_bit(R5_InJournal, &dev->flags)) {
2290 r5c_make_stripe_write_out(sh);
2291 return -EAGAIN;
2292 }
2293 }
2294
2295 for (i = disks; i--; ) {
2296 dev = &sh->dev[i];
2297 if (dev->towrite) {
2298 set_bit(R5_Wantwrite, &dev->flags);
2299 set_bit(R5_Wantdrain, &dev->flags);
2300 set_bit(R5_LOCKED, &dev->flags);
2301 to_cache++;
2302 }
2303 }
2304
2305 if (to_cache) {
2306 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2307 /*
2308 * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data()
2309 * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in
2310 * r5c_handle_data_cached()
2311 */
2312 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
2313 }
2314
2315 return 0;
2316}
2317
2318/*
2319 * free extra pages (orig_page) we allocated for prexor
2320 */
2321void r5c_release_extra_page(struct stripe_head *sh)
2322{
Song Liud7bd3982016-11-23 22:50:39 -08002323 struct r5conf *conf = sh->raid_conf;
Song Liu1e6d6902016-11-17 15:24:39 -08002324 int i;
Song Liud7bd3982016-11-23 22:50:39 -08002325 bool using_disk_info_extra_page;
2326
2327 using_disk_info_extra_page =
2328 sh->dev[0].orig_page == conf->disks[0].extra_page;
Song Liu1e6d6902016-11-17 15:24:39 -08002329
2330 for (i = sh->disks; i--; )
2331 if (sh->dev[i].page != sh->dev[i].orig_page) {
2332 struct page *p = sh->dev[i].orig_page;
2333
2334 sh->dev[i].orig_page = sh->dev[i].page;
Song Liud7bd3982016-11-23 22:50:39 -08002335 if (!using_disk_info_extra_page)
2336 put_page(p);
Song Liu1e6d6902016-11-17 15:24:39 -08002337 }
Song Liud7bd3982016-11-23 22:50:39 -08002338
2339 if (using_disk_info_extra_page) {
2340 clear_bit(R5C_EXTRA_PAGE_IN_USE, &conf->cache_state);
2341 md_wakeup_thread(conf->mddev->thread);
2342 }
2343}
2344
2345void r5c_use_extra_page(struct stripe_head *sh)
2346{
2347 struct r5conf *conf = sh->raid_conf;
2348 int i;
2349 struct r5dev *dev;
2350
2351 for (i = sh->disks; i--; ) {
2352 dev = &sh->dev[i];
2353 if (dev->orig_page != dev->page)
2354 put_page(dev->orig_page);
2355 dev->orig_page = conf->disks[i].extra_page;
2356 }
Song Liu2ded3702016-11-17 15:24:38 -08002357}
2358
2359/*
2360 * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the
2361 * stripe is committed to RAID disks.
2362 */
2363void r5c_finish_stripe_write_out(struct r5conf *conf,
2364 struct stripe_head *sh,
2365 struct stripe_head_state *s)
2366{
Song Liu1e6d6902016-11-17 15:24:39 -08002367 int i;
2368 int do_wakeup = 0;
2369
Song Liu2ded3702016-11-17 15:24:38 -08002370 if (!conf->log ||
2371 !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags))
2372 return;
2373
2374 WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
2375 clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
2376
2377 if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
2378 return;
Song Liu1e6d6902016-11-17 15:24:39 -08002379
2380 for (i = sh->disks; i--; ) {
2381 clear_bit(R5_InJournal, &sh->dev[i].flags);
2382 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2383 do_wakeup = 1;
2384 }
2385
2386 /*
2387 * analyse_stripe() runs before r5c_finish_stripe_write_out(),
2388 * We updated R5_InJournal, so we also update s->injournal.
2389 */
2390 s->injournal = 0;
2391
2392 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2393 if (atomic_dec_and_test(&conf->pending_full_writes))
2394 md_wakeup_thread(conf->mddev->thread);
2395
2396 if (do_wakeup)
2397 wake_up(&conf->wait_for_overlap);
Song Liua39f7af2016-11-17 15:24:40 -08002398
2399 if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
2400 return;
2401
2402 spin_lock_irq(&conf->log->stripe_in_journal_lock);
2403 list_del_init(&sh->r5c);
2404 spin_unlock_irq(&conf->log->stripe_in_journal_lock);
2405 sh->log_start = MaxSector;
2406 atomic_dec(&conf->log->stripe_in_journal_count);
Song Liu1e6d6902016-11-17 15:24:39 -08002407}
2408
2409int
2410r5c_cache_data(struct r5l_log *log, struct stripe_head *sh,
2411 struct stripe_head_state *s)
2412{
Song Liua39f7af2016-11-17 15:24:40 -08002413 struct r5conf *conf = sh->raid_conf;
Song Liu1e6d6902016-11-17 15:24:39 -08002414 int pages = 0;
2415 int reserve;
2416 int i;
2417 int ret = 0;
2418
2419 BUG_ON(!log);
2420
2421 for (i = 0; i < sh->disks; i++) {
2422 void *addr;
2423
2424 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
2425 continue;
2426 addr = kmap_atomic(sh->dev[i].page);
2427 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
2428 addr, PAGE_SIZE);
2429 kunmap_atomic(addr);
2430 pages++;
2431 }
2432 WARN_ON(pages == 0);
2433
2434 /*
2435 * The stripe must enter state machine again to call endio, so
2436 * don't delay.
2437 */
2438 clear_bit(STRIPE_DELAYED, &sh->state);
2439 atomic_inc(&sh->count);
2440
2441 mutex_lock(&log->io_mutex);
2442 /* meta + data */
2443 reserve = (1 + pages) << (PAGE_SHIFT - 9);
Song Liu1e6d6902016-11-17 15:24:39 -08002444
Song Liua39f7af2016-11-17 15:24:40 -08002445 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
2446 sh->log_start == MaxSector)
2447 r5l_add_no_space_stripe(log, sh);
2448 else if (!r5l_has_free_space(log, reserve)) {
2449 if (sh->log_start == log->last_checkpoint)
2450 BUG();
2451 else
2452 r5l_add_no_space_stripe(log, sh);
Song Liu1e6d6902016-11-17 15:24:39 -08002453 } else {
2454 ret = r5l_log_stripe(log, sh, pages, 0);
2455 if (ret) {
2456 spin_lock_irq(&log->io_list_lock);
2457 list_add_tail(&sh->log_list, &log->no_mem_stripes);
2458 spin_unlock_irq(&log->io_list_lock);
2459 }
2460 }
2461
2462 mutex_unlock(&log->io_mutex);
2463 return 0;
Song Liu2ded3702016-11-17 15:24:38 -08002464}
2465
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002466static int r5l_load_log(struct r5l_log *log)
2467{
2468 struct md_rdev *rdev = log->rdev;
2469 struct page *page;
2470 struct r5l_meta_block *mb;
2471 sector_t cp = log->rdev->journal_tail;
2472 u32 stored_crc, expected_crc;
2473 bool create_super = false;
2474 int ret;
2475
2476 /* Make sure it's valid */
2477 if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
2478 cp = 0;
2479 page = alloc_page(GFP_KERNEL);
2480 if (!page)
2481 return -ENOMEM;
2482
Mike Christie796a5cf2016-06-05 14:32:07 -05002483 if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) {
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002484 ret = -EIO;
2485 goto ioerr;
2486 }
2487 mb = page_address(page);
2488
2489 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
2490 mb->version != R5LOG_VERSION) {
2491 create_super = true;
2492 goto create;
2493 }
2494 stored_crc = le32_to_cpu(mb->checksum);
2495 mb->checksum = 0;
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07002496 expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002497 if (stored_crc != expected_crc) {
2498 create_super = true;
2499 goto create;
2500 }
2501 if (le64_to_cpu(mb->position) != cp) {
2502 create_super = true;
2503 goto create;
2504 }
2505create:
2506 if (create_super) {
2507 log->last_cp_seq = prandom_u32();
2508 cp = 0;
Zhengyuan Liu56056c22016-10-24 16:15:59 +08002509 r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002510 /*
2511 * Make sure super points to correct address. Log might have
2512 * data very soon. If super hasn't correct log tail address,
2513 * recovery can't find the log
2514 */
2515 r5l_write_super(log, cp);
2516 } else
2517 log->last_cp_seq = le64_to_cpu(mb->seq);
2518
2519 log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
Shaohua Li0576b1c2015-08-13 14:32:00 -07002520 log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
2521 if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
2522 log->max_free_space = RECLAIM_MAX_FREE_SPACE;
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002523 log->last_checkpoint = cp;
Zhengyuan Liu28cd88e2016-10-24 09:55:20 +08002524 log->next_checkpoint = cp;
Song Liua39f7af2016-11-17 15:24:40 -08002525 mutex_lock(&log->io_mutex);
2526 r5c_update_log_state(log);
2527 mutex_unlock(&log->io_mutex);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002528
2529 __free_page(page);
2530
2531 return r5l_recovery_log(log);
2532ioerr:
2533 __free_page(page);
2534 return ret;
2535}
2536
2537int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
2538{
Jens Axboec888a8f2016-04-13 13:33:19 -06002539 struct request_queue *q = bdev_get_queue(rdev->bdev);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002540 struct r5l_log *log;
2541
2542 if (PAGE_SIZE != 4096)
2543 return -EINVAL;
Song Liuc757ec92016-11-17 15:24:36 -08002544
2545 /*
2546 * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and
2547 * raid_disks r5l_payload_data_parity.
2548 *
2549 * Write journal and cache does not work for very big array
2550 * (raid_disks > 203)
2551 */
2552 if (sizeof(struct r5l_meta_block) +
2553 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) *
2554 conf->raid_disks) > PAGE_SIZE) {
2555 pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n",
2556 mdname(conf->mddev), conf->raid_disks);
2557 return -EINVAL;
2558 }
2559
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002560 log = kzalloc(sizeof(*log), GFP_KERNEL);
2561 if (!log)
2562 return -ENOMEM;
2563 log->rdev = rdev;
2564
Jens Axboec888a8f2016-04-13 13:33:19 -06002565 log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0;
Christoph Hellwig56fef7c2015-10-05 09:31:09 +02002566
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07002567 log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
2568 sizeof(rdev->mddev->uuid));
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002569
2570 mutex_init(&log->io_mutex);
2571
2572 spin_lock_init(&log->io_list_lock);
2573 INIT_LIST_HEAD(&log->running_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -07002574 INIT_LIST_HEAD(&log->io_end_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07002575 INIT_LIST_HEAD(&log->flushing_ios);
Christoph Hellwig04732f72015-10-05 09:31:07 +02002576 INIT_LIST_HEAD(&log->finished_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07002577 bio_init(&log->flush_bio);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002578
2579 log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
2580 if (!log->io_kc)
2581 goto io_kc;
2582
Christoph Hellwig5036c3902015-12-21 10:51:02 +11002583 log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc);
2584 if (!log->io_pool)
2585 goto io_pool;
2586
Christoph Hellwigc38d29b2015-12-21 10:51:02 +11002587 log->bs = bioset_create(R5L_POOL_SIZE, 0);
2588 if (!log->bs)
2589 goto io_bs;
2590
Christoph Hellwige8deb632015-12-21 10:51:02 +11002591 log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
2592 if (!log->meta_pool)
2593 goto out_mempool;
2594
Shaohua Li0576b1c2015-08-13 14:32:00 -07002595 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
2596 log->rdev->mddev, "reclaim");
2597 if (!log->reclaim_thread)
2598 goto reclaim_thread;
Song Liua39f7af2016-11-17 15:24:40 -08002599 log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
2600
Shaohua Li0fd22b42015-09-02 13:49:47 -07002601 init_waitqueue_head(&log->iounit_wait);
Shaohua Li0576b1c2015-08-13 14:32:00 -07002602
Christoph Hellwig5036c3902015-12-21 10:51:02 +11002603 INIT_LIST_HEAD(&log->no_mem_stripes);
2604
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002605 INIT_LIST_HEAD(&log->no_space_stripes);
2606 spin_lock_init(&log->no_space_stripes_lock);
2607
Song Liu3bddb7f2016-11-18 16:46:50 -08002608 INIT_WORK(&log->deferred_io_work, r5l_submit_io_async);
2609
Song Liu2ded3702016-11-17 15:24:38 -08002610 log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
Song Liua39f7af2016-11-17 15:24:40 -08002611 INIT_LIST_HEAD(&log->stripe_in_journal_list);
2612 spin_lock_init(&log->stripe_in_journal_lock);
2613 atomic_set(&log->stripe_in_journal_count, 0);
Song Liu2ded3702016-11-17 15:24:38 -08002614
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002615 if (r5l_load_log(log))
2616 goto error;
2617
Shaohua Lif6b6ec52015-12-21 10:51:02 +11002618 rcu_assign_pointer(conf->log, log);
Shaohua Lia62ab492016-01-06 14:37:13 -08002619 set_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002620 return 0;
Christoph Hellwige8deb632015-12-21 10:51:02 +11002621
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002622error:
Shaohua Li0576b1c2015-08-13 14:32:00 -07002623 md_unregister_thread(&log->reclaim_thread);
2624reclaim_thread:
Christoph Hellwige8deb632015-12-21 10:51:02 +11002625 mempool_destroy(log->meta_pool);
2626out_mempool:
Christoph Hellwigc38d29b2015-12-21 10:51:02 +11002627 bioset_free(log->bs);
2628io_bs:
Christoph Hellwig5036c3902015-12-21 10:51:02 +11002629 mempool_destroy(log->io_pool);
2630io_pool:
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002631 kmem_cache_destroy(log->io_kc);
2632io_kc:
2633 kfree(log);
2634 return -EINVAL;
2635}
2636
2637void r5l_exit_log(struct r5l_log *log)
2638{
Shaohua Li0576b1c2015-08-13 14:32:00 -07002639 md_unregister_thread(&log->reclaim_thread);
Christoph Hellwige8deb632015-12-21 10:51:02 +11002640 mempool_destroy(log->meta_pool);
Christoph Hellwigc38d29b2015-12-21 10:51:02 +11002641 bioset_free(log->bs);
Christoph Hellwig5036c3902015-12-21 10:51:02 +11002642 mempool_destroy(log->io_pool);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07002643 kmem_cache_destroy(log->io_kc);
2644 kfree(log);
2645}