raid10.c 140 KB

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