raid1.c 59 KB

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