raid5.c 138 KB

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