raid10.c 62 KB

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