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