extent-tree.c 154 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938
  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/pagemap.h>
  20. #include <linux/writeback.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/version.h>
  23. #include "compat.h"
  24. #include "hash.h"
  25. #include "crc32c.h"
  26. #include "ctree.h"
  27. #include "disk-io.h"
  28. #include "print-tree.h"
  29. #include "transaction.h"
  30. #include "volumes.h"
  31. #include "locking.h"
  32. #include "ref-cache.h"
  33. #include "compat.h"
  34. #define PENDING_EXTENT_INSERT 0
  35. #define PENDING_EXTENT_DELETE 1
  36. #define PENDING_BACKREF_UPDATE 2
  37. struct pending_extent_op {
  38. int type;
  39. u64 bytenr;
  40. u64 num_bytes;
  41. u64 parent;
  42. u64 orig_parent;
  43. u64 generation;
  44. u64 orig_generation;
  45. int level;
  46. struct list_head list;
  47. int del;
  48. };
  49. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  50. btrfs_root *extent_root, int all);
  51. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  52. btrfs_root *extent_root, int all);
  53. static struct btrfs_block_group_cache *
  54. __btrfs_find_block_group(struct btrfs_root *root,
  55. struct btrfs_block_group_cache *hint,
  56. u64 search_start, int data, int owner);
  57. static int pin_down_bytes(struct btrfs_trans_handle *trans,
  58. struct btrfs_root *root,
  59. u64 bytenr, u64 num_bytes, int is_data);
  60. static int update_block_group(struct btrfs_trans_handle *trans,
  61. struct btrfs_root *root,
  62. u64 bytenr, u64 num_bytes, int alloc,
  63. int mark_free);
  64. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  65. {
  66. return (cache->flags & bits) == bits;
  67. }
  68. /*
  69. * this adds the block group to the fs_info rb tree for the block group
  70. * cache
  71. */
  72. static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
  73. struct btrfs_block_group_cache *block_group)
  74. {
  75. struct rb_node **p;
  76. struct rb_node *parent = NULL;
  77. struct btrfs_block_group_cache *cache;
  78. spin_lock(&info->block_group_cache_lock);
  79. p = &info->block_group_cache_tree.rb_node;
  80. while (*p) {
  81. parent = *p;
  82. cache = rb_entry(parent, struct btrfs_block_group_cache,
  83. cache_node);
  84. if (block_group->key.objectid < cache->key.objectid) {
  85. p = &(*p)->rb_left;
  86. } else if (block_group->key.objectid > cache->key.objectid) {
  87. p = &(*p)->rb_right;
  88. } else {
  89. spin_unlock(&info->block_group_cache_lock);
  90. return -EEXIST;
  91. }
  92. }
  93. rb_link_node(&block_group->cache_node, parent, p);
  94. rb_insert_color(&block_group->cache_node,
  95. &info->block_group_cache_tree);
  96. spin_unlock(&info->block_group_cache_lock);
  97. return 0;
  98. }
  99. /*
  100. * This will return the block group at or after bytenr if contains is 0, else
  101. * it will return the block group that contains the bytenr
  102. */
  103. static struct btrfs_block_group_cache *
  104. block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
  105. int contains)
  106. {
  107. struct btrfs_block_group_cache *cache, *ret = NULL;
  108. struct rb_node *n;
  109. u64 end, start;
  110. spin_lock(&info->block_group_cache_lock);
  111. n = info->block_group_cache_tree.rb_node;
  112. while (n) {
  113. cache = rb_entry(n, struct btrfs_block_group_cache,
  114. cache_node);
  115. end = cache->key.objectid + cache->key.offset - 1;
  116. start = cache->key.objectid;
  117. if (bytenr < start) {
  118. if (!contains && (!ret || start < ret->key.objectid))
  119. ret = cache;
  120. n = n->rb_left;
  121. } else if (bytenr > start) {
  122. if (contains && bytenr <= end) {
  123. ret = cache;
  124. break;
  125. }
  126. n = n->rb_right;
  127. } else {
  128. ret = cache;
  129. break;
  130. }
  131. }
  132. spin_unlock(&info->block_group_cache_lock);
  133. return ret;
  134. }
  135. /*
  136. * this is only called by cache_block_group, since we could have freed extents
  137. * we need to check the pinned_extents for any extents that can't be used yet
  138. * since their free space will be released as soon as the transaction commits.
  139. */
  140. static int add_new_free_space(struct btrfs_block_group_cache *block_group,
  141. struct btrfs_fs_info *info, u64 start, u64 end)
  142. {
  143. u64 extent_start, extent_end, size;
  144. int ret;
  145. mutex_lock(&info->pinned_mutex);
  146. while (start < end) {
  147. ret = find_first_extent_bit(&info->pinned_extents, start,
  148. &extent_start, &extent_end,
  149. EXTENT_DIRTY);
  150. if (ret)
  151. break;
  152. if (extent_start == start) {
  153. start = extent_end + 1;
  154. } else if (extent_start > start && extent_start < end) {
  155. size = extent_start - start;
  156. ret = btrfs_add_free_space(block_group, start,
  157. size);
  158. BUG_ON(ret);
  159. start = extent_end + 1;
  160. } else {
  161. break;
  162. }
  163. }
  164. if (start < end) {
  165. size = end - start;
  166. ret = btrfs_add_free_space(block_group, start, size);
  167. BUG_ON(ret);
  168. }
  169. mutex_unlock(&info->pinned_mutex);
  170. return 0;
  171. }
  172. static int cache_block_group(struct btrfs_root *root,
  173. struct btrfs_block_group_cache *block_group)
  174. {
  175. struct btrfs_path *path;
  176. int ret = 0;
  177. struct btrfs_key key;
  178. struct extent_buffer *leaf;
  179. int slot;
  180. u64 last = 0;
  181. u64 first_free;
  182. int found = 0;
  183. if (!block_group)
  184. return 0;
  185. root = root->fs_info->extent_root;
  186. if (block_group->cached)
  187. return 0;
  188. path = btrfs_alloc_path();
  189. if (!path)
  190. return -ENOMEM;
  191. path->reada = 2;
  192. /*
  193. * we get into deadlocks with paths held by callers of this function.
  194. * since the alloc_mutex is protecting things right now, just
  195. * skip the locking here
  196. */
  197. path->skip_locking = 1;
  198. first_free = max_t(u64, block_group->key.objectid,
  199. BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
  200. key.objectid = block_group->key.objectid;
  201. key.offset = 0;
  202. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  203. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  204. if (ret < 0)
  205. goto err;
  206. ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
  207. if (ret < 0)
  208. goto err;
  209. if (ret == 0) {
  210. leaf = path->nodes[0];
  211. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  212. if (key.objectid + key.offset > first_free)
  213. first_free = key.objectid + key.offset;
  214. }
  215. while(1) {
  216. leaf = path->nodes[0];
  217. slot = path->slots[0];
  218. if (slot >= btrfs_header_nritems(leaf)) {
  219. ret = btrfs_next_leaf(root, path);
  220. if (ret < 0)
  221. goto err;
  222. if (ret == 0)
  223. continue;
  224. else
  225. break;
  226. }
  227. btrfs_item_key_to_cpu(leaf, &key, slot);
  228. if (key.objectid < block_group->key.objectid)
  229. goto next;
  230. if (key.objectid >= block_group->key.objectid +
  231. block_group->key.offset)
  232. break;
  233. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  234. if (!found) {
  235. last = first_free;
  236. found = 1;
  237. }
  238. add_new_free_space(block_group, root->fs_info, last,
  239. key.objectid);
  240. last = key.objectid + key.offset;
  241. }
  242. next:
  243. path->slots[0]++;
  244. }
  245. if (!found)
  246. last = first_free;
  247. add_new_free_space(block_group, root->fs_info, last,
  248. block_group->key.objectid +
  249. block_group->key.offset);
  250. block_group->cached = 1;
  251. ret = 0;
  252. err:
  253. btrfs_free_path(path);
  254. return ret;
  255. }
  256. /*
  257. * return the block group that starts at or after bytenr
  258. */
  259. static struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
  260. btrfs_fs_info *info,
  261. u64 bytenr)
  262. {
  263. struct btrfs_block_group_cache *cache;
  264. cache = block_group_cache_tree_search(info, bytenr, 0);
  265. return cache;
  266. }
  267. /*
  268. * return the block group that contains teh given bytenr
  269. */
  270. struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
  271. btrfs_fs_info *info,
  272. u64 bytenr)
  273. {
  274. struct btrfs_block_group_cache *cache;
  275. cache = block_group_cache_tree_search(info, bytenr, 1);
  276. return cache;
  277. }
  278. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  279. u64 flags)
  280. {
  281. struct list_head *head = &info->space_info;
  282. struct list_head *cur;
  283. struct btrfs_space_info *found;
  284. list_for_each(cur, head) {
  285. found = list_entry(cur, struct btrfs_space_info, list);
  286. if (found->flags == flags)
  287. return found;
  288. }
  289. return NULL;
  290. }
  291. static u64 div_factor(u64 num, int factor)
  292. {
  293. if (factor == 10)
  294. return num;
  295. num *= factor;
  296. do_div(num, 10);
  297. return num;
  298. }
  299. static struct btrfs_block_group_cache *
  300. __btrfs_find_block_group(struct btrfs_root *root,
  301. struct btrfs_block_group_cache *hint,
  302. u64 search_start, int data, int owner)
  303. {
  304. struct btrfs_block_group_cache *cache;
  305. struct btrfs_block_group_cache *found_group = NULL;
  306. struct btrfs_fs_info *info = root->fs_info;
  307. u64 used;
  308. u64 last = 0;
  309. u64 free_check;
  310. int full_search = 0;
  311. int factor = 10;
  312. int wrapped = 0;
  313. if (data & BTRFS_BLOCK_GROUP_METADATA)
  314. factor = 9;
  315. if (search_start) {
  316. struct btrfs_block_group_cache *shint;
  317. shint = btrfs_lookup_first_block_group(info, search_start);
  318. if (shint && block_group_bits(shint, data)) {
  319. spin_lock(&shint->lock);
  320. used = btrfs_block_group_used(&shint->item);
  321. if (used + shint->pinned + shint->reserved <
  322. div_factor(shint->key.offset, factor)) {
  323. spin_unlock(&shint->lock);
  324. return shint;
  325. }
  326. spin_unlock(&shint->lock);
  327. }
  328. }
  329. if (hint && block_group_bits(hint, data)) {
  330. spin_lock(&hint->lock);
  331. used = btrfs_block_group_used(&hint->item);
  332. if (used + hint->pinned + hint->reserved <
  333. div_factor(hint->key.offset, factor)) {
  334. spin_unlock(&hint->lock);
  335. return hint;
  336. }
  337. spin_unlock(&hint->lock);
  338. last = hint->key.objectid + hint->key.offset;
  339. } else {
  340. if (hint)
  341. last = max(hint->key.objectid, search_start);
  342. else
  343. last = search_start;
  344. }
  345. again:
  346. while (1) {
  347. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  348. if (!cache)
  349. break;
  350. spin_lock(&cache->lock);
  351. last = cache->key.objectid + cache->key.offset;
  352. used = btrfs_block_group_used(&cache->item);
  353. if (block_group_bits(cache, data)) {
  354. free_check = div_factor(cache->key.offset, factor);
  355. if (used + cache->pinned + cache->reserved <
  356. free_check) {
  357. found_group = cache;
  358. spin_unlock(&cache->lock);
  359. goto found;
  360. }
  361. }
  362. spin_unlock(&cache->lock);
  363. cond_resched();
  364. }
  365. if (!wrapped) {
  366. last = search_start;
  367. wrapped = 1;
  368. goto again;
  369. }
  370. if (!full_search && factor < 10) {
  371. last = search_start;
  372. full_search = 1;
  373. factor = 10;
  374. goto again;
  375. }
  376. found:
  377. return found_group;
  378. }
  379. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  380. struct btrfs_block_group_cache
  381. *hint, u64 search_start,
  382. int data, int owner)
  383. {
  384. struct btrfs_block_group_cache *ret;
  385. ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
  386. return ret;
  387. }
  388. /* simple helper to search for an existing extent at a given offset */
  389. int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
  390. {
  391. int ret;
  392. struct btrfs_key key;
  393. struct btrfs_path *path;
  394. path = btrfs_alloc_path();
  395. BUG_ON(!path);
  396. key.objectid = start;
  397. key.offset = len;
  398. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  399. ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
  400. 0, 0);
  401. btrfs_free_path(path);
  402. return ret;
  403. }
  404. /*
  405. * Back reference rules. Back refs have three main goals:
  406. *
  407. * 1) differentiate between all holders of references to an extent so that
  408. * when a reference is dropped we can make sure it was a valid reference
  409. * before freeing the extent.
  410. *
  411. * 2) Provide enough information to quickly find the holders of an extent
  412. * if we notice a given block is corrupted or bad.
  413. *
  414. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  415. * maintenance. This is actually the same as #2, but with a slightly
  416. * different use case.
  417. *
  418. * File extents can be referenced by:
  419. *
  420. * - multiple snapshots, subvolumes, or different generations in one subvol
  421. * - different files inside a single subvolume
  422. * - different offsets inside a file (bookend extents in file.c)
  423. *
  424. * The extent ref structure has fields for:
  425. *
  426. * - Objectid of the subvolume root
  427. * - Generation number of the tree holding the reference
  428. * - objectid of the file holding the reference
  429. * - number of references holding by parent node (alway 1 for tree blocks)
  430. *
  431. * Btree leaf may hold multiple references to a file extent. In most cases,
  432. * these references are from same file and the corresponding offsets inside
  433. * the file are close together.
  434. *
  435. * When a file extent is allocated the fields are filled in:
  436. * (root_key.objectid, trans->transid, inode objectid, 1)
  437. *
  438. * When a leaf is cow'd new references are added for every file extent found
  439. * in the leaf. It looks similar to the create case, but trans->transid will
  440. * be different when the block is cow'd.
  441. *
  442. * (root_key.objectid, trans->transid, inode objectid,
  443. * number of references in the leaf)
  444. *
  445. * When a file extent is removed either during snapshot deletion or
  446. * file truncation, we find the corresponding back reference and check
  447. * the following fields:
  448. *
  449. * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
  450. * inode objectid)
  451. *
  452. * Btree extents can be referenced by:
  453. *
  454. * - Different subvolumes
  455. * - Different generations of the same subvolume
  456. *
  457. * When a tree block is created, back references are inserted:
  458. *
  459. * (root->root_key.objectid, trans->transid, level, 1)
  460. *
  461. * When a tree block is cow'd, new back references are added for all the
  462. * blocks it points to. If the tree block isn't in reference counted root,
  463. * the old back references are removed. These new back references are of
  464. * the form (trans->transid will have increased since creation):
  465. *
  466. * (root->root_key.objectid, trans->transid, level, 1)
  467. *
  468. * When a backref is in deleting, the following fields are checked:
  469. *
  470. * if backref was for a tree root:
  471. * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
  472. * else
  473. * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
  474. *
  475. * Back Reference Key composing:
  476. *
  477. * The key objectid corresponds to the first byte in the extent, the key
  478. * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
  479. * byte of parent extent. If a extent is tree root, the key offset is set
  480. * to the key objectid.
  481. */
  482. static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
  483. struct btrfs_root *root,
  484. struct btrfs_path *path,
  485. u64 bytenr, u64 parent,
  486. u64 ref_root, u64 ref_generation,
  487. u64 owner_objectid, int del)
  488. {
  489. struct btrfs_key key;
  490. struct btrfs_extent_ref *ref;
  491. struct extent_buffer *leaf;
  492. u64 ref_objectid;
  493. int ret;
  494. key.objectid = bytenr;
  495. key.type = BTRFS_EXTENT_REF_KEY;
  496. key.offset = parent;
  497. ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
  498. if (ret < 0)
  499. goto out;
  500. if (ret > 0) {
  501. ret = -ENOENT;
  502. goto out;
  503. }
  504. leaf = path->nodes[0];
  505. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  506. ref_objectid = btrfs_ref_objectid(leaf, ref);
  507. if (btrfs_ref_root(leaf, ref) != ref_root ||
  508. btrfs_ref_generation(leaf, ref) != ref_generation ||
  509. (ref_objectid != owner_objectid &&
  510. ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
  511. ret = -EIO;
  512. WARN_ON(1);
  513. goto out;
  514. }
  515. ret = 0;
  516. out:
  517. return ret;
  518. }
  519. /*
  520. * updates all the backrefs that are pending on update_list for the
  521. * extent_root
  522. */
  523. static int noinline update_backrefs(struct btrfs_trans_handle *trans,
  524. struct btrfs_root *extent_root,
  525. struct btrfs_path *path,
  526. struct list_head *update_list)
  527. {
  528. struct btrfs_key key;
  529. struct btrfs_extent_ref *ref;
  530. struct btrfs_fs_info *info = extent_root->fs_info;
  531. struct pending_extent_op *op;
  532. struct extent_buffer *leaf;
  533. int ret = 0;
  534. struct list_head *cur = update_list->next;
  535. u64 ref_objectid;
  536. u64 ref_root = extent_root->root_key.objectid;
  537. op = list_entry(cur, struct pending_extent_op, list);
  538. search:
  539. key.objectid = op->bytenr;
  540. key.type = BTRFS_EXTENT_REF_KEY;
  541. key.offset = op->orig_parent;
  542. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
  543. BUG_ON(ret);
  544. leaf = path->nodes[0];
  545. loop:
  546. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  547. ref_objectid = btrfs_ref_objectid(leaf, ref);
  548. if (btrfs_ref_root(leaf, ref) != ref_root ||
  549. btrfs_ref_generation(leaf, ref) != op->orig_generation ||
  550. (ref_objectid != op->level &&
  551. ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
  552. printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, "
  553. "owner %u\n", op->bytenr, op->orig_parent,
  554. ref_root, op->level);
  555. btrfs_print_leaf(extent_root, leaf);
  556. BUG();
  557. }
  558. key.objectid = op->bytenr;
  559. key.offset = op->parent;
  560. key.type = BTRFS_EXTENT_REF_KEY;
  561. ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
  562. BUG_ON(ret);
  563. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  564. btrfs_set_ref_generation(leaf, ref, op->generation);
  565. cur = cur->next;
  566. list_del_init(&op->list);
  567. unlock_extent(&info->extent_ins, op->bytenr,
  568. op->bytenr + op->num_bytes - 1, GFP_NOFS);
  569. kfree(op);
  570. if (cur == update_list) {
  571. btrfs_mark_buffer_dirty(path->nodes[0]);
  572. btrfs_release_path(extent_root, path);
  573. goto out;
  574. }
  575. op = list_entry(cur, struct pending_extent_op, list);
  576. path->slots[0]++;
  577. while (path->slots[0] < btrfs_header_nritems(leaf)) {
  578. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  579. if (key.objectid == op->bytenr &&
  580. key.type == BTRFS_EXTENT_REF_KEY)
  581. goto loop;
  582. path->slots[0]++;
  583. }
  584. btrfs_mark_buffer_dirty(path->nodes[0]);
  585. btrfs_release_path(extent_root, path);
  586. goto search;
  587. out:
  588. return 0;
  589. }
  590. static int noinline insert_extents(struct btrfs_trans_handle *trans,
  591. struct btrfs_root *extent_root,
  592. struct btrfs_path *path,
  593. struct list_head *insert_list, int nr)
  594. {
  595. struct btrfs_key *keys;
  596. u32 *data_size;
  597. struct pending_extent_op *op;
  598. struct extent_buffer *leaf;
  599. struct list_head *cur = insert_list->next;
  600. struct btrfs_fs_info *info = extent_root->fs_info;
  601. u64 ref_root = extent_root->root_key.objectid;
  602. int i = 0, last = 0, ret;
  603. int total = nr * 2;
  604. if (!nr)
  605. return 0;
  606. keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
  607. if (!keys)
  608. return -ENOMEM;
  609. data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
  610. if (!data_size) {
  611. kfree(keys);
  612. return -ENOMEM;
  613. }
  614. list_for_each_entry(op, insert_list, list) {
  615. keys[i].objectid = op->bytenr;
  616. keys[i].offset = op->num_bytes;
  617. keys[i].type = BTRFS_EXTENT_ITEM_KEY;
  618. data_size[i] = sizeof(struct btrfs_extent_item);
  619. i++;
  620. keys[i].objectid = op->bytenr;
  621. keys[i].offset = op->parent;
  622. keys[i].type = BTRFS_EXTENT_REF_KEY;
  623. data_size[i] = sizeof(struct btrfs_extent_ref);
  624. i++;
  625. }
  626. op = list_entry(cur, struct pending_extent_op, list);
  627. i = 0;
  628. while (i < total) {
  629. int c;
  630. ret = btrfs_insert_some_items(trans, extent_root, path,
  631. keys+i, data_size+i, total-i);
  632. BUG_ON(ret < 0);
  633. if (last && ret > 1)
  634. BUG();
  635. leaf = path->nodes[0];
  636. for (c = 0; c < ret; c++) {
  637. int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
  638. /*
  639. * if the first item we inserted was a backref, then
  640. * the EXTENT_ITEM will be the odd c's, else it will
  641. * be the even c's
  642. */
  643. if ((ref_first && (c % 2)) ||
  644. (!ref_first && !(c % 2))) {
  645. struct btrfs_extent_item *itm;
  646. itm = btrfs_item_ptr(leaf, path->slots[0] + c,
  647. struct btrfs_extent_item);
  648. btrfs_set_extent_refs(path->nodes[0], itm, 1);
  649. op->del++;
  650. } else {
  651. struct btrfs_extent_ref *ref;
  652. ref = btrfs_item_ptr(leaf, path->slots[0] + c,
  653. struct btrfs_extent_ref);
  654. btrfs_set_ref_root(leaf, ref, ref_root);
  655. btrfs_set_ref_generation(leaf, ref,
  656. op->generation);
  657. btrfs_set_ref_objectid(leaf, ref, op->level);
  658. btrfs_set_ref_num_refs(leaf, ref, 1);
  659. op->del++;
  660. }
  661. /*
  662. * using del to see when its ok to free up the
  663. * pending_extent_op. In the case where we insert the
  664. * last item on the list in order to help do batching
  665. * we need to not free the extent op until we actually
  666. * insert the extent_item
  667. */
  668. if (op->del == 2) {
  669. unlock_extent(&info->extent_ins, op->bytenr,
  670. op->bytenr + op->num_bytes - 1,
  671. GFP_NOFS);
  672. cur = cur->next;
  673. list_del_init(&op->list);
  674. kfree(op);
  675. if (cur != insert_list)
  676. op = list_entry(cur,
  677. struct pending_extent_op,
  678. list);
  679. }
  680. }
  681. btrfs_mark_buffer_dirty(leaf);
  682. btrfs_release_path(extent_root, path);
  683. /*
  684. * Ok backref's and items usually go right next to eachother,
  685. * but if we could only insert 1 item that means that we
  686. * inserted on the end of a leaf, and we have no idea what may
  687. * be on the next leaf so we just play it safe. In order to
  688. * try and help this case we insert the last thing on our
  689. * insert list so hopefully it will end up being the last
  690. * thing on the leaf and everything else will be before it,
  691. * which will let us insert a whole bunch of items at the same
  692. * time.
  693. */
  694. if (ret == 1 && !last && (i + ret < total)) {
  695. /*
  696. * last: where we will pick up the next time around
  697. * i: our current key to insert, will be total - 1
  698. * cur: the current op we are screwing with
  699. * op: duh
  700. */
  701. last = i + ret;
  702. i = total - 1;
  703. cur = insert_list->prev;
  704. op = list_entry(cur, struct pending_extent_op, list);
  705. } else if (last) {
  706. /*
  707. * ok we successfully inserted the last item on the
  708. * list, lets reset everything
  709. *
  710. * i: our current key to insert, so where we left off
  711. * last time
  712. * last: done with this
  713. * cur: the op we are messing with
  714. * op: duh
  715. * total: since we inserted the last key, we need to
  716. * decrement total so we dont overflow
  717. */
  718. i = last;
  719. last = 0;
  720. total--;
  721. if (i < total) {
  722. cur = insert_list->next;
  723. op = list_entry(cur, struct pending_extent_op,
  724. list);
  725. }
  726. } else {
  727. i += ret;
  728. }
  729. cond_resched();
  730. }
  731. ret = 0;
  732. kfree(keys);
  733. kfree(data_size);
  734. return ret;
  735. }
  736. static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
  737. struct btrfs_root *root,
  738. struct btrfs_path *path,
  739. u64 bytenr, u64 parent,
  740. u64 ref_root, u64 ref_generation,
  741. u64 owner_objectid)
  742. {
  743. struct btrfs_key key;
  744. struct extent_buffer *leaf;
  745. struct btrfs_extent_ref *ref;
  746. u32 num_refs;
  747. int ret;
  748. key.objectid = bytenr;
  749. key.type = BTRFS_EXTENT_REF_KEY;
  750. key.offset = parent;
  751. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
  752. if (ret == 0) {
  753. leaf = path->nodes[0];
  754. ref = btrfs_item_ptr(leaf, path->slots[0],
  755. struct btrfs_extent_ref);
  756. btrfs_set_ref_root(leaf, ref, ref_root);
  757. btrfs_set_ref_generation(leaf, ref, ref_generation);
  758. btrfs_set_ref_objectid(leaf, ref, owner_objectid);
  759. btrfs_set_ref_num_refs(leaf, ref, 1);
  760. } else if (ret == -EEXIST) {
  761. u64 existing_owner;
  762. BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
  763. leaf = path->nodes[0];
  764. ref = btrfs_item_ptr(leaf, path->slots[0],
  765. struct btrfs_extent_ref);
  766. if (btrfs_ref_root(leaf, ref) != ref_root ||
  767. btrfs_ref_generation(leaf, ref) != ref_generation) {
  768. ret = -EIO;
  769. WARN_ON(1);
  770. goto out;
  771. }
  772. num_refs = btrfs_ref_num_refs(leaf, ref);
  773. BUG_ON(num_refs == 0);
  774. btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
  775. existing_owner = btrfs_ref_objectid(leaf, ref);
  776. if (existing_owner != owner_objectid &&
  777. existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
  778. btrfs_set_ref_objectid(leaf, ref,
  779. BTRFS_MULTIPLE_OBJECTIDS);
  780. }
  781. ret = 0;
  782. } else {
  783. goto out;
  784. }
  785. btrfs_mark_buffer_dirty(path->nodes[0]);
  786. out:
  787. btrfs_release_path(root, path);
  788. return ret;
  789. }
  790. static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
  791. struct btrfs_root *root,
  792. struct btrfs_path *path)
  793. {
  794. struct extent_buffer *leaf;
  795. struct btrfs_extent_ref *ref;
  796. u32 num_refs;
  797. int ret = 0;
  798. leaf = path->nodes[0];
  799. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  800. num_refs = btrfs_ref_num_refs(leaf, ref);
  801. BUG_ON(num_refs == 0);
  802. num_refs -= 1;
  803. if (num_refs == 0) {
  804. ret = btrfs_del_item(trans, root, path);
  805. } else {
  806. btrfs_set_ref_num_refs(leaf, ref, num_refs);
  807. btrfs_mark_buffer_dirty(leaf);
  808. }
  809. btrfs_release_path(root, path);
  810. return ret;
  811. }
  812. #ifdef BIO_RW_DISCARD
  813. static void btrfs_issue_discard(struct block_device *bdev,
  814. u64 start, u64 len)
  815. {
  816. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  817. blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
  818. #else
  819. blkdev_issue_discard(bdev, start >> 9, len >> 9);
  820. #endif
  821. }
  822. #endif
  823. static int noinline free_extents(struct btrfs_trans_handle *trans,
  824. struct btrfs_root *extent_root,
  825. struct list_head *del_list)
  826. {
  827. struct btrfs_fs_info *info = extent_root->fs_info;
  828. struct btrfs_path *path;
  829. struct btrfs_key key, found_key;
  830. struct extent_buffer *leaf;
  831. struct list_head *cur;
  832. struct pending_extent_op *op;
  833. struct btrfs_extent_item *ei;
  834. int ret, num_to_del, extent_slot = 0, found_extent = 0;
  835. u32 refs;
  836. u64 bytes_freed = 0;
  837. path = btrfs_alloc_path();
  838. if (!path)
  839. return -ENOMEM;
  840. path->reada = 1;
  841. search:
  842. /* search for the backref for the current ref we want to delete */
  843. cur = del_list->next;
  844. op = list_entry(cur, struct pending_extent_op, list);
  845. ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
  846. op->orig_parent,
  847. extent_root->root_key.objectid,
  848. op->orig_generation, op->level, 1);
  849. if (ret) {
  850. printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
  851. "owner %u\n", op->bytenr,
  852. extent_root->root_key.objectid, op->orig_generation,
  853. op->level);
  854. btrfs_print_leaf(extent_root, path->nodes[0]);
  855. WARN_ON(1);
  856. goto out;
  857. }
  858. extent_slot = path->slots[0];
  859. num_to_del = 1;
  860. found_extent = 0;
  861. /*
  862. * if we aren't the first item on the leaf we can move back one and see
  863. * if our ref is right next to our extent item
  864. */
  865. if (likely(extent_slot)) {
  866. extent_slot--;
  867. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  868. extent_slot);
  869. if (found_key.objectid == op->bytenr &&
  870. found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  871. found_key.offset == op->num_bytes) {
  872. num_to_del++;
  873. found_extent = 1;
  874. }
  875. }
  876. /*
  877. * if we didn't find the extent we need to delete the backref and then
  878. * search for the extent item key so we can update its ref count
  879. */
  880. if (!found_extent) {
  881. key.objectid = op->bytenr;
  882. key.type = BTRFS_EXTENT_ITEM_KEY;
  883. key.offset = op->num_bytes;
  884. ret = remove_extent_backref(trans, extent_root, path);
  885. BUG_ON(ret);
  886. btrfs_release_path(extent_root, path);
  887. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  888. BUG_ON(ret);
  889. extent_slot = path->slots[0];
  890. }
  891. /* this is where we update the ref count for the extent */
  892. leaf = path->nodes[0];
  893. ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
  894. refs = btrfs_extent_refs(leaf, ei);
  895. BUG_ON(refs == 0);
  896. refs--;
  897. btrfs_set_extent_refs(leaf, ei, refs);
  898. btrfs_mark_buffer_dirty(leaf);
  899. /*
  900. * This extent needs deleting. The reason cur_slot is extent_slot +
  901. * num_to_del is because extent_slot points to the slot where the extent
  902. * is, and if the backref was not right next to the extent we will be
  903. * deleting at least 1 item, and will want to start searching at the
  904. * slot directly next to extent_slot. However if we did find the
  905. * backref next to the extent item them we will be deleting at least 2
  906. * items and will want to start searching directly after the ref slot
  907. */
  908. if (!refs) {
  909. struct list_head *pos, *n, *end;
  910. int cur_slot = extent_slot+num_to_del;
  911. u64 super_used;
  912. u64 root_used;
  913. path->slots[0] = extent_slot;
  914. bytes_freed = op->num_bytes;
  915. mutex_lock(&info->pinned_mutex);
  916. ret = pin_down_bytes(trans, extent_root, op->bytenr,
  917. op->num_bytes, op->level >=
  918. BTRFS_FIRST_FREE_OBJECTID);
  919. mutex_unlock(&info->pinned_mutex);
  920. BUG_ON(ret < 0);
  921. op->del = ret;
  922. /*
  923. * we need to see if we can delete multiple things at once, so
  924. * start looping through the list of extents we are wanting to
  925. * delete and see if their extent/backref's are right next to
  926. * eachother and the extents only have 1 ref
  927. */
  928. for (pos = cur->next; pos != del_list; pos = pos->next) {
  929. struct pending_extent_op *tmp;
  930. tmp = list_entry(pos, struct pending_extent_op, list);
  931. /* we only want to delete extent+ref at this stage */
  932. if (cur_slot >= btrfs_header_nritems(leaf) - 1)
  933. break;
  934. btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
  935. if (found_key.objectid != tmp->bytenr ||
  936. found_key.type != BTRFS_EXTENT_ITEM_KEY ||
  937. found_key.offset != tmp->num_bytes)
  938. break;
  939. /* check to make sure this extent only has one ref */
  940. ei = btrfs_item_ptr(leaf, cur_slot,
  941. struct btrfs_extent_item);
  942. if (btrfs_extent_refs(leaf, ei) != 1)
  943. break;
  944. btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
  945. if (found_key.objectid != tmp->bytenr ||
  946. found_key.type != BTRFS_EXTENT_REF_KEY ||
  947. found_key.offset != tmp->orig_parent)
  948. break;
  949. /*
  950. * the ref is right next to the extent, we can set the
  951. * ref count to 0 since we will delete them both now
  952. */
  953. btrfs_set_extent_refs(leaf, ei, 0);
  954. /* pin down the bytes for this extent */
  955. mutex_lock(&info->pinned_mutex);
  956. ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
  957. tmp->num_bytes, tmp->level >=
  958. BTRFS_FIRST_FREE_OBJECTID);
  959. mutex_unlock(&info->pinned_mutex);
  960. BUG_ON(ret < 0);
  961. /*
  962. * use the del field to tell if we need to go ahead and
  963. * free up the extent when we delete the item or not.
  964. */
  965. tmp->del = ret;
  966. bytes_freed += tmp->num_bytes;
  967. num_to_del += 2;
  968. cur_slot += 2;
  969. }
  970. end = pos;
  971. /* update the free space counters */
  972. spin_lock_irq(&info->delalloc_lock);
  973. super_used = btrfs_super_bytes_used(&info->super_copy);
  974. btrfs_set_super_bytes_used(&info->super_copy,
  975. super_used - bytes_freed);
  976. spin_unlock_irq(&info->delalloc_lock);
  977. root_used = btrfs_root_used(&extent_root->root_item);
  978. btrfs_set_root_used(&extent_root->root_item,
  979. root_used - bytes_freed);
  980. /* delete the items */
  981. ret = btrfs_del_items(trans, extent_root, path,
  982. path->slots[0], num_to_del);
  983. BUG_ON(ret);
  984. /*
  985. * loop through the extents we deleted and do the cleanup work
  986. * on them
  987. */
  988. for (pos = cur, n = pos->next; pos != end;
  989. pos = n, n = pos->next) {
  990. struct pending_extent_op *tmp;
  991. #ifdef BIO_RW_DISCARD
  992. u64 map_length;
  993. struct btrfs_multi_bio *multi = NULL;
  994. #endif
  995. tmp = list_entry(pos, struct pending_extent_op, list);
  996. /*
  997. * remember tmp->del tells us wether or not we pinned
  998. * down the extent
  999. */
  1000. ret = update_block_group(trans, extent_root,
  1001. tmp->bytenr, tmp->num_bytes, 0,
  1002. tmp->del);
  1003. BUG_ON(ret);
  1004. #ifdef BIO_RW_DISCARD
  1005. map_length = tmp->num_bytes;
  1006. ret = btrfs_map_block(&info->mapping_tree, READ,
  1007. tmp->bytenr, &map_length, &multi,
  1008. 0);
  1009. if (!ret) {
  1010. struct btrfs_bio_stripe *stripe;
  1011. int i;
  1012. stripe = multi->stripes;
  1013. if (map_length > tmp->num_bytes)
  1014. map_length = tmp->num_bytes;
  1015. for (i = 0; i < multi->num_stripes;
  1016. i++, stripe++)
  1017. btrfs_issue_discard(stripe->dev->bdev,
  1018. stripe->physical,
  1019. map_length);
  1020. kfree(multi);
  1021. }
  1022. #endif
  1023. list_del_init(&tmp->list);
  1024. unlock_extent(&info->extent_ins, tmp->bytenr,
  1025. tmp->bytenr + tmp->num_bytes - 1,
  1026. GFP_NOFS);
  1027. kfree(tmp);
  1028. }
  1029. } else if (refs && found_extent) {
  1030. /*
  1031. * the ref and extent were right next to eachother, but the
  1032. * extent still has a ref, so just free the backref and keep
  1033. * going
  1034. */
  1035. ret = remove_extent_backref(trans, extent_root, path);
  1036. BUG_ON(ret);
  1037. list_del_init(&op->list);
  1038. unlock_extent(&info->extent_ins, op->bytenr,
  1039. op->bytenr + op->num_bytes - 1, GFP_NOFS);
  1040. kfree(op);
  1041. } else {
  1042. /*
  1043. * the extent has multiple refs and the backref we were looking
  1044. * for was not right next to it, so just unlock and go next,
  1045. * we're good to go
  1046. */
  1047. list_del_init(&op->list);
  1048. unlock_extent(&info->extent_ins, op->bytenr,
  1049. op->bytenr + op->num_bytes - 1, GFP_NOFS);
  1050. kfree(op);
  1051. }
  1052. btrfs_release_path(extent_root, path);
  1053. if (!list_empty(del_list))
  1054. goto search;
  1055. out:
  1056. btrfs_free_path(path);
  1057. return ret;
  1058. }
  1059. static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
  1060. struct btrfs_root *root, u64 bytenr,
  1061. u64 orig_parent, u64 parent,
  1062. u64 orig_root, u64 ref_root,
  1063. u64 orig_generation, u64 ref_generation,
  1064. u64 owner_objectid)
  1065. {
  1066. int ret;
  1067. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1068. struct btrfs_path *path;
  1069. if (root == root->fs_info->extent_root) {
  1070. struct pending_extent_op *extent_op;
  1071. u64 num_bytes;
  1072. BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
  1073. num_bytes = btrfs_level_size(root, (int)owner_objectid);
  1074. mutex_lock(&root->fs_info->extent_ins_mutex);
  1075. if (test_range_bit(&root->fs_info->extent_ins, bytenr,
  1076. bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
  1077. u64 priv;
  1078. ret = get_state_private(&root->fs_info->extent_ins,
  1079. bytenr, &priv);
  1080. BUG_ON(ret);
  1081. extent_op = (struct pending_extent_op *)
  1082. (unsigned long)priv;
  1083. BUG_ON(extent_op->parent != orig_parent);
  1084. BUG_ON(extent_op->generation != orig_generation);
  1085. extent_op->parent = parent;
  1086. extent_op->generation = ref_generation;
  1087. } else {
  1088. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  1089. BUG_ON(!extent_op);
  1090. extent_op->type = PENDING_BACKREF_UPDATE;
  1091. extent_op->bytenr = bytenr;
  1092. extent_op->num_bytes = num_bytes;
  1093. extent_op->parent = parent;
  1094. extent_op->orig_parent = orig_parent;
  1095. extent_op->generation = ref_generation;
  1096. extent_op->orig_generation = orig_generation;
  1097. extent_op->level = (int)owner_objectid;
  1098. INIT_LIST_HEAD(&extent_op->list);
  1099. extent_op->del = 0;
  1100. set_extent_bits(&root->fs_info->extent_ins,
  1101. bytenr, bytenr + num_bytes - 1,
  1102. EXTENT_WRITEBACK, GFP_NOFS);
  1103. set_state_private(&root->fs_info->extent_ins,
  1104. bytenr, (unsigned long)extent_op);
  1105. }
  1106. mutex_unlock(&root->fs_info->extent_ins_mutex);
  1107. return 0;
  1108. }
  1109. path = btrfs_alloc_path();
  1110. if (!path)
  1111. return -ENOMEM;
  1112. ret = lookup_extent_backref(trans, extent_root, path,
  1113. bytenr, orig_parent, orig_root,
  1114. orig_generation, owner_objectid, 1);
  1115. if (ret)
  1116. goto out;
  1117. ret = remove_extent_backref(trans, extent_root, path);
  1118. if (ret)
  1119. goto out;
  1120. ret = insert_extent_backref(trans, extent_root, path, bytenr,
  1121. parent, ref_root, ref_generation,
  1122. owner_objectid);
  1123. BUG_ON(ret);
  1124. finish_current_insert(trans, extent_root, 0);
  1125. del_pending_extents(trans, extent_root, 0);
  1126. out:
  1127. btrfs_free_path(path);
  1128. return ret;
  1129. }
  1130. int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
  1131. struct btrfs_root *root, u64 bytenr,
  1132. u64 orig_parent, u64 parent,
  1133. u64 ref_root, u64 ref_generation,
  1134. u64 owner_objectid)
  1135. {
  1136. int ret;
  1137. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  1138. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  1139. return 0;
  1140. ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
  1141. parent, ref_root, ref_root,
  1142. ref_generation, ref_generation,
  1143. owner_objectid);
  1144. return ret;
  1145. }
  1146. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1147. struct btrfs_root *root, u64 bytenr,
  1148. u64 orig_parent, u64 parent,
  1149. u64 orig_root, u64 ref_root,
  1150. u64 orig_generation, u64 ref_generation,
  1151. u64 owner_objectid)
  1152. {
  1153. struct btrfs_path *path;
  1154. int ret;
  1155. struct btrfs_key key;
  1156. struct extent_buffer *l;
  1157. struct btrfs_extent_item *item;
  1158. u32 refs;
  1159. path = btrfs_alloc_path();
  1160. if (!path)
  1161. return -ENOMEM;
  1162. path->reada = 1;
  1163. key.objectid = bytenr;
  1164. key.type = BTRFS_EXTENT_ITEM_KEY;
  1165. key.offset = (u64)-1;
  1166. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  1167. 0, 1);
  1168. if (ret < 0)
  1169. return ret;
  1170. BUG_ON(ret == 0 || path->slots[0] == 0);
  1171. path->slots[0]--;
  1172. l = path->nodes[0];
  1173. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1174. if (key.objectid != bytenr) {
  1175. btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
  1176. printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
  1177. BUG();
  1178. }
  1179. BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
  1180. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  1181. refs = btrfs_extent_refs(l, item);
  1182. btrfs_set_extent_refs(l, item, refs + 1);
  1183. btrfs_mark_buffer_dirty(path->nodes[0]);
  1184. btrfs_release_path(root->fs_info->extent_root, path);
  1185. path->reada = 1;
  1186. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  1187. path, bytenr, parent,
  1188. ref_root, ref_generation,
  1189. owner_objectid);
  1190. BUG_ON(ret);
  1191. finish_current_insert(trans, root->fs_info->extent_root, 0);
  1192. del_pending_extents(trans, root->fs_info->extent_root, 0);
  1193. btrfs_free_path(path);
  1194. return 0;
  1195. }
  1196. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  1197. struct btrfs_root *root,
  1198. u64 bytenr, u64 num_bytes, u64 parent,
  1199. u64 ref_root, u64 ref_generation,
  1200. u64 owner_objectid)
  1201. {
  1202. int ret;
  1203. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  1204. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  1205. return 0;
  1206. ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
  1207. 0, ref_root, 0, ref_generation,
  1208. owner_objectid);
  1209. return ret;
  1210. }
  1211. int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
  1212. struct btrfs_root *root)
  1213. {
  1214. finish_current_insert(trans, root->fs_info->extent_root, 1);
  1215. del_pending_extents(trans, root->fs_info->extent_root, 1);
  1216. return 0;
  1217. }
  1218. int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
  1219. struct btrfs_root *root, u64 bytenr,
  1220. u64 num_bytes, u32 *refs)
  1221. {
  1222. struct btrfs_path *path;
  1223. int ret;
  1224. struct btrfs_key key;
  1225. struct extent_buffer *l;
  1226. struct btrfs_extent_item *item;
  1227. WARN_ON(num_bytes < root->sectorsize);
  1228. path = btrfs_alloc_path();
  1229. path->reada = 1;
  1230. key.objectid = bytenr;
  1231. key.offset = num_bytes;
  1232. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  1233. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  1234. 0, 0);
  1235. if (ret < 0)
  1236. goto out;
  1237. if (ret != 0) {
  1238. btrfs_print_leaf(root, path->nodes[0]);
  1239. printk("failed to find block number %Lu\n", bytenr);
  1240. BUG();
  1241. }
  1242. l = path->nodes[0];
  1243. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  1244. *refs = btrfs_extent_refs(l, item);
  1245. out:
  1246. btrfs_free_path(path);
  1247. return 0;
  1248. }
  1249. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  1250. struct btrfs_root *root, u64 bytenr)
  1251. {
  1252. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1253. struct btrfs_path *path;
  1254. struct extent_buffer *leaf;
  1255. struct btrfs_extent_ref *ref_item;
  1256. struct btrfs_key key;
  1257. struct btrfs_key found_key;
  1258. u64 ref_root;
  1259. u64 last_snapshot;
  1260. u32 nritems;
  1261. int ret;
  1262. key.objectid = bytenr;
  1263. key.offset = (u64)-1;
  1264. key.type = BTRFS_EXTENT_ITEM_KEY;
  1265. path = btrfs_alloc_path();
  1266. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  1267. if (ret < 0)
  1268. goto out;
  1269. BUG_ON(ret == 0);
  1270. ret = -ENOENT;
  1271. if (path->slots[0] == 0)
  1272. goto out;
  1273. path->slots[0]--;
  1274. leaf = path->nodes[0];
  1275. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1276. if (found_key.objectid != bytenr ||
  1277. found_key.type != BTRFS_EXTENT_ITEM_KEY)
  1278. goto out;
  1279. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  1280. while (1) {
  1281. leaf = path->nodes[0];
  1282. nritems = btrfs_header_nritems(leaf);
  1283. if (path->slots[0] >= nritems) {
  1284. ret = btrfs_next_leaf(extent_root, path);
  1285. if (ret < 0)
  1286. goto out;
  1287. if (ret == 0)
  1288. continue;
  1289. break;
  1290. }
  1291. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1292. if (found_key.objectid != bytenr)
  1293. break;
  1294. if (found_key.type != BTRFS_EXTENT_REF_KEY) {
  1295. path->slots[0]++;
  1296. continue;
  1297. }
  1298. ref_item = btrfs_item_ptr(leaf, path->slots[0],
  1299. struct btrfs_extent_ref);
  1300. ref_root = btrfs_ref_root(leaf, ref_item);
  1301. if (ref_root != root->root_key.objectid &&
  1302. ref_root != BTRFS_TREE_LOG_OBJECTID) {
  1303. ret = 1;
  1304. goto out;
  1305. }
  1306. if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
  1307. ret = 1;
  1308. goto out;
  1309. }
  1310. path->slots[0]++;
  1311. }
  1312. ret = 0;
  1313. out:
  1314. btrfs_free_path(path);
  1315. return ret;
  1316. }
  1317. int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  1318. struct extent_buffer *buf, u32 nr_extents)
  1319. {
  1320. struct btrfs_key key;
  1321. struct btrfs_file_extent_item *fi;
  1322. u64 root_gen;
  1323. u32 nritems;
  1324. int i;
  1325. int level;
  1326. int ret = 0;
  1327. int shared = 0;
  1328. if (!root->ref_cows)
  1329. return 0;
  1330. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  1331. shared = 0;
  1332. root_gen = root->root_key.offset;
  1333. } else {
  1334. shared = 1;
  1335. root_gen = trans->transid - 1;
  1336. }
  1337. level = btrfs_header_level(buf);
  1338. nritems = btrfs_header_nritems(buf);
  1339. if (level == 0) {
  1340. struct btrfs_leaf_ref *ref;
  1341. struct btrfs_extent_info *info;
  1342. ref = btrfs_alloc_leaf_ref(root, nr_extents);
  1343. if (!ref) {
  1344. ret = -ENOMEM;
  1345. goto out;
  1346. }
  1347. ref->root_gen = root_gen;
  1348. ref->bytenr = buf->start;
  1349. ref->owner = btrfs_header_owner(buf);
  1350. ref->generation = btrfs_header_generation(buf);
  1351. ref->nritems = nr_extents;
  1352. info = ref->extents;
  1353. for (i = 0; nr_extents > 0 && i < nritems; i++) {
  1354. u64 disk_bytenr;
  1355. btrfs_item_key_to_cpu(buf, &key, i);
  1356. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1357. continue;
  1358. fi = btrfs_item_ptr(buf, i,
  1359. struct btrfs_file_extent_item);
  1360. if (btrfs_file_extent_type(buf, fi) ==
  1361. BTRFS_FILE_EXTENT_INLINE)
  1362. continue;
  1363. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1364. if (disk_bytenr == 0)
  1365. continue;
  1366. info->bytenr = disk_bytenr;
  1367. info->num_bytes =
  1368. btrfs_file_extent_disk_num_bytes(buf, fi);
  1369. info->objectid = key.objectid;
  1370. info->offset = key.offset;
  1371. info++;
  1372. }
  1373. ret = btrfs_add_leaf_ref(root, ref, shared);
  1374. if (ret == -EEXIST && shared) {
  1375. struct btrfs_leaf_ref *old;
  1376. old = btrfs_lookup_leaf_ref(root, ref->bytenr);
  1377. BUG_ON(!old);
  1378. btrfs_remove_leaf_ref(root, old);
  1379. btrfs_free_leaf_ref(root, old);
  1380. ret = btrfs_add_leaf_ref(root, ref, shared);
  1381. }
  1382. WARN_ON(ret);
  1383. btrfs_free_leaf_ref(root, ref);
  1384. }
  1385. out:
  1386. return ret;
  1387. }
  1388. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  1389. struct extent_buffer *orig_buf, struct extent_buffer *buf,
  1390. u32 *nr_extents)
  1391. {
  1392. u64 bytenr;
  1393. u64 ref_root;
  1394. u64 orig_root;
  1395. u64 ref_generation;
  1396. u64 orig_generation;
  1397. u32 nritems;
  1398. u32 nr_file_extents = 0;
  1399. struct btrfs_key key;
  1400. struct btrfs_file_extent_item *fi;
  1401. int i;
  1402. int level;
  1403. int ret = 0;
  1404. int faili = 0;
  1405. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  1406. u64, u64, u64, u64, u64, u64, u64, u64);
  1407. ref_root = btrfs_header_owner(buf);
  1408. ref_generation = btrfs_header_generation(buf);
  1409. orig_root = btrfs_header_owner(orig_buf);
  1410. orig_generation = btrfs_header_generation(orig_buf);
  1411. nritems = btrfs_header_nritems(buf);
  1412. level = btrfs_header_level(buf);
  1413. if (root->ref_cows) {
  1414. process_func = __btrfs_inc_extent_ref;
  1415. } else {
  1416. if (level == 0 &&
  1417. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  1418. goto out;
  1419. if (level != 0 &&
  1420. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  1421. goto out;
  1422. process_func = __btrfs_update_extent_ref;
  1423. }
  1424. for (i = 0; i < nritems; i++) {
  1425. cond_resched();
  1426. if (level == 0) {
  1427. btrfs_item_key_to_cpu(buf, &key, i);
  1428. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1429. continue;
  1430. fi = btrfs_item_ptr(buf, i,
  1431. struct btrfs_file_extent_item);
  1432. if (btrfs_file_extent_type(buf, fi) ==
  1433. BTRFS_FILE_EXTENT_INLINE)
  1434. continue;
  1435. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1436. if (bytenr == 0)
  1437. continue;
  1438. nr_file_extents++;
  1439. ret = process_func(trans, root, bytenr,
  1440. orig_buf->start, buf->start,
  1441. orig_root, ref_root,
  1442. orig_generation, ref_generation,
  1443. key.objectid);
  1444. if (ret) {
  1445. faili = i;
  1446. WARN_ON(1);
  1447. goto fail;
  1448. }
  1449. } else {
  1450. bytenr = btrfs_node_blockptr(buf, i);
  1451. ret = process_func(trans, root, bytenr,
  1452. orig_buf->start, buf->start,
  1453. orig_root, ref_root,
  1454. orig_generation, ref_generation,
  1455. level - 1);
  1456. if (ret) {
  1457. faili = i;
  1458. WARN_ON(1);
  1459. goto fail;
  1460. }
  1461. }
  1462. }
  1463. out:
  1464. if (nr_extents) {
  1465. if (level == 0)
  1466. *nr_extents = nr_file_extents;
  1467. else
  1468. *nr_extents = nritems;
  1469. }
  1470. return 0;
  1471. fail:
  1472. WARN_ON(1);
  1473. return ret;
  1474. }
  1475. int btrfs_update_ref(struct btrfs_trans_handle *trans,
  1476. struct btrfs_root *root, struct extent_buffer *orig_buf,
  1477. struct extent_buffer *buf, int start_slot, int nr)
  1478. {
  1479. u64 bytenr;
  1480. u64 ref_root;
  1481. u64 orig_root;
  1482. u64 ref_generation;
  1483. u64 orig_generation;
  1484. struct btrfs_key key;
  1485. struct btrfs_file_extent_item *fi;
  1486. int i;
  1487. int ret;
  1488. int slot;
  1489. int level;
  1490. BUG_ON(start_slot < 0);
  1491. BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
  1492. ref_root = btrfs_header_owner(buf);
  1493. ref_generation = btrfs_header_generation(buf);
  1494. orig_root = btrfs_header_owner(orig_buf);
  1495. orig_generation = btrfs_header_generation(orig_buf);
  1496. level = btrfs_header_level(buf);
  1497. if (!root->ref_cows) {
  1498. if (level == 0 &&
  1499. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  1500. return 0;
  1501. if (level != 0 &&
  1502. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  1503. return 0;
  1504. }
  1505. for (i = 0, slot = start_slot; i < nr; i++, slot++) {
  1506. cond_resched();
  1507. if (level == 0) {
  1508. btrfs_item_key_to_cpu(buf, &key, slot);
  1509. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1510. continue;
  1511. fi = btrfs_item_ptr(buf, slot,
  1512. struct btrfs_file_extent_item);
  1513. if (btrfs_file_extent_type(buf, fi) ==
  1514. BTRFS_FILE_EXTENT_INLINE)
  1515. continue;
  1516. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1517. if (bytenr == 0)
  1518. continue;
  1519. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1520. orig_buf->start, buf->start,
  1521. orig_root, ref_root,
  1522. orig_generation, ref_generation,
  1523. key.objectid);
  1524. if (ret)
  1525. goto fail;
  1526. } else {
  1527. bytenr = btrfs_node_blockptr(buf, slot);
  1528. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1529. orig_buf->start, buf->start,
  1530. orig_root, ref_root,
  1531. orig_generation, ref_generation,
  1532. level - 1);
  1533. if (ret)
  1534. goto fail;
  1535. }
  1536. }
  1537. return 0;
  1538. fail:
  1539. WARN_ON(1);
  1540. return -1;
  1541. }
  1542. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  1543. struct btrfs_root *root,
  1544. struct btrfs_path *path,
  1545. struct btrfs_block_group_cache *cache)
  1546. {
  1547. int ret;
  1548. int pending_ret;
  1549. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1550. unsigned long bi;
  1551. struct extent_buffer *leaf;
  1552. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  1553. if (ret < 0)
  1554. goto fail;
  1555. BUG_ON(ret);
  1556. leaf = path->nodes[0];
  1557. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1558. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  1559. btrfs_mark_buffer_dirty(leaf);
  1560. btrfs_release_path(extent_root, path);
  1561. fail:
  1562. finish_current_insert(trans, extent_root, 0);
  1563. pending_ret = del_pending_extents(trans, extent_root, 0);
  1564. if (ret)
  1565. return ret;
  1566. if (pending_ret)
  1567. return pending_ret;
  1568. return 0;
  1569. }
  1570. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  1571. struct btrfs_root *root)
  1572. {
  1573. struct btrfs_block_group_cache *cache, *entry;
  1574. struct rb_node *n;
  1575. int err = 0;
  1576. int werr = 0;
  1577. struct btrfs_path *path;
  1578. u64 last = 0;
  1579. path = btrfs_alloc_path();
  1580. if (!path)
  1581. return -ENOMEM;
  1582. while(1) {
  1583. cache = NULL;
  1584. spin_lock(&root->fs_info->block_group_cache_lock);
  1585. for (n = rb_first(&root->fs_info->block_group_cache_tree);
  1586. n; n = rb_next(n)) {
  1587. entry = rb_entry(n, struct btrfs_block_group_cache,
  1588. cache_node);
  1589. if (entry->dirty) {
  1590. cache = entry;
  1591. break;
  1592. }
  1593. }
  1594. spin_unlock(&root->fs_info->block_group_cache_lock);
  1595. if (!cache)
  1596. break;
  1597. cache->dirty = 0;
  1598. last += cache->key.offset;
  1599. err = write_one_cache_group(trans, root,
  1600. path, cache);
  1601. /*
  1602. * if we fail to write the cache group, we want
  1603. * to keep it marked dirty in hopes that a later
  1604. * write will work
  1605. */
  1606. if (err) {
  1607. werr = err;
  1608. continue;
  1609. }
  1610. }
  1611. btrfs_free_path(path);
  1612. return werr;
  1613. }
  1614. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  1615. u64 total_bytes, u64 bytes_used,
  1616. struct btrfs_space_info **space_info)
  1617. {
  1618. struct btrfs_space_info *found;
  1619. found = __find_space_info(info, flags);
  1620. if (found) {
  1621. spin_lock(&found->lock);
  1622. found->total_bytes += total_bytes;
  1623. found->bytes_used += bytes_used;
  1624. found->full = 0;
  1625. spin_unlock(&found->lock);
  1626. *space_info = found;
  1627. return 0;
  1628. }
  1629. found = kzalloc(sizeof(*found), GFP_NOFS);
  1630. if (!found)
  1631. return -ENOMEM;
  1632. list_add(&found->list, &info->space_info);
  1633. INIT_LIST_HEAD(&found->block_groups);
  1634. init_rwsem(&found->groups_sem);
  1635. spin_lock_init(&found->lock);
  1636. found->flags = flags;
  1637. found->total_bytes = total_bytes;
  1638. found->bytes_used = bytes_used;
  1639. found->bytes_pinned = 0;
  1640. found->bytes_reserved = 0;
  1641. found->bytes_readonly = 0;
  1642. found->full = 0;
  1643. found->force_alloc = 0;
  1644. *space_info = found;
  1645. return 0;
  1646. }
  1647. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  1648. {
  1649. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  1650. BTRFS_BLOCK_GROUP_RAID1 |
  1651. BTRFS_BLOCK_GROUP_RAID10 |
  1652. BTRFS_BLOCK_GROUP_DUP);
  1653. if (extra_flags) {
  1654. if (flags & BTRFS_BLOCK_GROUP_DATA)
  1655. fs_info->avail_data_alloc_bits |= extra_flags;
  1656. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  1657. fs_info->avail_metadata_alloc_bits |= extra_flags;
  1658. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  1659. fs_info->avail_system_alloc_bits |= extra_flags;
  1660. }
  1661. }
  1662. static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
  1663. {
  1664. spin_lock(&cache->space_info->lock);
  1665. spin_lock(&cache->lock);
  1666. if (!cache->ro) {
  1667. cache->space_info->bytes_readonly += cache->key.offset -
  1668. btrfs_block_group_used(&cache->item);
  1669. cache->ro = 1;
  1670. }
  1671. spin_unlock(&cache->lock);
  1672. spin_unlock(&cache->space_info->lock);
  1673. }
  1674. u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  1675. {
  1676. u64 num_devices = root->fs_info->fs_devices->rw_devices;
  1677. if (num_devices == 1)
  1678. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  1679. if (num_devices < 4)
  1680. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  1681. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  1682. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  1683. BTRFS_BLOCK_GROUP_RAID10))) {
  1684. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  1685. }
  1686. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  1687. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  1688. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  1689. }
  1690. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  1691. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  1692. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  1693. (flags & BTRFS_BLOCK_GROUP_DUP)))
  1694. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  1695. return flags;
  1696. }
  1697. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  1698. struct btrfs_root *extent_root, u64 alloc_bytes,
  1699. u64 flags, int force)
  1700. {
  1701. struct btrfs_space_info *space_info;
  1702. u64 thresh;
  1703. int ret = 0;
  1704. mutex_lock(&extent_root->fs_info->chunk_mutex);
  1705. flags = btrfs_reduce_alloc_profile(extent_root, flags);
  1706. space_info = __find_space_info(extent_root->fs_info, flags);
  1707. if (!space_info) {
  1708. ret = update_space_info(extent_root->fs_info, flags,
  1709. 0, 0, &space_info);
  1710. BUG_ON(ret);
  1711. }
  1712. BUG_ON(!space_info);
  1713. spin_lock(&space_info->lock);
  1714. if (space_info->force_alloc) {
  1715. force = 1;
  1716. space_info->force_alloc = 0;
  1717. }
  1718. if (space_info->full) {
  1719. spin_unlock(&space_info->lock);
  1720. goto out;
  1721. }
  1722. thresh = space_info->total_bytes - space_info->bytes_readonly;
  1723. thresh = div_factor(thresh, 6);
  1724. if (!force &&
  1725. (space_info->bytes_used + space_info->bytes_pinned +
  1726. space_info->bytes_reserved + alloc_bytes) < thresh) {
  1727. spin_unlock(&space_info->lock);
  1728. goto out;
  1729. }
  1730. spin_unlock(&space_info->lock);
  1731. ret = btrfs_alloc_chunk(trans, extent_root, flags);
  1732. if (ret) {
  1733. printk("space info full %Lu\n", flags);
  1734. space_info->full = 1;
  1735. }
  1736. out:
  1737. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  1738. return ret;
  1739. }
  1740. static int update_block_group(struct btrfs_trans_handle *trans,
  1741. struct btrfs_root *root,
  1742. u64 bytenr, u64 num_bytes, int alloc,
  1743. int mark_free)
  1744. {
  1745. struct btrfs_block_group_cache *cache;
  1746. struct btrfs_fs_info *info = root->fs_info;
  1747. u64 total = num_bytes;
  1748. u64 old_val;
  1749. u64 byte_in_group;
  1750. while(total) {
  1751. cache = btrfs_lookup_block_group(info, bytenr);
  1752. if (!cache)
  1753. return -1;
  1754. byte_in_group = bytenr - cache->key.objectid;
  1755. WARN_ON(byte_in_group > cache->key.offset);
  1756. spin_lock(&cache->space_info->lock);
  1757. spin_lock(&cache->lock);
  1758. cache->dirty = 1;
  1759. old_val = btrfs_block_group_used(&cache->item);
  1760. num_bytes = min(total, cache->key.offset - byte_in_group);
  1761. if (alloc) {
  1762. old_val += num_bytes;
  1763. cache->space_info->bytes_used += num_bytes;
  1764. if (cache->ro) {
  1765. cache->space_info->bytes_readonly -= num_bytes;
  1766. WARN_ON(1);
  1767. }
  1768. btrfs_set_block_group_used(&cache->item, old_val);
  1769. spin_unlock(&cache->lock);
  1770. spin_unlock(&cache->space_info->lock);
  1771. } else {
  1772. old_val -= num_bytes;
  1773. cache->space_info->bytes_used -= num_bytes;
  1774. if (cache->ro)
  1775. cache->space_info->bytes_readonly += num_bytes;
  1776. btrfs_set_block_group_used(&cache->item, old_val);
  1777. spin_unlock(&cache->lock);
  1778. spin_unlock(&cache->space_info->lock);
  1779. if (mark_free) {
  1780. int ret;
  1781. ret = btrfs_add_free_space(cache, bytenr,
  1782. num_bytes);
  1783. if (ret)
  1784. return -1;
  1785. }
  1786. }
  1787. total -= num_bytes;
  1788. bytenr += num_bytes;
  1789. }
  1790. return 0;
  1791. }
  1792. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  1793. {
  1794. struct btrfs_block_group_cache *cache;
  1795. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  1796. if (!cache)
  1797. return 0;
  1798. return cache->key.objectid;
  1799. }
  1800. int btrfs_update_pinned_extents(struct btrfs_root *root,
  1801. u64 bytenr, u64 num, int pin)
  1802. {
  1803. u64 len;
  1804. struct btrfs_block_group_cache *cache;
  1805. struct btrfs_fs_info *fs_info = root->fs_info;
  1806. WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
  1807. if (pin) {
  1808. set_extent_dirty(&fs_info->pinned_extents,
  1809. bytenr, bytenr + num - 1, GFP_NOFS);
  1810. } else {
  1811. clear_extent_dirty(&fs_info->pinned_extents,
  1812. bytenr, bytenr + num - 1, GFP_NOFS);
  1813. }
  1814. while (num > 0) {
  1815. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1816. BUG_ON(!cache);
  1817. len = min(num, cache->key.offset -
  1818. (bytenr - cache->key.objectid));
  1819. if (pin) {
  1820. spin_lock(&cache->space_info->lock);
  1821. spin_lock(&cache->lock);
  1822. cache->pinned += len;
  1823. cache->space_info->bytes_pinned += len;
  1824. spin_unlock(&cache->lock);
  1825. spin_unlock(&cache->space_info->lock);
  1826. fs_info->total_pinned += len;
  1827. } else {
  1828. spin_lock(&cache->space_info->lock);
  1829. spin_lock(&cache->lock);
  1830. cache->pinned -= len;
  1831. cache->space_info->bytes_pinned -= len;
  1832. spin_unlock(&cache->lock);
  1833. spin_unlock(&cache->space_info->lock);
  1834. fs_info->total_pinned -= len;
  1835. if (cache->cached)
  1836. btrfs_add_free_space(cache, bytenr, len);
  1837. }
  1838. bytenr += len;
  1839. num -= len;
  1840. }
  1841. return 0;
  1842. }
  1843. static int update_reserved_extents(struct btrfs_root *root,
  1844. u64 bytenr, u64 num, int reserve)
  1845. {
  1846. u64 len;
  1847. struct btrfs_block_group_cache *cache;
  1848. struct btrfs_fs_info *fs_info = root->fs_info;
  1849. while (num > 0) {
  1850. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1851. BUG_ON(!cache);
  1852. len = min(num, cache->key.offset -
  1853. (bytenr - cache->key.objectid));
  1854. spin_lock(&cache->space_info->lock);
  1855. spin_lock(&cache->lock);
  1856. if (reserve) {
  1857. cache->reserved += len;
  1858. cache->space_info->bytes_reserved += len;
  1859. } else {
  1860. cache->reserved -= len;
  1861. cache->space_info->bytes_reserved -= len;
  1862. }
  1863. spin_unlock(&cache->lock);
  1864. spin_unlock(&cache->space_info->lock);
  1865. bytenr += len;
  1866. num -= len;
  1867. }
  1868. return 0;
  1869. }
  1870. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
  1871. {
  1872. u64 last = 0;
  1873. u64 start;
  1874. u64 end;
  1875. struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
  1876. int ret;
  1877. mutex_lock(&root->fs_info->pinned_mutex);
  1878. while(1) {
  1879. ret = find_first_extent_bit(pinned_extents, last,
  1880. &start, &end, EXTENT_DIRTY);
  1881. if (ret)
  1882. break;
  1883. set_extent_dirty(copy, start, end, GFP_NOFS);
  1884. last = end + 1;
  1885. }
  1886. mutex_unlock(&root->fs_info->pinned_mutex);
  1887. return 0;
  1888. }
  1889. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  1890. struct btrfs_root *root,
  1891. struct extent_io_tree *unpin)
  1892. {
  1893. u64 start;
  1894. u64 end;
  1895. int ret;
  1896. mutex_lock(&root->fs_info->pinned_mutex);
  1897. while(1) {
  1898. ret = find_first_extent_bit(unpin, 0, &start, &end,
  1899. EXTENT_DIRTY);
  1900. if (ret)
  1901. break;
  1902. btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
  1903. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  1904. if (need_resched()) {
  1905. mutex_unlock(&root->fs_info->pinned_mutex);
  1906. cond_resched();
  1907. mutex_lock(&root->fs_info->pinned_mutex);
  1908. }
  1909. }
  1910. mutex_unlock(&root->fs_info->pinned_mutex);
  1911. return 0;
  1912. }
  1913. static int finish_current_insert(struct btrfs_trans_handle *trans,
  1914. struct btrfs_root *extent_root, int all)
  1915. {
  1916. u64 start;
  1917. u64 end;
  1918. u64 priv;
  1919. u64 search = 0;
  1920. u64 skipped = 0;
  1921. struct btrfs_fs_info *info = extent_root->fs_info;
  1922. struct btrfs_path *path;
  1923. struct pending_extent_op *extent_op, *tmp;
  1924. struct list_head insert_list, update_list;
  1925. int ret;
  1926. int num_inserts = 0, max_inserts;
  1927. path = btrfs_alloc_path();
  1928. INIT_LIST_HEAD(&insert_list);
  1929. INIT_LIST_HEAD(&update_list);
  1930. max_inserts = extent_root->leafsize /
  1931. (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
  1932. sizeof(struct btrfs_extent_ref) +
  1933. sizeof(struct btrfs_extent_item));
  1934. again:
  1935. mutex_lock(&info->extent_ins_mutex);
  1936. while (1) {
  1937. ret = find_first_extent_bit(&info->extent_ins, search, &start,
  1938. &end, EXTENT_WRITEBACK);
  1939. if (ret) {
  1940. if (skipped && all && !num_inserts) {
  1941. skipped = 0;
  1942. search = 0;
  1943. continue;
  1944. }
  1945. mutex_unlock(&info->extent_ins_mutex);
  1946. break;
  1947. }
  1948. ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
  1949. if (!ret) {
  1950. skipped = 1;
  1951. search = end + 1;
  1952. if (need_resched()) {
  1953. mutex_unlock(&info->extent_ins_mutex);
  1954. cond_resched();
  1955. mutex_lock(&info->extent_ins_mutex);
  1956. }
  1957. continue;
  1958. }
  1959. ret = get_state_private(&info->extent_ins, start, &priv);
  1960. BUG_ON(ret);
  1961. extent_op = (struct pending_extent_op *)(unsigned long) priv;
  1962. if (extent_op->type == PENDING_EXTENT_INSERT) {
  1963. num_inserts++;
  1964. list_add_tail(&extent_op->list, &insert_list);
  1965. search = end + 1;
  1966. if (num_inserts == max_inserts) {
  1967. mutex_unlock(&info->extent_ins_mutex);
  1968. break;
  1969. }
  1970. } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
  1971. list_add_tail(&extent_op->list, &update_list);
  1972. search = end + 1;
  1973. } else {
  1974. BUG();
  1975. }
  1976. }
  1977. /*
  1978. * process the update list, clear the writeback bit for it, and if
  1979. * somebody marked this thing for deletion then just unlock it and be
  1980. * done, the free_extents will handle it
  1981. */
  1982. mutex_lock(&info->extent_ins_mutex);
  1983. list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
  1984. clear_extent_bits(&info->extent_ins, extent_op->bytenr,
  1985. extent_op->bytenr + extent_op->num_bytes - 1,
  1986. EXTENT_WRITEBACK, GFP_NOFS);
  1987. if (extent_op->del) {
  1988. list_del_init(&extent_op->list);
  1989. unlock_extent(&info->extent_ins, extent_op->bytenr,
  1990. extent_op->bytenr + extent_op->num_bytes
  1991. - 1, GFP_NOFS);
  1992. kfree(extent_op);
  1993. }
  1994. }
  1995. mutex_unlock(&info->extent_ins_mutex);
  1996. /*
  1997. * still have things left on the update list, go ahead an update
  1998. * everything
  1999. */
  2000. if (!list_empty(&update_list)) {
  2001. ret = update_backrefs(trans, extent_root, path, &update_list);
  2002. BUG_ON(ret);
  2003. }
  2004. /*
  2005. * if no inserts need to be done, but we skipped some extents and we
  2006. * need to make sure everything is cleaned then reset everything and
  2007. * go back to the beginning
  2008. */
  2009. if (!num_inserts && all && skipped) {
  2010. search = 0;
  2011. skipped = 0;
  2012. INIT_LIST_HEAD(&update_list);
  2013. INIT_LIST_HEAD(&insert_list);
  2014. goto again;
  2015. } else if (!num_inserts) {
  2016. goto out;
  2017. }
  2018. /*
  2019. * process the insert extents list. Again if we are deleting this
  2020. * extent, then just unlock it, pin down the bytes if need be, and be
  2021. * done with it. Saves us from having to actually insert the extent
  2022. * into the tree and then subsequently come along and delete it
  2023. */
  2024. mutex_lock(&info->extent_ins_mutex);
  2025. list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
  2026. clear_extent_bits(&info->extent_ins, extent_op->bytenr,
  2027. extent_op->bytenr + extent_op->num_bytes - 1,
  2028. EXTENT_WRITEBACK, GFP_NOFS);
  2029. if (extent_op->del) {
  2030. list_del_init(&extent_op->list);
  2031. unlock_extent(&info->extent_ins, extent_op->bytenr,
  2032. extent_op->bytenr + extent_op->num_bytes
  2033. - 1, GFP_NOFS);
  2034. mutex_lock(&extent_root->fs_info->pinned_mutex);
  2035. ret = pin_down_bytes(trans, extent_root,
  2036. extent_op->bytenr,
  2037. extent_op->num_bytes, 0);
  2038. mutex_unlock(&extent_root->fs_info->pinned_mutex);
  2039. ret = update_block_group(trans, extent_root,
  2040. extent_op->bytenr,
  2041. extent_op->num_bytes,
  2042. 0, ret > 0);
  2043. BUG_ON(ret);
  2044. kfree(extent_op);
  2045. num_inserts--;
  2046. }
  2047. }
  2048. mutex_unlock(&info->extent_ins_mutex);
  2049. ret = insert_extents(trans, extent_root, path, &insert_list,
  2050. num_inserts);
  2051. BUG_ON(ret);
  2052. /*
  2053. * if we broke out of the loop in order to insert stuff because we hit
  2054. * the maximum number of inserts at a time we can handle, then loop
  2055. * back and pick up where we left off
  2056. */
  2057. if (num_inserts == max_inserts) {
  2058. INIT_LIST_HEAD(&insert_list);
  2059. INIT_LIST_HEAD(&update_list);
  2060. num_inserts = 0;
  2061. goto again;
  2062. }
  2063. /*
  2064. * again, if we need to make absolutely sure there are no more pending
  2065. * extent operations left and we know that we skipped some, go back to
  2066. * the beginning and do it all again
  2067. */
  2068. if (all && skipped) {
  2069. INIT_LIST_HEAD(&insert_list);
  2070. INIT_LIST_HEAD(&update_list);
  2071. search = 0;
  2072. skipped = 0;
  2073. num_inserts = 0;
  2074. goto again;
  2075. }
  2076. out:
  2077. btrfs_free_path(path);
  2078. return 0;
  2079. }
  2080. static int pin_down_bytes(struct btrfs_trans_handle *trans,
  2081. struct btrfs_root *root,
  2082. u64 bytenr, u64 num_bytes, int is_data)
  2083. {
  2084. int err = 0;
  2085. struct extent_buffer *buf;
  2086. if (is_data)
  2087. goto pinit;
  2088. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  2089. if (!buf)
  2090. goto pinit;
  2091. /* we can reuse a block if it hasn't been written
  2092. * and it is from this transaction. We can't
  2093. * reuse anything from the tree log root because
  2094. * it has tiny sub-transactions.
  2095. */
  2096. if (btrfs_buffer_uptodate(buf, 0) &&
  2097. btrfs_try_tree_lock(buf)) {
  2098. u64 header_owner = btrfs_header_owner(buf);
  2099. u64 header_transid = btrfs_header_generation(buf);
  2100. if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
  2101. header_owner != BTRFS_TREE_RELOC_OBJECTID &&
  2102. header_transid == trans->transid &&
  2103. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  2104. clean_tree_block(NULL, root, buf);
  2105. btrfs_tree_unlock(buf);
  2106. free_extent_buffer(buf);
  2107. return 1;
  2108. }
  2109. btrfs_tree_unlock(buf);
  2110. }
  2111. free_extent_buffer(buf);
  2112. pinit:
  2113. btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
  2114. BUG_ON(err < 0);
  2115. return 0;
  2116. }
  2117. /*
  2118. * remove an extent from the root, returns 0 on success
  2119. */
  2120. static int __free_extent(struct btrfs_trans_handle *trans,
  2121. struct btrfs_root *root,
  2122. u64 bytenr, u64 num_bytes, u64 parent,
  2123. u64 root_objectid, u64 ref_generation,
  2124. u64 owner_objectid, int pin, int mark_free)
  2125. {
  2126. struct btrfs_path *path;
  2127. struct btrfs_key key;
  2128. struct btrfs_fs_info *info = root->fs_info;
  2129. struct btrfs_root *extent_root = info->extent_root;
  2130. struct extent_buffer *leaf;
  2131. int ret;
  2132. int extent_slot = 0;
  2133. int found_extent = 0;
  2134. int num_to_del = 1;
  2135. struct btrfs_extent_item *ei;
  2136. u32 refs;
  2137. key.objectid = bytenr;
  2138. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  2139. key.offset = num_bytes;
  2140. path = btrfs_alloc_path();
  2141. if (!path)
  2142. return -ENOMEM;
  2143. path->reada = 1;
  2144. ret = lookup_extent_backref(trans, extent_root, path,
  2145. bytenr, parent, root_objectid,
  2146. ref_generation, owner_objectid, 1);
  2147. if (ret == 0) {
  2148. struct btrfs_key found_key;
  2149. extent_slot = path->slots[0];
  2150. while(extent_slot > 0) {
  2151. extent_slot--;
  2152. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2153. extent_slot);
  2154. if (found_key.objectid != bytenr)
  2155. break;
  2156. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  2157. found_key.offset == num_bytes) {
  2158. found_extent = 1;
  2159. break;
  2160. }
  2161. if (path->slots[0] - extent_slot > 5)
  2162. break;
  2163. }
  2164. if (!found_extent) {
  2165. ret = remove_extent_backref(trans, extent_root, path);
  2166. BUG_ON(ret);
  2167. btrfs_release_path(extent_root, path);
  2168. ret = btrfs_search_slot(trans, extent_root,
  2169. &key, path, -1, 1);
  2170. if (ret) {
  2171. printk(KERN_ERR "umm, got %d back from search"
  2172. ", was looking for %Lu\n", ret,
  2173. bytenr);
  2174. btrfs_print_leaf(extent_root, path->nodes[0]);
  2175. }
  2176. BUG_ON(ret);
  2177. extent_slot = path->slots[0];
  2178. }
  2179. } else {
  2180. btrfs_print_leaf(extent_root, path->nodes[0]);
  2181. WARN_ON(1);
  2182. printk("Unable to find ref byte nr %Lu root %Lu "
  2183. "gen %Lu owner %Lu\n", bytenr,
  2184. root_objectid, ref_generation, owner_objectid);
  2185. }
  2186. leaf = path->nodes[0];
  2187. ei = btrfs_item_ptr(leaf, extent_slot,
  2188. struct btrfs_extent_item);
  2189. refs = btrfs_extent_refs(leaf, ei);
  2190. BUG_ON(refs == 0);
  2191. refs -= 1;
  2192. btrfs_set_extent_refs(leaf, ei, refs);
  2193. btrfs_mark_buffer_dirty(leaf);
  2194. if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
  2195. struct btrfs_extent_ref *ref;
  2196. ref = btrfs_item_ptr(leaf, path->slots[0],
  2197. struct btrfs_extent_ref);
  2198. BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
  2199. /* if the back ref and the extent are next to each other
  2200. * they get deleted below in one shot
  2201. */
  2202. path->slots[0] = extent_slot;
  2203. num_to_del = 2;
  2204. } else if (found_extent) {
  2205. /* otherwise delete the extent back ref */
  2206. ret = remove_extent_backref(trans, extent_root, path);
  2207. BUG_ON(ret);
  2208. /* if refs are 0, we need to setup the path for deletion */
  2209. if (refs == 0) {
  2210. btrfs_release_path(extent_root, path);
  2211. ret = btrfs_search_slot(trans, extent_root, &key, path,
  2212. -1, 1);
  2213. BUG_ON(ret);
  2214. }
  2215. }
  2216. if (refs == 0) {
  2217. u64 super_used;
  2218. u64 root_used;
  2219. #ifdef BIO_RW_DISCARD
  2220. u64 map_length = num_bytes;
  2221. struct btrfs_multi_bio *multi = NULL;
  2222. #endif
  2223. if (pin) {
  2224. mutex_lock(&root->fs_info->pinned_mutex);
  2225. ret = pin_down_bytes(trans, root, bytenr, num_bytes,
  2226. owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
  2227. mutex_unlock(&root->fs_info->pinned_mutex);
  2228. if (ret > 0)
  2229. mark_free = 1;
  2230. BUG_ON(ret < 0);
  2231. }
  2232. /* block accounting for super block */
  2233. spin_lock_irq(&info->delalloc_lock);
  2234. super_used = btrfs_super_bytes_used(&info->super_copy);
  2235. btrfs_set_super_bytes_used(&info->super_copy,
  2236. super_used - num_bytes);
  2237. spin_unlock_irq(&info->delalloc_lock);
  2238. /* block accounting for root item */
  2239. root_used = btrfs_root_used(&root->root_item);
  2240. btrfs_set_root_used(&root->root_item,
  2241. root_used - num_bytes);
  2242. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  2243. num_to_del);
  2244. BUG_ON(ret);
  2245. btrfs_release_path(extent_root, path);
  2246. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  2247. mark_free);
  2248. BUG_ON(ret);
  2249. #ifdef BIO_RW_DISCARD
  2250. /* Tell the block device(s) that the sectors can be discarded */
  2251. ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
  2252. bytenr, &map_length, &multi, 0);
  2253. if (!ret) {
  2254. struct btrfs_bio_stripe *stripe = multi->stripes;
  2255. int i;
  2256. if (map_length > num_bytes)
  2257. map_length = num_bytes;
  2258. for (i = 0; i < multi->num_stripes; i++, stripe++) {
  2259. btrfs_issue_discard(stripe->dev->bdev,
  2260. stripe->physical,
  2261. map_length);
  2262. }
  2263. kfree(multi);
  2264. }
  2265. #endif
  2266. }
  2267. btrfs_free_path(path);
  2268. finish_current_insert(trans, extent_root, 0);
  2269. return ret;
  2270. }
  2271. /*
  2272. * find all the blocks marked as pending in the radix tree and remove
  2273. * them from the extent map
  2274. */
  2275. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  2276. btrfs_root *extent_root, int all)
  2277. {
  2278. int ret;
  2279. int err = 0;
  2280. u64 start;
  2281. u64 end;
  2282. u64 priv;
  2283. u64 search = 0;
  2284. int nr = 0, skipped = 0;
  2285. struct extent_io_tree *pending_del;
  2286. struct extent_io_tree *extent_ins;
  2287. struct pending_extent_op *extent_op;
  2288. struct btrfs_fs_info *info = extent_root->fs_info;
  2289. struct list_head delete_list;
  2290. INIT_LIST_HEAD(&delete_list);
  2291. extent_ins = &extent_root->fs_info->extent_ins;
  2292. pending_del = &extent_root->fs_info->pending_del;
  2293. again:
  2294. mutex_lock(&info->extent_ins_mutex);
  2295. while(1) {
  2296. ret = find_first_extent_bit(pending_del, search, &start, &end,
  2297. EXTENT_WRITEBACK);
  2298. if (ret) {
  2299. if (all && skipped && !nr) {
  2300. search = 0;
  2301. continue;
  2302. }
  2303. mutex_unlock(&info->extent_ins_mutex);
  2304. break;
  2305. }
  2306. ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
  2307. if (!ret) {
  2308. search = end+1;
  2309. skipped = 1;
  2310. if (need_resched()) {
  2311. mutex_unlock(&info->extent_ins_mutex);
  2312. cond_resched();
  2313. mutex_lock(&info->extent_ins_mutex);
  2314. }
  2315. continue;
  2316. }
  2317. BUG_ON(ret < 0);
  2318. ret = get_state_private(pending_del, start, &priv);
  2319. BUG_ON(ret);
  2320. extent_op = (struct pending_extent_op *)(unsigned long)priv;
  2321. clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
  2322. GFP_NOFS);
  2323. if (!test_range_bit(extent_ins, start, end,
  2324. EXTENT_WRITEBACK, 0)) {
  2325. list_add_tail(&extent_op->list, &delete_list);
  2326. nr++;
  2327. } else {
  2328. kfree(extent_op);
  2329. ret = get_state_private(&info->extent_ins, start,
  2330. &priv);
  2331. BUG_ON(ret);
  2332. extent_op = (struct pending_extent_op *)
  2333. (unsigned long)priv;
  2334. clear_extent_bits(&info->extent_ins, start, end,
  2335. EXTENT_WRITEBACK, GFP_NOFS);
  2336. if (extent_op->type == PENDING_BACKREF_UPDATE) {
  2337. list_add_tail(&extent_op->list, &delete_list);
  2338. search = end + 1;
  2339. nr++;
  2340. continue;
  2341. }
  2342. mutex_lock(&extent_root->fs_info->pinned_mutex);
  2343. ret = pin_down_bytes(trans, extent_root, start,
  2344. end + 1 - start, 0);
  2345. mutex_unlock(&extent_root->fs_info->pinned_mutex);
  2346. ret = update_block_group(trans, extent_root, start,
  2347. end + 1 - start, 0, ret > 0);
  2348. unlock_extent(extent_ins, start, end, GFP_NOFS);
  2349. BUG_ON(ret);
  2350. kfree(extent_op);
  2351. }
  2352. if (ret)
  2353. err = ret;
  2354. search = end + 1;
  2355. if (need_resched()) {
  2356. mutex_unlock(&info->extent_ins_mutex);
  2357. cond_resched();
  2358. mutex_lock(&info->extent_ins_mutex);
  2359. }
  2360. }
  2361. if (nr) {
  2362. ret = free_extents(trans, extent_root, &delete_list);
  2363. BUG_ON(ret);
  2364. }
  2365. if (all && skipped) {
  2366. INIT_LIST_HEAD(&delete_list);
  2367. search = 0;
  2368. nr = 0;
  2369. goto again;
  2370. }
  2371. return err;
  2372. }
  2373. /*
  2374. * remove an extent from the root, returns 0 on success
  2375. */
  2376. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  2377. struct btrfs_root *root,
  2378. u64 bytenr, u64 num_bytes, u64 parent,
  2379. u64 root_objectid, u64 ref_generation,
  2380. u64 owner_objectid, int pin)
  2381. {
  2382. struct btrfs_root *extent_root = root->fs_info->extent_root;
  2383. int pending_ret;
  2384. int ret;
  2385. WARN_ON(num_bytes < root->sectorsize);
  2386. if (root == extent_root) {
  2387. struct pending_extent_op *extent_op = NULL;
  2388. mutex_lock(&root->fs_info->extent_ins_mutex);
  2389. if (test_range_bit(&root->fs_info->extent_ins, bytenr,
  2390. bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
  2391. u64 priv;
  2392. ret = get_state_private(&root->fs_info->extent_ins,
  2393. bytenr, &priv);
  2394. BUG_ON(ret);
  2395. extent_op = (struct pending_extent_op *)
  2396. (unsigned long)priv;
  2397. extent_op->del = 1;
  2398. if (extent_op->type == PENDING_EXTENT_INSERT) {
  2399. mutex_unlock(&root->fs_info->extent_ins_mutex);
  2400. return 0;
  2401. }
  2402. }
  2403. if (extent_op) {
  2404. ref_generation = extent_op->orig_generation;
  2405. parent = extent_op->orig_parent;
  2406. }
  2407. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  2408. BUG_ON(!extent_op);
  2409. extent_op->type = PENDING_EXTENT_DELETE;
  2410. extent_op->bytenr = bytenr;
  2411. extent_op->num_bytes = num_bytes;
  2412. extent_op->parent = parent;
  2413. extent_op->orig_parent = parent;
  2414. extent_op->generation = ref_generation;
  2415. extent_op->orig_generation = ref_generation;
  2416. extent_op->level = (int)owner_objectid;
  2417. INIT_LIST_HEAD(&extent_op->list);
  2418. extent_op->del = 0;
  2419. set_extent_bits(&root->fs_info->pending_del,
  2420. bytenr, bytenr + num_bytes - 1,
  2421. EXTENT_WRITEBACK, GFP_NOFS);
  2422. set_state_private(&root->fs_info->pending_del,
  2423. bytenr, (unsigned long)extent_op);
  2424. mutex_unlock(&root->fs_info->extent_ins_mutex);
  2425. return 0;
  2426. }
  2427. /* if metadata always pin */
  2428. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  2429. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  2430. struct btrfs_block_group_cache *cache;
  2431. /* btrfs_free_reserved_extent */
  2432. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  2433. BUG_ON(!cache);
  2434. btrfs_add_free_space(cache, bytenr, num_bytes);
  2435. update_reserved_extents(root, bytenr, num_bytes, 0);
  2436. return 0;
  2437. }
  2438. pin = 1;
  2439. }
  2440. /* if data pin when any transaction has committed this */
  2441. if (ref_generation != trans->transid)
  2442. pin = 1;
  2443. ret = __free_extent(trans, root, bytenr, num_bytes, parent,
  2444. root_objectid, ref_generation,
  2445. owner_objectid, pin, pin == 0);
  2446. finish_current_insert(trans, root->fs_info->extent_root, 0);
  2447. pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
  2448. return ret ? ret : pending_ret;
  2449. }
  2450. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  2451. struct btrfs_root *root,
  2452. u64 bytenr, u64 num_bytes, u64 parent,
  2453. u64 root_objectid, u64 ref_generation,
  2454. u64 owner_objectid, int pin)
  2455. {
  2456. int ret;
  2457. ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
  2458. root_objectid, ref_generation,
  2459. owner_objectid, pin);
  2460. return ret;
  2461. }
  2462. static u64 stripe_align(struct btrfs_root *root, u64 val)
  2463. {
  2464. u64 mask = ((u64)root->stripesize - 1);
  2465. u64 ret = (val + mask) & ~mask;
  2466. return ret;
  2467. }
  2468. /*
  2469. * walks the btree of allocated extents and find a hole of a given size.
  2470. * The key ins is changed to record the hole:
  2471. * ins->objectid == block start
  2472. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  2473. * ins->offset == number of blocks
  2474. * Any available blocks before search_start are skipped.
  2475. */
  2476. static int noinline find_free_extent(struct btrfs_trans_handle *trans,
  2477. struct btrfs_root *orig_root,
  2478. u64 num_bytes, u64 empty_size,
  2479. u64 search_start, u64 search_end,
  2480. u64 hint_byte, struct btrfs_key *ins,
  2481. u64 exclude_start, u64 exclude_nr,
  2482. int data)
  2483. {
  2484. int ret = 0;
  2485. struct btrfs_root * root = orig_root->fs_info->extent_root;
  2486. u64 total_needed = num_bytes;
  2487. u64 *last_ptr = NULL;
  2488. u64 last_wanted = 0;
  2489. struct btrfs_block_group_cache *block_group = NULL;
  2490. int chunk_alloc_done = 0;
  2491. int empty_cluster = 2 * 1024 * 1024;
  2492. int allowed_chunk_alloc = 0;
  2493. struct list_head *head = NULL, *cur = NULL;
  2494. int loop = 0;
  2495. int extra_loop = 0;
  2496. struct btrfs_space_info *space_info;
  2497. WARN_ON(num_bytes < root->sectorsize);
  2498. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  2499. ins->objectid = 0;
  2500. ins->offset = 0;
  2501. if (orig_root->ref_cows || empty_size)
  2502. allowed_chunk_alloc = 1;
  2503. if (data & BTRFS_BLOCK_GROUP_METADATA) {
  2504. last_ptr = &root->fs_info->last_alloc;
  2505. empty_cluster = 64 * 1024;
  2506. }
  2507. if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
  2508. last_ptr = &root->fs_info->last_data_alloc;
  2509. if (last_ptr) {
  2510. if (*last_ptr) {
  2511. hint_byte = *last_ptr;
  2512. last_wanted = *last_ptr;
  2513. } else
  2514. empty_size += empty_cluster;
  2515. } else {
  2516. empty_cluster = 0;
  2517. }
  2518. search_start = max(search_start, first_logical_byte(root, 0));
  2519. search_start = max(search_start, hint_byte);
  2520. if (last_wanted && search_start != last_wanted) {
  2521. last_wanted = 0;
  2522. empty_size += empty_cluster;
  2523. }
  2524. total_needed += empty_size;
  2525. block_group = btrfs_lookup_block_group(root->fs_info, search_start);
  2526. if (!block_group)
  2527. block_group = btrfs_lookup_first_block_group(root->fs_info,
  2528. search_start);
  2529. space_info = __find_space_info(root->fs_info, data);
  2530. down_read(&space_info->groups_sem);
  2531. while (1) {
  2532. struct btrfs_free_space *free_space;
  2533. /*
  2534. * the only way this happens if our hint points to a block
  2535. * group thats not of the proper type, while looping this
  2536. * should never happen
  2537. */
  2538. if (empty_size)
  2539. extra_loop = 1;
  2540. if (!block_group)
  2541. goto new_group_no_lock;
  2542. if (unlikely(!block_group->cached)) {
  2543. mutex_lock(&block_group->cache_mutex);
  2544. ret = cache_block_group(root, block_group);
  2545. mutex_unlock(&block_group->cache_mutex);
  2546. if (ret)
  2547. break;
  2548. }
  2549. mutex_lock(&block_group->alloc_mutex);
  2550. if (unlikely(!block_group_bits(block_group, data)))
  2551. goto new_group;
  2552. if (unlikely(block_group->ro))
  2553. goto new_group;
  2554. free_space = btrfs_find_free_space(block_group, search_start,
  2555. total_needed);
  2556. if (free_space) {
  2557. u64 start = block_group->key.objectid;
  2558. u64 end = block_group->key.objectid +
  2559. block_group->key.offset;
  2560. search_start = stripe_align(root, free_space->offset);
  2561. /* move on to the next group */
  2562. if (search_start + num_bytes >= search_end)
  2563. goto new_group;
  2564. /* move on to the next group */
  2565. if (search_start + num_bytes > end)
  2566. goto new_group;
  2567. if (last_wanted && search_start != last_wanted) {
  2568. total_needed += empty_cluster;
  2569. empty_size += empty_cluster;
  2570. last_wanted = 0;
  2571. /*
  2572. * if search_start is still in this block group
  2573. * then we just re-search this block group
  2574. */
  2575. if (search_start >= start &&
  2576. search_start < end) {
  2577. mutex_unlock(&block_group->alloc_mutex);
  2578. continue;
  2579. }
  2580. /* else we go to the next block group */
  2581. goto new_group;
  2582. }
  2583. if (exclude_nr > 0 &&
  2584. (search_start + num_bytes > exclude_start &&
  2585. search_start < exclude_start + exclude_nr)) {
  2586. search_start = exclude_start + exclude_nr;
  2587. /*
  2588. * if search_start is still in this block group
  2589. * then we just re-search this block group
  2590. */
  2591. if (search_start >= start &&
  2592. search_start < end) {
  2593. mutex_unlock(&block_group->alloc_mutex);
  2594. last_wanted = 0;
  2595. continue;
  2596. }
  2597. /* else we go to the next block group */
  2598. goto new_group;
  2599. }
  2600. ins->objectid = search_start;
  2601. ins->offset = num_bytes;
  2602. btrfs_remove_free_space_lock(block_group, search_start,
  2603. num_bytes);
  2604. /* we are all good, lets return */
  2605. mutex_unlock(&block_group->alloc_mutex);
  2606. break;
  2607. }
  2608. new_group:
  2609. mutex_unlock(&block_group->alloc_mutex);
  2610. new_group_no_lock:
  2611. /* don't try to compare new allocations against the
  2612. * last allocation any more
  2613. */
  2614. last_wanted = 0;
  2615. /*
  2616. * Here's how this works.
  2617. * loop == 0: we were searching a block group via a hint
  2618. * and didn't find anything, so we start at
  2619. * the head of the block groups and keep searching
  2620. * loop == 1: we're searching through all of the block groups
  2621. * if we hit the head again we have searched
  2622. * all of the block groups for this space and we
  2623. * need to try and allocate, if we cant error out.
  2624. * loop == 2: we allocated more space and are looping through
  2625. * all of the block groups again.
  2626. */
  2627. if (loop == 0) {
  2628. head = &space_info->block_groups;
  2629. cur = head->next;
  2630. loop++;
  2631. } else if (loop == 1 && cur == head) {
  2632. int keep_going;
  2633. /* at this point we give up on the empty_size
  2634. * allocations and just try to allocate the min
  2635. * space.
  2636. *
  2637. * The extra_loop field was set if an empty_size
  2638. * allocation was attempted above, and if this
  2639. * is try we need to try the loop again without
  2640. * the additional empty_size.
  2641. */
  2642. total_needed -= empty_size;
  2643. empty_size = 0;
  2644. keep_going = extra_loop;
  2645. loop++;
  2646. if (allowed_chunk_alloc && !chunk_alloc_done) {
  2647. up_read(&space_info->groups_sem);
  2648. ret = do_chunk_alloc(trans, root, num_bytes +
  2649. 2 * 1024 * 1024, data, 1);
  2650. down_read(&space_info->groups_sem);
  2651. if (ret < 0)
  2652. goto loop_check;
  2653. head = &space_info->block_groups;
  2654. /*
  2655. * we've allocated a new chunk, keep
  2656. * trying
  2657. */
  2658. keep_going = 1;
  2659. chunk_alloc_done = 1;
  2660. } else if (!allowed_chunk_alloc) {
  2661. space_info->force_alloc = 1;
  2662. }
  2663. loop_check:
  2664. if (keep_going) {
  2665. cur = head->next;
  2666. extra_loop = 0;
  2667. } else {
  2668. break;
  2669. }
  2670. } else if (cur == head) {
  2671. break;
  2672. }
  2673. block_group = list_entry(cur, struct btrfs_block_group_cache,
  2674. list);
  2675. search_start = block_group->key.objectid;
  2676. cur = cur->next;
  2677. }
  2678. /* we found what we needed */
  2679. if (ins->objectid) {
  2680. if (!(data & BTRFS_BLOCK_GROUP_DATA))
  2681. trans->block_group = block_group;
  2682. if (last_ptr)
  2683. *last_ptr = ins->objectid + ins->offset;
  2684. ret = 0;
  2685. } else if (!ret) {
  2686. printk(KERN_ERR "we were searching for %Lu bytes, num_bytes %Lu,"
  2687. " loop %d, allowed_alloc %d\n", total_needed, num_bytes,
  2688. loop, allowed_chunk_alloc);
  2689. ret = -ENOSPC;
  2690. }
  2691. up_read(&space_info->groups_sem);
  2692. return ret;
  2693. }
  2694. static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
  2695. {
  2696. struct btrfs_block_group_cache *cache;
  2697. struct list_head *l;
  2698. printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
  2699. info->total_bytes - info->bytes_used - info->bytes_pinned -
  2700. info->bytes_reserved, (info->full) ? "" : "not ");
  2701. down_read(&info->groups_sem);
  2702. list_for_each(l, &info->block_groups) {
  2703. cache = list_entry(l, struct btrfs_block_group_cache, list);
  2704. spin_lock(&cache->lock);
  2705. printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
  2706. "%Lu pinned %Lu reserved\n",
  2707. cache->key.objectid, cache->key.offset,
  2708. btrfs_block_group_used(&cache->item),
  2709. cache->pinned, cache->reserved);
  2710. btrfs_dump_free_space(cache, bytes);
  2711. spin_unlock(&cache->lock);
  2712. }
  2713. up_read(&info->groups_sem);
  2714. }
  2715. static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2716. struct btrfs_root *root,
  2717. u64 num_bytes, u64 min_alloc_size,
  2718. u64 empty_size, u64 hint_byte,
  2719. u64 search_end, struct btrfs_key *ins,
  2720. u64 data)
  2721. {
  2722. int ret;
  2723. u64 search_start = 0;
  2724. u64 alloc_profile;
  2725. struct btrfs_fs_info *info = root->fs_info;
  2726. if (data) {
  2727. alloc_profile = info->avail_data_alloc_bits &
  2728. info->data_alloc_profile;
  2729. data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
  2730. } else if (root == root->fs_info->chunk_root) {
  2731. alloc_profile = info->avail_system_alloc_bits &
  2732. info->system_alloc_profile;
  2733. data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
  2734. } else {
  2735. alloc_profile = info->avail_metadata_alloc_bits &
  2736. info->metadata_alloc_profile;
  2737. data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
  2738. }
  2739. again:
  2740. data = btrfs_reduce_alloc_profile(root, data);
  2741. /*
  2742. * the only place that sets empty_size is btrfs_realloc_node, which
  2743. * is not called recursively on allocations
  2744. */
  2745. if (empty_size || root->ref_cows) {
  2746. if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
  2747. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2748. 2 * 1024 * 1024,
  2749. BTRFS_BLOCK_GROUP_METADATA |
  2750. (info->metadata_alloc_profile &
  2751. info->avail_metadata_alloc_bits), 0);
  2752. }
  2753. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2754. num_bytes + 2 * 1024 * 1024, data, 0);
  2755. }
  2756. WARN_ON(num_bytes < root->sectorsize);
  2757. ret = find_free_extent(trans, root, num_bytes, empty_size,
  2758. search_start, search_end, hint_byte, ins,
  2759. trans->alloc_exclude_start,
  2760. trans->alloc_exclude_nr, data);
  2761. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  2762. num_bytes = num_bytes >> 1;
  2763. num_bytes = num_bytes & ~(root->sectorsize - 1);
  2764. num_bytes = max(num_bytes, min_alloc_size);
  2765. do_chunk_alloc(trans, root->fs_info->extent_root,
  2766. num_bytes, data, 1);
  2767. goto again;
  2768. }
  2769. if (ret) {
  2770. struct btrfs_space_info *sinfo;
  2771. sinfo = __find_space_info(root->fs_info, data);
  2772. printk("allocation failed flags %Lu, wanted %Lu\n",
  2773. data, num_bytes);
  2774. dump_space_info(sinfo, num_bytes);
  2775. BUG();
  2776. }
  2777. return ret;
  2778. }
  2779. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  2780. {
  2781. struct btrfs_block_group_cache *cache;
  2782. cache = btrfs_lookup_block_group(root->fs_info, start);
  2783. if (!cache) {
  2784. printk(KERN_ERR "Unable to find block group for %Lu\n", start);
  2785. return -ENOSPC;
  2786. }
  2787. btrfs_add_free_space(cache, start, len);
  2788. update_reserved_extents(root, start, len, 0);
  2789. return 0;
  2790. }
  2791. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2792. struct btrfs_root *root,
  2793. u64 num_bytes, u64 min_alloc_size,
  2794. u64 empty_size, u64 hint_byte,
  2795. u64 search_end, struct btrfs_key *ins,
  2796. u64 data)
  2797. {
  2798. int ret;
  2799. ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
  2800. empty_size, hint_byte, search_end, ins,
  2801. data);
  2802. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  2803. return ret;
  2804. }
  2805. static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2806. struct btrfs_root *root, u64 parent,
  2807. u64 root_objectid, u64 ref_generation,
  2808. u64 owner, struct btrfs_key *ins)
  2809. {
  2810. int ret;
  2811. int pending_ret;
  2812. u64 super_used;
  2813. u64 root_used;
  2814. u64 num_bytes = ins->offset;
  2815. u32 sizes[2];
  2816. struct btrfs_fs_info *info = root->fs_info;
  2817. struct btrfs_root *extent_root = info->extent_root;
  2818. struct btrfs_extent_item *extent_item;
  2819. struct btrfs_extent_ref *ref;
  2820. struct btrfs_path *path;
  2821. struct btrfs_key keys[2];
  2822. if (parent == 0)
  2823. parent = ins->objectid;
  2824. /* block accounting for super block */
  2825. spin_lock_irq(&info->delalloc_lock);
  2826. super_used = btrfs_super_bytes_used(&info->super_copy);
  2827. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  2828. spin_unlock_irq(&info->delalloc_lock);
  2829. /* block accounting for root item */
  2830. root_used = btrfs_root_used(&root->root_item);
  2831. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  2832. if (root == extent_root) {
  2833. struct pending_extent_op *extent_op;
  2834. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  2835. BUG_ON(!extent_op);
  2836. extent_op->type = PENDING_EXTENT_INSERT;
  2837. extent_op->bytenr = ins->objectid;
  2838. extent_op->num_bytes = ins->offset;
  2839. extent_op->parent = parent;
  2840. extent_op->orig_parent = 0;
  2841. extent_op->generation = ref_generation;
  2842. extent_op->orig_generation = 0;
  2843. extent_op->level = (int)owner;
  2844. INIT_LIST_HEAD(&extent_op->list);
  2845. extent_op->del = 0;
  2846. mutex_lock(&root->fs_info->extent_ins_mutex);
  2847. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  2848. ins->objectid + ins->offset - 1,
  2849. EXTENT_WRITEBACK, GFP_NOFS);
  2850. set_state_private(&root->fs_info->extent_ins,
  2851. ins->objectid, (unsigned long)extent_op);
  2852. mutex_unlock(&root->fs_info->extent_ins_mutex);
  2853. goto update_block;
  2854. }
  2855. memcpy(&keys[0], ins, sizeof(*ins));
  2856. keys[1].objectid = ins->objectid;
  2857. keys[1].type = BTRFS_EXTENT_REF_KEY;
  2858. keys[1].offset = parent;
  2859. sizes[0] = sizeof(*extent_item);
  2860. sizes[1] = sizeof(*ref);
  2861. path = btrfs_alloc_path();
  2862. BUG_ON(!path);
  2863. ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
  2864. sizes, 2);
  2865. BUG_ON(ret);
  2866. extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2867. struct btrfs_extent_item);
  2868. btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
  2869. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  2870. struct btrfs_extent_ref);
  2871. btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
  2872. btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
  2873. btrfs_set_ref_objectid(path->nodes[0], ref, owner);
  2874. btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
  2875. btrfs_mark_buffer_dirty(path->nodes[0]);
  2876. trans->alloc_exclude_start = 0;
  2877. trans->alloc_exclude_nr = 0;
  2878. btrfs_free_path(path);
  2879. finish_current_insert(trans, extent_root, 0);
  2880. pending_ret = del_pending_extents(trans, extent_root, 0);
  2881. if (ret)
  2882. goto out;
  2883. if (pending_ret) {
  2884. ret = pending_ret;
  2885. goto out;
  2886. }
  2887. update_block:
  2888. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
  2889. if (ret) {
  2890. printk("update block group failed for %Lu %Lu\n",
  2891. ins->objectid, ins->offset);
  2892. BUG();
  2893. }
  2894. out:
  2895. return ret;
  2896. }
  2897. int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2898. struct btrfs_root *root, u64 parent,
  2899. u64 root_objectid, u64 ref_generation,
  2900. u64 owner, struct btrfs_key *ins)
  2901. {
  2902. int ret;
  2903. if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
  2904. return 0;
  2905. ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
  2906. ref_generation, owner, ins);
  2907. update_reserved_extents(root, ins->objectid, ins->offset, 0);
  2908. return ret;
  2909. }
  2910. /*
  2911. * this is used by the tree logging recovery code. It records that
  2912. * an extent has been allocated and makes sure to clear the free
  2913. * space cache bits as well
  2914. */
  2915. int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
  2916. struct btrfs_root *root, u64 parent,
  2917. u64 root_objectid, u64 ref_generation,
  2918. u64 owner, struct btrfs_key *ins)
  2919. {
  2920. int ret;
  2921. struct btrfs_block_group_cache *block_group;
  2922. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  2923. mutex_lock(&block_group->cache_mutex);
  2924. cache_block_group(root, block_group);
  2925. mutex_unlock(&block_group->cache_mutex);
  2926. ret = btrfs_remove_free_space(block_group, ins->objectid,
  2927. ins->offset);
  2928. BUG_ON(ret);
  2929. ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
  2930. ref_generation, owner, ins);
  2931. return ret;
  2932. }
  2933. /*
  2934. * finds a free extent and does all the dirty work required for allocation
  2935. * returns the key for the extent through ins, and a tree buffer for
  2936. * the first block of the extent through buf.
  2937. *
  2938. * returns 0 if everything worked, non-zero otherwise.
  2939. */
  2940. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  2941. struct btrfs_root *root,
  2942. u64 num_bytes, u64 parent, u64 min_alloc_size,
  2943. u64 root_objectid, u64 ref_generation,
  2944. u64 owner_objectid, u64 empty_size, u64 hint_byte,
  2945. u64 search_end, struct btrfs_key *ins, u64 data)
  2946. {
  2947. int ret;
  2948. ret = __btrfs_reserve_extent(trans, root, num_bytes,
  2949. min_alloc_size, empty_size, hint_byte,
  2950. search_end, ins, data);
  2951. BUG_ON(ret);
  2952. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  2953. ret = __btrfs_alloc_reserved_extent(trans, root, parent,
  2954. root_objectid, ref_generation,
  2955. owner_objectid, ins);
  2956. BUG_ON(ret);
  2957. } else {
  2958. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  2959. }
  2960. return ret;
  2961. }
  2962. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  2963. struct btrfs_root *root,
  2964. u64 bytenr, u32 blocksize)
  2965. {
  2966. struct extent_buffer *buf;
  2967. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  2968. if (!buf)
  2969. return ERR_PTR(-ENOMEM);
  2970. btrfs_set_header_generation(buf, trans->transid);
  2971. btrfs_tree_lock(buf);
  2972. clean_tree_block(trans, root, buf);
  2973. btrfs_set_buffer_uptodate(buf);
  2974. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  2975. set_extent_dirty(&root->dirty_log_pages, buf->start,
  2976. buf->start + buf->len - 1, GFP_NOFS);
  2977. } else {
  2978. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  2979. buf->start + buf->len - 1, GFP_NOFS);
  2980. }
  2981. trans->blocks_used++;
  2982. return buf;
  2983. }
  2984. /*
  2985. * helper function to allocate a block for a given tree
  2986. * returns the tree buffer or NULL.
  2987. */
  2988. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  2989. struct btrfs_root *root,
  2990. u32 blocksize, u64 parent,
  2991. u64 root_objectid,
  2992. u64 ref_generation,
  2993. int level,
  2994. u64 hint,
  2995. u64 empty_size)
  2996. {
  2997. struct btrfs_key ins;
  2998. int ret;
  2999. struct extent_buffer *buf;
  3000. ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
  3001. root_objectid, ref_generation, level,
  3002. empty_size, hint, (u64)-1, &ins, 0);
  3003. if (ret) {
  3004. BUG_ON(ret > 0);
  3005. return ERR_PTR(ret);
  3006. }
  3007. buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
  3008. return buf;
  3009. }
  3010. int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
  3011. struct btrfs_root *root, struct extent_buffer *leaf)
  3012. {
  3013. u64 leaf_owner;
  3014. u64 leaf_generation;
  3015. struct btrfs_key key;
  3016. struct btrfs_file_extent_item *fi;
  3017. int i;
  3018. int nritems;
  3019. int ret;
  3020. BUG_ON(!btrfs_is_leaf(leaf));
  3021. nritems = btrfs_header_nritems(leaf);
  3022. leaf_owner = btrfs_header_owner(leaf);
  3023. leaf_generation = btrfs_header_generation(leaf);
  3024. for (i = 0; i < nritems; i++) {
  3025. u64 disk_bytenr;
  3026. cond_resched();
  3027. btrfs_item_key_to_cpu(leaf, &key, i);
  3028. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  3029. continue;
  3030. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  3031. if (btrfs_file_extent_type(leaf, fi) ==
  3032. BTRFS_FILE_EXTENT_INLINE)
  3033. continue;
  3034. /*
  3035. * FIXME make sure to insert a trans record that
  3036. * repeats the snapshot del on crash
  3037. */
  3038. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  3039. if (disk_bytenr == 0)
  3040. continue;
  3041. ret = __btrfs_free_extent(trans, root, disk_bytenr,
  3042. btrfs_file_extent_disk_num_bytes(leaf, fi),
  3043. leaf->start, leaf_owner, leaf_generation,
  3044. key.objectid, 0);
  3045. BUG_ON(ret);
  3046. atomic_inc(&root->fs_info->throttle_gen);
  3047. wake_up(&root->fs_info->transaction_throttle);
  3048. cond_resched();
  3049. }
  3050. return 0;
  3051. }
  3052. static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
  3053. struct btrfs_root *root,
  3054. struct btrfs_leaf_ref *ref)
  3055. {
  3056. int i;
  3057. int ret;
  3058. struct btrfs_extent_info *info = ref->extents;
  3059. for (i = 0; i < ref->nritems; i++) {
  3060. ret = __btrfs_free_extent(trans, root, info->bytenr,
  3061. info->num_bytes, ref->bytenr,
  3062. ref->owner, ref->generation,
  3063. info->objectid, 0);
  3064. atomic_inc(&root->fs_info->throttle_gen);
  3065. wake_up(&root->fs_info->transaction_throttle);
  3066. cond_resched();
  3067. BUG_ON(ret);
  3068. info++;
  3069. }
  3070. return 0;
  3071. }
  3072. static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
  3073. u32 *refs)
  3074. {
  3075. int ret;
  3076. ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
  3077. BUG_ON(ret);
  3078. #if 0 // some debugging code in case we see problems here
  3079. /* if the refs count is one, it won't get increased again. But
  3080. * if the ref count is > 1, someone may be decreasing it at
  3081. * the same time we are.
  3082. */
  3083. if (*refs != 1) {
  3084. struct extent_buffer *eb = NULL;
  3085. eb = btrfs_find_create_tree_block(root, start, len);
  3086. if (eb)
  3087. btrfs_tree_lock(eb);
  3088. mutex_lock(&root->fs_info->alloc_mutex);
  3089. ret = lookup_extent_ref(NULL, root, start, len, refs);
  3090. BUG_ON(ret);
  3091. mutex_unlock(&root->fs_info->alloc_mutex);
  3092. if (eb) {
  3093. btrfs_tree_unlock(eb);
  3094. free_extent_buffer(eb);
  3095. }
  3096. if (*refs == 1) {
  3097. printk("block %llu went down to one during drop_snap\n",
  3098. (unsigned long long)start);
  3099. }
  3100. }
  3101. #endif
  3102. cond_resched();
  3103. return ret;
  3104. }
  3105. /*
  3106. * helper function for drop_snapshot, this walks down the tree dropping ref
  3107. * counts as it goes.
  3108. */
  3109. static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
  3110. struct btrfs_root *root,
  3111. struct btrfs_path *path, int *level)
  3112. {
  3113. u64 root_owner;
  3114. u64 root_gen;
  3115. u64 bytenr;
  3116. u64 ptr_gen;
  3117. struct extent_buffer *next;
  3118. struct extent_buffer *cur;
  3119. struct extent_buffer *parent;
  3120. struct btrfs_leaf_ref *ref;
  3121. u32 blocksize;
  3122. int ret;
  3123. u32 refs;
  3124. WARN_ON(*level < 0);
  3125. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3126. ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
  3127. path->nodes[*level]->len, &refs);
  3128. BUG_ON(ret);
  3129. if (refs > 1)
  3130. goto out;
  3131. /*
  3132. * walk down to the last node level and free all the leaves
  3133. */
  3134. while(*level >= 0) {
  3135. WARN_ON(*level < 0);
  3136. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3137. cur = path->nodes[*level];
  3138. if (btrfs_header_level(cur) != *level)
  3139. WARN_ON(1);
  3140. if (path->slots[*level] >=
  3141. btrfs_header_nritems(cur))
  3142. break;
  3143. if (*level == 0) {
  3144. ret = btrfs_drop_leaf_ref(trans, root, cur);
  3145. BUG_ON(ret);
  3146. break;
  3147. }
  3148. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  3149. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  3150. blocksize = btrfs_level_size(root, *level - 1);
  3151. ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
  3152. BUG_ON(ret);
  3153. if (refs != 1) {
  3154. parent = path->nodes[*level];
  3155. root_owner = btrfs_header_owner(parent);
  3156. root_gen = btrfs_header_generation(parent);
  3157. path->slots[*level]++;
  3158. ret = __btrfs_free_extent(trans, root, bytenr,
  3159. blocksize, parent->start,
  3160. root_owner, root_gen,
  3161. *level - 1, 1);
  3162. BUG_ON(ret);
  3163. atomic_inc(&root->fs_info->throttle_gen);
  3164. wake_up(&root->fs_info->transaction_throttle);
  3165. cond_resched();
  3166. continue;
  3167. }
  3168. /*
  3169. * at this point, we have a single ref, and since the
  3170. * only place referencing this extent is a dead root
  3171. * the reference count should never go higher.
  3172. * So, we don't need to check it again
  3173. */
  3174. if (*level == 1) {
  3175. ref = btrfs_lookup_leaf_ref(root, bytenr);
  3176. if (ref && ref->generation != ptr_gen) {
  3177. btrfs_free_leaf_ref(root, ref);
  3178. ref = NULL;
  3179. }
  3180. if (ref) {
  3181. ret = cache_drop_leaf_ref(trans, root, ref);
  3182. BUG_ON(ret);
  3183. btrfs_remove_leaf_ref(root, ref);
  3184. btrfs_free_leaf_ref(root, ref);
  3185. *level = 0;
  3186. break;
  3187. }
  3188. if (printk_ratelimit()) {
  3189. printk("leaf ref miss for bytenr %llu\n",
  3190. (unsigned long long)bytenr);
  3191. }
  3192. }
  3193. next = btrfs_find_tree_block(root, bytenr, blocksize);
  3194. if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
  3195. free_extent_buffer(next);
  3196. next = read_tree_block(root, bytenr, blocksize,
  3197. ptr_gen);
  3198. cond_resched();
  3199. #if 0
  3200. /*
  3201. * this is a debugging check and can go away
  3202. * the ref should never go all the way down to 1
  3203. * at this point
  3204. */
  3205. ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
  3206. &refs);
  3207. BUG_ON(ret);
  3208. WARN_ON(refs != 1);
  3209. #endif
  3210. }
  3211. WARN_ON(*level <= 0);
  3212. if (path->nodes[*level-1])
  3213. free_extent_buffer(path->nodes[*level-1]);
  3214. path->nodes[*level-1] = next;
  3215. *level = btrfs_header_level(next);
  3216. path->slots[*level] = 0;
  3217. cond_resched();
  3218. }
  3219. out:
  3220. WARN_ON(*level < 0);
  3221. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  3222. if (path->nodes[*level] == root->node) {
  3223. parent = path->nodes[*level];
  3224. bytenr = path->nodes[*level]->start;
  3225. } else {
  3226. parent = path->nodes[*level + 1];
  3227. bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
  3228. }
  3229. blocksize = btrfs_level_size(root, *level);
  3230. root_owner = btrfs_header_owner(parent);
  3231. root_gen = btrfs_header_generation(parent);
  3232. ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
  3233. parent->start, root_owner, root_gen,
  3234. *level, 1);
  3235. free_extent_buffer(path->nodes[*level]);
  3236. path->nodes[*level] = NULL;
  3237. *level += 1;
  3238. BUG_ON(ret);
  3239. cond_resched();
  3240. return 0;
  3241. }
  3242. /*
  3243. * helper function for drop_subtree, this function is similar to
  3244. * walk_down_tree. The main difference is that it checks reference
  3245. * counts while tree blocks are locked.
  3246. */
  3247. static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
  3248. struct btrfs_root *root,
  3249. struct btrfs_path *path, int *level)
  3250. {
  3251. struct extent_buffer *next;
  3252. struct extent_buffer *cur;
  3253. struct extent_buffer *parent;
  3254. u64 bytenr;
  3255. u64 ptr_gen;
  3256. u32 blocksize;
  3257. u32 refs;
  3258. int ret;
  3259. cur = path->nodes[*level];
  3260. ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
  3261. &refs);
  3262. BUG_ON(ret);
  3263. if (refs > 1)
  3264. goto out;
  3265. while (*level >= 0) {
  3266. cur = path->nodes[*level];
  3267. if (*level == 0) {
  3268. ret = btrfs_drop_leaf_ref(trans, root, cur);
  3269. BUG_ON(ret);
  3270. clean_tree_block(trans, root, cur);
  3271. break;
  3272. }
  3273. if (path->slots[*level] >= btrfs_header_nritems(cur)) {
  3274. clean_tree_block(trans, root, cur);
  3275. break;
  3276. }
  3277. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  3278. blocksize = btrfs_level_size(root, *level - 1);
  3279. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  3280. next = read_tree_block(root, bytenr, blocksize, ptr_gen);
  3281. btrfs_tree_lock(next);
  3282. ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
  3283. &refs);
  3284. BUG_ON(ret);
  3285. if (refs > 1) {
  3286. parent = path->nodes[*level];
  3287. ret = btrfs_free_extent(trans, root, bytenr,
  3288. blocksize, parent->start,
  3289. btrfs_header_owner(parent),
  3290. btrfs_header_generation(parent),
  3291. *level - 1, 1);
  3292. BUG_ON(ret);
  3293. path->slots[*level]++;
  3294. btrfs_tree_unlock(next);
  3295. free_extent_buffer(next);
  3296. continue;
  3297. }
  3298. *level = btrfs_header_level(next);
  3299. path->nodes[*level] = next;
  3300. path->slots[*level] = 0;
  3301. path->locks[*level] = 1;
  3302. cond_resched();
  3303. }
  3304. out:
  3305. parent = path->nodes[*level + 1];
  3306. bytenr = path->nodes[*level]->start;
  3307. blocksize = path->nodes[*level]->len;
  3308. ret = btrfs_free_extent(trans, root, bytenr, blocksize,
  3309. parent->start, btrfs_header_owner(parent),
  3310. btrfs_header_generation(parent), *level, 1);
  3311. BUG_ON(ret);
  3312. if (path->locks[*level]) {
  3313. btrfs_tree_unlock(path->nodes[*level]);
  3314. path->locks[*level] = 0;
  3315. }
  3316. free_extent_buffer(path->nodes[*level]);
  3317. path->nodes[*level] = NULL;
  3318. *level += 1;
  3319. cond_resched();
  3320. return 0;
  3321. }
  3322. /*
  3323. * helper for dropping snapshots. This walks back up the tree in the path
  3324. * to find the first node higher up where we haven't yet gone through
  3325. * all the slots
  3326. */
  3327. static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
  3328. struct btrfs_root *root,
  3329. struct btrfs_path *path,
  3330. int *level, int max_level)
  3331. {
  3332. u64 root_owner;
  3333. u64 root_gen;
  3334. struct btrfs_root_item *root_item = &root->root_item;
  3335. int i;
  3336. int slot;
  3337. int ret;
  3338. for (i = *level; i < max_level && path->nodes[i]; i++) {
  3339. slot = path->slots[i];
  3340. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  3341. struct extent_buffer *node;
  3342. struct btrfs_disk_key disk_key;
  3343. node = path->nodes[i];
  3344. path->slots[i]++;
  3345. *level = i;
  3346. WARN_ON(*level == 0);
  3347. btrfs_node_key(node, &disk_key, path->slots[i]);
  3348. memcpy(&root_item->drop_progress,
  3349. &disk_key, sizeof(disk_key));
  3350. root_item->drop_level = i;
  3351. return 0;
  3352. } else {
  3353. struct extent_buffer *parent;
  3354. if (path->nodes[*level] == root->node)
  3355. parent = path->nodes[*level];
  3356. else
  3357. parent = path->nodes[*level + 1];
  3358. root_owner = btrfs_header_owner(parent);
  3359. root_gen = btrfs_header_generation(parent);
  3360. clean_tree_block(trans, root, path->nodes[*level]);
  3361. ret = btrfs_free_extent(trans, root,
  3362. path->nodes[*level]->start,
  3363. path->nodes[*level]->len,
  3364. parent->start, root_owner,
  3365. root_gen, *level, 1);
  3366. BUG_ON(ret);
  3367. if (path->locks[*level]) {
  3368. btrfs_tree_unlock(path->nodes[*level]);
  3369. path->locks[*level] = 0;
  3370. }
  3371. free_extent_buffer(path->nodes[*level]);
  3372. path->nodes[*level] = NULL;
  3373. *level = i + 1;
  3374. }
  3375. }
  3376. return 1;
  3377. }
  3378. /*
  3379. * drop the reference count on the tree rooted at 'snap'. This traverses
  3380. * the tree freeing any blocks that have a ref count of zero after being
  3381. * decremented.
  3382. */
  3383. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  3384. *root)
  3385. {
  3386. int ret = 0;
  3387. int wret;
  3388. int level;
  3389. struct btrfs_path *path;
  3390. int i;
  3391. int orig_level;
  3392. struct btrfs_root_item *root_item = &root->root_item;
  3393. WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
  3394. path = btrfs_alloc_path();
  3395. BUG_ON(!path);
  3396. level = btrfs_header_level(root->node);
  3397. orig_level = level;
  3398. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  3399. path->nodes[level] = root->node;
  3400. extent_buffer_get(root->node);
  3401. path->slots[level] = 0;
  3402. } else {
  3403. struct btrfs_key key;
  3404. struct btrfs_disk_key found_key;
  3405. struct extent_buffer *node;
  3406. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  3407. level = root_item->drop_level;
  3408. path->lowest_level = level;
  3409. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3410. if (wret < 0) {
  3411. ret = wret;
  3412. goto out;
  3413. }
  3414. node = path->nodes[level];
  3415. btrfs_node_key(node, &found_key, path->slots[level]);
  3416. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  3417. sizeof(found_key)));
  3418. /*
  3419. * unlock our path, this is safe because only this
  3420. * function is allowed to delete this snapshot
  3421. */
  3422. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  3423. if (path->nodes[i] && path->locks[i]) {
  3424. path->locks[i] = 0;
  3425. btrfs_tree_unlock(path->nodes[i]);
  3426. }
  3427. }
  3428. }
  3429. while(1) {
  3430. wret = walk_down_tree(trans, root, path, &level);
  3431. if (wret > 0)
  3432. break;
  3433. if (wret < 0)
  3434. ret = wret;
  3435. wret = walk_up_tree(trans, root, path, &level,
  3436. BTRFS_MAX_LEVEL);
  3437. if (wret > 0)
  3438. break;
  3439. if (wret < 0)
  3440. ret = wret;
  3441. if (trans->transaction->in_commit) {
  3442. ret = -EAGAIN;
  3443. break;
  3444. }
  3445. atomic_inc(&root->fs_info->throttle_gen);
  3446. wake_up(&root->fs_info->transaction_throttle);
  3447. }
  3448. for (i = 0; i <= orig_level; i++) {
  3449. if (path->nodes[i]) {
  3450. free_extent_buffer(path->nodes[i]);
  3451. path->nodes[i] = NULL;
  3452. }
  3453. }
  3454. out:
  3455. btrfs_free_path(path);
  3456. return ret;
  3457. }
  3458. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  3459. struct btrfs_root *root,
  3460. struct extent_buffer *node,
  3461. struct extent_buffer *parent)
  3462. {
  3463. struct btrfs_path *path;
  3464. int level;
  3465. int parent_level;
  3466. int ret = 0;
  3467. int wret;
  3468. path = btrfs_alloc_path();
  3469. BUG_ON(!path);
  3470. BUG_ON(!btrfs_tree_locked(parent));
  3471. parent_level = btrfs_header_level(parent);
  3472. extent_buffer_get(parent);
  3473. path->nodes[parent_level] = parent;
  3474. path->slots[parent_level] = btrfs_header_nritems(parent);
  3475. BUG_ON(!btrfs_tree_locked(node));
  3476. level = btrfs_header_level(node);
  3477. extent_buffer_get(node);
  3478. path->nodes[level] = node;
  3479. path->slots[level] = 0;
  3480. while (1) {
  3481. wret = walk_down_subtree(trans, root, path, &level);
  3482. if (wret < 0)
  3483. ret = wret;
  3484. if (wret != 0)
  3485. break;
  3486. wret = walk_up_tree(trans, root, path, &level, parent_level);
  3487. if (wret < 0)
  3488. ret = wret;
  3489. if (wret != 0)
  3490. break;
  3491. }
  3492. btrfs_free_path(path);
  3493. return ret;
  3494. }
  3495. static unsigned long calc_ra(unsigned long start, unsigned long last,
  3496. unsigned long nr)
  3497. {
  3498. return min(last, start + nr - 1);
  3499. }
  3500. static int noinline relocate_inode_pages(struct inode *inode, u64 start,
  3501. u64 len)
  3502. {
  3503. u64 page_start;
  3504. u64 page_end;
  3505. unsigned long first_index;
  3506. unsigned long last_index;
  3507. unsigned long i;
  3508. struct page *page;
  3509. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3510. struct file_ra_state *ra;
  3511. struct btrfs_ordered_extent *ordered;
  3512. unsigned int total_read = 0;
  3513. unsigned int total_dirty = 0;
  3514. int ret = 0;
  3515. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  3516. mutex_lock(&inode->i_mutex);
  3517. first_index = start >> PAGE_CACHE_SHIFT;
  3518. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  3519. /* make sure the dirty trick played by the caller work */
  3520. ret = invalidate_inode_pages2_range(inode->i_mapping,
  3521. first_index, last_index);
  3522. if (ret)
  3523. goto out_unlock;
  3524. file_ra_state_init(ra, inode->i_mapping);
  3525. for (i = first_index ; i <= last_index; i++) {
  3526. if (total_read % ra->ra_pages == 0) {
  3527. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  3528. calc_ra(i, last_index, ra->ra_pages));
  3529. }
  3530. total_read++;
  3531. again:
  3532. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  3533. BUG_ON(1);
  3534. page = grab_cache_page(inode->i_mapping, i);
  3535. if (!page) {
  3536. ret = -ENOMEM;
  3537. goto out_unlock;
  3538. }
  3539. if (!PageUptodate(page)) {
  3540. btrfs_readpage(NULL, page);
  3541. lock_page(page);
  3542. if (!PageUptodate(page)) {
  3543. unlock_page(page);
  3544. page_cache_release(page);
  3545. ret = -EIO;
  3546. goto out_unlock;
  3547. }
  3548. }
  3549. wait_on_page_writeback(page);
  3550. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  3551. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3552. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3553. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3554. if (ordered) {
  3555. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3556. unlock_page(page);
  3557. page_cache_release(page);
  3558. btrfs_start_ordered_extent(inode, ordered, 1);
  3559. btrfs_put_ordered_extent(ordered);
  3560. goto again;
  3561. }
  3562. set_page_extent_mapped(page);
  3563. btrfs_set_extent_delalloc(inode, page_start, page_end);
  3564. if (i == first_index)
  3565. set_extent_bits(io_tree, page_start, page_end,
  3566. EXTENT_BOUNDARY, GFP_NOFS);
  3567. set_page_dirty(page);
  3568. total_dirty++;
  3569. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3570. unlock_page(page);
  3571. page_cache_release(page);
  3572. }
  3573. out_unlock:
  3574. kfree(ra);
  3575. mutex_unlock(&inode->i_mutex);
  3576. balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
  3577. return ret;
  3578. }
  3579. static int noinline relocate_data_extent(struct inode *reloc_inode,
  3580. struct btrfs_key *extent_key,
  3581. u64 offset)
  3582. {
  3583. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  3584. struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
  3585. struct extent_map *em;
  3586. u64 start = extent_key->objectid - offset;
  3587. u64 end = start + extent_key->offset - 1;
  3588. em = alloc_extent_map(GFP_NOFS);
  3589. BUG_ON(!em || IS_ERR(em));
  3590. em->start = start;
  3591. em->len = extent_key->offset;
  3592. em->block_len = extent_key->offset;
  3593. em->block_start = extent_key->objectid;
  3594. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3595. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  3596. /* setup extent map to cheat btrfs_readpage */
  3597. lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  3598. while (1) {
  3599. int ret;
  3600. spin_lock(&em_tree->lock);
  3601. ret = add_extent_mapping(em_tree, em);
  3602. spin_unlock(&em_tree->lock);
  3603. if (ret != -EEXIST) {
  3604. free_extent_map(em);
  3605. break;
  3606. }
  3607. btrfs_drop_extent_cache(reloc_inode, start, end, 0);
  3608. }
  3609. unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  3610. return relocate_inode_pages(reloc_inode, start, extent_key->offset);
  3611. }
  3612. struct btrfs_ref_path {
  3613. u64 extent_start;
  3614. u64 nodes[BTRFS_MAX_LEVEL];
  3615. u64 root_objectid;
  3616. u64 root_generation;
  3617. u64 owner_objectid;
  3618. u32 num_refs;
  3619. int lowest_level;
  3620. int current_level;
  3621. int shared_level;
  3622. struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
  3623. u64 new_nodes[BTRFS_MAX_LEVEL];
  3624. };
  3625. struct disk_extent {
  3626. u64 ram_bytes;
  3627. u64 disk_bytenr;
  3628. u64 disk_num_bytes;
  3629. u64 offset;
  3630. u64 num_bytes;
  3631. u8 compression;
  3632. u8 encryption;
  3633. u16 other_encoding;
  3634. };
  3635. static int is_cowonly_root(u64 root_objectid)
  3636. {
  3637. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
  3638. root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
  3639. root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
  3640. root_objectid == BTRFS_DEV_TREE_OBJECTID ||
  3641. root_objectid == BTRFS_TREE_LOG_OBJECTID)
  3642. return 1;
  3643. return 0;
  3644. }
  3645. static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
  3646. struct btrfs_root *extent_root,
  3647. struct btrfs_ref_path *ref_path,
  3648. int first_time)
  3649. {
  3650. struct extent_buffer *leaf;
  3651. struct btrfs_path *path;
  3652. struct btrfs_extent_ref *ref;
  3653. struct btrfs_key key;
  3654. struct btrfs_key found_key;
  3655. u64 bytenr;
  3656. u32 nritems;
  3657. int level;
  3658. int ret = 1;
  3659. path = btrfs_alloc_path();
  3660. if (!path)
  3661. return -ENOMEM;
  3662. if (first_time) {
  3663. ref_path->lowest_level = -1;
  3664. ref_path->current_level = -1;
  3665. ref_path->shared_level = -1;
  3666. goto walk_up;
  3667. }
  3668. walk_down:
  3669. level = ref_path->current_level - 1;
  3670. while (level >= -1) {
  3671. u64 parent;
  3672. if (level < ref_path->lowest_level)
  3673. break;
  3674. if (level >= 0) {
  3675. bytenr = ref_path->nodes[level];
  3676. } else {
  3677. bytenr = ref_path->extent_start;
  3678. }
  3679. BUG_ON(bytenr == 0);
  3680. parent = ref_path->nodes[level + 1];
  3681. ref_path->nodes[level + 1] = 0;
  3682. ref_path->current_level = level;
  3683. BUG_ON(parent == 0);
  3684. key.objectid = bytenr;
  3685. key.offset = parent + 1;
  3686. key.type = BTRFS_EXTENT_REF_KEY;
  3687. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3688. if (ret < 0)
  3689. goto out;
  3690. BUG_ON(ret == 0);
  3691. leaf = path->nodes[0];
  3692. nritems = btrfs_header_nritems(leaf);
  3693. if (path->slots[0] >= nritems) {
  3694. ret = btrfs_next_leaf(extent_root, path);
  3695. if (ret < 0)
  3696. goto out;
  3697. if (ret > 0)
  3698. goto next;
  3699. leaf = path->nodes[0];
  3700. }
  3701. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3702. if (found_key.objectid == bytenr &&
  3703. found_key.type == BTRFS_EXTENT_REF_KEY) {
  3704. if (level < ref_path->shared_level)
  3705. ref_path->shared_level = level;
  3706. goto found;
  3707. }
  3708. next:
  3709. level--;
  3710. btrfs_release_path(extent_root, path);
  3711. cond_resched();
  3712. }
  3713. /* reached lowest level */
  3714. ret = 1;
  3715. goto out;
  3716. walk_up:
  3717. level = ref_path->current_level;
  3718. while (level < BTRFS_MAX_LEVEL - 1) {
  3719. u64 ref_objectid;
  3720. if (level >= 0) {
  3721. bytenr = ref_path->nodes[level];
  3722. } else {
  3723. bytenr = ref_path->extent_start;
  3724. }
  3725. BUG_ON(bytenr == 0);
  3726. key.objectid = bytenr;
  3727. key.offset = 0;
  3728. key.type = BTRFS_EXTENT_REF_KEY;
  3729. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3730. if (ret < 0)
  3731. goto out;
  3732. leaf = path->nodes[0];
  3733. nritems = btrfs_header_nritems(leaf);
  3734. if (path->slots[0] >= nritems) {
  3735. ret = btrfs_next_leaf(extent_root, path);
  3736. if (ret < 0)
  3737. goto out;
  3738. if (ret > 0) {
  3739. /* the extent was freed by someone */
  3740. if (ref_path->lowest_level == level)
  3741. goto out;
  3742. btrfs_release_path(extent_root, path);
  3743. goto walk_down;
  3744. }
  3745. leaf = path->nodes[0];
  3746. }
  3747. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3748. if (found_key.objectid != bytenr ||
  3749. found_key.type != BTRFS_EXTENT_REF_KEY) {
  3750. /* the extent was freed by someone */
  3751. if (ref_path->lowest_level == level) {
  3752. ret = 1;
  3753. goto out;
  3754. }
  3755. btrfs_release_path(extent_root, path);
  3756. goto walk_down;
  3757. }
  3758. found:
  3759. ref = btrfs_item_ptr(leaf, path->slots[0],
  3760. struct btrfs_extent_ref);
  3761. ref_objectid = btrfs_ref_objectid(leaf, ref);
  3762. if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  3763. if (first_time) {
  3764. level = (int)ref_objectid;
  3765. BUG_ON(level >= BTRFS_MAX_LEVEL);
  3766. ref_path->lowest_level = level;
  3767. ref_path->current_level = level;
  3768. ref_path->nodes[level] = bytenr;
  3769. } else {
  3770. WARN_ON(ref_objectid != level);
  3771. }
  3772. } else {
  3773. WARN_ON(level != -1);
  3774. }
  3775. first_time = 0;
  3776. if (ref_path->lowest_level == level) {
  3777. ref_path->owner_objectid = ref_objectid;
  3778. ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
  3779. }
  3780. /*
  3781. * the block is tree root or the block isn't in reference
  3782. * counted tree.
  3783. */
  3784. if (found_key.objectid == found_key.offset ||
  3785. is_cowonly_root(btrfs_ref_root(leaf, ref))) {
  3786. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  3787. ref_path->root_generation =
  3788. btrfs_ref_generation(leaf, ref);
  3789. if (level < 0) {
  3790. /* special reference from the tree log */
  3791. ref_path->nodes[0] = found_key.offset;
  3792. ref_path->current_level = 0;
  3793. }
  3794. ret = 0;
  3795. goto out;
  3796. }
  3797. level++;
  3798. BUG_ON(ref_path->nodes[level] != 0);
  3799. ref_path->nodes[level] = found_key.offset;
  3800. ref_path->current_level = level;
  3801. /*
  3802. * the reference was created in the running transaction,
  3803. * no need to continue walking up.
  3804. */
  3805. if (btrfs_ref_generation(leaf, ref) == trans->transid) {
  3806. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  3807. ref_path->root_generation =
  3808. btrfs_ref_generation(leaf, ref);
  3809. ret = 0;
  3810. goto out;
  3811. }
  3812. btrfs_release_path(extent_root, path);
  3813. cond_resched();
  3814. }
  3815. /* reached max tree level, but no tree root found. */
  3816. BUG();
  3817. out:
  3818. btrfs_free_path(path);
  3819. return ret;
  3820. }
  3821. static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
  3822. struct btrfs_root *extent_root,
  3823. struct btrfs_ref_path *ref_path,
  3824. u64 extent_start)
  3825. {
  3826. memset(ref_path, 0, sizeof(*ref_path));
  3827. ref_path->extent_start = extent_start;
  3828. return __next_ref_path(trans, extent_root, ref_path, 1);
  3829. }
  3830. static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
  3831. struct btrfs_root *extent_root,
  3832. struct btrfs_ref_path *ref_path)
  3833. {
  3834. return __next_ref_path(trans, extent_root, ref_path, 0);
  3835. }
  3836. static int noinline get_new_locations(struct inode *reloc_inode,
  3837. struct btrfs_key *extent_key,
  3838. u64 offset, int no_fragment,
  3839. struct disk_extent **extents,
  3840. int *nr_extents)
  3841. {
  3842. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  3843. struct btrfs_path *path;
  3844. struct btrfs_file_extent_item *fi;
  3845. struct extent_buffer *leaf;
  3846. struct disk_extent *exts = *extents;
  3847. struct btrfs_key found_key;
  3848. u64 cur_pos;
  3849. u64 last_byte;
  3850. u32 nritems;
  3851. int nr = 0;
  3852. int max = *nr_extents;
  3853. int ret;
  3854. WARN_ON(!no_fragment && *extents);
  3855. if (!exts) {
  3856. max = 1;
  3857. exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
  3858. if (!exts)
  3859. return -ENOMEM;
  3860. }
  3861. path = btrfs_alloc_path();
  3862. BUG_ON(!path);
  3863. cur_pos = extent_key->objectid - offset;
  3864. last_byte = extent_key->objectid + extent_key->offset;
  3865. ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
  3866. cur_pos, 0);
  3867. if (ret < 0)
  3868. goto out;
  3869. if (ret > 0) {
  3870. ret = -ENOENT;
  3871. goto out;
  3872. }
  3873. while (1) {
  3874. leaf = path->nodes[0];
  3875. nritems = btrfs_header_nritems(leaf);
  3876. if (path->slots[0] >= nritems) {
  3877. ret = btrfs_next_leaf(root, path);
  3878. if (ret < 0)
  3879. goto out;
  3880. if (ret > 0)
  3881. break;
  3882. leaf = path->nodes[0];
  3883. }
  3884. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3885. if (found_key.offset != cur_pos ||
  3886. found_key.type != BTRFS_EXTENT_DATA_KEY ||
  3887. found_key.objectid != reloc_inode->i_ino)
  3888. break;
  3889. fi = btrfs_item_ptr(leaf, path->slots[0],
  3890. struct btrfs_file_extent_item);
  3891. if (btrfs_file_extent_type(leaf, fi) !=
  3892. BTRFS_FILE_EXTENT_REG ||
  3893. btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  3894. break;
  3895. if (nr == max) {
  3896. struct disk_extent *old = exts;
  3897. max *= 2;
  3898. exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
  3899. memcpy(exts, old, sizeof(*exts) * nr);
  3900. if (old != *extents)
  3901. kfree(old);
  3902. }
  3903. exts[nr].disk_bytenr =
  3904. btrfs_file_extent_disk_bytenr(leaf, fi);
  3905. exts[nr].disk_num_bytes =
  3906. btrfs_file_extent_disk_num_bytes(leaf, fi);
  3907. exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
  3908. exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  3909. exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  3910. exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
  3911. exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
  3912. exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
  3913. fi);
  3914. BUG_ON(exts[nr].offset > 0);
  3915. BUG_ON(exts[nr].compression || exts[nr].encryption);
  3916. BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
  3917. cur_pos += exts[nr].num_bytes;
  3918. nr++;
  3919. if (cur_pos + offset >= last_byte)
  3920. break;
  3921. if (no_fragment) {
  3922. ret = 1;
  3923. goto out;
  3924. }
  3925. path->slots[0]++;
  3926. }
  3927. WARN_ON(cur_pos + offset > last_byte);
  3928. if (cur_pos + offset < last_byte) {
  3929. ret = -ENOENT;
  3930. goto out;
  3931. }
  3932. ret = 0;
  3933. out:
  3934. btrfs_free_path(path);
  3935. if (ret) {
  3936. if (exts != *extents)
  3937. kfree(exts);
  3938. } else {
  3939. *extents = exts;
  3940. *nr_extents = nr;
  3941. }
  3942. return ret;
  3943. }
  3944. static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
  3945. struct btrfs_root *root,
  3946. struct btrfs_path *path,
  3947. struct btrfs_key *extent_key,
  3948. struct btrfs_key *leaf_key,
  3949. struct btrfs_ref_path *ref_path,
  3950. struct disk_extent *new_extents,
  3951. int nr_extents)
  3952. {
  3953. struct extent_buffer *leaf;
  3954. struct btrfs_file_extent_item *fi;
  3955. struct inode *inode = NULL;
  3956. struct btrfs_key key;
  3957. u64 lock_start = 0;
  3958. u64 lock_end = 0;
  3959. u64 num_bytes;
  3960. u64 ext_offset;
  3961. u64 first_pos;
  3962. u32 nritems;
  3963. int nr_scaned = 0;
  3964. int extent_locked = 0;
  3965. int extent_type;
  3966. int ret;
  3967. memcpy(&key, leaf_key, sizeof(key));
  3968. first_pos = INT_LIMIT(loff_t) - extent_key->offset;
  3969. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  3970. if (key.objectid < ref_path->owner_objectid ||
  3971. (key.objectid == ref_path->owner_objectid &&
  3972. key.type < BTRFS_EXTENT_DATA_KEY)) {
  3973. key.objectid = ref_path->owner_objectid;
  3974. key.type = BTRFS_EXTENT_DATA_KEY;
  3975. key.offset = 0;
  3976. }
  3977. }
  3978. while (1) {
  3979. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3980. if (ret < 0)
  3981. goto out;
  3982. leaf = path->nodes[0];
  3983. nritems = btrfs_header_nritems(leaf);
  3984. next:
  3985. if (extent_locked && ret > 0) {
  3986. /*
  3987. * the file extent item was modified by someone
  3988. * before the extent got locked.
  3989. */
  3990. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  3991. lock_end, GFP_NOFS);
  3992. extent_locked = 0;
  3993. }
  3994. if (path->slots[0] >= nritems) {
  3995. if (++nr_scaned > 2)
  3996. break;
  3997. BUG_ON(extent_locked);
  3998. ret = btrfs_next_leaf(root, path);
  3999. if (ret < 0)
  4000. goto out;
  4001. if (ret > 0)
  4002. break;
  4003. leaf = path->nodes[0];
  4004. nritems = btrfs_header_nritems(leaf);
  4005. }
  4006. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4007. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  4008. if ((key.objectid > ref_path->owner_objectid) ||
  4009. (key.objectid == ref_path->owner_objectid &&
  4010. key.type > BTRFS_EXTENT_DATA_KEY) ||
  4011. (key.offset >= first_pos + extent_key->offset))
  4012. break;
  4013. }
  4014. if (inode && key.objectid != inode->i_ino) {
  4015. BUG_ON(extent_locked);
  4016. btrfs_release_path(root, path);
  4017. mutex_unlock(&inode->i_mutex);
  4018. iput(inode);
  4019. inode = NULL;
  4020. continue;
  4021. }
  4022. if (key.type != BTRFS_EXTENT_DATA_KEY) {
  4023. path->slots[0]++;
  4024. ret = 1;
  4025. goto next;
  4026. }
  4027. fi = btrfs_item_ptr(leaf, path->slots[0],
  4028. struct btrfs_file_extent_item);
  4029. extent_type = btrfs_file_extent_type(leaf, fi);
  4030. if ((extent_type != BTRFS_FILE_EXTENT_REG &&
  4031. extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
  4032. (btrfs_file_extent_disk_bytenr(leaf, fi) !=
  4033. extent_key->objectid)) {
  4034. path->slots[0]++;
  4035. ret = 1;
  4036. goto next;
  4037. }
  4038. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  4039. ext_offset = btrfs_file_extent_offset(leaf, fi);
  4040. if (first_pos > key.offset - ext_offset)
  4041. first_pos = key.offset - ext_offset;
  4042. if (!extent_locked) {
  4043. lock_start = key.offset;
  4044. lock_end = lock_start + num_bytes - 1;
  4045. } else {
  4046. if (lock_start > key.offset ||
  4047. lock_end + 1 < key.offset + num_bytes) {
  4048. unlock_extent(&BTRFS_I(inode)->io_tree,
  4049. lock_start, lock_end, GFP_NOFS);
  4050. extent_locked = 0;
  4051. }
  4052. }
  4053. if (!inode) {
  4054. btrfs_release_path(root, path);
  4055. inode = btrfs_iget_locked(root->fs_info->sb,
  4056. key.objectid, root);
  4057. if (inode->i_state & I_NEW) {
  4058. BTRFS_I(inode)->root = root;
  4059. BTRFS_I(inode)->location.objectid =
  4060. key.objectid;
  4061. BTRFS_I(inode)->location.type =
  4062. BTRFS_INODE_ITEM_KEY;
  4063. BTRFS_I(inode)->location.offset = 0;
  4064. btrfs_read_locked_inode(inode);
  4065. unlock_new_inode(inode);
  4066. }
  4067. /*
  4068. * some code call btrfs_commit_transaction while
  4069. * holding the i_mutex, so we can't use mutex_lock
  4070. * here.
  4071. */
  4072. if (is_bad_inode(inode) ||
  4073. !mutex_trylock(&inode->i_mutex)) {
  4074. iput(inode);
  4075. inode = NULL;
  4076. key.offset = (u64)-1;
  4077. goto skip;
  4078. }
  4079. }
  4080. if (!extent_locked) {
  4081. struct btrfs_ordered_extent *ordered;
  4082. btrfs_release_path(root, path);
  4083. lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4084. lock_end, GFP_NOFS);
  4085. ordered = btrfs_lookup_first_ordered_extent(inode,
  4086. lock_end);
  4087. if (ordered &&
  4088. ordered->file_offset <= lock_end &&
  4089. ordered->file_offset + ordered->len > lock_start) {
  4090. unlock_extent(&BTRFS_I(inode)->io_tree,
  4091. lock_start, lock_end, GFP_NOFS);
  4092. btrfs_start_ordered_extent(inode, ordered, 1);
  4093. btrfs_put_ordered_extent(ordered);
  4094. key.offset += num_bytes;
  4095. goto skip;
  4096. }
  4097. if (ordered)
  4098. btrfs_put_ordered_extent(ordered);
  4099. extent_locked = 1;
  4100. continue;
  4101. }
  4102. if (nr_extents == 1) {
  4103. /* update extent pointer in place */
  4104. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4105. new_extents[0].disk_bytenr);
  4106. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4107. new_extents[0].disk_num_bytes);
  4108. btrfs_mark_buffer_dirty(leaf);
  4109. btrfs_drop_extent_cache(inode, key.offset,
  4110. key.offset + num_bytes - 1, 0);
  4111. ret = btrfs_inc_extent_ref(trans, root,
  4112. new_extents[0].disk_bytenr,
  4113. new_extents[0].disk_num_bytes,
  4114. leaf->start,
  4115. root->root_key.objectid,
  4116. trans->transid,
  4117. key.objectid);
  4118. BUG_ON(ret);
  4119. ret = btrfs_free_extent(trans, root,
  4120. extent_key->objectid,
  4121. extent_key->offset,
  4122. leaf->start,
  4123. btrfs_header_owner(leaf),
  4124. btrfs_header_generation(leaf),
  4125. key.objectid, 0);
  4126. BUG_ON(ret);
  4127. btrfs_release_path(root, path);
  4128. key.offset += num_bytes;
  4129. } else {
  4130. BUG_ON(1);
  4131. #if 0
  4132. u64 alloc_hint;
  4133. u64 extent_len;
  4134. int i;
  4135. /*
  4136. * drop old extent pointer at first, then insert the
  4137. * new pointers one bye one
  4138. */
  4139. btrfs_release_path(root, path);
  4140. ret = btrfs_drop_extents(trans, root, inode, key.offset,
  4141. key.offset + num_bytes,
  4142. key.offset, &alloc_hint);
  4143. BUG_ON(ret);
  4144. for (i = 0; i < nr_extents; i++) {
  4145. if (ext_offset >= new_extents[i].num_bytes) {
  4146. ext_offset -= new_extents[i].num_bytes;
  4147. continue;
  4148. }
  4149. extent_len = min(new_extents[i].num_bytes -
  4150. ext_offset, num_bytes);
  4151. ret = btrfs_insert_empty_item(trans, root,
  4152. path, &key,
  4153. sizeof(*fi));
  4154. BUG_ON(ret);
  4155. leaf = path->nodes[0];
  4156. fi = btrfs_item_ptr(leaf, path->slots[0],
  4157. struct btrfs_file_extent_item);
  4158. btrfs_set_file_extent_generation(leaf, fi,
  4159. trans->transid);
  4160. btrfs_set_file_extent_type(leaf, fi,
  4161. BTRFS_FILE_EXTENT_REG);
  4162. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4163. new_extents[i].disk_bytenr);
  4164. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4165. new_extents[i].disk_num_bytes);
  4166. btrfs_set_file_extent_ram_bytes(leaf, fi,
  4167. new_extents[i].ram_bytes);
  4168. btrfs_set_file_extent_compression(leaf, fi,
  4169. new_extents[i].compression);
  4170. btrfs_set_file_extent_encryption(leaf, fi,
  4171. new_extents[i].encryption);
  4172. btrfs_set_file_extent_other_encoding(leaf, fi,
  4173. new_extents[i].other_encoding);
  4174. btrfs_set_file_extent_num_bytes(leaf, fi,
  4175. extent_len);
  4176. ext_offset += new_extents[i].offset;
  4177. btrfs_set_file_extent_offset(leaf, fi,
  4178. ext_offset);
  4179. btrfs_mark_buffer_dirty(leaf);
  4180. btrfs_drop_extent_cache(inode, key.offset,
  4181. key.offset + extent_len - 1, 0);
  4182. ret = btrfs_inc_extent_ref(trans, root,
  4183. new_extents[i].disk_bytenr,
  4184. new_extents[i].disk_num_bytes,
  4185. leaf->start,
  4186. root->root_key.objectid,
  4187. trans->transid, key.objectid);
  4188. BUG_ON(ret);
  4189. btrfs_release_path(root, path);
  4190. inode_add_bytes(inode, extent_len);
  4191. ext_offset = 0;
  4192. num_bytes -= extent_len;
  4193. key.offset += extent_len;
  4194. if (num_bytes == 0)
  4195. break;
  4196. }
  4197. BUG_ON(i >= nr_extents);
  4198. #endif
  4199. }
  4200. if (extent_locked) {
  4201. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4202. lock_end, GFP_NOFS);
  4203. extent_locked = 0;
  4204. }
  4205. skip:
  4206. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
  4207. key.offset >= first_pos + extent_key->offset)
  4208. break;
  4209. cond_resched();
  4210. }
  4211. ret = 0;
  4212. out:
  4213. btrfs_release_path(root, path);
  4214. if (inode) {
  4215. mutex_unlock(&inode->i_mutex);
  4216. if (extent_locked) {
  4217. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  4218. lock_end, GFP_NOFS);
  4219. }
  4220. iput(inode);
  4221. }
  4222. return ret;
  4223. }
  4224. int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
  4225. struct btrfs_root *root,
  4226. struct extent_buffer *buf, u64 orig_start)
  4227. {
  4228. int level;
  4229. int ret;
  4230. BUG_ON(btrfs_header_generation(buf) != trans->transid);
  4231. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  4232. level = btrfs_header_level(buf);
  4233. if (level == 0) {
  4234. struct btrfs_leaf_ref *ref;
  4235. struct btrfs_leaf_ref *orig_ref;
  4236. orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
  4237. if (!orig_ref)
  4238. return -ENOENT;
  4239. ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
  4240. if (!ref) {
  4241. btrfs_free_leaf_ref(root, orig_ref);
  4242. return -ENOMEM;
  4243. }
  4244. ref->nritems = orig_ref->nritems;
  4245. memcpy(ref->extents, orig_ref->extents,
  4246. sizeof(ref->extents[0]) * ref->nritems);
  4247. btrfs_free_leaf_ref(root, orig_ref);
  4248. ref->root_gen = trans->transid;
  4249. ref->bytenr = buf->start;
  4250. ref->owner = btrfs_header_owner(buf);
  4251. ref->generation = btrfs_header_generation(buf);
  4252. ret = btrfs_add_leaf_ref(root, ref, 0);
  4253. WARN_ON(ret);
  4254. btrfs_free_leaf_ref(root, ref);
  4255. }
  4256. return 0;
  4257. }
  4258. static int noinline invalidate_extent_cache(struct btrfs_root *root,
  4259. struct extent_buffer *leaf,
  4260. struct btrfs_block_group_cache *group,
  4261. struct btrfs_root *target_root)
  4262. {
  4263. struct btrfs_key key;
  4264. struct inode *inode = NULL;
  4265. struct btrfs_file_extent_item *fi;
  4266. u64 num_bytes;
  4267. u64 skip_objectid = 0;
  4268. u32 nritems;
  4269. u32 i;
  4270. nritems = btrfs_header_nritems(leaf);
  4271. for (i = 0; i < nritems; i++) {
  4272. btrfs_item_key_to_cpu(leaf, &key, i);
  4273. if (key.objectid == skip_objectid ||
  4274. key.type != BTRFS_EXTENT_DATA_KEY)
  4275. continue;
  4276. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  4277. if (btrfs_file_extent_type(leaf, fi) ==
  4278. BTRFS_FILE_EXTENT_INLINE)
  4279. continue;
  4280. if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  4281. continue;
  4282. if (!inode || inode->i_ino != key.objectid) {
  4283. iput(inode);
  4284. inode = btrfs_ilookup(target_root->fs_info->sb,
  4285. key.objectid, target_root, 1);
  4286. }
  4287. if (!inode) {
  4288. skip_objectid = key.objectid;
  4289. continue;
  4290. }
  4291. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  4292. lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  4293. key.offset + num_bytes - 1, GFP_NOFS);
  4294. btrfs_drop_extent_cache(inode, key.offset,
  4295. key.offset + num_bytes - 1, 1);
  4296. unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  4297. key.offset + num_bytes - 1, GFP_NOFS);
  4298. cond_resched();
  4299. }
  4300. iput(inode);
  4301. return 0;
  4302. }
  4303. static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
  4304. struct btrfs_root *root,
  4305. struct extent_buffer *leaf,
  4306. struct btrfs_block_group_cache *group,
  4307. struct inode *reloc_inode)
  4308. {
  4309. struct btrfs_key key;
  4310. struct btrfs_key extent_key;
  4311. struct btrfs_file_extent_item *fi;
  4312. struct btrfs_leaf_ref *ref;
  4313. struct disk_extent *new_extent;
  4314. u64 bytenr;
  4315. u64 num_bytes;
  4316. u32 nritems;
  4317. u32 i;
  4318. int ext_index;
  4319. int nr_extent;
  4320. int ret;
  4321. new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
  4322. BUG_ON(!new_extent);
  4323. ref = btrfs_lookup_leaf_ref(root, leaf->start);
  4324. BUG_ON(!ref);
  4325. ext_index = -1;
  4326. nritems = btrfs_header_nritems(leaf);
  4327. for (i = 0; i < nritems; i++) {
  4328. btrfs_item_key_to_cpu(leaf, &key, i);
  4329. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  4330. continue;
  4331. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  4332. if (btrfs_file_extent_type(leaf, fi) ==
  4333. BTRFS_FILE_EXTENT_INLINE)
  4334. continue;
  4335. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  4336. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  4337. if (bytenr == 0)
  4338. continue;
  4339. ext_index++;
  4340. if (bytenr >= group->key.objectid + group->key.offset ||
  4341. bytenr + num_bytes <= group->key.objectid)
  4342. continue;
  4343. extent_key.objectid = bytenr;
  4344. extent_key.offset = num_bytes;
  4345. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  4346. nr_extent = 1;
  4347. ret = get_new_locations(reloc_inode, &extent_key,
  4348. group->key.objectid, 1,
  4349. &new_extent, &nr_extent);
  4350. if (ret > 0)
  4351. continue;
  4352. BUG_ON(ret < 0);
  4353. BUG_ON(ref->extents[ext_index].bytenr != bytenr);
  4354. BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
  4355. ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
  4356. ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
  4357. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  4358. new_extent->disk_bytenr);
  4359. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  4360. new_extent->disk_num_bytes);
  4361. btrfs_mark_buffer_dirty(leaf);
  4362. ret = btrfs_inc_extent_ref(trans, root,
  4363. new_extent->disk_bytenr,
  4364. new_extent->disk_num_bytes,
  4365. leaf->start,
  4366. root->root_key.objectid,
  4367. trans->transid, key.objectid);
  4368. BUG_ON(ret);
  4369. ret = btrfs_free_extent(trans, root,
  4370. bytenr, num_bytes, leaf->start,
  4371. btrfs_header_owner(leaf),
  4372. btrfs_header_generation(leaf),
  4373. key.objectid, 0);
  4374. BUG_ON(ret);
  4375. cond_resched();
  4376. }
  4377. kfree(new_extent);
  4378. BUG_ON(ext_index + 1 != ref->nritems);
  4379. btrfs_free_leaf_ref(root, ref);
  4380. return 0;
  4381. }
  4382. int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
  4383. struct btrfs_root *root)
  4384. {
  4385. struct btrfs_root *reloc_root;
  4386. int ret;
  4387. if (root->reloc_root) {
  4388. reloc_root = root->reloc_root;
  4389. root->reloc_root = NULL;
  4390. list_add(&reloc_root->dead_list,
  4391. &root->fs_info->dead_reloc_roots);
  4392. btrfs_set_root_bytenr(&reloc_root->root_item,
  4393. reloc_root->node->start);
  4394. btrfs_set_root_level(&root->root_item,
  4395. btrfs_header_level(reloc_root->node));
  4396. memset(&reloc_root->root_item.drop_progress, 0,
  4397. sizeof(struct btrfs_disk_key));
  4398. reloc_root->root_item.drop_level = 0;
  4399. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  4400. &reloc_root->root_key,
  4401. &reloc_root->root_item);
  4402. BUG_ON(ret);
  4403. }
  4404. return 0;
  4405. }
  4406. int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
  4407. {
  4408. struct btrfs_trans_handle *trans;
  4409. struct btrfs_root *reloc_root;
  4410. struct btrfs_root *prev_root = NULL;
  4411. struct list_head dead_roots;
  4412. int ret;
  4413. unsigned long nr;
  4414. INIT_LIST_HEAD(&dead_roots);
  4415. list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
  4416. while (!list_empty(&dead_roots)) {
  4417. reloc_root = list_entry(dead_roots.prev,
  4418. struct btrfs_root, dead_list);
  4419. list_del_init(&reloc_root->dead_list);
  4420. BUG_ON(reloc_root->commit_root != NULL);
  4421. while (1) {
  4422. trans = btrfs_join_transaction(root, 1);
  4423. BUG_ON(!trans);
  4424. mutex_lock(&root->fs_info->drop_mutex);
  4425. ret = btrfs_drop_snapshot(trans, reloc_root);
  4426. if (ret != -EAGAIN)
  4427. break;
  4428. mutex_unlock(&root->fs_info->drop_mutex);
  4429. nr = trans->blocks_used;
  4430. ret = btrfs_end_transaction(trans, root);
  4431. BUG_ON(ret);
  4432. btrfs_btree_balance_dirty(root, nr);
  4433. }
  4434. free_extent_buffer(reloc_root->node);
  4435. ret = btrfs_del_root(trans, root->fs_info->tree_root,
  4436. &reloc_root->root_key);
  4437. BUG_ON(ret);
  4438. mutex_unlock(&root->fs_info->drop_mutex);
  4439. nr = trans->blocks_used;
  4440. ret = btrfs_end_transaction(trans, root);
  4441. BUG_ON(ret);
  4442. btrfs_btree_balance_dirty(root, nr);
  4443. kfree(prev_root);
  4444. prev_root = reloc_root;
  4445. }
  4446. if (prev_root) {
  4447. btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
  4448. kfree(prev_root);
  4449. }
  4450. return 0;
  4451. }
  4452. int btrfs_add_dead_reloc_root(struct btrfs_root *root)
  4453. {
  4454. list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
  4455. return 0;
  4456. }
  4457. int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
  4458. {
  4459. struct btrfs_root *reloc_root;
  4460. struct btrfs_trans_handle *trans;
  4461. struct btrfs_key location;
  4462. int found;
  4463. int ret;
  4464. mutex_lock(&root->fs_info->tree_reloc_mutex);
  4465. ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
  4466. BUG_ON(ret);
  4467. found = !list_empty(&root->fs_info->dead_reloc_roots);
  4468. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  4469. if (found) {
  4470. trans = btrfs_start_transaction(root, 1);
  4471. BUG_ON(!trans);
  4472. ret = btrfs_commit_transaction(trans, root);
  4473. BUG_ON(ret);
  4474. }
  4475. location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  4476. location.offset = (u64)-1;
  4477. location.type = BTRFS_ROOT_ITEM_KEY;
  4478. reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  4479. BUG_ON(!reloc_root);
  4480. btrfs_orphan_cleanup(reloc_root);
  4481. return 0;
  4482. }
  4483. static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
  4484. struct btrfs_root *root)
  4485. {
  4486. struct btrfs_root *reloc_root;
  4487. struct extent_buffer *eb;
  4488. struct btrfs_root_item *root_item;
  4489. struct btrfs_key root_key;
  4490. int ret;
  4491. BUG_ON(!root->ref_cows);
  4492. if (root->reloc_root)
  4493. return 0;
  4494. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  4495. BUG_ON(!root_item);
  4496. ret = btrfs_copy_root(trans, root, root->commit_root,
  4497. &eb, BTRFS_TREE_RELOC_OBJECTID);
  4498. BUG_ON(ret);
  4499. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  4500. root_key.offset = root->root_key.objectid;
  4501. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4502. memcpy(root_item, &root->root_item, sizeof(root_item));
  4503. btrfs_set_root_refs(root_item, 0);
  4504. btrfs_set_root_bytenr(root_item, eb->start);
  4505. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  4506. btrfs_set_root_generation(root_item, trans->transid);
  4507. btrfs_tree_unlock(eb);
  4508. free_extent_buffer(eb);
  4509. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  4510. &root_key, root_item);
  4511. BUG_ON(ret);
  4512. kfree(root_item);
  4513. reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  4514. &root_key);
  4515. BUG_ON(!reloc_root);
  4516. reloc_root->last_trans = trans->transid;
  4517. reloc_root->commit_root = NULL;
  4518. reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
  4519. root->reloc_root = reloc_root;
  4520. return 0;
  4521. }
  4522. /*
  4523. * Core function of space balance.
  4524. *
  4525. * The idea is using reloc trees to relocate tree blocks in reference
  4526. * counted roots. There is one reloc tree for each subvol, and all
  4527. * reloc trees share same root key objectid. Reloc trees are snapshots
  4528. * of the latest committed roots of subvols (root->commit_root).
  4529. *
  4530. * To relocate a tree block referenced by a subvol, there are two steps.
  4531. * COW the block through subvol's reloc tree, then update block pointer
  4532. * in the subvol to point to the new block. Since all reloc trees share
  4533. * same root key objectid, doing special handing for tree blocks owned
  4534. * by them is easy. Once a tree block has been COWed in one reloc tree,
  4535. * we can use the resulting new block directly when the same block is
  4536. * required to COW again through other reloc trees. By this way, relocated
  4537. * tree blocks are shared between reloc trees, so they are also shared
  4538. * between subvols.
  4539. */
  4540. static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
  4541. struct btrfs_root *root,
  4542. struct btrfs_path *path,
  4543. struct btrfs_key *first_key,
  4544. struct btrfs_ref_path *ref_path,
  4545. struct btrfs_block_group_cache *group,
  4546. struct inode *reloc_inode)
  4547. {
  4548. struct btrfs_root *reloc_root;
  4549. struct extent_buffer *eb = NULL;
  4550. struct btrfs_key *keys;
  4551. u64 *nodes;
  4552. int level;
  4553. int shared_level;
  4554. int lowest_level = 0;
  4555. int ret;
  4556. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  4557. lowest_level = ref_path->owner_objectid;
  4558. if (!root->ref_cows) {
  4559. path->lowest_level = lowest_level;
  4560. ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
  4561. BUG_ON(ret < 0);
  4562. path->lowest_level = 0;
  4563. btrfs_release_path(root, path);
  4564. return 0;
  4565. }
  4566. mutex_lock(&root->fs_info->tree_reloc_mutex);
  4567. ret = init_reloc_tree(trans, root);
  4568. BUG_ON(ret);
  4569. reloc_root = root->reloc_root;
  4570. shared_level = ref_path->shared_level;
  4571. ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
  4572. keys = ref_path->node_keys;
  4573. nodes = ref_path->new_nodes;
  4574. memset(&keys[shared_level + 1], 0,
  4575. sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
  4576. memset(&nodes[shared_level + 1], 0,
  4577. sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
  4578. if (nodes[lowest_level] == 0) {
  4579. path->lowest_level = lowest_level;
  4580. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  4581. 0, 1);
  4582. BUG_ON(ret);
  4583. for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
  4584. eb = path->nodes[level];
  4585. if (!eb || eb == reloc_root->node)
  4586. break;
  4587. nodes[level] = eb->start;
  4588. if (level == 0)
  4589. btrfs_item_key_to_cpu(eb, &keys[level], 0);
  4590. else
  4591. btrfs_node_key_to_cpu(eb, &keys[level], 0);
  4592. }
  4593. if (nodes[0] &&
  4594. ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4595. eb = path->nodes[0];
  4596. ret = replace_extents_in_leaf(trans, reloc_root, eb,
  4597. group, reloc_inode);
  4598. BUG_ON(ret);
  4599. }
  4600. btrfs_release_path(reloc_root, path);
  4601. } else {
  4602. ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
  4603. lowest_level);
  4604. BUG_ON(ret);
  4605. }
  4606. /*
  4607. * replace tree blocks in the fs tree with tree blocks in
  4608. * the reloc tree.
  4609. */
  4610. ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
  4611. BUG_ON(ret < 0);
  4612. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4613. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  4614. 0, 0);
  4615. BUG_ON(ret);
  4616. extent_buffer_get(path->nodes[0]);
  4617. eb = path->nodes[0];
  4618. btrfs_release_path(reloc_root, path);
  4619. ret = invalidate_extent_cache(reloc_root, eb, group, root);
  4620. BUG_ON(ret);
  4621. free_extent_buffer(eb);
  4622. }
  4623. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  4624. path->lowest_level = 0;
  4625. return 0;
  4626. }
  4627. static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
  4628. struct btrfs_root *root,
  4629. struct btrfs_path *path,
  4630. struct btrfs_key *first_key,
  4631. struct btrfs_ref_path *ref_path)
  4632. {
  4633. int ret;
  4634. ret = relocate_one_path(trans, root, path, first_key,
  4635. ref_path, NULL, NULL);
  4636. BUG_ON(ret);
  4637. if (root == root->fs_info->extent_root)
  4638. btrfs_extent_post_op(trans, root);
  4639. return 0;
  4640. }
  4641. static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
  4642. struct btrfs_root *extent_root,
  4643. struct btrfs_path *path,
  4644. struct btrfs_key *extent_key)
  4645. {
  4646. int ret;
  4647. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  4648. if (ret)
  4649. goto out;
  4650. ret = btrfs_del_item(trans, extent_root, path);
  4651. out:
  4652. btrfs_release_path(extent_root, path);
  4653. return ret;
  4654. }
  4655. static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
  4656. struct btrfs_ref_path *ref_path)
  4657. {
  4658. struct btrfs_key root_key;
  4659. root_key.objectid = ref_path->root_objectid;
  4660. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4661. if (is_cowonly_root(ref_path->root_objectid))
  4662. root_key.offset = 0;
  4663. else
  4664. root_key.offset = (u64)-1;
  4665. return btrfs_read_fs_root_no_name(fs_info, &root_key);
  4666. }
  4667. static int noinline relocate_one_extent(struct btrfs_root *extent_root,
  4668. struct btrfs_path *path,
  4669. struct btrfs_key *extent_key,
  4670. struct btrfs_block_group_cache *group,
  4671. struct inode *reloc_inode, int pass)
  4672. {
  4673. struct btrfs_trans_handle *trans;
  4674. struct btrfs_root *found_root;
  4675. struct btrfs_ref_path *ref_path = NULL;
  4676. struct disk_extent *new_extents = NULL;
  4677. int nr_extents = 0;
  4678. int loops;
  4679. int ret;
  4680. int level;
  4681. struct btrfs_key first_key;
  4682. u64 prev_block = 0;
  4683. trans = btrfs_start_transaction(extent_root, 1);
  4684. BUG_ON(!trans);
  4685. if (extent_key->objectid == 0) {
  4686. ret = del_extent_zero(trans, extent_root, path, extent_key);
  4687. goto out;
  4688. }
  4689. ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
  4690. if (!ref_path) {
  4691. ret = -ENOMEM;
  4692. goto out;
  4693. }
  4694. for (loops = 0; ; loops++) {
  4695. if (loops == 0) {
  4696. ret = btrfs_first_ref_path(trans, extent_root, ref_path,
  4697. extent_key->objectid);
  4698. } else {
  4699. ret = btrfs_next_ref_path(trans, extent_root, ref_path);
  4700. }
  4701. if (ret < 0)
  4702. goto out;
  4703. if (ret > 0)
  4704. break;
  4705. if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  4706. ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  4707. continue;
  4708. found_root = read_ref_root(extent_root->fs_info, ref_path);
  4709. BUG_ON(!found_root);
  4710. /*
  4711. * for reference counted tree, only process reference paths
  4712. * rooted at the latest committed root.
  4713. */
  4714. if (found_root->ref_cows &&
  4715. ref_path->root_generation != found_root->root_key.offset)
  4716. continue;
  4717. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4718. if (pass == 0) {
  4719. /*
  4720. * copy data extents to new locations
  4721. */
  4722. u64 group_start = group->key.objectid;
  4723. ret = relocate_data_extent(reloc_inode,
  4724. extent_key,
  4725. group_start);
  4726. if (ret < 0)
  4727. goto out;
  4728. break;
  4729. }
  4730. level = 0;
  4731. } else {
  4732. level = ref_path->owner_objectid;
  4733. }
  4734. if (prev_block != ref_path->nodes[level]) {
  4735. struct extent_buffer *eb;
  4736. u64 block_start = ref_path->nodes[level];
  4737. u64 block_size = btrfs_level_size(found_root, level);
  4738. eb = read_tree_block(found_root, block_start,
  4739. block_size, 0);
  4740. btrfs_tree_lock(eb);
  4741. BUG_ON(level != btrfs_header_level(eb));
  4742. if (level == 0)
  4743. btrfs_item_key_to_cpu(eb, &first_key, 0);
  4744. else
  4745. btrfs_node_key_to_cpu(eb, &first_key, 0);
  4746. btrfs_tree_unlock(eb);
  4747. free_extent_buffer(eb);
  4748. prev_block = block_start;
  4749. }
  4750. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
  4751. pass >= 2) {
  4752. /*
  4753. * use fallback method to process the remaining
  4754. * references.
  4755. */
  4756. if (!new_extents) {
  4757. u64 group_start = group->key.objectid;
  4758. new_extents = kmalloc(sizeof(*new_extents),
  4759. GFP_NOFS);
  4760. nr_extents = 1;
  4761. ret = get_new_locations(reloc_inode,
  4762. extent_key,
  4763. group_start, 1,
  4764. &new_extents,
  4765. &nr_extents);
  4766. if (ret)
  4767. goto out;
  4768. }
  4769. btrfs_record_root_in_trans(found_root);
  4770. ret = replace_one_extent(trans, found_root,
  4771. path, extent_key,
  4772. &first_key, ref_path,
  4773. new_extents, nr_extents);
  4774. if (ret < 0)
  4775. goto out;
  4776. continue;
  4777. }
  4778. btrfs_record_root_in_trans(found_root);
  4779. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  4780. ret = relocate_tree_block(trans, found_root, path,
  4781. &first_key, ref_path);
  4782. } else {
  4783. /*
  4784. * try to update data extent references while
  4785. * keeping metadata shared between snapshots.
  4786. */
  4787. ret = relocate_one_path(trans, found_root, path,
  4788. &first_key, ref_path,
  4789. group, reloc_inode);
  4790. }
  4791. if (ret < 0)
  4792. goto out;
  4793. }
  4794. ret = 0;
  4795. out:
  4796. btrfs_end_transaction(trans, extent_root);
  4797. kfree(new_extents);
  4798. kfree(ref_path);
  4799. return ret;
  4800. }
  4801. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  4802. {
  4803. u64 num_devices;
  4804. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  4805. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  4806. num_devices = root->fs_info->fs_devices->rw_devices;
  4807. if (num_devices == 1) {
  4808. stripped |= BTRFS_BLOCK_GROUP_DUP;
  4809. stripped = flags & ~stripped;
  4810. /* turn raid0 into single device chunks */
  4811. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  4812. return stripped;
  4813. /* turn mirroring into duplication */
  4814. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  4815. BTRFS_BLOCK_GROUP_RAID10))
  4816. return stripped | BTRFS_BLOCK_GROUP_DUP;
  4817. return flags;
  4818. } else {
  4819. /* they already had raid on here, just return */
  4820. if (flags & stripped)
  4821. return flags;
  4822. stripped |= BTRFS_BLOCK_GROUP_DUP;
  4823. stripped = flags & ~stripped;
  4824. /* switch duplicated blocks with raid1 */
  4825. if (flags & BTRFS_BLOCK_GROUP_DUP)
  4826. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  4827. /* turn single device chunks into raid0 */
  4828. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  4829. }
  4830. return flags;
  4831. }
  4832. static int __alloc_chunk_for_shrink(struct btrfs_root *root,
  4833. struct btrfs_block_group_cache *shrink_block_group,
  4834. int force)
  4835. {
  4836. struct btrfs_trans_handle *trans;
  4837. u64 new_alloc_flags;
  4838. u64 calc;
  4839. spin_lock(&shrink_block_group->lock);
  4840. if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
  4841. spin_unlock(&shrink_block_group->lock);
  4842. trans = btrfs_start_transaction(root, 1);
  4843. spin_lock(&shrink_block_group->lock);
  4844. new_alloc_flags = update_block_group_flags(root,
  4845. shrink_block_group->flags);
  4846. if (new_alloc_flags != shrink_block_group->flags) {
  4847. calc =
  4848. btrfs_block_group_used(&shrink_block_group->item);
  4849. } else {
  4850. calc = shrink_block_group->key.offset;
  4851. }
  4852. spin_unlock(&shrink_block_group->lock);
  4853. do_chunk_alloc(trans, root->fs_info->extent_root,
  4854. calc + 2 * 1024 * 1024, new_alloc_flags, force);
  4855. btrfs_end_transaction(trans, root);
  4856. } else
  4857. spin_unlock(&shrink_block_group->lock);
  4858. return 0;
  4859. }
  4860. static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
  4861. struct btrfs_root *root,
  4862. u64 objectid, u64 size)
  4863. {
  4864. struct btrfs_path *path;
  4865. struct btrfs_inode_item *item;
  4866. struct extent_buffer *leaf;
  4867. int ret;
  4868. path = btrfs_alloc_path();
  4869. if (!path)
  4870. return -ENOMEM;
  4871. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  4872. if (ret)
  4873. goto out;
  4874. leaf = path->nodes[0];
  4875. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
  4876. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  4877. btrfs_set_inode_generation(leaf, item, 1);
  4878. btrfs_set_inode_size(leaf, item, size);
  4879. btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
  4880. btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
  4881. BTRFS_INODE_NOCOMPRESS);
  4882. btrfs_mark_buffer_dirty(leaf);
  4883. btrfs_release_path(root, path);
  4884. out:
  4885. btrfs_free_path(path);
  4886. return ret;
  4887. }
  4888. static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
  4889. struct btrfs_block_group_cache *group)
  4890. {
  4891. struct inode *inode = NULL;
  4892. struct btrfs_trans_handle *trans;
  4893. struct btrfs_root *root;
  4894. struct btrfs_key root_key;
  4895. u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
  4896. int err = 0;
  4897. root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  4898. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4899. root_key.offset = (u64)-1;
  4900. root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  4901. if (IS_ERR(root))
  4902. return ERR_CAST(root);
  4903. trans = btrfs_start_transaction(root, 1);
  4904. BUG_ON(!trans);
  4905. err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
  4906. if (err)
  4907. goto out;
  4908. err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
  4909. BUG_ON(err);
  4910. err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
  4911. group->key.offset, 0, group->key.offset,
  4912. 0, 0, 0);
  4913. BUG_ON(err);
  4914. inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
  4915. if (inode->i_state & I_NEW) {
  4916. BTRFS_I(inode)->root = root;
  4917. BTRFS_I(inode)->location.objectid = objectid;
  4918. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  4919. BTRFS_I(inode)->location.offset = 0;
  4920. btrfs_read_locked_inode(inode);
  4921. unlock_new_inode(inode);
  4922. BUG_ON(is_bad_inode(inode));
  4923. } else {
  4924. BUG_ON(1);
  4925. }
  4926. err = btrfs_orphan_add(trans, inode);
  4927. out:
  4928. btrfs_end_transaction(trans, root);
  4929. if (err) {
  4930. if (inode)
  4931. iput(inode);
  4932. inode = ERR_PTR(err);
  4933. }
  4934. return inode;
  4935. }
  4936. int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
  4937. {
  4938. struct btrfs_trans_handle *trans;
  4939. struct btrfs_path *path;
  4940. struct btrfs_fs_info *info = root->fs_info;
  4941. struct extent_buffer *leaf;
  4942. struct inode *reloc_inode;
  4943. struct btrfs_block_group_cache *block_group;
  4944. struct btrfs_key key;
  4945. u64 skipped;
  4946. u64 cur_byte;
  4947. u64 total_found;
  4948. u32 nritems;
  4949. int ret;
  4950. int progress;
  4951. int pass = 0;
  4952. root = root->fs_info->extent_root;
  4953. block_group = btrfs_lookup_block_group(info, group_start);
  4954. BUG_ON(!block_group);
  4955. printk("btrfs relocating block group %llu flags %llu\n",
  4956. (unsigned long long)block_group->key.objectid,
  4957. (unsigned long long)block_group->flags);
  4958. path = btrfs_alloc_path();
  4959. BUG_ON(!path);
  4960. reloc_inode = create_reloc_inode(info, block_group);
  4961. BUG_ON(IS_ERR(reloc_inode));
  4962. __alloc_chunk_for_shrink(root, block_group, 1);
  4963. set_block_group_readonly(block_group);
  4964. btrfs_start_delalloc_inodes(info->tree_root);
  4965. btrfs_wait_ordered_extents(info->tree_root, 0);
  4966. again:
  4967. skipped = 0;
  4968. total_found = 0;
  4969. progress = 0;
  4970. key.objectid = block_group->key.objectid;
  4971. key.offset = 0;
  4972. key.type = 0;
  4973. cur_byte = key.objectid;
  4974. trans = btrfs_start_transaction(info->tree_root, 1);
  4975. btrfs_commit_transaction(trans, info->tree_root);
  4976. mutex_lock(&root->fs_info->cleaner_mutex);
  4977. btrfs_clean_old_snapshots(info->tree_root);
  4978. btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
  4979. mutex_unlock(&root->fs_info->cleaner_mutex);
  4980. while(1) {
  4981. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4982. if (ret < 0)
  4983. goto out;
  4984. next:
  4985. leaf = path->nodes[0];
  4986. nritems = btrfs_header_nritems(leaf);
  4987. if (path->slots[0] >= nritems) {
  4988. ret = btrfs_next_leaf(root, path);
  4989. if (ret < 0)
  4990. goto out;
  4991. if (ret == 1) {
  4992. ret = 0;
  4993. break;
  4994. }
  4995. leaf = path->nodes[0];
  4996. nritems = btrfs_header_nritems(leaf);
  4997. }
  4998. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4999. if (key.objectid >= block_group->key.objectid +
  5000. block_group->key.offset)
  5001. break;
  5002. if (progress && need_resched()) {
  5003. btrfs_release_path(root, path);
  5004. cond_resched();
  5005. progress = 0;
  5006. continue;
  5007. }
  5008. progress = 1;
  5009. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
  5010. key.objectid + key.offset <= cur_byte) {
  5011. path->slots[0]++;
  5012. goto next;
  5013. }
  5014. total_found++;
  5015. cur_byte = key.objectid + key.offset;
  5016. btrfs_release_path(root, path);
  5017. __alloc_chunk_for_shrink(root, block_group, 0);
  5018. ret = relocate_one_extent(root, path, &key, block_group,
  5019. reloc_inode, pass);
  5020. BUG_ON(ret < 0);
  5021. if (ret > 0)
  5022. skipped++;
  5023. key.objectid = cur_byte;
  5024. key.type = 0;
  5025. key.offset = 0;
  5026. }
  5027. btrfs_release_path(root, path);
  5028. if (pass == 0) {
  5029. btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
  5030. invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
  5031. WARN_ON(reloc_inode->i_mapping->nrpages);
  5032. }
  5033. if (total_found > 0) {
  5034. printk("btrfs found %llu extents in pass %d\n",
  5035. (unsigned long long)total_found, pass);
  5036. pass++;
  5037. if (total_found == skipped && pass > 2) {
  5038. iput(reloc_inode);
  5039. reloc_inode = create_reloc_inode(info, block_group);
  5040. pass = 0;
  5041. }
  5042. goto again;
  5043. }
  5044. /* delete reloc_inode */
  5045. iput(reloc_inode);
  5046. /* unpin extents in this range */
  5047. trans = btrfs_start_transaction(info->tree_root, 1);
  5048. btrfs_commit_transaction(trans, info->tree_root);
  5049. spin_lock(&block_group->lock);
  5050. WARN_ON(block_group->pinned > 0);
  5051. WARN_ON(block_group->reserved > 0);
  5052. WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
  5053. spin_unlock(&block_group->lock);
  5054. ret = 0;
  5055. out:
  5056. btrfs_free_path(path);
  5057. return ret;
  5058. }
  5059. static int find_first_block_group(struct btrfs_root *root,
  5060. struct btrfs_path *path, struct btrfs_key *key)
  5061. {
  5062. int ret = 0;
  5063. struct btrfs_key found_key;
  5064. struct extent_buffer *leaf;
  5065. int slot;
  5066. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  5067. if (ret < 0)
  5068. goto out;
  5069. while(1) {
  5070. slot = path->slots[0];
  5071. leaf = path->nodes[0];
  5072. if (slot >= btrfs_header_nritems(leaf)) {
  5073. ret = btrfs_next_leaf(root, path);
  5074. if (ret == 0)
  5075. continue;
  5076. if (ret < 0)
  5077. goto out;
  5078. break;
  5079. }
  5080. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  5081. if (found_key.objectid >= key->objectid &&
  5082. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  5083. ret = 0;
  5084. goto out;
  5085. }
  5086. path->slots[0]++;
  5087. }
  5088. ret = -ENOENT;
  5089. out:
  5090. return ret;
  5091. }
  5092. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  5093. {
  5094. struct btrfs_block_group_cache *block_group;
  5095. struct rb_node *n;
  5096. spin_lock(&info->block_group_cache_lock);
  5097. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  5098. block_group = rb_entry(n, struct btrfs_block_group_cache,
  5099. cache_node);
  5100. rb_erase(&block_group->cache_node,
  5101. &info->block_group_cache_tree);
  5102. spin_unlock(&info->block_group_cache_lock);
  5103. btrfs_remove_free_space_cache(block_group);
  5104. down_write(&block_group->space_info->groups_sem);
  5105. list_del(&block_group->list);
  5106. up_write(&block_group->space_info->groups_sem);
  5107. kfree(block_group);
  5108. spin_lock(&info->block_group_cache_lock);
  5109. }
  5110. spin_unlock(&info->block_group_cache_lock);
  5111. return 0;
  5112. }
  5113. int btrfs_read_block_groups(struct btrfs_root *root)
  5114. {
  5115. struct btrfs_path *path;
  5116. int ret;
  5117. struct btrfs_block_group_cache *cache;
  5118. struct btrfs_fs_info *info = root->fs_info;
  5119. struct btrfs_space_info *space_info;
  5120. struct btrfs_key key;
  5121. struct btrfs_key found_key;
  5122. struct extent_buffer *leaf;
  5123. root = info->extent_root;
  5124. key.objectid = 0;
  5125. key.offset = 0;
  5126. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  5127. path = btrfs_alloc_path();
  5128. if (!path)
  5129. return -ENOMEM;
  5130. while(1) {
  5131. ret = find_first_block_group(root, path, &key);
  5132. if (ret > 0) {
  5133. ret = 0;
  5134. goto error;
  5135. }
  5136. if (ret != 0)
  5137. goto error;
  5138. leaf = path->nodes[0];
  5139. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5140. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  5141. if (!cache) {
  5142. ret = -ENOMEM;
  5143. break;
  5144. }
  5145. spin_lock_init(&cache->lock);
  5146. mutex_init(&cache->alloc_mutex);
  5147. mutex_init(&cache->cache_mutex);
  5148. INIT_LIST_HEAD(&cache->list);
  5149. read_extent_buffer(leaf, &cache->item,
  5150. btrfs_item_ptr_offset(leaf, path->slots[0]),
  5151. sizeof(cache->item));
  5152. memcpy(&cache->key, &found_key, sizeof(found_key));
  5153. key.objectid = found_key.objectid + found_key.offset;
  5154. btrfs_release_path(root, path);
  5155. cache->flags = btrfs_block_group_flags(&cache->item);
  5156. ret = update_space_info(info, cache->flags, found_key.offset,
  5157. btrfs_block_group_used(&cache->item),
  5158. &space_info);
  5159. BUG_ON(ret);
  5160. cache->space_info = space_info;
  5161. down_write(&space_info->groups_sem);
  5162. list_add_tail(&cache->list, &space_info->block_groups);
  5163. up_write(&space_info->groups_sem);
  5164. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  5165. BUG_ON(ret);
  5166. set_avail_alloc_bits(root->fs_info, cache->flags);
  5167. if (btrfs_chunk_readonly(root, cache->key.objectid))
  5168. set_block_group_readonly(cache);
  5169. }
  5170. ret = 0;
  5171. error:
  5172. btrfs_free_path(path);
  5173. return ret;
  5174. }
  5175. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  5176. struct btrfs_root *root, u64 bytes_used,
  5177. u64 type, u64 chunk_objectid, u64 chunk_offset,
  5178. u64 size)
  5179. {
  5180. int ret;
  5181. struct btrfs_root *extent_root;
  5182. struct btrfs_block_group_cache *cache;
  5183. extent_root = root->fs_info->extent_root;
  5184. root->fs_info->last_trans_new_blockgroup = trans->transid;
  5185. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  5186. if (!cache)
  5187. return -ENOMEM;
  5188. cache->key.objectid = chunk_offset;
  5189. cache->key.offset = size;
  5190. spin_lock_init(&cache->lock);
  5191. mutex_init(&cache->alloc_mutex);
  5192. mutex_init(&cache->cache_mutex);
  5193. INIT_LIST_HEAD(&cache->list);
  5194. btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  5195. btrfs_set_block_group_used(&cache->item, bytes_used);
  5196. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  5197. cache->flags = type;
  5198. btrfs_set_block_group_flags(&cache->item, type);
  5199. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  5200. &cache->space_info);
  5201. BUG_ON(ret);
  5202. down_write(&cache->space_info->groups_sem);
  5203. list_add_tail(&cache->list, &cache->space_info->block_groups);
  5204. up_write(&cache->space_info->groups_sem);
  5205. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  5206. BUG_ON(ret);
  5207. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  5208. sizeof(cache->item));
  5209. BUG_ON(ret);
  5210. finish_current_insert(trans, extent_root, 0);
  5211. ret = del_pending_extents(trans, extent_root, 0);
  5212. BUG_ON(ret);
  5213. set_avail_alloc_bits(extent_root->fs_info, type);
  5214. return 0;
  5215. }
  5216. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  5217. struct btrfs_root *root, u64 group_start)
  5218. {
  5219. struct btrfs_path *path;
  5220. struct btrfs_block_group_cache *block_group;
  5221. struct btrfs_key key;
  5222. int ret;
  5223. root = root->fs_info->extent_root;
  5224. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  5225. BUG_ON(!block_group);
  5226. BUG_ON(!block_group->ro);
  5227. memcpy(&key, &block_group->key, sizeof(key));
  5228. path = btrfs_alloc_path();
  5229. BUG_ON(!path);
  5230. btrfs_remove_free_space_cache(block_group);
  5231. rb_erase(&block_group->cache_node,
  5232. &root->fs_info->block_group_cache_tree);
  5233. down_write(&block_group->space_info->groups_sem);
  5234. list_del(&block_group->list);
  5235. up_write(&block_group->space_info->groups_sem);
  5236. spin_lock(&block_group->space_info->lock);
  5237. block_group->space_info->total_bytes -= block_group->key.offset;
  5238. block_group->space_info->bytes_readonly -= block_group->key.offset;
  5239. spin_unlock(&block_group->space_info->lock);
  5240. block_group->space_info->full = 0;
  5241. /*
  5242. memset(shrink_block_group, 0, sizeof(*shrink_block_group));
  5243. kfree(shrink_block_group);
  5244. */
  5245. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  5246. if (ret > 0)
  5247. ret = -EIO;
  5248. if (ret < 0)
  5249. goto out;
  5250. ret = btrfs_del_item(trans, root, path);
  5251. out:
  5252. btrfs_free_path(path);
  5253. return ret;
  5254. }