raid1.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2, or (at your option)
  18. * any later version.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * (for example /usr/src/linux/COPYING); if not, write to the Free
  22. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/raid/raid1.h>
  25. /*
  26. * Number of guaranteed r1bios in case of extreme VM load:
  27. */
  28. #define NR_RAID1_BIOS 256
  29. static mdk_personality_t raid1_personality;
  30. static void unplug_slaves(mddev_t *mddev);
  31. static void * r1bio_pool_alloc(unsigned int __nocast gfp_flags, void *data)
  32. {
  33. struct pool_info *pi = data;
  34. r1bio_t *r1_bio;
  35. int size = offsetof(r1bio_t, bios[pi->raid_disks]);
  36. /* allocate a r1bio with room for raid_disks entries in the bios array */
  37. r1_bio = kmalloc(size, gfp_flags);
  38. if (r1_bio)
  39. memset(r1_bio, 0, size);
  40. else
  41. unplug_slaves(pi->mddev);
  42. return r1_bio;
  43. }
  44. static void r1bio_pool_free(void *r1_bio, void *data)
  45. {
  46. kfree(r1_bio);
  47. }
  48. #define RESYNC_BLOCK_SIZE (64*1024)
  49. //#define RESYNC_BLOCK_SIZE PAGE_SIZE
  50. #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
  51. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  52. #define RESYNC_WINDOW (2048*1024)
  53. static void * r1buf_pool_alloc(unsigned int __nocast gfp_flags, void *data)
  54. {
  55. struct pool_info *pi = data;
  56. struct page *page;
  57. r1bio_t *r1_bio;
  58. struct bio *bio;
  59. int i, j;
  60. r1_bio = r1bio_pool_alloc(gfp_flags, pi);
  61. if (!r1_bio) {
  62. unplug_slaves(pi->mddev);
  63. return NULL;
  64. }
  65. /*
  66. * Allocate bios : 1 for reading, n-1 for writing
  67. */
  68. for (j = pi->raid_disks ; j-- ; ) {
  69. bio = bio_alloc(gfp_flags, RESYNC_PAGES);
  70. if (!bio)
  71. goto out_free_bio;
  72. r1_bio->bios[j] = bio;
  73. }
  74. /*
  75. * Allocate RESYNC_PAGES data pages and attach them to
  76. * the first bio;
  77. */
  78. bio = r1_bio->bios[0];
  79. for (i = 0; i < RESYNC_PAGES; i++) {
  80. page = alloc_page(gfp_flags);
  81. if (unlikely(!page))
  82. goto out_free_pages;
  83. bio->bi_io_vec[i].bv_page = page;
  84. }
  85. r1_bio->master_bio = NULL;
  86. return r1_bio;
  87. out_free_pages:
  88. for ( ; i > 0 ; i--)
  89. __free_page(bio->bi_io_vec[i-1].bv_page);
  90. out_free_bio:
  91. while ( ++j < pi->raid_disks )
  92. bio_put(r1_bio->bios[j]);
  93. r1bio_pool_free(r1_bio, data);
  94. return NULL;
  95. }
  96. static void r1buf_pool_free(void *__r1_bio, void *data)
  97. {
  98. struct pool_info *pi = data;
  99. int i;
  100. r1bio_t *r1bio = __r1_bio;
  101. struct bio *bio = r1bio->bios[0];
  102. for (i = 0; i < RESYNC_PAGES; i++) {
  103. __free_page(bio->bi_io_vec[i].bv_page);
  104. bio->bi_io_vec[i].bv_page = NULL;
  105. }
  106. for (i=0 ; i < pi->raid_disks; i++)
  107. bio_put(r1bio->bios[i]);
  108. r1bio_pool_free(r1bio, data);
  109. }
  110. static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
  111. {
  112. int i;
  113. for (i = 0; i < conf->raid_disks; i++) {
  114. struct bio **bio = r1_bio->bios + i;
  115. if (*bio)
  116. bio_put(*bio);
  117. *bio = NULL;
  118. }
  119. }
  120. static inline void free_r1bio(r1bio_t *r1_bio)
  121. {
  122. unsigned long flags;
  123. conf_t *conf = mddev_to_conf(r1_bio->mddev);
  124. /*
  125. * Wake up any possible resync thread that waits for the device
  126. * to go idle.
  127. */
  128. spin_lock_irqsave(&conf->resync_lock, flags);
  129. if (!--conf->nr_pending) {
  130. wake_up(&conf->wait_idle);
  131. wake_up(&conf->wait_resume);
  132. }
  133. spin_unlock_irqrestore(&conf->resync_lock, flags);
  134. put_all_bios(conf, r1_bio);
  135. mempool_free(r1_bio, conf->r1bio_pool);
  136. }
  137. static inline void put_buf(r1bio_t *r1_bio)
  138. {
  139. conf_t *conf = mddev_to_conf(r1_bio->mddev);
  140. unsigned long flags;
  141. mempool_free(r1_bio, conf->r1buf_pool);
  142. spin_lock_irqsave(&conf->resync_lock, flags);
  143. if (!conf->barrier)
  144. BUG();
  145. --conf->barrier;
  146. wake_up(&conf->wait_resume);
  147. wake_up(&conf->wait_idle);
  148. if (!--conf->nr_pending) {
  149. wake_up(&conf->wait_idle);
  150. wake_up(&conf->wait_resume);
  151. }
  152. spin_unlock_irqrestore(&conf->resync_lock, flags);
  153. }
  154. static void reschedule_retry(r1bio_t *r1_bio)
  155. {
  156. unsigned long flags;
  157. mddev_t *mddev = r1_bio->mddev;
  158. conf_t *conf = mddev_to_conf(mddev);
  159. spin_lock_irqsave(&conf->device_lock, flags);
  160. list_add(&r1_bio->retry_list, &conf->retry_list);
  161. spin_unlock_irqrestore(&conf->device_lock, flags);
  162. md_wakeup_thread(mddev->thread);
  163. }
  164. /*
  165. * raid_end_bio_io() is called when we have finished servicing a mirrored
  166. * operation and are ready to return a success/failure code to the buffer
  167. * cache layer.
  168. */
  169. static void raid_end_bio_io(r1bio_t *r1_bio)
  170. {
  171. struct bio *bio = r1_bio->master_bio;
  172. bio_endio(bio, bio->bi_size,
  173. test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
  174. free_r1bio(r1_bio);
  175. }
  176. /*
  177. * Update disk head position estimator based on IRQ completion info.
  178. */
  179. static inline void update_head_pos(int disk, r1bio_t *r1_bio)
  180. {
  181. conf_t *conf = mddev_to_conf(r1_bio->mddev);
  182. conf->mirrors[disk].head_position =
  183. r1_bio->sector + (r1_bio->sectors);
  184. }
  185. static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
  186. {
  187. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  188. r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
  189. int mirror;
  190. conf_t *conf = mddev_to_conf(r1_bio->mddev);
  191. if (bio->bi_size)
  192. return 1;
  193. mirror = r1_bio->read_disk;
  194. /*
  195. * this branch is our 'one mirror IO has finished' event handler:
  196. */
  197. if (!uptodate)
  198. md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
  199. else
  200. /*
  201. * Set R1BIO_Uptodate in our master bio, so that
  202. * we will return a good error code for to the higher
  203. * levels even if IO on some other mirrored buffer fails.
  204. *
  205. * The 'master' represents the composite IO operation to
  206. * user-side. So if something waits for IO, then it will
  207. * wait for the 'master' bio.
  208. */
  209. set_bit(R1BIO_Uptodate, &r1_bio->state);
  210. update_head_pos(mirror, r1_bio);
  211. /*
  212. * we have only one bio on the read side
  213. */
  214. if (uptodate)
  215. raid_end_bio_io(r1_bio);
  216. else {
  217. /*
  218. * oops, read error:
  219. */
  220. char b[BDEVNAME_SIZE];
  221. if (printk_ratelimit())
  222. printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
  223. bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
  224. reschedule_retry(r1_bio);
  225. }
  226. rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
  227. return 0;
  228. }
  229. static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
  230. {
  231. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  232. r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
  233. int mirror;
  234. conf_t *conf = mddev_to_conf(r1_bio->mddev);
  235. if (bio->bi_size)
  236. return 1;
  237. for (mirror = 0; mirror < conf->raid_disks; mirror++)
  238. if (r1_bio->bios[mirror] == bio)
  239. break;
  240. /*
  241. * this branch is our 'one mirror IO has finished' event handler:
  242. */
  243. if (!uptodate)
  244. md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
  245. else
  246. /*
  247. * Set R1BIO_Uptodate in our master bio, so that
  248. * we will return a good error code for to the higher
  249. * levels even if IO on some other mirrored buffer fails.
  250. *
  251. * The 'master' represents the composite IO operation to
  252. * user-side. So if something waits for IO, then it will
  253. * wait for the 'master' bio.
  254. */
  255. set_bit(R1BIO_Uptodate, &r1_bio->state);
  256. update_head_pos(mirror, r1_bio);
  257. /*
  258. *
  259. * Let's see if all mirrored write operations have finished
  260. * already.
  261. */
  262. if (atomic_dec_and_test(&r1_bio->remaining)) {
  263. md_write_end(r1_bio->mddev);
  264. raid_end_bio_io(r1_bio);
  265. }
  266. rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
  267. return 0;
  268. }
  269. /*
  270. * This routine returns the disk from which the requested read should
  271. * be done. There is a per-array 'next expected sequential IO' sector
  272. * number - if this matches on the next IO then we use the last disk.
  273. * There is also a per-disk 'last know head position' sector that is
  274. * maintained from IRQ contexts, both the normal and the resync IO
  275. * completion handlers update this position correctly. If there is no
  276. * perfect sequential match then we pick the disk whose head is closest.
  277. *
  278. * If there are 2 mirrors in the same 2 devices, performance degrades
  279. * because position is mirror, not device based.
  280. *
  281. * The rdev for the device selected will have nr_pending incremented.
  282. */
  283. static int read_balance(conf_t *conf, r1bio_t *r1_bio)
  284. {
  285. const unsigned long this_sector = r1_bio->sector;
  286. int new_disk = conf->last_used, disk = new_disk;
  287. const int sectors = r1_bio->sectors;
  288. sector_t new_distance, current_distance;
  289. mdk_rdev_t *new_rdev, *rdev;
  290. rcu_read_lock();
  291. /*
  292. * Check if it if we can balance. We can balance on the whole
  293. * device if no resync is going on, or below the resync window.
  294. * We take the first readable disk when above the resync window.
  295. */
  296. retry:
  297. if (conf->mddev->recovery_cp < MaxSector &&
  298. (this_sector + sectors >= conf->next_resync)) {
  299. /* Choose the first operation device, for consistancy */
  300. new_disk = 0;
  301. while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
  302. !new_rdev->in_sync) {
  303. new_disk++;
  304. if (new_disk == conf->raid_disks) {
  305. new_disk = -1;
  306. break;
  307. }
  308. }
  309. goto rb_out;
  310. }
  311. /* make sure the disk is operational */
  312. while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
  313. !new_rdev->in_sync) {
  314. if (new_disk <= 0)
  315. new_disk = conf->raid_disks;
  316. new_disk--;
  317. if (new_disk == disk) {
  318. new_disk = -1;
  319. goto rb_out;
  320. }
  321. }
  322. disk = new_disk;
  323. /* now disk == new_disk == starting point for search */
  324. /*
  325. * Don't change to another disk for sequential reads:
  326. */
  327. if (conf->next_seq_sect == this_sector)
  328. goto rb_out;
  329. if (this_sector == conf->mirrors[new_disk].head_position)
  330. goto rb_out;
  331. current_distance = abs(this_sector - conf->mirrors[disk].head_position);
  332. /* Find the disk whose head is closest */
  333. do {
  334. if (disk <= 0)
  335. disk = conf->raid_disks;
  336. disk--;
  337. if ((rdev=conf->mirrors[disk].rdev) == NULL ||
  338. !rdev->in_sync)
  339. continue;
  340. if (!atomic_read(&rdev->nr_pending)) {
  341. new_disk = disk;
  342. new_rdev = rdev;
  343. break;
  344. }
  345. new_distance = abs(this_sector - conf->mirrors[disk].head_position);
  346. if (new_distance < current_distance) {
  347. current_distance = new_distance;
  348. new_disk = disk;
  349. new_rdev = rdev;
  350. }
  351. } while (disk != conf->last_used);
  352. rb_out:
  353. if (new_disk >= 0) {
  354. conf->next_seq_sect = this_sector + sectors;
  355. conf->last_used = new_disk;
  356. atomic_inc(&new_rdev->nr_pending);
  357. if (!new_rdev->in_sync) {
  358. /* cannot risk returning a device that failed
  359. * before we inc'ed nr_pending
  360. */
  361. atomic_dec(&new_rdev->nr_pending);
  362. goto retry;
  363. }
  364. }
  365. rcu_read_unlock();
  366. return new_disk;
  367. }
  368. static void unplug_slaves(mddev_t *mddev)
  369. {
  370. conf_t *conf = mddev_to_conf(mddev);
  371. int i;
  372. rcu_read_lock();
  373. for (i=0; i<mddev->raid_disks; i++) {
  374. mdk_rdev_t *rdev = conf->mirrors[i].rdev;
  375. if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
  376. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  377. atomic_inc(&rdev->nr_pending);
  378. rcu_read_unlock();
  379. if (r_queue->unplug_fn)
  380. r_queue->unplug_fn(r_queue);
  381. rdev_dec_pending(rdev, mddev);
  382. rcu_read_lock();
  383. }
  384. }
  385. rcu_read_unlock();
  386. }
  387. static void raid1_unplug(request_queue_t *q)
  388. {
  389. unplug_slaves(q->queuedata);
  390. }
  391. static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
  392. sector_t *error_sector)
  393. {
  394. mddev_t *mddev = q->queuedata;
  395. conf_t *conf = mddev_to_conf(mddev);
  396. int i, ret = 0;
  397. rcu_read_lock();
  398. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  399. mdk_rdev_t *rdev = conf->mirrors[i].rdev;
  400. if (rdev && !rdev->faulty) {
  401. struct block_device *bdev = rdev->bdev;
  402. request_queue_t *r_queue = bdev_get_queue(bdev);
  403. if (!r_queue->issue_flush_fn)
  404. ret = -EOPNOTSUPP;
  405. else {
  406. atomic_inc(&rdev->nr_pending);
  407. rcu_read_unlock();
  408. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  409. error_sector);
  410. rdev_dec_pending(rdev, mddev);
  411. rcu_read_lock();
  412. }
  413. }
  414. }
  415. rcu_read_unlock();
  416. return ret;
  417. }
  418. /*
  419. * Throttle resync depth, so that we can both get proper overlapping of
  420. * requests, but are still able to handle normal requests quickly.
  421. */
  422. #define RESYNC_DEPTH 32
  423. static void device_barrier(conf_t *conf, sector_t sect)
  424. {
  425. spin_lock_irq(&conf->resync_lock);
  426. wait_event_lock_irq(conf->wait_idle, !waitqueue_active(&conf->wait_resume),
  427. conf->resync_lock, unplug_slaves(conf->mddev));
  428. if (!conf->barrier++) {
  429. wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
  430. conf->resync_lock, unplug_slaves(conf->mddev));
  431. if (conf->nr_pending)
  432. BUG();
  433. }
  434. wait_event_lock_irq(conf->wait_resume, conf->barrier < RESYNC_DEPTH,
  435. conf->resync_lock, unplug_slaves(conf->mddev));
  436. conf->next_resync = sect;
  437. spin_unlock_irq(&conf->resync_lock);
  438. }
  439. static int make_request(request_queue_t *q, struct bio * bio)
  440. {
  441. mddev_t *mddev = q->queuedata;
  442. conf_t *conf = mddev_to_conf(mddev);
  443. mirror_info_t *mirror;
  444. r1bio_t *r1_bio;
  445. struct bio *read_bio;
  446. int i, disks;
  447. mdk_rdev_t *rdev;
  448. /*
  449. * Register the new request and wait if the reconstruction
  450. * thread has put up a bar for new requests.
  451. * Continue immediately if no resync is active currently.
  452. */
  453. spin_lock_irq(&conf->resync_lock);
  454. wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
  455. conf->nr_pending++;
  456. spin_unlock_irq(&conf->resync_lock);
  457. if (bio_data_dir(bio)==WRITE) {
  458. disk_stat_inc(mddev->gendisk, writes);
  459. disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
  460. } else {
  461. disk_stat_inc(mddev->gendisk, reads);
  462. disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
  463. }
  464. /*
  465. * make_request() can abort the operation when READA is being
  466. * used and no empty request is available.
  467. *
  468. */
  469. r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
  470. r1_bio->master_bio = bio;
  471. r1_bio->sectors = bio->bi_size >> 9;
  472. r1_bio->mddev = mddev;
  473. r1_bio->sector = bio->bi_sector;
  474. r1_bio->state = 0;
  475. if (bio_data_dir(bio) == READ) {
  476. /*
  477. * read balancing logic:
  478. */
  479. int rdisk = read_balance(conf, r1_bio);
  480. if (rdisk < 0) {
  481. /* couldn't find anywhere to read from */
  482. raid_end_bio_io(r1_bio);
  483. return 0;
  484. }
  485. mirror = conf->mirrors + rdisk;
  486. r1_bio->read_disk = rdisk;
  487. read_bio = bio_clone(bio, GFP_NOIO);
  488. r1_bio->bios[rdisk] = read_bio;
  489. read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
  490. read_bio->bi_bdev = mirror->rdev->bdev;
  491. read_bio->bi_end_io = raid1_end_read_request;
  492. read_bio->bi_rw = READ;
  493. read_bio->bi_private = r1_bio;
  494. generic_make_request(read_bio);
  495. return 0;
  496. }
  497. /*
  498. * WRITE:
  499. */
  500. /* first select target devices under spinlock and
  501. * inc refcount on their rdev. Record them by setting
  502. * bios[x] to bio
  503. */
  504. disks = conf->raid_disks;
  505. rcu_read_lock();
  506. for (i = 0; i < disks; i++) {
  507. if ((rdev=conf->mirrors[i].rdev) != NULL &&
  508. !rdev->faulty) {
  509. atomic_inc(&rdev->nr_pending);
  510. if (rdev->faulty) {
  511. atomic_dec(&rdev->nr_pending);
  512. r1_bio->bios[i] = NULL;
  513. } else
  514. r1_bio->bios[i] = bio;
  515. } else
  516. r1_bio->bios[i] = NULL;
  517. }
  518. rcu_read_unlock();
  519. atomic_set(&r1_bio->remaining, 1);
  520. md_write_start(mddev);
  521. for (i = 0; i < disks; i++) {
  522. struct bio *mbio;
  523. if (!r1_bio->bios[i])
  524. continue;
  525. mbio = bio_clone(bio, GFP_NOIO);
  526. r1_bio->bios[i] = mbio;
  527. mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
  528. mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
  529. mbio->bi_end_io = raid1_end_write_request;
  530. mbio->bi_rw = WRITE;
  531. mbio->bi_private = r1_bio;
  532. atomic_inc(&r1_bio->remaining);
  533. generic_make_request(mbio);
  534. }
  535. if (atomic_dec_and_test(&r1_bio->remaining)) {
  536. md_write_end(mddev);
  537. raid_end_bio_io(r1_bio);
  538. }
  539. return 0;
  540. }
  541. static void status(struct seq_file *seq, mddev_t *mddev)
  542. {
  543. conf_t *conf = mddev_to_conf(mddev);
  544. int i;
  545. seq_printf(seq, " [%d/%d] [", conf->raid_disks,
  546. conf->working_disks);
  547. for (i = 0; i < conf->raid_disks; i++)
  548. seq_printf(seq, "%s",
  549. conf->mirrors[i].rdev &&
  550. conf->mirrors[i].rdev->in_sync ? "U" : "_");
  551. seq_printf(seq, "]");
  552. }
  553. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  554. {
  555. char b[BDEVNAME_SIZE];
  556. conf_t *conf = mddev_to_conf(mddev);
  557. /*
  558. * If it is not operational, then we have already marked it as dead
  559. * else if it is the last working disks, ignore the error, let the
  560. * next level up know.
  561. * else mark the drive as failed
  562. */
  563. if (rdev->in_sync
  564. && conf->working_disks == 1)
  565. /*
  566. * Don't fail the drive, act as though we were just a
  567. * normal single drive
  568. */
  569. return;
  570. if (rdev->in_sync) {
  571. mddev->degraded++;
  572. conf->working_disks--;
  573. /*
  574. * if recovery is running, make sure it aborts.
  575. */
  576. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  577. }
  578. rdev->in_sync = 0;
  579. rdev->faulty = 1;
  580. mddev->sb_dirty = 1;
  581. printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
  582. " Operation continuing on %d devices\n",
  583. bdevname(rdev->bdev,b), conf->working_disks);
  584. }
  585. static void print_conf(conf_t *conf)
  586. {
  587. int i;
  588. mirror_info_t *tmp;
  589. printk("RAID1 conf printout:\n");
  590. if (!conf) {
  591. printk("(!conf)\n");
  592. return;
  593. }
  594. printk(" --- wd:%d rd:%d\n", conf->working_disks,
  595. conf->raid_disks);
  596. for (i = 0; i < conf->raid_disks; i++) {
  597. char b[BDEVNAME_SIZE];
  598. tmp = conf->mirrors + i;
  599. if (tmp->rdev)
  600. printk(" disk %d, wo:%d, o:%d, dev:%s\n",
  601. i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
  602. bdevname(tmp->rdev->bdev,b));
  603. }
  604. }
  605. static void close_sync(conf_t *conf)
  606. {
  607. spin_lock_irq(&conf->resync_lock);
  608. wait_event_lock_irq(conf->wait_resume, !conf->barrier,
  609. conf->resync_lock, unplug_slaves(conf->mddev));
  610. spin_unlock_irq(&conf->resync_lock);
  611. if (conf->barrier) BUG();
  612. if (waitqueue_active(&conf->wait_idle)) BUG();
  613. mempool_destroy(conf->r1buf_pool);
  614. conf->r1buf_pool = NULL;
  615. }
  616. static int raid1_spare_active(mddev_t *mddev)
  617. {
  618. int i;
  619. conf_t *conf = mddev->private;
  620. mirror_info_t *tmp;
  621. /*
  622. * Find all failed disks within the RAID1 configuration
  623. * and mark them readable
  624. */
  625. for (i = 0; i < conf->raid_disks; i++) {
  626. tmp = conf->mirrors + i;
  627. if (tmp->rdev
  628. && !tmp->rdev->faulty
  629. && !tmp->rdev->in_sync) {
  630. conf->working_disks++;
  631. mddev->degraded--;
  632. tmp->rdev->in_sync = 1;
  633. }
  634. }
  635. print_conf(conf);
  636. return 0;
  637. }
  638. static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  639. {
  640. conf_t *conf = mddev->private;
  641. int found = 0;
  642. int mirror;
  643. mirror_info_t *p;
  644. for (mirror=0; mirror < mddev->raid_disks; mirror++)
  645. if ( !(p=conf->mirrors+mirror)->rdev) {
  646. blk_queue_stack_limits(mddev->queue,
  647. rdev->bdev->bd_disk->queue);
  648. /* as we don't honour merge_bvec_fn, we must never risk
  649. * violating it, so limit ->max_sector to one PAGE, as
  650. * a one page request is never in violation.
  651. */
  652. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  653. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  654. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  655. p->head_position = 0;
  656. rdev->raid_disk = mirror;
  657. found = 1;
  658. p->rdev = rdev;
  659. break;
  660. }
  661. print_conf(conf);
  662. return found;
  663. }
  664. static int raid1_remove_disk(mddev_t *mddev, int number)
  665. {
  666. conf_t *conf = mddev->private;
  667. int err = 0;
  668. mdk_rdev_t *rdev;
  669. mirror_info_t *p = conf->mirrors+ number;
  670. print_conf(conf);
  671. rdev = p->rdev;
  672. if (rdev) {
  673. if (rdev->in_sync ||
  674. atomic_read(&rdev->nr_pending)) {
  675. err = -EBUSY;
  676. goto abort;
  677. }
  678. p->rdev = NULL;
  679. synchronize_rcu();
  680. if (atomic_read(&rdev->nr_pending)) {
  681. /* lost the race, try later */
  682. err = -EBUSY;
  683. p->rdev = rdev;
  684. }
  685. }
  686. abort:
  687. print_conf(conf);
  688. return err;
  689. }
  690. static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
  691. {
  692. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  693. r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
  694. conf_t *conf = mddev_to_conf(r1_bio->mddev);
  695. if (bio->bi_size)
  696. return 1;
  697. if (r1_bio->bios[r1_bio->read_disk] != bio)
  698. BUG();
  699. update_head_pos(r1_bio->read_disk, r1_bio);
  700. /*
  701. * we have read a block, now it needs to be re-written,
  702. * or re-read if the read failed.
  703. * We don't do much here, just schedule handling by raid1d
  704. */
  705. if (!uptodate)
  706. md_error(r1_bio->mddev,
  707. conf->mirrors[r1_bio->read_disk].rdev);
  708. else
  709. set_bit(R1BIO_Uptodate, &r1_bio->state);
  710. rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
  711. reschedule_retry(r1_bio);
  712. return 0;
  713. }
  714. static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
  715. {
  716. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  717. r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
  718. mddev_t *mddev = r1_bio->mddev;
  719. conf_t *conf = mddev_to_conf(mddev);
  720. int i;
  721. int mirror=0;
  722. if (bio->bi_size)
  723. return 1;
  724. for (i = 0; i < conf->raid_disks; i++)
  725. if (r1_bio->bios[i] == bio) {
  726. mirror = i;
  727. break;
  728. }
  729. if (!uptodate)
  730. md_error(mddev, conf->mirrors[mirror].rdev);
  731. update_head_pos(mirror, r1_bio);
  732. if (atomic_dec_and_test(&r1_bio->remaining)) {
  733. md_done_sync(mddev, r1_bio->sectors, uptodate);
  734. put_buf(r1_bio);
  735. }
  736. rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
  737. return 0;
  738. }
  739. static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
  740. {
  741. conf_t *conf = mddev_to_conf(mddev);
  742. int i;
  743. int disks = conf->raid_disks;
  744. struct bio *bio, *wbio;
  745. bio = r1_bio->bios[r1_bio->read_disk];
  746. /*
  747. * schedule writes
  748. */
  749. if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
  750. /*
  751. * There is no point trying a read-for-reconstruct as
  752. * reconstruct is about to be aborted
  753. */
  754. char b[BDEVNAME_SIZE];
  755. printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
  756. " for block %llu\n",
  757. bdevname(bio->bi_bdev,b),
  758. (unsigned long long)r1_bio->sector);
  759. md_done_sync(mddev, r1_bio->sectors, 0);
  760. put_buf(r1_bio);
  761. return;
  762. }
  763. atomic_set(&r1_bio->remaining, 1);
  764. for (i = 0; i < disks ; i++) {
  765. wbio = r1_bio->bios[i];
  766. if (wbio->bi_end_io != end_sync_write)
  767. continue;
  768. atomic_inc(&conf->mirrors[i].rdev->nr_pending);
  769. atomic_inc(&r1_bio->remaining);
  770. md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
  771. generic_make_request(wbio);
  772. }
  773. if (atomic_dec_and_test(&r1_bio->remaining)) {
  774. md_done_sync(mddev, r1_bio->sectors, 1);
  775. put_buf(r1_bio);
  776. }
  777. }
  778. /*
  779. * This is a kernel thread which:
  780. *
  781. * 1. Retries failed read operations on working mirrors.
  782. * 2. Updates the raid superblock when problems encounter.
  783. * 3. Performs writes following reads for array syncronising.
  784. */
  785. static void raid1d(mddev_t *mddev)
  786. {
  787. r1bio_t *r1_bio;
  788. struct bio *bio;
  789. unsigned long flags;
  790. conf_t *conf = mddev_to_conf(mddev);
  791. struct list_head *head = &conf->retry_list;
  792. int unplug=0;
  793. mdk_rdev_t *rdev;
  794. md_check_recovery(mddev);
  795. md_handle_safemode(mddev);
  796. for (;;) {
  797. char b[BDEVNAME_SIZE];
  798. spin_lock_irqsave(&conf->device_lock, flags);
  799. if (list_empty(head))
  800. break;
  801. r1_bio = list_entry(head->prev, r1bio_t, retry_list);
  802. list_del(head->prev);
  803. spin_unlock_irqrestore(&conf->device_lock, flags);
  804. mddev = r1_bio->mddev;
  805. conf = mddev_to_conf(mddev);
  806. if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
  807. sync_request_write(mddev, r1_bio);
  808. unplug = 1;
  809. } else {
  810. int disk;
  811. bio = r1_bio->bios[r1_bio->read_disk];
  812. if ((disk=read_balance(conf, r1_bio)) == -1) {
  813. printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
  814. " read error for block %llu\n",
  815. bdevname(bio->bi_bdev,b),
  816. (unsigned long long)r1_bio->sector);
  817. raid_end_bio_io(r1_bio);
  818. } else {
  819. r1_bio->bios[r1_bio->read_disk] = NULL;
  820. r1_bio->read_disk = disk;
  821. bio_put(bio);
  822. bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
  823. r1_bio->bios[r1_bio->read_disk] = bio;
  824. rdev = conf->mirrors[disk].rdev;
  825. if (printk_ratelimit())
  826. printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
  827. " another mirror\n",
  828. bdevname(rdev->bdev,b),
  829. (unsigned long long)r1_bio->sector);
  830. bio->bi_sector = r1_bio->sector + rdev->data_offset;
  831. bio->bi_bdev = rdev->bdev;
  832. bio->bi_end_io = raid1_end_read_request;
  833. bio->bi_rw = READ;
  834. bio->bi_private = r1_bio;
  835. unplug = 1;
  836. generic_make_request(bio);
  837. }
  838. }
  839. }
  840. spin_unlock_irqrestore(&conf->device_lock, flags);
  841. if (unplug)
  842. unplug_slaves(mddev);
  843. }
  844. static int init_resync(conf_t *conf)
  845. {
  846. int buffs;
  847. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  848. if (conf->r1buf_pool)
  849. BUG();
  850. conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
  851. conf->poolinfo);
  852. if (!conf->r1buf_pool)
  853. return -ENOMEM;
  854. conf->next_resync = 0;
  855. return 0;
  856. }
  857. /*
  858. * perform a "sync" on one "block"
  859. *
  860. * We need to make sure that no normal I/O request - particularly write
  861. * requests - conflict with active sync requests.
  862. *
  863. * This is achieved by tracking pending requests and a 'barrier' concept
  864. * that can be installed to exclude normal IO requests.
  865. */
  866. static int sync_request(mddev_t *mddev, sector_t sector_nr, int go_faster)
  867. {
  868. conf_t *conf = mddev_to_conf(mddev);
  869. mirror_info_t *mirror;
  870. r1bio_t *r1_bio;
  871. struct bio *bio;
  872. sector_t max_sector, nr_sectors;
  873. int disk;
  874. int i;
  875. int write_targets = 0;
  876. if (!conf->r1buf_pool)
  877. if (init_resync(conf))
  878. return -ENOMEM;
  879. max_sector = mddev->size << 1;
  880. if (sector_nr >= max_sector) {
  881. close_sync(conf);
  882. return 0;
  883. }
  884. /*
  885. * If there is non-resync activity waiting for us then
  886. * put in a delay to throttle resync.
  887. */
  888. if (!go_faster && waitqueue_active(&conf->wait_resume))
  889. msleep_interruptible(1000);
  890. device_barrier(conf, sector_nr + RESYNC_SECTORS);
  891. /*
  892. * If reconstructing, and >1 working disc,
  893. * could dedicate one to rebuild and others to
  894. * service read requests ..
  895. */
  896. disk = conf->last_used;
  897. /* make sure disk is operational */
  898. while (conf->mirrors[disk].rdev == NULL ||
  899. !conf->mirrors[disk].rdev->in_sync) {
  900. if (disk <= 0)
  901. disk = conf->raid_disks;
  902. disk--;
  903. if (disk == conf->last_used)
  904. break;
  905. }
  906. conf->last_used = disk;
  907. atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
  908. mirror = conf->mirrors + disk;
  909. r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
  910. spin_lock_irq(&conf->resync_lock);
  911. conf->nr_pending++;
  912. spin_unlock_irq(&conf->resync_lock);
  913. r1_bio->mddev = mddev;
  914. r1_bio->sector = sector_nr;
  915. set_bit(R1BIO_IsSync, &r1_bio->state);
  916. r1_bio->read_disk = disk;
  917. for (i=0; i < conf->raid_disks; i++) {
  918. bio = r1_bio->bios[i];
  919. /* take from bio_init */
  920. bio->bi_next = NULL;
  921. bio->bi_flags |= 1 << BIO_UPTODATE;
  922. bio->bi_rw = 0;
  923. bio->bi_vcnt = 0;
  924. bio->bi_idx = 0;
  925. bio->bi_phys_segments = 0;
  926. bio->bi_hw_segments = 0;
  927. bio->bi_size = 0;
  928. bio->bi_end_io = NULL;
  929. bio->bi_private = NULL;
  930. if (i == disk) {
  931. bio->bi_rw = READ;
  932. bio->bi_end_io = end_sync_read;
  933. } else if (conf->mirrors[i].rdev &&
  934. !conf->mirrors[i].rdev->faulty &&
  935. (!conf->mirrors[i].rdev->in_sync ||
  936. sector_nr + RESYNC_SECTORS > mddev->recovery_cp)) {
  937. bio->bi_rw = WRITE;
  938. bio->bi_end_io = end_sync_write;
  939. write_targets ++;
  940. } else
  941. continue;
  942. bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
  943. bio->bi_bdev = conf->mirrors[i].rdev->bdev;
  944. bio->bi_private = r1_bio;
  945. }
  946. if (write_targets == 0) {
  947. /* There is nowhere to write, so all non-sync
  948. * drives must be failed - so we are finished
  949. */
  950. int rv = max_sector - sector_nr;
  951. md_done_sync(mddev, rv, 1);
  952. put_buf(r1_bio);
  953. rdev_dec_pending(conf->mirrors[disk].rdev, mddev);
  954. return rv;
  955. }
  956. nr_sectors = 0;
  957. do {
  958. struct page *page;
  959. int len = PAGE_SIZE;
  960. if (sector_nr + (len>>9) > max_sector)
  961. len = (max_sector - sector_nr) << 9;
  962. if (len == 0)
  963. break;
  964. for (i=0 ; i < conf->raid_disks; i++) {
  965. bio = r1_bio->bios[i];
  966. if (bio->bi_end_io) {
  967. page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
  968. if (bio_add_page(bio, page, len, 0) == 0) {
  969. /* stop here */
  970. r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
  971. while (i > 0) {
  972. i--;
  973. bio = r1_bio->bios[i];
  974. if (bio->bi_end_io==NULL) continue;
  975. /* remove last page from this bio */
  976. bio->bi_vcnt--;
  977. bio->bi_size -= len;
  978. bio->bi_flags &= ~(1<< BIO_SEG_VALID);
  979. }
  980. goto bio_full;
  981. }
  982. }
  983. }
  984. nr_sectors += len>>9;
  985. sector_nr += len>>9;
  986. } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
  987. bio_full:
  988. bio = r1_bio->bios[disk];
  989. r1_bio->sectors = nr_sectors;
  990. md_sync_acct(mirror->rdev->bdev, nr_sectors);
  991. generic_make_request(bio);
  992. return nr_sectors;
  993. }
  994. static int run(mddev_t *mddev)
  995. {
  996. conf_t *conf;
  997. int i, j, disk_idx;
  998. mirror_info_t *disk;
  999. mdk_rdev_t *rdev;
  1000. struct list_head *tmp;
  1001. if (mddev->level != 1) {
  1002. printk("raid1: %s: raid level not set to mirroring (%d)\n",
  1003. mdname(mddev), mddev->level);
  1004. goto out;
  1005. }
  1006. /*
  1007. * copy the already verified devices into our private RAID1
  1008. * bookkeeping area. [whatever we allocate in run(),
  1009. * should be freed in stop()]
  1010. */
  1011. conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
  1012. mddev->private = conf;
  1013. if (!conf)
  1014. goto out_no_mem;
  1015. memset(conf, 0, sizeof(*conf));
  1016. conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
  1017. GFP_KERNEL);
  1018. if (!conf->mirrors)
  1019. goto out_no_mem;
  1020. memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
  1021. conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
  1022. if (!conf->poolinfo)
  1023. goto out_no_mem;
  1024. conf->poolinfo->mddev = mddev;
  1025. conf->poolinfo->raid_disks = mddev->raid_disks;
  1026. conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  1027. r1bio_pool_free,
  1028. conf->poolinfo);
  1029. if (!conf->r1bio_pool)
  1030. goto out_no_mem;
  1031. ITERATE_RDEV(mddev, rdev, tmp) {
  1032. disk_idx = rdev->raid_disk;
  1033. if (disk_idx >= mddev->raid_disks
  1034. || disk_idx < 0)
  1035. continue;
  1036. disk = conf->mirrors + disk_idx;
  1037. disk->rdev = rdev;
  1038. blk_queue_stack_limits(mddev->queue,
  1039. rdev->bdev->bd_disk->queue);
  1040. /* as we don't honour merge_bvec_fn, we must never risk
  1041. * violating it, so limit ->max_sector to one PAGE, as
  1042. * a one page request is never in violation.
  1043. */
  1044. if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
  1045. mddev->queue->max_sectors > (PAGE_SIZE>>9))
  1046. blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
  1047. disk->head_position = 0;
  1048. if (!rdev->faulty && rdev->in_sync)
  1049. conf->working_disks++;
  1050. }
  1051. conf->raid_disks = mddev->raid_disks;
  1052. conf->mddev = mddev;
  1053. spin_lock_init(&conf->device_lock);
  1054. INIT_LIST_HEAD(&conf->retry_list);
  1055. if (conf->working_disks == 1)
  1056. mddev->recovery_cp = MaxSector;
  1057. spin_lock_init(&conf->resync_lock);
  1058. init_waitqueue_head(&conf->wait_idle);
  1059. init_waitqueue_head(&conf->wait_resume);
  1060. if (!conf->working_disks) {
  1061. printk(KERN_ERR "raid1: no operational mirrors for %s\n",
  1062. mdname(mddev));
  1063. goto out_free_conf;
  1064. }
  1065. mddev->degraded = 0;
  1066. for (i = 0; i < conf->raid_disks; i++) {
  1067. disk = conf->mirrors + i;
  1068. if (!disk->rdev) {
  1069. disk->head_position = 0;
  1070. mddev->degraded++;
  1071. }
  1072. }
  1073. /*
  1074. * find the first working one and use it as a starting point
  1075. * to read balancing.
  1076. */
  1077. for (j = 0; j < conf->raid_disks &&
  1078. (!conf->mirrors[j].rdev ||
  1079. !conf->mirrors[j].rdev->in_sync) ; j++)
  1080. /* nothing */;
  1081. conf->last_used = j;
  1082. {
  1083. mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
  1084. if (!mddev->thread) {
  1085. printk(KERN_ERR
  1086. "raid1: couldn't allocate thread for %s\n",
  1087. mdname(mddev));
  1088. goto out_free_conf;
  1089. }
  1090. }
  1091. printk(KERN_INFO
  1092. "raid1: raid set %s active with %d out of %d mirrors\n",
  1093. mdname(mddev), mddev->raid_disks - mddev->degraded,
  1094. mddev->raid_disks);
  1095. /*
  1096. * Ok, everything is just fine now
  1097. */
  1098. mddev->array_size = mddev->size;
  1099. mddev->queue->unplug_fn = raid1_unplug;
  1100. mddev->queue->issue_flush_fn = raid1_issue_flush;
  1101. return 0;
  1102. out_no_mem:
  1103. printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
  1104. mdname(mddev));
  1105. out_free_conf:
  1106. if (conf) {
  1107. if (conf->r1bio_pool)
  1108. mempool_destroy(conf->r1bio_pool);
  1109. if (conf->mirrors)
  1110. kfree(conf->mirrors);
  1111. if (conf->poolinfo)
  1112. kfree(conf->poolinfo);
  1113. kfree(conf);
  1114. mddev->private = NULL;
  1115. }
  1116. out:
  1117. return -EIO;
  1118. }
  1119. static int stop(mddev_t *mddev)
  1120. {
  1121. conf_t *conf = mddev_to_conf(mddev);
  1122. md_unregister_thread(mddev->thread);
  1123. mddev->thread = NULL;
  1124. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  1125. if (conf->r1bio_pool)
  1126. mempool_destroy(conf->r1bio_pool);
  1127. if (conf->mirrors)
  1128. kfree(conf->mirrors);
  1129. if (conf->poolinfo)
  1130. kfree(conf->poolinfo);
  1131. kfree(conf);
  1132. mddev->private = NULL;
  1133. return 0;
  1134. }
  1135. static int raid1_resize(mddev_t *mddev, sector_t sectors)
  1136. {
  1137. /* no resync is happening, and there is enough space
  1138. * on all devices, so we can resize.
  1139. * We need to make sure resync covers any new space.
  1140. * If the array is shrinking we should possibly wait until
  1141. * any io in the removed space completes, but it hardly seems
  1142. * worth it.
  1143. */
  1144. mddev->array_size = sectors>>1;
  1145. set_capacity(mddev->gendisk, mddev->array_size << 1);
  1146. mddev->changed = 1;
  1147. if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
  1148. mddev->recovery_cp = mddev->size << 1;
  1149. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1150. }
  1151. mddev->size = mddev->array_size;
  1152. return 0;
  1153. }
  1154. static int raid1_reshape(mddev_t *mddev, int raid_disks)
  1155. {
  1156. /* We need to:
  1157. * 1/ resize the r1bio_pool
  1158. * 2/ resize conf->mirrors
  1159. *
  1160. * We allocate a new r1bio_pool if we can.
  1161. * Then raise a device barrier and wait until all IO stops.
  1162. * Then resize conf->mirrors and swap in the new r1bio pool.
  1163. */
  1164. mempool_t *newpool, *oldpool;
  1165. struct pool_info *newpoolinfo;
  1166. mirror_info_t *newmirrors;
  1167. conf_t *conf = mddev_to_conf(mddev);
  1168. int d;
  1169. for (d= raid_disks; d < conf->raid_disks; d++)
  1170. if (conf->mirrors[d].rdev)
  1171. return -EBUSY;
  1172. newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
  1173. if (!newpoolinfo)
  1174. return -ENOMEM;
  1175. newpoolinfo->mddev = mddev;
  1176. newpoolinfo->raid_disks = raid_disks;
  1177. newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  1178. r1bio_pool_free, newpoolinfo);
  1179. if (!newpool) {
  1180. kfree(newpoolinfo);
  1181. return -ENOMEM;
  1182. }
  1183. newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
  1184. if (!newmirrors) {
  1185. kfree(newpoolinfo);
  1186. mempool_destroy(newpool);
  1187. return -ENOMEM;
  1188. }
  1189. memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
  1190. spin_lock_irq(&conf->resync_lock);
  1191. conf->barrier++;
  1192. wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
  1193. conf->resync_lock, unplug_slaves(mddev));
  1194. spin_unlock_irq(&conf->resync_lock);
  1195. /* ok, everything is stopped */
  1196. oldpool = conf->r1bio_pool;
  1197. conf->r1bio_pool = newpool;
  1198. for (d=0; d < raid_disks && d < conf->raid_disks; d++)
  1199. newmirrors[d] = conf->mirrors[d];
  1200. kfree(conf->mirrors);
  1201. conf->mirrors = newmirrors;
  1202. kfree(conf->poolinfo);
  1203. conf->poolinfo = newpoolinfo;
  1204. mddev->degraded += (raid_disks - conf->raid_disks);
  1205. conf->raid_disks = mddev->raid_disks = raid_disks;
  1206. spin_lock_irq(&conf->resync_lock);
  1207. conf->barrier--;
  1208. spin_unlock_irq(&conf->resync_lock);
  1209. wake_up(&conf->wait_resume);
  1210. wake_up(&conf->wait_idle);
  1211. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1212. md_wakeup_thread(mddev->thread);
  1213. mempool_destroy(oldpool);
  1214. return 0;
  1215. }
  1216. static mdk_personality_t raid1_personality =
  1217. {
  1218. .name = "raid1",
  1219. .owner = THIS_MODULE,
  1220. .make_request = make_request,
  1221. .run = run,
  1222. .stop = stop,
  1223. .status = status,
  1224. .error_handler = error,
  1225. .hot_add_disk = raid1_add_disk,
  1226. .hot_remove_disk= raid1_remove_disk,
  1227. .spare_active = raid1_spare_active,
  1228. .sync_request = sync_request,
  1229. .resize = raid1_resize,
  1230. .reshape = raid1_reshape,
  1231. };
  1232. static int __init raid_init(void)
  1233. {
  1234. return register_md_personality(RAID1, &raid1_personality);
  1235. }
  1236. static void raid_exit(void)
  1237. {
  1238. unregister_md_personality(RAID1);
  1239. }
  1240. module_init(raid_init);
  1241. module_exit(raid_exit);
  1242. MODULE_LICENSE("GPL");
  1243. MODULE_ALIAS("md-personality-3"); /* RAID1 */