raid5.c 137 KB

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