raid1.c 59 KB

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