raid5.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  1. /*
  2. * raid5.c : Multiple Devices driver for Linux
  3. * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
  4. * Copyright (C) 1999, 2000 Ingo Molnar
  5. *
  6. * RAID-5 management functions.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * (for example /usr/src/linux/COPYING); if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/config.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/raid/raid5.h>
  21. #include <linux/highmem.h>
  22. #include <linux/bitops.h>
  23. #include <asm/atomic.h>
  24. /*
  25. * Stripe cache
  26. */
  27. #define NR_STRIPES 256
  28. #define STRIPE_SIZE PAGE_SIZE
  29. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  30. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  31. #define IO_THRESHOLD 1
  32. #define HASH_PAGES 1
  33. #define HASH_PAGES_ORDER 0
  34. #define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *))
  35. #define HASH_MASK (NR_HASH - 1)
  36. #define stripe_hash(conf, sect) ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])
  37. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  38. * order without overlap. There may be several bio's per stripe+device, and
  39. * a bio could span several devices.
  40. * When walking this list for a particular stripe+device, we must never proceed
  41. * beyond a bio that extends past this device, as the next bio might no longer
  42. * be valid.
  43. * This macro is used to determine the 'next' bio in the list, given the sector
  44. * of the current stripe+device
  45. */
  46. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  47. /*
  48. * The following can be used to debug the driver
  49. */
  50. #define RAID5_DEBUG 0
  51. #define RAID5_PARANOIA 1
  52. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  53. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  54. #else
  55. # define CHECK_DEVLOCK()
  56. #endif
  57. #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
  58. #if RAID5_DEBUG
  59. #define inline
  60. #define __inline__
  61. #endif
  62. static void print_raid5_conf (raid5_conf_t *conf);
  63. static inline void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  64. {
  65. if (atomic_dec_and_test(&sh->count)) {
  66. if (!list_empty(&sh->lru))
  67. BUG();
  68. if (atomic_read(&conf->active_stripes)==0)
  69. BUG();
  70. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  71. if (test_bit(STRIPE_DELAYED, &sh->state))
  72. list_add_tail(&sh->lru, &conf->delayed_list);
  73. else
  74. list_add_tail(&sh->lru, &conf->handle_list);
  75. md_wakeup_thread(conf->mddev->thread);
  76. } else {
  77. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  78. atomic_dec(&conf->preread_active_stripes);
  79. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  80. md_wakeup_thread(conf->mddev->thread);
  81. }
  82. list_add_tail(&sh->lru, &conf->inactive_list);
  83. atomic_dec(&conf->active_stripes);
  84. if (!conf->inactive_blocked ||
  85. atomic_read(&conf->active_stripes) < (NR_STRIPES*3/4))
  86. wake_up(&conf->wait_for_stripe);
  87. }
  88. }
  89. }
  90. static void release_stripe(struct stripe_head *sh)
  91. {
  92. raid5_conf_t *conf = sh->raid_conf;
  93. unsigned long flags;
  94. spin_lock_irqsave(&conf->device_lock, flags);
  95. __release_stripe(conf, sh);
  96. spin_unlock_irqrestore(&conf->device_lock, flags);
  97. }
  98. static void remove_hash(struct stripe_head *sh)
  99. {
  100. PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  101. if (sh->hash_pprev) {
  102. if (sh->hash_next)
  103. sh->hash_next->hash_pprev = sh->hash_pprev;
  104. *sh->hash_pprev = sh->hash_next;
  105. sh->hash_pprev = NULL;
  106. }
  107. }
  108. static __inline__ void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  109. {
  110. struct stripe_head **shp = &stripe_hash(conf, sh->sector);
  111. PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  112. CHECK_DEVLOCK();
  113. if ((sh->hash_next = *shp) != NULL)
  114. (*shp)->hash_pprev = &sh->hash_next;
  115. *shp = sh;
  116. sh->hash_pprev = shp;
  117. }
  118. /* find an idle stripe, make sure it is unhashed, and return it. */
  119. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  120. {
  121. struct stripe_head *sh = NULL;
  122. struct list_head *first;
  123. CHECK_DEVLOCK();
  124. if (list_empty(&conf->inactive_list))
  125. goto out;
  126. first = conf->inactive_list.next;
  127. sh = list_entry(first, struct stripe_head, lru);
  128. list_del_init(first);
  129. remove_hash(sh);
  130. atomic_inc(&conf->active_stripes);
  131. out:
  132. return sh;
  133. }
  134. static void shrink_buffers(struct stripe_head *sh, int num)
  135. {
  136. struct page *p;
  137. int i;
  138. for (i=0; i<num ; i++) {
  139. p = sh->dev[i].page;
  140. if (!p)
  141. continue;
  142. sh->dev[i].page = NULL;
  143. page_cache_release(p);
  144. }
  145. }
  146. static int grow_buffers(struct stripe_head *sh, int num)
  147. {
  148. int i;
  149. for (i=0; i<num; i++) {
  150. struct page *page;
  151. if (!(page = alloc_page(GFP_KERNEL))) {
  152. return 1;
  153. }
  154. sh->dev[i].page = page;
  155. }
  156. return 0;
  157. }
  158. static void raid5_build_block (struct stripe_head *sh, int i);
  159. static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
  160. {
  161. raid5_conf_t *conf = sh->raid_conf;
  162. int disks = conf->raid_disks, i;
  163. if (atomic_read(&sh->count) != 0)
  164. BUG();
  165. if (test_bit(STRIPE_HANDLE, &sh->state))
  166. BUG();
  167. CHECK_DEVLOCK();
  168. PRINTK("init_stripe called, stripe %llu\n",
  169. (unsigned long long)sh->sector);
  170. remove_hash(sh);
  171. sh->sector = sector;
  172. sh->pd_idx = pd_idx;
  173. sh->state = 0;
  174. for (i=disks; i--; ) {
  175. struct r5dev *dev = &sh->dev[i];
  176. if (dev->toread || dev->towrite || dev->written ||
  177. test_bit(R5_LOCKED, &dev->flags)) {
  178. printk("sector=%llx i=%d %p %p %p %d\n",
  179. (unsigned long long)sh->sector, i, dev->toread,
  180. dev->towrite, dev->written,
  181. test_bit(R5_LOCKED, &dev->flags));
  182. BUG();
  183. }
  184. dev->flags = 0;
  185. raid5_build_block(sh, i);
  186. }
  187. insert_hash(conf, sh);
  188. }
  189. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector)
  190. {
  191. struct stripe_head *sh;
  192. CHECK_DEVLOCK();
  193. PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
  194. for (sh = stripe_hash(conf, sector); sh; sh = sh->hash_next)
  195. if (sh->sector == sector)
  196. return sh;
  197. PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
  198. return NULL;
  199. }
  200. static void unplug_slaves(mddev_t *mddev);
  201. static void raid5_unplug_device(request_queue_t *q);
  202. static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector,
  203. int pd_idx, int noblock)
  204. {
  205. struct stripe_head *sh;
  206. PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
  207. spin_lock_irq(&conf->device_lock);
  208. do {
  209. sh = __find_stripe(conf, sector);
  210. if (!sh) {
  211. if (!conf->inactive_blocked)
  212. sh = get_free_stripe(conf);
  213. if (noblock && sh == NULL)
  214. break;
  215. if (!sh) {
  216. conf->inactive_blocked = 1;
  217. wait_event_lock_irq(conf->wait_for_stripe,
  218. !list_empty(&conf->inactive_list) &&
  219. (atomic_read(&conf->active_stripes) < (NR_STRIPES *3/4)
  220. || !conf->inactive_blocked),
  221. conf->device_lock,
  222. unplug_slaves(conf->mddev);
  223. );
  224. conf->inactive_blocked = 0;
  225. } else
  226. init_stripe(sh, sector, pd_idx);
  227. } else {
  228. if (atomic_read(&sh->count)) {
  229. if (!list_empty(&sh->lru))
  230. BUG();
  231. } else {
  232. if (!test_bit(STRIPE_HANDLE, &sh->state))
  233. atomic_inc(&conf->active_stripes);
  234. if (list_empty(&sh->lru))
  235. BUG();
  236. list_del_init(&sh->lru);
  237. }
  238. }
  239. } while (sh == NULL);
  240. if (sh)
  241. atomic_inc(&sh->count);
  242. spin_unlock_irq(&conf->device_lock);
  243. return sh;
  244. }
  245. static int grow_stripes(raid5_conf_t *conf, int num)
  246. {
  247. struct stripe_head *sh;
  248. kmem_cache_t *sc;
  249. int devs = conf->raid_disks;
  250. sprintf(conf->cache_name, "raid5/%s", mdname(conf->mddev));
  251. sc = kmem_cache_create(conf->cache_name,
  252. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  253. 0, 0, NULL, NULL);
  254. if (!sc)
  255. return 1;
  256. conf->slab_cache = sc;
  257. while (num--) {
  258. sh = kmem_cache_alloc(sc, GFP_KERNEL);
  259. if (!sh)
  260. return 1;
  261. memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev));
  262. sh->raid_conf = conf;
  263. spin_lock_init(&sh->lock);
  264. if (grow_buffers(sh, conf->raid_disks)) {
  265. shrink_buffers(sh, conf->raid_disks);
  266. kmem_cache_free(sc, sh);
  267. return 1;
  268. }
  269. /* we just created an active stripe so... */
  270. atomic_set(&sh->count, 1);
  271. atomic_inc(&conf->active_stripes);
  272. INIT_LIST_HEAD(&sh->lru);
  273. release_stripe(sh);
  274. }
  275. return 0;
  276. }
  277. static void shrink_stripes(raid5_conf_t *conf)
  278. {
  279. struct stripe_head *sh;
  280. while (1) {
  281. spin_lock_irq(&conf->device_lock);
  282. sh = get_free_stripe(conf);
  283. spin_unlock_irq(&conf->device_lock);
  284. if (!sh)
  285. break;
  286. if (atomic_read(&sh->count))
  287. BUG();
  288. shrink_buffers(sh, conf->raid_disks);
  289. kmem_cache_free(conf->slab_cache, sh);
  290. atomic_dec(&conf->active_stripes);
  291. }
  292. kmem_cache_destroy(conf->slab_cache);
  293. conf->slab_cache = NULL;
  294. }
  295. static int raid5_end_read_request (struct bio * bi, unsigned int bytes_done,
  296. int error)
  297. {
  298. struct stripe_head *sh = bi->bi_private;
  299. raid5_conf_t *conf = sh->raid_conf;
  300. int disks = conf->raid_disks, i;
  301. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  302. if (bi->bi_size)
  303. return 1;
  304. for (i=0 ; i<disks; i++)
  305. if (bi == &sh->dev[i].req)
  306. break;
  307. PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  308. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  309. uptodate);
  310. if (i == disks) {
  311. BUG();
  312. return 0;
  313. }
  314. if (uptodate) {
  315. #if 0
  316. struct bio *bio;
  317. unsigned long flags;
  318. spin_lock_irqsave(&conf->device_lock, flags);
  319. /* we can return a buffer if we bypassed the cache or
  320. * if the top buffer is not in highmem. If there are
  321. * multiple buffers, leave the extra work to
  322. * handle_stripe
  323. */
  324. buffer = sh->bh_read[i];
  325. if (buffer &&
  326. (!PageHighMem(buffer->b_page)
  327. || buffer->b_page == bh->b_page )
  328. ) {
  329. sh->bh_read[i] = buffer->b_reqnext;
  330. buffer->b_reqnext = NULL;
  331. } else
  332. buffer = NULL;
  333. spin_unlock_irqrestore(&conf->device_lock, flags);
  334. if (sh->bh_page[i]==bh->b_page)
  335. set_buffer_uptodate(bh);
  336. if (buffer) {
  337. if (buffer->b_page != bh->b_page)
  338. memcpy(buffer->b_data, bh->b_data, bh->b_size);
  339. buffer->b_end_io(buffer, 1);
  340. }
  341. #else
  342. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  343. #endif
  344. } else {
  345. md_error(conf->mddev, conf->disks[i].rdev);
  346. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  347. }
  348. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  349. #if 0
  350. /* must restore b_page before unlocking buffer... */
  351. if (sh->bh_page[i] != bh->b_page) {
  352. bh->b_page = sh->bh_page[i];
  353. bh->b_data = page_address(bh->b_page);
  354. clear_buffer_uptodate(bh);
  355. }
  356. #endif
  357. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  358. set_bit(STRIPE_HANDLE, &sh->state);
  359. release_stripe(sh);
  360. return 0;
  361. }
  362. static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
  363. int error)
  364. {
  365. struct stripe_head *sh = bi->bi_private;
  366. raid5_conf_t *conf = sh->raid_conf;
  367. int disks = conf->raid_disks, i;
  368. unsigned long flags;
  369. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  370. if (bi->bi_size)
  371. return 1;
  372. for (i=0 ; i<disks; i++)
  373. if (bi == &sh->dev[i].req)
  374. break;
  375. PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  376. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  377. uptodate);
  378. if (i == disks) {
  379. BUG();
  380. return 0;
  381. }
  382. spin_lock_irqsave(&conf->device_lock, flags);
  383. if (!uptodate)
  384. md_error(conf->mddev, conf->disks[i].rdev);
  385. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  386. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  387. set_bit(STRIPE_HANDLE, &sh->state);
  388. __release_stripe(conf, sh);
  389. spin_unlock_irqrestore(&conf->device_lock, flags);
  390. return 0;
  391. }
  392. static sector_t compute_blocknr(struct stripe_head *sh, int i);
  393. static void raid5_build_block (struct stripe_head *sh, int i)
  394. {
  395. struct r5dev *dev = &sh->dev[i];
  396. bio_init(&dev->req);
  397. dev->req.bi_io_vec = &dev->vec;
  398. dev->req.bi_vcnt++;
  399. dev->req.bi_max_vecs++;
  400. dev->vec.bv_page = dev->page;
  401. dev->vec.bv_len = STRIPE_SIZE;
  402. dev->vec.bv_offset = 0;
  403. dev->req.bi_sector = sh->sector;
  404. dev->req.bi_private = sh;
  405. dev->flags = 0;
  406. if (i != sh->pd_idx)
  407. dev->sector = compute_blocknr(sh, i);
  408. }
  409. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  410. {
  411. char b[BDEVNAME_SIZE];
  412. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  413. PRINTK("raid5: error called\n");
  414. if (!rdev->faulty) {
  415. mddev->sb_dirty = 1;
  416. if (rdev->in_sync) {
  417. conf->working_disks--;
  418. mddev->degraded++;
  419. conf->failed_disks++;
  420. rdev->in_sync = 0;
  421. /*
  422. * if recovery was running, make sure it aborts.
  423. */
  424. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  425. }
  426. rdev->faulty = 1;
  427. printk (KERN_ALERT
  428. "raid5: Disk failure on %s, disabling device."
  429. " Operation continuing on %d devices\n",
  430. bdevname(rdev->bdev,b), conf->working_disks);
  431. }
  432. }
  433. /*
  434. * Input: a 'big' sector number,
  435. * Output: index of the data and parity disk, and the sector # in them.
  436. */
  437. static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
  438. unsigned int data_disks, unsigned int * dd_idx,
  439. unsigned int * pd_idx, raid5_conf_t *conf)
  440. {
  441. long stripe;
  442. unsigned long chunk_number;
  443. unsigned int chunk_offset;
  444. sector_t new_sector;
  445. int sectors_per_chunk = conf->chunk_size >> 9;
  446. /* First compute the information on this sector */
  447. /*
  448. * Compute the chunk number and the sector offset inside the chunk
  449. */
  450. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  451. chunk_number = r_sector;
  452. BUG_ON(r_sector != chunk_number);
  453. /*
  454. * Compute the stripe number
  455. */
  456. stripe = chunk_number / data_disks;
  457. /*
  458. * Compute the data disk and parity disk indexes inside the stripe
  459. */
  460. *dd_idx = chunk_number % data_disks;
  461. /*
  462. * Select the parity disk based on the user selected algorithm.
  463. */
  464. if (conf->level == 4)
  465. *pd_idx = data_disks;
  466. else switch (conf->algorithm) {
  467. case ALGORITHM_LEFT_ASYMMETRIC:
  468. *pd_idx = data_disks - stripe % raid_disks;
  469. if (*dd_idx >= *pd_idx)
  470. (*dd_idx)++;
  471. break;
  472. case ALGORITHM_RIGHT_ASYMMETRIC:
  473. *pd_idx = stripe % raid_disks;
  474. if (*dd_idx >= *pd_idx)
  475. (*dd_idx)++;
  476. break;
  477. case ALGORITHM_LEFT_SYMMETRIC:
  478. *pd_idx = data_disks - stripe % raid_disks;
  479. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  480. break;
  481. case ALGORITHM_RIGHT_SYMMETRIC:
  482. *pd_idx = stripe % raid_disks;
  483. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  484. break;
  485. default:
  486. printk("raid5: unsupported algorithm %d\n",
  487. conf->algorithm);
  488. }
  489. /*
  490. * Finally, compute the new sector number
  491. */
  492. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  493. return new_sector;
  494. }
  495. static sector_t compute_blocknr(struct stripe_head *sh, int i)
  496. {
  497. raid5_conf_t *conf = sh->raid_conf;
  498. int raid_disks = conf->raid_disks, data_disks = raid_disks - 1;
  499. sector_t new_sector = sh->sector, check;
  500. int sectors_per_chunk = conf->chunk_size >> 9;
  501. sector_t stripe;
  502. int chunk_offset;
  503. int chunk_number, dummy1, dummy2, dd_idx = i;
  504. sector_t r_sector;
  505. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  506. stripe = new_sector;
  507. BUG_ON(new_sector != stripe);
  508. switch (conf->algorithm) {
  509. case ALGORITHM_LEFT_ASYMMETRIC:
  510. case ALGORITHM_RIGHT_ASYMMETRIC:
  511. if (i > sh->pd_idx)
  512. i--;
  513. break;
  514. case ALGORITHM_LEFT_SYMMETRIC:
  515. case ALGORITHM_RIGHT_SYMMETRIC:
  516. if (i < sh->pd_idx)
  517. i += raid_disks;
  518. i -= (sh->pd_idx + 1);
  519. break;
  520. default:
  521. printk("raid5: unsupported algorithm %d\n",
  522. conf->algorithm);
  523. }
  524. chunk_number = stripe * data_disks + i;
  525. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  526. check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
  527. if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
  528. printk("compute_blocknr: map not correct\n");
  529. return 0;
  530. }
  531. return r_sector;
  532. }
  533. /*
  534. * Copy data between a page in the stripe cache, and a bio.
  535. * There are no alignment or size guarantees between the page or the
  536. * bio except that there is some overlap.
  537. * All iovecs in the bio must be considered.
  538. */
  539. static void copy_data(int frombio, struct bio *bio,
  540. struct page *page,
  541. sector_t sector)
  542. {
  543. char *pa = page_address(page);
  544. struct bio_vec *bvl;
  545. int i;
  546. int page_offset;
  547. if (bio->bi_sector >= sector)
  548. page_offset = (signed)(bio->bi_sector - sector) * 512;
  549. else
  550. page_offset = (signed)(sector - bio->bi_sector) * -512;
  551. bio_for_each_segment(bvl, bio, i) {
  552. int len = bio_iovec_idx(bio,i)->bv_len;
  553. int clen;
  554. int b_offset = 0;
  555. if (page_offset < 0) {
  556. b_offset = -page_offset;
  557. page_offset += b_offset;
  558. len -= b_offset;
  559. }
  560. if (len > 0 && page_offset + len > STRIPE_SIZE)
  561. clen = STRIPE_SIZE - page_offset;
  562. else clen = len;
  563. if (clen > 0) {
  564. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  565. if (frombio)
  566. memcpy(pa+page_offset, ba+b_offset, clen);
  567. else
  568. memcpy(ba+b_offset, pa+page_offset, clen);
  569. __bio_kunmap_atomic(ba, KM_USER0);
  570. }
  571. if (clen < len) /* hit end of page */
  572. break;
  573. page_offset += len;
  574. }
  575. }
  576. #define check_xor() do { \
  577. if (count == MAX_XOR_BLOCKS) { \
  578. xor_block(count, STRIPE_SIZE, ptr); \
  579. count = 1; \
  580. } \
  581. } while(0)
  582. static void compute_block(struct stripe_head *sh, int dd_idx)
  583. {
  584. raid5_conf_t *conf = sh->raid_conf;
  585. int i, count, disks = conf->raid_disks;
  586. void *ptr[MAX_XOR_BLOCKS], *p;
  587. PRINTK("compute_block, stripe %llu, idx %d\n",
  588. (unsigned long long)sh->sector, dd_idx);
  589. ptr[0] = page_address(sh->dev[dd_idx].page);
  590. memset(ptr[0], 0, STRIPE_SIZE);
  591. count = 1;
  592. for (i = disks ; i--; ) {
  593. if (i == dd_idx)
  594. continue;
  595. p = page_address(sh->dev[i].page);
  596. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  597. ptr[count++] = p;
  598. else
  599. printk("compute_block() %d, stripe %llu, %d"
  600. " not present\n", dd_idx,
  601. (unsigned long long)sh->sector, i);
  602. check_xor();
  603. }
  604. if (count != 1)
  605. xor_block(count, STRIPE_SIZE, ptr);
  606. set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  607. }
  608. static void compute_parity(struct stripe_head *sh, int method)
  609. {
  610. raid5_conf_t *conf = sh->raid_conf;
  611. int i, pd_idx = sh->pd_idx, disks = conf->raid_disks, count;
  612. void *ptr[MAX_XOR_BLOCKS];
  613. struct bio *chosen;
  614. PRINTK("compute_parity, stripe %llu, method %d\n",
  615. (unsigned long long)sh->sector, method);
  616. count = 1;
  617. ptr[0] = page_address(sh->dev[pd_idx].page);
  618. switch(method) {
  619. case READ_MODIFY_WRITE:
  620. if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags))
  621. BUG();
  622. for (i=disks ; i-- ;) {
  623. if (i==pd_idx)
  624. continue;
  625. if (sh->dev[i].towrite &&
  626. test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  627. ptr[count++] = page_address(sh->dev[i].page);
  628. chosen = sh->dev[i].towrite;
  629. sh->dev[i].towrite = NULL;
  630. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  631. wake_up(&conf->wait_for_overlap);
  632. if (sh->dev[i].written) BUG();
  633. sh->dev[i].written = chosen;
  634. check_xor();
  635. }
  636. }
  637. break;
  638. case RECONSTRUCT_WRITE:
  639. memset(ptr[0], 0, STRIPE_SIZE);
  640. for (i= disks; i-- ;)
  641. if (i!=pd_idx && sh->dev[i].towrite) {
  642. chosen = sh->dev[i].towrite;
  643. sh->dev[i].towrite = NULL;
  644. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  645. wake_up(&conf->wait_for_overlap);
  646. if (sh->dev[i].written) BUG();
  647. sh->dev[i].written = chosen;
  648. }
  649. break;
  650. case CHECK_PARITY:
  651. break;
  652. }
  653. if (count>1) {
  654. xor_block(count, STRIPE_SIZE, ptr);
  655. count = 1;
  656. }
  657. for (i = disks; i--;)
  658. if (sh->dev[i].written) {
  659. sector_t sector = sh->dev[i].sector;
  660. struct bio *wbi = sh->dev[i].written;
  661. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  662. copy_data(1, wbi, sh->dev[i].page, sector);
  663. wbi = r5_next_bio(wbi, sector);
  664. }
  665. set_bit(R5_LOCKED, &sh->dev[i].flags);
  666. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  667. }
  668. switch(method) {
  669. case RECONSTRUCT_WRITE:
  670. case CHECK_PARITY:
  671. for (i=disks; i--;)
  672. if (i != pd_idx) {
  673. ptr[count++] = page_address(sh->dev[i].page);
  674. check_xor();
  675. }
  676. break;
  677. case READ_MODIFY_WRITE:
  678. for (i = disks; i--;)
  679. if (sh->dev[i].written) {
  680. ptr[count++] = page_address(sh->dev[i].page);
  681. check_xor();
  682. }
  683. }
  684. if (count != 1)
  685. xor_block(count, STRIPE_SIZE, ptr);
  686. if (method != CHECK_PARITY) {
  687. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  688. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  689. } else
  690. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  691. }
  692. /*
  693. * Each stripe/dev can have one or more bion attached.
  694. * toread/towrite point to the first in a chain.
  695. * The bi_next chain must be in order.
  696. */
  697. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  698. {
  699. struct bio **bip;
  700. raid5_conf_t *conf = sh->raid_conf;
  701. PRINTK("adding bh b#%llu to stripe s#%llu\n",
  702. (unsigned long long)bi->bi_sector,
  703. (unsigned long long)sh->sector);
  704. spin_lock(&sh->lock);
  705. spin_lock_irq(&conf->device_lock);
  706. if (forwrite)
  707. bip = &sh->dev[dd_idx].towrite;
  708. else
  709. bip = &sh->dev[dd_idx].toread;
  710. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  711. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  712. goto overlap;
  713. bip = & (*bip)->bi_next;
  714. }
  715. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  716. goto overlap;
  717. if (*bip && bi->bi_next && (*bip) != bi->bi_next)
  718. BUG();
  719. if (*bip)
  720. bi->bi_next = *bip;
  721. *bip = bi;
  722. bi->bi_phys_segments ++;
  723. spin_unlock_irq(&conf->device_lock);
  724. spin_unlock(&sh->lock);
  725. PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
  726. (unsigned long long)bi->bi_sector,
  727. (unsigned long long)sh->sector, dd_idx);
  728. if (forwrite) {
  729. /* check if page is covered */
  730. sector_t sector = sh->dev[dd_idx].sector;
  731. for (bi=sh->dev[dd_idx].towrite;
  732. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  733. bi && bi->bi_sector <= sector;
  734. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  735. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  736. sector = bi->bi_sector + (bi->bi_size>>9);
  737. }
  738. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  739. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  740. }
  741. return 1;
  742. overlap:
  743. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  744. spin_unlock_irq(&conf->device_lock);
  745. spin_unlock(&sh->lock);
  746. return 0;
  747. }
  748. /*
  749. * handle_stripe - do things to a stripe.
  750. *
  751. * We lock the stripe and then examine the state of various bits
  752. * to see what needs to be done.
  753. * Possible results:
  754. * return some read request which now have data
  755. * return some write requests which are safely on disc
  756. * schedule a read on some buffers
  757. * schedule a write of some buffers
  758. * return confirmation of parity correctness
  759. *
  760. * Parity calculations are done inside the stripe lock
  761. * buffers are taken off read_list or write_list, and bh_cache buffers
  762. * get BH_Lock set before the stripe lock is released.
  763. *
  764. */
  765. static void handle_stripe(struct stripe_head *sh)
  766. {
  767. raid5_conf_t *conf = sh->raid_conf;
  768. int disks = conf->raid_disks;
  769. struct bio *return_bi= NULL;
  770. struct bio *bi;
  771. int i;
  772. int syncing;
  773. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  774. int non_overwrite = 0;
  775. int failed_num=0;
  776. struct r5dev *dev;
  777. PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
  778. (unsigned long long)sh->sector, atomic_read(&sh->count),
  779. sh->pd_idx);
  780. spin_lock(&sh->lock);
  781. clear_bit(STRIPE_HANDLE, &sh->state);
  782. clear_bit(STRIPE_DELAYED, &sh->state);
  783. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  784. /* Now to look around and see what can be done */
  785. for (i=disks; i--; ) {
  786. mdk_rdev_t *rdev;
  787. dev = &sh->dev[i];
  788. clear_bit(R5_Insync, &dev->flags);
  789. clear_bit(R5_Syncio, &dev->flags);
  790. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  791. i, dev->flags, dev->toread, dev->towrite, dev->written);
  792. /* maybe we can reply to a read */
  793. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  794. struct bio *rbi, *rbi2;
  795. PRINTK("Return read for disc %d\n", i);
  796. spin_lock_irq(&conf->device_lock);
  797. rbi = dev->toread;
  798. dev->toread = NULL;
  799. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  800. wake_up(&conf->wait_for_overlap);
  801. spin_unlock_irq(&conf->device_lock);
  802. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  803. copy_data(0, rbi, dev->page, dev->sector);
  804. rbi2 = r5_next_bio(rbi, dev->sector);
  805. spin_lock_irq(&conf->device_lock);
  806. if (--rbi->bi_phys_segments == 0) {
  807. rbi->bi_next = return_bi;
  808. return_bi = rbi;
  809. }
  810. spin_unlock_irq(&conf->device_lock);
  811. rbi = rbi2;
  812. }
  813. }
  814. /* now count some things */
  815. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  816. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  817. if (dev->toread) to_read++;
  818. if (dev->towrite) {
  819. to_write++;
  820. if (!test_bit(R5_OVERWRITE, &dev->flags))
  821. non_overwrite++;
  822. }
  823. if (dev->written) written++;
  824. rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */
  825. if (!rdev || !rdev->in_sync) {
  826. failed++;
  827. failed_num = i;
  828. } else
  829. set_bit(R5_Insync, &dev->flags);
  830. }
  831. PRINTK("locked=%d uptodate=%d to_read=%d"
  832. " to_write=%d failed=%d failed_num=%d\n",
  833. locked, uptodate, to_read, to_write, failed, failed_num);
  834. /* check if the array has lost two devices and, if so, some requests might
  835. * need to be failed
  836. */
  837. if (failed > 1 && to_read+to_write+written) {
  838. spin_lock_irq(&conf->device_lock);
  839. for (i=disks; i--; ) {
  840. /* fail all writes first */
  841. bi = sh->dev[i].towrite;
  842. sh->dev[i].towrite = NULL;
  843. if (bi) to_write--;
  844. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  845. wake_up(&conf->wait_for_overlap);
  846. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  847. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  848. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  849. if (--bi->bi_phys_segments == 0) {
  850. md_write_end(conf->mddev);
  851. bi->bi_next = return_bi;
  852. return_bi = bi;
  853. }
  854. bi = nextbi;
  855. }
  856. /* and fail all 'written' */
  857. bi = sh->dev[i].written;
  858. sh->dev[i].written = NULL;
  859. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  860. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  861. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  862. if (--bi->bi_phys_segments == 0) {
  863. md_write_end(conf->mddev);
  864. bi->bi_next = return_bi;
  865. return_bi = bi;
  866. }
  867. bi = bi2;
  868. }
  869. /* fail any reads if this device is non-operational */
  870. if (!test_bit(R5_Insync, &sh->dev[i].flags)) {
  871. bi = sh->dev[i].toread;
  872. sh->dev[i].toread = NULL;
  873. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  874. wake_up(&conf->wait_for_overlap);
  875. if (bi) to_read--;
  876. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  877. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  878. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  879. if (--bi->bi_phys_segments == 0) {
  880. bi->bi_next = return_bi;
  881. return_bi = bi;
  882. }
  883. bi = nextbi;
  884. }
  885. }
  886. }
  887. spin_unlock_irq(&conf->device_lock);
  888. }
  889. if (failed > 1 && syncing) {
  890. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  891. clear_bit(STRIPE_SYNCING, &sh->state);
  892. syncing = 0;
  893. }
  894. /* might be able to return some write requests if the parity block
  895. * is safe, or on a failed drive
  896. */
  897. dev = &sh->dev[sh->pd_idx];
  898. if ( written &&
  899. ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
  900. test_bit(R5_UPTODATE, &dev->flags))
  901. || (failed == 1 && failed_num == sh->pd_idx))
  902. ) {
  903. /* any written block on an uptodate or failed drive can be returned.
  904. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  905. * never LOCKED, so we don't need to test 'failed' directly.
  906. */
  907. for (i=disks; i--; )
  908. if (sh->dev[i].written) {
  909. dev = &sh->dev[i];
  910. if (!test_bit(R5_LOCKED, &dev->flags) &&
  911. test_bit(R5_UPTODATE, &dev->flags) ) {
  912. /* We can return any write requests */
  913. struct bio *wbi, *wbi2;
  914. PRINTK("Return write for disc %d\n", i);
  915. spin_lock_irq(&conf->device_lock);
  916. wbi = dev->written;
  917. dev->written = NULL;
  918. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  919. wbi2 = r5_next_bio(wbi, dev->sector);
  920. if (--wbi->bi_phys_segments == 0) {
  921. md_write_end(conf->mddev);
  922. wbi->bi_next = return_bi;
  923. return_bi = wbi;
  924. }
  925. wbi = wbi2;
  926. }
  927. spin_unlock_irq(&conf->device_lock);
  928. }
  929. }
  930. }
  931. /* Now we might consider reading some blocks, either to check/generate
  932. * parity, or to satisfy requests
  933. * or to load a block that is being partially written.
  934. */
  935. if (to_read || non_overwrite || (syncing && (uptodate < disks))) {
  936. for (i=disks; i--;) {
  937. dev = &sh->dev[i];
  938. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  939. (dev->toread ||
  940. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  941. syncing ||
  942. (failed && (sh->dev[failed_num].toread ||
  943. (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
  944. )
  945. ) {
  946. /* we would like to get this block, possibly
  947. * by computing it, but we might not be able to
  948. */
  949. if (uptodate == disks-1) {
  950. PRINTK("Computing block %d\n", i);
  951. compute_block(sh, i);
  952. uptodate++;
  953. } else if (test_bit(R5_Insync, &dev->flags)) {
  954. set_bit(R5_LOCKED, &dev->flags);
  955. set_bit(R5_Wantread, &dev->flags);
  956. #if 0
  957. /* if I am just reading this block and we don't have
  958. a failed drive, or any pending writes then sidestep the cache */
  959. if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
  960. ! syncing && !failed && !to_write) {
  961. sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
  962. sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
  963. }
  964. #endif
  965. locked++;
  966. PRINTK("Reading block %d (sync=%d)\n",
  967. i, syncing);
  968. if (syncing)
  969. md_sync_acct(conf->disks[i].rdev->bdev,
  970. STRIPE_SECTORS);
  971. }
  972. }
  973. }
  974. set_bit(STRIPE_HANDLE, &sh->state);
  975. }
  976. /* now to consider writing and what else, if anything should be read */
  977. if (to_write) {
  978. int rmw=0, rcw=0;
  979. for (i=disks ; i--;) {
  980. /* would I have to read this buffer for read_modify_write */
  981. dev = &sh->dev[i];
  982. if ((dev->towrite || i == sh->pd_idx) &&
  983. (!test_bit(R5_LOCKED, &dev->flags)
  984. #if 0
  985. || sh->bh_page[i]!=bh->b_page
  986. #endif
  987. ) &&
  988. !test_bit(R5_UPTODATE, &dev->flags)) {
  989. if (test_bit(R5_Insync, &dev->flags)
  990. /* && !(!mddev->insync && i == sh->pd_idx) */
  991. )
  992. rmw++;
  993. else rmw += 2*disks; /* cannot read it */
  994. }
  995. /* Would I have to read this buffer for reconstruct_write */
  996. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  997. (!test_bit(R5_LOCKED, &dev->flags)
  998. #if 0
  999. || sh->bh_page[i] != bh->b_page
  1000. #endif
  1001. ) &&
  1002. !test_bit(R5_UPTODATE, &dev->flags)) {
  1003. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1004. else rcw += 2*disks;
  1005. }
  1006. }
  1007. PRINTK("for sector %llu, rmw=%d rcw=%d\n",
  1008. (unsigned long long)sh->sector, rmw, rcw);
  1009. set_bit(STRIPE_HANDLE, &sh->state);
  1010. if (rmw < rcw && rmw > 0)
  1011. /* prefer read-modify-write, but need to get some data */
  1012. for (i=disks; i--;) {
  1013. dev = &sh->dev[i];
  1014. if ((dev->towrite || i == sh->pd_idx) &&
  1015. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1016. test_bit(R5_Insync, &dev->flags)) {
  1017. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1018. {
  1019. PRINTK("Read_old block %d for r-m-w\n", i);
  1020. set_bit(R5_LOCKED, &dev->flags);
  1021. set_bit(R5_Wantread, &dev->flags);
  1022. locked++;
  1023. } else {
  1024. set_bit(STRIPE_DELAYED, &sh->state);
  1025. set_bit(STRIPE_HANDLE, &sh->state);
  1026. }
  1027. }
  1028. }
  1029. if (rcw <= rmw && rcw > 0)
  1030. /* want reconstruct write, but need to get some data */
  1031. for (i=disks; i--;) {
  1032. dev = &sh->dev[i];
  1033. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1034. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1035. test_bit(R5_Insync, &dev->flags)) {
  1036. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1037. {
  1038. PRINTK("Read_old block %d for Reconstruct\n", i);
  1039. set_bit(R5_LOCKED, &dev->flags);
  1040. set_bit(R5_Wantread, &dev->flags);
  1041. locked++;
  1042. } else {
  1043. set_bit(STRIPE_DELAYED, &sh->state);
  1044. set_bit(STRIPE_HANDLE, &sh->state);
  1045. }
  1046. }
  1047. }
  1048. /* now if nothing is locked, and if we have enough data, we can start a write request */
  1049. if (locked == 0 && (rcw == 0 ||rmw == 0)) {
  1050. PRINTK("Computing parity...\n");
  1051. compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
  1052. /* now every locked buffer is ready to be written */
  1053. for (i=disks; i--;)
  1054. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  1055. PRINTK("Writing block %d\n", i);
  1056. locked++;
  1057. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1058. if (!test_bit(R5_Insync, &sh->dev[i].flags)
  1059. || (i==sh->pd_idx && failed == 0))
  1060. set_bit(STRIPE_INSYNC, &sh->state);
  1061. }
  1062. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1063. atomic_dec(&conf->preread_active_stripes);
  1064. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  1065. md_wakeup_thread(conf->mddev->thread);
  1066. }
  1067. }
  1068. }
  1069. /* maybe we need to check and possibly fix the parity for this stripe
  1070. * Any reads will already have been scheduled, so we just see if enough data
  1071. * is available
  1072. */
  1073. if (syncing && locked == 0 &&
  1074. !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 1) {
  1075. set_bit(STRIPE_HANDLE, &sh->state);
  1076. if (failed == 0) {
  1077. char *pagea;
  1078. if (uptodate != disks)
  1079. BUG();
  1080. compute_parity(sh, CHECK_PARITY);
  1081. uptodate--;
  1082. pagea = page_address(sh->dev[sh->pd_idx].page);
  1083. if ((*(u32*)pagea) == 0 &&
  1084. !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
  1085. /* parity is correct (on disc, not in buffer any more) */
  1086. set_bit(STRIPE_INSYNC, &sh->state);
  1087. }
  1088. }
  1089. if (!test_bit(STRIPE_INSYNC, &sh->state)) {
  1090. if (failed==0)
  1091. failed_num = sh->pd_idx;
  1092. /* should be able to compute the missing block and write it to spare */
  1093. if (!test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)) {
  1094. if (uptodate+1 != disks)
  1095. BUG();
  1096. compute_block(sh, failed_num);
  1097. uptodate++;
  1098. }
  1099. if (uptodate != disks)
  1100. BUG();
  1101. dev = &sh->dev[failed_num];
  1102. set_bit(R5_LOCKED, &dev->flags);
  1103. set_bit(R5_Wantwrite, &dev->flags);
  1104. locked++;
  1105. set_bit(STRIPE_INSYNC, &sh->state);
  1106. set_bit(R5_Syncio, &dev->flags);
  1107. }
  1108. }
  1109. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  1110. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  1111. clear_bit(STRIPE_SYNCING, &sh->state);
  1112. }
  1113. spin_unlock(&sh->lock);
  1114. while ((bi=return_bi)) {
  1115. int bytes = bi->bi_size;
  1116. return_bi = bi->bi_next;
  1117. bi->bi_next = NULL;
  1118. bi->bi_size = 0;
  1119. bi->bi_end_io(bi, bytes, 0);
  1120. }
  1121. for (i=disks; i-- ;) {
  1122. int rw;
  1123. struct bio *bi;
  1124. mdk_rdev_t *rdev;
  1125. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  1126. rw = 1;
  1127. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  1128. rw = 0;
  1129. else
  1130. continue;
  1131. bi = &sh->dev[i].req;
  1132. bi->bi_rw = rw;
  1133. if (rw)
  1134. bi->bi_end_io = raid5_end_write_request;
  1135. else
  1136. bi->bi_end_io = raid5_end_read_request;
  1137. rcu_read_lock();
  1138. rdev = conf->disks[i].rdev;
  1139. if (rdev && rdev->faulty)
  1140. rdev = NULL;
  1141. if (rdev)
  1142. atomic_inc(&rdev->nr_pending);
  1143. rcu_read_unlock();
  1144. if (rdev) {
  1145. if (test_bit(R5_Syncio, &sh->dev[i].flags))
  1146. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  1147. bi->bi_bdev = rdev->bdev;
  1148. PRINTK("for %llu schedule op %ld on disc %d\n",
  1149. (unsigned long long)sh->sector, bi->bi_rw, i);
  1150. atomic_inc(&sh->count);
  1151. bi->bi_sector = sh->sector + rdev->data_offset;
  1152. bi->bi_flags = 1 << BIO_UPTODATE;
  1153. bi->bi_vcnt = 1;
  1154. bi->bi_max_vecs = 1;
  1155. bi->bi_idx = 0;
  1156. bi->bi_io_vec = &sh->dev[i].vec;
  1157. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  1158. bi->bi_io_vec[0].bv_offset = 0;
  1159. bi->bi_size = STRIPE_SIZE;
  1160. bi->bi_next = NULL;
  1161. generic_make_request(bi);
  1162. } else {
  1163. PRINTK("skip op %ld on disc %d for sector %llu\n",
  1164. bi->bi_rw, i, (unsigned long long)sh->sector);
  1165. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1166. set_bit(STRIPE_HANDLE, &sh->state);
  1167. }
  1168. }
  1169. }
  1170. static inline void raid5_activate_delayed(raid5_conf_t *conf)
  1171. {
  1172. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  1173. while (!list_empty(&conf->delayed_list)) {
  1174. struct list_head *l = conf->delayed_list.next;
  1175. struct stripe_head *sh;
  1176. sh = list_entry(l, struct stripe_head, lru);
  1177. list_del_init(l);
  1178. clear_bit(STRIPE_DELAYED, &sh->state);
  1179. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1180. atomic_inc(&conf->preread_active_stripes);
  1181. list_add_tail(&sh->lru, &conf->handle_list);
  1182. }
  1183. }
  1184. }
  1185. static void unplug_slaves(mddev_t *mddev)
  1186. {
  1187. raid5_conf_t *conf = mddev_to_conf(mddev);
  1188. int i;
  1189. rcu_read_lock();
  1190. for (i=0; i<mddev->raid_disks; i++) {
  1191. mdk_rdev_t *rdev = conf->disks[i].rdev;
  1192. if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
  1193. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  1194. atomic_inc(&rdev->nr_pending);
  1195. rcu_read_unlock();
  1196. if (r_queue->unplug_fn)
  1197. r_queue->unplug_fn(r_queue);
  1198. rdev_dec_pending(rdev, mddev);
  1199. rcu_read_lock();
  1200. }
  1201. }
  1202. rcu_read_unlock();
  1203. }
  1204. static void raid5_unplug_device(request_queue_t *q)
  1205. {
  1206. mddev_t *mddev = q->queuedata;
  1207. raid5_conf_t *conf = mddev_to_conf(mddev);
  1208. unsigned long flags;
  1209. spin_lock_irqsave(&conf->device_lock, flags);
  1210. if (blk_remove_plug(q))
  1211. raid5_activate_delayed(conf);
  1212. md_wakeup_thread(mddev->thread);
  1213. spin_unlock_irqrestore(&conf->device_lock, flags);
  1214. unplug_slaves(mddev);
  1215. }
  1216. static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
  1217. sector_t *error_sector)
  1218. {
  1219. mddev_t *mddev = q->queuedata;
  1220. raid5_conf_t *conf = mddev_to_conf(mddev);
  1221. int i, ret = 0;
  1222. rcu_read_lock();
  1223. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  1224. mdk_rdev_t *rdev = conf->disks[i].rdev;
  1225. if (rdev && !rdev->faulty) {
  1226. struct block_device *bdev = rdev->bdev;
  1227. request_queue_t *r_queue = bdev_get_queue(bdev);
  1228. if (!r_queue->issue_flush_fn)
  1229. ret = -EOPNOTSUPP;
  1230. else {
  1231. atomic_inc(&rdev->nr_pending);
  1232. rcu_read_unlock();
  1233. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  1234. error_sector);
  1235. rdev_dec_pending(rdev, mddev);
  1236. rcu_read_lock();
  1237. }
  1238. }
  1239. }
  1240. rcu_read_unlock();
  1241. return ret;
  1242. }
  1243. static inline void raid5_plug_device(raid5_conf_t *conf)
  1244. {
  1245. spin_lock_irq(&conf->device_lock);
  1246. blk_plug_device(conf->mddev->queue);
  1247. spin_unlock_irq(&conf->device_lock);
  1248. }
  1249. static int make_request (request_queue_t *q, struct bio * bi)
  1250. {
  1251. mddev_t *mddev = q->queuedata;
  1252. raid5_conf_t *conf = mddev_to_conf(mddev);
  1253. const unsigned int raid_disks = conf->raid_disks;
  1254. const unsigned int data_disks = raid_disks - 1;
  1255. unsigned int dd_idx, pd_idx;
  1256. sector_t new_sector;
  1257. sector_t logical_sector, last_sector;
  1258. struct stripe_head *sh;
  1259. if (bio_data_dir(bi)==WRITE) {
  1260. disk_stat_inc(mddev->gendisk, writes);
  1261. disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bi));
  1262. } else {
  1263. disk_stat_inc(mddev->gendisk, reads);
  1264. disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bi));
  1265. }
  1266. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  1267. last_sector = bi->bi_sector + (bi->bi_size>>9);
  1268. bi->bi_next = NULL;
  1269. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  1270. if ( bio_data_dir(bi) == WRITE )
  1271. md_write_start(mddev);
  1272. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  1273. DEFINE_WAIT(w);
  1274. new_sector = raid5_compute_sector(logical_sector,
  1275. raid_disks, data_disks, &dd_idx, &pd_idx, conf);
  1276. PRINTK("raid5: make_request, sector %llu logical %llu\n",
  1277. (unsigned long long)new_sector,
  1278. (unsigned long long)logical_sector);
  1279. retry:
  1280. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  1281. sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
  1282. if (sh) {
  1283. if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  1284. /* Add failed due to overlap. Flush everything
  1285. * and wait a while
  1286. */
  1287. raid5_unplug_device(mddev->queue);
  1288. release_stripe(sh);
  1289. schedule();
  1290. goto retry;
  1291. }
  1292. finish_wait(&conf->wait_for_overlap, &w);
  1293. raid5_plug_device(conf);
  1294. handle_stripe(sh);
  1295. release_stripe(sh);
  1296. } else {
  1297. /* cannot get stripe for read-ahead, just give-up */
  1298. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1299. finish_wait(&conf->wait_for_overlap, &w);
  1300. break;
  1301. }
  1302. }
  1303. spin_lock_irq(&conf->device_lock);
  1304. if (--bi->bi_phys_segments == 0) {
  1305. int bytes = bi->bi_size;
  1306. if ( bio_data_dir(bi) == WRITE )
  1307. md_write_end(mddev);
  1308. bi->bi_size = 0;
  1309. bi->bi_end_io(bi, bytes, 0);
  1310. }
  1311. spin_unlock_irq(&conf->device_lock);
  1312. return 0;
  1313. }
  1314. /* FIXME go_faster isn't used */
  1315. static int sync_request (mddev_t *mddev, sector_t sector_nr, int go_faster)
  1316. {
  1317. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1318. struct stripe_head *sh;
  1319. int sectors_per_chunk = conf->chunk_size >> 9;
  1320. sector_t x;
  1321. unsigned long stripe;
  1322. int chunk_offset;
  1323. int dd_idx, pd_idx;
  1324. sector_t first_sector;
  1325. int raid_disks = conf->raid_disks;
  1326. int data_disks = raid_disks-1;
  1327. if (sector_nr >= mddev->size <<1) {
  1328. /* just being told to finish up .. nothing much to do */
  1329. unplug_slaves(mddev);
  1330. return 0;
  1331. }
  1332. /* if there is 1 or more failed drives and we are trying
  1333. * to resync, then assert that we are finished, because there is
  1334. * nothing we can do.
  1335. */
  1336. if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  1337. int rv = (mddev->size << 1) - sector_nr;
  1338. md_done_sync(mddev, rv, 1);
  1339. return rv;
  1340. }
  1341. x = sector_nr;
  1342. chunk_offset = sector_div(x, sectors_per_chunk);
  1343. stripe = x;
  1344. BUG_ON(x != stripe);
  1345. first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
  1346. + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
  1347. sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
  1348. if (sh == NULL) {
  1349. sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
  1350. /* make sure we don't swamp the stripe cache if someone else
  1351. * is trying to get access
  1352. */
  1353. set_current_state(TASK_UNINTERRUPTIBLE);
  1354. schedule_timeout(1);
  1355. }
  1356. spin_lock(&sh->lock);
  1357. set_bit(STRIPE_SYNCING, &sh->state);
  1358. clear_bit(STRIPE_INSYNC, &sh->state);
  1359. spin_unlock(&sh->lock);
  1360. handle_stripe(sh);
  1361. release_stripe(sh);
  1362. return STRIPE_SECTORS;
  1363. }
  1364. /*
  1365. * This is our raid5 kernel thread.
  1366. *
  1367. * We scan the hash table for stripes which can be handled now.
  1368. * During the scan, completed stripes are saved for us by the interrupt
  1369. * handler, so that they will not have to wait for our next wakeup.
  1370. */
  1371. static void raid5d (mddev_t *mddev)
  1372. {
  1373. struct stripe_head *sh;
  1374. raid5_conf_t *conf = mddev_to_conf(mddev);
  1375. int handled;
  1376. PRINTK("+++ raid5d active\n");
  1377. md_check_recovery(mddev);
  1378. md_handle_safemode(mddev);
  1379. handled = 0;
  1380. spin_lock_irq(&conf->device_lock);
  1381. while (1) {
  1382. struct list_head *first;
  1383. if (list_empty(&conf->handle_list) &&
  1384. atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
  1385. !blk_queue_plugged(mddev->queue) &&
  1386. !list_empty(&conf->delayed_list))
  1387. raid5_activate_delayed(conf);
  1388. if (list_empty(&conf->handle_list))
  1389. break;
  1390. first = conf->handle_list.next;
  1391. sh = list_entry(first, struct stripe_head, lru);
  1392. list_del_init(first);
  1393. atomic_inc(&sh->count);
  1394. if (atomic_read(&sh->count)!= 1)
  1395. BUG();
  1396. spin_unlock_irq(&conf->device_lock);
  1397. handled++;
  1398. handle_stripe(sh);
  1399. release_stripe(sh);
  1400. spin_lock_irq(&conf->device_lock);
  1401. }
  1402. PRINTK("%d stripes handled\n", handled);
  1403. spin_unlock_irq(&conf->device_lock);
  1404. unplug_slaves(mddev);
  1405. PRINTK("--- raid5d inactive\n");
  1406. }
  1407. static int run (mddev_t *mddev)
  1408. {
  1409. raid5_conf_t *conf;
  1410. int raid_disk, memory;
  1411. mdk_rdev_t *rdev;
  1412. struct disk_info *disk;
  1413. struct list_head *tmp;
  1414. if (mddev->level != 5 && mddev->level != 4) {
  1415. printk("raid5: %s: raid level not set to 4/5 (%d)\n", mdname(mddev), mddev->level);
  1416. return -EIO;
  1417. }
  1418. mddev->private = kmalloc (sizeof (raid5_conf_t)
  1419. + mddev->raid_disks * sizeof(struct disk_info),
  1420. GFP_KERNEL);
  1421. if ((conf = mddev->private) == NULL)
  1422. goto abort;
  1423. memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
  1424. conf->mddev = mddev;
  1425. if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
  1426. goto abort;
  1427. memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
  1428. spin_lock_init(&conf->device_lock);
  1429. init_waitqueue_head(&conf->wait_for_stripe);
  1430. init_waitqueue_head(&conf->wait_for_overlap);
  1431. INIT_LIST_HEAD(&conf->handle_list);
  1432. INIT_LIST_HEAD(&conf->delayed_list);
  1433. INIT_LIST_HEAD(&conf->inactive_list);
  1434. atomic_set(&conf->active_stripes, 0);
  1435. atomic_set(&conf->preread_active_stripes, 0);
  1436. PRINTK("raid5: run(%s) called.\n", mdname(mddev));
  1437. ITERATE_RDEV(mddev,rdev,tmp) {
  1438. raid_disk = rdev->raid_disk;
  1439. if (raid_disk >= mddev->raid_disks
  1440. || raid_disk < 0)
  1441. continue;
  1442. disk = conf->disks + raid_disk;
  1443. disk->rdev = rdev;
  1444. if (rdev->in_sync) {
  1445. char b[BDEVNAME_SIZE];
  1446. printk(KERN_INFO "raid5: device %s operational as raid"
  1447. " disk %d\n", bdevname(rdev->bdev,b),
  1448. raid_disk);
  1449. conf->working_disks++;
  1450. }
  1451. }
  1452. conf->raid_disks = mddev->raid_disks;
  1453. /*
  1454. * 0 for a fully functional array, 1 for a degraded array.
  1455. */
  1456. mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
  1457. conf->mddev = mddev;
  1458. conf->chunk_size = mddev->chunk_size;
  1459. conf->level = mddev->level;
  1460. conf->algorithm = mddev->layout;
  1461. conf->max_nr_stripes = NR_STRIPES;
  1462. /* device size must be a multiple of chunk size */
  1463. mddev->size &= ~(mddev->chunk_size/1024 -1);
  1464. if (!conf->chunk_size || conf->chunk_size % 4) {
  1465. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  1466. conf->chunk_size, mdname(mddev));
  1467. goto abort;
  1468. }
  1469. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  1470. printk(KERN_ERR
  1471. "raid5: unsupported parity algorithm %d for %s\n",
  1472. conf->algorithm, mdname(mddev));
  1473. goto abort;
  1474. }
  1475. if (mddev->degraded > 1) {
  1476. printk(KERN_ERR "raid5: not enough operational devices for %s"
  1477. " (%d/%d failed)\n",
  1478. mdname(mddev), conf->failed_disks, conf->raid_disks);
  1479. goto abort;
  1480. }
  1481. if (mddev->degraded == 1 &&
  1482. mddev->recovery_cp != MaxSector) {
  1483. printk(KERN_ERR
  1484. "raid5: cannot start dirty degraded array for %s\n",
  1485. mdname(mddev));
  1486. goto abort;
  1487. }
  1488. {
  1489. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  1490. if (!mddev->thread) {
  1491. printk(KERN_ERR
  1492. "raid5: couldn't allocate thread for %s\n",
  1493. mdname(mddev));
  1494. goto abort;
  1495. }
  1496. }
  1497. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  1498. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  1499. if (grow_stripes(conf, conf->max_nr_stripes)) {
  1500. printk(KERN_ERR
  1501. "raid5: couldn't allocate %dkB for buffers\n", memory);
  1502. shrink_stripes(conf);
  1503. md_unregister_thread(mddev->thread);
  1504. goto abort;
  1505. } else
  1506. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  1507. memory, mdname(mddev));
  1508. if (mddev->degraded == 0)
  1509. printk("raid5: raid level %d set %s active with %d out of %d"
  1510. " devices, algorithm %d\n", conf->level, mdname(mddev),
  1511. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  1512. conf->algorithm);
  1513. else
  1514. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  1515. " out of %d devices, algorithm %d\n", conf->level,
  1516. mdname(mddev), mddev->raid_disks - mddev->degraded,
  1517. mddev->raid_disks, conf->algorithm);
  1518. print_raid5_conf(conf);
  1519. /* read-ahead size must cover two whole stripes, which is
  1520. * 2 * (n-1) * chunksize where 'n' is the number of raid devices
  1521. */
  1522. {
  1523. int stripe = (mddev->raid_disks-1) * mddev->chunk_size
  1524. / PAGE_CACHE_SIZE;
  1525. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  1526. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  1527. }
  1528. /* Ok, everything is just fine now */
  1529. mddev->queue->unplug_fn = raid5_unplug_device;
  1530. mddev->queue->issue_flush_fn = raid5_issue_flush;
  1531. mddev->array_size = mddev->size * (mddev->raid_disks - 1);
  1532. return 0;
  1533. abort:
  1534. if (conf) {
  1535. print_raid5_conf(conf);
  1536. if (conf->stripe_hashtbl)
  1537. free_pages((unsigned long) conf->stripe_hashtbl,
  1538. HASH_PAGES_ORDER);
  1539. kfree(conf);
  1540. }
  1541. mddev->private = NULL;
  1542. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  1543. return -EIO;
  1544. }
  1545. static int stop (mddev_t *mddev)
  1546. {
  1547. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1548. md_unregister_thread(mddev->thread);
  1549. mddev->thread = NULL;
  1550. shrink_stripes(conf);
  1551. free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
  1552. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  1553. kfree(conf);
  1554. mddev->private = NULL;
  1555. return 0;
  1556. }
  1557. #if RAID5_DEBUG
  1558. static void print_sh (struct stripe_head *sh)
  1559. {
  1560. int i;
  1561. printk("sh %llu, pd_idx %d, state %ld.\n",
  1562. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  1563. printk("sh %llu, count %d.\n",
  1564. (unsigned long long)sh->sector, atomic_read(&sh->count));
  1565. printk("sh %llu, ", (unsigned long long)sh->sector);
  1566. for (i = 0; i < sh->raid_conf->raid_disks; i++) {
  1567. printk("(cache%d: %p %ld) ",
  1568. i, sh->dev[i].page, sh->dev[i].flags);
  1569. }
  1570. printk("\n");
  1571. }
  1572. static void printall (raid5_conf_t *conf)
  1573. {
  1574. struct stripe_head *sh;
  1575. int i;
  1576. spin_lock_irq(&conf->device_lock);
  1577. for (i = 0; i < NR_HASH; i++) {
  1578. sh = conf->stripe_hashtbl[i];
  1579. for (; sh; sh = sh->hash_next) {
  1580. if (sh->raid_conf != conf)
  1581. continue;
  1582. print_sh(sh);
  1583. }
  1584. }
  1585. spin_unlock_irq(&conf->device_lock);
  1586. }
  1587. #endif
  1588. static void status (struct seq_file *seq, mddev_t *mddev)
  1589. {
  1590. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1591. int i;
  1592. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  1593. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
  1594. for (i = 0; i < conf->raid_disks; i++)
  1595. seq_printf (seq, "%s",
  1596. conf->disks[i].rdev &&
  1597. conf->disks[i].rdev->in_sync ? "U" : "_");
  1598. seq_printf (seq, "]");
  1599. #if RAID5_DEBUG
  1600. #define D(x) \
  1601. seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
  1602. printall(conf);
  1603. #endif
  1604. }
  1605. static void print_raid5_conf (raid5_conf_t *conf)
  1606. {
  1607. int i;
  1608. struct disk_info *tmp;
  1609. printk("RAID5 conf printout:\n");
  1610. if (!conf) {
  1611. printk("(conf==NULL)\n");
  1612. return;
  1613. }
  1614. printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
  1615. conf->working_disks, conf->failed_disks);
  1616. for (i = 0; i < conf->raid_disks; i++) {
  1617. char b[BDEVNAME_SIZE];
  1618. tmp = conf->disks + i;
  1619. if (tmp->rdev)
  1620. printk(" disk %d, o:%d, dev:%s\n",
  1621. i, !tmp->rdev->faulty,
  1622. bdevname(tmp->rdev->bdev,b));
  1623. }
  1624. }
  1625. static int raid5_spare_active(mddev_t *mddev)
  1626. {
  1627. int i;
  1628. raid5_conf_t *conf = mddev->private;
  1629. struct disk_info *tmp;
  1630. for (i = 0; i < conf->raid_disks; i++) {
  1631. tmp = conf->disks + i;
  1632. if (tmp->rdev
  1633. && !tmp->rdev->faulty
  1634. && !tmp->rdev->in_sync) {
  1635. mddev->degraded--;
  1636. conf->failed_disks--;
  1637. conf->working_disks++;
  1638. tmp->rdev->in_sync = 1;
  1639. }
  1640. }
  1641. print_raid5_conf(conf);
  1642. return 0;
  1643. }
  1644. static int raid5_remove_disk(mddev_t *mddev, int number)
  1645. {
  1646. raid5_conf_t *conf = mddev->private;
  1647. int err = 0;
  1648. mdk_rdev_t *rdev;
  1649. struct disk_info *p = conf->disks + number;
  1650. print_raid5_conf(conf);
  1651. rdev = p->rdev;
  1652. if (rdev) {
  1653. if (rdev->in_sync ||
  1654. atomic_read(&rdev->nr_pending)) {
  1655. err = -EBUSY;
  1656. goto abort;
  1657. }
  1658. p->rdev = NULL;
  1659. synchronize_rcu();
  1660. if (atomic_read(&rdev->nr_pending)) {
  1661. /* lost the race, try later */
  1662. err = -EBUSY;
  1663. p->rdev = rdev;
  1664. }
  1665. }
  1666. abort:
  1667. print_raid5_conf(conf);
  1668. return err;
  1669. }
  1670. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  1671. {
  1672. raid5_conf_t *conf = mddev->private;
  1673. int found = 0;
  1674. int disk;
  1675. struct disk_info *p;
  1676. if (mddev->degraded > 1)
  1677. /* no point adding a device */
  1678. return 0;
  1679. /*
  1680. * find the disk ...
  1681. */
  1682. for (disk=0; disk < mddev->raid_disks; disk++)
  1683. if ((p=conf->disks + disk)->rdev == NULL) {
  1684. rdev->in_sync = 0;
  1685. rdev->raid_disk = disk;
  1686. found = 1;
  1687. p->rdev = rdev;
  1688. break;
  1689. }
  1690. print_raid5_conf(conf);
  1691. return found;
  1692. }
  1693. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  1694. {
  1695. /* no resync is happening, and there is enough space
  1696. * on all devices, so we can resize.
  1697. * We need to make sure resync covers any new space.
  1698. * If the array is shrinking we should possibly wait until
  1699. * any io in the removed space completes, but it hardly seems
  1700. * worth it.
  1701. */
  1702. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  1703. mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
  1704. set_capacity(mddev->gendisk, mddev->array_size << 1);
  1705. mddev->changed = 1;
  1706. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  1707. mddev->recovery_cp = mddev->size << 1;
  1708. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1709. }
  1710. mddev->size = sectors /2;
  1711. return 0;
  1712. }
  1713. static mdk_personality_t raid5_personality=
  1714. {
  1715. .name = "raid5",
  1716. .owner = THIS_MODULE,
  1717. .make_request = make_request,
  1718. .run = run,
  1719. .stop = stop,
  1720. .status = status,
  1721. .error_handler = error,
  1722. .hot_add_disk = raid5_add_disk,
  1723. .hot_remove_disk= raid5_remove_disk,
  1724. .spare_active = raid5_spare_active,
  1725. .sync_request = sync_request,
  1726. .resize = raid5_resize,
  1727. };
  1728. static int __init raid5_init (void)
  1729. {
  1730. return register_md_personality (RAID5, &raid5_personality);
  1731. }
  1732. static void raid5_exit (void)
  1733. {
  1734. unregister_md_personality (RAID5);
  1735. }
  1736. module_init(raid5_init);
  1737. module_exit(raid5_exit);
  1738. MODULE_LICENSE("GPL");
  1739. MODULE_ALIAS("md-personality-4"); /* RAID5 */