blob: 2ac93fbc31eb35627c83ba991fe31dbf5bfa4537 [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
153static struct r5l_io_unit *r5l_alloc_io_unit(struct r5l_log *log)
154{
155 struct r5l_io_unit *io;
156 /* We can't handle memory allocate failure so far */
157 gfp_t gfp = GFP_NOIO | __GFP_NOFAIL;
158
159 io = kmem_cache_zalloc(log->io_kc, gfp);
160 io->log = log;
161 io->meta_page = alloc_page(gfp | __GFP_ZERO);
162
163 bio_list_init(&io->bios);
164 INIT_LIST_HEAD(&io->log_sibling);
165 INIT_LIST_HEAD(&io->stripe_list);
166 io->state = IO_UNIT_RUNNING;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700167 return io;
168}
169
170static void r5l_free_io_unit(struct r5l_log *log, struct r5l_io_unit *io)
171{
172 __free_page(io->meta_page);
173 kmem_cache_free(log->io_kc, io);
174}
175
176static void r5l_move_io_unit_list(struct list_head *from, struct list_head *to,
177 enum r5l_io_unit_state state)
178{
179 struct r5l_io_unit *io;
180
181 while (!list_empty(from)) {
182 io = list_first_entry(from, struct r5l_io_unit, log_sibling);
183 /* don't change list order */
184 if (io->state >= state)
185 list_move_tail(&io->log_sibling, to);
186 else
187 break;
188 }
189}
190
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700191static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
192 enum r5l_io_unit_state state)
193{
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700194 if (WARN_ON(io->state >= state))
195 return;
196 io->state = state;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700197}
198
Christoph Hellwigd8858f42015-10-05 09:31:08 +0200199static void r5l_io_run_stripes(struct r5l_io_unit *io)
200{
201 struct stripe_head *sh, *next;
202
203 list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
204 list_del_init(&sh->log_list);
205 set_bit(STRIPE_HANDLE, &sh->state);
206 raid5_release_stripe(sh);
207 }
208}
209
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700210/* XXX: totally ignores I/O errors */
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200211static void r5l_log_run_stripes(struct r5l_log *log)
212{
213 struct r5l_io_unit *io, *next;
214
215 assert_spin_locked(&log->io_list_lock);
216
217 list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
218 /* don't change list order */
219 if (io->state < IO_UNIT_IO_END)
220 break;
221
222 list_move_tail(&io->log_sibling, &log->finished_ios);
223 r5l_io_run_stripes(io);
224 }
225}
226
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700227static 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);
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200240 if (log->need_cache_flush)
241 r5l_move_io_unit_list(&log->running_ios, &log->io_end_ios,
242 IO_UNIT_IO_END);
243 else
244 r5l_log_run_stripes(log);
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700245 spin_unlock_irqrestore(&log->io_list_lock, flags);
246
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200247 if (log->need_cache_flush)
248 md_wakeup_thread(log->rdev->mddev->thread);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700249}
250
251static void r5l_submit_current_io(struct r5l_log *log)
252{
253 struct r5l_io_unit *io = log->current_io;
254 struct r5l_meta_block *block;
255 struct bio *bio;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700256 unsigned long flags;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700257 u32 crc;
258
259 if (!io)
260 return;
261
262 block = page_address(io->meta_page);
263 block->meta_size = cpu_to_le32(io->meta_offset);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700264 crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700265 block->checksum = cpu_to_le32(crc);
266
267 log->current_io = NULL;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700268 spin_lock_irqsave(&log->io_list_lock, flags);
269 __r5l_set_io_unit_state(io, IO_UNIT_IO_START);
270 spin_unlock_irqrestore(&log->io_list_lock, flags);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700271
Christoph Hellwig1e932a32015-10-05 09:31:12 +0200272 while ((bio = bio_list_pop(&io->bios)))
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700273 submit_bio(WRITE, bio);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700274}
275
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200276static struct bio *r5l_bio_alloc(struct r5l_log *log, struct r5l_io_unit *io)
277{
278 struct bio *bio = bio_kmalloc(GFP_NOIO | __GFP_NOFAIL, BIO_MAX_PAGES);
279
280 bio->bi_rw = WRITE;
281 bio->bi_bdev = log->rdev->bdev;
Christoph Hellwig1e932a32015-10-05 09:31:12 +0200282 bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200283 bio->bi_end_io = r5l_log_endio;
284 bio->bi_private = io;
285
286 bio_list_add(&io->bios, bio);
287 atomic_inc(&io->pending_io);
288 return bio;
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
296 io = r5l_alloc_io_unit(log);
297
298 block = page_address(io->meta_page);
299 block->magic = cpu_to_le32(R5LOG_MAGIC);
300 block->version = R5LOG_VERSION;
301 block->seq = cpu_to_le64(log->seq);
302 block->position = cpu_to_le64(log->log_start);
303
304 io->log_start = log->log_start;
305 io->meta_offset = sizeof(struct r5l_meta_block);
306 io->seq = log->seq;
307
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200308 io->current_bio = r5l_bio_alloc(log, io);
309 bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700310
311 log->seq++;
312 log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
313 io->log_end = log->log_start;
314 /* current bio hit disk end */
315 if (log->log_start == 0)
316 io->current_bio = NULL;
317
318 spin_lock_irq(&log->io_list_lock);
319 list_add_tail(&io->log_sibling, &log->running_ios);
320 spin_unlock_irq(&log->io_list_lock);
321
322 return io;
323}
324
325static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
326{
Christoph Hellwig22581f52015-10-05 09:31:10 +0200327 if (log->current_io &&
328 log->current_io->meta_offset + payload_size > PAGE_SIZE)
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700329 r5l_submit_current_io(log);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700330
Christoph Hellwig22581f52015-10-05 09:31:10 +0200331 if (!log->current_io)
332 log->current_io = r5l_new_meta(log);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700333 return 0;
334}
335
336static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
337 sector_t location,
338 u32 checksum1, u32 checksum2,
339 bool checksum2_valid)
340{
341 struct r5l_io_unit *io = log->current_io;
342 struct r5l_payload_data_parity *payload;
343
344 payload = page_address(io->meta_page) + io->meta_offset;
345 payload->header.type = cpu_to_le16(type);
346 payload->header.flags = cpu_to_le16(0);
347 payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
348 (PAGE_SHIFT - 9));
349 payload->location = cpu_to_le64(location);
350 payload->checksum[0] = cpu_to_le32(checksum1);
351 if (checksum2_valid)
352 payload->checksum[1] = cpu_to_le32(checksum2);
353
354 io->meta_offset += sizeof(struct r5l_payload_data_parity) +
355 sizeof(__le32) * (1 + !!checksum2_valid);
356}
357
358static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
359{
360 struct r5l_io_unit *io = log->current_io;
361
362alloc_bio:
Christoph Hellwigb349feb2015-10-05 09:31:11 +0200363 if (!io->current_bio)
364 io->current_bio = r5l_bio_alloc(log, io);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700365
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700366 if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0)) {
367 io->current_bio = NULL;
368 goto alloc_bio;
369 }
370 log->log_start = r5l_ring_add(log, log->log_start,
371 BLOCK_SECTORS);
372 /* current bio hit disk end */
373 if (log->log_start == 0)
374 io->current_bio = NULL;
375
376 io->log_end = log->log_start;
377}
378
379static void r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
380 int data_pages, int parity_pages)
381{
382 int i;
383 int meta_size;
384 struct r5l_io_unit *io;
385
386 meta_size =
387 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
388 * data_pages) +
389 sizeof(struct r5l_payload_data_parity) +
390 sizeof(__le32) * parity_pages;
391
392 r5l_get_meta(log, meta_size);
393 io = log->current_io;
394
395 for (i = 0; i < sh->disks; i++) {
396 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
397 continue;
398 if (i == sh->pd_idx || i == sh->qd_idx)
399 continue;
400 r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
401 raid5_compute_blocknr(sh, i, 0),
402 sh->dev[i].log_checksum, 0, false);
403 r5l_append_payload_page(log, sh->dev[i].page);
404 }
405
406 if (sh->qd_idx >= 0) {
407 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
408 sh->sector, sh->dev[sh->pd_idx].log_checksum,
409 sh->dev[sh->qd_idx].log_checksum, true);
410 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
411 r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
412 } else {
413 r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
414 sh->sector, sh->dev[sh->pd_idx].log_checksum,
415 0, false);
416 r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
417 }
418
419 list_add_tail(&sh->log_list, &io->stripe_list);
420 atomic_inc(&io->pending_stripe);
421 sh->log_io = io;
422}
423
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700424static void r5l_wake_reclaim(struct r5l_log *log, sector_t space);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700425/*
426 * running in raid5d, where reclaim could wait for raid5d too (when it flushes
427 * data from log to raid disks), so we shouldn't wait for reclaim here
428 */
429int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
430{
431 int write_disks = 0;
432 int data_pages, parity_pages;
433 int meta_size;
434 int reserve;
435 int i;
436
437 if (!log)
438 return -EAGAIN;
439 /* Don't support stripe batch */
440 if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
441 test_bit(STRIPE_SYNCING, &sh->state)) {
442 /* the stripe is written to log, we start writing it to raid */
443 clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
444 return -EAGAIN;
445 }
446
447 for (i = 0; i < sh->disks; i++) {
448 void *addr;
449
450 if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
451 continue;
452 write_disks++;
453 /* checksum is already calculated in last run */
454 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
455 continue;
456 addr = kmap_atomic(sh->dev[i].page);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700457 sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
458 addr, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700459 kunmap_atomic(addr);
460 }
461 parity_pages = 1 + !!(sh->qd_idx >= 0);
462 data_pages = write_disks - parity_pages;
463
464 meta_size =
465 ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
466 * data_pages) +
467 sizeof(struct r5l_payload_data_parity) +
468 sizeof(__le32) * parity_pages;
469 /* Doesn't work with very big raid array */
470 if (meta_size + sizeof(struct r5l_meta_block) > PAGE_SIZE)
471 return -EINVAL;
472
473 set_bit(STRIPE_LOG_TRAPPED, &sh->state);
Shaohua Li253f9fd42015-09-04 14:14:16 -0700474 /*
475 * The stripe must enter state machine again to finish the write, so
476 * don't delay.
477 */
478 clear_bit(STRIPE_DELAYED, &sh->state);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700479 atomic_inc(&sh->count);
480
481 mutex_lock(&log->io_mutex);
482 /* meta + data */
483 reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
484 if (r5l_has_free_space(log, reserve))
485 r5l_log_stripe(log, sh, data_pages, parity_pages);
486 else {
487 spin_lock(&log->no_space_stripes_lock);
488 list_add_tail(&sh->log_list, &log->no_space_stripes);
489 spin_unlock(&log->no_space_stripes_lock);
490
491 r5l_wake_reclaim(log, reserve);
492 }
493 mutex_unlock(&log->io_mutex);
494
495 return 0;
496}
497
498void r5l_write_stripe_run(struct r5l_log *log)
499{
500 if (!log)
501 return;
502 mutex_lock(&log->io_mutex);
503 r5l_submit_current_io(log);
504 mutex_unlock(&log->io_mutex);
505}
506
Shaohua Li828cbe92015-09-02 13:49:49 -0700507int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
508{
509 if (!log)
510 return -ENODEV;
511 /*
512 * we flush log disk cache first, then write stripe data to raid disks.
513 * So if bio is finished, the log disk cache is flushed already. The
514 * recovery guarantees we can recovery the bio from log disk, so we
515 * don't need to flush again
516 */
517 if (bio->bi_iter.bi_size == 0) {
518 bio_endio(bio);
519 return 0;
520 }
521 bio->bi_rw &= ~REQ_FLUSH;
522 return -EAGAIN;
523}
524
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700525/* This will run after log space is reclaimed */
526static void r5l_run_no_space_stripes(struct r5l_log *log)
527{
528 struct stripe_head *sh;
529
530 spin_lock(&log->no_space_stripes_lock);
531 while (!list_empty(&log->no_space_stripes)) {
532 sh = list_first_entry(&log->no_space_stripes,
533 struct stripe_head, log_list);
534 list_del_init(&sh->log_list);
535 set_bit(STRIPE_HANDLE, &sh->state);
536 raid5_release_stripe(sh);
537 }
538 spin_unlock(&log->no_space_stripes_lock);
539}
540
Christoph Hellwig17036462015-10-05 09:31:06 +0200541static sector_t r5l_reclaimable_space(struct r5l_log *log)
542{
543 return r5l_ring_distance(log, log->last_checkpoint,
544 log->next_checkpoint);
545}
546
Christoph Hellwig04732f72015-10-05 09:31:07 +0200547static bool r5l_complete_finished_ios(struct r5l_log *log)
Christoph Hellwig17036462015-10-05 09:31:06 +0200548{
549 struct r5l_io_unit *io, *next;
550 bool found = false;
551
552 assert_spin_locked(&log->io_list_lock);
553
Christoph Hellwig04732f72015-10-05 09:31:07 +0200554 list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
Christoph Hellwig17036462015-10-05 09:31:06 +0200555 /* don't change list order */
556 if (io->state < IO_UNIT_STRIPE_END)
557 break;
558
559 log->next_checkpoint = io->log_start;
560 log->next_cp_seq = io->seq;
561
562 list_del(&io->log_sibling);
563 r5l_free_io_unit(log, io);
564
565 found = true;
566 }
567
568 return found;
569}
570
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700571static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
572{
573 struct r5l_log *log = io->log;
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700574 unsigned long flags;
575
576 spin_lock_irqsave(&log->io_list_lock, flags);
577 __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
Christoph Hellwig17036462015-10-05 09:31:06 +0200578
Christoph Hellwig04732f72015-10-05 09:31:07 +0200579 if (!r5l_complete_finished_ios(log)) {
Shaohua Li85f2f9a2015-09-04 14:14:05 -0700580 spin_unlock_irqrestore(&log->io_list_lock, flags);
581 return;
582 }
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700583
Christoph Hellwig17036462015-10-05 09:31:06 +0200584 if (r5l_reclaimable_space(log) > log->max_free_space)
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700585 r5l_wake_reclaim(log, 0);
586
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700587 spin_unlock_irqrestore(&log->io_list_lock, flags);
588 wake_up(&log->iounit_wait);
589}
590
Shaohua Li0576b1c2015-08-13 14:32:00 -0700591void r5l_stripe_write_finished(struct stripe_head *sh)
592{
593 struct r5l_io_unit *io;
594
Shaohua Li0576b1c2015-08-13 14:32:00 -0700595 io = sh->log_io;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700596 sh->log_io = NULL;
597
Christoph Hellwig509ffec2015-09-02 13:49:48 -0700598 if (io && atomic_dec_and_test(&io->pending_stripe))
599 __r5l_stripe_write_finished(io);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700600}
601
Shaohua Lia8c34f92015-09-02 13:49:46 -0700602static void r5l_log_flush_endio(struct bio *bio)
603{
604 struct r5l_log *log = container_of(bio, struct r5l_log,
605 flush_bio);
606 unsigned long flags;
607 struct r5l_io_unit *io;
Shaohua Lia8c34f92015-09-02 13:49:46 -0700608
609 spin_lock_irqsave(&log->io_list_lock, flags);
Christoph Hellwigd8858f42015-10-05 09:31:08 +0200610 list_for_each_entry(io, &log->flushing_ios, log_sibling)
611 r5l_io_run_stripes(io);
Christoph Hellwig04732f72015-10-05 09:31:07 +0200612 list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -0700613 spin_unlock_irqrestore(&log->io_list_lock, flags);
614}
615
Shaohua Li0576b1c2015-08-13 14:32:00 -0700616/*
617 * Starting dispatch IO to raid.
618 * io_unit(meta) consists of a log. There is one situation we want to avoid. A
619 * broken meta in the middle of a log causes recovery can't find meta at the
620 * head of log. If operations require meta at the head persistent in log, we
621 * must make sure meta before it persistent in log too. A case is:
622 *
623 * stripe data/parity is in log, we start write stripe to raid disks. stripe
624 * data/parity must be persistent in log before we do the write to raid disks.
625 *
626 * The solution is we restrictly maintain io_unit list order. In this case, we
627 * only write stripes of an io_unit to raid disks till the io_unit is the first
628 * one whose data/parity is in log.
629 */
630void r5l_flush_stripe_to_raid(struct r5l_log *log)
631{
Shaohua Lia8c34f92015-09-02 13:49:46 -0700632 bool do_flush;
Christoph Hellwig56fef7c2015-10-05 09:31:09 +0200633
634 if (!log || !log->need_cache_flush)
Shaohua Li0576b1c2015-08-13 14:32:00 -0700635 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700636
Shaohua Lia8c34f92015-09-02 13:49:46 -0700637 spin_lock_irq(&log->io_list_lock);
638 /* flush bio is running */
639 if (!list_empty(&log->flushing_ios)) {
640 spin_unlock_irq(&log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700641 return;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700642 }
Shaohua Lia8c34f92015-09-02 13:49:46 -0700643 list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
644 do_flush = !list_empty(&log->flushing_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700645 spin_unlock_irq(&log->io_list_lock);
Shaohua Lia8c34f92015-09-02 13:49:46 -0700646
647 if (!do_flush)
648 return;
649 bio_reset(&log->flush_bio);
650 log->flush_bio.bi_bdev = log->rdev->bdev;
651 log->flush_bio.bi_end_io = r5l_log_flush_endio;
652 submit_bio(WRITE_FLUSH, &log->flush_bio);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700653}
654
Shaohua Li0576b1c2015-08-13 14:32:00 -0700655static void r5l_write_super(struct r5l_log *log, sector_t cp);
656static void r5l_do_reclaim(struct r5l_log *log)
657{
Shaohua Li0576b1c2015-08-13 14:32:00 -0700658 sector_t reclaim_target = xchg(&log->reclaim_target, 0);
Christoph Hellwig17036462015-10-05 09:31:06 +0200659 sector_t reclaimable;
660 sector_t next_checkpoint;
661 u64 next_cp_seq;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700662
663 spin_lock_irq(&log->io_list_lock);
664 /*
665 * move proper io_unit to reclaim list. We should not change the order.
666 * reclaimable/unreclaimable io_unit can be mixed in the list, we
667 * shouldn't reuse space of an unreclaimable io_unit
668 */
669 while (1) {
Christoph Hellwig17036462015-10-05 09:31:06 +0200670 reclaimable = r5l_reclaimable_space(log);
671 if (reclaimable >= reclaim_target ||
Shaohua Li0576b1c2015-08-13 14:32:00 -0700672 (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) &&
Christoph Hellwig04732f72015-10-05 09:31:07 +0200675 list_empty(&log->finished_ios)))
Shaohua Li0576b1c2015-08-13 14:32:00 -0700676 break;
677
Christoph Hellwig17036462015-10-05 09:31:06 +0200678 md_wakeup_thread(log->rdev->mddev->thread);
679 wait_event_lock_irq(log->iounit_wait,
680 r5l_reclaimable_space(log) > reclaimable,
681 log->io_list_lock);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700682 }
Christoph Hellwig17036462015-10-05 09:31:06 +0200683
684 next_checkpoint = log->next_checkpoint;
685 next_cp_seq = log->next_cp_seq;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700686 spin_unlock_irq(&log->io_list_lock);
687
Christoph Hellwig17036462015-10-05 09:31:06 +0200688 BUG_ON(reclaimable < 0);
689 if (reclaimable == 0)
Shaohua Li0576b1c2015-08-13 14:32:00 -0700690 return;
691
Shaohua Li0576b1c2015-08-13 14:32:00 -0700692 /*
693 * write_super will flush cache of each raid disk. We must write super
694 * here, because the log area might be reused soon and we don't want to
695 * confuse recovery
696 */
Christoph Hellwig17036462015-10-05 09:31:06 +0200697 r5l_write_super(log, next_checkpoint);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700698
699 mutex_lock(&log->io_mutex);
Christoph Hellwig17036462015-10-05 09:31:06 +0200700 log->last_checkpoint = next_checkpoint;
701 log->last_cp_seq = next_cp_seq;
Shaohua Li0576b1c2015-08-13 14:32:00 -0700702 mutex_unlock(&log->io_mutex);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700703
Christoph Hellwig17036462015-10-05 09:31:06 +0200704 r5l_run_no_space_stripes(log);
Shaohua Li0576b1c2015-08-13 14:32:00 -0700705}
706
707static void r5l_reclaim_thread(struct md_thread *thread)
708{
709 struct mddev *mddev = thread->mddev;
710 struct r5conf *conf = mddev->private;
711 struct r5l_log *log = conf->log;
712
713 if (!log)
714 return;
715 r5l_do_reclaim(log);
716}
717
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700718static void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
719{
Shaohua Li0576b1c2015-08-13 14:32:00 -0700720 unsigned long target;
721 unsigned long new = (unsigned long)space; /* overflow in theory */
722
723 do {
724 target = log->reclaim_target;
725 if (new < target)
726 return;
727 } while (cmpxchg(&log->reclaim_target, target, new) != target);
728 md_wakeup_thread(log->reclaim_thread);
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700729}
730
Shaohua Lie6c033f2015-10-04 09:20:12 -0700731void r5l_quiesce(struct r5l_log *log, int state)
732{
733 if (!log || state == 2)
734 return;
735 if (state == 0) {
736 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
737 log->rdev->mddev, "reclaim");
738 } else if (state == 1) {
739 /*
740 * at this point all stripes are finished, so io_unit is at
741 * least in STRIPE_END state
742 */
743 r5l_wake_reclaim(log, -1L);
744 md_unregister_thread(&log->reclaim_thread);
745 r5l_do_reclaim(log);
746 }
747}
748
Shaohua Li355810d2015-08-13 14:32:01 -0700749struct r5l_recovery_ctx {
750 struct page *meta_page; /* current meta */
751 sector_t meta_total_blocks; /* total size of current meta and data */
752 sector_t pos; /* recovery position */
753 u64 seq; /* recovery position seq */
754};
755
756static int r5l_read_meta_block(struct r5l_log *log,
757 struct r5l_recovery_ctx *ctx)
758{
759 struct page *page = ctx->meta_page;
760 struct r5l_meta_block *mb;
761 u32 crc, stored_crc;
762
763 if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, READ, false))
764 return -EIO;
765
766 mb = page_address(page);
767 stored_crc = le32_to_cpu(mb->checksum);
768 mb->checksum = 0;
769
770 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
771 le64_to_cpu(mb->seq) != ctx->seq ||
772 mb->version != R5LOG_VERSION ||
773 le64_to_cpu(mb->position) != ctx->pos)
774 return -EINVAL;
775
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700776 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -0700777 if (stored_crc != crc)
778 return -EINVAL;
779
780 if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
781 return -EINVAL;
782
783 ctx->meta_total_blocks = BLOCK_SECTORS;
784
785 return 0;
786}
787
788static int r5l_recovery_flush_one_stripe(struct r5l_log *log,
789 struct r5l_recovery_ctx *ctx,
790 sector_t stripe_sect,
791 int *offset, sector_t *log_offset)
792{
793 struct r5conf *conf = log->rdev->mddev->private;
794 struct stripe_head *sh;
795 struct r5l_payload_data_parity *payload;
796 int disk_index;
797
798 sh = raid5_get_active_stripe(conf, stripe_sect, 0, 0, 0);
799 while (1) {
800 payload = page_address(ctx->meta_page) + *offset;
801
802 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) {
803 raid5_compute_sector(conf,
804 le64_to_cpu(payload->location), 0,
805 &disk_index, sh);
806
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 ctx->meta_total_blocks += BLOCK_SECTORS;
813 } else {
814 disk_index = sh->pd_idx;
815 sync_page_io(log->rdev, *log_offset, PAGE_SIZE,
816 sh->dev[disk_index].page, READ, false);
817 sh->dev[disk_index].log_checksum =
818 le32_to_cpu(payload->checksum[0]);
819 set_bit(R5_Wantwrite, &sh->dev[disk_index].flags);
820
821 if (sh->qd_idx >= 0) {
822 disk_index = sh->qd_idx;
823 sync_page_io(log->rdev,
824 r5l_ring_add(log, *log_offset, BLOCK_SECTORS),
825 PAGE_SIZE, sh->dev[disk_index].page,
826 READ, false);
827 sh->dev[disk_index].log_checksum =
828 le32_to_cpu(payload->checksum[1]);
829 set_bit(R5_Wantwrite,
830 &sh->dev[disk_index].flags);
831 }
832 ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
833 }
834
835 *log_offset = r5l_ring_add(log, *log_offset,
836 le32_to_cpu(payload->size));
837 *offset += sizeof(struct r5l_payload_data_parity) +
838 sizeof(__le32) *
839 (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
840 if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY)
841 break;
842 }
843
844 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
845 void *addr;
846 u32 checksum;
847
848 if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
849 continue;
850 addr = kmap_atomic(sh->dev[disk_index].page);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700851 checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -0700852 kunmap_atomic(addr);
853 if (checksum != sh->dev[disk_index].log_checksum)
854 goto error;
855 }
856
857 for (disk_index = 0; disk_index < sh->disks; disk_index++) {
858 struct md_rdev *rdev, *rrdev;
859
860 if (!test_and_clear_bit(R5_Wantwrite,
861 &sh->dev[disk_index].flags))
862 continue;
863
864 /* in case device is broken */
865 rdev = rcu_dereference(conf->disks[disk_index].rdev);
866 if (rdev)
867 sync_page_io(rdev, stripe_sect, PAGE_SIZE,
868 sh->dev[disk_index].page, WRITE, false);
869 rrdev = rcu_dereference(conf->disks[disk_index].replacement);
870 if (rrdev)
871 sync_page_io(rrdev, stripe_sect, PAGE_SIZE,
872 sh->dev[disk_index].page, WRITE, false);
873 }
874 raid5_release_stripe(sh);
875 return 0;
876
877error:
878 for (disk_index = 0; disk_index < sh->disks; disk_index++)
879 sh->dev[disk_index].flags = 0;
880 raid5_release_stripe(sh);
881 return -EINVAL;
882}
883
884static int r5l_recovery_flush_one_meta(struct r5l_log *log,
885 struct r5l_recovery_ctx *ctx)
886{
887 struct r5conf *conf = log->rdev->mddev->private;
888 struct r5l_payload_data_parity *payload;
889 struct r5l_meta_block *mb;
890 int offset;
891 sector_t log_offset;
892 sector_t stripe_sector;
893
894 mb = page_address(ctx->meta_page);
895 offset = sizeof(struct r5l_meta_block);
896 log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
897
898 while (offset < le32_to_cpu(mb->meta_size)) {
899 int dd;
900
901 payload = (void *)mb + offset;
902 stripe_sector = raid5_compute_sector(conf,
903 le64_to_cpu(payload->location), 0, &dd, NULL);
904 if (r5l_recovery_flush_one_stripe(log, ctx, stripe_sector,
905 &offset, &log_offset))
906 return -EINVAL;
907 }
908 return 0;
909}
910
911/* copy data/parity from log to raid disks */
912static void r5l_recovery_flush_log(struct r5l_log *log,
913 struct r5l_recovery_ctx *ctx)
914{
915 while (1) {
916 if (r5l_read_meta_block(log, ctx))
917 return;
918 if (r5l_recovery_flush_one_meta(log, ctx))
919 return;
920 ctx->seq++;
921 ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
922 }
923}
924
925static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
926 u64 seq)
927{
928 struct page *page;
929 struct r5l_meta_block *mb;
930 u32 crc;
931
932 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
933 if (!page)
934 return -ENOMEM;
935 mb = page_address(page);
936 mb->magic = cpu_to_le32(R5LOG_MAGIC);
937 mb->version = R5LOG_VERSION;
938 mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
939 mb->seq = cpu_to_le64(seq);
940 mb->position = cpu_to_le64(pos);
Shaohua Li5cb2fbd2015-10-28 08:41:25 -0700941 crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Li355810d2015-08-13 14:32:01 -0700942 mb->checksum = cpu_to_le32(crc);
943
944 if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, WRITE_FUA, false)) {
945 __free_page(page);
946 return -EIO;
947 }
948 __free_page(page);
949 return 0;
950}
951
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700952static int r5l_recovery_log(struct r5l_log *log)
953{
Shaohua Li355810d2015-08-13 14:32:01 -0700954 struct r5l_recovery_ctx ctx;
955
956 ctx.pos = log->last_checkpoint;
957 ctx.seq = log->last_cp_seq;
958 ctx.meta_page = alloc_page(GFP_KERNEL);
959 if (!ctx.meta_page)
960 return -ENOMEM;
961
962 r5l_recovery_flush_log(log, &ctx);
963 __free_page(ctx.meta_page);
964
965 /*
966 * we did a recovery. Now ctx.pos points to an invalid meta block. New
967 * log will start here. but we can't let superblock point to last valid
968 * meta block. The log might looks like:
969 * | meta 1| meta 2| meta 3|
970 * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
971 * superblock points to meta 1, we write a new valid meta 2n. if crash
972 * happens again, new recovery will start from meta 1. Since meta 2n is
973 * valid now, recovery will think meta 3 is valid, which is wrong.
974 * The solution is we create a new meta in meta2 with its seq == meta
975 * 1's seq + 10 and let superblock points to meta2. The same recovery will
976 * not think meta 3 is a valid meta, because its seq doesn't match
977 */
978 if (ctx.seq > log->last_cp_seq + 1) {
979 int ret;
980
981 ret = r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq + 10);
982 if (ret)
983 return ret;
984 log->seq = ctx.seq + 11;
985 log->log_start = r5l_ring_add(log, ctx.pos, BLOCK_SECTORS);
986 r5l_write_super(log, ctx.pos);
987 } else {
988 log->log_start = ctx.pos;
989 log->seq = ctx.seq;
990 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700991 return 0;
992}
993
994static void r5l_write_super(struct r5l_log *log, sector_t cp)
995{
996 struct mddev *mddev = log->rdev->mddev;
997
998 log->rdev->journal_tail = cp;
999 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1000}
1001
1002static int r5l_load_log(struct r5l_log *log)
1003{
1004 struct md_rdev *rdev = log->rdev;
1005 struct page *page;
1006 struct r5l_meta_block *mb;
1007 sector_t cp = log->rdev->journal_tail;
1008 u32 stored_crc, expected_crc;
1009 bool create_super = false;
1010 int ret;
1011
1012 /* Make sure it's valid */
1013 if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
1014 cp = 0;
1015 page = alloc_page(GFP_KERNEL);
1016 if (!page)
1017 return -ENOMEM;
1018
1019 if (!sync_page_io(rdev, cp, PAGE_SIZE, page, READ, false)) {
1020 ret = -EIO;
1021 goto ioerr;
1022 }
1023 mb = page_address(page);
1024
1025 if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
1026 mb->version != R5LOG_VERSION) {
1027 create_super = true;
1028 goto create;
1029 }
1030 stored_crc = le32_to_cpu(mb->checksum);
1031 mb->checksum = 0;
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001032 expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001033 if (stored_crc != expected_crc) {
1034 create_super = true;
1035 goto create;
1036 }
1037 if (le64_to_cpu(mb->position) != cp) {
1038 create_super = true;
1039 goto create;
1040 }
1041create:
1042 if (create_super) {
1043 log->last_cp_seq = prandom_u32();
1044 cp = 0;
1045 /*
1046 * Make sure super points to correct address. Log might have
1047 * data very soon. If super hasn't correct log tail address,
1048 * recovery can't find the log
1049 */
1050 r5l_write_super(log, cp);
1051 } else
1052 log->last_cp_seq = le64_to_cpu(mb->seq);
1053
1054 log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001055 log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
1056 if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
1057 log->max_free_space = RECLAIM_MAX_FREE_SPACE;
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001058 log->last_checkpoint = cp;
1059
1060 __free_page(page);
1061
1062 return r5l_recovery_log(log);
1063ioerr:
1064 __free_page(page);
1065 return ret;
1066}
1067
1068int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
1069{
1070 struct r5l_log *log;
1071
1072 if (PAGE_SIZE != 4096)
1073 return -EINVAL;
1074 log = kzalloc(sizeof(*log), GFP_KERNEL);
1075 if (!log)
1076 return -ENOMEM;
1077 log->rdev = rdev;
1078
Christoph Hellwig56fef7c2015-10-05 09:31:09 +02001079 log->need_cache_flush = (rdev->bdev->bd_disk->queue->flush_flags != 0);
1080
Shaohua Li5cb2fbd2015-10-28 08:41:25 -07001081 log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
1082 sizeof(rdev->mddev->uuid));
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001083
1084 mutex_init(&log->io_mutex);
1085
1086 spin_lock_init(&log->io_list_lock);
1087 INIT_LIST_HEAD(&log->running_ios);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001088 INIT_LIST_HEAD(&log->io_end_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07001089 INIT_LIST_HEAD(&log->flushing_ios);
Christoph Hellwig04732f72015-10-05 09:31:07 +02001090 INIT_LIST_HEAD(&log->finished_ios);
Shaohua Lia8c34f92015-09-02 13:49:46 -07001091 bio_init(&log->flush_bio);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001092
1093 log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
1094 if (!log->io_kc)
1095 goto io_kc;
1096
Shaohua Li0576b1c2015-08-13 14:32:00 -07001097 log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
1098 log->rdev->mddev, "reclaim");
1099 if (!log->reclaim_thread)
1100 goto reclaim_thread;
Shaohua Li0fd22b42015-09-02 13:49:47 -07001101 init_waitqueue_head(&log->iounit_wait);
Shaohua Li0576b1c2015-08-13 14:32:00 -07001102
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001103 INIT_LIST_HEAD(&log->no_space_stripes);
1104 spin_lock_init(&log->no_space_stripes_lock);
1105
1106 if (r5l_load_log(log))
1107 goto error;
1108
1109 conf->log = log;
1110 return 0;
1111error:
Shaohua Li0576b1c2015-08-13 14:32:00 -07001112 md_unregister_thread(&log->reclaim_thread);
1113reclaim_thread:
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001114 kmem_cache_destroy(log->io_kc);
1115io_kc:
1116 kfree(log);
1117 return -EINVAL;
1118}
1119
1120void r5l_exit_log(struct r5l_log *log)
1121{
Shaohua Li0576b1c2015-08-13 14:32:00 -07001122 md_unregister_thread(&log->reclaim_thread);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001123 kmem_cache_destroy(log->io_kc);
1124 kfree(log);
1125}