raid10.c 47 KB

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