raid1.c 62 KB

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