blob: ea1480392ebac3c0cbad7119a5955823b58a5b46 [file] [log] [blame]
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001/*
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>
Shaohua Li5cb2fbd2015-10-28 08:41:25 -070019#include <linux/crc32c.h>
Shaohua Lif6bed0e2015-08-13 14:31:59 -070020#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
Shaohua Li0576b1c2015-08-13 14:32:00 -070030/*
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
Shaohua Lif6bed0e2015-08-13 14:31:59 -070037struct 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 */
Shaohua Li0576b1c2015-08-13 14:32:00 -070044 sector_t max_free_space; /* reclaim run if free space is at
45 * this size */
Shaohua Lif6bed0e2015-08-13 14:31:59 -070046
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 */
Shaohua Lia8c34f92015-09-02 13:49:46 -070064 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;
Shaohua Li0576b1c2015-08-13 14:32:00 -070068 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 */
Shaohua Lif6bed0e2015-08-13 14:31:59 -070071
72 struct kmem_cache *io_kc;
73
Shaohua Li0576b1c2015-08-13 14:32:00 -070074 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) */
Shaohua Li0fd22b42015-09-02 13:49:47 -070082 wait_queue_head_t iounit_wait;
Shaohua Li0576b1c2015-08-13 14:32:00 -070083
Shaohua Lif6bed0e2015-08-13 14:31:59 -070084 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;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700113};
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 */
Shaohua Lia8c34f92015-09-02 13:49:46 -0700121 IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700122};
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;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700165 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
Shaohua Li0576b1c2015-08-13 14:32:00 -0700189/*
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
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700218static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
219 enum r5l_io_unit_state state)
220{
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700221 if (WARN_ON(io->state >= state))
222 return;
223 io->state = state;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700224}
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;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700231 unsigned long flags;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700232
233 bio_put(bio);
234
235 if (!atomic_dec_and_test(&io->pending_io))
236 return;
237
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700238 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
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700244 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;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700252 unsigned long flags;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700253 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);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700260 crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700261 block->checksum = cpu_to_le32(crc);
262
263 log->current_io = NULL;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700264 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);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700267
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
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700432static void r5l_wake_reclaim(struct r5l_log *log, sector_t space);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700433/*
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);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700465 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
466 addr, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700467 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);
482 atomic_inc(&sh->count);
483
484 mutex_lock(&log->io_mutex);
485 /* meta + data */
486 reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
487 if (r5l_has_free_space(log, reserve))
488 r5l_log_stripe(log, sh, data_pages, parity_pages);
489 else {
490 spin_lock(&log->no_space_stripes_lock);
491 list_add_tail(&sh->log_list, &log->no_space_stripes);
492 spin_unlock(&log->no_space_stripes_lock);
493
494 r5l_wake_reclaim(log, reserve);
495 }
496 mutex_unlock(&log->io_mutex);
497
498 return 0;
499}
500
501void r5l_write_stripe_run(struct r5l_log *log)
502{
503 if (!log)
504 return;
505 mutex_lock(&log->io_mutex);
506 r5l_submit_current_io(log);
507 mutex_unlock(&log->io_mutex);
508}
509
Shaohua Li828cbe92015-09-02 13:49:49 -0700510int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
511{
512 if (!log)
513 return -ENODEV;
514 /*
515 * we flush log disk cache first, then write stripe data to raid disks.
516 * So if bio is finished, the log disk cache is flushed already. The
517 * recovery guarantees we can recovery the bio from log disk, so we
518 * don't need to flush again
519 */
520 if (bio->bi_iter.bi_size == 0) {
521 bio_endio(bio);
522 return 0;
523 }
524 bio->bi_rw &= ~REQ_FLUSH;
525 return -EAGAIN;
526}
527
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700528/* This will run after log space is reclaimed */
529static void r5l_run_no_space_stripes(struct r5l_log *log)
530{
531 struct stripe_head *sh;
532
533 spin_lock(&log->no_space_stripes_lock);
534 while (!list_empty(&log->no_space_stripes)) {
535 sh = list_first_entry(&log->no_space_stripes,
536 struct stripe_head, log_list);
537 list_del_init(&sh->log_list);
538 set_bit(STRIPE_HANDLE, &sh->state);
539 raid5_release_stripe(sh);
540 }
541 spin_unlock(&log->no_space_stripes_lock);
542}
543
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700544static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
545{
546 struct r5l_log *log = io->log;
547 struct r5l_io_unit *last;
548 sector_t reclaimable_space;
549 unsigned long flags;
550
551 spin_lock_irqsave(&log->io_list_lock, flags);
552 __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
553 r5l_move_io_unit_list(&log->flushed_ios, &log->stripe_end_ios,
554 IO_UNIT_STRIPE_END);
555
556 last = list_last_entry(&log->stripe_end_ios,
557 struct r5l_io_unit, log_sibling);
558 reclaimable_space = r5l_ring_distance(log, log->last_checkpoint,
559 last->log_end);
560 if (reclaimable_space >= log->max_free_space)
561 r5l_wake_reclaim(log, 0);
562
563 r5l_compress_stripe_end_list(log);
564 spin_unlock_irqrestore(&log->io_list_lock, flags);
565 wake_up(&log->iounit_wait);
566}
567
Shaohua Li0576b1c2015-08-13 14:32:00 -0700568void r5l_stripe_write_finished(struct stripe_head *sh)
569{
570 struct r5l_io_unit *io;
571
Shaohua Li0576b1c2015-08-13 14:32:00 -0700572 io = sh->log_io;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700573 sh->log_io = NULL;
574
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700575 if (io && atomic_dec_and_test(&io->pending_stripe))
576 __r5l_stripe_write_finished(io);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700577}
578
Shaohua Lia8c34f92015-09-02 13:49:46 -0700579static void r5l_log_flush_endio(struct bio *bio)
580{
581 struct r5l_log *log = container_of(bio, struct r5l_log,
582 flush_bio);
583 unsigned long flags;
584 struct r5l_io_unit *io;
585 struct stripe_head *sh;
586
587 spin_lock_irqsave(&log->io_list_lock, flags);
588 list_for_each_entry(io, &log->flushing_ios, log_sibling) {
589 while (!list_empty(&io->stripe_list)) {
590 sh = list_first_entry(&io->stripe_list,
591 struct stripe_head, log_list);
592 list_del_init(&sh->log_list);
593 set_bit(STRIPE_HANDLE, &sh->state);
594 raid5_release_stripe(sh);
595 }
596 }
597 list_splice_tail_init(&log->flushing_ios, &log->flushed_ios);
598 spin_unlock_irqrestore(&log->io_list_lock, flags);
599}
600
Shaohua Li0576b1c2015-08-13 14:32:00 -0700601/*
602 * Starting dispatch IO to raid.
603 * io_unit(meta) consists of a log. There is one situation we want to avoid. A
604 * broken meta in the middle of a log causes recovery can't find meta at the
605 * head of log. If operations require meta at the head persistent in log, we
606 * must make sure meta before it persistent in log too. A case is:
607 *
608 * stripe data/parity is in log, we start write stripe to raid disks. stripe
609 * data/parity must be persistent in log before we do the write to raid disks.
610 *
611 * The solution is we restrictly maintain io_unit list order. In this case, we
612 * only write stripes of an io_unit to raid disks till the io_unit is the first
613 * one whose data/parity is in log.
614 */
615void r5l_flush_stripe_to_raid(struct r5l_log *log)
616{
Shaohua Lia8c34f92015-09-02 13:49:46 -0700617 bool do_flush;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700618 if (!log)
619 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700620
Shaohua Lia8c34f92015-09-02 13:49:46 -0700621 spin_lock_irq(&log->io_list_lock);
622 /* flush bio is running */
623 if (!list_empty(&log->flushing_ios)) {
624 spin_unlock_irq(&log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700625 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700626 }
Shaohua Lia8c34f92015-09-02 13:49:46 -0700627 list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
628 do_flush = !list_empty(&log->flushing_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700629 spin_unlock_irq(&log->io_list_lock);
Shaohua Lia8c34f92015-09-02 13:49:46 -0700630
631 if (!do_flush)
632 return;
633 bio_reset(&log->flush_bio);
634 log->flush_bio.bi_bdev = log->rdev->bdev;
635 log->flush_bio.bi_end_io = r5l_log_flush_endio;
636 submit_bio(WRITE_FLUSH, &log->flush_bio);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700637}
638
Shaohua Li0fd22b42015-09-02 13:49:47 -0700639static void r5l_kick_io_unit(struct r5l_log *log)
Shaohua Li0576b1c2015-08-13 14:32:00 -0700640{
Shaohua Lia8c34f92015-09-02 13:49:46 -0700641 md_wakeup_thread(log->rdev->mddev->thread);
Shaohua Li0fd22b42015-09-02 13:49:47 -0700642 wait_event_lock_irq(log->iounit_wait, !list_empty(&log->stripe_end_ios),
643 log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700644}
645
646static void r5l_write_super(struct r5l_log *log, sector_t cp);
647static void r5l_do_reclaim(struct r5l_log *log)
648{
649 struct r5l_io_unit *io, *last;
650 LIST_HEAD(list);
651 sector_t free = 0;
652 sector_t reclaim_target = xchg(&log->reclaim_target, 0);
653
654 spin_lock_irq(&log->io_list_lock);
655 /*
656 * move proper io_unit to reclaim list. We should not change the order.
657 * reclaimable/unreclaimable io_unit can be mixed in the list, we
658 * shouldn't reuse space of an unreclaimable io_unit
659 */
660 while (1) {
Shaohua Lia8c34f92015-09-02 13:49:46 -0700661 struct list_head *target_list = NULL;
662
Shaohua Li0576b1c2015-08-13 14:32:00 -0700663 while (!list_empty(&log->stripe_end_ios)) {
664 io = list_first_entry(&log->stripe_end_ios,
665 struct r5l_io_unit, log_sibling);
666 list_move_tail(&io->log_sibling, &list);
667 free += r5l_ring_distance(log, io->log_start,
668 io->log_end);
669 }
670
671 if (free >= reclaim_target ||
672 (list_empty(&log->running_ios) &&
673 list_empty(&log->io_end_ios) &&
Shaohua Lia8c34f92015-09-02 13:49:46 -0700674 list_empty(&log->flushing_ios) &&
675 list_empty(&log->flushed_ios)))
Shaohua Li0576b1c2015-08-13 14:32:00 -0700676 break;
677
678 /* Below waiting mostly happens when we shutdown the raid */
Shaohua Lia8c34f92015-09-02 13:49:46 -0700679 if (!list_empty(&log->flushed_ios))
680 target_list = &log->flushed_ios;
681 else if (!list_empty(&log->flushing_ios))
682 target_list = &log->flushing_ios;
683 else if (!list_empty(&log->io_end_ios))
684 target_list = &log->io_end_ios;
685 else if (!list_empty(&log->running_ios))
686 target_list = &log->running_ios;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700687
Shaohua Li0fd22b42015-09-02 13:49:47 -0700688 r5l_kick_io_unit(log);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700689 }
690 spin_unlock_irq(&log->io_list_lock);
691
692 if (list_empty(&list))
693 return;
694
695 /* super always point to last valid meta */
696 last = list_last_entry(&list, struct r5l_io_unit, log_sibling);
697 /*
698 * write_super will flush cache of each raid disk. We must write super
699 * here, because the log area might be reused soon and we don't want to
700 * confuse recovery
701 */
702 r5l_write_super(log, last->log_start);
703
704 mutex_lock(&log->io_mutex);
705 log->last_checkpoint = last->log_start;
706 log->last_cp_seq = last->seq;
707 mutex_unlock(&log->io_mutex);
708 r5l_run_no_space_stripes(log);
709
710 while (!list_empty(&list)) {
711 io = list_first_entry(&list, struct r5l_io_unit, log_sibling);
712 list_del(&io->log_sibling);
713 r5l_free_io_unit(log, io);
714 }
715}
716
717static void r5l_reclaim_thread(struct md_thread *thread)
718{
719 struct mddev *mddev = thread->mddev;
720 struct r5conf *conf = mddev->private;
721 struct r5l_log *log = conf->log;
722
723 if (!log)
724 return;
725 r5l_do_reclaim(log);
726}
727
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700728static void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
729{
Shaohua Li0576b1c2015-08-13 14:32:00 -0700730 unsigned long target;
731 unsigned long new = (unsigned long)space; /* overflow in theory */
732
733 do {
734 target = log->reclaim_target;
735 if (new < target)
736 return;
737 } while (cmpxchg(&log->reclaim_target, target, new) != target);
738 md_wakeup_thread(log->reclaim_thread);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700739}
740
Shaohua Li355810d2015-08-13 14:32:01 -0700741struct r5l_recovery_ctx {
742 struct page *meta_page; /* current meta */
743 sector_t meta_total_blocks; /* total size of current meta and data */
744 sector_t pos; /* recovery position */
745 u64 seq; /* recovery position seq */
746};
747
748static int r5l_read_meta_block(struct r5l_log *log,
749 struct r5l_recovery_ctx *ctx)
750{
751 struct page *page = ctx->meta_page;
752 struct r5l_meta_block *mb;
753 u32 crc, stored_crc;
754
755 if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, READ, false))
756 return -EIO;
757
758 mb = page_address(page);
759 stored_crc = le32_to_cpu(mb->checksum);
760 mb->checksum = 0;
761
762 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
763 le64_to_cpu(mb->seq) != ctx->seq ||
764 mb->version != R5LOG_VERSION ||
765 le64_to_cpu(mb->position) != ctx->pos)
766 return -EINVAL;
767
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700768 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -0700769 if (stored_crc != crc)
770 return -EINVAL;
771
772 if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
773 return -EINVAL;
774
775 ctx->meta_total_blocks = BLOCK_SECTORS;
776
777 return 0;
778}
779
780static int r5l_recovery_flush_one_stripe(struct r5l_log *log,
781 struct r5l_recovery_ctx *ctx,
782 sector_t stripe_sect,
783 int *offset, sector_t *log_offset)
784{
785 struct r5conf *conf = log->rdev->mddev->private;
786 struct stripe_head *sh;
787 struct r5l_payload_data_parity *payload;
788 int disk_index;
789
790 sh = raid5_get_active_stripe(conf, stripe_sect, 0, 0, 0);
791 while (1) {
792 payload = page_address(ctx->meta_page) + *offset;
793
794 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) {
795 raid5_compute_sector(conf,
796 le64_to_cpu(payload->location), 0,
797 &disk_index, sh);
798
799 sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
800 sh->dev[disk_index].page, READ, false);
801 sh->dev[disk_index].log_checksum =
802 le32_to_cpu(payload->checksum[0]);
803 set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
804 ctx->meta_total_blocks += BLOCK_SECTORS;
805 } else {
806 disk_index = sh->pd_idx;
807 sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
808 sh->dev[disk_index].page, READ, false);
809 sh->dev[disk_index].log_checksum =
810 le32_to_cpu(payload->checksum[0]);
811 set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
812
813 if (sh->qd_idx >= 0) {
814 disk_index = sh->qd_idx;
815 sync_page_io(log->rdev,
816 r5l_ring_add(log, *log_offset, BLOCK_SECTORS),
817 PAGE_SIZE, sh->dev[disk_index].page,
818 READ, false);
819 sh->dev[disk_index].log_checksum =
820 le32_to_cpu(payload->checksum[1]);
821 set_bit(R5_Wantwrite,
822 &sh->dev[disk_index].flags);
823 }
824 ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
825 }
826
827 *log_offset = r5l_ring_add(log, *log_offset,
828 le32_to_cpu(payload->size));
829 *offset += sizeof(struct r5l_payload_data_parity) +
830 sizeof(__le32) *
831 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
832 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY)
833 break;
834 }
835
836 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
837 void *addr;
838 u32 checksum;
839
840 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
841 continue;
842 addr = kmap_atomic(sh->dev[disk_index].page);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700843 checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -0700844 kunmap_atomic(addr);
845 if (checksum != sh->dev[disk_index].log_checksum)
846 goto error;
847 }
848
849 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
850 struct md_rdev *rdev, *rrdev;
851
852 if (!test_and_clear_bit(R5_Wantwrite,
853 &sh->dev[disk_index].flags))
854 continue;
855
856 /* in case device is broken */
857 rdev = rcu_dereference(conf->disks[disk_index].rdev);
858 if (rdev)
859 sync_page_io(rdev, stripe_sect, PAGE_SIZE,
860 sh->dev[disk_index].page, WRITE, false);
861 rrdev = rcu_dereference(conf->disks[disk_index].replacement);
862 if (rrdev)
863 sync_page_io(rrdev, stripe_sect, PAGE_SIZE,
864 sh->dev[disk_index].page, WRITE, false);
865 }
866 raid5_release_stripe(sh);
867 return 0;
868
869error:
870 for (disk_index = 0; disk_index < sh->disks; disk_index++)
871 sh->dev[disk_index].flags = 0;
872 raid5_release_stripe(sh);
873 return -EINVAL;
874}
875
876static int r5l_recovery_flush_one_meta(struct r5l_log *log,
877 struct r5l_recovery_ctx *ctx)
878{
879 struct r5conf *conf = log->rdev->mddev->private;
880 struct r5l_payload_data_parity *payload;
881 struct r5l_meta_block *mb;
882 int offset;
883 sector_t log_offset;
884 sector_t stripe_sector;
885
886 mb = page_address(ctx->meta_page);
887 offset = sizeof(struct r5l_meta_block);
888 log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
889
890 while (offset < le32_to_cpu(mb->meta_size)) {
891 int dd;
892
893 payload = (void *)mb + offset;
894 stripe_sector = raid5_compute_sector(conf,
895 le64_to_cpu(payload->location), 0, &dd, NULL);
896 if (r5l_recovery_flush_one_stripe(log, ctx, stripe_sector,
897 &offset, &log_offset))
898 return -EINVAL;
899 }
900 return 0;
901}
902
903/* copy data/parity from log to raid disks */
904static void r5l_recovery_flush_log(struct r5l_log *log,
905 struct r5l_recovery_ctx *ctx)
906{
907 while (1) {
908 if (r5l_read_meta_block(log, ctx))
909 return;
910 if (r5l_recovery_flush_one_meta(log, ctx))
911 return;
912 ctx->seq++;
913 ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
914 }
915}
916
917static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
918 u64 seq)
919{
920 struct page *page;
921 struct r5l_meta_block *mb;
922 u32 crc;
923
924 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
925 if (!page)
926 return -ENOMEM;
927 mb = page_address(page);
928 mb->magic = cpu_to_le32(R5LOG_MAGIC);
929 mb->version = R5LOG_VERSION;
930 mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
931 mb->seq = cpu_to_le64(seq);
932 mb->position = cpu_to_le64(pos);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700933 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -0700934 mb->checksum = cpu_to_le32(crc);
935
936 if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, WRITE_FUA, false)) {
937 __free_page(page);
938 return -EIO;
939 }
940 __free_page(page);
941 return 0;
942}
943
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700944static int r5l_recovery_log(struct r5l_log *log)
945{
Shaohua Li355810d2015-08-13 14:32:01 -0700946 struct r5l_recovery_ctx ctx;
947
948 ctx.pos = log->last_checkpoint;
949 ctx.seq = log->last_cp_seq;
950 ctx.meta_page = alloc_page(GFP_KERNEL);
951 if (!ctx.meta_page)
952 return -ENOMEM;
953
954 r5l_recovery_flush_log(log, &ctx);
955 __free_page(ctx.meta_page);
956
957 /*
958 * we did a recovery. Now ctx.pos points to an invalid meta block. New
959 * log will start here. but we can't let superblock point to last valid
960 * meta block. The log might looks like:
961 * | meta 1| meta 2| meta 3|
962 * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
963 * superblock points to meta 1, we write a new valid meta 2n. if crash
964 * happens again, new recovery will start from meta 1. Since meta 2n is
965 * valid now, recovery will think meta 3 is valid, which is wrong.
966 * The solution is we create a new meta in meta2 with its seq == meta
967 * 1's seq + 10 and let superblock points to meta2. The same recovery will
968 * not think meta 3 is a valid meta, because its seq doesn't match
969 */
970 if (ctx.seq > log->last_cp_seq + 1) {
971 int ret;
972
973 ret = r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq + 10);
974 if (ret)
975 return ret;
976 log->seq = ctx.seq + 11;
977 log->log_start = r5l_ring_add(log, ctx.pos, BLOCK_SECTORS);
978 r5l_write_super(log, ctx.pos);
979 } else {
980 log->log_start = ctx.pos;
981 log->seq = ctx.seq;
982 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700983 return 0;
984}
985
986static void r5l_write_super(struct r5l_log *log, sector_t cp)
987{
988 struct mddev *mddev = log->rdev->mddev;
989
990 log->rdev->journal_tail = cp;
991 set_bit(MD_CHANGE_DEVS, &mddev->flags);
992}
993
994static int r5l_load_log(struct r5l_log *log)
995{
996 struct md_rdev *rdev = log->rdev;
997 struct page *page;
998 struct r5l_meta_block *mb;
999 sector_t cp = log->rdev->journal_tail;
1000 u32 stored_crc, expected_crc;
1001 bool create_super = false;
1002 int ret;
1003
1004 /* Make sure it's valid */
1005 if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
1006 cp = 0;
1007 page = alloc_page(GFP_KERNEL);
1008 if (!page)
1009 return -ENOMEM;
1010
1011 if (!sync_page_io(rdev, cp, PAGE_SIZE, page, READ, false)) {
1012 ret = -EIO;
1013 goto ioerr;
1014 }
1015 mb = page_address(page);
1016
1017 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
1018 mb->version != R5LOG_VERSION) {
1019 create_super = true;
1020 goto create;
1021 }
1022 stored_crc = le32_to_cpu(mb->checksum);
1023 mb->checksum = 0;
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001024 expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001025 if (stored_crc != expected_crc) {
1026 create_super = true;
1027 goto create;
1028 }
1029 if (le64_to_cpu(mb->position) != cp) {
1030 create_super = true;
1031 goto create;
1032 }
1033create:
1034 if (create_super) {
1035 log->last_cp_seq = prandom_u32();
1036 cp = 0;
1037 /*
1038 * Make sure super points to correct address. Log might have
1039 * data very soon. If super hasn't correct log tail address,
1040 * recovery can't find the log
1041 */
1042 r5l_write_super(log, cp);
1043 } else
1044 log->last_cp_seq = le64_to_cpu(mb->seq);
1045
1046 log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001047 log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
1048 if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
1049 log->max_free_space = RECLAIM_MAX_FREE_SPACE;
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001050 log->last_checkpoint = cp;
1051
1052 __free_page(page);
1053
1054 return r5l_recovery_log(log);
1055ioerr:
1056 __free_page(page);
1057 return ret;
1058}
1059
1060int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
1061{
1062 struct r5l_log *log;
1063
1064 if (PAGE_SIZE != 4096)
1065 return -EINVAL;
1066 log = kzalloc(sizeof(*log), GFP_KERNEL);
1067 if (!log)
1068 return -ENOMEM;
1069 log->rdev = rdev;
1070
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001071 log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
1072 sizeof(rdev->mddev->uuid));
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001073
1074 mutex_init(&log->io_mutex);
1075
1076 spin_lock_init(&log->io_list_lock);
1077 INIT_LIST_HEAD(&log->running_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001078 INIT_LIST_HEAD(&log->io_end_ios);
1079 INIT_LIST_HEAD(&log->stripe_end_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07001080 INIT_LIST_HEAD(&log->flushing_ios);
1081 INIT_LIST_HEAD(&log->flushed_ios);
1082 bio_init(&log->flush_bio);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001083
1084 log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
1085 if (!log->io_kc)
1086 goto io_kc;
1087
Shaohua Li0576b1c2015-08-13 14:32:00 -07001088 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
1089 log->rdev->mddev, "reclaim");
1090 if (!log->reclaim_thread)
1091 goto reclaim_thread;
Shaohua Li0fd22b42015-09-02 13:49:47 -07001092 init_waitqueue_head(&log->iounit_wait);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001093
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001094 INIT_LIST_HEAD(&log->no_space_stripes);
1095 spin_lock_init(&log->no_space_stripes_lock);
1096
1097 if (r5l_load_log(log))
1098 goto error;
1099
1100 conf->log = log;
1101 return 0;
1102error:
Shaohua Li0576b1c2015-08-13 14:32:00 -07001103 md_unregister_thread(&log->reclaim_thread);
1104reclaim_thread:
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001105 kmem_cache_destroy(log->io_kc);
1106io_kc:
1107 kfree(log);
1108 return -EINVAL;
1109}
1110
1111void r5l_exit_log(struct r5l_log *log)
1112{
Shaohua Li0576b1c2015-08-13 14:32:00 -07001113 /*
1114 * at this point all stripes are finished, so io_unit is at least in
1115 * STRIPE_END state
1116 */
1117 r5l_wake_reclaim(log, -1L);
1118 md_unregister_thread(&log->reclaim_thread);
1119 r5l_do_reclaim(log);
1120 /*
1121 * force a super update, r5l_do_reclaim might updated the super.
1122 * mddev->thread is already stopped
1123 */
1124 md_update_sb(log->rdev->mddev, 1);
1125
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001126 kmem_cache_destroy(log->io_kc);
1127 kfree(log);
1128}