raid1.c 87 KB

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