raid10.c 90 KB

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