raid5.c 60 KB

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