raid10.c 61 KB

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