raid5.c 55 KB

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