raid6main.c 66 KB

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