raid5.c 60 KB

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