raid10.c 81 KB

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