raid1.c 94 KB

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