raid1.c 83 KB

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