xref: /linux/drivers/md/raid5-cache.c (revision 3bddb7f8f264ec58dc86e11ca97341c24f9d38f6)
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>
22f6bed0efSShaohua Li #include "md.h"
23f6bed0efSShaohua Li #include "raid5.h"
241e6d690bSSong Liu #include "bitmap.h"
25f6bed0efSShaohua Li 
26f6bed0efSShaohua Li /*
27f6bed0efSShaohua Li  * metadata/data stored in disk with 4k size unit (a block) regardless
28f6bed0efSShaohua Li  * underneath hardware sector size. only works with PAGE_SIZE == 4096
29f6bed0efSShaohua Li  */
30f6bed0efSShaohua Li #define BLOCK_SECTORS (8)
31f6bed0efSShaohua Li 
320576b1c6SShaohua Li /*
33a39f7afdSSong Liu  * log->max_free_space is min(1/4 disk size, 10G reclaimable space).
34a39f7afdSSong Liu  *
35a39f7afdSSong Liu  * In write through mode, the reclaim runs every log->max_free_space.
36a39f7afdSSong Liu  * This can prevent the recovery scans for too long
370576b1c6SShaohua Li  */
380576b1c6SShaohua Li #define RECLAIM_MAX_FREE_SPACE (10 * 1024 * 1024 * 2) /* sector */
390576b1c6SShaohua Li #define RECLAIM_MAX_FREE_SPACE_SHIFT (2)
400576b1c6SShaohua Li 
41a39f7afdSSong Liu /* wake up reclaim thread periodically */
42a39f7afdSSong Liu #define R5C_RECLAIM_WAKEUP_INTERVAL (30 * HZ)
43a39f7afdSSong Liu /* start flush with these full stripes */
44a39f7afdSSong Liu #define R5C_FULL_STRIPE_FLUSH_BATCH 256
45a39f7afdSSong Liu /* reclaim stripes in groups */
46a39f7afdSSong Liu #define R5C_RECLAIM_STRIPE_GROUP (NR_STRIPE_HASH_LOCKS * 2)
47a39f7afdSSong Liu 
48c38d29b3SChristoph Hellwig /*
49c38d29b3SChristoph Hellwig  * We only need 2 bios per I/O unit to make progress, but ensure we
50c38d29b3SChristoph Hellwig  * have a few more available to not get too tight.
51c38d29b3SChristoph Hellwig  */
52c38d29b3SChristoph Hellwig #define R5L_POOL_SIZE	4
53c38d29b3SChristoph Hellwig 
542ded3703SSong Liu /*
552ded3703SSong Liu  * r5c journal modes of the array: write-back or write-through.
562ded3703SSong Liu  * write-through mode has identical behavior as existing log only
572ded3703SSong Liu  * implementation.
582ded3703SSong Liu  */
592ded3703SSong Liu enum r5c_journal_mode {
602ded3703SSong Liu 	R5C_JOURNAL_MODE_WRITE_THROUGH = 0,
612ded3703SSong Liu 	R5C_JOURNAL_MODE_WRITE_BACK = 1,
622ded3703SSong Liu };
632ded3703SSong Liu 
642c7da14bSSong Liu static char *r5c_journal_mode_str[] = {"write-through",
652c7da14bSSong Liu 				       "write-back"};
662ded3703SSong Liu /*
672ded3703SSong Liu  * raid5 cache state machine
682ded3703SSong Liu  *
692ded3703SSong Liu  * With rhe RAID cache, each stripe works in two phases:
702ded3703SSong Liu  *	- caching phase
712ded3703SSong Liu  *	- writing-out phase
722ded3703SSong Liu  *
732ded3703SSong Liu  * These two phases are controlled by bit STRIPE_R5C_CACHING:
742ded3703SSong Liu  *   if STRIPE_R5C_CACHING == 0, the stripe is in writing-out phase
752ded3703SSong Liu  *   if STRIPE_R5C_CACHING == 1, the stripe is in caching phase
762ded3703SSong Liu  *
772ded3703SSong Liu  * When there is no journal, or the journal is in write-through mode,
782ded3703SSong Liu  * the stripe is always in writing-out phase.
792ded3703SSong Liu  *
802ded3703SSong Liu  * For write-back journal, the stripe is sent to caching phase on write
812ded3703SSong Liu  * (r5c_try_caching_write). r5c_make_stripe_write_out() kicks off
822ded3703SSong Liu  * the write-out phase by clearing STRIPE_R5C_CACHING.
832ded3703SSong Liu  *
842ded3703SSong Liu  * Stripes in caching phase do not write the raid disks. Instead, all
852ded3703SSong Liu  * writes are committed from the log device. Therefore, a stripe in
862ded3703SSong Liu  * caching phase handles writes as:
872ded3703SSong Liu  *	- write to log device
882ded3703SSong Liu  *	- return IO
892ded3703SSong Liu  *
902ded3703SSong Liu  * Stripes in writing-out phase handle writes as:
912ded3703SSong Liu  *	- calculate parity
922ded3703SSong Liu  *	- write pending data and parity to journal
932ded3703SSong Liu  *	- write data and parity to raid disks
942ded3703SSong Liu  *	- return IO for pending writes
952ded3703SSong Liu  */
962ded3703SSong Liu 
97f6bed0efSShaohua Li struct r5l_log {
98f6bed0efSShaohua Li 	struct md_rdev *rdev;
99f6bed0efSShaohua Li 
100f6bed0efSShaohua Li 	u32 uuid_checksum;
101f6bed0efSShaohua Li 
102f6bed0efSShaohua Li 	sector_t device_size;		/* log device size, round to
103f6bed0efSShaohua Li 					 * BLOCK_SECTORS */
1040576b1c6SShaohua Li 	sector_t max_free_space;	/* reclaim run if free space is at
1050576b1c6SShaohua Li 					 * this size */
106f6bed0efSShaohua Li 
107f6bed0efSShaohua Li 	sector_t last_checkpoint;	/* log tail. where recovery scan
108f6bed0efSShaohua Li 					 * starts from */
109f6bed0efSShaohua Li 	u64 last_cp_seq;		/* log tail sequence */
110f6bed0efSShaohua Li 
111f6bed0efSShaohua Li 	sector_t log_start;		/* log head. where new data appends */
112f6bed0efSShaohua Li 	u64 seq;			/* log head sequence */
113f6bed0efSShaohua Li 
11417036461SChristoph Hellwig 	sector_t next_checkpoint;
11517036461SChristoph Hellwig 	u64 next_cp_seq;
11617036461SChristoph Hellwig 
117f6bed0efSShaohua Li 	struct mutex io_mutex;
118f6bed0efSShaohua Li 	struct r5l_io_unit *current_io;	/* current io_unit accepting new data */
119f6bed0efSShaohua Li 
120f6bed0efSShaohua Li 	spinlock_t io_list_lock;
121f6bed0efSShaohua Li 	struct list_head running_ios;	/* io_units which are still running,
122f6bed0efSShaohua Li 					 * and have not yet been completely
123f6bed0efSShaohua Li 					 * written to the log */
124f6bed0efSShaohua Li 	struct list_head io_end_ios;	/* io_units which have been completely
125f6bed0efSShaohua Li 					 * written to the log but not yet written
126f6bed0efSShaohua Li 					 * to the RAID */
127a8c34f91SShaohua Li 	struct list_head flushing_ios;	/* io_units which are waiting for log
128a8c34f91SShaohua Li 					 * cache flush */
12904732f74SChristoph Hellwig 	struct list_head finished_ios;	/* io_units which settle down in log disk */
130a8c34f91SShaohua Li 	struct bio flush_bio;
131f6bed0efSShaohua Li 
1325036c390SChristoph Hellwig 	struct list_head no_mem_stripes;   /* pending stripes, -ENOMEM */
1335036c390SChristoph Hellwig 
134f6bed0efSShaohua Li 	struct kmem_cache *io_kc;
1355036c390SChristoph Hellwig 	mempool_t *io_pool;
136c38d29b3SChristoph Hellwig 	struct bio_set *bs;
137e8deb638SChristoph Hellwig 	mempool_t *meta_pool;
138f6bed0efSShaohua Li 
1390576b1c6SShaohua Li 	struct md_thread *reclaim_thread;
1400576b1c6SShaohua Li 	unsigned long reclaim_target;	/* number of space that need to be
1410576b1c6SShaohua Li 					 * reclaimed.  if it's 0, reclaim spaces
1420576b1c6SShaohua Li 					 * used by io_units which are in
1430576b1c6SShaohua Li 					 * IO_UNIT_STRIPE_END state (eg, reclaim
1440576b1c6SShaohua Li 					 * dones't wait for specific io_unit
1450576b1c6SShaohua Li 					 * switching to IO_UNIT_STRIPE_END
1460576b1c6SShaohua Li 					 * state) */
1470fd22b45SShaohua Li 	wait_queue_head_t iounit_wait;
1480576b1c6SShaohua Li 
149f6bed0efSShaohua Li 	struct list_head no_space_stripes; /* pending stripes, log has no space */
150f6bed0efSShaohua Li 	spinlock_t no_space_stripes_lock;
15156fef7c6SChristoph Hellwig 
15256fef7c6SChristoph Hellwig 	bool need_cache_flush;
1532ded3703SSong Liu 
1542ded3703SSong Liu 	/* for r5c_cache */
1552ded3703SSong Liu 	enum r5c_journal_mode r5c_journal_mode;
156a39f7afdSSong Liu 
157a39f7afdSSong Liu 	/* all stripes in r5cache, in the order of seq at sh->log_start */
158a39f7afdSSong Liu 	struct list_head stripe_in_journal_list;
159a39f7afdSSong Liu 
160a39f7afdSSong Liu 	spinlock_t stripe_in_journal_lock;
161a39f7afdSSong Liu 	atomic_t stripe_in_journal_count;
162*3bddb7f8SSong Liu 
163*3bddb7f8SSong Liu 	/* to submit async io_units, to fulfill ordering of flush */
164*3bddb7f8SSong Liu 	struct work_struct deferred_io_work;
165f6bed0efSShaohua Li };
166f6bed0efSShaohua Li 
167f6bed0efSShaohua Li /*
168f6bed0efSShaohua Li  * an IO range starts from a meta data block and end at the next meta data
169f6bed0efSShaohua Li  * block. The io unit's the meta data block tracks data/parity followed it. io
170f6bed0efSShaohua Li  * unit is written to log disk with normal write, as we always flush log disk
171f6bed0efSShaohua Li  * first and then start move data to raid disks, there is no requirement to
172f6bed0efSShaohua Li  * write io unit with FLUSH/FUA
173f6bed0efSShaohua Li  */
174f6bed0efSShaohua Li struct r5l_io_unit {
175f6bed0efSShaohua Li 	struct r5l_log *log;
176f6bed0efSShaohua Li 
177f6bed0efSShaohua Li 	struct page *meta_page;	/* store meta block */
178f6bed0efSShaohua Li 	int meta_offset;	/* current offset in meta_page */
179f6bed0efSShaohua Li 
180f6bed0efSShaohua Li 	struct bio *current_bio;/* current_bio accepting new data */
181f6bed0efSShaohua Li 
182f6bed0efSShaohua Li 	atomic_t pending_stripe;/* how many stripes not flushed to raid */
183f6bed0efSShaohua Li 	u64 seq;		/* seq number of the metablock */
184f6bed0efSShaohua Li 	sector_t log_start;	/* where the io_unit starts */
185f6bed0efSShaohua Li 	sector_t log_end;	/* where the io_unit ends */
186f6bed0efSShaohua Li 	struct list_head log_sibling; /* log->running_ios */
187f6bed0efSShaohua Li 	struct list_head stripe_list; /* stripes added to the io_unit */
188f6bed0efSShaohua Li 
189f6bed0efSShaohua Li 	int state;
1906143e2ceSChristoph Hellwig 	bool need_split_bio;
191*3bddb7f8SSong Liu 	struct bio *split_bio;
192*3bddb7f8SSong Liu 
193*3bddb7f8SSong Liu 	unsigned int has_flush:1;      /* include flush request */
194*3bddb7f8SSong Liu 	unsigned int has_fua:1;        /* include fua request */
195*3bddb7f8SSong Liu 	unsigned int has_null_flush:1; /* include empty flush request */
196*3bddb7f8SSong Liu 	/*
197*3bddb7f8SSong Liu 	 * io isn't sent yet, flush/fua request can only be submitted till it's
198*3bddb7f8SSong Liu 	 * the first IO in running_ios list
199*3bddb7f8SSong Liu 	 */
200*3bddb7f8SSong Liu 	unsigned int io_deferred:1;
201*3bddb7f8SSong Liu 
202*3bddb7f8SSong Liu 	struct bio_list flush_barriers;   /* size == 0 flush bios */
203f6bed0efSShaohua Li };
204f6bed0efSShaohua Li 
205f6bed0efSShaohua Li /* r5l_io_unit state */
206f6bed0efSShaohua Li enum r5l_io_unit_state {
207f6bed0efSShaohua Li 	IO_UNIT_RUNNING = 0,	/* accepting new IO */
208f6bed0efSShaohua Li 	IO_UNIT_IO_START = 1,	/* io_unit bio start writing to log,
209f6bed0efSShaohua Li 				 * don't accepting new bio */
210f6bed0efSShaohua Li 	IO_UNIT_IO_END = 2,	/* io_unit bio finish writing to log */
211a8c34f91SShaohua Li 	IO_UNIT_STRIPE_END = 3,	/* stripes data finished writing to raid */
212f6bed0efSShaohua Li };
213f6bed0efSShaohua Li 
2142ded3703SSong Liu bool r5c_is_writeback(struct r5l_log *log)
2152ded3703SSong Liu {
2162ded3703SSong Liu 	return (log != NULL &&
2172ded3703SSong Liu 		log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK);
2182ded3703SSong Liu }
2192ded3703SSong Liu 
220f6bed0efSShaohua Li static sector_t r5l_ring_add(struct r5l_log *log, sector_t start, sector_t inc)
221f6bed0efSShaohua Li {
222f6bed0efSShaohua Li 	start += inc;
223f6bed0efSShaohua Li 	if (start >= log->device_size)
224f6bed0efSShaohua Li 		start = start - log->device_size;
225f6bed0efSShaohua Li 	return start;
226f6bed0efSShaohua Li }
227f6bed0efSShaohua Li 
228f6bed0efSShaohua Li static sector_t r5l_ring_distance(struct r5l_log *log, sector_t start,
229f6bed0efSShaohua Li 				  sector_t end)
230f6bed0efSShaohua Li {
231f6bed0efSShaohua Li 	if (end >= start)
232f6bed0efSShaohua Li 		return end - start;
233f6bed0efSShaohua Li 	else
234f6bed0efSShaohua Li 		return end + log->device_size - start;
235f6bed0efSShaohua Li }
236f6bed0efSShaohua Li 
237f6bed0efSShaohua Li static bool r5l_has_free_space(struct r5l_log *log, sector_t size)
238f6bed0efSShaohua Li {
239f6bed0efSShaohua Li 	sector_t used_size;
240f6bed0efSShaohua Li 
241f6bed0efSShaohua Li 	used_size = r5l_ring_distance(log, log->last_checkpoint,
242f6bed0efSShaohua Li 					log->log_start);
243f6bed0efSShaohua Li 
244f6bed0efSShaohua Li 	return log->device_size > used_size + size;
245f6bed0efSShaohua Li }
246f6bed0efSShaohua Li 
247f6bed0efSShaohua Li static void __r5l_set_io_unit_state(struct r5l_io_unit *io,
248f6bed0efSShaohua Li 				    enum r5l_io_unit_state state)
249f6bed0efSShaohua Li {
250f6bed0efSShaohua Li 	if (WARN_ON(io->state >= state))
251f6bed0efSShaohua Li 		return;
252f6bed0efSShaohua Li 	io->state = state;
253f6bed0efSShaohua Li }
254f6bed0efSShaohua Li 
2551e6d690bSSong Liu static void
2561e6d690bSSong Liu r5c_return_dev_pending_writes(struct r5conf *conf, struct r5dev *dev,
2571e6d690bSSong Liu 			      struct bio_list *return_bi)
2581e6d690bSSong Liu {
2591e6d690bSSong Liu 	struct bio *wbi, *wbi2;
2601e6d690bSSong Liu 
2611e6d690bSSong Liu 	wbi = dev->written;
2621e6d690bSSong Liu 	dev->written = NULL;
2631e6d690bSSong Liu 	while (wbi && wbi->bi_iter.bi_sector <
2641e6d690bSSong Liu 	       dev->sector + STRIPE_SECTORS) {
2651e6d690bSSong Liu 		wbi2 = r5_next_bio(wbi, dev->sector);
2661e6d690bSSong Liu 		if (!raid5_dec_bi_active_stripes(wbi)) {
2671e6d690bSSong Liu 			md_write_end(conf->mddev);
2681e6d690bSSong Liu 			bio_list_add(return_bi, wbi);
2691e6d690bSSong Liu 		}
2701e6d690bSSong Liu 		wbi = wbi2;
2711e6d690bSSong Liu 	}
2721e6d690bSSong Liu }
2731e6d690bSSong Liu 
2741e6d690bSSong Liu void r5c_handle_cached_data_endio(struct r5conf *conf,
2751e6d690bSSong Liu 	  struct stripe_head *sh, int disks, struct bio_list *return_bi)
2761e6d690bSSong Liu {
2771e6d690bSSong Liu 	int i;
2781e6d690bSSong Liu 
2791e6d690bSSong Liu 	for (i = sh->disks; i--; ) {
2801e6d690bSSong Liu 		if (sh->dev[i].written) {
2811e6d690bSSong Liu 			set_bit(R5_UPTODATE, &sh->dev[i].flags);
2821e6d690bSSong Liu 			r5c_return_dev_pending_writes(conf, &sh->dev[i],
2831e6d690bSSong Liu 						      return_bi);
2841e6d690bSSong Liu 			bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2851e6d690bSSong Liu 					STRIPE_SECTORS,
2861e6d690bSSong Liu 					!test_bit(STRIPE_DEGRADED, &sh->state),
2871e6d690bSSong Liu 					0);
2881e6d690bSSong Liu 		}
2891e6d690bSSong Liu 	}
2901e6d690bSSong Liu }
2911e6d690bSSong Liu 
292a39f7afdSSong Liu /* Check whether we should flush some stripes to free up stripe cache */
293a39f7afdSSong Liu void r5c_check_stripe_cache_usage(struct r5conf *conf)
294a39f7afdSSong Liu {
295a39f7afdSSong Liu 	int total_cached;
296a39f7afdSSong Liu 
297a39f7afdSSong Liu 	if (!r5c_is_writeback(conf->log))
298a39f7afdSSong Liu 		return;
299a39f7afdSSong Liu 
300a39f7afdSSong Liu 	total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
301a39f7afdSSong Liu 		atomic_read(&conf->r5c_cached_full_stripes);
302a39f7afdSSong Liu 
303a39f7afdSSong Liu 	/*
304a39f7afdSSong Liu 	 * The following condition is true for either of the following:
305a39f7afdSSong Liu 	 *   - stripe cache pressure high:
306a39f7afdSSong Liu 	 *          total_cached > 3/4 min_nr_stripes ||
307a39f7afdSSong Liu 	 *          empty_inactive_list_nr > 0
308a39f7afdSSong Liu 	 *   - stripe cache pressure moderate:
309a39f7afdSSong Liu 	 *          total_cached > 1/2 min_nr_stripes
310a39f7afdSSong Liu 	 */
311a39f7afdSSong Liu 	if (total_cached > conf->min_nr_stripes * 1 / 2 ||
312a39f7afdSSong Liu 	    atomic_read(&conf->empty_inactive_list_nr) > 0)
313a39f7afdSSong Liu 		r5l_wake_reclaim(conf->log, 0);
314a39f7afdSSong Liu }
315a39f7afdSSong Liu 
316a39f7afdSSong Liu /*
317a39f7afdSSong Liu  * flush cache when there are R5C_FULL_STRIPE_FLUSH_BATCH or more full
318a39f7afdSSong Liu  * stripes in the cache
319a39f7afdSSong Liu  */
320a39f7afdSSong Liu void r5c_check_cached_full_stripe(struct r5conf *conf)
321a39f7afdSSong Liu {
322a39f7afdSSong Liu 	if (!r5c_is_writeback(conf->log))
323a39f7afdSSong Liu 		return;
324a39f7afdSSong Liu 
325a39f7afdSSong Liu 	/*
326a39f7afdSSong Liu 	 * wake up reclaim for R5C_FULL_STRIPE_FLUSH_BATCH cached stripes
327a39f7afdSSong Liu 	 * or a full stripe (chunk size / 4k stripes).
328a39f7afdSSong Liu 	 */
329a39f7afdSSong Liu 	if (atomic_read(&conf->r5c_cached_full_stripes) >=
330a39f7afdSSong Liu 	    min(R5C_FULL_STRIPE_FLUSH_BATCH,
331a39f7afdSSong Liu 		conf->chunk_sectors >> STRIPE_SHIFT))
332a39f7afdSSong Liu 		r5l_wake_reclaim(conf->log, 0);
333a39f7afdSSong Liu }
334a39f7afdSSong Liu 
335a39f7afdSSong Liu /*
336a39f7afdSSong Liu  * Total log space (in sectors) needed to flush all data in cache
337a39f7afdSSong Liu  *
338a39f7afdSSong Liu  * Currently, writing-out phase automatically includes all pending writes
339a39f7afdSSong Liu  * to the same sector. So the reclaim of each stripe takes up to
340a39f7afdSSong Liu  * (conf->raid_disks + 1) pages of log space.
341a39f7afdSSong Liu  *
342a39f7afdSSong Liu  * To totally avoid deadlock due to log space, the code reserves
343a39f7afdSSong Liu  * (conf->raid_disks + 1) pages for each stripe in cache, which is not
344a39f7afdSSong Liu  * necessary in most cases.
345a39f7afdSSong Liu  *
346a39f7afdSSong Liu  * To improve this, we will need writing-out phase to be able to NOT include
347a39f7afdSSong Liu  * pending writes, which will reduce the requirement to
348a39f7afdSSong Liu  * (conf->max_degraded + 1) pages per stripe in cache.
349a39f7afdSSong Liu  */
350a39f7afdSSong Liu static sector_t r5c_log_required_to_flush_cache(struct r5conf *conf)
351a39f7afdSSong Liu {
352a39f7afdSSong Liu 	struct r5l_log *log = conf->log;
353a39f7afdSSong Liu 
354a39f7afdSSong Liu 	if (!r5c_is_writeback(log))
355a39f7afdSSong Liu 		return 0;
356a39f7afdSSong Liu 
357a39f7afdSSong Liu 	return BLOCK_SECTORS * (conf->raid_disks + 1) *
358a39f7afdSSong Liu 		atomic_read(&log->stripe_in_journal_count);
359a39f7afdSSong Liu }
360a39f7afdSSong Liu 
361a39f7afdSSong Liu /*
362a39f7afdSSong Liu  * evaluate log space usage and update R5C_LOG_TIGHT and R5C_LOG_CRITICAL
363a39f7afdSSong Liu  *
364a39f7afdSSong Liu  * R5C_LOG_TIGHT is set when free space on the log device is less than 3x of
365a39f7afdSSong Liu  * reclaim_required_space. R5C_LOG_CRITICAL is set when free space on the log
366a39f7afdSSong Liu  * device is less than 2x of reclaim_required_space.
367a39f7afdSSong Liu  */
368a39f7afdSSong Liu static inline void r5c_update_log_state(struct r5l_log *log)
369a39f7afdSSong Liu {
370a39f7afdSSong Liu 	struct r5conf *conf = log->rdev->mddev->private;
371a39f7afdSSong Liu 	sector_t free_space;
372a39f7afdSSong Liu 	sector_t reclaim_space;
373a39f7afdSSong Liu 
374a39f7afdSSong Liu 	if (!r5c_is_writeback(log))
375a39f7afdSSong Liu 		return;
376a39f7afdSSong Liu 
377a39f7afdSSong Liu 	free_space = r5l_ring_distance(log, log->log_start,
378a39f7afdSSong Liu 				       log->last_checkpoint);
379a39f7afdSSong Liu 	reclaim_space = r5c_log_required_to_flush_cache(conf);
380a39f7afdSSong Liu 	if (free_space < 2 * reclaim_space)
381a39f7afdSSong Liu 		set_bit(R5C_LOG_CRITICAL, &conf->cache_state);
382a39f7afdSSong Liu 	else
383a39f7afdSSong Liu 		clear_bit(R5C_LOG_CRITICAL, &conf->cache_state);
384a39f7afdSSong Liu 	if (free_space < 3 * reclaim_space)
385a39f7afdSSong Liu 		set_bit(R5C_LOG_TIGHT, &conf->cache_state);
386a39f7afdSSong Liu 	else
387a39f7afdSSong Liu 		clear_bit(R5C_LOG_TIGHT, &conf->cache_state);
388a39f7afdSSong Liu }
389a39f7afdSSong Liu 
3902ded3703SSong Liu /*
3912ded3703SSong Liu  * Put the stripe into writing-out phase by clearing STRIPE_R5C_CACHING.
3922ded3703SSong Liu  * This function should only be called in write-back mode.
3932ded3703SSong Liu  */
394a39f7afdSSong Liu void r5c_make_stripe_write_out(struct stripe_head *sh)
3952ded3703SSong Liu {
3962ded3703SSong Liu 	struct r5conf *conf = sh->raid_conf;
3972ded3703SSong Liu 	struct r5l_log *log = conf->log;
3982ded3703SSong Liu 
3992ded3703SSong Liu 	BUG_ON(!r5c_is_writeback(log));
4002ded3703SSong Liu 
4012ded3703SSong Liu 	WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
4022ded3703SSong Liu 	clear_bit(STRIPE_R5C_CACHING, &sh->state);
4031e6d690bSSong Liu 
4041e6d690bSSong Liu 	if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4051e6d690bSSong Liu 		atomic_inc(&conf->preread_active_stripes);
4061e6d690bSSong Liu 
4071e6d690bSSong Liu 	if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) {
4081e6d690bSSong Liu 		BUG_ON(atomic_read(&conf->r5c_cached_partial_stripes) == 0);
4091e6d690bSSong Liu 		atomic_dec(&conf->r5c_cached_partial_stripes);
4101e6d690bSSong Liu 	}
4111e6d690bSSong Liu 
4121e6d690bSSong Liu 	if (test_and_clear_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) {
4131e6d690bSSong Liu 		BUG_ON(atomic_read(&conf->r5c_cached_full_stripes) == 0);
4141e6d690bSSong Liu 		atomic_dec(&conf->r5c_cached_full_stripes);
4151e6d690bSSong Liu 	}
4161e6d690bSSong Liu }
4171e6d690bSSong Liu 
4181e6d690bSSong Liu static void r5c_handle_data_cached(struct stripe_head *sh)
4191e6d690bSSong Liu {
4201e6d690bSSong Liu 	int i;
4211e6d690bSSong Liu 
4221e6d690bSSong Liu 	for (i = sh->disks; i--; )
4231e6d690bSSong Liu 		if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
4241e6d690bSSong Liu 			set_bit(R5_InJournal, &sh->dev[i].flags);
4251e6d690bSSong Liu 			clear_bit(R5_LOCKED, &sh->dev[i].flags);
4261e6d690bSSong Liu 		}
4271e6d690bSSong Liu 	clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
4281e6d690bSSong Liu }
4291e6d690bSSong Liu 
4301e6d690bSSong Liu /*
4311e6d690bSSong Liu  * this journal write must contain full parity,
4321e6d690bSSong Liu  * it may also contain some data pages
4331e6d690bSSong Liu  */
4341e6d690bSSong Liu static void r5c_handle_parity_cached(struct stripe_head *sh)
4351e6d690bSSong Liu {
4361e6d690bSSong Liu 	int i;
4371e6d690bSSong Liu 
4381e6d690bSSong Liu 	for (i = sh->disks; i--; )
4391e6d690bSSong Liu 		if (test_bit(R5_InJournal, &sh->dev[i].flags))
4401e6d690bSSong Liu 			set_bit(R5_Wantwrite, &sh->dev[i].flags);
4412ded3703SSong Liu }
4422ded3703SSong Liu 
4432ded3703SSong Liu /*
4442ded3703SSong Liu  * Setting proper flags after writing (or flushing) data and/or parity to the
4452ded3703SSong Liu  * log device. This is called from r5l_log_endio() or r5l_log_flush_endio().
4462ded3703SSong Liu  */
4472ded3703SSong Liu static void r5c_finish_cache_stripe(struct stripe_head *sh)
4482ded3703SSong Liu {
4492ded3703SSong Liu 	struct r5l_log *log = sh->raid_conf->log;
4502ded3703SSong Liu 
4512ded3703SSong Liu 	if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
4522ded3703SSong Liu 		BUG_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
4532ded3703SSong Liu 		/*
4542ded3703SSong Liu 		 * Set R5_InJournal for parity dev[pd_idx]. This means
4552ded3703SSong Liu 		 * all data AND parity in the journal. For RAID 6, it is
4562ded3703SSong Liu 		 * NOT necessary to set the flag for dev[qd_idx], as the
4572ded3703SSong Liu 		 * two parities are written out together.
4582ded3703SSong Liu 		 */
4592ded3703SSong Liu 		set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
4601e6d690bSSong Liu 	} else if (test_bit(STRIPE_R5C_CACHING, &sh->state)) {
4611e6d690bSSong Liu 		r5c_handle_data_cached(sh);
4621e6d690bSSong Liu 	} else {
4631e6d690bSSong Liu 		r5c_handle_parity_cached(sh);
4641e6d690bSSong Liu 		set_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
4651e6d690bSSong Liu 	}
4662ded3703SSong Liu }
4672ded3703SSong Liu 
468d8858f43SChristoph Hellwig static void r5l_io_run_stripes(struct r5l_io_unit *io)
469d8858f43SChristoph Hellwig {
470d8858f43SChristoph Hellwig 	struct stripe_head *sh, *next;
471d8858f43SChristoph Hellwig 
472d8858f43SChristoph Hellwig 	list_for_each_entry_safe(sh, next, &io->stripe_list, log_list) {
473d8858f43SChristoph Hellwig 		list_del_init(&sh->log_list);
4742ded3703SSong Liu 
4752ded3703SSong Liu 		r5c_finish_cache_stripe(sh);
4762ded3703SSong Liu 
477d8858f43SChristoph Hellwig 		set_bit(STRIPE_HANDLE, &sh->state);
478d8858f43SChristoph Hellwig 		raid5_release_stripe(sh);
479d8858f43SChristoph Hellwig 	}
480d8858f43SChristoph Hellwig }
481d8858f43SChristoph Hellwig 
48256fef7c6SChristoph Hellwig static void r5l_log_run_stripes(struct r5l_log *log)
48356fef7c6SChristoph Hellwig {
48456fef7c6SChristoph Hellwig 	struct r5l_io_unit *io, *next;
48556fef7c6SChristoph Hellwig 
48656fef7c6SChristoph Hellwig 	assert_spin_locked(&log->io_list_lock);
48756fef7c6SChristoph Hellwig 
48856fef7c6SChristoph Hellwig 	list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
48956fef7c6SChristoph Hellwig 		/* don't change list order */
49056fef7c6SChristoph Hellwig 		if (io->state < IO_UNIT_IO_END)
49156fef7c6SChristoph Hellwig 			break;
49256fef7c6SChristoph Hellwig 
49356fef7c6SChristoph Hellwig 		list_move_tail(&io->log_sibling, &log->finished_ios);
49456fef7c6SChristoph Hellwig 		r5l_io_run_stripes(io);
49556fef7c6SChristoph Hellwig 	}
49656fef7c6SChristoph Hellwig }
49756fef7c6SChristoph Hellwig 
4983848c0bcSChristoph Hellwig static void r5l_move_to_end_ios(struct r5l_log *log)
4993848c0bcSChristoph Hellwig {
5003848c0bcSChristoph Hellwig 	struct r5l_io_unit *io, *next;
5013848c0bcSChristoph Hellwig 
5023848c0bcSChristoph Hellwig 	assert_spin_locked(&log->io_list_lock);
5033848c0bcSChristoph Hellwig 
5043848c0bcSChristoph Hellwig 	list_for_each_entry_safe(io, next, &log->running_ios, log_sibling) {
5053848c0bcSChristoph Hellwig 		/* don't change list order */
5063848c0bcSChristoph Hellwig 		if (io->state < IO_UNIT_IO_END)
5073848c0bcSChristoph Hellwig 			break;
5083848c0bcSChristoph Hellwig 		list_move_tail(&io->log_sibling, &log->io_end_ios);
5093848c0bcSChristoph Hellwig 	}
5103848c0bcSChristoph Hellwig }
5113848c0bcSChristoph Hellwig 
512*3bddb7f8SSong Liu static void __r5l_stripe_write_finished(struct r5l_io_unit *io);
513f6bed0efSShaohua Li static void r5l_log_endio(struct bio *bio)
514f6bed0efSShaohua Li {
515f6bed0efSShaohua Li 	struct r5l_io_unit *io = bio->bi_private;
516*3bddb7f8SSong Liu 	struct r5l_io_unit *io_deferred;
517f6bed0efSShaohua Li 	struct r5l_log *log = io->log;
518509ffec7SChristoph Hellwig 	unsigned long flags;
519f6bed0efSShaohua Li 
5206e74a9cfSShaohua Li 	if (bio->bi_error)
5216e74a9cfSShaohua Li 		md_error(log->rdev->mddev, log->rdev);
5226e74a9cfSShaohua Li 
523f6bed0efSShaohua Li 	bio_put(bio);
524e8deb638SChristoph Hellwig 	mempool_free(io->meta_page, log->meta_pool);
525f6bed0efSShaohua Li 
526509ffec7SChristoph Hellwig 	spin_lock_irqsave(&log->io_list_lock, flags);
527509ffec7SChristoph Hellwig 	__r5l_set_io_unit_state(io, IO_UNIT_IO_END);
52856fef7c6SChristoph Hellwig 	if (log->need_cache_flush)
5293848c0bcSChristoph Hellwig 		r5l_move_to_end_ios(log);
53056fef7c6SChristoph Hellwig 	else
53156fef7c6SChristoph Hellwig 		r5l_log_run_stripes(log);
532*3bddb7f8SSong Liu 	if (!list_empty(&log->running_ios)) {
533*3bddb7f8SSong Liu 		/*
534*3bddb7f8SSong Liu 		 * FLUSH/FUA io_unit is deferred because of ordering, now we
535*3bddb7f8SSong Liu 		 * can dispatch it
536*3bddb7f8SSong Liu 		 */
537*3bddb7f8SSong Liu 		io_deferred = list_first_entry(&log->running_ios,
538*3bddb7f8SSong Liu 					       struct r5l_io_unit, log_sibling);
539*3bddb7f8SSong Liu 		if (io_deferred->io_deferred)
540*3bddb7f8SSong Liu 			schedule_work(&log->deferred_io_work);
541*3bddb7f8SSong Liu 	}
542*3bddb7f8SSong Liu 
543509ffec7SChristoph Hellwig 	spin_unlock_irqrestore(&log->io_list_lock, flags);
544509ffec7SChristoph Hellwig 
54556fef7c6SChristoph Hellwig 	if (log->need_cache_flush)
546f6bed0efSShaohua Li 		md_wakeup_thread(log->rdev->mddev->thread);
547*3bddb7f8SSong Liu 
548*3bddb7f8SSong Liu 	if (io->has_null_flush) {
549*3bddb7f8SSong Liu 		struct bio *bi;
550*3bddb7f8SSong Liu 
551*3bddb7f8SSong Liu 		WARN_ON(bio_list_empty(&io->flush_barriers));
552*3bddb7f8SSong Liu 		while ((bi = bio_list_pop(&io->flush_barriers)) != NULL) {
553*3bddb7f8SSong Liu 			bio_endio(bi);
554*3bddb7f8SSong Liu 			atomic_dec(&io->pending_stripe);
555*3bddb7f8SSong Liu 		}
556*3bddb7f8SSong Liu 		if (atomic_read(&io->pending_stripe) == 0)
557*3bddb7f8SSong Liu 			__r5l_stripe_write_finished(io);
558*3bddb7f8SSong Liu 	}
559*3bddb7f8SSong Liu }
560*3bddb7f8SSong Liu 
561*3bddb7f8SSong Liu static void r5l_do_submit_io(struct r5l_log *log, struct r5l_io_unit *io)
562*3bddb7f8SSong Liu {
563*3bddb7f8SSong Liu 	unsigned long flags;
564*3bddb7f8SSong Liu 
565*3bddb7f8SSong Liu 	spin_lock_irqsave(&log->io_list_lock, flags);
566*3bddb7f8SSong Liu 	__r5l_set_io_unit_state(io, IO_UNIT_IO_START);
567*3bddb7f8SSong Liu 	spin_unlock_irqrestore(&log->io_list_lock, flags);
568*3bddb7f8SSong Liu 
569*3bddb7f8SSong Liu 	if (io->has_flush)
570*3bddb7f8SSong Liu 		bio_set_op_attrs(io->current_bio, REQ_OP_WRITE, WRITE_FLUSH);
571*3bddb7f8SSong Liu 	if (io->has_fua)
572*3bddb7f8SSong Liu 		bio_set_op_attrs(io->current_bio, REQ_OP_WRITE, WRITE_FUA);
573*3bddb7f8SSong Liu 	submit_bio(io->current_bio);
574*3bddb7f8SSong Liu 
575*3bddb7f8SSong Liu 	if (!io->split_bio)
576*3bddb7f8SSong Liu 		return;
577*3bddb7f8SSong Liu 
578*3bddb7f8SSong Liu 	if (io->has_flush)
579*3bddb7f8SSong Liu 		bio_set_op_attrs(io->split_bio, REQ_OP_WRITE, WRITE_FLUSH);
580*3bddb7f8SSong Liu 	if (io->has_fua)
581*3bddb7f8SSong Liu 		bio_set_op_attrs(io->split_bio, REQ_OP_WRITE, WRITE_FUA);
582*3bddb7f8SSong Liu 	submit_bio(io->split_bio);
583*3bddb7f8SSong Liu }
584*3bddb7f8SSong Liu 
585*3bddb7f8SSong Liu /* deferred io_unit will be dispatched here */
586*3bddb7f8SSong Liu static void r5l_submit_io_async(struct work_struct *work)
587*3bddb7f8SSong Liu {
588*3bddb7f8SSong Liu 	struct r5l_log *log = container_of(work, struct r5l_log,
589*3bddb7f8SSong Liu 					   deferred_io_work);
590*3bddb7f8SSong Liu 	struct r5l_io_unit *io = NULL;
591*3bddb7f8SSong Liu 	unsigned long flags;
592*3bddb7f8SSong Liu 
593*3bddb7f8SSong Liu 	spin_lock_irqsave(&log->io_list_lock, flags);
594*3bddb7f8SSong Liu 	if (!list_empty(&log->running_ios)) {
595*3bddb7f8SSong Liu 		io = list_first_entry(&log->running_ios, struct r5l_io_unit,
596*3bddb7f8SSong Liu 				      log_sibling);
597*3bddb7f8SSong Liu 		if (!io->io_deferred)
598*3bddb7f8SSong Liu 			io = NULL;
599*3bddb7f8SSong Liu 		else
600*3bddb7f8SSong Liu 			io->io_deferred = 0;
601*3bddb7f8SSong Liu 	}
602*3bddb7f8SSong Liu 	spin_unlock_irqrestore(&log->io_list_lock, flags);
603*3bddb7f8SSong Liu 	if (io)
604*3bddb7f8SSong Liu 		r5l_do_submit_io(log, io);
605f6bed0efSShaohua Li }
606f6bed0efSShaohua Li 
607f6bed0efSShaohua Li static void r5l_submit_current_io(struct r5l_log *log)
608f6bed0efSShaohua Li {
609f6bed0efSShaohua Li 	struct r5l_io_unit *io = log->current_io;
610*3bddb7f8SSong Liu 	struct bio *bio;
611f6bed0efSShaohua Li 	struct r5l_meta_block *block;
612509ffec7SChristoph Hellwig 	unsigned long flags;
613f6bed0efSShaohua Li 	u32 crc;
614*3bddb7f8SSong Liu 	bool do_submit = true;
615f6bed0efSShaohua Li 
616f6bed0efSShaohua Li 	if (!io)
617f6bed0efSShaohua Li 		return;
618f6bed0efSShaohua Li 
619f6bed0efSShaohua Li 	block = page_address(io->meta_page);
620f6bed0efSShaohua Li 	block->meta_size = cpu_to_le32(io->meta_offset);
6215cb2fbd6SShaohua Li 	crc = crc32c_le(log->uuid_checksum, block, PAGE_SIZE);
622f6bed0efSShaohua Li 	block->checksum = cpu_to_le32(crc);
623*3bddb7f8SSong Liu 	bio = io->current_bio;
624f6bed0efSShaohua Li 
625f6bed0efSShaohua Li 	log->current_io = NULL;
626509ffec7SChristoph Hellwig 	spin_lock_irqsave(&log->io_list_lock, flags);
627*3bddb7f8SSong Liu 	if (io->has_flush || io->has_fua) {
628*3bddb7f8SSong Liu 		if (io != list_first_entry(&log->running_ios,
629*3bddb7f8SSong Liu 					   struct r5l_io_unit, log_sibling)) {
630*3bddb7f8SSong Liu 			io->io_deferred = 1;
631*3bddb7f8SSong Liu 			do_submit = false;
632*3bddb7f8SSong Liu 		}
633*3bddb7f8SSong Liu 	}
634509ffec7SChristoph Hellwig 	spin_unlock_irqrestore(&log->io_list_lock, flags);
635*3bddb7f8SSong Liu 	if (do_submit)
636*3bddb7f8SSong Liu 		r5l_do_submit_io(log, io);
637f6bed0efSShaohua Li }
638f6bed0efSShaohua Li 
6396143e2ceSChristoph Hellwig static struct bio *r5l_bio_alloc(struct r5l_log *log)
640b349feb3SChristoph Hellwig {
641c38d29b3SChristoph Hellwig 	struct bio *bio = bio_alloc_bioset(GFP_NOIO, BIO_MAX_PAGES, log->bs);
642b349feb3SChristoph Hellwig 
643796a5cf0SMike Christie 	bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
644b349feb3SChristoph Hellwig 	bio->bi_bdev = log->rdev->bdev;
6451e932a37SChristoph Hellwig 	bio->bi_iter.bi_sector = log->rdev->data_offset + log->log_start;
646b349feb3SChristoph Hellwig 
647b349feb3SChristoph Hellwig 	return bio;
648b349feb3SChristoph Hellwig }
649b349feb3SChristoph Hellwig 
650c1b99198SChristoph Hellwig static void r5_reserve_log_entry(struct r5l_log *log, struct r5l_io_unit *io)
651c1b99198SChristoph Hellwig {
652c1b99198SChristoph Hellwig 	log->log_start = r5l_ring_add(log, log->log_start, BLOCK_SECTORS);
653c1b99198SChristoph Hellwig 
654a39f7afdSSong Liu 	r5c_update_log_state(log);
655c1b99198SChristoph Hellwig 	/*
656c1b99198SChristoph Hellwig 	 * If we filled up the log device start from the beginning again,
657c1b99198SChristoph Hellwig 	 * which will require a new bio.
658c1b99198SChristoph Hellwig 	 *
659c1b99198SChristoph Hellwig 	 * Note: for this to work properly the log size needs to me a multiple
660c1b99198SChristoph Hellwig 	 * of BLOCK_SECTORS.
661c1b99198SChristoph Hellwig 	 */
662c1b99198SChristoph Hellwig 	if (log->log_start == 0)
6636143e2ceSChristoph Hellwig 		io->need_split_bio = true;
664c1b99198SChristoph Hellwig 
665c1b99198SChristoph Hellwig 	io->log_end = log->log_start;
666c1b99198SChristoph Hellwig }
667c1b99198SChristoph Hellwig 
668f6bed0efSShaohua Li static struct r5l_io_unit *r5l_new_meta(struct r5l_log *log)
669f6bed0efSShaohua Li {
670f6bed0efSShaohua Li 	struct r5l_io_unit *io;
671f6bed0efSShaohua Li 	struct r5l_meta_block *block;
672f6bed0efSShaohua Li 
6735036c390SChristoph Hellwig 	io = mempool_alloc(log->io_pool, GFP_ATOMIC);
6745036c390SChristoph Hellwig 	if (!io)
6755036c390SChristoph Hellwig 		return NULL;
6765036c390SChristoph Hellwig 	memset(io, 0, sizeof(*io));
6775036c390SChristoph Hellwig 
67851039cd0SChristoph Hellwig 	io->log = log;
67951039cd0SChristoph Hellwig 	INIT_LIST_HEAD(&io->log_sibling);
68051039cd0SChristoph Hellwig 	INIT_LIST_HEAD(&io->stripe_list);
681*3bddb7f8SSong Liu 	bio_list_init(&io->flush_barriers);
68251039cd0SChristoph Hellwig 	io->state = IO_UNIT_RUNNING;
683f6bed0efSShaohua Li 
684e8deb638SChristoph Hellwig 	io->meta_page = mempool_alloc(log->meta_pool, GFP_NOIO);
685f6bed0efSShaohua Li 	block = page_address(io->meta_page);
686e8deb638SChristoph Hellwig 	clear_page(block);
687f6bed0efSShaohua Li 	block->magic = cpu_to_le32(R5LOG_MAGIC);
688f6bed0efSShaohua Li 	block->version = R5LOG_VERSION;
689f6bed0efSShaohua Li 	block->seq = cpu_to_le64(log->seq);
690f6bed0efSShaohua Li 	block->position = cpu_to_le64(log->log_start);
691f6bed0efSShaohua Li 
692f6bed0efSShaohua Li 	io->log_start = log->log_start;
693f6bed0efSShaohua Li 	io->meta_offset = sizeof(struct r5l_meta_block);
6942b8ef16eSChristoph Hellwig 	io->seq = log->seq++;
695f6bed0efSShaohua Li 
6966143e2ceSChristoph Hellwig 	io->current_bio = r5l_bio_alloc(log);
6976143e2ceSChristoph Hellwig 	io->current_bio->bi_end_io = r5l_log_endio;
6986143e2ceSChristoph Hellwig 	io->current_bio->bi_private = io;
699b349feb3SChristoph Hellwig 	bio_add_page(io->current_bio, io->meta_page, PAGE_SIZE, 0);
700f6bed0efSShaohua Li 
701c1b99198SChristoph Hellwig 	r5_reserve_log_entry(log, io);
702f6bed0efSShaohua Li 
703f6bed0efSShaohua Li 	spin_lock_irq(&log->io_list_lock);
704f6bed0efSShaohua Li 	list_add_tail(&io->log_sibling, &log->running_ios);
705f6bed0efSShaohua Li 	spin_unlock_irq(&log->io_list_lock);
706f6bed0efSShaohua Li 
707f6bed0efSShaohua Li 	return io;
708f6bed0efSShaohua Li }
709f6bed0efSShaohua Li 
710f6bed0efSShaohua Li static int r5l_get_meta(struct r5l_log *log, unsigned int payload_size)
711f6bed0efSShaohua Li {
71222581f58SChristoph Hellwig 	if (log->current_io &&
71322581f58SChristoph Hellwig 	    log->current_io->meta_offset + payload_size > PAGE_SIZE)
714f6bed0efSShaohua Li 		r5l_submit_current_io(log);
715f6bed0efSShaohua Li 
7165036c390SChristoph Hellwig 	if (!log->current_io) {
717f6bed0efSShaohua Li 		log->current_io = r5l_new_meta(log);
7185036c390SChristoph Hellwig 		if (!log->current_io)
7195036c390SChristoph Hellwig 			return -ENOMEM;
7205036c390SChristoph Hellwig 	}
7215036c390SChristoph Hellwig 
722f6bed0efSShaohua Li 	return 0;
723f6bed0efSShaohua Li }
724f6bed0efSShaohua Li 
725f6bed0efSShaohua Li static void r5l_append_payload_meta(struct r5l_log *log, u16 type,
726f6bed0efSShaohua Li 				    sector_t location,
727f6bed0efSShaohua Li 				    u32 checksum1, u32 checksum2,
728f6bed0efSShaohua Li 				    bool checksum2_valid)
729f6bed0efSShaohua Li {
730f6bed0efSShaohua Li 	struct r5l_io_unit *io = log->current_io;
731f6bed0efSShaohua Li 	struct r5l_payload_data_parity *payload;
732f6bed0efSShaohua Li 
733f6bed0efSShaohua Li 	payload = page_address(io->meta_page) + io->meta_offset;
734f6bed0efSShaohua Li 	payload->header.type = cpu_to_le16(type);
735f6bed0efSShaohua Li 	payload->header.flags = cpu_to_le16(0);
736f6bed0efSShaohua Li 	payload->size = cpu_to_le32((1 + !!checksum2_valid) <<
737f6bed0efSShaohua Li 				    (PAGE_SHIFT - 9));
738f6bed0efSShaohua Li 	payload->location = cpu_to_le64(location);
739f6bed0efSShaohua Li 	payload->checksum[0] = cpu_to_le32(checksum1);
740f6bed0efSShaohua Li 	if (checksum2_valid)
741f6bed0efSShaohua Li 		payload->checksum[1] = cpu_to_le32(checksum2);
742f6bed0efSShaohua Li 
743f6bed0efSShaohua Li 	io->meta_offset += sizeof(struct r5l_payload_data_parity) +
744f6bed0efSShaohua Li 		sizeof(__le32) * (1 + !!checksum2_valid);
745f6bed0efSShaohua Li }
746f6bed0efSShaohua Li 
747f6bed0efSShaohua Li static void r5l_append_payload_page(struct r5l_log *log, struct page *page)
748f6bed0efSShaohua Li {
749f6bed0efSShaohua Li 	struct r5l_io_unit *io = log->current_io;
750f6bed0efSShaohua Li 
7516143e2ceSChristoph Hellwig 	if (io->need_split_bio) {
752*3bddb7f8SSong Liu 		BUG_ON(io->split_bio);
753*3bddb7f8SSong Liu 		io->split_bio = io->current_bio;
7546143e2ceSChristoph Hellwig 		io->current_bio = r5l_bio_alloc(log);
755*3bddb7f8SSong Liu 		bio_chain(io->current_bio, io->split_bio);
756*3bddb7f8SSong Liu 		io->need_split_bio = false;
757f6bed0efSShaohua Li 	}
758f6bed0efSShaohua Li 
7596143e2ceSChristoph Hellwig 	if (!bio_add_page(io->current_bio, page, PAGE_SIZE, 0))
7606143e2ceSChristoph Hellwig 		BUG();
7616143e2ceSChristoph Hellwig 
762c1b99198SChristoph Hellwig 	r5_reserve_log_entry(log, io);
763f6bed0efSShaohua Li }
764f6bed0efSShaohua Li 
7655036c390SChristoph Hellwig static int r5l_log_stripe(struct r5l_log *log, struct stripe_head *sh,
766f6bed0efSShaohua Li 			   int data_pages, int parity_pages)
767f6bed0efSShaohua Li {
768f6bed0efSShaohua Li 	int i;
769f6bed0efSShaohua Li 	int meta_size;
7705036c390SChristoph Hellwig 	int ret;
771f6bed0efSShaohua Li 	struct r5l_io_unit *io;
772f6bed0efSShaohua Li 
773f6bed0efSShaohua Li 	meta_size =
774f6bed0efSShaohua Li 		((sizeof(struct r5l_payload_data_parity) + sizeof(__le32))
775f6bed0efSShaohua Li 		 * data_pages) +
776f6bed0efSShaohua Li 		sizeof(struct r5l_payload_data_parity) +
777f6bed0efSShaohua Li 		sizeof(__le32) * parity_pages;
778f6bed0efSShaohua Li 
7795036c390SChristoph Hellwig 	ret = r5l_get_meta(log, meta_size);
7805036c390SChristoph Hellwig 	if (ret)
7815036c390SChristoph Hellwig 		return ret;
7825036c390SChristoph Hellwig 
783f6bed0efSShaohua Li 	io = log->current_io;
784f6bed0efSShaohua Li 
785*3bddb7f8SSong Liu 	if (test_and_clear_bit(STRIPE_R5C_PREFLUSH, &sh->state))
786*3bddb7f8SSong Liu 		io->has_flush = 1;
787*3bddb7f8SSong Liu 
788f6bed0efSShaohua Li 	for (i = 0; i < sh->disks; i++) {
7891e6d690bSSong Liu 		if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
7901e6d690bSSong Liu 		    test_bit(R5_InJournal, &sh->dev[i].flags))
791f6bed0efSShaohua Li 			continue;
792f6bed0efSShaohua Li 		if (i == sh->pd_idx || i == sh->qd_idx)
793f6bed0efSShaohua Li 			continue;
794*3bddb7f8SSong Liu 		if (test_bit(R5_WantFUA, &sh->dev[i].flags) &&
795*3bddb7f8SSong Liu 		    log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_BACK) {
796*3bddb7f8SSong Liu 			io->has_fua = 1;
797*3bddb7f8SSong Liu 			/*
798*3bddb7f8SSong Liu 			 * we need to flush journal to make sure recovery can
799*3bddb7f8SSong Liu 			 * reach the data with fua flag
800*3bddb7f8SSong Liu 			 */
801*3bddb7f8SSong Liu 			io->has_flush = 1;
802*3bddb7f8SSong Liu 		}
803f6bed0efSShaohua Li 		r5l_append_payload_meta(log, R5LOG_PAYLOAD_DATA,
804f6bed0efSShaohua Li 					raid5_compute_blocknr(sh, i, 0),
805f6bed0efSShaohua Li 					sh->dev[i].log_checksum, 0, false);
806f6bed0efSShaohua Li 		r5l_append_payload_page(log, sh->dev[i].page);
807f6bed0efSShaohua Li 	}
808f6bed0efSShaohua Li 
8092ded3703SSong Liu 	if (parity_pages == 2) {
810f6bed0efSShaohua Li 		r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
811f6bed0efSShaohua Li 					sh->sector, sh->dev[sh->pd_idx].log_checksum,
812f6bed0efSShaohua Li 					sh->dev[sh->qd_idx].log_checksum, true);
813f6bed0efSShaohua Li 		r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
814f6bed0efSShaohua Li 		r5l_append_payload_page(log, sh->dev[sh->qd_idx].page);
8152ded3703SSong Liu 	} else if (parity_pages == 1) {
816f6bed0efSShaohua Li 		r5l_append_payload_meta(log, R5LOG_PAYLOAD_PARITY,
817f6bed0efSShaohua Li 					sh->sector, sh->dev[sh->pd_idx].log_checksum,
818f6bed0efSShaohua Li 					0, false);
819f6bed0efSShaohua Li 		r5l_append_payload_page(log, sh->dev[sh->pd_idx].page);
8202ded3703SSong Liu 	} else  /* Just writing data, not parity, in caching phase */
8212ded3703SSong Liu 		BUG_ON(parity_pages != 0);
822f6bed0efSShaohua Li 
823f6bed0efSShaohua Li 	list_add_tail(&sh->log_list, &io->stripe_list);
824f6bed0efSShaohua Li 	atomic_inc(&io->pending_stripe);
825f6bed0efSShaohua Li 	sh->log_io = io;
8265036c390SChristoph Hellwig 
827a39f7afdSSong Liu 	if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
828a39f7afdSSong Liu 		return 0;
829a39f7afdSSong Liu 
830a39f7afdSSong Liu 	if (sh->log_start == MaxSector) {
831a39f7afdSSong Liu 		BUG_ON(!list_empty(&sh->r5c));
832a39f7afdSSong Liu 		sh->log_start = io->log_start;
833a39f7afdSSong Liu 		spin_lock_irq(&log->stripe_in_journal_lock);
834a39f7afdSSong Liu 		list_add_tail(&sh->r5c,
835a39f7afdSSong Liu 			      &log->stripe_in_journal_list);
836a39f7afdSSong Liu 		spin_unlock_irq(&log->stripe_in_journal_lock);
837a39f7afdSSong Liu 		atomic_inc(&log->stripe_in_journal_count);
838a39f7afdSSong Liu 	}
8395036c390SChristoph Hellwig 	return 0;
840f6bed0efSShaohua Li }
841f6bed0efSShaohua Li 
842a39f7afdSSong Liu /* add stripe to no_space_stripes, and then wake up reclaim */
843a39f7afdSSong Liu static inline void r5l_add_no_space_stripe(struct r5l_log *log,
844a39f7afdSSong Liu 					   struct stripe_head *sh)
845a39f7afdSSong Liu {
846a39f7afdSSong Liu 	spin_lock(&log->no_space_stripes_lock);
847a39f7afdSSong Liu 	list_add_tail(&sh->log_list, &log->no_space_stripes);
848a39f7afdSSong Liu 	spin_unlock(&log->no_space_stripes_lock);
849a39f7afdSSong Liu }
850a39f7afdSSong Liu 
851f6bed0efSShaohua Li /*
852f6bed0efSShaohua Li  * running in raid5d, where reclaim could wait for raid5d too (when it flushes
853f6bed0efSShaohua Li  * data from log to raid disks), so we shouldn't wait for reclaim here
854f6bed0efSShaohua Li  */
855f6bed0efSShaohua Li int r5l_write_stripe(struct r5l_log *log, struct stripe_head *sh)
856f6bed0efSShaohua Li {
857a39f7afdSSong Liu 	struct r5conf *conf = sh->raid_conf;
858f6bed0efSShaohua Li 	int write_disks = 0;
859f6bed0efSShaohua Li 	int data_pages, parity_pages;
860f6bed0efSShaohua Li 	int reserve;
861f6bed0efSShaohua Li 	int i;
8625036c390SChristoph Hellwig 	int ret = 0;
863a39f7afdSSong Liu 	bool wake_reclaim = false;
864f6bed0efSShaohua Li 
865f6bed0efSShaohua Li 	if (!log)
866f6bed0efSShaohua Li 		return -EAGAIN;
867f6bed0efSShaohua Li 	/* Don't support stripe batch */
868f6bed0efSShaohua Li 	if (sh->log_io || !test_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags) ||
869f6bed0efSShaohua Li 	    test_bit(STRIPE_SYNCING, &sh->state)) {
870f6bed0efSShaohua Li 		/* the stripe is written to log, we start writing it to raid */
871f6bed0efSShaohua Li 		clear_bit(STRIPE_LOG_TRAPPED, &sh->state);
872f6bed0efSShaohua Li 		return -EAGAIN;
873f6bed0efSShaohua Li 	}
874f6bed0efSShaohua Li 
8752ded3703SSong Liu 	WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
8762ded3703SSong Liu 
877f6bed0efSShaohua Li 	for (i = 0; i < sh->disks; i++) {
878f6bed0efSShaohua Li 		void *addr;
879f6bed0efSShaohua Li 
8801e6d690bSSong Liu 		if (!test_bit(R5_Wantwrite, &sh->dev[i].flags) ||
8811e6d690bSSong Liu 		    test_bit(R5_InJournal, &sh->dev[i].flags))
882f6bed0efSShaohua Li 			continue;
8831e6d690bSSong Liu 
884f6bed0efSShaohua Li 		write_disks++;
885f6bed0efSShaohua Li 		/* checksum is already calculated in last run */
886f6bed0efSShaohua Li 		if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
887f6bed0efSShaohua Li 			continue;
888f6bed0efSShaohua Li 		addr = kmap_atomic(sh->dev[i].page);
8895cb2fbd6SShaohua Li 		sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
890f6bed0efSShaohua Li 						    addr, PAGE_SIZE);
891f6bed0efSShaohua Li 		kunmap_atomic(addr);
892f6bed0efSShaohua Li 	}
893f6bed0efSShaohua Li 	parity_pages = 1 + !!(sh->qd_idx >= 0);
894f6bed0efSShaohua Li 	data_pages = write_disks - parity_pages;
895f6bed0efSShaohua Li 
896f6bed0efSShaohua Li 	set_bit(STRIPE_LOG_TRAPPED, &sh->state);
897253f9fd4SShaohua Li 	/*
898253f9fd4SShaohua Li 	 * The stripe must enter state machine again to finish the write, so
899253f9fd4SShaohua Li 	 * don't delay.
900253f9fd4SShaohua Li 	 */
901253f9fd4SShaohua Li 	clear_bit(STRIPE_DELAYED, &sh->state);
902f6bed0efSShaohua Li 	atomic_inc(&sh->count);
903f6bed0efSShaohua Li 
904f6bed0efSShaohua Li 	mutex_lock(&log->io_mutex);
905f6bed0efSShaohua Li 	/* meta + data */
906f6bed0efSShaohua Li 	reserve = (1 + write_disks) << (PAGE_SHIFT - 9);
907f6bed0efSShaohua Li 
908a39f7afdSSong Liu 	if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
909a39f7afdSSong Liu 		if (!r5l_has_free_space(log, reserve)) {
910a39f7afdSSong Liu 			r5l_add_no_space_stripe(log, sh);
911a39f7afdSSong Liu 			wake_reclaim = true;
9125036c390SChristoph Hellwig 		} else {
9135036c390SChristoph Hellwig 			ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
9145036c390SChristoph Hellwig 			if (ret) {
9155036c390SChristoph Hellwig 				spin_lock_irq(&log->io_list_lock);
916a39f7afdSSong Liu 				list_add_tail(&sh->log_list,
917a39f7afdSSong Liu 					      &log->no_mem_stripes);
9185036c390SChristoph Hellwig 				spin_unlock_irq(&log->io_list_lock);
919f6bed0efSShaohua Li 			}
9205036c390SChristoph Hellwig 		}
921a39f7afdSSong Liu 	} else {  /* R5C_JOURNAL_MODE_WRITE_BACK */
922a39f7afdSSong Liu 		/*
923a39f7afdSSong Liu 		 * log space critical, do not process stripes that are
924a39f7afdSSong Liu 		 * not in cache yet (sh->log_start == MaxSector).
925a39f7afdSSong Liu 		 */
926a39f7afdSSong Liu 		if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
927a39f7afdSSong Liu 		    sh->log_start == MaxSector) {
928a39f7afdSSong Liu 			r5l_add_no_space_stripe(log, sh);
929a39f7afdSSong Liu 			wake_reclaim = true;
930a39f7afdSSong Liu 			reserve = 0;
931a39f7afdSSong Liu 		} else if (!r5l_has_free_space(log, reserve)) {
932a39f7afdSSong Liu 			if (sh->log_start == log->last_checkpoint)
933a39f7afdSSong Liu 				BUG();
934a39f7afdSSong Liu 			else
935a39f7afdSSong Liu 				r5l_add_no_space_stripe(log, sh);
936a39f7afdSSong Liu 		} else {
937a39f7afdSSong Liu 			ret = r5l_log_stripe(log, sh, data_pages, parity_pages);
938a39f7afdSSong Liu 			if (ret) {
939a39f7afdSSong Liu 				spin_lock_irq(&log->io_list_lock);
940a39f7afdSSong Liu 				list_add_tail(&sh->log_list,
941a39f7afdSSong Liu 					      &log->no_mem_stripes);
942a39f7afdSSong Liu 				spin_unlock_irq(&log->io_list_lock);
943a39f7afdSSong Liu 			}
944a39f7afdSSong Liu 		}
945a39f7afdSSong Liu 	}
946f6bed0efSShaohua Li 
9475036c390SChristoph Hellwig 	mutex_unlock(&log->io_mutex);
948a39f7afdSSong Liu 	if (wake_reclaim)
949a39f7afdSSong Liu 		r5l_wake_reclaim(log, reserve);
950f6bed0efSShaohua Li 	return 0;
951f6bed0efSShaohua Li }
952f6bed0efSShaohua Li 
953f6bed0efSShaohua Li void r5l_write_stripe_run(struct r5l_log *log)
954f6bed0efSShaohua Li {
955f6bed0efSShaohua Li 	if (!log)
956f6bed0efSShaohua Li 		return;
957f6bed0efSShaohua Li 	mutex_lock(&log->io_mutex);
958f6bed0efSShaohua Li 	r5l_submit_current_io(log);
959f6bed0efSShaohua Li 	mutex_unlock(&log->io_mutex);
960f6bed0efSShaohua Li }
961f6bed0efSShaohua Li 
962828cbe98SShaohua Li int r5l_handle_flush_request(struct r5l_log *log, struct bio *bio)
963828cbe98SShaohua Li {
964828cbe98SShaohua Li 	if (!log)
965828cbe98SShaohua Li 		return -ENODEV;
966*3bddb7f8SSong Liu 
967*3bddb7f8SSong Liu 	if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH) {
968828cbe98SShaohua Li 		/*
969*3bddb7f8SSong Liu 		 * in write through (journal only)
970*3bddb7f8SSong Liu 		 * we flush log disk cache first, then write stripe data to
971*3bddb7f8SSong Liu 		 * raid disks. So if bio is finished, the log disk cache is
972*3bddb7f8SSong Liu 		 * flushed already. The recovery guarantees we can recovery
973*3bddb7f8SSong Liu 		 * the bio from log disk, so we don't need to flush again
974828cbe98SShaohua Li 		 */
975828cbe98SShaohua Li 		if (bio->bi_iter.bi_size == 0) {
976828cbe98SShaohua Li 			bio_endio(bio);
977828cbe98SShaohua Li 			return 0;
978828cbe98SShaohua Li 		}
9791eff9d32SJens Axboe 		bio->bi_opf &= ~REQ_PREFLUSH;
980*3bddb7f8SSong Liu 	} else {
981*3bddb7f8SSong Liu 		/* write back (with cache) */
982*3bddb7f8SSong Liu 		if (bio->bi_iter.bi_size == 0) {
983*3bddb7f8SSong Liu 			mutex_lock(&log->io_mutex);
984*3bddb7f8SSong Liu 			r5l_get_meta(log, 0);
985*3bddb7f8SSong Liu 			bio_list_add(&log->current_io->flush_barriers, bio);
986*3bddb7f8SSong Liu 			log->current_io->has_flush = 1;
987*3bddb7f8SSong Liu 			log->current_io->has_null_flush = 1;
988*3bddb7f8SSong Liu 			atomic_inc(&log->current_io->pending_stripe);
989*3bddb7f8SSong Liu 			r5l_submit_current_io(log);
990*3bddb7f8SSong Liu 			mutex_unlock(&log->io_mutex);
991*3bddb7f8SSong Liu 			return 0;
992*3bddb7f8SSong Liu 		}
993*3bddb7f8SSong Liu 	}
994828cbe98SShaohua Li 	return -EAGAIN;
995828cbe98SShaohua Li }
996828cbe98SShaohua Li 
997f6bed0efSShaohua Li /* This will run after log space is reclaimed */
998f6bed0efSShaohua Li static void r5l_run_no_space_stripes(struct r5l_log *log)
999f6bed0efSShaohua Li {
1000f6bed0efSShaohua Li 	struct stripe_head *sh;
1001f6bed0efSShaohua Li 
1002f6bed0efSShaohua Li 	spin_lock(&log->no_space_stripes_lock);
1003f6bed0efSShaohua Li 	while (!list_empty(&log->no_space_stripes)) {
1004f6bed0efSShaohua Li 		sh = list_first_entry(&log->no_space_stripes,
1005f6bed0efSShaohua Li 				      struct stripe_head, log_list);
1006f6bed0efSShaohua Li 		list_del_init(&sh->log_list);
1007f6bed0efSShaohua Li 		set_bit(STRIPE_HANDLE, &sh->state);
1008f6bed0efSShaohua Li 		raid5_release_stripe(sh);
1009f6bed0efSShaohua Li 	}
1010f6bed0efSShaohua Li 	spin_unlock(&log->no_space_stripes_lock);
1011f6bed0efSShaohua Li }
1012f6bed0efSShaohua Li 
1013a39f7afdSSong Liu /*
1014a39f7afdSSong Liu  * calculate new last_checkpoint
1015a39f7afdSSong Liu  * for write through mode, returns log->next_checkpoint
1016a39f7afdSSong Liu  * for write back, returns log_start of first sh in stripe_in_journal_list
1017a39f7afdSSong Liu  */
1018a39f7afdSSong Liu static sector_t r5c_calculate_new_cp(struct r5conf *conf)
1019a39f7afdSSong Liu {
1020a39f7afdSSong Liu 	struct stripe_head *sh;
1021a39f7afdSSong Liu 	struct r5l_log *log = conf->log;
1022a39f7afdSSong Liu 	sector_t new_cp;
1023a39f7afdSSong Liu 	unsigned long flags;
1024a39f7afdSSong Liu 
1025a39f7afdSSong Liu 	if (log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
1026a39f7afdSSong Liu 		return log->next_checkpoint;
1027a39f7afdSSong Liu 
1028a39f7afdSSong Liu 	spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
1029a39f7afdSSong Liu 	if (list_empty(&conf->log->stripe_in_journal_list)) {
1030a39f7afdSSong Liu 		/* all stripes flushed */
1031a39f7afdSSong Liu 		spin_unlock(&log->stripe_in_journal_lock);
1032a39f7afdSSong Liu 		return log->next_checkpoint;
1033a39f7afdSSong Liu 	}
1034a39f7afdSSong Liu 	sh = list_first_entry(&conf->log->stripe_in_journal_list,
1035a39f7afdSSong Liu 			      struct stripe_head, r5c);
1036a39f7afdSSong Liu 	new_cp = sh->log_start;
1037a39f7afdSSong Liu 	spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
1038a39f7afdSSong Liu 	return new_cp;
1039a39f7afdSSong Liu }
1040a39f7afdSSong Liu 
104117036461SChristoph Hellwig static sector_t r5l_reclaimable_space(struct r5l_log *log)
104217036461SChristoph Hellwig {
1043a39f7afdSSong Liu 	struct r5conf *conf = log->rdev->mddev->private;
1044a39f7afdSSong Liu 
104517036461SChristoph Hellwig 	return r5l_ring_distance(log, log->last_checkpoint,
1046a39f7afdSSong Liu 				 r5c_calculate_new_cp(conf));
104717036461SChristoph Hellwig }
104817036461SChristoph Hellwig 
10495036c390SChristoph Hellwig static void r5l_run_no_mem_stripe(struct r5l_log *log)
10505036c390SChristoph Hellwig {
10515036c390SChristoph Hellwig 	struct stripe_head *sh;
10525036c390SChristoph Hellwig 
10535036c390SChristoph Hellwig 	assert_spin_locked(&log->io_list_lock);
10545036c390SChristoph Hellwig 
10555036c390SChristoph Hellwig 	if (!list_empty(&log->no_mem_stripes)) {
10565036c390SChristoph Hellwig 		sh = list_first_entry(&log->no_mem_stripes,
10575036c390SChristoph Hellwig 				      struct stripe_head, log_list);
10585036c390SChristoph Hellwig 		list_del_init(&sh->log_list);
10595036c390SChristoph Hellwig 		set_bit(STRIPE_HANDLE, &sh->state);
10605036c390SChristoph Hellwig 		raid5_release_stripe(sh);
10615036c390SChristoph Hellwig 	}
10625036c390SChristoph Hellwig }
10635036c390SChristoph Hellwig 
106404732f74SChristoph Hellwig static bool r5l_complete_finished_ios(struct r5l_log *log)
106517036461SChristoph Hellwig {
106617036461SChristoph Hellwig 	struct r5l_io_unit *io, *next;
106717036461SChristoph Hellwig 	bool found = false;
106817036461SChristoph Hellwig 
106917036461SChristoph Hellwig 	assert_spin_locked(&log->io_list_lock);
107017036461SChristoph Hellwig 
107104732f74SChristoph Hellwig 	list_for_each_entry_safe(io, next, &log->finished_ios, log_sibling) {
107217036461SChristoph Hellwig 		/* don't change list order */
107317036461SChristoph Hellwig 		if (io->state < IO_UNIT_STRIPE_END)
107417036461SChristoph Hellwig 			break;
107517036461SChristoph Hellwig 
107617036461SChristoph Hellwig 		log->next_checkpoint = io->log_start;
107717036461SChristoph Hellwig 		log->next_cp_seq = io->seq;
107817036461SChristoph Hellwig 
107917036461SChristoph Hellwig 		list_del(&io->log_sibling);
10805036c390SChristoph Hellwig 		mempool_free(io, log->io_pool);
10815036c390SChristoph Hellwig 		r5l_run_no_mem_stripe(log);
108217036461SChristoph Hellwig 
108317036461SChristoph Hellwig 		found = true;
108417036461SChristoph Hellwig 	}
108517036461SChristoph Hellwig 
108617036461SChristoph Hellwig 	return found;
108717036461SChristoph Hellwig }
108817036461SChristoph Hellwig 
1089509ffec7SChristoph Hellwig static void __r5l_stripe_write_finished(struct r5l_io_unit *io)
1090509ffec7SChristoph Hellwig {
1091509ffec7SChristoph Hellwig 	struct r5l_log *log = io->log;
1092a39f7afdSSong Liu 	struct r5conf *conf = log->rdev->mddev->private;
1093509ffec7SChristoph Hellwig 	unsigned long flags;
1094509ffec7SChristoph Hellwig 
1095509ffec7SChristoph Hellwig 	spin_lock_irqsave(&log->io_list_lock, flags);
1096509ffec7SChristoph Hellwig 	__r5l_set_io_unit_state(io, IO_UNIT_STRIPE_END);
109717036461SChristoph Hellwig 
109804732f74SChristoph Hellwig 	if (!r5l_complete_finished_ios(log)) {
109985f2f9a4SShaohua Li 		spin_unlock_irqrestore(&log->io_list_lock, flags);
110085f2f9a4SShaohua Li 		return;
110185f2f9a4SShaohua Li 	}
1102509ffec7SChristoph Hellwig 
1103a39f7afdSSong Liu 	if (r5l_reclaimable_space(log) > log->max_free_space ||
1104a39f7afdSSong Liu 	    test_bit(R5C_LOG_TIGHT, &conf->cache_state))
1105509ffec7SChristoph Hellwig 		r5l_wake_reclaim(log, 0);
1106509ffec7SChristoph Hellwig 
1107509ffec7SChristoph Hellwig 	spin_unlock_irqrestore(&log->io_list_lock, flags);
1108509ffec7SChristoph Hellwig 	wake_up(&log->iounit_wait);
1109509ffec7SChristoph Hellwig }
1110509ffec7SChristoph Hellwig 
11110576b1c6SShaohua Li void r5l_stripe_write_finished(struct stripe_head *sh)
11120576b1c6SShaohua Li {
11130576b1c6SShaohua Li 	struct r5l_io_unit *io;
11140576b1c6SShaohua Li 
11150576b1c6SShaohua Li 	io = sh->log_io;
11160576b1c6SShaohua Li 	sh->log_io = NULL;
11170576b1c6SShaohua Li 
1118509ffec7SChristoph Hellwig 	if (io && atomic_dec_and_test(&io->pending_stripe))
1119509ffec7SChristoph Hellwig 		__r5l_stripe_write_finished(io);
11200576b1c6SShaohua Li }
11210576b1c6SShaohua Li 
1122a8c34f91SShaohua Li static void r5l_log_flush_endio(struct bio *bio)
1123a8c34f91SShaohua Li {
1124a8c34f91SShaohua Li 	struct r5l_log *log = container_of(bio, struct r5l_log,
1125a8c34f91SShaohua Li 		flush_bio);
1126a8c34f91SShaohua Li 	unsigned long flags;
1127a8c34f91SShaohua Li 	struct r5l_io_unit *io;
1128a8c34f91SShaohua Li 
11296e74a9cfSShaohua Li 	if (bio->bi_error)
11306e74a9cfSShaohua Li 		md_error(log->rdev->mddev, log->rdev);
11316e74a9cfSShaohua Li 
1132a8c34f91SShaohua Li 	spin_lock_irqsave(&log->io_list_lock, flags);
1133d8858f43SChristoph Hellwig 	list_for_each_entry(io, &log->flushing_ios, log_sibling)
1134d8858f43SChristoph Hellwig 		r5l_io_run_stripes(io);
113504732f74SChristoph Hellwig 	list_splice_tail_init(&log->flushing_ios, &log->finished_ios);
1136a8c34f91SShaohua Li 	spin_unlock_irqrestore(&log->io_list_lock, flags);
1137a8c34f91SShaohua Li }
1138a8c34f91SShaohua Li 
11390576b1c6SShaohua Li /*
11400576b1c6SShaohua Li  * Starting dispatch IO to raid.
11410576b1c6SShaohua Li  * io_unit(meta) consists of a log. There is one situation we want to avoid. A
11420576b1c6SShaohua Li  * broken meta in the middle of a log causes recovery can't find meta at the
11430576b1c6SShaohua Li  * head of log. If operations require meta at the head persistent in log, we
11440576b1c6SShaohua Li  * must make sure meta before it persistent in log too. A case is:
11450576b1c6SShaohua Li  *
11460576b1c6SShaohua Li  * stripe data/parity is in log, we start write stripe to raid disks. stripe
11470576b1c6SShaohua Li  * data/parity must be persistent in log before we do the write to raid disks.
11480576b1c6SShaohua Li  *
11490576b1c6SShaohua Li  * The solution is we restrictly maintain io_unit list order. In this case, we
11500576b1c6SShaohua Li  * only write stripes of an io_unit to raid disks till the io_unit is the first
11510576b1c6SShaohua Li  * one whose data/parity is in log.
11520576b1c6SShaohua Li  */
11530576b1c6SShaohua Li void r5l_flush_stripe_to_raid(struct r5l_log *log)
11540576b1c6SShaohua Li {
1155a8c34f91SShaohua Li 	bool do_flush;
115656fef7c6SChristoph Hellwig 
115756fef7c6SChristoph Hellwig 	if (!log || !log->need_cache_flush)
11580576b1c6SShaohua Li 		return;
11590576b1c6SShaohua Li 
1160a8c34f91SShaohua Li 	spin_lock_irq(&log->io_list_lock);
1161a8c34f91SShaohua Li 	/* flush bio is running */
1162a8c34f91SShaohua Li 	if (!list_empty(&log->flushing_ios)) {
1163a8c34f91SShaohua Li 		spin_unlock_irq(&log->io_list_lock);
11640576b1c6SShaohua Li 		return;
11650576b1c6SShaohua Li 	}
1166a8c34f91SShaohua Li 	list_splice_tail_init(&log->io_end_ios, &log->flushing_ios);
1167a8c34f91SShaohua Li 	do_flush = !list_empty(&log->flushing_ios);
11680576b1c6SShaohua Li 	spin_unlock_irq(&log->io_list_lock);
1169a8c34f91SShaohua Li 
1170a8c34f91SShaohua Li 	if (!do_flush)
1171a8c34f91SShaohua Li 		return;
1172a8c34f91SShaohua Li 	bio_reset(&log->flush_bio);
1173a8c34f91SShaohua Li 	log->flush_bio.bi_bdev = log->rdev->bdev;
1174a8c34f91SShaohua Li 	log->flush_bio.bi_end_io = r5l_log_flush_endio;
1175796a5cf0SMike Christie 	bio_set_op_attrs(&log->flush_bio, REQ_OP_WRITE, WRITE_FLUSH);
11764e49ea4aSMike Christie 	submit_bio(&log->flush_bio);
11770576b1c6SShaohua Li }
11780576b1c6SShaohua Li 
11790576b1c6SShaohua Li static void r5l_write_super(struct r5l_log *log, sector_t cp);
11804b482044SShaohua Li static void r5l_write_super_and_discard_space(struct r5l_log *log,
11814b482044SShaohua Li 	sector_t end)
11824b482044SShaohua Li {
11834b482044SShaohua Li 	struct block_device *bdev = log->rdev->bdev;
11844b482044SShaohua Li 	struct mddev *mddev;
11854b482044SShaohua Li 
11864b482044SShaohua Li 	r5l_write_super(log, end);
11874b482044SShaohua Li 
11884b482044SShaohua Li 	if (!blk_queue_discard(bdev_get_queue(bdev)))
11894b482044SShaohua Li 		return;
11904b482044SShaohua Li 
11914b482044SShaohua Li 	mddev = log->rdev->mddev;
11924b482044SShaohua Li 	/*
11938e018c21SShaohua Li 	 * Discard could zero data, so before discard we must make sure
11948e018c21SShaohua Li 	 * superblock is updated to new log tail. Updating superblock (either
11958e018c21SShaohua Li 	 * directly call md_update_sb() or depend on md thread) must hold
11968e018c21SShaohua Li 	 * reconfig mutex. On the other hand, raid5_quiesce is called with
11978e018c21SShaohua Li 	 * reconfig_mutex hold. The first step of raid5_quiesce() is waitting
11988e018c21SShaohua Li 	 * for all IO finish, hence waitting for reclaim thread, while reclaim
11998e018c21SShaohua Li 	 * thread is calling this function and waitting for reconfig mutex. So
12008e018c21SShaohua Li 	 * there is a deadlock. We workaround this issue with a trylock.
12018e018c21SShaohua Li 	 * FIXME: we could miss discard if we can't take reconfig mutex
12024b482044SShaohua Li 	 */
120385ad1d13SGuoqing Jiang 	set_mask_bits(&mddev->flags, 0,
120485ad1d13SGuoqing Jiang 		BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
12058e018c21SShaohua Li 	if (!mddev_trylock(mddev))
12068e018c21SShaohua Li 		return;
12074b482044SShaohua Li 	md_update_sb(mddev, 1);
12088e018c21SShaohua Li 	mddev_unlock(mddev);
12094b482044SShaohua Li 
12106e74a9cfSShaohua Li 	/* discard IO error really doesn't matter, ignore it */
12114b482044SShaohua Li 	if (log->last_checkpoint < end) {
12124b482044SShaohua Li 		blkdev_issue_discard(bdev,
12134b482044SShaohua Li 				log->last_checkpoint + log->rdev->data_offset,
12144b482044SShaohua Li 				end - log->last_checkpoint, GFP_NOIO, 0);
12154b482044SShaohua Li 	} else {
12164b482044SShaohua Li 		blkdev_issue_discard(bdev,
12174b482044SShaohua Li 				log->last_checkpoint + log->rdev->data_offset,
12184b482044SShaohua Li 				log->device_size - log->last_checkpoint,
12194b482044SShaohua Li 				GFP_NOIO, 0);
12204b482044SShaohua Li 		blkdev_issue_discard(bdev, log->rdev->data_offset, end,
12214b482044SShaohua Li 				GFP_NOIO, 0);
12224b482044SShaohua Li 	}
12234b482044SShaohua Li }
12244b482044SShaohua Li 
1225a39f7afdSSong Liu /*
1226a39f7afdSSong Liu  * r5c_flush_stripe moves stripe from cached list to handle_list. When called,
1227a39f7afdSSong Liu  * the stripe must be on r5c_cached_full_stripes or r5c_cached_partial_stripes.
1228a39f7afdSSong Liu  *
1229a39f7afdSSong Liu  * must hold conf->device_lock
1230a39f7afdSSong Liu  */
1231a39f7afdSSong Liu static void r5c_flush_stripe(struct r5conf *conf, struct stripe_head *sh)
1232a39f7afdSSong Liu {
1233a39f7afdSSong Liu 	BUG_ON(list_empty(&sh->lru));
1234a39f7afdSSong Liu 	BUG_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
1235a39f7afdSSong Liu 	BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
1236a39f7afdSSong Liu 
1237a39f7afdSSong Liu 	/*
1238a39f7afdSSong Liu 	 * The stripe is not ON_RELEASE_LIST, so it is safe to call
1239a39f7afdSSong Liu 	 * raid5_release_stripe() while holding conf->device_lock
1240a39f7afdSSong Liu 	 */
1241a39f7afdSSong Liu 	BUG_ON(test_bit(STRIPE_ON_RELEASE_LIST, &sh->state));
1242a39f7afdSSong Liu 	assert_spin_locked(&conf->device_lock);
1243a39f7afdSSong Liu 
1244a39f7afdSSong Liu 	list_del_init(&sh->lru);
1245a39f7afdSSong Liu 	atomic_inc(&sh->count);
1246a39f7afdSSong Liu 
1247a39f7afdSSong Liu 	set_bit(STRIPE_HANDLE, &sh->state);
1248a39f7afdSSong Liu 	atomic_inc(&conf->active_stripes);
1249a39f7afdSSong Liu 	r5c_make_stripe_write_out(sh);
1250a39f7afdSSong Liu 
1251a39f7afdSSong Liu 	if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1252a39f7afdSSong Liu 		atomic_inc(&conf->preread_active_stripes);
1253a39f7afdSSong Liu 	raid5_release_stripe(sh);
1254a39f7afdSSong Liu }
1255a39f7afdSSong Liu 
1256a39f7afdSSong Liu /*
1257a39f7afdSSong Liu  * if num == 0, flush all full stripes
1258a39f7afdSSong Liu  * if num > 0, flush all full stripes. If less than num full stripes are
1259a39f7afdSSong Liu  *             flushed, flush some partial stripes until totally num stripes are
1260a39f7afdSSong Liu  *             flushed or there is no more cached stripes.
1261a39f7afdSSong Liu  */
1262a39f7afdSSong Liu void r5c_flush_cache(struct r5conf *conf, int num)
1263a39f7afdSSong Liu {
1264a39f7afdSSong Liu 	int count;
1265a39f7afdSSong Liu 	struct stripe_head *sh, *next;
1266a39f7afdSSong Liu 
1267a39f7afdSSong Liu 	assert_spin_locked(&conf->device_lock);
1268a39f7afdSSong Liu 	if (!conf->log)
1269a39f7afdSSong Liu 		return;
1270a39f7afdSSong Liu 
1271a39f7afdSSong Liu 	count = 0;
1272a39f7afdSSong Liu 	list_for_each_entry_safe(sh, next, &conf->r5c_full_stripe_list, lru) {
1273a39f7afdSSong Liu 		r5c_flush_stripe(conf, sh);
1274a39f7afdSSong Liu 		count++;
1275a39f7afdSSong Liu 	}
1276a39f7afdSSong Liu 
1277a39f7afdSSong Liu 	if (count >= num)
1278a39f7afdSSong Liu 		return;
1279a39f7afdSSong Liu 	list_for_each_entry_safe(sh, next,
1280a39f7afdSSong Liu 				 &conf->r5c_partial_stripe_list, lru) {
1281a39f7afdSSong Liu 		r5c_flush_stripe(conf, sh);
1282a39f7afdSSong Liu 		if (++count >= num)
1283a39f7afdSSong Liu 			break;
1284a39f7afdSSong Liu 	}
1285a39f7afdSSong Liu }
1286a39f7afdSSong Liu 
1287a39f7afdSSong Liu static void r5c_do_reclaim(struct r5conf *conf)
1288a39f7afdSSong Liu {
1289a39f7afdSSong Liu 	struct r5l_log *log = conf->log;
1290a39f7afdSSong Liu 	struct stripe_head *sh;
1291a39f7afdSSong Liu 	int count = 0;
1292a39f7afdSSong Liu 	unsigned long flags;
1293a39f7afdSSong Liu 	int total_cached;
1294a39f7afdSSong Liu 	int stripes_to_flush;
1295a39f7afdSSong Liu 
1296a39f7afdSSong Liu 	if (!r5c_is_writeback(log))
1297a39f7afdSSong Liu 		return;
1298a39f7afdSSong Liu 
1299a39f7afdSSong Liu 	total_cached = atomic_read(&conf->r5c_cached_partial_stripes) +
1300a39f7afdSSong Liu 		atomic_read(&conf->r5c_cached_full_stripes);
1301a39f7afdSSong Liu 
1302a39f7afdSSong Liu 	if (total_cached > conf->min_nr_stripes * 3 / 4 ||
1303a39f7afdSSong Liu 	    atomic_read(&conf->empty_inactive_list_nr) > 0)
1304a39f7afdSSong Liu 		/*
1305a39f7afdSSong Liu 		 * if stripe cache pressure high, flush all full stripes and
1306a39f7afdSSong Liu 		 * some partial stripes
1307a39f7afdSSong Liu 		 */
1308a39f7afdSSong Liu 		stripes_to_flush = R5C_RECLAIM_STRIPE_GROUP;
1309a39f7afdSSong Liu 	else if (total_cached > conf->min_nr_stripes * 1 / 2 ||
1310a39f7afdSSong Liu 		 atomic_read(&conf->r5c_cached_full_stripes) >
1311a39f7afdSSong Liu 		 R5C_FULL_STRIPE_FLUSH_BATCH)
1312a39f7afdSSong Liu 		/*
1313a39f7afdSSong Liu 		 * if stripe cache pressure moderate, or if there is many full
1314a39f7afdSSong Liu 		 * stripes,flush all full stripes
1315a39f7afdSSong Liu 		 */
1316a39f7afdSSong Liu 		stripes_to_flush = 0;
1317a39f7afdSSong Liu 	else
1318a39f7afdSSong Liu 		/* no need to flush */
1319a39f7afdSSong Liu 		stripes_to_flush = -1;
1320a39f7afdSSong Liu 
1321a39f7afdSSong Liu 	if (stripes_to_flush >= 0) {
1322a39f7afdSSong Liu 		spin_lock_irqsave(&conf->device_lock, flags);
1323a39f7afdSSong Liu 		r5c_flush_cache(conf, stripes_to_flush);
1324a39f7afdSSong Liu 		spin_unlock_irqrestore(&conf->device_lock, flags);
1325a39f7afdSSong Liu 	}
1326a39f7afdSSong Liu 
1327a39f7afdSSong Liu 	/* if log space is tight, flush stripes on stripe_in_journal_list */
1328a39f7afdSSong Liu 	if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) {
1329a39f7afdSSong Liu 		spin_lock_irqsave(&log->stripe_in_journal_lock, flags);
1330a39f7afdSSong Liu 		spin_lock(&conf->device_lock);
1331a39f7afdSSong Liu 		list_for_each_entry(sh, &log->stripe_in_journal_list, r5c) {
1332a39f7afdSSong Liu 			/*
1333a39f7afdSSong Liu 			 * stripes on stripe_in_journal_list could be in any
1334a39f7afdSSong Liu 			 * state of the stripe_cache state machine. In this
1335a39f7afdSSong Liu 			 * case, we only want to flush stripe on
1336a39f7afdSSong Liu 			 * r5c_cached_full/partial_stripes. The following
1337a39f7afdSSong Liu 			 * condition makes sure the stripe is on one of the
1338a39f7afdSSong Liu 			 * two lists.
1339a39f7afdSSong Liu 			 */
1340a39f7afdSSong Liu 			if (!list_empty(&sh->lru) &&
1341a39f7afdSSong Liu 			    !test_bit(STRIPE_HANDLE, &sh->state) &&
1342a39f7afdSSong Liu 			    atomic_read(&sh->count) == 0) {
1343a39f7afdSSong Liu 				r5c_flush_stripe(conf, sh);
1344a39f7afdSSong Liu 			}
1345a39f7afdSSong Liu 			if (count++ >= R5C_RECLAIM_STRIPE_GROUP)
1346a39f7afdSSong Liu 				break;
1347a39f7afdSSong Liu 		}
1348a39f7afdSSong Liu 		spin_unlock(&conf->device_lock);
1349a39f7afdSSong Liu 		spin_unlock_irqrestore(&log->stripe_in_journal_lock, flags);
1350a39f7afdSSong Liu 	}
1351a39f7afdSSong Liu 	md_wakeup_thread(conf->mddev->thread);
1352a39f7afdSSong Liu }
1353a39f7afdSSong Liu 
13540576b1c6SShaohua Li static void r5l_do_reclaim(struct r5l_log *log)
13550576b1c6SShaohua Li {
1356a39f7afdSSong Liu 	struct r5conf *conf = log->rdev->mddev->private;
13570576b1c6SShaohua Li 	sector_t reclaim_target = xchg(&log->reclaim_target, 0);
135817036461SChristoph Hellwig 	sector_t reclaimable;
135917036461SChristoph Hellwig 	sector_t next_checkpoint;
1360a39f7afdSSong Liu 	bool write_super;
13610576b1c6SShaohua Li 
13620576b1c6SShaohua Li 	spin_lock_irq(&log->io_list_lock);
1363a39f7afdSSong Liu 	write_super = r5l_reclaimable_space(log) > log->max_free_space ||
1364a39f7afdSSong Liu 		reclaim_target != 0 || !list_empty(&log->no_space_stripes);
13650576b1c6SShaohua Li 	/*
13660576b1c6SShaohua Li 	 * move proper io_unit to reclaim list. We should not change the order.
13670576b1c6SShaohua Li 	 * reclaimable/unreclaimable io_unit can be mixed in the list, we
13680576b1c6SShaohua Li 	 * shouldn't reuse space of an unreclaimable io_unit
13690576b1c6SShaohua Li 	 */
13700576b1c6SShaohua Li 	while (1) {
137117036461SChristoph Hellwig 		reclaimable = r5l_reclaimable_space(log);
137217036461SChristoph Hellwig 		if (reclaimable >= reclaim_target ||
13730576b1c6SShaohua Li 		    (list_empty(&log->running_ios) &&
13740576b1c6SShaohua Li 		     list_empty(&log->io_end_ios) &&
1375a8c34f91SShaohua Li 		     list_empty(&log->flushing_ios) &&
137604732f74SChristoph Hellwig 		     list_empty(&log->finished_ios)))
13770576b1c6SShaohua Li 			break;
13780576b1c6SShaohua Li 
137917036461SChristoph Hellwig 		md_wakeup_thread(log->rdev->mddev->thread);
138017036461SChristoph Hellwig 		wait_event_lock_irq(log->iounit_wait,
138117036461SChristoph Hellwig 				    r5l_reclaimable_space(log) > reclaimable,
138217036461SChristoph Hellwig 				    log->io_list_lock);
13830576b1c6SShaohua Li 	}
138417036461SChristoph Hellwig 
1385a39f7afdSSong Liu 	next_checkpoint = r5c_calculate_new_cp(conf);
13860576b1c6SShaohua Li 	spin_unlock_irq(&log->io_list_lock);
13870576b1c6SShaohua Li 
138817036461SChristoph Hellwig 	BUG_ON(reclaimable < 0);
1389a39f7afdSSong Liu 
1390a39f7afdSSong Liu 	if (reclaimable == 0 || !write_super)
13910576b1c6SShaohua Li 		return;
13920576b1c6SShaohua Li 
13930576b1c6SShaohua Li 	/*
13940576b1c6SShaohua Li 	 * write_super will flush cache of each raid disk. We must write super
13950576b1c6SShaohua Li 	 * here, because the log area might be reused soon and we don't want to
13960576b1c6SShaohua Li 	 * confuse recovery
13970576b1c6SShaohua Li 	 */
13984b482044SShaohua Li 	r5l_write_super_and_discard_space(log, next_checkpoint);
13990576b1c6SShaohua Li 
14000576b1c6SShaohua Li 	mutex_lock(&log->io_mutex);
140117036461SChristoph Hellwig 	log->last_checkpoint = next_checkpoint;
1402a39f7afdSSong Liu 	r5c_update_log_state(log);
14030576b1c6SShaohua Li 	mutex_unlock(&log->io_mutex);
14040576b1c6SShaohua Li 
140517036461SChristoph Hellwig 	r5l_run_no_space_stripes(log);
14060576b1c6SShaohua Li }
14070576b1c6SShaohua Li 
14080576b1c6SShaohua Li static void r5l_reclaim_thread(struct md_thread *thread)
14090576b1c6SShaohua Li {
14100576b1c6SShaohua Li 	struct mddev *mddev = thread->mddev;
14110576b1c6SShaohua Li 	struct r5conf *conf = mddev->private;
14120576b1c6SShaohua Li 	struct r5l_log *log = conf->log;
14130576b1c6SShaohua Li 
14140576b1c6SShaohua Li 	if (!log)
14150576b1c6SShaohua Li 		return;
1416a39f7afdSSong Liu 	r5c_do_reclaim(conf);
14170576b1c6SShaohua Li 	r5l_do_reclaim(log);
14180576b1c6SShaohua Li }
14190576b1c6SShaohua Li 
1420a39f7afdSSong Liu void r5l_wake_reclaim(struct r5l_log *log, sector_t space)
1421f6bed0efSShaohua Li {
14220576b1c6SShaohua Li 	unsigned long target;
14230576b1c6SShaohua Li 	unsigned long new = (unsigned long)space; /* overflow in theory */
14240576b1c6SShaohua Li 
1425a39f7afdSSong Liu 	if (!log)
1426a39f7afdSSong Liu 		return;
14270576b1c6SShaohua Li 	do {
14280576b1c6SShaohua Li 		target = log->reclaim_target;
14290576b1c6SShaohua Li 		if (new < target)
14300576b1c6SShaohua Li 			return;
14310576b1c6SShaohua Li 	} while (cmpxchg(&log->reclaim_target, target, new) != target);
14320576b1c6SShaohua Li 	md_wakeup_thread(log->reclaim_thread);
1433f6bed0efSShaohua Li }
1434f6bed0efSShaohua Li 
1435e6c033f7SShaohua Li void r5l_quiesce(struct r5l_log *log, int state)
1436e6c033f7SShaohua Li {
14374b482044SShaohua Li 	struct mddev *mddev;
1438e6c033f7SShaohua Li 	if (!log || state == 2)
1439e6c033f7SShaohua Li 		return;
1440e6c033f7SShaohua Li 	if (state == 0) {
144116a43f6aSShaohua Li 		/*
144216a43f6aSShaohua Li 		 * This is a special case for hotadd. In suspend, the array has
144316a43f6aSShaohua Li 		 * no journal. In resume, journal is initialized as well as the
144416a43f6aSShaohua Li 		 * reclaim thread.
144516a43f6aSShaohua Li 		 */
144616a43f6aSShaohua Li 		if (log->reclaim_thread)
144716a43f6aSShaohua Li 			return;
1448e6c033f7SShaohua Li 		log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
1449e6c033f7SShaohua Li 					log->rdev->mddev, "reclaim");
1450a39f7afdSSong Liu 		log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
1451e6c033f7SShaohua Li 	} else if (state == 1) {
14524b482044SShaohua Li 		/* make sure r5l_write_super_and_discard_space exits */
14534b482044SShaohua Li 		mddev = log->rdev->mddev;
14544b482044SShaohua Li 		wake_up(&mddev->sb_wait);
1455a39f7afdSSong Liu 		r5l_wake_reclaim(log, MaxSector);
1456e6c033f7SShaohua Li 		md_unregister_thread(&log->reclaim_thread);
1457e6c033f7SShaohua Li 		r5l_do_reclaim(log);
1458e6c033f7SShaohua Li 	}
1459e6c033f7SShaohua Li }
1460e6c033f7SShaohua Li 
14616e74a9cfSShaohua Li bool r5l_log_disk_error(struct r5conf *conf)
14626e74a9cfSShaohua Li {
1463f6b6ec5cSShaohua Li 	struct r5l_log *log;
1464f6b6ec5cSShaohua Li 	bool ret;
14657dde2ad3SShaohua Li 	/* don't allow write if journal disk is missing */
1466f6b6ec5cSShaohua Li 	rcu_read_lock();
1467f6b6ec5cSShaohua Li 	log = rcu_dereference(conf->log);
1468f6b6ec5cSShaohua Li 
1469f6b6ec5cSShaohua Li 	if (!log)
1470f6b6ec5cSShaohua Li 		ret = test_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
1471f6b6ec5cSShaohua Li 	else
1472f6b6ec5cSShaohua Li 		ret = test_bit(Faulty, &log->rdev->flags);
1473f6b6ec5cSShaohua Li 	rcu_read_unlock();
1474f6b6ec5cSShaohua Li 	return ret;
14756e74a9cfSShaohua Li }
14766e74a9cfSShaohua Li 
1477355810d1SShaohua Li struct r5l_recovery_ctx {
1478355810d1SShaohua Li 	struct page *meta_page;		/* current meta */
1479355810d1SShaohua Li 	sector_t meta_total_blocks;	/* total size of current meta and data */
1480355810d1SShaohua Li 	sector_t pos;			/* recovery position */
1481355810d1SShaohua Li 	u64 seq;			/* recovery position seq */
1482b4c625c6SSong Liu 	int data_parity_stripes;	/* number of data_parity stripes */
1483b4c625c6SSong Liu 	int data_only_stripes;		/* number of data_only stripes */
1484b4c625c6SSong Liu 	struct list_head cached_list;
1485355810d1SShaohua Li };
1486355810d1SShaohua Li 
14879ed988f5SSong Liu static int r5l_recovery_read_meta_block(struct r5l_log *log,
1488355810d1SShaohua Li 					struct r5l_recovery_ctx *ctx)
1489355810d1SShaohua Li {
1490355810d1SShaohua Li 	struct page *page = ctx->meta_page;
1491355810d1SShaohua Li 	struct r5l_meta_block *mb;
1492355810d1SShaohua Li 	u32 crc, stored_crc;
1493355810d1SShaohua Li 
1494796a5cf0SMike Christie 	if (!sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page, REQ_OP_READ, 0,
1495796a5cf0SMike Christie 			  false))
1496355810d1SShaohua Li 		return -EIO;
1497355810d1SShaohua Li 
1498355810d1SShaohua Li 	mb = page_address(page);
1499355810d1SShaohua Li 	stored_crc = le32_to_cpu(mb->checksum);
1500355810d1SShaohua Li 	mb->checksum = 0;
1501355810d1SShaohua Li 
1502355810d1SShaohua Li 	if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
1503355810d1SShaohua Li 	    le64_to_cpu(mb->seq) != ctx->seq ||
1504355810d1SShaohua Li 	    mb->version != R5LOG_VERSION ||
1505355810d1SShaohua Li 	    le64_to_cpu(mb->position) != ctx->pos)
1506355810d1SShaohua Li 		return -EINVAL;
1507355810d1SShaohua Li 
15085cb2fbd6SShaohua Li 	crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
1509355810d1SShaohua Li 	if (stored_crc != crc)
1510355810d1SShaohua Li 		return -EINVAL;
1511355810d1SShaohua Li 
1512355810d1SShaohua Li 	if (le32_to_cpu(mb->meta_size) > PAGE_SIZE)
1513355810d1SShaohua Li 		return -EINVAL;
1514355810d1SShaohua Li 
1515355810d1SShaohua Li 	ctx->meta_total_blocks = BLOCK_SECTORS;
1516355810d1SShaohua Li 
1517355810d1SShaohua Li 	return 0;
1518355810d1SShaohua Li }
1519355810d1SShaohua Li 
15209ed988f5SSong Liu static void
15219ed988f5SSong Liu r5l_recovery_create_empty_meta_block(struct r5l_log *log,
15229ed988f5SSong Liu 				     struct page *page,
15239ed988f5SSong Liu 				     sector_t pos, u64 seq)
1524355810d1SShaohua Li {
1525355810d1SShaohua Li 	struct r5l_meta_block *mb;
1526355810d1SShaohua Li 	u32 crc;
1527355810d1SShaohua Li 
1528355810d1SShaohua Li 	mb = page_address(page);
15299ed988f5SSong Liu 	clear_page(mb);
1530355810d1SShaohua Li 	mb->magic = cpu_to_le32(R5LOG_MAGIC);
1531355810d1SShaohua Li 	mb->version = R5LOG_VERSION;
1532355810d1SShaohua Li 	mb->meta_size = cpu_to_le32(sizeof(struct r5l_meta_block));
1533355810d1SShaohua Li 	mb->seq = cpu_to_le64(seq);
1534355810d1SShaohua Li 	mb->position = cpu_to_le64(pos);
15355cb2fbd6SShaohua Li 	crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
1536355810d1SShaohua Li 	mb->checksum = cpu_to_le32(crc);
15379ed988f5SSong Liu }
1538355810d1SShaohua Li 
15399ed988f5SSong Liu static int r5l_log_write_empty_meta_block(struct r5l_log *log, sector_t pos,
15409ed988f5SSong Liu 					  u64 seq)
15419ed988f5SSong Liu {
15429ed988f5SSong Liu 	struct page *page;
15439ed988f5SSong Liu 
15449ed988f5SSong Liu 	page = alloc_page(GFP_KERNEL);
15459ed988f5SSong Liu 	if (!page)
15469ed988f5SSong Liu 		return -ENOMEM;
15479ed988f5SSong Liu 	r5l_recovery_create_empty_meta_block(log, page, pos, seq);
1548796a5cf0SMike Christie 	if (!sync_page_io(log->rdev, pos, PAGE_SIZE, page, REQ_OP_WRITE,
1549796a5cf0SMike Christie 			  WRITE_FUA, false)) {
1550355810d1SShaohua Li 		__free_page(page);
1551355810d1SShaohua Li 		return -EIO;
1552355810d1SShaohua Li 	}
1553355810d1SShaohua Li 	__free_page(page);
1554355810d1SShaohua Li 	return 0;
1555355810d1SShaohua Li }
1556355810d1SShaohua Li 
1557b4c625c6SSong Liu /*
1558b4c625c6SSong Liu  * r5l_recovery_load_data and r5l_recovery_load_parity uses flag R5_Wantwrite
1559b4c625c6SSong Liu  * to mark valid (potentially not flushed) data in the journal.
1560b4c625c6SSong Liu  *
1561b4c625c6SSong Liu  * We already verified checksum in r5l_recovery_verify_data_checksum_for_mb,
1562b4c625c6SSong Liu  * so there should not be any mismatch here.
1563b4c625c6SSong Liu  */
1564b4c625c6SSong Liu static void r5l_recovery_load_data(struct r5l_log *log,
1565b4c625c6SSong Liu 				   struct stripe_head *sh,
1566b4c625c6SSong Liu 				   struct r5l_recovery_ctx *ctx,
1567b4c625c6SSong Liu 				   struct r5l_payload_data_parity *payload,
1568b4c625c6SSong Liu 				   sector_t log_offset)
1569b4c625c6SSong Liu {
1570b4c625c6SSong Liu 	struct mddev *mddev = log->rdev->mddev;
1571b4c625c6SSong Liu 	struct r5conf *conf = mddev->private;
1572b4c625c6SSong Liu 	int dd_idx;
1573b4c625c6SSong Liu 
1574b4c625c6SSong Liu 	raid5_compute_sector(conf,
1575b4c625c6SSong Liu 			     le64_to_cpu(payload->location), 0,
1576b4c625c6SSong Liu 			     &dd_idx, sh);
1577b4c625c6SSong Liu 	sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1578b4c625c6SSong Liu 		     sh->dev[dd_idx].page, REQ_OP_READ, 0, false);
1579b4c625c6SSong Liu 	sh->dev[dd_idx].log_checksum =
1580b4c625c6SSong Liu 		le32_to_cpu(payload->checksum[0]);
1581b4c625c6SSong Liu 	ctx->meta_total_blocks += BLOCK_SECTORS;
1582b4c625c6SSong Liu 
1583b4c625c6SSong Liu 	set_bit(R5_Wantwrite, &sh->dev[dd_idx].flags);
1584b4c625c6SSong Liu 	set_bit(STRIPE_R5C_CACHING, &sh->state);
1585b4c625c6SSong Liu }
1586b4c625c6SSong Liu 
1587b4c625c6SSong Liu static void r5l_recovery_load_parity(struct r5l_log *log,
1588b4c625c6SSong Liu 				     struct stripe_head *sh,
1589b4c625c6SSong Liu 				     struct r5l_recovery_ctx *ctx,
1590b4c625c6SSong Liu 				     struct r5l_payload_data_parity *payload,
1591b4c625c6SSong Liu 				     sector_t log_offset)
1592b4c625c6SSong Liu {
1593b4c625c6SSong Liu 	struct mddev *mddev = log->rdev->mddev;
1594b4c625c6SSong Liu 	struct r5conf *conf = mddev->private;
1595b4c625c6SSong Liu 
1596b4c625c6SSong Liu 	ctx->meta_total_blocks += BLOCK_SECTORS * conf->max_degraded;
1597b4c625c6SSong Liu 	sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1598b4c625c6SSong Liu 		     sh->dev[sh->pd_idx].page, REQ_OP_READ, 0, false);
1599b4c625c6SSong Liu 	sh->dev[sh->pd_idx].log_checksum =
1600b4c625c6SSong Liu 		le32_to_cpu(payload->checksum[0]);
1601b4c625c6SSong Liu 	set_bit(R5_Wantwrite, &sh->dev[sh->pd_idx].flags);
1602b4c625c6SSong Liu 
1603b4c625c6SSong Liu 	if (sh->qd_idx >= 0) {
1604b4c625c6SSong Liu 		sync_page_io(log->rdev,
1605b4c625c6SSong Liu 			     r5l_ring_add(log, log_offset, BLOCK_SECTORS),
1606b4c625c6SSong Liu 			     PAGE_SIZE, sh->dev[sh->qd_idx].page,
1607b4c625c6SSong Liu 			     REQ_OP_READ, 0, false);
1608b4c625c6SSong Liu 		sh->dev[sh->qd_idx].log_checksum =
1609b4c625c6SSong Liu 			le32_to_cpu(payload->checksum[1]);
1610b4c625c6SSong Liu 		set_bit(R5_Wantwrite, &sh->dev[sh->qd_idx].flags);
1611b4c625c6SSong Liu 	}
1612b4c625c6SSong Liu 	clear_bit(STRIPE_R5C_CACHING, &sh->state);
1613b4c625c6SSong Liu }
1614b4c625c6SSong Liu 
1615b4c625c6SSong Liu static void r5l_recovery_reset_stripe(struct stripe_head *sh)
1616b4c625c6SSong Liu {
1617b4c625c6SSong Liu 	int i;
1618b4c625c6SSong Liu 
1619b4c625c6SSong Liu 	sh->state = 0;
1620b4c625c6SSong Liu 	sh->log_start = MaxSector;
1621b4c625c6SSong Liu 	for (i = sh->disks; i--; )
1622b4c625c6SSong Liu 		sh->dev[i].flags = 0;
1623b4c625c6SSong Liu }
1624b4c625c6SSong Liu 
1625b4c625c6SSong Liu static void
1626b4c625c6SSong Liu r5l_recovery_replay_one_stripe(struct r5conf *conf,
1627b4c625c6SSong Liu 			       struct stripe_head *sh,
1628b4c625c6SSong Liu 			       struct r5l_recovery_ctx *ctx)
1629b4c625c6SSong Liu {
1630b4c625c6SSong Liu 	struct md_rdev *rdev, *rrdev;
1631b4c625c6SSong Liu 	int disk_index;
1632b4c625c6SSong Liu 	int data_count = 0;
1633b4c625c6SSong Liu 
1634b4c625c6SSong Liu 	for (disk_index = 0; disk_index < sh->disks; disk_index++) {
1635b4c625c6SSong Liu 		if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
1636b4c625c6SSong Liu 			continue;
1637b4c625c6SSong Liu 		if (disk_index == sh->qd_idx || disk_index == sh->pd_idx)
1638b4c625c6SSong Liu 			continue;
1639b4c625c6SSong Liu 		data_count++;
1640b4c625c6SSong Liu 	}
1641b4c625c6SSong Liu 
1642b4c625c6SSong Liu 	/*
1643b4c625c6SSong Liu 	 * stripes that only have parity must have been flushed
1644b4c625c6SSong Liu 	 * before the crash that we are now recovering from, so
1645b4c625c6SSong Liu 	 * there is nothing more to recovery.
1646b4c625c6SSong Liu 	 */
1647b4c625c6SSong Liu 	if (data_count == 0)
1648b4c625c6SSong Liu 		goto out;
1649b4c625c6SSong Liu 
1650b4c625c6SSong Liu 	for (disk_index = 0; disk_index < sh->disks; disk_index++) {
1651b4c625c6SSong Liu 		if (!test_bit(R5_Wantwrite, &sh->dev[disk_index].flags))
1652b4c625c6SSong Liu 			continue;
1653b4c625c6SSong Liu 
1654b4c625c6SSong Liu 		/* in case device is broken */
1655b4c625c6SSong Liu 		rcu_read_lock();
1656b4c625c6SSong Liu 		rdev = rcu_dereference(conf->disks[disk_index].rdev);
1657b4c625c6SSong Liu 		if (rdev) {
1658b4c625c6SSong Liu 			atomic_inc(&rdev->nr_pending);
1659b4c625c6SSong Liu 			rcu_read_unlock();
1660b4c625c6SSong Liu 			sync_page_io(rdev, sh->sector, PAGE_SIZE,
1661b4c625c6SSong Liu 				     sh->dev[disk_index].page, REQ_OP_WRITE, 0,
1662b4c625c6SSong Liu 				     false);
1663b4c625c6SSong Liu 			rdev_dec_pending(rdev, rdev->mddev);
1664b4c625c6SSong Liu 			rcu_read_lock();
1665b4c625c6SSong Liu 		}
1666b4c625c6SSong Liu 		rrdev = rcu_dereference(conf->disks[disk_index].replacement);
1667b4c625c6SSong Liu 		if (rrdev) {
1668b4c625c6SSong Liu 			atomic_inc(&rrdev->nr_pending);
1669b4c625c6SSong Liu 			rcu_read_unlock();
1670b4c625c6SSong Liu 			sync_page_io(rrdev, sh->sector, PAGE_SIZE,
1671b4c625c6SSong Liu 				     sh->dev[disk_index].page, REQ_OP_WRITE, 0,
1672b4c625c6SSong Liu 				     false);
1673b4c625c6SSong Liu 			rdev_dec_pending(rrdev, rrdev->mddev);
1674b4c625c6SSong Liu 			rcu_read_lock();
1675b4c625c6SSong Liu 		}
1676b4c625c6SSong Liu 		rcu_read_unlock();
1677b4c625c6SSong Liu 	}
1678b4c625c6SSong Liu 	ctx->data_parity_stripes++;
1679b4c625c6SSong Liu out:
1680b4c625c6SSong Liu 	r5l_recovery_reset_stripe(sh);
1681b4c625c6SSong Liu }
1682b4c625c6SSong Liu 
1683b4c625c6SSong Liu static struct stripe_head *
1684b4c625c6SSong Liu r5c_recovery_alloc_stripe(struct r5conf *conf,
1685b4c625c6SSong Liu 			  struct list_head *recovery_list,
1686b4c625c6SSong Liu 			  sector_t stripe_sect,
1687b4c625c6SSong Liu 			  sector_t log_start)
1688b4c625c6SSong Liu {
1689b4c625c6SSong Liu 	struct stripe_head *sh;
1690b4c625c6SSong Liu 
1691b4c625c6SSong Liu 	sh = raid5_get_active_stripe(conf, stripe_sect, 0, 1, 0);
1692b4c625c6SSong Liu 	if (!sh)
1693b4c625c6SSong Liu 		return NULL;  /* no more stripe available */
1694b4c625c6SSong Liu 
1695b4c625c6SSong Liu 	r5l_recovery_reset_stripe(sh);
1696b4c625c6SSong Liu 	sh->log_start = log_start;
1697b4c625c6SSong Liu 
1698b4c625c6SSong Liu 	return sh;
1699b4c625c6SSong Liu }
1700b4c625c6SSong Liu 
1701b4c625c6SSong Liu static struct stripe_head *
1702b4c625c6SSong Liu r5c_recovery_lookup_stripe(struct list_head *list, sector_t sect)
1703b4c625c6SSong Liu {
1704b4c625c6SSong Liu 	struct stripe_head *sh;
1705b4c625c6SSong Liu 
1706b4c625c6SSong Liu 	list_for_each_entry(sh, list, lru)
1707b4c625c6SSong Liu 		if (sh->sector == sect)
1708b4c625c6SSong Liu 			return sh;
1709b4c625c6SSong Liu 	return NULL;
1710b4c625c6SSong Liu }
1711b4c625c6SSong Liu 
1712b4c625c6SSong Liu static void
1713b4c625c6SSong Liu r5c_recovery_drop_stripes(struct list_head *cached_stripe_list,
1714b4c625c6SSong Liu 			  struct r5l_recovery_ctx *ctx)
1715b4c625c6SSong Liu {
1716b4c625c6SSong Liu 	struct stripe_head *sh, *next;
1717b4c625c6SSong Liu 
1718b4c625c6SSong Liu 	list_for_each_entry_safe(sh, next, cached_stripe_list, lru) {
1719b4c625c6SSong Liu 		r5l_recovery_reset_stripe(sh);
1720b4c625c6SSong Liu 		list_del_init(&sh->lru);
1721b4c625c6SSong Liu 		raid5_release_stripe(sh);
1722b4c625c6SSong Liu 	}
1723b4c625c6SSong Liu }
1724b4c625c6SSong Liu 
1725b4c625c6SSong Liu static void
1726b4c625c6SSong Liu r5c_recovery_replay_stripes(struct list_head *cached_stripe_list,
1727b4c625c6SSong Liu 			    struct r5l_recovery_ctx *ctx)
1728b4c625c6SSong Liu {
1729b4c625c6SSong Liu 	struct stripe_head *sh, *next;
1730b4c625c6SSong Liu 
1731b4c625c6SSong Liu 	list_for_each_entry_safe(sh, next, cached_stripe_list, lru)
1732b4c625c6SSong Liu 		if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
1733b4c625c6SSong Liu 			r5l_recovery_replay_one_stripe(sh->raid_conf, sh, ctx);
1734b4c625c6SSong Liu 			list_del_init(&sh->lru);
1735b4c625c6SSong Liu 			raid5_release_stripe(sh);
1736b4c625c6SSong Liu 		}
1737b4c625c6SSong Liu }
1738b4c625c6SSong Liu 
1739b4c625c6SSong Liu /* if matches return 0; otherwise return -EINVAL */
1740b4c625c6SSong Liu static int
1741b4c625c6SSong Liu r5l_recovery_verify_data_checksum(struct r5l_log *log, struct page *page,
1742b4c625c6SSong Liu 				  sector_t log_offset, __le32 log_checksum)
1743b4c625c6SSong Liu {
1744b4c625c6SSong Liu 	void *addr;
1745b4c625c6SSong Liu 	u32 checksum;
1746b4c625c6SSong Liu 
1747b4c625c6SSong Liu 	sync_page_io(log->rdev, log_offset, PAGE_SIZE,
1748b4c625c6SSong Liu 		     page, REQ_OP_READ, 0, false);
1749b4c625c6SSong Liu 	addr = kmap_atomic(page);
1750b4c625c6SSong Liu 	checksum = crc32c_le(log->uuid_checksum, addr, PAGE_SIZE);
1751b4c625c6SSong Liu 	kunmap_atomic(addr);
1752b4c625c6SSong Liu 	return (le32_to_cpu(log_checksum) == checksum) ? 0 : -EINVAL;
1753b4c625c6SSong Liu }
1754b4c625c6SSong Liu 
1755b4c625c6SSong Liu /*
1756b4c625c6SSong Liu  * before loading data to stripe cache, we need verify checksum for all data,
1757b4c625c6SSong Liu  * if there is mismatch for any data page, we drop all data in the mata block
1758b4c625c6SSong Liu  */
1759b4c625c6SSong Liu static int
1760b4c625c6SSong Liu r5l_recovery_verify_data_checksum_for_mb(struct r5l_log *log,
1761b4c625c6SSong Liu 					 struct r5l_recovery_ctx *ctx)
1762b4c625c6SSong Liu {
1763b4c625c6SSong Liu 	struct mddev *mddev = log->rdev->mddev;
1764b4c625c6SSong Liu 	struct r5conf *conf = mddev->private;
1765b4c625c6SSong Liu 	struct r5l_meta_block *mb = page_address(ctx->meta_page);
1766b4c625c6SSong Liu 	sector_t mb_offset = sizeof(struct r5l_meta_block);
1767b4c625c6SSong Liu 	sector_t log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
1768b4c625c6SSong Liu 	struct page *page;
1769b4c625c6SSong Liu 	struct r5l_payload_data_parity *payload;
1770b4c625c6SSong Liu 
1771b4c625c6SSong Liu 	page = alloc_page(GFP_KERNEL);
1772b4c625c6SSong Liu 	if (!page)
1773b4c625c6SSong Liu 		return -ENOMEM;
1774b4c625c6SSong Liu 
1775b4c625c6SSong Liu 	while (mb_offset < le32_to_cpu(mb->meta_size)) {
1776b4c625c6SSong Liu 		payload = (void *)mb + mb_offset;
1777b4c625c6SSong Liu 
1778b4c625c6SSong Liu 		if (payload->header.type == R5LOG_PAYLOAD_DATA) {
1779b4c625c6SSong Liu 			if (r5l_recovery_verify_data_checksum(
1780b4c625c6SSong Liu 				    log, page, log_offset,
1781b4c625c6SSong Liu 				    payload->checksum[0]) < 0)
1782b4c625c6SSong Liu 				goto mismatch;
1783b4c625c6SSong Liu 		} else if (payload->header.type == R5LOG_PAYLOAD_PARITY) {
1784b4c625c6SSong Liu 			if (r5l_recovery_verify_data_checksum(
1785b4c625c6SSong Liu 				    log, page, log_offset,
1786b4c625c6SSong Liu 				    payload->checksum[0]) < 0)
1787b4c625c6SSong Liu 				goto mismatch;
1788b4c625c6SSong Liu 			if (conf->max_degraded == 2 && /* q for RAID 6 */
1789b4c625c6SSong Liu 			    r5l_recovery_verify_data_checksum(
1790b4c625c6SSong Liu 				    log, page,
1791b4c625c6SSong Liu 				    r5l_ring_add(log, log_offset,
1792b4c625c6SSong Liu 						 BLOCK_SECTORS),
1793b4c625c6SSong Liu 				    payload->checksum[1]) < 0)
1794b4c625c6SSong Liu 				goto mismatch;
1795b4c625c6SSong Liu 		} else /* not R5LOG_PAYLOAD_DATA or R5LOG_PAYLOAD_PARITY */
1796b4c625c6SSong Liu 			goto mismatch;
1797b4c625c6SSong Liu 
1798b4c625c6SSong Liu 		log_offset = r5l_ring_add(log, log_offset,
1799b4c625c6SSong Liu 					  le32_to_cpu(payload->size));
1800b4c625c6SSong Liu 
1801b4c625c6SSong Liu 		mb_offset += sizeof(struct r5l_payload_data_parity) +
1802b4c625c6SSong Liu 			sizeof(__le32) *
1803b4c625c6SSong Liu 			(le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
1804b4c625c6SSong Liu 	}
1805b4c625c6SSong Liu 
1806b4c625c6SSong Liu 	put_page(page);
1807b4c625c6SSong Liu 	return 0;
1808b4c625c6SSong Liu 
1809b4c625c6SSong Liu mismatch:
1810b4c625c6SSong Liu 	put_page(page);
1811b4c625c6SSong Liu 	return -EINVAL;
1812b4c625c6SSong Liu }
1813b4c625c6SSong Liu 
1814b4c625c6SSong Liu /*
1815b4c625c6SSong Liu  * Analyze all data/parity pages in one meta block
1816b4c625c6SSong Liu  * Returns:
1817b4c625c6SSong Liu  * 0 for success
1818b4c625c6SSong Liu  * -EINVAL for unknown playload type
1819b4c625c6SSong Liu  * -EAGAIN for checksum mismatch of data page
1820b4c625c6SSong Liu  * -ENOMEM for run out of memory (alloc_page failed or run out of stripes)
1821b4c625c6SSong Liu  */
1822b4c625c6SSong Liu static int
1823b4c625c6SSong Liu r5c_recovery_analyze_meta_block(struct r5l_log *log,
1824b4c625c6SSong Liu 				struct r5l_recovery_ctx *ctx,
1825b4c625c6SSong Liu 				struct list_head *cached_stripe_list)
1826b4c625c6SSong Liu {
1827b4c625c6SSong Liu 	struct mddev *mddev = log->rdev->mddev;
1828b4c625c6SSong Liu 	struct r5conf *conf = mddev->private;
1829b4c625c6SSong Liu 	struct r5l_meta_block *mb;
1830b4c625c6SSong Liu 	struct r5l_payload_data_parity *payload;
1831b4c625c6SSong Liu 	int mb_offset;
1832b4c625c6SSong Liu 	sector_t log_offset;
1833b4c625c6SSong Liu 	sector_t stripe_sect;
1834b4c625c6SSong Liu 	struct stripe_head *sh;
1835b4c625c6SSong Liu 	int ret;
1836b4c625c6SSong Liu 
1837b4c625c6SSong Liu 	/*
1838b4c625c6SSong Liu 	 * for mismatch in data blocks, we will drop all data in this mb, but
1839b4c625c6SSong Liu 	 * we will still read next mb for other data with FLUSH flag, as
1840b4c625c6SSong Liu 	 * io_unit could finish out of order.
1841b4c625c6SSong Liu 	 */
1842b4c625c6SSong Liu 	ret = r5l_recovery_verify_data_checksum_for_mb(log, ctx);
1843b4c625c6SSong Liu 	if (ret == -EINVAL)
1844b4c625c6SSong Liu 		return -EAGAIN;
1845b4c625c6SSong Liu 	else if (ret)
1846b4c625c6SSong Liu 		return ret;   /* -ENOMEM duo to alloc_page() failed */
1847b4c625c6SSong Liu 
1848b4c625c6SSong Liu 	mb = page_address(ctx->meta_page);
1849b4c625c6SSong Liu 	mb_offset = sizeof(struct r5l_meta_block);
1850b4c625c6SSong Liu 	log_offset = r5l_ring_add(log, ctx->pos, BLOCK_SECTORS);
1851b4c625c6SSong Liu 
1852b4c625c6SSong Liu 	while (mb_offset < le32_to_cpu(mb->meta_size)) {
1853b4c625c6SSong Liu 		int dd;
1854b4c625c6SSong Liu 
1855b4c625c6SSong Liu 		payload = (void *)mb + mb_offset;
1856b4c625c6SSong Liu 		stripe_sect = (payload->header.type == R5LOG_PAYLOAD_DATA) ?
1857b4c625c6SSong Liu 			raid5_compute_sector(
1858b4c625c6SSong Liu 				conf, le64_to_cpu(payload->location), 0, &dd,
1859b4c625c6SSong Liu 				NULL)
1860b4c625c6SSong Liu 			: le64_to_cpu(payload->location);
1861b4c625c6SSong Liu 
1862b4c625c6SSong Liu 		sh = r5c_recovery_lookup_stripe(cached_stripe_list,
1863b4c625c6SSong Liu 						stripe_sect);
1864b4c625c6SSong Liu 
1865b4c625c6SSong Liu 		if (!sh) {
1866b4c625c6SSong Liu 			sh = r5c_recovery_alloc_stripe(conf, cached_stripe_list,
1867b4c625c6SSong Liu 						       stripe_sect, ctx->pos);
1868b4c625c6SSong Liu 			/*
1869b4c625c6SSong Liu 			 * cannot get stripe from raid5_get_active_stripe
1870b4c625c6SSong Liu 			 * try replay some stripes
1871b4c625c6SSong Liu 			 */
1872b4c625c6SSong Liu 			if (!sh) {
1873b4c625c6SSong Liu 				r5c_recovery_replay_stripes(
1874b4c625c6SSong Liu 					cached_stripe_list, ctx);
1875b4c625c6SSong Liu 				sh = r5c_recovery_alloc_stripe(
1876b4c625c6SSong Liu 					conf, cached_stripe_list,
1877b4c625c6SSong Liu 					stripe_sect, ctx->pos);
1878b4c625c6SSong Liu 			}
1879b4c625c6SSong Liu 			if (!sh) {
1880b4c625c6SSong Liu 				pr_debug("md/raid:%s: Increasing stripe cache size to %d to recovery data on journal.\n",
1881b4c625c6SSong Liu 					mdname(mddev),
1882b4c625c6SSong Liu 					conf->min_nr_stripes * 2);
1883b4c625c6SSong Liu 				raid5_set_cache_size(mddev,
1884b4c625c6SSong Liu 						     conf->min_nr_stripes * 2);
1885b4c625c6SSong Liu 				sh = r5c_recovery_alloc_stripe(
1886b4c625c6SSong Liu 					conf, cached_stripe_list, stripe_sect,
1887b4c625c6SSong Liu 					ctx->pos);
1888b4c625c6SSong Liu 			}
1889b4c625c6SSong Liu 			if (!sh) {
1890b4c625c6SSong Liu 				pr_err("md/raid:%s: Cannot get enough stripes due to memory pressure. Recovery failed.\n",
1891b4c625c6SSong Liu 				       mdname(mddev));
1892b4c625c6SSong Liu 				return -ENOMEM;
1893b4c625c6SSong Liu 			}
1894b4c625c6SSong Liu 			list_add_tail(&sh->lru, cached_stripe_list);
1895b4c625c6SSong Liu 		}
1896b4c625c6SSong Liu 
1897b4c625c6SSong Liu 		if (payload->header.type == R5LOG_PAYLOAD_DATA) {
1898b4c625c6SSong Liu 			if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
1899b4c625c6SSong Liu 				r5l_recovery_replay_one_stripe(conf, sh, ctx);
1900b4c625c6SSong Liu 				r5l_recovery_reset_stripe(sh);
1901b4c625c6SSong Liu 				sh->log_start = ctx->pos;
1902b4c625c6SSong Liu 				list_move_tail(&sh->lru, cached_stripe_list);
1903b4c625c6SSong Liu 			}
1904b4c625c6SSong Liu 			r5l_recovery_load_data(log, sh, ctx, payload,
1905b4c625c6SSong Liu 					       log_offset);
1906b4c625c6SSong Liu 		} else if (payload->header.type == R5LOG_PAYLOAD_PARITY)
1907b4c625c6SSong Liu 			r5l_recovery_load_parity(log, sh, ctx, payload,
1908b4c625c6SSong Liu 						 log_offset);
1909b4c625c6SSong Liu 		else
1910b4c625c6SSong Liu 			return -EINVAL;
1911b4c625c6SSong Liu 
1912b4c625c6SSong Liu 		log_offset = r5l_ring_add(log, log_offset,
1913b4c625c6SSong Liu 					  le32_to_cpu(payload->size));
1914b4c625c6SSong Liu 
1915b4c625c6SSong Liu 		mb_offset += sizeof(struct r5l_payload_data_parity) +
1916b4c625c6SSong Liu 			sizeof(__le32) *
1917b4c625c6SSong Liu 			(le32_to_cpu(payload->size) >> (PAGE_SHIFT - 9));
1918b4c625c6SSong Liu 	}
1919b4c625c6SSong Liu 
1920b4c625c6SSong Liu 	return 0;
1921b4c625c6SSong Liu }
1922b4c625c6SSong Liu 
1923b4c625c6SSong Liu /*
1924b4c625c6SSong Liu  * Load the stripe into cache. The stripe will be written out later by
1925b4c625c6SSong Liu  * the stripe cache state machine.
1926b4c625c6SSong Liu  */
1927b4c625c6SSong Liu static void r5c_recovery_load_one_stripe(struct r5l_log *log,
1928b4c625c6SSong Liu 					 struct stripe_head *sh)
1929b4c625c6SSong Liu {
1930b4c625c6SSong Liu 	struct r5conf *conf = sh->raid_conf;
1931b4c625c6SSong Liu 	struct r5dev *dev;
1932b4c625c6SSong Liu 	int i;
1933b4c625c6SSong Liu 
1934b4c625c6SSong Liu 	for (i = sh->disks; i--; ) {
1935b4c625c6SSong Liu 		dev = sh->dev + i;
1936b4c625c6SSong Liu 		if (test_and_clear_bit(R5_Wantwrite, &dev->flags)) {
1937b4c625c6SSong Liu 			set_bit(R5_InJournal, &dev->flags);
1938b4c625c6SSong Liu 			set_bit(R5_UPTODATE, &dev->flags);
1939b4c625c6SSong Liu 		}
1940b4c625c6SSong Liu 	}
1941b4c625c6SSong Liu 	set_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state);
1942b4c625c6SSong Liu 	atomic_inc(&conf->r5c_cached_partial_stripes);
1943b4c625c6SSong Liu 	list_add_tail(&sh->r5c, &log->stripe_in_journal_list);
1944b4c625c6SSong Liu }
1945b4c625c6SSong Liu 
1946b4c625c6SSong Liu /*
1947b4c625c6SSong Liu  * Scan through the log for all to-be-flushed data
1948b4c625c6SSong Liu  *
1949b4c625c6SSong Liu  * For stripes with data and parity, namely Data-Parity stripe
1950b4c625c6SSong Liu  * (STRIPE_R5C_CACHING == 0), we simply replay all the writes.
1951b4c625c6SSong Liu  *
1952b4c625c6SSong Liu  * For stripes with only data, namely Data-Only stripe
1953b4c625c6SSong Liu  * (STRIPE_R5C_CACHING == 1), we load them to stripe cache state machine.
1954b4c625c6SSong Liu  *
1955b4c625c6SSong Liu  * For a stripe, if we see data after parity, we should discard all previous
1956b4c625c6SSong Liu  * data and parity for this stripe, as these data are already flushed to
1957b4c625c6SSong Liu  * the array.
1958b4c625c6SSong Liu  *
1959b4c625c6SSong Liu  * At the end of the scan, we return the new journal_tail, which points to
1960b4c625c6SSong Liu  * first data-only stripe on the journal device, or next invalid meta block.
1961b4c625c6SSong Liu  */
1962b4c625c6SSong Liu static int r5c_recovery_flush_log(struct r5l_log *log,
1963b4c625c6SSong Liu 				  struct r5l_recovery_ctx *ctx)
1964b4c625c6SSong Liu {
1965b4c625c6SSong Liu 	struct stripe_head *sh, *next;
1966b4c625c6SSong Liu 	int ret = 0;
1967b4c625c6SSong Liu 
1968b4c625c6SSong Liu 	/* scan through the log */
1969b4c625c6SSong Liu 	while (1) {
1970b4c625c6SSong Liu 		if (r5l_recovery_read_meta_block(log, ctx))
1971b4c625c6SSong Liu 			break;
1972b4c625c6SSong Liu 
1973b4c625c6SSong Liu 		ret = r5c_recovery_analyze_meta_block(log, ctx,
1974b4c625c6SSong Liu 						      &ctx->cached_list);
1975b4c625c6SSong Liu 		/*
1976b4c625c6SSong Liu 		 * -EAGAIN means mismatch in data block, in this case, we still
1977b4c625c6SSong Liu 		 * try scan the next metablock
1978b4c625c6SSong Liu 		 */
1979b4c625c6SSong Liu 		if (ret && ret != -EAGAIN)
1980b4c625c6SSong Liu 			break;   /* ret == -EINVAL or -ENOMEM */
1981b4c625c6SSong Liu 		ctx->seq++;
1982b4c625c6SSong Liu 		ctx->pos = r5l_ring_add(log, ctx->pos, ctx->meta_total_blocks);
1983b4c625c6SSong Liu 	}
1984b4c625c6SSong Liu 
1985b4c625c6SSong Liu 	if (ret == -ENOMEM) {
1986b4c625c6SSong Liu 		r5c_recovery_drop_stripes(&ctx->cached_list, ctx);
1987b4c625c6SSong Liu 		return ret;
1988b4c625c6SSong Liu 	}
1989b4c625c6SSong Liu 
1990b4c625c6SSong Liu 	/* replay data-parity stripes */
1991b4c625c6SSong Liu 	r5c_recovery_replay_stripes(&ctx->cached_list, ctx);
1992b4c625c6SSong Liu 
1993b4c625c6SSong Liu 	/* load data-only stripes to stripe cache */
1994b4c625c6SSong Liu 	list_for_each_entry_safe(sh, next, &ctx->cached_list, lru) {
1995b4c625c6SSong Liu 		WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
1996b4c625c6SSong Liu 		r5c_recovery_load_one_stripe(log, sh);
1997b4c625c6SSong Liu 		list_del_init(&sh->lru);
1998b4c625c6SSong Liu 		raid5_release_stripe(sh);
1999b4c625c6SSong Liu 		ctx->data_only_stripes++;
2000b4c625c6SSong Liu 	}
2001b4c625c6SSong Liu 
2002b4c625c6SSong Liu 	return 0;
2003b4c625c6SSong Liu }
2004b4c625c6SSong Liu 
2005b4c625c6SSong Liu /*
2006b4c625c6SSong Liu  * we did a recovery. Now ctx.pos points to an invalid meta block. New
2007b4c625c6SSong Liu  * log will start here. but we can't let superblock point to last valid
2008b4c625c6SSong Liu  * meta block. The log might looks like:
2009b4c625c6SSong Liu  * | meta 1| meta 2| meta 3|
2010b4c625c6SSong Liu  * meta 1 is valid, meta 2 is invalid. meta 3 could be valid. If
2011b4c625c6SSong Liu  * superblock points to meta 1, we write a new valid meta 2n.  if crash
2012b4c625c6SSong Liu  * happens again, new recovery will start from meta 1. Since meta 2n is
2013b4c625c6SSong Liu  * valid now, recovery will think meta 3 is valid, which is wrong.
2014b4c625c6SSong Liu  * The solution is we create a new meta in meta2 with its seq == meta
2015b4c625c6SSong Liu  * 1's seq + 10 and let superblock points to meta2. The same recovery will
2016b4c625c6SSong Liu  * not think meta 3 is a valid meta, because its seq doesn't match
2017b4c625c6SSong Liu  */
2018b4c625c6SSong Liu 
2019b4c625c6SSong Liu /*
2020b4c625c6SSong Liu  * Before recovery, the log looks like the following
2021b4c625c6SSong Liu  *
2022b4c625c6SSong Liu  *   ---------------------------------------------
2023b4c625c6SSong Liu  *   |           valid log        | invalid log  |
2024b4c625c6SSong Liu  *   ---------------------------------------------
2025b4c625c6SSong Liu  *   ^
2026b4c625c6SSong Liu  *   |- log->last_checkpoint
2027b4c625c6SSong Liu  *   |- log->last_cp_seq
2028b4c625c6SSong Liu  *
2029b4c625c6SSong Liu  * Now we scan through the log until we see invalid entry
2030b4c625c6SSong Liu  *
2031b4c625c6SSong Liu  *   ---------------------------------------------
2032b4c625c6SSong Liu  *   |           valid log        | invalid log  |
2033b4c625c6SSong Liu  *   ---------------------------------------------
2034b4c625c6SSong Liu  *   ^                            ^
2035b4c625c6SSong Liu  *   |- log->last_checkpoint      |- ctx->pos
2036b4c625c6SSong Liu  *   |- log->last_cp_seq          |- ctx->seq
2037b4c625c6SSong Liu  *
2038b4c625c6SSong Liu  * From this point, we need to increase seq number by 10 to avoid
2039b4c625c6SSong Liu  * confusing next recovery.
2040b4c625c6SSong Liu  *
2041b4c625c6SSong Liu  *   ---------------------------------------------
2042b4c625c6SSong Liu  *   |           valid log        | invalid log  |
2043b4c625c6SSong Liu  *   ---------------------------------------------
2044b4c625c6SSong Liu  *   ^                              ^
2045b4c625c6SSong Liu  *   |- log->last_checkpoint        |- ctx->pos+1
2046b4c625c6SSong Liu  *   |- log->last_cp_seq            |- ctx->seq+11
2047b4c625c6SSong Liu  *
2048b4c625c6SSong Liu  * However, it is not safe to start the state machine yet, because data only
2049b4c625c6SSong Liu  * parities are not yet secured in RAID. To save these data only parities, we
2050b4c625c6SSong Liu  * rewrite them from seq+11.
2051b4c625c6SSong Liu  *
2052b4c625c6SSong Liu  *   -----------------------------------------------------------------
2053b4c625c6SSong Liu  *   |           valid log        | data only stripes | invalid log  |
2054b4c625c6SSong Liu  *   -----------------------------------------------------------------
2055b4c625c6SSong Liu  *   ^                                                ^
2056b4c625c6SSong Liu  *   |- log->last_checkpoint                          |- ctx->pos+n
2057b4c625c6SSong Liu  *   |- log->last_cp_seq                              |- ctx->seq+10+n
2058b4c625c6SSong Liu  *
2059b4c625c6SSong Liu  * If failure happens again during this process, the recovery can safe start
2060b4c625c6SSong Liu  * again from log->last_checkpoint.
2061b4c625c6SSong Liu  *
2062b4c625c6SSong Liu  * Once data only stripes are rewritten to journal, we move log_tail
2063b4c625c6SSong Liu  *
2064b4c625c6SSong Liu  *   -----------------------------------------------------------------
2065b4c625c6SSong Liu  *   |     old log        |    data only stripes    | invalid log  |
2066b4c625c6SSong Liu  *   -----------------------------------------------------------------
2067b4c625c6SSong Liu  *                        ^                         ^
2068b4c625c6SSong Liu  *                        |- log->last_checkpoint   |- ctx->pos+n
2069b4c625c6SSong Liu  *                        |- log->last_cp_seq       |- ctx->seq+10+n
2070b4c625c6SSong Liu  *
2071b4c625c6SSong Liu  * Then we can safely start the state machine. If failure happens from this
2072b4c625c6SSong Liu  * point on, the recovery will start from new log->last_checkpoint.
2073b4c625c6SSong Liu  */
2074b4c625c6SSong Liu static int
2075b4c625c6SSong Liu r5c_recovery_rewrite_data_only_stripes(struct r5l_log *log,
2076b4c625c6SSong Liu 				       struct r5l_recovery_ctx *ctx)
2077b4c625c6SSong Liu {
2078b4c625c6SSong Liu 	struct stripe_head *sh;
2079b4c625c6SSong Liu 	struct mddev *mddev = log->rdev->mddev;
2080b4c625c6SSong Liu 	struct page *page;
2081b4c625c6SSong Liu 
2082b4c625c6SSong Liu 	page = alloc_page(GFP_KERNEL);
2083b4c625c6SSong Liu 	if (!page) {
2084b4c625c6SSong Liu 		pr_err("md/raid:%s: cannot allocate memory to rewrite data only stripes\n",
2085b4c625c6SSong Liu 		       mdname(mddev));
2086b4c625c6SSong Liu 		return -ENOMEM;
2087b4c625c6SSong Liu 	}
2088b4c625c6SSong Liu 
2089b4c625c6SSong Liu 	ctx->seq += 10;
2090b4c625c6SSong Liu 	list_for_each_entry(sh, &ctx->cached_list, lru) {
2091b4c625c6SSong Liu 		struct r5l_meta_block *mb;
2092b4c625c6SSong Liu 		int i;
2093b4c625c6SSong Liu 		int offset;
2094b4c625c6SSong Liu 		sector_t write_pos;
2095b4c625c6SSong Liu 
2096b4c625c6SSong Liu 		WARN_ON(!test_bit(STRIPE_R5C_CACHING, &sh->state));
2097b4c625c6SSong Liu 		r5l_recovery_create_empty_meta_block(log, page,
2098b4c625c6SSong Liu 						     ctx->pos, ctx->seq);
2099b4c625c6SSong Liu 		mb = page_address(page);
2100b4c625c6SSong Liu 		offset = le32_to_cpu(mb->meta_size);
2101b4c625c6SSong Liu 		write_pos = ctx->pos + BLOCK_SECTORS;
2102b4c625c6SSong Liu 
2103b4c625c6SSong Liu 		for (i = sh->disks; i--; ) {
2104b4c625c6SSong Liu 			struct r5dev *dev = &sh->dev[i];
2105b4c625c6SSong Liu 			struct r5l_payload_data_parity *payload;
2106b4c625c6SSong Liu 			void *addr;
2107b4c625c6SSong Liu 
2108b4c625c6SSong Liu 			if (test_bit(R5_InJournal, &dev->flags)) {
2109b4c625c6SSong Liu 				payload = (void *)mb + offset;
2110b4c625c6SSong Liu 				payload->header.type = cpu_to_le16(
2111b4c625c6SSong Liu 					R5LOG_PAYLOAD_DATA);
2112b4c625c6SSong Liu 				payload->size = BLOCK_SECTORS;
2113b4c625c6SSong Liu 				payload->location = cpu_to_le64(
2114b4c625c6SSong Liu 					raid5_compute_blocknr(sh, i, 0));
2115b4c625c6SSong Liu 				addr = kmap_atomic(dev->page);
2116b4c625c6SSong Liu 				payload->checksum[0] = cpu_to_le32(
2117b4c625c6SSong Liu 					crc32c_le(log->uuid_checksum, addr,
2118b4c625c6SSong Liu 						  PAGE_SIZE));
2119b4c625c6SSong Liu 				kunmap_atomic(addr);
2120b4c625c6SSong Liu 				sync_page_io(log->rdev, write_pos, PAGE_SIZE,
2121b4c625c6SSong Liu 					     dev->page, REQ_OP_WRITE, 0, false);
2122b4c625c6SSong Liu 				write_pos = r5l_ring_add(log, write_pos,
2123b4c625c6SSong Liu 							 BLOCK_SECTORS);
2124b4c625c6SSong Liu 				offset += sizeof(__le32) +
2125b4c625c6SSong Liu 					sizeof(struct r5l_payload_data_parity);
2126b4c625c6SSong Liu 
2127b4c625c6SSong Liu 			}
2128b4c625c6SSong Liu 		}
2129b4c625c6SSong Liu 		mb->meta_size = cpu_to_le32(offset);
2130b4c625c6SSong Liu 		mb->checksum = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
2131b4c625c6SSong Liu 		sync_page_io(log->rdev, ctx->pos, PAGE_SIZE, page,
2132b4c625c6SSong Liu 			     REQ_OP_WRITE, WRITE_FUA, false);
2133b4c625c6SSong Liu 		sh->log_start = ctx->pos;
2134b4c625c6SSong Liu 		ctx->pos = write_pos;
2135b4c625c6SSong Liu 		ctx->seq += 1;
2136b4c625c6SSong Liu 	}
2137b4c625c6SSong Liu 	__free_page(page);
2138b4c625c6SSong Liu 	return 0;
2139b4c625c6SSong Liu }
2140b4c625c6SSong Liu 
2141f6bed0efSShaohua Li static int r5l_recovery_log(struct r5l_log *log)
2142f6bed0efSShaohua Li {
21435aabf7c4SSong Liu 	struct mddev *mddev = log->rdev->mddev;
2144355810d1SShaohua Li 	struct r5l_recovery_ctx ctx;
21455aabf7c4SSong Liu 	int ret;
2146355810d1SShaohua Li 
2147355810d1SShaohua Li 	ctx.pos = log->last_checkpoint;
2148355810d1SShaohua Li 	ctx.seq = log->last_cp_seq;
2149355810d1SShaohua Li 	ctx.meta_page = alloc_page(GFP_KERNEL);
2150b4c625c6SSong Liu 	ctx.data_only_stripes = 0;
2151b4c625c6SSong Liu 	ctx.data_parity_stripes = 0;
2152b4c625c6SSong Liu 	INIT_LIST_HEAD(&ctx.cached_list);
2153b4c625c6SSong Liu 
2154355810d1SShaohua Li 	if (!ctx.meta_page)
2155355810d1SShaohua Li 		return -ENOMEM;
2156355810d1SShaohua Li 
21575aabf7c4SSong Liu 	ret = r5c_recovery_flush_log(log, &ctx);
2158355810d1SShaohua Li 	__free_page(ctx.meta_page);
2159355810d1SShaohua Li 
2160355810d1SShaohua Li 	if (ret)
2161355810d1SShaohua Li 		return ret;
21625aabf7c4SSong Liu 
21635aabf7c4SSong Liu 	if ((ctx.data_only_stripes == 0) && (ctx.data_parity_stripes == 0))
21645aabf7c4SSong Liu 		pr_debug("md/raid:%s: starting from clean shutdown\n",
21655aabf7c4SSong Liu 			 mdname(mddev));
21665aabf7c4SSong Liu 	else {
21675aabf7c4SSong Liu 		pr_debug("md/raid:%s: recoverying %d data-only stripes and %d data-parity stripes\n",
21685aabf7c4SSong Liu 			 mdname(mddev), ctx.data_only_stripes,
21695aabf7c4SSong Liu 			 ctx.data_parity_stripes);
21705aabf7c4SSong Liu 
21715aabf7c4SSong Liu 		if (ctx.data_only_stripes > 0)
21725aabf7c4SSong Liu 			if (r5c_recovery_rewrite_data_only_stripes(log, &ctx)) {
21735aabf7c4SSong Liu 				pr_err("md/raid:%s: failed to rewrite stripes to journal\n",
21745aabf7c4SSong Liu 				       mdname(mddev));
21755aabf7c4SSong Liu 				return -EIO;
21765aabf7c4SSong Liu 			}
21775aabf7c4SSong Liu 	}
21785aabf7c4SSong Liu 
2179355810d1SShaohua Li 	log->log_start = ctx.pos;
21805aabf7c4SSong Liu 	log->next_checkpoint = ctx.pos;
2181355810d1SShaohua Li 	log->seq = ctx.seq;
21825aabf7c4SSong Liu 	r5l_log_write_empty_meta_block(log, ctx.pos, ctx.seq);
21835aabf7c4SSong Liu 	r5l_write_super(log, ctx.pos);
2184f6bed0efSShaohua Li 	return 0;
2185f6bed0efSShaohua Li }
2186f6bed0efSShaohua Li 
2187f6bed0efSShaohua Li static void r5l_write_super(struct r5l_log *log, sector_t cp)
2188f6bed0efSShaohua Li {
2189f6bed0efSShaohua Li 	struct mddev *mddev = log->rdev->mddev;
2190f6bed0efSShaohua Li 
2191f6bed0efSShaohua Li 	log->rdev->journal_tail = cp;
2192f6bed0efSShaohua Li 	set_bit(MD_CHANGE_DEVS, &mddev->flags);
2193f6bed0efSShaohua Li }
2194f6bed0efSShaohua Li 
21952c7da14bSSong Liu static ssize_t r5c_journal_mode_show(struct mddev *mddev, char *page)
21962c7da14bSSong Liu {
21972c7da14bSSong Liu 	struct r5conf *conf = mddev->private;
21982c7da14bSSong Liu 	int ret;
21992c7da14bSSong Liu 
22002c7da14bSSong Liu 	if (!conf->log)
22012c7da14bSSong Liu 		return 0;
22022c7da14bSSong Liu 
22032c7da14bSSong Liu 	switch (conf->log->r5c_journal_mode) {
22042c7da14bSSong Liu 	case R5C_JOURNAL_MODE_WRITE_THROUGH:
22052c7da14bSSong Liu 		ret = snprintf(
22062c7da14bSSong Liu 			page, PAGE_SIZE, "[%s] %s\n",
22072c7da14bSSong Liu 			r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
22082c7da14bSSong Liu 			r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
22092c7da14bSSong Liu 		break;
22102c7da14bSSong Liu 	case R5C_JOURNAL_MODE_WRITE_BACK:
22112c7da14bSSong Liu 		ret = snprintf(
22122c7da14bSSong Liu 			page, PAGE_SIZE, "%s [%s]\n",
22132c7da14bSSong Liu 			r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_THROUGH],
22142c7da14bSSong Liu 			r5c_journal_mode_str[R5C_JOURNAL_MODE_WRITE_BACK]);
22152c7da14bSSong Liu 		break;
22162c7da14bSSong Liu 	default:
22172c7da14bSSong Liu 		ret = 0;
22182c7da14bSSong Liu 	}
22192c7da14bSSong Liu 	return ret;
22202c7da14bSSong Liu }
22212c7da14bSSong Liu 
22222c7da14bSSong Liu static ssize_t r5c_journal_mode_store(struct mddev *mddev,
22232c7da14bSSong Liu 				      const char *page, size_t length)
22242c7da14bSSong Liu {
22252c7da14bSSong Liu 	struct r5conf *conf = mddev->private;
22262c7da14bSSong Liu 	struct r5l_log *log = conf->log;
22272c7da14bSSong Liu 	int val = -1, i;
22282c7da14bSSong Liu 	int len = length;
22292c7da14bSSong Liu 
22302c7da14bSSong Liu 	if (!log)
22312c7da14bSSong Liu 		return -ENODEV;
22322c7da14bSSong Liu 
22332c7da14bSSong Liu 	if (len && page[len - 1] == '\n')
22342c7da14bSSong Liu 		len -= 1;
22352c7da14bSSong Liu 	for (i = 0; i < ARRAY_SIZE(r5c_journal_mode_str); i++)
22362c7da14bSSong Liu 		if (strlen(r5c_journal_mode_str[i]) == len &&
22372c7da14bSSong Liu 		    strncmp(page, r5c_journal_mode_str[i], len) == 0) {
22382c7da14bSSong Liu 			val = i;
22392c7da14bSSong Liu 			break;
22402c7da14bSSong Liu 		}
22412c7da14bSSong Liu 	if (val < R5C_JOURNAL_MODE_WRITE_THROUGH ||
22422c7da14bSSong Liu 	    val > R5C_JOURNAL_MODE_WRITE_BACK)
22432c7da14bSSong Liu 		return -EINVAL;
22442c7da14bSSong Liu 
22452c7da14bSSong Liu 	mddev_suspend(mddev);
22462c7da14bSSong Liu 	conf->log->r5c_journal_mode = val;
22472c7da14bSSong Liu 	mddev_resume(mddev);
22482c7da14bSSong Liu 
22492c7da14bSSong Liu 	pr_debug("md/raid:%s: setting r5c cache mode to %d: %s\n",
22502c7da14bSSong Liu 		 mdname(mddev), val, r5c_journal_mode_str[val]);
22512c7da14bSSong Liu 	return length;
22522c7da14bSSong Liu }
22532c7da14bSSong Liu 
22542c7da14bSSong Liu struct md_sysfs_entry
22552c7da14bSSong Liu r5c_journal_mode = __ATTR(journal_mode, 0644,
22562c7da14bSSong Liu 			  r5c_journal_mode_show, r5c_journal_mode_store);
22572c7da14bSSong Liu 
22582ded3703SSong Liu /*
22592ded3703SSong Liu  * Try handle write operation in caching phase. This function should only
22602ded3703SSong Liu  * be called in write-back mode.
22612ded3703SSong Liu  *
22622ded3703SSong Liu  * If all outstanding writes can be handled in caching phase, returns 0
22632ded3703SSong Liu  * If writes requires write-out phase, call r5c_make_stripe_write_out()
22642ded3703SSong Liu  * and returns -EAGAIN
22652ded3703SSong Liu  */
22662ded3703SSong Liu int r5c_try_caching_write(struct r5conf *conf,
22672ded3703SSong Liu 			  struct stripe_head *sh,
22682ded3703SSong Liu 			  struct stripe_head_state *s,
22692ded3703SSong Liu 			  int disks)
22702ded3703SSong Liu {
22712ded3703SSong Liu 	struct r5l_log *log = conf->log;
22721e6d690bSSong Liu 	int i;
22731e6d690bSSong Liu 	struct r5dev *dev;
22741e6d690bSSong Liu 	int to_cache = 0;
22752ded3703SSong Liu 
22762ded3703SSong Liu 	BUG_ON(!r5c_is_writeback(log));
22772ded3703SSong Liu 
22781e6d690bSSong Liu 	if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
22791e6d690bSSong Liu 		/*
22801e6d690bSSong Liu 		 * There are two different scenarios here:
22811e6d690bSSong Liu 		 *  1. The stripe has some data cached, and it is sent to
22821e6d690bSSong Liu 		 *     write-out phase for reclaim
22831e6d690bSSong Liu 		 *  2. The stripe is clean, and this is the first write
22841e6d690bSSong Liu 		 *
22851e6d690bSSong Liu 		 * For 1, return -EAGAIN, so we continue with
22861e6d690bSSong Liu 		 * handle_stripe_dirtying().
22871e6d690bSSong Liu 		 *
22881e6d690bSSong Liu 		 * For 2, set STRIPE_R5C_CACHING and continue with caching
22891e6d690bSSong Liu 		 * write.
22901e6d690bSSong Liu 		 */
22911e6d690bSSong Liu 
22921e6d690bSSong Liu 		/* case 1: anything injournal or anything in written */
22931e6d690bSSong Liu 		if (s->injournal > 0 || s->written > 0)
22941e6d690bSSong Liu 			return -EAGAIN;
22951e6d690bSSong Liu 		/* case 2 */
22961e6d690bSSong Liu 		set_bit(STRIPE_R5C_CACHING, &sh->state);
22971e6d690bSSong Liu 	}
22981e6d690bSSong Liu 
22991e6d690bSSong Liu 	for (i = disks; i--; ) {
23001e6d690bSSong Liu 		dev = &sh->dev[i];
23011e6d690bSSong Liu 		/* if non-overwrite, use writing-out phase */
23021e6d690bSSong Liu 		if (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags) &&
23031e6d690bSSong Liu 		    !test_bit(R5_InJournal, &dev->flags)) {
23042ded3703SSong Liu 			r5c_make_stripe_write_out(sh);
23052ded3703SSong Liu 			return -EAGAIN;
23062ded3703SSong Liu 		}
23071e6d690bSSong Liu 	}
23081e6d690bSSong Liu 
23091e6d690bSSong Liu 	for (i = disks; i--; ) {
23101e6d690bSSong Liu 		dev = &sh->dev[i];
23111e6d690bSSong Liu 		if (dev->towrite) {
23121e6d690bSSong Liu 			set_bit(R5_Wantwrite, &dev->flags);
23131e6d690bSSong Liu 			set_bit(R5_Wantdrain, &dev->flags);
23141e6d690bSSong Liu 			set_bit(R5_LOCKED, &dev->flags);
23151e6d690bSSong Liu 			to_cache++;
23161e6d690bSSong Liu 		}
23171e6d690bSSong Liu 	}
23181e6d690bSSong Liu 
23191e6d690bSSong Liu 	if (to_cache) {
23201e6d690bSSong Liu 		set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
23211e6d690bSSong Liu 		/*
23221e6d690bSSong Liu 		 * set STRIPE_LOG_TRAPPED, which triggers r5c_cache_data()
23231e6d690bSSong Liu 		 * in ops_run_io(). STRIPE_LOG_TRAPPED will be cleared in
23241e6d690bSSong Liu 		 * r5c_handle_data_cached()
23251e6d690bSSong Liu 		 */
23261e6d690bSSong Liu 		set_bit(STRIPE_LOG_TRAPPED, &sh->state);
23271e6d690bSSong Liu 	}
23281e6d690bSSong Liu 
23291e6d690bSSong Liu 	return 0;
23301e6d690bSSong Liu }
23311e6d690bSSong Liu 
23321e6d690bSSong Liu /*
23331e6d690bSSong Liu  * free extra pages (orig_page) we allocated for prexor
23341e6d690bSSong Liu  */
23351e6d690bSSong Liu void r5c_release_extra_page(struct stripe_head *sh)
23361e6d690bSSong Liu {
23371e6d690bSSong Liu 	int i;
23381e6d690bSSong Liu 
23391e6d690bSSong Liu 	for (i = sh->disks; i--; )
23401e6d690bSSong Liu 		if (sh->dev[i].page != sh->dev[i].orig_page) {
23411e6d690bSSong Liu 			struct page *p = sh->dev[i].orig_page;
23421e6d690bSSong Liu 
23431e6d690bSSong Liu 			sh->dev[i].orig_page = sh->dev[i].page;
23441e6d690bSSong Liu 			put_page(p);
23451e6d690bSSong Liu 		}
23461e6d690bSSong Liu }
23472ded3703SSong Liu 
23482ded3703SSong Liu /*
23492ded3703SSong Liu  * clean up the stripe (clear R5_InJournal for dev[pd_idx] etc.) after the
23502ded3703SSong Liu  * stripe is committed to RAID disks.
23512ded3703SSong Liu  */
23522ded3703SSong Liu void r5c_finish_stripe_write_out(struct r5conf *conf,
23532ded3703SSong Liu 				 struct stripe_head *sh,
23542ded3703SSong Liu 				 struct stripe_head_state *s)
23552ded3703SSong Liu {
23561e6d690bSSong Liu 	int i;
23571e6d690bSSong Liu 	int do_wakeup = 0;
23581e6d690bSSong Liu 
23592ded3703SSong Liu 	if (!conf->log ||
23602ded3703SSong Liu 	    !test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags))
23612ded3703SSong Liu 		return;
23622ded3703SSong Liu 
23632ded3703SSong Liu 	WARN_ON(test_bit(STRIPE_R5C_CACHING, &sh->state));
23642ded3703SSong Liu 	clear_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags);
23652ded3703SSong Liu 
23662ded3703SSong Liu 	if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
23672ded3703SSong Liu 		return;
23681e6d690bSSong Liu 
23691e6d690bSSong Liu 	for (i = sh->disks; i--; ) {
23701e6d690bSSong Liu 		clear_bit(R5_InJournal, &sh->dev[i].flags);
23711e6d690bSSong Liu 		if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
23721e6d690bSSong Liu 			do_wakeup = 1;
23731e6d690bSSong Liu 	}
23741e6d690bSSong Liu 
23751e6d690bSSong Liu 	/*
23761e6d690bSSong Liu 	 * analyse_stripe() runs before r5c_finish_stripe_write_out(),
23771e6d690bSSong Liu 	 * We updated R5_InJournal, so we also update s->injournal.
23781e6d690bSSong Liu 	 */
23791e6d690bSSong Liu 	s->injournal = 0;
23801e6d690bSSong Liu 
23811e6d690bSSong Liu 	if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
23821e6d690bSSong Liu 		if (atomic_dec_and_test(&conf->pending_full_writes))
23831e6d690bSSong Liu 			md_wakeup_thread(conf->mddev->thread);
23841e6d690bSSong Liu 
23851e6d690bSSong Liu 	if (do_wakeup)
23861e6d690bSSong Liu 		wake_up(&conf->wait_for_overlap);
2387a39f7afdSSong Liu 
2388a39f7afdSSong Liu 	if (conf->log->r5c_journal_mode == R5C_JOURNAL_MODE_WRITE_THROUGH)
2389a39f7afdSSong Liu 		return;
2390a39f7afdSSong Liu 
2391a39f7afdSSong Liu 	spin_lock_irq(&conf->log->stripe_in_journal_lock);
2392a39f7afdSSong Liu 	list_del_init(&sh->r5c);
2393a39f7afdSSong Liu 	spin_unlock_irq(&conf->log->stripe_in_journal_lock);
2394a39f7afdSSong Liu 	sh->log_start = MaxSector;
2395a39f7afdSSong Liu 	atomic_dec(&conf->log->stripe_in_journal_count);
23961e6d690bSSong Liu }
23971e6d690bSSong Liu 
23981e6d690bSSong Liu int
23991e6d690bSSong Liu r5c_cache_data(struct r5l_log *log, struct stripe_head *sh,
24001e6d690bSSong Liu 	       struct stripe_head_state *s)
24011e6d690bSSong Liu {
2402a39f7afdSSong Liu 	struct r5conf *conf = sh->raid_conf;
24031e6d690bSSong Liu 	int pages = 0;
24041e6d690bSSong Liu 	int reserve;
24051e6d690bSSong Liu 	int i;
24061e6d690bSSong Liu 	int ret = 0;
24071e6d690bSSong Liu 
24081e6d690bSSong Liu 	BUG_ON(!log);
24091e6d690bSSong Liu 
24101e6d690bSSong Liu 	for (i = 0; i < sh->disks; i++) {
24111e6d690bSSong Liu 		void *addr;
24121e6d690bSSong Liu 
24131e6d690bSSong Liu 		if (!test_bit(R5_Wantwrite, &sh->dev[i].flags))
24141e6d690bSSong Liu 			continue;
24151e6d690bSSong Liu 		addr = kmap_atomic(sh->dev[i].page);
24161e6d690bSSong Liu 		sh->dev[i].log_checksum = crc32c_le(log->uuid_checksum,
24171e6d690bSSong Liu 						    addr, PAGE_SIZE);
24181e6d690bSSong Liu 		kunmap_atomic(addr);
24191e6d690bSSong Liu 		pages++;
24201e6d690bSSong Liu 	}
24211e6d690bSSong Liu 	WARN_ON(pages == 0);
24221e6d690bSSong Liu 
24231e6d690bSSong Liu 	/*
24241e6d690bSSong Liu 	 * The stripe must enter state machine again to call endio, so
24251e6d690bSSong Liu 	 * don't delay.
24261e6d690bSSong Liu 	 */
24271e6d690bSSong Liu 	clear_bit(STRIPE_DELAYED, &sh->state);
24281e6d690bSSong Liu 	atomic_inc(&sh->count);
24291e6d690bSSong Liu 
24301e6d690bSSong Liu 	mutex_lock(&log->io_mutex);
24311e6d690bSSong Liu 	/* meta + data */
24321e6d690bSSong Liu 	reserve = (1 + pages) << (PAGE_SHIFT - 9);
24331e6d690bSSong Liu 
2434a39f7afdSSong Liu 	if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
2435a39f7afdSSong Liu 	    sh->log_start == MaxSector)
2436a39f7afdSSong Liu 		r5l_add_no_space_stripe(log, sh);
2437a39f7afdSSong Liu 	else if (!r5l_has_free_space(log, reserve)) {
2438a39f7afdSSong Liu 		if (sh->log_start == log->last_checkpoint)
2439a39f7afdSSong Liu 			BUG();
2440a39f7afdSSong Liu 		else
2441a39f7afdSSong Liu 			r5l_add_no_space_stripe(log, sh);
24421e6d690bSSong Liu 	} else {
24431e6d690bSSong Liu 		ret = r5l_log_stripe(log, sh, pages, 0);
24441e6d690bSSong Liu 		if (ret) {
24451e6d690bSSong Liu 			spin_lock_irq(&log->io_list_lock);
24461e6d690bSSong Liu 			list_add_tail(&sh->log_list, &log->no_mem_stripes);
24471e6d690bSSong Liu 			spin_unlock_irq(&log->io_list_lock);
24481e6d690bSSong Liu 		}
24491e6d690bSSong Liu 	}
24501e6d690bSSong Liu 
24511e6d690bSSong Liu 	mutex_unlock(&log->io_mutex);
24521e6d690bSSong Liu 	return 0;
24532ded3703SSong Liu }
24542ded3703SSong Liu 
2455f6bed0efSShaohua Li static int r5l_load_log(struct r5l_log *log)
2456f6bed0efSShaohua Li {
2457f6bed0efSShaohua Li 	struct md_rdev *rdev = log->rdev;
2458f6bed0efSShaohua Li 	struct page *page;
2459f6bed0efSShaohua Li 	struct r5l_meta_block *mb;
2460f6bed0efSShaohua Li 	sector_t cp = log->rdev->journal_tail;
2461f6bed0efSShaohua Li 	u32 stored_crc, expected_crc;
2462f6bed0efSShaohua Li 	bool create_super = false;
2463f6bed0efSShaohua Li 	int ret;
2464f6bed0efSShaohua Li 
2465f6bed0efSShaohua Li 	/* Make sure it's valid */
2466f6bed0efSShaohua Li 	if (cp >= rdev->sectors || round_down(cp, BLOCK_SECTORS) != cp)
2467f6bed0efSShaohua Li 		cp = 0;
2468f6bed0efSShaohua Li 	page = alloc_page(GFP_KERNEL);
2469f6bed0efSShaohua Li 	if (!page)
2470f6bed0efSShaohua Li 		return -ENOMEM;
2471f6bed0efSShaohua Li 
2472796a5cf0SMike Christie 	if (!sync_page_io(rdev, cp, PAGE_SIZE, page, REQ_OP_READ, 0, false)) {
2473f6bed0efSShaohua Li 		ret = -EIO;
2474f6bed0efSShaohua Li 		goto ioerr;
2475f6bed0efSShaohua Li 	}
2476f6bed0efSShaohua Li 	mb = page_address(page);
2477f6bed0efSShaohua Li 
2478f6bed0efSShaohua Li 	if (le32_to_cpu(mb->magic) != R5LOG_MAGIC ||
2479f6bed0efSShaohua Li 	    mb->version != R5LOG_VERSION) {
2480f6bed0efSShaohua Li 		create_super = true;
2481f6bed0efSShaohua Li 		goto create;
2482f6bed0efSShaohua Li 	}
2483f6bed0efSShaohua Li 	stored_crc = le32_to_cpu(mb->checksum);
2484f6bed0efSShaohua Li 	mb->checksum = 0;
24855cb2fbd6SShaohua Li 	expected_crc = crc32c_le(log->uuid_checksum, mb, PAGE_SIZE);
2486f6bed0efSShaohua Li 	if (stored_crc != expected_crc) {
2487f6bed0efSShaohua Li 		create_super = true;
2488f6bed0efSShaohua Li 		goto create;
2489f6bed0efSShaohua Li 	}
2490f6bed0efSShaohua Li 	if (le64_to_cpu(mb->position) != cp) {
2491f6bed0efSShaohua Li 		create_super = true;
2492f6bed0efSShaohua Li 		goto create;
2493f6bed0efSShaohua Li 	}
2494f6bed0efSShaohua Li create:
2495f6bed0efSShaohua Li 	if (create_super) {
2496f6bed0efSShaohua Li 		log->last_cp_seq = prandom_u32();
2497f6bed0efSShaohua Li 		cp = 0;
249856056c2eSZhengyuan Liu 		r5l_log_write_empty_meta_block(log, cp, log->last_cp_seq);
2499f6bed0efSShaohua Li 		/*
2500f6bed0efSShaohua Li 		 * Make sure super points to correct address. Log might have
2501f6bed0efSShaohua Li 		 * data very soon. If super hasn't correct log tail address,
2502f6bed0efSShaohua Li 		 * recovery can't find the log
2503f6bed0efSShaohua Li 		 */
2504f6bed0efSShaohua Li 		r5l_write_super(log, cp);
2505f6bed0efSShaohua Li 	} else
2506f6bed0efSShaohua Li 		log->last_cp_seq = le64_to_cpu(mb->seq);
2507f6bed0efSShaohua Li 
2508f6bed0efSShaohua Li 	log->device_size = round_down(rdev->sectors, BLOCK_SECTORS);
25090576b1c6SShaohua Li 	log->max_free_space = log->device_size >> RECLAIM_MAX_FREE_SPACE_SHIFT;
25100576b1c6SShaohua Li 	if (log->max_free_space > RECLAIM_MAX_FREE_SPACE)
25110576b1c6SShaohua Li 		log->max_free_space = RECLAIM_MAX_FREE_SPACE;
2512f6bed0efSShaohua Li 	log->last_checkpoint = cp;
251328cd88e2SZhengyuan Liu 	log->next_checkpoint = cp;
2514a39f7afdSSong Liu 	mutex_lock(&log->io_mutex);
2515a39f7afdSSong Liu 	r5c_update_log_state(log);
2516a39f7afdSSong Liu 	mutex_unlock(&log->io_mutex);
2517f6bed0efSShaohua Li 
2518f6bed0efSShaohua Li 	__free_page(page);
2519f6bed0efSShaohua Li 
2520f6bed0efSShaohua Li 	return r5l_recovery_log(log);
2521f6bed0efSShaohua Li ioerr:
2522f6bed0efSShaohua Li 	__free_page(page);
2523f6bed0efSShaohua Li 	return ret;
2524f6bed0efSShaohua Li }
2525f6bed0efSShaohua Li 
2526f6bed0efSShaohua Li int r5l_init_log(struct r5conf *conf, struct md_rdev *rdev)
2527f6bed0efSShaohua Li {
2528c888a8f9SJens Axboe 	struct request_queue *q = bdev_get_queue(rdev->bdev);
2529f6bed0efSShaohua Li 	struct r5l_log *log;
2530f6bed0efSShaohua Li 
2531f6bed0efSShaohua Li 	if (PAGE_SIZE != 4096)
2532f6bed0efSShaohua Li 		return -EINVAL;
2533c757ec95SSong Liu 
2534c757ec95SSong Liu 	/*
2535c757ec95SSong Liu 	 * The PAGE_SIZE must be big enough to hold 1 r5l_meta_block and
2536c757ec95SSong Liu 	 * raid_disks r5l_payload_data_parity.
2537c757ec95SSong Liu 	 *
2538c757ec95SSong Liu 	 * Write journal and cache does not work for very big array
2539c757ec95SSong Liu 	 * (raid_disks > 203)
2540c757ec95SSong Liu 	 */
2541c757ec95SSong Liu 	if (sizeof(struct r5l_meta_block) +
2542c757ec95SSong Liu 	    ((sizeof(struct r5l_payload_data_parity) + sizeof(__le32)) *
2543c757ec95SSong Liu 	     conf->raid_disks) > PAGE_SIZE) {
2544c757ec95SSong Liu 		pr_err("md/raid:%s: write journal/cache doesn't work for array with %d disks\n",
2545c757ec95SSong Liu 		       mdname(conf->mddev), conf->raid_disks);
2546c757ec95SSong Liu 		return -EINVAL;
2547c757ec95SSong Liu 	}
2548c757ec95SSong Liu 
2549f6bed0efSShaohua Li 	log = kzalloc(sizeof(*log), GFP_KERNEL);
2550f6bed0efSShaohua Li 	if (!log)
2551f6bed0efSShaohua Li 		return -ENOMEM;
2552f6bed0efSShaohua Li 	log->rdev = rdev;
2553f6bed0efSShaohua Li 
2554c888a8f9SJens Axboe 	log->need_cache_flush = test_bit(QUEUE_FLAG_WC, &q->queue_flags) != 0;
255556fef7c6SChristoph Hellwig 
25565cb2fbd6SShaohua Li 	log->uuid_checksum = crc32c_le(~0, rdev->mddev->uuid,
2557f6bed0efSShaohua Li 				       sizeof(rdev->mddev->uuid));
2558f6bed0efSShaohua Li 
2559f6bed0efSShaohua Li 	mutex_init(&log->io_mutex);
2560f6bed0efSShaohua Li 
2561f6bed0efSShaohua Li 	spin_lock_init(&log->io_list_lock);
2562f6bed0efSShaohua Li 	INIT_LIST_HEAD(&log->running_ios);
25630576b1c6SShaohua Li 	INIT_LIST_HEAD(&log->io_end_ios);
2564a8c34f91SShaohua Li 	INIT_LIST_HEAD(&log->flushing_ios);
256504732f74SChristoph Hellwig 	INIT_LIST_HEAD(&log->finished_ios);
2566a8c34f91SShaohua Li 	bio_init(&log->flush_bio);
2567f6bed0efSShaohua Li 
2568f6bed0efSShaohua Li 	log->io_kc = KMEM_CACHE(r5l_io_unit, 0);
2569f6bed0efSShaohua Li 	if (!log->io_kc)
2570f6bed0efSShaohua Li 		goto io_kc;
2571f6bed0efSShaohua Li 
25725036c390SChristoph Hellwig 	log->io_pool = mempool_create_slab_pool(R5L_POOL_SIZE, log->io_kc);
25735036c390SChristoph Hellwig 	if (!log->io_pool)
25745036c390SChristoph Hellwig 		goto io_pool;
25755036c390SChristoph Hellwig 
2576c38d29b3SChristoph Hellwig 	log->bs = bioset_create(R5L_POOL_SIZE, 0);
2577c38d29b3SChristoph Hellwig 	if (!log->bs)
2578c38d29b3SChristoph Hellwig 		goto io_bs;
2579c38d29b3SChristoph Hellwig 
2580e8deb638SChristoph Hellwig 	log->meta_pool = mempool_create_page_pool(R5L_POOL_SIZE, 0);
2581e8deb638SChristoph Hellwig 	if (!log->meta_pool)
2582e8deb638SChristoph Hellwig 		goto out_mempool;
2583e8deb638SChristoph Hellwig 
25840576b1c6SShaohua Li 	log->reclaim_thread = md_register_thread(r5l_reclaim_thread,
25850576b1c6SShaohua Li 						 log->rdev->mddev, "reclaim");
25860576b1c6SShaohua Li 	if (!log->reclaim_thread)
25870576b1c6SShaohua Li 		goto reclaim_thread;
2588a39f7afdSSong Liu 	log->reclaim_thread->timeout = R5C_RECLAIM_WAKEUP_INTERVAL;
2589a39f7afdSSong Liu 
25900fd22b45SShaohua Li 	init_waitqueue_head(&log->iounit_wait);
25910576b1c6SShaohua Li 
25925036c390SChristoph Hellwig 	INIT_LIST_HEAD(&log->no_mem_stripes);
25935036c390SChristoph Hellwig 
2594f6bed0efSShaohua Li 	INIT_LIST_HEAD(&log->no_space_stripes);
2595f6bed0efSShaohua Li 	spin_lock_init(&log->no_space_stripes_lock);
2596f6bed0efSShaohua Li 
2597*3bddb7f8SSong Liu 	INIT_WORK(&log->deferred_io_work, r5l_submit_io_async);
2598*3bddb7f8SSong Liu 
25992ded3703SSong Liu 	log->r5c_journal_mode = R5C_JOURNAL_MODE_WRITE_THROUGH;
2600a39f7afdSSong Liu 	INIT_LIST_HEAD(&log->stripe_in_journal_list);
2601a39f7afdSSong Liu 	spin_lock_init(&log->stripe_in_journal_lock);
2602a39f7afdSSong Liu 	atomic_set(&log->stripe_in_journal_count, 0);
26032ded3703SSong Liu 
2604f6bed0efSShaohua Li 	if (r5l_load_log(log))
2605f6bed0efSShaohua Li 		goto error;
2606f6bed0efSShaohua Li 
2607f6b6ec5cSShaohua Li 	rcu_assign_pointer(conf->log, log);
2608a62ab49eSShaohua Li 	set_bit(MD_HAS_JOURNAL, &conf->mddev->flags);
2609f6bed0efSShaohua Li 	return 0;
2610e8deb638SChristoph Hellwig 
2611f6bed0efSShaohua Li error:
26120576b1c6SShaohua Li 	md_unregister_thread(&log->reclaim_thread);
26130576b1c6SShaohua Li reclaim_thread:
2614e8deb638SChristoph Hellwig 	mempool_destroy(log->meta_pool);
2615e8deb638SChristoph Hellwig out_mempool:
2616c38d29b3SChristoph Hellwig 	bioset_free(log->bs);
2617c38d29b3SChristoph Hellwig io_bs:
26185036c390SChristoph Hellwig 	mempool_destroy(log->io_pool);
26195036c390SChristoph Hellwig io_pool:
2620f6bed0efSShaohua Li 	kmem_cache_destroy(log->io_kc);
2621f6bed0efSShaohua Li io_kc:
2622f6bed0efSShaohua Li 	kfree(log);
2623f6bed0efSShaohua Li 	return -EINVAL;
2624f6bed0efSShaohua Li }
2625f6bed0efSShaohua Li 
2626f6bed0efSShaohua Li void r5l_exit_log(struct r5l_log *log)
2627f6bed0efSShaohua Li {
26280576b1c6SShaohua Li 	md_unregister_thread(&log->reclaim_thread);
2629e8deb638SChristoph Hellwig 	mempool_destroy(log->meta_pool);
2630c38d29b3SChristoph Hellwig 	bioset_free(log->bs);
26315036c390SChristoph Hellwig 	mempool_destroy(log->io_pool);
2632f6bed0efSShaohua Li 	kmem_cache_destroy(log->io_kc);
2633f6bed0efSShaohua Li 	kfree(log);
2634f6bed0efSShaohua Li }
2635