raid1.c 91 KB

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