raid5.c 200 KB

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