raid5.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798
  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 <linux/kthread.h>
  24. #include <asm/atomic.h>
  25. #include <linux/raid/bitmap.h>
  26. /*
  27. * Stripe cache
  28. */
  29. #define NR_STRIPES 256
  30. #define STRIPE_SIZE PAGE_SIZE
  31. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  32. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  33. #define IO_THRESHOLD 1
  34. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_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 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 if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  74. conf->seq_write == sh->bm_seq)
  75. list_add_tail(&sh->lru, &conf->bitmap_list);
  76. else {
  77. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  78. list_add_tail(&sh->lru, &conf->handle_list);
  79. }
  80. md_wakeup_thread(conf->mddev->thread);
  81. } else {
  82. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  83. atomic_dec(&conf->preread_active_stripes);
  84. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  85. md_wakeup_thread(conf->mddev->thread);
  86. }
  87. atomic_dec(&conf->active_stripes);
  88. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  89. list_add_tail(&sh->lru, &conf->inactive_list);
  90. wake_up(&conf->wait_for_stripe);
  91. }
  92. }
  93. }
  94. }
  95. static void release_stripe(struct stripe_head *sh)
  96. {
  97. raid5_conf_t *conf = sh->raid_conf;
  98. unsigned long flags;
  99. spin_lock_irqsave(&conf->device_lock, flags);
  100. __release_stripe(conf, sh);
  101. spin_unlock_irqrestore(&conf->device_lock, flags);
  102. }
  103. static inline void remove_hash(struct stripe_head *sh)
  104. {
  105. PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  106. hlist_del_init(&sh->hash);
  107. }
  108. static void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  109. {
  110. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  111. PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  112. CHECK_DEVLOCK();
  113. hlist_add_head(&sh->hash, hp);
  114. }
  115. /* find an idle stripe, make sure it is unhashed, and return it. */
  116. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  117. {
  118. struct stripe_head *sh = NULL;
  119. struct list_head *first;
  120. CHECK_DEVLOCK();
  121. if (list_empty(&conf->inactive_list))
  122. goto out;
  123. first = conf->inactive_list.next;
  124. sh = list_entry(first, struct stripe_head, lru);
  125. list_del_init(first);
  126. remove_hash(sh);
  127. atomic_inc(&conf->active_stripes);
  128. out:
  129. return sh;
  130. }
  131. static void shrink_buffers(struct stripe_head *sh, int num)
  132. {
  133. struct page *p;
  134. int i;
  135. for (i=0; i<num ; i++) {
  136. p = sh->dev[i].page;
  137. if (!p)
  138. continue;
  139. sh->dev[i].page = NULL;
  140. put_page(p);
  141. }
  142. }
  143. static int grow_buffers(struct stripe_head *sh, int num)
  144. {
  145. int i;
  146. for (i=0; i<num; i++) {
  147. struct page *page;
  148. if (!(page = alloc_page(GFP_KERNEL))) {
  149. return 1;
  150. }
  151. sh->dev[i].page = page;
  152. }
  153. return 0;
  154. }
  155. static void raid5_build_block (struct stripe_head *sh, int i);
  156. static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
  157. {
  158. raid5_conf_t *conf = sh->raid_conf;
  159. int i;
  160. if (atomic_read(&sh->count) != 0)
  161. BUG();
  162. if (test_bit(STRIPE_HANDLE, &sh->state))
  163. BUG();
  164. CHECK_DEVLOCK();
  165. PRINTK("init_stripe called, stripe %llu\n",
  166. (unsigned long long)sh->sector);
  167. remove_hash(sh);
  168. sh->sector = sector;
  169. sh->pd_idx = pd_idx;
  170. sh->state = 0;
  171. sh->disks = disks;
  172. for (i = sh->disks; i--; ) {
  173. struct r5dev *dev = &sh->dev[i];
  174. if (dev->toread || dev->towrite || dev->written ||
  175. test_bit(R5_LOCKED, &dev->flags)) {
  176. printk("sector=%llx i=%d %p %p %p %d\n",
  177. (unsigned long long)sh->sector, i, dev->toread,
  178. dev->towrite, dev->written,
  179. test_bit(R5_LOCKED, &dev->flags));
  180. BUG();
  181. }
  182. dev->flags = 0;
  183. raid5_build_block(sh, i);
  184. }
  185. insert_hash(conf, sh);
  186. }
  187. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
  188. {
  189. struct stripe_head *sh;
  190. struct hlist_node *hn;
  191. CHECK_DEVLOCK();
  192. PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
  193. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  194. if (sh->sector == sector && sh->disks == disks)
  195. return sh;
  196. PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
  197. return NULL;
  198. }
  199. static void unplug_slaves(mddev_t *mddev);
  200. static void raid5_unplug_device(request_queue_t *q);
  201. static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
  202. int pd_idx, int noblock)
  203. {
  204. struct stripe_head *sh;
  205. PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
  206. spin_lock_irq(&conf->device_lock);
  207. do {
  208. wait_event_lock_irq(conf->wait_for_stripe,
  209. conf->quiesce == 0,
  210. conf->device_lock, /* nothing */);
  211. sh = __find_stripe(conf, sector, disks);
  212. if (!sh) {
  213. if (!conf->inactive_blocked)
  214. sh = get_free_stripe(conf);
  215. if (noblock && sh == NULL)
  216. break;
  217. if (!sh) {
  218. conf->inactive_blocked = 1;
  219. wait_event_lock_irq(conf->wait_for_stripe,
  220. !list_empty(&conf->inactive_list) &&
  221. (atomic_read(&conf->active_stripes)
  222. < (conf->max_nr_stripes *3/4)
  223. || !conf->inactive_blocked),
  224. conf->device_lock,
  225. unplug_slaves(conf->mddev);
  226. );
  227. conf->inactive_blocked = 0;
  228. } else
  229. init_stripe(sh, sector, pd_idx, disks);
  230. } else {
  231. if (atomic_read(&sh->count)) {
  232. if (!list_empty(&sh->lru))
  233. BUG();
  234. } else {
  235. if (!test_bit(STRIPE_HANDLE, &sh->state))
  236. atomic_inc(&conf->active_stripes);
  237. if (!list_empty(&sh->lru))
  238. list_del_init(&sh->lru);
  239. }
  240. }
  241. } while (sh == NULL);
  242. if (sh)
  243. atomic_inc(&sh->count);
  244. spin_unlock_irq(&conf->device_lock);
  245. return sh;
  246. }
  247. static int grow_one_stripe(raid5_conf_t *conf)
  248. {
  249. struct stripe_head *sh;
  250. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  251. if (!sh)
  252. return 0;
  253. memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
  254. sh->raid_conf = conf;
  255. spin_lock_init(&sh->lock);
  256. if (grow_buffers(sh, conf->raid_disks)) {
  257. shrink_buffers(sh, conf->raid_disks);
  258. kmem_cache_free(conf->slab_cache, sh);
  259. return 0;
  260. }
  261. sh->disks = conf->raid_disks;
  262. /* we just created an active stripe so... */
  263. atomic_set(&sh->count, 1);
  264. atomic_inc(&conf->active_stripes);
  265. INIT_LIST_HEAD(&sh->lru);
  266. release_stripe(sh);
  267. return 1;
  268. }
  269. static int grow_stripes(raid5_conf_t *conf, int num)
  270. {
  271. kmem_cache_t *sc;
  272. int devs = conf->raid_disks;
  273. sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
  274. sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
  275. conf->active_name = 0;
  276. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  277. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  278. 0, 0, NULL, NULL);
  279. if (!sc)
  280. return 1;
  281. conf->slab_cache = sc;
  282. conf->pool_size = devs;
  283. while (num--) {
  284. if (!grow_one_stripe(conf))
  285. return 1;
  286. }
  287. return 0;
  288. }
  289. #ifdef CONFIG_MD_RAID5_RESHAPE
  290. static int resize_stripes(raid5_conf_t *conf, int newsize)
  291. {
  292. /* Make all the stripes able to hold 'newsize' devices.
  293. * New slots in each stripe get 'page' set to a new page.
  294. *
  295. * This happens in stages:
  296. * 1/ create a new kmem_cache and allocate the required number of
  297. * stripe_heads.
  298. * 2/ gather all the old stripe_heads and tranfer the pages across
  299. * to the new stripe_heads. This will have the side effect of
  300. * freezing the array as once all stripe_heads have been collected,
  301. * no IO will be possible. Old stripe heads are freed once their
  302. * pages have been transferred over, and the old kmem_cache is
  303. * freed when all stripes are done.
  304. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  305. * we simple return a failre status - no need to clean anything up.
  306. * 4/ allocate new pages for the new slots in the new stripe_heads.
  307. * If this fails, we don't bother trying the shrink the
  308. * stripe_heads down again, we just leave them as they are.
  309. * As each stripe_head is processed the new one is released into
  310. * active service.
  311. *
  312. * Once step2 is started, we cannot afford to wait for a write,
  313. * so we use GFP_NOIO allocations.
  314. */
  315. struct stripe_head *osh, *nsh;
  316. LIST_HEAD(newstripes);
  317. struct disk_info *ndisks;
  318. int err = 0;
  319. kmem_cache_t *sc;
  320. int i;
  321. if (newsize <= conf->pool_size)
  322. return 0; /* never bother to shrink */
  323. /* Step 1 */
  324. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  325. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  326. 0, 0, NULL, NULL);
  327. if (!sc)
  328. return -ENOMEM;
  329. for (i = conf->max_nr_stripes; i; i--) {
  330. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  331. if (!nsh)
  332. break;
  333. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  334. nsh->raid_conf = conf;
  335. spin_lock_init(&nsh->lock);
  336. list_add(&nsh->lru, &newstripes);
  337. }
  338. if (i) {
  339. /* didn't get enough, give up */
  340. while (!list_empty(&newstripes)) {
  341. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  342. list_del(&nsh->lru);
  343. kmem_cache_free(sc, nsh);
  344. }
  345. kmem_cache_destroy(sc);
  346. return -ENOMEM;
  347. }
  348. /* Step 2 - Must use GFP_NOIO now.
  349. * OK, we have enough stripes, start collecting inactive
  350. * stripes and copying them over
  351. */
  352. list_for_each_entry(nsh, &newstripes, lru) {
  353. spin_lock_irq(&conf->device_lock);
  354. wait_event_lock_irq(conf->wait_for_stripe,
  355. !list_empty(&conf->inactive_list),
  356. conf->device_lock,
  357. unplug_slaves(conf->mddev);
  358. );
  359. osh = get_free_stripe(conf);
  360. spin_unlock_irq(&conf->device_lock);
  361. atomic_set(&nsh->count, 1);
  362. for(i=0; i<conf->pool_size; i++)
  363. nsh->dev[i].page = osh->dev[i].page;
  364. for( ; i<newsize; i++)
  365. nsh->dev[i].page = NULL;
  366. kmem_cache_free(conf->slab_cache, osh);
  367. }
  368. kmem_cache_destroy(conf->slab_cache);
  369. /* Step 3.
  370. * At this point, we are holding all the stripes so the array
  371. * is completely stalled, so now is a good time to resize
  372. * conf->disks.
  373. */
  374. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  375. if (ndisks) {
  376. for (i=0; i<conf->raid_disks; i++)
  377. ndisks[i] = conf->disks[i];
  378. kfree(conf->disks);
  379. conf->disks = ndisks;
  380. } else
  381. err = -ENOMEM;
  382. /* Step 4, return new stripes to service */
  383. while(!list_empty(&newstripes)) {
  384. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  385. list_del_init(&nsh->lru);
  386. for (i=conf->raid_disks; i < newsize; i++)
  387. if (nsh->dev[i].page == NULL) {
  388. struct page *p = alloc_page(GFP_NOIO);
  389. nsh->dev[i].page = p;
  390. if (!p)
  391. err = -ENOMEM;
  392. }
  393. release_stripe(nsh);
  394. }
  395. /* critical section pass, GFP_NOIO no longer needed */
  396. conf->slab_cache = sc;
  397. conf->active_name = 1-conf->active_name;
  398. conf->pool_size = newsize;
  399. return err;
  400. }
  401. #endif
  402. static int drop_one_stripe(raid5_conf_t *conf)
  403. {
  404. struct stripe_head *sh;
  405. spin_lock_irq(&conf->device_lock);
  406. sh = get_free_stripe(conf);
  407. spin_unlock_irq(&conf->device_lock);
  408. if (!sh)
  409. return 0;
  410. if (atomic_read(&sh->count))
  411. BUG();
  412. shrink_buffers(sh, conf->pool_size);
  413. kmem_cache_free(conf->slab_cache, sh);
  414. atomic_dec(&conf->active_stripes);
  415. return 1;
  416. }
  417. static void shrink_stripes(raid5_conf_t *conf)
  418. {
  419. while (drop_one_stripe(conf))
  420. ;
  421. if (conf->slab_cache)
  422. kmem_cache_destroy(conf->slab_cache);
  423. conf->slab_cache = NULL;
  424. }
  425. static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
  426. int error)
  427. {
  428. struct stripe_head *sh = bi->bi_private;
  429. raid5_conf_t *conf = sh->raid_conf;
  430. int disks = sh->disks, i;
  431. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  432. if (bi->bi_size)
  433. return 1;
  434. for (i=0 ; i<disks; i++)
  435. if (bi == &sh->dev[i].req)
  436. break;
  437. PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  438. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  439. uptodate);
  440. if (i == disks) {
  441. BUG();
  442. return 0;
  443. }
  444. if (uptodate) {
  445. #if 0
  446. struct bio *bio;
  447. unsigned long flags;
  448. spin_lock_irqsave(&conf->device_lock, flags);
  449. /* we can return a buffer if we bypassed the cache or
  450. * if the top buffer is not in highmem. If there are
  451. * multiple buffers, leave the extra work to
  452. * handle_stripe
  453. */
  454. buffer = sh->bh_read[i];
  455. if (buffer &&
  456. (!PageHighMem(buffer->b_page)
  457. || buffer->b_page == bh->b_page )
  458. ) {
  459. sh->bh_read[i] = buffer->b_reqnext;
  460. buffer->b_reqnext = NULL;
  461. } else
  462. buffer = NULL;
  463. spin_unlock_irqrestore(&conf->device_lock, flags);
  464. if (sh->bh_page[i]==bh->b_page)
  465. set_buffer_uptodate(bh);
  466. if (buffer) {
  467. if (buffer->b_page != bh->b_page)
  468. memcpy(buffer->b_data, bh->b_data, bh->b_size);
  469. buffer->b_end_io(buffer, 1);
  470. }
  471. #else
  472. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  473. #endif
  474. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  475. printk(KERN_INFO "raid5: read error corrected!!\n");
  476. clear_bit(R5_ReadError, &sh->dev[i].flags);
  477. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  478. }
  479. if (atomic_read(&conf->disks[i].rdev->read_errors))
  480. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  481. } else {
  482. int retry = 0;
  483. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  484. atomic_inc(&conf->disks[i].rdev->read_errors);
  485. if (conf->mddev->degraded)
  486. printk(KERN_WARNING "raid5: read error not correctable.\n");
  487. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  488. /* Oh, no!!! */
  489. printk(KERN_WARNING "raid5: read error NOT corrected!!\n");
  490. else if (atomic_read(&conf->disks[i].rdev->read_errors)
  491. > conf->max_nr_stripes)
  492. printk(KERN_WARNING
  493. "raid5: Too many read errors, failing device.\n");
  494. else
  495. retry = 1;
  496. if (retry)
  497. set_bit(R5_ReadError, &sh->dev[i].flags);
  498. else {
  499. clear_bit(R5_ReadError, &sh->dev[i].flags);
  500. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  501. md_error(conf->mddev, conf->disks[i].rdev);
  502. }
  503. }
  504. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  505. #if 0
  506. /* must restore b_page before unlocking buffer... */
  507. if (sh->bh_page[i] != bh->b_page) {
  508. bh->b_page = sh->bh_page[i];
  509. bh->b_data = page_address(bh->b_page);
  510. clear_buffer_uptodate(bh);
  511. }
  512. #endif
  513. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  514. set_bit(STRIPE_HANDLE, &sh->state);
  515. release_stripe(sh);
  516. return 0;
  517. }
  518. static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
  519. int error)
  520. {
  521. struct stripe_head *sh = bi->bi_private;
  522. raid5_conf_t *conf = sh->raid_conf;
  523. int disks = sh->disks, i;
  524. unsigned long flags;
  525. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  526. if (bi->bi_size)
  527. return 1;
  528. for (i=0 ; i<disks; i++)
  529. if (bi == &sh->dev[i].req)
  530. break;
  531. PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  532. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  533. uptodate);
  534. if (i == disks) {
  535. BUG();
  536. return 0;
  537. }
  538. spin_lock_irqsave(&conf->device_lock, flags);
  539. if (!uptodate)
  540. md_error(conf->mddev, conf->disks[i].rdev);
  541. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  542. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  543. set_bit(STRIPE_HANDLE, &sh->state);
  544. __release_stripe(conf, sh);
  545. spin_unlock_irqrestore(&conf->device_lock, flags);
  546. return 0;
  547. }
  548. static sector_t compute_blocknr(struct stripe_head *sh, int i);
  549. static void raid5_build_block (struct stripe_head *sh, int i)
  550. {
  551. struct r5dev *dev = &sh->dev[i];
  552. bio_init(&dev->req);
  553. dev->req.bi_io_vec = &dev->vec;
  554. dev->req.bi_vcnt++;
  555. dev->req.bi_max_vecs++;
  556. dev->vec.bv_page = dev->page;
  557. dev->vec.bv_len = STRIPE_SIZE;
  558. dev->vec.bv_offset = 0;
  559. dev->req.bi_sector = sh->sector;
  560. dev->req.bi_private = sh;
  561. dev->flags = 0;
  562. if (i != sh->pd_idx)
  563. dev->sector = compute_blocknr(sh, i);
  564. }
  565. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  566. {
  567. char b[BDEVNAME_SIZE];
  568. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  569. PRINTK("raid5: error called\n");
  570. if (!test_bit(Faulty, &rdev->flags)) {
  571. mddev->sb_dirty = 1;
  572. if (test_bit(In_sync, &rdev->flags)) {
  573. conf->working_disks--;
  574. mddev->degraded++;
  575. conf->failed_disks++;
  576. clear_bit(In_sync, &rdev->flags);
  577. /*
  578. * if recovery was running, make sure it aborts.
  579. */
  580. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  581. }
  582. set_bit(Faulty, &rdev->flags);
  583. printk (KERN_ALERT
  584. "raid5: Disk failure on %s, disabling device."
  585. " Operation continuing on %d devices\n",
  586. bdevname(rdev->bdev,b), conf->working_disks);
  587. }
  588. }
  589. /*
  590. * Input: a 'big' sector number,
  591. * Output: index of the data and parity disk, and the sector # in them.
  592. */
  593. static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
  594. unsigned int data_disks, unsigned int * dd_idx,
  595. unsigned int * pd_idx, raid5_conf_t *conf)
  596. {
  597. long stripe;
  598. unsigned long chunk_number;
  599. unsigned int chunk_offset;
  600. sector_t new_sector;
  601. int sectors_per_chunk = conf->chunk_size >> 9;
  602. /* First compute the information on this sector */
  603. /*
  604. * Compute the chunk number and the sector offset inside the chunk
  605. */
  606. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  607. chunk_number = r_sector;
  608. BUG_ON(r_sector != chunk_number);
  609. /*
  610. * Compute the stripe number
  611. */
  612. stripe = chunk_number / data_disks;
  613. /*
  614. * Compute the data disk and parity disk indexes inside the stripe
  615. */
  616. *dd_idx = chunk_number % data_disks;
  617. /*
  618. * Select the parity disk based on the user selected algorithm.
  619. */
  620. if (conf->level == 4)
  621. *pd_idx = data_disks;
  622. else switch (conf->algorithm) {
  623. case ALGORITHM_LEFT_ASYMMETRIC:
  624. *pd_idx = data_disks - stripe % raid_disks;
  625. if (*dd_idx >= *pd_idx)
  626. (*dd_idx)++;
  627. break;
  628. case ALGORITHM_RIGHT_ASYMMETRIC:
  629. *pd_idx = stripe % raid_disks;
  630. if (*dd_idx >= *pd_idx)
  631. (*dd_idx)++;
  632. break;
  633. case ALGORITHM_LEFT_SYMMETRIC:
  634. *pd_idx = data_disks - stripe % raid_disks;
  635. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  636. break;
  637. case ALGORITHM_RIGHT_SYMMETRIC:
  638. *pd_idx = stripe % raid_disks;
  639. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  640. break;
  641. default:
  642. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  643. conf->algorithm);
  644. }
  645. /*
  646. * Finally, compute the new sector number
  647. */
  648. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  649. return new_sector;
  650. }
  651. static sector_t compute_blocknr(struct stripe_head *sh, int i)
  652. {
  653. raid5_conf_t *conf = sh->raid_conf;
  654. int raid_disks = sh->disks, data_disks = raid_disks - 1;
  655. sector_t new_sector = sh->sector, check;
  656. int sectors_per_chunk = conf->chunk_size >> 9;
  657. sector_t stripe;
  658. int chunk_offset;
  659. int chunk_number, dummy1, dummy2, dd_idx = i;
  660. sector_t r_sector;
  661. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  662. stripe = new_sector;
  663. BUG_ON(new_sector != stripe);
  664. switch (conf->algorithm) {
  665. case ALGORITHM_LEFT_ASYMMETRIC:
  666. case ALGORITHM_RIGHT_ASYMMETRIC:
  667. if (i > sh->pd_idx)
  668. i--;
  669. break;
  670. case ALGORITHM_LEFT_SYMMETRIC:
  671. case ALGORITHM_RIGHT_SYMMETRIC:
  672. if (i < sh->pd_idx)
  673. i += raid_disks;
  674. i -= (sh->pd_idx + 1);
  675. break;
  676. default:
  677. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  678. conf->algorithm);
  679. }
  680. chunk_number = stripe * data_disks + i;
  681. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  682. check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
  683. if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
  684. printk(KERN_ERR "compute_blocknr: map not correct\n");
  685. return 0;
  686. }
  687. return r_sector;
  688. }
  689. /*
  690. * Copy data between a page in the stripe cache, and a bio.
  691. * There are no alignment or size guarantees between the page or the
  692. * bio except that there is some overlap.
  693. * All iovecs in the bio must be considered.
  694. */
  695. static void copy_data(int frombio, struct bio *bio,
  696. struct page *page,
  697. sector_t sector)
  698. {
  699. char *pa = page_address(page);
  700. struct bio_vec *bvl;
  701. int i;
  702. int page_offset;
  703. if (bio->bi_sector >= sector)
  704. page_offset = (signed)(bio->bi_sector - sector) * 512;
  705. else
  706. page_offset = (signed)(sector - bio->bi_sector) * -512;
  707. bio_for_each_segment(bvl, bio, i) {
  708. int len = bio_iovec_idx(bio,i)->bv_len;
  709. int clen;
  710. int b_offset = 0;
  711. if (page_offset < 0) {
  712. b_offset = -page_offset;
  713. page_offset += b_offset;
  714. len -= b_offset;
  715. }
  716. if (len > 0 && page_offset + len > STRIPE_SIZE)
  717. clen = STRIPE_SIZE - page_offset;
  718. else clen = len;
  719. if (clen > 0) {
  720. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  721. if (frombio)
  722. memcpy(pa+page_offset, ba+b_offset, clen);
  723. else
  724. memcpy(ba+b_offset, pa+page_offset, clen);
  725. __bio_kunmap_atomic(ba, KM_USER0);
  726. }
  727. if (clen < len) /* hit end of page */
  728. break;
  729. page_offset += len;
  730. }
  731. }
  732. #define check_xor() do { \
  733. if (count == MAX_XOR_BLOCKS) { \
  734. xor_block(count, STRIPE_SIZE, ptr); \
  735. count = 1; \
  736. } \
  737. } while(0)
  738. static void compute_block(struct stripe_head *sh, int dd_idx)
  739. {
  740. int i, count, disks = sh->disks;
  741. void *ptr[MAX_XOR_BLOCKS], *p;
  742. PRINTK("compute_block, stripe %llu, idx %d\n",
  743. (unsigned long long)sh->sector, dd_idx);
  744. ptr[0] = page_address(sh->dev[dd_idx].page);
  745. memset(ptr[0], 0, STRIPE_SIZE);
  746. count = 1;
  747. for (i = disks ; i--; ) {
  748. if (i == dd_idx)
  749. continue;
  750. p = page_address(sh->dev[i].page);
  751. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  752. ptr[count++] = p;
  753. else
  754. printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
  755. " not present\n", dd_idx,
  756. (unsigned long long)sh->sector, i);
  757. check_xor();
  758. }
  759. if (count != 1)
  760. xor_block(count, STRIPE_SIZE, ptr);
  761. set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  762. }
  763. static void compute_parity(struct stripe_head *sh, int method)
  764. {
  765. raid5_conf_t *conf = sh->raid_conf;
  766. int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
  767. void *ptr[MAX_XOR_BLOCKS];
  768. struct bio *chosen;
  769. PRINTK("compute_parity, stripe %llu, method %d\n",
  770. (unsigned long long)sh->sector, method);
  771. count = 1;
  772. ptr[0] = page_address(sh->dev[pd_idx].page);
  773. switch(method) {
  774. case READ_MODIFY_WRITE:
  775. if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags))
  776. BUG();
  777. for (i=disks ; i-- ;) {
  778. if (i==pd_idx)
  779. continue;
  780. if (sh->dev[i].towrite &&
  781. test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  782. ptr[count++] = page_address(sh->dev[i].page);
  783. chosen = sh->dev[i].towrite;
  784. sh->dev[i].towrite = NULL;
  785. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  786. wake_up(&conf->wait_for_overlap);
  787. if (sh->dev[i].written) BUG();
  788. sh->dev[i].written = chosen;
  789. check_xor();
  790. }
  791. }
  792. break;
  793. case RECONSTRUCT_WRITE:
  794. memset(ptr[0], 0, STRIPE_SIZE);
  795. for (i= disks; i-- ;)
  796. if (i!=pd_idx && sh->dev[i].towrite) {
  797. chosen = sh->dev[i].towrite;
  798. sh->dev[i].towrite = NULL;
  799. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  800. wake_up(&conf->wait_for_overlap);
  801. if (sh->dev[i].written) BUG();
  802. sh->dev[i].written = chosen;
  803. }
  804. break;
  805. case CHECK_PARITY:
  806. break;
  807. }
  808. if (count>1) {
  809. xor_block(count, STRIPE_SIZE, ptr);
  810. count = 1;
  811. }
  812. for (i = disks; i--;)
  813. if (sh->dev[i].written) {
  814. sector_t sector = sh->dev[i].sector;
  815. struct bio *wbi = sh->dev[i].written;
  816. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  817. copy_data(1, wbi, sh->dev[i].page, sector);
  818. wbi = r5_next_bio(wbi, sector);
  819. }
  820. set_bit(R5_LOCKED, &sh->dev[i].flags);
  821. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  822. }
  823. switch(method) {
  824. case RECONSTRUCT_WRITE:
  825. case CHECK_PARITY:
  826. for (i=disks; i--;)
  827. if (i != pd_idx) {
  828. ptr[count++] = page_address(sh->dev[i].page);
  829. check_xor();
  830. }
  831. break;
  832. case READ_MODIFY_WRITE:
  833. for (i = disks; i--;)
  834. if (sh->dev[i].written) {
  835. ptr[count++] = page_address(sh->dev[i].page);
  836. check_xor();
  837. }
  838. }
  839. if (count != 1)
  840. xor_block(count, STRIPE_SIZE, ptr);
  841. if (method != CHECK_PARITY) {
  842. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  843. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  844. } else
  845. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  846. }
  847. /*
  848. * Each stripe/dev can have one or more bion attached.
  849. * toread/towrite point to the first in a chain.
  850. * The bi_next chain must be in order.
  851. */
  852. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  853. {
  854. struct bio **bip;
  855. raid5_conf_t *conf = sh->raid_conf;
  856. int firstwrite=0;
  857. PRINTK("adding bh b#%llu to stripe s#%llu\n",
  858. (unsigned long long)bi->bi_sector,
  859. (unsigned long long)sh->sector);
  860. spin_lock(&sh->lock);
  861. spin_lock_irq(&conf->device_lock);
  862. if (forwrite) {
  863. bip = &sh->dev[dd_idx].towrite;
  864. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  865. firstwrite = 1;
  866. } else
  867. bip = &sh->dev[dd_idx].toread;
  868. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  869. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  870. goto overlap;
  871. bip = & (*bip)->bi_next;
  872. }
  873. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  874. goto overlap;
  875. if (*bip && bi->bi_next && (*bip) != bi->bi_next)
  876. BUG();
  877. if (*bip)
  878. bi->bi_next = *bip;
  879. *bip = bi;
  880. bi->bi_phys_segments ++;
  881. spin_unlock_irq(&conf->device_lock);
  882. spin_unlock(&sh->lock);
  883. PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
  884. (unsigned long long)bi->bi_sector,
  885. (unsigned long long)sh->sector, dd_idx);
  886. if (conf->mddev->bitmap && firstwrite) {
  887. sh->bm_seq = conf->seq_write;
  888. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  889. STRIPE_SECTORS, 0);
  890. set_bit(STRIPE_BIT_DELAY, &sh->state);
  891. }
  892. if (forwrite) {
  893. /* check if page is covered */
  894. sector_t sector = sh->dev[dd_idx].sector;
  895. for (bi=sh->dev[dd_idx].towrite;
  896. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  897. bi && bi->bi_sector <= sector;
  898. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  899. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  900. sector = bi->bi_sector + (bi->bi_size>>9);
  901. }
  902. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  903. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  904. }
  905. return 1;
  906. overlap:
  907. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  908. spin_unlock_irq(&conf->device_lock);
  909. spin_unlock(&sh->lock);
  910. return 0;
  911. }
  912. static void end_reshape(raid5_conf_t *conf);
  913. static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
  914. {
  915. int sectors_per_chunk = conf->chunk_size >> 9;
  916. sector_t x = stripe;
  917. int pd_idx, dd_idx;
  918. int chunk_offset = sector_div(x, sectors_per_chunk);
  919. stripe = x;
  920. raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
  921. + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
  922. return pd_idx;
  923. }
  924. /*
  925. * handle_stripe - do things to a stripe.
  926. *
  927. * We lock the stripe and then examine the state of various bits
  928. * to see what needs to be done.
  929. * Possible results:
  930. * return some read request which now have data
  931. * return some write requests which are safely on disc
  932. * schedule a read on some buffers
  933. * schedule a write of some buffers
  934. * return confirmation of parity correctness
  935. *
  936. * Parity calculations are done inside the stripe lock
  937. * buffers are taken off read_list or write_list, and bh_cache buffers
  938. * get BH_Lock set before the stripe lock is released.
  939. *
  940. */
  941. static void handle_stripe(struct stripe_head *sh)
  942. {
  943. raid5_conf_t *conf = sh->raid_conf;
  944. int disks = sh->disks;
  945. struct bio *return_bi= NULL;
  946. struct bio *bi;
  947. int i;
  948. int syncing, expanding, expanded;
  949. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  950. int non_overwrite = 0;
  951. int failed_num=0;
  952. struct r5dev *dev;
  953. PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
  954. (unsigned long long)sh->sector, atomic_read(&sh->count),
  955. sh->pd_idx);
  956. spin_lock(&sh->lock);
  957. clear_bit(STRIPE_HANDLE, &sh->state);
  958. clear_bit(STRIPE_DELAYED, &sh->state);
  959. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  960. expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  961. expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  962. /* Now to look around and see what can be done */
  963. rcu_read_lock();
  964. for (i=disks; i--; ) {
  965. mdk_rdev_t *rdev;
  966. dev = &sh->dev[i];
  967. clear_bit(R5_Insync, &dev->flags);
  968. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  969. i, dev->flags, dev->toread, dev->towrite, dev->written);
  970. /* maybe we can reply to a read */
  971. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  972. struct bio *rbi, *rbi2;
  973. PRINTK("Return read for disc %d\n", i);
  974. spin_lock_irq(&conf->device_lock);
  975. rbi = dev->toread;
  976. dev->toread = NULL;
  977. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  978. wake_up(&conf->wait_for_overlap);
  979. spin_unlock_irq(&conf->device_lock);
  980. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  981. copy_data(0, rbi, dev->page, dev->sector);
  982. rbi2 = r5_next_bio(rbi, dev->sector);
  983. spin_lock_irq(&conf->device_lock);
  984. if (--rbi->bi_phys_segments == 0) {
  985. rbi->bi_next = return_bi;
  986. return_bi = rbi;
  987. }
  988. spin_unlock_irq(&conf->device_lock);
  989. rbi = rbi2;
  990. }
  991. }
  992. /* now count some things */
  993. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  994. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  995. if (dev->toread) to_read++;
  996. if (dev->towrite) {
  997. to_write++;
  998. if (!test_bit(R5_OVERWRITE, &dev->flags))
  999. non_overwrite++;
  1000. }
  1001. if (dev->written) written++;
  1002. rdev = rcu_dereference(conf->disks[i].rdev);
  1003. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  1004. /* The ReadError flag will just be confusing now */
  1005. clear_bit(R5_ReadError, &dev->flags);
  1006. clear_bit(R5_ReWrite, &dev->flags);
  1007. }
  1008. if (!rdev || !test_bit(In_sync, &rdev->flags)
  1009. || test_bit(R5_ReadError, &dev->flags)) {
  1010. failed++;
  1011. failed_num = i;
  1012. } else
  1013. set_bit(R5_Insync, &dev->flags);
  1014. }
  1015. rcu_read_unlock();
  1016. PRINTK("locked=%d uptodate=%d to_read=%d"
  1017. " to_write=%d failed=%d failed_num=%d\n",
  1018. locked, uptodate, to_read, to_write, failed, failed_num);
  1019. /* check if the array has lost two devices and, if so, some requests might
  1020. * need to be failed
  1021. */
  1022. if (failed > 1 && to_read+to_write+written) {
  1023. for (i=disks; i--; ) {
  1024. int bitmap_end = 0;
  1025. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1026. mdk_rdev_t *rdev;
  1027. rcu_read_lock();
  1028. rdev = rcu_dereference(conf->disks[i].rdev);
  1029. if (rdev && test_bit(In_sync, &rdev->flags))
  1030. /* multiple read failures in one stripe */
  1031. md_error(conf->mddev, rdev);
  1032. rcu_read_unlock();
  1033. }
  1034. spin_lock_irq(&conf->device_lock);
  1035. /* fail all writes first */
  1036. bi = sh->dev[i].towrite;
  1037. sh->dev[i].towrite = NULL;
  1038. if (bi) { to_write--; bitmap_end = 1; }
  1039. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1040. wake_up(&conf->wait_for_overlap);
  1041. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1042. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1043. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1044. if (--bi->bi_phys_segments == 0) {
  1045. md_write_end(conf->mddev);
  1046. bi->bi_next = return_bi;
  1047. return_bi = bi;
  1048. }
  1049. bi = nextbi;
  1050. }
  1051. /* and fail all 'written' */
  1052. bi = sh->dev[i].written;
  1053. sh->dev[i].written = NULL;
  1054. if (bi) bitmap_end = 1;
  1055. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  1056. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1057. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1058. if (--bi->bi_phys_segments == 0) {
  1059. md_write_end(conf->mddev);
  1060. bi->bi_next = return_bi;
  1061. return_bi = bi;
  1062. }
  1063. bi = bi2;
  1064. }
  1065. /* fail any reads if this device is non-operational */
  1066. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1067. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1068. bi = sh->dev[i].toread;
  1069. sh->dev[i].toread = NULL;
  1070. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1071. wake_up(&conf->wait_for_overlap);
  1072. if (bi) to_read--;
  1073. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1074. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1075. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1076. if (--bi->bi_phys_segments == 0) {
  1077. bi->bi_next = return_bi;
  1078. return_bi = bi;
  1079. }
  1080. bi = nextbi;
  1081. }
  1082. }
  1083. spin_unlock_irq(&conf->device_lock);
  1084. if (bitmap_end)
  1085. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1086. STRIPE_SECTORS, 0, 0);
  1087. }
  1088. }
  1089. if (failed > 1 && syncing) {
  1090. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  1091. clear_bit(STRIPE_SYNCING, &sh->state);
  1092. syncing = 0;
  1093. }
  1094. /* might be able to return some write requests if the parity block
  1095. * is safe, or on a failed drive
  1096. */
  1097. dev = &sh->dev[sh->pd_idx];
  1098. if ( written &&
  1099. ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
  1100. test_bit(R5_UPTODATE, &dev->flags))
  1101. || (failed == 1 && failed_num == sh->pd_idx))
  1102. ) {
  1103. /* any written block on an uptodate or failed drive can be returned.
  1104. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  1105. * never LOCKED, so we don't need to test 'failed' directly.
  1106. */
  1107. for (i=disks; i--; )
  1108. if (sh->dev[i].written) {
  1109. dev = &sh->dev[i];
  1110. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1111. test_bit(R5_UPTODATE, &dev->flags) ) {
  1112. /* We can return any write requests */
  1113. struct bio *wbi, *wbi2;
  1114. int bitmap_end = 0;
  1115. PRINTK("Return write for disc %d\n", i);
  1116. spin_lock_irq(&conf->device_lock);
  1117. wbi = dev->written;
  1118. dev->written = NULL;
  1119. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1120. wbi2 = r5_next_bio(wbi, dev->sector);
  1121. if (--wbi->bi_phys_segments == 0) {
  1122. md_write_end(conf->mddev);
  1123. wbi->bi_next = return_bi;
  1124. return_bi = wbi;
  1125. }
  1126. wbi = wbi2;
  1127. }
  1128. if (dev->towrite == NULL)
  1129. bitmap_end = 1;
  1130. spin_unlock_irq(&conf->device_lock);
  1131. if (bitmap_end)
  1132. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1133. STRIPE_SECTORS,
  1134. !test_bit(STRIPE_DEGRADED, &sh->state), 0);
  1135. }
  1136. }
  1137. }
  1138. /* Now we might consider reading some blocks, either to check/generate
  1139. * parity, or to satisfy requests
  1140. * or to load a block that is being partially written.
  1141. */
  1142. if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
  1143. for (i=disks; i--;) {
  1144. dev = &sh->dev[i];
  1145. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1146. (dev->toread ||
  1147. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1148. syncing ||
  1149. expanding ||
  1150. (failed && (sh->dev[failed_num].toread ||
  1151. (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
  1152. )
  1153. ) {
  1154. /* we would like to get this block, possibly
  1155. * by computing it, but we might not be able to
  1156. */
  1157. if (uptodate == disks-1) {
  1158. PRINTK("Computing block %d\n", i);
  1159. compute_block(sh, i);
  1160. uptodate++;
  1161. } else if (test_bit(R5_Insync, &dev->flags)) {
  1162. set_bit(R5_LOCKED, &dev->flags);
  1163. set_bit(R5_Wantread, &dev->flags);
  1164. #if 0
  1165. /* if I am just reading this block and we don't have
  1166. a failed drive, or any pending writes then sidestep the cache */
  1167. if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
  1168. ! syncing && !failed && !to_write) {
  1169. sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
  1170. sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
  1171. }
  1172. #endif
  1173. locked++;
  1174. PRINTK("Reading block %d (sync=%d)\n",
  1175. i, syncing);
  1176. }
  1177. }
  1178. }
  1179. set_bit(STRIPE_HANDLE, &sh->state);
  1180. }
  1181. /* now to consider writing and what else, if anything should be read */
  1182. if (to_write) {
  1183. int rmw=0, rcw=0;
  1184. for (i=disks ; i--;) {
  1185. /* would I have to read this buffer for read_modify_write */
  1186. dev = &sh->dev[i];
  1187. if ((dev->towrite || i == sh->pd_idx) &&
  1188. (!test_bit(R5_LOCKED, &dev->flags)
  1189. #if 0
  1190. || sh->bh_page[i]!=bh->b_page
  1191. #endif
  1192. ) &&
  1193. !test_bit(R5_UPTODATE, &dev->flags)) {
  1194. if (test_bit(R5_Insync, &dev->flags)
  1195. /* && !(!mddev->insync && i == sh->pd_idx) */
  1196. )
  1197. rmw++;
  1198. else rmw += 2*disks; /* cannot read it */
  1199. }
  1200. /* Would I have to read this buffer for reconstruct_write */
  1201. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1202. (!test_bit(R5_LOCKED, &dev->flags)
  1203. #if 0
  1204. || sh->bh_page[i] != bh->b_page
  1205. #endif
  1206. ) &&
  1207. !test_bit(R5_UPTODATE, &dev->flags)) {
  1208. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1209. else rcw += 2*disks;
  1210. }
  1211. }
  1212. PRINTK("for sector %llu, rmw=%d rcw=%d\n",
  1213. (unsigned long long)sh->sector, rmw, rcw);
  1214. set_bit(STRIPE_HANDLE, &sh->state);
  1215. if (rmw < rcw && rmw > 0)
  1216. /* prefer read-modify-write, but need to get some data */
  1217. for (i=disks; i--;) {
  1218. dev = &sh->dev[i];
  1219. if ((dev->towrite || i == sh->pd_idx) &&
  1220. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1221. test_bit(R5_Insync, &dev->flags)) {
  1222. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1223. {
  1224. PRINTK("Read_old block %d for r-m-w\n", i);
  1225. set_bit(R5_LOCKED, &dev->flags);
  1226. set_bit(R5_Wantread, &dev->flags);
  1227. locked++;
  1228. } else {
  1229. set_bit(STRIPE_DELAYED, &sh->state);
  1230. set_bit(STRIPE_HANDLE, &sh->state);
  1231. }
  1232. }
  1233. }
  1234. if (rcw <= rmw && rcw > 0)
  1235. /* want reconstruct write, but need to get some data */
  1236. for (i=disks; i--;) {
  1237. dev = &sh->dev[i];
  1238. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1239. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1240. test_bit(R5_Insync, &dev->flags)) {
  1241. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1242. {
  1243. PRINTK("Read_old block %d for Reconstruct\n", i);
  1244. set_bit(R5_LOCKED, &dev->flags);
  1245. set_bit(R5_Wantread, &dev->flags);
  1246. locked++;
  1247. } else {
  1248. set_bit(STRIPE_DELAYED, &sh->state);
  1249. set_bit(STRIPE_HANDLE, &sh->state);
  1250. }
  1251. }
  1252. }
  1253. /* now if nothing is locked, and if we have enough data, we can start a write request */
  1254. if (locked == 0 && (rcw == 0 ||rmw == 0) &&
  1255. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1256. PRINTK("Computing parity...\n");
  1257. compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
  1258. /* now every locked buffer is ready to be written */
  1259. for (i=disks; i--;)
  1260. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  1261. PRINTK("Writing block %d\n", i);
  1262. locked++;
  1263. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1264. if (!test_bit(R5_Insync, &sh->dev[i].flags)
  1265. || (i==sh->pd_idx && failed == 0))
  1266. set_bit(STRIPE_INSYNC, &sh->state);
  1267. }
  1268. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1269. atomic_dec(&conf->preread_active_stripes);
  1270. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  1271. md_wakeup_thread(conf->mddev->thread);
  1272. }
  1273. }
  1274. }
  1275. /* maybe we need to check and possibly fix the parity for this stripe
  1276. * Any reads will already have been scheduled, so we just see if enough data
  1277. * is available
  1278. */
  1279. if (syncing && locked == 0 &&
  1280. !test_bit(STRIPE_INSYNC, &sh->state)) {
  1281. set_bit(STRIPE_HANDLE, &sh->state);
  1282. if (failed == 0) {
  1283. char *pagea;
  1284. if (uptodate != disks)
  1285. BUG();
  1286. compute_parity(sh, CHECK_PARITY);
  1287. uptodate--;
  1288. pagea = page_address(sh->dev[sh->pd_idx].page);
  1289. if ((*(u32*)pagea) == 0 &&
  1290. !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
  1291. /* parity is correct (on disc, not in buffer any more) */
  1292. set_bit(STRIPE_INSYNC, &sh->state);
  1293. } else {
  1294. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  1295. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  1296. /* don't try to repair!! */
  1297. set_bit(STRIPE_INSYNC, &sh->state);
  1298. else {
  1299. compute_block(sh, sh->pd_idx);
  1300. uptodate++;
  1301. }
  1302. }
  1303. }
  1304. if (!test_bit(STRIPE_INSYNC, &sh->state)) {
  1305. /* either failed parity check, or recovery is happening */
  1306. if (failed==0)
  1307. failed_num = sh->pd_idx;
  1308. dev = &sh->dev[failed_num];
  1309. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  1310. BUG_ON(uptodate != disks);
  1311. set_bit(R5_LOCKED, &dev->flags);
  1312. set_bit(R5_Wantwrite, &dev->flags);
  1313. clear_bit(STRIPE_DEGRADED, &sh->state);
  1314. locked++;
  1315. set_bit(STRIPE_INSYNC, &sh->state);
  1316. }
  1317. }
  1318. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  1319. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  1320. clear_bit(STRIPE_SYNCING, &sh->state);
  1321. }
  1322. /* If the failed drive is just a ReadError, then we might need to progress
  1323. * the repair/check process
  1324. */
  1325. if (failed == 1 && ! conf->mddev->ro &&
  1326. test_bit(R5_ReadError, &sh->dev[failed_num].flags)
  1327. && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
  1328. && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
  1329. ) {
  1330. dev = &sh->dev[failed_num];
  1331. if (!test_bit(R5_ReWrite, &dev->flags)) {
  1332. set_bit(R5_Wantwrite, &dev->flags);
  1333. set_bit(R5_ReWrite, &dev->flags);
  1334. set_bit(R5_LOCKED, &dev->flags);
  1335. locked++;
  1336. } else {
  1337. /* let's read it back */
  1338. set_bit(R5_Wantread, &dev->flags);
  1339. set_bit(R5_LOCKED, &dev->flags);
  1340. locked++;
  1341. }
  1342. }
  1343. if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  1344. /* Need to write out all blocks after computing parity */
  1345. sh->disks = conf->raid_disks;
  1346. sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
  1347. compute_parity(sh, RECONSTRUCT_WRITE);
  1348. for (i= conf->raid_disks; i--;) {
  1349. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1350. locked++;
  1351. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1352. }
  1353. clear_bit(STRIPE_EXPANDING, &sh->state);
  1354. } else if (expanded) {
  1355. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  1356. atomic_dec(&conf->reshape_stripes);
  1357. wake_up(&conf->wait_for_overlap);
  1358. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  1359. }
  1360. if (expanding && locked == 0) {
  1361. /* We have read all the blocks in this stripe and now we need to
  1362. * copy some of them into a target stripe for expand.
  1363. */
  1364. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1365. for (i=0; i< sh->disks; i++)
  1366. if (i != sh->pd_idx) {
  1367. int dd_idx, pd_idx, j;
  1368. struct stripe_head *sh2;
  1369. sector_t bn = compute_blocknr(sh, i);
  1370. sector_t s = raid5_compute_sector(bn, conf->raid_disks,
  1371. conf->raid_disks-1,
  1372. &dd_idx, &pd_idx, conf);
  1373. sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
  1374. if (sh2 == NULL)
  1375. /* so far only the early blocks of this stripe
  1376. * have been requested. When later blocks
  1377. * get requested, we will try again
  1378. */
  1379. continue;
  1380. if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  1381. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  1382. /* must have already done this block */
  1383. release_stripe(sh2);
  1384. continue;
  1385. }
  1386. memcpy(page_address(sh2->dev[dd_idx].page),
  1387. page_address(sh->dev[i].page),
  1388. STRIPE_SIZE);
  1389. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  1390. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  1391. for (j=0; j<conf->raid_disks; j++)
  1392. if (j != sh2->pd_idx &&
  1393. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  1394. break;
  1395. if (j == conf->raid_disks) {
  1396. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  1397. set_bit(STRIPE_HANDLE, &sh2->state);
  1398. }
  1399. release_stripe(sh2);
  1400. }
  1401. }
  1402. spin_unlock(&sh->lock);
  1403. while ((bi=return_bi)) {
  1404. int bytes = bi->bi_size;
  1405. return_bi = bi->bi_next;
  1406. bi->bi_next = NULL;
  1407. bi->bi_size = 0;
  1408. bi->bi_end_io(bi, bytes, 0);
  1409. }
  1410. for (i=disks; i-- ;) {
  1411. int rw;
  1412. struct bio *bi;
  1413. mdk_rdev_t *rdev;
  1414. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  1415. rw = 1;
  1416. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  1417. rw = 0;
  1418. else
  1419. continue;
  1420. bi = &sh->dev[i].req;
  1421. bi->bi_rw = rw;
  1422. if (rw)
  1423. bi->bi_end_io = raid5_end_write_request;
  1424. else
  1425. bi->bi_end_io = raid5_end_read_request;
  1426. rcu_read_lock();
  1427. rdev = rcu_dereference(conf->disks[i].rdev);
  1428. if (rdev && test_bit(Faulty, &rdev->flags))
  1429. rdev = NULL;
  1430. if (rdev)
  1431. atomic_inc(&rdev->nr_pending);
  1432. rcu_read_unlock();
  1433. if (rdev) {
  1434. if (syncing || expanding || expanded)
  1435. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  1436. bi->bi_bdev = rdev->bdev;
  1437. PRINTK("for %llu schedule op %ld on disc %d\n",
  1438. (unsigned long long)sh->sector, bi->bi_rw, i);
  1439. atomic_inc(&sh->count);
  1440. bi->bi_sector = sh->sector + rdev->data_offset;
  1441. bi->bi_flags = 1 << BIO_UPTODATE;
  1442. bi->bi_vcnt = 1;
  1443. bi->bi_max_vecs = 1;
  1444. bi->bi_idx = 0;
  1445. bi->bi_io_vec = &sh->dev[i].vec;
  1446. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  1447. bi->bi_io_vec[0].bv_offset = 0;
  1448. bi->bi_size = STRIPE_SIZE;
  1449. bi->bi_next = NULL;
  1450. if (rw == WRITE &&
  1451. test_bit(R5_ReWrite, &sh->dev[i].flags))
  1452. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  1453. generic_make_request(bi);
  1454. } else {
  1455. if (rw == 1)
  1456. set_bit(STRIPE_DEGRADED, &sh->state);
  1457. PRINTK("skip op %ld on disc %d for sector %llu\n",
  1458. bi->bi_rw, i, (unsigned long long)sh->sector);
  1459. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1460. set_bit(STRIPE_HANDLE, &sh->state);
  1461. }
  1462. }
  1463. }
  1464. static void raid5_activate_delayed(raid5_conf_t *conf)
  1465. {
  1466. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  1467. while (!list_empty(&conf->delayed_list)) {
  1468. struct list_head *l = conf->delayed_list.next;
  1469. struct stripe_head *sh;
  1470. sh = list_entry(l, struct stripe_head, lru);
  1471. list_del_init(l);
  1472. clear_bit(STRIPE_DELAYED, &sh->state);
  1473. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1474. atomic_inc(&conf->preread_active_stripes);
  1475. list_add_tail(&sh->lru, &conf->handle_list);
  1476. }
  1477. }
  1478. }
  1479. static void activate_bit_delay(raid5_conf_t *conf)
  1480. {
  1481. /* device_lock is held */
  1482. struct list_head head;
  1483. list_add(&head, &conf->bitmap_list);
  1484. list_del_init(&conf->bitmap_list);
  1485. while (!list_empty(&head)) {
  1486. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  1487. list_del_init(&sh->lru);
  1488. atomic_inc(&sh->count);
  1489. __release_stripe(conf, sh);
  1490. }
  1491. }
  1492. static void unplug_slaves(mddev_t *mddev)
  1493. {
  1494. raid5_conf_t *conf = mddev_to_conf(mddev);
  1495. int i;
  1496. rcu_read_lock();
  1497. for (i=0; i<mddev->raid_disks; i++) {
  1498. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  1499. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  1500. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  1501. atomic_inc(&rdev->nr_pending);
  1502. rcu_read_unlock();
  1503. if (r_queue->unplug_fn)
  1504. r_queue->unplug_fn(r_queue);
  1505. rdev_dec_pending(rdev, mddev);
  1506. rcu_read_lock();
  1507. }
  1508. }
  1509. rcu_read_unlock();
  1510. }
  1511. static void raid5_unplug_device(request_queue_t *q)
  1512. {
  1513. mddev_t *mddev = q->queuedata;
  1514. raid5_conf_t *conf = mddev_to_conf(mddev);
  1515. unsigned long flags;
  1516. spin_lock_irqsave(&conf->device_lock, flags);
  1517. if (blk_remove_plug(q)) {
  1518. conf->seq_flush++;
  1519. raid5_activate_delayed(conf);
  1520. }
  1521. md_wakeup_thread(mddev->thread);
  1522. spin_unlock_irqrestore(&conf->device_lock, flags);
  1523. unplug_slaves(mddev);
  1524. }
  1525. static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
  1526. sector_t *error_sector)
  1527. {
  1528. mddev_t *mddev = q->queuedata;
  1529. raid5_conf_t *conf = mddev_to_conf(mddev);
  1530. int i, ret = 0;
  1531. rcu_read_lock();
  1532. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  1533. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  1534. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  1535. struct block_device *bdev = rdev->bdev;
  1536. request_queue_t *r_queue = bdev_get_queue(bdev);
  1537. if (!r_queue->issue_flush_fn)
  1538. ret = -EOPNOTSUPP;
  1539. else {
  1540. atomic_inc(&rdev->nr_pending);
  1541. rcu_read_unlock();
  1542. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  1543. error_sector);
  1544. rdev_dec_pending(rdev, mddev);
  1545. rcu_read_lock();
  1546. }
  1547. }
  1548. }
  1549. rcu_read_unlock();
  1550. return ret;
  1551. }
  1552. static inline void raid5_plug_device(raid5_conf_t *conf)
  1553. {
  1554. spin_lock_irq(&conf->device_lock);
  1555. blk_plug_device(conf->mddev->queue);
  1556. spin_unlock_irq(&conf->device_lock);
  1557. }
  1558. static int make_request(request_queue_t *q, struct bio * bi)
  1559. {
  1560. mddev_t *mddev = q->queuedata;
  1561. raid5_conf_t *conf = mddev_to_conf(mddev);
  1562. unsigned int dd_idx, pd_idx;
  1563. sector_t new_sector;
  1564. sector_t logical_sector, last_sector;
  1565. struct stripe_head *sh;
  1566. const int rw = bio_data_dir(bi);
  1567. if (unlikely(bio_barrier(bi))) {
  1568. bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
  1569. return 0;
  1570. }
  1571. md_write_start(mddev, bi);
  1572. disk_stat_inc(mddev->gendisk, ios[rw]);
  1573. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
  1574. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  1575. last_sector = bi->bi_sector + (bi->bi_size>>9);
  1576. bi->bi_next = NULL;
  1577. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  1578. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  1579. DEFINE_WAIT(w);
  1580. int disks;
  1581. retry:
  1582. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  1583. if (likely(conf->expand_progress == MaxSector))
  1584. disks = conf->raid_disks;
  1585. else {
  1586. spin_lock_irq(&conf->device_lock);
  1587. disks = conf->raid_disks;
  1588. if (logical_sector >= conf->expand_progress)
  1589. disks = conf->previous_raid_disks;
  1590. else {
  1591. if (logical_sector >= conf->expand_lo) {
  1592. spin_unlock_irq(&conf->device_lock);
  1593. schedule();
  1594. goto retry;
  1595. }
  1596. }
  1597. spin_unlock_irq(&conf->device_lock);
  1598. }
  1599. new_sector = raid5_compute_sector(logical_sector, disks, disks - 1,
  1600. &dd_idx, &pd_idx, conf);
  1601. PRINTK("raid5: make_request, sector %llu logical %llu\n",
  1602. (unsigned long long)new_sector,
  1603. (unsigned long long)logical_sector);
  1604. sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
  1605. if (sh) {
  1606. if (unlikely(conf->expand_progress != MaxSector)) {
  1607. /* expansion might have moved on while waiting for a
  1608. * stripe, so we much do the range check again.
  1609. */
  1610. int must_retry = 0;
  1611. spin_lock_irq(&conf->device_lock);
  1612. if (logical_sector < conf->expand_progress &&
  1613. disks == conf->previous_raid_disks)
  1614. /* mismatch, need to try again */
  1615. must_retry = 1;
  1616. spin_unlock_irq(&conf->device_lock);
  1617. if (must_retry) {
  1618. release_stripe(sh);
  1619. goto retry;
  1620. }
  1621. }
  1622. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  1623. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  1624. /* Stripe is busy expanding or
  1625. * add failed due to overlap. Flush everything
  1626. * and wait a while
  1627. */
  1628. raid5_unplug_device(mddev->queue);
  1629. release_stripe(sh);
  1630. schedule();
  1631. goto retry;
  1632. }
  1633. finish_wait(&conf->wait_for_overlap, &w);
  1634. raid5_plug_device(conf);
  1635. handle_stripe(sh);
  1636. release_stripe(sh);
  1637. } else {
  1638. /* cannot get stripe for read-ahead, just give-up */
  1639. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1640. finish_wait(&conf->wait_for_overlap, &w);
  1641. break;
  1642. }
  1643. }
  1644. spin_lock_irq(&conf->device_lock);
  1645. if (--bi->bi_phys_segments == 0) {
  1646. int bytes = bi->bi_size;
  1647. if ( bio_data_dir(bi) == WRITE )
  1648. md_write_end(mddev);
  1649. bi->bi_size = 0;
  1650. bi->bi_end_io(bi, bytes, 0);
  1651. }
  1652. spin_unlock_irq(&conf->device_lock);
  1653. return 0;
  1654. }
  1655. /* FIXME go_faster isn't used */
  1656. static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  1657. {
  1658. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1659. struct stripe_head *sh;
  1660. int pd_idx;
  1661. sector_t first_sector, last_sector;
  1662. int raid_disks = conf->raid_disks;
  1663. int data_disks = raid_disks-1;
  1664. sector_t max_sector = mddev->size << 1;
  1665. int sync_blocks;
  1666. if (sector_nr >= max_sector) {
  1667. /* just being told to finish up .. nothing much to do */
  1668. unplug_slaves(mddev);
  1669. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  1670. end_reshape(conf);
  1671. return 0;
  1672. }
  1673. if (mddev->curr_resync < max_sector) /* aborted */
  1674. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  1675. &sync_blocks, 1);
  1676. else /* compelted sync */
  1677. conf->fullsync = 0;
  1678. bitmap_close_sync(mddev->bitmap);
  1679. return 0;
  1680. }
  1681. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  1682. /* reshaping is quite different to recovery/resync so it is
  1683. * handled quite separately ... here.
  1684. *
  1685. * On each call to sync_request, we gather one chunk worth of
  1686. * destination stripes and flag them as expanding.
  1687. * Then we find all the source stripes and request reads.
  1688. * As the reads complete, handle_stripe will copy the data
  1689. * into the destination stripe and release that stripe.
  1690. */
  1691. int i;
  1692. int dd_idx;
  1693. sector_t writepos, safepos, gap;
  1694. if (sector_nr == 0 &&
  1695. conf->expand_progress != 0) {
  1696. /* restarting in the middle, skip the initial sectors */
  1697. sector_nr = conf->expand_progress;
  1698. sector_div(sector_nr, conf->raid_disks-1);
  1699. *skipped = 1;
  1700. return sector_nr;
  1701. }
  1702. /* we update the metadata when there is more than 3Meg
  1703. * in the block range (that is rather arbitrary, should
  1704. * probably be time based) or when the data about to be
  1705. * copied would over-write the source of the data at
  1706. * the front of the range.
  1707. * i.e. one new_stripe forward from expand_progress new_maps
  1708. * to after where expand_lo old_maps to
  1709. */
  1710. writepos = conf->expand_progress +
  1711. conf->chunk_size/512*(conf->raid_disks-1);
  1712. sector_div(writepos, conf->raid_disks-1);
  1713. safepos = conf->expand_lo;
  1714. sector_div(safepos, conf->previous_raid_disks-1);
  1715. gap = conf->expand_progress - conf->expand_lo;
  1716. if (writepos >= safepos ||
  1717. gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
  1718. /* Cannot proceed until we've updated the superblock... */
  1719. wait_event(conf->wait_for_overlap,
  1720. atomic_read(&conf->reshape_stripes)==0);
  1721. mddev->reshape_position = conf->expand_progress;
  1722. mddev->sb_dirty = 1;
  1723. md_wakeup_thread(mddev->thread);
  1724. wait_event(mddev->sb_wait, mddev->sb_dirty == 0 ||
  1725. kthread_should_stop());
  1726. spin_lock_irq(&conf->device_lock);
  1727. conf->expand_lo = mddev->reshape_position;
  1728. spin_unlock_irq(&conf->device_lock);
  1729. wake_up(&conf->wait_for_overlap);
  1730. }
  1731. for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
  1732. int j;
  1733. int skipped = 0;
  1734. pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
  1735. sh = get_active_stripe(conf, sector_nr+i,
  1736. conf->raid_disks, pd_idx, 0);
  1737. set_bit(STRIPE_EXPANDING, &sh->state);
  1738. atomic_inc(&conf->reshape_stripes);
  1739. /* If any of this stripe is beyond the end of the old
  1740. * array, then we need to zero those blocks
  1741. */
  1742. for (j=sh->disks; j--;) {
  1743. sector_t s;
  1744. if (j == sh->pd_idx)
  1745. continue;
  1746. s = compute_blocknr(sh, j);
  1747. if (s < (mddev->array_size<<1)) {
  1748. skipped = 1;
  1749. continue;
  1750. }
  1751. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  1752. set_bit(R5_Expanded, &sh->dev[j].flags);
  1753. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  1754. }
  1755. if (!skipped) {
  1756. set_bit(STRIPE_EXPAND_READY, &sh->state);
  1757. set_bit(STRIPE_HANDLE, &sh->state);
  1758. }
  1759. release_stripe(sh);
  1760. }
  1761. spin_lock_irq(&conf->device_lock);
  1762. conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
  1763. spin_unlock_irq(&conf->device_lock);
  1764. /* Ok, those stripe are ready. We can start scheduling
  1765. * reads on the source stripes.
  1766. * The source stripes are determined by mapping the first and last
  1767. * block on the destination stripes.
  1768. */
  1769. raid_disks = conf->previous_raid_disks;
  1770. data_disks = raid_disks - 1;
  1771. first_sector =
  1772. raid5_compute_sector(sector_nr*(conf->raid_disks-1),
  1773. raid_disks, data_disks,
  1774. &dd_idx, &pd_idx, conf);
  1775. last_sector =
  1776. raid5_compute_sector((sector_nr+conf->chunk_size/512)
  1777. *(conf->raid_disks-1) -1,
  1778. raid_disks, data_disks,
  1779. &dd_idx, &pd_idx, conf);
  1780. if (last_sector >= (mddev->size<<1))
  1781. last_sector = (mddev->size<<1)-1;
  1782. while (first_sector <= last_sector) {
  1783. pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
  1784. sh = get_active_stripe(conf, first_sector,
  1785. conf->previous_raid_disks, pd_idx, 0);
  1786. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1787. set_bit(STRIPE_HANDLE, &sh->state);
  1788. release_stripe(sh);
  1789. first_sector += STRIPE_SECTORS;
  1790. }
  1791. return conf->chunk_size>>9;
  1792. }
  1793. /* if there is 1 or more failed drives and we are trying
  1794. * to resync, then assert that we are finished, because there is
  1795. * nothing we can do.
  1796. */
  1797. if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  1798. sector_t rv = (mddev->size << 1) - sector_nr;
  1799. *skipped = 1;
  1800. return rv;
  1801. }
  1802. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  1803. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  1804. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  1805. /* we can skip this block, and probably more */
  1806. sync_blocks /= STRIPE_SECTORS;
  1807. *skipped = 1;
  1808. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  1809. }
  1810. pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
  1811. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
  1812. if (sh == NULL) {
  1813. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
  1814. /* make sure we don't swamp the stripe cache if someone else
  1815. * is trying to get access
  1816. */
  1817. schedule_timeout_uninterruptible(1);
  1818. }
  1819. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0);
  1820. spin_lock(&sh->lock);
  1821. set_bit(STRIPE_SYNCING, &sh->state);
  1822. clear_bit(STRIPE_INSYNC, &sh->state);
  1823. spin_unlock(&sh->lock);
  1824. handle_stripe(sh);
  1825. release_stripe(sh);
  1826. return STRIPE_SECTORS;
  1827. }
  1828. /*
  1829. * This is our raid5 kernel thread.
  1830. *
  1831. * We scan the hash table for stripes which can be handled now.
  1832. * During the scan, completed stripes are saved for us by the interrupt
  1833. * handler, so that they will not have to wait for our next wakeup.
  1834. */
  1835. static void raid5d (mddev_t *mddev)
  1836. {
  1837. struct stripe_head *sh;
  1838. raid5_conf_t *conf = mddev_to_conf(mddev);
  1839. int handled;
  1840. PRINTK("+++ raid5d active\n");
  1841. md_check_recovery(mddev);
  1842. handled = 0;
  1843. spin_lock_irq(&conf->device_lock);
  1844. while (1) {
  1845. struct list_head *first;
  1846. if (conf->seq_flush - conf->seq_write > 0) {
  1847. int seq = conf->seq_flush;
  1848. spin_unlock_irq(&conf->device_lock);
  1849. bitmap_unplug(mddev->bitmap);
  1850. spin_lock_irq(&conf->device_lock);
  1851. conf->seq_write = seq;
  1852. activate_bit_delay(conf);
  1853. }
  1854. if (list_empty(&conf->handle_list) &&
  1855. atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
  1856. !blk_queue_plugged(mddev->queue) &&
  1857. !list_empty(&conf->delayed_list))
  1858. raid5_activate_delayed(conf);
  1859. if (list_empty(&conf->handle_list))
  1860. break;
  1861. first = conf->handle_list.next;
  1862. sh = list_entry(first, struct stripe_head, lru);
  1863. list_del_init(first);
  1864. atomic_inc(&sh->count);
  1865. if (atomic_read(&sh->count)!= 1)
  1866. BUG();
  1867. spin_unlock_irq(&conf->device_lock);
  1868. handled++;
  1869. handle_stripe(sh);
  1870. release_stripe(sh);
  1871. spin_lock_irq(&conf->device_lock);
  1872. }
  1873. PRINTK("%d stripes handled\n", handled);
  1874. spin_unlock_irq(&conf->device_lock);
  1875. unplug_slaves(mddev);
  1876. PRINTK("--- raid5d inactive\n");
  1877. }
  1878. static ssize_t
  1879. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  1880. {
  1881. raid5_conf_t *conf = mddev_to_conf(mddev);
  1882. if (conf)
  1883. return sprintf(page, "%d\n", conf->max_nr_stripes);
  1884. else
  1885. return 0;
  1886. }
  1887. static ssize_t
  1888. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  1889. {
  1890. raid5_conf_t *conf = mddev_to_conf(mddev);
  1891. char *end;
  1892. int new;
  1893. if (len >= PAGE_SIZE)
  1894. return -EINVAL;
  1895. if (!conf)
  1896. return -ENODEV;
  1897. new = simple_strtoul(page, &end, 10);
  1898. if (!*page || (*end && *end != '\n') )
  1899. return -EINVAL;
  1900. if (new <= 16 || new > 32768)
  1901. return -EINVAL;
  1902. while (new < conf->max_nr_stripes) {
  1903. if (drop_one_stripe(conf))
  1904. conf->max_nr_stripes--;
  1905. else
  1906. break;
  1907. }
  1908. while (new > conf->max_nr_stripes) {
  1909. if (grow_one_stripe(conf))
  1910. conf->max_nr_stripes++;
  1911. else break;
  1912. }
  1913. return len;
  1914. }
  1915. static struct md_sysfs_entry
  1916. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  1917. raid5_show_stripe_cache_size,
  1918. raid5_store_stripe_cache_size);
  1919. static ssize_t
  1920. stripe_cache_active_show(mddev_t *mddev, char *page)
  1921. {
  1922. raid5_conf_t *conf = mddev_to_conf(mddev);
  1923. if (conf)
  1924. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  1925. else
  1926. return 0;
  1927. }
  1928. static struct md_sysfs_entry
  1929. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  1930. static struct attribute *raid5_attrs[] = {
  1931. &raid5_stripecache_size.attr,
  1932. &raid5_stripecache_active.attr,
  1933. NULL,
  1934. };
  1935. static struct attribute_group raid5_attrs_group = {
  1936. .name = NULL,
  1937. .attrs = raid5_attrs,
  1938. };
  1939. static int run(mddev_t *mddev)
  1940. {
  1941. raid5_conf_t *conf;
  1942. int raid_disk, memory;
  1943. mdk_rdev_t *rdev;
  1944. struct disk_info *disk;
  1945. struct list_head *tmp;
  1946. if (mddev->level != 5 && mddev->level != 4) {
  1947. printk(KERN_ERR "raid5: %s: raid level not set to 4/5 (%d)\n",
  1948. mdname(mddev), mddev->level);
  1949. return -EIO;
  1950. }
  1951. if (mddev->reshape_position != MaxSector) {
  1952. /* Check that we can continue the reshape.
  1953. * Currently only disks can change, it must
  1954. * increase, and we must be past the point where
  1955. * a stripe over-writes itself
  1956. */
  1957. sector_t here_new, here_old;
  1958. int old_disks;
  1959. if (mddev->new_level != mddev->level ||
  1960. mddev->new_layout != mddev->layout ||
  1961. mddev->new_chunk != mddev->chunk_size) {
  1962. printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
  1963. mdname(mddev));
  1964. return -EINVAL;
  1965. }
  1966. if (mddev->delta_disks <= 0) {
  1967. printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
  1968. mdname(mddev));
  1969. return -EINVAL;
  1970. }
  1971. old_disks = mddev->raid_disks - mddev->delta_disks;
  1972. /* reshape_position must be on a new-stripe boundary, and one
  1973. * further up in new geometry must map after here in old geometry.
  1974. */
  1975. here_new = mddev->reshape_position;
  1976. if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
  1977. printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
  1978. return -EINVAL;
  1979. }
  1980. /* here_new is the stripe we will write to */
  1981. here_old = mddev->reshape_position;
  1982. sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
  1983. /* here_old is the first stripe that we might need to read from */
  1984. if (here_new >= here_old) {
  1985. /* Reading from the same stripe as writing to - bad */
  1986. printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
  1987. return -EINVAL;
  1988. }
  1989. printk(KERN_INFO "raid5: reshape will continue\n");
  1990. /* OK, we should be able to continue; */
  1991. }
  1992. mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
  1993. if ((conf = mddev->private) == NULL)
  1994. goto abort;
  1995. if (mddev->reshape_position == MaxSector) {
  1996. conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
  1997. } else {
  1998. conf->raid_disks = mddev->raid_disks;
  1999. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  2000. }
  2001. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  2002. GFP_KERNEL);
  2003. if (!conf->disks)
  2004. goto abort;
  2005. conf->mddev = mddev;
  2006. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  2007. goto abort;
  2008. spin_lock_init(&conf->device_lock);
  2009. init_waitqueue_head(&conf->wait_for_stripe);
  2010. init_waitqueue_head(&conf->wait_for_overlap);
  2011. INIT_LIST_HEAD(&conf->handle_list);
  2012. INIT_LIST_HEAD(&conf->delayed_list);
  2013. INIT_LIST_HEAD(&conf->bitmap_list);
  2014. INIT_LIST_HEAD(&conf->inactive_list);
  2015. atomic_set(&conf->active_stripes, 0);
  2016. atomic_set(&conf->preread_active_stripes, 0);
  2017. PRINTK("raid5: run(%s) called.\n", mdname(mddev));
  2018. ITERATE_RDEV(mddev,rdev,tmp) {
  2019. raid_disk = rdev->raid_disk;
  2020. if (raid_disk >= conf->raid_disks
  2021. || raid_disk < 0)
  2022. continue;
  2023. disk = conf->disks + raid_disk;
  2024. disk->rdev = rdev;
  2025. if (test_bit(In_sync, &rdev->flags)) {
  2026. char b[BDEVNAME_SIZE];
  2027. printk(KERN_INFO "raid5: device %s operational as raid"
  2028. " disk %d\n", bdevname(rdev->bdev,b),
  2029. raid_disk);
  2030. conf->working_disks++;
  2031. }
  2032. }
  2033. /*
  2034. * 0 for a fully functional array, 1 for a degraded array.
  2035. */
  2036. mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
  2037. conf->mddev = mddev;
  2038. conf->chunk_size = mddev->chunk_size;
  2039. conf->level = mddev->level;
  2040. conf->algorithm = mddev->layout;
  2041. conf->max_nr_stripes = NR_STRIPES;
  2042. conf->expand_progress = mddev->reshape_position;
  2043. /* device size must be a multiple of chunk size */
  2044. mddev->size &= ~(mddev->chunk_size/1024 -1);
  2045. mddev->resync_max_sectors = mddev->size << 1;
  2046. if (!conf->chunk_size || conf->chunk_size % 4) {
  2047. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  2048. conf->chunk_size, mdname(mddev));
  2049. goto abort;
  2050. }
  2051. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  2052. printk(KERN_ERR
  2053. "raid5: unsupported parity algorithm %d for %s\n",
  2054. conf->algorithm, mdname(mddev));
  2055. goto abort;
  2056. }
  2057. if (mddev->degraded > 1) {
  2058. printk(KERN_ERR "raid5: not enough operational devices for %s"
  2059. " (%d/%d failed)\n",
  2060. mdname(mddev), conf->failed_disks, conf->raid_disks);
  2061. goto abort;
  2062. }
  2063. if (mddev->degraded == 1 &&
  2064. mddev->recovery_cp != MaxSector) {
  2065. if (mddev->ok_start_degraded)
  2066. printk(KERN_WARNING
  2067. "raid5: starting dirty degraded array: %s"
  2068. "- data corruption possible.\n",
  2069. mdname(mddev));
  2070. else {
  2071. printk(KERN_ERR
  2072. "raid5: cannot start dirty degraded array for %s\n",
  2073. mdname(mddev));
  2074. goto abort;
  2075. }
  2076. }
  2077. {
  2078. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  2079. if (!mddev->thread) {
  2080. printk(KERN_ERR
  2081. "raid5: couldn't allocate thread for %s\n",
  2082. mdname(mddev));
  2083. goto abort;
  2084. }
  2085. }
  2086. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  2087. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  2088. if (grow_stripes(conf, conf->max_nr_stripes)) {
  2089. printk(KERN_ERR
  2090. "raid5: couldn't allocate %dkB for buffers\n", memory);
  2091. shrink_stripes(conf);
  2092. md_unregister_thread(mddev->thread);
  2093. goto abort;
  2094. } else
  2095. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  2096. memory, mdname(mddev));
  2097. if (mddev->degraded == 0)
  2098. printk("raid5: raid level %d set %s active with %d out of %d"
  2099. " devices, algorithm %d\n", conf->level, mdname(mddev),
  2100. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  2101. conf->algorithm);
  2102. else
  2103. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  2104. " out of %d devices, algorithm %d\n", conf->level,
  2105. mdname(mddev), mddev->raid_disks - mddev->degraded,
  2106. mddev->raid_disks, conf->algorithm);
  2107. print_raid5_conf(conf);
  2108. if (conf->expand_progress != MaxSector) {
  2109. printk("...ok start reshape thread\n");
  2110. conf->expand_lo = conf->expand_progress;
  2111. atomic_set(&conf->reshape_stripes, 0);
  2112. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  2113. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  2114. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  2115. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  2116. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  2117. "%s_reshape");
  2118. /* FIXME if md_register_thread fails?? */
  2119. md_wakeup_thread(mddev->sync_thread);
  2120. }
  2121. /* read-ahead size must cover two whole stripes, which is
  2122. * 2 * (n-1) * chunksize where 'n' is the number of raid devices
  2123. */
  2124. {
  2125. int stripe = (mddev->raid_disks-1) * mddev->chunk_size
  2126. / PAGE_SIZE;
  2127. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  2128. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  2129. }
  2130. /* Ok, everything is just fine now */
  2131. sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
  2132. mddev->queue->unplug_fn = raid5_unplug_device;
  2133. mddev->queue->issue_flush_fn = raid5_issue_flush;
  2134. mddev->array_size = mddev->size * (conf->previous_raid_disks - 1);
  2135. return 0;
  2136. abort:
  2137. if (conf) {
  2138. print_raid5_conf(conf);
  2139. kfree(conf->disks);
  2140. kfree(conf->stripe_hashtbl);
  2141. kfree(conf);
  2142. }
  2143. mddev->private = NULL;
  2144. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  2145. return -EIO;
  2146. }
  2147. static int stop(mddev_t *mddev)
  2148. {
  2149. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2150. md_unregister_thread(mddev->thread);
  2151. mddev->thread = NULL;
  2152. shrink_stripes(conf);
  2153. kfree(conf->stripe_hashtbl);
  2154. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  2155. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  2156. kfree(conf->disks);
  2157. kfree(conf);
  2158. mddev->private = NULL;
  2159. return 0;
  2160. }
  2161. #if RAID5_DEBUG
  2162. static void print_sh (struct stripe_head *sh)
  2163. {
  2164. int i;
  2165. printk("sh %llu, pd_idx %d, state %ld.\n",
  2166. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  2167. printk("sh %llu, count %d.\n",
  2168. (unsigned long long)sh->sector, atomic_read(&sh->count));
  2169. printk("sh %llu, ", (unsigned long long)sh->sector);
  2170. for (i = 0; i < sh->disks; i++) {
  2171. printk("(cache%d: %p %ld) ",
  2172. i, sh->dev[i].page, sh->dev[i].flags);
  2173. }
  2174. printk("\n");
  2175. }
  2176. static void printall (raid5_conf_t *conf)
  2177. {
  2178. struct stripe_head *sh;
  2179. struct hlist_node *hn;
  2180. int i;
  2181. spin_lock_irq(&conf->device_lock);
  2182. for (i = 0; i < NR_HASH; i++) {
  2183. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  2184. if (sh->raid_conf != conf)
  2185. continue;
  2186. print_sh(sh);
  2187. }
  2188. }
  2189. spin_unlock_irq(&conf->device_lock);
  2190. }
  2191. #endif
  2192. static void status (struct seq_file *seq, mddev_t *mddev)
  2193. {
  2194. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2195. int i;
  2196. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  2197. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
  2198. for (i = 0; i < conf->raid_disks; i++)
  2199. seq_printf (seq, "%s",
  2200. conf->disks[i].rdev &&
  2201. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  2202. seq_printf (seq, "]");
  2203. #if RAID5_DEBUG
  2204. #define D(x) \
  2205. seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
  2206. printall(conf);
  2207. #endif
  2208. }
  2209. static void print_raid5_conf (raid5_conf_t *conf)
  2210. {
  2211. int i;
  2212. struct disk_info *tmp;
  2213. printk("RAID5 conf printout:\n");
  2214. if (!conf) {
  2215. printk("(conf==NULL)\n");
  2216. return;
  2217. }
  2218. printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
  2219. conf->working_disks, conf->failed_disks);
  2220. for (i = 0; i < conf->raid_disks; i++) {
  2221. char b[BDEVNAME_SIZE];
  2222. tmp = conf->disks + i;
  2223. if (tmp->rdev)
  2224. printk(" disk %d, o:%d, dev:%s\n",
  2225. i, !test_bit(Faulty, &tmp->rdev->flags),
  2226. bdevname(tmp->rdev->bdev,b));
  2227. }
  2228. }
  2229. static int raid5_spare_active(mddev_t *mddev)
  2230. {
  2231. int i;
  2232. raid5_conf_t *conf = mddev->private;
  2233. struct disk_info *tmp;
  2234. for (i = 0; i < conf->raid_disks; i++) {
  2235. tmp = conf->disks + i;
  2236. if (tmp->rdev
  2237. && !test_bit(Faulty, &tmp->rdev->flags)
  2238. && !test_bit(In_sync, &tmp->rdev->flags)) {
  2239. mddev->degraded--;
  2240. conf->failed_disks--;
  2241. conf->working_disks++;
  2242. set_bit(In_sync, &tmp->rdev->flags);
  2243. }
  2244. }
  2245. print_raid5_conf(conf);
  2246. return 0;
  2247. }
  2248. static int raid5_remove_disk(mddev_t *mddev, int number)
  2249. {
  2250. raid5_conf_t *conf = mddev->private;
  2251. int err = 0;
  2252. mdk_rdev_t *rdev;
  2253. struct disk_info *p = conf->disks + number;
  2254. print_raid5_conf(conf);
  2255. rdev = p->rdev;
  2256. if (rdev) {
  2257. if (test_bit(In_sync, &rdev->flags) ||
  2258. atomic_read(&rdev->nr_pending)) {
  2259. err = -EBUSY;
  2260. goto abort;
  2261. }
  2262. p->rdev = NULL;
  2263. synchronize_rcu();
  2264. if (atomic_read(&rdev->nr_pending)) {
  2265. /* lost the race, try later */
  2266. err = -EBUSY;
  2267. p->rdev = rdev;
  2268. }
  2269. }
  2270. abort:
  2271. print_raid5_conf(conf);
  2272. return err;
  2273. }
  2274. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  2275. {
  2276. raid5_conf_t *conf = mddev->private;
  2277. int found = 0;
  2278. int disk;
  2279. struct disk_info *p;
  2280. if (mddev->degraded > 1)
  2281. /* no point adding a device */
  2282. return 0;
  2283. /*
  2284. * find the disk ...
  2285. */
  2286. for (disk=0; disk < conf->raid_disks; disk++)
  2287. if ((p=conf->disks + disk)->rdev == NULL) {
  2288. clear_bit(In_sync, &rdev->flags);
  2289. rdev->raid_disk = disk;
  2290. found = 1;
  2291. if (rdev->saved_raid_disk != disk)
  2292. conf->fullsync = 1;
  2293. rcu_assign_pointer(p->rdev, rdev);
  2294. break;
  2295. }
  2296. print_raid5_conf(conf);
  2297. return found;
  2298. }
  2299. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  2300. {
  2301. /* no resync is happening, and there is enough space
  2302. * on all devices, so we can resize.
  2303. * We need to make sure resync covers any new space.
  2304. * If the array is shrinking we should possibly wait until
  2305. * any io in the removed space completes, but it hardly seems
  2306. * worth it.
  2307. */
  2308. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  2309. mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
  2310. set_capacity(mddev->gendisk, mddev->array_size << 1);
  2311. mddev->changed = 1;
  2312. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  2313. mddev->recovery_cp = mddev->size << 1;
  2314. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  2315. }
  2316. mddev->size = sectors /2;
  2317. mddev->resync_max_sectors = sectors;
  2318. return 0;
  2319. }
  2320. #ifdef CONFIG_MD_RAID5_RESHAPE
  2321. static int raid5_reshape(mddev_t *mddev, int raid_disks)
  2322. {
  2323. raid5_conf_t *conf = mddev_to_conf(mddev);
  2324. int err;
  2325. mdk_rdev_t *rdev;
  2326. struct list_head *rtmp;
  2327. int spares = 0;
  2328. int added_devices = 0;
  2329. if (mddev->degraded ||
  2330. test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  2331. return -EBUSY;
  2332. if (conf->raid_disks > raid_disks)
  2333. return -EINVAL; /* Cannot shrink array yet */
  2334. if (conf->raid_disks == raid_disks)
  2335. return 0; /* nothing to do */
  2336. /* Can only proceed if there are plenty of stripe_heads.
  2337. * We need a minimum of one full stripe,, and for sensible progress
  2338. * it is best to have about 4 times that.
  2339. * If we require 4 times, then the default 256 4K stripe_heads will
  2340. * allow for chunk sizes up to 256K, which is probably OK.
  2341. * If the chunk size is greater, user-space should request more
  2342. * stripe_heads first.
  2343. */
  2344. if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
  2345. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  2346. (mddev->chunk_size / STRIPE_SIZE)*4);
  2347. return -ENOSPC;
  2348. }
  2349. ITERATE_RDEV(mddev, rdev, rtmp)
  2350. if (rdev->raid_disk < 0 &&
  2351. !test_bit(Faulty, &rdev->flags))
  2352. spares++;
  2353. if (conf->raid_disks + spares < raid_disks-1)
  2354. /* Not enough devices even to make a degraded array
  2355. * of that size
  2356. */
  2357. return -EINVAL;
  2358. err = resize_stripes(conf, raid_disks);
  2359. if (err)
  2360. return err;
  2361. atomic_set(&conf->reshape_stripes, 0);
  2362. spin_lock_irq(&conf->device_lock);
  2363. conf->previous_raid_disks = conf->raid_disks;
  2364. conf->raid_disks = raid_disks;
  2365. conf->expand_progress = 0;
  2366. conf->expand_lo = 0;
  2367. spin_unlock_irq(&conf->device_lock);
  2368. /* Add some new drives, as many as will fit.
  2369. * We know there are enough to make the newly sized array work.
  2370. */
  2371. ITERATE_RDEV(mddev, rdev, rtmp)
  2372. if (rdev->raid_disk < 0 &&
  2373. !test_bit(Faulty, &rdev->flags)) {
  2374. if (raid5_add_disk(mddev, rdev)) {
  2375. char nm[20];
  2376. set_bit(In_sync, &rdev->flags);
  2377. conf->working_disks++;
  2378. added_devices++;
  2379. sprintf(nm, "rd%d", rdev->raid_disk);
  2380. sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
  2381. } else
  2382. break;
  2383. }
  2384. mddev->degraded = (raid_disks - conf->previous_raid_disks) - added_devices;
  2385. mddev->new_chunk = mddev->chunk_size;
  2386. mddev->new_layout = mddev->layout;
  2387. mddev->new_level = mddev->level;
  2388. mddev->raid_disks = raid_disks;
  2389. mddev->delta_disks = raid_disks - conf->previous_raid_disks;
  2390. mddev->reshape_position = 0;
  2391. mddev->sb_dirty = 1;
  2392. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  2393. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  2394. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  2395. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  2396. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  2397. "%s_reshape");
  2398. if (!mddev->sync_thread) {
  2399. mddev->recovery = 0;
  2400. spin_lock_irq(&conf->device_lock);
  2401. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  2402. mddev->delta_disks = 0;
  2403. conf->expand_progress = MaxSector;
  2404. spin_unlock_irq(&conf->device_lock);
  2405. return -EAGAIN;
  2406. }
  2407. md_wakeup_thread(mddev->sync_thread);
  2408. md_new_event(mddev);
  2409. return 0;
  2410. }
  2411. #endif
  2412. static void end_reshape(raid5_conf_t *conf)
  2413. {
  2414. struct block_device *bdev;
  2415. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  2416. conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
  2417. set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
  2418. conf->mddev->changed = 1;
  2419. bdev = bdget_disk(conf->mddev->gendisk, 0);
  2420. if (bdev) {
  2421. mutex_lock(&bdev->bd_inode->i_mutex);
  2422. i_size_write(bdev->bd_inode, conf->mddev->array_size << 10);
  2423. mutex_unlock(&bdev->bd_inode->i_mutex);
  2424. bdput(bdev);
  2425. }
  2426. spin_lock_irq(&conf->device_lock);
  2427. conf->expand_progress = MaxSector;
  2428. spin_unlock_irq(&conf->device_lock);
  2429. conf->mddev->reshape_position = MaxSector;
  2430. }
  2431. }
  2432. static void raid5_quiesce(mddev_t *mddev, int state)
  2433. {
  2434. raid5_conf_t *conf = mddev_to_conf(mddev);
  2435. switch(state) {
  2436. case 1: /* stop all writes */
  2437. spin_lock_irq(&conf->device_lock);
  2438. conf->quiesce = 1;
  2439. wait_event_lock_irq(conf->wait_for_stripe,
  2440. atomic_read(&conf->active_stripes) == 0,
  2441. conf->device_lock, /* nothing */);
  2442. spin_unlock_irq(&conf->device_lock);
  2443. break;
  2444. case 0: /* re-enable writes */
  2445. spin_lock_irq(&conf->device_lock);
  2446. conf->quiesce = 0;
  2447. wake_up(&conf->wait_for_stripe);
  2448. spin_unlock_irq(&conf->device_lock);
  2449. break;
  2450. }
  2451. }
  2452. static struct mdk_personality raid5_personality =
  2453. {
  2454. .name = "raid5",
  2455. .level = 5,
  2456. .owner = THIS_MODULE,
  2457. .make_request = make_request,
  2458. .run = run,
  2459. .stop = stop,
  2460. .status = status,
  2461. .error_handler = error,
  2462. .hot_add_disk = raid5_add_disk,
  2463. .hot_remove_disk= raid5_remove_disk,
  2464. .spare_active = raid5_spare_active,
  2465. .sync_request = sync_request,
  2466. .resize = raid5_resize,
  2467. #ifdef CONFIG_MD_RAID5_RESHAPE
  2468. .reshape = raid5_reshape,
  2469. #endif
  2470. .quiesce = raid5_quiesce,
  2471. };
  2472. static struct mdk_personality raid4_personality =
  2473. {
  2474. .name = "raid4",
  2475. .level = 4,
  2476. .owner = THIS_MODULE,
  2477. .make_request = make_request,
  2478. .run = run,
  2479. .stop = stop,
  2480. .status = status,
  2481. .error_handler = error,
  2482. .hot_add_disk = raid5_add_disk,
  2483. .hot_remove_disk= raid5_remove_disk,
  2484. .spare_active = raid5_spare_active,
  2485. .sync_request = sync_request,
  2486. .resize = raid5_resize,
  2487. .quiesce = raid5_quiesce,
  2488. };
  2489. static int __init raid5_init(void)
  2490. {
  2491. register_md_personality(&raid5_personality);
  2492. register_md_personality(&raid4_personality);
  2493. return 0;
  2494. }
  2495. static void raid5_exit(void)
  2496. {
  2497. unregister_md_personality(&raid5_personality);
  2498. unregister_md_personality(&raid4_personality);
  2499. }
  2500. module_init(raid5_init);
  2501. module_exit(raid5_exit);
  2502. MODULE_LICENSE("GPL");
  2503. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  2504. MODULE_ALIAS("md-raid5");
  2505. MODULE_ALIAS("md-raid4");
  2506. MODULE_ALIAS("md-level-5");
  2507. MODULE_ALIAS("md-level-4");