extent-tree.c 153 KB

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