raid1.c 92 KB

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