raid1.c 87 KB

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