volumes.c 155 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/bio.h>
  20. #include <linux/slab.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/random.h>
  24. #include <linux/iocontext.h>
  25. #include <linux/capability.h>
  26. #include <linux/ratelimit.h>
  27. #include <linux/kthread.h>
  28. #include <linux/raid/pq.h>
  29. #include <asm/div64.h>
  30. #include "compat.h"
  31. #include "ctree.h"
  32. #include "extent_map.h"
  33. #include "disk-io.h"
  34. #include "transaction.h"
  35. #include "print-tree.h"
  36. #include "volumes.h"
  37. #include "raid56.h"
  38. #include "async-thread.h"
  39. #include "check-integrity.h"
  40. #include "rcu-string.h"
  41. #include "math.h"
  42. #include "dev-replace.h"
  43. static int init_first_rw_device(struct btrfs_trans_handle *trans,
  44. struct btrfs_root *root,
  45. struct btrfs_device *device);
  46. static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
  47. static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
  48. static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
  49. static DEFINE_MUTEX(uuid_mutex);
  50. static LIST_HEAD(fs_uuids);
  51. static void lock_chunks(struct btrfs_root *root)
  52. {
  53. mutex_lock(&root->fs_info->chunk_mutex);
  54. }
  55. static void unlock_chunks(struct btrfs_root *root)
  56. {
  57. mutex_unlock(&root->fs_info->chunk_mutex);
  58. }
  59. static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
  60. {
  61. struct btrfs_device *device;
  62. WARN_ON(fs_devices->opened);
  63. while (!list_empty(&fs_devices->devices)) {
  64. device = list_entry(fs_devices->devices.next,
  65. struct btrfs_device, dev_list);
  66. list_del(&device->dev_list);
  67. rcu_string_free(device->name);
  68. kfree(device);
  69. }
  70. kfree(fs_devices);
  71. }
  72. static void btrfs_kobject_uevent(struct block_device *bdev,
  73. enum kobject_action action)
  74. {
  75. int ret;
  76. ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
  77. if (ret)
  78. pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
  79. action,
  80. kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
  81. &disk_to_dev(bdev->bd_disk)->kobj);
  82. }
  83. void btrfs_cleanup_fs_uuids(void)
  84. {
  85. struct btrfs_fs_devices *fs_devices;
  86. while (!list_empty(&fs_uuids)) {
  87. fs_devices = list_entry(fs_uuids.next,
  88. struct btrfs_fs_devices, list);
  89. list_del(&fs_devices->list);
  90. free_fs_devices(fs_devices);
  91. }
  92. }
  93. static noinline struct btrfs_device *__find_device(struct list_head *head,
  94. u64 devid, u8 *uuid)
  95. {
  96. struct btrfs_device *dev;
  97. list_for_each_entry(dev, head, dev_list) {
  98. if (dev->devid == devid &&
  99. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  100. return dev;
  101. }
  102. }
  103. return NULL;
  104. }
  105. static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
  106. {
  107. struct btrfs_fs_devices *fs_devices;
  108. list_for_each_entry(fs_devices, &fs_uuids, list) {
  109. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  110. return fs_devices;
  111. }
  112. return NULL;
  113. }
  114. static int
  115. btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
  116. int flush, struct block_device **bdev,
  117. struct buffer_head **bh)
  118. {
  119. int ret;
  120. *bdev = blkdev_get_by_path(device_path, flags, holder);
  121. if (IS_ERR(*bdev)) {
  122. ret = PTR_ERR(*bdev);
  123. printk(KERN_INFO "btrfs: open %s failed\n", device_path);
  124. goto error;
  125. }
  126. if (flush)
  127. filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
  128. ret = set_blocksize(*bdev, 4096);
  129. if (ret) {
  130. blkdev_put(*bdev, flags);
  131. goto error;
  132. }
  133. invalidate_bdev(*bdev);
  134. *bh = btrfs_read_dev_super(*bdev);
  135. if (!*bh) {
  136. ret = -EINVAL;
  137. blkdev_put(*bdev, flags);
  138. goto error;
  139. }
  140. return 0;
  141. error:
  142. *bdev = NULL;
  143. *bh = NULL;
  144. return ret;
  145. }
  146. static void requeue_list(struct btrfs_pending_bios *pending_bios,
  147. struct bio *head, struct bio *tail)
  148. {
  149. struct bio *old_head;
  150. old_head = pending_bios->head;
  151. pending_bios->head = head;
  152. if (pending_bios->tail)
  153. tail->bi_next = old_head;
  154. else
  155. pending_bios->tail = tail;
  156. }
  157. /*
  158. * we try to collect pending bios for a device so we don't get a large
  159. * number of procs sending bios down to the same device. This greatly
  160. * improves the schedulers ability to collect and merge the bios.
  161. *
  162. * But, it also turns into a long list of bios to process and that is sure
  163. * to eventually make the worker thread block. The solution here is to
  164. * make some progress and then put this work struct back at the end of
  165. * the list if the block device is congested. This way, multiple devices
  166. * can make progress from a single worker thread.
  167. */
  168. static noinline void run_scheduled_bios(struct btrfs_device *device)
  169. {
  170. struct bio *pending;
  171. struct backing_dev_info *bdi;
  172. struct btrfs_fs_info *fs_info;
  173. struct btrfs_pending_bios *pending_bios;
  174. struct bio *tail;
  175. struct bio *cur;
  176. int again = 0;
  177. unsigned long num_run;
  178. unsigned long batch_run = 0;
  179. unsigned long limit;
  180. unsigned long last_waited = 0;
  181. int force_reg = 0;
  182. int sync_pending = 0;
  183. struct blk_plug plug;
  184. /*
  185. * this function runs all the bios we've collected for
  186. * a particular device. We don't want to wander off to
  187. * another device without first sending all of these down.
  188. * So, setup a plug here and finish it off before we return
  189. */
  190. blk_start_plug(&plug);
  191. bdi = blk_get_backing_dev_info(device->bdev);
  192. fs_info = device->dev_root->fs_info;
  193. limit = btrfs_async_submit_limit(fs_info);
  194. limit = limit * 2 / 3;
  195. loop:
  196. spin_lock(&device->io_lock);
  197. loop_lock:
  198. num_run = 0;
  199. /* take all the bios off the list at once and process them
  200. * later on (without the lock held). But, remember the
  201. * tail and other pointers so the bios can be properly reinserted
  202. * into the list if we hit congestion
  203. */
  204. if (!force_reg && device->pending_sync_bios.head) {
  205. pending_bios = &device->pending_sync_bios;
  206. force_reg = 1;
  207. } else {
  208. pending_bios = &device->pending_bios;
  209. force_reg = 0;
  210. }
  211. pending = pending_bios->head;
  212. tail = pending_bios->tail;
  213. WARN_ON(pending && !tail);
  214. /*
  215. * if pending was null this time around, no bios need processing
  216. * at all and we can stop. Otherwise it'll loop back up again
  217. * and do an additional check so no bios are missed.
  218. *
  219. * device->running_pending is used to synchronize with the
  220. * schedule_bio code.
  221. */
  222. if (device->pending_sync_bios.head == NULL &&
  223. device->pending_bios.head == NULL) {
  224. again = 0;
  225. device->running_pending = 0;
  226. } else {
  227. again = 1;
  228. device->running_pending = 1;
  229. }
  230. pending_bios->head = NULL;
  231. pending_bios->tail = NULL;
  232. spin_unlock(&device->io_lock);
  233. while (pending) {
  234. rmb();
  235. /* we want to work on both lists, but do more bios on the
  236. * sync list than the regular list
  237. */
  238. if ((num_run > 32 &&
  239. pending_bios != &device->pending_sync_bios &&
  240. device->pending_sync_bios.head) ||
  241. (num_run > 64 && pending_bios == &device->pending_sync_bios &&
  242. device->pending_bios.head)) {
  243. spin_lock(&device->io_lock);
  244. requeue_list(pending_bios, pending, tail);
  245. goto loop_lock;
  246. }
  247. cur = pending;
  248. pending = pending->bi_next;
  249. cur->bi_next = NULL;
  250. if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
  251. waitqueue_active(&fs_info->async_submit_wait))
  252. wake_up(&fs_info->async_submit_wait);
  253. BUG_ON(atomic_read(&cur->bi_cnt) == 0);
  254. /*
  255. * if we're doing the sync list, record that our
  256. * plug has some sync requests on it
  257. *
  258. * If we're doing the regular list and there are
  259. * sync requests sitting around, unplug before
  260. * we add more
  261. */
  262. if (pending_bios == &device->pending_sync_bios) {
  263. sync_pending = 1;
  264. } else if (sync_pending) {
  265. blk_finish_plug(&plug);
  266. blk_start_plug(&plug);
  267. sync_pending = 0;
  268. }
  269. btrfsic_submit_bio(cur->bi_rw, cur);
  270. num_run++;
  271. batch_run++;
  272. if (need_resched())
  273. cond_resched();
  274. /*
  275. * we made progress, there is more work to do and the bdi
  276. * is now congested. Back off and let other work structs
  277. * run instead
  278. */
  279. if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
  280. fs_info->fs_devices->open_devices > 1) {
  281. struct io_context *ioc;
  282. ioc = current->io_context;
  283. /*
  284. * the main goal here is that we don't want to
  285. * block if we're going to be able to submit
  286. * more requests without blocking.
  287. *
  288. * This code does two great things, it pokes into
  289. * the elevator code from a filesystem _and_
  290. * it makes assumptions about how batching works.
  291. */
  292. if (ioc && ioc->nr_batch_requests > 0 &&
  293. time_before(jiffies, ioc->last_waited + HZ/50UL) &&
  294. (last_waited == 0 ||
  295. ioc->last_waited == last_waited)) {
  296. /*
  297. * we want to go through our batch of
  298. * requests and stop. So, we copy out
  299. * the ioc->last_waited time and test
  300. * against it before looping
  301. */
  302. last_waited = ioc->last_waited;
  303. if (need_resched())
  304. cond_resched();
  305. continue;
  306. }
  307. spin_lock(&device->io_lock);
  308. requeue_list(pending_bios, pending, tail);
  309. device->running_pending = 1;
  310. spin_unlock(&device->io_lock);
  311. btrfs_requeue_work(&device->work);
  312. goto done;
  313. }
  314. /* unplug every 64 requests just for good measure */
  315. if (batch_run % 64 == 0) {
  316. blk_finish_plug(&plug);
  317. blk_start_plug(&plug);
  318. sync_pending = 0;
  319. }
  320. }
  321. cond_resched();
  322. if (again)
  323. goto loop;
  324. spin_lock(&device->io_lock);
  325. if (device->pending_bios.head || device->pending_sync_bios.head)
  326. goto loop_lock;
  327. spin_unlock(&device->io_lock);
  328. done:
  329. blk_finish_plug(&plug);
  330. }
  331. static void pending_bios_fn(struct btrfs_work *work)
  332. {
  333. struct btrfs_device *device;
  334. device = container_of(work, struct btrfs_device, work);
  335. run_scheduled_bios(device);
  336. }
  337. static noinline int device_list_add(const char *path,
  338. struct btrfs_super_block *disk_super,
  339. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  340. {
  341. struct btrfs_device *device;
  342. struct btrfs_fs_devices *fs_devices;
  343. struct rcu_string *name;
  344. u64 found_transid = btrfs_super_generation(disk_super);
  345. fs_devices = find_fsid(disk_super->fsid);
  346. if (!fs_devices) {
  347. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  348. if (!fs_devices)
  349. return -ENOMEM;
  350. INIT_LIST_HEAD(&fs_devices->devices);
  351. INIT_LIST_HEAD(&fs_devices->alloc_list);
  352. list_add(&fs_devices->list, &fs_uuids);
  353. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  354. fs_devices->latest_devid = devid;
  355. fs_devices->latest_trans = found_transid;
  356. mutex_init(&fs_devices->device_list_mutex);
  357. device = NULL;
  358. } else {
  359. device = __find_device(&fs_devices->devices, devid,
  360. disk_super->dev_item.uuid);
  361. }
  362. if (!device) {
  363. if (fs_devices->opened)
  364. return -EBUSY;
  365. device = kzalloc(sizeof(*device), GFP_NOFS);
  366. if (!device) {
  367. /* we can safely leave the fs_devices entry around */
  368. return -ENOMEM;
  369. }
  370. device->devid = devid;
  371. device->dev_stats_valid = 0;
  372. device->work.func = pending_bios_fn;
  373. memcpy(device->uuid, disk_super->dev_item.uuid,
  374. BTRFS_UUID_SIZE);
  375. spin_lock_init(&device->io_lock);
  376. name = rcu_string_strdup(path, GFP_NOFS);
  377. if (!name) {
  378. kfree(device);
  379. return -ENOMEM;
  380. }
  381. rcu_assign_pointer(device->name, name);
  382. INIT_LIST_HEAD(&device->dev_alloc_list);
  383. /* init readahead state */
  384. spin_lock_init(&device->reada_lock);
  385. device->reada_curr_zone = NULL;
  386. atomic_set(&device->reada_in_flight, 0);
  387. device->reada_next = 0;
  388. INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
  389. INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
  390. mutex_lock(&fs_devices->device_list_mutex);
  391. list_add_rcu(&device->dev_list, &fs_devices->devices);
  392. mutex_unlock(&fs_devices->device_list_mutex);
  393. device->fs_devices = fs_devices;
  394. fs_devices->num_devices++;
  395. } else if (!device->name || strcmp(device->name->str, path)) {
  396. name = rcu_string_strdup(path, GFP_NOFS);
  397. if (!name)
  398. return -ENOMEM;
  399. rcu_string_free(device->name);
  400. rcu_assign_pointer(device->name, name);
  401. if (device->missing) {
  402. fs_devices->missing_devices--;
  403. device->missing = 0;
  404. }
  405. }
  406. if (found_transid > fs_devices->latest_trans) {
  407. fs_devices->latest_devid = devid;
  408. fs_devices->latest_trans = found_transid;
  409. }
  410. *fs_devices_ret = fs_devices;
  411. return 0;
  412. }
  413. static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
  414. {
  415. struct btrfs_fs_devices *fs_devices;
  416. struct btrfs_device *device;
  417. struct btrfs_device *orig_dev;
  418. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  419. if (!fs_devices)
  420. return ERR_PTR(-ENOMEM);
  421. INIT_LIST_HEAD(&fs_devices->devices);
  422. INIT_LIST_HEAD(&fs_devices->alloc_list);
  423. INIT_LIST_HEAD(&fs_devices->list);
  424. mutex_init(&fs_devices->device_list_mutex);
  425. fs_devices->latest_devid = orig->latest_devid;
  426. fs_devices->latest_trans = orig->latest_trans;
  427. fs_devices->total_devices = orig->total_devices;
  428. memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
  429. /* We have held the volume lock, it is safe to get the devices. */
  430. list_for_each_entry(orig_dev, &orig->devices, dev_list) {
  431. struct rcu_string *name;
  432. device = kzalloc(sizeof(*device), GFP_NOFS);
  433. if (!device)
  434. goto error;
  435. /*
  436. * This is ok to do without rcu read locked because we hold the
  437. * uuid mutex so nothing we touch in here is going to disappear.
  438. */
  439. name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
  440. if (!name) {
  441. kfree(device);
  442. goto error;
  443. }
  444. rcu_assign_pointer(device->name, name);
  445. device->devid = orig_dev->devid;
  446. device->work.func = pending_bios_fn;
  447. memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
  448. spin_lock_init(&device->io_lock);
  449. INIT_LIST_HEAD(&device->dev_list);
  450. INIT_LIST_HEAD(&device->dev_alloc_list);
  451. list_add(&device->dev_list, &fs_devices->devices);
  452. device->fs_devices = fs_devices;
  453. fs_devices->num_devices++;
  454. }
  455. return fs_devices;
  456. error:
  457. free_fs_devices(fs_devices);
  458. return ERR_PTR(-ENOMEM);
  459. }
  460. void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
  461. struct btrfs_fs_devices *fs_devices, int step)
  462. {
  463. struct btrfs_device *device, *next;
  464. struct block_device *latest_bdev = NULL;
  465. u64 latest_devid = 0;
  466. u64 latest_transid = 0;
  467. mutex_lock(&uuid_mutex);
  468. again:
  469. /* This is the initialized path, it is safe to release the devices. */
  470. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  471. if (device->in_fs_metadata) {
  472. if (!device->is_tgtdev_for_dev_replace &&
  473. (!latest_transid ||
  474. device->generation > latest_transid)) {
  475. latest_devid = device->devid;
  476. latest_transid = device->generation;
  477. latest_bdev = device->bdev;
  478. }
  479. continue;
  480. }
  481. if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
  482. /*
  483. * In the first step, keep the device which has
  484. * the correct fsid and the devid that is used
  485. * for the dev_replace procedure.
  486. * In the second step, the dev_replace state is
  487. * read from the device tree and it is known
  488. * whether the procedure is really active or
  489. * not, which means whether this device is
  490. * used or whether it should be removed.
  491. */
  492. if (step == 0 || device->is_tgtdev_for_dev_replace) {
  493. continue;
  494. }
  495. }
  496. if (device->bdev) {
  497. blkdev_put(device->bdev, device->mode);
  498. device->bdev = NULL;
  499. fs_devices->open_devices--;
  500. }
  501. if (device->writeable) {
  502. list_del_init(&device->dev_alloc_list);
  503. device->writeable = 0;
  504. if (!device->is_tgtdev_for_dev_replace)
  505. fs_devices->rw_devices--;
  506. }
  507. list_del_init(&device->dev_list);
  508. fs_devices->num_devices--;
  509. rcu_string_free(device->name);
  510. kfree(device);
  511. }
  512. if (fs_devices->seed) {
  513. fs_devices = fs_devices->seed;
  514. goto again;
  515. }
  516. fs_devices->latest_bdev = latest_bdev;
  517. fs_devices->latest_devid = latest_devid;
  518. fs_devices->latest_trans = latest_transid;
  519. mutex_unlock(&uuid_mutex);
  520. }
  521. static void __free_device(struct work_struct *work)
  522. {
  523. struct btrfs_device *device;
  524. device = container_of(work, struct btrfs_device, rcu_work);
  525. if (device->bdev)
  526. blkdev_put(device->bdev, device->mode);
  527. rcu_string_free(device->name);
  528. kfree(device);
  529. }
  530. static void free_device(struct rcu_head *head)
  531. {
  532. struct btrfs_device *device;
  533. device = container_of(head, struct btrfs_device, rcu);
  534. INIT_WORK(&device->rcu_work, __free_device);
  535. schedule_work(&device->rcu_work);
  536. }
  537. static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  538. {
  539. struct btrfs_device *device;
  540. if (--fs_devices->opened > 0)
  541. return 0;
  542. mutex_lock(&fs_devices->device_list_mutex);
  543. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  544. struct btrfs_device *new_device;
  545. struct rcu_string *name;
  546. if (device->bdev)
  547. fs_devices->open_devices--;
  548. if (device->writeable && !device->is_tgtdev_for_dev_replace) {
  549. list_del_init(&device->dev_alloc_list);
  550. fs_devices->rw_devices--;
  551. }
  552. if (device->can_discard)
  553. fs_devices->num_can_discard--;
  554. new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
  555. BUG_ON(!new_device); /* -ENOMEM */
  556. memcpy(new_device, device, sizeof(*new_device));
  557. /* Safe because we are under uuid_mutex */
  558. if (device->name) {
  559. name = rcu_string_strdup(device->name->str, GFP_NOFS);
  560. BUG_ON(device->name && !name); /* -ENOMEM */
  561. rcu_assign_pointer(new_device->name, name);
  562. }
  563. new_device->bdev = NULL;
  564. new_device->writeable = 0;
  565. new_device->in_fs_metadata = 0;
  566. new_device->can_discard = 0;
  567. spin_lock_init(&new_device->io_lock);
  568. list_replace_rcu(&device->dev_list, &new_device->dev_list);
  569. call_rcu(&device->rcu, free_device);
  570. }
  571. mutex_unlock(&fs_devices->device_list_mutex);
  572. WARN_ON(fs_devices->open_devices);
  573. WARN_ON(fs_devices->rw_devices);
  574. fs_devices->opened = 0;
  575. fs_devices->seeding = 0;
  576. return 0;
  577. }
  578. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  579. {
  580. struct btrfs_fs_devices *seed_devices = NULL;
  581. int ret;
  582. mutex_lock(&uuid_mutex);
  583. ret = __btrfs_close_devices(fs_devices);
  584. if (!fs_devices->opened) {
  585. seed_devices = fs_devices->seed;
  586. fs_devices->seed = NULL;
  587. }
  588. mutex_unlock(&uuid_mutex);
  589. while (seed_devices) {
  590. fs_devices = seed_devices;
  591. seed_devices = fs_devices->seed;
  592. __btrfs_close_devices(fs_devices);
  593. free_fs_devices(fs_devices);
  594. }
  595. /*
  596. * Wait for rcu kworkers under __btrfs_close_devices
  597. * to finish all blkdev_puts so device is really
  598. * free when umount is done.
  599. */
  600. rcu_barrier();
  601. return ret;
  602. }
  603. static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  604. fmode_t flags, void *holder)
  605. {
  606. struct request_queue *q;
  607. struct block_device *bdev;
  608. struct list_head *head = &fs_devices->devices;
  609. struct btrfs_device *device;
  610. struct block_device *latest_bdev = NULL;
  611. struct buffer_head *bh;
  612. struct btrfs_super_block *disk_super;
  613. u64 latest_devid = 0;
  614. u64 latest_transid = 0;
  615. u64 devid;
  616. int seeding = 1;
  617. int ret = 0;
  618. flags |= FMODE_EXCL;
  619. list_for_each_entry(device, head, dev_list) {
  620. if (device->bdev)
  621. continue;
  622. if (!device->name)
  623. continue;
  624. /* Just open everything we can; ignore failures here */
  625. if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
  626. &bdev, &bh))
  627. continue;
  628. disk_super = (struct btrfs_super_block *)bh->b_data;
  629. devid = btrfs_stack_device_id(&disk_super->dev_item);
  630. if (devid != device->devid)
  631. goto error_brelse;
  632. if (memcmp(device->uuid, disk_super->dev_item.uuid,
  633. BTRFS_UUID_SIZE))
  634. goto error_brelse;
  635. device->generation = btrfs_super_generation(disk_super);
  636. if (!latest_transid || device->generation > latest_transid) {
  637. latest_devid = devid;
  638. latest_transid = device->generation;
  639. latest_bdev = bdev;
  640. }
  641. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
  642. device->writeable = 0;
  643. } else {
  644. device->writeable = !bdev_read_only(bdev);
  645. seeding = 0;
  646. }
  647. q = bdev_get_queue(bdev);
  648. if (blk_queue_discard(q)) {
  649. device->can_discard = 1;
  650. fs_devices->num_can_discard++;
  651. }
  652. device->bdev = bdev;
  653. device->in_fs_metadata = 0;
  654. device->mode = flags;
  655. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  656. fs_devices->rotating = 1;
  657. fs_devices->open_devices++;
  658. if (device->writeable && !device->is_tgtdev_for_dev_replace) {
  659. fs_devices->rw_devices++;
  660. list_add(&device->dev_alloc_list,
  661. &fs_devices->alloc_list);
  662. }
  663. brelse(bh);
  664. continue;
  665. error_brelse:
  666. brelse(bh);
  667. blkdev_put(bdev, flags);
  668. continue;
  669. }
  670. if (fs_devices->open_devices == 0) {
  671. ret = -EINVAL;
  672. goto out;
  673. }
  674. fs_devices->seeding = seeding;
  675. fs_devices->opened = 1;
  676. fs_devices->latest_bdev = latest_bdev;
  677. fs_devices->latest_devid = latest_devid;
  678. fs_devices->latest_trans = latest_transid;
  679. fs_devices->total_rw_bytes = 0;
  680. out:
  681. return ret;
  682. }
  683. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  684. fmode_t flags, void *holder)
  685. {
  686. int ret;
  687. mutex_lock(&uuid_mutex);
  688. if (fs_devices->opened) {
  689. fs_devices->opened++;
  690. ret = 0;
  691. } else {
  692. ret = __btrfs_open_devices(fs_devices, flags, holder);
  693. }
  694. mutex_unlock(&uuid_mutex);
  695. return ret;
  696. }
  697. /*
  698. * Look for a btrfs signature on a device. This may be called out of the mount path
  699. * and we are not allowed to call set_blocksize during the scan. The superblock
  700. * is read via pagecache
  701. */
  702. int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
  703. struct btrfs_fs_devices **fs_devices_ret)
  704. {
  705. struct btrfs_super_block *disk_super;
  706. struct block_device *bdev;
  707. struct page *page;
  708. void *p;
  709. int ret = -EINVAL;
  710. u64 devid;
  711. u64 transid;
  712. u64 total_devices;
  713. u64 bytenr;
  714. pgoff_t index;
  715. /*
  716. * we would like to check all the supers, but that would make
  717. * a btrfs mount succeed after a mkfs from a different FS.
  718. * So, we need to add a special mount option to scan for
  719. * later supers, using BTRFS_SUPER_MIRROR_MAX instead
  720. */
  721. bytenr = btrfs_sb_offset(0);
  722. flags |= FMODE_EXCL;
  723. mutex_lock(&uuid_mutex);
  724. bdev = blkdev_get_by_path(path, flags, holder);
  725. if (IS_ERR(bdev)) {
  726. ret = PTR_ERR(bdev);
  727. goto error;
  728. }
  729. /* make sure our super fits in the device */
  730. if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
  731. goto error_bdev_put;
  732. /* make sure our super fits in the page */
  733. if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
  734. goto error_bdev_put;
  735. /* make sure our super doesn't straddle pages on disk */
  736. index = bytenr >> PAGE_CACHE_SHIFT;
  737. if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
  738. goto error_bdev_put;
  739. /* pull in the page with our super */
  740. page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
  741. index, GFP_NOFS);
  742. if (IS_ERR_OR_NULL(page))
  743. goto error_bdev_put;
  744. p = kmap(page);
  745. /* align our pointer to the offset of the super block */
  746. disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
  747. if (btrfs_super_bytenr(disk_super) != bytenr ||
  748. disk_super->magic != cpu_to_le64(BTRFS_MAGIC))
  749. goto error_unmap;
  750. devid = btrfs_stack_device_id(&disk_super->dev_item);
  751. transid = btrfs_super_generation(disk_super);
  752. total_devices = btrfs_super_num_devices(disk_super);
  753. if (disk_super->label[0]) {
  754. if (disk_super->label[BTRFS_LABEL_SIZE - 1])
  755. disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
  756. printk(KERN_INFO "device label %s ", disk_super->label);
  757. } else {
  758. printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
  759. }
  760. printk(KERN_CONT "devid %llu transid %llu %s\n",
  761. (unsigned long long)devid, (unsigned long long)transid, path);
  762. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  763. if (!ret && fs_devices_ret)
  764. (*fs_devices_ret)->total_devices = total_devices;
  765. error_unmap:
  766. kunmap(page);
  767. page_cache_release(page);
  768. error_bdev_put:
  769. blkdev_put(bdev, flags);
  770. error:
  771. mutex_unlock(&uuid_mutex);
  772. return ret;
  773. }
  774. /* helper to account the used device space in the range */
  775. int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
  776. u64 end, u64 *length)
  777. {
  778. struct btrfs_key key;
  779. struct btrfs_root *root = device->dev_root;
  780. struct btrfs_dev_extent *dev_extent;
  781. struct btrfs_path *path;
  782. u64 extent_end;
  783. int ret;
  784. int slot;
  785. struct extent_buffer *l;
  786. *length = 0;
  787. if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
  788. return 0;
  789. path = btrfs_alloc_path();
  790. if (!path)
  791. return -ENOMEM;
  792. path->reada = 2;
  793. key.objectid = device->devid;
  794. key.offset = start;
  795. key.type = BTRFS_DEV_EXTENT_KEY;
  796. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  797. if (ret < 0)
  798. goto out;
  799. if (ret > 0) {
  800. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  801. if (ret < 0)
  802. goto out;
  803. }
  804. while (1) {
  805. l = path->nodes[0];
  806. slot = path->slots[0];
  807. if (slot >= btrfs_header_nritems(l)) {
  808. ret = btrfs_next_leaf(root, path);
  809. if (ret == 0)
  810. continue;
  811. if (ret < 0)
  812. goto out;
  813. break;
  814. }
  815. btrfs_item_key_to_cpu(l, &key, slot);
  816. if (key.objectid < device->devid)
  817. goto next;
  818. if (key.objectid > device->devid)
  819. break;
  820. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  821. goto next;
  822. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  823. extent_end = key.offset + btrfs_dev_extent_length(l,
  824. dev_extent);
  825. if (key.offset <= start && extent_end > end) {
  826. *length = end - start + 1;
  827. break;
  828. } else if (key.offset <= start && extent_end > start)
  829. *length += extent_end - start;
  830. else if (key.offset > start && extent_end <= end)
  831. *length += extent_end - key.offset;
  832. else if (key.offset > start && key.offset <= end) {
  833. *length += end - key.offset + 1;
  834. break;
  835. } else if (key.offset > end)
  836. break;
  837. next:
  838. path->slots[0]++;
  839. }
  840. ret = 0;
  841. out:
  842. btrfs_free_path(path);
  843. return ret;
  844. }
  845. /*
  846. * find_free_dev_extent - find free space in the specified device
  847. * @device: the device which we search the free space in
  848. * @num_bytes: the size of the free space that we need
  849. * @start: store the start of the free space.
  850. * @len: the size of the free space. that we find, or the size of the max
  851. * free space if we don't find suitable free space
  852. *
  853. * this uses a pretty simple search, the expectation is that it is
  854. * called very infrequently and that a given device has a small number
  855. * of extents
  856. *
  857. * @start is used to store the start of the free space if we find. But if we
  858. * don't find suitable free space, it will be used to store the start position
  859. * of the max free space.
  860. *
  861. * @len is used to store the size of the free space that we find.
  862. * But if we don't find suitable free space, it is used to store the size of
  863. * the max free space.
  864. */
  865. int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
  866. u64 *start, u64 *len)
  867. {
  868. struct btrfs_key key;
  869. struct btrfs_root *root = device->dev_root;
  870. struct btrfs_dev_extent *dev_extent;
  871. struct btrfs_path *path;
  872. u64 hole_size;
  873. u64 max_hole_start;
  874. u64 max_hole_size;
  875. u64 extent_end;
  876. u64 search_start;
  877. u64 search_end = device->total_bytes;
  878. int ret;
  879. int slot;
  880. struct extent_buffer *l;
  881. /* FIXME use last free of some kind */
  882. /* we don't want to overwrite the superblock on the drive,
  883. * so we make sure to start at an offset of at least 1MB
  884. */
  885. search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
  886. max_hole_start = search_start;
  887. max_hole_size = 0;
  888. hole_size = 0;
  889. if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
  890. ret = -ENOSPC;
  891. goto error;
  892. }
  893. path = btrfs_alloc_path();
  894. if (!path) {
  895. ret = -ENOMEM;
  896. goto error;
  897. }
  898. path->reada = 2;
  899. key.objectid = device->devid;
  900. key.offset = search_start;
  901. key.type = BTRFS_DEV_EXTENT_KEY;
  902. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  903. if (ret < 0)
  904. goto out;
  905. if (ret > 0) {
  906. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  907. if (ret < 0)
  908. goto out;
  909. }
  910. while (1) {
  911. l = path->nodes[0];
  912. slot = path->slots[0];
  913. if (slot >= btrfs_header_nritems(l)) {
  914. ret = btrfs_next_leaf(root, path);
  915. if (ret == 0)
  916. continue;
  917. if (ret < 0)
  918. goto out;
  919. break;
  920. }
  921. btrfs_item_key_to_cpu(l, &key, slot);
  922. if (key.objectid < device->devid)
  923. goto next;
  924. if (key.objectid > device->devid)
  925. break;
  926. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  927. goto next;
  928. if (key.offset > search_start) {
  929. hole_size = key.offset - search_start;
  930. if (hole_size > max_hole_size) {
  931. max_hole_start = search_start;
  932. max_hole_size = hole_size;
  933. }
  934. /*
  935. * If this free space is greater than which we need,
  936. * it must be the max free space that we have found
  937. * until now, so max_hole_start must point to the start
  938. * of this free space and the length of this free space
  939. * is stored in max_hole_size. Thus, we return
  940. * max_hole_start and max_hole_size and go back to the
  941. * caller.
  942. */
  943. if (hole_size >= num_bytes) {
  944. ret = 0;
  945. goto out;
  946. }
  947. }
  948. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  949. extent_end = key.offset + btrfs_dev_extent_length(l,
  950. dev_extent);
  951. if (extent_end > search_start)
  952. search_start = extent_end;
  953. next:
  954. path->slots[0]++;
  955. cond_resched();
  956. }
  957. /*
  958. * At this point, search_start should be the end of
  959. * allocated dev extents, and when shrinking the device,
  960. * search_end may be smaller than search_start.
  961. */
  962. if (search_end > search_start)
  963. hole_size = search_end - search_start;
  964. if (hole_size > max_hole_size) {
  965. max_hole_start = search_start;
  966. max_hole_size = hole_size;
  967. }
  968. /* See above. */
  969. if (hole_size < num_bytes)
  970. ret = -ENOSPC;
  971. else
  972. ret = 0;
  973. out:
  974. btrfs_free_path(path);
  975. error:
  976. *start = max_hole_start;
  977. if (len)
  978. *len = max_hole_size;
  979. return ret;
  980. }
  981. static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  982. struct btrfs_device *device,
  983. u64 start)
  984. {
  985. int ret;
  986. struct btrfs_path *path;
  987. struct btrfs_root *root = device->dev_root;
  988. struct btrfs_key key;
  989. struct btrfs_key found_key;
  990. struct extent_buffer *leaf = NULL;
  991. struct btrfs_dev_extent *extent = NULL;
  992. path = btrfs_alloc_path();
  993. if (!path)
  994. return -ENOMEM;
  995. key.objectid = device->devid;
  996. key.offset = start;
  997. key.type = BTRFS_DEV_EXTENT_KEY;
  998. again:
  999. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1000. if (ret > 0) {
  1001. ret = btrfs_previous_item(root, path, key.objectid,
  1002. BTRFS_DEV_EXTENT_KEY);
  1003. if (ret)
  1004. goto out;
  1005. leaf = path->nodes[0];
  1006. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1007. extent = btrfs_item_ptr(leaf, path->slots[0],
  1008. struct btrfs_dev_extent);
  1009. BUG_ON(found_key.offset > start || found_key.offset +
  1010. btrfs_dev_extent_length(leaf, extent) < start);
  1011. key = found_key;
  1012. btrfs_release_path(path);
  1013. goto again;
  1014. } else if (ret == 0) {
  1015. leaf = path->nodes[0];
  1016. extent = btrfs_item_ptr(leaf, path->slots[0],
  1017. struct btrfs_dev_extent);
  1018. } else {
  1019. btrfs_error(root->fs_info, ret, "Slot search failed");
  1020. goto out;
  1021. }
  1022. if (device->bytes_used > 0) {
  1023. u64 len = btrfs_dev_extent_length(leaf, extent);
  1024. device->bytes_used -= len;
  1025. spin_lock(&root->fs_info->free_chunk_lock);
  1026. root->fs_info->free_chunk_space += len;
  1027. spin_unlock(&root->fs_info->free_chunk_lock);
  1028. }
  1029. ret = btrfs_del_item(trans, root, path);
  1030. if (ret) {
  1031. btrfs_error(root->fs_info, ret,
  1032. "Failed to remove dev extent item");
  1033. }
  1034. out:
  1035. btrfs_free_path(path);
  1036. return ret;
  1037. }
  1038. int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  1039. struct btrfs_device *device,
  1040. u64 chunk_tree, u64 chunk_objectid,
  1041. u64 chunk_offset, u64 start, u64 num_bytes)
  1042. {
  1043. int ret;
  1044. struct btrfs_path *path;
  1045. struct btrfs_root *root = device->dev_root;
  1046. struct btrfs_dev_extent *extent;
  1047. struct extent_buffer *leaf;
  1048. struct btrfs_key key;
  1049. WARN_ON(!device->in_fs_metadata);
  1050. WARN_ON(device->is_tgtdev_for_dev_replace);
  1051. path = btrfs_alloc_path();
  1052. if (!path)
  1053. return -ENOMEM;
  1054. key.objectid = device->devid;
  1055. key.offset = start;
  1056. key.type = BTRFS_DEV_EXTENT_KEY;
  1057. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1058. sizeof(*extent));
  1059. if (ret)
  1060. goto out;
  1061. leaf = path->nodes[0];
  1062. extent = btrfs_item_ptr(leaf, path->slots[0],
  1063. struct btrfs_dev_extent);
  1064. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  1065. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  1066. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  1067. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  1068. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  1069. BTRFS_UUID_SIZE);
  1070. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  1071. btrfs_mark_buffer_dirty(leaf);
  1072. out:
  1073. btrfs_free_path(path);
  1074. return ret;
  1075. }
  1076. static noinline int find_next_chunk(struct btrfs_root *root,
  1077. u64 objectid, u64 *offset)
  1078. {
  1079. struct btrfs_path *path;
  1080. int ret;
  1081. struct btrfs_key key;
  1082. struct btrfs_chunk *chunk;
  1083. struct btrfs_key found_key;
  1084. path = btrfs_alloc_path();
  1085. if (!path)
  1086. return -ENOMEM;
  1087. key.objectid = objectid;
  1088. key.offset = (u64)-1;
  1089. key.type = BTRFS_CHUNK_ITEM_KEY;
  1090. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1091. if (ret < 0)
  1092. goto error;
  1093. BUG_ON(ret == 0); /* Corruption */
  1094. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  1095. if (ret) {
  1096. *offset = 0;
  1097. } else {
  1098. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1099. path->slots[0]);
  1100. if (found_key.objectid != objectid)
  1101. *offset = 0;
  1102. else {
  1103. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1104. struct btrfs_chunk);
  1105. *offset = found_key.offset +
  1106. btrfs_chunk_length(path->nodes[0], chunk);
  1107. }
  1108. }
  1109. ret = 0;
  1110. error:
  1111. btrfs_free_path(path);
  1112. return ret;
  1113. }
  1114. static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
  1115. {
  1116. int ret;
  1117. struct btrfs_key key;
  1118. struct btrfs_key found_key;
  1119. struct btrfs_path *path;
  1120. root = root->fs_info->chunk_root;
  1121. path = btrfs_alloc_path();
  1122. if (!path)
  1123. return -ENOMEM;
  1124. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1125. key.type = BTRFS_DEV_ITEM_KEY;
  1126. key.offset = (u64)-1;
  1127. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1128. if (ret < 0)
  1129. goto error;
  1130. BUG_ON(ret == 0); /* Corruption */
  1131. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  1132. BTRFS_DEV_ITEM_KEY);
  1133. if (ret) {
  1134. *objectid = 1;
  1135. } else {
  1136. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1137. path->slots[0]);
  1138. *objectid = found_key.offset + 1;
  1139. }
  1140. ret = 0;
  1141. error:
  1142. btrfs_free_path(path);
  1143. return ret;
  1144. }
  1145. /*
  1146. * the device information is stored in the chunk root
  1147. * the btrfs_device struct should be fully filled in
  1148. */
  1149. int btrfs_add_device(struct btrfs_trans_handle *trans,
  1150. struct btrfs_root *root,
  1151. struct btrfs_device *device)
  1152. {
  1153. int ret;
  1154. struct btrfs_path *path;
  1155. struct btrfs_dev_item *dev_item;
  1156. struct extent_buffer *leaf;
  1157. struct btrfs_key key;
  1158. unsigned long ptr;
  1159. root = root->fs_info->chunk_root;
  1160. path = btrfs_alloc_path();
  1161. if (!path)
  1162. return -ENOMEM;
  1163. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1164. key.type = BTRFS_DEV_ITEM_KEY;
  1165. key.offset = device->devid;
  1166. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1167. sizeof(*dev_item));
  1168. if (ret)
  1169. goto out;
  1170. leaf = path->nodes[0];
  1171. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1172. btrfs_set_device_id(leaf, dev_item, device->devid);
  1173. btrfs_set_device_generation(leaf, dev_item, 0);
  1174. btrfs_set_device_type(leaf, dev_item, device->type);
  1175. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1176. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1177. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1178. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  1179. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1180. btrfs_set_device_group(leaf, dev_item, 0);
  1181. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  1182. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  1183. btrfs_set_device_start_offset(leaf, dev_item, 0);
  1184. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  1185. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1186. ptr = (unsigned long)btrfs_device_fsid(dev_item);
  1187. write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
  1188. btrfs_mark_buffer_dirty(leaf);
  1189. ret = 0;
  1190. out:
  1191. btrfs_free_path(path);
  1192. return ret;
  1193. }
  1194. static int btrfs_rm_dev_item(struct btrfs_root *root,
  1195. struct btrfs_device *device)
  1196. {
  1197. int ret;
  1198. struct btrfs_path *path;
  1199. struct btrfs_key key;
  1200. struct btrfs_trans_handle *trans;
  1201. root = root->fs_info->chunk_root;
  1202. path = btrfs_alloc_path();
  1203. if (!path)
  1204. return -ENOMEM;
  1205. trans = btrfs_start_transaction(root, 0);
  1206. if (IS_ERR(trans)) {
  1207. btrfs_free_path(path);
  1208. return PTR_ERR(trans);
  1209. }
  1210. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1211. key.type = BTRFS_DEV_ITEM_KEY;
  1212. key.offset = device->devid;
  1213. lock_chunks(root);
  1214. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1215. if (ret < 0)
  1216. goto out;
  1217. if (ret > 0) {
  1218. ret = -ENOENT;
  1219. goto out;
  1220. }
  1221. ret = btrfs_del_item(trans, root, path);
  1222. if (ret)
  1223. goto out;
  1224. out:
  1225. btrfs_free_path(path);
  1226. unlock_chunks(root);
  1227. btrfs_commit_transaction(trans, root);
  1228. return ret;
  1229. }
  1230. int btrfs_rm_device(struct btrfs_root *root, char *device_path)
  1231. {
  1232. struct btrfs_device *device;
  1233. struct btrfs_device *next_device;
  1234. struct block_device *bdev;
  1235. struct buffer_head *bh = NULL;
  1236. struct btrfs_super_block *disk_super;
  1237. struct btrfs_fs_devices *cur_devices;
  1238. u64 all_avail;
  1239. u64 devid;
  1240. u64 num_devices;
  1241. u8 *dev_uuid;
  1242. unsigned seq;
  1243. int ret = 0;
  1244. bool clear_super = false;
  1245. mutex_lock(&uuid_mutex);
  1246. do {
  1247. seq = read_seqbegin(&root->fs_info->profiles_lock);
  1248. all_avail = root->fs_info->avail_data_alloc_bits |
  1249. root->fs_info->avail_system_alloc_bits |
  1250. root->fs_info->avail_metadata_alloc_bits;
  1251. } while (read_seqretry(&root->fs_info->profiles_lock, seq));
  1252. num_devices = root->fs_info->fs_devices->num_devices;
  1253. btrfs_dev_replace_lock(&root->fs_info->dev_replace);
  1254. if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
  1255. WARN_ON(num_devices < 1);
  1256. num_devices--;
  1257. }
  1258. btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
  1259. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
  1260. printk(KERN_ERR "btrfs: unable to go below four devices "
  1261. "on raid10\n");
  1262. ret = -EINVAL;
  1263. goto out;
  1264. }
  1265. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
  1266. printk(KERN_ERR "btrfs: unable to go below two "
  1267. "devices on raid1\n");
  1268. ret = -EINVAL;
  1269. goto out;
  1270. }
  1271. if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
  1272. root->fs_info->fs_devices->rw_devices <= 2) {
  1273. printk(KERN_ERR "btrfs: unable to go below two "
  1274. "devices on raid5\n");
  1275. ret = -EINVAL;
  1276. goto out;
  1277. }
  1278. if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
  1279. root->fs_info->fs_devices->rw_devices <= 3) {
  1280. printk(KERN_ERR "btrfs: unable to go below three "
  1281. "devices on raid6\n");
  1282. ret = -EINVAL;
  1283. goto out;
  1284. }
  1285. if (strcmp(device_path, "missing") == 0) {
  1286. struct list_head *devices;
  1287. struct btrfs_device *tmp;
  1288. device = NULL;
  1289. devices = &root->fs_info->fs_devices->devices;
  1290. /*
  1291. * It is safe to read the devices since the volume_mutex
  1292. * is held.
  1293. */
  1294. list_for_each_entry(tmp, devices, dev_list) {
  1295. if (tmp->in_fs_metadata &&
  1296. !tmp->is_tgtdev_for_dev_replace &&
  1297. !tmp->bdev) {
  1298. device = tmp;
  1299. break;
  1300. }
  1301. }
  1302. bdev = NULL;
  1303. bh = NULL;
  1304. disk_super = NULL;
  1305. if (!device) {
  1306. printk(KERN_ERR "btrfs: no missing devices found to "
  1307. "remove\n");
  1308. goto out;
  1309. }
  1310. } else {
  1311. ret = btrfs_get_bdev_and_sb(device_path,
  1312. FMODE_WRITE | FMODE_EXCL,
  1313. root->fs_info->bdev_holder, 0,
  1314. &bdev, &bh);
  1315. if (ret)
  1316. goto out;
  1317. disk_super = (struct btrfs_super_block *)bh->b_data;
  1318. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1319. dev_uuid = disk_super->dev_item.uuid;
  1320. device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1321. disk_super->fsid);
  1322. if (!device) {
  1323. ret = -ENOENT;
  1324. goto error_brelse;
  1325. }
  1326. }
  1327. if (device->is_tgtdev_for_dev_replace) {
  1328. pr_err("btrfs: unable to remove the dev_replace target dev\n");
  1329. ret = -EINVAL;
  1330. goto error_brelse;
  1331. }
  1332. if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
  1333. printk(KERN_ERR "btrfs: unable to remove the only writeable "
  1334. "device\n");
  1335. ret = -EINVAL;
  1336. goto error_brelse;
  1337. }
  1338. if (device->writeable) {
  1339. lock_chunks(root);
  1340. list_del_init(&device->dev_alloc_list);
  1341. unlock_chunks(root);
  1342. root->fs_info->fs_devices->rw_devices--;
  1343. clear_super = true;
  1344. }
  1345. ret = btrfs_shrink_device(device, 0);
  1346. if (ret)
  1347. goto error_undo;
  1348. /*
  1349. * TODO: the superblock still includes this device in its num_devices
  1350. * counter although write_all_supers() is not locked out. This
  1351. * could give a filesystem state which requires a degraded mount.
  1352. */
  1353. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  1354. if (ret)
  1355. goto error_undo;
  1356. spin_lock(&root->fs_info->free_chunk_lock);
  1357. root->fs_info->free_chunk_space = device->total_bytes -
  1358. device->bytes_used;
  1359. spin_unlock(&root->fs_info->free_chunk_lock);
  1360. device->in_fs_metadata = 0;
  1361. btrfs_scrub_cancel_dev(root->fs_info, device);
  1362. /*
  1363. * the device list mutex makes sure that we don't change
  1364. * the device list while someone else is writing out all
  1365. * the device supers.
  1366. */
  1367. cur_devices = device->fs_devices;
  1368. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1369. list_del_rcu(&device->dev_list);
  1370. device->fs_devices->num_devices--;
  1371. device->fs_devices->total_devices--;
  1372. if (device->missing)
  1373. root->fs_info->fs_devices->missing_devices--;
  1374. next_device = list_entry(root->fs_info->fs_devices->devices.next,
  1375. struct btrfs_device, dev_list);
  1376. if (device->bdev == root->fs_info->sb->s_bdev)
  1377. root->fs_info->sb->s_bdev = next_device->bdev;
  1378. if (device->bdev == root->fs_info->fs_devices->latest_bdev)
  1379. root->fs_info->fs_devices->latest_bdev = next_device->bdev;
  1380. if (device->bdev)
  1381. device->fs_devices->open_devices--;
  1382. call_rcu(&device->rcu, free_device);
  1383. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1384. num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
  1385. btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
  1386. if (cur_devices->open_devices == 0) {
  1387. struct btrfs_fs_devices *fs_devices;
  1388. fs_devices = root->fs_info->fs_devices;
  1389. while (fs_devices) {
  1390. if (fs_devices->seed == cur_devices)
  1391. break;
  1392. fs_devices = fs_devices->seed;
  1393. }
  1394. fs_devices->seed = cur_devices->seed;
  1395. cur_devices->seed = NULL;
  1396. lock_chunks(root);
  1397. __btrfs_close_devices(cur_devices);
  1398. unlock_chunks(root);
  1399. free_fs_devices(cur_devices);
  1400. }
  1401. root->fs_info->num_tolerated_disk_barrier_failures =
  1402. btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
  1403. /*
  1404. * at this point, the device is zero sized. We want to
  1405. * remove it from the devices list and zero out the old super
  1406. */
  1407. if (clear_super && disk_super) {
  1408. /* make sure this device isn't detected as part of
  1409. * the FS anymore
  1410. */
  1411. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  1412. set_buffer_dirty(bh);
  1413. sync_dirty_buffer(bh);
  1414. }
  1415. ret = 0;
  1416. /* Notify udev that device has changed */
  1417. if (bdev)
  1418. btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
  1419. error_brelse:
  1420. brelse(bh);
  1421. if (bdev)
  1422. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  1423. out:
  1424. mutex_unlock(&uuid_mutex);
  1425. return ret;
  1426. error_undo:
  1427. if (device->writeable) {
  1428. lock_chunks(root);
  1429. list_add(&device->dev_alloc_list,
  1430. &root->fs_info->fs_devices->alloc_list);
  1431. unlock_chunks(root);
  1432. root->fs_info->fs_devices->rw_devices++;
  1433. }
  1434. goto error_brelse;
  1435. }
  1436. void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
  1437. struct btrfs_device *srcdev)
  1438. {
  1439. WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
  1440. list_del_rcu(&srcdev->dev_list);
  1441. list_del_rcu(&srcdev->dev_alloc_list);
  1442. fs_info->fs_devices->num_devices--;
  1443. if (srcdev->missing) {
  1444. fs_info->fs_devices->missing_devices--;
  1445. fs_info->fs_devices->rw_devices++;
  1446. }
  1447. if (srcdev->can_discard)
  1448. fs_info->fs_devices->num_can_discard--;
  1449. if (srcdev->bdev)
  1450. fs_info->fs_devices->open_devices--;
  1451. call_rcu(&srcdev->rcu, free_device);
  1452. }
  1453. void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
  1454. struct btrfs_device *tgtdev)
  1455. {
  1456. struct btrfs_device *next_device;
  1457. WARN_ON(!tgtdev);
  1458. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  1459. if (tgtdev->bdev) {
  1460. btrfs_scratch_superblock(tgtdev);
  1461. fs_info->fs_devices->open_devices--;
  1462. }
  1463. fs_info->fs_devices->num_devices--;
  1464. if (tgtdev->can_discard)
  1465. fs_info->fs_devices->num_can_discard++;
  1466. next_device = list_entry(fs_info->fs_devices->devices.next,
  1467. struct btrfs_device, dev_list);
  1468. if (tgtdev->bdev == fs_info->sb->s_bdev)
  1469. fs_info->sb->s_bdev = next_device->bdev;
  1470. if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
  1471. fs_info->fs_devices->latest_bdev = next_device->bdev;
  1472. list_del_rcu(&tgtdev->dev_list);
  1473. call_rcu(&tgtdev->rcu, free_device);
  1474. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  1475. }
  1476. int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
  1477. struct btrfs_device **device)
  1478. {
  1479. int ret = 0;
  1480. struct btrfs_super_block *disk_super;
  1481. u64 devid;
  1482. u8 *dev_uuid;
  1483. struct block_device *bdev;
  1484. struct buffer_head *bh;
  1485. *device = NULL;
  1486. ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
  1487. root->fs_info->bdev_holder, 0, &bdev, &bh);
  1488. if (ret)
  1489. return ret;
  1490. disk_super = (struct btrfs_super_block *)bh->b_data;
  1491. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1492. dev_uuid = disk_super->dev_item.uuid;
  1493. *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1494. disk_super->fsid);
  1495. brelse(bh);
  1496. if (!*device)
  1497. ret = -ENOENT;
  1498. blkdev_put(bdev, FMODE_READ);
  1499. return ret;
  1500. }
  1501. int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
  1502. char *device_path,
  1503. struct btrfs_device **device)
  1504. {
  1505. *device = NULL;
  1506. if (strcmp(device_path, "missing") == 0) {
  1507. struct list_head *devices;
  1508. struct btrfs_device *tmp;
  1509. devices = &root->fs_info->fs_devices->devices;
  1510. /*
  1511. * It is safe to read the devices since the volume_mutex
  1512. * is held by the caller.
  1513. */
  1514. list_for_each_entry(tmp, devices, dev_list) {
  1515. if (tmp->in_fs_metadata && !tmp->bdev) {
  1516. *device = tmp;
  1517. break;
  1518. }
  1519. }
  1520. if (!*device) {
  1521. pr_err("btrfs: no missing device found\n");
  1522. return -ENOENT;
  1523. }
  1524. return 0;
  1525. } else {
  1526. return btrfs_find_device_by_path(root, device_path, device);
  1527. }
  1528. }
  1529. /*
  1530. * does all the dirty work required for changing file system's UUID.
  1531. */
  1532. static int btrfs_prepare_sprout(struct btrfs_root *root)
  1533. {
  1534. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1535. struct btrfs_fs_devices *old_devices;
  1536. struct btrfs_fs_devices *seed_devices;
  1537. struct btrfs_super_block *disk_super = root->fs_info->super_copy;
  1538. struct btrfs_device *device;
  1539. u64 super_flags;
  1540. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1541. if (!fs_devices->seeding)
  1542. return -EINVAL;
  1543. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1544. if (!seed_devices)
  1545. return -ENOMEM;
  1546. old_devices = clone_fs_devices(fs_devices);
  1547. if (IS_ERR(old_devices)) {
  1548. kfree(seed_devices);
  1549. return PTR_ERR(old_devices);
  1550. }
  1551. list_add(&old_devices->list, &fs_uuids);
  1552. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1553. seed_devices->opened = 1;
  1554. INIT_LIST_HEAD(&seed_devices->devices);
  1555. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1556. mutex_init(&seed_devices->device_list_mutex);
  1557. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1558. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1559. synchronize_rcu);
  1560. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1561. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1562. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1563. device->fs_devices = seed_devices;
  1564. }
  1565. fs_devices->seeding = 0;
  1566. fs_devices->num_devices = 0;
  1567. fs_devices->open_devices = 0;
  1568. fs_devices->total_devices = 0;
  1569. fs_devices->seed = seed_devices;
  1570. generate_random_uuid(fs_devices->fsid);
  1571. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1572. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1573. super_flags = btrfs_super_flags(disk_super) &
  1574. ~BTRFS_SUPER_FLAG_SEEDING;
  1575. btrfs_set_super_flags(disk_super, super_flags);
  1576. return 0;
  1577. }
  1578. /*
  1579. * strore the expected generation for seed devices in device items.
  1580. */
  1581. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1582. struct btrfs_root *root)
  1583. {
  1584. struct btrfs_path *path;
  1585. struct extent_buffer *leaf;
  1586. struct btrfs_dev_item *dev_item;
  1587. struct btrfs_device *device;
  1588. struct btrfs_key key;
  1589. u8 fs_uuid[BTRFS_UUID_SIZE];
  1590. u8 dev_uuid[BTRFS_UUID_SIZE];
  1591. u64 devid;
  1592. int ret;
  1593. path = btrfs_alloc_path();
  1594. if (!path)
  1595. return -ENOMEM;
  1596. root = root->fs_info->chunk_root;
  1597. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1598. key.offset = 0;
  1599. key.type = BTRFS_DEV_ITEM_KEY;
  1600. while (1) {
  1601. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1602. if (ret < 0)
  1603. goto error;
  1604. leaf = path->nodes[0];
  1605. next_slot:
  1606. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1607. ret = btrfs_next_leaf(root, path);
  1608. if (ret > 0)
  1609. break;
  1610. if (ret < 0)
  1611. goto error;
  1612. leaf = path->nodes[0];
  1613. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1614. btrfs_release_path(path);
  1615. continue;
  1616. }
  1617. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1618. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1619. key.type != BTRFS_DEV_ITEM_KEY)
  1620. break;
  1621. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1622. struct btrfs_dev_item);
  1623. devid = btrfs_device_id(leaf, dev_item);
  1624. read_extent_buffer(leaf, dev_uuid,
  1625. (unsigned long)btrfs_device_uuid(dev_item),
  1626. BTRFS_UUID_SIZE);
  1627. read_extent_buffer(leaf, fs_uuid,
  1628. (unsigned long)btrfs_device_fsid(dev_item),
  1629. BTRFS_UUID_SIZE);
  1630. device = btrfs_find_device(root->fs_info, devid, dev_uuid,
  1631. fs_uuid);
  1632. BUG_ON(!device); /* Logic error */
  1633. if (device->fs_devices->seeding) {
  1634. btrfs_set_device_generation(leaf, dev_item,
  1635. device->generation);
  1636. btrfs_mark_buffer_dirty(leaf);
  1637. }
  1638. path->slots[0]++;
  1639. goto next_slot;
  1640. }
  1641. ret = 0;
  1642. error:
  1643. btrfs_free_path(path);
  1644. return ret;
  1645. }
  1646. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1647. {
  1648. struct request_queue *q;
  1649. struct btrfs_trans_handle *trans;
  1650. struct btrfs_device *device;
  1651. struct block_device *bdev;
  1652. struct list_head *devices;
  1653. struct super_block *sb = root->fs_info->sb;
  1654. struct rcu_string *name;
  1655. u64 total_bytes;
  1656. int seeding_dev = 0;
  1657. int ret = 0;
  1658. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1659. return -EROFS;
  1660. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1661. root->fs_info->bdev_holder);
  1662. if (IS_ERR(bdev))
  1663. return PTR_ERR(bdev);
  1664. if (root->fs_info->fs_devices->seeding) {
  1665. seeding_dev = 1;
  1666. down_write(&sb->s_umount);
  1667. mutex_lock(&uuid_mutex);
  1668. }
  1669. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1670. devices = &root->fs_info->fs_devices->devices;
  1671. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1672. list_for_each_entry(device, devices, dev_list) {
  1673. if (device->bdev == bdev) {
  1674. ret = -EEXIST;
  1675. mutex_unlock(
  1676. &root->fs_info->fs_devices->device_list_mutex);
  1677. goto error;
  1678. }
  1679. }
  1680. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1681. device = kzalloc(sizeof(*device), GFP_NOFS);
  1682. if (!device) {
  1683. /* we can safely leave the fs_devices entry around */
  1684. ret = -ENOMEM;
  1685. goto error;
  1686. }
  1687. name = rcu_string_strdup(device_path, GFP_NOFS);
  1688. if (!name) {
  1689. kfree(device);
  1690. ret = -ENOMEM;
  1691. goto error;
  1692. }
  1693. rcu_assign_pointer(device->name, name);
  1694. ret = find_next_devid(root, &device->devid);
  1695. if (ret) {
  1696. rcu_string_free(device->name);
  1697. kfree(device);
  1698. goto error;
  1699. }
  1700. trans = btrfs_start_transaction(root, 0);
  1701. if (IS_ERR(trans)) {
  1702. rcu_string_free(device->name);
  1703. kfree(device);
  1704. ret = PTR_ERR(trans);
  1705. goto error;
  1706. }
  1707. lock_chunks(root);
  1708. q = bdev_get_queue(bdev);
  1709. if (blk_queue_discard(q))
  1710. device->can_discard = 1;
  1711. device->writeable = 1;
  1712. device->work.func = pending_bios_fn;
  1713. generate_random_uuid(device->uuid);
  1714. spin_lock_init(&device->io_lock);
  1715. device->generation = trans->transid;
  1716. device->io_width = root->sectorsize;
  1717. device->io_align = root->sectorsize;
  1718. device->sector_size = root->sectorsize;
  1719. device->total_bytes = i_size_read(bdev->bd_inode);
  1720. device->disk_total_bytes = device->total_bytes;
  1721. device->dev_root = root->fs_info->dev_root;
  1722. device->bdev = bdev;
  1723. device->in_fs_metadata = 1;
  1724. device->is_tgtdev_for_dev_replace = 0;
  1725. device->mode = FMODE_EXCL;
  1726. set_blocksize(device->bdev, 4096);
  1727. if (seeding_dev) {
  1728. sb->s_flags &= ~MS_RDONLY;
  1729. ret = btrfs_prepare_sprout(root);
  1730. BUG_ON(ret); /* -ENOMEM */
  1731. }
  1732. device->fs_devices = root->fs_info->fs_devices;
  1733. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1734. list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
  1735. list_add(&device->dev_alloc_list,
  1736. &root->fs_info->fs_devices->alloc_list);
  1737. root->fs_info->fs_devices->num_devices++;
  1738. root->fs_info->fs_devices->open_devices++;
  1739. root->fs_info->fs_devices->rw_devices++;
  1740. root->fs_info->fs_devices->total_devices++;
  1741. if (device->can_discard)
  1742. root->fs_info->fs_devices->num_can_discard++;
  1743. root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  1744. spin_lock(&root->fs_info->free_chunk_lock);
  1745. root->fs_info->free_chunk_space += device->total_bytes;
  1746. spin_unlock(&root->fs_info->free_chunk_lock);
  1747. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  1748. root->fs_info->fs_devices->rotating = 1;
  1749. total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
  1750. btrfs_set_super_total_bytes(root->fs_info->super_copy,
  1751. total_bytes + device->total_bytes);
  1752. total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
  1753. btrfs_set_super_num_devices(root->fs_info->super_copy,
  1754. total_bytes + 1);
  1755. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1756. if (seeding_dev) {
  1757. ret = init_first_rw_device(trans, root, device);
  1758. if (ret) {
  1759. btrfs_abort_transaction(trans, root, ret);
  1760. goto error_trans;
  1761. }
  1762. ret = btrfs_finish_sprout(trans, root);
  1763. if (ret) {
  1764. btrfs_abort_transaction(trans, root, ret);
  1765. goto error_trans;
  1766. }
  1767. } else {
  1768. ret = btrfs_add_device(trans, root, device);
  1769. if (ret) {
  1770. btrfs_abort_transaction(trans, root, ret);
  1771. goto error_trans;
  1772. }
  1773. }
  1774. /*
  1775. * we've got more storage, clear any full flags on the space
  1776. * infos
  1777. */
  1778. btrfs_clear_space_info_full(root->fs_info);
  1779. unlock_chunks(root);
  1780. root->fs_info->num_tolerated_disk_barrier_failures =
  1781. btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
  1782. ret = btrfs_commit_transaction(trans, root);
  1783. if (seeding_dev) {
  1784. mutex_unlock(&uuid_mutex);
  1785. up_write(&sb->s_umount);
  1786. if (ret) /* transaction commit */
  1787. return ret;
  1788. ret = btrfs_relocate_sys_chunks(root);
  1789. if (ret < 0)
  1790. btrfs_error(root->fs_info, ret,
  1791. "Failed to relocate sys chunks after "
  1792. "device initialization. This can be fixed "
  1793. "using the \"btrfs balance\" command.");
  1794. trans = btrfs_attach_transaction(root);
  1795. if (IS_ERR(trans)) {
  1796. if (PTR_ERR(trans) == -ENOENT)
  1797. return 0;
  1798. return PTR_ERR(trans);
  1799. }
  1800. ret = btrfs_commit_transaction(trans, root);
  1801. }
  1802. return ret;
  1803. error_trans:
  1804. unlock_chunks(root);
  1805. btrfs_end_transaction(trans, root);
  1806. rcu_string_free(device->name);
  1807. kfree(device);
  1808. error:
  1809. blkdev_put(bdev, FMODE_EXCL);
  1810. if (seeding_dev) {
  1811. mutex_unlock(&uuid_mutex);
  1812. up_write(&sb->s_umount);
  1813. }
  1814. return ret;
  1815. }
  1816. int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
  1817. struct btrfs_device **device_out)
  1818. {
  1819. struct request_queue *q;
  1820. struct btrfs_device *device;
  1821. struct block_device *bdev;
  1822. struct btrfs_fs_info *fs_info = root->fs_info;
  1823. struct list_head *devices;
  1824. struct rcu_string *name;
  1825. int ret = 0;
  1826. *device_out = NULL;
  1827. if (fs_info->fs_devices->seeding)
  1828. return -EINVAL;
  1829. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1830. fs_info->bdev_holder);
  1831. if (IS_ERR(bdev))
  1832. return PTR_ERR(bdev);
  1833. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1834. devices = &fs_info->fs_devices->devices;
  1835. list_for_each_entry(device, devices, dev_list) {
  1836. if (device->bdev == bdev) {
  1837. ret = -EEXIST;
  1838. goto error;
  1839. }
  1840. }
  1841. device = kzalloc(sizeof(*device), GFP_NOFS);
  1842. if (!device) {
  1843. ret = -ENOMEM;
  1844. goto error;
  1845. }
  1846. name = rcu_string_strdup(device_path, GFP_NOFS);
  1847. if (!name) {
  1848. kfree(device);
  1849. ret = -ENOMEM;
  1850. goto error;
  1851. }
  1852. rcu_assign_pointer(device->name, name);
  1853. q = bdev_get_queue(bdev);
  1854. if (blk_queue_discard(q))
  1855. device->can_discard = 1;
  1856. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1857. device->writeable = 1;
  1858. device->work.func = pending_bios_fn;
  1859. generate_random_uuid(device->uuid);
  1860. device->devid = BTRFS_DEV_REPLACE_DEVID;
  1861. spin_lock_init(&device->io_lock);
  1862. device->generation = 0;
  1863. device->io_width = root->sectorsize;
  1864. device->io_align = root->sectorsize;
  1865. device->sector_size = root->sectorsize;
  1866. device->total_bytes = i_size_read(bdev->bd_inode);
  1867. device->disk_total_bytes = device->total_bytes;
  1868. device->dev_root = fs_info->dev_root;
  1869. device->bdev = bdev;
  1870. device->in_fs_metadata = 1;
  1871. device->is_tgtdev_for_dev_replace = 1;
  1872. device->mode = FMODE_EXCL;
  1873. set_blocksize(device->bdev, 4096);
  1874. device->fs_devices = fs_info->fs_devices;
  1875. list_add(&device->dev_list, &fs_info->fs_devices->devices);
  1876. fs_info->fs_devices->num_devices++;
  1877. fs_info->fs_devices->open_devices++;
  1878. if (device->can_discard)
  1879. fs_info->fs_devices->num_can_discard++;
  1880. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1881. *device_out = device;
  1882. return ret;
  1883. error:
  1884. blkdev_put(bdev, FMODE_EXCL);
  1885. return ret;
  1886. }
  1887. void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
  1888. struct btrfs_device *tgtdev)
  1889. {
  1890. WARN_ON(fs_info->fs_devices->rw_devices == 0);
  1891. tgtdev->io_width = fs_info->dev_root->sectorsize;
  1892. tgtdev->io_align = fs_info->dev_root->sectorsize;
  1893. tgtdev->sector_size = fs_info->dev_root->sectorsize;
  1894. tgtdev->dev_root = fs_info->dev_root;
  1895. tgtdev->in_fs_metadata = 1;
  1896. }
  1897. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1898. struct btrfs_device *device)
  1899. {
  1900. int ret;
  1901. struct btrfs_path *path;
  1902. struct btrfs_root *root;
  1903. struct btrfs_dev_item *dev_item;
  1904. struct extent_buffer *leaf;
  1905. struct btrfs_key key;
  1906. root = device->dev_root->fs_info->chunk_root;
  1907. path = btrfs_alloc_path();
  1908. if (!path)
  1909. return -ENOMEM;
  1910. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1911. key.type = BTRFS_DEV_ITEM_KEY;
  1912. key.offset = device->devid;
  1913. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1914. if (ret < 0)
  1915. goto out;
  1916. if (ret > 0) {
  1917. ret = -ENOENT;
  1918. goto out;
  1919. }
  1920. leaf = path->nodes[0];
  1921. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1922. btrfs_set_device_id(leaf, dev_item, device->devid);
  1923. btrfs_set_device_type(leaf, dev_item, device->type);
  1924. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1925. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1926. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1927. btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
  1928. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1929. btrfs_mark_buffer_dirty(leaf);
  1930. out:
  1931. btrfs_free_path(path);
  1932. return ret;
  1933. }
  1934. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1935. struct btrfs_device *device, u64 new_size)
  1936. {
  1937. struct btrfs_super_block *super_copy =
  1938. device->dev_root->fs_info->super_copy;
  1939. u64 old_total = btrfs_super_total_bytes(super_copy);
  1940. u64 diff = new_size - device->total_bytes;
  1941. if (!device->writeable)
  1942. return -EACCES;
  1943. if (new_size <= device->total_bytes ||
  1944. device->is_tgtdev_for_dev_replace)
  1945. return -EINVAL;
  1946. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1947. device->fs_devices->total_rw_bytes += diff;
  1948. device->total_bytes = new_size;
  1949. device->disk_total_bytes = new_size;
  1950. btrfs_clear_space_info_full(device->dev_root->fs_info);
  1951. return btrfs_update_device(trans, device);
  1952. }
  1953. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1954. struct btrfs_device *device, u64 new_size)
  1955. {
  1956. int ret;
  1957. lock_chunks(device->dev_root);
  1958. ret = __btrfs_grow_device(trans, device, new_size);
  1959. unlock_chunks(device->dev_root);
  1960. return ret;
  1961. }
  1962. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1963. struct btrfs_root *root,
  1964. u64 chunk_tree, u64 chunk_objectid,
  1965. u64 chunk_offset)
  1966. {
  1967. int ret;
  1968. struct btrfs_path *path;
  1969. struct btrfs_key key;
  1970. root = root->fs_info->chunk_root;
  1971. path = btrfs_alloc_path();
  1972. if (!path)
  1973. return -ENOMEM;
  1974. key.objectid = chunk_objectid;
  1975. key.offset = chunk_offset;
  1976. key.type = BTRFS_CHUNK_ITEM_KEY;
  1977. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1978. if (ret < 0)
  1979. goto out;
  1980. else if (ret > 0) { /* Logic error or corruption */
  1981. btrfs_error(root->fs_info, -ENOENT,
  1982. "Failed lookup while freeing chunk.");
  1983. ret = -ENOENT;
  1984. goto out;
  1985. }
  1986. ret = btrfs_del_item(trans, root, path);
  1987. if (ret < 0)
  1988. btrfs_error(root->fs_info, ret,
  1989. "Failed to delete chunk item.");
  1990. out:
  1991. btrfs_free_path(path);
  1992. return ret;
  1993. }
  1994. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1995. chunk_offset)
  1996. {
  1997. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1998. struct btrfs_disk_key *disk_key;
  1999. struct btrfs_chunk *chunk;
  2000. u8 *ptr;
  2001. int ret = 0;
  2002. u32 num_stripes;
  2003. u32 array_size;
  2004. u32 len = 0;
  2005. u32 cur;
  2006. struct btrfs_key key;
  2007. array_size = btrfs_super_sys_array_size(super_copy);
  2008. ptr = super_copy->sys_chunk_array;
  2009. cur = 0;
  2010. while (cur < array_size) {
  2011. disk_key = (struct btrfs_disk_key *)ptr;
  2012. btrfs_disk_key_to_cpu(&key, disk_key);
  2013. len = sizeof(*disk_key);
  2014. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  2015. chunk = (struct btrfs_chunk *)(ptr + len);
  2016. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  2017. len += btrfs_chunk_item_size(num_stripes);
  2018. } else {
  2019. ret = -EIO;
  2020. break;
  2021. }
  2022. if (key.objectid == chunk_objectid &&
  2023. key.offset == chunk_offset) {
  2024. memmove(ptr, ptr + len, array_size - (cur + len));
  2025. array_size -= len;
  2026. btrfs_set_super_sys_array_size(super_copy, array_size);
  2027. } else {
  2028. ptr += len;
  2029. cur += len;
  2030. }
  2031. }
  2032. return ret;
  2033. }
  2034. static int btrfs_relocate_chunk(struct btrfs_root *root,
  2035. u64 chunk_tree, u64 chunk_objectid,
  2036. u64 chunk_offset)
  2037. {
  2038. struct extent_map_tree *em_tree;
  2039. struct btrfs_root *extent_root;
  2040. struct btrfs_trans_handle *trans;
  2041. struct extent_map *em;
  2042. struct map_lookup *map;
  2043. int ret;
  2044. int i;
  2045. root = root->fs_info->chunk_root;
  2046. extent_root = root->fs_info->extent_root;
  2047. em_tree = &root->fs_info->mapping_tree.map_tree;
  2048. ret = btrfs_can_relocate(extent_root, chunk_offset);
  2049. if (ret)
  2050. return -ENOSPC;
  2051. /* step one, relocate all the extents inside this chunk */
  2052. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  2053. if (ret)
  2054. return ret;
  2055. trans = btrfs_start_transaction(root, 0);
  2056. if (IS_ERR(trans)) {
  2057. ret = PTR_ERR(trans);
  2058. btrfs_std_error(root->fs_info, ret);
  2059. return ret;
  2060. }
  2061. lock_chunks(root);
  2062. /*
  2063. * step two, delete the device extents and the
  2064. * chunk tree entries
  2065. */
  2066. read_lock(&em_tree->lock);
  2067. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  2068. read_unlock(&em_tree->lock);
  2069. BUG_ON(!em || em->start > chunk_offset ||
  2070. em->start + em->len < chunk_offset);
  2071. map = (struct map_lookup *)em->bdev;
  2072. for (i = 0; i < map->num_stripes; i++) {
  2073. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  2074. map->stripes[i].physical);
  2075. BUG_ON(ret);
  2076. if (map->stripes[i].dev) {
  2077. ret = btrfs_update_device(trans, map->stripes[i].dev);
  2078. BUG_ON(ret);
  2079. }
  2080. }
  2081. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  2082. chunk_offset);
  2083. BUG_ON(ret);
  2084. trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
  2085. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2086. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  2087. BUG_ON(ret);
  2088. }
  2089. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  2090. BUG_ON(ret);
  2091. write_lock(&em_tree->lock);
  2092. remove_extent_mapping(em_tree, em);
  2093. write_unlock(&em_tree->lock);
  2094. kfree(map);
  2095. em->bdev = NULL;
  2096. /* once for the tree */
  2097. free_extent_map(em);
  2098. /* once for us */
  2099. free_extent_map(em);
  2100. unlock_chunks(root);
  2101. btrfs_end_transaction(trans, root);
  2102. return 0;
  2103. }
  2104. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  2105. {
  2106. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  2107. struct btrfs_path *path;
  2108. struct extent_buffer *leaf;
  2109. struct btrfs_chunk *chunk;
  2110. struct btrfs_key key;
  2111. struct btrfs_key found_key;
  2112. u64 chunk_tree = chunk_root->root_key.objectid;
  2113. u64 chunk_type;
  2114. bool retried = false;
  2115. int failed = 0;
  2116. int ret;
  2117. path = btrfs_alloc_path();
  2118. if (!path)
  2119. return -ENOMEM;
  2120. again:
  2121. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2122. key.offset = (u64)-1;
  2123. key.type = BTRFS_CHUNK_ITEM_KEY;
  2124. while (1) {
  2125. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2126. if (ret < 0)
  2127. goto error;
  2128. BUG_ON(ret == 0); /* Corruption */
  2129. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  2130. key.type);
  2131. if (ret < 0)
  2132. goto error;
  2133. if (ret > 0)
  2134. break;
  2135. leaf = path->nodes[0];
  2136. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2137. chunk = btrfs_item_ptr(leaf, path->slots[0],
  2138. struct btrfs_chunk);
  2139. chunk_type = btrfs_chunk_type(leaf, chunk);
  2140. btrfs_release_path(path);
  2141. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2142. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  2143. found_key.objectid,
  2144. found_key.offset);
  2145. if (ret == -ENOSPC)
  2146. failed++;
  2147. else if (ret)
  2148. BUG();
  2149. }
  2150. if (found_key.offset == 0)
  2151. break;
  2152. key.offset = found_key.offset - 1;
  2153. }
  2154. ret = 0;
  2155. if (failed && !retried) {
  2156. failed = 0;
  2157. retried = true;
  2158. goto again;
  2159. } else if (failed && retried) {
  2160. WARN_ON(1);
  2161. ret = -ENOSPC;
  2162. }
  2163. error:
  2164. btrfs_free_path(path);
  2165. return ret;
  2166. }
  2167. static int insert_balance_item(struct btrfs_root *root,
  2168. struct btrfs_balance_control *bctl)
  2169. {
  2170. struct btrfs_trans_handle *trans;
  2171. struct btrfs_balance_item *item;
  2172. struct btrfs_disk_balance_args disk_bargs;
  2173. struct btrfs_path *path;
  2174. struct extent_buffer *leaf;
  2175. struct btrfs_key key;
  2176. int ret, err;
  2177. path = btrfs_alloc_path();
  2178. if (!path)
  2179. return -ENOMEM;
  2180. trans = btrfs_start_transaction(root, 0);
  2181. if (IS_ERR(trans)) {
  2182. btrfs_free_path(path);
  2183. return PTR_ERR(trans);
  2184. }
  2185. key.objectid = BTRFS_BALANCE_OBJECTID;
  2186. key.type = BTRFS_BALANCE_ITEM_KEY;
  2187. key.offset = 0;
  2188. ret = btrfs_insert_empty_item(trans, root, path, &key,
  2189. sizeof(*item));
  2190. if (ret)
  2191. goto out;
  2192. leaf = path->nodes[0];
  2193. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2194. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  2195. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
  2196. btrfs_set_balance_data(leaf, item, &disk_bargs);
  2197. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
  2198. btrfs_set_balance_meta(leaf, item, &disk_bargs);
  2199. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
  2200. btrfs_set_balance_sys(leaf, item, &disk_bargs);
  2201. btrfs_set_balance_flags(leaf, item, bctl->flags);
  2202. btrfs_mark_buffer_dirty(leaf);
  2203. out:
  2204. btrfs_free_path(path);
  2205. err = btrfs_commit_transaction(trans, root);
  2206. if (err && !ret)
  2207. ret = err;
  2208. return ret;
  2209. }
  2210. static int del_balance_item(struct btrfs_root *root)
  2211. {
  2212. struct btrfs_trans_handle *trans;
  2213. struct btrfs_path *path;
  2214. struct btrfs_key key;
  2215. int ret, err;
  2216. path = btrfs_alloc_path();
  2217. if (!path)
  2218. return -ENOMEM;
  2219. trans = btrfs_start_transaction(root, 0);
  2220. if (IS_ERR(trans)) {
  2221. btrfs_free_path(path);
  2222. return PTR_ERR(trans);
  2223. }
  2224. key.objectid = BTRFS_BALANCE_OBJECTID;
  2225. key.type = BTRFS_BALANCE_ITEM_KEY;
  2226. key.offset = 0;
  2227. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2228. if (ret < 0)
  2229. goto out;
  2230. if (ret > 0) {
  2231. ret = -ENOENT;
  2232. goto out;
  2233. }
  2234. ret = btrfs_del_item(trans, root, path);
  2235. out:
  2236. btrfs_free_path(path);
  2237. err = btrfs_commit_transaction(trans, root);
  2238. if (err && !ret)
  2239. ret = err;
  2240. return ret;
  2241. }
  2242. /*
  2243. * This is a heuristic used to reduce the number of chunks balanced on
  2244. * resume after balance was interrupted.
  2245. */
  2246. static void update_balance_args(struct btrfs_balance_control *bctl)
  2247. {
  2248. /*
  2249. * Turn on soft mode for chunk types that were being converted.
  2250. */
  2251. if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2252. bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2253. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2254. bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2255. if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
  2256. bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
  2257. /*
  2258. * Turn on usage filter if is not already used. The idea is
  2259. * that chunks that we have already balanced should be
  2260. * reasonably full. Don't do it for chunks that are being
  2261. * converted - that will keep us from relocating unconverted
  2262. * (albeit full) chunks.
  2263. */
  2264. if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2265. !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2266. bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2267. bctl->data.usage = 90;
  2268. }
  2269. if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2270. !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2271. bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2272. bctl->sys.usage = 90;
  2273. }
  2274. if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2275. !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
  2276. bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
  2277. bctl->meta.usage = 90;
  2278. }
  2279. }
  2280. /*
  2281. * Should be called with both balance and volume mutexes held to
  2282. * serialize other volume operations (add_dev/rm_dev/resize) with
  2283. * restriper. Same goes for unset_balance_control.
  2284. */
  2285. static void set_balance_control(struct btrfs_balance_control *bctl)
  2286. {
  2287. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2288. BUG_ON(fs_info->balance_ctl);
  2289. spin_lock(&fs_info->balance_lock);
  2290. fs_info->balance_ctl = bctl;
  2291. spin_unlock(&fs_info->balance_lock);
  2292. }
  2293. static void unset_balance_control(struct btrfs_fs_info *fs_info)
  2294. {
  2295. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2296. BUG_ON(!fs_info->balance_ctl);
  2297. spin_lock(&fs_info->balance_lock);
  2298. fs_info->balance_ctl = NULL;
  2299. spin_unlock(&fs_info->balance_lock);
  2300. kfree(bctl);
  2301. }
  2302. /*
  2303. * Balance filters. Return 1 if chunk should be filtered out
  2304. * (should not be balanced).
  2305. */
  2306. static int chunk_profiles_filter(u64 chunk_type,
  2307. struct btrfs_balance_args *bargs)
  2308. {
  2309. chunk_type = chunk_to_extended(chunk_type) &
  2310. BTRFS_EXTENDED_PROFILE_MASK;
  2311. if (bargs->profiles & chunk_type)
  2312. return 0;
  2313. return 1;
  2314. }
  2315. static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
  2316. struct btrfs_balance_args *bargs)
  2317. {
  2318. struct btrfs_block_group_cache *cache;
  2319. u64 chunk_used, user_thresh;
  2320. int ret = 1;
  2321. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  2322. chunk_used = btrfs_block_group_used(&cache->item);
  2323. if (bargs->usage == 0)
  2324. user_thresh = 1;
  2325. else if (bargs->usage > 100)
  2326. user_thresh = cache->key.offset;
  2327. else
  2328. user_thresh = div_factor_fine(cache->key.offset,
  2329. bargs->usage);
  2330. if (chunk_used < user_thresh)
  2331. ret = 0;
  2332. btrfs_put_block_group(cache);
  2333. return ret;
  2334. }
  2335. static int chunk_devid_filter(struct extent_buffer *leaf,
  2336. struct btrfs_chunk *chunk,
  2337. struct btrfs_balance_args *bargs)
  2338. {
  2339. struct btrfs_stripe *stripe;
  2340. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2341. int i;
  2342. for (i = 0; i < num_stripes; i++) {
  2343. stripe = btrfs_stripe_nr(chunk, i);
  2344. if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
  2345. return 0;
  2346. }
  2347. return 1;
  2348. }
  2349. /* [pstart, pend) */
  2350. static int chunk_drange_filter(struct extent_buffer *leaf,
  2351. struct btrfs_chunk *chunk,
  2352. u64 chunk_offset,
  2353. struct btrfs_balance_args *bargs)
  2354. {
  2355. struct btrfs_stripe *stripe;
  2356. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2357. u64 stripe_offset;
  2358. u64 stripe_length;
  2359. int factor;
  2360. int i;
  2361. if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
  2362. return 0;
  2363. if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
  2364. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
  2365. factor = num_stripes / 2;
  2366. } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
  2367. factor = num_stripes - 1;
  2368. } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
  2369. factor = num_stripes - 2;
  2370. } else {
  2371. factor = num_stripes;
  2372. }
  2373. for (i = 0; i < num_stripes; i++) {
  2374. stripe = btrfs_stripe_nr(chunk, i);
  2375. if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
  2376. continue;
  2377. stripe_offset = btrfs_stripe_offset(leaf, stripe);
  2378. stripe_length = btrfs_chunk_length(leaf, chunk);
  2379. do_div(stripe_length, factor);
  2380. if (stripe_offset < bargs->pend &&
  2381. stripe_offset + stripe_length > bargs->pstart)
  2382. return 0;
  2383. }
  2384. return 1;
  2385. }
  2386. /* [vstart, vend) */
  2387. static int chunk_vrange_filter(struct extent_buffer *leaf,
  2388. struct btrfs_chunk *chunk,
  2389. u64 chunk_offset,
  2390. struct btrfs_balance_args *bargs)
  2391. {
  2392. if (chunk_offset < bargs->vend &&
  2393. chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
  2394. /* at least part of the chunk is inside this vrange */
  2395. return 0;
  2396. return 1;
  2397. }
  2398. static int chunk_soft_convert_filter(u64 chunk_type,
  2399. struct btrfs_balance_args *bargs)
  2400. {
  2401. if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
  2402. return 0;
  2403. chunk_type = chunk_to_extended(chunk_type) &
  2404. BTRFS_EXTENDED_PROFILE_MASK;
  2405. if (bargs->target == chunk_type)
  2406. return 1;
  2407. return 0;
  2408. }
  2409. static int should_balance_chunk(struct btrfs_root *root,
  2410. struct extent_buffer *leaf,
  2411. struct btrfs_chunk *chunk, u64 chunk_offset)
  2412. {
  2413. struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
  2414. struct btrfs_balance_args *bargs = NULL;
  2415. u64 chunk_type = btrfs_chunk_type(leaf, chunk);
  2416. /* type filter */
  2417. if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
  2418. (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
  2419. return 0;
  2420. }
  2421. if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
  2422. bargs = &bctl->data;
  2423. else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
  2424. bargs = &bctl->sys;
  2425. else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
  2426. bargs = &bctl->meta;
  2427. /* profiles filter */
  2428. if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
  2429. chunk_profiles_filter(chunk_type, bargs)) {
  2430. return 0;
  2431. }
  2432. /* usage filter */
  2433. if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2434. chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
  2435. return 0;
  2436. }
  2437. /* devid filter */
  2438. if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
  2439. chunk_devid_filter(leaf, chunk, bargs)) {
  2440. return 0;
  2441. }
  2442. /* drange filter, makes sense only with devid filter */
  2443. if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
  2444. chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
  2445. return 0;
  2446. }
  2447. /* vrange filter */
  2448. if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
  2449. chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
  2450. return 0;
  2451. }
  2452. /* soft profile changing mode */
  2453. if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
  2454. chunk_soft_convert_filter(chunk_type, bargs)) {
  2455. return 0;
  2456. }
  2457. return 1;
  2458. }
  2459. static int __btrfs_balance(struct btrfs_fs_info *fs_info)
  2460. {
  2461. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  2462. struct btrfs_root *chunk_root = fs_info->chunk_root;
  2463. struct btrfs_root *dev_root = fs_info->dev_root;
  2464. struct list_head *devices;
  2465. struct btrfs_device *device;
  2466. u64 old_size;
  2467. u64 size_to_free;
  2468. struct btrfs_chunk *chunk;
  2469. struct btrfs_path *path;
  2470. struct btrfs_key key;
  2471. struct btrfs_key found_key;
  2472. struct btrfs_trans_handle *trans;
  2473. struct extent_buffer *leaf;
  2474. int slot;
  2475. int ret;
  2476. int enospc_errors = 0;
  2477. bool counting = true;
  2478. /* step one make some room on all the devices */
  2479. devices = &fs_info->fs_devices->devices;
  2480. list_for_each_entry(device, devices, dev_list) {
  2481. old_size = device->total_bytes;
  2482. size_to_free = div_factor(old_size, 1);
  2483. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  2484. if (!device->writeable ||
  2485. device->total_bytes - device->bytes_used > size_to_free ||
  2486. device->is_tgtdev_for_dev_replace)
  2487. continue;
  2488. ret = btrfs_shrink_device(device, old_size - size_to_free);
  2489. if (ret == -ENOSPC)
  2490. break;
  2491. BUG_ON(ret);
  2492. trans = btrfs_start_transaction(dev_root, 0);
  2493. BUG_ON(IS_ERR(trans));
  2494. ret = btrfs_grow_device(trans, device, old_size);
  2495. BUG_ON(ret);
  2496. btrfs_end_transaction(trans, dev_root);
  2497. }
  2498. /* step two, relocate all the chunks */
  2499. path = btrfs_alloc_path();
  2500. if (!path) {
  2501. ret = -ENOMEM;
  2502. goto error;
  2503. }
  2504. /* zero out stat counters */
  2505. spin_lock(&fs_info->balance_lock);
  2506. memset(&bctl->stat, 0, sizeof(bctl->stat));
  2507. spin_unlock(&fs_info->balance_lock);
  2508. again:
  2509. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2510. key.offset = (u64)-1;
  2511. key.type = BTRFS_CHUNK_ITEM_KEY;
  2512. while (1) {
  2513. if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
  2514. atomic_read(&fs_info->balance_cancel_req)) {
  2515. ret = -ECANCELED;
  2516. goto error;
  2517. }
  2518. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2519. if (ret < 0)
  2520. goto error;
  2521. /*
  2522. * this shouldn't happen, it means the last relocate
  2523. * failed
  2524. */
  2525. if (ret == 0)
  2526. BUG(); /* FIXME break ? */
  2527. ret = btrfs_previous_item(chunk_root, path, 0,
  2528. BTRFS_CHUNK_ITEM_KEY);
  2529. if (ret) {
  2530. ret = 0;
  2531. break;
  2532. }
  2533. leaf = path->nodes[0];
  2534. slot = path->slots[0];
  2535. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2536. if (found_key.objectid != key.objectid)
  2537. break;
  2538. /* chunk zero is special */
  2539. if (found_key.offset == 0)
  2540. break;
  2541. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2542. if (!counting) {
  2543. spin_lock(&fs_info->balance_lock);
  2544. bctl->stat.considered++;
  2545. spin_unlock(&fs_info->balance_lock);
  2546. }
  2547. ret = should_balance_chunk(chunk_root, leaf, chunk,
  2548. found_key.offset);
  2549. btrfs_release_path(path);
  2550. if (!ret)
  2551. goto loop;
  2552. if (counting) {
  2553. spin_lock(&fs_info->balance_lock);
  2554. bctl->stat.expected++;
  2555. spin_unlock(&fs_info->balance_lock);
  2556. goto loop;
  2557. }
  2558. ret = btrfs_relocate_chunk(chunk_root,
  2559. chunk_root->root_key.objectid,
  2560. found_key.objectid,
  2561. found_key.offset);
  2562. if (ret && ret != -ENOSPC)
  2563. goto error;
  2564. if (ret == -ENOSPC) {
  2565. enospc_errors++;
  2566. } else {
  2567. spin_lock(&fs_info->balance_lock);
  2568. bctl->stat.completed++;
  2569. spin_unlock(&fs_info->balance_lock);
  2570. }
  2571. loop:
  2572. key.offset = found_key.offset - 1;
  2573. }
  2574. if (counting) {
  2575. btrfs_release_path(path);
  2576. counting = false;
  2577. goto again;
  2578. }
  2579. error:
  2580. btrfs_free_path(path);
  2581. if (enospc_errors) {
  2582. printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
  2583. enospc_errors);
  2584. if (!ret)
  2585. ret = -ENOSPC;
  2586. }
  2587. return ret;
  2588. }
  2589. /**
  2590. * alloc_profile_is_valid - see if a given profile is valid and reduced
  2591. * @flags: profile to validate
  2592. * @extended: if true @flags is treated as an extended profile
  2593. */
  2594. static int alloc_profile_is_valid(u64 flags, int extended)
  2595. {
  2596. u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
  2597. BTRFS_BLOCK_GROUP_PROFILE_MASK);
  2598. flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
  2599. /* 1) check that all other bits are zeroed */
  2600. if (flags & ~mask)
  2601. return 0;
  2602. /* 2) see if profile is reduced */
  2603. if (flags == 0)
  2604. return !extended; /* "0" is valid for usual profiles */
  2605. /* true if exactly one bit set */
  2606. return (flags & (flags - 1)) == 0;
  2607. }
  2608. static inline int balance_need_close(struct btrfs_fs_info *fs_info)
  2609. {
  2610. /* cancel requested || normal exit path */
  2611. return atomic_read(&fs_info->balance_cancel_req) ||
  2612. (atomic_read(&fs_info->balance_pause_req) == 0 &&
  2613. atomic_read(&fs_info->balance_cancel_req) == 0);
  2614. }
  2615. static void __cancel_balance(struct btrfs_fs_info *fs_info)
  2616. {
  2617. int ret;
  2618. unset_balance_control(fs_info);
  2619. ret = del_balance_item(fs_info->tree_root);
  2620. if (ret)
  2621. btrfs_std_error(fs_info, ret);
  2622. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  2623. }
  2624. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
  2625. struct btrfs_ioctl_balance_args *bargs);
  2626. /*
  2627. * Should be called with both balance and volume mutexes held
  2628. */
  2629. int btrfs_balance(struct btrfs_balance_control *bctl,
  2630. struct btrfs_ioctl_balance_args *bargs)
  2631. {
  2632. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2633. u64 allowed;
  2634. int mixed = 0;
  2635. int ret;
  2636. u64 num_devices;
  2637. unsigned seq;
  2638. if (btrfs_fs_closing(fs_info) ||
  2639. atomic_read(&fs_info->balance_pause_req) ||
  2640. atomic_read(&fs_info->balance_cancel_req)) {
  2641. ret = -EINVAL;
  2642. goto out;
  2643. }
  2644. allowed = btrfs_super_incompat_flags(fs_info->super_copy);
  2645. if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
  2646. mixed = 1;
  2647. /*
  2648. * In case of mixed groups both data and meta should be picked,
  2649. * and identical options should be given for both of them.
  2650. */
  2651. allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
  2652. if (mixed && (bctl->flags & allowed)) {
  2653. if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
  2654. !(bctl->flags & BTRFS_BALANCE_METADATA) ||
  2655. memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
  2656. printk(KERN_ERR "btrfs: with mixed groups data and "
  2657. "metadata balance options must be the same\n");
  2658. ret = -EINVAL;
  2659. goto out;
  2660. }
  2661. }
  2662. num_devices = fs_info->fs_devices->num_devices;
  2663. btrfs_dev_replace_lock(&fs_info->dev_replace);
  2664. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
  2665. BUG_ON(num_devices < 1);
  2666. num_devices--;
  2667. }
  2668. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  2669. allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2670. if (num_devices == 1)
  2671. allowed |= BTRFS_BLOCK_GROUP_DUP;
  2672. else if (num_devices < 4)
  2673. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
  2674. else
  2675. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  2676. BTRFS_BLOCK_GROUP_RAID10 |
  2677. BTRFS_BLOCK_GROUP_RAID5 |
  2678. BTRFS_BLOCK_GROUP_RAID6);
  2679. if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2680. (!alloc_profile_is_valid(bctl->data.target, 1) ||
  2681. (bctl->data.target & ~allowed))) {
  2682. printk(KERN_ERR "btrfs: unable to start balance with target "
  2683. "data profile %llu\n",
  2684. (unsigned long long)bctl->data.target);
  2685. ret = -EINVAL;
  2686. goto out;
  2687. }
  2688. if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2689. (!alloc_profile_is_valid(bctl->meta.target, 1) ||
  2690. (bctl->meta.target & ~allowed))) {
  2691. printk(KERN_ERR "btrfs: unable to start balance with target "
  2692. "metadata profile %llu\n",
  2693. (unsigned long long)bctl->meta.target);
  2694. ret = -EINVAL;
  2695. goto out;
  2696. }
  2697. if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2698. (!alloc_profile_is_valid(bctl->sys.target, 1) ||
  2699. (bctl->sys.target & ~allowed))) {
  2700. printk(KERN_ERR "btrfs: unable to start balance with target "
  2701. "system profile %llu\n",
  2702. (unsigned long long)bctl->sys.target);
  2703. ret = -EINVAL;
  2704. goto out;
  2705. }
  2706. /* allow dup'ed data chunks only in mixed mode */
  2707. if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2708. (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
  2709. printk(KERN_ERR "btrfs: dup for data is not allowed\n");
  2710. ret = -EINVAL;
  2711. goto out;
  2712. }
  2713. /* allow to reduce meta or sys integrity only if force set */
  2714. allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2715. BTRFS_BLOCK_GROUP_RAID10 |
  2716. BTRFS_BLOCK_GROUP_RAID5 |
  2717. BTRFS_BLOCK_GROUP_RAID6;
  2718. do {
  2719. seq = read_seqbegin(&fs_info->profiles_lock);
  2720. if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2721. (fs_info->avail_system_alloc_bits & allowed) &&
  2722. !(bctl->sys.target & allowed)) ||
  2723. ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2724. (fs_info->avail_metadata_alloc_bits & allowed) &&
  2725. !(bctl->meta.target & allowed))) {
  2726. if (bctl->flags & BTRFS_BALANCE_FORCE) {
  2727. printk(KERN_INFO "btrfs: force reducing metadata "
  2728. "integrity\n");
  2729. } else {
  2730. printk(KERN_ERR "btrfs: balance will reduce metadata "
  2731. "integrity, use force if you want this\n");
  2732. ret = -EINVAL;
  2733. goto out;
  2734. }
  2735. }
  2736. } while (read_seqretry(&fs_info->profiles_lock, seq));
  2737. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2738. int num_tolerated_disk_barrier_failures;
  2739. u64 target = bctl->sys.target;
  2740. num_tolerated_disk_barrier_failures =
  2741. btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
  2742. if (num_tolerated_disk_barrier_failures > 0 &&
  2743. (target &
  2744. (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
  2745. BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
  2746. num_tolerated_disk_barrier_failures = 0;
  2747. else if (num_tolerated_disk_barrier_failures > 1 &&
  2748. (target &
  2749. (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
  2750. num_tolerated_disk_barrier_failures = 1;
  2751. fs_info->num_tolerated_disk_barrier_failures =
  2752. num_tolerated_disk_barrier_failures;
  2753. }
  2754. ret = insert_balance_item(fs_info->tree_root, bctl);
  2755. if (ret && ret != -EEXIST)
  2756. goto out;
  2757. if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
  2758. BUG_ON(ret == -EEXIST);
  2759. set_balance_control(bctl);
  2760. } else {
  2761. BUG_ON(ret != -EEXIST);
  2762. spin_lock(&fs_info->balance_lock);
  2763. update_balance_args(bctl);
  2764. spin_unlock(&fs_info->balance_lock);
  2765. }
  2766. atomic_inc(&fs_info->balance_running);
  2767. mutex_unlock(&fs_info->balance_mutex);
  2768. ret = __btrfs_balance(fs_info);
  2769. mutex_lock(&fs_info->balance_mutex);
  2770. atomic_dec(&fs_info->balance_running);
  2771. if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
  2772. fs_info->num_tolerated_disk_barrier_failures =
  2773. btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
  2774. }
  2775. if (bargs) {
  2776. memset(bargs, 0, sizeof(*bargs));
  2777. update_ioctl_balance_args(fs_info, 0, bargs);
  2778. }
  2779. if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
  2780. balance_need_close(fs_info)) {
  2781. __cancel_balance(fs_info);
  2782. }
  2783. wake_up(&fs_info->balance_wait_q);
  2784. return ret;
  2785. out:
  2786. if (bctl->flags & BTRFS_BALANCE_RESUME)
  2787. __cancel_balance(fs_info);
  2788. else {
  2789. kfree(bctl);
  2790. atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
  2791. }
  2792. return ret;
  2793. }
  2794. static int balance_kthread(void *data)
  2795. {
  2796. struct btrfs_fs_info *fs_info = data;
  2797. int ret = 0;
  2798. mutex_lock(&fs_info->volume_mutex);
  2799. mutex_lock(&fs_info->balance_mutex);
  2800. if (fs_info->balance_ctl) {
  2801. printk(KERN_INFO "btrfs: continuing balance\n");
  2802. ret = btrfs_balance(fs_info->balance_ctl, NULL);
  2803. }
  2804. mutex_unlock(&fs_info->balance_mutex);
  2805. mutex_unlock(&fs_info->volume_mutex);
  2806. return ret;
  2807. }
  2808. int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
  2809. {
  2810. struct task_struct *tsk;
  2811. spin_lock(&fs_info->balance_lock);
  2812. if (!fs_info->balance_ctl) {
  2813. spin_unlock(&fs_info->balance_lock);
  2814. return 0;
  2815. }
  2816. spin_unlock(&fs_info->balance_lock);
  2817. if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
  2818. printk(KERN_INFO "btrfs: force skipping balance\n");
  2819. return 0;
  2820. }
  2821. tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
  2822. if (IS_ERR(tsk))
  2823. return PTR_ERR(tsk);
  2824. return 0;
  2825. }
  2826. int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
  2827. {
  2828. struct btrfs_balance_control *bctl;
  2829. struct btrfs_balance_item *item;
  2830. struct btrfs_disk_balance_args disk_bargs;
  2831. struct btrfs_path *path;
  2832. struct extent_buffer *leaf;
  2833. struct btrfs_key key;
  2834. int ret;
  2835. path = btrfs_alloc_path();
  2836. if (!path)
  2837. return -ENOMEM;
  2838. key.objectid = BTRFS_BALANCE_OBJECTID;
  2839. key.type = BTRFS_BALANCE_ITEM_KEY;
  2840. key.offset = 0;
  2841. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  2842. if (ret < 0)
  2843. goto out;
  2844. if (ret > 0) { /* ret = -ENOENT; */
  2845. ret = 0;
  2846. goto out;
  2847. }
  2848. bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
  2849. if (!bctl) {
  2850. ret = -ENOMEM;
  2851. goto out;
  2852. }
  2853. leaf = path->nodes[0];
  2854. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  2855. bctl->fs_info = fs_info;
  2856. bctl->flags = btrfs_balance_flags(leaf, item);
  2857. bctl->flags |= BTRFS_BALANCE_RESUME;
  2858. btrfs_balance_data(leaf, item, &disk_bargs);
  2859. btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
  2860. btrfs_balance_meta(leaf, item, &disk_bargs);
  2861. btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
  2862. btrfs_balance_sys(leaf, item, &disk_bargs);
  2863. btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
  2864. WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
  2865. mutex_lock(&fs_info->volume_mutex);
  2866. mutex_lock(&fs_info->balance_mutex);
  2867. set_balance_control(bctl);
  2868. mutex_unlock(&fs_info->balance_mutex);
  2869. mutex_unlock(&fs_info->volume_mutex);
  2870. out:
  2871. btrfs_free_path(path);
  2872. return ret;
  2873. }
  2874. int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
  2875. {
  2876. int ret = 0;
  2877. mutex_lock(&fs_info->balance_mutex);
  2878. if (!fs_info->balance_ctl) {
  2879. mutex_unlock(&fs_info->balance_mutex);
  2880. return -ENOTCONN;
  2881. }
  2882. if (atomic_read(&fs_info->balance_running)) {
  2883. atomic_inc(&fs_info->balance_pause_req);
  2884. mutex_unlock(&fs_info->balance_mutex);
  2885. wait_event(fs_info->balance_wait_q,
  2886. atomic_read(&fs_info->balance_running) == 0);
  2887. mutex_lock(&fs_info->balance_mutex);
  2888. /* we are good with balance_ctl ripped off from under us */
  2889. BUG_ON(atomic_read(&fs_info->balance_running));
  2890. atomic_dec(&fs_info->balance_pause_req);
  2891. } else {
  2892. ret = -ENOTCONN;
  2893. }
  2894. mutex_unlock(&fs_info->balance_mutex);
  2895. return ret;
  2896. }
  2897. int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
  2898. {
  2899. mutex_lock(&fs_info->balance_mutex);
  2900. if (!fs_info->balance_ctl) {
  2901. mutex_unlock(&fs_info->balance_mutex);
  2902. return -ENOTCONN;
  2903. }
  2904. atomic_inc(&fs_info->balance_cancel_req);
  2905. /*
  2906. * if we are running just wait and return, balance item is
  2907. * deleted in btrfs_balance in this case
  2908. */
  2909. if (atomic_read(&fs_info->balance_running)) {
  2910. mutex_unlock(&fs_info->balance_mutex);
  2911. wait_event(fs_info->balance_wait_q,
  2912. atomic_read(&fs_info->balance_running) == 0);
  2913. mutex_lock(&fs_info->balance_mutex);
  2914. } else {
  2915. /* __cancel_balance needs volume_mutex */
  2916. mutex_unlock(&fs_info->balance_mutex);
  2917. mutex_lock(&fs_info->volume_mutex);
  2918. mutex_lock(&fs_info->balance_mutex);
  2919. if (fs_info->balance_ctl)
  2920. __cancel_balance(fs_info);
  2921. mutex_unlock(&fs_info->volume_mutex);
  2922. }
  2923. BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
  2924. atomic_dec(&fs_info->balance_cancel_req);
  2925. mutex_unlock(&fs_info->balance_mutex);
  2926. return 0;
  2927. }
  2928. /*
  2929. * shrinking a device means finding all of the device extents past
  2930. * the new size, and then following the back refs to the chunks.
  2931. * The chunk relocation code actually frees the device extent
  2932. */
  2933. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  2934. {
  2935. struct btrfs_trans_handle *trans;
  2936. struct btrfs_root *root = device->dev_root;
  2937. struct btrfs_dev_extent *dev_extent = NULL;
  2938. struct btrfs_path *path;
  2939. u64 length;
  2940. u64 chunk_tree;
  2941. u64 chunk_objectid;
  2942. u64 chunk_offset;
  2943. int ret;
  2944. int slot;
  2945. int failed = 0;
  2946. bool retried = false;
  2947. struct extent_buffer *l;
  2948. struct btrfs_key key;
  2949. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2950. u64 old_total = btrfs_super_total_bytes(super_copy);
  2951. u64 old_size = device->total_bytes;
  2952. u64 diff = device->total_bytes - new_size;
  2953. if (device->is_tgtdev_for_dev_replace)
  2954. return -EINVAL;
  2955. path = btrfs_alloc_path();
  2956. if (!path)
  2957. return -ENOMEM;
  2958. path->reada = 2;
  2959. lock_chunks(root);
  2960. device->total_bytes = new_size;
  2961. if (device->writeable) {
  2962. device->fs_devices->total_rw_bytes -= diff;
  2963. spin_lock(&root->fs_info->free_chunk_lock);
  2964. root->fs_info->free_chunk_space -= diff;
  2965. spin_unlock(&root->fs_info->free_chunk_lock);
  2966. }
  2967. unlock_chunks(root);
  2968. again:
  2969. key.objectid = device->devid;
  2970. key.offset = (u64)-1;
  2971. key.type = BTRFS_DEV_EXTENT_KEY;
  2972. do {
  2973. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2974. if (ret < 0)
  2975. goto done;
  2976. ret = btrfs_previous_item(root, path, 0, key.type);
  2977. if (ret < 0)
  2978. goto done;
  2979. if (ret) {
  2980. ret = 0;
  2981. btrfs_release_path(path);
  2982. break;
  2983. }
  2984. l = path->nodes[0];
  2985. slot = path->slots[0];
  2986. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  2987. if (key.objectid != device->devid) {
  2988. btrfs_release_path(path);
  2989. break;
  2990. }
  2991. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  2992. length = btrfs_dev_extent_length(l, dev_extent);
  2993. if (key.offset + length <= new_size) {
  2994. btrfs_release_path(path);
  2995. break;
  2996. }
  2997. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  2998. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  2999. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  3000. btrfs_release_path(path);
  3001. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  3002. chunk_offset);
  3003. if (ret && ret != -ENOSPC)
  3004. goto done;
  3005. if (ret == -ENOSPC)
  3006. failed++;
  3007. } while (key.offset-- > 0);
  3008. if (failed && !retried) {
  3009. failed = 0;
  3010. retried = true;
  3011. goto again;
  3012. } else if (failed && retried) {
  3013. ret = -ENOSPC;
  3014. lock_chunks(root);
  3015. device->total_bytes = old_size;
  3016. if (device->writeable)
  3017. device->fs_devices->total_rw_bytes += diff;
  3018. spin_lock(&root->fs_info->free_chunk_lock);
  3019. root->fs_info->free_chunk_space += diff;
  3020. spin_unlock(&root->fs_info->free_chunk_lock);
  3021. unlock_chunks(root);
  3022. goto done;
  3023. }
  3024. /* Shrinking succeeded, else we would be at "done". */
  3025. trans = btrfs_start_transaction(root, 0);
  3026. if (IS_ERR(trans)) {
  3027. ret = PTR_ERR(trans);
  3028. goto done;
  3029. }
  3030. lock_chunks(root);
  3031. device->disk_total_bytes = new_size;
  3032. /* Now btrfs_update_device() will change the on-disk size. */
  3033. ret = btrfs_update_device(trans, device);
  3034. if (ret) {
  3035. unlock_chunks(root);
  3036. btrfs_end_transaction(trans, root);
  3037. goto done;
  3038. }
  3039. WARN_ON(diff > old_total);
  3040. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  3041. unlock_chunks(root);
  3042. btrfs_end_transaction(trans, root);
  3043. done:
  3044. btrfs_free_path(path);
  3045. return ret;
  3046. }
  3047. static int btrfs_add_system_chunk(struct btrfs_root *root,
  3048. struct btrfs_key *key,
  3049. struct btrfs_chunk *chunk, int item_size)
  3050. {
  3051. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  3052. struct btrfs_disk_key disk_key;
  3053. u32 array_size;
  3054. u8 *ptr;
  3055. array_size = btrfs_super_sys_array_size(super_copy);
  3056. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  3057. return -EFBIG;
  3058. ptr = super_copy->sys_chunk_array + array_size;
  3059. btrfs_cpu_key_to_disk(&disk_key, key);
  3060. memcpy(ptr, &disk_key, sizeof(disk_key));
  3061. ptr += sizeof(disk_key);
  3062. memcpy(ptr, chunk, item_size);
  3063. item_size += sizeof(disk_key);
  3064. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  3065. return 0;
  3066. }
  3067. /*
  3068. * sort the devices in descending order by max_avail, total_avail
  3069. */
  3070. static int btrfs_cmp_device_info(const void *a, const void *b)
  3071. {
  3072. const struct btrfs_device_info *di_a = a;
  3073. const struct btrfs_device_info *di_b = b;
  3074. if (di_a->max_avail > di_b->max_avail)
  3075. return -1;
  3076. if (di_a->max_avail < di_b->max_avail)
  3077. return 1;
  3078. if (di_a->total_avail > di_b->total_avail)
  3079. return -1;
  3080. if (di_a->total_avail < di_b->total_avail)
  3081. return 1;
  3082. return 0;
  3083. }
  3084. struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
  3085. [BTRFS_RAID_RAID10] = {
  3086. .sub_stripes = 2,
  3087. .dev_stripes = 1,
  3088. .devs_max = 0, /* 0 == as many as possible */
  3089. .devs_min = 4,
  3090. .devs_increment = 2,
  3091. .ncopies = 2,
  3092. },
  3093. [BTRFS_RAID_RAID1] = {
  3094. .sub_stripes = 1,
  3095. .dev_stripes = 1,
  3096. .devs_max = 2,
  3097. .devs_min = 2,
  3098. .devs_increment = 2,
  3099. .ncopies = 2,
  3100. },
  3101. [BTRFS_RAID_DUP] = {
  3102. .sub_stripes = 1,
  3103. .dev_stripes = 2,
  3104. .devs_max = 1,
  3105. .devs_min = 1,
  3106. .devs_increment = 1,
  3107. .ncopies = 2,
  3108. },
  3109. [BTRFS_RAID_RAID0] = {
  3110. .sub_stripes = 1,
  3111. .dev_stripes = 1,
  3112. .devs_max = 0,
  3113. .devs_min = 2,
  3114. .devs_increment = 1,
  3115. .ncopies = 1,
  3116. },
  3117. [BTRFS_RAID_SINGLE] = {
  3118. .sub_stripes = 1,
  3119. .dev_stripes = 1,
  3120. .devs_max = 1,
  3121. .devs_min = 1,
  3122. .devs_increment = 1,
  3123. .ncopies = 1,
  3124. },
  3125. [BTRFS_RAID_RAID5] = {
  3126. .sub_stripes = 1,
  3127. .dev_stripes = 1,
  3128. .devs_max = 0,
  3129. .devs_min = 2,
  3130. .devs_increment = 1,
  3131. .ncopies = 2,
  3132. },
  3133. [BTRFS_RAID_RAID6] = {
  3134. .sub_stripes = 1,
  3135. .dev_stripes = 1,
  3136. .devs_max = 0,
  3137. .devs_min = 3,
  3138. .devs_increment = 1,
  3139. .ncopies = 3,
  3140. },
  3141. };
  3142. static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
  3143. {
  3144. /* TODO allow them to set a preferred stripe size */
  3145. return 64 * 1024;
  3146. }
  3147. static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
  3148. {
  3149. if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
  3150. return;
  3151. btrfs_set_fs_incompat(info, RAID56);
  3152. }
  3153. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  3154. struct btrfs_root *extent_root,
  3155. struct map_lookup **map_ret,
  3156. u64 *num_bytes_out, u64 *stripe_size_out,
  3157. u64 start, u64 type)
  3158. {
  3159. struct btrfs_fs_info *info = extent_root->fs_info;
  3160. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  3161. struct list_head *cur;
  3162. struct map_lookup *map = NULL;
  3163. struct extent_map_tree *em_tree;
  3164. struct extent_map *em;
  3165. struct btrfs_device_info *devices_info = NULL;
  3166. u64 total_avail;
  3167. int num_stripes; /* total number of stripes to allocate */
  3168. int data_stripes; /* number of stripes that count for
  3169. block group size */
  3170. int sub_stripes; /* sub_stripes info for map */
  3171. int dev_stripes; /* stripes per dev */
  3172. int devs_max; /* max devs to use */
  3173. int devs_min; /* min devs needed */
  3174. int devs_increment; /* ndevs has to be a multiple of this */
  3175. int ncopies; /* how many copies to data has */
  3176. int ret;
  3177. u64 max_stripe_size;
  3178. u64 max_chunk_size;
  3179. u64 stripe_size;
  3180. u64 num_bytes;
  3181. u64 raid_stripe_len = BTRFS_STRIPE_LEN;
  3182. int ndevs;
  3183. int i;
  3184. int j;
  3185. int index;
  3186. BUG_ON(!alloc_profile_is_valid(type, 0));
  3187. if (list_empty(&fs_devices->alloc_list))
  3188. return -ENOSPC;
  3189. index = __get_raid_index(type);
  3190. sub_stripes = btrfs_raid_array[index].sub_stripes;
  3191. dev_stripes = btrfs_raid_array[index].dev_stripes;
  3192. devs_max = btrfs_raid_array[index].devs_max;
  3193. devs_min = btrfs_raid_array[index].devs_min;
  3194. devs_increment = btrfs_raid_array[index].devs_increment;
  3195. ncopies = btrfs_raid_array[index].ncopies;
  3196. if (type & BTRFS_BLOCK_GROUP_DATA) {
  3197. max_stripe_size = 1024 * 1024 * 1024;
  3198. max_chunk_size = 10 * max_stripe_size;
  3199. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  3200. /* for larger filesystems, use larger metadata chunks */
  3201. if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
  3202. max_stripe_size = 1024 * 1024 * 1024;
  3203. else
  3204. max_stripe_size = 256 * 1024 * 1024;
  3205. max_chunk_size = max_stripe_size;
  3206. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  3207. max_stripe_size = 32 * 1024 * 1024;
  3208. max_chunk_size = 2 * max_stripe_size;
  3209. } else {
  3210. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  3211. type);
  3212. BUG_ON(1);
  3213. }
  3214. /* we don't want a chunk larger than 10% of writeable space */
  3215. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  3216. max_chunk_size);
  3217. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  3218. GFP_NOFS);
  3219. if (!devices_info)
  3220. return -ENOMEM;
  3221. cur = fs_devices->alloc_list.next;
  3222. /*
  3223. * in the first pass through the devices list, we gather information
  3224. * about the available holes on each device.
  3225. */
  3226. ndevs = 0;
  3227. while (cur != &fs_devices->alloc_list) {
  3228. struct btrfs_device *device;
  3229. u64 max_avail;
  3230. u64 dev_offset;
  3231. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  3232. cur = cur->next;
  3233. if (!device->writeable) {
  3234. WARN(1, KERN_ERR
  3235. "btrfs: read-only device in alloc_list\n");
  3236. continue;
  3237. }
  3238. if (!device->in_fs_metadata ||
  3239. device->is_tgtdev_for_dev_replace)
  3240. continue;
  3241. if (device->total_bytes > device->bytes_used)
  3242. total_avail = device->total_bytes - device->bytes_used;
  3243. else
  3244. total_avail = 0;
  3245. /* If there is no space on this device, skip it. */
  3246. if (total_avail == 0)
  3247. continue;
  3248. ret = find_free_dev_extent(device,
  3249. max_stripe_size * dev_stripes,
  3250. &dev_offset, &max_avail);
  3251. if (ret && ret != -ENOSPC)
  3252. goto error;
  3253. if (ret == 0)
  3254. max_avail = max_stripe_size * dev_stripes;
  3255. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  3256. continue;
  3257. if (ndevs == fs_devices->rw_devices) {
  3258. WARN(1, "%s: found more than %llu devices\n",
  3259. __func__, fs_devices->rw_devices);
  3260. break;
  3261. }
  3262. devices_info[ndevs].dev_offset = dev_offset;
  3263. devices_info[ndevs].max_avail = max_avail;
  3264. devices_info[ndevs].total_avail = total_avail;
  3265. devices_info[ndevs].dev = device;
  3266. ++ndevs;
  3267. }
  3268. /*
  3269. * now sort the devices by hole size / available space
  3270. */
  3271. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  3272. btrfs_cmp_device_info, NULL);
  3273. /* round down to number of usable stripes */
  3274. ndevs -= ndevs % devs_increment;
  3275. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  3276. ret = -ENOSPC;
  3277. goto error;
  3278. }
  3279. if (devs_max && ndevs > devs_max)
  3280. ndevs = devs_max;
  3281. /*
  3282. * the primary goal is to maximize the number of stripes, so use as many
  3283. * devices as possible, even if the stripes are not maximum sized.
  3284. */
  3285. stripe_size = devices_info[ndevs-1].max_avail;
  3286. num_stripes = ndevs * dev_stripes;
  3287. /*
  3288. * this will have to be fixed for RAID1 and RAID10 over
  3289. * more drives
  3290. */
  3291. data_stripes = num_stripes / ncopies;
  3292. if (type & BTRFS_BLOCK_GROUP_RAID5) {
  3293. raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
  3294. btrfs_super_stripesize(info->super_copy));
  3295. data_stripes = num_stripes - 1;
  3296. }
  3297. if (type & BTRFS_BLOCK_GROUP_RAID6) {
  3298. raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
  3299. btrfs_super_stripesize(info->super_copy));
  3300. data_stripes = num_stripes - 2;
  3301. }
  3302. /*
  3303. * Use the number of data stripes to figure out how big this chunk
  3304. * is really going to be in terms of logical address space,
  3305. * and compare that answer with the max chunk size
  3306. */
  3307. if (stripe_size * data_stripes > max_chunk_size) {
  3308. u64 mask = (1ULL << 24) - 1;
  3309. stripe_size = max_chunk_size;
  3310. do_div(stripe_size, data_stripes);
  3311. /* bump the answer up to a 16MB boundary */
  3312. stripe_size = (stripe_size + mask) & ~mask;
  3313. /* but don't go higher than the limits we found
  3314. * while searching for free extents
  3315. */
  3316. if (stripe_size > devices_info[ndevs-1].max_avail)
  3317. stripe_size = devices_info[ndevs-1].max_avail;
  3318. }
  3319. do_div(stripe_size, dev_stripes);
  3320. /* align to BTRFS_STRIPE_LEN */
  3321. do_div(stripe_size, raid_stripe_len);
  3322. stripe_size *= raid_stripe_len;
  3323. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3324. if (!map) {
  3325. ret = -ENOMEM;
  3326. goto error;
  3327. }
  3328. map->num_stripes = num_stripes;
  3329. for (i = 0; i < ndevs; ++i) {
  3330. for (j = 0; j < dev_stripes; ++j) {
  3331. int s = i * dev_stripes + j;
  3332. map->stripes[s].dev = devices_info[i].dev;
  3333. map->stripes[s].physical = devices_info[i].dev_offset +
  3334. j * stripe_size;
  3335. }
  3336. }
  3337. map->sector_size = extent_root->sectorsize;
  3338. map->stripe_len = raid_stripe_len;
  3339. map->io_align = raid_stripe_len;
  3340. map->io_width = raid_stripe_len;
  3341. map->type = type;
  3342. map->sub_stripes = sub_stripes;
  3343. *map_ret = map;
  3344. num_bytes = stripe_size * data_stripes;
  3345. *stripe_size_out = stripe_size;
  3346. *num_bytes_out = num_bytes;
  3347. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  3348. em = alloc_extent_map();
  3349. if (!em) {
  3350. ret = -ENOMEM;
  3351. goto error;
  3352. }
  3353. em->bdev = (struct block_device *)map;
  3354. em->start = start;
  3355. em->len = num_bytes;
  3356. em->block_start = 0;
  3357. em->block_len = em->len;
  3358. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  3359. write_lock(&em_tree->lock);
  3360. ret = add_extent_mapping(em_tree, em, 0);
  3361. write_unlock(&em_tree->lock);
  3362. if (ret) {
  3363. free_extent_map(em);
  3364. goto error;
  3365. }
  3366. for (i = 0; i < map->num_stripes; ++i) {
  3367. struct btrfs_device *device;
  3368. u64 dev_offset;
  3369. device = map->stripes[i].dev;
  3370. dev_offset = map->stripes[i].physical;
  3371. ret = btrfs_alloc_dev_extent(trans, device,
  3372. info->chunk_root->root_key.objectid,
  3373. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3374. start, dev_offset, stripe_size);
  3375. if (ret)
  3376. goto error_dev_extent;
  3377. }
  3378. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  3379. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3380. start, num_bytes);
  3381. if (ret) {
  3382. i = map->num_stripes - 1;
  3383. goto error_dev_extent;
  3384. }
  3385. free_extent_map(em);
  3386. check_raid56_incompat_flag(extent_root->fs_info, type);
  3387. kfree(devices_info);
  3388. return 0;
  3389. error_dev_extent:
  3390. for (; i >= 0; i--) {
  3391. struct btrfs_device *device;
  3392. int err;
  3393. device = map->stripes[i].dev;
  3394. err = btrfs_free_dev_extent(trans, device, start);
  3395. if (err) {
  3396. btrfs_abort_transaction(trans, extent_root, err);
  3397. break;
  3398. }
  3399. }
  3400. write_lock(&em_tree->lock);
  3401. remove_extent_mapping(em_tree, em);
  3402. write_unlock(&em_tree->lock);
  3403. /* One for our allocation */
  3404. free_extent_map(em);
  3405. /* One for the tree reference */
  3406. free_extent_map(em);
  3407. error:
  3408. kfree(map);
  3409. kfree(devices_info);
  3410. return ret;
  3411. }
  3412. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  3413. struct btrfs_root *extent_root,
  3414. struct map_lookup *map, u64 chunk_offset,
  3415. u64 chunk_size, u64 stripe_size)
  3416. {
  3417. u64 dev_offset;
  3418. struct btrfs_key key;
  3419. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  3420. struct btrfs_device *device;
  3421. struct btrfs_chunk *chunk;
  3422. struct btrfs_stripe *stripe;
  3423. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  3424. int index = 0;
  3425. int ret;
  3426. chunk = kzalloc(item_size, GFP_NOFS);
  3427. if (!chunk)
  3428. return -ENOMEM;
  3429. index = 0;
  3430. while (index < map->num_stripes) {
  3431. device = map->stripes[index].dev;
  3432. device->bytes_used += stripe_size;
  3433. ret = btrfs_update_device(trans, device);
  3434. if (ret)
  3435. goto out_free;
  3436. index++;
  3437. }
  3438. spin_lock(&extent_root->fs_info->free_chunk_lock);
  3439. extent_root->fs_info->free_chunk_space -= (stripe_size *
  3440. map->num_stripes);
  3441. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  3442. index = 0;
  3443. stripe = &chunk->stripe;
  3444. while (index < map->num_stripes) {
  3445. device = map->stripes[index].dev;
  3446. dev_offset = map->stripes[index].physical;
  3447. btrfs_set_stack_stripe_devid(stripe, device->devid);
  3448. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  3449. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  3450. stripe++;
  3451. index++;
  3452. }
  3453. btrfs_set_stack_chunk_length(chunk, chunk_size);
  3454. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  3455. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  3456. btrfs_set_stack_chunk_type(chunk, map->type);
  3457. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  3458. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  3459. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  3460. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  3461. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  3462. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  3463. key.type = BTRFS_CHUNK_ITEM_KEY;
  3464. key.offset = chunk_offset;
  3465. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  3466. if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  3467. /*
  3468. * TODO: Cleanup of inserted chunk root in case of
  3469. * failure.
  3470. */
  3471. ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
  3472. item_size);
  3473. }
  3474. out_free:
  3475. kfree(chunk);
  3476. return ret;
  3477. }
  3478. /*
  3479. * Chunk allocation falls into two parts. The first part does works
  3480. * that make the new allocated chunk useable, but not do any operation
  3481. * that modifies the chunk tree. The second part does the works that
  3482. * require modifying the chunk tree. This division is important for the
  3483. * bootstrap process of adding storage to a seed btrfs.
  3484. */
  3485. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  3486. struct btrfs_root *extent_root, u64 type)
  3487. {
  3488. u64 chunk_offset;
  3489. u64 chunk_size;
  3490. u64 stripe_size;
  3491. struct map_lookup *map;
  3492. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  3493. int ret;
  3494. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  3495. &chunk_offset);
  3496. if (ret)
  3497. return ret;
  3498. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  3499. &stripe_size, chunk_offset, type);
  3500. if (ret)
  3501. return ret;
  3502. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  3503. chunk_size, stripe_size);
  3504. if (ret)
  3505. return ret;
  3506. return 0;
  3507. }
  3508. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  3509. struct btrfs_root *root,
  3510. struct btrfs_device *device)
  3511. {
  3512. u64 chunk_offset;
  3513. u64 sys_chunk_offset;
  3514. u64 chunk_size;
  3515. u64 sys_chunk_size;
  3516. u64 stripe_size;
  3517. u64 sys_stripe_size;
  3518. u64 alloc_profile;
  3519. struct map_lookup *map;
  3520. struct map_lookup *sys_map;
  3521. struct btrfs_fs_info *fs_info = root->fs_info;
  3522. struct btrfs_root *extent_root = fs_info->extent_root;
  3523. int ret;
  3524. ret = find_next_chunk(fs_info->chunk_root,
  3525. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  3526. if (ret)
  3527. return ret;
  3528. alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
  3529. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  3530. &stripe_size, chunk_offset, alloc_profile);
  3531. if (ret)
  3532. return ret;
  3533. sys_chunk_offset = chunk_offset + chunk_size;
  3534. alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
  3535. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  3536. &sys_chunk_size, &sys_stripe_size,
  3537. sys_chunk_offset, alloc_profile);
  3538. if (ret) {
  3539. btrfs_abort_transaction(trans, root, ret);
  3540. goto out;
  3541. }
  3542. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  3543. if (ret) {
  3544. btrfs_abort_transaction(trans, root, ret);
  3545. goto out;
  3546. }
  3547. /*
  3548. * Modifying chunk tree needs allocating new blocks from both
  3549. * system block group and metadata block group. So we only can
  3550. * do operations require modifying the chunk tree after both
  3551. * block groups were created.
  3552. */
  3553. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  3554. chunk_size, stripe_size);
  3555. if (ret) {
  3556. btrfs_abort_transaction(trans, root, ret);
  3557. goto out;
  3558. }
  3559. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  3560. sys_chunk_offset, sys_chunk_size,
  3561. sys_stripe_size);
  3562. if (ret)
  3563. btrfs_abort_transaction(trans, root, ret);
  3564. out:
  3565. return ret;
  3566. }
  3567. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  3568. {
  3569. struct extent_map *em;
  3570. struct map_lookup *map;
  3571. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  3572. int readonly = 0;
  3573. int i;
  3574. read_lock(&map_tree->map_tree.lock);
  3575. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  3576. read_unlock(&map_tree->map_tree.lock);
  3577. if (!em)
  3578. return 1;
  3579. if (btrfs_test_opt(root, DEGRADED)) {
  3580. free_extent_map(em);
  3581. return 0;
  3582. }
  3583. map = (struct map_lookup *)em->bdev;
  3584. for (i = 0; i < map->num_stripes; i++) {
  3585. if (!map->stripes[i].dev->writeable) {
  3586. readonly = 1;
  3587. break;
  3588. }
  3589. }
  3590. free_extent_map(em);
  3591. return readonly;
  3592. }
  3593. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  3594. {
  3595. extent_map_tree_init(&tree->map_tree);
  3596. }
  3597. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  3598. {
  3599. struct extent_map *em;
  3600. while (1) {
  3601. write_lock(&tree->map_tree.lock);
  3602. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  3603. if (em)
  3604. remove_extent_mapping(&tree->map_tree, em);
  3605. write_unlock(&tree->map_tree.lock);
  3606. if (!em)
  3607. break;
  3608. kfree(em->bdev);
  3609. /* once for us */
  3610. free_extent_map(em);
  3611. /* once for the tree */
  3612. free_extent_map(em);
  3613. }
  3614. }
  3615. int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
  3616. {
  3617. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  3618. struct extent_map *em;
  3619. struct map_lookup *map;
  3620. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3621. int ret;
  3622. read_lock(&em_tree->lock);
  3623. em = lookup_extent_mapping(em_tree, logical, len);
  3624. read_unlock(&em_tree->lock);
  3625. /*
  3626. * We could return errors for these cases, but that could get ugly and
  3627. * we'd probably do the same thing which is just not do anything else
  3628. * and exit, so return 1 so the callers don't try to use other copies.
  3629. */
  3630. if (!em) {
  3631. btrfs_emerg(fs_info, "No mapping for %Lu-%Lu\n", logical,
  3632. logical+len);
  3633. return 1;
  3634. }
  3635. if (em->start > logical || em->start + em->len < logical) {
  3636. btrfs_emerg(fs_info, "Invalid mapping for %Lu-%Lu, got "
  3637. "%Lu-%Lu\n", logical, logical+len, em->start,
  3638. em->start + em->len);
  3639. return 1;
  3640. }
  3641. map = (struct map_lookup *)em->bdev;
  3642. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  3643. ret = map->num_stripes;
  3644. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  3645. ret = map->sub_stripes;
  3646. else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
  3647. ret = 2;
  3648. else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  3649. ret = 3;
  3650. else
  3651. ret = 1;
  3652. free_extent_map(em);
  3653. btrfs_dev_replace_lock(&fs_info->dev_replace);
  3654. if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
  3655. ret++;
  3656. btrfs_dev_replace_unlock(&fs_info->dev_replace);
  3657. return ret;
  3658. }
  3659. unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
  3660. struct btrfs_mapping_tree *map_tree,
  3661. u64 logical)
  3662. {
  3663. struct extent_map *em;
  3664. struct map_lookup *map;
  3665. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3666. unsigned long len = root->sectorsize;
  3667. read_lock(&em_tree->lock);
  3668. em = lookup_extent_mapping(em_tree, logical, len);
  3669. read_unlock(&em_tree->lock);
  3670. BUG_ON(!em);
  3671. BUG_ON(em->start > logical || em->start + em->len < logical);
  3672. map = (struct map_lookup *)em->bdev;
  3673. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  3674. BTRFS_BLOCK_GROUP_RAID6)) {
  3675. len = map->stripe_len * nr_data_stripes(map);
  3676. }
  3677. free_extent_map(em);
  3678. return len;
  3679. }
  3680. int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
  3681. u64 logical, u64 len, int mirror_num)
  3682. {
  3683. struct extent_map *em;
  3684. struct map_lookup *map;
  3685. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3686. int ret = 0;
  3687. read_lock(&em_tree->lock);
  3688. em = lookup_extent_mapping(em_tree, logical, len);
  3689. read_unlock(&em_tree->lock);
  3690. BUG_ON(!em);
  3691. BUG_ON(em->start > logical || em->start + em->len < logical);
  3692. map = (struct map_lookup *)em->bdev;
  3693. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  3694. BTRFS_BLOCK_GROUP_RAID6))
  3695. ret = 1;
  3696. free_extent_map(em);
  3697. return ret;
  3698. }
  3699. static int find_live_mirror(struct btrfs_fs_info *fs_info,
  3700. struct map_lookup *map, int first, int num,
  3701. int optimal, int dev_replace_is_ongoing)
  3702. {
  3703. int i;
  3704. int tolerance;
  3705. struct btrfs_device *srcdev;
  3706. if (dev_replace_is_ongoing &&
  3707. fs_info->dev_replace.cont_reading_from_srcdev_mode ==
  3708. BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
  3709. srcdev = fs_info->dev_replace.srcdev;
  3710. else
  3711. srcdev = NULL;
  3712. /*
  3713. * try to avoid the drive that is the source drive for a
  3714. * dev-replace procedure, only choose it if no other non-missing
  3715. * mirror is available
  3716. */
  3717. for (tolerance = 0; tolerance < 2; tolerance++) {
  3718. if (map->stripes[optimal].dev->bdev &&
  3719. (tolerance || map->stripes[optimal].dev != srcdev))
  3720. return optimal;
  3721. for (i = first; i < first + num; i++) {
  3722. if (map->stripes[i].dev->bdev &&
  3723. (tolerance || map->stripes[i].dev != srcdev))
  3724. return i;
  3725. }
  3726. }
  3727. /* we couldn't find one that doesn't fail. Just return something
  3728. * and the io error handling code will clean up eventually
  3729. */
  3730. return optimal;
  3731. }
  3732. static inline int parity_smaller(u64 a, u64 b)
  3733. {
  3734. return a > b;
  3735. }
  3736. /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
  3737. static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
  3738. {
  3739. struct btrfs_bio_stripe s;
  3740. int i;
  3741. u64 l;
  3742. int again = 1;
  3743. while (again) {
  3744. again = 0;
  3745. for (i = 0; i < bbio->num_stripes - 1; i++) {
  3746. if (parity_smaller(raid_map[i], raid_map[i+1])) {
  3747. s = bbio->stripes[i];
  3748. l = raid_map[i];
  3749. bbio->stripes[i] = bbio->stripes[i+1];
  3750. raid_map[i] = raid_map[i+1];
  3751. bbio->stripes[i+1] = s;
  3752. raid_map[i+1] = l;
  3753. again = 1;
  3754. }
  3755. }
  3756. }
  3757. }
  3758. static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  3759. u64 logical, u64 *length,
  3760. struct btrfs_bio **bbio_ret,
  3761. int mirror_num, u64 **raid_map_ret)
  3762. {
  3763. struct extent_map *em;
  3764. struct map_lookup *map;
  3765. struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
  3766. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3767. u64 offset;
  3768. u64 stripe_offset;
  3769. u64 stripe_end_offset;
  3770. u64 stripe_nr;
  3771. u64 stripe_nr_orig;
  3772. u64 stripe_nr_end;
  3773. u64 stripe_len;
  3774. u64 *raid_map = NULL;
  3775. int stripe_index;
  3776. int i;
  3777. int ret = 0;
  3778. int num_stripes;
  3779. int max_errors = 0;
  3780. struct btrfs_bio *bbio = NULL;
  3781. struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
  3782. int dev_replace_is_ongoing = 0;
  3783. int num_alloc_stripes;
  3784. int patch_the_first_stripe_for_dev_replace = 0;
  3785. u64 physical_to_patch_in_first_stripe = 0;
  3786. u64 raid56_full_stripe_start = (u64)-1;
  3787. read_lock(&em_tree->lock);
  3788. em = lookup_extent_mapping(em_tree, logical, *length);
  3789. read_unlock(&em_tree->lock);
  3790. if (!em) {
  3791. btrfs_crit(fs_info, "unable to find logical %llu len %llu",
  3792. (unsigned long long)logical,
  3793. (unsigned long long)*length);
  3794. return -EINVAL;
  3795. }
  3796. if (em->start > logical || em->start + em->len < logical) {
  3797. btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
  3798. "found %Lu-%Lu\n", logical, em->start,
  3799. em->start + em->len);
  3800. return -EINVAL;
  3801. }
  3802. map = (struct map_lookup *)em->bdev;
  3803. offset = logical - em->start;
  3804. if (mirror_num > map->num_stripes)
  3805. mirror_num = 0;
  3806. stripe_len = map->stripe_len;
  3807. stripe_nr = offset;
  3808. /*
  3809. * stripe_nr counts the total number of stripes we have to stride
  3810. * to get to this block
  3811. */
  3812. do_div(stripe_nr, stripe_len);
  3813. stripe_offset = stripe_nr * stripe_len;
  3814. BUG_ON(offset < stripe_offset);
  3815. /* stripe_offset is the offset of this block in its stripe*/
  3816. stripe_offset = offset - stripe_offset;
  3817. /* if we're here for raid56, we need to know the stripe aligned start */
  3818. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
  3819. unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
  3820. raid56_full_stripe_start = offset;
  3821. /* allow a write of a full stripe, but make sure we don't
  3822. * allow straddling of stripes
  3823. */
  3824. do_div(raid56_full_stripe_start, full_stripe_len);
  3825. raid56_full_stripe_start *= full_stripe_len;
  3826. }
  3827. if (rw & REQ_DISCARD) {
  3828. /* we don't discard raid56 yet */
  3829. if (map->type &
  3830. (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
  3831. ret = -EOPNOTSUPP;
  3832. goto out;
  3833. }
  3834. *length = min_t(u64, em->len - offset, *length);
  3835. } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  3836. u64 max_len;
  3837. /* For writes to RAID[56], allow a full stripeset across all disks.
  3838. For other RAID types and for RAID[56] reads, just allow a single
  3839. stripe (on a single disk). */
  3840. if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
  3841. (rw & REQ_WRITE)) {
  3842. max_len = stripe_len * nr_data_stripes(map) -
  3843. (offset - raid56_full_stripe_start);
  3844. } else {
  3845. /* we limit the length of each bio to what fits in a stripe */
  3846. max_len = stripe_len - stripe_offset;
  3847. }
  3848. *length = min_t(u64, em->len - offset, max_len);
  3849. } else {
  3850. *length = em->len - offset;
  3851. }
  3852. /* This is for when we're called from btrfs_merge_bio_hook() and all
  3853. it cares about is the length */
  3854. if (!bbio_ret)
  3855. goto out;
  3856. btrfs_dev_replace_lock(dev_replace);
  3857. dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
  3858. if (!dev_replace_is_ongoing)
  3859. btrfs_dev_replace_unlock(dev_replace);
  3860. if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
  3861. !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
  3862. dev_replace->tgtdev != NULL) {
  3863. /*
  3864. * in dev-replace case, for repair case (that's the only
  3865. * case where the mirror is selected explicitly when
  3866. * calling btrfs_map_block), blocks left of the left cursor
  3867. * can also be read from the target drive.
  3868. * For REQ_GET_READ_MIRRORS, the target drive is added as
  3869. * the last one to the array of stripes. For READ, it also
  3870. * needs to be supported using the same mirror number.
  3871. * If the requested block is not left of the left cursor,
  3872. * EIO is returned. This can happen because btrfs_num_copies()
  3873. * returns one more in the dev-replace case.
  3874. */
  3875. u64 tmp_length = *length;
  3876. struct btrfs_bio *tmp_bbio = NULL;
  3877. int tmp_num_stripes;
  3878. u64 srcdev_devid = dev_replace->srcdev->devid;
  3879. int index_srcdev = 0;
  3880. int found = 0;
  3881. u64 physical_of_found = 0;
  3882. ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
  3883. logical, &tmp_length, &tmp_bbio, 0, NULL);
  3884. if (ret) {
  3885. WARN_ON(tmp_bbio != NULL);
  3886. goto out;
  3887. }
  3888. tmp_num_stripes = tmp_bbio->num_stripes;
  3889. if (mirror_num > tmp_num_stripes) {
  3890. /*
  3891. * REQ_GET_READ_MIRRORS does not contain this
  3892. * mirror, that means that the requested area
  3893. * is not left of the left cursor
  3894. */
  3895. ret = -EIO;
  3896. kfree(tmp_bbio);
  3897. goto out;
  3898. }
  3899. /*
  3900. * process the rest of the function using the mirror_num
  3901. * of the source drive. Therefore look it up first.
  3902. * At the end, patch the device pointer to the one of the
  3903. * target drive.
  3904. */
  3905. for (i = 0; i < tmp_num_stripes; i++) {
  3906. if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
  3907. /*
  3908. * In case of DUP, in order to keep it
  3909. * simple, only add the mirror with the
  3910. * lowest physical address
  3911. */
  3912. if (found &&
  3913. physical_of_found <=
  3914. tmp_bbio->stripes[i].physical)
  3915. continue;
  3916. index_srcdev = i;
  3917. found = 1;
  3918. physical_of_found =
  3919. tmp_bbio->stripes[i].physical;
  3920. }
  3921. }
  3922. if (found) {
  3923. mirror_num = index_srcdev + 1;
  3924. patch_the_first_stripe_for_dev_replace = 1;
  3925. physical_to_patch_in_first_stripe = physical_of_found;
  3926. } else {
  3927. WARN_ON(1);
  3928. ret = -EIO;
  3929. kfree(tmp_bbio);
  3930. goto out;
  3931. }
  3932. kfree(tmp_bbio);
  3933. } else if (mirror_num > map->num_stripes) {
  3934. mirror_num = 0;
  3935. }
  3936. num_stripes = 1;
  3937. stripe_index = 0;
  3938. stripe_nr_orig = stripe_nr;
  3939. stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
  3940. do_div(stripe_nr_end, map->stripe_len);
  3941. stripe_end_offset = stripe_nr_end * map->stripe_len -
  3942. (offset + *length);
  3943. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3944. if (rw & REQ_DISCARD)
  3945. num_stripes = min_t(u64, map->num_stripes,
  3946. stripe_nr_end - stripe_nr_orig);
  3947. stripe_index = do_div(stripe_nr, map->num_stripes);
  3948. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  3949. if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
  3950. num_stripes = map->num_stripes;
  3951. else if (mirror_num)
  3952. stripe_index = mirror_num - 1;
  3953. else {
  3954. stripe_index = find_live_mirror(fs_info, map, 0,
  3955. map->num_stripes,
  3956. current->pid % map->num_stripes,
  3957. dev_replace_is_ongoing);
  3958. mirror_num = stripe_index + 1;
  3959. }
  3960. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  3961. if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
  3962. num_stripes = map->num_stripes;
  3963. } else if (mirror_num) {
  3964. stripe_index = mirror_num - 1;
  3965. } else {
  3966. mirror_num = 1;
  3967. }
  3968. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3969. int factor = map->num_stripes / map->sub_stripes;
  3970. stripe_index = do_div(stripe_nr, factor);
  3971. stripe_index *= map->sub_stripes;
  3972. if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
  3973. num_stripes = map->sub_stripes;
  3974. else if (rw & REQ_DISCARD)
  3975. num_stripes = min_t(u64, map->sub_stripes *
  3976. (stripe_nr_end - stripe_nr_orig),
  3977. map->num_stripes);
  3978. else if (mirror_num)
  3979. stripe_index += mirror_num - 1;
  3980. else {
  3981. int old_stripe_index = stripe_index;
  3982. stripe_index = find_live_mirror(fs_info, map,
  3983. stripe_index,
  3984. map->sub_stripes, stripe_index +
  3985. current->pid % map->sub_stripes,
  3986. dev_replace_is_ongoing);
  3987. mirror_num = stripe_index - old_stripe_index + 1;
  3988. }
  3989. } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  3990. BTRFS_BLOCK_GROUP_RAID6)) {
  3991. u64 tmp;
  3992. if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
  3993. && raid_map_ret) {
  3994. int i, rot;
  3995. /* push stripe_nr back to the start of the full stripe */
  3996. stripe_nr = raid56_full_stripe_start;
  3997. do_div(stripe_nr, stripe_len);
  3998. stripe_index = do_div(stripe_nr, nr_data_stripes(map));
  3999. /* RAID[56] write or recovery. Return all stripes */
  4000. num_stripes = map->num_stripes;
  4001. max_errors = nr_parity_stripes(map);
  4002. raid_map = kmalloc(sizeof(u64) * num_stripes,
  4003. GFP_NOFS);
  4004. if (!raid_map) {
  4005. ret = -ENOMEM;
  4006. goto out;
  4007. }
  4008. /* Work out the disk rotation on this stripe-set */
  4009. tmp = stripe_nr;
  4010. rot = do_div(tmp, num_stripes);
  4011. /* Fill in the logical address of each stripe */
  4012. tmp = stripe_nr * nr_data_stripes(map);
  4013. for (i = 0; i < nr_data_stripes(map); i++)
  4014. raid_map[(i+rot) % num_stripes] =
  4015. em->start + (tmp + i) * map->stripe_len;
  4016. raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
  4017. if (map->type & BTRFS_BLOCK_GROUP_RAID6)
  4018. raid_map[(i+rot+1) % num_stripes] =
  4019. RAID6_Q_STRIPE;
  4020. *length = map->stripe_len;
  4021. stripe_index = 0;
  4022. stripe_offset = 0;
  4023. } else {
  4024. /*
  4025. * Mirror #0 or #1 means the original data block.
  4026. * Mirror #2 is RAID5 parity block.
  4027. * Mirror #3 is RAID6 Q block.
  4028. */
  4029. stripe_index = do_div(stripe_nr, nr_data_stripes(map));
  4030. if (mirror_num > 1)
  4031. stripe_index = nr_data_stripes(map) +
  4032. mirror_num - 2;
  4033. /* We distribute the parity blocks across stripes */
  4034. tmp = stripe_nr + stripe_index;
  4035. stripe_index = do_div(tmp, map->num_stripes);
  4036. }
  4037. } else {
  4038. /*
  4039. * after this do_div call, stripe_nr is the number of stripes
  4040. * on this device we have to walk to find the data, and
  4041. * stripe_index is the number of our device in the stripe array
  4042. */
  4043. stripe_index = do_div(stripe_nr, map->num_stripes);
  4044. mirror_num = stripe_index + 1;
  4045. }
  4046. BUG_ON(stripe_index >= map->num_stripes);
  4047. num_alloc_stripes = num_stripes;
  4048. if (dev_replace_is_ongoing) {
  4049. if (rw & (REQ_WRITE | REQ_DISCARD))
  4050. num_alloc_stripes <<= 1;
  4051. if (rw & REQ_GET_READ_MIRRORS)
  4052. num_alloc_stripes++;
  4053. }
  4054. bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
  4055. if (!bbio) {
  4056. ret = -ENOMEM;
  4057. goto out;
  4058. }
  4059. atomic_set(&bbio->error, 0);
  4060. if (rw & REQ_DISCARD) {
  4061. int factor = 0;
  4062. int sub_stripes = 0;
  4063. u64 stripes_per_dev = 0;
  4064. u32 remaining_stripes = 0;
  4065. u32 last_stripe = 0;
  4066. if (map->type &
  4067. (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
  4068. if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  4069. sub_stripes = 1;
  4070. else
  4071. sub_stripes = map->sub_stripes;
  4072. factor = map->num_stripes / sub_stripes;
  4073. stripes_per_dev = div_u64_rem(stripe_nr_end -
  4074. stripe_nr_orig,
  4075. factor,
  4076. &remaining_stripes);
  4077. div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
  4078. last_stripe *= sub_stripes;
  4079. }
  4080. for (i = 0; i < num_stripes; i++) {
  4081. bbio->stripes[i].physical =
  4082. map->stripes[stripe_index].physical +
  4083. stripe_offset + stripe_nr * map->stripe_len;
  4084. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  4085. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  4086. BTRFS_BLOCK_GROUP_RAID10)) {
  4087. bbio->stripes[i].length = stripes_per_dev *
  4088. map->stripe_len;
  4089. if (i / sub_stripes < remaining_stripes)
  4090. bbio->stripes[i].length +=
  4091. map->stripe_len;
  4092. /*
  4093. * Special for the first stripe and
  4094. * the last stripe:
  4095. *
  4096. * |-------|...|-------|
  4097. * |----------|
  4098. * off end_off
  4099. */
  4100. if (i < sub_stripes)
  4101. bbio->stripes[i].length -=
  4102. stripe_offset;
  4103. if (stripe_index >= last_stripe &&
  4104. stripe_index <= (last_stripe +
  4105. sub_stripes - 1))
  4106. bbio->stripes[i].length -=
  4107. stripe_end_offset;
  4108. if (i == sub_stripes - 1)
  4109. stripe_offset = 0;
  4110. } else
  4111. bbio->stripes[i].length = *length;
  4112. stripe_index++;
  4113. if (stripe_index == map->num_stripes) {
  4114. /* This could only happen for RAID0/10 */
  4115. stripe_index = 0;
  4116. stripe_nr++;
  4117. }
  4118. }
  4119. } else {
  4120. for (i = 0; i < num_stripes; i++) {
  4121. bbio->stripes[i].physical =
  4122. map->stripes[stripe_index].physical +
  4123. stripe_offset +
  4124. stripe_nr * map->stripe_len;
  4125. bbio->stripes[i].dev =
  4126. map->stripes[stripe_index].dev;
  4127. stripe_index++;
  4128. }
  4129. }
  4130. if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
  4131. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  4132. BTRFS_BLOCK_GROUP_RAID10 |
  4133. BTRFS_BLOCK_GROUP_RAID5 |
  4134. BTRFS_BLOCK_GROUP_DUP)) {
  4135. max_errors = 1;
  4136. } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
  4137. max_errors = 2;
  4138. }
  4139. }
  4140. if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  4141. dev_replace->tgtdev != NULL) {
  4142. int index_where_to_add;
  4143. u64 srcdev_devid = dev_replace->srcdev->devid;
  4144. /*
  4145. * duplicate the write operations while the dev replace
  4146. * procedure is running. Since the copying of the old disk
  4147. * to the new disk takes place at run time while the
  4148. * filesystem is mounted writable, the regular write
  4149. * operations to the old disk have to be duplicated to go
  4150. * to the new disk as well.
  4151. * Note that device->missing is handled by the caller, and
  4152. * that the write to the old disk is already set up in the
  4153. * stripes array.
  4154. */
  4155. index_where_to_add = num_stripes;
  4156. for (i = 0; i < num_stripes; i++) {
  4157. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  4158. /* write to new disk, too */
  4159. struct btrfs_bio_stripe *new =
  4160. bbio->stripes + index_where_to_add;
  4161. struct btrfs_bio_stripe *old =
  4162. bbio->stripes + i;
  4163. new->physical = old->physical;
  4164. new->length = old->length;
  4165. new->dev = dev_replace->tgtdev;
  4166. index_where_to_add++;
  4167. max_errors++;
  4168. }
  4169. }
  4170. num_stripes = index_where_to_add;
  4171. } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
  4172. dev_replace->tgtdev != NULL) {
  4173. u64 srcdev_devid = dev_replace->srcdev->devid;
  4174. int index_srcdev = 0;
  4175. int found = 0;
  4176. u64 physical_of_found = 0;
  4177. /*
  4178. * During the dev-replace procedure, the target drive can
  4179. * also be used to read data in case it is needed to repair
  4180. * a corrupt block elsewhere. This is possible if the
  4181. * requested area is left of the left cursor. In this area,
  4182. * the target drive is a full copy of the source drive.
  4183. */
  4184. for (i = 0; i < num_stripes; i++) {
  4185. if (bbio->stripes[i].dev->devid == srcdev_devid) {
  4186. /*
  4187. * In case of DUP, in order to keep it
  4188. * simple, only add the mirror with the
  4189. * lowest physical address
  4190. */
  4191. if (found &&
  4192. physical_of_found <=
  4193. bbio->stripes[i].physical)
  4194. continue;
  4195. index_srcdev = i;
  4196. found = 1;
  4197. physical_of_found = bbio->stripes[i].physical;
  4198. }
  4199. }
  4200. if (found) {
  4201. u64 length = map->stripe_len;
  4202. if (physical_of_found + length <=
  4203. dev_replace->cursor_left) {
  4204. struct btrfs_bio_stripe *tgtdev_stripe =
  4205. bbio->stripes + num_stripes;
  4206. tgtdev_stripe->physical = physical_of_found;
  4207. tgtdev_stripe->length =
  4208. bbio->stripes[index_srcdev].length;
  4209. tgtdev_stripe->dev = dev_replace->tgtdev;
  4210. num_stripes++;
  4211. }
  4212. }
  4213. }
  4214. *bbio_ret = bbio;
  4215. bbio->num_stripes = num_stripes;
  4216. bbio->max_errors = max_errors;
  4217. bbio->mirror_num = mirror_num;
  4218. /*
  4219. * this is the case that REQ_READ && dev_replace_is_ongoing &&
  4220. * mirror_num == num_stripes + 1 && dev_replace target drive is
  4221. * available as a mirror
  4222. */
  4223. if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
  4224. WARN_ON(num_stripes > 1);
  4225. bbio->stripes[0].dev = dev_replace->tgtdev;
  4226. bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
  4227. bbio->mirror_num = map->num_stripes + 1;
  4228. }
  4229. if (raid_map) {
  4230. sort_parity_stripes(bbio, raid_map);
  4231. *raid_map_ret = raid_map;
  4232. }
  4233. out:
  4234. if (dev_replace_is_ongoing)
  4235. btrfs_dev_replace_unlock(dev_replace);
  4236. free_extent_map(em);
  4237. return ret;
  4238. }
  4239. int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
  4240. u64 logical, u64 *length,
  4241. struct btrfs_bio **bbio_ret, int mirror_num)
  4242. {
  4243. return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
  4244. mirror_num, NULL);
  4245. }
  4246. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  4247. u64 chunk_start, u64 physical, u64 devid,
  4248. u64 **logical, int *naddrs, int *stripe_len)
  4249. {
  4250. struct extent_map_tree *em_tree = &map_tree->map_tree;
  4251. struct extent_map *em;
  4252. struct map_lookup *map;
  4253. u64 *buf;
  4254. u64 bytenr;
  4255. u64 length;
  4256. u64 stripe_nr;
  4257. u64 rmap_len;
  4258. int i, j, nr = 0;
  4259. read_lock(&em_tree->lock);
  4260. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  4261. read_unlock(&em_tree->lock);
  4262. if (!em) {
  4263. printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
  4264. chunk_start);
  4265. return -EIO;
  4266. }
  4267. if (em->start != chunk_start) {
  4268. printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
  4269. em->start, chunk_start);
  4270. free_extent_map(em);
  4271. return -EIO;
  4272. }
  4273. map = (struct map_lookup *)em->bdev;
  4274. length = em->len;
  4275. rmap_len = map->stripe_len;
  4276. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  4277. do_div(length, map->num_stripes / map->sub_stripes);
  4278. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  4279. do_div(length, map->num_stripes);
  4280. else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
  4281. BTRFS_BLOCK_GROUP_RAID6)) {
  4282. do_div(length, nr_data_stripes(map));
  4283. rmap_len = map->stripe_len * nr_data_stripes(map);
  4284. }
  4285. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  4286. BUG_ON(!buf); /* -ENOMEM */
  4287. for (i = 0; i < map->num_stripes; i++) {
  4288. if (devid && map->stripes[i].dev->devid != devid)
  4289. continue;
  4290. if (map->stripes[i].physical > physical ||
  4291. map->stripes[i].physical + length <= physical)
  4292. continue;
  4293. stripe_nr = physical - map->stripes[i].physical;
  4294. do_div(stripe_nr, map->stripe_len);
  4295. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  4296. stripe_nr = stripe_nr * map->num_stripes + i;
  4297. do_div(stripe_nr, map->sub_stripes);
  4298. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  4299. stripe_nr = stripe_nr * map->num_stripes + i;
  4300. } /* else if RAID[56], multiply by nr_data_stripes().
  4301. * Alternatively, just use rmap_len below instead of
  4302. * map->stripe_len */
  4303. bytenr = chunk_start + stripe_nr * rmap_len;
  4304. WARN_ON(nr >= map->num_stripes);
  4305. for (j = 0; j < nr; j++) {
  4306. if (buf[j] == bytenr)
  4307. break;
  4308. }
  4309. if (j == nr) {
  4310. WARN_ON(nr >= map->num_stripes);
  4311. buf[nr++] = bytenr;
  4312. }
  4313. }
  4314. *logical = buf;
  4315. *naddrs = nr;
  4316. *stripe_len = rmap_len;
  4317. free_extent_map(em);
  4318. return 0;
  4319. }
  4320. static void *merge_stripe_index_into_bio_private(void *bi_private,
  4321. unsigned int stripe_index)
  4322. {
  4323. /*
  4324. * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
  4325. * at most 1.
  4326. * The alternative solution (instead of stealing bits from the
  4327. * pointer) would be to allocate an intermediate structure
  4328. * that contains the old private pointer plus the stripe_index.
  4329. */
  4330. BUG_ON((((uintptr_t)bi_private) & 3) != 0);
  4331. BUG_ON(stripe_index > 3);
  4332. return (void *)(((uintptr_t)bi_private) | stripe_index);
  4333. }
  4334. static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
  4335. {
  4336. return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
  4337. }
  4338. static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
  4339. {
  4340. return (unsigned int)((uintptr_t)bi_private) & 3;
  4341. }
  4342. static void btrfs_end_bio(struct bio *bio, int err)
  4343. {
  4344. struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
  4345. int is_orig_bio = 0;
  4346. if (err) {
  4347. atomic_inc(&bbio->error);
  4348. if (err == -EIO || err == -EREMOTEIO) {
  4349. unsigned int stripe_index =
  4350. extract_stripe_index_from_bio_private(
  4351. bio->bi_private);
  4352. struct btrfs_device *dev;
  4353. BUG_ON(stripe_index >= bbio->num_stripes);
  4354. dev = bbio->stripes[stripe_index].dev;
  4355. if (dev->bdev) {
  4356. if (bio->bi_rw & WRITE)
  4357. btrfs_dev_stat_inc(dev,
  4358. BTRFS_DEV_STAT_WRITE_ERRS);
  4359. else
  4360. btrfs_dev_stat_inc(dev,
  4361. BTRFS_DEV_STAT_READ_ERRS);
  4362. if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
  4363. btrfs_dev_stat_inc(dev,
  4364. BTRFS_DEV_STAT_FLUSH_ERRS);
  4365. btrfs_dev_stat_print_on_error(dev);
  4366. }
  4367. }
  4368. }
  4369. if (bio == bbio->orig_bio)
  4370. is_orig_bio = 1;
  4371. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  4372. if (!is_orig_bio) {
  4373. bio_put(bio);
  4374. bio = bbio->orig_bio;
  4375. }
  4376. bio->bi_private = bbio->private;
  4377. bio->bi_end_io = bbio->end_io;
  4378. bio->bi_bdev = (struct block_device *)
  4379. (unsigned long)bbio->mirror_num;
  4380. /* only send an error to the higher layers if it is
  4381. * beyond the tolerance of the btrfs bio
  4382. */
  4383. if (atomic_read(&bbio->error) > bbio->max_errors) {
  4384. err = -EIO;
  4385. } else {
  4386. /*
  4387. * this bio is actually up to date, we didn't
  4388. * go over the max number of errors
  4389. */
  4390. set_bit(BIO_UPTODATE, &bio->bi_flags);
  4391. err = 0;
  4392. }
  4393. kfree(bbio);
  4394. bio_endio(bio, err);
  4395. } else if (!is_orig_bio) {
  4396. bio_put(bio);
  4397. }
  4398. }
  4399. struct async_sched {
  4400. struct bio *bio;
  4401. int rw;
  4402. struct btrfs_fs_info *info;
  4403. struct btrfs_work work;
  4404. };
  4405. /*
  4406. * see run_scheduled_bios for a description of why bios are collected for
  4407. * async submit.
  4408. *
  4409. * This will add one bio to the pending list for a device and make sure
  4410. * the work struct is scheduled.
  4411. */
  4412. noinline void btrfs_schedule_bio(struct btrfs_root *root,
  4413. struct btrfs_device *device,
  4414. int rw, struct bio *bio)
  4415. {
  4416. int should_queue = 1;
  4417. struct btrfs_pending_bios *pending_bios;
  4418. if (device->missing || !device->bdev) {
  4419. bio_endio(bio, -EIO);
  4420. return;
  4421. }
  4422. /* don't bother with additional async steps for reads, right now */
  4423. if (!(rw & REQ_WRITE)) {
  4424. bio_get(bio);
  4425. btrfsic_submit_bio(rw, bio);
  4426. bio_put(bio);
  4427. return;
  4428. }
  4429. /*
  4430. * nr_async_bios allows us to reliably return congestion to the
  4431. * higher layers. Otherwise, the async bio makes it appear we have
  4432. * made progress against dirty pages when we've really just put it
  4433. * on a queue for later
  4434. */
  4435. atomic_inc(&root->fs_info->nr_async_bios);
  4436. WARN_ON(bio->bi_next);
  4437. bio->bi_next = NULL;
  4438. bio->bi_rw |= rw;
  4439. spin_lock(&device->io_lock);
  4440. if (bio->bi_rw & REQ_SYNC)
  4441. pending_bios = &device->pending_sync_bios;
  4442. else
  4443. pending_bios = &device->pending_bios;
  4444. if (pending_bios->tail)
  4445. pending_bios->tail->bi_next = bio;
  4446. pending_bios->tail = bio;
  4447. if (!pending_bios->head)
  4448. pending_bios->head = bio;
  4449. if (device->running_pending)
  4450. should_queue = 0;
  4451. spin_unlock(&device->io_lock);
  4452. if (should_queue)
  4453. btrfs_queue_worker(&root->fs_info->submit_workers,
  4454. &device->work);
  4455. }
  4456. static int bio_size_ok(struct block_device *bdev, struct bio *bio,
  4457. sector_t sector)
  4458. {
  4459. struct bio_vec *prev;
  4460. struct request_queue *q = bdev_get_queue(bdev);
  4461. unsigned short max_sectors = queue_max_sectors(q);
  4462. struct bvec_merge_data bvm = {
  4463. .bi_bdev = bdev,
  4464. .bi_sector = sector,
  4465. .bi_rw = bio->bi_rw,
  4466. };
  4467. if (bio->bi_vcnt == 0) {
  4468. WARN_ON(1);
  4469. return 1;
  4470. }
  4471. prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  4472. if ((bio->bi_size >> 9) > max_sectors)
  4473. return 0;
  4474. if (!q->merge_bvec_fn)
  4475. return 1;
  4476. bvm.bi_size = bio->bi_size - prev->bv_len;
  4477. if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
  4478. return 0;
  4479. return 1;
  4480. }
  4481. static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
  4482. struct bio *bio, u64 physical, int dev_nr,
  4483. int rw, int async)
  4484. {
  4485. struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
  4486. bio->bi_private = bbio;
  4487. bio->bi_private = merge_stripe_index_into_bio_private(
  4488. bio->bi_private, (unsigned int)dev_nr);
  4489. bio->bi_end_io = btrfs_end_bio;
  4490. bio->bi_sector = physical >> 9;
  4491. #ifdef DEBUG
  4492. {
  4493. struct rcu_string *name;
  4494. rcu_read_lock();
  4495. name = rcu_dereference(dev->name);
  4496. pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
  4497. "(%s id %llu), size=%u\n", rw,
  4498. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  4499. name->str, dev->devid, bio->bi_size);
  4500. rcu_read_unlock();
  4501. }
  4502. #endif
  4503. bio->bi_bdev = dev->bdev;
  4504. if (async)
  4505. btrfs_schedule_bio(root, dev, rw, bio);
  4506. else
  4507. btrfsic_submit_bio(rw, bio);
  4508. }
  4509. static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
  4510. struct bio *first_bio, struct btrfs_device *dev,
  4511. int dev_nr, int rw, int async)
  4512. {
  4513. struct bio_vec *bvec = first_bio->bi_io_vec;
  4514. struct bio *bio;
  4515. int nr_vecs = bio_get_nr_vecs(dev->bdev);
  4516. u64 physical = bbio->stripes[dev_nr].physical;
  4517. again:
  4518. bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
  4519. if (!bio)
  4520. return -ENOMEM;
  4521. while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
  4522. if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  4523. bvec->bv_offset) < bvec->bv_len) {
  4524. u64 len = bio->bi_size;
  4525. atomic_inc(&bbio->stripes_pending);
  4526. submit_stripe_bio(root, bbio, bio, physical, dev_nr,
  4527. rw, async);
  4528. physical += len;
  4529. goto again;
  4530. }
  4531. bvec++;
  4532. }
  4533. submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
  4534. return 0;
  4535. }
  4536. static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
  4537. {
  4538. atomic_inc(&bbio->error);
  4539. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  4540. bio->bi_private = bbio->private;
  4541. bio->bi_end_io = bbio->end_io;
  4542. bio->bi_bdev = (struct block_device *)
  4543. (unsigned long)bbio->mirror_num;
  4544. bio->bi_sector = logical >> 9;
  4545. kfree(bbio);
  4546. bio_endio(bio, -EIO);
  4547. }
  4548. }
  4549. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  4550. int mirror_num, int async_submit)
  4551. {
  4552. struct btrfs_device *dev;
  4553. struct bio *first_bio = bio;
  4554. u64 logical = (u64)bio->bi_sector << 9;
  4555. u64 length = 0;
  4556. u64 map_length;
  4557. u64 *raid_map = NULL;
  4558. int ret;
  4559. int dev_nr = 0;
  4560. int total_devs = 1;
  4561. struct btrfs_bio *bbio = NULL;
  4562. length = bio->bi_size;
  4563. map_length = length;
  4564. ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
  4565. mirror_num, &raid_map);
  4566. if (ret) /* -ENOMEM */
  4567. return ret;
  4568. total_devs = bbio->num_stripes;
  4569. bbio->orig_bio = first_bio;
  4570. bbio->private = first_bio->bi_private;
  4571. bbio->end_io = first_bio->bi_end_io;
  4572. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  4573. if (raid_map) {
  4574. /* In this case, map_length has been set to the length of
  4575. a single stripe; not the whole write */
  4576. if (rw & WRITE) {
  4577. return raid56_parity_write(root, bio, bbio,
  4578. raid_map, map_length);
  4579. } else {
  4580. return raid56_parity_recover(root, bio, bbio,
  4581. raid_map, map_length,
  4582. mirror_num);
  4583. }
  4584. }
  4585. if (map_length < length) {
  4586. btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
  4587. (unsigned long long)logical,
  4588. (unsigned long long)length,
  4589. (unsigned long long)map_length);
  4590. BUG();
  4591. }
  4592. while (dev_nr < total_devs) {
  4593. dev = bbio->stripes[dev_nr].dev;
  4594. if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
  4595. bbio_error(bbio, first_bio, logical);
  4596. dev_nr++;
  4597. continue;
  4598. }
  4599. /*
  4600. * Check and see if we're ok with this bio based on it's size
  4601. * and offset with the given device.
  4602. */
  4603. if (!bio_size_ok(dev->bdev, first_bio,
  4604. bbio->stripes[dev_nr].physical >> 9)) {
  4605. ret = breakup_stripe_bio(root, bbio, first_bio, dev,
  4606. dev_nr, rw, async_submit);
  4607. BUG_ON(ret);
  4608. dev_nr++;
  4609. continue;
  4610. }
  4611. if (dev_nr < total_devs - 1) {
  4612. bio = bio_clone(first_bio, GFP_NOFS);
  4613. BUG_ON(!bio); /* -ENOMEM */
  4614. } else {
  4615. bio = first_bio;
  4616. }
  4617. submit_stripe_bio(root, bbio, bio,
  4618. bbio->stripes[dev_nr].physical, dev_nr, rw,
  4619. async_submit);
  4620. dev_nr++;
  4621. }
  4622. return 0;
  4623. }
  4624. struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
  4625. u8 *uuid, u8 *fsid)
  4626. {
  4627. struct btrfs_device *device;
  4628. struct btrfs_fs_devices *cur_devices;
  4629. cur_devices = fs_info->fs_devices;
  4630. while (cur_devices) {
  4631. if (!fsid ||
  4632. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  4633. device = __find_device(&cur_devices->devices,
  4634. devid, uuid);
  4635. if (device)
  4636. return device;
  4637. }
  4638. cur_devices = cur_devices->seed;
  4639. }
  4640. return NULL;
  4641. }
  4642. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  4643. u64 devid, u8 *dev_uuid)
  4644. {
  4645. struct btrfs_device *device;
  4646. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  4647. device = kzalloc(sizeof(*device), GFP_NOFS);
  4648. if (!device)
  4649. return NULL;
  4650. list_add(&device->dev_list,
  4651. &fs_devices->devices);
  4652. device->dev_root = root->fs_info->dev_root;
  4653. device->devid = devid;
  4654. device->work.func = pending_bios_fn;
  4655. device->fs_devices = fs_devices;
  4656. device->missing = 1;
  4657. fs_devices->num_devices++;
  4658. fs_devices->missing_devices++;
  4659. spin_lock_init(&device->io_lock);
  4660. INIT_LIST_HEAD(&device->dev_alloc_list);
  4661. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  4662. return device;
  4663. }
  4664. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  4665. struct extent_buffer *leaf,
  4666. struct btrfs_chunk *chunk)
  4667. {
  4668. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  4669. struct map_lookup *map;
  4670. struct extent_map *em;
  4671. u64 logical;
  4672. u64 length;
  4673. u64 devid;
  4674. u8 uuid[BTRFS_UUID_SIZE];
  4675. int num_stripes;
  4676. int ret;
  4677. int i;
  4678. logical = key->offset;
  4679. length = btrfs_chunk_length(leaf, chunk);
  4680. read_lock(&map_tree->map_tree.lock);
  4681. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  4682. read_unlock(&map_tree->map_tree.lock);
  4683. /* already mapped? */
  4684. if (em && em->start <= logical && em->start + em->len > logical) {
  4685. free_extent_map(em);
  4686. return 0;
  4687. } else if (em) {
  4688. free_extent_map(em);
  4689. }
  4690. em = alloc_extent_map();
  4691. if (!em)
  4692. return -ENOMEM;
  4693. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  4694. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  4695. if (!map) {
  4696. free_extent_map(em);
  4697. return -ENOMEM;
  4698. }
  4699. em->bdev = (struct block_device *)map;
  4700. em->start = logical;
  4701. em->len = length;
  4702. em->orig_start = 0;
  4703. em->block_start = 0;
  4704. em->block_len = em->len;
  4705. map->num_stripes = num_stripes;
  4706. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  4707. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  4708. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  4709. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  4710. map->type = btrfs_chunk_type(leaf, chunk);
  4711. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  4712. for (i = 0; i < num_stripes; i++) {
  4713. map->stripes[i].physical =
  4714. btrfs_stripe_offset_nr(leaf, chunk, i);
  4715. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  4716. read_extent_buffer(leaf, uuid, (unsigned long)
  4717. btrfs_stripe_dev_uuid_nr(chunk, i),
  4718. BTRFS_UUID_SIZE);
  4719. map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
  4720. uuid, NULL);
  4721. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  4722. kfree(map);
  4723. free_extent_map(em);
  4724. return -EIO;
  4725. }
  4726. if (!map->stripes[i].dev) {
  4727. map->stripes[i].dev =
  4728. add_missing_dev(root, devid, uuid);
  4729. if (!map->stripes[i].dev) {
  4730. kfree(map);
  4731. free_extent_map(em);
  4732. return -EIO;
  4733. }
  4734. }
  4735. map->stripes[i].dev->in_fs_metadata = 1;
  4736. }
  4737. write_lock(&map_tree->map_tree.lock);
  4738. ret = add_extent_mapping(&map_tree->map_tree, em, 0);
  4739. write_unlock(&map_tree->map_tree.lock);
  4740. BUG_ON(ret); /* Tree corruption */
  4741. free_extent_map(em);
  4742. return 0;
  4743. }
  4744. static void fill_device_from_item(struct extent_buffer *leaf,
  4745. struct btrfs_dev_item *dev_item,
  4746. struct btrfs_device *device)
  4747. {
  4748. unsigned long ptr;
  4749. device->devid = btrfs_device_id(leaf, dev_item);
  4750. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  4751. device->total_bytes = device->disk_total_bytes;
  4752. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  4753. device->type = btrfs_device_type(leaf, dev_item);
  4754. device->io_align = btrfs_device_io_align(leaf, dev_item);
  4755. device->io_width = btrfs_device_io_width(leaf, dev_item);
  4756. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  4757. WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
  4758. device->is_tgtdev_for_dev_replace = 0;
  4759. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  4760. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  4761. }
  4762. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  4763. {
  4764. struct btrfs_fs_devices *fs_devices;
  4765. int ret;
  4766. BUG_ON(!mutex_is_locked(&uuid_mutex));
  4767. fs_devices = root->fs_info->fs_devices->seed;
  4768. while (fs_devices) {
  4769. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  4770. ret = 0;
  4771. goto out;
  4772. }
  4773. fs_devices = fs_devices->seed;
  4774. }
  4775. fs_devices = find_fsid(fsid);
  4776. if (!fs_devices) {
  4777. ret = -ENOENT;
  4778. goto out;
  4779. }
  4780. fs_devices = clone_fs_devices(fs_devices);
  4781. if (IS_ERR(fs_devices)) {
  4782. ret = PTR_ERR(fs_devices);
  4783. goto out;
  4784. }
  4785. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  4786. root->fs_info->bdev_holder);
  4787. if (ret) {
  4788. free_fs_devices(fs_devices);
  4789. goto out;
  4790. }
  4791. if (!fs_devices->seeding) {
  4792. __btrfs_close_devices(fs_devices);
  4793. free_fs_devices(fs_devices);
  4794. ret = -EINVAL;
  4795. goto out;
  4796. }
  4797. fs_devices->seed = root->fs_info->fs_devices->seed;
  4798. root->fs_info->fs_devices->seed = fs_devices;
  4799. out:
  4800. return ret;
  4801. }
  4802. static int read_one_dev(struct btrfs_root *root,
  4803. struct extent_buffer *leaf,
  4804. struct btrfs_dev_item *dev_item)
  4805. {
  4806. struct btrfs_device *device;
  4807. u64 devid;
  4808. int ret;
  4809. u8 fs_uuid[BTRFS_UUID_SIZE];
  4810. u8 dev_uuid[BTRFS_UUID_SIZE];
  4811. devid = btrfs_device_id(leaf, dev_item);
  4812. read_extent_buffer(leaf, dev_uuid,
  4813. (unsigned long)btrfs_device_uuid(dev_item),
  4814. BTRFS_UUID_SIZE);
  4815. read_extent_buffer(leaf, fs_uuid,
  4816. (unsigned long)btrfs_device_fsid(dev_item),
  4817. BTRFS_UUID_SIZE);
  4818. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  4819. ret = open_seed_devices(root, fs_uuid);
  4820. if (ret && !btrfs_test_opt(root, DEGRADED))
  4821. return ret;
  4822. }
  4823. device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
  4824. if (!device || !device->bdev) {
  4825. if (!btrfs_test_opt(root, DEGRADED))
  4826. return -EIO;
  4827. if (!device) {
  4828. btrfs_warn(root->fs_info, "devid %llu missing",
  4829. (unsigned long long)devid);
  4830. device = add_missing_dev(root, devid, dev_uuid);
  4831. if (!device)
  4832. return -ENOMEM;
  4833. } else if (!device->missing) {
  4834. /*
  4835. * this happens when a device that was properly setup
  4836. * in the device info lists suddenly goes bad.
  4837. * device->bdev is NULL, and so we have to set
  4838. * device->missing to one here
  4839. */
  4840. root->fs_info->fs_devices->missing_devices++;
  4841. device->missing = 1;
  4842. }
  4843. }
  4844. if (device->fs_devices != root->fs_info->fs_devices) {
  4845. BUG_ON(device->writeable);
  4846. if (device->generation !=
  4847. btrfs_device_generation(leaf, dev_item))
  4848. return -EINVAL;
  4849. }
  4850. fill_device_from_item(leaf, dev_item, device);
  4851. device->dev_root = root->fs_info->dev_root;
  4852. device->in_fs_metadata = 1;
  4853. if (device->writeable && !device->is_tgtdev_for_dev_replace) {
  4854. device->fs_devices->total_rw_bytes += device->total_bytes;
  4855. spin_lock(&root->fs_info->free_chunk_lock);
  4856. root->fs_info->free_chunk_space += device->total_bytes -
  4857. device->bytes_used;
  4858. spin_unlock(&root->fs_info->free_chunk_lock);
  4859. }
  4860. ret = 0;
  4861. return ret;
  4862. }
  4863. int btrfs_read_sys_array(struct btrfs_root *root)
  4864. {
  4865. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  4866. struct extent_buffer *sb;
  4867. struct btrfs_disk_key *disk_key;
  4868. struct btrfs_chunk *chunk;
  4869. u8 *ptr;
  4870. unsigned long sb_ptr;
  4871. int ret = 0;
  4872. u32 num_stripes;
  4873. u32 array_size;
  4874. u32 len = 0;
  4875. u32 cur;
  4876. struct btrfs_key key;
  4877. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  4878. BTRFS_SUPER_INFO_SIZE);
  4879. if (!sb)
  4880. return -ENOMEM;
  4881. btrfs_set_buffer_uptodate(sb);
  4882. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  4883. /*
  4884. * The sb extent buffer is artifical and just used to read the system array.
  4885. * btrfs_set_buffer_uptodate() call does not properly mark all it's
  4886. * pages up-to-date when the page is larger: extent does not cover the
  4887. * whole page and consequently check_page_uptodate does not find all
  4888. * the page's extents up-to-date (the hole beyond sb),
  4889. * write_extent_buffer then triggers a WARN_ON.
  4890. *
  4891. * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
  4892. * but sb spans only this function. Add an explicit SetPageUptodate call
  4893. * to silence the warning eg. on PowerPC 64.
  4894. */
  4895. if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
  4896. SetPageUptodate(sb->pages[0]);
  4897. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  4898. array_size = btrfs_super_sys_array_size(super_copy);
  4899. ptr = super_copy->sys_chunk_array;
  4900. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  4901. cur = 0;
  4902. while (cur < array_size) {
  4903. disk_key = (struct btrfs_disk_key *)ptr;
  4904. btrfs_disk_key_to_cpu(&key, disk_key);
  4905. len = sizeof(*disk_key); ptr += len;
  4906. sb_ptr += len;
  4907. cur += len;
  4908. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  4909. chunk = (struct btrfs_chunk *)sb_ptr;
  4910. ret = read_one_chunk(root, &key, sb, chunk);
  4911. if (ret)
  4912. break;
  4913. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  4914. len = btrfs_chunk_item_size(num_stripes);
  4915. } else {
  4916. ret = -EIO;
  4917. break;
  4918. }
  4919. ptr += len;
  4920. sb_ptr += len;
  4921. cur += len;
  4922. }
  4923. free_extent_buffer(sb);
  4924. return ret;
  4925. }
  4926. int btrfs_read_chunk_tree(struct btrfs_root *root)
  4927. {
  4928. struct btrfs_path *path;
  4929. struct extent_buffer *leaf;
  4930. struct btrfs_key key;
  4931. struct btrfs_key found_key;
  4932. int ret;
  4933. int slot;
  4934. root = root->fs_info->chunk_root;
  4935. path = btrfs_alloc_path();
  4936. if (!path)
  4937. return -ENOMEM;
  4938. mutex_lock(&uuid_mutex);
  4939. lock_chunks(root);
  4940. /* first we search for all of the device items, and then we
  4941. * read in all of the chunk items. This way we can create chunk
  4942. * mappings that reference all of the devices that are afound
  4943. */
  4944. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  4945. key.offset = 0;
  4946. key.type = 0;
  4947. again:
  4948. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4949. if (ret < 0)
  4950. goto error;
  4951. while (1) {
  4952. leaf = path->nodes[0];
  4953. slot = path->slots[0];
  4954. if (slot >= btrfs_header_nritems(leaf)) {
  4955. ret = btrfs_next_leaf(root, path);
  4956. if (ret == 0)
  4957. continue;
  4958. if (ret < 0)
  4959. goto error;
  4960. break;
  4961. }
  4962. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  4963. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  4964. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  4965. break;
  4966. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  4967. struct btrfs_dev_item *dev_item;
  4968. dev_item = btrfs_item_ptr(leaf, slot,
  4969. struct btrfs_dev_item);
  4970. ret = read_one_dev(root, leaf, dev_item);
  4971. if (ret)
  4972. goto error;
  4973. }
  4974. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  4975. struct btrfs_chunk *chunk;
  4976. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  4977. ret = read_one_chunk(root, &found_key, leaf, chunk);
  4978. if (ret)
  4979. goto error;
  4980. }
  4981. path->slots[0]++;
  4982. }
  4983. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  4984. key.objectid = 0;
  4985. btrfs_release_path(path);
  4986. goto again;
  4987. }
  4988. ret = 0;
  4989. error:
  4990. unlock_chunks(root);
  4991. mutex_unlock(&uuid_mutex);
  4992. btrfs_free_path(path);
  4993. return ret;
  4994. }
  4995. static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
  4996. {
  4997. int i;
  4998. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  4999. btrfs_dev_stat_reset(dev, i);
  5000. }
  5001. int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
  5002. {
  5003. struct btrfs_key key;
  5004. struct btrfs_key found_key;
  5005. struct btrfs_root *dev_root = fs_info->dev_root;
  5006. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  5007. struct extent_buffer *eb;
  5008. int slot;
  5009. int ret = 0;
  5010. struct btrfs_device *device;
  5011. struct btrfs_path *path = NULL;
  5012. int i;
  5013. path = btrfs_alloc_path();
  5014. if (!path) {
  5015. ret = -ENOMEM;
  5016. goto out;
  5017. }
  5018. mutex_lock(&fs_devices->device_list_mutex);
  5019. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  5020. int item_size;
  5021. struct btrfs_dev_stats_item *ptr;
  5022. key.objectid = 0;
  5023. key.type = BTRFS_DEV_STATS_KEY;
  5024. key.offset = device->devid;
  5025. ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
  5026. if (ret) {
  5027. __btrfs_reset_dev_stats(device);
  5028. device->dev_stats_valid = 1;
  5029. btrfs_release_path(path);
  5030. continue;
  5031. }
  5032. slot = path->slots[0];
  5033. eb = path->nodes[0];
  5034. btrfs_item_key_to_cpu(eb, &found_key, slot);
  5035. item_size = btrfs_item_size_nr(eb, slot);
  5036. ptr = btrfs_item_ptr(eb, slot,
  5037. struct btrfs_dev_stats_item);
  5038. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  5039. if (item_size >= (1 + i) * sizeof(__le64))
  5040. btrfs_dev_stat_set(device, i,
  5041. btrfs_dev_stats_value(eb, ptr, i));
  5042. else
  5043. btrfs_dev_stat_reset(device, i);
  5044. }
  5045. device->dev_stats_valid = 1;
  5046. btrfs_dev_stat_print_on_load(device);
  5047. btrfs_release_path(path);
  5048. }
  5049. mutex_unlock(&fs_devices->device_list_mutex);
  5050. out:
  5051. btrfs_free_path(path);
  5052. return ret < 0 ? ret : 0;
  5053. }
  5054. static int update_dev_stat_item(struct btrfs_trans_handle *trans,
  5055. struct btrfs_root *dev_root,
  5056. struct btrfs_device *device)
  5057. {
  5058. struct btrfs_path *path;
  5059. struct btrfs_key key;
  5060. struct extent_buffer *eb;
  5061. struct btrfs_dev_stats_item *ptr;
  5062. int ret;
  5063. int i;
  5064. key.objectid = 0;
  5065. key.type = BTRFS_DEV_STATS_KEY;
  5066. key.offset = device->devid;
  5067. path = btrfs_alloc_path();
  5068. BUG_ON(!path);
  5069. ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
  5070. if (ret < 0) {
  5071. printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
  5072. ret, rcu_str_deref(device->name));
  5073. goto out;
  5074. }
  5075. if (ret == 0 &&
  5076. btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
  5077. /* need to delete old one and insert a new one */
  5078. ret = btrfs_del_item(trans, dev_root, path);
  5079. if (ret != 0) {
  5080. printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
  5081. rcu_str_deref(device->name), ret);
  5082. goto out;
  5083. }
  5084. ret = 1;
  5085. }
  5086. if (ret == 1) {
  5087. /* need to insert a new item */
  5088. btrfs_release_path(path);
  5089. ret = btrfs_insert_empty_item(trans, dev_root, path,
  5090. &key, sizeof(*ptr));
  5091. if (ret < 0) {
  5092. printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
  5093. rcu_str_deref(device->name), ret);
  5094. goto out;
  5095. }
  5096. }
  5097. eb = path->nodes[0];
  5098. ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
  5099. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  5100. btrfs_set_dev_stats_value(eb, ptr, i,
  5101. btrfs_dev_stat_read(device, i));
  5102. btrfs_mark_buffer_dirty(eb);
  5103. out:
  5104. btrfs_free_path(path);
  5105. return ret;
  5106. }
  5107. /*
  5108. * called from commit_transaction. Writes all changed device stats to disk.
  5109. */
  5110. int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
  5111. struct btrfs_fs_info *fs_info)
  5112. {
  5113. struct btrfs_root *dev_root = fs_info->dev_root;
  5114. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  5115. struct btrfs_device *device;
  5116. int ret = 0;
  5117. mutex_lock(&fs_devices->device_list_mutex);
  5118. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  5119. if (!device->dev_stats_valid || !device->dev_stats_dirty)
  5120. continue;
  5121. ret = update_dev_stat_item(trans, dev_root, device);
  5122. if (!ret)
  5123. device->dev_stats_dirty = 0;
  5124. }
  5125. mutex_unlock(&fs_devices->device_list_mutex);
  5126. return ret;
  5127. }
  5128. void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
  5129. {
  5130. btrfs_dev_stat_inc(dev, index);
  5131. btrfs_dev_stat_print_on_error(dev);
  5132. }
  5133. void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
  5134. {
  5135. if (!dev->dev_stats_valid)
  5136. return;
  5137. printk_ratelimited_in_rcu(KERN_ERR
  5138. "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
  5139. rcu_str_deref(dev->name),
  5140. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  5141. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  5142. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  5143. btrfs_dev_stat_read(dev,
  5144. BTRFS_DEV_STAT_CORRUPTION_ERRS),
  5145. btrfs_dev_stat_read(dev,
  5146. BTRFS_DEV_STAT_GENERATION_ERRS));
  5147. }
  5148. static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
  5149. {
  5150. int i;
  5151. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  5152. if (btrfs_dev_stat_read(dev, i) != 0)
  5153. break;
  5154. if (i == BTRFS_DEV_STAT_VALUES_MAX)
  5155. return; /* all values == 0, suppress message */
  5156. printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
  5157. rcu_str_deref(dev->name),
  5158. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
  5159. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
  5160. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
  5161. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
  5162. btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
  5163. }
  5164. int btrfs_get_dev_stats(struct btrfs_root *root,
  5165. struct btrfs_ioctl_get_dev_stats *stats)
  5166. {
  5167. struct btrfs_device *dev;
  5168. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  5169. int i;
  5170. mutex_lock(&fs_devices->device_list_mutex);
  5171. dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
  5172. mutex_unlock(&fs_devices->device_list_mutex);
  5173. if (!dev) {
  5174. printk(KERN_WARNING
  5175. "btrfs: get dev_stats failed, device not found\n");
  5176. return -ENODEV;
  5177. } else if (!dev->dev_stats_valid) {
  5178. printk(KERN_WARNING
  5179. "btrfs: get dev_stats failed, not yet valid\n");
  5180. return -ENODEV;
  5181. } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
  5182. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
  5183. if (stats->nr_items > i)
  5184. stats->values[i] =
  5185. btrfs_dev_stat_read_and_reset(dev, i);
  5186. else
  5187. btrfs_dev_stat_reset(dev, i);
  5188. }
  5189. } else {
  5190. for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
  5191. if (stats->nr_items > i)
  5192. stats->values[i] = btrfs_dev_stat_read(dev, i);
  5193. }
  5194. if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
  5195. stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
  5196. return 0;
  5197. }
  5198. int btrfs_scratch_superblock(struct btrfs_device *device)
  5199. {
  5200. struct buffer_head *bh;
  5201. struct btrfs_super_block *disk_super;
  5202. bh = btrfs_read_dev_super(device->bdev);
  5203. if (!bh)
  5204. return -EINVAL;
  5205. disk_super = (struct btrfs_super_block *)bh->b_data;
  5206. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  5207. set_buffer_dirty(bh);
  5208. sync_dirty_buffer(bh);
  5209. brelse(bh);
  5210. return 0;
  5211. }