raid1.c 81 KB

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