extent-tree.c 154 KB

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