raid1.c 92 KB

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