blob: fa2d6321f3a4b73e065fdc963828b3e25e168154 [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
Christoph Hellwigc38d29b2015-12-21 10:51:02 +110037/*
38 * We only need 2 bios per I/O unit to make progress, but ensure we
39 * have a few more available to not get too tight.
40 */
41#define R5L_POOL_SIZE 4
42
Shaohua Lif6bed0e2015-08-13 14:31:59 -070043struct r5l_log {
44 struct md_rdev *rdev;
45
46 u32 uuid_checksum;
47
48 sector_t device_size; /* log device size, round to
49 * BLOCK_SECTORS */
Shaohua Li0576b1c2015-08-13 14:32:00 -070050 sector_t max_free_space; /* reclaim run if free space is at
51 * this size */
Shaohua Lif6bed0e2015-08-13 14:31:59 -070052
53 sector_t last_checkpoint; /* log tail. where recovery scan
54 * starts from */
55 u64 last_cp_seq; /* log tail sequence */
56
57 sector_t log_start; /* log head. where new data appends */
58 u64 seq; /* log head sequence */
59
Christoph Hellwig17036462015-10-05 09:31:06 +020060 sector_t next_checkpoint;
61 u64 next_cp_seq;
62
Shaohua Lif6bed0e2015-08-13 14:31:59 -070063 struct mutex io_mutex;
64 struct r5l_io_unit *current_io; /* current io_unit accepting new data */
65
66 spinlock_t io_list_lock;
67 struct list_head running_ios; /* io_units which are still running,
68 * and have not yet been completely
69 * written to the log */
70 struct list_head io_end_ios; /* io_units which have been completely
71 * written to the log but not yet written
72 * to the RAID */
Shaohua Lia8c34f92015-09-02 13:49:46 -070073 struct list_head flushing_ios; /* io_units which are waiting for log
74 * cache flush */
Christoph Hellwig04732f72015-10-05 09:31:07 +020075 struct list_head finished_ios; /* io_units which settle down in log disk */
Shaohua Lia8c34f92015-09-02 13:49:46 -070076 struct bio flush_bio;
Shaohua Lif6bed0e2015-08-13 14:31:59 -070077
78 struct kmem_cache *io_kc;
Christoph Hellwigc38d29b2015-12-21 10:51:02 +110079 struct bio_set *bs;
Christoph Hellwige8deb632015-12-21 10:51:02 +110080 mempool_t *meta_pool;
Shaohua Lif6bed0e2015-08-13 14:31:59 -070081
Shaohua Li0576b1c2015-08-13 14:32:00 -070082 struct md_thread *reclaim_thread;
83 unsigned long reclaim_target; /* number of space that need to be
84 * reclaimed. if it's 0, reclaim spaces
85 * used by io_units which are in
86 * IO_UNIT_STRIPE_END state (eg, reclaim
87 * dones't wait for specific io_unit
88 * switching to IO_UNIT_STRIPE_END
89 * state) */
Shaohua Li0fd22b42015-09-02 13:49:47 -070090 wait_queue_head_t iounit_wait;
Shaohua Li0576b1c2015-08-13 14:32:00 -070091
Shaohua Lif6bed0e2015-08-13 14:31:59 -070092 struct list_head no_space_stripes; /* pending stripes, log has no space */
93 spinlock_t no_space_stripes_lock;
Christoph Hellwig56fef7c2015-10-05 09:31:09 +020094
95 bool need_cache_flush;
Shaohua Li4b482042015-10-08 21:54:06 -070096 bool in_teardown;
Shaohua Lif6bed0e2015-08-13 14:31:59 -070097};
98
99/*
100 * an IO range starts from a meta data block and end at the next meta data
101 * block. The io unit's the meta data block tracks data/parity followed it. io
102 * unit is written to log disk with normal write, as we always flush log disk
103 * first and then start move data to raid disks, there is no requirement to
104 * write io unit with FLUSH/FUA
105 */
106struct r5l_io_unit {
107 struct r5l_log *log;
108
109 struct page *meta_page; /* store meta block */
110 int meta_offset; /* current offset in meta_page */
111
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700112 struct bio *current_bio;/* current_bio accepting new data */
113
114 atomic_t pending_stripe;/* how many stripes not flushed to raid */
115 u64 seq; /* seq number of the metablock */
116 sector_t log_start; /* where the io_unit starts */
117 sector_t log_end; /* where the io_unit ends */
118 struct list_head log_sibling; /* log->running_ios */
119 struct list_head stripe_list; /* stripes added to the io_unit */
120
121 int state;
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200122 bool need_split_bio;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700123};
124
125/* r5l_io_unit state */
126enum r5l_io_unit_state {
127 IO_UNIT_RUNNING = 0, /* accepting new IO */
128 IO_UNIT_IO_START = 1, /* io_unit bio start writing to log,
129 * don't accepting new bio */
130 IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */
Shaohua Lia8c34f92015-09-02 13:49:46 -0700131 IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700132};
133
134static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
135{
136 start += inc;
137 if (start >= log->device_size)
138 start = start - log->device_size;
139 return start;
140}
141
142static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
143 sector_t end)
144{
145 if (end >= start)
146 return end - start;
147 else
148 return end + log->device_size - start;
149}
150
151static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
152{
153 sector_t used_size;
154
155 used_size = r5l_ring_distance(log, log->last_checkpoint,
156 log->log_start);
157
158 return log->device_size > used_size + size;
159}
160
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700161static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
162 enum r5l_io_unit_state state)
163{
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700164 if (WARN_ON(io->state >= state))
165 return;
166 io->state = state;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700167}
168
Christoph Hellwigd8858f42015-10-05 09:31:08 +0200169static void r5l_io_run_stripes(struct r5l_io_unit *io)
170{
171 struct stripe_head *sh, *next;
172
173 list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
174 list_del_init(&sh->log_list);
175 set_bit(STRIPE_HANDLE, &sh->state);
176 raid5_release_stripe(sh);
177 }
178}
179
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200180static void r5l_log_run_stripes(struct r5l_log *log)
181{
182 struct r5l_io_unit *io, *next;
183
184 assert_spin_locked(&log->io_list_lock);
185
186 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
187 /* don't change list order */
188 if (io->state < IO_UNIT_IO_END)
189 break;
190
191 list_move_tail(&io->log_sibling, &log->finished_ios);
192 r5l_io_run_stripes(io);
193 }
194}
195
Christoph Hellwig3848c0b2015-12-21 10:51:01 +1100196static void r5l_move_to_end_ios(struct r5l_log *log)
197{
198 struct r5l_io_unit *io, *next;
199
200 assert_spin_locked(&log->io_list_lock);
201
202 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
203 /* don't change list order */
204 if (io->state < IO_UNIT_IO_END)
205 break;
206 list_move_tail(&io->log_sibling, &log->io_end_ios);
207 }
208}
209
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700210static void r5l_log_endio(struct bio *bio)
211{
212 struct r5l_io_unit *io = bio->bi_private;
213 struct r5l_log *log = io->log;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700214 unsigned long flags;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700215
Shaohua Li6e74a9c2015-10-08 21:54:08 -0700216 if (bio->bi_error)
217 md_error(log->rdev->mddev, log->rdev);
218
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700219 bio_put(bio);
Christoph Hellwige8deb632015-12-21 10:51:02 +1100220 mempool_free(io->meta_page, log->meta_pool);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700221
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700222 spin_lock_irqsave(&log->io_list_lock, flags);
223 __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200224 if (log->need_cache_flush)
Christoph Hellwig3848c0b2015-12-21 10:51:01 +1100225 r5l_move_to_end_ios(log);
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200226 else
227 r5l_log_run_stripes(log);
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700228 spin_unlock_irqrestore(&log->io_list_lock, flags);
229
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200230 if (log->need_cache_flush)
231 md_wakeup_thread(log->rdev->mddev->thread);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700232}
233
234static void r5l_submit_current_io(struct r5l_log *log)
235{
236 struct r5l_io_unit *io = log->current_io;
237 struct r5l_meta_block *block;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700238 unsigned long flags;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700239 u32 crc;
240
241 if (!io)
242 return;
243
244 block = page_address(io->meta_page);
245 block->meta_size = cpu_to_le32(io->meta_offset);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700246 crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700247 block->checksum = cpu_to_le32(crc);
248
249 log->current_io = NULL;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700250 spin_lock_irqsave(&log->io_list_lock, flags);
251 __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
252 spin_unlock_irqrestore(&log->io_list_lock, flags);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700253
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200254 submit_bio(WRITE, io->current_bio);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700255}
256
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200257static struct bio *r5l_bio_alloc(struct r5l_log *log)
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200258{
Christoph Hellwigc38d29b2015-12-21 10:51:02 +1100259 struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200260
261 bio->bi_rw = WRITE;
262 bio->bi_bdev = log->rdev->bdev;
Christoph Hellwig1e932a32015-10-05 09:31:12 +0200263 bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200264
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200265 return bio;
266}
267
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200268static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
269{
270 log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
271
272 /*
273 * If we filled up the log device start from the beginning again,
274 * which will require a new bio.
275 *
276 * Note: for this to work properly the log size needs to me a multiple
277 * of BLOCK_SECTORS.
278 */
279 if (log->log_start == 0)
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200280 io->need_split_bio = true;
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200281
282 io->log_end = log->log_start;
283}
284
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700285static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
286{
287 struct r5l_io_unit *io;
288 struct r5l_meta_block *block;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700289
Christoph Hellwig51039cd2015-10-05 09:31:13 +0200290 /* We can't handle memory allocate failure so far */
291 io = kmem_cache_zalloc(log->io_kc, GFP_NOIO | __GFP_NOFAIL);
292 io->log = log;
Christoph Hellwig51039cd2015-10-05 09:31:13 +0200293 INIT_LIST_HEAD(&io->log_sibling);
294 INIT_LIST_HEAD(&io->stripe_list);
295 io->state = IO_UNIT_RUNNING;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700296
Christoph Hellwige8deb632015-12-21 10:51:02 +1100297 io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700298 block = page_address(io->meta_page);
Christoph Hellwige8deb632015-12-21 10:51:02 +1100299 clear_page(block);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700300 block->magic = cpu_to_le32(R5LOG_MAGIC);
301 block->version = R5LOG_VERSION;
302 block->seq = cpu_to_le64(log->seq);
303 block->position = cpu_to_le64(log->log_start);
304
305 io->log_start = log->log_start;
306 io->meta_offset = sizeof(struct r5l_meta_block);
Christoph Hellwig2b8ef162015-10-05 09:31:15 +0200307 io->seq = log->seq++;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700308
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200309 io->current_bio = r5l_bio_alloc(log);
310 io->current_bio->bi_end_io = r5l_log_endio;
311 io->current_bio->bi_private = io;
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200312 bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700313
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200314 r5_reserve_log_entry(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700315
316 spin_lock_irq(&log->io_list_lock);
317 list_add_tail(&io->log_sibling, &log->running_ios);
318 spin_unlock_irq(&log->io_list_lock);
319
320 return io;
321}
322
323static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
324{
Christoph Hellwig22581f52015-10-05 09:31:10 +0200325 if (log->current_io &&
326 log->current_io->meta_offset + payload_size > PAGE_SIZE)
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700327 r5l_submit_current_io(log);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700328
Christoph Hellwig22581f52015-10-05 09:31:10 +0200329 if (!log->current_io)
330 log->current_io = r5l_new_meta(log);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700331 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
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200360 if (io->need_split_bio) {
361 struct bio *prev = io->current_bio;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700362
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200363 io->current_bio = r5l_bio_alloc(log);
364 bio_chain(io->current_bio, prev);
365
366 submit_bio(WRITE, prev);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700367 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700368
Christoph Hellwig6143e2c2015-10-05 09:31:16 +0200369 if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
370 BUG();
371
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200372 r5_reserve_log_entry(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700373}
374
375static void r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
376 int data_pages, int parity_pages)
377{
378 int i;
379 int meta_size;
380 struct r5l_io_unit *io;
381
382 meta_size =
383 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
384 * data_pages) +
385 sizeof(struct r5l_payload_data_parity) +
386 sizeof(__le32) * parity_pages;
387
388 r5l_get_meta(log, meta_size);
389 io = log->current_io;
390
391 for (i = 0; i < sh->disks; i++) {
392 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
393 continue;
394 if (i == sh->pd_idx || i == sh->qd_idx)
395 continue;
396 r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
397 raid5_compute_blocknr(sh, i, 0),
398 sh->dev[i].log_checksum, 0, false);
399 r5l_append_payload_page(log, sh->dev[i].page);
400 }
401
402 if (sh->qd_idx >= 0) {
403 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
404 sh->sector, sh->dev[sh->pd_idx].log_checksum,
405 sh->dev[sh->qd_idx].log_checksum, true);
406 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
407 r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
408 } else {
409 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
410 sh->sector, sh->dev[sh->pd_idx].log_checksum,
411 0, false);
412 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
413 }
414
415 list_add_tail(&sh->log_list, &io->stripe_list);
416 atomic_inc(&io->pending_stripe);
417 sh->log_io = io;
418}
419
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700420static void r5l_wake_reclaim(struct r5l_log *log, sector_t space);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700421/*
422 * running in raid5d, where reclaim could wait for raid5d too (when it flushes
423 * data from log to raid disks), so we shouldn't wait for reclaim here
424 */
425int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
426{
427 int write_disks = 0;
428 int data_pages, parity_pages;
429 int meta_size;
430 int reserve;
431 int i;
432
433 if (!log)
434 return -EAGAIN;
435 /* Don't support stripe batch */
436 if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
437 test_bit(STRIPE_SYNCING, &sh->state)) {
438 /* the stripe is written to log, we start writing it to raid */
439 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
440 return -EAGAIN;
441 }
442
443 for (i = 0; i < sh->disks; i++) {
444 void *addr;
445
446 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
447 continue;
448 write_disks++;
449 /* checksum is already calculated in last run */
450 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
451 continue;
452 addr = kmap_atomic(sh->dev[i].page);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700453 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
454 addr, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700455 kunmap_atomic(addr);
456 }
457 parity_pages = 1 + !!(sh->qd_idx >= 0);
458 data_pages = write_disks - parity_pages;
459
460 meta_size =
461 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
462 * data_pages) +
463 sizeof(struct r5l_payload_data_parity) +
464 sizeof(__le32) * parity_pages;
465 /* Doesn't work with very big raid array */
466 if (meta_size + sizeof(struct r5l_meta_block) > PAGE_SIZE)
467 return -EINVAL;
468
469 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
Shaohua Li253f9fd42015-09-04 14:14:16 -0700470 /*
471 * The stripe must enter state machine again to finish the write, so
472 * don't delay.
473 */
474 clear_bit(STRIPE_DELAYED, &sh->state);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700475 atomic_inc(&sh->count);
476
477 mutex_lock(&log->io_mutex);
478 /* meta + data */
479 reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
480 if (r5l_has_free_space(log, reserve))
481 r5l_log_stripe(log, sh, data_pages, parity_pages);
482 else {
483 spin_lock(&log->no_space_stripes_lock);
484 list_add_tail(&sh->log_list, &log->no_space_stripes);
485 spin_unlock(&log->no_space_stripes_lock);
486
487 r5l_wake_reclaim(log, reserve);
488 }
489 mutex_unlock(&log->io_mutex);
490
491 return 0;
492}
493
494void r5l_write_stripe_run(struct r5l_log *log)
495{
496 if (!log)
497 return;
498 mutex_lock(&log->io_mutex);
499 r5l_submit_current_io(log);
500 mutex_unlock(&log->io_mutex);
501}
502
Shaohua Li828cbe92015-09-02 13:49:49 -0700503int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
504{
505 if (!log)
506 return -ENODEV;
507 /*
508 * we flush log disk cache first, then write stripe data to raid disks.
509 * So if bio is finished, the log disk cache is flushed already. The
510 * recovery guarantees we can recovery the bio from log disk, so we
511 * don't need to flush again
512 */
513 if (bio->bi_iter.bi_size == 0) {
514 bio_endio(bio);
515 return 0;
516 }
517 bio->bi_rw &= ~REQ_FLUSH;
518 return -EAGAIN;
519}
520
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700521/* This will run after log space is reclaimed */
522static void r5l_run_no_space_stripes(struct r5l_log *log)
523{
524 struct stripe_head *sh;
525
526 spin_lock(&log->no_space_stripes_lock);
527 while (!list_empty(&log->no_space_stripes)) {
528 sh = list_first_entry(&log->no_space_stripes,
529 struct stripe_head, log_list);
530 list_del_init(&sh->log_list);
531 set_bit(STRIPE_HANDLE, &sh->state);
532 raid5_release_stripe(sh);
533 }
534 spin_unlock(&log->no_space_stripes_lock);
535}
536
Christoph Hellwig17036462015-10-05 09:31:06 +0200537static sector_t r5l_reclaimable_space(struct r5l_log *log)
538{
539 return r5l_ring_distance(log, log->last_checkpoint,
540 log->next_checkpoint);
541}
542
Christoph Hellwig04732f72015-10-05 09:31:07 +0200543static bool r5l_complete_finished_ios(struct r5l_log *log)
Christoph Hellwig17036462015-10-05 09:31:06 +0200544{
545 struct r5l_io_unit *io, *next;
546 bool found = false;
547
548 assert_spin_locked(&log->io_list_lock);
549
Christoph Hellwig04732f72015-10-05 09:31:07 +0200550 list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
Christoph Hellwig17036462015-10-05 09:31:06 +0200551 /* don't change list order */
552 if (io->state < IO_UNIT_STRIPE_END)
553 break;
554
555 log->next_checkpoint = io->log_start;
556 log->next_cp_seq = io->seq;
557
558 list_del(&io->log_sibling);
Christoph Hellwigad66d442015-12-21 10:51:01 +1100559 kmem_cache_free(log->io_kc, io);
Christoph Hellwig17036462015-10-05 09:31:06 +0200560
561 found = true;
562 }
563
564 return found;
565}
566
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700567static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
568{
569 struct r5l_log *log = io->log;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700570 unsigned long flags;
571
572 spin_lock_irqsave(&log->io_list_lock, flags);
573 __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
Christoph Hellwig17036462015-10-05 09:31:06 +0200574
Christoph Hellwig04732f72015-10-05 09:31:07 +0200575 if (!r5l_complete_finished_ios(log)) {
Shaohua Li85f2f9a2015-09-04 14:14:05 -0700576 spin_unlock_irqrestore(&log->io_list_lock, flags);
577 return;
578 }
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700579
Christoph Hellwig17036462015-10-05 09:31:06 +0200580 if (r5l_reclaimable_space(log) > log->max_free_space)
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700581 r5l_wake_reclaim(log, 0);
582
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700583 spin_unlock_irqrestore(&log->io_list_lock, flags);
584 wake_up(&log->iounit_wait);
585}
586
Shaohua Li0576b1c2015-08-13 14:32:00 -0700587void r5l_stripe_write_finished(struct stripe_head *sh)
588{
589 struct r5l_io_unit *io;
590
Shaohua Li0576b1c2015-08-13 14:32:00 -0700591 io = sh->log_io;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700592 sh->log_io = NULL;
593
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700594 if (io && atomic_dec_and_test(&io->pending_stripe))
595 __r5l_stripe_write_finished(io);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700596}
597
Shaohua Lia8c34f92015-09-02 13:49:46 -0700598static void r5l_log_flush_endio(struct bio *bio)
599{
600 struct r5l_log *log = container_of(bio, struct r5l_log,
601 flush_bio);
602 unsigned long flags;
603 struct r5l_io_unit *io;
Shaohua Lia8c34f92015-09-02 13:49:46 -0700604
Shaohua Li6e74a9c2015-10-08 21:54:08 -0700605 if (bio->bi_error)
606 md_error(log->rdev->mddev, log->rdev);
607
Shaohua Lia8c34f92015-09-02 13:49:46 -0700608 spin_lock_irqsave(&log->io_list_lock, flags);
Christoph Hellwigd8858f42015-10-05 09:31:08 +0200609 list_for_each_entry(io, &log->flushing_ios, log_sibling)
610 r5l_io_run_stripes(io);
Christoph Hellwig04732f72015-10-05 09:31:07 +0200611 list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -0700612 spin_unlock_irqrestore(&log->io_list_lock, flags);
613}
614
Shaohua Li0576b1c2015-08-13 14:32:00 -0700615/*
616 * Starting dispatch IO to raid.
617 * io_unit(meta) consists of a log. There is one situation we want to avoid. A
618 * broken meta in the middle of a log causes recovery can't find meta at the
619 * head of log. If operations require meta at the head persistent in log, we
620 * must make sure meta before it persistent in log too. A case is:
621 *
622 * stripe data/parity is in log, we start write stripe to raid disks. stripe
623 * data/parity must be persistent in log before we do the write to raid disks.
624 *
625 * The solution is we restrictly maintain io_unit list order. In this case, we
626 * only write stripes of an io_unit to raid disks till the io_unit is the first
627 * one whose data/parity is in log.
628 */
629void r5l_flush_stripe_to_raid(struct r5l_log *log)
630{
Shaohua Lia8c34f92015-09-02 13:49:46 -0700631 bool do_flush;
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200632
633 if (!log || !log->need_cache_flush)
Shaohua Li0576b1c2015-08-13 14:32:00 -0700634 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700635
Shaohua Lia8c34f92015-09-02 13:49:46 -0700636 spin_lock_irq(&log->io_list_lock);
637 /* flush bio is running */
638 if (!list_empty(&log->flushing_ios)) {
639 spin_unlock_irq(&log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700640 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700641 }
Shaohua Lia8c34f92015-09-02 13:49:46 -0700642 list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
643 do_flush = !list_empty(&log->flushing_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700644 spin_unlock_irq(&log->io_list_lock);
Shaohua Lia8c34f92015-09-02 13:49:46 -0700645
646 if (!do_flush)
647 return;
648 bio_reset(&log->flush_bio);
649 log->flush_bio.bi_bdev = log->rdev->bdev;
650 log->flush_bio.bi_end_io = r5l_log_flush_endio;
651 submit_bio(WRITE_FLUSH, &log->flush_bio);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700652}
653
Shaohua Li0576b1c2015-08-13 14:32:00 -0700654static void r5l_write_super(struct r5l_log *log, sector_t cp);
Shaohua Li4b482042015-10-08 21:54:06 -0700655static void r5l_write_super_and_discard_space(struct r5l_log *log,
656 sector_t end)
657{
658 struct block_device *bdev = log->rdev->bdev;
659 struct mddev *mddev;
660
661 r5l_write_super(log, end);
662
663 if (!blk_queue_discard(bdev_get_queue(bdev)))
664 return;
665
666 mddev = log->rdev->mddev;
667 /*
668 * This is to avoid a deadlock. r5l_quiesce holds reconfig_mutex and
669 * wait for this thread to finish. This thread waits for
670 * MD_CHANGE_PENDING clear, which is supposed to be done in
671 * md_check_recovery(). md_check_recovery() tries to get
672 * reconfig_mutex. Since r5l_quiesce already holds the mutex,
673 * md_check_recovery() fails, so the PENDING never get cleared. The
674 * in_teardown check workaround this issue.
675 */
676 if (!log->in_teardown) {
677 set_bit(MD_CHANGE_DEVS, &mddev->flags);
678 set_bit(MD_CHANGE_PENDING, &mddev->flags);
679 md_wakeup_thread(mddev->thread);
680 wait_event(mddev->sb_wait,
681 !test_bit(MD_CHANGE_PENDING, &mddev->flags) ||
682 log->in_teardown);
683 /*
684 * r5l_quiesce could run after in_teardown check and hold
685 * mutex first. Superblock might get updated twice.
686 */
687 if (log->in_teardown)
688 md_update_sb(mddev, 1);
689 } else {
690 WARN_ON(!mddev_is_locked(mddev));
691 md_update_sb(mddev, 1);
692 }
693
Shaohua Li6e74a9c2015-10-08 21:54:08 -0700694 /* discard IO error really doesn't matter, ignore it */
Shaohua Li4b482042015-10-08 21:54:06 -0700695 if (log->last_checkpoint < end) {
696 blkdev_issue_discard(bdev,
697 log->last_checkpoint + log->rdev->data_offset,
698 end - log->last_checkpoint, GFP_NOIO, 0);
699 } else {
700 blkdev_issue_discard(bdev,
701 log->last_checkpoint + log->rdev->data_offset,
702 log->device_size - log->last_checkpoint,
703 GFP_NOIO, 0);
704 blkdev_issue_discard(bdev, log->rdev->data_offset, end,
705 GFP_NOIO, 0);
706 }
707}
708
709
Shaohua Li0576b1c2015-08-13 14:32:00 -0700710static void r5l_do_reclaim(struct r5l_log *log)
711{
Shaohua Li0576b1c2015-08-13 14:32:00 -0700712 sector_t reclaim_target = xchg(&log->reclaim_target, 0);
Christoph Hellwig17036462015-10-05 09:31:06 +0200713 sector_t reclaimable;
714 sector_t next_checkpoint;
715 u64 next_cp_seq;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700716
717 spin_lock_irq(&log->io_list_lock);
718 /*
719 * move proper io_unit to reclaim list. We should not change the order.
720 * reclaimable/unreclaimable io_unit can be mixed in the list, we
721 * shouldn't reuse space of an unreclaimable io_unit
722 */
723 while (1) {
Christoph Hellwig17036462015-10-05 09:31:06 +0200724 reclaimable = r5l_reclaimable_space(log);
725 if (reclaimable >= reclaim_target ||
Shaohua Li0576b1c2015-08-13 14:32:00 -0700726 (list_empty(&log->running_ios) &&
727 list_empty(&log->io_end_ios) &&
Shaohua Lia8c34f92015-09-02 13:49:46 -0700728 list_empty(&log->flushing_ios) &&
Christoph Hellwig04732f72015-10-05 09:31:07 +0200729 list_empty(&log->finished_ios)))
Shaohua Li0576b1c2015-08-13 14:32:00 -0700730 break;
731
Christoph Hellwig17036462015-10-05 09:31:06 +0200732 md_wakeup_thread(log->rdev->mddev->thread);
733 wait_event_lock_irq(log->iounit_wait,
734 r5l_reclaimable_space(log) > reclaimable,
735 log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700736 }
Christoph Hellwig17036462015-10-05 09:31:06 +0200737
738 next_checkpoint = log->next_checkpoint;
739 next_cp_seq = log->next_cp_seq;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700740 spin_unlock_irq(&log->io_list_lock);
741
Christoph Hellwig17036462015-10-05 09:31:06 +0200742 BUG_ON(reclaimable < 0);
743 if (reclaimable == 0)
Shaohua Li0576b1c2015-08-13 14:32:00 -0700744 return;
745
Shaohua Li0576b1c2015-08-13 14:32:00 -0700746 /*
747 * write_super will flush cache of each raid disk. We must write super
748 * here, because the log area might be reused soon and we don't want to
749 * confuse recovery
750 */
Shaohua Li4b482042015-10-08 21:54:06 -0700751 r5l_write_super_and_discard_space(log, next_checkpoint);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700752
753 mutex_lock(&log->io_mutex);
Christoph Hellwig17036462015-10-05 09:31:06 +0200754 log->last_checkpoint = next_checkpoint;
755 log->last_cp_seq = next_cp_seq;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700756 mutex_unlock(&log->io_mutex);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700757
Christoph Hellwig17036462015-10-05 09:31:06 +0200758 r5l_run_no_space_stripes(log);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700759}
760
761static void r5l_reclaim_thread(struct md_thread *thread)
762{
763 struct mddev *mddev = thread->mddev;
764 struct r5conf *conf = mddev->private;
765 struct r5l_log *log = conf->log;
766
767 if (!log)
768 return;
769 r5l_do_reclaim(log);
770}
771
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700772static void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
773{
Shaohua Li0576b1c2015-08-13 14:32:00 -0700774 unsigned long target;
775 unsigned long new = (unsigned long)space; /* overflow in theory */
776
777 do {
778 target = log->reclaim_target;
779 if (new < target)
780 return;
781 } while (cmpxchg(&log->reclaim_target, target, new) != target);
782 md_wakeup_thread(log->reclaim_thread);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700783}
784
Shaohua Lie6c033f2015-10-04 09:20:12 -0700785void r5l_quiesce(struct r5l_log *log, int state)
786{
Shaohua Li4b482042015-10-08 21:54:06 -0700787 struct mddev *mddev;
Shaohua Lie6c033f2015-10-04 09:20:12 -0700788 if (!log || state == 2)
789 return;
790 if (state == 0) {
Shaohua Li4b482042015-10-08 21:54:06 -0700791 log->in_teardown = 0;
Shaohua Lie6c033f2015-10-04 09:20:12 -0700792 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
793 log->rdev->mddev, "reclaim");
794 } else if (state == 1) {
795 /*
796 * at this point all stripes are finished, so io_unit is at
797 * least in STRIPE_END state
798 */
Shaohua Li4b482042015-10-08 21:54:06 -0700799 log->in_teardown = 1;
800 /* make sure r5l_write_super_and_discard_space exits */
801 mddev = log->rdev->mddev;
802 wake_up(&mddev->sb_wait);
Shaohua Lie6c033f2015-10-04 09:20:12 -0700803 r5l_wake_reclaim(log, -1L);
804 md_unregister_thread(&log->reclaim_thread);
805 r5l_do_reclaim(log);
806 }
807}
808
Shaohua Li6e74a9c2015-10-08 21:54:08 -0700809bool r5l_log_disk_error(struct r5conf *conf)
810{
Shaohua Lif6b6ec52015-12-21 10:51:02 +1100811 struct r5l_log *log;
812 bool ret;
Shaohua Li7dde2ad2015-10-08 21:54:10 -0700813 /* don't allow write if journal disk is missing */
Shaohua Lif6b6ec52015-12-21 10:51:02 +1100814 rcu_read_lock();
815 log = rcu_dereference(conf->log);
816
817 if (!log)
818 ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
819 else
820 ret = test_bit(Faulty, &log->rdev->flags);
821 rcu_read_unlock();
822 return ret;
Shaohua Li6e74a9c2015-10-08 21:54:08 -0700823}
824
Shaohua Li355810d2015-08-13 14:32:01 -0700825struct r5l_recovery_ctx {
826 struct page *meta_page; /* current meta */
827 sector_t meta_total_blocks; /* total size of current meta and data */
828 sector_t pos; /* recovery position */
829 u64 seq; /* recovery position seq */
830};
831
832static int r5l_read_meta_block(struct r5l_log *log,
833 struct r5l_recovery_ctx *ctx)
834{
835 struct page *page = ctx->meta_page;
836 struct r5l_meta_block *mb;
837 u32 crc, stored_crc;
838
839 if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, READ, false))
840 return -EIO;
841
842 mb = page_address(page);
843 stored_crc = le32_to_cpu(mb->checksum);
844 mb->checksum = 0;
845
846 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
847 le64_to_cpu(mb->seq) != ctx->seq ||
848 mb->version != R5LOG_VERSION ||
849 le64_to_cpu(mb->position) != ctx->pos)
850 return -EINVAL;
851
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700852 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -0700853 if (stored_crc != crc)
854 return -EINVAL;
855
856 if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
857 return -EINVAL;
858
859 ctx->meta_total_blocks = BLOCK_SECTORS;
860
861 return 0;
862}
863
864static int r5l_recovery_flush_one_stripe(struct r5l_log *log,
865 struct r5l_recovery_ctx *ctx,
866 sector_t stripe_sect,
867 int *offset, sector_t *log_offset)
868{
869 struct r5conf *conf = log->rdev->mddev->private;
870 struct stripe_head *sh;
871 struct r5l_payload_data_parity *payload;
872 int disk_index;
873
874 sh = raid5_get_active_stripe(conf, stripe_sect, 0, 0, 0);
875 while (1) {
876 payload = page_address(ctx->meta_page) + *offset;
877
878 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) {
879 raid5_compute_sector(conf,
880 le64_to_cpu(payload->location), 0,
881 &disk_index, sh);
882
883 sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
884 sh->dev[disk_index].page, READ, false);
885 sh->dev[disk_index].log_checksum =
886 le32_to_cpu(payload->checksum[0]);
887 set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
888 ctx->meta_total_blocks += BLOCK_SECTORS;
889 } else {
890 disk_index = sh->pd_idx;
891 sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
892 sh->dev[disk_index].page, READ, false);
893 sh->dev[disk_index].log_checksum =
894 le32_to_cpu(payload->checksum[0]);
895 set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
896
897 if (sh->qd_idx >= 0) {
898 disk_index = sh->qd_idx;
899 sync_page_io(log->rdev,
900 r5l_ring_add(log, *log_offset, BLOCK_SECTORS),
901 PAGE_SIZE, sh->dev[disk_index].page,
902 READ, false);
903 sh->dev[disk_index].log_checksum =
904 le32_to_cpu(payload->checksum[1]);
905 set_bit(R5_Wantwrite,
906 &sh->dev[disk_index].flags);
907 }
908 ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
909 }
910
911 *log_offset = r5l_ring_add(log, *log_offset,
912 le32_to_cpu(payload->size));
913 *offset += sizeof(struct r5l_payload_data_parity) +
914 sizeof(__le32) *
915 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
916 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY)
917 break;
918 }
919
920 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
921 void *addr;
922 u32 checksum;
923
924 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
925 continue;
926 addr = kmap_atomic(sh->dev[disk_index].page);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700927 checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -0700928 kunmap_atomic(addr);
929 if (checksum != sh->dev[disk_index].log_checksum)
930 goto error;
931 }
932
933 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
934 struct md_rdev *rdev, *rrdev;
935
936 if (!test_and_clear_bit(R5_Wantwrite,
937 &sh->dev[disk_index].flags))
938 continue;
939
940 /* in case device is broken */
941 rdev = rcu_dereference(conf->disks[disk_index].rdev);
942 if (rdev)
943 sync_page_io(rdev, stripe_sect, PAGE_SIZE,
944 sh->dev[disk_index].page, WRITE, false);
945 rrdev = rcu_dereference(conf->disks[disk_index].replacement);
946 if (rrdev)
947 sync_page_io(rrdev, stripe_sect, PAGE_SIZE,
948 sh->dev[disk_index].page, WRITE, false);
949 }
950 raid5_release_stripe(sh);
951 return 0;
952
953error:
954 for (disk_index = 0; disk_index < sh->disks; disk_index++)
955 sh->dev[disk_index].flags = 0;
956 raid5_release_stripe(sh);
957 return -EINVAL;
958}
959
960static int r5l_recovery_flush_one_meta(struct r5l_log *log,
961 struct r5l_recovery_ctx *ctx)
962{
963 struct r5conf *conf = log->rdev->mddev->private;
964 struct r5l_payload_data_parity *payload;
965 struct r5l_meta_block *mb;
966 int offset;
967 sector_t log_offset;
968 sector_t stripe_sector;
969
970 mb = page_address(ctx->meta_page);
971 offset = sizeof(struct r5l_meta_block);
972 log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
973
974 while (offset < le32_to_cpu(mb->meta_size)) {
975 int dd;
976
977 payload = (void *)mb + offset;
978 stripe_sector = raid5_compute_sector(conf,
979 le64_to_cpu(payload->location), 0, &dd, NULL);
980 if (r5l_recovery_flush_one_stripe(log, ctx, stripe_sector,
981 &offset, &log_offset))
982 return -EINVAL;
983 }
984 return 0;
985}
986
987/* copy data/parity from log to raid disks */
988static void r5l_recovery_flush_log(struct r5l_log *log,
989 struct r5l_recovery_ctx *ctx)
990{
991 while (1) {
992 if (r5l_read_meta_block(log, ctx))
993 return;
994 if (r5l_recovery_flush_one_meta(log, ctx))
995 return;
996 ctx->seq++;
997 ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
998 }
999}
1000
1001static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
1002 u64 seq)
1003{
1004 struct page *page;
1005 struct r5l_meta_block *mb;
1006 u32 crc;
1007
1008 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1009 if (!page)
1010 return -ENOMEM;
1011 mb = page_address(page);
1012 mb->magic = cpu_to_le32(R5LOG_MAGIC);
1013 mb->version = R5LOG_VERSION;
1014 mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
1015 mb->seq = cpu_to_le64(seq);
1016 mb->position = cpu_to_le64(pos);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001017 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -07001018 mb->checksum = cpu_to_le32(crc);
1019
1020 if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, WRITE_FUA, false)) {
1021 __free_page(page);
1022 return -EIO;
1023 }
1024 __free_page(page);
1025 return 0;
1026}
1027
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001028static int r5l_recovery_log(struct r5l_log *log)
1029{
Shaohua Li355810d2015-08-13 14:32:01 -07001030 struct r5l_recovery_ctx ctx;
1031
1032 ctx.pos = log->last_checkpoint;
1033 ctx.seq = log->last_cp_seq;
1034 ctx.meta_page = alloc_page(GFP_KERNEL);
1035 if (!ctx.meta_page)
1036 return -ENOMEM;
1037
1038 r5l_recovery_flush_log(log, &ctx);
1039 __free_page(ctx.meta_page);
1040
1041 /*
1042 * we did a recovery. Now ctx.pos points to an invalid meta block. New
1043 * log will start here. but we can't let superblock point to last valid
1044 * meta block. The log might looks like:
1045 * | meta 1| meta 2| meta 3|
1046 * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
1047 * superblock points to meta 1, we write a new valid meta 2n. if crash
1048 * happens again, new recovery will start from meta 1. Since meta 2n is
1049 * valid now, recovery will think meta 3 is valid, which is wrong.
1050 * The solution is we create a new meta in meta2 with its seq == meta
1051 * 1's seq + 10 and let superblock points to meta2. The same recovery will
1052 * not think meta 3 is a valid meta, because its seq doesn't match
1053 */
1054 if (ctx.seq > log->last_cp_seq + 1) {
1055 int ret;
1056
1057 ret = r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq + 10);
1058 if (ret)
1059 return ret;
1060 log->seq = ctx.seq + 11;
1061 log->log_start = r5l_ring_add(log, ctx.pos, BLOCK_SECTORS);
1062 r5l_write_super(log, ctx.pos);
1063 } else {
1064 log->log_start = ctx.pos;
1065 log->seq = ctx.seq;
1066 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001067 return 0;
1068}
1069
1070static void r5l_write_super(struct r5l_log *log, sector_t cp)
1071{
1072 struct mddev *mddev = log->rdev->mddev;
1073
1074 log->rdev->journal_tail = cp;
1075 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1076}
1077
1078static int r5l_load_log(struct r5l_log *log)
1079{
1080 struct md_rdev *rdev = log->rdev;
1081 struct page *page;
1082 struct r5l_meta_block *mb;
1083 sector_t cp = log->rdev->journal_tail;
1084 u32 stored_crc, expected_crc;
1085 bool create_super = false;
1086 int ret;
1087
1088 /* Make sure it's valid */
1089 if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
1090 cp = 0;
1091 page = alloc_page(GFP_KERNEL);
1092 if (!page)
1093 return -ENOMEM;
1094
1095 if (!sync_page_io(rdev, cp, PAGE_SIZE, page, READ, false)) {
1096 ret = -EIO;
1097 goto ioerr;
1098 }
1099 mb = page_address(page);
1100
1101 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
1102 mb->version != R5LOG_VERSION) {
1103 create_super = true;
1104 goto create;
1105 }
1106 stored_crc = le32_to_cpu(mb->checksum);
1107 mb->checksum = 0;
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001108 expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001109 if (stored_crc != expected_crc) {
1110 create_super = true;
1111 goto create;
1112 }
1113 if (le64_to_cpu(mb->position) != cp) {
1114 create_super = true;
1115 goto create;
1116 }
1117create:
1118 if (create_super) {
1119 log->last_cp_seq = prandom_u32();
1120 cp = 0;
1121 /*
1122 * Make sure super points to correct address. Log might have
1123 * data very soon. If super hasn't correct log tail address,
1124 * recovery can't find the log
1125 */
1126 r5l_write_super(log, cp);
1127 } else
1128 log->last_cp_seq = le64_to_cpu(mb->seq);
1129
1130 log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001131 log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
1132 if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
1133 log->max_free_space = RECLAIM_MAX_FREE_SPACE;
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001134 log->last_checkpoint = cp;
1135
1136 __free_page(page);
1137
1138 return r5l_recovery_log(log);
1139ioerr:
1140 __free_page(page);
1141 return ret;
1142}
1143
1144int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
1145{
1146 struct r5l_log *log;
1147
1148 if (PAGE_SIZE != 4096)
1149 return -EINVAL;
1150 log = kzalloc(sizeof(*log), GFP_KERNEL);
1151 if (!log)
1152 return -ENOMEM;
1153 log->rdev = rdev;
1154
Christoph Hellwig56fef7c2015-10-05 09:31:09 +02001155 log->need_cache_flush = (rdev->bdev->bd_disk->queue->flush_flags != 0);
1156
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001157 log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
1158 sizeof(rdev->mddev->uuid));
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001159
1160 mutex_init(&log->io_mutex);
1161
1162 spin_lock_init(&log->io_list_lock);
1163 INIT_LIST_HEAD(&log->running_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001164 INIT_LIST_HEAD(&log->io_end_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07001165 INIT_LIST_HEAD(&log->flushing_ios);
Christoph Hellwig04732f72015-10-05 09:31:07 +02001166 INIT_LIST_HEAD(&log->finished_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07001167 bio_init(&log->flush_bio);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001168
1169 log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
1170 if (!log->io_kc)
1171 goto io_kc;
1172
Christoph Hellwigc38d29b2015-12-21 10:51:02 +11001173 log->bs = bioset_create(R5L_POOL_SIZE, 0);
1174 if (!log->bs)
1175 goto io_bs;
1176
Christoph Hellwige8deb632015-12-21 10:51:02 +11001177 log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
1178 if (!log->meta_pool)
1179 goto out_mempool;
1180
Shaohua Li0576b1c2015-08-13 14:32:00 -07001181 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
1182 log->rdev->mddev, "reclaim");
1183 if (!log->reclaim_thread)
1184 goto reclaim_thread;
Shaohua Li0fd22b42015-09-02 13:49:47 -07001185 init_waitqueue_head(&log->iounit_wait);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001186
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001187 INIT_LIST_HEAD(&log->no_space_stripes);
1188 spin_lock_init(&log->no_space_stripes_lock);
1189
1190 if (r5l_load_log(log))
1191 goto error;
1192
Shaohua Lif6b6ec52015-12-21 10:51:02 +11001193 rcu_assign_pointer(conf->log, log);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001194 return 0;
Christoph Hellwige8deb632015-12-21 10:51:02 +11001195
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001196error:
Shaohua Li0576b1c2015-08-13 14:32:00 -07001197 md_unregister_thread(&log->reclaim_thread);
1198reclaim_thread:
Christoph Hellwige8deb632015-12-21 10:51:02 +11001199 mempool_destroy(log->meta_pool);
1200out_mempool:
Christoph Hellwigc38d29b2015-12-21 10:51:02 +11001201 bioset_free(log->bs);
1202io_bs:
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001203 kmem_cache_destroy(log->io_kc);
1204io_kc:
1205 kfree(log);
1206 return -EINVAL;
1207}
1208
1209void r5l_exit_log(struct r5l_log *log)
1210{
Shaohua Li0576b1c2015-08-13 14:32:00 -07001211 md_unregister_thread(&log->reclaim_thread);
Christoph Hellwige8deb632015-12-21 10:51:02 +11001212 mempool_destroy(log->meta_pool);
Christoph Hellwigc38d29b2015-12-21 10:51:02 +11001213 bioset_free(log->bs);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001214 kmem_cache_destroy(log->io_kc);
1215 kfree(log);
1216}