raid10.c 67 KB

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