raid10.c 128 KB

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