raid5.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783
  1. /*
  2. * raid5.c : Multiple Devices driver for Linux
  3. * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  4. * Copyright (C) 1999, 2000 Ingo Molnar
  5. * Copyright (C) 2002, 2003 H. Peter Anvin
  6. *
  7. * RAID-4/5/6 management functions.
  8. * Thanks to Penguin Computing for making the RAID-6 development possible
  9. * by donating a test server!
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * (for example /usr/src/linux/COPYING); if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /*
  21. * BITMAP UNPLUGGING:
  22. *
  23. * The sequencing for updating the bitmap reliably is a little
  24. * subtle (and I got it wrong the first time) so it deserves some
  25. * explanation.
  26. *
  27. * We group bitmap updates into batches. Each batch has a number.
  28. * We may write out several batches at once, but that isn't very important.
  29. * conf->bm_write is the number of the last batch successfully written.
  30. * conf->bm_flush is the number of the last batch that was closed to
  31. * new additions.
  32. * When we discover that we will need to write to any block in a stripe
  33. * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
  34. * the number of the batch it will be in. This is bm_flush+1.
  35. * When we are ready to do a write, if that batch hasn't been written yet,
  36. * we plug the array and queue the stripe for later.
  37. * When an unplug happens, we increment bm_flush, thus closing the current
  38. * batch.
  39. * When we notice that bm_flush > bm_write, we write out all pending updates
  40. * to the bitmap, and advance bm_write to where bm_flush was.
  41. * This may occasionally write a bit out twice, but is sure never to
  42. * miss any bits.
  43. */
  44. #include <linux/module.h>
  45. #include <linux/slab.h>
  46. #include <linux/highmem.h>
  47. #include <linux/bitops.h>
  48. #include <linux/kthread.h>
  49. #include <asm/atomic.h>
  50. #include "raid6.h"
  51. #include <linux/raid/bitmap.h>
  52. /*
  53. * Stripe cache
  54. */
  55. #define NR_STRIPES 256
  56. #define STRIPE_SIZE PAGE_SIZE
  57. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  58. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  59. #define IO_THRESHOLD 1
  60. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  61. #define HASH_MASK (NR_HASH - 1)
  62. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  63. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  64. * order without overlap. There may be several bio's per stripe+device, and
  65. * a bio could span several devices.
  66. * When walking this list for a particular stripe+device, we must never proceed
  67. * beyond a bio that extends past this device, as the next bio might no longer
  68. * be valid.
  69. * This macro is used to determine the 'next' bio in the list, given the sector
  70. * of the current stripe+device
  71. */
  72. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  73. /*
  74. * The following can be used to debug the driver
  75. */
  76. #define RAID5_DEBUG 0
  77. #define RAID5_PARANOIA 1
  78. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  79. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  80. #else
  81. # define CHECK_DEVLOCK()
  82. #endif
  83. #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
  84. #if RAID5_DEBUG
  85. #define inline
  86. #define __inline__
  87. #endif
  88. #if !RAID6_USE_EMPTY_ZERO_PAGE
  89. /* In .bss so it's zeroed */
  90. const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
  91. #endif
  92. static inline int raid6_next_disk(int disk, int raid_disks)
  93. {
  94. disk++;
  95. return (disk < raid_disks) ? disk : 0;
  96. }
  97. static void print_raid5_conf (raid5_conf_t *conf);
  98. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  99. {
  100. if (atomic_dec_and_test(&sh->count)) {
  101. BUG_ON(!list_empty(&sh->lru));
  102. BUG_ON(atomic_read(&conf->active_stripes)==0);
  103. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  104. if (test_bit(STRIPE_DELAYED, &sh->state)) {
  105. list_add_tail(&sh->lru, &conf->delayed_list);
  106. blk_plug_device(conf->mddev->queue);
  107. } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  108. sh->bm_seq - conf->seq_write > 0) {
  109. list_add_tail(&sh->lru, &conf->bitmap_list);
  110. blk_plug_device(conf->mddev->queue);
  111. } else {
  112. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  113. list_add_tail(&sh->lru, &conf->handle_list);
  114. }
  115. md_wakeup_thread(conf->mddev->thread);
  116. } else {
  117. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  118. atomic_dec(&conf->preread_active_stripes);
  119. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  120. md_wakeup_thread(conf->mddev->thread);
  121. }
  122. atomic_dec(&conf->active_stripes);
  123. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  124. list_add_tail(&sh->lru, &conf->inactive_list);
  125. wake_up(&conf->wait_for_stripe);
  126. }
  127. }
  128. }
  129. }
  130. static void release_stripe(struct stripe_head *sh)
  131. {
  132. raid5_conf_t *conf = sh->raid_conf;
  133. unsigned long flags;
  134. spin_lock_irqsave(&conf->device_lock, flags);
  135. __release_stripe(conf, sh);
  136. spin_unlock_irqrestore(&conf->device_lock, flags);
  137. }
  138. static inline void remove_hash(struct stripe_head *sh)
  139. {
  140. PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  141. hlist_del_init(&sh->hash);
  142. }
  143. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  144. {
  145. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  146. PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  147. CHECK_DEVLOCK();
  148. hlist_add_head(&sh->hash, hp);
  149. }
  150. /* find an idle stripe, make sure it is unhashed, and return it. */
  151. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  152. {
  153. struct stripe_head *sh = NULL;
  154. struct list_head *first;
  155. CHECK_DEVLOCK();
  156. if (list_empty(&conf->inactive_list))
  157. goto out;
  158. first = conf->inactive_list.next;
  159. sh = list_entry(first, struct stripe_head, lru);
  160. list_del_init(first);
  161. remove_hash(sh);
  162. atomic_inc(&conf->active_stripes);
  163. out:
  164. return sh;
  165. }
  166. static void shrink_buffers(struct stripe_head *sh, int num)
  167. {
  168. struct page *p;
  169. int i;
  170. for (i=0; i<num ; i++) {
  171. p = sh->dev[i].page;
  172. if (!p)
  173. continue;
  174. sh->dev[i].page = NULL;
  175. put_page(p);
  176. }
  177. }
  178. static int grow_buffers(struct stripe_head *sh, int num)
  179. {
  180. int i;
  181. for (i=0; i<num; i++) {
  182. struct page *page;
  183. if (!(page = alloc_page(GFP_KERNEL))) {
  184. return 1;
  185. }
  186. sh->dev[i].page = page;
  187. }
  188. return 0;
  189. }
  190. static void raid5_build_block (struct stripe_head *sh, int i);
  191. static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
  192. {
  193. raid5_conf_t *conf = sh->raid_conf;
  194. int i;
  195. BUG_ON(atomic_read(&sh->count) != 0);
  196. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  197. CHECK_DEVLOCK();
  198. PRINTK("init_stripe called, stripe %llu\n",
  199. (unsigned long long)sh->sector);
  200. remove_hash(sh);
  201. sh->sector = sector;
  202. sh->pd_idx = pd_idx;
  203. sh->state = 0;
  204. sh->disks = disks;
  205. for (i = sh->disks; i--; ) {
  206. struct r5dev *dev = &sh->dev[i];
  207. if (dev->toread || dev->towrite || dev->written ||
  208. test_bit(R5_LOCKED, &dev->flags)) {
  209. printk("sector=%llx i=%d %p %p %p %d\n",
  210. (unsigned long long)sh->sector, i, dev->toread,
  211. dev->towrite, dev->written,
  212. test_bit(R5_LOCKED, &dev->flags));
  213. BUG();
  214. }
  215. dev->flags = 0;
  216. raid5_build_block(sh, i);
  217. }
  218. insert_hash(conf, sh);
  219. }
  220. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
  221. {
  222. struct stripe_head *sh;
  223. struct hlist_node *hn;
  224. CHECK_DEVLOCK();
  225. PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
  226. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  227. if (sh->sector == sector && sh->disks == disks)
  228. return sh;
  229. PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
  230. return NULL;
  231. }
  232. static void unplug_slaves(mddev_t *mddev);
  233. static void raid5_unplug_device(request_queue_t *q);
  234. static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
  235. int pd_idx, int noblock)
  236. {
  237. struct stripe_head *sh;
  238. PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
  239. spin_lock_irq(&conf->device_lock);
  240. do {
  241. wait_event_lock_irq(conf->wait_for_stripe,
  242. conf->quiesce == 0,
  243. conf->device_lock, /* nothing */);
  244. sh = __find_stripe(conf, sector, disks);
  245. if (!sh) {
  246. if (!conf->inactive_blocked)
  247. sh = get_free_stripe(conf);
  248. if (noblock && sh == NULL)
  249. break;
  250. if (!sh) {
  251. conf->inactive_blocked = 1;
  252. wait_event_lock_irq(conf->wait_for_stripe,
  253. !list_empty(&conf->inactive_list) &&
  254. (atomic_read(&conf->active_stripes)
  255. < (conf->max_nr_stripes *3/4)
  256. || !conf->inactive_blocked),
  257. conf->device_lock,
  258. raid5_unplug_device(conf->mddev->queue)
  259. );
  260. conf->inactive_blocked = 0;
  261. } else
  262. init_stripe(sh, sector, pd_idx, disks);
  263. } else {
  264. if (atomic_read(&sh->count)) {
  265. BUG_ON(!list_empty(&sh->lru));
  266. } else {
  267. if (!test_bit(STRIPE_HANDLE, &sh->state))
  268. atomic_inc(&conf->active_stripes);
  269. if (list_empty(&sh->lru) &&
  270. !test_bit(STRIPE_EXPANDING, &sh->state))
  271. BUG();
  272. list_del_init(&sh->lru);
  273. }
  274. }
  275. } while (sh == NULL);
  276. if (sh)
  277. atomic_inc(&sh->count);
  278. spin_unlock_irq(&conf->device_lock);
  279. return sh;
  280. }
  281. static int grow_one_stripe(raid5_conf_t *conf)
  282. {
  283. struct stripe_head *sh;
  284. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  285. if (!sh)
  286. return 0;
  287. memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
  288. sh->raid_conf = conf;
  289. spin_lock_init(&sh->lock);
  290. if (grow_buffers(sh, conf->raid_disks)) {
  291. shrink_buffers(sh, conf->raid_disks);
  292. kmem_cache_free(conf->slab_cache, sh);
  293. return 0;
  294. }
  295. sh->disks = conf->raid_disks;
  296. /* we just created an active stripe so... */
  297. atomic_set(&sh->count, 1);
  298. atomic_inc(&conf->active_stripes);
  299. INIT_LIST_HEAD(&sh->lru);
  300. release_stripe(sh);
  301. return 1;
  302. }
  303. static int grow_stripes(raid5_conf_t *conf, int num)
  304. {
  305. kmem_cache_t *sc;
  306. int devs = conf->raid_disks;
  307. sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
  308. sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
  309. conf->active_name = 0;
  310. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  311. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  312. 0, 0, NULL, NULL);
  313. if (!sc)
  314. return 1;
  315. conf->slab_cache = sc;
  316. conf->pool_size = devs;
  317. while (num--)
  318. if (!grow_one_stripe(conf))
  319. return 1;
  320. return 0;
  321. }
  322. #ifdef CONFIG_MD_RAID5_RESHAPE
  323. static int resize_stripes(raid5_conf_t *conf, int newsize)
  324. {
  325. /* Make all the stripes able to hold 'newsize' devices.
  326. * New slots in each stripe get 'page' set to a new page.
  327. *
  328. * This happens in stages:
  329. * 1/ create a new kmem_cache and allocate the required number of
  330. * stripe_heads.
  331. * 2/ gather all the old stripe_heads and tranfer the pages across
  332. * to the new stripe_heads. This will have the side effect of
  333. * freezing the array as once all stripe_heads have been collected,
  334. * no IO will be possible. Old stripe heads are freed once their
  335. * pages have been transferred over, and the old kmem_cache is
  336. * freed when all stripes are done.
  337. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  338. * we simple return a failre status - no need to clean anything up.
  339. * 4/ allocate new pages for the new slots in the new stripe_heads.
  340. * If this fails, we don't bother trying the shrink the
  341. * stripe_heads down again, we just leave them as they are.
  342. * As each stripe_head is processed the new one is released into
  343. * active service.
  344. *
  345. * Once step2 is started, we cannot afford to wait for a write,
  346. * so we use GFP_NOIO allocations.
  347. */
  348. struct stripe_head *osh, *nsh;
  349. LIST_HEAD(newstripes);
  350. struct disk_info *ndisks;
  351. int err = 0;
  352. kmem_cache_t *sc;
  353. int i;
  354. if (newsize <= conf->pool_size)
  355. return 0; /* never bother to shrink */
  356. /* Step 1 */
  357. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  358. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  359. 0, 0, NULL, NULL);
  360. if (!sc)
  361. return -ENOMEM;
  362. for (i = conf->max_nr_stripes; i; i--) {
  363. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  364. if (!nsh)
  365. break;
  366. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  367. nsh->raid_conf = conf;
  368. spin_lock_init(&nsh->lock);
  369. list_add(&nsh->lru, &newstripes);
  370. }
  371. if (i) {
  372. /* didn't get enough, give up */
  373. while (!list_empty(&newstripes)) {
  374. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  375. list_del(&nsh->lru);
  376. kmem_cache_free(sc, nsh);
  377. }
  378. kmem_cache_destroy(sc);
  379. return -ENOMEM;
  380. }
  381. /* Step 2 - Must use GFP_NOIO now.
  382. * OK, we have enough stripes, start collecting inactive
  383. * stripes and copying them over
  384. */
  385. list_for_each_entry(nsh, &newstripes, lru) {
  386. spin_lock_irq(&conf->device_lock);
  387. wait_event_lock_irq(conf->wait_for_stripe,
  388. !list_empty(&conf->inactive_list),
  389. conf->device_lock,
  390. unplug_slaves(conf->mddev)
  391. );
  392. osh = get_free_stripe(conf);
  393. spin_unlock_irq(&conf->device_lock);
  394. atomic_set(&nsh->count, 1);
  395. for(i=0; i<conf->pool_size; i++)
  396. nsh->dev[i].page = osh->dev[i].page;
  397. for( ; i<newsize; i++)
  398. nsh->dev[i].page = NULL;
  399. kmem_cache_free(conf->slab_cache, osh);
  400. }
  401. kmem_cache_destroy(conf->slab_cache);
  402. /* Step 3.
  403. * At this point, we are holding all the stripes so the array
  404. * is completely stalled, so now is a good time to resize
  405. * conf->disks.
  406. */
  407. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  408. if (ndisks) {
  409. for (i=0; i<conf->raid_disks; i++)
  410. ndisks[i] = conf->disks[i];
  411. kfree(conf->disks);
  412. conf->disks = ndisks;
  413. } else
  414. err = -ENOMEM;
  415. /* Step 4, return new stripes to service */
  416. while(!list_empty(&newstripes)) {
  417. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  418. list_del_init(&nsh->lru);
  419. for (i=conf->raid_disks; i < newsize; i++)
  420. if (nsh->dev[i].page == NULL) {
  421. struct page *p = alloc_page(GFP_NOIO);
  422. nsh->dev[i].page = p;
  423. if (!p)
  424. err = -ENOMEM;
  425. }
  426. release_stripe(nsh);
  427. }
  428. /* critical section pass, GFP_NOIO no longer needed */
  429. conf->slab_cache = sc;
  430. conf->active_name = 1-conf->active_name;
  431. conf->pool_size = newsize;
  432. return err;
  433. }
  434. #endif
  435. static int drop_one_stripe(raid5_conf_t *conf)
  436. {
  437. struct stripe_head *sh;
  438. spin_lock_irq(&conf->device_lock);
  439. sh = get_free_stripe(conf);
  440. spin_unlock_irq(&conf->device_lock);
  441. if (!sh)
  442. return 0;
  443. BUG_ON(atomic_read(&sh->count));
  444. shrink_buffers(sh, conf->pool_size);
  445. kmem_cache_free(conf->slab_cache, sh);
  446. atomic_dec(&conf->active_stripes);
  447. return 1;
  448. }
  449. static void shrink_stripes(raid5_conf_t *conf)
  450. {
  451. while (drop_one_stripe(conf))
  452. ;
  453. if (conf->slab_cache)
  454. kmem_cache_destroy(conf->slab_cache);
  455. conf->slab_cache = NULL;
  456. }
  457. static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
  458. int error)
  459. {
  460. struct stripe_head *sh = bi->bi_private;
  461. raid5_conf_t *conf = sh->raid_conf;
  462. int disks = sh->disks, i;
  463. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  464. char b[BDEVNAME_SIZE];
  465. mdk_rdev_t *rdev;
  466. if (bi->bi_size)
  467. return 1;
  468. for (i=0 ; i<disks; i++)
  469. if (bi == &sh->dev[i].req)
  470. break;
  471. PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  472. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  473. uptodate);
  474. if (i == disks) {
  475. BUG();
  476. return 0;
  477. }
  478. if (uptodate) {
  479. #if 0
  480. struct bio *bio;
  481. unsigned long flags;
  482. spin_lock_irqsave(&conf->device_lock, flags);
  483. /* we can return a buffer if we bypassed the cache or
  484. * if the top buffer is not in highmem. If there are
  485. * multiple buffers, leave the extra work to
  486. * handle_stripe
  487. */
  488. buffer = sh->bh_read[i];
  489. if (buffer &&
  490. (!PageHighMem(buffer->b_page)
  491. || buffer->b_page == bh->b_page )
  492. ) {
  493. sh->bh_read[i] = buffer->b_reqnext;
  494. buffer->b_reqnext = NULL;
  495. } else
  496. buffer = NULL;
  497. spin_unlock_irqrestore(&conf->device_lock, flags);
  498. if (sh->bh_page[i]==bh->b_page)
  499. set_buffer_uptodate(bh);
  500. if (buffer) {
  501. if (buffer->b_page != bh->b_page)
  502. memcpy(buffer->b_data, bh->b_data, bh->b_size);
  503. buffer->b_end_io(buffer, 1);
  504. }
  505. #else
  506. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  507. #endif
  508. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  509. rdev = conf->disks[i].rdev;
  510. printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
  511. mdname(conf->mddev), STRIPE_SECTORS,
  512. (unsigned long long)sh->sector + rdev->data_offset,
  513. bdevname(rdev->bdev, b));
  514. clear_bit(R5_ReadError, &sh->dev[i].flags);
  515. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  516. }
  517. if (atomic_read(&conf->disks[i].rdev->read_errors))
  518. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  519. } else {
  520. const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
  521. int retry = 0;
  522. rdev = conf->disks[i].rdev;
  523. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  524. atomic_inc(&rdev->read_errors);
  525. if (conf->mddev->degraded)
  526. printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n",
  527. mdname(conf->mddev),
  528. (unsigned long long)sh->sector + rdev->data_offset,
  529. bdn);
  530. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  531. /* Oh, no!!! */
  532. printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
  533. mdname(conf->mddev),
  534. (unsigned long long)sh->sector + rdev->data_offset,
  535. bdn);
  536. else if (atomic_read(&rdev->read_errors)
  537. > conf->max_nr_stripes)
  538. printk(KERN_WARNING
  539. "raid5:%s: Too many read errors, failing device %s.\n",
  540. mdname(conf->mddev), bdn);
  541. else
  542. retry = 1;
  543. if (retry)
  544. set_bit(R5_ReadError, &sh->dev[i].flags);
  545. else {
  546. clear_bit(R5_ReadError, &sh->dev[i].flags);
  547. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  548. md_error(conf->mddev, rdev);
  549. }
  550. }
  551. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  552. #if 0
  553. /* must restore b_page before unlocking buffer... */
  554. if (sh->bh_page[i] != bh->b_page) {
  555. bh->b_page = sh->bh_page[i];
  556. bh->b_data = page_address(bh->b_page);
  557. clear_buffer_uptodate(bh);
  558. }
  559. #endif
  560. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  561. set_bit(STRIPE_HANDLE, &sh->state);
  562. release_stripe(sh);
  563. return 0;
  564. }
  565. static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
  566. int error)
  567. {
  568. struct stripe_head *sh = bi->bi_private;
  569. raid5_conf_t *conf = sh->raid_conf;
  570. int disks = sh->disks, i;
  571. unsigned long flags;
  572. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  573. if (bi->bi_size)
  574. return 1;
  575. for (i=0 ; i<disks; i++)
  576. if (bi == &sh->dev[i].req)
  577. break;
  578. PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  579. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  580. uptodate);
  581. if (i == disks) {
  582. BUG();
  583. return 0;
  584. }
  585. spin_lock_irqsave(&conf->device_lock, flags);
  586. if (!uptodate)
  587. md_error(conf->mddev, conf->disks[i].rdev);
  588. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  589. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  590. set_bit(STRIPE_HANDLE, &sh->state);
  591. __release_stripe(conf, sh);
  592. spin_unlock_irqrestore(&conf->device_lock, flags);
  593. return 0;
  594. }
  595. static sector_t compute_blocknr(struct stripe_head *sh, int i);
  596. static void raid5_build_block (struct stripe_head *sh, int i)
  597. {
  598. struct r5dev *dev = &sh->dev[i];
  599. bio_init(&dev->req);
  600. dev->req.bi_io_vec = &dev->vec;
  601. dev->req.bi_vcnt++;
  602. dev->req.bi_max_vecs++;
  603. dev->vec.bv_page = dev->page;
  604. dev->vec.bv_len = STRIPE_SIZE;
  605. dev->vec.bv_offset = 0;
  606. dev->req.bi_sector = sh->sector;
  607. dev->req.bi_private = sh;
  608. dev->flags = 0;
  609. dev->sector = compute_blocknr(sh, i);
  610. }
  611. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  612. {
  613. char b[BDEVNAME_SIZE];
  614. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  615. PRINTK("raid5: error called\n");
  616. if (!test_bit(Faulty, &rdev->flags)) {
  617. mddev->sb_dirty = 1;
  618. if (test_bit(In_sync, &rdev->flags)) {
  619. conf->working_disks--;
  620. mddev->degraded++;
  621. conf->failed_disks++;
  622. clear_bit(In_sync, &rdev->flags);
  623. /*
  624. * if recovery was running, make sure it aborts.
  625. */
  626. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  627. }
  628. set_bit(Faulty, &rdev->flags);
  629. printk (KERN_ALERT
  630. "raid5: Disk failure on %s, disabling device."
  631. " Operation continuing on %d devices\n",
  632. bdevname(rdev->bdev,b), conf->working_disks);
  633. }
  634. }
  635. /*
  636. * Input: a 'big' sector number,
  637. * Output: index of the data and parity disk, and the sector # in them.
  638. */
  639. static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
  640. unsigned int data_disks, unsigned int * dd_idx,
  641. unsigned int * pd_idx, raid5_conf_t *conf)
  642. {
  643. long stripe;
  644. unsigned long chunk_number;
  645. unsigned int chunk_offset;
  646. sector_t new_sector;
  647. int sectors_per_chunk = conf->chunk_size >> 9;
  648. /* First compute the information on this sector */
  649. /*
  650. * Compute the chunk number and the sector offset inside the chunk
  651. */
  652. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  653. chunk_number = r_sector;
  654. BUG_ON(r_sector != chunk_number);
  655. /*
  656. * Compute the stripe number
  657. */
  658. stripe = chunk_number / data_disks;
  659. /*
  660. * Compute the data disk and parity disk indexes inside the stripe
  661. */
  662. *dd_idx = chunk_number % data_disks;
  663. /*
  664. * Select the parity disk based on the user selected algorithm.
  665. */
  666. switch(conf->level) {
  667. case 4:
  668. *pd_idx = data_disks;
  669. break;
  670. case 5:
  671. switch (conf->algorithm) {
  672. case ALGORITHM_LEFT_ASYMMETRIC:
  673. *pd_idx = data_disks - stripe % raid_disks;
  674. if (*dd_idx >= *pd_idx)
  675. (*dd_idx)++;
  676. break;
  677. case ALGORITHM_RIGHT_ASYMMETRIC:
  678. *pd_idx = stripe % raid_disks;
  679. if (*dd_idx >= *pd_idx)
  680. (*dd_idx)++;
  681. break;
  682. case ALGORITHM_LEFT_SYMMETRIC:
  683. *pd_idx = data_disks - stripe % raid_disks;
  684. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  685. break;
  686. case ALGORITHM_RIGHT_SYMMETRIC:
  687. *pd_idx = stripe % raid_disks;
  688. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  689. break;
  690. default:
  691. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  692. conf->algorithm);
  693. }
  694. break;
  695. case 6:
  696. /**** FIX THIS ****/
  697. switch (conf->algorithm) {
  698. case ALGORITHM_LEFT_ASYMMETRIC:
  699. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  700. if (*pd_idx == raid_disks-1)
  701. (*dd_idx)++; /* Q D D D P */
  702. else if (*dd_idx >= *pd_idx)
  703. (*dd_idx) += 2; /* D D P Q D */
  704. break;
  705. case ALGORITHM_RIGHT_ASYMMETRIC:
  706. *pd_idx = stripe % raid_disks;
  707. if (*pd_idx == raid_disks-1)
  708. (*dd_idx)++; /* Q D D D P */
  709. else if (*dd_idx >= *pd_idx)
  710. (*dd_idx) += 2; /* D D P Q D */
  711. break;
  712. case ALGORITHM_LEFT_SYMMETRIC:
  713. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  714. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  715. break;
  716. case ALGORITHM_RIGHT_SYMMETRIC:
  717. *pd_idx = stripe % raid_disks;
  718. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  719. break;
  720. default:
  721. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  722. conf->algorithm);
  723. }
  724. break;
  725. }
  726. /*
  727. * Finally, compute the new sector number
  728. */
  729. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  730. return new_sector;
  731. }
  732. static sector_t compute_blocknr(struct stripe_head *sh, int i)
  733. {
  734. raid5_conf_t *conf = sh->raid_conf;
  735. int raid_disks = sh->disks, data_disks = raid_disks - 1;
  736. sector_t new_sector = sh->sector, check;
  737. int sectors_per_chunk = conf->chunk_size >> 9;
  738. sector_t stripe;
  739. int chunk_offset;
  740. int chunk_number, dummy1, dummy2, dd_idx = i;
  741. sector_t r_sector;
  742. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  743. stripe = new_sector;
  744. BUG_ON(new_sector != stripe);
  745. if (i == sh->pd_idx)
  746. return 0;
  747. switch(conf->level) {
  748. case 4: break;
  749. case 5:
  750. switch (conf->algorithm) {
  751. case ALGORITHM_LEFT_ASYMMETRIC:
  752. case ALGORITHM_RIGHT_ASYMMETRIC:
  753. if (i > sh->pd_idx)
  754. i--;
  755. break;
  756. case ALGORITHM_LEFT_SYMMETRIC:
  757. case ALGORITHM_RIGHT_SYMMETRIC:
  758. if (i < sh->pd_idx)
  759. i += raid_disks;
  760. i -= (sh->pd_idx + 1);
  761. break;
  762. default:
  763. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  764. conf->algorithm);
  765. }
  766. break;
  767. case 6:
  768. data_disks = raid_disks - 2;
  769. if (i == raid6_next_disk(sh->pd_idx, raid_disks))
  770. return 0; /* It is the Q disk */
  771. switch (conf->algorithm) {
  772. case ALGORITHM_LEFT_ASYMMETRIC:
  773. case ALGORITHM_RIGHT_ASYMMETRIC:
  774. if (sh->pd_idx == raid_disks-1)
  775. i--; /* Q D D D P */
  776. else if (i > sh->pd_idx)
  777. i -= 2; /* D D P Q D */
  778. break;
  779. case ALGORITHM_LEFT_SYMMETRIC:
  780. case ALGORITHM_RIGHT_SYMMETRIC:
  781. if (sh->pd_idx == raid_disks-1)
  782. i--; /* Q D D D P */
  783. else {
  784. /* D D P Q D */
  785. if (i < sh->pd_idx)
  786. i += raid_disks;
  787. i -= (sh->pd_idx + 2);
  788. }
  789. break;
  790. default:
  791. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  792. conf->algorithm);
  793. }
  794. break;
  795. }
  796. chunk_number = stripe * data_disks + i;
  797. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  798. check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
  799. if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
  800. printk(KERN_ERR "compute_blocknr: map not correct\n");
  801. return 0;
  802. }
  803. return r_sector;
  804. }
  805. /*
  806. * Copy data between a page in the stripe cache, and one or more bion
  807. * The page could align with the middle of the bio, or there could be
  808. * several bion, each with several bio_vecs, which cover part of the page
  809. * Multiple bion are linked together on bi_next. There may be extras
  810. * at the end of this list. We ignore them.
  811. */
  812. static void copy_data(int frombio, struct bio *bio,
  813. struct page *page,
  814. sector_t sector)
  815. {
  816. char *pa = page_address(page);
  817. struct bio_vec *bvl;
  818. int i;
  819. int page_offset;
  820. if (bio->bi_sector >= sector)
  821. page_offset = (signed)(bio->bi_sector - sector) * 512;
  822. else
  823. page_offset = (signed)(sector - bio->bi_sector) * -512;
  824. bio_for_each_segment(bvl, bio, i) {
  825. int len = bio_iovec_idx(bio,i)->bv_len;
  826. int clen;
  827. int b_offset = 0;
  828. if (page_offset < 0) {
  829. b_offset = -page_offset;
  830. page_offset += b_offset;
  831. len -= b_offset;
  832. }
  833. if (len > 0 && page_offset + len > STRIPE_SIZE)
  834. clen = STRIPE_SIZE - page_offset;
  835. else clen = len;
  836. if (clen > 0) {
  837. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  838. if (frombio)
  839. memcpy(pa+page_offset, ba+b_offset, clen);
  840. else
  841. memcpy(ba+b_offset, pa+page_offset, clen);
  842. __bio_kunmap_atomic(ba, KM_USER0);
  843. }
  844. if (clen < len) /* hit end of page */
  845. break;
  846. page_offset += len;
  847. }
  848. }
  849. #define check_xor() do { \
  850. if (count == MAX_XOR_BLOCKS) { \
  851. xor_block(count, STRIPE_SIZE, ptr); \
  852. count = 1; \
  853. } \
  854. } while(0)
  855. static void compute_block(struct stripe_head *sh, int dd_idx)
  856. {
  857. int i, count, disks = sh->disks;
  858. void *ptr[MAX_XOR_BLOCKS], *p;
  859. PRINTK("compute_block, stripe %llu, idx %d\n",
  860. (unsigned long long)sh->sector, dd_idx);
  861. ptr[0] = page_address(sh->dev[dd_idx].page);
  862. memset(ptr[0], 0, STRIPE_SIZE);
  863. count = 1;
  864. for (i = disks ; i--; ) {
  865. if (i == dd_idx)
  866. continue;
  867. p = page_address(sh->dev[i].page);
  868. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  869. ptr[count++] = p;
  870. else
  871. printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
  872. " not present\n", dd_idx,
  873. (unsigned long long)sh->sector, i);
  874. check_xor();
  875. }
  876. if (count != 1)
  877. xor_block(count, STRIPE_SIZE, ptr);
  878. set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  879. }
  880. static void compute_parity5(struct stripe_head *sh, int method)
  881. {
  882. raid5_conf_t *conf = sh->raid_conf;
  883. int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
  884. void *ptr[MAX_XOR_BLOCKS];
  885. struct bio *chosen;
  886. PRINTK("compute_parity5, stripe %llu, method %d\n",
  887. (unsigned long long)sh->sector, method);
  888. count = 1;
  889. ptr[0] = page_address(sh->dev[pd_idx].page);
  890. switch(method) {
  891. case READ_MODIFY_WRITE:
  892. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
  893. for (i=disks ; i-- ;) {
  894. if (i==pd_idx)
  895. continue;
  896. if (sh->dev[i].towrite &&
  897. test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  898. ptr[count++] = page_address(sh->dev[i].page);
  899. chosen = sh->dev[i].towrite;
  900. sh->dev[i].towrite = NULL;
  901. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  902. wake_up(&conf->wait_for_overlap);
  903. BUG_ON(sh->dev[i].written);
  904. sh->dev[i].written = chosen;
  905. check_xor();
  906. }
  907. }
  908. break;
  909. case RECONSTRUCT_WRITE:
  910. memset(ptr[0], 0, STRIPE_SIZE);
  911. for (i= disks; i-- ;)
  912. if (i!=pd_idx && sh->dev[i].towrite) {
  913. chosen = sh->dev[i].towrite;
  914. sh->dev[i].towrite = NULL;
  915. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  916. wake_up(&conf->wait_for_overlap);
  917. BUG_ON(sh->dev[i].written);
  918. sh->dev[i].written = chosen;
  919. }
  920. break;
  921. case CHECK_PARITY:
  922. break;
  923. }
  924. if (count>1) {
  925. xor_block(count, STRIPE_SIZE, ptr);
  926. count = 1;
  927. }
  928. for (i = disks; i--;)
  929. if (sh->dev[i].written) {
  930. sector_t sector = sh->dev[i].sector;
  931. struct bio *wbi = sh->dev[i].written;
  932. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  933. copy_data(1, wbi, sh->dev[i].page, sector);
  934. wbi = r5_next_bio(wbi, sector);
  935. }
  936. set_bit(R5_LOCKED, &sh->dev[i].flags);
  937. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  938. }
  939. switch(method) {
  940. case RECONSTRUCT_WRITE:
  941. case CHECK_PARITY:
  942. for (i=disks; i--;)
  943. if (i != pd_idx) {
  944. ptr[count++] = page_address(sh->dev[i].page);
  945. check_xor();
  946. }
  947. break;
  948. case READ_MODIFY_WRITE:
  949. for (i = disks; i--;)
  950. if (sh->dev[i].written) {
  951. ptr[count++] = page_address(sh->dev[i].page);
  952. check_xor();
  953. }
  954. }
  955. if (count != 1)
  956. xor_block(count, STRIPE_SIZE, ptr);
  957. if (method != CHECK_PARITY) {
  958. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  959. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  960. } else
  961. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  962. }
  963. static void compute_parity6(struct stripe_head *sh, int method)
  964. {
  965. raid6_conf_t *conf = sh->raid_conf;
  966. int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
  967. struct bio *chosen;
  968. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  969. void *ptrs[disks];
  970. qd_idx = raid6_next_disk(pd_idx, disks);
  971. d0_idx = raid6_next_disk(qd_idx, disks);
  972. PRINTK("compute_parity, stripe %llu, method %d\n",
  973. (unsigned long long)sh->sector, method);
  974. switch(method) {
  975. case READ_MODIFY_WRITE:
  976. BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
  977. case RECONSTRUCT_WRITE:
  978. for (i= disks; i-- ;)
  979. if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
  980. chosen = sh->dev[i].towrite;
  981. sh->dev[i].towrite = NULL;
  982. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  983. wake_up(&conf->wait_for_overlap);
  984. if (sh->dev[i].written) BUG();
  985. sh->dev[i].written = chosen;
  986. }
  987. break;
  988. case CHECK_PARITY:
  989. BUG(); /* Not implemented yet */
  990. }
  991. for (i = disks; i--;)
  992. if (sh->dev[i].written) {
  993. sector_t sector = sh->dev[i].sector;
  994. struct bio *wbi = sh->dev[i].written;
  995. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  996. copy_data(1, wbi, sh->dev[i].page, sector);
  997. wbi = r5_next_bio(wbi, sector);
  998. }
  999. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1000. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  1001. }
  1002. // switch(method) {
  1003. // case RECONSTRUCT_WRITE:
  1004. // case CHECK_PARITY:
  1005. // case UPDATE_PARITY:
  1006. /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
  1007. /* FIX: Is this ordering of drives even remotely optimal? */
  1008. count = 0;
  1009. i = d0_idx;
  1010. do {
  1011. ptrs[count++] = page_address(sh->dev[i].page);
  1012. if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1013. printk("block %d/%d not uptodate on parity calc\n", i,count);
  1014. i = raid6_next_disk(i, disks);
  1015. } while ( i != d0_idx );
  1016. // break;
  1017. // }
  1018. raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
  1019. switch(method) {
  1020. case RECONSTRUCT_WRITE:
  1021. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1022. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1023. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1024. set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
  1025. break;
  1026. case UPDATE_PARITY:
  1027. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1028. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1029. break;
  1030. }
  1031. }
  1032. /* Compute one missing block */
  1033. static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
  1034. {
  1035. raid6_conf_t *conf = sh->raid_conf;
  1036. int i, count, disks = conf->raid_disks;
  1037. void *ptr[MAX_XOR_BLOCKS], *p;
  1038. int pd_idx = sh->pd_idx;
  1039. int qd_idx = raid6_next_disk(pd_idx, disks);
  1040. PRINTK("compute_block_1, stripe %llu, idx %d\n",
  1041. (unsigned long long)sh->sector, dd_idx);
  1042. if ( dd_idx == qd_idx ) {
  1043. /* We're actually computing the Q drive */
  1044. compute_parity6(sh, UPDATE_PARITY);
  1045. } else {
  1046. ptr[0] = page_address(sh->dev[dd_idx].page);
  1047. if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
  1048. count = 1;
  1049. for (i = disks ; i--; ) {
  1050. if (i == dd_idx || i == qd_idx)
  1051. continue;
  1052. p = page_address(sh->dev[i].page);
  1053. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1054. ptr[count++] = p;
  1055. else
  1056. printk("compute_block() %d, stripe %llu, %d"
  1057. " not present\n", dd_idx,
  1058. (unsigned long long)sh->sector, i);
  1059. check_xor();
  1060. }
  1061. if (count != 1)
  1062. xor_block(count, STRIPE_SIZE, ptr);
  1063. if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1064. else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1065. }
  1066. }
  1067. /* Compute two missing blocks */
  1068. static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
  1069. {
  1070. raid6_conf_t *conf = sh->raid_conf;
  1071. int i, count, disks = conf->raid_disks;
  1072. int pd_idx = sh->pd_idx;
  1073. int qd_idx = raid6_next_disk(pd_idx, disks);
  1074. int d0_idx = raid6_next_disk(qd_idx, disks);
  1075. int faila, failb;
  1076. /* faila and failb are disk numbers relative to d0_idx */
  1077. /* pd_idx become disks-2 and qd_idx become disks-1 */
  1078. faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
  1079. failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
  1080. BUG_ON(faila == failb);
  1081. if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
  1082. PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
  1083. (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
  1084. if ( failb == disks-1 ) {
  1085. /* Q disk is one of the missing disks */
  1086. if ( faila == disks-2 ) {
  1087. /* Missing P+Q, just recompute */
  1088. compute_parity6(sh, UPDATE_PARITY);
  1089. return;
  1090. } else {
  1091. /* We're missing D+Q; recompute D from P */
  1092. compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
  1093. compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
  1094. return;
  1095. }
  1096. }
  1097. /* We're missing D+P or D+D; build pointer table */
  1098. {
  1099. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1100. void *ptrs[disks];
  1101. count = 0;
  1102. i = d0_idx;
  1103. do {
  1104. ptrs[count++] = page_address(sh->dev[i].page);
  1105. i = raid6_next_disk(i, disks);
  1106. if (i != dd_idx1 && i != dd_idx2 &&
  1107. !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1108. printk("compute_2 with missing block %d/%d\n", count, i);
  1109. } while ( i != d0_idx );
  1110. if ( failb == disks-2 ) {
  1111. /* We're missing D+P. */
  1112. raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
  1113. } else {
  1114. /* We're missing D+D. */
  1115. raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
  1116. }
  1117. /* Both the above update both missing blocks */
  1118. set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
  1119. set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
  1120. }
  1121. }
  1122. /*
  1123. * Each stripe/dev can have one or more bion attached.
  1124. * toread/towrite point to the first in a chain.
  1125. * The bi_next chain must be in order.
  1126. */
  1127. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1128. {
  1129. struct bio **bip;
  1130. raid5_conf_t *conf = sh->raid_conf;
  1131. int firstwrite=0;
  1132. PRINTK("adding bh b#%llu to stripe s#%llu\n",
  1133. (unsigned long long)bi->bi_sector,
  1134. (unsigned long long)sh->sector);
  1135. spin_lock(&sh->lock);
  1136. spin_lock_irq(&conf->device_lock);
  1137. if (forwrite) {
  1138. bip = &sh->dev[dd_idx].towrite;
  1139. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1140. firstwrite = 1;
  1141. } else
  1142. bip = &sh->dev[dd_idx].toread;
  1143. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1144. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1145. goto overlap;
  1146. bip = & (*bip)->bi_next;
  1147. }
  1148. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1149. goto overlap;
  1150. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1151. if (*bip)
  1152. bi->bi_next = *bip;
  1153. *bip = bi;
  1154. bi->bi_phys_segments ++;
  1155. spin_unlock_irq(&conf->device_lock);
  1156. spin_unlock(&sh->lock);
  1157. PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1158. (unsigned long long)bi->bi_sector,
  1159. (unsigned long long)sh->sector, dd_idx);
  1160. if (conf->mddev->bitmap && firstwrite) {
  1161. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1162. STRIPE_SECTORS, 0);
  1163. sh->bm_seq = conf->seq_flush+1;
  1164. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1165. }
  1166. if (forwrite) {
  1167. /* check if page is covered */
  1168. sector_t sector = sh->dev[dd_idx].sector;
  1169. for (bi=sh->dev[dd_idx].towrite;
  1170. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1171. bi && bi->bi_sector <= sector;
  1172. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1173. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1174. sector = bi->bi_sector + (bi->bi_size>>9);
  1175. }
  1176. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1177. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1178. }
  1179. return 1;
  1180. overlap:
  1181. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1182. spin_unlock_irq(&conf->device_lock);
  1183. spin_unlock(&sh->lock);
  1184. return 0;
  1185. }
  1186. static void end_reshape(raid5_conf_t *conf);
  1187. static int page_is_zero(struct page *p)
  1188. {
  1189. char *a = page_address(p);
  1190. return ((*(u32*)a) == 0 &&
  1191. memcmp(a, a+4, STRIPE_SIZE-4)==0);
  1192. }
  1193. static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
  1194. {
  1195. int sectors_per_chunk = conf->chunk_size >> 9;
  1196. sector_t x = stripe;
  1197. int pd_idx, dd_idx;
  1198. int chunk_offset = sector_div(x, sectors_per_chunk);
  1199. stripe = x;
  1200. raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
  1201. + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
  1202. return pd_idx;
  1203. }
  1204. /*
  1205. * handle_stripe - do things to a stripe.
  1206. *
  1207. * We lock the stripe and then examine the state of various bits
  1208. * to see what needs to be done.
  1209. * Possible results:
  1210. * return some read request which now have data
  1211. * return some write requests which are safely on disc
  1212. * schedule a read on some buffers
  1213. * schedule a write of some buffers
  1214. * return confirmation of parity correctness
  1215. *
  1216. * Parity calculations are done inside the stripe lock
  1217. * buffers are taken off read_list or write_list, and bh_cache buffers
  1218. * get BH_Lock set before the stripe lock is released.
  1219. *
  1220. */
  1221. static void handle_stripe5(struct stripe_head *sh)
  1222. {
  1223. raid5_conf_t *conf = sh->raid_conf;
  1224. int disks = sh->disks;
  1225. struct bio *return_bi= NULL;
  1226. struct bio *bi;
  1227. int i;
  1228. int syncing, expanding, expanded;
  1229. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  1230. int non_overwrite = 0;
  1231. int failed_num=0;
  1232. struct r5dev *dev;
  1233. PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
  1234. (unsigned long long)sh->sector, atomic_read(&sh->count),
  1235. sh->pd_idx);
  1236. spin_lock(&sh->lock);
  1237. clear_bit(STRIPE_HANDLE, &sh->state);
  1238. clear_bit(STRIPE_DELAYED, &sh->state);
  1239. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  1240. expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1241. expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  1242. /* Now to look around and see what can be done */
  1243. rcu_read_lock();
  1244. for (i=disks; i--; ) {
  1245. mdk_rdev_t *rdev;
  1246. dev = &sh->dev[i];
  1247. clear_bit(R5_Insync, &dev->flags);
  1248. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  1249. i, dev->flags, dev->toread, dev->towrite, dev->written);
  1250. /* maybe we can reply to a read */
  1251. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  1252. struct bio *rbi, *rbi2;
  1253. PRINTK("Return read for disc %d\n", i);
  1254. spin_lock_irq(&conf->device_lock);
  1255. rbi = dev->toread;
  1256. dev->toread = NULL;
  1257. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1258. wake_up(&conf->wait_for_overlap);
  1259. spin_unlock_irq(&conf->device_lock);
  1260. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1261. copy_data(0, rbi, dev->page, dev->sector);
  1262. rbi2 = r5_next_bio(rbi, dev->sector);
  1263. spin_lock_irq(&conf->device_lock);
  1264. if (--rbi->bi_phys_segments == 0) {
  1265. rbi->bi_next = return_bi;
  1266. return_bi = rbi;
  1267. }
  1268. spin_unlock_irq(&conf->device_lock);
  1269. rbi = rbi2;
  1270. }
  1271. }
  1272. /* now count some things */
  1273. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  1274. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  1275. if (dev->toread) to_read++;
  1276. if (dev->towrite) {
  1277. to_write++;
  1278. if (!test_bit(R5_OVERWRITE, &dev->flags))
  1279. non_overwrite++;
  1280. }
  1281. if (dev->written) written++;
  1282. rdev = rcu_dereference(conf->disks[i].rdev);
  1283. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  1284. /* The ReadError flag will just be confusing now */
  1285. clear_bit(R5_ReadError, &dev->flags);
  1286. clear_bit(R5_ReWrite, &dev->flags);
  1287. }
  1288. if (!rdev || !test_bit(In_sync, &rdev->flags)
  1289. || test_bit(R5_ReadError, &dev->flags)) {
  1290. failed++;
  1291. failed_num = i;
  1292. } else
  1293. set_bit(R5_Insync, &dev->flags);
  1294. }
  1295. rcu_read_unlock();
  1296. PRINTK("locked=%d uptodate=%d to_read=%d"
  1297. " to_write=%d failed=%d failed_num=%d\n",
  1298. locked, uptodate, to_read, to_write, failed, failed_num);
  1299. /* check if the array has lost two devices and, if so, some requests might
  1300. * need to be failed
  1301. */
  1302. if (failed > 1 && to_read+to_write+written) {
  1303. for (i=disks; i--; ) {
  1304. int bitmap_end = 0;
  1305. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1306. mdk_rdev_t *rdev;
  1307. rcu_read_lock();
  1308. rdev = rcu_dereference(conf->disks[i].rdev);
  1309. if (rdev && test_bit(In_sync, &rdev->flags))
  1310. /* multiple read failures in one stripe */
  1311. md_error(conf->mddev, rdev);
  1312. rcu_read_unlock();
  1313. }
  1314. spin_lock_irq(&conf->device_lock);
  1315. /* fail all writes first */
  1316. bi = sh->dev[i].towrite;
  1317. sh->dev[i].towrite = NULL;
  1318. if (bi) { to_write--; bitmap_end = 1; }
  1319. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1320. wake_up(&conf->wait_for_overlap);
  1321. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1322. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1323. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1324. if (--bi->bi_phys_segments == 0) {
  1325. md_write_end(conf->mddev);
  1326. bi->bi_next = return_bi;
  1327. return_bi = bi;
  1328. }
  1329. bi = nextbi;
  1330. }
  1331. /* and fail all 'written' */
  1332. bi = sh->dev[i].written;
  1333. sh->dev[i].written = NULL;
  1334. if (bi) bitmap_end = 1;
  1335. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  1336. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1337. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1338. if (--bi->bi_phys_segments == 0) {
  1339. md_write_end(conf->mddev);
  1340. bi->bi_next = return_bi;
  1341. return_bi = bi;
  1342. }
  1343. bi = bi2;
  1344. }
  1345. /* fail any reads if this device is non-operational */
  1346. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1347. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1348. bi = sh->dev[i].toread;
  1349. sh->dev[i].toread = NULL;
  1350. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1351. wake_up(&conf->wait_for_overlap);
  1352. if (bi) to_read--;
  1353. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1354. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1355. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1356. if (--bi->bi_phys_segments == 0) {
  1357. bi->bi_next = return_bi;
  1358. return_bi = bi;
  1359. }
  1360. bi = nextbi;
  1361. }
  1362. }
  1363. spin_unlock_irq(&conf->device_lock);
  1364. if (bitmap_end)
  1365. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1366. STRIPE_SECTORS, 0, 0);
  1367. }
  1368. }
  1369. if (failed > 1 && syncing) {
  1370. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  1371. clear_bit(STRIPE_SYNCING, &sh->state);
  1372. syncing = 0;
  1373. }
  1374. /* might be able to return some write requests if the parity block
  1375. * is safe, or on a failed drive
  1376. */
  1377. dev = &sh->dev[sh->pd_idx];
  1378. if ( written &&
  1379. ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
  1380. test_bit(R5_UPTODATE, &dev->flags))
  1381. || (failed == 1 && failed_num == sh->pd_idx))
  1382. ) {
  1383. /* any written block on an uptodate or failed drive can be returned.
  1384. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  1385. * never LOCKED, so we don't need to test 'failed' directly.
  1386. */
  1387. for (i=disks; i--; )
  1388. if (sh->dev[i].written) {
  1389. dev = &sh->dev[i];
  1390. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1391. test_bit(R5_UPTODATE, &dev->flags) ) {
  1392. /* We can return any write requests */
  1393. struct bio *wbi, *wbi2;
  1394. int bitmap_end = 0;
  1395. PRINTK("Return write for disc %d\n", i);
  1396. spin_lock_irq(&conf->device_lock);
  1397. wbi = dev->written;
  1398. dev->written = NULL;
  1399. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1400. wbi2 = r5_next_bio(wbi, dev->sector);
  1401. if (--wbi->bi_phys_segments == 0) {
  1402. md_write_end(conf->mddev);
  1403. wbi->bi_next = return_bi;
  1404. return_bi = wbi;
  1405. }
  1406. wbi = wbi2;
  1407. }
  1408. if (dev->towrite == NULL)
  1409. bitmap_end = 1;
  1410. spin_unlock_irq(&conf->device_lock);
  1411. if (bitmap_end)
  1412. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1413. STRIPE_SECTORS,
  1414. !test_bit(STRIPE_DEGRADED, &sh->state), 0);
  1415. }
  1416. }
  1417. }
  1418. /* Now we might consider reading some blocks, either to check/generate
  1419. * parity, or to satisfy requests
  1420. * or to load a block that is being partially written.
  1421. */
  1422. if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
  1423. for (i=disks; i--;) {
  1424. dev = &sh->dev[i];
  1425. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1426. (dev->toread ||
  1427. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1428. syncing ||
  1429. expanding ||
  1430. (failed && (sh->dev[failed_num].toread ||
  1431. (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
  1432. )
  1433. ) {
  1434. /* we would like to get this block, possibly
  1435. * by computing it, but we might not be able to
  1436. */
  1437. if (uptodate == disks-1) {
  1438. PRINTK("Computing block %d\n", i);
  1439. compute_block(sh, i);
  1440. uptodate++;
  1441. } else if (test_bit(R5_Insync, &dev->flags)) {
  1442. set_bit(R5_LOCKED, &dev->flags);
  1443. set_bit(R5_Wantread, &dev->flags);
  1444. #if 0
  1445. /* if I am just reading this block and we don't have
  1446. a failed drive, or any pending writes then sidestep the cache */
  1447. if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
  1448. ! syncing && !failed && !to_write) {
  1449. sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
  1450. sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
  1451. }
  1452. #endif
  1453. locked++;
  1454. PRINTK("Reading block %d (sync=%d)\n",
  1455. i, syncing);
  1456. }
  1457. }
  1458. }
  1459. set_bit(STRIPE_HANDLE, &sh->state);
  1460. }
  1461. /* now to consider writing and what else, if anything should be read */
  1462. if (to_write) {
  1463. int rmw=0, rcw=0;
  1464. for (i=disks ; i--;) {
  1465. /* would I have to read this buffer for read_modify_write */
  1466. dev = &sh->dev[i];
  1467. if ((dev->towrite || i == sh->pd_idx) &&
  1468. (!test_bit(R5_LOCKED, &dev->flags)
  1469. #if 0
  1470. || sh->bh_page[i]!=bh->b_page
  1471. #endif
  1472. ) &&
  1473. !test_bit(R5_UPTODATE, &dev->flags)) {
  1474. if (test_bit(R5_Insync, &dev->flags)
  1475. /* && !(!mddev->insync && i == sh->pd_idx) */
  1476. )
  1477. rmw++;
  1478. else rmw += 2*disks; /* cannot read it */
  1479. }
  1480. /* Would I have to read this buffer for reconstruct_write */
  1481. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1482. (!test_bit(R5_LOCKED, &dev->flags)
  1483. #if 0
  1484. || sh->bh_page[i] != bh->b_page
  1485. #endif
  1486. ) &&
  1487. !test_bit(R5_UPTODATE, &dev->flags)) {
  1488. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1489. else rcw += 2*disks;
  1490. }
  1491. }
  1492. PRINTK("for sector %llu, rmw=%d rcw=%d\n",
  1493. (unsigned long long)sh->sector, rmw, rcw);
  1494. set_bit(STRIPE_HANDLE, &sh->state);
  1495. if (rmw < rcw && rmw > 0)
  1496. /* prefer read-modify-write, but need to get some data */
  1497. for (i=disks; i--;) {
  1498. dev = &sh->dev[i];
  1499. if ((dev->towrite || i == sh->pd_idx) &&
  1500. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1501. test_bit(R5_Insync, &dev->flags)) {
  1502. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1503. {
  1504. PRINTK("Read_old block %d for r-m-w\n", i);
  1505. set_bit(R5_LOCKED, &dev->flags);
  1506. set_bit(R5_Wantread, &dev->flags);
  1507. locked++;
  1508. } else {
  1509. set_bit(STRIPE_DELAYED, &sh->state);
  1510. set_bit(STRIPE_HANDLE, &sh->state);
  1511. }
  1512. }
  1513. }
  1514. if (rcw <= rmw && rcw > 0)
  1515. /* want reconstruct write, but need to get some data */
  1516. for (i=disks; i--;) {
  1517. dev = &sh->dev[i];
  1518. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1519. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1520. test_bit(R5_Insync, &dev->flags)) {
  1521. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1522. {
  1523. PRINTK("Read_old block %d for Reconstruct\n", i);
  1524. set_bit(R5_LOCKED, &dev->flags);
  1525. set_bit(R5_Wantread, &dev->flags);
  1526. locked++;
  1527. } else {
  1528. set_bit(STRIPE_DELAYED, &sh->state);
  1529. set_bit(STRIPE_HANDLE, &sh->state);
  1530. }
  1531. }
  1532. }
  1533. /* now if nothing is locked, and if we have enough data, we can start a write request */
  1534. if (locked == 0 && (rcw == 0 ||rmw == 0) &&
  1535. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1536. PRINTK("Computing parity...\n");
  1537. compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
  1538. /* now every locked buffer is ready to be written */
  1539. for (i=disks; i--;)
  1540. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  1541. PRINTK("Writing block %d\n", i);
  1542. locked++;
  1543. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1544. if (!test_bit(R5_Insync, &sh->dev[i].flags)
  1545. || (i==sh->pd_idx && failed == 0))
  1546. set_bit(STRIPE_INSYNC, &sh->state);
  1547. }
  1548. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1549. atomic_dec(&conf->preread_active_stripes);
  1550. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  1551. md_wakeup_thread(conf->mddev->thread);
  1552. }
  1553. }
  1554. }
  1555. /* maybe we need to check and possibly fix the parity for this stripe
  1556. * Any reads will already have been scheduled, so we just see if enough data
  1557. * is available
  1558. */
  1559. if (syncing && locked == 0 &&
  1560. !test_bit(STRIPE_INSYNC, &sh->state)) {
  1561. set_bit(STRIPE_HANDLE, &sh->state);
  1562. if (failed == 0) {
  1563. BUG_ON(uptodate != disks);
  1564. compute_parity5(sh, CHECK_PARITY);
  1565. uptodate--;
  1566. if (page_is_zero(sh->dev[sh->pd_idx].page)) {
  1567. /* parity is correct (on disc, not in buffer any more) */
  1568. set_bit(STRIPE_INSYNC, &sh->state);
  1569. } else {
  1570. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  1571. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  1572. /* don't try to repair!! */
  1573. set_bit(STRIPE_INSYNC, &sh->state);
  1574. else {
  1575. compute_block(sh, sh->pd_idx);
  1576. uptodate++;
  1577. }
  1578. }
  1579. }
  1580. if (!test_bit(STRIPE_INSYNC, &sh->state)) {
  1581. /* either failed parity check, or recovery is happening */
  1582. if (failed==0)
  1583. failed_num = sh->pd_idx;
  1584. dev = &sh->dev[failed_num];
  1585. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  1586. BUG_ON(uptodate != disks);
  1587. set_bit(R5_LOCKED, &dev->flags);
  1588. set_bit(R5_Wantwrite, &dev->flags);
  1589. clear_bit(STRIPE_DEGRADED, &sh->state);
  1590. locked++;
  1591. set_bit(STRIPE_INSYNC, &sh->state);
  1592. }
  1593. }
  1594. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  1595. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  1596. clear_bit(STRIPE_SYNCING, &sh->state);
  1597. }
  1598. /* If the failed drive is just a ReadError, then we might need to progress
  1599. * the repair/check process
  1600. */
  1601. if (failed == 1 && ! conf->mddev->ro &&
  1602. test_bit(R5_ReadError, &sh->dev[failed_num].flags)
  1603. && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
  1604. && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
  1605. ) {
  1606. dev = &sh->dev[failed_num];
  1607. if (!test_bit(R5_ReWrite, &dev->flags)) {
  1608. set_bit(R5_Wantwrite, &dev->flags);
  1609. set_bit(R5_ReWrite, &dev->flags);
  1610. set_bit(R5_LOCKED, &dev->flags);
  1611. locked++;
  1612. } else {
  1613. /* let's read it back */
  1614. set_bit(R5_Wantread, &dev->flags);
  1615. set_bit(R5_LOCKED, &dev->flags);
  1616. locked++;
  1617. }
  1618. }
  1619. if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  1620. /* Need to write out all blocks after computing parity */
  1621. sh->disks = conf->raid_disks;
  1622. sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
  1623. compute_parity5(sh, RECONSTRUCT_WRITE);
  1624. for (i= conf->raid_disks; i--;) {
  1625. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1626. locked++;
  1627. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1628. }
  1629. clear_bit(STRIPE_EXPANDING, &sh->state);
  1630. } else if (expanded) {
  1631. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  1632. atomic_dec(&conf->reshape_stripes);
  1633. wake_up(&conf->wait_for_overlap);
  1634. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  1635. }
  1636. if (expanding && locked == 0) {
  1637. /* We have read all the blocks in this stripe and now we need to
  1638. * copy some of them into a target stripe for expand.
  1639. */
  1640. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1641. for (i=0; i< sh->disks; i++)
  1642. if (i != sh->pd_idx) {
  1643. int dd_idx, pd_idx, j;
  1644. struct stripe_head *sh2;
  1645. sector_t bn = compute_blocknr(sh, i);
  1646. sector_t s = raid5_compute_sector(bn, conf->raid_disks,
  1647. conf->raid_disks-1,
  1648. &dd_idx, &pd_idx, conf);
  1649. sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
  1650. if (sh2 == NULL)
  1651. /* so far only the early blocks of this stripe
  1652. * have been requested. When later blocks
  1653. * get requested, we will try again
  1654. */
  1655. continue;
  1656. if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  1657. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  1658. /* must have already done this block */
  1659. release_stripe(sh2);
  1660. continue;
  1661. }
  1662. memcpy(page_address(sh2->dev[dd_idx].page),
  1663. page_address(sh->dev[i].page),
  1664. STRIPE_SIZE);
  1665. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  1666. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  1667. for (j=0; j<conf->raid_disks; j++)
  1668. if (j != sh2->pd_idx &&
  1669. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  1670. break;
  1671. if (j == conf->raid_disks) {
  1672. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  1673. set_bit(STRIPE_HANDLE, &sh2->state);
  1674. }
  1675. release_stripe(sh2);
  1676. }
  1677. }
  1678. spin_unlock(&sh->lock);
  1679. while ((bi=return_bi)) {
  1680. int bytes = bi->bi_size;
  1681. return_bi = bi->bi_next;
  1682. bi->bi_next = NULL;
  1683. bi->bi_size = 0;
  1684. bi->bi_end_io(bi, bytes, 0);
  1685. }
  1686. for (i=disks; i-- ;) {
  1687. int rw;
  1688. struct bio *bi;
  1689. mdk_rdev_t *rdev;
  1690. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  1691. rw = 1;
  1692. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  1693. rw = 0;
  1694. else
  1695. continue;
  1696. bi = &sh->dev[i].req;
  1697. bi->bi_rw = rw;
  1698. if (rw)
  1699. bi->bi_end_io = raid5_end_write_request;
  1700. else
  1701. bi->bi_end_io = raid5_end_read_request;
  1702. rcu_read_lock();
  1703. rdev = rcu_dereference(conf->disks[i].rdev);
  1704. if (rdev && test_bit(Faulty, &rdev->flags))
  1705. rdev = NULL;
  1706. if (rdev)
  1707. atomic_inc(&rdev->nr_pending);
  1708. rcu_read_unlock();
  1709. if (rdev) {
  1710. if (syncing || expanding || expanded)
  1711. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  1712. bi->bi_bdev = rdev->bdev;
  1713. PRINTK("for %llu schedule op %ld on disc %d\n",
  1714. (unsigned long long)sh->sector, bi->bi_rw, i);
  1715. atomic_inc(&sh->count);
  1716. bi->bi_sector = sh->sector + rdev->data_offset;
  1717. bi->bi_flags = 1 << BIO_UPTODATE;
  1718. bi->bi_vcnt = 1;
  1719. bi->bi_max_vecs = 1;
  1720. bi->bi_idx = 0;
  1721. bi->bi_io_vec = &sh->dev[i].vec;
  1722. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  1723. bi->bi_io_vec[0].bv_offset = 0;
  1724. bi->bi_size = STRIPE_SIZE;
  1725. bi->bi_next = NULL;
  1726. if (rw == WRITE &&
  1727. test_bit(R5_ReWrite, &sh->dev[i].flags))
  1728. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  1729. generic_make_request(bi);
  1730. } else {
  1731. if (rw == 1)
  1732. set_bit(STRIPE_DEGRADED, &sh->state);
  1733. PRINTK("skip op %ld on disc %d for sector %llu\n",
  1734. bi->bi_rw, i, (unsigned long long)sh->sector);
  1735. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1736. set_bit(STRIPE_HANDLE, &sh->state);
  1737. }
  1738. }
  1739. }
  1740. static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
  1741. {
  1742. raid6_conf_t *conf = sh->raid_conf;
  1743. int disks = conf->raid_disks;
  1744. struct bio *return_bi= NULL;
  1745. struct bio *bi;
  1746. int i;
  1747. int syncing;
  1748. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  1749. int non_overwrite = 0;
  1750. int failed_num[2] = {0, 0};
  1751. struct r5dev *dev, *pdev, *qdev;
  1752. int pd_idx = sh->pd_idx;
  1753. int qd_idx = raid6_next_disk(pd_idx, disks);
  1754. int p_failed, q_failed;
  1755. PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
  1756. (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
  1757. pd_idx, qd_idx);
  1758. spin_lock(&sh->lock);
  1759. clear_bit(STRIPE_HANDLE, &sh->state);
  1760. clear_bit(STRIPE_DELAYED, &sh->state);
  1761. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  1762. /* Now to look around and see what can be done */
  1763. rcu_read_lock();
  1764. for (i=disks; i--; ) {
  1765. mdk_rdev_t *rdev;
  1766. dev = &sh->dev[i];
  1767. clear_bit(R5_Insync, &dev->flags);
  1768. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  1769. i, dev->flags, dev->toread, dev->towrite, dev->written);
  1770. /* maybe we can reply to a read */
  1771. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  1772. struct bio *rbi, *rbi2;
  1773. PRINTK("Return read for disc %d\n", i);
  1774. spin_lock_irq(&conf->device_lock);
  1775. rbi = dev->toread;
  1776. dev->toread = NULL;
  1777. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1778. wake_up(&conf->wait_for_overlap);
  1779. spin_unlock_irq(&conf->device_lock);
  1780. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1781. copy_data(0, rbi, dev->page, dev->sector);
  1782. rbi2 = r5_next_bio(rbi, dev->sector);
  1783. spin_lock_irq(&conf->device_lock);
  1784. if (--rbi->bi_phys_segments == 0) {
  1785. rbi->bi_next = return_bi;
  1786. return_bi = rbi;
  1787. }
  1788. spin_unlock_irq(&conf->device_lock);
  1789. rbi = rbi2;
  1790. }
  1791. }
  1792. /* now count some things */
  1793. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  1794. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  1795. if (dev->toread) to_read++;
  1796. if (dev->towrite) {
  1797. to_write++;
  1798. if (!test_bit(R5_OVERWRITE, &dev->flags))
  1799. non_overwrite++;
  1800. }
  1801. if (dev->written) written++;
  1802. rdev = rcu_dereference(conf->disks[i].rdev);
  1803. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  1804. /* The ReadError flag will just be confusing now */
  1805. clear_bit(R5_ReadError, &dev->flags);
  1806. clear_bit(R5_ReWrite, &dev->flags);
  1807. }
  1808. if (!rdev || !test_bit(In_sync, &rdev->flags)
  1809. || test_bit(R5_ReadError, &dev->flags)) {
  1810. if ( failed < 2 )
  1811. failed_num[failed] = i;
  1812. failed++;
  1813. } else
  1814. set_bit(R5_Insync, &dev->flags);
  1815. }
  1816. rcu_read_unlock();
  1817. PRINTK("locked=%d uptodate=%d to_read=%d"
  1818. " to_write=%d failed=%d failed_num=%d,%d\n",
  1819. locked, uptodate, to_read, to_write, failed,
  1820. failed_num[0], failed_num[1]);
  1821. /* check if the array has lost >2 devices and, if so, some requests might
  1822. * need to be failed
  1823. */
  1824. if (failed > 2 && to_read+to_write+written) {
  1825. for (i=disks; i--; ) {
  1826. int bitmap_end = 0;
  1827. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1828. mdk_rdev_t *rdev;
  1829. rcu_read_lock();
  1830. rdev = rcu_dereference(conf->disks[i].rdev);
  1831. if (rdev && test_bit(In_sync, &rdev->flags))
  1832. /* multiple read failures in one stripe */
  1833. md_error(conf->mddev, rdev);
  1834. rcu_read_unlock();
  1835. }
  1836. spin_lock_irq(&conf->device_lock);
  1837. /* fail all writes first */
  1838. bi = sh->dev[i].towrite;
  1839. sh->dev[i].towrite = NULL;
  1840. if (bi) { to_write--; bitmap_end = 1; }
  1841. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1842. wake_up(&conf->wait_for_overlap);
  1843. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1844. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1845. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1846. if (--bi->bi_phys_segments == 0) {
  1847. md_write_end(conf->mddev);
  1848. bi->bi_next = return_bi;
  1849. return_bi = bi;
  1850. }
  1851. bi = nextbi;
  1852. }
  1853. /* and fail all 'written' */
  1854. bi = sh->dev[i].written;
  1855. sh->dev[i].written = NULL;
  1856. if (bi) bitmap_end = 1;
  1857. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  1858. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1859. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1860. if (--bi->bi_phys_segments == 0) {
  1861. md_write_end(conf->mddev);
  1862. bi->bi_next = return_bi;
  1863. return_bi = bi;
  1864. }
  1865. bi = bi2;
  1866. }
  1867. /* fail any reads if this device is non-operational */
  1868. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1869. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1870. bi = sh->dev[i].toread;
  1871. sh->dev[i].toread = NULL;
  1872. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1873. wake_up(&conf->wait_for_overlap);
  1874. if (bi) to_read--;
  1875. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1876. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1877. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1878. if (--bi->bi_phys_segments == 0) {
  1879. bi->bi_next = return_bi;
  1880. return_bi = bi;
  1881. }
  1882. bi = nextbi;
  1883. }
  1884. }
  1885. spin_unlock_irq(&conf->device_lock);
  1886. if (bitmap_end)
  1887. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1888. STRIPE_SECTORS, 0, 0);
  1889. }
  1890. }
  1891. if (failed > 2 && syncing) {
  1892. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  1893. clear_bit(STRIPE_SYNCING, &sh->state);
  1894. syncing = 0;
  1895. }
  1896. /*
  1897. * might be able to return some write requests if the parity blocks
  1898. * are safe, or on a failed drive
  1899. */
  1900. pdev = &sh->dev[pd_idx];
  1901. p_failed = (failed >= 1 && failed_num[0] == pd_idx)
  1902. || (failed >= 2 && failed_num[1] == pd_idx);
  1903. qdev = &sh->dev[qd_idx];
  1904. q_failed = (failed >= 1 && failed_num[0] == qd_idx)
  1905. || (failed >= 2 && failed_num[1] == qd_idx);
  1906. if ( written &&
  1907. ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
  1908. && !test_bit(R5_LOCKED, &pdev->flags)
  1909. && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
  1910. ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
  1911. && !test_bit(R5_LOCKED, &qdev->flags)
  1912. && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
  1913. /* any written block on an uptodate or failed drive can be
  1914. * returned. Note that if we 'wrote' to a failed drive,
  1915. * it will be UPTODATE, but never LOCKED, so we don't need
  1916. * to test 'failed' directly.
  1917. */
  1918. for (i=disks; i--; )
  1919. if (sh->dev[i].written) {
  1920. dev = &sh->dev[i];
  1921. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1922. test_bit(R5_UPTODATE, &dev->flags) ) {
  1923. /* We can return any write requests */
  1924. int bitmap_end = 0;
  1925. struct bio *wbi, *wbi2;
  1926. PRINTK("Return write for stripe %llu disc %d\n",
  1927. (unsigned long long)sh->sector, i);
  1928. spin_lock_irq(&conf->device_lock);
  1929. wbi = dev->written;
  1930. dev->written = NULL;
  1931. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1932. wbi2 = r5_next_bio(wbi, dev->sector);
  1933. if (--wbi->bi_phys_segments == 0) {
  1934. md_write_end(conf->mddev);
  1935. wbi->bi_next = return_bi;
  1936. return_bi = wbi;
  1937. }
  1938. wbi = wbi2;
  1939. }
  1940. if (dev->towrite == NULL)
  1941. bitmap_end = 1;
  1942. spin_unlock_irq(&conf->device_lock);
  1943. if (bitmap_end)
  1944. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1945. STRIPE_SECTORS,
  1946. !test_bit(STRIPE_DEGRADED, &sh->state), 0);
  1947. }
  1948. }
  1949. }
  1950. /* Now we might consider reading some blocks, either to check/generate
  1951. * parity, or to satisfy requests
  1952. * or to load a block that is being partially written.
  1953. */
  1954. if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
  1955. for (i=disks; i--;) {
  1956. dev = &sh->dev[i];
  1957. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1958. (dev->toread ||
  1959. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1960. syncing ||
  1961. (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
  1962. (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
  1963. )
  1964. ) {
  1965. /* we would like to get this block, possibly
  1966. * by computing it, but we might not be able to
  1967. */
  1968. if (uptodate == disks-1) {
  1969. PRINTK("Computing stripe %llu block %d\n",
  1970. (unsigned long long)sh->sector, i);
  1971. compute_block_1(sh, i, 0);
  1972. uptodate++;
  1973. } else if ( uptodate == disks-2 && failed >= 2 ) {
  1974. /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
  1975. int other;
  1976. for (other=disks; other--;) {
  1977. if ( other == i )
  1978. continue;
  1979. if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
  1980. break;
  1981. }
  1982. BUG_ON(other < 0);
  1983. PRINTK("Computing stripe %llu blocks %d,%d\n",
  1984. (unsigned long long)sh->sector, i, other);
  1985. compute_block_2(sh, i, other);
  1986. uptodate += 2;
  1987. } else if (test_bit(R5_Insync, &dev->flags)) {
  1988. set_bit(R5_LOCKED, &dev->flags);
  1989. set_bit(R5_Wantread, &dev->flags);
  1990. #if 0
  1991. /* if I am just reading this block and we don't have
  1992. a failed drive, or any pending writes then sidestep the cache */
  1993. if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
  1994. ! syncing && !failed && !to_write) {
  1995. sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
  1996. sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
  1997. }
  1998. #endif
  1999. locked++;
  2000. PRINTK("Reading block %d (sync=%d)\n",
  2001. i, syncing);
  2002. }
  2003. }
  2004. }
  2005. set_bit(STRIPE_HANDLE, &sh->state);
  2006. }
  2007. /* now to consider writing and what else, if anything should be read */
  2008. if (to_write) {
  2009. int rcw=0, must_compute=0;
  2010. for (i=disks ; i--;) {
  2011. dev = &sh->dev[i];
  2012. /* Would I have to read this buffer for reconstruct_write */
  2013. if (!test_bit(R5_OVERWRITE, &dev->flags)
  2014. && i != pd_idx && i != qd_idx
  2015. && (!test_bit(R5_LOCKED, &dev->flags)
  2016. #if 0
  2017. || sh->bh_page[i] != bh->b_page
  2018. #endif
  2019. ) &&
  2020. !test_bit(R5_UPTODATE, &dev->flags)) {
  2021. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  2022. else {
  2023. PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
  2024. must_compute++;
  2025. }
  2026. }
  2027. }
  2028. PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
  2029. (unsigned long long)sh->sector, rcw, must_compute);
  2030. set_bit(STRIPE_HANDLE, &sh->state);
  2031. if (rcw > 0)
  2032. /* want reconstruct write, but need to get some data */
  2033. for (i=disks; i--;) {
  2034. dev = &sh->dev[i];
  2035. if (!test_bit(R5_OVERWRITE, &dev->flags)
  2036. && !(failed == 0 && (i == pd_idx || i == qd_idx))
  2037. && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  2038. test_bit(R5_Insync, &dev->flags)) {
  2039. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2040. {
  2041. PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
  2042. (unsigned long long)sh->sector, i);
  2043. set_bit(R5_LOCKED, &dev->flags);
  2044. set_bit(R5_Wantread, &dev->flags);
  2045. locked++;
  2046. } else {
  2047. PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
  2048. (unsigned long long)sh->sector, i);
  2049. set_bit(STRIPE_DELAYED, &sh->state);
  2050. set_bit(STRIPE_HANDLE, &sh->state);
  2051. }
  2052. }
  2053. }
  2054. /* now if nothing is locked, and if we have enough data, we can start a write request */
  2055. if (locked == 0 && rcw == 0 &&
  2056. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  2057. if ( must_compute > 0 ) {
  2058. /* We have failed blocks and need to compute them */
  2059. switch ( failed ) {
  2060. case 0: BUG();
  2061. case 1: compute_block_1(sh, failed_num[0], 0); break;
  2062. case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
  2063. default: BUG(); /* This request should have been failed? */
  2064. }
  2065. }
  2066. PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
  2067. compute_parity6(sh, RECONSTRUCT_WRITE);
  2068. /* now every locked buffer is ready to be written */
  2069. for (i=disks; i--;)
  2070. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  2071. PRINTK("Writing stripe %llu block %d\n",
  2072. (unsigned long long)sh->sector, i);
  2073. locked++;
  2074. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2075. }
  2076. /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
  2077. set_bit(STRIPE_INSYNC, &sh->state);
  2078. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2079. atomic_dec(&conf->preread_active_stripes);
  2080. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  2081. md_wakeup_thread(conf->mddev->thread);
  2082. }
  2083. }
  2084. }
  2085. /* maybe we need to check and possibly fix the parity for this stripe
  2086. * Any reads will already have been scheduled, so we just see if enough data
  2087. * is available
  2088. */
  2089. if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
  2090. int update_p = 0, update_q = 0;
  2091. struct r5dev *dev;
  2092. set_bit(STRIPE_HANDLE, &sh->state);
  2093. BUG_ON(failed>2);
  2094. BUG_ON(uptodate < disks);
  2095. /* Want to check and possibly repair P and Q.
  2096. * However there could be one 'failed' device, in which
  2097. * case we can only check one of them, possibly using the
  2098. * other to generate missing data
  2099. */
  2100. /* If !tmp_page, we cannot do the calculations,
  2101. * but as we have set STRIPE_HANDLE, we will soon be called
  2102. * by stripe_handle with a tmp_page - just wait until then.
  2103. */
  2104. if (tmp_page) {
  2105. if (failed == q_failed) {
  2106. /* The only possible failed device holds 'Q', so it makes
  2107. * sense to check P (If anything else were failed, we would
  2108. * have used P to recreate it).
  2109. */
  2110. compute_block_1(sh, pd_idx, 1);
  2111. if (!page_is_zero(sh->dev[pd_idx].page)) {
  2112. compute_block_1(sh,pd_idx,0);
  2113. update_p = 1;
  2114. }
  2115. }
  2116. if (!q_failed && failed < 2) {
  2117. /* q is not failed, and we didn't use it to generate
  2118. * anything, so it makes sense to check it
  2119. */
  2120. memcpy(page_address(tmp_page),
  2121. page_address(sh->dev[qd_idx].page),
  2122. STRIPE_SIZE);
  2123. compute_parity6(sh, UPDATE_PARITY);
  2124. if (memcmp(page_address(tmp_page),
  2125. page_address(sh->dev[qd_idx].page),
  2126. STRIPE_SIZE)!= 0) {
  2127. clear_bit(STRIPE_INSYNC, &sh->state);
  2128. update_q = 1;
  2129. }
  2130. }
  2131. if (update_p || update_q) {
  2132. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2133. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2134. /* don't try to repair!! */
  2135. update_p = update_q = 0;
  2136. }
  2137. /* now write out any block on a failed drive,
  2138. * or P or Q if they need it
  2139. */
  2140. if (failed == 2) {
  2141. dev = &sh->dev[failed_num[1]];
  2142. locked++;
  2143. set_bit(R5_LOCKED, &dev->flags);
  2144. set_bit(R5_Wantwrite, &dev->flags);
  2145. }
  2146. if (failed >= 1) {
  2147. dev = &sh->dev[failed_num[0]];
  2148. locked++;
  2149. set_bit(R5_LOCKED, &dev->flags);
  2150. set_bit(R5_Wantwrite, &dev->flags);
  2151. }
  2152. if (update_p) {
  2153. dev = &sh->dev[pd_idx];
  2154. locked ++;
  2155. set_bit(R5_LOCKED, &dev->flags);
  2156. set_bit(R5_Wantwrite, &dev->flags);
  2157. }
  2158. if (update_q) {
  2159. dev = &sh->dev[qd_idx];
  2160. locked++;
  2161. set_bit(R5_LOCKED, &dev->flags);
  2162. set_bit(R5_Wantwrite, &dev->flags);
  2163. }
  2164. clear_bit(STRIPE_DEGRADED, &sh->state);
  2165. set_bit(STRIPE_INSYNC, &sh->state);
  2166. }
  2167. }
  2168. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2169. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2170. clear_bit(STRIPE_SYNCING, &sh->state);
  2171. }
  2172. /* If the failed drives are just a ReadError, then we might need
  2173. * to progress the repair/check process
  2174. */
  2175. if (failed <= 2 && ! conf->mddev->ro)
  2176. for (i=0; i<failed;i++) {
  2177. dev = &sh->dev[failed_num[i]];
  2178. if (test_bit(R5_ReadError, &dev->flags)
  2179. && !test_bit(R5_LOCKED, &dev->flags)
  2180. && test_bit(R5_UPTODATE, &dev->flags)
  2181. ) {
  2182. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2183. set_bit(R5_Wantwrite, &dev->flags);
  2184. set_bit(R5_ReWrite, &dev->flags);
  2185. set_bit(R5_LOCKED, &dev->flags);
  2186. } else {
  2187. /* let's read it back */
  2188. set_bit(R5_Wantread, &dev->flags);
  2189. set_bit(R5_LOCKED, &dev->flags);
  2190. }
  2191. }
  2192. }
  2193. spin_unlock(&sh->lock);
  2194. while ((bi=return_bi)) {
  2195. int bytes = bi->bi_size;
  2196. return_bi = bi->bi_next;
  2197. bi->bi_next = NULL;
  2198. bi->bi_size = 0;
  2199. bi->bi_end_io(bi, bytes, 0);
  2200. }
  2201. for (i=disks; i-- ;) {
  2202. int rw;
  2203. struct bio *bi;
  2204. mdk_rdev_t *rdev;
  2205. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  2206. rw = 1;
  2207. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  2208. rw = 0;
  2209. else
  2210. continue;
  2211. bi = &sh->dev[i].req;
  2212. bi->bi_rw = rw;
  2213. if (rw)
  2214. bi->bi_end_io = raid5_end_write_request;
  2215. else
  2216. bi->bi_end_io = raid5_end_read_request;
  2217. rcu_read_lock();
  2218. rdev = rcu_dereference(conf->disks[i].rdev);
  2219. if (rdev && test_bit(Faulty, &rdev->flags))
  2220. rdev = NULL;
  2221. if (rdev)
  2222. atomic_inc(&rdev->nr_pending);
  2223. rcu_read_unlock();
  2224. if (rdev) {
  2225. if (syncing)
  2226. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  2227. bi->bi_bdev = rdev->bdev;
  2228. PRINTK("for %llu schedule op %ld on disc %d\n",
  2229. (unsigned long long)sh->sector, bi->bi_rw, i);
  2230. atomic_inc(&sh->count);
  2231. bi->bi_sector = sh->sector + rdev->data_offset;
  2232. bi->bi_flags = 1 << BIO_UPTODATE;
  2233. bi->bi_vcnt = 1;
  2234. bi->bi_max_vecs = 1;
  2235. bi->bi_idx = 0;
  2236. bi->bi_io_vec = &sh->dev[i].vec;
  2237. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  2238. bi->bi_io_vec[0].bv_offset = 0;
  2239. bi->bi_size = STRIPE_SIZE;
  2240. bi->bi_next = NULL;
  2241. if (rw == WRITE &&
  2242. test_bit(R5_ReWrite, &sh->dev[i].flags))
  2243. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  2244. generic_make_request(bi);
  2245. } else {
  2246. if (rw == 1)
  2247. set_bit(STRIPE_DEGRADED, &sh->state);
  2248. PRINTK("skip op %ld on disc %d for sector %llu\n",
  2249. bi->bi_rw, i, (unsigned long long)sh->sector);
  2250. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2251. set_bit(STRIPE_HANDLE, &sh->state);
  2252. }
  2253. }
  2254. }
  2255. static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
  2256. {
  2257. if (sh->raid_conf->level == 6)
  2258. handle_stripe6(sh, tmp_page);
  2259. else
  2260. handle_stripe5(sh);
  2261. }
  2262. static void raid5_activate_delayed(raid5_conf_t *conf)
  2263. {
  2264. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  2265. while (!list_empty(&conf->delayed_list)) {
  2266. struct list_head *l = conf->delayed_list.next;
  2267. struct stripe_head *sh;
  2268. sh = list_entry(l, struct stripe_head, lru);
  2269. list_del_init(l);
  2270. clear_bit(STRIPE_DELAYED, &sh->state);
  2271. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2272. atomic_inc(&conf->preread_active_stripes);
  2273. list_add_tail(&sh->lru, &conf->handle_list);
  2274. }
  2275. }
  2276. }
  2277. static void activate_bit_delay(raid5_conf_t *conf)
  2278. {
  2279. /* device_lock is held */
  2280. struct list_head head;
  2281. list_add(&head, &conf->bitmap_list);
  2282. list_del_init(&conf->bitmap_list);
  2283. while (!list_empty(&head)) {
  2284. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  2285. list_del_init(&sh->lru);
  2286. atomic_inc(&sh->count);
  2287. __release_stripe(conf, sh);
  2288. }
  2289. }
  2290. static void unplug_slaves(mddev_t *mddev)
  2291. {
  2292. raid5_conf_t *conf = mddev_to_conf(mddev);
  2293. int i;
  2294. rcu_read_lock();
  2295. for (i=0; i<mddev->raid_disks; i++) {
  2296. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2297. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  2298. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  2299. atomic_inc(&rdev->nr_pending);
  2300. rcu_read_unlock();
  2301. if (r_queue->unplug_fn)
  2302. r_queue->unplug_fn(r_queue);
  2303. rdev_dec_pending(rdev, mddev);
  2304. rcu_read_lock();
  2305. }
  2306. }
  2307. rcu_read_unlock();
  2308. }
  2309. static void raid5_unplug_device(request_queue_t *q)
  2310. {
  2311. mddev_t *mddev = q->queuedata;
  2312. raid5_conf_t *conf = mddev_to_conf(mddev);
  2313. unsigned long flags;
  2314. spin_lock_irqsave(&conf->device_lock, flags);
  2315. if (blk_remove_plug(q)) {
  2316. conf->seq_flush++;
  2317. raid5_activate_delayed(conf);
  2318. }
  2319. md_wakeup_thread(mddev->thread);
  2320. spin_unlock_irqrestore(&conf->device_lock, flags);
  2321. unplug_slaves(mddev);
  2322. }
  2323. static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
  2324. sector_t *error_sector)
  2325. {
  2326. mddev_t *mddev = q->queuedata;
  2327. raid5_conf_t *conf = mddev_to_conf(mddev);
  2328. int i, ret = 0;
  2329. rcu_read_lock();
  2330. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  2331. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2332. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  2333. struct block_device *bdev = rdev->bdev;
  2334. request_queue_t *r_queue = bdev_get_queue(bdev);
  2335. if (!r_queue->issue_flush_fn)
  2336. ret = -EOPNOTSUPP;
  2337. else {
  2338. atomic_inc(&rdev->nr_pending);
  2339. rcu_read_unlock();
  2340. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  2341. error_sector);
  2342. rdev_dec_pending(rdev, mddev);
  2343. rcu_read_lock();
  2344. }
  2345. }
  2346. }
  2347. rcu_read_unlock();
  2348. return ret;
  2349. }
  2350. static int make_request(request_queue_t *q, struct bio * bi)
  2351. {
  2352. mddev_t *mddev = q->queuedata;
  2353. raid5_conf_t *conf = mddev_to_conf(mddev);
  2354. unsigned int dd_idx, pd_idx;
  2355. sector_t new_sector;
  2356. sector_t logical_sector, last_sector;
  2357. struct stripe_head *sh;
  2358. const int rw = bio_data_dir(bi);
  2359. int remaining;
  2360. if (unlikely(bio_barrier(bi))) {
  2361. bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
  2362. return 0;
  2363. }
  2364. md_write_start(mddev, bi);
  2365. disk_stat_inc(mddev->gendisk, ios[rw]);
  2366. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
  2367. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  2368. last_sector = bi->bi_sector + (bi->bi_size>>9);
  2369. bi->bi_next = NULL;
  2370. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  2371. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  2372. DEFINE_WAIT(w);
  2373. int disks, data_disks;
  2374. retry:
  2375. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  2376. if (likely(conf->expand_progress == MaxSector))
  2377. disks = conf->raid_disks;
  2378. else {
  2379. /* spinlock is needed as expand_progress may be
  2380. * 64bit on a 32bit platform, and so it might be
  2381. * possible to see a half-updated value
  2382. * Ofcourse expand_progress could change after
  2383. * the lock is dropped, so once we get a reference
  2384. * to the stripe that we think it is, we will have
  2385. * to check again.
  2386. */
  2387. spin_lock_irq(&conf->device_lock);
  2388. disks = conf->raid_disks;
  2389. if (logical_sector >= conf->expand_progress)
  2390. disks = conf->previous_raid_disks;
  2391. else {
  2392. if (logical_sector >= conf->expand_lo) {
  2393. spin_unlock_irq(&conf->device_lock);
  2394. schedule();
  2395. goto retry;
  2396. }
  2397. }
  2398. spin_unlock_irq(&conf->device_lock);
  2399. }
  2400. data_disks = disks - conf->max_degraded;
  2401. new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
  2402. &dd_idx, &pd_idx, conf);
  2403. PRINTK("raid5: make_request, sector %llu logical %llu\n",
  2404. (unsigned long long)new_sector,
  2405. (unsigned long long)logical_sector);
  2406. sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
  2407. if (sh) {
  2408. if (unlikely(conf->expand_progress != MaxSector)) {
  2409. /* expansion might have moved on while waiting for a
  2410. * stripe, so we must do the range check again.
  2411. * Expansion could still move past after this
  2412. * test, but as we are holding a reference to
  2413. * 'sh', we know that if that happens,
  2414. * STRIPE_EXPANDING will get set and the expansion
  2415. * won't proceed until we finish with the stripe.
  2416. */
  2417. int must_retry = 0;
  2418. spin_lock_irq(&conf->device_lock);
  2419. if (logical_sector < conf->expand_progress &&
  2420. disks == conf->previous_raid_disks)
  2421. /* mismatch, need to try again */
  2422. must_retry = 1;
  2423. spin_unlock_irq(&conf->device_lock);
  2424. if (must_retry) {
  2425. release_stripe(sh);
  2426. goto retry;
  2427. }
  2428. }
  2429. /* FIXME what if we get a false positive because these
  2430. * are being updated.
  2431. */
  2432. if (logical_sector >= mddev->suspend_lo &&
  2433. logical_sector < mddev->suspend_hi) {
  2434. release_stripe(sh);
  2435. schedule();
  2436. goto retry;
  2437. }
  2438. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  2439. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  2440. /* Stripe is busy expanding or
  2441. * add failed due to overlap. Flush everything
  2442. * and wait a while
  2443. */
  2444. raid5_unplug_device(mddev->queue);
  2445. release_stripe(sh);
  2446. schedule();
  2447. goto retry;
  2448. }
  2449. finish_wait(&conf->wait_for_overlap, &w);
  2450. handle_stripe(sh, NULL);
  2451. release_stripe(sh);
  2452. } else {
  2453. /* cannot get stripe for read-ahead, just give-up */
  2454. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2455. finish_wait(&conf->wait_for_overlap, &w);
  2456. break;
  2457. }
  2458. }
  2459. spin_lock_irq(&conf->device_lock);
  2460. remaining = --bi->bi_phys_segments;
  2461. spin_unlock_irq(&conf->device_lock);
  2462. if (remaining == 0) {
  2463. int bytes = bi->bi_size;
  2464. if ( rw == WRITE )
  2465. md_write_end(mddev);
  2466. bi->bi_size = 0;
  2467. bi->bi_end_io(bi, bytes, 0);
  2468. }
  2469. return 0;
  2470. }
  2471. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  2472. {
  2473. /* reshaping is quite different to recovery/resync so it is
  2474. * handled quite separately ... here.
  2475. *
  2476. * On each call to sync_request, we gather one chunk worth of
  2477. * destination stripes and flag them as expanding.
  2478. * Then we find all the source stripes and request reads.
  2479. * As the reads complete, handle_stripe will copy the data
  2480. * into the destination stripe and release that stripe.
  2481. */
  2482. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2483. struct stripe_head *sh;
  2484. int pd_idx;
  2485. sector_t first_sector, last_sector;
  2486. int raid_disks;
  2487. int data_disks;
  2488. int i;
  2489. int dd_idx;
  2490. sector_t writepos, safepos, gap;
  2491. if (sector_nr == 0 &&
  2492. conf->expand_progress != 0) {
  2493. /* restarting in the middle, skip the initial sectors */
  2494. sector_nr = conf->expand_progress;
  2495. sector_div(sector_nr, conf->raid_disks-1);
  2496. *skipped = 1;
  2497. return sector_nr;
  2498. }
  2499. /* we update the metadata when there is more than 3Meg
  2500. * in the block range (that is rather arbitrary, should
  2501. * probably be time based) or when the data about to be
  2502. * copied would over-write the source of the data at
  2503. * the front of the range.
  2504. * i.e. one new_stripe forward from expand_progress new_maps
  2505. * to after where expand_lo old_maps to
  2506. */
  2507. writepos = conf->expand_progress +
  2508. conf->chunk_size/512*(conf->raid_disks-1);
  2509. sector_div(writepos, conf->raid_disks-1);
  2510. safepos = conf->expand_lo;
  2511. sector_div(safepos, conf->previous_raid_disks-1);
  2512. gap = conf->expand_progress - conf->expand_lo;
  2513. if (writepos >= safepos ||
  2514. gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
  2515. /* Cannot proceed until we've updated the superblock... */
  2516. wait_event(conf->wait_for_overlap,
  2517. atomic_read(&conf->reshape_stripes)==0);
  2518. mddev->reshape_position = conf->expand_progress;
  2519. mddev->sb_dirty = 1;
  2520. md_wakeup_thread(mddev->thread);
  2521. wait_event(mddev->sb_wait, mddev->sb_dirty == 0 ||
  2522. kthread_should_stop());
  2523. spin_lock_irq(&conf->device_lock);
  2524. conf->expand_lo = mddev->reshape_position;
  2525. spin_unlock_irq(&conf->device_lock);
  2526. wake_up(&conf->wait_for_overlap);
  2527. }
  2528. for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
  2529. int j;
  2530. int skipped = 0;
  2531. pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
  2532. sh = get_active_stripe(conf, sector_nr+i,
  2533. conf->raid_disks, pd_idx, 0);
  2534. set_bit(STRIPE_EXPANDING, &sh->state);
  2535. atomic_inc(&conf->reshape_stripes);
  2536. /* If any of this stripe is beyond the end of the old
  2537. * array, then we need to zero those blocks
  2538. */
  2539. for (j=sh->disks; j--;) {
  2540. sector_t s;
  2541. if (j == sh->pd_idx)
  2542. continue;
  2543. s = compute_blocknr(sh, j);
  2544. if (s < (mddev->array_size<<1)) {
  2545. skipped = 1;
  2546. continue;
  2547. }
  2548. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  2549. set_bit(R5_Expanded, &sh->dev[j].flags);
  2550. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  2551. }
  2552. if (!skipped) {
  2553. set_bit(STRIPE_EXPAND_READY, &sh->state);
  2554. set_bit(STRIPE_HANDLE, &sh->state);
  2555. }
  2556. release_stripe(sh);
  2557. }
  2558. spin_lock_irq(&conf->device_lock);
  2559. conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
  2560. spin_unlock_irq(&conf->device_lock);
  2561. /* Ok, those stripe are ready. We can start scheduling
  2562. * reads on the source stripes.
  2563. * The source stripes are determined by mapping the first and last
  2564. * block on the destination stripes.
  2565. */
  2566. raid_disks = conf->previous_raid_disks;
  2567. data_disks = raid_disks - 1;
  2568. first_sector =
  2569. raid5_compute_sector(sector_nr*(conf->raid_disks-1),
  2570. raid_disks, data_disks,
  2571. &dd_idx, &pd_idx, conf);
  2572. last_sector =
  2573. raid5_compute_sector((sector_nr+conf->chunk_size/512)
  2574. *(conf->raid_disks-1) -1,
  2575. raid_disks, data_disks,
  2576. &dd_idx, &pd_idx, conf);
  2577. if (last_sector >= (mddev->size<<1))
  2578. last_sector = (mddev->size<<1)-1;
  2579. while (first_sector <= last_sector) {
  2580. pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
  2581. sh = get_active_stripe(conf, first_sector,
  2582. conf->previous_raid_disks, pd_idx, 0);
  2583. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2584. set_bit(STRIPE_HANDLE, &sh->state);
  2585. release_stripe(sh);
  2586. first_sector += STRIPE_SECTORS;
  2587. }
  2588. return conf->chunk_size>>9;
  2589. }
  2590. /* FIXME go_faster isn't used */
  2591. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  2592. {
  2593. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2594. struct stripe_head *sh;
  2595. int pd_idx;
  2596. int raid_disks = conf->raid_disks;
  2597. sector_t max_sector = mddev->size << 1;
  2598. int sync_blocks;
  2599. int still_degraded = 0;
  2600. int i;
  2601. if (sector_nr >= max_sector) {
  2602. /* just being told to finish up .. nothing much to do */
  2603. unplug_slaves(mddev);
  2604. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  2605. end_reshape(conf);
  2606. return 0;
  2607. }
  2608. if (mddev->curr_resync < max_sector) /* aborted */
  2609. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2610. &sync_blocks, 1);
  2611. else /* completed sync */
  2612. conf->fullsync = 0;
  2613. bitmap_close_sync(mddev->bitmap);
  2614. return 0;
  2615. }
  2616. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2617. return reshape_request(mddev, sector_nr, skipped);
  2618. /* if there is too many failed drives and we are trying
  2619. * to resync, then assert that we are finished, because there is
  2620. * nothing we can do.
  2621. */
  2622. if (mddev->degraded >= conf->max_degraded &&
  2623. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2624. sector_t rv = (mddev->size << 1) - sector_nr;
  2625. *skipped = 1;
  2626. return rv;
  2627. }
  2628. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  2629. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  2630. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  2631. /* we can skip this block, and probably more */
  2632. sync_blocks /= STRIPE_SECTORS;
  2633. *skipped = 1;
  2634. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  2635. }
  2636. pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
  2637. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
  2638. if (sh == NULL) {
  2639. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
  2640. /* make sure we don't swamp the stripe cache if someone else
  2641. * is trying to get access
  2642. */
  2643. schedule_timeout_uninterruptible(1);
  2644. }
  2645. /* Need to check if array will still be degraded after recovery/resync
  2646. * We don't need to check the 'failed' flag as when that gets set,
  2647. * recovery aborts.
  2648. */
  2649. for (i=0; i<mddev->raid_disks; i++)
  2650. if (conf->disks[i].rdev == NULL)
  2651. still_degraded = 1;
  2652. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  2653. spin_lock(&sh->lock);
  2654. set_bit(STRIPE_SYNCING, &sh->state);
  2655. clear_bit(STRIPE_INSYNC, &sh->state);
  2656. spin_unlock(&sh->lock);
  2657. handle_stripe(sh, NULL);
  2658. release_stripe(sh);
  2659. return STRIPE_SECTORS;
  2660. }
  2661. /*
  2662. * This is our raid5 kernel thread.
  2663. *
  2664. * We scan the hash table for stripes which can be handled now.
  2665. * During the scan, completed stripes are saved for us by the interrupt
  2666. * handler, so that they will not have to wait for our next wakeup.
  2667. */
  2668. static void raid5d (mddev_t *mddev)
  2669. {
  2670. struct stripe_head *sh;
  2671. raid5_conf_t *conf = mddev_to_conf(mddev);
  2672. int handled;
  2673. PRINTK("+++ raid5d active\n");
  2674. md_check_recovery(mddev);
  2675. handled = 0;
  2676. spin_lock_irq(&conf->device_lock);
  2677. while (1) {
  2678. struct list_head *first;
  2679. if (conf->seq_flush != conf->seq_write) {
  2680. int seq = conf->seq_flush;
  2681. spin_unlock_irq(&conf->device_lock);
  2682. bitmap_unplug(mddev->bitmap);
  2683. spin_lock_irq(&conf->device_lock);
  2684. conf->seq_write = seq;
  2685. activate_bit_delay(conf);
  2686. }
  2687. if (list_empty(&conf->handle_list) &&
  2688. atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
  2689. !blk_queue_plugged(mddev->queue) &&
  2690. !list_empty(&conf->delayed_list))
  2691. raid5_activate_delayed(conf);
  2692. if (list_empty(&conf->handle_list))
  2693. break;
  2694. first = conf->handle_list.next;
  2695. sh = list_entry(first, struct stripe_head, lru);
  2696. list_del_init(first);
  2697. atomic_inc(&sh->count);
  2698. BUG_ON(atomic_read(&sh->count)!= 1);
  2699. spin_unlock_irq(&conf->device_lock);
  2700. handled++;
  2701. handle_stripe(sh, conf->spare_page);
  2702. release_stripe(sh);
  2703. spin_lock_irq(&conf->device_lock);
  2704. }
  2705. PRINTK("%d stripes handled\n", handled);
  2706. spin_unlock_irq(&conf->device_lock);
  2707. unplug_slaves(mddev);
  2708. PRINTK("--- raid5d inactive\n");
  2709. }
  2710. static ssize_t
  2711. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  2712. {
  2713. raid5_conf_t *conf = mddev_to_conf(mddev);
  2714. if (conf)
  2715. return sprintf(page, "%d\n", conf->max_nr_stripes);
  2716. else
  2717. return 0;
  2718. }
  2719. static ssize_t
  2720. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  2721. {
  2722. raid5_conf_t *conf = mddev_to_conf(mddev);
  2723. char *end;
  2724. int new;
  2725. if (len >= PAGE_SIZE)
  2726. return -EINVAL;
  2727. if (!conf)
  2728. return -ENODEV;
  2729. new = simple_strtoul(page, &end, 10);
  2730. if (!*page || (*end && *end != '\n') )
  2731. return -EINVAL;
  2732. if (new <= 16 || new > 32768)
  2733. return -EINVAL;
  2734. while (new < conf->max_nr_stripes) {
  2735. if (drop_one_stripe(conf))
  2736. conf->max_nr_stripes--;
  2737. else
  2738. break;
  2739. }
  2740. while (new > conf->max_nr_stripes) {
  2741. if (grow_one_stripe(conf))
  2742. conf->max_nr_stripes++;
  2743. else break;
  2744. }
  2745. return len;
  2746. }
  2747. static struct md_sysfs_entry
  2748. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  2749. raid5_show_stripe_cache_size,
  2750. raid5_store_stripe_cache_size);
  2751. static ssize_t
  2752. stripe_cache_active_show(mddev_t *mddev, char *page)
  2753. {
  2754. raid5_conf_t *conf = mddev_to_conf(mddev);
  2755. if (conf)
  2756. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  2757. else
  2758. return 0;
  2759. }
  2760. static struct md_sysfs_entry
  2761. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  2762. static struct attribute *raid5_attrs[] = {
  2763. &raid5_stripecache_size.attr,
  2764. &raid5_stripecache_active.attr,
  2765. NULL,
  2766. };
  2767. static struct attribute_group raid5_attrs_group = {
  2768. .name = NULL,
  2769. .attrs = raid5_attrs,
  2770. };
  2771. static int run(mddev_t *mddev)
  2772. {
  2773. raid5_conf_t *conf;
  2774. int raid_disk, memory;
  2775. mdk_rdev_t *rdev;
  2776. struct disk_info *disk;
  2777. struct list_head *tmp;
  2778. if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
  2779. printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
  2780. mdname(mddev), mddev->level);
  2781. return -EIO;
  2782. }
  2783. if (mddev->reshape_position != MaxSector) {
  2784. /* Check that we can continue the reshape.
  2785. * Currently only disks can change, it must
  2786. * increase, and we must be past the point where
  2787. * a stripe over-writes itself
  2788. */
  2789. sector_t here_new, here_old;
  2790. int old_disks;
  2791. if (mddev->new_level != mddev->level ||
  2792. mddev->new_layout != mddev->layout ||
  2793. mddev->new_chunk != mddev->chunk_size) {
  2794. printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
  2795. mdname(mddev));
  2796. return -EINVAL;
  2797. }
  2798. if (mddev->delta_disks <= 0) {
  2799. printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
  2800. mdname(mddev));
  2801. return -EINVAL;
  2802. }
  2803. old_disks = mddev->raid_disks - mddev->delta_disks;
  2804. /* reshape_position must be on a new-stripe boundary, and one
  2805. * further up in new geometry must map after here in old geometry.
  2806. */
  2807. here_new = mddev->reshape_position;
  2808. if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
  2809. printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
  2810. return -EINVAL;
  2811. }
  2812. /* here_new is the stripe we will write to */
  2813. here_old = mddev->reshape_position;
  2814. sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
  2815. /* here_old is the first stripe that we might need to read from */
  2816. if (here_new >= here_old) {
  2817. /* Reading from the same stripe as writing to - bad */
  2818. printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
  2819. return -EINVAL;
  2820. }
  2821. printk(KERN_INFO "raid5: reshape will continue\n");
  2822. /* OK, we should be able to continue; */
  2823. }
  2824. mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
  2825. if ((conf = mddev->private) == NULL)
  2826. goto abort;
  2827. if (mddev->reshape_position == MaxSector) {
  2828. conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
  2829. } else {
  2830. conf->raid_disks = mddev->raid_disks;
  2831. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  2832. }
  2833. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  2834. GFP_KERNEL);
  2835. if (!conf->disks)
  2836. goto abort;
  2837. conf->mddev = mddev;
  2838. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  2839. goto abort;
  2840. if (mddev->level == 6) {
  2841. conf->spare_page = alloc_page(GFP_KERNEL);
  2842. if (!conf->spare_page)
  2843. goto abort;
  2844. }
  2845. spin_lock_init(&conf->device_lock);
  2846. init_waitqueue_head(&conf->wait_for_stripe);
  2847. init_waitqueue_head(&conf->wait_for_overlap);
  2848. INIT_LIST_HEAD(&conf->handle_list);
  2849. INIT_LIST_HEAD(&conf->delayed_list);
  2850. INIT_LIST_HEAD(&conf->bitmap_list);
  2851. INIT_LIST_HEAD(&conf->inactive_list);
  2852. atomic_set(&conf->active_stripes, 0);
  2853. atomic_set(&conf->preread_active_stripes, 0);
  2854. PRINTK("raid5: run(%s) called.\n", mdname(mddev));
  2855. ITERATE_RDEV(mddev,rdev,tmp) {
  2856. raid_disk = rdev->raid_disk;
  2857. if (raid_disk >= conf->raid_disks
  2858. || raid_disk < 0)
  2859. continue;
  2860. disk = conf->disks + raid_disk;
  2861. disk->rdev = rdev;
  2862. if (test_bit(In_sync, &rdev->flags)) {
  2863. char b[BDEVNAME_SIZE];
  2864. printk(KERN_INFO "raid5: device %s operational as raid"
  2865. " disk %d\n", bdevname(rdev->bdev,b),
  2866. raid_disk);
  2867. conf->working_disks++;
  2868. }
  2869. }
  2870. /*
  2871. * 0 for a fully functional array, 1 or 2 for a degraded array.
  2872. */
  2873. mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
  2874. conf->mddev = mddev;
  2875. conf->chunk_size = mddev->chunk_size;
  2876. conf->level = mddev->level;
  2877. if (conf->level == 6)
  2878. conf->max_degraded = 2;
  2879. else
  2880. conf->max_degraded = 1;
  2881. conf->algorithm = mddev->layout;
  2882. conf->max_nr_stripes = NR_STRIPES;
  2883. conf->expand_progress = mddev->reshape_position;
  2884. /* device size must be a multiple of chunk size */
  2885. mddev->size &= ~(mddev->chunk_size/1024 -1);
  2886. mddev->resync_max_sectors = mddev->size << 1;
  2887. if (conf->level == 6 && conf->raid_disks < 4) {
  2888. printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
  2889. mdname(mddev), conf->raid_disks);
  2890. goto abort;
  2891. }
  2892. if (!conf->chunk_size || conf->chunk_size % 4) {
  2893. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  2894. conf->chunk_size, mdname(mddev));
  2895. goto abort;
  2896. }
  2897. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  2898. printk(KERN_ERR
  2899. "raid5: unsupported parity algorithm %d for %s\n",
  2900. conf->algorithm, mdname(mddev));
  2901. goto abort;
  2902. }
  2903. if (mddev->degraded > conf->max_degraded) {
  2904. printk(KERN_ERR "raid5: not enough operational devices for %s"
  2905. " (%d/%d failed)\n",
  2906. mdname(mddev), conf->failed_disks, conf->raid_disks);
  2907. goto abort;
  2908. }
  2909. if (mddev->degraded > 0 &&
  2910. mddev->recovery_cp != MaxSector) {
  2911. if (mddev->ok_start_degraded)
  2912. printk(KERN_WARNING
  2913. "raid5: starting dirty degraded array: %s"
  2914. "- data corruption possible.\n",
  2915. mdname(mddev));
  2916. else {
  2917. printk(KERN_ERR
  2918. "raid5: cannot start dirty degraded array for %s\n",
  2919. mdname(mddev));
  2920. goto abort;
  2921. }
  2922. }
  2923. {
  2924. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  2925. if (!mddev->thread) {
  2926. printk(KERN_ERR
  2927. "raid5: couldn't allocate thread for %s\n",
  2928. mdname(mddev));
  2929. goto abort;
  2930. }
  2931. }
  2932. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  2933. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  2934. if (grow_stripes(conf, conf->max_nr_stripes)) {
  2935. printk(KERN_ERR
  2936. "raid5: couldn't allocate %dkB for buffers\n", memory);
  2937. shrink_stripes(conf);
  2938. md_unregister_thread(mddev->thread);
  2939. goto abort;
  2940. } else
  2941. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  2942. memory, mdname(mddev));
  2943. if (mddev->degraded == 0)
  2944. printk("raid5: raid level %d set %s active with %d out of %d"
  2945. " devices, algorithm %d\n", conf->level, mdname(mddev),
  2946. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  2947. conf->algorithm);
  2948. else
  2949. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  2950. " out of %d devices, algorithm %d\n", conf->level,
  2951. mdname(mddev), mddev->raid_disks - mddev->degraded,
  2952. mddev->raid_disks, conf->algorithm);
  2953. print_raid5_conf(conf);
  2954. if (conf->expand_progress != MaxSector) {
  2955. printk("...ok start reshape thread\n");
  2956. conf->expand_lo = conf->expand_progress;
  2957. atomic_set(&conf->reshape_stripes, 0);
  2958. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  2959. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  2960. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  2961. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  2962. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  2963. "%s_reshape");
  2964. }
  2965. /* read-ahead size must cover two whole stripes, which is
  2966. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  2967. */
  2968. {
  2969. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  2970. int stripe = data_disks *
  2971. (mddev->chunk_size / PAGE_SIZE);
  2972. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  2973. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  2974. }
  2975. /* Ok, everything is just fine now */
  2976. sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
  2977. mddev->queue->unplug_fn = raid5_unplug_device;
  2978. mddev->queue->issue_flush_fn = raid5_issue_flush;
  2979. mddev->array_size = mddev->size * (conf->previous_raid_disks -
  2980. conf->max_degraded);
  2981. return 0;
  2982. abort:
  2983. if (conf) {
  2984. print_raid5_conf(conf);
  2985. safe_put_page(conf->spare_page);
  2986. kfree(conf->disks);
  2987. kfree(conf->stripe_hashtbl);
  2988. kfree(conf);
  2989. }
  2990. mddev->private = NULL;
  2991. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  2992. return -EIO;
  2993. }
  2994. static int stop(mddev_t *mddev)
  2995. {
  2996. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2997. md_unregister_thread(mddev->thread);
  2998. mddev->thread = NULL;
  2999. shrink_stripes(conf);
  3000. kfree(conf->stripe_hashtbl);
  3001. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  3002. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  3003. kfree(conf->disks);
  3004. kfree(conf);
  3005. mddev->private = NULL;
  3006. return 0;
  3007. }
  3008. #if RAID5_DEBUG
  3009. static void print_sh (struct seq_file *seq, struct stripe_head *sh)
  3010. {
  3011. int i;
  3012. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  3013. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  3014. seq_printf(seq, "sh %llu, count %d.\n",
  3015. (unsigned long long)sh->sector, atomic_read(&sh->count));
  3016. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  3017. for (i = 0; i < sh->disks; i++) {
  3018. seq_printf(seq, "(cache%d: %p %ld) ",
  3019. i, sh->dev[i].page, sh->dev[i].flags);
  3020. }
  3021. seq_printf(seq, "\n");
  3022. }
  3023. static void printall (struct seq_file *seq, raid5_conf_t *conf)
  3024. {
  3025. struct stripe_head *sh;
  3026. struct hlist_node *hn;
  3027. int i;
  3028. spin_lock_irq(&conf->device_lock);
  3029. for (i = 0; i < NR_HASH; i++) {
  3030. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  3031. if (sh->raid_conf != conf)
  3032. continue;
  3033. print_sh(seq, sh);
  3034. }
  3035. }
  3036. spin_unlock_irq(&conf->device_lock);
  3037. }
  3038. #endif
  3039. static void status (struct seq_file *seq, mddev_t *mddev)
  3040. {
  3041. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3042. int i;
  3043. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  3044. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
  3045. for (i = 0; i < conf->raid_disks; i++)
  3046. seq_printf (seq, "%s",
  3047. conf->disks[i].rdev &&
  3048. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  3049. seq_printf (seq, "]");
  3050. #if RAID5_DEBUG
  3051. seq_printf (seq, "\n");
  3052. printall(seq, conf);
  3053. #endif
  3054. }
  3055. static void print_raid5_conf (raid5_conf_t *conf)
  3056. {
  3057. int i;
  3058. struct disk_info *tmp;
  3059. printk("RAID5 conf printout:\n");
  3060. if (!conf) {
  3061. printk("(conf==NULL)\n");
  3062. return;
  3063. }
  3064. printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
  3065. conf->working_disks, conf->failed_disks);
  3066. for (i = 0; i < conf->raid_disks; i++) {
  3067. char b[BDEVNAME_SIZE];
  3068. tmp = conf->disks + i;
  3069. if (tmp->rdev)
  3070. printk(" disk %d, o:%d, dev:%s\n",
  3071. i, !test_bit(Faulty, &tmp->rdev->flags),
  3072. bdevname(tmp->rdev->bdev,b));
  3073. }
  3074. }
  3075. static int raid5_spare_active(mddev_t *mddev)
  3076. {
  3077. int i;
  3078. raid5_conf_t *conf = mddev->private;
  3079. struct disk_info *tmp;
  3080. for (i = 0; i < conf->raid_disks; i++) {
  3081. tmp = conf->disks + i;
  3082. if (tmp->rdev
  3083. && !test_bit(Faulty, &tmp->rdev->flags)
  3084. && !test_bit(In_sync, &tmp->rdev->flags)) {
  3085. mddev->degraded--;
  3086. conf->failed_disks--;
  3087. conf->working_disks++;
  3088. set_bit(In_sync, &tmp->rdev->flags);
  3089. }
  3090. }
  3091. print_raid5_conf(conf);
  3092. return 0;
  3093. }
  3094. static int raid5_remove_disk(mddev_t *mddev, int number)
  3095. {
  3096. raid5_conf_t *conf = mddev->private;
  3097. int err = 0;
  3098. mdk_rdev_t *rdev;
  3099. struct disk_info *p = conf->disks + number;
  3100. print_raid5_conf(conf);
  3101. rdev = p->rdev;
  3102. if (rdev) {
  3103. if (test_bit(In_sync, &rdev->flags) ||
  3104. atomic_read(&rdev->nr_pending)) {
  3105. err = -EBUSY;
  3106. goto abort;
  3107. }
  3108. p->rdev = NULL;
  3109. synchronize_rcu();
  3110. if (atomic_read(&rdev->nr_pending)) {
  3111. /* lost the race, try later */
  3112. err = -EBUSY;
  3113. p->rdev = rdev;
  3114. }
  3115. }
  3116. abort:
  3117. print_raid5_conf(conf);
  3118. return err;
  3119. }
  3120. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  3121. {
  3122. raid5_conf_t *conf = mddev->private;
  3123. int found = 0;
  3124. int disk;
  3125. struct disk_info *p;
  3126. if (mddev->degraded > conf->max_degraded)
  3127. /* no point adding a device */
  3128. return 0;
  3129. /*
  3130. * find the disk ... but prefer rdev->saved_raid_disk
  3131. * if possible.
  3132. */
  3133. if (rdev->saved_raid_disk >= 0 &&
  3134. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  3135. disk = rdev->saved_raid_disk;
  3136. else
  3137. disk = 0;
  3138. for ( ; disk < conf->raid_disks; disk++)
  3139. if ((p=conf->disks + disk)->rdev == NULL) {
  3140. clear_bit(In_sync, &rdev->flags);
  3141. rdev->raid_disk = disk;
  3142. found = 1;
  3143. if (rdev->saved_raid_disk != disk)
  3144. conf->fullsync = 1;
  3145. rcu_assign_pointer(p->rdev, rdev);
  3146. break;
  3147. }
  3148. print_raid5_conf(conf);
  3149. return found;
  3150. }
  3151. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  3152. {
  3153. /* no resync is happening, and there is enough space
  3154. * on all devices, so we can resize.
  3155. * We need to make sure resync covers any new space.
  3156. * If the array is shrinking we should possibly wait until
  3157. * any io in the removed space completes, but it hardly seems
  3158. * worth it.
  3159. */
  3160. raid5_conf_t *conf = mddev_to_conf(mddev);
  3161. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  3162. mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
  3163. set_capacity(mddev->gendisk, mddev->array_size << 1);
  3164. mddev->changed = 1;
  3165. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  3166. mddev->recovery_cp = mddev->size << 1;
  3167. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3168. }
  3169. mddev->size = sectors /2;
  3170. mddev->resync_max_sectors = sectors;
  3171. return 0;
  3172. }
  3173. #ifdef CONFIG_MD_RAID5_RESHAPE
  3174. static int raid5_check_reshape(mddev_t *mddev)
  3175. {
  3176. raid5_conf_t *conf = mddev_to_conf(mddev);
  3177. int err;
  3178. if (mddev->delta_disks < 0 ||
  3179. mddev->new_level != mddev->level)
  3180. return -EINVAL; /* Cannot shrink array or change level yet */
  3181. if (mddev->delta_disks == 0)
  3182. return 0; /* nothing to do */
  3183. /* Can only proceed if there are plenty of stripe_heads.
  3184. * We need a minimum of one full stripe,, and for sensible progress
  3185. * it is best to have about 4 times that.
  3186. * If we require 4 times, then the default 256 4K stripe_heads will
  3187. * allow for chunk sizes up to 256K, which is probably OK.
  3188. * If the chunk size is greater, user-space should request more
  3189. * stripe_heads first.
  3190. */
  3191. if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
  3192. (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
  3193. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  3194. (mddev->chunk_size / STRIPE_SIZE)*4);
  3195. return -ENOSPC;
  3196. }
  3197. err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  3198. if (err)
  3199. return err;
  3200. /* looks like we might be able to manage this */
  3201. return 0;
  3202. }
  3203. static int raid5_start_reshape(mddev_t *mddev)
  3204. {
  3205. raid5_conf_t *conf = mddev_to_conf(mddev);
  3206. mdk_rdev_t *rdev;
  3207. struct list_head *rtmp;
  3208. int spares = 0;
  3209. int added_devices = 0;
  3210. if (mddev->degraded ||
  3211. test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3212. return -EBUSY;
  3213. ITERATE_RDEV(mddev, rdev, rtmp)
  3214. if (rdev->raid_disk < 0 &&
  3215. !test_bit(Faulty, &rdev->flags))
  3216. spares++;
  3217. if (spares < mddev->delta_disks-1)
  3218. /* Not enough devices even to make a degraded array
  3219. * of that size
  3220. */
  3221. return -EINVAL;
  3222. atomic_set(&conf->reshape_stripes, 0);
  3223. spin_lock_irq(&conf->device_lock);
  3224. conf->previous_raid_disks = conf->raid_disks;
  3225. conf->raid_disks += mddev->delta_disks;
  3226. conf->expand_progress = 0;
  3227. conf->expand_lo = 0;
  3228. spin_unlock_irq(&conf->device_lock);
  3229. /* Add some new drives, as many as will fit.
  3230. * We know there are enough to make the newly sized array work.
  3231. */
  3232. ITERATE_RDEV(mddev, rdev, rtmp)
  3233. if (rdev->raid_disk < 0 &&
  3234. !test_bit(Faulty, &rdev->flags)) {
  3235. if (raid5_add_disk(mddev, rdev)) {
  3236. char nm[20];
  3237. set_bit(In_sync, &rdev->flags);
  3238. conf->working_disks++;
  3239. added_devices++;
  3240. rdev->recovery_offset = 0;
  3241. sprintf(nm, "rd%d", rdev->raid_disk);
  3242. sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
  3243. } else
  3244. break;
  3245. }
  3246. mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
  3247. mddev->raid_disks = conf->raid_disks;
  3248. mddev->reshape_position = 0;
  3249. mddev->sb_dirty = 1;
  3250. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3251. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3252. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3253. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3254. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3255. "%s_reshape");
  3256. if (!mddev->sync_thread) {
  3257. mddev->recovery = 0;
  3258. spin_lock_irq(&conf->device_lock);
  3259. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  3260. conf->expand_progress = MaxSector;
  3261. spin_unlock_irq(&conf->device_lock);
  3262. return -EAGAIN;
  3263. }
  3264. md_wakeup_thread(mddev->sync_thread);
  3265. md_new_event(mddev);
  3266. return 0;
  3267. }
  3268. #endif
  3269. static void end_reshape(raid5_conf_t *conf)
  3270. {
  3271. struct block_device *bdev;
  3272. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  3273. conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
  3274. set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
  3275. conf->mddev->changed = 1;
  3276. bdev = bdget_disk(conf->mddev->gendisk, 0);
  3277. if (bdev) {
  3278. mutex_lock(&bdev->bd_inode->i_mutex);
  3279. i_size_write(bdev->bd_inode, conf->mddev->array_size << 10);
  3280. mutex_unlock(&bdev->bd_inode->i_mutex);
  3281. bdput(bdev);
  3282. }
  3283. spin_lock_irq(&conf->device_lock);
  3284. conf->expand_progress = MaxSector;
  3285. spin_unlock_irq(&conf->device_lock);
  3286. conf->mddev->reshape_position = MaxSector;
  3287. /* read-ahead size must cover two whole stripes, which is
  3288. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3289. */
  3290. {
  3291. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  3292. int stripe = data_disks *
  3293. (conf->mddev->chunk_size / PAGE_SIZE);
  3294. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3295. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3296. }
  3297. }
  3298. }
  3299. static void raid5_quiesce(mddev_t *mddev, int state)
  3300. {
  3301. raid5_conf_t *conf = mddev_to_conf(mddev);
  3302. switch(state) {
  3303. case 2: /* resume for a suspend */
  3304. wake_up(&conf->wait_for_overlap);
  3305. break;
  3306. case 1: /* stop all writes */
  3307. spin_lock_irq(&conf->device_lock);
  3308. conf->quiesce = 1;
  3309. wait_event_lock_irq(conf->wait_for_stripe,
  3310. atomic_read(&conf->active_stripes) == 0,
  3311. conf->device_lock, /* nothing */);
  3312. spin_unlock_irq(&conf->device_lock);
  3313. break;
  3314. case 0: /* re-enable writes */
  3315. spin_lock_irq(&conf->device_lock);
  3316. conf->quiesce = 0;
  3317. wake_up(&conf->wait_for_stripe);
  3318. wake_up(&conf->wait_for_overlap);
  3319. spin_unlock_irq(&conf->device_lock);
  3320. break;
  3321. }
  3322. }
  3323. static struct mdk_personality raid6_personality =
  3324. {
  3325. .name = "raid6",
  3326. .level = 6,
  3327. .owner = THIS_MODULE,
  3328. .make_request = make_request,
  3329. .run = run,
  3330. .stop = stop,
  3331. .status = status,
  3332. .error_handler = error,
  3333. .hot_add_disk = raid5_add_disk,
  3334. .hot_remove_disk= raid5_remove_disk,
  3335. .spare_active = raid5_spare_active,
  3336. .sync_request = sync_request,
  3337. .resize = raid5_resize,
  3338. .quiesce = raid5_quiesce,
  3339. };
  3340. static struct mdk_personality raid5_personality =
  3341. {
  3342. .name = "raid5",
  3343. .level = 5,
  3344. .owner = THIS_MODULE,
  3345. .make_request = make_request,
  3346. .run = run,
  3347. .stop = stop,
  3348. .status = status,
  3349. .error_handler = error,
  3350. .hot_add_disk = raid5_add_disk,
  3351. .hot_remove_disk= raid5_remove_disk,
  3352. .spare_active = raid5_spare_active,
  3353. .sync_request = sync_request,
  3354. .resize = raid5_resize,
  3355. #ifdef CONFIG_MD_RAID5_RESHAPE
  3356. .check_reshape = raid5_check_reshape,
  3357. .start_reshape = raid5_start_reshape,
  3358. #endif
  3359. .quiesce = raid5_quiesce,
  3360. };
  3361. static struct mdk_personality raid4_personality =
  3362. {
  3363. .name = "raid4",
  3364. .level = 4,
  3365. .owner = THIS_MODULE,
  3366. .make_request = make_request,
  3367. .run = run,
  3368. .stop = stop,
  3369. .status = status,
  3370. .error_handler = error,
  3371. .hot_add_disk = raid5_add_disk,
  3372. .hot_remove_disk= raid5_remove_disk,
  3373. .spare_active = raid5_spare_active,
  3374. .sync_request = sync_request,
  3375. .resize = raid5_resize,
  3376. .quiesce = raid5_quiesce,
  3377. };
  3378. static int __init raid5_init(void)
  3379. {
  3380. int e;
  3381. e = raid6_select_algo();
  3382. if ( e )
  3383. return e;
  3384. register_md_personality(&raid6_personality);
  3385. register_md_personality(&raid5_personality);
  3386. register_md_personality(&raid4_personality);
  3387. return 0;
  3388. }
  3389. static void raid5_exit(void)
  3390. {
  3391. unregister_md_personality(&raid6_personality);
  3392. unregister_md_personality(&raid5_personality);
  3393. unregister_md_personality(&raid4_personality);
  3394. }
  3395. module_init(raid5_init);
  3396. module_exit(raid5_exit);
  3397. MODULE_LICENSE("GPL");
  3398. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  3399. MODULE_ALIAS("md-raid5");
  3400. MODULE_ALIAS("md-raid4");
  3401. MODULE_ALIAS("md-level-5");
  3402. MODULE_ALIAS("md-level-4");
  3403. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  3404. MODULE_ALIAS("md-raid6");
  3405. MODULE_ALIAS("md-level-6");
  3406. /* This used to be two separate modules, they were: */
  3407. MODULE_ALIAS("raid5");
  3408. MODULE_ALIAS("raid6");