raid1.c 82 KB

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