xref: /kernel/linux/linux-6.6/drivers/md/raid5.h (revision 62306a36)
162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _RAID5_H
362306a36Sopenharmony_ci#define _RAID5_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <linux/raid/xor.h>
662306a36Sopenharmony_ci#include <linux/dmaengine.h>
762306a36Sopenharmony_ci#include <linux/local_lock.h>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/*
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * Each stripe contains one buffer per device.  Each buffer can be in
1262306a36Sopenharmony_ci * one of a number of states stored in "flags".  Changes between
1362306a36Sopenharmony_ci * these states happen *almost* exclusively under the protection of the
1462306a36Sopenharmony_ci * STRIPE_ACTIVE flag.  Some very specific changes can happen in bi_end_io, and
1562306a36Sopenharmony_ci * these are not protected by STRIPE_ACTIVE.
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * The flag bits that are used to represent these states are:
1862306a36Sopenharmony_ci *   R5_UPTODATE and R5_LOCKED
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * State Empty == !UPTODATE, !LOCK
2162306a36Sopenharmony_ci *        We have no data, and there is no active request
2262306a36Sopenharmony_ci * State Want == !UPTODATE, LOCK
2362306a36Sopenharmony_ci *        A read request is being submitted for this block
2462306a36Sopenharmony_ci * State Dirty == UPTODATE, LOCK
2562306a36Sopenharmony_ci *        Some new data is in this buffer, and it is being written out
2662306a36Sopenharmony_ci * State Clean == UPTODATE, !LOCK
2762306a36Sopenharmony_ci *        We have valid data which is the same as on disc
2862306a36Sopenharmony_ci *
2962306a36Sopenharmony_ci * The possible state transitions are:
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci *  Empty -> Want   - on read or write to get old data for  parity calc
3262306a36Sopenharmony_ci *  Empty -> Dirty  - on compute_parity to satisfy write/sync request.
3362306a36Sopenharmony_ci *  Empty -> Clean  - on compute_block when computing a block for failed drive
3462306a36Sopenharmony_ci *  Want  -> Empty  - on failed read
3562306a36Sopenharmony_ci *  Want  -> Clean  - on successful completion of read request
3662306a36Sopenharmony_ci *  Dirty -> Clean  - on successful completion of write request
3762306a36Sopenharmony_ci *  Dirty -> Clean  - on failed write
3862306a36Sopenharmony_ci *  Clean -> Dirty  - on compute_parity to satisfy write/sync (RECONSTRUCT or RMW)
3962306a36Sopenharmony_ci *
4062306a36Sopenharmony_ci * The Want->Empty, Want->Clean, Dirty->Clean, transitions
4162306a36Sopenharmony_ci * all happen in b_end_io at interrupt time.
4262306a36Sopenharmony_ci * Each sets the Uptodate bit before releasing the Lock bit.
4362306a36Sopenharmony_ci * This leaves one multi-stage transition:
4462306a36Sopenharmony_ci *    Want->Dirty->Clean
4562306a36Sopenharmony_ci * This is safe because thinking that a Clean buffer is actually dirty
4662306a36Sopenharmony_ci * will at worst delay some action, and the stripe will be scheduled
4762306a36Sopenharmony_ci * for attention after the transition is complete.
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * There is one possibility that is not covered by these states.  That
5062306a36Sopenharmony_ci * is if one drive has failed and there is a spare being rebuilt.  We
5162306a36Sopenharmony_ci * can't distinguish between a clean block that has been generated
5262306a36Sopenharmony_ci * from parity calculations, and a clean block that has been
5362306a36Sopenharmony_ci * successfully written to the spare ( or to parity when resyncing).
5462306a36Sopenharmony_ci * To distinguish these states we have a stripe bit STRIPE_INSYNC that
5562306a36Sopenharmony_ci * is set whenever a write is scheduled to the spare, or to the parity
5662306a36Sopenharmony_ci * disc if there is no spare.  A sync request clears this bit, and
5762306a36Sopenharmony_ci * when we find it set with no buffers locked, we know the sync is
5862306a36Sopenharmony_ci * complete.
5962306a36Sopenharmony_ci *
6062306a36Sopenharmony_ci * Buffers for the md device that arrive via make_request are attached
6162306a36Sopenharmony_ci * to the appropriate stripe in one of two lists linked on b_reqnext.
6262306a36Sopenharmony_ci * One list (bh_read) for read requests, one (bh_write) for write.
6362306a36Sopenharmony_ci * There should never be more than one buffer on the two lists
6462306a36Sopenharmony_ci * together, but we are not guaranteed of that so we allow for more.
6562306a36Sopenharmony_ci *
6662306a36Sopenharmony_ci * If a buffer is on the read list when the associated cache buffer is
6762306a36Sopenharmony_ci * Uptodate, the data is copied into the read buffer and it's b_end_io
6862306a36Sopenharmony_ci * routine is called.  This may happen in the end_request routine only
6962306a36Sopenharmony_ci * if the buffer has just successfully been read.  end_request should
7062306a36Sopenharmony_ci * remove the buffers from the list and then set the Uptodate bit on
7162306a36Sopenharmony_ci * the buffer.  Other threads may do this only if they first check
7262306a36Sopenharmony_ci * that the Uptodate bit is set.  Once they have checked that they may
7362306a36Sopenharmony_ci * take buffers off the read queue.
7462306a36Sopenharmony_ci *
7562306a36Sopenharmony_ci * When a buffer on the write list is committed for write it is copied
7662306a36Sopenharmony_ci * into the cache buffer, which is then marked dirty, and moved onto a
7762306a36Sopenharmony_ci * third list, the written list (bh_written).  Once both the parity
7862306a36Sopenharmony_ci * block and the cached buffer are successfully written, any buffer on
7962306a36Sopenharmony_ci * a written list can be returned with b_end_io.
8062306a36Sopenharmony_ci *
8162306a36Sopenharmony_ci * The write list and read list both act as fifos.  The read list,
8262306a36Sopenharmony_ci * write list and written list are protected by the device_lock.
8362306a36Sopenharmony_ci * The device_lock is only for list manipulations and will only be
8462306a36Sopenharmony_ci * held for a very short time.  It can be claimed from interrupts.
8562306a36Sopenharmony_ci *
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci * Stripes in the stripe cache can be on one of two lists (or on
8862306a36Sopenharmony_ci * neither).  The "inactive_list" contains stripes which are not
8962306a36Sopenharmony_ci * currently being used for any request.  They can freely be reused
9062306a36Sopenharmony_ci * for another stripe.  The "handle_list" contains stripes that need
9162306a36Sopenharmony_ci * to be handled in some way.  Both of these are fifo queues.  Each
9262306a36Sopenharmony_ci * stripe is also (potentially) linked to a hash bucket in the hash
9362306a36Sopenharmony_ci * table so that it can be found by sector number.  Stripes that are
9462306a36Sopenharmony_ci * not hashed must be on the inactive_list, and will normally be at
9562306a36Sopenharmony_ci * the front.  All stripes start life this way.
9662306a36Sopenharmony_ci *
9762306a36Sopenharmony_ci * The inactive_list, handle_list and hash bucket lists are all protected by the
9862306a36Sopenharmony_ci * device_lock.
9962306a36Sopenharmony_ci *  - stripes have a reference counter. If count==0, they are on a list.
10062306a36Sopenharmony_ci *  - If a stripe might need handling, STRIPE_HANDLE is set.
10162306a36Sopenharmony_ci *  - When refcount reaches zero, then if STRIPE_HANDLE it is put on
10262306a36Sopenharmony_ci *    handle_list else inactive_list
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci * This, combined with the fact that STRIPE_HANDLE is only ever
10562306a36Sopenharmony_ci * cleared while a stripe has a non-zero count means that if the
10662306a36Sopenharmony_ci * refcount is 0 and STRIPE_HANDLE is set, then it is on the
10762306a36Sopenharmony_ci * handle_list and if recount is 0 and STRIPE_HANDLE is not set, then
10862306a36Sopenharmony_ci * the stripe is on inactive_list.
10962306a36Sopenharmony_ci *
11062306a36Sopenharmony_ci * The possible transitions are:
11162306a36Sopenharmony_ci *  activate an unhashed/inactive stripe (get_active_stripe())
11262306a36Sopenharmony_ci *     lockdev check-hash unlink-stripe cnt++ clean-stripe hash-stripe unlockdev
11362306a36Sopenharmony_ci *  activate a hashed, possibly active stripe (get_active_stripe())
11462306a36Sopenharmony_ci *     lockdev check-hash if(!cnt++)unlink-stripe unlockdev
11562306a36Sopenharmony_ci *  attach a request to an active stripe (add_stripe_bh())
11662306a36Sopenharmony_ci *     lockdev attach-buffer unlockdev
11762306a36Sopenharmony_ci *  handle a stripe (handle_stripe())
11862306a36Sopenharmony_ci *     setSTRIPE_ACTIVE,  clrSTRIPE_HANDLE ...
11962306a36Sopenharmony_ci *		(lockdev check-buffers unlockdev) ..
12062306a36Sopenharmony_ci *		change-state ..
12162306a36Sopenharmony_ci *		record io/ops needed clearSTRIPE_ACTIVE schedule io/ops
12262306a36Sopenharmony_ci *  release an active stripe (release_stripe())
12362306a36Sopenharmony_ci *     lockdev if (!--cnt) { if  STRIPE_HANDLE, add to handle_list else add to inactive-list } unlockdev
12462306a36Sopenharmony_ci *
12562306a36Sopenharmony_ci * The refcount counts each thread that have activated the stripe,
12662306a36Sopenharmony_ci * plus raid5d if it is handling it, plus one for each active request
12762306a36Sopenharmony_ci * on a cached buffer, and plus one if the stripe is undergoing stripe
12862306a36Sopenharmony_ci * operations.
12962306a36Sopenharmony_ci *
13062306a36Sopenharmony_ci * The stripe operations are:
13162306a36Sopenharmony_ci * -copying data between the stripe cache and user application buffers
13262306a36Sopenharmony_ci * -computing blocks to save a disk access, or to recover a missing block
13362306a36Sopenharmony_ci * -updating the parity on a write operation (reconstruct write and
13462306a36Sopenharmony_ci *  read-modify-write)
13562306a36Sopenharmony_ci * -checking parity correctness
13662306a36Sopenharmony_ci * -running i/o to disk
13762306a36Sopenharmony_ci * These operations are carried out by raid5_run_ops which uses the async_tx
13862306a36Sopenharmony_ci * api to (optionally) offload operations to dedicated hardware engines.
13962306a36Sopenharmony_ci * When requesting an operation handle_stripe sets the pending bit for the
14062306a36Sopenharmony_ci * operation and increments the count.  raid5_run_ops is then run whenever
14162306a36Sopenharmony_ci * the count is non-zero.
14262306a36Sopenharmony_ci * There are some critical dependencies between the operations that prevent some
14362306a36Sopenharmony_ci * from being requested while another is in flight.
14462306a36Sopenharmony_ci * 1/ Parity check operations destroy the in cache version of the parity block,
14562306a36Sopenharmony_ci *    so we prevent parity dependent operations like writes and compute_blocks
14662306a36Sopenharmony_ci *    from starting while a check is in progress.  Some dma engines can perform
14762306a36Sopenharmony_ci *    the check without damaging the parity block, in these cases the parity
14862306a36Sopenharmony_ci *    block is re-marked up to date (assuming the check was successful) and is
14962306a36Sopenharmony_ci *    not re-read from disk.
15062306a36Sopenharmony_ci * 2/ When a write operation is requested we immediately lock the affected
15162306a36Sopenharmony_ci *    blocks, and mark them as not up to date.  This causes new read requests
15262306a36Sopenharmony_ci *    to be held off, as well as parity checks and compute block operations.
15362306a36Sopenharmony_ci * 3/ Once a compute block operation has been requested handle_stripe treats
15462306a36Sopenharmony_ci *    that block as if it is up to date.  raid5_run_ops guaruntees that any
15562306a36Sopenharmony_ci *    operation that is dependent on the compute block result is initiated after
15662306a36Sopenharmony_ci *    the compute block completes.
15762306a36Sopenharmony_ci */
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci/*
16062306a36Sopenharmony_ci * Operations state - intermediate states that are visible outside of
16162306a36Sopenharmony_ci *   STRIPE_ACTIVE.
16262306a36Sopenharmony_ci * In general _idle indicates nothing is running, _run indicates a data
16362306a36Sopenharmony_ci * processing operation is active, and _result means the data processing result
16462306a36Sopenharmony_ci * is stable and can be acted upon.  For simple operations like biofill and
16562306a36Sopenharmony_ci * compute that only have an _idle and _run state they are indicated with
16662306a36Sopenharmony_ci * sh->state flags (STRIPE_BIOFILL_RUN and STRIPE_COMPUTE_RUN)
16762306a36Sopenharmony_ci */
16862306a36Sopenharmony_ci/**
16962306a36Sopenharmony_ci * enum check_states - handles syncing / repairing a stripe
17062306a36Sopenharmony_ci * @check_state_idle - check operations are quiesced
17162306a36Sopenharmony_ci * @check_state_run - check operation is running
17262306a36Sopenharmony_ci * @check_state_result - set outside lock when check result is valid
17362306a36Sopenharmony_ci * @check_state_compute_run - check failed and we are repairing
17462306a36Sopenharmony_ci * @check_state_compute_result - set outside lock when compute result is valid
17562306a36Sopenharmony_ci */
17662306a36Sopenharmony_cienum check_states {
17762306a36Sopenharmony_ci	check_state_idle = 0,
17862306a36Sopenharmony_ci	check_state_run, /* xor parity check */
17962306a36Sopenharmony_ci	check_state_run_q, /* q-parity check */
18062306a36Sopenharmony_ci	check_state_run_pq, /* pq dual parity check */
18162306a36Sopenharmony_ci	check_state_check_result,
18262306a36Sopenharmony_ci	check_state_compute_run, /* parity repair */
18362306a36Sopenharmony_ci	check_state_compute_result,
18462306a36Sopenharmony_ci};
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci/**
18762306a36Sopenharmony_ci * enum reconstruct_states - handles writing or expanding a stripe
18862306a36Sopenharmony_ci */
18962306a36Sopenharmony_cienum reconstruct_states {
19062306a36Sopenharmony_ci	reconstruct_state_idle = 0,
19162306a36Sopenharmony_ci	reconstruct_state_prexor_drain_run,	/* prexor-write */
19262306a36Sopenharmony_ci	reconstruct_state_drain_run,		/* write */
19362306a36Sopenharmony_ci	reconstruct_state_run,			/* expand */
19462306a36Sopenharmony_ci	reconstruct_state_prexor_drain_result,
19562306a36Sopenharmony_ci	reconstruct_state_drain_result,
19662306a36Sopenharmony_ci	reconstruct_state_result,
19762306a36Sopenharmony_ci};
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci#define DEFAULT_STRIPE_SIZE	4096
20062306a36Sopenharmony_cistruct stripe_head {
20162306a36Sopenharmony_ci	struct hlist_node	hash;
20262306a36Sopenharmony_ci	struct list_head	lru;	      /* inactive_list or handle_list */
20362306a36Sopenharmony_ci	struct llist_node	release_list;
20462306a36Sopenharmony_ci	struct r5conf		*raid_conf;
20562306a36Sopenharmony_ci	short			generation;	/* increments with every
20662306a36Sopenharmony_ci						 * reshape */
20762306a36Sopenharmony_ci	sector_t		sector;		/* sector of this row */
20862306a36Sopenharmony_ci	short			pd_idx;		/* parity disk index */
20962306a36Sopenharmony_ci	short			qd_idx;		/* 'Q' disk index for raid6 */
21062306a36Sopenharmony_ci	short			ddf_layout;/* use DDF ordering to calculate Q */
21162306a36Sopenharmony_ci	short			hash_lock_index;
21262306a36Sopenharmony_ci	unsigned long		state;		/* state flags */
21362306a36Sopenharmony_ci	atomic_t		count;	      /* nr of active thread/requests */
21462306a36Sopenharmony_ci	int			bm_seq;	/* sequence number for bitmap flushes */
21562306a36Sopenharmony_ci	int			disks;		/* disks in stripe */
21662306a36Sopenharmony_ci	int			overwrite_disks; /* total overwrite disks in stripe,
21762306a36Sopenharmony_ci						  * this is only checked when stripe
21862306a36Sopenharmony_ci						  * has STRIPE_BATCH_READY
21962306a36Sopenharmony_ci						  */
22062306a36Sopenharmony_ci	enum check_states	check_state;
22162306a36Sopenharmony_ci	enum reconstruct_states reconstruct_state;
22262306a36Sopenharmony_ci	spinlock_t		stripe_lock;
22362306a36Sopenharmony_ci	int			cpu;
22462306a36Sopenharmony_ci	struct r5worker_group	*group;
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	struct stripe_head	*batch_head; /* protected by stripe lock */
22762306a36Sopenharmony_ci	spinlock_t		batch_lock; /* only header's lock is useful */
22862306a36Sopenharmony_ci	struct list_head	batch_list; /* protected by head's batch lock*/
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	union {
23162306a36Sopenharmony_ci		struct r5l_io_unit	*log_io;
23262306a36Sopenharmony_ci		struct ppl_io_unit	*ppl_io;
23362306a36Sopenharmony_ci	};
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	struct list_head	log_list;
23662306a36Sopenharmony_ci	sector_t		log_start; /* first meta block on the journal */
23762306a36Sopenharmony_ci	struct list_head	r5c; /* for r5c_cache->stripe_in_journal */
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	struct page		*ppl_page; /* partial parity of this stripe */
24062306a36Sopenharmony_ci	/**
24162306a36Sopenharmony_ci	 * struct stripe_operations
24262306a36Sopenharmony_ci	 * @target - STRIPE_OP_COMPUTE_BLK target
24362306a36Sopenharmony_ci	 * @target2 - 2nd compute target in the raid6 case
24462306a36Sopenharmony_ci	 * @zero_sum_result - P and Q verification flags
24562306a36Sopenharmony_ci	 * @request - async service request flags for raid_run_ops
24662306a36Sopenharmony_ci	 */
24762306a36Sopenharmony_ci	struct stripe_operations {
24862306a36Sopenharmony_ci		int 		     target, target2;
24962306a36Sopenharmony_ci		enum sum_check_flags zero_sum_result;
25062306a36Sopenharmony_ci	} ops;
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
25362306a36Sopenharmony_ci	/* These pages will be used by bios in dev[i] */
25462306a36Sopenharmony_ci	struct page	**pages;
25562306a36Sopenharmony_ci	int	nr_pages;	/* page array size */
25662306a36Sopenharmony_ci	int	stripes_per_page;
25762306a36Sopenharmony_ci#endif
25862306a36Sopenharmony_ci	struct r5dev {
25962306a36Sopenharmony_ci		/* rreq and rvec are used for the replacement device when
26062306a36Sopenharmony_ci		 * writing data to both devices.
26162306a36Sopenharmony_ci		 */
26262306a36Sopenharmony_ci		struct bio	req, rreq;
26362306a36Sopenharmony_ci		struct bio_vec	vec, rvec;
26462306a36Sopenharmony_ci		struct page	*page, *orig_page;
26562306a36Sopenharmony_ci		unsigned int    offset;     /* offset of the page */
26662306a36Sopenharmony_ci		struct bio	*toread, *read, *towrite, *written;
26762306a36Sopenharmony_ci		sector_t	sector;			/* sector of this page */
26862306a36Sopenharmony_ci		unsigned long	flags;
26962306a36Sopenharmony_ci		u32		log_checksum;
27062306a36Sopenharmony_ci		unsigned short	write_hint;
27162306a36Sopenharmony_ci	} dev[]; /* allocated depending of RAID geometry ("disks" member) */
27262306a36Sopenharmony_ci};
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci/* stripe_head_state - collects and tracks the dynamic state of a stripe_head
27562306a36Sopenharmony_ci *     for handle_stripe.
27662306a36Sopenharmony_ci */
27762306a36Sopenharmony_cistruct stripe_head_state {
27862306a36Sopenharmony_ci	/* 'syncing' means that we need to read all devices, either
27962306a36Sopenharmony_ci	 * to check/correct parity, or to reconstruct a missing device.
28062306a36Sopenharmony_ci	 * 'replacing' means we are replacing one or more drives and
28162306a36Sopenharmony_ci	 * the source is valid at this point so we don't need to
28262306a36Sopenharmony_ci	 * read all devices, just the replacement targets.
28362306a36Sopenharmony_ci	 */
28462306a36Sopenharmony_ci	int syncing, expanding, expanded, replacing;
28562306a36Sopenharmony_ci	int locked, uptodate, to_read, to_write, failed, written;
28662306a36Sopenharmony_ci	int to_fill, compute, req_compute, non_overwrite;
28762306a36Sopenharmony_ci	int injournal, just_cached;
28862306a36Sopenharmony_ci	int failed_num[2];
28962306a36Sopenharmony_ci	int p_failed, q_failed;
29062306a36Sopenharmony_ci	int dec_preread_active;
29162306a36Sopenharmony_ci	unsigned long ops_request;
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci	struct md_rdev *blocked_rdev;
29462306a36Sopenharmony_ci	int handle_bad_blocks;
29562306a36Sopenharmony_ci	int log_failed;
29662306a36Sopenharmony_ci	int waiting_extra_page;
29762306a36Sopenharmony_ci};
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci/* Flags for struct r5dev.flags */
30062306a36Sopenharmony_cienum r5dev_flags {
30162306a36Sopenharmony_ci	R5_UPTODATE,	/* page contains current data */
30262306a36Sopenharmony_ci	R5_LOCKED,	/* IO has been submitted on "req" */
30362306a36Sopenharmony_ci	R5_DOUBLE_LOCKED,/* Cannot clear R5_LOCKED until 2 writes complete */
30462306a36Sopenharmony_ci	R5_OVERWRITE,	/* towrite covers whole page */
30562306a36Sopenharmony_ci/* and some that are internal to handle_stripe */
30662306a36Sopenharmony_ci	R5_Insync,	/* rdev && rdev->in_sync at start */
30762306a36Sopenharmony_ci	R5_Wantread,	/* want to schedule a read */
30862306a36Sopenharmony_ci	R5_Wantwrite,
30962306a36Sopenharmony_ci	R5_Overlap,	/* There is a pending overlapping request
31062306a36Sopenharmony_ci			 * on this block */
31162306a36Sopenharmony_ci	R5_ReadNoMerge, /* prevent bio from merging in block-layer */
31262306a36Sopenharmony_ci	R5_ReadError,	/* seen a read error here recently */
31362306a36Sopenharmony_ci	R5_ReWrite,	/* have tried to over-write the readerror */
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci	R5_Expanded,	/* This block now has post-expand data */
31662306a36Sopenharmony_ci	R5_Wantcompute,	/* compute_block in progress treat as
31762306a36Sopenharmony_ci			 * uptodate
31862306a36Sopenharmony_ci			 */
31962306a36Sopenharmony_ci	R5_Wantfill,	/* dev->toread contains a bio that needs
32062306a36Sopenharmony_ci			 * filling
32162306a36Sopenharmony_ci			 */
32262306a36Sopenharmony_ci	R5_Wantdrain,	/* dev->towrite needs to be drained */
32362306a36Sopenharmony_ci	R5_WantFUA,	/* Write should be FUA */
32462306a36Sopenharmony_ci	R5_SyncIO,	/* The IO is sync */
32562306a36Sopenharmony_ci	R5_WriteError,	/* got a write error - need to record it */
32662306a36Sopenharmony_ci	R5_MadeGood,	/* A bad block has been fixed by writing to it */
32762306a36Sopenharmony_ci	R5_ReadRepl,	/* Will/did read from replacement rather than orig */
32862306a36Sopenharmony_ci	R5_MadeGoodRepl,/* A bad block on the replacement device has been
32962306a36Sopenharmony_ci			 * fixed by writing to it */
33062306a36Sopenharmony_ci	R5_NeedReplace,	/* This device has a replacement which is not
33162306a36Sopenharmony_ci			 * up-to-date at this stripe. */
33262306a36Sopenharmony_ci	R5_WantReplace, /* We need to update the replacement, we have read
33362306a36Sopenharmony_ci			 * data in, and now is a good time to write it out.
33462306a36Sopenharmony_ci			 */
33562306a36Sopenharmony_ci	R5_Discard,	/* Discard the stripe */
33662306a36Sopenharmony_ci	R5_SkipCopy,	/* Don't copy data from bio to stripe cache */
33762306a36Sopenharmony_ci	R5_InJournal,	/* data being written is in the journal device.
33862306a36Sopenharmony_ci			 * if R5_InJournal is set for parity pd_idx, all the
33962306a36Sopenharmony_ci			 * data and parity being written are in the journal
34062306a36Sopenharmony_ci			 * device
34162306a36Sopenharmony_ci			 */
34262306a36Sopenharmony_ci	R5_OrigPageUPTDODATE,	/* with write back cache, we read old data into
34362306a36Sopenharmony_ci				 * dev->orig_page for prexor. When this flag is
34462306a36Sopenharmony_ci				 * set, orig_page contains latest data in the
34562306a36Sopenharmony_ci				 * raid disk.
34662306a36Sopenharmony_ci				 */
34762306a36Sopenharmony_ci};
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci/*
35062306a36Sopenharmony_ci * Stripe state
35162306a36Sopenharmony_ci */
35262306a36Sopenharmony_cienum {
35362306a36Sopenharmony_ci	STRIPE_ACTIVE,
35462306a36Sopenharmony_ci	STRIPE_HANDLE,
35562306a36Sopenharmony_ci	STRIPE_SYNC_REQUESTED,
35662306a36Sopenharmony_ci	STRIPE_SYNCING,
35762306a36Sopenharmony_ci	STRIPE_INSYNC,
35862306a36Sopenharmony_ci	STRIPE_REPLACED,
35962306a36Sopenharmony_ci	STRIPE_PREREAD_ACTIVE,
36062306a36Sopenharmony_ci	STRIPE_DELAYED,
36162306a36Sopenharmony_ci	STRIPE_DEGRADED,
36262306a36Sopenharmony_ci	STRIPE_BIT_DELAY,
36362306a36Sopenharmony_ci	STRIPE_EXPANDING,
36462306a36Sopenharmony_ci	STRIPE_EXPAND_SOURCE,
36562306a36Sopenharmony_ci	STRIPE_EXPAND_READY,
36662306a36Sopenharmony_ci	STRIPE_IO_STARTED,	/* do not count towards 'bypass_count' */
36762306a36Sopenharmony_ci	STRIPE_FULL_WRITE,	/* all blocks are set to be overwritten */
36862306a36Sopenharmony_ci	STRIPE_BIOFILL_RUN,
36962306a36Sopenharmony_ci	STRIPE_COMPUTE_RUN,
37062306a36Sopenharmony_ci	STRIPE_ON_UNPLUG_LIST,
37162306a36Sopenharmony_ci	STRIPE_DISCARD,
37262306a36Sopenharmony_ci	STRIPE_ON_RELEASE_LIST,
37362306a36Sopenharmony_ci	STRIPE_BATCH_READY,
37462306a36Sopenharmony_ci	STRIPE_BATCH_ERR,
37562306a36Sopenharmony_ci	STRIPE_BITMAP_PENDING,	/* Being added to bitmap, don't add
37662306a36Sopenharmony_ci				 * to batch yet.
37762306a36Sopenharmony_ci				 */
37862306a36Sopenharmony_ci	STRIPE_LOG_TRAPPED,	/* trapped into log (see raid5-cache.c)
37962306a36Sopenharmony_ci				 * this bit is used in two scenarios:
38062306a36Sopenharmony_ci				 *
38162306a36Sopenharmony_ci				 * 1. write-out phase
38262306a36Sopenharmony_ci				 *  set in first entry of r5l_write_stripe
38362306a36Sopenharmony_ci				 *  clear in second entry of r5l_write_stripe
38462306a36Sopenharmony_ci				 *  used to bypass logic in handle_stripe
38562306a36Sopenharmony_ci				 *
38662306a36Sopenharmony_ci				 * 2. caching phase
38762306a36Sopenharmony_ci				 *  set in r5c_try_caching_write()
38862306a36Sopenharmony_ci				 *  clear when journal write is done
38962306a36Sopenharmony_ci				 *  used to initiate r5c_cache_data()
39062306a36Sopenharmony_ci				 *  also used to bypass logic in handle_stripe
39162306a36Sopenharmony_ci				 */
39262306a36Sopenharmony_ci	STRIPE_R5C_CACHING,	/* the stripe is in caching phase
39362306a36Sopenharmony_ci				 * see more detail in the raid5-cache.c
39462306a36Sopenharmony_ci				 */
39562306a36Sopenharmony_ci	STRIPE_R5C_PARTIAL_STRIPE,	/* in r5c cache (to-be/being handled or
39662306a36Sopenharmony_ci					 * in conf->r5c_partial_stripe_list)
39762306a36Sopenharmony_ci					 */
39862306a36Sopenharmony_ci	STRIPE_R5C_FULL_STRIPE,	/* in r5c cache (to-be/being handled or
39962306a36Sopenharmony_ci				 * in conf->r5c_full_stripe_list)
40062306a36Sopenharmony_ci				 */
40162306a36Sopenharmony_ci	STRIPE_R5C_PREFLUSH,	/* need to flush journal device */
40262306a36Sopenharmony_ci};
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci#define STRIPE_EXPAND_SYNC_FLAGS \
40562306a36Sopenharmony_ci	((1 << STRIPE_EXPAND_SOURCE) |\
40662306a36Sopenharmony_ci	(1 << STRIPE_EXPAND_READY) |\
40762306a36Sopenharmony_ci	(1 << STRIPE_EXPANDING) |\
40862306a36Sopenharmony_ci	(1 << STRIPE_SYNC_REQUESTED))
40962306a36Sopenharmony_ci/*
41062306a36Sopenharmony_ci * Operation request flags
41162306a36Sopenharmony_ci */
41262306a36Sopenharmony_cienum {
41362306a36Sopenharmony_ci	STRIPE_OP_BIOFILL,
41462306a36Sopenharmony_ci	STRIPE_OP_COMPUTE_BLK,
41562306a36Sopenharmony_ci	STRIPE_OP_PREXOR,
41662306a36Sopenharmony_ci	STRIPE_OP_BIODRAIN,
41762306a36Sopenharmony_ci	STRIPE_OP_RECONSTRUCT,
41862306a36Sopenharmony_ci	STRIPE_OP_CHECK,
41962306a36Sopenharmony_ci	STRIPE_OP_PARTIAL_PARITY,
42062306a36Sopenharmony_ci};
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci/*
42362306a36Sopenharmony_ci * RAID parity calculation preferences
42462306a36Sopenharmony_ci */
42562306a36Sopenharmony_cienum {
42662306a36Sopenharmony_ci	PARITY_DISABLE_RMW = 0,
42762306a36Sopenharmony_ci	PARITY_ENABLE_RMW,
42862306a36Sopenharmony_ci	PARITY_PREFER_RMW,
42962306a36Sopenharmony_ci};
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci/*
43262306a36Sopenharmony_ci * Pages requested from set_syndrome_sources()
43362306a36Sopenharmony_ci */
43462306a36Sopenharmony_cienum {
43562306a36Sopenharmony_ci	SYNDROME_SRC_ALL,
43662306a36Sopenharmony_ci	SYNDROME_SRC_WANT_DRAIN,
43762306a36Sopenharmony_ci	SYNDROME_SRC_WRITTEN,
43862306a36Sopenharmony_ci};
43962306a36Sopenharmony_ci/*
44062306a36Sopenharmony_ci * Plugging:
44162306a36Sopenharmony_ci *
44262306a36Sopenharmony_ci * To improve write throughput, we need to delay the handling of some
44362306a36Sopenharmony_ci * stripes until there has been a chance that several write requests
44462306a36Sopenharmony_ci * for the one stripe have all been collected.
44562306a36Sopenharmony_ci * In particular, any write request that would require pre-reading
44662306a36Sopenharmony_ci * is put on a "delayed" queue until there are no stripes currently
44762306a36Sopenharmony_ci * in a pre-read phase.  Further, if the "delayed" queue is empty when
44862306a36Sopenharmony_ci * a stripe is put on it then we "plug" the queue and do not process it
44962306a36Sopenharmony_ci * until an unplug call is made. (the unplug_io_fn() is called).
45062306a36Sopenharmony_ci *
45162306a36Sopenharmony_ci * When preread is initiated on a stripe, we set PREREAD_ACTIVE and add
45262306a36Sopenharmony_ci * it to the count of prereading stripes.
45362306a36Sopenharmony_ci * When write is initiated, or the stripe refcnt == 0 (just in case) we
45462306a36Sopenharmony_ci * clear the PREREAD_ACTIVE flag and decrement the count
45562306a36Sopenharmony_ci * Whenever the 'handle' queue is empty and the device is not plugged, we
45662306a36Sopenharmony_ci * move any strips from delayed to handle and clear the DELAYED flag and set
45762306a36Sopenharmony_ci * PREREAD_ACTIVE.
45862306a36Sopenharmony_ci * In stripe_handle, if we find pre-reading is necessary, we do it if
45962306a36Sopenharmony_ci * PREREAD_ACTIVE is set, else we set DELAYED which will send it to the delayed queue.
46062306a36Sopenharmony_ci * HANDLE gets cleared if stripe_handle leaves nothing locked.
46162306a36Sopenharmony_ci */
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci/* Note: disk_info.rdev can be set to NULL asynchronously by raid5_remove_disk.
46462306a36Sopenharmony_ci * There are three safe ways to access disk_info.rdev.
46562306a36Sopenharmony_ci * 1/ when holding mddev->reconfig_mutex
46662306a36Sopenharmony_ci * 2/ when resync/recovery/reshape is known to be happening - i.e. in code that
46762306a36Sopenharmony_ci *    is called as part of performing resync/recovery/reshape.
46862306a36Sopenharmony_ci * 3/ while holding rcu_read_lock(), use rcu_dereference to get the pointer
46962306a36Sopenharmony_ci *    and if it is non-NULL, increment rdev->nr_pending before dropping the RCU
47062306a36Sopenharmony_ci *    lock.
47162306a36Sopenharmony_ci * When .rdev is set to NULL, the nr_pending count checked again and if
47262306a36Sopenharmony_ci * it has been incremented, the pointer is put back in .rdev.
47362306a36Sopenharmony_ci */
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_cistruct disk_info {
47662306a36Sopenharmony_ci	struct md_rdev	__rcu *rdev;
47762306a36Sopenharmony_ci	struct md_rdev  __rcu *replacement;
47862306a36Sopenharmony_ci	struct page	*extra_page; /* extra page to use in prexor */
47962306a36Sopenharmony_ci};
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci/*
48262306a36Sopenharmony_ci * Stripe cache
48362306a36Sopenharmony_ci */
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci#define NR_STRIPES		256
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci#if PAGE_SIZE == DEFAULT_STRIPE_SIZE
48862306a36Sopenharmony_ci#define STRIPE_SIZE		PAGE_SIZE
48962306a36Sopenharmony_ci#define STRIPE_SHIFT		(PAGE_SHIFT - 9)
49062306a36Sopenharmony_ci#define STRIPE_SECTORS		(STRIPE_SIZE>>9)
49162306a36Sopenharmony_ci#endif
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ci#define	IO_THRESHOLD		1
49462306a36Sopenharmony_ci#define BYPASS_THRESHOLD	1
49562306a36Sopenharmony_ci#define NR_HASH			(PAGE_SIZE / sizeof(struct hlist_head))
49662306a36Sopenharmony_ci#define HASH_MASK		(NR_HASH - 1)
49762306a36Sopenharmony_ci#define MAX_STRIPE_BATCH	8
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci/* NOTE NR_STRIPE_HASH_LOCKS must remain below 64.
50062306a36Sopenharmony_ci * This is because we sometimes take all the spinlocks
50162306a36Sopenharmony_ci * and creating that much locking depth can cause
50262306a36Sopenharmony_ci * problems.
50362306a36Sopenharmony_ci */
50462306a36Sopenharmony_ci#define NR_STRIPE_HASH_LOCKS 8
50562306a36Sopenharmony_ci#define STRIPE_HASH_LOCKS_MASK (NR_STRIPE_HASH_LOCKS - 1)
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_cistruct r5worker {
50862306a36Sopenharmony_ci	struct work_struct work;
50962306a36Sopenharmony_ci	struct r5worker_group *group;
51062306a36Sopenharmony_ci	struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
51162306a36Sopenharmony_ci	bool working;
51262306a36Sopenharmony_ci};
51362306a36Sopenharmony_ci
51462306a36Sopenharmony_cistruct r5worker_group {
51562306a36Sopenharmony_ci	struct list_head handle_list;
51662306a36Sopenharmony_ci	struct list_head loprio_list;
51762306a36Sopenharmony_ci	struct r5conf *conf;
51862306a36Sopenharmony_ci	struct r5worker *workers;
51962306a36Sopenharmony_ci	int stripes_cnt;
52062306a36Sopenharmony_ci};
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci/*
52362306a36Sopenharmony_ci * r5c journal modes of the array: write-back or write-through.
52462306a36Sopenharmony_ci * write-through mode has identical behavior as existing log only
52562306a36Sopenharmony_ci * implementation.
52662306a36Sopenharmony_ci */
52762306a36Sopenharmony_cienum r5c_journal_mode {
52862306a36Sopenharmony_ci	R5C_JOURNAL_MODE_WRITE_THROUGH = 0,
52962306a36Sopenharmony_ci	R5C_JOURNAL_MODE_WRITE_BACK = 1,
53062306a36Sopenharmony_ci};
53162306a36Sopenharmony_ci
53262306a36Sopenharmony_cienum r5_cache_state {
53362306a36Sopenharmony_ci	R5_INACTIVE_BLOCKED,	/* release of inactive stripes blocked,
53462306a36Sopenharmony_ci				 * waiting for 25% to be free
53562306a36Sopenharmony_ci				 */
53662306a36Sopenharmony_ci	R5_ALLOC_MORE,		/* It might help to allocate another
53762306a36Sopenharmony_ci				 * stripe.
53862306a36Sopenharmony_ci				 */
53962306a36Sopenharmony_ci	R5_DID_ALLOC,		/* A stripe was allocated, don't allocate
54062306a36Sopenharmony_ci				 * more until at least one has been
54162306a36Sopenharmony_ci				 * released.  This avoids flooding
54262306a36Sopenharmony_ci				 * the cache.
54362306a36Sopenharmony_ci				 */
54462306a36Sopenharmony_ci	R5C_LOG_TIGHT,		/* log device space tight, need to
54562306a36Sopenharmony_ci				 * prioritize stripes at last_checkpoint
54662306a36Sopenharmony_ci				 */
54762306a36Sopenharmony_ci	R5C_LOG_CRITICAL,	/* log device is running out of space,
54862306a36Sopenharmony_ci				 * only process stripes that are already
54962306a36Sopenharmony_ci				 * occupying the log
55062306a36Sopenharmony_ci				 */
55162306a36Sopenharmony_ci	R5C_EXTRA_PAGE_IN_USE,	/* a stripe is using disk_info.extra_page
55262306a36Sopenharmony_ci				 * for prexor
55362306a36Sopenharmony_ci				 */
55462306a36Sopenharmony_ci};
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci#define PENDING_IO_MAX 512
55762306a36Sopenharmony_ci#define PENDING_IO_ONE_FLUSH 128
55862306a36Sopenharmony_cistruct r5pending_data {
55962306a36Sopenharmony_ci	struct list_head sibling;
56062306a36Sopenharmony_ci	sector_t sector; /* stripe sector */
56162306a36Sopenharmony_ci	struct bio_list bios;
56262306a36Sopenharmony_ci};
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_cistruct raid5_percpu {
56562306a36Sopenharmony_ci	struct page	*spare_page; /* Used when checking P/Q in raid6 */
56662306a36Sopenharmony_ci	void		*scribble;  /* space for constructing buffer
56762306a36Sopenharmony_ci				     * lists and performing address
56862306a36Sopenharmony_ci				     * conversions
56962306a36Sopenharmony_ci				     */
57062306a36Sopenharmony_ci	int             scribble_obj_size;
57162306a36Sopenharmony_ci	local_lock_t    lock;
57262306a36Sopenharmony_ci};
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_cistruct r5conf {
57562306a36Sopenharmony_ci	struct hlist_head	*stripe_hashtbl;
57662306a36Sopenharmony_ci	/* only protect corresponding hash list and inactive_list */
57762306a36Sopenharmony_ci	spinlock_t		hash_locks[NR_STRIPE_HASH_LOCKS];
57862306a36Sopenharmony_ci	struct mddev		*mddev;
57962306a36Sopenharmony_ci	int			chunk_sectors;
58062306a36Sopenharmony_ci	int			level, algorithm, rmw_level;
58162306a36Sopenharmony_ci	int			max_degraded;
58262306a36Sopenharmony_ci	int			raid_disks;
58362306a36Sopenharmony_ci	int			max_nr_stripes;
58462306a36Sopenharmony_ci	int			min_nr_stripes;
58562306a36Sopenharmony_ci#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
58662306a36Sopenharmony_ci	unsigned long	stripe_size;
58762306a36Sopenharmony_ci	unsigned int	stripe_shift;
58862306a36Sopenharmony_ci	unsigned long	stripe_sectors;
58962306a36Sopenharmony_ci#endif
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci	/* reshape_progress is the leading edge of a 'reshape'
59262306a36Sopenharmony_ci	 * It has value MaxSector when no reshape is happening
59362306a36Sopenharmony_ci	 * If delta_disks < 0, it is the last sector we started work on,
59462306a36Sopenharmony_ci	 * else is it the next sector to work on.
59562306a36Sopenharmony_ci	 */
59662306a36Sopenharmony_ci	sector_t		reshape_progress;
59762306a36Sopenharmony_ci	/* reshape_safe is the trailing edge of a reshape.  We know that
59862306a36Sopenharmony_ci	 * before (or after) this address, all reshape has completed.
59962306a36Sopenharmony_ci	 */
60062306a36Sopenharmony_ci	sector_t		reshape_safe;
60162306a36Sopenharmony_ci	int			previous_raid_disks;
60262306a36Sopenharmony_ci	int			prev_chunk_sectors;
60362306a36Sopenharmony_ci	int			prev_algo;
60462306a36Sopenharmony_ci	short			generation; /* increments with every reshape */
60562306a36Sopenharmony_ci	seqcount_spinlock_t	gen_lock;	/* lock against generation changes */
60662306a36Sopenharmony_ci	unsigned long		reshape_checkpoint; /* Time we last updated
60762306a36Sopenharmony_ci						     * metadata */
60862306a36Sopenharmony_ci	long long		min_offset_diff; /* minimum difference between
60962306a36Sopenharmony_ci						  * data_offset and
61062306a36Sopenharmony_ci						  * new_data_offset across all
61162306a36Sopenharmony_ci						  * devices.  May be negative,
61262306a36Sopenharmony_ci						  * but is closest to zero.
61362306a36Sopenharmony_ci						  */
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_ci	struct list_head	handle_list; /* stripes needing handling */
61662306a36Sopenharmony_ci	struct list_head	loprio_list; /* low priority stripes */
61762306a36Sopenharmony_ci	struct list_head	hold_list; /* preread ready stripes */
61862306a36Sopenharmony_ci	struct list_head	delayed_list; /* stripes that have plugged requests */
61962306a36Sopenharmony_ci	struct list_head	bitmap_list; /* stripes delaying awaiting bitmap update */
62062306a36Sopenharmony_ci	struct bio		*retry_read_aligned; /* currently retrying aligned bios   */
62162306a36Sopenharmony_ci	unsigned int		retry_read_offset; /* sector offset into retry_read_aligned */
62262306a36Sopenharmony_ci	struct bio		*retry_read_aligned_list; /* aligned bios retry list  */
62362306a36Sopenharmony_ci	atomic_t		preread_active_stripes; /* stripes with scheduled io */
62462306a36Sopenharmony_ci	atomic_t		active_aligned_reads;
62562306a36Sopenharmony_ci	atomic_t		pending_full_writes; /* full write backlog */
62662306a36Sopenharmony_ci	int			bypass_count; /* bypassed prereads */
62762306a36Sopenharmony_ci	int			bypass_threshold; /* preread nice */
62862306a36Sopenharmony_ci	int			skip_copy; /* Don't copy data from bio to stripe cache */
62962306a36Sopenharmony_ci	struct list_head	*last_hold; /* detect hold_list promotions */
63062306a36Sopenharmony_ci
63162306a36Sopenharmony_ci	atomic_t		reshape_stripes; /* stripes with pending writes for reshape */
63262306a36Sopenharmony_ci	/* unfortunately we need two cache names as we temporarily have
63362306a36Sopenharmony_ci	 * two caches.
63462306a36Sopenharmony_ci	 */
63562306a36Sopenharmony_ci	int			active_name;
63662306a36Sopenharmony_ci	char			cache_name[2][32];
63762306a36Sopenharmony_ci	struct kmem_cache	*slab_cache; /* for allocating stripes */
63862306a36Sopenharmony_ci	struct mutex		cache_size_mutex; /* Protect changes to cache size */
63962306a36Sopenharmony_ci
64062306a36Sopenharmony_ci	int			seq_flush, seq_write;
64162306a36Sopenharmony_ci	int			quiesce;
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci	int			fullsync;  /* set to 1 if a full sync is needed,
64462306a36Sopenharmony_ci					    * (fresh device added).
64562306a36Sopenharmony_ci					    * Cleared when a sync completes.
64662306a36Sopenharmony_ci					    */
64762306a36Sopenharmony_ci	int			recovery_disabled;
64862306a36Sopenharmony_ci	/* per cpu variables */
64962306a36Sopenharmony_ci	struct raid5_percpu __percpu *percpu;
65062306a36Sopenharmony_ci	int scribble_disks;
65162306a36Sopenharmony_ci	int scribble_sectors;
65262306a36Sopenharmony_ci	struct hlist_node node;
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci	/*
65562306a36Sopenharmony_ci	 * Free stripes pool
65662306a36Sopenharmony_ci	 */
65762306a36Sopenharmony_ci	atomic_t		active_stripes;
65862306a36Sopenharmony_ci	struct list_head	inactive_list[NR_STRIPE_HASH_LOCKS];
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci	atomic_t		r5c_cached_full_stripes;
66162306a36Sopenharmony_ci	struct list_head	r5c_full_stripe_list;
66262306a36Sopenharmony_ci	atomic_t		r5c_cached_partial_stripes;
66362306a36Sopenharmony_ci	struct list_head	r5c_partial_stripe_list;
66462306a36Sopenharmony_ci	atomic_t		r5c_flushing_full_stripes;
66562306a36Sopenharmony_ci	atomic_t		r5c_flushing_partial_stripes;
66662306a36Sopenharmony_ci
66762306a36Sopenharmony_ci	atomic_t		empty_inactive_list_nr;
66862306a36Sopenharmony_ci	struct llist_head	released_stripes;
66962306a36Sopenharmony_ci	wait_queue_head_t	wait_for_quiescent;
67062306a36Sopenharmony_ci	wait_queue_head_t	wait_for_stripe;
67162306a36Sopenharmony_ci	wait_queue_head_t	wait_for_overlap;
67262306a36Sopenharmony_ci	unsigned long		cache_state;
67362306a36Sopenharmony_ci	struct shrinker		shrinker;
67462306a36Sopenharmony_ci	int			pool_size; /* number of disks in stripeheads in pool */
67562306a36Sopenharmony_ci	spinlock_t		device_lock;
67662306a36Sopenharmony_ci	struct disk_info	*disks;
67762306a36Sopenharmony_ci	struct bio_set		bio_split;
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci	/* When taking over an array from a different personality, we store
68062306a36Sopenharmony_ci	 * the new thread here until we fully activate the array.
68162306a36Sopenharmony_ci	 */
68262306a36Sopenharmony_ci	struct md_thread __rcu	*thread;
68362306a36Sopenharmony_ci	struct list_head	temp_inactive_list[NR_STRIPE_HASH_LOCKS];
68462306a36Sopenharmony_ci	struct r5worker_group	*worker_groups;
68562306a36Sopenharmony_ci	int			group_cnt;
68662306a36Sopenharmony_ci	int			worker_cnt_per_group;
68762306a36Sopenharmony_ci	struct r5l_log		*log;
68862306a36Sopenharmony_ci	void			*log_private;
68962306a36Sopenharmony_ci
69062306a36Sopenharmony_ci	spinlock_t		pending_bios_lock;
69162306a36Sopenharmony_ci	bool			batch_bio_dispatch;
69262306a36Sopenharmony_ci	struct r5pending_data	*pending_data;
69362306a36Sopenharmony_ci	struct list_head	free_list;
69462306a36Sopenharmony_ci	struct list_head	pending_list;
69562306a36Sopenharmony_ci	int			pending_data_cnt;
69662306a36Sopenharmony_ci	struct r5pending_data	*next_pending_data;
69762306a36Sopenharmony_ci};
69862306a36Sopenharmony_ci
69962306a36Sopenharmony_ci#if PAGE_SIZE == DEFAULT_STRIPE_SIZE
70062306a36Sopenharmony_ci#define RAID5_STRIPE_SIZE(conf)	STRIPE_SIZE
70162306a36Sopenharmony_ci#define RAID5_STRIPE_SHIFT(conf)	STRIPE_SHIFT
70262306a36Sopenharmony_ci#define RAID5_STRIPE_SECTORS(conf)	STRIPE_SECTORS
70362306a36Sopenharmony_ci#else
70462306a36Sopenharmony_ci#define RAID5_STRIPE_SIZE(conf)	((conf)->stripe_size)
70562306a36Sopenharmony_ci#define RAID5_STRIPE_SHIFT(conf)	((conf)->stripe_shift)
70662306a36Sopenharmony_ci#define RAID5_STRIPE_SECTORS(conf)	((conf)->stripe_sectors)
70762306a36Sopenharmony_ci#endif
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci/* bio's attached to a stripe+device for I/O are linked together in bi_sector
71062306a36Sopenharmony_ci * order without overlap.  There may be several bio's per stripe+device, and
71162306a36Sopenharmony_ci * a bio could span several devices.
71262306a36Sopenharmony_ci * When walking this list for a particular stripe+device, we must never proceed
71362306a36Sopenharmony_ci * beyond a bio that extends past this device, as the next bio might no longer
71462306a36Sopenharmony_ci * be valid.
71562306a36Sopenharmony_ci * This function is used to determine the 'next' bio in the list, given the
71662306a36Sopenharmony_ci * sector of the current stripe+device
71762306a36Sopenharmony_ci */
71862306a36Sopenharmony_cistatic inline struct bio *r5_next_bio(struct r5conf *conf, struct bio *bio, sector_t sector)
71962306a36Sopenharmony_ci{
72062306a36Sopenharmony_ci	if (bio_end_sector(bio) < sector + RAID5_STRIPE_SECTORS(conf))
72162306a36Sopenharmony_ci		return bio->bi_next;
72262306a36Sopenharmony_ci	else
72362306a36Sopenharmony_ci		return NULL;
72462306a36Sopenharmony_ci}
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci/*
72762306a36Sopenharmony_ci * Our supported algorithms
72862306a36Sopenharmony_ci */
72962306a36Sopenharmony_ci#define ALGORITHM_LEFT_ASYMMETRIC	0 /* Rotating Parity N with Data Restart */
73062306a36Sopenharmony_ci#define ALGORITHM_RIGHT_ASYMMETRIC	1 /* Rotating Parity 0 with Data Restart */
73162306a36Sopenharmony_ci#define ALGORITHM_LEFT_SYMMETRIC	2 /* Rotating Parity N with Data Continuation */
73262306a36Sopenharmony_ci#define ALGORITHM_RIGHT_SYMMETRIC	3 /* Rotating Parity 0 with Data Continuation */
73362306a36Sopenharmony_ci
73462306a36Sopenharmony_ci/* Define non-rotating (raid4) algorithms.  These allow
73562306a36Sopenharmony_ci * conversion of raid4 to raid5.
73662306a36Sopenharmony_ci */
73762306a36Sopenharmony_ci#define ALGORITHM_PARITY_0		4 /* P or P,Q are initial devices */
73862306a36Sopenharmony_ci#define ALGORITHM_PARITY_N		5 /* P or P,Q are final devices. */
73962306a36Sopenharmony_ci
74062306a36Sopenharmony_ci/* DDF RAID6 layouts differ from md/raid6 layouts in two ways.
74162306a36Sopenharmony_ci * Firstly, the exact positioning of the parity block is slightly
74262306a36Sopenharmony_ci * different between the 'LEFT_*' modes of md and the "_N_*" modes
74362306a36Sopenharmony_ci * of DDF.
74462306a36Sopenharmony_ci * Secondly, or order of datablocks over which the Q syndrome is computed
74562306a36Sopenharmony_ci * is different.
74662306a36Sopenharmony_ci * Consequently we have different layouts for DDF/raid6 than md/raid6.
74762306a36Sopenharmony_ci * These layouts are from the DDFv1.2 spec.
74862306a36Sopenharmony_ci * Interestingly DDFv1.2-Errata-A does not specify N_CONTINUE but
74962306a36Sopenharmony_ci * leaves RLQ=3 as 'Vendor Specific'
75062306a36Sopenharmony_ci */
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_ci#define ALGORITHM_ROTATING_ZERO_RESTART	8 /* DDF PRL=6 RLQ=1 */
75362306a36Sopenharmony_ci#define ALGORITHM_ROTATING_N_RESTART	9 /* DDF PRL=6 RLQ=2 */
75462306a36Sopenharmony_ci#define ALGORITHM_ROTATING_N_CONTINUE	10 /*DDF PRL=6 RLQ=3 */
75562306a36Sopenharmony_ci
75662306a36Sopenharmony_ci/* For every RAID5 algorithm we define a RAID6 algorithm
75762306a36Sopenharmony_ci * with exactly the same layout for data and parity, and
75862306a36Sopenharmony_ci * with the Q block always on the last device (N-1).
75962306a36Sopenharmony_ci * This allows trivial conversion from RAID5 to RAID6
76062306a36Sopenharmony_ci */
76162306a36Sopenharmony_ci#define ALGORITHM_LEFT_ASYMMETRIC_6	16
76262306a36Sopenharmony_ci#define ALGORITHM_RIGHT_ASYMMETRIC_6	17
76362306a36Sopenharmony_ci#define ALGORITHM_LEFT_SYMMETRIC_6	18
76462306a36Sopenharmony_ci#define ALGORITHM_RIGHT_SYMMETRIC_6	19
76562306a36Sopenharmony_ci#define ALGORITHM_PARITY_0_6		20
76662306a36Sopenharmony_ci#define ALGORITHM_PARITY_N_6		ALGORITHM_PARITY_N
76762306a36Sopenharmony_ci
76862306a36Sopenharmony_cistatic inline int algorithm_valid_raid5(int layout)
76962306a36Sopenharmony_ci{
77062306a36Sopenharmony_ci	return (layout >= 0) &&
77162306a36Sopenharmony_ci		(layout <= 5);
77262306a36Sopenharmony_ci}
77362306a36Sopenharmony_cistatic inline int algorithm_valid_raid6(int layout)
77462306a36Sopenharmony_ci{
77562306a36Sopenharmony_ci	return (layout >= 0 && layout <= 5)
77662306a36Sopenharmony_ci		||
77762306a36Sopenharmony_ci		(layout >= 8 && layout <= 10)
77862306a36Sopenharmony_ci		||
77962306a36Sopenharmony_ci		(layout >= 16 && layout <= 20);
78062306a36Sopenharmony_ci}
78162306a36Sopenharmony_ci
78262306a36Sopenharmony_cistatic inline int algorithm_is_DDF(int layout)
78362306a36Sopenharmony_ci{
78462306a36Sopenharmony_ci	return layout >= 8 && layout <= 10;
78562306a36Sopenharmony_ci}
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
78862306a36Sopenharmony_ci/*
78962306a36Sopenharmony_ci * Return offset of the corresponding page for r5dev.
79062306a36Sopenharmony_ci */
79162306a36Sopenharmony_cistatic inline int raid5_get_page_offset(struct stripe_head *sh, int disk_idx)
79262306a36Sopenharmony_ci{
79362306a36Sopenharmony_ci	return (disk_idx % sh->stripes_per_page) * RAID5_STRIPE_SIZE(sh->raid_conf);
79462306a36Sopenharmony_ci}
79562306a36Sopenharmony_ci
79662306a36Sopenharmony_ci/*
79762306a36Sopenharmony_ci * Return corresponding page address for r5dev.
79862306a36Sopenharmony_ci */
79962306a36Sopenharmony_cistatic inline struct page *
80062306a36Sopenharmony_ciraid5_get_dev_page(struct stripe_head *sh, int disk_idx)
80162306a36Sopenharmony_ci{
80262306a36Sopenharmony_ci	return sh->pages[disk_idx / sh->stripes_per_page];
80362306a36Sopenharmony_ci}
80462306a36Sopenharmony_ci#endif
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_civoid md_raid5_kick_device(struct r5conf *conf);
80762306a36Sopenharmony_ciint raid5_set_cache_size(struct mddev *mddev, int size);
80862306a36Sopenharmony_cisector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous);
80962306a36Sopenharmony_civoid raid5_release_stripe(struct stripe_head *sh);
81062306a36Sopenharmony_cisector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
81162306a36Sopenharmony_ci		int previous, int *dd_idx, struct stripe_head *sh);
81262306a36Sopenharmony_ci
81362306a36Sopenharmony_cistruct stripe_request_ctx;
81462306a36Sopenharmony_ci/* get stripe from previous generation (when reshaping) */
81562306a36Sopenharmony_ci#define R5_GAS_PREVIOUS		(1 << 0)
81662306a36Sopenharmony_ci/* do not block waiting for a free stripe */
81762306a36Sopenharmony_ci#define R5_GAS_NOBLOCK		(1 << 1)
81862306a36Sopenharmony_ci/* do not block waiting for quiesce to be released */
81962306a36Sopenharmony_ci#define R5_GAS_NOQUIESCE	(1 << 2)
82062306a36Sopenharmony_cistruct stripe_head *raid5_get_active_stripe(struct r5conf *conf,
82162306a36Sopenharmony_ci		struct stripe_request_ctx *ctx, sector_t sector,
82262306a36Sopenharmony_ci		unsigned int flags);
82362306a36Sopenharmony_ci
82462306a36Sopenharmony_ciint raid5_calc_degraded(struct r5conf *conf);
82562306a36Sopenharmony_ciint r5c_journal_mode_set(struct mddev *mddev, int journal_mode);
82662306a36Sopenharmony_ci#endif
827