raid10.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759
  1. /*
  2. * raid10.c : Multiple Devices driver for Linux
  3. *
  4. * Copyright (C) 2000-2004 Neil Brown
  5. *
  6. * RAID-10 support for md.
  7. *
  8. * Base on code in raid1.c. See raid1.c for further copyright information.
  9. *
  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. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/module.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/ratelimit.h>
  26. #include <linux/kthread.h>
  27. #include "md.h"
  28. #include "raid10.h"
  29. #include "raid0.h"
  30. #include "bitmap.h"
  31. /*
  32. * RAID10 provides a combination of RAID0 and RAID1 functionality.
  33. * The layout of data is defined by
  34. * chunk_size
  35. * raid_disks
  36. * near_copies (stored in low byte of layout)
  37. * far_copies (stored in second byte of layout)
  38. * far_offset (stored in bit 16 of layout )
  39. * use_far_sets (stored in bit 17 of layout )
  40. *
  41. * The data to be stored is divided into chunks using chunksize. Each device
  42. * is divided into far_copies sections. In each section, chunks are laid out
  43. * in a style similar to raid0, but near_copies copies of each chunk is stored
  44. * (each on a different drive). The starting device for each section is offset
  45. * near_copies from the starting device of the previous section. Thus there
  46. * are (near_copies * far_copies) of each chunk, and each is on a different
  47. * drive. near_copies and far_copies must be at least one, and their product
  48. * is at most raid_disks.
  49. *
  50. * If far_offset is true, then the far_copies are handled a bit differently.
  51. * The copies are still in different stripes, but instead of being very far
  52. * apart on disk, there are adjacent stripes.
  53. *
  54. * The far and offset algorithms are handled slightly differently if
  55. * 'use_far_sets' is true. In this case, the array's devices are grouped into
  56. * sets that are (near_copies * far_copies) in size. The far copied stripes
  57. * are still shifted by 'near_copies' devices, but this shifting stays confined
  58. * to the set rather than the entire array. This is done to improve the number
  59. * of device combinations that can fail without causing the array to fail.
  60. * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
  61. * on a device):
  62. * A B C D A B C D E
  63. * ... ...
  64. * D A B C E A B C D
  65. * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
  66. * [A B] [C D] [A B] [C D E]
  67. * |...| |...| |...| | ... |
  68. * [B A] [D C] [B A] [E C D]
  69. */
  70. /*
  71. * Number of guaranteed r10bios in case of extreme VM load:
  72. */
  73. #define NR_RAID10_BIOS 256
  74. /* when we get a read error on a read-only array, we redirect to another
  75. * device without failing the first device, or trying to over-write to
  76. * correct the read error. To keep track of bad blocks on a per-bio
  77. * level, we store IO_BLOCKED in the appropriate 'bios' pointer
  78. */
  79. #define IO_BLOCKED ((struct bio *)1)
  80. /* When we successfully write to a known bad-block, we need to remove the
  81. * bad-block marking which must be done from process context. So we record
  82. * the success by setting devs[n].bio to IO_MADE_GOOD
  83. */
  84. #define IO_MADE_GOOD ((struct bio *)2)
  85. #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
  86. /* When there are this many requests queued to be written by
  87. * the raid10 thread, we become 'congested' to provide back-pressure
  88. * for writeback.
  89. */
  90. static int max_queued_requests = 1024;
  91. static void allow_barrier(struct r10conf *conf);
  92. static void lower_barrier(struct r10conf *conf);
  93. static int _enough(struct r10conf *conf, int previous, int ignore);
  94. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
  95. int *skipped);
  96. static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
  97. static void end_reshape_write(struct bio *bio, int error);
  98. static void end_reshape(struct r10conf *conf);
  99. static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
  100. {
  101. struct r10conf *conf = data;
  102. int size = offsetof(struct r10bio, devs[conf->copies]);
  103. /* allocate a r10bio with room for raid_disks entries in the
  104. * bios array */
  105. return kzalloc(size, gfp_flags);
  106. }
  107. static void r10bio_pool_free(void *r10_bio, void *data)
  108. {
  109. kfree(r10_bio);
  110. }
  111. /* Maximum size of each resync request */
  112. #define RESYNC_BLOCK_SIZE (64*1024)
  113. #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
  114. /* amount of memory to reserve for resync requests */
  115. #define RESYNC_WINDOW (1024*1024)
  116. /* maximum number of concurrent requests, memory permitting */
  117. #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
  118. /*
  119. * When performing a resync, we need to read and compare, so
  120. * we need as many pages are there are copies.
  121. * When performing a recovery, we need 2 bios, one for read,
  122. * one for write (we recover only one drive per r10buf)
  123. *
  124. */
  125. static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
  126. {
  127. struct r10conf *conf = data;
  128. struct page *page;
  129. struct r10bio *r10_bio;
  130. struct bio *bio;
  131. int i, j;
  132. int nalloc;
  133. r10_bio = r10bio_pool_alloc(gfp_flags, conf);
  134. if (!r10_bio)
  135. return NULL;
  136. if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
  137. test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
  138. nalloc = conf->copies; /* resync */
  139. else
  140. nalloc = 2; /* recovery */
  141. /*
  142. * Allocate bios.
  143. */
  144. for (j = nalloc ; j-- ; ) {
  145. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  146. if (!bio)
  147. goto out_free_bio;
  148. r10_bio->devs[j].bio = bio;
  149. if (!conf->have_replacement)
  150. continue;
  151. bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
  152. if (!bio)
  153. goto out_free_bio;
  154. r10_bio->devs[j].repl_bio = bio;
  155. }
  156. /*
  157. * Allocate RESYNC_PAGES data pages and attach them
  158. * where needed.
  159. */
  160. for (j = 0 ; j < nalloc; j++) {
  161. struct bio *rbio = r10_bio->devs[j].repl_bio;
  162. bio = r10_bio->devs[j].bio;
  163. for (i = 0; i < RESYNC_PAGES; i++) {
  164. if (j > 0 && !test_bit(MD_RECOVERY_SYNC,
  165. &conf->mddev->recovery)) {
  166. /* we can share bv_page's during recovery
  167. * and reshape */
  168. struct bio *rbio = r10_bio->devs[0].bio;
  169. page = rbio->bi_io_vec[i].bv_page;
  170. get_page(page);
  171. } else
  172. page = alloc_page(gfp_flags);
  173. if (unlikely(!page))
  174. goto out_free_pages;
  175. bio->bi_io_vec[i].bv_page = page;
  176. if (rbio)
  177. rbio->bi_io_vec[i].bv_page = page;
  178. }
  179. }
  180. return r10_bio;
  181. out_free_pages:
  182. for ( ; i > 0 ; i--)
  183. safe_put_page(bio->bi_io_vec[i-1].bv_page);
  184. while (j--)
  185. for (i = 0; i < RESYNC_PAGES ; i++)
  186. safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
  187. j = 0;
  188. out_free_bio:
  189. for ( ; j < nalloc; j++) {
  190. if (r10_bio->devs[j].bio)
  191. bio_put(r10_bio->devs[j].bio);
  192. if (r10_bio->devs[j].repl_bio)
  193. bio_put(r10_bio->devs[j].repl_bio);
  194. }
  195. r10bio_pool_free(r10_bio, conf);
  196. return NULL;
  197. }
  198. static void r10buf_pool_free(void *__r10_bio, void *data)
  199. {
  200. int i;
  201. struct r10conf *conf = data;
  202. struct r10bio *r10bio = __r10_bio;
  203. int j;
  204. for (j=0; j < conf->copies; j++) {
  205. struct bio *bio = r10bio->devs[j].bio;
  206. if (bio) {
  207. for (i = 0; i < RESYNC_PAGES; i++) {
  208. safe_put_page(bio->bi_io_vec[i].bv_page);
  209. bio->bi_io_vec[i].bv_page = NULL;
  210. }
  211. bio_put(bio);
  212. }
  213. bio = r10bio->devs[j].repl_bio;
  214. if (bio)
  215. bio_put(bio);
  216. }
  217. r10bio_pool_free(r10bio, conf);
  218. }
  219. static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
  220. {
  221. int i;
  222. for (i = 0; i < conf->copies; i++) {
  223. struct bio **bio = & r10_bio->devs[i].bio;
  224. if (!BIO_SPECIAL(*bio))
  225. bio_put(*bio);
  226. *bio = NULL;
  227. bio = &r10_bio->devs[i].repl_bio;
  228. if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
  229. bio_put(*bio);
  230. *bio = NULL;
  231. }
  232. }
  233. static void free_r10bio(struct r10bio *r10_bio)
  234. {
  235. struct r10conf *conf = r10_bio->mddev->private;
  236. put_all_bios(conf, r10_bio);
  237. mempool_free(r10_bio, conf->r10bio_pool);
  238. }
  239. static void put_buf(struct r10bio *r10_bio)
  240. {
  241. struct r10conf *conf = r10_bio->mddev->private;
  242. mempool_free(r10_bio, conf->r10buf_pool);
  243. lower_barrier(conf);
  244. }
  245. static void reschedule_retry(struct r10bio *r10_bio)
  246. {
  247. unsigned long flags;
  248. struct mddev *mddev = r10_bio->mddev;
  249. struct r10conf *conf = mddev->private;
  250. spin_lock_irqsave(&conf->device_lock, flags);
  251. list_add(&r10_bio->retry_list, &conf->retry_list);
  252. conf->nr_queued ++;
  253. spin_unlock_irqrestore(&conf->device_lock, flags);
  254. /* wake up frozen array... */
  255. wake_up(&conf->wait_barrier);
  256. md_wakeup_thread(mddev->thread);
  257. }
  258. /*
  259. * raid_end_bio_io() is called when we have finished servicing a mirrored
  260. * operation and are ready to return a success/failure code to the buffer
  261. * cache layer.
  262. */
  263. static void raid_end_bio_io(struct r10bio *r10_bio)
  264. {
  265. struct bio *bio = r10_bio->master_bio;
  266. int done;
  267. struct r10conf *conf = r10_bio->mddev->private;
  268. if (bio->bi_phys_segments) {
  269. unsigned long flags;
  270. spin_lock_irqsave(&conf->device_lock, flags);
  271. bio->bi_phys_segments--;
  272. done = (bio->bi_phys_segments == 0);
  273. spin_unlock_irqrestore(&conf->device_lock, flags);
  274. } else
  275. done = 1;
  276. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  277. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  278. if (done) {
  279. bio_endio(bio, 0);
  280. /*
  281. * Wake up any possible resync thread that waits for the device
  282. * to go idle.
  283. */
  284. allow_barrier(conf);
  285. }
  286. free_r10bio(r10_bio);
  287. }
  288. /*
  289. * Update disk head position estimator based on IRQ completion info.
  290. */
  291. static inline void update_head_pos(int slot, struct r10bio *r10_bio)
  292. {
  293. struct r10conf *conf = r10_bio->mddev->private;
  294. conf->mirrors[r10_bio->devs[slot].devnum].head_position =
  295. r10_bio->devs[slot].addr + (r10_bio->sectors);
  296. }
  297. /*
  298. * Find the disk number which triggered given bio
  299. */
  300. static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
  301. struct bio *bio, int *slotp, int *replp)
  302. {
  303. int slot;
  304. int repl = 0;
  305. for (slot = 0; slot < conf->copies; slot++) {
  306. if (r10_bio->devs[slot].bio == bio)
  307. break;
  308. if (r10_bio->devs[slot].repl_bio == bio) {
  309. repl = 1;
  310. break;
  311. }
  312. }
  313. BUG_ON(slot == conf->copies);
  314. update_head_pos(slot, r10_bio);
  315. if (slotp)
  316. *slotp = slot;
  317. if (replp)
  318. *replp = repl;
  319. return r10_bio->devs[slot].devnum;
  320. }
  321. static void raid10_end_read_request(struct bio *bio, int error)
  322. {
  323. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  324. struct r10bio *r10_bio = bio->bi_private;
  325. int slot, dev;
  326. struct md_rdev *rdev;
  327. struct r10conf *conf = r10_bio->mddev->private;
  328. slot = r10_bio->read_slot;
  329. dev = r10_bio->devs[slot].devnum;
  330. rdev = r10_bio->devs[slot].rdev;
  331. /*
  332. * this branch is our 'one mirror IO has finished' event handler:
  333. */
  334. update_head_pos(slot, r10_bio);
  335. if (uptodate) {
  336. /*
  337. * Set R10BIO_Uptodate in our master bio, so that
  338. * we will return a good error code to the higher
  339. * levels even if IO on some other mirrored buffer fails.
  340. *
  341. * The 'master' represents the composite IO operation to
  342. * user-side. So if something waits for IO, then it will
  343. * wait for the 'master' bio.
  344. */
  345. set_bit(R10BIO_Uptodate, &r10_bio->state);
  346. } else {
  347. /* If all other devices that store this block have
  348. * failed, we want to return the error upwards rather
  349. * than fail the last device. Here we redefine
  350. * "uptodate" to mean "Don't want to retry"
  351. */
  352. if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
  353. rdev->raid_disk))
  354. uptodate = 1;
  355. }
  356. if (uptodate) {
  357. raid_end_bio_io(r10_bio);
  358. rdev_dec_pending(rdev, conf->mddev);
  359. } else {
  360. /*
  361. * oops, read error - keep the refcount on the rdev
  362. */
  363. char b[BDEVNAME_SIZE];
  364. printk_ratelimited(KERN_ERR
  365. "md/raid10:%s: %s: rescheduling sector %llu\n",
  366. mdname(conf->mddev),
  367. bdevname(rdev->bdev, b),
  368. (unsigned long long)r10_bio->sector);
  369. set_bit(R10BIO_ReadError, &r10_bio->state);
  370. reschedule_retry(r10_bio);
  371. }
  372. }
  373. static void close_write(struct r10bio *r10_bio)
  374. {
  375. /* clear the bitmap if all writes complete successfully */
  376. bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
  377. r10_bio->sectors,
  378. !test_bit(R10BIO_Degraded, &r10_bio->state),
  379. 0);
  380. md_write_end(r10_bio->mddev);
  381. }
  382. static void one_write_done(struct r10bio *r10_bio)
  383. {
  384. if (atomic_dec_and_test(&r10_bio->remaining)) {
  385. if (test_bit(R10BIO_WriteError, &r10_bio->state))
  386. reschedule_retry(r10_bio);
  387. else {
  388. close_write(r10_bio);
  389. if (test_bit(R10BIO_MadeGood, &r10_bio->state))
  390. reschedule_retry(r10_bio);
  391. else
  392. raid_end_bio_io(r10_bio);
  393. }
  394. }
  395. }
  396. static void raid10_end_write_request(struct bio *bio, int error)
  397. {
  398. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  399. struct r10bio *r10_bio = bio->bi_private;
  400. int dev;
  401. int dec_rdev = 1;
  402. struct r10conf *conf = r10_bio->mddev->private;
  403. int slot, repl;
  404. struct md_rdev *rdev = NULL;
  405. dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  406. if (repl)
  407. rdev = conf->mirrors[dev].replacement;
  408. if (!rdev) {
  409. smp_rmb();
  410. repl = 0;
  411. rdev = conf->mirrors[dev].rdev;
  412. }
  413. /*
  414. * this branch is our 'one mirror IO has finished' event handler:
  415. */
  416. if (!uptodate) {
  417. if (repl)
  418. /* Never record new bad blocks to replacement,
  419. * just fail it.
  420. */
  421. md_error(rdev->mddev, rdev);
  422. else {
  423. set_bit(WriteErrorSeen, &rdev->flags);
  424. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  425. set_bit(MD_RECOVERY_NEEDED,
  426. &rdev->mddev->recovery);
  427. set_bit(R10BIO_WriteError, &r10_bio->state);
  428. dec_rdev = 0;
  429. }
  430. } else {
  431. /*
  432. * Set R10BIO_Uptodate in our master bio, so that
  433. * we will return a good error code for to the higher
  434. * levels even if IO on some other mirrored buffer fails.
  435. *
  436. * The 'master' represents the composite IO operation to
  437. * user-side. So if something waits for IO, then it will
  438. * wait for the 'master' bio.
  439. */
  440. sector_t first_bad;
  441. int bad_sectors;
  442. /*
  443. * Do not set R10BIO_Uptodate if the current device is
  444. * rebuilding or Faulty. This is because we cannot use
  445. * such device for properly reading the data back (we could
  446. * potentially use it, if the current write would have felt
  447. * before rdev->recovery_offset, but for simplicity we don't
  448. * check this here.
  449. */
  450. if (test_bit(In_sync, &rdev->flags) &&
  451. !test_bit(Faulty, &rdev->flags))
  452. set_bit(R10BIO_Uptodate, &r10_bio->state);
  453. /* Maybe we can clear some bad blocks. */
  454. if (is_badblock(rdev,
  455. r10_bio->devs[slot].addr,
  456. r10_bio->sectors,
  457. &first_bad, &bad_sectors)) {
  458. bio_put(bio);
  459. if (repl)
  460. r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
  461. else
  462. r10_bio->devs[slot].bio = IO_MADE_GOOD;
  463. dec_rdev = 0;
  464. set_bit(R10BIO_MadeGood, &r10_bio->state);
  465. }
  466. }
  467. /*
  468. *
  469. * Let's see if all mirrored write operations have finished
  470. * already.
  471. */
  472. one_write_done(r10_bio);
  473. if (dec_rdev)
  474. rdev_dec_pending(rdev, conf->mddev);
  475. }
  476. /*
  477. * RAID10 layout manager
  478. * As well as the chunksize and raid_disks count, there are two
  479. * parameters: near_copies and far_copies.
  480. * near_copies * far_copies must be <= raid_disks.
  481. * Normally one of these will be 1.
  482. * If both are 1, we get raid0.
  483. * If near_copies == raid_disks, we get raid1.
  484. *
  485. * Chunks are laid out in raid0 style with near_copies copies of the
  486. * first chunk, followed by near_copies copies of the next chunk and
  487. * so on.
  488. * If far_copies > 1, then after 1/far_copies of the array has been assigned
  489. * as described above, we start again with a device offset of near_copies.
  490. * So we effectively have another copy of the whole array further down all
  491. * the drives, but with blocks on different drives.
  492. * With this layout, and block is never stored twice on the one device.
  493. *
  494. * raid10_find_phys finds the sector offset of a given virtual sector
  495. * on each device that it is on.
  496. *
  497. * raid10_find_virt does the reverse mapping, from a device and a
  498. * sector offset to a virtual address
  499. */
  500. static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
  501. {
  502. int n,f;
  503. sector_t sector;
  504. sector_t chunk;
  505. sector_t stripe;
  506. int dev;
  507. int slot = 0;
  508. int last_far_set_start, last_far_set_size;
  509. last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
  510. last_far_set_start *= geo->far_set_size;
  511. last_far_set_size = geo->far_set_size;
  512. last_far_set_size += (geo->raid_disks % geo->far_set_size);
  513. /* now calculate first sector/dev */
  514. chunk = r10bio->sector >> geo->chunk_shift;
  515. sector = r10bio->sector & geo->chunk_mask;
  516. chunk *= geo->near_copies;
  517. stripe = chunk;
  518. dev = sector_div(stripe, geo->raid_disks);
  519. if (geo->far_offset)
  520. stripe *= geo->far_copies;
  521. sector += stripe << geo->chunk_shift;
  522. /* and calculate all the others */
  523. for (n = 0; n < geo->near_copies; n++) {
  524. int d = dev;
  525. int set;
  526. sector_t s = sector;
  527. r10bio->devs[slot].devnum = d;
  528. r10bio->devs[slot].addr = s;
  529. slot++;
  530. for (f = 1; f < geo->far_copies; f++) {
  531. set = d / geo->far_set_size;
  532. d += geo->near_copies;
  533. if ((geo->raid_disks % geo->far_set_size) &&
  534. (d > last_far_set_start)) {
  535. d -= last_far_set_start;
  536. d %= last_far_set_size;
  537. d += last_far_set_start;
  538. } else {
  539. d %= geo->far_set_size;
  540. d += geo->far_set_size * set;
  541. }
  542. s += geo->stride;
  543. r10bio->devs[slot].devnum = d;
  544. r10bio->devs[slot].addr = s;
  545. slot++;
  546. }
  547. dev++;
  548. if (dev >= geo->raid_disks) {
  549. dev = 0;
  550. sector += (geo->chunk_mask + 1);
  551. }
  552. }
  553. }
  554. static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
  555. {
  556. struct geom *geo = &conf->geo;
  557. if (conf->reshape_progress != MaxSector &&
  558. ((r10bio->sector >= conf->reshape_progress) !=
  559. conf->mddev->reshape_backwards)) {
  560. set_bit(R10BIO_Previous, &r10bio->state);
  561. geo = &conf->prev;
  562. } else
  563. clear_bit(R10BIO_Previous, &r10bio->state);
  564. __raid10_find_phys(geo, r10bio);
  565. }
  566. static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
  567. {
  568. sector_t offset, chunk, vchunk;
  569. /* Never use conf->prev as this is only called during resync
  570. * or recovery, so reshape isn't happening
  571. */
  572. struct geom *geo = &conf->geo;
  573. int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
  574. int far_set_size = geo->far_set_size;
  575. int last_far_set_start;
  576. if (geo->raid_disks % geo->far_set_size) {
  577. last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
  578. last_far_set_start *= geo->far_set_size;
  579. if (dev >= last_far_set_start) {
  580. far_set_size = geo->far_set_size;
  581. far_set_size += (geo->raid_disks % geo->far_set_size);
  582. far_set_start = last_far_set_start;
  583. }
  584. }
  585. offset = sector & geo->chunk_mask;
  586. if (geo->far_offset) {
  587. int fc;
  588. chunk = sector >> geo->chunk_shift;
  589. fc = sector_div(chunk, geo->far_copies);
  590. dev -= fc * geo->near_copies;
  591. if (dev < far_set_start)
  592. dev += far_set_size;
  593. } else {
  594. while (sector >= geo->stride) {
  595. sector -= geo->stride;
  596. if (dev < (geo->near_copies + far_set_start))
  597. dev += far_set_size - geo->near_copies;
  598. else
  599. dev -= geo->near_copies;
  600. }
  601. chunk = sector >> geo->chunk_shift;
  602. }
  603. vchunk = chunk * geo->raid_disks + dev;
  604. sector_div(vchunk, geo->near_copies);
  605. return (vchunk << geo->chunk_shift) + offset;
  606. }
  607. /**
  608. * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged
  609. * @q: request queue
  610. * @bvm: properties of new bio
  611. * @biovec: the request that could be merged to it.
  612. *
  613. * Return amount of bytes we can accept at this offset
  614. * This requires checking for end-of-chunk if near_copies != raid_disks,
  615. * and for subordinate merge_bvec_fns if merge_check_needed.
  616. */
  617. static int raid10_mergeable_bvec(struct request_queue *q,
  618. struct bvec_merge_data *bvm,
  619. struct bio_vec *biovec)
  620. {
  621. struct mddev *mddev = q->queuedata;
  622. struct r10conf *conf = mddev->private;
  623. sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
  624. int max;
  625. unsigned int chunk_sectors;
  626. unsigned int bio_sectors = bvm->bi_size >> 9;
  627. struct geom *geo = &conf->geo;
  628. chunk_sectors = (conf->geo.chunk_mask & conf->prev.chunk_mask) + 1;
  629. if (conf->reshape_progress != MaxSector &&
  630. ((sector >= conf->reshape_progress) !=
  631. conf->mddev->reshape_backwards))
  632. geo = &conf->prev;
  633. if (geo->near_copies < geo->raid_disks) {
  634. max = (chunk_sectors - ((sector & (chunk_sectors - 1))
  635. + bio_sectors)) << 9;
  636. if (max < 0)
  637. /* bio_add cannot handle a negative return */
  638. max = 0;
  639. if (max <= biovec->bv_len && bio_sectors == 0)
  640. return biovec->bv_len;
  641. } else
  642. max = biovec->bv_len;
  643. if (mddev->merge_check_needed) {
  644. struct {
  645. struct r10bio r10_bio;
  646. struct r10dev devs[conf->copies];
  647. } on_stack;
  648. struct r10bio *r10_bio = &on_stack.r10_bio;
  649. int s;
  650. if (conf->reshape_progress != MaxSector) {
  651. /* Cannot give any guidance during reshape */
  652. if (max <= biovec->bv_len && bio_sectors == 0)
  653. return biovec->bv_len;
  654. return 0;
  655. }
  656. r10_bio->sector = sector;
  657. raid10_find_phys(conf, r10_bio);
  658. rcu_read_lock();
  659. for (s = 0; s < conf->copies; s++) {
  660. int disk = r10_bio->devs[s].devnum;
  661. struct md_rdev *rdev = rcu_dereference(
  662. conf->mirrors[disk].rdev);
  663. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  664. struct request_queue *q =
  665. bdev_get_queue(rdev->bdev);
  666. if (q->merge_bvec_fn) {
  667. bvm->bi_sector = r10_bio->devs[s].addr
  668. + rdev->data_offset;
  669. bvm->bi_bdev = rdev->bdev;
  670. max = min(max, q->merge_bvec_fn(
  671. q, bvm, biovec));
  672. }
  673. }
  674. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  675. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  676. struct request_queue *q =
  677. bdev_get_queue(rdev->bdev);
  678. if (q->merge_bvec_fn) {
  679. bvm->bi_sector = r10_bio->devs[s].addr
  680. + rdev->data_offset;
  681. bvm->bi_bdev = rdev->bdev;
  682. max = min(max, q->merge_bvec_fn(
  683. q, bvm, biovec));
  684. }
  685. }
  686. }
  687. rcu_read_unlock();
  688. }
  689. return max;
  690. }
  691. /*
  692. * This routine returns the disk from which the requested read should
  693. * be done. There is a per-array 'next expected sequential IO' sector
  694. * number - if this matches on the next IO then we use the last disk.
  695. * There is also a per-disk 'last know head position' sector that is
  696. * maintained from IRQ contexts, both the normal and the resync IO
  697. * completion handlers update this position correctly. If there is no
  698. * perfect sequential match then we pick the disk whose head is closest.
  699. *
  700. * If there are 2 mirrors in the same 2 devices, performance degrades
  701. * because position is mirror, not device based.
  702. *
  703. * The rdev for the device selected will have nr_pending incremented.
  704. */
  705. /*
  706. * FIXME: possibly should rethink readbalancing and do it differently
  707. * depending on near_copies / far_copies geometry.
  708. */
  709. static struct md_rdev *read_balance(struct r10conf *conf,
  710. struct r10bio *r10_bio,
  711. int *max_sectors)
  712. {
  713. const sector_t this_sector = r10_bio->sector;
  714. int disk, slot;
  715. int sectors = r10_bio->sectors;
  716. int best_good_sectors;
  717. sector_t new_distance, best_dist;
  718. struct md_rdev *best_rdev, *rdev = NULL;
  719. int do_balance;
  720. int best_slot;
  721. struct geom *geo = &conf->geo;
  722. raid10_find_phys(conf, r10_bio);
  723. rcu_read_lock();
  724. retry:
  725. sectors = r10_bio->sectors;
  726. best_slot = -1;
  727. best_rdev = NULL;
  728. best_dist = MaxSector;
  729. best_good_sectors = 0;
  730. do_balance = 1;
  731. /*
  732. * Check if we can balance. We can balance on the whole
  733. * device if no resync is going on (recovery is ok), or below
  734. * the resync window. We take the first readable disk when
  735. * above the resync window.
  736. */
  737. if (conf->mddev->recovery_cp < MaxSector
  738. && (this_sector + sectors >= conf->next_resync))
  739. do_balance = 0;
  740. for (slot = 0; slot < conf->copies ; slot++) {
  741. sector_t first_bad;
  742. int bad_sectors;
  743. sector_t dev_sector;
  744. if (r10_bio->devs[slot].bio == IO_BLOCKED)
  745. continue;
  746. disk = r10_bio->devs[slot].devnum;
  747. rdev = rcu_dereference(conf->mirrors[disk].replacement);
  748. if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
  749. test_bit(Unmerged, &rdev->flags) ||
  750. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  751. rdev = rcu_dereference(conf->mirrors[disk].rdev);
  752. if (rdev == NULL ||
  753. test_bit(Faulty, &rdev->flags) ||
  754. test_bit(Unmerged, &rdev->flags))
  755. continue;
  756. if (!test_bit(In_sync, &rdev->flags) &&
  757. r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
  758. continue;
  759. dev_sector = r10_bio->devs[slot].addr;
  760. if (is_badblock(rdev, dev_sector, sectors,
  761. &first_bad, &bad_sectors)) {
  762. if (best_dist < MaxSector)
  763. /* Already have a better slot */
  764. continue;
  765. if (first_bad <= dev_sector) {
  766. /* Cannot read here. If this is the
  767. * 'primary' device, then we must not read
  768. * beyond 'bad_sectors' from another device.
  769. */
  770. bad_sectors -= (dev_sector - first_bad);
  771. if (!do_balance && sectors > bad_sectors)
  772. sectors = bad_sectors;
  773. if (best_good_sectors > sectors)
  774. best_good_sectors = sectors;
  775. } else {
  776. sector_t good_sectors =
  777. first_bad - dev_sector;
  778. if (good_sectors > best_good_sectors) {
  779. best_good_sectors = good_sectors;
  780. best_slot = slot;
  781. best_rdev = rdev;
  782. }
  783. if (!do_balance)
  784. /* Must read from here */
  785. break;
  786. }
  787. continue;
  788. } else
  789. best_good_sectors = sectors;
  790. if (!do_balance)
  791. break;
  792. /* This optimisation is debatable, and completely destroys
  793. * sequential read speed for 'far copies' arrays. So only
  794. * keep it for 'near' arrays, and review those later.
  795. */
  796. if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
  797. break;
  798. /* for far > 1 always use the lowest address */
  799. if (geo->far_copies > 1)
  800. new_distance = r10_bio->devs[slot].addr;
  801. else
  802. new_distance = abs(r10_bio->devs[slot].addr -
  803. conf->mirrors[disk].head_position);
  804. if (new_distance < best_dist) {
  805. best_dist = new_distance;
  806. best_slot = slot;
  807. best_rdev = rdev;
  808. }
  809. }
  810. if (slot >= conf->copies) {
  811. slot = best_slot;
  812. rdev = best_rdev;
  813. }
  814. if (slot >= 0) {
  815. atomic_inc(&rdev->nr_pending);
  816. if (test_bit(Faulty, &rdev->flags)) {
  817. /* Cannot risk returning a device that failed
  818. * before we inc'ed nr_pending
  819. */
  820. rdev_dec_pending(rdev, conf->mddev);
  821. goto retry;
  822. }
  823. r10_bio->read_slot = slot;
  824. } else
  825. rdev = NULL;
  826. rcu_read_unlock();
  827. *max_sectors = best_good_sectors;
  828. return rdev;
  829. }
  830. int md_raid10_congested(struct mddev *mddev, int bits)
  831. {
  832. struct r10conf *conf = mddev->private;
  833. int i, ret = 0;
  834. if ((bits & (1 << BDI_async_congested)) &&
  835. conf->pending_count >= max_queued_requests)
  836. return 1;
  837. rcu_read_lock();
  838. for (i = 0;
  839. (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
  840. && ret == 0;
  841. i++) {
  842. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  843. if (rdev && !test_bit(Faulty, &rdev->flags)) {
  844. struct request_queue *q = bdev_get_queue(rdev->bdev);
  845. ret |= bdi_congested(&q->backing_dev_info, bits);
  846. }
  847. }
  848. rcu_read_unlock();
  849. return ret;
  850. }
  851. EXPORT_SYMBOL_GPL(md_raid10_congested);
  852. static int raid10_congested(void *data, int bits)
  853. {
  854. struct mddev *mddev = data;
  855. return mddev_congested(mddev, bits) ||
  856. md_raid10_congested(mddev, bits);
  857. }
  858. static void flush_pending_writes(struct r10conf *conf)
  859. {
  860. /* Any writes that have been queued but are awaiting
  861. * bitmap updates get flushed here.
  862. */
  863. spin_lock_irq(&conf->device_lock);
  864. if (conf->pending_bio_list.head) {
  865. struct bio *bio;
  866. bio = bio_list_get(&conf->pending_bio_list);
  867. conf->pending_count = 0;
  868. spin_unlock_irq(&conf->device_lock);
  869. /* flush any pending bitmap writes to disk
  870. * before proceeding w/ I/O */
  871. bitmap_unplug(conf->mddev->bitmap);
  872. wake_up(&conf->wait_barrier);
  873. while (bio) { /* submit pending writes */
  874. struct bio *next = bio->bi_next;
  875. bio->bi_next = NULL;
  876. if (unlikely((bio->bi_rw & REQ_DISCARD) &&
  877. !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
  878. /* Just ignore it */
  879. bio_endio(bio, 0);
  880. else
  881. generic_make_request(bio);
  882. bio = next;
  883. }
  884. } else
  885. spin_unlock_irq(&conf->device_lock);
  886. }
  887. /* Barriers....
  888. * Sometimes we need to suspend IO while we do something else,
  889. * either some resync/recovery, or reconfigure the array.
  890. * To do this we raise a 'barrier'.
  891. * The 'barrier' is a counter that can be raised multiple times
  892. * to count how many activities are happening which preclude
  893. * normal IO.
  894. * We can only raise the barrier if there is no pending IO.
  895. * i.e. if nr_pending == 0.
  896. * We choose only to raise the barrier if no-one is waiting for the
  897. * barrier to go down. This means that as soon as an IO request
  898. * is ready, no other operations which require a barrier will start
  899. * until the IO request has had a chance.
  900. *
  901. * So: regular IO calls 'wait_barrier'. When that returns there
  902. * is no backgroup IO happening, It must arrange to call
  903. * allow_barrier when it has finished its IO.
  904. * backgroup IO calls must call raise_barrier. Once that returns
  905. * there is no normal IO happeing. It must arrange to call
  906. * lower_barrier when the particular background IO completes.
  907. */
  908. static void raise_barrier(struct r10conf *conf, int force)
  909. {
  910. BUG_ON(force && !conf->barrier);
  911. spin_lock_irq(&conf->resync_lock);
  912. /* Wait until no block IO is waiting (unless 'force') */
  913. wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
  914. conf->resync_lock);
  915. /* block any new IO from starting */
  916. conf->barrier++;
  917. /* Now wait for all pending IO to complete */
  918. wait_event_lock_irq(conf->wait_barrier,
  919. !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
  920. conf->resync_lock);
  921. spin_unlock_irq(&conf->resync_lock);
  922. }
  923. static void lower_barrier(struct r10conf *conf)
  924. {
  925. unsigned long flags;
  926. spin_lock_irqsave(&conf->resync_lock, flags);
  927. conf->barrier--;
  928. spin_unlock_irqrestore(&conf->resync_lock, flags);
  929. wake_up(&conf->wait_barrier);
  930. }
  931. static void wait_barrier(struct r10conf *conf)
  932. {
  933. spin_lock_irq(&conf->resync_lock);
  934. if (conf->barrier) {
  935. conf->nr_waiting++;
  936. /* Wait for the barrier to drop.
  937. * However if there are already pending
  938. * requests (preventing the barrier from
  939. * rising completely), and the
  940. * pre-process bio queue isn't empty,
  941. * then don't wait, as we need to empty
  942. * that queue to get the nr_pending
  943. * count down.
  944. */
  945. wait_event_lock_irq(conf->wait_barrier,
  946. !conf->barrier ||
  947. (conf->nr_pending &&
  948. current->bio_list &&
  949. !bio_list_empty(current->bio_list)),
  950. conf->resync_lock);
  951. conf->nr_waiting--;
  952. }
  953. conf->nr_pending++;
  954. spin_unlock_irq(&conf->resync_lock);
  955. }
  956. static void allow_barrier(struct r10conf *conf)
  957. {
  958. unsigned long flags;
  959. spin_lock_irqsave(&conf->resync_lock, flags);
  960. conf->nr_pending--;
  961. spin_unlock_irqrestore(&conf->resync_lock, flags);
  962. wake_up(&conf->wait_barrier);
  963. }
  964. static void freeze_array(struct r10conf *conf, int extra)
  965. {
  966. /* stop syncio and normal IO and wait for everything to
  967. * go quiet.
  968. * We increment barrier and nr_waiting, and then
  969. * wait until nr_pending match nr_queued+extra
  970. * This is called in the context of one normal IO request
  971. * that has failed. Thus any sync request that might be pending
  972. * will be blocked by nr_pending, and we need to wait for
  973. * pending IO requests to complete or be queued for re-try.
  974. * Thus the number queued (nr_queued) plus this request (extra)
  975. * must match the number of pending IOs (nr_pending) before
  976. * we continue.
  977. */
  978. spin_lock_irq(&conf->resync_lock);
  979. conf->barrier++;
  980. conf->nr_waiting++;
  981. wait_event_lock_irq_cmd(conf->wait_barrier,
  982. conf->nr_pending == conf->nr_queued+extra,
  983. conf->resync_lock,
  984. flush_pending_writes(conf));
  985. spin_unlock_irq(&conf->resync_lock);
  986. }
  987. static void unfreeze_array(struct r10conf *conf)
  988. {
  989. /* reverse the effect of the freeze */
  990. spin_lock_irq(&conf->resync_lock);
  991. conf->barrier--;
  992. conf->nr_waiting--;
  993. wake_up(&conf->wait_barrier);
  994. spin_unlock_irq(&conf->resync_lock);
  995. }
  996. static sector_t choose_data_offset(struct r10bio *r10_bio,
  997. struct md_rdev *rdev)
  998. {
  999. if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
  1000. test_bit(R10BIO_Previous, &r10_bio->state))
  1001. return rdev->data_offset;
  1002. else
  1003. return rdev->new_data_offset;
  1004. }
  1005. struct raid10_plug_cb {
  1006. struct blk_plug_cb cb;
  1007. struct bio_list pending;
  1008. int pending_cnt;
  1009. };
  1010. static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
  1011. {
  1012. struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
  1013. cb);
  1014. struct mddev *mddev = plug->cb.data;
  1015. struct r10conf *conf = mddev->private;
  1016. struct bio *bio;
  1017. if (from_schedule || current->bio_list) {
  1018. spin_lock_irq(&conf->device_lock);
  1019. bio_list_merge(&conf->pending_bio_list, &plug->pending);
  1020. conf->pending_count += plug->pending_cnt;
  1021. spin_unlock_irq(&conf->device_lock);
  1022. wake_up(&conf->wait_barrier);
  1023. md_wakeup_thread(mddev->thread);
  1024. kfree(plug);
  1025. return;
  1026. }
  1027. /* we aren't scheduling, so we can do the write-out directly. */
  1028. bio = bio_list_get(&plug->pending);
  1029. bitmap_unplug(mddev->bitmap);
  1030. wake_up(&conf->wait_barrier);
  1031. while (bio) { /* submit pending writes */
  1032. struct bio *next = bio->bi_next;
  1033. bio->bi_next = NULL;
  1034. if (unlikely((bio->bi_rw & REQ_DISCARD) &&
  1035. !blk_queue_discard(bdev_get_queue(bio->bi_bdev))))
  1036. /* Just ignore it */
  1037. bio_endio(bio, 0);
  1038. else
  1039. generic_make_request(bio);
  1040. bio = next;
  1041. }
  1042. kfree(plug);
  1043. }
  1044. static void make_request(struct mddev *mddev, struct bio * bio)
  1045. {
  1046. struct r10conf *conf = mddev->private;
  1047. struct r10bio *r10_bio;
  1048. struct bio *read_bio;
  1049. int i;
  1050. sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
  1051. int chunk_sects = chunk_mask + 1;
  1052. const int rw = bio_data_dir(bio);
  1053. const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
  1054. const unsigned long do_fua = (bio->bi_rw & REQ_FUA);
  1055. const unsigned long do_discard = (bio->bi_rw
  1056. & (REQ_DISCARD | REQ_SECURE));
  1057. const unsigned long do_same = (bio->bi_rw & REQ_WRITE_SAME);
  1058. unsigned long flags;
  1059. struct md_rdev *blocked_rdev;
  1060. struct blk_plug_cb *cb;
  1061. struct raid10_plug_cb *plug = NULL;
  1062. int sectors_handled;
  1063. int max_sectors;
  1064. int sectors;
  1065. if (unlikely(bio->bi_rw & REQ_FLUSH)) {
  1066. md_flush_request(mddev, bio);
  1067. return;
  1068. }
  1069. /* If this request crosses a chunk boundary, we need to
  1070. * split it. This will only happen for 1 PAGE (or less) requests.
  1071. */
  1072. if (unlikely((bio->bi_sector & chunk_mask) + bio_sectors(bio)
  1073. > chunk_sects
  1074. && (conf->geo.near_copies < conf->geo.raid_disks
  1075. || conf->prev.near_copies < conf->prev.raid_disks))) {
  1076. struct bio_pair *bp;
  1077. /* Sanity check -- queue functions should prevent this happening */
  1078. if (bio_segments(bio) > 1)
  1079. goto bad_map;
  1080. /* This is a one page bio that upper layers
  1081. * refuse to split for us, so we need to split it.
  1082. */
  1083. bp = bio_split(bio,
  1084. chunk_sects - (bio->bi_sector & (chunk_sects - 1)) );
  1085. /* Each of these 'make_request' calls will call 'wait_barrier'.
  1086. * If the first succeeds but the second blocks due to the resync
  1087. * thread raising the barrier, we will deadlock because the
  1088. * IO to the underlying device will be queued in generic_make_request
  1089. * and will never complete, so will never reduce nr_pending.
  1090. * So increment nr_waiting here so no new raise_barriers will
  1091. * succeed, and so the second wait_barrier cannot block.
  1092. */
  1093. spin_lock_irq(&conf->resync_lock);
  1094. conf->nr_waiting++;
  1095. spin_unlock_irq(&conf->resync_lock);
  1096. make_request(mddev, &bp->bio1);
  1097. make_request(mddev, &bp->bio2);
  1098. spin_lock_irq(&conf->resync_lock);
  1099. conf->nr_waiting--;
  1100. wake_up(&conf->wait_barrier);
  1101. spin_unlock_irq(&conf->resync_lock);
  1102. bio_pair_release(bp);
  1103. return;
  1104. bad_map:
  1105. printk("md/raid10:%s: make_request bug: can't convert block across chunks"
  1106. " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2,
  1107. (unsigned long long)bio->bi_sector, bio_sectors(bio) / 2);
  1108. bio_io_error(bio);
  1109. return;
  1110. }
  1111. md_write_start(mddev, bio);
  1112. /*
  1113. * Register the new request and wait if the reconstruction
  1114. * thread has put up a bar for new requests.
  1115. * Continue immediately if no resync is active currently.
  1116. */
  1117. wait_barrier(conf);
  1118. sectors = bio_sectors(bio);
  1119. while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  1120. bio->bi_sector < conf->reshape_progress &&
  1121. bio->bi_sector + sectors > conf->reshape_progress) {
  1122. /* IO spans the reshape position. Need to wait for
  1123. * reshape to pass
  1124. */
  1125. allow_barrier(conf);
  1126. wait_event(conf->wait_barrier,
  1127. conf->reshape_progress <= bio->bi_sector ||
  1128. conf->reshape_progress >= bio->bi_sector + sectors);
  1129. wait_barrier(conf);
  1130. }
  1131. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  1132. bio_data_dir(bio) == WRITE &&
  1133. (mddev->reshape_backwards
  1134. ? (bio->bi_sector < conf->reshape_safe &&
  1135. bio->bi_sector + sectors > conf->reshape_progress)
  1136. : (bio->bi_sector + sectors > conf->reshape_safe &&
  1137. bio->bi_sector < conf->reshape_progress))) {
  1138. /* Need to update reshape_position in metadata */
  1139. mddev->reshape_position = conf->reshape_progress;
  1140. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1141. set_bit(MD_CHANGE_PENDING, &mddev->flags);
  1142. md_wakeup_thread(mddev->thread);
  1143. wait_event(mddev->sb_wait,
  1144. !test_bit(MD_CHANGE_PENDING, &mddev->flags));
  1145. conf->reshape_safe = mddev->reshape_position;
  1146. }
  1147. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1148. r10_bio->master_bio = bio;
  1149. r10_bio->sectors = sectors;
  1150. r10_bio->mddev = mddev;
  1151. r10_bio->sector = bio->bi_sector;
  1152. r10_bio->state = 0;
  1153. /* We might need to issue multiple reads to different
  1154. * devices if there are bad blocks around, so we keep
  1155. * track of the number of reads in bio->bi_phys_segments.
  1156. * If this is 0, there is only one r10_bio and no locking
  1157. * will be needed when the request completes. If it is
  1158. * non-zero, then it is the number of not-completed requests.
  1159. */
  1160. bio->bi_phys_segments = 0;
  1161. clear_bit(BIO_SEG_VALID, &bio->bi_flags);
  1162. if (rw == READ) {
  1163. /*
  1164. * read balancing logic:
  1165. */
  1166. struct md_rdev *rdev;
  1167. int slot;
  1168. read_again:
  1169. rdev = read_balance(conf, r10_bio, &max_sectors);
  1170. if (!rdev) {
  1171. raid_end_bio_io(r10_bio);
  1172. return;
  1173. }
  1174. slot = r10_bio->read_slot;
  1175. read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1176. md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
  1177. max_sectors);
  1178. r10_bio->devs[slot].bio = read_bio;
  1179. r10_bio->devs[slot].rdev = rdev;
  1180. read_bio->bi_sector = r10_bio->devs[slot].addr +
  1181. choose_data_offset(r10_bio, rdev);
  1182. read_bio->bi_bdev = rdev->bdev;
  1183. read_bio->bi_end_io = raid10_end_read_request;
  1184. read_bio->bi_rw = READ | do_sync;
  1185. read_bio->bi_private = r10_bio;
  1186. if (max_sectors < r10_bio->sectors) {
  1187. /* Could not read all from this device, so we will
  1188. * need another r10_bio.
  1189. */
  1190. sectors_handled = (r10_bio->sectors + max_sectors
  1191. - bio->bi_sector);
  1192. r10_bio->sectors = max_sectors;
  1193. spin_lock_irq(&conf->device_lock);
  1194. if (bio->bi_phys_segments == 0)
  1195. bio->bi_phys_segments = 2;
  1196. else
  1197. bio->bi_phys_segments++;
  1198. spin_unlock(&conf->device_lock);
  1199. /* Cannot call generic_make_request directly
  1200. * as that will be queued in __generic_make_request
  1201. * and subsequent mempool_alloc might block
  1202. * waiting for it. so hand bio over to raid10d.
  1203. */
  1204. reschedule_retry(r10_bio);
  1205. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1206. r10_bio->master_bio = bio;
  1207. r10_bio->sectors = bio_sectors(bio) - sectors_handled;
  1208. r10_bio->state = 0;
  1209. r10_bio->mddev = mddev;
  1210. r10_bio->sector = bio->bi_sector + sectors_handled;
  1211. goto read_again;
  1212. } else
  1213. generic_make_request(read_bio);
  1214. return;
  1215. }
  1216. /*
  1217. * WRITE:
  1218. */
  1219. if (conf->pending_count >= max_queued_requests) {
  1220. md_wakeup_thread(mddev->thread);
  1221. wait_event(conf->wait_barrier,
  1222. conf->pending_count < max_queued_requests);
  1223. }
  1224. /* first select target devices under rcu_lock and
  1225. * inc refcount on their rdev. Record them by setting
  1226. * bios[x] to bio
  1227. * If there are known/acknowledged bad blocks on any device
  1228. * on which we have seen a write error, we want to avoid
  1229. * writing to those blocks. This potentially requires several
  1230. * writes to write around the bad blocks. Each set of writes
  1231. * gets its own r10_bio with a set of bios attached. The number
  1232. * of r10_bios is recored in bio->bi_phys_segments just as with
  1233. * the read case.
  1234. */
  1235. r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
  1236. raid10_find_phys(conf, r10_bio);
  1237. retry_write:
  1238. blocked_rdev = NULL;
  1239. rcu_read_lock();
  1240. max_sectors = r10_bio->sectors;
  1241. for (i = 0; i < conf->copies; i++) {
  1242. int d = r10_bio->devs[i].devnum;
  1243. struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
  1244. struct md_rdev *rrdev = rcu_dereference(
  1245. conf->mirrors[d].replacement);
  1246. if (rdev == rrdev)
  1247. rrdev = NULL;
  1248. if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
  1249. atomic_inc(&rdev->nr_pending);
  1250. blocked_rdev = rdev;
  1251. break;
  1252. }
  1253. if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
  1254. atomic_inc(&rrdev->nr_pending);
  1255. blocked_rdev = rrdev;
  1256. break;
  1257. }
  1258. if (rdev && (test_bit(Faulty, &rdev->flags)
  1259. || test_bit(Unmerged, &rdev->flags)))
  1260. rdev = NULL;
  1261. if (rrdev && (test_bit(Faulty, &rrdev->flags)
  1262. || test_bit(Unmerged, &rrdev->flags)))
  1263. rrdev = NULL;
  1264. r10_bio->devs[i].bio = NULL;
  1265. r10_bio->devs[i].repl_bio = NULL;
  1266. if (!rdev && !rrdev) {
  1267. set_bit(R10BIO_Degraded, &r10_bio->state);
  1268. continue;
  1269. }
  1270. if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
  1271. sector_t first_bad;
  1272. sector_t dev_sector = r10_bio->devs[i].addr;
  1273. int bad_sectors;
  1274. int is_bad;
  1275. is_bad = is_badblock(rdev, dev_sector,
  1276. max_sectors,
  1277. &first_bad, &bad_sectors);
  1278. if (is_bad < 0) {
  1279. /* Mustn't write here until the bad block
  1280. * is acknowledged
  1281. */
  1282. atomic_inc(&rdev->nr_pending);
  1283. set_bit(BlockedBadBlocks, &rdev->flags);
  1284. blocked_rdev = rdev;
  1285. break;
  1286. }
  1287. if (is_bad && first_bad <= dev_sector) {
  1288. /* Cannot write here at all */
  1289. bad_sectors -= (dev_sector - first_bad);
  1290. if (bad_sectors < max_sectors)
  1291. /* Mustn't write more than bad_sectors
  1292. * to other devices yet
  1293. */
  1294. max_sectors = bad_sectors;
  1295. /* We don't set R10BIO_Degraded as that
  1296. * only applies if the disk is missing,
  1297. * so it might be re-added, and we want to
  1298. * know to recover this chunk.
  1299. * In this case the device is here, and the
  1300. * fact that this chunk is not in-sync is
  1301. * recorded in the bad block log.
  1302. */
  1303. continue;
  1304. }
  1305. if (is_bad) {
  1306. int good_sectors = first_bad - dev_sector;
  1307. if (good_sectors < max_sectors)
  1308. max_sectors = good_sectors;
  1309. }
  1310. }
  1311. if (rdev) {
  1312. r10_bio->devs[i].bio = bio;
  1313. atomic_inc(&rdev->nr_pending);
  1314. }
  1315. if (rrdev) {
  1316. r10_bio->devs[i].repl_bio = bio;
  1317. atomic_inc(&rrdev->nr_pending);
  1318. }
  1319. }
  1320. rcu_read_unlock();
  1321. if (unlikely(blocked_rdev)) {
  1322. /* Have to wait for this device to get unblocked, then retry */
  1323. int j;
  1324. int d;
  1325. for (j = 0; j < i; j++) {
  1326. if (r10_bio->devs[j].bio) {
  1327. d = r10_bio->devs[j].devnum;
  1328. rdev_dec_pending(conf->mirrors[d].rdev, mddev);
  1329. }
  1330. if (r10_bio->devs[j].repl_bio) {
  1331. struct md_rdev *rdev;
  1332. d = r10_bio->devs[j].devnum;
  1333. rdev = conf->mirrors[d].replacement;
  1334. if (!rdev) {
  1335. /* Race with remove_disk */
  1336. smp_mb();
  1337. rdev = conf->mirrors[d].rdev;
  1338. }
  1339. rdev_dec_pending(rdev, mddev);
  1340. }
  1341. }
  1342. allow_barrier(conf);
  1343. md_wait_for_blocked_rdev(blocked_rdev, mddev);
  1344. wait_barrier(conf);
  1345. goto retry_write;
  1346. }
  1347. if (max_sectors < r10_bio->sectors) {
  1348. /* We are splitting this into multiple parts, so
  1349. * we need to prepare for allocating another r10_bio.
  1350. */
  1351. r10_bio->sectors = max_sectors;
  1352. spin_lock_irq(&conf->device_lock);
  1353. if (bio->bi_phys_segments == 0)
  1354. bio->bi_phys_segments = 2;
  1355. else
  1356. bio->bi_phys_segments++;
  1357. spin_unlock_irq(&conf->device_lock);
  1358. }
  1359. sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector;
  1360. atomic_set(&r10_bio->remaining, 1);
  1361. bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
  1362. for (i = 0; i < conf->copies; i++) {
  1363. struct bio *mbio;
  1364. int d = r10_bio->devs[i].devnum;
  1365. if (r10_bio->devs[i].bio) {
  1366. struct md_rdev *rdev = conf->mirrors[d].rdev;
  1367. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1368. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1369. max_sectors);
  1370. r10_bio->devs[i].bio = mbio;
  1371. mbio->bi_sector = (r10_bio->devs[i].addr+
  1372. choose_data_offset(r10_bio,
  1373. rdev));
  1374. mbio->bi_bdev = rdev->bdev;
  1375. mbio->bi_end_io = raid10_end_write_request;
  1376. mbio->bi_rw =
  1377. WRITE | do_sync | do_fua | do_discard | do_same;
  1378. mbio->bi_private = r10_bio;
  1379. atomic_inc(&r10_bio->remaining);
  1380. cb = blk_check_plugged(raid10_unplug, mddev,
  1381. sizeof(*plug));
  1382. if (cb)
  1383. plug = container_of(cb, struct raid10_plug_cb,
  1384. cb);
  1385. else
  1386. plug = NULL;
  1387. spin_lock_irqsave(&conf->device_lock, flags);
  1388. if (plug) {
  1389. bio_list_add(&plug->pending, mbio);
  1390. plug->pending_cnt++;
  1391. } else {
  1392. bio_list_add(&conf->pending_bio_list, mbio);
  1393. conf->pending_count++;
  1394. }
  1395. spin_unlock_irqrestore(&conf->device_lock, flags);
  1396. if (!plug)
  1397. md_wakeup_thread(mddev->thread);
  1398. }
  1399. if (r10_bio->devs[i].repl_bio) {
  1400. struct md_rdev *rdev = conf->mirrors[d].replacement;
  1401. if (rdev == NULL) {
  1402. /* Replacement just got moved to main 'rdev' */
  1403. smp_mb();
  1404. rdev = conf->mirrors[d].rdev;
  1405. }
  1406. mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  1407. md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
  1408. max_sectors);
  1409. r10_bio->devs[i].repl_bio = mbio;
  1410. mbio->bi_sector = (r10_bio->devs[i].addr +
  1411. choose_data_offset(
  1412. r10_bio, rdev));
  1413. mbio->bi_bdev = rdev->bdev;
  1414. mbio->bi_end_io = raid10_end_write_request;
  1415. mbio->bi_rw =
  1416. WRITE | do_sync | do_fua | do_discard | do_same;
  1417. mbio->bi_private = r10_bio;
  1418. atomic_inc(&r10_bio->remaining);
  1419. spin_lock_irqsave(&conf->device_lock, flags);
  1420. bio_list_add(&conf->pending_bio_list, mbio);
  1421. conf->pending_count++;
  1422. spin_unlock_irqrestore(&conf->device_lock, flags);
  1423. if (!mddev_check_plugged(mddev))
  1424. md_wakeup_thread(mddev->thread);
  1425. }
  1426. }
  1427. /* Don't remove the bias on 'remaining' (one_write_done) until
  1428. * after checking if we need to go around again.
  1429. */
  1430. if (sectors_handled < bio_sectors(bio)) {
  1431. one_write_done(r10_bio);
  1432. /* We need another r10_bio. It has already been counted
  1433. * in bio->bi_phys_segments.
  1434. */
  1435. r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
  1436. r10_bio->master_bio = bio;
  1437. r10_bio->sectors = bio_sectors(bio) - sectors_handled;
  1438. r10_bio->mddev = mddev;
  1439. r10_bio->sector = bio->bi_sector + sectors_handled;
  1440. r10_bio->state = 0;
  1441. goto retry_write;
  1442. }
  1443. one_write_done(r10_bio);
  1444. /* In case raid10d snuck in to freeze_array */
  1445. wake_up(&conf->wait_barrier);
  1446. }
  1447. static void status(struct seq_file *seq, struct mddev *mddev)
  1448. {
  1449. struct r10conf *conf = mddev->private;
  1450. int i;
  1451. if (conf->geo.near_copies < conf->geo.raid_disks)
  1452. seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
  1453. if (conf->geo.near_copies > 1)
  1454. seq_printf(seq, " %d near-copies", conf->geo.near_copies);
  1455. if (conf->geo.far_copies > 1) {
  1456. if (conf->geo.far_offset)
  1457. seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
  1458. else
  1459. seq_printf(seq, " %d far-copies", conf->geo.far_copies);
  1460. }
  1461. seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
  1462. conf->geo.raid_disks - mddev->degraded);
  1463. for (i = 0; i < conf->geo.raid_disks; i++)
  1464. seq_printf(seq, "%s",
  1465. conf->mirrors[i].rdev &&
  1466. test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_");
  1467. seq_printf(seq, "]");
  1468. }
  1469. /* check if there are enough drives for
  1470. * every block to appear on atleast one.
  1471. * Don't consider the device numbered 'ignore'
  1472. * as we might be about to remove it.
  1473. */
  1474. static int _enough(struct r10conf *conf, int previous, int ignore)
  1475. {
  1476. int first = 0;
  1477. int has_enough = 0;
  1478. int disks, ncopies;
  1479. if (previous) {
  1480. disks = conf->prev.raid_disks;
  1481. ncopies = conf->prev.near_copies;
  1482. } else {
  1483. disks = conf->geo.raid_disks;
  1484. ncopies = conf->geo.near_copies;
  1485. }
  1486. rcu_read_lock();
  1487. do {
  1488. int n = conf->copies;
  1489. int cnt = 0;
  1490. int this = first;
  1491. while (n--) {
  1492. struct md_rdev *rdev;
  1493. if (this != ignore &&
  1494. (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
  1495. test_bit(In_sync, &rdev->flags))
  1496. cnt++;
  1497. this = (this+1) % disks;
  1498. }
  1499. if (cnt == 0)
  1500. goto out;
  1501. first = (first + ncopies) % disks;
  1502. } while (first != 0);
  1503. has_enough = 1;
  1504. out:
  1505. rcu_read_unlock();
  1506. return has_enough;
  1507. }
  1508. static int enough(struct r10conf *conf, int ignore)
  1509. {
  1510. /* when calling 'enough', both 'prev' and 'geo' must
  1511. * be stable.
  1512. * This is ensured if ->reconfig_mutex or ->device_lock
  1513. * is held.
  1514. */
  1515. return _enough(conf, 0, ignore) &&
  1516. _enough(conf, 1, ignore);
  1517. }
  1518. static void error(struct mddev *mddev, struct md_rdev *rdev)
  1519. {
  1520. char b[BDEVNAME_SIZE];
  1521. struct r10conf *conf = mddev->private;
  1522. unsigned long flags;
  1523. /*
  1524. * If it is not operational, then we have already marked it as dead
  1525. * else if it is the last working disks, ignore the error, let the
  1526. * next level up know.
  1527. * else mark the drive as failed
  1528. */
  1529. spin_lock_irqsave(&conf->device_lock, flags);
  1530. if (test_bit(In_sync, &rdev->flags)
  1531. && !enough(conf, rdev->raid_disk)) {
  1532. /*
  1533. * Don't fail the drive, just return an IO error.
  1534. */
  1535. spin_unlock_irqrestore(&conf->device_lock, flags);
  1536. return;
  1537. }
  1538. if (test_and_clear_bit(In_sync, &rdev->flags)) {
  1539. mddev->degraded++;
  1540. /*
  1541. * if recovery is running, make sure it aborts.
  1542. */
  1543. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  1544. }
  1545. set_bit(Blocked, &rdev->flags);
  1546. set_bit(Faulty, &rdev->flags);
  1547. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1548. spin_unlock_irqrestore(&conf->device_lock, flags);
  1549. printk(KERN_ALERT
  1550. "md/raid10:%s: Disk failure on %s, disabling device.\n"
  1551. "md/raid10:%s: Operation continuing on %d devices.\n",
  1552. mdname(mddev), bdevname(rdev->bdev, b),
  1553. mdname(mddev), conf->geo.raid_disks - mddev->degraded);
  1554. }
  1555. static void print_conf(struct r10conf *conf)
  1556. {
  1557. int i;
  1558. struct raid10_info *tmp;
  1559. printk(KERN_DEBUG "RAID10 conf printout:\n");
  1560. if (!conf) {
  1561. printk(KERN_DEBUG "(!conf)\n");
  1562. return;
  1563. }
  1564. printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
  1565. conf->geo.raid_disks);
  1566. for (i = 0; i < conf->geo.raid_disks; i++) {
  1567. char b[BDEVNAME_SIZE];
  1568. tmp = conf->mirrors + i;
  1569. if (tmp->rdev)
  1570. printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
  1571. i, !test_bit(In_sync, &tmp->rdev->flags),
  1572. !test_bit(Faulty, &tmp->rdev->flags),
  1573. bdevname(tmp->rdev->bdev,b));
  1574. }
  1575. }
  1576. static void close_sync(struct r10conf *conf)
  1577. {
  1578. wait_barrier(conf);
  1579. allow_barrier(conf);
  1580. mempool_destroy(conf->r10buf_pool);
  1581. conf->r10buf_pool = NULL;
  1582. }
  1583. static int raid10_spare_active(struct mddev *mddev)
  1584. {
  1585. int i;
  1586. struct r10conf *conf = mddev->private;
  1587. struct raid10_info *tmp;
  1588. int count = 0;
  1589. unsigned long flags;
  1590. /*
  1591. * Find all non-in_sync disks within the RAID10 configuration
  1592. * and mark them in_sync
  1593. */
  1594. for (i = 0; i < conf->geo.raid_disks; i++) {
  1595. tmp = conf->mirrors + i;
  1596. if (tmp->replacement
  1597. && tmp->replacement->recovery_offset == MaxSector
  1598. && !test_bit(Faulty, &tmp->replacement->flags)
  1599. && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
  1600. /* Replacement has just become active */
  1601. if (!tmp->rdev
  1602. || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
  1603. count++;
  1604. if (tmp->rdev) {
  1605. /* Replaced device not technically faulty,
  1606. * but we need to be sure it gets removed
  1607. * and never re-added.
  1608. */
  1609. set_bit(Faulty, &tmp->rdev->flags);
  1610. sysfs_notify_dirent_safe(
  1611. tmp->rdev->sysfs_state);
  1612. }
  1613. sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
  1614. } else if (tmp->rdev
  1615. && !test_bit(Faulty, &tmp->rdev->flags)
  1616. && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
  1617. count++;
  1618. sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
  1619. }
  1620. }
  1621. spin_lock_irqsave(&conf->device_lock, flags);
  1622. mddev->degraded -= count;
  1623. spin_unlock_irqrestore(&conf->device_lock, flags);
  1624. print_conf(conf);
  1625. return count;
  1626. }
  1627. static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
  1628. {
  1629. struct r10conf *conf = mddev->private;
  1630. int err = -EEXIST;
  1631. int mirror;
  1632. int first = 0;
  1633. int last = conf->geo.raid_disks - 1;
  1634. struct request_queue *q = bdev_get_queue(rdev->bdev);
  1635. if (mddev->recovery_cp < MaxSector)
  1636. /* only hot-add to in-sync arrays, as recovery is
  1637. * very different from resync
  1638. */
  1639. return -EBUSY;
  1640. if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
  1641. return -EINVAL;
  1642. if (rdev->raid_disk >= 0)
  1643. first = last = rdev->raid_disk;
  1644. if (q->merge_bvec_fn) {
  1645. set_bit(Unmerged, &rdev->flags);
  1646. mddev->merge_check_needed = 1;
  1647. }
  1648. if (rdev->saved_raid_disk >= first &&
  1649. conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
  1650. mirror = rdev->saved_raid_disk;
  1651. else
  1652. mirror = first;
  1653. for ( ; mirror <= last ; mirror++) {
  1654. struct raid10_info *p = &conf->mirrors[mirror];
  1655. if (p->recovery_disabled == mddev->recovery_disabled)
  1656. continue;
  1657. if (p->rdev) {
  1658. if (!test_bit(WantReplacement, &p->rdev->flags) ||
  1659. p->replacement != NULL)
  1660. continue;
  1661. clear_bit(In_sync, &rdev->flags);
  1662. set_bit(Replacement, &rdev->flags);
  1663. rdev->raid_disk = mirror;
  1664. err = 0;
  1665. if (mddev->gendisk)
  1666. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1667. rdev->data_offset << 9);
  1668. conf->fullsync = 1;
  1669. rcu_assign_pointer(p->replacement, rdev);
  1670. break;
  1671. }
  1672. if (mddev->gendisk)
  1673. disk_stack_limits(mddev->gendisk, rdev->bdev,
  1674. rdev->data_offset << 9);
  1675. p->head_position = 0;
  1676. p->recovery_disabled = mddev->recovery_disabled - 1;
  1677. rdev->raid_disk = mirror;
  1678. err = 0;
  1679. if (rdev->saved_raid_disk != mirror)
  1680. conf->fullsync = 1;
  1681. rcu_assign_pointer(p->rdev, rdev);
  1682. break;
  1683. }
  1684. if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
  1685. /* Some requests might not have seen this new
  1686. * merge_bvec_fn. We must wait for them to complete
  1687. * before merging the device fully.
  1688. * First we make sure any code which has tested
  1689. * our function has submitted the request, then
  1690. * we wait for all outstanding requests to complete.
  1691. */
  1692. synchronize_sched();
  1693. freeze_array(conf, 0);
  1694. unfreeze_array(conf);
  1695. clear_bit(Unmerged, &rdev->flags);
  1696. }
  1697. md_integrity_add_rdev(rdev, mddev);
  1698. if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
  1699. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
  1700. print_conf(conf);
  1701. return err;
  1702. }
  1703. static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1704. {
  1705. struct r10conf *conf = mddev->private;
  1706. int err = 0;
  1707. int number = rdev->raid_disk;
  1708. struct md_rdev **rdevp;
  1709. struct raid10_info *p = conf->mirrors + number;
  1710. print_conf(conf);
  1711. if (rdev == p->rdev)
  1712. rdevp = &p->rdev;
  1713. else if (rdev == p->replacement)
  1714. rdevp = &p->replacement;
  1715. else
  1716. return 0;
  1717. if (test_bit(In_sync, &rdev->flags) ||
  1718. atomic_read(&rdev->nr_pending)) {
  1719. err = -EBUSY;
  1720. goto abort;
  1721. }
  1722. /* Only remove faulty devices if recovery
  1723. * is not possible.
  1724. */
  1725. if (!test_bit(Faulty, &rdev->flags) &&
  1726. mddev->recovery_disabled != p->recovery_disabled &&
  1727. (!p->replacement || p->replacement == rdev) &&
  1728. number < conf->geo.raid_disks &&
  1729. enough(conf, -1)) {
  1730. err = -EBUSY;
  1731. goto abort;
  1732. }
  1733. *rdevp = NULL;
  1734. synchronize_rcu();
  1735. if (atomic_read(&rdev->nr_pending)) {
  1736. /* lost the race, try later */
  1737. err = -EBUSY;
  1738. *rdevp = rdev;
  1739. goto abort;
  1740. } else if (p->replacement) {
  1741. /* We must have just cleared 'rdev' */
  1742. p->rdev = p->replacement;
  1743. clear_bit(Replacement, &p->replacement->flags);
  1744. smp_mb(); /* Make sure other CPUs may see both as identical
  1745. * but will never see neither -- if they are careful.
  1746. */
  1747. p->replacement = NULL;
  1748. clear_bit(WantReplacement, &rdev->flags);
  1749. } else
  1750. /* We might have just remove the Replacement as faulty
  1751. * Clear the flag just in case
  1752. */
  1753. clear_bit(WantReplacement, &rdev->flags);
  1754. err = md_integrity_register(mddev);
  1755. abort:
  1756. print_conf(conf);
  1757. return err;
  1758. }
  1759. static void end_sync_read(struct bio *bio, int error)
  1760. {
  1761. struct r10bio *r10_bio = bio->bi_private;
  1762. struct r10conf *conf = r10_bio->mddev->private;
  1763. int d;
  1764. if (bio == r10_bio->master_bio) {
  1765. /* this is a reshape read */
  1766. d = r10_bio->read_slot; /* really the read dev */
  1767. } else
  1768. d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
  1769. if (test_bit(BIO_UPTODATE, &bio->bi_flags))
  1770. set_bit(R10BIO_Uptodate, &r10_bio->state);
  1771. else
  1772. /* The write handler will notice the lack of
  1773. * R10BIO_Uptodate and record any errors etc
  1774. */
  1775. atomic_add(r10_bio->sectors,
  1776. &conf->mirrors[d].rdev->corrected_errors);
  1777. /* for reconstruct, we always reschedule after a read.
  1778. * for resync, only after all reads
  1779. */
  1780. rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
  1781. if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
  1782. atomic_dec_and_test(&r10_bio->remaining)) {
  1783. /* we have read all the blocks,
  1784. * do the comparison in process context in raid10d
  1785. */
  1786. reschedule_retry(r10_bio);
  1787. }
  1788. }
  1789. static void end_sync_request(struct r10bio *r10_bio)
  1790. {
  1791. struct mddev *mddev = r10_bio->mddev;
  1792. while (atomic_dec_and_test(&r10_bio->remaining)) {
  1793. if (r10_bio->master_bio == NULL) {
  1794. /* the primary of several recovery bios */
  1795. sector_t s = r10_bio->sectors;
  1796. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1797. test_bit(R10BIO_WriteError, &r10_bio->state))
  1798. reschedule_retry(r10_bio);
  1799. else
  1800. put_buf(r10_bio);
  1801. md_done_sync(mddev, s, 1);
  1802. break;
  1803. } else {
  1804. struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
  1805. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  1806. test_bit(R10BIO_WriteError, &r10_bio->state))
  1807. reschedule_retry(r10_bio);
  1808. else
  1809. put_buf(r10_bio);
  1810. r10_bio = r10_bio2;
  1811. }
  1812. }
  1813. }
  1814. static void end_sync_write(struct bio *bio, int error)
  1815. {
  1816. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  1817. struct r10bio *r10_bio = bio->bi_private;
  1818. struct mddev *mddev = r10_bio->mddev;
  1819. struct r10conf *conf = mddev->private;
  1820. int d;
  1821. sector_t first_bad;
  1822. int bad_sectors;
  1823. int slot;
  1824. int repl;
  1825. struct md_rdev *rdev = NULL;
  1826. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  1827. if (repl)
  1828. rdev = conf->mirrors[d].replacement;
  1829. else
  1830. rdev = conf->mirrors[d].rdev;
  1831. if (!uptodate) {
  1832. if (repl)
  1833. md_error(mddev, rdev);
  1834. else {
  1835. set_bit(WriteErrorSeen, &rdev->flags);
  1836. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  1837. set_bit(MD_RECOVERY_NEEDED,
  1838. &rdev->mddev->recovery);
  1839. set_bit(R10BIO_WriteError, &r10_bio->state);
  1840. }
  1841. } else if (is_badblock(rdev,
  1842. r10_bio->devs[slot].addr,
  1843. r10_bio->sectors,
  1844. &first_bad, &bad_sectors))
  1845. set_bit(R10BIO_MadeGood, &r10_bio->state);
  1846. rdev_dec_pending(rdev, mddev);
  1847. end_sync_request(r10_bio);
  1848. }
  1849. /*
  1850. * Note: sync and recover and handled very differently for raid10
  1851. * This code is for resync.
  1852. * For resync, we read through virtual addresses and read all blocks.
  1853. * If there is any error, we schedule a write. The lowest numbered
  1854. * drive is authoritative.
  1855. * However requests come for physical address, so we need to map.
  1856. * For every physical address there are raid_disks/copies virtual addresses,
  1857. * which is always are least one, but is not necessarly an integer.
  1858. * This means that a physical address can span multiple chunks, so we may
  1859. * have to submit multiple io requests for a single sync request.
  1860. */
  1861. /*
  1862. * We check if all blocks are in-sync and only write to blocks that
  1863. * aren't in sync
  1864. */
  1865. static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  1866. {
  1867. struct r10conf *conf = mddev->private;
  1868. int i, first;
  1869. struct bio *tbio, *fbio;
  1870. int vcnt;
  1871. atomic_set(&r10_bio->remaining, 1);
  1872. /* find the first device with a block */
  1873. for (i=0; i<conf->copies; i++)
  1874. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags))
  1875. break;
  1876. if (i == conf->copies)
  1877. goto done;
  1878. first = i;
  1879. fbio = r10_bio->devs[i].bio;
  1880. vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
  1881. /* now find blocks with errors */
  1882. for (i=0 ; i < conf->copies ; i++) {
  1883. int j, d;
  1884. tbio = r10_bio->devs[i].bio;
  1885. if (tbio->bi_end_io != end_sync_read)
  1886. continue;
  1887. if (i == first)
  1888. continue;
  1889. if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) {
  1890. /* We know that the bi_io_vec layout is the same for
  1891. * both 'first' and 'i', so we just compare them.
  1892. * All vec entries are PAGE_SIZE;
  1893. */
  1894. int sectors = r10_bio->sectors;
  1895. for (j = 0; j < vcnt; j++) {
  1896. int len = PAGE_SIZE;
  1897. if (sectors < (len / 512))
  1898. len = sectors * 512;
  1899. if (memcmp(page_address(fbio->bi_io_vec[j].bv_page),
  1900. page_address(tbio->bi_io_vec[j].bv_page),
  1901. len))
  1902. break;
  1903. sectors -= len/512;
  1904. }
  1905. if (j == vcnt)
  1906. continue;
  1907. atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
  1908. if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
  1909. /* Don't fix anything. */
  1910. continue;
  1911. }
  1912. /* Ok, we need to write this bio, either to correct an
  1913. * inconsistency or to correct an unreadable block.
  1914. * First we need to fixup bv_offset, bv_len and
  1915. * bi_vecs, as the read request might have corrupted these
  1916. */
  1917. bio_reset(tbio);
  1918. tbio->bi_vcnt = vcnt;
  1919. tbio->bi_size = r10_bio->sectors << 9;
  1920. tbio->bi_rw = WRITE;
  1921. tbio->bi_private = r10_bio;
  1922. tbio->bi_sector = r10_bio->devs[i].addr;
  1923. for (j=0; j < vcnt ; j++) {
  1924. tbio->bi_io_vec[j].bv_offset = 0;
  1925. tbio->bi_io_vec[j].bv_len = PAGE_SIZE;
  1926. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1927. page_address(fbio->bi_io_vec[j].bv_page),
  1928. PAGE_SIZE);
  1929. }
  1930. tbio->bi_end_io = end_sync_write;
  1931. d = r10_bio->devs[i].devnum;
  1932. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  1933. atomic_inc(&r10_bio->remaining);
  1934. md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
  1935. tbio->bi_sector += conf->mirrors[d].rdev->data_offset;
  1936. tbio->bi_bdev = conf->mirrors[d].rdev->bdev;
  1937. generic_make_request(tbio);
  1938. }
  1939. /* Now write out to any replacement devices
  1940. * that are active
  1941. */
  1942. for (i = 0; i < conf->copies; i++) {
  1943. int j, d;
  1944. tbio = r10_bio->devs[i].repl_bio;
  1945. if (!tbio || !tbio->bi_end_io)
  1946. continue;
  1947. if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
  1948. && r10_bio->devs[i].bio != fbio)
  1949. for (j = 0; j < vcnt; j++)
  1950. memcpy(page_address(tbio->bi_io_vec[j].bv_page),
  1951. page_address(fbio->bi_io_vec[j].bv_page),
  1952. PAGE_SIZE);
  1953. d = r10_bio->devs[i].devnum;
  1954. atomic_inc(&r10_bio->remaining);
  1955. md_sync_acct(conf->mirrors[d].replacement->bdev,
  1956. bio_sectors(tbio));
  1957. generic_make_request(tbio);
  1958. }
  1959. done:
  1960. if (atomic_dec_and_test(&r10_bio->remaining)) {
  1961. md_done_sync(mddev, r10_bio->sectors, 1);
  1962. put_buf(r10_bio);
  1963. }
  1964. }
  1965. /*
  1966. * Now for the recovery code.
  1967. * Recovery happens across physical sectors.
  1968. * We recover all non-is_sync drives by finding the virtual address of
  1969. * each, and then choose a working drive that also has that virt address.
  1970. * There is a separate r10_bio for each non-in_sync drive.
  1971. * Only the first two slots are in use. The first for reading,
  1972. * The second for writing.
  1973. *
  1974. */
  1975. static void fix_recovery_read_error(struct r10bio *r10_bio)
  1976. {
  1977. /* We got a read error during recovery.
  1978. * We repeat the read in smaller page-sized sections.
  1979. * If a read succeeds, write it to the new device or record
  1980. * a bad block if we cannot.
  1981. * If a read fails, record a bad block on both old and
  1982. * new devices.
  1983. */
  1984. struct mddev *mddev = r10_bio->mddev;
  1985. struct r10conf *conf = mddev->private;
  1986. struct bio *bio = r10_bio->devs[0].bio;
  1987. sector_t sect = 0;
  1988. int sectors = r10_bio->sectors;
  1989. int idx = 0;
  1990. int dr = r10_bio->devs[0].devnum;
  1991. int dw = r10_bio->devs[1].devnum;
  1992. while (sectors) {
  1993. int s = sectors;
  1994. struct md_rdev *rdev;
  1995. sector_t addr;
  1996. int ok;
  1997. if (s > (PAGE_SIZE>>9))
  1998. s = PAGE_SIZE >> 9;
  1999. rdev = conf->mirrors[dr].rdev;
  2000. addr = r10_bio->devs[0].addr + sect,
  2001. ok = sync_page_io(rdev,
  2002. addr,
  2003. s << 9,
  2004. bio->bi_io_vec[idx].bv_page,
  2005. READ, false);
  2006. if (ok) {
  2007. rdev = conf->mirrors[dw].rdev;
  2008. addr = r10_bio->devs[1].addr + sect;
  2009. ok = sync_page_io(rdev,
  2010. addr,
  2011. s << 9,
  2012. bio->bi_io_vec[idx].bv_page,
  2013. WRITE, false);
  2014. if (!ok) {
  2015. set_bit(WriteErrorSeen, &rdev->flags);
  2016. if (!test_and_set_bit(WantReplacement,
  2017. &rdev->flags))
  2018. set_bit(MD_RECOVERY_NEEDED,
  2019. &rdev->mddev->recovery);
  2020. }
  2021. }
  2022. if (!ok) {
  2023. /* We don't worry if we cannot set a bad block -
  2024. * it really is bad so there is no loss in not
  2025. * recording it yet
  2026. */
  2027. rdev_set_badblocks(rdev, addr, s, 0);
  2028. if (rdev != conf->mirrors[dw].rdev) {
  2029. /* need bad block on destination too */
  2030. struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
  2031. addr = r10_bio->devs[1].addr + sect;
  2032. ok = rdev_set_badblocks(rdev2, addr, s, 0);
  2033. if (!ok) {
  2034. /* just abort the recovery */
  2035. printk(KERN_NOTICE
  2036. "md/raid10:%s: recovery aborted"
  2037. " due to read error\n",
  2038. mdname(mddev));
  2039. conf->mirrors[dw].recovery_disabled
  2040. = mddev->recovery_disabled;
  2041. set_bit(MD_RECOVERY_INTR,
  2042. &mddev->recovery);
  2043. break;
  2044. }
  2045. }
  2046. }
  2047. sectors -= s;
  2048. sect += s;
  2049. idx++;
  2050. }
  2051. }
  2052. static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  2053. {
  2054. struct r10conf *conf = mddev->private;
  2055. int d;
  2056. struct bio *wbio, *wbio2;
  2057. if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
  2058. fix_recovery_read_error(r10_bio);
  2059. end_sync_request(r10_bio);
  2060. return;
  2061. }
  2062. /*
  2063. * share the pages with the first bio
  2064. * and submit the write request
  2065. */
  2066. d = r10_bio->devs[1].devnum;
  2067. wbio = r10_bio->devs[1].bio;
  2068. wbio2 = r10_bio->devs[1].repl_bio;
  2069. /* Need to test wbio2->bi_end_io before we call
  2070. * generic_make_request as if the former is NULL,
  2071. * the latter is free to free wbio2.
  2072. */
  2073. if (wbio2 && !wbio2->bi_end_io)
  2074. wbio2 = NULL;
  2075. if (wbio->bi_end_io) {
  2076. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  2077. md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
  2078. generic_make_request(wbio);
  2079. }
  2080. if (wbio2) {
  2081. atomic_inc(&conf->mirrors[d].replacement->nr_pending);
  2082. md_sync_acct(conf->mirrors[d].replacement->bdev,
  2083. bio_sectors(wbio2));
  2084. generic_make_request(wbio2);
  2085. }
  2086. }
  2087. /*
  2088. * Used by fix_read_error() to decay the per rdev read_errors.
  2089. * We halve the read error count for every hour that has elapsed
  2090. * since the last recorded read error.
  2091. *
  2092. */
  2093. static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
  2094. {
  2095. struct timespec cur_time_mon;
  2096. unsigned long hours_since_last;
  2097. unsigned int read_errors = atomic_read(&rdev->read_errors);
  2098. ktime_get_ts(&cur_time_mon);
  2099. if (rdev->last_read_error.tv_sec == 0 &&
  2100. rdev->last_read_error.tv_nsec == 0) {
  2101. /* first time we've seen a read error */
  2102. rdev->last_read_error = cur_time_mon;
  2103. return;
  2104. }
  2105. hours_since_last = (cur_time_mon.tv_sec -
  2106. rdev->last_read_error.tv_sec) / 3600;
  2107. rdev->last_read_error = cur_time_mon;
  2108. /*
  2109. * if hours_since_last is > the number of bits in read_errors
  2110. * just set read errors to 0. We do this to avoid
  2111. * overflowing the shift of read_errors by hours_since_last.
  2112. */
  2113. if (hours_since_last >= 8 * sizeof(read_errors))
  2114. atomic_set(&rdev->read_errors, 0);
  2115. else
  2116. atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
  2117. }
  2118. static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
  2119. int sectors, struct page *page, int rw)
  2120. {
  2121. sector_t first_bad;
  2122. int bad_sectors;
  2123. if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
  2124. && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
  2125. return -1;
  2126. if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
  2127. /* success */
  2128. return 1;
  2129. if (rw == WRITE) {
  2130. set_bit(WriteErrorSeen, &rdev->flags);
  2131. if (!test_and_set_bit(WantReplacement, &rdev->flags))
  2132. set_bit(MD_RECOVERY_NEEDED,
  2133. &rdev->mddev->recovery);
  2134. }
  2135. /* need to record an error - either for the block or the device */
  2136. if (!rdev_set_badblocks(rdev, sector, sectors, 0))
  2137. md_error(rdev->mddev, rdev);
  2138. return 0;
  2139. }
  2140. /*
  2141. * This is a kernel thread which:
  2142. *
  2143. * 1. Retries failed read operations on working mirrors.
  2144. * 2. Updates the raid superblock when problems encounter.
  2145. * 3. Performs writes following reads for array synchronising.
  2146. */
  2147. static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
  2148. {
  2149. int sect = 0; /* Offset from r10_bio->sector */
  2150. int sectors = r10_bio->sectors;
  2151. struct md_rdev*rdev;
  2152. int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
  2153. int d = r10_bio->devs[r10_bio->read_slot].devnum;
  2154. /* still own a reference to this rdev, so it cannot
  2155. * have been cleared recently.
  2156. */
  2157. rdev = conf->mirrors[d].rdev;
  2158. if (test_bit(Faulty, &rdev->flags))
  2159. /* drive has already been failed, just ignore any
  2160. more fix_read_error() attempts */
  2161. return;
  2162. check_decay_read_errors(mddev, rdev);
  2163. atomic_inc(&rdev->read_errors);
  2164. if (atomic_read(&rdev->read_errors) > max_read_errors) {
  2165. char b[BDEVNAME_SIZE];
  2166. bdevname(rdev->bdev, b);
  2167. printk(KERN_NOTICE
  2168. "md/raid10:%s: %s: Raid device exceeded "
  2169. "read_error threshold [cur %d:max %d]\n",
  2170. mdname(mddev), b,
  2171. atomic_read(&rdev->read_errors), max_read_errors);
  2172. printk(KERN_NOTICE
  2173. "md/raid10:%s: %s: Failing raid device\n",
  2174. mdname(mddev), b);
  2175. md_error(mddev, conf->mirrors[d].rdev);
  2176. r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
  2177. return;
  2178. }
  2179. while(sectors) {
  2180. int s = sectors;
  2181. int sl = r10_bio->read_slot;
  2182. int success = 0;
  2183. int start;
  2184. if (s > (PAGE_SIZE>>9))
  2185. s = PAGE_SIZE >> 9;
  2186. rcu_read_lock();
  2187. do {
  2188. sector_t first_bad;
  2189. int bad_sectors;
  2190. d = r10_bio->devs[sl].devnum;
  2191. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2192. if (rdev &&
  2193. !test_bit(Unmerged, &rdev->flags) &&
  2194. test_bit(In_sync, &rdev->flags) &&
  2195. is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
  2196. &first_bad, &bad_sectors) == 0) {
  2197. atomic_inc(&rdev->nr_pending);
  2198. rcu_read_unlock();
  2199. success = sync_page_io(rdev,
  2200. r10_bio->devs[sl].addr +
  2201. sect,
  2202. s<<9,
  2203. conf->tmppage, READ, false);
  2204. rdev_dec_pending(rdev, mddev);
  2205. rcu_read_lock();
  2206. if (success)
  2207. break;
  2208. }
  2209. sl++;
  2210. if (sl == conf->copies)
  2211. sl = 0;
  2212. } while (!success && sl != r10_bio->read_slot);
  2213. rcu_read_unlock();
  2214. if (!success) {
  2215. /* Cannot read from anywhere, just mark the block
  2216. * as bad on the first device to discourage future
  2217. * reads.
  2218. */
  2219. int dn = r10_bio->devs[r10_bio->read_slot].devnum;
  2220. rdev = conf->mirrors[dn].rdev;
  2221. if (!rdev_set_badblocks(
  2222. rdev,
  2223. r10_bio->devs[r10_bio->read_slot].addr
  2224. + sect,
  2225. s, 0)) {
  2226. md_error(mddev, rdev);
  2227. r10_bio->devs[r10_bio->read_slot].bio
  2228. = IO_BLOCKED;
  2229. }
  2230. break;
  2231. }
  2232. start = sl;
  2233. /* write it back and re-read */
  2234. rcu_read_lock();
  2235. while (sl != r10_bio->read_slot) {
  2236. char b[BDEVNAME_SIZE];
  2237. if (sl==0)
  2238. sl = conf->copies;
  2239. sl--;
  2240. d = r10_bio->devs[sl].devnum;
  2241. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2242. if (!rdev ||
  2243. test_bit(Unmerged, &rdev->flags) ||
  2244. !test_bit(In_sync, &rdev->flags))
  2245. continue;
  2246. atomic_inc(&rdev->nr_pending);
  2247. rcu_read_unlock();
  2248. if (r10_sync_page_io(rdev,
  2249. r10_bio->devs[sl].addr +
  2250. sect,
  2251. s, conf->tmppage, WRITE)
  2252. == 0) {
  2253. /* Well, this device is dead */
  2254. printk(KERN_NOTICE
  2255. "md/raid10:%s: read correction "
  2256. "write failed"
  2257. " (%d sectors at %llu on %s)\n",
  2258. mdname(mddev), s,
  2259. (unsigned long long)(
  2260. sect +
  2261. choose_data_offset(r10_bio,
  2262. rdev)),
  2263. bdevname(rdev->bdev, b));
  2264. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2265. "drive\n",
  2266. mdname(mddev),
  2267. bdevname(rdev->bdev, b));
  2268. }
  2269. rdev_dec_pending(rdev, mddev);
  2270. rcu_read_lock();
  2271. }
  2272. sl = start;
  2273. while (sl != r10_bio->read_slot) {
  2274. char b[BDEVNAME_SIZE];
  2275. if (sl==0)
  2276. sl = conf->copies;
  2277. sl--;
  2278. d = r10_bio->devs[sl].devnum;
  2279. rdev = rcu_dereference(conf->mirrors[d].rdev);
  2280. if (!rdev ||
  2281. !test_bit(In_sync, &rdev->flags))
  2282. continue;
  2283. atomic_inc(&rdev->nr_pending);
  2284. rcu_read_unlock();
  2285. switch (r10_sync_page_io(rdev,
  2286. r10_bio->devs[sl].addr +
  2287. sect,
  2288. s, conf->tmppage,
  2289. READ)) {
  2290. case 0:
  2291. /* Well, this device is dead */
  2292. printk(KERN_NOTICE
  2293. "md/raid10:%s: unable to read back "
  2294. "corrected sectors"
  2295. " (%d sectors at %llu on %s)\n",
  2296. mdname(mddev), s,
  2297. (unsigned long long)(
  2298. sect +
  2299. choose_data_offset(r10_bio, rdev)),
  2300. bdevname(rdev->bdev, b));
  2301. printk(KERN_NOTICE "md/raid10:%s: %s: failing "
  2302. "drive\n",
  2303. mdname(mddev),
  2304. bdevname(rdev->bdev, b));
  2305. break;
  2306. case 1:
  2307. printk(KERN_INFO
  2308. "md/raid10:%s: read error corrected"
  2309. " (%d sectors at %llu on %s)\n",
  2310. mdname(mddev), s,
  2311. (unsigned long long)(
  2312. sect +
  2313. choose_data_offset(r10_bio, rdev)),
  2314. bdevname(rdev->bdev, b));
  2315. atomic_add(s, &rdev->corrected_errors);
  2316. }
  2317. rdev_dec_pending(rdev, mddev);
  2318. rcu_read_lock();
  2319. }
  2320. rcu_read_unlock();
  2321. sectors -= s;
  2322. sect += s;
  2323. }
  2324. }
  2325. static int narrow_write_error(struct r10bio *r10_bio, int i)
  2326. {
  2327. struct bio *bio = r10_bio->master_bio;
  2328. struct mddev *mddev = r10_bio->mddev;
  2329. struct r10conf *conf = mddev->private;
  2330. struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
  2331. /* bio has the data to be written to slot 'i' where
  2332. * we just recently had a write error.
  2333. * We repeatedly clone the bio and trim down to one block,
  2334. * then try the write. Where the write fails we record
  2335. * a bad block.
  2336. * It is conceivable that the bio doesn't exactly align with
  2337. * blocks. We must handle this.
  2338. *
  2339. * We currently own a reference to the rdev.
  2340. */
  2341. int block_sectors;
  2342. sector_t sector;
  2343. int sectors;
  2344. int sect_to_write = r10_bio->sectors;
  2345. int ok = 1;
  2346. if (rdev->badblocks.shift < 0)
  2347. return 0;
  2348. block_sectors = 1 << rdev->badblocks.shift;
  2349. sector = r10_bio->sector;
  2350. sectors = ((r10_bio->sector + block_sectors)
  2351. & ~(sector_t)(block_sectors - 1))
  2352. - sector;
  2353. while (sect_to_write) {
  2354. struct bio *wbio;
  2355. if (sectors > sect_to_write)
  2356. sectors = sect_to_write;
  2357. /* Write at 'sector' for 'sectors' */
  2358. wbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
  2359. md_trim_bio(wbio, sector - bio->bi_sector, sectors);
  2360. wbio->bi_sector = (r10_bio->devs[i].addr+
  2361. choose_data_offset(r10_bio, rdev) +
  2362. (sector - r10_bio->sector));
  2363. wbio->bi_bdev = rdev->bdev;
  2364. if (submit_bio_wait(WRITE, wbio) == 0)
  2365. /* Failure! */
  2366. ok = rdev_set_badblocks(rdev, sector,
  2367. sectors, 0)
  2368. && ok;
  2369. bio_put(wbio);
  2370. sect_to_write -= sectors;
  2371. sector += sectors;
  2372. sectors = block_sectors;
  2373. }
  2374. return ok;
  2375. }
  2376. static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
  2377. {
  2378. int slot = r10_bio->read_slot;
  2379. struct bio *bio;
  2380. struct r10conf *conf = mddev->private;
  2381. struct md_rdev *rdev = r10_bio->devs[slot].rdev;
  2382. char b[BDEVNAME_SIZE];
  2383. unsigned long do_sync;
  2384. int max_sectors;
  2385. /* we got a read error. Maybe the drive is bad. Maybe just
  2386. * the block and we can fix it.
  2387. * We freeze all other IO, and try reading the block from
  2388. * other devices. When we find one, we re-write
  2389. * and check it that fixes the read error.
  2390. * This is all done synchronously while the array is
  2391. * frozen.
  2392. */
  2393. bio = r10_bio->devs[slot].bio;
  2394. bdevname(bio->bi_bdev, b);
  2395. bio_put(bio);
  2396. r10_bio->devs[slot].bio = NULL;
  2397. if (mddev->ro == 0) {
  2398. freeze_array(conf, 1);
  2399. fix_read_error(conf, mddev, r10_bio);
  2400. unfreeze_array(conf);
  2401. } else
  2402. r10_bio->devs[slot].bio = IO_BLOCKED;
  2403. rdev_dec_pending(rdev, mddev);
  2404. read_more:
  2405. rdev = read_balance(conf, r10_bio, &max_sectors);
  2406. if (rdev == NULL) {
  2407. printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
  2408. " read error for block %llu\n",
  2409. mdname(mddev), b,
  2410. (unsigned long long)r10_bio->sector);
  2411. raid_end_bio_io(r10_bio);
  2412. return;
  2413. }
  2414. do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC);
  2415. slot = r10_bio->read_slot;
  2416. printk_ratelimited(
  2417. KERN_ERR
  2418. "md/raid10:%s: %s: redirecting "
  2419. "sector %llu to another mirror\n",
  2420. mdname(mddev),
  2421. bdevname(rdev->bdev, b),
  2422. (unsigned long long)r10_bio->sector);
  2423. bio = bio_clone_mddev(r10_bio->master_bio,
  2424. GFP_NOIO, mddev);
  2425. md_trim_bio(bio,
  2426. r10_bio->sector - bio->bi_sector,
  2427. max_sectors);
  2428. r10_bio->devs[slot].bio = bio;
  2429. r10_bio->devs[slot].rdev = rdev;
  2430. bio->bi_sector = r10_bio->devs[slot].addr
  2431. + choose_data_offset(r10_bio, rdev);
  2432. bio->bi_bdev = rdev->bdev;
  2433. bio->bi_rw = READ | do_sync;
  2434. bio->bi_private = r10_bio;
  2435. bio->bi_end_io = raid10_end_read_request;
  2436. if (max_sectors < r10_bio->sectors) {
  2437. /* Drat - have to split this up more */
  2438. struct bio *mbio = r10_bio->master_bio;
  2439. int sectors_handled =
  2440. r10_bio->sector + max_sectors
  2441. - mbio->bi_sector;
  2442. r10_bio->sectors = max_sectors;
  2443. spin_lock_irq(&conf->device_lock);
  2444. if (mbio->bi_phys_segments == 0)
  2445. mbio->bi_phys_segments = 2;
  2446. else
  2447. mbio->bi_phys_segments++;
  2448. spin_unlock_irq(&conf->device_lock);
  2449. generic_make_request(bio);
  2450. r10_bio = mempool_alloc(conf->r10bio_pool,
  2451. GFP_NOIO);
  2452. r10_bio->master_bio = mbio;
  2453. r10_bio->sectors = bio_sectors(mbio) - sectors_handled;
  2454. r10_bio->state = 0;
  2455. set_bit(R10BIO_ReadError,
  2456. &r10_bio->state);
  2457. r10_bio->mddev = mddev;
  2458. r10_bio->sector = mbio->bi_sector
  2459. + sectors_handled;
  2460. goto read_more;
  2461. } else
  2462. generic_make_request(bio);
  2463. }
  2464. static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
  2465. {
  2466. /* Some sort of write request has finished and it
  2467. * succeeded in writing where we thought there was a
  2468. * bad block. So forget the bad block.
  2469. * Or possibly if failed and we need to record
  2470. * a bad block.
  2471. */
  2472. int m;
  2473. struct md_rdev *rdev;
  2474. if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
  2475. test_bit(R10BIO_IsRecover, &r10_bio->state)) {
  2476. for (m = 0; m < conf->copies; m++) {
  2477. int dev = r10_bio->devs[m].devnum;
  2478. rdev = conf->mirrors[dev].rdev;
  2479. if (r10_bio->devs[m].bio == NULL)
  2480. continue;
  2481. if (test_bit(BIO_UPTODATE,
  2482. &r10_bio->devs[m].bio->bi_flags)) {
  2483. rdev_clear_badblocks(
  2484. rdev,
  2485. r10_bio->devs[m].addr,
  2486. r10_bio->sectors, 0);
  2487. } else {
  2488. if (!rdev_set_badblocks(
  2489. rdev,
  2490. r10_bio->devs[m].addr,
  2491. r10_bio->sectors, 0))
  2492. md_error(conf->mddev, rdev);
  2493. }
  2494. rdev = conf->mirrors[dev].replacement;
  2495. if (r10_bio->devs[m].repl_bio == NULL)
  2496. continue;
  2497. if (test_bit(BIO_UPTODATE,
  2498. &r10_bio->devs[m].repl_bio->bi_flags)) {
  2499. rdev_clear_badblocks(
  2500. rdev,
  2501. r10_bio->devs[m].addr,
  2502. r10_bio->sectors, 0);
  2503. } else {
  2504. if (!rdev_set_badblocks(
  2505. rdev,
  2506. r10_bio->devs[m].addr,
  2507. r10_bio->sectors, 0))
  2508. md_error(conf->mddev, rdev);
  2509. }
  2510. }
  2511. put_buf(r10_bio);
  2512. } else {
  2513. for (m = 0; m < conf->copies; m++) {
  2514. int dev = r10_bio->devs[m].devnum;
  2515. struct bio *bio = r10_bio->devs[m].bio;
  2516. rdev = conf->mirrors[dev].rdev;
  2517. if (bio == IO_MADE_GOOD) {
  2518. rdev_clear_badblocks(
  2519. rdev,
  2520. r10_bio->devs[m].addr,
  2521. r10_bio->sectors, 0);
  2522. rdev_dec_pending(rdev, conf->mddev);
  2523. } else if (bio != NULL &&
  2524. !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
  2525. if (!narrow_write_error(r10_bio, m)) {
  2526. md_error(conf->mddev, rdev);
  2527. set_bit(R10BIO_Degraded,
  2528. &r10_bio->state);
  2529. }
  2530. rdev_dec_pending(rdev, conf->mddev);
  2531. }
  2532. bio = r10_bio->devs[m].repl_bio;
  2533. rdev = conf->mirrors[dev].replacement;
  2534. if (rdev && bio == IO_MADE_GOOD) {
  2535. rdev_clear_badblocks(
  2536. rdev,
  2537. r10_bio->devs[m].addr,
  2538. r10_bio->sectors, 0);
  2539. rdev_dec_pending(rdev, conf->mddev);
  2540. }
  2541. }
  2542. if (test_bit(R10BIO_WriteError,
  2543. &r10_bio->state))
  2544. close_write(r10_bio);
  2545. raid_end_bio_io(r10_bio);
  2546. }
  2547. }
  2548. static void raid10d(struct md_thread *thread)
  2549. {
  2550. struct mddev *mddev = thread->mddev;
  2551. struct r10bio *r10_bio;
  2552. unsigned long flags;
  2553. struct r10conf *conf = mddev->private;
  2554. struct list_head *head = &conf->retry_list;
  2555. struct blk_plug plug;
  2556. md_check_recovery(mddev);
  2557. blk_start_plug(&plug);
  2558. for (;;) {
  2559. flush_pending_writes(conf);
  2560. spin_lock_irqsave(&conf->device_lock, flags);
  2561. if (list_empty(head)) {
  2562. spin_unlock_irqrestore(&conf->device_lock, flags);
  2563. break;
  2564. }
  2565. r10_bio = list_entry(head->prev, struct r10bio, retry_list);
  2566. list_del(head->prev);
  2567. conf->nr_queued--;
  2568. spin_unlock_irqrestore(&conf->device_lock, flags);
  2569. mddev = r10_bio->mddev;
  2570. conf = mddev->private;
  2571. if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
  2572. test_bit(R10BIO_WriteError, &r10_bio->state))
  2573. handle_write_completed(conf, r10_bio);
  2574. else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
  2575. reshape_request_write(mddev, r10_bio);
  2576. else if (test_bit(R10BIO_IsSync, &r10_bio->state))
  2577. sync_request_write(mddev, r10_bio);
  2578. else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
  2579. recovery_request_write(mddev, r10_bio);
  2580. else if (test_bit(R10BIO_ReadError, &r10_bio->state))
  2581. handle_read_error(mddev, r10_bio);
  2582. else {
  2583. /* just a partial read to be scheduled from a
  2584. * separate context
  2585. */
  2586. int slot = r10_bio->read_slot;
  2587. generic_make_request(r10_bio->devs[slot].bio);
  2588. }
  2589. cond_resched();
  2590. if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
  2591. md_check_recovery(mddev);
  2592. }
  2593. blk_finish_plug(&plug);
  2594. }
  2595. static int init_resync(struct r10conf *conf)
  2596. {
  2597. int buffs;
  2598. int i;
  2599. buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
  2600. BUG_ON(conf->r10buf_pool);
  2601. conf->have_replacement = 0;
  2602. for (i = 0; i < conf->geo.raid_disks; i++)
  2603. if (conf->mirrors[i].replacement)
  2604. conf->have_replacement = 1;
  2605. conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
  2606. if (!conf->r10buf_pool)
  2607. return -ENOMEM;
  2608. conf->next_resync = 0;
  2609. return 0;
  2610. }
  2611. /*
  2612. * perform a "sync" on one "block"
  2613. *
  2614. * We need to make sure that no normal I/O request - particularly write
  2615. * requests - conflict with active sync requests.
  2616. *
  2617. * This is achieved by tracking pending requests and a 'barrier' concept
  2618. * that can be installed to exclude normal IO requests.
  2619. *
  2620. * Resync and recovery are handled very differently.
  2621. * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
  2622. *
  2623. * For resync, we iterate over virtual addresses, read all copies,
  2624. * and update if there are differences. If only one copy is live,
  2625. * skip it.
  2626. * For recovery, we iterate over physical addresses, read a good
  2627. * value for each non-in_sync drive, and over-write.
  2628. *
  2629. * So, for recovery we may have several outstanding complex requests for a
  2630. * given address, one for each out-of-sync device. We model this by allocating
  2631. * a number of r10_bio structures, one for each out-of-sync device.
  2632. * As we setup these structures, we collect all bio's together into a list
  2633. * which we then process collectively to add pages, and then process again
  2634. * to pass to generic_make_request.
  2635. *
  2636. * The r10_bio structures are linked using a borrowed master_bio pointer.
  2637. * This link is counted in ->remaining. When the r10_bio that points to NULL
  2638. * has its remaining count decremented to 0, the whole complex operation
  2639. * is complete.
  2640. *
  2641. */
  2642. static sector_t sync_request(struct mddev *mddev, sector_t sector_nr,
  2643. int *skipped, int go_faster)
  2644. {
  2645. struct r10conf *conf = mddev->private;
  2646. struct r10bio *r10_bio;
  2647. struct bio *biolist = NULL, *bio;
  2648. sector_t max_sector, nr_sectors;
  2649. int i;
  2650. int max_sync;
  2651. sector_t sync_blocks;
  2652. sector_t sectors_skipped = 0;
  2653. int chunks_skipped = 0;
  2654. sector_t chunk_mask = conf->geo.chunk_mask;
  2655. if (!conf->r10buf_pool)
  2656. if (init_resync(conf))
  2657. return 0;
  2658. /*
  2659. * Allow skipping a full rebuild for incremental assembly
  2660. * of a clean array, like RAID1 does.
  2661. */
  2662. if (mddev->bitmap == NULL &&
  2663. mddev->recovery_cp == MaxSector &&
  2664. mddev->reshape_position == MaxSector &&
  2665. !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
  2666. !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
  2667. !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  2668. conf->fullsync == 0) {
  2669. *skipped = 1;
  2670. return mddev->dev_sectors - sector_nr;
  2671. }
  2672. skipped:
  2673. max_sector = mddev->dev_sectors;
  2674. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
  2675. test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2676. max_sector = mddev->resync_max_sectors;
  2677. if (sector_nr >= max_sector) {
  2678. /* If we aborted, we need to abort the
  2679. * sync on the 'current' bitmap chucks (there can
  2680. * be several when recovering multiple devices).
  2681. * as we may have started syncing it but not finished.
  2682. * We can find the current address in
  2683. * mddev->curr_resync, but for recovery,
  2684. * we need to convert that to several
  2685. * virtual addresses.
  2686. */
  2687. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
  2688. end_reshape(conf);
  2689. return 0;
  2690. }
  2691. if (mddev->curr_resync < max_sector) { /* aborted */
  2692. if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
  2693. bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
  2694. &sync_blocks, 1);
  2695. else for (i = 0; i < conf->geo.raid_disks; i++) {
  2696. sector_t sect =
  2697. raid10_find_virt(conf, mddev->curr_resync, i);
  2698. bitmap_end_sync(mddev->bitmap, sect,
  2699. &sync_blocks, 1);
  2700. }
  2701. } else {
  2702. /* completed sync */
  2703. if ((!mddev->bitmap || conf->fullsync)
  2704. && conf->have_replacement
  2705. && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2706. /* Completed a full sync so the replacements
  2707. * are now fully recovered.
  2708. */
  2709. for (i = 0; i < conf->geo.raid_disks; i++)
  2710. if (conf->mirrors[i].replacement)
  2711. conf->mirrors[i].replacement
  2712. ->recovery_offset
  2713. = MaxSector;
  2714. }
  2715. conf->fullsync = 0;
  2716. }
  2717. bitmap_close_sync(mddev->bitmap);
  2718. close_sync(conf);
  2719. *skipped = 1;
  2720. return sectors_skipped;
  2721. }
  2722. if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
  2723. return reshape_request(mddev, sector_nr, skipped);
  2724. if (chunks_skipped >= conf->geo.raid_disks) {
  2725. /* if there has been nothing to do on any drive,
  2726. * then there is nothing to do at all..
  2727. */
  2728. *skipped = 1;
  2729. return (max_sector - sector_nr) + sectors_skipped;
  2730. }
  2731. if (max_sector > mddev->resync_max)
  2732. max_sector = mddev->resync_max; /* Don't do IO beyond here */
  2733. /* make sure whole request will fit in a chunk - if chunks
  2734. * are meaningful
  2735. */
  2736. if (conf->geo.near_copies < conf->geo.raid_disks &&
  2737. max_sector > (sector_nr | chunk_mask))
  2738. max_sector = (sector_nr | chunk_mask) + 1;
  2739. /*
  2740. * If there is non-resync activity waiting for us then
  2741. * put in a delay to throttle resync.
  2742. */
  2743. if (!go_faster && conf->nr_waiting)
  2744. msleep_interruptible(1000);
  2745. /* Again, very different code for resync and recovery.
  2746. * Both must result in an r10bio with a list of bios that
  2747. * have bi_end_io, bi_sector, bi_bdev set,
  2748. * and bi_private set to the r10bio.
  2749. * For recovery, we may actually create several r10bios
  2750. * with 2 bios in each, that correspond to the bios in the main one.
  2751. * In this case, the subordinate r10bios link back through a
  2752. * borrowed master_bio pointer, and the counter in the master
  2753. * includes a ref from each subordinate.
  2754. */
  2755. /* First, we decide what to do and set ->bi_end_io
  2756. * To end_sync_read if we want to read, and
  2757. * end_sync_write if we will want to write.
  2758. */
  2759. max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
  2760. if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
  2761. /* recovery... the complicated one */
  2762. int j;
  2763. r10_bio = NULL;
  2764. for (i = 0 ; i < conf->geo.raid_disks; i++) {
  2765. int still_degraded;
  2766. struct r10bio *rb2;
  2767. sector_t sect;
  2768. int must_sync;
  2769. int any_working;
  2770. struct raid10_info *mirror = &conf->mirrors[i];
  2771. if ((mirror->rdev == NULL ||
  2772. test_bit(In_sync, &mirror->rdev->flags))
  2773. &&
  2774. (mirror->replacement == NULL ||
  2775. test_bit(Faulty,
  2776. &mirror->replacement->flags)))
  2777. continue;
  2778. still_degraded = 0;
  2779. /* want to reconstruct this device */
  2780. rb2 = r10_bio;
  2781. sect = raid10_find_virt(conf, sector_nr, i);
  2782. if (sect >= mddev->resync_max_sectors) {
  2783. /* last stripe is not complete - don't
  2784. * try to recover this sector.
  2785. */
  2786. continue;
  2787. }
  2788. /* Unless we are doing a full sync, or a replacement
  2789. * we only need to recover the block if it is set in
  2790. * the bitmap
  2791. */
  2792. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2793. &sync_blocks, 1);
  2794. if (sync_blocks < max_sync)
  2795. max_sync = sync_blocks;
  2796. if (!must_sync &&
  2797. mirror->replacement == NULL &&
  2798. !conf->fullsync) {
  2799. /* yep, skip the sync_blocks here, but don't assume
  2800. * that there will never be anything to do here
  2801. */
  2802. chunks_skipped = -1;
  2803. continue;
  2804. }
  2805. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2806. raise_barrier(conf, rb2 != NULL);
  2807. atomic_set(&r10_bio->remaining, 0);
  2808. r10_bio->master_bio = (struct bio*)rb2;
  2809. if (rb2)
  2810. atomic_inc(&rb2->remaining);
  2811. r10_bio->mddev = mddev;
  2812. set_bit(R10BIO_IsRecover, &r10_bio->state);
  2813. r10_bio->sector = sect;
  2814. raid10_find_phys(conf, r10_bio);
  2815. /* Need to check if the array will still be
  2816. * degraded
  2817. */
  2818. for (j = 0; j < conf->geo.raid_disks; j++)
  2819. if (conf->mirrors[j].rdev == NULL ||
  2820. test_bit(Faulty, &conf->mirrors[j].rdev->flags)) {
  2821. still_degraded = 1;
  2822. break;
  2823. }
  2824. must_sync = bitmap_start_sync(mddev->bitmap, sect,
  2825. &sync_blocks, still_degraded);
  2826. any_working = 0;
  2827. for (j=0; j<conf->copies;j++) {
  2828. int k;
  2829. int d = r10_bio->devs[j].devnum;
  2830. sector_t from_addr, to_addr;
  2831. struct md_rdev *rdev;
  2832. sector_t sector, first_bad;
  2833. int bad_sectors;
  2834. if (!conf->mirrors[d].rdev ||
  2835. !test_bit(In_sync, &conf->mirrors[d].rdev->flags))
  2836. continue;
  2837. /* This is where we read from */
  2838. any_working = 1;
  2839. rdev = conf->mirrors[d].rdev;
  2840. sector = r10_bio->devs[j].addr;
  2841. if (is_badblock(rdev, sector, max_sync,
  2842. &first_bad, &bad_sectors)) {
  2843. if (first_bad > sector)
  2844. max_sync = first_bad - sector;
  2845. else {
  2846. bad_sectors -= (sector
  2847. - first_bad);
  2848. if (max_sync > bad_sectors)
  2849. max_sync = bad_sectors;
  2850. continue;
  2851. }
  2852. }
  2853. bio = r10_bio->devs[0].bio;
  2854. bio_reset(bio);
  2855. bio->bi_next = biolist;
  2856. biolist = bio;
  2857. bio->bi_private = r10_bio;
  2858. bio->bi_end_io = end_sync_read;
  2859. bio->bi_rw = READ;
  2860. from_addr = r10_bio->devs[j].addr;
  2861. bio->bi_sector = from_addr + rdev->data_offset;
  2862. bio->bi_bdev = rdev->bdev;
  2863. atomic_inc(&rdev->nr_pending);
  2864. /* and we write to 'i' (if not in_sync) */
  2865. for (k=0; k<conf->copies; k++)
  2866. if (r10_bio->devs[k].devnum == i)
  2867. break;
  2868. BUG_ON(k == conf->copies);
  2869. to_addr = r10_bio->devs[k].addr;
  2870. r10_bio->devs[0].devnum = d;
  2871. r10_bio->devs[0].addr = from_addr;
  2872. r10_bio->devs[1].devnum = i;
  2873. r10_bio->devs[1].addr = to_addr;
  2874. rdev = mirror->rdev;
  2875. if (!test_bit(In_sync, &rdev->flags)) {
  2876. bio = r10_bio->devs[1].bio;
  2877. bio_reset(bio);
  2878. bio->bi_next = biolist;
  2879. biolist = bio;
  2880. bio->bi_private = r10_bio;
  2881. bio->bi_end_io = end_sync_write;
  2882. bio->bi_rw = WRITE;
  2883. bio->bi_sector = to_addr
  2884. + rdev->data_offset;
  2885. bio->bi_bdev = rdev->bdev;
  2886. atomic_inc(&r10_bio->remaining);
  2887. } else
  2888. r10_bio->devs[1].bio->bi_end_io = NULL;
  2889. /* and maybe write to replacement */
  2890. bio = r10_bio->devs[1].repl_bio;
  2891. if (bio)
  2892. bio->bi_end_io = NULL;
  2893. rdev = mirror->replacement;
  2894. /* Note: if rdev != NULL, then bio
  2895. * cannot be NULL as r10buf_pool_alloc will
  2896. * have allocated it.
  2897. * So the second test here is pointless.
  2898. * But it keeps semantic-checkers happy, and
  2899. * this comment keeps human reviewers
  2900. * happy.
  2901. */
  2902. if (rdev == NULL || bio == NULL ||
  2903. test_bit(Faulty, &rdev->flags))
  2904. break;
  2905. bio_reset(bio);
  2906. bio->bi_next = biolist;
  2907. biolist = bio;
  2908. bio->bi_private = r10_bio;
  2909. bio->bi_end_io = end_sync_write;
  2910. bio->bi_rw = WRITE;
  2911. bio->bi_sector = to_addr + rdev->data_offset;
  2912. bio->bi_bdev = rdev->bdev;
  2913. atomic_inc(&r10_bio->remaining);
  2914. break;
  2915. }
  2916. if (j == conf->copies) {
  2917. /* Cannot recover, so abort the recovery or
  2918. * record a bad block */
  2919. put_buf(r10_bio);
  2920. if (rb2)
  2921. atomic_dec(&rb2->remaining);
  2922. r10_bio = rb2;
  2923. if (any_working) {
  2924. /* problem is that there are bad blocks
  2925. * on other device(s)
  2926. */
  2927. int k;
  2928. for (k = 0; k < conf->copies; k++)
  2929. if (r10_bio->devs[k].devnum == i)
  2930. break;
  2931. if (!test_bit(In_sync,
  2932. &mirror->rdev->flags)
  2933. && !rdev_set_badblocks(
  2934. mirror->rdev,
  2935. r10_bio->devs[k].addr,
  2936. max_sync, 0))
  2937. any_working = 0;
  2938. if (mirror->replacement &&
  2939. !rdev_set_badblocks(
  2940. mirror->replacement,
  2941. r10_bio->devs[k].addr,
  2942. max_sync, 0))
  2943. any_working = 0;
  2944. }
  2945. if (!any_working) {
  2946. if (!test_and_set_bit(MD_RECOVERY_INTR,
  2947. &mddev->recovery))
  2948. printk(KERN_INFO "md/raid10:%s: insufficient "
  2949. "working devices for recovery.\n",
  2950. mdname(mddev));
  2951. mirror->recovery_disabled
  2952. = mddev->recovery_disabled;
  2953. }
  2954. break;
  2955. }
  2956. }
  2957. if (biolist == NULL) {
  2958. while (r10_bio) {
  2959. struct r10bio *rb2 = r10_bio;
  2960. r10_bio = (struct r10bio*) rb2->master_bio;
  2961. rb2->master_bio = NULL;
  2962. put_buf(rb2);
  2963. }
  2964. goto giveup;
  2965. }
  2966. } else {
  2967. /* resync. Schedule a read for every block at this virt offset */
  2968. int count = 0;
  2969. bitmap_cond_end_sync(mddev->bitmap, sector_nr);
  2970. if (!bitmap_start_sync(mddev->bitmap, sector_nr,
  2971. &sync_blocks, mddev->degraded) &&
  2972. !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
  2973. &mddev->recovery)) {
  2974. /* We can skip this block */
  2975. *skipped = 1;
  2976. return sync_blocks + sectors_skipped;
  2977. }
  2978. if (sync_blocks < max_sync)
  2979. max_sync = sync_blocks;
  2980. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  2981. r10_bio->mddev = mddev;
  2982. atomic_set(&r10_bio->remaining, 0);
  2983. raise_barrier(conf, 0);
  2984. conf->next_resync = sector_nr;
  2985. r10_bio->master_bio = NULL;
  2986. r10_bio->sector = sector_nr;
  2987. set_bit(R10BIO_IsSync, &r10_bio->state);
  2988. raid10_find_phys(conf, r10_bio);
  2989. r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
  2990. for (i = 0; i < conf->copies; i++) {
  2991. int d = r10_bio->devs[i].devnum;
  2992. sector_t first_bad, sector;
  2993. int bad_sectors;
  2994. if (r10_bio->devs[i].repl_bio)
  2995. r10_bio->devs[i].repl_bio->bi_end_io = NULL;
  2996. bio = r10_bio->devs[i].bio;
  2997. bio_reset(bio);
  2998. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  2999. if (conf->mirrors[d].rdev == NULL ||
  3000. test_bit(Faulty, &conf->mirrors[d].rdev->flags))
  3001. continue;
  3002. sector = r10_bio->devs[i].addr;
  3003. if (is_badblock(conf->mirrors[d].rdev,
  3004. sector, max_sync,
  3005. &first_bad, &bad_sectors)) {
  3006. if (first_bad > sector)
  3007. max_sync = first_bad - sector;
  3008. else {
  3009. bad_sectors -= (sector - first_bad);
  3010. if (max_sync > bad_sectors)
  3011. max_sync = bad_sectors;
  3012. continue;
  3013. }
  3014. }
  3015. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  3016. atomic_inc(&r10_bio->remaining);
  3017. bio->bi_next = biolist;
  3018. biolist = bio;
  3019. bio->bi_private = r10_bio;
  3020. bio->bi_end_io = end_sync_read;
  3021. bio->bi_rw = READ;
  3022. bio->bi_sector = sector +
  3023. conf->mirrors[d].rdev->data_offset;
  3024. bio->bi_bdev = conf->mirrors[d].rdev->bdev;
  3025. count++;
  3026. if (conf->mirrors[d].replacement == NULL ||
  3027. test_bit(Faulty,
  3028. &conf->mirrors[d].replacement->flags))
  3029. continue;
  3030. /* Need to set up for writing to the replacement */
  3031. bio = r10_bio->devs[i].repl_bio;
  3032. bio_reset(bio);
  3033. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  3034. sector = r10_bio->devs[i].addr;
  3035. atomic_inc(&conf->mirrors[d].rdev->nr_pending);
  3036. bio->bi_next = biolist;
  3037. biolist = bio;
  3038. bio->bi_private = r10_bio;
  3039. bio->bi_end_io = end_sync_write;
  3040. bio->bi_rw = WRITE;
  3041. bio->bi_sector = sector +
  3042. conf->mirrors[d].replacement->data_offset;
  3043. bio->bi_bdev = conf->mirrors[d].replacement->bdev;
  3044. count++;
  3045. }
  3046. if (count < 2) {
  3047. for (i=0; i<conf->copies; i++) {
  3048. int d = r10_bio->devs[i].devnum;
  3049. if (r10_bio->devs[i].bio->bi_end_io)
  3050. rdev_dec_pending(conf->mirrors[d].rdev,
  3051. mddev);
  3052. if (r10_bio->devs[i].repl_bio &&
  3053. r10_bio->devs[i].repl_bio->bi_end_io)
  3054. rdev_dec_pending(
  3055. conf->mirrors[d].replacement,
  3056. mddev);
  3057. }
  3058. put_buf(r10_bio);
  3059. biolist = NULL;
  3060. goto giveup;
  3061. }
  3062. }
  3063. nr_sectors = 0;
  3064. if (sector_nr + max_sync < max_sector)
  3065. max_sector = sector_nr + max_sync;
  3066. do {
  3067. struct page *page;
  3068. int len = PAGE_SIZE;
  3069. if (sector_nr + (len>>9) > max_sector)
  3070. len = (max_sector - sector_nr) << 9;
  3071. if (len == 0)
  3072. break;
  3073. for (bio= biolist ; bio ; bio=bio->bi_next) {
  3074. struct bio *bio2;
  3075. page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
  3076. if (bio_add_page(bio, page, len, 0))
  3077. continue;
  3078. /* stop here */
  3079. bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
  3080. for (bio2 = biolist;
  3081. bio2 && bio2 != bio;
  3082. bio2 = bio2->bi_next) {
  3083. /* remove last page from this bio */
  3084. bio2->bi_vcnt--;
  3085. bio2->bi_size -= len;
  3086. bio2->bi_flags &= ~(1<< BIO_SEG_VALID);
  3087. }
  3088. goto bio_full;
  3089. }
  3090. nr_sectors += len>>9;
  3091. sector_nr += len>>9;
  3092. } while (biolist->bi_vcnt < RESYNC_PAGES);
  3093. bio_full:
  3094. r10_bio->sectors = nr_sectors;
  3095. while (biolist) {
  3096. bio = biolist;
  3097. biolist = biolist->bi_next;
  3098. bio->bi_next = NULL;
  3099. r10_bio = bio->bi_private;
  3100. r10_bio->sectors = nr_sectors;
  3101. if (bio->bi_end_io == end_sync_read) {
  3102. md_sync_acct(bio->bi_bdev, nr_sectors);
  3103. set_bit(BIO_UPTODATE, &bio->bi_flags);
  3104. generic_make_request(bio);
  3105. }
  3106. }
  3107. if (sectors_skipped)
  3108. /* pretend they weren't skipped, it makes
  3109. * no important difference in this case
  3110. */
  3111. md_done_sync(mddev, sectors_skipped, 1);
  3112. return sectors_skipped + nr_sectors;
  3113. giveup:
  3114. /* There is nowhere to write, so all non-sync
  3115. * drives must be failed or in resync, all drives
  3116. * have a bad block, so try the next chunk...
  3117. */
  3118. if (sector_nr + max_sync < max_sector)
  3119. max_sector = sector_nr + max_sync;
  3120. sectors_skipped += (max_sector - sector_nr);
  3121. chunks_skipped ++;
  3122. sector_nr = max_sector;
  3123. goto skipped;
  3124. }
  3125. static sector_t
  3126. raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
  3127. {
  3128. sector_t size;
  3129. struct r10conf *conf = mddev->private;
  3130. if (!raid_disks)
  3131. raid_disks = min(conf->geo.raid_disks,
  3132. conf->prev.raid_disks);
  3133. if (!sectors)
  3134. sectors = conf->dev_sectors;
  3135. size = sectors >> conf->geo.chunk_shift;
  3136. sector_div(size, conf->geo.far_copies);
  3137. size = size * raid_disks;
  3138. sector_div(size, conf->geo.near_copies);
  3139. return size << conf->geo.chunk_shift;
  3140. }
  3141. static void calc_sectors(struct r10conf *conf, sector_t size)
  3142. {
  3143. /* Calculate the number of sectors-per-device that will
  3144. * actually be used, and set conf->dev_sectors and
  3145. * conf->stride
  3146. */
  3147. size = size >> conf->geo.chunk_shift;
  3148. sector_div(size, conf->geo.far_copies);
  3149. size = size * conf->geo.raid_disks;
  3150. sector_div(size, conf->geo.near_copies);
  3151. /* 'size' is now the number of chunks in the array */
  3152. /* calculate "used chunks per device" */
  3153. size = size * conf->copies;
  3154. /* We need to round up when dividing by raid_disks to
  3155. * get the stride size.
  3156. */
  3157. size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
  3158. conf->dev_sectors = size << conf->geo.chunk_shift;
  3159. if (conf->geo.far_offset)
  3160. conf->geo.stride = 1 << conf->geo.chunk_shift;
  3161. else {
  3162. sector_div(size, conf->geo.far_copies);
  3163. conf->geo.stride = size << conf->geo.chunk_shift;
  3164. }
  3165. }
  3166. enum geo_type {geo_new, geo_old, geo_start};
  3167. static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
  3168. {
  3169. int nc, fc, fo;
  3170. int layout, chunk, disks;
  3171. switch (new) {
  3172. case geo_old:
  3173. layout = mddev->layout;
  3174. chunk = mddev->chunk_sectors;
  3175. disks = mddev->raid_disks - mddev->delta_disks;
  3176. break;
  3177. case geo_new:
  3178. layout = mddev->new_layout;
  3179. chunk = mddev->new_chunk_sectors;
  3180. disks = mddev->raid_disks;
  3181. break;
  3182. default: /* avoid 'may be unused' warnings */
  3183. case geo_start: /* new when starting reshape - raid_disks not
  3184. * updated yet. */
  3185. layout = mddev->new_layout;
  3186. chunk = mddev->new_chunk_sectors;
  3187. disks = mddev->raid_disks + mddev->delta_disks;
  3188. break;
  3189. }
  3190. if (layout >> 18)
  3191. return -1;
  3192. if (chunk < (PAGE_SIZE >> 9) ||
  3193. !is_power_of_2(chunk))
  3194. return -2;
  3195. nc = layout & 255;
  3196. fc = (layout >> 8) & 255;
  3197. fo = layout & (1<<16);
  3198. geo->raid_disks = disks;
  3199. geo->near_copies = nc;
  3200. geo->far_copies = fc;
  3201. geo->far_offset = fo;
  3202. geo->far_set_size = (layout & (1<<17)) ? disks / fc : disks;
  3203. geo->chunk_mask = chunk - 1;
  3204. geo->chunk_shift = ffz(~chunk);
  3205. return nc*fc;
  3206. }
  3207. static struct r10conf *setup_conf(struct mddev *mddev)
  3208. {
  3209. struct r10conf *conf = NULL;
  3210. int err = -EINVAL;
  3211. struct geom geo;
  3212. int copies;
  3213. copies = setup_geo(&geo, mddev, geo_new);
  3214. if (copies == -2) {
  3215. printk(KERN_ERR "md/raid10:%s: chunk size must be "
  3216. "at least PAGE_SIZE(%ld) and be a power of 2.\n",
  3217. mdname(mddev), PAGE_SIZE);
  3218. goto out;
  3219. }
  3220. if (copies < 2 || copies > mddev->raid_disks) {
  3221. printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
  3222. mdname(mddev), mddev->new_layout);
  3223. goto out;
  3224. }
  3225. err = -ENOMEM;
  3226. conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
  3227. if (!conf)
  3228. goto out;
  3229. /* FIXME calc properly */
  3230. conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
  3231. max(0,-mddev->delta_disks)),
  3232. GFP_KERNEL);
  3233. if (!conf->mirrors)
  3234. goto out;
  3235. conf->tmppage = alloc_page(GFP_KERNEL);
  3236. if (!conf->tmppage)
  3237. goto out;
  3238. conf->geo = geo;
  3239. conf->copies = copies;
  3240. conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
  3241. r10bio_pool_free, conf);
  3242. if (!conf->r10bio_pool)
  3243. goto out;
  3244. calc_sectors(conf, mddev->dev_sectors);
  3245. if (mddev->reshape_position == MaxSector) {
  3246. conf->prev = conf->geo;
  3247. conf->reshape_progress = MaxSector;
  3248. } else {
  3249. if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
  3250. err = -EINVAL;
  3251. goto out;
  3252. }
  3253. conf->reshape_progress = mddev->reshape_position;
  3254. if (conf->prev.far_offset)
  3255. conf->prev.stride = 1 << conf->prev.chunk_shift;
  3256. else
  3257. /* far_copies must be 1 */
  3258. conf->prev.stride = conf->dev_sectors;
  3259. }
  3260. spin_lock_init(&conf->device_lock);
  3261. INIT_LIST_HEAD(&conf->retry_list);
  3262. spin_lock_init(&conf->resync_lock);
  3263. init_waitqueue_head(&conf->wait_barrier);
  3264. conf->thread = md_register_thread(raid10d, mddev, "raid10");
  3265. if (!conf->thread)
  3266. goto out;
  3267. conf->mddev = mddev;
  3268. return conf;
  3269. out:
  3270. if (err == -ENOMEM)
  3271. printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n",
  3272. mdname(mddev));
  3273. if (conf) {
  3274. if (conf->r10bio_pool)
  3275. mempool_destroy(conf->r10bio_pool);
  3276. kfree(conf->mirrors);
  3277. safe_put_page(conf->tmppage);
  3278. kfree(conf);
  3279. }
  3280. return ERR_PTR(err);
  3281. }
  3282. static int run(struct mddev *mddev)
  3283. {
  3284. struct r10conf *conf;
  3285. int i, disk_idx, chunk_size;
  3286. struct raid10_info *disk;
  3287. struct md_rdev *rdev;
  3288. sector_t size;
  3289. sector_t min_offset_diff = 0;
  3290. int first = 1;
  3291. bool discard_supported = false;
  3292. if (mddev->private == NULL) {
  3293. conf = setup_conf(mddev);
  3294. if (IS_ERR(conf))
  3295. return PTR_ERR(conf);
  3296. mddev->private = conf;
  3297. }
  3298. conf = mddev->private;
  3299. if (!conf)
  3300. goto out;
  3301. mddev->thread = conf->thread;
  3302. conf->thread = NULL;
  3303. chunk_size = mddev->chunk_sectors << 9;
  3304. if (mddev->queue) {
  3305. blk_queue_max_discard_sectors(mddev->queue,
  3306. mddev->chunk_sectors);
  3307. blk_queue_max_write_same_sectors(mddev->queue, 0);
  3308. blk_queue_io_min(mddev->queue, chunk_size);
  3309. if (conf->geo.raid_disks % conf->geo.near_copies)
  3310. blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
  3311. else
  3312. blk_queue_io_opt(mddev->queue, chunk_size *
  3313. (conf->geo.raid_disks / conf->geo.near_copies));
  3314. }
  3315. rdev_for_each(rdev, mddev) {
  3316. long long diff;
  3317. struct request_queue *q;
  3318. disk_idx = rdev->raid_disk;
  3319. if (disk_idx < 0)
  3320. continue;
  3321. if (disk_idx >= conf->geo.raid_disks &&
  3322. disk_idx >= conf->prev.raid_disks)
  3323. continue;
  3324. disk = conf->mirrors + disk_idx;
  3325. if (test_bit(Replacement, &rdev->flags)) {
  3326. if (disk->replacement)
  3327. goto out_free_conf;
  3328. disk->replacement = rdev;
  3329. } else {
  3330. if (disk->rdev)
  3331. goto out_free_conf;
  3332. disk->rdev = rdev;
  3333. }
  3334. q = bdev_get_queue(rdev->bdev);
  3335. if (q->merge_bvec_fn)
  3336. mddev->merge_check_needed = 1;
  3337. diff = (rdev->new_data_offset - rdev->data_offset);
  3338. if (!mddev->reshape_backwards)
  3339. diff = -diff;
  3340. if (diff < 0)
  3341. diff = 0;
  3342. if (first || diff < min_offset_diff)
  3343. min_offset_diff = diff;
  3344. if (mddev->gendisk)
  3345. disk_stack_limits(mddev->gendisk, rdev->bdev,
  3346. rdev->data_offset << 9);
  3347. disk->head_position = 0;
  3348. if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
  3349. discard_supported = true;
  3350. }
  3351. if (mddev->queue) {
  3352. if (discard_supported)
  3353. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
  3354. mddev->queue);
  3355. else
  3356. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
  3357. mddev->queue);
  3358. }
  3359. /* need to check that every block has at least one working mirror */
  3360. if (!enough(conf, -1)) {
  3361. printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n",
  3362. mdname(mddev));
  3363. goto out_free_conf;
  3364. }
  3365. if (conf->reshape_progress != MaxSector) {
  3366. /* must ensure that shape change is supported */
  3367. if (conf->geo.far_copies != 1 &&
  3368. conf->geo.far_offset == 0)
  3369. goto out_free_conf;
  3370. if (conf->prev.far_copies != 1 &&
  3371. conf->prev.far_offset == 0)
  3372. goto out_free_conf;
  3373. }
  3374. mddev->degraded = 0;
  3375. for (i = 0;
  3376. i < conf->geo.raid_disks
  3377. || i < conf->prev.raid_disks;
  3378. i++) {
  3379. disk = conf->mirrors + i;
  3380. if (!disk->rdev && disk->replacement) {
  3381. /* The replacement is all we have - use it */
  3382. disk->rdev = disk->replacement;
  3383. disk->replacement = NULL;
  3384. clear_bit(Replacement, &disk->rdev->flags);
  3385. }
  3386. if (!disk->rdev ||
  3387. !test_bit(In_sync, &disk->rdev->flags)) {
  3388. disk->head_position = 0;
  3389. mddev->degraded++;
  3390. if (disk->rdev)
  3391. conf->fullsync = 1;
  3392. }
  3393. disk->recovery_disabled = mddev->recovery_disabled - 1;
  3394. }
  3395. if (mddev->recovery_cp != MaxSector)
  3396. printk(KERN_NOTICE "md/raid10:%s: not clean"
  3397. " -- starting background reconstruction\n",
  3398. mdname(mddev));
  3399. printk(KERN_INFO
  3400. "md/raid10:%s: active with %d out of %d devices\n",
  3401. mdname(mddev), conf->geo.raid_disks - mddev->degraded,
  3402. conf->geo.raid_disks);
  3403. /*
  3404. * Ok, everything is just fine now
  3405. */
  3406. mddev->dev_sectors = conf->dev_sectors;
  3407. size = raid10_size(mddev, 0, 0);
  3408. md_set_array_sectors(mddev, size);
  3409. mddev->resync_max_sectors = size;
  3410. if (mddev->queue) {
  3411. int stripe = conf->geo.raid_disks *
  3412. ((mddev->chunk_sectors << 9) / PAGE_SIZE);
  3413. mddev->queue->backing_dev_info.congested_fn = raid10_congested;
  3414. mddev->queue->backing_dev_info.congested_data = mddev;
  3415. /* Calculate max read-ahead size.
  3416. * We need to readahead at least twice a whole stripe....
  3417. * maybe...
  3418. */
  3419. stripe /= conf->geo.near_copies;
  3420. if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  3421. mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  3422. blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec);
  3423. }
  3424. if (md_integrity_register(mddev))
  3425. goto out_free_conf;
  3426. if (conf->reshape_progress != MaxSector) {
  3427. unsigned long before_length, after_length;
  3428. before_length = ((1 << conf->prev.chunk_shift) *
  3429. conf->prev.far_copies);
  3430. after_length = ((1 << conf->geo.chunk_shift) *
  3431. conf->geo.far_copies);
  3432. if (max(before_length, after_length) > min_offset_diff) {
  3433. /* This cannot work */
  3434. printk("md/raid10: offset difference not enough to continue reshape\n");
  3435. goto out_free_conf;
  3436. }
  3437. conf->offset_diff = min_offset_diff;
  3438. conf->reshape_safe = conf->reshape_progress;
  3439. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3440. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3441. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3442. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3443. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3444. "reshape");
  3445. }
  3446. return 0;
  3447. out_free_conf:
  3448. md_unregister_thread(&mddev->thread);
  3449. if (conf->r10bio_pool)
  3450. mempool_destroy(conf->r10bio_pool);
  3451. safe_put_page(conf->tmppage);
  3452. kfree(conf->mirrors);
  3453. kfree(conf);
  3454. mddev->private = NULL;
  3455. out:
  3456. return -EIO;
  3457. }
  3458. static int stop(struct mddev *mddev)
  3459. {
  3460. struct r10conf *conf = mddev->private;
  3461. raise_barrier(conf, 0);
  3462. lower_barrier(conf);
  3463. md_unregister_thread(&mddev->thread);
  3464. if (mddev->queue)
  3465. /* the unplug fn references 'conf'*/
  3466. blk_sync_queue(mddev->queue);
  3467. if (conf->r10bio_pool)
  3468. mempool_destroy(conf->r10bio_pool);
  3469. safe_put_page(conf->tmppage);
  3470. kfree(conf->mirrors);
  3471. kfree(conf);
  3472. mddev->private = NULL;
  3473. return 0;
  3474. }
  3475. static void raid10_quiesce(struct mddev *mddev, int state)
  3476. {
  3477. struct r10conf *conf = mddev->private;
  3478. switch(state) {
  3479. case 1:
  3480. raise_barrier(conf, 0);
  3481. break;
  3482. case 0:
  3483. lower_barrier(conf);
  3484. break;
  3485. }
  3486. }
  3487. static int raid10_resize(struct mddev *mddev, sector_t sectors)
  3488. {
  3489. /* Resize of 'far' arrays is not supported.
  3490. * For 'near' and 'offset' arrays we can set the
  3491. * number of sectors used to be an appropriate multiple
  3492. * of the chunk size.
  3493. * For 'offset', this is far_copies*chunksize.
  3494. * For 'near' the multiplier is the LCM of
  3495. * near_copies and raid_disks.
  3496. * So if far_copies > 1 && !far_offset, fail.
  3497. * Else find LCM(raid_disks, near_copy)*far_copies and
  3498. * multiply by chunk_size. Then round to this number.
  3499. * This is mostly done by raid10_size()
  3500. */
  3501. struct r10conf *conf = mddev->private;
  3502. sector_t oldsize, size;
  3503. if (mddev->reshape_position != MaxSector)
  3504. return -EBUSY;
  3505. if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
  3506. return -EINVAL;
  3507. oldsize = raid10_size(mddev, 0, 0);
  3508. size = raid10_size(mddev, sectors, 0);
  3509. if (mddev->external_size &&
  3510. mddev->array_sectors > size)
  3511. return -EINVAL;
  3512. if (mddev->bitmap) {
  3513. int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
  3514. if (ret)
  3515. return ret;
  3516. }
  3517. md_set_array_sectors(mddev, size);
  3518. set_capacity(mddev->gendisk, mddev->array_sectors);
  3519. revalidate_disk(mddev->gendisk);
  3520. if (sectors > mddev->dev_sectors &&
  3521. mddev->recovery_cp > oldsize) {
  3522. mddev->recovery_cp = oldsize;
  3523. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  3524. }
  3525. calc_sectors(conf, sectors);
  3526. mddev->dev_sectors = conf->dev_sectors;
  3527. mddev->resync_max_sectors = size;
  3528. return 0;
  3529. }
  3530. static void *raid10_takeover_raid0(struct mddev *mddev)
  3531. {
  3532. struct md_rdev *rdev;
  3533. struct r10conf *conf;
  3534. if (mddev->degraded > 0) {
  3535. printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n",
  3536. mdname(mddev));
  3537. return ERR_PTR(-EINVAL);
  3538. }
  3539. /* Set new parameters */
  3540. mddev->new_level = 10;
  3541. /* new layout: far_copies = 1, near_copies = 2 */
  3542. mddev->new_layout = (1<<8) + 2;
  3543. mddev->new_chunk_sectors = mddev->chunk_sectors;
  3544. mddev->delta_disks = mddev->raid_disks;
  3545. mddev->raid_disks *= 2;
  3546. /* make sure it will be not marked as dirty */
  3547. mddev->recovery_cp = MaxSector;
  3548. conf = setup_conf(mddev);
  3549. if (!IS_ERR(conf)) {
  3550. rdev_for_each(rdev, mddev)
  3551. if (rdev->raid_disk >= 0)
  3552. rdev->new_raid_disk = rdev->raid_disk * 2;
  3553. conf->barrier = 1;
  3554. }
  3555. return conf;
  3556. }
  3557. static void *raid10_takeover(struct mddev *mddev)
  3558. {
  3559. struct r0conf *raid0_conf;
  3560. /* raid10 can take over:
  3561. * raid0 - providing it has only two drives
  3562. */
  3563. if (mddev->level == 0) {
  3564. /* for raid0 takeover only one zone is supported */
  3565. raid0_conf = mddev->private;
  3566. if (raid0_conf->nr_strip_zones > 1) {
  3567. printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0"
  3568. " with more than one zone.\n",
  3569. mdname(mddev));
  3570. return ERR_PTR(-EINVAL);
  3571. }
  3572. return raid10_takeover_raid0(mddev);
  3573. }
  3574. return ERR_PTR(-EINVAL);
  3575. }
  3576. static int raid10_check_reshape(struct mddev *mddev)
  3577. {
  3578. /* Called when there is a request to change
  3579. * - layout (to ->new_layout)
  3580. * - chunk size (to ->new_chunk_sectors)
  3581. * - raid_disks (by delta_disks)
  3582. * or when trying to restart a reshape that was ongoing.
  3583. *
  3584. * We need to validate the request and possibly allocate
  3585. * space if that might be an issue later.
  3586. *
  3587. * Currently we reject any reshape of a 'far' mode array,
  3588. * allow chunk size to change if new is generally acceptable,
  3589. * allow raid_disks to increase, and allow
  3590. * a switch between 'near' mode and 'offset' mode.
  3591. */
  3592. struct r10conf *conf = mddev->private;
  3593. struct geom geo;
  3594. if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
  3595. return -EINVAL;
  3596. if (setup_geo(&geo, mddev, geo_start) != conf->copies)
  3597. /* mustn't change number of copies */
  3598. return -EINVAL;
  3599. if (geo.far_copies > 1 && !geo.far_offset)
  3600. /* Cannot switch to 'far' mode */
  3601. return -EINVAL;
  3602. if (mddev->array_sectors & geo.chunk_mask)
  3603. /* not factor of array size */
  3604. return -EINVAL;
  3605. if (!enough(conf, -1))
  3606. return -EINVAL;
  3607. kfree(conf->mirrors_new);
  3608. conf->mirrors_new = NULL;
  3609. if (mddev->delta_disks > 0) {
  3610. /* allocate new 'mirrors' list */
  3611. conf->mirrors_new = kzalloc(
  3612. sizeof(struct raid10_info)
  3613. *(mddev->raid_disks +
  3614. mddev->delta_disks),
  3615. GFP_KERNEL);
  3616. if (!conf->mirrors_new)
  3617. return -ENOMEM;
  3618. }
  3619. return 0;
  3620. }
  3621. /*
  3622. * Need to check if array has failed when deciding whether to:
  3623. * - start an array
  3624. * - remove non-faulty devices
  3625. * - add a spare
  3626. * - allow a reshape
  3627. * This determination is simple when no reshape is happening.
  3628. * However if there is a reshape, we need to carefully check
  3629. * both the before and after sections.
  3630. * This is because some failed devices may only affect one
  3631. * of the two sections, and some non-in_sync devices may
  3632. * be insync in the section most affected by failed devices.
  3633. */
  3634. static int calc_degraded(struct r10conf *conf)
  3635. {
  3636. int degraded, degraded2;
  3637. int i;
  3638. rcu_read_lock();
  3639. degraded = 0;
  3640. /* 'prev' section first */
  3641. for (i = 0; i < conf->prev.raid_disks; i++) {
  3642. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  3643. if (!rdev || test_bit(Faulty, &rdev->flags))
  3644. degraded++;
  3645. else if (!test_bit(In_sync, &rdev->flags))
  3646. /* When we can reduce the number of devices in
  3647. * an array, this might not contribute to
  3648. * 'degraded'. It does now.
  3649. */
  3650. degraded++;
  3651. }
  3652. rcu_read_unlock();
  3653. if (conf->geo.raid_disks == conf->prev.raid_disks)
  3654. return degraded;
  3655. rcu_read_lock();
  3656. degraded2 = 0;
  3657. for (i = 0; i < conf->geo.raid_disks; i++) {
  3658. struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
  3659. if (!rdev || test_bit(Faulty, &rdev->flags))
  3660. degraded2++;
  3661. else if (!test_bit(In_sync, &rdev->flags)) {
  3662. /* If reshape is increasing the number of devices,
  3663. * this section has already been recovered, so
  3664. * it doesn't contribute to degraded.
  3665. * else it does.
  3666. */
  3667. if (conf->geo.raid_disks <= conf->prev.raid_disks)
  3668. degraded2++;
  3669. }
  3670. }
  3671. rcu_read_unlock();
  3672. if (degraded2 > degraded)
  3673. return degraded2;
  3674. return degraded;
  3675. }
  3676. static int raid10_start_reshape(struct mddev *mddev)
  3677. {
  3678. /* A 'reshape' has been requested. This commits
  3679. * the various 'new' fields and sets MD_RECOVER_RESHAPE
  3680. * This also checks if there are enough spares and adds them
  3681. * to the array.
  3682. * We currently require enough spares to make the final
  3683. * array non-degraded. We also require that the difference
  3684. * between old and new data_offset - on each device - is
  3685. * enough that we never risk over-writing.
  3686. */
  3687. unsigned long before_length, after_length;
  3688. sector_t min_offset_diff = 0;
  3689. int first = 1;
  3690. struct geom new;
  3691. struct r10conf *conf = mddev->private;
  3692. struct md_rdev *rdev;
  3693. int spares = 0;
  3694. int ret;
  3695. if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
  3696. return -EBUSY;
  3697. if (setup_geo(&new, mddev, geo_start) != conf->copies)
  3698. return -EINVAL;
  3699. before_length = ((1 << conf->prev.chunk_shift) *
  3700. conf->prev.far_copies);
  3701. after_length = ((1 << conf->geo.chunk_shift) *
  3702. conf->geo.far_copies);
  3703. rdev_for_each(rdev, mddev) {
  3704. if (!test_bit(In_sync, &rdev->flags)
  3705. && !test_bit(Faulty, &rdev->flags))
  3706. spares++;
  3707. if (rdev->raid_disk >= 0) {
  3708. long long diff = (rdev->new_data_offset
  3709. - rdev->data_offset);
  3710. if (!mddev->reshape_backwards)
  3711. diff = -diff;
  3712. if (diff < 0)
  3713. diff = 0;
  3714. if (first || diff < min_offset_diff)
  3715. min_offset_diff = diff;
  3716. }
  3717. }
  3718. if (max(before_length, after_length) > min_offset_diff)
  3719. return -EINVAL;
  3720. if (spares < mddev->delta_disks)
  3721. return -EINVAL;
  3722. conf->offset_diff = min_offset_diff;
  3723. spin_lock_irq(&conf->device_lock);
  3724. if (conf->mirrors_new) {
  3725. memcpy(conf->mirrors_new, conf->mirrors,
  3726. sizeof(struct raid10_info)*conf->prev.raid_disks);
  3727. smp_mb();
  3728. kfree(conf->mirrors_old); /* FIXME and elsewhere */
  3729. conf->mirrors_old = conf->mirrors;
  3730. conf->mirrors = conf->mirrors_new;
  3731. conf->mirrors_new = NULL;
  3732. }
  3733. setup_geo(&conf->geo, mddev, geo_start);
  3734. smp_mb();
  3735. if (mddev->reshape_backwards) {
  3736. sector_t size = raid10_size(mddev, 0, 0);
  3737. if (size < mddev->array_sectors) {
  3738. spin_unlock_irq(&conf->device_lock);
  3739. printk(KERN_ERR "md/raid10:%s: array size must be reduce before number of disks\n",
  3740. mdname(mddev));
  3741. return -EINVAL;
  3742. }
  3743. mddev->resync_max_sectors = size;
  3744. conf->reshape_progress = size;
  3745. } else
  3746. conf->reshape_progress = 0;
  3747. spin_unlock_irq(&conf->device_lock);
  3748. if (mddev->delta_disks && mddev->bitmap) {
  3749. ret = bitmap_resize(mddev->bitmap,
  3750. raid10_size(mddev, 0,
  3751. conf->geo.raid_disks),
  3752. 0, 0);
  3753. if (ret)
  3754. goto abort;
  3755. }
  3756. if (mddev->delta_disks > 0) {
  3757. rdev_for_each(rdev, mddev)
  3758. if (rdev->raid_disk < 0 &&
  3759. !test_bit(Faulty, &rdev->flags)) {
  3760. if (raid10_add_disk(mddev, rdev) == 0) {
  3761. if (rdev->raid_disk >=
  3762. conf->prev.raid_disks)
  3763. set_bit(In_sync, &rdev->flags);
  3764. else
  3765. rdev->recovery_offset = 0;
  3766. if (sysfs_link_rdev(mddev, rdev))
  3767. /* Failure here is OK */;
  3768. }
  3769. } else if (rdev->raid_disk >= conf->prev.raid_disks
  3770. && !test_bit(Faulty, &rdev->flags)) {
  3771. /* This is a spare that was manually added */
  3772. set_bit(In_sync, &rdev->flags);
  3773. }
  3774. }
  3775. /* When a reshape changes the number of devices,
  3776. * ->degraded is measured against the larger of the
  3777. * pre and post numbers.
  3778. */
  3779. spin_lock_irq(&conf->device_lock);
  3780. mddev->degraded = calc_degraded(conf);
  3781. spin_unlock_irq(&conf->device_lock);
  3782. mddev->raid_disks = conf->geo.raid_disks;
  3783. mddev->reshape_position = conf->reshape_progress;
  3784. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3785. clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
  3786. clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
  3787. set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
  3788. set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
  3789. mddev->sync_thread = md_register_thread(md_do_sync, mddev,
  3790. "reshape");
  3791. if (!mddev->sync_thread) {
  3792. ret = -EAGAIN;
  3793. goto abort;
  3794. }
  3795. conf->reshape_checkpoint = jiffies;
  3796. md_wakeup_thread(mddev->sync_thread);
  3797. md_new_event(mddev);
  3798. return 0;
  3799. abort:
  3800. mddev->recovery = 0;
  3801. spin_lock_irq(&conf->device_lock);
  3802. conf->geo = conf->prev;
  3803. mddev->raid_disks = conf->geo.raid_disks;
  3804. rdev_for_each(rdev, mddev)
  3805. rdev->new_data_offset = rdev->data_offset;
  3806. smp_wmb();
  3807. conf->reshape_progress = MaxSector;
  3808. mddev->reshape_position = MaxSector;
  3809. spin_unlock_irq(&conf->device_lock);
  3810. return ret;
  3811. }
  3812. /* Calculate the last device-address that could contain
  3813. * any block from the chunk that includes the array-address 's'
  3814. * and report the next address.
  3815. * i.e. the address returned will be chunk-aligned and after
  3816. * any data that is in the chunk containing 's'.
  3817. */
  3818. static sector_t last_dev_address(sector_t s, struct geom *geo)
  3819. {
  3820. s = (s | geo->chunk_mask) + 1;
  3821. s >>= geo->chunk_shift;
  3822. s *= geo->near_copies;
  3823. s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
  3824. s *= geo->far_copies;
  3825. s <<= geo->chunk_shift;
  3826. return s;
  3827. }
  3828. /* Calculate the first device-address that could contain
  3829. * any block from the chunk that includes the array-address 's'.
  3830. * This too will be the start of a chunk
  3831. */
  3832. static sector_t first_dev_address(sector_t s, struct geom *geo)
  3833. {
  3834. s >>= geo->chunk_shift;
  3835. s *= geo->near_copies;
  3836. sector_div(s, geo->raid_disks);
  3837. s *= geo->far_copies;
  3838. s <<= geo->chunk_shift;
  3839. return s;
  3840. }
  3841. static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
  3842. int *skipped)
  3843. {
  3844. /* We simply copy at most one chunk (smallest of old and new)
  3845. * at a time, possibly less if that exceeds RESYNC_PAGES,
  3846. * or we hit a bad block or something.
  3847. * This might mean we pause for normal IO in the middle of
  3848. * a chunk, but that is not a problem was mddev->reshape_position
  3849. * can record any location.
  3850. *
  3851. * If we will want to write to a location that isn't
  3852. * yet recorded as 'safe' (i.e. in metadata on disk) then
  3853. * we need to flush all reshape requests and update the metadata.
  3854. *
  3855. * When reshaping forwards (e.g. to more devices), we interpret
  3856. * 'safe' as the earliest block which might not have been copied
  3857. * down yet. We divide this by previous stripe size and multiply
  3858. * by previous stripe length to get lowest device offset that we
  3859. * cannot write to yet.
  3860. * We interpret 'sector_nr' as an address that we want to write to.
  3861. * From this we use last_device_address() to find where we might
  3862. * write to, and first_device_address on the 'safe' position.
  3863. * If this 'next' write position is after the 'safe' position,
  3864. * we must update the metadata to increase the 'safe' position.
  3865. *
  3866. * When reshaping backwards, we round in the opposite direction
  3867. * and perform the reverse test: next write position must not be
  3868. * less than current safe position.
  3869. *
  3870. * In all this the minimum difference in data offsets
  3871. * (conf->offset_diff - always positive) allows a bit of slack,
  3872. * so next can be after 'safe', but not by more than offset_disk
  3873. *
  3874. * We need to prepare all the bios here before we start any IO
  3875. * to ensure the size we choose is acceptable to all devices.
  3876. * The means one for each copy for write-out and an extra one for
  3877. * read-in.
  3878. * We store the read-in bio in ->master_bio and the others in
  3879. * ->devs[x].bio and ->devs[x].repl_bio.
  3880. */
  3881. struct r10conf *conf = mddev->private;
  3882. struct r10bio *r10_bio;
  3883. sector_t next, safe, last;
  3884. int max_sectors;
  3885. int nr_sectors;
  3886. int s;
  3887. struct md_rdev *rdev;
  3888. int need_flush = 0;
  3889. struct bio *blist;
  3890. struct bio *bio, *read_bio;
  3891. int sectors_done = 0;
  3892. if (sector_nr == 0) {
  3893. /* If restarting in the middle, skip the initial sectors */
  3894. if (mddev->reshape_backwards &&
  3895. conf->reshape_progress < raid10_size(mddev, 0, 0)) {
  3896. sector_nr = (raid10_size(mddev, 0, 0)
  3897. - conf->reshape_progress);
  3898. } else if (!mddev->reshape_backwards &&
  3899. conf->reshape_progress > 0)
  3900. sector_nr = conf->reshape_progress;
  3901. if (sector_nr) {
  3902. mddev->curr_resync_completed = sector_nr;
  3903. sysfs_notify(&mddev->kobj, NULL, "sync_completed");
  3904. *skipped = 1;
  3905. return sector_nr;
  3906. }
  3907. }
  3908. /* We don't use sector_nr to track where we are up to
  3909. * as that doesn't work well for ->reshape_backwards.
  3910. * So just use ->reshape_progress.
  3911. */
  3912. if (mddev->reshape_backwards) {
  3913. /* 'next' is the earliest device address that we might
  3914. * write to for this chunk in the new layout
  3915. */
  3916. next = first_dev_address(conf->reshape_progress - 1,
  3917. &conf->geo);
  3918. /* 'safe' is the last device address that we might read from
  3919. * in the old layout after a restart
  3920. */
  3921. safe = last_dev_address(conf->reshape_safe - 1,
  3922. &conf->prev);
  3923. if (next + conf->offset_diff < safe)
  3924. need_flush = 1;
  3925. last = conf->reshape_progress - 1;
  3926. sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
  3927. & conf->prev.chunk_mask);
  3928. if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
  3929. sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
  3930. } else {
  3931. /* 'next' is after the last device address that we
  3932. * might write to for this chunk in the new layout
  3933. */
  3934. next = last_dev_address(conf->reshape_progress, &conf->geo);
  3935. /* 'safe' is the earliest device address that we might
  3936. * read from in the old layout after a restart
  3937. */
  3938. safe = first_dev_address(conf->reshape_safe, &conf->prev);
  3939. /* Need to update metadata if 'next' might be beyond 'safe'
  3940. * as that would possibly corrupt data
  3941. */
  3942. if (next > safe + conf->offset_diff)
  3943. need_flush = 1;
  3944. sector_nr = conf->reshape_progress;
  3945. last = sector_nr | (conf->geo.chunk_mask
  3946. & conf->prev.chunk_mask);
  3947. if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
  3948. last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
  3949. }
  3950. if (need_flush ||
  3951. time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
  3952. /* Need to update reshape_position in metadata */
  3953. wait_barrier(conf);
  3954. mddev->reshape_position = conf->reshape_progress;
  3955. if (mddev->reshape_backwards)
  3956. mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
  3957. - conf->reshape_progress;
  3958. else
  3959. mddev->curr_resync_completed = conf->reshape_progress;
  3960. conf->reshape_checkpoint = jiffies;
  3961. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  3962. md_wakeup_thread(mddev->thread);
  3963. wait_event(mddev->sb_wait, mddev->flags == 0 ||
  3964. kthread_should_stop());
  3965. conf->reshape_safe = mddev->reshape_position;
  3966. allow_barrier(conf);
  3967. }
  3968. read_more:
  3969. /* Now schedule reads for blocks from sector_nr to last */
  3970. r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
  3971. raise_barrier(conf, sectors_done != 0);
  3972. atomic_set(&r10_bio->remaining, 0);
  3973. r10_bio->mddev = mddev;
  3974. r10_bio->sector = sector_nr;
  3975. set_bit(R10BIO_IsReshape, &r10_bio->state);
  3976. r10_bio->sectors = last - sector_nr + 1;
  3977. rdev = read_balance(conf, r10_bio, &max_sectors);
  3978. BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
  3979. if (!rdev) {
  3980. /* Cannot read from here, so need to record bad blocks
  3981. * on all the target devices.
  3982. */
  3983. // FIXME
  3984. set_bit(MD_RECOVERY_INTR, &mddev->recovery);
  3985. return sectors_done;
  3986. }
  3987. read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
  3988. read_bio->bi_bdev = rdev->bdev;
  3989. read_bio->bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
  3990. + rdev->data_offset);
  3991. read_bio->bi_private = r10_bio;
  3992. read_bio->bi_end_io = end_sync_read;
  3993. read_bio->bi_rw = READ;
  3994. read_bio->bi_flags &= ~(BIO_POOL_MASK - 1);
  3995. read_bio->bi_flags |= 1 << BIO_UPTODATE;
  3996. read_bio->bi_vcnt = 0;
  3997. read_bio->bi_size = 0;
  3998. r10_bio->master_bio = read_bio;
  3999. r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
  4000. /* Now find the locations in the new layout */
  4001. __raid10_find_phys(&conf->geo, r10_bio);
  4002. blist = read_bio;
  4003. read_bio->bi_next = NULL;
  4004. for (s = 0; s < conf->copies*2; s++) {
  4005. struct bio *b;
  4006. int d = r10_bio->devs[s/2].devnum;
  4007. struct md_rdev *rdev2;
  4008. if (s&1) {
  4009. rdev2 = conf->mirrors[d].replacement;
  4010. b = r10_bio->devs[s/2].repl_bio;
  4011. } else {
  4012. rdev2 = conf->mirrors[d].rdev;
  4013. b = r10_bio->devs[s/2].bio;
  4014. }
  4015. if (!rdev2 || test_bit(Faulty, &rdev2->flags))
  4016. continue;
  4017. bio_reset(b);
  4018. b->bi_bdev = rdev2->bdev;
  4019. b->bi_sector = r10_bio->devs[s/2].addr + rdev2->new_data_offset;
  4020. b->bi_private = r10_bio;
  4021. b->bi_end_io = end_reshape_write;
  4022. b->bi_rw = WRITE;
  4023. b->bi_next = blist;
  4024. blist = b;
  4025. }
  4026. /* Now add as many pages as possible to all of these bios. */
  4027. nr_sectors = 0;
  4028. for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
  4029. struct page *page = r10_bio->devs[0].bio->bi_io_vec[s/(PAGE_SIZE>>9)].bv_page;
  4030. int len = (max_sectors - s) << 9;
  4031. if (len > PAGE_SIZE)
  4032. len = PAGE_SIZE;
  4033. for (bio = blist; bio ; bio = bio->bi_next) {
  4034. struct bio *bio2;
  4035. if (bio_add_page(bio, page, len, 0))
  4036. continue;
  4037. /* Didn't fit, must stop */
  4038. for (bio2 = blist;
  4039. bio2 && bio2 != bio;
  4040. bio2 = bio2->bi_next) {
  4041. /* Remove last page from this bio */
  4042. bio2->bi_vcnt--;
  4043. bio2->bi_size -= len;
  4044. bio2->bi_flags &= ~(1<<BIO_SEG_VALID);
  4045. }
  4046. goto bio_full;
  4047. }
  4048. sector_nr += len >> 9;
  4049. nr_sectors += len >> 9;
  4050. }
  4051. bio_full:
  4052. r10_bio->sectors = nr_sectors;
  4053. /* Now submit the read */
  4054. md_sync_acct(read_bio->bi_bdev, r10_bio->sectors);
  4055. atomic_inc(&r10_bio->remaining);
  4056. read_bio->bi_next = NULL;
  4057. generic_make_request(read_bio);
  4058. sector_nr += nr_sectors;
  4059. sectors_done += nr_sectors;
  4060. if (sector_nr <= last)
  4061. goto read_more;
  4062. /* Now that we have done the whole section we can
  4063. * update reshape_progress
  4064. */
  4065. if (mddev->reshape_backwards)
  4066. conf->reshape_progress -= sectors_done;
  4067. else
  4068. conf->reshape_progress += sectors_done;
  4069. return sectors_done;
  4070. }
  4071. static void end_reshape_request(struct r10bio *r10_bio);
  4072. static int handle_reshape_read_error(struct mddev *mddev,
  4073. struct r10bio *r10_bio);
  4074. static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
  4075. {
  4076. /* Reshape read completed. Hopefully we have a block
  4077. * to write out.
  4078. * If we got a read error then we do sync 1-page reads from
  4079. * elsewhere until we find the data - or give up.
  4080. */
  4081. struct r10conf *conf = mddev->private;
  4082. int s;
  4083. if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
  4084. if (handle_reshape_read_error(mddev, r10_bio) < 0) {
  4085. /* Reshape has been aborted */
  4086. md_done_sync(mddev, r10_bio->sectors, 0);
  4087. return;
  4088. }
  4089. /* We definitely have the data in the pages, schedule the
  4090. * writes.
  4091. */
  4092. atomic_set(&r10_bio->remaining, 1);
  4093. for (s = 0; s < conf->copies*2; s++) {
  4094. struct bio *b;
  4095. int d = r10_bio->devs[s/2].devnum;
  4096. struct md_rdev *rdev;
  4097. if (s&1) {
  4098. rdev = conf->mirrors[d].replacement;
  4099. b = r10_bio->devs[s/2].repl_bio;
  4100. } else {
  4101. rdev = conf->mirrors[d].rdev;
  4102. b = r10_bio->devs[s/2].bio;
  4103. }
  4104. if (!rdev || test_bit(Faulty, &rdev->flags))
  4105. continue;
  4106. atomic_inc(&rdev->nr_pending);
  4107. md_sync_acct(b->bi_bdev, r10_bio->sectors);
  4108. atomic_inc(&r10_bio->remaining);
  4109. b->bi_next = NULL;
  4110. generic_make_request(b);
  4111. }
  4112. end_reshape_request(r10_bio);
  4113. }
  4114. static void end_reshape(struct r10conf *conf)
  4115. {
  4116. if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
  4117. return;
  4118. spin_lock_irq(&conf->device_lock);
  4119. conf->prev = conf->geo;
  4120. md_finish_reshape(conf->mddev);
  4121. smp_wmb();
  4122. conf->reshape_progress = MaxSector;
  4123. spin_unlock_irq(&conf->device_lock);
  4124. /* read-ahead size must cover two whole stripes, which is
  4125. * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
  4126. */
  4127. if (conf->mddev->queue) {
  4128. int stripe = conf->geo.raid_disks *
  4129. ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
  4130. stripe /= conf->geo.near_copies;
  4131. if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
  4132. conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
  4133. }
  4134. conf->fullsync = 0;
  4135. }
  4136. static int handle_reshape_read_error(struct mddev *mddev,
  4137. struct r10bio *r10_bio)
  4138. {
  4139. /* Use sync reads to get the blocks from somewhere else */
  4140. int sectors = r10_bio->sectors;
  4141. struct r10conf *conf = mddev->private;
  4142. struct {
  4143. struct r10bio r10_bio;
  4144. struct r10dev devs[conf->copies];
  4145. } on_stack;
  4146. struct r10bio *r10b = &on_stack.r10_bio;
  4147. int slot = 0;
  4148. int idx = 0;
  4149. struct bio_vec *bvec = r10_bio->master_bio->bi_io_vec;
  4150. r10b->sector = r10_bio->sector;
  4151. __raid10_find_phys(&conf->prev, r10b);
  4152. while (sectors) {
  4153. int s = sectors;
  4154. int success = 0;
  4155. int first_slot = slot;
  4156. if (s > (PAGE_SIZE >> 9))
  4157. s = PAGE_SIZE >> 9;
  4158. while (!success) {
  4159. int d = r10b->devs[slot].devnum;
  4160. struct md_rdev *rdev = conf->mirrors[d].rdev;
  4161. sector_t addr;
  4162. if (rdev == NULL ||
  4163. test_bit(Faulty, &rdev->flags) ||
  4164. !test_bit(In_sync, &rdev->flags))
  4165. goto failed;
  4166. addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
  4167. success = sync_page_io(rdev,
  4168. addr,
  4169. s << 9,
  4170. bvec[idx].bv_page,
  4171. READ, false);
  4172. if (success)
  4173. break;
  4174. failed:
  4175. slot++;
  4176. if (slot >= conf->copies)
  4177. slot = 0;
  4178. if (slot == first_slot)
  4179. break;
  4180. }
  4181. if (!success) {
  4182. /* couldn't read this block, must give up */
  4183. set_bit(MD_RECOVERY_INTR,
  4184. &mddev->recovery);
  4185. return -EIO;
  4186. }
  4187. sectors -= s;
  4188. idx++;
  4189. }
  4190. return 0;
  4191. }
  4192. static void end_reshape_write(struct bio *bio, int error)
  4193. {
  4194. int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  4195. struct r10bio *r10_bio = bio->bi_private;
  4196. struct mddev *mddev = r10_bio->mddev;
  4197. struct r10conf *conf = mddev->private;
  4198. int d;
  4199. int slot;
  4200. int repl;
  4201. struct md_rdev *rdev = NULL;
  4202. d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
  4203. if (repl)
  4204. rdev = conf->mirrors[d].replacement;
  4205. if (!rdev) {
  4206. smp_mb();
  4207. rdev = conf->mirrors[d].rdev;
  4208. }
  4209. if (!uptodate) {
  4210. /* FIXME should record badblock */
  4211. md_error(mddev, rdev);
  4212. }
  4213. rdev_dec_pending(rdev, mddev);
  4214. end_reshape_request(r10_bio);
  4215. }
  4216. static void end_reshape_request(struct r10bio *r10_bio)
  4217. {
  4218. if (!atomic_dec_and_test(&r10_bio->remaining))
  4219. return;
  4220. md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
  4221. bio_put(r10_bio->master_bio);
  4222. put_buf(r10_bio);
  4223. }
  4224. static void raid10_finish_reshape(struct mddev *mddev)
  4225. {
  4226. struct r10conf *conf = mddev->private;
  4227. if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
  4228. return;
  4229. if (mddev->delta_disks > 0) {
  4230. sector_t size = raid10_size(mddev, 0, 0);
  4231. md_set_array_sectors(mddev, size);
  4232. if (mddev->recovery_cp > mddev->resync_max_sectors) {
  4233. mddev->recovery_cp = mddev->resync_max_sectors;
  4234. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  4235. }
  4236. mddev->resync_max_sectors = size;
  4237. set_capacity(mddev->gendisk, mddev->array_sectors);
  4238. revalidate_disk(mddev->gendisk);
  4239. } else {
  4240. int d;
  4241. for (d = conf->geo.raid_disks ;
  4242. d < conf->geo.raid_disks - mddev->delta_disks;
  4243. d++) {
  4244. struct md_rdev *rdev = conf->mirrors[d].rdev;
  4245. if (rdev)
  4246. clear_bit(In_sync, &rdev->flags);
  4247. rdev = conf->mirrors[d].replacement;
  4248. if (rdev)
  4249. clear_bit(In_sync, &rdev->flags);
  4250. }
  4251. }
  4252. mddev->layout = mddev->new_layout;
  4253. mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
  4254. mddev->reshape_position = MaxSector;
  4255. mddev->delta_disks = 0;
  4256. mddev->reshape_backwards = 0;
  4257. }
  4258. static struct md_personality raid10_personality =
  4259. {
  4260. .name = "raid10",
  4261. .level = 10,
  4262. .owner = THIS_MODULE,
  4263. .make_request = make_request,
  4264. .run = run,
  4265. .stop = stop,
  4266. .status = status,
  4267. .error_handler = error,
  4268. .hot_add_disk = raid10_add_disk,
  4269. .hot_remove_disk= raid10_remove_disk,
  4270. .spare_active = raid10_spare_active,
  4271. .sync_request = sync_request,
  4272. .quiesce = raid10_quiesce,
  4273. .size = raid10_size,
  4274. .resize = raid10_resize,
  4275. .takeover = raid10_takeover,
  4276. .check_reshape = raid10_check_reshape,
  4277. .start_reshape = raid10_start_reshape,
  4278. .finish_reshape = raid10_finish_reshape,
  4279. };
  4280. static int __init raid_init(void)
  4281. {
  4282. return register_md_personality(&raid10_personality);
  4283. }
  4284. static void raid_exit(void)
  4285. {
  4286. unregister_md_personality(&raid10_personality);
  4287. }
  4288. module_init(raid_init);
  4289. module_exit(raid_exit);
  4290. MODULE_LICENSE("GPL");
  4291. MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
  4292. MODULE_ALIAS("md-personality-9"); /* RAID10 */
  4293. MODULE_ALIAS("md-raid10");
  4294. MODULE_ALIAS("md-level-10");
  4295. module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);