raid5.c 60 KB

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