raid1.c 39 KB

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