raid10.c 66 KB

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