raid5.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751
  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. * Copyright (C) 2002, 2003 H. Peter Anvin
  6. *
  7. * RAID-4/5/6 management functions.
  8. * Thanks to Penguin Computing for making the RAID-6 development possible
  9. * by donating a test server!
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * (for example /usr/src/linux/COPYING); if not, write to the Free
  18. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/highmem.h>
  23. #include <linux/bitops.h>
  24. #include <linux/kthread.h>
  25. #include <asm/atomic.h>
  26. #include "raid6.h"
  27. #include <linux/raid/bitmap.h>
  28. /*
  29. * Stripe cache
  30. */
  31. #define NR_STRIPES 256
  32. #define STRIPE_SIZE PAGE_SIZE
  33. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  34. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  35. #define IO_THRESHOLD 1
  36. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  37. #define HASH_MASK (NR_HASH - 1)
  38. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  39. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  40. * order without overlap. There may be several bio's per stripe+device, and
  41. * a bio could span several devices.
  42. * When walking this list for a particular stripe+device, we must never proceed
  43. * beyond a bio that extends past this device, as the next bio might no longer
  44. * be valid.
  45. * This macro is used to determine the 'next' bio in the list, given the sector
  46. * of the current stripe+device
  47. */
  48. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  49. /*
  50. * The following can be used to debug the driver
  51. */
  52. #define RAID5_DEBUG 0
  53. #define RAID5_PARANOIA 1
  54. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  55. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  56. #else
  57. # define CHECK_DEVLOCK()
  58. #endif
  59. #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
  60. #if RAID5_DEBUG
  61. #define inline
  62. #define __inline__
  63. #endif
  64. #if !RAID6_USE_EMPTY_ZERO_PAGE
  65. /* In .bss so it's zeroed */
  66. const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
  67. #endif
  68. static inline int raid6_next_disk(int disk, int raid_disks)
  69. {
  70. disk++;
  71. return (disk < raid_disks) ? disk : 0;
  72. }
  73. static void print_raid5_conf (raid5_conf_t *conf);
  74. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  75. {
  76. if (atomic_dec_and_test(&sh->count)) {
  77. BUG_ON(!list_empty(&sh->lru));
  78. BUG_ON(atomic_read(&conf->active_stripes)==0);
  79. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  80. if (test_bit(STRIPE_DELAYED, &sh->state))
  81. list_add_tail(&sh->lru, &conf->delayed_list);
  82. else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  83. conf->seq_write == sh->bm_seq)
  84. list_add_tail(&sh->lru, &conf->bitmap_list);
  85. else {
  86. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  87. list_add_tail(&sh->lru, &conf->handle_list);
  88. }
  89. md_wakeup_thread(conf->mddev->thread);
  90. } else {
  91. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  92. atomic_dec(&conf->preread_active_stripes);
  93. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  94. md_wakeup_thread(conf->mddev->thread);
  95. }
  96. atomic_dec(&conf->active_stripes);
  97. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  98. list_add_tail(&sh->lru, &conf->inactive_list);
  99. wake_up(&conf->wait_for_stripe);
  100. }
  101. }
  102. }
  103. }
  104. static void release_stripe(struct stripe_head *sh)
  105. {
  106. raid5_conf_t *conf = sh->raid_conf;
  107. unsigned long flags;
  108. spin_lock_irqsave(&conf->device_lock, flags);
  109. __release_stripe(conf, sh);
  110. spin_unlock_irqrestore(&conf->device_lock, flags);
  111. }
  112. static inline void remove_hash(struct stripe_head *sh)
  113. {
  114. PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  115. hlist_del_init(&sh->hash);
  116. }
  117. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  118. {
  119. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  120. PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
  121. CHECK_DEVLOCK();
  122. hlist_add_head(&sh->hash, hp);
  123. }
  124. /* find an idle stripe, make sure it is unhashed, and return it. */
  125. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  126. {
  127. struct stripe_head *sh = NULL;
  128. struct list_head *first;
  129. CHECK_DEVLOCK();
  130. if (list_empty(&conf->inactive_list))
  131. goto out;
  132. first = conf->inactive_list.next;
  133. sh = list_entry(first, struct stripe_head, lru);
  134. list_del_init(first);
  135. remove_hash(sh);
  136. atomic_inc(&conf->active_stripes);
  137. out:
  138. return sh;
  139. }
  140. static void shrink_buffers(struct stripe_head *sh, int num)
  141. {
  142. struct page *p;
  143. int i;
  144. for (i=0; i<num ; i++) {
  145. p = sh->dev[i].page;
  146. if (!p)
  147. continue;
  148. sh->dev[i].page = NULL;
  149. put_page(p);
  150. }
  151. }
  152. static int grow_buffers(struct stripe_head *sh, int num)
  153. {
  154. int i;
  155. for (i=0; i<num; i++) {
  156. struct page *page;
  157. if (!(page = alloc_page(GFP_KERNEL))) {
  158. return 1;
  159. }
  160. sh->dev[i].page = page;
  161. }
  162. return 0;
  163. }
  164. static void raid5_build_block (struct stripe_head *sh, int i);
  165. static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
  166. {
  167. raid5_conf_t *conf = sh->raid_conf;
  168. int i;
  169. BUG_ON(atomic_read(&sh->count) != 0);
  170. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  171. CHECK_DEVLOCK();
  172. PRINTK("init_stripe called, stripe %llu\n",
  173. (unsigned long long)sh->sector);
  174. remove_hash(sh);
  175. sh->sector = sector;
  176. sh->pd_idx = pd_idx;
  177. sh->state = 0;
  178. sh->disks = disks;
  179. for (i = sh->disks; i--; ) {
  180. struct r5dev *dev = &sh->dev[i];
  181. if (dev->toread || dev->towrite || dev->written ||
  182. test_bit(R5_LOCKED, &dev->flags)) {
  183. printk("sector=%llx i=%d %p %p %p %d\n",
  184. (unsigned long long)sh->sector, i, dev->toread,
  185. dev->towrite, dev->written,
  186. test_bit(R5_LOCKED, &dev->flags));
  187. BUG();
  188. }
  189. dev->flags = 0;
  190. raid5_build_block(sh, i);
  191. }
  192. insert_hash(conf, sh);
  193. }
  194. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
  195. {
  196. struct stripe_head *sh;
  197. struct hlist_node *hn;
  198. CHECK_DEVLOCK();
  199. PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
  200. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  201. if (sh->sector == sector && sh->disks == disks)
  202. return sh;
  203. PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
  204. return NULL;
  205. }
  206. static void unplug_slaves(mddev_t *mddev);
  207. static void raid5_unplug_device(request_queue_t *q);
  208. static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
  209. int pd_idx, int noblock)
  210. {
  211. struct stripe_head *sh;
  212. PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
  213. spin_lock_irq(&conf->device_lock);
  214. do {
  215. wait_event_lock_irq(conf->wait_for_stripe,
  216. conf->quiesce == 0,
  217. conf->device_lock, /* nothing */);
  218. sh = __find_stripe(conf, sector, disks);
  219. if (!sh) {
  220. if (!conf->inactive_blocked)
  221. sh = get_free_stripe(conf);
  222. if (noblock && sh == NULL)
  223. break;
  224. if (!sh) {
  225. conf->inactive_blocked = 1;
  226. wait_event_lock_irq(conf->wait_for_stripe,
  227. !list_empty(&conf->inactive_list) &&
  228. (atomic_read(&conf->active_stripes)
  229. < (conf->max_nr_stripes *3/4)
  230. || !conf->inactive_blocked),
  231. conf->device_lock,
  232. unplug_slaves(conf->mddev)
  233. );
  234. conf->inactive_blocked = 0;
  235. } else
  236. init_stripe(sh, sector, pd_idx, disks);
  237. } else {
  238. if (atomic_read(&sh->count)) {
  239. BUG_ON(!list_empty(&sh->lru));
  240. } else {
  241. if (!test_bit(STRIPE_HANDLE, &sh->state))
  242. atomic_inc(&conf->active_stripes);
  243. if (list_empty(&sh->lru))
  244. BUG();
  245. list_del_init(&sh->lru);
  246. }
  247. }
  248. } while (sh == NULL);
  249. if (sh)
  250. atomic_inc(&sh->count);
  251. spin_unlock_irq(&conf->device_lock);
  252. return sh;
  253. }
  254. static int grow_one_stripe(raid5_conf_t *conf)
  255. {
  256. struct stripe_head *sh;
  257. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  258. if (!sh)
  259. return 0;
  260. memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
  261. sh->raid_conf = conf;
  262. spin_lock_init(&sh->lock);
  263. if (grow_buffers(sh, conf->raid_disks)) {
  264. shrink_buffers(sh, conf->raid_disks);
  265. kmem_cache_free(conf->slab_cache, sh);
  266. return 0;
  267. }
  268. sh->disks = conf->raid_disks;
  269. /* we just created an active stripe so... */
  270. atomic_set(&sh->count, 1);
  271. atomic_inc(&conf->active_stripes);
  272. INIT_LIST_HEAD(&sh->lru);
  273. release_stripe(sh);
  274. return 1;
  275. }
  276. static int grow_stripes(raid5_conf_t *conf, int num)
  277. {
  278. kmem_cache_t *sc;
  279. int devs = conf->raid_disks;
  280. sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
  281. sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
  282. conf->active_name = 0;
  283. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  284. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  285. 0, 0, NULL, NULL);
  286. if (!sc)
  287. return 1;
  288. conf->slab_cache = sc;
  289. conf->pool_size = devs;
  290. while (num--)
  291. if (!grow_one_stripe(conf))
  292. return 1;
  293. return 0;
  294. }
  295. #ifdef CONFIG_MD_RAID5_RESHAPE
  296. static int resize_stripes(raid5_conf_t *conf, int newsize)
  297. {
  298. /* Make all the stripes able to hold 'newsize' devices.
  299. * New slots in each stripe get 'page' set to a new page.
  300. *
  301. * This happens in stages:
  302. * 1/ create a new kmem_cache and allocate the required number of
  303. * stripe_heads.
  304. * 2/ gather all the old stripe_heads and tranfer the pages across
  305. * to the new stripe_heads. This will have the side effect of
  306. * freezing the array as once all stripe_heads have been collected,
  307. * no IO will be possible. Old stripe heads are freed once their
  308. * pages have been transferred over, and the old kmem_cache is
  309. * freed when all stripes are done.
  310. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  311. * we simple return a failre status - no need to clean anything up.
  312. * 4/ allocate new pages for the new slots in the new stripe_heads.
  313. * If this fails, we don't bother trying the shrink the
  314. * stripe_heads down again, we just leave them as they are.
  315. * As each stripe_head is processed the new one is released into
  316. * active service.
  317. *
  318. * Once step2 is started, we cannot afford to wait for a write,
  319. * so we use GFP_NOIO allocations.
  320. */
  321. struct stripe_head *osh, *nsh;
  322. LIST_HEAD(newstripes);
  323. struct disk_info *ndisks;
  324. int err = 0;
  325. kmem_cache_t *sc;
  326. int i;
  327. if (newsize <= conf->pool_size)
  328. return 0; /* never bother to shrink */
  329. /* Step 1 */
  330. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  331. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  332. 0, 0, NULL, NULL);
  333. if (!sc)
  334. return -ENOMEM;
  335. for (i = conf->max_nr_stripes; i; i--) {
  336. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  337. if (!nsh)
  338. break;
  339. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  340. nsh->raid_conf = conf;
  341. spin_lock_init(&nsh->lock);
  342. list_add(&nsh->lru, &newstripes);
  343. }
  344. if (i) {
  345. /* didn't get enough, give up */
  346. while (!list_empty(&newstripes)) {
  347. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  348. list_del(&nsh->lru);
  349. kmem_cache_free(sc, nsh);
  350. }
  351. kmem_cache_destroy(sc);
  352. return -ENOMEM;
  353. }
  354. /* Step 2 - Must use GFP_NOIO now.
  355. * OK, we have enough stripes, start collecting inactive
  356. * stripes and copying them over
  357. */
  358. list_for_each_entry(nsh, &newstripes, lru) {
  359. spin_lock_irq(&conf->device_lock);
  360. wait_event_lock_irq(conf->wait_for_stripe,
  361. !list_empty(&conf->inactive_list),
  362. conf->device_lock,
  363. unplug_slaves(conf->mddev)
  364. );
  365. osh = get_free_stripe(conf);
  366. spin_unlock_irq(&conf->device_lock);
  367. atomic_set(&nsh->count, 1);
  368. for(i=0; i<conf->pool_size; i++)
  369. nsh->dev[i].page = osh->dev[i].page;
  370. for( ; i<newsize; i++)
  371. nsh->dev[i].page = NULL;
  372. kmem_cache_free(conf->slab_cache, osh);
  373. }
  374. kmem_cache_destroy(conf->slab_cache);
  375. /* Step 3.
  376. * At this point, we are holding all the stripes so the array
  377. * is completely stalled, so now is a good time to resize
  378. * conf->disks.
  379. */
  380. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  381. if (ndisks) {
  382. for (i=0; i<conf->raid_disks; i++)
  383. ndisks[i] = conf->disks[i];
  384. kfree(conf->disks);
  385. conf->disks = ndisks;
  386. } else
  387. err = -ENOMEM;
  388. /* Step 4, return new stripes to service */
  389. while(!list_empty(&newstripes)) {
  390. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  391. list_del_init(&nsh->lru);
  392. for (i=conf->raid_disks; i < newsize; i++)
  393. if (nsh->dev[i].page == NULL) {
  394. struct page *p = alloc_page(GFP_NOIO);
  395. nsh->dev[i].page = p;
  396. if (!p)
  397. err = -ENOMEM;
  398. }
  399. release_stripe(nsh);
  400. }
  401. /* critical section pass, GFP_NOIO no longer needed */
  402. conf->slab_cache = sc;
  403. conf->active_name = 1-conf->active_name;
  404. conf->pool_size = newsize;
  405. return err;
  406. }
  407. #endif
  408. static int drop_one_stripe(raid5_conf_t *conf)
  409. {
  410. struct stripe_head *sh;
  411. spin_lock_irq(&conf->device_lock);
  412. sh = get_free_stripe(conf);
  413. spin_unlock_irq(&conf->device_lock);
  414. if (!sh)
  415. return 0;
  416. BUG_ON(atomic_read(&sh->count));
  417. shrink_buffers(sh, conf->pool_size);
  418. kmem_cache_free(conf->slab_cache, sh);
  419. atomic_dec(&conf->active_stripes);
  420. return 1;
  421. }
  422. static void shrink_stripes(raid5_conf_t *conf)
  423. {
  424. while (drop_one_stripe(conf))
  425. ;
  426. if (conf->slab_cache)
  427. kmem_cache_destroy(conf->slab_cache);
  428. conf->slab_cache = NULL;
  429. }
  430. static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
  431. int error)
  432. {
  433. struct stripe_head *sh = bi->bi_private;
  434. raid5_conf_t *conf = sh->raid_conf;
  435. int disks = sh->disks, i;
  436. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  437. if (bi->bi_size)
  438. return 1;
  439. for (i=0 ; i<disks; i++)
  440. if (bi == &sh->dev[i].req)
  441. break;
  442. PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  443. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  444. uptodate);
  445. if (i == disks) {
  446. BUG();
  447. return 0;
  448. }
  449. if (uptodate) {
  450. #if 0
  451. struct bio *bio;
  452. unsigned long flags;
  453. spin_lock_irqsave(&conf->device_lock, flags);
  454. /* we can return a buffer if we bypassed the cache or
  455. * if the top buffer is not in highmem. If there are
  456. * multiple buffers, leave the extra work to
  457. * handle_stripe
  458. */
  459. buffer = sh->bh_read[i];
  460. if (buffer &&
  461. (!PageHighMem(buffer->b_page)
  462. || buffer->b_page == bh->b_page )
  463. ) {
  464. sh->bh_read[i] = buffer->b_reqnext;
  465. buffer->b_reqnext = NULL;
  466. } else
  467. buffer = NULL;
  468. spin_unlock_irqrestore(&conf->device_lock, flags);
  469. if (sh->bh_page[i]==bh->b_page)
  470. set_buffer_uptodate(bh);
  471. if (buffer) {
  472. if (buffer->b_page != bh->b_page)
  473. memcpy(buffer->b_data, bh->b_data, bh->b_size);
  474. buffer->b_end_io(buffer, 1);
  475. }
  476. #else
  477. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  478. #endif
  479. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  480. printk(KERN_INFO "raid5: read error corrected!!\n");
  481. clear_bit(R5_ReadError, &sh->dev[i].flags);
  482. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  483. }
  484. if (atomic_read(&conf->disks[i].rdev->read_errors))
  485. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  486. } else {
  487. int retry = 0;
  488. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  489. atomic_inc(&conf->disks[i].rdev->read_errors);
  490. if (conf->mddev->degraded)
  491. printk(KERN_WARNING "raid5: read error not correctable.\n");
  492. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  493. /* Oh, no!!! */
  494. printk(KERN_WARNING "raid5: read error NOT corrected!!\n");
  495. else if (atomic_read(&conf->disks[i].rdev->read_errors)
  496. > conf->max_nr_stripes)
  497. printk(KERN_WARNING
  498. "raid5: Too many read errors, failing device.\n");
  499. else
  500. retry = 1;
  501. if (retry)
  502. set_bit(R5_ReadError, &sh->dev[i].flags);
  503. else {
  504. clear_bit(R5_ReadError, &sh->dev[i].flags);
  505. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  506. md_error(conf->mddev, conf->disks[i].rdev);
  507. }
  508. }
  509. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  510. #if 0
  511. /* must restore b_page before unlocking buffer... */
  512. if (sh->bh_page[i] != bh->b_page) {
  513. bh->b_page = sh->bh_page[i];
  514. bh->b_data = page_address(bh->b_page);
  515. clear_buffer_uptodate(bh);
  516. }
  517. #endif
  518. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  519. set_bit(STRIPE_HANDLE, &sh->state);
  520. release_stripe(sh);
  521. return 0;
  522. }
  523. static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
  524. int error)
  525. {
  526. struct stripe_head *sh = bi->bi_private;
  527. raid5_conf_t *conf = sh->raid_conf;
  528. int disks = sh->disks, i;
  529. unsigned long flags;
  530. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  531. if (bi->bi_size)
  532. return 1;
  533. for (i=0 ; i<disks; i++)
  534. if (bi == &sh->dev[i].req)
  535. break;
  536. PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  537. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  538. uptodate);
  539. if (i == disks) {
  540. BUG();
  541. return 0;
  542. }
  543. spin_lock_irqsave(&conf->device_lock, flags);
  544. if (!uptodate)
  545. md_error(conf->mddev, conf->disks[i].rdev);
  546. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  547. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  548. set_bit(STRIPE_HANDLE, &sh->state);
  549. __release_stripe(conf, sh);
  550. spin_unlock_irqrestore(&conf->device_lock, flags);
  551. return 0;
  552. }
  553. static sector_t compute_blocknr(struct stripe_head *sh, int i);
  554. static void raid5_build_block (struct stripe_head *sh, int i)
  555. {
  556. struct r5dev *dev = &sh->dev[i];
  557. bio_init(&dev->req);
  558. dev->req.bi_io_vec = &dev->vec;
  559. dev->req.bi_vcnt++;
  560. dev->req.bi_max_vecs++;
  561. dev->vec.bv_page = dev->page;
  562. dev->vec.bv_len = STRIPE_SIZE;
  563. dev->vec.bv_offset = 0;
  564. dev->req.bi_sector = sh->sector;
  565. dev->req.bi_private = sh;
  566. dev->flags = 0;
  567. dev->sector = compute_blocknr(sh, i);
  568. }
  569. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  570. {
  571. char b[BDEVNAME_SIZE];
  572. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  573. PRINTK("raid5: error called\n");
  574. if (!test_bit(Faulty, &rdev->flags)) {
  575. mddev->sb_dirty = 1;
  576. if (test_bit(In_sync, &rdev->flags)) {
  577. conf->working_disks--;
  578. mddev->degraded++;
  579. conf->failed_disks++;
  580. clear_bit(In_sync, &rdev->flags);
  581. /*
  582. * if recovery was running, make sure it aborts.
  583. */
  584. set_bit(MD_RECOVERY_ERR, &mddev->recovery);
  585. }
  586. set_bit(Faulty, &rdev->flags);
  587. printk (KERN_ALERT
  588. "raid5: Disk failure on %s, disabling device."
  589. " Operation continuing on %d devices\n",
  590. bdevname(rdev->bdev,b), conf->working_disks);
  591. }
  592. }
  593. /*
  594. * Input: a 'big' sector number,
  595. * Output: index of the data and parity disk, and the sector # in them.
  596. */
  597. static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
  598. unsigned int data_disks, unsigned int * dd_idx,
  599. unsigned int * pd_idx, raid5_conf_t *conf)
  600. {
  601. long stripe;
  602. unsigned long chunk_number;
  603. unsigned int chunk_offset;
  604. sector_t new_sector;
  605. int sectors_per_chunk = conf->chunk_size >> 9;
  606. /* First compute the information on this sector */
  607. /*
  608. * Compute the chunk number and the sector offset inside the chunk
  609. */
  610. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  611. chunk_number = r_sector;
  612. BUG_ON(r_sector != chunk_number);
  613. /*
  614. * Compute the stripe number
  615. */
  616. stripe = chunk_number / data_disks;
  617. /*
  618. * Compute the data disk and parity disk indexes inside the stripe
  619. */
  620. *dd_idx = chunk_number % data_disks;
  621. /*
  622. * Select the parity disk based on the user selected algorithm.
  623. */
  624. switch(conf->level) {
  625. case 4:
  626. *pd_idx = data_disks;
  627. break;
  628. case 5:
  629. switch (conf->algorithm) {
  630. case ALGORITHM_LEFT_ASYMMETRIC:
  631. *pd_idx = data_disks - stripe % raid_disks;
  632. if (*dd_idx >= *pd_idx)
  633. (*dd_idx)++;
  634. break;
  635. case ALGORITHM_RIGHT_ASYMMETRIC:
  636. *pd_idx = stripe % raid_disks;
  637. if (*dd_idx >= *pd_idx)
  638. (*dd_idx)++;
  639. break;
  640. case ALGORITHM_LEFT_SYMMETRIC:
  641. *pd_idx = data_disks - stripe % raid_disks;
  642. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  643. break;
  644. case ALGORITHM_RIGHT_SYMMETRIC:
  645. *pd_idx = stripe % raid_disks;
  646. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  647. break;
  648. default:
  649. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  650. conf->algorithm);
  651. }
  652. break;
  653. case 6:
  654. /**** FIX THIS ****/
  655. switch (conf->algorithm) {
  656. case ALGORITHM_LEFT_ASYMMETRIC:
  657. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  658. if (*pd_idx == raid_disks-1)
  659. (*dd_idx)++; /* Q D D D P */
  660. else if (*dd_idx >= *pd_idx)
  661. (*dd_idx) += 2; /* D D P Q D */
  662. break;
  663. case ALGORITHM_RIGHT_ASYMMETRIC:
  664. *pd_idx = stripe % raid_disks;
  665. if (*pd_idx == raid_disks-1)
  666. (*dd_idx)++; /* Q D D D P */
  667. else if (*dd_idx >= *pd_idx)
  668. (*dd_idx) += 2; /* D D P Q D */
  669. break;
  670. case ALGORITHM_LEFT_SYMMETRIC:
  671. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  672. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  673. break;
  674. case ALGORITHM_RIGHT_SYMMETRIC:
  675. *pd_idx = stripe % raid_disks;
  676. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  677. break;
  678. default:
  679. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  680. conf->algorithm);
  681. }
  682. break;
  683. }
  684. /*
  685. * Finally, compute the new sector number
  686. */
  687. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  688. return new_sector;
  689. }
  690. static sector_t compute_blocknr(struct stripe_head *sh, int i)
  691. {
  692. raid5_conf_t *conf = sh->raid_conf;
  693. int raid_disks = sh->disks, data_disks = raid_disks - 1;
  694. sector_t new_sector = sh->sector, check;
  695. int sectors_per_chunk = conf->chunk_size >> 9;
  696. sector_t stripe;
  697. int chunk_offset;
  698. int chunk_number, dummy1, dummy2, dd_idx = i;
  699. sector_t r_sector;
  700. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  701. stripe = new_sector;
  702. BUG_ON(new_sector != stripe);
  703. if (i == sh->pd_idx)
  704. return 0;
  705. switch(conf->level) {
  706. case 4: break;
  707. case 5:
  708. switch (conf->algorithm) {
  709. case ALGORITHM_LEFT_ASYMMETRIC:
  710. case ALGORITHM_RIGHT_ASYMMETRIC:
  711. if (i > sh->pd_idx)
  712. i--;
  713. break;
  714. case ALGORITHM_LEFT_SYMMETRIC:
  715. case ALGORITHM_RIGHT_SYMMETRIC:
  716. if (i < sh->pd_idx)
  717. i += raid_disks;
  718. i -= (sh->pd_idx + 1);
  719. break;
  720. default:
  721. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  722. conf->algorithm);
  723. }
  724. break;
  725. case 6:
  726. data_disks = raid_disks - 2;
  727. if (i == raid6_next_disk(sh->pd_idx, raid_disks))
  728. return 0; /* It is the Q disk */
  729. switch (conf->algorithm) {
  730. case ALGORITHM_LEFT_ASYMMETRIC:
  731. case ALGORITHM_RIGHT_ASYMMETRIC:
  732. if (sh->pd_idx == raid_disks-1)
  733. i--; /* Q D D D P */
  734. else if (i > sh->pd_idx)
  735. i -= 2; /* D D P Q D */
  736. break;
  737. case ALGORITHM_LEFT_SYMMETRIC:
  738. case ALGORITHM_RIGHT_SYMMETRIC:
  739. if (sh->pd_idx == raid_disks-1)
  740. i--; /* Q D D D P */
  741. else {
  742. /* D D P Q D */
  743. if (i < sh->pd_idx)
  744. i += raid_disks;
  745. i -= (sh->pd_idx + 2);
  746. }
  747. break;
  748. default:
  749. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  750. conf->algorithm);
  751. }
  752. break;
  753. }
  754. chunk_number = stripe * data_disks + i;
  755. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  756. check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
  757. if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
  758. printk(KERN_ERR "compute_blocknr: map not correct\n");
  759. return 0;
  760. }
  761. return r_sector;
  762. }
  763. /*
  764. * Copy data between a page in the stripe cache, and one or more bion
  765. * The page could align with the middle of the bio, or there could be
  766. * several bion, each with several bio_vecs, which cover part of the page
  767. * Multiple bion are linked together on bi_next. There may be extras
  768. * at the end of this list. We ignore them.
  769. */
  770. static void copy_data(int frombio, struct bio *bio,
  771. struct page *page,
  772. sector_t sector)
  773. {
  774. char *pa = page_address(page);
  775. struct bio_vec *bvl;
  776. int i;
  777. int page_offset;
  778. if (bio->bi_sector >= sector)
  779. page_offset = (signed)(bio->bi_sector - sector) * 512;
  780. else
  781. page_offset = (signed)(sector - bio->bi_sector) * -512;
  782. bio_for_each_segment(bvl, bio, i) {
  783. int len = bio_iovec_idx(bio,i)->bv_len;
  784. int clen;
  785. int b_offset = 0;
  786. if (page_offset < 0) {
  787. b_offset = -page_offset;
  788. page_offset += b_offset;
  789. len -= b_offset;
  790. }
  791. if (len > 0 && page_offset + len > STRIPE_SIZE)
  792. clen = STRIPE_SIZE - page_offset;
  793. else clen = len;
  794. if (clen > 0) {
  795. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  796. if (frombio)
  797. memcpy(pa+page_offset, ba+b_offset, clen);
  798. else
  799. memcpy(ba+b_offset, pa+page_offset, clen);
  800. __bio_kunmap_atomic(ba, KM_USER0);
  801. }
  802. if (clen < len) /* hit end of page */
  803. break;
  804. page_offset += len;
  805. }
  806. }
  807. #define check_xor() do { \
  808. if (count == MAX_XOR_BLOCKS) { \
  809. xor_block(count, STRIPE_SIZE, ptr); \
  810. count = 1; \
  811. } \
  812. } while(0)
  813. static void compute_block(struct stripe_head *sh, int dd_idx)
  814. {
  815. int i, count, disks = sh->disks;
  816. void *ptr[MAX_XOR_BLOCKS], *p;
  817. PRINTK("compute_block, stripe %llu, idx %d\n",
  818. (unsigned long long)sh->sector, dd_idx);
  819. ptr[0] = page_address(sh->dev[dd_idx].page);
  820. memset(ptr[0], 0, STRIPE_SIZE);
  821. count = 1;
  822. for (i = disks ; i--; ) {
  823. if (i == dd_idx)
  824. continue;
  825. p = page_address(sh->dev[i].page);
  826. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  827. ptr[count++] = p;
  828. else
  829. printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
  830. " not present\n", dd_idx,
  831. (unsigned long long)sh->sector, i);
  832. check_xor();
  833. }
  834. if (count != 1)
  835. xor_block(count, STRIPE_SIZE, ptr);
  836. set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  837. }
  838. static void compute_parity5(struct stripe_head *sh, int method)
  839. {
  840. raid5_conf_t *conf = sh->raid_conf;
  841. int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
  842. void *ptr[MAX_XOR_BLOCKS];
  843. struct bio *chosen;
  844. PRINTK("compute_parity5, stripe %llu, method %d\n",
  845. (unsigned long long)sh->sector, method);
  846. count = 1;
  847. ptr[0] = page_address(sh->dev[pd_idx].page);
  848. switch(method) {
  849. case READ_MODIFY_WRITE:
  850. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
  851. for (i=disks ; i-- ;) {
  852. if (i==pd_idx)
  853. continue;
  854. if (sh->dev[i].towrite &&
  855. test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
  856. ptr[count++] = page_address(sh->dev[i].page);
  857. chosen = sh->dev[i].towrite;
  858. sh->dev[i].towrite = NULL;
  859. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  860. wake_up(&conf->wait_for_overlap);
  861. BUG_ON(sh->dev[i].written);
  862. sh->dev[i].written = chosen;
  863. check_xor();
  864. }
  865. }
  866. break;
  867. case RECONSTRUCT_WRITE:
  868. memset(ptr[0], 0, STRIPE_SIZE);
  869. for (i= disks; i-- ;)
  870. if (i!=pd_idx && sh->dev[i].towrite) {
  871. chosen = sh->dev[i].towrite;
  872. sh->dev[i].towrite = NULL;
  873. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  874. wake_up(&conf->wait_for_overlap);
  875. BUG_ON(sh->dev[i].written);
  876. sh->dev[i].written = chosen;
  877. }
  878. break;
  879. case CHECK_PARITY:
  880. break;
  881. }
  882. if (count>1) {
  883. xor_block(count, STRIPE_SIZE, ptr);
  884. count = 1;
  885. }
  886. for (i = disks; i--;)
  887. if (sh->dev[i].written) {
  888. sector_t sector = sh->dev[i].sector;
  889. struct bio *wbi = sh->dev[i].written;
  890. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  891. copy_data(1, wbi, sh->dev[i].page, sector);
  892. wbi = r5_next_bio(wbi, sector);
  893. }
  894. set_bit(R5_LOCKED, &sh->dev[i].flags);
  895. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  896. }
  897. switch(method) {
  898. case RECONSTRUCT_WRITE:
  899. case CHECK_PARITY:
  900. for (i=disks; i--;)
  901. if (i != pd_idx) {
  902. ptr[count++] = page_address(sh->dev[i].page);
  903. check_xor();
  904. }
  905. break;
  906. case READ_MODIFY_WRITE:
  907. for (i = disks; i--;)
  908. if (sh->dev[i].written) {
  909. ptr[count++] = page_address(sh->dev[i].page);
  910. check_xor();
  911. }
  912. }
  913. if (count != 1)
  914. xor_block(count, STRIPE_SIZE, ptr);
  915. if (method != CHECK_PARITY) {
  916. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  917. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  918. } else
  919. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  920. }
  921. static void compute_parity6(struct stripe_head *sh, int method)
  922. {
  923. raid6_conf_t *conf = sh->raid_conf;
  924. int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
  925. struct bio *chosen;
  926. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  927. void *ptrs[disks];
  928. qd_idx = raid6_next_disk(pd_idx, disks);
  929. d0_idx = raid6_next_disk(qd_idx, disks);
  930. PRINTK("compute_parity, stripe %llu, method %d\n",
  931. (unsigned long long)sh->sector, method);
  932. switch(method) {
  933. case READ_MODIFY_WRITE:
  934. BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
  935. case RECONSTRUCT_WRITE:
  936. for (i= disks; i-- ;)
  937. if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
  938. chosen = sh->dev[i].towrite;
  939. sh->dev[i].towrite = NULL;
  940. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  941. wake_up(&conf->wait_for_overlap);
  942. if (sh->dev[i].written) BUG();
  943. sh->dev[i].written = chosen;
  944. }
  945. break;
  946. case CHECK_PARITY:
  947. BUG(); /* Not implemented yet */
  948. }
  949. for (i = disks; i--;)
  950. if (sh->dev[i].written) {
  951. sector_t sector = sh->dev[i].sector;
  952. struct bio *wbi = sh->dev[i].written;
  953. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  954. copy_data(1, wbi, sh->dev[i].page, sector);
  955. wbi = r5_next_bio(wbi, sector);
  956. }
  957. set_bit(R5_LOCKED, &sh->dev[i].flags);
  958. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  959. }
  960. // switch(method) {
  961. // case RECONSTRUCT_WRITE:
  962. // case CHECK_PARITY:
  963. // case UPDATE_PARITY:
  964. /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
  965. /* FIX: Is this ordering of drives even remotely optimal? */
  966. count = 0;
  967. i = d0_idx;
  968. do {
  969. ptrs[count++] = page_address(sh->dev[i].page);
  970. if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  971. printk("block %d/%d not uptodate on parity calc\n", i,count);
  972. i = raid6_next_disk(i, disks);
  973. } while ( i != d0_idx );
  974. // break;
  975. // }
  976. raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
  977. switch(method) {
  978. case RECONSTRUCT_WRITE:
  979. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  980. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  981. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  982. set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
  983. break;
  984. case UPDATE_PARITY:
  985. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  986. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  987. break;
  988. }
  989. }
  990. /* Compute one missing block */
  991. static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
  992. {
  993. raid6_conf_t *conf = sh->raid_conf;
  994. int i, count, disks = conf->raid_disks;
  995. void *ptr[MAX_XOR_BLOCKS], *p;
  996. int pd_idx = sh->pd_idx;
  997. int qd_idx = raid6_next_disk(pd_idx, disks);
  998. PRINTK("compute_block_1, stripe %llu, idx %d\n",
  999. (unsigned long long)sh->sector, dd_idx);
  1000. if ( dd_idx == qd_idx ) {
  1001. /* We're actually computing the Q drive */
  1002. compute_parity6(sh, UPDATE_PARITY);
  1003. } else {
  1004. ptr[0] = page_address(sh->dev[dd_idx].page);
  1005. if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
  1006. count = 1;
  1007. for (i = disks ; i--; ) {
  1008. if (i == dd_idx || i == qd_idx)
  1009. continue;
  1010. p = page_address(sh->dev[i].page);
  1011. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1012. ptr[count++] = p;
  1013. else
  1014. printk("compute_block() %d, stripe %llu, %d"
  1015. " not present\n", dd_idx,
  1016. (unsigned long long)sh->sector, i);
  1017. check_xor();
  1018. }
  1019. if (count != 1)
  1020. xor_block(count, STRIPE_SIZE, ptr);
  1021. if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1022. else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1023. }
  1024. }
  1025. /* Compute two missing blocks */
  1026. static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
  1027. {
  1028. raid6_conf_t *conf = sh->raid_conf;
  1029. int i, count, disks = conf->raid_disks;
  1030. int pd_idx = sh->pd_idx;
  1031. int qd_idx = raid6_next_disk(pd_idx, disks);
  1032. int d0_idx = raid6_next_disk(qd_idx, disks);
  1033. int faila, failb;
  1034. /* faila and failb are disk numbers relative to d0_idx */
  1035. /* pd_idx become disks-2 and qd_idx become disks-1 */
  1036. faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
  1037. failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
  1038. BUG_ON(faila == failb);
  1039. if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
  1040. PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
  1041. (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
  1042. if ( failb == disks-1 ) {
  1043. /* Q disk is one of the missing disks */
  1044. if ( faila == disks-2 ) {
  1045. /* Missing P+Q, just recompute */
  1046. compute_parity6(sh, UPDATE_PARITY);
  1047. return;
  1048. } else {
  1049. /* We're missing D+Q; recompute D from P */
  1050. compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
  1051. compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
  1052. return;
  1053. }
  1054. }
  1055. /* We're missing D+P or D+D; build pointer table */
  1056. {
  1057. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1058. void *ptrs[disks];
  1059. count = 0;
  1060. i = d0_idx;
  1061. do {
  1062. ptrs[count++] = page_address(sh->dev[i].page);
  1063. i = raid6_next_disk(i, disks);
  1064. if (i != dd_idx1 && i != dd_idx2 &&
  1065. !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1066. printk("compute_2 with missing block %d/%d\n", count, i);
  1067. } while ( i != d0_idx );
  1068. if ( failb == disks-2 ) {
  1069. /* We're missing D+P. */
  1070. raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
  1071. } else {
  1072. /* We're missing D+D. */
  1073. raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
  1074. }
  1075. /* Both the above update both missing blocks */
  1076. set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
  1077. set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
  1078. }
  1079. }
  1080. /*
  1081. * Each stripe/dev can have one or more bion attached.
  1082. * toread/towrite point to the first in a chain.
  1083. * The bi_next chain must be in order.
  1084. */
  1085. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1086. {
  1087. struct bio **bip;
  1088. raid5_conf_t *conf = sh->raid_conf;
  1089. int firstwrite=0;
  1090. PRINTK("adding bh b#%llu to stripe s#%llu\n",
  1091. (unsigned long long)bi->bi_sector,
  1092. (unsigned long long)sh->sector);
  1093. spin_lock(&sh->lock);
  1094. spin_lock_irq(&conf->device_lock);
  1095. if (forwrite) {
  1096. bip = &sh->dev[dd_idx].towrite;
  1097. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1098. firstwrite = 1;
  1099. } else
  1100. bip = &sh->dev[dd_idx].toread;
  1101. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1102. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1103. goto overlap;
  1104. bip = & (*bip)->bi_next;
  1105. }
  1106. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1107. goto overlap;
  1108. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1109. if (*bip)
  1110. bi->bi_next = *bip;
  1111. *bip = bi;
  1112. bi->bi_phys_segments ++;
  1113. spin_unlock_irq(&conf->device_lock);
  1114. spin_unlock(&sh->lock);
  1115. PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1116. (unsigned long long)bi->bi_sector,
  1117. (unsigned long long)sh->sector, dd_idx);
  1118. if (conf->mddev->bitmap && firstwrite) {
  1119. sh->bm_seq = conf->seq_write;
  1120. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1121. STRIPE_SECTORS, 0);
  1122. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1123. }
  1124. if (forwrite) {
  1125. /* check if page is covered */
  1126. sector_t sector = sh->dev[dd_idx].sector;
  1127. for (bi=sh->dev[dd_idx].towrite;
  1128. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1129. bi && bi->bi_sector <= sector;
  1130. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1131. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1132. sector = bi->bi_sector + (bi->bi_size>>9);
  1133. }
  1134. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1135. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1136. }
  1137. return 1;
  1138. overlap:
  1139. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1140. spin_unlock_irq(&conf->device_lock);
  1141. spin_unlock(&sh->lock);
  1142. return 0;
  1143. }
  1144. static void end_reshape(raid5_conf_t *conf);
  1145. static int page_is_zero(struct page *p)
  1146. {
  1147. char *a = page_address(p);
  1148. return ((*(u32*)a) == 0 &&
  1149. memcmp(a, a+4, STRIPE_SIZE-4)==0);
  1150. }
  1151. static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
  1152. {
  1153. int sectors_per_chunk = conf->chunk_size >> 9;
  1154. sector_t x = stripe;
  1155. int pd_idx, dd_idx;
  1156. int chunk_offset = sector_div(x, sectors_per_chunk);
  1157. stripe = x;
  1158. raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
  1159. + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
  1160. return pd_idx;
  1161. }
  1162. /*
  1163. * handle_stripe - do things to a stripe.
  1164. *
  1165. * We lock the stripe and then examine the state of various bits
  1166. * to see what needs to be done.
  1167. * Possible results:
  1168. * return some read request which now have data
  1169. * return some write requests which are safely on disc
  1170. * schedule a read on some buffers
  1171. * schedule a write of some buffers
  1172. * return confirmation of parity correctness
  1173. *
  1174. * Parity calculations are done inside the stripe lock
  1175. * buffers are taken off read_list or write_list, and bh_cache buffers
  1176. * get BH_Lock set before the stripe lock is released.
  1177. *
  1178. */
  1179. static void handle_stripe5(struct stripe_head *sh)
  1180. {
  1181. raid5_conf_t *conf = sh->raid_conf;
  1182. int disks = sh->disks;
  1183. struct bio *return_bi= NULL;
  1184. struct bio *bi;
  1185. int i;
  1186. int syncing, expanding, expanded;
  1187. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  1188. int non_overwrite = 0;
  1189. int failed_num=0;
  1190. struct r5dev *dev;
  1191. PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
  1192. (unsigned long long)sh->sector, atomic_read(&sh->count),
  1193. sh->pd_idx);
  1194. spin_lock(&sh->lock);
  1195. clear_bit(STRIPE_HANDLE, &sh->state);
  1196. clear_bit(STRIPE_DELAYED, &sh->state);
  1197. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  1198. expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1199. expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  1200. /* Now to look around and see what can be done */
  1201. rcu_read_lock();
  1202. for (i=disks; i--; ) {
  1203. mdk_rdev_t *rdev;
  1204. dev = &sh->dev[i];
  1205. clear_bit(R5_Insync, &dev->flags);
  1206. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  1207. i, dev->flags, dev->toread, dev->towrite, dev->written);
  1208. /* maybe we can reply to a read */
  1209. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  1210. struct bio *rbi, *rbi2;
  1211. PRINTK("Return read for disc %d\n", i);
  1212. spin_lock_irq(&conf->device_lock);
  1213. rbi = dev->toread;
  1214. dev->toread = NULL;
  1215. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1216. wake_up(&conf->wait_for_overlap);
  1217. spin_unlock_irq(&conf->device_lock);
  1218. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1219. copy_data(0, rbi, dev->page, dev->sector);
  1220. rbi2 = r5_next_bio(rbi, dev->sector);
  1221. spin_lock_irq(&conf->device_lock);
  1222. if (--rbi->bi_phys_segments == 0) {
  1223. rbi->bi_next = return_bi;
  1224. return_bi = rbi;
  1225. }
  1226. spin_unlock_irq(&conf->device_lock);
  1227. rbi = rbi2;
  1228. }
  1229. }
  1230. /* now count some things */
  1231. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  1232. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  1233. if (dev->toread) to_read++;
  1234. if (dev->towrite) {
  1235. to_write++;
  1236. if (!test_bit(R5_OVERWRITE, &dev->flags))
  1237. non_overwrite++;
  1238. }
  1239. if (dev->written) written++;
  1240. rdev = rcu_dereference(conf->disks[i].rdev);
  1241. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  1242. /* The ReadError flag will just be confusing now */
  1243. clear_bit(R5_ReadError, &dev->flags);
  1244. clear_bit(R5_ReWrite, &dev->flags);
  1245. }
  1246. if (!rdev || !test_bit(In_sync, &rdev->flags)
  1247. || test_bit(R5_ReadError, &dev->flags)) {
  1248. failed++;
  1249. failed_num = i;
  1250. } else
  1251. set_bit(R5_Insync, &dev->flags);
  1252. }
  1253. rcu_read_unlock();
  1254. PRINTK("locked=%d uptodate=%d to_read=%d"
  1255. " to_write=%d failed=%d failed_num=%d\n",
  1256. locked, uptodate, to_read, to_write, failed, failed_num);
  1257. /* check if the array has lost two devices and, if so, some requests might
  1258. * need to be failed
  1259. */
  1260. if (failed > 1 && to_read+to_write+written) {
  1261. for (i=disks; i--; ) {
  1262. int bitmap_end = 0;
  1263. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1264. mdk_rdev_t *rdev;
  1265. rcu_read_lock();
  1266. rdev = rcu_dereference(conf->disks[i].rdev);
  1267. if (rdev && test_bit(In_sync, &rdev->flags))
  1268. /* multiple read failures in one stripe */
  1269. md_error(conf->mddev, rdev);
  1270. rcu_read_unlock();
  1271. }
  1272. spin_lock_irq(&conf->device_lock);
  1273. /* fail all writes first */
  1274. bi = sh->dev[i].towrite;
  1275. sh->dev[i].towrite = NULL;
  1276. if (bi) { to_write--; bitmap_end = 1; }
  1277. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1278. wake_up(&conf->wait_for_overlap);
  1279. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1280. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1281. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1282. if (--bi->bi_phys_segments == 0) {
  1283. md_write_end(conf->mddev);
  1284. bi->bi_next = return_bi;
  1285. return_bi = bi;
  1286. }
  1287. bi = nextbi;
  1288. }
  1289. /* and fail all 'written' */
  1290. bi = sh->dev[i].written;
  1291. sh->dev[i].written = NULL;
  1292. if (bi) bitmap_end = 1;
  1293. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  1294. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1295. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1296. if (--bi->bi_phys_segments == 0) {
  1297. md_write_end(conf->mddev);
  1298. bi->bi_next = return_bi;
  1299. return_bi = bi;
  1300. }
  1301. bi = bi2;
  1302. }
  1303. /* fail any reads if this device is non-operational */
  1304. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1305. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1306. bi = sh->dev[i].toread;
  1307. sh->dev[i].toread = NULL;
  1308. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1309. wake_up(&conf->wait_for_overlap);
  1310. if (bi) to_read--;
  1311. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1312. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1313. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1314. if (--bi->bi_phys_segments == 0) {
  1315. bi->bi_next = return_bi;
  1316. return_bi = bi;
  1317. }
  1318. bi = nextbi;
  1319. }
  1320. }
  1321. spin_unlock_irq(&conf->device_lock);
  1322. if (bitmap_end)
  1323. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1324. STRIPE_SECTORS, 0, 0);
  1325. }
  1326. }
  1327. if (failed > 1 && syncing) {
  1328. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  1329. clear_bit(STRIPE_SYNCING, &sh->state);
  1330. syncing = 0;
  1331. }
  1332. /* might be able to return some write requests if the parity block
  1333. * is safe, or on a failed drive
  1334. */
  1335. dev = &sh->dev[sh->pd_idx];
  1336. if ( written &&
  1337. ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
  1338. test_bit(R5_UPTODATE, &dev->flags))
  1339. || (failed == 1 && failed_num == sh->pd_idx))
  1340. ) {
  1341. /* any written block on an uptodate or failed drive can be returned.
  1342. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  1343. * never LOCKED, so we don't need to test 'failed' directly.
  1344. */
  1345. for (i=disks; i--; )
  1346. if (sh->dev[i].written) {
  1347. dev = &sh->dev[i];
  1348. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1349. test_bit(R5_UPTODATE, &dev->flags) ) {
  1350. /* We can return any write requests */
  1351. struct bio *wbi, *wbi2;
  1352. int bitmap_end = 0;
  1353. PRINTK("Return write for disc %d\n", i);
  1354. spin_lock_irq(&conf->device_lock);
  1355. wbi = dev->written;
  1356. dev->written = NULL;
  1357. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1358. wbi2 = r5_next_bio(wbi, dev->sector);
  1359. if (--wbi->bi_phys_segments == 0) {
  1360. md_write_end(conf->mddev);
  1361. wbi->bi_next = return_bi;
  1362. return_bi = wbi;
  1363. }
  1364. wbi = wbi2;
  1365. }
  1366. if (dev->towrite == NULL)
  1367. bitmap_end = 1;
  1368. spin_unlock_irq(&conf->device_lock);
  1369. if (bitmap_end)
  1370. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1371. STRIPE_SECTORS,
  1372. !test_bit(STRIPE_DEGRADED, &sh->state), 0);
  1373. }
  1374. }
  1375. }
  1376. /* Now we might consider reading some blocks, either to check/generate
  1377. * parity, or to satisfy requests
  1378. * or to load a block that is being partially written.
  1379. */
  1380. if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
  1381. for (i=disks; i--;) {
  1382. dev = &sh->dev[i];
  1383. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1384. (dev->toread ||
  1385. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1386. syncing ||
  1387. expanding ||
  1388. (failed && (sh->dev[failed_num].toread ||
  1389. (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
  1390. )
  1391. ) {
  1392. /* we would like to get this block, possibly
  1393. * by computing it, but we might not be able to
  1394. */
  1395. if (uptodate == disks-1) {
  1396. PRINTK("Computing block %d\n", i);
  1397. compute_block(sh, i);
  1398. uptodate++;
  1399. } else if (test_bit(R5_Insync, &dev->flags)) {
  1400. set_bit(R5_LOCKED, &dev->flags);
  1401. set_bit(R5_Wantread, &dev->flags);
  1402. #if 0
  1403. /* if I am just reading this block and we don't have
  1404. a failed drive, or any pending writes then sidestep the cache */
  1405. if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
  1406. ! syncing && !failed && !to_write) {
  1407. sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
  1408. sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
  1409. }
  1410. #endif
  1411. locked++;
  1412. PRINTK("Reading block %d (sync=%d)\n",
  1413. i, syncing);
  1414. }
  1415. }
  1416. }
  1417. set_bit(STRIPE_HANDLE, &sh->state);
  1418. }
  1419. /* now to consider writing and what else, if anything should be read */
  1420. if (to_write) {
  1421. int rmw=0, rcw=0;
  1422. for (i=disks ; i--;) {
  1423. /* would I have to read this buffer for read_modify_write */
  1424. dev = &sh->dev[i];
  1425. if ((dev->towrite || i == sh->pd_idx) &&
  1426. (!test_bit(R5_LOCKED, &dev->flags)
  1427. #if 0
  1428. || sh->bh_page[i]!=bh->b_page
  1429. #endif
  1430. ) &&
  1431. !test_bit(R5_UPTODATE, &dev->flags)) {
  1432. if (test_bit(R5_Insync, &dev->flags)
  1433. /* && !(!mddev->insync && i == sh->pd_idx) */
  1434. )
  1435. rmw++;
  1436. else rmw += 2*disks; /* cannot read it */
  1437. }
  1438. /* Would I have to read this buffer for reconstruct_write */
  1439. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1440. (!test_bit(R5_LOCKED, &dev->flags)
  1441. #if 0
  1442. || sh->bh_page[i] != bh->b_page
  1443. #endif
  1444. ) &&
  1445. !test_bit(R5_UPTODATE, &dev->flags)) {
  1446. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1447. else rcw += 2*disks;
  1448. }
  1449. }
  1450. PRINTK("for sector %llu, rmw=%d rcw=%d\n",
  1451. (unsigned long long)sh->sector, rmw, rcw);
  1452. set_bit(STRIPE_HANDLE, &sh->state);
  1453. if (rmw < rcw && rmw > 0)
  1454. /* prefer read-modify-write, but need to get some data */
  1455. for (i=disks; i--;) {
  1456. dev = &sh->dev[i];
  1457. if ((dev->towrite || i == sh->pd_idx) &&
  1458. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1459. test_bit(R5_Insync, &dev->flags)) {
  1460. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1461. {
  1462. PRINTK("Read_old block %d for r-m-w\n", i);
  1463. set_bit(R5_LOCKED, &dev->flags);
  1464. set_bit(R5_Wantread, &dev->flags);
  1465. locked++;
  1466. } else {
  1467. set_bit(STRIPE_DELAYED, &sh->state);
  1468. set_bit(STRIPE_HANDLE, &sh->state);
  1469. }
  1470. }
  1471. }
  1472. if (rcw <= rmw && rcw > 0)
  1473. /* want reconstruct write, but need to get some data */
  1474. for (i=disks; i--;) {
  1475. dev = &sh->dev[i];
  1476. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1477. !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1478. test_bit(R5_Insync, &dev->flags)) {
  1479. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1480. {
  1481. PRINTK("Read_old block %d for Reconstruct\n", i);
  1482. set_bit(R5_LOCKED, &dev->flags);
  1483. set_bit(R5_Wantread, &dev->flags);
  1484. locked++;
  1485. } else {
  1486. set_bit(STRIPE_DELAYED, &sh->state);
  1487. set_bit(STRIPE_HANDLE, &sh->state);
  1488. }
  1489. }
  1490. }
  1491. /* now if nothing is locked, and if we have enough data, we can start a write request */
  1492. if (locked == 0 && (rcw == 0 ||rmw == 0) &&
  1493. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1494. PRINTK("Computing parity...\n");
  1495. compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
  1496. /* now every locked buffer is ready to be written */
  1497. for (i=disks; i--;)
  1498. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  1499. PRINTK("Writing block %d\n", i);
  1500. locked++;
  1501. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1502. if (!test_bit(R5_Insync, &sh->dev[i].flags)
  1503. || (i==sh->pd_idx && failed == 0))
  1504. set_bit(STRIPE_INSYNC, &sh->state);
  1505. }
  1506. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1507. atomic_dec(&conf->preread_active_stripes);
  1508. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  1509. md_wakeup_thread(conf->mddev->thread);
  1510. }
  1511. }
  1512. }
  1513. /* maybe we need to check and possibly fix the parity for this stripe
  1514. * Any reads will already have been scheduled, so we just see if enough data
  1515. * is available
  1516. */
  1517. if (syncing && locked == 0 &&
  1518. !test_bit(STRIPE_INSYNC, &sh->state)) {
  1519. set_bit(STRIPE_HANDLE, &sh->state);
  1520. if (failed == 0) {
  1521. BUG_ON(uptodate != disks);
  1522. compute_parity5(sh, CHECK_PARITY);
  1523. uptodate--;
  1524. if (page_is_zero(sh->dev[sh->pd_idx].page)) {
  1525. /* parity is correct (on disc, not in buffer any more) */
  1526. set_bit(STRIPE_INSYNC, &sh->state);
  1527. } else {
  1528. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  1529. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  1530. /* don't try to repair!! */
  1531. set_bit(STRIPE_INSYNC, &sh->state);
  1532. else {
  1533. compute_block(sh, sh->pd_idx);
  1534. uptodate++;
  1535. }
  1536. }
  1537. }
  1538. if (!test_bit(STRIPE_INSYNC, &sh->state)) {
  1539. /* either failed parity check, or recovery is happening */
  1540. if (failed==0)
  1541. failed_num = sh->pd_idx;
  1542. dev = &sh->dev[failed_num];
  1543. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  1544. BUG_ON(uptodate != disks);
  1545. set_bit(R5_LOCKED, &dev->flags);
  1546. set_bit(R5_Wantwrite, &dev->flags);
  1547. clear_bit(STRIPE_DEGRADED, &sh->state);
  1548. locked++;
  1549. set_bit(STRIPE_INSYNC, &sh->state);
  1550. }
  1551. }
  1552. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  1553. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  1554. clear_bit(STRIPE_SYNCING, &sh->state);
  1555. }
  1556. /* If the failed drive is just a ReadError, then we might need to progress
  1557. * the repair/check process
  1558. */
  1559. if (failed == 1 && ! conf->mddev->ro &&
  1560. test_bit(R5_ReadError, &sh->dev[failed_num].flags)
  1561. && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
  1562. && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
  1563. ) {
  1564. dev = &sh->dev[failed_num];
  1565. if (!test_bit(R5_ReWrite, &dev->flags)) {
  1566. set_bit(R5_Wantwrite, &dev->flags);
  1567. set_bit(R5_ReWrite, &dev->flags);
  1568. set_bit(R5_LOCKED, &dev->flags);
  1569. locked++;
  1570. } else {
  1571. /* let's read it back */
  1572. set_bit(R5_Wantread, &dev->flags);
  1573. set_bit(R5_LOCKED, &dev->flags);
  1574. locked++;
  1575. }
  1576. }
  1577. if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  1578. /* Need to write out all blocks after computing parity */
  1579. sh->disks = conf->raid_disks;
  1580. sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
  1581. compute_parity5(sh, RECONSTRUCT_WRITE);
  1582. for (i= conf->raid_disks; i--;) {
  1583. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1584. locked++;
  1585. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  1586. }
  1587. clear_bit(STRIPE_EXPANDING, &sh->state);
  1588. } else if (expanded) {
  1589. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  1590. atomic_dec(&conf->reshape_stripes);
  1591. wake_up(&conf->wait_for_overlap);
  1592. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  1593. }
  1594. if (expanding && locked == 0) {
  1595. /* We have read all the blocks in this stripe and now we need to
  1596. * copy some of them into a target stripe for expand.
  1597. */
  1598. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  1599. for (i=0; i< sh->disks; i++)
  1600. if (i != sh->pd_idx) {
  1601. int dd_idx, pd_idx, j;
  1602. struct stripe_head *sh2;
  1603. sector_t bn = compute_blocknr(sh, i);
  1604. sector_t s = raid5_compute_sector(bn, conf->raid_disks,
  1605. conf->raid_disks-1,
  1606. &dd_idx, &pd_idx, conf);
  1607. sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
  1608. if (sh2 == NULL)
  1609. /* so far only the early blocks of this stripe
  1610. * have been requested. When later blocks
  1611. * get requested, we will try again
  1612. */
  1613. continue;
  1614. if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  1615. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  1616. /* must have already done this block */
  1617. release_stripe(sh2);
  1618. continue;
  1619. }
  1620. memcpy(page_address(sh2->dev[dd_idx].page),
  1621. page_address(sh->dev[i].page),
  1622. STRIPE_SIZE);
  1623. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  1624. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  1625. for (j=0; j<conf->raid_disks; j++)
  1626. if (j != sh2->pd_idx &&
  1627. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  1628. break;
  1629. if (j == conf->raid_disks) {
  1630. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  1631. set_bit(STRIPE_HANDLE, &sh2->state);
  1632. }
  1633. release_stripe(sh2);
  1634. }
  1635. }
  1636. spin_unlock(&sh->lock);
  1637. while ((bi=return_bi)) {
  1638. int bytes = bi->bi_size;
  1639. return_bi = bi->bi_next;
  1640. bi->bi_next = NULL;
  1641. bi->bi_size = 0;
  1642. bi->bi_end_io(bi, bytes, 0);
  1643. }
  1644. for (i=disks; i-- ;) {
  1645. int rw;
  1646. struct bio *bi;
  1647. mdk_rdev_t *rdev;
  1648. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  1649. rw = 1;
  1650. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  1651. rw = 0;
  1652. else
  1653. continue;
  1654. bi = &sh->dev[i].req;
  1655. bi->bi_rw = rw;
  1656. if (rw)
  1657. bi->bi_end_io = raid5_end_write_request;
  1658. else
  1659. bi->bi_end_io = raid5_end_read_request;
  1660. rcu_read_lock();
  1661. rdev = rcu_dereference(conf->disks[i].rdev);
  1662. if (rdev && test_bit(Faulty, &rdev->flags))
  1663. rdev = NULL;
  1664. if (rdev)
  1665. atomic_inc(&rdev->nr_pending);
  1666. rcu_read_unlock();
  1667. if (rdev) {
  1668. if (syncing || expanding || expanded)
  1669. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  1670. bi->bi_bdev = rdev->bdev;
  1671. PRINTK("for %llu schedule op %ld on disc %d\n",
  1672. (unsigned long long)sh->sector, bi->bi_rw, i);
  1673. atomic_inc(&sh->count);
  1674. bi->bi_sector = sh->sector + rdev->data_offset;
  1675. bi->bi_flags = 1 << BIO_UPTODATE;
  1676. bi->bi_vcnt = 1;
  1677. bi->bi_max_vecs = 1;
  1678. bi->bi_idx = 0;
  1679. bi->bi_io_vec = &sh->dev[i].vec;
  1680. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  1681. bi->bi_io_vec[0].bv_offset = 0;
  1682. bi->bi_size = STRIPE_SIZE;
  1683. bi->bi_next = NULL;
  1684. if (rw == WRITE &&
  1685. test_bit(R5_ReWrite, &sh->dev[i].flags))
  1686. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  1687. generic_make_request(bi);
  1688. } else {
  1689. if (rw == 1)
  1690. set_bit(STRIPE_DEGRADED, &sh->state);
  1691. PRINTK("skip op %ld on disc %d for sector %llu\n",
  1692. bi->bi_rw, i, (unsigned long long)sh->sector);
  1693. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1694. set_bit(STRIPE_HANDLE, &sh->state);
  1695. }
  1696. }
  1697. }
  1698. static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
  1699. {
  1700. raid6_conf_t *conf = sh->raid_conf;
  1701. int disks = conf->raid_disks;
  1702. struct bio *return_bi= NULL;
  1703. struct bio *bi;
  1704. int i;
  1705. int syncing;
  1706. int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
  1707. int non_overwrite = 0;
  1708. int failed_num[2] = {0, 0};
  1709. struct r5dev *dev, *pdev, *qdev;
  1710. int pd_idx = sh->pd_idx;
  1711. int qd_idx = raid6_next_disk(pd_idx, disks);
  1712. int p_failed, q_failed;
  1713. PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
  1714. (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
  1715. pd_idx, qd_idx);
  1716. spin_lock(&sh->lock);
  1717. clear_bit(STRIPE_HANDLE, &sh->state);
  1718. clear_bit(STRIPE_DELAYED, &sh->state);
  1719. syncing = test_bit(STRIPE_SYNCING, &sh->state);
  1720. /* Now to look around and see what can be done */
  1721. rcu_read_lock();
  1722. for (i=disks; i--; ) {
  1723. mdk_rdev_t *rdev;
  1724. dev = &sh->dev[i];
  1725. clear_bit(R5_Insync, &dev->flags);
  1726. PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
  1727. i, dev->flags, dev->toread, dev->towrite, dev->written);
  1728. /* maybe we can reply to a read */
  1729. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  1730. struct bio *rbi, *rbi2;
  1731. PRINTK("Return read for disc %d\n", i);
  1732. spin_lock_irq(&conf->device_lock);
  1733. rbi = dev->toread;
  1734. dev->toread = NULL;
  1735. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  1736. wake_up(&conf->wait_for_overlap);
  1737. spin_unlock_irq(&conf->device_lock);
  1738. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1739. copy_data(0, rbi, dev->page, dev->sector);
  1740. rbi2 = r5_next_bio(rbi, dev->sector);
  1741. spin_lock_irq(&conf->device_lock);
  1742. if (--rbi->bi_phys_segments == 0) {
  1743. rbi->bi_next = return_bi;
  1744. return_bi = rbi;
  1745. }
  1746. spin_unlock_irq(&conf->device_lock);
  1747. rbi = rbi2;
  1748. }
  1749. }
  1750. /* now count some things */
  1751. if (test_bit(R5_LOCKED, &dev->flags)) locked++;
  1752. if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
  1753. if (dev->toread) to_read++;
  1754. if (dev->towrite) {
  1755. to_write++;
  1756. if (!test_bit(R5_OVERWRITE, &dev->flags))
  1757. non_overwrite++;
  1758. }
  1759. if (dev->written) written++;
  1760. rdev = rcu_dereference(conf->disks[i].rdev);
  1761. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  1762. /* The ReadError flag will just be confusing now */
  1763. clear_bit(R5_ReadError, &dev->flags);
  1764. clear_bit(R5_ReWrite, &dev->flags);
  1765. }
  1766. if (!rdev || !test_bit(In_sync, &rdev->flags)
  1767. || test_bit(R5_ReadError, &dev->flags)) {
  1768. if ( failed < 2 )
  1769. failed_num[failed] = i;
  1770. failed++;
  1771. } else
  1772. set_bit(R5_Insync, &dev->flags);
  1773. }
  1774. rcu_read_unlock();
  1775. PRINTK("locked=%d uptodate=%d to_read=%d"
  1776. " to_write=%d failed=%d failed_num=%d,%d\n",
  1777. locked, uptodate, to_read, to_write, failed,
  1778. failed_num[0], failed_num[1]);
  1779. /* check if the array has lost >2 devices and, if so, some requests might
  1780. * need to be failed
  1781. */
  1782. if (failed > 2 && to_read+to_write+written) {
  1783. for (i=disks; i--; ) {
  1784. int bitmap_end = 0;
  1785. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1786. mdk_rdev_t *rdev;
  1787. rcu_read_lock();
  1788. rdev = rcu_dereference(conf->disks[i].rdev);
  1789. if (rdev && test_bit(In_sync, &rdev->flags))
  1790. /* multiple read failures in one stripe */
  1791. md_error(conf->mddev, rdev);
  1792. rcu_read_unlock();
  1793. }
  1794. spin_lock_irq(&conf->device_lock);
  1795. /* fail all writes first */
  1796. bi = sh->dev[i].towrite;
  1797. sh->dev[i].towrite = NULL;
  1798. if (bi) { to_write--; bitmap_end = 1; }
  1799. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1800. wake_up(&conf->wait_for_overlap);
  1801. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1802. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1803. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1804. if (--bi->bi_phys_segments == 0) {
  1805. md_write_end(conf->mddev);
  1806. bi->bi_next = return_bi;
  1807. return_bi = bi;
  1808. }
  1809. bi = nextbi;
  1810. }
  1811. /* and fail all 'written' */
  1812. bi = sh->dev[i].written;
  1813. sh->dev[i].written = NULL;
  1814. if (bi) bitmap_end = 1;
  1815. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
  1816. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1817. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1818. if (--bi->bi_phys_segments == 0) {
  1819. md_write_end(conf->mddev);
  1820. bi->bi_next = return_bi;
  1821. return_bi = bi;
  1822. }
  1823. bi = bi2;
  1824. }
  1825. /* fail any reads if this device is non-operational */
  1826. if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1827. test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1828. bi = sh->dev[i].toread;
  1829. sh->dev[i].toread = NULL;
  1830. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1831. wake_up(&conf->wait_for_overlap);
  1832. if (bi) to_read--;
  1833. while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
  1834. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1835. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1836. if (--bi->bi_phys_segments == 0) {
  1837. bi->bi_next = return_bi;
  1838. return_bi = bi;
  1839. }
  1840. bi = nextbi;
  1841. }
  1842. }
  1843. spin_unlock_irq(&conf->device_lock);
  1844. if (bitmap_end)
  1845. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1846. STRIPE_SECTORS, 0, 0);
  1847. }
  1848. }
  1849. if (failed > 2 && syncing) {
  1850. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  1851. clear_bit(STRIPE_SYNCING, &sh->state);
  1852. syncing = 0;
  1853. }
  1854. /*
  1855. * might be able to return some write requests if the parity blocks
  1856. * are safe, or on a failed drive
  1857. */
  1858. pdev = &sh->dev[pd_idx];
  1859. p_failed = (failed >= 1 && failed_num[0] == pd_idx)
  1860. || (failed >= 2 && failed_num[1] == pd_idx);
  1861. qdev = &sh->dev[qd_idx];
  1862. q_failed = (failed >= 1 && failed_num[0] == qd_idx)
  1863. || (failed >= 2 && failed_num[1] == qd_idx);
  1864. if ( written &&
  1865. ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
  1866. && !test_bit(R5_LOCKED, &pdev->flags)
  1867. && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
  1868. ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
  1869. && !test_bit(R5_LOCKED, &qdev->flags)
  1870. && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
  1871. /* any written block on an uptodate or failed drive can be
  1872. * returned. Note that if we 'wrote' to a failed drive,
  1873. * it will be UPTODATE, but never LOCKED, so we don't need
  1874. * to test 'failed' directly.
  1875. */
  1876. for (i=disks; i--; )
  1877. if (sh->dev[i].written) {
  1878. dev = &sh->dev[i];
  1879. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1880. test_bit(R5_UPTODATE, &dev->flags) ) {
  1881. /* We can return any write requests */
  1882. int bitmap_end = 0;
  1883. struct bio *wbi, *wbi2;
  1884. PRINTK("Return write for stripe %llu disc %d\n",
  1885. (unsigned long long)sh->sector, i);
  1886. spin_lock_irq(&conf->device_lock);
  1887. wbi = dev->written;
  1888. dev->written = NULL;
  1889. while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  1890. wbi2 = r5_next_bio(wbi, dev->sector);
  1891. if (--wbi->bi_phys_segments == 0) {
  1892. md_write_end(conf->mddev);
  1893. wbi->bi_next = return_bi;
  1894. return_bi = wbi;
  1895. }
  1896. wbi = wbi2;
  1897. }
  1898. if (dev->towrite == NULL)
  1899. bitmap_end = 1;
  1900. spin_unlock_irq(&conf->device_lock);
  1901. if (bitmap_end)
  1902. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1903. STRIPE_SECTORS,
  1904. !test_bit(STRIPE_DEGRADED, &sh->state), 0);
  1905. }
  1906. }
  1907. }
  1908. /* Now we might consider reading some blocks, either to check/generate
  1909. * parity, or to satisfy requests
  1910. * or to load a block that is being partially written.
  1911. */
  1912. if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
  1913. for (i=disks; i--;) {
  1914. dev = &sh->dev[i];
  1915. if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1916. (dev->toread ||
  1917. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1918. syncing ||
  1919. (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
  1920. (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
  1921. )
  1922. ) {
  1923. /* we would like to get this block, possibly
  1924. * by computing it, but we might not be able to
  1925. */
  1926. if (uptodate == disks-1) {
  1927. PRINTK("Computing stripe %llu block %d\n",
  1928. (unsigned long long)sh->sector, i);
  1929. compute_block_1(sh, i, 0);
  1930. uptodate++;
  1931. } else if ( uptodate == disks-2 && failed >= 2 ) {
  1932. /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
  1933. int other;
  1934. for (other=disks; other--;) {
  1935. if ( other == i )
  1936. continue;
  1937. if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
  1938. break;
  1939. }
  1940. BUG_ON(other < 0);
  1941. PRINTK("Computing stripe %llu blocks %d,%d\n",
  1942. (unsigned long long)sh->sector, i, other);
  1943. compute_block_2(sh, i, other);
  1944. uptodate += 2;
  1945. } else if (test_bit(R5_Insync, &dev->flags)) {
  1946. set_bit(R5_LOCKED, &dev->flags);
  1947. set_bit(R5_Wantread, &dev->flags);
  1948. #if 0
  1949. /* if I am just reading this block and we don't have
  1950. a failed drive, or any pending writes then sidestep the cache */
  1951. if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
  1952. ! syncing && !failed && !to_write) {
  1953. sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
  1954. sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
  1955. }
  1956. #endif
  1957. locked++;
  1958. PRINTK("Reading block %d (sync=%d)\n",
  1959. i, syncing);
  1960. }
  1961. }
  1962. }
  1963. set_bit(STRIPE_HANDLE, &sh->state);
  1964. }
  1965. /* now to consider writing and what else, if anything should be read */
  1966. if (to_write) {
  1967. int rcw=0, must_compute=0;
  1968. for (i=disks ; i--;) {
  1969. dev = &sh->dev[i];
  1970. /* Would I have to read this buffer for reconstruct_write */
  1971. if (!test_bit(R5_OVERWRITE, &dev->flags)
  1972. && i != pd_idx && i != qd_idx
  1973. && (!test_bit(R5_LOCKED, &dev->flags)
  1974. #if 0
  1975. || sh->bh_page[i] != bh->b_page
  1976. #endif
  1977. ) &&
  1978. !test_bit(R5_UPTODATE, &dev->flags)) {
  1979. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1980. else {
  1981. PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
  1982. must_compute++;
  1983. }
  1984. }
  1985. }
  1986. PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
  1987. (unsigned long long)sh->sector, rcw, must_compute);
  1988. set_bit(STRIPE_HANDLE, &sh->state);
  1989. if (rcw > 0)
  1990. /* want reconstruct write, but need to get some data */
  1991. for (i=disks; i--;) {
  1992. dev = &sh->dev[i];
  1993. if (!test_bit(R5_OVERWRITE, &dev->flags)
  1994. && !(failed == 0 && (i == pd_idx || i == qd_idx))
  1995. && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
  1996. test_bit(R5_Insync, &dev->flags)) {
  1997. if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  1998. {
  1999. PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
  2000. (unsigned long long)sh->sector, i);
  2001. set_bit(R5_LOCKED, &dev->flags);
  2002. set_bit(R5_Wantread, &dev->flags);
  2003. locked++;
  2004. } else {
  2005. PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
  2006. (unsigned long long)sh->sector, i);
  2007. set_bit(STRIPE_DELAYED, &sh->state);
  2008. set_bit(STRIPE_HANDLE, &sh->state);
  2009. }
  2010. }
  2011. }
  2012. /* now if nothing is locked, and if we have enough data, we can start a write request */
  2013. if (locked == 0 && rcw == 0 &&
  2014. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  2015. if ( must_compute > 0 ) {
  2016. /* We have failed blocks and need to compute them */
  2017. switch ( failed ) {
  2018. case 0: BUG();
  2019. case 1: compute_block_1(sh, failed_num[0], 0); break;
  2020. case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
  2021. default: BUG(); /* This request should have been failed? */
  2022. }
  2023. }
  2024. PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
  2025. compute_parity6(sh, RECONSTRUCT_WRITE);
  2026. /* now every locked buffer is ready to be written */
  2027. for (i=disks; i--;)
  2028. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  2029. PRINTK("Writing stripe %llu block %d\n",
  2030. (unsigned long long)sh->sector, i);
  2031. locked++;
  2032. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2033. }
  2034. /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
  2035. set_bit(STRIPE_INSYNC, &sh->state);
  2036. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2037. atomic_dec(&conf->preread_active_stripes);
  2038. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  2039. md_wakeup_thread(conf->mddev->thread);
  2040. }
  2041. }
  2042. }
  2043. /* maybe we need to check and possibly fix the parity for this stripe
  2044. * Any reads will already have been scheduled, so we just see if enough data
  2045. * is available
  2046. */
  2047. if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
  2048. int update_p = 0, update_q = 0;
  2049. struct r5dev *dev;
  2050. set_bit(STRIPE_HANDLE, &sh->state);
  2051. BUG_ON(failed>2);
  2052. BUG_ON(uptodate < disks);
  2053. /* Want to check and possibly repair P and Q.
  2054. * However there could be one 'failed' device, in which
  2055. * case we can only check one of them, possibly using the
  2056. * other to generate missing data
  2057. */
  2058. /* If !tmp_page, we cannot do the calculations,
  2059. * but as we have set STRIPE_HANDLE, we will soon be called
  2060. * by stripe_handle with a tmp_page - just wait until then.
  2061. */
  2062. if (tmp_page) {
  2063. if (failed == q_failed) {
  2064. /* The only possible failed device holds 'Q', so it makes
  2065. * sense to check P (If anything else were failed, we would
  2066. * have used P to recreate it).
  2067. */
  2068. compute_block_1(sh, pd_idx, 1);
  2069. if (!page_is_zero(sh->dev[pd_idx].page)) {
  2070. compute_block_1(sh,pd_idx,0);
  2071. update_p = 1;
  2072. }
  2073. }
  2074. if (!q_failed && failed < 2) {
  2075. /* q is not failed, and we didn't use it to generate
  2076. * anything, so it makes sense to check it
  2077. */
  2078. memcpy(page_address(tmp_page),
  2079. page_address(sh->dev[qd_idx].page),
  2080. STRIPE_SIZE);
  2081. compute_parity6(sh, UPDATE_PARITY);
  2082. if (memcmp(page_address(tmp_page),
  2083. page_address(sh->dev[qd_idx].page),
  2084. STRIPE_SIZE)!= 0) {
  2085. clear_bit(STRIPE_INSYNC, &sh->state);
  2086. update_q = 1;
  2087. }
  2088. }
  2089. if (update_p || update_q) {
  2090. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2091. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2092. /* don't try to repair!! */
  2093. update_p = update_q = 0;
  2094. }
  2095. /* now write out any block on a failed drive,
  2096. * or P or Q if they need it
  2097. */
  2098. if (failed == 2) {
  2099. dev = &sh->dev[failed_num[1]];
  2100. locked++;
  2101. set_bit(R5_LOCKED, &dev->flags);
  2102. set_bit(R5_Wantwrite, &dev->flags);
  2103. }
  2104. if (failed >= 1) {
  2105. dev = &sh->dev[failed_num[0]];
  2106. locked++;
  2107. set_bit(R5_LOCKED, &dev->flags);
  2108. set_bit(R5_Wantwrite, &dev->flags);
  2109. }
  2110. if (update_p) {
  2111. dev = &sh->dev[pd_idx];
  2112. locked ++;
  2113. set_bit(R5_LOCKED, &dev->flags);
  2114. set_bit(R5_Wantwrite, &dev->flags);
  2115. }
  2116. if (update_q) {
  2117. dev = &sh->dev[qd_idx];
  2118. locked++;
  2119. set_bit(R5_LOCKED, &dev->flags);
  2120. set_bit(R5_Wantwrite, &dev->flags);
  2121. }
  2122. clear_bit(STRIPE_DEGRADED, &sh->state);
  2123. set_bit(STRIPE_INSYNC, &sh->state);
  2124. }
  2125. }
  2126. if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2127. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2128. clear_bit(STRIPE_SYNCING, &sh->state);
  2129. }
  2130. /* If the failed drives are just a ReadError, then we might need
  2131. * to progress the repair/check process
  2132. */
  2133. if (failed <= 2 && ! conf->mddev->ro)
  2134. for (i=0; i<failed;i++) {
  2135. dev = &sh->dev[failed_num[i]];
  2136. if (test_bit(R5_ReadError, &dev->flags)
  2137. && !test_bit(R5_LOCKED, &dev->flags)
  2138. && test_bit(R5_UPTODATE, &dev->flags)
  2139. ) {
  2140. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2141. set_bit(R5_Wantwrite, &dev->flags);
  2142. set_bit(R5_ReWrite, &dev->flags);
  2143. set_bit(R5_LOCKED, &dev->flags);
  2144. } else {
  2145. /* let's read it back */
  2146. set_bit(R5_Wantread, &dev->flags);
  2147. set_bit(R5_LOCKED, &dev->flags);
  2148. }
  2149. }
  2150. }
  2151. spin_unlock(&sh->lock);
  2152. while ((bi=return_bi)) {
  2153. int bytes = bi->bi_size;
  2154. return_bi = bi->bi_next;
  2155. bi->bi_next = NULL;
  2156. bi->bi_size = 0;
  2157. bi->bi_end_io(bi, bytes, 0);
  2158. }
  2159. for (i=disks; i-- ;) {
  2160. int rw;
  2161. struct bio *bi;
  2162. mdk_rdev_t *rdev;
  2163. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  2164. rw = 1;
  2165. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  2166. rw = 0;
  2167. else
  2168. continue;
  2169. bi = &sh->dev[i].req;
  2170. bi->bi_rw = rw;
  2171. if (rw)
  2172. bi->bi_end_io = raid5_end_write_request;
  2173. else
  2174. bi->bi_end_io = raid5_end_read_request;
  2175. rcu_read_lock();
  2176. rdev = rcu_dereference(conf->disks[i].rdev);
  2177. if (rdev && test_bit(Faulty, &rdev->flags))
  2178. rdev = NULL;
  2179. if (rdev)
  2180. atomic_inc(&rdev->nr_pending);
  2181. rcu_read_unlock();
  2182. if (rdev) {
  2183. if (syncing)
  2184. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  2185. bi->bi_bdev = rdev->bdev;
  2186. PRINTK("for %llu schedule op %ld on disc %d\n",
  2187. (unsigned long long)sh->sector, bi->bi_rw, i);
  2188. atomic_inc(&sh->count);
  2189. bi->bi_sector = sh->sector + rdev->data_offset;
  2190. bi->bi_flags = 1 << BIO_UPTODATE;
  2191. bi->bi_vcnt = 1;
  2192. bi->bi_max_vecs = 1;
  2193. bi->bi_idx = 0;
  2194. bi->bi_io_vec = &sh->dev[i].vec;
  2195. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  2196. bi->bi_io_vec[0].bv_offset = 0;
  2197. bi->bi_size = STRIPE_SIZE;
  2198. bi->bi_next = NULL;
  2199. if (rw == WRITE &&
  2200. test_bit(R5_ReWrite, &sh->dev[i].flags))
  2201. atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
  2202. generic_make_request(bi);
  2203. } else {
  2204. if (rw == 1)
  2205. set_bit(STRIPE_DEGRADED, &sh->state);
  2206. PRINTK("skip op %ld on disc %d for sector %llu\n",
  2207. bi->bi_rw, i, (unsigned long long)sh->sector);
  2208. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  2209. set_bit(STRIPE_HANDLE, &sh->state);
  2210. }
  2211. }
  2212. }
  2213. static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
  2214. {
  2215. if (sh->raid_conf->level == 6)
  2216. handle_stripe6(sh, tmp_page);
  2217. else
  2218. handle_stripe5(sh);
  2219. }
  2220. static void raid5_activate_delayed(raid5_conf_t *conf)
  2221. {
  2222. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  2223. while (!list_empty(&conf->delayed_list)) {
  2224. struct list_head *l = conf->delayed_list.next;
  2225. struct stripe_head *sh;
  2226. sh = list_entry(l, struct stripe_head, lru);
  2227. list_del_init(l);
  2228. clear_bit(STRIPE_DELAYED, &sh->state);
  2229. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2230. atomic_inc(&conf->preread_active_stripes);
  2231. list_add_tail(&sh->lru, &conf->handle_list);
  2232. }
  2233. }
  2234. }
  2235. static void activate_bit_delay(raid5_conf_t *conf)
  2236. {
  2237. /* device_lock is held */
  2238. struct list_head head;
  2239. list_add(&head, &conf->bitmap_list);
  2240. list_del_init(&conf->bitmap_list);
  2241. while (!list_empty(&head)) {
  2242. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  2243. list_del_init(&sh->lru);
  2244. atomic_inc(&sh->count);
  2245. __release_stripe(conf, sh);
  2246. }
  2247. }
  2248. static void unplug_slaves(mddev_t *mddev)
  2249. {
  2250. raid5_conf_t *conf = mddev_to_conf(mddev);
  2251. int i;
  2252. rcu_read_lock();
  2253. for (i=0; i<mddev->raid_disks; i++) {
  2254. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2255. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  2256. request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
  2257. atomic_inc(&rdev->nr_pending);
  2258. rcu_read_unlock();
  2259. if (r_queue->unplug_fn)
  2260. r_queue->unplug_fn(r_queue);
  2261. rdev_dec_pending(rdev, mddev);
  2262. rcu_read_lock();
  2263. }
  2264. }
  2265. rcu_read_unlock();
  2266. }
  2267. static void raid5_unplug_device(request_queue_t *q)
  2268. {
  2269. mddev_t *mddev = q->queuedata;
  2270. raid5_conf_t *conf = mddev_to_conf(mddev);
  2271. unsigned long flags;
  2272. spin_lock_irqsave(&conf->device_lock, flags);
  2273. if (blk_remove_plug(q)) {
  2274. conf->seq_flush++;
  2275. raid5_activate_delayed(conf);
  2276. }
  2277. md_wakeup_thread(mddev->thread);
  2278. spin_unlock_irqrestore(&conf->device_lock, flags);
  2279. unplug_slaves(mddev);
  2280. }
  2281. static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
  2282. sector_t *error_sector)
  2283. {
  2284. mddev_t *mddev = q->queuedata;
  2285. raid5_conf_t *conf = mddev_to_conf(mddev);
  2286. int i, ret = 0;
  2287. rcu_read_lock();
  2288. for (i=0; i<mddev->raid_disks && ret == 0; i++) {
  2289. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2290. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  2291. struct block_device *bdev = rdev->bdev;
  2292. request_queue_t *r_queue = bdev_get_queue(bdev);
  2293. if (!r_queue->issue_flush_fn)
  2294. ret = -EOPNOTSUPP;
  2295. else {
  2296. atomic_inc(&rdev->nr_pending);
  2297. rcu_read_unlock();
  2298. ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
  2299. error_sector);
  2300. rdev_dec_pending(rdev, mddev);
  2301. rcu_read_lock();
  2302. }
  2303. }
  2304. }
  2305. rcu_read_unlock();
  2306. return ret;
  2307. }
  2308. static inline void raid5_plug_device(raid5_conf_t *conf)
  2309. {
  2310. spin_lock_irq(&conf->device_lock);
  2311. blk_plug_device(conf->mddev->queue);
  2312. spin_unlock_irq(&conf->device_lock);
  2313. }
  2314. static int make_request(request_queue_t *q, struct bio * bi)
  2315. {
  2316. mddev_t *mddev = q->queuedata;
  2317. raid5_conf_t *conf = mddev_to_conf(mddev);
  2318. unsigned int dd_idx, pd_idx;
  2319. sector_t new_sector;
  2320. sector_t logical_sector, last_sector;
  2321. struct stripe_head *sh;
  2322. const int rw = bio_data_dir(bi);
  2323. int remaining;
  2324. if (unlikely(bio_barrier(bi))) {
  2325. bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
  2326. return 0;
  2327. }
  2328. md_write_start(mddev, bi);
  2329. disk_stat_inc(mddev->gendisk, ios[rw]);
  2330. disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
  2331. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  2332. last_sector = bi->bi_sector + (bi->bi_size>>9);
  2333. bi->bi_next = NULL;
  2334. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  2335. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  2336. DEFINE_WAIT(w);
  2337. int disks, data_disks;
  2338. retry:
  2339. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  2340. if (likely(conf->expand_progress == MaxSector))
  2341. disks = conf->raid_disks;
  2342. else {
  2343. /* spinlock is needed as expand_progress may be
  2344. * 64bit on a 32bit platform, and so it might be
  2345. * possible to see a half-updated value
  2346. * Ofcourse expand_progress could change after
  2347. * the lock is dropped, so once we get a reference
  2348. * to the stripe that we think it is, we will have
  2349. * to check again.
  2350. */
  2351. spin_lock_irq(&conf->device_lock);
  2352. disks = conf->raid_disks;
  2353. if (logical_sector >= conf->expand_progress)
  2354. disks = conf->previous_raid_disks;
  2355. else {
  2356. if (logical_sector >= conf->expand_lo) {
  2357. spin_unlock_irq(&conf->device_lock);
  2358. schedule();
  2359. goto retry;
  2360. }
  2361. }
  2362. spin_unlock_irq(&conf->device_lock);
  2363. }
  2364. data_disks = disks - conf->max_degraded;
  2365. new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
  2366. &dd_idx, &pd_idx, conf);
  2367. PRINTK("raid5: make_request, sector %llu logical %llu\n",
  2368. (unsigned long long)new_sector,
  2369. (unsigned long long)logical_sector);
  2370. sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
  2371. if (sh) {
  2372. if (unlikely(conf->expand_progress != MaxSector)) {
  2373. /* expansion might have moved on while waiting for a
  2374. * stripe, so we must do the range check again.
  2375. * Expansion could still move past after this
  2376. * test, but as we are holding a reference to
  2377. * 'sh', we know that if that happens,
  2378. * STRIPE_EXPANDING will get set and the expansion
  2379. * won't proceed until we finish with the stripe.
  2380. */
  2381. int must_retry = 0;
  2382. spin_lock_irq(&conf->device_lock);
  2383. if (logical_sector < conf->expand_progress &&
  2384. disks == conf->previous_raid_disks)
  2385. /* mismatch, need to try again */
  2386. must_retry = 1;
  2387. spin_unlock_irq(&conf->device_lock);
  2388. if (must_retry) {
  2389. release_stripe(sh);
  2390. goto retry;
  2391. }
  2392. }
  2393. /* FIXME what if we get a false positive because these
  2394. * are being updated.
  2395. */
  2396. if (logical_sector >= mddev->suspend_lo &&
  2397. logical_sector < mddev->suspend_hi) {
  2398. release_stripe(sh);
  2399. schedule();
  2400. goto retry;
  2401. }
  2402. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  2403. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  2404. /* Stripe is busy expanding or
  2405. * add failed due to overlap. Flush everything
  2406. * and wait a while
  2407. */
  2408. raid5_unplug_device(mddev->queue);
  2409. release_stripe(sh);
  2410. schedule();
  2411. goto retry;
  2412. }
  2413. finish_wait(&conf->wait_for_overlap, &w);
  2414. raid5_plug_device(conf);
  2415. handle_stripe(sh, NULL);
  2416. release_stripe(sh);
  2417. } else {
  2418. /* cannot get stripe for read-ahead, just give-up */
  2419. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  2420. finish_wait(&conf->wait_for_overlap, &w);
  2421. break;
  2422. }
  2423. }
  2424. spin_lock_irq(&conf->device_lock);
  2425. remaining = --bi->bi_phys_segments;
  2426. spin_unlock_irq(&conf->device_lock);
  2427. if (remaining == 0) {
  2428. int bytes = bi->bi_size;
  2429. if ( rw == WRITE )
  2430. md_write_end(mddev);
  2431. bi->bi_size = 0;
  2432. bi->bi_end_io(bi, bytes, 0);
  2433. }
  2434. return 0;
  2435. }
  2436. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  2437. {
  2438. /* reshaping is quite different to recovery/resync so it is
  2439. * handled quite separately ... here.
  2440. *
  2441. * On each call to sync_request, we gather one chunk worth of
  2442. * destination stripes and flag them as expanding.
  2443. * Then we find all the source stripes and request reads.
  2444. * As the reads complete, handle_stripe will copy the data
  2445. * into the destination stripe and release that stripe.
  2446. */
  2447. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2448. struct stripe_head *sh;
  2449. int pd_idx;
  2450. sector_t first_sector, last_sector;
  2451. int raid_disks;
  2452. int data_disks;
  2453. int i;
  2454. int dd_idx;
  2455. sector_t writepos, safepos, gap;
  2456. if (sector_nr == 0 &&
  2457. conf->expand_progress != 0) {
  2458. /* restarting in the middle, skip the initial sectors */
  2459. sector_nr = conf->expand_progress;
  2460. sector_div(sector_nr, conf->raid_disks-1);
  2461. *skipped = 1;
  2462. return sector_nr;
  2463. }
  2464. /* we update the metadata when there is more than 3Meg
  2465. * in the block range (that is rather arbitrary, should
  2466. * probably be time based) or when the data about to be
  2467. * copied would over-write the source of the data at
  2468. * the front of the range.
  2469. * i.e. one new_stripe forward from expand_progress new_maps
  2470. * to after where expand_lo old_maps to
  2471. */
  2472. writepos = conf->expand_progress +
  2473. conf->chunk_size/512*(conf->raid_disks-1);
  2474. sector_div(writepos, conf->raid_disks-1);
  2475. safepos = conf->expand_lo;
  2476. sector_div(safepos, conf->previous_raid_disks-1);
  2477. gap = conf->expand_progress - conf->expand_lo;
  2478. if (writepos >= safepos ||
  2479. gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
  2480. /* Cannot proceed until we've updated the superblock... */
  2481. wait_event(conf->wait_for_overlap,
  2482. atomic_read(&conf->reshape_stripes)==0);
  2483. mddev->reshape_position = conf->expand_progress;
  2484. mddev->sb_dirty = 1;
  2485. md_wakeup_thread(mddev->thread);
  2486. wait_event(mddev->sb_wait, mddev->sb_dirty == 0 ||
  2487. kthread_should_stop());
  2488. spin_lock_irq(&conf->device_lock);
  2489. conf->expand_lo = mddev->reshape_position;
  2490. spin_unlock_irq(&conf->device_lock);
  2491. wake_up(&conf->wait_for_overlap);
  2492. }
  2493. for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
  2494. int j;
  2495. int skipped = 0;
  2496. pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
  2497. sh = get_active_stripe(conf, sector_nr+i,
  2498. conf->raid_disks, pd_idx, 0);
  2499. set_bit(STRIPE_EXPANDING, &sh->state);
  2500. atomic_inc(&conf->reshape_stripes);
  2501. /* If any of this stripe is beyond the end of the old
  2502. * array, then we need to zero those blocks
  2503. */
  2504. for (j=sh->disks; j--;) {
  2505. sector_t s;
  2506. if (j == sh->pd_idx)
  2507. continue;
  2508. s = compute_blocknr(sh, j);
  2509. if (s < (mddev->array_size<<1)) {
  2510. skipped = 1;
  2511. continue;
  2512. }
  2513. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  2514. set_bit(R5_Expanded, &sh->dev[j].flags);
  2515. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  2516. }
  2517. if (!skipped) {
  2518. set_bit(STRIPE_EXPAND_READY, &sh->state);
  2519. set_bit(STRIPE_HANDLE, &sh->state);
  2520. }
  2521. release_stripe(sh);
  2522. }
  2523. spin_lock_irq(&conf->device_lock);
  2524. conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
  2525. spin_unlock_irq(&conf->device_lock);
  2526. /* Ok, those stripe are ready. We can start scheduling
  2527. * reads on the source stripes.
  2528. * The source stripes are determined by mapping the first and last
  2529. * block on the destination stripes.
  2530. */
  2531. raid_disks = conf->previous_raid_disks;
  2532. data_disks = raid_disks - 1;
  2533. first_sector =
  2534. raid5_compute_sector(sector_nr*(conf->raid_disks-1),
  2535. raid_disks, data_disks,
  2536. &dd_idx, &pd_idx, conf);
  2537. last_sector =
  2538. raid5_compute_sector((sector_nr+conf->chunk_size/512)
  2539. *(conf->raid_disks-1) -1,
  2540. raid_disks, data_disks,
  2541. &dd_idx, &pd_idx, conf);
  2542. if (last_sector >= (mddev->size<<1))
  2543. last_sector = (mddev->size<<1)-1;
  2544. while (first_sector <= last_sector) {
  2545. pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
  2546. sh = get_active_stripe(conf, first_sector,
  2547. conf->previous_raid_disks, pd_idx, 0);
  2548. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2549. set_bit(STRIPE_HANDLE, &sh->state);
  2550. release_stripe(sh);
  2551. first_sector += STRIPE_SECTORS;
  2552. }
  2553. return conf->chunk_size>>9;
  2554. }
  2555. /* FIXME go_faster isn't used */
  2556. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  2557. {
  2558. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2559. struct stripe_head *sh;
  2560. int pd_idx;
  2561. int raid_disks = conf->raid_disks;
  2562. sector_t max_sector = mddev->size << 1;
  2563. int sync_blocks;
  2564. int still_degraded = 0;
  2565. int i;
  2566. if (sector_nr >= max_sector) {
  2567. /* just being told to finish up .. nothing much to do */
  2568. unplug_slaves(mddev);
  2569. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  2570. end_reshape(conf);
  2571. return 0;
  2572. }
  2573. if (mddev->curr_resync < max_sector) /* aborted */
  2574. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2575. &sync_blocks, 1);
  2576. else /* completed sync */
  2577. conf->fullsync = 0;
  2578. bitmap_close_sync(mddev->bitmap);
  2579. return 0;
  2580. }
  2581. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2582. return reshape_request(mddev, sector_nr, skipped);
  2583. /* if there is too many failed drives and we are trying
  2584. * to resync, then assert that we are finished, because there is
  2585. * nothing we can do.
  2586. */
  2587. if (mddev->degraded >= conf->max_degraded &&
  2588. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2589. sector_t rv = (mddev->size << 1) - sector_nr;
  2590. *skipped = 1;
  2591. return rv;
  2592. }
  2593. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  2594. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  2595. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  2596. /* we can skip this block, and probably more */
  2597. sync_blocks /= STRIPE_SECTORS;
  2598. *skipped = 1;
  2599. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  2600. }
  2601. pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
  2602. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
  2603. if (sh == NULL) {
  2604. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
  2605. /* make sure we don't swamp the stripe cache if someone else
  2606. * is trying to get access
  2607. */
  2608. schedule_timeout_uninterruptible(1);
  2609. }
  2610. /* Need to check if array will still be degraded after recovery/resync
  2611. * We don't need to check the 'failed' flag as when that gets set,
  2612. * recovery aborts.
  2613. */
  2614. for (i=0; i<mddev->raid_disks; i++)
  2615. if (conf->disks[i].rdev == NULL)
  2616. still_degraded = 1;
  2617. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  2618. spin_lock(&sh->lock);
  2619. set_bit(STRIPE_SYNCING, &sh->state);
  2620. clear_bit(STRIPE_INSYNC, &sh->state);
  2621. spin_unlock(&sh->lock);
  2622. handle_stripe(sh, NULL);
  2623. release_stripe(sh);
  2624. return STRIPE_SECTORS;
  2625. }
  2626. /*
  2627. * This is our raid5 kernel thread.
  2628. *
  2629. * We scan the hash table for stripes which can be handled now.
  2630. * During the scan, completed stripes are saved for us by the interrupt
  2631. * handler, so that they will not have to wait for our next wakeup.
  2632. */
  2633. static void raid5d (mddev_t *mddev)
  2634. {
  2635. struct stripe_head *sh;
  2636. raid5_conf_t *conf = mddev_to_conf(mddev);
  2637. int handled;
  2638. PRINTK("+++ raid5d active\n");
  2639. md_check_recovery(mddev);
  2640. handled = 0;
  2641. spin_lock_irq(&conf->device_lock);
  2642. while (1) {
  2643. struct list_head *first;
  2644. if (conf->seq_flush - conf->seq_write > 0) {
  2645. int seq = conf->seq_flush;
  2646. spin_unlock_irq(&conf->device_lock);
  2647. bitmap_unplug(mddev->bitmap);
  2648. spin_lock_irq(&conf->device_lock);
  2649. conf->seq_write = seq;
  2650. activate_bit_delay(conf);
  2651. }
  2652. if (list_empty(&conf->handle_list) &&
  2653. atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
  2654. !blk_queue_plugged(mddev->queue) &&
  2655. !list_empty(&conf->delayed_list))
  2656. raid5_activate_delayed(conf);
  2657. if (list_empty(&conf->handle_list))
  2658. break;
  2659. first = conf->handle_list.next;
  2660. sh = list_entry(first, struct stripe_head, lru);
  2661. list_del_init(first);
  2662. atomic_inc(&sh->count);
  2663. BUG_ON(atomic_read(&sh->count)!= 1);
  2664. spin_unlock_irq(&conf->device_lock);
  2665. handled++;
  2666. handle_stripe(sh, conf->spare_page);
  2667. release_stripe(sh);
  2668. spin_lock_irq(&conf->device_lock);
  2669. }
  2670. PRINTK("%d stripes handled\n", handled);
  2671. spin_unlock_irq(&conf->device_lock);
  2672. unplug_slaves(mddev);
  2673. PRINTK("--- raid5d inactive\n");
  2674. }
  2675. static ssize_t
  2676. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  2677. {
  2678. raid5_conf_t *conf = mddev_to_conf(mddev);
  2679. if (conf)
  2680. return sprintf(page, "%d\n", conf->max_nr_stripes);
  2681. else
  2682. return 0;
  2683. }
  2684. static ssize_t
  2685. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  2686. {
  2687. raid5_conf_t *conf = mddev_to_conf(mddev);
  2688. char *end;
  2689. int new;
  2690. if (len >= PAGE_SIZE)
  2691. return -EINVAL;
  2692. if (!conf)
  2693. return -ENODEV;
  2694. new = simple_strtoul(page, &end, 10);
  2695. if (!*page || (*end && *end != '\n') )
  2696. return -EINVAL;
  2697. if (new <= 16 || new > 32768)
  2698. return -EINVAL;
  2699. while (new < conf->max_nr_stripes) {
  2700. if (drop_one_stripe(conf))
  2701. conf->max_nr_stripes--;
  2702. else
  2703. break;
  2704. }
  2705. while (new > conf->max_nr_stripes) {
  2706. if (grow_one_stripe(conf))
  2707. conf->max_nr_stripes++;
  2708. else break;
  2709. }
  2710. return len;
  2711. }
  2712. static struct md_sysfs_entry
  2713. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  2714. raid5_show_stripe_cache_size,
  2715. raid5_store_stripe_cache_size);
  2716. static ssize_t
  2717. stripe_cache_active_show(mddev_t *mddev, char *page)
  2718. {
  2719. raid5_conf_t *conf = mddev_to_conf(mddev);
  2720. if (conf)
  2721. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  2722. else
  2723. return 0;
  2724. }
  2725. static struct md_sysfs_entry
  2726. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  2727. static struct attribute *raid5_attrs[] = {
  2728. &raid5_stripecache_size.attr,
  2729. &raid5_stripecache_active.attr,
  2730. NULL,
  2731. };
  2732. static struct attribute_group raid5_attrs_group = {
  2733. .name = NULL,
  2734. .attrs = raid5_attrs,
  2735. };
  2736. static int run(mddev_t *mddev)
  2737. {
  2738. raid5_conf_t *conf;
  2739. int raid_disk, memory;
  2740. mdk_rdev_t *rdev;
  2741. struct disk_info *disk;
  2742. struct list_head *tmp;
  2743. if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
  2744. printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
  2745. mdname(mddev), mddev->level);
  2746. return -EIO;
  2747. }
  2748. if (mddev->reshape_position != MaxSector) {
  2749. /* Check that we can continue the reshape.
  2750. * Currently only disks can change, it must
  2751. * increase, and we must be past the point where
  2752. * a stripe over-writes itself
  2753. */
  2754. sector_t here_new, here_old;
  2755. int old_disks;
  2756. if (mddev->new_level != mddev->level ||
  2757. mddev->new_layout != mddev->layout ||
  2758. mddev->new_chunk != mddev->chunk_size) {
  2759. printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
  2760. mdname(mddev));
  2761. return -EINVAL;
  2762. }
  2763. if (mddev->delta_disks <= 0) {
  2764. printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
  2765. mdname(mddev));
  2766. return -EINVAL;
  2767. }
  2768. old_disks = mddev->raid_disks - mddev->delta_disks;
  2769. /* reshape_position must be on a new-stripe boundary, and one
  2770. * further up in new geometry must map after here in old geometry.
  2771. */
  2772. here_new = mddev->reshape_position;
  2773. if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
  2774. printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
  2775. return -EINVAL;
  2776. }
  2777. /* here_new is the stripe we will write to */
  2778. here_old = mddev->reshape_position;
  2779. sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
  2780. /* here_old is the first stripe that we might need to read from */
  2781. if (here_new >= here_old) {
  2782. /* Reading from the same stripe as writing to - bad */
  2783. printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
  2784. return -EINVAL;
  2785. }
  2786. printk(KERN_INFO "raid5: reshape will continue\n");
  2787. /* OK, we should be able to continue; */
  2788. }
  2789. mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
  2790. if ((conf = mddev->private) == NULL)
  2791. goto abort;
  2792. if (mddev->reshape_position == MaxSector) {
  2793. conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
  2794. } else {
  2795. conf->raid_disks = mddev->raid_disks;
  2796. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  2797. }
  2798. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  2799. GFP_KERNEL);
  2800. if (!conf->disks)
  2801. goto abort;
  2802. conf->mddev = mddev;
  2803. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  2804. goto abort;
  2805. if (mddev->level == 6) {
  2806. conf->spare_page = alloc_page(GFP_KERNEL);
  2807. if (!conf->spare_page)
  2808. goto abort;
  2809. }
  2810. spin_lock_init(&conf->device_lock);
  2811. init_waitqueue_head(&conf->wait_for_stripe);
  2812. init_waitqueue_head(&conf->wait_for_overlap);
  2813. INIT_LIST_HEAD(&conf->handle_list);
  2814. INIT_LIST_HEAD(&conf->delayed_list);
  2815. INIT_LIST_HEAD(&conf->bitmap_list);
  2816. INIT_LIST_HEAD(&conf->inactive_list);
  2817. atomic_set(&conf->active_stripes, 0);
  2818. atomic_set(&conf->preread_active_stripes, 0);
  2819. PRINTK("raid5: run(%s) called.\n", mdname(mddev));
  2820. ITERATE_RDEV(mddev,rdev,tmp) {
  2821. raid_disk = rdev->raid_disk;
  2822. if (raid_disk >= conf->raid_disks
  2823. || raid_disk < 0)
  2824. continue;
  2825. disk = conf->disks + raid_disk;
  2826. disk->rdev = rdev;
  2827. if (test_bit(In_sync, &rdev->flags)) {
  2828. char b[BDEVNAME_SIZE];
  2829. printk(KERN_INFO "raid5: device %s operational as raid"
  2830. " disk %d\n", bdevname(rdev->bdev,b),
  2831. raid_disk);
  2832. conf->working_disks++;
  2833. }
  2834. }
  2835. /*
  2836. * 0 for a fully functional array, 1 or 2 for a degraded array.
  2837. */
  2838. mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
  2839. conf->mddev = mddev;
  2840. conf->chunk_size = mddev->chunk_size;
  2841. conf->level = mddev->level;
  2842. if (conf->level == 6)
  2843. conf->max_degraded = 2;
  2844. else
  2845. conf->max_degraded = 1;
  2846. conf->algorithm = mddev->layout;
  2847. conf->max_nr_stripes = NR_STRIPES;
  2848. conf->expand_progress = mddev->reshape_position;
  2849. /* device size must be a multiple of chunk size */
  2850. mddev->size &= ~(mddev->chunk_size/1024 -1);
  2851. mddev->resync_max_sectors = mddev->size << 1;
  2852. if (conf->level == 6 && conf->raid_disks < 4) {
  2853. printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
  2854. mdname(mddev), conf->raid_disks);
  2855. goto abort;
  2856. }
  2857. if (!conf->chunk_size || conf->chunk_size % 4) {
  2858. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  2859. conf->chunk_size, mdname(mddev));
  2860. goto abort;
  2861. }
  2862. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  2863. printk(KERN_ERR
  2864. "raid5: unsupported parity algorithm %d for %s\n",
  2865. conf->algorithm, mdname(mddev));
  2866. goto abort;
  2867. }
  2868. if (mddev->degraded > conf->max_degraded) {
  2869. printk(KERN_ERR "raid5: not enough operational devices for %s"
  2870. " (%d/%d failed)\n",
  2871. mdname(mddev), conf->failed_disks, conf->raid_disks);
  2872. goto abort;
  2873. }
  2874. if (mddev->degraded > 0 &&
  2875. mddev->recovery_cp != MaxSector) {
  2876. if (mddev->ok_start_degraded)
  2877. printk(KERN_WARNING
  2878. "raid5: starting dirty degraded array: %s"
  2879. "- data corruption possible.\n",
  2880. mdname(mddev));
  2881. else {
  2882. printk(KERN_ERR
  2883. "raid5: cannot start dirty degraded array for %s\n",
  2884. mdname(mddev));
  2885. goto abort;
  2886. }
  2887. }
  2888. {
  2889. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  2890. if (!mddev->thread) {
  2891. printk(KERN_ERR
  2892. "raid5: couldn't allocate thread for %s\n",
  2893. mdname(mddev));
  2894. goto abort;
  2895. }
  2896. }
  2897. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  2898. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  2899. if (grow_stripes(conf, conf->max_nr_stripes)) {
  2900. printk(KERN_ERR
  2901. "raid5: couldn't allocate %dkB for buffers\n", memory);
  2902. shrink_stripes(conf);
  2903. md_unregister_thread(mddev->thread);
  2904. goto abort;
  2905. } else
  2906. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  2907. memory, mdname(mddev));
  2908. if (mddev->degraded == 0)
  2909. printk("raid5: raid level %d set %s active with %d out of %d"
  2910. " devices, algorithm %d\n", conf->level, mdname(mddev),
  2911. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  2912. conf->algorithm);
  2913. else
  2914. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  2915. " out of %d devices, algorithm %d\n", conf->level,
  2916. mdname(mddev), mddev->raid_disks - mddev->degraded,
  2917. mddev->raid_disks, conf->algorithm);
  2918. print_raid5_conf(conf);
  2919. if (conf->expand_progress != MaxSector) {
  2920. printk("...ok start reshape thread\n");
  2921. conf->expand_lo = conf->expand_progress;
  2922. atomic_set(&conf->reshape_stripes, 0);
  2923. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  2924. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  2925. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  2926. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  2927. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  2928. "%s_reshape");
  2929. /* FIXME if md_register_thread fails?? */
  2930. md_wakeup_thread(mddev->sync_thread);
  2931. }
  2932. /* read-ahead size must cover two whole stripes, which is
  2933. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  2934. */
  2935. {
  2936. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  2937. int stripe = data_disks *
  2938. (mddev->chunk_size / PAGE_SIZE);
  2939. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  2940. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  2941. }
  2942. /* Ok, everything is just fine now */
  2943. sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
  2944. mddev->queue->unplug_fn = raid5_unplug_device;
  2945. mddev->queue->issue_flush_fn = raid5_issue_flush;
  2946. mddev->array_size = mddev->size * (conf->previous_raid_disks -
  2947. conf->max_degraded);
  2948. return 0;
  2949. abort:
  2950. if (conf) {
  2951. print_raid5_conf(conf);
  2952. safe_put_page(conf->spare_page);
  2953. kfree(conf->disks);
  2954. kfree(conf->stripe_hashtbl);
  2955. kfree(conf);
  2956. }
  2957. mddev->private = NULL;
  2958. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  2959. return -EIO;
  2960. }
  2961. static int stop(mddev_t *mddev)
  2962. {
  2963. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  2964. md_unregister_thread(mddev->thread);
  2965. mddev->thread = NULL;
  2966. shrink_stripes(conf);
  2967. kfree(conf->stripe_hashtbl);
  2968. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  2969. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  2970. kfree(conf->disks);
  2971. kfree(conf);
  2972. mddev->private = NULL;
  2973. return 0;
  2974. }
  2975. #if RAID5_DEBUG
  2976. static void print_sh (struct seq_file *seq, struct stripe_head *sh)
  2977. {
  2978. int i;
  2979. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  2980. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  2981. seq_printf(seq, "sh %llu, count %d.\n",
  2982. (unsigned long long)sh->sector, atomic_read(&sh->count));
  2983. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  2984. for (i = 0; i < sh->disks; i++) {
  2985. seq_printf(seq, "(cache%d: %p %ld) ",
  2986. i, sh->dev[i].page, sh->dev[i].flags);
  2987. }
  2988. seq_printf(seq, "\n");
  2989. }
  2990. static void printall (struct seq_file *seq, raid5_conf_t *conf)
  2991. {
  2992. struct stripe_head *sh;
  2993. struct hlist_node *hn;
  2994. int i;
  2995. spin_lock_irq(&conf->device_lock);
  2996. for (i = 0; i < NR_HASH; i++) {
  2997. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  2998. if (sh->raid_conf != conf)
  2999. continue;
  3000. print_sh(seq, sh);
  3001. }
  3002. }
  3003. spin_unlock_irq(&conf->device_lock);
  3004. }
  3005. #endif
  3006. static void status (struct seq_file *seq, mddev_t *mddev)
  3007. {
  3008. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3009. int i;
  3010. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  3011. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
  3012. for (i = 0; i < conf->raid_disks; i++)
  3013. seq_printf (seq, "%s",
  3014. conf->disks[i].rdev &&
  3015. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  3016. seq_printf (seq, "]");
  3017. #if RAID5_DEBUG
  3018. seq_printf (seq, "\n");
  3019. printall(seq, conf);
  3020. #endif
  3021. }
  3022. static void print_raid5_conf (raid5_conf_t *conf)
  3023. {
  3024. int i;
  3025. struct disk_info *tmp;
  3026. printk("RAID5 conf printout:\n");
  3027. if (!conf) {
  3028. printk("(conf==NULL)\n");
  3029. return;
  3030. }
  3031. printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
  3032. conf->working_disks, conf->failed_disks);
  3033. for (i = 0; i < conf->raid_disks; i++) {
  3034. char b[BDEVNAME_SIZE];
  3035. tmp = conf->disks + i;
  3036. if (tmp->rdev)
  3037. printk(" disk %d, o:%d, dev:%s\n",
  3038. i, !test_bit(Faulty, &tmp->rdev->flags),
  3039. bdevname(tmp->rdev->bdev,b));
  3040. }
  3041. }
  3042. static int raid5_spare_active(mddev_t *mddev)
  3043. {
  3044. int i;
  3045. raid5_conf_t *conf = mddev->private;
  3046. struct disk_info *tmp;
  3047. for (i = 0; i < conf->raid_disks; i++) {
  3048. tmp = conf->disks + i;
  3049. if (tmp->rdev
  3050. && !test_bit(Faulty, &tmp->rdev->flags)
  3051. && !test_bit(In_sync, &tmp->rdev->flags)) {
  3052. mddev->degraded--;
  3053. conf->failed_disks--;
  3054. conf->working_disks++;
  3055. set_bit(In_sync, &tmp->rdev->flags);
  3056. }
  3057. }
  3058. print_raid5_conf(conf);
  3059. return 0;
  3060. }
  3061. static int raid5_remove_disk(mddev_t *mddev, int number)
  3062. {
  3063. raid5_conf_t *conf = mddev->private;
  3064. int err = 0;
  3065. mdk_rdev_t *rdev;
  3066. struct disk_info *p = conf->disks + number;
  3067. print_raid5_conf(conf);
  3068. rdev = p->rdev;
  3069. if (rdev) {
  3070. if (test_bit(In_sync, &rdev->flags) ||
  3071. atomic_read(&rdev->nr_pending)) {
  3072. err = -EBUSY;
  3073. goto abort;
  3074. }
  3075. p->rdev = NULL;
  3076. synchronize_rcu();
  3077. if (atomic_read(&rdev->nr_pending)) {
  3078. /* lost the race, try later */
  3079. err = -EBUSY;
  3080. p->rdev = rdev;
  3081. }
  3082. }
  3083. abort:
  3084. print_raid5_conf(conf);
  3085. return err;
  3086. }
  3087. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  3088. {
  3089. raid5_conf_t *conf = mddev->private;
  3090. int found = 0;
  3091. int disk;
  3092. struct disk_info *p;
  3093. if (mddev->degraded > conf->max_degraded)
  3094. /* no point adding a device */
  3095. return 0;
  3096. /*
  3097. * find the disk ... but prefer rdev->saved_raid_disk
  3098. * if possible.
  3099. */
  3100. if (rdev->saved_raid_disk >= 0 &&
  3101. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  3102. disk = rdev->saved_raid_disk;
  3103. else
  3104. disk = 0;
  3105. for ( ; disk < conf->raid_disks; disk++)
  3106. if ((p=conf->disks + disk)->rdev == NULL) {
  3107. clear_bit(In_sync, &rdev->flags);
  3108. rdev->raid_disk = disk;
  3109. found = 1;
  3110. if (rdev->saved_raid_disk != disk)
  3111. conf->fullsync = 1;
  3112. rcu_assign_pointer(p->rdev, rdev);
  3113. break;
  3114. }
  3115. print_raid5_conf(conf);
  3116. return found;
  3117. }
  3118. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  3119. {
  3120. /* no resync is happening, and there is enough space
  3121. * on all devices, so we can resize.
  3122. * We need to make sure resync covers any new space.
  3123. * If the array is shrinking we should possibly wait until
  3124. * any io in the removed space completes, but it hardly seems
  3125. * worth it.
  3126. */
  3127. raid5_conf_t *conf = mddev_to_conf(mddev);
  3128. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  3129. mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
  3130. set_capacity(mddev->gendisk, mddev->array_size << 1);
  3131. mddev->changed = 1;
  3132. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  3133. mddev->recovery_cp = mddev->size << 1;
  3134. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3135. }
  3136. mddev->size = sectors /2;
  3137. mddev->resync_max_sectors = sectors;
  3138. return 0;
  3139. }
  3140. #ifdef CONFIG_MD_RAID5_RESHAPE
  3141. static int raid5_check_reshape(mddev_t *mddev)
  3142. {
  3143. raid5_conf_t *conf = mddev_to_conf(mddev);
  3144. int err;
  3145. if (mddev->delta_disks < 0 ||
  3146. mddev->new_level != mddev->level)
  3147. return -EINVAL; /* Cannot shrink array or change level yet */
  3148. if (mddev->delta_disks == 0)
  3149. return 0; /* nothing to do */
  3150. /* Can only proceed if there are plenty of stripe_heads.
  3151. * We need a minimum of one full stripe,, and for sensible progress
  3152. * it is best to have about 4 times that.
  3153. * If we require 4 times, then the default 256 4K stripe_heads will
  3154. * allow for chunk sizes up to 256K, which is probably OK.
  3155. * If the chunk size is greater, user-space should request more
  3156. * stripe_heads first.
  3157. */
  3158. if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
  3159. (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
  3160. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  3161. (mddev->chunk_size / STRIPE_SIZE)*4);
  3162. return -ENOSPC;
  3163. }
  3164. err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  3165. if (err)
  3166. return err;
  3167. /* looks like we might be able to manage this */
  3168. return 0;
  3169. }
  3170. static int raid5_start_reshape(mddev_t *mddev)
  3171. {
  3172. raid5_conf_t *conf = mddev_to_conf(mddev);
  3173. mdk_rdev_t *rdev;
  3174. struct list_head *rtmp;
  3175. int spares = 0;
  3176. int added_devices = 0;
  3177. if (mddev->degraded ||
  3178. test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3179. return -EBUSY;
  3180. ITERATE_RDEV(mddev, rdev, rtmp)
  3181. if (rdev->raid_disk < 0 &&
  3182. !test_bit(Faulty, &rdev->flags))
  3183. spares++;
  3184. if (spares < mddev->delta_disks-1)
  3185. /* Not enough devices even to make a degraded array
  3186. * of that size
  3187. */
  3188. return -EINVAL;
  3189. atomic_set(&conf->reshape_stripes, 0);
  3190. spin_lock_irq(&conf->device_lock);
  3191. conf->previous_raid_disks = conf->raid_disks;
  3192. conf->raid_disks += mddev->delta_disks;
  3193. conf->expand_progress = 0;
  3194. conf->expand_lo = 0;
  3195. spin_unlock_irq(&conf->device_lock);
  3196. /* Add some new drives, as many as will fit.
  3197. * We know there are enough to make the newly sized array work.
  3198. */
  3199. ITERATE_RDEV(mddev, rdev, rtmp)
  3200. if (rdev->raid_disk < 0 &&
  3201. !test_bit(Faulty, &rdev->flags)) {
  3202. if (raid5_add_disk(mddev, rdev)) {
  3203. char nm[20];
  3204. set_bit(In_sync, &rdev->flags);
  3205. conf->working_disks++;
  3206. added_devices++;
  3207. rdev->recovery_offset = 0;
  3208. sprintf(nm, "rd%d", rdev->raid_disk);
  3209. sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
  3210. } else
  3211. break;
  3212. }
  3213. mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
  3214. mddev->raid_disks = conf->raid_disks;
  3215. mddev->reshape_position = 0;
  3216. mddev->sb_dirty = 1;
  3217. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3218. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3219. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3220. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3221. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3222. "%s_reshape");
  3223. if (!mddev->sync_thread) {
  3224. mddev->recovery = 0;
  3225. spin_lock_irq(&conf->device_lock);
  3226. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  3227. conf->expand_progress = MaxSector;
  3228. spin_unlock_irq(&conf->device_lock);
  3229. return -EAGAIN;
  3230. }
  3231. md_wakeup_thread(mddev->sync_thread);
  3232. md_new_event(mddev);
  3233. return 0;
  3234. }
  3235. #endif
  3236. static void end_reshape(raid5_conf_t *conf)
  3237. {
  3238. struct block_device *bdev;
  3239. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  3240. conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
  3241. set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
  3242. conf->mddev->changed = 1;
  3243. bdev = bdget_disk(conf->mddev->gendisk, 0);
  3244. if (bdev) {
  3245. mutex_lock(&bdev->bd_inode->i_mutex);
  3246. i_size_write(bdev->bd_inode, conf->mddev->array_size << 10);
  3247. mutex_unlock(&bdev->bd_inode->i_mutex);
  3248. bdput(bdev);
  3249. }
  3250. spin_lock_irq(&conf->device_lock);
  3251. conf->expand_progress = MaxSector;
  3252. spin_unlock_irq(&conf->device_lock);
  3253. conf->mddev->reshape_position = MaxSector;
  3254. /* read-ahead size must cover two whole stripes, which is
  3255. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3256. */
  3257. {
  3258. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  3259. int stripe = data_disks *
  3260. (conf->mddev->chunk_size / PAGE_SIZE);
  3261. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3262. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3263. }
  3264. }
  3265. }
  3266. static void raid5_quiesce(mddev_t *mddev, int state)
  3267. {
  3268. raid5_conf_t *conf = mddev_to_conf(mddev);
  3269. switch(state) {
  3270. case 2: /* resume for a suspend */
  3271. wake_up(&conf->wait_for_overlap);
  3272. break;
  3273. case 1: /* stop all writes */
  3274. spin_lock_irq(&conf->device_lock);
  3275. conf->quiesce = 1;
  3276. wait_event_lock_irq(conf->wait_for_stripe,
  3277. atomic_read(&conf->active_stripes) == 0,
  3278. conf->device_lock, /* nothing */);
  3279. spin_unlock_irq(&conf->device_lock);
  3280. break;
  3281. case 0: /* re-enable writes */
  3282. spin_lock_irq(&conf->device_lock);
  3283. conf->quiesce = 0;
  3284. wake_up(&conf->wait_for_stripe);
  3285. wake_up(&conf->wait_for_overlap);
  3286. spin_unlock_irq(&conf->device_lock);
  3287. break;
  3288. }
  3289. }
  3290. static struct mdk_personality raid6_personality =
  3291. {
  3292. .name = "raid6",
  3293. .level = 6,
  3294. .owner = THIS_MODULE,
  3295. .make_request = make_request,
  3296. .run = run,
  3297. .stop = stop,
  3298. .status = status,
  3299. .error_handler = error,
  3300. .hot_add_disk = raid5_add_disk,
  3301. .hot_remove_disk= raid5_remove_disk,
  3302. .spare_active = raid5_spare_active,
  3303. .sync_request = sync_request,
  3304. .resize = raid5_resize,
  3305. .quiesce = raid5_quiesce,
  3306. };
  3307. static struct mdk_personality raid5_personality =
  3308. {
  3309. .name = "raid5",
  3310. .level = 5,
  3311. .owner = THIS_MODULE,
  3312. .make_request = make_request,
  3313. .run = run,
  3314. .stop = stop,
  3315. .status = status,
  3316. .error_handler = error,
  3317. .hot_add_disk = raid5_add_disk,
  3318. .hot_remove_disk= raid5_remove_disk,
  3319. .spare_active = raid5_spare_active,
  3320. .sync_request = sync_request,
  3321. .resize = raid5_resize,
  3322. #ifdef CONFIG_MD_RAID5_RESHAPE
  3323. .check_reshape = raid5_check_reshape,
  3324. .start_reshape = raid5_start_reshape,
  3325. #endif
  3326. .quiesce = raid5_quiesce,
  3327. };
  3328. static struct mdk_personality raid4_personality =
  3329. {
  3330. .name = "raid4",
  3331. .level = 4,
  3332. .owner = THIS_MODULE,
  3333. .make_request = make_request,
  3334. .run = run,
  3335. .stop = stop,
  3336. .status = status,
  3337. .error_handler = error,
  3338. .hot_add_disk = raid5_add_disk,
  3339. .hot_remove_disk= raid5_remove_disk,
  3340. .spare_active = raid5_spare_active,
  3341. .sync_request = sync_request,
  3342. .resize = raid5_resize,
  3343. .quiesce = raid5_quiesce,
  3344. };
  3345. static int __init raid5_init(void)
  3346. {
  3347. int e;
  3348. e = raid6_select_algo();
  3349. if ( e )
  3350. return e;
  3351. register_md_personality(&raid6_personality);
  3352. register_md_personality(&raid5_personality);
  3353. register_md_personality(&raid4_personality);
  3354. return 0;
  3355. }
  3356. static void raid5_exit(void)
  3357. {
  3358. unregister_md_personality(&raid6_personality);
  3359. unregister_md_personality(&raid5_personality);
  3360. unregister_md_personality(&raid4_personality);
  3361. }
  3362. module_init(raid5_init);
  3363. module_exit(raid5_exit);
  3364. MODULE_LICENSE("GPL");
  3365. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  3366. MODULE_ALIAS("md-raid5");
  3367. MODULE_ALIAS("md-raid4");
  3368. MODULE_ALIAS("md-level-5");
  3369. MODULE_ALIAS("md-level-4");
  3370. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  3371. MODULE_ALIAS("md-raid6");
  3372. MODULE_ALIAS("md-level-6");
  3373. /* This used to be two separate modules, they were: */
  3374. MODULE_ALIAS("raid5");
  3375. MODULE_ALIAS("raid6");