raid10.c 83 KB

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