raid5.c 180 KB

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