raid10.c 83 KB

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