raid1.c 92 KB

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