raid10.c 131 KB

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