raid5.c 107 KB

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