raid1.c 86 KB

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