raid5.c 182 KB

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