raid5.c 150 KB

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