raid1.c 91 KB

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