xref: /linux/drivers/md/raid5-cache.c (revision d7bd398e97f236a2353689eca5e8950f67cd34d5)
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>
23f6bed0efSShaohua Li #include "md.h"
24f6bed0efSShaohua Li #include "raid5.h"
251e6d690bSSong Liu #include "bitmap.h"
26f6bed0efSShaohua Li 
27f6bed0efSShaohua Li /*
28f6bed0efSShaohua Li  * metadata/data stored in disk with 4k size unit (a block) regardless
29f6bed0efSShaohua Li  * underneath hardware sector size. only works with PAGE_SIZE == 4096
30f6bed0efSShaohua Li  */
31f6bed0efSShaohua Li #define BLOCK_SECTORS (8)
32f6bed0efSShaohua Li 
330576b1c6SShaohua Li /*
34a39f7afdSSong Liu  * log->max_free_space is min(1/4 disk size, 10G reclaimable space).
35a39f7afdSSong Liu  *
36a39f7afdSSong Liu  * In write through mode, the reclaim runs every log->max_free_space.
37a39f7afdSSong Liu  * This can prevent the recovery scans for too long
380576b1c6SShaohua Li  */
390576b1c6SShaohua Li #define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
400576b1c6SShaohua Li #define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
410576b1c6SShaohua Li 
42a39f7afdSSong Liu /* wake up reclaim thread periodically */
43a39f7afdSSong Liu #define R5C_RECLAIM_WAKEUP_INTERVAL (30 * HZ)
44a39f7afdSSong Liu /* start flush with these full stripes */
45a39f7afdSSong Liu #define R5C_FULL_STRIPE_FLUSH_BATCH 256
46a39f7afdSSong Liu /* reclaim stripes in groups */
47a39f7afdSSong Liu #define R5C_RECLAIM_STRIPE_GROUP (NR_STRIPE_HASH_LOCKS * 2)
48a39f7afdSSong Liu 
49c38d29b3SChristoph Hellwig /*
50c38d29b3SChristoph Hellwig  * We only need 2 bios per I/O unit to make progress, but ensure we
51c38d29b3SChristoph Hellwig  * have a few more available to not get too tight.
52c38d29b3SChristoph Hellwig  */
53c38d29b3SChristoph Hellwig #define R5L_POOL_SIZE	4
54c38d29b3SChristoph Hellwig 
552ded3703SSong Liu /*
562ded3703SSong Liu  * r5c journal modes of the array: write-back or write-through.
572ded3703SSong Liu  * write-through mode has identical behavior as existing log only
582ded3703SSong Liu  * implementation.
592ded3703SSong Liu  */
602ded3703SSong Liu enum r5c_journal_mode {
612ded3703SSong Liu 	R5C_JOURNAL_MODE_WRITE_THROUGH = 0,
622ded3703SSong Liu 	R5C_JOURNAL_MODE_WRITE_BACK = 1,
632ded3703SSong Liu };
642ded3703SSong Liu 
652c7da14bSSong Liu static char *r5c_journal_mode_str[] = {"write-through",
662c7da14bSSong Liu 				       "write-back"};
672ded3703SSong Liu /*
682ded3703SSong Liu  * raid5 cache state machine
692ded3703SSong Liu  *
702ded3703SSong Liu  * With rhe RAID cache, each stripe works in two phases:
712ded3703SSong Liu  *	- caching phase
722ded3703SSong Liu  *	- writing-out phase
732ded3703SSong Liu  *
742ded3703SSong Liu  * These two phases are controlled by bit STRIPE_R5C_CACHING:
752ded3703SSong Liu  *   if STRIPE_R5C_CACHING == 0, the stripe is in writing-out phase
762ded3703SSong Liu  *   if STRIPE_R5C_CACHING == 1, the stripe is in caching phase
772ded3703SSong Liu  *
782ded3703SSong Liu  * When there is no journal, or the journal is in write-through mode,
792ded3703SSong Liu  * the stripe is always in writing-out phase.
802ded3703SSong Liu  *
812ded3703SSong Liu  * For write-back journal, the stripe is sent to caching phase on write
822ded3703SSong Liu  * (r5c_try_caching_write). r5c_make_stripe_write_out() kicks off
832ded3703SSong Liu  * the write-out phase by clearing STRIPE_R5C_CACHING.
842ded3703SSong Liu  *
852ded3703SSong Liu  * Stripes in caching phase do not write the raid disks. Instead, all
862ded3703SSong Liu  * writes are committed from the log device. Therefore, a stripe in
872ded3703SSong Liu  * caching phase handles writes as:
882ded3703SSong Liu  *	- write to log device
892ded3703SSong Liu  *	- return IO
902ded3703SSong Liu  *
912ded3703SSong Liu  * Stripes in writing-out phase handle writes as:
922ded3703SSong Liu  *	- calculate parity
932ded3703SSong Liu  *	- write pending data and parity to journal
942ded3703SSong Liu  *	- write data and parity to raid disks
952ded3703SSong Liu  *	- return IO for pending writes
962ded3703SSong Liu  */
972ded3703SSong Liu 
98f6bed0efSShaohua Li struct r5l_log {
99f6bed0efSShaohua Li 	struct md_rdev *rdev;
100f6bed0efSShaohua Li 
101f6bed0efSShaohua Li 	u32 uuid_checksum;
102f6bed0efSShaohua Li 
103f6bed0efSShaohua Li 	sector_t device_size;		/* log device size, round to
104f6bed0efSShaohua Li 					 * BLOCK_SECTORS */
1050576b1c6SShaohua Li 	sector_t max_free_space;	/* reclaim run if free space is at
1060576b1c6SShaohua Li 					 * this size */
107f6bed0efSShaohua Li 
108f6bed0efSShaohua Li 	sector_t last_checkpoint;	/* log tail. where recovery scan
109f6bed0efSShaohua Li 					 * starts from */
110f6bed0efSShaohua Li 	u64 last_cp_seq;		/* log tail sequence */
111f6bed0efSShaohua Li 
112f6bed0efSShaohua Li 	sector_t log_start;		/* log head. where new data appends */
113f6bed0efSShaohua Li 	u64 seq;			/* log head sequence */
114f6bed0efSShaohua Li 
11517036461SChristoph Hellwig 	sector_t next_checkpoint;
11617036461SChristoph Hellwig 	u64 next_cp_seq;
11717036461SChristoph Hellwig 
118f6bed0efSShaohua Li 	struct mutex io_mutex;
119f6bed0efSShaohua Li 	struct r5l_io_unit *current_io;	/* current io_unit accepting new data */
120f6bed0efSShaohua Li 
121f6bed0efSShaohua Li 	spinlock_t io_list_lock;
122f6bed0efSShaohua Li 	struct list_head running_ios;	/* io_units which are still running,
123f6bed0efSShaohua Li 					 * and have not yet been completely
124f6bed0efSShaohua Li 					 * written to the log */
125f6bed0efSShaohua Li 	struct list_head io_end_ios;	/* io_units which have been completely
126f6bed0efSShaohua Li 					 * written to the log but not yet written
127f6bed0efSShaohua Li 					 * to the RAID */
128a8c34f91SShaohua Li 	struct list_head flushing_ios;	/* io_units which are waiting for log
129a8c34f91SShaohua Li 					 * cache flush */
13004732f74SChristoph Hellwig 	struct list_head finished_ios;	/* io_units which settle down in log disk */
131a8c34f91SShaohua Li 	struct bio flush_bio;
132f6bed0efSShaohua Li 
1335036c390SChristoph Hellwig 	struct list_head no_mem_stripes;   /* pending stripes, -ENOMEM */
1345036c390SChristoph Hellwig 
135f6bed0efSShaohua Li 	struct kmem_cache *io_kc;
1365036c390SChristoph Hellwig 	mempool_t *io_pool;
137c38d29b3SChristoph Hellwig 	struct bio_set *bs;
138e8deb638SChristoph Hellwig 	mempool_t *meta_pool;
139f6bed0efSShaohua Li 
1400576b1c6SShaohua Li 	struct md_thread *reclaim_thread;
1410576b1c6SShaohua Li 	unsigned long reclaim_target;	/* number of space that need to be
1420576b1c6SShaohua Li 					 * reclaimed.  if it's 0, reclaim spaces
1430576b1c6SShaohua Li 					 * used by io_units which are in
1440576b1c6SShaohua Li 					 * IO_UNIT_STRIPE_END state (eg, reclaim
1450576b1c6SShaohua Li 					 * dones't wait for specific io_unit
1460576b1c6SShaohua Li 					 * switching to IO_UNIT_STRIPE_END
1470576b1c6SShaohua Li 					 * state) */
1480fd22b45SShaohua Li 	wait_queue_head_t iounit_wait;
1490576b1c6SShaohua Li 
150f6bed0efSShaohua Li 	struct list_head no_space_stripes; /* pending stripes, log has no space */
151f6bed0efSShaohua Li 	spinlock_t no_space_stripes_lock;
15256fef7c6SChristoph Hellwig 
15356fef7c6SChristoph Hellwig 	bool need_cache_flush;
1542ded3703SSong Liu 
1552ded3703SSong Liu 	/* for r5c_cache */
1562ded3703SSong Liu 	enum r5c_journal_mode r5c_journal_mode;
157a39f7afdSSong Liu 
158a39f7afdSSong Liu 	/* all stripes in r5cache, in the order of seq at sh->log_start */
159a39f7afdSSong Liu 	struct list_head stripe_in_journal_list;
160a39f7afdSSong Liu 
161a39f7afdSSong Liu 	spinlock_t stripe_in_journal_lock;
162a39f7afdSSong Liu 	atomic_t stripe_in_journal_count;
1633bddb7f8SSong Liu 
1643bddb7f8SSong Liu 	/* to submit async io_units, to fulfill ordering of flush */
1653bddb7f8SSong Liu 	struct work_struct deferred_io_work;
166f6bed0efSShaohua Li };
167f6bed0efSShaohua Li 
168f6bed0efSShaohua Li /*
169f6bed0efSShaohua Li  * an IO range starts from a meta data block and end at the next meta data
170f6bed0efSShaohua Li  * block. The io unit's the meta data block tracks data/parity followed it. io
171f6bed0efSShaohua Li  * unit is written to log disk with normal write, as we always flush log disk
172f6bed0efSShaohua Li  * first and then start move data to raid disks, there is no requirement to
173f6bed0efSShaohua Li  * write io unit with FLUSH/FUA
174f6bed0efSShaohua Li  */
175f6bed0efSShaohua Li struct r5l_io_unit {
176f6bed0efSShaohua Li 	struct r5l_log *log;
177f6bed0efSShaohua Li 
178f6bed0efSShaohua Li 	struct page *meta_page;	/* store meta block */
179f6bed0efSShaohua Li 	int meta_offset;	/* current offset in meta_page */
180f6bed0efSShaohua Li 
181f6bed0efSShaohua Li 	struct bio *current_bio;/* current_bio accepting new data */
182f6bed0efSShaohua Li 
183f6bed0efSShaohua Li 	atomic_t pending_stripe;/* how many stripes not flushed to raid */
184f6bed0efSShaohua Li 	u64 seq;		/* seq number of the metablock */
185f6bed0efSShaohua Li 	sector_t log_start;	/* where the io_unit starts */
186f6bed0efSShaohua Li 	sector_t log_end;	/* where the io_unit ends */
187f6bed0efSShaohua Li 	struct list_head log_sibling; /* log->running_ios */
188f6bed0efSShaohua Li 	struct list_head stripe_list; /* stripes added to the io_unit */
189f6bed0efSShaohua Li 
190f6bed0efSShaohua Li 	int state;
1916143e2ceSChristoph Hellwig 	bool need_split_bio;
1923bddb7f8SSong Liu 	struct bio *split_bio;
1933bddb7f8SSong Liu 
1943bddb7f8SSong Liu 	unsigned int has_flush:1;      /* include flush request */
1953bddb7f8SSong Liu 	unsigned int has_fua:1;        /* include fua request */
1963bddb7f8SSong Liu 	unsigned int has_null_flush:1; /* include empty flush request */
1973bddb7f8SSong Liu 	/*
1983bddb7f8SSong Liu 	 * io isn't sent yet, flush/fua request can only be submitted till it's
1993bddb7f8SSong Liu 	 * the first IO in running_ios list
2003bddb7f8SSong Liu 	 */
2013bddb7f8SSong Liu 	unsigned int io_deferred:1;
2023bddb7f8SSong Liu 
2033bddb7f8SSong Liu 	struct bio_list flush_barriers;   /* size == 0 flush bios */
204f6bed0efSShaohua Li };
205f6bed0efSShaohua Li 
206f6bed0efSShaohua Li /* r5l_io_unit state */
207f6bed0efSShaohua Li enum r5l_io_unit_state {
208f6bed0efSShaohua Li 	IO_UNIT_RUNNING = 0,	/* accepting new IO */
209f6bed0efSShaohua Li 	IO_UNIT_IO_START = 1,	/* io_unit bio start writing to log,
210f6bed0efSShaohua Li 				 * don't accepting new bio */
211f6bed0efSShaohua Li 	IO_UNIT_IO_END = 2,	/* io_unit bio finish writing to log */
212a8c34f91SShaohua Li 	IO_UNIT_STRIPE_END = 3,	/* stripes data finished writing to raid */
213f6bed0efSShaohua Li };
214f6bed0efSShaohua Li 
2152ded3703SSong Liu bool r5c_is_writeback(struct r5l_log *log)
2162ded3703SSong Liu {
2172ded3703SSong Liu 	return (log != NULL &&
2182ded3703SSong Liu 		log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK);
2192ded3703SSong Liu }
2202ded3703SSong Liu 
221f6bed0efSShaohua Li static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
222f6bed0efSShaohua Li {
223f6bed0efSShaohua Li 	start += inc;
224f6bed0efSShaohua Li 	if (start >= log->device_size)
225f6bed0efSShaohua Li 		start = start - log->device_size;
226f6bed0efSShaohua Li 	return start;
227f6bed0efSShaohua Li }
228f6bed0efSShaohua Li 
229f6bed0efSShaohua Li static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
230f6bed0efSShaohua Li 				  sector_t end)
231f6bed0efSShaohua Li {
232f6bed0efSShaohua Li 	if (end >= start)
233f6bed0efSShaohua Li 		return end - start;
234f6bed0efSShaohua Li 	else
235f6bed0efSShaohua Li 		return end + log->device_size - start;
236f6bed0efSShaohua Li }
237f6bed0efSShaohua Li 
238f6bed0efSShaohua Li static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
239f6bed0efSShaohua Li {
240f6bed0efSShaohua Li 	sector_t used_size;
241f6bed0efSShaohua Li 
242f6bed0efSShaohua Li 	used_size = r5l_ring_distance(log, log->last_checkpoint,
243f6bed0efSShaohua Li 					log->log_start);
244f6bed0efSShaohua Li 
245f6bed0efSShaohua Li 	return log->device_size > used_size + size;
246f6bed0efSShaohua Li }
247f6bed0efSShaohua Li 
248f6bed0efSShaohua Li static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
249f6bed0efSShaohua Li 				    enum r5l_io_unit_state state)
250f6bed0efSShaohua Li {
251f6bed0efSShaohua Li 	if (WARN_ON(io->state >= state))
252f6bed0efSShaohua Li 		return;
253f6bed0efSShaohua Li 	io->state = state;
254f6bed0efSShaohua Li }
255f6bed0efSShaohua Li 
2561e6d690bSSong Liu static void
2571e6d690bSSong Liu r5c_return_dev_pending_writes(struct r5conf *conf, struct r5dev *dev,
2581e6d690bSSong Liu 			      struct bio_list *return_bi)
2591e6d690bSSong Liu {
2601e6d690bSSong Liu 	struct bio *wbi, *wbi2;
2611e6d690bSSong Liu 
2621e6d690bSSong Liu 	wbi = dev->written;
2631e6d690bSSong Liu 	dev->written = NULL;
2641e6d690bSSong Liu 	while (wbi && wbi->bi_iter.bi_sector <
2651e6d690bSSong Liu 	       dev->sector + STRIPE_SECTORS) {
2661e6d690bSSong Liu 		wbi2 = r5_next_bio(wbi, dev->sector);
2671e6d690bSSong Liu 		if (!raid5_dec_bi_active_stripes(wbi)) {
2681e6d690bSSong Liu 			md_write_end(conf->mddev);
2691e6d690bSSong Liu 			bio_list_add(return_bi, wbi);
2701e6d690bSSong Liu 		}
2711e6d690bSSong Liu 		wbi = wbi2;
2721e6d690bSSong Liu 	}
2731e6d690bSSong Liu }
2741e6d690bSSong Liu 
2751e6d690bSSong Liu void r5c_handle_cached_data_endio(struct r5conf *conf,
2761e6d690bSSong Liu 	  struct stripe_head *sh, int disks, struct bio_list *return_bi)
2771e6d690bSSong Liu {
2781e6d690bSSong Liu 	int i;
2791e6d690bSSong Liu 
2801e6d690bSSong Liu 	for (i = sh->disks; i--; ) {
2811e6d690bSSong Liu 		if (sh->dev[i].written) {
2821e6d690bSSong Liu 			set_bit(R5_UPTODATE, &sh->dev[i].flags);
2831e6d690bSSong Liu 			r5c_return_dev_pending_writes(conf, &sh->dev[i],
2841e6d690bSSong Liu 						      return_bi);
2851e6d690bSSong Liu 			bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2861e6d690bSSong Liu 					STRIPE_SECTORS,
2871e6d690bSSong Liu 					!test_bit(STRIPE_DEGRADED, &sh->state),
2881e6d690bSSong Liu 					0);
2891e6d690bSSong Liu 		}
2901e6d690bSSong Liu 	}
2911e6d690bSSong Liu }
2921e6d690bSSong Liu 
293a39f7afdSSong Liu /* Check whether we should flush some stripes to free up stripe cache */
294a39f7afdSSong Liu void r5c_check_stripe_cache_usage(struct r5conf *conf)
295a39f7afdSSong Liu {
296a39f7afdSSong Liu 	int total_cached;
297a39f7afdSSong Liu 
298a39f7afdSSong Liu 	if (!r5c_is_writeback(conf->log))
299a39f7afdSSong Liu 		return;
300a39f7afdSSong Liu 
301a39f7afdSSong Liu 	total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
302a39f7afdSSong Liu 		atomic_read(&conf->r5c_cached_full_stripes);
303a39f7afdSSong Liu 
304a39f7afdSSong Liu 	/*
305a39f7afdSSong Liu 	 * The following condition is true for either of the following:
306a39f7afdSSong Liu 	 *   - stripe cache pressure high:
307a39f7afdSSong Liu 	 *          total_cached > 3/4 min_nr_stripes ||
308a39f7afdSSong Liu 	 *          empty_inactive_list_nr > 0
309a39f7afdSSong Liu 	 *   - stripe cache pressure moderate:
310a39f7afdSSong Liu 	 *          total_cached > 1/2 min_nr_stripes
311a39f7afdSSong Liu 	 */
312a39f7afdSSong Liu 	if (total_cached > conf->min_nr_stripes * 1 / 2 ||
313a39f7afdSSong Liu 	    atomic_read(&conf->empty_inactive_list_nr) > 0)
314a39f7afdSSong Liu 		r5l_wake_reclaim(conf->log, 0);
315a39f7afdSSong Liu }
316a39f7afdSSong Liu 
317a39f7afdSSong Liu /*
318a39f7afdSSong Liu  * flush cache when there are R5C_FULL_STRIPE_FLUSH_BATCH or more full
319a39f7afdSSong Liu  * stripes in the cache
320a39f7afdSSong Liu  */
321a39f7afdSSong Liu void r5c_check_cached_full_stripe(struct r5conf *conf)
322a39f7afdSSong Liu {
323a39f7afdSSong Liu 	if (!r5c_is_writeback(conf->log))
324a39f7afdSSong Liu 		return;
325a39f7afdSSong Liu 
326a39f7afdSSong Liu 	/*
327a39f7afdSSong Liu 	 * wake up reclaim for R5C_FULL_STRIPE_FLUSH_BATCH cached stripes
328a39f7afdSSong Liu 	 * or a full stripe (chunk size / 4k stripes).
329a39f7afdSSong Liu 	 */
330a39f7afdSSong Liu 	if (atomic_read(&conf->r5c_cached_full_stripes) >=
331a39f7afdSSong Liu 	    min(R5C_FULL_STRIPE_FLUSH_BATCH,
332a39f7afdSSong Liu 		conf->chunk_sectors >> STRIPE_SHIFT))
333a39f7afdSSong Liu 		r5l_wake_reclaim(conf->log, 0);
334a39f7afdSSong Liu }
335a39f7afdSSong Liu 
336a39f7afdSSong Liu /*
337a39f7afdSSong Liu  * Total log space (in sectors) needed to flush all data in cache
338a39f7afdSSong Liu  *
339a39f7afdSSong Liu  * Currently, writing-out phase automatically includes all pending writes
340a39f7afdSSong Liu  * to the same sector. So the reclaim of each stripe takes up to
341a39f7afdSSong Liu  * (conf->raid_disks + 1) pages of log space.
342a39f7afdSSong Liu  *
343a39f7afdSSong Liu  * To totally avoid deadlock due to log space, the code reserves
344a39f7afdSSong Liu  * (conf->raid_disks + 1) pages for each stripe in cache, which is not
345a39f7afdSSong Liu  * necessary in most cases.
346a39f7afdSSong Liu  *
347a39f7afdSSong Liu  * To improve this, we will need writing-out phase to be able to NOT include
348a39f7afdSSong Liu  * pending writes, which will reduce the requirement to
349a39f7afdSSong Liu  * (conf->max_degraded + 1) pages per stripe in cache.
350a39f7afdSSong Liu  */
351a39f7afdSSong Liu static sector_t r5c_log_required_to_flush_cache(struct r5conf *conf)
352a39f7afdSSong Liu {
353a39f7afdSSong Liu 	struct r5l_log *log = conf->log;
354a39f7afdSSong Liu 
355a39f7afdSSong Liu 	if (!r5c_is_writeback(log))
356a39f7afdSSong Liu 		return 0;
357a39f7afdSSong Liu 
358a39f7afdSSong Liu 	return BLOCK_SECTORS * (conf->raid_disks + 1) *
359a39f7afdSSong Liu 		atomic_read(&log->stripe_in_journal_count);
360a39f7afdSSong Liu }
361a39f7afdSSong Liu 
362a39f7afdSSong Liu /*
363a39f7afdSSong Liu  * evaluate log space usage and update R5C_LOG_TIGHT and R5C_LOG_CRITICAL
364a39f7afdSSong Liu  *
365a39f7afdSSong Liu  * R5C_LOG_TIGHT is set when free space on the log device is less than 3x of
366a39f7afdSSong Liu  * reclaim_required_space. R5C_LOG_CRITICAL is set when free space on the log
367a39f7afdSSong Liu  * device is less than 2x of reclaim_required_space.
368a39f7afdSSong Liu  */
369a39f7afdSSong Liu static inline void r5c_update_log_state(struct r5l_log *log)
370a39f7afdSSong Liu {
371a39f7afdSSong Liu 	struct r5conf *conf = log->rdev->mddev->private;
372a39f7afdSSong Liu 	sector_t free_space;
373a39f7afdSSong Liu 	sector_t reclaim_space;
374a39f7afdSSong Liu 
375a39f7afdSSong Liu 	if (!r5c_is_writeback(log))
376a39f7afdSSong Liu 		return;
377a39f7afdSSong Liu 
378a39f7afdSSong Liu 	free_space = r5l_ring_distance(log, log->log_start,
379a39f7afdSSong Liu 				       log->last_checkpoint);
380a39f7afdSSong Liu 	reclaim_space = r5c_log_required_to_flush_cache(conf);
381a39f7afdSSong Liu 	if (free_space < 2 * reclaim_space)
382a39f7afdSSong Liu 		set_bit(R5C_LOG_CRITICAL, &conf->cache_state);
383a39f7afdSSong Liu 	else
384a39f7afdSSong Liu 		clear_bit(R5C_LOG_CRITICAL, &conf->cache_state);
385a39f7afdSSong Liu 	if (free_space < 3 * reclaim_space)
386a39f7afdSSong Liu 		set_bit(R5C_LOG_TIGHT, &conf->cache_state);
387a39f7afdSSong Liu 	else
388a39f7afdSSong Liu 		clear_bit(R5C_LOG_TIGHT, &conf->cache_state);
389a39f7afdSSong Liu }
390a39f7afdSSong Liu 
3912ded3703SSong Liu /*
3922ded3703SSong Liu  * Put the stripe into writing-out phase by clearing STRIPE_R5C_CACHING.
3932ded3703SSong Liu  * This function should only be called in write-back mode.
3942ded3703SSong Liu  */
395a39f7afdSSong Liu void r5c_make_stripe_write_out(struct stripe_head *sh)
3962ded3703SSong Liu {
3972ded3703SSong Liu 	struct r5conf *conf = sh->raid_conf;
3982ded3703SSong Liu 	struct r5l_log *log = conf->log;
3992ded3703SSong Liu 
4002ded3703SSong Liu 	BUG_ON(!r5c_is_writeback(log));
4012ded3703SSong Liu 
4022ded3703SSong Liu 	WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
4032ded3703SSong Liu 	clear_bit(STRIPE_R5C_CACHING, &sh->state);
4041e6d690bSSong Liu 
4051e6d690bSSong Liu 	if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4061e6d690bSSong Liu 		atomic_inc(&conf->preread_active_stripes);
4071e6d690bSSong Liu 
4081e6d690bSSong Liu 	if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) {
4091e6d690bSSong Liu 		BUG_ON(atomic_read(&conf->r5c_cached_partial_stripes) == 0);
4101e6d690bSSong Liu 		atomic_dec(&conf->r5c_cached_partial_stripes);
4111e6d690bSSong Liu 	}
4121e6d690bSSong Liu 
4131e6d690bSSong Liu 	if (test_and_clear_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
4141e6d690bSSong Liu 		BUG_ON(atomic_read(&conf->r5c_cached_full_stripes) == 0);
4151e6d690bSSong Liu 		atomic_dec(&conf->r5c_cached_full_stripes);
4161e6d690bSSong Liu 	}
4171e6d690bSSong Liu }
4181e6d690bSSong Liu 
4191e6d690bSSong Liu static void r5c_handle_data_cached(struct stripe_head *sh)
4201e6d690bSSong Liu {
4211e6d690bSSong Liu 	int i;
4221e6d690bSSong Liu 
4231e6d690bSSong Liu 	for (i = sh->disks; i--; )
4241e6d690bSSong Liu 		if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
4251e6d690bSSong Liu 			set_bit(R5_InJournal, &sh->dev[i].flags);
4261e6d690bSSong Liu 			clear_bit(R5_LOCKED, &sh->dev[i].flags);
4271e6d690bSSong Liu 		}
4281e6d690bSSong Liu 	clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
4291e6d690bSSong Liu }
4301e6d690bSSong Liu 
4311e6d690bSSong Liu /*
4321e6d690bSSong Liu  * this journal write must contain full parity,
4331e6d690bSSong Liu  * it may also contain some data pages
4341e6d690bSSong Liu  */
4351e6d690bSSong Liu static void r5c_handle_parity_cached(struct stripe_head *sh)
4361e6d690bSSong Liu {
4371e6d690bSSong Liu 	int i;
4381e6d690bSSong Liu 
4391e6d690bSSong Liu 	for (i = sh->disks; i--; )
4401e6d690bSSong Liu 		if (test_bit(R5_InJournal, &sh->dev[i].flags))
4411e6d690bSSong Liu 			set_bit(R5_Wantwrite, &sh->dev[i].flags);
4422ded3703SSong Liu }
4432ded3703SSong Liu 
4442ded3703SSong Liu /*
4452ded3703SSong Liu  * Setting proper flags after writing (or flushing) data and/or parity to the
4462ded3703SSong Liu  * log device. This is called from r5l_log_endio() or r5l_log_flush_endio().
4472ded3703SSong Liu  */
4482ded3703SSong Liu static void r5c_finish_cache_stripe(struct stripe_head *sh)
4492ded3703SSong Liu {
4502ded3703SSong Liu 	struct r5l_log *log = sh->raid_conf->log;
4512ded3703SSong Liu 
4522ded3703SSong Liu 	if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
4532ded3703SSong Liu 		BUG_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
4542ded3703SSong Liu 		/*
4552ded3703SSong Liu 		 * Set R5_InJournal for parity dev[pd_idx]. This means
4562ded3703SSong Liu 		 * all data AND parity in the journal. For RAID 6, it is
4572ded3703SSong Liu 		 * NOT necessary to set the flag for dev[qd_idx], as the
4582ded3703SSong Liu 		 * two parities are written out together.
4592ded3703SSong Liu 		 */
4602ded3703SSong Liu 		set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
4611e6d690bSSong Liu 	} else if (test_bit(STRIPE_R5C_CACHING, &sh->state)) {
4621e6d690bSSong Liu 		r5c_handle_data_cached(sh);
4631e6d690bSSong Liu 	} else {
4641e6d690bSSong Liu 		r5c_handle_parity_cached(sh);
4651e6d690bSSong Liu 		set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
4661e6d690bSSong Liu 	}
4672ded3703SSong Liu }
4682ded3703SSong Liu 
469d8858f43SChristoph Hellwig static void r5l_io_run_stripes(struct r5l_io_unit *io)
470d8858f43SChristoph Hellwig {
471d8858f43SChristoph Hellwig 	struct stripe_head *sh, *next;
472d8858f43SChristoph Hellwig 
473d8858f43SChristoph Hellwig 	list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
474d8858f43SChristoph Hellwig 		list_del_init(&sh->log_list);
4752ded3703SSong Liu 
4762ded3703SSong Liu 		r5c_finish_cache_stripe(sh);
4772ded3703SSong Liu 
478d8858f43SChristoph Hellwig 		set_bit(STRIPE_HANDLE, &sh->state);
479d8858f43SChristoph Hellwig 		raid5_release_stripe(sh);
480d8858f43SChristoph Hellwig 	}
481d8858f43SChristoph Hellwig }
482d8858f43SChristoph Hellwig 
48356fef7c6SChristoph Hellwig static void r5l_log_run_stripes(struct r5l_log *log)
48456fef7c6SChristoph Hellwig {
48556fef7c6SChristoph Hellwig 	struct r5l_io_unit *io, *next;
48656fef7c6SChristoph Hellwig 
48756fef7c6SChristoph Hellwig 	assert_spin_locked(&log->io_list_lock);
48856fef7c6SChristoph Hellwig 
48956fef7c6SChristoph Hellwig 	list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
49056fef7c6SChristoph Hellwig 		/* don't change list order */
49156fef7c6SChristoph Hellwig 		if (io->state < IO_UNIT_IO_END)
49256fef7c6SChristoph Hellwig 			break;
49356fef7c6SChristoph Hellwig 
49456fef7c6SChristoph Hellwig 		list_move_tail(&io->log_sibling, &log->finished_ios);
49556fef7c6SChristoph Hellwig 		r5l_io_run_stripes(io);
49656fef7c6SChristoph Hellwig 	}
49756fef7c6SChristoph Hellwig }
49856fef7c6SChristoph Hellwig 
4993848c0bcSChristoph Hellwig static void r5l_move_to_end_ios(struct r5l_log *log)
5003848c0bcSChristoph Hellwig {
5013848c0bcSChristoph Hellwig 	struct r5l_io_unit *io, *next;
5023848c0bcSChristoph Hellwig 
5033848c0bcSChristoph Hellwig 	assert_spin_locked(&log->io_list_lock);
5043848c0bcSChristoph Hellwig 
5053848c0bcSChristoph Hellwig 	list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
5063848c0bcSChristoph Hellwig 		/* don't change list order */
5073848c0bcSChristoph Hellwig 		if (io->state < IO_UNIT_IO_END)
5083848c0bcSChristoph Hellwig 			break;
5093848c0bcSChristoph Hellwig 		list_move_tail(&io->log_sibling, &log->io_end_ios);
5103848c0bcSChristoph Hellwig 	}
5113848c0bcSChristoph Hellwig }
5123848c0bcSChristoph Hellwig 
5133bddb7f8SSong Liu static void __r5l_stripe_write_finished(struct r5l_io_unit *io);
514f6bed0efSShaohua Li static void r5l_log_endio(struct bio *bio)
515f6bed0efSShaohua Li {
516f6bed0efSShaohua Li 	struct r5l_io_unit *io = bio->bi_private;
5173bddb7f8SSong Liu 	struct r5l_io_unit *io_deferred;
518f6bed0efSShaohua Li 	struct r5l_log *log = io->log;
519509ffec7SChristoph Hellwig 	unsigned long flags;
520f6bed0efSShaohua Li 
5216e74a9cfSShaohua Li 	if (bio->bi_error)
5226e74a9cfSShaohua Li 		md_error(log->rdev->mddev, log->rdev);
5236e74a9cfSShaohua Li 
524f6bed0efSShaohua Li 	bio_put(bio);
525e8deb638SChristoph Hellwig 	mempool_free(io->meta_page, log->meta_pool);
526f6bed0efSShaohua Li 
527509ffec7SChristoph Hellwig 	spin_lock_irqsave(&log->io_list_lock, flags);
528509ffec7SChristoph Hellwig 	__r5l_set_io_unit_state(io, IO_UNIT_IO_END);
52956fef7c6SChristoph Hellwig 	if (log->need_cache_flush)
5303848c0bcSChristoph Hellwig 		r5l_move_to_end_ios(log);
53156fef7c6SChristoph Hellwig 	else
53256fef7c6SChristoph Hellwig 		r5l_log_run_stripes(log);
5333bddb7f8SSong Liu 	if (!list_empty(&log->running_ios)) {
5343bddb7f8SSong Liu 		/*
5353bddb7f8SSong Liu 		 * FLUSH/FUA io_unit is deferred because of ordering, now we
5363bddb7f8SSong Liu 		 * can dispatch it
5373bddb7f8SSong Liu 		 */
5383bddb7f8SSong Liu 		io_deferred = list_first_entry(&log->running_ios,
5393bddb7f8SSong Liu 					       struct r5l_io_unit, log_sibling);
5403bddb7f8SSong Liu 		if (io_deferred->io_deferred)
5413bddb7f8SSong Liu 			schedule_work(&log->deferred_io_work);
5423bddb7f8SSong Liu 	}
5433bddb7f8SSong Liu 
544509ffec7SChristoph Hellwig 	spin_unlock_irqrestore(&log->io_list_lock, flags);
545509ffec7SChristoph Hellwig 
54656fef7c6SChristoph Hellwig 	if (log->need_cache_flush)
547f6bed0efSShaohua Li 		md_wakeup_thread(log->rdev->mddev->thread);
5483bddb7f8SSong Liu 
5493bddb7f8SSong Liu 	if (io->has_null_flush) {
5503bddb7f8SSong Liu 		struct bio *bi;
5513bddb7f8SSong Liu 
5523bddb7f8SSong Liu 		WARN_ON(bio_list_empty(&io->flush_barriers));
5533bddb7f8SSong Liu 		while ((bi = bio_list_pop(&io->flush_barriers)) != NULL) {
5543bddb7f8SSong Liu 			bio_endio(bi);
5553bddb7f8SSong Liu 			atomic_dec(&io->pending_stripe);
5563bddb7f8SSong Liu 		}
5573bddb7f8SSong Liu 		if (atomic_read(&io->pending_stripe) == 0)
5583bddb7f8SSong Liu 			__r5l_stripe_write_finished(io);
5593bddb7f8SSong Liu 	}
5603bddb7f8SSong Liu }
5613bddb7f8SSong Liu 
5623bddb7f8SSong Liu static void r5l_do_submit_io(struct r5l_log *log, struct r5l_io_unit *io)
5633bddb7f8SSong Liu {
5643bddb7f8SSong Liu 	unsigned long flags;
5653bddb7f8SSong Liu 
5663bddb7f8SSong Liu 	spin_lock_irqsave(&log->io_list_lock, flags);
5673bddb7f8SSong Liu 	__r5l_set_io_unit_state(io, IO_UNIT_IO_START);
5683bddb7f8SSong Liu 	spin_unlock_irqrestore(&log->io_list_lock, flags);
5693bddb7f8SSong Liu 
5703bddb7f8SSong Liu 	if (io->has_flush)
5713bddb7f8SSong Liu 		bio_set_op_attrs(io->current_bio, REQ_OP_WRITE, WRITE_FLUSH);
5723bddb7f8SSong Liu 	if (io->has_fua)
5733bddb7f8SSong Liu 		bio_set_op_attrs(io->current_bio, REQ_OP_WRITE, WRITE_FUA);
5743bddb7f8SSong Liu 	submit_bio(io->current_bio);
5753bddb7f8SSong Liu 
5763bddb7f8SSong Liu 	if (!io->split_bio)
5773bddb7f8SSong Liu 		return;
5783bddb7f8SSong Liu 
5793bddb7f8SSong Liu 	if (io->has_flush)
5803bddb7f8SSong Liu 		bio_set_op_attrs(io->split_bio, REQ_OP_WRITE, WRITE_FLUSH);
5813bddb7f8SSong Liu 	if (io->has_fua)
5823bddb7f8SSong Liu 		bio_set_op_attrs(io->split_bio, REQ_OP_WRITE, WRITE_FUA);
5833bddb7f8SSong Liu 	submit_bio(io->split_bio);
5843bddb7f8SSong Liu }
5853bddb7f8SSong Liu 
5863bddb7f8SSong Liu /* deferred io_unit will be dispatched here */
5873bddb7f8SSong Liu static void r5l_submit_io_async(struct work_struct *work)
5883bddb7f8SSong Liu {
5893bddb7f8SSong Liu 	struct r5l_log *log = container_of(work, struct r5l_log,
5903bddb7f8SSong Liu 					   deferred_io_work);
5913bddb7f8SSong Liu 	struct r5l_io_unit *io = NULL;
5923bddb7f8SSong Liu 	unsigned long flags;
5933bddb7f8SSong Liu 
5943bddb7f8SSong Liu 	spin_lock_irqsave(&log->io_list_lock, flags);
5953bddb7f8SSong Liu 	if (!list_empty(&log->running_ios)) {
5963bddb7f8SSong Liu 		io = list_first_entry(&log->running_ios, struct r5l_io_unit,
5973bddb7f8SSong Liu 				      log_sibling);
5983bddb7f8SSong Liu 		if (!io->io_deferred)
5993bddb7f8SSong Liu 			io = NULL;
6003bddb7f8SSong Liu 		else
6013bddb7f8SSong Liu 			io->io_deferred = 0;
6023bddb7f8SSong Liu 	}
6033bddb7f8SSong Liu 	spin_unlock_irqrestore(&log->io_list_lock, flags);
6043bddb7f8SSong Liu 	if (io)
6053bddb7f8SSong Liu 		r5l_do_submit_io(log, io);
606f6bed0efSShaohua Li }
607f6bed0efSShaohua Li 
608f6bed0efSShaohua Li static void r5l_submit_current_io(struct r5l_log *log)
609f6bed0efSShaohua Li {
610f6bed0efSShaohua Li 	struct r5l_io_unit *io = log->current_io;
6113bddb7f8SSong Liu 	struct bio *bio;
612f6bed0efSShaohua Li 	struct r5l_meta_block *block;
613509ffec7SChristoph Hellwig 	unsigned long flags;
614f6bed0efSShaohua Li 	u32 crc;
6153bddb7f8SSong Liu 	bool do_submit = true;
616f6bed0efSShaohua Li 
617f6bed0efSShaohua Li 	if (!io)
618f6bed0efSShaohua Li 		return;
619f6bed0efSShaohua Li 
620f6bed0efSShaohua Li 	block = page_address(io->meta_page);
621f6bed0efSShaohua Li 	block->meta_size = cpu_to_le32(io->meta_offset);
6225cb2fbd6SShaohua Li 	crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
623f6bed0efSShaohua Li 	block->checksum = cpu_to_le32(crc);
6243bddb7f8SSong Liu 	bio = io->current_bio;
625f6bed0efSShaohua Li 
626f6bed0efSShaohua Li 	log->current_io = NULL;
627509ffec7SChristoph Hellwig 	spin_lock_irqsave(&log->io_list_lock, flags);
6283bddb7f8SSong Liu 	if (io->has_flush || io->has_fua) {
6293bddb7f8SSong Liu 		if (io != list_first_entry(&log->running_ios,
6303bddb7f8SSong Liu 					   struct r5l_io_unit, log_sibling)) {
6313bddb7f8SSong Liu 			io->io_deferred = 1;
6323bddb7f8SSong Liu 			do_submit = false;
6333bddb7f8SSong Liu 		}
6343bddb7f8SSong Liu 	}
635509ffec7SChristoph Hellwig 	spin_unlock_irqrestore(&log->io_list_lock, flags);
6363bddb7f8SSong Liu 	if (do_submit)
6373bddb7f8SSong Liu 		r5l_do_submit_io(log, io);
638f6bed0efSShaohua Li }
639f6bed0efSShaohua Li 
6406143e2ceSChristoph Hellwig static struct bio *r5l_bio_alloc(struct r5l_log *log)
641b349feb3SChristoph Hellwig {
642c38d29b3SChristoph Hellwig 	struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
643b349feb3SChristoph Hellwig 
644796a5cf0SMike Christie 	bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
645b349feb3SChristoph Hellwig 	bio->bi_bdev = log->rdev->bdev;
6461e932a37SChristoph Hellwig 	bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
647b349feb3SChristoph Hellwig 
648b349feb3SChristoph Hellwig 	return bio;
649b349feb3SChristoph Hellwig }
650b349feb3SChristoph Hellwig 
651c1b99198SChristoph Hellwig static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
652c1b99198SChristoph Hellwig {
653c1b99198SChristoph Hellwig 	log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
654c1b99198SChristoph Hellwig 
655a39f7afdSSong Liu 	r5c_update_log_state(log);
656c1b99198SChristoph Hellwig 	/*
657c1b99198SChristoph Hellwig 	 * If we filled up the log device start from the beginning again,
658c1b99198SChristoph Hellwig 	 * which will require a new bio.
659c1b99198SChristoph Hellwig 	 *
660c1b99198SChristoph Hellwig 	 * Note: for this to work properly the log size needs to me a multiple
661c1b99198SChristoph Hellwig 	 * of BLOCK_SECTORS.
662c1b99198SChristoph Hellwig 	 */
663c1b99198SChristoph Hellwig 	if (log->log_start == 0)
6646143e2ceSChristoph Hellwig 		io->need_split_bio = true;
665c1b99198SChristoph Hellwig 
666c1b99198SChristoph Hellwig 	io->log_end = log->log_start;
667c1b99198SChristoph Hellwig }
668c1b99198SChristoph Hellwig 
669f6bed0efSShaohua Li static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
670f6bed0efSShaohua Li {
671f6bed0efSShaohua Li 	struct r5l_io_unit *io;
672f6bed0efSShaohua Li 	struct r5l_meta_block *block;
673f6bed0efSShaohua Li 
6745036c390SChristoph Hellwig 	io = mempool_alloc(log->io_pool, GFP_ATOMIC);
6755036c390SChristoph Hellwig 	if (!io)
6765036c390SChristoph Hellwig 		return NULL;
6775036c390SChristoph Hellwig 	memset(io, 0, sizeof(*io));
6785036c390SChristoph Hellwig 
67951039cd0SChristoph Hellwig 	io->log = log;
68051039cd0SChristoph Hellwig 	INIT_LIST_HEAD(&io->log_sibling);
68151039cd0SChristoph Hellwig 	INIT_LIST_HEAD(&io->stripe_list);
6823bddb7f8SSong Liu 	bio_list_init(&io->flush_barriers);
68351039cd0SChristoph Hellwig 	io->state = IO_UNIT_RUNNING;
684f6bed0efSShaohua Li 
685e8deb638SChristoph Hellwig 	io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
686f6bed0efSShaohua Li 	block = page_address(io->meta_page);
687e8deb638SChristoph Hellwig 	clear_page(block);
688f6bed0efSShaohua Li 	block->magic = cpu_to_le32(R5LOG_MAGIC);
689f6bed0efSShaohua Li 	block->version = R5LOG_VERSION;
690f6bed0efSShaohua Li 	block->seq = cpu_to_le64(log->seq);
691f6bed0efSShaohua Li 	block->position = cpu_to_le64(log->log_start);
692f6bed0efSShaohua Li 
693f6bed0efSShaohua Li 	io->log_start = log->log_start;
694f6bed0efSShaohua Li 	io->meta_offset = sizeof(struct r5l_meta_block);
6952b8ef16eSChristoph Hellwig 	io->seq = log->seq++;
696f6bed0efSShaohua Li 
6976143e2ceSChristoph Hellwig 	io->current_bio = r5l_bio_alloc(log);
6986143e2ceSChristoph Hellwig 	io->current_bio->bi_end_io = r5l_log_endio;
6996143e2ceSChristoph Hellwig 	io->current_bio->bi_private = io;
700b349feb3SChristoph Hellwig 	bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
701f6bed0efSShaohua Li 
702c1b99198SChristoph Hellwig 	r5_reserve_log_entry(log, io);
703f6bed0efSShaohua Li 
704f6bed0efSShaohua Li 	spin_lock_irq(&log->io_list_lock);
705f6bed0efSShaohua Li 	list_add_tail(&io->log_sibling, &log->running_ios);
706f6bed0efSShaohua Li 	spin_unlock_irq(&log->io_list_lock);
707f6bed0efSShaohua Li 
708f6bed0efSShaohua Li 	return io;
709f6bed0efSShaohua Li }
710f6bed0efSShaohua Li 
711f6bed0efSShaohua Li static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
712f6bed0efSShaohua Li {
71322581f58SChristoph Hellwig 	if (log->current_io &&
71422581f58SChristoph Hellwig 	    log->current_io->meta_offset + payload_size > PAGE_SIZE)
715f6bed0efSShaohua Li 		r5l_submit_current_io(log);
716f6bed0efSShaohua Li 
7175036c390SChristoph Hellwig 	if (!log->current_io) {
718f6bed0efSShaohua Li 		log->current_io = r5l_new_meta(log);
7195036c390SChristoph Hellwig 		if (!log->current_io)
7205036c390SChristoph Hellwig 			return -ENOMEM;
7215036c390SChristoph Hellwig 	}
7225036c390SChristoph Hellwig 
723f6bed0efSShaohua Li 	return 0;
724f6bed0efSShaohua Li }
725f6bed0efSShaohua Li 
726f6bed0efSShaohua Li static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
727f6bed0efSShaohua Li 				    sector_t location,
728f6bed0efSShaohua Li 				    u32 checksum1, u32 checksum2,
729f6bed0efSShaohua Li 				    bool checksum2_valid)
730f6bed0efSShaohua Li {
731f6bed0efSShaohua Li 	struct r5l_io_unit *io = log->current_io;
732f6bed0efSShaohua Li 	struct r5l_payload_data_parity *payload;
733f6bed0efSShaohua Li 
734f6bed0efSShaohua Li 	payload = page_address(io->meta_page) + io->meta_offset;
735f6bed0efSShaohua Li 	payload->header.type = cpu_to_le16(type);
736f6bed0efSShaohua Li 	payload->header.flags = cpu_to_le16(0);
737f6bed0efSShaohua Li 	payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
738f6bed0efSShaohua Li 				    (PAGE_SHIFT - 9));
739f6bed0efSShaohua Li 	payload->location = cpu_to_le64(location);
740f6bed0efSShaohua Li 	payload->checksum[0] = cpu_to_le32(checksum1);
741f6bed0efSShaohua Li 	if (checksum2_valid)
742f6bed0efSShaohua Li 		payload->checksum[1] = cpu_to_le32(checksum2);
743f6bed0efSShaohua Li 
744f6bed0efSShaohua Li 	io->meta_offset += sizeof(struct r5l_payload_data_parity) +
745f6bed0efSShaohua Li 		sizeof(__le32) * (1 + !!checksum2_valid);
746f6bed0efSShaohua Li }
747f6bed0efSShaohua Li 
748f6bed0efSShaohua Li static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
749f6bed0efSShaohua Li {
750f6bed0efSShaohua Li 	struct r5l_io_unit *io = log->current_io;
751f6bed0efSShaohua Li 
7526143e2ceSChristoph Hellwig 	if (io->need_split_bio) {
7533bddb7f8SSong Liu 		BUG_ON(io->split_bio);
7543bddb7f8SSong Liu 		io->split_bio = io->current_bio;
7556143e2ceSChristoph Hellwig 		io->current_bio = r5l_bio_alloc(log);
7563bddb7f8SSong Liu 		bio_chain(io->current_bio, io->split_bio);
7573bddb7f8SSong Liu 		io->need_split_bio = false;
758f6bed0efSShaohua Li 	}
759f6bed0efSShaohua Li 
7606143e2ceSChristoph Hellwig 	if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
7616143e2ceSChristoph Hellwig 		BUG();
7626143e2ceSChristoph Hellwig 
763c1b99198SChristoph Hellwig 	r5_reserve_log_entry(log, io);
764f6bed0efSShaohua Li }
765f6bed0efSShaohua Li 
7665036c390SChristoph Hellwig static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
767f6bed0efSShaohua Li 			   int data_pages, int parity_pages)
768f6bed0efSShaohua Li {
769f6bed0efSShaohua Li 	int i;
770f6bed0efSShaohua Li 	int meta_size;
7715036c390SChristoph Hellwig 	int ret;
772f6bed0efSShaohua Li 	struct r5l_io_unit *io;
773f6bed0efSShaohua Li 
774f6bed0efSShaohua Li 	meta_size =
775f6bed0efSShaohua Li 		((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
776f6bed0efSShaohua Li 		 * data_pages) +
777f6bed0efSShaohua Li 		sizeof(struct r5l_payload_data_parity) +
778f6bed0efSShaohua Li 		sizeof(__le32) * parity_pages;
779f6bed0efSShaohua Li 
7805036c390SChristoph Hellwig 	ret = r5l_get_meta(log, meta_size);
7815036c390SChristoph Hellwig 	if (ret)
7825036c390SChristoph Hellwig 		return ret;
7835036c390SChristoph Hellwig 
784f6bed0efSShaohua Li 	io = log->current_io;
785f6bed0efSShaohua Li 
7863bddb7f8SSong Liu 	if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state))
7873bddb7f8SSong Liu 		io->has_flush = 1;
7883bddb7f8SSong Liu 
789f6bed0efSShaohua Li 	for (i = 0; i < sh->disks; i++) {
7901e6d690bSSong Liu 		if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
7911e6d690bSSong Liu 		    test_bit(R5_InJournal, &sh->dev[i].flags))
792f6bed0efSShaohua Li 			continue;
793f6bed0efSShaohua Li 		if (i == sh->pd_idx || i == sh->qd_idx)
794f6bed0efSShaohua Li 			continue;
7953bddb7f8SSong Liu 		if (test_bit(R5_WantFUA, &sh->dev[i].flags) &&
7963bddb7f8SSong Liu 		    log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) {
7973bddb7f8SSong Liu 			io->has_fua = 1;
7983bddb7f8SSong Liu 			/*
7993bddb7f8SSong Liu 			 * we need to flush journal to make sure recovery can
8003bddb7f8SSong Liu 			 * reach the data with fua flag
8013bddb7f8SSong Liu 			 */
8023bddb7f8SSong Liu 			io->has_flush = 1;
8033bddb7f8SSong Liu 		}
804f6bed0efSShaohua Li 		r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
805f6bed0efSShaohua Li 					raid5_compute_blocknr(sh, i, 0),
806f6bed0efSShaohua Li 					sh->dev[i].log_checksum, 0, false);
807f6bed0efSShaohua Li 		r5l_append_payload_page(log, sh->dev[i].page);
808f6bed0efSShaohua Li 	}
809f6bed0efSShaohua Li 
8102ded3703SSong Liu 	if (parity_pages == 2) {
811f6bed0efSShaohua Li 		r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
812f6bed0efSShaohua Li 					sh->sector, sh->dev[sh->pd_idx].log_checksum,
813f6bed0efSShaohua Li 					sh->dev[sh->qd_idx].log_checksum, true);
814f6bed0efSShaohua Li 		r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
815f6bed0efSShaohua Li 		r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
8162ded3703SSong Liu 	} else if (parity_pages == 1) {
817f6bed0efSShaohua Li 		r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
818f6bed0efSShaohua Li 					sh->sector, sh->dev[sh->pd_idx].log_checksum,
819f6bed0efSShaohua Li 					0, false);
820f6bed0efSShaohua Li 		r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
8212ded3703SSong Liu 	} else  /* Just writing data, not parity, in caching phase */
8222ded3703SSong Liu 		BUG_ON(parity_pages != 0);
823f6bed0efSShaohua Li 
824f6bed0efSShaohua Li 	list_add_tail(&sh->log_list, &io->stripe_list);
825f6bed0efSShaohua Li 	atomic_inc(&io->pending_stripe);
826f6bed0efSShaohua Li 	sh->log_io = io;
8275036c390SChristoph Hellwig 
828a39f7afdSSong Liu 	if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
829a39f7afdSSong Liu 		return 0;
830a39f7afdSSong Liu 
831a39f7afdSSong Liu 	if (sh->log_start == MaxSector) {
832a39f7afdSSong Liu 		BUG_ON(!list_empty(&sh->r5c));
833a39f7afdSSong Liu 		sh->log_start = io->log_start;
834a39f7afdSSong Liu 		spin_lock_irq(&log->stripe_in_journal_lock);
835a39f7afdSSong Liu 		list_add_tail(&sh->r5c,
836a39f7afdSSong Liu 			      &log->stripe_in_journal_list);
837a39f7afdSSong Liu 		spin_unlock_irq(&log->stripe_in_journal_lock);
838a39f7afdSSong Liu 		atomic_inc(&log->stripe_in_journal_count);
839a39f7afdSSong Liu 	}
8405036c390SChristoph Hellwig 	return 0;
841f6bed0efSShaohua Li }
842f6bed0efSShaohua Li 
843a39f7afdSSong Liu /* add stripe to no_space_stripes, and then wake up reclaim */
844a39f7afdSSong Liu static inline void r5l_add_no_space_stripe(struct r5l_log *log,
845a39f7afdSSong Liu 					   struct stripe_head *sh)
846a39f7afdSSong Liu {
847a39f7afdSSong Liu 	spin_lock(&log->no_space_stripes_lock);
848a39f7afdSSong Liu 	list_add_tail(&sh->log_list, &log->no_space_stripes);
849a39f7afdSSong Liu 	spin_unlock(&log->no_space_stripes_lock);
850a39f7afdSSong Liu }
851a39f7afdSSong Liu 
852f6bed0efSShaohua Li /*
853f6bed0efSShaohua Li  * running in raid5d, where reclaim could wait for raid5d too (when it flushes
854f6bed0efSShaohua Li  * data from log to raid disks), so we shouldn't wait for reclaim here
855f6bed0efSShaohua Li  */
856f6bed0efSShaohua Li int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
857f6bed0efSShaohua Li {
858a39f7afdSSong Liu 	struct r5conf *conf = sh->raid_conf;
859f6bed0efSShaohua Li 	int write_disks = 0;
860f6bed0efSShaohua Li 	int data_pages, parity_pages;
861f6bed0efSShaohua Li 	int reserve;
862f6bed0efSShaohua Li 	int i;
8635036c390SChristoph Hellwig 	int ret = 0;
864a39f7afdSSong Liu 	bool wake_reclaim = false;
865f6bed0efSShaohua Li 
866f6bed0efSShaohua Li 	if (!log)
867f6bed0efSShaohua Li 		return -EAGAIN;
868f6bed0efSShaohua Li 	/* Don't support stripe batch */
869f6bed0efSShaohua Li 	if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
870f6bed0efSShaohua Li 	    test_bit(STRIPE_SYNCING, &sh->state)) {
871f6bed0efSShaohua Li 		/* the stripe is written to log, we start writing it to raid */
872f6bed0efSShaohua Li 		clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
873f6bed0efSShaohua Li 		return -EAGAIN;
874f6bed0efSShaohua Li 	}
875f6bed0efSShaohua Li 
8762ded3703SSong Liu 	WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
8772ded3703SSong Liu 
878f6bed0efSShaohua Li 	for (i = 0; i < sh->disks; i++) {
879f6bed0efSShaohua Li 		void *addr;
880f6bed0efSShaohua Li 
8811e6d690bSSong Liu 		if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
8821e6d690bSSong Liu 		    test_bit(R5_InJournal, &sh->dev[i].flags))
883f6bed0efSShaohua Li 			continue;
8841e6d690bSSong Liu 
885f6bed0efSShaohua Li 		write_disks++;
886f6bed0efSShaohua Li 		/* checksum is already calculated in last run */
887f6bed0efSShaohua Li 		if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
888f6bed0efSShaohua Li 			continue;
889f6bed0efSShaohua Li 		addr = kmap_atomic(sh->dev[i].page);
8905cb2fbd6SShaohua Li 		sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
891f6bed0efSShaohua Li 						    addr, PAGE_SIZE);
892f6bed0efSShaohua Li 		kunmap_atomic(addr);
893f6bed0efSShaohua Li 	}
894f6bed0efSShaohua Li 	parity_pages = 1 + !!(sh->qd_idx >= 0);
895f6bed0efSShaohua Li 	data_pages = write_disks - parity_pages;
896f6bed0efSShaohua Li 
897f6bed0efSShaohua Li 	set_bit(STRIPE_LOG_TRAPPED, &sh->state);
898253f9fd4SShaohua Li 	/*
899253f9fd4SShaohua Li 	 * The stripe must enter state machine again to finish the write, so
900253f9fd4SShaohua Li 	 * don't delay.
901253f9fd4SShaohua Li 	 */
902253f9fd4SShaohua Li 	clear_bit(STRIPE_DELAYED, &sh->state);
903f6bed0efSShaohua Li 	atomic_inc(&sh->count);
904f6bed0efSShaohua Li 
905f6bed0efSShaohua Li 	mutex_lock(&log->io_mutex);
906f6bed0efSShaohua Li 	/* meta + data */
907f6bed0efSShaohua Li 	reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
908f6bed0efSShaohua Li 
909a39f7afdSSong Liu 	if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
910a39f7afdSSong Liu 		if (!r5l_has_free_space(log, reserve)) {
911a39f7afdSSong Liu 			r5l_add_no_space_stripe(log, sh);
912a39f7afdSSong Liu 			wake_reclaim = true;
9135036c390SChristoph Hellwig 		} else {
9145036c390SChristoph Hellwig 			ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
9155036c390SChristoph Hellwig 			if (ret) {
9165036c390SChristoph Hellwig 				spin_lock_irq(&log->io_list_lock);
917a39f7afdSSong Liu 				list_add_tail(&sh->log_list,
918a39f7afdSSong Liu 					      &log->no_mem_stripes);
9195036c390SChristoph Hellwig 				spin_unlock_irq(&log->io_list_lock);
920f6bed0efSShaohua Li 			}
9215036c390SChristoph Hellwig 		}
922a39f7afdSSong Liu 	} else {  /* R5C_JOURNAL_MODE_WRITE_BACK */
923a39f7afdSSong Liu 		/*
924a39f7afdSSong Liu 		 * log space critical, do not process stripes that are
925a39f7afdSSong Liu 		 * not in cache yet (sh->log_start == MaxSector).
926a39f7afdSSong Liu 		 */
927a39f7afdSSong Liu 		if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
928a39f7afdSSong Liu 		    sh->log_start == MaxSector) {
929a39f7afdSSong Liu 			r5l_add_no_space_stripe(log, sh);
930a39f7afdSSong Liu 			wake_reclaim = true;
931a39f7afdSSong Liu 			reserve = 0;
932a39f7afdSSong Liu 		} else if (!r5l_has_free_space(log, reserve)) {
933a39f7afdSSong Liu 			if (sh->log_start == log->last_checkpoint)
934a39f7afdSSong Liu 				BUG();
935a39f7afdSSong Liu 			else
936a39f7afdSSong Liu 				r5l_add_no_space_stripe(log, sh);
937a39f7afdSSong Liu 		} else {
938a39f7afdSSong Liu 			ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
939a39f7afdSSong Liu 			if (ret) {
940a39f7afdSSong Liu 				spin_lock_irq(&log->io_list_lock);
941a39f7afdSSong Liu 				list_add_tail(&sh->log_list,
942a39f7afdSSong Liu 					      &log->no_mem_stripes);
943a39f7afdSSong Liu 				spin_unlock_irq(&log->io_list_lock);
944a39f7afdSSong Liu 			}
945a39f7afdSSong Liu 		}
946a39f7afdSSong Liu 	}
947f6bed0efSShaohua Li 
9485036c390SChristoph Hellwig 	mutex_unlock(&log->io_mutex);
949a39f7afdSSong Liu 	if (wake_reclaim)
950a39f7afdSSong Liu 		r5l_wake_reclaim(log, reserve);
951f6bed0efSShaohua Li 	return 0;
952f6bed0efSShaohua Li }
953f6bed0efSShaohua Li 
954f6bed0efSShaohua Li void r5l_write_stripe_run(struct r5l_log *log)
955f6bed0efSShaohua Li {
956f6bed0efSShaohua Li 	if (!log)
957f6bed0efSShaohua Li 		return;
958f6bed0efSShaohua Li 	mutex_lock(&log->io_mutex);
959f6bed0efSShaohua Li 	r5l_submit_current_io(log);
960f6bed0efSShaohua Li 	mutex_unlock(&log->io_mutex);
961f6bed0efSShaohua Li }
962f6bed0efSShaohua Li 
963828cbe98SShaohua Li int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
964828cbe98SShaohua Li {
965828cbe98SShaohua Li 	if (!log)
966828cbe98SShaohua Li 		return -ENODEV;
9673bddb7f8SSong Liu 
9683bddb7f8SSong Liu 	if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
969828cbe98SShaohua Li 		/*
9703bddb7f8SSong Liu 		 * in write through (journal only)
9713bddb7f8SSong Liu 		 * we flush log disk cache first, then write stripe data to
9723bddb7f8SSong Liu 		 * raid disks. So if bio is finished, the log disk cache is
9733bddb7f8SSong Liu 		 * flushed already. The recovery guarantees we can recovery
9743bddb7f8SSong Liu 		 * the bio from log disk, so we don't need to flush again
975828cbe98SShaohua Li 		 */
976828cbe98SShaohua Li 		if (bio->bi_iter.bi_size == 0) {
977828cbe98SShaohua Li 			bio_endio(bio);
978828cbe98SShaohua Li 			return 0;
979828cbe98SShaohua Li 		}
9801eff9d32SJens Axboe 		bio->bi_opf &= ~REQ_PREFLUSH;
9813bddb7f8SSong Liu 	} else {
9823bddb7f8SSong Liu 		/* write back (with cache) */
9833bddb7f8SSong Liu 		if (bio->bi_iter.bi_size == 0) {
9843bddb7f8SSong Liu 			mutex_lock(&log->io_mutex);
9853bddb7f8SSong Liu 			r5l_get_meta(log, 0);
9863bddb7f8SSong Liu 			bio_list_add(&log->current_io->flush_barriers, bio);
9873bddb7f8SSong Liu 			log->current_io->has_flush = 1;
9883bddb7f8SSong Liu 			log->current_io->has_null_flush = 1;
9893bddb7f8SSong Liu 			atomic_inc(&log->current_io->pending_stripe);
9903bddb7f8SSong Liu 			r5l_submit_current_io(log);
9913bddb7f8SSong Liu 			mutex_unlock(&log->io_mutex);
9923bddb7f8SSong Liu 			return 0;
9933bddb7f8SSong Liu 		}
9943bddb7f8SSong Liu 	}
995828cbe98SShaohua Li 	return -EAGAIN;
996828cbe98SShaohua Li }
997828cbe98SShaohua Li 
998f6bed0efSShaohua Li /* This will run after log space is reclaimed */
999f6bed0efSShaohua Li static void r5l_run_no_space_stripes(struct r5l_log *log)
1000f6bed0efSShaohua Li {
1001f6bed0efSShaohua Li 	struct stripe_head *sh;
1002f6bed0efSShaohua Li 
1003f6bed0efSShaohua Li 	spin_lock(&log->no_space_stripes_lock);
1004f6bed0efSShaohua Li 	while (!list_empty(&log->no_space_stripes)) {
1005f6bed0efSShaohua Li 		sh = list_first_entry(&log->no_space_stripes,
1006f6bed0efSShaohua Li 				      struct stripe_head, log_list);
1007f6bed0efSShaohua Li 		list_del_init(&sh->log_list);
1008f6bed0efSShaohua Li 		set_bit(STRIPE_HANDLE, &sh->state);
1009f6bed0efSShaohua Li 		raid5_release_stripe(sh);
1010f6bed0efSShaohua Li 	}
1011f6bed0efSShaohua Li 	spin_unlock(&log->no_space_stripes_lock);
1012f6bed0efSShaohua Li }
1013f6bed0efSShaohua Li 
1014a39f7afdSSong Liu /*
1015a39f7afdSSong Liu  * calculate new last_checkpoint
1016a39f7afdSSong Liu  * for write through mode, returns log->next_checkpoint
1017a39f7afdSSong Liu  * for write back, returns log_start of first sh in stripe_in_journal_list
1018a39f7afdSSong Liu  */
1019a39f7afdSSong Liu static sector_t r5c_calculate_new_cp(struct r5conf *conf)
1020a39f7afdSSong Liu {
1021a39f7afdSSong Liu 	struct stripe_head *sh;
1022a39f7afdSSong Liu 	struct r5l_log *log = conf->log;
1023a39f7afdSSong Liu 	sector_t new_cp;
1024a39f7afdSSong Liu 	unsigned long flags;
1025a39f7afdSSong Liu 
1026a39f7afdSSong Liu 	if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
1027a39f7afdSSong Liu 		return log->next_checkpoint;
1028a39f7afdSSong Liu 
1029a39f7afdSSong Liu 	spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
1030a39f7afdSSong Liu 	if (list_empty(&conf->log->stripe_in_journal_list)) {
1031a39f7afdSSong Liu 		/* all stripes flushed */
1032a39f7afdSSong Liu 		spin_unlock(&log->stripe_in_journal_lock);
1033a39f7afdSSong Liu 		return log->next_checkpoint;
1034a39f7afdSSong Liu 	}
1035a39f7afdSSong Liu 	sh = list_first_entry(&conf->log->stripe_in_journal_list,
1036a39f7afdSSong Liu 			      struct stripe_head, r5c);
1037a39f7afdSSong Liu 	new_cp = sh->log_start;
1038a39f7afdSSong Liu 	spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
1039a39f7afdSSong Liu 	return new_cp;
1040a39f7afdSSong Liu }
1041a39f7afdSSong Liu 
104217036461SChristoph Hellwig static sector_t r5l_reclaimable_space(struct r5l_log *log)
104317036461SChristoph Hellwig {
1044a39f7afdSSong Liu 	struct r5conf *conf = log->rdev->mddev->private;
1045a39f7afdSSong Liu 
104617036461SChristoph Hellwig 	return r5l_ring_distance(log, log->last_checkpoint,
1047a39f7afdSSong Liu 				 r5c_calculate_new_cp(conf));
104817036461SChristoph Hellwig }
104917036461SChristoph Hellwig 
10505036c390SChristoph Hellwig static void r5l_run_no_mem_stripe(struct r5l_log *log)
10515036c390SChristoph Hellwig {
10525036c390SChristoph Hellwig 	struct stripe_head *sh;
10535036c390SChristoph Hellwig 
10545036c390SChristoph Hellwig 	assert_spin_locked(&log->io_list_lock);
10555036c390SChristoph Hellwig 
10565036c390SChristoph Hellwig 	if (!list_empty(&log->no_mem_stripes)) {
10575036c390SChristoph Hellwig 		sh = list_first_entry(&log->no_mem_stripes,
10585036c390SChristoph Hellwig 				      struct stripe_head, log_list);
10595036c390SChristoph Hellwig 		list_del_init(&sh->log_list);
10605036c390SChristoph Hellwig 		set_bit(STRIPE_HANDLE, &sh->state);
10615036c390SChristoph Hellwig 		raid5_release_stripe(sh);
10625036c390SChristoph Hellwig 	}
10635036c390SChristoph Hellwig }
10645036c390SChristoph Hellwig 
106504732f74SChristoph Hellwig static bool r5l_complete_finished_ios(struct r5l_log *log)
106617036461SChristoph Hellwig {
106717036461SChristoph Hellwig 	struct r5l_io_unit *io, *next;
106817036461SChristoph Hellwig 	bool found = false;
106917036461SChristoph Hellwig 
107017036461SChristoph Hellwig 	assert_spin_locked(&log->io_list_lock);
107117036461SChristoph Hellwig 
107204732f74SChristoph Hellwig 	list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
107317036461SChristoph Hellwig 		/* don't change list order */
107417036461SChristoph Hellwig 		if (io->state < IO_UNIT_STRIPE_END)
107517036461SChristoph Hellwig 			break;
107617036461SChristoph Hellwig 
107717036461SChristoph Hellwig 		log->next_checkpoint = io->log_start;
107817036461SChristoph Hellwig 		log->next_cp_seq = io->seq;
107917036461SChristoph Hellwig 
108017036461SChristoph Hellwig 		list_del(&io->log_sibling);
10815036c390SChristoph Hellwig 		mempool_free(io, log->io_pool);
10825036c390SChristoph Hellwig 		r5l_run_no_mem_stripe(log);
108317036461SChristoph Hellwig 
108417036461SChristoph Hellwig 		found = true;
108517036461SChristoph Hellwig 	}
108617036461SChristoph Hellwig 
108717036461SChristoph Hellwig 	return found;
108817036461SChristoph Hellwig }
108917036461SChristoph Hellwig 
1090509ffec7SChristoph Hellwig static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
1091509ffec7SChristoph Hellwig {
1092509ffec7SChristoph Hellwig 	struct r5l_log *log = io->log;
1093a39f7afdSSong Liu 	struct r5conf *conf = log->rdev->mddev->private;
1094509ffec7SChristoph Hellwig 	unsigned long flags;
1095509ffec7SChristoph Hellwig 
1096509ffec7SChristoph Hellwig 	spin_lock_irqsave(&log->io_list_lock, flags);
1097509ffec7SChristoph Hellwig 	__r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
109817036461SChristoph Hellwig 
109904732f74SChristoph Hellwig 	if (!r5l_complete_finished_ios(log)) {
110085f2f9a4SShaohua Li 		spin_unlock_irqrestore(&log->io_list_lock, flags);
110185f2f9a4SShaohua Li 		return;
110285f2f9a4SShaohua Li 	}
1103509ffec7SChristoph Hellwig 
1104a39f7afdSSong Liu 	if (r5l_reclaimable_space(log) > log->max_free_space ||
1105a39f7afdSSong Liu 	    test_bit(R5C_LOG_TIGHT, &conf->cache_state))
1106509ffec7SChristoph Hellwig 		r5l_wake_reclaim(log, 0);
1107509ffec7SChristoph Hellwig 
1108509ffec7SChristoph Hellwig 	spin_unlock_irqrestore(&log->io_list_lock, flags);
1109509ffec7SChristoph Hellwig 	wake_up(&log->iounit_wait);
1110509ffec7SChristoph Hellwig }
1111509ffec7SChristoph Hellwig 
11120576b1c6SShaohua Li void r5l_stripe_write_finished(struct stripe_head *sh)
11130576b1c6SShaohua Li {
11140576b1c6SShaohua Li 	struct r5l_io_unit *io;
11150576b1c6SShaohua Li 
11160576b1c6SShaohua Li 	io = sh->log_io;
11170576b1c6SShaohua Li 	sh->log_io = NULL;
11180576b1c6SShaohua Li 
1119509ffec7SChristoph Hellwig 	if (io && atomic_dec_and_test(&io->pending_stripe))
1120509ffec7SChristoph Hellwig 		__r5l_stripe_write_finished(io);
11210576b1c6SShaohua Li }
11220576b1c6SShaohua Li 
1123a8c34f91SShaohua Li static void r5l_log_flush_endio(struct bio *bio)
1124a8c34f91SShaohua Li {
1125a8c34f91SShaohua Li 	struct r5l_log *log = container_of(bio, struct r5l_log,
1126a8c34f91SShaohua Li 		flush_bio);
1127a8c34f91SShaohua Li 	unsigned long flags;
1128a8c34f91SShaohua Li 	struct r5l_io_unit *io;
1129a8c34f91SShaohua Li 
11306e74a9cfSShaohua Li 	if (bio->bi_error)
11316e74a9cfSShaohua Li 		md_error(log->rdev->mddev, log->rdev);
11326e74a9cfSShaohua Li 
1133a8c34f91SShaohua Li 	spin_lock_irqsave(&log->io_list_lock, flags);
1134d8858f43SChristoph Hellwig 	list_for_each_entry(io, &log->flushing_ios, log_sibling)
1135d8858f43SChristoph Hellwig 		r5l_io_run_stripes(io);
113604732f74SChristoph Hellwig 	list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
1137a8c34f91SShaohua Li 	spin_unlock_irqrestore(&log->io_list_lock, flags);
1138a8c34f91SShaohua Li }
1139a8c34f91SShaohua Li 
11400576b1c6SShaohua Li /*
11410576b1c6SShaohua Li  * Starting dispatch IO to raid.
11420576b1c6SShaohua Li  * io_unit(meta) consists of a log. There is one situation we want to avoid. A
11430576b1c6SShaohua Li  * broken meta in the middle of a log causes recovery can't find meta at the
11440576b1c6SShaohua Li  * head of log. If operations require meta at the head persistent in log, we
11450576b1c6SShaohua Li  * must make sure meta before it persistent in log too. A case is:
11460576b1c6SShaohua Li  *
11470576b1c6SShaohua Li  * stripe data/parity is in log, we start write stripe to raid disks. stripe
11480576b1c6SShaohua Li  * data/parity must be persistent in log before we do the write to raid disks.
11490576b1c6SShaohua Li  *
11500576b1c6SShaohua Li  * The solution is we restrictly maintain io_unit list order. In this case, we
11510576b1c6SShaohua Li  * only write stripes of an io_unit to raid disks till the io_unit is the first
11520576b1c6SShaohua Li  * one whose data/parity is in log.
11530576b1c6SShaohua Li  */
11540576b1c6SShaohua Li void r5l_flush_stripe_to_raid(struct r5l_log *log)
11550576b1c6SShaohua Li {
1156a8c34f91SShaohua Li 	bool do_flush;
115756fef7c6SChristoph Hellwig 
115856fef7c6SChristoph Hellwig 	if (!log || !log->need_cache_flush)
11590576b1c6SShaohua Li 		return;
11600576b1c6SShaohua Li 
1161a8c34f91SShaohua Li 	spin_lock_irq(&log->io_list_lock);
1162a8c34f91SShaohua Li 	/* flush bio is running */
1163a8c34f91SShaohua Li 	if (!list_empty(&log->flushing_ios)) {
1164a8c34f91SShaohua Li 		spin_unlock_irq(&log->io_list_lock);
11650576b1c6SShaohua Li 		return;
11660576b1c6SShaohua Li 	}
1167a8c34f91SShaohua Li 	list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
1168a8c34f91SShaohua Li 	do_flush = !list_empty(&log->flushing_ios);
11690576b1c6SShaohua Li 	spin_unlock_irq(&log->io_list_lock);
1170a8c34f91SShaohua Li 
1171a8c34f91SShaohua Li 	if (!do_flush)
1172a8c34f91SShaohua Li 		return;
1173a8c34f91SShaohua Li 	bio_reset(&log->flush_bio);
1174a8c34f91SShaohua Li 	log->flush_bio.bi_bdev = log->rdev->bdev;
1175a8c34f91SShaohua Li 	log->flush_bio.bi_end_io = r5l_log_flush_endio;
1176796a5cf0SMike Christie 	bio_set_op_attrs(&log->flush_bio, REQ_OP_WRITE, WRITE_FLUSH);
11774e49ea4aSMike Christie 	submit_bio(&log->flush_bio);
11780576b1c6SShaohua Li }
11790576b1c6SShaohua Li 
11800576b1c6SShaohua Li static void r5l_write_super(struct r5l_log *log, sector_t cp);
11814b482044SShaohua Li static void r5l_write_super_and_discard_space(struct r5l_log *log,
11824b482044SShaohua Li 	sector_t end)
11834b482044SShaohua Li {
11844b482044SShaohua Li 	struct block_device *bdev = log->rdev->bdev;
11854b482044SShaohua Li 	struct mddev *mddev;
11864b482044SShaohua Li 
11874b482044SShaohua Li 	r5l_write_super(log, end);
11884b482044SShaohua Li 
11894b482044SShaohua Li 	if (!blk_queue_discard(bdev_get_queue(bdev)))
11904b482044SShaohua Li 		return;
11914b482044SShaohua Li 
11924b482044SShaohua Li 	mddev = log->rdev->mddev;
11934b482044SShaohua Li 	/*
11948e018c21SShaohua Li 	 * Discard could zero data, so before discard we must make sure
11958e018c21SShaohua Li 	 * superblock is updated to new log tail. Updating superblock (either
11968e018c21SShaohua Li 	 * directly call md_update_sb() or depend on md thread) must hold
11978e018c21SShaohua Li 	 * reconfig mutex. On the other hand, raid5_quiesce is called with
11988e018c21SShaohua Li 	 * reconfig_mutex hold. The first step of raid5_quiesce() is waitting
11998e018c21SShaohua Li 	 * for all IO finish, hence waitting for reclaim thread, while reclaim
12008e018c21SShaohua Li 	 * thread is calling this function and waitting for reconfig mutex. So
12018e018c21SShaohua Li 	 * there is a deadlock. We workaround this issue with a trylock.
12028e018c21SShaohua Li 	 * FIXME: we could miss discard if we can't take reconfig mutex
12034b482044SShaohua Li 	 */
120485ad1d13SGuoqing Jiang 	set_mask_bits(&mddev->flags, 0,
120585ad1d13SGuoqing Jiang 		BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
12068e018c21SShaohua Li 	if (!mddev_trylock(mddev))
12078e018c21SShaohua Li 		return;
12084b482044SShaohua Li 	md_update_sb(mddev, 1);
12098e018c21SShaohua Li 	mddev_unlock(mddev);
12104b482044SShaohua Li 
12116e74a9cfSShaohua Li 	/* discard IO error really doesn't matter, ignore it */
12124b482044SShaohua Li 	if (log->last_checkpoint < end) {
12134b482044SShaohua Li 		blkdev_issue_discard(bdev,
12144b482044SShaohua Li 				log->last_checkpoint + log->rdev->data_offset,
12154b482044SShaohua Li 				end - log->last_checkpoint, GFP_NOIO, 0);
12164b482044SShaohua Li 	} else {
12174b482044SShaohua Li 		blkdev_issue_discard(bdev,
12184b482044SShaohua Li 				log->last_checkpoint + log->rdev->data_offset,
12194b482044SShaohua Li 				log->device_size - log->last_checkpoint,
12204b482044SShaohua Li 				GFP_NOIO, 0);
12214b482044SShaohua Li 		blkdev_issue_discard(bdev, log->rdev->data_offset, end,
12224b482044SShaohua Li 				GFP_NOIO, 0);
12234b482044SShaohua Li 	}
12244b482044SShaohua Li }
12254b482044SShaohua Li 
1226a39f7afdSSong Liu /*
1227a39f7afdSSong Liu  * r5c_flush_stripe moves stripe from cached list to handle_list. When called,
1228a39f7afdSSong Liu  * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes.
1229a39f7afdSSong Liu  *
1230a39f7afdSSong Liu  * must hold conf->device_lock
1231a39f7afdSSong Liu  */
1232a39f7afdSSong Liu static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh)
1233a39f7afdSSong Liu {
1234a39f7afdSSong Liu 	BUG_ON(list_empty(&sh->lru));
1235a39f7afdSSong Liu 	BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
1236a39f7afdSSong Liu 	BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
1237a39f7afdSSong Liu 
1238a39f7afdSSong Liu 	/*
1239a39f7afdSSong Liu 	 * The stripe is not ON_RELEASE_LIST, so it is safe to call
1240a39f7afdSSong Liu 	 * raid5_release_stripe() while holding conf->device_lock
1241a39f7afdSSong Liu 	 */
1242a39f7afdSSong Liu 	BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
1243a39f7afdSSong Liu 	assert_spin_locked(&conf->device_lock);
1244a39f7afdSSong Liu 
1245a39f7afdSSong Liu 	list_del_init(&sh->lru);
1246a39f7afdSSong Liu 	atomic_inc(&sh->count);
1247a39f7afdSSong Liu 
1248a39f7afdSSong Liu 	set_bit(STRIPE_HANDLE, &sh->state);
1249a39f7afdSSong Liu 	atomic_inc(&conf->active_stripes);
1250a39f7afdSSong Liu 	r5c_make_stripe_write_out(sh);
1251a39f7afdSSong Liu 
1252a39f7afdSSong Liu 	if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1253a39f7afdSSong Liu 		atomic_inc(&conf->preread_active_stripes);
1254a39f7afdSSong Liu 	raid5_release_stripe(sh);
1255a39f7afdSSong Liu }
1256a39f7afdSSong Liu 
1257a39f7afdSSong Liu /*
1258a39f7afdSSong Liu  * if num == 0, flush all full stripes
1259a39f7afdSSong Liu  * if num > 0, flush all full stripes. If less than num full stripes are
1260a39f7afdSSong Liu  *             flushed, flush some partial stripes until totally num stripes are
1261a39f7afdSSong Liu  *             flushed or there is no more cached stripes.
1262a39f7afdSSong Liu  */
1263a39f7afdSSong Liu void r5c_flush_cache(struct r5conf *conf, int num)
1264a39f7afdSSong Liu {
1265a39f7afdSSong Liu 	int count;
1266a39f7afdSSong Liu 	struct stripe_head *sh, *next;
1267a39f7afdSSong Liu 
1268a39f7afdSSong Liu 	assert_spin_locked(&conf->device_lock);
1269a39f7afdSSong Liu 	if (!conf->log)
1270a39f7afdSSong Liu 		return;
1271a39f7afdSSong Liu 
1272a39f7afdSSong Liu 	count = 0;
1273a39f7afdSSong Liu 	list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) {
1274a39f7afdSSong Liu 		r5c_flush_stripe(conf, sh);
1275a39f7afdSSong Liu 		count++;
1276a39f7afdSSong Liu 	}
1277a39f7afdSSong Liu 
1278a39f7afdSSong Liu 	if (count >= num)
1279a39f7afdSSong Liu 		return;
1280a39f7afdSSong Liu 	list_for_each_entry_safe(sh, next,
1281a39f7afdSSong Liu 				 &conf->r5c_partial_stripe_list, lru) {
1282a39f7afdSSong Liu 		r5c_flush_stripe(conf, sh);
1283a39f7afdSSong Liu 		if (++count >= num)
1284a39f7afdSSong Liu 			break;
1285a39f7afdSSong Liu 	}
1286a39f7afdSSong Liu }
1287a39f7afdSSong Liu 
1288a39f7afdSSong Liu static void r5c_do_reclaim(struct r5conf *conf)
1289a39f7afdSSong Liu {
1290a39f7afdSSong Liu 	struct r5l_log *log = conf->log;
1291a39f7afdSSong Liu 	struct stripe_head *sh;
1292a39f7afdSSong Liu 	int count = 0;
1293a39f7afdSSong Liu 	unsigned long flags;
1294a39f7afdSSong Liu 	int total_cached;
1295a39f7afdSSong Liu 	int stripes_to_flush;
1296a39f7afdSSong Liu 
1297a39f7afdSSong Liu 	if (!r5c_is_writeback(log))
1298a39f7afdSSong Liu 		return;
1299a39f7afdSSong Liu 
1300a39f7afdSSong Liu 	total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
1301a39f7afdSSong Liu 		atomic_read(&conf->r5c_cached_full_stripes);
1302a39f7afdSSong Liu 
1303a39f7afdSSong Liu 	if (total_cached > conf->min_nr_stripes * 3 / 4 ||
1304a39f7afdSSong Liu 	    atomic_read(&conf->empty_inactive_list_nr) > 0)
1305a39f7afdSSong Liu 		/*
1306a39f7afdSSong Liu 		 * if stripe cache pressure high, flush all full stripes and
1307a39f7afdSSong Liu 		 * some partial stripes
1308a39f7afdSSong Liu 		 */
1309a39f7afdSSong Liu 		stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP;
1310a39f7afdSSong Liu 	else if (total_cached > conf->min_nr_stripes * 1 / 2 ||
1311a39f7afdSSong Liu 		 atomic_read(&conf->r5c_cached_full_stripes) >
1312a39f7afdSSong Liu 		 R5C_FULL_STRIPE_FLUSH_BATCH)
1313a39f7afdSSong Liu 		/*
1314a39f7afdSSong Liu 		 * if stripe cache pressure moderate, or if there is many full
1315a39f7afdSSong Liu 		 * stripes,flush all full stripes
1316a39f7afdSSong Liu 		 */
1317a39f7afdSSong Liu 		stripes_to_flush = 0;
1318a39f7afdSSong Liu 	else
1319a39f7afdSSong Liu 		/* no need to flush */
1320a39f7afdSSong Liu 		stripes_to_flush = -1;
1321a39f7afdSSong Liu 
1322a39f7afdSSong Liu 	if (stripes_to_flush >= 0) {
1323a39f7afdSSong Liu 		spin_lock_irqsave(&conf->device_lock, flags);
1324a39f7afdSSong Liu 		r5c_flush_cache(conf, stripes_to_flush);
1325a39f7afdSSong Liu 		spin_unlock_irqrestore(&conf->device_lock, flags);
1326a39f7afdSSong Liu 	}
1327a39f7afdSSong Liu 
1328a39f7afdSSong Liu 	/* if log space is tight, flush stripes on stripe_in_journal_list */
1329a39f7afdSSong Liu 	if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) {
1330a39f7afdSSong Liu 		spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
1331a39f7afdSSong Liu 		spin_lock(&conf->device_lock);
1332a39f7afdSSong Liu 		list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) {
1333a39f7afdSSong Liu 			/*
1334a39f7afdSSong Liu 			 * stripes on stripe_in_journal_list could be in any
1335a39f7afdSSong Liu 			 * state of the stripe_cache state machine. In this
1336a39f7afdSSong Liu 			 * case, we only want to flush stripe on
1337a39f7afdSSong Liu 			 * r5c_cached_full/partial_stripes. The following
1338a39f7afdSSong Liu 			 * condition makes sure the stripe is on one of the
1339a39f7afdSSong Liu 			 * two lists.
1340a39f7afdSSong Liu 			 */
1341a39f7afdSSong Liu 			if (!list_empty(&sh->lru) &&
1342a39f7afdSSong Liu 			    !test_bit(STRIPE_HANDLE, &sh->state) &&
1343a39f7afdSSong Liu 			    atomic_read(&sh->count) == 0) {
1344a39f7afdSSong Liu 				r5c_flush_stripe(conf, sh);
1345a39f7afdSSong Liu 			}
1346a39f7afdSSong Liu 			if (count++ >= R5C_RECLAIM_STRIPE_GROUP)
1347a39f7afdSSong Liu 				break;
1348a39f7afdSSong Liu 		}
1349a39f7afdSSong Liu 		spin_unlock(&conf->device_lock);
1350a39f7afdSSong Liu 		spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
1351a39f7afdSSong Liu 	}
1352a39f7afdSSong Liu 	md_wakeup_thread(conf->mddev->thread);
1353a39f7afdSSong Liu }
1354a39f7afdSSong Liu 
13550576b1c6SShaohua Li static void r5l_do_reclaim(struct r5l_log *log)
13560576b1c6SShaohua Li {
1357a39f7afdSSong Liu 	struct r5conf *conf = log->rdev->mddev->private;
13580576b1c6SShaohua Li 	sector_t reclaim_target = xchg(&log->reclaim_target, 0);
135917036461SChristoph Hellwig 	sector_t reclaimable;
136017036461SChristoph Hellwig 	sector_t next_checkpoint;
1361a39f7afdSSong Liu 	bool write_super;
13620576b1c6SShaohua Li 
13630576b1c6SShaohua Li 	spin_lock_irq(&log->io_list_lock);
1364a39f7afdSSong Liu 	write_super = r5l_reclaimable_space(log) > log->max_free_space ||
1365a39f7afdSSong Liu 		reclaim_target != 0 || !list_empty(&log->no_space_stripes);
13660576b1c6SShaohua Li 	/*
13670576b1c6SShaohua Li 	 * move proper io_unit to reclaim list. We should not change the order.
13680576b1c6SShaohua Li 	 * reclaimable/unreclaimable io_unit can be mixed in the list, we
13690576b1c6SShaohua Li 	 * shouldn't reuse space of an unreclaimable io_unit
13700576b1c6SShaohua Li 	 */
13710576b1c6SShaohua Li 	while (1) {
137217036461SChristoph Hellwig 		reclaimable = r5l_reclaimable_space(log);
137317036461SChristoph Hellwig 		if (reclaimable >= reclaim_target ||
13740576b1c6SShaohua Li 		    (list_empty(&log->running_ios) &&
13750576b1c6SShaohua Li 		     list_empty(&log->io_end_ios) &&
1376a8c34f91SShaohua Li 		     list_empty(&log->flushing_ios) &&
137704732f74SChristoph Hellwig 		     list_empty(&log->finished_ios)))
13780576b1c6SShaohua Li 			break;
13790576b1c6SShaohua Li 
138017036461SChristoph Hellwig 		md_wakeup_thread(log->rdev->mddev->thread);
138117036461SChristoph Hellwig 		wait_event_lock_irq(log->iounit_wait,
138217036461SChristoph Hellwig 				    r5l_reclaimable_space(log) > reclaimable,
138317036461SChristoph Hellwig 				    log->io_list_lock);
13840576b1c6SShaohua Li 	}
138517036461SChristoph Hellwig 
1386a39f7afdSSong Liu 	next_checkpoint = r5c_calculate_new_cp(conf);
13870576b1c6SShaohua Li 	spin_unlock_irq(&log->io_list_lock);
13880576b1c6SShaohua Li 
138917036461SChristoph Hellwig 	BUG_ON(reclaimable < 0);
1390a39f7afdSSong Liu 
1391a39f7afdSSong Liu 	if (reclaimable == 0 || !write_super)
13920576b1c6SShaohua Li 		return;
13930576b1c6SShaohua Li 
13940576b1c6SShaohua Li 	/*
13950576b1c6SShaohua Li 	 * write_super will flush cache of each raid disk. We must write super
13960576b1c6SShaohua Li 	 * here, because the log area might be reused soon and we don't want to
13970576b1c6SShaohua Li 	 * confuse recovery
13980576b1c6SShaohua Li 	 */
13994b482044SShaohua Li 	r5l_write_super_and_discard_space(log, next_checkpoint);
14000576b1c6SShaohua Li 
14010576b1c6SShaohua Li 	mutex_lock(&log->io_mutex);
140217036461SChristoph Hellwig 	log->last_checkpoint = next_checkpoint;
1403a39f7afdSSong Liu 	r5c_update_log_state(log);
14040576b1c6SShaohua Li 	mutex_unlock(&log->io_mutex);
14050576b1c6SShaohua Li 
140617036461SChristoph Hellwig 	r5l_run_no_space_stripes(log);
14070576b1c6SShaohua Li }
14080576b1c6SShaohua Li 
14090576b1c6SShaohua Li static void r5l_reclaim_thread(struct md_thread *thread)
14100576b1c6SShaohua Li {
14110576b1c6SShaohua Li 	struct mddev *mddev = thread->mddev;
14120576b1c6SShaohua Li 	struct r5conf *conf = mddev->private;
14130576b1c6SShaohua Li 	struct r5l_log *log = conf->log;
14140576b1c6SShaohua Li 
14150576b1c6SShaohua Li 	if (!log)
14160576b1c6SShaohua Li 		return;
1417a39f7afdSSong Liu 	r5c_do_reclaim(conf);
14180576b1c6SShaohua Li 	r5l_do_reclaim(log);
14190576b1c6SShaohua Li }
14200576b1c6SShaohua Li 
1421a39f7afdSSong Liu void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
1422f6bed0efSShaohua Li {
14230576b1c6SShaohua Li 	unsigned long target;
14240576b1c6SShaohua Li 	unsigned long new = (unsigned long)space; /* overflow in theory */
14250576b1c6SShaohua Li 
1426a39f7afdSSong Liu 	if (!log)
1427a39f7afdSSong Liu 		return;
14280576b1c6SShaohua Li 	do {
14290576b1c6SShaohua Li 		target = log->reclaim_target;
14300576b1c6SShaohua Li 		if (new < target)
14310576b1c6SShaohua Li 			return;
14320576b1c6SShaohua Li 	} while (cmpxchg(&log->reclaim_target, target, new) != target);
14330576b1c6SShaohua Li 	md_wakeup_thread(log->reclaim_thread);
1434f6bed0efSShaohua Li }
1435f6bed0efSShaohua Li 
1436e6c033f7SShaohua Li void r5l_quiesce(struct r5l_log *log, int state)
1437e6c033f7SShaohua Li {
14384b482044SShaohua Li 	struct mddev *mddev;
1439e6c033f7SShaohua Li 	if (!log || state == 2)
1440e6c033f7SShaohua Li 		return;
1441ce1ccd07SShaohua Li 	if (state == 0)
1442ce1ccd07SShaohua Li 		kthread_unpark(log->reclaim_thread->tsk);
1443ce1ccd07SShaohua Li 	else if (state == 1) {
14444b482044SShaohua Li 		/* make sure r5l_write_super_and_discard_space exits */
14454b482044SShaohua Li 		mddev = log->rdev->mddev;
14464b482044SShaohua Li 		wake_up(&mddev->sb_wait);
1447ce1ccd07SShaohua Li 		kthread_park(log->reclaim_thread->tsk);
1448a39f7afdSSong Liu 		r5l_wake_reclaim(log, MaxSector);
1449e6c033f7SShaohua Li 		r5l_do_reclaim(log);
1450e6c033f7SShaohua Li 	}
1451e6c033f7SShaohua Li }
1452e6c033f7SShaohua Li 
14536e74a9cfSShaohua Li bool r5l_log_disk_error(struct r5conf *conf)
14546e74a9cfSShaohua Li {
1455f6b6ec5cSShaohua Li 	struct r5l_log *log;
1456f6b6ec5cSShaohua Li 	bool ret;
14577dde2ad3SShaohua Li 	/* don't allow write if journal disk is missing */
1458f6b6ec5cSShaohua Li 	rcu_read_lock();
1459f6b6ec5cSShaohua Li 	log = rcu_dereference(conf->log);
1460f6b6ec5cSShaohua Li 
1461f6b6ec5cSShaohua Li 	if (!log)
1462f6b6ec5cSShaohua Li 		ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
1463f6b6ec5cSShaohua Li 	else
1464f6b6ec5cSShaohua Li 		ret = test_bit(Faulty, &log->rdev->flags);
1465f6b6ec5cSShaohua Li 	rcu_read_unlock();
1466f6b6ec5cSShaohua Li 	return ret;
14676e74a9cfSShaohua Li }
14686e74a9cfSShaohua Li 
1469355810d1SShaohua Li struct r5l_recovery_ctx {
1470355810d1SShaohua Li 	struct page *meta_page;		/* current meta */
1471355810d1SShaohua Li 	sector_t meta_total_blocks;	/* total size of current meta and data */
1472355810d1SShaohua Li 	sector_t pos;			/* recovery position */
1473355810d1SShaohua Li 	u64 seq;			/* recovery position seq */
1474b4c625c6SSong Liu 	int data_parity_stripes;	/* number of data_parity stripes */
1475b4c625c6SSong Liu 	int data_only_stripes;		/* number of data_only stripes */
1476b4c625c6SSong Liu 	struct list_head cached_list;
1477355810d1SShaohua Li };
1478355810d1SShaohua Li 
14799ed988f5SSong Liu static int r5l_recovery_read_meta_block(struct r5l_log *log,
1480355810d1SShaohua Li 					struct r5l_recovery_ctx *ctx)
1481355810d1SShaohua Li {
1482355810d1SShaohua Li 	struct page *page = ctx->meta_page;
1483355810d1SShaohua Li 	struct r5l_meta_block *mb;
1484355810d1SShaohua Li 	u32 crc, stored_crc;
1485355810d1SShaohua Li 
1486796a5cf0SMike Christie 	if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, REQ_OP_READ, 0,
1487796a5cf0SMike Christie 			  false))
1488355810d1SShaohua Li 		return -EIO;
1489355810d1SShaohua Li 
1490355810d1SShaohua Li 	mb = page_address(page);
1491355810d1SShaohua Li 	stored_crc = le32_to_cpu(mb->checksum);
1492355810d1SShaohua Li 	mb->checksum = 0;
1493355810d1SShaohua Li 
1494355810d1SShaohua Li 	if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
1495355810d1SShaohua Li 	    le64_to_cpu(mb->seq) != ctx->seq ||
1496355810d1SShaohua Li 	    mb->version != R5LOG_VERSION ||
1497355810d1SShaohua Li 	    le64_to_cpu(mb->position) != ctx->pos)
1498355810d1SShaohua Li 		return -EINVAL;
1499355810d1SShaohua Li 
15005cb2fbd6SShaohua Li 	crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
1501355810d1SShaohua Li 	if (stored_crc != crc)
1502355810d1SShaohua Li 		return -EINVAL;
1503355810d1SShaohua Li 
1504355810d1SShaohua Li 	if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
1505355810d1SShaohua Li 		return -EINVAL;
1506355810d1SShaohua Li 
1507355810d1SShaohua Li 	ctx->meta_total_blocks = BLOCK_SECTORS;
1508355810d1SShaohua Li 
1509355810d1SShaohua Li 	return 0;
1510355810d1SShaohua Li }
1511355810d1SShaohua Li 
15129ed988f5SSong Liu static void
15139ed988f5SSong Liu r5l_recovery_create_empty_meta_block(struct r5l_log *log,
15149ed988f5SSong Liu 				     struct page *page,
15159ed988f5SSong Liu 				     sector_t pos, u64 seq)
1516355810d1SShaohua Li {
1517355810d1SShaohua Li 	struct r5l_meta_block *mb;
1518355810d1SShaohua Li 	u32 crc;
1519355810d1SShaohua Li 
1520355810d1SShaohua Li 	mb = page_address(page);
15219ed988f5SSong Liu 	clear_page(mb);
1522355810d1SShaohua Li 	mb->magic = cpu_to_le32(R5LOG_MAGIC);
1523355810d1SShaohua Li 	mb->version = R5LOG_VERSION;
1524355810d1SShaohua Li 	mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
1525355810d1SShaohua Li 	mb->seq = cpu_to_le64(seq);
1526355810d1SShaohua Li 	mb->position = cpu_to_le64(pos);
15275cb2fbd6SShaohua Li 	crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
1528355810d1SShaohua Li 	mb->checksum = cpu_to_le32(crc);
15299ed988f5SSong Liu }
1530355810d1SShaohua Li 
15319ed988f5SSong Liu static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
15329ed988f5SSong Liu 					  u64 seq)
15339ed988f5SSong Liu {
15349ed988f5SSong Liu 	struct page *page;
15359ed988f5SSong Liu 
15369ed988f5SSong Liu 	page = alloc_page(GFP_KERNEL);
15379ed988f5SSong Liu 	if (!page)
15389ed988f5SSong Liu 		return -ENOMEM;
15399ed988f5SSong Liu 	r5l_recovery_create_empty_meta_block(log, page, pos, seq);
1540796a5cf0SMike Christie 	if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE,
1541796a5cf0SMike Christie 			  WRITE_FUA, false)) {
1542355810d1SShaohua Li 		__free_page(page);
1543355810d1SShaohua Li 		return -EIO;
1544355810d1SShaohua Li 	}
1545355810d1SShaohua Li 	__free_page(page);
1546355810d1SShaohua Li 	return 0;
1547355810d1SShaohua Li }
1548355810d1SShaohua Li 
1549b4c625c6SSong Liu /*
1550b4c625c6SSong Liu  * r5l_recovery_load_data and r5l_recovery_load_parity uses flag R5_Wantwrite
1551b4c625c6SSong Liu  * to mark valid (potentially not flushed) data in the journal.
1552b4c625c6SSong Liu  *
1553b4c625c6SSong Liu  * We already verified checksum in r5l_recovery_verify_data_checksum_for_mb,
1554b4c625c6SSong Liu  * so there should not be any mismatch here.
1555b4c625c6SSong Liu  */
1556b4c625c6SSong Liu static void r5l_recovery_load_data(struct r5l_log *log,
1557b4c625c6SSong Liu 				   struct stripe_head *sh,
1558b4c625c6SSong Liu 				   struct r5l_recovery_ctx *ctx,
1559b4c625c6SSong Liu 				   struct r5l_payload_data_parity *payload,
1560b4c625c6SSong Liu 				   sector_t log_offset)
1561b4c625c6SSong Liu {
1562b4c625c6SSong Liu 	struct mddev *mddev = log->rdev->mddev;
1563b4c625c6SSong Liu 	struct r5conf *conf = mddev->private;
1564b4c625c6SSong Liu 	int dd_idx;
1565b4c625c6SSong Liu 
1566b4c625c6SSong Liu 	raid5_compute_sector(conf,
1567b4c625c6SSong Liu 			     le64_to_cpu(payload->location), 0,
1568b4c625c6SSong Liu 			     &dd_idx, sh);
1569b4c625c6SSong Liu 	sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1570b4c625c6SSong Liu 		     sh->dev[dd_idx].page, REQ_OP_READ, 0, false);
1571b4c625c6SSong Liu 	sh->dev[dd_idx].log_checksum =
1572b4c625c6SSong Liu 		le32_to_cpu(payload->checksum[0]);
1573b4c625c6SSong Liu 	ctx->meta_total_blocks += BLOCK_SECTORS;
1574b4c625c6SSong Liu 
1575b4c625c6SSong Liu 	set_bit(R5_Wantwrite, &sh->dev[dd_idx].flags);
1576b4c625c6SSong Liu 	set_bit(STRIPE_R5C_CACHING, &sh->state);
1577b4c625c6SSong Liu }
1578b4c625c6SSong Liu 
1579b4c625c6SSong Liu static void r5l_recovery_load_parity(struct r5l_log *log,
1580b4c625c6SSong Liu 				     struct stripe_head *sh,
1581b4c625c6SSong Liu 				     struct r5l_recovery_ctx *ctx,
1582b4c625c6SSong Liu 				     struct r5l_payload_data_parity *payload,
1583b4c625c6SSong Liu 				     sector_t log_offset)
1584b4c625c6SSong Liu {
1585b4c625c6SSong Liu 	struct mddev *mddev = log->rdev->mddev;
1586b4c625c6SSong Liu 	struct r5conf *conf = mddev->private;
1587b4c625c6SSong Liu 
1588b4c625c6SSong Liu 	ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
1589b4c625c6SSong Liu 	sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1590b4c625c6SSong Liu 		     sh->dev[sh->pd_idx].page, REQ_OP_READ, 0, false);
1591b4c625c6SSong Liu 	sh->dev[sh->pd_idx].log_checksum =
1592b4c625c6SSong Liu 		le32_to_cpu(payload->checksum[0]);
1593b4c625c6SSong Liu 	set_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags);
1594b4c625c6SSong Liu 
1595b4c625c6SSong Liu 	if (sh->qd_idx >= 0) {
1596b4c625c6SSong Liu 		sync_page_io(log->rdev,
1597b4c625c6SSong Liu 			     r5l_ring_add(log, log_offset, BLOCK_SECTORS),
1598b4c625c6SSong Liu 			     PAGE_SIZE, sh->dev[sh->qd_idx].page,
1599b4c625c6SSong Liu 			     REQ_OP_READ, 0, false);
1600b4c625c6SSong Liu 		sh->dev[sh->qd_idx].log_checksum =
1601b4c625c6SSong Liu 			le32_to_cpu(payload->checksum[1]);
1602b4c625c6SSong Liu 		set_bit(R5_Wantwrite, &sh->dev[sh->qd_idx].flags);
1603b4c625c6SSong Liu 	}
1604b4c625c6SSong Liu 	clear_bit(STRIPE_R5C_CACHING, &sh->state);
1605b4c625c6SSong Liu }
1606b4c625c6SSong Liu 
1607b4c625c6SSong Liu static void r5l_recovery_reset_stripe(struct stripe_head *sh)
1608b4c625c6SSong Liu {
1609b4c625c6SSong Liu 	int i;
1610b4c625c6SSong Liu 
1611b4c625c6SSong Liu 	sh->state = 0;
1612b4c625c6SSong Liu 	sh->log_start = MaxSector;
1613b4c625c6SSong Liu 	for (i = sh->disks; i--; )
1614b4c625c6SSong Liu 		sh->dev[i].flags = 0;
1615b4c625c6SSong Liu }
1616b4c625c6SSong Liu 
1617b4c625c6SSong Liu static void
1618b4c625c6SSong Liu r5l_recovery_replay_one_stripe(struct r5conf *conf,
1619b4c625c6SSong Liu 			       struct stripe_head *sh,
1620b4c625c6SSong Liu 			       struct r5l_recovery_ctx *ctx)
1621b4c625c6SSong Liu {
1622b4c625c6SSong Liu 	struct md_rdev *rdev, *rrdev;
1623b4c625c6SSong Liu 	int disk_index;
1624b4c625c6SSong Liu 	int data_count = 0;
1625b4c625c6SSong Liu 
1626b4c625c6SSong Liu 	for (disk_index = 0; disk_index < sh->disks; disk_index++) {
1627b4c625c6SSong Liu 		if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
1628b4c625c6SSong Liu 			continue;
1629b4c625c6SSong Liu 		if (disk_index == sh->qd_idx || disk_index == sh->pd_idx)
1630b4c625c6SSong Liu 			continue;
1631b4c625c6SSong Liu 		data_count++;
1632b4c625c6SSong Liu 	}
1633b4c625c6SSong Liu 
1634b4c625c6SSong Liu 	/*
1635b4c625c6SSong Liu 	 * stripes that only have parity must have been flushed
1636b4c625c6SSong Liu 	 * before the crash that we are now recovering from, so
1637b4c625c6SSong Liu 	 * there is nothing more to recovery.
1638b4c625c6SSong Liu 	 */
1639b4c625c6SSong Liu 	if (data_count == 0)
1640b4c625c6SSong Liu 		goto out;
1641b4c625c6SSong Liu 
1642b4c625c6SSong Liu 	for (disk_index = 0; disk_index < sh->disks; disk_index++) {
1643b4c625c6SSong Liu 		if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
1644b4c625c6SSong Liu 			continue;
1645b4c625c6SSong Liu 
1646b4c625c6SSong Liu 		/* in case device is broken */
1647b4c625c6SSong Liu 		rcu_read_lock();
1648b4c625c6SSong Liu 		rdev = rcu_dereference(conf->disks[disk_index].rdev);
1649b4c625c6SSong Liu 		if (rdev) {
1650b4c625c6SSong Liu 			atomic_inc(&rdev->nr_pending);
1651b4c625c6SSong Liu 			rcu_read_unlock();
1652b4c625c6SSong Liu 			sync_page_io(rdev, sh->sector, PAGE_SIZE,
1653b4c625c6SSong Liu 				     sh->dev[disk_index].page, REQ_OP_WRITE, 0,
1654b4c625c6SSong Liu 				     false);
1655b4c625c6SSong Liu 			rdev_dec_pending(rdev, rdev->mddev);
1656b4c625c6SSong Liu 			rcu_read_lock();
1657b4c625c6SSong Liu 		}
1658b4c625c6SSong Liu 		rrdev = rcu_dereference(conf->disks[disk_index].replacement);
1659b4c625c6SSong Liu 		if (rrdev) {
1660b4c625c6SSong Liu 			atomic_inc(&rrdev->nr_pending);
1661b4c625c6SSong Liu 			rcu_read_unlock();
1662b4c625c6SSong Liu 			sync_page_io(rrdev, sh->sector, PAGE_SIZE,
1663b4c625c6SSong Liu 				     sh->dev[disk_index].page, REQ_OP_WRITE, 0,
1664b4c625c6SSong Liu 				     false);
1665b4c625c6SSong Liu 			rdev_dec_pending(rrdev, rrdev->mddev);
1666b4c625c6SSong Liu 			rcu_read_lock();
1667b4c625c6SSong Liu 		}
1668b4c625c6SSong Liu 		rcu_read_unlock();
1669b4c625c6SSong Liu 	}
1670b4c625c6SSong Liu 	ctx->data_parity_stripes++;
1671b4c625c6SSong Liu out:
1672b4c625c6SSong Liu 	r5l_recovery_reset_stripe(sh);
1673b4c625c6SSong Liu }
1674b4c625c6SSong Liu 
1675b4c625c6SSong Liu static struct stripe_head *
1676b4c625c6SSong Liu r5c_recovery_alloc_stripe(struct r5conf *conf,
1677b4c625c6SSong Liu 			  struct list_head *recovery_list,
1678b4c625c6SSong Liu 			  sector_t stripe_sect,
1679b4c625c6SSong Liu 			  sector_t log_start)
1680b4c625c6SSong Liu {
1681b4c625c6SSong Liu 	struct stripe_head *sh;
1682b4c625c6SSong Liu 
1683b4c625c6SSong Liu 	sh = raid5_get_active_stripe(conf, stripe_sect, 0, 1, 0);
1684b4c625c6SSong Liu 	if (!sh)
1685b4c625c6SSong Liu 		return NULL;  /* no more stripe available */
1686b4c625c6SSong Liu 
1687b4c625c6SSong Liu 	r5l_recovery_reset_stripe(sh);
1688b4c625c6SSong Liu 	sh->log_start = log_start;
1689b4c625c6SSong Liu 
1690b4c625c6SSong Liu 	return sh;
1691b4c625c6SSong Liu }
1692b4c625c6SSong Liu 
1693b4c625c6SSong Liu static struct stripe_head *
1694b4c625c6SSong Liu r5c_recovery_lookup_stripe(struct list_head *list, sector_t sect)
1695b4c625c6SSong Liu {
1696b4c625c6SSong Liu 	struct stripe_head *sh;
1697b4c625c6SSong Liu 
1698b4c625c6SSong Liu 	list_for_each_entry(sh, list, lru)
1699b4c625c6SSong Liu 		if (sh->sector == sect)
1700b4c625c6SSong Liu 			return sh;
1701b4c625c6SSong Liu 	return NULL;
1702b4c625c6SSong Liu }
1703b4c625c6SSong Liu 
1704b4c625c6SSong Liu static void
1705b4c625c6SSong Liu r5c_recovery_drop_stripes(struct list_head *cached_stripe_list,
1706b4c625c6SSong Liu 			  struct r5l_recovery_ctx *ctx)
1707b4c625c6SSong Liu {
1708b4c625c6SSong Liu 	struct stripe_head *sh, *next;
1709b4c625c6SSong Liu 
1710b4c625c6SSong Liu 	list_for_each_entry_safe(sh, next, cached_stripe_list, lru) {
1711b4c625c6SSong Liu 		r5l_recovery_reset_stripe(sh);
1712b4c625c6SSong Liu 		list_del_init(&sh->lru);
1713b4c625c6SSong Liu 		raid5_release_stripe(sh);
1714b4c625c6SSong Liu 	}
1715b4c625c6SSong Liu }
1716b4c625c6SSong Liu 
1717b4c625c6SSong Liu static void
1718b4c625c6SSong Liu r5c_recovery_replay_stripes(struct list_head *cached_stripe_list,
1719b4c625c6SSong Liu 			    struct r5l_recovery_ctx *ctx)
1720b4c625c6SSong Liu {
1721b4c625c6SSong Liu 	struct stripe_head *sh, *next;
1722b4c625c6SSong Liu 
1723b4c625c6SSong Liu 	list_for_each_entry_safe(sh, next, cached_stripe_list, lru)
1724b4c625c6SSong Liu 		if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
1725b4c625c6SSong Liu 			r5l_recovery_replay_one_stripe(sh->raid_conf, sh, ctx);
1726b4c625c6SSong Liu 			list_del_init(&sh->lru);
1727b4c625c6SSong Liu 			raid5_release_stripe(sh);
1728b4c625c6SSong Liu 		}
1729b4c625c6SSong Liu }
1730b4c625c6SSong Liu 
1731b4c625c6SSong Liu /* if matches return 0; otherwise return -EINVAL */
1732b4c625c6SSong Liu static int
1733b4c625c6SSong Liu r5l_recovery_verify_data_checksum(struct r5l_log *log, struct page *page,
1734b4c625c6SSong Liu 				  sector_t log_offset, __le32 log_checksum)
1735b4c625c6SSong Liu {
1736b4c625c6SSong Liu 	void *addr;
1737b4c625c6SSong Liu 	u32 checksum;
1738b4c625c6SSong Liu 
1739b4c625c6SSong Liu 	sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1740b4c625c6SSong Liu 		     page, REQ_OP_READ, 0, false);
1741b4c625c6SSong Liu 	addr = kmap_atomic(page);
1742b4c625c6SSong Liu 	checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
1743b4c625c6SSong Liu 	kunmap_atomic(addr);
1744b4c625c6SSong Liu 	return (le32_to_cpu(log_checksum) == checksum) ? 0 : -EINVAL;
1745b4c625c6SSong Liu }
1746b4c625c6SSong Liu 
1747b4c625c6SSong Liu /*
1748b4c625c6SSong Liu  * before loading data to stripe cache, we need verify checksum for all data,
1749b4c625c6SSong Liu  * if there is mismatch for any data page, we drop all data in the mata block
1750b4c625c6SSong Liu  */
1751b4c625c6SSong Liu static int
1752b4c625c6SSong Liu r5l_recovery_verify_data_checksum_for_mb(struct r5l_log *log,
1753b4c625c6SSong Liu 					 struct r5l_recovery_ctx *ctx)
1754b4c625c6SSong Liu {
1755b4c625c6SSong Liu 	struct mddev *mddev = log->rdev->mddev;
1756b4c625c6SSong Liu 	struct r5conf *conf = mddev->private;
1757b4c625c6SSong Liu 	struct r5l_meta_block *mb = page_address(ctx->meta_page);
1758b4c625c6SSong Liu 	sector_t mb_offset = sizeof(struct r5l_meta_block);
1759b4c625c6SSong Liu 	sector_t log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
1760b4c625c6SSong Liu 	struct page *page;
1761b4c625c6SSong Liu 	struct r5l_payload_data_parity *payload;
1762b4c625c6SSong Liu 
1763b4c625c6SSong Liu 	page = alloc_page(GFP_KERNEL);
1764b4c625c6SSong Liu 	if (!page)
1765b4c625c6SSong Liu 		return -ENOMEM;
1766b4c625c6SSong Liu 
1767b4c625c6SSong Liu 	while (mb_offset < le32_to_cpu(mb->meta_size)) {
1768b4c625c6SSong Liu 		payload = (void *)mb + mb_offset;
1769b4c625c6SSong Liu 
1770b4c625c6SSong Liu 		if (payload->header.type == R5LOG_PAYLOAD_DATA) {
1771b4c625c6SSong Liu 			if (r5l_recovery_verify_data_checksum(
1772b4c625c6SSong Liu 				    log, page, log_offset,
1773b4c625c6SSong Liu 				    payload->checksum[0]) < 0)
1774b4c625c6SSong Liu 				goto mismatch;
1775b4c625c6SSong Liu 		} else if (payload->header.type == R5LOG_PAYLOAD_PARITY) {
1776b4c625c6SSong Liu 			if (r5l_recovery_verify_data_checksum(
1777b4c625c6SSong Liu 				    log, page, log_offset,
1778b4c625c6SSong Liu 				    payload->checksum[0]) < 0)
1779b4c625c6SSong Liu 				goto mismatch;
1780b4c625c6SSong Liu 			if (conf->max_degraded == 2 && /* q for RAID 6 */
1781b4c625c6SSong Liu 			    r5l_recovery_verify_data_checksum(
1782b4c625c6SSong Liu 				    log, page,
1783b4c625c6SSong Liu 				    r5l_ring_add(log, log_offset,
1784b4c625c6SSong Liu 						 BLOCK_SECTORS),
1785b4c625c6SSong Liu 				    payload->checksum[1]) < 0)
1786b4c625c6SSong Liu 				goto mismatch;
1787b4c625c6SSong Liu 		} else /* not R5LOG_PAYLOAD_DATA or R5LOG_PAYLOAD_PARITY */
1788b4c625c6SSong Liu 			goto mismatch;
1789b4c625c6SSong Liu 
1790b4c625c6SSong Liu 		log_offset = r5l_ring_add(log, log_offset,
1791b4c625c6SSong Liu 					  le32_to_cpu(payload->size));
1792b4c625c6SSong Liu 
1793b4c625c6SSong Liu 		mb_offset += sizeof(struct r5l_payload_data_parity) +
1794b4c625c6SSong Liu 			sizeof(__le32) *
1795b4c625c6SSong Liu 			(le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
1796b4c625c6SSong Liu 	}
1797b4c625c6SSong Liu 
1798b4c625c6SSong Liu 	put_page(page);
1799b4c625c6SSong Liu 	return 0;
1800b4c625c6SSong Liu 
1801b4c625c6SSong Liu mismatch:
1802b4c625c6SSong Liu 	put_page(page);
1803b4c625c6SSong Liu 	return -EINVAL;
1804b4c625c6SSong Liu }
1805b4c625c6SSong Liu 
1806b4c625c6SSong Liu /*
1807b4c625c6SSong Liu  * Analyze all data/parity pages in one meta block
1808b4c625c6SSong Liu  * Returns:
1809b4c625c6SSong Liu  * 0 for success
1810b4c625c6SSong Liu  * -EINVAL for unknown playload type
1811b4c625c6SSong Liu  * -EAGAIN for checksum mismatch of data page
1812b4c625c6SSong Liu  * -ENOMEM for run out of memory (alloc_page failed or run out of stripes)
1813b4c625c6SSong Liu  */
1814b4c625c6SSong Liu static int
1815b4c625c6SSong Liu r5c_recovery_analyze_meta_block(struct r5l_log *log,
1816b4c625c6SSong Liu 				struct r5l_recovery_ctx *ctx,
1817b4c625c6SSong Liu 				struct list_head *cached_stripe_list)
1818b4c625c6SSong Liu {
1819b4c625c6SSong Liu 	struct mddev *mddev = log->rdev->mddev;
1820b4c625c6SSong Liu 	struct r5conf *conf = mddev->private;
1821b4c625c6SSong Liu 	struct r5l_meta_block *mb;
1822b4c625c6SSong Liu 	struct r5l_payload_data_parity *payload;
1823b4c625c6SSong Liu 	int mb_offset;
1824b4c625c6SSong Liu 	sector_t log_offset;
1825b4c625c6SSong Liu 	sector_t stripe_sect;
1826b4c625c6SSong Liu 	struct stripe_head *sh;
1827b4c625c6SSong Liu 	int ret;
1828b4c625c6SSong Liu 
1829b4c625c6SSong Liu 	/*
1830b4c625c6SSong Liu 	 * for mismatch in data blocks, we will drop all data in this mb, but
1831b4c625c6SSong Liu 	 * we will still read next mb for other data with FLUSH flag, as
1832b4c625c6SSong Liu 	 * io_unit could finish out of order.
1833b4c625c6SSong Liu 	 */
1834b4c625c6SSong Liu 	ret = r5l_recovery_verify_data_checksum_for_mb(log, ctx);
1835b4c625c6SSong Liu 	if (ret == -EINVAL)
1836b4c625c6SSong Liu 		return -EAGAIN;
1837b4c625c6SSong Liu 	else if (ret)
1838b4c625c6SSong Liu 		return ret;   /* -ENOMEM duo to alloc_page() failed */
1839b4c625c6SSong Liu 
1840b4c625c6SSong Liu 	mb = page_address(ctx->meta_page);
1841b4c625c6SSong Liu 	mb_offset = sizeof(struct r5l_meta_block);
1842b4c625c6SSong Liu 	log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
1843b4c625c6SSong Liu 
1844b4c625c6SSong Liu 	while (mb_offset < le32_to_cpu(mb->meta_size)) {
1845b4c625c6SSong Liu 		int dd;
1846b4c625c6SSong Liu 
1847b4c625c6SSong Liu 		payload = (void *)mb + mb_offset;
1848b4c625c6SSong Liu 		stripe_sect = (payload->header.type == R5LOG_PAYLOAD_DATA) ?
1849b4c625c6SSong Liu 			raid5_compute_sector(
1850b4c625c6SSong Liu 				conf, le64_to_cpu(payload->location), 0, &dd,
1851b4c625c6SSong Liu 				NULL)
1852b4c625c6SSong Liu 			: le64_to_cpu(payload->location);
1853b4c625c6SSong Liu 
1854b4c625c6SSong Liu 		sh = r5c_recovery_lookup_stripe(cached_stripe_list,
1855b4c625c6SSong Liu 						stripe_sect);
1856b4c625c6SSong Liu 
1857b4c625c6SSong Liu 		if (!sh) {
1858b4c625c6SSong Liu 			sh = r5c_recovery_alloc_stripe(conf, cached_stripe_list,
1859b4c625c6SSong Liu 						       stripe_sect, ctx->pos);
1860b4c625c6SSong Liu 			/*
1861b4c625c6SSong Liu 			 * cannot get stripe from raid5_get_active_stripe
1862b4c625c6SSong Liu 			 * try replay some stripes
1863b4c625c6SSong Liu 			 */
1864b4c625c6SSong Liu 			if (!sh) {
1865b4c625c6SSong Liu 				r5c_recovery_replay_stripes(
1866b4c625c6SSong Liu 					cached_stripe_list, ctx);
1867b4c625c6SSong Liu 				sh = r5c_recovery_alloc_stripe(
1868b4c625c6SSong Liu 					conf, cached_stripe_list,
1869b4c625c6SSong Liu 					stripe_sect, ctx->pos);
1870b4c625c6SSong Liu 			}
1871b4c625c6SSong Liu 			if (!sh) {
1872b4c625c6SSong Liu 				pr_debug("md/raid:%s: Increasing stripe cache size to %d to recovery data on journal.\n",
1873b4c625c6SSong Liu 					mdname(mddev),
1874b4c625c6SSong Liu 					conf->min_nr_stripes * 2);
1875b4c625c6SSong Liu 				raid5_set_cache_size(mddev,
1876b4c625c6SSong Liu 						     conf->min_nr_stripes * 2);
1877b4c625c6SSong Liu 				sh = r5c_recovery_alloc_stripe(
1878b4c625c6SSong Liu 					conf, cached_stripe_list, stripe_sect,
1879b4c625c6SSong Liu 					ctx->pos);
1880b4c625c6SSong Liu 			}
1881b4c625c6SSong Liu 			if (!sh) {
1882b4c625c6SSong Liu 				pr_err("md/raid:%s: Cannot get enough stripes due to memory pressure. Recovery failed.\n",
1883b4c625c6SSong Liu 				       mdname(mddev));
1884b4c625c6SSong Liu 				return -ENOMEM;
1885b4c625c6SSong Liu 			}
1886b4c625c6SSong Liu 			list_add_tail(&sh->lru, cached_stripe_list);
1887b4c625c6SSong Liu 		}
1888b4c625c6SSong Liu 
1889b4c625c6SSong Liu 		if (payload->header.type == R5LOG_PAYLOAD_DATA) {
1890b4c625c6SSong Liu 			if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
1891b4c625c6SSong Liu 				r5l_recovery_replay_one_stripe(conf, sh, ctx);
1892b4c625c6SSong Liu 				r5l_recovery_reset_stripe(sh);
1893b4c625c6SSong Liu 				sh->log_start = ctx->pos;
1894b4c625c6SSong Liu 				list_move_tail(&sh->lru, cached_stripe_list);
1895b4c625c6SSong Liu 			}
1896b4c625c6SSong Liu 			r5l_recovery_load_data(log, sh, ctx, payload,
1897b4c625c6SSong Liu 					       log_offset);
1898b4c625c6SSong Liu 		} else if (payload->header.type == R5LOG_PAYLOAD_PARITY)
1899b4c625c6SSong Liu 			r5l_recovery_load_parity(log, sh, ctx, payload,
1900b4c625c6SSong Liu 						 log_offset);
1901b4c625c6SSong Liu 		else
1902b4c625c6SSong Liu 			return -EINVAL;
1903b4c625c6SSong Liu 
1904b4c625c6SSong Liu 		log_offset = r5l_ring_add(log, log_offset,
1905b4c625c6SSong Liu 					  le32_to_cpu(payload->size));
1906b4c625c6SSong Liu 
1907b4c625c6SSong Liu 		mb_offset += sizeof(struct r5l_payload_data_parity) +
1908b4c625c6SSong Liu 			sizeof(__le32) *
1909b4c625c6SSong Liu 			(le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
1910b4c625c6SSong Liu 	}
1911b4c625c6SSong Liu 
1912b4c625c6SSong Liu 	return 0;
1913b4c625c6SSong Liu }
1914b4c625c6SSong Liu 
1915b4c625c6SSong Liu /*
1916b4c625c6SSong Liu  * Load the stripe into cache. The stripe will be written out later by
1917b4c625c6SSong Liu  * the stripe cache state machine.
1918b4c625c6SSong Liu  */
1919b4c625c6SSong Liu static void r5c_recovery_load_one_stripe(struct r5l_log *log,
1920b4c625c6SSong Liu 					 struct stripe_head *sh)
1921b4c625c6SSong Liu {
1922b4c625c6SSong Liu 	struct r5conf *conf = sh->raid_conf;
1923b4c625c6SSong Liu 	struct r5dev *dev;
1924b4c625c6SSong Liu 	int i;
1925b4c625c6SSong Liu 
1926b4c625c6SSong Liu 	for (i = sh->disks; i--; ) {
1927b4c625c6SSong Liu 		dev = sh->dev + i;
1928b4c625c6SSong Liu 		if (test_and_clear_bit(R5_Wantwrite, &dev->flags)) {
1929b4c625c6SSong Liu 			set_bit(R5_InJournal, &dev->flags);
1930b4c625c6SSong Liu 			set_bit(R5_UPTODATE, &dev->flags);
1931b4c625c6SSong Liu 		}
1932b4c625c6SSong Liu 	}
1933b4c625c6SSong Liu 	set_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state);
1934b4c625c6SSong Liu 	atomic_inc(&conf->r5c_cached_partial_stripes);
1935b4c625c6SSong Liu 	list_add_tail(&sh->r5c, &log->stripe_in_journal_list);
1936b4c625c6SSong Liu }
1937b4c625c6SSong Liu 
1938b4c625c6SSong Liu /*
1939b4c625c6SSong Liu  * Scan through the log for all to-be-flushed data
1940b4c625c6SSong Liu  *
1941b4c625c6SSong Liu  * For stripes with data and parity, namely Data-Parity stripe
1942b4c625c6SSong Liu  * (STRIPE_R5C_CACHING == 0), we simply replay all the writes.
1943b4c625c6SSong Liu  *
1944b4c625c6SSong Liu  * For stripes with only data, namely Data-Only stripe
1945b4c625c6SSong Liu  * (STRIPE_R5C_CACHING == 1), we load them to stripe cache state machine.
1946b4c625c6SSong Liu  *
1947b4c625c6SSong Liu  * For a stripe, if we see data after parity, we should discard all previous
1948b4c625c6SSong Liu  * data and parity for this stripe, as these data are already flushed to
1949b4c625c6SSong Liu  * the array.
1950b4c625c6SSong Liu  *
1951b4c625c6SSong Liu  * At the end of the scan, we return the new journal_tail, which points to
1952b4c625c6SSong Liu  * first data-only stripe on the journal device, or next invalid meta block.
1953b4c625c6SSong Liu  */
1954b4c625c6SSong Liu static int r5c_recovery_flush_log(struct r5l_log *log,
1955b4c625c6SSong Liu 				  struct r5l_recovery_ctx *ctx)
1956b4c625c6SSong Liu {
1957b4c625c6SSong Liu 	struct stripe_head *sh, *next;
1958b4c625c6SSong Liu 	int ret = 0;
1959b4c625c6SSong Liu 
1960b4c625c6SSong Liu 	/* scan through the log */
1961b4c625c6SSong Liu 	while (1) {
1962b4c625c6SSong Liu 		if (r5l_recovery_read_meta_block(log, ctx))
1963b4c625c6SSong Liu 			break;
1964b4c625c6SSong Liu 
1965b4c625c6SSong Liu 		ret = r5c_recovery_analyze_meta_block(log, ctx,
1966b4c625c6SSong Liu 						      &ctx->cached_list);
1967b4c625c6SSong Liu 		/*
1968b4c625c6SSong Liu 		 * -EAGAIN means mismatch in data block, in this case, we still
1969b4c625c6SSong Liu 		 * try scan the next metablock
1970b4c625c6SSong Liu 		 */
1971b4c625c6SSong Liu 		if (ret && ret != -EAGAIN)
1972b4c625c6SSong Liu 			break;   /* ret == -EINVAL or -ENOMEM */
1973b4c625c6SSong Liu 		ctx->seq++;
1974b4c625c6SSong Liu 		ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
1975b4c625c6SSong Liu 	}
1976b4c625c6SSong Liu 
1977b4c625c6SSong Liu 	if (ret == -ENOMEM) {
1978b4c625c6SSong Liu 		r5c_recovery_drop_stripes(&ctx->cached_list, ctx);
1979b4c625c6SSong Liu 		return ret;
1980b4c625c6SSong Liu 	}
1981b4c625c6SSong Liu 
1982b4c625c6SSong Liu 	/* replay data-parity stripes */
1983b4c625c6SSong Liu 	r5c_recovery_replay_stripes(&ctx->cached_list, ctx);
1984b4c625c6SSong Liu 
1985b4c625c6SSong Liu 	/* load data-only stripes to stripe cache */
1986b4c625c6SSong Liu 	list_for_each_entry_safe(sh, next, &ctx->cached_list, lru) {
1987b4c625c6SSong Liu 		WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
1988b4c625c6SSong Liu 		r5c_recovery_load_one_stripe(log, sh);
1989b4c625c6SSong Liu 		list_del_init(&sh->lru);
1990b4c625c6SSong Liu 		raid5_release_stripe(sh);
1991b4c625c6SSong Liu 		ctx->data_only_stripes++;
1992b4c625c6SSong Liu 	}
1993b4c625c6SSong Liu 
1994b4c625c6SSong Liu 	return 0;
1995b4c625c6SSong Liu }
1996b4c625c6SSong Liu 
1997b4c625c6SSong Liu /*
1998b4c625c6SSong Liu  * we did a recovery. Now ctx.pos points to an invalid meta block. New
1999b4c625c6SSong Liu  * log will start here. but we can't let superblock point to last valid
2000b4c625c6SSong Liu  * meta block. The log might looks like:
2001b4c625c6SSong Liu  * | meta 1| meta 2| meta 3|
2002b4c625c6SSong Liu  * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
2003b4c625c6SSong Liu  * superblock points to meta 1, we write a new valid meta 2n.  if crash
2004b4c625c6SSong Liu  * happens again, new recovery will start from meta 1. Since meta 2n is
2005b4c625c6SSong Liu  * valid now, recovery will think meta 3 is valid, which is wrong.
2006b4c625c6SSong Liu  * The solution is we create a new meta in meta2 with its seq == meta
2007b4c625c6SSong Liu  * 1's seq + 10 and let superblock points to meta2. The same recovery will
2008b4c625c6SSong Liu  * not think meta 3 is a valid meta, because its seq doesn't match
2009b4c625c6SSong Liu  */
2010b4c625c6SSong Liu 
2011b4c625c6SSong Liu /*
2012b4c625c6SSong Liu  * Before recovery, the log looks like the following
2013b4c625c6SSong Liu  *
2014b4c625c6SSong Liu  *   ---------------------------------------------
2015b4c625c6SSong Liu  *   |           valid log        | invalid log  |
2016b4c625c6SSong Liu  *   ---------------------------------------------
2017b4c625c6SSong Liu  *   ^
2018b4c625c6SSong Liu  *   |- log->last_checkpoint
2019b4c625c6SSong Liu  *   |- log->last_cp_seq
2020b4c625c6SSong Liu  *
2021b4c625c6SSong Liu  * Now we scan through the log until we see invalid entry
2022b4c625c6SSong Liu  *
2023b4c625c6SSong Liu  *   ---------------------------------------------
2024b4c625c6SSong Liu  *   |           valid log        | invalid log  |
2025b4c625c6SSong Liu  *   ---------------------------------------------
2026b4c625c6SSong Liu  *   ^                            ^
2027b4c625c6SSong Liu  *   |- log->last_checkpoint      |- ctx->pos
2028b4c625c6SSong Liu  *   |- log->last_cp_seq          |- ctx->seq
2029b4c625c6SSong Liu  *
2030b4c625c6SSong Liu  * From this point, we need to increase seq number by 10 to avoid
2031b4c625c6SSong Liu  * confusing next recovery.
2032b4c625c6SSong Liu  *
2033b4c625c6SSong Liu  *   ---------------------------------------------
2034b4c625c6SSong Liu  *   |           valid log        | invalid log  |
2035b4c625c6SSong Liu  *   ---------------------------------------------
2036b4c625c6SSong Liu  *   ^                              ^
2037b4c625c6SSong Liu  *   |- log->last_checkpoint        |- ctx->pos+1
2038b4c625c6SSong Liu  *   |- log->last_cp_seq            |- ctx->seq+11
2039b4c625c6SSong Liu  *
2040b4c625c6SSong Liu  * However, it is not safe to start the state machine yet, because data only
2041b4c625c6SSong Liu  * parities are not yet secured in RAID. To save these data only parities, we
2042b4c625c6SSong Liu  * rewrite them from seq+11.
2043b4c625c6SSong Liu  *
2044b4c625c6SSong Liu  *   -----------------------------------------------------------------
2045b4c625c6SSong Liu  *   |           valid log        | data only stripes | invalid log  |
2046b4c625c6SSong Liu  *   -----------------------------------------------------------------
2047b4c625c6SSong Liu  *   ^                                                ^
2048b4c625c6SSong Liu  *   |- log->last_checkpoint                          |- ctx->pos+n
2049b4c625c6SSong Liu  *   |- log->last_cp_seq                              |- ctx->seq+10+n
2050b4c625c6SSong Liu  *
2051b4c625c6SSong Liu  * If failure happens again during this process, the recovery can safe start
2052b4c625c6SSong Liu  * again from log->last_checkpoint.
2053b4c625c6SSong Liu  *
2054b4c625c6SSong Liu  * Once data only stripes are rewritten to journal, we move log_tail
2055b4c625c6SSong Liu  *
2056b4c625c6SSong Liu  *   -----------------------------------------------------------------
2057b4c625c6SSong Liu  *   |     old log        |    data only stripes    | invalid log  |
2058b4c625c6SSong Liu  *   -----------------------------------------------------------------
2059b4c625c6SSong Liu  *                        ^                         ^
2060b4c625c6SSong Liu  *                        |- log->last_checkpoint   |- ctx->pos+n
2061b4c625c6SSong Liu  *                        |- log->last_cp_seq       |- ctx->seq+10+n
2062b4c625c6SSong Liu  *
2063b4c625c6SSong Liu  * Then we can safely start the state machine. If failure happens from this
2064b4c625c6SSong Liu  * point on, the recovery will start from new log->last_checkpoint.
2065b4c625c6SSong Liu  */
2066b4c625c6SSong Liu static int
2067b4c625c6SSong Liu r5c_recovery_rewrite_data_only_stripes(struct r5l_log *log,
2068b4c625c6SSong Liu 				       struct r5l_recovery_ctx *ctx)
2069b4c625c6SSong Liu {
2070b4c625c6SSong Liu 	struct stripe_head *sh;
2071b4c625c6SSong Liu 	struct mddev *mddev = log->rdev->mddev;
2072b4c625c6SSong Liu 	struct page *page;
2073b4c625c6SSong Liu 
2074b4c625c6SSong Liu 	page = alloc_page(GFP_KERNEL);
2075b4c625c6SSong Liu 	if (!page) {
2076b4c625c6SSong Liu 		pr_err("md/raid:%s: cannot allocate memory to rewrite data only stripes\n",
2077b4c625c6SSong Liu 		       mdname(mddev));
2078b4c625c6SSong Liu 		return -ENOMEM;
2079b4c625c6SSong Liu 	}
2080b4c625c6SSong Liu 
2081b4c625c6SSong Liu 	ctx->seq += 10;
2082b4c625c6SSong Liu 	list_for_each_entry(sh, &ctx->cached_list, lru) {
2083b4c625c6SSong Liu 		struct r5l_meta_block *mb;
2084b4c625c6SSong Liu 		int i;
2085b4c625c6SSong Liu 		int offset;
2086b4c625c6SSong Liu 		sector_t write_pos;
2087b4c625c6SSong Liu 
2088b4c625c6SSong Liu 		WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
2089b4c625c6SSong Liu 		r5l_recovery_create_empty_meta_block(log, page,
2090b4c625c6SSong Liu 						     ctx->pos, ctx->seq);
2091b4c625c6SSong Liu 		mb = page_address(page);
2092b4c625c6SSong Liu 		offset = le32_to_cpu(mb->meta_size);
2093b4c625c6SSong Liu 		write_pos = ctx->pos + BLOCK_SECTORS;
2094b4c625c6SSong Liu 
2095b4c625c6SSong Liu 		for (i = sh->disks; i--; ) {
2096b4c625c6SSong Liu 			struct r5dev *dev = &sh->dev[i];
2097b4c625c6SSong Liu 			struct r5l_payload_data_parity *payload;
2098b4c625c6SSong Liu 			void *addr;
2099b4c625c6SSong Liu 
2100b4c625c6SSong Liu 			if (test_bit(R5_InJournal, &dev->flags)) {
2101b4c625c6SSong Liu 				payload = (void *)mb + offset;
2102b4c625c6SSong Liu 				payload->header.type = cpu_to_le16(
2103b4c625c6SSong Liu 					R5LOG_PAYLOAD_DATA);
2104b4c625c6SSong Liu 				payload->size = BLOCK_SECTORS;
2105b4c625c6SSong Liu 				payload->location = cpu_to_le64(
2106b4c625c6SSong Liu 					raid5_compute_blocknr(sh, i, 0));
2107b4c625c6SSong Liu 				addr = kmap_atomic(dev->page);
2108b4c625c6SSong Liu 				payload->checksum[0] = cpu_to_le32(
2109b4c625c6SSong Liu 					crc32c_le(log->uuid_checksum, addr,
2110b4c625c6SSong Liu 						  PAGE_SIZE));
2111b4c625c6SSong Liu 				kunmap_atomic(addr);
2112b4c625c6SSong Liu 				sync_page_io(log->rdev, write_pos, PAGE_SIZE,
2113b4c625c6SSong Liu 					     dev->page, REQ_OP_WRITE, 0, false);
2114b4c625c6SSong Liu 				write_pos = r5l_ring_add(log, write_pos,
2115b4c625c6SSong Liu 							 BLOCK_SECTORS);
2116b4c625c6SSong Liu 				offset += sizeof(__le32) +
2117b4c625c6SSong Liu 					sizeof(struct r5l_payload_data_parity);
2118b4c625c6SSong Liu 
2119b4c625c6SSong Liu 			}
2120b4c625c6SSong Liu 		}
2121b4c625c6SSong Liu 		mb->meta_size = cpu_to_le32(offset);
2122b4c625c6SSong Liu 		mb->checksum = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
2123b4c625c6SSong Liu 		sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page,
2124b4c625c6SSong Liu 			     REQ_OP_WRITE, WRITE_FUA, false);
2125b4c625c6SSong Liu 		sh->log_start = ctx->pos;
2126b4c625c6SSong Liu 		ctx->pos = write_pos;
2127b4c625c6SSong Liu 		ctx->seq += 1;
2128b4c625c6SSong Liu 	}
2129b4c625c6SSong Liu 	__free_page(page);
2130b4c625c6SSong Liu 	return 0;
2131b4c625c6SSong Liu }
2132b4c625c6SSong Liu 
2133f6bed0efSShaohua Li static int r5l_recovery_log(struct r5l_log *log)
2134f6bed0efSShaohua Li {
21355aabf7c4SSong Liu 	struct mddev *mddev = log->rdev->mddev;
2136355810d1SShaohua Li 	struct r5l_recovery_ctx ctx;
21375aabf7c4SSong Liu 	int ret;
2138355810d1SShaohua Li 
2139355810d1SShaohua Li 	ctx.pos = log->last_checkpoint;
2140355810d1SShaohua Li 	ctx.seq = log->last_cp_seq;
2141355810d1SShaohua Li 	ctx.meta_page = alloc_page(GFP_KERNEL);
2142b4c625c6SSong Liu 	ctx.data_only_stripes = 0;
2143b4c625c6SSong Liu 	ctx.data_parity_stripes = 0;
2144b4c625c6SSong Liu 	INIT_LIST_HEAD(&ctx.cached_list);
2145b4c625c6SSong Liu 
2146355810d1SShaohua Li 	if (!ctx.meta_page)
2147355810d1SShaohua Li 		return -ENOMEM;
2148355810d1SShaohua Li 
21495aabf7c4SSong Liu 	ret = r5c_recovery_flush_log(log, &ctx);
2150355810d1SShaohua Li 	__free_page(ctx.meta_page);
2151355810d1SShaohua Li 
2152355810d1SShaohua Li 	if (ret)
2153355810d1SShaohua Li 		return ret;
21545aabf7c4SSong Liu 
21555aabf7c4SSong Liu 	if ((ctx.data_only_stripes == 0) && (ctx.data_parity_stripes == 0))
21565aabf7c4SSong Liu 		pr_debug("md/raid:%s: starting from clean shutdown\n",
21575aabf7c4SSong Liu 			 mdname(mddev));
21585aabf7c4SSong Liu 	else {
21595aabf7c4SSong Liu 		pr_debug("md/raid:%s: recoverying %d data-only stripes and %d data-parity stripes\n",
21605aabf7c4SSong Liu 			 mdname(mddev), ctx.data_only_stripes,
21615aabf7c4SSong Liu 			 ctx.data_parity_stripes);
21625aabf7c4SSong Liu 
21635aabf7c4SSong Liu 		if (ctx.data_only_stripes > 0)
21645aabf7c4SSong Liu 			if (r5c_recovery_rewrite_data_only_stripes(log, &ctx)) {
21655aabf7c4SSong Liu 				pr_err("md/raid:%s: failed to rewrite stripes to journal\n",
21665aabf7c4SSong Liu 				       mdname(mddev));
21675aabf7c4SSong Liu 				return -EIO;
21685aabf7c4SSong Liu 			}
21695aabf7c4SSong Liu 	}
21705aabf7c4SSong Liu 
2171355810d1SShaohua Li 	log->log_start = ctx.pos;
21725aabf7c4SSong Liu 	log->next_checkpoint = ctx.pos;
2173355810d1SShaohua Li 	log->seq = ctx.seq;
21745aabf7c4SSong Liu 	r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq);
21755aabf7c4SSong Liu 	r5l_write_super(log, ctx.pos);
2176f6bed0efSShaohua Li 	return 0;
2177f6bed0efSShaohua Li }
2178f6bed0efSShaohua Li 
2179f6bed0efSShaohua Li static void r5l_write_super(struct r5l_log *log, sector_t cp)
2180f6bed0efSShaohua Li {
2181f6bed0efSShaohua Li 	struct mddev *mddev = log->rdev->mddev;
2182f6bed0efSShaohua Li 
2183f6bed0efSShaohua Li 	log->rdev->journal_tail = cp;
2184f6bed0efSShaohua Li 	set_bit(MD_CHANGE_DEVS, &mddev->flags);
2185f6bed0efSShaohua Li }
2186f6bed0efSShaohua Li 
21872c7da14bSSong Liu static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page)
21882c7da14bSSong Liu {
21892c7da14bSSong Liu 	struct r5conf *conf = mddev->private;
21902c7da14bSSong Liu 	int ret;
21912c7da14bSSong Liu 
21922c7da14bSSong Liu 	if (!conf->log)
21932c7da14bSSong Liu 		return 0;
21942c7da14bSSong Liu 
21952c7da14bSSong Liu 	switch (conf->log->r5c_journal_mode) {
21962c7da14bSSong Liu 	case R5C_JOURNAL_MODE_WRITE_THROUGH:
21972c7da14bSSong Liu 		ret = snprintf(
21982c7da14bSSong Liu 			page, PAGE_SIZE, "[%s] %s\n",
21992c7da14bSSong Liu 			r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
22002c7da14bSSong Liu 			r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
22012c7da14bSSong Liu 		break;
22022c7da14bSSong Liu 	case R5C_JOURNAL_MODE_WRITE_BACK:
22032c7da14bSSong Liu 		ret = snprintf(
22042c7da14bSSong Liu 			page, PAGE_SIZE, "%s [%s]\n",
22052c7da14bSSong Liu 			r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
22062c7da14bSSong Liu 			r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
22072c7da14bSSong Liu 		break;
22082c7da14bSSong Liu 	default:
22092c7da14bSSong Liu 		ret = 0;
22102c7da14bSSong Liu 	}
22112c7da14bSSong Liu 	return ret;
22122c7da14bSSong Liu }
22132c7da14bSSong Liu 
22142c7da14bSSong Liu static ssize_t r5c_journal_mode_store(struct mddev *mddev,
22152c7da14bSSong Liu 				      const char *page, size_t length)
22162c7da14bSSong Liu {
22172c7da14bSSong Liu 	struct r5conf *conf = mddev->private;
22182c7da14bSSong Liu 	struct r5l_log *log = conf->log;
22192c7da14bSSong Liu 	int val = -1, i;
22202c7da14bSSong Liu 	int len = length;
22212c7da14bSSong Liu 
22222c7da14bSSong Liu 	if (!log)
22232c7da14bSSong Liu 		return -ENODEV;
22242c7da14bSSong Liu 
22252c7da14bSSong Liu 	if (len && page[len - 1] == '\n')
22262c7da14bSSong Liu 		len -= 1;
22272c7da14bSSong Liu 	for (i = 0; i < ARRAY_SIZE(r5c_journal_mode_str); i++)
22282c7da14bSSong Liu 		if (strlen(r5c_journal_mode_str[i]) == len &&
22292c7da14bSSong Liu 		    strncmp(page, r5c_journal_mode_str[i], len) == 0) {
22302c7da14bSSong Liu 			val = i;
22312c7da14bSSong Liu 			break;
22322c7da14bSSong Liu 		}
22332c7da14bSSong Liu 	if (val < R5C_JOURNAL_MODE_WRITE_THROUGH ||
22342c7da14bSSong Liu 	    val > R5C_JOURNAL_MODE_WRITE_BACK)
22352c7da14bSSong Liu 		return -EINVAL;
22362c7da14bSSong Liu 
22372c7da14bSSong Liu 	mddev_suspend(mddev);
22382c7da14bSSong Liu 	conf->log->r5c_journal_mode = val;
22392c7da14bSSong Liu 	mddev_resume(mddev);
22402c7da14bSSong Liu 
22412c7da14bSSong Liu 	pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n",
22422c7da14bSSong Liu 		 mdname(mddev), val, r5c_journal_mode_str[val]);
22432c7da14bSSong Liu 	return length;
22442c7da14bSSong Liu }
22452c7da14bSSong Liu 
22462c7da14bSSong Liu struct md_sysfs_entry
22472c7da14bSSong Liu r5c_journal_mode = __ATTR(journal_mode, 0644,
22482c7da14bSSong Liu 			  r5c_journal_mode_show, r5c_journal_mode_store);
22492c7da14bSSong Liu 
22502ded3703SSong Liu /*
22512ded3703SSong Liu  * Try handle write operation in caching phase. This function should only
22522ded3703SSong Liu  * be called in write-back mode.
22532ded3703SSong Liu  *
22542ded3703SSong Liu  * If all outstanding writes can be handled in caching phase, returns 0
22552ded3703SSong Liu  * If writes requires write-out phase, call r5c_make_stripe_write_out()
22562ded3703SSong Liu  * and returns -EAGAIN
22572ded3703SSong Liu  */
22582ded3703SSong Liu int r5c_try_caching_write(struct r5conf *conf,
22592ded3703SSong Liu 			  struct stripe_head *sh,
22602ded3703SSong Liu 			  struct stripe_head_state *s,
22612ded3703SSong Liu 			  int disks)
22622ded3703SSong Liu {
22632ded3703SSong Liu 	struct r5l_log *log = conf->log;
22641e6d690bSSong Liu 	int i;
22651e6d690bSSong Liu 	struct r5dev *dev;
22661e6d690bSSong Liu 	int to_cache = 0;
22672ded3703SSong Liu 
22682ded3703SSong Liu 	BUG_ON(!r5c_is_writeback(log));
22692ded3703SSong Liu 
22701e6d690bSSong Liu 	if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
22711e6d690bSSong Liu 		/*
22721e6d690bSSong Liu 		 * There are two different scenarios here:
22731e6d690bSSong Liu 		 *  1. The stripe has some data cached, and it is sent to
22741e6d690bSSong Liu 		 *     write-out phase for reclaim
22751e6d690bSSong Liu 		 *  2. The stripe is clean, and this is the first write
22761e6d690bSSong Liu 		 *
22771e6d690bSSong Liu 		 * For 1, return -EAGAIN, so we continue with
22781e6d690bSSong Liu 		 * handle_stripe_dirtying().
22791e6d690bSSong Liu 		 *
22801e6d690bSSong Liu 		 * For 2, set STRIPE_R5C_CACHING and continue with caching
22811e6d690bSSong Liu 		 * write.
22821e6d690bSSong Liu 		 */
22831e6d690bSSong Liu 
22841e6d690bSSong Liu 		/* case 1: anything injournal or anything in written */
22851e6d690bSSong Liu 		if (s->injournal > 0 || s->written > 0)
22861e6d690bSSong Liu 			return -EAGAIN;
22871e6d690bSSong Liu 		/* case 2 */
22881e6d690bSSong Liu 		set_bit(STRIPE_R5C_CACHING, &sh->state);
22891e6d690bSSong Liu 	}
22901e6d690bSSong Liu 
22911e6d690bSSong Liu 	for (i = disks; i--; ) {
22921e6d690bSSong Liu 		dev = &sh->dev[i];
22931e6d690bSSong Liu 		/* if non-overwrite, use writing-out phase */
22941e6d690bSSong Liu 		if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) &&
22951e6d690bSSong Liu 		    !test_bit(R5_InJournal, &dev->flags)) {
22962ded3703SSong Liu 			r5c_make_stripe_write_out(sh);
22972ded3703SSong Liu 			return -EAGAIN;
22982ded3703SSong Liu 		}
22991e6d690bSSong Liu 	}
23001e6d690bSSong Liu 
23011e6d690bSSong Liu 	for (i = disks; i--; ) {
23021e6d690bSSong Liu 		dev = &sh->dev[i];
23031e6d690bSSong Liu 		if (dev->towrite) {
23041e6d690bSSong Liu 			set_bit(R5_Wantwrite, &dev->flags);
23051e6d690bSSong Liu 			set_bit(R5_Wantdrain, &dev->flags);
23061e6d690bSSong Liu 			set_bit(R5_LOCKED, &dev->flags);
23071e6d690bSSong Liu 			to_cache++;
23081e6d690bSSong Liu 		}
23091e6d690bSSong Liu 	}
23101e6d690bSSong Liu 
23111e6d690bSSong Liu 	if (to_cache) {
23121e6d690bSSong Liu 		set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
23131e6d690bSSong Liu 		/*
23141e6d690bSSong Liu 		 * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data()
23151e6d690bSSong Liu 		 * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in
23161e6d690bSSong Liu 		 * r5c_handle_data_cached()
23171e6d690bSSong Liu 		 */
23181e6d690bSSong Liu 		set_bit(STRIPE_LOG_TRAPPED, &sh->state);
23191e6d690bSSong Liu 	}
23201e6d690bSSong Liu 
23211e6d690bSSong Liu 	return 0;
23221e6d690bSSong Liu }
23231e6d690bSSong Liu 
23241e6d690bSSong Liu /*
23251e6d690bSSong Liu  * free extra pages (orig_page) we allocated for prexor
23261e6d690bSSong Liu  */
23271e6d690bSSong Liu void r5c_release_extra_page(struct stripe_head *sh)
23281e6d690bSSong Liu {
2329*d7bd398eSSong Liu 	struct r5conf *conf = sh->raid_conf;
23301e6d690bSSong Liu 	int i;
2331*d7bd398eSSong Liu 	bool using_disk_info_extra_page;
2332*d7bd398eSSong Liu 
2333*d7bd398eSSong Liu 	using_disk_info_extra_page =
2334*d7bd398eSSong Liu 		sh->dev[0].orig_page == conf->disks[0].extra_page;
23351e6d690bSSong Liu 
23361e6d690bSSong Liu 	for (i = sh->disks; i--; )
23371e6d690bSSong Liu 		if (sh->dev[i].page != sh->dev[i].orig_page) {
23381e6d690bSSong Liu 			struct page *p = sh->dev[i].orig_page;
23391e6d690bSSong Liu 
23401e6d690bSSong Liu 			sh->dev[i].orig_page = sh->dev[i].page;
2341*d7bd398eSSong Liu 			if (!using_disk_info_extra_page)
23421e6d690bSSong Liu 				put_page(p);
23431e6d690bSSong Liu 		}
2344*d7bd398eSSong Liu 
2345*d7bd398eSSong Liu 	if (using_disk_info_extra_page) {
2346*d7bd398eSSong Liu 		clear_bit(R5C_EXTRA_PAGE_IN_USE, &conf->cache_state);
2347*d7bd398eSSong Liu 		md_wakeup_thread(conf->mddev->thread);
2348*d7bd398eSSong Liu 	}
2349*d7bd398eSSong Liu }
2350*d7bd398eSSong Liu 
2351*d7bd398eSSong Liu void r5c_use_extra_page(struct stripe_head *sh)
2352*d7bd398eSSong Liu {
2353*d7bd398eSSong Liu 	struct r5conf *conf = sh->raid_conf;
2354*d7bd398eSSong Liu 	int i;
2355*d7bd398eSSong Liu 	struct r5dev *dev;
2356*d7bd398eSSong Liu 
2357*d7bd398eSSong Liu 	for (i = sh->disks; i--; ) {
2358*d7bd398eSSong Liu 		dev = &sh->dev[i];
2359*d7bd398eSSong Liu 		if (dev->orig_page != dev->page)
2360*d7bd398eSSong Liu 			put_page(dev->orig_page);
2361*d7bd398eSSong Liu 		dev->orig_page = conf->disks[i].extra_page;
2362*d7bd398eSSong Liu 	}
23631e6d690bSSong Liu }
23642ded3703SSong Liu 
23652ded3703SSong Liu /*
23662ded3703SSong Liu  * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the
23672ded3703SSong Liu  * stripe is committed to RAID disks.
23682ded3703SSong Liu  */
23692ded3703SSong Liu void r5c_finish_stripe_write_out(struct r5conf *conf,
23702ded3703SSong Liu 				 struct stripe_head *sh,
23712ded3703SSong Liu 				 struct stripe_head_state *s)
23722ded3703SSong Liu {
23731e6d690bSSong Liu 	int i;
23741e6d690bSSong Liu 	int do_wakeup = 0;
23751e6d690bSSong Liu 
23762ded3703SSong Liu 	if (!conf->log ||
23772ded3703SSong Liu 	    !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags))
23782ded3703SSong Liu 		return;
23792ded3703SSong Liu 
23802ded3703SSong Liu 	WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
23812ded3703SSong Liu 	clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
23822ded3703SSong Liu 
23832ded3703SSong Liu 	if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
23842ded3703SSong Liu 		return;
23851e6d690bSSong Liu 
23861e6d690bSSong Liu 	for (i = sh->disks; i--; ) {
23871e6d690bSSong Liu 		clear_bit(R5_InJournal, &sh->dev[i].flags);
23881e6d690bSSong Liu 		if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
23891e6d690bSSong Liu 			do_wakeup = 1;
23901e6d690bSSong Liu 	}
23911e6d690bSSong Liu 
23921e6d690bSSong Liu 	/*
23931e6d690bSSong Liu 	 * analyse_stripe() runs before r5c_finish_stripe_write_out(),
23941e6d690bSSong Liu 	 * We updated R5_InJournal, so we also update s->injournal.
23951e6d690bSSong Liu 	 */
23961e6d690bSSong Liu 	s->injournal = 0;
23971e6d690bSSong Liu 
23981e6d690bSSong Liu 	if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
23991e6d690bSSong Liu 		if (atomic_dec_and_test(&conf->pending_full_writes))
24001e6d690bSSong Liu 			md_wakeup_thread(conf->mddev->thread);
24011e6d690bSSong Liu 
24021e6d690bSSong Liu 	if (do_wakeup)
24031e6d690bSSong Liu 		wake_up(&conf->wait_for_overlap);
2404a39f7afdSSong Liu 
2405a39f7afdSSong Liu 	if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
2406a39f7afdSSong Liu 		return;
2407a39f7afdSSong Liu 
2408a39f7afdSSong Liu 	spin_lock_irq(&conf->log->stripe_in_journal_lock);
2409a39f7afdSSong Liu 	list_del_init(&sh->r5c);
2410a39f7afdSSong Liu 	spin_unlock_irq(&conf->log->stripe_in_journal_lock);
2411a39f7afdSSong Liu 	sh->log_start = MaxSector;
2412a39f7afdSSong Liu 	atomic_dec(&conf->log->stripe_in_journal_count);
24131e6d690bSSong Liu }
24141e6d690bSSong Liu 
24151e6d690bSSong Liu int
24161e6d690bSSong Liu r5c_cache_data(struct r5l_log *log, struct stripe_head *sh,
24171e6d690bSSong Liu 	       struct stripe_head_state *s)
24181e6d690bSSong Liu {
2419a39f7afdSSong Liu 	struct r5conf *conf = sh->raid_conf;
24201e6d690bSSong Liu 	int pages = 0;
24211e6d690bSSong Liu 	int reserve;
24221e6d690bSSong Liu 	int i;
24231e6d690bSSong Liu 	int ret = 0;
24241e6d690bSSong Liu 
24251e6d690bSSong Liu 	BUG_ON(!log);
24261e6d690bSSong Liu 
24271e6d690bSSong Liu 	for (i = 0; i < sh->disks; i++) {
24281e6d690bSSong Liu 		void *addr;
24291e6d690bSSong Liu 
24301e6d690bSSong Liu 		if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
24311e6d690bSSong Liu 			continue;
24321e6d690bSSong Liu 		addr = kmap_atomic(sh->dev[i].page);
24331e6d690bSSong Liu 		sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
24341e6d690bSSong Liu 						    addr, PAGE_SIZE);
24351e6d690bSSong Liu 		kunmap_atomic(addr);
24361e6d690bSSong Liu 		pages++;
24371e6d690bSSong Liu 	}
24381e6d690bSSong Liu 	WARN_ON(pages == 0);
24391e6d690bSSong Liu 
24401e6d690bSSong Liu 	/*
24411e6d690bSSong Liu 	 * The stripe must enter state machine again to call endio, so
24421e6d690bSSong Liu 	 * don't delay.
24431e6d690bSSong Liu 	 */
24441e6d690bSSong Liu 	clear_bit(STRIPE_DELAYED, &sh->state);
24451e6d690bSSong Liu 	atomic_inc(&sh->count);
24461e6d690bSSong Liu 
24471e6d690bSSong Liu 	mutex_lock(&log->io_mutex);
24481e6d690bSSong Liu 	/* meta + data */
24491e6d690bSSong Liu 	reserve = (1 + pages) << (PAGE_SHIFT - 9);
24501e6d690bSSong Liu 
2451a39f7afdSSong Liu 	if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
2452a39f7afdSSong Liu 	    sh->log_start == MaxSector)
2453a39f7afdSSong Liu 		r5l_add_no_space_stripe(log, sh);
2454a39f7afdSSong Liu 	else if (!r5l_has_free_space(log, reserve)) {
2455a39f7afdSSong Liu 		if (sh->log_start == log->last_checkpoint)
2456a39f7afdSSong Liu 			BUG();
2457a39f7afdSSong Liu 		else
2458a39f7afdSSong Liu 			r5l_add_no_space_stripe(log, sh);
24591e6d690bSSong Liu 	} else {
24601e6d690bSSong Liu 		ret = r5l_log_stripe(log, sh, pages, 0);
24611e6d690bSSong Liu 		if (ret) {
24621e6d690bSSong Liu 			spin_lock_irq(&log->io_list_lock);
24631e6d690bSSong Liu 			list_add_tail(&sh->log_list, &log->no_mem_stripes);
24641e6d690bSSong Liu 			spin_unlock_irq(&log->io_list_lock);
24651e6d690bSSong Liu 		}
24661e6d690bSSong Liu 	}
24671e6d690bSSong Liu 
24681e6d690bSSong Liu 	mutex_unlock(&log->io_mutex);
24691e6d690bSSong Liu 	return 0;
24702ded3703SSong Liu }
24712ded3703SSong Liu 
2472f6bed0efSShaohua Li static int r5l_load_log(struct r5l_log *log)
2473f6bed0efSShaohua Li {
2474f6bed0efSShaohua Li 	struct md_rdev *rdev = log->rdev;
2475f6bed0efSShaohua Li 	struct page *page;
2476f6bed0efSShaohua Li 	struct r5l_meta_block *mb;
2477f6bed0efSShaohua Li 	sector_t cp = log->rdev->journal_tail;
2478f6bed0efSShaohua Li 	u32 stored_crc, expected_crc;
2479f6bed0efSShaohua Li 	bool create_super = false;
2480f6bed0efSShaohua Li 	int ret;
2481f6bed0efSShaohua Li 
2482f6bed0efSShaohua Li 	/* Make sure it's valid */
2483f6bed0efSShaohua Li 	if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
2484f6bed0efSShaohua Li 		cp = 0;
2485f6bed0efSShaohua Li 	page = alloc_page(GFP_KERNEL);
2486f6bed0efSShaohua Li 	if (!page)
2487f6bed0efSShaohua Li 		return -ENOMEM;
2488f6bed0efSShaohua Li 
2489796a5cf0SMike Christie 	if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) {
2490f6bed0efSShaohua Li 		ret = -EIO;
2491f6bed0efSShaohua Li 		goto ioerr;
2492f6bed0efSShaohua Li 	}
2493f6bed0efSShaohua Li 	mb = page_address(page);
2494f6bed0efSShaohua Li 
2495f6bed0efSShaohua Li 	if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
2496f6bed0efSShaohua Li 	    mb->version != R5LOG_VERSION) {
2497f6bed0efSShaohua Li 		create_super = true;
2498f6bed0efSShaohua Li 		goto create;
2499f6bed0efSShaohua Li 	}
2500f6bed0efSShaohua Li 	stored_crc = le32_to_cpu(mb->checksum);
2501f6bed0efSShaohua Li 	mb->checksum = 0;
25025cb2fbd6SShaohua Li 	expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
2503f6bed0efSShaohua Li 	if (stored_crc != expected_crc) {
2504f6bed0efSShaohua Li 		create_super = true;
2505f6bed0efSShaohua Li 		goto create;
2506f6bed0efSShaohua Li 	}
2507f6bed0efSShaohua Li 	if (le64_to_cpu(mb->position) != cp) {
2508f6bed0efSShaohua Li 		create_super = true;
2509f6bed0efSShaohua Li 		goto create;
2510f6bed0efSShaohua Li 	}
2511f6bed0efSShaohua Li create:
2512f6bed0efSShaohua Li 	if (create_super) {
2513f6bed0efSShaohua Li 		log->last_cp_seq = prandom_u32();
2514f6bed0efSShaohua Li 		cp = 0;
251556056c2eSZhengyuan Liu 		r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq);
2516f6bed0efSShaohua Li 		/*
2517f6bed0efSShaohua Li 		 * Make sure super points to correct address. Log might have
2518f6bed0efSShaohua Li 		 * data very soon. If super hasn't correct log tail address,
2519f6bed0efSShaohua Li 		 * recovery can't find the log
2520f6bed0efSShaohua Li 		 */
2521f6bed0efSShaohua Li 		r5l_write_super(log, cp);
2522f6bed0efSShaohua Li 	} else
2523f6bed0efSShaohua Li 		log->last_cp_seq = le64_to_cpu(mb->seq);
2524f6bed0efSShaohua Li 
2525f6bed0efSShaohua Li 	log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
25260576b1c6SShaohua Li 	log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
25270576b1c6SShaohua Li 	if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
25280576b1c6SShaohua Li 		log->max_free_space = RECLAIM_MAX_FREE_SPACE;
2529f6bed0efSShaohua Li 	log->last_checkpoint = cp;
253028cd88e2SZhengyuan Liu 	log->next_checkpoint = cp;
2531a39f7afdSSong Liu 	mutex_lock(&log->io_mutex);
2532a39f7afdSSong Liu 	r5c_update_log_state(log);
2533a39f7afdSSong Liu 	mutex_unlock(&log->io_mutex);
2534f6bed0efSShaohua Li 
2535f6bed0efSShaohua Li 	__free_page(page);
2536f6bed0efSShaohua Li 
2537f6bed0efSShaohua Li 	return r5l_recovery_log(log);
2538f6bed0efSShaohua Li ioerr:
2539f6bed0efSShaohua Li 	__free_page(page);
2540f6bed0efSShaohua Li 	return ret;
2541f6bed0efSShaohua Li }
2542f6bed0efSShaohua Li 
2543f6bed0efSShaohua Li int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
2544f6bed0efSShaohua Li {
2545c888a8f9SJens Axboe 	struct request_queue *q = bdev_get_queue(rdev->bdev);
2546f6bed0efSShaohua Li 	struct r5l_log *log;
2547f6bed0efSShaohua Li 
2548f6bed0efSShaohua Li 	if (PAGE_SIZE != 4096)
2549f6bed0efSShaohua Li 		return -EINVAL;
2550c757ec95SSong Liu 
2551c757ec95SSong Liu 	/*
2552c757ec95SSong Liu 	 * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and
2553c757ec95SSong Liu 	 * raid_disks r5l_payload_data_parity.
2554c757ec95SSong Liu 	 *
2555c757ec95SSong Liu 	 * Write journal and cache does not work for very big array
2556c757ec95SSong Liu 	 * (raid_disks > 203)
2557c757ec95SSong Liu 	 */
2558c757ec95SSong Liu 	if (sizeof(struct r5l_meta_block) +
2559c757ec95SSong Liu 	    ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) *
2560c757ec95SSong Liu 	     conf->raid_disks) > PAGE_SIZE) {
2561c757ec95SSong Liu 		pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n",
2562c757ec95SSong Liu 		       mdname(conf->mddev), conf->raid_disks);
2563c757ec95SSong Liu 		return -EINVAL;
2564c757ec95SSong Liu 	}
2565c757ec95SSong Liu 
2566f6bed0efSShaohua Li 	log = kzalloc(sizeof(*log), GFP_KERNEL);
2567f6bed0efSShaohua Li 	if (!log)
2568f6bed0efSShaohua Li 		return -ENOMEM;
2569f6bed0efSShaohua Li 	log->rdev = rdev;
2570f6bed0efSShaohua Li 
2571c888a8f9SJens Axboe 	log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0;
257256fef7c6SChristoph Hellwig 
25735cb2fbd6SShaohua Li 	log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
2574f6bed0efSShaohua Li 				       sizeof(rdev->mddev->uuid));
2575f6bed0efSShaohua Li 
2576f6bed0efSShaohua Li 	mutex_init(&log->io_mutex);
2577f6bed0efSShaohua Li 
2578f6bed0efSShaohua Li 	spin_lock_init(&log->io_list_lock);
2579f6bed0efSShaohua Li 	INIT_LIST_HEAD(&log->running_ios);
25800576b1c6SShaohua Li 	INIT_LIST_HEAD(&log->io_end_ios);
2581a8c34f91SShaohua Li 	INIT_LIST_HEAD(&log->flushing_ios);
258204732f74SChristoph Hellwig 	INIT_LIST_HEAD(&log->finished_ios);
2583a8c34f91SShaohua Li 	bio_init(&log->flush_bio);
2584f6bed0efSShaohua Li 
2585f6bed0efSShaohua Li 	log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
2586f6bed0efSShaohua Li 	if (!log->io_kc)
2587f6bed0efSShaohua Li 		goto io_kc;
2588f6bed0efSShaohua Li 
25895036c390SChristoph Hellwig 	log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc);
25905036c390SChristoph Hellwig 	if (!log->io_pool)
25915036c390SChristoph Hellwig 		goto io_pool;
25925036c390SChristoph Hellwig 
2593c38d29b3SChristoph Hellwig 	log->bs = bioset_create(R5L_POOL_SIZE, 0);
2594c38d29b3SChristoph Hellwig 	if (!log->bs)
2595c38d29b3SChristoph Hellwig 		goto io_bs;
2596c38d29b3SChristoph Hellwig 
2597e8deb638SChristoph Hellwig 	log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
2598e8deb638SChristoph Hellwig 	if (!log->meta_pool)
2599e8deb638SChristoph Hellwig 		goto out_mempool;
2600e8deb638SChristoph Hellwig 
26010576b1c6SShaohua Li 	log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
26020576b1c6SShaohua Li 						 log->rdev->mddev, "reclaim");
26030576b1c6SShaohua Li 	if (!log->reclaim_thread)
26040576b1c6SShaohua Li 		goto reclaim_thread;
2605a39f7afdSSong Liu 	log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
2606a39f7afdSSong Liu 
26070fd22b45SShaohua Li 	init_waitqueue_head(&log->iounit_wait);
26080576b1c6SShaohua Li 
26095036c390SChristoph Hellwig 	INIT_LIST_HEAD(&log->no_mem_stripes);
26105036c390SChristoph Hellwig 
2611f6bed0efSShaohua Li 	INIT_LIST_HEAD(&log->no_space_stripes);
2612f6bed0efSShaohua Li 	spin_lock_init(&log->no_space_stripes_lock);
2613f6bed0efSShaohua Li 
26143bddb7f8SSong Liu 	INIT_WORK(&log->deferred_io_work, r5l_submit_io_async);
26153bddb7f8SSong Liu 
26162ded3703SSong Liu 	log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
2617a39f7afdSSong Liu 	INIT_LIST_HEAD(&log->stripe_in_journal_list);
2618a39f7afdSSong Liu 	spin_lock_init(&log->stripe_in_journal_lock);
2619a39f7afdSSong Liu 	atomic_set(&log->stripe_in_journal_count, 0);
26202ded3703SSong Liu 
2621f6bed0efSShaohua Li 	if (r5l_load_log(log))
2622f6bed0efSShaohua Li 		goto error;
2623f6bed0efSShaohua Li 
2624f6b6ec5cSShaohua Li 	rcu_assign_pointer(conf->log, log);
2625a62ab49eSShaohua Li 	set_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
2626f6bed0efSShaohua Li 	return 0;
2627e8deb638SChristoph Hellwig 
2628f6bed0efSShaohua Li error:
26290576b1c6SShaohua Li 	md_unregister_thread(&log->reclaim_thread);
26300576b1c6SShaohua Li reclaim_thread:
2631e8deb638SChristoph Hellwig 	mempool_destroy(log->meta_pool);
2632e8deb638SChristoph Hellwig out_mempool:
2633c38d29b3SChristoph Hellwig 	bioset_free(log->bs);
2634c38d29b3SChristoph Hellwig io_bs:
26355036c390SChristoph Hellwig 	mempool_destroy(log->io_pool);
26365036c390SChristoph Hellwig io_pool:
2637f6bed0efSShaohua Li 	kmem_cache_destroy(log->io_kc);
2638f6bed0efSShaohua Li io_kc:
2639f6bed0efSShaohua Li 	kfree(log);
2640f6bed0efSShaohua Li 	return -EINVAL;
2641f6bed0efSShaohua Li }
2642f6bed0efSShaohua Li 
2643f6bed0efSShaohua Li void r5l_exit_log(struct r5l_log *log)
2644f6bed0efSShaohua Li {
26450576b1c6SShaohua Li 	md_unregister_thread(&log->reclaim_thread);
2646e8deb638SChristoph Hellwig 	mempool_destroy(log->meta_pool);
2647c38d29b3SChristoph Hellwig 	bioset_free(log->bs);
26485036c390SChristoph Hellwig 	mempool_destroy(log->io_pool);
2649f6bed0efSShaohua Li 	kmem_cache_destroy(log->io_kc);
2650f6bed0efSShaohua Li 	kfree(log);
2651f6bed0efSShaohua Li }
2652