raid5.c 167 KB

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