1f6bed0efSShaohua Li /* 2f6bed0efSShaohua Li * Copyright (C) 2015 Shaohua Li <shli@fb.com> 3b4c625c6SSong Liu * Copyright (C) 2016 Song Liu <songliubraving@fb.com> 4f6bed0efSShaohua Li * 5f6bed0efSShaohua Li * This program is free software; you can redistribute it and/or modify it 6f6bed0efSShaohua Li * under the terms and conditions of the GNU General Public License, 7f6bed0efSShaohua Li * version 2, as published by the Free Software Foundation. 8f6bed0efSShaohua Li * 9f6bed0efSShaohua Li * This program is distributed in the hope it will be useful, but WITHOUT 10f6bed0efSShaohua Li * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11f6bed0efSShaohua Li * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12f6bed0efSShaohua Li * more details. 13f6bed0efSShaohua Li * 14f6bed0efSShaohua Li */ 15f6bed0efSShaohua Li #include <linux/kernel.h> 16f6bed0efSShaohua Li #include <linux/wait.h> 17f6bed0efSShaohua Li #include <linux/blkdev.h> 18f6bed0efSShaohua Li #include <linux/slab.h> 19f6bed0efSShaohua Li #include <linux/raid/md_p.h> 205cb2fbd6SShaohua Li #include <linux/crc32c.h> 21f6bed0efSShaohua Li #include <linux/random.h> 22ce1ccd07SShaohua Li #include <linux/kthread.h> 2303b047f4SSong Liu #include <linux/types.h> 24f6bed0efSShaohua Li #include "md.h" 25f6bed0efSShaohua Li #include "raid5.h" 261e6d690bSSong Liu #include "bitmap.h" 27f6bed0efSShaohua Li 28f6bed0efSShaohua Li /* 29f6bed0efSShaohua Li * metadata/data stored in disk with 4k size unit (a block) regardless 30f6bed0efSShaohua Li * underneath hardware sector size. only works with PAGE_SIZE == 4096 31f6bed0efSShaohua Li */ 32f6bed0efSShaohua Li #define BLOCK_SECTORS (8) 33effe6ee7SSong Liu #define BLOCK_SECTOR_SHIFT (3) 34f6bed0efSShaohua Li 350576b1c6SShaohua Li /* 36a39f7afdSSong Liu * log->max_free_space is min(1/4 disk size, 10G reclaimable space). 37a39f7afdSSong Liu * 38a39f7afdSSong Liu * In write through mode, the reclaim runs every log->max_free_space. 39a39f7afdSSong Liu * This can prevent the recovery scans for too long 400576b1c6SShaohua Li */ 410576b1c6SShaohua Li #define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */ 420576b1c6SShaohua Li #define RECLAIM_MAX_FREE_SPACE_SHIFT (2) 430576b1c6SShaohua Li 44a39f7afdSSong Liu /* wake up reclaim thread periodically */ 45a39f7afdSSong Liu #define R5C_RECLAIM_WAKEUP_INTERVAL (30 * HZ) 46a39f7afdSSong Liu /* start flush with these full stripes */ 4784890c03SShaohua Li #define R5C_FULL_STRIPE_FLUSH_BATCH(conf) (conf->max_nr_stripes / 4) 48a39f7afdSSong Liu /* reclaim stripes in groups */ 49a39f7afdSSong Liu #define R5C_RECLAIM_STRIPE_GROUP (NR_STRIPE_HASH_LOCKS * 2) 50a39f7afdSSong Liu 51c38d29b3SChristoph Hellwig /* 52c38d29b3SChristoph Hellwig * We only need 2 bios per I/O unit to make progress, but ensure we 53c38d29b3SChristoph Hellwig * have a few more available to not get too tight. 54c38d29b3SChristoph Hellwig */ 55c38d29b3SChristoph Hellwig #define R5L_POOL_SIZE 4 56c38d29b3SChristoph Hellwig 572c7da14bSSong Liu static char *r5c_journal_mode_str[] = {"write-through", 582c7da14bSSong Liu "write-back"}; 592ded3703SSong Liu /* 602ded3703SSong Liu * raid5 cache state machine 612ded3703SSong Liu * 629b69173eSJackieLiu * With the RAID cache, each stripe works in two phases: 632ded3703SSong Liu * - caching phase 642ded3703SSong Liu * - writing-out phase 652ded3703SSong Liu * 662ded3703SSong Liu * These two phases are controlled by bit STRIPE_R5C_CACHING: 672ded3703SSong Liu * if STRIPE_R5C_CACHING == 0, the stripe is in writing-out phase 682ded3703SSong Liu * if STRIPE_R5C_CACHING == 1, the stripe is in caching phase 692ded3703SSong Liu * 702ded3703SSong Liu * When there is no journal, or the journal is in write-through mode, 712ded3703SSong Liu * the stripe is always in writing-out phase. 722ded3703SSong Liu * 732ded3703SSong Liu * For write-back journal, the stripe is sent to caching phase on write 742ded3703SSong Liu * (r5c_try_caching_write). r5c_make_stripe_write_out() kicks off 752ded3703SSong Liu * the write-out phase by clearing STRIPE_R5C_CACHING. 762ded3703SSong Liu * 772ded3703SSong Liu * Stripes in caching phase do not write the raid disks. Instead, all 782ded3703SSong Liu * writes are committed from the log device. Therefore, a stripe in 792ded3703SSong Liu * caching phase handles writes as: 802ded3703SSong Liu * - write to log device 812ded3703SSong Liu * - return IO 822ded3703SSong Liu * 832ded3703SSong Liu * Stripes in writing-out phase handle writes as: 842ded3703SSong Liu * - calculate parity 852ded3703SSong Liu * - write pending data and parity to journal 862ded3703SSong Liu * - write data and parity to raid disks 872ded3703SSong Liu * - return IO for pending writes 882ded3703SSong Liu */ 892ded3703SSong Liu 90f6bed0efSShaohua Li struct r5l_log { 91f6bed0efSShaohua Li struct md_rdev *rdev; 92f6bed0efSShaohua Li 93f6bed0efSShaohua Li u32 uuid_checksum; 94f6bed0efSShaohua Li 95f6bed0efSShaohua Li sector_t device_size; /* log device size, round to 96f6bed0efSShaohua Li * BLOCK_SECTORS */ 970576b1c6SShaohua Li sector_t max_free_space; /* reclaim run if free space is at 980576b1c6SShaohua Li * this size */ 99f6bed0efSShaohua Li 100f6bed0efSShaohua Li sector_t last_checkpoint; /* log tail. where recovery scan 101f6bed0efSShaohua Li * starts from */ 102f6bed0efSShaohua Li u64 last_cp_seq; /* log tail sequence */ 103f6bed0efSShaohua Li 104f6bed0efSShaohua Li sector_t log_start; /* log head. where new data appends */ 105f6bed0efSShaohua Li u64 seq; /* log head sequence */ 106f6bed0efSShaohua Li 10717036461SChristoph Hellwig sector_t next_checkpoint; 10817036461SChristoph Hellwig 109f6bed0efSShaohua Li struct mutex io_mutex; 110f6bed0efSShaohua Li struct r5l_io_unit *current_io; /* current io_unit accepting new data */ 111f6bed0efSShaohua Li 112f6bed0efSShaohua Li spinlock_t io_list_lock; 113f6bed0efSShaohua Li struct list_head running_ios; /* io_units which are still running, 114f6bed0efSShaohua Li * and have not yet been completely 115f6bed0efSShaohua Li * written to the log */ 116f6bed0efSShaohua Li struct list_head io_end_ios; /* io_units which have been completely 117f6bed0efSShaohua Li * written to the log but not yet written 118f6bed0efSShaohua Li * to the RAID */ 119a8c34f91SShaohua Li struct list_head flushing_ios; /* io_units which are waiting for log 120a8c34f91SShaohua Li * cache flush */ 12104732f74SChristoph Hellwig struct list_head finished_ios; /* io_units which settle down in log disk */ 122a8c34f91SShaohua Li struct bio flush_bio; 123f6bed0efSShaohua Li 1245036c390SChristoph Hellwig struct list_head no_mem_stripes; /* pending stripes, -ENOMEM */ 1255036c390SChristoph Hellwig 126f6bed0efSShaohua Li struct kmem_cache *io_kc; 1275036c390SChristoph Hellwig mempool_t *io_pool; 128c38d29b3SChristoph Hellwig struct bio_set *bs; 129e8deb638SChristoph Hellwig mempool_t *meta_pool; 130f6bed0efSShaohua Li 1310576b1c6SShaohua Li struct md_thread *reclaim_thread; 1320576b1c6SShaohua Li unsigned long reclaim_target; /* number of space that need to be 1330576b1c6SShaohua Li * reclaimed. if it's 0, reclaim spaces 1340576b1c6SShaohua Li * used by io_units which are in 1350576b1c6SShaohua Li * IO_UNIT_STRIPE_END state (eg, reclaim 1360576b1c6SShaohua Li * dones't wait for specific io_unit 1370576b1c6SShaohua Li * switching to IO_UNIT_STRIPE_END 1380576b1c6SShaohua Li * state) */ 1390fd22b45SShaohua Li wait_queue_head_t iounit_wait; 1400576b1c6SShaohua Li 141f6bed0efSShaohua Li struct list_head no_space_stripes; /* pending stripes, log has no space */ 142f6bed0efSShaohua Li spinlock_t no_space_stripes_lock; 14356fef7c6SChristoph Hellwig 14456fef7c6SChristoph Hellwig bool need_cache_flush; 1452ded3703SSong Liu 1462ded3703SSong Liu /* for r5c_cache */ 1472ded3703SSong Liu enum r5c_journal_mode r5c_journal_mode; 148a39f7afdSSong Liu 149a39f7afdSSong Liu /* all stripes in r5cache, in the order of seq at sh->log_start */ 150a39f7afdSSong Liu struct list_head stripe_in_journal_list; 151a39f7afdSSong Liu 152a39f7afdSSong Liu spinlock_t stripe_in_journal_lock; 153a39f7afdSSong Liu atomic_t stripe_in_journal_count; 1543bddb7f8SSong Liu 1553bddb7f8SSong Liu /* to submit async io_units, to fulfill ordering of flush */ 1563bddb7f8SSong Liu struct work_struct deferred_io_work; 1572e38a37fSSong Liu /* to disable write back during in degraded mode */ 1582e38a37fSSong Liu struct work_struct disable_writeback_work; 15903b047f4SSong Liu 16003b047f4SSong Liu /* to for chunk_aligned_read in writeback mode, details below */ 16103b047f4SSong Liu spinlock_t tree_lock; 16203b047f4SSong Liu struct radix_tree_root big_stripe_tree; 163f6bed0efSShaohua Li }; 164f6bed0efSShaohua Li 165f6bed0efSShaohua Li /* 16603b047f4SSong Liu * Enable chunk_aligned_read() with write back cache. 16703b047f4SSong Liu * 16803b047f4SSong Liu * Each chunk may contain more than one stripe (for example, a 256kB 16903b047f4SSong Liu * chunk contains 64 4kB-page, so this chunk contain 64 stripes). For 17003b047f4SSong Liu * chunk_aligned_read, these stripes are grouped into one "big_stripe". 17103b047f4SSong Liu * For each big_stripe, we count how many stripes of this big_stripe 17203b047f4SSong Liu * are in the write back cache. These data are tracked in a radix tree 17303b047f4SSong Liu * (big_stripe_tree). We use radix_tree item pointer as the counter. 17403b047f4SSong Liu * r5c_tree_index() is used to calculate keys for the radix tree. 17503b047f4SSong Liu * 17603b047f4SSong Liu * chunk_aligned_read() calls r5c_big_stripe_cached() to look up 17703b047f4SSong Liu * big_stripe of each chunk in the tree. If this big_stripe is in the 17803b047f4SSong Liu * tree, chunk_aligned_read() aborts. This look up is protected by 17903b047f4SSong Liu * rcu_read_lock(). 18003b047f4SSong Liu * 18103b047f4SSong Liu * It is necessary to remember whether a stripe is counted in 18203b047f4SSong Liu * big_stripe_tree. Instead of adding new flag, we reuses existing flags: 18303b047f4SSong Liu * STRIPE_R5C_PARTIAL_STRIPE and STRIPE_R5C_FULL_STRIPE. If either of these 18403b047f4SSong Liu * two flags are set, the stripe is counted in big_stripe_tree. This 18503b047f4SSong Liu * requires moving set_bit(STRIPE_R5C_PARTIAL_STRIPE) to 18603b047f4SSong Liu * r5c_try_caching_write(); and moving clear_bit of 18703b047f4SSong Liu * STRIPE_R5C_PARTIAL_STRIPE and STRIPE_R5C_FULL_STRIPE to 18803b047f4SSong Liu * r5c_finish_stripe_write_out(). 18903b047f4SSong Liu */ 19003b047f4SSong Liu 19103b047f4SSong Liu /* 19203b047f4SSong Liu * radix tree requests lowest 2 bits of data pointer to be 2b'00. 19303b047f4SSong Liu * So it is necessary to left shift the counter by 2 bits before using it 19403b047f4SSong Liu * as data pointer of the tree. 19503b047f4SSong Liu */ 19603b047f4SSong Liu #define R5C_RADIX_COUNT_SHIFT 2 19703b047f4SSong Liu 19803b047f4SSong Liu /* 19903b047f4SSong Liu * calculate key for big_stripe_tree 20003b047f4SSong Liu * 20103b047f4SSong Liu * sect: align_bi->bi_iter.bi_sector or sh->sector 20203b047f4SSong Liu */ 20303b047f4SSong Liu static inline sector_t r5c_tree_index(struct r5conf *conf, 20403b047f4SSong Liu sector_t sect) 20503b047f4SSong Liu { 20603b047f4SSong Liu sector_t offset; 20703b047f4SSong Liu 20803b047f4SSong Liu offset = sector_div(sect, conf->chunk_sectors); 20903b047f4SSong Liu return sect; 21003b047f4SSong Liu } 21103b047f4SSong Liu 21203b047f4SSong Liu /* 213f6bed0efSShaohua Li * an IO range starts from a meta data block and end at the next meta data 214f6bed0efSShaohua Li * block. The io unit's the meta data block tracks data/parity followed it. io 215f6bed0efSShaohua Li * unit is written to log disk with normal write, as we always flush log disk 216f6bed0efSShaohua Li * first and then start move data to raid disks, there is no requirement to 217f6bed0efSShaohua Li * write io unit with FLUSH/FUA 218f6bed0efSShaohua Li */ 219f6bed0efSShaohua Li struct r5l_io_unit { 220f6bed0efSShaohua Li struct r5l_log *log; 221f6bed0efSShaohua Li 222f6bed0efSShaohua Li struct page *meta_page; /* store meta block */ 223f6bed0efSShaohua Li int meta_offset; /* current offset in meta_page */ 224f6bed0efSShaohua Li 225f6bed0efSShaohua Li struct bio *current_bio;/* current_bio accepting new data */ 226f6bed0efSShaohua Li 227f6bed0efSShaohua Li atomic_t pending_stripe;/* how many stripes not flushed to raid */ 228f6bed0efSShaohua Li u64 seq; /* seq number of the metablock */ 229f6bed0efSShaohua Li sector_t log_start; /* where the io_unit starts */ 230f6bed0efSShaohua Li sector_t log_end; /* where the io_unit ends */ 231f6bed0efSShaohua Li struct list_head log_sibling; /* log->running_ios */ 232f6bed0efSShaohua Li struct list_head stripe_list; /* stripes added to the io_unit */ 233f6bed0efSShaohua Li 234f6bed0efSShaohua Li int state; 2356143e2ceSChristoph Hellwig bool need_split_bio; 2363bddb7f8SSong Liu struct bio *split_bio; 2373bddb7f8SSong Liu 2383bddb7f8SSong Liu unsigned int has_flush:1; /* include flush request */ 2393bddb7f8SSong Liu unsigned int has_fua:1; /* include fua request */ 2403bddb7f8SSong Liu unsigned int has_null_flush:1; /* include empty flush request */ 2413bddb7f8SSong Liu /* 2423bddb7f8SSong Liu * io isn't sent yet, flush/fua request can only be submitted till it's 2433bddb7f8SSong Liu * the first IO in running_ios list 2443bddb7f8SSong Liu */ 2453bddb7f8SSong Liu unsigned int io_deferred:1; 2463bddb7f8SSong Liu 2473bddb7f8SSong Liu struct bio_list flush_barriers; /* size == 0 flush bios */ 248f6bed0efSShaohua Li }; 249f6bed0efSShaohua Li 250f6bed0efSShaohua Li /* r5l_io_unit state */ 251f6bed0efSShaohua Li enum r5l_io_unit_state { 252f6bed0efSShaohua Li IO_UNIT_RUNNING = 0, /* accepting new IO */ 253f6bed0efSShaohua Li IO_UNIT_IO_START = 1, /* io_unit bio start writing to log, 254f6bed0efSShaohua Li * don't accepting new bio */ 255f6bed0efSShaohua Li IO_UNIT_IO_END = 2, /* io_unit bio finish writing to log */ 256a8c34f91SShaohua Li IO_UNIT_STRIPE_END = 3, /* stripes data finished writing to raid */ 257f6bed0efSShaohua Li }; 258f6bed0efSShaohua Li 2592ded3703SSong Liu bool r5c_is_writeback(struct r5l_log *log) 2602ded3703SSong Liu { 2612ded3703SSong Liu return (log != NULL && 2622ded3703SSong Liu log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK); 2632ded3703SSong Liu } 2642ded3703SSong Liu 265f6bed0efSShaohua Li static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc) 266f6bed0efSShaohua Li { 267f6bed0efSShaohua Li start += inc; 268f6bed0efSShaohua Li if (start >= log->device_size) 269f6bed0efSShaohua Li start = start - log->device_size; 270f6bed0efSShaohua Li return start; 271f6bed0efSShaohua Li } 272f6bed0efSShaohua Li 273f6bed0efSShaohua Li static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start, 274f6bed0efSShaohua Li sector_t end) 275f6bed0efSShaohua Li { 276f6bed0efSShaohua Li if (end >= start) 277f6bed0efSShaohua Li return end - start; 278f6bed0efSShaohua Li else 279f6bed0efSShaohua Li return end + log->device_size - start; 280f6bed0efSShaohua Li } 281f6bed0efSShaohua Li 282f6bed0efSShaohua Li static bool r5l_has_free_space(struct r5l_log *log, sector_t size) 283f6bed0efSShaohua Li { 284f6bed0efSShaohua Li sector_t used_size; 285f6bed0efSShaohua Li 286f6bed0efSShaohua Li used_size = r5l_ring_distance(log, log->last_checkpoint, 287f6bed0efSShaohua Li log->log_start); 288f6bed0efSShaohua Li 289f6bed0efSShaohua Li return log->device_size > used_size + size; 290f6bed0efSShaohua Li } 291f6bed0efSShaohua Li 292f6bed0efSShaohua Li static void __r5l_set_io_unit_state(struct r5l_io_unit *io, 293f6bed0efSShaohua Li enum r5l_io_unit_state state) 294f6bed0efSShaohua Li { 295f6bed0efSShaohua Li if (WARN_ON(io->state >= state)) 296f6bed0efSShaohua Li return; 297f6bed0efSShaohua Li io->state = state; 298f6bed0efSShaohua Li } 299f6bed0efSShaohua Li 3001e6d690bSSong Liu static void 301bd83d0a2SNeilBrown r5c_return_dev_pending_writes(struct r5conf *conf, struct r5dev *dev) 3021e6d690bSSong Liu { 3031e6d690bSSong Liu struct bio *wbi, *wbi2; 3041e6d690bSSong Liu 3051e6d690bSSong Liu wbi = dev->written; 3061e6d690bSSong Liu dev->written = NULL; 3071e6d690bSSong Liu while (wbi && wbi->bi_iter.bi_sector < 3081e6d690bSSong Liu dev->sector + STRIPE_SECTORS) { 3091e6d690bSSong Liu wbi2 = r5_next_bio(wbi, dev->sector); 3101e6d690bSSong Liu md_write_end(conf->mddev); 311bd83d0a2SNeilBrown bio_endio(wbi); 3121e6d690bSSong Liu wbi = wbi2; 3131e6d690bSSong Liu } 3141e6d690bSSong Liu } 3151e6d690bSSong Liu 3161e6d690bSSong Liu void r5c_handle_cached_data_endio(struct r5conf *conf, 317bd83d0a2SNeilBrown struct stripe_head *sh, int disks) 3181e6d690bSSong Liu { 3191e6d690bSSong Liu int i; 3201e6d690bSSong Liu 3211e6d690bSSong Liu for (i = sh->disks; i--; ) { 3221e6d690bSSong Liu if (sh->dev[i].written) { 3231e6d690bSSong Liu set_bit(R5_UPTODATE, &sh->dev[i].flags); 324bd83d0a2SNeilBrown r5c_return_dev_pending_writes(conf, &sh->dev[i]); 3251e6d690bSSong Liu bitmap_endwrite(conf->mddev->bitmap, sh->sector, 3261e6d690bSSong Liu STRIPE_SECTORS, 3271e6d690bSSong Liu !test_bit(STRIPE_DEGRADED, &sh->state), 3281e6d690bSSong Liu 0); 3291e6d690bSSong Liu } 3301e6d690bSSong Liu } 3311e6d690bSSong Liu } 3321e6d690bSSong Liu 333ff875738SArtur Paszkiewicz void r5l_wake_reclaim(struct r5l_log *log, sector_t space); 334ff875738SArtur Paszkiewicz 335a39f7afdSSong Liu /* Check whether we should flush some stripes to free up stripe cache */ 336a39f7afdSSong Liu void r5c_check_stripe_cache_usage(struct r5conf *conf) 337a39f7afdSSong Liu { 338a39f7afdSSong Liu int total_cached; 339a39f7afdSSong Liu 340a39f7afdSSong Liu if (!r5c_is_writeback(conf->log)) 341a39f7afdSSong Liu return; 342a39f7afdSSong Liu 343a39f7afdSSong Liu total_cached = atomic_read(&conf->r5c_cached_partial_stripes) + 344a39f7afdSSong Liu atomic_read(&conf->r5c_cached_full_stripes); 345a39f7afdSSong Liu 346a39f7afdSSong Liu /* 347a39f7afdSSong Liu * The following condition is true for either of the following: 348a39f7afdSSong Liu * - stripe cache pressure high: 349a39f7afdSSong Liu * total_cached > 3/4 min_nr_stripes || 350a39f7afdSSong Liu * empty_inactive_list_nr > 0 351a39f7afdSSong Liu * - stripe cache pressure moderate: 352a39f7afdSSong Liu * total_cached > 1/2 min_nr_stripes 353a39f7afdSSong Liu */ 354a39f7afdSSong Liu if (total_cached > conf->min_nr_stripes * 1 / 2 || 355a39f7afdSSong Liu atomic_read(&conf->empty_inactive_list_nr) > 0) 356a39f7afdSSong Liu r5l_wake_reclaim(conf->log, 0); 357a39f7afdSSong Liu } 358a39f7afdSSong Liu 359a39f7afdSSong Liu /* 360a39f7afdSSong Liu * flush cache when there are R5C_FULL_STRIPE_FLUSH_BATCH or more full 361a39f7afdSSong Liu * stripes in the cache 362a39f7afdSSong Liu */ 363a39f7afdSSong Liu void r5c_check_cached_full_stripe(struct r5conf *conf) 364a39f7afdSSong Liu { 365a39f7afdSSong Liu if (!r5c_is_writeback(conf->log)) 366a39f7afdSSong Liu return; 367a39f7afdSSong Liu 368a39f7afdSSong Liu /* 369a39f7afdSSong Liu * wake up reclaim for R5C_FULL_STRIPE_FLUSH_BATCH cached stripes 370a39f7afdSSong Liu * or a full stripe (chunk size / 4k stripes). 371a39f7afdSSong Liu */ 372a39f7afdSSong Liu if (atomic_read(&conf->r5c_cached_full_stripes) >= 37384890c03SShaohua Li min(R5C_FULL_STRIPE_FLUSH_BATCH(conf), 374a39f7afdSSong Liu conf->chunk_sectors >> STRIPE_SHIFT)) 375a39f7afdSSong Liu r5l_wake_reclaim(conf->log, 0); 376a39f7afdSSong Liu } 377a39f7afdSSong Liu 378a39f7afdSSong Liu /* 379a39f7afdSSong Liu * Total log space (in sectors) needed to flush all data in cache 380a39f7afdSSong Liu * 38139b99586SSong Liu * To avoid deadlock due to log space, it is necessary to reserve log 38239b99586SSong Liu * space to flush critical stripes (stripes that occupying log space near 38339b99586SSong Liu * last_checkpoint). This function helps check how much log space is 38439b99586SSong Liu * required to flush all cached stripes. 385a39f7afdSSong Liu * 38639b99586SSong Liu * To reduce log space requirements, two mechanisms are used to give cache 38739b99586SSong Liu * flush higher priorities: 38839b99586SSong Liu * 1. In handle_stripe_dirtying() and schedule_reconstruction(), 38939b99586SSong Liu * stripes ALREADY in journal can be flushed w/o pending writes; 39039b99586SSong Liu * 2. In r5l_write_stripe() and r5c_cache_data(), stripes NOT in journal 39139b99586SSong Liu * can be delayed (r5l_add_no_space_stripe). 392a39f7afdSSong Liu * 39339b99586SSong Liu * In cache flush, the stripe goes through 1 and then 2. For a stripe that 39439b99586SSong Liu * already passed 1, flushing it requires at most (conf->max_degraded + 1) 39539b99586SSong Liu * pages of journal space. For stripes that has not passed 1, flushing it 39639b99586SSong Liu * requires (conf->raid_disks + 1) pages of journal space. There are at 39739b99586SSong Liu * most (conf->group_cnt + 1) stripe that passed 1. So total journal space 39839b99586SSong Liu * required to flush all cached stripes (in pages) is: 39939b99586SSong Liu * 40039b99586SSong Liu * (stripe_in_journal_count - group_cnt - 1) * (max_degraded + 1) + 40139b99586SSong Liu * (group_cnt + 1) * (raid_disks + 1) 40239b99586SSong Liu * or 40339b99586SSong Liu * (stripe_in_journal_count) * (max_degraded + 1) + 40439b99586SSong Liu * (group_cnt + 1) * (raid_disks - max_degraded) 405a39f7afdSSong Liu */ 406a39f7afdSSong Liu static sector_t r5c_log_required_to_flush_cache(struct r5conf *conf) 407a39f7afdSSong Liu { 408a39f7afdSSong Liu struct r5l_log *log = conf->log; 409a39f7afdSSong Liu 410a39f7afdSSong Liu if (!r5c_is_writeback(log)) 411a39f7afdSSong Liu return 0; 412a39f7afdSSong Liu 41339b99586SSong Liu return BLOCK_SECTORS * 41439b99586SSong Liu ((conf->max_degraded + 1) * atomic_read(&log->stripe_in_journal_count) + 41539b99586SSong Liu (conf->raid_disks - conf->max_degraded) * (conf->group_cnt + 1)); 416a39f7afdSSong Liu } 417a39f7afdSSong Liu 418a39f7afdSSong Liu /* 419a39f7afdSSong Liu * evaluate log space usage and update R5C_LOG_TIGHT and R5C_LOG_CRITICAL 420a39f7afdSSong Liu * 421a39f7afdSSong Liu * R5C_LOG_TIGHT is set when free space on the log device is less than 3x of 422a39f7afdSSong Liu * reclaim_required_space. R5C_LOG_CRITICAL is set when free space on the log 423a39f7afdSSong Liu * device is less than 2x of reclaim_required_space. 424a39f7afdSSong Liu */ 425a39f7afdSSong Liu static inline void r5c_update_log_state(struct r5l_log *log) 426a39f7afdSSong Liu { 427a39f7afdSSong Liu struct r5conf *conf = log->rdev->mddev->private; 428a39f7afdSSong Liu sector_t free_space; 429a39f7afdSSong Liu sector_t reclaim_space; 430f687a33eSSong Liu bool wake_reclaim = false; 431a39f7afdSSong Liu 432a39f7afdSSong Liu if (!r5c_is_writeback(log)) 433a39f7afdSSong Liu return; 434a39f7afdSSong Liu 435a39f7afdSSong Liu free_space = r5l_ring_distance(log, log->log_start, 436a39f7afdSSong Liu log->last_checkpoint); 437a39f7afdSSong Liu reclaim_space = r5c_log_required_to_flush_cache(conf); 438a39f7afdSSong Liu if (free_space < 2 * reclaim_space) 439a39f7afdSSong Liu set_bit(R5C_LOG_CRITICAL, &conf->cache_state); 440f687a33eSSong Liu else { 441f687a33eSSong Liu if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state)) 442f687a33eSSong Liu wake_reclaim = true; 443a39f7afdSSong Liu clear_bit(R5C_LOG_CRITICAL, &conf->cache_state); 444f687a33eSSong Liu } 445a39f7afdSSong Liu if (free_space < 3 * reclaim_space) 446a39f7afdSSong Liu set_bit(R5C_LOG_TIGHT, &conf->cache_state); 447a39f7afdSSong Liu else 448a39f7afdSSong Liu clear_bit(R5C_LOG_TIGHT, &conf->cache_state); 449f687a33eSSong Liu 450f687a33eSSong Liu if (wake_reclaim) 451f687a33eSSong Liu r5l_wake_reclaim(log, 0); 452a39f7afdSSong Liu } 453a39f7afdSSong Liu 4542ded3703SSong Liu /* 4552ded3703SSong Liu * Put the stripe into writing-out phase by clearing STRIPE_R5C_CACHING. 4562ded3703SSong Liu * This function should only be called in write-back mode. 4572ded3703SSong Liu */ 458a39f7afdSSong Liu void r5c_make_stripe_write_out(struct stripe_head *sh) 4592ded3703SSong Liu { 4602ded3703SSong Liu struct r5conf *conf = sh->raid_conf; 4612ded3703SSong Liu struct r5l_log *log = conf->log; 4622ded3703SSong Liu 4632ded3703SSong Liu BUG_ON(!r5c_is_writeback(log)); 4642ded3703SSong Liu 4652ded3703SSong Liu WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); 4662ded3703SSong Liu clear_bit(STRIPE_R5C_CACHING, &sh->state); 4671e6d690bSSong Liu 4681e6d690bSSong Liu if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) 4691e6d690bSSong Liu atomic_inc(&conf->preread_active_stripes); 4701e6d690bSSong Liu } 4711e6d690bSSong Liu 4721e6d690bSSong Liu static void r5c_handle_data_cached(struct stripe_head *sh) 4731e6d690bSSong Liu { 4741e6d690bSSong Liu int i; 4751e6d690bSSong Liu 4761e6d690bSSong Liu for (i = sh->disks; i--; ) 4771e6d690bSSong Liu if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { 4781e6d690bSSong Liu set_bit(R5_InJournal, &sh->dev[i].flags); 4791e6d690bSSong Liu clear_bit(R5_LOCKED, &sh->dev[i].flags); 4801e6d690bSSong Liu } 4811e6d690bSSong Liu clear_bit(STRIPE_LOG_TRAPPED, &sh->state); 4821e6d690bSSong Liu } 4831e6d690bSSong Liu 4841e6d690bSSong Liu /* 4851e6d690bSSong Liu * this journal write must contain full parity, 4861e6d690bSSong Liu * it may also contain some data pages 4871e6d690bSSong Liu */ 4881e6d690bSSong Liu static void r5c_handle_parity_cached(struct stripe_head *sh) 4891e6d690bSSong Liu { 4901e6d690bSSong Liu int i; 4911e6d690bSSong Liu 4921e6d690bSSong Liu for (i = sh->disks; i--; ) 4931e6d690bSSong Liu if (test_bit(R5_InJournal, &sh->dev[i].flags)) 4941e6d690bSSong Liu set_bit(R5_Wantwrite, &sh->dev[i].flags); 4952ded3703SSong Liu } 4962ded3703SSong Liu 4972ded3703SSong Liu /* 4982ded3703SSong Liu * Setting proper flags after writing (or flushing) data and/or parity to the 4992ded3703SSong Liu * log device. This is called from r5l_log_endio() or r5l_log_flush_endio(). 5002ded3703SSong Liu */ 5012ded3703SSong Liu static void r5c_finish_cache_stripe(struct stripe_head *sh) 5022ded3703SSong Liu { 5032ded3703SSong Liu struct r5l_log *log = sh->raid_conf->log; 5042ded3703SSong Liu 5052ded3703SSong Liu if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) { 5062ded3703SSong Liu BUG_ON(test_bit(STRIPE_R5C_CACHING, &sh->state)); 5072ded3703SSong Liu /* 5082ded3703SSong Liu * Set R5_InJournal for parity dev[pd_idx]. This means 5092ded3703SSong Liu * all data AND parity in the journal. For RAID 6, it is 5102ded3703SSong Liu * NOT necessary to set the flag for dev[qd_idx], as the 5112ded3703SSong Liu * two parities are written out together. 5122ded3703SSong Liu */ 5132ded3703SSong Liu set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags); 5141e6d690bSSong Liu } else if (test_bit(STRIPE_R5C_CACHING, &sh->state)) { 5151e6d690bSSong Liu r5c_handle_data_cached(sh); 5161e6d690bSSong Liu } else { 5171e6d690bSSong Liu r5c_handle_parity_cached(sh); 5181e6d690bSSong Liu set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags); 5191e6d690bSSong Liu } 5202ded3703SSong Liu } 5212ded3703SSong Liu 522d8858f43SChristoph Hellwig static void r5l_io_run_stripes(struct r5l_io_unit *io) 523d8858f43SChristoph Hellwig { 524d8858f43SChristoph Hellwig struct stripe_head *sh, *next; 525d8858f43SChristoph Hellwig 526d8858f43SChristoph Hellwig list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) { 527d8858f43SChristoph Hellwig list_del_init(&sh->log_list); 5282ded3703SSong Liu 5292ded3703SSong Liu r5c_finish_cache_stripe(sh); 5302ded3703SSong Liu 531d8858f43SChristoph Hellwig set_bit(STRIPE_HANDLE, &sh->state); 532d8858f43SChristoph Hellwig raid5_release_stripe(sh); 533d8858f43SChristoph Hellwig } 534d8858f43SChristoph Hellwig } 535d8858f43SChristoph Hellwig 53656fef7c6SChristoph Hellwig static void r5l_log_run_stripes(struct r5l_log *log) 53756fef7c6SChristoph Hellwig { 53856fef7c6SChristoph Hellwig struct r5l_io_unit *io, *next; 53956fef7c6SChristoph Hellwig 54056fef7c6SChristoph Hellwig assert_spin_locked(&log->io_list_lock); 54156fef7c6SChristoph Hellwig 54256fef7c6SChristoph Hellwig list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) { 54356fef7c6SChristoph Hellwig /* don't change list order */ 54456fef7c6SChristoph Hellwig if (io->state < IO_UNIT_IO_END) 54556fef7c6SChristoph Hellwig break; 54656fef7c6SChristoph Hellwig 54756fef7c6SChristoph Hellwig list_move_tail(&io->log_sibling, &log->finished_ios); 54856fef7c6SChristoph Hellwig r5l_io_run_stripes(io); 54956fef7c6SChristoph Hellwig } 55056fef7c6SChristoph Hellwig } 55156fef7c6SChristoph Hellwig 5523848c0bcSChristoph Hellwig static void r5l_move_to_end_ios(struct r5l_log *log) 5533848c0bcSChristoph Hellwig { 5543848c0bcSChristoph Hellwig struct r5l_io_unit *io, *next; 5553848c0bcSChristoph Hellwig 5563848c0bcSChristoph Hellwig assert_spin_locked(&log->io_list_lock); 5573848c0bcSChristoph Hellwig 5583848c0bcSChristoph Hellwig list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) { 5593848c0bcSChristoph Hellwig /* don't change list order */ 5603848c0bcSChristoph Hellwig if (io->state < IO_UNIT_IO_END) 5613848c0bcSChristoph Hellwig break; 5623848c0bcSChristoph Hellwig list_move_tail(&io->log_sibling, &log->io_end_ios); 5633848c0bcSChristoph Hellwig } 5643848c0bcSChristoph Hellwig } 5653848c0bcSChristoph Hellwig 5663bddb7f8SSong Liu static void __r5l_stripe_write_finished(struct r5l_io_unit *io); 567f6bed0efSShaohua Li static void r5l_log_endio(struct bio *bio) 568f6bed0efSShaohua Li { 569f6bed0efSShaohua Li struct r5l_io_unit *io = bio->bi_private; 5703bddb7f8SSong Liu struct r5l_io_unit *io_deferred; 571f6bed0efSShaohua Li struct r5l_log *log = io->log; 572509ffec7SChristoph Hellwig unsigned long flags; 573f6bed0efSShaohua Li 5746e74a9cfSShaohua Li if (bio->bi_error) 5756e74a9cfSShaohua Li md_error(log->rdev->mddev, log->rdev); 5766e74a9cfSShaohua Li 577f6bed0efSShaohua Li bio_put(bio); 578e8deb638SChristoph Hellwig mempool_free(io->meta_page, log->meta_pool); 579f6bed0efSShaohua Li 580509ffec7SChristoph Hellwig spin_lock_irqsave(&log->io_list_lock, flags); 581509ffec7SChristoph Hellwig __r5l_set_io_unit_state(io, IO_UNIT_IO_END); 582ea17481fSSong Liu if (log->need_cache_flush && !list_empty(&io->stripe_list)) 5833848c0bcSChristoph Hellwig r5l_move_to_end_ios(log); 58456fef7c6SChristoph Hellwig else 58556fef7c6SChristoph Hellwig r5l_log_run_stripes(log); 5863bddb7f8SSong Liu if (!list_empty(&log->running_ios)) { 5873bddb7f8SSong Liu /* 5883bddb7f8SSong Liu * FLUSH/FUA io_unit is deferred because of ordering, now we 5893bddb7f8SSong Liu * can dispatch it 5903bddb7f8SSong Liu */ 5913bddb7f8SSong Liu io_deferred = list_first_entry(&log->running_ios, 5923bddb7f8SSong Liu struct r5l_io_unit, log_sibling); 5933bddb7f8SSong Liu if (io_deferred->io_deferred) 5943bddb7f8SSong Liu schedule_work(&log->deferred_io_work); 5953bddb7f8SSong Liu } 5963bddb7f8SSong Liu 597509ffec7SChristoph Hellwig spin_unlock_irqrestore(&log->io_list_lock, flags); 598509ffec7SChristoph Hellwig 59956fef7c6SChristoph Hellwig if (log->need_cache_flush) 600f6bed0efSShaohua Li md_wakeup_thread(log->rdev->mddev->thread); 6013bddb7f8SSong Liu 6023bddb7f8SSong Liu if (io->has_null_flush) { 6033bddb7f8SSong Liu struct bio *bi; 6043bddb7f8SSong Liu 6053bddb7f8SSong Liu WARN_ON(bio_list_empty(&io->flush_barriers)); 6063bddb7f8SSong Liu while ((bi = bio_list_pop(&io->flush_barriers)) != NULL) { 6073bddb7f8SSong Liu bio_endio(bi); 6083bddb7f8SSong Liu atomic_dec(&io->pending_stripe); 6093bddb7f8SSong Liu } 610ea17481fSSong Liu } 611ea17481fSSong Liu 612ea17481fSSong Liu /* finish flush only io_unit and PAYLOAD_FLUSH only io_unit */ 6133bddb7f8SSong Liu if (atomic_read(&io->pending_stripe) == 0) 6143bddb7f8SSong Liu __r5l_stripe_write_finished(io); 6153bddb7f8SSong Liu } 6163bddb7f8SSong Liu 6173bddb7f8SSong Liu static void r5l_do_submit_io(struct r5l_log *log, struct r5l_io_unit *io) 6183bddb7f8SSong Liu { 6193bddb7f8SSong Liu unsigned long flags; 6203bddb7f8SSong Liu 6213bddb7f8SSong Liu spin_lock_irqsave(&log->io_list_lock, flags); 6223bddb7f8SSong Liu __r5l_set_io_unit_state(io, IO_UNIT_IO_START); 6233bddb7f8SSong Liu spin_unlock_irqrestore(&log->io_list_lock, flags); 6243bddb7f8SSong Liu 625*bb3338d3SSong Liu /* 626*bb3338d3SSong Liu * In case of journal device failures, submit_bio will get error 627*bb3338d3SSong Liu * and calls endio, then active stripes will continue write 628*bb3338d3SSong Liu * process. Therefore, it is not necessary to check Faulty bit 629*bb3338d3SSong Liu * of journal device here. 630*bb3338d3SSong Liu * 631*bb3338d3SSong Liu * We can't check split_bio after current_bio is submitted. If 632*bb3338d3SSong Liu * io->split_bio is null, after current_bio is submitted, current_bio 633*bb3338d3SSong Liu * might already be completed and the io_unit is freed. We submit 634*bb3338d3SSong Liu * split_bio first to avoid the issue. 635*bb3338d3SSong Liu */ 636*bb3338d3SSong Liu if (io->split_bio) { 6373bddb7f8SSong Liu if (io->has_flush) 63820737738SShaohua Li io->split_bio->bi_opf |= REQ_PREFLUSH; 6393bddb7f8SSong Liu if (io->has_fua) 64020737738SShaohua Li io->split_bio->bi_opf |= REQ_FUA; 6413bddb7f8SSong Liu submit_bio(io->split_bio); 6423bddb7f8SSong Liu } 6433bddb7f8SSong Liu 644*bb3338d3SSong Liu if (io->has_flush) 645*bb3338d3SSong Liu io->current_bio->bi_opf |= REQ_PREFLUSH; 646*bb3338d3SSong Liu if (io->has_fua) 647*bb3338d3SSong Liu io->current_bio->bi_opf |= REQ_FUA; 648*bb3338d3SSong Liu submit_bio(io->current_bio); 649*bb3338d3SSong Liu } 650*bb3338d3SSong Liu 6513bddb7f8SSong Liu /* deferred io_unit will be dispatched here */ 6523bddb7f8SSong Liu static void r5l_submit_io_async(struct work_struct *work) 6533bddb7f8SSong Liu { 6543bddb7f8SSong Liu struct r5l_log *log = container_of(work, struct r5l_log, 6553bddb7f8SSong Liu deferred_io_work); 6563bddb7f8SSong Liu struct r5l_io_unit *io = NULL; 6573bddb7f8SSong Liu unsigned long flags; 6583bddb7f8SSong Liu 6593bddb7f8SSong Liu spin_lock_irqsave(&log->io_list_lock, flags); 6603bddb7f8SSong Liu if (!list_empty(&log->running_ios)) { 6613bddb7f8SSong Liu io = list_first_entry(&log->running_ios, struct r5l_io_unit, 6623bddb7f8SSong Liu log_sibling); 6633bddb7f8SSong Liu if (!io->io_deferred) 6643bddb7f8SSong Liu io = NULL; 6653bddb7f8SSong Liu else 6663bddb7f8SSong Liu io->io_deferred = 0; 6673bddb7f8SSong Liu } 6683bddb7f8SSong Liu spin_unlock_irqrestore(&log->io_list_lock, flags); 6693bddb7f8SSong Liu if (io) 6703bddb7f8SSong Liu r5l_do_submit_io(log, io); 671f6bed0efSShaohua Li } 672f6bed0efSShaohua Li 6732e38a37fSSong Liu static void r5c_disable_writeback_async(struct work_struct *work) 6742e38a37fSSong Liu { 6752e38a37fSSong Liu struct r5l_log *log = container_of(work, struct r5l_log, 6762e38a37fSSong Liu disable_writeback_work); 6772e38a37fSSong Liu struct mddev *mddev = log->rdev->mddev; 6782e38a37fSSong Liu 6792e38a37fSSong Liu if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) 6802e38a37fSSong Liu return; 6812e38a37fSSong Liu pr_info("md/raid:%s: Disabling writeback cache for degraded array.\n", 6822e38a37fSSong Liu mdname(mddev)); 6832e38a37fSSong Liu mddev_suspend(mddev); 6842e38a37fSSong Liu log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH; 6852e38a37fSSong Liu mddev_resume(mddev); 6862e38a37fSSong Liu } 6872e38a37fSSong Liu 688f6bed0efSShaohua Li static void r5l_submit_current_io(struct r5l_log *log) 689f6bed0efSShaohua Li { 690f6bed0efSShaohua Li struct r5l_io_unit *io = log->current_io; 6913bddb7f8SSong Liu struct bio *bio; 692f6bed0efSShaohua Li struct r5l_meta_block *block; 693509ffec7SChristoph Hellwig unsigned long flags; 694f6bed0efSShaohua Li u32 crc; 6953bddb7f8SSong Liu bool do_submit = true; 696f6bed0efSShaohua Li 697f6bed0efSShaohua Li if (!io) 698f6bed0efSShaohua Li return; 699f6bed0efSShaohua Li 700f6bed0efSShaohua Li block = page_address(io->meta_page); 701f6bed0efSShaohua Li block->meta_size = cpu_to_le32(io->meta_offset); 7025cb2fbd6SShaohua Li crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE); 703f6bed0efSShaohua Li block->checksum = cpu_to_le32(crc); 7043bddb7f8SSong Liu bio = io->current_bio; 705f6bed0efSShaohua Li 706f6bed0efSShaohua Li log->current_io = NULL; 707509ffec7SChristoph Hellwig spin_lock_irqsave(&log->io_list_lock, flags); 7083bddb7f8SSong Liu if (io->has_flush || io->has_fua) { 7093bddb7f8SSong Liu if (io != list_first_entry(&log->running_ios, 7103bddb7f8SSong Liu struct r5l_io_unit, log_sibling)) { 7113bddb7f8SSong Liu io->io_deferred = 1; 7123bddb7f8SSong Liu do_submit = false; 7133bddb7f8SSong Liu } 7143bddb7f8SSong Liu } 715509ffec7SChristoph Hellwig spin_unlock_irqrestore(&log->io_list_lock, flags); 7163bddb7f8SSong Liu if (do_submit) 7173bddb7f8SSong Liu r5l_do_submit_io(log, io); 718f6bed0efSShaohua Li } 719f6bed0efSShaohua Li 7206143e2ceSChristoph Hellwig static struct bio *r5l_bio_alloc(struct r5l_log *log) 721b349feb3SChristoph Hellwig { 722c38d29b3SChristoph Hellwig struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs); 723b349feb3SChristoph Hellwig 724796a5cf0SMike Christie bio_set_op_attrs(bio, REQ_OP_WRITE, 0); 725b349feb3SChristoph Hellwig bio->bi_bdev = log->rdev->bdev; 7261e932a37SChristoph Hellwig bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start; 727b349feb3SChristoph Hellwig 728b349feb3SChristoph Hellwig return bio; 729b349feb3SChristoph Hellwig } 730b349feb3SChristoph Hellwig 731c1b99198SChristoph Hellwig static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io) 732c1b99198SChristoph Hellwig { 733c1b99198SChristoph Hellwig log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS); 734c1b99198SChristoph Hellwig 735a39f7afdSSong Liu r5c_update_log_state(log); 736c1b99198SChristoph Hellwig /* 737c1b99198SChristoph Hellwig * If we filled up the log device start from the beginning again, 738c1b99198SChristoph Hellwig * which will require a new bio. 739c1b99198SChristoph Hellwig * 740c1b99198SChristoph Hellwig * Note: for this to work properly the log size needs to me a multiple 741c1b99198SChristoph Hellwig * of BLOCK_SECTORS. 742c1b99198SChristoph Hellwig */ 743c1b99198SChristoph Hellwig if (log->log_start == 0) 7446143e2ceSChristoph Hellwig io->need_split_bio = true; 745c1b99198SChristoph Hellwig 746c1b99198SChristoph Hellwig io->log_end = log->log_start; 747c1b99198SChristoph Hellwig } 748c1b99198SChristoph Hellwig 749f6bed0efSShaohua Li static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log) 750f6bed0efSShaohua Li { 751f6bed0efSShaohua Li struct r5l_io_unit *io; 752f6bed0efSShaohua Li struct r5l_meta_block *block; 753f6bed0efSShaohua Li 7545036c390SChristoph Hellwig io = mempool_alloc(log->io_pool, GFP_ATOMIC); 7555036c390SChristoph Hellwig if (!io) 7565036c390SChristoph Hellwig return NULL; 7575036c390SChristoph Hellwig memset(io, 0, sizeof(*io)); 7585036c390SChristoph Hellwig 75951039cd0SChristoph Hellwig io->log = log; 76051039cd0SChristoph Hellwig INIT_LIST_HEAD(&io->log_sibling); 76151039cd0SChristoph Hellwig INIT_LIST_HEAD(&io->stripe_list); 7623bddb7f8SSong Liu bio_list_init(&io->flush_barriers); 76351039cd0SChristoph Hellwig io->state = IO_UNIT_RUNNING; 764f6bed0efSShaohua Li 765e8deb638SChristoph Hellwig io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO); 766f6bed0efSShaohua Li block = page_address(io->meta_page); 767e8deb638SChristoph Hellwig clear_page(block); 768f6bed0efSShaohua Li block->magic = cpu_to_le32(R5LOG_MAGIC); 769f6bed0efSShaohua Li block->version = R5LOG_VERSION; 770f6bed0efSShaohua Li block->seq = cpu_to_le64(log->seq); 771f6bed0efSShaohua Li block->position = cpu_to_le64(log->log_start); 772f6bed0efSShaohua Li 773f6bed0efSShaohua Li io->log_start = log->log_start; 774f6bed0efSShaohua Li io->meta_offset = sizeof(struct r5l_meta_block); 7752b8ef16eSChristoph Hellwig io->seq = log->seq++; 776f6bed0efSShaohua Li 7776143e2ceSChristoph Hellwig io->current_bio = r5l_bio_alloc(log); 7786143e2ceSChristoph Hellwig io->current_bio->bi_end_io = r5l_log_endio; 7796143e2ceSChristoph Hellwig io->current_bio->bi_private = io; 780b349feb3SChristoph Hellwig bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0); 781f6bed0efSShaohua Li 782c1b99198SChristoph Hellwig r5_reserve_log_entry(log, io); 783f6bed0efSShaohua Li 784f6bed0efSShaohua Li spin_lock_irq(&log->io_list_lock); 785f6bed0efSShaohua Li list_add_tail(&io->log_sibling, &log->running_ios); 786f6bed0efSShaohua Li spin_unlock_irq(&log->io_list_lock); 787f6bed0efSShaohua Li 788f6bed0efSShaohua Li return io; 789f6bed0efSShaohua Li } 790f6bed0efSShaohua Li 791f6bed0efSShaohua Li static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size) 792f6bed0efSShaohua Li { 79322581f58SChristoph Hellwig if (log->current_io && 79422581f58SChristoph Hellwig log->current_io->meta_offset + payload_size > PAGE_SIZE) 795f6bed0efSShaohua Li r5l_submit_current_io(log); 796f6bed0efSShaohua Li 7975036c390SChristoph Hellwig if (!log->current_io) { 798f6bed0efSShaohua Li log->current_io = r5l_new_meta(log); 7995036c390SChristoph Hellwig if (!log->current_io) 8005036c390SChristoph Hellwig return -ENOMEM; 8015036c390SChristoph Hellwig } 8025036c390SChristoph Hellwig 803f6bed0efSShaohua Li return 0; 804f6bed0efSShaohua Li } 805f6bed0efSShaohua Li 806f6bed0efSShaohua Li static void r5l_append_payload_meta(struct r5l_log *log, u16 type, 807f6bed0efSShaohua Li sector_t location, 808f6bed0efSShaohua Li u32 checksum1, u32 checksum2, 809f6bed0efSShaohua Li bool checksum2_valid) 810f6bed0efSShaohua Li { 811f6bed0efSShaohua Li struct r5l_io_unit *io = log->current_io; 812f6bed0efSShaohua Li struct r5l_payload_data_parity *payload; 813f6bed0efSShaohua Li 814f6bed0efSShaohua Li payload = page_address(io->meta_page) + io->meta_offset; 815f6bed0efSShaohua Li payload->header.type = cpu_to_le16(type); 816f6bed0efSShaohua Li payload->header.flags = cpu_to_le16(0); 817f6bed0efSShaohua Li payload->size = cpu_to_le32((1 + !!checksum2_valid) << 818f6bed0efSShaohua Li (PAGE_SHIFT - 9)); 819f6bed0efSShaohua Li payload->location = cpu_to_le64(location); 820f6bed0efSShaohua Li payload->checksum[0] = cpu_to_le32(checksum1); 821f6bed0efSShaohua Li if (checksum2_valid) 822f6bed0efSShaohua Li payload->checksum[1] = cpu_to_le32(checksum2); 823f6bed0efSShaohua Li 824f6bed0efSShaohua Li io->meta_offset += sizeof(struct r5l_payload_data_parity) + 825f6bed0efSShaohua Li sizeof(__le32) * (1 + !!checksum2_valid); 826f6bed0efSShaohua Li } 827f6bed0efSShaohua Li 828f6bed0efSShaohua Li static void r5l_append_payload_page(struct r5l_log *log, struct page *page) 829f6bed0efSShaohua Li { 830f6bed0efSShaohua Li struct r5l_io_unit *io = log->current_io; 831f6bed0efSShaohua Li 8326143e2ceSChristoph Hellwig if (io->need_split_bio) { 8333bddb7f8SSong Liu BUG_ON(io->split_bio); 8343bddb7f8SSong Liu io->split_bio = io->current_bio; 8356143e2ceSChristoph Hellwig io->current_bio = r5l_bio_alloc(log); 8363bddb7f8SSong Liu bio_chain(io->current_bio, io->split_bio); 8373bddb7f8SSong Liu io->need_split_bio = false; 838f6bed0efSShaohua Li } 839f6bed0efSShaohua Li 8406143e2ceSChristoph Hellwig if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0)) 8416143e2ceSChristoph Hellwig BUG(); 8426143e2ceSChristoph Hellwig 843c1b99198SChristoph Hellwig r5_reserve_log_entry(log, io); 844f6bed0efSShaohua Li } 845f6bed0efSShaohua Li 846ea17481fSSong Liu static void r5l_append_flush_payload(struct r5l_log *log, sector_t sect) 847ea17481fSSong Liu { 848ea17481fSSong Liu struct mddev *mddev = log->rdev->mddev; 849ea17481fSSong Liu struct r5conf *conf = mddev->private; 850ea17481fSSong Liu struct r5l_io_unit *io; 851ea17481fSSong Liu struct r5l_payload_flush *payload; 852ea17481fSSong Liu int meta_size; 853ea17481fSSong Liu 854ea17481fSSong Liu /* 855ea17481fSSong Liu * payload_flush requires extra writes to the journal. 856ea17481fSSong Liu * To avoid handling the extra IO in quiesce, just skip 857ea17481fSSong Liu * flush_payload 858ea17481fSSong Liu */ 859ea17481fSSong Liu if (conf->quiesce) 860ea17481fSSong Liu return; 861ea17481fSSong Liu 862ea17481fSSong Liu mutex_lock(&log->io_mutex); 863ea17481fSSong Liu meta_size = sizeof(struct r5l_payload_flush) + sizeof(__le64); 864ea17481fSSong Liu 865ea17481fSSong Liu if (r5l_get_meta(log, meta_size)) { 866ea17481fSSong Liu mutex_unlock(&log->io_mutex); 867ea17481fSSong Liu return; 868ea17481fSSong Liu } 869ea17481fSSong Liu 870ea17481fSSong Liu /* current implementation is one stripe per flush payload */ 871ea17481fSSong Liu io = log->current_io; 872ea17481fSSong Liu payload = page_address(io->meta_page) + io->meta_offset; 873ea17481fSSong Liu payload->header.type = cpu_to_le16(R5LOG_PAYLOAD_FLUSH); 874ea17481fSSong Liu payload->header.flags = cpu_to_le16(0); 875ea17481fSSong Liu payload->size = cpu_to_le32(sizeof(__le64)); 876ea17481fSSong Liu payload->flush_stripes[0] = cpu_to_le64(sect); 877ea17481fSSong Liu io->meta_offset += meta_size; 878ea17481fSSong Liu mutex_unlock(&log->io_mutex); 879ea17481fSSong Liu } 880ea17481fSSong Liu 8815036c390SChristoph Hellwig static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh, 882f6bed0efSShaohua Li int data_pages, int parity_pages) 883f6bed0efSShaohua Li { 884f6bed0efSShaohua Li int i; 885f6bed0efSShaohua Li int meta_size; 8865036c390SChristoph Hellwig int ret; 887f6bed0efSShaohua Li struct r5l_io_unit *io; 888f6bed0efSShaohua Li 889f6bed0efSShaohua Li meta_size = 890f6bed0efSShaohua Li ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) 891f6bed0efSShaohua Li * data_pages) + 892f6bed0efSShaohua Li sizeof(struct r5l_payload_data_parity) + 893f6bed0efSShaohua Li sizeof(__le32) * parity_pages; 894f6bed0efSShaohua Li 8955036c390SChristoph Hellwig ret = r5l_get_meta(log, meta_size); 8965036c390SChristoph Hellwig if (ret) 8975036c390SChristoph Hellwig return ret; 8985036c390SChristoph Hellwig 899f6bed0efSShaohua Li io = log->current_io; 900f6bed0efSShaohua Li 9013bddb7f8SSong Liu if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state)) 9023bddb7f8SSong Liu io->has_flush = 1; 9033bddb7f8SSong Liu 904f6bed0efSShaohua Li for (i = 0; i < sh->disks; i++) { 9051e6d690bSSong Liu if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) || 9061e6d690bSSong Liu test_bit(R5_InJournal, &sh->dev[i].flags)) 907f6bed0efSShaohua Li continue; 908f6bed0efSShaohua Li if (i == sh->pd_idx || i == sh->qd_idx) 909f6bed0efSShaohua Li continue; 9103bddb7f8SSong Liu if (test_bit(R5_WantFUA, &sh->dev[i].flags) && 9113bddb7f8SSong Liu log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) { 9123bddb7f8SSong Liu io->has_fua = 1; 9133bddb7f8SSong Liu /* 9143bddb7f8SSong Liu * we need to flush journal to make sure recovery can 9153bddb7f8SSong Liu * reach the data with fua flag 9163bddb7f8SSong Liu */ 9173bddb7f8SSong Liu io->has_flush = 1; 9183bddb7f8SSong Liu } 919f6bed0efSShaohua Li r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA, 920f6bed0efSShaohua Li raid5_compute_blocknr(sh, i, 0), 921f6bed0efSShaohua Li sh->dev[i].log_checksum, 0, false); 922f6bed0efSShaohua Li r5l_append_payload_page(log, sh->dev[i].page); 923f6bed0efSShaohua Li } 924f6bed0efSShaohua Li 9252ded3703SSong Liu if (parity_pages == 2) { 926f6bed0efSShaohua Li r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY, 927f6bed0efSShaohua Li sh->sector, sh->dev[sh->pd_idx].log_checksum, 928f6bed0efSShaohua Li sh->dev[sh->qd_idx].log_checksum, true); 929f6bed0efSShaohua Li r5l_append_payload_page(log, sh->dev[sh->pd_idx].page); 930f6bed0efSShaohua Li r5l_append_payload_page(log, sh->dev[sh->qd_idx].page); 9312ded3703SSong Liu } else if (parity_pages == 1) { 932f6bed0efSShaohua Li r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY, 933f6bed0efSShaohua Li sh->sector, sh->dev[sh->pd_idx].log_checksum, 934f6bed0efSShaohua Li 0, false); 935f6bed0efSShaohua Li r5l_append_payload_page(log, sh->dev[sh->pd_idx].page); 9362ded3703SSong Liu } else /* Just writing data, not parity, in caching phase */ 9372ded3703SSong Liu BUG_ON(parity_pages != 0); 938f6bed0efSShaohua Li 939f6bed0efSShaohua Li list_add_tail(&sh->log_list, &io->stripe_list); 940f6bed0efSShaohua Li atomic_inc(&io->pending_stripe); 941f6bed0efSShaohua Li sh->log_io = io; 9425036c390SChristoph Hellwig 943a39f7afdSSong Liu if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) 944a39f7afdSSong Liu return 0; 945a39f7afdSSong Liu 946a39f7afdSSong Liu if (sh->log_start == MaxSector) { 947a39f7afdSSong Liu BUG_ON(!list_empty(&sh->r5c)); 948a39f7afdSSong Liu sh->log_start = io->log_start; 949a39f7afdSSong Liu spin_lock_irq(&log->stripe_in_journal_lock); 950a39f7afdSSong Liu list_add_tail(&sh->r5c, 951a39f7afdSSong Liu &log->stripe_in_journal_list); 952a39f7afdSSong Liu spin_unlock_irq(&log->stripe_in_journal_lock); 953a39f7afdSSong Liu atomic_inc(&log->stripe_in_journal_count); 954a39f7afdSSong Liu } 9555036c390SChristoph Hellwig return 0; 956f6bed0efSShaohua Li } 957f6bed0efSShaohua Li 958a39f7afdSSong Liu /* add stripe to no_space_stripes, and then wake up reclaim */ 959a39f7afdSSong Liu static inline void r5l_add_no_space_stripe(struct r5l_log *log, 960a39f7afdSSong Liu struct stripe_head *sh) 961a39f7afdSSong Liu { 962a39f7afdSSong Liu spin_lock(&log->no_space_stripes_lock); 963a39f7afdSSong Liu list_add_tail(&sh->log_list, &log->no_space_stripes); 964a39f7afdSSong Liu spin_unlock(&log->no_space_stripes_lock); 965a39f7afdSSong Liu } 966a39f7afdSSong Liu 967f6bed0efSShaohua Li /* 968f6bed0efSShaohua Li * running in raid5d, where reclaim could wait for raid5d too (when it flushes 969f6bed0efSShaohua Li * data from log to raid disks), so we shouldn't wait for reclaim here 970f6bed0efSShaohua Li */ 971f6bed0efSShaohua Li int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh) 972f6bed0efSShaohua Li { 973a39f7afdSSong Liu struct r5conf *conf = sh->raid_conf; 974f6bed0efSShaohua Li int write_disks = 0; 975f6bed0efSShaohua Li int data_pages, parity_pages; 976f6bed0efSShaohua Li int reserve; 977f6bed0efSShaohua Li int i; 9785036c390SChristoph Hellwig int ret = 0; 979a39f7afdSSong Liu bool wake_reclaim = false; 980f6bed0efSShaohua Li 981f6bed0efSShaohua Li if (!log) 982f6bed0efSShaohua Li return -EAGAIN; 983f6bed0efSShaohua Li /* Don't support stripe batch */ 984f6bed0efSShaohua Li if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) || 985f6bed0efSShaohua Li test_bit(STRIPE_SYNCING, &sh->state)) { 986f6bed0efSShaohua Li /* the stripe is written to log, we start writing it to raid */ 987f6bed0efSShaohua Li clear_bit(STRIPE_LOG_TRAPPED, &sh->state); 988f6bed0efSShaohua Li return -EAGAIN; 989f6bed0efSShaohua Li } 990f6bed0efSShaohua Li 9912ded3703SSong Liu WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state)); 9922ded3703SSong Liu 993f6bed0efSShaohua Li for (i = 0; i < sh->disks; i++) { 994f6bed0efSShaohua Li void *addr; 995f6bed0efSShaohua Li 9961e6d690bSSong Liu if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) || 9971e6d690bSSong Liu test_bit(R5_InJournal, &sh->dev[i].flags)) 998f6bed0efSShaohua Li continue; 9991e6d690bSSong Liu 1000f6bed0efSShaohua Li write_disks++; 1001f6bed0efSShaohua Li /* checksum is already calculated in last run */ 1002f6bed0efSShaohua Li if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) 1003f6bed0efSShaohua Li continue; 1004f6bed0efSShaohua Li addr = kmap_atomic(sh->dev[i].page); 10055cb2fbd6SShaohua Li sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum, 1006f6bed0efSShaohua Li addr, PAGE_SIZE); 1007f6bed0efSShaohua Li kunmap_atomic(addr); 1008f6bed0efSShaohua Li } 1009f6bed0efSShaohua Li parity_pages = 1 + !!(sh->qd_idx >= 0); 1010f6bed0efSShaohua Li data_pages = write_disks - parity_pages; 1011f6bed0efSShaohua Li 1012f6bed0efSShaohua Li set_bit(STRIPE_LOG_TRAPPED, &sh->state); 1013253f9fd4SShaohua Li /* 1014253f9fd4SShaohua Li * The stripe must enter state machine again to finish the write, so 1015253f9fd4SShaohua Li * don't delay. 1016253f9fd4SShaohua Li */ 1017253f9fd4SShaohua Li clear_bit(STRIPE_DELAYED, &sh->state); 1018f6bed0efSShaohua Li atomic_inc(&sh->count); 1019f6bed0efSShaohua Li 1020f6bed0efSShaohua Li mutex_lock(&log->io_mutex); 1021f6bed0efSShaohua Li /* meta + data */ 1022f6bed0efSShaohua Li reserve = (1 + write_disks) << (PAGE_SHIFT - 9); 1023f6bed0efSShaohua Li 1024a39f7afdSSong Liu if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) { 1025a39f7afdSSong Liu if (!r5l_has_free_space(log, reserve)) { 1026a39f7afdSSong Liu r5l_add_no_space_stripe(log, sh); 1027a39f7afdSSong Liu wake_reclaim = true; 10285036c390SChristoph Hellwig } else { 10295036c390SChristoph Hellwig ret = r5l_log_stripe(log, sh, data_pages, parity_pages); 10305036c390SChristoph Hellwig if (ret) { 10315036c390SChristoph Hellwig spin_lock_irq(&log->io_list_lock); 1032a39f7afdSSong Liu list_add_tail(&sh->log_list, 1033a39f7afdSSong Liu &log->no_mem_stripes); 10345036c390SChristoph Hellwig spin_unlock_irq(&log->io_list_lock); 1035f6bed0efSShaohua Li } 10365036c390SChristoph Hellwig } 1037a39f7afdSSong Liu } else { /* R5C_JOURNAL_MODE_WRITE_BACK */ 1038a39f7afdSSong Liu /* 1039a39f7afdSSong Liu * log space critical, do not process stripes that are 1040a39f7afdSSong Liu * not in cache yet (sh->log_start == MaxSector). 1041a39f7afdSSong Liu */ 1042a39f7afdSSong Liu if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && 1043a39f7afdSSong Liu sh->log_start == MaxSector) { 1044a39f7afdSSong Liu r5l_add_no_space_stripe(log, sh); 1045a39f7afdSSong Liu wake_reclaim = true; 1046a39f7afdSSong Liu reserve = 0; 1047a39f7afdSSong Liu } else if (!r5l_has_free_space(log, reserve)) { 1048a39f7afdSSong Liu if (sh->log_start == log->last_checkpoint) 1049a39f7afdSSong Liu BUG(); 1050a39f7afdSSong Liu else 1051a39f7afdSSong Liu r5l_add_no_space_stripe(log, sh); 1052a39f7afdSSong Liu } else { 1053a39f7afdSSong Liu ret = r5l_log_stripe(log, sh, data_pages, parity_pages); 1054a39f7afdSSong Liu if (ret) { 1055a39f7afdSSong Liu spin_lock_irq(&log->io_list_lock); 1056a39f7afdSSong Liu list_add_tail(&sh->log_list, 1057a39f7afdSSong Liu &log->no_mem_stripes); 1058a39f7afdSSong Liu spin_unlock_irq(&log->io_list_lock); 1059a39f7afdSSong Liu } 1060a39f7afdSSong Liu } 1061a39f7afdSSong Liu } 1062f6bed0efSShaohua Li 10635036c390SChristoph Hellwig mutex_unlock(&log->io_mutex); 1064a39f7afdSSong Liu if (wake_reclaim) 1065a39f7afdSSong Liu r5l_wake_reclaim(log, reserve); 1066f6bed0efSShaohua Li return 0; 1067f6bed0efSShaohua Li } 1068f6bed0efSShaohua Li 1069f6bed0efSShaohua Li void r5l_write_stripe_run(struct r5l_log *log) 1070f6bed0efSShaohua Li { 1071f6bed0efSShaohua Li if (!log) 1072f6bed0efSShaohua Li return; 1073f6bed0efSShaohua Li mutex_lock(&log->io_mutex); 1074f6bed0efSShaohua Li r5l_submit_current_io(log); 1075f6bed0efSShaohua Li mutex_unlock(&log->io_mutex); 1076f6bed0efSShaohua Li } 1077f6bed0efSShaohua Li 1078828cbe98SShaohua Li int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio) 1079828cbe98SShaohua Li { 1080828cbe98SShaohua Li if (!log) 1081828cbe98SShaohua Li return -ENODEV; 10823bddb7f8SSong Liu 10833bddb7f8SSong Liu if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) { 1084828cbe98SShaohua Li /* 10853bddb7f8SSong Liu * in write through (journal only) 10863bddb7f8SSong Liu * we flush log disk cache first, then write stripe data to 10873bddb7f8SSong Liu * raid disks. So if bio is finished, the log disk cache is 10883bddb7f8SSong Liu * flushed already. The recovery guarantees we can recovery 10893bddb7f8SSong Liu * the bio from log disk, so we don't need to flush again 1090828cbe98SShaohua Li */ 1091828cbe98SShaohua Li if (bio->bi_iter.bi_size == 0) { 1092828cbe98SShaohua Li bio_endio(bio); 1093828cbe98SShaohua Li return 0; 1094828cbe98SShaohua Li } 10951eff9d32SJens Axboe bio->bi_opf &= ~REQ_PREFLUSH; 10963bddb7f8SSong Liu } else { 10973bddb7f8SSong Liu /* write back (with cache) */ 10983bddb7f8SSong Liu if (bio->bi_iter.bi_size == 0) { 10993bddb7f8SSong Liu mutex_lock(&log->io_mutex); 11003bddb7f8SSong Liu r5l_get_meta(log, 0); 11013bddb7f8SSong Liu bio_list_add(&log->current_io->flush_barriers, bio); 11023bddb7f8SSong Liu log->current_io->has_flush = 1; 11033bddb7f8SSong Liu log->current_io->has_null_flush = 1; 11043bddb7f8SSong Liu atomic_inc(&log->current_io->pending_stripe); 11053bddb7f8SSong Liu r5l_submit_current_io(log); 11063bddb7f8SSong Liu mutex_unlock(&log->io_mutex); 11073bddb7f8SSong Liu return 0; 11083bddb7f8SSong Liu } 11093bddb7f8SSong Liu } 1110828cbe98SShaohua Li return -EAGAIN; 1111828cbe98SShaohua Li } 1112828cbe98SShaohua Li 1113f6bed0efSShaohua Li /* This will run after log space is reclaimed */ 1114f6bed0efSShaohua Li static void r5l_run_no_space_stripes(struct r5l_log *log) 1115f6bed0efSShaohua Li { 1116f6bed0efSShaohua Li struct stripe_head *sh; 1117f6bed0efSShaohua Li 1118f6bed0efSShaohua Li spin_lock(&log->no_space_stripes_lock); 1119f6bed0efSShaohua Li while (!list_empty(&log->no_space_stripes)) { 1120f6bed0efSShaohua Li sh = list_first_entry(&log->no_space_stripes, 1121f6bed0efSShaohua Li struct stripe_head, log_list); 1122f6bed0efSShaohua Li list_del_init(&sh->log_list); 1123f6bed0efSShaohua Li set_bit(STRIPE_HANDLE, &sh->state); 1124f6bed0efSShaohua Li raid5_release_stripe(sh); 1125f6bed0efSShaohua Li } 1126f6bed0efSShaohua Li spin_unlock(&log->no_space_stripes_lock); 1127f6bed0efSShaohua Li } 1128f6bed0efSShaohua Li 1129a39f7afdSSong Liu /* 1130a39f7afdSSong Liu * calculate new last_checkpoint 1131a39f7afdSSong Liu * for write through mode, returns log->next_checkpoint 1132a39f7afdSSong Liu * for write back, returns log_start of first sh in stripe_in_journal_list 1133a39f7afdSSong Liu */ 1134a39f7afdSSong Liu static sector_t r5c_calculate_new_cp(struct r5conf *conf) 1135a39f7afdSSong Liu { 1136a39f7afdSSong Liu struct stripe_head *sh; 1137a39f7afdSSong Liu struct r5l_log *log = conf->log; 1138a39f7afdSSong Liu sector_t new_cp; 1139a39f7afdSSong Liu unsigned long flags; 1140a39f7afdSSong Liu 1141a39f7afdSSong Liu if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) 1142a39f7afdSSong Liu return log->next_checkpoint; 1143a39f7afdSSong Liu 1144a39f7afdSSong Liu spin_lock_irqsave(&log->stripe_in_journal_lock, flags); 1145a39f7afdSSong Liu if (list_empty(&conf->log->stripe_in_journal_list)) { 1146a39f7afdSSong Liu /* all stripes flushed */ 1147d3014e21SDan Carpenter spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags); 1148a39f7afdSSong Liu return log->next_checkpoint; 1149a39f7afdSSong Liu } 1150a39f7afdSSong Liu sh = list_first_entry(&conf->log->stripe_in_journal_list, 1151a39f7afdSSong Liu struct stripe_head, r5c); 1152a39f7afdSSong Liu new_cp = sh->log_start; 1153a39f7afdSSong Liu spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags); 1154a39f7afdSSong Liu return new_cp; 1155a39f7afdSSong Liu } 1156a39f7afdSSong Liu 115717036461SChristoph Hellwig static sector_t r5l_reclaimable_space(struct r5l_log *log) 115817036461SChristoph Hellwig { 1159a39f7afdSSong Liu struct r5conf *conf = log->rdev->mddev->private; 1160a39f7afdSSong Liu 116117036461SChristoph Hellwig return r5l_ring_distance(log, log->last_checkpoint, 1162a39f7afdSSong Liu r5c_calculate_new_cp(conf)); 116317036461SChristoph Hellwig } 116417036461SChristoph Hellwig 11655036c390SChristoph Hellwig static void r5l_run_no_mem_stripe(struct r5l_log *log) 11665036c390SChristoph Hellwig { 11675036c390SChristoph Hellwig struct stripe_head *sh; 11685036c390SChristoph Hellwig 11695036c390SChristoph Hellwig assert_spin_locked(&log->io_list_lock); 11705036c390SChristoph Hellwig 11715036c390SChristoph Hellwig if (!list_empty(&log->no_mem_stripes)) { 11725036c390SChristoph Hellwig sh = list_first_entry(&log->no_mem_stripes, 11735036c390SChristoph Hellwig struct stripe_head, log_list); 11745036c390SChristoph Hellwig list_del_init(&sh->log_list); 11755036c390SChristoph Hellwig set_bit(STRIPE_HANDLE, &sh->state); 11765036c390SChristoph Hellwig raid5_release_stripe(sh); 11775036c390SChristoph Hellwig } 11785036c390SChristoph Hellwig } 11795036c390SChristoph Hellwig 118004732f74SChristoph Hellwig static bool r5l_complete_finished_ios(struct r5l_log *log) 118117036461SChristoph Hellwig { 118217036461SChristoph Hellwig struct r5l_io_unit *io, *next; 118317036461SChristoph Hellwig bool found = false; 118417036461SChristoph Hellwig 118517036461SChristoph Hellwig assert_spin_locked(&log->io_list_lock); 118617036461SChristoph Hellwig 118704732f74SChristoph Hellwig list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) { 118817036461SChristoph Hellwig /* don't change list order */ 118917036461SChristoph Hellwig if (io->state < IO_UNIT_STRIPE_END) 119017036461SChristoph Hellwig break; 119117036461SChristoph Hellwig 119217036461SChristoph Hellwig log->next_checkpoint = io->log_start; 119317036461SChristoph Hellwig 119417036461SChristoph Hellwig list_del(&io->log_sibling); 11955036c390SChristoph Hellwig mempool_free(io, log->io_pool); 11965036c390SChristoph Hellwig r5l_run_no_mem_stripe(log); 119717036461SChristoph Hellwig 119817036461SChristoph Hellwig found = true; 119917036461SChristoph Hellwig } 120017036461SChristoph Hellwig 120117036461SChristoph Hellwig return found; 120217036461SChristoph Hellwig } 120317036461SChristoph Hellwig 1204509ffec7SChristoph Hellwig static void __r5l_stripe_write_finished(struct r5l_io_unit *io) 1205509ffec7SChristoph Hellwig { 1206509ffec7SChristoph Hellwig struct r5l_log *log = io->log; 1207a39f7afdSSong Liu struct r5conf *conf = log->rdev->mddev->private; 1208509ffec7SChristoph Hellwig unsigned long flags; 1209509ffec7SChristoph Hellwig 1210509ffec7SChristoph Hellwig spin_lock_irqsave(&log->io_list_lock, flags); 1211509ffec7SChristoph Hellwig __r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END); 121217036461SChristoph Hellwig 121304732f74SChristoph Hellwig if (!r5l_complete_finished_ios(log)) { 121485f2f9a4SShaohua Li spin_unlock_irqrestore(&log->io_list_lock, flags); 121585f2f9a4SShaohua Li return; 121685f2f9a4SShaohua Li } 1217509ffec7SChristoph Hellwig 1218a39f7afdSSong Liu if (r5l_reclaimable_space(log) > log->max_free_space || 1219a39f7afdSSong Liu test_bit(R5C_LOG_TIGHT, &conf->cache_state)) 1220509ffec7SChristoph Hellwig r5l_wake_reclaim(log, 0); 1221509ffec7SChristoph Hellwig 1222509ffec7SChristoph Hellwig spin_unlock_irqrestore(&log->io_list_lock, flags); 1223509ffec7SChristoph Hellwig wake_up(&log->iounit_wait); 1224509ffec7SChristoph Hellwig } 1225509ffec7SChristoph Hellwig 12260576b1c6SShaohua Li void r5l_stripe_write_finished(struct stripe_head *sh) 12270576b1c6SShaohua Li { 12280576b1c6SShaohua Li struct r5l_io_unit *io; 12290576b1c6SShaohua Li 12300576b1c6SShaohua Li io = sh->log_io; 12310576b1c6SShaohua Li sh->log_io = NULL; 12320576b1c6SShaohua Li 1233509ffec7SChristoph Hellwig if (io && atomic_dec_and_test(&io->pending_stripe)) 1234509ffec7SChristoph Hellwig __r5l_stripe_write_finished(io); 12350576b1c6SShaohua Li } 12360576b1c6SShaohua Li 1237a8c34f91SShaohua Li static void r5l_log_flush_endio(struct bio *bio) 1238a8c34f91SShaohua Li { 1239a8c34f91SShaohua Li struct r5l_log *log = container_of(bio, struct r5l_log, 1240a8c34f91SShaohua Li flush_bio); 1241a8c34f91SShaohua Li unsigned long flags; 1242a8c34f91SShaohua Li struct r5l_io_unit *io; 1243a8c34f91SShaohua Li 12446e74a9cfSShaohua Li if (bio->bi_error) 12456e74a9cfSShaohua Li md_error(log->rdev->mddev, log->rdev); 12466e74a9cfSShaohua Li 1247a8c34f91SShaohua Li spin_lock_irqsave(&log->io_list_lock, flags); 1248d8858f43SChristoph Hellwig list_for_each_entry(io, &log->flushing_ios, log_sibling) 1249d8858f43SChristoph Hellwig r5l_io_run_stripes(io); 125004732f74SChristoph Hellwig list_splice_tail_init(&log->flushing_ios, &log->finished_ios); 1251a8c34f91SShaohua Li spin_unlock_irqrestore(&log->io_list_lock, flags); 1252a8c34f91SShaohua Li } 1253a8c34f91SShaohua Li 12540576b1c6SShaohua Li /* 12550576b1c6SShaohua Li * Starting dispatch IO to raid. 12560576b1c6SShaohua Li * io_unit(meta) consists of a log. There is one situation we want to avoid. A 12570576b1c6SShaohua Li * broken meta in the middle of a log causes recovery can't find meta at the 12580576b1c6SShaohua Li * head of log. If operations require meta at the head persistent in log, we 12590576b1c6SShaohua Li * must make sure meta before it persistent in log too. A case is: 12600576b1c6SShaohua Li * 12610576b1c6SShaohua Li * stripe data/parity is in log, we start write stripe to raid disks. stripe 12620576b1c6SShaohua Li * data/parity must be persistent in log before we do the write to raid disks. 12630576b1c6SShaohua Li * 12640576b1c6SShaohua Li * The solution is we restrictly maintain io_unit list order. In this case, we 12650576b1c6SShaohua Li * only write stripes of an io_unit to raid disks till the io_unit is the first 12660576b1c6SShaohua Li * one whose data/parity is in log. 12670576b1c6SShaohua Li */ 12680576b1c6SShaohua Li void r5l_flush_stripe_to_raid(struct r5l_log *log) 12690576b1c6SShaohua Li { 1270a8c34f91SShaohua Li bool do_flush; 127156fef7c6SChristoph Hellwig 127256fef7c6SChristoph Hellwig if (!log || !log->need_cache_flush) 12730576b1c6SShaohua Li return; 12740576b1c6SShaohua Li 1275a8c34f91SShaohua Li spin_lock_irq(&log->io_list_lock); 1276a8c34f91SShaohua Li /* flush bio is running */ 1277a8c34f91SShaohua Li if (!list_empty(&log->flushing_ios)) { 1278a8c34f91SShaohua Li spin_unlock_irq(&log->io_list_lock); 12790576b1c6SShaohua Li return; 12800576b1c6SShaohua Li } 1281a8c34f91SShaohua Li list_splice_tail_init(&log->io_end_ios, &log->flushing_ios); 1282a8c34f91SShaohua Li do_flush = !list_empty(&log->flushing_ios); 12830576b1c6SShaohua Li spin_unlock_irq(&log->io_list_lock); 1284a8c34f91SShaohua Li 1285a8c34f91SShaohua Li if (!do_flush) 1286a8c34f91SShaohua Li return; 1287a8c34f91SShaohua Li bio_reset(&log->flush_bio); 1288a8c34f91SShaohua Li log->flush_bio.bi_bdev = log->rdev->bdev; 1289a8c34f91SShaohua Li log->flush_bio.bi_end_io = r5l_log_flush_endio; 129070fd7614SChristoph Hellwig log->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; 12914e49ea4aSMike Christie submit_bio(&log->flush_bio); 12920576b1c6SShaohua Li } 12930576b1c6SShaohua Li 12940576b1c6SShaohua Li static void r5l_write_super(struct r5l_log *log, sector_t cp); 12954b482044SShaohua Li static void r5l_write_super_and_discard_space(struct r5l_log *log, 12964b482044SShaohua Li sector_t end) 12974b482044SShaohua Li { 12984b482044SShaohua Li struct block_device *bdev = log->rdev->bdev; 12994b482044SShaohua Li struct mddev *mddev; 13004b482044SShaohua Li 13014b482044SShaohua Li r5l_write_super(log, end); 13024b482044SShaohua Li 13034b482044SShaohua Li if (!blk_queue_discard(bdev_get_queue(bdev))) 13044b482044SShaohua Li return; 13054b482044SShaohua Li 13064b482044SShaohua Li mddev = log->rdev->mddev; 13074b482044SShaohua Li /* 13088e018c21SShaohua Li * Discard could zero data, so before discard we must make sure 13098e018c21SShaohua Li * superblock is updated to new log tail. Updating superblock (either 13108e018c21SShaohua Li * directly call md_update_sb() or depend on md thread) must hold 13118e018c21SShaohua Li * reconfig mutex. On the other hand, raid5_quiesce is called with 13128e018c21SShaohua Li * reconfig_mutex hold. The first step of raid5_quiesce() is waitting 13138e018c21SShaohua Li * for all IO finish, hence waitting for reclaim thread, while reclaim 13148e018c21SShaohua Li * thread is calling this function and waitting for reconfig mutex. So 13158e018c21SShaohua Li * there is a deadlock. We workaround this issue with a trylock. 13168e018c21SShaohua Li * FIXME: we could miss discard if we can't take reconfig mutex 13174b482044SShaohua Li */ 13182953079cSShaohua Li set_mask_bits(&mddev->sb_flags, 0, 13192953079cSShaohua Li BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); 13208e018c21SShaohua Li if (!mddev_trylock(mddev)) 13218e018c21SShaohua Li return; 13224b482044SShaohua Li md_update_sb(mddev, 1); 13238e018c21SShaohua Li mddev_unlock(mddev); 13244b482044SShaohua Li 13256e74a9cfSShaohua Li /* discard IO error really doesn't matter, ignore it */ 13264b482044SShaohua Li if (log->last_checkpoint < end) { 13274b482044SShaohua Li blkdev_issue_discard(bdev, 13284b482044SShaohua Li log->last_checkpoint + log->rdev->data_offset, 13294b482044SShaohua Li end - log->last_checkpoint, GFP_NOIO, 0); 13304b482044SShaohua Li } else { 13314b482044SShaohua Li blkdev_issue_discard(bdev, 13324b482044SShaohua Li log->last_checkpoint + log->rdev->data_offset, 13334b482044SShaohua Li log->device_size - log->last_checkpoint, 13344b482044SShaohua Li GFP_NOIO, 0); 13354b482044SShaohua Li blkdev_issue_discard(bdev, log->rdev->data_offset, end, 13364b482044SShaohua Li GFP_NOIO, 0); 13374b482044SShaohua Li } 13384b482044SShaohua Li } 13394b482044SShaohua Li 1340a39f7afdSSong Liu /* 1341a39f7afdSSong Liu * r5c_flush_stripe moves stripe from cached list to handle_list. When called, 1342a39f7afdSSong Liu * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes. 1343a39f7afdSSong Liu * 1344a39f7afdSSong Liu * must hold conf->device_lock 1345a39f7afdSSong Liu */ 1346a39f7afdSSong Liu static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh) 1347a39f7afdSSong Liu { 1348a39f7afdSSong Liu BUG_ON(list_empty(&sh->lru)); 1349a39f7afdSSong Liu BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); 1350a39f7afdSSong Liu BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); 1351a39f7afdSSong Liu 1352a39f7afdSSong Liu /* 1353a39f7afdSSong Liu * The stripe is not ON_RELEASE_LIST, so it is safe to call 1354a39f7afdSSong Liu * raid5_release_stripe() while holding conf->device_lock 1355a39f7afdSSong Liu */ 1356a39f7afdSSong Liu BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state)); 1357a39f7afdSSong Liu assert_spin_locked(&conf->device_lock); 1358a39f7afdSSong Liu 1359a39f7afdSSong Liu list_del_init(&sh->lru); 1360a39f7afdSSong Liu atomic_inc(&sh->count); 1361a39f7afdSSong Liu 1362a39f7afdSSong Liu set_bit(STRIPE_HANDLE, &sh->state); 1363a39f7afdSSong Liu atomic_inc(&conf->active_stripes); 1364a39f7afdSSong Liu r5c_make_stripe_write_out(sh); 1365a39f7afdSSong Liu 1366e33fbb9cSShaohua Li if (test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) 1367e33fbb9cSShaohua Li atomic_inc(&conf->r5c_flushing_partial_stripes); 1368e33fbb9cSShaohua Li else 1369e33fbb9cSShaohua Li atomic_inc(&conf->r5c_flushing_full_stripes); 1370a39f7afdSSong Liu raid5_release_stripe(sh); 1371a39f7afdSSong Liu } 1372a39f7afdSSong Liu 1373a39f7afdSSong Liu /* 1374a39f7afdSSong Liu * if num == 0, flush all full stripes 1375a39f7afdSSong Liu * if num > 0, flush all full stripes. If less than num full stripes are 1376a39f7afdSSong Liu * flushed, flush some partial stripes until totally num stripes are 1377a39f7afdSSong Liu * flushed or there is no more cached stripes. 1378a39f7afdSSong Liu */ 1379a39f7afdSSong Liu void r5c_flush_cache(struct r5conf *conf, int num) 1380a39f7afdSSong Liu { 1381a39f7afdSSong Liu int count; 1382a39f7afdSSong Liu struct stripe_head *sh, *next; 1383a39f7afdSSong Liu 1384a39f7afdSSong Liu assert_spin_locked(&conf->device_lock); 1385a39f7afdSSong Liu if (!conf->log) 1386a39f7afdSSong Liu return; 1387a39f7afdSSong Liu 1388a39f7afdSSong Liu count = 0; 1389a39f7afdSSong Liu list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) { 1390a39f7afdSSong Liu r5c_flush_stripe(conf, sh); 1391a39f7afdSSong Liu count++; 1392a39f7afdSSong Liu } 1393a39f7afdSSong Liu 1394a39f7afdSSong Liu if (count >= num) 1395a39f7afdSSong Liu return; 1396a39f7afdSSong Liu list_for_each_entry_safe(sh, next, 1397a39f7afdSSong Liu &conf->r5c_partial_stripe_list, lru) { 1398a39f7afdSSong Liu r5c_flush_stripe(conf, sh); 1399a39f7afdSSong Liu if (++count >= num) 1400a39f7afdSSong Liu break; 1401a39f7afdSSong Liu } 1402a39f7afdSSong Liu } 1403a39f7afdSSong Liu 1404a39f7afdSSong Liu static void r5c_do_reclaim(struct r5conf *conf) 1405a39f7afdSSong Liu { 1406a39f7afdSSong Liu struct r5l_log *log = conf->log; 1407a39f7afdSSong Liu struct stripe_head *sh; 1408a39f7afdSSong Liu int count = 0; 1409a39f7afdSSong Liu unsigned long flags; 1410a39f7afdSSong Liu int total_cached; 1411a39f7afdSSong Liu int stripes_to_flush; 1412e33fbb9cSShaohua Li int flushing_partial, flushing_full; 1413a39f7afdSSong Liu 1414a39f7afdSSong Liu if (!r5c_is_writeback(log)) 1415a39f7afdSSong Liu return; 1416a39f7afdSSong Liu 1417e33fbb9cSShaohua Li flushing_partial = atomic_read(&conf->r5c_flushing_partial_stripes); 1418e33fbb9cSShaohua Li flushing_full = atomic_read(&conf->r5c_flushing_full_stripes); 1419a39f7afdSSong Liu total_cached = atomic_read(&conf->r5c_cached_partial_stripes) + 1420e33fbb9cSShaohua Li atomic_read(&conf->r5c_cached_full_stripes) - 1421e33fbb9cSShaohua Li flushing_full - flushing_partial; 1422a39f7afdSSong Liu 1423a39f7afdSSong Liu if (total_cached > conf->min_nr_stripes * 3 / 4 || 1424a39f7afdSSong Liu atomic_read(&conf->empty_inactive_list_nr) > 0) 1425a39f7afdSSong Liu /* 1426a39f7afdSSong Liu * if stripe cache pressure high, flush all full stripes and 1427a39f7afdSSong Liu * some partial stripes 1428a39f7afdSSong Liu */ 1429a39f7afdSSong Liu stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP; 1430a39f7afdSSong Liu else if (total_cached > conf->min_nr_stripes * 1 / 2 || 1431e33fbb9cSShaohua Li atomic_read(&conf->r5c_cached_full_stripes) - flushing_full > 143284890c03SShaohua Li R5C_FULL_STRIPE_FLUSH_BATCH(conf)) 1433a39f7afdSSong Liu /* 1434a39f7afdSSong Liu * if stripe cache pressure moderate, or if there is many full 1435a39f7afdSSong Liu * stripes,flush all full stripes 1436a39f7afdSSong Liu */ 1437a39f7afdSSong Liu stripes_to_flush = 0; 1438a39f7afdSSong Liu else 1439a39f7afdSSong Liu /* no need to flush */ 1440a39f7afdSSong Liu stripes_to_flush = -1; 1441a39f7afdSSong Liu 1442a39f7afdSSong Liu if (stripes_to_flush >= 0) { 1443a39f7afdSSong Liu spin_lock_irqsave(&conf->device_lock, flags); 1444a39f7afdSSong Liu r5c_flush_cache(conf, stripes_to_flush); 1445a39f7afdSSong Liu spin_unlock_irqrestore(&conf->device_lock, flags); 1446a39f7afdSSong Liu } 1447a39f7afdSSong Liu 1448a39f7afdSSong Liu /* if log space is tight, flush stripes on stripe_in_journal_list */ 1449a39f7afdSSong Liu if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) { 1450a39f7afdSSong Liu spin_lock_irqsave(&log->stripe_in_journal_lock, flags); 1451a39f7afdSSong Liu spin_lock(&conf->device_lock); 1452a39f7afdSSong Liu list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) { 1453a39f7afdSSong Liu /* 1454a39f7afdSSong Liu * stripes on stripe_in_journal_list could be in any 1455a39f7afdSSong Liu * state of the stripe_cache state machine. In this 1456a39f7afdSSong Liu * case, we only want to flush stripe on 1457a39f7afdSSong Liu * r5c_cached_full/partial_stripes. The following 1458a39f7afdSSong Liu * condition makes sure the stripe is on one of the 1459a39f7afdSSong Liu * two lists. 1460a39f7afdSSong Liu */ 1461a39f7afdSSong Liu if (!list_empty(&sh->lru) && 1462a39f7afdSSong Liu !test_bit(STRIPE_HANDLE, &sh->state) && 1463a39f7afdSSong Liu atomic_read(&sh->count) == 0) { 1464a39f7afdSSong Liu r5c_flush_stripe(conf, sh); 1465a39f7afdSSong Liu if (count++ >= R5C_RECLAIM_STRIPE_GROUP) 1466a39f7afdSSong Liu break; 1467a39f7afdSSong Liu } 1468e8fd52eeSShaohua Li } 1469a39f7afdSSong Liu spin_unlock(&conf->device_lock); 1470a39f7afdSSong Liu spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags); 1471a39f7afdSSong Liu } 1472f687a33eSSong Liu 1473f687a33eSSong Liu if (!test_bit(R5C_LOG_CRITICAL, &conf->cache_state)) 1474f687a33eSSong Liu r5l_run_no_space_stripes(log); 1475f687a33eSSong Liu 1476a39f7afdSSong Liu md_wakeup_thread(conf->mddev->thread); 1477a39f7afdSSong Liu } 14784b482044SShaohua Li 14790576b1c6SShaohua Li static void r5l_do_reclaim(struct r5l_log *log) 14800576b1c6SShaohua Li { 1481a39f7afdSSong Liu struct r5conf *conf = log->rdev->mddev->private; 14820576b1c6SShaohua Li sector_t reclaim_target = xchg(&log->reclaim_target, 0); 148317036461SChristoph Hellwig sector_t reclaimable; 148417036461SChristoph Hellwig sector_t next_checkpoint; 1485a39f7afdSSong Liu bool write_super; 14860576b1c6SShaohua Li 14870576b1c6SShaohua Li spin_lock_irq(&log->io_list_lock); 1488a39f7afdSSong Liu write_super = r5l_reclaimable_space(log) > log->max_free_space || 1489a39f7afdSSong Liu reclaim_target != 0 || !list_empty(&log->no_space_stripes); 14900576b1c6SShaohua Li /* 14910576b1c6SShaohua Li * move proper io_unit to reclaim list. We should not change the order. 14920576b1c6SShaohua Li * reclaimable/unreclaimable io_unit can be mixed in the list, we 14930576b1c6SShaohua Li * shouldn't reuse space of an unreclaimable io_unit 14940576b1c6SShaohua Li */ 14950576b1c6SShaohua Li while (1) { 149617036461SChristoph Hellwig reclaimable = r5l_reclaimable_space(log); 149717036461SChristoph Hellwig if (reclaimable >= reclaim_target || 14980576b1c6SShaohua Li (list_empty(&log->running_ios) && 14990576b1c6SShaohua Li list_empty(&log->io_end_ios) && 1500a8c34f91SShaohua Li list_empty(&log->flushing_ios) && 150104732f74SChristoph Hellwig list_empty(&log->finished_ios))) 15020576b1c6SShaohua Li break; 15030576b1c6SShaohua Li 150417036461SChristoph Hellwig md_wakeup_thread(log->rdev->mddev->thread); 150517036461SChristoph Hellwig wait_event_lock_irq(log->iounit_wait, 150617036461SChristoph Hellwig r5l_reclaimable_space(log) > reclaimable, 150717036461SChristoph Hellwig log->io_list_lock); 15080576b1c6SShaohua Li } 150917036461SChristoph Hellwig 1510a39f7afdSSong Liu next_checkpoint = r5c_calculate_new_cp(conf); 15110576b1c6SShaohua Li spin_unlock_irq(&log->io_list_lock); 15120576b1c6SShaohua Li 1513a39f7afdSSong Liu if (reclaimable == 0 || !write_super) 15140576b1c6SShaohua Li return; 15150576b1c6SShaohua Li 15160576b1c6SShaohua Li /* 15170576b1c6SShaohua Li * write_super will flush cache of each raid disk. We must write super 15180576b1c6SShaohua Li * here, because the log area might be reused soon and we don't want to 15190576b1c6SShaohua Li * confuse recovery 15200576b1c6SShaohua Li */ 15214b482044SShaohua Li r5l_write_super_and_discard_space(log, next_checkpoint); 15220576b1c6SShaohua Li 15230576b1c6SShaohua Li mutex_lock(&log->io_mutex); 152417036461SChristoph Hellwig log->last_checkpoint = next_checkpoint; 1525a39f7afdSSong Liu r5c_update_log_state(log); 15260576b1c6SShaohua Li mutex_unlock(&log->io_mutex); 15270576b1c6SShaohua Li 152817036461SChristoph Hellwig r5l_run_no_space_stripes(log); 15290576b1c6SShaohua Li } 15300576b1c6SShaohua Li 15310576b1c6SShaohua Li static void r5l_reclaim_thread(struct md_thread *thread) 15320576b1c6SShaohua Li { 15330576b1c6SShaohua Li struct mddev *mddev = thread->mddev; 15340576b1c6SShaohua Li struct r5conf *conf = mddev->private; 15350576b1c6SShaohua Li struct r5l_log *log = conf->log; 15360576b1c6SShaohua Li 15370576b1c6SShaohua Li if (!log) 15380576b1c6SShaohua Li return; 1539a39f7afdSSong Liu r5c_do_reclaim(conf); 15400576b1c6SShaohua Li r5l_do_reclaim(log); 15410576b1c6SShaohua Li } 15420576b1c6SShaohua Li 1543a39f7afdSSong Liu void r5l_wake_reclaim(struct r5l_log *log, sector_t space) 1544f6bed0efSShaohua Li { 15450576b1c6SShaohua Li unsigned long target; 15460576b1c6SShaohua Li unsigned long new = (unsigned long)space; /* overflow in theory */ 15470576b1c6SShaohua Li 1548a39f7afdSSong Liu if (!log) 1549a39f7afdSSong Liu return; 15500576b1c6SShaohua Li do { 15510576b1c6SShaohua Li target = log->reclaim_target; 15520576b1c6SShaohua Li if (new < target) 15530576b1c6SShaohua Li return; 15540576b1c6SShaohua Li } while (cmpxchg(&log->reclaim_target, target, new) != target); 15550576b1c6SShaohua Li md_wakeup_thread(log->reclaim_thread); 1556f6bed0efSShaohua Li } 1557f6bed0efSShaohua Li 1558e6c033f7SShaohua Li void r5l_quiesce(struct r5l_log *log, int state) 1559e6c033f7SShaohua Li { 15604b482044SShaohua Li struct mddev *mddev; 1561e6c033f7SShaohua Li if (!log || state == 2) 1562e6c033f7SShaohua Li return; 1563ce1ccd07SShaohua Li if (state == 0) 1564ce1ccd07SShaohua Li kthread_unpark(log->reclaim_thread->tsk); 1565ce1ccd07SShaohua Li else if (state == 1) { 15664b482044SShaohua Li /* make sure r5l_write_super_and_discard_space exits */ 15674b482044SShaohua Li mddev = log->rdev->mddev; 15684b482044SShaohua Li wake_up(&mddev->sb_wait); 1569ce1ccd07SShaohua Li kthread_park(log->reclaim_thread->tsk); 1570a39f7afdSSong Liu r5l_wake_reclaim(log, MaxSector); 1571e6c033f7SShaohua Li r5l_do_reclaim(log); 1572e6c033f7SShaohua Li } 1573e6c033f7SShaohua Li } 1574e6c033f7SShaohua Li 15756e74a9cfSShaohua Li bool r5l_log_disk_error(struct r5conf *conf) 15766e74a9cfSShaohua Li { 1577f6b6ec5cSShaohua Li struct r5l_log *log; 1578f6b6ec5cSShaohua Li bool ret; 15797dde2ad3SShaohua Li /* don't allow write if journal disk is missing */ 1580f6b6ec5cSShaohua Li rcu_read_lock(); 1581f6b6ec5cSShaohua Li log = rcu_dereference(conf->log); 1582f6b6ec5cSShaohua Li 1583f6b6ec5cSShaohua Li if (!log) 1584f6b6ec5cSShaohua Li ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags); 1585f6b6ec5cSShaohua Li else 1586f6b6ec5cSShaohua Li ret = test_bit(Faulty, &log->rdev->flags); 1587f6b6ec5cSShaohua Li rcu_read_unlock(); 1588f6b6ec5cSShaohua Li return ret; 15896e74a9cfSShaohua Li } 15906e74a9cfSShaohua Li 1591effe6ee7SSong Liu #define R5L_RECOVERY_PAGE_POOL_SIZE 256 1592effe6ee7SSong Liu 1593355810d1SShaohua Li struct r5l_recovery_ctx { 1594355810d1SShaohua Li struct page *meta_page; /* current meta */ 1595355810d1SShaohua Li sector_t meta_total_blocks; /* total size of current meta and data */ 1596355810d1SShaohua Li sector_t pos; /* recovery position */ 1597355810d1SShaohua Li u64 seq; /* recovery position seq */ 1598b4c625c6SSong Liu int data_parity_stripes; /* number of data_parity stripes */ 1599b4c625c6SSong Liu int data_only_stripes; /* number of data_only stripes */ 1600b4c625c6SSong Liu struct list_head cached_list; 1601effe6ee7SSong Liu 1602effe6ee7SSong Liu /* 1603effe6ee7SSong Liu * read ahead page pool (ra_pool) 1604effe6ee7SSong Liu * in recovery, log is read sequentially. It is not efficient to 1605effe6ee7SSong Liu * read every page with sync_page_io(). The read ahead page pool 1606effe6ee7SSong Liu * reads multiple pages with one IO, so further log read can 1607effe6ee7SSong Liu * just copy data from the pool. 1608effe6ee7SSong Liu */ 1609effe6ee7SSong Liu struct page *ra_pool[R5L_RECOVERY_PAGE_POOL_SIZE]; 1610effe6ee7SSong Liu sector_t pool_offset; /* offset of first page in the pool */ 1611effe6ee7SSong Liu int total_pages; /* total allocated pages */ 1612effe6ee7SSong Liu int valid_pages; /* pages with valid data */ 1613effe6ee7SSong Liu struct bio *ra_bio; /* bio to do the read ahead */ 1614355810d1SShaohua Li }; 1615355810d1SShaohua Li 1616effe6ee7SSong Liu static int r5l_recovery_allocate_ra_pool(struct r5l_log *log, 1617effe6ee7SSong Liu struct r5l_recovery_ctx *ctx) 1618effe6ee7SSong Liu { 1619effe6ee7SSong Liu struct page *page; 1620effe6ee7SSong Liu 1621effe6ee7SSong Liu ctx->ra_bio = bio_alloc_bioset(GFP_KERNEL, BIO_MAX_PAGES, log->bs); 1622effe6ee7SSong Liu if (!ctx->ra_bio) 1623effe6ee7SSong Liu return -ENOMEM; 1624effe6ee7SSong Liu 1625effe6ee7SSong Liu ctx->valid_pages = 0; 1626effe6ee7SSong Liu ctx->total_pages = 0; 1627effe6ee7SSong Liu while (ctx->total_pages < R5L_RECOVERY_PAGE_POOL_SIZE) { 1628effe6ee7SSong Liu page = alloc_page(GFP_KERNEL); 1629effe6ee7SSong Liu 1630effe6ee7SSong Liu if (!page) 1631effe6ee7SSong Liu break; 1632effe6ee7SSong Liu ctx->ra_pool[ctx->total_pages] = page; 1633effe6ee7SSong Liu ctx->total_pages += 1; 1634effe6ee7SSong Liu } 1635effe6ee7SSong Liu 1636effe6ee7SSong Liu if (ctx->total_pages == 0) { 1637effe6ee7SSong Liu bio_put(ctx->ra_bio); 1638effe6ee7SSong Liu return -ENOMEM; 1639effe6ee7SSong Liu } 1640effe6ee7SSong Liu 1641effe6ee7SSong Liu ctx->pool_offset = 0; 1642effe6ee7SSong Liu return 0; 1643effe6ee7SSong Liu } 1644effe6ee7SSong Liu 1645effe6ee7SSong Liu static void r5l_recovery_free_ra_pool(struct r5l_log *log, 1646effe6ee7SSong Liu struct r5l_recovery_ctx *ctx) 1647effe6ee7SSong Liu { 1648effe6ee7SSong Liu int i; 1649effe6ee7SSong Liu 1650effe6ee7SSong Liu for (i = 0; i < ctx->total_pages; ++i) 1651effe6ee7SSong Liu put_page(ctx->ra_pool[i]); 1652effe6ee7SSong Liu bio_put(ctx->ra_bio); 1653effe6ee7SSong Liu } 1654effe6ee7SSong Liu 1655effe6ee7SSong Liu /* 1656effe6ee7SSong Liu * fetch ctx->valid_pages pages from offset 1657effe6ee7SSong Liu * In normal cases, ctx->valid_pages == ctx->total_pages after the call. 1658effe6ee7SSong Liu * However, if the offset is close to the end of the journal device, 1659effe6ee7SSong Liu * ctx->valid_pages could be smaller than ctx->total_pages 1660effe6ee7SSong Liu */ 1661effe6ee7SSong Liu static int r5l_recovery_fetch_ra_pool(struct r5l_log *log, 1662effe6ee7SSong Liu struct r5l_recovery_ctx *ctx, 1663effe6ee7SSong Liu sector_t offset) 1664effe6ee7SSong Liu { 1665effe6ee7SSong Liu bio_reset(ctx->ra_bio); 1666effe6ee7SSong Liu ctx->ra_bio->bi_bdev = log->rdev->bdev; 1667effe6ee7SSong Liu bio_set_op_attrs(ctx->ra_bio, REQ_OP_READ, 0); 1668effe6ee7SSong Liu ctx->ra_bio->bi_iter.bi_sector = log->rdev->data_offset + offset; 1669effe6ee7SSong Liu 1670effe6ee7SSong Liu ctx->valid_pages = 0; 1671effe6ee7SSong Liu ctx->pool_offset = offset; 1672effe6ee7SSong Liu 1673effe6ee7SSong Liu while (ctx->valid_pages < ctx->total_pages) { 1674effe6ee7SSong Liu bio_add_page(ctx->ra_bio, 1675effe6ee7SSong Liu ctx->ra_pool[ctx->valid_pages], PAGE_SIZE, 0); 1676effe6ee7SSong Liu ctx->valid_pages += 1; 1677effe6ee7SSong Liu 1678effe6ee7SSong Liu offset = r5l_ring_add(log, offset, BLOCK_SECTORS); 1679effe6ee7SSong Liu 1680effe6ee7SSong Liu if (offset == 0) /* reached end of the device */ 1681effe6ee7SSong Liu break; 1682effe6ee7SSong Liu } 1683effe6ee7SSong Liu 1684effe6ee7SSong Liu return submit_bio_wait(ctx->ra_bio); 1685effe6ee7SSong Liu } 1686effe6ee7SSong Liu 1687effe6ee7SSong Liu /* 1688effe6ee7SSong Liu * try read a page from the read ahead page pool, if the page is not in the 1689effe6ee7SSong Liu * pool, call r5l_recovery_fetch_ra_pool 1690effe6ee7SSong Liu */ 1691effe6ee7SSong Liu static int r5l_recovery_read_page(struct r5l_log *log, 1692effe6ee7SSong Liu struct r5l_recovery_ctx *ctx, 1693effe6ee7SSong Liu struct page *page, 1694effe6ee7SSong Liu sector_t offset) 1695effe6ee7SSong Liu { 1696effe6ee7SSong Liu int ret; 1697effe6ee7SSong Liu 1698effe6ee7SSong Liu if (offset < ctx->pool_offset || 1699effe6ee7SSong Liu offset >= ctx->pool_offset + ctx->valid_pages * BLOCK_SECTORS) { 1700effe6ee7SSong Liu ret = r5l_recovery_fetch_ra_pool(log, ctx, offset); 1701effe6ee7SSong Liu if (ret) 1702effe6ee7SSong Liu return ret; 1703effe6ee7SSong Liu } 1704effe6ee7SSong Liu 1705effe6ee7SSong Liu BUG_ON(offset < ctx->pool_offset || 1706effe6ee7SSong Liu offset >= ctx->pool_offset + ctx->valid_pages * BLOCK_SECTORS); 1707effe6ee7SSong Liu 1708effe6ee7SSong Liu memcpy(page_address(page), 1709effe6ee7SSong Liu page_address(ctx->ra_pool[(offset - ctx->pool_offset) >> 1710effe6ee7SSong Liu BLOCK_SECTOR_SHIFT]), 1711effe6ee7SSong Liu PAGE_SIZE); 1712effe6ee7SSong Liu return 0; 1713effe6ee7SSong Liu } 1714effe6ee7SSong Liu 17159ed988f5SSong Liu static int r5l_recovery_read_meta_block(struct r5l_log *log, 1716355810d1SShaohua Li struct r5l_recovery_ctx *ctx) 1717355810d1SShaohua Li { 1718355810d1SShaohua Li struct page *page = ctx->meta_page; 1719355810d1SShaohua Li struct r5l_meta_block *mb; 1720355810d1SShaohua Li u32 crc, stored_crc; 1721effe6ee7SSong Liu int ret; 1722355810d1SShaohua Li 1723effe6ee7SSong Liu ret = r5l_recovery_read_page(log, ctx, page, ctx->pos); 1724effe6ee7SSong Liu if (ret != 0) 1725effe6ee7SSong Liu return ret; 1726355810d1SShaohua Li 1727355810d1SShaohua Li mb = page_address(page); 1728355810d1SShaohua Li stored_crc = le32_to_cpu(mb->checksum); 1729355810d1SShaohua Li mb->checksum = 0; 1730355810d1SShaohua Li 1731355810d1SShaohua Li if (le32_to_cpu(mb->magic) != R5LOG_MAGIC || 1732355810d1SShaohua Li le64_to_cpu(mb->seq) != ctx->seq || 1733355810d1SShaohua Li mb->version != R5LOG_VERSION || 1734355810d1SShaohua Li le64_to_cpu(mb->position) != ctx->pos) 1735355810d1SShaohua Li return -EINVAL; 1736355810d1SShaohua Li 17375cb2fbd6SShaohua Li crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE); 1738355810d1SShaohua Li if (stored_crc != crc) 1739355810d1SShaohua Li return -EINVAL; 1740355810d1SShaohua Li 1741355810d1SShaohua Li if (le32_to_cpu(mb->meta_size) > PAGE_SIZE) 1742355810d1SShaohua Li return -EINVAL; 1743355810d1SShaohua Li 1744355810d1SShaohua Li ctx->meta_total_blocks = BLOCK_SECTORS; 1745355810d1SShaohua Li 1746355810d1SShaohua Li return 0; 1747355810d1SShaohua Li } 1748355810d1SShaohua Li 17499ed988f5SSong Liu static void 17509ed988f5SSong Liu r5l_recovery_create_empty_meta_block(struct r5l_log *log, 17519ed988f5SSong Liu struct page *page, 17529ed988f5SSong Liu sector_t pos, u64 seq) 1753355810d1SShaohua Li { 1754355810d1SShaohua Li struct r5l_meta_block *mb; 1755355810d1SShaohua Li 1756355810d1SShaohua Li mb = page_address(page); 17579ed988f5SSong Liu clear_page(mb); 1758355810d1SShaohua Li mb->magic = cpu_to_le32(R5LOG_MAGIC); 1759355810d1SShaohua Li mb->version = R5LOG_VERSION; 1760355810d1SShaohua Li mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block)); 1761355810d1SShaohua Li mb->seq = cpu_to_le64(seq); 1762355810d1SShaohua Li mb->position = cpu_to_le64(pos); 1763355810d1SShaohua Li } 1764355810d1SShaohua Li 1765355810d1SShaohua Li static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos, 1766355810d1SShaohua Li u64 seq) 1767355810d1SShaohua Li { 1768355810d1SShaohua Li struct page *page; 1769355810d1SShaohua Li struct r5l_meta_block *mb; 1770355810d1SShaohua Li 17719ed988f5SSong Liu page = alloc_page(GFP_KERNEL); 1772355810d1SShaohua Li if (!page) 1773355810d1SShaohua Li return -ENOMEM; 17749ed988f5SSong Liu r5l_recovery_create_empty_meta_block(log, page, pos, seq); 1775355810d1SShaohua Li mb = page_address(page); 17765c88f403SSong Liu mb->checksum = cpu_to_le32(crc32c_le(log->uuid_checksum, 17775c88f403SSong Liu mb, PAGE_SIZE)); 1778796a5cf0SMike Christie if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE, 177970fd7614SChristoph Hellwig REQ_FUA, false)) { 1780355810d1SShaohua Li __free_page(page); 1781355810d1SShaohua Li return -EIO; 1782355810d1SShaohua Li } 1783355810d1SShaohua Li __free_page(page); 1784355810d1SShaohua Li return 0; 1785355810d1SShaohua Li } 1786355810d1SShaohua Li 1787b4c625c6SSong Liu /* 1788b4c625c6SSong Liu * r5l_recovery_load_data and r5l_recovery_load_parity uses flag R5_Wantwrite 1789b4c625c6SSong Liu * to mark valid (potentially not flushed) data in the journal. 1790b4c625c6SSong Liu * 1791b4c625c6SSong Liu * We already verified checksum in r5l_recovery_verify_data_checksum_for_mb, 1792b4c625c6SSong Liu * so there should not be any mismatch here. 1793b4c625c6SSong Liu */ 1794b4c625c6SSong Liu static void r5l_recovery_load_data(struct r5l_log *log, 1795b4c625c6SSong Liu struct stripe_head *sh, 1796b4c625c6SSong Liu struct r5l_recovery_ctx *ctx, 1797b4c625c6SSong Liu struct r5l_payload_data_parity *payload, 1798b4c625c6SSong Liu sector_t log_offset) 1799f6bed0efSShaohua Li { 1800b4c625c6SSong Liu struct mddev *mddev = log->rdev->mddev; 1801b4c625c6SSong Liu struct r5conf *conf = mddev->private; 1802b4c625c6SSong Liu int dd_idx; 1803355810d1SShaohua Li 1804b4c625c6SSong Liu raid5_compute_sector(conf, 1805b4c625c6SSong Liu le64_to_cpu(payload->location), 0, 1806b4c625c6SSong Liu &dd_idx, sh); 1807effe6ee7SSong Liu r5l_recovery_read_page(log, ctx, sh->dev[dd_idx].page, log_offset); 1808b4c625c6SSong Liu sh->dev[dd_idx].log_checksum = 1809b4c625c6SSong Liu le32_to_cpu(payload->checksum[0]); 1810b4c625c6SSong Liu ctx->meta_total_blocks += BLOCK_SECTORS; 1811b4c625c6SSong Liu 1812b4c625c6SSong Liu set_bit(R5_Wantwrite, &sh->dev[dd_idx].flags); 1813b4c625c6SSong Liu set_bit(STRIPE_R5C_CACHING, &sh->state); 1814b4c625c6SSong Liu } 1815b4c625c6SSong Liu 1816b4c625c6SSong Liu static void r5l_recovery_load_parity(struct r5l_log *log, 1817b4c625c6SSong Liu struct stripe_head *sh, 1818b4c625c6SSong Liu struct r5l_recovery_ctx *ctx, 1819b4c625c6SSong Liu struct r5l_payload_data_parity *payload, 1820b4c625c6SSong Liu sector_t log_offset) 1821b4c625c6SSong Liu { 1822b4c625c6SSong Liu struct mddev *mddev = log->rdev->mddev; 1823b4c625c6SSong Liu struct r5conf *conf = mddev->private; 1824b4c625c6SSong Liu 1825b4c625c6SSong Liu ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded; 1826effe6ee7SSong Liu r5l_recovery_read_page(log, ctx, sh->dev[sh->pd_idx].page, log_offset); 1827b4c625c6SSong Liu sh->dev[sh->pd_idx].log_checksum = 1828b4c625c6SSong Liu le32_to_cpu(payload->checksum[0]); 1829b4c625c6SSong Liu set_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags); 1830b4c625c6SSong Liu 1831b4c625c6SSong Liu if (sh->qd_idx >= 0) { 1832effe6ee7SSong Liu r5l_recovery_read_page( 1833effe6ee7SSong Liu log, ctx, sh->dev[sh->qd_idx].page, 1834effe6ee7SSong Liu r5l_ring_add(log, log_offset, BLOCK_SECTORS)); 1835b4c625c6SSong Liu sh->dev[sh->qd_idx].log_checksum = 1836b4c625c6SSong Liu le32_to_cpu(payload->checksum[1]); 1837b4c625c6SSong Liu set_bit(R5_Wantwrite, &sh->dev[sh->qd_idx].flags); 1838b4c625c6SSong Liu } 1839b4c625c6SSong Liu clear_bit(STRIPE_R5C_CACHING, &sh->state); 1840b4c625c6SSong Liu } 1841b4c625c6SSong Liu 1842b4c625c6SSong Liu static void r5l_recovery_reset_stripe(struct stripe_head *sh) 1843b4c625c6SSong Liu { 1844b4c625c6SSong Liu int i; 1845b4c625c6SSong Liu 1846b4c625c6SSong Liu sh->state = 0; 1847b4c625c6SSong Liu sh->log_start = MaxSector; 1848b4c625c6SSong Liu for (i = sh->disks; i--; ) 1849b4c625c6SSong Liu sh->dev[i].flags = 0; 1850b4c625c6SSong Liu } 1851b4c625c6SSong Liu 1852b4c625c6SSong Liu static void 1853b4c625c6SSong Liu r5l_recovery_replay_one_stripe(struct r5conf *conf, 1854b4c625c6SSong Liu struct stripe_head *sh, 1855b4c625c6SSong Liu struct r5l_recovery_ctx *ctx) 1856b4c625c6SSong Liu { 1857b4c625c6SSong Liu struct md_rdev *rdev, *rrdev; 1858b4c625c6SSong Liu int disk_index; 1859b4c625c6SSong Liu int data_count = 0; 1860b4c625c6SSong Liu 1861b4c625c6SSong Liu for (disk_index = 0; disk_index < sh->disks; disk_index++) { 1862b4c625c6SSong Liu if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags)) 1863b4c625c6SSong Liu continue; 1864b4c625c6SSong Liu if (disk_index == sh->qd_idx || disk_index == sh->pd_idx) 1865b4c625c6SSong Liu continue; 1866b4c625c6SSong Liu data_count++; 1867b4c625c6SSong Liu } 1868b4c625c6SSong Liu 1869b4c625c6SSong Liu /* 1870b4c625c6SSong Liu * stripes that only have parity must have been flushed 1871b4c625c6SSong Liu * before the crash that we are now recovering from, so 1872b4c625c6SSong Liu * there is nothing more to recovery. 1873b4c625c6SSong Liu */ 1874b4c625c6SSong Liu if (data_count == 0) 1875b4c625c6SSong Liu goto out; 1876b4c625c6SSong Liu 1877b4c625c6SSong Liu for (disk_index = 0; disk_index < sh->disks; disk_index++) { 1878b4c625c6SSong Liu if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags)) 1879b4c625c6SSong Liu continue; 1880b4c625c6SSong Liu 1881b4c625c6SSong Liu /* in case device is broken */ 1882b4c625c6SSong Liu rcu_read_lock(); 1883b4c625c6SSong Liu rdev = rcu_dereference(conf->disks[disk_index].rdev); 1884b4c625c6SSong Liu if (rdev) { 1885b4c625c6SSong Liu atomic_inc(&rdev->nr_pending); 1886b4c625c6SSong Liu rcu_read_unlock(); 1887b4c625c6SSong Liu sync_page_io(rdev, sh->sector, PAGE_SIZE, 1888b4c625c6SSong Liu sh->dev[disk_index].page, REQ_OP_WRITE, 0, 1889b4c625c6SSong Liu false); 1890b4c625c6SSong Liu rdev_dec_pending(rdev, rdev->mddev); 1891b4c625c6SSong Liu rcu_read_lock(); 1892b4c625c6SSong Liu } 1893b4c625c6SSong Liu rrdev = rcu_dereference(conf->disks[disk_index].replacement); 1894b4c625c6SSong Liu if (rrdev) { 1895b4c625c6SSong Liu atomic_inc(&rrdev->nr_pending); 1896b4c625c6SSong Liu rcu_read_unlock(); 1897b4c625c6SSong Liu sync_page_io(rrdev, sh->sector, PAGE_SIZE, 1898b4c625c6SSong Liu sh->dev[disk_index].page, REQ_OP_WRITE, 0, 1899b4c625c6SSong Liu false); 1900b4c625c6SSong Liu rdev_dec_pending(rrdev, rrdev->mddev); 1901b4c625c6SSong Liu rcu_read_lock(); 1902b4c625c6SSong Liu } 1903b4c625c6SSong Liu rcu_read_unlock(); 1904b4c625c6SSong Liu } 1905b4c625c6SSong Liu ctx->data_parity_stripes++; 1906b4c625c6SSong Liu out: 1907b4c625c6SSong Liu r5l_recovery_reset_stripe(sh); 1908b4c625c6SSong Liu } 1909b4c625c6SSong Liu 1910b4c625c6SSong Liu static struct stripe_head * 1911b4c625c6SSong Liu r5c_recovery_alloc_stripe(struct r5conf *conf, 19123c66abbaSSong Liu sector_t stripe_sect) 1913b4c625c6SSong Liu { 1914b4c625c6SSong Liu struct stripe_head *sh; 1915b4c625c6SSong Liu 1916b4c625c6SSong Liu sh = raid5_get_active_stripe(conf, stripe_sect, 0, 1, 0); 1917b4c625c6SSong Liu if (!sh) 1918b4c625c6SSong Liu return NULL; /* no more stripe available */ 1919b4c625c6SSong Liu 1920b4c625c6SSong Liu r5l_recovery_reset_stripe(sh); 1921b4c625c6SSong Liu 1922b4c625c6SSong Liu return sh; 1923b4c625c6SSong Liu } 1924b4c625c6SSong Liu 1925b4c625c6SSong Liu static struct stripe_head * 1926b4c625c6SSong Liu r5c_recovery_lookup_stripe(struct list_head *list, sector_t sect) 1927b4c625c6SSong Liu { 1928b4c625c6SSong Liu struct stripe_head *sh; 1929b4c625c6SSong Liu 1930b4c625c6SSong Liu list_for_each_entry(sh, list, lru) 1931b4c625c6SSong Liu if (sh->sector == sect) 1932b4c625c6SSong Liu return sh; 1933b4c625c6SSong Liu return NULL; 1934b4c625c6SSong Liu } 1935b4c625c6SSong Liu 1936b4c625c6SSong Liu static void 1937b4c625c6SSong Liu r5c_recovery_drop_stripes(struct list_head *cached_stripe_list, 1938b4c625c6SSong Liu struct r5l_recovery_ctx *ctx) 1939b4c625c6SSong Liu { 1940b4c625c6SSong Liu struct stripe_head *sh, *next; 1941b4c625c6SSong Liu 1942b4c625c6SSong Liu list_for_each_entry_safe(sh, next, cached_stripe_list, lru) { 1943b4c625c6SSong Liu r5l_recovery_reset_stripe(sh); 1944b4c625c6SSong Liu list_del_init(&sh->lru); 1945b4c625c6SSong Liu raid5_release_stripe(sh); 1946b4c625c6SSong Liu } 1947b4c625c6SSong Liu } 1948b4c625c6SSong Liu 1949b4c625c6SSong Liu static void 1950b4c625c6SSong Liu r5c_recovery_replay_stripes(struct list_head *cached_stripe_list, 1951b4c625c6SSong Liu struct r5l_recovery_ctx *ctx) 1952b4c625c6SSong Liu { 1953b4c625c6SSong Liu struct stripe_head *sh, *next; 1954b4c625c6SSong Liu 1955b4c625c6SSong Liu list_for_each_entry_safe(sh, next, cached_stripe_list, lru) 1956b4c625c6SSong Liu if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) { 1957b4c625c6SSong Liu r5l_recovery_replay_one_stripe(sh->raid_conf, sh, ctx); 1958b4c625c6SSong Liu list_del_init(&sh->lru); 1959b4c625c6SSong Liu raid5_release_stripe(sh); 1960b4c625c6SSong Liu } 1961b4c625c6SSong Liu } 1962b4c625c6SSong Liu 1963b4c625c6SSong Liu /* if matches return 0; otherwise return -EINVAL */ 1964b4c625c6SSong Liu static int 1965effe6ee7SSong Liu r5l_recovery_verify_data_checksum(struct r5l_log *log, 1966effe6ee7SSong Liu struct r5l_recovery_ctx *ctx, 1967effe6ee7SSong Liu struct page *page, 1968b4c625c6SSong Liu sector_t log_offset, __le32 log_checksum) 1969b4c625c6SSong Liu { 1970b4c625c6SSong Liu void *addr; 1971b4c625c6SSong Liu u32 checksum; 1972b4c625c6SSong Liu 1973effe6ee7SSong Liu r5l_recovery_read_page(log, ctx, page, log_offset); 1974b4c625c6SSong Liu addr = kmap_atomic(page); 1975b4c625c6SSong Liu checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE); 1976b4c625c6SSong Liu kunmap_atomic(addr); 1977b4c625c6SSong Liu return (le32_to_cpu(log_checksum) == checksum) ? 0 : -EINVAL; 1978b4c625c6SSong Liu } 1979b4c625c6SSong Liu 1980b4c625c6SSong Liu /* 1981b4c625c6SSong Liu * before loading data to stripe cache, we need verify checksum for all data, 1982b4c625c6SSong Liu * if there is mismatch for any data page, we drop all data in the mata block 1983b4c625c6SSong Liu */ 1984b4c625c6SSong Liu static int 1985b4c625c6SSong Liu r5l_recovery_verify_data_checksum_for_mb(struct r5l_log *log, 1986b4c625c6SSong Liu struct r5l_recovery_ctx *ctx) 1987b4c625c6SSong Liu { 1988b4c625c6SSong Liu struct mddev *mddev = log->rdev->mddev; 1989b4c625c6SSong Liu struct r5conf *conf = mddev->private; 1990b4c625c6SSong Liu struct r5l_meta_block *mb = page_address(ctx->meta_page); 1991b4c625c6SSong Liu sector_t mb_offset = sizeof(struct r5l_meta_block); 1992b4c625c6SSong Liu sector_t log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS); 1993b4c625c6SSong Liu struct page *page; 1994b4c625c6SSong Liu struct r5l_payload_data_parity *payload; 19952d4f4687SSong Liu struct r5l_payload_flush *payload_flush; 1996b4c625c6SSong Liu 1997b4c625c6SSong Liu page = alloc_page(GFP_KERNEL); 1998b4c625c6SSong Liu if (!page) 1999355810d1SShaohua Li return -ENOMEM; 2000355810d1SShaohua Li 2001b4c625c6SSong Liu while (mb_offset < le32_to_cpu(mb->meta_size)) { 2002b4c625c6SSong Liu payload = (void *)mb + mb_offset; 20032d4f4687SSong Liu payload_flush = (void *)mb + mb_offset; 2004b4c625c6SSong Liu 20051ad45a9bSJason Yan if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) { 2006b4c625c6SSong Liu if (r5l_recovery_verify_data_checksum( 2007effe6ee7SSong Liu log, ctx, page, log_offset, 2008b4c625c6SSong Liu payload->checksum[0]) < 0) 2009b4c625c6SSong Liu goto mismatch; 20101ad45a9bSJason Yan } else if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY) { 2011b4c625c6SSong Liu if (r5l_recovery_verify_data_checksum( 2012effe6ee7SSong Liu log, ctx, page, log_offset, 2013b4c625c6SSong Liu payload->checksum[0]) < 0) 2014b4c625c6SSong Liu goto mismatch; 2015b4c625c6SSong Liu if (conf->max_degraded == 2 && /* q for RAID 6 */ 2016b4c625c6SSong Liu r5l_recovery_verify_data_checksum( 2017effe6ee7SSong Liu log, ctx, page, 2018b4c625c6SSong Liu r5l_ring_add(log, log_offset, 2019b4c625c6SSong Liu BLOCK_SECTORS), 2020b4c625c6SSong Liu payload->checksum[1]) < 0) 2021b4c625c6SSong Liu goto mismatch; 20221ad45a9bSJason Yan } else if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_FLUSH) { 20232d4f4687SSong Liu /* nothing to do for R5LOG_PAYLOAD_FLUSH here */ 20242d4f4687SSong Liu } else /* not R5LOG_PAYLOAD_DATA/PARITY/FLUSH */ 2025b4c625c6SSong Liu goto mismatch; 2026b4c625c6SSong Liu 20271ad45a9bSJason Yan if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_FLUSH) { 20282d4f4687SSong Liu mb_offset += sizeof(struct r5l_payload_flush) + 20292d4f4687SSong Liu le32_to_cpu(payload_flush->size); 20302d4f4687SSong Liu } else { 20312d4f4687SSong Liu /* DATA or PARITY payload */ 2032b4c625c6SSong Liu log_offset = r5l_ring_add(log, log_offset, 2033b4c625c6SSong Liu le32_to_cpu(payload->size)); 2034b4c625c6SSong Liu mb_offset += sizeof(struct r5l_payload_data_parity) + 2035b4c625c6SSong Liu sizeof(__le32) * 2036b4c625c6SSong Liu (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9)); 2037b4c625c6SSong Liu } 2038b4c625c6SSong Liu 20392d4f4687SSong Liu } 20402d4f4687SSong Liu 2041b4c625c6SSong Liu put_page(page); 2042b4c625c6SSong Liu return 0; 2043b4c625c6SSong Liu 2044b4c625c6SSong Liu mismatch: 2045b4c625c6SSong Liu put_page(page); 2046b4c625c6SSong Liu return -EINVAL; 2047b4c625c6SSong Liu } 2048b4c625c6SSong Liu 2049b4c625c6SSong Liu /* 2050b4c625c6SSong Liu * Analyze all data/parity pages in one meta block 2051b4c625c6SSong Liu * Returns: 2052b4c625c6SSong Liu * 0 for success 2053b4c625c6SSong Liu * -EINVAL for unknown playload type 2054b4c625c6SSong Liu * -EAGAIN for checksum mismatch of data page 2055b4c625c6SSong Liu * -ENOMEM for run out of memory (alloc_page failed or run out of stripes) 2056b4c625c6SSong Liu */ 2057b4c625c6SSong Liu static int 2058b4c625c6SSong Liu r5c_recovery_analyze_meta_block(struct r5l_log *log, 2059b4c625c6SSong Liu struct r5l_recovery_ctx *ctx, 2060b4c625c6SSong Liu struct list_head *cached_stripe_list) 2061b4c625c6SSong Liu { 2062b4c625c6SSong Liu struct mddev *mddev = log->rdev->mddev; 2063b4c625c6SSong Liu struct r5conf *conf = mddev->private; 2064b4c625c6SSong Liu struct r5l_meta_block *mb; 2065b4c625c6SSong Liu struct r5l_payload_data_parity *payload; 20662d4f4687SSong Liu struct r5l_payload_flush *payload_flush; 2067b4c625c6SSong Liu int mb_offset; 2068b4c625c6SSong Liu sector_t log_offset; 2069b4c625c6SSong Liu sector_t stripe_sect; 2070b4c625c6SSong Liu struct stripe_head *sh; 2071b4c625c6SSong Liu int ret; 2072b4c625c6SSong Liu 2073b4c625c6SSong Liu /* 2074b4c625c6SSong Liu * for mismatch in data blocks, we will drop all data in this mb, but 2075b4c625c6SSong Liu * we will still read next mb for other data with FLUSH flag, as 2076b4c625c6SSong Liu * io_unit could finish out of order. 2077b4c625c6SSong Liu */ 2078b4c625c6SSong Liu ret = r5l_recovery_verify_data_checksum_for_mb(log, ctx); 2079b4c625c6SSong Liu if (ret == -EINVAL) 2080b4c625c6SSong Liu return -EAGAIN; 2081b4c625c6SSong Liu else if (ret) 2082b4c625c6SSong Liu return ret; /* -ENOMEM duo to alloc_page() failed */ 2083b4c625c6SSong Liu 2084b4c625c6SSong Liu mb = page_address(ctx->meta_page); 2085b4c625c6SSong Liu mb_offset = sizeof(struct r5l_meta_block); 2086b4c625c6SSong Liu log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS); 2087b4c625c6SSong Liu 2088b4c625c6SSong Liu while (mb_offset < le32_to_cpu(mb->meta_size)) { 2089b4c625c6SSong Liu int dd; 2090b4c625c6SSong Liu 2091b4c625c6SSong Liu payload = (void *)mb + mb_offset; 20922d4f4687SSong Liu payload_flush = (void *)mb + mb_offset; 20932d4f4687SSong Liu 20941ad45a9bSJason Yan if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_FLUSH) { 20952d4f4687SSong Liu int i, count; 20962d4f4687SSong Liu 20972d4f4687SSong Liu count = le32_to_cpu(payload_flush->size) / sizeof(__le64); 20982d4f4687SSong Liu for (i = 0; i < count; ++i) { 20992d4f4687SSong Liu stripe_sect = le64_to_cpu(payload_flush->flush_stripes[i]); 21002d4f4687SSong Liu sh = r5c_recovery_lookup_stripe(cached_stripe_list, 21012d4f4687SSong Liu stripe_sect); 21022d4f4687SSong Liu if (sh) { 21032d4f4687SSong Liu WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state)); 21042d4f4687SSong Liu r5l_recovery_reset_stripe(sh); 21052d4f4687SSong Liu list_del_init(&sh->lru); 21062d4f4687SSong Liu raid5_release_stripe(sh); 21072d4f4687SSong Liu } 21082d4f4687SSong Liu } 21092d4f4687SSong Liu 21102d4f4687SSong Liu mb_offset += sizeof(struct r5l_payload_flush) + 21112d4f4687SSong Liu le32_to_cpu(payload_flush->size); 21122d4f4687SSong Liu continue; 21132d4f4687SSong Liu } 21142d4f4687SSong Liu 21152d4f4687SSong Liu /* DATA or PARITY payload */ 21161ad45a9bSJason Yan stripe_sect = (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) ? 2117b4c625c6SSong Liu raid5_compute_sector( 2118b4c625c6SSong Liu conf, le64_to_cpu(payload->location), 0, &dd, 2119b4c625c6SSong Liu NULL) 2120b4c625c6SSong Liu : le64_to_cpu(payload->location); 2121b4c625c6SSong Liu 2122b4c625c6SSong Liu sh = r5c_recovery_lookup_stripe(cached_stripe_list, 2123b4c625c6SSong Liu stripe_sect); 2124b4c625c6SSong Liu 2125b4c625c6SSong Liu if (!sh) { 21263c66abbaSSong Liu sh = r5c_recovery_alloc_stripe(conf, stripe_sect); 2127b4c625c6SSong Liu /* 2128b4c625c6SSong Liu * cannot get stripe from raid5_get_active_stripe 2129b4c625c6SSong Liu * try replay some stripes 2130b4c625c6SSong Liu */ 2131b4c625c6SSong Liu if (!sh) { 2132b4c625c6SSong Liu r5c_recovery_replay_stripes( 2133b4c625c6SSong Liu cached_stripe_list, ctx); 2134b4c625c6SSong Liu sh = r5c_recovery_alloc_stripe( 21353c66abbaSSong Liu conf, stripe_sect); 2136b4c625c6SSong Liu } 2137b4c625c6SSong Liu if (!sh) { 2138b4c625c6SSong Liu pr_debug("md/raid:%s: Increasing stripe cache size to %d to recovery data on journal.\n", 2139b4c625c6SSong Liu mdname(mddev), 2140b4c625c6SSong Liu conf->min_nr_stripes * 2); 2141b4c625c6SSong Liu raid5_set_cache_size(mddev, 2142b4c625c6SSong Liu conf->min_nr_stripes * 2); 21433c66abbaSSong Liu sh = r5c_recovery_alloc_stripe(conf, 21443c66abbaSSong Liu stripe_sect); 2145b4c625c6SSong Liu } 2146b4c625c6SSong Liu if (!sh) { 2147b4c625c6SSong Liu pr_err("md/raid:%s: Cannot get enough stripes due to memory pressure. Recovery failed.\n", 2148b4c625c6SSong Liu mdname(mddev)); 2149b4c625c6SSong Liu return -ENOMEM; 2150b4c625c6SSong Liu } 2151b4c625c6SSong Liu list_add_tail(&sh->lru, cached_stripe_list); 2152b4c625c6SSong Liu } 2153b4c625c6SSong Liu 21541ad45a9bSJason Yan if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_DATA) { 2155f7b7bee7SZhengyuan Liu if (!test_bit(STRIPE_R5C_CACHING, &sh->state) && 2156f7b7bee7SZhengyuan Liu test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags)) { 2157b4c625c6SSong Liu r5l_recovery_replay_one_stripe(conf, sh, ctx); 2158b4c625c6SSong Liu list_move_tail(&sh->lru, cached_stripe_list); 2159b4c625c6SSong Liu } 2160b4c625c6SSong Liu r5l_recovery_load_data(log, sh, ctx, payload, 2161b4c625c6SSong Liu log_offset); 21621ad45a9bSJason Yan } else if (le16_to_cpu(payload->header.type) == R5LOG_PAYLOAD_PARITY) 2163b4c625c6SSong Liu r5l_recovery_load_parity(log, sh, ctx, payload, 2164b4c625c6SSong Liu log_offset); 2165b4c625c6SSong Liu else 2166b4c625c6SSong Liu return -EINVAL; 2167b4c625c6SSong Liu 2168b4c625c6SSong Liu log_offset = r5l_ring_add(log, log_offset, 2169b4c625c6SSong Liu le32_to_cpu(payload->size)); 2170b4c625c6SSong Liu 2171b4c625c6SSong Liu mb_offset += sizeof(struct r5l_payload_data_parity) + 2172b4c625c6SSong Liu sizeof(__le32) * 2173b4c625c6SSong Liu (le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9)); 2174b4c625c6SSong Liu } 2175b4c625c6SSong Liu 2176b4c625c6SSong Liu return 0; 2177b4c625c6SSong Liu } 2178b4c625c6SSong Liu 2179b4c625c6SSong Liu /* 2180b4c625c6SSong Liu * Load the stripe into cache. The stripe will be written out later by 2181b4c625c6SSong Liu * the stripe cache state machine. 2182b4c625c6SSong Liu */ 2183b4c625c6SSong Liu static void r5c_recovery_load_one_stripe(struct r5l_log *log, 2184b4c625c6SSong Liu struct stripe_head *sh) 2185b4c625c6SSong Liu { 2186b4c625c6SSong Liu struct r5dev *dev; 2187b4c625c6SSong Liu int i; 2188b4c625c6SSong Liu 2189b4c625c6SSong Liu for (i = sh->disks; i--; ) { 2190b4c625c6SSong Liu dev = sh->dev + i; 2191b4c625c6SSong Liu if (test_and_clear_bit(R5_Wantwrite, &dev->flags)) { 2192b4c625c6SSong Liu set_bit(R5_InJournal, &dev->flags); 2193b4c625c6SSong Liu set_bit(R5_UPTODATE, &dev->flags); 2194b4c625c6SSong Liu } 2195b4c625c6SSong Liu } 2196b4c625c6SSong Liu } 2197b4c625c6SSong Liu 2198b4c625c6SSong Liu /* 2199b4c625c6SSong Liu * Scan through the log for all to-be-flushed data 2200b4c625c6SSong Liu * 2201b4c625c6SSong Liu * For stripes with data and parity, namely Data-Parity stripe 2202b4c625c6SSong Liu * (STRIPE_R5C_CACHING == 0), we simply replay all the writes. 2203b4c625c6SSong Liu * 2204b4c625c6SSong Liu * For stripes with only data, namely Data-Only stripe 2205b4c625c6SSong Liu * (STRIPE_R5C_CACHING == 1), we load them to stripe cache state machine. 2206b4c625c6SSong Liu * 2207b4c625c6SSong Liu * For a stripe, if we see data after parity, we should discard all previous 2208b4c625c6SSong Liu * data and parity for this stripe, as these data are already flushed to 2209b4c625c6SSong Liu * the array. 2210b4c625c6SSong Liu * 2211b4c625c6SSong Liu * At the end of the scan, we return the new journal_tail, which points to 2212b4c625c6SSong Liu * first data-only stripe on the journal device, or next invalid meta block. 2213b4c625c6SSong Liu */ 2214b4c625c6SSong Liu static int r5c_recovery_flush_log(struct r5l_log *log, 2215b4c625c6SSong Liu struct r5l_recovery_ctx *ctx) 2216b4c625c6SSong Liu { 2217bc8f167fSJackieLiu struct stripe_head *sh; 2218b4c625c6SSong Liu int ret = 0; 2219b4c625c6SSong Liu 2220b4c625c6SSong Liu /* scan through the log */ 2221b4c625c6SSong Liu while (1) { 2222b4c625c6SSong Liu if (r5l_recovery_read_meta_block(log, ctx)) 2223b4c625c6SSong Liu break; 2224b4c625c6SSong Liu 2225b4c625c6SSong Liu ret = r5c_recovery_analyze_meta_block(log, ctx, 2226b4c625c6SSong Liu &ctx->cached_list); 2227b4c625c6SSong Liu /* 2228b4c625c6SSong Liu * -EAGAIN means mismatch in data block, in this case, we still 2229b4c625c6SSong Liu * try scan the next metablock 2230b4c625c6SSong Liu */ 2231b4c625c6SSong Liu if (ret && ret != -EAGAIN) 2232b4c625c6SSong Liu break; /* ret == -EINVAL or -ENOMEM */ 2233b4c625c6SSong Liu ctx->seq++; 2234b4c625c6SSong Liu ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks); 2235b4c625c6SSong Liu } 2236b4c625c6SSong Liu 2237b4c625c6SSong Liu if (ret == -ENOMEM) { 2238b4c625c6SSong Liu r5c_recovery_drop_stripes(&ctx->cached_list, ctx); 2239b4c625c6SSong Liu return ret; 2240b4c625c6SSong Liu } 2241b4c625c6SSong Liu 2242b4c625c6SSong Liu /* replay data-parity stripes */ 2243b4c625c6SSong Liu r5c_recovery_replay_stripes(&ctx->cached_list, ctx); 2244b4c625c6SSong Liu 2245b4c625c6SSong Liu /* load data-only stripes to stripe cache */ 2246bc8f167fSJackieLiu list_for_each_entry(sh, &ctx->cached_list, lru) { 2247b4c625c6SSong Liu WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); 2248b4c625c6SSong Liu r5c_recovery_load_one_stripe(log, sh); 2249b4c625c6SSong Liu ctx->data_only_stripes++; 2250b4c625c6SSong Liu } 2251b4c625c6SSong Liu 2252b4c625c6SSong Liu return 0; 2253b4c625c6SSong Liu } 2254355810d1SShaohua Li 2255355810d1SShaohua Li /* 2256355810d1SShaohua Li * we did a recovery. Now ctx.pos points to an invalid meta block. New 2257355810d1SShaohua Li * log will start here. but we can't let superblock point to last valid 2258355810d1SShaohua Li * meta block. The log might looks like: 2259355810d1SShaohua Li * | meta 1| meta 2| meta 3| 2260355810d1SShaohua Li * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If 2261355810d1SShaohua Li * superblock points to meta 1, we write a new valid meta 2n. if crash 2262355810d1SShaohua Li * happens again, new recovery will start from meta 1. Since meta 2n is 2263355810d1SShaohua Li * valid now, recovery will think meta 3 is valid, which is wrong. 2264355810d1SShaohua Li * The solution is we create a new meta in meta2 with its seq == meta 22653c6edc66SSong Liu * 1's seq + 10000 and let superblock points to meta2. The same recovery 22663c6edc66SSong Liu * will not think meta 3 is a valid meta, because its seq doesn't match 2267355810d1SShaohua Li */ 2268355810d1SShaohua Li 2269b4c625c6SSong Liu /* 2270b4c625c6SSong Liu * Before recovery, the log looks like the following 2271b4c625c6SSong Liu * 2272b4c625c6SSong Liu * --------------------------------------------- 2273b4c625c6SSong Liu * | valid log | invalid log | 2274b4c625c6SSong Liu * --------------------------------------------- 2275b4c625c6SSong Liu * ^ 2276b4c625c6SSong Liu * |- log->last_checkpoint 2277b4c625c6SSong Liu * |- log->last_cp_seq 2278b4c625c6SSong Liu * 2279b4c625c6SSong Liu * Now we scan through the log until we see invalid entry 2280b4c625c6SSong Liu * 2281b4c625c6SSong Liu * --------------------------------------------- 2282b4c625c6SSong Liu * | valid log | invalid log | 2283b4c625c6SSong Liu * --------------------------------------------- 2284b4c625c6SSong Liu * ^ ^ 2285b4c625c6SSong Liu * |- log->last_checkpoint |- ctx->pos 2286b4c625c6SSong Liu * |- log->last_cp_seq |- ctx->seq 2287b4c625c6SSong Liu * 2288b4c625c6SSong Liu * From this point, we need to increase seq number by 10 to avoid 2289b4c625c6SSong Liu * confusing next recovery. 2290b4c625c6SSong Liu * 2291b4c625c6SSong Liu * --------------------------------------------- 2292b4c625c6SSong Liu * | valid log | invalid log | 2293b4c625c6SSong Liu * --------------------------------------------- 2294b4c625c6SSong Liu * ^ ^ 2295b4c625c6SSong Liu * |- log->last_checkpoint |- ctx->pos+1 22963c6edc66SSong Liu * |- log->last_cp_seq |- ctx->seq+10001 2297b4c625c6SSong Liu * 2298b4c625c6SSong Liu * However, it is not safe to start the state machine yet, because data only 2299b4c625c6SSong Liu * parities are not yet secured in RAID. To save these data only parities, we 2300b4c625c6SSong Liu * rewrite them from seq+11. 2301b4c625c6SSong Liu * 2302b4c625c6SSong Liu * ----------------------------------------------------------------- 2303b4c625c6SSong Liu * | valid log | data only stripes | invalid log | 2304b4c625c6SSong Liu * ----------------------------------------------------------------- 2305b4c625c6SSong Liu * ^ ^ 2306b4c625c6SSong Liu * |- log->last_checkpoint |- ctx->pos+n 23073c6edc66SSong Liu * |- log->last_cp_seq |- ctx->seq+10000+n 2308b4c625c6SSong Liu * 2309b4c625c6SSong Liu * If failure happens again during this process, the recovery can safe start 2310b4c625c6SSong Liu * again from log->last_checkpoint. 2311b4c625c6SSong Liu * 2312b4c625c6SSong Liu * Once data only stripes are rewritten to journal, we move log_tail 2313b4c625c6SSong Liu * 2314b4c625c6SSong Liu * ----------------------------------------------------------------- 2315b4c625c6SSong Liu * | old log | data only stripes | invalid log | 2316b4c625c6SSong Liu * ----------------------------------------------------------------- 2317b4c625c6SSong Liu * ^ ^ 2318b4c625c6SSong Liu * |- log->last_checkpoint |- ctx->pos+n 23193c6edc66SSong Liu * |- log->last_cp_seq |- ctx->seq+10000+n 2320b4c625c6SSong Liu * 2321b4c625c6SSong Liu * Then we can safely start the state machine. If failure happens from this 2322b4c625c6SSong Liu * point on, the recovery will start from new log->last_checkpoint. 2323b4c625c6SSong Liu */ 2324b4c625c6SSong Liu static int 2325b4c625c6SSong Liu r5c_recovery_rewrite_data_only_stripes(struct r5l_log *log, 2326b4c625c6SSong Liu struct r5l_recovery_ctx *ctx) 2327b4c625c6SSong Liu { 2328a85dd7b8SSong Liu struct stripe_head *sh; 2329b4c625c6SSong Liu struct mddev *mddev = log->rdev->mddev; 2330b4c625c6SSong Liu struct page *page; 23313c66abbaSSong Liu sector_t next_checkpoint = MaxSector; 2332b4c625c6SSong Liu 2333b4c625c6SSong Liu page = alloc_page(GFP_KERNEL); 2334b4c625c6SSong Liu if (!page) { 2335b4c625c6SSong Liu pr_err("md/raid:%s: cannot allocate memory to rewrite data only stripes\n", 2336b4c625c6SSong Liu mdname(mddev)); 2337b4c625c6SSong Liu return -ENOMEM; 2338b4c625c6SSong Liu } 2339b4c625c6SSong Liu 23403c66abbaSSong Liu WARN_ON(list_empty(&ctx->cached_list)); 23413c66abbaSSong Liu 2342a85dd7b8SSong Liu list_for_each_entry(sh, &ctx->cached_list, lru) { 2343b4c625c6SSong Liu struct r5l_meta_block *mb; 2344b4c625c6SSong Liu int i; 2345b4c625c6SSong Liu int offset; 2346b4c625c6SSong Liu sector_t write_pos; 2347b4c625c6SSong Liu 2348b4c625c6SSong Liu WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state)); 2349b4c625c6SSong Liu r5l_recovery_create_empty_meta_block(log, page, 2350b4c625c6SSong Liu ctx->pos, ctx->seq); 2351b4c625c6SSong Liu mb = page_address(page); 2352b4c625c6SSong Liu offset = le32_to_cpu(mb->meta_size); 2353fc833c2aSJackieLiu write_pos = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS); 2354b4c625c6SSong Liu 2355b4c625c6SSong Liu for (i = sh->disks; i--; ) { 2356b4c625c6SSong Liu struct r5dev *dev = &sh->dev[i]; 2357b4c625c6SSong Liu struct r5l_payload_data_parity *payload; 2358b4c625c6SSong Liu void *addr; 2359b4c625c6SSong Liu 2360b4c625c6SSong Liu if (test_bit(R5_InJournal, &dev->flags)) { 2361b4c625c6SSong Liu payload = (void *)mb + offset; 2362b4c625c6SSong Liu payload->header.type = cpu_to_le16( 2363b4c625c6SSong Liu R5LOG_PAYLOAD_DATA); 23641ad45a9bSJason Yan payload->size = cpu_to_le32(BLOCK_SECTORS); 2365b4c625c6SSong Liu payload->location = cpu_to_le64( 2366b4c625c6SSong Liu raid5_compute_blocknr(sh, i, 0)); 2367b4c625c6SSong Liu addr = kmap_atomic(dev->page); 2368b4c625c6SSong Liu payload->checksum[0] = cpu_to_le32( 2369b4c625c6SSong Liu crc32c_le(log->uuid_checksum, addr, 2370b4c625c6SSong Liu PAGE_SIZE)); 2371b4c625c6SSong Liu kunmap_atomic(addr); 2372b4c625c6SSong Liu sync_page_io(log->rdev, write_pos, PAGE_SIZE, 2373b4c625c6SSong Liu dev->page, REQ_OP_WRITE, 0, false); 2374b4c625c6SSong Liu write_pos = r5l_ring_add(log, write_pos, 2375b4c625c6SSong Liu BLOCK_SECTORS); 2376b4c625c6SSong Liu offset += sizeof(__le32) + 2377b4c625c6SSong Liu sizeof(struct r5l_payload_data_parity); 2378b4c625c6SSong Liu 2379b4c625c6SSong Liu } 2380b4c625c6SSong Liu } 2381b4c625c6SSong Liu mb->meta_size = cpu_to_le32(offset); 23825c88f403SSong Liu mb->checksum = cpu_to_le32(crc32c_le(log->uuid_checksum, 23835c88f403SSong Liu mb, PAGE_SIZE)); 2384b4c625c6SSong Liu sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, 238520737738SShaohua Li REQ_OP_WRITE, REQ_FUA, false); 2386b4c625c6SSong Liu sh->log_start = ctx->pos; 23873c66abbaSSong Liu list_add_tail(&sh->r5c, &log->stripe_in_journal_list); 23883c66abbaSSong Liu atomic_inc(&log->stripe_in_journal_count); 2389b4c625c6SSong Liu ctx->pos = write_pos; 2390b4c625c6SSong Liu ctx->seq += 1; 23913c66abbaSSong Liu next_checkpoint = sh->log_start; 2392b4c625c6SSong Liu } 23933c66abbaSSong Liu log->next_checkpoint = next_checkpoint; 2394b4c625c6SSong Liu __free_page(page); 2395b4c625c6SSong Liu return 0; 2396b4c625c6SSong Liu } 2397b4c625c6SSong Liu 2398a85dd7b8SSong Liu static void r5c_recovery_flush_data_only_stripes(struct r5l_log *log, 2399a85dd7b8SSong Liu struct r5l_recovery_ctx *ctx) 2400a85dd7b8SSong Liu { 2401a85dd7b8SSong Liu struct mddev *mddev = log->rdev->mddev; 2402a85dd7b8SSong Liu struct r5conf *conf = mddev->private; 2403a85dd7b8SSong Liu struct stripe_head *sh, *next; 2404a85dd7b8SSong Liu 2405a85dd7b8SSong Liu if (ctx->data_only_stripes == 0) 2406a85dd7b8SSong Liu return; 2407a85dd7b8SSong Liu 2408a85dd7b8SSong Liu log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_BACK; 2409a85dd7b8SSong Liu 2410a85dd7b8SSong Liu list_for_each_entry_safe(sh, next, &ctx->cached_list, lru) { 2411a85dd7b8SSong Liu r5c_make_stripe_write_out(sh); 2412a85dd7b8SSong Liu set_bit(STRIPE_HANDLE, &sh->state); 2413a85dd7b8SSong Liu list_del_init(&sh->lru); 2414a85dd7b8SSong Liu raid5_release_stripe(sh); 2415a85dd7b8SSong Liu } 2416a85dd7b8SSong Liu 2417a85dd7b8SSong Liu md_wakeup_thread(conf->mddev->thread); 2418a85dd7b8SSong Liu /* reuse conf->wait_for_quiescent in recovery */ 2419a85dd7b8SSong Liu wait_event(conf->wait_for_quiescent, 2420a85dd7b8SSong Liu atomic_read(&conf->active_stripes) == 0); 2421a85dd7b8SSong Liu 2422a85dd7b8SSong Liu log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH; 2423a85dd7b8SSong Liu } 2424a85dd7b8SSong Liu 2425f6bed0efSShaohua Li static int r5l_recovery_log(struct r5l_log *log) 2426f6bed0efSShaohua Li { 24275aabf7c4SSong Liu struct mddev *mddev = log->rdev->mddev; 2428effe6ee7SSong Liu struct r5l_recovery_ctx *ctx; 24295aabf7c4SSong Liu int ret; 243043b96748SJackieLiu sector_t pos; 2431355810d1SShaohua Li 2432effe6ee7SSong Liu ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 2433effe6ee7SSong Liu if (!ctx) 2434355810d1SShaohua Li return -ENOMEM; 2435355810d1SShaohua Li 2436effe6ee7SSong Liu ctx->pos = log->last_checkpoint; 2437effe6ee7SSong Liu ctx->seq = log->last_cp_seq; 2438effe6ee7SSong Liu INIT_LIST_HEAD(&ctx->cached_list); 2439effe6ee7SSong Liu ctx->meta_page = alloc_page(GFP_KERNEL); 2440effe6ee7SSong Liu 2441effe6ee7SSong Liu if (!ctx->meta_page) { 2442effe6ee7SSong Liu ret = -ENOMEM; 2443effe6ee7SSong Liu goto meta_page; 2444effe6ee7SSong Liu } 2445effe6ee7SSong Liu 2446effe6ee7SSong Liu if (r5l_recovery_allocate_ra_pool(log, ctx) != 0) { 2447effe6ee7SSong Liu ret = -ENOMEM; 2448effe6ee7SSong Liu goto ra_pool; 2449effe6ee7SSong Liu } 2450effe6ee7SSong Liu 2451effe6ee7SSong Liu ret = r5c_recovery_flush_log(log, ctx); 2452355810d1SShaohua Li 2453355810d1SShaohua Li if (ret) 2454effe6ee7SSong Liu goto error; 24555aabf7c4SSong Liu 2456effe6ee7SSong Liu pos = ctx->pos; 2457effe6ee7SSong Liu ctx->seq += 10000; 245843b96748SJackieLiu 2459effe6ee7SSong Liu if ((ctx->data_only_stripes == 0) && (ctx->data_parity_stripes == 0)) 24605aabf7c4SSong Liu pr_debug("md/raid:%s: starting from clean shutdown\n", 24615aabf7c4SSong Liu mdname(mddev)); 2462a85dd7b8SSong Liu else 246399f17890SColin Ian King pr_debug("md/raid:%s: recovering %d data-only stripes and %d data-parity stripes\n", 2464effe6ee7SSong Liu mdname(mddev), ctx->data_only_stripes, 2465effe6ee7SSong Liu ctx->data_parity_stripes); 24665aabf7c4SSong Liu 2467effe6ee7SSong Liu if (ctx->data_only_stripes == 0) { 2468effe6ee7SSong Liu log->next_checkpoint = ctx->pos; 2469effe6ee7SSong Liu r5l_log_write_empty_meta_block(log, ctx->pos, ctx->seq++); 2470effe6ee7SSong Liu ctx->pos = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS); 2471effe6ee7SSong Liu } else if (r5c_recovery_rewrite_data_only_stripes(log, ctx)) { 24725aabf7c4SSong Liu pr_err("md/raid:%s: failed to rewrite stripes to journal\n", 24735aabf7c4SSong Liu mdname(mddev)); 2474effe6ee7SSong Liu ret = -EIO; 2475effe6ee7SSong Liu goto error; 24765aabf7c4SSong Liu } 24775aabf7c4SSong Liu 2478effe6ee7SSong Liu log->log_start = ctx->pos; 2479effe6ee7SSong Liu log->seq = ctx->seq; 248043b96748SJackieLiu log->last_checkpoint = pos; 248143b96748SJackieLiu r5l_write_super(log, pos); 2482a85dd7b8SSong Liu 2483effe6ee7SSong Liu r5c_recovery_flush_data_only_stripes(log, ctx); 2484effe6ee7SSong Liu ret = 0; 2485effe6ee7SSong Liu error: 2486effe6ee7SSong Liu r5l_recovery_free_ra_pool(log, ctx); 2487effe6ee7SSong Liu ra_pool: 2488effe6ee7SSong Liu __free_page(ctx->meta_page); 2489effe6ee7SSong Liu meta_page: 2490effe6ee7SSong Liu kfree(ctx); 2491effe6ee7SSong Liu return ret; 2492f6bed0efSShaohua Li } 2493f6bed0efSShaohua Li 2494f6bed0efSShaohua Li static void r5l_write_super(struct r5l_log *log, sector_t cp) 2495f6bed0efSShaohua Li { 2496f6bed0efSShaohua Li struct mddev *mddev = log->rdev->mddev; 2497f6bed0efSShaohua Li 2498f6bed0efSShaohua Li log->rdev->journal_tail = cp; 24992953079cSShaohua Li set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); 2500f6bed0efSShaohua Li } 2501f6bed0efSShaohua Li 25022c7da14bSSong Liu static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page) 25032c7da14bSSong Liu { 25042c7da14bSSong Liu struct r5conf *conf = mddev->private; 25052c7da14bSSong Liu int ret; 25062c7da14bSSong Liu 25072c7da14bSSong Liu if (!conf->log) 25082c7da14bSSong Liu return 0; 25092c7da14bSSong Liu 25102c7da14bSSong Liu switch (conf->log->r5c_journal_mode) { 25112c7da14bSSong Liu case R5C_JOURNAL_MODE_WRITE_THROUGH: 25122c7da14bSSong Liu ret = snprintf( 25132c7da14bSSong Liu page, PAGE_SIZE, "[%s] %s\n", 25142c7da14bSSong Liu r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH], 25152c7da14bSSong Liu r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]); 25162c7da14bSSong Liu break; 25172c7da14bSSong Liu case R5C_JOURNAL_MODE_WRITE_BACK: 25182c7da14bSSong Liu ret = snprintf( 25192c7da14bSSong Liu page, PAGE_SIZE, "%s [%s]\n", 25202c7da14bSSong Liu r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH], 25212c7da14bSSong Liu r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]); 25222c7da14bSSong Liu break; 25232c7da14bSSong Liu default: 25242c7da14bSSong Liu ret = 0; 25252c7da14bSSong Liu } 25262c7da14bSSong Liu return ret; 25272c7da14bSSong Liu } 25282c7da14bSSong Liu 252978e470c2SHeinz Mauelshagen /* 253078e470c2SHeinz Mauelshagen * Set journal cache mode on @mddev (external API initially needed by dm-raid). 253178e470c2SHeinz Mauelshagen * 253278e470c2SHeinz Mauelshagen * @mode as defined in 'enum r5c_journal_mode'. 253378e470c2SHeinz Mauelshagen * 253478e470c2SHeinz Mauelshagen */ 253578e470c2SHeinz Mauelshagen int r5c_journal_mode_set(struct mddev *mddev, int mode) 25362c7da14bSSong Liu { 25372c7da14bSSong Liu struct r5conf *conf = mddev->private; 25382c7da14bSSong Liu struct r5l_log *log = conf->log; 25392c7da14bSSong Liu 25402c7da14bSSong Liu if (!log) 25412c7da14bSSong Liu return -ENODEV; 25422c7da14bSSong Liu 254378e470c2SHeinz Mauelshagen if (mode < R5C_JOURNAL_MODE_WRITE_THROUGH || 254478e470c2SHeinz Mauelshagen mode > R5C_JOURNAL_MODE_WRITE_BACK) 25452c7da14bSSong Liu return -EINVAL; 25462c7da14bSSong Liu 25472e38a37fSSong Liu if (raid5_calc_degraded(conf) > 0 && 254878e470c2SHeinz Mauelshagen mode == R5C_JOURNAL_MODE_WRITE_BACK) 25492e38a37fSSong Liu return -EINVAL; 25502e38a37fSSong Liu 25512c7da14bSSong Liu mddev_suspend(mddev); 255278e470c2SHeinz Mauelshagen conf->log->r5c_journal_mode = mode; 25532c7da14bSSong Liu mddev_resume(mddev); 25542c7da14bSSong Liu 25552c7da14bSSong Liu pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n", 255678e470c2SHeinz Mauelshagen mdname(mddev), mode, r5c_journal_mode_str[mode]); 255778e470c2SHeinz Mauelshagen return 0; 255878e470c2SHeinz Mauelshagen } 255978e470c2SHeinz Mauelshagen EXPORT_SYMBOL(r5c_journal_mode_set); 256078e470c2SHeinz Mauelshagen 256178e470c2SHeinz Mauelshagen static ssize_t r5c_journal_mode_store(struct mddev *mddev, 256278e470c2SHeinz Mauelshagen const char *page, size_t length) 256378e470c2SHeinz Mauelshagen { 256478e470c2SHeinz Mauelshagen int mode = ARRAY_SIZE(r5c_journal_mode_str); 256578e470c2SHeinz Mauelshagen size_t len = length; 256678e470c2SHeinz Mauelshagen 256778e470c2SHeinz Mauelshagen if (len < 2) 256878e470c2SHeinz Mauelshagen return -EINVAL; 256978e470c2SHeinz Mauelshagen 257078e470c2SHeinz Mauelshagen if (page[len - 1] == '\n') 257178e470c2SHeinz Mauelshagen len--; 257278e470c2SHeinz Mauelshagen 257378e470c2SHeinz Mauelshagen while (mode--) 257478e470c2SHeinz Mauelshagen if (strlen(r5c_journal_mode_str[mode]) == len && 257578e470c2SHeinz Mauelshagen !strncmp(page, r5c_journal_mode_str[mode], len)) 257678e470c2SHeinz Mauelshagen break; 257778e470c2SHeinz Mauelshagen 257878e470c2SHeinz Mauelshagen return r5c_journal_mode_set(mddev, mode) ?: length; 25792c7da14bSSong Liu } 25802c7da14bSSong Liu 25812c7da14bSSong Liu struct md_sysfs_entry 25822c7da14bSSong Liu r5c_journal_mode = __ATTR(journal_mode, 0644, 25832c7da14bSSong Liu r5c_journal_mode_show, r5c_journal_mode_store); 25842c7da14bSSong Liu 25852ded3703SSong Liu /* 25862ded3703SSong Liu * Try handle write operation in caching phase. This function should only 25872ded3703SSong Liu * be called in write-back mode. 25882ded3703SSong Liu * 25892ded3703SSong Liu * If all outstanding writes can be handled in caching phase, returns 0 25902ded3703SSong Liu * If writes requires write-out phase, call r5c_make_stripe_write_out() 25912ded3703SSong Liu * and returns -EAGAIN 25922ded3703SSong Liu */ 25932ded3703SSong Liu int r5c_try_caching_write(struct r5conf *conf, 25942ded3703SSong Liu struct stripe_head *sh, 25952ded3703SSong Liu struct stripe_head_state *s, 25962ded3703SSong Liu int disks) 25972ded3703SSong Liu { 25982ded3703SSong Liu struct r5l_log *log = conf->log; 25991e6d690bSSong Liu int i; 26001e6d690bSSong Liu struct r5dev *dev; 26011e6d690bSSong Liu int to_cache = 0; 260203b047f4SSong Liu void **pslot; 260303b047f4SSong Liu sector_t tree_index; 260403b047f4SSong Liu int ret; 260503b047f4SSong Liu uintptr_t refcount; 26062ded3703SSong Liu 26072ded3703SSong Liu BUG_ON(!r5c_is_writeback(log)); 26082ded3703SSong Liu 26091e6d690bSSong Liu if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) { 26101e6d690bSSong Liu /* 26111e6d690bSSong Liu * There are two different scenarios here: 26121e6d690bSSong Liu * 1. The stripe has some data cached, and it is sent to 26131e6d690bSSong Liu * write-out phase for reclaim 26141e6d690bSSong Liu * 2. The stripe is clean, and this is the first write 26151e6d690bSSong Liu * 26161e6d690bSSong Liu * For 1, return -EAGAIN, so we continue with 26171e6d690bSSong Liu * handle_stripe_dirtying(). 26181e6d690bSSong Liu * 26191e6d690bSSong Liu * For 2, set STRIPE_R5C_CACHING and continue with caching 26201e6d690bSSong Liu * write. 26211e6d690bSSong Liu */ 26221e6d690bSSong Liu 26231e6d690bSSong Liu /* case 1: anything injournal or anything in written */ 26241e6d690bSSong Liu if (s->injournal > 0 || s->written > 0) 26251e6d690bSSong Liu return -EAGAIN; 26261e6d690bSSong Liu /* case 2 */ 26271e6d690bSSong Liu set_bit(STRIPE_R5C_CACHING, &sh->state); 26281e6d690bSSong Liu } 26291e6d690bSSong Liu 26302e38a37fSSong Liu /* 26312e38a37fSSong Liu * When run in degraded mode, array is set to write-through mode. 26322e38a37fSSong Liu * This check helps drain pending write safely in the transition to 26332e38a37fSSong Liu * write-through mode. 26342e38a37fSSong Liu */ 26352e38a37fSSong Liu if (s->failed) { 26362e38a37fSSong Liu r5c_make_stripe_write_out(sh); 26372e38a37fSSong Liu return -EAGAIN; 26382e38a37fSSong Liu } 26392e38a37fSSong Liu 26401e6d690bSSong Liu for (i = disks; i--; ) { 26411e6d690bSSong Liu dev = &sh->dev[i]; 26421e6d690bSSong Liu /* if non-overwrite, use writing-out phase */ 26431e6d690bSSong Liu if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) && 26441e6d690bSSong Liu !test_bit(R5_InJournal, &dev->flags)) { 26452ded3703SSong Liu r5c_make_stripe_write_out(sh); 26462ded3703SSong Liu return -EAGAIN; 26472ded3703SSong Liu } 26481e6d690bSSong Liu } 26491e6d690bSSong Liu 265003b047f4SSong Liu /* if the stripe is not counted in big_stripe_tree, add it now */ 265103b047f4SSong Liu if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) && 265203b047f4SSong Liu !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) { 265303b047f4SSong Liu tree_index = r5c_tree_index(conf, sh->sector); 265403b047f4SSong Liu spin_lock(&log->tree_lock); 265503b047f4SSong Liu pslot = radix_tree_lookup_slot(&log->big_stripe_tree, 265603b047f4SSong Liu tree_index); 265703b047f4SSong Liu if (pslot) { 265803b047f4SSong Liu refcount = (uintptr_t)radix_tree_deref_slot_protected( 265903b047f4SSong Liu pslot, &log->tree_lock) >> 266003b047f4SSong Liu R5C_RADIX_COUNT_SHIFT; 266103b047f4SSong Liu radix_tree_replace_slot( 266203b047f4SSong Liu &log->big_stripe_tree, pslot, 266303b047f4SSong Liu (void *)((refcount + 1) << R5C_RADIX_COUNT_SHIFT)); 266403b047f4SSong Liu } else { 266503b047f4SSong Liu /* 266603b047f4SSong Liu * this radix_tree_insert can fail safely, so no 266703b047f4SSong Liu * need to call radix_tree_preload() 266803b047f4SSong Liu */ 266903b047f4SSong Liu ret = radix_tree_insert( 267003b047f4SSong Liu &log->big_stripe_tree, tree_index, 267103b047f4SSong Liu (void *)(1 << R5C_RADIX_COUNT_SHIFT)); 267203b047f4SSong Liu if (ret) { 267303b047f4SSong Liu spin_unlock(&log->tree_lock); 267403b047f4SSong Liu r5c_make_stripe_write_out(sh); 267503b047f4SSong Liu return -EAGAIN; 267603b047f4SSong Liu } 267703b047f4SSong Liu } 267803b047f4SSong Liu spin_unlock(&log->tree_lock); 267903b047f4SSong Liu 268003b047f4SSong Liu /* 268103b047f4SSong Liu * set STRIPE_R5C_PARTIAL_STRIPE, this shows the stripe is 268203b047f4SSong Liu * counted in the radix tree 268303b047f4SSong Liu */ 268403b047f4SSong Liu set_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state); 268503b047f4SSong Liu atomic_inc(&conf->r5c_cached_partial_stripes); 268603b047f4SSong Liu } 268703b047f4SSong Liu 26881e6d690bSSong Liu for (i = disks; i--; ) { 26891e6d690bSSong Liu dev = &sh->dev[i]; 26901e6d690bSSong Liu if (dev->towrite) { 26911e6d690bSSong Liu set_bit(R5_Wantwrite, &dev->flags); 26921e6d690bSSong Liu set_bit(R5_Wantdrain, &dev->flags); 26931e6d690bSSong Liu set_bit(R5_LOCKED, &dev->flags); 26941e6d690bSSong Liu to_cache++; 26951e6d690bSSong Liu } 26961e6d690bSSong Liu } 26971e6d690bSSong Liu 26981e6d690bSSong Liu if (to_cache) { 26991e6d690bSSong Liu set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); 27001e6d690bSSong Liu /* 27011e6d690bSSong Liu * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data() 27021e6d690bSSong Liu * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in 27031e6d690bSSong Liu * r5c_handle_data_cached() 27041e6d690bSSong Liu */ 27051e6d690bSSong Liu set_bit(STRIPE_LOG_TRAPPED, &sh->state); 27061e6d690bSSong Liu } 27071e6d690bSSong Liu 27081e6d690bSSong Liu return 0; 27091e6d690bSSong Liu } 27101e6d690bSSong Liu 27111e6d690bSSong Liu /* 27121e6d690bSSong Liu * free extra pages (orig_page) we allocated for prexor 27131e6d690bSSong Liu */ 27141e6d690bSSong Liu void r5c_release_extra_page(struct stripe_head *sh) 27151e6d690bSSong Liu { 2716d7bd398eSSong Liu struct r5conf *conf = sh->raid_conf; 27171e6d690bSSong Liu int i; 2718d7bd398eSSong Liu bool using_disk_info_extra_page; 2719d7bd398eSSong Liu 2720d7bd398eSSong Liu using_disk_info_extra_page = 2721d7bd398eSSong Liu sh->dev[0].orig_page == conf->disks[0].extra_page; 27221e6d690bSSong Liu 27231e6d690bSSong Liu for (i = sh->disks; i--; ) 27241e6d690bSSong Liu if (sh->dev[i].page != sh->dev[i].orig_page) { 27251e6d690bSSong Liu struct page *p = sh->dev[i].orig_page; 27261e6d690bSSong Liu 27271e6d690bSSong Liu sh->dev[i].orig_page = sh->dev[i].page; 272886aa1397SSong Liu clear_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags); 272986aa1397SSong Liu 2730d7bd398eSSong Liu if (!using_disk_info_extra_page) 27311e6d690bSSong Liu put_page(p); 27321e6d690bSSong Liu } 2733d7bd398eSSong Liu 2734d7bd398eSSong Liu if (using_disk_info_extra_page) { 2735d7bd398eSSong Liu clear_bit(R5C_EXTRA_PAGE_IN_USE, &conf->cache_state); 2736d7bd398eSSong Liu md_wakeup_thread(conf->mddev->thread); 2737d7bd398eSSong Liu } 2738d7bd398eSSong Liu } 2739d7bd398eSSong Liu 2740d7bd398eSSong Liu void r5c_use_extra_page(struct stripe_head *sh) 2741d7bd398eSSong Liu { 2742d7bd398eSSong Liu struct r5conf *conf = sh->raid_conf; 2743d7bd398eSSong Liu int i; 2744d7bd398eSSong Liu struct r5dev *dev; 2745d7bd398eSSong Liu 2746d7bd398eSSong Liu for (i = sh->disks; i--; ) { 2747d7bd398eSSong Liu dev = &sh->dev[i]; 2748d7bd398eSSong Liu if (dev->orig_page != dev->page) 2749d7bd398eSSong Liu put_page(dev->orig_page); 2750d7bd398eSSong Liu dev->orig_page = conf->disks[i].extra_page; 2751d7bd398eSSong Liu } 27521e6d690bSSong Liu } 27532ded3703SSong Liu 27542ded3703SSong Liu /* 27552ded3703SSong Liu * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the 27562ded3703SSong Liu * stripe is committed to RAID disks. 27572ded3703SSong Liu */ 27582ded3703SSong Liu void r5c_finish_stripe_write_out(struct r5conf *conf, 27592ded3703SSong Liu struct stripe_head *sh, 27602ded3703SSong Liu struct stripe_head_state *s) 27612ded3703SSong Liu { 276203b047f4SSong Liu struct r5l_log *log = conf->log; 27631e6d690bSSong Liu int i; 27641e6d690bSSong Liu int do_wakeup = 0; 276503b047f4SSong Liu sector_t tree_index; 276603b047f4SSong Liu void **pslot; 276703b047f4SSong Liu uintptr_t refcount; 27681e6d690bSSong Liu 276903b047f4SSong Liu if (!log || !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags)) 27702ded3703SSong Liu return; 27712ded3703SSong Liu 27722ded3703SSong Liu WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state)); 27732ded3703SSong Liu clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags); 27742ded3703SSong Liu 277503b047f4SSong Liu if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) 27762ded3703SSong Liu return; 27771e6d690bSSong Liu 27781e6d690bSSong Liu for (i = sh->disks; i--; ) { 27791e6d690bSSong Liu clear_bit(R5_InJournal, &sh->dev[i].flags); 27801e6d690bSSong Liu if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) 27811e6d690bSSong Liu do_wakeup = 1; 27821e6d690bSSong Liu } 27831e6d690bSSong Liu 27841e6d690bSSong Liu /* 27851e6d690bSSong Liu * analyse_stripe() runs before r5c_finish_stripe_write_out(), 27861e6d690bSSong Liu * We updated R5_InJournal, so we also update s->injournal. 27871e6d690bSSong Liu */ 27881e6d690bSSong Liu s->injournal = 0; 27891e6d690bSSong Liu 27901e6d690bSSong Liu if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) 27911e6d690bSSong Liu if (atomic_dec_and_test(&conf->pending_full_writes)) 27921e6d690bSSong Liu md_wakeup_thread(conf->mddev->thread); 27931e6d690bSSong Liu 27941e6d690bSSong Liu if (do_wakeup) 27951e6d690bSSong Liu wake_up(&conf->wait_for_overlap); 2796a39f7afdSSong Liu 279703b047f4SSong Liu spin_lock_irq(&log->stripe_in_journal_lock); 2798a39f7afdSSong Liu list_del_init(&sh->r5c); 279903b047f4SSong Liu spin_unlock_irq(&log->stripe_in_journal_lock); 2800a39f7afdSSong Liu sh->log_start = MaxSector; 280103b047f4SSong Liu 280203b047f4SSong Liu atomic_dec(&log->stripe_in_journal_count); 280303b047f4SSong Liu r5c_update_log_state(log); 280403b047f4SSong Liu 280503b047f4SSong Liu /* stop counting this stripe in big_stripe_tree */ 280603b047f4SSong Liu if (test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) || 280703b047f4SSong Liu test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) { 280803b047f4SSong Liu tree_index = r5c_tree_index(conf, sh->sector); 280903b047f4SSong Liu spin_lock(&log->tree_lock); 281003b047f4SSong Liu pslot = radix_tree_lookup_slot(&log->big_stripe_tree, 281103b047f4SSong Liu tree_index); 281203b047f4SSong Liu BUG_ON(pslot == NULL); 281303b047f4SSong Liu refcount = (uintptr_t)radix_tree_deref_slot_protected( 281403b047f4SSong Liu pslot, &log->tree_lock) >> 281503b047f4SSong Liu R5C_RADIX_COUNT_SHIFT; 281603b047f4SSong Liu if (refcount == 1) 281703b047f4SSong Liu radix_tree_delete(&log->big_stripe_tree, tree_index); 281803b047f4SSong Liu else 281903b047f4SSong Liu radix_tree_replace_slot( 282003b047f4SSong Liu &log->big_stripe_tree, pslot, 282103b047f4SSong Liu (void *)((refcount - 1) << R5C_RADIX_COUNT_SHIFT)); 282203b047f4SSong Liu spin_unlock(&log->tree_lock); 282303b047f4SSong Liu } 282403b047f4SSong Liu 282503b047f4SSong Liu if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) { 282603b047f4SSong Liu BUG_ON(atomic_read(&conf->r5c_cached_partial_stripes) == 0); 2827e33fbb9cSShaohua Li atomic_dec(&conf->r5c_flushing_partial_stripes); 282803b047f4SSong Liu atomic_dec(&conf->r5c_cached_partial_stripes); 282903b047f4SSong Liu } 283003b047f4SSong Liu 283103b047f4SSong Liu if (test_and_clear_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) { 283203b047f4SSong Liu BUG_ON(atomic_read(&conf->r5c_cached_full_stripes) == 0); 2833e33fbb9cSShaohua Li atomic_dec(&conf->r5c_flushing_full_stripes); 283403b047f4SSong Liu atomic_dec(&conf->r5c_cached_full_stripes); 283503b047f4SSong Liu } 2836ea17481fSSong Liu 2837ea17481fSSong Liu r5l_append_flush_payload(log, sh->sector); 28381e6d690bSSong Liu } 28391e6d690bSSong Liu 2840ff875738SArtur Paszkiewicz int r5c_cache_data(struct r5l_log *log, struct stripe_head *sh) 28411e6d690bSSong Liu { 2842a39f7afdSSong Liu struct r5conf *conf = sh->raid_conf; 28431e6d690bSSong Liu int pages = 0; 28441e6d690bSSong Liu int reserve; 28451e6d690bSSong Liu int i; 28461e6d690bSSong Liu int ret = 0; 28471e6d690bSSong Liu 28481e6d690bSSong Liu BUG_ON(!log); 28491e6d690bSSong Liu 28501e6d690bSSong Liu for (i = 0; i < sh->disks; i++) { 28511e6d690bSSong Liu void *addr; 28521e6d690bSSong Liu 28531e6d690bSSong Liu if (!test_bit(R5_Wantwrite, &sh->dev[i].flags)) 28541e6d690bSSong Liu continue; 28551e6d690bSSong Liu addr = kmap_atomic(sh->dev[i].page); 28561e6d690bSSong Liu sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum, 28571e6d690bSSong Liu addr, PAGE_SIZE); 28581e6d690bSSong Liu kunmap_atomic(addr); 28591e6d690bSSong Liu pages++; 28601e6d690bSSong Liu } 28611e6d690bSSong Liu WARN_ON(pages == 0); 28621e6d690bSSong Liu 28631e6d690bSSong Liu /* 28641e6d690bSSong Liu * The stripe must enter state machine again to call endio, so 28651e6d690bSSong Liu * don't delay. 28661e6d690bSSong Liu */ 28671e6d690bSSong Liu clear_bit(STRIPE_DELAYED, &sh->state); 28681e6d690bSSong Liu atomic_inc(&sh->count); 28691e6d690bSSong Liu 28701e6d690bSSong Liu mutex_lock(&log->io_mutex); 28711e6d690bSSong Liu /* meta + data */ 28721e6d690bSSong Liu reserve = (1 + pages) << (PAGE_SHIFT - 9); 28731e6d690bSSong Liu 2874a39f7afdSSong Liu if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && 2875a39f7afdSSong Liu sh->log_start == MaxSector) 2876a39f7afdSSong Liu r5l_add_no_space_stripe(log, sh); 2877a39f7afdSSong Liu else if (!r5l_has_free_space(log, reserve)) { 2878a39f7afdSSong Liu if (sh->log_start == log->last_checkpoint) 2879a39f7afdSSong Liu BUG(); 2880a39f7afdSSong Liu else 2881a39f7afdSSong Liu r5l_add_no_space_stripe(log, sh); 28821e6d690bSSong Liu } else { 28831e6d690bSSong Liu ret = r5l_log_stripe(log, sh, pages, 0); 28841e6d690bSSong Liu if (ret) { 28851e6d690bSSong Liu spin_lock_irq(&log->io_list_lock); 28861e6d690bSSong Liu list_add_tail(&sh->log_list, &log->no_mem_stripes); 28871e6d690bSSong Liu spin_unlock_irq(&log->io_list_lock); 28881e6d690bSSong Liu } 28891e6d690bSSong Liu } 28901e6d690bSSong Liu 28911e6d690bSSong Liu mutex_unlock(&log->io_mutex); 28921e6d690bSSong Liu return 0; 2893f6bed0efSShaohua Li } 2894f6bed0efSShaohua Li 289503b047f4SSong Liu /* check whether this big stripe is in write back cache. */ 289603b047f4SSong Liu bool r5c_big_stripe_cached(struct r5conf *conf, sector_t sect) 289703b047f4SSong Liu { 289803b047f4SSong Liu struct r5l_log *log = conf->log; 289903b047f4SSong Liu sector_t tree_index; 290003b047f4SSong Liu void *slot; 290103b047f4SSong Liu 290203b047f4SSong Liu if (!log) 290303b047f4SSong Liu return false; 290403b047f4SSong Liu 290503b047f4SSong Liu WARN_ON_ONCE(!rcu_read_lock_held()); 290603b047f4SSong Liu tree_index = r5c_tree_index(conf, sect); 290703b047f4SSong Liu slot = radix_tree_lookup(&log->big_stripe_tree, tree_index); 290803b047f4SSong Liu return slot != NULL; 290903b047f4SSong Liu } 291003b047f4SSong Liu 2911f6bed0efSShaohua Li static int r5l_load_log(struct r5l_log *log) 2912f6bed0efSShaohua Li { 2913f6bed0efSShaohua Li struct md_rdev *rdev = log->rdev; 2914f6bed0efSShaohua Li struct page *page; 2915f6bed0efSShaohua Li struct r5l_meta_block *mb; 2916f6bed0efSShaohua Li sector_t cp = log->rdev->journal_tail; 2917f6bed0efSShaohua Li u32 stored_crc, expected_crc; 2918f6bed0efSShaohua Li bool create_super = false; 2919d30dfeb9SJackieLiu int ret = 0; 2920f6bed0efSShaohua Li 2921f6bed0efSShaohua Li /* Make sure it's valid */ 2922f6bed0efSShaohua Li if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp) 2923f6bed0efSShaohua Li cp = 0; 2924f6bed0efSShaohua Li page = alloc_page(GFP_KERNEL); 2925f6bed0efSShaohua Li if (!page) 2926f6bed0efSShaohua Li return -ENOMEM; 2927f6bed0efSShaohua Li 2928796a5cf0SMike Christie if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) { 2929f6bed0efSShaohua Li ret = -EIO; 2930f6bed0efSShaohua Li goto ioerr; 2931f6bed0efSShaohua Li } 2932f6bed0efSShaohua Li mb = page_address(page); 2933f6bed0efSShaohua Li 2934f6bed0efSShaohua Li if (le32_to_cpu(mb->magic) != R5LOG_MAGIC || 2935f6bed0efSShaohua Li mb->version != R5LOG_VERSION) { 2936f6bed0efSShaohua Li create_super = true; 2937f6bed0efSShaohua Li goto create; 2938f6bed0efSShaohua Li } 2939f6bed0efSShaohua Li stored_crc = le32_to_cpu(mb->checksum); 2940f6bed0efSShaohua Li mb->checksum = 0; 29415cb2fbd6SShaohua Li expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE); 2942f6bed0efSShaohua Li if (stored_crc != expected_crc) { 2943f6bed0efSShaohua Li create_super = true; 2944f6bed0efSShaohua Li goto create; 2945f6bed0efSShaohua Li } 2946f6bed0efSShaohua Li if (le64_to_cpu(mb->position) != cp) { 2947f6bed0efSShaohua Li create_super = true; 2948f6bed0efSShaohua Li goto create; 2949f6bed0efSShaohua Li } 2950f6bed0efSShaohua Li create: 2951f6bed0efSShaohua Li if (create_super) { 2952f6bed0efSShaohua Li log->last_cp_seq = prandom_u32(); 2953f6bed0efSShaohua Li cp = 0; 295456056c2eSZhengyuan Liu r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq); 2955f6bed0efSShaohua Li /* 2956f6bed0efSShaohua Li * Make sure super points to correct address. Log might have 2957f6bed0efSShaohua Li * data very soon. If super hasn't correct log tail address, 2958f6bed0efSShaohua Li * recovery can't find the log 2959f6bed0efSShaohua Li */ 2960f6bed0efSShaohua Li r5l_write_super(log, cp); 2961f6bed0efSShaohua Li } else 2962f6bed0efSShaohua Li log->last_cp_seq = le64_to_cpu(mb->seq); 2963f6bed0efSShaohua Li 2964f6bed0efSShaohua Li log->device_size = round_down(rdev->sectors, BLOCK_SECTORS); 29650576b1c6SShaohua Li log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT; 29660576b1c6SShaohua Li if (log->max_free_space > RECLAIM_MAX_FREE_SPACE) 29670576b1c6SShaohua Li log->max_free_space = RECLAIM_MAX_FREE_SPACE; 2968f6bed0efSShaohua Li log->last_checkpoint = cp; 2969f6bed0efSShaohua Li 2970f6bed0efSShaohua Li __free_page(page); 2971f6bed0efSShaohua Li 2972d30dfeb9SJackieLiu if (create_super) { 2973d30dfeb9SJackieLiu log->log_start = r5l_ring_add(log, cp, BLOCK_SECTORS); 2974d30dfeb9SJackieLiu log->seq = log->last_cp_seq + 1; 2975d30dfeb9SJackieLiu log->next_checkpoint = cp; 2976d30dfeb9SJackieLiu } else 29773d7e7e1dSZhengyuan Liu ret = r5l_recovery_log(log); 2978d30dfeb9SJackieLiu 29793d7e7e1dSZhengyuan Liu r5c_update_log_state(log); 29803d7e7e1dSZhengyuan Liu return ret; 2981f6bed0efSShaohua Li ioerr: 2982f6bed0efSShaohua Li __free_page(page); 2983f6bed0efSShaohua Li return ret; 2984f6bed0efSShaohua Li } 2985f6bed0efSShaohua Li 29862e38a37fSSong Liu void r5c_update_on_rdev_error(struct mddev *mddev) 29872e38a37fSSong Liu { 29882e38a37fSSong Liu struct r5conf *conf = mddev->private; 29892e38a37fSSong Liu struct r5l_log *log = conf->log; 29902e38a37fSSong Liu 29912e38a37fSSong Liu if (!log) 29922e38a37fSSong Liu return; 29932e38a37fSSong Liu 29942e38a37fSSong Liu if (raid5_calc_degraded(conf) > 0 && 29952e38a37fSSong Liu conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) 29962e38a37fSSong Liu schedule_work(&log->disable_writeback_work); 29972e38a37fSSong Liu } 29982e38a37fSSong Liu 2999f6bed0efSShaohua Li int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev) 3000f6bed0efSShaohua Li { 3001c888a8f9SJens Axboe struct request_queue *q = bdev_get_queue(rdev->bdev); 3002f6bed0efSShaohua Li struct r5l_log *log; 3003ff875738SArtur Paszkiewicz char b[BDEVNAME_SIZE]; 3004ff875738SArtur Paszkiewicz 3005ff875738SArtur Paszkiewicz pr_debug("md/raid:%s: using device %s as journal\n", 3006ff875738SArtur Paszkiewicz mdname(conf->mddev), bdevname(rdev->bdev, b)); 3007f6bed0efSShaohua Li 3008f6bed0efSShaohua Li if (PAGE_SIZE != 4096) 3009f6bed0efSShaohua Li return -EINVAL; 3010c757ec95SSong Liu 3011c757ec95SSong Liu /* 3012c757ec95SSong Liu * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and 3013c757ec95SSong Liu * raid_disks r5l_payload_data_parity. 3014c757ec95SSong Liu * 3015c757ec95SSong Liu * Write journal and cache does not work for very big array 3016c757ec95SSong Liu * (raid_disks > 203) 3017c757ec95SSong Liu */ 3018c757ec95SSong Liu if (sizeof(struct r5l_meta_block) + 3019c757ec95SSong Liu ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) * 3020c757ec95SSong Liu conf->raid_disks) > PAGE_SIZE) { 3021c757ec95SSong Liu pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n", 3022c757ec95SSong Liu mdname(conf->mddev), conf->raid_disks); 3023c757ec95SSong Liu return -EINVAL; 3024c757ec95SSong Liu } 3025c757ec95SSong Liu 3026f6bed0efSShaohua Li log = kzalloc(sizeof(*log), GFP_KERNEL); 3027f6bed0efSShaohua Li if (!log) 3028f6bed0efSShaohua Li return -ENOMEM; 3029f6bed0efSShaohua Li log->rdev = rdev; 3030f6bed0efSShaohua Li 3031c888a8f9SJens Axboe log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0; 303256fef7c6SChristoph Hellwig 30335cb2fbd6SShaohua Li log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid, 3034f6bed0efSShaohua Li sizeof(rdev->mddev->uuid)); 3035f6bed0efSShaohua Li 3036f6bed0efSShaohua Li mutex_init(&log->io_mutex); 3037f6bed0efSShaohua Li 3038f6bed0efSShaohua Li spin_lock_init(&log->io_list_lock); 3039f6bed0efSShaohua Li INIT_LIST_HEAD(&log->running_ios); 30400576b1c6SShaohua Li INIT_LIST_HEAD(&log->io_end_ios); 3041a8c34f91SShaohua Li INIT_LIST_HEAD(&log->flushing_ios); 304204732f74SChristoph Hellwig INIT_LIST_HEAD(&log->finished_ios); 30433a83f467SMing Lei bio_init(&log->flush_bio, NULL, 0); 3044f6bed0efSShaohua Li 3045f6bed0efSShaohua Li log->io_kc = KMEM_CACHE(r5l_io_unit, 0); 3046f6bed0efSShaohua Li if (!log->io_kc) 3047f6bed0efSShaohua Li goto io_kc; 3048f6bed0efSShaohua Li 30495036c390SChristoph Hellwig log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc); 30505036c390SChristoph Hellwig if (!log->io_pool) 30515036c390SChristoph Hellwig goto io_pool; 30525036c390SChristoph Hellwig 3053c38d29b3SChristoph Hellwig log->bs = bioset_create(R5L_POOL_SIZE, 0); 3054c38d29b3SChristoph Hellwig if (!log->bs) 3055c38d29b3SChristoph Hellwig goto io_bs; 3056c38d29b3SChristoph Hellwig 3057e8deb638SChristoph Hellwig log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0); 3058e8deb638SChristoph Hellwig if (!log->meta_pool) 3059e8deb638SChristoph Hellwig goto out_mempool; 3060e8deb638SChristoph Hellwig 306103b047f4SSong Liu spin_lock_init(&log->tree_lock); 306203b047f4SSong Liu INIT_RADIX_TREE(&log->big_stripe_tree, GFP_NOWAIT | __GFP_NOWARN); 306303b047f4SSong Liu 30640576b1c6SShaohua Li log->reclaim_thread = md_register_thread(r5l_reclaim_thread, 30650576b1c6SShaohua Li log->rdev->mddev, "reclaim"); 30660576b1c6SShaohua Li if (!log->reclaim_thread) 30670576b1c6SShaohua Li goto reclaim_thread; 3068a39f7afdSSong Liu log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL; 3069a39f7afdSSong Liu 30700fd22b45SShaohua Li init_waitqueue_head(&log->iounit_wait); 30710576b1c6SShaohua Li 30725036c390SChristoph Hellwig INIT_LIST_HEAD(&log->no_mem_stripes); 30735036c390SChristoph Hellwig 3074f6bed0efSShaohua Li INIT_LIST_HEAD(&log->no_space_stripes); 3075f6bed0efSShaohua Li spin_lock_init(&log->no_space_stripes_lock); 3076f6bed0efSShaohua Li 30773bddb7f8SSong Liu INIT_WORK(&log->deferred_io_work, r5l_submit_io_async); 30782e38a37fSSong Liu INIT_WORK(&log->disable_writeback_work, r5c_disable_writeback_async); 30793bddb7f8SSong Liu 30802ded3703SSong Liu log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH; 3081a39f7afdSSong Liu INIT_LIST_HEAD(&log->stripe_in_journal_list); 3082a39f7afdSSong Liu spin_lock_init(&log->stripe_in_journal_lock); 3083a39f7afdSSong Liu atomic_set(&log->stripe_in_journal_count, 0); 30842ded3703SSong Liu 3085d2250f10SSong Liu rcu_assign_pointer(conf->log, log); 3086d2250f10SSong Liu 3087f6bed0efSShaohua Li if (r5l_load_log(log)) 3088f6bed0efSShaohua Li goto error; 3089f6bed0efSShaohua Li 3090a62ab49eSShaohua Li set_bit(MD_HAS_JOURNAL, &conf->mddev->flags); 3091f6bed0efSShaohua Li return 0; 3092e8deb638SChristoph Hellwig 3093f6bed0efSShaohua Li error: 3094d2250f10SSong Liu rcu_assign_pointer(conf->log, NULL); 30950576b1c6SShaohua Li md_unregister_thread(&log->reclaim_thread); 30960576b1c6SShaohua Li reclaim_thread: 3097e8deb638SChristoph Hellwig mempool_destroy(log->meta_pool); 3098e8deb638SChristoph Hellwig out_mempool: 3099c38d29b3SChristoph Hellwig bioset_free(log->bs); 3100c38d29b3SChristoph Hellwig io_bs: 31015036c390SChristoph Hellwig mempool_destroy(log->io_pool); 31025036c390SChristoph Hellwig io_pool: 3103f6bed0efSShaohua Li kmem_cache_destroy(log->io_kc); 3104f6bed0efSShaohua Li io_kc: 3105f6bed0efSShaohua Li kfree(log); 3106f6bed0efSShaohua Li return -EINVAL; 3107f6bed0efSShaohua Li } 3108f6bed0efSShaohua Li 3109ff875738SArtur Paszkiewicz void r5l_exit_log(struct r5conf *conf) 3110f6bed0efSShaohua Li { 3111ff875738SArtur Paszkiewicz struct r5l_log *log = conf->log; 3112ff875738SArtur Paszkiewicz 3113ff875738SArtur Paszkiewicz conf->log = NULL; 3114ff875738SArtur Paszkiewicz synchronize_rcu(); 3115ff875738SArtur Paszkiewicz 31162e38a37fSSong Liu flush_work(&log->disable_writeback_work); 31170576b1c6SShaohua Li md_unregister_thread(&log->reclaim_thread); 3118e8deb638SChristoph Hellwig mempool_destroy(log->meta_pool); 3119c38d29b3SChristoph Hellwig bioset_free(log->bs); 31205036c390SChristoph Hellwig mempool_destroy(log->io_pool); 3121f6bed0efSShaohua Li kmem_cache_destroy(log->io_kc); 3122f6bed0efSShaohua Li kfree(log); 3123f6bed0efSShaohua Li } 3124