raid5.c 134 KB

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