raid1.c 92 KB

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