raid5.c 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774
  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. #include <linux/async_tx.h>
  53. /*
  54. * Stripe cache
  55. */
  56. #define NR_STRIPES 256
  57. #define STRIPE_SIZE PAGE_SIZE
  58. #define STRIPE_SHIFT (PAGE_SHIFT - 9)
  59. #define STRIPE_SECTORS (STRIPE_SIZE>>9)
  60. #define IO_THRESHOLD 1
  61. #define BYPASS_THRESHOLD 1
  62. #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
  63. #define HASH_MASK (NR_HASH - 1)
  64. #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
  65. /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  66. * order without overlap. There may be several bio's per stripe+device, and
  67. * a bio could span several devices.
  68. * When walking this list for a particular stripe+device, we must never proceed
  69. * beyond a bio that extends past this device, as the next bio might no longer
  70. * be valid.
  71. * This macro is used to determine the 'next' bio in the list, given the sector
  72. * of the current stripe+device
  73. */
  74. #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
  75. /*
  76. * The following can be used to debug the driver
  77. */
  78. #define RAID5_PARANOIA 1
  79. #if RAID5_PARANOIA && defined(CONFIG_SMP)
  80. # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
  81. #else
  82. # define CHECK_DEVLOCK()
  83. #endif
  84. #ifdef DEBUG
  85. #define inline
  86. #define __inline__
  87. #endif
  88. #define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
  89. #if !RAID6_USE_EMPTY_ZERO_PAGE
  90. /* In .bss so it's zeroed */
  91. const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
  92. #endif
  93. /*
  94. * We maintain a biased count of active stripes in the bottom 16 bits of
  95. * bi_phys_segments, and a count of processed stripes in the upper 16 bits
  96. */
  97. static inline int raid5_bi_phys_segments(struct bio *bio)
  98. {
  99. return bio->bi_phys_segments & 0xffff;
  100. }
  101. static inline int raid5_bi_hw_segments(struct bio *bio)
  102. {
  103. return (bio->bi_phys_segments >> 16) & 0xffff;
  104. }
  105. static inline int raid5_dec_bi_phys_segments(struct bio *bio)
  106. {
  107. --bio->bi_phys_segments;
  108. return raid5_bi_phys_segments(bio);
  109. }
  110. static inline int raid5_dec_bi_hw_segments(struct bio *bio)
  111. {
  112. unsigned short val = raid5_bi_hw_segments(bio);
  113. --val;
  114. bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
  115. return val;
  116. }
  117. static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
  118. {
  119. bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
  120. }
  121. static inline int raid6_next_disk(int disk, int raid_disks)
  122. {
  123. disk++;
  124. return (disk < raid_disks) ? disk : 0;
  125. }
  126. static void return_io(struct bio *return_bi)
  127. {
  128. struct bio *bi = return_bi;
  129. while (bi) {
  130. return_bi = bi->bi_next;
  131. bi->bi_next = NULL;
  132. bi->bi_size = 0;
  133. bio_endio(bi, 0);
  134. bi = return_bi;
  135. }
  136. }
  137. static void print_raid5_conf (raid5_conf_t *conf);
  138. static int stripe_operations_active(struct stripe_head *sh)
  139. {
  140. return sh->check_state || sh->reconstruct_state ||
  141. test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
  142. test_bit(STRIPE_COMPUTE_RUN, &sh->state);
  143. }
  144. static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
  145. {
  146. if (atomic_dec_and_test(&sh->count)) {
  147. BUG_ON(!list_empty(&sh->lru));
  148. BUG_ON(atomic_read(&conf->active_stripes)==0);
  149. if (test_bit(STRIPE_HANDLE, &sh->state)) {
  150. if (test_bit(STRIPE_DELAYED, &sh->state)) {
  151. list_add_tail(&sh->lru, &conf->delayed_list);
  152. blk_plug_device(conf->mddev->queue);
  153. } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
  154. sh->bm_seq - conf->seq_write > 0) {
  155. list_add_tail(&sh->lru, &conf->bitmap_list);
  156. blk_plug_device(conf->mddev->queue);
  157. } else {
  158. clear_bit(STRIPE_BIT_DELAY, &sh->state);
  159. list_add_tail(&sh->lru, &conf->handle_list);
  160. }
  161. md_wakeup_thread(conf->mddev->thread);
  162. } else {
  163. BUG_ON(stripe_operations_active(sh));
  164. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  165. atomic_dec(&conf->preread_active_stripes);
  166. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
  167. md_wakeup_thread(conf->mddev->thread);
  168. }
  169. atomic_dec(&conf->active_stripes);
  170. if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
  171. list_add_tail(&sh->lru, &conf->inactive_list);
  172. wake_up(&conf->wait_for_stripe);
  173. if (conf->retry_read_aligned)
  174. md_wakeup_thread(conf->mddev->thread);
  175. }
  176. }
  177. }
  178. }
  179. static void release_stripe(struct stripe_head *sh)
  180. {
  181. raid5_conf_t *conf = sh->raid_conf;
  182. unsigned long flags;
  183. spin_lock_irqsave(&conf->device_lock, flags);
  184. __release_stripe(conf, sh);
  185. spin_unlock_irqrestore(&conf->device_lock, flags);
  186. }
  187. static inline void remove_hash(struct stripe_head *sh)
  188. {
  189. pr_debug("remove_hash(), stripe %llu\n",
  190. (unsigned long long)sh->sector);
  191. hlist_del_init(&sh->hash);
  192. }
  193. static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
  194. {
  195. struct hlist_head *hp = stripe_hash(conf, sh->sector);
  196. pr_debug("insert_hash(), stripe %llu\n",
  197. (unsigned long long)sh->sector);
  198. CHECK_DEVLOCK();
  199. hlist_add_head(&sh->hash, hp);
  200. }
  201. /* find an idle stripe, make sure it is unhashed, and return it. */
  202. static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
  203. {
  204. struct stripe_head *sh = NULL;
  205. struct list_head *first;
  206. CHECK_DEVLOCK();
  207. if (list_empty(&conf->inactive_list))
  208. goto out;
  209. first = conf->inactive_list.next;
  210. sh = list_entry(first, struct stripe_head, lru);
  211. list_del_init(first);
  212. remove_hash(sh);
  213. atomic_inc(&conf->active_stripes);
  214. out:
  215. return sh;
  216. }
  217. static void shrink_buffers(struct stripe_head *sh, int num)
  218. {
  219. struct page *p;
  220. int i;
  221. for (i=0; i<num ; i++) {
  222. p = sh->dev[i].page;
  223. if (!p)
  224. continue;
  225. sh->dev[i].page = NULL;
  226. put_page(p);
  227. }
  228. }
  229. static int grow_buffers(struct stripe_head *sh, int num)
  230. {
  231. int i;
  232. for (i=0; i<num; i++) {
  233. struct page *page;
  234. if (!(page = alloc_page(GFP_KERNEL))) {
  235. return 1;
  236. }
  237. sh->dev[i].page = page;
  238. }
  239. return 0;
  240. }
  241. static void raid5_build_block (struct stripe_head *sh, int i);
  242. static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
  243. {
  244. raid5_conf_t *conf = sh->raid_conf;
  245. int i;
  246. BUG_ON(atomic_read(&sh->count) != 0);
  247. BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
  248. BUG_ON(stripe_operations_active(sh));
  249. CHECK_DEVLOCK();
  250. pr_debug("init_stripe called, stripe %llu\n",
  251. (unsigned long long)sh->sector);
  252. remove_hash(sh);
  253. sh->sector = sector;
  254. sh->pd_idx = pd_idx;
  255. sh->state = 0;
  256. sh->disks = disks;
  257. for (i = sh->disks; i--; ) {
  258. struct r5dev *dev = &sh->dev[i];
  259. if (dev->toread || dev->read || dev->towrite || dev->written ||
  260. test_bit(R5_LOCKED, &dev->flags)) {
  261. printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
  262. (unsigned long long)sh->sector, i, dev->toread,
  263. dev->read, dev->towrite, dev->written,
  264. test_bit(R5_LOCKED, &dev->flags));
  265. BUG();
  266. }
  267. dev->flags = 0;
  268. raid5_build_block(sh, i);
  269. }
  270. insert_hash(conf, sh);
  271. }
  272. static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
  273. {
  274. struct stripe_head *sh;
  275. struct hlist_node *hn;
  276. CHECK_DEVLOCK();
  277. pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
  278. hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
  279. if (sh->sector == sector && sh->disks == disks)
  280. return sh;
  281. pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
  282. return NULL;
  283. }
  284. static void unplug_slaves(mddev_t *mddev);
  285. static void raid5_unplug_device(struct request_queue *q);
  286. static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
  287. int pd_idx, int noblock)
  288. {
  289. struct stripe_head *sh;
  290. pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
  291. spin_lock_irq(&conf->device_lock);
  292. do {
  293. wait_event_lock_irq(conf->wait_for_stripe,
  294. conf->quiesce == 0,
  295. conf->device_lock, /* nothing */);
  296. sh = __find_stripe(conf, sector, disks);
  297. if (!sh) {
  298. if (!conf->inactive_blocked)
  299. sh = get_free_stripe(conf);
  300. if (noblock && sh == NULL)
  301. break;
  302. if (!sh) {
  303. conf->inactive_blocked = 1;
  304. wait_event_lock_irq(conf->wait_for_stripe,
  305. !list_empty(&conf->inactive_list) &&
  306. (atomic_read(&conf->active_stripes)
  307. < (conf->max_nr_stripes *3/4)
  308. || !conf->inactive_blocked),
  309. conf->device_lock,
  310. raid5_unplug_device(conf->mddev->queue)
  311. );
  312. conf->inactive_blocked = 0;
  313. } else
  314. init_stripe(sh, sector, pd_idx, disks);
  315. } else {
  316. if (atomic_read(&sh->count)) {
  317. BUG_ON(!list_empty(&sh->lru));
  318. } else {
  319. if (!test_bit(STRIPE_HANDLE, &sh->state))
  320. atomic_inc(&conf->active_stripes);
  321. if (list_empty(&sh->lru) &&
  322. !test_bit(STRIPE_EXPANDING, &sh->state))
  323. BUG();
  324. list_del_init(&sh->lru);
  325. }
  326. }
  327. } while (sh == NULL);
  328. if (sh)
  329. atomic_inc(&sh->count);
  330. spin_unlock_irq(&conf->device_lock);
  331. return sh;
  332. }
  333. static void
  334. raid5_end_read_request(struct bio *bi, int error);
  335. static void
  336. raid5_end_write_request(struct bio *bi, int error);
  337. static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
  338. {
  339. raid5_conf_t *conf = sh->raid_conf;
  340. int i, disks = sh->disks;
  341. might_sleep();
  342. for (i = disks; i--; ) {
  343. int rw;
  344. struct bio *bi;
  345. mdk_rdev_t *rdev;
  346. if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
  347. rw = WRITE;
  348. else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
  349. rw = READ;
  350. else
  351. continue;
  352. bi = &sh->dev[i].req;
  353. bi->bi_rw = rw;
  354. if (rw == WRITE)
  355. bi->bi_end_io = raid5_end_write_request;
  356. else
  357. bi->bi_end_io = raid5_end_read_request;
  358. rcu_read_lock();
  359. rdev = rcu_dereference(conf->disks[i].rdev);
  360. if (rdev && test_bit(Faulty, &rdev->flags))
  361. rdev = NULL;
  362. if (rdev)
  363. atomic_inc(&rdev->nr_pending);
  364. rcu_read_unlock();
  365. if (rdev) {
  366. if (s->syncing || s->expanding || s->expanded)
  367. md_sync_acct(rdev->bdev, STRIPE_SECTORS);
  368. set_bit(STRIPE_IO_STARTED, &sh->state);
  369. bi->bi_bdev = rdev->bdev;
  370. pr_debug("%s: for %llu schedule op %ld on disc %d\n",
  371. __func__, (unsigned long long)sh->sector,
  372. bi->bi_rw, i);
  373. atomic_inc(&sh->count);
  374. bi->bi_sector = sh->sector + rdev->data_offset;
  375. bi->bi_flags = 1 << BIO_UPTODATE;
  376. bi->bi_vcnt = 1;
  377. bi->bi_max_vecs = 1;
  378. bi->bi_idx = 0;
  379. bi->bi_io_vec = &sh->dev[i].vec;
  380. bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
  381. bi->bi_io_vec[0].bv_offset = 0;
  382. bi->bi_size = STRIPE_SIZE;
  383. bi->bi_next = NULL;
  384. if (rw == WRITE &&
  385. test_bit(R5_ReWrite, &sh->dev[i].flags))
  386. atomic_add(STRIPE_SECTORS,
  387. &rdev->corrected_errors);
  388. generic_make_request(bi);
  389. } else {
  390. if (rw == WRITE)
  391. set_bit(STRIPE_DEGRADED, &sh->state);
  392. pr_debug("skip op %ld on disc %d for sector %llu\n",
  393. bi->bi_rw, i, (unsigned long long)sh->sector);
  394. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  395. set_bit(STRIPE_HANDLE, &sh->state);
  396. }
  397. }
  398. }
  399. static struct dma_async_tx_descriptor *
  400. async_copy_data(int frombio, struct bio *bio, struct page *page,
  401. sector_t sector, struct dma_async_tx_descriptor *tx)
  402. {
  403. struct bio_vec *bvl;
  404. struct page *bio_page;
  405. int i;
  406. int page_offset;
  407. if (bio->bi_sector >= sector)
  408. page_offset = (signed)(bio->bi_sector - sector) * 512;
  409. else
  410. page_offset = (signed)(sector - bio->bi_sector) * -512;
  411. bio_for_each_segment(bvl, bio, i) {
  412. int len = bio_iovec_idx(bio, i)->bv_len;
  413. int clen;
  414. int b_offset = 0;
  415. if (page_offset < 0) {
  416. b_offset = -page_offset;
  417. page_offset += b_offset;
  418. len -= b_offset;
  419. }
  420. if (len > 0 && page_offset + len > STRIPE_SIZE)
  421. clen = STRIPE_SIZE - page_offset;
  422. else
  423. clen = len;
  424. if (clen > 0) {
  425. b_offset += bio_iovec_idx(bio, i)->bv_offset;
  426. bio_page = bio_iovec_idx(bio, i)->bv_page;
  427. if (frombio)
  428. tx = async_memcpy(page, bio_page, page_offset,
  429. b_offset, clen,
  430. ASYNC_TX_DEP_ACK,
  431. tx, NULL, NULL);
  432. else
  433. tx = async_memcpy(bio_page, page, b_offset,
  434. page_offset, clen,
  435. ASYNC_TX_DEP_ACK,
  436. tx, NULL, NULL);
  437. }
  438. if (clen < len) /* hit end of page */
  439. break;
  440. page_offset += len;
  441. }
  442. return tx;
  443. }
  444. static void ops_complete_biofill(void *stripe_head_ref)
  445. {
  446. struct stripe_head *sh = stripe_head_ref;
  447. struct bio *return_bi = NULL;
  448. raid5_conf_t *conf = sh->raid_conf;
  449. int i;
  450. pr_debug("%s: stripe %llu\n", __func__,
  451. (unsigned long long)sh->sector);
  452. /* clear completed biofills */
  453. spin_lock_irq(&conf->device_lock);
  454. for (i = sh->disks; i--; ) {
  455. struct r5dev *dev = &sh->dev[i];
  456. /* acknowledge completion of a biofill operation */
  457. /* and check if we need to reply to a read request,
  458. * new R5_Wantfill requests are held off until
  459. * !STRIPE_BIOFILL_RUN
  460. */
  461. if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
  462. struct bio *rbi, *rbi2;
  463. BUG_ON(!dev->read);
  464. rbi = dev->read;
  465. dev->read = NULL;
  466. while (rbi && rbi->bi_sector <
  467. dev->sector + STRIPE_SECTORS) {
  468. rbi2 = r5_next_bio(rbi, dev->sector);
  469. if (!raid5_dec_bi_phys_segments(rbi)) {
  470. rbi->bi_next = return_bi;
  471. return_bi = rbi;
  472. }
  473. rbi = rbi2;
  474. }
  475. }
  476. }
  477. spin_unlock_irq(&conf->device_lock);
  478. clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
  479. return_io(return_bi);
  480. set_bit(STRIPE_HANDLE, &sh->state);
  481. release_stripe(sh);
  482. }
  483. static void ops_run_biofill(struct stripe_head *sh)
  484. {
  485. struct dma_async_tx_descriptor *tx = NULL;
  486. raid5_conf_t *conf = sh->raid_conf;
  487. int i;
  488. pr_debug("%s: stripe %llu\n", __func__,
  489. (unsigned long long)sh->sector);
  490. for (i = sh->disks; i--; ) {
  491. struct r5dev *dev = &sh->dev[i];
  492. if (test_bit(R5_Wantfill, &dev->flags)) {
  493. struct bio *rbi;
  494. spin_lock_irq(&conf->device_lock);
  495. dev->read = rbi = dev->toread;
  496. dev->toread = NULL;
  497. spin_unlock_irq(&conf->device_lock);
  498. while (rbi && rbi->bi_sector <
  499. dev->sector + STRIPE_SECTORS) {
  500. tx = async_copy_data(0, rbi, dev->page,
  501. dev->sector, tx);
  502. rbi = r5_next_bio(rbi, dev->sector);
  503. }
  504. }
  505. }
  506. atomic_inc(&sh->count);
  507. async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
  508. ops_complete_biofill, sh);
  509. }
  510. static void ops_complete_compute5(void *stripe_head_ref)
  511. {
  512. struct stripe_head *sh = stripe_head_ref;
  513. int target = sh->ops.target;
  514. struct r5dev *tgt = &sh->dev[target];
  515. pr_debug("%s: stripe %llu\n", __func__,
  516. (unsigned long long)sh->sector);
  517. set_bit(R5_UPTODATE, &tgt->flags);
  518. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  519. clear_bit(R5_Wantcompute, &tgt->flags);
  520. clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
  521. if (sh->check_state == check_state_compute_run)
  522. sh->check_state = check_state_compute_result;
  523. set_bit(STRIPE_HANDLE, &sh->state);
  524. release_stripe(sh);
  525. }
  526. static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
  527. {
  528. /* kernel stack size limits the total number of disks */
  529. int disks = sh->disks;
  530. struct page *xor_srcs[disks];
  531. int target = sh->ops.target;
  532. struct r5dev *tgt = &sh->dev[target];
  533. struct page *xor_dest = tgt->page;
  534. int count = 0;
  535. struct dma_async_tx_descriptor *tx;
  536. int i;
  537. pr_debug("%s: stripe %llu block: %d\n",
  538. __func__, (unsigned long long)sh->sector, target);
  539. BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
  540. for (i = disks; i--; )
  541. if (i != target)
  542. xor_srcs[count++] = sh->dev[i].page;
  543. atomic_inc(&sh->count);
  544. if (unlikely(count == 1))
  545. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
  546. 0, NULL, ops_complete_compute5, sh);
  547. else
  548. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  549. ASYNC_TX_XOR_ZERO_DST, NULL,
  550. ops_complete_compute5, sh);
  551. return tx;
  552. }
  553. static void ops_complete_prexor(void *stripe_head_ref)
  554. {
  555. struct stripe_head *sh = stripe_head_ref;
  556. pr_debug("%s: stripe %llu\n", __func__,
  557. (unsigned long long)sh->sector);
  558. }
  559. static struct dma_async_tx_descriptor *
  560. ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  561. {
  562. /* kernel stack size limits the total number of disks */
  563. int disks = sh->disks;
  564. struct page *xor_srcs[disks];
  565. int count = 0, pd_idx = sh->pd_idx, i;
  566. /* existing parity data subtracted */
  567. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  568. pr_debug("%s: stripe %llu\n", __func__,
  569. (unsigned long long)sh->sector);
  570. for (i = disks; i--; ) {
  571. struct r5dev *dev = &sh->dev[i];
  572. /* Only process blocks that are known to be uptodate */
  573. if (test_bit(R5_Wantdrain, &dev->flags))
  574. xor_srcs[count++] = dev->page;
  575. }
  576. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  577. ASYNC_TX_DEP_ACK | ASYNC_TX_XOR_DROP_DST, tx,
  578. ops_complete_prexor, sh);
  579. return tx;
  580. }
  581. static struct dma_async_tx_descriptor *
  582. ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  583. {
  584. int disks = sh->disks;
  585. int i;
  586. pr_debug("%s: stripe %llu\n", __func__,
  587. (unsigned long long)sh->sector);
  588. for (i = disks; i--; ) {
  589. struct r5dev *dev = &sh->dev[i];
  590. struct bio *chosen;
  591. if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
  592. struct bio *wbi;
  593. spin_lock(&sh->lock);
  594. chosen = dev->towrite;
  595. dev->towrite = NULL;
  596. BUG_ON(dev->written);
  597. wbi = dev->written = chosen;
  598. spin_unlock(&sh->lock);
  599. while (wbi && wbi->bi_sector <
  600. dev->sector + STRIPE_SECTORS) {
  601. tx = async_copy_data(1, wbi, dev->page,
  602. dev->sector, tx);
  603. wbi = r5_next_bio(wbi, dev->sector);
  604. }
  605. }
  606. }
  607. return tx;
  608. }
  609. static void ops_complete_postxor(void *stripe_head_ref)
  610. {
  611. struct stripe_head *sh = stripe_head_ref;
  612. int disks = sh->disks, i, pd_idx = sh->pd_idx;
  613. pr_debug("%s: stripe %llu\n", __func__,
  614. (unsigned long long)sh->sector);
  615. for (i = disks; i--; ) {
  616. struct r5dev *dev = &sh->dev[i];
  617. if (dev->written || i == pd_idx)
  618. set_bit(R5_UPTODATE, &dev->flags);
  619. }
  620. if (sh->reconstruct_state == reconstruct_state_drain_run)
  621. sh->reconstruct_state = reconstruct_state_drain_result;
  622. else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
  623. sh->reconstruct_state = reconstruct_state_prexor_drain_result;
  624. else {
  625. BUG_ON(sh->reconstruct_state != reconstruct_state_run);
  626. sh->reconstruct_state = reconstruct_state_result;
  627. }
  628. set_bit(STRIPE_HANDLE, &sh->state);
  629. release_stripe(sh);
  630. }
  631. static void
  632. ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
  633. {
  634. /* kernel stack size limits the total number of disks */
  635. int disks = sh->disks;
  636. struct page *xor_srcs[disks];
  637. int count = 0, pd_idx = sh->pd_idx, i;
  638. struct page *xor_dest;
  639. int prexor = 0;
  640. unsigned long flags;
  641. pr_debug("%s: stripe %llu\n", __func__,
  642. (unsigned long long)sh->sector);
  643. /* check if prexor is active which means only process blocks
  644. * that are part of a read-modify-write (written)
  645. */
  646. if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
  647. prexor = 1;
  648. xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  649. for (i = disks; i--; ) {
  650. struct r5dev *dev = &sh->dev[i];
  651. if (dev->written)
  652. xor_srcs[count++] = dev->page;
  653. }
  654. } else {
  655. xor_dest = sh->dev[pd_idx].page;
  656. for (i = disks; i--; ) {
  657. struct r5dev *dev = &sh->dev[i];
  658. if (i != pd_idx)
  659. xor_srcs[count++] = dev->page;
  660. }
  661. }
  662. /* 1/ if we prexor'd then the dest is reused as a source
  663. * 2/ if we did not prexor then we are redoing the parity
  664. * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
  665. * for the synchronous xor case
  666. */
  667. flags = ASYNC_TX_DEP_ACK | ASYNC_TX_ACK |
  668. (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
  669. atomic_inc(&sh->count);
  670. if (unlikely(count == 1)) {
  671. flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST);
  672. tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
  673. flags, tx, ops_complete_postxor, sh);
  674. } else
  675. tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  676. flags, tx, ops_complete_postxor, sh);
  677. }
  678. static void ops_complete_check(void *stripe_head_ref)
  679. {
  680. struct stripe_head *sh = stripe_head_ref;
  681. pr_debug("%s: stripe %llu\n", __func__,
  682. (unsigned long long)sh->sector);
  683. sh->check_state = check_state_check_result;
  684. set_bit(STRIPE_HANDLE, &sh->state);
  685. release_stripe(sh);
  686. }
  687. static void ops_run_check(struct stripe_head *sh)
  688. {
  689. /* kernel stack size limits the total number of disks */
  690. int disks = sh->disks;
  691. struct page *xor_srcs[disks];
  692. struct dma_async_tx_descriptor *tx;
  693. int count = 0, pd_idx = sh->pd_idx, i;
  694. struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
  695. pr_debug("%s: stripe %llu\n", __func__,
  696. (unsigned long long)sh->sector);
  697. for (i = disks; i--; ) {
  698. struct r5dev *dev = &sh->dev[i];
  699. if (i != pd_idx)
  700. xor_srcs[count++] = dev->page;
  701. }
  702. tx = async_xor_zero_sum(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
  703. &sh->ops.zero_sum_result, 0, NULL, NULL, NULL);
  704. atomic_inc(&sh->count);
  705. tx = async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
  706. ops_complete_check, sh);
  707. }
  708. static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
  709. {
  710. int overlap_clear = 0, i, disks = sh->disks;
  711. struct dma_async_tx_descriptor *tx = NULL;
  712. if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
  713. ops_run_biofill(sh);
  714. overlap_clear++;
  715. }
  716. if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
  717. tx = ops_run_compute5(sh);
  718. /* terminate the chain if postxor is not set to be run */
  719. if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
  720. async_tx_ack(tx);
  721. }
  722. if (test_bit(STRIPE_OP_PREXOR, &ops_request))
  723. tx = ops_run_prexor(sh, tx);
  724. if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
  725. tx = ops_run_biodrain(sh, tx);
  726. overlap_clear++;
  727. }
  728. if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
  729. ops_run_postxor(sh, tx);
  730. if (test_bit(STRIPE_OP_CHECK, &ops_request))
  731. ops_run_check(sh);
  732. if (overlap_clear)
  733. for (i = disks; i--; ) {
  734. struct r5dev *dev = &sh->dev[i];
  735. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  736. wake_up(&sh->raid_conf->wait_for_overlap);
  737. }
  738. }
  739. static int grow_one_stripe(raid5_conf_t *conf)
  740. {
  741. struct stripe_head *sh;
  742. sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
  743. if (!sh)
  744. return 0;
  745. memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
  746. sh->raid_conf = conf;
  747. spin_lock_init(&sh->lock);
  748. if (grow_buffers(sh, conf->raid_disks)) {
  749. shrink_buffers(sh, conf->raid_disks);
  750. kmem_cache_free(conf->slab_cache, sh);
  751. return 0;
  752. }
  753. sh->disks = conf->raid_disks;
  754. /* we just created an active stripe so... */
  755. atomic_set(&sh->count, 1);
  756. atomic_inc(&conf->active_stripes);
  757. INIT_LIST_HEAD(&sh->lru);
  758. release_stripe(sh);
  759. return 1;
  760. }
  761. static int grow_stripes(raid5_conf_t *conf, int num)
  762. {
  763. struct kmem_cache *sc;
  764. int devs = conf->raid_disks;
  765. sprintf(conf->cache_name[0], "raid5-%s", mdname(conf->mddev));
  766. sprintf(conf->cache_name[1], "raid5-%s-alt", mdname(conf->mddev));
  767. conf->active_name = 0;
  768. sc = kmem_cache_create(conf->cache_name[conf->active_name],
  769. sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
  770. 0, 0, NULL);
  771. if (!sc)
  772. return 1;
  773. conf->slab_cache = sc;
  774. conf->pool_size = devs;
  775. while (num--)
  776. if (!grow_one_stripe(conf))
  777. return 1;
  778. return 0;
  779. }
  780. #ifdef CONFIG_MD_RAID5_RESHAPE
  781. static int resize_stripes(raid5_conf_t *conf, int newsize)
  782. {
  783. /* Make all the stripes able to hold 'newsize' devices.
  784. * New slots in each stripe get 'page' set to a new page.
  785. *
  786. * This happens in stages:
  787. * 1/ create a new kmem_cache and allocate the required number of
  788. * stripe_heads.
  789. * 2/ gather all the old stripe_heads and tranfer the pages across
  790. * to the new stripe_heads. This will have the side effect of
  791. * freezing the array as once all stripe_heads have been collected,
  792. * no IO will be possible. Old stripe heads are freed once their
  793. * pages have been transferred over, and the old kmem_cache is
  794. * freed when all stripes are done.
  795. * 3/ reallocate conf->disks to be suitable bigger. If this fails,
  796. * we simple return a failre status - no need to clean anything up.
  797. * 4/ allocate new pages for the new slots in the new stripe_heads.
  798. * If this fails, we don't bother trying the shrink the
  799. * stripe_heads down again, we just leave them as they are.
  800. * As each stripe_head is processed the new one is released into
  801. * active service.
  802. *
  803. * Once step2 is started, we cannot afford to wait for a write,
  804. * so we use GFP_NOIO allocations.
  805. */
  806. struct stripe_head *osh, *nsh;
  807. LIST_HEAD(newstripes);
  808. struct disk_info *ndisks;
  809. int err;
  810. struct kmem_cache *sc;
  811. int i;
  812. if (newsize <= conf->pool_size)
  813. return 0; /* never bother to shrink */
  814. err = md_allow_write(conf->mddev);
  815. if (err)
  816. return err;
  817. /* Step 1 */
  818. sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
  819. sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
  820. 0, 0, NULL);
  821. if (!sc)
  822. return -ENOMEM;
  823. for (i = conf->max_nr_stripes; i; i--) {
  824. nsh = kmem_cache_alloc(sc, GFP_KERNEL);
  825. if (!nsh)
  826. break;
  827. memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
  828. nsh->raid_conf = conf;
  829. spin_lock_init(&nsh->lock);
  830. list_add(&nsh->lru, &newstripes);
  831. }
  832. if (i) {
  833. /* didn't get enough, give up */
  834. while (!list_empty(&newstripes)) {
  835. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  836. list_del(&nsh->lru);
  837. kmem_cache_free(sc, nsh);
  838. }
  839. kmem_cache_destroy(sc);
  840. return -ENOMEM;
  841. }
  842. /* Step 2 - Must use GFP_NOIO now.
  843. * OK, we have enough stripes, start collecting inactive
  844. * stripes and copying them over
  845. */
  846. list_for_each_entry(nsh, &newstripes, lru) {
  847. spin_lock_irq(&conf->device_lock);
  848. wait_event_lock_irq(conf->wait_for_stripe,
  849. !list_empty(&conf->inactive_list),
  850. conf->device_lock,
  851. unplug_slaves(conf->mddev)
  852. );
  853. osh = get_free_stripe(conf);
  854. spin_unlock_irq(&conf->device_lock);
  855. atomic_set(&nsh->count, 1);
  856. for(i=0; i<conf->pool_size; i++)
  857. nsh->dev[i].page = osh->dev[i].page;
  858. for( ; i<newsize; i++)
  859. nsh->dev[i].page = NULL;
  860. kmem_cache_free(conf->slab_cache, osh);
  861. }
  862. kmem_cache_destroy(conf->slab_cache);
  863. /* Step 3.
  864. * At this point, we are holding all the stripes so the array
  865. * is completely stalled, so now is a good time to resize
  866. * conf->disks.
  867. */
  868. ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
  869. if (ndisks) {
  870. for (i=0; i<conf->raid_disks; i++)
  871. ndisks[i] = conf->disks[i];
  872. kfree(conf->disks);
  873. conf->disks = ndisks;
  874. } else
  875. err = -ENOMEM;
  876. /* Step 4, return new stripes to service */
  877. while(!list_empty(&newstripes)) {
  878. nsh = list_entry(newstripes.next, struct stripe_head, lru);
  879. list_del_init(&nsh->lru);
  880. for (i=conf->raid_disks; i < newsize; i++)
  881. if (nsh->dev[i].page == NULL) {
  882. struct page *p = alloc_page(GFP_NOIO);
  883. nsh->dev[i].page = p;
  884. if (!p)
  885. err = -ENOMEM;
  886. }
  887. release_stripe(nsh);
  888. }
  889. /* critical section pass, GFP_NOIO no longer needed */
  890. conf->slab_cache = sc;
  891. conf->active_name = 1-conf->active_name;
  892. conf->pool_size = newsize;
  893. return err;
  894. }
  895. #endif
  896. static int drop_one_stripe(raid5_conf_t *conf)
  897. {
  898. struct stripe_head *sh;
  899. spin_lock_irq(&conf->device_lock);
  900. sh = get_free_stripe(conf);
  901. spin_unlock_irq(&conf->device_lock);
  902. if (!sh)
  903. return 0;
  904. BUG_ON(atomic_read(&sh->count));
  905. shrink_buffers(sh, conf->pool_size);
  906. kmem_cache_free(conf->slab_cache, sh);
  907. atomic_dec(&conf->active_stripes);
  908. return 1;
  909. }
  910. static void shrink_stripes(raid5_conf_t *conf)
  911. {
  912. while (drop_one_stripe(conf))
  913. ;
  914. if (conf->slab_cache)
  915. kmem_cache_destroy(conf->slab_cache);
  916. conf->slab_cache = NULL;
  917. }
  918. static void raid5_end_read_request(struct bio * bi, int error)
  919. {
  920. struct stripe_head *sh = bi->bi_private;
  921. raid5_conf_t *conf = sh->raid_conf;
  922. int disks = sh->disks, i;
  923. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  924. char b[BDEVNAME_SIZE];
  925. mdk_rdev_t *rdev;
  926. for (i=0 ; i<disks; i++)
  927. if (bi == &sh->dev[i].req)
  928. break;
  929. pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
  930. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  931. uptodate);
  932. if (i == disks) {
  933. BUG();
  934. return;
  935. }
  936. if (uptodate) {
  937. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  938. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  939. rdev = conf->disks[i].rdev;
  940. printk_rl(KERN_INFO "raid5:%s: read error corrected"
  941. " (%lu sectors at %llu on %s)\n",
  942. mdname(conf->mddev), STRIPE_SECTORS,
  943. (unsigned long long)(sh->sector
  944. + rdev->data_offset),
  945. bdevname(rdev->bdev, b));
  946. clear_bit(R5_ReadError, &sh->dev[i].flags);
  947. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  948. }
  949. if (atomic_read(&conf->disks[i].rdev->read_errors))
  950. atomic_set(&conf->disks[i].rdev->read_errors, 0);
  951. } else {
  952. const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
  953. int retry = 0;
  954. rdev = conf->disks[i].rdev;
  955. clear_bit(R5_UPTODATE, &sh->dev[i].flags);
  956. atomic_inc(&rdev->read_errors);
  957. if (conf->mddev->degraded)
  958. printk_rl(KERN_WARNING
  959. "raid5:%s: read error not correctable "
  960. "(sector %llu on %s).\n",
  961. mdname(conf->mddev),
  962. (unsigned long long)(sh->sector
  963. + rdev->data_offset),
  964. bdn);
  965. else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
  966. /* Oh, no!!! */
  967. printk_rl(KERN_WARNING
  968. "raid5:%s: read error NOT corrected!! "
  969. "(sector %llu on %s).\n",
  970. mdname(conf->mddev),
  971. (unsigned long long)(sh->sector
  972. + rdev->data_offset),
  973. bdn);
  974. else if (atomic_read(&rdev->read_errors)
  975. > conf->max_nr_stripes)
  976. printk(KERN_WARNING
  977. "raid5:%s: Too many read errors, failing device %s.\n",
  978. mdname(conf->mddev), bdn);
  979. else
  980. retry = 1;
  981. if (retry)
  982. set_bit(R5_ReadError, &sh->dev[i].flags);
  983. else {
  984. clear_bit(R5_ReadError, &sh->dev[i].flags);
  985. clear_bit(R5_ReWrite, &sh->dev[i].flags);
  986. md_error(conf->mddev, rdev);
  987. }
  988. }
  989. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  990. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  991. set_bit(STRIPE_HANDLE, &sh->state);
  992. release_stripe(sh);
  993. }
  994. static void raid5_end_write_request (struct bio *bi, int error)
  995. {
  996. struct stripe_head *sh = bi->bi_private;
  997. raid5_conf_t *conf = sh->raid_conf;
  998. int disks = sh->disks, i;
  999. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  1000. for (i=0 ; i<disks; i++)
  1001. if (bi == &sh->dev[i].req)
  1002. break;
  1003. pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
  1004. (unsigned long long)sh->sector, i, atomic_read(&sh->count),
  1005. uptodate);
  1006. if (i == disks) {
  1007. BUG();
  1008. return;
  1009. }
  1010. if (!uptodate)
  1011. md_error(conf->mddev, conf->disks[i].rdev);
  1012. rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
  1013. clear_bit(R5_LOCKED, &sh->dev[i].flags);
  1014. set_bit(STRIPE_HANDLE, &sh->state);
  1015. release_stripe(sh);
  1016. }
  1017. static sector_t compute_blocknr(struct stripe_head *sh, int i);
  1018. static void raid5_build_block (struct stripe_head *sh, int i)
  1019. {
  1020. struct r5dev *dev = &sh->dev[i];
  1021. bio_init(&dev->req);
  1022. dev->req.bi_io_vec = &dev->vec;
  1023. dev->req.bi_vcnt++;
  1024. dev->req.bi_max_vecs++;
  1025. dev->vec.bv_page = dev->page;
  1026. dev->vec.bv_len = STRIPE_SIZE;
  1027. dev->vec.bv_offset = 0;
  1028. dev->req.bi_sector = sh->sector;
  1029. dev->req.bi_private = sh;
  1030. dev->flags = 0;
  1031. dev->sector = compute_blocknr(sh, i);
  1032. }
  1033. static void error(mddev_t *mddev, mdk_rdev_t *rdev)
  1034. {
  1035. char b[BDEVNAME_SIZE];
  1036. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  1037. pr_debug("raid5: error called\n");
  1038. if (!test_bit(Faulty, &rdev->flags)) {
  1039. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1040. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1041. unsigned long flags;
  1042. spin_lock_irqsave(&conf->device_lock, flags);
  1043. mddev->degraded++;
  1044. spin_unlock_irqrestore(&conf->device_lock, flags);
  1045. /*
  1046. * if recovery was running, make sure it aborts.
  1047. */
  1048. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1049. }
  1050. set_bit(Faulty, &rdev->flags);
  1051. printk (KERN_ALERT
  1052. "raid5: Disk failure on %s, disabling device.\n"
  1053. "raid5: Operation continuing on %d devices.\n",
  1054. bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
  1055. }
  1056. }
  1057. /*
  1058. * Input: a 'big' sector number,
  1059. * Output: index of the data and parity disk, and the sector # in them.
  1060. */
  1061. static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
  1062. unsigned int data_disks, unsigned int * dd_idx,
  1063. unsigned int * pd_idx, raid5_conf_t *conf)
  1064. {
  1065. long stripe;
  1066. unsigned long chunk_number;
  1067. unsigned int chunk_offset;
  1068. sector_t new_sector;
  1069. int sectors_per_chunk = conf->chunk_size >> 9;
  1070. /* First compute the information on this sector */
  1071. /*
  1072. * Compute the chunk number and the sector offset inside the chunk
  1073. */
  1074. chunk_offset = sector_div(r_sector, sectors_per_chunk);
  1075. chunk_number = r_sector;
  1076. BUG_ON(r_sector != chunk_number);
  1077. /*
  1078. * Compute the stripe number
  1079. */
  1080. stripe = chunk_number / data_disks;
  1081. /*
  1082. * Compute the data disk and parity disk indexes inside the stripe
  1083. */
  1084. *dd_idx = chunk_number % data_disks;
  1085. /*
  1086. * Select the parity disk based on the user selected algorithm.
  1087. */
  1088. switch(conf->level) {
  1089. case 4:
  1090. *pd_idx = data_disks;
  1091. break;
  1092. case 5:
  1093. switch (conf->algorithm) {
  1094. case ALGORITHM_LEFT_ASYMMETRIC:
  1095. *pd_idx = data_disks - stripe % raid_disks;
  1096. if (*dd_idx >= *pd_idx)
  1097. (*dd_idx)++;
  1098. break;
  1099. case ALGORITHM_RIGHT_ASYMMETRIC:
  1100. *pd_idx = stripe % raid_disks;
  1101. if (*dd_idx >= *pd_idx)
  1102. (*dd_idx)++;
  1103. break;
  1104. case ALGORITHM_LEFT_SYMMETRIC:
  1105. *pd_idx = data_disks - stripe % raid_disks;
  1106. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  1107. break;
  1108. case ALGORITHM_RIGHT_SYMMETRIC:
  1109. *pd_idx = stripe % raid_disks;
  1110. *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
  1111. break;
  1112. default:
  1113. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  1114. conf->algorithm);
  1115. }
  1116. break;
  1117. case 6:
  1118. /**** FIX THIS ****/
  1119. switch (conf->algorithm) {
  1120. case ALGORITHM_LEFT_ASYMMETRIC:
  1121. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1122. if (*pd_idx == raid_disks-1)
  1123. (*dd_idx)++; /* Q D D D P */
  1124. else if (*dd_idx >= *pd_idx)
  1125. (*dd_idx) += 2; /* D D P Q D */
  1126. break;
  1127. case ALGORITHM_RIGHT_ASYMMETRIC:
  1128. *pd_idx = stripe % raid_disks;
  1129. if (*pd_idx == raid_disks-1)
  1130. (*dd_idx)++; /* Q D D D P */
  1131. else if (*dd_idx >= *pd_idx)
  1132. (*dd_idx) += 2; /* D D P Q D */
  1133. break;
  1134. case ALGORITHM_LEFT_SYMMETRIC:
  1135. *pd_idx = raid_disks - 1 - (stripe % raid_disks);
  1136. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  1137. break;
  1138. case ALGORITHM_RIGHT_SYMMETRIC:
  1139. *pd_idx = stripe % raid_disks;
  1140. *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
  1141. break;
  1142. default:
  1143. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  1144. conf->algorithm);
  1145. }
  1146. break;
  1147. }
  1148. /*
  1149. * Finally, compute the new sector number
  1150. */
  1151. new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
  1152. return new_sector;
  1153. }
  1154. static sector_t compute_blocknr(struct stripe_head *sh, int i)
  1155. {
  1156. raid5_conf_t *conf = sh->raid_conf;
  1157. int raid_disks = sh->disks;
  1158. int data_disks = raid_disks - conf->max_degraded;
  1159. sector_t new_sector = sh->sector, check;
  1160. int sectors_per_chunk = conf->chunk_size >> 9;
  1161. sector_t stripe;
  1162. int chunk_offset;
  1163. int chunk_number, dummy1, dummy2, dd_idx = i;
  1164. sector_t r_sector;
  1165. chunk_offset = sector_div(new_sector, sectors_per_chunk);
  1166. stripe = new_sector;
  1167. BUG_ON(new_sector != stripe);
  1168. if (i == sh->pd_idx)
  1169. return 0;
  1170. switch(conf->level) {
  1171. case 4: break;
  1172. case 5:
  1173. switch (conf->algorithm) {
  1174. case ALGORITHM_LEFT_ASYMMETRIC:
  1175. case ALGORITHM_RIGHT_ASYMMETRIC:
  1176. if (i > sh->pd_idx)
  1177. i--;
  1178. break;
  1179. case ALGORITHM_LEFT_SYMMETRIC:
  1180. case ALGORITHM_RIGHT_SYMMETRIC:
  1181. if (i < sh->pd_idx)
  1182. i += raid_disks;
  1183. i -= (sh->pd_idx + 1);
  1184. break;
  1185. default:
  1186. printk(KERN_ERR "raid5: unsupported algorithm %d\n",
  1187. conf->algorithm);
  1188. }
  1189. break;
  1190. case 6:
  1191. if (i == raid6_next_disk(sh->pd_idx, raid_disks))
  1192. return 0; /* It is the Q disk */
  1193. switch (conf->algorithm) {
  1194. case ALGORITHM_LEFT_ASYMMETRIC:
  1195. case ALGORITHM_RIGHT_ASYMMETRIC:
  1196. if (sh->pd_idx == raid_disks-1)
  1197. i--; /* Q D D D P */
  1198. else if (i > sh->pd_idx)
  1199. i -= 2; /* D D P Q D */
  1200. break;
  1201. case ALGORITHM_LEFT_SYMMETRIC:
  1202. case ALGORITHM_RIGHT_SYMMETRIC:
  1203. if (sh->pd_idx == raid_disks-1)
  1204. i--; /* Q D D D P */
  1205. else {
  1206. /* D D P Q D */
  1207. if (i < sh->pd_idx)
  1208. i += raid_disks;
  1209. i -= (sh->pd_idx + 2);
  1210. }
  1211. break;
  1212. default:
  1213. printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
  1214. conf->algorithm);
  1215. }
  1216. break;
  1217. }
  1218. chunk_number = stripe * data_disks + i;
  1219. r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
  1220. check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
  1221. if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
  1222. printk(KERN_ERR "compute_blocknr: map not correct\n");
  1223. return 0;
  1224. }
  1225. return r_sector;
  1226. }
  1227. /*
  1228. * Copy data between a page in the stripe cache, and one or more bion
  1229. * The page could align with the middle of the bio, or there could be
  1230. * several bion, each with several bio_vecs, which cover part of the page
  1231. * Multiple bion are linked together on bi_next. There may be extras
  1232. * at the end of this list. We ignore them.
  1233. */
  1234. static void copy_data(int frombio, struct bio *bio,
  1235. struct page *page,
  1236. sector_t sector)
  1237. {
  1238. char *pa = page_address(page);
  1239. struct bio_vec *bvl;
  1240. int i;
  1241. int page_offset;
  1242. if (bio->bi_sector >= sector)
  1243. page_offset = (signed)(bio->bi_sector - sector) * 512;
  1244. else
  1245. page_offset = (signed)(sector - bio->bi_sector) * -512;
  1246. bio_for_each_segment(bvl, bio, i) {
  1247. int len = bio_iovec_idx(bio,i)->bv_len;
  1248. int clen;
  1249. int b_offset = 0;
  1250. if (page_offset < 0) {
  1251. b_offset = -page_offset;
  1252. page_offset += b_offset;
  1253. len -= b_offset;
  1254. }
  1255. if (len > 0 && page_offset + len > STRIPE_SIZE)
  1256. clen = STRIPE_SIZE - page_offset;
  1257. else clen = len;
  1258. if (clen > 0) {
  1259. char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
  1260. if (frombio)
  1261. memcpy(pa+page_offset, ba+b_offset, clen);
  1262. else
  1263. memcpy(ba+b_offset, pa+page_offset, clen);
  1264. __bio_kunmap_atomic(ba, KM_USER0);
  1265. }
  1266. if (clen < len) /* hit end of page */
  1267. break;
  1268. page_offset += len;
  1269. }
  1270. }
  1271. #define check_xor() do { \
  1272. if (count == MAX_XOR_BLOCKS) { \
  1273. xor_blocks(count, STRIPE_SIZE, dest, ptr);\
  1274. count = 0; \
  1275. } \
  1276. } while(0)
  1277. static void compute_parity6(struct stripe_head *sh, int method)
  1278. {
  1279. raid6_conf_t *conf = sh->raid_conf;
  1280. int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
  1281. struct bio *chosen;
  1282. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1283. void *ptrs[disks];
  1284. qd_idx = raid6_next_disk(pd_idx, disks);
  1285. d0_idx = raid6_next_disk(qd_idx, disks);
  1286. pr_debug("compute_parity, stripe %llu, method %d\n",
  1287. (unsigned long long)sh->sector, method);
  1288. switch(method) {
  1289. case READ_MODIFY_WRITE:
  1290. BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
  1291. case RECONSTRUCT_WRITE:
  1292. for (i= disks; i-- ;)
  1293. if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
  1294. chosen = sh->dev[i].towrite;
  1295. sh->dev[i].towrite = NULL;
  1296. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1297. wake_up(&conf->wait_for_overlap);
  1298. BUG_ON(sh->dev[i].written);
  1299. sh->dev[i].written = chosen;
  1300. }
  1301. break;
  1302. case CHECK_PARITY:
  1303. BUG(); /* Not implemented yet */
  1304. }
  1305. for (i = disks; i--;)
  1306. if (sh->dev[i].written) {
  1307. sector_t sector = sh->dev[i].sector;
  1308. struct bio *wbi = sh->dev[i].written;
  1309. while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
  1310. copy_data(1, wbi, sh->dev[i].page, sector);
  1311. wbi = r5_next_bio(wbi, sector);
  1312. }
  1313. set_bit(R5_LOCKED, &sh->dev[i].flags);
  1314. set_bit(R5_UPTODATE, &sh->dev[i].flags);
  1315. }
  1316. // switch(method) {
  1317. // case RECONSTRUCT_WRITE:
  1318. // case CHECK_PARITY:
  1319. // case UPDATE_PARITY:
  1320. /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
  1321. /* FIX: Is this ordering of drives even remotely optimal? */
  1322. count = 0;
  1323. i = d0_idx;
  1324. do {
  1325. ptrs[count++] = page_address(sh->dev[i].page);
  1326. if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1327. printk("block %d/%d not uptodate on parity calc\n", i,count);
  1328. i = raid6_next_disk(i, disks);
  1329. } while ( i != d0_idx );
  1330. // break;
  1331. // }
  1332. raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
  1333. switch(method) {
  1334. case RECONSTRUCT_WRITE:
  1335. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1336. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1337. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1338. set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
  1339. break;
  1340. case UPDATE_PARITY:
  1341. set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1342. set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
  1343. break;
  1344. }
  1345. }
  1346. /* Compute one missing block */
  1347. static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
  1348. {
  1349. int i, count, disks = sh->disks;
  1350. void *ptr[MAX_XOR_BLOCKS], *dest, *p;
  1351. int pd_idx = sh->pd_idx;
  1352. int qd_idx = raid6_next_disk(pd_idx, disks);
  1353. pr_debug("compute_block_1, stripe %llu, idx %d\n",
  1354. (unsigned long long)sh->sector, dd_idx);
  1355. if ( dd_idx == qd_idx ) {
  1356. /* We're actually computing the Q drive */
  1357. compute_parity6(sh, UPDATE_PARITY);
  1358. } else {
  1359. dest = page_address(sh->dev[dd_idx].page);
  1360. if (!nozero) memset(dest, 0, STRIPE_SIZE);
  1361. count = 0;
  1362. for (i = disks ; i--; ) {
  1363. if (i == dd_idx || i == qd_idx)
  1364. continue;
  1365. p = page_address(sh->dev[i].page);
  1366. if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1367. ptr[count++] = p;
  1368. else
  1369. printk("compute_block() %d, stripe %llu, %d"
  1370. " not present\n", dd_idx,
  1371. (unsigned long long)sh->sector, i);
  1372. check_xor();
  1373. }
  1374. if (count)
  1375. xor_blocks(count, STRIPE_SIZE, dest, ptr);
  1376. if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1377. else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
  1378. }
  1379. }
  1380. /* Compute two missing blocks */
  1381. static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
  1382. {
  1383. int i, count, disks = sh->disks;
  1384. int pd_idx = sh->pd_idx;
  1385. int qd_idx = raid6_next_disk(pd_idx, disks);
  1386. int d0_idx = raid6_next_disk(qd_idx, disks);
  1387. int faila, failb;
  1388. /* faila and failb are disk numbers relative to d0_idx */
  1389. /* pd_idx become disks-2 and qd_idx become disks-1 */
  1390. faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
  1391. failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
  1392. BUG_ON(faila == failb);
  1393. if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
  1394. pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
  1395. (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
  1396. if ( failb == disks-1 ) {
  1397. /* Q disk is one of the missing disks */
  1398. if ( faila == disks-2 ) {
  1399. /* Missing P+Q, just recompute */
  1400. compute_parity6(sh, UPDATE_PARITY);
  1401. return;
  1402. } else {
  1403. /* We're missing D+Q; recompute D from P */
  1404. compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
  1405. compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
  1406. return;
  1407. }
  1408. }
  1409. /* We're missing D+P or D+D; build pointer table */
  1410. {
  1411. /**** FIX THIS: This could be very bad if disks is close to 256 ****/
  1412. void *ptrs[disks];
  1413. count = 0;
  1414. i = d0_idx;
  1415. do {
  1416. ptrs[count++] = page_address(sh->dev[i].page);
  1417. i = raid6_next_disk(i, disks);
  1418. if (i != dd_idx1 && i != dd_idx2 &&
  1419. !test_bit(R5_UPTODATE, &sh->dev[i].flags))
  1420. printk("compute_2 with missing block %d/%d\n", count, i);
  1421. } while ( i != d0_idx );
  1422. if ( failb == disks-2 ) {
  1423. /* We're missing D+P. */
  1424. raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
  1425. } else {
  1426. /* We're missing D+D. */
  1427. raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
  1428. }
  1429. /* Both the above update both missing blocks */
  1430. set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
  1431. set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
  1432. }
  1433. }
  1434. static void
  1435. schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
  1436. int rcw, int expand)
  1437. {
  1438. int i, pd_idx = sh->pd_idx, disks = sh->disks;
  1439. if (rcw) {
  1440. /* if we are not expanding this is a proper write request, and
  1441. * there will be bios with new data to be drained into the
  1442. * stripe cache
  1443. */
  1444. if (!expand) {
  1445. sh->reconstruct_state = reconstruct_state_drain_run;
  1446. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1447. } else
  1448. sh->reconstruct_state = reconstruct_state_run;
  1449. set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
  1450. for (i = disks; i--; ) {
  1451. struct r5dev *dev = &sh->dev[i];
  1452. if (dev->towrite) {
  1453. set_bit(R5_LOCKED, &dev->flags);
  1454. set_bit(R5_Wantdrain, &dev->flags);
  1455. if (!expand)
  1456. clear_bit(R5_UPTODATE, &dev->flags);
  1457. s->locked++;
  1458. }
  1459. }
  1460. if (s->locked + 1 == disks)
  1461. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  1462. atomic_inc(&sh->raid_conf->pending_full_writes);
  1463. } else {
  1464. BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
  1465. test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
  1466. sh->reconstruct_state = reconstruct_state_prexor_drain_run;
  1467. set_bit(STRIPE_OP_PREXOR, &s->ops_request);
  1468. set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
  1469. set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
  1470. for (i = disks; i--; ) {
  1471. struct r5dev *dev = &sh->dev[i];
  1472. if (i == pd_idx)
  1473. continue;
  1474. if (dev->towrite &&
  1475. (test_bit(R5_UPTODATE, &dev->flags) ||
  1476. test_bit(R5_Wantcompute, &dev->flags))) {
  1477. set_bit(R5_Wantdrain, &dev->flags);
  1478. set_bit(R5_LOCKED, &dev->flags);
  1479. clear_bit(R5_UPTODATE, &dev->flags);
  1480. s->locked++;
  1481. }
  1482. }
  1483. }
  1484. /* keep the parity disk locked while asynchronous operations
  1485. * are in flight
  1486. */
  1487. set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
  1488. clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
  1489. s->locked++;
  1490. pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
  1491. __func__, (unsigned long long)sh->sector,
  1492. s->locked, s->ops_request);
  1493. }
  1494. /*
  1495. * Each stripe/dev can have one or more bion attached.
  1496. * toread/towrite point to the first in a chain.
  1497. * The bi_next chain must be in order.
  1498. */
  1499. static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
  1500. {
  1501. struct bio **bip;
  1502. raid5_conf_t *conf = sh->raid_conf;
  1503. int firstwrite=0;
  1504. pr_debug("adding bh b#%llu to stripe s#%llu\n",
  1505. (unsigned long long)bi->bi_sector,
  1506. (unsigned long long)sh->sector);
  1507. spin_lock(&sh->lock);
  1508. spin_lock_irq(&conf->device_lock);
  1509. if (forwrite) {
  1510. bip = &sh->dev[dd_idx].towrite;
  1511. if (*bip == NULL && sh->dev[dd_idx].written == NULL)
  1512. firstwrite = 1;
  1513. } else
  1514. bip = &sh->dev[dd_idx].toread;
  1515. while (*bip && (*bip)->bi_sector < bi->bi_sector) {
  1516. if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
  1517. goto overlap;
  1518. bip = & (*bip)->bi_next;
  1519. }
  1520. if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
  1521. goto overlap;
  1522. BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
  1523. if (*bip)
  1524. bi->bi_next = *bip;
  1525. *bip = bi;
  1526. bi->bi_phys_segments++;
  1527. spin_unlock_irq(&conf->device_lock);
  1528. spin_unlock(&sh->lock);
  1529. pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
  1530. (unsigned long long)bi->bi_sector,
  1531. (unsigned long long)sh->sector, dd_idx);
  1532. if (conf->mddev->bitmap && firstwrite) {
  1533. bitmap_startwrite(conf->mddev->bitmap, sh->sector,
  1534. STRIPE_SECTORS, 0);
  1535. sh->bm_seq = conf->seq_flush+1;
  1536. set_bit(STRIPE_BIT_DELAY, &sh->state);
  1537. }
  1538. if (forwrite) {
  1539. /* check if page is covered */
  1540. sector_t sector = sh->dev[dd_idx].sector;
  1541. for (bi=sh->dev[dd_idx].towrite;
  1542. sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
  1543. bi && bi->bi_sector <= sector;
  1544. bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
  1545. if (bi->bi_sector + (bi->bi_size>>9) >= sector)
  1546. sector = bi->bi_sector + (bi->bi_size>>9);
  1547. }
  1548. if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
  1549. set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
  1550. }
  1551. return 1;
  1552. overlap:
  1553. set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
  1554. spin_unlock_irq(&conf->device_lock);
  1555. spin_unlock(&sh->lock);
  1556. return 0;
  1557. }
  1558. static void end_reshape(raid5_conf_t *conf);
  1559. static int page_is_zero(struct page *p)
  1560. {
  1561. char *a = page_address(p);
  1562. return ((*(u32*)a) == 0 &&
  1563. memcmp(a, a+4, STRIPE_SIZE-4)==0);
  1564. }
  1565. static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
  1566. {
  1567. int sectors_per_chunk = conf->chunk_size >> 9;
  1568. int pd_idx, dd_idx;
  1569. int chunk_offset = sector_div(stripe, sectors_per_chunk);
  1570. raid5_compute_sector(stripe * (disks - conf->max_degraded)
  1571. *sectors_per_chunk + chunk_offset,
  1572. disks, disks - conf->max_degraded,
  1573. &dd_idx, &pd_idx, conf);
  1574. return pd_idx;
  1575. }
  1576. static void
  1577. handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
  1578. struct stripe_head_state *s, int disks,
  1579. struct bio **return_bi)
  1580. {
  1581. int i;
  1582. for (i = disks; i--; ) {
  1583. struct bio *bi;
  1584. int bitmap_end = 0;
  1585. if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
  1586. mdk_rdev_t *rdev;
  1587. rcu_read_lock();
  1588. rdev = rcu_dereference(conf->disks[i].rdev);
  1589. if (rdev && test_bit(In_sync, &rdev->flags))
  1590. /* multiple read failures in one stripe */
  1591. md_error(conf->mddev, rdev);
  1592. rcu_read_unlock();
  1593. }
  1594. spin_lock_irq(&conf->device_lock);
  1595. /* fail all writes first */
  1596. bi = sh->dev[i].towrite;
  1597. sh->dev[i].towrite = NULL;
  1598. if (bi) {
  1599. s->to_write--;
  1600. bitmap_end = 1;
  1601. }
  1602. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1603. wake_up(&conf->wait_for_overlap);
  1604. while (bi && bi->bi_sector <
  1605. sh->dev[i].sector + STRIPE_SECTORS) {
  1606. struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
  1607. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1608. if (!raid5_dec_bi_phys_segments(bi)) {
  1609. md_write_end(conf->mddev);
  1610. bi->bi_next = *return_bi;
  1611. *return_bi = bi;
  1612. }
  1613. bi = nextbi;
  1614. }
  1615. /* and fail all 'written' */
  1616. bi = sh->dev[i].written;
  1617. sh->dev[i].written = NULL;
  1618. if (bi) bitmap_end = 1;
  1619. while (bi && bi->bi_sector <
  1620. sh->dev[i].sector + STRIPE_SECTORS) {
  1621. struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
  1622. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1623. if (!raid5_dec_bi_phys_segments(bi)) {
  1624. md_write_end(conf->mddev);
  1625. bi->bi_next = *return_bi;
  1626. *return_bi = bi;
  1627. }
  1628. bi = bi2;
  1629. }
  1630. /* fail any reads if this device is non-operational and
  1631. * the data has not reached the cache yet.
  1632. */
  1633. if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
  1634. (!test_bit(R5_Insync, &sh->dev[i].flags) ||
  1635. test_bit(R5_ReadError, &sh->dev[i].flags))) {
  1636. bi = sh->dev[i].toread;
  1637. sh->dev[i].toread = NULL;
  1638. if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
  1639. wake_up(&conf->wait_for_overlap);
  1640. if (bi) s->to_read--;
  1641. while (bi && bi->bi_sector <
  1642. sh->dev[i].sector + STRIPE_SECTORS) {
  1643. struct bio *nextbi =
  1644. r5_next_bio(bi, sh->dev[i].sector);
  1645. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  1646. if (!raid5_dec_bi_phys_segments(bi)) {
  1647. bi->bi_next = *return_bi;
  1648. *return_bi = bi;
  1649. }
  1650. bi = nextbi;
  1651. }
  1652. }
  1653. spin_unlock_irq(&conf->device_lock);
  1654. if (bitmap_end)
  1655. bitmap_endwrite(conf->mddev->bitmap, sh->sector,
  1656. STRIPE_SECTORS, 0, 0);
  1657. }
  1658. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  1659. if (atomic_dec_and_test(&conf->pending_full_writes))
  1660. md_wakeup_thread(conf->mddev->thread);
  1661. }
  1662. /* fetch_block5 - checks the given member device to see if its data needs
  1663. * to be read or computed to satisfy a request.
  1664. *
  1665. * Returns 1 when no more member devices need to be checked, otherwise returns
  1666. * 0 to tell the loop in handle_stripe_fill5 to continue
  1667. */
  1668. static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
  1669. int disk_idx, int disks)
  1670. {
  1671. struct r5dev *dev = &sh->dev[disk_idx];
  1672. struct r5dev *failed_dev = &sh->dev[s->failed_num];
  1673. /* is the data in this block needed, and can we get it? */
  1674. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1675. !test_bit(R5_UPTODATE, &dev->flags) &&
  1676. (dev->toread ||
  1677. (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1678. s->syncing || s->expanding ||
  1679. (s->failed &&
  1680. (failed_dev->toread ||
  1681. (failed_dev->towrite &&
  1682. !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
  1683. /* We would like to get this block, possibly by computing it,
  1684. * otherwise read it if the backing disk is insync
  1685. */
  1686. if ((s->uptodate == disks - 1) &&
  1687. (s->failed && disk_idx == s->failed_num)) {
  1688. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  1689. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  1690. set_bit(R5_Wantcompute, &dev->flags);
  1691. sh->ops.target = disk_idx;
  1692. s->req_compute = 1;
  1693. /* Careful: from this point on 'uptodate' is in the eye
  1694. * of raid5_run_ops which services 'compute' operations
  1695. * before writes. R5_Wantcompute flags a block that will
  1696. * be R5_UPTODATE by the time it is needed for a
  1697. * subsequent operation.
  1698. */
  1699. s->uptodate++;
  1700. return 1; /* uptodate + compute == disks */
  1701. } else if (test_bit(R5_Insync, &dev->flags)) {
  1702. set_bit(R5_LOCKED, &dev->flags);
  1703. set_bit(R5_Wantread, &dev->flags);
  1704. s->locked++;
  1705. pr_debug("Reading block %d (sync=%d)\n", disk_idx,
  1706. s->syncing);
  1707. }
  1708. }
  1709. return 0;
  1710. }
  1711. /**
  1712. * handle_stripe_fill5 - read or compute data to satisfy pending requests.
  1713. */
  1714. static void handle_stripe_fill5(struct stripe_head *sh,
  1715. struct stripe_head_state *s, int disks)
  1716. {
  1717. int i;
  1718. /* look for blocks to read/compute, skip this if a compute
  1719. * is already in flight, or if the stripe contents are in the
  1720. * midst of changing due to a write
  1721. */
  1722. if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
  1723. !sh->reconstruct_state)
  1724. for (i = disks; i--; )
  1725. if (fetch_block5(sh, s, i, disks))
  1726. break;
  1727. set_bit(STRIPE_HANDLE, &sh->state);
  1728. }
  1729. static void handle_stripe_fill6(struct stripe_head *sh,
  1730. struct stripe_head_state *s, struct r6_state *r6s,
  1731. int disks)
  1732. {
  1733. int i;
  1734. for (i = disks; i--; ) {
  1735. struct r5dev *dev = &sh->dev[i];
  1736. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1737. !test_bit(R5_UPTODATE, &dev->flags) &&
  1738. (dev->toread || (dev->towrite &&
  1739. !test_bit(R5_OVERWRITE, &dev->flags)) ||
  1740. s->syncing || s->expanding ||
  1741. (s->failed >= 1 &&
  1742. (sh->dev[r6s->failed_num[0]].toread ||
  1743. s->to_write)) ||
  1744. (s->failed >= 2 &&
  1745. (sh->dev[r6s->failed_num[1]].toread ||
  1746. s->to_write)))) {
  1747. /* we would like to get this block, possibly
  1748. * by computing it, but we might not be able to
  1749. */
  1750. if ((s->uptodate == disks - 1) &&
  1751. (s->failed && (i == r6s->failed_num[0] ||
  1752. i == r6s->failed_num[1]))) {
  1753. pr_debug("Computing stripe %llu block %d\n",
  1754. (unsigned long long)sh->sector, i);
  1755. compute_block_1(sh, i, 0);
  1756. s->uptodate++;
  1757. } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
  1758. /* Computing 2-failure is *very* expensive; only
  1759. * do it if failed >= 2
  1760. */
  1761. int other;
  1762. for (other = disks; other--; ) {
  1763. if (other == i)
  1764. continue;
  1765. if (!test_bit(R5_UPTODATE,
  1766. &sh->dev[other].flags))
  1767. break;
  1768. }
  1769. BUG_ON(other < 0);
  1770. pr_debug("Computing stripe %llu blocks %d,%d\n",
  1771. (unsigned long long)sh->sector,
  1772. i, other);
  1773. compute_block_2(sh, i, other);
  1774. s->uptodate += 2;
  1775. } else if (test_bit(R5_Insync, &dev->flags)) {
  1776. set_bit(R5_LOCKED, &dev->flags);
  1777. set_bit(R5_Wantread, &dev->flags);
  1778. s->locked++;
  1779. pr_debug("Reading block %d (sync=%d)\n",
  1780. i, s->syncing);
  1781. }
  1782. }
  1783. }
  1784. set_bit(STRIPE_HANDLE, &sh->state);
  1785. }
  1786. /* handle_stripe_clean_event
  1787. * any written block on an uptodate or failed drive can be returned.
  1788. * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
  1789. * never LOCKED, so we don't need to test 'failed' directly.
  1790. */
  1791. static void handle_stripe_clean_event(raid5_conf_t *conf,
  1792. struct stripe_head *sh, int disks, struct bio **return_bi)
  1793. {
  1794. int i;
  1795. struct r5dev *dev;
  1796. for (i = disks; i--; )
  1797. if (sh->dev[i].written) {
  1798. dev = &sh->dev[i];
  1799. if (!test_bit(R5_LOCKED, &dev->flags) &&
  1800. test_bit(R5_UPTODATE, &dev->flags)) {
  1801. /* We can return any write requests */
  1802. struct bio *wbi, *wbi2;
  1803. int bitmap_end = 0;
  1804. pr_debug("Return write for disc %d\n", i);
  1805. spin_lock_irq(&conf->device_lock);
  1806. wbi = dev->written;
  1807. dev->written = NULL;
  1808. while (wbi && wbi->bi_sector <
  1809. dev->sector + STRIPE_SECTORS) {
  1810. wbi2 = r5_next_bio(wbi, dev->sector);
  1811. if (!raid5_dec_bi_phys_segments(wbi)) {
  1812. md_write_end(conf->mddev);
  1813. wbi->bi_next = *return_bi;
  1814. *return_bi = wbi;
  1815. }
  1816. wbi = wbi2;
  1817. }
  1818. if (dev->towrite == NULL)
  1819. bitmap_end = 1;
  1820. spin_unlock_irq(&conf->device_lock);
  1821. if (bitmap_end)
  1822. bitmap_endwrite(conf->mddev->bitmap,
  1823. sh->sector,
  1824. STRIPE_SECTORS,
  1825. !test_bit(STRIPE_DEGRADED, &sh->state),
  1826. 0);
  1827. }
  1828. }
  1829. if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
  1830. if (atomic_dec_and_test(&conf->pending_full_writes))
  1831. md_wakeup_thread(conf->mddev->thread);
  1832. }
  1833. static void handle_stripe_dirtying5(raid5_conf_t *conf,
  1834. struct stripe_head *sh, struct stripe_head_state *s, int disks)
  1835. {
  1836. int rmw = 0, rcw = 0, i;
  1837. for (i = disks; i--; ) {
  1838. /* would I have to read this buffer for read_modify_write */
  1839. struct r5dev *dev = &sh->dev[i];
  1840. if ((dev->towrite || i == sh->pd_idx) &&
  1841. !test_bit(R5_LOCKED, &dev->flags) &&
  1842. !(test_bit(R5_UPTODATE, &dev->flags) ||
  1843. test_bit(R5_Wantcompute, &dev->flags))) {
  1844. if (test_bit(R5_Insync, &dev->flags))
  1845. rmw++;
  1846. else
  1847. rmw += 2*disks; /* cannot read it */
  1848. }
  1849. /* Would I have to read this buffer for reconstruct_write */
  1850. if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
  1851. !test_bit(R5_LOCKED, &dev->flags) &&
  1852. !(test_bit(R5_UPTODATE, &dev->flags) ||
  1853. test_bit(R5_Wantcompute, &dev->flags))) {
  1854. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1855. else
  1856. rcw += 2*disks;
  1857. }
  1858. }
  1859. pr_debug("for sector %llu, rmw=%d rcw=%d\n",
  1860. (unsigned long long)sh->sector, rmw, rcw);
  1861. set_bit(STRIPE_HANDLE, &sh->state);
  1862. if (rmw < rcw && rmw > 0)
  1863. /* prefer read-modify-write, but need to get some data */
  1864. for (i = disks; i--; ) {
  1865. struct r5dev *dev = &sh->dev[i];
  1866. if ((dev->towrite || i == sh->pd_idx) &&
  1867. !test_bit(R5_LOCKED, &dev->flags) &&
  1868. !(test_bit(R5_UPTODATE, &dev->flags) ||
  1869. test_bit(R5_Wantcompute, &dev->flags)) &&
  1870. test_bit(R5_Insync, &dev->flags)) {
  1871. if (
  1872. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1873. pr_debug("Read_old block "
  1874. "%d for r-m-w\n", i);
  1875. set_bit(R5_LOCKED, &dev->flags);
  1876. set_bit(R5_Wantread, &dev->flags);
  1877. s->locked++;
  1878. } else {
  1879. set_bit(STRIPE_DELAYED, &sh->state);
  1880. set_bit(STRIPE_HANDLE, &sh->state);
  1881. }
  1882. }
  1883. }
  1884. if (rcw <= rmw && rcw > 0)
  1885. /* want reconstruct write, but need to get some data */
  1886. for (i = disks; i--; ) {
  1887. struct r5dev *dev = &sh->dev[i];
  1888. if (!test_bit(R5_OVERWRITE, &dev->flags) &&
  1889. i != sh->pd_idx &&
  1890. !test_bit(R5_LOCKED, &dev->flags) &&
  1891. !(test_bit(R5_UPTODATE, &dev->flags) ||
  1892. test_bit(R5_Wantcompute, &dev->flags)) &&
  1893. test_bit(R5_Insync, &dev->flags)) {
  1894. if (
  1895. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1896. pr_debug("Read_old block "
  1897. "%d for Reconstruct\n", i);
  1898. set_bit(R5_LOCKED, &dev->flags);
  1899. set_bit(R5_Wantread, &dev->flags);
  1900. s->locked++;
  1901. } else {
  1902. set_bit(STRIPE_DELAYED, &sh->state);
  1903. set_bit(STRIPE_HANDLE, &sh->state);
  1904. }
  1905. }
  1906. }
  1907. /* now if nothing is locked, and if we have enough data,
  1908. * we can start a write request
  1909. */
  1910. /* since handle_stripe can be called at any time we need to handle the
  1911. * case where a compute block operation has been submitted and then a
  1912. * subsequent call wants to start a write request. raid5_run_ops only
  1913. * handles the case where compute block and postxor are requested
  1914. * simultaneously. If this is not the case then new writes need to be
  1915. * held off until the compute completes.
  1916. */
  1917. if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
  1918. (s->locked == 0 && (rcw == 0 || rmw == 0) &&
  1919. !test_bit(STRIPE_BIT_DELAY, &sh->state)))
  1920. schedule_reconstruction5(sh, s, rcw == 0, 0);
  1921. }
  1922. static void handle_stripe_dirtying6(raid5_conf_t *conf,
  1923. struct stripe_head *sh, struct stripe_head_state *s,
  1924. struct r6_state *r6s, int disks)
  1925. {
  1926. int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
  1927. int qd_idx = r6s->qd_idx;
  1928. for (i = disks; i--; ) {
  1929. struct r5dev *dev = &sh->dev[i];
  1930. /* Would I have to read this buffer for reconstruct_write */
  1931. if (!test_bit(R5_OVERWRITE, &dev->flags)
  1932. && i != pd_idx && i != qd_idx
  1933. && (!test_bit(R5_LOCKED, &dev->flags)
  1934. ) &&
  1935. !test_bit(R5_UPTODATE, &dev->flags)) {
  1936. if (test_bit(R5_Insync, &dev->flags)) rcw++;
  1937. else {
  1938. pr_debug("raid6: must_compute: "
  1939. "disk %d flags=%#lx\n", i, dev->flags);
  1940. must_compute++;
  1941. }
  1942. }
  1943. }
  1944. pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
  1945. (unsigned long long)sh->sector, rcw, must_compute);
  1946. set_bit(STRIPE_HANDLE, &sh->state);
  1947. if (rcw > 0)
  1948. /* want reconstruct write, but need to get some data */
  1949. for (i = disks; i--; ) {
  1950. struct r5dev *dev = &sh->dev[i];
  1951. if (!test_bit(R5_OVERWRITE, &dev->flags)
  1952. && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
  1953. && !test_bit(R5_LOCKED, &dev->flags) &&
  1954. !test_bit(R5_UPTODATE, &dev->flags) &&
  1955. test_bit(R5_Insync, &dev->flags)) {
  1956. if (
  1957. test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  1958. pr_debug("Read_old stripe %llu "
  1959. "block %d for Reconstruct\n",
  1960. (unsigned long long)sh->sector, i);
  1961. set_bit(R5_LOCKED, &dev->flags);
  1962. set_bit(R5_Wantread, &dev->flags);
  1963. s->locked++;
  1964. } else {
  1965. pr_debug("Request delayed stripe %llu "
  1966. "block %d for Reconstruct\n",
  1967. (unsigned long long)sh->sector, i);
  1968. set_bit(STRIPE_DELAYED, &sh->state);
  1969. set_bit(STRIPE_HANDLE, &sh->state);
  1970. }
  1971. }
  1972. }
  1973. /* now if nothing is locked, and if we have enough data, we can start a
  1974. * write request
  1975. */
  1976. if (s->locked == 0 && rcw == 0 &&
  1977. !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
  1978. if (must_compute > 0) {
  1979. /* We have failed blocks and need to compute them */
  1980. switch (s->failed) {
  1981. case 0:
  1982. BUG();
  1983. case 1:
  1984. compute_block_1(sh, r6s->failed_num[0], 0);
  1985. break;
  1986. case 2:
  1987. compute_block_2(sh, r6s->failed_num[0],
  1988. r6s->failed_num[1]);
  1989. break;
  1990. default: /* This request should have been failed? */
  1991. BUG();
  1992. }
  1993. }
  1994. pr_debug("Computing parity for stripe %llu\n",
  1995. (unsigned long long)sh->sector);
  1996. compute_parity6(sh, RECONSTRUCT_WRITE);
  1997. /* now every locked buffer is ready to be written */
  1998. for (i = disks; i--; )
  1999. if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
  2000. pr_debug("Writing stripe %llu block %d\n",
  2001. (unsigned long long)sh->sector, i);
  2002. s->locked++;
  2003. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2004. }
  2005. if (s->locked == disks)
  2006. if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
  2007. atomic_inc(&conf->pending_full_writes);
  2008. /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
  2009. set_bit(STRIPE_INSYNC, &sh->state);
  2010. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2011. atomic_dec(&conf->preread_active_stripes);
  2012. if (atomic_read(&conf->preread_active_stripes) <
  2013. IO_THRESHOLD)
  2014. md_wakeup_thread(conf->mddev->thread);
  2015. }
  2016. }
  2017. }
  2018. static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
  2019. struct stripe_head_state *s, int disks)
  2020. {
  2021. struct r5dev *dev = NULL;
  2022. set_bit(STRIPE_HANDLE, &sh->state);
  2023. switch (sh->check_state) {
  2024. case check_state_idle:
  2025. /* start a new check operation if there are no failures */
  2026. if (s->failed == 0) {
  2027. BUG_ON(s->uptodate != disks);
  2028. sh->check_state = check_state_run;
  2029. set_bit(STRIPE_OP_CHECK, &s->ops_request);
  2030. clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
  2031. s->uptodate--;
  2032. break;
  2033. }
  2034. dev = &sh->dev[s->failed_num];
  2035. /* fall through */
  2036. case check_state_compute_result:
  2037. sh->check_state = check_state_idle;
  2038. if (!dev)
  2039. dev = &sh->dev[sh->pd_idx];
  2040. /* check that a write has not made the stripe insync */
  2041. if (test_bit(STRIPE_INSYNC, &sh->state))
  2042. break;
  2043. /* either failed parity check, or recovery is happening */
  2044. BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
  2045. BUG_ON(s->uptodate != disks);
  2046. set_bit(R5_LOCKED, &dev->flags);
  2047. s->locked++;
  2048. set_bit(R5_Wantwrite, &dev->flags);
  2049. clear_bit(STRIPE_DEGRADED, &sh->state);
  2050. set_bit(STRIPE_INSYNC, &sh->state);
  2051. break;
  2052. case check_state_run:
  2053. break; /* we will be called again upon completion */
  2054. case check_state_check_result:
  2055. sh->check_state = check_state_idle;
  2056. /* if a failure occurred during the check operation, leave
  2057. * STRIPE_INSYNC not set and let the stripe be handled again
  2058. */
  2059. if (s->failed)
  2060. break;
  2061. /* handle a successful check operation, if parity is correct
  2062. * we are done. Otherwise update the mismatch count and repair
  2063. * parity if !MD_RECOVERY_CHECK
  2064. */
  2065. if (sh->ops.zero_sum_result == 0)
  2066. /* parity is correct (on disc,
  2067. * not in buffer any more)
  2068. */
  2069. set_bit(STRIPE_INSYNC, &sh->state);
  2070. else {
  2071. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2072. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2073. /* don't try to repair!! */
  2074. set_bit(STRIPE_INSYNC, &sh->state);
  2075. else {
  2076. sh->check_state = check_state_compute_run;
  2077. set_bit(STRIPE_COMPUTE_RUN, &sh->state);
  2078. set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
  2079. set_bit(R5_Wantcompute,
  2080. &sh->dev[sh->pd_idx].flags);
  2081. sh->ops.target = sh->pd_idx;
  2082. s->uptodate++;
  2083. }
  2084. }
  2085. break;
  2086. case check_state_compute_run:
  2087. break;
  2088. default:
  2089. printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
  2090. __func__, sh->check_state,
  2091. (unsigned long long) sh->sector);
  2092. BUG();
  2093. }
  2094. }
  2095. static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
  2096. struct stripe_head_state *s,
  2097. struct r6_state *r6s, struct page *tmp_page,
  2098. int disks)
  2099. {
  2100. int update_p = 0, update_q = 0;
  2101. struct r5dev *dev;
  2102. int pd_idx = sh->pd_idx;
  2103. int qd_idx = r6s->qd_idx;
  2104. set_bit(STRIPE_HANDLE, &sh->state);
  2105. BUG_ON(s->failed > 2);
  2106. BUG_ON(s->uptodate < disks);
  2107. /* Want to check and possibly repair P and Q.
  2108. * However there could be one 'failed' device, in which
  2109. * case we can only check one of them, possibly using the
  2110. * other to generate missing data
  2111. */
  2112. /* If !tmp_page, we cannot do the calculations,
  2113. * but as we have set STRIPE_HANDLE, we will soon be called
  2114. * by stripe_handle with a tmp_page - just wait until then.
  2115. */
  2116. if (tmp_page) {
  2117. if (s->failed == r6s->q_failed) {
  2118. /* The only possible failed device holds 'Q', so it
  2119. * makes sense to check P (If anything else were failed,
  2120. * we would have used P to recreate it).
  2121. */
  2122. compute_block_1(sh, pd_idx, 1);
  2123. if (!page_is_zero(sh->dev[pd_idx].page)) {
  2124. compute_block_1(sh, pd_idx, 0);
  2125. update_p = 1;
  2126. }
  2127. }
  2128. if (!r6s->q_failed && s->failed < 2) {
  2129. /* q is not failed, and we didn't use it to generate
  2130. * anything, so it makes sense to check it
  2131. */
  2132. memcpy(page_address(tmp_page),
  2133. page_address(sh->dev[qd_idx].page),
  2134. STRIPE_SIZE);
  2135. compute_parity6(sh, UPDATE_PARITY);
  2136. if (memcmp(page_address(tmp_page),
  2137. page_address(sh->dev[qd_idx].page),
  2138. STRIPE_SIZE) != 0) {
  2139. clear_bit(STRIPE_INSYNC, &sh->state);
  2140. update_q = 1;
  2141. }
  2142. }
  2143. if (update_p || update_q) {
  2144. conf->mddev->resync_mismatches += STRIPE_SECTORS;
  2145. if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
  2146. /* don't try to repair!! */
  2147. update_p = update_q = 0;
  2148. }
  2149. /* now write out any block on a failed drive,
  2150. * or P or Q if they need it
  2151. */
  2152. if (s->failed == 2) {
  2153. dev = &sh->dev[r6s->failed_num[1]];
  2154. s->locked++;
  2155. set_bit(R5_LOCKED, &dev->flags);
  2156. set_bit(R5_Wantwrite, &dev->flags);
  2157. }
  2158. if (s->failed >= 1) {
  2159. dev = &sh->dev[r6s->failed_num[0]];
  2160. s->locked++;
  2161. set_bit(R5_LOCKED, &dev->flags);
  2162. set_bit(R5_Wantwrite, &dev->flags);
  2163. }
  2164. if (update_p) {
  2165. dev = &sh->dev[pd_idx];
  2166. s->locked++;
  2167. set_bit(R5_LOCKED, &dev->flags);
  2168. set_bit(R5_Wantwrite, &dev->flags);
  2169. }
  2170. if (update_q) {
  2171. dev = &sh->dev[qd_idx];
  2172. s->locked++;
  2173. set_bit(R5_LOCKED, &dev->flags);
  2174. set_bit(R5_Wantwrite, &dev->flags);
  2175. }
  2176. clear_bit(STRIPE_DEGRADED, &sh->state);
  2177. set_bit(STRIPE_INSYNC, &sh->state);
  2178. }
  2179. }
  2180. static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
  2181. struct r6_state *r6s)
  2182. {
  2183. int i;
  2184. /* We have read all the blocks in this stripe and now we need to
  2185. * copy some of them into a target stripe for expand.
  2186. */
  2187. struct dma_async_tx_descriptor *tx = NULL;
  2188. clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2189. for (i = 0; i < sh->disks; i++)
  2190. if (i != sh->pd_idx && (!r6s || i != r6s->qd_idx)) {
  2191. int dd_idx, pd_idx, j;
  2192. struct stripe_head *sh2;
  2193. sector_t bn = compute_blocknr(sh, i);
  2194. sector_t s = raid5_compute_sector(bn, conf->raid_disks,
  2195. conf->raid_disks -
  2196. conf->max_degraded, &dd_idx,
  2197. &pd_idx, conf);
  2198. sh2 = get_active_stripe(conf, s, conf->raid_disks,
  2199. pd_idx, 1);
  2200. if (sh2 == NULL)
  2201. /* so far only the early blocks of this stripe
  2202. * have been requested. When later blocks
  2203. * get requested, we will try again
  2204. */
  2205. continue;
  2206. if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
  2207. test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
  2208. /* must have already done this block */
  2209. release_stripe(sh2);
  2210. continue;
  2211. }
  2212. /* place all the copies on one channel */
  2213. tx = async_memcpy(sh2->dev[dd_idx].page,
  2214. sh->dev[i].page, 0, 0, STRIPE_SIZE,
  2215. ASYNC_TX_DEP_ACK, tx, NULL, NULL);
  2216. set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
  2217. set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
  2218. for (j = 0; j < conf->raid_disks; j++)
  2219. if (j != sh2->pd_idx &&
  2220. (!r6s || j != raid6_next_disk(sh2->pd_idx,
  2221. sh2->disks)) &&
  2222. !test_bit(R5_Expanded, &sh2->dev[j].flags))
  2223. break;
  2224. if (j == conf->raid_disks) {
  2225. set_bit(STRIPE_EXPAND_READY, &sh2->state);
  2226. set_bit(STRIPE_HANDLE, &sh2->state);
  2227. }
  2228. release_stripe(sh2);
  2229. }
  2230. /* done submitting copies, wait for them to complete */
  2231. if (tx) {
  2232. async_tx_ack(tx);
  2233. dma_wait_for_async_tx(tx);
  2234. }
  2235. }
  2236. /*
  2237. * handle_stripe - do things to a stripe.
  2238. *
  2239. * We lock the stripe and then examine the state of various bits
  2240. * to see what needs to be done.
  2241. * Possible results:
  2242. * return some read request which now have data
  2243. * return some write requests which are safely on disc
  2244. * schedule a read on some buffers
  2245. * schedule a write of some buffers
  2246. * return confirmation of parity correctness
  2247. *
  2248. * buffers are taken off read_list or write_list, and bh_cache buffers
  2249. * get BH_Lock set before the stripe lock is released.
  2250. *
  2251. */
  2252. static bool handle_stripe5(struct stripe_head *sh)
  2253. {
  2254. raid5_conf_t *conf = sh->raid_conf;
  2255. int disks = sh->disks, i;
  2256. struct bio *return_bi = NULL;
  2257. struct stripe_head_state s;
  2258. struct r5dev *dev;
  2259. mdk_rdev_t *blocked_rdev = NULL;
  2260. int prexor;
  2261. memset(&s, 0, sizeof(s));
  2262. pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
  2263. "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
  2264. atomic_read(&sh->count), sh->pd_idx, sh->check_state,
  2265. sh->reconstruct_state);
  2266. spin_lock(&sh->lock);
  2267. clear_bit(STRIPE_HANDLE, &sh->state);
  2268. clear_bit(STRIPE_DELAYED, &sh->state);
  2269. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2270. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2271. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2272. /* Now to look around and see what can be done */
  2273. rcu_read_lock();
  2274. for (i=disks; i--; ) {
  2275. mdk_rdev_t *rdev;
  2276. struct r5dev *dev = &sh->dev[i];
  2277. clear_bit(R5_Insync, &dev->flags);
  2278. pr_debug("check %d: state 0x%lx toread %p read %p write %p "
  2279. "written %p\n", i, dev->flags, dev->toread, dev->read,
  2280. dev->towrite, dev->written);
  2281. /* maybe we can request a biofill operation
  2282. *
  2283. * new wantfill requests are only permitted while
  2284. * ops_complete_biofill is guaranteed to be inactive
  2285. */
  2286. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
  2287. !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
  2288. set_bit(R5_Wantfill, &dev->flags);
  2289. /* now count some things */
  2290. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2291. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2292. if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
  2293. if (test_bit(R5_Wantfill, &dev->flags))
  2294. s.to_fill++;
  2295. else if (dev->toread)
  2296. s.to_read++;
  2297. if (dev->towrite) {
  2298. s.to_write++;
  2299. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2300. s.non_overwrite++;
  2301. }
  2302. if (dev->written)
  2303. s.written++;
  2304. rdev = rcu_dereference(conf->disks[i].rdev);
  2305. if (blocked_rdev == NULL &&
  2306. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2307. blocked_rdev = rdev;
  2308. atomic_inc(&rdev->nr_pending);
  2309. }
  2310. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2311. /* The ReadError flag will just be confusing now */
  2312. clear_bit(R5_ReadError, &dev->flags);
  2313. clear_bit(R5_ReWrite, &dev->flags);
  2314. }
  2315. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2316. || test_bit(R5_ReadError, &dev->flags)) {
  2317. s.failed++;
  2318. s.failed_num = i;
  2319. } else
  2320. set_bit(R5_Insync, &dev->flags);
  2321. }
  2322. rcu_read_unlock();
  2323. if (unlikely(blocked_rdev)) {
  2324. if (s.syncing || s.expanding || s.expanded ||
  2325. s.to_write || s.written) {
  2326. set_bit(STRIPE_HANDLE, &sh->state);
  2327. goto unlock;
  2328. }
  2329. /* There is nothing for the blocked_rdev to block */
  2330. rdev_dec_pending(blocked_rdev, conf->mddev);
  2331. blocked_rdev = NULL;
  2332. }
  2333. if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
  2334. set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
  2335. set_bit(STRIPE_BIOFILL_RUN, &sh->state);
  2336. }
  2337. pr_debug("locked=%d uptodate=%d to_read=%d"
  2338. " to_write=%d failed=%d failed_num=%d\n",
  2339. s.locked, s.uptodate, s.to_read, s.to_write,
  2340. s.failed, s.failed_num);
  2341. /* check if the array has lost two devices and, if so, some requests might
  2342. * need to be failed
  2343. */
  2344. if (s.failed > 1 && s.to_read+s.to_write+s.written)
  2345. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  2346. if (s.failed > 1 && s.syncing) {
  2347. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2348. clear_bit(STRIPE_SYNCING, &sh->state);
  2349. s.syncing = 0;
  2350. }
  2351. /* might be able to return some write requests if the parity block
  2352. * is safe, or on a failed drive
  2353. */
  2354. dev = &sh->dev[sh->pd_idx];
  2355. if ( s.written &&
  2356. ((test_bit(R5_Insync, &dev->flags) &&
  2357. !test_bit(R5_LOCKED, &dev->flags) &&
  2358. test_bit(R5_UPTODATE, &dev->flags)) ||
  2359. (s.failed == 1 && s.failed_num == sh->pd_idx)))
  2360. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  2361. /* Now we might consider reading some blocks, either to check/generate
  2362. * parity, or to satisfy requests
  2363. * or to load a block that is being partially written.
  2364. */
  2365. if (s.to_read || s.non_overwrite ||
  2366. (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
  2367. handle_stripe_fill5(sh, &s, disks);
  2368. /* Now we check to see if any write operations have recently
  2369. * completed
  2370. */
  2371. prexor = 0;
  2372. if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
  2373. prexor = 1;
  2374. if (sh->reconstruct_state == reconstruct_state_drain_result ||
  2375. sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
  2376. sh->reconstruct_state = reconstruct_state_idle;
  2377. /* All the 'written' buffers and the parity block are ready to
  2378. * be written back to disk
  2379. */
  2380. BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
  2381. for (i = disks; i--; ) {
  2382. dev = &sh->dev[i];
  2383. if (test_bit(R5_LOCKED, &dev->flags) &&
  2384. (i == sh->pd_idx || dev->written)) {
  2385. pr_debug("Writing block %d\n", i);
  2386. set_bit(R5_Wantwrite, &dev->flags);
  2387. if (prexor)
  2388. continue;
  2389. if (!test_bit(R5_Insync, &dev->flags) ||
  2390. (i == sh->pd_idx && s.failed == 0))
  2391. set_bit(STRIPE_INSYNC, &sh->state);
  2392. }
  2393. }
  2394. if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
  2395. atomic_dec(&conf->preread_active_stripes);
  2396. if (atomic_read(&conf->preread_active_stripes) <
  2397. IO_THRESHOLD)
  2398. md_wakeup_thread(conf->mddev->thread);
  2399. }
  2400. }
  2401. /* Now to consider new write requests and what else, if anything
  2402. * should be read. We do not handle new writes when:
  2403. * 1/ A 'write' operation (copy+xor) is already in flight.
  2404. * 2/ A 'check' operation is in flight, as it may clobber the parity
  2405. * block.
  2406. */
  2407. if (s.to_write && !sh->reconstruct_state && !sh->check_state)
  2408. handle_stripe_dirtying5(conf, sh, &s, disks);
  2409. /* maybe we need to check and possibly fix the parity for this stripe
  2410. * Any reads will already have been scheduled, so we just see if enough
  2411. * data is available. The parity check is held off while parity
  2412. * dependent operations are in flight.
  2413. */
  2414. if (sh->check_state ||
  2415. (s.syncing && s.locked == 0 &&
  2416. !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
  2417. !test_bit(STRIPE_INSYNC, &sh->state)))
  2418. handle_parity_checks5(conf, sh, &s, disks);
  2419. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2420. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2421. clear_bit(STRIPE_SYNCING, &sh->state);
  2422. }
  2423. /* If the failed drive is just a ReadError, then we might need to progress
  2424. * the repair/check process
  2425. */
  2426. if (s.failed == 1 && !conf->mddev->ro &&
  2427. test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
  2428. && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
  2429. && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
  2430. ) {
  2431. dev = &sh->dev[s.failed_num];
  2432. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2433. set_bit(R5_Wantwrite, &dev->flags);
  2434. set_bit(R5_ReWrite, &dev->flags);
  2435. set_bit(R5_LOCKED, &dev->flags);
  2436. s.locked++;
  2437. } else {
  2438. /* let's read it back */
  2439. set_bit(R5_Wantread, &dev->flags);
  2440. set_bit(R5_LOCKED, &dev->flags);
  2441. s.locked++;
  2442. }
  2443. }
  2444. /* Finish reconstruct operations initiated by the expansion process */
  2445. if (sh->reconstruct_state == reconstruct_state_result) {
  2446. sh->reconstruct_state = reconstruct_state_idle;
  2447. clear_bit(STRIPE_EXPANDING, &sh->state);
  2448. for (i = conf->raid_disks; i--; ) {
  2449. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2450. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2451. s.locked++;
  2452. }
  2453. }
  2454. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
  2455. !sh->reconstruct_state) {
  2456. /* Need to write out all blocks after computing parity */
  2457. sh->disks = conf->raid_disks;
  2458. sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
  2459. conf->raid_disks);
  2460. schedule_reconstruction5(sh, &s, 1, 1);
  2461. } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
  2462. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2463. atomic_dec(&conf->reshape_stripes);
  2464. wake_up(&conf->wait_for_overlap);
  2465. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2466. }
  2467. if (s.expanding && s.locked == 0 &&
  2468. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  2469. handle_stripe_expansion(conf, sh, NULL);
  2470. unlock:
  2471. spin_unlock(&sh->lock);
  2472. /* wait for this device to become unblocked */
  2473. if (unlikely(blocked_rdev))
  2474. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  2475. if (s.ops_request)
  2476. raid5_run_ops(sh, s.ops_request);
  2477. ops_run_io(sh, &s);
  2478. return_io(return_bi);
  2479. return blocked_rdev == NULL;
  2480. }
  2481. static bool handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
  2482. {
  2483. raid6_conf_t *conf = sh->raid_conf;
  2484. int disks = sh->disks;
  2485. struct bio *return_bi = NULL;
  2486. int i, pd_idx = sh->pd_idx;
  2487. struct stripe_head_state s;
  2488. struct r6_state r6s;
  2489. struct r5dev *dev, *pdev, *qdev;
  2490. mdk_rdev_t *blocked_rdev = NULL;
  2491. r6s.qd_idx = raid6_next_disk(pd_idx, disks);
  2492. pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
  2493. "pd_idx=%d, qd_idx=%d\n",
  2494. (unsigned long long)sh->sector, sh->state,
  2495. atomic_read(&sh->count), pd_idx, r6s.qd_idx);
  2496. memset(&s, 0, sizeof(s));
  2497. spin_lock(&sh->lock);
  2498. clear_bit(STRIPE_HANDLE, &sh->state);
  2499. clear_bit(STRIPE_DELAYED, &sh->state);
  2500. s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
  2501. s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  2502. s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
  2503. /* Now to look around and see what can be done */
  2504. rcu_read_lock();
  2505. for (i=disks; i--; ) {
  2506. mdk_rdev_t *rdev;
  2507. dev = &sh->dev[i];
  2508. clear_bit(R5_Insync, &dev->flags);
  2509. pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
  2510. i, dev->flags, dev->toread, dev->towrite, dev->written);
  2511. /* maybe we can reply to a read */
  2512. if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
  2513. struct bio *rbi, *rbi2;
  2514. pr_debug("Return read for disc %d\n", i);
  2515. spin_lock_irq(&conf->device_lock);
  2516. rbi = dev->toread;
  2517. dev->toread = NULL;
  2518. if (test_and_clear_bit(R5_Overlap, &dev->flags))
  2519. wake_up(&conf->wait_for_overlap);
  2520. spin_unlock_irq(&conf->device_lock);
  2521. while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
  2522. copy_data(0, rbi, dev->page, dev->sector);
  2523. rbi2 = r5_next_bio(rbi, dev->sector);
  2524. spin_lock_irq(&conf->device_lock);
  2525. if (!raid5_dec_bi_phys_segments(rbi)) {
  2526. rbi->bi_next = return_bi;
  2527. return_bi = rbi;
  2528. }
  2529. spin_unlock_irq(&conf->device_lock);
  2530. rbi = rbi2;
  2531. }
  2532. }
  2533. /* now count some things */
  2534. if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
  2535. if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
  2536. if (dev->toread)
  2537. s.to_read++;
  2538. if (dev->towrite) {
  2539. s.to_write++;
  2540. if (!test_bit(R5_OVERWRITE, &dev->flags))
  2541. s.non_overwrite++;
  2542. }
  2543. if (dev->written)
  2544. s.written++;
  2545. rdev = rcu_dereference(conf->disks[i].rdev);
  2546. if (blocked_rdev == NULL &&
  2547. rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  2548. blocked_rdev = rdev;
  2549. atomic_inc(&rdev->nr_pending);
  2550. }
  2551. if (!rdev || !test_bit(In_sync, &rdev->flags)) {
  2552. /* The ReadError flag will just be confusing now */
  2553. clear_bit(R5_ReadError, &dev->flags);
  2554. clear_bit(R5_ReWrite, &dev->flags);
  2555. }
  2556. if (!rdev || !test_bit(In_sync, &rdev->flags)
  2557. || test_bit(R5_ReadError, &dev->flags)) {
  2558. if (s.failed < 2)
  2559. r6s.failed_num[s.failed] = i;
  2560. s.failed++;
  2561. } else
  2562. set_bit(R5_Insync, &dev->flags);
  2563. }
  2564. rcu_read_unlock();
  2565. if (unlikely(blocked_rdev)) {
  2566. if (s.syncing || s.expanding || s.expanded ||
  2567. s.to_write || s.written) {
  2568. set_bit(STRIPE_HANDLE, &sh->state);
  2569. goto unlock;
  2570. }
  2571. /* There is nothing for the blocked_rdev to block */
  2572. rdev_dec_pending(blocked_rdev, conf->mddev);
  2573. blocked_rdev = NULL;
  2574. }
  2575. pr_debug("locked=%d uptodate=%d to_read=%d"
  2576. " to_write=%d failed=%d failed_num=%d,%d\n",
  2577. s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
  2578. r6s.failed_num[0], r6s.failed_num[1]);
  2579. /* check if the array has lost >2 devices and, if so, some requests
  2580. * might need to be failed
  2581. */
  2582. if (s.failed > 2 && s.to_read+s.to_write+s.written)
  2583. handle_failed_stripe(conf, sh, &s, disks, &return_bi);
  2584. if (s.failed > 2 && s.syncing) {
  2585. md_done_sync(conf->mddev, STRIPE_SECTORS,0);
  2586. clear_bit(STRIPE_SYNCING, &sh->state);
  2587. s.syncing = 0;
  2588. }
  2589. /*
  2590. * might be able to return some write requests if the parity blocks
  2591. * are safe, or on a failed drive
  2592. */
  2593. pdev = &sh->dev[pd_idx];
  2594. r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
  2595. || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
  2596. qdev = &sh->dev[r6s.qd_idx];
  2597. r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == r6s.qd_idx)
  2598. || (s.failed >= 2 && r6s.failed_num[1] == r6s.qd_idx);
  2599. if ( s.written &&
  2600. ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
  2601. && !test_bit(R5_LOCKED, &pdev->flags)
  2602. && test_bit(R5_UPTODATE, &pdev->flags)))) &&
  2603. ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
  2604. && !test_bit(R5_LOCKED, &qdev->flags)
  2605. && test_bit(R5_UPTODATE, &qdev->flags)))))
  2606. handle_stripe_clean_event(conf, sh, disks, &return_bi);
  2607. /* Now we might consider reading some blocks, either to check/generate
  2608. * parity, or to satisfy requests
  2609. * or to load a block that is being partially written.
  2610. */
  2611. if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
  2612. (s.syncing && (s.uptodate < disks)) || s.expanding)
  2613. handle_stripe_fill6(sh, &s, &r6s, disks);
  2614. /* now to consider writing and what else, if anything should be read */
  2615. if (s.to_write)
  2616. handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
  2617. /* maybe we need to check and possibly fix the parity for this stripe
  2618. * Any reads will already have been scheduled, so we just see if enough
  2619. * data is available
  2620. */
  2621. if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
  2622. handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
  2623. if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
  2624. md_done_sync(conf->mddev, STRIPE_SECTORS,1);
  2625. clear_bit(STRIPE_SYNCING, &sh->state);
  2626. }
  2627. /* If the failed drives are just a ReadError, then we might need
  2628. * to progress the repair/check process
  2629. */
  2630. if (s.failed <= 2 && !conf->mddev->ro)
  2631. for (i = 0; i < s.failed; i++) {
  2632. dev = &sh->dev[r6s.failed_num[i]];
  2633. if (test_bit(R5_ReadError, &dev->flags)
  2634. && !test_bit(R5_LOCKED, &dev->flags)
  2635. && test_bit(R5_UPTODATE, &dev->flags)
  2636. ) {
  2637. if (!test_bit(R5_ReWrite, &dev->flags)) {
  2638. set_bit(R5_Wantwrite, &dev->flags);
  2639. set_bit(R5_ReWrite, &dev->flags);
  2640. set_bit(R5_LOCKED, &dev->flags);
  2641. } else {
  2642. /* let's read it back */
  2643. set_bit(R5_Wantread, &dev->flags);
  2644. set_bit(R5_LOCKED, &dev->flags);
  2645. }
  2646. }
  2647. }
  2648. if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
  2649. /* Need to write out all blocks after computing P&Q */
  2650. sh->disks = conf->raid_disks;
  2651. sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
  2652. conf->raid_disks);
  2653. compute_parity6(sh, RECONSTRUCT_WRITE);
  2654. for (i = conf->raid_disks ; i-- ; ) {
  2655. set_bit(R5_LOCKED, &sh->dev[i].flags);
  2656. s.locked++;
  2657. set_bit(R5_Wantwrite, &sh->dev[i].flags);
  2658. }
  2659. clear_bit(STRIPE_EXPANDING, &sh->state);
  2660. } else if (s.expanded) {
  2661. clear_bit(STRIPE_EXPAND_READY, &sh->state);
  2662. atomic_dec(&conf->reshape_stripes);
  2663. wake_up(&conf->wait_for_overlap);
  2664. md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
  2665. }
  2666. if (s.expanding && s.locked == 0 &&
  2667. !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
  2668. handle_stripe_expansion(conf, sh, &r6s);
  2669. unlock:
  2670. spin_unlock(&sh->lock);
  2671. /* wait for this device to become unblocked */
  2672. if (unlikely(blocked_rdev))
  2673. md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
  2674. ops_run_io(sh, &s);
  2675. return_io(return_bi);
  2676. return blocked_rdev == NULL;
  2677. }
  2678. /* returns true if the stripe was handled */
  2679. static bool handle_stripe(struct stripe_head *sh, struct page *tmp_page)
  2680. {
  2681. if (sh->raid_conf->level == 6)
  2682. return handle_stripe6(sh, tmp_page);
  2683. else
  2684. return handle_stripe5(sh);
  2685. }
  2686. static void raid5_activate_delayed(raid5_conf_t *conf)
  2687. {
  2688. if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
  2689. while (!list_empty(&conf->delayed_list)) {
  2690. struct list_head *l = conf->delayed_list.next;
  2691. struct stripe_head *sh;
  2692. sh = list_entry(l, struct stripe_head, lru);
  2693. list_del_init(l);
  2694. clear_bit(STRIPE_DELAYED, &sh->state);
  2695. if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
  2696. atomic_inc(&conf->preread_active_stripes);
  2697. list_add_tail(&sh->lru, &conf->hold_list);
  2698. }
  2699. } else
  2700. blk_plug_device(conf->mddev->queue);
  2701. }
  2702. static void activate_bit_delay(raid5_conf_t *conf)
  2703. {
  2704. /* device_lock is held */
  2705. struct list_head head;
  2706. list_add(&head, &conf->bitmap_list);
  2707. list_del_init(&conf->bitmap_list);
  2708. while (!list_empty(&head)) {
  2709. struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
  2710. list_del_init(&sh->lru);
  2711. atomic_inc(&sh->count);
  2712. __release_stripe(conf, sh);
  2713. }
  2714. }
  2715. static void unplug_slaves(mddev_t *mddev)
  2716. {
  2717. raid5_conf_t *conf = mddev_to_conf(mddev);
  2718. int i;
  2719. rcu_read_lock();
  2720. for (i=0; i<mddev->raid_disks; i++) {
  2721. mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
  2722. if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
  2723. struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
  2724. atomic_inc(&rdev->nr_pending);
  2725. rcu_read_unlock();
  2726. blk_unplug(r_queue);
  2727. rdev_dec_pending(rdev, mddev);
  2728. rcu_read_lock();
  2729. }
  2730. }
  2731. rcu_read_unlock();
  2732. }
  2733. static void raid5_unplug_device(struct request_queue *q)
  2734. {
  2735. mddev_t *mddev = q->queuedata;
  2736. raid5_conf_t *conf = mddev_to_conf(mddev);
  2737. unsigned long flags;
  2738. spin_lock_irqsave(&conf->device_lock, flags);
  2739. if (blk_remove_plug(q)) {
  2740. conf->seq_flush++;
  2741. raid5_activate_delayed(conf);
  2742. }
  2743. md_wakeup_thread(mddev->thread);
  2744. spin_unlock_irqrestore(&conf->device_lock, flags);
  2745. unplug_slaves(mddev);
  2746. }
  2747. static int raid5_congested(void *data, int bits)
  2748. {
  2749. mddev_t *mddev = data;
  2750. raid5_conf_t *conf = mddev_to_conf(mddev);
  2751. /* No difference between reads and writes. Just check
  2752. * how busy the stripe_cache is
  2753. */
  2754. if (conf->inactive_blocked)
  2755. return 1;
  2756. if (conf->quiesce)
  2757. return 1;
  2758. if (list_empty_careful(&conf->inactive_list))
  2759. return 1;
  2760. return 0;
  2761. }
  2762. /* We want read requests to align with chunks where possible,
  2763. * but write requests don't need to.
  2764. */
  2765. static int raid5_mergeable_bvec(struct request_queue *q,
  2766. struct bvec_merge_data *bvm,
  2767. struct bio_vec *biovec)
  2768. {
  2769. mddev_t *mddev = q->queuedata;
  2770. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  2771. int max;
  2772. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2773. unsigned int bio_sectors = bvm->bi_size >> 9;
  2774. if ((bvm->bi_rw & 1) == WRITE)
  2775. return biovec->bv_len; /* always allow writes to be mergeable */
  2776. max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
  2777. if (max < 0) max = 0;
  2778. if (max <= biovec->bv_len && bio_sectors == 0)
  2779. return biovec->bv_len;
  2780. else
  2781. return max;
  2782. }
  2783. static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
  2784. {
  2785. sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
  2786. unsigned int chunk_sectors = mddev->chunk_size >> 9;
  2787. unsigned int bio_sectors = bio->bi_size >> 9;
  2788. return chunk_sectors >=
  2789. ((sector & (chunk_sectors - 1)) + bio_sectors);
  2790. }
  2791. /*
  2792. * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
  2793. * later sampled by raid5d.
  2794. */
  2795. static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
  2796. {
  2797. unsigned long flags;
  2798. spin_lock_irqsave(&conf->device_lock, flags);
  2799. bi->bi_next = conf->retry_read_aligned_list;
  2800. conf->retry_read_aligned_list = bi;
  2801. spin_unlock_irqrestore(&conf->device_lock, flags);
  2802. md_wakeup_thread(conf->mddev->thread);
  2803. }
  2804. static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
  2805. {
  2806. struct bio *bi;
  2807. bi = conf->retry_read_aligned;
  2808. if (bi) {
  2809. conf->retry_read_aligned = NULL;
  2810. return bi;
  2811. }
  2812. bi = conf->retry_read_aligned_list;
  2813. if(bi) {
  2814. conf->retry_read_aligned_list = bi->bi_next;
  2815. bi->bi_next = NULL;
  2816. /*
  2817. * this sets the active strip count to 1 and the processed
  2818. * strip count to zero (upper 8 bits)
  2819. */
  2820. bi->bi_phys_segments = 1; /* biased count of active stripes */
  2821. }
  2822. return bi;
  2823. }
  2824. /*
  2825. * The "raid5_align_endio" should check if the read succeeded and if it
  2826. * did, call bio_endio on the original bio (having bio_put the new bio
  2827. * first).
  2828. * If the read failed..
  2829. */
  2830. static void raid5_align_endio(struct bio *bi, int error)
  2831. {
  2832. struct bio* raid_bi = bi->bi_private;
  2833. mddev_t *mddev;
  2834. raid5_conf_t *conf;
  2835. int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
  2836. mdk_rdev_t *rdev;
  2837. bio_put(bi);
  2838. mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
  2839. conf = mddev_to_conf(mddev);
  2840. rdev = (void*)raid_bi->bi_next;
  2841. raid_bi->bi_next = NULL;
  2842. rdev_dec_pending(rdev, conf->mddev);
  2843. if (!error && uptodate) {
  2844. bio_endio(raid_bi, 0);
  2845. if (atomic_dec_and_test(&conf->active_aligned_reads))
  2846. wake_up(&conf->wait_for_stripe);
  2847. return;
  2848. }
  2849. pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
  2850. add_bio_to_retry(raid_bi, conf);
  2851. }
  2852. static int bio_fits_rdev(struct bio *bi)
  2853. {
  2854. struct request_queue *q = bdev_get_queue(bi->bi_bdev);
  2855. if ((bi->bi_size>>9) > q->max_sectors)
  2856. return 0;
  2857. blk_recount_segments(q, bi);
  2858. if (bi->bi_phys_segments > q->max_phys_segments)
  2859. return 0;
  2860. if (q->merge_bvec_fn)
  2861. /* it's too hard to apply the merge_bvec_fn at this stage,
  2862. * just just give up
  2863. */
  2864. return 0;
  2865. return 1;
  2866. }
  2867. static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
  2868. {
  2869. mddev_t *mddev = q->queuedata;
  2870. raid5_conf_t *conf = mddev_to_conf(mddev);
  2871. const unsigned int raid_disks = conf->raid_disks;
  2872. const unsigned int data_disks = raid_disks - conf->max_degraded;
  2873. unsigned int dd_idx, pd_idx;
  2874. struct bio* align_bi;
  2875. mdk_rdev_t *rdev;
  2876. if (!in_chunk_boundary(mddev, raid_bio)) {
  2877. pr_debug("chunk_aligned_read : non aligned\n");
  2878. return 0;
  2879. }
  2880. /*
  2881. * use bio_clone to make a copy of the bio
  2882. */
  2883. align_bi = bio_clone(raid_bio, GFP_NOIO);
  2884. if (!align_bi)
  2885. return 0;
  2886. /*
  2887. * set bi_end_io to a new function, and set bi_private to the
  2888. * original bio.
  2889. */
  2890. align_bi->bi_end_io = raid5_align_endio;
  2891. align_bi->bi_private = raid_bio;
  2892. /*
  2893. * compute position
  2894. */
  2895. align_bi->bi_sector = raid5_compute_sector(raid_bio->bi_sector,
  2896. raid_disks,
  2897. data_disks,
  2898. &dd_idx,
  2899. &pd_idx,
  2900. conf);
  2901. rcu_read_lock();
  2902. rdev = rcu_dereference(conf->disks[dd_idx].rdev);
  2903. if (rdev && test_bit(In_sync, &rdev->flags)) {
  2904. atomic_inc(&rdev->nr_pending);
  2905. rcu_read_unlock();
  2906. raid_bio->bi_next = (void*)rdev;
  2907. align_bi->bi_bdev = rdev->bdev;
  2908. align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
  2909. align_bi->bi_sector += rdev->data_offset;
  2910. if (!bio_fits_rdev(align_bi)) {
  2911. /* too big in some way */
  2912. bio_put(align_bi);
  2913. rdev_dec_pending(rdev, mddev);
  2914. return 0;
  2915. }
  2916. spin_lock_irq(&conf->device_lock);
  2917. wait_event_lock_irq(conf->wait_for_stripe,
  2918. conf->quiesce == 0,
  2919. conf->device_lock, /* nothing */);
  2920. atomic_inc(&conf->active_aligned_reads);
  2921. spin_unlock_irq(&conf->device_lock);
  2922. generic_make_request(align_bi);
  2923. return 1;
  2924. } else {
  2925. rcu_read_unlock();
  2926. bio_put(align_bi);
  2927. return 0;
  2928. }
  2929. }
  2930. /* __get_priority_stripe - get the next stripe to process
  2931. *
  2932. * Full stripe writes are allowed to pass preread active stripes up until
  2933. * the bypass_threshold is exceeded. In general the bypass_count
  2934. * increments when the handle_list is handled before the hold_list; however, it
  2935. * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
  2936. * stripe with in flight i/o. The bypass_count will be reset when the
  2937. * head of the hold_list has changed, i.e. the head was promoted to the
  2938. * handle_list.
  2939. */
  2940. static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
  2941. {
  2942. struct stripe_head *sh;
  2943. pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
  2944. __func__,
  2945. list_empty(&conf->handle_list) ? "empty" : "busy",
  2946. list_empty(&conf->hold_list) ? "empty" : "busy",
  2947. atomic_read(&conf->pending_full_writes), conf->bypass_count);
  2948. if (!list_empty(&conf->handle_list)) {
  2949. sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
  2950. if (list_empty(&conf->hold_list))
  2951. conf->bypass_count = 0;
  2952. else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
  2953. if (conf->hold_list.next == conf->last_hold)
  2954. conf->bypass_count++;
  2955. else {
  2956. conf->last_hold = conf->hold_list.next;
  2957. conf->bypass_count -= conf->bypass_threshold;
  2958. if (conf->bypass_count < 0)
  2959. conf->bypass_count = 0;
  2960. }
  2961. }
  2962. } else if (!list_empty(&conf->hold_list) &&
  2963. ((conf->bypass_threshold &&
  2964. conf->bypass_count > conf->bypass_threshold) ||
  2965. atomic_read(&conf->pending_full_writes) == 0)) {
  2966. sh = list_entry(conf->hold_list.next,
  2967. typeof(*sh), lru);
  2968. conf->bypass_count -= conf->bypass_threshold;
  2969. if (conf->bypass_count < 0)
  2970. conf->bypass_count = 0;
  2971. } else
  2972. return NULL;
  2973. list_del_init(&sh->lru);
  2974. atomic_inc(&sh->count);
  2975. BUG_ON(atomic_read(&sh->count) != 1);
  2976. return sh;
  2977. }
  2978. static int make_request(struct request_queue *q, struct bio * bi)
  2979. {
  2980. mddev_t *mddev = q->queuedata;
  2981. raid5_conf_t *conf = mddev_to_conf(mddev);
  2982. unsigned int dd_idx, pd_idx;
  2983. sector_t new_sector;
  2984. sector_t logical_sector, last_sector;
  2985. struct stripe_head *sh;
  2986. const int rw = bio_data_dir(bi);
  2987. int cpu, remaining;
  2988. if (unlikely(bio_barrier(bi))) {
  2989. bio_endio(bi, -EOPNOTSUPP);
  2990. return 0;
  2991. }
  2992. md_write_start(mddev, bi);
  2993. cpu = part_stat_lock();
  2994. part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
  2995. part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
  2996. bio_sectors(bi));
  2997. part_stat_unlock();
  2998. if (rw == READ &&
  2999. mddev->reshape_position == MaxSector &&
  3000. chunk_aligned_read(q,bi))
  3001. return 0;
  3002. logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3003. last_sector = bi->bi_sector + (bi->bi_size>>9);
  3004. bi->bi_next = NULL;
  3005. bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
  3006. for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
  3007. DEFINE_WAIT(w);
  3008. int disks, data_disks;
  3009. retry:
  3010. prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
  3011. if (likely(conf->expand_progress == MaxSector))
  3012. disks = conf->raid_disks;
  3013. else {
  3014. /* spinlock is needed as expand_progress may be
  3015. * 64bit on a 32bit platform, and so it might be
  3016. * possible to see a half-updated value
  3017. * Ofcourse expand_progress could change after
  3018. * the lock is dropped, so once we get a reference
  3019. * to the stripe that we think it is, we will have
  3020. * to check again.
  3021. */
  3022. spin_lock_irq(&conf->device_lock);
  3023. disks = conf->raid_disks;
  3024. if (logical_sector >= conf->expand_progress)
  3025. disks = conf->previous_raid_disks;
  3026. else {
  3027. if (logical_sector >= conf->expand_lo) {
  3028. spin_unlock_irq(&conf->device_lock);
  3029. schedule();
  3030. goto retry;
  3031. }
  3032. }
  3033. spin_unlock_irq(&conf->device_lock);
  3034. }
  3035. data_disks = disks - conf->max_degraded;
  3036. new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
  3037. &dd_idx, &pd_idx, conf);
  3038. pr_debug("raid5: make_request, sector %llu logical %llu\n",
  3039. (unsigned long long)new_sector,
  3040. (unsigned long long)logical_sector);
  3041. sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
  3042. if (sh) {
  3043. if (unlikely(conf->expand_progress != MaxSector)) {
  3044. /* expansion might have moved on while waiting for a
  3045. * stripe, so we must do the range check again.
  3046. * Expansion could still move past after this
  3047. * test, but as we are holding a reference to
  3048. * 'sh', we know that if that happens,
  3049. * STRIPE_EXPANDING will get set and the expansion
  3050. * won't proceed until we finish with the stripe.
  3051. */
  3052. int must_retry = 0;
  3053. spin_lock_irq(&conf->device_lock);
  3054. if (logical_sector < conf->expand_progress &&
  3055. disks == conf->previous_raid_disks)
  3056. /* mismatch, need to try again */
  3057. must_retry = 1;
  3058. spin_unlock_irq(&conf->device_lock);
  3059. if (must_retry) {
  3060. release_stripe(sh);
  3061. goto retry;
  3062. }
  3063. }
  3064. /* FIXME what if we get a false positive because these
  3065. * are being updated.
  3066. */
  3067. if (logical_sector >= mddev->suspend_lo &&
  3068. logical_sector < mddev->suspend_hi) {
  3069. release_stripe(sh);
  3070. schedule();
  3071. goto retry;
  3072. }
  3073. if (test_bit(STRIPE_EXPANDING, &sh->state) ||
  3074. !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
  3075. /* Stripe is busy expanding or
  3076. * add failed due to overlap. Flush everything
  3077. * and wait a while
  3078. */
  3079. raid5_unplug_device(mddev->queue);
  3080. release_stripe(sh);
  3081. schedule();
  3082. goto retry;
  3083. }
  3084. finish_wait(&conf->wait_for_overlap, &w);
  3085. set_bit(STRIPE_HANDLE, &sh->state);
  3086. clear_bit(STRIPE_DELAYED, &sh->state);
  3087. release_stripe(sh);
  3088. } else {
  3089. /* cannot get stripe for read-ahead, just give-up */
  3090. clear_bit(BIO_UPTODATE, &bi->bi_flags);
  3091. finish_wait(&conf->wait_for_overlap, &w);
  3092. break;
  3093. }
  3094. }
  3095. spin_lock_irq(&conf->device_lock);
  3096. remaining = raid5_dec_bi_phys_segments(bi);
  3097. spin_unlock_irq(&conf->device_lock);
  3098. if (remaining == 0) {
  3099. if ( rw == WRITE )
  3100. md_write_end(mddev);
  3101. bio_endio(bi, 0);
  3102. }
  3103. return 0;
  3104. }
  3105. static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
  3106. {
  3107. /* reshaping is quite different to recovery/resync so it is
  3108. * handled quite separately ... here.
  3109. *
  3110. * On each call to sync_request, we gather one chunk worth of
  3111. * destination stripes and flag them as expanding.
  3112. * Then we find all the source stripes and request reads.
  3113. * As the reads complete, handle_stripe will copy the data
  3114. * into the destination stripe and release that stripe.
  3115. */
  3116. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3117. struct stripe_head *sh;
  3118. int pd_idx;
  3119. sector_t first_sector, last_sector;
  3120. int raid_disks = conf->previous_raid_disks;
  3121. int data_disks = raid_disks - conf->max_degraded;
  3122. int new_data_disks = conf->raid_disks - conf->max_degraded;
  3123. int i;
  3124. int dd_idx;
  3125. sector_t writepos, safepos, gap;
  3126. if (sector_nr == 0 &&
  3127. conf->expand_progress != 0) {
  3128. /* restarting in the middle, skip the initial sectors */
  3129. sector_nr = conf->expand_progress;
  3130. sector_div(sector_nr, new_data_disks);
  3131. *skipped = 1;
  3132. return sector_nr;
  3133. }
  3134. /* we update the metadata when there is more than 3Meg
  3135. * in the block range (that is rather arbitrary, should
  3136. * probably be time based) or when the data about to be
  3137. * copied would over-write the source of the data at
  3138. * the front of the range.
  3139. * i.e. one new_stripe forward from expand_progress new_maps
  3140. * to after where expand_lo old_maps to
  3141. */
  3142. writepos = conf->expand_progress +
  3143. conf->chunk_size/512*(new_data_disks);
  3144. sector_div(writepos, new_data_disks);
  3145. safepos = conf->expand_lo;
  3146. sector_div(safepos, data_disks);
  3147. gap = conf->expand_progress - conf->expand_lo;
  3148. if (writepos >= safepos ||
  3149. gap > (new_data_disks)*3000*2 /*3Meg*/) {
  3150. /* Cannot proceed until we've updated the superblock... */
  3151. wait_event(conf->wait_for_overlap,
  3152. atomic_read(&conf->reshape_stripes)==0);
  3153. mddev->reshape_position = conf->expand_progress;
  3154. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3155. md_wakeup_thread(mddev->thread);
  3156. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3157. kthread_should_stop());
  3158. spin_lock_irq(&conf->device_lock);
  3159. conf->expand_lo = mddev->reshape_position;
  3160. spin_unlock_irq(&conf->device_lock);
  3161. wake_up(&conf->wait_for_overlap);
  3162. }
  3163. for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
  3164. int j;
  3165. int skipped = 0;
  3166. pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
  3167. sh = get_active_stripe(conf, sector_nr+i,
  3168. conf->raid_disks, pd_idx, 0);
  3169. set_bit(STRIPE_EXPANDING, &sh->state);
  3170. atomic_inc(&conf->reshape_stripes);
  3171. /* If any of this stripe is beyond the end of the old
  3172. * array, then we need to zero those blocks
  3173. */
  3174. for (j=sh->disks; j--;) {
  3175. sector_t s;
  3176. if (j == sh->pd_idx)
  3177. continue;
  3178. if (conf->level == 6 &&
  3179. j == raid6_next_disk(sh->pd_idx, sh->disks))
  3180. continue;
  3181. s = compute_blocknr(sh, j);
  3182. if (s < mddev->array_sectors) {
  3183. skipped = 1;
  3184. continue;
  3185. }
  3186. memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
  3187. set_bit(R5_Expanded, &sh->dev[j].flags);
  3188. set_bit(R5_UPTODATE, &sh->dev[j].flags);
  3189. }
  3190. if (!skipped) {
  3191. set_bit(STRIPE_EXPAND_READY, &sh->state);
  3192. set_bit(STRIPE_HANDLE, &sh->state);
  3193. }
  3194. release_stripe(sh);
  3195. }
  3196. spin_lock_irq(&conf->device_lock);
  3197. conf->expand_progress = (sector_nr + i) * new_data_disks;
  3198. spin_unlock_irq(&conf->device_lock);
  3199. /* Ok, those stripe are ready. We can start scheduling
  3200. * reads on the source stripes.
  3201. * The source stripes are determined by mapping the first and last
  3202. * block on the destination stripes.
  3203. */
  3204. first_sector =
  3205. raid5_compute_sector(sector_nr*(new_data_disks),
  3206. raid_disks, data_disks,
  3207. &dd_idx, &pd_idx, conf);
  3208. last_sector =
  3209. raid5_compute_sector((sector_nr+conf->chunk_size/512)
  3210. *(new_data_disks) -1,
  3211. raid_disks, data_disks,
  3212. &dd_idx, &pd_idx, conf);
  3213. if (last_sector >= (mddev->size<<1))
  3214. last_sector = (mddev->size<<1)-1;
  3215. while (first_sector <= last_sector) {
  3216. pd_idx = stripe_to_pdidx(first_sector, conf,
  3217. conf->previous_raid_disks);
  3218. sh = get_active_stripe(conf, first_sector,
  3219. conf->previous_raid_disks, pd_idx, 0);
  3220. set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
  3221. set_bit(STRIPE_HANDLE, &sh->state);
  3222. release_stripe(sh);
  3223. first_sector += STRIPE_SECTORS;
  3224. }
  3225. /* If this takes us to the resync_max point where we have to pause,
  3226. * then we need to write out the superblock.
  3227. */
  3228. sector_nr += conf->chunk_size>>9;
  3229. if (sector_nr >= mddev->resync_max) {
  3230. /* Cannot proceed until we've updated the superblock... */
  3231. wait_event(conf->wait_for_overlap,
  3232. atomic_read(&conf->reshape_stripes) == 0);
  3233. mddev->reshape_position = conf->expand_progress;
  3234. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3235. md_wakeup_thread(mddev->thread);
  3236. wait_event(mddev->sb_wait,
  3237. !test_bit(MD_CHANGE_DEVS, &mddev->flags)
  3238. || kthread_should_stop());
  3239. spin_lock_irq(&conf->device_lock);
  3240. conf->expand_lo = mddev->reshape_position;
  3241. spin_unlock_irq(&conf->device_lock);
  3242. wake_up(&conf->wait_for_overlap);
  3243. }
  3244. return conf->chunk_size>>9;
  3245. }
  3246. /* FIXME go_faster isn't used */
  3247. static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
  3248. {
  3249. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3250. struct stripe_head *sh;
  3251. int pd_idx;
  3252. int raid_disks = conf->raid_disks;
  3253. sector_t max_sector = mddev->size << 1;
  3254. int sync_blocks;
  3255. int still_degraded = 0;
  3256. int i;
  3257. if (sector_nr >= max_sector) {
  3258. /* just being told to finish up .. nothing much to do */
  3259. unplug_slaves(mddev);
  3260. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  3261. end_reshape(conf);
  3262. return 0;
  3263. }
  3264. if (mddev->curr_resync < max_sector) /* aborted */
  3265. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  3266. &sync_blocks, 1);
  3267. else /* completed sync */
  3268. conf->fullsync = 0;
  3269. bitmap_close_sync(mddev->bitmap);
  3270. return 0;
  3271. }
  3272. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  3273. return reshape_request(mddev, sector_nr, skipped);
  3274. /* No need to check resync_max as we never do more than one
  3275. * stripe, and as resync_max will always be on a chunk boundary,
  3276. * if the check in md_do_sync didn't fire, there is no chance
  3277. * of overstepping resync_max here
  3278. */
  3279. /* if there is too many failed drives and we are trying
  3280. * to resync, then assert that we are finished, because there is
  3281. * nothing we can do.
  3282. */
  3283. if (mddev->degraded >= conf->max_degraded &&
  3284. test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  3285. sector_t rv = (mddev->size << 1) - sector_nr;
  3286. *skipped = 1;
  3287. return rv;
  3288. }
  3289. if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
  3290. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  3291. !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
  3292. /* we can skip this block, and probably more */
  3293. sync_blocks /= STRIPE_SECTORS;
  3294. *skipped = 1;
  3295. return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
  3296. }
  3297. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  3298. pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
  3299. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
  3300. if (sh == NULL) {
  3301. sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
  3302. /* make sure we don't swamp the stripe cache if someone else
  3303. * is trying to get access
  3304. */
  3305. schedule_timeout_uninterruptible(1);
  3306. }
  3307. /* Need to check if array will still be degraded after recovery/resync
  3308. * We don't need to check the 'failed' flag as when that gets set,
  3309. * recovery aborts.
  3310. */
  3311. for (i=0; i<mddev->raid_disks; i++)
  3312. if (conf->disks[i].rdev == NULL)
  3313. still_degraded = 1;
  3314. bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
  3315. spin_lock(&sh->lock);
  3316. set_bit(STRIPE_SYNCING, &sh->state);
  3317. clear_bit(STRIPE_INSYNC, &sh->state);
  3318. spin_unlock(&sh->lock);
  3319. /* wait for any blocked device to be handled */
  3320. while(unlikely(!handle_stripe(sh, NULL)))
  3321. ;
  3322. release_stripe(sh);
  3323. return STRIPE_SECTORS;
  3324. }
  3325. static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
  3326. {
  3327. /* We may not be able to submit a whole bio at once as there
  3328. * may not be enough stripe_heads available.
  3329. * We cannot pre-allocate enough stripe_heads as we may need
  3330. * more than exist in the cache (if we allow ever large chunks).
  3331. * So we do one stripe head at a time and record in
  3332. * ->bi_hw_segments how many have been done.
  3333. *
  3334. * We *know* that this entire raid_bio is in one chunk, so
  3335. * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
  3336. */
  3337. struct stripe_head *sh;
  3338. int dd_idx, pd_idx;
  3339. sector_t sector, logical_sector, last_sector;
  3340. int scnt = 0;
  3341. int remaining;
  3342. int handled = 0;
  3343. logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
  3344. sector = raid5_compute_sector( logical_sector,
  3345. conf->raid_disks,
  3346. conf->raid_disks - conf->max_degraded,
  3347. &dd_idx,
  3348. &pd_idx,
  3349. conf);
  3350. last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
  3351. for (; logical_sector < last_sector;
  3352. logical_sector += STRIPE_SECTORS,
  3353. sector += STRIPE_SECTORS,
  3354. scnt++) {
  3355. if (scnt < raid5_bi_hw_segments(raid_bio))
  3356. /* already done this stripe */
  3357. continue;
  3358. sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1);
  3359. if (!sh) {
  3360. /* failed to get a stripe - must wait */
  3361. raid5_set_bi_hw_segments(raid_bio, scnt);
  3362. conf->retry_read_aligned = raid_bio;
  3363. return handled;
  3364. }
  3365. set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
  3366. if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
  3367. release_stripe(sh);
  3368. raid5_set_bi_hw_segments(raid_bio, scnt);
  3369. conf->retry_read_aligned = raid_bio;
  3370. return handled;
  3371. }
  3372. handle_stripe(sh, NULL);
  3373. release_stripe(sh);
  3374. handled++;
  3375. }
  3376. spin_lock_irq(&conf->device_lock);
  3377. remaining = raid5_dec_bi_phys_segments(raid_bio);
  3378. spin_unlock_irq(&conf->device_lock);
  3379. if (remaining == 0)
  3380. bio_endio(raid_bio, 0);
  3381. if (atomic_dec_and_test(&conf->active_aligned_reads))
  3382. wake_up(&conf->wait_for_stripe);
  3383. return handled;
  3384. }
  3385. /*
  3386. * This is our raid5 kernel thread.
  3387. *
  3388. * We scan the hash table for stripes which can be handled now.
  3389. * During the scan, completed stripes are saved for us by the interrupt
  3390. * handler, so that they will not have to wait for our next wakeup.
  3391. */
  3392. static void raid5d(mddev_t *mddev)
  3393. {
  3394. struct stripe_head *sh;
  3395. raid5_conf_t *conf = mddev_to_conf(mddev);
  3396. int handled;
  3397. pr_debug("+++ raid5d active\n");
  3398. md_check_recovery(mddev);
  3399. handled = 0;
  3400. spin_lock_irq(&conf->device_lock);
  3401. while (1) {
  3402. struct bio *bio;
  3403. if (conf->seq_flush != conf->seq_write) {
  3404. int seq = conf->seq_flush;
  3405. spin_unlock_irq(&conf->device_lock);
  3406. bitmap_unplug(mddev->bitmap);
  3407. spin_lock_irq(&conf->device_lock);
  3408. conf->seq_write = seq;
  3409. activate_bit_delay(conf);
  3410. }
  3411. while ((bio = remove_bio_from_retry(conf))) {
  3412. int ok;
  3413. spin_unlock_irq(&conf->device_lock);
  3414. ok = retry_aligned_read(conf, bio);
  3415. spin_lock_irq(&conf->device_lock);
  3416. if (!ok)
  3417. break;
  3418. handled++;
  3419. }
  3420. sh = __get_priority_stripe(conf);
  3421. if (!sh)
  3422. break;
  3423. spin_unlock_irq(&conf->device_lock);
  3424. handled++;
  3425. handle_stripe(sh, conf->spare_page);
  3426. release_stripe(sh);
  3427. spin_lock_irq(&conf->device_lock);
  3428. }
  3429. pr_debug("%d stripes handled\n", handled);
  3430. spin_unlock_irq(&conf->device_lock);
  3431. async_tx_issue_pending_all();
  3432. unplug_slaves(mddev);
  3433. pr_debug("--- raid5d inactive\n");
  3434. }
  3435. static ssize_t
  3436. raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
  3437. {
  3438. raid5_conf_t *conf = mddev_to_conf(mddev);
  3439. if (conf)
  3440. return sprintf(page, "%d\n", conf->max_nr_stripes);
  3441. else
  3442. return 0;
  3443. }
  3444. static ssize_t
  3445. raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
  3446. {
  3447. raid5_conf_t *conf = mddev_to_conf(mddev);
  3448. unsigned long new;
  3449. int err;
  3450. if (len >= PAGE_SIZE)
  3451. return -EINVAL;
  3452. if (!conf)
  3453. return -ENODEV;
  3454. if (strict_strtoul(page, 10, &new))
  3455. return -EINVAL;
  3456. if (new <= 16 || new > 32768)
  3457. return -EINVAL;
  3458. while (new < conf->max_nr_stripes) {
  3459. if (drop_one_stripe(conf))
  3460. conf->max_nr_stripes--;
  3461. else
  3462. break;
  3463. }
  3464. err = md_allow_write(mddev);
  3465. if (err)
  3466. return err;
  3467. while (new > conf->max_nr_stripes) {
  3468. if (grow_one_stripe(conf))
  3469. conf->max_nr_stripes++;
  3470. else break;
  3471. }
  3472. return len;
  3473. }
  3474. static struct md_sysfs_entry
  3475. raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
  3476. raid5_show_stripe_cache_size,
  3477. raid5_store_stripe_cache_size);
  3478. static ssize_t
  3479. raid5_show_preread_threshold(mddev_t *mddev, char *page)
  3480. {
  3481. raid5_conf_t *conf = mddev_to_conf(mddev);
  3482. if (conf)
  3483. return sprintf(page, "%d\n", conf->bypass_threshold);
  3484. else
  3485. return 0;
  3486. }
  3487. static ssize_t
  3488. raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
  3489. {
  3490. raid5_conf_t *conf = mddev_to_conf(mddev);
  3491. unsigned long new;
  3492. if (len >= PAGE_SIZE)
  3493. return -EINVAL;
  3494. if (!conf)
  3495. return -ENODEV;
  3496. if (strict_strtoul(page, 10, &new))
  3497. return -EINVAL;
  3498. if (new > conf->max_nr_stripes)
  3499. return -EINVAL;
  3500. conf->bypass_threshold = new;
  3501. return len;
  3502. }
  3503. static struct md_sysfs_entry
  3504. raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
  3505. S_IRUGO | S_IWUSR,
  3506. raid5_show_preread_threshold,
  3507. raid5_store_preread_threshold);
  3508. static ssize_t
  3509. stripe_cache_active_show(mddev_t *mddev, char *page)
  3510. {
  3511. raid5_conf_t *conf = mddev_to_conf(mddev);
  3512. if (conf)
  3513. return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
  3514. else
  3515. return 0;
  3516. }
  3517. static struct md_sysfs_entry
  3518. raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
  3519. static struct attribute *raid5_attrs[] = {
  3520. &raid5_stripecache_size.attr,
  3521. &raid5_stripecache_active.attr,
  3522. &raid5_preread_bypass_threshold.attr,
  3523. NULL,
  3524. };
  3525. static struct attribute_group raid5_attrs_group = {
  3526. .name = NULL,
  3527. .attrs = raid5_attrs,
  3528. };
  3529. static int run(mddev_t *mddev)
  3530. {
  3531. raid5_conf_t *conf;
  3532. int raid_disk, memory;
  3533. mdk_rdev_t *rdev;
  3534. struct disk_info *disk;
  3535. struct list_head *tmp;
  3536. int working_disks = 0;
  3537. if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
  3538. printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
  3539. mdname(mddev), mddev->level);
  3540. return -EIO;
  3541. }
  3542. if (mddev->reshape_position != MaxSector) {
  3543. /* Check that we can continue the reshape.
  3544. * Currently only disks can change, it must
  3545. * increase, and we must be past the point where
  3546. * a stripe over-writes itself
  3547. */
  3548. sector_t here_new, here_old;
  3549. int old_disks;
  3550. int max_degraded = (mddev->level == 5 ? 1 : 2);
  3551. if (mddev->new_level != mddev->level ||
  3552. mddev->new_layout != mddev->layout ||
  3553. mddev->new_chunk != mddev->chunk_size) {
  3554. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3555. "required - aborting.\n",
  3556. mdname(mddev));
  3557. return -EINVAL;
  3558. }
  3559. if (mddev->delta_disks <= 0) {
  3560. printk(KERN_ERR "raid5: %s: unsupported reshape "
  3561. "(reduce disks) required - aborting.\n",
  3562. mdname(mddev));
  3563. return -EINVAL;
  3564. }
  3565. old_disks = mddev->raid_disks - mddev->delta_disks;
  3566. /* reshape_position must be on a new-stripe boundary, and one
  3567. * further up in new geometry must map after here in old
  3568. * geometry.
  3569. */
  3570. here_new = mddev->reshape_position;
  3571. if (sector_div(here_new, (mddev->chunk_size>>9)*
  3572. (mddev->raid_disks - max_degraded))) {
  3573. printk(KERN_ERR "raid5: reshape_position not "
  3574. "on a stripe boundary\n");
  3575. return -EINVAL;
  3576. }
  3577. /* here_new is the stripe we will write to */
  3578. here_old = mddev->reshape_position;
  3579. sector_div(here_old, (mddev->chunk_size>>9)*
  3580. (old_disks-max_degraded));
  3581. /* here_old is the first stripe that we might need to read
  3582. * from */
  3583. if (here_new >= here_old) {
  3584. /* Reading from the same stripe as writing to - bad */
  3585. printk(KERN_ERR "raid5: reshape_position too early for "
  3586. "auto-recovery - aborting.\n");
  3587. return -EINVAL;
  3588. }
  3589. printk(KERN_INFO "raid5: reshape will continue\n");
  3590. /* OK, we should be able to continue; */
  3591. }
  3592. mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
  3593. if ((conf = mddev->private) == NULL)
  3594. goto abort;
  3595. if (mddev->reshape_position == MaxSector) {
  3596. conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
  3597. } else {
  3598. conf->raid_disks = mddev->raid_disks;
  3599. conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
  3600. }
  3601. conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
  3602. GFP_KERNEL);
  3603. if (!conf->disks)
  3604. goto abort;
  3605. conf->mddev = mddev;
  3606. if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
  3607. goto abort;
  3608. if (mddev->level == 6) {
  3609. conf->spare_page = alloc_page(GFP_KERNEL);
  3610. if (!conf->spare_page)
  3611. goto abort;
  3612. }
  3613. spin_lock_init(&conf->device_lock);
  3614. mddev->queue->queue_lock = &conf->device_lock;
  3615. init_waitqueue_head(&conf->wait_for_stripe);
  3616. init_waitqueue_head(&conf->wait_for_overlap);
  3617. INIT_LIST_HEAD(&conf->handle_list);
  3618. INIT_LIST_HEAD(&conf->hold_list);
  3619. INIT_LIST_HEAD(&conf->delayed_list);
  3620. INIT_LIST_HEAD(&conf->bitmap_list);
  3621. INIT_LIST_HEAD(&conf->inactive_list);
  3622. atomic_set(&conf->active_stripes, 0);
  3623. atomic_set(&conf->preread_active_stripes, 0);
  3624. atomic_set(&conf->active_aligned_reads, 0);
  3625. conf->bypass_threshold = BYPASS_THRESHOLD;
  3626. pr_debug("raid5: run(%s) called.\n", mdname(mddev));
  3627. rdev_for_each(rdev, tmp, mddev) {
  3628. raid_disk = rdev->raid_disk;
  3629. if (raid_disk >= conf->raid_disks
  3630. || raid_disk < 0)
  3631. continue;
  3632. disk = conf->disks + raid_disk;
  3633. disk->rdev = rdev;
  3634. if (test_bit(In_sync, &rdev->flags)) {
  3635. char b[BDEVNAME_SIZE];
  3636. printk(KERN_INFO "raid5: device %s operational as raid"
  3637. " disk %d\n", bdevname(rdev->bdev,b),
  3638. raid_disk);
  3639. working_disks++;
  3640. } else
  3641. /* Cannot rely on bitmap to complete recovery */
  3642. conf->fullsync = 1;
  3643. }
  3644. /*
  3645. * 0 for a fully functional array, 1 or 2 for a degraded array.
  3646. */
  3647. mddev->degraded = conf->raid_disks - working_disks;
  3648. conf->mddev = mddev;
  3649. conf->chunk_size = mddev->chunk_size;
  3650. conf->level = mddev->level;
  3651. if (conf->level == 6)
  3652. conf->max_degraded = 2;
  3653. else
  3654. conf->max_degraded = 1;
  3655. conf->algorithm = mddev->layout;
  3656. conf->max_nr_stripes = NR_STRIPES;
  3657. conf->expand_progress = mddev->reshape_position;
  3658. /* device size must be a multiple of chunk size */
  3659. mddev->size &= ~(mddev->chunk_size/1024 -1);
  3660. mddev->resync_max_sectors = mddev->size << 1;
  3661. if (conf->level == 6 && conf->raid_disks < 4) {
  3662. printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
  3663. mdname(mddev), conf->raid_disks);
  3664. goto abort;
  3665. }
  3666. if (!conf->chunk_size || conf->chunk_size % 4) {
  3667. printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
  3668. conf->chunk_size, mdname(mddev));
  3669. goto abort;
  3670. }
  3671. if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
  3672. printk(KERN_ERR
  3673. "raid5: unsupported parity algorithm %d for %s\n",
  3674. conf->algorithm, mdname(mddev));
  3675. goto abort;
  3676. }
  3677. if (mddev->degraded > conf->max_degraded) {
  3678. printk(KERN_ERR "raid5: not enough operational devices for %s"
  3679. " (%d/%d failed)\n",
  3680. mdname(mddev), mddev->degraded, conf->raid_disks);
  3681. goto abort;
  3682. }
  3683. if (mddev->degraded > 0 &&
  3684. mddev->recovery_cp != MaxSector) {
  3685. if (mddev->ok_start_degraded)
  3686. printk(KERN_WARNING
  3687. "raid5: starting dirty degraded array: %s"
  3688. "- data corruption possible.\n",
  3689. mdname(mddev));
  3690. else {
  3691. printk(KERN_ERR
  3692. "raid5: cannot start dirty degraded array for %s\n",
  3693. mdname(mddev));
  3694. goto abort;
  3695. }
  3696. }
  3697. {
  3698. mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
  3699. if (!mddev->thread) {
  3700. printk(KERN_ERR
  3701. "raid5: couldn't allocate thread for %s\n",
  3702. mdname(mddev));
  3703. goto abort;
  3704. }
  3705. }
  3706. memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
  3707. conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
  3708. if (grow_stripes(conf, conf->max_nr_stripes)) {
  3709. printk(KERN_ERR
  3710. "raid5: couldn't allocate %dkB for buffers\n", memory);
  3711. shrink_stripes(conf);
  3712. md_unregister_thread(mddev->thread);
  3713. goto abort;
  3714. } else
  3715. printk(KERN_INFO "raid5: allocated %dkB for %s\n",
  3716. memory, mdname(mddev));
  3717. if (mddev->degraded == 0)
  3718. printk("raid5: raid level %d set %s active with %d out of %d"
  3719. " devices, algorithm %d\n", conf->level, mdname(mddev),
  3720. mddev->raid_disks-mddev->degraded, mddev->raid_disks,
  3721. conf->algorithm);
  3722. else
  3723. printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
  3724. " out of %d devices, algorithm %d\n", conf->level,
  3725. mdname(mddev), mddev->raid_disks - mddev->degraded,
  3726. mddev->raid_disks, conf->algorithm);
  3727. print_raid5_conf(conf);
  3728. if (conf->expand_progress != MaxSector) {
  3729. printk("...ok start reshape thread\n");
  3730. conf->expand_lo = conf->expand_progress;
  3731. atomic_set(&conf->reshape_stripes, 0);
  3732. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3733. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3734. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3735. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3736. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3737. "%s_reshape");
  3738. }
  3739. /* read-ahead size must cover two whole stripes, which is
  3740. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  3741. */
  3742. {
  3743. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  3744. int stripe = data_disks *
  3745. (mddev->chunk_size / PAGE_SIZE);
  3746. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3747. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3748. }
  3749. /* Ok, everything is just fine now */
  3750. if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
  3751. printk(KERN_WARNING
  3752. "raid5: failed to create sysfs attributes for %s\n",
  3753. mdname(mddev));
  3754. mddev->queue->unplug_fn = raid5_unplug_device;
  3755. mddev->queue->backing_dev_info.congested_data = mddev;
  3756. mddev->queue->backing_dev_info.congested_fn = raid5_congested;
  3757. mddev->array_sectors = 2 * mddev->size * (conf->previous_raid_disks -
  3758. conf->max_degraded);
  3759. blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
  3760. return 0;
  3761. abort:
  3762. if (conf) {
  3763. print_raid5_conf(conf);
  3764. safe_put_page(conf->spare_page);
  3765. kfree(conf->disks);
  3766. kfree(conf->stripe_hashtbl);
  3767. kfree(conf);
  3768. }
  3769. mddev->private = NULL;
  3770. printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
  3771. return -EIO;
  3772. }
  3773. static int stop(mddev_t *mddev)
  3774. {
  3775. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3776. md_unregister_thread(mddev->thread);
  3777. mddev->thread = NULL;
  3778. shrink_stripes(conf);
  3779. kfree(conf->stripe_hashtbl);
  3780. mddev->queue->backing_dev_info.congested_fn = NULL;
  3781. blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
  3782. sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
  3783. kfree(conf->disks);
  3784. kfree(conf);
  3785. mddev->private = NULL;
  3786. return 0;
  3787. }
  3788. #ifdef DEBUG
  3789. static void print_sh (struct seq_file *seq, struct stripe_head *sh)
  3790. {
  3791. int i;
  3792. seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
  3793. (unsigned long long)sh->sector, sh->pd_idx, sh->state);
  3794. seq_printf(seq, "sh %llu, count %d.\n",
  3795. (unsigned long long)sh->sector, atomic_read(&sh->count));
  3796. seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
  3797. for (i = 0; i < sh->disks; i++) {
  3798. seq_printf(seq, "(cache%d: %p %ld) ",
  3799. i, sh->dev[i].page, sh->dev[i].flags);
  3800. }
  3801. seq_printf(seq, "\n");
  3802. }
  3803. static void printall (struct seq_file *seq, raid5_conf_t *conf)
  3804. {
  3805. struct stripe_head *sh;
  3806. struct hlist_node *hn;
  3807. int i;
  3808. spin_lock_irq(&conf->device_lock);
  3809. for (i = 0; i < NR_HASH; i++) {
  3810. hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
  3811. if (sh->raid_conf != conf)
  3812. continue;
  3813. print_sh(seq, sh);
  3814. }
  3815. }
  3816. spin_unlock_irq(&conf->device_lock);
  3817. }
  3818. #endif
  3819. static void status (struct seq_file *seq, mddev_t *mddev)
  3820. {
  3821. raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
  3822. int i;
  3823. seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
  3824. seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
  3825. for (i = 0; i < conf->raid_disks; i++)
  3826. seq_printf (seq, "%s",
  3827. conf->disks[i].rdev &&
  3828. test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
  3829. seq_printf (seq, "]");
  3830. #ifdef DEBUG
  3831. seq_printf (seq, "\n");
  3832. printall(seq, conf);
  3833. #endif
  3834. }
  3835. static void print_raid5_conf (raid5_conf_t *conf)
  3836. {
  3837. int i;
  3838. struct disk_info *tmp;
  3839. printk("RAID5 conf printout:\n");
  3840. if (!conf) {
  3841. printk("(conf==NULL)\n");
  3842. return;
  3843. }
  3844. printk(" --- rd:%d wd:%d\n", conf->raid_disks,
  3845. conf->raid_disks - conf->mddev->degraded);
  3846. for (i = 0; i < conf->raid_disks; i++) {
  3847. char b[BDEVNAME_SIZE];
  3848. tmp = conf->disks + i;
  3849. if (tmp->rdev)
  3850. printk(" disk %d, o:%d, dev:%s\n",
  3851. i, !test_bit(Faulty, &tmp->rdev->flags),
  3852. bdevname(tmp->rdev->bdev,b));
  3853. }
  3854. }
  3855. static int raid5_spare_active(mddev_t *mddev)
  3856. {
  3857. int i;
  3858. raid5_conf_t *conf = mddev->private;
  3859. struct disk_info *tmp;
  3860. for (i = 0; i < conf->raid_disks; i++) {
  3861. tmp = conf->disks + i;
  3862. if (tmp->rdev
  3863. && !test_bit(Faulty, &tmp->rdev->flags)
  3864. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  3865. unsigned long flags;
  3866. spin_lock_irqsave(&conf->device_lock, flags);
  3867. mddev->degraded--;
  3868. spin_unlock_irqrestore(&conf->device_lock, flags);
  3869. }
  3870. }
  3871. print_raid5_conf(conf);
  3872. return 0;
  3873. }
  3874. static int raid5_remove_disk(mddev_t *mddev, int number)
  3875. {
  3876. raid5_conf_t *conf = mddev->private;
  3877. int err = 0;
  3878. mdk_rdev_t *rdev;
  3879. struct disk_info *p = conf->disks + number;
  3880. print_raid5_conf(conf);
  3881. rdev = p->rdev;
  3882. if (rdev) {
  3883. if (test_bit(In_sync, &rdev->flags) ||
  3884. atomic_read(&rdev->nr_pending)) {
  3885. err = -EBUSY;
  3886. goto abort;
  3887. }
  3888. /* Only remove non-faulty devices if recovery
  3889. * isn't possible.
  3890. */
  3891. if (!test_bit(Faulty, &rdev->flags) &&
  3892. mddev->degraded <= conf->max_degraded) {
  3893. err = -EBUSY;
  3894. goto abort;
  3895. }
  3896. p->rdev = NULL;
  3897. synchronize_rcu();
  3898. if (atomic_read(&rdev->nr_pending)) {
  3899. /* lost the race, try later */
  3900. err = -EBUSY;
  3901. p->rdev = rdev;
  3902. }
  3903. }
  3904. abort:
  3905. print_raid5_conf(conf);
  3906. return err;
  3907. }
  3908. static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
  3909. {
  3910. raid5_conf_t *conf = mddev->private;
  3911. int err = -EEXIST;
  3912. int disk;
  3913. struct disk_info *p;
  3914. int first = 0;
  3915. int last = conf->raid_disks - 1;
  3916. if (mddev->degraded > conf->max_degraded)
  3917. /* no point adding a device */
  3918. return -EINVAL;
  3919. if (rdev->raid_disk >= 0)
  3920. first = last = rdev->raid_disk;
  3921. /*
  3922. * find the disk ... but prefer rdev->saved_raid_disk
  3923. * if possible.
  3924. */
  3925. if (rdev->saved_raid_disk >= 0 &&
  3926. rdev->saved_raid_disk >= first &&
  3927. conf->disks[rdev->saved_raid_disk].rdev == NULL)
  3928. disk = rdev->saved_raid_disk;
  3929. else
  3930. disk = first;
  3931. for ( ; disk <= last ; disk++)
  3932. if ((p=conf->disks + disk)->rdev == NULL) {
  3933. clear_bit(In_sync, &rdev->flags);
  3934. rdev->raid_disk = disk;
  3935. err = 0;
  3936. if (rdev->saved_raid_disk != disk)
  3937. conf->fullsync = 1;
  3938. rcu_assign_pointer(p->rdev, rdev);
  3939. break;
  3940. }
  3941. print_raid5_conf(conf);
  3942. return err;
  3943. }
  3944. static int raid5_resize(mddev_t *mddev, sector_t sectors)
  3945. {
  3946. /* no resync is happening, and there is enough space
  3947. * on all devices, so we can resize.
  3948. * We need to make sure resync covers any new space.
  3949. * If the array is shrinking we should possibly wait until
  3950. * any io in the removed space completes, but it hardly seems
  3951. * worth it.
  3952. */
  3953. raid5_conf_t *conf = mddev_to_conf(mddev);
  3954. sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
  3955. mddev->array_sectors = sectors * (mddev->raid_disks
  3956. - conf->max_degraded);
  3957. set_capacity(mddev->gendisk, mddev->array_sectors);
  3958. mddev->changed = 1;
  3959. if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
  3960. mddev->recovery_cp = mddev->size << 1;
  3961. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3962. }
  3963. mddev->size = sectors /2;
  3964. mddev->resync_max_sectors = sectors;
  3965. return 0;
  3966. }
  3967. #ifdef CONFIG_MD_RAID5_RESHAPE
  3968. static int raid5_check_reshape(mddev_t *mddev)
  3969. {
  3970. raid5_conf_t *conf = mddev_to_conf(mddev);
  3971. int err;
  3972. if (mddev->delta_disks < 0 ||
  3973. mddev->new_level != mddev->level)
  3974. return -EINVAL; /* Cannot shrink array or change level yet */
  3975. if (mddev->delta_disks == 0)
  3976. return 0; /* nothing to do */
  3977. if (mddev->bitmap)
  3978. /* Cannot grow a bitmap yet */
  3979. return -EBUSY;
  3980. /* Can only proceed if there are plenty of stripe_heads.
  3981. * We need a minimum of one full stripe,, and for sensible progress
  3982. * it is best to have about 4 times that.
  3983. * If we require 4 times, then the default 256 4K stripe_heads will
  3984. * allow for chunk sizes up to 256K, which is probably OK.
  3985. * If the chunk size is greater, user-space should request more
  3986. * stripe_heads first.
  3987. */
  3988. if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
  3989. (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
  3990. printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
  3991. (mddev->chunk_size / STRIPE_SIZE)*4);
  3992. return -ENOSPC;
  3993. }
  3994. err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
  3995. if (err)
  3996. return err;
  3997. if (mddev->degraded > conf->max_degraded)
  3998. return -EINVAL;
  3999. /* looks like we might be able to manage this */
  4000. return 0;
  4001. }
  4002. static int raid5_start_reshape(mddev_t *mddev)
  4003. {
  4004. raid5_conf_t *conf = mddev_to_conf(mddev);
  4005. mdk_rdev_t *rdev;
  4006. struct list_head *rtmp;
  4007. int spares = 0;
  4008. int added_devices = 0;
  4009. unsigned long flags;
  4010. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  4011. return -EBUSY;
  4012. rdev_for_each(rdev, rtmp, mddev)
  4013. if (rdev->raid_disk < 0 &&
  4014. !test_bit(Faulty, &rdev->flags))
  4015. spares++;
  4016. if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
  4017. /* Not enough devices even to make a degraded array
  4018. * of that size
  4019. */
  4020. return -EINVAL;
  4021. atomic_set(&conf->reshape_stripes, 0);
  4022. spin_lock_irq(&conf->device_lock);
  4023. conf->previous_raid_disks = conf->raid_disks;
  4024. conf->raid_disks += mddev->delta_disks;
  4025. conf->expand_progress = 0;
  4026. conf->expand_lo = 0;
  4027. spin_unlock_irq(&conf->device_lock);
  4028. /* Add some new drives, as many as will fit.
  4029. * We know there are enough to make the newly sized array work.
  4030. */
  4031. rdev_for_each(rdev, rtmp, mddev)
  4032. if (rdev->raid_disk < 0 &&
  4033. !test_bit(Faulty, &rdev->flags)) {
  4034. if (raid5_add_disk(mddev, rdev) == 0) {
  4035. char nm[20];
  4036. set_bit(In_sync, &rdev->flags);
  4037. added_devices++;
  4038. rdev->recovery_offset = 0;
  4039. sprintf(nm, "rd%d", rdev->raid_disk);
  4040. if (sysfs_create_link(&mddev->kobj,
  4041. &rdev->kobj, nm))
  4042. printk(KERN_WARNING
  4043. "raid5: failed to create "
  4044. " link %s for %s\n",
  4045. nm, mdname(mddev));
  4046. } else
  4047. break;
  4048. }
  4049. spin_lock_irqsave(&conf->device_lock, flags);
  4050. mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
  4051. spin_unlock_irqrestore(&conf->device_lock, flags);
  4052. mddev->raid_disks = conf->raid_disks;
  4053. mddev->reshape_position = 0;
  4054. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  4055. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  4056. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  4057. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  4058. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  4059. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  4060. "%s_reshape");
  4061. if (!mddev->sync_thread) {
  4062. mddev->recovery = 0;
  4063. spin_lock_irq(&conf->device_lock);
  4064. mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
  4065. conf->expand_progress = MaxSector;
  4066. spin_unlock_irq(&conf->device_lock);
  4067. return -EAGAIN;
  4068. }
  4069. md_wakeup_thread(mddev->sync_thread);
  4070. md_new_event(mddev);
  4071. return 0;
  4072. }
  4073. #endif
  4074. static void end_reshape(raid5_conf_t *conf)
  4075. {
  4076. struct block_device *bdev;
  4077. if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
  4078. conf->mddev->array_sectors = 2 * conf->mddev->size *
  4079. (conf->raid_disks - conf->max_degraded);
  4080. set_capacity(conf->mddev->gendisk, conf->mddev->array_sectors);
  4081. conf->mddev->changed = 1;
  4082. bdev = bdget_disk(conf->mddev->gendisk, 0);
  4083. if (bdev) {
  4084. mutex_lock(&bdev->bd_inode->i_mutex);
  4085. i_size_write(bdev->bd_inode,
  4086. (loff_t)conf->mddev->array_sectors << 9);
  4087. mutex_unlock(&bdev->bd_inode->i_mutex);
  4088. bdput(bdev);
  4089. }
  4090. spin_lock_irq(&conf->device_lock);
  4091. conf->expand_progress = MaxSector;
  4092. spin_unlock_irq(&conf->device_lock);
  4093. conf->mddev->reshape_position = MaxSector;
  4094. /* read-ahead size must cover two whole stripes, which is
  4095. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4096. */
  4097. {
  4098. int data_disks = conf->previous_raid_disks - conf->max_degraded;
  4099. int stripe = data_disks *
  4100. (conf->mddev->chunk_size / PAGE_SIZE);
  4101. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4102. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4103. }
  4104. }
  4105. }
  4106. static void raid5_quiesce(mddev_t *mddev, int state)
  4107. {
  4108. raid5_conf_t *conf = mddev_to_conf(mddev);
  4109. switch(state) {
  4110. case 2: /* resume for a suspend */
  4111. wake_up(&conf->wait_for_overlap);
  4112. break;
  4113. case 1: /* stop all writes */
  4114. spin_lock_irq(&conf->device_lock);
  4115. conf->quiesce = 1;
  4116. wait_event_lock_irq(conf->wait_for_stripe,
  4117. atomic_read(&conf->active_stripes) == 0 &&
  4118. atomic_read(&conf->active_aligned_reads) == 0,
  4119. conf->device_lock, /* nothing */);
  4120. spin_unlock_irq(&conf->device_lock);
  4121. break;
  4122. case 0: /* re-enable writes */
  4123. spin_lock_irq(&conf->device_lock);
  4124. conf->quiesce = 0;
  4125. wake_up(&conf->wait_for_stripe);
  4126. wake_up(&conf->wait_for_overlap);
  4127. spin_unlock_irq(&conf->device_lock);
  4128. break;
  4129. }
  4130. }
  4131. static struct mdk_personality raid6_personality =
  4132. {
  4133. .name = "raid6",
  4134. .level = 6,
  4135. .owner = THIS_MODULE,
  4136. .make_request = make_request,
  4137. .run = run,
  4138. .stop = stop,
  4139. .status = status,
  4140. .error_handler = error,
  4141. .hot_add_disk = raid5_add_disk,
  4142. .hot_remove_disk= raid5_remove_disk,
  4143. .spare_active = raid5_spare_active,
  4144. .sync_request = sync_request,
  4145. .resize = raid5_resize,
  4146. #ifdef CONFIG_MD_RAID5_RESHAPE
  4147. .check_reshape = raid5_check_reshape,
  4148. .start_reshape = raid5_start_reshape,
  4149. #endif
  4150. .quiesce = raid5_quiesce,
  4151. };
  4152. static struct mdk_personality raid5_personality =
  4153. {
  4154. .name = "raid5",
  4155. .level = 5,
  4156. .owner = THIS_MODULE,
  4157. .make_request = make_request,
  4158. .run = run,
  4159. .stop = stop,
  4160. .status = status,
  4161. .error_handler = error,
  4162. .hot_add_disk = raid5_add_disk,
  4163. .hot_remove_disk= raid5_remove_disk,
  4164. .spare_active = raid5_spare_active,
  4165. .sync_request = sync_request,
  4166. .resize = raid5_resize,
  4167. #ifdef CONFIG_MD_RAID5_RESHAPE
  4168. .check_reshape = raid5_check_reshape,
  4169. .start_reshape = raid5_start_reshape,
  4170. #endif
  4171. .quiesce = raid5_quiesce,
  4172. };
  4173. static struct mdk_personality raid4_personality =
  4174. {
  4175. .name = "raid4",
  4176. .level = 4,
  4177. .owner = THIS_MODULE,
  4178. .make_request = make_request,
  4179. .run = run,
  4180. .stop = stop,
  4181. .status = status,
  4182. .error_handler = error,
  4183. .hot_add_disk = raid5_add_disk,
  4184. .hot_remove_disk= raid5_remove_disk,
  4185. .spare_active = raid5_spare_active,
  4186. .sync_request = sync_request,
  4187. .resize = raid5_resize,
  4188. #ifdef CONFIG_MD_RAID5_RESHAPE
  4189. .check_reshape = raid5_check_reshape,
  4190. .start_reshape = raid5_start_reshape,
  4191. #endif
  4192. .quiesce = raid5_quiesce,
  4193. };
  4194. static int __init raid5_init(void)
  4195. {
  4196. int e;
  4197. e = raid6_select_algo();
  4198. if ( e )
  4199. return e;
  4200. register_md_personality(&raid6_personality);
  4201. register_md_personality(&raid5_personality);
  4202. register_md_personality(&raid4_personality);
  4203. return 0;
  4204. }
  4205. static void raid5_exit(void)
  4206. {
  4207. unregister_md_personality(&raid6_personality);
  4208. unregister_md_personality(&raid5_personality);
  4209. unregister_md_personality(&raid4_personality);
  4210. }
  4211. module_init(raid5_init);
  4212. module_exit(raid5_exit);
  4213. MODULE_LICENSE("GPL");
  4214. MODULE_ALIAS("md-personality-4"); /* RAID5 */
  4215. MODULE_ALIAS("md-raid5");
  4216. MODULE_ALIAS("md-raid4");
  4217. MODULE_ALIAS("md-level-5");
  4218. MODULE_ALIAS("md-level-4");
  4219. MODULE_ALIAS("md-personality-8"); /* RAID6 */
  4220. MODULE_ALIAS("md-raid6");
  4221. MODULE_ALIAS("md-level-6");
  4222. /* This used to be two separate modules, they were: */
  4223. MODULE_ALIAS("raid5");
  4224. MODULE_ALIAS("raid6");