raid5.c 168 KB

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