raid10.c 60 KB

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