raid6main.c 58 KB

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