raid5.c 180 KB

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