raid1.c 81 KB

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