raid1.c 86 KB

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