raid1.c 44 KB

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