raid5.c 156 KB

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