raid10.c 83 KB

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