raid5.c 78 KB

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