raid5.c 52 KB

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