raid6main.c 66 KB

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