raid1.c 87 KB

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