raid5.c 60 KB

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