raid1.c 95 KB

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