raid5.c 149 KB

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