raid1.c 62 KB

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