raid1.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  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)
  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. !rdev || !test_bit(In_sync, &rdev->flags)
  365. || test_bit(WriteMostly, &rdev->flags);
  366. rdev = rcu_dereference(conf->mirrors[++new_disk].rdev)) {
  367. if (rdev && test_bit(In_sync, &rdev->flags))
  368. wonly_disk = new_disk;
  369. if (new_disk == conf->raid_disks - 1) {
  370. new_disk = wonly_disk;
  371. break;
  372. }
  373. }
  374. goto rb_out;
  375. }
  376. /* make sure the disk is operational */
  377. for (rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
  378. !rdev || !test_bit(In_sync, &rdev->flags) ||
  379. test_bit(WriteMostly, &rdev->flags);
  380. rdev = rcu_dereference(conf->mirrors[new_disk].rdev)) {
  381. if (rdev && test_bit(In_sync, &rdev->flags))
  382. wonly_disk = new_disk;
  383. if (new_disk <= 0)
  384. new_disk = conf->raid_disks;
  385. new_disk--;
  386. if (new_disk == disk) {
  387. new_disk = wonly_disk;
  388. break;
  389. }
  390. }
  391. if (new_disk < 0)
  392. goto rb_out;
  393. disk = new_disk;
  394. /* now disk == new_disk == starting point for search */
  395. /*
  396. * Don't change to another disk for sequential reads:
  397. */
  398. if (conf->next_seq_sect == this_sector)
  399. goto rb_out;
  400. if (this_sector == conf->mirrors[new_disk].head_position)
  401. goto rb_out;
  402. current_distance = abs(this_sector - conf->mirrors[disk].head_position);
  403. /* Find the disk whose head is closest */
  404. do {
  405. if (disk <= 0)
  406. disk = conf->raid_disks;
  407. disk--;
  408. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  409. if (!rdev ||
  410. !test_bit(In_sync, &rdev->flags) ||
  411. test_bit(WriteMostly, &rdev->flags))
  412. continue;
  413. if (!atomic_read(&rdev->nr_pending)) {
  414. new_disk = disk;
  415. break;
  416. }
  417. new_distance = abs(this_sector - conf->mirrors[disk].head_position);
  418. if (new_distance < current_distance) {
  419. current_distance = new_distance;
  420. new_disk = disk;
  421. }
  422. } while (disk != conf->last_used);
  423. rb_out:
  424. if (new_disk >= 0) {
  425. rdev = rcu_dereference(conf->mirrors[new_disk].rdev);
  426. if (!rdev)
  427. goto retry;
  428. atomic_inc(&rdev->nr_pending);
  429. if (!test_bit(In_sync, &rdev->flags)) {
  430. /* cannot risk returning a device that failed
  431. * before we inc'ed nr_pending
  432. */
  433. atomic_dec(&rdev->nr_pending);
  434. goto retry;
  435. }
  436. conf->next_seq_sect = this_sector + sectors;
  437. conf->last_used = new_disk;
  438. }
  439. rcu_read_unlock();
  440. return new_disk;
  441. }
  442. static void unplug_slaves(mddev_t *mddev)
  443. {
  444. conf_t *conf = mddev_to_conf(mddev);
  445. int i;
  446. rcu_read_lock();
  447. for (i=0; i<mddev->raid_disks; i++) {
  448. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  449. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  450. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  451. atomic_inc(&rdev->nr_pending);
  452. rcu_read_unlock();
  453. if (r_queue->unplug_fn)
  454. r_queue->unplug_fn(r_queue);
  455. rdev_dec_pending(rdev, mddev);
  456. rcu_read_lock();
  457. }
  458. }
  459. rcu_read_unlock();
  460. }
  461. static void raid1_unplug(request_queue_t *q)
  462. {
  463. mddev_t *mddev = q->queuedata;
  464. unplug_slaves(mddev);
  465. md_wakeup_thread(mddev->thread);
  466. }
  467. static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
  468. sector_t *error_sector)
  469. {
  470. mddev_t *mddev = q->queuedata;
  471. conf_t *conf = mddev_to_conf(mddev);
  472. int i, ret = 0;
  473. rcu_read_lock();
  474. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  475. mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev);
  476. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  477. struct block_device *bdev = rdev->bdev;
  478. request_queue_t *r_queue = bdev_get_queue(bdev);
  479. if (!r_queue->issue_flush_fn)
  480. ret = -EOPNOTSUPP;
  481. else {
  482. atomic_inc(&rdev->nr_pending);
  483. rcu_read_unlock();
  484. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  485. error_sector);
  486. rdev_dec_pending(rdev, mddev);
  487. rcu_read_lock();
  488. }
  489. }
  490. }
  491. rcu_read_unlock();
  492. return ret;
  493. }
  494. /* Barriers....
  495. * Sometimes we need to suspend IO while we do something else,
  496. * either some resync/recovery, or reconfigure the array.
  497. * To do this we raise a 'barrier'.
  498. * The 'barrier' is a counter that can be raised multiple times
  499. * to count how many activities are happening which preclude
  500. * normal IO.
  501. * We can only raise the barrier if there is no pending IO.
  502. * i.e. if nr_pending == 0.
  503. * We choose only to raise the barrier if no-one is waiting for the
  504. * barrier to go down. This means that as soon as an IO request
  505. * is ready, no other operations which require a barrier will start
  506. * until the IO request has had a chance.
  507. *
  508. * So: regular IO calls 'wait_barrier'. When that returns there
  509. * is no backgroup IO happening, It must arrange to call
  510. * allow_barrier when it has finished its IO.
  511. * backgroup IO calls must call raise_barrier. Once that returns
  512. * there is no normal IO happeing. It must arrange to call
  513. * lower_barrier when the particular background IO completes.
  514. */
  515. #define RESYNC_DEPTH 32
  516. static void raise_barrier(conf_t *conf)
  517. {
  518. spin_lock_irq(&conf->resync_lock);
  519. /* Wait until no block IO is waiting */
  520. wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
  521. conf->resync_lock,
  522. raid1_unplug(conf->mddev->queue));
  523. /* block any new IO from starting */
  524. conf->barrier++;
  525. /* No wait for all pending IO to complete */
  526. wait_event_lock_irq(conf->wait_barrier,
  527. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  528. conf->resync_lock,
  529. raid1_unplug(conf->mddev->queue));
  530. spin_unlock_irq(&conf->resync_lock);
  531. }
  532. static void lower_barrier(conf_t *conf)
  533. {
  534. unsigned long flags;
  535. spin_lock_irqsave(&conf->resync_lock, flags);
  536. conf->barrier--;
  537. spin_unlock_irqrestore(&conf->resync_lock, flags);
  538. wake_up(&conf->wait_barrier);
  539. }
  540. static void wait_barrier(conf_t *conf)
  541. {
  542. spin_lock_irq(&conf->resync_lock);
  543. if (conf->barrier) {
  544. conf->nr_waiting++;
  545. wait_event_lock_irq(conf->wait_barrier, !conf->barrier,
  546. conf->resync_lock,
  547. raid1_unplug(conf->mddev->queue));
  548. conf->nr_waiting--;
  549. }
  550. conf->nr_pending++;
  551. spin_unlock_irq(&conf->resync_lock);
  552. }
  553. static void allow_barrier(conf_t *conf)
  554. {
  555. unsigned long flags;
  556. spin_lock_irqsave(&conf->resync_lock, flags);
  557. conf->nr_pending--;
  558. spin_unlock_irqrestore(&conf->resync_lock, flags);
  559. wake_up(&conf->wait_barrier);
  560. }
  561. static void freeze_array(conf_t *conf)
  562. {
  563. /* stop syncio and normal IO and wait for everything to
  564. * go quite.
  565. * We increment barrier and nr_waiting, and then
  566. * wait until barrier+nr_pending match nr_queued+2
  567. */
  568. spin_lock_irq(&conf->resync_lock);
  569. conf->barrier++;
  570. conf->nr_waiting++;
  571. wait_event_lock_irq(conf->wait_barrier,
  572. conf->barrier+conf->nr_pending == conf->nr_queued+2,
  573. conf->resync_lock,
  574. raid1_unplug(conf->mddev->queue));
  575. spin_unlock_irq(&conf->resync_lock);
  576. }
  577. static void unfreeze_array(conf_t *conf)
  578. {
  579. /* reverse the effect of the freeze */
  580. spin_lock_irq(&conf->resync_lock);
  581. conf->barrier--;
  582. conf->nr_waiting--;
  583. wake_up(&conf->wait_barrier);
  584. spin_unlock_irq(&conf->resync_lock);
  585. }
  586. /* duplicate the data pages for behind I/O */
  587. static struct page **alloc_behind_pages(struct bio *bio)
  588. {
  589. int i;
  590. struct bio_vec *bvec;
  591. struct page **pages = kmalloc(bio->bi_vcnt * sizeof(struct page *),
  592. GFP_NOIO);
  593. if (unlikely(!pages))
  594. goto do_sync_io;
  595. memset(pages, 0, bio->bi_vcnt * sizeof(struct page *));
  596. bio_for_each_segment(bvec, bio, i) {
  597. pages[i] = alloc_page(GFP_NOIO);
  598. if (unlikely(!pages[i]))
  599. goto do_sync_io;
  600. memcpy(kmap(pages[i]) + bvec->bv_offset,
  601. kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
  602. kunmap(pages[i]);
  603. kunmap(bvec->bv_page);
  604. }
  605. return pages;
  606. do_sync_io:
  607. if (pages)
  608. for (i = 0; i < bio->bi_vcnt && pages[i]; i++)
  609. __free_page(pages[i]);
  610. kfree(pages);
  611. PRINTK("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
  612. return NULL;
  613. }
  614. static int make_request(request_queue_t *q, struct bio * bio)
  615. {
  616. mddev_t *mddev = q->queuedata;
  617. conf_t *conf = mddev_to_conf(mddev);
  618. mirror_info_t *mirror;
  619. r1bio_t *r1_bio;
  620. struct bio *read_bio;
  621. int i, targets = 0, disks;
  622. mdk_rdev_t *rdev;
  623. struct bitmap *bitmap = mddev->bitmap;
  624. unsigned long flags;
  625. struct bio_list bl;
  626. struct page **behind_pages = NULL;
  627. const int rw = bio_data_dir(bio);
  628. int do_barriers;
  629. if (unlikely(!mddev->barriers_work && bio_barrier(bio))) {
  630. bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
  631. return 0;
  632. }
  633. /*
  634. * Register the new request and wait if the reconstruction
  635. * thread has put up a bar for new requests.
  636. * Continue immediately if no resync is active currently.
  637. */
  638. md_write_start(mddev, bio); /* wait on superblock update early */
  639. wait_barrier(conf);
  640. disk_stat_inc(mddev->gendisk, ios[rw]);
  641. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bio));
  642. /*
  643. * make_request() can abort the operation when READA is being
  644. * used and no empty request is available.
  645. *
  646. */
  647. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  648. r1_bio->master_bio = bio;
  649. r1_bio->sectors = bio->bi_size >> 9;
  650. r1_bio->state = 0;
  651. r1_bio->mddev = mddev;
  652. r1_bio->sector = bio->bi_sector;
  653. if (rw == READ) {
  654. /*
  655. * read balancing logic:
  656. */
  657. int rdisk = read_balance(conf, r1_bio);
  658. if (rdisk < 0) {
  659. /* couldn't find anywhere to read from */
  660. raid_end_bio_io(r1_bio);
  661. return 0;
  662. }
  663. mirror = conf->mirrors + rdisk;
  664. r1_bio->read_disk = rdisk;
  665. read_bio = bio_clone(bio, GFP_NOIO);
  666. r1_bio->bios[rdisk] = read_bio;
  667. read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
  668. read_bio->bi_bdev = mirror->rdev->bdev;
  669. read_bio->bi_end_io = raid1_end_read_request;
  670. read_bio->bi_rw = READ;
  671. read_bio->bi_private = r1_bio;
  672. generic_make_request(read_bio);
  673. return 0;
  674. }
  675. /*
  676. * WRITE:
  677. */
  678. /* first select target devices under spinlock and
  679. * inc refcount on their rdev. Record them by setting
  680. * bios[x] to bio
  681. */
  682. disks = conf->raid_disks;
  683. #if 0
  684. { static int first=1;
  685. if (first) printk("First Write sector %llu disks %d\n",
  686. (unsigned long long)r1_bio->sector, disks);
  687. first = 0;
  688. }
  689. #endif
  690. rcu_read_lock();
  691. for (i = 0; i < disks; i++) {
  692. if ((rdev=rcu_dereference(conf->mirrors[i].rdev)) != NULL &&
  693. !test_bit(Faulty, &rdev->flags)) {
  694. atomic_inc(&rdev->nr_pending);
  695. if (test_bit(Faulty, &rdev->flags)) {
  696. atomic_dec(&rdev->nr_pending);
  697. r1_bio->bios[i] = NULL;
  698. } else
  699. r1_bio->bios[i] = bio;
  700. targets++;
  701. } else
  702. r1_bio->bios[i] = NULL;
  703. }
  704. rcu_read_unlock();
  705. BUG_ON(targets == 0); /* we never fail the last device */
  706. if (targets < conf->raid_disks) {
  707. /* array is degraded, we will not clear the bitmap
  708. * on I/O completion (see raid1_end_write_request) */
  709. set_bit(R1BIO_Degraded, &r1_bio->state);
  710. }
  711. /* do behind I/O ? */
  712. if (bitmap &&
  713. atomic_read(&bitmap->behind_writes) < bitmap->max_write_behind &&
  714. (behind_pages = alloc_behind_pages(bio)) != NULL)
  715. set_bit(R1BIO_BehindIO, &r1_bio->state);
  716. atomic_set(&r1_bio->remaining, 0);
  717. atomic_set(&r1_bio->behind_remaining, 0);
  718. do_barriers = bio->bi_rw & BIO_RW_BARRIER;
  719. if (do_barriers)
  720. set_bit(R1BIO_Barrier, &r1_bio->state);
  721. bio_list_init(&bl);
  722. for (i = 0; i < disks; i++) {
  723. struct bio *mbio;
  724. if (!r1_bio->bios[i])
  725. continue;
  726. mbio = bio_clone(bio, GFP_NOIO);
  727. r1_bio->bios[i] = mbio;
  728. mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
  729. mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
  730. mbio->bi_end_io = raid1_end_write_request;
  731. mbio->bi_rw = WRITE | do_barriers;
  732. mbio->bi_private = r1_bio;
  733. if (behind_pages) {
  734. struct bio_vec *bvec;
  735. int j;
  736. /* Yes, I really want the '__' version so that
  737. * we clear any unused pointer in the io_vec, rather
  738. * than leave them unchanged. This is important
  739. * because when we come to free the pages, we won't
  740. * know the originial bi_idx, so we just free
  741. * them all
  742. */
  743. __bio_for_each_segment(bvec, mbio, j, 0)
  744. bvec->bv_page = behind_pages[j];
  745. if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
  746. atomic_inc(&r1_bio->behind_remaining);
  747. }
  748. atomic_inc(&r1_bio->remaining);
  749. bio_list_add(&bl, mbio);
  750. }
  751. kfree(behind_pages); /* the behind pages are attached to the bios now */
  752. bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors,
  753. test_bit(R1BIO_BehindIO, &r1_bio->state));
  754. spin_lock_irqsave(&conf->device_lock, flags);
  755. bio_list_merge(&conf->pending_bio_list, &bl);
  756. bio_list_init(&bl);
  757. blk_plug_device(mddev->queue);
  758. spin_unlock_irqrestore(&conf->device_lock, flags);
  759. #if 0
  760. while ((bio = bio_list_pop(&bl)) != NULL)
  761. generic_make_request(bio);
  762. #endif
  763. return 0;
  764. }
  765. static void status(struct seq_file *seq, mddev_t *mddev)
  766. {
  767. conf_t *conf = mddev_to_conf(mddev);
  768. int i;
  769. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  770. conf->working_disks);
  771. for (i = 0; i < conf->raid_disks; i++)
  772. seq_printf(seq, "%s",
  773. conf->mirrors[i].rdev &&
  774. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  775. seq_printf(seq, "]");
  776. }
  777. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  778. {
  779. char b[BDEVNAME_SIZE];
  780. conf_t *conf = mddev_to_conf(mddev);
  781. /*
  782. * If it is not operational, then we have already marked it as dead
  783. * else if it is the last working disks, ignore the error, let the
  784. * next level up know.
  785. * else mark the drive as failed
  786. */
  787. if (test_bit(In_sync, &rdev->flags)
  788. && conf->working_disks == 1)
  789. /*
  790. * Don't fail the drive, act as though we were just a
  791. * normal single drive
  792. */
  793. return;
  794. if (test_bit(In_sync, &rdev->flags)) {
  795. mddev->degraded++;
  796. conf->working_disks--;
  797. /*
  798. * if recovery is running, make sure it aborts.
  799. */
  800. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  801. }
  802. clear_bit(In_sync, &rdev->flags);
  803. set_bit(Faulty, &rdev->flags);
  804. mddev->sb_dirty = 1;
  805. printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
  806. " Operation continuing on %d devices\n",
  807. bdevname(rdev->bdev,b), conf->working_disks);
  808. }
  809. static void print_conf(conf_t *conf)
  810. {
  811. int i;
  812. mirror_info_t *tmp;
  813. printk("RAID1 conf printout:\n");
  814. if (!conf) {
  815. printk("(!conf)\n");
  816. return;
  817. }
  818. printk(" --- wd:%d rd:%d\n", conf->working_disks,
  819. conf->raid_disks);
  820. for (i = 0; i < conf->raid_disks; i++) {
  821. char b[BDEVNAME_SIZE];
  822. tmp = conf->mirrors + i;
  823. if (tmp->rdev)
  824. printk(" disk %d, wo:%d, o:%d, dev:%s\n",
  825. i, !test_bit(In_sync, &tmp->rdev->flags), !test_bit(Faulty, &tmp->rdev->flags),
  826. bdevname(tmp->rdev->bdev,b));
  827. }
  828. }
  829. static void close_sync(conf_t *conf)
  830. {
  831. wait_barrier(conf);
  832. allow_barrier(conf);
  833. mempool_destroy(conf->r1buf_pool);
  834. conf->r1buf_pool = NULL;
  835. }
  836. static int raid1_spare_active(mddev_t *mddev)
  837. {
  838. int i;
  839. conf_t *conf = mddev->private;
  840. mirror_info_t *tmp;
  841. /*
  842. * Find all failed disks within the RAID1 configuration
  843. * and mark them readable
  844. */
  845. for (i = 0; i < conf->raid_disks; i++) {
  846. tmp = conf->mirrors + i;
  847. if (tmp->rdev
  848. && !test_bit(Faulty, &tmp->rdev->flags)
  849. && !test_bit(In_sync, &tmp->rdev->flags)) {
  850. conf->working_disks++;
  851. mddev->degraded--;
  852. set_bit(In_sync, &tmp->rdev->flags);
  853. }
  854. }
  855. print_conf(conf);
  856. return 0;
  857. }
  858. static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  859. {
  860. conf_t *conf = mddev->private;
  861. int found = 0;
  862. int mirror = 0;
  863. mirror_info_t *p;
  864. for (mirror=0; mirror < mddev->raid_disks; mirror++)
  865. if ( !(p=conf->mirrors+mirror)->rdev) {
  866. blk_queue_stack_limits(mddev->queue,
  867. rdev->bdev->bd_disk->queue);
  868. /* as we don't honour merge_bvec_fn, we must never risk
  869. * violating it, so limit ->max_sector to one PAGE, as
  870. * a one page request is never in violation.
  871. */
  872. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  873. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  874. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  875. p->head_position = 0;
  876. rdev->raid_disk = mirror;
  877. found = 1;
  878. /* As all devices are equivalent, we don't need a full recovery
  879. * if this was recently any drive of the array
  880. */
  881. if (rdev->saved_raid_disk < 0)
  882. conf->fullsync = 1;
  883. rcu_assign_pointer(p->rdev, rdev);
  884. break;
  885. }
  886. print_conf(conf);
  887. return found;
  888. }
  889. static int raid1_remove_disk(mddev_t *mddev, int number)
  890. {
  891. conf_t *conf = mddev->private;
  892. int err = 0;
  893. mdk_rdev_t *rdev;
  894. mirror_info_t *p = conf->mirrors+ number;
  895. print_conf(conf);
  896. rdev = p->rdev;
  897. if (rdev) {
  898. if (test_bit(In_sync, &rdev->flags) ||
  899. atomic_read(&rdev->nr_pending)) {
  900. err = -EBUSY;
  901. goto abort;
  902. }
  903. p->rdev = NULL;
  904. synchronize_rcu();
  905. if (atomic_read(&rdev->nr_pending)) {
  906. /* lost the race, try later */
  907. err = -EBUSY;
  908. p->rdev = rdev;
  909. }
  910. }
  911. abort:
  912. print_conf(conf);
  913. return err;
  914. }
  915. static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
  916. {
  917. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  918. r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
  919. conf_t *conf = mddev_to_conf(r1_bio->mddev);
  920. if (bio->bi_size)
  921. return 1;
  922. if (r1_bio->bios[r1_bio->read_disk] != bio)
  923. BUG();
  924. update_head_pos(r1_bio->read_disk, r1_bio);
  925. /*
  926. * we have read a block, now it needs to be re-written,
  927. * or re-read if the read failed.
  928. * We don't do much here, just schedule handling by raid1d
  929. */
  930. if (!uptodate) {
  931. md_error(r1_bio->mddev,
  932. conf->mirrors[r1_bio->read_disk].rdev);
  933. } else
  934. set_bit(R1BIO_Uptodate, &r1_bio->state);
  935. reschedule_retry(r1_bio);
  936. return 0;
  937. }
  938. static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
  939. {
  940. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  941. r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
  942. mddev_t *mddev = r1_bio->mddev;
  943. conf_t *conf = mddev_to_conf(mddev);
  944. int i;
  945. int mirror=0;
  946. if (bio->bi_size)
  947. return 1;
  948. for (i = 0; i < conf->raid_disks; i++)
  949. if (r1_bio->bios[i] == bio) {
  950. mirror = i;
  951. break;
  952. }
  953. if (!uptodate)
  954. md_error(mddev, conf->mirrors[mirror].rdev);
  955. update_head_pos(mirror, r1_bio);
  956. if (atomic_dec_and_test(&r1_bio->remaining)) {
  957. md_done_sync(mddev, r1_bio->sectors, uptodate);
  958. put_buf(r1_bio);
  959. }
  960. return 0;
  961. }
  962. static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
  963. {
  964. conf_t *conf = mddev_to_conf(mddev);
  965. int i;
  966. int disks = conf->raid_disks;
  967. struct bio *bio, *wbio;
  968. bio = r1_bio->bios[r1_bio->read_disk];
  969. /*
  970. if (r1_bio->sector == 0) printk("First sync write startss\n");
  971. */
  972. /*
  973. * schedule writes
  974. */
  975. if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
  976. /*
  977. * There is no point trying a read-for-reconstruct as
  978. * reconstruct is about to be aborted
  979. */
  980. char b[BDEVNAME_SIZE];
  981. printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
  982. " for block %llu\n",
  983. bdevname(bio->bi_bdev,b),
  984. (unsigned long long)r1_bio->sector);
  985. md_done_sync(mddev, r1_bio->sectors, 0);
  986. put_buf(r1_bio);
  987. return;
  988. }
  989. atomic_set(&r1_bio->remaining, 1);
  990. for (i = 0; i < disks ; i++) {
  991. wbio = r1_bio->bios[i];
  992. if (wbio->bi_end_io == NULL ||
  993. (wbio->bi_end_io == end_sync_read &&
  994. (i == r1_bio->read_disk ||
  995. !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
  996. continue;
  997. wbio->bi_rw = WRITE;
  998. wbio->bi_end_io = end_sync_write;
  999. atomic_inc(&r1_bio->remaining);
  1000. md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
  1001. generic_make_request(wbio);
  1002. }
  1003. if (atomic_dec_and_test(&r1_bio->remaining)) {
  1004. /* if we're here, all write(s) have completed, so clean up */
  1005. md_done_sync(mddev, r1_bio->sectors, 1);
  1006. put_buf(r1_bio);
  1007. }
  1008. }
  1009. /*
  1010. * This is a kernel thread which:
  1011. *
  1012. * 1. Retries failed read operations on working mirrors.
  1013. * 2. Updates the raid superblock when problems encounter.
  1014. * 3. Performs writes following reads for array syncronising.
  1015. */
  1016. static void raid1d(mddev_t *mddev)
  1017. {
  1018. r1bio_t *r1_bio;
  1019. struct bio *bio;
  1020. unsigned long flags;
  1021. conf_t *conf = mddev_to_conf(mddev);
  1022. struct list_head *head = &conf->retry_list;
  1023. int unplug=0;
  1024. mdk_rdev_t *rdev;
  1025. md_check_recovery(mddev);
  1026. for (;;) {
  1027. char b[BDEVNAME_SIZE];
  1028. spin_lock_irqsave(&conf->device_lock, flags);
  1029. if (conf->pending_bio_list.head) {
  1030. bio = bio_list_get(&conf->pending_bio_list);
  1031. blk_remove_plug(mddev->queue);
  1032. spin_unlock_irqrestore(&conf->device_lock, flags);
  1033. /* flush any pending bitmap writes to disk before proceeding w/ I/O */
  1034. if (bitmap_unplug(mddev->bitmap) != 0)
  1035. printk("%s: bitmap file write failed!\n", mdname(mddev));
  1036. while (bio) { /* submit pending writes */
  1037. struct bio *next = bio->bi_next;
  1038. bio->bi_next = NULL;
  1039. generic_make_request(bio);
  1040. bio = next;
  1041. }
  1042. unplug = 1;
  1043. continue;
  1044. }
  1045. if (list_empty(head))
  1046. break;
  1047. r1_bio = list_entry(head->prev, r1bio_t, retry_list);
  1048. list_del(head->prev);
  1049. conf->nr_queued--;
  1050. spin_unlock_irqrestore(&conf->device_lock, flags);
  1051. mddev = r1_bio->mddev;
  1052. conf = mddev_to_conf(mddev);
  1053. if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
  1054. sync_request_write(mddev, r1_bio);
  1055. unplug = 1;
  1056. } else if (test_bit(R1BIO_BarrierRetry, &r1_bio->state)) {
  1057. /* some requests in the r1bio were BIO_RW_BARRIER
  1058. * requests which failed with -ENOTSUPP. Hohumm..
  1059. * Better resubmit without the barrier.
  1060. * We know which devices to resubmit for, because
  1061. * all others have had their bios[] entry cleared.
  1062. */
  1063. int i;
  1064. clear_bit(R1BIO_BarrierRetry, &r1_bio->state);
  1065. clear_bit(R1BIO_Barrier, &r1_bio->state);
  1066. for (i=0; i < conf->raid_disks; i++)
  1067. if (r1_bio->bios[i]) {
  1068. struct bio_vec *bvec;
  1069. int j;
  1070. bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
  1071. /* copy pages from the failed bio, as
  1072. * this might be a write-behind device */
  1073. __bio_for_each_segment(bvec, bio, j, 0)
  1074. bvec->bv_page = bio_iovec_idx(r1_bio->bios[i], j)->bv_page;
  1075. bio_put(r1_bio->bios[i]);
  1076. bio->bi_sector = r1_bio->sector +
  1077. conf->mirrors[i].rdev->data_offset;
  1078. bio->bi_bdev = conf->mirrors[i].rdev->bdev;
  1079. bio->bi_end_io = raid1_end_write_request;
  1080. bio->bi_rw = WRITE;
  1081. bio->bi_private = r1_bio;
  1082. r1_bio->bios[i] = bio;
  1083. generic_make_request(bio);
  1084. }
  1085. } else {
  1086. int disk;
  1087. /* we got a read error. Maybe the drive is bad. Maybe just
  1088. * the block and we can fix it.
  1089. * We freeze all other IO, and try reading the block from
  1090. * other devices. When we find one, we re-write
  1091. * and check it that fixes the read error.
  1092. * This is all done synchronously while the array is
  1093. * frozen
  1094. */
  1095. sector_t sect = r1_bio->sector;
  1096. int sectors = r1_bio->sectors;
  1097. freeze_array(conf);
  1098. while(sectors) {
  1099. int s = sectors;
  1100. int d = r1_bio->read_disk;
  1101. int success = 0;
  1102. if (s > (PAGE_SIZE>>9))
  1103. s = PAGE_SIZE >> 9;
  1104. do {
  1105. rdev = conf->mirrors[d].rdev;
  1106. if (rdev &&
  1107. test_bit(In_sync, &rdev->flags) &&
  1108. sync_page_io(rdev->bdev,
  1109. sect + rdev->data_offset,
  1110. s<<9,
  1111. conf->tmppage, READ))
  1112. success = 1;
  1113. else {
  1114. d++;
  1115. if (d == conf->raid_disks)
  1116. d = 0;
  1117. }
  1118. } while (!success && d != r1_bio->read_disk);
  1119. if (success) {
  1120. /* write it back and re-read */
  1121. while (d != r1_bio->read_disk) {
  1122. if (d==0)
  1123. d = conf->raid_disks;
  1124. d--;
  1125. rdev = conf->mirrors[d].rdev;
  1126. if (rdev &&
  1127. test_bit(In_sync, &rdev->flags)) {
  1128. if (sync_page_io(rdev->bdev,
  1129. sect + rdev->data_offset,
  1130. s<<9, conf->tmppage, WRITE) == 0 ||
  1131. sync_page_io(rdev->bdev,
  1132. sect + rdev->data_offset,
  1133. s<<9, conf->tmppage, READ) == 0) {
  1134. /* Well, this device is dead */
  1135. md_error(mddev, rdev);
  1136. }
  1137. }
  1138. }
  1139. } else {
  1140. /* Cannot read from anywhere -- bye bye array */
  1141. md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
  1142. break;
  1143. }
  1144. sectors -= s;
  1145. sect += s;
  1146. }
  1147. unfreeze_array(conf);
  1148. bio = r1_bio->bios[r1_bio->read_disk];
  1149. if ((disk=read_balance(conf, r1_bio)) == -1) {
  1150. printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
  1151. " read error for block %llu\n",
  1152. bdevname(bio->bi_bdev,b),
  1153. (unsigned long long)r1_bio->sector);
  1154. raid_end_bio_io(r1_bio);
  1155. } else {
  1156. r1_bio->bios[r1_bio->read_disk] = NULL;
  1157. r1_bio->read_disk = disk;
  1158. bio_put(bio);
  1159. bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
  1160. r1_bio->bios[r1_bio->read_disk] = bio;
  1161. rdev = conf->mirrors[disk].rdev;
  1162. if (printk_ratelimit())
  1163. printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
  1164. " another mirror\n",
  1165. bdevname(rdev->bdev,b),
  1166. (unsigned long long)r1_bio->sector);
  1167. bio->bi_sector = r1_bio->sector + rdev->data_offset;
  1168. bio->bi_bdev = rdev->bdev;
  1169. bio->bi_end_io = raid1_end_read_request;
  1170. bio->bi_rw = READ;
  1171. bio->bi_private = r1_bio;
  1172. unplug = 1;
  1173. generic_make_request(bio);
  1174. }
  1175. }
  1176. }
  1177. spin_unlock_irqrestore(&conf->device_lock, flags);
  1178. if (unplug)
  1179. unplug_slaves(mddev);
  1180. }
  1181. static int init_resync(conf_t *conf)
  1182. {
  1183. int buffs;
  1184. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  1185. if (conf->r1buf_pool)
  1186. BUG();
  1187. conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
  1188. conf->poolinfo);
  1189. if (!conf->r1buf_pool)
  1190. return -ENOMEM;
  1191. conf->next_resync = 0;
  1192. return 0;
  1193. }
  1194. /*
  1195. * perform a "sync" on one "block"
  1196. *
  1197. * We need to make sure that no normal I/O request - particularly write
  1198. * requests - conflict with active sync requests.
  1199. *
  1200. * This is achieved by tracking pending requests and a 'barrier' concept
  1201. * that can be installed to exclude normal IO requests.
  1202. */
  1203. static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  1204. {
  1205. conf_t *conf = mddev_to_conf(mddev);
  1206. r1bio_t *r1_bio;
  1207. struct bio *bio;
  1208. sector_t max_sector, nr_sectors;
  1209. int disk = -1;
  1210. int i;
  1211. int wonly = -1;
  1212. int write_targets = 0, read_targets = 0;
  1213. int sync_blocks;
  1214. int still_degraded = 0;
  1215. if (!conf->r1buf_pool)
  1216. {
  1217. /*
  1218. printk("sync start - bitmap %p\n", mddev->bitmap);
  1219. */
  1220. if (init_resync(conf))
  1221. return 0;
  1222. }
  1223. max_sector = mddev->size << 1;
  1224. if (sector_nr >= max_sector) {
  1225. /* If we aborted, we need to abort the
  1226. * sync on the 'current' bitmap chunk (there will
  1227. * only be one in raid1 resync.
  1228. * We can find the current addess in mddev->curr_resync
  1229. */
  1230. if (mddev->curr_resync < max_sector) /* aborted */
  1231. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1232. &sync_blocks, 1);
  1233. else /* completed sync */
  1234. conf->fullsync = 0;
  1235. bitmap_close_sync(mddev->bitmap);
  1236. close_sync(conf);
  1237. return 0;
  1238. }
  1239. /* before building a request, check if we can skip these blocks..
  1240. * This call the bitmap_start_sync doesn't actually record anything
  1241. */
  1242. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  1243. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
  1244. /* We can skip this block, and probably several more */
  1245. *skipped = 1;
  1246. return sync_blocks;
  1247. }
  1248. /*
  1249. * If there is non-resync activity waiting for a turn,
  1250. * and resync is going fast enough,
  1251. * then let it though before starting on this new sync request.
  1252. */
  1253. if (!go_faster && conf->nr_waiting)
  1254. msleep_interruptible(1000);
  1255. raise_barrier(conf);
  1256. conf->next_resync = sector_nr;
  1257. r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
  1258. rcu_read_lock();
  1259. /*
  1260. * If we get a correctably read error during resync or recovery,
  1261. * we might want to read from a different device. So we
  1262. * flag all drives that could conceivably be read from for READ,
  1263. * and any others (which will be non-In_sync devices) for WRITE.
  1264. * If a read fails, we try reading from something else for which READ
  1265. * is OK.
  1266. */
  1267. r1_bio->mddev = mddev;
  1268. r1_bio->sector = sector_nr;
  1269. r1_bio->state = 0;
  1270. set_bit(R1BIO_IsSync, &r1_bio->state);
  1271. for (i=0; i < conf->raid_disks; i++) {
  1272. mdk_rdev_t *rdev;
  1273. bio = r1_bio->bios[i];
  1274. /* take from bio_init */
  1275. bio->bi_next = NULL;
  1276. bio->bi_flags |= 1 << BIO_UPTODATE;
  1277. bio->bi_rw = 0;
  1278. bio->bi_vcnt = 0;
  1279. bio->bi_idx = 0;
  1280. bio->bi_phys_segments = 0;
  1281. bio->bi_hw_segments = 0;
  1282. bio->bi_size = 0;
  1283. bio->bi_end_io = NULL;
  1284. bio->bi_private = NULL;
  1285. rdev = rcu_dereference(conf->mirrors[i].rdev);
  1286. if (rdev == NULL ||
  1287. test_bit(Faulty, &rdev->flags)) {
  1288. still_degraded = 1;
  1289. continue;
  1290. } else if (!test_bit(In_sync, &rdev->flags)) {
  1291. bio->bi_rw = WRITE;
  1292. bio->bi_end_io = end_sync_write;
  1293. write_targets ++;
  1294. } else {
  1295. /* may need to read from here */
  1296. bio->bi_rw = READ;
  1297. bio->bi_end_io = end_sync_read;
  1298. if (test_bit(WriteMostly, &rdev->flags)) {
  1299. if (wonly < 0)
  1300. wonly = i;
  1301. } else {
  1302. if (disk < 0)
  1303. disk = i;
  1304. }
  1305. read_targets++;
  1306. }
  1307. atomic_inc(&rdev->nr_pending);
  1308. bio->bi_sector = sector_nr + rdev->data_offset;
  1309. bio->bi_bdev = rdev->bdev;
  1310. bio->bi_private = r1_bio;
  1311. }
  1312. rcu_read_unlock();
  1313. if (disk < 0)
  1314. disk = wonly;
  1315. r1_bio->read_disk = disk;
  1316. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
  1317. /* extra read targets are also write targets */
  1318. write_targets += read_targets-1;
  1319. if (write_targets == 0 || read_targets == 0) {
  1320. /* There is nowhere to write, so all non-sync
  1321. * drives must be failed - so we are finished
  1322. */
  1323. sector_t rv = max_sector - sector_nr;
  1324. *skipped = 1;
  1325. put_buf(r1_bio);
  1326. return rv;
  1327. }
  1328. nr_sectors = 0;
  1329. sync_blocks = 0;
  1330. do {
  1331. struct page *page;
  1332. int len = PAGE_SIZE;
  1333. if (sector_nr + (len>>9) > max_sector)
  1334. len = (max_sector - sector_nr) << 9;
  1335. if (len == 0)
  1336. break;
  1337. if (sync_blocks == 0) {
  1338. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  1339. &sync_blocks, still_degraded) &&
  1340. !conf->fullsync &&
  1341. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
  1342. break;
  1343. if (sync_blocks < (PAGE_SIZE>>9))
  1344. BUG();
  1345. if (len > (sync_blocks<<9))
  1346. len = sync_blocks<<9;
  1347. }
  1348. for (i=0 ; i < conf->raid_disks; i++) {
  1349. bio = r1_bio->bios[i];
  1350. if (bio->bi_end_io) {
  1351. page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
  1352. if (bio_add_page(bio, page, len, 0) == 0) {
  1353. /* stop here */
  1354. r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
  1355. while (i > 0) {
  1356. i--;
  1357. bio = r1_bio->bios[i];
  1358. if (bio->bi_end_io==NULL)
  1359. continue;
  1360. /* remove last page from this bio */
  1361. bio->bi_vcnt--;
  1362. bio->bi_size -= len;
  1363. bio->bi_flags &= ~(1<< BIO_SEG_VALID);
  1364. }
  1365. goto bio_full;
  1366. }
  1367. }
  1368. }
  1369. nr_sectors += len>>9;
  1370. sector_nr += len>>9;
  1371. sync_blocks -= (len>>9);
  1372. } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
  1373. bio_full:
  1374. bio = r1_bio->bios[r1_bio->read_disk];
  1375. r1_bio->sectors = nr_sectors;
  1376. md_sync_acct(conf->mirrors[r1_bio->read_disk].rdev->bdev, nr_sectors);
  1377. generic_make_request(bio);
  1378. return nr_sectors;
  1379. }
  1380. static int run(mddev_t *mddev)
  1381. {
  1382. conf_t *conf;
  1383. int i, j, disk_idx;
  1384. mirror_info_t *disk;
  1385. mdk_rdev_t *rdev;
  1386. struct list_head *tmp;
  1387. if (mddev->level != 1) {
  1388. printk("raid1: %s: raid level not set to mirroring (%d)\n",
  1389. mdname(mddev), mddev->level);
  1390. goto out;
  1391. }
  1392. /*
  1393. * copy the already verified devices into our private RAID1
  1394. * bookkeeping area. [whatever we allocate in run(),
  1395. * should be freed in stop()]
  1396. */
  1397. conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
  1398. mddev->private = conf;
  1399. if (!conf)
  1400. goto out_no_mem;
  1401. memset(conf, 0, sizeof(*conf));
  1402. conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  1403. GFP_KERNEL);
  1404. if (!conf->mirrors)
  1405. goto out_no_mem;
  1406. memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
  1407. conf->tmppage = alloc_page(GFP_KERNEL);
  1408. if (!conf->tmppage)
  1409. goto out_no_mem;
  1410. conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
  1411. if (!conf->poolinfo)
  1412. goto out_no_mem;
  1413. conf->poolinfo->mddev = mddev;
  1414. conf->poolinfo->raid_disks = mddev->raid_disks;
  1415. conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  1416. r1bio_pool_free,
  1417. conf->poolinfo);
  1418. if (!conf->r1bio_pool)
  1419. goto out_no_mem;
  1420. ITERATE_RDEV(mddev, rdev, tmp) {
  1421. disk_idx = rdev->raid_disk;
  1422. if (disk_idx >= mddev->raid_disks
  1423. || disk_idx < 0)
  1424. continue;
  1425. disk = conf->mirrors + disk_idx;
  1426. disk->rdev = rdev;
  1427. blk_queue_stack_limits(mddev->queue,
  1428. rdev->bdev->bd_disk->queue);
  1429. /* as we don't honour merge_bvec_fn, we must never risk
  1430. * violating it, so limit ->max_sector to one PAGE, as
  1431. * a one page request is never in violation.
  1432. */
  1433. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  1434. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  1435. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  1436. disk->head_position = 0;
  1437. if (!test_bit(Faulty, &rdev->flags) && test_bit(In_sync, &rdev->flags))
  1438. conf->working_disks++;
  1439. }
  1440. conf->raid_disks = mddev->raid_disks;
  1441. conf->mddev = mddev;
  1442. spin_lock_init(&conf->device_lock);
  1443. INIT_LIST_HEAD(&conf->retry_list);
  1444. if (conf->working_disks == 1)
  1445. mddev->recovery_cp = MaxSector;
  1446. spin_lock_init(&conf->resync_lock);
  1447. init_waitqueue_head(&conf->wait_barrier);
  1448. bio_list_init(&conf->pending_bio_list);
  1449. bio_list_init(&conf->flushing_bio_list);
  1450. if (!conf->working_disks) {
  1451. printk(KERN_ERR "raid1: no operational mirrors for %s\n",
  1452. mdname(mddev));
  1453. goto out_free_conf;
  1454. }
  1455. mddev->degraded = 0;
  1456. for (i = 0; i < conf->raid_disks; i++) {
  1457. disk = conf->mirrors + i;
  1458. if (!disk->rdev) {
  1459. disk->head_position = 0;
  1460. mddev->degraded++;
  1461. }
  1462. }
  1463. /*
  1464. * find the first working one and use it as a starting point
  1465. * to read balancing.
  1466. */
  1467. for (j = 0; j < conf->raid_disks &&
  1468. (!conf->mirrors[j].rdev ||
  1469. !test_bit(In_sync, &conf->mirrors[j].rdev->flags)) ; j++)
  1470. /* nothing */;
  1471. conf->last_used = j;
  1472. mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
  1473. if (!mddev->thread) {
  1474. printk(KERN_ERR
  1475. "raid1: couldn't allocate thread for %s\n",
  1476. mdname(mddev));
  1477. goto out_free_conf;
  1478. }
  1479. printk(KERN_INFO
  1480. "raid1: raid set %s active with %d out of %d mirrors\n",
  1481. mdname(mddev), mddev->raid_disks - mddev->degraded,
  1482. mddev->raid_disks);
  1483. /*
  1484. * Ok, everything is just fine now
  1485. */
  1486. mddev->array_size = mddev->size;
  1487. mddev->queue->unplug_fn = raid1_unplug;
  1488. mddev->queue->issue_flush_fn = raid1_issue_flush;
  1489. return 0;
  1490. out_no_mem:
  1491. printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
  1492. mdname(mddev));
  1493. out_free_conf:
  1494. if (conf) {
  1495. if (conf->r1bio_pool)
  1496. mempool_destroy(conf->r1bio_pool);
  1497. kfree(conf->mirrors);
  1498. __free_page(conf->tmppage);
  1499. kfree(conf->poolinfo);
  1500. kfree(conf);
  1501. mddev->private = NULL;
  1502. }
  1503. out:
  1504. return -EIO;
  1505. }
  1506. static int stop(mddev_t *mddev)
  1507. {
  1508. conf_t *conf = mddev_to_conf(mddev);
  1509. struct bitmap *bitmap = mddev->bitmap;
  1510. int behind_wait = 0;
  1511. /* wait for behind writes to complete */
  1512. while (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
  1513. behind_wait++;
  1514. printk(KERN_INFO "raid1: behind writes in progress on device %s, waiting to stop (%d)\n", mdname(mddev), behind_wait);
  1515. set_current_state(TASK_UNINTERRUPTIBLE);
  1516. schedule_timeout(HZ); /* wait a second */
  1517. /* need to kick something here to make sure I/O goes? */
  1518. }
  1519. md_unregister_thread(mddev->thread);
  1520. mddev->thread = NULL;
  1521. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  1522. if (conf->r1bio_pool)
  1523. mempool_destroy(conf->r1bio_pool);
  1524. kfree(conf->mirrors);
  1525. kfree(conf->poolinfo);
  1526. kfree(conf);
  1527. mddev->private = NULL;
  1528. return 0;
  1529. }
  1530. static int raid1_resize(mddev_t *mddev, sector_t sectors)
  1531. {
  1532. /* no resync is happening, and there is enough space
  1533. * on all devices, so we can resize.
  1534. * We need to make sure resync covers any new space.
  1535. * If the array is shrinking we should possibly wait until
  1536. * any io in the removed space completes, but it hardly seems
  1537. * worth it.
  1538. */
  1539. mddev->array_size = sectors>>1;
  1540. set_capacity(mddev->gendisk, mddev->array_size << 1);
  1541. mddev->changed = 1;
  1542. if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
  1543. mddev->recovery_cp = mddev->size << 1;
  1544. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1545. }
  1546. mddev->size = mddev->array_size;
  1547. mddev->resync_max_sectors = sectors;
  1548. return 0;
  1549. }
  1550. static int raid1_reshape(mddev_t *mddev, int raid_disks)
  1551. {
  1552. /* We need to:
  1553. * 1/ resize the r1bio_pool
  1554. * 2/ resize conf->mirrors
  1555. *
  1556. * We allocate a new r1bio_pool if we can.
  1557. * Then raise a device barrier and wait until all IO stops.
  1558. * Then resize conf->mirrors and swap in the new r1bio pool.
  1559. *
  1560. * At the same time, we "pack" the devices so that all the missing
  1561. * devices have the higher raid_disk numbers.
  1562. */
  1563. mempool_t *newpool, *oldpool;
  1564. struct pool_info *newpoolinfo;
  1565. mirror_info_t *newmirrors;
  1566. conf_t *conf = mddev_to_conf(mddev);
  1567. int cnt;
  1568. int d, d2;
  1569. if (raid_disks < conf->raid_disks) {
  1570. cnt=0;
  1571. for (d= 0; d < conf->raid_disks; d++)
  1572. if (conf->mirrors[d].rdev)
  1573. cnt++;
  1574. if (cnt > raid_disks)
  1575. return -EBUSY;
  1576. }
  1577. newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
  1578. if (!newpoolinfo)
  1579. return -ENOMEM;
  1580. newpoolinfo->mddev = mddev;
  1581. newpoolinfo->raid_disks = raid_disks;
  1582. newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  1583. r1bio_pool_free, newpoolinfo);
  1584. if (!newpool) {
  1585. kfree(newpoolinfo);
  1586. return -ENOMEM;
  1587. }
  1588. newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
  1589. if (!newmirrors) {
  1590. kfree(newpoolinfo);
  1591. mempool_destroy(newpool);
  1592. return -ENOMEM;
  1593. }
  1594. memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
  1595. raise_barrier(conf);
  1596. /* ok, everything is stopped */
  1597. oldpool = conf->r1bio_pool;
  1598. conf->r1bio_pool = newpool;
  1599. for (d=d2=0; d < conf->raid_disks; d++)
  1600. if (conf->mirrors[d].rdev) {
  1601. conf->mirrors[d].rdev->raid_disk = d2;
  1602. newmirrors[d2++].rdev = conf->mirrors[d].rdev;
  1603. }
  1604. kfree(conf->mirrors);
  1605. conf->mirrors = newmirrors;
  1606. kfree(conf->poolinfo);
  1607. conf->poolinfo = newpoolinfo;
  1608. mddev->degraded += (raid_disks - conf->raid_disks);
  1609. conf->raid_disks = mddev->raid_disks = raid_disks;
  1610. conf->last_used = 0; /* just make sure it is in-range */
  1611. lower_barrier(conf);
  1612. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1613. md_wakeup_thread(mddev->thread);
  1614. mempool_destroy(oldpool);
  1615. return 0;
  1616. }
  1617. static void raid1_quiesce(mddev_t *mddev, int state)
  1618. {
  1619. conf_t *conf = mddev_to_conf(mddev);
  1620. switch(state) {
  1621. case 1:
  1622. raise_barrier(conf);
  1623. break;
  1624. case 0:
  1625. lower_barrier(conf);
  1626. break;
  1627. }
  1628. }
  1629. static mdk_personality_t raid1_personality =
  1630. {
  1631. .name = "raid1",
  1632. .owner = THIS_MODULE,
  1633. .make_request = make_request,
  1634. .run = run,
  1635. .stop = stop,
  1636. .status = status,
  1637. .error_handler = error,
  1638. .hot_add_disk = raid1_add_disk,
  1639. .hot_remove_disk= raid1_remove_disk,
  1640. .spare_active = raid1_spare_active,
  1641. .sync_request = sync_request,
  1642. .resize = raid1_resize,
  1643. .reshape = raid1_reshape,
  1644. .quiesce = raid1_quiesce,
  1645. };
  1646. static int __init raid_init(void)
  1647. {
  1648. return register_md_personality(RAID1, &raid1_personality);
  1649. }
  1650. static void raid_exit(void)
  1651. {
  1652. unregister_md_personality(RAID1);
  1653. }
  1654. module_init(raid_init);
  1655. module_exit(raid_exit);
  1656. MODULE_LICENSE("GPL");
  1657. MODULE_ALIAS("md-personality-3"); /* RAID1 */