raid10.c 83 KB

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