raid10.c 61 KB

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