raid1.c 58 KB

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