raid10.c 131 KB

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