raid5.c 108 KB

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