raid1.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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. * At the same time, we "pack" the devices so that all the missing
  1165. * devices have the higher raid_disk numbers.
  1166. */
  1167. mempool_t *newpool, *oldpool;
  1168. struct pool_info *newpoolinfo;
  1169. mirror_info_t *newmirrors;
  1170. conf_t *conf = mddev_to_conf(mddev);
  1171. int cnt;
  1172. int d, d2;
  1173. if (raid_disks < conf->raid_disks) {
  1174. cnt=0;
  1175. for (d= 0; d < conf->raid_disks; d++)
  1176. if (conf->mirrors[d].rdev)
  1177. cnt++;
  1178. if (cnt > raid_disks)
  1179. return -EBUSY;
  1180. }
  1181. newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
  1182. if (!newpoolinfo)
  1183. return -ENOMEM;
  1184. newpoolinfo->mddev = mddev;
  1185. newpoolinfo->raid_disks = raid_disks;
  1186. newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
  1187. r1bio_pool_free, newpoolinfo);
  1188. if (!newpool) {
  1189. kfree(newpoolinfo);
  1190. return -ENOMEM;
  1191. }
  1192. newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
  1193. if (!newmirrors) {
  1194. kfree(newpoolinfo);
  1195. mempool_destroy(newpool);
  1196. return -ENOMEM;
  1197. }
  1198. memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
  1199. spin_lock_irq(&conf->resync_lock);
  1200. conf->barrier++;
  1201. wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
  1202. conf->resync_lock, unplug_slaves(mddev));
  1203. spin_unlock_irq(&conf->resync_lock);
  1204. /* ok, everything is stopped */
  1205. oldpool = conf->r1bio_pool;
  1206. conf->r1bio_pool = newpool;
  1207. for (d=d2=0; d < conf->raid_disks; d++)
  1208. if (conf->mirrors[d].rdev) {
  1209. conf->mirrors[d].rdev->raid_disk = d2;
  1210. newmirrors[d2++].rdev = conf->mirrors[d].rdev;
  1211. }
  1212. kfree(conf->mirrors);
  1213. conf->mirrors = newmirrors;
  1214. kfree(conf->poolinfo);
  1215. conf->poolinfo = newpoolinfo;
  1216. mddev->degraded += (raid_disks - conf->raid_disks);
  1217. conf->raid_disks = mddev->raid_disks = raid_disks;
  1218. conf->last_used = 0; /* just make sure it is in-range */
  1219. spin_lock_irq(&conf->resync_lock);
  1220. conf->barrier--;
  1221. spin_unlock_irq(&conf->resync_lock);
  1222. wake_up(&conf->wait_resume);
  1223. wake_up(&conf->wait_idle);
  1224. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1225. md_wakeup_thread(mddev->thread);
  1226. mempool_destroy(oldpool);
  1227. return 0;
  1228. }
  1229. static mdk_personality_t raid1_personality =
  1230. {
  1231. .name = "raid1",
  1232. .owner = THIS_MODULE,
  1233. .make_request = make_request,
  1234. .run = run,
  1235. .stop = stop,
  1236. .status = status,
  1237. .error_handler = error,
  1238. .hot_add_disk = raid1_add_disk,
  1239. .hot_remove_disk= raid1_remove_disk,
  1240. .spare_active = raid1_spare_active,
  1241. .sync_request = sync_request,
  1242. .resize = raid1_resize,
  1243. .reshape = raid1_reshape,
  1244. };
  1245. static int __init raid_init(void)
  1246. {
  1247. return register_md_personality(RAID1, &raid1_personality);
  1248. }
  1249. static void raid_exit(void)
  1250. {
  1251. unregister_md_personality(RAID1);
  1252. }
  1253. module_init(raid_init);
  1254. module_exit(raid_exit);
  1255. MODULE_LICENSE("GPL");
  1256. MODULE_ALIAS("md-personality-3"); /* RAID1 */