raid10.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648
  1. /*
  2. * raid10.c : Multiple Devices driver for Linux
  3. *
  4. * Copyright (C) 2000-2004 Neil Brown
  5. *
  6. * RAID-10 support for md.
  7. *
  8. * Base on code in raid1.c. See raid1.c for further copyright information.
  9. *
  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/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/module.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/ratelimit.h>
  26. #include "md.h"
  27. #include "raid10.h"
  28. #include "raid0.h"
  29. #include "bitmap.h"
  30. /*
  31. * RAID10 provides a combination of RAID0 and RAID1 functionality.
  32. * The layout of data is defined by
  33. * chunk_size
  34. * raid_disks
  35. * near_copies (stored in low byte of layout)
  36. * far_copies (stored in second byte of layout)
  37. * far_offset (stored in bit 16 of layout )
  38. *
  39. * The data to be stored is divided into chunks using chunksize.
  40. * Each device is divided into far_copies sections.
  41. * In each section, chunks are laid out in a style similar to raid0, but
  42. * near_copies copies of each chunk is stored (each on a different drive).
  43. * The starting device for each section is offset near_copies from the starting
  44. * device of the previous section.
  45. * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
  46. * drive.
  47. * near_copies and far_copies must be at least one, and their product is at most
  48. * raid_disks.
  49. *
  50. * If far_offset is true, then the far_copies are handled a bit differently.
  51. * The copies are still in different stripes, but instead of be very far apart
  52. * on disk, there are adjacent stripes.
  53. */
  54. /*
  55. * Number of guaranteed r10bios in case of extreme VM load:
  56. */
  57. #define NR_RAID10_BIOS 256
  58. /* When there are this many requests queue to be written by
  59. * the raid10 thread, we become 'congested' to provide back-pressure
  60. * for writeback.
  61. */
  62. static int max_queued_requests = 1024;
  63. static void allow_barrier(struct r10conf *conf);
  64. static void lower_barrier(struct r10conf *conf);
  65. static int enough(struct r10conf *conf, int ignore);
  66. static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
  67. {
  68. struct r10conf *conf = data;
  69. int size = offsetof(struct r10bio, devs[conf->copies]);
  70. /* allocate a r10bio with room for raid_disks entries in the
  71. * bios array */
  72. return kzalloc(size, gfp_flags);
  73. }
  74. static void r10bio_pool_free(void *r10_bio, void *data)
  75. {
  76. kfree(r10_bio);
  77. }
  78. /* Maximum size of each resync request */
  79. #define RESYNC_BLOCK_SIZE (64*1024)
  80. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  81. /* amount of memory to reserve for resync requests */
  82. #define RESYNC_WINDOW (1024*1024)
  83. /* maximum number of concurrent requests, memory permitting */
  84. #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
  85. /*
  86. * When performing a resync, we need to read and compare, so
  87. * we need as many pages are there are copies.
  88. * When performing a recovery, we need 2 bios, one for read,
  89. * one for write (we recover only one drive per r10buf)
  90. *
  91. */
  92. static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
  93. {
  94. struct r10conf *conf = data;
  95. struct page *page;
  96. struct r10bio *r10_bio;
  97. struct bio *bio;
  98. int i, j;
  99. int nalloc;
  100. r10_bio = r10bio_pool_alloc(gfp_flags, conf);
  101. if (!r10_bio)
  102. return NULL;
  103. if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
  104. nalloc = conf->copies; /* resync */
  105. else
  106. nalloc = 2; /* recovery */
  107. /*
  108. * Allocate bios.
  109. */
  110. for (j = nalloc ; j-- ; ) {
  111. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  112. if (!bio)
  113. goto out_free_bio;
  114. r10_bio->devs[j].bio = bio;
  115. if (!conf->have_replacement)
  116. continue;
  117. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  118. if (!bio)
  119. goto out_free_bio;
  120. r10_bio->devs[j].repl_bio = bio;
  121. }
  122. /*
  123. * Allocate RESYNC_PAGES data pages and attach them
  124. * where needed.
  125. */
  126. for (j = 0 ; j < nalloc; j++) {
  127. struct bio *rbio = r10_bio->devs[j].repl_bio;
  128. bio = r10_bio->devs[j].bio;
  129. for (i = 0; i < RESYNC_PAGES; i++) {
  130. if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
  131. &conf->mddev->recovery)) {
  132. /* we can share bv_page's during recovery */
  133. struct bio *rbio = r10_bio->devs[0].bio;
  134. page = rbio->bi_io_vec[i].bv_page;
  135. get_page(page);
  136. } else
  137. page = alloc_page(gfp_flags);
  138. if (unlikely(!page))
  139. goto out_free_pages;
  140. bio->bi_io_vec[i].bv_page = page;
  141. if (rbio)
  142. rbio->bi_io_vec[i].bv_page = page;
  143. }
  144. }
  145. return r10_bio;
  146. out_free_pages:
  147. for ( ; i > 0 ; i--)
  148. safe_put_page(bio->bi_io_vec[i-1].bv_page);
  149. while (j--)
  150. for (i = 0; i < RESYNC_PAGES ; i++)
  151. safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
  152. j = -1;
  153. out_free_bio:
  154. while (++j < nalloc) {
  155. bio_put(r10_bio->devs[j].bio);
  156. if (r10_bio->devs[j].repl_bio)
  157. bio_put(r10_bio->devs[j].repl_bio);
  158. }
  159. r10bio_pool_free(r10_bio, conf);
  160. return NULL;
  161. }
  162. static void r10buf_pool_free(void *__r10_bio, void *data)
  163. {
  164. int i;
  165. struct r10conf *conf = data;
  166. struct r10bio *r10bio = __r10_bio;
  167. int j;
  168. for (j=0; j < conf->copies; j++) {
  169. struct bio *bio = r10bio->devs[j].bio;
  170. if (bio) {
  171. for (i = 0; i < RESYNC_PAGES; i++) {
  172. safe_put_page(bio->bi_io_vec[i].bv_page);
  173. bio->bi_io_vec[i].bv_page = NULL;
  174. }
  175. bio_put(bio);
  176. }
  177. bio = r10bio->devs[j].repl_bio;
  178. if (bio)
  179. bio_put(bio);
  180. }
  181. r10bio_pool_free(r10bio, conf);
  182. }
  183. static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
  184. {
  185. int i;
  186. for (i = 0; i < conf->copies; i++) {
  187. struct bio **bio = & r10_bio->devs[i].bio;
  188. if (!BIO_SPECIAL(*bio))
  189. bio_put(*bio);
  190. *bio = NULL;
  191. bio = &r10_bio->devs[i].repl_bio;
  192. if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
  193. bio_put(*bio);
  194. *bio = NULL;
  195. }
  196. }
  197. static void free_r10bio(struct r10bio *r10_bio)
  198. {
  199. struct r10conf *conf = r10_bio->mddev->private;
  200. put_all_bios(conf, r10_bio);
  201. mempool_free(r10_bio, conf->r10bio_pool);
  202. }
  203. static void put_buf(struct r10bio *r10_bio)
  204. {
  205. struct r10conf *conf = r10_bio->mddev->private;
  206. mempool_free(r10_bio, conf->r10buf_pool);
  207. lower_barrier(conf);
  208. }
  209. static void reschedule_retry(struct r10bio *r10_bio)
  210. {
  211. unsigned long flags;
  212. struct mddev *mddev = r10_bio->mddev;
  213. struct r10conf *conf = mddev->private;
  214. spin_lock_irqsave(&conf->device_lock, flags);
  215. list_add(&r10_bio->retry_list, &conf->retry_list);
  216. conf->nr_queued ++;
  217. spin_unlock_irqrestore(&conf->device_lock, flags);
  218. /* wake up frozen array... */
  219. wake_up(&conf->wait_barrier);
  220. md_wakeup_thread(mddev->thread);
  221. }
  222. /*
  223. * raid_end_bio_io() is called when we have finished servicing a mirrored
  224. * operation and are ready to return a success/failure code to the buffer
  225. * cache layer.
  226. */
  227. static void raid_end_bio_io(struct r10bio *r10_bio)
  228. {
  229. struct bio *bio = r10_bio->master_bio;
  230. int done;
  231. struct r10conf *conf = r10_bio->mddev->private;
  232. if (bio->bi_phys_segments) {
  233. unsigned long flags;
  234. spin_lock_irqsave(&conf->device_lock, flags);
  235. bio->bi_phys_segments--;
  236. done = (bio->bi_phys_segments == 0);
  237. spin_unlock_irqrestore(&conf->device_lock, flags);
  238. } else
  239. done = 1;
  240. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  241. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  242. if (done) {
  243. bio_endio(bio, 0);
  244. /*
  245. * Wake up any possible resync thread that waits for the device
  246. * to go idle.
  247. */
  248. allow_barrier(conf);
  249. }
  250. free_r10bio(r10_bio);
  251. }
  252. /*
  253. * Update disk head position estimator based on IRQ completion info.
  254. */
  255. static inline void update_head_pos(int slot, struct r10bio *r10_bio)
  256. {
  257. struct r10conf *conf = r10_bio->mddev->private;
  258. conf->mirrors[r10_bio->devs[slot].devnum].head_position =
  259. r10_bio->devs[slot].addr + (r10_bio->sectors);
  260. }
  261. /*
  262. * Find the disk number which triggered given bio
  263. */
  264. static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
  265. struct bio *bio, int *slotp, int *replp)
  266. {
  267. int slot;
  268. int repl = 0;
  269. for (slot = 0; slot < conf->copies; slot++) {
  270. if (r10_bio->devs[slot].bio == bio)
  271. break;
  272. if (r10_bio->devs[slot].repl_bio == bio) {
  273. repl = 1;
  274. break;
  275. }
  276. }
  277. BUG_ON(slot == conf->copies);
  278. update_head_pos(slot, r10_bio);
  279. if (slotp)
  280. *slotp = slot;
  281. if (replp)
  282. *replp = repl;
  283. return r10_bio->devs[slot].devnum;
  284. }
  285. static void raid10_end_read_request(struct bio *bio, int error)
  286. {
  287. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  288. struct r10bio *r10_bio = bio->bi_private;
  289. int slot, dev;
  290. struct md_rdev *rdev;
  291. struct r10conf *conf = r10_bio->mddev->private;
  292. slot = r10_bio->read_slot;
  293. dev = r10_bio->devs[slot].devnum;
  294. rdev = r10_bio->devs[slot].rdev;
  295. /*
  296. * this branch is our 'one mirror IO has finished' event handler:
  297. */
  298. update_head_pos(slot, r10_bio);
  299. if (uptodate) {
  300. /*
  301. * Set R10BIO_Uptodate in our master bio, so that
  302. * we will return a good error code to the higher
  303. * levels even if IO on some other mirrored buffer fails.
  304. *
  305. * The 'master' represents the composite IO operation to
  306. * user-side. So if something waits for IO, then it will
  307. * wait for the 'master' bio.
  308. */
  309. set_bit(R10BIO_Uptodate, &r10_bio->state);
  310. } else {
  311. /* If all other devices that store this block have
  312. * failed, we want to return the error upwards rather
  313. * than fail the last device. Here we redefine
  314. * "uptodate" to mean "Don't want to retry"
  315. */
  316. unsigned long flags;
  317. spin_lock_irqsave(&conf->device_lock, flags);
  318. if (!enough(conf, rdev->raid_disk))
  319. uptodate = 1;
  320. spin_unlock_irqrestore(&conf->device_lock, flags);
  321. }
  322. if (uptodate) {
  323. raid_end_bio_io(r10_bio);
  324. rdev_dec_pending(rdev, conf->mddev);
  325. } else {
  326. /*
  327. * oops, read error - keep the refcount on the rdev
  328. */
  329. char b[BDEVNAME_SIZE];
  330. printk_ratelimited(KERN_ERR
  331. "md/raid10:%s: %s: rescheduling sector %llu\n",
  332. mdname(conf->mddev),
  333. bdevname(rdev->bdev, b),
  334. (unsigned long long)r10_bio->sector);
  335. set_bit(R10BIO_ReadError, &r10_bio->state);
  336. reschedule_retry(r10_bio);
  337. }
  338. }
  339. static void close_write(struct r10bio *r10_bio)
  340. {
  341. /* clear the bitmap if all writes complete successfully */
  342. bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
  343. r10_bio->sectors,
  344. !test_bit(R10BIO_Degraded, &r10_bio->state),
  345. 0);
  346. md_write_end(r10_bio->mddev);
  347. }
  348. static void one_write_done(struct r10bio *r10_bio)
  349. {
  350. if (atomic_dec_and_test(&r10_bio->remaining)) {
  351. if (test_bit(R10BIO_WriteError, &r10_bio->state))
  352. reschedule_retry(r10_bio);
  353. else {
  354. close_write(r10_bio);
  355. if (test_bit(R10BIO_MadeGood, &r10_bio->state))
  356. reschedule_retry(r10_bio);
  357. else
  358. raid_end_bio_io(r10_bio);
  359. }
  360. }
  361. }
  362. static void raid10_end_write_request(struct bio *bio, int error)
  363. {
  364. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  365. struct r10bio *r10_bio = bio->bi_private;
  366. int dev;
  367. int dec_rdev = 1;
  368. struct r10conf *conf = r10_bio->mddev->private;
  369. int slot, repl;
  370. struct md_rdev *rdev = NULL;
  371. dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  372. if (repl)
  373. rdev = conf->mirrors[dev].replacement;
  374. if (!rdev) {
  375. smp_rmb();
  376. repl = 0;
  377. rdev = conf->mirrors[dev].rdev;
  378. }
  379. /*
  380. * this branch is our 'one mirror IO has finished' event handler:
  381. */
  382. if (!uptodate) {
  383. if (repl)
  384. /* Never record new bad blocks to replacement,
  385. * just fail it.
  386. */
  387. md_error(rdev->mddev, rdev);
  388. else {
  389. set_bit(WriteErrorSeen, &rdev->flags);
  390. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  391. set_bit(MD_RECOVERY_NEEDED,
  392. &rdev->mddev->recovery);
  393. set_bit(R10BIO_WriteError, &r10_bio->state);
  394. dec_rdev = 0;
  395. }
  396. } else {
  397. /*
  398. * Set R10BIO_Uptodate in our master bio, so that
  399. * we will return a good error code for to the higher
  400. * levels even if IO on some other mirrored buffer fails.
  401. *
  402. * The 'master' represents the composite IO operation to
  403. * user-side. So if something waits for IO, then it will
  404. * wait for the 'master' bio.
  405. */
  406. sector_t first_bad;
  407. int bad_sectors;
  408. /*
  409. * Do not set R10BIO_Uptodate if the current device is
  410. * rebuilding or Faulty. This is because we cannot use
  411. * such device for properly reading the data back (we could
  412. * potentially use it, if the current write would have felt
  413. * before rdev->recovery_offset, but for simplicity we don't
  414. * check this here.
  415. */
  416. if (test_bit(In_sync, &rdev->flags) &&
  417. !test_bit(Faulty, &rdev->flags))
  418. set_bit(R10BIO_Uptodate, &r10_bio->state);
  419. /* Maybe we can clear some bad blocks. */
  420. if (is_badblock(rdev,
  421. r10_bio->devs[slot].addr,
  422. r10_bio->sectors,
  423. &first_bad, &bad_sectors)) {
  424. bio_put(bio);
  425. if (repl)
  426. r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
  427. else
  428. r10_bio->devs[slot].bio = IO_MADE_GOOD;
  429. dec_rdev = 0;
  430. set_bit(R10BIO_MadeGood, &r10_bio->state);
  431. }
  432. }
  433. /*
  434. *
  435. * Let's see if all mirrored write operations have finished
  436. * already.
  437. */
  438. one_write_done(r10_bio);
  439. if (dec_rdev)
  440. rdev_dec_pending(rdev, conf->mddev);
  441. }
  442. /*
  443. * RAID10 layout manager
  444. * As well as the chunksize and raid_disks count, there are two
  445. * parameters: near_copies and far_copies.
  446. * near_copies * far_copies must be <= raid_disks.
  447. * Normally one of these will be 1.
  448. * If both are 1, we get raid0.
  449. * If near_copies == raid_disks, we get raid1.
  450. *
  451. * Chunks are laid out in raid0 style with near_copies copies of the
  452. * first chunk, followed by near_copies copies of the next chunk and
  453. * so on.
  454. * If far_copies > 1, then after 1/far_copies of the array has been assigned
  455. * as described above, we start again with a device offset of near_copies.
  456. * So we effectively have another copy of the whole array further down all
  457. * the drives, but with blocks on different drives.
  458. * With this layout, and block is never stored twice on the one device.
  459. *
  460. * raid10_find_phys finds the sector offset of a given virtual sector
  461. * on each device that it is on.
  462. *
  463. * raid10_find_virt does the reverse mapping, from a device and a
  464. * sector offset to a virtual address
  465. */
  466. static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
  467. {
  468. int n,f;
  469. sector_t sector;
  470. sector_t chunk;
  471. sector_t stripe;
  472. int dev;
  473. int slot = 0;
  474. /* now calculate first sector/dev */
  475. chunk = r10bio->sector >> conf->chunk_shift;
  476. sector = r10bio->sector & conf->chunk_mask;
  477. chunk *= conf->near_copies;
  478. stripe = chunk;
  479. dev = sector_div(stripe, conf->raid_disks);
  480. if (conf->far_offset)
  481. stripe *= conf->far_copies;
  482. sector += stripe << conf->chunk_shift;
  483. /* and calculate all the others */
  484. for (n=0; n < conf->near_copies; n++) {
  485. int d = dev;
  486. sector_t s = sector;
  487. r10bio->devs[slot].addr = sector;
  488. r10bio->devs[slot].devnum = d;
  489. slot++;
  490. for (f = 1; f < conf->far_copies; f++) {
  491. d += conf->near_copies;
  492. if (d >= conf->raid_disks)
  493. d -= conf->raid_disks;
  494. s += conf->stride;
  495. r10bio->devs[slot].devnum = d;
  496. r10bio->devs[slot].addr = s;
  497. slot++;
  498. }
  499. dev++;
  500. if (dev >= conf->raid_disks) {
  501. dev = 0;
  502. sector += (conf->chunk_mask + 1);
  503. }
  504. }
  505. BUG_ON(slot != conf->copies);
  506. }
  507. static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
  508. {
  509. sector_t offset, chunk, vchunk;
  510. offset = sector & conf->chunk_mask;
  511. if (conf->far_offset) {
  512. int fc;
  513. chunk = sector >> conf->chunk_shift;
  514. fc = sector_div(chunk, conf->far_copies);
  515. dev -= fc * conf->near_copies;
  516. if (dev < 0)
  517. dev += conf->raid_disks;
  518. } else {
  519. while (sector >= conf->stride) {
  520. sector -= conf->stride;
  521. if (dev < conf->near_copies)
  522. dev += conf->raid_disks - conf->near_copies;
  523. else
  524. dev -= conf->near_copies;
  525. }
  526. chunk = sector >> conf->chunk_shift;
  527. }
  528. vchunk = chunk * conf->raid_disks + dev;
  529. sector_div(vchunk, conf->near_copies);
  530. return (vchunk << conf->chunk_shift) + offset;
  531. }
  532. /**
  533. * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
  534. * @q: request queue
  535. * @bvm: properties of new bio
  536. * @biovec: the request that could be merged to it.
  537. *
  538. * Return amount of bytes we can accept at this offset
  539. * This requires checking for end-of-chunk if near_copies != raid_disks,
  540. * and for subordinate merge_bvec_fns if merge_check_needed.
  541. */
  542. static int raid10_mergeable_bvec(struct request_queue *q,
  543. struct bvec_merge_data *bvm,
  544. struct bio_vec *biovec)
  545. {
  546. struct mddev *mddev = q->queuedata;
  547. struct r10conf *conf = mddev->private;
  548. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  549. int max;
  550. unsigned int chunk_sectors = mddev->chunk_sectors;
  551. unsigned int bio_sectors = bvm->bi_size >> 9;
  552. if (conf->near_copies < conf->raid_disks) {
  553. max = (chunk_sectors - ((sector & (chunk_sectors - 1))
  554. + bio_sectors)) << 9;
  555. if (max < 0)
  556. /* bio_add cannot handle a negative return */
  557. max = 0;
  558. if (max <= biovec->bv_len && bio_sectors == 0)
  559. return biovec->bv_len;
  560. } else
  561. max = biovec->bv_len;
  562. if (mddev->merge_check_needed) {
  563. struct {
  564. struct r10bio r10_bio;
  565. struct r10dev devs[conf->copies];
  566. } on_stack;
  567. struct r10bio *r10_bio = &on_stack.r10_bio;
  568. int s;
  569. r10_bio->sector = sector;
  570. raid10_find_phys(conf, r10_bio);
  571. rcu_read_lock();
  572. for (s = 0; s < conf->copies; s++) {
  573. int disk = r10_bio->devs[s].devnum;
  574. struct md_rdev *rdev = rcu_dereference(
  575. conf->mirrors[disk].rdev);
  576. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  577. struct request_queue *q =
  578. bdev_get_queue(rdev->bdev);
  579. if (q->merge_bvec_fn) {
  580. bvm->bi_sector = r10_bio->devs[s].addr
  581. + rdev->data_offset;
  582. bvm->bi_bdev = rdev->bdev;
  583. max = min(max, q->merge_bvec_fn(
  584. q, bvm, biovec));
  585. }
  586. }
  587. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  588. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  589. struct request_queue *q =
  590. bdev_get_queue(rdev->bdev);
  591. if (q->merge_bvec_fn) {
  592. bvm->bi_sector = r10_bio->devs[s].addr
  593. + rdev->data_offset;
  594. bvm->bi_bdev = rdev->bdev;
  595. max = min(max, q->merge_bvec_fn(
  596. q, bvm, biovec));
  597. }
  598. }
  599. }
  600. rcu_read_unlock();
  601. }
  602. return max;
  603. }
  604. /*
  605. * This routine returns the disk from which the requested read should
  606. * be done. There is a per-array 'next expected sequential IO' sector
  607. * number - if this matches on the next IO then we use the last disk.
  608. * There is also a per-disk 'last know head position' sector that is
  609. * maintained from IRQ contexts, both the normal and the resync IO
  610. * completion handlers update this position correctly. If there is no
  611. * perfect sequential match then we pick the disk whose head is closest.
  612. *
  613. * If there are 2 mirrors in the same 2 devices, performance degrades
  614. * because position is mirror, not device based.
  615. *
  616. * The rdev for the device selected will have nr_pending incremented.
  617. */
  618. /*
  619. * FIXME: possibly should rethink readbalancing and do it differently
  620. * depending on near_copies / far_copies geometry.
  621. */
  622. static struct md_rdev *read_balance(struct r10conf *conf,
  623. struct r10bio *r10_bio,
  624. int *max_sectors)
  625. {
  626. const sector_t this_sector = r10_bio->sector;
  627. int disk, slot;
  628. int sectors = r10_bio->sectors;
  629. int best_good_sectors;
  630. sector_t new_distance, best_dist;
  631. struct md_rdev *rdev, *best_rdev;
  632. int do_balance;
  633. int best_slot;
  634. raid10_find_phys(conf, r10_bio);
  635. rcu_read_lock();
  636. retry:
  637. sectors = r10_bio->sectors;
  638. best_slot = -1;
  639. best_rdev = NULL;
  640. best_dist = MaxSector;
  641. best_good_sectors = 0;
  642. do_balance = 1;
  643. /*
  644. * Check if we can balance. We can balance on the whole
  645. * device if no resync is going on (recovery is ok), or below
  646. * the resync window. We take the first readable disk when
  647. * above the resync window.
  648. */
  649. if (conf->mddev->recovery_cp < MaxSector
  650. && (this_sector + sectors >= conf->next_resync))
  651. do_balance = 0;
  652. for (slot = 0; slot < conf->copies ; slot++) {
  653. sector_t first_bad;
  654. int bad_sectors;
  655. sector_t dev_sector;
  656. if (r10_bio->devs[slot].bio == IO_BLOCKED)
  657. continue;
  658. disk = r10_bio->devs[slot].devnum;
  659. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  660. if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
  661. test_bit(Unmerged, &rdev->flags) ||
  662. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  663. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  664. if (rdev == NULL ||
  665. test_bit(Faulty, &rdev->flags) ||
  666. test_bit(Unmerged, &rdev->flags))
  667. continue;
  668. if (!test_bit(In_sync, &rdev->flags) &&
  669. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  670. continue;
  671. dev_sector = r10_bio->devs[slot].addr;
  672. if (is_badblock(rdev, dev_sector, sectors,
  673. &first_bad, &bad_sectors)) {
  674. if (best_dist < MaxSector)
  675. /* Already have a better slot */
  676. continue;
  677. if (first_bad <= dev_sector) {
  678. /* Cannot read here. If this is the
  679. * 'primary' device, then we must not read
  680. * beyond 'bad_sectors' from another device.
  681. */
  682. bad_sectors -= (dev_sector - first_bad);
  683. if (!do_balance && sectors > bad_sectors)
  684. sectors = bad_sectors;
  685. if (best_good_sectors > sectors)
  686. best_good_sectors = sectors;
  687. } else {
  688. sector_t good_sectors =
  689. first_bad - dev_sector;
  690. if (good_sectors > best_good_sectors) {
  691. best_good_sectors = good_sectors;
  692. best_slot = slot;
  693. best_rdev = rdev;
  694. }
  695. if (!do_balance)
  696. /* Must read from here */
  697. break;
  698. }
  699. continue;
  700. } else
  701. best_good_sectors = sectors;
  702. if (!do_balance)
  703. break;
  704. /* This optimisation is debatable, and completely destroys
  705. * sequential read speed for 'far copies' arrays. So only
  706. * keep it for 'near' arrays, and review those later.
  707. */
  708. if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending))
  709. break;
  710. /* for far > 1 always use the lowest address */
  711. if (conf->far_copies > 1)
  712. new_distance = r10_bio->devs[slot].addr;
  713. else
  714. new_distance = abs(r10_bio->devs[slot].addr -
  715. conf->mirrors[disk].head_position);
  716. if (new_distance < best_dist) {
  717. best_dist = new_distance;
  718. best_slot = slot;
  719. best_rdev = rdev;
  720. }
  721. }
  722. if (slot >= conf->copies) {
  723. slot = best_slot;
  724. rdev = best_rdev;
  725. }
  726. if (slot >= 0) {
  727. atomic_inc(&rdev->nr_pending);
  728. if (test_bit(Faulty, &rdev->flags)) {
  729. /* Cannot risk returning a device that failed
  730. * before we inc'ed nr_pending
  731. */
  732. rdev_dec_pending(rdev, conf->mddev);
  733. goto retry;
  734. }
  735. r10_bio->read_slot = slot;
  736. } else
  737. rdev = NULL;
  738. rcu_read_unlock();
  739. *max_sectors = best_good_sectors;
  740. return rdev;
  741. }
  742. static int raid10_congested(void *data, int bits)
  743. {
  744. struct mddev *mddev = data;
  745. struct r10conf *conf = mddev->private;
  746. int i, ret = 0;
  747. if ((bits & (1 << BDI_async_congested)) &&
  748. conf->pending_count >= max_queued_requests)
  749. return 1;
  750. if (mddev_congested(mddev, bits))
  751. return 1;
  752. rcu_read_lock();
  753. for (i = 0; i < conf->raid_disks && ret == 0; i++) {
  754. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  755. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  756. struct request_queue *q = bdev_get_queue(rdev->bdev);
  757. ret |= bdi_congested(&q->backing_dev_info, bits);
  758. }
  759. }
  760. rcu_read_unlock();
  761. return ret;
  762. }
  763. static void flush_pending_writes(struct r10conf *conf)
  764. {
  765. /* Any writes that have been queued but are awaiting
  766. * bitmap updates get flushed here.
  767. */
  768. spin_lock_irq(&conf->device_lock);
  769. if (conf->pending_bio_list.head) {
  770. struct bio *bio;
  771. bio = bio_list_get(&conf->pending_bio_list);
  772. conf->pending_count = 0;
  773. spin_unlock_irq(&conf->device_lock);
  774. /* flush any pending bitmap writes to disk
  775. * before proceeding w/ I/O */
  776. bitmap_unplug(conf->mddev->bitmap);
  777. wake_up(&conf->wait_barrier);
  778. while (bio) { /* submit pending writes */
  779. struct bio *next = bio->bi_next;
  780. bio->bi_next = NULL;
  781. generic_make_request(bio);
  782. bio = next;
  783. }
  784. } else
  785. spin_unlock_irq(&conf->device_lock);
  786. }
  787. /* Barriers....
  788. * Sometimes we need to suspend IO while we do something else,
  789. * either some resync/recovery, or reconfigure the array.
  790. * To do this we raise a 'barrier'.
  791. * The 'barrier' is a counter that can be raised multiple times
  792. * to count how many activities are happening which preclude
  793. * normal IO.
  794. * We can only raise the barrier if there is no pending IO.
  795. * i.e. if nr_pending == 0.
  796. * We choose only to raise the barrier if no-one is waiting for the
  797. * barrier to go down. This means that as soon as an IO request
  798. * is ready, no other operations which require a barrier will start
  799. * until the IO request has had a chance.
  800. *
  801. * So: regular IO calls 'wait_barrier'. When that returns there
  802. * is no backgroup IO happening, It must arrange to call
  803. * allow_barrier when it has finished its IO.
  804. * backgroup IO calls must call raise_barrier. Once that returns
  805. * there is no normal IO happeing. It must arrange to call
  806. * lower_barrier when the particular background IO completes.
  807. */
  808. static void raise_barrier(struct r10conf *conf, int force)
  809. {
  810. BUG_ON(force && !conf->barrier);
  811. spin_lock_irq(&conf->resync_lock);
  812. /* Wait until no block IO is waiting (unless 'force') */
  813. wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
  814. conf->resync_lock, );
  815. /* block any new IO from starting */
  816. conf->barrier++;
  817. /* Now wait for all pending IO to complete */
  818. wait_event_lock_irq(conf->wait_barrier,
  819. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  820. conf->resync_lock, );
  821. spin_unlock_irq(&conf->resync_lock);
  822. }
  823. static void lower_barrier(struct r10conf *conf)
  824. {
  825. unsigned long flags;
  826. spin_lock_irqsave(&conf->resync_lock, flags);
  827. conf->barrier--;
  828. spin_unlock_irqrestore(&conf->resync_lock, flags);
  829. wake_up(&conf->wait_barrier);
  830. }
  831. static void wait_barrier(struct r10conf *conf)
  832. {
  833. spin_lock_irq(&conf->resync_lock);
  834. if (conf->barrier) {
  835. conf->nr_waiting++;
  836. /* Wait for the barrier to drop.
  837. * However if there are already pending
  838. * requests (preventing the barrier from
  839. * rising completely), and the
  840. * pre-process bio queue isn't empty,
  841. * then don't wait, as we need to empty
  842. * that queue to get the nr_pending
  843. * count down.
  844. */
  845. wait_event_lock_irq(conf->wait_barrier,
  846. !conf->barrier ||
  847. (conf->nr_pending &&
  848. current->bio_list &&
  849. !bio_list_empty(current->bio_list)),
  850. conf->resync_lock,
  851. );
  852. conf->nr_waiting--;
  853. }
  854. conf->nr_pending++;
  855. spin_unlock_irq(&conf->resync_lock);
  856. }
  857. static void allow_barrier(struct r10conf *conf)
  858. {
  859. unsigned long flags;
  860. spin_lock_irqsave(&conf->resync_lock, flags);
  861. conf->nr_pending--;
  862. spin_unlock_irqrestore(&conf->resync_lock, flags);
  863. wake_up(&conf->wait_barrier);
  864. }
  865. static void freeze_array(struct r10conf *conf, int extra)
  866. {
  867. /* stop syncio and normal IO and wait for everything to
  868. * go quiet.
  869. * We increment barrier and nr_waiting, and then
  870. * wait until nr_pending match nr_queued+extra
  871. * This is called in the context of one normal IO request
  872. * that has failed. Thus any sync request that might be pending
  873. * will be blocked by nr_pending, and we need to wait for
  874. * pending IO requests to complete or be queued for re-try.
  875. * Thus the number queued (nr_queued) plus this request (extra)
  876. * must match the number of pending IOs (nr_pending) before
  877. * we continue.
  878. */
  879. spin_lock_irq(&conf->resync_lock);
  880. conf->barrier++;
  881. conf->nr_waiting++;
  882. wait_event_lock_irq(conf->wait_barrier,
  883. conf->nr_pending == conf->nr_queued+extra,
  884. conf->resync_lock,
  885. flush_pending_writes(conf));
  886. spin_unlock_irq(&conf->resync_lock);
  887. }
  888. static void unfreeze_array(struct r10conf *conf)
  889. {
  890. /* reverse the effect of the freeze */
  891. spin_lock_irq(&conf->resync_lock);
  892. conf->barrier--;
  893. conf->nr_waiting--;
  894. wake_up(&conf->wait_barrier);
  895. spin_unlock_irq(&conf->resync_lock);
  896. }
  897. static void make_request(struct mddev *mddev, struct bio * bio)
  898. {
  899. struct r10conf *conf = mddev->private;
  900. struct r10bio *r10_bio;
  901. struct bio *read_bio;
  902. int i;
  903. int chunk_sects = conf->chunk_mask + 1;
  904. const int rw = bio_data_dir(bio);
  905. const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
  906. const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
  907. unsigned long flags;
  908. struct md_rdev *blocked_rdev;
  909. int plugged;
  910. int sectors_handled;
  911. int max_sectors;
  912. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  913. md_flush_request(mddev, bio);
  914. return;
  915. }
  916. /* If this request crosses a chunk boundary, we need to
  917. * split it. This will only happen for 1 PAGE (or less) requests.
  918. */
  919. if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
  920. > chunk_sects &&
  921. conf->near_copies < conf->raid_disks)) {
  922. struct bio_pair *bp;
  923. /* Sanity check -- queue functions should prevent this happening */
  924. if (bio->bi_vcnt != 1 ||
  925. bio->bi_idx != 0)
  926. goto bad_map;
  927. /* This is a one page bio that upper layers
  928. * refuse to split for us, so we need to split it.
  929. */
  930. bp = bio_split(bio,
  931. chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
  932. /* Each of these 'make_request' calls will call 'wait_barrier'.
  933. * If the first succeeds but the second blocks due to the resync
  934. * thread raising the barrier, we will deadlock because the
  935. * IO to the underlying device will be queued in generic_make_request
  936. * and will never complete, so will never reduce nr_pending.
  937. * So increment nr_waiting here so no new raise_barriers will
  938. * succeed, and so the second wait_barrier cannot block.
  939. */
  940. spin_lock_irq(&conf->resync_lock);
  941. conf->nr_waiting++;
  942. spin_unlock_irq(&conf->resync_lock);
  943. make_request(mddev, &bp->bio1);
  944. make_request(mddev, &bp->bio2);
  945. spin_lock_irq(&conf->resync_lock);
  946. conf->nr_waiting--;
  947. wake_up(&conf->wait_barrier);
  948. spin_unlock_irq(&conf->resync_lock);
  949. bio_pair_release(bp);
  950. return;
  951. bad_map:
  952. printk("md/raid10:%s: make_request bug: can't convert block across chunks"
  953. " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
  954. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  955. bio_io_error(bio);
  956. return;
  957. }
  958. md_write_start(mddev, bio);
  959. /*
  960. * Register the new request and wait if the reconstruction
  961. * thread has put up a bar for new requests.
  962. * Continue immediately if no resync is active currently.
  963. */
  964. wait_barrier(conf);
  965. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  966. r10_bio->master_bio = bio;
  967. r10_bio->sectors = bio->bi_size >> 9;
  968. r10_bio->mddev = mddev;
  969. r10_bio->sector = bio->bi_sector;
  970. r10_bio->state = 0;
  971. /* We might need to issue multiple reads to different
  972. * devices if there are bad blocks around, so we keep
  973. * track of the number of reads in bio->bi_phys_segments.
  974. * If this is 0, there is only one r10_bio and no locking
  975. * will be needed when the request completes. If it is
  976. * non-zero, then it is the number of not-completed requests.
  977. */
  978. bio->bi_phys_segments = 0;
  979. clear_bit(BIO_SEG_VALID, &bio->bi_flags);
  980. if (rw == READ) {
  981. /*
  982. * read balancing logic:
  983. */
  984. struct md_rdev *rdev;
  985. int slot;
  986. read_again:
  987. rdev = read_balance(conf, r10_bio, &max_sectors);
  988. if (!rdev) {
  989. raid_end_bio_io(r10_bio);
  990. return;
  991. }
  992. slot = r10_bio->read_slot;
  993. read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  994. md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
  995. max_sectors);
  996. r10_bio->devs[slot].bio = read_bio;
  997. r10_bio->devs[slot].rdev = rdev;
  998. read_bio->bi_sector = r10_bio->devs[slot].addr +
  999. rdev->data_offset;
  1000. read_bio->bi_bdev = rdev->bdev;
  1001. read_bio->bi_end_io = raid10_end_read_request;
  1002. read_bio->bi_rw = READ | do_sync;
  1003. read_bio->bi_private = r10_bio;
  1004. if (max_sectors < r10_bio->sectors) {
  1005. /* Could not read all from this device, so we will
  1006. * need another r10_bio.
  1007. */
  1008. sectors_handled = (r10_bio->sector + max_sectors
  1009. - bio->bi_sector);
  1010. r10_bio->sectors = max_sectors;
  1011. spin_lock_irq(&conf->device_lock);
  1012. if (bio->bi_phys_segments == 0)
  1013. bio->bi_phys_segments = 2;
  1014. else
  1015. bio->bi_phys_segments++;
  1016. spin_unlock_irq(&conf->device_lock);
  1017. /* Cannot call generic_make_request directly
  1018. * as that will be queued in __generic_make_request
  1019. * and subsequent mempool_alloc might block
  1020. * waiting for it. so hand bio over to raid10d.
  1021. */
  1022. reschedule_retry(r10_bio);
  1023. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1024. r10_bio->master_bio = bio;
  1025. r10_bio->sectors = ((bio->bi_size >> 9)
  1026. - sectors_handled);
  1027. r10_bio->state = 0;
  1028. r10_bio->mddev = mddev;
  1029. r10_bio->sector = bio->bi_sector + sectors_handled;
  1030. goto read_again;
  1031. } else
  1032. generic_make_request(read_bio);
  1033. return;
  1034. }
  1035. /*
  1036. * WRITE:
  1037. */
  1038. if (conf->pending_count >= max_queued_requests) {
  1039. md_wakeup_thread(mddev->thread);
  1040. wait_event(conf->wait_barrier,
  1041. conf->pending_count < max_queued_requests);
  1042. }
  1043. /* first select target devices under rcu_lock and
  1044. * inc refcount on their rdev. Record them by setting
  1045. * bios[x] to bio
  1046. * If there are known/acknowledged bad blocks on any device
  1047. * on which we have seen a write error, we want to avoid
  1048. * writing to those blocks. This potentially requires several
  1049. * writes to write around the bad blocks. Each set of writes
  1050. * gets its own r10_bio with a set of bios attached. The number
  1051. * of r10_bios is recored in bio->bi_phys_segments just as with
  1052. * the read case.
  1053. */
  1054. plugged = mddev_check_plugged(mddev);
  1055. r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
  1056. raid10_find_phys(conf, r10_bio);
  1057. retry_write:
  1058. blocked_rdev = NULL;
  1059. rcu_read_lock();
  1060. max_sectors = r10_bio->sectors;
  1061. for (i = 0; i < conf->copies; i++) {
  1062. int d = r10_bio->devs[i].devnum;
  1063. struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
  1064. struct md_rdev *rrdev = rcu_dereference(
  1065. conf->mirrors[d].replacement);
  1066. if (rdev == rrdev)
  1067. rrdev = NULL;
  1068. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  1069. atomic_inc(&rdev->nr_pending);
  1070. blocked_rdev = rdev;
  1071. break;
  1072. }
  1073. if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
  1074. atomic_inc(&rrdev->nr_pending);
  1075. blocked_rdev = rrdev;
  1076. break;
  1077. }
  1078. if (rdev && (test_bit(Faulty, &rdev->flags)
  1079. || test_bit(Unmerged, &rdev->flags)))
  1080. rdev = NULL;
  1081. if (rrdev && (test_bit(Faulty, &rrdev->flags)
  1082. || test_bit(Unmerged, &rrdev->flags)))
  1083. rrdev = NULL;
  1084. r10_bio->devs[i].bio = NULL;
  1085. r10_bio->devs[i].repl_bio = NULL;
  1086. if (!rdev && !rrdev) {
  1087. set_bit(R10BIO_Degraded, &r10_bio->state);
  1088. continue;
  1089. }
  1090. if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
  1091. sector_t first_bad;
  1092. sector_t dev_sector = r10_bio->devs[i].addr;
  1093. int bad_sectors;
  1094. int is_bad;
  1095. is_bad = is_badblock(rdev, dev_sector,
  1096. max_sectors,
  1097. &first_bad, &bad_sectors);
  1098. if (is_bad < 0) {
  1099. /* Mustn't write here until the bad block
  1100. * is acknowledged
  1101. */
  1102. atomic_inc(&rdev->nr_pending);
  1103. set_bit(BlockedBadBlocks, &rdev->flags);
  1104. blocked_rdev = rdev;
  1105. break;
  1106. }
  1107. if (is_bad && first_bad <= dev_sector) {
  1108. /* Cannot write here at all */
  1109. bad_sectors -= (dev_sector - first_bad);
  1110. if (bad_sectors < max_sectors)
  1111. /* Mustn't write more than bad_sectors
  1112. * to other devices yet
  1113. */
  1114. max_sectors = bad_sectors;
  1115. /* We don't set R10BIO_Degraded as that
  1116. * only applies if the disk is missing,
  1117. * so it might be re-added, and we want to
  1118. * know to recover this chunk.
  1119. * In this case the device is here, and the
  1120. * fact that this chunk is not in-sync is
  1121. * recorded in the bad block log.
  1122. */
  1123. continue;
  1124. }
  1125. if (is_bad) {
  1126. int good_sectors = first_bad - dev_sector;
  1127. if (good_sectors < max_sectors)
  1128. max_sectors = good_sectors;
  1129. }
  1130. }
  1131. if (rdev) {
  1132. r10_bio->devs[i].bio = bio;
  1133. atomic_inc(&rdev->nr_pending);
  1134. }
  1135. if (rrdev) {
  1136. r10_bio->devs[i].repl_bio = bio;
  1137. atomic_inc(&rrdev->nr_pending);
  1138. }
  1139. }
  1140. rcu_read_unlock();
  1141. if (unlikely(blocked_rdev)) {
  1142. /* Have to wait for this device to get unblocked, then retry */
  1143. int j;
  1144. int d;
  1145. for (j = 0; j < i; j++) {
  1146. if (r10_bio->devs[j].bio) {
  1147. d = r10_bio->devs[j].devnum;
  1148. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1149. }
  1150. if (r10_bio->devs[j].repl_bio) {
  1151. struct md_rdev *rdev;
  1152. d = r10_bio->devs[j].devnum;
  1153. rdev = conf->mirrors[d].replacement;
  1154. if (!rdev) {
  1155. /* Race with remove_disk */
  1156. smp_mb();
  1157. rdev = conf->mirrors[d].rdev;
  1158. }
  1159. rdev_dec_pending(rdev, mddev);
  1160. }
  1161. }
  1162. allow_barrier(conf);
  1163. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  1164. wait_barrier(conf);
  1165. goto retry_write;
  1166. }
  1167. if (max_sectors < r10_bio->sectors) {
  1168. /* We are splitting this into multiple parts, so
  1169. * we need to prepare for allocating another r10_bio.
  1170. */
  1171. r10_bio->sectors = max_sectors;
  1172. spin_lock_irq(&conf->device_lock);
  1173. if (bio->bi_phys_segments == 0)
  1174. bio->bi_phys_segments = 2;
  1175. else
  1176. bio->bi_phys_segments++;
  1177. spin_unlock_irq(&conf->device_lock);
  1178. }
  1179. sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
  1180. atomic_set(&r10_bio->remaining, 1);
  1181. bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
  1182. for (i = 0; i < conf->copies; i++) {
  1183. struct bio *mbio;
  1184. int d = r10_bio->devs[i].devnum;
  1185. if (r10_bio->devs[i].bio) {
  1186. struct md_rdev *rdev = conf->mirrors[d].rdev;
  1187. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1188. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1189. max_sectors);
  1190. r10_bio->devs[i].bio = mbio;
  1191. mbio->bi_sector = (r10_bio->devs[i].addr+
  1192. rdev->data_offset);
  1193. mbio->bi_bdev = rdev->bdev;
  1194. mbio->bi_end_io = raid10_end_write_request;
  1195. mbio->bi_rw = WRITE | do_sync | do_fua;
  1196. mbio->bi_private = r10_bio;
  1197. atomic_inc(&r10_bio->remaining);
  1198. spin_lock_irqsave(&conf->device_lock, flags);
  1199. bio_list_add(&conf->pending_bio_list, mbio);
  1200. conf->pending_count++;
  1201. spin_unlock_irqrestore(&conf->device_lock, flags);
  1202. }
  1203. if (r10_bio->devs[i].repl_bio) {
  1204. struct md_rdev *rdev = conf->mirrors[d].replacement;
  1205. if (rdev == NULL) {
  1206. /* Replacement just got moved to main 'rdev' */
  1207. smp_mb();
  1208. rdev = conf->mirrors[d].rdev;
  1209. }
  1210. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1211. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1212. max_sectors);
  1213. r10_bio->devs[i].repl_bio = mbio;
  1214. mbio->bi_sector = (r10_bio->devs[i].addr+
  1215. rdev->data_offset);
  1216. mbio->bi_bdev = rdev->bdev;
  1217. mbio->bi_end_io = raid10_end_write_request;
  1218. mbio->bi_rw = WRITE | do_sync | do_fua;
  1219. mbio->bi_private = r10_bio;
  1220. atomic_inc(&r10_bio->remaining);
  1221. spin_lock_irqsave(&conf->device_lock, flags);
  1222. bio_list_add(&conf->pending_bio_list, mbio);
  1223. conf->pending_count++;
  1224. spin_unlock_irqrestore(&conf->device_lock, flags);
  1225. }
  1226. }
  1227. /* Don't remove the bias on 'remaining' (one_write_done) until
  1228. * after checking if we need to go around again.
  1229. */
  1230. if (sectors_handled < (bio->bi_size >> 9)) {
  1231. one_write_done(r10_bio);
  1232. /* We need another r10_bio. It has already been counted
  1233. * in bio->bi_phys_segments.
  1234. */
  1235. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1236. r10_bio->master_bio = bio;
  1237. r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
  1238. r10_bio->mddev = mddev;
  1239. r10_bio->sector = bio->bi_sector + sectors_handled;
  1240. r10_bio->state = 0;
  1241. goto retry_write;
  1242. }
  1243. one_write_done(r10_bio);
  1244. /* In case raid10d snuck in to freeze_array */
  1245. wake_up(&conf->wait_barrier);
  1246. if (do_sync || !mddev->bitmap || !plugged)
  1247. md_wakeup_thread(mddev->thread);
  1248. }
  1249. static void status(struct seq_file *seq, struct mddev *mddev)
  1250. {
  1251. struct r10conf *conf = mddev->private;
  1252. int i;
  1253. if (conf->near_copies < conf->raid_disks)
  1254. seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
  1255. if (conf->near_copies > 1)
  1256. seq_printf(seq, " %d near-copies", conf->near_copies);
  1257. if (conf->far_copies > 1) {
  1258. if (conf->far_offset)
  1259. seq_printf(seq, " %d offset-copies", conf->far_copies);
  1260. else
  1261. seq_printf(seq, " %d far-copies", conf->far_copies);
  1262. }
  1263. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  1264. conf->raid_disks - mddev->degraded);
  1265. for (i = 0; i < conf->raid_disks; i++)
  1266. seq_printf(seq, "%s",
  1267. conf->mirrors[i].rdev &&
  1268. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  1269. seq_printf(seq, "]");
  1270. }
  1271. /* check if there are enough drives for
  1272. * every block to appear on atleast one.
  1273. * Don't consider the device numbered 'ignore'
  1274. * as we might be about to remove it.
  1275. */
  1276. static int enough(struct r10conf *conf, int ignore)
  1277. {
  1278. int first = 0;
  1279. do {
  1280. int n = conf->copies;
  1281. int cnt = 0;
  1282. int this = first;
  1283. while (n--) {
  1284. if (conf->mirrors[this].rdev &&
  1285. this != ignore)
  1286. cnt++;
  1287. this = (this+1) % conf->raid_disks;
  1288. }
  1289. if (cnt == 0)
  1290. return 0;
  1291. first = (first + conf->near_copies) % conf->raid_disks;
  1292. } while (first != 0);
  1293. return 1;
  1294. }
  1295. static void error(struct mddev *mddev, struct md_rdev *rdev)
  1296. {
  1297. char b[BDEVNAME_SIZE];
  1298. struct r10conf *conf = mddev->private;
  1299. /*
  1300. * If it is not operational, then we have already marked it as dead
  1301. * else if it is the last working disks, ignore the error, let the
  1302. * next level up know.
  1303. * else mark the drive as failed
  1304. */
  1305. if (test_bit(In_sync, &rdev->flags)
  1306. && !enough(conf, rdev->raid_disk))
  1307. /*
  1308. * Don't fail the drive, just return an IO error.
  1309. */
  1310. return;
  1311. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1312. unsigned long flags;
  1313. spin_lock_irqsave(&conf->device_lock, flags);
  1314. mddev->degraded++;
  1315. spin_unlock_irqrestore(&conf->device_lock, flags);
  1316. /*
  1317. * if recovery is running, make sure it aborts.
  1318. */
  1319. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1320. }
  1321. set_bit(Blocked, &rdev->flags);
  1322. set_bit(Faulty, &rdev->flags);
  1323. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1324. set_bit(MD_CHANGE_PENDING, &mddev->flags);
  1325. printk(KERN_ALERT
  1326. "md/raid10:%s: Disk failure on %s, disabling device.\n"
  1327. "md/raid10:%s: Operation continuing on %d devices.\n",
  1328. mdname(mddev), bdevname(rdev->bdev, b),
  1329. mdname(mddev), conf->raid_disks - mddev->degraded);
  1330. }
  1331. static void print_conf(struct r10conf *conf)
  1332. {
  1333. int i;
  1334. struct mirror_info *tmp;
  1335. printk(KERN_DEBUG "RAID10 conf printout:\n");
  1336. if (!conf) {
  1337. printk(KERN_DEBUG "(!conf)\n");
  1338. return;
  1339. }
  1340. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
  1341. conf->raid_disks);
  1342. for (i = 0; i < conf->raid_disks; i++) {
  1343. char b[BDEVNAME_SIZE];
  1344. tmp = conf->mirrors + i;
  1345. if (tmp->rdev)
  1346. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  1347. i, !test_bit(In_sync, &tmp->rdev->flags),
  1348. !test_bit(Faulty, &tmp->rdev->flags),
  1349. bdevname(tmp->rdev->bdev,b));
  1350. }
  1351. }
  1352. static void close_sync(struct r10conf *conf)
  1353. {
  1354. wait_barrier(conf);
  1355. allow_barrier(conf);
  1356. mempool_destroy(conf->r10buf_pool);
  1357. conf->r10buf_pool = NULL;
  1358. }
  1359. static int raid10_spare_active(struct mddev *mddev)
  1360. {
  1361. int i;
  1362. struct r10conf *conf = mddev->private;
  1363. struct mirror_info *tmp;
  1364. int count = 0;
  1365. unsigned long flags;
  1366. /*
  1367. * Find all non-in_sync disks within the RAID10 configuration
  1368. * and mark them in_sync
  1369. */
  1370. for (i = 0; i < conf->raid_disks; i++) {
  1371. tmp = conf->mirrors + i;
  1372. if (tmp->replacement
  1373. && tmp->replacement->recovery_offset == MaxSector
  1374. && !test_bit(Faulty, &tmp->replacement->flags)
  1375. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  1376. /* Replacement has just become active */
  1377. if (!tmp->rdev
  1378. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  1379. count++;
  1380. if (tmp->rdev) {
  1381. /* Replaced device not technically faulty,
  1382. * but we need to be sure it gets removed
  1383. * and never re-added.
  1384. */
  1385. set_bit(Faulty, &tmp->rdev->flags);
  1386. sysfs_notify_dirent_safe(
  1387. tmp->rdev->sysfs_state);
  1388. }
  1389. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  1390. } else if (tmp->rdev
  1391. && tmp->rdev->recovery_offset == MaxSector
  1392. && !test_bit(Faulty, &tmp->rdev->flags)
  1393. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  1394. count++;
  1395. sysfs_notify_dirent(tmp->rdev->sysfs_state);
  1396. }
  1397. }
  1398. spin_lock_irqsave(&conf->device_lock, flags);
  1399. mddev->degraded -= count;
  1400. spin_unlock_irqrestore(&conf->device_lock, flags);
  1401. print_conf(conf);
  1402. return count;
  1403. }
  1404. static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1405. {
  1406. struct r10conf *conf = mddev->private;
  1407. int err = -EEXIST;
  1408. int mirror;
  1409. int first = 0;
  1410. int last = conf->raid_disks - 1;
  1411. struct request_queue *q = bdev_get_queue(rdev->bdev);
  1412. if (mddev->recovery_cp < MaxSector)
  1413. /* only hot-add to in-sync arrays, as recovery is
  1414. * very different from resync
  1415. */
  1416. return -EBUSY;
  1417. if (rdev->saved_raid_disk < 0 && !enough(conf, -1))
  1418. return -EINVAL;
  1419. if (rdev->raid_disk >= 0)
  1420. first = last = rdev->raid_disk;
  1421. if (q->merge_bvec_fn) {
  1422. set_bit(Unmerged, &rdev->flags);
  1423. mddev->merge_check_needed = 1;
  1424. }
  1425. if (rdev->saved_raid_disk >= first &&
  1426. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  1427. mirror = rdev->saved_raid_disk;
  1428. else
  1429. mirror = first;
  1430. for ( ; mirror <= last ; mirror++) {
  1431. struct mirror_info *p = &conf->mirrors[mirror];
  1432. if (p->recovery_disabled == mddev->recovery_disabled)
  1433. continue;
  1434. if (p->rdev) {
  1435. if (!test_bit(WantReplacement, &p->rdev->flags) ||
  1436. p->replacement != NULL)
  1437. continue;
  1438. clear_bit(In_sync, &rdev->flags);
  1439. set_bit(Replacement, &rdev->flags);
  1440. rdev->raid_disk = mirror;
  1441. err = 0;
  1442. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1443. rdev->data_offset << 9);
  1444. conf->fullsync = 1;
  1445. rcu_assign_pointer(p->replacement, rdev);
  1446. break;
  1447. }
  1448. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1449. rdev->data_offset << 9);
  1450. p->head_position = 0;
  1451. p->recovery_disabled = mddev->recovery_disabled - 1;
  1452. rdev->raid_disk = mirror;
  1453. err = 0;
  1454. if (rdev->saved_raid_disk != mirror)
  1455. conf->fullsync = 1;
  1456. rcu_assign_pointer(p->rdev, rdev);
  1457. break;
  1458. }
  1459. if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
  1460. /* Some requests might not have seen this new
  1461. * merge_bvec_fn. We must wait for them to complete
  1462. * before merging the device fully.
  1463. * First we make sure any code which has tested
  1464. * our function has submitted the request, then
  1465. * we wait for all outstanding requests to complete.
  1466. */
  1467. synchronize_sched();
  1468. freeze_array(conf, 0);
  1469. unfreeze_array(conf);
  1470. clear_bit(Unmerged, &rdev->flags);
  1471. }
  1472. md_integrity_add_rdev(rdev, mddev);
  1473. print_conf(conf);
  1474. return err;
  1475. }
  1476. static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1477. {
  1478. struct r10conf *conf = mddev->private;
  1479. int err = 0;
  1480. int number = rdev->raid_disk;
  1481. struct md_rdev **rdevp;
  1482. struct mirror_info *p = conf->mirrors + number;
  1483. print_conf(conf);
  1484. if (rdev == p->rdev)
  1485. rdevp = &p->rdev;
  1486. else if (rdev == p->replacement)
  1487. rdevp = &p->replacement;
  1488. else
  1489. return 0;
  1490. if (test_bit(In_sync, &rdev->flags) ||
  1491. atomic_read(&rdev->nr_pending)) {
  1492. err = -EBUSY;
  1493. goto abort;
  1494. }
  1495. /* Only remove faulty devices if recovery
  1496. * is not possible.
  1497. */
  1498. if (!test_bit(Faulty, &rdev->flags) &&
  1499. mddev->recovery_disabled != p->recovery_disabled &&
  1500. (!p->replacement || p->replacement == rdev) &&
  1501. enough(conf, -1)) {
  1502. err = -EBUSY;
  1503. goto abort;
  1504. }
  1505. *rdevp = NULL;
  1506. synchronize_rcu();
  1507. if (atomic_read(&rdev->nr_pending)) {
  1508. /* lost the race, try later */
  1509. err = -EBUSY;
  1510. *rdevp = rdev;
  1511. goto abort;
  1512. } else if (p->replacement) {
  1513. /* We must have just cleared 'rdev' */
  1514. p->rdev = p->replacement;
  1515. clear_bit(Replacement, &p->replacement->flags);
  1516. smp_mb(); /* Make sure other CPUs may see both as identical
  1517. * but will never see neither -- if they are careful.
  1518. */
  1519. p->replacement = NULL;
  1520. clear_bit(WantReplacement, &rdev->flags);
  1521. } else
  1522. /* We might have just remove the Replacement as faulty
  1523. * Clear the flag just in case
  1524. */
  1525. clear_bit(WantReplacement, &rdev->flags);
  1526. err = md_integrity_register(mddev);
  1527. abort:
  1528. print_conf(conf);
  1529. return err;
  1530. }
  1531. static void end_sync_read(struct bio *bio, int error)
  1532. {
  1533. struct r10bio *r10_bio = bio->bi_private;
  1534. struct r10conf *conf = r10_bio->mddev->private;
  1535. int d;
  1536. d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
  1537. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1538. set_bit(R10BIO_Uptodate, &r10_bio->state);
  1539. else
  1540. /* The write handler will notice the lack of
  1541. * R10BIO_Uptodate and record any errors etc
  1542. */
  1543. atomic_add(r10_bio->sectors,
  1544. &conf->mirrors[d].rdev->corrected_errors);
  1545. /* for reconstruct, we always reschedule after a read.
  1546. * for resync, only after all reads
  1547. */
  1548. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1549. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  1550. atomic_dec_and_test(&r10_bio->remaining)) {
  1551. /* we have read all the blocks,
  1552. * do the comparison in process context in raid10d
  1553. */
  1554. reschedule_retry(r10_bio);
  1555. }
  1556. }
  1557. static void end_sync_request(struct r10bio *r10_bio)
  1558. {
  1559. struct mddev *mddev = r10_bio->mddev;
  1560. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1561. if (r10_bio->master_bio == NULL) {
  1562. /* the primary of several recovery bios */
  1563. sector_t s = r10_bio->sectors;
  1564. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1565. test_bit(R10BIO_WriteError, &r10_bio->state))
  1566. reschedule_retry(r10_bio);
  1567. else
  1568. put_buf(r10_bio);
  1569. md_done_sync(mddev, s, 1);
  1570. break;
  1571. } else {
  1572. struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
  1573. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1574. test_bit(R10BIO_WriteError, &r10_bio->state))
  1575. reschedule_retry(r10_bio);
  1576. else
  1577. put_buf(r10_bio);
  1578. r10_bio = r10_bio2;
  1579. }
  1580. }
  1581. }
  1582. static void end_sync_write(struct bio *bio, int error)
  1583. {
  1584. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1585. struct r10bio *r10_bio = bio->bi_private;
  1586. struct mddev *mddev = r10_bio->mddev;
  1587. struct r10conf *conf = mddev->private;
  1588. int d;
  1589. sector_t first_bad;
  1590. int bad_sectors;
  1591. int slot;
  1592. int repl;
  1593. struct md_rdev *rdev = NULL;
  1594. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  1595. if (repl)
  1596. rdev = conf->mirrors[d].replacement;
  1597. else
  1598. rdev = conf->mirrors[d].rdev;
  1599. if (!uptodate) {
  1600. if (repl)
  1601. md_error(mddev, rdev);
  1602. else {
  1603. set_bit(WriteErrorSeen, &rdev->flags);
  1604. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1605. set_bit(MD_RECOVERY_NEEDED,
  1606. &rdev->mddev->recovery);
  1607. set_bit(R10BIO_WriteError, &r10_bio->state);
  1608. }
  1609. } else if (is_badblock(rdev,
  1610. r10_bio->devs[slot].addr,
  1611. r10_bio->sectors,
  1612. &first_bad, &bad_sectors))
  1613. set_bit(R10BIO_MadeGood, &r10_bio->state);
  1614. rdev_dec_pending(rdev, mddev);
  1615. end_sync_request(r10_bio);
  1616. }
  1617. /*
  1618. * Note: sync and recover and handled very differently for raid10
  1619. * This code is for resync.
  1620. * For resync, we read through virtual addresses and read all blocks.
  1621. * If there is any error, we schedule a write. The lowest numbered
  1622. * drive is authoritative.
  1623. * However requests come for physical address, so we need to map.
  1624. * For every physical address there are raid_disks/copies virtual addresses,
  1625. * which is always are least one, but is not necessarly an integer.
  1626. * This means that a physical address can span multiple chunks, so we may
  1627. * have to submit multiple io requests for a single sync request.
  1628. */
  1629. /*
  1630. * We check if all blocks are in-sync and only write to blocks that
  1631. * aren't in sync
  1632. */
  1633. static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1634. {
  1635. struct r10conf *conf = mddev->private;
  1636. int i, first;
  1637. struct bio *tbio, *fbio;
  1638. int vcnt;
  1639. atomic_set(&r10_bio->remaining, 1);
  1640. /* find the first device with a block */
  1641. for (i=0; i<conf->copies; i++)
  1642. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1643. break;
  1644. if (i == conf->copies)
  1645. goto done;
  1646. first = i;
  1647. fbio = r10_bio->devs[i].bio;
  1648. vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
  1649. /* now find blocks with errors */
  1650. for (i=0 ; i < conf->copies ; i++) {
  1651. int j, d;
  1652. tbio = r10_bio->devs[i].bio;
  1653. if (tbio->bi_end_io != end_sync_read)
  1654. continue;
  1655. if (i == first)
  1656. continue;
  1657. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1658. /* We know that the bi_io_vec layout is the same for
  1659. * both 'first' and 'i', so we just compare them.
  1660. * All vec entries are PAGE_SIZE;
  1661. */
  1662. for (j = 0; j < vcnt; j++)
  1663. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1664. page_address(tbio->bi_io_vec[j].bv_page),
  1665. fbio->bi_io_vec[j].bv_len))
  1666. break;
  1667. if (j == vcnt)
  1668. continue;
  1669. mddev->resync_mismatches += r10_bio->sectors;
  1670. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1671. /* Don't fix anything. */
  1672. continue;
  1673. }
  1674. /* Ok, we need to write this bio, either to correct an
  1675. * inconsistency or to correct an unreadable block.
  1676. * First we need to fixup bv_offset, bv_len and
  1677. * bi_vecs, as the read request might have corrupted these
  1678. */
  1679. tbio->bi_vcnt = vcnt;
  1680. tbio->bi_size = r10_bio->sectors << 9;
  1681. tbio->bi_idx = 0;
  1682. tbio->bi_phys_segments = 0;
  1683. tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1684. tbio->bi_flags |= 1 << BIO_UPTODATE;
  1685. tbio->bi_next = NULL;
  1686. tbio->bi_rw = WRITE;
  1687. tbio->bi_private = r10_bio;
  1688. tbio->bi_sector = r10_bio->devs[i].addr;
  1689. for (j=0; j < vcnt ; j++) {
  1690. tbio->bi_io_vec[j].bv_offset = 0;
  1691. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1692. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1693. page_address(fbio->bi_io_vec[j].bv_page),
  1694. PAGE_SIZE);
  1695. }
  1696. tbio->bi_end_io = end_sync_write;
  1697. d = r10_bio->devs[i].devnum;
  1698. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1699. atomic_inc(&r10_bio->remaining);
  1700. md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
  1701. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1702. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1703. generic_make_request(tbio);
  1704. }
  1705. /* Now write out to any replacement devices
  1706. * that are active
  1707. */
  1708. for (i = 0; i < conf->copies; i++) {
  1709. int j, d;
  1710. tbio = r10_bio->devs[i].repl_bio;
  1711. if (!tbio || !tbio->bi_end_io)
  1712. continue;
  1713. if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
  1714. && r10_bio->devs[i].bio != fbio)
  1715. for (j = 0; j < vcnt; j++)
  1716. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1717. page_address(fbio->bi_io_vec[j].bv_page),
  1718. PAGE_SIZE);
  1719. d = r10_bio->devs[i].devnum;
  1720. atomic_inc(&r10_bio->remaining);
  1721. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1722. tbio->bi_size >> 9);
  1723. generic_make_request(tbio);
  1724. }
  1725. done:
  1726. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1727. md_done_sync(mddev, r10_bio->sectors, 1);
  1728. put_buf(r10_bio);
  1729. }
  1730. }
  1731. /*
  1732. * Now for the recovery code.
  1733. * Recovery happens across physical sectors.
  1734. * We recover all non-is_sync drives by finding the virtual address of
  1735. * each, and then choose a working drive that also has that virt address.
  1736. * There is a separate r10_bio for each non-in_sync drive.
  1737. * Only the first two slots are in use. The first for reading,
  1738. * The second for writing.
  1739. *
  1740. */
  1741. static void fix_recovery_read_error(struct r10bio *r10_bio)
  1742. {
  1743. /* We got a read error during recovery.
  1744. * We repeat the read in smaller page-sized sections.
  1745. * If a read succeeds, write it to the new device or record
  1746. * a bad block if we cannot.
  1747. * If a read fails, record a bad block on both old and
  1748. * new devices.
  1749. */
  1750. struct mddev *mddev = r10_bio->mddev;
  1751. struct r10conf *conf = mddev->private;
  1752. struct bio *bio = r10_bio->devs[0].bio;
  1753. sector_t sect = 0;
  1754. int sectors = r10_bio->sectors;
  1755. int idx = 0;
  1756. int dr = r10_bio->devs[0].devnum;
  1757. int dw = r10_bio->devs[1].devnum;
  1758. while (sectors) {
  1759. int s = sectors;
  1760. struct md_rdev *rdev;
  1761. sector_t addr;
  1762. int ok;
  1763. if (s > (PAGE_SIZE>>9))
  1764. s = PAGE_SIZE >> 9;
  1765. rdev = conf->mirrors[dr].rdev;
  1766. addr = r10_bio->devs[0].addr + sect,
  1767. ok = sync_page_io(rdev,
  1768. addr,
  1769. s << 9,
  1770. bio->bi_io_vec[idx].bv_page,
  1771. READ, false);
  1772. if (ok) {
  1773. rdev = conf->mirrors[dw].rdev;
  1774. addr = r10_bio->devs[1].addr + sect;
  1775. ok = sync_page_io(rdev,
  1776. addr,
  1777. s << 9,
  1778. bio->bi_io_vec[idx].bv_page,
  1779. WRITE, false);
  1780. if (!ok) {
  1781. set_bit(WriteErrorSeen, &rdev->flags);
  1782. if (!test_and_set_bit(WantReplacement,
  1783. &rdev->flags))
  1784. set_bit(MD_RECOVERY_NEEDED,
  1785. &rdev->mddev->recovery);
  1786. }
  1787. }
  1788. if (!ok) {
  1789. /* We don't worry if we cannot set a bad block -
  1790. * it really is bad so there is no loss in not
  1791. * recording it yet
  1792. */
  1793. rdev_set_badblocks(rdev, addr, s, 0);
  1794. if (rdev != conf->mirrors[dw].rdev) {
  1795. /* need bad block on destination too */
  1796. struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
  1797. addr = r10_bio->devs[1].addr + sect;
  1798. ok = rdev_set_badblocks(rdev2, addr, s, 0);
  1799. if (!ok) {
  1800. /* just abort the recovery */
  1801. printk(KERN_NOTICE
  1802. "md/raid10:%s: recovery aborted"
  1803. " due to read error\n",
  1804. mdname(mddev));
  1805. conf->mirrors[dw].recovery_disabled
  1806. = mddev->recovery_disabled;
  1807. set_bit(MD_RECOVERY_INTR,
  1808. &mddev->recovery);
  1809. break;
  1810. }
  1811. }
  1812. }
  1813. sectors -= s;
  1814. sect += s;
  1815. idx++;
  1816. }
  1817. }
  1818. static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1819. {
  1820. struct r10conf *conf = mddev->private;
  1821. int d;
  1822. struct bio *wbio, *wbio2;
  1823. if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
  1824. fix_recovery_read_error(r10_bio);
  1825. end_sync_request(r10_bio);
  1826. return;
  1827. }
  1828. /*
  1829. * share the pages with the first bio
  1830. * and submit the write request
  1831. */
  1832. d = r10_bio->devs[1].devnum;
  1833. wbio = r10_bio->devs[1].bio;
  1834. wbio2 = r10_bio->devs[1].repl_bio;
  1835. /* Need to test wbio2->bi_end_io before we call
  1836. * generic_make_request as if the former is NULL,
  1837. * the latter is free to free wbio2.
  1838. */
  1839. if (wbio2 && !wbio2->bi_end_io)
  1840. wbio2 = NULL;
  1841. if (wbio->bi_end_io) {
  1842. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1843. md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
  1844. generic_make_request(wbio);
  1845. }
  1846. if (wbio2) {
  1847. atomic_inc(&conf->mirrors[d].replacement->nr_pending);
  1848. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1849. wbio2->bi_size >> 9);
  1850. generic_make_request(wbio2);
  1851. }
  1852. }
  1853. /*
  1854. * Used by fix_read_error() to decay the per rdev read_errors.
  1855. * We halve the read error count for every hour that has elapsed
  1856. * since the last recorded read error.
  1857. *
  1858. */
  1859. static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
  1860. {
  1861. struct timespec cur_time_mon;
  1862. unsigned long hours_since_last;
  1863. unsigned int read_errors = atomic_read(&rdev->read_errors);
  1864. ktime_get_ts(&cur_time_mon);
  1865. if (rdev->last_read_error.tv_sec == 0 &&
  1866. rdev->last_read_error.tv_nsec == 0) {
  1867. /* first time we've seen a read error */
  1868. rdev->last_read_error = cur_time_mon;
  1869. return;
  1870. }
  1871. hours_since_last = (cur_time_mon.tv_sec -
  1872. rdev->last_read_error.tv_sec) / 3600;
  1873. rdev->last_read_error = cur_time_mon;
  1874. /*
  1875. * if hours_since_last is > the number of bits in read_errors
  1876. * just set read errors to 0. We do this to avoid
  1877. * overflowing the shift of read_errors by hours_since_last.
  1878. */
  1879. if (hours_since_last >= 8 * sizeof(read_errors))
  1880. atomic_set(&rdev->read_errors, 0);
  1881. else
  1882. atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
  1883. }
  1884. static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
  1885. int sectors, struct page *page, int rw)
  1886. {
  1887. sector_t first_bad;
  1888. int bad_sectors;
  1889. if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
  1890. && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
  1891. return -1;
  1892. if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
  1893. /* success */
  1894. return 1;
  1895. if (rw == WRITE) {
  1896. set_bit(WriteErrorSeen, &rdev->flags);
  1897. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1898. set_bit(MD_RECOVERY_NEEDED,
  1899. &rdev->mddev->recovery);
  1900. }
  1901. /* need to record an error - either for the block or the device */
  1902. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  1903. md_error(rdev->mddev, rdev);
  1904. return 0;
  1905. }
  1906. /*
  1907. * This is a kernel thread which:
  1908. *
  1909. * 1. Retries failed read operations on working mirrors.
  1910. * 2. Updates the raid superblock when problems encounter.
  1911. * 3. Performs writes following reads for array synchronising.
  1912. */
  1913. static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
  1914. {
  1915. int sect = 0; /* Offset from r10_bio->sector */
  1916. int sectors = r10_bio->sectors;
  1917. struct md_rdev*rdev;
  1918. int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
  1919. int d = r10_bio->devs[r10_bio->read_slot].devnum;
  1920. /* still own a reference to this rdev, so it cannot
  1921. * have been cleared recently.
  1922. */
  1923. rdev = conf->mirrors[d].rdev;
  1924. if (test_bit(Faulty, &rdev->flags))
  1925. /* drive has already been failed, just ignore any
  1926. more fix_read_error() attempts */
  1927. return;
  1928. check_decay_read_errors(mddev, rdev);
  1929. atomic_inc(&rdev->read_errors);
  1930. if (atomic_read(&rdev->read_errors) > max_read_errors) {
  1931. char b[BDEVNAME_SIZE];
  1932. bdevname(rdev->bdev, b);
  1933. printk(KERN_NOTICE
  1934. "md/raid10:%s: %s: Raid device exceeded "
  1935. "read_error threshold [cur %d:max %d]\n",
  1936. mdname(mddev), b,
  1937. atomic_read(&rdev->read_errors), max_read_errors);
  1938. printk(KERN_NOTICE
  1939. "md/raid10:%s: %s: Failing raid device\n",
  1940. mdname(mddev), b);
  1941. md_error(mddev, conf->mirrors[d].rdev);
  1942. r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
  1943. return;
  1944. }
  1945. while(sectors) {
  1946. int s = sectors;
  1947. int sl = r10_bio->read_slot;
  1948. int success = 0;
  1949. int start;
  1950. if (s > (PAGE_SIZE>>9))
  1951. s = PAGE_SIZE >> 9;
  1952. rcu_read_lock();
  1953. do {
  1954. sector_t first_bad;
  1955. int bad_sectors;
  1956. d = r10_bio->devs[sl].devnum;
  1957. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1958. if (rdev &&
  1959. !test_bit(Unmerged, &rdev->flags) &&
  1960. test_bit(In_sync, &rdev->flags) &&
  1961. is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
  1962. &first_bad, &bad_sectors) == 0) {
  1963. atomic_inc(&rdev->nr_pending);
  1964. rcu_read_unlock();
  1965. success = sync_page_io(rdev,
  1966. r10_bio->devs[sl].addr +
  1967. sect,
  1968. s<<9,
  1969. conf->tmppage, READ, false);
  1970. rdev_dec_pending(rdev, mddev);
  1971. rcu_read_lock();
  1972. if (success)
  1973. break;
  1974. }
  1975. sl++;
  1976. if (sl == conf->copies)
  1977. sl = 0;
  1978. } while (!success && sl != r10_bio->read_slot);
  1979. rcu_read_unlock();
  1980. if (!success) {
  1981. /* Cannot read from anywhere, just mark the block
  1982. * as bad on the first device to discourage future
  1983. * reads.
  1984. */
  1985. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  1986. rdev = conf->mirrors[dn].rdev;
  1987. if (!rdev_set_badblocks(
  1988. rdev,
  1989. r10_bio->devs[r10_bio->read_slot].addr
  1990. + sect,
  1991. s, 0)) {
  1992. md_error(mddev, rdev);
  1993. r10_bio->devs[r10_bio->read_slot].bio
  1994. = IO_BLOCKED;
  1995. }
  1996. break;
  1997. }
  1998. start = sl;
  1999. /* write it back and re-read */
  2000. rcu_read_lock();
  2001. while (sl != r10_bio->read_slot) {
  2002. char b[BDEVNAME_SIZE];
  2003. if (sl==0)
  2004. sl = conf->copies;
  2005. sl--;
  2006. d = r10_bio->devs[sl].devnum;
  2007. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2008. if (!rdev ||
  2009. test_bit(Unmerged, &rdev->flags) ||
  2010. !test_bit(In_sync, &rdev->flags))
  2011. continue;
  2012. atomic_inc(&rdev->nr_pending);
  2013. rcu_read_unlock();
  2014. if (r10_sync_page_io(rdev,
  2015. r10_bio->devs[sl].addr +
  2016. sect,
  2017. s, conf->tmppage, WRITE)
  2018. == 0) {
  2019. /* Well, this device is dead */
  2020. printk(KERN_NOTICE
  2021. "md/raid10:%s: read correction "
  2022. "write failed"
  2023. " (%d sectors at %llu on %s)\n",
  2024. mdname(mddev), s,
  2025. (unsigned long long)(
  2026. sect + rdev->data_offset),
  2027. bdevname(rdev->bdev, b));
  2028. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2029. "drive\n",
  2030. mdname(mddev),
  2031. bdevname(rdev->bdev, b));
  2032. }
  2033. rdev_dec_pending(rdev, mddev);
  2034. rcu_read_lock();
  2035. }
  2036. sl = start;
  2037. while (sl != r10_bio->read_slot) {
  2038. char b[BDEVNAME_SIZE];
  2039. if (sl==0)
  2040. sl = conf->copies;
  2041. sl--;
  2042. d = r10_bio->devs[sl].devnum;
  2043. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2044. if (!rdev ||
  2045. !test_bit(In_sync, &rdev->flags))
  2046. continue;
  2047. atomic_inc(&rdev->nr_pending);
  2048. rcu_read_unlock();
  2049. switch (r10_sync_page_io(rdev,
  2050. r10_bio->devs[sl].addr +
  2051. sect,
  2052. s, conf->tmppage,
  2053. READ)) {
  2054. case 0:
  2055. /* Well, this device is dead */
  2056. printk(KERN_NOTICE
  2057. "md/raid10:%s: unable to read back "
  2058. "corrected sectors"
  2059. " (%d sectors at %llu on %s)\n",
  2060. mdname(mddev), s,
  2061. (unsigned long long)(
  2062. sect + rdev->data_offset),
  2063. bdevname(rdev->bdev, b));
  2064. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2065. "drive\n",
  2066. mdname(mddev),
  2067. bdevname(rdev->bdev, b));
  2068. break;
  2069. case 1:
  2070. printk(KERN_INFO
  2071. "md/raid10:%s: read error corrected"
  2072. " (%d sectors at %llu on %s)\n",
  2073. mdname(mddev), s,
  2074. (unsigned long long)(
  2075. sect + rdev->data_offset),
  2076. bdevname(rdev->bdev, b));
  2077. atomic_add(s, &rdev->corrected_errors);
  2078. }
  2079. rdev_dec_pending(rdev, mddev);
  2080. rcu_read_lock();
  2081. }
  2082. rcu_read_unlock();
  2083. sectors -= s;
  2084. sect += s;
  2085. }
  2086. }
  2087. static void bi_complete(struct bio *bio, int error)
  2088. {
  2089. complete((struct completion *)bio->bi_private);
  2090. }
  2091. static int submit_bio_wait(int rw, struct bio *bio)
  2092. {
  2093. struct completion event;
  2094. rw |= REQ_SYNC;
  2095. init_completion(&event);
  2096. bio->bi_private = &event;
  2097. bio->bi_end_io = bi_complete;
  2098. submit_bio(rw, bio);
  2099. wait_for_completion(&event);
  2100. return test_bit(BIO_UPTODATE, &bio->bi_flags);
  2101. }
  2102. static int narrow_write_error(struct r10bio *r10_bio, int i)
  2103. {
  2104. struct bio *bio = r10_bio->master_bio;
  2105. struct mddev *mddev = r10_bio->mddev;
  2106. struct r10conf *conf = mddev->private;
  2107. struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
  2108. /* bio has the data to be written to slot 'i' where
  2109. * we just recently had a write error.
  2110. * We repeatedly clone the bio and trim down to one block,
  2111. * then try the write. Where the write fails we record
  2112. * a bad block.
  2113. * It is conceivable that the bio doesn't exactly align with
  2114. * blocks. We must handle this.
  2115. *
  2116. * We currently own a reference to the rdev.
  2117. */
  2118. int block_sectors;
  2119. sector_t sector;
  2120. int sectors;
  2121. int sect_to_write = r10_bio->sectors;
  2122. int ok = 1;
  2123. if (rdev->badblocks.shift < 0)
  2124. return 0;
  2125. block_sectors = 1 << rdev->badblocks.shift;
  2126. sector = r10_bio->sector;
  2127. sectors = ((r10_bio->sector + block_sectors)
  2128. & ~(sector_t)(block_sectors - 1))
  2129. - sector;
  2130. while (sect_to_write) {
  2131. struct bio *wbio;
  2132. if (sectors > sect_to_write)
  2133. sectors = sect_to_write;
  2134. /* Write at 'sector' for 'sectors' */
  2135. wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  2136. md_trim_bio(wbio, sector - bio->bi_sector, sectors);
  2137. wbio->bi_sector = (r10_bio->devs[i].addr+
  2138. rdev->data_offset+
  2139. (sector - r10_bio->sector));
  2140. wbio->bi_bdev = rdev->bdev;
  2141. if (submit_bio_wait(WRITE, wbio) == 0)
  2142. /* Failure! */
  2143. ok = rdev_set_badblocks(rdev, sector,
  2144. sectors, 0)
  2145. && ok;
  2146. bio_put(wbio);
  2147. sect_to_write -= sectors;
  2148. sector += sectors;
  2149. sectors = block_sectors;
  2150. }
  2151. return ok;
  2152. }
  2153. static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
  2154. {
  2155. int slot = r10_bio->read_slot;
  2156. struct bio *bio;
  2157. struct r10conf *conf = mddev->private;
  2158. struct md_rdev *rdev = r10_bio->devs[slot].rdev;
  2159. char b[BDEVNAME_SIZE];
  2160. unsigned long do_sync;
  2161. int max_sectors;
  2162. /* we got a read error. Maybe the drive is bad. Maybe just
  2163. * the block and we can fix it.
  2164. * We freeze all other IO, and try reading the block from
  2165. * other devices. When we find one, we re-write
  2166. * and check it that fixes the read error.
  2167. * This is all done synchronously while the array is
  2168. * frozen.
  2169. */
  2170. bio = r10_bio->devs[slot].bio;
  2171. bdevname(bio->bi_bdev, b);
  2172. bio_put(bio);
  2173. r10_bio->devs[slot].bio = NULL;
  2174. if (mddev->ro == 0) {
  2175. freeze_array(conf, 1);
  2176. fix_read_error(conf, mddev, r10_bio);
  2177. unfreeze_array(conf);
  2178. } else
  2179. r10_bio->devs[slot].bio = IO_BLOCKED;
  2180. rdev_dec_pending(rdev, mddev);
  2181. read_more:
  2182. rdev = read_balance(conf, r10_bio, &max_sectors);
  2183. if (rdev == NULL) {
  2184. printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
  2185. " read error for block %llu\n",
  2186. mdname(mddev), b,
  2187. (unsigned long long)r10_bio->sector);
  2188. raid_end_bio_io(r10_bio);
  2189. return;
  2190. }
  2191. do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
  2192. slot = r10_bio->read_slot;
  2193. printk_ratelimited(
  2194. KERN_ERR
  2195. "md/raid10:%s: %s: redirecting "
  2196. "sector %llu to another mirror\n",
  2197. mdname(mddev),
  2198. bdevname(rdev->bdev, b),
  2199. (unsigned long long)r10_bio->sector);
  2200. bio = bio_clone_mddev(r10_bio->master_bio,
  2201. GFP_NOIO, mddev);
  2202. md_trim_bio(bio,
  2203. r10_bio->sector - bio->bi_sector,
  2204. max_sectors);
  2205. r10_bio->devs[slot].bio = bio;
  2206. r10_bio->devs[slot].rdev = rdev;
  2207. bio->bi_sector = r10_bio->devs[slot].addr
  2208. + rdev->data_offset;
  2209. bio->bi_bdev = rdev->bdev;
  2210. bio->bi_rw = READ | do_sync;
  2211. bio->bi_private = r10_bio;
  2212. bio->bi_end_io = raid10_end_read_request;
  2213. if (max_sectors < r10_bio->sectors) {
  2214. /* Drat - have to split this up more */
  2215. struct bio *mbio = r10_bio->master_bio;
  2216. int sectors_handled =
  2217. r10_bio->sector + max_sectors
  2218. - mbio->bi_sector;
  2219. r10_bio->sectors = max_sectors;
  2220. spin_lock_irq(&conf->device_lock);
  2221. if (mbio->bi_phys_segments == 0)
  2222. mbio->bi_phys_segments = 2;
  2223. else
  2224. mbio->bi_phys_segments++;
  2225. spin_unlock_irq(&conf->device_lock);
  2226. generic_make_request(bio);
  2227. r10_bio = mempool_alloc(conf->r10bio_pool,
  2228. GFP_NOIO);
  2229. r10_bio->master_bio = mbio;
  2230. r10_bio->sectors = (mbio->bi_size >> 9)
  2231. - sectors_handled;
  2232. r10_bio->state = 0;
  2233. set_bit(R10BIO_ReadError,
  2234. &r10_bio->state);
  2235. r10_bio->mddev = mddev;
  2236. r10_bio->sector = mbio->bi_sector
  2237. + sectors_handled;
  2238. goto read_more;
  2239. } else
  2240. generic_make_request(bio);
  2241. }
  2242. static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
  2243. {
  2244. /* Some sort of write request has finished and it
  2245. * succeeded in writing where we thought there was a
  2246. * bad block. So forget the bad block.
  2247. * Or possibly if failed and we need to record
  2248. * a bad block.
  2249. */
  2250. int m;
  2251. struct md_rdev *rdev;
  2252. if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
  2253. test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  2254. for (m = 0; m < conf->copies; m++) {
  2255. int dev = r10_bio->devs[m].devnum;
  2256. rdev = conf->mirrors[dev].rdev;
  2257. if (r10_bio->devs[m].bio == NULL)
  2258. continue;
  2259. if (test_bit(BIO_UPTODATE,
  2260. &r10_bio->devs[m].bio->bi_flags)) {
  2261. rdev_clear_badblocks(
  2262. rdev,
  2263. r10_bio->devs[m].addr,
  2264. r10_bio->sectors);
  2265. } else {
  2266. if (!rdev_set_badblocks(
  2267. rdev,
  2268. r10_bio->devs[m].addr,
  2269. r10_bio->sectors, 0))
  2270. md_error(conf->mddev, rdev);
  2271. }
  2272. rdev = conf->mirrors[dev].replacement;
  2273. if (r10_bio->devs[m].repl_bio == NULL)
  2274. continue;
  2275. if (test_bit(BIO_UPTODATE,
  2276. &r10_bio->devs[m].repl_bio->bi_flags)) {
  2277. rdev_clear_badblocks(
  2278. rdev,
  2279. r10_bio->devs[m].addr,
  2280. r10_bio->sectors);
  2281. } else {
  2282. if (!rdev_set_badblocks(
  2283. rdev,
  2284. r10_bio->devs[m].addr,
  2285. r10_bio->sectors, 0))
  2286. md_error(conf->mddev, rdev);
  2287. }
  2288. }
  2289. put_buf(r10_bio);
  2290. } else {
  2291. bool fail = false;
  2292. for (m = 0; m < conf->copies; m++) {
  2293. int dev = r10_bio->devs[m].devnum;
  2294. struct bio *bio = r10_bio->devs[m].bio;
  2295. rdev = conf->mirrors[dev].rdev;
  2296. if (bio == IO_MADE_GOOD) {
  2297. rdev_clear_badblocks(
  2298. rdev,
  2299. r10_bio->devs[m].addr,
  2300. r10_bio->sectors);
  2301. rdev_dec_pending(rdev, conf->mddev);
  2302. } else if (bio != NULL &&
  2303. !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  2304. fail = true;
  2305. if (!narrow_write_error(r10_bio, m)) {
  2306. md_error(conf->mddev, rdev);
  2307. set_bit(R10BIO_Degraded,
  2308. &r10_bio->state);
  2309. }
  2310. rdev_dec_pending(rdev, conf->mddev);
  2311. }
  2312. bio = r10_bio->devs[m].repl_bio;
  2313. rdev = conf->mirrors[dev].replacement;
  2314. if (rdev && bio == IO_MADE_GOOD) {
  2315. rdev_clear_badblocks(
  2316. rdev,
  2317. r10_bio->devs[m].addr,
  2318. r10_bio->sectors);
  2319. rdev_dec_pending(rdev, conf->mddev);
  2320. }
  2321. }
  2322. if (fail) {
  2323. spin_lock_irq(&conf->device_lock);
  2324. list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
  2325. spin_unlock_irq(&conf->device_lock);
  2326. md_wakeup_thread(conf->mddev->thread);
  2327. } else {
  2328. if (test_bit(R10BIO_WriteError,
  2329. &r10_bio->state))
  2330. close_write(r10_bio);
  2331. raid_end_bio_io(r10_bio);
  2332. }
  2333. }
  2334. }
  2335. static void raid10d(struct mddev *mddev)
  2336. {
  2337. struct r10bio *r10_bio;
  2338. unsigned long flags;
  2339. struct r10conf *conf = mddev->private;
  2340. struct list_head *head = &conf->retry_list;
  2341. struct blk_plug plug;
  2342. md_check_recovery(mddev);
  2343. if (!list_empty_careful(&conf->bio_end_io_list) &&
  2344. !test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
  2345. LIST_HEAD(tmp);
  2346. spin_lock_irqsave(&conf->device_lock, flags);
  2347. if (!test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
  2348. list_add(&tmp, &conf->bio_end_io_list);
  2349. list_del_init(&conf->bio_end_io_list);
  2350. }
  2351. spin_unlock_irqrestore(&conf->device_lock, flags);
  2352. while (!list_empty(&tmp)) {
  2353. r10_bio = list_first_entry(&conf->bio_end_io_list,
  2354. struct r10bio, retry_list);
  2355. list_del(&r10_bio->retry_list);
  2356. if (mddev->degraded)
  2357. set_bit(R10BIO_Degraded, &r10_bio->state);
  2358. if (test_bit(R10BIO_WriteError,
  2359. &r10_bio->state))
  2360. close_write(r10_bio);
  2361. raid_end_bio_io(r10_bio);
  2362. }
  2363. }
  2364. blk_start_plug(&plug);
  2365. for (;;) {
  2366. flush_pending_writes(conf);
  2367. spin_lock_irqsave(&conf->device_lock, flags);
  2368. if (list_empty(head)) {
  2369. spin_unlock_irqrestore(&conf->device_lock, flags);
  2370. break;
  2371. }
  2372. r10_bio = list_entry(head->prev, struct r10bio, retry_list);
  2373. list_del(head->prev);
  2374. conf->nr_queued--;
  2375. spin_unlock_irqrestore(&conf->device_lock, flags);
  2376. mddev = r10_bio->mddev;
  2377. conf = mddev->private;
  2378. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  2379. test_bit(R10BIO_WriteError, &r10_bio->state))
  2380. handle_write_completed(conf, r10_bio);
  2381. else if (test_bit(R10BIO_IsSync, &r10_bio->state))
  2382. sync_request_write(mddev, r10_bio);
  2383. else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
  2384. recovery_request_write(mddev, r10_bio);
  2385. else if (test_bit(R10BIO_ReadError, &r10_bio->state))
  2386. handle_read_error(mddev, r10_bio);
  2387. else {
  2388. /* just a partial read to be scheduled from a
  2389. * separate context
  2390. */
  2391. int slot = r10_bio->read_slot;
  2392. generic_make_request(r10_bio->devs[slot].bio);
  2393. }
  2394. cond_resched();
  2395. if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
  2396. md_check_recovery(mddev);
  2397. }
  2398. blk_finish_plug(&plug);
  2399. }
  2400. static int init_resync(struct r10conf *conf)
  2401. {
  2402. int buffs;
  2403. int i;
  2404. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  2405. BUG_ON(conf->r10buf_pool);
  2406. conf->have_replacement = 0;
  2407. for (i = 0; i < conf->raid_disks; i++)
  2408. if (conf->mirrors[i].replacement)
  2409. conf->have_replacement = 1;
  2410. conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
  2411. if (!conf->r10buf_pool)
  2412. return -ENOMEM;
  2413. conf->next_resync = 0;
  2414. return 0;
  2415. }
  2416. /*
  2417. * perform a "sync" on one "block"
  2418. *
  2419. * We need to make sure that no normal I/O request - particularly write
  2420. * requests - conflict with active sync requests.
  2421. *
  2422. * This is achieved by tracking pending requests and a 'barrier' concept
  2423. * that can be installed to exclude normal IO requests.
  2424. *
  2425. * Resync and recovery are handled very differently.
  2426. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  2427. *
  2428. * For resync, we iterate over virtual addresses, read all copies,
  2429. * and update if there are differences. If only one copy is live,
  2430. * skip it.
  2431. * For recovery, we iterate over physical addresses, read a good
  2432. * value for each non-in_sync drive, and over-write.
  2433. *
  2434. * So, for recovery we may have several outstanding complex requests for a
  2435. * given address, one for each out-of-sync device. We model this by allocating
  2436. * a number of r10_bio structures, one for each out-of-sync device.
  2437. * As we setup these structures, we collect all bio's together into a list
  2438. * which we then process collectively to add pages, and then process again
  2439. * to pass to generic_make_request.
  2440. *
  2441. * The r10_bio structures are linked using a borrowed master_bio pointer.
  2442. * This link is counted in ->remaining. When the r10_bio that points to NULL
  2443. * has its remaining count decremented to 0, the whole complex operation
  2444. * is complete.
  2445. *
  2446. */
  2447. static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
  2448. int *skipped, int go_faster)
  2449. {
  2450. struct r10conf *conf = mddev->private;
  2451. struct r10bio *r10_bio;
  2452. struct bio *biolist = NULL, *bio;
  2453. sector_t max_sector, nr_sectors;
  2454. int i;
  2455. int max_sync;
  2456. sector_t sync_blocks;
  2457. sector_t sectors_skipped = 0;
  2458. int chunks_skipped = 0;
  2459. if (!conf->r10buf_pool)
  2460. if (init_resync(conf))
  2461. return 0;
  2462. skipped:
  2463. max_sector = mddev->dev_sectors;
  2464. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  2465. max_sector = mddev->resync_max_sectors;
  2466. if (sector_nr >= max_sector) {
  2467. /* If we aborted, we need to abort the
  2468. * sync on the 'current' bitmap chucks (there can
  2469. * be several when recovering multiple devices).
  2470. * as we may have started syncing it but not finished.
  2471. * We can find the current address in
  2472. * mddev->curr_resync, but for recovery,
  2473. * we need to convert that to several
  2474. * virtual addresses.
  2475. */
  2476. if (mddev->curr_resync < max_sector) { /* aborted */
  2477. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  2478. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2479. &sync_blocks, 1);
  2480. else for (i=0; i<conf->raid_disks; i++) {
  2481. sector_t sect =
  2482. raid10_find_virt(conf, mddev->curr_resync, i);
  2483. bitmap_end_sync(mddev->bitmap, sect,
  2484. &sync_blocks, 1);
  2485. }
  2486. } else {
  2487. /* completed sync */
  2488. if ((!mddev->bitmap || conf->fullsync)
  2489. && conf->have_replacement
  2490. && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2491. /* Completed a full sync so the replacements
  2492. * are now fully recovered.
  2493. */
  2494. for (i = 0; i < conf->raid_disks; i++)
  2495. if (conf->mirrors[i].replacement)
  2496. conf->mirrors[i].replacement
  2497. ->recovery_offset
  2498. = MaxSector;
  2499. }
  2500. conf->fullsync = 0;
  2501. }
  2502. bitmap_close_sync(mddev->bitmap);
  2503. close_sync(conf);
  2504. *skipped = 1;
  2505. return sectors_skipped;
  2506. }
  2507. if (chunks_skipped >= conf->raid_disks) {
  2508. /* if there has been nothing to do on any drive,
  2509. * then there is nothing to do at all..
  2510. */
  2511. *skipped = 1;
  2512. return (max_sector - sector_nr) + sectors_skipped;
  2513. }
  2514. if (max_sector > mddev->resync_max)
  2515. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2516. /* make sure whole request will fit in a chunk - if chunks
  2517. * are meaningful
  2518. */
  2519. if (conf->near_copies < conf->raid_disks &&
  2520. max_sector > (sector_nr | conf->chunk_mask))
  2521. max_sector = (sector_nr | conf->chunk_mask) + 1;
  2522. /*
  2523. * If there is non-resync activity waiting for us then
  2524. * put in a delay to throttle resync.
  2525. */
  2526. if (!go_faster && conf->nr_waiting)
  2527. msleep_interruptible(1000);
  2528. /* Again, very different code for resync and recovery.
  2529. * Both must result in an r10bio with a list of bios that
  2530. * have bi_end_io, bi_sector, bi_bdev set,
  2531. * and bi_private set to the r10bio.
  2532. * For recovery, we may actually create several r10bios
  2533. * with 2 bios in each, that correspond to the bios in the main one.
  2534. * In this case, the subordinate r10bios link back through a
  2535. * borrowed master_bio pointer, and the counter in the master
  2536. * includes a ref from each subordinate.
  2537. */
  2538. /* First, we decide what to do and set ->bi_end_io
  2539. * To end_sync_read if we want to read, and
  2540. * end_sync_write if we will want to write.
  2541. */
  2542. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  2543. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2544. /* recovery... the complicated one */
  2545. int j;
  2546. r10_bio = NULL;
  2547. for (i=0 ; i<conf->raid_disks; i++) {
  2548. int still_degraded;
  2549. struct r10bio *rb2;
  2550. sector_t sect;
  2551. int must_sync;
  2552. int any_working;
  2553. struct mirror_info *mirror = &conf->mirrors[i];
  2554. if ((mirror->rdev == NULL ||
  2555. test_bit(In_sync, &mirror->rdev->flags))
  2556. &&
  2557. (mirror->replacement == NULL ||
  2558. test_bit(Faulty,
  2559. &mirror->replacement->flags)))
  2560. continue;
  2561. still_degraded = 0;
  2562. /* want to reconstruct this device */
  2563. rb2 = r10_bio;
  2564. sect = raid10_find_virt(conf, sector_nr, i);
  2565. if (sect >= mddev->resync_max_sectors) {
  2566. /* last stripe is not complete - don't
  2567. * try to recover this sector.
  2568. */
  2569. continue;
  2570. }
  2571. /* Unless we are doing a full sync, or a replacement
  2572. * we only need to recover the block if it is set in
  2573. * the bitmap
  2574. */
  2575. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2576. &sync_blocks, 1);
  2577. if (sync_blocks < max_sync)
  2578. max_sync = sync_blocks;
  2579. if (!must_sync &&
  2580. mirror->replacement == NULL &&
  2581. !conf->fullsync) {
  2582. /* yep, skip the sync_blocks here, but don't assume
  2583. * that there will never be anything to do here
  2584. */
  2585. chunks_skipped = -1;
  2586. continue;
  2587. }
  2588. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2589. raise_barrier(conf, rb2 != NULL);
  2590. atomic_set(&r10_bio->remaining, 0);
  2591. r10_bio->master_bio = (struct bio*)rb2;
  2592. if (rb2)
  2593. atomic_inc(&rb2->remaining);
  2594. r10_bio->mddev = mddev;
  2595. set_bit(R10BIO_IsRecover, &r10_bio->state);
  2596. r10_bio->sector = sect;
  2597. raid10_find_phys(conf, r10_bio);
  2598. /* Need to check if the array will still be
  2599. * degraded
  2600. */
  2601. for (j=0; j<conf->raid_disks; j++)
  2602. if (conf->mirrors[j].rdev == NULL ||
  2603. test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
  2604. still_degraded = 1;
  2605. break;
  2606. }
  2607. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2608. &sync_blocks, still_degraded);
  2609. any_working = 0;
  2610. for (j=0; j<conf->copies;j++) {
  2611. int k;
  2612. int d = r10_bio->devs[j].devnum;
  2613. sector_t from_addr, to_addr;
  2614. struct md_rdev *rdev;
  2615. sector_t sector, first_bad;
  2616. int bad_sectors;
  2617. if (!conf->mirrors[d].rdev ||
  2618. !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
  2619. continue;
  2620. /* This is where we read from */
  2621. any_working = 1;
  2622. rdev = conf->mirrors[d].rdev;
  2623. sector = r10_bio->devs[j].addr;
  2624. if (is_badblock(rdev, sector, max_sync,
  2625. &first_bad, &bad_sectors)) {
  2626. if (first_bad > sector)
  2627. max_sync = first_bad - sector;
  2628. else {
  2629. bad_sectors -= (sector
  2630. - first_bad);
  2631. if (max_sync > bad_sectors)
  2632. max_sync = bad_sectors;
  2633. continue;
  2634. }
  2635. }
  2636. bio = r10_bio->devs[0].bio;
  2637. bio->bi_next = biolist;
  2638. biolist = bio;
  2639. bio->bi_private = r10_bio;
  2640. bio->bi_end_io = end_sync_read;
  2641. bio->bi_rw = READ;
  2642. from_addr = r10_bio->devs[j].addr;
  2643. bio->bi_sector = from_addr + rdev->data_offset;
  2644. bio->bi_bdev = rdev->bdev;
  2645. atomic_inc(&rdev->nr_pending);
  2646. /* and we write to 'i' (if not in_sync) */
  2647. for (k=0; k<conf->copies; k++)
  2648. if (r10_bio->devs[k].devnum == i)
  2649. break;
  2650. BUG_ON(k == conf->copies);
  2651. to_addr = r10_bio->devs[k].addr;
  2652. r10_bio->devs[0].devnum = d;
  2653. r10_bio->devs[0].addr = from_addr;
  2654. r10_bio->devs[1].devnum = i;
  2655. r10_bio->devs[1].addr = to_addr;
  2656. rdev = mirror->rdev;
  2657. if (!test_bit(In_sync, &rdev->flags)) {
  2658. bio = r10_bio->devs[1].bio;
  2659. bio->bi_next = biolist;
  2660. biolist = bio;
  2661. bio->bi_private = r10_bio;
  2662. bio->bi_end_io = end_sync_write;
  2663. bio->bi_rw = WRITE;
  2664. bio->bi_sector = to_addr
  2665. + rdev->data_offset;
  2666. bio->bi_bdev = rdev->bdev;
  2667. atomic_inc(&r10_bio->remaining);
  2668. } else
  2669. r10_bio->devs[1].bio->bi_end_io = NULL;
  2670. /* and maybe write to replacement */
  2671. bio = r10_bio->devs[1].repl_bio;
  2672. if (bio)
  2673. bio->bi_end_io = NULL;
  2674. rdev = mirror->replacement;
  2675. /* Note: if rdev != NULL, then bio
  2676. * cannot be NULL as r10buf_pool_alloc will
  2677. * have allocated it.
  2678. * So the second test here is pointless.
  2679. * But it keeps semantic-checkers happy, and
  2680. * this comment keeps human reviewers
  2681. * happy.
  2682. */
  2683. if (rdev == NULL || bio == NULL ||
  2684. test_bit(Faulty, &rdev->flags))
  2685. break;
  2686. bio->bi_next = biolist;
  2687. biolist = bio;
  2688. bio->bi_private = r10_bio;
  2689. bio->bi_end_io = end_sync_write;
  2690. bio->bi_rw = WRITE;
  2691. bio->bi_sector = to_addr + rdev->data_offset;
  2692. bio->bi_bdev = rdev->bdev;
  2693. atomic_inc(&r10_bio->remaining);
  2694. break;
  2695. }
  2696. if (j == conf->copies) {
  2697. /* Cannot recover, so abort the recovery or
  2698. * record a bad block */
  2699. if (any_working) {
  2700. /* problem is that there are bad blocks
  2701. * on other device(s)
  2702. */
  2703. int k;
  2704. for (k = 0; k < conf->copies; k++)
  2705. if (r10_bio->devs[k].devnum == i)
  2706. break;
  2707. if (!test_bit(In_sync,
  2708. &mirror->rdev->flags)
  2709. && !rdev_set_badblocks(
  2710. mirror->rdev,
  2711. r10_bio->devs[k].addr,
  2712. max_sync, 0))
  2713. any_working = 0;
  2714. if (mirror->replacement &&
  2715. !rdev_set_badblocks(
  2716. mirror->replacement,
  2717. r10_bio->devs[k].addr,
  2718. max_sync, 0))
  2719. any_working = 0;
  2720. }
  2721. if (!any_working) {
  2722. if (!test_and_set_bit(MD_RECOVERY_INTR,
  2723. &mddev->recovery))
  2724. printk(KERN_INFO "md/raid10:%s: insufficient "
  2725. "working devices for recovery.\n",
  2726. mdname(mddev));
  2727. mirror->recovery_disabled
  2728. = mddev->recovery_disabled;
  2729. }
  2730. put_buf(r10_bio);
  2731. if (rb2)
  2732. atomic_dec(&rb2->remaining);
  2733. r10_bio = rb2;
  2734. break;
  2735. }
  2736. }
  2737. if (biolist == NULL) {
  2738. while (r10_bio) {
  2739. struct r10bio *rb2 = r10_bio;
  2740. r10_bio = (struct r10bio*) rb2->master_bio;
  2741. rb2->master_bio = NULL;
  2742. put_buf(rb2);
  2743. }
  2744. goto giveup;
  2745. }
  2746. } else {
  2747. /* resync. Schedule a read for every block at this virt offset */
  2748. int count = 0;
  2749. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  2750. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  2751. &sync_blocks, mddev->degraded) &&
  2752. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
  2753. &mddev->recovery)) {
  2754. /* We can skip this block */
  2755. *skipped = 1;
  2756. return sync_blocks + sectors_skipped;
  2757. }
  2758. if (sync_blocks < max_sync)
  2759. max_sync = sync_blocks;
  2760. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2761. r10_bio->mddev = mddev;
  2762. atomic_set(&r10_bio->remaining, 0);
  2763. raise_barrier(conf, 0);
  2764. conf->next_resync = sector_nr;
  2765. r10_bio->master_bio = NULL;
  2766. r10_bio->sector = sector_nr;
  2767. set_bit(R10BIO_IsSync, &r10_bio->state);
  2768. raid10_find_phys(conf, r10_bio);
  2769. r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
  2770. for (i=0; i<conf->copies; i++) {
  2771. int d = r10_bio->devs[i].devnum;
  2772. sector_t first_bad, sector;
  2773. int bad_sectors;
  2774. if (r10_bio->devs[i].repl_bio)
  2775. r10_bio->devs[i].repl_bio->bi_end_io = NULL;
  2776. bio = r10_bio->devs[i].bio;
  2777. bio->bi_end_io = NULL;
  2778. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2779. if (conf->mirrors[d].rdev == NULL ||
  2780. test_bit(Faulty, &conf->mirrors[d].rdev->flags))
  2781. continue;
  2782. sector = r10_bio->devs[i].addr;
  2783. if (is_badblock(conf->mirrors[d].rdev,
  2784. sector, max_sync,
  2785. &first_bad, &bad_sectors)) {
  2786. if (first_bad > sector)
  2787. max_sync = first_bad - sector;
  2788. else {
  2789. bad_sectors -= (sector - first_bad);
  2790. if (max_sync > bad_sectors)
  2791. max_sync = bad_sectors;
  2792. continue;
  2793. }
  2794. }
  2795. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2796. atomic_inc(&r10_bio->remaining);
  2797. bio->bi_next = biolist;
  2798. biolist = bio;
  2799. bio->bi_private = r10_bio;
  2800. bio->bi_end_io = end_sync_read;
  2801. bio->bi_rw = READ;
  2802. bio->bi_sector = sector +
  2803. conf->mirrors[d].rdev->data_offset;
  2804. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  2805. count++;
  2806. if (conf->mirrors[d].replacement == NULL ||
  2807. test_bit(Faulty,
  2808. &conf->mirrors[d].replacement->flags))
  2809. continue;
  2810. /* Need to set up for writing to the replacement */
  2811. bio = r10_bio->devs[i].repl_bio;
  2812. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2813. sector = r10_bio->devs[i].addr;
  2814. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2815. bio->bi_next = biolist;
  2816. biolist = bio;
  2817. bio->bi_private = r10_bio;
  2818. bio->bi_end_io = end_sync_write;
  2819. bio->bi_rw = WRITE;
  2820. bio->bi_sector = sector +
  2821. conf->mirrors[d].replacement->data_offset;
  2822. bio->bi_bdev = conf->mirrors[d].replacement->bdev;
  2823. count++;
  2824. }
  2825. if (count < 2) {
  2826. for (i=0; i<conf->copies; i++) {
  2827. int d = r10_bio->devs[i].devnum;
  2828. if (r10_bio->devs[i].bio->bi_end_io)
  2829. rdev_dec_pending(conf->mirrors[d].rdev,
  2830. mddev);
  2831. if (r10_bio->devs[i].repl_bio &&
  2832. r10_bio->devs[i].repl_bio->bi_end_io)
  2833. rdev_dec_pending(
  2834. conf->mirrors[d].replacement,
  2835. mddev);
  2836. }
  2837. put_buf(r10_bio);
  2838. biolist = NULL;
  2839. goto giveup;
  2840. }
  2841. }
  2842. for (bio = biolist; bio ; bio=bio->bi_next) {
  2843. bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  2844. if (bio->bi_end_io)
  2845. bio->bi_flags |= 1 << BIO_UPTODATE;
  2846. bio->bi_vcnt = 0;
  2847. bio->bi_idx = 0;
  2848. bio->bi_phys_segments = 0;
  2849. bio->bi_size = 0;
  2850. }
  2851. nr_sectors = 0;
  2852. if (sector_nr + max_sync < max_sector)
  2853. max_sector = sector_nr + max_sync;
  2854. do {
  2855. struct page *page;
  2856. int len = PAGE_SIZE;
  2857. if (sector_nr + (len>>9) > max_sector)
  2858. len = (max_sector - sector_nr) << 9;
  2859. if (len == 0)
  2860. break;
  2861. for (bio= biolist ; bio ; bio=bio->bi_next) {
  2862. struct bio *bio2;
  2863. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  2864. if (bio_add_page(bio, page, len, 0))
  2865. continue;
  2866. /* stop here */
  2867. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  2868. for (bio2 = biolist;
  2869. bio2 && bio2 != bio;
  2870. bio2 = bio2->bi_next) {
  2871. /* remove last page from this bio */
  2872. bio2->bi_vcnt--;
  2873. bio2->bi_size -= len;
  2874. bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
  2875. }
  2876. goto bio_full;
  2877. }
  2878. nr_sectors += len>>9;
  2879. sector_nr += len>>9;
  2880. } while (biolist->bi_vcnt < RESYNC_PAGES);
  2881. bio_full:
  2882. r10_bio->sectors = nr_sectors;
  2883. while (biolist) {
  2884. bio = biolist;
  2885. biolist = biolist->bi_next;
  2886. bio->bi_next = NULL;
  2887. r10_bio = bio->bi_private;
  2888. r10_bio->sectors = nr_sectors;
  2889. if (bio->bi_end_io == end_sync_read) {
  2890. md_sync_acct(bio->bi_bdev, nr_sectors);
  2891. generic_make_request(bio);
  2892. }
  2893. }
  2894. if (sectors_skipped)
  2895. /* pretend they weren't skipped, it makes
  2896. * no important difference in this case
  2897. */
  2898. md_done_sync(mddev, sectors_skipped, 1);
  2899. return sectors_skipped + nr_sectors;
  2900. giveup:
  2901. /* There is nowhere to write, so all non-sync
  2902. * drives must be failed or in resync, all drives
  2903. * have a bad block, so try the next chunk...
  2904. */
  2905. if (sector_nr + max_sync < max_sector)
  2906. max_sector = sector_nr + max_sync;
  2907. sectors_skipped += (max_sector - sector_nr);
  2908. chunks_skipped ++;
  2909. sector_nr = max_sector;
  2910. goto skipped;
  2911. }
  2912. static sector_t
  2913. raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  2914. {
  2915. sector_t size;
  2916. struct r10conf *conf = mddev->private;
  2917. if (!raid_disks)
  2918. raid_disks = conf->raid_disks;
  2919. if (!sectors)
  2920. sectors = conf->dev_sectors;
  2921. size = sectors >> conf->chunk_shift;
  2922. sector_div(size, conf->far_copies);
  2923. size = size * raid_disks;
  2924. sector_div(size, conf->near_copies);
  2925. return size << conf->chunk_shift;
  2926. }
  2927. static void calc_sectors(struct r10conf *conf, sector_t size)
  2928. {
  2929. /* Calculate the number of sectors-per-device that will
  2930. * actually be used, and set conf->dev_sectors and
  2931. * conf->stride
  2932. */
  2933. size = size >> conf->chunk_shift;
  2934. sector_div(size, conf->far_copies);
  2935. size = size * conf->raid_disks;
  2936. sector_div(size, conf->near_copies);
  2937. /* 'size' is now the number of chunks in the array */
  2938. /* calculate "used chunks per device" */
  2939. size = size * conf->copies;
  2940. /* We need to round up when dividing by raid_disks to
  2941. * get the stride size.
  2942. */
  2943. size = DIV_ROUND_UP_SECTOR_T(size, conf->raid_disks);
  2944. conf->dev_sectors = size << conf->chunk_shift;
  2945. if (conf->far_offset)
  2946. conf->stride = 1 << conf->chunk_shift;
  2947. else {
  2948. sector_div(size, conf->far_copies);
  2949. conf->stride = size << conf->chunk_shift;
  2950. }
  2951. }
  2952. static struct r10conf *setup_conf(struct mddev *mddev)
  2953. {
  2954. struct r10conf *conf = NULL;
  2955. int nc, fc, fo;
  2956. int err = -EINVAL;
  2957. if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) ||
  2958. !is_power_of_2(mddev->new_chunk_sectors)) {
  2959. printk(KERN_ERR "md/raid10:%s: chunk size must be "
  2960. "at least PAGE_SIZE(%ld) and be a power of 2.\n",
  2961. mdname(mddev), PAGE_SIZE);
  2962. goto out;
  2963. }
  2964. nc = mddev->new_layout & 255;
  2965. fc = (mddev->new_layout >> 8) & 255;
  2966. fo = mddev->new_layout & (1<<16);
  2967. if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
  2968. (mddev->new_layout >> 17)) {
  2969. printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
  2970. mdname(mddev), mddev->new_layout);
  2971. goto out;
  2972. }
  2973. err = -ENOMEM;
  2974. conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
  2975. if (!conf)
  2976. goto out;
  2977. conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  2978. GFP_KERNEL);
  2979. if (!conf->mirrors)
  2980. goto out;
  2981. conf->tmppage = alloc_page(GFP_KERNEL);
  2982. if (!conf->tmppage)
  2983. goto out;
  2984. conf->raid_disks = mddev->raid_disks;
  2985. conf->near_copies = nc;
  2986. conf->far_copies = fc;
  2987. conf->copies = nc*fc;
  2988. conf->far_offset = fo;
  2989. conf->chunk_mask = mddev->new_chunk_sectors - 1;
  2990. conf->chunk_shift = ffz(~mddev->new_chunk_sectors);
  2991. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  2992. r10bio_pool_free, conf);
  2993. if (!conf->r10bio_pool)
  2994. goto out;
  2995. calc_sectors(conf, mddev->dev_sectors);
  2996. spin_lock_init(&conf->device_lock);
  2997. INIT_LIST_HEAD(&conf->retry_list);
  2998. INIT_LIST_HEAD(&conf->bio_end_io_list);
  2999. spin_lock_init(&conf->resync_lock);
  3000. init_waitqueue_head(&conf->wait_barrier);
  3001. conf->thread = md_register_thread(raid10d, mddev, NULL);
  3002. if (!conf->thread)
  3003. goto out;
  3004. conf->mddev = mddev;
  3005. return conf;
  3006. out:
  3007. printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
  3008. mdname(mddev));
  3009. if (conf) {
  3010. if (conf->r10bio_pool)
  3011. mempool_destroy(conf->r10bio_pool);
  3012. kfree(conf->mirrors);
  3013. safe_put_page(conf->tmppage);
  3014. kfree(conf);
  3015. }
  3016. return ERR_PTR(err);
  3017. }
  3018. static int run(struct mddev *mddev)
  3019. {
  3020. struct r10conf *conf;
  3021. int i, disk_idx, chunk_size;
  3022. struct mirror_info *disk;
  3023. struct md_rdev *rdev;
  3024. sector_t size;
  3025. /*
  3026. * copy the already verified devices into our private RAID10
  3027. * bookkeeping area. [whatever we allocate in run(),
  3028. * should be freed in stop()]
  3029. */
  3030. if (mddev->private == NULL) {
  3031. conf = setup_conf(mddev);
  3032. if (IS_ERR(conf))
  3033. return PTR_ERR(conf);
  3034. mddev->private = conf;
  3035. }
  3036. conf = mddev->private;
  3037. if (!conf)
  3038. goto out;
  3039. mddev->thread = conf->thread;
  3040. conf->thread = NULL;
  3041. chunk_size = mddev->chunk_sectors << 9;
  3042. blk_queue_io_min(mddev->queue, chunk_size);
  3043. if (conf->raid_disks % conf->near_copies)
  3044. blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks);
  3045. else
  3046. blk_queue_io_opt(mddev->queue, chunk_size *
  3047. (conf->raid_disks / conf->near_copies));
  3048. rdev_for_each(rdev, mddev) {
  3049. struct request_queue *q;
  3050. disk_idx = rdev->raid_disk;
  3051. if (disk_idx >= conf->raid_disks
  3052. || disk_idx < 0)
  3053. continue;
  3054. disk = conf->mirrors + disk_idx;
  3055. if (test_bit(Replacement, &rdev->flags)) {
  3056. if (disk->replacement)
  3057. goto out_free_conf;
  3058. disk->replacement = rdev;
  3059. } else {
  3060. if (disk->rdev)
  3061. goto out_free_conf;
  3062. disk->rdev = rdev;
  3063. }
  3064. q = bdev_get_queue(rdev->bdev);
  3065. if (q->merge_bvec_fn)
  3066. mddev->merge_check_needed = 1;
  3067. disk_stack_limits(mddev->gendisk, rdev->bdev,
  3068. rdev->data_offset << 9);
  3069. disk->head_position = 0;
  3070. }
  3071. /* need to check that every block has at least one working mirror */
  3072. if (!enough(conf, -1)) {
  3073. printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
  3074. mdname(mddev));
  3075. goto out_free_conf;
  3076. }
  3077. mddev->degraded = 0;
  3078. for (i = 0; i < conf->raid_disks; i++) {
  3079. disk = conf->mirrors + i;
  3080. if (!disk->rdev && disk->replacement) {
  3081. /* The replacement is all we have - use it */
  3082. disk->rdev = disk->replacement;
  3083. disk->replacement = NULL;
  3084. clear_bit(Replacement, &disk->rdev->flags);
  3085. }
  3086. if (!disk->rdev ||
  3087. !test_bit(In_sync, &disk->rdev->flags)) {
  3088. disk->head_position = 0;
  3089. mddev->degraded++;
  3090. if (disk->rdev)
  3091. conf->fullsync = 1;
  3092. }
  3093. disk->recovery_disabled = mddev->recovery_disabled - 1;
  3094. }
  3095. if (mddev->recovery_cp != MaxSector)
  3096. printk(KERN_NOTICE "md/raid10:%s: not clean"
  3097. " -- starting background reconstruction\n",
  3098. mdname(mddev));
  3099. printk(KERN_INFO
  3100. "md/raid10:%s: active with %d out of %d devices\n",
  3101. mdname(mddev), conf->raid_disks - mddev->degraded,
  3102. conf->raid_disks);
  3103. /*
  3104. * Ok, everything is just fine now
  3105. */
  3106. mddev->dev_sectors = conf->dev_sectors;
  3107. size = raid10_size(mddev, 0, 0);
  3108. md_set_array_sectors(mddev, size);
  3109. mddev->resync_max_sectors = size;
  3110. mddev->queue->backing_dev_info.congested_fn = raid10_congested;
  3111. mddev->queue->backing_dev_info.congested_data = mddev;
  3112. /* Calculate max read-ahead size.
  3113. * We need to readahead at least twice a whole stripe....
  3114. * maybe...
  3115. */
  3116. {
  3117. int stripe = conf->raid_disks *
  3118. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  3119. stripe /= conf->near_copies;
  3120. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  3121. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  3122. }
  3123. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  3124. if (md_integrity_register(mddev))
  3125. goto out_free_conf;
  3126. return 0;
  3127. out_free_conf:
  3128. md_unregister_thread(&mddev->thread);
  3129. if (conf->r10bio_pool)
  3130. mempool_destroy(conf->r10bio_pool);
  3131. safe_put_page(conf->tmppage);
  3132. kfree(conf->mirrors);
  3133. kfree(conf);
  3134. mddev->private = NULL;
  3135. out:
  3136. return -EIO;
  3137. }
  3138. static int stop(struct mddev *mddev)
  3139. {
  3140. struct r10conf *conf = mddev->private;
  3141. raise_barrier(conf, 0);
  3142. lower_barrier(conf);
  3143. md_unregister_thread(&mddev->thread);
  3144. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  3145. if (conf->r10bio_pool)
  3146. mempool_destroy(conf->r10bio_pool);
  3147. kfree(conf->mirrors);
  3148. kfree(conf);
  3149. mddev->private = NULL;
  3150. return 0;
  3151. }
  3152. static void raid10_quiesce(struct mddev *mddev, int state)
  3153. {
  3154. struct r10conf *conf = mddev->private;
  3155. switch(state) {
  3156. case 1:
  3157. raise_barrier(conf, 0);
  3158. break;
  3159. case 0:
  3160. lower_barrier(conf);
  3161. break;
  3162. }
  3163. }
  3164. static int raid10_resize(struct mddev *mddev, sector_t sectors)
  3165. {
  3166. /* Resize of 'far' arrays is not supported.
  3167. * For 'near' and 'offset' arrays we can set the
  3168. * number of sectors used to be an appropriate multiple
  3169. * of the chunk size.
  3170. * For 'offset', this is far_copies*chunksize.
  3171. * For 'near' the multiplier is the LCM of
  3172. * near_copies and raid_disks.
  3173. * So if far_copies > 1 && !far_offset, fail.
  3174. * Else find LCM(raid_disks, near_copy)*far_copies and
  3175. * multiply by chunk_size. Then round to this number.
  3176. * This is mostly done by raid10_size()
  3177. */
  3178. struct r10conf *conf = mddev->private;
  3179. sector_t oldsize, size;
  3180. if (conf->far_copies > 1 && !conf->far_offset)
  3181. return -EINVAL;
  3182. oldsize = raid10_size(mddev, 0, 0);
  3183. size = raid10_size(mddev, sectors, 0);
  3184. md_set_array_sectors(mddev, size);
  3185. if (mddev->array_sectors > size)
  3186. return -EINVAL;
  3187. set_capacity(mddev->gendisk, mddev->array_sectors);
  3188. revalidate_disk(mddev->gendisk);
  3189. if (sectors > mddev->dev_sectors &&
  3190. mddev->recovery_cp > oldsize) {
  3191. mddev->recovery_cp = oldsize;
  3192. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3193. }
  3194. calc_sectors(conf, sectors);
  3195. mddev->dev_sectors = conf->dev_sectors;
  3196. mddev->resync_max_sectors = size;
  3197. return 0;
  3198. }
  3199. static void *raid10_takeover_raid0(struct mddev *mddev)
  3200. {
  3201. struct md_rdev *rdev;
  3202. struct r10conf *conf;
  3203. if (mddev->degraded > 0) {
  3204. printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
  3205. mdname(mddev));
  3206. return ERR_PTR(-EINVAL);
  3207. }
  3208. /* Set new parameters */
  3209. mddev->new_level = 10;
  3210. /* new layout: far_copies = 1, near_copies = 2 */
  3211. mddev->new_layout = (1<<8) + 2;
  3212. mddev->new_chunk_sectors = mddev->chunk_sectors;
  3213. mddev->delta_disks = mddev->raid_disks;
  3214. mddev->raid_disks *= 2;
  3215. /* make sure it will be not marked as dirty */
  3216. mddev->recovery_cp = MaxSector;
  3217. conf = setup_conf(mddev);
  3218. if (!IS_ERR(conf)) {
  3219. rdev_for_each(rdev, mddev)
  3220. if (rdev->raid_disk >= 0)
  3221. rdev->new_raid_disk = rdev->raid_disk * 2;
  3222. conf->barrier = 1;
  3223. }
  3224. return conf;
  3225. }
  3226. static void *raid10_takeover(struct mddev *mddev)
  3227. {
  3228. struct r0conf *raid0_conf;
  3229. /* raid10 can take over:
  3230. * raid0 - providing it has only two drives
  3231. */
  3232. if (mddev->level == 0) {
  3233. /* for raid0 takeover only one zone is supported */
  3234. raid0_conf = mddev->private;
  3235. if (raid0_conf->nr_strip_zones > 1) {
  3236. printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
  3237. " with more than one zone.\n",
  3238. mdname(mddev));
  3239. return ERR_PTR(-EINVAL);
  3240. }
  3241. return raid10_takeover_raid0(mddev);
  3242. }
  3243. return ERR_PTR(-EINVAL);
  3244. }
  3245. static struct md_personality raid10_personality =
  3246. {
  3247. .name = "raid10",
  3248. .level = 10,
  3249. .owner = THIS_MODULE,
  3250. .make_request = make_request,
  3251. .run = run,
  3252. .stop = stop,
  3253. .status = status,
  3254. .error_handler = error,
  3255. .hot_add_disk = raid10_add_disk,
  3256. .hot_remove_disk= raid10_remove_disk,
  3257. .spare_active = raid10_spare_active,
  3258. .sync_request = sync_request,
  3259. .quiesce = raid10_quiesce,
  3260. .size = raid10_size,
  3261. .resize = raid10_resize,
  3262. .takeover = raid10_takeover,
  3263. };
  3264. static int __init raid_init(void)
  3265. {
  3266. return register_md_personality(&raid10_personality);
  3267. }
  3268. static void raid_exit(void)
  3269. {
  3270. unregister_md_personality(&raid10_personality);
  3271. }
  3272. module_init(raid_init);
  3273. module_exit(raid_exit);
  3274. MODULE_LICENSE("GPL");
  3275. MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
  3276. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  3277. MODULE_ALIAS("md-raid10");
  3278. MODULE_ALIAS("md-level-10");
  3279. module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);