Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _RAID5_H
0003 #define _RAID5_H
0004 
0005 #include <linux/raid/xor.h>
0006 #include <linux/dmaengine.h>
0007 #include <linux/local_lock.h>
0008 
0009 /*
0010  *
0011  * Each stripe contains one buffer per device.  Each buffer can be in
0012  * one of a number of states stored in "flags".  Changes between
0013  * these states happen *almost* exclusively under the protection of the
0014  * STRIPE_ACTIVE flag.  Some very specific changes can happen in bi_end_io, and
0015  * these are not protected by STRIPE_ACTIVE.
0016  *
0017  * The flag bits that are used to represent these states are:
0018  *   R5_UPTODATE and R5_LOCKED
0019  *
0020  * State Empty == !UPTODATE, !LOCK
0021  *        We have no data, and there is no active request
0022  * State Want == !UPTODATE, LOCK
0023  *        A read request is being submitted for this block
0024  * State Dirty == UPTODATE, LOCK
0025  *        Some new data is in this buffer, and it is being written out
0026  * State Clean == UPTODATE, !LOCK
0027  *        We have valid data which is the same as on disc
0028  *
0029  * The possible state transitions are:
0030  *
0031  *  Empty -> Want   - on read or write to get old data for  parity calc
0032  *  Empty -> Dirty  - on compute_parity to satisfy write/sync request.
0033  *  Empty -> Clean  - on compute_block when computing a block for failed drive
0034  *  Want  -> Empty  - on failed read
0035  *  Want  -> Clean  - on successful completion of read request
0036  *  Dirty -> Clean  - on successful completion of write request
0037  *  Dirty -> Clean  - on failed write
0038  *  Clean -> Dirty  - on compute_parity to satisfy write/sync (RECONSTRUCT or RMW)
0039  *
0040  * The Want->Empty, Want->Clean, Dirty->Clean, transitions
0041  * all happen in b_end_io at interrupt time.
0042  * Each sets the Uptodate bit before releasing the Lock bit.
0043  * This leaves one multi-stage transition:
0044  *    Want->Dirty->Clean
0045  * This is safe because thinking that a Clean buffer is actually dirty
0046  * will at worst delay some action, and the stripe will be scheduled
0047  * for attention after the transition is complete.
0048  *
0049  * There is one possibility that is not covered by these states.  That
0050  * is if one drive has failed and there is a spare being rebuilt.  We
0051  * can't distinguish between a clean block that has been generated
0052  * from parity calculations, and a clean block that has been
0053  * successfully written to the spare ( or to parity when resyncing).
0054  * To distinguish these states we have a stripe bit STRIPE_INSYNC that
0055  * is set whenever a write is scheduled to the spare, or to the parity
0056  * disc if there is no spare.  A sync request clears this bit, and
0057  * when we find it set with no buffers locked, we know the sync is
0058  * complete.
0059  *
0060  * Buffers for the md device that arrive via make_request are attached
0061  * to the appropriate stripe in one of two lists linked on b_reqnext.
0062  * One list (bh_read) for read requests, one (bh_write) for write.
0063  * There should never be more than one buffer on the two lists
0064  * together, but we are not guaranteed of that so we allow for more.
0065  *
0066  * If a buffer is on the read list when the associated cache buffer is
0067  * Uptodate, the data is copied into the read buffer and it's b_end_io
0068  * routine is called.  This may happen in the end_request routine only
0069  * if the buffer has just successfully been read.  end_request should
0070  * remove the buffers from the list and then set the Uptodate bit on
0071  * the buffer.  Other threads may do this only if they first check
0072  * that the Uptodate bit is set.  Once they have checked that they may
0073  * take buffers off the read queue.
0074  *
0075  * When a buffer on the write list is committed for write it is copied
0076  * into the cache buffer, which is then marked dirty, and moved onto a
0077  * third list, the written list (bh_written).  Once both the parity
0078  * block and the cached buffer are successfully written, any buffer on
0079  * a written list can be returned with b_end_io.
0080  *
0081  * The write list and read list both act as fifos.  The read list,
0082  * write list and written list are protected by the device_lock.
0083  * The device_lock is only for list manipulations and will only be
0084  * held for a very short time.  It can be claimed from interrupts.
0085  *
0086  *
0087  * Stripes in the stripe cache can be on one of two lists (or on
0088  * neither).  The "inactive_list" contains stripes which are not
0089  * currently being used for any request.  They can freely be reused
0090  * for another stripe.  The "handle_list" contains stripes that need
0091  * to be handled in some way.  Both of these are fifo queues.  Each
0092  * stripe is also (potentially) linked to a hash bucket in the hash
0093  * table so that it can be found by sector number.  Stripes that are
0094  * not hashed must be on the inactive_list, and will normally be at
0095  * the front.  All stripes start life this way.
0096  *
0097  * The inactive_list, handle_list and hash bucket lists are all protected by the
0098  * device_lock.
0099  *  - stripes have a reference counter. If count==0, they are on a list.
0100  *  - If a stripe might need handling, STRIPE_HANDLE is set.
0101  *  - When refcount reaches zero, then if STRIPE_HANDLE it is put on
0102  *    handle_list else inactive_list
0103  *
0104  * This, combined with the fact that STRIPE_HANDLE is only ever
0105  * cleared while a stripe has a non-zero count means that if the
0106  * refcount is 0 and STRIPE_HANDLE is set, then it is on the
0107  * handle_list and if recount is 0 and STRIPE_HANDLE is not set, then
0108  * the stripe is on inactive_list.
0109  *
0110  * The possible transitions are:
0111  *  activate an unhashed/inactive stripe (get_active_stripe())
0112  *     lockdev check-hash unlink-stripe cnt++ clean-stripe hash-stripe unlockdev
0113  *  activate a hashed, possibly active stripe (get_active_stripe())
0114  *     lockdev check-hash if(!cnt++)unlink-stripe unlockdev
0115  *  attach a request to an active stripe (add_stripe_bh())
0116  *     lockdev attach-buffer unlockdev
0117  *  handle a stripe (handle_stripe())
0118  *     setSTRIPE_ACTIVE,  clrSTRIPE_HANDLE ...
0119  *      (lockdev check-buffers unlockdev) ..
0120  *      change-state ..
0121  *      record io/ops needed clearSTRIPE_ACTIVE schedule io/ops
0122  *  release an active stripe (release_stripe())
0123  *     lockdev if (!--cnt) { if  STRIPE_HANDLE, add to handle_list else add to inactive-list } unlockdev
0124  *
0125  * The refcount counts each thread that have activated the stripe,
0126  * plus raid5d if it is handling it, plus one for each active request
0127  * on a cached buffer, and plus one if the stripe is undergoing stripe
0128  * operations.
0129  *
0130  * The stripe operations are:
0131  * -copying data between the stripe cache and user application buffers
0132  * -computing blocks to save a disk access, or to recover a missing block
0133  * -updating the parity on a write operation (reconstruct write and
0134  *  read-modify-write)
0135  * -checking parity correctness
0136  * -running i/o to disk
0137  * These operations are carried out by raid5_run_ops which uses the async_tx
0138  * api to (optionally) offload operations to dedicated hardware engines.
0139  * When requesting an operation handle_stripe sets the pending bit for the
0140  * operation and increments the count.  raid5_run_ops is then run whenever
0141  * the count is non-zero.
0142  * There are some critical dependencies between the operations that prevent some
0143  * from being requested while another is in flight.
0144  * 1/ Parity check operations destroy the in cache version of the parity block,
0145  *    so we prevent parity dependent operations like writes and compute_blocks
0146  *    from starting while a check is in progress.  Some dma engines can perform
0147  *    the check without damaging the parity block, in these cases the parity
0148  *    block is re-marked up to date (assuming the check was successful) and is
0149  *    not re-read from disk.
0150  * 2/ When a write operation is requested we immediately lock the affected
0151  *    blocks, and mark them as not up to date.  This causes new read requests
0152  *    to be held off, as well as parity checks and compute block operations.
0153  * 3/ Once a compute block operation has been requested handle_stripe treats
0154  *    that block as if it is up to date.  raid5_run_ops guaruntees that any
0155  *    operation that is dependent on the compute block result is initiated after
0156  *    the compute block completes.
0157  */
0158 
0159 /*
0160  * Operations state - intermediate states that are visible outside of
0161  *   STRIPE_ACTIVE.
0162  * In general _idle indicates nothing is running, _run indicates a data
0163  * processing operation is active, and _result means the data processing result
0164  * is stable and can be acted upon.  For simple operations like biofill and
0165  * compute that only have an _idle and _run state they are indicated with
0166  * sh->state flags (STRIPE_BIOFILL_RUN and STRIPE_COMPUTE_RUN)
0167  */
0168 /**
0169  * enum check_states - handles syncing / repairing a stripe
0170  * @check_state_idle - check operations are quiesced
0171  * @check_state_run - check operation is running
0172  * @check_state_result - set outside lock when check result is valid
0173  * @check_state_compute_run - check failed and we are repairing
0174  * @check_state_compute_result - set outside lock when compute result is valid
0175  */
0176 enum check_states {
0177     check_state_idle = 0,
0178     check_state_run, /* xor parity check */
0179     check_state_run_q, /* q-parity check */
0180     check_state_run_pq, /* pq dual parity check */
0181     check_state_check_result,
0182     check_state_compute_run, /* parity repair */
0183     check_state_compute_result,
0184 };
0185 
0186 /**
0187  * enum reconstruct_states - handles writing or expanding a stripe
0188  */
0189 enum reconstruct_states {
0190     reconstruct_state_idle = 0,
0191     reconstruct_state_prexor_drain_run, /* prexor-write */
0192     reconstruct_state_drain_run,        /* write */
0193     reconstruct_state_run,          /* expand */
0194     reconstruct_state_prexor_drain_result,
0195     reconstruct_state_drain_result,
0196     reconstruct_state_result,
0197 };
0198 
0199 #define DEFAULT_STRIPE_SIZE 4096
0200 struct stripe_head {
0201     struct hlist_node   hash;
0202     struct list_head    lru;          /* inactive_list or handle_list */
0203     struct llist_node   release_list;
0204     struct r5conf       *raid_conf;
0205     short           generation; /* increments with every
0206                          * reshape */
0207     sector_t        sector;     /* sector of this row */
0208     short           pd_idx;     /* parity disk index */
0209     short           qd_idx;     /* 'Q' disk index for raid6 */
0210     short           ddf_layout;/* use DDF ordering to calculate Q */
0211     short           hash_lock_index;
0212     unsigned long       state;      /* state flags */
0213     atomic_t        count;        /* nr of active thread/requests */
0214     int         bm_seq; /* sequence number for bitmap flushes */
0215     int         disks;      /* disks in stripe */
0216     int         overwrite_disks; /* total overwrite disks in stripe,
0217                           * this is only checked when stripe
0218                           * has STRIPE_BATCH_READY
0219                           */
0220     enum check_states   check_state;
0221     enum reconstruct_states reconstruct_state;
0222     spinlock_t      stripe_lock;
0223     int         cpu;
0224     struct r5worker_group   *group;
0225 
0226     struct stripe_head  *batch_head; /* protected by stripe lock */
0227     spinlock_t      batch_lock; /* only header's lock is useful */
0228     struct list_head    batch_list; /* protected by head's batch lock*/
0229 
0230     union {
0231         struct r5l_io_unit  *log_io;
0232         struct ppl_io_unit  *ppl_io;
0233     };
0234 
0235     struct list_head    log_list;
0236     sector_t        log_start; /* first meta block on the journal */
0237     struct list_head    r5c; /* for r5c_cache->stripe_in_journal */
0238 
0239     struct page     *ppl_page; /* partial parity of this stripe */
0240     /**
0241      * struct stripe_operations
0242      * @target - STRIPE_OP_COMPUTE_BLK target
0243      * @target2 - 2nd compute target in the raid6 case
0244      * @zero_sum_result - P and Q verification flags
0245      * @request - async service request flags for raid_run_ops
0246      */
0247     struct stripe_operations {
0248         int              target, target2;
0249         enum sum_check_flags zero_sum_result;
0250     } ops;
0251 
0252 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
0253     /* These pages will be used by bios in dev[i] */
0254     struct page **pages;
0255     int nr_pages;   /* page array size */
0256     int stripes_per_page;
0257 #endif
0258     struct r5dev {
0259         /* rreq and rvec are used for the replacement device when
0260          * writing data to both devices.
0261          */
0262         struct bio  req, rreq;
0263         struct bio_vec  vec, rvec;
0264         struct page *page, *orig_page;
0265         unsigned int    offset;     /* offset of the page */
0266         struct bio  *toread, *read, *towrite, *written;
0267         sector_t    sector;         /* sector of this page */
0268         unsigned long   flags;
0269         u32     log_checksum;
0270         unsigned short  write_hint;
0271     } dev[1]; /* allocated with extra space depending of RAID geometry */
0272 };
0273 
0274 /* stripe_head_state - collects and tracks the dynamic state of a stripe_head
0275  *     for handle_stripe.
0276  */
0277 struct stripe_head_state {
0278     /* 'syncing' means that we need to read all devices, either
0279      * to check/correct parity, or to reconstruct a missing device.
0280      * 'replacing' means we are replacing one or more drives and
0281      * the source is valid at this point so we don't need to
0282      * read all devices, just the replacement targets.
0283      */
0284     int syncing, expanding, expanded, replacing;
0285     int locked, uptodate, to_read, to_write, failed, written;
0286     int to_fill, compute, req_compute, non_overwrite;
0287     int injournal, just_cached;
0288     int failed_num[2];
0289     int p_failed, q_failed;
0290     int dec_preread_active;
0291     unsigned long ops_request;
0292 
0293     struct md_rdev *blocked_rdev;
0294     int handle_bad_blocks;
0295     int log_failed;
0296     int waiting_extra_page;
0297 };
0298 
0299 /* Flags for struct r5dev.flags */
0300 enum r5dev_flags {
0301     R5_UPTODATE,    /* page contains current data */
0302     R5_LOCKED,  /* IO has been submitted on "req" */
0303     R5_DOUBLE_LOCKED,/* Cannot clear R5_LOCKED until 2 writes complete */
0304     R5_OVERWRITE,   /* towrite covers whole page */
0305 /* and some that are internal to handle_stripe */
0306     R5_Insync,  /* rdev && rdev->in_sync at start */
0307     R5_Wantread,    /* want to schedule a read */
0308     R5_Wantwrite,
0309     R5_Overlap, /* There is a pending overlapping request
0310              * on this block */
0311     R5_ReadNoMerge, /* prevent bio from merging in block-layer */
0312     R5_ReadError,   /* seen a read error here recently */
0313     R5_ReWrite, /* have tried to over-write the readerror */
0314 
0315     R5_Expanded,    /* This block now has post-expand data */
0316     R5_Wantcompute, /* compute_block in progress treat as
0317              * uptodate
0318              */
0319     R5_Wantfill,    /* dev->toread contains a bio that needs
0320              * filling
0321              */
0322     R5_Wantdrain,   /* dev->towrite needs to be drained */
0323     R5_WantFUA, /* Write should be FUA */
0324     R5_SyncIO,  /* The IO is sync */
0325     R5_WriteError,  /* got a write error - need to record it */
0326     R5_MadeGood,    /* A bad block has been fixed by writing to it */
0327     R5_ReadRepl,    /* Will/did read from replacement rather than orig */
0328     R5_MadeGoodRepl,/* A bad block on the replacement device has been
0329              * fixed by writing to it */
0330     R5_NeedReplace, /* This device has a replacement which is not
0331              * up-to-date at this stripe. */
0332     R5_WantReplace, /* We need to update the replacement, we have read
0333              * data in, and now is a good time to write it out.
0334              */
0335     R5_Discard, /* Discard the stripe */
0336     R5_SkipCopy,    /* Don't copy data from bio to stripe cache */
0337     R5_InJournal,   /* data being written is in the journal device.
0338              * if R5_InJournal is set for parity pd_idx, all the
0339              * data and parity being written are in the journal
0340              * device
0341              */
0342     R5_OrigPageUPTDODATE,   /* with write back cache, we read old data into
0343                  * dev->orig_page for prexor. When this flag is
0344                  * set, orig_page contains latest data in the
0345                  * raid disk.
0346                  */
0347 };
0348 
0349 /*
0350  * Stripe state
0351  */
0352 enum {
0353     STRIPE_ACTIVE,
0354     STRIPE_HANDLE,
0355     STRIPE_SYNC_REQUESTED,
0356     STRIPE_SYNCING,
0357     STRIPE_INSYNC,
0358     STRIPE_REPLACED,
0359     STRIPE_PREREAD_ACTIVE,
0360     STRIPE_DELAYED,
0361     STRIPE_DEGRADED,
0362     STRIPE_BIT_DELAY,
0363     STRIPE_EXPANDING,
0364     STRIPE_EXPAND_SOURCE,
0365     STRIPE_EXPAND_READY,
0366     STRIPE_IO_STARTED,  /* do not count towards 'bypass_count' */
0367     STRIPE_FULL_WRITE,  /* all blocks are set to be overwritten */
0368     STRIPE_BIOFILL_RUN,
0369     STRIPE_COMPUTE_RUN,
0370     STRIPE_ON_UNPLUG_LIST,
0371     STRIPE_DISCARD,
0372     STRIPE_ON_RELEASE_LIST,
0373     STRIPE_BATCH_READY,
0374     STRIPE_BATCH_ERR,
0375     STRIPE_BITMAP_PENDING,  /* Being added to bitmap, don't add
0376                  * to batch yet.
0377                  */
0378     STRIPE_LOG_TRAPPED, /* trapped into log (see raid5-cache.c)
0379                  * this bit is used in two scenarios:
0380                  *
0381                  * 1. write-out phase
0382                  *  set in first entry of r5l_write_stripe
0383                  *  clear in second entry of r5l_write_stripe
0384                  *  used to bypass logic in handle_stripe
0385                  *
0386                  * 2. caching phase
0387                  *  set in r5c_try_caching_write()
0388                  *  clear when journal write is done
0389                  *  used to initiate r5c_cache_data()
0390                  *  also used to bypass logic in handle_stripe
0391                  */
0392     STRIPE_R5C_CACHING, /* the stripe is in caching phase
0393                  * see more detail in the raid5-cache.c
0394                  */
0395     STRIPE_R5C_PARTIAL_STRIPE,  /* in r5c cache (to-be/being handled or
0396                      * in conf->r5c_partial_stripe_list)
0397                      */
0398     STRIPE_R5C_FULL_STRIPE, /* in r5c cache (to-be/being handled or
0399                  * in conf->r5c_full_stripe_list)
0400                  */
0401     STRIPE_R5C_PREFLUSH,    /* need to flush journal device */
0402 };
0403 
0404 #define STRIPE_EXPAND_SYNC_FLAGS \
0405     ((1 << STRIPE_EXPAND_SOURCE) |\
0406     (1 << STRIPE_EXPAND_READY) |\
0407     (1 << STRIPE_EXPANDING) |\
0408     (1 << STRIPE_SYNC_REQUESTED))
0409 /*
0410  * Operation request flags
0411  */
0412 enum {
0413     STRIPE_OP_BIOFILL,
0414     STRIPE_OP_COMPUTE_BLK,
0415     STRIPE_OP_PREXOR,
0416     STRIPE_OP_BIODRAIN,
0417     STRIPE_OP_RECONSTRUCT,
0418     STRIPE_OP_CHECK,
0419     STRIPE_OP_PARTIAL_PARITY,
0420 };
0421 
0422 /*
0423  * RAID parity calculation preferences
0424  */
0425 enum {
0426     PARITY_DISABLE_RMW = 0,
0427     PARITY_ENABLE_RMW,
0428     PARITY_PREFER_RMW,
0429 };
0430 
0431 /*
0432  * Pages requested from set_syndrome_sources()
0433  */
0434 enum {
0435     SYNDROME_SRC_ALL,
0436     SYNDROME_SRC_WANT_DRAIN,
0437     SYNDROME_SRC_WRITTEN,
0438 };
0439 /*
0440  * Plugging:
0441  *
0442  * To improve write throughput, we need to delay the handling of some
0443  * stripes until there has been a chance that several write requests
0444  * for the one stripe have all been collected.
0445  * In particular, any write request that would require pre-reading
0446  * is put on a "delayed" queue until there are no stripes currently
0447  * in a pre-read phase.  Further, if the "delayed" queue is empty when
0448  * a stripe is put on it then we "plug" the queue and do not process it
0449  * until an unplug call is made. (the unplug_io_fn() is called).
0450  *
0451  * When preread is initiated on a stripe, we set PREREAD_ACTIVE and add
0452  * it to the count of prereading stripes.
0453  * When write is initiated, or the stripe refcnt == 0 (just in case) we
0454  * clear the PREREAD_ACTIVE flag and decrement the count
0455  * Whenever the 'handle' queue is empty and the device is not plugged, we
0456  * move any strips from delayed to handle and clear the DELAYED flag and set
0457  * PREREAD_ACTIVE.
0458  * In stripe_handle, if we find pre-reading is necessary, we do it if
0459  * PREREAD_ACTIVE is set, else we set DELAYED which will send it to the delayed queue.
0460  * HANDLE gets cleared if stripe_handle leaves nothing locked.
0461  */
0462 
0463 /* Note: disk_info.rdev can be set to NULL asynchronously by raid5_remove_disk.
0464  * There are three safe ways to access disk_info.rdev.
0465  * 1/ when holding mddev->reconfig_mutex
0466  * 2/ when resync/recovery/reshape is known to be happening - i.e. in code that
0467  *    is called as part of performing resync/recovery/reshape.
0468  * 3/ while holding rcu_read_lock(), use rcu_dereference to get the pointer
0469  *    and if it is non-NULL, increment rdev->nr_pending before dropping the RCU
0470  *    lock.
0471  * When .rdev is set to NULL, the nr_pending count checked again and if
0472  * it has been incremented, the pointer is put back in .rdev.
0473  */
0474 
0475 struct disk_info {
0476     struct md_rdev  __rcu *rdev;
0477     struct md_rdev  __rcu *replacement;
0478     struct page *extra_page; /* extra page to use in prexor */
0479 };
0480 
0481 /*
0482  * Stripe cache
0483  */
0484 
0485 #define NR_STRIPES      256
0486 
0487 #if PAGE_SIZE == DEFAULT_STRIPE_SIZE
0488 #define STRIPE_SIZE     PAGE_SIZE
0489 #define STRIPE_SHIFT        (PAGE_SHIFT - 9)
0490 #define STRIPE_SECTORS      (STRIPE_SIZE>>9)
0491 #endif
0492 
0493 #define IO_THRESHOLD        1
0494 #define BYPASS_THRESHOLD    1
0495 #define NR_HASH         (PAGE_SIZE / sizeof(struct hlist_head))
0496 #define HASH_MASK       (NR_HASH - 1)
0497 #define MAX_STRIPE_BATCH    8
0498 
0499 /* NOTE NR_STRIPE_HASH_LOCKS must remain below 64.
0500  * This is because we sometimes take all the spinlocks
0501  * and creating that much locking depth can cause
0502  * problems.
0503  */
0504 #define NR_STRIPE_HASH_LOCKS 8
0505 #define STRIPE_HASH_LOCKS_MASK (NR_STRIPE_HASH_LOCKS - 1)
0506 
0507 struct r5worker {
0508     struct work_struct work;
0509     struct r5worker_group *group;
0510     struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
0511     bool working;
0512 };
0513 
0514 struct r5worker_group {
0515     struct list_head handle_list;
0516     struct list_head loprio_list;
0517     struct r5conf *conf;
0518     struct r5worker *workers;
0519     int stripes_cnt;
0520 };
0521 
0522 /*
0523  * r5c journal modes of the array: write-back or write-through.
0524  * write-through mode has identical behavior as existing log only
0525  * implementation.
0526  */
0527 enum r5c_journal_mode {
0528     R5C_JOURNAL_MODE_WRITE_THROUGH = 0,
0529     R5C_JOURNAL_MODE_WRITE_BACK = 1,
0530 };
0531 
0532 enum r5_cache_state {
0533     R5_INACTIVE_BLOCKED,    /* release of inactive stripes blocked,
0534                  * waiting for 25% to be free
0535                  */
0536     R5_ALLOC_MORE,      /* It might help to allocate another
0537                  * stripe.
0538                  */
0539     R5_DID_ALLOC,       /* A stripe was allocated, don't allocate
0540                  * more until at least one has been
0541                  * released.  This avoids flooding
0542                  * the cache.
0543                  */
0544     R5C_LOG_TIGHT,      /* log device space tight, need to
0545                  * prioritize stripes at last_checkpoint
0546                  */
0547     R5C_LOG_CRITICAL,   /* log device is running out of space,
0548                  * only process stripes that are already
0549                  * occupying the log
0550                  */
0551     R5C_EXTRA_PAGE_IN_USE,  /* a stripe is using disk_info.extra_page
0552                  * for prexor
0553                  */
0554 };
0555 
0556 #define PENDING_IO_MAX 512
0557 #define PENDING_IO_ONE_FLUSH 128
0558 struct r5pending_data {
0559     struct list_head sibling;
0560     sector_t sector; /* stripe sector */
0561     struct bio_list bios;
0562 };
0563 
0564 struct raid5_percpu {
0565     struct page *spare_page; /* Used when checking P/Q in raid6 */
0566     void        *scribble;  /* space for constructing buffer
0567                      * lists and performing address
0568                      * conversions
0569                      */
0570     int             scribble_obj_size;
0571     local_lock_t    lock;
0572 };
0573 
0574 struct r5conf {
0575     struct hlist_head   *stripe_hashtbl;
0576     /* only protect corresponding hash list and inactive_list */
0577     spinlock_t      hash_locks[NR_STRIPE_HASH_LOCKS];
0578     struct mddev        *mddev;
0579     int         chunk_sectors;
0580     int         level, algorithm, rmw_level;
0581     int         max_degraded;
0582     int         raid_disks;
0583     int         max_nr_stripes;
0584     int         min_nr_stripes;
0585 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
0586     unsigned long   stripe_size;
0587     unsigned int    stripe_shift;
0588     unsigned long   stripe_sectors;
0589 #endif
0590 
0591     /* reshape_progress is the leading edge of a 'reshape'
0592      * It has value MaxSector when no reshape is happening
0593      * If delta_disks < 0, it is the last sector we started work on,
0594      * else is it the next sector to work on.
0595      */
0596     sector_t        reshape_progress;
0597     /* reshape_safe is the trailing edge of a reshape.  We know that
0598      * before (or after) this address, all reshape has completed.
0599      */
0600     sector_t        reshape_safe;
0601     int         previous_raid_disks;
0602     int         prev_chunk_sectors;
0603     int         prev_algo;
0604     short           generation; /* increments with every reshape */
0605     seqcount_spinlock_t gen_lock;   /* lock against generation changes */
0606     unsigned long       reshape_checkpoint; /* Time we last updated
0607                              * metadata */
0608     long long       min_offset_diff; /* minimum difference between
0609                           * data_offset and
0610                           * new_data_offset across all
0611                           * devices.  May be negative,
0612                           * but is closest to zero.
0613                           */
0614 
0615     struct list_head    handle_list; /* stripes needing handling */
0616     struct list_head    loprio_list; /* low priority stripes */
0617     struct list_head    hold_list; /* preread ready stripes */
0618     struct list_head    delayed_list; /* stripes that have plugged requests */
0619     struct list_head    bitmap_list; /* stripes delaying awaiting bitmap update */
0620     struct bio      *retry_read_aligned; /* currently retrying aligned bios   */
0621     unsigned int        retry_read_offset; /* sector offset into retry_read_aligned */
0622     struct bio      *retry_read_aligned_list; /* aligned bios retry list  */
0623     atomic_t        preread_active_stripes; /* stripes with scheduled io */
0624     atomic_t        active_aligned_reads;
0625     atomic_t        pending_full_writes; /* full write backlog */
0626     int         bypass_count; /* bypassed prereads */
0627     int         bypass_threshold; /* preread nice */
0628     int         skip_copy; /* Don't copy data from bio to stripe cache */
0629     struct list_head    *last_hold; /* detect hold_list promotions */
0630 
0631     atomic_t        reshape_stripes; /* stripes with pending writes for reshape */
0632     /* unfortunately we need two cache names as we temporarily have
0633      * two caches.
0634      */
0635     int         active_name;
0636     char            cache_name[2][32];
0637     struct kmem_cache   *slab_cache; /* for allocating stripes */
0638     struct mutex        cache_size_mutex; /* Protect changes to cache size */
0639 
0640     int         seq_flush, seq_write;
0641     int         quiesce;
0642 
0643     int         fullsync;  /* set to 1 if a full sync is needed,
0644                         * (fresh device added).
0645                         * Cleared when a sync completes.
0646                         */
0647     int         recovery_disabled;
0648     /* per cpu variables */
0649     struct raid5_percpu __percpu *percpu;
0650     int scribble_disks;
0651     int scribble_sectors;
0652     struct hlist_node node;
0653 
0654     /*
0655      * Free stripes pool
0656      */
0657     atomic_t        active_stripes;
0658     struct list_head    inactive_list[NR_STRIPE_HASH_LOCKS];
0659 
0660     atomic_t        r5c_cached_full_stripes;
0661     struct list_head    r5c_full_stripe_list;
0662     atomic_t        r5c_cached_partial_stripes;
0663     struct list_head    r5c_partial_stripe_list;
0664     atomic_t        r5c_flushing_full_stripes;
0665     atomic_t        r5c_flushing_partial_stripes;
0666 
0667     atomic_t        empty_inactive_list_nr;
0668     struct llist_head   released_stripes;
0669     wait_queue_head_t   wait_for_quiescent;
0670     wait_queue_head_t   wait_for_stripe;
0671     wait_queue_head_t   wait_for_overlap;
0672     unsigned long       cache_state;
0673     struct shrinker     shrinker;
0674     int         pool_size; /* number of disks in stripeheads in pool */
0675     spinlock_t      device_lock;
0676     struct disk_info    *disks;
0677     struct bio_set      bio_split;
0678 
0679     /* When taking over an array from a different personality, we store
0680      * the new thread here until we fully activate the array.
0681      */
0682     struct md_thread    *thread;
0683     struct list_head    temp_inactive_list[NR_STRIPE_HASH_LOCKS];
0684     struct r5worker_group   *worker_groups;
0685     int         group_cnt;
0686     int         worker_cnt_per_group;
0687     struct r5l_log      *log;
0688     void            *log_private;
0689 
0690     spinlock_t      pending_bios_lock;
0691     bool            batch_bio_dispatch;
0692     struct r5pending_data   *pending_data;
0693     struct list_head    free_list;
0694     struct list_head    pending_list;
0695     int         pending_data_cnt;
0696     struct r5pending_data   *next_pending_data;
0697 };
0698 
0699 #if PAGE_SIZE == DEFAULT_STRIPE_SIZE
0700 #define RAID5_STRIPE_SIZE(conf) STRIPE_SIZE
0701 #define RAID5_STRIPE_SHIFT(conf)    STRIPE_SHIFT
0702 #define RAID5_STRIPE_SECTORS(conf)  STRIPE_SECTORS
0703 #else
0704 #define RAID5_STRIPE_SIZE(conf) ((conf)->stripe_size)
0705 #define RAID5_STRIPE_SHIFT(conf)    ((conf)->stripe_shift)
0706 #define RAID5_STRIPE_SECTORS(conf)  ((conf)->stripe_sectors)
0707 #endif
0708 
0709 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
0710  * order without overlap.  There may be several bio's per stripe+device, and
0711  * a bio could span several devices.
0712  * When walking this list for a particular stripe+device, we must never proceed
0713  * beyond a bio that extends past this device, as the next bio might no longer
0714  * be valid.
0715  * This function is used to determine the 'next' bio in the list, given the
0716  * sector of the current stripe+device
0717  */
0718 static inline struct bio *r5_next_bio(struct r5conf *conf, struct bio *bio, sector_t sector)
0719 {
0720     if (bio_end_sector(bio) < sector + RAID5_STRIPE_SECTORS(conf))
0721         return bio->bi_next;
0722     else
0723         return NULL;
0724 }
0725 
0726 /*
0727  * Our supported algorithms
0728  */
0729 #define ALGORITHM_LEFT_ASYMMETRIC   0 /* Rotating Parity N with Data Restart */
0730 #define ALGORITHM_RIGHT_ASYMMETRIC  1 /* Rotating Parity 0 with Data Restart */
0731 #define ALGORITHM_LEFT_SYMMETRIC    2 /* Rotating Parity N with Data Continuation */
0732 #define ALGORITHM_RIGHT_SYMMETRIC   3 /* Rotating Parity 0 with Data Continuation */
0733 
0734 /* Define non-rotating (raid4) algorithms.  These allow
0735  * conversion of raid4 to raid5.
0736  */
0737 #define ALGORITHM_PARITY_0      4 /* P or P,Q are initial devices */
0738 #define ALGORITHM_PARITY_N      5 /* P or P,Q are final devices. */
0739 
0740 /* DDF RAID6 layouts differ from md/raid6 layouts in two ways.
0741  * Firstly, the exact positioning of the parity block is slightly
0742  * different between the 'LEFT_*' modes of md and the "_N_*" modes
0743  * of DDF.
0744  * Secondly, or order of datablocks over which the Q syndrome is computed
0745  * is different.
0746  * Consequently we have different layouts for DDF/raid6 than md/raid6.
0747  * These layouts are from the DDFv1.2 spec.
0748  * Interestingly DDFv1.2-Errata-A does not specify N_CONTINUE but
0749  * leaves RLQ=3 as 'Vendor Specific'
0750  */
0751 
0752 #define ALGORITHM_ROTATING_ZERO_RESTART 8 /* DDF PRL=6 RLQ=1 */
0753 #define ALGORITHM_ROTATING_N_RESTART    9 /* DDF PRL=6 RLQ=2 */
0754 #define ALGORITHM_ROTATING_N_CONTINUE   10 /*DDF PRL=6 RLQ=3 */
0755 
0756 /* For every RAID5 algorithm we define a RAID6 algorithm
0757  * with exactly the same layout for data and parity, and
0758  * with the Q block always on the last device (N-1).
0759  * This allows trivial conversion from RAID5 to RAID6
0760  */
0761 #define ALGORITHM_LEFT_ASYMMETRIC_6 16
0762 #define ALGORITHM_RIGHT_ASYMMETRIC_6    17
0763 #define ALGORITHM_LEFT_SYMMETRIC_6  18
0764 #define ALGORITHM_RIGHT_SYMMETRIC_6 19
0765 #define ALGORITHM_PARITY_0_6        20
0766 #define ALGORITHM_PARITY_N_6        ALGORITHM_PARITY_N
0767 
0768 static inline int algorithm_valid_raid5(int layout)
0769 {
0770     return (layout >= 0) &&
0771         (layout <= 5);
0772 }
0773 static inline int algorithm_valid_raid6(int layout)
0774 {
0775     return (layout >= 0 && layout <= 5)
0776         ||
0777         (layout >= 8 && layout <= 10)
0778         ||
0779         (layout >= 16 && layout <= 20);
0780 }
0781 
0782 static inline int algorithm_is_DDF(int layout)
0783 {
0784     return layout >= 8 && layout <= 10;
0785 }
0786 
0787 #if PAGE_SIZE != DEFAULT_STRIPE_SIZE
0788 /*
0789  * Return offset of the corresponding page for r5dev.
0790  */
0791 static inline int raid5_get_page_offset(struct stripe_head *sh, int disk_idx)
0792 {
0793     return (disk_idx % sh->stripes_per_page) * RAID5_STRIPE_SIZE(sh->raid_conf);
0794 }
0795 
0796 /*
0797  * Return corresponding page address for r5dev.
0798  */
0799 static inline struct page *
0800 raid5_get_dev_page(struct stripe_head *sh, int disk_idx)
0801 {
0802     return sh->pages[disk_idx / sh->stripes_per_page];
0803 }
0804 #endif
0805 
0806 extern void md_raid5_kick_device(struct r5conf *conf);
0807 extern int raid5_set_cache_size(struct mddev *mddev, int size);
0808 extern sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous);
0809 extern void raid5_release_stripe(struct stripe_head *sh);
0810 extern sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
0811                      int previous, int *dd_idx,
0812                      struct stripe_head *sh);
0813 extern struct stripe_head *
0814 raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
0815             bool previous, bool noblock, bool noquiesce);
0816 extern int raid5_calc_degraded(struct r5conf *conf);
0817 extern int r5c_journal_mode_set(struct mddev *mddev, int journal_mode);
0818 #endif