raid1.c 73 KB

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