raid10.c 84 KB

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