raid10.c 59 KB

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