raid5.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  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. if (!conf->chunk_size || conf->chunk_size % 4) {
  1463. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  1464. conf->chunk_size, mdname(mddev));
  1465. goto abort;
  1466. }
  1467. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  1468. printk(KERN_ERR
  1469. "raid5: unsupported parity algorithm %d for %s\n",
  1470. conf->algorithm, mdname(mddev));
  1471. goto abort;
  1472. }
  1473. if (mddev->degraded > 1) {
  1474. printk(KERN_ERR "raid5: not enough operational devices for %s"
  1475. " (%d/%d failed)\n",
  1476. mdname(mddev), conf->failed_disks, conf->raid_disks);
  1477. goto abort;
  1478. }
  1479. if (mddev->degraded == 1 &&
  1480. mddev->recovery_cp != MaxSector) {
  1481. printk(KERN_ERR
  1482. "raid5: cannot start dirty degraded array for %s\n",
  1483. mdname(mddev));
  1484. goto abort;
  1485. }
  1486. {
  1487. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  1488. if (!mddev->thread) {
  1489. printk(KERN_ERR
  1490. "raid5: couldn't allocate thread for %s\n",
  1491. mdname(mddev));
  1492. goto abort;
  1493. }
  1494. }
  1495. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  1496. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  1497. if (grow_stripes(conf, conf->max_nr_stripes)) {
  1498. printk(KERN_ERR
  1499. "raid5: couldn't allocate %dkB for buffers\n", memory);
  1500. shrink_stripes(conf);
  1501. md_unregister_thread(mddev->thread);
  1502. goto abort;
  1503. } else
  1504. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  1505. memory, mdname(mddev));
  1506. if (mddev->degraded == 0)
  1507. printk("raid5: raid level %d set %s active with %d out of %d"
  1508. " devices, algorithm %d\n", conf->level, mdname(mddev),
  1509. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  1510. conf->algorithm);
  1511. else
  1512. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  1513. " out of %d devices, algorithm %d\n", conf->level,
  1514. mdname(mddev), mddev->raid_disks - mddev->degraded,
  1515. mddev->raid_disks, conf->algorithm);
  1516. print_raid5_conf(conf);
  1517. /* read-ahead size must cover two whole stripes, which is
  1518. * 2 * (n-1) * chunksize where 'n' is the number of raid devices
  1519. */
  1520. {
  1521. int stripe = (mddev->raid_disks-1) * mddev->chunk_size
  1522. / PAGE_CACHE_SIZE;
  1523. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  1524. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  1525. }
  1526. /* Ok, everything is just fine now */
  1527. mddev->queue->unplug_fn = raid5_unplug_device;
  1528. mddev->queue->issue_flush_fn = raid5_issue_flush;
  1529. mddev->array_size = mddev->size * (mddev->raid_disks - 1);
  1530. return 0;
  1531. abort:
  1532. if (conf) {
  1533. print_raid5_conf(conf);
  1534. if (conf->stripe_hashtbl)
  1535. free_pages((unsigned long) conf->stripe_hashtbl,
  1536. HASH_PAGES_ORDER);
  1537. kfree(conf);
  1538. }
  1539. mddev->private = NULL;
  1540. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  1541. return -EIO;
  1542. }
  1543. static int stop (mddev_t *mddev)
  1544. {
  1545. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1546. md_unregister_thread(mddev->thread);
  1547. mddev->thread = NULL;
  1548. shrink_stripes(conf);
  1549. free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
  1550. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  1551. kfree(conf);
  1552. mddev->private = NULL;
  1553. return 0;
  1554. }
  1555. #if RAID5_DEBUG
  1556. static void print_sh (struct stripe_head *sh)
  1557. {
  1558. int i;
  1559. printk("sh %llu, pd_idx %d, state %ld.\n",
  1560. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  1561. printk("sh %llu, count %d.\n",
  1562. (unsigned long long)sh->sector, atomic_read(&sh->count));
  1563. printk("sh %llu, ", (unsigned long long)sh->sector);
  1564. for (i = 0; i < sh->raid_conf->raid_disks; i++) {
  1565. printk("(cache%d: %p %ld) ",
  1566. i, sh->dev[i].page, sh->dev[i].flags);
  1567. }
  1568. printk("\n");
  1569. }
  1570. static void printall (raid5_conf_t *conf)
  1571. {
  1572. struct stripe_head *sh;
  1573. int i;
  1574. spin_lock_irq(&conf->device_lock);
  1575. for (i = 0; i < NR_HASH; i++) {
  1576. sh = conf->stripe_hashtbl[i];
  1577. for (; sh; sh = sh->hash_next) {
  1578. if (sh->raid_conf != conf)
  1579. continue;
  1580. print_sh(sh);
  1581. }
  1582. }
  1583. spin_unlock_irq(&conf->device_lock);
  1584. }
  1585. #endif
  1586. static void status (struct seq_file *seq, mddev_t *mddev)
  1587. {
  1588. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1589. int i;
  1590. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  1591. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
  1592. for (i = 0; i < conf->raid_disks; i++)
  1593. seq_printf (seq, "%s",
  1594. conf->disks[i].rdev &&
  1595. conf->disks[i].rdev->in_sync ? "U" : "_");
  1596. seq_printf (seq, "]");
  1597. #if RAID5_DEBUG
  1598. #define D(x) \
  1599. seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
  1600. printall(conf);
  1601. #endif
  1602. }
  1603. static void print_raid5_conf (raid5_conf_t *conf)
  1604. {
  1605. int i;
  1606. struct disk_info *tmp;
  1607. printk("RAID5 conf printout:\n");
  1608. if (!conf) {
  1609. printk("(conf==NULL)\n");
  1610. return;
  1611. }
  1612. printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
  1613. conf->working_disks, conf->failed_disks);
  1614. for (i = 0; i < conf->raid_disks; i++) {
  1615. char b[BDEVNAME_SIZE];
  1616. tmp = conf->disks + i;
  1617. if (tmp->rdev)
  1618. printk(" disk %d, o:%d, dev:%s\n",
  1619. i, !tmp->rdev->faulty,
  1620. bdevname(tmp->rdev->bdev,b));
  1621. }
  1622. }
  1623. static int raid5_spare_active(mddev_t *mddev)
  1624. {
  1625. int i;
  1626. raid5_conf_t *conf = mddev->private;
  1627. struct disk_info *tmp;
  1628. for (i = 0; i < conf->raid_disks; i++) {
  1629. tmp = conf->disks + i;
  1630. if (tmp->rdev
  1631. && !tmp->rdev->faulty
  1632. && !tmp->rdev->in_sync) {
  1633. mddev->degraded--;
  1634. conf->failed_disks--;
  1635. conf->working_disks++;
  1636. tmp->rdev->in_sync = 1;
  1637. }
  1638. }
  1639. print_raid5_conf(conf);
  1640. return 0;
  1641. }
  1642. static int raid5_remove_disk(mddev_t *mddev, int number)
  1643. {
  1644. raid5_conf_t *conf = mddev->private;
  1645. int err = 0;
  1646. mdk_rdev_t *rdev;
  1647. struct disk_info *p = conf->disks + number;
  1648. print_raid5_conf(conf);
  1649. rdev = p->rdev;
  1650. if (rdev) {
  1651. if (rdev->in_sync ||
  1652. atomic_read(&rdev->nr_pending)) {
  1653. err = -EBUSY;
  1654. goto abort;
  1655. }
  1656. p->rdev = NULL;
  1657. synchronize_rcu();
  1658. if (atomic_read(&rdev->nr_pending)) {
  1659. /* lost the race, try later */
  1660. err = -EBUSY;
  1661. p->rdev = rdev;
  1662. }
  1663. }
  1664. abort:
  1665. print_raid5_conf(conf);
  1666. return err;
  1667. }
  1668. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  1669. {
  1670. raid5_conf_t *conf = mddev->private;
  1671. int found = 0;
  1672. int disk;
  1673. struct disk_info *p;
  1674. if (mddev->degraded > 1)
  1675. /* no point adding a device */
  1676. return 0;
  1677. /*
  1678. * find the disk ...
  1679. */
  1680. for (disk=0; disk < mddev->raid_disks; disk++)
  1681. if ((p=conf->disks + disk)->rdev == NULL) {
  1682. rdev->in_sync = 0;
  1683. rdev->raid_disk = disk;
  1684. found = 1;
  1685. p->rdev = rdev;
  1686. break;
  1687. }
  1688. print_raid5_conf(conf);
  1689. return found;
  1690. }
  1691. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  1692. {
  1693. /* no resync is happening, and there is enough space
  1694. * on all devices, so we can resize.
  1695. * We need to make sure resync covers any new space.
  1696. * If the array is shrinking we should possibly wait until
  1697. * any io in the removed space completes, but it hardly seems
  1698. * worth it.
  1699. */
  1700. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  1701. mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
  1702. set_capacity(mddev->gendisk, mddev->array_size << 1);
  1703. mddev->changed = 1;
  1704. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  1705. mddev->recovery_cp = mddev->size << 1;
  1706. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  1707. }
  1708. mddev->size = sectors /2;
  1709. mddev->resync_max_sectors = sectors;
  1710. return 0;
  1711. }
  1712. static mdk_personality_t raid5_personality=
  1713. {
  1714. .name = "raid5",
  1715. .owner = THIS_MODULE,
  1716. .make_request = make_request,
  1717. .run = run,
  1718. .stop = stop,
  1719. .status = status,
  1720. .error_handler = error,
  1721. .hot_add_disk = raid5_add_disk,
  1722. .hot_remove_disk= raid5_remove_disk,
  1723. .spare_active = raid5_spare_active,
  1724. .sync_request = sync_request,
  1725. .resize = raid5_resize,
  1726. };
  1727. static int __init raid5_init (void)
  1728. {
  1729. return register_md_personality (RAID5, &raid5_personality);
  1730. }
  1731. static void raid5_exit (void)
  1732. {
  1733. unregister_md_personality (RAID5);
  1734. }
  1735. module_init(raid5_init);
  1736. module_exit(raid5_exit);
  1737. MODULE_LICENSE("GPL");
  1738. MODULE_ALIAS("md-personality-4"); /* RAID5 */