raid5.c 180 KB

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