raid5.c 131 KB

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