raid10.c 128 KB

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