raid5.c 182 KB

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