raid1.c 58 KB

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