]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/md/raid5-cache.c
raid5-cache: use a bio_set
[mirror_ubuntu-artful-kernel.git] / drivers / md / raid5-cache.c
CommitLineData
f6bed0ef
SL
1/*
2 * Copyright (C) 2015 Shaohua Li <shli@fb.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 */
14#include <linux/kernel.h>
15#include <linux/wait.h>
16#include <linux/blkdev.h>
17#include <linux/slab.h>
18#include <linux/raid/md_p.h>
5cb2fbd6 19#include <linux/crc32c.h>
f6bed0ef
SL
20#include <linux/random.h>
21#include "md.h"
22#include "raid5.h"
23
24/*
25 * metadata/data stored in disk with 4k size unit (a block) regardless
26 * underneath hardware sector size. only works with PAGE_SIZE == 4096
27 */
28#define BLOCK_SECTORS (8)
29
0576b1c6
SL
30/*
31 * reclaim runs every 1/4 disk size or 10G reclaimable space. This can prevent
32 * recovery scans a very long log
33 */
34#define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
35#define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
36
c38d29b3
CH
37/*
38 * We only need 2 bios per I/O unit to make progress, but ensure we
39 * have a few more available to not get too tight.
40 */
41#define R5L_POOL_SIZE 4
42
f6bed0ef
SL
43struct r5l_log {
44 struct md_rdev *rdev;
45
46 u32 uuid_checksum;
47
48 sector_t device_size; /* log device size, round to
49 * BLOCK_SECTORS */
0576b1c6
SL
50 sector_t max_free_space; /* reclaim run if free space is at
51 * this size */
f6bed0ef
SL
52
53 sector_t last_checkpoint; /* log tail. where recovery scan
54 * starts from */
55 u64 last_cp_seq; /* log tail sequence */
56
57 sector_t log_start; /* log head. where new data appends */
58 u64 seq; /* log head sequence */
59
17036461
CH
60 sector_t next_checkpoint;
61 u64 next_cp_seq;
62
f6bed0ef
SL
63 struct mutex io_mutex;
64 struct r5l_io_unit *current_io; /* current io_unit accepting new data */
65
66 spinlock_t io_list_lock;
67 struct list_head running_ios; /* io_units which are still running,
68 * and have not yet been completely
69 * written to the log */
70 struct list_head io_end_ios; /* io_units which have been completely
71 * written to the log but not yet written
72 * to the RAID */
a8c34f91
SL
73 struct list_head flushing_ios; /* io_units which are waiting for log
74 * cache flush */
04732f74 75 struct list_head finished_ios; /* io_units which settle down in log disk */
a8c34f91 76 struct bio flush_bio;
f6bed0ef
SL
77
78 struct kmem_cache *io_kc;
c38d29b3 79 struct bio_set *bs;
f6bed0ef 80
0576b1c6
SL
81 struct md_thread *reclaim_thread;
82 unsigned long reclaim_target; /* number of space that need to be
83 * reclaimed. if it's 0, reclaim spaces
84 * used by io_units which are in
85 * IO_UNIT_STRIPE_END state (eg, reclaim
86 * dones't wait for specific io_unit
87 * switching to IO_UNIT_STRIPE_END
88 * state) */
0fd22b45 89 wait_queue_head_t iounit_wait;
0576b1c6 90
f6bed0ef
SL
91 struct list_head no_space_stripes; /* pending stripes, log has no space */
92 spinlock_t no_space_stripes_lock;
56fef7c6
CH
93
94 bool need_cache_flush;
4b482044 95 bool in_teardown;
f6bed0ef
SL
96};
97
98/*
99 * an IO range starts from a meta data block and end at the next meta data
100 * block. The io unit's the meta data block tracks data/parity followed it. io
101 * unit is written to log disk with normal write, as we always flush log disk
102 * first and then start move data to raid disks, there is no requirement to
103 * write io unit with FLUSH/FUA
104 */
105struct r5l_io_unit {
106 struct r5l_log *log;
107
108 struct page *meta_page; /* store meta block */
109 int meta_offset; /* current offset in meta_page */
110
f6bed0ef
SL
111 struct bio *current_bio;/* current_bio accepting new data */
112
113 atomic_t pending_stripe;/* how many stripes not flushed to raid */
114 u64 seq; /* seq number of the metablock */
115 sector_t log_start; /* where the io_unit starts */
116 sector_t log_end; /* where the io_unit ends */
117 struct list_head log_sibling; /* log->running_ios */
118 struct list_head stripe_list; /* stripes added to the io_unit */
119
120 int state;
6143e2ce 121 bool need_split_bio;
f6bed0ef
SL
122};
123
124/* r5l_io_unit state */
125enum r5l_io_unit_state {
126 IO_UNIT_RUNNING = 0, /* accepting new IO */
127 IO_UNIT_IO_START = 1, /* io_unit bio start writing to log,
128 * don't accepting new bio */
129 IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */
a8c34f91 130 IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
f6bed0ef
SL
131};
132
133static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
134{
135 start += inc;
136 if (start >= log->device_size)
137 start = start - log->device_size;
138 return start;
139}
140
141static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
142 sector_t end)
143{
144 if (end >= start)
145 return end - start;
146 else
147 return end + log->device_size - start;
148}
149
150static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
151{
152 sector_t used_size;
153
154 used_size = r5l_ring_distance(log, log->last_checkpoint,
155 log->log_start);
156
157 return log->device_size > used_size + size;
158}
159
f6bed0ef
SL
160static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
161 enum r5l_io_unit_state state)
162{
f6bed0ef
SL
163 if (WARN_ON(io->state >= state))
164 return;
165 io->state = state;
f6bed0ef
SL
166}
167
d8858f43
CH
168static void r5l_io_run_stripes(struct r5l_io_unit *io)
169{
170 struct stripe_head *sh, *next;
171
172 list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
173 list_del_init(&sh->log_list);
174 set_bit(STRIPE_HANDLE, &sh->state);
175 raid5_release_stripe(sh);
176 }
177}
178
56fef7c6
CH
179static void r5l_log_run_stripes(struct r5l_log *log)
180{
181 struct r5l_io_unit *io, *next;
182
183 assert_spin_locked(&log->io_list_lock);
184
185 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
186 /* don't change list order */
187 if (io->state < IO_UNIT_IO_END)
188 break;
189
190 list_move_tail(&io->log_sibling, &log->finished_ios);
191 r5l_io_run_stripes(io);
192 }
193}
194
3848c0bc
CH
195static void r5l_move_to_end_ios(struct r5l_log *log)
196{
197 struct r5l_io_unit *io, *next;
198
199 assert_spin_locked(&log->io_list_lock);
200
201 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
202 /* don't change list order */
203 if (io->state < IO_UNIT_IO_END)
204 break;
205 list_move_tail(&io->log_sibling, &log->io_end_ios);
206 }
207}
208
f6bed0ef
SL
209static void r5l_log_endio(struct bio *bio)
210{
211 struct r5l_io_unit *io = bio->bi_private;
212 struct r5l_log *log = io->log;
509ffec7 213 unsigned long flags;
f6bed0ef 214
6e74a9cf
SL
215 if (bio->bi_error)
216 md_error(log->rdev->mddev, log->rdev);
217
f6bed0ef 218 bio_put(bio);
ad66d445 219 __free_page(io->meta_page);
f6bed0ef 220
509ffec7
CH
221 spin_lock_irqsave(&log->io_list_lock, flags);
222 __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
56fef7c6 223 if (log->need_cache_flush)
3848c0bc 224 r5l_move_to_end_ios(log);
56fef7c6
CH
225 else
226 r5l_log_run_stripes(log);
509ffec7
CH
227 spin_unlock_irqrestore(&log->io_list_lock, flags);
228
56fef7c6
CH
229 if (log->need_cache_flush)
230 md_wakeup_thread(log->rdev->mddev->thread);
f6bed0ef
SL
231}
232
233static void r5l_submit_current_io(struct r5l_log *log)
234{
235 struct r5l_io_unit *io = log->current_io;
236 struct r5l_meta_block *block;
509ffec7 237 unsigned long flags;
f6bed0ef
SL
238 u32 crc;
239
240 if (!io)
241 return;
242
243 block = page_address(io->meta_page);
244 block->meta_size = cpu_to_le32(io->meta_offset);
5cb2fbd6 245 crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
f6bed0ef
SL
246 block->checksum = cpu_to_le32(crc);
247
248 log->current_io = NULL;
509ffec7
CH
249 spin_lock_irqsave(&log->io_list_lock, flags);
250 __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
251 spin_unlock_irqrestore(&log->io_list_lock, flags);
f6bed0ef 252
6143e2ce 253 submit_bio(WRITE, io->current_bio);
f6bed0ef
SL
254}
255
6143e2ce 256static struct bio *r5l_bio_alloc(struct r5l_log *log)
b349feb3 257{
c38d29b3 258 struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
b349feb3
CH
259
260 bio->bi_rw = WRITE;
261 bio->bi_bdev = log->rdev->bdev;
1e932a37 262 bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
b349feb3 263
b349feb3
CH
264 return bio;
265}
266
c1b99198
CH
267static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
268{
269 log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
270
271 /*
272 * If we filled up the log device start from the beginning again,
273 * which will require a new bio.
274 *
275 * Note: for this to work properly the log size needs to me a multiple
276 * of BLOCK_SECTORS.
277 */
278 if (log->log_start == 0)
6143e2ce 279 io->need_split_bio = true;
c1b99198
CH
280
281 io->log_end = log->log_start;
282}
283
f6bed0ef
SL
284static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
285{
286 struct r5l_io_unit *io;
287 struct r5l_meta_block *block;
f6bed0ef 288
51039cd0
CH
289 /* We can't handle memory allocate failure so far */
290 io = kmem_cache_zalloc(log->io_kc, GFP_NOIO | __GFP_NOFAIL);
291 io->log = log;
51039cd0
CH
292 INIT_LIST_HEAD(&io->log_sibling);
293 INIT_LIST_HEAD(&io->stripe_list);
294 io->state = IO_UNIT_RUNNING;
f6bed0ef 295
51039cd0 296 io->meta_page = alloc_page(GFP_NOIO | __GFP_NOFAIL | __GFP_ZERO);
f6bed0ef
SL
297 block = page_address(io->meta_page);
298 block->magic = cpu_to_le32(R5LOG_MAGIC);
299 block->version = R5LOG_VERSION;
300 block->seq = cpu_to_le64(log->seq);
301 block->position = cpu_to_le64(log->log_start);
302
303 io->log_start = log->log_start;
304 io->meta_offset = sizeof(struct r5l_meta_block);
2b8ef16e 305 io->seq = log->seq++;
f6bed0ef 306
6143e2ce
CH
307 io->current_bio = r5l_bio_alloc(log);
308 io->current_bio->bi_end_io = r5l_log_endio;
309 io->current_bio->bi_private = io;
b349feb3 310 bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
f6bed0ef 311
c1b99198 312 r5_reserve_log_entry(log, io);
f6bed0ef
SL
313
314 spin_lock_irq(&log->io_list_lock);
315 list_add_tail(&io->log_sibling, &log->running_ios);
316 spin_unlock_irq(&log->io_list_lock);
317
318 return io;
319}
320
321static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
322{
22581f58
CH
323 if (log->current_io &&
324 log->current_io->meta_offset + payload_size > PAGE_SIZE)
f6bed0ef 325 r5l_submit_current_io(log);
f6bed0ef 326
22581f58
CH
327 if (!log->current_io)
328 log->current_io = r5l_new_meta(log);
f6bed0ef
SL
329 return 0;
330}
331
332static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
333 sector_t location,
334 u32 checksum1, u32 checksum2,
335 bool checksum2_valid)
336{
337 struct r5l_io_unit *io = log->current_io;
338 struct r5l_payload_data_parity *payload;
339
340 payload = page_address(io->meta_page) + io->meta_offset;
341 payload->header.type = cpu_to_le16(type);
342 payload->header.flags = cpu_to_le16(0);
343 payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
344 (PAGE_SHIFT - 9));
345 payload->location = cpu_to_le64(location);
346 payload->checksum[0] = cpu_to_le32(checksum1);
347 if (checksum2_valid)
348 payload->checksum[1] = cpu_to_le32(checksum2);
349
350 io->meta_offset += sizeof(struct r5l_payload_data_parity) +
351 sizeof(__le32) * (1 + !!checksum2_valid);
352}
353
354static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
355{
356 struct r5l_io_unit *io = log->current_io;
357
6143e2ce
CH
358 if (io->need_split_bio) {
359 struct bio *prev = io->current_bio;
b349feb3 360
6143e2ce
CH
361 io->current_bio = r5l_bio_alloc(log);
362 bio_chain(io->current_bio, prev);
363
364 submit_bio(WRITE, prev);
f6bed0ef 365 }
f6bed0ef 366
6143e2ce
CH
367 if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
368 BUG();
369
c1b99198 370 r5_reserve_log_entry(log, io);
f6bed0ef
SL
371}
372
373static void r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
374 int data_pages, int parity_pages)
375{
376 int i;
377 int meta_size;
378 struct r5l_io_unit *io;
379
380 meta_size =
381 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
382 * data_pages) +
383 sizeof(struct r5l_payload_data_parity) +
384 sizeof(__le32) * parity_pages;
385
386 r5l_get_meta(log, meta_size);
387 io = log->current_io;
388
389 for (i = 0; i < sh->disks; i++) {
390 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
391 continue;
392 if (i == sh->pd_idx || i == sh->qd_idx)
393 continue;
394 r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
395 raid5_compute_blocknr(sh, i, 0),
396 sh->dev[i].log_checksum, 0, false);
397 r5l_append_payload_page(log, sh->dev[i].page);
398 }
399
400 if (sh->qd_idx >= 0) {
401 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
402 sh->sector, sh->dev[sh->pd_idx].log_checksum,
403 sh->dev[sh->qd_idx].log_checksum, true);
404 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
405 r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
406 } else {
407 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
408 sh->sector, sh->dev[sh->pd_idx].log_checksum,
409 0, false);
410 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
411 }
412
413 list_add_tail(&sh->log_list, &io->stripe_list);
414 atomic_inc(&io->pending_stripe);
415 sh->log_io = io;
416}
417
509ffec7 418static void r5l_wake_reclaim(struct r5l_log *log, sector_t space);
f6bed0ef
SL
419/*
420 * running in raid5d, where reclaim could wait for raid5d too (when it flushes
421 * data from log to raid disks), so we shouldn't wait for reclaim here
422 */
423int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
424{
425 int write_disks = 0;
426 int data_pages, parity_pages;
427 int meta_size;
428 int reserve;
429 int i;
430
431 if (!log)
432 return -EAGAIN;
433 /* Don't support stripe batch */
434 if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
435 test_bit(STRIPE_SYNCING, &sh->state)) {
436 /* the stripe is written to log, we start writing it to raid */
437 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
438 return -EAGAIN;
439 }
440
441 for (i = 0; i < sh->disks; i++) {
442 void *addr;
443
444 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
445 continue;
446 write_disks++;
447 /* checksum is already calculated in last run */
448 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
449 continue;
450 addr = kmap_atomic(sh->dev[i].page);
5cb2fbd6
SL
451 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
452 addr, PAGE_SIZE);
f6bed0ef
SL
453 kunmap_atomic(addr);
454 }
455 parity_pages = 1 + !!(sh->qd_idx >= 0);
456 data_pages = write_disks - parity_pages;
457
458 meta_size =
459 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
460 * data_pages) +
461 sizeof(struct r5l_payload_data_parity) +
462 sizeof(__le32) * parity_pages;
463 /* Doesn't work with very big raid array */
464 if (meta_size + sizeof(struct r5l_meta_block) > PAGE_SIZE)
465 return -EINVAL;
466
467 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
253f9fd4
SL
468 /*
469 * The stripe must enter state machine again to finish the write, so
470 * don't delay.
471 */
472 clear_bit(STRIPE_DELAYED, &sh->state);
f6bed0ef
SL
473 atomic_inc(&sh->count);
474
475 mutex_lock(&log->io_mutex);
476 /* meta + data */
477 reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
478 if (r5l_has_free_space(log, reserve))
479 r5l_log_stripe(log, sh, data_pages, parity_pages);
480 else {
481 spin_lock(&log->no_space_stripes_lock);
482 list_add_tail(&sh->log_list, &log->no_space_stripes);
483 spin_unlock(&log->no_space_stripes_lock);
484
485 r5l_wake_reclaim(log, reserve);
486 }
487 mutex_unlock(&log->io_mutex);
488
489 return 0;
490}
491
492void r5l_write_stripe_run(struct r5l_log *log)
493{
494 if (!log)
495 return;
496 mutex_lock(&log->io_mutex);
497 r5l_submit_current_io(log);
498 mutex_unlock(&log->io_mutex);
499}
500
828cbe98
SL
501int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
502{
503 if (!log)
504 return -ENODEV;
505 /*
506 * we flush log disk cache first, then write stripe data to raid disks.
507 * So if bio is finished, the log disk cache is flushed already. The
508 * recovery guarantees we can recovery the bio from log disk, so we
509 * don't need to flush again
510 */
511 if (bio->bi_iter.bi_size == 0) {
512 bio_endio(bio);
513 return 0;
514 }
515 bio->bi_rw &= ~REQ_FLUSH;
516 return -EAGAIN;
517}
518
f6bed0ef
SL
519/* This will run after log space is reclaimed */
520static void r5l_run_no_space_stripes(struct r5l_log *log)
521{
522 struct stripe_head *sh;
523
524 spin_lock(&log->no_space_stripes_lock);
525 while (!list_empty(&log->no_space_stripes)) {
526 sh = list_first_entry(&log->no_space_stripes,
527 struct stripe_head, log_list);
528 list_del_init(&sh->log_list);
529 set_bit(STRIPE_HANDLE, &sh->state);
530 raid5_release_stripe(sh);
531 }
532 spin_unlock(&log->no_space_stripes_lock);
533}
534
17036461
CH
535static sector_t r5l_reclaimable_space(struct r5l_log *log)
536{
537 return r5l_ring_distance(log, log->last_checkpoint,
538 log->next_checkpoint);
539}
540
04732f74 541static bool r5l_complete_finished_ios(struct r5l_log *log)
17036461
CH
542{
543 struct r5l_io_unit *io, *next;
544 bool found = false;
545
546 assert_spin_locked(&log->io_list_lock);
547
04732f74 548 list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
17036461
CH
549 /* don't change list order */
550 if (io->state < IO_UNIT_STRIPE_END)
551 break;
552
553 log->next_checkpoint = io->log_start;
554 log->next_cp_seq = io->seq;
555
556 list_del(&io->log_sibling);
ad66d445 557 kmem_cache_free(log->io_kc, io);
17036461
CH
558
559 found = true;
560 }
561
562 return found;
563}
564
509ffec7
CH
565static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
566{
567 struct r5l_log *log = io->log;
509ffec7
CH
568 unsigned long flags;
569
570 spin_lock_irqsave(&log->io_list_lock, flags);
571 __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
17036461 572
04732f74 573 if (!r5l_complete_finished_ios(log)) {
85f2f9a4
SL
574 spin_unlock_irqrestore(&log->io_list_lock, flags);
575 return;
576 }
509ffec7 577
17036461 578 if (r5l_reclaimable_space(log) > log->max_free_space)
509ffec7
CH
579 r5l_wake_reclaim(log, 0);
580
509ffec7
CH
581 spin_unlock_irqrestore(&log->io_list_lock, flags);
582 wake_up(&log->iounit_wait);
583}
584
0576b1c6
SL
585void r5l_stripe_write_finished(struct stripe_head *sh)
586{
587 struct r5l_io_unit *io;
588
0576b1c6 589 io = sh->log_io;
0576b1c6
SL
590 sh->log_io = NULL;
591
509ffec7
CH
592 if (io && atomic_dec_and_test(&io->pending_stripe))
593 __r5l_stripe_write_finished(io);
0576b1c6
SL
594}
595
a8c34f91
SL
596static void r5l_log_flush_endio(struct bio *bio)
597{
598 struct r5l_log *log = container_of(bio, struct r5l_log,
599 flush_bio);
600 unsigned long flags;
601 struct r5l_io_unit *io;
a8c34f91 602
6e74a9cf
SL
603 if (bio->bi_error)
604 md_error(log->rdev->mddev, log->rdev);
605
a8c34f91 606 spin_lock_irqsave(&log->io_list_lock, flags);
d8858f43
CH
607 list_for_each_entry(io, &log->flushing_ios, log_sibling)
608 r5l_io_run_stripes(io);
04732f74 609 list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
a8c34f91
SL
610 spin_unlock_irqrestore(&log->io_list_lock, flags);
611}
612
0576b1c6
SL
613/*
614 * Starting dispatch IO to raid.
615 * io_unit(meta) consists of a log. There is one situation we want to avoid. A
616 * broken meta in the middle of a log causes recovery can't find meta at the
617 * head of log. If operations require meta at the head persistent in log, we
618 * must make sure meta before it persistent in log too. A case is:
619 *
620 * stripe data/parity is in log, we start write stripe to raid disks. stripe
621 * data/parity must be persistent in log before we do the write to raid disks.
622 *
623 * The solution is we restrictly maintain io_unit list order. In this case, we
624 * only write stripes of an io_unit to raid disks till the io_unit is the first
625 * one whose data/parity is in log.
626 */
627void r5l_flush_stripe_to_raid(struct r5l_log *log)
628{
a8c34f91 629 bool do_flush;
56fef7c6
CH
630
631 if (!log || !log->need_cache_flush)
0576b1c6 632 return;
0576b1c6
SL
633
634 spin_lock_irq(&log->io_list_lock);
a8c34f91
SL
635 /* flush bio is running */
636 if (!list_empty(&log->flushing_ios)) {
637 spin_unlock_irq(&log->io_list_lock);
638 return;
0576b1c6 639 }
a8c34f91
SL
640 list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
641 do_flush = !list_empty(&log->flushing_ios);
0576b1c6 642 spin_unlock_irq(&log->io_list_lock);
a8c34f91
SL
643
644 if (!do_flush)
645 return;
646 bio_reset(&log->flush_bio);
647 log->flush_bio.bi_bdev = log->rdev->bdev;
648 log->flush_bio.bi_end_io = r5l_log_flush_endio;
649 submit_bio(WRITE_FLUSH, &log->flush_bio);
0576b1c6
SL
650}
651
0576b1c6 652static void r5l_write_super(struct r5l_log *log, sector_t cp);
4b482044
SL
653static void r5l_write_super_and_discard_space(struct r5l_log *log,
654 sector_t end)
655{
656 struct block_device *bdev = log->rdev->bdev;
657 struct mddev *mddev;
658
659 r5l_write_super(log, end);
660
661 if (!blk_queue_discard(bdev_get_queue(bdev)))
662 return;
663
664 mddev = log->rdev->mddev;
665 /*
666 * This is to avoid a deadlock. r5l_quiesce holds reconfig_mutex and
667 * wait for this thread to finish. This thread waits for
668 * MD_CHANGE_PENDING clear, which is supposed to be done in
669 * md_check_recovery(). md_check_recovery() tries to get
670 * reconfig_mutex. Since r5l_quiesce already holds the mutex,
671 * md_check_recovery() fails, so the PENDING never get cleared. The
672 * in_teardown check workaround this issue.
673 */
674 if (!log->in_teardown) {
675 set_bit(MD_CHANGE_DEVS, &mddev->flags);
676 set_bit(MD_CHANGE_PENDING, &mddev->flags);
677 md_wakeup_thread(mddev->thread);
678 wait_event(mddev->sb_wait,
679 !test_bit(MD_CHANGE_PENDING, &mddev->flags) ||
680 log->in_teardown);
681 /*
682 * r5l_quiesce could run after in_teardown check and hold
683 * mutex first. Superblock might get updated twice.
684 */
685 if (log->in_teardown)
686 md_update_sb(mddev, 1);
687 } else {
688 WARN_ON(!mddev_is_locked(mddev));
689 md_update_sb(mddev, 1);
690 }
691
6e74a9cf 692 /* discard IO error really doesn't matter, ignore it */
4b482044
SL
693 if (log->last_checkpoint < end) {
694 blkdev_issue_discard(bdev,
695 log->last_checkpoint + log->rdev->data_offset,
696 end - log->last_checkpoint, GFP_NOIO, 0);
697 } else {
698 blkdev_issue_discard(bdev,
699 log->last_checkpoint + log->rdev->data_offset,
700 log->device_size - log->last_checkpoint,
701 GFP_NOIO, 0);
702 blkdev_issue_discard(bdev, log->rdev->data_offset, end,
703 GFP_NOIO, 0);
704 }
705}
706
707
0576b1c6
SL
708static void r5l_do_reclaim(struct r5l_log *log)
709{
0576b1c6 710 sector_t reclaim_target = xchg(&log->reclaim_target, 0);
17036461
CH
711 sector_t reclaimable;
712 sector_t next_checkpoint;
713 u64 next_cp_seq;
0576b1c6
SL
714
715 spin_lock_irq(&log->io_list_lock);
716 /*
717 * move proper io_unit to reclaim list. We should not change the order.
718 * reclaimable/unreclaimable io_unit can be mixed in the list, we
719 * shouldn't reuse space of an unreclaimable io_unit
720 */
721 while (1) {
17036461
CH
722 reclaimable = r5l_reclaimable_space(log);
723 if (reclaimable >= reclaim_target ||
0576b1c6
SL
724 (list_empty(&log->running_ios) &&
725 list_empty(&log->io_end_ios) &&
a8c34f91 726 list_empty(&log->flushing_ios) &&
04732f74 727 list_empty(&log->finished_ios)))
0576b1c6
SL
728 break;
729
17036461
CH
730 md_wakeup_thread(log->rdev->mddev->thread);
731 wait_event_lock_irq(log->iounit_wait,
732 r5l_reclaimable_space(log) > reclaimable,
733 log->io_list_lock);
0576b1c6 734 }
17036461
CH
735
736 next_checkpoint = log->next_checkpoint;
737 next_cp_seq = log->next_cp_seq;
0576b1c6
SL
738 spin_unlock_irq(&log->io_list_lock);
739
17036461
CH
740 BUG_ON(reclaimable < 0);
741 if (reclaimable == 0)
0576b1c6
SL
742 return;
743
0576b1c6
SL
744 /*
745 * write_super will flush cache of each raid disk. We must write super
746 * here, because the log area might be reused soon and we don't want to
747 * confuse recovery
748 */
4b482044 749 r5l_write_super_and_discard_space(log, next_checkpoint);
0576b1c6
SL
750
751 mutex_lock(&log->io_mutex);
17036461
CH
752 log->last_checkpoint = next_checkpoint;
753 log->last_cp_seq = next_cp_seq;
0576b1c6 754 mutex_unlock(&log->io_mutex);
0576b1c6 755
17036461 756 r5l_run_no_space_stripes(log);
0576b1c6
SL
757}
758
759static void r5l_reclaim_thread(struct md_thread *thread)
760{
761 struct mddev *mddev = thread->mddev;
762 struct r5conf *conf = mddev->private;
763 struct r5l_log *log = conf->log;
764
765 if (!log)
766 return;
767 r5l_do_reclaim(log);
768}
769
f6bed0ef
SL
770static void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
771{
0576b1c6
SL
772 unsigned long target;
773 unsigned long new = (unsigned long)space; /* overflow in theory */
774
775 do {
776 target = log->reclaim_target;
777 if (new < target)
778 return;
779 } while (cmpxchg(&log->reclaim_target, target, new) != target);
780 md_wakeup_thread(log->reclaim_thread);
f6bed0ef
SL
781}
782
e6c033f7
SL
783void r5l_quiesce(struct r5l_log *log, int state)
784{
4b482044 785 struct mddev *mddev;
e6c033f7
SL
786 if (!log || state == 2)
787 return;
788 if (state == 0) {
4b482044 789 log->in_teardown = 0;
e6c033f7
SL
790 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
791 log->rdev->mddev, "reclaim");
792 } else if (state == 1) {
793 /*
794 * at this point all stripes are finished, so io_unit is at
795 * least in STRIPE_END state
796 */
4b482044
SL
797 log->in_teardown = 1;
798 /* make sure r5l_write_super_and_discard_space exits */
799 mddev = log->rdev->mddev;
800 wake_up(&mddev->sb_wait);
e6c033f7
SL
801 r5l_wake_reclaim(log, -1L);
802 md_unregister_thread(&log->reclaim_thread);
803 r5l_do_reclaim(log);
804 }
805}
806
6e74a9cf
SL
807bool r5l_log_disk_error(struct r5conf *conf)
808{
f6b6ec5c
SL
809 struct r5l_log *log;
810 bool ret;
7dde2ad3 811 /* don't allow write if journal disk is missing */
f6b6ec5c
SL
812 rcu_read_lock();
813 log = rcu_dereference(conf->log);
814
815 if (!log)
816 ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
817 else
818 ret = test_bit(Faulty, &log->rdev->flags);
819 rcu_read_unlock();
820 return ret;
6e74a9cf
SL
821}
822
355810d1
SL
823struct r5l_recovery_ctx {
824 struct page *meta_page; /* current meta */
825 sector_t meta_total_blocks; /* total size of current meta and data */
826 sector_t pos; /* recovery position */
827 u64 seq; /* recovery position seq */
828};
829
830static int r5l_read_meta_block(struct r5l_log *log,
831 struct r5l_recovery_ctx *ctx)
832{
833 struct page *page = ctx->meta_page;
834 struct r5l_meta_block *mb;
835 u32 crc, stored_crc;
836
837 if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, READ, false))
838 return -EIO;
839
840 mb = page_address(page);
841 stored_crc = le32_to_cpu(mb->checksum);
842 mb->checksum = 0;
843
844 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
845 le64_to_cpu(mb->seq) != ctx->seq ||
846 mb->version != R5LOG_VERSION ||
847 le64_to_cpu(mb->position) != ctx->pos)
848 return -EINVAL;
849
5cb2fbd6 850 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
355810d1
SL
851 if (stored_crc != crc)
852 return -EINVAL;
853
854 if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
855 return -EINVAL;
856
857 ctx->meta_total_blocks = BLOCK_SECTORS;
858
859 return 0;
860}
861
862static int r5l_recovery_flush_one_stripe(struct r5l_log *log,
863 struct r5l_recovery_ctx *ctx,
864 sector_t stripe_sect,
865 int *offset, sector_t *log_offset)
866{
867 struct r5conf *conf = log->rdev->mddev->private;
868 struct stripe_head *sh;
869 struct r5l_payload_data_parity *payload;
870 int disk_index;
871
872 sh = raid5_get_active_stripe(conf, stripe_sect, 0, 0, 0);
873 while (1) {
874 payload = page_address(ctx->meta_page) + *offset;
875
876 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) {
877 raid5_compute_sector(conf,
878 le64_to_cpu(payload->location), 0,
879 &disk_index, sh);
880
881 sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
882 sh->dev[disk_index].page, READ, false);
883 sh->dev[disk_index].log_checksum =
884 le32_to_cpu(payload->checksum[0]);
885 set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
886 ctx->meta_total_blocks += BLOCK_SECTORS;
887 } else {
888 disk_index = sh->pd_idx;
889 sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
890 sh->dev[disk_index].page, READ, false);
891 sh->dev[disk_index].log_checksum =
892 le32_to_cpu(payload->checksum[0]);
893 set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
894
895 if (sh->qd_idx >= 0) {
896 disk_index = sh->qd_idx;
897 sync_page_io(log->rdev,
898 r5l_ring_add(log, *log_offset, BLOCK_SECTORS),
899 PAGE_SIZE, sh->dev[disk_index].page,
900 READ, false);
901 sh->dev[disk_index].log_checksum =
902 le32_to_cpu(payload->checksum[1]);
903 set_bit(R5_Wantwrite,
904 &sh->dev[disk_index].flags);
905 }
906 ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
907 }
908
909 *log_offset = r5l_ring_add(log, *log_offset,
910 le32_to_cpu(payload->size));
911 *offset += sizeof(struct r5l_payload_data_parity) +
912 sizeof(__le32) *
913 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
914 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY)
915 break;
916 }
917
918 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
919 void *addr;
920 u32 checksum;
921
922 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
923 continue;
924 addr = kmap_atomic(sh->dev[disk_index].page);
5cb2fbd6 925 checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
355810d1
SL
926 kunmap_atomic(addr);
927 if (checksum != sh->dev[disk_index].log_checksum)
928 goto error;
929 }
930
931 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
932 struct md_rdev *rdev, *rrdev;
933
934 if (!test_and_clear_bit(R5_Wantwrite,
935 &sh->dev[disk_index].flags))
936 continue;
937
938 /* in case device is broken */
939 rdev = rcu_dereference(conf->disks[disk_index].rdev);
940 if (rdev)
941 sync_page_io(rdev, stripe_sect, PAGE_SIZE,
942 sh->dev[disk_index].page, WRITE, false);
943 rrdev = rcu_dereference(conf->disks[disk_index].replacement);
944 if (rrdev)
945 sync_page_io(rrdev, stripe_sect, PAGE_SIZE,
946 sh->dev[disk_index].page, WRITE, false);
947 }
948 raid5_release_stripe(sh);
949 return 0;
950
951error:
952 for (disk_index = 0; disk_index < sh->disks; disk_index++)
953 sh->dev[disk_index].flags = 0;
954 raid5_release_stripe(sh);
955 return -EINVAL;
956}
957
958static int r5l_recovery_flush_one_meta(struct r5l_log *log,
959 struct r5l_recovery_ctx *ctx)
960{
961 struct r5conf *conf = log->rdev->mddev->private;
962 struct r5l_payload_data_parity *payload;
963 struct r5l_meta_block *mb;
964 int offset;
965 sector_t log_offset;
966 sector_t stripe_sector;
967
968 mb = page_address(ctx->meta_page);
969 offset = sizeof(struct r5l_meta_block);
970 log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
971
972 while (offset < le32_to_cpu(mb->meta_size)) {
973 int dd;
974
975 payload = (void *)mb + offset;
976 stripe_sector = raid5_compute_sector(conf,
977 le64_to_cpu(payload->location), 0, &dd, NULL);
978 if (r5l_recovery_flush_one_stripe(log, ctx, stripe_sector,
979 &offset, &log_offset))
980 return -EINVAL;
981 }
982 return 0;
983}
984
985/* copy data/parity from log to raid disks */
986static void r5l_recovery_flush_log(struct r5l_log *log,
987 struct r5l_recovery_ctx *ctx)
988{
989 while (1) {
990 if (r5l_read_meta_block(log, ctx))
991 return;
992 if (r5l_recovery_flush_one_meta(log, ctx))
993 return;
994 ctx->seq++;
995 ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
996 }
997}
998
999static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
1000 u64 seq)
1001{
1002 struct page *page;
1003 struct r5l_meta_block *mb;
1004 u32 crc;
1005
1006 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1007 if (!page)
1008 return -ENOMEM;
1009 mb = page_address(page);
1010 mb->magic = cpu_to_le32(R5LOG_MAGIC);
1011 mb->version = R5LOG_VERSION;
1012 mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
1013 mb->seq = cpu_to_le64(seq);
1014 mb->position = cpu_to_le64(pos);
5cb2fbd6 1015 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
355810d1
SL
1016 mb->checksum = cpu_to_le32(crc);
1017
1018 if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, WRITE_FUA, false)) {
1019 __free_page(page);
1020 return -EIO;
1021 }
1022 __free_page(page);
1023 return 0;
1024}
1025
f6bed0ef
SL
1026static int r5l_recovery_log(struct r5l_log *log)
1027{
355810d1
SL
1028 struct r5l_recovery_ctx ctx;
1029
1030 ctx.pos = log->last_checkpoint;
1031 ctx.seq = log->last_cp_seq;
1032 ctx.meta_page = alloc_page(GFP_KERNEL);
1033 if (!ctx.meta_page)
1034 return -ENOMEM;
1035
1036 r5l_recovery_flush_log(log, &ctx);
1037 __free_page(ctx.meta_page);
1038
1039 /*
1040 * we did a recovery. Now ctx.pos points to an invalid meta block. New
1041 * log will start here. but we can't let superblock point to last valid
1042 * meta block. The log might looks like:
1043 * | meta 1| meta 2| meta 3|
1044 * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
1045 * superblock points to meta 1, we write a new valid meta 2n. if crash
1046 * happens again, new recovery will start from meta 1. Since meta 2n is
1047 * valid now, recovery will think meta 3 is valid, which is wrong.
1048 * The solution is we create a new meta in meta2 with its seq == meta
1049 * 1's seq + 10 and let superblock points to meta2. The same recovery will
1050 * not think meta 3 is a valid meta, because its seq doesn't match
1051 */
1052 if (ctx.seq > log->last_cp_seq + 1) {
1053 int ret;
1054
1055 ret = r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq + 10);
1056 if (ret)
1057 return ret;
1058 log->seq = ctx.seq + 11;
1059 log->log_start = r5l_ring_add(log, ctx.pos, BLOCK_SECTORS);
1060 r5l_write_super(log, ctx.pos);
1061 } else {
1062 log->log_start = ctx.pos;
1063 log->seq = ctx.seq;
1064 }
f6bed0ef
SL
1065 return 0;
1066}
1067
1068static void r5l_write_super(struct r5l_log *log, sector_t cp)
1069{
1070 struct mddev *mddev = log->rdev->mddev;
1071
1072 log->rdev->journal_tail = cp;
1073 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1074}
1075
1076static int r5l_load_log(struct r5l_log *log)
1077{
1078 struct md_rdev *rdev = log->rdev;
1079 struct page *page;
1080 struct r5l_meta_block *mb;
1081 sector_t cp = log->rdev->journal_tail;
1082 u32 stored_crc, expected_crc;
1083 bool create_super = false;
1084 int ret;
1085
1086 /* Make sure it's valid */
1087 if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
1088 cp = 0;
1089 page = alloc_page(GFP_KERNEL);
1090 if (!page)
1091 return -ENOMEM;
1092
1093 if (!sync_page_io(rdev, cp, PAGE_SIZE, page, READ, false)) {
1094 ret = -EIO;
1095 goto ioerr;
1096 }
1097 mb = page_address(page);
1098
1099 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
1100 mb->version != R5LOG_VERSION) {
1101 create_super = true;
1102 goto create;
1103 }
1104 stored_crc = le32_to_cpu(mb->checksum);
1105 mb->checksum = 0;
5cb2fbd6 1106 expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
f6bed0ef
SL
1107 if (stored_crc != expected_crc) {
1108 create_super = true;
1109 goto create;
1110 }
1111 if (le64_to_cpu(mb->position) != cp) {
1112 create_super = true;
1113 goto create;
1114 }
1115create:
1116 if (create_super) {
1117 log->last_cp_seq = prandom_u32();
1118 cp = 0;
1119 /*
1120 * Make sure super points to correct address. Log might have
1121 * data very soon. If super hasn't correct log tail address,
1122 * recovery can't find the log
1123 */
1124 r5l_write_super(log, cp);
1125 } else
1126 log->last_cp_seq = le64_to_cpu(mb->seq);
1127
1128 log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
0576b1c6
SL
1129 log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
1130 if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
1131 log->max_free_space = RECLAIM_MAX_FREE_SPACE;
f6bed0ef
SL
1132 log->last_checkpoint = cp;
1133
1134 __free_page(page);
1135
1136 return r5l_recovery_log(log);
1137ioerr:
1138 __free_page(page);
1139 return ret;
1140}
1141
1142int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
1143{
1144 struct r5l_log *log;
1145
1146 if (PAGE_SIZE != 4096)
1147 return -EINVAL;
1148 log = kzalloc(sizeof(*log), GFP_KERNEL);
1149 if (!log)
1150 return -ENOMEM;
1151 log->rdev = rdev;
1152
56fef7c6
CH
1153 log->need_cache_flush = (rdev->bdev->bd_disk->queue->flush_flags != 0);
1154
5cb2fbd6
SL
1155 log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
1156 sizeof(rdev->mddev->uuid));
f6bed0ef
SL
1157
1158 mutex_init(&log->io_mutex);
1159
1160 spin_lock_init(&log->io_list_lock);
1161 INIT_LIST_HEAD(&log->running_ios);
0576b1c6 1162 INIT_LIST_HEAD(&log->io_end_ios);
a8c34f91 1163 INIT_LIST_HEAD(&log->flushing_ios);
04732f74 1164 INIT_LIST_HEAD(&log->finished_ios);
a8c34f91 1165 bio_init(&log->flush_bio);
f6bed0ef
SL
1166
1167 log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
1168 if (!log->io_kc)
1169 goto io_kc;
1170
c38d29b3
CH
1171 log->bs = bioset_create(R5L_POOL_SIZE, 0);
1172 if (!log->bs)
1173 goto io_bs;
1174
0576b1c6
SL
1175 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
1176 log->rdev->mddev, "reclaim");
1177 if (!log->reclaim_thread)
1178 goto reclaim_thread;
0fd22b45 1179 init_waitqueue_head(&log->iounit_wait);
0576b1c6 1180
f6bed0ef
SL
1181 INIT_LIST_HEAD(&log->no_space_stripes);
1182 spin_lock_init(&log->no_space_stripes_lock);
1183
1184 if (r5l_load_log(log))
1185 goto error;
1186
f6b6ec5c 1187 rcu_assign_pointer(conf->log, log);
f6bed0ef
SL
1188 return 0;
1189error:
0576b1c6
SL
1190 md_unregister_thread(&log->reclaim_thread);
1191reclaim_thread:
c38d29b3
CH
1192 bioset_free(log->bs);
1193io_bs:
f6bed0ef
SL
1194 kmem_cache_destroy(log->io_kc);
1195io_kc:
1196 kfree(log);
1197 return -EINVAL;
1198}
1199
1200void r5l_exit_log(struct r5l_log *log)
1201{
0576b1c6 1202 md_unregister_thread(&log->reclaim_thread);
c38d29b3 1203 bioset_free(log->bs);
f6bed0ef
SL
1204 kmem_cache_destroy(log->io_kc);
1205 kfree(log);
1206}