raid5.c 142 KB

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