raid1.c 84 KB

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