raid5.c 181 KB

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