raid1.c 61 KB

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