raid10.c 130 KB

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