raid5.c 138 KB

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