raid5.c 164 KB

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