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