raid6main.c 58 KB

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