raid1.c 80 KB

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