raid10.c 131 KB

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