raid5.c 168 KB

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