raid1.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772
  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 void 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;
  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;
  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. r1_bio_write_done(r1_bio);
  976. /* In case raid1d snuck in to freeze_array */
  977. wake_up(&conf->wait_barrier);
  978. if (sectors_handled < (bio->bi_size >> 9)) {
  979. /* We need another r1_bio. It has already been counted
  980. * in bio->bi_phys_segments
  981. */
  982. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  983. r1_bio->master_bio = bio;
  984. r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
  985. r1_bio->state = 0;
  986. r1_bio->mddev = mddev;
  987. r1_bio->sector = bio->bi_sector + sectors_handled;
  988. goto retry_write;
  989. }
  990. if (do_sync || !bitmap || !plugged)
  991. md_wakeup_thread(mddev->thread);
  992. }
  993. static void status(struct seq_file *seq, mddev_t *mddev)
  994. {
  995. conf_t *conf = mddev->private;
  996. int i;
  997. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  998. conf->raid_disks - mddev->degraded);
  999. rcu_read_lock();
  1000. for (i = 0; i < conf->raid_disks; i++) {
  1001. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  1002. seq_printf(seq, "%s",
  1003. rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
  1004. }
  1005. rcu_read_unlock();
  1006. seq_printf(seq, "]");
  1007. }
  1008. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  1009. {
  1010. char b[BDEVNAME_SIZE];
  1011. conf_t *conf = mddev->private;
  1012. /*
  1013. * If it is not operational, then we have already marked it as dead
  1014. * else if it is the last working disks, ignore the error, let the
  1015. * next level up know.
  1016. * else mark the drive as failed
  1017. */
  1018. if (test_bit(In_sync, &rdev->flags)
  1019. && (conf->raid_disks - mddev->degraded) == 1) {
  1020. /*
  1021. * Don't fail the drive, act as though we were just a
  1022. * normal single drive.
  1023. * However don't try a recovery from this drive as
  1024. * it is very likely to fail.
  1025. */
  1026. conf->recovery_disabled = mddev->recovery_disabled;
  1027. return;
  1028. }
  1029. set_bit(Blocked, &rdev->flags);
  1030. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1031. unsigned long flags;
  1032. spin_lock_irqsave(&conf->device_lock, flags);
  1033. mddev->degraded++;
  1034. set_bit(Faulty, &rdev->flags);
  1035. spin_unlock_irqrestore(&conf->device_lock, flags);
  1036. /*
  1037. * if recovery is running, make sure it aborts.
  1038. */
  1039. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1040. } else
  1041. set_bit(Faulty, &rdev->flags);
  1042. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1043. printk(KERN_ALERT
  1044. "md/raid1:%s: Disk failure on %s, disabling device.\n"
  1045. "md/raid1:%s: Operation continuing on %d devices.\n",
  1046. mdname(mddev), bdevname(rdev->bdev, b),
  1047. mdname(mddev), conf->raid_disks - mddev->degraded);
  1048. }
  1049. static void print_conf(conf_t *conf)
  1050. {
  1051. int i;
  1052. printk(KERN_DEBUG "RAID1 conf printout:\n");
  1053. if (!conf) {
  1054. printk(KERN_DEBUG "(!conf)\n");
  1055. return;
  1056. }
  1057. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
  1058. conf->raid_disks);
  1059. rcu_read_lock();
  1060. for (i = 0; i < conf->raid_disks; i++) {
  1061. char b[BDEVNAME_SIZE];
  1062. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  1063. if (rdev)
  1064. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  1065. i, !test_bit(In_sync, &rdev->flags),
  1066. !test_bit(Faulty, &rdev->flags),
  1067. bdevname(rdev->bdev,b));
  1068. }
  1069. rcu_read_unlock();
  1070. }
  1071. static void close_sync(conf_t *conf)
  1072. {
  1073. wait_barrier(conf);
  1074. allow_barrier(conf);
  1075. mempool_destroy(conf->r1buf_pool);
  1076. conf->r1buf_pool = NULL;
  1077. }
  1078. static int raid1_spare_active(mddev_t *mddev)
  1079. {
  1080. int i;
  1081. conf_t *conf = mddev->private;
  1082. int count = 0;
  1083. unsigned long flags;
  1084. /*
  1085. * Find all failed disks within the RAID1 configuration
  1086. * and mark them readable.
  1087. * Called under mddev lock, so rcu protection not needed.
  1088. */
  1089. for (i = 0; i < conf->raid_disks; i++) {
  1090. mdk_rdev_t *rdev = conf->mirrors[i].rdev;
  1091. if (rdev
  1092. && !test_bit(Faulty, &rdev->flags)
  1093. && !test_and_set_bit(In_sync, &rdev->flags)) {
  1094. count++;
  1095. sysfs_notify_dirent_safe(rdev->sysfs_state);
  1096. }
  1097. }
  1098. spin_lock_irqsave(&conf->device_lock, flags);
  1099. mddev->degraded -= count;
  1100. spin_unlock_irqrestore(&conf->device_lock, flags);
  1101. print_conf(conf);
  1102. return count;
  1103. }
  1104. static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  1105. {
  1106. conf_t *conf = mddev->private;
  1107. int err = -EEXIST;
  1108. int mirror = 0;
  1109. mirror_info_t *p;
  1110. int first = 0;
  1111. int last = mddev->raid_disks - 1;
  1112. if (mddev->recovery_disabled == conf->recovery_disabled)
  1113. return -EBUSY;
  1114. if (rdev->raid_disk >= 0)
  1115. first = last = rdev->raid_disk;
  1116. for (mirror = first; mirror <= last; mirror++)
  1117. if ( !(p=conf->mirrors+mirror)->rdev) {
  1118. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1119. rdev->data_offset << 9);
  1120. /* as we don't honour merge_bvec_fn, we must
  1121. * never risk violating it, so limit
  1122. * ->max_segments to one lying with a single
  1123. * page, as a one page request is never in
  1124. * violation.
  1125. */
  1126. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  1127. blk_queue_max_segments(mddev->queue, 1);
  1128. blk_queue_segment_boundary(mddev->queue,
  1129. PAGE_CACHE_SIZE - 1);
  1130. }
  1131. p->head_position = 0;
  1132. rdev->raid_disk = mirror;
  1133. err = 0;
  1134. /* As all devices are equivalent, we don't need a full recovery
  1135. * if this was recently any drive of the array
  1136. */
  1137. if (rdev->saved_raid_disk < 0)
  1138. conf->fullsync = 1;
  1139. rcu_assign_pointer(p->rdev, rdev);
  1140. break;
  1141. }
  1142. md_integrity_add_rdev(rdev, mddev);
  1143. print_conf(conf);
  1144. return err;
  1145. }
  1146. static int raid1_remove_disk(mddev_t *mddev, int number)
  1147. {
  1148. conf_t *conf = mddev->private;
  1149. int err = 0;
  1150. mdk_rdev_t *rdev;
  1151. mirror_info_t *p = conf->mirrors+ number;
  1152. print_conf(conf);
  1153. rdev = p->rdev;
  1154. if (rdev) {
  1155. if (test_bit(In_sync, &rdev->flags) ||
  1156. atomic_read(&rdev->nr_pending)) {
  1157. err = -EBUSY;
  1158. goto abort;
  1159. }
  1160. /* Only remove non-faulty devices if recovery
  1161. * is not possible.
  1162. */
  1163. if (!test_bit(Faulty, &rdev->flags) &&
  1164. mddev->recovery_disabled != conf->recovery_disabled &&
  1165. mddev->degraded < conf->raid_disks) {
  1166. err = -EBUSY;
  1167. goto abort;
  1168. }
  1169. p->rdev = NULL;
  1170. synchronize_rcu();
  1171. if (atomic_read(&rdev->nr_pending)) {
  1172. /* lost the race, try later */
  1173. err = -EBUSY;
  1174. p->rdev = rdev;
  1175. goto abort;
  1176. }
  1177. err = md_integrity_register(mddev);
  1178. }
  1179. abort:
  1180. print_conf(conf);
  1181. return err;
  1182. }
  1183. static void end_sync_read(struct bio *bio, int error)
  1184. {
  1185. r1bio_t *r1_bio = bio->bi_private;
  1186. int i;
  1187. for (i=r1_bio->mddev->raid_disks; i--; )
  1188. if (r1_bio->bios[i] == bio)
  1189. break;
  1190. BUG_ON(i < 0);
  1191. update_head_pos(i, r1_bio);
  1192. /*
  1193. * we have read a block, now it needs to be re-written,
  1194. * or re-read if the read failed.
  1195. * We don't do much here, just schedule handling by raid1d
  1196. */
  1197. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1198. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1199. if (atomic_dec_and_test(&r1_bio->remaining))
  1200. reschedule_retry(r1_bio);
  1201. }
  1202. static void end_sync_write(struct bio *bio, int error)
  1203. {
  1204. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1205. r1bio_t *r1_bio = bio->bi_private;
  1206. mddev_t *mddev = r1_bio->mddev;
  1207. conf_t *conf = mddev->private;
  1208. int i;
  1209. int mirror=0;
  1210. sector_t first_bad;
  1211. int bad_sectors;
  1212. for (i = 0; i < conf->raid_disks; i++)
  1213. if (r1_bio->bios[i] == bio) {
  1214. mirror = i;
  1215. break;
  1216. }
  1217. if (!uptodate) {
  1218. sector_t sync_blocks = 0;
  1219. sector_t s = r1_bio->sector;
  1220. long sectors_to_go = r1_bio->sectors;
  1221. /* make sure these bits doesn't get cleared. */
  1222. do {
  1223. bitmap_end_sync(mddev->bitmap, s,
  1224. &sync_blocks, 1);
  1225. s += sync_blocks;
  1226. sectors_to_go -= sync_blocks;
  1227. } while (sectors_to_go > 0);
  1228. set_bit(WriteErrorSeen,
  1229. &conf->mirrors[mirror].rdev->flags);
  1230. set_bit(R1BIO_WriteError, &r1_bio->state);
  1231. } else if (is_badblock(conf->mirrors[mirror].rdev,
  1232. r1_bio->sector,
  1233. r1_bio->sectors,
  1234. &first_bad, &bad_sectors) &&
  1235. !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
  1236. r1_bio->sector,
  1237. r1_bio->sectors,
  1238. &first_bad, &bad_sectors)
  1239. )
  1240. set_bit(R1BIO_MadeGood, &r1_bio->state);
  1241. update_head_pos(mirror, r1_bio);
  1242. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1243. int s = r1_bio->sectors;
  1244. if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  1245. test_bit(R1BIO_WriteError, &r1_bio->state))
  1246. reschedule_retry(r1_bio);
  1247. else {
  1248. put_buf(r1_bio);
  1249. md_done_sync(mddev, s, uptodate);
  1250. }
  1251. }
  1252. }
  1253. static int r1_sync_page_io(mdk_rdev_t *rdev, sector_t sector,
  1254. int sectors, struct page *page, int rw)
  1255. {
  1256. if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
  1257. /* success */
  1258. return 1;
  1259. if (rw == WRITE)
  1260. set_bit(WriteErrorSeen, &rdev->flags);
  1261. /* need to record an error - either for the block or the device */
  1262. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  1263. md_error(rdev->mddev, rdev);
  1264. return 0;
  1265. }
  1266. static int fix_sync_read_error(r1bio_t *r1_bio)
  1267. {
  1268. /* Try some synchronous reads of other devices to get
  1269. * good data, much like with normal read errors. Only
  1270. * read into the pages we already have so we don't
  1271. * need to re-issue the read request.
  1272. * We don't need to freeze the array, because being in an
  1273. * active sync request, there is no normal IO, and
  1274. * no overlapping syncs.
  1275. * We don't need to check is_badblock() again as we
  1276. * made sure that anything with a bad block in range
  1277. * will have bi_end_io clear.
  1278. */
  1279. mddev_t *mddev = r1_bio->mddev;
  1280. conf_t *conf = mddev->private;
  1281. struct bio *bio = r1_bio->bios[r1_bio->read_disk];
  1282. sector_t sect = r1_bio->sector;
  1283. int sectors = r1_bio->sectors;
  1284. int idx = 0;
  1285. while(sectors) {
  1286. int s = sectors;
  1287. int d = r1_bio->read_disk;
  1288. int success = 0;
  1289. mdk_rdev_t *rdev;
  1290. int start;
  1291. if (s > (PAGE_SIZE>>9))
  1292. s = PAGE_SIZE >> 9;
  1293. do {
  1294. if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
  1295. /* No rcu protection needed here devices
  1296. * can only be removed when no resync is
  1297. * active, and resync is currently active
  1298. */
  1299. rdev = conf->mirrors[d].rdev;
  1300. if (sync_page_io(rdev, sect, s<<9,
  1301. bio->bi_io_vec[idx].bv_page,
  1302. READ, false)) {
  1303. success = 1;
  1304. break;
  1305. }
  1306. }
  1307. d++;
  1308. if (d == conf->raid_disks)
  1309. d = 0;
  1310. } while (!success && d != r1_bio->read_disk);
  1311. if (!success) {
  1312. char b[BDEVNAME_SIZE];
  1313. int abort = 0;
  1314. /* Cannot read from anywhere, this block is lost.
  1315. * Record a bad block on each device. If that doesn't
  1316. * work just disable and interrupt the recovery.
  1317. * Don't fail devices as that won't really help.
  1318. */
  1319. printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
  1320. " for block %llu\n",
  1321. mdname(mddev),
  1322. bdevname(bio->bi_bdev, b),
  1323. (unsigned long long)r1_bio->sector);
  1324. for (d = 0; d < conf->raid_disks; d++) {
  1325. rdev = conf->mirrors[d].rdev;
  1326. if (!rdev || test_bit(Faulty, &rdev->flags))
  1327. continue;
  1328. if (!rdev_set_badblocks(rdev, sect, s, 0))
  1329. abort = 1;
  1330. }
  1331. if (abort) {
  1332. mddev->recovery_disabled = 1;
  1333. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1334. md_done_sync(mddev, r1_bio->sectors, 0);
  1335. put_buf(r1_bio);
  1336. return 0;
  1337. }
  1338. /* Try next page */
  1339. sectors -= s;
  1340. sect += s;
  1341. idx++;
  1342. continue;
  1343. }
  1344. start = d;
  1345. /* write it back and re-read */
  1346. while (d != r1_bio->read_disk) {
  1347. if (d == 0)
  1348. d = conf->raid_disks;
  1349. d--;
  1350. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1351. continue;
  1352. rdev = conf->mirrors[d].rdev;
  1353. if (r1_sync_page_io(rdev, sect, s,
  1354. bio->bi_io_vec[idx].bv_page,
  1355. WRITE) == 0) {
  1356. r1_bio->bios[d]->bi_end_io = NULL;
  1357. rdev_dec_pending(rdev, mddev);
  1358. }
  1359. }
  1360. d = start;
  1361. while (d != r1_bio->read_disk) {
  1362. if (d == 0)
  1363. d = conf->raid_disks;
  1364. d--;
  1365. if (r1_bio->bios[d]->bi_end_io != end_sync_read)
  1366. continue;
  1367. rdev = conf->mirrors[d].rdev;
  1368. if (r1_sync_page_io(rdev, sect, s,
  1369. bio->bi_io_vec[idx].bv_page,
  1370. READ) != 0)
  1371. atomic_add(s, &rdev->corrected_errors);
  1372. }
  1373. sectors -= s;
  1374. sect += s;
  1375. idx ++;
  1376. }
  1377. set_bit(R1BIO_Uptodate, &r1_bio->state);
  1378. set_bit(BIO_UPTODATE, &bio->bi_flags);
  1379. return 1;
  1380. }
  1381. static int process_checks(r1bio_t *r1_bio)
  1382. {
  1383. /* We have read all readable devices. If we haven't
  1384. * got the block, then there is no hope left.
  1385. * If we have, then we want to do a comparison
  1386. * and skip the write if everything is the same.
  1387. * If any blocks failed to read, then we need to
  1388. * attempt an over-write
  1389. */
  1390. mddev_t *mddev = r1_bio->mddev;
  1391. conf_t *conf = mddev->private;
  1392. int primary;
  1393. int i;
  1394. for (primary = 0; primary < conf->raid_disks; primary++)
  1395. if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
  1396. test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
  1397. r1_bio->bios[primary]->bi_end_io = NULL;
  1398. rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
  1399. break;
  1400. }
  1401. r1_bio->read_disk = primary;
  1402. for (i = 0; i < conf->raid_disks; i++) {
  1403. int j;
  1404. int vcnt = r1_bio->sectors >> (PAGE_SHIFT- 9);
  1405. struct bio *pbio = r1_bio->bios[primary];
  1406. struct bio *sbio = r1_bio->bios[i];
  1407. int size;
  1408. if (r1_bio->bios[i]->bi_end_io != end_sync_read)
  1409. continue;
  1410. if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
  1411. for (j = vcnt; j-- ; ) {
  1412. struct page *p, *s;
  1413. p = pbio->bi_io_vec[j].bv_page;
  1414. s = sbio->bi_io_vec[j].bv_page;
  1415. if (memcmp(page_address(p),
  1416. page_address(s),
  1417. PAGE_SIZE))
  1418. break;
  1419. }
  1420. } else
  1421. j = 0;
  1422. if (j >= 0)
  1423. mddev->resync_mismatches += r1_bio->sectors;
  1424. if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
  1425. && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
  1426. /* No need to write to this device. */
  1427. sbio->bi_end_io = NULL;
  1428. rdev_dec_pending(conf->mirrors[i].rdev, mddev);
  1429. continue;
  1430. }
  1431. /* fixup the bio for reuse */
  1432. sbio->bi_vcnt = vcnt;
  1433. sbio->bi_size = r1_bio->sectors << 9;
  1434. sbio->bi_idx = 0;
  1435. sbio->bi_phys_segments = 0;
  1436. sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
  1437. sbio->bi_flags |= 1 << BIO_UPTODATE;
  1438. sbio->bi_next = NULL;
  1439. sbio->bi_sector = r1_bio->sector +
  1440. conf->mirrors[i].rdev->data_offset;
  1441. sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1442. size = sbio->bi_size;
  1443. for (j = 0; j < vcnt ; j++) {
  1444. struct bio_vec *bi;
  1445. bi = &sbio->bi_io_vec[j];
  1446. bi->bv_offset = 0;
  1447. if (size > PAGE_SIZE)
  1448. bi->bv_len = PAGE_SIZE;
  1449. else
  1450. bi->bv_len = size;
  1451. size -= PAGE_SIZE;
  1452. memcpy(page_address(bi->bv_page),
  1453. page_address(pbio->bi_io_vec[j].bv_page),
  1454. PAGE_SIZE);
  1455. }
  1456. }
  1457. return 0;
  1458. }
  1459. static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
  1460. {
  1461. conf_t *conf = mddev->private;
  1462. int i;
  1463. int disks = conf->raid_disks;
  1464. struct bio *bio, *wbio;
  1465. bio = r1_bio->bios[r1_bio->read_disk];
  1466. if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
  1467. /* ouch - failed to read all of that. */
  1468. if (!fix_sync_read_error(r1_bio))
  1469. return;
  1470. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  1471. if (process_checks(r1_bio) < 0)
  1472. return;
  1473. /*
  1474. * schedule writes
  1475. */
  1476. atomic_set(&r1_bio->remaining, 1);
  1477. for (i = 0; i < disks ; i++) {
  1478. wbio = r1_bio->bios[i];
  1479. if (wbio->bi_end_io == NULL ||
  1480. (wbio->bi_end_io == end_sync_read &&
  1481. (i == r1_bio->read_disk ||
  1482. !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
  1483. continue;
  1484. wbio->bi_rw = WRITE;
  1485. wbio->bi_end_io = end_sync_write;
  1486. atomic_inc(&r1_bio->remaining);
  1487. md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
  1488. generic_make_request(wbio);
  1489. }
  1490. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1491. /* if we're here, all write(s) have completed, so clean up */
  1492. md_done_sync(mddev, r1_bio->sectors, 1);
  1493. put_buf(r1_bio);
  1494. }
  1495. }
  1496. /*
  1497. * This is a kernel thread which:
  1498. *
  1499. * 1. Retries failed read operations on working mirrors.
  1500. * 2. Updates the raid superblock when problems encounter.
  1501. * 3. Performs writes following reads for array synchronising.
  1502. */
  1503. static void fix_read_error(conf_t *conf, int read_disk,
  1504. sector_t sect, int sectors)
  1505. {
  1506. mddev_t *mddev = conf->mddev;
  1507. while(sectors) {
  1508. int s = sectors;
  1509. int d = read_disk;
  1510. int success = 0;
  1511. int start;
  1512. mdk_rdev_t *rdev;
  1513. if (s > (PAGE_SIZE>>9))
  1514. s = PAGE_SIZE >> 9;
  1515. do {
  1516. /* Note: no rcu protection needed here
  1517. * as this is synchronous in the raid1d thread
  1518. * which is the thread that might remove
  1519. * a device. If raid1d ever becomes multi-threaded....
  1520. */
  1521. sector_t first_bad;
  1522. int bad_sectors;
  1523. rdev = conf->mirrors[d].rdev;
  1524. if (rdev &&
  1525. test_bit(In_sync, &rdev->flags) &&
  1526. is_badblock(rdev, sect, s,
  1527. &first_bad, &bad_sectors) == 0 &&
  1528. sync_page_io(rdev, sect, s<<9,
  1529. conf->tmppage, READ, false))
  1530. success = 1;
  1531. else {
  1532. d++;
  1533. if (d == conf->raid_disks)
  1534. d = 0;
  1535. }
  1536. } while (!success && d != read_disk);
  1537. if (!success) {
  1538. /* Cannot read from anywhere - mark it bad */
  1539. mdk_rdev_t *rdev = conf->mirrors[read_disk].rdev;
  1540. if (!rdev_set_badblocks(rdev, sect, s, 0))
  1541. md_error(mddev, rdev);
  1542. break;
  1543. }
  1544. /* write it back and re-read */
  1545. start = d;
  1546. while (d != read_disk) {
  1547. if (d==0)
  1548. d = conf->raid_disks;
  1549. d--;
  1550. rdev = conf->mirrors[d].rdev;
  1551. if (rdev &&
  1552. test_bit(In_sync, &rdev->flags))
  1553. r1_sync_page_io(rdev, sect, s,
  1554. conf->tmppage, WRITE);
  1555. }
  1556. d = start;
  1557. while (d != read_disk) {
  1558. char b[BDEVNAME_SIZE];
  1559. if (d==0)
  1560. d = conf->raid_disks;
  1561. d--;
  1562. rdev = conf->mirrors[d].rdev;
  1563. if (rdev &&
  1564. test_bit(In_sync, &rdev->flags)) {
  1565. if (r1_sync_page_io(rdev, sect, s,
  1566. conf->tmppage, READ)) {
  1567. atomic_add(s, &rdev->corrected_errors);
  1568. printk(KERN_INFO
  1569. "md/raid1:%s: read error corrected "
  1570. "(%d sectors at %llu on %s)\n",
  1571. mdname(mddev), s,
  1572. (unsigned long long)(sect +
  1573. rdev->data_offset),
  1574. bdevname(rdev->bdev, b));
  1575. }
  1576. }
  1577. }
  1578. sectors -= s;
  1579. sect += s;
  1580. }
  1581. }
  1582. static void bi_complete(struct bio *bio, int error)
  1583. {
  1584. complete((struct completion *)bio->bi_private);
  1585. }
  1586. static int submit_bio_wait(int rw, struct bio *bio)
  1587. {
  1588. struct completion event;
  1589. rw |= REQ_SYNC;
  1590. init_completion(&event);
  1591. bio->bi_private = &event;
  1592. bio->bi_end_io = bi_complete;
  1593. submit_bio(rw, bio);
  1594. wait_for_completion(&event);
  1595. return test_bit(BIO_UPTODATE, &bio->bi_flags);
  1596. }
  1597. static int narrow_write_error(r1bio_t *r1_bio, int i)
  1598. {
  1599. mddev_t *mddev = r1_bio->mddev;
  1600. conf_t *conf = mddev->private;
  1601. mdk_rdev_t *rdev = conf->mirrors[i].rdev;
  1602. int vcnt, idx;
  1603. struct bio_vec *vec;
  1604. /* bio has the data to be written to device 'i' where
  1605. * we just recently had a write error.
  1606. * We repeatedly clone the bio and trim down to one block,
  1607. * then try the write. Where the write fails we record
  1608. * a bad block.
  1609. * It is conceivable that the bio doesn't exactly align with
  1610. * blocks. We must handle this somehow.
  1611. *
  1612. * We currently own a reference on the rdev.
  1613. */
  1614. int block_sectors;
  1615. sector_t sector;
  1616. int sectors;
  1617. int sect_to_write = r1_bio->sectors;
  1618. int ok = 1;
  1619. if (rdev->badblocks.shift < 0)
  1620. return 0;
  1621. block_sectors = 1 << rdev->badblocks.shift;
  1622. sector = r1_bio->sector;
  1623. sectors = ((sector + block_sectors)
  1624. & ~(sector_t)(block_sectors - 1))
  1625. - sector;
  1626. if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
  1627. vcnt = r1_bio->behind_page_count;
  1628. vec = r1_bio->behind_bvecs;
  1629. idx = 0;
  1630. while (vec[idx].bv_page == NULL)
  1631. idx++;
  1632. } else {
  1633. vcnt = r1_bio->master_bio->bi_vcnt;
  1634. vec = r1_bio->master_bio->bi_io_vec;
  1635. idx = r1_bio->master_bio->bi_idx;
  1636. }
  1637. while (sect_to_write) {
  1638. struct bio *wbio;
  1639. if (sectors > sect_to_write)
  1640. sectors = sect_to_write;
  1641. /* Write at 'sector' for 'sectors'*/
  1642. wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
  1643. memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
  1644. wbio->bi_sector = r1_bio->sector;
  1645. wbio->bi_rw = WRITE;
  1646. wbio->bi_vcnt = vcnt;
  1647. wbio->bi_size = r1_bio->sectors << 9;
  1648. wbio->bi_idx = idx;
  1649. md_trim_bio(wbio, sector - r1_bio->sector, sectors);
  1650. wbio->bi_sector += rdev->data_offset;
  1651. wbio->bi_bdev = rdev->bdev;
  1652. if (submit_bio_wait(WRITE, wbio) == 0)
  1653. /* failure! */
  1654. ok = rdev_set_badblocks(rdev, sector,
  1655. sectors, 0)
  1656. && ok;
  1657. bio_put(wbio);
  1658. sect_to_write -= sectors;
  1659. sector += sectors;
  1660. sectors = block_sectors;
  1661. }
  1662. return ok;
  1663. }
  1664. static void handle_sync_write_finished(conf_t *conf, r1bio_t *r1_bio)
  1665. {
  1666. int m;
  1667. int s = r1_bio->sectors;
  1668. for (m = 0; m < conf->raid_disks ; m++) {
  1669. mdk_rdev_t *rdev = conf->mirrors[m].rdev;
  1670. struct bio *bio = r1_bio->bios[m];
  1671. if (bio->bi_end_io == NULL)
  1672. continue;
  1673. if (test_bit(BIO_UPTODATE, &bio->bi_flags) &&
  1674. test_bit(R1BIO_MadeGood, &r1_bio->state)) {
  1675. rdev_clear_badblocks(rdev, r1_bio->sector, s);
  1676. }
  1677. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
  1678. test_bit(R1BIO_WriteError, &r1_bio->state)) {
  1679. if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
  1680. md_error(conf->mddev, rdev);
  1681. }
  1682. }
  1683. put_buf(r1_bio);
  1684. md_done_sync(conf->mddev, s, 1);
  1685. }
  1686. static void handle_write_finished(conf_t *conf, r1bio_t *r1_bio)
  1687. {
  1688. int m;
  1689. for (m = 0; m < conf->raid_disks ; m++)
  1690. if (r1_bio->bios[m] == IO_MADE_GOOD) {
  1691. mdk_rdev_t *rdev = conf->mirrors[m].rdev;
  1692. rdev_clear_badblocks(rdev,
  1693. r1_bio->sector,
  1694. r1_bio->sectors);
  1695. rdev_dec_pending(rdev, conf->mddev);
  1696. } else if (r1_bio->bios[m] != NULL) {
  1697. /* This drive got a write error. We need to
  1698. * narrow down and record precise write
  1699. * errors.
  1700. */
  1701. if (!narrow_write_error(r1_bio, m)) {
  1702. md_error(conf->mddev,
  1703. conf->mirrors[m].rdev);
  1704. /* an I/O failed, we can't clear the bitmap */
  1705. set_bit(R1BIO_Degraded, &r1_bio->state);
  1706. }
  1707. rdev_dec_pending(conf->mirrors[m].rdev,
  1708. conf->mddev);
  1709. }
  1710. if (test_bit(R1BIO_WriteError, &r1_bio->state))
  1711. close_write(r1_bio);
  1712. raid_end_bio_io(r1_bio);
  1713. }
  1714. static void handle_read_error(conf_t *conf, r1bio_t *r1_bio)
  1715. {
  1716. int disk;
  1717. int max_sectors;
  1718. mddev_t *mddev = conf->mddev;
  1719. struct bio *bio;
  1720. char b[BDEVNAME_SIZE];
  1721. mdk_rdev_t *rdev;
  1722. clear_bit(R1BIO_ReadError, &r1_bio->state);
  1723. /* we got a read error. Maybe the drive is bad. Maybe just
  1724. * the block and we can fix it.
  1725. * We freeze all other IO, and try reading the block from
  1726. * other devices. When we find one, we re-write
  1727. * and check it that fixes the read error.
  1728. * This is all done synchronously while the array is
  1729. * frozen
  1730. */
  1731. if (mddev->ro == 0) {
  1732. freeze_array(conf);
  1733. fix_read_error(conf, r1_bio->read_disk,
  1734. r1_bio->sector, r1_bio->sectors);
  1735. unfreeze_array(conf);
  1736. } else
  1737. md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
  1738. bio = r1_bio->bios[r1_bio->read_disk];
  1739. bdevname(bio->bi_bdev, b);
  1740. read_more:
  1741. disk = read_balance(conf, r1_bio, &max_sectors);
  1742. if (disk == -1) {
  1743. printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
  1744. " read error for block %llu\n",
  1745. mdname(mddev), b, (unsigned long long)r1_bio->sector);
  1746. raid_end_bio_io(r1_bio);
  1747. } else {
  1748. const unsigned long do_sync
  1749. = r1_bio->master_bio->bi_rw & REQ_SYNC;
  1750. if (bio) {
  1751. r1_bio->bios[r1_bio->read_disk] =
  1752. mddev->ro ? IO_BLOCKED : NULL;
  1753. bio_put(bio);
  1754. }
  1755. r1_bio->read_disk = disk;
  1756. bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
  1757. md_trim_bio(bio, r1_bio->sector - bio->bi_sector, max_sectors);
  1758. r1_bio->bios[r1_bio->read_disk] = bio;
  1759. rdev = conf->mirrors[disk].rdev;
  1760. printk_ratelimited(KERN_ERR
  1761. "md/raid1:%s: redirecting sector %llu"
  1762. " to other mirror: %s\n",
  1763. mdname(mddev),
  1764. (unsigned long long)r1_bio->sector,
  1765. bdevname(rdev->bdev, b));
  1766. bio->bi_sector = r1_bio->sector + rdev->data_offset;
  1767. bio->bi_bdev = rdev->bdev;
  1768. bio->bi_end_io = raid1_end_read_request;
  1769. bio->bi_rw = READ | do_sync;
  1770. bio->bi_private = r1_bio;
  1771. if (max_sectors < r1_bio->sectors) {
  1772. /* Drat - have to split this up more */
  1773. struct bio *mbio = r1_bio->master_bio;
  1774. int sectors_handled = (r1_bio->sector + max_sectors
  1775. - mbio->bi_sector);
  1776. r1_bio->sectors = max_sectors;
  1777. spin_lock_irq(&conf->device_lock);
  1778. if (mbio->bi_phys_segments == 0)
  1779. mbio->bi_phys_segments = 2;
  1780. else
  1781. mbio->bi_phys_segments++;
  1782. spin_unlock_irq(&conf->device_lock);
  1783. generic_make_request(bio);
  1784. bio = NULL;
  1785. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  1786. r1_bio->master_bio = mbio;
  1787. r1_bio->sectors = (mbio->bi_size >> 9)
  1788. - sectors_handled;
  1789. r1_bio->state = 0;
  1790. set_bit(R1BIO_ReadError, &r1_bio->state);
  1791. r1_bio->mddev = mddev;
  1792. r1_bio->sector = mbio->bi_sector + sectors_handled;
  1793. goto read_more;
  1794. } else
  1795. generic_make_request(bio);
  1796. }
  1797. }
  1798. static void raid1d(mddev_t *mddev)
  1799. {
  1800. r1bio_t *r1_bio;
  1801. unsigned long flags;
  1802. conf_t *conf = mddev->private;
  1803. struct list_head *head = &conf->retry_list;
  1804. struct blk_plug plug;
  1805. md_check_recovery(mddev);
  1806. blk_start_plug(&plug);
  1807. for (;;) {
  1808. if (atomic_read(&mddev->plug_cnt) == 0)
  1809. flush_pending_writes(conf);
  1810. spin_lock_irqsave(&conf->device_lock, flags);
  1811. if (list_empty(head)) {
  1812. spin_unlock_irqrestore(&conf->device_lock, flags);
  1813. break;
  1814. }
  1815. r1_bio = list_entry(head->prev, r1bio_t, retry_list);
  1816. list_del(head->prev);
  1817. conf->nr_queued--;
  1818. spin_unlock_irqrestore(&conf->device_lock, flags);
  1819. mddev = r1_bio->mddev;
  1820. conf = mddev->private;
  1821. if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
  1822. if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  1823. test_bit(R1BIO_WriteError, &r1_bio->state))
  1824. handle_sync_write_finished(conf, r1_bio);
  1825. else
  1826. sync_request_write(mddev, r1_bio);
  1827. } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
  1828. test_bit(R1BIO_WriteError, &r1_bio->state))
  1829. handle_write_finished(conf, r1_bio);
  1830. else if (test_bit(R1BIO_ReadError, &r1_bio->state))
  1831. handle_read_error(conf, r1_bio);
  1832. else
  1833. /* just a partial read to be scheduled from separate
  1834. * context
  1835. */
  1836. generic_make_request(r1_bio->bios[r1_bio->read_disk]);
  1837. cond_resched();
  1838. if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
  1839. md_check_recovery(mddev);
  1840. }
  1841. blk_finish_plug(&plug);
  1842. }
  1843. static int init_resync(conf_t *conf)
  1844. {
  1845. int buffs;
  1846. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  1847. BUG_ON(conf->r1buf_pool);
  1848. conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
  1849. conf->poolinfo);
  1850. if (!conf->r1buf_pool)
  1851. return -ENOMEM;
  1852. conf->next_resync = 0;
  1853. return 0;
  1854. }
  1855. /*
  1856. * perform a "sync" on one "block"
  1857. *
  1858. * We need to make sure that no normal I/O request - particularly write
  1859. * requests - conflict with active sync requests.
  1860. *
  1861. * This is achieved by tracking pending requests and a 'barrier' concept
  1862. * that can be installed to exclude normal IO requests.
  1863. */
  1864. static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  1865. {
  1866. conf_t *conf = mddev->private;
  1867. r1bio_t *r1_bio;
  1868. struct bio *bio;
  1869. sector_t max_sector, nr_sectors;
  1870. int disk = -1;
  1871. int i;
  1872. int wonly = -1;
  1873. int write_targets = 0, read_targets = 0;
  1874. sector_t sync_blocks;
  1875. int still_degraded = 0;
  1876. int good_sectors = RESYNC_SECTORS;
  1877. int min_bad = 0; /* number of sectors that are bad in all devices */
  1878. if (!conf->r1buf_pool)
  1879. if (init_resync(conf))
  1880. return 0;
  1881. max_sector = mddev->dev_sectors;
  1882. if (sector_nr >= max_sector) {
  1883. /* If we aborted, we need to abort the
  1884. * sync on the 'current' bitmap chunk (there will
  1885. * only be one in raid1 resync.
  1886. * We can find the current addess in mddev->curr_resync
  1887. */
  1888. if (mddev->curr_resync < max_sector) /* aborted */
  1889. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1890. &sync_blocks, 1);
  1891. else /* completed sync */
  1892. conf->fullsync = 0;
  1893. bitmap_close_sync(mddev->bitmap);
  1894. close_sync(conf);
  1895. return 0;
  1896. }
  1897. if (mddev->bitmap == NULL &&
  1898. mddev->recovery_cp == MaxSector &&
  1899. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  1900. conf->fullsync == 0) {
  1901. *skipped = 1;
  1902. return max_sector - sector_nr;
  1903. }
  1904. /* before building a request, check if we can skip these blocks..
  1905. * This call the bitmap_start_sync doesn't actually record anything
  1906. */
  1907. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  1908. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  1909. /* We can skip this block, and probably several more */
  1910. *skipped = 1;
  1911. return sync_blocks;
  1912. }
  1913. /*
  1914. * If there is non-resync activity waiting for a turn,
  1915. * and resync is going fast enough,
  1916. * then let it though before starting on this new sync request.
  1917. */
  1918. if (!go_faster && conf->nr_waiting)
  1919. msleep_interruptible(1000);
  1920. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  1921. r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
  1922. raise_barrier(conf);
  1923. conf->next_resync = sector_nr;
  1924. rcu_read_lock();
  1925. /*
  1926. * If we get a correctably read error during resync or recovery,
  1927. * we might want to read from a different device. So we
  1928. * flag all drives that could conceivably be read from for READ,
  1929. * and any others (which will be non-In_sync devices) for WRITE.
  1930. * If a read fails, we try reading from something else for which READ
  1931. * is OK.
  1932. */
  1933. r1_bio->mddev = mddev;
  1934. r1_bio->sector = sector_nr;
  1935. r1_bio->state = 0;
  1936. set_bit(R1BIO_IsSync, &r1_bio->state);
  1937. for (i=0; i < conf->raid_disks; i++) {
  1938. mdk_rdev_t *rdev;
  1939. bio = r1_bio->bios[i];
  1940. /* take from bio_init */
  1941. bio->bi_next = NULL;
  1942. bio->bi_flags &= ~(BIO_POOL_MASK-1);
  1943. bio->bi_flags |= 1 << BIO_UPTODATE;
  1944. bio->bi_comp_cpu = -1;
  1945. bio->bi_rw = READ;
  1946. bio->bi_vcnt = 0;
  1947. bio->bi_idx = 0;
  1948. bio->bi_phys_segments = 0;
  1949. bio->bi_size = 0;
  1950. bio->bi_end_io = NULL;
  1951. bio->bi_private = NULL;
  1952. rdev = rcu_dereference(conf->mirrors[i].rdev);
  1953. if (rdev == NULL ||
  1954. test_bit(Faulty, &rdev->flags)) {
  1955. still_degraded = 1;
  1956. } else if (!test_bit(In_sync, &rdev->flags)) {
  1957. bio->bi_rw = WRITE;
  1958. bio->bi_end_io = end_sync_write;
  1959. write_targets ++;
  1960. } else {
  1961. /* may need to read from here */
  1962. sector_t first_bad = MaxSector;
  1963. int bad_sectors;
  1964. if (is_badblock(rdev, sector_nr, good_sectors,
  1965. &first_bad, &bad_sectors)) {
  1966. if (first_bad > sector_nr)
  1967. good_sectors = first_bad - sector_nr;
  1968. else {
  1969. bad_sectors -= (sector_nr - first_bad);
  1970. if (min_bad == 0 ||
  1971. min_bad > bad_sectors)
  1972. min_bad = bad_sectors;
  1973. }
  1974. }
  1975. if (sector_nr < first_bad) {
  1976. if (test_bit(WriteMostly, &rdev->flags)) {
  1977. if (wonly < 0)
  1978. wonly = i;
  1979. } else {
  1980. if (disk < 0)
  1981. disk = i;
  1982. }
  1983. bio->bi_rw = READ;
  1984. bio->bi_end_io = end_sync_read;
  1985. read_targets++;
  1986. }
  1987. }
  1988. if (bio->bi_end_io) {
  1989. atomic_inc(&rdev->nr_pending);
  1990. bio->bi_sector = sector_nr + rdev->data_offset;
  1991. bio->bi_bdev = rdev->bdev;
  1992. bio->bi_private = r1_bio;
  1993. }
  1994. }
  1995. rcu_read_unlock();
  1996. if (disk < 0)
  1997. disk = wonly;
  1998. r1_bio->read_disk = disk;
  1999. if (read_targets == 0 && min_bad > 0) {
  2000. /* These sectors are bad on all InSync devices, so we
  2001. * need to mark them bad on all write targets
  2002. */
  2003. int ok = 1;
  2004. for (i = 0 ; i < conf->raid_disks ; i++)
  2005. if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
  2006. mdk_rdev_t *rdev =
  2007. rcu_dereference(conf->mirrors[i].rdev);
  2008. ok = rdev_set_badblocks(rdev, sector_nr,
  2009. min_bad, 0
  2010. ) && ok;
  2011. }
  2012. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  2013. *skipped = 1;
  2014. put_buf(r1_bio);
  2015. if (!ok) {
  2016. /* Cannot record the badblocks, so need to
  2017. * abort the resync.
  2018. * If there are multiple read targets, could just
  2019. * fail the really bad ones ???
  2020. */
  2021. conf->recovery_disabled = mddev->recovery_disabled;
  2022. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  2023. return 0;
  2024. } else
  2025. return min_bad;
  2026. }
  2027. if (min_bad > 0 && min_bad < good_sectors) {
  2028. /* only resync enough to reach the next bad->good
  2029. * transition */
  2030. good_sectors = min_bad;
  2031. }
  2032. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
  2033. /* extra read targets are also write targets */
  2034. write_targets += read_targets-1;
  2035. if (write_targets == 0 || read_targets == 0) {
  2036. /* There is nowhere to write, so all non-sync
  2037. * drives must be failed - so we are finished
  2038. */
  2039. sector_t rv = max_sector - sector_nr;
  2040. *skipped = 1;
  2041. put_buf(r1_bio);
  2042. return rv;
  2043. }
  2044. if (max_sector > mddev->resync_max)
  2045. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2046. if (max_sector > sector_nr + good_sectors)
  2047. max_sector = sector_nr + good_sectors;
  2048. nr_sectors = 0;
  2049. sync_blocks = 0;
  2050. do {
  2051. struct page *page;
  2052. int len = PAGE_SIZE;
  2053. if (sector_nr + (len>>9) > max_sector)
  2054. len = (max_sector - sector_nr) << 9;
  2055. if (len == 0)
  2056. break;
  2057. if (sync_blocks == 0) {
  2058. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  2059. &sync_blocks, still_degraded) &&
  2060. !conf->fullsync &&
  2061. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  2062. break;
  2063. BUG_ON(sync_blocks < (PAGE_SIZE>>9));
  2064. if ((len >> 9) > sync_blocks)
  2065. len = sync_blocks<<9;
  2066. }
  2067. for (i=0 ; i < conf->raid_disks; i++) {
  2068. bio = r1_bio->bios[i];
  2069. if (bio->bi_end_io) {
  2070. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  2071. if (bio_add_page(bio, page, len, 0) == 0) {
  2072. /* stop here */
  2073. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  2074. while (i > 0) {
  2075. i--;
  2076. bio = r1_bio->bios[i];
  2077. if (bio->bi_end_io==NULL)
  2078. continue;
  2079. /* remove last page from this bio */
  2080. bio->bi_vcnt--;
  2081. bio->bi_size -= len;
  2082. bio->bi_flags &= ~(1<< BIO_SEG_VALID);
  2083. }
  2084. goto bio_full;
  2085. }
  2086. }
  2087. }
  2088. nr_sectors += len>>9;
  2089. sector_nr += len>>9;
  2090. sync_blocks -= (len>>9);
  2091. } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
  2092. bio_full:
  2093. r1_bio->sectors = nr_sectors;
  2094. /* For a user-requested sync, we read all readable devices and do a
  2095. * compare
  2096. */
  2097. if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  2098. atomic_set(&r1_bio->remaining, read_targets);
  2099. for (i=0; i<conf->raid_disks; i++) {
  2100. bio = r1_bio->bios[i];
  2101. if (bio->bi_end_io == end_sync_read) {
  2102. md_sync_acct(bio->bi_bdev, nr_sectors);
  2103. generic_make_request(bio);
  2104. }
  2105. }
  2106. } else {
  2107. atomic_set(&r1_bio->remaining, 1);
  2108. bio = r1_bio->bios[r1_bio->read_disk];
  2109. md_sync_acct(bio->bi_bdev, nr_sectors);
  2110. generic_make_request(bio);
  2111. }
  2112. return nr_sectors;
  2113. }
  2114. static sector_t raid1_size(mddev_t *mddev, sector_t sectors, int raid_disks)
  2115. {
  2116. if (sectors)
  2117. return sectors;
  2118. return mddev->dev_sectors;
  2119. }
  2120. static conf_t *setup_conf(mddev_t *mddev)
  2121. {
  2122. conf_t *conf;
  2123. int i;
  2124. mirror_info_t *disk;
  2125. mdk_rdev_t *rdev;
  2126. int err = -ENOMEM;
  2127. conf = kzalloc(sizeof(conf_t), GFP_KERNEL);
  2128. if (!conf)
  2129. goto abort;
  2130. conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  2131. GFP_KERNEL);
  2132. if (!conf->mirrors)
  2133. goto abort;
  2134. conf->tmppage = alloc_page(GFP_KERNEL);
  2135. if (!conf->tmppage)
  2136. goto abort;
  2137. conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
  2138. if (!conf->poolinfo)
  2139. goto abort;
  2140. conf->poolinfo->raid_disks = mddev->raid_disks;
  2141. conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  2142. r1bio_pool_free,
  2143. conf->poolinfo);
  2144. if (!conf->r1bio_pool)
  2145. goto abort;
  2146. conf->poolinfo->mddev = mddev;
  2147. spin_lock_init(&conf->device_lock);
  2148. list_for_each_entry(rdev, &mddev->disks, same_set) {
  2149. int disk_idx = rdev->raid_disk;
  2150. if (disk_idx >= mddev->raid_disks
  2151. || disk_idx < 0)
  2152. continue;
  2153. disk = conf->mirrors + disk_idx;
  2154. disk->rdev = rdev;
  2155. disk->head_position = 0;
  2156. }
  2157. conf->raid_disks = mddev->raid_disks;
  2158. conf->mddev = mddev;
  2159. INIT_LIST_HEAD(&conf->retry_list);
  2160. spin_lock_init(&conf->resync_lock);
  2161. init_waitqueue_head(&conf->wait_barrier);
  2162. bio_list_init(&conf->pending_bio_list);
  2163. conf->last_used = -1;
  2164. for (i = 0; i < conf->raid_disks; i++) {
  2165. disk = conf->mirrors + i;
  2166. if (!disk->rdev ||
  2167. !test_bit(In_sync, &disk->rdev->flags)) {
  2168. disk->head_position = 0;
  2169. if (disk->rdev)
  2170. conf->fullsync = 1;
  2171. } else if (conf->last_used < 0)
  2172. /*
  2173. * The first working device is used as a
  2174. * starting point to read balancing.
  2175. */
  2176. conf->last_used = i;
  2177. }
  2178. err = -EIO;
  2179. if (conf->last_used < 0) {
  2180. printk(KERN_ERR "md/raid1:%s: no operational mirrors\n",
  2181. mdname(mddev));
  2182. goto abort;
  2183. }
  2184. err = -ENOMEM;
  2185. conf->thread = md_register_thread(raid1d, mddev, NULL);
  2186. if (!conf->thread) {
  2187. printk(KERN_ERR
  2188. "md/raid1:%s: couldn't allocate thread\n",
  2189. mdname(mddev));
  2190. goto abort;
  2191. }
  2192. return conf;
  2193. abort:
  2194. if (conf) {
  2195. if (conf->r1bio_pool)
  2196. mempool_destroy(conf->r1bio_pool);
  2197. kfree(conf->mirrors);
  2198. safe_put_page(conf->tmppage);
  2199. kfree(conf->poolinfo);
  2200. kfree(conf);
  2201. }
  2202. return ERR_PTR(err);
  2203. }
  2204. static int run(mddev_t *mddev)
  2205. {
  2206. conf_t *conf;
  2207. int i;
  2208. mdk_rdev_t *rdev;
  2209. if (mddev->level != 1) {
  2210. printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
  2211. mdname(mddev), mddev->level);
  2212. return -EIO;
  2213. }
  2214. if (mddev->reshape_position != MaxSector) {
  2215. printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
  2216. mdname(mddev));
  2217. return -EIO;
  2218. }
  2219. /*
  2220. * copy the already verified devices into our private RAID1
  2221. * bookkeeping area. [whatever we allocate in run(),
  2222. * should be freed in stop()]
  2223. */
  2224. if (mddev->private == NULL)
  2225. conf = setup_conf(mddev);
  2226. else
  2227. conf = mddev->private;
  2228. if (IS_ERR(conf))
  2229. return PTR_ERR(conf);
  2230. list_for_each_entry(rdev, &mddev->disks, same_set) {
  2231. if (!mddev->gendisk)
  2232. continue;
  2233. disk_stack_limits(mddev->gendisk, rdev->bdev,
  2234. rdev->data_offset << 9);
  2235. /* as we don't honour merge_bvec_fn, we must never risk
  2236. * violating it, so limit ->max_segments to 1 lying within
  2237. * a single page, as a one page request is never in violation.
  2238. */
  2239. if (rdev->bdev->bd_disk->queue->merge_bvec_fn) {
  2240. blk_queue_max_segments(mddev->queue, 1);
  2241. blk_queue_segment_boundary(mddev->queue,
  2242. PAGE_CACHE_SIZE - 1);
  2243. }
  2244. }
  2245. mddev->degraded = 0;
  2246. for (i=0; i < conf->raid_disks; i++)
  2247. if (conf->mirrors[i].rdev == NULL ||
  2248. !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
  2249. test_bit(Faulty, &conf->mirrors[i].rdev->flags))
  2250. mddev->degraded++;
  2251. if (conf->raid_disks - mddev->degraded == 1)
  2252. mddev->recovery_cp = MaxSector;
  2253. if (mddev->recovery_cp != MaxSector)
  2254. printk(KERN_NOTICE "md/raid1:%s: not clean"
  2255. " -- starting background reconstruction\n",
  2256. mdname(mddev));
  2257. printk(KERN_INFO
  2258. "md/raid1:%s: active with %d out of %d mirrors\n",
  2259. mdname(mddev), mddev->raid_disks - mddev->degraded,
  2260. mddev->raid_disks);
  2261. /*
  2262. * Ok, everything is just fine now
  2263. */
  2264. mddev->thread = conf->thread;
  2265. conf->thread = NULL;
  2266. mddev->private = conf;
  2267. md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
  2268. if (mddev->queue) {
  2269. mddev->queue->backing_dev_info.congested_fn = raid1_congested;
  2270. mddev->queue->backing_dev_info.congested_data = mddev;
  2271. }
  2272. return md_integrity_register(mddev);
  2273. }
  2274. static int stop(mddev_t *mddev)
  2275. {
  2276. conf_t *conf = mddev->private;
  2277. struct bitmap *bitmap = mddev->bitmap;
  2278. /* wait for behind writes to complete */
  2279. if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
  2280. printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
  2281. mdname(mddev));
  2282. /* need to kick something here to make sure I/O goes? */
  2283. wait_event(bitmap->behind_wait,
  2284. atomic_read(&bitmap->behind_writes) == 0);
  2285. }
  2286. raise_barrier(conf);
  2287. lower_barrier(conf);
  2288. md_unregister_thread(mddev->thread);
  2289. mddev->thread = NULL;
  2290. if (conf->r1bio_pool)
  2291. mempool_destroy(conf->r1bio_pool);
  2292. kfree(conf->mirrors);
  2293. kfree(conf->poolinfo);
  2294. kfree(conf);
  2295. mddev->private = NULL;
  2296. return 0;
  2297. }
  2298. static int raid1_resize(mddev_t *mddev, sector_t sectors)
  2299. {
  2300. /* no resync is happening, and there is enough space
  2301. * on all devices, so we can resize.
  2302. * We need to make sure resync covers any new space.
  2303. * If the array is shrinking we should possibly wait until
  2304. * any io in the removed space completes, but it hardly seems
  2305. * worth it.
  2306. */
  2307. md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
  2308. if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
  2309. return -EINVAL;
  2310. set_capacity(mddev->gendisk, mddev->array_sectors);
  2311. revalidate_disk(mddev->gendisk);
  2312. if (sectors > mddev->dev_sectors &&
  2313. mddev->recovery_cp > mddev->dev_sectors) {
  2314. mddev->recovery_cp = mddev->dev_sectors;
  2315. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  2316. }
  2317. mddev->dev_sectors = sectors;
  2318. mddev->resync_max_sectors = sectors;
  2319. return 0;
  2320. }
  2321. static int raid1_reshape(mddev_t *mddev)
  2322. {
  2323. /* We need to:
  2324. * 1/ resize the r1bio_pool
  2325. * 2/ resize conf->mirrors
  2326. *
  2327. * We allocate a new r1bio_pool if we can.
  2328. * Then raise a device barrier and wait until all IO stops.
  2329. * Then resize conf->mirrors and swap in the new r1bio pool.
  2330. *
  2331. * At the same time, we "pack" the devices so that all the missing
  2332. * devices have the higher raid_disk numbers.
  2333. */
  2334. mempool_t *newpool, *oldpool;
  2335. struct pool_info *newpoolinfo;
  2336. mirror_info_t *newmirrors;
  2337. conf_t *conf = mddev->private;
  2338. int cnt, raid_disks;
  2339. unsigned long flags;
  2340. int d, d2, err;
  2341. /* Cannot change chunk_size, layout, or level */
  2342. if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
  2343. mddev->layout != mddev->new_layout ||
  2344. mddev->level != mddev->new_level) {
  2345. mddev->new_chunk_sectors = mddev->chunk_sectors;
  2346. mddev->new_layout = mddev->layout;
  2347. mddev->new_level = mddev->level;
  2348. return -EINVAL;
  2349. }
  2350. err = md_allow_write(mddev);
  2351. if (err)
  2352. return err;
  2353. raid_disks = mddev->raid_disks + mddev->delta_disks;
  2354. if (raid_disks < conf->raid_disks) {
  2355. cnt=0;
  2356. for (d= 0; d < conf->raid_disks; d++)
  2357. if (conf->mirrors[d].rdev)
  2358. cnt++;
  2359. if (cnt > raid_disks)
  2360. return -EBUSY;
  2361. }
  2362. newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
  2363. if (!newpoolinfo)
  2364. return -ENOMEM;
  2365. newpoolinfo->mddev = mddev;
  2366. newpoolinfo->raid_disks = raid_disks;
  2367. newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  2368. r1bio_pool_free, newpoolinfo);
  2369. if (!newpool) {
  2370. kfree(newpoolinfo);
  2371. return -ENOMEM;
  2372. }
  2373. newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
  2374. if (!newmirrors) {
  2375. kfree(newpoolinfo);
  2376. mempool_destroy(newpool);
  2377. return -ENOMEM;
  2378. }
  2379. raise_barrier(conf);
  2380. /* ok, everything is stopped */
  2381. oldpool = conf->r1bio_pool;
  2382. conf->r1bio_pool = newpool;
  2383. for (d = d2 = 0; d < conf->raid_disks; d++) {
  2384. mdk_rdev_t *rdev = conf->mirrors[d].rdev;
  2385. if (rdev && rdev->raid_disk != d2) {
  2386. sysfs_unlink_rdev(mddev, rdev);
  2387. rdev->raid_disk = d2;
  2388. sysfs_unlink_rdev(mddev, rdev);
  2389. if (sysfs_link_rdev(mddev, rdev))
  2390. printk(KERN_WARNING
  2391. "md/raid1:%s: cannot register rd%d\n",
  2392. mdname(mddev), rdev->raid_disk);
  2393. }
  2394. if (rdev)
  2395. newmirrors[d2++].rdev = rdev;
  2396. }
  2397. kfree(conf->mirrors);
  2398. conf->mirrors = newmirrors;
  2399. kfree(conf->poolinfo);
  2400. conf->poolinfo = newpoolinfo;
  2401. spin_lock_irqsave(&conf->device_lock, flags);
  2402. mddev->degraded += (raid_disks - conf->raid_disks);
  2403. spin_unlock_irqrestore(&conf->device_lock, flags);
  2404. conf->raid_disks = mddev->raid_disks = raid_disks;
  2405. mddev->delta_disks = 0;
  2406. conf->last_used = 0; /* just make sure it is in-range */
  2407. lower_barrier(conf);
  2408. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  2409. md_wakeup_thread(mddev->thread);
  2410. mempool_destroy(oldpool);
  2411. return 0;
  2412. }
  2413. static void raid1_quiesce(mddev_t *mddev, int state)
  2414. {
  2415. conf_t *conf = mddev->private;
  2416. switch(state) {
  2417. case 2: /* wake for suspend */
  2418. wake_up(&conf->wait_barrier);
  2419. break;
  2420. case 1:
  2421. raise_barrier(conf);
  2422. break;
  2423. case 0:
  2424. lower_barrier(conf);
  2425. break;
  2426. }
  2427. }
  2428. static void *raid1_takeover(mddev_t *mddev)
  2429. {
  2430. /* raid1 can take over:
  2431. * raid5 with 2 devices, any layout or chunk size
  2432. */
  2433. if (mddev->level == 5 && mddev->raid_disks == 2) {
  2434. conf_t *conf;
  2435. mddev->new_level = 1;
  2436. mddev->new_layout = 0;
  2437. mddev->new_chunk_sectors = 0;
  2438. conf = setup_conf(mddev);
  2439. if (!IS_ERR(conf))
  2440. conf->barrier = 1;
  2441. return conf;
  2442. }
  2443. return ERR_PTR(-EINVAL);
  2444. }
  2445. static struct mdk_personality raid1_personality =
  2446. {
  2447. .name = "raid1",
  2448. .level = 1,
  2449. .owner = THIS_MODULE,
  2450. .make_request = make_request,
  2451. .run = run,
  2452. .stop = stop,
  2453. .status = status,
  2454. .error_handler = error,
  2455. .hot_add_disk = raid1_add_disk,
  2456. .hot_remove_disk= raid1_remove_disk,
  2457. .spare_active = raid1_spare_active,
  2458. .sync_request = sync_request,
  2459. .resize = raid1_resize,
  2460. .size = raid1_size,
  2461. .check_reshape = raid1_reshape,
  2462. .quiesce = raid1_quiesce,
  2463. .takeover = raid1_takeover,
  2464. };
  2465. static int __init raid_init(void)
  2466. {
  2467. return register_md_personality(&raid1_personality);
  2468. }
  2469. static void raid_exit(void)
  2470. {
  2471. unregister_md_personality(&raid1_personality);
  2472. }
  2473. module_init(raid_init);
  2474. module_exit(raid_exit);
  2475. MODULE_LICENSE("GPL");
  2476. MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
  2477. MODULE_ALIAS("md-personality-3"); /* RAID1 */
  2478. MODULE_ALIAS("md-raid1");
  2479. MODULE_ALIAS("md-level-1");