]>
Commit | Line | Data |
---|---|---|
f6bed0ef SL |
1 | /* |
2 | * Copyright (C) 2015 Shaohua Li <shli@fb.com> | |
b4c625c6 | 3 | * Copyright (C) 2016 Song Liu <songliubraving@fb.com> |
f6bed0ef SL |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms and conditions of the GNU General Public License, | |
7 | * version 2, as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | */ | |
15 | #include <linux/kernel.h> | |
16 | #include <linux/wait.h> | |
17 | #include <linux/blkdev.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/raid/md_p.h> | |
5cb2fbd6 | 20 | #include <linux/crc32c.h> |
f6bed0ef | 21 | #include <linux/random.h> |
ce1ccd07 | 22 | #include <linux/kthread.h> |
f6bed0ef SL |
23 | #include "md.h" |
24 | #include "raid5.h" | |
1e6d690b | 25 | #include "bitmap.h" |
f6bed0ef SL |
26 | |
27 | /* | |
28 | * metadata/data stored in disk with 4k size unit (a block) regardless | |
29 | * underneath hardware sector size. only works with PAGE_SIZE == 4096 | |
30 | */ | |
31 | #define BLOCK_SECTORS (8) | |
32 | ||
0576b1c6 | 33 | /* |
a39f7afd SL |
34 | * log->max_free_space is min(1/4 disk size, 10G reclaimable space). |
35 | * | |
36 | * In write through mode, the reclaim runs every log->max_free_space. | |
37 | * This can prevent the recovery scans for too long | |
0576b1c6 SL |
38 | */ |
39 | #define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */ | |
40 | #define RECLAIM_MAX_FREE_SPACE_SHIFT (2) | |
41 | ||
a39f7afd SL |
42 | /* wake up reclaim thread periodically */ |
43 | #define R5C_RECLAIM_WAKEUP_INTERVAL (30 * HZ) | |
44 | /* start flush with these full stripes */ | |
45 | #define R5C_FULL_STRIPE_FLUSH_BATCH 256 | |
46 | /* reclaim stripes in groups */ | |
47 | #define R5C_RECLAIM_STRIPE_GROUP (NR_STRIPE_HASH_LOCKS * 2) | |
48 | ||
c38d29b3 CH |
49 | /* |
50 | * We only need 2 bios per I/O unit to make progress, but ensure we | |
51 | * have a few more available to not get too tight. | |
52 | */ | |
53 | #define R5L_POOL_SIZE 4 | |
54 | ||
2ded3703 SL |
55 | /* |
56 | * r5c journal modes of the array: write-back or write-through. | |
57 | * write-through mode has identical behavior as existing log only | |
58 | * implementation. | |
59 | */ | |
60 | enum r5c_journal_mode { | |
61 | R5C_JOURNAL_MODE_WRITE_THROUGH = 0, | |
62 | R5C_JOURNAL_MODE_WRITE_BACK = 1, | |
63 | }; | |
64 | ||
2c7da14b SL |
65 | static char *r5c_journal_mode_str[] = {"write-through", |
66 | "write-back"}; | |
2ded3703 SL |
67 | /* |
68 | * raid5 cache state machine | |
69 | * | |
9b69173e | 70 | * With the RAID cache, each stripe works in two phases: |
2ded3703 SL |
71 | * - caching phase |
72 | * - writing-out phase | |
73 | * | |
74 | * These two phases are controlled by bit STRIPE_R5C_CACHING: | |
75 | * if STRIPE_R5C_CACHING == 0, the stripe is in writing-out phase | |
76 | * if STRIPE_R5C_CACHING == 1, the stripe is in caching phase | |
77 | * | |
78 | * When there is no journal, or the journal is in write-through mode, | |
79 | * the stripe is always in writing-out phase. | |
80 | * | |
81 | * For write-back journal, the stripe is sent to caching phase on write | |
82 | * (r5c_try_caching_write). r5c_make_stripe_write_out() kicks off | |
83 | * the write-out phase by clearing STRIPE_R5C_CACHING. | |
84 | * | |
85 | * Stripes in caching phase do not write the raid disks. Instead, all | |
86 | * writes are committed from the log device. Therefore, a stripe in | |
87 | * caching phase handles writes as: | |
88 | * - write to log device | |
89 | * - return IO | |
90 | * | |
91 | * Stripes in writing-out phase handle writes as: | |
92 | * - calculate parity | |
93 | * - write pending data and parity to journal | |
94 | * - write data and parity to raid disks | |
95 | * - return IO for pending writes | |
96 | */ | |
97 | ||
f6bed0ef SL |
98 | struct r5l_log { |
99 | struct md_rdev *rdev; | |
100 | ||
101 | u32 uuid_checksum; | |
102 | ||
103 | sector_t device_size; /* log device size, round to | |
104 | * BLOCK_SECTORS */ | |
0576b1c6 SL |
105 | sector_t max_free_space; /* reclaim run if free space is at |
106 | * this size */ | |
f6bed0ef SL |
107 | |
108 | sector_t last_checkpoint; /* log tail. where recovery scan | |
109 | * starts from */ | |
110 | u64 last_cp_seq; /* log tail sequence */ | |
111 | ||
112 | sector_t log_start; /* log head. where new data appends */ | |
113 | u64 seq; /* log head sequence */ | |
114 | ||
17036461 | 115 | sector_t next_checkpoint; |
17036461 | 116 | |
f6bed0ef SL |
117 | struct mutex io_mutex; |
118 | struct r5l_io_unit *current_io; /* current io_unit accepting new data */ | |
119 | ||
120 | spinlock_t io_list_lock; | |
121 | struct list_head running_ios; /* io_units which are still running, | |
122 | * and have not yet been completely | |
123 | * written to the log */ | |
124 | struct list_head io_end_ios; /* io_units which have been completely | |
125 | * written to the log but not yet written | |
126 | * to the RAID */ | |
a8c34f91 SL |
127 | struct list_head flushing_ios; /* io_units which are waiting for log |
128 | * cache flush */ | |
04732f74 | 129 | struct list_head finished_ios; /* io_units which settle down in log disk */ |
a8c34f91 | 130 | struct bio flush_bio; |
f6bed0ef | 131 | |
5036c390 CH |
132 | struct list_head no_mem_stripes; /* pending stripes, -ENOMEM */ |
133 | ||
f6bed0ef | 134 | struct kmem_cache *io_kc; |
5036c390 | 135 | mempool_t *io_pool; |
c38d29b3 | 136 | struct bio_set *bs; |
e8deb638 | 137 | mempool_t *meta_pool; |
f6bed0ef | 138 | |
0576b1c6 SL |
139 | struct md_thread *reclaim_thread; |
140 | unsigned long reclaim_target; /* number of space that need to be | |
141 | * reclaimed. if it's 0, reclaim spaces | |
142 | * used by io_units which are in | |
143 | * IO_UNIT_STRIPE_END state (eg, reclaim | |
144 | * dones't wait for specific io_unit | |
145 | * switching to IO_UNIT_STRIPE_END | |
146 | * state) */ | |
0fd22b45 | 147 | wait_queue_head_t iounit_wait; |
0576b1c6 | 148 | |
f6bed0ef SL |
149 | struct list_head no_space_stripes; /* pending stripes, log has no space */ |
150 | spinlock_t no_space_stripes_lock; | |
56fef7c6 CH |
151 | |
152 | bool need_cache_flush; | |
2ded3703 SL |
153 | |
154 | /* for r5c_cache */ | |
155 | enum r5c_journal_mode r5c_journal_mode; | |
a39f7afd SL |
156 | |
157 | /* all stripes in r5cache, in the order of seq at sh->log_start */ | |
158 | struct list_head stripe_in_journal_list; | |
159 | ||
160 | spinlock_t stripe_in_journal_lock; | |
161 | atomic_t stripe_in_journal_count; | |
3bddb7f8 SL |
162 | |
163 | /* to submit async io_units, to fulfill ordering of flush */ | |
164 | struct work_struct deferred_io_work; | |
f6bed0ef SL |
165 | }; |
166 | ||
167 | /* | |
168 | * an IO range starts from a meta data block and end at the next meta data | |
169 | * block. The io unit's the meta data block tracks data/parity followed it. io | |
170 | * unit is written to log disk with normal write, as we always flush log disk | |
171 | * first and then start move data to raid disks, there is no requirement to | |
172 | * write io unit with FLUSH/FUA | |
173 | */ | |
174 | struct r5l_io_unit { | |
175 | struct r5l_log *log; | |
176 | ||
177 | struct page *meta_page; /* store meta block */ | |
178 | int meta_offset; /* current offset in meta_page */ | |
179 | ||
f6bed0ef SL |
180 | struct bio *current_bio;/* current_bio accepting new data */ |
181 | ||
182 | atomic_t pending_stripe;/* how many stripes not flushed to raid */ | |
183 | u64 seq; /* seq number of the metablock */ | |
184 | sector_t log_start; /* where the io_unit starts */ | |
185 | sector_t log_end; /* where the io_unit ends */ | |
186 | struct list_head log_sibling; /* log->running_ios */ | |
187 | struct list_head stripe_list; /* stripes added to the io_unit */ | |
188 | ||
189 | int state; | |
6143e2ce | 190 | bool need_split_bio; |
3bddb7f8 SL |
191 | struct bio *split_bio; |
192 | ||
193 | unsigned int has_flush:1; /* include flush request */ | |
194 | unsigned int has_fua:1; /* include fua request */ | |
195 | unsigned int has_null_flush:1; /* include empty flush request */ | |
196 | /* | |
197 | * io isn't sent yet, flush/fua request can only be submitted till it's | |
198 | * the first IO in running_ios list | |
199 | */ | |
200 | unsigned int io_deferred:1; | |
201 | ||
202 | struct bio_list flush_barriers; /* size == 0 flush bios */ | |
f6bed0ef SL |
203 | }; |
204 | ||
205 | /* r5l_io_unit state */ | |
206 | enum r5l_io_unit_state { | |
207 | IO_UNIT_RUNNING = 0, /* accepting new IO */ | |
208 | IO_UNIT_IO_START = 1, /* io_unit bio start writing to log, | |
209 | * don't accepting new bio */ | |
210 | IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */ | |
a8c34f91 | 211 | IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */ |
f6bed0ef SL |
212 | }; |
213 | ||
2ded3703 SL |
214 | bool r5c_is_writeback(struct r5l_log *log) |
215 | { | |
216 | return (log != NULL && | |
217 | log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK); | |
218 | } | |
219 | ||
f6bed0ef SL |
220 | static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc) |
221 | { | |
222 | start += inc; | |
223 | if (start >= log->device_size) | |
224 | start = start - log->device_size; | |
225 | return start; | |
226 | } | |
227 | ||
228 | static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start, | |
229 | sector_t end) | |
230 | { | |
231 | if (end >= start) | |
232 | return end - start; | |
233 | else | |
234 | return end + log->device_size - start; | |
235 | } | |
236 | ||
237 | static bool r5l_has_free_space(struct r5l_log *log, sector_t size) | |
238 | { | |
239 | sector_t used_size; | |
240 | ||
241 | used_size = r5l_ring_distance(log, log->last_checkpoint, | |
242 | log->log_start); | |
243 | ||
244 | return log->device_size > used_size + size; | |
245 | } | |
246 | ||
f6bed0ef SL |
247 | static void __r5l_set_io_unit_state(struct r5l_io_unit *io, |
248 | enum r5l_io_unit_state state) | |
249 | { | |
f6bed0ef SL |
250 | if (WARN_ON(io->state >= state)) |
251 | return; | |
252 | io->state = state; | |
f6bed0ef SL |
253 | } |
254 | ||
1e6d690b SL |
255 | static void |
256 | r5c_return_dev_pending_writes(struct r5conf *conf, struct r5dev *dev, | |
257 | struct bio_list *return_bi) | |
258 | { | |
259 | struct bio *wbi, *wbi2; | |
260 | ||
261 | wbi = dev->written; | |
262 | dev->written = NULL; | |
263 | while (wbi && wbi->bi_iter.bi_sector < | |
264 | dev->sector + STRIPE_SECTORS) { | |
265 | wbi2 = r5_next_bio(wbi, dev->sector); | |
266 | if (!raid5_dec_bi_active_stripes(wbi)) { | |
267 | md_write_end(conf->mddev); | |
268 | bio_list_add(return_bi, wbi); | |
269 | } | |
270 | wbi = wbi2; | |
271 | } | |
272 | } | |
273 | ||
274 | void r5c_handle_cached_data_endio(struct r5conf *conf, | |
275 | struct stripe_head *sh, int disks, struct bio_list *return_bi) | |
276 | { | |
277 | int i; | |
278 | ||
279 | for (i = sh->disks; i--; ) { | |
280 | if (sh->dev[i].written) { | |
281 | set_bit(R5_UPTODATE, &sh->dev[i].flags); | |
282 | r5c_return_dev_pending_writes(conf, &sh->dev[i], | |
283 | return_bi); | |
284 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, | |
285 | STRIPE_SECTORS, | |
286 | !test_bit(STRIPE_DEGRADED, &sh->state), | |
287 | 0); | |
288 | } | |
289 | } | |
290 | } | |
291 | ||
a39f7afd SL |
292 | /* Check whether we should flush some stripes to free up stripe cache */ |
293 | void r5c_check_stripe_cache_usage(struct r5conf *conf) | |
294 | { | |
295 | int total_cached; | |
296 | ||
297 | if (!r5c_is_writeback(conf->log)) | |
298 | return; | |
299 | ||
300 | total_cached = atomic_read(&conf->r5c_cached_partial_stripes) + | |
301 | atomic_read(&conf->r5c_cached_full_stripes); | |
302 | ||
303 | /* | |
304 | * The following condition is true for either of the following: | |
305 | * - stripe cache pressure high: | |
306 | * total_cached > 3/4 min_nr_stripes || | |
307 | * empty_inactive_list_nr > 0 | |
308 | * - stripe cache pressure moderate: | |
309 | * total_cached > 1/2 min_nr_stripes | |
310 | */ | |
311 | if (total_cached > conf->min_nr_stripes * 1 / 2 || | |
312 | atomic_read(&conf->empty_inactive_list_nr) > 0) | |
313 | r5l_wake_reclaim(conf->log, 0); | |
314 | } | |
315 | ||
316 | /* | |
317 | * flush cache when there are R5C_FULL_STRIPE_FLUSH_BATCH or more full | |
318 | * stripes in the cache | |
319 | */ | |
320 | void r5c_check_cached_full_stripe(struct r5conf *conf) | |
321 | { | |
322 | if (!r5c_is_writeback(conf->log)) | |
323 | return; | |
324 | ||
325 | /* | |
326 | * wake up reclaim for R5C_FULL_STRIPE_FLUSH_BATCH cached stripes | |
327 | * or a full stripe (chunk size / 4k stripes). | |
328 | */ | |
329 | if (atomic_read(&conf->r5c_cached_full_stripes) >= | |
330 | min(R5C_FULL_STRIPE_FLUSH_BATCH, | |
331 | conf->chunk_sectors >> STRIPE_SHIFT)) | |
332 | r5l_wake_reclaim(conf->log, 0); | |
333 | } | |
334 | ||
335 | /* | |
336 | * Total log space (in sectors) needed to flush all data in cache | |
337 | * | |
338 | * Currently, writing-out phase automatically includes all pending writes | |
339 | * to the same sector. So the reclaim of each stripe takes up to | |
340 | * (conf->raid_disks + 1) pages of log space. | |
341 | * | |
342 | * To totally avoid deadlock due to log space, the code reserves | |
343 | * (conf->raid_disks + 1) pages for each stripe in cache, which is not | |
344 | * necessary in most cases. | |
345 | * | |
346 | * To improve this, we will need writing-out phase to be able to NOT include | |
347 | * pending writes, which will reduce the requirement to | |
348 | * (conf->max_degraded + 1) pages per stripe in cache. | |
349 | */ | |
350 | static sector_t r5c_log_required_to_flush_cache(struct r5conf *conf) | |
351 | { | |
352 | struct r5l_log *log = conf->log; | |
353 | ||
354 | if (!r5c_is_writeback(log)) | |
355 | return 0; | |
356 | ||
357 | return BLOCK_SECTORS * (conf->raid_disks + 1) * | |
358 | atomic_read(&log->stripe_in_journal_count); | |
359 | } | |
360 | ||
361 | /* | |
362 | * evaluate log space usage and update R5C_LOG_TIGHT and R5C_LOG_CRITICAL | |
363 | * | |
364 | * R5C_LOG_TIGHT is set when free space on the log device is less than 3x of | |
365 | * reclaim_required_space. R5C_LOG_CRITICAL is set when free space on the log | |
366 | * device is less than 2x of reclaim_required_space. | |
367 | */ | |
368 | static inline void r5c_update_log_state(struct r5l_log *log) | |
369 | { | |
370 | struct r5conf *conf = log->rdev->mddev->private; | |
371 | sector_t free_space; | |
372 | sector_t reclaim_space; | |
373 | ||
374 | if (!r5c_is_writeback(log)) | |
375 | return; | |
376 | ||
377 | free_space = r5l_ring_distance(log, log->log_start, | |
378 | log->last_checkpoint); | |
379 | reclaim_space = r5c_log_required_to_flush_cache(conf); | |
380 | if (free_space < 2 * reclaim_space) | |
381 | set_bit(R5C_LOG_CRITICAL, &conf->cache_state); | |
382 | else | |
383 | clear_bit(R5C_LOG_CRITICAL, &conf->cache_state); | |
384 | if (free_space < 3 * reclaim_space) | |
385 | set_bit(R5C_LOG_TIGHT, &conf->cache_state); | |
386 | else | |
387 | clear_bit(R5C_LOG_TIGHT, &conf->cache_state); | |
388 | } | |
389 | ||
2ded3703 SL |
390 | /* |
391 | * Put the stripe into writing-out phase by clearing STRIPE_R5C_CACHING. | |
392 | * This function should only be called in write-back mode. | |
393 | */ | |
a39f7afd | 394 | void r5c_make_stripe_write_out(struct stripe_head *sh) |
2ded3703 SL |
395 | { |
396 | struct r5conf *conf = sh->raid_conf; | |
397 | struct r5l_log *log = conf->log; | |
398 | ||
399 | BUG_ON(!r5c_is_writeback(log)); | |
400 | ||
401 | WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); | |
402 | clear_bit(STRIPE_R5C_CACHING, &sh->state); | |
1e6d690b SL |
403 | |
404 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) | |
405 | atomic_inc(&conf->preread_active_stripes); | |
406 | ||
407 | if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) { | |
408 | BUG_ON(atomic_read(&conf->r5c_cached_partial_stripes) == 0); | |
409 | atomic_dec(&conf->r5c_cached_partial_stripes); | |
410 | } | |
411 | ||
412 | if (test_and_clear_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) { | |
413 | BUG_ON(atomic_read(&conf->r5c_cached_full_stripes) == 0); | |
414 | atomic_dec(&conf->r5c_cached_full_stripes); | |
415 | } | |
416 | } | |
417 | ||
418 | static void r5c_handle_data_cached(struct stripe_head *sh) | |
419 | { | |
420 | int i; | |
421 | ||
422 | for (i = sh->disks; i--; ) | |
423 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { | |
424 | set_bit(R5_InJournal, &sh->dev[i].flags); | |
425 | clear_bit(R5_LOCKED, &sh->dev[i].flags); | |
426 | } | |
427 | clear_bit(STRIPE_LOG_TRAPPED, &sh->state); | |
428 | } | |
429 | ||
430 | /* | |
431 | * this journal write must contain full parity, | |
432 | * it may also contain some data pages | |
433 | */ | |
434 | static void r5c_handle_parity_cached(struct stripe_head *sh) | |
435 | { | |
436 | int i; | |
437 | ||
438 | for (i = sh->disks; i--; ) | |
439 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) | |
440 | set_bit(R5_Wantwrite, &sh->dev[i].flags); | |
2ded3703 SL |
441 | } |
442 | ||
443 | /* | |
444 | * Setting proper flags after writing (or flushing) data and/or parity to the | |
445 | * log device. This is called from r5l_log_endio() or r5l_log_flush_endio(). | |
446 | */ | |
447 | static void r5c_finish_cache_stripe(struct stripe_head *sh) | |
448 | { | |
449 | struct r5l_log *log = sh->raid_conf->log; | |
450 | ||
451 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) { | |
452 | BUG_ON(test_bit(STRIPE_R5C_CACHING, &sh->state)); | |
453 | /* | |
454 | * Set R5_InJournal for parity dev[pd_idx]. This means | |
455 | * all data AND parity in the journal. For RAID 6, it is | |
456 | * NOT necessary to set the flag for dev[qd_idx], as the | |
457 | * two parities are written out together. | |
458 | */ | |
459 | set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags); | |
1e6d690b SL |
460 | } else if (test_bit(STRIPE_R5C_CACHING, &sh->state)) { |
461 | r5c_handle_data_cached(sh); | |
462 | } else { | |
463 | r5c_handle_parity_cached(sh); | |
464 | set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags); | |
465 | } | |
2ded3703 SL |
466 | } |
467 | ||
d8858f43 CH |
468 | static void r5l_io_run_stripes(struct r5l_io_unit *io) |
469 | { | |
470 | struct stripe_head *sh, *next; | |
471 | ||
472 | list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) { | |
473 | list_del_init(&sh->log_list); | |
2ded3703 SL |
474 | |
475 | r5c_finish_cache_stripe(sh); | |
476 | ||
d8858f43 CH |
477 | set_bit(STRIPE_HANDLE, &sh->state); |
478 | raid5_release_stripe(sh); | |
479 | } | |
480 | } | |
481 | ||
56fef7c6 CH |
482 | static void r5l_log_run_stripes(struct r5l_log *log) |
483 | { | |
484 | struct r5l_io_unit *io, *next; | |
485 | ||
486 | assert_spin_locked(&log->io_list_lock); | |
487 | ||
488 | list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) { | |
489 | /* don't change list order */ | |
490 | if (io->state < IO_UNIT_IO_END) | |
491 | break; | |
492 | ||
493 | list_move_tail(&io->log_sibling, &log->finished_ios); | |
494 | r5l_io_run_stripes(io); | |
495 | } | |
496 | } | |
497 | ||
3848c0bc CH |
498 | static void r5l_move_to_end_ios(struct r5l_log *log) |
499 | { | |
500 | struct r5l_io_unit *io, *next; | |
501 | ||
502 | assert_spin_locked(&log->io_list_lock); | |
503 | ||
504 | list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) { | |
505 | /* don't change list order */ | |
506 | if (io->state < IO_UNIT_IO_END) | |
507 | break; | |
508 | list_move_tail(&io->log_sibling, &log->io_end_ios); | |
509 | } | |
510 | } | |
511 | ||
3bddb7f8 | 512 | static void __r5l_stripe_write_finished(struct r5l_io_unit *io); |
f6bed0ef SL |
513 | static void r5l_log_endio(struct bio *bio) |
514 | { | |
515 | struct r5l_io_unit *io = bio->bi_private; | |
3bddb7f8 | 516 | struct r5l_io_unit *io_deferred; |
f6bed0ef | 517 | struct r5l_log *log = io->log; |
509ffec7 | 518 | unsigned long flags; |
f6bed0ef | 519 | |
6e74a9cf SL |
520 | if (bio->bi_error) |
521 | md_error(log->rdev->mddev, log->rdev); | |
522 | ||
f6bed0ef | 523 | bio_put(bio); |
e8deb638 | 524 | mempool_free(io->meta_page, log->meta_pool); |
f6bed0ef | 525 | |
509ffec7 CH |
526 | spin_lock_irqsave(&log->io_list_lock, flags); |
527 | __r5l_set_io_unit_state(io, IO_UNIT_IO_END); | |
56fef7c6 | 528 | if (log->need_cache_flush) |
3848c0bc | 529 | r5l_move_to_end_ios(log); |
56fef7c6 CH |
530 | else |
531 | r5l_log_run_stripes(log); | |
3bddb7f8 SL |
532 | if (!list_empty(&log->running_ios)) { |
533 | /* | |
534 | * FLUSH/FUA io_unit is deferred because of ordering, now we | |
535 | * can dispatch it | |
536 | */ | |
537 | io_deferred = list_first_entry(&log->running_ios, | |
538 | struct r5l_io_unit, log_sibling); | |
539 | if (io_deferred->io_deferred) | |
540 | schedule_work(&log->deferred_io_work); | |
541 | } | |
542 | ||
509ffec7 CH |
543 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
544 | ||
56fef7c6 CH |
545 | if (log->need_cache_flush) |
546 | md_wakeup_thread(log->rdev->mddev->thread); | |
3bddb7f8 SL |
547 | |
548 | if (io->has_null_flush) { | |
549 | struct bio *bi; | |
550 | ||
551 | WARN_ON(bio_list_empty(&io->flush_barriers)); | |
552 | while ((bi = bio_list_pop(&io->flush_barriers)) != NULL) { | |
553 | bio_endio(bi); | |
554 | atomic_dec(&io->pending_stripe); | |
555 | } | |
556 | if (atomic_read(&io->pending_stripe) == 0) | |
557 | __r5l_stripe_write_finished(io); | |
558 | } | |
559 | } | |
560 | ||
561 | static void r5l_do_submit_io(struct r5l_log *log, struct r5l_io_unit *io) | |
562 | { | |
563 | unsigned long flags; | |
564 | ||
565 | spin_lock_irqsave(&log->io_list_lock, flags); | |
566 | __r5l_set_io_unit_state(io, IO_UNIT_IO_START); | |
567 | spin_unlock_irqrestore(&log->io_list_lock, flags); | |
568 | ||
569 | if (io->has_flush) | |
570 | bio_set_op_attrs(io->current_bio, REQ_OP_WRITE, WRITE_FLUSH); | |
571 | if (io->has_fua) | |
572 | bio_set_op_attrs(io->current_bio, REQ_OP_WRITE, WRITE_FUA); | |
573 | submit_bio(io->current_bio); | |
574 | ||
575 | if (!io->split_bio) | |
576 | return; | |
577 | ||
578 | if (io->has_flush) | |
579 | bio_set_op_attrs(io->split_bio, REQ_OP_WRITE, WRITE_FLUSH); | |
580 | if (io->has_fua) | |
581 | bio_set_op_attrs(io->split_bio, REQ_OP_WRITE, WRITE_FUA); | |
582 | submit_bio(io->split_bio); | |
583 | } | |
584 | ||
585 | /* deferred io_unit will be dispatched here */ | |
586 | static void r5l_submit_io_async(struct work_struct *work) | |
587 | { | |
588 | struct r5l_log *log = container_of(work, struct r5l_log, | |
589 | deferred_io_work); | |
590 | struct r5l_io_unit *io = NULL; | |
591 | unsigned long flags; | |
592 | ||
593 | spin_lock_irqsave(&log->io_list_lock, flags); | |
594 | if (!list_empty(&log->running_ios)) { | |
595 | io = list_first_entry(&log->running_ios, struct r5l_io_unit, | |
596 | log_sibling); | |
597 | if (!io->io_deferred) | |
598 | io = NULL; | |
599 | else | |
600 | io->io_deferred = 0; | |
601 | } | |
602 | spin_unlock_irqrestore(&log->io_list_lock, flags); | |
603 | if (io) | |
604 | r5l_do_submit_io(log, io); | |
f6bed0ef SL |
605 | } |
606 | ||
607 | static void r5l_submit_current_io(struct r5l_log *log) | |
608 | { | |
609 | struct r5l_io_unit *io = log->current_io; | |
3bddb7f8 | 610 | struct bio *bio; |
f6bed0ef | 611 | struct r5l_meta_block *block; |
509ffec7 | 612 | unsigned long flags; |
f6bed0ef | 613 | u32 crc; |
3bddb7f8 | 614 | bool do_submit = true; |
f6bed0ef SL |
615 | |
616 | if (!io) | |
617 | return; | |
618 | ||
619 | block = page_address(io->meta_page); | |
620 | block->meta_size = cpu_to_le32(io->meta_offset); | |
5cb2fbd6 | 621 | crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE); |
f6bed0ef | 622 | block->checksum = cpu_to_le32(crc); |
3bddb7f8 | 623 | bio = io->current_bio; |
f6bed0ef SL |
624 | |
625 | log->current_io = NULL; | |
509ffec7 | 626 | spin_lock_irqsave(&log->io_list_lock, flags); |
3bddb7f8 SL |
627 | if (io->has_flush || io->has_fua) { |
628 | if (io != list_first_entry(&log->running_ios, | |
629 | struct r5l_io_unit, log_sibling)) { | |
630 | io->io_deferred = 1; | |
631 | do_submit = false; | |
632 | } | |
633 | } | |
509ffec7 | 634 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
3bddb7f8 SL |
635 | if (do_submit) |
636 | r5l_do_submit_io(log, io); | |
f6bed0ef SL |
637 | } |
638 | ||
6143e2ce | 639 | static struct bio *r5l_bio_alloc(struct r5l_log *log) |
b349feb3 | 640 | { |
c38d29b3 | 641 | struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs); |
b349feb3 | 642 | |
796a5cf0 | 643 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
b349feb3 | 644 | bio->bi_bdev = log->rdev->bdev; |
1e932a37 | 645 | bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start; |
b349feb3 | 646 | |
b349feb3 CH |
647 | return bio; |
648 | } | |
649 | ||
c1b99198 CH |
650 | static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io) |
651 | { | |
652 | log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS); | |
653 | ||
a39f7afd | 654 | r5c_update_log_state(log); |
c1b99198 CH |
655 | /* |
656 | * If we filled up the log device start from the beginning again, | |
657 | * which will require a new bio. | |
658 | * | |
659 | * Note: for this to work properly the log size needs to me a multiple | |
660 | * of BLOCK_SECTORS. | |
661 | */ | |
662 | if (log->log_start == 0) | |
6143e2ce | 663 | io->need_split_bio = true; |
c1b99198 CH |
664 | |
665 | io->log_end = log->log_start; | |
666 | } | |
667 | ||
f6bed0ef SL |
668 | static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log) |
669 | { | |
670 | struct r5l_io_unit *io; | |
671 | struct r5l_meta_block *block; | |
f6bed0ef | 672 | |
5036c390 CH |
673 | io = mempool_alloc(log->io_pool, GFP_ATOMIC); |
674 | if (!io) | |
675 | return NULL; | |
676 | memset(io, 0, sizeof(*io)); | |
677 | ||
51039cd0 | 678 | io->log = log; |
51039cd0 CH |
679 | INIT_LIST_HEAD(&io->log_sibling); |
680 | INIT_LIST_HEAD(&io->stripe_list); | |
3bddb7f8 | 681 | bio_list_init(&io->flush_barriers); |
51039cd0 | 682 | io->state = IO_UNIT_RUNNING; |
f6bed0ef | 683 | |
e8deb638 | 684 | io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO); |
f6bed0ef | 685 | block = page_address(io->meta_page); |
e8deb638 | 686 | clear_page(block); |
f6bed0ef SL |
687 | block->magic = cpu_to_le32(R5LOG_MAGIC); |
688 | block->version = R5LOG_VERSION; | |
689 | block->seq = cpu_to_le64(log->seq); | |
690 | block->position = cpu_to_le64(log->log_start); | |
691 | ||
692 | io->log_start = log->log_start; | |
693 | io->meta_offset = sizeof(struct r5l_meta_block); | |
2b8ef16e | 694 | io->seq = log->seq++; |
f6bed0ef | 695 | |
6143e2ce CH |
696 | io->current_bio = r5l_bio_alloc(log); |
697 | io->current_bio->bi_end_io = r5l_log_endio; | |
698 | io->current_bio->bi_private = io; | |
b349feb3 | 699 | bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0); |
f6bed0ef | 700 | |
c1b99198 | 701 | r5_reserve_log_entry(log, io); |
f6bed0ef SL |
702 | |
703 | spin_lock_irq(&log->io_list_lock); | |
704 | list_add_tail(&io->log_sibling, &log->running_ios); | |
705 | spin_unlock_irq(&log->io_list_lock); | |
706 | ||
707 | return io; | |
708 | } | |
709 | ||
710 | static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size) | |
711 | { | |
22581f58 CH |
712 | if (log->current_io && |
713 | log->current_io->meta_offset + payload_size > PAGE_SIZE) | |
f6bed0ef | 714 | r5l_submit_current_io(log); |
f6bed0ef | 715 | |
5036c390 | 716 | if (!log->current_io) { |
22581f58 | 717 | log->current_io = r5l_new_meta(log); |
5036c390 CH |
718 | if (!log->current_io) |
719 | return -ENOMEM; | |
720 | } | |
721 | ||
f6bed0ef SL |
722 | return 0; |
723 | } | |
724 | ||
725 | static void r5l_append_payload_meta(struct r5l_log *log, u16 type, | |
726 | sector_t location, | |
727 | u32 checksum1, u32 checksum2, | |
728 | bool checksum2_valid) | |
729 | { | |
730 | struct r5l_io_unit *io = log->current_io; | |
731 | struct r5l_payload_data_parity *payload; | |
732 | ||
733 | payload = page_address(io->meta_page) + io->meta_offset; | |
734 | payload->header.type = cpu_to_le16(type); | |
735 | payload->header.flags = cpu_to_le16(0); | |
736 | payload->size = cpu_to_le32((1 + !!checksum2_valid) << | |
737 | (PAGE_SHIFT - 9)); | |
738 | payload->location = cpu_to_le64(location); | |
739 | payload->checksum[0] = cpu_to_le32(checksum1); | |
740 | if (checksum2_valid) | |
741 | payload->checksum[1] = cpu_to_le32(checksum2); | |
742 | ||
743 | io->meta_offset += sizeof(struct r5l_payload_data_parity) + | |
744 | sizeof(__le32) * (1 + !!checksum2_valid); | |
745 | } | |
746 | ||
747 | static void r5l_append_payload_page(struct r5l_log *log, struct page *page) | |
748 | { | |
749 | struct r5l_io_unit *io = log->current_io; | |
750 | ||
6143e2ce | 751 | if (io->need_split_bio) { |
3bddb7f8 SL |
752 | BUG_ON(io->split_bio); |
753 | io->split_bio = io->current_bio; | |
6143e2ce | 754 | io->current_bio = r5l_bio_alloc(log); |
3bddb7f8 SL |
755 | bio_chain(io->current_bio, io->split_bio); |
756 | io->need_split_bio = false; | |
f6bed0ef | 757 | } |
f6bed0ef | 758 | |
6143e2ce CH |
759 | if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0)) |
760 | BUG(); | |
761 | ||
c1b99198 | 762 | r5_reserve_log_entry(log, io); |
f6bed0ef SL |
763 | } |
764 | ||
5036c390 | 765 | static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh, |
f6bed0ef SL |
766 | int data_pages, int parity_pages) |
767 | { | |
768 | int i; | |
769 | int meta_size; | |
5036c390 | 770 | int ret; |
f6bed0ef SL |
771 | struct r5l_io_unit *io; |
772 | ||
773 | meta_size = | |
774 | ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) | |
775 | * data_pages) + | |
776 | sizeof(struct r5l_payload_data_parity) + | |
777 | sizeof(__le32) * parity_pages; | |
778 | ||
5036c390 CH |
779 | ret = r5l_get_meta(log, meta_size); |
780 | if (ret) | |
781 | return ret; | |
782 | ||
f6bed0ef SL |
783 | io = log->current_io; |
784 | ||
3bddb7f8 SL |
785 | if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state)) |
786 | io->has_flush = 1; | |
787 | ||
f6bed0ef | 788 | for (i = 0; i < sh->disks; i++) { |
1e6d690b SL |
789 | if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) || |
790 | test_bit(R5_InJournal, &sh->dev[i].flags)) | |
f6bed0ef SL |
791 | continue; |
792 | if (i == sh->pd_idx || i == sh->qd_idx) | |
793 | continue; | |
3bddb7f8 SL |
794 | if (test_bit(R5_WantFUA, &sh->dev[i].flags) && |
795 | log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) { | |
796 | io->has_fua = 1; | |
797 | /* | |
798 | * we need to flush journal to make sure recovery can | |
799 | * reach the data with fua flag | |
800 | */ | |
801 | io->has_flush = 1; | |
802 | } | |
f6bed0ef SL |
803 | r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA, |
804 | raid5_compute_blocknr(sh, i, 0), | |
805 | sh->dev[i].log_checksum, 0, false); | |
806 | r5l_append_payload_page(log, sh->dev[i].page); | |
807 | } | |
808 | ||
2ded3703 | 809 | if (parity_pages == 2) { |
f6bed0ef SL |
810 | r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY, |
811 | sh->sector, sh->dev[sh->pd_idx].log_checksum, | |
812 | sh->dev[sh->qd_idx].log_checksum, true); | |
813 | r5l_append_payload_page(log, sh->dev[sh->pd_idx].page); | |
814 | r5l_append_payload_page(log, sh->dev[sh->qd_idx].page); | |
2ded3703 | 815 | } else if (parity_pages == 1) { |
f6bed0ef SL |
816 | r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY, |
817 | sh->sector, sh->dev[sh->pd_idx].log_checksum, | |
818 | 0, false); | |
819 | r5l_append_payload_page(log, sh->dev[sh->pd_idx].page); | |
2ded3703 SL |
820 | } else /* Just writing data, not parity, in caching phase */ |
821 | BUG_ON(parity_pages != 0); | |
f6bed0ef SL |
822 | |
823 | list_add_tail(&sh->log_list, &io->stripe_list); | |
824 | atomic_inc(&io->pending_stripe); | |
825 | sh->log_io = io; | |
5036c390 | 826 | |
a39f7afd SL |
827 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) |
828 | return 0; | |
829 | ||
830 | if (sh->log_start == MaxSector) { | |
831 | BUG_ON(!list_empty(&sh->r5c)); | |
832 | sh->log_start = io->log_start; | |
833 | spin_lock_irq(&log->stripe_in_journal_lock); | |
834 | list_add_tail(&sh->r5c, | |
835 | &log->stripe_in_journal_list); | |
836 | spin_unlock_irq(&log->stripe_in_journal_lock); | |
837 | atomic_inc(&log->stripe_in_journal_count); | |
838 | } | |
5036c390 | 839 | return 0; |
f6bed0ef SL |
840 | } |
841 | ||
a39f7afd SL |
842 | /* add stripe to no_space_stripes, and then wake up reclaim */ |
843 | static inline void r5l_add_no_space_stripe(struct r5l_log *log, | |
844 | struct stripe_head *sh) | |
845 | { | |
846 | spin_lock(&log->no_space_stripes_lock); | |
847 | list_add_tail(&sh->log_list, &log->no_space_stripes); | |
848 | spin_unlock(&log->no_space_stripes_lock); | |
849 | } | |
850 | ||
f6bed0ef SL |
851 | /* |
852 | * running in raid5d, where reclaim could wait for raid5d too (when it flushes | |
853 | * data from log to raid disks), so we shouldn't wait for reclaim here | |
854 | */ | |
855 | int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh) | |
856 | { | |
a39f7afd | 857 | struct r5conf *conf = sh->raid_conf; |
f6bed0ef SL |
858 | int write_disks = 0; |
859 | int data_pages, parity_pages; | |
f6bed0ef SL |
860 | int reserve; |
861 | int i; | |
5036c390 | 862 | int ret = 0; |
a39f7afd | 863 | bool wake_reclaim = false; |
f6bed0ef SL |
864 | |
865 | if (!log) | |
866 | return -EAGAIN; | |
867 | /* Don't support stripe batch */ | |
868 | if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) || | |
869 | test_bit(STRIPE_SYNCING, &sh->state)) { | |
870 | /* the stripe is written to log, we start writing it to raid */ | |
871 | clear_bit(STRIPE_LOG_TRAPPED, &sh->state); | |
872 | return -EAGAIN; | |
873 | } | |
874 | ||
2ded3703 SL |
875 | WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state)); |
876 | ||
f6bed0ef SL |
877 | for (i = 0; i < sh->disks; i++) { |
878 | void *addr; | |
879 | ||
1e6d690b SL |
880 | if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) || |
881 | test_bit(R5_InJournal, &sh->dev[i].flags)) | |
f6bed0ef | 882 | continue; |
1e6d690b | 883 | |
f6bed0ef SL |
884 | write_disks++; |
885 | /* checksum is already calculated in last run */ | |
886 | if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) | |
887 | continue; | |
888 | addr = kmap_atomic(sh->dev[i].page); | |
5cb2fbd6 SL |
889 | sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum, |
890 | addr, PAGE_SIZE); | |
f6bed0ef SL |
891 | kunmap_atomic(addr); |
892 | } | |
893 | parity_pages = 1 + !!(sh->qd_idx >= 0); | |
894 | data_pages = write_disks - parity_pages; | |
895 | ||
f6bed0ef | 896 | set_bit(STRIPE_LOG_TRAPPED, &sh->state); |
253f9fd4 SL |
897 | /* |
898 | * The stripe must enter state machine again to finish the write, so | |
899 | * don't delay. | |
900 | */ | |
901 | clear_bit(STRIPE_DELAYED, &sh->state); | |
f6bed0ef SL |
902 | atomic_inc(&sh->count); |
903 | ||
904 | mutex_lock(&log->io_mutex); | |
905 | /* meta + data */ | |
906 | reserve = (1 + write_disks) << (PAGE_SHIFT - 9); | |
f6bed0ef | 907 | |
a39f7afd SL |
908 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) { |
909 | if (!r5l_has_free_space(log, reserve)) { | |
910 | r5l_add_no_space_stripe(log, sh); | |
911 | wake_reclaim = true; | |
912 | } else { | |
913 | ret = r5l_log_stripe(log, sh, data_pages, parity_pages); | |
914 | if (ret) { | |
915 | spin_lock_irq(&log->io_list_lock); | |
916 | list_add_tail(&sh->log_list, | |
917 | &log->no_mem_stripes); | |
918 | spin_unlock_irq(&log->io_list_lock); | |
919 | } | |
920 | } | |
921 | } else { /* R5C_JOURNAL_MODE_WRITE_BACK */ | |
922 | /* | |
923 | * log space critical, do not process stripes that are | |
924 | * not in cache yet (sh->log_start == MaxSector). | |
925 | */ | |
926 | if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && | |
927 | sh->log_start == MaxSector) { | |
928 | r5l_add_no_space_stripe(log, sh); | |
929 | wake_reclaim = true; | |
930 | reserve = 0; | |
931 | } else if (!r5l_has_free_space(log, reserve)) { | |
932 | if (sh->log_start == log->last_checkpoint) | |
933 | BUG(); | |
934 | else | |
935 | r5l_add_no_space_stripe(log, sh); | |
936 | } else { | |
937 | ret = r5l_log_stripe(log, sh, data_pages, parity_pages); | |
938 | if (ret) { | |
939 | spin_lock_irq(&log->io_list_lock); | |
940 | list_add_tail(&sh->log_list, | |
941 | &log->no_mem_stripes); | |
942 | spin_unlock_irq(&log->io_list_lock); | |
943 | } | |
5036c390 | 944 | } |
f6bed0ef | 945 | } |
f6bed0ef | 946 | |
5036c390 | 947 | mutex_unlock(&log->io_mutex); |
a39f7afd SL |
948 | if (wake_reclaim) |
949 | r5l_wake_reclaim(log, reserve); | |
f6bed0ef SL |
950 | return 0; |
951 | } | |
952 | ||
953 | void r5l_write_stripe_run(struct r5l_log *log) | |
954 | { | |
955 | if (!log) | |
956 | return; | |
957 | mutex_lock(&log->io_mutex); | |
958 | r5l_submit_current_io(log); | |
959 | mutex_unlock(&log->io_mutex); | |
960 | } | |
961 | ||
828cbe98 SL |
962 | int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio) |
963 | { | |
964 | if (!log) | |
965 | return -ENODEV; | |
3bddb7f8 SL |
966 | |
967 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) { | |
968 | /* | |
969 | * in write through (journal only) | |
970 | * we flush log disk cache first, then write stripe data to | |
971 | * raid disks. So if bio is finished, the log disk cache is | |
972 | * flushed already. The recovery guarantees we can recovery | |
973 | * the bio from log disk, so we don't need to flush again | |
974 | */ | |
975 | if (bio->bi_iter.bi_size == 0) { | |
976 | bio_endio(bio); | |
977 | return 0; | |
978 | } | |
979 | bio->bi_opf &= ~REQ_PREFLUSH; | |
980 | } else { | |
981 | /* write back (with cache) */ | |
982 | if (bio->bi_iter.bi_size == 0) { | |
983 | mutex_lock(&log->io_mutex); | |
984 | r5l_get_meta(log, 0); | |
985 | bio_list_add(&log->current_io->flush_barriers, bio); | |
986 | log->current_io->has_flush = 1; | |
987 | log->current_io->has_null_flush = 1; | |
988 | atomic_inc(&log->current_io->pending_stripe); | |
989 | r5l_submit_current_io(log); | |
990 | mutex_unlock(&log->io_mutex); | |
991 | return 0; | |
992 | } | |
828cbe98 | 993 | } |
828cbe98 SL |
994 | return -EAGAIN; |
995 | } | |
996 | ||
f6bed0ef SL |
997 | /* This will run after log space is reclaimed */ |
998 | static void r5l_run_no_space_stripes(struct r5l_log *log) | |
999 | { | |
1000 | struct stripe_head *sh; | |
1001 | ||
1002 | spin_lock(&log->no_space_stripes_lock); | |
1003 | while (!list_empty(&log->no_space_stripes)) { | |
1004 | sh = list_first_entry(&log->no_space_stripes, | |
1005 | struct stripe_head, log_list); | |
1006 | list_del_init(&sh->log_list); | |
1007 | set_bit(STRIPE_HANDLE, &sh->state); | |
1008 | raid5_release_stripe(sh); | |
1009 | } | |
1010 | spin_unlock(&log->no_space_stripes_lock); | |
1011 | } | |
1012 | ||
a39f7afd SL |
1013 | /* |
1014 | * calculate new last_checkpoint | |
1015 | * for write through mode, returns log->next_checkpoint | |
1016 | * for write back, returns log_start of first sh in stripe_in_journal_list | |
1017 | */ | |
1018 | static sector_t r5c_calculate_new_cp(struct r5conf *conf) | |
1019 | { | |
1020 | struct stripe_head *sh; | |
1021 | struct r5l_log *log = conf->log; | |
1022 | sector_t new_cp; | |
1023 | unsigned long flags; | |
1024 | ||
1025 | if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) | |
1026 | return log->next_checkpoint; | |
1027 | ||
1028 | spin_lock_irqsave(&log->stripe_in_journal_lock, flags); | |
1029 | if (list_empty(&conf->log->stripe_in_journal_list)) { | |
1030 | /* all stripes flushed */ | |
d3014e21 | 1031 | spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags); |
a39f7afd SL |
1032 | return log->next_checkpoint; |
1033 | } | |
1034 | sh = list_first_entry(&conf->log->stripe_in_journal_list, | |
1035 | struct stripe_head, r5c); | |
1036 | new_cp = sh->log_start; | |
1037 | spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags); | |
1038 | return new_cp; | |
1039 | } | |
1040 | ||
17036461 CH |
1041 | static sector_t r5l_reclaimable_space(struct r5l_log *log) |
1042 | { | |
a39f7afd SL |
1043 | struct r5conf *conf = log->rdev->mddev->private; |
1044 | ||
17036461 | 1045 | return r5l_ring_distance(log, log->last_checkpoint, |
a39f7afd | 1046 | r5c_calculate_new_cp(conf)); |
17036461 CH |
1047 | } |
1048 | ||
5036c390 CH |
1049 | static void r5l_run_no_mem_stripe(struct r5l_log *log) |
1050 | { | |
1051 | struct stripe_head *sh; | |
1052 | ||
1053 | assert_spin_locked(&log->io_list_lock); | |
1054 | ||
1055 | if (!list_empty(&log->no_mem_stripes)) { | |
1056 | sh = list_first_entry(&log->no_mem_stripes, | |
1057 | struct stripe_head, log_list); | |
1058 | list_del_init(&sh->log_list); | |
1059 | set_bit(STRIPE_HANDLE, &sh->state); | |
1060 | raid5_release_stripe(sh); | |
1061 | } | |
1062 | } | |
1063 | ||
04732f74 | 1064 | static bool r5l_complete_finished_ios(struct r5l_log *log) |
17036461 CH |
1065 | { |
1066 | struct r5l_io_unit *io, *next; | |
1067 | bool found = false; | |
1068 | ||
1069 | assert_spin_locked(&log->io_list_lock); | |
1070 | ||
04732f74 | 1071 | list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) { |
17036461 CH |
1072 | /* don't change list order */ |
1073 | if (io->state < IO_UNIT_STRIPE_END) | |
1074 | break; | |
1075 | ||
1076 | log->next_checkpoint = io->log_start; | |
17036461 CH |
1077 | |
1078 | list_del(&io->log_sibling); | |
5036c390 CH |
1079 | mempool_free(io, log->io_pool); |
1080 | r5l_run_no_mem_stripe(log); | |
17036461 CH |
1081 | |
1082 | found = true; | |
1083 | } | |
1084 | ||
1085 | return found; | |
1086 | } | |
1087 | ||
509ffec7 CH |
1088 | static void __r5l_stripe_write_finished(struct r5l_io_unit *io) |
1089 | { | |
1090 | struct r5l_log *log = io->log; | |
a39f7afd | 1091 | struct r5conf *conf = log->rdev->mddev->private; |
509ffec7 CH |
1092 | unsigned long flags; |
1093 | ||
1094 | spin_lock_irqsave(&log->io_list_lock, flags); | |
1095 | __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END); | |
17036461 | 1096 | |
04732f74 | 1097 | if (!r5l_complete_finished_ios(log)) { |
85f2f9a4 SL |
1098 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
1099 | return; | |
1100 | } | |
509ffec7 | 1101 | |
a39f7afd SL |
1102 | if (r5l_reclaimable_space(log) > log->max_free_space || |
1103 | test_bit(R5C_LOG_TIGHT, &conf->cache_state)) | |
509ffec7 CH |
1104 | r5l_wake_reclaim(log, 0); |
1105 | ||
509ffec7 CH |
1106 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
1107 | wake_up(&log->iounit_wait); | |
1108 | } | |
1109 | ||
0576b1c6 SL |
1110 | void r5l_stripe_write_finished(struct stripe_head *sh) |
1111 | { | |
1112 | struct r5l_io_unit *io; | |
1113 | ||
0576b1c6 | 1114 | io = sh->log_io; |
0576b1c6 SL |
1115 | sh->log_io = NULL; |
1116 | ||
509ffec7 CH |
1117 | if (io && atomic_dec_and_test(&io->pending_stripe)) |
1118 | __r5l_stripe_write_finished(io); | |
0576b1c6 SL |
1119 | } |
1120 | ||
a8c34f91 SL |
1121 | static void r5l_log_flush_endio(struct bio *bio) |
1122 | { | |
1123 | struct r5l_log *log = container_of(bio, struct r5l_log, | |
1124 | flush_bio); | |
1125 | unsigned long flags; | |
1126 | struct r5l_io_unit *io; | |
a8c34f91 | 1127 | |
6e74a9cf SL |
1128 | if (bio->bi_error) |
1129 | md_error(log->rdev->mddev, log->rdev); | |
1130 | ||
a8c34f91 | 1131 | spin_lock_irqsave(&log->io_list_lock, flags); |
d8858f43 CH |
1132 | list_for_each_entry(io, &log->flushing_ios, log_sibling) |
1133 | r5l_io_run_stripes(io); | |
04732f74 | 1134 | list_splice_tail_init(&log->flushing_ios, &log->finished_ios); |
a8c34f91 SL |
1135 | spin_unlock_irqrestore(&log->io_list_lock, flags); |
1136 | } | |
1137 | ||
0576b1c6 SL |
1138 | /* |
1139 | * Starting dispatch IO to raid. | |
1140 | * io_unit(meta) consists of a log. There is one situation we want to avoid. A | |
1141 | * broken meta in the middle of a log causes recovery can't find meta at the | |
1142 | * head of log. If operations require meta at the head persistent in log, we | |
1143 | * must make sure meta before it persistent in log too. A case is: | |
1144 | * | |
1145 | * stripe data/parity is in log, we start write stripe to raid disks. stripe | |
1146 | * data/parity must be persistent in log before we do the write to raid disks. | |
1147 | * | |
1148 | * The solution is we restrictly maintain io_unit list order. In this case, we | |
1149 | * only write stripes of an io_unit to raid disks till the io_unit is the first | |
1150 | * one whose data/parity is in log. | |
1151 | */ | |
1152 | void r5l_flush_stripe_to_raid(struct r5l_log *log) | |
1153 | { | |
a8c34f91 | 1154 | bool do_flush; |
56fef7c6 CH |
1155 | |
1156 | if (!log || !log->need_cache_flush) | |
0576b1c6 | 1157 | return; |
0576b1c6 SL |
1158 | |
1159 | spin_lock_irq(&log->io_list_lock); | |
a8c34f91 SL |
1160 | /* flush bio is running */ |
1161 | if (!list_empty(&log->flushing_ios)) { | |
1162 | spin_unlock_irq(&log->io_list_lock); | |
1163 | return; | |
0576b1c6 | 1164 | } |
a8c34f91 SL |
1165 | list_splice_tail_init(&log->io_end_ios, &log->flushing_ios); |
1166 | do_flush = !list_empty(&log->flushing_ios); | |
0576b1c6 | 1167 | spin_unlock_irq(&log->io_list_lock); |
a8c34f91 SL |
1168 | |
1169 | if (!do_flush) | |
1170 | return; | |
1171 | bio_reset(&log->flush_bio); | |
1172 | log->flush_bio.bi_bdev = log->rdev->bdev; | |
1173 | log->flush_bio.bi_end_io = r5l_log_flush_endio; | |
796a5cf0 | 1174 | bio_set_op_attrs(&log->flush_bio, REQ_OP_WRITE, WRITE_FLUSH); |
4e49ea4a | 1175 | submit_bio(&log->flush_bio); |
0576b1c6 SL |
1176 | } |
1177 | ||
0576b1c6 | 1178 | static void r5l_write_super(struct r5l_log *log, sector_t cp); |
4b482044 SL |
1179 | static void r5l_write_super_and_discard_space(struct r5l_log *log, |
1180 | sector_t end) | |
1181 | { | |
1182 | struct block_device *bdev = log->rdev->bdev; | |
1183 | struct mddev *mddev; | |
1184 | ||
1185 | r5l_write_super(log, end); | |
1186 | ||
1187 | if (!blk_queue_discard(bdev_get_queue(bdev))) | |
1188 | return; | |
1189 | ||
1190 | mddev = log->rdev->mddev; | |
1191 | /* | |
8e018c21 SL |
1192 | * Discard could zero data, so before discard we must make sure |
1193 | * superblock is updated to new log tail. Updating superblock (either | |
1194 | * directly call md_update_sb() or depend on md thread) must hold | |
1195 | * reconfig mutex. On the other hand, raid5_quiesce is called with | |
1196 | * reconfig_mutex hold. The first step of raid5_quiesce() is waitting | |
1197 | * for all IO finish, hence waitting for reclaim thread, while reclaim | |
1198 | * thread is calling this function and waitting for reconfig mutex. So | |
1199 | * there is a deadlock. We workaround this issue with a trylock. | |
1200 | * FIXME: we could miss discard if we can't take reconfig mutex | |
4b482044 | 1201 | */ |
8e018c21 SL |
1202 | set_mask_bits(&mddev->flags, 0, |
1203 | BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING)); | |
1204 | if (!mddev_trylock(mddev)) | |
1205 | return; | |
1206 | md_update_sb(mddev, 1); | |
1207 | mddev_unlock(mddev); | |
4b482044 | 1208 | |
6e74a9cf | 1209 | /* discard IO error really doesn't matter, ignore it */ |
4b482044 SL |
1210 | if (log->last_checkpoint < end) { |
1211 | blkdev_issue_discard(bdev, | |
1212 | log->last_checkpoint + log->rdev->data_offset, | |
1213 | end - log->last_checkpoint, GFP_NOIO, 0); | |
1214 | } else { | |
1215 | blkdev_issue_discard(bdev, | |
1216 | log->last_checkpoint + log->rdev->data_offset, | |
1217 | log->device_size - log->last_checkpoint, | |
1218 | GFP_NOIO, 0); | |
1219 | blkdev_issue_discard(bdev, log->rdev->data_offset, end, | |
1220 | GFP_NOIO, 0); | |
1221 | } | |
1222 | } | |
1223 | ||
a39f7afd SL |
1224 | /* |
1225 | * r5c_flush_stripe moves stripe from cached list to handle_list. When called, | |
1226 | * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes. | |
1227 | * | |
1228 | * must hold conf->device_lock | |
1229 | */ | |
1230 | static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh) | |
1231 | { | |
1232 | BUG_ON(list_empty(&sh->lru)); | |
1233 | BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); | |
1234 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); | |
1235 | ||
1236 | /* | |
1237 | * The stripe is not ON_RELEASE_LIST, so it is safe to call | |
1238 | * raid5_release_stripe() while holding conf->device_lock | |
1239 | */ | |
1240 | BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state)); | |
1241 | assert_spin_locked(&conf->device_lock); | |
1242 | ||
1243 | list_del_init(&sh->lru); | |
1244 | atomic_inc(&sh->count); | |
1245 | ||
1246 | set_bit(STRIPE_HANDLE, &sh->state); | |
1247 | atomic_inc(&conf->active_stripes); | |
1248 | r5c_make_stripe_write_out(sh); | |
1249 | ||
a39f7afd SL |
1250 | raid5_release_stripe(sh); |
1251 | } | |
1252 | ||
1253 | /* | |
1254 | * if num == 0, flush all full stripes | |
1255 | * if num > 0, flush all full stripes. If less than num full stripes are | |
1256 | * flushed, flush some partial stripes until totally num stripes are | |
1257 | * flushed or there is no more cached stripes. | |
1258 | */ | |
1259 | void r5c_flush_cache(struct r5conf *conf, int num) | |
1260 | { | |
1261 | int count; | |
1262 | struct stripe_head *sh, *next; | |
1263 | ||
1264 | assert_spin_locked(&conf->device_lock); | |
1265 | if (!conf->log) | |
1266 | return; | |
1267 | ||
1268 | count = 0; | |
1269 | list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) { | |
1270 | r5c_flush_stripe(conf, sh); | |
1271 | count++; | |
1272 | } | |
1273 | ||
1274 | if (count >= num) | |
1275 | return; | |
1276 | list_for_each_entry_safe(sh, next, | |
1277 | &conf->r5c_partial_stripe_list, lru) { | |
1278 | r5c_flush_stripe(conf, sh); | |
1279 | if (++count >= num) | |
1280 | break; | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | static void r5c_do_reclaim(struct r5conf *conf) | |
1285 | { | |
1286 | struct r5l_log *log = conf->log; | |
1287 | struct stripe_head *sh; | |
1288 | int count = 0; | |
1289 | unsigned long flags; | |
1290 | int total_cached; | |
1291 | int stripes_to_flush; | |
1292 | ||
1293 | if (!r5c_is_writeback(log)) | |
1294 | return; | |
1295 | ||
1296 | total_cached = atomic_read(&conf->r5c_cached_partial_stripes) + | |
1297 | atomic_read(&conf->r5c_cached_full_stripes); | |
1298 | ||
1299 | if (total_cached > conf->min_nr_stripes * 3 / 4 || | |
1300 | atomic_read(&conf->empty_inactive_list_nr) > 0) | |
1301 | /* | |
1302 | * if stripe cache pressure high, flush all full stripes and | |
1303 | * some partial stripes | |
1304 | */ | |
1305 | stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP; | |
1306 | else if (total_cached > conf->min_nr_stripes * 1 / 2 || | |
1307 | atomic_read(&conf->r5c_cached_full_stripes) > | |
1308 | R5C_FULL_STRIPE_FLUSH_BATCH) | |
1309 | /* | |
1310 | * if stripe cache pressure moderate, or if there is many full | |
1311 | * stripes,flush all full stripes | |
1312 | */ | |
1313 | stripes_to_flush = 0; | |
1314 | else | |
1315 | /* no need to flush */ | |
1316 | stripes_to_flush = -1; | |
1317 | ||
1318 | if (stripes_to_flush >= 0) { | |
1319 | spin_lock_irqsave(&conf->device_lock, flags); | |
1320 | r5c_flush_cache(conf, stripes_to_flush); | |
1321 | spin_unlock_irqrestore(&conf->device_lock, flags); | |
1322 | } | |
1323 | ||
1324 | /* if log space is tight, flush stripes on stripe_in_journal_list */ | |
1325 | if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) { | |
1326 | spin_lock_irqsave(&log->stripe_in_journal_lock, flags); | |
1327 | spin_lock(&conf->device_lock); | |
1328 | list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) { | |
1329 | /* | |
1330 | * stripes on stripe_in_journal_list could be in any | |
1331 | * state of the stripe_cache state machine. In this | |
1332 | * case, we only want to flush stripe on | |
1333 | * r5c_cached_full/partial_stripes. The following | |
1334 | * condition makes sure the stripe is on one of the | |
1335 | * two lists. | |
1336 | */ | |
1337 | if (!list_empty(&sh->lru) && | |
1338 | !test_bit(STRIPE_HANDLE, &sh->state) && | |
1339 | atomic_read(&sh->count) == 0) { | |
1340 | r5c_flush_stripe(conf, sh); | |
1341 | } | |
1342 | if (count++ >= R5C_RECLAIM_STRIPE_GROUP) | |
1343 | break; | |
1344 | } | |
1345 | spin_unlock(&conf->device_lock); | |
1346 | spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags); | |
1347 | } | |
1348 | md_wakeup_thread(conf->mddev->thread); | |
1349 | } | |
1350 | ||
0576b1c6 SL |
1351 | static void r5l_do_reclaim(struct r5l_log *log) |
1352 | { | |
a39f7afd | 1353 | struct r5conf *conf = log->rdev->mddev->private; |
0576b1c6 | 1354 | sector_t reclaim_target = xchg(&log->reclaim_target, 0); |
17036461 CH |
1355 | sector_t reclaimable; |
1356 | sector_t next_checkpoint; | |
a39f7afd | 1357 | bool write_super; |
0576b1c6 SL |
1358 | |
1359 | spin_lock_irq(&log->io_list_lock); | |
a39f7afd SL |
1360 | write_super = r5l_reclaimable_space(log) > log->max_free_space || |
1361 | reclaim_target != 0 || !list_empty(&log->no_space_stripes); | |
0576b1c6 SL |
1362 | /* |
1363 | * move proper io_unit to reclaim list. We should not change the order. | |
1364 | * reclaimable/unreclaimable io_unit can be mixed in the list, we | |
1365 | * shouldn't reuse space of an unreclaimable io_unit | |
1366 | */ | |
1367 | while (1) { | |
17036461 CH |
1368 | reclaimable = r5l_reclaimable_space(log); |
1369 | if (reclaimable >= reclaim_target || | |
0576b1c6 SL |
1370 | (list_empty(&log->running_ios) && |
1371 | list_empty(&log->io_end_ios) && | |
a8c34f91 | 1372 | list_empty(&log->flushing_ios) && |
04732f74 | 1373 | list_empty(&log->finished_ios))) |
0576b1c6 SL |
1374 | break; |
1375 | ||
17036461 CH |
1376 | md_wakeup_thread(log->rdev->mddev->thread); |
1377 | wait_event_lock_irq(log->iounit_wait, | |
1378 | r5l_reclaimable_space(log) > reclaimable, | |
1379 | log->io_list_lock); | |
0576b1c6 | 1380 | } |
17036461 | 1381 | |
a39f7afd | 1382 | next_checkpoint = r5c_calculate_new_cp(conf); |
0576b1c6 SL |
1383 | spin_unlock_irq(&log->io_list_lock); |
1384 | ||
17036461 | 1385 | BUG_ON(reclaimable < 0); |
a39f7afd SL |
1386 | |
1387 | if (reclaimable == 0 || !write_super) | |
0576b1c6 SL |
1388 | return; |
1389 | ||
0576b1c6 SL |
1390 | /* |
1391 | * write_super will flush cache of each raid disk. We must write super | |
1392 | * here, because the log area might be reused soon and we don't want to | |
1393 | * confuse recovery | |
1394 | */ | |
4b482044 | 1395 | r5l_write_super_and_discard_space(log, next_checkpoint); |
0576b1c6 SL |
1396 | |
1397 | mutex_lock(&log->io_mutex); | |
17036461 | 1398 | log->last_checkpoint = next_checkpoint; |
a39f7afd | 1399 | r5c_update_log_state(log); |
0576b1c6 | 1400 | mutex_unlock(&log->io_mutex); |
0576b1c6 | 1401 | |
17036461 | 1402 | r5l_run_no_space_stripes(log); |
0576b1c6 SL |
1403 | } |
1404 | ||
1405 | static void r5l_reclaim_thread(struct md_thread *thread) | |
1406 | { | |
1407 | struct mddev *mddev = thread->mddev; | |
1408 | struct r5conf *conf = mddev->private; | |
1409 | struct r5l_log *log = conf->log; | |
1410 | ||
1411 | if (!log) | |
1412 | return; | |
a39f7afd | 1413 | r5c_do_reclaim(conf); |
0576b1c6 SL |
1414 | r5l_do_reclaim(log); |
1415 | } | |
1416 | ||
a39f7afd | 1417 | void r5l_wake_reclaim(struct r5l_log *log, sector_t space) |
f6bed0ef | 1418 | { |
0576b1c6 SL |
1419 | unsigned long target; |
1420 | unsigned long new = (unsigned long)space; /* overflow in theory */ | |
1421 | ||
a39f7afd SL |
1422 | if (!log) |
1423 | return; | |
0576b1c6 SL |
1424 | do { |
1425 | target = log->reclaim_target; | |
1426 | if (new < target) | |
1427 | return; | |
1428 | } while (cmpxchg(&log->reclaim_target, target, new) != target); | |
1429 | md_wakeup_thread(log->reclaim_thread); | |
f6bed0ef SL |
1430 | } |
1431 | ||
e6c033f7 SL |
1432 | void r5l_quiesce(struct r5l_log *log, int state) |
1433 | { | |
4b482044 | 1434 | struct mddev *mddev; |
e6c033f7 SL |
1435 | if (!log || state == 2) |
1436 | return; | |
ce1ccd07 SL |
1437 | if (state == 0) |
1438 | kthread_unpark(log->reclaim_thread->tsk); | |
1439 | else if (state == 1) { | |
4b482044 SL |
1440 | /* make sure r5l_write_super_and_discard_space exits */ |
1441 | mddev = log->rdev->mddev; | |
1442 | wake_up(&mddev->sb_wait); | |
ce1ccd07 | 1443 | kthread_park(log->reclaim_thread->tsk); |
a39f7afd | 1444 | r5l_wake_reclaim(log, MaxSector); |
e6c033f7 SL |
1445 | r5l_do_reclaim(log); |
1446 | } | |
1447 | } | |
1448 | ||
6e74a9cf SL |
1449 | bool r5l_log_disk_error(struct r5conf *conf) |
1450 | { | |
f6b6ec5c SL |
1451 | struct r5l_log *log; |
1452 | bool ret; | |
7dde2ad3 | 1453 | /* don't allow write if journal disk is missing */ |
f6b6ec5c SL |
1454 | rcu_read_lock(); |
1455 | log = rcu_dereference(conf->log); | |
1456 | ||
1457 | if (!log) | |
1458 | ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags); | |
1459 | else | |
1460 | ret = test_bit(Faulty, &log->rdev->flags); | |
1461 | rcu_read_unlock(); | |
1462 | return ret; | |
6e74a9cf SL |
1463 | } |
1464 | ||
355810d1 SL |
1465 | struct r5l_recovery_ctx { |
1466 | struct page *meta_page; /* current meta */ | |
1467 | sector_t meta_total_blocks; /* total size of current meta and data */ | |
1468 | sector_t pos; /* recovery position */ | |
1469 | u64 seq; /* recovery position seq */ | |
b4c625c6 SL |
1470 | int data_parity_stripes; /* number of data_parity stripes */ |
1471 | int data_only_stripes; /* number of data_only stripes */ | |
1472 | struct list_head cached_list; | |
355810d1 SL |
1473 | }; |
1474 | ||
9ed988f5 SL |
1475 | static int r5l_recovery_read_meta_block(struct r5l_log *log, |
1476 | struct r5l_recovery_ctx *ctx) | |
355810d1 SL |
1477 | { |
1478 | struct page *page = ctx->meta_page; | |
1479 | struct r5l_meta_block *mb; | |
1480 | u32 crc, stored_crc; | |
1481 | ||
796a5cf0 MC |
1482 | if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, REQ_OP_READ, 0, |
1483 | false)) | |
355810d1 SL |
1484 | return -EIO; |
1485 | ||
1486 | mb = page_address(page); | |
1487 | stored_crc = le32_to_cpu(mb->checksum); | |
1488 | mb->checksum = 0; | |
1489 | ||
1490 | if (le32_to_cpu(mb->magic) != R5LOG_MAGIC || | |
1491 | le64_to_cpu(mb->seq) != ctx->seq || | |
1492 | mb->version != R5LOG_VERSION || | |
1493 | le64_to_cpu(mb->position) != ctx->pos) | |
1494 | return -EINVAL; | |
1495 | ||
5cb2fbd6 | 1496 | crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE); |
355810d1 SL |
1497 | if (stored_crc != crc) |
1498 | return -EINVAL; | |
1499 | ||
1500 | if (le32_to_cpu(mb->meta_size) > PAGE_SIZE) | |
1501 | return -EINVAL; | |
1502 | ||
1503 | ctx->meta_total_blocks = BLOCK_SECTORS; | |
1504 | ||
1505 | return 0; | |
1506 | } | |
1507 | ||
9ed988f5 SL |
1508 | static void |
1509 | r5l_recovery_create_empty_meta_block(struct r5l_log *log, | |
1510 | struct page *page, | |
1511 | sector_t pos, u64 seq) | |
355810d1 | 1512 | { |
355810d1 SL |
1513 | struct r5l_meta_block *mb; |
1514 | u32 crc; | |
1515 | ||
355810d1 | 1516 | mb = page_address(page); |
9ed988f5 | 1517 | clear_page(mb); |
355810d1 SL |
1518 | mb->magic = cpu_to_le32(R5LOG_MAGIC); |
1519 | mb->version = R5LOG_VERSION; | |
1520 | mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block)); | |
1521 | mb->seq = cpu_to_le64(seq); | |
1522 | mb->position = cpu_to_le64(pos); | |
5cb2fbd6 | 1523 | crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE); |
355810d1 | 1524 | mb->checksum = cpu_to_le32(crc); |
9ed988f5 | 1525 | } |
355810d1 | 1526 | |
9ed988f5 SL |
1527 | static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos, |
1528 | u64 seq) | |
1529 | { | |
1530 | struct page *page; | |
1531 | ||
1532 | page = alloc_page(GFP_KERNEL); | |
1533 | if (!page) | |
1534 | return -ENOMEM; | |
1535 | r5l_recovery_create_empty_meta_block(log, page, pos, seq); | |
796a5cf0 MC |
1536 | if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE, |
1537 | WRITE_FUA, false)) { | |
355810d1 SL |
1538 | __free_page(page); |
1539 | return -EIO; | |
1540 | } | |
1541 | __free_page(page); | |
1542 | return 0; | |
1543 | } | |
1544 | ||
b4c625c6 SL |
1545 | /* |
1546 | * r5l_recovery_load_data and r5l_recovery_load_parity uses flag R5_Wantwrite | |
1547 | * to mark valid (potentially not flushed) data in the journal. | |
1548 | * | |
1549 | * We already verified checksum in r5l_recovery_verify_data_checksum_for_mb, | |
1550 | * so there should not be any mismatch here. | |
1551 | */ | |
1552 | static void r5l_recovery_load_data(struct r5l_log *log, | |
1553 | struct stripe_head *sh, | |
1554 | struct r5l_recovery_ctx *ctx, | |
1555 | struct r5l_payload_data_parity *payload, | |
1556 | sector_t log_offset) | |
1557 | { | |
1558 | struct mddev *mddev = log->rdev->mddev; | |
1559 | struct r5conf *conf = mddev->private; | |
1560 | int dd_idx; | |
1561 | ||
1562 | raid5_compute_sector(conf, | |
1563 | le64_to_cpu(payload->location), 0, | |
1564 | &dd_idx, sh); | |
1565 | sync_page_io(log->rdev, log_offset, PAGE_SIZE, | |
1566 | sh->dev[dd_idx].page, REQ_OP_READ, 0, false); | |
1567 | sh->dev[dd_idx].log_checksum = | |
1568 | le32_to_cpu(payload->checksum[0]); | |
1569 | ctx->meta_total_blocks += BLOCK_SECTORS; | |
1570 | ||
1571 | set_bit(R5_Wantwrite, &sh->dev[dd_idx].flags); | |
1572 | set_bit(STRIPE_R5C_CACHING, &sh->state); | |
1573 | } | |
1574 | ||
1575 | static void r5l_recovery_load_parity(struct r5l_log *log, | |
1576 | struct stripe_head *sh, | |
1577 | struct r5l_recovery_ctx *ctx, | |
1578 | struct r5l_payload_data_parity *payload, | |
1579 | sector_t log_offset) | |
1580 | { | |
1581 | struct mddev *mddev = log->rdev->mddev; | |
1582 | struct r5conf *conf = mddev->private; | |
1583 | ||
1584 | ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded; | |
1585 | sync_page_io(log->rdev, log_offset, PAGE_SIZE, | |
1586 | sh->dev[sh->pd_idx].page, REQ_OP_READ, 0, false); | |
1587 | sh->dev[sh->pd_idx].log_checksum = | |
1588 | le32_to_cpu(payload->checksum[0]); | |
1589 | set_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags); | |
1590 | ||
1591 | if (sh->qd_idx >= 0) { | |
1592 | sync_page_io(log->rdev, | |
1593 | r5l_ring_add(log, log_offset, BLOCK_SECTORS), | |
1594 | PAGE_SIZE, sh->dev[sh->qd_idx].page, | |
1595 | REQ_OP_READ, 0, false); | |
1596 | sh->dev[sh->qd_idx].log_checksum = | |
1597 | le32_to_cpu(payload->checksum[1]); | |
1598 | set_bit(R5_Wantwrite, &sh->dev[sh->qd_idx].flags); | |
1599 | } | |
1600 | clear_bit(STRIPE_R5C_CACHING, &sh->state); | |
1601 | } | |
1602 | ||
1603 | static void r5l_recovery_reset_stripe(struct stripe_head *sh) | |
1604 | { | |
1605 | int i; | |
1606 | ||
1607 | sh->state = 0; | |
1608 | sh->log_start = MaxSector; | |
1609 | for (i = sh->disks; i--; ) | |
1610 | sh->dev[i].flags = 0; | |
1611 | } | |
1612 | ||
1613 | static void | |
1614 | r5l_recovery_replay_one_stripe(struct r5conf *conf, | |
1615 | struct stripe_head *sh, | |
1616 | struct r5l_recovery_ctx *ctx) | |
1617 | { | |
1618 | struct md_rdev *rdev, *rrdev; | |
1619 | int disk_index; | |
1620 | int data_count = 0; | |
1621 | ||
1622 | for (disk_index = 0; disk_index < sh->disks; disk_index++) { | |
1623 | if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags)) | |
1624 | continue; | |
1625 | if (disk_index == sh->qd_idx || disk_index == sh->pd_idx) | |
1626 | continue; | |
1627 | data_count++; | |
1628 | } | |
1629 | ||
1630 | /* | |
1631 | * stripes that only have parity must have been flushed | |
1632 | * before the crash that we are now recovering from, so | |
1633 | * there is nothing more to recovery. | |
1634 | */ | |
1635 | if (data_count == 0) | |
1636 | goto out; | |
1637 | ||
1638 | for (disk_index = 0; disk_index < sh->disks; disk_index++) { | |
1639 | if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags)) | |
1640 | continue; | |
1641 | ||
1642 | /* in case device is broken */ | |
1643 | rcu_read_lock(); | |
1644 | rdev = rcu_dereference(conf->disks[disk_index].rdev); | |
1645 | if (rdev) { | |
1646 | atomic_inc(&rdev->nr_pending); | |
1647 | rcu_read_unlock(); | |
1648 | sync_page_io(rdev, sh->sector, PAGE_SIZE, | |
1649 | sh->dev[disk_index].page, REQ_OP_WRITE, 0, | |
1650 | false); | |
1651 | rdev_dec_pending(rdev, rdev->mddev); | |
1652 | rcu_read_lock(); | |
1653 | } | |
1654 | rrdev = rcu_dereference(conf->disks[disk_index].replacement); | |
1655 | if (rrdev) { | |
1656 | atomic_inc(&rrdev->nr_pending); | |
1657 | rcu_read_unlock(); | |
1658 | sync_page_io(rrdev, sh->sector, PAGE_SIZE, | |
1659 | sh->dev[disk_index].page, REQ_OP_WRITE, 0, | |
1660 | false); | |
1661 | rdev_dec_pending(rrdev, rrdev->mddev); | |
1662 | rcu_read_lock(); | |
1663 | } | |
1664 | rcu_read_unlock(); | |
1665 | } | |
1666 | ctx->data_parity_stripes++; | |
1667 | out: | |
1668 | r5l_recovery_reset_stripe(sh); | |
1669 | } | |
1670 | ||
1671 | static struct stripe_head * | |
1672 | r5c_recovery_alloc_stripe(struct r5conf *conf, | |
b4c625c6 SL |
1673 | sector_t stripe_sect, |
1674 | sector_t log_start) | |
1675 | { | |
1676 | struct stripe_head *sh; | |
1677 | ||
1678 | sh = raid5_get_active_stripe(conf, stripe_sect, 0, 1, 0); | |
1679 | if (!sh) | |
1680 | return NULL; /* no more stripe available */ | |
1681 | ||
1682 | r5l_recovery_reset_stripe(sh); | |
1683 | sh->log_start = log_start; | |
1684 | ||
1685 | return sh; | |
1686 | } | |
1687 | ||
1688 | static struct stripe_head * | |
1689 | r5c_recovery_lookup_stripe(struct list_head *list, sector_t sect) | |
1690 | { | |
1691 | struct stripe_head *sh; | |
1692 | ||
1693 | list_for_each_entry(sh, list, lru) | |
1694 | if (sh->sector == sect) | |
1695 | return sh; | |
1696 | return NULL; | |
1697 | } | |
1698 | ||
1699 | static void | |
1700 | r5c_recovery_drop_stripes(struct list_head *cached_stripe_list, | |
1701 | struct r5l_recovery_ctx *ctx) | |
1702 | { | |
1703 | struct stripe_head *sh, *next; | |
1704 | ||
1705 | list_for_each_entry_safe(sh, next, cached_stripe_list, lru) { | |
1706 | r5l_recovery_reset_stripe(sh); | |
1707 | list_del_init(&sh->lru); | |
1708 | raid5_release_stripe(sh); | |
1709 | } | |
1710 | } | |
1711 | ||
1712 | static void | |
1713 | r5c_recovery_replay_stripes(struct list_head *cached_stripe_list, | |
1714 | struct r5l_recovery_ctx *ctx) | |
1715 | { | |
1716 | struct stripe_head *sh, *next; | |
1717 | ||
1718 | list_for_each_entry_safe(sh, next, cached_stripe_list, lru) | |
1719 | if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) { | |
1720 | r5l_recovery_replay_one_stripe(sh->raid_conf, sh, ctx); | |
1721 | list_del_init(&sh->lru); | |
1722 | raid5_release_stripe(sh); | |
1723 | } | |
1724 | } | |
1725 | ||
1726 | /* if matches return 0; otherwise return -EINVAL */ | |
1727 | static int | |
1728 | r5l_recovery_verify_data_checksum(struct r5l_log *log, struct page *page, | |
1729 | sector_t log_offset, __le32 log_checksum) | |
1730 | { | |
1731 | void *addr; | |
1732 | u32 checksum; | |
1733 | ||
1734 | sync_page_io(log->rdev, log_offset, PAGE_SIZE, | |
1735 | page, REQ_OP_READ, 0, false); | |
1736 | addr = kmap_atomic(page); | |
1737 | checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE); | |
1738 | kunmap_atomic(addr); | |
1739 | return (le32_to_cpu(log_checksum) == checksum) ? 0 : -EINVAL; | |
1740 | } | |
1741 | ||
1742 | /* | |
1743 | * before loading data to stripe cache, we need verify checksum for all data, | |
1744 | * if there is mismatch for any data page, we drop all data in the mata block | |
1745 | */ | |
1746 | static int | |
1747 | r5l_recovery_verify_data_checksum_for_mb(struct r5l_log *log, | |
1748 | struct r5l_recovery_ctx *ctx) | |
1749 | { | |
1750 | struct mddev *mddev = log->rdev->mddev; | |
1751 | struct r5conf *conf = mddev->private; | |
1752 | struct r5l_meta_block *mb = page_address(ctx->meta_page); | |
1753 | sector_t mb_offset = sizeof(struct r5l_meta_block); | |
1754 | sector_t log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS); | |
1755 | struct page *page; | |
1756 | struct r5l_payload_data_parity *payload; | |
1757 | ||
1758 | page = alloc_page(GFP_KERNEL); | |
1759 | if (!page) | |
1760 | return -ENOMEM; | |
1761 | ||
1762 | while (mb_offset < le32_to_cpu(mb->meta_size)) { | |
1763 | payload = (void *)mb + mb_offset; | |
1764 | ||
1765 | if (payload->header.type == R5LOG_PAYLOAD_DATA) { | |
1766 | if (r5l_recovery_verify_data_checksum( | |
1767 | log, page, log_offset, | |
1768 | payload->checksum[0]) < 0) | |
1769 | goto mismatch; | |
1770 | } else if (payload->header.type == R5LOG_PAYLOAD_PARITY) { | |
1771 | if (r5l_recovery_verify_data_checksum( | |
1772 | log, page, log_offset, | |
1773 | payload->checksum[0]) < 0) | |
1774 | goto mismatch; | |
1775 | if (conf->max_degraded == 2 && /* q for RAID 6 */ | |
1776 | r5l_recovery_verify_data_checksum( | |
1777 | log, page, | |
1778 | r5l_ring_add(log, log_offset, | |
1779 | BLOCK_SECTORS), | |
1780 | payload->checksum[1]) < 0) | |
1781 | goto mismatch; | |
1782 | } else /* not R5LOG_PAYLOAD_DATA or R5LOG_PAYLOAD_PARITY */ | |
1783 | goto mismatch; | |
1784 | ||
1785 | log_offset = r5l_ring_add(log, log_offset, | |
1786 | le32_to_cpu(payload->size)); | |
1787 | ||
1788 | mb_offset += sizeof(struct r5l_payload_data_parity) + | |
1789 | sizeof(__le32) * | |
1790 | (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9)); | |
1791 | } | |
1792 | ||
1793 | put_page(page); | |
1794 | return 0; | |
1795 | ||
1796 | mismatch: | |
1797 | put_page(page); | |
1798 | return -EINVAL; | |
1799 | } | |
1800 | ||
1801 | /* | |
1802 | * Analyze all data/parity pages in one meta block | |
1803 | * Returns: | |
1804 | * 0 for success | |
1805 | * -EINVAL for unknown playload type | |
1806 | * -EAGAIN for checksum mismatch of data page | |
1807 | * -ENOMEM for run out of memory (alloc_page failed or run out of stripes) | |
1808 | */ | |
1809 | static int | |
1810 | r5c_recovery_analyze_meta_block(struct r5l_log *log, | |
1811 | struct r5l_recovery_ctx *ctx, | |
1812 | struct list_head *cached_stripe_list) | |
1813 | { | |
1814 | struct mddev *mddev = log->rdev->mddev; | |
1815 | struct r5conf *conf = mddev->private; | |
1816 | struct r5l_meta_block *mb; | |
1817 | struct r5l_payload_data_parity *payload; | |
1818 | int mb_offset; | |
1819 | sector_t log_offset; | |
1820 | sector_t stripe_sect; | |
1821 | struct stripe_head *sh; | |
1822 | int ret; | |
1823 | ||
1824 | /* | |
1825 | * for mismatch in data blocks, we will drop all data in this mb, but | |
1826 | * we will still read next mb for other data with FLUSH flag, as | |
1827 | * io_unit could finish out of order. | |
1828 | */ | |
1829 | ret = r5l_recovery_verify_data_checksum_for_mb(log, ctx); | |
1830 | if (ret == -EINVAL) | |
1831 | return -EAGAIN; | |
1832 | else if (ret) | |
1833 | return ret; /* -ENOMEM duo to alloc_page() failed */ | |
1834 | ||
1835 | mb = page_address(ctx->meta_page); | |
1836 | mb_offset = sizeof(struct r5l_meta_block); | |
1837 | log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS); | |
1838 | ||
1839 | while (mb_offset < le32_to_cpu(mb->meta_size)) { | |
1840 | int dd; | |
1841 | ||
1842 | payload = (void *)mb + mb_offset; | |
1843 | stripe_sect = (payload->header.type == R5LOG_PAYLOAD_DATA) ? | |
1844 | raid5_compute_sector( | |
1845 | conf, le64_to_cpu(payload->location), 0, &dd, | |
1846 | NULL) | |
1847 | : le64_to_cpu(payload->location); | |
1848 | ||
1849 | sh = r5c_recovery_lookup_stripe(cached_stripe_list, | |
1850 | stripe_sect); | |
1851 | ||
1852 | if (!sh) { | |
9b69173e | 1853 | sh = r5c_recovery_alloc_stripe(conf, stripe_sect, ctx->pos); |
b4c625c6 SL |
1854 | /* |
1855 | * cannot get stripe from raid5_get_active_stripe | |
1856 | * try replay some stripes | |
1857 | */ | |
1858 | if (!sh) { | |
1859 | r5c_recovery_replay_stripes( | |
1860 | cached_stripe_list, ctx); | |
1861 | sh = r5c_recovery_alloc_stripe( | |
9b69173e | 1862 | conf, stripe_sect, ctx->pos); |
b4c625c6 SL |
1863 | } |
1864 | if (!sh) { | |
1865 | pr_debug("md/raid:%s: Increasing stripe cache size to %d to recovery data on journal.\n", | |
1866 | mdname(mddev), | |
1867 | conf->min_nr_stripes * 2); | |
1868 | raid5_set_cache_size(mddev, | |
1869 | conf->min_nr_stripes * 2); | |
1870 | sh = r5c_recovery_alloc_stripe( | |
9b69173e | 1871 | conf, stripe_sect, ctx->pos); |
b4c625c6 SL |
1872 | } |
1873 | if (!sh) { | |
1874 | pr_err("md/raid:%s: Cannot get enough stripes due to memory pressure. Recovery failed.\n", | |
1875 | mdname(mddev)); | |
1876 | return -ENOMEM; | |
1877 | } | |
1878 | list_add_tail(&sh->lru, cached_stripe_list); | |
1879 | } | |
1880 | ||
1881 | if (payload->header.type == R5LOG_PAYLOAD_DATA) { | |
f7b7bee7 ZL |
1882 | if (!test_bit(STRIPE_R5C_CACHING, &sh->state) && |
1883 | test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags)) { | |
b4c625c6 | 1884 | r5l_recovery_replay_one_stripe(conf, sh, ctx); |
b4c625c6 SL |
1885 | sh->log_start = ctx->pos; |
1886 | list_move_tail(&sh->lru, cached_stripe_list); | |
1887 | } | |
1888 | r5l_recovery_load_data(log, sh, ctx, payload, | |
1889 | log_offset); | |
1890 | } else if (payload->header.type == R5LOG_PAYLOAD_PARITY) | |
1891 | r5l_recovery_load_parity(log, sh, ctx, payload, | |
1892 | log_offset); | |
1893 | else | |
1894 | return -EINVAL; | |
1895 | ||
1896 | log_offset = r5l_ring_add(log, log_offset, | |
1897 | le32_to_cpu(payload->size)); | |
1898 | ||
1899 | mb_offset += sizeof(struct r5l_payload_data_parity) + | |
1900 | sizeof(__le32) * | |
1901 | (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9)); | |
1902 | } | |
1903 | ||
1904 | return 0; | |
1905 | } | |
1906 | ||
1907 | /* | |
1908 | * Load the stripe into cache. The stripe will be written out later by | |
1909 | * the stripe cache state machine. | |
1910 | */ | |
1911 | static void r5c_recovery_load_one_stripe(struct r5l_log *log, | |
1912 | struct stripe_head *sh) | |
1913 | { | |
b4c625c6 SL |
1914 | struct r5dev *dev; |
1915 | int i; | |
1916 | ||
1917 | for (i = sh->disks; i--; ) { | |
1918 | dev = sh->dev + i; | |
1919 | if (test_and_clear_bit(R5_Wantwrite, &dev->flags)) { | |
1920 | set_bit(R5_InJournal, &dev->flags); | |
1921 | set_bit(R5_UPTODATE, &dev->flags); | |
1922 | } | |
1923 | } | |
b4c625c6 | 1924 | list_add_tail(&sh->r5c, &log->stripe_in_journal_list); |
462eb7d8 | 1925 | atomic_inc(&log->stripe_in_journal_count); |
b4c625c6 SL |
1926 | } |
1927 | ||
1928 | /* | |
1929 | * Scan through the log for all to-be-flushed data | |
1930 | * | |
1931 | * For stripes with data and parity, namely Data-Parity stripe | |
1932 | * (STRIPE_R5C_CACHING == 0), we simply replay all the writes. | |
1933 | * | |
1934 | * For stripes with only data, namely Data-Only stripe | |
1935 | * (STRIPE_R5C_CACHING == 1), we load them to stripe cache state machine. | |
1936 | * | |
1937 | * For a stripe, if we see data after parity, we should discard all previous | |
1938 | * data and parity for this stripe, as these data are already flushed to | |
1939 | * the array. | |
1940 | * | |
1941 | * At the end of the scan, we return the new journal_tail, which points to | |
1942 | * first data-only stripe on the journal device, or next invalid meta block. | |
1943 | */ | |
1944 | static int r5c_recovery_flush_log(struct r5l_log *log, | |
1945 | struct r5l_recovery_ctx *ctx) | |
1946 | { | |
bc8f167f | 1947 | struct stripe_head *sh; |
b4c625c6 SL |
1948 | int ret = 0; |
1949 | ||
1950 | /* scan through the log */ | |
1951 | while (1) { | |
1952 | if (r5l_recovery_read_meta_block(log, ctx)) | |
1953 | break; | |
1954 | ||
1955 | ret = r5c_recovery_analyze_meta_block(log, ctx, | |
1956 | &ctx->cached_list); | |
1957 | /* | |
1958 | * -EAGAIN means mismatch in data block, in this case, we still | |
1959 | * try scan the next metablock | |
1960 | */ | |
1961 | if (ret && ret != -EAGAIN) | |
1962 | break; /* ret == -EINVAL or -ENOMEM */ | |
1963 | ctx->seq++; | |
1964 | ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks); | |
1965 | } | |
1966 | ||
1967 | if (ret == -ENOMEM) { | |
1968 | r5c_recovery_drop_stripes(&ctx->cached_list, ctx); | |
1969 | return ret; | |
1970 | } | |
1971 | ||
1972 | /* replay data-parity stripes */ | |
1973 | r5c_recovery_replay_stripes(&ctx->cached_list, ctx); | |
1974 | ||
1975 | /* load data-only stripes to stripe cache */ | |
bc8f167f | 1976 | list_for_each_entry(sh, &ctx->cached_list, lru) { |
b4c625c6 SL |
1977 | WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); |
1978 | r5c_recovery_load_one_stripe(log, sh); | |
b4c625c6 SL |
1979 | ctx->data_only_stripes++; |
1980 | } | |
1981 | ||
1982 | return 0; | |
1983 | } | |
1984 | ||
1985 | /* | |
1986 | * we did a recovery. Now ctx.pos points to an invalid meta block. New | |
1987 | * log will start here. but we can't let superblock point to last valid | |
1988 | * meta block. The log might looks like: | |
1989 | * | meta 1| meta 2| meta 3| | |
1990 | * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If | |
1991 | * superblock points to meta 1, we write a new valid meta 2n. if crash | |
1992 | * happens again, new recovery will start from meta 1. Since meta 2n is | |
1993 | * valid now, recovery will think meta 3 is valid, which is wrong. | |
1994 | * The solution is we create a new meta in meta2 with its seq == meta | |
1995 | * 1's seq + 10 and let superblock points to meta2. The same recovery will | |
1996 | * not think meta 3 is a valid meta, because its seq doesn't match | |
1997 | */ | |
1998 | ||
1999 | /* | |
2000 | * Before recovery, the log looks like the following | |
2001 | * | |
2002 | * --------------------------------------------- | |
2003 | * | valid log | invalid log | | |
2004 | * --------------------------------------------- | |
2005 | * ^ | |
2006 | * |- log->last_checkpoint | |
2007 | * |- log->last_cp_seq | |
2008 | * | |
2009 | * Now we scan through the log until we see invalid entry | |
2010 | * | |
2011 | * --------------------------------------------- | |
2012 | * | valid log | invalid log | | |
2013 | * --------------------------------------------- | |
2014 | * ^ ^ | |
2015 | * |- log->last_checkpoint |- ctx->pos | |
2016 | * |- log->last_cp_seq |- ctx->seq | |
2017 | * | |
2018 | * From this point, we need to increase seq number by 10 to avoid | |
2019 | * confusing next recovery. | |
2020 | * | |
2021 | * --------------------------------------------- | |
2022 | * | valid log | invalid log | | |
2023 | * --------------------------------------------- | |
2024 | * ^ ^ | |
2025 | * |- log->last_checkpoint |- ctx->pos+1 | |
2026 | * |- log->last_cp_seq |- ctx->seq+11 | |
2027 | * | |
2028 | * However, it is not safe to start the state machine yet, because data only | |
2029 | * parities are not yet secured in RAID. To save these data only parities, we | |
2030 | * rewrite them from seq+11. | |
2031 | * | |
2032 | * ----------------------------------------------------------------- | |
2033 | * | valid log | data only stripes | invalid log | | |
2034 | * ----------------------------------------------------------------- | |
2035 | * ^ ^ | |
2036 | * |- log->last_checkpoint |- ctx->pos+n | |
2037 | * |- log->last_cp_seq |- ctx->seq+10+n | |
2038 | * | |
2039 | * If failure happens again during this process, the recovery can safe start | |
2040 | * again from log->last_checkpoint. | |
2041 | * | |
2042 | * Once data only stripes are rewritten to journal, we move log_tail | |
2043 | * | |
2044 | * ----------------------------------------------------------------- | |
2045 | * | old log | data only stripes | invalid log | | |
2046 | * ----------------------------------------------------------------- | |
2047 | * ^ ^ | |
2048 | * |- log->last_checkpoint |- ctx->pos+n | |
2049 | * |- log->last_cp_seq |- ctx->seq+10+n | |
2050 | * | |
2051 | * Then we can safely start the state machine. If failure happens from this | |
2052 | * point on, the recovery will start from new log->last_checkpoint. | |
2053 | */ | |
2054 | static int | |
2055 | r5c_recovery_rewrite_data_only_stripes(struct r5l_log *log, | |
2056 | struct r5l_recovery_ctx *ctx) | |
2057 | { | |
bc8f167f | 2058 | struct stripe_head *sh, *next; |
b4c625c6 SL |
2059 | struct mddev *mddev = log->rdev->mddev; |
2060 | struct page *page; | |
2061 | ||
2062 | page = alloc_page(GFP_KERNEL); | |
2063 | if (!page) { | |
2064 | pr_err("md/raid:%s: cannot allocate memory to rewrite data only stripes\n", | |
2065 | mdname(mddev)); | |
2066 | return -ENOMEM; | |
2067 | } | |
2068 | ||
2069 | ctx->seq += 10; | |
bc8f167f | 2070 | list_for_each_entry_safe(sh, next, &ctx->cached_list, lru) { |
b4c625c6 SL |
2071 | struct r5l_meta_block *mb; |
2072 | int i; | |
2073 | int offset; | |
2074 | sector_t write_pos; | |
2075 | ||
2076 | WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); | |
2077 | r5l_recovery_create_empty_meta_block(log, page, | |
2078 | ctx->pos, ctx->seq); | |
2079 | mb = page_address(page); | |
2080 | offset = le32_to_cpu(mb->meta_size); | |
fc833c2a | 2081 | write_pos = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS); |
b4c625c6 SL |
2082 | |
2083 | for (i = sh->disks; i--; ) { | |
2084 | struct r5dev *dev = &sh->dev[i]; | |
2085 | struct r5l_payload_data_parity *payload; | |
2086 | void *addr; | |
2087 | ||
2088 | if (test_bit(R5_InJournal, &dev->flags)) { | |
2089 | payload = (void *)mb + offset; | |
2090 | payload->header.type = cpu_to_le16( | |
2091 | R5LOG_PAYLOAD_DATA); | |
2092 | payload->size = BLOCK_SECTORS; | |
2093 | payload->location = cpu_to_le64( | |
2094 | raid5_compute_blocknr(sh, i, 0)); | |
2095 | addr = kmap_atomic(dev->page); | |
2096 | payload->checksum[0] = cpu_to_le32( | |
2097 | crc32c_le(log->uuid_checksum, addr, | |
2098 | PAGE_SIZE)); | |
2099 | kunmap_atomic(addr); | |
2100 | sync_page_io(log->rdev, write_pos, PAGE_SIZE, | |
2101 | dev->page, REQ_OP_WRITE, 0, false); | |
2102 | write_pos = r5l_ring_add(log, write_pos, | |
2103 | BLOCK_SECTORS); | |
2104 | offset += sizeof(__le32) + | |
2105 | sizeof(struct r5l_payload_data_parity); | |
2106 | ||
2107 | } | |
2108 | } | |
2109 | mb->meta_size = cpu_to_le32(offset); | |
2110 | mb->checksum = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE); | |
2111 | sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, | |
2112 | REQ_OP_WRITE, WRITE_FUA, false); | |
2113 | sh->log_start = ctx->pos; | |
2114 | ctx->pos = write_pos; | |
2115 | ctx->seq += 1; | |
bc8f167f J |
2116 | |
2117 | list_del_init(&sh->lru); | |
2118 | raid5_release_stripe(sh); | |
b4c625c6 SL |
2119 | } |
2120 | __free_page(page); | |
2121 | return 0; | |
2122 | } | |
2123 | ||
f6bed0ef SL |
2124 | static int r5l_recovery_log(struct r5l_log *log) |
2125 | { | |
5aabf7c4 | 2126 | struct mddev *mddev = log->rdev->mddev; |
355810d1 | 2127 | struct r5l_recovery_ctx ctx; |
5aabf7c4 | 2128 | int ret; |
355810d1 SL |
2129 | |
2130 | ctx.pos = log->last_checkpoint; | |
2131 | ctx.seq = log->last_cp_seq; | |
2132 | ctx.meta_page = alloc_page(GFP_KERNEL); | |
b4c625c6 SL |
2133 | ctx.data_only_stripes = 0; |
2134 | ctx.data_parity_stripes = 0; | |
2135 | INIT_LIST_HEAD(&ctx.cached_list); | |
2136 | ||
355810d1 SL |
2137 | if (!ctx.meta_page) |
2138 | return -ENOMEM; | |
2139 | ||
5aabf7c4 | 2140 | ret = r5c_recovery_flush_log(log, &ctx); |
355810d1 SL |
2141 | __free_page(ctx.meta_page); |
2142 | ||
5aabf7c4 SL |
2143 | if (ret) |
2144 | return ret; | |
b4c625c6 | 2145 | |
5aabf7c4 SL |
2146 | if ((ctx.data_only_stripes == 0) && (ctx.data_parity_stripes == 0)) |
2147 | pr_debug("md/raid:%s: starting from clean shutdown\n", | |
2148 | mdname(mddev)); | |
2149 | else { | |
2150 | pr_debug("md/raid:%s: recoverying %d data-only stripes and %d data-parity stripes\n", | |
2151 | mdname(mddev), ctx.data_only_stripes, | |
2152 | ctx.data_parity_stripes); | |
2153 | ||
2154 | if (ctx.data_only_stripes > 0) | |
2155 | if (r5c_recovery_rewrite_data_only_stripes(log, &ctx)) { | |
2156 | pr_err("md/raid:%s: failed to rewrite stripes to journal\n", | |
2157 | mdname(mddev)); | |
2158 | return -EIO; | |
2159 | } | |
b4c625c6 SL |
2160 | } |
2161 | ||
5aabf7c4 SL |
2162 | log->log_start = ctx.pos; |
2163 | log->next_checkpoint = ctx.pos; | |
2164 | log->seq = ctx.seq; | |
2165 | r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq); | |
2166 | r5l_write_super(log, ctx.pos); | |
f6bed0ef SL |
2167 | return 0; |
2168 | } | |
2169 | ||
2170 | static void r5l_write_super(struct r5l_log *log, sector_t cp) | |
2171 | { | |
2172 | struct mddev *mddev = log->rdev->mddev; | |
2173 | ||
2174 | log->rdev->journal_tail = cp; | |
2175 | set_bit(MD_CHANGE_DEVS, &mddev->flags); | |
2176 | } | |
2177 | ||
2c7da14b SL |
2178 | static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page) |
2179 | { | |
2180 | struct r5conf *conf = mddev->private; | |
2181 | int ret; | |
2182 | ||
2183 | if (!conf->log) | |
2184 | return 0; | |
2185 | ||
2186 | switch (conf->log->r5c_journal_mode) { | |
2187 | case R5C_JOURNAL_MODE_WRITE_THROUGH: | |
2188 | ret = snprintf( | |
2189 | page, PAGE_SIZE, "[%s] %s\n", | |
2190 | r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH], | |
2191 | r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]); | |
2192 | break; | |
2193 | case R5C_JOURNAL_MODE_WRITE_BACK: | |
2194 | ret = snprintf( | |
2195 | page, PAGE_SIZE, "%s [%s]\n", | |
2196 | r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH], | |
2197 | r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]); | |
2198 | break; | |
2199 | default: | |
2200 | ret = 0; | |
2201 | } | |
2202 | return ret; | |
2203 | } | |
2204 | ||
2205 | static ssize_t r5c_journal_mode_store(struct mddev *mddev, | |
2206 | const char *page, size_t length) | |
2207 | { | |
2208 | struct r5conf *conf = mddev->private; | |
2209 | struct r5l_log *log = conf->log; | |
2210 | int val = -1, i; | |
2211 | int len = length; | |
2212 | ||
2213 | if (!log) | |
2214 | return -ENODEV; | |
2215 | ||
2216 | if (len && page[len - 1] == '\n') | |
2217 | len -= 1; | |
2218 | for (i = 0; i < ARRAY_SIZE(r5c_journal_mode_str); i++) | |
2219 | if (strlen(r5c_journal_mode_str[i]) == len && | |
2220 | strncmp(page, r5c_journal_mode_str[i], len) == 0) { | |
2221 | val = i; | |
2222 | break; | |
2223 | } | |
2224 | if (val < R5C_JOURNAL_MODE_WRITE_THROUGH || | |
2225 | val > R5C_JOURNAL_MODE_WRITE_BACK) | |
2226 | return -EINVAL; | |
2227 | ||
2228 | mddev_suspend(mddev); | |
2229 | conf->log->r5c_journal_mode = val; | |
2230 | mddev_resume(mddev); | |
2231 | ||
2232 | pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n", | |
2233 | mdname(mddev), val, r5c_journal_mode_str[val]); | |
2234 | return length; | |
2235 | } | |
2236 | ||
2237 | struct md_sysfs_entry | |
2238 | r5c_journal_mode = __ATTR(journal_mode, 0644, | |
2239 | r5c_journal_mode_show, r5c_journal_mode_store); | |
2240 | ||
2ded3703 SL |
2241 | /* |
2242 | * Try handle write operation in caching phase. This function should only | |
2243 | * be called in write-back mode. | |
2244 | * | |
2245 | * If all outstanding writes can be handled in caching phase, returns 0 | |
2246 | * If writes requires write-out phase, call r5c_make_stripe_write_out() | |
2247 | * and returns -EAGAIN | |
2248 | */ | |
2249 | int r5c_try_caching_write(struct r5conf *conf, | |
2250 | struct stripe_head *sh, | |
2251 | struct stripe_head_state *s, | |
2252 | int disks) | |
2253 | { | |
2254 | struct r5l_log *log = conf->log; | |
1e6d690b SL |
2255 | int i; |
2256 | struct r5dev *dev; | |
2257 | int to_cache = 0; | |
2ded3703 SL |
2258 | |
2259 | BUG_ON(!r5c_is_writeback(log)); | |
2260 | ||
1e6d690b SL |
2261 | if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) { |
2262 | /* | |
2263 | * There are two different scenarios here: | |
2264 | * 1. The stripe has some data cached, and it is sent to | |
2265 | * write-out phase for reclaim | |
2266 | * 2. The stripe is clean, and this is the first write | |
2267 | * | |
2268 | * For 1, return -EAGAIN, so we continue with | |
2269 | * handle_stripe_dirtying(). | |
2270 | * | |
2271 | * For 2, set STRIPE_R5C_CACHING and continue with caching | |
2272 | * write. | |
2273 | */ | |
2274 | ||
2275 | /* case 1: anything injournal or anything in written */ | |
2276 | if (s->injournal > 0 || s->written > 0) | |
2277 | return -EAGAIN; | |
2278 | /* case 2 */ | |
2279 | set_bit(STRIPE_R5C_CACHING, &sh->state); | |
2280 | } | |
2281 | ||
2282 | for (i = disks; i--; ) { | |
2283 | dev = &sh->dev[i]; | |
2284 | /* if non-overwrite, use writing-out phase */ | |
2285 | if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) && | |
2286 | !test_bit(R5_InJournal, &dev->flags)) { | |
2287 | r5c_make_stripe_write_out(sh); | |
2288 | return -EAGAIN; | |
2289 | } | |
2290 | } | |
2291 | ||
2292 | for (i = disks; i--; ) { | |
2293 | dev = &sh->dev[i]; | |
2294 | if (dev->towrite) { | |
2295 | set_bit(R5_Wantwrite, &dev->flags); | |
2296 | set_bit(R5_Wantdrain, &dev->flags); | |
2297 | set_bit(R5_LOCKED, &dev->flags); | |
2298 | to_cache++; | |
2299 | } | |
2300 | } | |
2301 | ||
2302 | if (to_cache) { | |
2303 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); | |
2304 | /* | |
2305 | * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data() | |
2306 | * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in | |
2307 | * r5c_handle_data_cached() | |
2308 | */ | |
2309 | set_bit(STRIPE_LOG_TRAPPED, &sh->state); | |
2310 | } | |
2311 | ||
2312 | return 0; | |
2313 | } | |
2314 | ||
2315 | /* | |
2316 | * free extra pages (orig_page) we allocated for prexor | |
2317 | */ | |
2318 | void r5c_release_extra_page(struct stripe_head *sh) | |
2319 | { | |
d7bd398e | 2320 | struct r5conf *conf = sh->raid_conf; |
1e6d690b | 2321 | int i; |
d7bd398e SL |
2322 | bool using_disk_info_extra_page; |
2323 | ||
2324 | using_disk_info_extra_page = | |
2325 | sh->dev[0].orig_page == conf->disks[0].extra_page; | |
1e6d690b SL |
2326 | |
2327 | for (i = sh->disks; i--; ) | |
2328 | if (sh->dev[i].page != sh->dev[i].orig_page) { | |
2329 | struct page *p = sh->dev[i].orig_page; | |
2330 | ||
2331 | sh->dev[i].orig_page = sh->dev[i].page; | |
d7bd398e SL |
2332 | if (!using_disk_info_extra_page) |
2333 | put_page(p); | |
1e6d690b | 2334 | } |
d7bd398e SL |
2335 | |
2336 | if (using_disk_info_extra_page) { | |
2337 | clear_bit(R5C_EXTRA_PAGE_IN_USE, &conf->cache_state); | |
2338 | md_wakeup_thread(conf->mddev->thread); | |
2339 | } | |
2340 | } | |
2341 | ||
2342 | void r5c_use_extra_page(struct stripe_head *sh) | |
2343 | { | |
2344 | struct r5conf *conf = sh->raid_conf; | |
2345 | int i; | |
2346 | struct r5dev *dev; | |
2347 | ||
2348 | for (i = sh->disks; i--; ) { | |
2349 | dev = &sh->dev[i]; | |
2350 | if (dev->orig_page != dev->page) | |
2351 | put_page(dev->orig_page); | |
2352 | dev->orig_page = conf->disks[i].extra_page; | |
2353 | } | |
2ded3703 SL |
2354 | } |
2355 | ||
2356 | /* | |
2357 | * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the | |
2358 | * stripe is committed to RAID disks. | |
2359 | */ | |
2360 | void r5c_finish_stripe_write_out(struct r5conf *conf, | |
2361 | struct stripe_head *sh, | |
2362 | struct stripe_head_state *s) | |
2363 | { | |
1e6d690b SL |
2364 | int i; |
2365 | int do_wakeup = 0; | |
2366 | ||
2ded3703 SL |
2367 | if (!conf->log || |
2368 | !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags)) | |
2369 | return; | |
2370 | ||
2371 | WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state)); | |
2372 | clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags); | |
2373 | ||
2374 | if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) | |
2375 | return; | |
1e6d690b SL |
2376 | |
2377 | for (i = sh->disks; i--; ) { | |
2378 | clear_bit(R5_InJournal, &sh->dev[i].flags); | |
2379 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) | |
2380 | do_wakeup = 1; | |
2381 | } | |
2382 | ||
2383 | /* | |
2384 | * analyse_stripe() runs before r5c_finish_stripe_write_out(), | |
2385 | * We updated R5_InJournal, so we also update s->injournal. | |
2386 | */ | |
2387 | s->injournal = 0; | |
2388 | ||
2389 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) | |
2390 | if (atomic_dec_and_test(&conf->pending_full_writes)) | |
2391 | md_wakeup_thread(conf->mddev->thread); | |
2392 | ||
2393 | if (do_wakeup) | |
2394 | wake_up(&conf->wait_for_overlap); | |
a39f7afd SL |
2395 | |
2396 | if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) | |
2397 | return; | |
2398 | ||
2399 | spin_lock_irq(&conf->log->stripe_in_journal_lock); | |
2400 | list_del_init(&sh->r5c); | |
2401 | spin_unlock_irq(&conf->log->stripe_in_journal_lock); | |
2402 | sh->log_start = MaxSector; | |
2403 | atomic_dec(&conf->log->stripe_in_journal_count); | |
1e6d690b SL |
2404 | } |
2405 | ||
2406 | int | |
2407 | r5c_cache_data(struct r5l_log *log, struct stripe_head *sh, | |
2408 | struct stripe_head_state *s) | |
2409 | { | |
a39f7afd | 2410 | struct r5conf *conf = sh->raid_conf; |
1e6d690b SL |
2411 | int pages = 0; |
2412 | int reserve; | |
2413 | int i; | |
2414 | int ret = 0; | |
2415 | ||
2416 | BUG_ON(!log); | |
2417 | ||
2418 | for (i = 0; i < sh->disks; i++) { | |
2419 | void *addr; | |
2420 | ||
2421 | if (!test_bit(R5_Wantwrite, &sh->dev[i].flags)) | |
2422 | continue; | |
2423 | addr = kmap_atomic(sh->dev[i].page); | |
2424 | sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum, | |
2425 | addr, PAGE_SIZE); | |
2426 | kunmap_atomic(addr); | |
2427 | pages++; | |
2428 | } | |
2429 | WARN_ON(pages == 0); | |
2430 | ||
2431 | /* | |
2432 | * The stripe must enter state machine again to call endio, so | |
2433 | * don't delay. | |
2434 | */ | |
2435 | clear_bit(STRIPE_DELAYED, &sh->state); | |
2436 | atomic_inc(&sh->count); | |
2437 | ||
2438 | mutex_lock(&log->io_mutex); | |
2439 | /* meta + data */ | |
2440 | reserve = (1 + pages) << (PAGE_SHIFT - 9); | |
1e6d690b | 2441 | |
a39f7afd SL |
2442 | if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && |
2443 | sh->log_start == MaxSector) | |
2444 | r5l_add_no_space_stripe(log, sh); | |
2445 | else if (!r5l_has_free_space(log, reserve)) { | |
2446 | if (sh->log_start == log->last_checkpoint) | |
2447 | BUG(); | |
2448 | else | |
2449 | r5l_add_no_space_stripe(log, sh); | |
1e6d690b SL |
2450 | } else { |
2451 | ret = r5l_log_stripe(log, sh, pages, 0); | |
2452 | if (ret) { | |
2453 | spin_lock_irq(&log->io_list_lock); | |
2454 | list_add_tail(&sh->log_list, &log->no_mem_stripes); | |
2455 | spin_unlock_irq(&log->io_list_lock); | |
2456 | } | |
2457 | } | |
2458 | ||
2459 | mutex_unlock(&log->io_mutex); | |
2460 | return 0; | |
2ded3703 SL |
2461 | } |
2462 | ||
f6bed0ef SL |
2463 | static int r5l_load_log(struct r5l_log *log) |
2464 | { | |
2465 | struct md_rdev *rdev = log->rdev; | |
2466 | struct page *page; | |
2467 | struct r5l_meta_block *mb; | |
2468 | sector_t cp = log->rdev->journal_tail; | |
2469 | u32 stored_crc, expected_crc; | |
2470 | bool create_super = false; | |
2471 | int ret; | |
2472 | ||
2473 | /* Make sure it's valid */ | |
2474 | if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp) | |
2475 | cp = 0; | |
2476 | page = alloc_page(GFP_KERNEL); | |
2477 | if (!page) | |
2478 | return -ENOMEM; | |
2479 | ||
796a5cf0 | 2480 | if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) { |
f6bed0ef SL |
2481 | ret = -EIO; |
2482 | goto ioerr; | |
2483 | } | |
2484 | mb = page_address(page); | |
2485 | ||
2486 | if (le32_to_cpu(mb->magic) != R5LOG_MAGIC || | |
2487 | mb->version != R5LOG_VERSION) { | |
2488 | create_super = true; | |
2489 | goto create; | |
2490 | } | |
2491 | stored_crc = le32_to_cpu(mb->checksum); | |
2492 | mb->checksum = 0; | |
5cb2fbd6 | 2493 | expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE); |
f6bed0ef SL |
2494 | if (stored_crc != expected_crc) { |
2495 | create_super = true; | |
2496 | goto create; | |
2497 | } | |
2498 | if (le64_to_cpu(mb->position) != cp) { | |
2499 | create_super = true; | |
2500 | goto create; | |
2501 | } | |
2502 | create: | |
2503 | if (create_super) { | |
2504 | log->last_cp_seq = prandom_u32(); | |
2505 | cp = 0; | |
56056c2e | 2506 | r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq); |
f6bed0ef SL |
2507 | /* |
2508 | * Make sure super points to correct address. Log might have | |
2509 | * data very soon. If super hasn't correct log tail address, | |
2510 | * recovery can't find the log | |
2511 | */ | |
2512 | r5l_write_super(log, cp); | |
2513 | } else | |
2514 | log->last_cp_seq = le64_to_cpu(mb->seq); | |
2515 | ||
2516 | log->device_size = round_down(rdev->sectors, BLOCK_SECTORS); | |
0576b1c6 SL |
2517 | log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT; |
2518 | if (log->max_free_space > RECLAIM_MAX_FREE_SPACE) | |
2519 | log->max_free_space = RECLAIM_MAX_FREE_SPACE; | |
f6bed0ef | 2520 | log->last_checkpoint = cp; |
28cd88e2 | 2521 | log->next_checkpoint = cp; |
a39f7afd SL |
2522 | mutex_lock(&log->io_mutex); |
2523 | r5c_update_log_state(log); | |
2524 | mutex_unlock(&log->io_mutex); | |
f6bed0ef SL |
2525 | |
2526 | __free_page(page); | |
2527 | ||
2528 | return r5l_recovery_log(log); | |
2529 | ioerr: | |
2530 | __free_page(page); | |
2531 | return ret; | |
2532 | } | |
2533 | ||
2534 | int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev) | |
2535 | { | |
c888a8f9 | 2536 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
f6bed0ef SL |
2537 | struct r5l_log *log; |
2538 | ||
2539 | if (PAGE_SIZE != 4096) | |
2540 | return -EINVAL; | |
c757ec95 SL |
2541 | |
2542 | /* | |
2543 | * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and | |
2544 | * raid_disks r5l_payload_data_parity. | |
2545 | * | |
2546 | * Write journal and cache does not work for very big array | |
2547 | * (raid_disks > 203) | |
2548 | */ | |
2549 | if (sizeof(struct r5l_meta_block) + | |
2550 | ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) * | |
2551 | conf->raid_disks) > PAGE_SIZE) { | |
2552 | pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n", | |
2553 | mdname(conf->mddev), conf->raid_disks); | |
2554 | return -EINVAL; | |
2555 | } | |
2556 | ||
f6bed0ef SL |
2557 | log = kzalloc(sizeof(*log), GFP_KERNEL); |
2558 | if (!log) | |
2559 | return -ENOMEM; | |
2560 | log->rdev = rdev; | |
2561 | ||
c888a8f9 | 2562 | log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0; |
56fef7c6 | 2563 | |
5cb2fbd6 SL |
2564 | log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid, |
2565 | sizeof(rdev->mddev->uuid)); | |
f6bed0ef SL |
2566 | |
2567 | mutex_init(&log->io_mutex); | |
2568 | ||
2569 | spin_lock_init(&log->io_list_lock); | |
2570 | INIT_LIST_HEAD(&log->running_ios); | |
0576b1c6 | 2571 | INIT_LIST_HEAD(&log->io_end_ios); |
a8c34f91 | 2572 | INIT_LIST_HEAD(&log->flushing_ios); |
04732f74 | 2573 | INIT_LIST_HEAD(&log->finished_ios); |
a8c34f91 | 2574 | bio_init(&log->flush_bio); |
f6bed0ef SL |
2575 | |
2576 | log->io_kc = KMEM_CACHE(r5l_io_unit, 0); | |
2577 | if (!log->io_kc) | |
2578 | goto io_kc; | |
2579 | ||
5036c390 CH |
2580 | log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc); |
2581 | if (!log->io_pool) | |
2582 | goto io_pool; | |
2583 | ||
c38d29b3 CH |
2584 | log->bs = bioset_create(R5L_POOL_SIZE, 0); |
2585 | if (!log->bs) | |
2586 | goto io_bs; | |
2587 | ||
e8deb638 CH |
2588 | log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0); |
2589 | if (!log->meta_pool) | |
2590 | goto out_mempool; | |
2591 | ||
0576b1c6 SL |
2592 | log->reclaim_thread = md_register_thread(r5l_reclaim_thread, |
2593 | log->rdev->mddev, "reclaim"); | |
2594 | if (!log->reclaim_thread) | |
2595 | goto reclaim_thread; | |
a39f7afd SL |
2596 | log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL; |
2597 | ||
0fd22b45 | 2598 | init_waitqueue_head(&log->iounit_wait); |
0576b1c6 | 2599 | |
5036c390 CH |
2600 | INIT_LIST_HEAD(&log->no_mem_stripes); |
2601 | ||
f6bed0ef SL |
2602 | INIT_LIST_HEAD(&log->no_space_stripes); |
2603 | spin_lock_init(&log->no_space_stripes_lock); | |
2604 | ||
3bddb7f8 SL |
2605 | INIT_WORK(&log->deferred_io_work, r5l_submit_io_async); |
2606 | ||
2ded3703 | 2607 | log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH; |
a39f7afd SL |
2608 | INIT_LIST_HEAD(&log->stripe_in_journal_list); |
2609 | spin_lock_init(&log->stripe_in_journal_lock); | |
2610 | atomic_set(&log->stripe_in_journal_count, 0); | |
2ded3703 | 2611 | |
f6bed0ef SL |
2612 | if (r5l_load_log(log)) |
2613 | goto error; | |
2614 | ||
f6b6ec5c | 2615 | rcu_assign_pointer(conf->log, log); |
a62ab49e | 2616 | set_bit(MD_HAS_JOURNAL, &conf->mddev->flags); |
f6bed0ef | 2617 | return 0; |
e8deb638 | 2618 | |
f6bed0ef | 2619 | error: |
0576b1c6 SL |
2620 | md_unregister_thread(&log->reclaim_thread); |
2621 | reclaim_thread: | |
e8deb638 CH |
2622 | mempool_destroy(log->meta_pool); |
2623 | out_mempool: | |
c38d29b3 CH |
2624 | bioset_free(log->bs); |
2625 | io_bs: | |
5036c390 CH |
2626 | mempool_destroy(log->io_pool); |
2627 | io_pool: | |
f6bed0ef SL |
2628 | kmem_cache_destroy(log->io_kc); |
2629 | io_kc: | |
2630 | kfree(log); | |
2631 | return -EINVAL; | |
2632 | } | |
2633 | ||
2634 | void r5l_exit_log(struct r5l_log *log) | |
2635 | { | |
0576b1c6 | 2636 | md_unregister_thread(&log->reclaim_thread); |
e8deb638 | 2637 | mempool_destroy(log->meta_pool); |
c38d29b3 | 2638 | bioset_free(log->bs); |
5036c390 | 2639 | mempool_destroy(log->io_pool); |
f6bed0ef SL |
2640 | kmem_cache_destroy(log->io_kc); |
2641 | kfree(log); | |
2642 | } |