raid10.c 130 KB

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