raid1.c 51 KB

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