raid10.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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. /*
  609. * Register the new request and wait if the reconstruction
  610. * thread has put up a bar for new requests.
  611. * Continue immediately if no resync is active currently.
  612. */
  613. spin_lock_irq(&conf->resync_lock);
  614. wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
  615. conf->nr_pending++;
  616. spin_unlock_irq(&conf->resync_lock);
  617. if (bio_data_dir(bio)==WRITE) {
  618. disk_stat_inc(mddev->gendisk, writes);
  619. disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
  620. } else {
  621. disk_stat_inc(mddev->gendisk, reads);
  622. disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
  623. }
  624. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  625. r10_bio->master_bio = bio;
  626. r10_bio->sectors = bio->bi_size >> 9;
  627. r10_bio->mddev = mddev;
  628. r10_bio->sector = bio->bi_sector;
  629. if (bio_data_dir(bio) == READ) {
  630. /*
  631. * read balancing logic:
  632. */
  633. int disk = read_balance(conf, r10_bio);
  634. int slot = r10_bio->read_slot;
  635. if (disk < 0) {
  636. raid_end_bio_io(r10_bio);
  637. return 0;
  638. }
  639. mirror = conf->mirrors + disk;
  640. read_bio = bio_clone(bio, GFP_NOIO);
  641. r10_bio->devs[slot].bio = read_bio;
  642. read_bio->bi_sector = r10_bio->devs[slot].addr +
  643. mirror->rdev->data_offset;
  644. read_bio->bi_bdev = mirror->rdev->bdev;
  645. read_bio->bi_end_io = raid10_end_read_request;
  646. read_bio->bi_rw = READ;
  647. read_bio->bi_private = r10_bio;
  648. generic_make_request(read_bio);
  649. return 0;
  650. }
  651. /*
  652. * WRITE:
  653. */
  654. /* first select target devices under spinlock and
  655. * inc refcount on their rdev. Record them by setting
  656. * bios[x] to bio
  657. */
  658. raid10_find_phys(conf, r10_bio);
  659. rcu_read_lock();
  660. for (i = 0; i < conf->copies; i++) {
  661. int d = r10_bio->devs[i].devnum;
  662. if (conf->mirrors[d].rdev &&
  663. !conf->mirrors[d].rdev->faulty) {
  664. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  665. r10_bio->devs[i].bio = bio;
  666. } else
  667. r10_bio->devs[i].bio = NULL;
  668. }
  669. rcu_read_unlock();
  670. atomic_set(&r10_bio->remaining, 1);
  671. md_write_start(mddev);
  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 int sync_request(mddev_t *mddev, sector_t sector_nr, 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 -ENOMEM;
  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. return sectors_skipped;
  1168. }
  1169. if (chunks_skipped >= conf->raid_disks) {
  1170. /* if there has been nothing to do on any drive,
  1171. * then there is nothing to do at all..
  1172. */
  1173. sector_t sec = max_sector - sector_nr;
  1174. md_done_sync(mddev, sec, 1);
  1175. return sec + 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. return sectors_skipped + nr_sectors;
  1369. giveup:
  1370. /* There is nowhere to write, so all non-sync
  1371. * drives must be failed, so try the next chunk...
  1372. */
  1373. {
  1374. int sec = max_sector - sector_nr;
  1375. sectors_skipped += sec;
  1376. chunks_skipped ++;
  1377. sector_nr = max_sector;
  1378. md_done_sync(mddev, sec, 1);
  1379. goto skipped;
  1380. }
  1381. }
  1382. static int run(mddev_t *mddev)
  1383. {
  1384. conf_t *conf;
  1385. int i, disk_idx;
  1386. mirror_info_t *disk;
  1387. mdk_rdev_t *rdev;
  1388. struct list_head *tmp;
  1389. int nc, fc;
  1390. sector_t stride, size;
  1391. if (mddev->level != 10) {
  1392. printk(KERN_ERR "raid10: %s: raid level not set correctly... (%d)\n",
  1393. mdname(mddev), mddev->level);
  1394. goto out;
  1395. }
  1396. nc = mddev->layout & 255;
  1397. fc = (mddev->layout >> 8) & 255;
  1398. if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks ||
  1399. (mddev->layout >> 16)) {
  1400. printk(KERN_ERR "raid10: %s: unsupported raid10 layout: 0x%8x\n",
  1401. mdname(mddev), mddev->layout);
  1402. goto out;
  1403. }
  1404. /*
  1405. * copy the already verified devices into our private RAID10
  1406. * bookkeeping area. [whatever we allocate in run(),
  1407. * should be freed in stop()]
  1408. */
  1409. conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
  1410. mddev->private = conf;
  1411. if (!conf) {
  1412. printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
  1413. mdname(mddev));
  1414. goto out;
  1415. }
  1416. memset(conf, 0, sizeof(*conf));
  1417. conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  1418. GFP_KERNEL);
  1419. if (!conf->mirrors) {
  1420. printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
  1421. mdname(mddev));
  1422. goto out_free_conf;
  1423. }
  1424. memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
  1425. conf->near_copies = nc;
  1426. conf->far_copies = fc;
  1427. conf->copies = nc*fc;
  1428. conf->chunk_mask = (sector_t)(mddev->chunk_size>>9)-1;
  1429. conf->chunk_shift = ffz(~mddev->chunk_size) - 9;
  1430. stride = mddev->size >> (conf->chunk_shift-1);
  1431. sector_div(stride, fc);
  1432. conf->stride = stride << conf->chunk_shift;
  1433. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  1434. r10bio_pool_free, conf);
  1435. if (!conf->r10bio_pool) {
  1436. printk(KERN_ERR "raid10: couldn't allocate memory for %s\n",
  1437. mdname(mddev));
  1438. goto out_free_conf;
  1439. }
  1440. ITERATE_RDEV(mddev, rdev, tmp) {
  1441. disk_idx = rdev->raid_disk;
  1442. if (disk_idx >= mddev->raid_disks
  1443. || disk_idx < 0)
  1444. continue;
  1445. disk = conf->mirrors + disk_idx;
  1446. disk->rdev = rdev;
  1447. blk_queue_stack_limits(mddev->queue,
  1448. rdev->bdev->bd_disk->queue);
  1449. /* as we don't honour merge_bvec_fn, we must never risk
  1450. * violating it, so limit ->max_sector to one PAGE, as
  1451. * a one page request is never in violation.
  1452. */
  1453. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  1454. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  1455. mddev->queue->max_sectors = (PAGE_SIZE>>9);
  1456. disk->head_position = 0;
  1457. if (!rdev->faulty && rdev->in_sync)
  1458. conf->working_disks++;
  1459. }
  1460. conf->raid_disks = mddev->raid_disks;
  1461. conf->mddev = mddev;
  1462. spin_lock_init(&conf->device_lock);
  1463. INIT_LIST_HEAD(&conf->retry_list);
  1464. spin_lock_init(&conf->resync_lock);
  1465. init_waitqueue_head(&conf->wait_idle);
  1466. init_waitqueue_head(&conf->wait_resume);
  1467. if (!conf->working_disks) {
  1468. printk(KERN_ERR "raid10: no operational mirrors for %s\n",
  1469. mdname(mddev));
  1470. goto out_free_conf;
  1471. }
  1472. mddev->degraded = 0;
  1473. for (i = 0; i < conf->raid_disks; i++) {
  1474. disk = conf->mirrors + i;
  1475. if (!disk->rdev) {
  1476. disk->head_position = 0;
  1477. mddev->degraded++;
  1478. }
  1479. }
  1480. mddev->thread = md_register_thread(raid10d, mddev, "%s_raid10");
  1481. if (!mddev->thread) {
  1482. printk(KERN_ERR
  1483. "raid10: couldn't allocate thread for %s\n",
  1484. mdname(mddev));
  1485. goto out_free_conf;
  1486. }
  1487. printk(KERN_INFO
  1488. "raid10: raid set %s active with %d out of %d devices\n",
  1489. mdname(mddev), mddev->raid_disks - mddev->degraded,
  1490. mddev->raid_disks);
  1491. /*
  1492. * Ok, everything is just fine now
  1493. */
  1494. size = conf->stride * conf->raid_disks;
  1495. sector_div(size, conf->near_copies);
  1496. mddev->array_size = size/2;
  1497. mddev->resync_max_sectors = size;
  1498. mddev->queue->unplug_fn = raid10_unplug;
  1499. mddev->queue->issue_flush_fn = raid10_issue_flush;
  1500. /* Calculate max read-ahead size.
  1501. * We need to readahead at least twice a whole stripe....
  1502. * maybe...
  1503. */
  1504. {
  1505. int stripe = conf->raid_disks * mddev->chunk_size / PAGE_CACHE_SIZE;
  1506. stripe /= conf->near_copies;
  1507. if (mddev->queue->backing_dev_info.ra_pages < 2* stripe)
  1508. mddev->queue->backing_dev_info.ra_pages = 2* stripe;
  1509. }
  1510. if (conf->near_copies < mddev->raid_disks)
  1511. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  1512. return 0;
  1513. out_free_conf:
  1514. if (conf->r10bio_pool)
  1515. mempool_destroy(conf->r10bio_pool);
  1516. if (conf->mirrors)
  1517. kfree(conf->mirrors);
  1518. kfree(conf);
  1519. mddev->private = NULL;
  1520. out:
  1521. return -EIO;
  1522. }
  1523. static int stop(mddev_t *mddev)
  1524. {
  1525. conf_t *conf = mddev_to_conf(mddev);
  1526. md_unregister_thread(mddev->thread);
  1527. mddev->thread = NULL;
  1528. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  1529. if (conf->r10bio_pool)
  1530. mempool_destroy(conf->r10bio_pool);
  1531. if (conf->mirrors)
  1532. kfree(conf->mirrors);
  1533. kfree(conf);
  1534. mddev->private = NULL;
  1535. return 0;
  1536. }
  1537. static mdk_personality_t raid10_personality =
  1538. {
  1539. .name = "raid10",
  1540. .owner = THIS_MODULE,
  1541. .make_request = make_request,
  1542. .run = run,
  1543. .stop = stop,
  1544. .status = status,
  1545. .error_handler = error,
  1546. .hot_add_disk = raid10_add_disk,
  1547. .hot_remove_disk= raid10_remove_disk,
  1548. .spare_active = raid10_spare_active,
  1549. .sync_request = sync_request,
  1550. };
  1551. static int __init raid_init(void)
  1552. {
  1553. return register_md_personality(RAID10, &raid10_personality);
  1554. }
  1555. static void raid_exit(void)
  1556. {
  1557. unregister_md_personality(RAID10);
  1558. }
  1559. module_init(raid_init);
  1560. module_exit(raid_exit);
  1561. MODULE_LICENSE("GPL");
  1562. MODULE_ALIAS("md-personality-9"); /* RAID10 */