raid10.c 129 KB

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