raid5.c 146 KB

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