raid10.c 84 KB

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