raid10.c 70 KB

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