raid10.c 145 KB

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