raid5.c 138 KB

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