raid5.c 133 KB

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