raid5.c 196 KB

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