raid5.c 162 KB

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