raid5.c 149 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387
  1. /*
  2. * raid5.c : Multiple Devices driver for Linux
  3. * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  4. * Copyright (C) 1999, 2000 Ingo Molnar
  5. * Copyright (C) 2002, 2003 H. Peter Anvin
  6. *
  7. * RAID-4/5/6 management functions.
  8. * Thanks to Penguin Computing for making the RAID-6 development possible
  9. * by donating a test server!
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * (for example /usr/src/linux/COPYING); if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /*
  21. * BITMAP UNPLUGGING:
  22. *
  23. * The sequencing for updating the bitmap reliably is a little
  24. * subtle (and I got it wrong the first time) so it deserves some
  25. * explanation.
  26. *
  27. * We group bitmap updates into batches. Each batch has a number.
  28. * We may write out several batches at once, but that isn't very important.
  29. * conf->bm_write is the number of the last batch successfully written.
  30. * conf->bm_flush is the number of the last batch that was closed to
  31. * new additions.
  32. * When we discover that we will need to write to any block in a stripe
  33. * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
  34. * the number of the batch it will be in. This is bm_flush+1.
  35. * When we are ready to do a write, if that batch hasn't been written yet,
  36. * we plug the array and queue the stripe for later.
  37. * When an unplug happens, we increment bm_flush, thus closing the current
  38. * batch.
  39. * When we notice that bm_flush > bm_write, we write out all pending updates
  40. * to the bitmap, and advance bm_write to where bm_flush was.
  41. * This may occasionally write a bit out twice, but is sure never to
  42. * miss any bits.
  43. */
  44. #include <linux/blkdev.h>
  45. #include <linux/kthread.h>
  46. #include <linux/raid/pq.h>
  47. #include <linux/async_tx.h>
  48. #include <linux/seq_file.h>
  49. #include "md.h"
  50. #include "raid5.h"
  51. #include "bitmap.h"
  52. /*
  53. * Stripe cache
  54. */
  55. #define NR_STRIPES 256
  56. #define STRIPE_SIZE PAGE_SIZE
  57. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  58. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  59. #define IO_THRESHOLD 1
  60. #define BYPASS_THRESHOLD 1
  61. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  62. #define HASH_MASK (NR_HASH - 1)
  63. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  64. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  65. * order without overlap. There may be several bio's per stripe+device, and
  66. * a bio could span several devices.
  67. * When walking this list for a particular stripe+device, we must never proceed
  68. * beyond a bio that extends past this device, as the next bio might no longer
  69. * be valid.
  70. * This macro is used to determine the 'next' bio in the list, given the sector
  71. * of the current stripe+device
  72. */
  73. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  74. /*
  75. * The following can be used to debug the driver
  76. */
  77. #define RAID5_PARANOIA 1
  78. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  79. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  80. #else
  81. # define CHECK_DEVLOCK()
  82. #endif
  83. #ifdef DEBUG
  84. #define inline
  85. #define __inline__
  86. #endif
  87. #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
  88. /*
  89. * We maintain a biased count of active stripes in the bottom 16 bits of
  90. * bi_phys_segments, and a count of processed stripes in the upper 16 bits
  91. */
  92. static inline int raid5_bi_phys_segments(struct bio *bio)
  93. {
  94. return bio->bi_phys_segments & 0xffff;
  95. }
  96. static inline int raid5_bi_hw_segments(struct bio *bio)
  97. {
  98. return (bio->bi_phys_segments >> 16) & 0xffff;
  99. }
  100. static inline int raid5_dec_bi_phys_segments(struct bio *bio)
  101. {
  102. --bio->bi_phys_segments;
  103. return raid5_bi_phys_segments(bio);
  104. }
  105. static inline int raid5_dec_bi_hw_segments(struct bio *bio)
  106. {
  107. unsigned short val = raid5_bi_hw_segments(bio);
  108. --val;
  109. bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
  110. return val;
  111. }
  112. static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
  113. {
  114. bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
  115. }
  116. /* Find first data disk in a raid6 stripe */
  117. static inline int raid6_d0(struct stripe_head *sh)
  118. {
  119. if (sh->ddf_layout)
  120. /* ddf always start from first device */
  121. return 0;
  122. /* md starts just after Q block */
  123. if (sh->qd_idx == sh->disks - 1)
  124. return 0;
  125. else
  126. return sh->qd_idx + 1;
  127. }
  128. static inline int raid6_next_disk(int disk, int raid_disks)
  129. {
  130. disk++;
  131. return (disk < raid_disks) ? disk : 0;
  132. }
  133. /* When walking through the disks in a raid5, starting at raid6_d0,
  134. * We need to map each disk to a 'slot', where the data disks are slot
  135. * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
  136. * is raid_disks-1. This help does that mapping.
  137. */
  138. static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
  139. int *count, int syndrome_disks)
  140. {
  141. int slot;
  142. if (idx == sh->pd_idx)
  143. return syndrome_disks;
  144. if (idx == sh->qd_idx)
  145. return syndrome_disks + 1;
  146. slot = (*count)++;
  147. return slot;
  148. }
  149. static void return_io(struct bio *return_bi)
  150. {
  151. struct bio *bi = return_bi;
  152. while (bi) {
  153. return_bi = bi->bi_next;
  154. bi->bi_next = NULL;
  155. bi->bi_size = 0;
  156. bio_endio(bi, 0);
  157. bi = return_bi;
  158. }
  159. }
  160. static void print_raid5_conf (raid5_conf_t *conf);
  161. static int stripe_operations_active(struct stripe_head *sh)
  162. {
  163. return sh->check_state || sh->reconstruct_state ||
  164. test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
  165. test_bit(STRIPE_COMPUTE_RUN, &sh->state);
  166. }
  167. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  168. {
  169. if (atomic_dec_and_test(&sh->count)) {
  170. BUG_ON(!list_empty(&sh->lru));
  171. BUG_ON(atomic_read(&conf->active_stripes)==0);
  172. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  173. if (test_bit(STRIPE_DELAYED, &sh->state)) {
  174. list_add_tail(&sh->lru, &conf->delayed_list);
  175. blk_plug_device(conf->mddev->queue);
  176. } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  177. sh->bm_seq - conf->seq_write > 0) {
  178. list_add_tail(&sh->lru, &conf->bitmap_list);
  179. blk_plug_device(conf->mddev->queue);
  180. } else {
  181. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  182. list_add_tail(&sh->lru, &conf->handle_list);
  183. }
  184. md_wakeup_thread(conf->mddev->thread);
  185. } else {
  186. BUG_ON(stripe_operations_active(sh));
  187. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  188. atomic_dec(&conf->preread_active_stripes);
  189. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  190. md_wakeup_thread(conf->mddev->thread);
  191. }
  192. atomic_dec(&conf->active_stripes);
  193. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  194. list_add_tail(&sh->lru, &conf->inactive_list);
  195. wake_up(&conf->wait_for_stripe);
  196. if (conf->retry_read_aligned)
  197. md_wakeup_thread(conf->mddev->thread);
  198. }
  199. }
  200. }
  201. }
  202. static void release_stripe(struct stripe_head *sh)
  203. {
  204. raid5_conf_t *conf = sh->raid_conf;
  205. unsigned long flags;
  206. spin_lock_irqsave(&conf->device_lock, flags);
  207. __release_stripe(conf, sh);
  208. spin_unlock_irqrestore(&conf->device_lock, flags);
  209. }
  210. static inline void remove_hash(struct stripe_head *sh)
  211. {
  212. pr_debug("remove_hash(), stripe %llu\n",
  213. (unsigned long long)sh->sector);
  214. hlist_del_init(&sh->hash);
  215. }
  216. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  217. {
  218. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  219. pr_debug("insert_hash(), stripe %llu\n",
  220. (unsigned long long)sh->sector);
  221. CHECK_DEVLOCK();
  222. hlist_add_head(&sh->hash, hp);
  223. }
  224. /* find an idle stripe, make sure it is unhashed, and return it. */
  225. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  226. {
  227. struct stripe_head *sh = NULL;
  228. struct list_head *first;
  229. CHECK_DEVLOCK();
  230. if (list_empty(&conf->inactive_list))
  231. goto out;
  232. first = conf->inactive_list.next;
  233. sh = list_entry(first, struct stripe_head, lru);
  234. list_del_init(first);
  235. remove_hash(sh);
  236. atomic_inc(&conf->active_stripes);
  237. out:
  238. return sh;
  239. }
  240. static void shrink_buffers(struct stripe_head *sh, int num)
  241. {
  242. struct page *p;
  243. int i;
  244. for (i=0; i<num ; i++) {
  245. p = sh->dev[i].page;
  246. if (!p)
  247. continue;
  248. sh->dev[i].page = NULL;
  249. put_page(p);
  250. }
  251. }
  252. static int grow_buffers(struct stripe_head *sh, int num)
  253. {
  254. int i;
  255. for (i=0; i<num; i++) {
  256. struct page *page;
  257. if (!(page = alloc_page(GFP_KERNEL))) {
  258. return 1;
  259. }
  260. sh->dev[i].page = page;
  261. }
  262. return 0;
  263. }
  264. static void raid5_build_block(struct stripe_head *sh, int i, int previous);
  265. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  266. struct stripe_head *sh);
  267. static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
  268. {
  269. raid5_conf_t *conf = sh->raid_conf;
  270. int i;
  271. BUG_ON(atomic_read(&sh->count) != 0);
  272. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  273. BUG_ON(stripe_operations_active(sh));
  274. CHECK_DEVLOCK();
  275. pr_debug("init_stripe called, stripe %llu\n",
  276. (unsigned long long)sh->sector);
  277. remove_hash(sh);
  278. sh->generation = conf->generation - previous;
  279. sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  280. sh->sector = sector;
  281. stripe_set_idx(sector, conf, previous, sh);
  282. sh->state = 0;
  283. for (i = sh->disks; i--; ) {
  284. struct r5dev *dev = &sh->dev[i];
  285. if (dev->toread || dev->read || dev->towrite || dev->written ||
  286. test_bit(R5_LOCKED, &dev->flags)) {
  287. printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
  288. (unsigned long long)sh->sector, i, dev->toread,
  289. dev->read, dev->towrite, dev->written,
  290. test_bit(R5_LOCKED, &dev->flags));
  291. BUG();
  292. }
  293. dev->flags = 0;
  294. raid5_build_block(sh, i, previous);
  295. }
  296. insert_hash(conf, sh);
  297. }
  298. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
  299. short generation)
  300. {
  301. struct stripe_head *sh;
  302. struct hlist_node *hn;
  303. CHECK_DEVLOCK();
  304. pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
  305. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  306. if (sh->sector == sector && sh->generation == generation)
  307. return sh;
  308. pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
  309. return NULL;
  310. }
  311. static void unplug_slaves(mddev_t *mddev);
  312. static void raid5_unplug_device(struct request_queue *q);
  313. static struct stripe_head *
  314. get_active_stripe(raid5_conf_t *conf, sector_t sector,
  315. int previous, int noblock)
  316. {
  317. struct stripe_head *sh;
  318. pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
  319. spin_lock_irq(&conf->device_lock);
  320. do {
  321. wait_event_lock_irq(conf->wait_for_stripe,
  322. conf->quiesce == 0,
  323. conf->device_lock, /* nothing */);
  324. sh = __find_stripe(conf, sector, conf->generation - previous);
  325. if (!sh) {
  326. if (!conf->inactive_blocked)
  327. sh = get_free_stripe(conf);
  328. if (noblock && sh == NULL)
  329. break;
  330. if (!sh) {
  331. conf->inactive_blocked = 1;
  332. wait_event_lock_irq(conf->wait_for_stripe,
  333. !list_empty(&conf->inactive_list) &&
  334. (atomic_read(&conf->active_stripes)
  335. < (conf->max_nr_stripes *3/4)
  336. || !conf->inactive_blocked),
  337. conf->device_lock,
  338. raid5_unplug_device(conf->mddev->queue)
  339. );
  340. conf->inactive_blocked = 0;
  341. } else
  342. init_stripe(sh, sector, previous);
  343. } else {
  344. if (atomic_read(&sh->count)) {
  345. BUG_ON(!list_empty(&sh->lru)
  346. && !test_bit(STRIPE_EXPANDING, &sh->state));
  347. } else {
  348. if (!test_bit(STRIPE_HANDLE, &sh->state))
  349. atomic_inc(&conf->active_stripes);
  350. if (list_empty(&sh->lru) &&
  351. !test_bit(STRIPE_EXPANDING, &sh->state))
  352. BUG();
  353. list_del_init(&sh->lru);
  354. }
  355. }
  356. } while (sh == NULL);
  357. if (sh)
  358. atomic_inc(&sh->count);
  359. spin_unlock_irq(&conf->device_lock);
  360. return sh;
  361. }
  362. static void
  363. raid5_end_read_request(struct bio *bi, int error);
  364. static void
  365. raid5_end_write_request(struct bio *bi, int error);
  366. static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
  367. {
  368. raid5_conf_t *conf = sh->raid_conf;
  369. int i, disks = sh->disks;
  370. might_sleep();
  371. for (i = disks; i--; ) {
  372. int rw;
  373. struct bio *bi;
  374. mdk_rdev_t *rdev;
  375. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  376. rw = WRITE;
  377. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  378. rw = READ;
  379. else
  380. continue;
  381. bi = &sh->dev[i].req;
  382. bi->bi_rw = rw;
  383. if (rw == WRITE)
  384. bi->bi_end_io = raid5_end_write_request;
  385. else
  386. bi->bi_end_io = raid5_end_read_request;
  387. rcu_read_lock();
  388. rdev = rcu_dereference(conf->disks[i].rdev);
  389. if (rdev && test_bit(Faulty, &rdev->flags))
  390. rdev = NULL;
  391. if (rdev)
  392. atomic_inc(&rdev->nr_pending);
  393. rcu_read_unlock();
  394. if (rdev) {
  395. if (s->syncing || s->expanding || s->expanded)
  396. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  397. set_bit(STRIPE_IO_STARTED, &sh->state);
  398. bi->bi_bdev = rdev->bdev;
  399. pr_debug("%s: for %llu schedule op %ld on disc %d\n",
  400. __func__, (unsigned long long)sh->sector,
  401. bi->bi_rw, i);
  402. atomic_inc(&sh->count);
  403. bi->bi_sector = sh->sector + rdev->data_offset;
  404. bi->bi_flags = 1 << BIO_UPTODATE;
  405. bi->bi_vcnt = 1;
  406. bi->bi_max_vecs = 1;
  407. bi->bi_idx = 0;
  408. bi->bi_io_vec = &sh->dev[i].vec;
  409. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  410. bi->bi_io_vec[0].bv_offset = 0;
  411. bi->bi_size = STRIPE_SIZE;
  412. bi->bi_next = NULL;
  413. if (rw == WRITE &&
  414. test_bit(R5_ReWrite, &sh->dev[i].flags))
  415. atomic_add(STRIPE_SECTORS,
  416. &rdev->corrected_errors);
  417. generic_make_request(bi);
  418. } else {
  419. if (rw == WRITE)
  420. set_bit(STRIPE_DEGRADED, &sh->state);
  421. pr_debug("skip op %ld on disc %d for sector %llu\n",
  422. bi->bi_rw, i, (unsigned long long)sh->sector);
  423. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  424. set_bit(STRIPE_HANDLE, &sh->state);
  425. }
  426. }
  427. }
  428. static struct dma_async_tx_descriptor *
  429. async_copy_data(int frombio, struct bio *bio, struct page *page,
  430. sector_t sector, struct dma_async_tx_descriptor *tx)
  431. {
  432. struct bio_vec *bvl;
  433. struct page *bio_page;
  434. int i;
  435. int page_offset;
  436. if (bio->bi_sector >= sector)
  437. page_offset = (signed)(bio->bi_sector - sector) * 512;
  438. else
  439. page_offset = (signed)(sector - bio->bi_sector) * -512;
  440. bio_for_each_segment(bvl, bio, i) {
  441. int len = bio_iovec_idx(bio, i)->bv_len;
  442. int clen;
  443. int b_offset = 0;
  444. if (page_offset < 0) {
  445. b_offset = -page_offset;
  446. page_offset += b_offset;
  447. len -= b_offset;
  448. }
  449. if (len > 0 && page_offset + len > STRIPE_SIZE)
  450. clen = STRIPE_SIZE - page_offset;
  451. else
  452. clen = len;
  453. if (clen > 0) {
  454. b_offset += bio_iovec_idx(bio, i)->bv_offset;
  455. bio_page = bio_iovec_idx(bio, i)->bv_page;
  456. if (frombio)
  457. tx = async_memcpy(page, bio_page, page_offset,
  458. b_offset, clen, 0,
  459. tx, NULL, NULL);
  460. else
  461. tx = async_memcpy(bio_page, page, b_offset,
  462. page_offset, clen, 0,
  463. tx, NULL, NULL);
  464. }
  465. if (clen < len) /* hit end of page */
  466. break;
  467. page_offset += len;
  468. }
  469. return tx;
  470. }
  471. static void ops_complete_biofill(void *stripe_head_ref)
  472. {
  473. struct stripe_head *sh = stripe_head_ref;
  474. struct bio *return_bi = NULL;
  475. raid5_conf_t *conf = sh->raid_conf;
  476. int i;
  477. pr_debug("%s: stripe %llu\n", __func__,
  478. (unsigned long long)sh->sector);
  479. /* clear completed biofills */
  480. spin_lock_irq(&conf->device_lock);
  481. for (i = sh->disks; i--; ) {
  482. struct r5dev *dev = &sh->dev[i];
  483. /* acknowledge completion of a biofill operation */
  484. /* and check if we need to reply to a read request,
  485. * new R5_Wantfill requests are held off until
  486. * !STRIPE_BIOFILL_RUN
  487. */
  488. if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
  489. struct bio *rbi, *rbi2;
  490. BUG_ON(!dev->read);
  491. rbi = dev->read;
  492. dev->read = NULL;
  493. while (rbi && rbi->bi_sector <
  494. dev->sector + STRIPE_SECTORS) {
  495. rbi2 = r5_next_bio(rbi, dev->sector);
  496. if (!raid5_dec_bi_phys_segments(rbi)) {
  497. rbi->bi_next = return_bi;
  498. return_bi = rbi;
  499. }
  500. rbi = rbi2;
  501. }
  502. }
  503. }
  504. spin_unlock_irq(&conf->device_lock);
  505. clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
  506. return_io(return_bi);
  507. set_bit(STRIPE_HANDLE, &sh->state);
  508. release_stripe(sh);
  509. }
  510. static void ops_run_biofill(struct stripe_head *sh)
  511. {
  512. struct dma_async_tx_descriptor *tx = NULL;
  513. raid5_conf_t *conf = sh->raid_conf;
  514. int i;
  515. pr_debug("%s: stripe %llu\n", __func__,
  516. (unsigned long long)sh->sector);
  517. for (i = sh->disks; i--; ) {
  518. struct r5dev *dev = &sh->dev[i];
  519. if (test_bit(R5_Wantfill, &dev->flags)) {
  520. struct bio *rbi;
  521. spin_lock_irq(&conf->device_lock);
  522. dev->read = rbi = dev->toread;
  523. dev->toread = NULL;
  524. spin_unlock_irq(&conf->device_lock);
  525. while (rbi && rbi->bi_sector <
  526. dev->sector + STRIPE_SECTORS) {
  527. tx = async_copy_data(0, rbi, dev->page,
  528. dev->sector, tx);
  529. rbi = r5_next_bio(rbi, dev->sector);
  530. }
  531. }
  532. }
  533. atomic_inc(&sh->count);
  534. async_trigger_callback(ASYNC_TX_ACK, tx, ops_complete_biofill, sh);
  535. }
  536. static void ops_complete_compute5(void *stripe_head_ref)
  537. {
  538. struct stripe_head *sh = stripe_head_ref;
  539. int target = sh->ops.target;
  540. struct r5dev *tgt = &sh->dev[target];
  541. pr_debug("%s: stripe %llu\n", __func__,
  542. (unsigned long long)sh->sector);
  543. set_bit(R5_UPTODATE, &tgt->flags);
  544. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  545. clear_bit(R5_Wantcompute, &tgt->flags);
  546. clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
  547. if (sh->check_state == check_state_compute_run)
  548. sh->check_state = check_state_compute_result;
  549. set_bit(STRIPE_HANDLE, &sh->state);
  550. release_stripe(sh);
  551. }
  552. static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
  553. {
  554. /* kernel stack size limits the total number of disks */
  555. int disks = sh->disks;
  556. struct page *xor_srcs[disks];
  557. int target = sh->ops.target;
  558. struct r5dev *tgt = &sh->dev[target];
  559. struct page *xor_dest = tgt->page;
  560. int count = 0;
  561. struct dma_async_tx_descriptor *tx;
  562. int i;
  563. pr_debug("%s: stripe %llu block: %d\n",
  564. __func__, (unsigned long long)sh->sector, target);
  565. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  566. for (i = disks; i--; )
  567. if (i != target)
  568. xor_srcs[count++] = sh->dev[i].page;
  569. atomic_inc(&sh->count);
  570. if (unlikely(count == 1))
  571. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
  572. 0, NULL, ops_complete_compute5, sh);
  573. else
  574. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  575. ASYNC_TX_XOR_ZERO_DST, NULL,
  576. ops_complete_compute5, sh);
  577. return tx;
  578. }
  579. static void ops_complete_prexor(void *stripe_head_ref)
  580. {
  581. struct stripe_head *sh = stripe_head_ref;
  582. pr_debug("%s: stripe %llu\n", __func__,
  583. (unsigned long long)sh->sector);
  584. }
  585. static struct dma_async_tx_descriptor *
  586. ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  587. {
  588. /* kernel stack size limits the total number of disks */
  589. int disks = sh->disks;
  590. struct page *xor_srcs[disks];
  591. int count = 0, pd_idx = sh->pd_idx, i;
  592. /* existing parity data subtracted */
  593. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  594. pr_debug("%s: stripe %llu\n", __func__,
  595. (unsigned long long)sh->sector);
  596. for (i = disks; i--; ) {
  597. struct r5dev *dev = &sh->dev[i];
  598. /* Only process blocks that are known to be uptodate */
  599. if (test_bit(R5_Wantdrain, &dev->flags))
  600. xor_srcs[count++] = dev->page;
  601. }
  602. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  603. ASYNC_TX_XOR_DROP_DST, tx,
  604. ops_complete_prexor, sh);
  605. return tx;
  606. }
  607. static struct dma_async_tx_descriptor *
  608. ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  609. {
  610. int disks = sh->disks;
  611. int i;
  612. pr_debug("%s: stripe %llu\n", __func__,
  613. (unsigned long long)sh->sector);
  614. for (i = disks; i--; ) {
  615. struct r5dev *dev = &sh->dev[i];
  616. struct bio *chosen;
  617. if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
  618. struct bio *wbi;
  619. spin_lock(&sh->lock);
  620. chosen = dev->towrite;
  621. dev->towrite = NULL;
  622. BUG_ON(dev->written);
  623. wbi = dev->written = chosen;
  624. spin_unlock(&sh->lock);
  625. while (wbi && wbi->bi_sector <
  626. dev->sector + STRIPE_SECTORS) {
  627. tx = async_copy_data(1, wbi, dev->page,
  628. dev->sector, tx);
  629. wbi = r5_next_bio(wbi, dev->sector);
  630. }
  631. }
  632. }
  633. return tx;
  634. }
  635. static void ops_complete_postxor(void *stripe_head_ref)
  636. {
  637. struct stripe_head *sh = stripe_head_ref;
  638. int disks = sh->disks, i, pd_idx = sh->pd_idx;
  639. pr_debug("%s: stripe %llu\n", __func__,
  640. (unsigned long long)sh->sector);
  641. for (i = disks; i--; ) {
  642. struct r5dev *dev = &sh->dev[i];
  643. if (dev->written || i == pd_idx)
  644. set_bit(R5_UPTODATE, &dev->flags);
  645. }
  646. if (sh->reconstruct_state == reconstruct_state_drain_run)
  647. sh->reconstruct_state = reconstruct_state_drain_result;
  648. else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
  649. sh->reconstruct_state = reconstruct_state_prexor_drain_result;
  650. else {
  651. BUG_ON(sh->reconstruct_state != reconstruct_state_run);
  652. sh->reconstruct_state = reconstruct_state_result;
  653. }
  654. set_bit(STRIPE_HANDLE, &sh->state);
  655. release_stripe(sh);
  656. }
  657. static void
  658. ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  659. {
  660. /* kernel stack size limits the total number of disks */
  661. int disks = sh->disks;
  662. struct page *xor_srcs[disks];
  663. int count = 0, pd_idx = sh->pd_idx, i;
  664. struct page *xor_dest;
  665. int prexor = 0;
  666. unsigned long flags;
  667. pr_debug("%s: stripe %llu\n", __func__,
  668. (unsigned long long)sh->sector);
  669. /* check if prexor is active which means only process blocks
  670. * that are part of a read-modify-write (written)
  671. */
  672. if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  673. prexor = 1;
  674. xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  675. for (i = disks; i--; ) {
  676. struct r5dev *dev = &sh->dev[i];
  677. if (dev->written)
  678. xor_srcs[count++] = dev->page;
  679. }
  680. } else {
  681. xor_dest = sh->dev[pd_idx].page;
  682. for (i = disks; i--; ) {
  683. struct r5dev *dev = &sh->dev[i];
  684. if (i != pd_idx)
  685. xor_srcs[count++] = dev->page;
  686. }
  687. }
  688. /* 1/ if we prexor'd then the dest is reused as a source
  689. * 2/ if we did not prexor then we are redoing the parity
  690. * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
  691. * for the synchronous xor case
  692. */
  693. flags = ASYNC_TX_ACK |
  694. (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
  695. atomic_inc(&sh->count);
  696. if (unlikely(count == 1)) {
  697. flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST);
  698. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
  699. flags, tx, ops_complete_postxor, sh);
  700. } else
  701. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  702. flags, tx, ops_complete_postxor, sh);
  703. }
  704. static void ops_complete_check(void *stripe_head_ref)
  705. {
  706. struct stripe_head *sh = stripe_head_ref;
  707. pr_debug("%s: stripe %llu\n", __func__,
  708. (unsigned long long)sh->sector);
  709. sh->check_state = check_state_check_result;
  710. set_bit(STRIPE_HANDLE, &sh->state);
  711. release_stripe(sh);
  712. }
  713. static void ops_run_check(struct stripe_head *sh)
  714. {
  715. /* kernel stack size limits the total number of disks */
  716. int disks = sh->disks;
  717. struct page *xor_srcs[disks];
  718. struct dma_async_tx_descriptor *tx;
  719. int count = 0, pd_idx = sh->pd_idx, i;
  720. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  721. pr_debug("%s: stripe %llu\n", __func__,
  722. (unsigned long long)sh->sector);
  723. for (i = disks; i--; ) {
  724. struct r5dev *dev = &sh->dev[i];
  725. if (i != pd_idx)
  726. xor_srcs[count++] = dev->page;
  727. }
  728. tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  729. &sh->ops.zero_sum_result, 0, NULL, NULL, NULL);
  730. atomic_inc(&sh->count);
  731. tx = async_trigger_callback(ASYNC_TX_ACK, tx,
  732. ops_complete_check, sh);
  733. }
  734. static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
  735. {
  736. int overlap_clear = 0, i, disks = sh->disks;
  737. struct dma_async_tx_descriptor *tx = NULL;
  738. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  739. ops_run_biofill(sh);
  740. overlap_clear++;
  741. }
  742. if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
  743. tx = ops_run_compute5(sh);
  744. /* terminate the chain if postxor is not set to be run */
  745. if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
  746. async_tx_ack(tx);
  747. }
  748. if (test_bit(STRIPE_OP_PREXOR, &ops_request))
  749. tx = ops_run_prexor(sh, tx);
  750. if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
  751. tx = ops_run_biodrain(sh, tx);
  752. overlap_clear++;
  753. }
  754. if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
  755. ops_run_postxor(sh, tx);
  756. if (test_bit(STRIPE_OP_CHECK, &ops_request))
  757. ops_run_check(sh);
  758. if (overlap_clear)
  759. for (i = disks; i--; ) {
  760. struct r5dev *dev = &sh->dev[i];
  761. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  762. wake_up(&sh->raid_conf->wait_for_overlap);
  763. }
  764. }
  765. static int grow_one_stripe(raid5_conf_t *conf)
  766. {
  767. struct stripe_head *sh;
  768. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  769. if (!sh)
  770. return 0;
  771. memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
  772. sh->raid_conf = conf;
  773. spin_lock_init(&sh->lock);
  774. if (grow_buffers(sh, conf->raid_disks)) {
  775. shrink_buffers(sh, conf->raid_disks);
  776. kmem_cache_free(conf->slab_cache, sh);
  777. return 0;
  778. }
  779. sh->disks = conf->raid_disks;
  780. /* we just created an active stripe so... */
  781. atomic_set(&sh->count, 1);
  782. atomic_inc(&conf->active_stripes);
  783. INIT_LIST_HEAD(&sh->lru);
  784. release_stripe(sh);
  785. return 1;
  786. }
  787. static int grow_stripes(raid5_conf_t *conf, int num)
  788. {
  789. struct kmem_cache *sc;
  790. int devs = conf->raid_disks;
  791. sprintf(conf->cache_name[0],
  792. "raid%d-%s", conf->level, mdname(conf->mddev));
  793. sprintf(conf->cache_name[1],
  794. "raid%d-%s-alt", conf->level, mdname(conf->mddev));
  795. conf->active_name = 0;
  796. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  797. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  798. 0, 0, NULL);
  799. if (!sc)
  800. return 1;
  801. conf->slab_cache = sc;
  802. conf->pool_size = devs;
  803. while (num--)
  804. if (!grow_one_stripe(conf))
  805. return 1;
  806. return 0;
  807. }
  808. static int resize_stripes(raid5_conf_t *conf, int newsize)
  809. {
  810. /* Make all the stripes able to hold 'newsize' devices.
  811. * New slots in each stripe get 'page' set to a new page.
  812. *
  813. * This happens in stages:
  814. * 1/ create a new kmem_cache and allocate the required number of
  815. * stripe_heads.
  816. * 2/ gather all the old stripe_heads and tranfer the pages across
  817. * to the new stripe_heads. This will have the side effect of
  818. * freezing the array as once all stripe_heads have been collected,
  819. * no IO will be possible. Old stripe heads are freed once their
  820. * pages have been transferred over, and the old kmem_cache is
  821. * freed when all stripes are done.
  822. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  823. * we simple return a failre status - no need to clean anything up.
  824. * 4/ allocate new pages for the new slots in the new stripe_heads.
  825. * If this fails, we don't bother trying the shrink the
  826. * stripe_heads down again, we just leave them as they are.
  827. * As each stripe_head is processed the new one is released into
  828. * active service.
  829. *
  830. * Once step2 is started, we cannot afford to wait for a write,
  831. * so we use GFP_NOIO allocations.
  832. */
  833. struct stripe_head *osh, *nsh;
  834. LIST_HEAD(newstripes);
  835. struct disk_info *ndisks;
  836. int err;
  837. struct kmem_cache *sc;
  838. int i;
  839. if (newsize <= conf->pool_size)
  840. return 0; /* never bother to shrink */
  841. err = md_allow_write(conf->mddev);
  842. if (err)
  843. return err;
  844. /* Step 1 */
  845. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  846. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  847. 0, 0, NULL);
  848. if (!sc)
  849. return -ENOMEM;
  850. for (i = conf->max_nr_stripes; i; i--) {
  851. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  852. if (!nsh)
  853. break;
  854. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  855. nsh->raid_conf = conf;
  856. spin_lock_init(&nsh->lock);
  857. list_add(&nsh->lru, &newstripes);
  858. }
  859. if (i) {
  860. /* didn't get enough, give up */
  861. while (!list_empty(&newstripes)) {
  862. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  863. list_del(&nsh->lru);
  864. kmem_cache_free(sc, nsh);
  865. }
  866. kmem_cache_destroy(sc);
  867. return -ENOMEM;
  868. }
  869. /* Step 2 - Must use GFP_NOIO now.
  870. * OK, we have enough stripes, start collecting inactive
  871. * stripes and copying them over
  872. */
  873. list_for_each_entry(nsh, &newstripes, lru) {
  874. spin_lock_irq(&conf->device_lock);
  875. wait_event_lock_irq(conf->wait_for_stripe,
  876. !list_empty(&conf->inactive_list),
  877. conf->device_lock,
  878. unplug_slaves(conf->mddev)
  879. );
  880. osh = get_free_stripe(conf);
  881. spin_unlock_irq(&conf->device_lock);
  882. atomic_set(&nsh->count, 1);
  883. for(i=0; i<conf->pool_size; i++)
  884. nsh->dev[i].page = osh->dev[i].page;
  885. for( ; i<newsize; i++)
  886. nsh->dev[i].page = NULL;
  887. kmem_cache_free(conf->slab_cache, osh);
  888. }
  889. kmem_cache_destroy(conf->slab_cache);
  890. /* Step 3.
  891. * At this point, we are holding all the stripes so the array
  892. * is completely stalled, so now is a good time to resize
  893. * conf->disks.
  894. */
  895. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  896. if (ndisks) {
  897. for (i=0; i<conf->raid_disks; i++)
  898. ndisks[i] = conf->disks[i];
  899. kfree(conf->disks);
  900. conf->disks = ndisks;
  901. } else
  902. err = -ENOMEM;
  903. /* Step 4, return new stripes to service */
  904. while(!list_empty(&newstripes)) {
  905. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  906. list_del_init(&nsh->lru);
  907. for (i=conf->raid_disks; i < newsize; i++)
  908. if (nsh->dev[i].page == NULL) {
  909. struct page *p = alloc_page(GFP_NOIO);
  910. nsh->dev[i].page = p;
  911. if (!p)
  912. err = -ENOMEM;
  913. }
  914. release_stripe(nsh);
  915. }
  916. /* critical section pass, GFP_NOIO no longer needed */
  917. conf->slab_cache = sc;
  918. conf->active_name = 1-conf->active_name;
  919. conf->pool_size = newsize;
  920. return err;
  921. }
  922. static int drop_one_stripe(raid5_conf_t *conf)
  923. {
  924. struct stripe_head *sh;
  925. spin_lock_irq(&conf->device_lock);
  926. sh = get_free_stripe(conf);
  927. spin_unlock_irq(&conf->device_lock);
  928. if (!sh)
  929. return 0;
  930. BUG_ON(atomic_read(&sh->count));
  931. shrink_buffers(sh, conf->pool_size);
  932. kmem_cache_free(conf->slab_cache, sh);
  933. atomic_dec(&conf->active_stripes);
  934. return 1;
  935. }
  936. static void shrink_stripes(raid5_conf_t *conf)
  937. {
  938. while (drop_one_stripe(conf))
  939. ;
  940. if (conf->slab_cache)
  941. kmem_cache_destroy(conf->slab_cache);
  942. conf->slab_cache = NULL;
  943. }
  944. static void raid5_end_read_request(struct bio * bi, int error)
  945. {
  946. struct stripe_head *sh = bi->bi_private;
  947. raid5_conf_t *conf = sh->raid_conf;
  948. int disks = sh->disks, i;
  949. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  950. char b[BDEVNAME_SIZE];
  951. mdk_rdev_t *rdev;
  952. for (i=0 ; i<disks; i++)
  953. if (bi == &sh->dev[i].req)
  954. break;
  955. pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  956. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  957. uptodate);
  958. if (i == disks) {
  959. BUG();
  960. return;
  961. }
  962. if (uptodate) {
  963. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  964. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  965. rdev = conf->disks[i].rdev;
  966. printk_rl(KERN_INFO "raid5:%s: read error corrected"
  967. " (%lu sectors at %llu on %s)\n",
  968. mdname(conf->mddev), STRIPE_SECTORS,
  969. (unsigned long long)(sh->sector
  970. + rdev->data_offset),
  971. bdevname(rdev->bdev, b));
  972. clear_bit(R5_ReadError, &sh->dev[i].flags);
  973. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  974. }
  975. if (atomic_read(&conf->disks[i].rdev->read_errors))
  976. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  977. } else {
  978. const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
  979. int retry = 0;
  980. rdev = conf->disks[i].rdev;
  981. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  982. atomic_inc(&rdev->read_errors);
  983. if (conf->mddev->degraded)
  984. printk_rl(KERN_WARNING
  985. "raid5:%s: read error not correctable "
  986. "(sector %llu on %s).\n",
  987. mdname(conf->mddev),
  988. (unsigned long long)(sh->sector
  989. + rdev->data_offset),
  990. bdn);
  991. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  992. /* Oh, no!!! */
  993. printk_rl(KERN_WARNING
  994. "raid5:%s: read error NOT corrected!! "
  995. "(sector %llu on %s).\n",
  996. mdname(conf->mddev),
  997. (unsigned long long)(sh->sector
  998. + rdev->data_offset),
  999. bdn);
  1000. else if (atomic_read(&rdev->read_errors)
  1001. > conf->max_nr_stripes)
  1002. printk(KERN_WARNING
  1003. "raid5:%s: Too many read errors, failing device %s.\n",
  1004. mdname(conf->mddev), bdn);
  1005. else
  1006. retry = 1;
  1007. if (retry)
  1008. set_bit(R5_ReadError, &sh->dev[i].flags);
  1009. else {
  1010. clear_bit(R5_ReadError, &sh->dev[i].flags);
  1011. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  1012. md_error(conf->mddev, rdev);
  1013. }
  1014. }
  1015. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1016. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1017. set_bit(STRIPE_HANDLE, &sh->state);
  1018. release_stripe(sh);
  1019. }
  1020. static void raid5_end_write_request(struct bio *bi, int error)
  1021. {
  1022. struct stripe_head *sh = bi->bi_private;
  1023. raid5_conf_t *conf = sh->raid_conf;
  1024. int disks = sh->disks, i;
  1025. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1026. for (i=0 ; i<disks; i++)
  1027. if (bi == &sh->dev[i].req)
  1028. break;
  1029. pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  1030. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1031. uptodate);
  1032. if (i == disks) {
  1033. BUG();
  1034. return;
  1035. }
  1036. if (!uptodate)
  1037. md_error(conf->mddev, conf->disks[i].rdev);
  1038. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1039. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1040. set_bit(STRIPE_HANDLE, &sh->state);
  1041. release_stripe(sh);
  1042. }
  1043. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
  1044. static void raid5_build_block(struct stripe_head *sh, int i, int previous)
  1045. {
  1046. struct r5dev *dev = &sh->dev[i];
  1047. bio_init(&dev->req);
  1048. dev->req.bi_io_vec = &dev->vec;
  1049. dev->req.bi_vcnt++;
  1050. dev->req.bi_max_vecs++;
  1051. dev->vec.bv_page = dev->page;
  1052. dev->vec.bv_len = STRIPE_SIZE;
  1053. dev->vec.bv_offset = 0;
  1054. dev->req.bi_sector = sh->sector;
  1055. dev->req.bi_private = sh;
  1056. dev->flags = 0;
  1057. dev->sector = compute_blocknr(sh, i, previous);
  1058. }
  1059. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  1060. {
  1061. char b[BDEVNAME_SIZE];
  1062. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1063. pr_debug("raid5: error called\n");
  1064. if (!test_bit(Faulty, &rdev->flags)) {
  1065. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1066. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1067. unsigned long flags;
  1068. spin_lock_irqsave(&conf->device_lock, flags);
  1069. mddev->degraded++;
  1070. spin_unlock_irqrestore(&conf->device_lock, flags);
  1071. /*
  1072. * if recovery was running, make sure it aborts.
  1073. */
  1074. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1075. }
  1076. set_bit(Faulty, &rdev->flags);
  1077. printk(KERN_ALERT
  1078. "raid5: Disk failure on %s, disabling device.\n"
  1079. "raid5: Operation continuing on %d devices.\n",
  1080. bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
  1081. }
  1082. }
  1083. /*
  1084. * Input: a 'big' sector number,
  1085. * Output: index of the data and parity disk, and the sector # in them.
  1086. */
  1087. static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
  1088. int previous, int *dd_idx,
  1089. struct stripe_head *sh)
  1090. {
  1091. long stripe;
  1092. unsigned long chunk_number;
  1093. unsigned int chunk_offset;
  1094. int pd_idx, qd_idx;
  1095. int ddf_layout = 0;
  1096. sector_t new_sector;
  1097. int algorithm = previous ? conf->prev_algo
  1098. : conf->algorithm;
  1099. int sectors_per_chunk = previous ? (conf->prev_chunk >> 9)
  1100. : (conf->chunk_size >> 9);
  1101. int raid_disks = previous ? conf->previous_raid_disks
  1102. : conf->raid_disks;
  1103. int data_disks = raid_disks - conf->max_degraded;
  1104. /* First compute the information on this sector */
  1105. /*
  1106. * Compute the chunk number and the sector offset inside the chunk
  1107. */
  1108. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  1109. chunk_number = r_sector;
  1110. BUG_ON(r_sector != chunk_number);
  1111. /*
  1112. * Compute the stripe number
  1113. */
  1114. stripe = chunk_number / data_disks;
  1115. /*
  1116. * Compute the data disk and parity disk indexes inside the stripe
  1117. */
  1118. *dd_idx = chunk_number % data_disks;
  1119. /*
  1120. * Select the parity disk based on the user selected algorithm.
  1121. */
  1122. pd_idx = qd_idx = ~0;
  1123. switch(conf->level) {
  1124. case 4:
  1125. pd_idx = data_disks;
  1126. break;
  1127. case 5:
  1128. switch (algorithm) {
  1129. case ALGORITHM_LEFT_ASYMMETRIC:
  1130. pd_idx = data_disks - stripe % raid_disks;
  1131. if (*dd_idx >= pd_idx)
  1132. (*dd_idx)++;
  1133. break;
  1134. case ALGORITHM_RIGHT_ASYMMETRIC:
  1135. pd_idx = stripe % raid_disks;
  1136. if (*dd_idx >= pd_idx)
  1137. (*dd_idx)++;
  1138. break;
  1139. case ALGORITHM_LEFT_SYMMETRIC:
  1140. pd_idx = data_disks - stripe % raid_disks;
  1141. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1142. break;
  1143. case ALGORITHM_RIGHT_SYMMETRIC:
  1144. pd_idx = stripe % raid_disks;
  1145. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1146. break;
  1147. case ALGORITHM_PARITY_0:
  1148. pd_idx = 0;
  1149. (*dd_idx)++;
  1150. break;
  1151. case ALGORITHM_PARITY_N:
  1152. pd_idx = data_disks;
  1153. break;
  1154. default:
  1155. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  1156. algorithm);
  1157. BUG();
  1158. }
  1159. break;
  1160. case 6:
  1161. switch (algorithm) {
  1162. case ALGORITHM_LEFT_ASYMMETRIC:
  1163. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1164. qd_idx = pd_idx + 1;
  1165. if (pd_idx == raid_disks-1) {
  1166. (*dd_idx)++; /* Q D D D P */
  1167. qd_idx = 0;
  1168. } else if (*dd_idx >= pd_idx)
  1169. (*dd_idx) += 2; /* D D P Q D */
  1170. break;
  1171. case ALGORITHM_RIGHT_ASYMMETRIC:
  1172. pd_idx = stripe % raid_disks;
  1173. qd_idx = pd_idx + 1;
  1174. if (pd_idx == raid_disks-1) {
  1175. (*dd_idx)++; /* Q D D D P */
  1176. qd_idx = 0;
  1177. } else if (*dd_idx >= pd_idx)
  1178. (*dd_idx) += 2; /* D D P Q D */
  1179. break;
  1180. case ALGORITHM_LEFT_SYMMETRIC:
  1181. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1182. qd_idx = (pd_idx + 1) % raid_disks;
  1183. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1184. break;
  1185. case ALGORITHM_RIGHT_SYMMETRIC:
  1186. pd_idx = stripe % raid_disks;
  1187. qd_idx = (pd_idx + 1) % raid_disks;
  1188. *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
  1189. break;
  1190. case ALGORITHM_PARITY_0:
  1191. pd_idx = 0;
  1192. qd_idx = 1;
  1193. (*dd_idx) += 2;
  1194. break;
  1195. case ALGORITHM_PARITY_N:
  1196. pd_idx = data_disks;
  1197. qd_idx = data_disks + 1;
  1198. break;
  1199. case ALGORITHM_ROTATING_ZERO_RESTART:
  1200. /* Exactly the same as RIGHT_ASYMMETRIC, but or
  1201. * of blocks for computing Q is different.
  1202. */
  1203. pd_idx = stripe % raid_disks;
  1204. qd_idx = pd_idx + 1;
  1205. if (pd_idx == raid_disks-1) {
  1206. (*dd_idx)++; /* Q D D D P */
  1207. qd_idx = 0;
  1208. } else if (*dd_idx >= pd_idx)
  1209. (*dd_idx) += 2; /* D D P Q D */
  1210. ddf_layout = 1;
  1211. break;
  1212. case ALGORITHM_ROTATING_N_RESTART:
  1213. /* Same a left_asymmetric, by first stripe is
  1214. * D D D P Q rather than
  1215. * Q D D D P
  1216. */
  1217. pd_idx = raid_disks - 1 - ((stripe + 1) % raid_disks);
  1218. qd_idx = pd_idx + 1;
  1219. if (pd_idx == raid_disks-1) {
  1220. (*dd_idx)++; /* Q D D D P */
  1221. qd_idx = 0;
  1222. } else if (*dd_idx >= pd_idx)
  1223. (*dd_idx) += 2; /* D D P Q D */
  1224. ddf_layout = 1;
  1225. break;
  1226. case ALGORITHM_ROTATING_N_CONTINUE:
  1227. /* Same as left_symmetric but Q is before P */
  1228. pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1229. qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
  1230. *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
  1231. ddf_layout = 1;
  1232. break;
  1233. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1234. /* RAID5 left_asymmetric, with Q on last device */
  1235. pd_idx = data_disks - stripe % (raid_disks-1);
  1236. if (*dd_idx >= pd_idx)
  1237. (*dd_idx)++;
  1238. qd_idx = raid_disks - 1;
  1239. break;
  1240. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1241. pd_idx = stripe % (raid_disks-1);
  1242. if (*dd_idx >= pd_idx)
  1243. (*dd_idx)++;
  1244. qd_idx = raid_disks - 1;
  1245. break;
  1246. case ALGORITHM_LEFT_SYMMETRIC_6:
  1247. pd_idx = data_disks - stripe % (raid_disks-1);
  1248. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1249. qd_idx = raid_disks - 1;
  1250. break;
  1251. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1252. pd_idx = stripe % (raid_disks-1);
  1253. *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
  1254. qd_idx = raid_disks - 1;
  1255. break;
  1256. case ALGORITHM_PARITY_0_6:
  1257. pd_idx = 0;
  1258. (*dd_idx)++;
  1259. qd_idx = raid_disks - 1;
  1260. break;
  1261. default:
  1262. printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
  1263. algorithm);
  1264. BUG();
  1265. }
  1266. break;
  1267. }
  1268. if (sh) {
  1269. sh->pd_idx = pd_idx;
  1270. sh->qd_idx = qd_idx;
  1271. sh->ddf_layout = ddf_layout;
  1272. }
  1273. /*
  1274. * Finally, compute the new sector number
  1275. */
  1276. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  1277. return new_sector;
  1278. }
  1279. static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
  1280. {
  1281. raid5_conf_t *conf = sh->raid_conf;
  1282. int raid_disks = sh->disks;
  1283. int data_disks = raid_disks - conf->max_degraded;
  1284. sector_t new_sector = sh->sector, check;
  1285. int sectors_per_chunk = previous ? (conf->prev_chunk >> 9)
  1286. : (conf->chunk_size >> 9);
  1287. int algorithm = previous ? conf->prev_algo
  1288. : conf->algorithm;
  1289. sector_t stripe;
  1290. int chunk_offset;
  1291. int chunk_number, dummy1, dd_idx = i;
  1292. sector_t r_sector;
  1293. struct stripe_head sh2;
  1294. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  1295. stripe = new_sector;
  1296. BUG_ON(new_sector != stripe);
  1297. if (i == sh->pd_idx)
  1298. return 0;
  1299. switch(conf->level) {
  1300. case 4: break;
  1301. case 5:
  1302. switch (algorithm) {
  1303. case ALGORITHM_LEFT_ASYMMETRIC:
  1304. case ALGORITHM_RIGHT_ASYMMETRIC:
  1305. if (i > sh->pd_idx)
  1306. i--;
  1307. break;
  1308. case ALGORITHM_LEFT_SYMMETRIC:
  1309. case ALGORITHM_RIGHT_SYMMETRIC:
  1310. if (i < sh->pd_idx)
  1311. i += raid_disks;
  1312. i -= (sh->pd_idx + 1);
  1313. break;
  1314. case ALGORITHM_PARITY_0:
  1315. i -= 1;
  1316. break;
  1317. case ALGORITHM_PARITY_N:
  1318. break;
  1319. default:
  1320. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  1321. algorithm);
  1322. BUG();
  1323. }
  1324. break;
  1325. case 6:
  1326. if (i == sh->qd_idx)
  1327. return 0; /* It is the Q disk */
  1328. switch (algorithm) {
  1329. case ALGORITHM_LEFT_ASYMMETRIC:
  1330. case ALGORITHM_RIGHT_ASYMMETRIC:
  1331. case ALGORITHM_ROTATING_ZERO_RESTART:
  1332. case ALGORITHM_ROTATING_N_RESTART:
  1333. if (sh->pd_idx == raid_disks-1)
  1334. i--; /* Q D D D P */
  1335. else if (i > sh->pd_idx)
  1336. i -= 2; /* D D P Q D */
  1337. break;
  1338. case ALGORITHM_LEFT_SYMMETRIC:
  1339. case ALGORITHM_RIGHT_SYMMETRIC:
  1340. if (sh->pd_idx == raid_disks-1)
  1341. i--; /* Q D D D P */
  1342. else {
  1343. /* D D P Q D */
  1344. if (i < sh->pd_idx)
  1345. i += raid_disks;
  1346. i -= (sh->pd_idx + 2);
  1347. }
  1348. break;
  1349. case ALGORITHM_PARITY_0:
  1350. i -= 2;
  1351. break;
  1352. case ALGORITHM_PARITY_N:
  1353. break;
  1354. case ALGORITHM_ROTATING_N_CONTINUE:
  1355. if (sh->pd_idx == 0)
  1356. i--; /* P D D D Q */
  1357. else if (i > sh->pd_idx)
  1358. i -= 2; /* D D Q P D */
  1359. break;
  1360. case ALGORITHM_LEFT_ASYMMETRIC_6:
  1361. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  1362. if (i > sh->pd_idx)
  1363. i--;
  1364. break;
  1365. case ALGORITHM_LEFT_SYMMETRIC_6:
  1366. case ALGORITHM_RIGHT_SYMMETRIC_6:
  1367. if (i < sh->pd_idx)
  1368. i += data_disks + 1;
  1369. i -= (sh->pd_idx + 1);
  1370. break;
  1371. case ALGORITHM_PARITY_0_6:
  1372. i -= 1;
  1373. break;
  1374. default:
  1375. printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
  1376. algorithm);
  1377. BUG();
  1378. }
  1379. break;
  1380. }
  1381. chunk_number = stripe * data_disks + i;
  1382. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  1383. check = raid5_compute_sector(conf, r_sector,
  1384. previous, &dummy1, &sh2);
  1385. if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
  1386. || sh2.qd_idx != sh->qd_idx) {
  1387. printk(KERN_ERR "compute_blocknr: map not correct\n");
  1388. return 0;
  1389. }
  1390. return r_sector;
  1391. }
  1392. /*
  1393. * Copy data between a page in the stripe cache, and one or more bion
  1394. * The page could align with the middle of the bio, or there could be
  1395. * several bion, each with several bio_vecs, which cover part of the page
  1396. * Multiple bion are linked together on bi_next. There may be extras
  1397. * at the end of this list. We ignore them.
  1398. */
  1399. static void copy_data(int frombio, struct bio *bio,
  1400. struct page *page,
  1401. sector_t sector)
  1402. {
  1403. char *pa = page_address(page);
  1404. struct bio_vec *bvl;
  1405. int i;
  1406. int page_offset;
  1407. if (bio->bi_sector >= sector)
  1408. page_offset = (signed)(bio->bi_sector - sector) * 512;
  1409. else
  1410. page_offset = (signed)(sector - bio->bi_sector) * -512;
  1411. bio_for_each_segment(bvl, bio, i) {
  1412. int len = bio_iovec_idx(bio,i)->bv_len;
  1413. int clen;
  1414. int b_offset = 0;
  1415. if (page_offset < 0) {
  1416. b_offset = -page_offset;
  1417. page_offset += b_offset;
  1418. len -= b_offset;
  1419. }
  1420. if (len > 0 && page_offset + len > STRIPE_SIZE)
  1421. clen = STRIPE_SIZE - page_offset;
  1422. else clen = len;
  1423. if (clen > 0) {
  1424. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  1425. if (frombio)
  1426. memcpy(pa+page_offset, ba+b_offset, clen);
  1427. else
  1428. memcpy(ba+b_offset, pa+page_offset, clen);
  1429. __bio_kunmap_atomic(ba, KM_USER0);
  1430. }
  1431. if (clen < len) /* hit end of page */
  1432. break;
  1433. page_offset += len;
  1434. }
  1435. }
  1436. #define check_xor() do { \
  1437. if (count == MAX_XOR_BLOCKS) { \
  1438. xor_blocks(count, STRIPE_SIZE, dest, ptr);\
  1439. count = 0; \
  1440. } \
  1441. } while(0)
  1442. static void compute_parity6(struct stripe_head *sh, int method)
  1443. {
  1444. raid5_conf_t *conf = sh->raid_conf;
  1445. int i, pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
  1446. int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
  1447. struct bio *chosen;
  1448. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1449. void *ptrs[syndrome_disks+2];
  1450. pd_idx = sh->pd_idx;
  1451. qd_idx = sh->qd_idx;
  1452. d0_idx = raid6_d0(sh);
  1453. pr_debug("compute_parity, stripe %llu, method %d\n",
  1454. (unsigned long long)sh->sector, method);
  1455. switch(method) {
  1456. case READ_MODIFY_WRITE:
  1457. BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
  1458. case RECONSTRUCT_WRITE:
  1459. for (i= disks; i-- ;)
  1460. if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
  1461. chosen = sh->dev[i].towrite;
  1462. sh->dev[i].towrite = NULL;
  1463. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1464. wake_up(&conf->wait_for_overlap);
  1465. BUG_ON(sh->dev[i].written);
  1466. sh->dev[i].written = chosen;
  1467. }
  1468. break;
  1469. case CHECK_PARITY:
  1470. BUG(); /* Not implemented yet */
  1471. }
  1472. for (i = disks; i--;)
  1473. if (sh->dev[i].written) {
  1474. sector_t sector = sh->dev[i].sector;
  1475. struct bio *wbi = sh->dev[i].written;
  1476. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  1477. copy_data(1, wbi, sh->dev[i].page, sector);
  1478. wbi = r5_next_bio(wbi, sector);
  1479. }
  1480. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1481. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  1482. }
  1483. /* Note that unlike RAID-5, the ordering of the disks matters greatly.*/
  1484. for (i = 0; i < disks; i++)
  1485. ptrs[i] = (void *)raid6_empty_zero_page;
  1486. count = 0;
  1487. i = d0_idx;
  1488. do {
  1489. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  1490. ptrs[slot] = page_address(sh->dev[i].page);
  1491. if (slot < syndrome_disks &&
  1492. !test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  1493. printk(KERN_ERR "block %d/%d not uptodate "
  1494. "on parity calc\n", i, count);
  1495. BUG();
  1496. }
  1497. i = raid6_next_disk(i, disks);
  1498. } while (i != d0_idx);
  1499. BUG_ON(count != syndrome_disks);
  1500. raid6_call.gen_syndrome(syndrome_disks+2, STRIPE_SIZE, ptrs);
  1501. switch(method) {
  1502. case RECONSTRUCT_WRITE:
  1503. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1504. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1505. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1506. set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
  1507. break;
  1508. case UPDATE_PARITY:
  1509. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1510. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1511. break;
  1512. }
  1513. }
  1514. /* Compute one missing block */
  1515. static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
  1516. {
  1517. int i, count, disks = sh->disks;
  1518. void *ptr[MAX_XOR_BLOCKS], *dest, *p;
  1519. int qd_idx = sh->qd_idx;
  1520. pr_debug("compute_block_1, stripe %llu, idx %d\n",
  1521. (unsigned long long)sh->sector, dd_idx);
  1522. if ( dd_idx == qd_idx ) {
  1523. /* We're actually computing the Q drive */
  1524. compute_parity6(sh, UPDATE_PARITY);
  1525. } else {
  1526. dest = page_address(sh->dev[dd_idx].page);
  1527. if (!nozero) memset(dest, 0, STRIPE_SIZE);
  1528. count = 0;
  1529. for (i = disks ; i--; ) {
  1530. if (i == dd_idx || i == qd_idx)
  1531. continue;
  1532. p = page_address(sh->dev[i].page);
  1533. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1534. ptr[count++] = p;
  1535. else
  1536. printk("compute_block() %d, stripe %llu, %d"
  1537. " not present\n", dd_idx,
  1538. (unsigned long long)sh->sector, i);
  1539. check_xor();
  1540. }
  1541. if (count)
  1542. xor_blocks(count, STRIPE_SIZE, dest, ptr);
  1543. if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1544. else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1545. }
  1546. }
  1547. /* Compute two missing blocks */
  1548. static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
  1549. {
  1550. int i, count, disks = sh->disks;
  1551. int syndrome_disks = sh->ddf_layout ? disks : disks-2;
  1552. int d0_idx = raid6_d0(sh);
  1553. int faila = -1, failb = -1;
  1554. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1555. void *ptrs[syndrome_disks+2];
  1556. for (i = 0; i < disks ; i++)
  1557. ptrs[i] = (void *)raid6_empty_zero_page;
  1558. count = 0;
  1559. i = d0_idx;
  1560. do {
  1561. int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
  1562. ptrs[slot] = page_address(sh->dev[i].page);
  1563. if (i == dd_idx1)
  1564. faila = slot;
  1565. if (i == dd_idx2)
  1566. failb = slot;
  1567. i = raid6_next_disk(i, disks);
  1568. } while (i != d0_idx);
  1569. BUG_ON(count != syndrome_disks);
  1570. BUG_ON(faila == failb);
  1571. if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
  1572. pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
  1573. (unsigned long long)sh->sector, dd_idx1, dd_idx2,
  1574. faila, failb);
  1575. if (failb == syndrome_disks+1) {
  1576. /* Q disk is one of the missing disks */
  1577. if (faila == syndrome_disks) {
  1578. /* Missing P+Q, just recompute */
  1579. compute_parity6(sh, UPDATE_PARITY);
  1580. return;
  1581. } else {
  1582. /* We're missing D+Q; recompute D from P */
  1583. compute_block_1(sh, ((dd_idx1 == sh->qd_idx) ?
  1584. dd_idx2 : dd_idx1),
  1585. 0);
  1586. compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
  1587. return;
  1588. }
  1589. }
  1590. /* We're missing D+P or D+D; */
  1591. if (failb == syndrome_disks) {
  1592. /* We're missing D+P. */
  1593. raid6_datap_recov(syndrome_disks+2, STRIPE_SIZE, faila, ptrs);
  1594. } else {
  1595. /* We're missing D+D. */
  1596. raid6_2data_recov(syndrome_disks+2, STRIPE_SIZE, faila, failb,
  1597. ptrs);
  1598. }
  1599. /* Both the above update both missing blocks */
  1600. set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
  1601. set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
  1602. }
  1603. static void
  1604. schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
  1605. int rcw, int expand)
  1606. {
  1607. int i, pd_idx = sh->pd_idx, disks = sh->disks;
  1608. if (rcw) {
  1609. /* if we are not expanding this is a proper write request, and
  1610. * there will be bios with new data to be drained into the
  1611. * stripe cache
  1612. */
  1613. if (!expand) {
  1614. sh->reconstruct_state = reconstruct_state_drain_run;
  1615. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1616. } else
  1617. sh->reconstruct_state = reconstruct_state_run;
  1618. set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
  1619. for (i = disks; i--; ) {
  1620. struct r5dev *dev = &sh->dev[i];
  1621. if (dev->towrite) {
  1622. set_bit(R5_LOCKED, &dev->flags);
  1623. set_bit(R5_Wantdrain, &dev->flags);
  1624. if (!expand)
  1625. clear_bit(R5_UPTODATE, &dev->flags);
  1626. s->locked++;
  1627. }
  1628. }
  1629. if (s->locked + 1 == disks)
  1630. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  1631. atomic_inc(&sh->raid_conf->pending_full_writes);
  1632. } else {
  1633. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  1634. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  1635. sh->reconstruct_state = reconstruct_state_prexor_drain_run;
  1636. set_bit(STRIPE_OP_PREXOR, &s->ops_request);
  1637. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1638. set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
  1639. for (i = disks; i--; ) {
  1640. struct r5dev *dev = &sh->dev[i];
  1641. if (i == pd_idx)
  1642. continue;
  1643. if (dev->towrite &&
  1644. (test_bit(R5_UPTODATE, &dev->flags) ||
  1645. test_bit(R5_Wantcompute, &dev->flags))) {
  1646. set_bit(R5_Wantdrain, &dev->flags);
  1647. set_bit(R5_LOCKED, &dev->flags);
  1648. clear_bit(R5_UPTODATE, &dev->flags);
  1649. s->locked++;
  1650. }
  1651. }
  1652. }
  1653. /* keep the parity disk locked while asynchronous operations
  1654. * are in flight
  1655. */
  1656. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1657. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1658. s->locked++;
  1659. pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
  1660. __func__, (unsigned long long)sh->sector,
  1661. s->locked, s->ops_request);
  1662. }
  1663. /*
  1664. * Each stripe/dev can have one or more bion attached.
  1665. * toread/towrite point to the first in a chain.
  1666. * The bi_next chain must be in order.
  1667. */
  1668. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1669. {
  1670. struct bio **bip;
  1671. raid5_conf_t *conf = sh->raid_conf;
  1672. int firstwrite=0;
  1673. pr_debug("adding bh b#%llu to stripe s#%llu\n",
  1674. (unsigned long long)bi->bi_sector,
  1675. (unsigned long long)sh->sector);
  1676. spin_lock(&sh->lock);
  1677. spin_lock_irq(&conf->device_lock);
  1678. if (forwrite) {
  1679. bip = &sh->dev[dd_idx].towrite;
  1680. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1681. firstwrite = 1;
  1682. } else
  1683. bip = &sh->dev[dd_idx].toread;
  1684. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1685. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1686. goto overlap;
  1687. bip = & (*bip)->bi_next;
  1688. }
  1689. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1690. goto overlap;
  1691. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1692. if (*bip)
  1693. bi->bi_next = *bip;
  1694. *bip = bi;
  1695. bi->bi_phys_segments++;
  1696. spin_unlock_irq(&conf->device_lock);
  1697. spin_unlock(&sh->lock);
  1698. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1699. (unsigned long long)bi->bi_sector,
  1700. (unsigned long long)sh->sector, dd_idx);
  1701. if (conf->mddev->bitmap && firstwrite) {
  1702. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1703. STRIPE_SECTORS, 0);
  1704. sh->bm_seq = conf->seq_flush+1;
  1705. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1706. }
  1707. if (forwrite) {
  1708. /* check if page is covered */
  1709. sector_t sector = sh->dev[dd_idx].sector;
  1710. for (bi=sh->dev[dd_idx].towrite;
  1711. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1712. bi && bi->bi_sector <= sector;
  1713. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1714. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1715. sector = bi->bi_sector + (bi->bi_size>>9);
  1716. }
  1717. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1718. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1719. }
  1720. return 1;
  1721. overlap:
  1722. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1723. spin_unlock_irq(&conf->device_lock);
  1724. spin_unlock(&sh->lock);
  1725. return 0;
  1726. }
  1727. static void end_reshape(raid5_conf_t *conf);
  1728. static int page_is_zero(struct page *p)
  1729. {
  1730. char *a = page_address(p);
  1731. return ((*(u32*)a) == 0 &&
  1732. memcmp(a, a+4, STRIPE_SIZE-4)==0);
  1733. }
  1734. static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
  1735. struct stripe_head *sh)
  1736. {
  1737. int sectors_per_chunk =
  1738. previous ? (conf->prev_chunk >> 9)
  1739. : (conf->chunk_size >> 9);
  1740. int dd_idx;
  1741. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  1742. int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
  1743. raid5_compute_sector(conf,
  1744. stripe * (disks - conf->max_degraded)
  1745. *sectors_per_chunk + chunk_offset,
  1746. previous,
  1747. &dd_idx, sh);
  1748. }
  1749. static void
  1750. handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
  1751. struct stripe_head_state *s, int disks,
  1752. struct bio **return_bi)
  1753. {
  1754. int i;
  1755. for (i = disks; i--; ) {
  1756. struct bio *bi;
  1757. int bitmap_end = 0;
  1758. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1759. mdk_rdev_t *rdev;
  1760. rcu_read_lock();
  1761. rdev = rcu_dereference(conf->disks[i].rdev);
  1762. if (rdev && test_bit(In_sync, &rdev->flags))
  1763. /* multiple read failures in one stripe */
  1764. md_error(conf->mddev, rdev);
  1765. rcu_read_unlock();
  1766. }
  1767. spin_lock_irq(&conf->device_lock);
  1768. /* fail all writes first */
  1769. bi = sh->dev[i].towrite;
  1770. sh->dev[i].towrite = NULL;
  1771. if (bi) {
  1772. s->to_write--;
  1773. bitmap_end = 1;
  1774. }
  1775. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1776. wake_up(&conf->wait_for_overlap);
  1777. while (bi && bi->bi_sector <
  1778. sh->dev[i].sector + STRIPE_SECTORS) {
  1779. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1780. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1781. if (!raid5_dec_bi_phys_segments(bi)) {
  1782. md_write_end(conf->mddev);
  1783. bi->bi_next = *return_bi;
  1784. *return_bi = bi;
  1785. }
  1786. bi = nextbi;
  1787. }
  1788. /* and fail all 'written' */
  1789. bi = sh->dev[i].written;
  1790. sh->dev[i].written = NULL;
  1791. if (bi) bitmap_end = 1;
  1792. while (bi && bi->bi_sector <
  1793. sh->dev[i].sector + STRIPE_SECTORS) {
  1794. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1795. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1796. if (!raid5_dec_bi_phys_segments(bi)) {
  1797. md_write_end(conf->mddev);
  1798. bi->bi_next = *return_bi;
  1799. *return_bi = bi;
  1800. }
  1801. bi = bi2;
  1802. }
  1803. /* fail any reads if this device is non-operational and
  1804. * the data has not reached the cache yet.
  1805. */
  1806. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  1807. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1808. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  1809. bi = sh->dev[i].toread;
  1810. sh->dev[i].toread = NULL;
  1811. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1812. wake_up(&conf->wait_for_overlap);
  1813. if (bi) s->to_read--;
  1814. while (bi && bi->bi_sector <
  1815. sh->dev[i].sector + STRIPE_SECTORS) {
  1816. struct bio *nextbi =
  1817. r5_next_bio(bi, sh->dev[i].sector);
  1818. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1819. if (!raid5_dec_bi_phys_segments(bi)) {
  1820. bi->bi_next = *return_bi;
  1821. *return_bi = bi;
  1822. }
  1823. bi = nextbi;
  1824. }
  1825. }
  1826. spin_unlock_irq(&conf->device_lock);
  1827. if (bitmap_end)
  1828. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1829. STRIPE_SECTORS, 0, 0);
  1830. }
  1831. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  1832. if (atomic_dec_and_test(&conf->pending_full_writes))
  1833. md_wakeup_thread(conf->mddev->thread);
  1834. }
  1835. /* fetch_block5 - checks the given member device to see if its data needs
  1836. * to be read or computed to satisfy a request.
  1837. *
  1838. * Returns 1 when no more member devices need to be checked, otherwise returns
  1839. * 0 to tell the loop in handle_stripe_fill5 to continue
  1840. */
  1841. static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
  1842. int disk_idx, int disks)
  1843. {
  1844. struct r5dev *dev = &sh->dev[disk_idx];
  1845. struct r5dev *failed_dev = &sh->dev[s->failed_num];
  1846. /* is the data in this block needed, and can we get it? */
  1847. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1848. !test_bit(R5_UPTODATE, &dev->flags) &&
  1849. (dev->toread ||
  1850. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1851. s->syncing || s->expanding ||
  1852. (s->failed &&
  1853. (failed_dev->toread ||
  1854. (failed_dev->towrite &&
  1855. !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
  1856. /* We would like to get this block, possibly by computing it,
  1857. * otherwise read it if the backing disk is insync
  1858. */
  1859. if ((s->uptodate == disks - 1) &&
  1860. (s->failed && disk_idx == s->failed_num)) {
  1861. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  1862. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  1863. set_bit(R5_Wantcompute, &dev->flags);
  1864. sh->ops.target = disk_idx;
  1865. s->req_compute = 1;
  1866. /* Careful: from this point on 'uptodate' is in the eye
  1867. * of raid5_run_ops which services 'compute' operations
  1868. * before writes. R5_Wantcompute flags a block that will
  1869. * be R5_UPTODATE by the time it is needed for a
  1870. * subsequent operation.
  1871. */
  1872. s->uptodate++;
  1873. return 1; /* uptodate + compute == disks */
  1874. } else if (test_bit(R5_Insync, &dev->flags)) {
  1875. set_bit(R5_LOCKED, &dev->flags);
  1876. set_bit(R5_Wantread, &dev->flags);
  1877. s->locked++;
  1878. pr_debug("Reading block %d (sync=%d)\n", disk_idx,
  1879. s->syncing);
  1880. }
  1881. }
  1882. return 0;
  1883. }
  1884. /**
  1885. * handle_stripe_fill5 - read or compute data to satisfy pending requests.
  1886. */
  1887. static void handle_stripe_fill5(struct stripe_head *sh,
  1888. struct stripe_head_state *s, int disks)
  1889. {
  1890. int i;
  1891. /* look for blocks to read/compute, skip this if a compute
  1892. * is already in flight, or if the stripe contents are in the
  1893. * midst of changing due to a write
  1894. */
  1895. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  1896. !sh->reconstruct_state)
  1897. for (i = disks; i--; )
  1898. if (fetch_block5(sh, s, i, disks))
  1899. break;
  1900. set_bit(STRIPE_HANDLE, &sh->state);
  1901. }
  1902. static void handle_stripe_fill6(struct stripe_head *sh,
  1903. struct stripe_head_state *s, struct r6_state *r6s,
  1904. int disks)
  1905. {
  1906. int i;
  1907. for (i = disks; i--; ) {
  1908. struct r5dev *dev = &sh->dev[i];
  1909. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1910. !test_bit(R5_UPTODATE, &dev->flags) &&
  1911. (dev->toread || (dev->towrite &&
  1912. !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1913. s->syncing || s->expanding ||
  1914. (s->failed >= 1 &&
  1915. (sh->dev[r6s->failed_num[0]].toread ||
  1916. s->to_write)) ||
  1917. (s->failed >= 2 &&
  1918. (sh->dev[r6s->failed_num[1]].toread ||
  1919. s->to_write)))) {
  1920. /* we would like to get this block, possibly
  1921. * by computing it, but we might not be able to
  1922. */
  1923. if ((s->uptodate == disks - 1) &&
  1924. (s->failed && (i == r6s->failed_num[0] ||
  1925. i == r6s->failed_num[1]))) {
  1926. pr_debug("Computing stripe %llu block %d\n",
  1927. (unsigned long long)sh->sector, i);
  1928. compute_block_1(sh, i, 0);
  1929. s->uptodate++;
  1930. } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
  1931. /* Computing 2-failure is *very* expensive; only
  1932. * do it if failed >= 2
  1933. */
  1934. int other;
  1935. for (other = disks; other--; ) {
  1936. if (other == i)
  1937. continue;
  1938. if (!test_bit(R5_UPTODATE,
  1939. &sh->dev[other].flags))
  1940. break;
  1941. }
  1942. BUG_ON(other < 0);
  1943. pr_debug("Computing stripe %llu blocks %d,%d\n",
  1944. (unsigned long long)sh->sector,
  1945. i, other);
  1946. compute_block_2(sh, i, other);
  1947. s->uptodate += 2;
  1948. } else if (test_bit(R5_Insync, &dev->flags)) {
  1949. set_bit(R5_LOCKED, &dev->flags);
  1950. set_bit(R5_Wantread, &dev->flags);
  1951. s->locked++;
  1952. pr_debug("Reading block %d (sync=%d)\n",
  1953. i, s->syncing);
  1954. }
  1955. }
  1956. }
  1957. set_bit(STRIPE_HANDLE, &sh->state);
  1958. }
  1959. /* handle_stripe_clean_event
  1960. * any written block on an uptodate or failed drive can be returned.
  1961. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  1962. * never LOCKED, so we don't need to test 'failed' directly.
  1963. */
  1964. static void handle_stripe_clean_event(raid5_conf_t *conf,
  1965. struct stripe_head *sh, int disks, struct bio **return_bi)
  1966. {
  1967. int i;
  1968. struct r5dev *dev;
  1969. for (i = disks; i--; )
  1970. if (sh->dev[i].written) {
  1971. dev = &sh->dev[i];
  1972. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1973. test_bit(R5_UPTODATE, &dev->flags)) {
  1974. /* We can return any write requests */
  1975. struct bio *wbi, *wbi2;
  1976. int bitmap_end = 0;
  1977. pr_debug("Return write for disc %d\n", i);
  1978. spin_lock_irq(&conf->device_lock);
  1979. wbi = dev->written;
  1980. dev->written = NULL;
  1981. while (wbi && wbi->bi_sector <
  1982. dev->sector + STRIPE_SECTORS) {
  1983. wbi2 = r5_next_bio(wbi, dev->sector);
  1984. if (!raid5_dec_bi_phys_segments(wbi)) {
  1985. md_write_end(conf->mddev);
  1986. wbi->bi_next = *return_bi;
  1987. *return_bi = wbi;
  1988. }
  1989. wbi = wbi2;
  1990. }
  1991. if (dev->towrite == NULL)
  1992. bitmap_end = 1;
  1993. spin_unlock_irq(&conf->device_lock);
  1994. if (bitmap_end)
  1995. bitmap_endwrite(conf->mddev->bitmap,
  1996. sh->sector,
  1997. STRIPE_SECTORS,
  1998. !test_bit(STRIPE_DEGRADED, &sh->state),
  1999. 0);
  2000. }
  2001. }
  2002. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  2003. if (atomic_dec_and_test(&conf->pending_full_writes))
  2004. md_wakeup_thread(conf->mddev->thread);
  2005. }
  2006. static void handle_stripe_dirtying5(raid5_conf_t *conf,
  2007. struct stripe_head *sh, struct stripe_head_state *s, int disks)
  2008. {
  2009. int rmw = 0, rcw = 0, i;
  2010. for (i = disks; i--; ) {
  2011. /* would I have to read this buffer for read_modify_write */
  2012. struct r5dev *dev = &sh->dev[i];
  2013. if ((dev->towrite || i == sh->pd_idx) &&
  2014. !test_bit(R5_LOCKED, &dev->flags) &&
  2015. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2016. test_bit(R5_Wantcompute, &dev->flags))) {
  2017. if (test_bit(R5_Insync, &dev->flags))
  2018. rmw++;
  2019. else
  2020. rmw += 2*disks; /* cannot read it */
  2021. }
  2022. /* Would I have to read this buffer for reconstruct_write */
  2023. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  2024. !test_bit(R5_LOCKED, &dev->flags) &&
  2025. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2026. test_bit(R5_Wantcompute, &dev->flags))) {
  2027. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  2028. else
  2029. rcw += 2*disks;
  2030. }
  2031. }
  2032. pr_debug("for sector %llu, rmw=%d rcw=%d\n",
  2033. (unsigned long long)sh->sector, rmw, rcw);
  2034. set_bit(STRIPE_HANDLE, &sh->state);
  2035. if (rmw < rcw && rmw > 0)
  2036. /* prefer read-modify-write, but need to get some data */
  2037. for (i = disks; i--; ) {
  2038. struct r5dev *dev = &sh->dev[i];
  2039. if ((dev->towrite || i == sh->pd_idx) &&
  2040. !test_bit(R5_LOCKED, &dev->flags) &&
  2041. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2042. test_bit(R5_Wantcompute, &dev->flags)) &&
  2043. test_bit(R5_Insync, &dev->flags)) {
  2044. if (
  2045. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2046. pr_debug("Read_old block "
  2047. "%d for r-m-w\n", i);
  2048. set_bit(R5_LOCKED, &dev->flags);
  2049. set_bit(R5_Wantread, &dev->flags);
  2050. s->locked++;
  2051. } else {
  2052. set_bit(STRIPE_DELAYED, &sh->state);
  2053. set_bit(STRIPE_HANDLE, &sh->state);
  2054. }
  2055. }
  2056. }
  2057. if (rcw <= rmw && rcw > 0)
  2058. /* want reconstruct write, but need to get some data */
  2059. for (i = disks; i--; ) {
  2060. struct r5dev *dev = &sh->dev[i];
  2061. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  2062. i != sh->pd_idx &&
  2063. !test_bit(R5_LOCKED, &dev->flags) &&
  2064. !(test_bit(R5_UPTODATE, &dev->flags) ||
  2065. test_bit(R5_Wantcompute, &dev->flags)) &&
  2066. test_bit(R5_Insync, &dev->flags)) {
  2067. if (
  2068. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2069. pr_debug("Read_old block "
  2070. "%d for Reconstruct\n", i);
  2071. set_bit(R5_LOCKED, &dev->flags);
  2072. set_bit(R5_Wantread, &dev->flags);
  2073. s->locked++;
  2074. } else {
  2075. set_bit(STRIPE_DELAYED, &sh->state);
  2076. set_bit(STRIPE_HANDLE, &sh->state);
  2077. }
  2078. }
  2079. }
  2080. /* now if nothing is locked, and if we have enough data,
  2081. * we can start a write request
  2082. */
  2083. /* since handle_stripe can be called at any time we need to handle the
  2084. * case where a compute block operation has been submitted and then a
  2085. * subsequent call wants to start a write request. raid5_run_ops only
  2086. * handles the case where compute block and postxor are requested
  2087. * simultaneously. If this is not the case then new writes need to be
  2088. * held off until the compute completes.
  2089. */
  2090. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  2091. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  2092. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  2093. schedule_reconstruction5(sh, s, rcw == 0, 0);
  2094. }
  2095. static void handle_stripe_dirtying6(raid5_conf_t *conf,
  2096. struct stripe_head *sh, struct stripe_head_state *s,
  2097. struct r6_state *r6s, int disks)
  2098. {
  2099. int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
  2100. int qd_idx = sh->qd_idx;
  2101. for (i = disks; i--; ) {
  2102. struct r5dev *dev = &sh->dev[i];
  2103. /* Would I have to read this buffer for reconstruct_write */
  2104. if (!test_bit(R5_OVERWRITE, &dev->flags)
  2105. && i != pd_idx && i != qd_idx
  2106. && (!test_bit(R5_LOCKED, &dev->flags)
  2107. ) &&
  2108. !test_bit(R5_UPTODATE, &dev->flags)) {
  2109. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  2110. else {
  2111. pr_debug("raid6: must_compute: "
  2112. "disk %d flags=%#lx\n", i, dev->flags);
  2113. must_compute++;
  2114. }
  2115. }
  2116. }
  2117. pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
  2118. (unsigned long long)sh->sector, rcw, must_compute);
  2119. set_bit(STRIPE_HANDLE, &sh->state);
  2120. if (rcw > 0)
  2121. /* want reconstruct write, but need to get some data */
  2122. for (i = disks; i--; ) {
  2123. struct r5dev *dev = &sh->dev[i];
  2124. if (!test_bit(R5_OVERWRITE, &dev->flags)
  2125. && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
  2126. && !test_bit(R5_LOCKED, &dev->flags) &&
  2127. !test_bit(R5_UPTODATE, &dev->flags) &&
  2128. test_bit(R5_Insync, &dev->flags)) {
  2129. if (
  2130. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2131. pr_debug("Read_old stripe %llu "
  2132. "block %d for Reconstruct\n",
  2133. (unsigned long long)sh->sector, i);
  2134. set_bit(R5_LOCKED, &dev->flags);
  2135. set_bit(R5_Wantread, &dev->flags);
  2136. s->locked++;
  2137. } else {
  2138. pr_debug("Request delayed stripe %llu "
  2139. "block %d for Reconstruct\n",
  2140. (unsigned long long)sh->sector, i);
  2141. set_bit(STRIPE_DELAYED, &sh->state);
  2142. set_bit(STRIPE_HANDLE, &sh->state);
  2143. }
  2144. }
  2145. }
  2146. /* now if nothing is locked, and if we have enough data, we can start a
  2147. * write request
  2148. */
  2149. if (s->locked == 0 && rcw == 0 &&
  2150. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  2151. if (must_compute > 0) {
  2152. /* We have failed blocks and need to compute them */
  2153. switch (s->failed) {
  2154. case 0:
  2155. BUG();
  2156. case 1:
  2157. compute_block_1(sh, r6s->failed_num[0], 0);
  2158. break;
  2159. case 2:
  2160. compute_block_2(sh, r6s->failed_num[0],
  2161. r6s->failed_num[1]);
  2162. break;
  2163. default: /* This request should have been failed? */
  2164. BUG();
  2165. }
  2166. }
  2167. pr_debug("Computing parity for stripe %llu\n",
  2168. (unsigned long long)sh->sector);
  2169. compute_parity6(sh, RECONSTRUCT_WRITE);
  2170. /* now every locked buffer is ready to be written */
  2171. for (i = disks; i--; )
  2172. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  2173. pr_debug("Writing stripe %llu block %d\n",
  2174. (unsigned long long)sh->sector, i);
  2175. s->locked++;
  2176. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2177. }
  2178. if (s->locked == disks)
  2179. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  2180. atomic_inc(&conf->pending_full_writes);
  2181. /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
  2182. set_bit(STRIPE_INSYNC, &sh->state);
  2183. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2184. atomic_dec(&conf->preread_active_stripes);
  2185. if (atomic_read(&conf->preread_active_stripes) <
  2186. IO_THRESHOLD)
  2187. md_wakeup_thread(conf->mddev->thread);
  2188. }
  2189. }
  2190. }
  2191. static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
  2192. struct stripe_head_state *s, int disks)
  2193. {
  2194. struct r5dev *dev = NULL;
  2195. set_bit(STRIPE_HANDLE, &sh->state);
  2196. switch (sh->check_state) {
  2197. case check_state_idle:
  2198. /* start a new check operation if there are no failures */
  2199. if (s->failed == 0) {
  2200. BUG_ON(s->uptodate != disks);
  2201. sh->check_state = check_state_run;
  2202. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2203. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  2204. s->uptodate--;
  2205. break;
  2206. }
  2207. dev = &sh->dev[s->failed_num];
  2208. /* fall through */
  2209. case check_state_compute_result:
  2210. sh->check_state = check_state_idle;
  2211. if (!dev)
  2212. dev = &sh->dev[sh->pd_idx];
  2213. /* check that a write has not made the stripe insync */
  2214. if (test_bit(STRIPE_INSYNC, &sh->state))
  2215. break;
  2216. /* either failed parity check, or recovery is happening */
  2217. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  2218. BUG_ON(s->uptodate != disks);
  2219. set_bit(R5_LOCKED, &dev->flags);
  2220. s->locked++;
  2221. set_bit(R5_Wantwrite, &dev->flags);
  2222. clear_bit(STRIPE_DEGRADED, &sh->state);
  2223. set_bit(STRIPE_INSYNC, &sh->state);
  2224. break;
  2225. case check_state_run:
  2226. break; /* we will be called again upon completion */
  2227. case check_state_check_result:
  2228. sh->check_state = check_state_idle;
  2229. /* if a failure occurred during the check operation, leave
  2230. * STRIPE_INSYNC not set and let the stripe be handled again
  2231. */
  2232. if (s->failed)
  2233. break;
  2234. /* handle a successful check operation, if parity is correct
  2235. * we are done. Otherwise update the mismatch count and repair
  2236. * parity if !MD_RECOVERY_CHECK
  2237. */
  2238. if (sh->ops.zero_sum_result == 0)
  2239. /* parity is correct (on disc,
  2240. * not in buffer any more)
  2241. */
  2242. set_bit(STRIPE_INSYNC, &sh->state);
  2243. else {
  2244. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2245. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2246. /* don't try to repair!! */
  2247. set_bit(STRIPE_INSYNC, &sh->state);
  2248. else {
  2249. sh->check_state = check_state_compute_run;
  2250. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2251. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2252. set_bit(R5_Wantcompute,
  2253. &sh->dev[sh->pd_idx].flags);
  2254. sh->ops.target = sh->pd_idx;
  2255. s->uptodate++;
  2256. }
  2257. }
  2258. break;
  2259. case check_state_compute_run:
  2260. break;
  2261. default:
  2262. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2263. __func__, sh->check_state,
  2264. (unsigned long long) sh->sector);
  2265. BUG();
  2266. }
  2267. }
  2268. static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
  2269. struct stripe_head_state *s,
  2270. struct r6_state *r6s, struct page *tmp_page,
  2271. int disks)
  2272. {
  2273. int update_p = 0, update_q = 0;
  2274. struct r5dev *dev;
  2275. int pd_idx = sh->pd_idx;
  2276. int qd_idx = sh->qd_idx;
  2277. set_bit(STRIPE_HANDLE, &sh->state);
  2278. BUG_ON(s->failed > 2);
  2279. BUG_ON(s->uptodate < disks);
  2280. /* Want to check and possibly repair P and Q.
  2281. * However there could be one 'failed' device, in which
  2282. * case we can only check one of them, possibly using the
  2283. * other to generate missing data
  2284. */
  2285. /* If !tmp_page, we cannot do the calculations,
  2286. * but as we have set STRIPE_HANDLE, we will soon be called
  2287. * by stripe_handle with a tmp_page - just wait until then.
  2288. */
  2289. if (tmp_page) {
  2290. if (s->failed == r6s->q_failed) {
  2291. /* The only possible failed device holds 'Q', so it
  2292. * makes sense to check P (If anything else were failed,
  2293. * we would have used P to recreate it).
  2294. */
  2295. compute_block_1(sh, pd_idx, 1);
  2296. if (!page_is_zero(sh->dev[pd_idx].page)) {
  2297. compute_block_1(sh, pd_idx, 0);
  2298. update_p = 1;
  2299. }
  2300. }
  2301. if (!r6s->q_failed && s->failed < 2) {
  2302. /* q is not failed, and we didn't use it to generate
  2303. * anything, so it makes sense to check it
  2304. */
  2305. memcpy(page_address(tmp_page),
  2306. page_address(sh->dev[qd_idx].page),
  2307. STRIPE_SIZE);
  2308. compute_parity6(sh, UPDATE_PARITY);
  2309. if (memcmp(page_address(tmp_page),
  2310. page_address(sh->dev[qd_idx].page),
  2311. STRIPE_SIZE) != 0) {
  2312. clear_bit(STRIPE_INSYNC, &sh->state);
  2313. update_q = 1;
  2314. }
  2315. }
  2316. if (update_p || update_q) {
  2317. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2318. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2319. /* don't try to repair!! */
  2320. update_p = update_q = 0;
  2321. }
  2322. /* now write out any block on a failed drive,
  2323. * or P or Q if they need it
  2324. */
  2325. if (s->failed == 2) {
  2326. dev = &sh->dev[r6s->failed_num[1]];
  2327. s->locked++;
  2328. set_bit(R5_LOCKED, &dev->flags);
  2329. set_bit(R5_Wantwrite, &dev->flags);
  2330. }
  2331. if (s->failed >= 1) {
  2332. dev = &sh->dev[r6s->failed_num[0]];
  2333. s->locked++;
  2334. set_bit(R5_LOCKED, &dev->flags);
  2335. set_bit(R5_Wantwrite, &dev->flags);
  2336. }
  2337. if (update_p) {
  2338. dev = &sh->dev[pd_idx];
  2339. s->locked++;
  2340. set_bit(R5_LOCKED, &dev->flags);
  2341. set_bit(R5_Wantwrite, &dev->flags);
  2342. }
  2343. if (update_q) {
  2344. dev = &sh->dev[qd_idx];
  2345. s->locked++;
  2346. set_bit(R5_LOCKED, &dev->flags);
  2347. set_bit(R5_Wantwrite, &dev->flags);
  2348. }
  2349. clear_bit(STRIPE_DEGRADED, &sh->state);
  2350. set_bit(STRIPE_INSYNC, &sh->state);
  2351. }
  2352. }
  2353. static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
  2354. struct r6_state *r6s)
  2355. {
  2356. int i;
  2357. /* We have read all the blocks in this stripe and now we need to
  2358. * copy some of them into a target stripe for expand.
  2359. */
  2360. struct dma_async_tx_descriptor *tx = NULL;
  2361. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2362. for (i = 0; i < sh->disks; i++)
  2363. if (i != sh->pd_idx && i != sh->qd_idx) {
  2364. int dd_idx, j;
  2365. struct stripe_head *sh2;
  2366. sector_t bn = compute_blocknr(sh, i, 1);
  2367. sector_t s = raid5_compute_sector(conf, bn, 0,
  2368. &dd_idx, NULL);
  2369. sh2 = get_active_stripe(conf, s, 0, 1);
  2370. if (sh2 == NULL)
  2371. /* so far only the early blocks of this stripe
  2372. * have been requested. When later blocks
  2373. * get requested, we will try again
  2374. */
  2375. continue;
  2376. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  2377. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  2378. /* must have already done this block */
  2379. release_stripe(sh2);
  2380. continue;
  2381. }
  2382. /* place all the copies on one channel */
  2383. tx = async_memcpy(sh2->dev[dd_idx].page,
  2384. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  2385. 0, tx, NULL, NULL);
  2386. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  2387. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  2388. for (j = 0; j < conf->raid_disks; j++)
  2389. if (j != sh2->pd_idx &&
  2390. (!r6s || j != sh2->qd_idx) &&
  2391. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  2392. break;
  2393. if (j == conf->raid_disks) {
  2394. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  2395. set_bit(STRIPE_HANDLE, &sh2->state);
  2396. }
  2397. release_stripe(sh2);
  2398. }
  2399. /* done submitting copies, wait for them to complete */
  2400. if (tx) {
  2401. async_tx_ack(tx);
  2402. dma_wait_for_async_tx(tx);
  2403. }
  2404. }
  2405. /*
  2406. * handle_stripe - do things to a stripe.
  2407. *
  2408. * We lock the stripe and then examine the state of various bits
  2409. * to see what needs to be done.
  2410. * Possible results:
  2411. * return some read request which now have data
  2412. * return some write requests which are safely on disc
  2413. * schedule a read on some buffers
  2414. * schedule a write of some buffers
  2415. * return confirmation of parity correctness
  2416. *
  2417. * buffers are taken off read_list or write_list, and bh_cache buffers
  2418. * get BH_Lock set before the stripe lock is released.
  2419. *
  2420. */
  2421. static bool handle_stripe5(struct stripe_head *sh)
  2422. {
  2423. raid5_conf_t *conf = sh->raid_conf;
  2424. int disks = sh->disks, i;
  2425. struct bio *return_bi = NULL;
  2426. struct stripe_head_state s;
  2427. struct r5dev *dev;
  2428. mdk_rdev_t *blocked_rdev = NULL;
  2429. int prexor;
  2430. memset(&s, 0, sizeof(s));
  2431. pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
  2432. "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
  2433. atomic_read(&sh->count), sh->pd_idx, sh->check_state,
  2434. sh->reconstruct_state);
  2435. spin_lock(&sh->lock);
  2436. clear_bit(STRIPE_HANDLE, &sh->state);
  2437. clear_bit(STRIPE_DELAYED, &sh->state);
  2438. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2439. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2440. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2441. /* Now to look around and see what can be done */
  2442. rcu_read_lock();
  2443. for (i=disks; i--; ) {
  2444. mdk_rdev_t *rdev;
  2445. struct r5dev *dev = &sh->dev[i];
  2446. clear_bit(R5_Insync, &dev->flags);
  2447. pr_debug("check %d: state 0x%lx toread %p read %p write %p "
  2448. "written %p\n", i, dev->flags, dev->toread, dev->read,
  2449. dev->towrite, dev->written);
  2450. /* maybe we can request a biofill operation
  2451. *
  2452. * new wantfill requests are only permitted while
  2453. * ops_complete_biofill is guaranteed to be inactive
  2454. */
  2455. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2456. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  2457. set_bit(R5_Wantfill, &dev->flags);
  2458. /* now count some things */
  2459. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2460. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2461. if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
  2462. if (test_bit(R5_Wantfill, &dev->flags))
  2463. s.to_fill++;
  2464. else if (dev->toread)
  2465. s.to_read++;
  2466. if (dev->towrite) {
  2467. s.to_write++;
  2468. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2469. s.non_overwrite++;
  2470. }
  2471. if (dev->written)
  2472. s.written++;
  2473. rdev = rcu_dereference(conf->disks[i].rdev);
  2474. if (blocked_rdev == NULL &&
  2475. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2476. blocked_rdev = rdev;
  2477. atomic_inc(&rdev->nr_pending);
  2478. }
  2479. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2480. /* The ReadError flag will just be confusing now */
  2481. clear_bit(R5_ReadError, &dev->flags);
  2482. clear_bit(R5_ReWrite, &dev->flags);
  2483. }
  2484. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2485. || test_bit(R5_ReadError, &dev->flags)) {
  2486. s.failed++;
  2487. s.failed_num = i;
  2488. } else
  2489. set_bit(R5_Insync, &dev->flags);
  2490. }
  2491. rcu_read_unlock();
  2492. if (unlikely(blocked_rdev)) {
  2493. if (s.syncing || s.expanding || s.expanded ||
  2494. s.to_write || s.written) {
  2495. set_bit(STRIPE_HANDLE, &sh->state);
  2496. goto unlock;
  2497. }
  2498. /* There is nothing for the blocked_rdev to block */
  2499. rdev_dec_pending(blocked_rdev, conf->mddev);
  2500. blocked_rdev = NULL;
  2501. }
  2502. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  2503. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  2504. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  2505. }
  2506. pr_debug("locked=%d uptodate=%d to_read=%d"
  2507. " to_write=%d failed=%d failed_num=%d\n",
  2508. s.locked, s.uptodate, s.to_read, s.to_write,
  2509. s.failed, s.failed_num);
  2510. /* check if the array has lost two devices and, if so, some requests might
  2511. * need to be failed
  2512. */
  2513. if (s.failed > 1 && s.to_read+s.to_write+s.written)
  2514. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  2515. if (s.failed > 1 && s.syncing) {
  2516. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2517. clear_bit(STRIPE_SYNCING, &sh->state);
  2518. s.syncing = 0;
  2519. }
  2520. /* might be able to return some write requests if the parity block
  2521. * is safe, or on a failed drive
  2522. */
  2523. dev = &sh->dev[sh->pd_idx];
  2524. if ( s.written &&
  2525. ((test_bit(R5_Insync, &dev->flags) &&
  2526. !test_bit(R5_LOCKED, &dev->flags) &&
  2527. test_bit(R5_UPTODATE, &dev->flags)) ||
  2528. (s.failed == 1 && s.failed_num == sh->pd_idx)))
  2529. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  2530. /* Now we might consider reading some blocks, either to check/generate
  2531. * parity, or to satisfy requests
  2532. * or to load a block that is being partially written.
  2533. */
  2534. if (s.to_read || s.non_overwrite ||
  2535. (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
  2536. handle_stripe_fill5(sh, &s, disks);
  2537. /* Now we check to see if any write operations have recently
  2538. * completed
  2539. */
  2540. prexor = 0;
  2541. if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
  2542. prexor = 1;
  2543. if (sh->reconstruct_state == reconstruct_state_drain_result ||
  2544. sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
  2545. sh->reconstruct_state = reconstruct_state_idle;
  2546. /* All the 'written' buffers and the parity block are ready to
  2547. * be written back to disk
  2548. */
  2549. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
  2550. for (i = disks; i--; ) {
  2551. dev = &sh->dev[i];
  2552. if (test_bit(R5_LOCKED, &dev->flags) &&
  2553. (i == sh->pd_idx || dev->written)) {
  2554. pr_debug("Writing block %d\n", i);
  2555. set_bit(R5_Wantwrite, &dev->flags);
  2556. if (prexor)
  2557. continue;
  2558. if (!test_bit(R5_Insync, &dev->flags) ||
  2559. (i == sh->pd_idx && s.failed == 0))
  2560. set_bit(STRIPE_INSYNC, &sh->state);
  2561. }
  2562. }
  2563. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2564. atomic_dec(&conf->preread_active_stripes);
  2565. if (atomic_read(&conf->preread_active_stripes) <
  2566. IO_THRESHOLD)
  2567. md_wakeup_thread(conf->mddev->thread);
  2568. }
  2569. }
  2570. /* Now to consider new write requests and what else, if anything
  2571. * should be read. We do not handle new writes when:
  2572. * 1/ A 'write' operation (copy+xor) is already in flight.
  2573. * 2/ A 'check' operation is in flight, as it may clobber the parity
  2574. * block.
  2575. */
  2576. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  2577. handle_stripe_dirtying5(conf, sh, &s, disks);
  2578. /* maybe we need to check and possibly fix the parity for this stripe
  2579. * Any reads will already have been scheduled, so we just see if enough
  2580. * data is available. The parity check is held off while parity
  2581. * dependent operations are in flight.
  2582. */
  2583. if (sh->check_state ||
  2584. (s.syncing && s.locked == 0 &&
  2585. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  2586. !test_bit(STRIPE_INSYNC, &sh->state)))
  2587. handle_parity_checks5(conf, sh, &s, disks);
  2588. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2589. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2590. clear_bit(STRIPE_SYNCING, &sh->state);
  2591. }
  2592. /* If the failed drive is just a ReadError, then we might need to progress
  2593. * the repair/check process
  2594. */
  2595. if (s.failed == 1 && !conf->mddev->ro &&
  2596. test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
  2597. && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
  2598. && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
  2599. ) {
  2600. dev = &sh->dev[s.failed_num];
  2601. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2602. set_bit(R5_Wantwrite, &dev->flags);
  2603. set_bit(R5_ReWrite, &dev->flags);
  2604. set_bit(R5_LOCKED, &dev->flags);
  2605. s.locked++;
  2606. } else {
  2607. /* let's read it back */
  2608. set_bit(R5_Wantread, &dev->flags);
  2609. set_bit(R5_LOCKED, &dev->flags);
  2610. s.locked++;
  2611. }
  2612. }
  2613. /* Finish reconstruct operations initiated by the expansion process */
  2614. if (sh->reconstruct_state == reconstruct_state_result) {
  2615. struct stripe_head *sh2
  2616. = get_active_stripe(conf, sh->sector, 1, 1);
  2617. if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
  2618. /* sh cannot be written until sh2 has been read.
  2619. * so arrange for sh to be delayed a little
  2620. */
  2621. set_bit(STRIPE_DELAYED, &sh->state);
  2622. set_bit(STRIPE_HANDLE, &sh->state);
  2623. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  2624. &sh2->state))
  2625. atomic_inc(&conf->preread_active_stripes);
  2626. release_stripe(sh2);
  2627. goto unlock;
  2628. }
  2629. if (sh2)
  2630. release_stripe(sh2);
  2631. sh->reconstruct_state = reconstruct_state_idle;
  2632. clear_bit(STRIPE_EXPANDING, &sh->state);
  2633. for (i = conf->raid_disks; i--; ) {
  2634. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2635. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2636. s.locked++;
  2637. }
  2638. }
  2639. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  2640. !sh->reconstruct_state) {
  2641. /* Need to write out all blocks after computing parity */
  2642. sh->disks = conf->raid_disks;
  2643. stripe_set_idx(sh->sector, conf, 0, sh);
  2644. schedule_reconstruction5(sh, &s, 1, 1);
  2645. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  2646. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2647. atomic_dec(&conf->reshape_stripes);
  2648. wake_up(&conf->wait_for_overlap);
  2649. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2650. }
  2651. if (s.expanding && s.locked == 0 &&
  2652. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  2653. handle_stripe_expansion(conf, sh, NULL);
  2654. unlock:
  2655. spin_unlock(&sh->lock);
  2656. /* wait for this device to become unblocked */
  2657. if (unlikely(blocked_rdev))
  2658. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  2659. if (s.ops_request)
  2660. raid5_run_ops(sh, s.ops_request);
  2661. ops_run_io(sh, &s);
  2662. return_io(return_bi);
  2663. return blocked_rdev == NULL;
  2664. }
  2665. static bool handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
  2666. {
  2667. raid5_conf_t *conf = sh->raid_conf;
  2668. int disks = sh->disks;
  2669. struct bio *return_bi = NULL;
  2670. int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx;
  2671. struct stripe_head_state s;
  2672. struct r6_state r6s;
  2673. struct r5dev *dev, *pdev, *qdev;
  2674. mdk_rdev_t *blocked_rdev = NULL;
  2675. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  2676. "pd_idx=%d, qd_idx=%d\n",
  2677. (unsigned long long)sh->sector, sh->state,
  2678. atomic_read(&sh->count), pd_idx, qd_idx);
  2679. memset(&s, 0, sizeof(s));
  2680. spin_lock(&sh->lock);
  2681. clear_bit(STRIPE_HANDLE, &sh->state);
  2682. clear_bit(STRIPE_DELAYED, &sh->state);
  2683. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2684. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2685. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2686. /* Now to look around and see what can be done */
  2687. rcu_read_lock();
  2688. for (i=disks; i--; ) {
  2689. mdk_rdev_t *rdev;
  2690. dev = &sh->dev[i];
  2691. clear_bit(R5_Insync, &dev->flags);
  2692. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  2693. i, dev->flags, dev->toread, dev->towrite, dev->written);
  2694. /* maybe we can reply to a read */
  2695. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  2696. struct bio *rbi, *rbi2;
  2697. pr_debug("Return read for disc %d\n", i);
  2698. spin_lock_irq(&conf->device_lock);
  2699. rbi = dev->toread;
  2700. dev->toread = NULL;
  2701. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  2702. wake_up(&conf->wait_for_overlap);
  2703. spin_unlock_irq(&conf->device_lock);
  2704. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  2705. copy_data(0, rbi, dev->page, dev->sector);
  2706. rbi2 = r5_next_bio(rbi, dev->sector);
  2707. spin_lock_irq(&conf->device_lock);
  2708. if (!raid5_dec_bi_phys_segments(rbi)) {
  2709. rbi->bi_next = return_bi;
  2710. return_bi = rbi;
  2711. }
  2712. spin_unlock_irq(&conf->device_lock);
  2713. rbi = rbi2;
  2714. }
  2715. }
  2716. /* now count some things */
  2717. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2718. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2719. if (dev->toread)
  2720. s.to_read++;
  2721. if (dev->towrite) {
  2722. s.to_write++;
  2723. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2724. s.non_overwrite++;
  2725. }
  2726. if (dev->written)
  2727. s.written++;
  2728. rdev = rcu_dereference(conf->disks[i].rdev);
  2729. if (blocked_rdev == NULL &&
  2730. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2731. blocked_rdev = rdev;
  2732. atomic_inc(&rdev->nr_pending);
  2733. }
  2734. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2735. /* The ReadError flag will just be confusing now */
  2736. clear_bit(R5_ReadError, &dev->flags);
  2737. clear_bit(R5_ReWrite, &dev->flags);
  2738. }
  2739. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2740. || test_bit(R5_ReadError, &dev->flags)) {
  2741. if (s.failed < 2)
  2742. r6s.failed_num[s.failed] = i;
  2743. s.failed++;
  2744. } else
  2745. set_bit(R5_Insync, &dev->flags);
  2746. }
  2747. rcu_read_unlock();
  2748. if (unlikely(blocked_rdev)) {
  2749. if (s.syncing || s.expanding || s.expanded ||
  2750. s.to_write || s.written) {
  2751. set_bit(STRIPE_HANDLE, &sh->state);
  2752. goto unlock;
  2753. }
  2754. /* There is nothing for the blocked_rdev to block */
  2755. rdev_dec_pending(blocked_rdev, conf->mddev);
  2756. blocked_rdev = NULL;
  2757. }
  2758. pr_debug("locked=%d uptodate=%d to_read=%d"
  2759. " to_write=%d failed=%d failed_num=%d,%d\n",
  2760. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  2761. r6s.failed_num[0], r6s.failed_num[1]);
  2762. /* check if the array has lost >2 devices and, if so, some requests
  2763. * might need to be failed
  2764. */
  2765. if (s.failed > 2 && s.to_read+s.to_write+s.written)
  2766. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  2767. if (s.failed > 2 && s.syncing) {
  2768. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2769. clear_bit(STRIPE_SYNCING, &sh->state);
  2770. s.syncing = 0;
  2771. }
  2772. /*
  2773. * might be able to return some write requests if the parity blocks
  2774. * are safe, or on a failed drive
  2775. */
  2776. pdev = &sh->dev[pd_idx];
  2777. r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
  2778. || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
  2779. qdev = &sh->dev[qd_idx];
  2780. r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == qd_idx)
  2781. || (s.failed >= 2 && r6s.failed_num[1] == qd_idx);
  2782. if ( s.written &&
  2783. ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  2784. && !test_bit(R5_LOCKED, &pdev->flags)
  2785. && test_bit(R5_UPTODATE, &pdev->flags)))) &&
  2786. ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  2787. && !test_bit(R5_LOCKED, &qdev->flags)
  2788. && test_bit(R5_UPTODATE, &qdev->flags)))))
  2789. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  2790. /* Now we might consider reading some blocks, either to check/generate
  2791. * parity, or to satisfy requests
  2792. * or to load a block that is being partially written.
  2793. */
  2794. if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
  2795. (s.syncing && (s.uptodate < disks)) || s.expanding)
  2796. handle_stripe_fill6(sh, &s, &r6s, disks);
  2797. /* now to consider writing and what else, if anything should be read */
  2798. if (s.to_write)
  2799. handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
  2800. /* maybe we need to check and possibly fix the parity for this stripe
  2801. * Any reads will already have been scheduled, so we just see if enough
  2802. * data is available
  2803. */
  2804. if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
  2805. handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
  2806. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2807. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2808. clear_bit(STRIPE_SYNCING, &sh->state);
  2809. }
  2810. /* If the failed drives are just a ReadError, then we might need
  2811. * to progress the repair/check process
  2812. */
  2813. if (s.failed <= 2 && !conf->mddev->ro)
  2814. for (i = 0; i < s.failed; i++) {
  2815. dev = &sh->dev[r6s.failed_num[i]];
  2816. if (test_bit(R5_ReadError, &dev->flags)
  2817. && !test_bit(R5_LOCKED, &dev->flags)
  2818. && test_bit(R5_UPTODATE, &dev->flags)
  2819. ) {
  2820. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2821. set_bit(R5_Wantwrite, &dev->flags);
  2822. set_bit(R5_ReWrite, &dev->flags);
  2823. set_bit(R5_LOCKED, &dev->flags);
  2824. } else {
  2825. /* let's read it back */
  2826. set_bit(R5_Wantread, &dev->flags);
  2827. set_bit(R5_LOCKED, &dev->flags);
  2828. }
  2829. }
  2830. }
  2831. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  2832. struct stripe_head *sh2
  2833. = get_active_stripe(conf, sh->sector, 1, 1);
  2834. if (sh2 && test_bit(STRIPE_EXPAND_SOURCE, &sh2->state)) {
  2835. /* sh cannot be written until sh2 has been read.
  2836. * so arrange for sh to be delayed a little
  2837. */
  2838. set_bit(STRIPE_DELAYED, &sh->state);
  2839. set_bit(STRIPE_HANDLE, &sh->state);
  2840. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
  2841. &sh2->state))
  2842. atomic_inc(&conf->preread_active_stripes);
  2843. release_stripe(sh2);
  2844. goto unlock;
  2845. }
  2846. if (sh2)
  2847. release_stripe(sh2);
  2848. /* Need to write out all blocks after computing P&Q */
  2849. sh->disks = conf->raid_disks;
  2850. stripe_set_idx(sh->sector, conf, 0, sh);
  2851. compute_parity6(sh, RECONSTRUCT_WRITE);
  2852. for (i = conf->raid_disks ; i-- ; ) {
  2853. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2854. s.locked++;
  2855. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2856. }
  2857. clear_bit(STRIPE_EXPANDING, &sh->state);
  2858. } else if (s.expanded) {
  2859. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2860. atomic_dec(&conf->reshape_stripes);
  2861. wake_up(&conf->wait_for_overlap);
  2862. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2863. }
  2864. if (s.expanding && s.locked == 0 &&
  2865. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  2866. handle_stripe_expansion(conf, sh, &r6s);
  2867. unlock:
  2868. spin_unlock(&sh->lock);
  2869. /* wait for this device to become unblocked */
  2870. if (unlikely(blocked_rdev))
  2871. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  2872. ops_run_io(sh, &s);
  2873. return_io(return_bi);
  2874. return blocked_rdev == NULL;
  2875. }
  2876. /* returns true if the stripe was handled */
  2877. static bool handle_stripe(struct stripe_head *sh, struct page *tmp_page)
  2878. {
  2879. if (sh->raid_conf->level == 6)
  2880. return handle_stripe6(sh, tmp_page);
  2881. else
  2882. return handle_stripe5(sh);
  2883. }
  2884. static void raid5_activate_delayed(raid5_conf_t *conf)
  2885. {
  2886. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  2887. while (!list_empty(&conf->delayed_list)) {
  2888. struct list_head *l = conf->delayed_list.next;
  2889. struct stripe_head *sh;
  2890. sh = list_entry(l, struct stripe_head, lru);
  2891. list_del_init(l);
  2892. clear_bit(STRIPE_DELAYED, &sh->state);
  2893. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2894. atomic_inc(&conf->preread_active_stripes);
  2895. list_add_tail(&sh->lru, &conf->hold_list);
  2896. }
  2897. } else
  2898. blk_plug_device(conf->mddev->queue);
  2899. }
  2900. static void activate_bit_delay(raid5_conf_t *conf)
  2901. {
  2902. /* device_lock is held */
  2903. struct list_head head;
  2904. list_add(&head, &conf->bitmap_list);
  2905. list_del_init(&conf->bitmap_list);
  2906. while (!list_empty(&head)) {
  2907. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  2908. list_del_init(&sh->lru);
  2909. atomic_inc(&sh->count);
  2910. __release_stripe(conf, sh);
  2911. }
  2912. }
  2913. static void unplug_slaves(mddev_t *mddev)
  2914. {
  2915. raid5_conf_t *conf = mddev_to_conf(mddev);
  2916. int i;
  2917. rcu_read_lock();
  2918. for (i=0; i<mddev->raid_disks; i++) {
  2919. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2920. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  2921. struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
  2922. atomic_inc(&rdev->nr_pending);
  2923. rcu_read_unlock();
  2924. blk_unplug(r_queue);
  2925. rdev_dec_pending(rdev, mddev);
  2926. rcu_read_lock();
  2927. }
  2928. }
  2929. rcu_read_unlock();
  2930. }
  2931. static void raid5_unplug_device(struct request_queue *q)
  2932. {
  2933. mddev_t *mddev = q->queuedata;
  2934. raid5_conf_t *conf = mddev_to_conf(mddev);
  2935. unsigned long flags;
  2936. spin_lock_irqsave(&conf->device_lock, flags);
  2937. if (blk_remove_plug(q)) {
  2938. conf->seq_flush++;
  2939. raid5_activate_delayed(conf);
  2940. }
  2941. md_wakeup_thread(mddev->thread);
  2942. spin_unlock_irqrestore(&conf->device_lock, flags);
  2943. unplug_slaves(mddev);
  2944. }
  2945. static int raid5_congested(void *data, int bits)
  2946. {
  2947. mddev_t *mddev = data;
  2948. raid5_conf_t *conf = mddev_to_conf(mddev);
  2949. /* No difference between reads and writes. Just check
  2950. * how busy the stripe_cache is
  2951. */
  2952. if (conf->inactive_blocked)
  2953. return 1;
  2954. if (conf->quiesce)
  2955. return 1;
  2956. if (list_empty_careful(&conf->inactive_list))
  2957. return 1;
  2958. return 0;
  2959. }
  2960. /* We want read requests to align with chunks where possible,
  2961. * but write requests don't need to.
  2962. */
  2963. static int raid5_mergeable_bvec(struct request_queue *q,
  2964. struct bvec_merge_data *bvm,
  2965. struct bio_vec *biovec)
  2966. {
  2967. mddev_t *mddev = q->queuedata;
  2968. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  2969. int max;
  2970. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2971. unsigned int bio_sectors = bvm->bi_size >> 9;
  2972. if ((bvm->bi_rw & 1) == WRITE)
  2973. return biovec->bv_len; /* always allow writes to be mergeable */
  2974. if (mddev->new_chunk < mddev->chunk_size)
  2975. chunk_sectors = mddev->new_chunk >> 9;
  2976. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  2977. if (max < 0) max = 0;
  2978. if (max <= biovec->bv_len && bio_sectors == 0)
  2979. return biovec->bv_len;
  2980. else
  2981. return max;
  2982. }
  2983. static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
  2984. {
  2985. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2986. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2987. unsigned int bio_sectors = bio->bi_size >> 9;
  2988. if (mddev->new_chunk < mddev->chunk_size)
  2989. chunk_sectors = mddev->new_chunk >> 9;
  2990. return chunk_sectors >=
  2991. ((sector & (chunk_sectors - 1)) + bio_sectors);
  2992. }
  2993. /*
  2994. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  2995. * later sampled by raid5d.
  2996. */
  2997. static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
  2998. {
  2999. unsigned long flags;
  3000. spin_lock_irqsave(&conf->device_lock, flags);
  3001. bi->bi_next = conf->retry_read_aligned_list;
  3002. conf->retry_read_aligned_list = bi;
  3003. spin_unlock_irqrestore(&conf->device_lock, flags);
  3004. md_wakeup_thread(conf->mddev->thread);
  3005. }
  3006. static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
  3007. {
  3008. struct bio *bi;
  3009. bi = conf->retry_read_aligned;
  3010. if (bi) {
  3011. conf->retry_read_aligned = NULL;
  3012. return bi;
  3013. }
  3014. bi = conf->retry_read_aligned_list;
  3015. if(bi) {
  3016. conf->retry_read_aligned_list = bi->bi_next;
  3017. bi->bi_next = NULL;
  3018. /*
  3019. * this sets the active strip count to 1 and the processed
  3020. * strip count to zero (upper 8 bits)
  3021. */
  3022. bi->bi_phys_segments = 1; /* biased count of active stripes */
  3023. }
  3024. return bi;
  3025. }
  3026. /*
  3027. * The "raid5_align_endio" should check if the read succeeded and if it
  3028. * did, call bio_endio on the original bio (having bio_put the new bio
  3029. * first).
  3030. * If the read failed..
  3031. */
  3032. static void raid5_align_endio(struct bio *bi, int error)
  3033. {
  3034. struct bio* raid_bi = bi->bi_private;
  3035. mddev_t *mddev;
  3036. raid5_conf_t *conf;
  3037. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  3038. mdk_rdev_t *rdev;
  3039. bio_put(bi);
  3040. mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
  3041. conf = mddev_to_conf(mddev);
  3042. rdev = (void*)raid_bi->bi_next;
  3043. raid_bi->bi_next = NULL;
  3044. rdev_dec_pending(rdev, conf->mddev);
  3045. if (!error && uptodate) {
  3046. bio_endio(raid_bi, 0);
  3047. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3048. wake_up(&conf->wait_for_stripe);
  3049. return;
  3050. }
  3051. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  3052. add_bio_to_retry(raid_bi, conf);
  3053. }
  3054. static int bio_fits_rdev(struct bio *bi)
  3055. {
  3056. struct request_queue *q = bdev_get_queue(bi->bi_bdev);
  3057. if ((bi->bi_size>>9) > q->max_sectors)
  3058. return 0;
  3059. blk_recount_segments(q, bi);
  3060. if (bi->bi_phys_segments > q->max_phys_segments)
  3061. return 0;
  3062. if (q->merge_bvec_fn)
  3063. /* it's too hard to apply the merge_bvec_fn at this stage,
  3064. * just just give up
  3065. */
  3066. return 0;
  3067. return 1;
  3068. }
  3069. static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
  3070. {
  3071. mddev_t *mddev = q->queuedata;
  3072. raid5_conf_t *conf = mddev_to_conf(mddev);
  3073. unsigned int dd_idx;
  3074. struct bio* align_bi;
  3075. mdk_rdev_t *rdev;
  3076. if (!in_chunk_boundary(mddev, raid_bio)) {
  3077. pr_debug("chunk_aligned_read : non aligned\n");
  3078. return 0;
  3079. }
  3080. /*
  3081. * use bio_clone to make a copy of the bio
  3082. */
  3083. align_bi = bio_clone(raid_bio, GFP_NOIO);
  3084. if (!align_bi)
  3085. return 0;
  3086. /*
  3087. * set bi_end_io to a new function, and set bi_private to the
  3088. * original bio.
  3089. */
  3090. align_bi->bi_end_io = raid5_align_endio;
  3091. align_bi->bi_private = raid_bio;
  3092. /*
  3093. * compute position
  3094. */
  3095. align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
  3096. 0,
  3097. &dd_idx, NULL);
  3098. rcu_read_lock();
  3099. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  3100. if (rdev && test_bit(In_sync, &rdev->flags)) {
  3101. atomic_inc(&rdev->nr_pending);
  3102. rcu_read_unlock();
  3103. raid_bio->bi_next = (void*)rdev;
  3104. align_bi->bi_bdev = rdev->bdev;
  3105. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  3106. align_bi->bi_sector += rdev->data_offset;
  3107. if (!bio_fits_rdev(align_bi)) {
  3108. /* too big in some way */
  3109. bio_put(align_bi);
  3110. rdev_dec_pending(rdev, mddev);
  3111. return 0;
  3112. }
  3113. spin_lock_irq(&conf->device_lock);
  3114. wait_event_lock_irq(conf->wait_for_stripe,
  3115. conf->quiesce == 0,
  3116. conf->device_lock, /* nothing */);
  3117. atomic_inc(&conf->active_aligned_reads);
  3118. spin_unlock_irq(&conf->device_lock);
  3119. generic_make_request(align_bi);
  3120. return 1;
  3121. } else {
  3122. rcu_read_unlock();
  3123. bio_put(align_bi);
  3124. return 0;
  3125. }
  3126. }
  3127. /* __get_priority_stripe - get the next stripe to process
  3128. *
  3129. * Full stripe writes are allowed to pass preread active stripes up until
  3130. * the bypass_threshold is exceeded. In general the bypass_count
  3131. * increments when the handle_list is handled before the hold_list; however, it
  3132. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  3133. * stripe with in flight i/o. The bypass_count will be reset when the
  3134. * head of the hold_list has changed, i.e. the head was promoted to the
  3135. * handle_list.
  3136. */
  3137. static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
  3138. {
  3139. struct stripe_head *sh;
  3140. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  3141. __func__,
  3142. list_empty(&conf->handle_list) ? "empty" : "busy",
  3143. list_empty(&conf->hold_list) ? "empty" : "busy",
  3144. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  3145. if (!list_empty(&conf->handle_list)) {
  3146. sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
  3147. if (list_empty(&conf->hold_list))
  3148. conf->bypass_count = 0;
  3149. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  3150. if (conf->hold_list.next == conf->last_hold)
  3151. conf->bypass_count++;
  3152. else {
  3153. conf->last_hold = conf->hold_list.next;
  3154. conf->bypass_count -= conf->bypass_threshold;
  3155. if (conf->bypass_count < 0)
  3156. conf->bypass_count = 0;
  3157. }
  3158. }
  3159. } else if (!list_empty(&conf->hold_list) &&
  3160. ((conf->bypass_threshold &&
  3161. conf->bypass_count > conf->bypass_threshold) ||
  3162. atomic_read(&conf->pending_full_writes) == 0)) {
  3163. sh = list_entry(conf->hold_list.next,
  3164. typeof(*sh), lru);
  3165. conf->bypass_count -= conf->bypass_threshold;
  3166. if (conf->bypass_count < 0)
  3167. conf->bypass_count = 0;
  3168. } else
  3169. return NULL;
  3170. list_del_init(&sh->lru);
  3171. atomic_inc(&sh->count);
  3172. BUG_ON(atomic_read(&sh->count) != 1);
  3173. return sh;
  3174. }
  3175. static int make_request(struct request_queue *q, struct bio * bi)
  3176. {
  3177. mddev_t *mddev = q->queuedata;
  3178. raid5_conf_t *conf = mddev_to_conf(mddev);
  3179. int dd_idx;
  3180. sector_t new_sector;
  3181. sector_t logical_sector, last_sector;
  3182. struct stripe_head *sh;
  3183. const int rw = bio_data_dir(bi);
  3184. int cpu, remaining;
  3185. if (unlikely(bio_barrier(bi))) {
  3186. bio_endio(bi, -EOPNOTSUPP);
  3187. return 0;
  3188. }
  3189. md_write_start(mddev, bi);
  3190. cpu = part_stat_lock();
  3191. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  3192. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  3193. bio_sectors(bi));
  3194. part_stat_unlock();
  3195. if (rw == READ &&
  3196. mddev->reshape_position == MaxSector &&
  3197. chunk_aligned_read(q,bi))
  3198. return 0;
  3199. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3200. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3201. bi->bi_next = NULL;
  3202. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3203. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  3204. DEFINE_WAIT(w);
  3205. int disks, data_disks;
  3206. int previous;
  3207. retry:
  3208. previous = 0;
  3209. disks = conf->raid_disks;
  3210. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  3211. if (unlikely(conf->reshape_progress != MaxSector)) {
  3212. /* spinlock is needed as reshape_progress may be
  3213. * 64bit on a 32bit platform, and so it might be
  3214. * possible to see a half-updated value
  3215. * Ofcourse reshape_progress could change after
  3216. * the lock is dropped, so once we get a reference
  3217. * to the stripe that we think it is, we will have
  3218. * to check again.
  3219. */
  3220. spin_lock_irq(&conf->device_lock);
  3221. if (mddev->delta_disks < 0
  3222. ? logical_sector < conf->reshape_progress
  3223. : logical_sector >= conf->reshape_progress) {
  3224. disks = conf->previous_raid_disks;
  3225. previous = 1;
  3226. } else {
  3227. if (mddev->delta_disks < 0
  3228. ? logical_sector < conf->reshape_safe
  3229. : logical_sector >= conf->reshape_safe) {
  3230. spin_unlock_irq(&conf->device_lock);
  3231. schedule();
  3232. goto retry;
  3233. }
  3234. }
  3235. spin_unlock_irq(&conf->device_lock);
  3236. }
  3237. data_disks = disks - conf->max_degraded;
  3238. new_sector = raid5_compute_sector(conf, logical_sector,
  3239. previous,
  3240. &dd_idx, NULL);
  3241. pr_debug("raid5: make_request, sector %llu logical %llu\n",
  3242. (unsigned long long)new_sector,
  3243. (unsigned long long)logical_sector);
  3244. sh = get_active_stripe(conf, new_sector, previous,
  3245. (bi->bi_rw&RWA_MASK));
  3246. if (sh) {
  3247. if (unlikely(previous)) {
  3248. /* expansion might have moved on while waiting for a
  3249. * stripe, so we must do the range check again.
  3250. * Expansion could still move past after this
  3251. * test, but as we are holding a reference to
  3252. * 'sh', we know that if that happens,
  3253. * STRIPE_EXPANDING will get set and the expansion
  3254. * won't proceed until we finish with the stripe.
  3255. */
  3256. int must_retry = 0;
  3257. spin_lock_irq(&conf->device_lock);
  3258. if (mddev->delta_disks < 0
  3259. ? logical_sector >= conf->reshape_progress
  3260. : logical_sector < conf->reshape_progress)
  3261. /* mismatch, need to try again */
  3262. must_retry = 1;
  3263. spin_unlock_irq(&conf->device_lock);
  3264. if (must_retry) {
  3265. release_stripe(sh);
  3266. goto retry;
  3267. }
  3268. }
  3269. /* FIXME what if we get a false positive because these
  3270. * are being updated.
  3271. */
  3272. if (logical_sector >= mddev->suspend_lo &&
  3273. logical_sector < mddev->suspend_hi) {
  3274. release_stripe(sh);
  3275. schedule();
  3276. goto retry;
  3277. }
  3278. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  3279. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  3280. /* Stripe is busy expanding or
  3281. * add failed due to overlap. Flush everything
  3282. * and wait a while
  3283. */
  3284. raid5_unplug_device(mddev->queue);
  3285. release_stripe(sh);
  3286. schedule();
  3287. goto retry;
  3288. }
  3289. finish_wait(&conf->wait_for_overlap, &w);
  3290. set_bit(STRIPE_HANDLE, &sh->state);
  3291. clear_bit(STRIPE_DELAYED, &sh->state);
  3292. release_stripe(sh);
  3293. } else {
  3294. /* cannot get stripe for read-ahead, just give-up */
  3295. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  3296. finish_wait(&conf->wait_for_overlap, &w);
  3297. break;
  3298. }
  3299. }
  3300. spin_lock_irq(&conf->device_lock);
  3301. remaining = raid5_dec_bi_phys_segments(bi);
  3302. spin_unlock_irq(&conf->device_lock);
  3303. if (remaining == 0) {
  3304. if ( rw == WRITE )
  3305. md_write_end(mddev);
  3306. bio_endio(bi, 0);
  3307. }
  3308. return 0;
  3309. }
  3310. static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
  3311. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  3312. {
  3313. /* reshaping is quite different to recovery/resync so it is
  3314. * handled quite separately ... here.
  3315. *
  3316. * On each call to sync_request, we gather one chunk worth of
  3317. * destination stripes and flag them as expanding.
  3318. * Then we find all the source stripes and request reads.
  3319. * As the reads complete, handle_stripe will copy the data
  3320. * into the destination stripe and release that stripe.
  3321. */
  3322. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3323. struct stripe_head *sh;
  3324. sector_t first_sector, last_sector;
  3325. int raid_disks = conf->previous_raid_disks;
  3326. int data_disks = raid_disks - conf->max_degraded;
  3327. int new_data_disks = conf->raid_disks - conf->max_degraded;
  3328. int i;
  3329. int dd_idx;
  3330. sector_t writepos, readpos, safepos;
  3331. sector_t stripe_addr;
  3332. int reshape_sectors;
  3333. struct list_head stripes;
  3334. if (sector_nr == 0) {
  3335. /* If restarting in the middle, skip the initial sectors */
  3336. if (mddev->delta_disks < 0 &&
  3337. conf->reshape_progress < raid5_size(mddev, 0, 0)) {
  3338. sector_nr = raid5_size(mddev, 0, 0)
  3339. - conf->reshape_progress;
  3340. } else if (mddev->delta_disks > 0 &&
  3341. conf->reshape_progress > 0)
  3342. sector_nr = conf->reshape_progress;
  3343. sector_div(sector_nr, new_data_disks);
  3344. if (sector_nr) {
  3345. *skipped = 1;
  3346. return sector_nr;
  3347. }
  3348. }
  3349. /* We need to process a full chunk at a time.
  3350. * If old and new chunk sizes differ, we need to process the
  3351. * largest of these
  3352. */
  3353. if (mddev->new_chunk > mddev->chunk_size)
  3354. reshape_sectors = mddev->new_chunk / 512;
  3355. else
  3356. reshape_sectors = mddev->chunk_size / 512;
  3357. /* we update the metadata when there is more than 3Meg
  3358. * in the block range (that is rather arbitrary, should
  3359. * probably be time based) or when the data about to be
  3360. * copied would over-write the source of the data at
  3361. * the front of the range.
  3362. * i.e. one new_stripe along from reshape_progress new_maps
  3363. * to after where reshape_safe old_maps to
  3364. */
  3365. writepos = conf->reshape_progress;
  3366. sector_div(writepos, new_data_disks);
  3367. readpos = conf->reshape_progress;
  3368. sector_div(readpos, data_disks);
  3369. safepos = conf->reshape_safe;
  3370. sector_div(safepos, data_disks);
  3371. if (mddev->delta_disks < 0) {
  3372. writepos -= reshape_sectors;
  3373. readpos += reshape_sectors;
  3374. safepos += reshape_sectors;
  3375. } else {
  3376. writepos += reshape_sectors;
  3377. readpos -= reshape_sectors;
  3378. safepos -= reshape_sectors;
  3379. }
  3380. /* 'writepos' is the most advanced device address we might write.
  3381. * 'readpos' is the least advanced device address we might read.
  3382. * 'safepos' is the least address recorded in the metadata as having
  3383. * been reshaped.
  3384. * If 'readpos' is behind 'writepos', then there is no way that we can
  3385. * ensure safety in the face of a crash - that must be done by userspace
  3386. * making a backup of the data. So in that case there is no particular
  3387. * rush to update metadata.
  3388. * Otherwise if 'safepos' is behind 'writepos', then we really need to
  3389. * update the metadata to advance 'safepos' to match 'readpos' so that
  3390. * we can be safe in the event of a crash.
  3391. * So we insist on updating metadata if safepos is behind writepos and
  3392. * readpos is beyond writepos.
  3393. * In any case, update the metadata every 10 seconds.
  3394. * Maybe that number should be configurable, but I'm not sure it is
  3395. * worth it.... maybe it could be a multiple of safemode_delay???
  3396. */
  3397. if ((mddev->delta_disks < 0
  3398. ? (safepos > writepos && readpos < writepos)
  3399. : (safepos < writepos && readpos > writepos)) ||
  3400. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  3401. /* Cannot proceed until we've updated the superblock... */
  3402. wait_event(conf->wait_for_overlap,
  3403. atomic_read(&conf->reshape_stripes)==0);
  3404. mddev->reshape_position = conf->reshape_progress;
  3405. conf->reshape_checkpoint = jiffies;
  3406. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3407. md_wakeup_thread(mddev->thread);
  3408. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3409. kthread_should_stop());
  3410. spin_lock_irq(&conf->device_lock);
  3411. conf->reshape_safe = mddev->reshape_position;
  3412. spin_unlock_irq(&conf->device_lock);
  3413. wake_up(&conf->wait_for_overlap);
  3414. }
  3415. if (mddev->delta_disks < 0) {
  3416. BUG_ON(conf->reshape_progress == 0);
  3417. stripe_addr = writepos;
  3418. BUG_ON((mddev->dev_sectors &
  3419. ~((sector_t)reshape_sectors - 1))
  3420. - reshape_sectors - stripe_addr
  3421. != sector_nr);
  3422. } else {
  3423. BUG_ON(writepos != sector_nr + reshape_sectors);
  3424. stripe_addr = sector_nr;
  3425. }
  3426. INIT_LIST_HEAD(&stripes);
  3427. for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
  3428. int j;
  3429. int skipped = 0;
  3430. sh = get_active_stripe(conf, stripe_addr+i, 0, 0);
  3431. set_bit(STRIPE_EXPANDING, &sh->state);
  3432. atomic_inc(&conf->reshape_stripes);
  3433. /* If any of this stripe is beyond the end of the old
  3434. * array, then we need to zero those blocks
  3435. */
  3436. for (j=sh->disks; j--;) {
  3437. sector_t s;
  3438. if (j == sh->pd_idx)
  3439. continue;
  3440. if (conf->level == 6 &&
  3441. j == sh->qd_idx)
  3442. continue;
  3443. s = compute_blocknr(sh, j, 0);
  3444. if (s < raid5_size(mddev, 0, 0)) {
  3445. skipped = 1;
  3446. continue;
  3447. }
  3448. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  3449. set_bit(R5_Expanded, &sh->dev[j].flags);
  3450. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  3451. }
  3452. if (!skipped) {
  3453. set_bit(STRIPE_EXPAND_READY, &sh->state);
  3454. set_bit(STRIPE_HANDLE, &sh->state);
  3455. }
  3456. list_add(&sh->lru, &stripes);
  3457. }
  3458. spin_lock_irq(&conf->device_lock);
  3459. if (mddev->delta_disks < 0)
  3460. conf->reshape_progress -= reshape_sectors * new_data_disks;
  3461. else
  3462. conf->reshape_progress += reshape_sectors * new_data_disks;
  3463. spin_unlock_irq(&conf->device_lock);
  3464. /* Ok, those stripe are ready. We can start scheduling
  3465. * reads on the source stripes.
  3466. * The source stripes are determined by mapping the first and last
  3467. * block on the destination stripes.
  3468. */
  3469. first_sector =
  3470. raid5_compute_sector(conf, stripe_addr*(new_data_disks),
  3471. 1, &dd_idx, NULL);
  3472. last_sector =
  3473. raid5_compute_sector(conf, ((stripe_addr+conf->chunk_size/512)
  3474. *(new_data_disks) - 1),
  3475. 1, &dd_idx, NULL);
  3476. if (last_sector >= mddev->dev_sectors)
  3477. last_sector = mddev->dev_sectors - 1;
  3478. while (first_sector <= last_sector) {
  3479. sh = get_active_stripe(conf, first_sector, 1, 0);
  3480. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3481. set_bit(STRIPE_HANDLE, &sh->state);
  3482. release_stripe(sh);
  3483. first_sector += STRIPE_SECTORS;
  3484. }
  3485. /* Now that the sources are clearly marked, we can release
  3486. * the destination stripes
  3487. */
  3488. while (!list_empty(&stripes)) {
  3489. sh = list_entry(stripes.next, struct stripe_head, lru);
  3490. list_del_init(&sh->lru);
  3491. release_stripe(sh);
  3492. }
  3493. /* If this takes us to the resync_max point where we have to pause,
  3494. * then we need to write out the superblock.
  3495. */
  3496. sector_nr += reshape_sectors;
  3497. if (sector_nr >= mddev->resync_max) {
  3498. /* Cannot proceed until we've updated the superblock... */
  3499. wait_event(conf->wait_for_overlap,
  3500. atomic_read(&conf->reshape_stripes) == 0);
  3501. mddev->reshape_position = conf->reshape_progress;
  3502. conf->reshape_checkpoint = jiffies;
  3503. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3504. md_wakeup_thread(mddev->thread);
  3505. wait_event(mddev->sb_wait,
  3506. !test_bit(MD_CHANGE_DEVS, &mddev->flags)
  3507. || kthread_should_stop());
  3508. spin_lock_irq(&conf->device_lock);
  3509. conf->reshape_safe = mddev->reshape_position;
  3510. spin_unlock_irq(&conf->device_lock);
  3511. wake_up(&conf->wait_for_overlap);
  3512. }
  3513. return reshape_sectors;
  3514. }
  3515. /* FIXME go_faster isn't used */
  3516. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  3517. {
  3518. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3519. struct stripe_head *sh;
  3520. sector_t max_sector = mddev->dev_sectors;
  3521. int sync_blocks;
  3522. int still_degraded = 0;
  3523. int i;
  3524. if (sector_nr >= max_sector) {
  3525. /* just being told to finish up .. nothing much to do */
  3526. unplug_slaves(mddev);
  3527. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  3528. end_reshape(conf);
  3529. return 0;
  3530. }
  3531. if (mddev->curr_resync < max_sector) /* aborted */
  3532. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  3533. &sync_blocks, 1);
  3534. else /* completed sync */
  3535. conf->fullsync = 0;
  3536. bitmap_close_sync(mddev->bitmap);
  3537. return 0;
  3538. }
  3539. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  3540. return reshape_request(mddev, sector_nr, skipped);
  3541. /* No need to check resync_max as we never do more than one
  3542. * stripe, and as resync_max will always be on a chunk boundary,
  3543. * if the check in md_do_sync didn't fire, there is no chance
  3544. * of overstepping resync_max here
  3545. */
  3546. /* if there is too many failed drives and we are trying
  3547. * to resync, then assert that we are finished, because there is
  3548. * nothing we can do.
  3549. */
  3550. if (mddev->degraded >= conf->max_degraded &&
  3551. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  3552. sector_t rv = mddev->dev_sectors - sector_nr;
  3553. *skipped = 1;
  3554. return rv;
  3555. }
  3556. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  3557. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  3558. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  3559. /* we can skip this block, and probably more */
  3560. sync_blocks /= STRIPE_SECTORS;
  3561. *skipped = 1;
  3562. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  3563. }
  3564. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  3565. sh = get_active_stripe(conf, sector_nr, 0, 1);
  3566. if (sh == NULL) {
  3567. sh = get_active_stripe(conf, sector_nr, 0, 0);
  3568. /* make sure we don't swamp the stripe cache if someone else
  3569. * is trying to get access
  3570. */
  3571. schedule_timeout_uninterruptible(1);
  3572. }
  3573. /* Need to check if array will still be degraded after recovery/resync
  3574. * We don't need to check the 'failed' flag as when that gets set,
  3575. * recovery aborts.
  3576. */
  3577. for (i=0; i<mddev->raid_disks; i++)
  3578. if (conf->disks[i].rdev == NULL)
  3579. still_degraded = 1;
  3580. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  3581. spin_lock(&sh->lock);
  3582. set_bit(STRIPE_SYNCING, &sh->state);
  3583. clear_bit(STRIPE_INSYNC, &sh->state);
  3584. spin_unlock(&sh->lock);
  3585. /* wait for any blocked device to be handled */
  3586. while(unlikely(!handle_stripe(sh, NULL)))
  3587. ;
  3588. release_stripe(sh);
  3589. return STRIPE_SECTORS;
  3590. }
  3591. static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
  3592. {
  3593. /* We may not be able to submit a whole bio at once as there
  3594. * may not be enough stripe_heads available.
  3595. * We cannot pre-allocate enough stripe_heads as we may need
  3596. * more than exist in the cache (if we allow ever large chunks).
  3597. * So we do one stripe head at a time and record in
  3598. * ->bi_hw_segments how many have been done.
  3599. *
  3600. * We *know* that this entire raid_bio is in one chunk, so
  3601. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  3602. */
  3603. struct stripe_head *sh;
  3604. int dd_idx;
  3605. sector_t sector, logical_sector, last_sector;
  3606. int scnt = 0;
  3607. int remaining;
  3608. int handled = 0;
  3609. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3610. sector = raid5_compute_sector(conf, logical_sector,
  3611. 0, &dd_idx, NULL);
  3612. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  3613. for (; logical_sector < last_sector;
  3614. logical_sector += STRIPE_SECTORS,
  3615. sector += STRIPE_SECTORS,
  3616. scnt++) {
  3617. if (scnt < raid5_bi_hw_segments(raid_bio))
  3618. /* already done this stripe */
  3619. continue;
  3620. sh = get_active_stripe(conf, sector, 0, 1);
  3621. if (!sh) {
  3622. /* failed to get a stripe - must wait */
  3623. raid5_set_bi_hw_segments(raid_bio, scnt);
  3624. conf->retry_read_aligned = raid_bio;
  3625. return handled;
  3626. }
  3627. set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
  3628. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  3629. release_stripe(sh);
  3630. raid5_set_bi_hw_segments(raid_bio, scnt);
  3631. conf->retry_read_aligned = raid_bio;
  3632. return handled;
  3633. }
  3634. handle_stripe(sh, NULL);
  3635. release_stripe(sh);
  3636. handled++;
  3637. }
  3638. spin_lock_irq(&conf->device_lock);
  3639. remaining = raid5_dec_bi_phys_segments(raid_bio);
  3640. spin_unlock_irq(&conf->device_lock);
  3641. if (remaining == 0)
  3642. bio_endio(raid_bio, 0);
  3643. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3644. wake_up(&conf->wait_for_stripe);
  3645. return handled;
  3646. }
  3647. /*
  3648. * This is our raid5 kernel thread.
  3649. *
  3650. * We scan the hash table for stripes which can be handled now.
  3651. * During the scan, completed stripes are saved for us by the interrupt
  3652. * handler, so that they will not have to wait for our next wakeup.
  3653. */
  3654. static void raid5d(mddev_t *mddev)
  3655. {
  3656. struct stripe_head *sh;
  3657. raid5_conf_t *conf = mddev_to_conf(mddev);
  3658. int handled;
  3659. pr_debug("+++ raid5d active\n");
  3660. md_check_recovery(mddev);
  3661. handled = 0;
  3662. spin_lock_irq(&conf->device_lock);
  3663. while (1) {
  3664. struct bio *bio;
  3665. if (conf->seq_flush != conf->seq_write) {
  3666. int seq = conf->seq_flush;
  3667. spin_unlock_irq(&conf->device_lock);
  3668. bitmap_unplug(mddev->bitmap);
  3669. spin_lock_irq(&conf->device_lock);
  3670. conf->seq_write = seq;
  3671. activate_bit_delay(conf);
  3672. }
  3673. while ((bio = remove_bio_from_retry(conf))) {
  3674. int ok;
  3675. spin_unlock_irq(&conf->device_lock);
  3676. ok = retry_aligned_read(conf, bio);
  3677. spin_lock_irq(&conf->device_lock);
  3678. if (!ok)
  3679. break;
  3680. handled++;
  3681. }
  3682. sh = __get_priority_stripe(conf);
  3683. if (!sh)
  3684. break;
  3685. spin_unlock_irq(&conf->device_lock);
  3686. handled++;
  3687. handle_stripe(sh, conf->spare_page);
  3688. release_stripe(sh);
  3689. spin_lock_irq(&conf->device_lock);
  3690. }
  3691. pr_debug("%d stripes handled\n", handled);
  3692. spin_unlock_irq(&conf->device_lock);
  3693. async_tx_issue_pending_all();
  3694. unplug_slaves(mddev);
  3695. pr_debug("--- raid5d inactive\n");
  3696. }
  3697. static ssize_t
  3698. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  3699. {
  3700. raid5_conf_t *conf = mddev_to_conf(mddev);
  3701. if (conf)
  3702. return sprintf(page, "%d\n", conf->max_nr_stripes);
  3703. else
  3704. return 0;
  3705. }
  3706. static ssize_t
  3707. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  3708. {
  3709. raid5_conf_t *conf = mddev_to_conf(mddev);
  3710. unsigned long new;
  3711. int err;
  3712. if (len >= PAGE_SIZE)
  3713. return -EINVAL;
  3714. if (!conf)
  3715. return -ENODEV;
  3716. if (strict_strtoul(page, 10, &new))
  3717. return -EINVAL;
  3718. if (new <= 16 || new > 32768)
  3719. return -EINVAL;
  3720. while (new < conf->max_nr_stripes) {
  3721. if (drop_one_stripe(conf))
  3722. conf->max_nr_stripes--;
  3723. else
  3724. break;
  3725. }
  3726. err = md_allow_write(mddev);
  3727. if (err)
  3728. return err;
  3729. while (new > conf->max_nr_stripes) {
  3730. if (grow_one_stripe(conf))
  3731. conf->max_nr_stripes++;
  3732. else break;
  3733. }
  3734. return len;
  3735. }
  3736. static struct md_sysfs_entry
  3737. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  3738. raid5_show_stripe_cache_size,
  3739. raid5_store_stripe_cache_size);
  3740. static ssize_t
  3741. raid5_show_preread_threshold(mddev_t *mddev, char *page)
  3742. {
  3743. raid5_conf_t *conf = mddev_to_conf(mddev);
  3744. if (conf)
  3745. return sprintf(page, "%d\n", conf->bypass_threshold);
  3746. else
  3747. return 0;
  3748. }
  3749. static ssize_t
  3750. raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
  3751. {
  3752. raid5_conf_t *conf = mddev_to_conf(mddev);
  3753. unsigned long new;
  3754. if (len >= PAGE_SIZE)
  3755. return -EINVAL;
  3756. if (!conf)
  3757. return -ENODEV;
  3758. if (strict_strtoul(page, 10, &new))
  3759. return -EINVAL;
  3760. if (new > conf->max_nr_stripes)
  3761. return -EINVAL;
  3762. conf->bypass_threshold = new;
  3763. return len;
  3764. }
  3765. static struct md_sysfs_entry
  3766. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  3767. S_IRUGO | S_IWUSR,
  3768. raid5_show_preread_threshold,
  3769. raid5_store_preread_threshold);
  3770. static ssize_t
  3771. stripe_cache_active_show(mddev_t *mddev, char *page)
  3772. {
  3773. raid5_conf_t *conf = mddev_to_conf(mddev);
  3774. if (conf)
  3775. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  3776. else
  3777. return 0;
  3778. }
  3779. static struct md_sysfs_entry
  3780. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  3781. static struct attribute *raid5_attrs[] = {
  3782. &raid5_stripecache_size.attr,
  3783. &raid5_stripecache_active.attr,
  3784. &raid5_preread_bypass_threshold.attr,
  3785. NULL,
  3786. };
  3787. static struct attribute_group raid5_attrs_group = {
  3788. .name = NULL,
  3789. .attrs = raid5_attrs,
  3790. };
  3791. static sector_t
  3792. raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  3793. {
  3794. raid5_conf_t *conf = mddev_to_conf(mddev);
  3795. if (!sectors)
  3796. sectors = mddev->dev_sectors;
  3797. if (!raid_disks) {
  3798. /* size is defined by the smallest of previous and new size */
  3799. if (conf->raid_disks < conf->previous_raid_disks)
  3800. raid_disks = conf->raid_disks;
  3801. else
  3802. raid_disks = conf->previous_raid_disks;
  3803. }
  3804. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  3805. sectors &= ~((sector_t)mddev->new_chunk/512 - 1);
  3806. return sectors * (raid_disks - conf->max_degraded);
  3807. }
  3808. static raid5_conf_t *setup_conf(mddev_t *mddev)
  3809. {
  3810. raid5_conf_t *conf;
  3811. int raid_disk, memory;
  3812. mdk_rdev_t *rdev;
  3813. struct disk_info *disk;
  3814. if (mddev->new_level != 5
  3815. && mddev->new_level != 4
  3816. && mddev->new_level != 6) {
  3817. printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
  3818. mdname(mddev), mddev->new_level);
  3819. return ERR_PTR(-EIO);
  3820. }
  3821. if ((mddev->new_level == 5
  3822. && !algorithm_valid_raid5(mddev->new_layout)) ||
  3823. (mddev->new_level == 6
  3824. && !algorithm_valid_raid6(mddev->new_layout))) {
  3825. printk(KERN_ERR "raid5: %s: layout %d not supported\n",
  3826. mdname(mddev), mddev->new_layout);
  3827. return ERR_PTR(-EIO);
  3828. }
  3829. if (mddev->new_level == 6 && mddev->raid_disks < 4) {
  3830. printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
  3831. mdname(mddev), mddev->raid_disks);
  3832. return ERR_PTR(-EINVAL);
  3833. }
  3834. if (!mddev->new_chunk || mddev->new_chunk % PAGE_SIZE) {
  3835. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  3836. mddev->new_chunk, mdname(mddev));
  3837. return ERR_PTR(-EINVAL);
  3838. }
  3839. conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
  3840. if (conf == NULL)
  3841. goto abort;
  3842. conf->raid_disks = mddev->raid_disks;
  3843. if (mddev->reshape_position == MaxSector)
  3844. conf->previous_raid_disks = mddev->raid_disks;
  3845. else
  3846. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  3847. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  3848. GFP_KERNEL);
  3849. if (!conf->disks)
  3850. goto abort;
  3851. conf->mddev = mddev;
  3852. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  3853. goto abort;
  3854. if (mddev->new_level == 6) {
  3855. conf->spare_page = alloc_page(GFP_KERNEL);
  3856. if (!conf->spare_page)
  3857. goto abort;
  3858. }
  3859. spin_lock_init(&conf->device_lock);
  3860. init_waitqueue_head(&conf->wait_for_stripe);
  3861. init_waitqueue_head(&conf->wait_for_overlap);
  3862. INIT_LIST_HEAD(&conf->handle_list);
  3863. INIT_LIST_HEAD(&conf->hold_list);
  3864. INIT_LIST_HEAD(&conf->delayed_list);
  3865. INIT_LIST_HEAD(&conf->bitmap_list);
  3866. INIT_LIST_HEAD(&conf->inactive_list);
  3867. atomic_set(&conf->active_stripes, 0);
  3868. atomic_set(&conf->preread_active_stripes, 0);
  3869. atomic_set(&conf->active_aligned_reads, 0);
  3870. conf->bypass_threshold = BYPASS_THRESHOLD;
  3871. pr_debug("raid5: run(%s) called.\n", mdname(mddev));
  3872. list_for_each_entry(rdev, &mddev->disks, same_set) {
  3873. raid_disk = rdev->raid_disk;
  3874. if (raid_disk >= conf->raid_disks
  3875. || raid_disk < 0)
  3876. continue;
  3877. disk = conf->disks + raid_disk;
  3878. disk->rdev = rdev;
  3879. if (test_bit(In_sync, &rdev->flags)) {
  3880. char b[BDEVNAME_SIZE];
  3881. printk(KERN_INFO "raid5: device %s operational as raid"
  3882. " disk %d\n", bdevname(rdev->bdev,b),
  3883. raid_disk);
  3884. } else
  3885. /* Cannot rely on bitmap to complete recovery */
  3886. conf->fullsync = 1;
  3887. }
  3888. conf->chunk_size = mddev->new_chunk;
  3889. conf->level = mddev->new_level;
  3890. if (conf->level == 6)
  3891. conf->max_degraded = 2;
  3892. else
  3893. conf->max_degraded = 1;
  3894. conf->algorithm = mddev->new_layout;
  3895. conf->max_nr_stripes = NR_STRIPES;
  3896. conf->reshape_progress = mddev->reshape_position;
  3897. if (conf->reshape_progress != MaxSector) {
  3898. conf->prev_chunk = mddev->chunk_size;
  3899. conf->prev_algo = mddev->layout;
  3900. }
  3901. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  3902. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  3903. if (grow_stripes(conf, conf->max_nr_stripes)) {
  3904. printk(KERN_ERR
  3905. "raid5: couldn't allocate %dkB for buffers\n", memory);
  3906. goto abort;
  3907. } else
  3908. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  3909. memory, mdname(mddev));
  3910. conf->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  3911. if (!conf->thread) {
  3912. printk(KERN_ERR
  3913. "raid5: couldn't allocate thread for %s\n",
  3914. mdname(mddev));
  3915. goto abort;
  3916. }
  3917. return conf;
  3918. abort:
  3919. if (conf) {
  3920. shrink_stripes(conf);
  3921. safe_put_page(conf->spare_page);
  3922. kfree(conf->disks);
  3923. kfree(conf->stripe_hashtbl);
  3924. kfree(conf);
  3925. return ERR_PTR(-EIO);
  3926. } else
  3927. return ERR_PTR(-ENOMEM);
  3928. }
  3929. static int run(mddev_t *mddev)
  3930. {
  3931. raid5_conf_t *conf;
  3932. int working_disks = 0;
  3933. mdk_rdev_t *rdev;
  3934. if (mddev->reshape_position != MaxSector) {
  3935. /* Check that we can continue the reshape.
  3936. * Currently only disks can change, it must
  3937. * increase, and we must be past the point where
  3938. * a stripe over-writes itself
  3939. */
  3940. sector_t here_new, here_old;
  3941. int old_disks;
  3942. int max_degraded = (mddev->level == 6 ? 2 : 1);
  3943. if (mddev->new_level != mddev->level) {
  3944. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3945. "required - aborting.\n",
  3946. mdname(mddev));
  3947. return -EINVAL;
  3948. }
  3949. old_disks = mddev->raid_disks - mddev->delta_disks;
  3950. /* reshape_position must be on a new-stripe boundary, and one
  3951. * further up in new geometry must map after here in old
  3952. * geometry.
  3953. */
  3954. here_new = mddev->reshape_position;
  3955. if (sector_div(here_new, (mddev->new_chunk>>9)*
  3956. (mddev->raid_disks - max_degraded))) {
  3957. printk(KERN_ERR "raid5: reshape_position not "
  3958. "on a stripe boundary\n");
  3959. return -EINVAL;
  3960. }
  3961. /* here_new is the stripe we will write to */
  3962. here_old = mddev->reshape_position;
  3963. sector_div(here_old, (mddev->chunk_size>>9)*
  3964. (old_disks-max_degraded));
  3965. /* here_old is the first stripe that we might need to read
  3966. * from */
  3967. if (here_new >= here_old) {
  3968. /* Reading from the same stripe as writing to - bad */
  3969. printk(KERN_ERR "raid5: reshape_position too early for "
  3970. "auto-recovery - aborting.\n");
  3971. return -EINVAL;
  3972. }
  3973. printk(KERN_INFO "raid5: reshape will continue\n");
  3974. /* OK, we should be able to continue; */
  3975. } else {
  3976. BUG_ON(mddev->level != mddev->new_level);
  3977. BUG_ON(mddev->layout != mddev->new_layout);
  3978. BUG_ON(mddev->chunk_size != mddev->new_chunk);
  3979. BUG_ON(mddev->delta_disks != 0);
  3980. }
  3981. if (mddev->private == NULL)
  3982. conf = setup_conf(mddev);
  3983. else
  3984. conf = mddev->private;
  3985. if (IS_ERR(conf))
  3986. return PTR_ERR(conf);
  3987. mddev->thread = conf->thread;
  3988. conf->thread = NULL;
  3989. mddev->private = conf;
  3990. /*
  3991. * 0 for a fully functional array, 1 or 2 for a degraded array.
  3992. */
  3993. list_for_each_entry(rdev, &mddev->disks, same_set)
  3994. if (rdev->raid_disk >= 0 &&
  3995. test_bit(In_sync, &rdev->flags))
  3996. working_disks++;
  3997. mddev->degraded = conf->raid_disks - working_disks;
  3998. if (mddev->degraded > conf->max_degraded) {
  3999. printk(KERN_ERR "raid5: not enough operational devices for %s"
  4000. " (%d/%d failed)\n",
  4001. mdname(mddev), mddev->degraded, conf->raid_disks);
  4002. goto abort;
  4003. }
  4004. /* device size must be a multiple of chunk size */
  4005. mddev->dev_sectors &= ~(mddev->chunk_size / 512 - 1);
  4006. mddev->resync_max_sectors = mddev->dev_sectors;
  4007. if (mddev->degraded > 0 &&
  4008. mddev->recovery_cp != MaxSector) {
  4009. if (mddev->ok_start_degraded)
  4010. printk(KERN_WARNING
  4011. "raid5: starting dirty degraded array: %s"
  4012. "- data corruption possible.\n",
  4013. mdname(mddev));
  4014. else {
  4015. printk(KERN_ERR
  4016. "raid5: cannot start dirty degraded array for %s\n",
  4017. mdname(mddev));
  4018. goto abort;
  4019. }
  4020. }
  4021. if (mddev->degraded == 0)
  4022. printk("raid5: raid level %d set %s active with %d out of %d"
  4023. " devices, algorithm %d\n", conf->level, mdname(mddev),
  4024. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  4025. mddev->new_layout);
  4026. else
  4027. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  4028. " out of %d devices, algorithm %d\n", conf->level,
  4029. mdname(mddev), mddev->raid_disks - mddev->degraded,
  4030. mddev->raid_disks, mddev->new_layout);
  4031. print_raid5_conf(conf);
  4032. if (conf->reshape_progress != MaxSector) {
  4033. printk("...ok start reshape thread\n");
  4034. conf->reshape_safe = conf->reshape_progress;
  4035. atomic_set(&conf->reshape_stripes, 0);
  4036. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4037. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4038. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4039. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4040. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4041. "%s_reshape");
  4042. }
  4043. /* read-ahead size must cover two whole stripes, which is
  4044. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4045. */
  4046. {
  4047. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  4048. int stripe = data_disks *
  4049. (mddev->chunk_size / PAGE_SIZE);
  4050. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4051. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4052. }
  4053. /* Ok, everything is just fine now */
  4054. if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  4055. printk(KERN_WARNING
  4056. "raid5: failed to create sysfs attributes for %s\n",
  4057. mdname(mddev));
  4058. mddev->queue->queue_lock = &conf->device_lock;
  4059. mddev->queue->unplug_fn = raid5_unplug_device;
  4060. mddev->queue->backing_dev_info.congested_data = mddev;
  4061. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  4062. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  4063. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  4064. return 0;
  4065. abort:
  4066. md_unregister_thread(mddev->thread);
  4067. mddev->thread = NULL;
  4068. if (conf) {
  4069. shrink_stripes(conf);
  4070. print_raid5_conf(conf);
  4071. safe_put_page(conf->spare_page);
  4072. kfree(conf->disks);
  4073. kfree(conf->stripe_hashtbl);
  4074. kfree(conf);
  4075. }
  4076. mddev->private = NULL;
  4077. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  4078. return -EIO;
  4079. }
  4080. static int stop(mddev_t *mddev)
  4081. {
  4082. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  4083. md_unregister_thread(mddev->thread);
  4084. mddev->thread = NULL;
  4085. shrink_stripes(conf);
  4086. kfree(conf->stripe_hashtbl);
  4087. mddev->queue->backing_dev_info.congested_fn = NULL;
  4088. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  4089. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  4090. kfree(conf->disks);
  4091. kfree(conf);
  4092. mddev->private = NULL;
  4093. return 0;
  4094. }
  4095. #ifdef DEBUG
  4096. static void print_sh(struct seq_file *seq, struct stripe_head *sh)
  4097. {
  4098. int i;
  4099. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  4100. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  4101. seq_printf(seq, "sh %llu, count %d.\n",
  4102. (unsigned long long)sh->sector, atomic_read(&sh->count));
  4103. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  4104. for (i = 0; i < sh->disks; i++) {
  4105. seq_printf(seq, "(cache%d: %p %ld) ",
  4106. i, sh->dev[i].page, sh->dev[i].flags);
  4107. }
  4108. seq_printf(seq, "\n");
  4109. }
  4110. static void printall(struct seq_file *seq, raid5_conf_t *conf)
  4111. {
  4112. struct stripe_head *sh;
  4113. struct hlist_node *hn;
  4114. int i;
  4115. spin_lock_irq(&conf->device_lock);
  4116. for (i = 0; i < NR_HASH; i++) {
  4117. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  4118. if (sh->raid_conf != conf)
  4119. continue;
  4120. print_sh(seq, sh);
  4121. }
  4122. }
  4123. spin_unlock_irq(&conf->device_lock);
  4124. }
  4125. #endif
  4126. static void status(struct seq_file *seq, mddev_t *mddev)
  4127. {
  4128. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  4129. int i;
  4130. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  4131. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  4132. for (i = 0; i < conf->raid_disks; i++)
  4133. seq_printf (seq, "%s",
  4134. conf->disks[i].rdev &&
  4135. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  4136. seq_printf (seq, "]");
  4137. #ifdef DEBUG
  4138. seq_printf (seq, "\n");
  4139. printall(seq, conf);
  4140. #endif
  4141. }
  4142. static void print_raid5_conf (raid5_conf_t *conf)
  4143. {
  4144. int i;
  4145. struct disk_info *tmp;
  4146. printk("RAID5 conf printout:\n");
  4147. if (!conf) {
  4148. printk("(conf==NULL)\n");
  4149. return;
  4150. }
  4151. printk(" --- rd:%d wd:%d\n", conf->raid_disks,
  4152. conf->raid_disks - conf->mddev->degraded);
  4153. for (i = 0; i < conf->raid_disks; i++) {
  4154. char b[BDEVNAME_SIZE];
  4155. tmp = conf->disks + i;
  4156. if (tmp->rdev)
  4157. printk(" disk %d, o:%d, dev:%s\n",
  4158. i, !test_bit(Faulty, &tmp->rdev->flags),
  4159. bdevname(tmp->rdev->bdev,b));
  4160. }
  4161. }
  4162. static int raid5_spare_active(mddev_t *mddev)
  4163. {
  4164. int i;
  4165. raid5_conf_t *conf = mddev->private;
  4166. struct disk_info *tmp;
  4167. for (i = 0; i < conf->raid_disks; i++) {
  4168. tmp = conf->disks + i;
  4169. if (tmp->rdev
  4170. && !test_bit(Faulty, &tmp->rdev->flags)
  4171. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  4172. unsigned long flags;
  4173. spin_lock_irqsave(&conf->device_lock, flags);
  4174. mddev->degraded--;
  4175. spin_unlock_irqrestore(&conf->device_lock, flags);
  4176. }
  4177. }
  4178. print_raid5_conf(conf);
  4179. return 0;
  4180. }
  4181. static int raid5_remove_disk(mddev_t *mddev, int number)
  4182. {
  4183. raid5_conf_t *conf = mddev->private;
  4184. int err = 0;
  4185. mdk_rdev_t *rdev;
  4186. struct disk_info *p = conf->disks + number;
  4187. print_raid5_conf(conf);
  4188. rdev = p->rdev;
  4189. if (rdev) {
  4190. if (number >= conf->raid_disks &&
  4191. conf->reshape_progress == MaxSector)
  4192. clear_bit(In_sync, &rdev->flags);
  4193. if (test_bit(In_sync, &rdev->flags) ||
  4194. atomic_read(&rdev->nr_pending)) {
  4195. err = -EBUSY;
  4196. goto abort;
  4197. }
  4198. /* Only remove non-faulty devices if recovery
  4199. * isn't possible.
  4200. */
  4201. if (!test_bit(Faulty, &rdev->flags) &&
  4202. mddev->degraded <= conf->max_degraded &&
  4203. number < conf->raid_disks) {
  4204. err = -EBUSY;
  4205. goto abort;
  4206. }
  4207. p->rdev = NULL;
  4208. synchronize_rcu();
  4209. if (atomic_read(&rdev->nr_pending)) {
  4210. /* lost the race, try later */
  4211. err = -EBUSY;
  4212. p->rdev = rdev;
  4213. }
  4214. }
  4215. abort:
  4216. print_raid5_conf(conf);
  4217. return err;
  4218. }
  4219. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  4220. {
  4221. raid5_conf_t *conf = mddev->private;
  4222. int err = -EEXIST;
  4223. int disk;
  4224. struct disk_info *p;
  4225. int first = 0;
  4226. int last = conf->raid_disks - 1;
  4227. if (mddev->degraded > conf->max_degraded)
  4228. /* no point adding a device */
  4229. return -EINVAL;
  4230. if (rdev->raid_disk >= 0)
  4231. first = last = rdev->raid_disk;
  4232. /*
  4233. * find the disk ... but prefer rdev->saved_raid_disk
  4234. * if possible.
  4235. */
  4236. if (rdev->saved_raid_disk >= 0 &&
  4237. rdev->saved_raid_disk >= first &&
  4238. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  4239. disk = rdev->saved_raid_disk;
  4240. else
  4241. disk = first;
  4242. for ( ; disk <= last ; disk++)
  4243. if ((p=conf->disks + disk)->rdev == NULL) {
  4244. clear_bit(In_sync, &rdev->flags);
  4245. rdev->raid_disk = disk;
  4246. err = 0;
  4247. if (rdev->saved_raid_disk != disk)
  4248. conf->fullsync = 1;
  4249. rcu_assign_pointer(p->rdev, rdev);
  4250. break;
  4251. }
  4252. print_raid5_conf(conf);
  4253. return err;
  4254. }
  4255. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  4256. {
  4257. /* no resync is happening, and there is enough space
  4258. * on all devices, so we can resize.
  4259. * We need to make sure resync covers any new space.
  4260. * If the array is shrinking we should possibly wait until
  4261. * any io in the removed space completes, but it hardly seems
  4262. * worth it.
  4263. */
  4264. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  4265. md_set_array_sectors(mddev, raid5_size(mddev, sectors,
  4266. mddev->raid_disks));
  4267. if (mddev->array_sectors >
  4268. raid5_size(mddev, sectors, mddev->raid_disks))
  4269. return -EINVAL;
  4270. set_capacity(mddev->gendisk, mddev->array_sectors);
  4271. mddev->changed = 1;
  4272. if (sectors > mddev->dev_sectors && mddev->recovery_cp == MaxSector) {
  4273. mddev->recovery_cp = mddev->dev_sectors;
  4274. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4275. }
  4276. mddev->dev_sectors = sectors;
  4277. mddev->resync_max_sectors = sectors;
  4278. return 0;
  4279. }
  4280. static int raid5_check_reshape(mddev_t *mddev)
  4281. {
  4282. raid5_conf_t *conf = mddev_to_conf(mddev);
  4283. if (mddev->delta_disks == 0 &&
  4284. mddev->new_layout == mddev->layout &&
  4285. mddev->new_chunk == mddev->chunk_size)
  4286. return -EINVAL; /* nothing to do */
  4287. if (mddev->bitmap)
  4288. /* Cannot grow a bitmap yet */
  4289. return -EBUSY;
  4290. if (mddev->degraded > conf->max_degraded)
  4291. return -EINVAL;
  4292. if (mddev->delta_disks < 0) {
  4293. /* We might be able to shrink, but the devices must
  4294. * be made bigger first.
  4295. * For raid6, 4 is the minimum size.
  4296. * Otherwise 2 is the minimum
  4297. */
  4298. int min = 2;
  4299. if (mddev->level == 6)
  4300. min = 4;
  4301. if (mddev->raid_disks + mddev->delta_disks < min)
  4302. return -EINVAL;
  4303. }
  4304. /* Can only proceed if there are plenty of stripe_heads.
  4305. * We need a minimum of one full stripe,, and for sensible progress
  4306. * it is best to have about 4 times that.
  4307. * If we require 4 times, then the default 256 4K stripe_heads will
  4308. * allow for chunk sizes up to 256K, which is probably OK.
  4309. * If the chunk size is greater, user-space should request more
  4310. * stripe_heads first.
  4311. */
  4312. if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
  4313. (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
  4314. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  4315. (max(mddev->chunk_size, mddev->new_chunk)
  4316. / STRIPE_SIZE)*4);
  4317. return -ENOSPC;
  4318. }
  4319. return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  4320. }
  4321. static int raid5_start_reshape(mddev_t *mddev)
  4322. {
  4323. raid5_conf_t *conf = mddev_to_conf(mddev);
  4324. mdk_rdev_t *rdev;
  4325. int spares = 0;
  4326. int added_devices = 0;
  4327. unsigned long flags;
  4328. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  4329. return -EBUSY;
  4330. list_for_each_entry(rdev, &mddev->disks, same_set)
  4331. if (rdev->raid_disk < 0 &&
  4332. !test_bit(Faulty, &rdev->flags))
  4333. spares++;
  4334. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  4335. /* Not enough devices even to make a degraded array
  4336. * of that size
  4337. */
  4338. return -EINVAL;
  4339. /* Refuse to reduce size of the array. Any reductions in
  4340. * array size must be through explicit setting of array_size
  4341. * attribute.
  4342. */
  4343. if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
  4344. < mddev->array_sectors) {
  4345. printk(KERN_ERR "md: %s: array size must be reduced "
  4346. "before number of disks\n", mdname(mddev));
  4347. return -EINVAL;
  4348. }
  4349. atomic_set(&conf->reshape_stripes, 0);
  4350. spin_lock_irq(&conf->device_lock);
  4351. conf->previous_raid_disks = conf->raid_disks;
  4352. conf->raid_disks += mddev->delta_disks;
  4353. conf->prev_chunk = conf->chunk_size;
  4354. conf->chunk_size = mddev->new_chunk;
  4355. conf->prev_algo = conf->algorithm;
  4356. conf->algorithm = mddev->new_layout;
  4357. if (mddev->delta_disks < 0)
  4358. conf->reshape_progress = raid5_size(mddev, 0, 0);
  4359. else
  4360. conf->reshape_progress = 0;
  4361. conf->reshape_safe = conf->reshape_progress;
  4362. conf->generation++;
  4363. spin_unlock_irq(&conf->device_lock);
  4364. /* Add some new drives, as many as will fit.
  4365. * We know there are enough to make the newly sized array work.
  4366. */
  4367. list_for_each_entry(rdev, &mddev->disks, same_set)
  4368. if (rdev->raid_disk < 0 &&
  4369. !test_bit(Faulty, &rdev->flags)) {
  4370. if (raid5_add_disk(mddev, rdev) == 0) {
  4371. char nm[20];
  4372. set_bit(In_sync, &rdev->flags);
  4373. added_devices++;
  4374. rdev->recovery_offset = 0;
  4375. sprintf(nm, "rd%d", rdev->raid_disk);
  4376. if (sysfs_create_link(&mddev->kobj,
  4377. &rdev->kobj, nm))
  4378. printk(KERN_WARNING
  4379. "raid5: failed to create "
  4380. " link %s for %s\n",
  4381. nm, mdname(mddev));
  4382. } else
  4383. break;
  4384. }
  4385. if (mddev->delta_disks > 0) {
  4386. spin_lock_irqsave(&conf->device_lock, flags);
  4387. mddev->degraded = (conf->raid_disks - conf->previous_raid_disks)
  4388. - added_devices;
  4389. spin_unlock_irqrestore(&conf->device_lock, flags);
  4390. }
  4391. mddev->raid_disks = conf->raid_disks;
  4392. mddev->reshape_position = 0;
  4393. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4394. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4395. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4396. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4397. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4398. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4399. "%s_reshape");
  4400. if (!mddev->sync_thread) {
  4401. mddev->recovery = 0;
  4402. spin_lock_irq(&conf->device_lock);
  4403. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  4404. conf->reshape_progress = MaxSector;
  4405. spin_unlock_irq(&conf->device_lock);
  4406. return -EAGAIN;
  4407. }
  4408. conf->reshape_checkpoint = jiffies;
  4409. md_wakeup_thread(mddev->sync_thread);
  4410. md_new_event(mddev);
  4411. return 0;
  4412. }
  4413. /* This is called from the reshape thread and should make any
  4414. * changes needed in 'conf'
  4415. */
  4416. static void end_reshape(raid5_conf_t *conf)
  4417. {
  4418. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  4419. spin_lock_irq(&conf->device_lock);
  4420. conf->previous_raid_disks = conf->raid_disks;
  4421. conf->reshape_progress = MaxSector;
  4422. spin_unlock_irq(&conf->device_lock);
  4423. wake_up(&conf->wait_for_overlap);
  4424. /* read-ahead size must cover two whole stripes, which is
  4425. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4426. */
  4427. {
  4428. int data_disks = conf->raid_disks - conf->max_degraded;
  4429. int stripe = data_disks * (conf->chunk_size
  4430. / PAGE_SIZE);
  4431. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4432. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4433. }
  4434. }
  4435. }
  4436. /* This is called from the raid5d thread with mddev_lock held.
  4437. * It makes config changes to the device.
  4438. */
  4439. static void raid5_finish_reshape(mddev_t *mddev)
  4440. {
  4441. struct block_device *bdev;
  4442. raid5_conf_t *conf = mddev_to_conf(mddev);
  4443. if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
  4444. if (mddev->delta_disks > 0) {
  4445. md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
  4446. set_capacity(mddev->gendisk, mddev->array_sectors);
  4447. mddev->changed = 1;
  4448. bdev = bdget_disk(mddev->gendisk, 0);
  4449. if (bdev) {
  4450. mutex_lock(&bdev->bd_inode->i_mutex);
  4451. i_size_write(bdev->bd_inode,
  4452. (loff_t)mddev->array_sectors << 9);
  4453. mutex_unlock(&bdev->bd_inode->i_mutex);
  4454. bdput(bdev);
  4455. }
  4456. } else {
  4457. int d;
  4458. mddev->degraded = conf->raid_disks;
  4459. for (d = 0; d < conf->raid_disks ; d++)
  4460. if (conf->disks[d].rdev &&
  4461. test_bit(In_sync,
  4462. &conf->disks[d].rdev->flags))
  4463. mddev->degraded--;
  4464. for (d = conf->raid_disks ;
  4465. d < conf->raid_disks - mddev->delta_disks;
  4466. d++)
  4467. raid5_remove_disk(mddev, d);
  4468. }
  4469. mddev->layout = conf->algorithm;
  4470. mddev->chunk_size = conf->chunk_size;
  4471. mddev->reshape_position = MaxSector;
  4472. mddev->delta_disks = 0;
  4473. }
  4474. }
  4475. static void raid5_quiesce(mddev_t *mddev, int state)
  4476. {
  4477. raid5_conf_t *conf = mddev_to_conf(mddev);
  4478. switch(state) {
  4479. case 2: /* resume for a suspend */
  4480. wake_up(&conf->wait_for_overlap);
  4481. break;
  4482. case 1: /* stop all writes */
  4483. spin_lock_irq(&conf->device_lock);
  4484. conf->quiesce = 1;
  4485. wait_event_lock_irq(conf->wait_for_stripe,
  4486. atomic_read(&conf->active_stripes) == 0 &&
  4487. atomic_read(&conf->active_aligned_reads) == 0,
  4488. conf->device_lock, /* nothing */);
  4489. spin_unlock_irq(&conf->device_lock);
  4490. break;
  4491. case 0: /* re-enable writes */
  4492. spin_lock_irq(&conf->device_lock);
  4493. conf->quiesce = 0;
  4494. wake_up(&conf->wait_for_stripe);
  4495. wake_up(&conf->wait_for_overlap);
  4496. spin_unlock_irq(&conf->device_lock);
  4497. break;
  4498. }
  4499. }
  4500. static void *raid5_takeover_raid1(mddev_t *mddev)
  4501. {
  4502. int chunksect;
  4503. if (mddev->raid_disks != 2 ||
  4504. mddev->degraded > 1)
  4505. return ERR_PTR(-EINVAL);
  4506. /* Should check if there are write-behind devices? */
  4507. chunksect = 64*2; /* 64K by default */
  4508. /* The array must be an exact multiple of chunksize */
  4509. while (chunksect && (mddev->array_sectors & (chunksect-1)))
  4510. chunksect >>= 1;
  4511. if ((chunksect<<9) < STRIPE_SIZE)
  4512. /* array size does not allow a suitable chunk size */
  4513. return ERR_PTR(-EINVAL);
  4514. mddev->new_level = 5;
  4515. mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
  4516. mddev->new_chunk = chunksect << 9;
  4517. return setup_conf(mddev);
  4518. }
  4519. static void *raid5_takeover_raid6(mddev_t *mddev)
  4520. {
  4521. int new_layout;
  4522. switch (mddev->layout) {
  4523. case ALGORITHM_LEFT_ASYMMETRIC_6:
  4524. new_layout = ALGORITHM_LEFT_ASYMMETRIC;
  4525. break;
  4526. case ALGORITHM_RIGHT_ASYMMETRIC_6:
  4527. new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
  4528. break;
  4529. case ALGORITHM_LEFT_SYMMETRIC_6:
  4530. new_layout = ALGORITHM_LEFT_SYMMETRIC;
  4531. break;
  4532. case ALGORITHM_RIGHT_SYMMETRIC_6:
  4533. new_layout = ALGORITHM_RIGHT_SYMMETRIC;
  4534. break;
  4535. case ALGORITHM_PARITY_0_6:
  4536. new_layout = ALGORITHM_PARITY_0;
  4537. break;
  4538. case ALGORITHM_PARITY_N:
  4539. new_layout = ALGORITHM_PARITY_N;
  4540. break;
  4541. default:
  4542. return ERR_PTR(-EINVAL);
  4543. }
  4544. mddev->new_level = 5;
  4545. mddev->new_layout = new_layout;
  4546. mddev->delta_disks = -1;
  4547. mddev->raid_disks -= 1;
  4548. return setup_conf(mddev);
  4549. }
  4550. static int raid5_reconfig(mddev_t *mddev, int new_layout, int new_chunk)
  4551. {
  4552. /* For a 2-drive array, the layout and chunk size can be changed
  4553. * immediately as not restriping is needed.
  4554. * For larger arrays we record the new value - after validation
  4555. * to be used by a reshape pass.
  4556. */
  4557. raid5_conf_t *conf = mddev_to_conf(mddev);
  4558. if (new_layout >= 0 && !algorithm_valid_raid5(new_layout))
  4559. return -EINVAL;
  4560. if (new_chunk > 0) {
  4561. if (new_chunk & (new_chunk-1))
  4562. /* not a power of 2 */
  4563. return -EINVAL;
  4564. if (new_chunk < PAGE_SIZE)
  4565. return -EINVAL;
  4566. if (mddev->array_sectors & ((new_chunk>>9)-1))
  4567. /* not factor of array size */
  4568. return -EINVAL;
  4569. }
  4570. /* They look valid */
  4571. if (mddev->raid_disks == 2) {
  4572. if (new_layout >= 0) {
  4573. conf->algorithm = new_layout;
  4574. mddev->layout = mddev->new_layout = new_layout;
  4575. }
  4576. if (new_chunk > 0) {
  4577. conf->chunk_size = new_chunk;
  4578. mddev->chunk_size = mddev->new_chunk = new_chunk;
  4579. }
  4580. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4581. md_wakeup_thread(mddev->thread);
  4582. } else {
  4583. if (new_layout >= 0)
  4584. mddev->new_layout = new_layout;
  4585. if (new_chunk > 0)
  4586. mddev->new_chunk = new_chunk;
  4587. }
  4588. return 0;
  4589. }
  4590. static int raid6_reconfig(mddev_t *mddev, int new_layout, int new_chunk)
  4591. {
  4592. if (new_layout >= 0 && !algorithm_valid_raid6(new_layout))
  4593. return -EINVAL;
  4594. if (new_chunk > 0) {
  4595. if (new_chunk & (new_chunk-1))
  4596. /* not a power of 2 */
  4597. return -EINVAL;
  4598. if (new_chunk < PAGE_SIZE)
  4599. return -EINVAL;
  4600. if (mddev->array_sectors & ((new_chunk>>9)-1))
  4601. /* not factor of array size */
  4602. return -EINVAL;
  4603. }
  4604. /* They look valid */
  4605. if (new_layout >= 0)
  4606. mddev->new_layout = new_layout;
  4607. if (new_chunk > 0)
  4608. mddev->new_chunk = new_chunk;
  4609. return 0;
  4610. }
  4611. static void *raid5_takeover(mddev_t *mddev)
  4612. {
  4613. /* raid5 can take over:
  4614. * raid0 - if all devices are the same - make it a raid4 layout
  4615. * raid1 - if there are two drives. We need to know the chunk size
  4616. * raid4 - trivial - just use a raid4 layout.
  4617. * raid6 - Providing it is a *_6 layout
  4618. *
  4619. * For now, just do raid1
  4620. */
  4621. if (mddev->level == 1)
  4622. return raid5_takeover_raid1(mddev);
  4623. if (mddev->level == 4) {
  4624. mddev->new_layout = ALGORITHM_PARITY_N;
  4625. mddev->new_level = 5;
  4626. return setup_conf(mddev);
  4627. }
  4628. if (mddev->level == 6)
  4629. return raid5_takeover_raid6(mddev);
  4630. return ERR_PTR(-EINVAL);
  4631. }
  4632. static struct mdk_personality raid5_personality;
  4633. static void *raid6_takeover(mddev_t *mddev)
  4634. {
  4635. /* Currently can only take over a raid5. We map the
  4636. * personality to an equivalent raid6 personality
  4637. * with the Q block at the end.
  4638. */
  4639. int new_layout;
  4640. if (mddev->pers != &raid5_personality)
  4641. return ERR_PTR(-EINVAL);
  4642. if (mddev->degraded > 1)
  4643. return ERR_PTR(-EINVAL);
  4644. if (mddev->raid_disks > 253)
  4645. return ERR_PTR(-EINVAL);
  4646. if (mddev->raid_disks < 3)
  4647. return ERR_PTR(-EINVAL);
  4648. switch (mddev->layout) {
  4649. case ALGORITHM_LEFT_ASYMMETRIC:
  4650. new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
  4651. break;
  4652. case ALGORITHM_RIGHT_ASYMMETRIC:
  4653. new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
  4654. break;
  4655. case ALGORITHM_LEFT_SYMMETRIC:
  4656. new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
  4657. break;
  4658. case ALGORITHM_RIGHT_SYMMETRIC:
  4659. new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
  4660. break;
  4661. case ALGORITHM_PARITY_0:
  4662. new_layout = ALGORITHM_PARITY_0_6;
  4663. break;
  4664. case ALGORITHM_PARITY_N:
  4665. new_layout = ALGORITHM_PARITY_N;
  4666. break;
  4667. default:
  4668. return ERR_PTR(-EINVAL);
  4669. }
  4670. mddev->new_level = 6;
  4671. mddev->new_layout = new_layout;
  4672. mddev->delta_disks = 1;
  4673. mddev->raid_disks += 1;
  4674. return setup_conf(mddev);
  4675. }
  4676. static struct mdk_personality raid6_personality =
  4677. {
  4678. .name = "raid6",
  4679. .level = 6,
  4680. .owner = THIS_MODULE,
  4681. .make_request = make_request,
  4682. .run = run,
  4683. .stop = stop,
  4684. .status = status,
  4685. .error_handler = error,
  4686. .hot_add_disk = raid5_add_disk,
  4687. .hot_remove_disk= raid5_remove_disk,
  4688. .spare_active = raid5_spare_active,
  4689. .sync_request = sync_request,
  4690. .resize = raid5_resize,
  4691. .size = raid5_size,
  4692. .check_reshape = raid5_check_reshape,
  4693. .start_reshape = raid5_start_reshape,
  4694. .finish_reshape = raid5_finish_reshape,
  4695. .quiesce = raid5_quiesce,
  4696. .takeover = raid6_takeover,
  4697. .reconfig = raid6_reconfig,
  4698. };
  4699. static struct mdk_personality raid5_personality =
  4700. {
  4701. .name = "raid5",
  4702. .level = 5,
  4703. .owner = THIS_MODULE,
  4704. .make_request = make_request,
  4705. .run = run,
  4706. .stop = stop,
  4707. .status = status,
  4708. .error_handler = error,
  4709. .hot_add_disk = raid5_add_disk,
  4710. .hot_remove_disk= raid5_remove_disk,
  4711. .spare_active = raid5_spare_active,
  4712. .sync_request = sync_request,
  4713. .resize = raid5_resize,
  4714. .size = raid5_size,
  4715. .check_reshape = raid5_check_reshape,
  4716. .start_reshape = raid5_start_reshape,
  4717. .finish_reshape = raid5_finish_reshape,
  4718. .quiesce = raid5_quiesce,
  4719. .takeover = raid5_takeover,
  4720. .reconfig = raid5_reconfig,
  4721. };
  4722. static struct mdk_personality raid4_personality =
  4723. {
  4724. .name = "raid4",
  4725. .level = 4,
  4726. .owner = THIS_MODULE,
  4727. .make_request = make_request,
  4728. .run = run,
  4729. .stop = stop,
  4730. .status = status,
  4731. .error_handler = error,
  4732. .hot_add_disk = raid5_add_disk,
  4733. .hot_remove_disk= raid5_remove_disk,
  4734. .spare_active = raid5_spare_active,
  4735. .sync_request = sync_request,
  4736. .resize = raid5_resize,
  4737. .size = raid5_size,
  4738. .check_reshape = raid5_check_reshape,
  4739. .start_reshape = raid5_start_reshape,
  4740. .finish_reshape = raid5_finish_reshape,
  4741. .quiesce = raid5_quiesce,
  4742. };
  4743. static int __init raid5_init(void)
  4744. {
  4745. register_md_personality(&raid6_personality);
  4746. register_md_personality(&raid5_personality);
  4747. register_md_personality(&raid4_personality);
  4748. return 0;
  4749. }
  4750. static void raid5_exit(void)
  4751. {
  4752. unregister_md_personality(&raid6_personality);
  4753. unregister_md_personality(&raid5_personality);
  4754. unregister_md_personality(&raid4_personality);
  4755. }
  4756. module_init(raid5_init);
  4757. module_exit(raid5_exit);
  4758. MODULE_LICENSE("GPL");
  4759. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  4760. MODULE_ALIAS("md-raid5");
  4761. MODULE_ALIAS("md-raid4");
  4762. MODULE_ALIAS("md-level-5");
  4763. MODULE_ALIAS("md-level-4");
  4764. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  4765. MODULE_ALIAS("md-raid6");
  4766. MODULE_ALIAS("md-level-6");
  4767. /* This used to be two separate modules, they were: */
  4768. MODULE_ALIAS("raid5");
  4769. MODULE_ALIAS("raid6");