raid1.c 91 KB

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