raid10.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  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 futher 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 "dm-bio-list.h"
  21. #include <linux/raid/raid10.h>
  22. #include <linux/raid/bitmap.h>
  23. /*
  24. * RAID10 provides a combination of RAID0 and RAID1 functionality.
  25. * The layout of data is defined by
  26. * chunk_size
  27. * raid_disks
  28. * near_copies (stored in low byte of layout)
  29. * far_copies (stored in second byte of layout)
  30. * far_offset (stored in bit 16 of layout )
  31. *
  32. * The data to be stored is divided into chunks using chunksize.
  33. * Each device is divided into far_copies sections.
  34. * In each section, chunks are laid out in a style similar to raid0, but
  35. * near_copies copies of each chunk is stored (each on a different drive).
  36. * The starting device for each section is offset near_copies from the starting
  37. * device of the previous section.
  38. * Thus they are (near_copies*far_copies) of each chunk, and each is on a different
  39. * drive.
  40. * near_copies and far_copies must be at least one, and their product is at most
  41. * raid_disks.
  42. *
  43. * If far_offset is true, then the far_copies are handled a bit differently.
  44. * The copies are still in different stripes, but instead of be very far apart
  45. * on disk, there are adjacent stripes.
  46. */
  47. /*
  48. * Number of guaranteed r10bios in case of extreme VM load:
  49. */
  50. #define NR_RAID10_BIOS 256
  51. static void unplug_slaves(mddev_t *mddev);
  52. static void allow_barrier(conf_t *conf);
  53. static void lower_barrier(conf_t *conf);
  54. static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
  55. {
  56. conf_t *conf = data;
  57. r10bio_t *r10_bio;
  58. int size = offsetof(struct r10bio_s, devs[conf->copies]);
  59. /* allocate a r10bio with room for raid_disks entries in the bios array */
  60. r10_bio = kzalloc(size, gfp_flags);
  61. if (!r10_bio)
  62. unplug_slaves(conf->mddev);
  63. return r10_bio;
  64. }
  65. static void r10bio_pool_free(void *r10_bio, void *data)
  66. {
  67. kfree(r10_bio);
  68. }
  69. #define RESYNC_BLOCK_SIZE (64*1024)
  70. //#define RESYNC_BLOCK_SIZE PAGE_SIZE
  71. #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
  72. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  73. #define RESYNC_WINDOW (2048*1024)
  74. /*
  75. * When performing a resync, we need to read and compare, so
  76. * we need as many pages are there are copies.
  77. * When performing a recovery, we need 2 bios, one for read,
  78. * one for write (we recover only one drive per r10buf)
  79. *
  80. */
  81. static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
  82. {
  83. conf_t *conf = data;
  84. struct page *page;
  85. r10bio_t *r10_bio;
  86. struct bio *bio;
  87. int i, j;
  88. int nalloc;
  89. r10_bio = r10bio_pool_alloc(gfp_flags, conf);
  90. if (!r10_bio) {
  91. unplug_slaves(conf->mddev);
  92. return NULL;
  93. }
  94. if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
  95. nalloc = conf->copies; /* resync */
  96. else
  97. nalloc = 2; /* recovery */
  98. /*
  99. * Allocate bios.
  100. */
  101. for (j = nalloc ; j-- ; ) {
  102. bio = bio_alloc(gfp_flags, RESYNC_PAGES);
  103. if (!bio)
  104. goto out_free_bio;
  105. r10_bio->devs[j].bio = bio;
  106. }
  107. /*
  108. * Allocate RESYNC_PAGES data pages and attach them
  109. * where needed.
  110. */
  111. for (j = 0 ; j < nalloc; j++) {
  112. bio = r10_bio->devs[j].bio;
  113. for (i = 0; i < RESYNC_PAGES; i++) {
  114. page = alloc_page(gfp_flags);
  115. if (unlikely(!page))
  116. goto out_free_pages;
  117. bio->bi_io_vec[i].bv_page = page;
  118. }
  119. }
  120. return r10_bio;
  121. out_free_pages:
  122. for ( ; i > 0 ; i--)
  123. safe_put_page(bio->bi_io_vec[i-1].bv_page);
  124. while (j--)
  125. for (i = 0; i < RESYNC_PAGES ; i++)
  126. safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
  127. j = -1;
  128. out_free_bio:
  129. while ( ++j < nalloc )
  130. bio_put(r10_bio->devs[j].bio);
  131. r10bio_pool_free(r10_bio, conf);
  132. return NULL;
  133. }
  134. static void r10buf_pool_free(void *__r10_bio, void *data)
  135. {
  136. int i;
  137. conf_t *conf = data;
  138. r10bio_t *r10bio = __r10_bio;
  139. int j;
  140. for (j=0; j < conf->copies; j++) {
  141. struct bio *bio = r10bio->devs[j].bio;
  142. if (bio) {
  143. for (i = 0; i < RESYNC_PAGES; i++) {
  144. safe_put_page(bio->bi_io_vec[i].bv_page);
  145. bio->bi_io_vec[i].bv_page = NULL;
  146. }
  147. bio_put(bio);
  148. }
  149. }
  150. r10bio_pool_free(r10bio, conf);
  151. }
  152. static void put_all_bios(conf_t *conf, r10bio_t *r10_bio)
  153. {
  154. int i;
  155. for (i = 0; i < conf->copies; i++) {
  156. struct bio **bio = & r10_bio->devs[i].bio;
  157. if (*bio && *bio != IO_BLOCKED)
  158. bio_put(*bio);
  159. *bio = NULL;
  160. }
  161. }
  162. static void free_r10bio(r10bio_t *r10_bio)
  163. {
  164. conf_t *conf = mddev_to_conf(r10_bio->mddev);
  165. /*
  166. * Wake up any possible resync thread that waits for the device
  167. * to go idle.
  168. */
  169. allow_barrier(conf);
  170. put_all_bios(conf, r10_bio);
  171. mempool_free(r10_bio, conf->r10bio_pool);
  172. }
  173. static void put_buf(r10bio_t *r10_bio)
  174. {
  175. conf_t *conf = mddev_to_conf(r10_bio->mddev);
  176. mempool_free(r10_bio, conf->r10buf_pool);
  177. lower_barrier(conf);
  178. }
  179. static void reschedule_retry(r10bio_t *r10_bio)
  180. {
  181. unsigned long flags;
  182. mddev_t *mddev = r10_bio->mddev;
  183. conf_t *conf = mddev_to_conf(mddev);
  184. spin_lock_irqsave(&conf->device_lock, flags);
  185. list_add(&r10_bio->retry_list, &conf->retry_list);
  186. conf->nr_queued ++;
  187. spin_unlock_irqrestore(&conf->device_lock, flags);
  188. md_wakeup_thread(mddev->thread);
  189. }
  190. /*
  191. * raid_end_bio_io() is called when we have finished servicing a mirrored
  192. * operation and are ready to return a success/failure code to the buffer
  193. * cache layer.
  194. */
  195. static void raid_end_bio_io(r10bio_t *r10_bio)
  196. {
  197. struct bio *bio = r10_bio->master_bio;
  198. bio_endio(bio,
  199. test_bit(R10BIO_Uptodate, &r10_bio->state) ? 0 : -EIO);
  200. free_r10bio(r10_bio);
  201. }
  202. /*
  203. * Update disk head position estimator based on IRQ completion info.
  204. */
  205. static inline void update_head_pos(int slot, r10bio_t *r10_bio)
  206. {
  207. conf_t *conf = mddev_to_conf(r10_bio->mddev);
  208. conf->mirrors[r10_bio->devs[slot].devnum].head_position =
  209. r10_bio->devs[slot].addr + (r10_bio->sectors);
  210. }
  211. static void raid10_end_read_request(struct bio *bio, int error)
  212. {
  213. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  214. r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
  215. int slot, dev;
  216. conf_t *conf = mddev_to_conf(r10_bio->mddev);
  217. slot = r10_bio->read_slot;
  218. dev = r10_bio->devs[slot].devnum;
  219. /*
  220. * this branch is our 'one mirror IO has finished' event handler:
  221. */
  222. update_head_pos(slot, r10_bio);
  223. if (uptodate) {
  224. /*
  225. * Set R10BIO_Uptodate in our master bio, so that
  226. * we will return a good error code to the higher
  227. * levels even if IO on some other mirrored buffer fails.
  228. *
  229. * The 'master' represents the composite IO operation to
  230. * user-side. So if something waits for IO, then it will
  231. * wait for the 'master' bio.
  232. */
  233. set_bit(R10BIO_Uptodate, &r10_bio->state);
  234. raid_end_bio_io(r10_bio);
  235. } else {
  236. /*
  237. * oops, read error:
  238. */
  239. char b[BDEVNAME_SIZE];
  240. if (printk_ratelimit())
  241. printk(KERN_ERR "raid10: %s: rescheduling sector %llu\n",
  242. bdevname(conf->mirrors[dev].rdev->bdev,b), (unsigned long long)r10_bio->sector);
  243. reschedule_retry(r10_bio);
  244. }
  245. rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
  246. }
  247. static void raid10_end_write_request(struct bio *bio, int error)
  248. {
  249. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  250. r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
  251. int slot, dev;
  252. conf_t *conf = mddev_to_conf(r10_bio->mddev);
  253. for (slot = 0; slot < conf->copies; slot++)
  254. if (r10_bio->devs[slot].bio == bio)
  255. break;
  256. dev = r10_bio->devs[slot].devnum;
  257. /*
  258. * this branch is our 'one mirror IO has finished' event handler:
  259. */
  260. if (!uptodate) {
  261. md_error(r10_bio->mddev, conf->mirrors[dev].rdev);
  262. /* an I/O failed, we can't clear the bitmap */
  263. set_bit(R10BIO_Degraded, &r10_bio->state);
  264. } else
  265. /*
  266. * Set R10BIO_Uptodate in our master bio, so that
  267. * we will return a good error code for to the higher
  268. * levels even if IO on some other mirrored buffer fails.
  269. *
  270. * The 'master' represents the composite IO operation to
  271. * user-side. So if something waits for IO, then it will
  272. * wait for the 'master' bio.
  273. */
  274. set_bit(R10BIO_Uptodate, &r10_bio->state);
  275. update_head_pos(slot, r10_bio);
  276. /*
  277. *
  278. * Let's see if all mirrored write operations have finished
  279. * already.
  280. */
  281. if (atomic_dec_and_test(&r10_bio->remaining)) {
  282. /* clear the bitmap if all writes complete successfully */
  283. bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
  284. r10_bio->sectors,
  285. !test_bit(R10BIO_Degraded, &r10_bio->state),
  286. 0);
  287. md_write_end(r10_bio->mddev);
  288. raid_end_bio_io(r10_bio);
  289. }
  290. rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
  291. }
  292. /*
  293. * RAID10 layout manager
  294. * Aswell as the chunksize and raid_disks count, there are two
  295. * parameters: near_copies and far_copies.
  296. * near_copies * far_copies must be <= raid_disks.
  297. * Normally one of these will be 1.
  298. * If both are 1, we get raid0.
  299. * If near_copies == raid_disks, we get raid1.
  300. *
  301. * Chunks are layed out in raid0 style with near_copies copies of the
  302. * first chunk, followed by near_copies copies of the next chunk and
  303. * so on.
  304. * If far_copies > 1, then after 1/far_copies of the array has been assigned
  305. * as described above, we start again with a device offset of near_copies.
  306. * So we effectively have another copy of the whole array further down all
  307. * the drives, but with blocks on different drives.
  308. * With this layout, and block is never stored twice on the one device.
  309. *
  310. * raid10_find_phys finds the sector offset of a given virtual sector
  311. * on each device that it is on.
  312. *
  313. * raid10_find_virt does the reverse mapping, from a device and a
  314. * sector offset to a virtual address
  315. */
  316. static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio)
  317. {
  318. int n,f;
  319. sector_t sector;
  320. sector_t chunk;
  321. sector_t stripe;
  322. int dev;
  323. int slot = 0;
  324. /* now calculate first sector/dev */
  325. chunk = r10bio->sector >> conf->chunk_shift;
  326. sector = r10bio->sector & conf->chunk_mask;
  327. chunk *= conf->near_copies;
  328. stripe = chunk;
  329. dev = sector_div(stripe, conf->raid_disks);
  330. if (conf->far_offset)
  331. stripe *= conf->far_copies;
  332. sector += stripe << conf->chunk_shift;
  333. /* and calculate all the others */
  334. for (n=0; n < conf->near_copies; n++) {
  335. int d = dev;
  336. sector_t s = sector;
  337. r10bio->devs[slot].addr = sector;
  338. r10bio->devs[slot].devnum = d;
  339. slot++;
  340. for (f = 1; f < conf->far_copies; f++) {
  341. d += conf->near_copies;
  342. if (d >= conf->raid_disks)
  343. d -= conf->raid_disks;
  344. s += conf->stride;
  345. r10bio->devs[slot].devnum = d;
  346. r10bio->devs[slot].addr = s;
  347. slot++;
  348. }
  349. dev++;
  350. if (dev >= conf->raid_disks) {
  351. dev = 0;
  352. sector += (conf->chunk_mask + 1);
  353. }
  354. }
  355. BUG_ON(slot != conf->copies);
  356. }
  357. static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev)
  358. {
  359. sector_t offset, chunk, vchunk;
  360. offset = sector & conf->chunk_mask;
  361. if (conf->far_offset) {
  362. int fc;
  363. chunk = sector >> conf->chunk_shift;
  364. fc = sector_div(chunk, conf->far_copies);
  365. dev -= fc * conf->near_copies;
  366. if (dev < 0)
  367. dev += conf->raid_disks;
  368. } else {
  369. while (sector >= conf->stride) {
  370. sector -= conf->stride;
  371. if (dev < conf->near_copies)
  372. dev += conf->raid_disks - conf->near_copies;
  373. else
  374. dev -= conf->near_copies;
  375. }
  376. chunk = sector >> conf->chunk_shift;
  377. }
  378. vchunk = chunk * conf->raid_disks + dev;
  379. sector_div(vchunk, conf->near_copies);
  380. return (vchunk << conf->chunk_shift) + offset;
  381. }
  382. /**
  383. * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
  384. * @q: request queue
  385. * @bio: the buffer head that's been built up so far
  386. * @biovec: the request that could be merged to it.
  387. *
  388. * Return amount of bytes we can accept at this offset
  389. * If near_copies == raid_disk, there are no striping issues,
  390. * but in that case, the function isn't called at all.
  391. */
  392. static int raid10_mergeable_bvec(struct request_queue *q, struct bio *bio,
  393. struct bio_vec *bio_vec)
  394. {
  395. mddev_t *mddev = q->queuedata;
  396. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  397. int max;
  398. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  399. unsigned int bio_sectors = bio->bi_size >> 9;
  400. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  401. if (max < 0) max = 0; /* bio_add cannot handle a negative return */
  402. if (max <= bio_vec->bv_len && bio_sectors == 0)
  403. return bio_vec->bv_len;
  404. else
  405. return max;
  406. }
  407. /*
  408. * This routine returns the disk from which the requested read should
  409. * be done. There is a per-array 'next expected sequential IO' sector
  410. * number - if this matches on the next IO then we use the last disk.
  411. * There is also a per-disk 'last know head position' sector that is
  412. * maintained from IRQ contexts, both the normal and the resync IO
  413. * completion handlers update this position correctly. If there is no
  414. * perfect sequential match then we pick the disk whose head is closest.
  415. *
  416. * If there are 2 mirrors in the same 2 devices, performance degrades
  417. * because position is mirror, not device based.
  418. *
  419. * The rdev for the device selected will have nr_pending incremented.
  420. */
  421. /*
  422. * FIXME: possibly should rethink readbalancing and do it differently
  423. * depending on near_copies / far_copies geometry.
  424. */
  425. static int read_balance(conf_t *conf, r10bio_t *r10_bio)
  426. {
  427. const unsigned long this_sector = r10_bio->sector;
  428. int disk, slot, nslot;
  429. const int sectors = r10_bio->sectors;
  430. sector_t new_distance, current_distance;
  431. mdk_rdev_t *rdev;
  432. raid10_find_phys(conf, r10_bio);
  433. rcu_read_lock();
  434. /*
  435. * Check if we can balance. We can balance on the whole
  436. * device if no resync is going on (recovery is ok), or below
  437. * the resync window. We take the first readable disk when
  438. * above the resync window.
  439. */
  440. if (conf->mddev->recovery_cp < MaxSector
  441. && (this_sector + sectors >= conf->next_resync)) {
  442. /* make sure that disk is operational */
  443. slot = 0;
  444. disk = r10_bio->devs[slot].devnum;
  445. while ((rdev = rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
  446. r10_bio->devs[slot].bio == IO_BLOCKED ||
  447. !test_bit(In_sync, &rdev->flags)) {
  448. slot++;
  449. if (slot == conf->copies) {
  450. slot = 0;
  451. disk = -1;
  452. break;
  453. }
  454. disk = r10_bio->devs[slot].devnum;
  455. }
  456. goto rb_out;
  457. }
  458. /* make sure the disk is operational */
  459. slot = 0;
  460. disk = r10_bio->devs[slot].devnum;
  461. while ((rdev=rcu_dereference(conf->mirrors[disk].rdev)) == NULL ||
  462. r10_bio->devs[slot].bio == IO_BLOCKED ||
  463. !test_bit(In_sync, &rdev->flags)) {
  464. slot ++;
  465. if (slot == conf->copies) {
  466. disk = -1;
  467. goto rb_out;
  468. }
  469. disk = r10_bio->devs[slot].devnum;
  470. }
  471. current_distance = abs(r10_bio->devs[slot].addr -
  472. conf->mirrors[disk].head_position);
  473. /* Find the disk whose head is closest */
  474. for (nslot = slot; nslot < conf->copies; nslot++) {
  475. int ndisk = r10_bio->devs[nslot].devnum;
  476. if ((rdev=rcu_dereference(conf->mirrors[ndisk].rdev)) == NULL ||
  477. r10_bio->devs[nslot].bio == IO_BLOCKED ||
  478. !test_bit(In_sync, &rdev->flags))
  479. continue;
  480. /* This optimisation is debatable, and completely destroys
  481. * sequential read speed for 'far copies' arrays. So only
  482. * keep it for 'near' arrays, and review those later.
  483. */
  484. if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) {
  485. disk = ndisk;
  486. slot = nslot;
  487. break;
  488. }
  489. new_distance = abs(r10_bio->devs[nslot].addr -
  490. conf->mirrors[ndisk].head_position);
  491. if (new_distance < current_distance) {
  492. current_distance = new_distance;
  493. disk = ndisk;
  494. slot = nslot;
  495. }
  496. }
  497. rb_out:
  498. r10_bio->read_slot = slot;
  499. /* conf->next_seq_sect = this_sector + sectors;*/
  500. if (disk >= 0 && (rdev=rcu_dereference(conf->mirrors[disk].rdev))!= NULL)
  501. atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
  502. else
  503. disk = -1;
  504. rcu_read_unlock();
  505. return disk;
  506. }
  507. static void unplug_slaves(mddev_t *mddev)
  508. {
  509. conf_t *conf = mddev_to_conf(mddev);
  510. int i;
  511. rcu_read_lock();
  512. for (i=0; i<mddev->raid_disks; i++) {
  513. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  514. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  515. struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
  516. atomic_inc(&rdev->nr_pending);
  517. rcu_read_unlock();
  518. blk_unplug(r_queue);
  519. rdev_dec_pending(rdev, mddev);
  520. rcu_read_lock();
  521. }
  522. }
  523. rcu_read_unlock();
  524. }
  525. static void raid10_unplug(struct request_queue *q)
  526. {
  527. mddev_t *mddev = q->queuedata;
  528. unplug_slaves(q->queuedata);
  529. md_wakeup_thread(mddev->thread);
  530. }
  531. static int raid10_congested(void *data, int bits)
  532. {
  533. mddev_t *mddev = data;
  534. conf_t *conf = mddev_to_conf(mddev);
  535. int i, ret = 0;
  536. rcu_read_lock();
  537. for (i = 0; i < mddev->raid_disks && ret == 0; i++) {
  538. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  539. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  540. struct request_queue *q = bdev_get_queue(rdev->bdev);
  541. ret |= bdi_congested(&q->backing_dev_info, bits);
  542. }
  543. }
  544. rcu_read_unlock();
  545. return ret;
  546. }
  547. /* Barriers....
  548. * Sometimes we need to suspend IO while we do something else,
  549. * either some resync/recovery, or reconfigure the array.
  550. * To do this we raise a 'barrier'.
  551. * The 'barrier' is a counter that can be raised multiple times
  552. * to count how many activities are happening which preclude
  553. * normal IO.
  554. * We can only raise the barrier if there is no pending IO.
  555. * i.e. if nr_pending == 0.
  556. * We choose only to raise the barrier if no-one is waiting for the
  557. * barrier to go down. This means that as soon as an IO request
  558. * is ready, no other operations which require a barrier will start
  559. * until the IO request has had a chance.
  560. *
  561. * So: regular IO calls 'wait_barrier'. When that returns there
  562. * is no backgroup IO happening, It must arrange to call
  563. * allow_barrier when it has finished its IO.
  564. * backgroup IO calls must call raise_barrier. Once that returns
  565. * there is no normal IO happeing. It must arrange to call
  566. * lower_barrier when the particular background IO completes.
  567. */
  568. #define RESYNC_DEPTH 32
  569. static void raise_barrier(conf_t *conf, int force)
  570. {
  571. BUG_ON(force && !conf->barrier);
  572. spin_lock_irq(&conf->resync_lock);
  573. /* Wait until no block IO is waiting (unless 'force') */
  574. wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
  575. conf->resync_lock,
  576. raid10_unplug(conf->mddev->queue));
  577. /* block any new IO from starting */
  578. conf->barrier++;
  579. /* No wait for all pending IO to complete */
  580. wait_event_lock_irq(conf->wait_barrier,
  581. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  582. conf->resync_lock,
  583. raid10_unplug(conf->mddev->queue));
  584. spin_unlock_irq(&conf->resync_lock);
  585. }
  586. static void lower_barrier(conf_t *conf)
  587. {
  588. unsigned long flags;
  589. spin_lock_irqsave(&conf->resync_lock, flags);
  590. conf->barrier--;
  591. spin_unlock_irqrestore(&conf->resync_lock, flags);
  592. wake_up(&conf->wait_barrier);
  593. }
  594. static void wait_barrier(conf_t *conf)
  595. {
  596. spin_lock_irq(&conf->resync_lock);
  597. if (conf->barrier) {
  598. conf->nr_waiting++;
  599. wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
  600. conf->resync_lock,
  601. raid10_unplug(conf->mddev->queue));
  602. conf->nr_waiting--;
  603. }
  604. conf->nr_pending++;
  605. spin_unlock_irq(&conf->resync_lock);
  606. }
  607. static void allow_barrier(conf_t *conf)
  608. {
  609. unsigned long flags;
  610. spin_lock_irqsave(&conf->resync_lock, flags);
  611. conf->nr_pending--;
  612. spin_unlock_irqrestore(&conf->resync_lock, flags);
  613. wake_up(&conf->wait_barrier);
  614. }
  615. static void freeze_array(conf_t *conf)
  616. {
  617. /* stop syncio and normal IO and wait for everything to
  618. * go quiet.
  619. * We increment barrier and nr_waiting, and then
  620. * wait until barrier+nr_pending match nr_queued+2
  621. */
  622. spin_lock_irq(&conf->resync_lock);
  623. conf->barrier++;
  624. conf->nr_waiting++;
  625. wait_event_lock_irq(conf->wait_barrier,
  626. conf->barrier+conf->nr_pending == conf->nr_queued+2,
  627. conf->resync_lock,
  628. raid10_unplug(conf->mddev->queue));
  629. spin_unlock_irq(&conf->resync_lock);
  630. }
  631. static void unfreeze_array(conf_t *conf)
  632. {
  633. /* reverse the effect of the freeze */
  634. spin_lock_irq(&conf->resync_lock);
  635. conf->barrier--;
  636. conf->nr_waiting--;
  637. wake_up(&conf->wait_barrier);
  638. spin_unlock_irq(&conf->resync_lock);
  639. }
  640. static int make_request(struct request_queue *q, struct bio * bio)
  641. {
  642. mddev_t *mddev = q->queuedata;
  643. conf_t *conf = mddev_to_conf(mddev);
  644. mirror_info_t *mirror;
  645. r10bio_t *r10_bio;
  646. struct bio *read_bio;
  647. int i;
  648. int chunk_sects = conf->chunk_mask + 1;
  649. const int rw = bio_data_dir(bio);
  650. const int do_sync = bio_sync(bio);
  651. struct bio_list bl;
  652. unsigned long flags;
  653. if (unlikely(bio_barrier(bio))) {
  654. bio_endio(bio, -EOPNOTSUPP);
  655. return 0;
  656. }
  657. /* If this request crosses a chunk boundary, we need to
  658. * split it. This will only happen for 1 PAGE (or less) requests.
  659. */
  660. if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9)
  661. > chunk_sects &&
  662. conf->near_copies < conf->raid_disks)) {
  663. struct bio_pair *bp;
  664. /* Sanity check -- queue functions should prevent this happening */
  665. if (bio->bi_vcnt != 1 ||
  666. bio->bi_idx != 0)
  667. goto bad_map;
  668. /* This is a one page bio that upper layers
  669. * refuse to split for us, so we need to split it.
  670. */
  671. bp = bio_split(bio, bio_split_pool,
  672. chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
  673. if (make_request(q, &bp->bio1))
  674. generic_make_request(&bp->bio1);
  675. if (make_request(q, &bp->bio2))
  676. generic_make_request(&bp->bio2);
  677. bio_pair_release(bp);
  678. return 0;
  679. bad_map:
  680. printk("raid10_make_request bug: can't convert block across chunks"
  681. " or bigger than %dk %llu %d\n", chunk_sects/2,
  682. (unsigned long long)bio->bi_sector, bio->bi_size >> 10);
  683. bio_io_error(bio);
  684. return 0;
  685. }
  686. md_write_start(mddev, bio);
  687. /*
  688. * Register the new request and wait if the reconstruction
  689. * thread has put up a bar for new requests.
  690. * Continue immediately if no resync is active currently.
  691. */
  692. wait_barrier(conf);
  693. disk_stat_inc(mddev->gendisk, ios[rw]);
  694. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
  695. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  696. r10_bio->master_bio = bio;
  697. r10_bio->sectors = bio->bi_size >> 9;
  698. r10_bio->mddev = mddev;
  699. r10_bio->sector = bio->bi_sector;
  700. r10_bio->state = 0;
  701. if (rw == READ) {
  702. /*
  703. * read balancing logic:
  704. */
  705. int disk = read_balance(conf, r10_bio);
  706. int slot = r10_bio->read_slot;
  707. if (disk < 0) {
  708. raid_end_bio_io(r10_bio);
  709. return 0;
  710. }
  711. mirror = conf->mirrors + disk;
  712. read_bio = bio_clone(bio, GFP_NOIO);
  713. r10_bio->devs[slot].bio = read_bio;
  714. read_bio->bi_sector = r10_bio->devs[slot].addr +
  715. mirror->rdev->data_offset;
  716. read_bio->bi_bdev = mirror->rdev->bdev;
  717. read_bio->bi_end_io = raid10_end_read_request;
  718. read_bio->bi_rw = READ | do_sync;
  719. read_bio->bi_private = r10_bio;
  720. generic_make_request(read_bio);
  721. return 0;
  722. }
  723. /*
  724. * WRITE:
  725. */
  726. /* first select target devices under spinlock and
  727. * inc refcount on their rdev. Record them by setting
  728. * bios[x] to bio
  729. */
  730. raid10_find_phys(conf, r10_bio);
  731. rcu_read_lock();
  732. for (i = 0; i < conf->copies; i++) {
  733. int d = r10_bio->devs[i].devnum;
  734. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev);
  735. if (rdev &&
  736. !test_bit(Faulty, &rdev->flags)) {
  737. atomic_inc(&rdev->nr_pending);
  738. r10_bio->devs[i].bio = bio;
  739. } else {
  740. r10_bio->devs[i].bio = NULL;
  741. set_bit(R10BIO_Degraded, &r10_bio->state);
  742. }
  743. }
  744. rcu_read_unlock();
  745. atomic_set(&r10_bio->remaining, 0);
  746. bio_list_init(&bl);
  747. for (i = 0; i < conf->copies; i++) {
  748. struct bio *mbio;
  749. int d = r10_bio->devs[i].devnum;
  750. if (!r10_bio->devs[i].bio)
  751. continue;
  752. mbio = bio_clone(bio, GFP_NOIO);
  753. r10_bio->devs[i].bio = mbio;
  754. mbio->bi_sector = r10_bio->devs[i].addr+
  755. conf->mirrors[d].rdev->data_offset;
  756. mbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  757. mbio->bi_end_io = raid10_end_write_request;
  758. mbio->bi_rw = WRITE | do_sync;
  759. mbio->bi_private = r10_bio;
  760. atomic_inc(&r10_bio->remaining);
  761. bio_list_add(&bl, mbio);
  762. }
  763. if (unlikely(!atomic_read(&r10_bio->remaining))) {
  764. /* the array is dead */
  765. md_write_end(mddev);
  766. raid_end_bio_io(r10_bio);
  767. return 0;
  768. }
  769. bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0);
  770. spin_lock_irqsave(&conf->device_lock, flags);
  771. bio_list_merge(&conf->pending_bio_list, &bl);
  772. blk_plug_device(mddev->queue);
  773. spin_unlock_irqrestore(&conf->device_lock, flags);
  774. if (do_sync)
  775. md_wakeup_thread(mddev->thread);
  776. return 0;
  777. }
  778. static void status(struct seq_file *seq, mddev_t *mddev)
  779. {
  780. conf_t *conf = mddev_to_conf(mddev);
  781. int i;
  782. if (conf->near_copies < conf->raid_disks)
  783. seq_printf(seq, " %dK chunks", mddev->chunk_size/1024);
  784. if (conf->near_copies > 1)
  785. seq_printf(seq, " %d near-copies", conf->near_copies);
  786. if (conf->far_copies > 1) {
  787. if (conf->far_offset)
  788. seq_printf(seq, " %d offset-copies", conf->far_copies);
  789. else
  790. seq_printf(seq, " %d far-copies", conf->far_copies);
  791. }
  792. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  793. conf->raid_disks - mddev->degraded);
  794. for (i = 0; i < conf->raid_disks; i++)
  795. seq_printf(seq, "%s",
  796. conf->mirrors[i].rdev &&
  797. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  798. seq_printf(seq, "]");
  799. }
  800. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  801. {
  802. char b[BDEVNAME_SIZE];
  803. conf_t *conf = mddev_to_conf(mddev);
  804. /*
  805. * If it is not operational, then we have already marked it as dead
  806. * else if it is the last working disks, ignore the error, let the
  807. * next level up know.
  808. * else mark the drive as failed
  809. */
  810. if (test_bit(In_sync, &rdev->flags)
  811. && conf->raid_disks-mddev->degraded == 1)
  812. /*
  813. * Don't fail the drive, just return an IO error.
  814. * The test should really be more sophisticated than
  815. * "working_disks == 1", but it isn't critical, and
  816. * can wait until we do more sophisticated "is the drive
  817. * really dead" tests...
  818. */
  819. return;
  820. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  821. unsigned long flags;
  822. spin_lock_irqsave(&conf->device_lock, flags);
  823. mddev->degraded++;
  824. spin_unlock_irqrestore(&conf->device_lock, flags);
  825. /*
  826. * if recovery is running, make sure it aborts.
  827. */
  828. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  829. }
  830. set_bit(Faulty, &rdev->flags);
  831. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  832. printk(KERN_ALERT "raid10: Disk failure on %s, disabling device. \n"
  833. " Operation continuing on %d devices\n",
  834. bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
  835. }
  836. static void print_conf(conf_t *conf)
  837. {
  838. int i;
  839. mirror_info_t *tmp;
  840. printk("RAID10 conf printout:\n");
  841. if (!conf) {
  842. printk("(!conf)\n");
  843. return;
  844. }
  845. printk(" --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
  846. conf->raid_disks);
  847. for (i = 0; i < conf->raid_disks; i++) {
  848. char b[BDEVNAME_SIZE];
  849. tmp = conf->mirrors + i;
  850. if (tmp->rdev)
  851. printk(" disk %d, wo:%d, o:%d, dev:%s\n",
  852. i, !test_bit(In_sync, &tmp->rdev->flags),
  853. !test_bit(Faulty, &tmp->rdev->flags),
  854. bdevname(tmp->rdev->bdev,b));
  855. }
  856. }
  857. static void close_sync(conf_t *conf)
  858. {
  859. wait_barrier(conf);
  860. allow_barrier(conf);
  861. mempool_destroy(conf->r10buf_pool);
  862. conf->r10buf_pool = NULL;
  863. }
  864. /* check if there are enough drives for
  865. * every block to appear on atleast one
  866. */
  867. static int enough(conf_t *conf)
  868. {
  869. int first = 0;
  870. do {
  871. int n = conf->copies;
  872. int cnt = 0;
  873. while (n--) {
  874. if (conf->mirrors[first].rdev)
  875. cnt++;
  876. first = (first+1) % conf->raid_disks;
  877. }
  878. if (cnt == 0)
  879. return 0;
  880. } while (first != 0);
  881. return 1;
  882. }
  883. static int raid10_spare_active(mddev_t *mddev)
  884. {
  885. int i;
  886. conf_t *conf = mddev->private;
  887. mirror_info_t *tmp;
  888. /*
  889. * Find all non-in_sync disks within the RAID10 configuration
  890. * and mark them in_sync
  891. */
  892. for (i = 0; i < conf->raid_disks; i++) {
  893. tmp = conf->mirrors + i;
  894. if (tmp->rdev
  895. && !test_bit(Faulty, &tmp->rdev->flags)
  896. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  897. unsigned long flags;
  898. spin_lock_irqsave(&conf->device_lock, flags);
  899. mddev->degraded--;
  900. spin_unlock_irqrestore(&conf->device_lock, flags);
  901. }
  902. }
  903. print_conf(conf);
  904. return 0;
  905. }
  906. static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  907. {
  908. conf_t *conf = mddev->private;
  909. int found = 0;
  910. int mirror;
  911. mirror_info_t *p;
  912. if (mddev->recovery_cp < MaxSector)
  913. /* only hot-add to in-sync arrays, as recovery is
  914. * very different from resync
  915. */
  916. return 0;
  917. if (!enough(conf))
  918. return 0;
  919. if (rdev->saved_raid_disk >= 0 &&
  920. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  921. mirror = rdev->saved_raid_disk;
  922. else
  923. mirror = 0;
  924. for ( ; mirror < mddev->raid_disks; mirror++)
  925. if ( !(p=conf->mirrors+mirror)->rdev) {
  926. blk_queue_stack_limits(mddev->queue,
  927. rdev->bdev->bd_disk->queue);
  928. /* as we don't honour merge_bvec_fn, we must never risk
  929. * violating it, so limit ->max_sector to one PAGE, as
  930. * a one page request is never in violation.
  931. */
  932. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  933. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  934. mddev->queue->max_sectors = (PAGE_SIZE>>9);
  935. p->head_position = 0;
  936. rdev->raid_disk = mirror;
  937. found = 1;
  938. if (rdev->saved_raid_disk != mirror)
  939. conf->fullsync = 1;
  940. rcu_assign_pointer(p->rdev, rdev);
  941. break;
  942. }
  943. print_conf(conf);
  944. return found;
  945. }
  946. static int raid10_remove_disk(mddev_t *mddev, int number)
  947. {
  948. conf_t *conf = mddev->private;
  949. int err = 0;
  950. mdk_rdev_t *rdev;
  951. mirror_info_t *p = conf->mirrors+ number;
  952. print_conf(conf);
  953. rdev = p->rdev;
  954. if (rdev) {
  955. if (test_bit(In_sync, &rdev->flags) ||
  956. atomic_read(&rdev->nr_pending)) {
  957. err = -EBUSY;
  958. goto abort;
  959. }
  960. p->rdev = NULL;
  961. synchronize_rcu();
  962. if (atomic_read(&rdev->nr_pending)) {
  963. /* lost the race, try later */
  964. err = -EBUSY;
  965. p->rdev = rdev;
  966. }
  967. }
  968. abort:
  969. print_conf(conf);
  970. return err;
  971. }
  972. static void end_sync_read(struct bio *bio, int error)
  973. {
  974. r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
  975. conf_t *conf = mddev_to_conf(r10_bio->mddev);
  976. int i,d;
  977. for (i=0; i<conf->copies; i++)
  978. if (r10_bio->devs[i].bio == bio)
  979. break;
  980. BUG_ON(i == conf->copies);
  981. update_head_pos(i, r10_bio);
  982. d = r10_bio->devs[i].devnum;
  983. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  984. set_bit(R10BIO_Uptodate, &r10_bio->state);
  985. else {
  986. atomic_add(r10_bio->sectors,
  987. &conf->mirrors[d].rdev->corrected_errors);
  988. if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery))
  989. md_error(r10_bio->mddev,
  990. conf->mirrors[d].rdev);
  991. }
  992. /* for reconstruct, we always reschedule after a read.
  993. * for resync, only after all reads
  994. */
  995. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  996. atomic_dec_and_test(&r10_bio->remaining)) {
  997. /* we have read all the blocks,
  998. * do the comparison in process context in raid10d
  999. */
  1000. reschedule_retry(r10_bio);
  1001. }
  1002. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1003. }
  1004. static void end_sync_write(struct bio *bio, int error)
  1005. {
  1006. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1007. r10bio_t * r10_bio = (r10bio_t *)(bio->bi_private);
  1008. mddev_t *mddev = r10_bio->mddev;
  1009. conf_t *conf = mddev_to_conf(mddev);
  1010. int i,d;
  1011. for (i = 0; i < conf->copies; i++)
  1012. if (r10_bio->devs[i].bio == bio)
  1013. break;
  1014. d = r10_bio->devs[i].devnum;
  1015. if (!uptodate)
  1016. md_error(mddev, conf->mirrors[d].rdev);
  1017. update_head_pos(i, r10_bio);
  1018. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1019. if (r10_bio->master_bio == NULL) {
  1020. /* the primary of several recovery bios */
  1021. md_done_sync(mddev, r10_bio->sectors, 1);
  1022. put_buf(r10_bio);
  1023. break;
  1024. } else {
  1025. r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio;
  1026. put_buf(r10_bio);
  1027. r10_bio = r10_bio2;
  1028. }
  1029. }
  1030. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1031. }
  1032. /*
  1033. * Note: sync and recover and handled very differently for raid10
  1034. * This code is for resync.
  1035. * For resync, we read through virtual addresses and read all blocks.
  1036. * If there is any error, we schedule a write. The lowest numbered
  1037. * drive is authoritative.
  1038. * However requests come for physical address, so we need to map.
  1039. * For every physical address there are raid_disks/copies virtual addresses,
  1040. * which is always are least one, but is not necessarly an integer.
  1041. * This means that a physical address can span multiple chunks, so we may
  1042. * have to submit multiple io requests for a single sync request.
  1043. */
  1044. /*
  1045. * We check if all blocks are in-sync and only write to blocks that
  1046. * aren't in sync
  1047. */
  1048. static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio)
  1049. {
  1050. conf_t *conf = mddev_to_conf(mddev);
  1051. int i, first;
  1052. struct bio *tbio, *fbio;
  1053. atomic_set(&r10_bio->remaining, 1);
  1054. /* find the first device with a block */
  1055. for (i=0; i<conf->copies; i++)
  1056. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1057. break;
  1058. if (i == conf->copies)
  1059. goto done;
  1060. first = i;
  1061. fbio = r10_bio->devs[i].bio;
  1062. /* now find blocks with errors */
  1063. for (i=0 ; i < conf->copies ; i++) {
  1064. int j, d;
  1065. int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9);
  1066. tbio = r10_bio->devs[i].bio;
  1067. if (tbio->bi_end_io != end_sync_read)
  1068. continue;
  1069. if (i == first)
  1070. continue;
  1071. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1072. /* We know that the bi_io_vec layout is the same for
  1073. * both 'first' and 'i', so we just compare them.
  1074. * All vec entries are PAGE_SIZE;
  1075. */
  1076. for (j = 0; j < vcnt; j++)
  1077. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1078. page_address(tbio->bi_io_vec[j].bv_page),
  1079. PAGE_SIZE))
  1080. break;
  1081. if (j == vcnt)
  1082. continue;
  1083. mddev->resync_mismatches += r10_bio->sectors;
  1084. }
  1085. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1086. /* Don't fix anything. */
  1087. continue;
  1088. /* Ok, we need to write this bio
  1089. * First we need to fixup bv_offset, bv_len and
  1090. * bi_vecs, as the read request might have corrupted these
  1091. */
  1092. tbio->bi_vcnt = vcnt;
  1093. tbio->bi_size = r10_bio->sectors << 9;
  1094. tbio->bi_idx = 0;
  1095. tbio->bi_phys_segments = 0;
  1096. tbio->bi_hw_segments = 0;
  1097. tbio->bi_hw_front_size = 0;
  1098. tbio->bi_hw_back_size = 0;
  1099. tbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1100. tbio->bi_flags |= 1 << BIO_UPTODATE;
  1101. tbio->bi_next = NULL;
  1102. tbio->bi_rw = WRITE;
  1103. tbio->bi_private = r10_bio;
  1104. tbio->bi_sector = r10_bio->devs[i].addr;
  1105. for (j=0; j < vcnt ; j++) {
  1106. tbio->bi_io_vec[j].bv_offset = 0;
  1107. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1108. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1109. page_address(fbio->bi_io_vec[j].bv_page),
  1110. PAGE_SIZE);
  1111. }
  1112. tbio->bi_end_io = end_sync_write;
  1113. d = r10_bio->devs[i].devnum;
  1114. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1115. atomic_inc(&r10_bio->remaining);
  1116. md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9);
  1117. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1118. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1119. generic_make_request(tbio);
  1120. }
  1121. done:
  1122. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1123. md_done_sync(mddev, r10_bio->sectors, 1);
  1124. put_buf(r10_bio);
  1125. }
  1126. }
  1127. /*
  1128. * Now for the recovery code.
  1129. * Recovery happens across physical sectors.
  1130. * We recover all non-is_sync drives by finding the virtual address of
  1131. * each, and then choose a working drive that also has that virt address.
  1132. * There is a separate r10_bio for each non-in_sync drive.
  1133. * Only the first two slots are in use. The first for reading,
  1134. * The second for writing.
  1135. *
  1136. */
  1137. static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio)
  1138. {
  1139. conf_t *conf = mddev_to_conf(mddev);
  1140. int i, d;
  1141. struct bio *bio, *wbio;
  1142. /* move the pages across to the second bio
  1143. * and submit the write request
  1144. */
  1145. bio = r10_bio->devs[0].bio;
  1146. wbio = r10_bio->devs[1].bio;
  1147. for (i=0; i < wbio->bi_vcnt; i++) {
  1148. struct page *p = bio->bi_io_vec[i].bv_page;
  1149. bio->bi_io_vec[i].bv_page = wbio->bi_io_vec[i].bv_page;
  1150. wbio->bi_io_vec[i].bv_page = p;
  1151. }
  1152. d = r10_bio->devs[1].devnum;
  1153. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1154. md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9);
  1155. if (test_bit(R10BIO_Uptodate, &r10_bio->state))
  1156. generic_make_request(wbio);
  1157. else
  1158. bio_endio(wbio, -EIO);
  1159. }
  1160. /*
  1161. * This is a kernel thread which:
  1162. *
  1163. * 1. Retries failed read operations on working mirrors.
  1164. * 2. Updates the raid superblock when problems encounter.
  1165. * 3. Performs writes following reads for array synchronising.
  1166. */
  1167. static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio)
  1168. {
  1169. int sect = 0; /* Offset from r10_bio->sector */
  1170. int sectors = r10_bio->sectors;
  1171. mdk_rdev_t*rdev;
  1172. while(sectors) {
  1173. int s = sectors;
  1174. int sl = r10_bio->read_slot;
  1175. int success = 0;
  1176. int start;
  1177. if (s > (PAGE_SIZE>>9))
  1178. s = PAGE_SIZE >> 9;
  1179. rcu_read_lock();
  1180. do {
  1181. int d = r10_bio->devs[sl].devnum;
  1182. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1183. if (rdev &&
  1184. test_bit(In_sync, &rdev->flags)) {
  1185. atomic_inc(&rdev->nr_pending);
  1186. rcu_read_unlock();
  1187. success = sync_page_io(rdev->bdev,
  1188. r10_bio->devs[sl].addr +
  1189. sect + rdev->data_offset,
  1190. s<<9,
  1191. conf->tmppage, READ);
  1192. rdev_dec_pending(rdev, mddev);
  1193. rcu_read_lock();
  1194. if (success)
  1195. break;
  1196. }
  1197. sl++;
  1198. if (sl == conf->copies)
  1199. sl = 0;
  1200. } while (!success && sl != r10_bio->read_slot);
  1201. rcu_read_unlock();
  1202. if (!success) {
  1203. /* Cannot read from anywhere -- bye bye array */
  1204. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  1205. md_error(mddev, conf->mirrors[dn].rdev);
  1206. break;
  1207. }
  1208. start = sl;
  1209. /* write it back and re-read */
  1210. rcu_read_lock();
  1211. while (sl != r10_bio->read_slot) {
  1212. int d;
  1213. if (sl==0)
  1214. sl = conf->copies;
  1215. sl--;
  1216. d = r10_bio->devs[sl].devnum;
  1217. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1218. if (rdev &&
  1219. test_bit(In_sync, &rdev->flags)) {
  1220. atomic_inc(&rdev->nr_pending);
  1221. rcu_read_unlock();
  1222. atomic_add(s, &rdev->corrected_errors);
  1223. if (sync_page_io(rdev->bdev,
  1224. r10_bio->devs[sl].addr +
  1225. sect + rdev->data_offset,
  1226. s<<9, conf->tmppage, WRITE)
  1227. == 0)
  1228. /* Well, this device is dead */
  1229. md_error(mddev, rdev);
  1230. rdev_dec_pending(rdev, mddev);
  1231. rcu_read_lock();
  1232. }
  1233. }
  1234. sl = start;
  1235. while (sl != r10_bio->read_slot) {
  1236. int d;
  1237. if (sl==0)
  1238. sl = conf->copies;
  1239. sl--;
  1240. d = r10_bio->devs[sl].devnum;
  1241. rdev = rcu_dereference(conf->mirrors[d].rdev);
  1242. if (rdev &&
  1243. test_bit(In_sync, &rdev->flags)) {
  1244. char b[BDEVNAME_SIZE];
  1245. atomic_inc(&rdev->nr_pending);
  1246. rcu_read_unlock();
  1247. if (sync_page_io(rdev->bdev,
  1248. r10_bio->devs[sl].addr +
  1249. sect + rdev->data_offset,
  1250. s<<9, conf->tmppage, READ) == 0)
  1251. /* Well, this device is dead */
  1252. md_error(mddev, rdev);
  1253. else
  1254. printk(KERN_INFO
  1255. "raid10:%s: read error corrected"
  1256. " (%d sectors at %llu on %s)\n",
  1257. mdname(mddev), s,
  1258. (unsigned long long)(sect+
  1259. rdev->data_offset),
  1260. bdevname(rdev->bdev, b));
  1261. rdev_dec_pending(rdev, mddev);
  1262. rcu_read_lock();
  1263. }
  1264. }
  1265. rcu_read_unlock();
  1266. sectors -= s;
  1267. sect += s;
  1268. }
  1269. }
  1270. static void raid10d(mddev_t *mddev)
  1271. {
  1272. r10bio_t *r10_bio;
  1273. struct bio *bio;
  1274. unsigned long flags;
  1275. conf_t *conf = mddev_to_conf(mddev);
  1276. struct list_head *head = &conf->retry_list;
  1277. int unplug=0;
  1278. mdk_rdev_t *rdev;
  1279. md_check_recovery(mddev);
  1280. for (;;) {
  1281. char b[BDEVNAME_SIZE];
  1282. spin_lock_irqsave(&conf->device_lock, flags);
  1283. if (conf->pending_bio_list.head) {
  1284. bio = bio_list_get(&conf->pending_bio_list);
  1285. blk_remove_plug(mddev->queue);
  1286. spin_unlock_irqrestore(&conf->device_lock, flags);
  1287. /* flush any pending bitmap writes to disk before proceeding w/ I/O */
  1288. bitmap_unplug(mddev->bitmap);
  1289. while (bio) { /* submit pending writes */
  1290. struct bio *next = bio->bi_next;
  1291. bio->bi_next = NULL;
  1292. generic_make_request(bio);
  1293. bio = next;
  1294. }
  1295. unplug = 1;
  1296. continue;
  1297. }
  1298. if (list_empty(head))
  1299. break;
  1300. r10_bio = list_entry(head->prev, r10bio_t, retry_list);
  1301. list_del(head->prev);
  1302. conf->nr_queued--;
  1303. spin_unlock_irqrestore(&conf->device_lock, flags);
  1304. mddev = r10_bio->mddev;
  1305. conf = mddev_to_conf(mddev);
  1306. if (test_bit(R10BIO_IsSync, &r10_bio->state)) {
  1307. sync_request_write(mddev, r10_bio);
  1308. unplug = 1;
  1309. } else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  1310. recovery_request_write(mddev, r10_bio);
  1311. unplug = 1;
  1312. } else {
  1313. int mirror;
  1314. /* we got a read error. Maybe the drive is bad. Maybe just
  1315. * the block and we can fix it.
  1316. * We freeze all other IO, and try reading the block from
  1317. * other devices. When we find one, we re-write
  1318. * and check it that fixes the read error.
  1319. * This is all done synchronously while the array is
  1320. * frozen.
  1321. */
  1322. if (mddev->ro == 0) {
  1323. freeze_array(conf);
  1324. fix_read_error(conf, mddev, r10_bio);
  1325. unfreeze_array(conf);
  1326. }
  1327. bio = r10_bio->devs[r10_bio->read_slot].bio;
  1328. r10_bio->devs[r10_bio->read_slot].bio =
  1329. mddev->ro ? IO_BLOCKED : NULL;
  1330. mirror = read_balance(conf, r10_bio);
  1331. if (mirror == -1) {
  1332. printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
  1333. " read error for block %llu\n",
  1334. bdevname(bio->bi_bdev,b),
  1335. (unsigned long long)r10_bio->sector);
  1336. raid_end_bio_io(r10_bio);
  1337. bio_put(bio);
  1338. } else {
  1339. const int do_sync = bio_sync(r10_bio->master_bio);
  1340. bio_put(bio);
  1341. rdev = conf->mirrors[mirror].rdev;
  1342. if (printk_ratelimit())
  1343. printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
  1344. " another mirror\n",
  1345. bdevname(rdev->bdev,b),
  1346. (unsigned long long)r10_bio->sector);
  1347. bio = bio_clone(r10_bio->master_bio, GFP_NOIO);
  1348. r10_bio->devs[r10_bio->read_slot].bio = bio;
  1349. bio->bi_sector = r10_bio->devs[r10_bio->read_slot].addr
  1350. + rdev->data_offset;
  1351. bio->bi_bdev = rdev->bdev;
  1352. bio->bi_rw = READ | do_sync;
  1353. bio->bi_private = r10_bio;
  1354. bio->bi_end_io = raid10_end_read_request;
  1355. unplug = 1;
  1356. generic_make_request(bio);
  1357. }
  1358. }
  1359. }
  1360. spin_unlock_irqrestore(&conf->device_lock, flags);
  1361. if (unplug)
  1362. unplug_slaves(mddev);
  1363. }
  1364. static int init_resync(conf_t *conf)
  1365. {
  1366. int buffs;
  1367. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  1368. BUG_ON(conf->r10buf_pool);
  1369. conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
  1370. if (!conf->r10buf_pool)
  1371. return -ENOMEM;
  1372. conf->next_resync = 0;
  1373. return 0;
  1374. }
  1375. /*
  1376. * perform a "sync" on one "block"
  1377. *
  1378. * We need to make sure that no normal I/O request - particularly write
  1379. * requests - conflict with active sync requests.
  1380. *
  1381. * This is achieved by tracking pending requests and a 'barrier' concept
  1382. * that can be installed to exclude normal IO requests.
  1383. *
  1384. * Resync and recovery are handled very differently.
  1385. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  1386. *
  1387. * For resync, we iterate over virtual addresses, read all copies,
  1388. * and update if there are differences. If only one copy is live,
  1389. * skip it.
  1390. * For recovery, we iterate over physical addresses, read a good
  1391. * value for each non-in_sync drive, and over-write.
  1392. *
  1393. * So, for recovery we may have several outstanding complex requests for a
  1394. * given address, one for each out-of-sync device. We model this by allocating
  1395. * a number of r10_bio structures, one for each out-of-sync device.
  1396. * As we setup these structures, we collect all bio's together into a list
  1397. * which we then process collectively to add pages, and then process again
  1398. * to pass to generic_make_request.
  1399. *
  1400. * The r10_bio structures are linked using a borrowed master_bio pointer.
  1401. * This link is counted in ->remaining. When the r10_bio that points to NULL
  1402. * has its remaining count decremented to 0, the whole complex operation
  1403. * is complete.
  1404. *
  1405. */
  1406. static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  1407. {
  1408. conf_t *conf = mddev_to_conf(mddev);
  1409. r10bio_t *r10_bio;
  1410. struct bio *biolist = NULL, *bio;
  1411. sector_t max_sector, nr_sectors;
  1412. int disk;
  1413. int i;
  1414. int max_sync;
  1415. int sync_blocks;
  1416. sector_t sectors_skipped = 0;
  1417. int chunks_skipped = 0;
  1418. if (!conf->r10buf_pool)
  1419. if (init_resync(conf))
  1420. return 0;
  1421. skipped:
  1422. max_sector = mddev->size << 1;
  1423. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  1424. max_sector = mddev->resync_max_sectors;
  1425. if (sector_nr >= max_sector) {
  1426. /* If we aborted, we need to abort the
  1427. * sync on the 'current' bitmap chucks (there can
  1428. * be several when recovering multiple devices).
  1429. * as we may have started syncing it but not finished.
  1430. * We can find the current address in
  1431. * mddev->curr_resync, but for recovery,
  1432. * we need to convert that to several
  1433. * virtual addresses.
  1434. */
  1435. if (mddev->curr_resync < max_sector) { /* aborted */
  1436. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  1437. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1438. &sync_blocks, 1);
  1439. else for (i=0; i<conf->raid_disks; i++) {
  1440. sector_t sect =
  1441. raid10_find_virt(conf, mddev->curr_resync, i);
  1442. bitmap_end_sync(mddev->bitmap, sect,
  1443. &sync_blocks, 1);
  1444. }
  1445. } else /* completed sync */
  1446. conf->fullsync = 0;
  1447. bitmap_close_sync(mddev->bitmap);
  1448. close_sync(conf);
  1449. *skipped = 1;
  1450. return sectors_skipped;
  1451. }
  1452. if (chunks_skipped >= conf->raid_disks) {
  1453. /* if there has been nothing to do on any drive,
  1454. * then there is nothing to do at all..
  1455. */
  1456. *skipped = 1;
  1457. return (max_sector - sector_nr) + sectors_skipped;
  1458. }
  1459. /* make sure whole request will fit in a chunk - if chunks
  1460. * are meaningful
  1461. */
  1462. if (conf->near_copies < conf->raid_disks &&
  1463. max_sector > (sector_nr | conf->chunk_mask))
  1464. max_sector = (sector_nr | conf->chunk_mask) + 1;
  1465. /*
  1466. * If there is non-resync activity waiting for us then
  1467. * put in a delay to throttle resync.
  1468. */
  1469. if (!go_faster && conf->nr_waiting)
  1470. msleep_interruptible(1000);
  1471. /* Again, very different code for resync and recovery.
  1472. * Both must result in an r10bio with a list of bios that
  1473. * have bi_end_io, bi_sector, bi_bdev set,
  1474. * and bi_private set to the r10bio.
  1475. * For recovery, we may actually create several r10bios
  1476. * with 2 bios in each, that correspond to the bios in the main one.
  1477. * In this case, the subordinate r10bios link back through a
  1478. * borrowed master_bio pointer, and the counter in the master
  1479. * includes a ref from each subordinate.
  1480. */
  1481. /* First, we decide what to do and set ->bi_end_io
  1482. * To end_sync_read if we want to read, and
  1483. * end_sync_write if we will want to write.
  1484. */
  1485. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  1486. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  1487. /* recovery... the complicated one */
  1488. int i, j, k;
  1489. r10_bio = NULL;
  1490. for (i=0 ; i<conf->raid_disks; i++)
  1491. if (conf->mirrors[i].rdev &&
  1492. !test_bit(In_sync, &conf->mirrors[i].rdev->flags)) {
  1493. int still_degraded = 0;
  1494. /* want to reconstruct this device */
  1495. r10bio_t *rb2 = r10_bio;
  1496. sector_t sect = raid10_find_virt(conf, sector_nr, i);
  1497. int must_sync;
  1498. /* Unless we are doing a full sync, we only need
  1499. * to recover the block if it is set in the bitmap
  1500. */
  1501. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  1502. &sync_blocks, 1);
  1503. if (sync_blocks < max_sync)
  1504. max_sync = sync_blocks;
  1505. if (!must_sync &&
  1506. !conf->fullsync) {
  1507. /* yep, skip the sync_blocks here, but don't assume
  1508. * that there will never be anything to do here
  1509. */
  1510. chunks_skipped = -1;
  1511. continue;
  1512. }
  1513. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  1514. raise_barrier(conf, rb2 != NULL);
  1515. atomic_set(&r10_bio->remaining, 0);
  1516. r10_bio->master_bio = (struct bio*)rb2;
  1517. if (rb2)
  1518. atomic_inc(&rb2->remaining);
  1519. r10_bio->mddev = mddev;
  1520. set_bit(R10BIO_IsRecover, &r10_bio->state);
  1521. r10_bio->sector = sect;
  1522. raid10_find_phys(conf, r10_bio);
  1523. /* Need to check if this section will still be
  1524. * degraded
  1525. */
  1526. for (j=0; j<conf->copies;j++) {
  1527. int d = r10_bio->devs[j].devnum;
  1528. if (conf->mirrors[d].rdev == NULL ||
  1529. test_bit(Faulty, &conf->mirrors[d].rdev->flags)) {
  1530. still_degraded = 1;
  1531. break;
  1532. }
  1533. }
  1534. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  1535. &sync_blocks, still_degraded);
  1536. for (j=0; j<conf->copies;j++) {
  1537. int d = r10_bio->devs[j].devnum;
  1538. if (conf->mirrors[d].rdev &&
  1539. test_bit(In_sync, &conf->mirrors[d].rdev->flags)) {
  1540. /* This is where we read from */
  1541. bio = r10_bio->devs[0].bio;
  1542. bio->bi_next = biolist;
  1543. biolist = bio;
  1544. bio->bi_private = r10_bio;
  1545. bio->bi_end_io = end_sync_read;
  1546. bio->bi_rw = READ;
  1547. bio->bi_sector = r10_bio->devs[j].addr +
  1548. conf->mirrors[d].rdev->data_offset;
  1549. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1550. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1551. atomic_inc(&r10_bio->remaining);
  1552. /* and we write to 'i' */
  1553. for (k=0; k<conf->copies; k++)
  1554. if (r10_bio->devs[k].devnum == i)
  1555. break;
  1556. BUG_ON(k == conf->copies);
  1557. bio = r10_bio->devs[1].bio;
  1558. bio->bi_next = biolist;
  1559. biolist = bio;
  1560. bio->bi_private = r10_bio;
  1561. bio->bi_end_io = end_sync_write;
  1562. bio->bi_rw = WRITE;
  1563. bio->bi_sector = r10_bio->devs[k].addr +
  1564. conf->mirrors[i].rdev->data_offset;
  1565. bio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1566. r10_bio->devs[0].devnum = d;
  1567. r10_bio->devs[1].devnum = i;
  1568. break;
  1569. }
  1570. }
  1571. if (j == conf->copies) {
  1572. /* Cannot recover, so abort the recovery */
  1573. put_buf(r10_bio);
  1574. r10_bio = rb2;
  1575. if (!test_and_set_bit(MD_RECOVERY_ERR, &mddev->recovery))
  1576. printk(KERN_INFO "raid10: %s: insufficient working devices for recovery.\n",
  1577. mdname(mddev));
  1578. break;
  1579. }
  1580. }
  1581. if (biolist == NULL) {
  1582. while (r10_bio) {
  1583. r10bio_t *rb2 = r10_bio;
  1584. r10_bio = (r10bio_t*) rb2->master_bio;
  1585. rb2->master_bio = NULL;
  1586. put_buf(rb2);
  1587. }
  1588. goto giveup;
  1589. }
  1590. } else {
  1591. /* resync. Schedule a read for every block at this virt offset */
  1592. int count = 0;
  1593. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  1594. &sync_blocks, mddev->degraded) &&
  1595. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  1596. /* We can skip this block */
  1597. *skipped = 1;
  1598. return sync_blocks + sectors_skipped;
  1599. }
  1600. if (sync_blocks < max_sync)
  1601. max_sync = sync_blocks;
  1602. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  1603. r10_bio->mddev = mddev;
  1604. atomic_set(&r10_bio->remaining, 0);
  1605. raise_barrier(conf, 0);
  1606. conf->next_resync = sector_nr;
  1607. r10_bio->master_bio = NULL;
  1608. r10_bio->sector = sector_nr;
  1609. set_bit(R10BIO_IsSync, &r10_bio->state);
  1610. raid10_find_phys(conf, r10_bio);
  1611. r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1;
  1612. for (i=0; i<conf->copies; i++) {
  1613. int d = r10_bio->devs[i].devnum;
  1614. bio = r10_bio->devs[i].bio;
  1615. bio->bi_end_io = NULL;
  1616. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  1617. if (conf->mirrors[d].rdev == NULL ||
  1618. test_bit(Faulty, &conf->mirrors[d].rdev->flags))
  1619. continue;
  1620. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1621. atomic_inc(&r10_bio->remaining);
  1622. bio->bi_next = biolist;
  1623. biolist = bio;
  1624. bio->bi_private = r10_bio;
  1625. bio->bi_end_io = end_sync_read;
  1626. bio->bi_rw = READ;
  1627. bio->bi_sector = r10_bio->devs[i].addr +
  1628. conf->mirrors[d].rdev->data_offset;
  1629. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1630. count++;
  1631. }
  1632. if (count < 2) {
  1633. for (i=0; i<conf->copies; i++) {
  1634. int d = r10_bio->devs[i].devnum;
  1635. if (r10_bio->devs[i].bio->bi_end_io)
  1636. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1637. }
  1638. put_buf(r10_bio);
  1639. biolist = NULL;
  1640. goto giveup;
  1641. }
  1642. }
  1643. for (bio = biolist; bio ; bio=bio->bi_next) {
  1644. bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1645. if (bio->bi_end_io)
  1646. bio->bi_flags |= 1 << BIO_UPTODATE;
  1647. bio->bi_vcnt = 0;
  1648. bio->bi_idx = 0;
  1649. bio->bi_phys_segments = 0;
  1650. bio->bi_hw_segments = 0;
  1651. bio->bi_size = 0;
  1652. }
  1653. nr_sectors = 0;
  1654. if (sector_nr + max_sync < max_sector)
  1655. max_sector = sector_nr + max_sync;
  1656. do {
  1657. struct page *page;
  1658. int len = PAGE_SIZE;
  1659. disk = 0;
  1660. if (sector_nr + (len>>9) > max_sector)
  1661. len = (max_sector - sector_nr) << 9;
  1662. if (len == 0)
  1663. break;
  1664. for (bio= biolist ; bio ; bio=bio->bi_next) {
  1665. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  1666. if (bio_add_page(bio, page, len, 0) == 0) {
  1667. /* stop here */
  1668. struct bio *bio2;
  1669. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  1670. for (bio2 = biolist; bio2 && bio2 != bio; bio2 = bio2->bi_next) {
  1671. /* remove last page from this bio */
  1672. bio2->bi_vcnt--;
  1673. bio2->bi_size -= len;
  1674. bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
  1675. }
  1676. goto bio_full;
  1677. }
  1678. disk = i;
  1679. }
  1680. nr_sectors += len>>9;
  1681. sector_nr += len>>9;
  1682. } while (biolist->bi_vcnt < RESYNC_PAGES);
  1683. bio_full:
  1684. r10_bio->sectors = nr_sectors;
  1685. while (biolist) {
  1686. bio = biolist;
  1687. biolist = biolist->bi_next;
  1688. bio->bi_next = NULL;
  1689. r10_bio = bio->bi_private;
  1690. r10_bio->sectors = nr_sectors;
  1691. if (bio->bi_end_io == end_sync_read) {
  1692. md_sync_acct(bio->bi_bdev, nr_sectors);
  1693. generic_make_request(bio);
  1694. }
  1695. }
  1696. if (sectors_skipped)
  1697. /* pretend they weren't skipped, it makes
  1698. * no important difference in this case
  1699. */
  1700. md_done_sync(mddev, sectors_skipped, 1);
  1701. return sectors_skipped + nr_sectors;
  1702. giveup:
  1703. /* There is nowhere to write, so all non-sync
  1704. * drives must be failed, so try the next chunk...
  1705. */
  1706. {
  1707. sector_t sec = max_sector - sector_nr;
  1708. sectors_skipped += sec;
  1709. chunks_skipped ++;
  1710. sector_nr = max_sector;
  1711. goto skipped;
  1712. }
  1713. }
  1714. static int run(mddev_t *mddev)
  1715. {
  1716. conf_t *conf;
  1717. int i, disk_idx;
  1718. mirror_info_t *disk;
  1719. mdk_rdev_t *rdev;
  1720. struct list_head *tmp;
  1721. int nc, fc, fo;
  1722. sector_t stride, size;
  1723. if (mddev->chunk_size == 0) {
  1724. printk(KERN_ERR "md/raid10: non-zero chunk size required.\n");
  1725. return -EINVAL;
  1726. }
  1727. nc = mddev->layout & 255;
  1728. fc = (mddev->layout >> 8) & 255;
  1729. fo = mddev->layout & (1<<16);
  1730. if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
  1731. (mddev->layout >> 17)) {
  1732. printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
  1733. mdname(mddev), mddev->layout);
  1734. goto out;
  1735. }
  1736. /*
  1737. * copy the already verified devices into our private RAID10
  1738. * bookkeeping area. [whatever we allocate in run(),
  1739. * should be freed in stop()]
  1740. */
  1741. conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
  1742. mddev->private = conf;
  1743. if (!conf) {
  1744. printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
  1745. mdname(mddev));
  1746. goto out;
  1747. }
  1748. conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  1749. GFP_KERNEL);
  1750. if (!conf->mirrors) {
  1751. printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
  1752. mdname(mddev));
  1753. goto out_free_conf;
  1754. }
  1755. conf->tmppage = alloc_page(GFP_KERNEL);
  1756. if (!conf->tmppage)
  1757. goto out_free_conf;
  1758. conf->mddev = mddev;
  1759. conf->raid_disks = mddev->raid_disks;
  1760. conf->near_copies = nc;
  1761. conf->far_copies = fc;
  1762. conf->copies = nc*fc;
  1763. conf->far_offset = fo;
  1764. conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
  1765. conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
  1766. size = mddev->size >> (conf->chunk_shift-1);
  1767. sector_div(size, fc);
  1768. size = size * conf->raid_disks;
  1769. sector_div(size, nc);
  1770. /* 'size' is now the number of chunks in the array */
  1771. /* calculate "used chunks per device" in 'stride' */
  1772. stride = size * conf->copies;
  1773. /* We need to round up when dividing by raid_disks to
  1774. * get the stride size.
  1775. */
  1776. stride += conf->raid_disks - 1;
  1777. sector_div(stride, conf->raid_disks);
  1778. mddev->size = stride << (conf->chunk_shift-1);
  1779. if (fo)
  1780. stride = 1;
  1781. else
  1782. sector_div(stride, fc);
  1783. conf->stride = stride << conf->chunk_shift;
  1784. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  1785. r10bio_pool_free, conf);
  1786. if (!conf->r10bio_pool) {
  1787. printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
  1788. mdname(mddev));
  1789. goto out_free_conf;
  1790. }
  1791. ITERATE_RDEV(mddev, rdev, tmp) {
  1792. disk_idx = rdev->raid_disk;
  1793. if (disk_idx >= mddev->raid_disks
  1794. || disk_idx < 0)
  1795. continue;
  1796. disk = conf->mirrors + disk_idx;
  1797. disk->rdev = rdev;
  1798. blk_queue_stack_limits(mddev->queue,
  1799. rdev->bdev->bd_disk->queue);
  1800. /* as we don't honour merge_bvec_fn, we must never risk
  1801. * violating it, so limit ->max_sector to one PAGE, as
  1802. * a one page request is never in violation.
  1803. */
  1804. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  1805. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  1806. mddev->queue->max_sectors = (PAGE_SIZE>>9);
  1807. disk->head_position = 0;
  1808. }
  1809. spin_lock_init(&conf->device_lock);
  1810. INIT_LIST_HEAD(&conf->retry_list);
  1811. spin_lock_init(&conf->resync_lock);
  1812. init_waitqueue_head(&conf->wait_barrier);
  1813. /* need to check that every block has at least one working mirror */
  1814. if (!enough(conf)) {
  1815. printk(KERN_ERR "raid10: not enough operational mirrors for %s\n",
  1816. mdname(mddev));
  1817. goto out_free_conf;
  1818. }
  1819. mddev->degraded = 0;
  1820. for (i = 0; i < conf->raid_disks; i++) {
  1821. disk = conf->mirrors + i;
  1822. if (!disk->rdev ||
  1823. !test_bit(In_sync, &disk->rdev->flags)) {
  1824. disk->head_position = 0;
  1825. mddev->degraded++;
  1826. }
  1827. }
  1828. mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
  1829. if (!mddev->thread) {
  1830. printk(KERN_ERR
  1831. "raid10: couldn't allocate thread for %s\n",
  1832. mdname(mddev));
  1833. goto out_free_conf;
  1834. }
  1835. printk(KERN_INFO
  1836. "raid10: raid set %s active with %d out of %d devices\n",
  1837. mdname(mddev), mddev->raid_disks - mddev->degraded,
  1838. mddev->raid_disks);
  1839. /*
  1840. * Ok, everything is just fine now
  1841. */
  1842. mddev->array_size = size << (conf->chunk_shift-1);
  1843. mddev->resync_max_sectors = size << conf->chunk_shift;
  1844. mddev->queue->unplug_fn = raid10_unplug;
  1845. mddev->queue->backing_dev_info.congested_fn = raid10_congested;
  1846. mddev->queue->backing_dev_info.congested_data = mddev;
  1847. /* Calculate max read-ahead size.
  1848. * We need to readahead at least twice a whole stripe....
  1849. * maybe...
  1850. */
  1851. {
  1852. int stripe = conf->raid_disks * (mddev->chunk_size / PAGE_SIZE);
  1853. stripe /= conf->near_copies;
  1854. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  1855. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  1856. }
  1857. if (conf->near_copies < mddev->raid_disks)
  1858. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  1859. return 0;
  1860. out_free_conf:
  1861. if (conf->r10bio_pool)
  1862. mempool_destroy(conf->r10bio_pool);
  1863. safe_put_page(conf->tmppage);
  1864. kfree(conf->mirrors);
  1865. kfree(conf);
  1866. mddev->private = NULL;
  1867. out:
  1868. return -EIO;
  1869. }
  1870. static int stop(mddev_t *mddev)
  1871. {
  1872. conf_t *conf = mddev_to_conf(mddev);
  1873. md_unregister_thread(mddev->thread);
  1874. mddev->thread = NULL;
  1875. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  1876. if (conf->r10bio_pool)
  1877. mempool_destroy(conf->r10bio_pool);
  1878. kfree(conf->mirrors);
  1879. kfree(conf);
  1880. mddev->private = NULL;
  1881. return 0;
  1882. }
  1883. static void raid10_quiesce(mddev_t *mddev, int state)
  1884. {
  1885. conf_t *conf = mddev_to_conf(mddev);
  1886. switch(state) {
  1887. case 1:
  1888. raise_barrier(conf, 0);
  1889. break;
  1890. case 0:
  1891. lower_barrier(conf);
  1892. break;
  1893. }
  1894. if (mddev->thread) {
  1895. if (mddev->bitmap)
  1896. mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
  1897. else
  1898. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1899. md_wakeup_thread(mddev->thread);
  1900. }
  1901. }
  1902. static struct mdk_personality raid10_personality =
  1903. {
  1904. .name = "raid10",
  1905. .level = 10,
  1906. .owner = THIS_MODULE,
  1907. .make_request = make_request,
  1908. .run = run,
  1909. .stop = stop,
  1910. .status = status,
  1911. .error_handler = error,
  1912. .hot_add_disk = raid10_add_disk,
  1913. .hot_remove_disk= raid10_remove_disk,
  1914. .spare_active = raid10_spare_active,
  1915. .sync_request = sync_request,
  1916. .quiesce = raid10_quiesce,
  1917. };
  1918. static int __init raid_init(void)
  1919. {
  1920. return register_md_personality(&raid10_personality);
  1921. }
  1922. static void raid_exit(void)
  1923. {
  1924. unregister_md_personality(&raid10_personality);
  1925. }
  1926. module_init(raid_init);
  1927. module_exit(raid_exit);
  1928. MODULE_LICENSE("GPL");
  1929. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  1930. MODULE_ALIAS("md-raid10");
  1931. MODULE_ALIAS("md-level-10");