raid10.c 76 KB

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