raid6main.c 64 KB

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