/* SPDX-License-Identifier: GPL-2.0 */ #ifndef _RAID5_H #define _RAID5_H #include <linux/raid/xor.h> #include <linux/dmaengine.h> #include <linux/local_lock.h> /* * * Each stripe contains one buffer per device. Each buffer can be in * one of a number of states stored in "flags". Changes between * these states happen *almost* exclusively under the protection of the * STRIPE_ACTIVE flag. Some very specific changes can happen in bi_end_io, and * these are not protected by STRIPE_ACTIVE. * * The flag bits that are used to represent these states are: * R5_UPTODATE and R5_LOCKED * * State Empty == !UPTODATE, !LOCK * We have no data, and there is no active request * State Want == !UPTODATE, LOCK * A read request is being submitted for this block * State Dirty == UPTODATE, LOCK * Some new data is in this buffer, and it is being written out * State Clean == UPTODATE, !LOCK * We have valid data which is the same as on disc * * The possible state transitions are: * * Empty -> Want - on read or write to get old data for parity calc * Empty -> Dirty - on compute_parity to satisfy write/sync request. * Empty -> Clean - on compute_block when computing a block for failed drive * Want -> Empty - on failed read * Want -> Clean - on successful completion of read request * Dirty -> Clean - on successful completion of write request * Dirty -> Clean - on failed write * Clean -> Dirty - on compute_parity to satisfy write/sync (RECONSTRUCT or RMW) * * The Want->Empty, Want->Clean, Dirty->Clean, transitions * all happen in b_end_io at interrupt time. * Each sets the Uptodate bit before releasing the Lock bit. * This leaves one multi-stage transition: * Want->Dirty->Clean * This is safe because thinking that a Clean buffer is actually dirty * will at worst delay some action, and the stripe will be scheduled * for attention after the transition is complete. * * There is one possibility that is not covered by these states. That * is if one drive has failed and there is a spare being rebuilt. We * can't distinguish between a clean block that has been generated * from parity calculations, and a clean block that has been * successfully written to the spare ( or to parity when resyncing). * To distinguish these states we have a stripe bit STRIPE_INSYNC that * is set whenever a write is scheduled to the spare, or to the parity * disc if there is no spare. A sync request clears this bit, and * when we find it set with no buffers locked, we know the sync is * complete. * * Buffers for the md device that arrive via make_request are attached * to the appropriate stripe in one of two lists linked on b_reqnext. * One list (bh_read) for read requests, one (bh_write) for write. * There should never be more than one buffer on the two lists * together, but we are not guaranteed of that so we allow for more. * * If a buffer is on the read list when the associated cache buffer is * Uptodate, the data is copied into the read buffer and it's b_end_io * routine is called. This may happen in the end_request routine only * if the buffer has just successfully been read. end_request should * remove the buffers from the list and then set the Uptodate bit on * the buffer. Other threads may do this only if they first check * that the Uptodate bit is set. Once they have checked that they may * take buffers off the read queue. * * When a buffer on the write list is committed for write it is copied * into the cache buffer, which is then marked dirty, and moved onto a * third list, the written list (bh_written). Once both the parity * block and the cached buffer are successfully written, any buffer on * a written list can be returned with b_end_io. * * The write list and read list both act as fifos. The read list, * write list and written list are protected by the device_lock. * The device_lock is only for list manipulations and will only be * held for a very short time. It can be claimed from interrupts. * * * Stripes in the stripe cache can be on one of two lists (or on * neither). The "inactive_list" contains stripes which are not * currently being used for any request. They can freely be reused * for another stripe. The "handle_list" contains stripes that need * to be handled in some way. Both of these are fifo queues. Each * stripe is also (potentially) linked to a hash bucket in the hash * table so that it can be found by sector number. Stripes that are * not hashed must be on the inactive_list, and will normally be at * the front. All stripes start life this way. * * The inactive_list, handle_list and hash bucket lists are all protected by the * device_lock. * - stripes have a reference counter. If count==0, they are on a list. * - If a stripe might need handling, STRIPE_HANDLE is set. * - When refcount reaches zero, then if STRIPE_HANDLE it is put on * handle_list else inactive_list * * This, combined with the fact that STRIPE_HANDLE is only ever * cleared while a stripe has a non-zero count means that if the * refcount is 0 and STRIPE_HANDLE is set, then it is on the * handle_list and if recount is 0 and STRIPE_HANDLE is not set, then * the stripe is on inactive_list. * * The possible transitions are: * activate an unhashed/inactive stripe (get_active_stripe()) * lockdev check-hash unlink-stripe cnt++ clean-stripe hash-stripe unlockdev * activate a hashed, possibly active stripe (get_active_stripe()) * lockdev check-hash if(!cnt++)unlink-stripe unlockdev * attach a request to an active stripe (add_stripe_bh()) * lockdev attach-buffer unlockdev * handle a stripe (handle_stripe()) * setSTRIPE_ACTIVE, clrSTRIPE_HANDLE ... * (lockdev check-buffers unlockdev) .. * change-state .. * record io/ops needed clearSTRIPE_ACTIVE schedule io/ops * release an active stripe (release_stripe()) * lockdev if (!--cnt) { if STRIPE_HANDLE, add to handle_list else add to inactive-list } unlockdev * * The refcount counts each thread that have activated the stripe, * plus raid5d if it is handling it, plus one for each active request * on a cached buffer, and plus one if the stripe is undergoing stripe * operations. * * The stripe operations are: * -copying data between the stripe cache and user application buffers * -computing blocks to save a disk access, or to recover a missing block * -updating the parity on a write operation (reconstruct write and * read-modify-write) * -checking parity correctness * -running i/o to disk * These operations are carried out by raid5_run_ops which uses the async_tx * api to (optionally) offload operations to dedicated hardware engines. * When requesting an operation handle_stripe sets the pending bit for the * operation and increments the count. raid5_run_ops is then run whenever * the count is non-zero. * There are some critical dependencies between the operations that prevent some * from being requested while another is in flight. * 1/ Parity check operations destroy the in cache version of the parity block, * so we prevent parity dependent operations like writes and compute_blocks * from starting while a check is in progress. Some dma engines can perform * the check without damaging the parity block, in these cases the parity * block is re-marked up to date (assuming the check was successful) and is * not re-read from disk. * 2/ When a write operation is requested we immediately lock the affected * blocks, and mark them as not up to date. This causes new read requests * to be held off, as well as parity checks and compute block operations. * 3/ Once a compute block operation has been requested handle_stripe treats * that block as if it is up to date. raid5_run_ops guaruntees that any * operation that is dependent on the compute block result is initiated after * the compute block completes. */ /* * Operations state - intermediate states that are visible outside of * STRIPE_ACTIVE. * In general _idle indicates nothing is running, _run indicates a data * processing operation is active, and _result means the data processing result * is stable and can be acted upon. For simple operations like biofill and * compute that only have an _idle and _run state they are indicated with * sh->state flags (STRIPE_BIOFILL_RUN and STRIPE_COMPUTE_RUN) */ /** * enum check_states - handles syncing / repairing a stripe * @check_state_idle - check operations are quiesced * @check_state_run - check operation is running * @check_state_result - set outside lock when check result is valid * @check_state_compute_run - check failed and we are repairing * @check_state_compute_result - set outside lock when compute result is valid */ enum check_states { … }; /** * enum reconstruct_states - handles writing or expanding a stripe */ enum reconstruct_states { … }; #define DEFAULT_STRIPE_SIZE … struct stripe_head { … }; /* stripe_head_state - collects and tracks the dynamic state of a stripe_head * for handle_stripe. */ struct stripe_head_state { … }; /* Flags for struct r5dev.flags */ enum r5dev_flags { … }; /* * Stripe state */ enum { … }; #define STRIPE_EXPAND_SYNC_FLAGS … /* * Operation request flags */ enum { … }; /* * RAID parity calculation preferences */ enum { … }; /* * Pages requested from set_syndrome_sources() */ enum { … }; /* * Plugging: * * To improve write throughput, we need to delay the handling of some * stripes until there has been a chance that several write requests * for the one stripe have all been collected. * In particular, any write request that would require pre-reading * is put on a "delayed" queue until there are no stripes currently * in a pre-read phase. Further, if the "delayed" queue is empty when * a stripe is put on it then we "plug" the queue and do not process it * until an unplug call is made. (the unplug_io_fn() is called). * * When preread is initiated on a stripe, we set PREREAD_ACTIVE and add * it to the count of prereading stripes. * When write is initiated, or the stripe refcnt == 0 (just in case) we * clear the PREREAD_ACTIVE flag and decrement the count * Whenever the 'handle' queue is empty and the device is not plugged, we * move any strips from delayed to handle and clear the DELAYED flag and set * PREREAD_ACTIVE. * In stripe_handle, if we find pre-reading is necessary, we do it if * PREREAD_ACTIVE is set, else we set DELAYED which will send it to the delayed queue. * HANDLE gets cleared if stripe_handle leaves nothing locked. */ /* Note: disk_info.rdev can be set to NULL asynchronously by raid5_remove_disk. * There are three safe ways to access disk_info.rdev. * 1/ when holding mddev->reconfig_mutex * 2/ when resync/recovery/reshape is known to be happening - i.e. in code that * is called as part of performing resync/recovery/reshape. * 3/ while holding rcu_read_lock(), use rcu_dereference to get the pointer * and if it is non-NULL, increment rdev->nr_pending before dropping the RCU * lock. * When .rdev is set to NULL, the nr_pending count checked again and if * it has been incremented, the pointer is put back in .rdev. */ struct disk_info { … }; /* * Stripe cache */ #define NR_STRIPES … #if PAGE_SIZE == DEFAULT_STRIPE_SIZE #define STRIPE_SIZE … #define STRIPE_SHIFT … #define STRIPE_SECTORS … #endif #define IO_THRESHOLD … #define BYPASS_THRESHOLD … #define NR_HASH … #define HASH_MASK … #define MAX_STRIPE_BATCH … /* NOTE NR_STRIPE_HASH_LOCKS must remain below 64. * This is because we sometimes take all the spinlocks * and creating that much locking depth can cause * problems. */ #define NR_STRIPE_HASH_LOCKS … #define STRIPE_HASH_LOCKS_MASK … struct r5worker { … }; struct r5worker_group { … }; /* * r5c journal modes of the array: write-back or write-through. * write-through mode has identical behavior as existing log only * implementation. */ enum r5c_journal_mode { … }; enum r5_cache_state { … }; #define PENDING_IO_MAX … #define PENDING_IO_ONE_FLUSH … struct r5pending_data { … }; struct raid5_percpu { … }; struct r5conf { … }; #if PAGE_SIZE == DEFAULT_STRIPE_SIZE #define RAID5_STRIPE_SIZE(conf) … #define RAID5_STRIPE_SHIFT(conf) … #define RAID5_STRIPE_SECTORS(conf) … #else #define RAID5_STRIPE_SIZE … #define RAID5_STRIPE_SHIFT … #define RAID5_STRIPE_SECTORS … #endif /* bio's attached to a stripe+device for I/O are linked together in bi_sector * order without overlap. There may be several bio's per stripe+device, and * a bio could span several devices. * When walking this list for a particular stripe+device, we must never proceed * beyond a bio that extends past this device, as the next bio might no longer * be valid. * This function is used to determine the 'next' bio in the list, given the * sector of the current stripe+device */ static inline struct bio *r5_next_bio(struct r5conf *conf, struct bio *bio, sector_t sector) { … } /* * Our supported algorithms */ #define ALGORITHM_LEFT_ASYMMETRIC … #define ALGORITHM_RIGHT_ASYMMETRIC … #define ALGORITHM_LEFT_SYMMETRIC … #define ALGORITHM_RIGHT_SYMMETRIC … /* Define non-rotating (raid4) algorithms. These allow * conversion of raid4 to raid5. */ #define ALGORITHM_PARITY_0 … #define ALGORITHM_PARITY_N … /* DDF RAID6 layouts differ from md/raid6 layouts in two ways. * Firstly, the exact positioning of the parity block is slightly * different between the 'LEFT_*' modes of md and the "_N_*" modes * of DDF. * Secondly, or order of datablocks over which the Q syndrome is computed * is different. * Consequently we have different layouts for DDF/raid6 than md/raid6. * These layouts are from the DDFv1.2 spec. * Interestingly DDFv1.2-Errata-A does not specify N_CONTINUE but * leaves RLQ=3 as 'Vendor Specific' */ #define ALGORITHM_ROTATING_ZERO_RESTART … #define ALGORITHM_ROTATING_N_RESTART … #define ALGORITHM_ROTATING_N_CONTINUE … /* For every RAID5 algorithm we define a RAID6 algorithm * with exactly the same layout for data and parity, and * with the Q block always on the last device (N-1). * This allows trivial conversion from RAID5 to RAID6 */ #define ALGORITHM_LEFT_ASYMMETRIC_6 … #define ALGORITHM_RIGHT_ASYMMETRIC_6 … #define ALGORITHM_LEFT_SYMMETRIC_6 … #define ALGORITHM_RIGHT_SYMMETRIC_6 … #define ALGORITHM_PARITY_0_6 … #define ALGORITHM_PARITY_N_6 … static inline int algorithm_valid_raid5(int layout) { … } static inline int algorithm_valid_raid6(int layout) { … } static inline int algorithm_is_DDF(int layout) { … } #if PAGE_SIZE != DEFAULT_STRIPE_SIZE /* * Return offset of the corresponding page for r5dev. */ static inline int raid5_get_page_offset(struct stripe_head *sh, int disk_idx) { return (disk_idx % sh->stripes_per_page) * RAID5_STRIPE_SIZE(sh->raid_conf); } /* * Return corresponding page address for r5dev. */ static inline struct page * raid5_get_dev_page(struct stripe_head *sh, int disk_idx) { return sh->pages[disk_idx / sh->stripes_per_page]; } #endif void md_raid5_kick_device(struct r5conf *conf); int raid5_set_cache_size(struct mddev *mddev, int size); sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous); void raid5_release_stripe(struct stripe_head *sh); sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, int previous, int *dd_idx, struct stripe_head *sh); struct stripe_request_ctx; /* get stripe from previous generation (when reshaping) */ #define R5_GAS_PREVIOUS … /* do not block waiting for a free stripe */ #define R5_GAS_NOBLOCK … /* do not block waiting for quiesce to be released */ #define R5_GAS_NOQUIESCE … struct stripe_head *raid5_get_active_stripe(struct r5conf *conf, struct stripe_request_ctx *ctx, sector_t sector, unsigned int flags); int raid5_calc_degraded(struct r5conf *conf); int r5c_journal_mode_set(struct mddev *mddev, int journal_mode); #endif