extent-tree.c 153 KB

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