raid1.c 60 KB

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