raid5.c 105 KB

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