raid10.c 67 KB

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