raid5.c 167 KB

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