raid1.c 39 KB

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