raid5.c 161 KB

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