blob: 4e6046f1b0aa17e64e6cc0df37bb763559a5946e [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
Christoph Hellwig17036462015-10-05 09:31:06 +020054 sector_t next_checkpoint;
55 u64 next_cp_seq;
56
Shaohua Lif6bed0e2015-08-13 14:31:59 -070057 struct mutex io_mutex;
58 struct r5l_io_unit *current_io; /* current io_unit accepting new data */
59
60 spinlock_t io_list_lock;
61 struct list_head running_ios; /* io_units which are still running,
62 * and have not yet been completely
63 * written to the log */
64 struct list_head io_end_ios; /* io_units which have been completely
65 * written to the log but not yet written
66 * to the RAID */
Shaohua Lia8c34f92015-09-02 13:49:46 -070067 struct list_head flushing_ios; /* io_units which are waiting for log
68 * cache flush */
Christoph Hellwig04732f72015-10-05 09:31:07 +020069 struct list_head finished_ios; /* io_units which settle down in log disk */
Shaohua Lia8c34f92015-09-02 13:49:46 -070070 struct bio flush_bio;
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;
Christoph Hellwig56fef7c2015-10-05 09:31:09 +020086
87 bool need_cache_flush;
Shaohua Lif6bed0e2015-08-13 14:31:59 -070088};
89
90/*
91 * an IO range starts from a meta data block and end at the next meta data
92 * block. The io unit's the meta data block tracks data/parity followed it. io
93 * unit is written to log disk with normal write, as we always flush log disk
94 * first and then start move data to raid disks, there is no requirement to
95 * write io unit with FLUSH/FUA
96 */
97struct r5l_io_unit {
98 struct r5l_log *log;
99
100 struct page *meta_page; /* store meta block */
101 int meta_offset; /* current offset in meta_page */
102
103 struct bio_list bios;
104 atomic_t pending_io; /* pending bios not written to log yet */
105 struct bio *current_bio;/* current_bio accepting new data */
106
107 atomic_t pending_stripe;/* how many stripes not flushed to raid */
108 u64 seq; /* seq number of the metablock */
109 sector_t log_start; /* where the io_unit starts */
110 sector_t log_end; /* where the io_unit ends */
111 struct list_head log_sibling; /* log->running_ios */
112 struct list_head stripe_list; /* stripes added to the io_unit */
113
114 int state;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700115};
116
117/* r5l_io_unit state */
118enum r5l_io_unit_state {
119 IO_UNIT_RUNNING = 0, /* accepting new IO */
120 IO_UNIT_IO_START = 1, /* io_unit bio start writing to log,
121 * don't accepting new bio */
122 IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */
Shaohua Lia8c34f92015-09-02 13:49:46 -0700123 IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700124};
125
126static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
127{
128 start += inc;
129 if (start >= log->device_size)
130 start = start - log->device_size;
131 return start;
132}
133
134static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
135 sector_t end)
136{
137 if (end >= start)
138 return end - start;
139 else
140 return end + log->device_size - start;
141}
142
143static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
144{
145 sector_t used_size;
146
147 used_size = r5l_ring_distance(log, log->last_checkpoint,
148 log->log_start);
149
150 return log->device_size > used_size + size;
151}
152
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700153static void r5l_free_io_unit(struct r5l_log *log, struct r5l_io_unit *io)
154{
155 __free_page(io->meta_page);
156 kmem_cache_free(log->io_kc, io);
157}
158
159static void r5l_move_io_unit_list(struct list_head *from, struct list_head *to,
160 enum r5l_io_unit_state state)
161{
162 struct r5l_io_unit *io;
163
164 while (!list_empty(from)) {
165 io = list_first_entry(from, struct r5l_io_unit, log_sibling);
166 /* don't change list order */
167 if (io->state >= state)
168 list_move_tail(&io->log_sibling, to);
169 else
170 break;
171 }
172}
173
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700174static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
175 enum r5l_io_unit_state state)
176{
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700177 if (WARN_ON(io->state >= state))
178 return;
179 io->state = state;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700180}
181
Christoph Hellwigd8858f42015-10-05 09:31:08 +0200182static void r5l_io_run_stripes(struct r5l_io_unit *io)
183{
184 struct stripe_head *sh, *next;
185
186 list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
187 list_del_init(&sh->log_list);
188 set_bit(STRIPE_HANDLE, &sh->state);
189 raid5_release_stripe(sh);
190 }
191}
192
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700193/* XXX: totally ignores I/O errors */
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200194static void r5l_log_run_stripes(struct r5l_log *log)
195{
196 struct r5l_io_unit *io, *next;
197
198 assert_spin_locked(&log->io_list_lock);
199
200 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
201 /* don't change list order */
202 if (io->state < IO_UNIT_IO_END)
203 break;
204
205 list_move_tail(&io->log_sibling, &log->finished_ios);
206 r5l_io_run_stripes(io);
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
216 bio_put(bio);
217
218 if (!atomic_dec_and_test(&io->pending_io))
219 return;
220
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700221 spin_lock_irqsave(&log->io_list_lock, flags);
222 __r5l_set_io_unit_state(io, IO_UNIT_IO_END);
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200223 if (log->need_cache_flush)
224 r5l_move_io_unit_list(&log->running_ios, &log->io_end_ios,
225 IO_UNIT_IO_END);
226 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;
238 struct bio *bio;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700239 unsigned long flags;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700240 u32 crc;
241
242 if (!io)
243 return;
244
245 block = page_address(io->meta_page);
246 block->meta_size = cpu_to_le32(io->meta_offset);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700247 crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700248 block->checksum = cpu_to_le32(crc);
249
250 log->current_io = NULL;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700251 spin_lock_irqsave(&log->io_list_lock, flags);
252 __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
253 spin_unlock_irqrestore(&log->io_list_lock, flags);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700254
Christoph Hellwig1e932a32015-10-05 09:31:12 +0200255 while ((bio = bio_list_pop(&io->bios)))
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700256 submit_bio(WRITE, bio);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700257}
258
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200259static struct bio *r5l_bio_alloc(struct r5l_log *log, struct r5l_io_unit *io)
260{
261 struct bio *bio = bio_kmalloc(GFP_NOIO | __GFP_NOFAIL, BIO_MAX_PAGES);
262
263 bio->bi_rw = WRITE;
264 bio->bi_bdev = log->rdev->bdev;
Christoph Hellwig1e932a32015-10-05 09:31:12 +0200265 bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200266 bio->bi_end_io = r5l_log_endio;
267 bio->bi_private = io;
268
269 bio_list_add(&io->bios, bio);
270 atomic_inc(&io->pending_io);
271 return bio;
272}
273
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200274static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
275{
276 log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
277
278 /*
279 * If we filled up the log device start from the beginning again,
280 * which will require a new bio.
281 *
282 * Note: for this to work properly the log size needs to me a multiple
283 * of BLOCK_SECTORS.
284 */
285 if (log->log_start == 0)
286 io->current_bio = NULL;
287
288 io->log_end = log->log_start;
289}
290
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700291static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
292{
293 struct r5l_io_unit *io;
294 struct r5l_meta_block *block;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700295
Christoph Hellwig51039cd2015-10-05 09:31:13 +0200296 /* We can't handle memory allocate failure so far */
297 io = kmem_cache_zalloc(log->io_kc, GFP_NOIO | __GFP_NOFAIL);
298 io->log = log;
299 bio_list_init(&io->bios);
300 INIT_LIST_HEAD(&io->log_sibling);
301 INIT_LIST_HEAD(&io->stripe_list);
302 io->state = IO_UNIT_RUNNING;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700303
Christoph Hellwig51039cd2015-10-05 09:31:13 +0200304 io->meta_page = alloc_page(GFP_NOIO | __GFP_NOFAIL | __GFP_ZERO);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700305 block = page_address(io->meta_page);
306 block->magic = cpu_to_le32(R5LOG_MAGIC);
307 block->version = R5LOG_VERSION;
308 block->seq = cpu_to_le64(log->seq);
309 block->position = cpu_to_le64(log->log_start);
310
311 io->log_start = log->log_start;
312 io->meta_offset = sizeof(struct r5l_meta_block);
Christoph Hellwig2b8ef162015-10-05 09:31:15 +0200313 io->seq = log->seq++;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700314
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200315 io->current_bio = r5l_bio_alloc(log, io);
316 bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700317
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200318 r5_reserve_log_entry(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700319
320 spin_lock_irq(&log->io_list_lock);
321 list_add_tail(&io->log_sibling, &log->running_ios);
322 spin_unlock_irq(&log->io_list_lock);
323
324 return io;
325}
326
327static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
328{
Christoph Hellwig22581f52015-10-05 09:31:10 +0200329 if (log->current_io &&
330 log->current_io->meta_offset + payload_size > PAGE_SIZE)
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700331 r5l_submit_current_io(log);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700332
Christoph Hellwig22581f52015-10-05 09:31:10 +0200333 if (!log->current_io)
334 log->current_io = r5l_new_meta(log);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700335 return 0;
336}
337
338static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
339 sector_t location,
340 u32 checksum1, u32 checksum2,
341 bool checksum2_valid)
342{
343 struct r5l_io_unit *io = log->current_io;
344 struct r5l_payload_data_parity *payload;
345
346 payload = page_address(io->meta_page) + io->meta_offset;
347 payload->header.type = cpu_to_le16(type);
348 payload->header.flags = cpu_to_le16(0);
349 payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
350 (PAGE_SHIFT - 9));
351 payload->location = cpu_to_le64(location);
352 payload->checksum[0] = cpu_to_le32(checksum1);
353 if (checksum2_valid)
354 payload->checksum[1] = cpu_to_le32(checksum2);
355
356 io->meta_offset += sizeof(struct r5l_payload_data_parity) +
357 sizeof(__le32) * (1 + !!checksum2_valid);
358}
359
360static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
361{
362 struct r5l_io_unit *io = log->current_io;
363
364alloc_bio:
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200365 if (!io->current_bio)
366 io->current_bio = r5l_bio_alloc(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700367
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700368 if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0)) {
369 io->current_bio = NULL;
370 goto alloc_bio;
371 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700372
Christoph Hellwigc1b99192015-10-05 09:31:14 +0200373 r5_reserve_log_entry(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700374}
375
376static void r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
377 int data_pages, int parity_pages)
378{
379 int i;
380 int meta_size;
381 struct r5l_io_unit *io;
382
383 meta_size =
384 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
385 * data_pages) +
386 sizeof(struct r5l_payload_data_parity) +
387 sizeof(__le32) * parity_pages;
388
389 r5l_get_meta(log, meta_size);
390 io = log->current_io;
391
392 for (i = 0; i < sh->disks; i++) {
393 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
394 continue;
395 if (i == sh->pd_idx || i == sh->qd_idx)
396 continue;
397 r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
398 raid5_compute_blocknr(sh, i, 0),
399 sh->dev[i].log_checksum, 0, false);
400 r5l_append_payload_page(log, sh->dev[i].page);
401 }
402
403 if (sh->qd_idx >= 0) {
404 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
405 sh->sector, sh->dev[sh->pd_idx].log_checksum,
406 sh->dev[sh->qd_idx].log_checksum, true);
407 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
408 r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
409 } else {
410 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
411 sh->sector, sh->dev[sh->pd_idx].log_checksum,
412 0, false);
413 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
414 }
415
416 list_add_tail(&sh->log_list, &io->stripe_list);
417 atomic_inc(&io->pending_stripe);
418 sh->log_io = io;
419}
420
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700421static void r5l_wake_reclaim(struct r5l_log *log, sector_t space);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700422/*
423 * running in raid5d, where reclaim could wait for raid5d too (when it flushes
424 * data from log to raid disks), so we shouldn't wait for reclaim here
425 */
426int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
427{
428 int write_disks = 0;
429 int data_pages, parity_pages;
430 int meta_size;
431 int reserve;
432 int i;
433
434 if (!log)
435 return -EAGAIN;
436 /* Don't support stripe batch */
437 if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
438 test_bit(STRIPE_SYNCING, &sh->state)) {
439 /* the stripe is written to log, we start writing it to raid */
440 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
441 return -EAGAIN;
442 }
443
444 for (i = 0; i < sh->disks; i++) {
445 void *addr;
446
447 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
448 continue;
449 write_disks++;
450 /* checksum is already calculated in last run */
451 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
452 continue;
453 addr = kmap_atomic(sh->dev[i].page);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700454 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
455 addr, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700456 kunmap_atomic(addr);
457 }
458 parity_pages = 1 + !!(sh->qd_idx >= 0);
459 data_pages = write_disks - parity_pages;
460
461 meta_size =
462 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
463 * data_pages) +
464 sizeof(struct r5l_payload_data_parity) +
465 sizeof(__le32) * parity_pages;
466 /* Doesn't work with very big raid array */
467 if (meta_size + sizeof(struct r5l_meta_block) > PAGE_SIZE)
468 return -EINVAL;
469
470 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
Shaohua Li253f9fd42015-09-04 14:14:16 -0700471 /*
472 * The stripe must enter state machine again to finish the write, so
473 * don't delay.
474 */
475 clear_bit(STRIPE_DELAYED, &sh->state);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700476 atomic_inc(&sh->count);
477
478 mutex_lock(&log->io_mutex);
479 /* meta + data */
480 reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
481 if (r5l_has_free_space(log, reserve))
482 r5l_log_stripe(log, sh, data_pages, parity_pages);
483 else {
484 spin_lock(&log->no_space_stripes_lock);
485 list_add_tail(&sh->log_list, &log->no_space_stripes);
486 spin_unlock(&log->no_space_stripes_lock);
487
488 r5l_wake_reclaim(log, reserve);
489 }
490 mutex_unlock(&log->io_mutex);
491
492 return 0;
493}
494
495void r5l_write_stripe_run(struct r5l_log *log)
496{
497 if (!log)
498 return;
499 mutex_lock(&log->io_mutex);
500 r5l_submit_current_io(log);
501 mutex_unlock(&log->io_mutex);
502}
503
Shaohua Li828cbe92015-09-02 13:49:49 -0700504int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
505{
506 if (!log)
507 return -ENODEV;
508 /*
509 * we flush log disk cache first, then write stripe data to raid disks.
510 * So if bio is finished, the log disk cache is flushed already. The
511 * recovery guarantees we can recovery the bio from log disk, so we
512 * don't need to flush again
513 */
514 if (bio->bi_iter.bi_size == 0) {
515 bio_endio(bio);
516 return 0;
517 }
518 bio->bi_rw &= ~REQ_FLUSH;
519 return -EAGAIN;
520}
521
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700522/* This will run after log space is reclaimed */
523static void r5l_run_no_space_stripes(struct r5l_log *log)
524{
525 struct stripe_head *sh;
526
527 spin_lock(&log->no_space_stripes_lock);
528 while (!list_empty(&log->no_space_stripes)) {
529 sh = list_first_entry(&log->no_space_stripes,
530 struct stripe_head, log_list);
531 list_del_init(&sh->log_list);
532 set_bit(STRIPE_HANDLE, &sh->state);
533 raid5_release_stripe(sh);
534 }
535 spin_unlock(&log->no_space_stripes_lock);
536}
537
Christoph Hellwig17036462015-10-05 09:31:06 +0200538static sector_t r5l_reclaimable_space(struct r5l_log *log)
539{
540 return r5l_ring_distance(log, log->last_checkpoint,
541 log->next_checkpoint);
542}
543
Christoph Hellwig04732f72015-10-05 09:31:07 +0200544static bool r5l_complete_finished_ios(struct r5l_log *log)
Christoph Hellwig17036462015-10-05 09:31:06 +0200545{
546 struct r5l_io_unit *io, *next;
547 bool found = false;
548
549 assert_spin_locked(&log->io_list_lock);
550
Christoph Hellwig04732f72015-10-05 09:31:07 +0200551 list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
Christoph Hellwig17036462015-10-05 09:31:06 +0200552 /* don't change list order */
553 if (io->state < IO_UNIT_STRIPE_END)
554 break;
555
556 log->next_checkpoint = io->log_start;
557 log->next_cp_seq = io->seq;
558
559 list_del(&io->log_sibling);
560 r5l_free_io_unit(log, io);
561
562 found = true;
563 }
564
565 return found;
566}
567
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700568static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
569{
570 struct r5l_log *log = io->log;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700571 unsigned long flags;
572
573 spin_lock_irqsave(&log->io_list_lock, flags);
574 __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
Christoph Hellwig17036462015-10-05 09:31:06 +0200575
Christoph Hellwig04732f72015-10-05 09:31:07 +0200576 if (!r5l_complete_finished_ios(log)) {
Shaohua Li85f2f9a2015-09-04 14:14:05 -0700577 spin_unlock_irqrestore(&log->io_list_lock, flags);
578 return;
579 }
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700580
Christoph Hellwig17036462015-10-05 09:31:06 +0200581 if (r5l_reclaimable_space(log) > log->max_free_space)
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700582 r5l_wake_reclaim(log, 0);
583
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700584 spin_unlock_irqrestore(&log->io_list_lock, flags);
585 wake_up(&log->iounit_wait);
586}
587
Shaohua Li0576b1c2015-08-13 14:32:00 -0700588void r5l_stripe_write_finished(struct stripe_head *sh)
589{
590 struct r5l_io_unit *io;
591
Shaohua Li0576b1c2015-08-13 14:32:00 -0700592 io = sh->log_io;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700593 sh->log_io = NULL;
594
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700595 if (io && atomic_dec_and_test(&io->pending_stripe))
596 __r5l_stripe_write_finished(io);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700597}
598
Shaohua Lia8c34f92015-09-02 13:49:46 -0700599static void r5l_log_flush_endio(struct bio *bio)
600{
601 struct r5l_log *log = container_of(bio, struct r5l_log,
602 flush_bio);
603 unsigned long flags;
604 struct r5l_io_unit *io;
Shaohua Lia8c34f92015-09-02 13:49:46 -0700605
606 spin_lock_irqsave(&log->io_list_lock, flags);
Christoph Hellwigd8858f42015-10-05 09:31:08 +0200607 list_for_each_entry(io, &log->flushing_ios, log_sibling)
608 r5l_io_run_stripes(io);
Christoph Hellwig04732f72015-10-05 09:31:07 +0200609 list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -0700610 spin_unlock_irqrestore(&log->io_list_lock, flags);
611}
612
Shaohua Li0576b1c2015-08-13 14:32:00 -0700613/*
614 * Starting dispatch IO to raid.
615 * io_unit(meta) consists of a log. There is one situation we want to avoid. A
616 * broken meta in the middle of a log causes recovery can't find meta at the
617 * head of log. If operations require meta at the head persistent in log, we
618 * must make sure meta before it persistent in log too. A case is:
619 *
620 * stripe data/parity is in log, we start write stripe to raid disks. stripe
621 * data/parity must be persistent in log before we do the write to raid disks.
622 *
623 * The solution is we restrictly maintain io_unit list order. In this case, we
624 * only write stripes of an io_unit to raid disks till the io_unit is the first
625 * one whose data/parity is in log.
626 */
627void r5l_flush_stripe_to_raid(struct r5l_log *log)
628{
Shaohua Lia8c34f92015-09-02 13:49:46 -0700629 bool do_flush;
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200630
631 if (!log || !log->need_cache_flush)
Shaohua Li0576b1c2015-08-13 14:32:00 -0700632 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700633
Shaohua Lia8c34f92015-09-02 13:49:46 -0700634 spin_lock_irq(&log->io_list_lock);
635 /* flush bio is running */
636 if (!list_empty(&log->flushing_ios)) {
637 spin_unlock_irq(&log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700638 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700639 }
Shaohua Lia8c34f92015-09-02 13:49:46 -0700640 list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
641 do_flush = !list_empty(&log->flushing_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700642 spin_unlock_irq(&log->io_list_lock);
Shaohua Lia8c34f92015-09-02 13:49:46 -0700643
644 if (!do_flush)
645 return;
646 bio_reset(&log->flush_bio);
647 log->flush_bio.bi_bdev = log->rdev->bdev;
648 log->flush_bio.bi_end_io = r5l_log_flush_endio;
649 submit_bio(WRITE_FLUSH, &log->flush_bio);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700650}
651
Shaohua Li0576b1c2015-08-13 14:32:00 -0700652static void r5l_write_super(struct r5l_log *log, sector_t cp);
653static void r5l_do_reclaim(struct r5l_log *log)
654{
Shaohua Li0576b1c2015-08-13 14:32:00 -0700655 sector_t reclaim_target = xchg(&log->reclaim_target, 0);
Christoph Hellwig17036462015-10-05 09:31:06 +0200656 sector_t reclaimable;
657 sector_t next_checkpoint;
658 u64 next_cp_seq;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700659
660 spin_lock_irq(&log->io_list_lock);
661 /*
662 * move proper io_unit to reclaim list. We should not change the order.
663 * reclaimable/unreclaimable io_unit can be mixed in the list, we
664 * shouldn't reuse space of an unreclaimable io_unit
665 */
666 while (1) {
Christoph Hellwig17036462015-10-05 09:31:06 +0200667 reclaimable = r5l_reclaimable_space(log);
668 if (reclaimable >= reclaim_target ||
Shaohua Li0576b1c2015-08-13 14:32:00 -0700669 (list_empty(&log->running_ios) &&
670 list_empty(&log->io_end_ios) &&
Shaohua Lia8c34f92015-09-02 13:49:46 -0700671 list_empty(&log->flushing_ios) &&
Christoph Hellwig04732f72015-10-05 09:31:07 +0200672 list_empty(&log->finished_ios)))
Shaohua Li0576b1c2015-08-13 14:32:00 -0700673 break;
674
Christoph Hellwig17036462015-10-05 09:31:06 +0200675 md_wakeup_thread(log->rdev->mddev->thread);
676 wait_event_lock_irq(log->iounit_wait,
677 r5l_reclaimable_space(log) > reclaimable,
678 log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700679 }
Christoph Hellwig17036462015-10-05 09:31:06 +0200680
681 next_checkpoint = log->next_checkpoint;
682 next_cp_seq = log->next_cp_seq;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700683 spin_unlock_irq(&log->io_list_lock);
684
Christoph Hellwig17036462015-10-05 09:31:06 +0200685 BUG_ON(reclaimable < 0);
686 if (reclaimable == 0)
Shaohua Li0576b1c2015-08-13 14:32:00 -0700687 return;
688
Shaohua Li0576b1c2015-08-13 14:32:00 -0700689 /*
690 * write_super will flush cache of each raid disk. We must write super
691 * here, because the log area might be reused soon and we don't want to
692 * confuse recovery
693 */
Christoph Hellwig17036462015-10-05 09:31:06 +0200694 r5l_write_super(log, next_checkpoint);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700695
696 mutex_lock(&log->io_mutex);
Christoph Hellwig17036462015-10-05 09:31:06 +0200697 log->last_checkpoint = next_checkpoint;
698 log->last_cp_seq = next_cp_seq;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700699 mutex_unlock(&log->io_mutex);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700700
Christoph Hellwig17036462015-10-05 09:31:06 +0200701 r5l_run_no_space_stripes(log);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700702}
703
704static void r5l_reclaim_thread(struct md_thread *thread)
705{
706 struct mddev *mddev = thread->mddev;
707 struct r5conf *conf = mddev->private;
708 struct r5l_log *log = conf->log;
709
710 if (!log)
711 return;
712 r5l_do_reclaim(log);
713}
714
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700715static void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
716{
Shaohua Li0576b1c2015-08-13 14:32:00 -0700717 unsigned long target;
718 unsigned long new = (unsigned long)space; /* overflow in theory */
719
720 do {
721 target = log->reclaim_target;
722 if (new < target)
723 return;
724 } while (cmpxchg(&log->reclaim_target, target, new) != target);
725 md_wakeup_thread(log->reclaim_thread);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700726}
727
Shaohua Lie6c033f2015-10-04 09:20:12 -0700728void r5l_quiesce(struct r5l_log *log, int state)
729{
730 if (!log || state == 2)
731 return;
732 if (state == 0) {
733 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
734 log->rdev->mddev, "reclaim");
735 } else if (state == 1) {
736 /*
737 * at this point all stripes are finished, so io_unit is at
738 * least in STRIPE_END state
739 */
740 r5l_wake_reclaim(log, -1L);
741 md_unregister_thread(&log->reclaim_thread);
742 r5l_do_reclaim(log);
743 }
744}
745
Shaohua Li355810d2015-08-13 14:32:01 -0700746struct r5l_recovery_ctx {
747 struct page *meta_page; /* current meta */
748 sector_t meta_total_blocks; /* total size of current meta and data */
749 sector_t pos; /* recovery position */
750 u64 seq; /* recovery position seq */
751};
752
753static int r5l_read_meta_block(struct r5l_log *log,
754 struct r5l_recovery_ctx *ctx)
755{
756 struct page *page = ctx->meta_page;
757 struct r5l_meta_block *mb;
758 u32 crc, stored_crc;
759
760 if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, READ, false))
761 return -EIO;
762
763 mb = page_address(page);
764 stored_crc = le32_to_cpu(mb->checksum);
765 mb->checksum = 0;
766
767 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
768 le64_to_cpu(mb->seq) != ctx->seq ||
769 mb->version != R5LOG_VERSION ||
770 le64_to_cpu(mb->position) != ctx->pos)
771 return -EINVAL;
772
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700773 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -0700774 if (stored_crc != crc)
775 return -EINVAL;
776
777 if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
778 return -EINVAL;
779
780 ctx->meta_total_blocks = BLOCK_SECTORS;
781
782 return 0;
783}
784
785static int r5l_recovery_flush_one_stripe(struct r5l_log *log,
786 struct r5l_recovery_ctx *ctx,
787 sector_t stripe_sect,
788 int *offset, sector_t *log_offset)
789{
790 struct r5conf *conf = log->rdev->mddev->private;
791 struct stripe_head *sh;
792 struct r5l_payload_data_parity *payload;
793 int disk_index;
794
795 sh = raid5_get_active_stripe(conf, stripe_sect, 0, 0, 0);
796 while (1) {
797 payload = page_address(ctx->meta_page) + *offset;
798
799 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) {
800 raid5_compute_sector(conf,
801 le64_to_cpu(payload->location), 0,
802 &disk_index, sh);
803
804 sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
805 sh->dev[disk_index].page, READ, false);
806 sh->dev[disk_index].log_checksum =
807 le32_to_cpu(payload->checksum[0]);
808 set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
809 ctx->meta_total_blocks += BLOCK_SECTORS;
810 } else {
811 disk_index = sh->pd_idx;
812 sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
813 sh->dev[disk_index].page, READ, false);
814 sh->dev[disk_index].log_checksum =
815 le32_to_cpu(payload->checksum[0]);
816 set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
817
818 if (sh->qd_idx >= 0) {
819 disk_index = sh->qd_idx;
820 sync_page_io(log->rdev,
821 r5l_ring_add(log, *log_offset, BLOCK_SECTORS),
822 PAGE_SIZE, sh->dev[disk_index].page,
823 READ, false);
824 sh->dev[disk_index].log_checksum =
825 le32_to_cpu(payload->checksum[1]);
826 set_bit(R5_Wantwrite,
827 &sh->dev[disk_index].flags);
828 }
829 ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
830 }
831
832 *log_offset = r5l_ring_add(log, *log_offset,
833 le32_to_cpu(payload->size));
834 *offset += sizeof(struct r5l_payload_data_parity) +
835 sizeof(__le32) *
836 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
837 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY)
838 break;
839 }
840
841 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
842 void *addr;
843 u32 checksum;
844
845 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
846 continue;
847 addr = kmap_atomic(sh->dev[disk_index].page);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700848 checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -0700849 kunmap_atomic(addr);
850 if (checksum != sh->dev[disk_index].log_checksum)
851 goto error;
852 }
853
854 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
855 struct md_rdev *rdev, *rrdev;
856
857 if (!test_and_clear_bit(R5_Wantwrite,
858 &sh->dev[disk_index].flags))
859 continue;
860
861 /* in case device is broken */
862 rdev = rcu_dereference(conf->disks[disk_index].rdev);
863 if (rdev)
864 sync_page_io(rdev, stripe_sect, PAGE_SIZE,
865 sh->dev[disk_index].page, WRITE, false);
866 rrdev = rcu_dereference(conf->disks[disk_index].replacement);
867 if (rrdev)
868 sync_page_io(rrdev, stripe_sect, PAGE_SIZE,
869 sh->dev[disk_index].page, WRITE, false);
870 }
871 raid5_release_stripe(sh);
872 return 0;
873
874error:
875 for (disk_index = 0; disk_index < sh->disks; disk_index++)
876 sh->dev[disk_index].flags = 0;
877 raid5_release_stripe(sh);
878 return -EINVAL;
879}
880
881static int r5l_recovery_flush_one_meta(struct r5l_log *log,
882 struct r5l_recovery_ctx *ctx)
883{
884 struct r5conf *conf = log->rdev->mddev->private;
885 struct r5l_payload_data_parity *payload;
886 struct r5l_meta_block *mb;
887 int offset;
888 sector_t log_offset;
889 sector_t stripe_sector;
890
891 mb = page_address(ctx->meta_page);
892 offset = sizeof(struct r5l_meta_block);
893 log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
894
895 while (offset < le32_to_cpu(mb->meta_size)) {
896 int dd;
897
898 payload = (void *)mb + offset;
899 stripe_sector = raid5_compute_sector(conf,
900 le64_to_cpu(payload->location), 0, &dd, NULL);
901 if (r5l_recovery_flush_one_stripe(log, ctx, stripe_sector,
902 &offset, &log_offset))
903 return -EINVAL;
904 }
905 return 0;
906}
907
908/* copy data/parity from log to raid disks */
909static void r5l_recovery_flush_log(struct r5l_log *log,
910 struct r5l_recovery_ctx *ctx)
911{
912 while (1) {
913 if (r5l_read_meta_block(log, ctx))
914 return;
915 if (r5l_recovery_flush_one_meta(log, ctx))
916 return;
917 ctx->seq++;
918 ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
919 }
920}
921
922static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
923 u64 seq)
924{
925 struct page *page;
926 struct r5l_meta_block *mb;
927 u32 crc;
928
929 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
930 if (!page)
931 return -ENOMEM;
932 mb = page_address(page);
933 mb->magic = cpu_to_le32(R5LOG_MAGIC);
934 mb->version = R5LOG_VERSION;
935 mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
936 mb->seq = cpu_to_le64(seq);
937 mb->position = cpu_to_le64(pos);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700938 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -0700939 mb->checksum = cpu_to_le32(crc);
940
941 if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, WRITE_FUA, false)) {
942 __free_page(page);
943 return -EIO;
944 }
945 __free_page(page);
946 return 0;
947}
948
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700949static int r5l_recovery_log(struct r5l_log *log)
950{
Shaohua Li355810d2015-08-13 14:32:01 -0700951 struct r5l_recovery_ctx ctx;
952
953 ctx.pos = log->last_checkpoint;
954 ctx.seq = log->last_cp_seq;
955 ctx.meta_page = alloc_page(GFP_KERNEL);
956 if (!ctx.meta_page)
957 return -ENOMEM;
958
959 r5l_recovery_flush_log(log, &ctx);
960 __free_page(ctx.meta_page);
961
962 /*
963 * we did a recovery. Now ctx.pos points to an invalid meta block. New
964 * log will start here. but we can't let superblock point to last valid
965 * meta block. The log might looks like:
966 * | meta 1| meta 2| meta 3|
967 * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
968 * superblock points to meta 1, we write a new valid meta 2n. if crash
969 * happens again, new recovery will start from meta 1. Since meta 2n is
970 * valid now, recovery will think meta 3 is valid, which is wrong.
971 * The solution is we create a new meta in meta2 with its seq == meta
972 * 1's seq + 10 and let superblock points to meta2. The same recovery will
973 * not think meta 3 is a valid meta, because its seq doesn't match
974 */
975 if (ctx.seq > log->last_cp_seq + 1) {
976 int ret;
977
978 ret = r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq + 10);
979 if (ret)
980 return ret;
981 log->seq = ctx.seq + 11;
982 log->log_start = r5l_ring_add(log, ctx.pos, BLOCK_SECTORS);
983 r5l_write_super(log, ctx.pos);
984 } else {
985 log->log_start = ctx.pos;
986 log->seq = ctx.seq;
987 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700988 return 0;
989}
990
991static void r5l_write_super(struct r5l_log *log, sector_t cp)
992{
993 struct mddev *mddev = log->rdev->mddev;
994
995 log->rdev->journal_tail = cp;
996 set_bit(MD_CHANGE_DEVS, &mddev->flags);
997}
998
999static int r5l_load_log(struct r5l_log *log)
1000{
1001 struct md_rdev *rdev = log->rdev;
1002 struct page *page;
1003 struct r5l_meta_block *mb;
1004 sector_t cp = log->rdev->journal_tail;
1005 u32 stored_crc, expected_crc;
1006 bool create_super = false;
1007 int ret;
1008
1009 /* Make sure it's valid */
1010 if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
1011 cp = 0;
1012 page = alloc_page(GFP_KERNEL);
1013 if (!page)
1014 return -ENOMEM;
1015
1016 if (!sync_page_io(rdev, cp, PAGE_SIZE, page, READ, false)) {
1017 ret = -EIO;
1018 goto ioerr;
1019 }
1020 mb = page_address(page);
1021
1022 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
1023 mb->version != R5LOG_VERSION) {
1024 create_super = true;
1025 goto create;
1026 }
1027 stored_crc = le32_to_cpu(mb->checksum);
1028 mb->checksum = 0;
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001029 expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001030 if (stored_crc != expected_crc) {
1031 create_super = true;
1032 goto create;
1033 }
1034 if (le64_to_cpu(mb->position) != cp) {
1035 create_super = true;
1036 goto create;
1037 }
1038create:
1039 if (create_super) {
1040 log->last_cp_seq = prandom_u32();
1041 cp = 0;
1042 /*
1043 * Make sure super points to correct address. Log might have
1044 * data very soon. If super hasn't correct log tail address,
1045 * recovery can't find the log
1046 */
1047 r5l_write_super(log, cp);
1048 } else
1049 log->last_cp_seq = le64_to_cpu(mb->seq);
1050
1051 log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001052 log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
1053 if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
1054 log->max_free_space = RECLAIM_MAX_FREE_SPACE;
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001055 log->last_checkpoint = cp;
1056
1057 __free_page(page);
1058
1059 return r5l_recovery_log(log);
1060ioerr:
1061 __free_page(page);
1062 return ret;
1063}
1064
1065int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
1066{
1067 struct r5l_log *log;
1068
1069 if (PAGE_SIZE != 4096)
1070 return -EINVAL;
1071 log = kzalloc(sizeof(*log), GFP_KERNEL);
1072 if (!log)
1073 return -ENOMEM;
1074 log->rdev = rdev;
1075
Christoph Hellwig56fef7c2015-10-05 09:31:09 +02001076 log->need_cache_flush = (rdev->bdev->bd_disk->queue->flush_flags != 0);
1077
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001078 log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
1079 sizeof(rdev->mddev->uuid));
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001080
1081 mutex_init(&log->io_mutex);
1082
1083 spin_lock_init(&log->io_list_lock);
1084 INIT_LIST_HEAD(&log->running_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001085 INIT_LIST_HEAD(&log->io_end_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07001086 INIT_LIST_HEAD(&log->flushing_ios);
Christoph Hellwig04732f72015-10-05 09:31:07 +02001087 INIT_LIST_HEAD(&log->finished_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07001088 bio_init(&log->flush_bio);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001089
1090 log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
1091 if (!log->io_kc)
1092 goto io_kc;
1093
Shaohua Li0576b1c2015-08-13 14:32:00 -07001094 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
1095 log->rdev->mddev, "reclaim");
1096 if (!log->reclaim_thread)
1097 goto reclaim_thread;
Shaohua Li0fd22b42015-09-02 13:49:47 -07001098 init_waitqueue_head(&log->iounit_wait);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001099
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001100 INIT_LIST_HEAD(&log->no_space_stripes);
1101 spin_lock_init(&log->no_space_stripes_lock);
1102
1103 if (r5l_load_log(log))
1104 goto error;
1105
1106 conf->log = log;
1107 return 0;
1108error:
Shaohua Li0576b1c2015-08-13 14:32:00 -07001109 md_unregister_thread(&log->reclaim_thread);
1110reclaim_thread:
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001111 kmem_cache_destroy(log->io_kc);
1112io_kc:
1113 kfree(log);
1114 return -EINVAL;
1115}
1116
1117void r5l_exit_log(struct r5l_log *log)
1118{
Shaohua Li0576b1c2015-08-13 14:32:00 -07001119 md_unregister_thread(&log->reclaim_thread);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001120 kmem_cache_destroy(log->io_kc);
1121 kfree(log);
1122}