raid1.c 83 KB

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