raid1.c 82 KB

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