raid5.c 105 KB

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