raid10.c 124 KB

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