raid6main.c 65 KB

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