raid10.c 56 KB

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