raid5.c 113 KB

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