raid10.c 125 KB

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