raid5.c 133 KB

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