raid10.c 84 KB

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