raid5.c 60 KB

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