ctree.c 153 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965
  1. /*
  2. * Copyright (C) 2007,2008 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/slab.h>
  20. #include <linux/rbtree.h>
  21. #include <linux/vmalloc.h>
  22. #include "ctree.h"
  23. #include "disk-io.h"
  24. #include "transaction.h"
  25. #include "print-tree.h"
  26. #include "locking.h"
  27. static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
  28. *root, struct btrfs_path *path, int level);
  29. static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
  30. *root, struct btrfs_key *ins_key,
  31. struct btrfs_path *path, int data_size, int extend);
  32. static int push_node_left(struct btrfs_trans_handle *trans,
  33. struct btrfs_root *root, struct extent_buffer *dst,
  34. struct extent_buffer *src, int empty);
  35. static int balance_node_right(struct btrfs_trans_handle *trans,
  36. struct btrfs_root *root,
  37. struct extent_buffer *dst_buf,
  38. struct extent_buffer *src_buf);
  39. static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
  40. int level, int slot);
  41. static int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
  42. struct extent_buffer *eb);
  43. struct btrfs_path *btrfs_alloc_path(void)
  44. {
  45. struct btrfs_path *path;
  46. path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
  47. return path;
  48. }
  49. /*
  50. * set all locked nodes in the path to blocking locks. This should
  51. * be done before scheduling
  52. */
  53. noinline void btrfs_set_path_blocking(struct btrfs_path *p)
  54. {
  55. int i;
  56. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  57. if (!p->nodes[i] || !p->locks[i])
  58. continue;
  59. btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
  60. if (p->locks[i] == BTRFS_READ_LOCK)
  61. p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
  62. else if (p->locks[i] == BTRFS_WRITE_LOCK)
  63. p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
  64. }
  65. }
  66. /*
  67. * reset all the locked nodes in the patch to spinning locks.
  68. *
  69. * held is used to keep lockdep happy, when lockdep is enabled
  70. * we set held to a blocking lock before we go around and
  71. * retake all the spinlocks in the path. You can safely use NULL
  72. * for held
  73. */
  74. noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
  75. struct extent_buffer *held, int held_rw)
  76. {
  77. int i;
  78. if (held) {
  79. btrfs_set_lock_blocking_rw(held, held_rw);
  80. if (held_rw == BTRFS_WRITE_LOCK)
  81. held_rw = BTRFS_WRITE_LOCK_BLOCKING;
  82. else if (held_rw == BTRFS_READ_LOCK)
  83. held_rw = BTRFS_READ_LOCK_BLOCKING;
  84. }
  85. btrfs_set_path_blocking(p);
  86. for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
  87. if (p->nodes[i] && p->locks[i]) {
  88. btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
  89. if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
  90. p->locks[i] = BTRFS_WRITE_LOCK;
  91. else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
  92. p->locks[i] = BTRFS_READ_LOCK;
  93. }
  94. }
  95. if (held)
  96. btrfs_clear_lock_blocking_rw(held, held_rw);
  97. }
  98. /* this also releases the path */
  99. void btrfs_free_path(struct btrfs_path *p)
  100. {
  101. if (!p)
  102. return;
  103. btrfs_release_path(p);
  104. kmem_cache_free(btrfs_path_cachep, p);
  105. }
  106. /*
  107. * path release drops references on the extent buffers in the path
  108. * and it drops any locks held by this path
  109. *
  110. * It is safe to call this on paths that no locks or extent buffers held.
  111. */
  112. noinline void btrfs_release_path(struct btrfs_path *p)
  113. {
  114. int i;
  115. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  116. p->slots[i] = 0;
  117. if (!p->nodes[i])
  118. continue;
  119. if (p->locks[i]) {
  120. btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
  121. p->locks[i] = 0;
  122. }
  123. free_extent_buffer(p->nodes[i]);
  124. p->nodes[i] = NULL;
  125. }
  126. }
  127. /*
  128. * safely gets a reference on the root node of a tree. A lock
  129. * is not taken, so a concurrent writer may put a different node
  130. * at the root of the tree. See btrfs_lock_root_node for the
  131. * looping required.
  132. *
  133. * The extent buffer returned by this has a reference taken, so
  134. * it won't disappear. It may stop being the root of the tree
  135. * at any time because there are no locks held.
  136. */
  137. struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
  138. {
  139. struct extent_buffer *eb;
  140. while (1) {
  141. rcu_read_lock();
  142. eb = rcu_dereference(root->node);
  143. /*
  144. * RCU really hurts here, we could free up the root node because
  145. * it was COWed but we may not get the new root node yet so do
  146. * the inc_not_zero dance and if it doesn't work then
  147. * synchronize_rcu and try again.
  148. */
  149. if (atomic_inc_not_zero(&eb->refs)) {
  150. rcu_read_unlock();
  151. break;
  152. }
  153. rcu_read_unlock();
  154. synchronize_rcu();
  155. }
  156. return eb;
  157. }
  158. /* loop around taking references on and locking the root node of the
  159. * tree until you end up with a lock on the root. A locked buffer
  160. * is returned, with a reference held.
  161. */
  162. struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
  163. {
  164. struct extent_buffer *eb;
  165. while (1) {
  166. eb = btrfs_root_node(root);
  167. btrfs_tree_lock(eb);
  168. if (eb == root->node)
  169. break;
  170. btrfs_tree_unlock(eb);
  171. free_extent_buffer(eb);
  172. }
  173. return eb;
  174. }
  175. /* loop around taking references on and locking the root node of the
  176. * tree until you end up with a lock on the root. A locked buffer
  177. * is returned, with a reference held.
  178. */
  179. static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
  180. {
  181. struct extent_buffer *eb;
  182. while (1) {
  183. eb = btrfs_root_node(root);
  184. btrfs_tree_read_lock(eb);
  185. if (eb == root->node)
  186. break;
  187. btrfs_tree_read_unlock(eb);
  188. free_extent_buffer(eb);
  189. }
  190. return eb;
  191. }
  192. /* cowonly root (everything not a reference counted cow subvolume), just get
  193. * put onto a simple dirty list. transaction.c walks this to make sure they
  194. * get properly updated on disk.
  195. */
  196. static void add_root_to_dirty_list(struct btrfs_root *root)
  197. {
  198. if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
  199. !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
  200. return;
  201. spin_lock(&root->fs_info->trans_lock);
  202. if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
  203. /* Want the extent tree to be the last on the list */
  204. if (root->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  205. list_move_tail(&root->dirty_list,
  206. &root->fs_info->dirty_cowonly_roots);
  207. else
  208. list_move(&root->dirty_list,
  209. &root->fs_info->dirty_cowonly_roots);
  210. }
  211. spin_unlock(&root->fs_info->trans_lock);
  212. }
  213. /*
  214. * used by snapshot creation to make a copy of a root for a tree with
  215. * a given objectid. The buffer with the new root node is returned in
  216. * cow_ret, and this func returns zero on success or a negative error code.
  217. */
  218. int btrfs_copy_root(struct btrfs_trans_handle *trans,
  219. struct btrfs_root *root,
  220. struct extent_buffer *buf,
  221. struct extent_buffer **cow_ret, u64 new_root_objectid)
  222. {
  223. struct extent_buffer *cow;
  224. int ret = 0;
  225. int level;
  226. struct btrfs_disk_key disk_key;
  227. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  228. trans->transid != root->fs_info->running_transaction->transid);
  229. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  230. trans->transid != root->last_trans);
  231. level = btrfs_header_level(buf);
  232. if (level == 0)
  233. btrfs_item_key(buf, &disk_key, 0);
  234. else
  235. btrfs_node_key(buf, &disk_key, 0);
  236. cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
  237. &disk_key, level, buf->start, 0);
  238. if (IS_ERR(cow))
  239. return PTR_ERR(cow);
  240. copy_extent_buffer(cow, buf, 0, 0, cow->len);
  241. btrfs_set_header_bytenr(cow, cow->start);
  242. btrfs_set_header_generation(cow, trans->transid);
  243. btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
  244. btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
  245. BTRFS_HEADER_FLAG_RELOC);
  246. if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  247. btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
  248. else
  249. btrfs_set_header_owner(cow, new_root_objectid);
  250. write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
  251. BTRFS_FSID_SIZE);
  252. WARN_ON(btrfs_header_generation(buf) > trans->transid);
  253. if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  254. ret = btrfs_inc_ref(trans, root, cow, 1);
  255. else
  256. ret = btrfs_inc_ref(trans, root, cow, 0);
  257. if (ret)
  258. return ret;
  259. btrfs_mark_buffer_dirty(cow);
  260. *cow_ret = cow;
  261. return 0;
  262. }
  263. enum mod_log_op {
  264. MOD_LOG_KEY_REPLACE,
  265. MOD_LOG_KEY_ADD,
  266. MOD_LOG_KEY_REMOVE,
  267. MOD_LOG_KEY_REMOVE_WHILE_FREEING,
  268. MOD_LOG_KEY_REMOVE_WHILE_MOVING,
  269. MOD_LOG_MOVE_KEYS,
  270. MOD_LOG_ROOT_REPLACE,
  271. };
  272. struct tree_mod_move {
  273. int dst_slot;
  274. int nr_items;
  275. };
  276. struct tree_mod_root {
  277. u64 logical;
  278. u8 level;
  279. };
  280. struct tree_mod_elem {
  281. struct rb_node node;
  282. u64 logical;
  283. u64 seq;
  284. enum mod_log_op op;
  285. /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
  286. int slot;
  287. /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
  288. u64 generation;
  289. /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
  290. struct btrfs_disk_key key;
  291. u64 blockptr;
  292. /* this is used for op == MOD_LOG_MOVE_KEYS */
  293. struct tree_mod_move move;
  294. /* this is used for op == MOD_LOG_ROOT_REPLACE */
  295. struct tree_mod_root old_root;
  296. };
  297. static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
  298. {
  299. read_lock(&fs_info->tree_mod_log_lock);
  300. }
  301. static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
  302. {
  303. read_unlock(&fs_info->tree_mod_log_lock);
  304. }
  305. static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
  306. {
  307. write_lock(&fs_info->tree_mod_log_lock);
  308. }
  309. static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
  310. {
  311. write_unlock(&fs_info->tree_mod_log_lock);
  312. }
  313. /*
  314. * Pull a new tree mod seq number for our operation.
  315. */
  316. static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
  317. {
  318. return atomic64_inc_return(&fs_info->tree_mod_seq);
  319. }
  320. /*
  321. * This adds a new blocker to the tree mod log's blocker list if the @elem
  322. * passed does not already have a sequence number set. So when a caller expects
  323. * to record tree modifications, it should ensure to set elem->seq to zero
  324. * before calling btrfs_get_tree_mod_seq.
  325. * Returns a fresh, unused tree log modification sequence number, even if no new
  326. * blocker was added.
  327. */
  328. u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
  329. struct seq_list *elem)
  330. {
  331. tree_mod_log_write_lock(fs_info);
  332. spin_lock(&fs_info->tree_mod_seq_lock);
  333. if (!elem->seq) {
  334. elem->seq = btrfs_inc_tree_mod_seq(fs_info);
  335. list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
  336. }
  337. spin_unlock(&fs_info->tree_mod_seq_lock);
  338. tree_mod_log_write_unlock(fs_info);
  339. return elem->seq;
  340. }
  341. void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
  342. struct seq_list *elem)
  343. {
  344. struct rb_root *tm_root;
  345. struct rb_node *node;
  346. struct rb_node *next;
  347. struct seq_list *cur_elem;
  348. struct tree_mod_elem *tm;
  349. u64 min_seq = (u64)-1;
  350. u64 seq_putting = elem->seq;
  351. if (!seq_putting)
  352. return;
  353. spin_lock(&fs_info->tree_mod_seq_lock);
  354. list_del(&elem->list);
  355. elem->seq = 0;
  356. list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
  357. if (cur_elem->seq < min_seq) {
  358. if (seq_putting > cur_elem->seq) {
  359. /*
  360. * blocker with lower sequence number exists, we
  361. * cannot remove anything from the log
  362. */
  363. spin_unlock(&fs_info->tree_mod_seq_lock);
  364. return;
  365. }
  366. min_seq = cur_elem->seq;
  367. }
  368. }
  369. spin_unlock(&fs_info->tree_mod_seq_lock);
  370. /*
  371. * anything that's lower than the lowest existing (read: blocked)
  372. * sequence number can be removed from the tree.
  373. */
  374. tree_mod_log_write_lock(fs_info);
  375. tm_root = &fs_info->tree_mod_log;
  376. for (node = rb_first(tm_root); node; node = next) {
  377. next = rb_next(node);
  378. tm = container_of(node, struct tree_mod_elem, node);
  379. if (tm->seq > min_seq)
  380. continue;
  381. rb_erase(node, tm_root);
  382. kfree(tm);
  383. }
  384. tree_mod_log_write_unlock(fs_info);
  385. }
  386. /*
  387. * key order of the log:
  388. * node/leaf start address -> sequence
  389. *
  390. * The 'start address' is the logical address of the *new* root node
  391. * for root replace operations, or the logical address of the affected
  392. * block for all other operations.
  393. *
  394. * Note: must be called with write lock (tree_mod_log_write_lock).
  395. */
  396. static noinline int
  397. __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
  398. {
  399. struct rb_root *tm_root;
  400. struct rb_node **new;
  401. struct rb_node *parent = NULL;
  402. struct tree_mod_elem *cur;
  403. BUG_ON(!tm);
  404. tm->seq = btrfs_inc_tree_mod_seq(fs_info);
  405. tm_root = &fs_info->tree_mod_log;
  406. new = &tm_root->rb_node;
  407. while (*new) {
  408. cur = container_of(*new, struct tree_mod_elem, node);
  409. parent = *new;
  410. if (cur->logical < tm->logical)
  411. new = &((*new)->rb_left);
  412. else if (cur->logical > tm->logical)
  413. new = &((*new)->rb_right);
  414. else if (cur->seq < tm->seq)
  415. new = &((*new)->rb_left);
  416. else if (cur->seq > tm->seq)
  417. new = &((*new)->rb_right);
  418. else
  419. return -EEXIST;
  420. }
  421. rb_link_node(&tm->node, parent, new);
  422. rb_insert_color(&tm->node, tm_root);
  423. return 0;
  424. }
  425. /*
  426. * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
  427. * returns zero with the tree_mod_log_lock acquired. The caller must hold
  428. * this until all tree mod log insertions are recorded in the rb tree and then
  429. * call tree_mod_log_write_unlock() to release.
  430. */
  431. static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
  432. struct extent_buffer *eb) {
  433. smp_mb();
  434. if (list_empty(&(fs_info)->tree_mod_seq_list))
  435. return 1;
  436. if (eb && btrfs_header_level(eb) == 0)
  437. return 1;
  438. tree_mod_log_write_lock(fs_info);
  439. if (list_empty(&(fs_info)->tree_mod_seq_list)) {
  440. tree_mod_log_write_unlock(fs_info);
  441. return 1;
  442. }
  443. return 0;
  444. }
  445. /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
  446. static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
  447. struct extent_buffer *eb)
  448. {
  449. smp_mb();
  450. if (list_empty(&(fs_info)->tree_mod_seq_list))
  451. return 0;
  452. if (eb && btrfs_header_level(eb) == 0)
  453. return 0;
  454. return 1;
  455. }
  456. static struct tree_mod_elem *
  457. alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
  458. enum mod_log_op op, gfp_t flags)
  459. {
  460. struct tree_mod_elem *tm;
  461. tm = kzalloc(sizeof(*tm), flags);
  462. if (!tm)
  463. return NULL;
  464. tm->logical = eb->start;
  465. if (op != MOD_LOG_KEY_ADD) {
  466. btrfs_node_key(eb, &tm->key, slot);
  467. tm->blockptr = btrfs_node_blockptr(eb, slot);
  468. }
  469. tm->op = op;
  470. tm->slot = slot;
  471. tm->generation = btrfs_node_ptr_generation(eb, slot);
  472. RB_CLEAR_NODE(&tm->node);
  473. return tm;
  474. }
  475. static noinline int
  476. tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
  477. struct extent_buffer *eb, int slot,
  478. enum mod_log_op op, gfp_t flags)
  479. {
  480. struct tree_mod_elem *tm;
  481. int ret;
  482. if (!tree_mod_need_log(fs_info, eb))
  483. return 0;
  484. tm = alloc_tree_mod_elem(eb, slot, op, flags);
  485. if (!tm)
  486. return -ENOMEM;
  487. if (tree_mod_dont_log(fs_info, eb)) {
  488. kfree(tm);
  489. return 0;
  490. }
  491. ret = __tree_mod_log_insert(fs_info, tm);
  492. tree_mod_log_write_unlock(fs_info);
  493. if (ret)
  494. kfree(tm);
  495. return ret;
  496. }
  497. static noinline int
  498. tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
  499. struct extent_buffer *eb, int dst_slot, int src_slot,
  500. int nr_items, gfp_t flags)
  501. {
  502. struct tree_mod_elem *tm = NULL;
  503. struct tree_mod_elem **tm_list = NULL;
  504. int ret = 0;
  505. int i;
  506. int locked = 0;
  507. if (!tree_mod_need_log(fs_info, eb))
  508. return 0;
  509. tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), flags);
  510. if (!tm_list)
  511. return -ENOMEM;
  512. tm = kzalloc(sizeof(*tm), flags);
  513. if (!tm) {
  514. ret = -ENOMEM;
  515. goto free_tms;
  516. }
  517. tm->logical = eb->start;
  518. tm->slot = src_slot;
  519. tm->move.dst_slot = dst_slot;
  520. tm->move.nr_items = nr_items;
  521. tm->op = MOD_LOG_MOVE_KEYS;
  522. for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
  523. tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
  524. MOD_LOG_KEY_REMOVE_WHILE_MOVING, flags);
  525. if (!tm_list[i]) {
  526. ret = -ENOMEM;
  527. goto free_tms;
  528. }
  529. }
  530. if (tree_mod_dont_log(fs_info, eb))
  531. goto free_tms;
  532. locked = 1;
  533. /*
  534. * When we override something during the move, we log these removals.
  535. * This can only happen when we move towards the beginning of the
  536. * buffer, i.e. dst_slot < src_slot.
  537. */
  538. for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
  539. ret = __tree_mod_log_insert(fs_info, tm_list[i]);
  540. if (ret)
  541. goto free_tms;
  542. }
  543. ret = __tree_mod_log_insert(fs_info, tm);
  544. if (ret)
  545. goto free_tms;
  546. tree_mod_log_write_unlock(fs_info);
  547. kfree(tm_list);
  548. return 0;
  549. free_tms:
  550. for (i = 0; i < nr_items; i++) {
  551. if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
  552. rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
  553. kfree(tm_list[i]);
  554. }
  555. if (locked)
  556. tree_mod_log_write_unlock(fs_info);
  557. kfree(tm_list);
  558. kfree(tm);
  559. return ret;
  560. }
  561. static inline int
  562. __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
  563. struct tree_mod_elem **tm_list,
  564. int nritems)
  565. {
  566. int i, j;
  567. int ret;
  568. for (i = nritems - 1; i >= 0; i--) {
  569. ret = __tree_mod_log_insert(fs_info, tm_list[i]);
  570. if (ret) {
  571. for (j = nritems - 1; j > i; j--)
  572. rb_erase(&tm_list[j]->node,
  573. &fs_info->tree_mod_log);
  574. return ret;
  575. }
  576. }
  577. return 0;
  578. }
  579. static noinline int
  580. tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
  581. struct extent_buffer *old_root,
  582. struct extent_buffer *new_root, gfp_t flags,
  583. int log_removal)
  584. {
  585. struct tree_mod_elem *tm = NULL;
  586. struct tree_mod_elem **tm_list = NULL;
  587. int nritems = 0;
  588. int ret = 0;
  589. int i;
  590. if (!tree_mod_need_log(fs_info, NULL))
  591. return 0;
  592. if (log_removal && btrfs_header_level(old_root) > 0) {
  593. nritems = btrfs_header_nritems(old_root);
  594. tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
  595. flags);
  596. if (!tm_list) {
  597. ret = -ENOMEM;
  598. goto free_tms;
  599. }
  600. for (i = 0; i < nritems; i++) {
  601. tm_list[i] = alloc_tree_mod_elem(old_root, i,
  602. MOD_LOG_KEY_REMOVE_WHILE_FREEING, flags);
  603. if (!tm_list[i]) {
  604. ret = -ENOMEM;
  605. goto free_tms;
  606. }
  607. }
  608. }
  609. tm = kzalloc(sizeof(*tm), flags);
  610. if (!tm) {
  611. ret = -ENOMEM;
  612. goto free_tms;
  613. }
  614. tm->logical = new_root->start;
  615. tm->old_root.logical = old_root->start;
  616. tm->old_root.level = btrfs_header_level(old_root);
  617. tm->generation = btrfs_header_generation(old_root);
  618. tm->op = MOD_LOG_ROOT_REPLACE;
  619. if (tree_mod_dont_log(fs_info, NULL))
  620. goto free_tms;
  621. if (tm_list)
  622. ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
  623. if (!ret)
  624. ret = __tree_mod_log_insert(fs_info, tm);
  625. tree_mod_log_write_unlock(fs_info);
  626. if (ret)
  627. goto free_tms;
  628. kfree(tm_list);
  629. return ret;
  630. free_tms:
  631. if (tm_list) {
  632. for (i = 0; i < nritems; i++)
  633. kfree(tm_list[i]);
  634. kfree(tm_list);
  635. }
  636. kfree(tm);
  637. return ret;
  638. }
  639. static struct tree_mod_elem *
  640. __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
  641. int smallest)
  642. {
  643. struct rb_root *tm_root;
  644. struct rb_node *node;
  645. struct tree_mod_elem *cur = NULL;
  646. struct tree_mod_elem *found = NULL;
  647. tree_mod_log_read_lock(fs_info);
  648. tm_root = &fs_info->tree_mod_log;
  649. node = tm_root->rb_node;
  650. while (node) {
  651. cur = container_of(node, struct tree_mod_elem, node);
  652. if (cur->logical < start) {
  653. node = node->rb_left;
  654. } else if (cur->logical > start) {
  655. node = node->rb_right;
  656. } else if (cur->seq < min_seq) {
  657. node = node->rb_left;
  658. } else if (!smallest) {
  659. /* we want the node with the highest seq */
  660. if (found)
  661. BUG_ON(found->seq > cur->seq);
  662. found = cur;
  663. node = node->rb_left;
  664. } else if (cur->seq > min_seq) {
  665. /* we want the node with the smallest seq */
  666. if (found)
  667. BUG_ON(found->seq < cur->seq);
  668. found = cur;
  669. node = node->rb_right;
  670. } else {
  671. found = cur;
  672. break;
  673. }
  674. }
  675. tree_mod_log_read_unlock(fs_info);
  676. return found;
  677. }
  678. /*
  679. * this returns the element from the log with the smallest time sequence
  680. * value that's in the log (the oldest log item). any element with a time
  681. * sequence lower than min_seq will be ignored.
  682. */
  683. static struct tree_mod_elem *
  684. tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
  685. u64 min_seq)
  686. {
  687. return __tree_mod_log_search(fs_info, start, min_seq, 1);
  688. }
  689. /*
  690. * this returns the element from the log with the largest time sequence
  691. * value that's in the log (the most recent log item). any element with
  692. * a time sequence lower than min_seq will be ignored.
  693. */
  694. static struct tree_mod_elem *
  695. tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
  696. {
  697. return __tree_mod_log_search(fs_info, start, min_seq, 0);
  698. }
  699. static noinline int
  700. tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
  701. struct extent_buffer *src, unsigned long dst_offset,
  702. unsigned long src_offset, int nr_items)
  703. {
  704. int ret = 0;
  705. struct tree_mod_elem **tm_list = NULL;
  706. struct tree_mod_elem **tm_list_add, **tm_list_rem;
  707. int i;
  708. int locked = 0;
  709. if (!tree_mod_need_log(fs_info, NULL))
  710. return 0;
  711. if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
  712. return 0;
  713. tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
  714. GFP_NOFS);
  715. if (!tm_list)
  716. return -ENOMEM;
  717. tm_list_add = tm_list;
  718. tm_list_rem = tm_list + nr_items;
  719. for (i = 0; i < nr_items; i++) {
  720. tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
  721. MOD_LOG_KEY_REMOVE, GFP_NOFS);
  722. if (!tm_list_rem[i]) {
  723. ret = -ENOMEM;
  724. goto free_tms;
  725. }
  726. tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
  727. MOD_LOG_KEY_ADD, GFP_NOFS);
  728. if (!tm_list_add[i]) {
  729. ret = -ENOMEM;
  730. goto free_tms;
  731. }
  732. }
  733. if (tree_mod_dont_log(fs_info, NULL))
  734. goto free_tms;
  735. locked = 1;
  736. for (i = 0; i < nr_items; i++) {
  737. ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
  738. if (ret)
  739. goto free_tms;
  740. ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
  741. if (ret)
  742. goto free_tms;
  743. }
  744. tree_mod_log_write_unlock(fs_info);
  745. kfree(tm_list);
  746. return 0;
  747. free_tms:
  748. for (i = 0; i < nr_items * 2; i++) {
  749. if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
  750. rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
  751. kfree(tm_list[i]);
  752. }
  753. if (locked)
  754. tree_mod_log_write_unlock(fs_info);
  755. kfree(tm_list);
  756. return ret;
  757. }
  758. static inline void
  759. tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
  760. int dst_offset, int src_offset, int nr_items)
  761. {
  762. int ret;
  763. ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
  764. nr_items, GFP_NOFS);
  765. BUG_ON(ret < 0);
  766. }
  767. static noinline void
  768. tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
  769. struct extent_buffer *eb, int slot, int atomic)
  770. {
  771. int ret;
  772. ret = tree_mod_log_insert_key(fs_info, eb, slot,
  773. MOD_LOG_KEY_REPLACE,
  774. atomic ? GFP_ATOMIC : GFP_NOFS);
  775. BUG_ON(ret < 0);
  776. }
  777. static noinline int
  778. tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
  779. {
  780. struct tree_mod_elem **tm_list = NULL;
  781. int nritems = 0;
  782. int i;
  783. int ret = 0;
  784. if (btrfs_header_level(eb) == 0)
  785. return 0;
  786. if (!tree_mod_need_log(fs_info, NULL))
  787. return 0;
  788. nritems = btrfs_header_nritems(eb);
  789. tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
  790. if (!tm_list)
  791. return -ENOMEM;
  792. for (i = 0; i < nritems; i++) {
  793. tm_list[i] = alloc_tree_mod_elem(eb, i,
  794. MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
  795. if (!tm_list[i]) {
  796. ret = -ENOMEM;
  797. goto free_tms;
  798. }
  799. }
  800. if (tree_mod_dont_log(fs_info, eb))
  801. goto free_tms;
  802. ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
  803. tree_mod_log_write_unlock(fs_info);
  804. if (ret)
  805. goto free_tms;
  806. kfree(tm_list);
  807. return 0;
  808. free_tms:
  809. for (i = 0; i < nritems; i++)
  810. kfree(tm_list[i]);
  811. kfree(tm_list);
  812. return ret;
  813. }
  814. static noinline void
  815. tree_mod_log_set_root_pointer(struct btrfs_root *root,
  816. struct extent_buffer *new_root_node,
  817. int log_removal)
  818. {
  819. int ret;
  820. ret = tree_mod_log_insert_root(root->fs_info, root->node,
  821. new_root_node, GFP_NOFS, log_removal);
  822. BUG_ON(ret < 0);
  823. }
  824. /*
  825. * check if the tree block can be shared by multiple trees
  826. */
  827. int btrfs_block_can_be_shared(struct btrfs_root *root,
  828. struct extent_buffer *buf)
  829. {
  830. /*
  831. * Tree blocks not in reference counted trees and tree roots
  832. * are never shared. If a block was allocated after the last
  833. * snapshot and the block was not allocated by tree relocation,
  834. * we know the block is not shared.
  835. */
  836. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  837. buf != root->node && buf != root->commit_root &&
  838. (btrfs_header_generation(buf) <=
  839. btrfs_root_last_snapshot(&root->root_item) ||
  840. btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
  841. return 1;
  842. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  843. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  844. btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
  845. return 1;
  846. #endif
  847. return 0;
  848. }
  849. static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
  850. struct btrfs_root *root,
  851. struct extent_buffer *buf,
  852. struct extent_buffer *cow,
  853. int *last_ref)
  854. {
  855. u64 refs;
  856. u64 owner;
  857. u64 flags;
  858. u64 new_flags = 0;
  859. int ret;
  860. /*
  861. * Backrefs update rules:
  862. *
  863. * Always use full backrefs for extent pointers in tree block
  864. * allocated by tree relocation.
  865. *
  866. * If a shared tree block is no longer referenced by its owner
  867. * tree (btrfs_header_owner(buf) == root->root_key.objectid),
  868. * use full backrefs for extent pointers in tree block.
  869. *
  870. * If a tree block is been relocating
  871. * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
  872. * use full backrefs for extent pointers in tree block.
  873. * The reason for this is some operations (such as drop tree)
  874. * are only allowed for blocks use full backrefs.
  875. */
  876. if (btrfs_block_can_be_shared(root, buf)) {
  877. ret = btrfs_lookup_extent_info(trans, root, buf->start,
  878. btrfs_header_level(buf), 1,
  879. &refs, &flags);
  880. if (ret)
  881. return ret;
  882. if (refs == 0) {
  883. ret = -EROFS;
  884. btrfs_handle_fs_error(root->fs_info, ret, NULL);
  885. return ret;
  886. }
  887. } else {
  888. refs = 1;
  889. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  890. btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
  891. flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  892. else
  893. flags = 0;
  894. }
  895. owner = btrfs_header_owner(buf);
  896. BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
  897. !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  898. if (refs > 1) {
  899. if ((owner == root->root_key.objectid ||
  900. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
  901. !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
  902. ret = btrfs_inc_ref(trans, root, buf, 1);
  903. BUG_ON(ret); /* -ENOMEM */
  904. if (root->root_key.objectid ==
  905. BTRFS_TREE_RELOC_OBJECTID) {
  906. ret = btrfs_dec_ref(trans, root, buf, 0);
  907. BUG_ON(ret); /* -ENOMEM */
  908. ret = btrfs_inc_ref(trans, root, cow, 1);
  909. BUG_ON(ret); /* -ENOMEM */
  910. }
  911. new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  912. } else {
  913. if (root->root_key.objectid ==
  914. BTRFS_TREE_RELOC_OBJECTID)
  915. ret = btrfs_inc_ref(trans, root, cow, 1);
  916. else
  917. ret = btrfs_inc_ref(trans, root, cow, 0);
  918. BUG_ON(ret); /* -ENOMEM */
  919. }
  920. if (new_flags != 0) {
  921. int level = btrfs_header_level(buf);
  922. ret = btrfs_set_disk_extent_flags(trans, root,
  923. buf->start,
  924. buf->len,
  925. new_flags, level, 0);
  926. if (ret)
  927. return ret;
  928. }
  929. } else {
  930. if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  931. if (root->root_key.objectid ==
  932. BTRFS_TREE_RELOC_OBJECTID)
  933. ret = btrfs_inc_ref(trans, root, cow, 1);
  934. else
  935. ret = btrfs_inc_ref(trans, root, cow, 0);
  936. BUG_ON(ret); /* -ENOMEM */
  937. ret = btrfs_dec_ref(trans, root, buf, 1);
  938. BUG_ON(ret); /* -ENOMEM */
  939. }
  940. clean_tree_block(trans, root->fs_info, buf);
  941. *last_ref = 1;
  942. }
  943. return 0;
  944. }
  945. /*
  946. * does the dirty work in cow of a single block. The parent block (if
  947. * supplied) is updated to point to the new cow copy. The new buffer is marked
  948. * dirty and returned locked. If you modify the block it needs to be marked
  949. * dirty again.
  950. *
  951. * search_start -- an allocation hint for the new block
  952. *
  953. * empty_size -- a hint that you plan on doing more cow. This is the size in
  954. * bytes the allocator should try to find free next to the block it returns.
  955. * This is just a hint and may be ignored by the allocator.
  956. */
  957. static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
  958. struct btrfs_root *root,
  959. struct extent_buffer *buf,
  960. struct extent_buffer *parent, int parent_slot,
  961. struct extent_buffer **cow_ret,
  962. u64 search_start, u64 empty_size)
  963. {
  964. struct btrfs_disk_key disk_key;
  965. struct extent_buffer *cow;
  966. int level, ret;
  967. int last_ref = 0;
  968. int unlock_orig = 0;
  969. u64 parent_start;
  970. if (*cow_ret == buf)
  971. unlock_orig = 1;
  972. btrfs_assert_tree_locked(buf);
  973. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  974. trans->transid != root->fs_info->running_transaction->transid);
  975. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  976. trans->transid != root->last_trans);
  977. level = btrfs_header_level(buf);
  978. if (level == 0)
  979. btrfs_item_key(buf, &disk_key, 0);
  980. else
  981. btrfs_node_key(buf, &disk_key, 0);
  982. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
  983. if (parent)
  984. parent_start = parent->start;
  985. else
  986. parent_start = 0;
  987. } else
  988. parent_start = 0;
  989. cow = btrfs_alloc_tree_block(trans, root, parent_start,
  990. root->root_key.objectid, &disk_key, level,
  991. search_start, empty_size);
  992. if (IS_ERR(cow))
  993. return PTR_ERR(cow);
  994. /* cow is set to blocking by btrfs_init_new_buffer */
  995. copy_extent_buffer(cow, buf, 0, 0, cow->len);
  996. btrfs_set_header_bytenr(cow, cow->start);
  997. btrfs_set_header_generation(cow, trans->transid);
  998. btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
  999. btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
  1000. BTRFS_HEADER_FLAG_RELOC);
  1001. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
  1002. btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
  1003. else
  1004. btrfs_set_header_owner(cow, root->root_key.objectid);
  1005. write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
  1006. BTRFS_FSID_SIZE);
  1007. ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
  1008. if (ret) {
  1009. btrfs_abort_transaction(trans, ret);
  1010. return ret;
  1011. }
  1012. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
  1013. ret = btrfs_reloc_cow_block(trans, root, buf, cow);
  1014. if (ret) {
  1015. btrfs_abort_transaction(trans, ret);
  1016. return ret;
  1017. }
  1018. }
  1019. if (buf == root->node) {
  1020. WARN_ON(parent && parent != buf);
  1021. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  1022. btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
  1023. parent_start = buf->start;
  1024. else
  1025. parent_start = 0;
  1026. extent_buffer_get(cow);
  1027. tree_mod_log_set_root_pointer(root, cow, 1);
  1028. rcu_assign_pointer(root->node, cow);
  1029. btrfs_free_tree_block(trans, root, buf, parent_start,
  1030. last_ref);
  1031. free_extent_buffer(buf);
  1032. add_root_to_dirty_list(root);
  1033. } else {
  1034. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
  1035. parent_start = parent->start;
  1036. else
  1037. parent_start = 0;
  1038. WARN_ON(trans->transid != btrfs_header_generation(parent));
  1039. tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
  1040. MOD_LOG_KEY_REPLACE, GFP_NOFS);
  1041. btrfs_set_node_blockptr(parent, parent_slot,
  1042. cow->start);
  1043. btrfs_set_node_ptr_generation(parent, parent_slot,
  1044. trans->transid);
  1045. btrfs_mark_buffer_dirty(parent);
  1046. if (last_ref) {
  1047. ret = tree_mod_log_free_eb(root->fs_info, buf);
  1048. if (ret) {
  1049. btrfs_abort_transaction(trans, ret);
  1050. return ret;
  1051. }
  1052. }
  1053. btrfs_free_tree_block(trans, root, buf, parent_start,
  1054. last_ref);
  1055. }
  1056. if (unlock_orig)
  1057. btrfs_tree_unlock(buf);
  1058. free_extent_buffer_stale(buf);
  1059. btrfs_mark_buffer_dirty(cow);
  1060. *cow_ret = cow;
  1061. return 0;
  1062. }
  1063. /*
  1064. * returns the logical address of the oldest predecessor of the given root.
  1065. * entries older than time_seq are ignored.
  1066. */
  1067. static struct tree_mod_elem *
  1068. __tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
  1069. struct extent_buffer *eb_root, u64 time_seq)
  1070. {
  1071. struct tree_mod_elem *tm;
  1072. struct tree_mod_elem *found = NULL;
  1073. u64 root_logical = eb_root->start;
  1074. int looped = 0;
  1075. if (!time_seq)
  1076. return NULL;
  1077. /*
  1078. * the very last operation that's logged for a root is the
  1079. * replacement operation (if it is replaced at all). this has
  1080. * the logical address of the *new* root, making it the very
  1081. * first operation that's logged for this root.
  1082. */
  1083. while (1) {
  1084. tm = tree_mod_log_search_oldest(fs_info, root_logical,
  1085. time_seq);
  1086. if (!looped && !tm)
  1087. return NULL;
  1088. /*
  1089. * if there are no tree operation for the oldest root, we simply
  1090. * return it. this should only happen if that (old) root is at
  1091. * level 0.
  1092. */
  1093. if (!tm)
  1094. break;
  1095. /*
  1096. * if there's an operation that's not a root replacement, we
  1097. * found the oldest version of our root. normally, we'll find a
  1098. * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
  1099. */
  1100. if (tm->op != MOD_LOG_ROOT_REPLACE)
  1101. break;
  1102. found = tm;
  1103. root_logical = tm->old_root.logical;
  1104. looped = 1;
  1105. }
  1106. /* if there's no old root to return, return what we found instead */
  1107. if (!found)
  1108. found = tm;
  1109. return found;
  1110. }
  1111. /*
  1112. * tm is a pointer to the first operation to rewind within eb. then, all
  1113. * previous operations will be rewound (until we reach something older than
  1114. * time_seq).
  1115. */
  1116. static void
  1117. __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
  1118. u64 time_seq, struct tree_mod_elem *first_tm)
  1119. {
  1120. u32 n;
  1121. struct rb_node *next;
  1122. struct tree_mod_elem *tm = first_tm;
  1123. unsigned long o_dst;
  1124. unsigned long o_src;
  1125. unsigned long p_size = sizeof(struct btrfs_key_ptr);
  1126. n = btrfs_header_nritems(eb);
  1127. tree_mod_log_read_lock(fs_info);
  1128. while (tm && tm->seq >= time_seq) {
  1129. /*
  1130. * all the operations are recorded with the operator used for
  1131. * the modification. as we're going backwards, we do the
  1132. * opposite of each operation here.
  1133. */
  1134. switch (tm->op) {
  1135. case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
  1136. BUG_ON(tm->slot < n);
  1137. /* Fallthrough */
  1138. case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
  1139. case MOD_LOG_KEY_REMOVE:
  1140. btrfs_set_node_key(eb, &tm->key, tm->slot);
  1141. btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
  1142. btrfs_set_node_ptr_generation(eb, tm->slot,
  1143. tm->generation);
  1144. n++;
  1145. break;
  1146. case MOD_LOG_KEY_REPLACE:
  1147. BUG_ON(tm->slot >= n);
  1148. btrfs_set_node_key(eb, &tm->key, tm->slot);
  1149. btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
  1150. btrfs_set_node_ptr_generation(eb, tm->slot,
  1151. tm->generation);
  1152. break;
  1153. case MOD_LOG_KEY_ADD:
  1154. /* if a move operation is needed it's in the log */
  1155. n--;
  1156. break;
  1157. case MOD_LOG_MOVE_KEYS:
  1158. o_dst = btrfs_node_key_ptr_offset(tm->slot);
  1159. o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
  1160. memmove_extent_buffer(eb, o_dst, o_src,
  1161. tm->move.nr_items * p_size);
  1162. break;
  1163. case MOD_LOG_ROOT_REPLACE:
  1164. /*
  1165. * this operation is special. for roots, this must be
  1166. * handled explicitly before rewinding.
  1167. * for non-roots, this operation may exist if the node
  1168. * was a root: root A -> child B; then A gets empty and
  1169. * B is promoted to the new root. in the mod log, we'll
  1170. * have a root-replace operation for B, a tree block
  1171. * that is no root. we simply ignore that operation.
  1172. */
  1173. break;
  1174. }
  1175. next = rb_next(&tm->node);
  1176. if (!next)
  1177. break;
  1178. tm = container_of(next, struct tree_mod_elem, node);
  1179. if (tm->logical != first_tm->logical)
  1180. break;
  1181. }
  1182. tree_mod_log_read_unlock(fs_info);
  1183. btrfs_set_header_nritems(eb, n);
  1184. }
  1185. /*
  1186. * Called with eb read locked. If the buffer cannot be rewound, the same buffer
  1187. * is returned. If rewind operations happen, a fresh buffer is returned. The
  1188. * returned buffer is always read-locked. If the returned buffer is not the
  1189. * input buffer, the lock on the input buffer is released and the input buffer
  1190. * is freed (its refcount is decremented).
  1191. */
  1192. static struct extent_buffer *
  1193. tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  1194. struct extent_buffer *eb, u64 time_seq)
  1195. {
  1196. struct extent_buffer *eb_rewin;
  1197. struct tree_mod_elem *tm;
  1198. if (!time_seq)
  1199. return eb;
  1200. if (btrfs_header_level(eb) == 0)
  1201. return eb;
  1202. tm = tree_mod_log_search(fs_info, eb->start, time_seq);
  1203. if (!tm)
  1204. return eb;
  1205. btrfs_set_path_blocking(path);
  1206. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1207. if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
  1208. BUG_ON(tm->slot != 0);
  1209. eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start,
  1210. eb->len);
  1211. if (!eb_rewin) {
  1212. btrfs_tree_read_unlock_blocking(eb);
  1213. free_extent_buffer(eb);
  1214. return NULL;
  1215. }
  1216. btrfs_set_header_bytenr(eb_rewin, eb->start);
  1217. btrfs_set_header_backref_rev(eb_rewin,
  1218. btrfs_header_backref_rev(eb));
  1219. btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
  1220. btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
  1221. } else {
  1222. eb_rewin = btrfs_clone_extent_buffer(eb);
  1223. if (!eb_rewin) {
  1224. btrfs_tree_read_unlock_blocking(eb);
  1225. free_extent_buffer(eb);
  1226. return NULL;
  1227. }
  1228. }
  1229. btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
  1230. btrfs_tree_read_unlock_blocking(eb);
  1231. free_extent_buffer(eb);
  1232. extent_buffer_get(eb_rewin);
  1233. btrfs_tree_read_lock(eb_rewin);
  1234. __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
  1235. WARN_ON(btrfs_header_nritems(eb_rewin) >
  1236. BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
  1237. return eb_rewin;
  1238. }
  1239. /*
  1240. * get_old_root() rewinds the state of @root's root node to the given @time_seq
  1241. * value. If there are no changes, the current root->root_node is returned. If
  1242. * anything changed in between, there's a fresh buffer allocated on which the
  1243. * rewind operations are done. In any case, the returned buffer is read locked.
  1244. * Returns NULL on error (with no locks held).
  1245. */
  1246. static inline struct extent_buffer *
  1247. get_old_root(struct btrfs_root *root, u64 time_seq)
  1248. {
  1249. struct tree_mod_elem *tm;
  1250. struct extent_buffer *eb = NULL;
  1251. struct extent_buffer *eb_root;
  1252. struct extent_buffer *old;
  1253. struct tree_mod_root *old_root = NULL;
  1254. u64 old_generation = 0;
  1255. u64 logical;
  1256. eb_root = btrfs_read_lock_root_node(root);
  1257. tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
  1258. if (!tm)
  1259. return eb_root;
  1260. if (tm->op == MOD_LOG_ROOT_REPLACE) {
  1261. old_root = &tm->old_root;
  1262. old_generation = tm->generation;
  1263. logical = old_root->logical;
  1264. } else {
  1265. logical = eb_root->start;
  1266. }
  1267. tm = tree_mod_log_search(root->fs_info, logical, time_seq);
  1268. if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
  1269. btrfs_tree_read_unlock(eb_root);
  1270. free_extent_buffer(eb_root);
  1271. old = read_tree_block(root, logical, 0);
  1272. if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
  1273. if (!IS_ERR(old))
  1274. free_extent_buffer(old);
  1275. btrfs_warn(root->fs_info,
  1276. "failed to read tree block %llu from get_old_root", logical);
  1277. } else {
  1278. eb = btrfs_clone_extent_buffer(old);
  1279. free_extent_buffer(old);
  1280. }
  1281. } else if (old_root) {
  1282. btrfs_tree_read_unlock(eb_root);
  1283. free_extent_buffer(eb_root);
  1284. eb = alloc_dummy_extent_buffer(root->fs_info, logical,
  1285. root->nodesize);
  1286. } else {
  1287. btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
  1288. eb = btrfs_clone_extent_buffer(eb_root);
  1289. btrfs_tree_read_unlock_blocking(eb_root);
  1290. free_extent_buffer(eb_root);
  1291. }
  1292. if (!eb)
  1293. return NULL;
  1294. extent_buffer_get(eb);
  1295. btrfs_tree_read_lock(eb);
  1296. if (old_root) {
  1297. btrfs_set_header_bytenr(eb, eb->start);
  1298. btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
  1299. btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
  1300. btrfs_set_header_level(eb, old_root->level);
  1301. btrfs_set_header_generation(eb, old_generation);
  1302. }
  1303. if (tm)
  1304. __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
  1305. else
  1306. WARN_ON(btrfs_header_level(eb) != 0);
  1307. WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
  1308. return eb;
  1309. }
  1310. int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
  1311. {
  1312. struct tree_mod_elem *tm;
  1313. int level;
  1314. struct extent_buffer *eb_root = btrfs_root_node(root);
  1315. tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
  1316. if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
  1317. level = tm->old_root.level;
  1318. } else {
  1319. level = btrfs_header_level(eb_root);
  1320. }
  1321. free_extent_buffer(eb_root);
  1322. return level;
  1323. }
  1324. static inline int should_cow_block(struct btrfs_trans_handle *trans,
  1325. struct btrfs_root *root,
  1326. struct extent_buffer *buf)
  1327. {
  1328. if (btrfs_is_testing(root->fs_info))
  1329. return 0;
  1330. /* ensure we can see the force_cow */
  1331. smp_rmb();
  1332. /*
  1333. * We do not need to cow a block if
  1334. * 1) this block is not created or changed in this transaction;
  1335. * 2) this block does not belong to TREE_RELOC tree;
  1336. * 3) the root is not forced COW.
  1337. *
  1338. * What is forced COW:
  1339. * when we create snapshot during committing the transaction,
  1340. * after we've finished coping src root, we must COW the shared
  1341. * block to ensure the metadata consistency.
  1342. */
  1343. if (btrfs_header_generation(buf) == trans->transid &&
  1344. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
  1345. !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
  1346. btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
  1347. !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
  1348. return 0;
  1349. return 1;
  1350. }
  1351. /*
  1352. * cows a single block, see __btrfs_cow_block for the real work.
  1353. * This version of it has extra checks so that a block isn't COWed more than
  1354. * once per transaction, as long as it hasn't been written yet
  1355. */
  1356. noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
  1357. struct btrfs_root *root, struct extent_buffer *buf,
  1358. struct extent_buffer *parent, int parent_slot,
  1359. struct extent_buffer **cow_ret)
  1360. {
  1361. u64 search_start;
  1362. int ret;
  1363. if (trans->transaction != root->fs_info->running_transaction)
  1364. WARN(1, KERN_CRIT "trans %llu running %llu\n",
  1365. trans->transid,
  1366. root->fs_info->running_transaction->transid);
  1367. if (trans->transid != root->fs_info->generation)
  1368. WARN(1, KERN_CRIT "trans %llu running %llu\n",
  1369. trans->transid, root->fs_info->generation);
  1370. if (!should_cow_block(trans, root, buf)) {
  1371. trans->dirty = true;
  1372. *cow_ret = buf;
  1373. return 0;
  1374. }
  1375. search_start = buf->start & ~((u64)SZ_1G - 1);
  1376. if (parent)
  1377. btrfs_set_lock_blocking(parent);
  1378. btrfs_set_lock_blocking(buf);
  1379. ret = __btrfs_cow_block(trans, root, buf, parent,
  1380. parent_slot, cow_ret, search_start, 0);
  1381. trace_btrfs_cow_block(root, buf, *cow_ret);
  1382. return ret;
  1383. }
  1384. /*
  1385. * helper function for defrag to decide if two blocks pointed to by a
  1386. * node are actually close by
  1387. */
  1388. static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
  1389. {
  1390. if (blocknr < other && other - (blocknr + blocksize) < 32768)
  1391. return 1;
  1392. if (blocknr > other && blocknr - (other + blocksize) < 32768)
  1393. return 1;
  1394. return 0;
  1395. }
  1396. /*
  1397. * compare two keys in a memcmp fashion
  1398. */
  1399. static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
  1400. {
  1401. struct btrfs_key k1;
  1402. btrfs_disk_key_to_cpu(&k1, disk);
  1403. return btrfs_comp_cpu_keys(&k1, k2);
  1404. }
  1405. /*
  1406. * same as comp_keys only with two btrfs_key's
  1407. */
  1408. int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
  1409. {
  1410. if (k1->objectid > k2->objectid)
  1411. return 1;
  1412. if (k1->objectid < k2->objectid)
  1413. return -1;
  1414. if (k1->type > k2->type)
  1415. return 1;
  1416. if (k1->type < k2->type)
  1417. return -1;
  1418. if (k1->offset > k2->offset)
  1419. return 1;
  1420. if (k1->offset < k2->offset)
  1421. return -1;
  1422. return 0;
  1423. }
  1424. /*
  1425. * this is used by the defrag code to go through all the
  1426. * leaves pointed to by a node and reallocate them so that
  1427. * disk order is close to key order
  1428. */
  1429. int btrfs_realloc_node(struct btrfs_trans_handle *trans,
  1430. struct btrfs_root *root, struct extent_buffer *parent,
  1431. int start_slot, u64 *last_ret,
  1432. struct btrfs_key *progress)
  1433. {
  1434. struct extent_buffer *cur;
  1435. u64 blocknr;
  1436. u64 gen;
  1437. u64 search_start = *last_ret;
  1438. u64 last_block = 0;
  1439. u64 other;
  1440. u32 parent_nritems;
  1441. int end_slot;
  1442. int i;
  1443. int err = 0;
  1444. int parent_level;
  1445. int uptodate;
  1446. u32 blocksize;
  1447. int progress_passed = 0;
  1448. struct btrfs_disk_key disk_key;
  1449. parent_level = btrfs_header_level(parent);
  1450. WARN_ON(trans->transaction != root->fs_info->running_transaction);
  1451. WARN_ON(trans->transid != root->fs_info->generation);
  1452. parent_nritems = btrfs_header_nritems(parent);
  1453. blocksize = root->nodesize;
  1454. end_slot = parent_nritems - 1;
  1455. if (parent_nritems <= 1)
  1456. return 0;
  1457. btrfs_set_lock_blocking(parent);
  1458. for (i = start_slot; i <= end_slot; i++) {
  1459. int close = 1;
  1460. btrfs_node_key(parent, &disk_key, i);
  1461. if (!progress_passed && comp_keys(&disk_key, progress) < 0)
  1462. continue;
  1463. progress_passed = 1;
  1464. blocknr = btrfs_node_blockptr(parent, i);
  1465. gen = btrfs_node_ptr_generation(parent, i);
  1466. if (last_block == 0)
  1467. last_block = blocknr;
  1468. if (i > 0) {
  1469. other = btrfs_node_blockptr(parent, i - 1);
  1470. close = close_blocks(blocknr, other, blocksize);
  1471. }
  1472. if (!close && i < end_slot) {
  1473. other = btrfs_node_blockptr(parent, i + 1);
  1474. close = close_blocks(blocknr, other, blocksize);
  1475. }
  1476. if (close) {
  1477. last_block = blocknr;
  1478. continue;
  1479. }
  1480. cur = btrfs_find_tree_block(root->fs_info, blocknr);
  1481. if (cur)
  1482. uptodate = btrfs_buffer_uptodate(cur, gen, 0);
  1483. else
  1484. uptodate = 0;
  1485. if (!cur || !uptodate) {
  1486. if (!cur) {
  1487. cur = read_tree_block(root, blocknr, gen);
  1488. if (IS_ERR(cur)) {
  1489. return PTR_ERR(cur);
  1490. } else if (!extent_buffer_uptodate(cur)) {
  1491. free_extent_buffer(cur);
  1492. return -EIO;
  1493. }
  1494. } else if (!uptodate) {
  1495. err = btrfs_read_buffer(cur, gen);
  1496. if (err) {
  1497. free_extent_buffer(cur);
  1498. return err;
  1499. }
  1500. }
  1501. }
  1502. if (search_start == 0)
  1503. search_start = last_block;
  1504. btrfs_tree_lock(cur);
  1505. btrfs_set_lock_blocking(cur);
  1506. err = __btrfs_cow_block(trans, root, cur, parent, i,
  1507. &cur, search_start,
  1508. min(16 * blocksize,
  1509. (end_slot - i) * blocksize));
  1510. if (err) {
  1511. btrfs_tree_unlock(cur);
  1512. free_extent_buffer(cur);
  1513. break;
  1514. }
  1515. search_start = cur->start;
  1516. last_block = cur->start;
  1517. *last_ret = search_start;
  1518. btrfs_tree_unlock(cur);
  1519. free_extent_buffer(cur);
  1520. }
  1521. return err;
  1522. }
  1523. /*
  1524. * The leaf data grows from end-to-front in the node.
  1525. * this returns the address of the start of the last item,
  1526. * which is the stop of the leaf data stack
  1527. */
  1528. static inline unsigned int leaf_data_end(struct btrfs_root *root,
  1529. struct extent_buffer *leaf)
  1530. {
  1531. u32 nr = btrfs_header_nritems(leaf);
  1532. if (nr == 0)
  1533. return BTRFS_LEAF_DATA_SIZE(root);
  1534. return btrfs_item_offset_nr(leaf, nr - 1);
  1535. }
  1536. /*
  1537. * search for key in the extent_buffer. The items start at offset p,
  1538. * and they are item_size apart. There are 'max' items in p.
  1539. *
  1540. * the slot in the array is returned via slot, and it points to
  1541. * the place where you would insert key if it is not found in
  1542. * the array.
  1543. *
  1544. * slot may point to max if the key is bigger than all of the keys
  1545. */
  1546. static noinline int generic_bin_search(struct extent_buffer *eb,
  1547. unsigned long p,
  1548. int item_size, struct btrfs_key *key,
  1549. int max, int *slot)
  1550. {
  1551. int low = 0;
  1552. int high = max;
  1553. int mid;
  1554. int ret;
  1555. struct btrfs_disk_key *tmp = NULL;
  1556. struct btrfs_disk_key unaligned;
  1557. unsigned long offset;
  1558. char *kaddr = NULL;
  1559. unsigned long map_start = 0;
  1560. unsigned long map_len = 0;
  1561. int err;
  1562. if (low > high) {
  1563. btrfs_err(eb->fs_info,
  1564. "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
  1565. __func__, low, high, eb->start,
  1566. btrfs_header_owner(eb), btrfs_header_level(eb));
  1567. return -EINVAL;
  1568. }
  1569. while (low < high) {
  1570. mid = (low + high) / 2;
  1571. offset = p + mid * item_size;
  1572. if (!kaddr || offset < map_start ||
  1573. (offset + sizeof(struct btrfs_disk_key)) >
  1574. map_start + map_len) {
  1575. err = map_private_extent_buffer(eb, offset,
  1576. sizeof(struct btrfs_disk_key),
  1577. &kaddr, &map_start, &map_len);
  1578. if (!err) {
  1579. tmp = (struct btrfs_disk_key *)(kaddr + offset -
  1580. map_start);
  1581. } else if (err == 1) {
  1582. read_extent_buffer(eb, &unaligned,
  1583. offset, sizeof(unaligned));
  1584. tmp = &unaligned;
  1585. } else {
  1586. return err;
  1587. }
  1588. } else {
  1589. tmp = (struct btrfs_disk_key *)(kaddr + offset -
  1590. map_start);
  1591. }
  1592. ret = comp_keys(tmp, key);
  1593. if (ret < 0)
  1594. low = mid + 1;
  1595. else if (ret > 0)
  1596. high = mid;
  1597. else {
  1598. *slot = mid;
  1599. return 0;
  1600. }
  1601. }
  1602. *slot = low;
  1603. return 1;
  1604. }
  1605. /*
  1606. * simple bin_search frontend that does the right thing for
  1607. * leaves vs nodes
  1608. */
  1609. static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
  1610. int level, int *slot)
  1611. {
  1612. if (level == 0)
  1613. return generic_bin_search(eb,
  1614. offsetof(struct btrfs_leaf, items),
  1615. sizeof(struct btrfs_item),
  1616. key, btrfs_header_nritems(eb),
  1617. slot);
  1618. else
  1619. return generic_bin_search(eb,
  1620. offsetof(struct btrfs_node, ptrs),
  1621. sizeof(struct btrfs_key_ptr),
  1622. key, btrfs_header_nritems(eb),
  1623. slot);
  1624. }
  1625. int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
  1626. int level, int *slot)
  1627. {
  1628. return bin_search(eb, key, level, slot);
  1629. }
  1630. static void root_add_used(struct btrfs_root *root, u32 size)
  1631. {
  1632. spin_lock(&root->accounting_lock);
  1633. btrfs_set_root_used(&root->root_item,
  1634. btrfs_root_used(&root->root_item) + size);
  1635. spin_unlock(&root->accounting_lock);
  1636. }
  1637. static void root_sub_used(struct btrfs_root *root, u32 size)
  1638. {
  1639. spin_lock(&root->accounting_lock);
  1640. btrfs_set_root_used(&root->root_item,
  1641. btrfs_root_used(&root->root_item) - size);
  1642. spin_unlock(&root->accounting_lock);
  1643. }
  1644. /* given a node and slot number, this reads the blocks it points to. The
  1645. * extent buffer is returned with a reference taken (but unlocked).
  1646. */
  1647. static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
  1648. struct extent_buffer *parent, int slot)
  1649. {
  1650. int level = btrfs_header_level(parent);
  1651. struct extent_buffer *eb;
  1652. if (slot < 0 || slot >= btrfs_header_nritems(parent))
  1653. return ERR_PTR(-ENOENT);
  1654. BUG_ON(level == 0);
  1655. eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
  1656. btrfs_node_ptr_generation(parent, slot));
  1657. if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
  1658. free_extent_buffer(eb);
  1659. eb = ERR_PTR(-EIO);
  1660. }
  1661. return eb;
  1662. }
  1663. /*
  1664. * node level balancing, used to make sure nodes are in proper order for
  1665. * item deletion. We balance from the top down, so we have to make sure
  1666. * that a deletion won't leave an node completely empty later on.
  1667. */
  1668. static noinline int balance_level(struct btrfs_trans_handle *trans,
  1669. struct btrfs_root *root,
  1670. struct btrfs_path *path, int level)
  1671. {
  1672. struct extent_buffer *right = NULL;
  1673. struct extent_buffer *mid;
  1674. struct extent_buffer *left = NULL;
  1675. struct extent_buffer *parent = NULL;
  1676. int ret = 0;
  1677. int wret;
  1678. int pslot;
  1679. int orig_slot = path->slots[level];
  1680. u64 orig_ptr;
  1681. if (level == 0)
  1682. return 0;
  1683. mid = path->nodes[level];
  1684. WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
  1685. path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
  1686. WARN_ON(btrfs_header_generation(mid) != trans->transid);
  1687. orig_ptr = btrfs_node_blockptr(mid, orig_slot);
  1688. if (level < BTRFS_MAX_LEVEL - 1) {
  1689. parent = path->nodes[level + 1];
  1690. pslot = path->slots[level + 1];
  1691. }
  1692. /*
  1693. * deal with the case where there is only one pointer in the root
  1694. * by promoting the node below to a root
  1695. */
  1696. if (!parent) {
  1697. struct extent_buffer *child;
  1698. if (btrfs_header_nritems(mid) != 1)
  1699. return 0;
  1700. /* promote the child to a root */
  1701. child = read_node_slot(root, mid, 0);
  1702. if (IS_ERR(child)) {
  1703. ret = PTR_ERR(child);
  1704. btrfs_handle_fs_error(root->fs_info, ret, NULL);
  1705. goto enospc;
  1706. }
  1707. btrfs_tree_lock(child);
  1708. btrfs_set_lock_blocking(child);
  1709. ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
  1710. if (ret) {
  1711. btrfs_tree_unlock(child);
  1712. free_extent_buffer(child);
  1713. goto enospc;
  1714. }
  1715. tree_mod_log_set_root_pointer(root, child, 1);
  1716. rcu_assign_pointer(root->node, child);
  1717. add_root_to_dirty_list(root);
  1718. btrfs_tree_unlock(child);
  1719. path->locks[level] = 0;
  1720. path->nodes[level] = NULL;
  1721. clean_tree_block(trans, root->fs_info, mid);
  1722. btrfs_tree_unlock(mid);
  1723. /* once for the path */
  1724. free_extent_buffer(mid);
  1725. root_sub_used(root, mid->len);
  1726. btrfs_free_tree_block(trans, root, mid, 0, 1);
  1727. /* once for the root ptr */
  1728. free_extent_buffer_stale(mid);
  1729. return 0;
  1730. }
  1731. if (btrfs_header_nritems(mid) >
  1732. BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
  1733. return 0;
  1734. left = read_node_slot(root, parent, pslot - 1);
  1735. if (IS_ERR(left))
  1736. left = NULL;
  1737. if (left) {
  1738. btrfs_tree_lock(left);
  1739. btrfs_set_lock_blocking(left);
  1740. wret = btrfs_cow_block(trans, root, left,
  1741. parent, pslot - 1, &left);
  1742. if (wret) {
  1743. ret = wret;
  1744. goto enospc;
  1745. }
  1746. }
  1747. right = read_node_slot(root, parent, pslot + 1);
  1748. if (IS_ERR(right))
  1749. right = NULL;
  1750. if (right) {
  1751. btrfs_tree_lock(right);
  1752. btrfs_set_lock_blocking(right);
  1753. wret = btrfs_cow_block(trans, root, right,
  1754. parent, pslot + 1, &right);
  1755. if (wret) {
  1756. ret = wret;
  1757. goto enospc;
  1758. }
  1759. }
  1760. /* first, try to make some room in the middle buffer */
  1761. if (left) {
  1762. orig_slot += btrfs_header_nritems(left);
  1763. wret = push_node_left(trans, root, left, mid, 1);
  1764. if (wret < 0)
  1765. ret = wret;
  1766. }
  1767. /*
  1768. * then try to empty the right most buffer into the middle
  1769. */
  1770. if (right) {
  1771. wret = push_node_left(trans, root, mid, right, 1);
  1772. if (wret < 0 && wret != -ENOSPC)
  1773. ret = wret;
  1774. if (btrfs_header_nritems(right) == 0) {
  1775. clean_tree_block(trans, root->fs_info, right);
  1776. btrfs_tree_unlock(right);
  1777. del_ptr(root, path, level + 1, pslot + 1);
  1778. root_sub_used(root, right->len);
  1779. btrfs_free_tree_block(trans, root, right, 0, 1);
  1780. free_extent_buffer_stale(right);
  1781. right = NULL;
  1782. } else {
  1783. struct btrfs_disk_key right_key;
  1784. btrfs_node_key(right, &right_key, 0);
  1785. tree_mod_log_set_node_key(root->fs_info, parent,
  1786. pslot + 1, 0);
  1787. btrfs_set_node_key(parent, &right_key, pslot + 1);
  1788. btrfs_mark_buffer_dirty(parent);
  1789. }
  1790. }
  1791. if (btrfs_header_nritems(mid) == 1) {
  1792. /*
  1793. * we're not allowed to leave a node with one item in the
  1794. * tree during a delete. A deletion from lower in the tree
  1795. * could try to delete the only pointer in this node.
  1796. * So, pull some keys from the left.
  1797. * There has to be a left pointer at this point because
  1798. * otherwise we would have pulled some pointers from the
  1799. * right
  1800. */
  1801. if (!left) {
  1802. ret = -EROFS;
  1803. btrfs_handle_fs_error(root->fs_info, ret, NULL);
  1804. goto enospc;
  1805. }
  1806. wret = balance_node_right(trans, root, mid, left);
  1807. if (wret < 0) {
  1808. ret = wret;
  1809. goto enospc;
  1810. }
  1811. if (wret == 1) {
  1812. wret = push_node_left(trans, root, left, mid, 1);
  1813. if (wret < 0)
  1814. ret = wret;
  1815. }
  1816. BUG_ON(wret == 1);
  1817. }
  1818. if (btrfs_header_nritems(mid) == 0) {
  1819. clean_tree_block(trans, root->fs_info, mid);
  1820. btrfs_tree_unlock(mid);
  1821. del_ptr(root, path, level + 1, pslot);
  1822. root_sub_used(root, mid->len);
  1823. btrfs_free_tree_block(trans, root, mid, 0, 1);
  1824. free_extent_buffer_stale(mid);
  1825. mid = NULL;
  1826. } else {
  1827. /* update the parent key to reflect our changes */
  1828. struct btrfs_disk_key mid_key;
  1829. btrfs_node_key(mid, &mid_key, 0);
  1830. tree_mod_log_set_node_key(root->fs_info, parent,
  1831. pslot, 0);
  1832. btrfs_set_node_key(parent, &mid_key, pslot);
  1833. btrfs_mark_buffer_dirty(parent);
  1834. }
  1835. /* update the path */
  1836. if (left) {
  1837. if (btrfs_header_nritems(left) > orig_slot) {
  1838. extent_buffer_get(left);
  1839. /* left was locked after cow */
  1840. path->nodes[level] = left;
  1841. path->slots[level + 1] -= 1;
  1842. path->slots[level] = orig_slot;
  1843. if (mid) {
  1844. btrfs_tree_unlock(mid);
  1845. free_extent_buffer(mid);
  1846. }
  1847. } else {
  1848. orig_slot -= btrfs_header_nritems(left);
  1849. path->slots[level] = orig_slot;
  1850. }
  1851. }
  1852. /* double check we haven't messed things up */
  1853. if (orig_ptr !=
  1854. btrfs_node_blockptr(path->nodes[level], path->slots[level]))
  1855. BUG();
  1856. enospc:
  1857. if (right) {
  1858. btrfs_tree_unlock(right);
  1859. free_extent_buffer(right);
  1860. }
  1861. if (left) {
  1862. if (path->nodes[level] != left)
  1863. btrfs_tree_unlock(left);
  1864. free_extent_buffer(left);
  1865. }
  1866. return ret;
  1867. }
  1868. /* Node balancing for insertion. Here we only split or push nodes around
  1869. * when they are completely full. This is also done top down, so we
  1870. * have to be pessimistic.
  1871. */
  1872. static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
  1873. struct btrfs_root *root,
  1874. struct btrfs_path *path, int level)
  1875. {
  1876. struct extent_buffer *right = NULL;
  1877. struct extent_buffer *mid;
  1878. struct extent_buffer *left = NULL;
  1879. struct extent_buffer *parent = NULL;
  1880. int ret = 0;
  1881. int wret;
  1882. int pslot;
  1883. int orig_slot = path->slots[level];
  1884. if (level == 0)
  1885. return 1;
  1886. mid = path->nodes[level];
  1887. WARN_ON(btrfs_header_generation(mid) != trans->transid);
  1888. if (level < BTRFS_MAX_LEVEL - 1) {
  1889. parent = path->nodes[level + 1];
  1890. pslot = path->slots[level + 1];
  1891. }
  1892. if (!parent)
  1893. return 1;
  1894. left = read_node_slot(root, parent, pslot - 1);
  1895. if (IS_ERR(left))
  1896. left = NULL;
  1897. /* first, try to make some room in the middle buffer */
  1898. if (left) {
  1899. u32 left_nr;
  1900. btrfs_tree_lock(left);
  1901. btrfs_set_lock_blocking(left);
  1902. left_nr = btrfs_header_nritems(left);
  1903. if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
  1904. wret = 1;
  1905. } else {
  1906. ret = btrfs_cow_block(trans, root, left, parent,
  1907. pslot - 1, &left);
  1908. if (ret)
  1909. wret = 1;
  1910. else {
  1911. wret = push_node_left(trans, root,
  1912. left, mid, 0);
  1913. }
  1914. }
  1915. if (wret < 0)
  1916. ret = wret;
  1917. if (wret == 0) {
  1918. struct btrfs_disk_key disk_key;
  1919. orig_slot += left_nr;
  1920. btrfs_node_key(mid, &disk_key, 0);
  1921. tree_mod_log_set_node_key(root->fs_info, parent,
  1922. pslot, 0);
  1923. btrfs_set_node_key(parent, &disk_key, pslot);
  1924. btrfs_mark_buffer_dirty(parent);
  1925. if (btrfs_header_nritems(left) > orig_slot) {
  1926. path->nodes[level] = left;
  1927. path->slots[level + 1] -= 1;
  1928. path->slots[level] = orig_slot;
  1929. btrfs_tree_unlock(mid);
  1930. free_extent_buffer(mid);
  1931. } else {
  1932. orig_slot -=
  1933. btrfs_header_nritems(left);
  1934. path->slots[level] = orig_slot;
  1935. btrfs_tree_unlock(left);
  1936. free_extent_buffer(left);
  1937. }
  1938. return 0;
  1939. }
  1940. btrfs_tree_unlock(left);
  1941. free_extent_buffer(left);
  1942. }
  1943. right = read_node_slot(root, parent, pslot + 1);
  1944. if (IS_ERR(right))
  1945. right = NULL;
  1946. /*
  1947. * then try to empty the right most buffer into the middle
  1948. */
  1949. if (right) {
  1950. u32 right_nr;
  1951. btrfs_tree_lock(right);
  1952. btrfs_set_lock_blocking(right);
  1953. right_nr = btrfs_header_nritems(right);
  1954. if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
  1955. wret = 1;
  1956. } else {
  1957. ret = btrfs_cow_block(trans, root, right,
  1958. parent, pslot + 1,
  1959. &right);
  1960. if (ret)
  1961. wret = 1;
  1962. else {
  1963. wret = balance_node_right(trans, root,
  1964. right, mid);
  1965. }
  1966. }
  1967. if (wret < 0)
  1968. ret = wret;
  1969. if (wret == 0) {
  1970. struct btrfs_disk_key disk_key;
  1971. btrfs_node_key(right, &disk_key, 0);
  1972. tree_mod_log_set_node_key(root->fs_info, parent,
  1973. pslot + 1, 0);
  1974. btrfs_set_node_key(parent, &disk_key, pslot + 1);
  1975. btrfs_mark_buffer_dirty(parent);
  1976. if (btrfs_header_nritems(mid) <= orig_slot) {
  1977. path->nodes[level] = right;
  1978. path->slots[level + 1] += 1;
  1979. path->slots[level] = orig_slot -
  1980. btrfs_header_nritems(mid);
  1981. btrfs_tree_unlock(mid);
  1982. free_extent_buffer(mid);
  1983. } else {
  1984. btrfs_tree_unlock(right);
  1985. free_extent_buffer(right);
  1986. }
  1987. return 0;
  1988. }
  1989. btrfs_tree_unlock(right);
  1990. free_extent_buffer(right);
  1991. }
  1992. return 1;
  1993. }
  1994. /*
  1995. * readahead one full node of leaves, finding things that are close
  1996. * to the block in 'slot', and triggering ra on them.
  1997. */
  1998. static void reada_for_search(struct btrfs_root *root,
  1999. struct btrfs_path *path,
  2000. int level, int slot, u64 objectid)
  2001. {
  2002. struct extent_buffer *node;
  2003. struct btrfs_disk_key disk_key;
  2004. u32 nritems;
  2005. u64 search;
  2006. u64 target;
  2007. u64 nread = 0;
  2008. u64 gen;
  2009. struct extent_buffer *eb;
  2010. u32 nr;
  2011. u32 blocksize;
  2012. u32 nscan = 0;
  2013. if (level != 1)
  2014. return;
  2015. if (!path->nodes[level])
  2016. return;
  2017. node = path->nodes[level];
  2018. search = btrfs_node_blockptr(node, slot);
  2019. blocksize = root->nodesize;
  2020. eb = btrfs_find_tree_block(root->fs_info, search);
  2021. if (eb) {
  2022. free_extent_buffer(eb);
  2023. return;
  2024. }
  2025. target = search;
  2026. nritems = btrfs_header_nritems(node);
  2027. nr = slot;
  2028. while (1) {
  2029. if (path->reada == READA_BACK) {
  2030. if (nr == 0)
  2031. break;
  2032. nr--;
  2033. } else if (path->reada == READA_FORWARD) {
  2034. nr++;
  2035. if (nr >= nritems)
  2036. break;
  2037. }
  2038. if (path->reada == READA_BACK && objectid) {
  2039. btrfs_node_key(node, &disk_key, nr);
  2040. if (btrfs_disk_key_objectid(&disk_key) != objectid)
  2041. break;
  2042. }
  2043. search = btrfs_node_blockptr(node, nr);
  2044. if ((search <= target && target - search <= 65536) ||
  2045. (search > target && search - target <= 65536)) {
  2046. gen = btrfs_node_ptr_generation(node, nr);
  2047. readahead_tree_block(root, search);
  2048. nread += blocksize;
  2049. }
  2050. nscan++;
  2051. if ((nread > 65536 || nscan > 32))
  2052. break;
  2053. }
  2054. }
  2055. static noinline void reada_for_balance(struct btrfs_root *root,
  2056. struct btrfs_path *path, int level)
  2057. {
  2058. int slot;
  2059. int nritems;
  2060. struct extent_buffer *parent;
  2061. struct extent_buffer *eb;
  2062. u64 gen;
  2063. u64 block1 = 0;
  2064. u64 block2 = 0;
  2065. parent = path->nodes[level + 1];
  2066. if (!parent)
  2067. return;
  2068. nritems = btrfs_header_nritems(parent);
  2069. slot = path->slots[level + 1];
  2070. if (slot > 0) {
  2071. block1 = btrfs_node_blockptr(parent, slot - 1);
  2072. gen = btrfs_node_ptr_generation(parent, slot - 1);
  2073. eb = btrfs_find_tree_block(root->fs_info, block1);
  2074. /*
  2075. * if we get -eagain from btrfs_buffer_uptodate, we
  2076. * don't want to return eagain here. That will loop
  2077. * forever
  2078. */
  2079. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  2080. block1 = 0;
  2081. free_extent_buffer(eb);
  2082. }
  2083. if (slot + 1 < nritems) {
  2084. block2 = btrfs_node_blockptr(parent, slot + 1);
  2085. gen = btrfs_node_ptr_generation(parent, slot + 1);
  2086. eb = btrfs_find_tree_block(root->fs_info, block2);
  2087. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  2088. block2 = 0;
  2089. free_extent_buffer(eb);
  2090. }
  2091. if (block1)
  2092. readahead_tree_block(root, block1);
  2093. if (block2)
  2094. readahead_tree_block(root, block2);
  2095. }
  2096. /*
  2097. * when we walk down the tree, it is usually safe to unlock the higher layers
  2098. * in the tree. The exceptions are when our path goes through slot 0, because
  2099. * operations on the tree might require changing key pointers higher up in the
  2100. * tree.
  2101. *
  2102. * callers might also have set path->keep_locks, which tells this code to keep
  2103. * the lock if the path points to the last slot in the block. This is part of
  2104. * walking through the tree, and selecting the next slot in the higher block.
  2105. *
  2106. * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
  2107. * if lowest_unlock is 1, level 0 won't be unlocked
  2108. */
  2109. static noinline void unlock_up(struct btrfs_path *path, int level,
  2110. int lowest_unlock, int min_write_lock_level,
  2111. int *write_lock_level)
  2112. {
  2113. int i;
  2114. int skip_level = level;
  2115. int no_skips = 0;
  2116. struct extent_buffer *t;
  2117. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2118. if (!path->nodes[i])
  2119. break;
  2120. if (!path->locks[i])
  2121. break;
  2122. if (!no_skips && path->slots[i] == 0) {
  2123. skip_level = i + 1;
  2124. continue;
  2125. }
  2126. if (!no_skips && path->keep_locks) {
  2127. u32 nritems;
  2128. t = path->nodes[i];
  2129. nritems = btrfs_header_nritems(t);
  2130. if (nritems < 1 || path->slots[i] >= nritems - 1) {
  2131. skip_level = i + 1;
  2132. continue;
  2133. }
  2134. }
  2135. if (skip_level < i && i >= lowest_unlock)
  2136. no_skips = 1;
  2137. t = path->nodes[i];
  2138. if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
  2139. btrfs_tree_unlock_rw(t, path->locks[i]);
  2140. path->locks[i] = 0;
  2141. if (write_lock_level &&
  2142. i > min_write_lock_level &&
  2143. i <= *write_lock_level) {
  2144. *write_lock_level = i - 1;
  2145. }
  2146. }
  2147. }
  2148. }
  2149. /*
  2150. * This releases any locks held in the path starting at level and
  2151. * going all the way up to the root.
  2152. *
  2153. * btrfs_search_slot will keep the lock held on higher nodes in a few
  2154. * corner cases, such as COW of the block at slot zero in the node. This
  2155. * ignores those rules, and it should only be called when there are no
  2156. * more updates to be done higher up in the tree.
  2157. */
  2158. noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
  2159. {
  2160. int i;
  2161. if (path->keep_locks)
  2162. return;
  2163. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2164. if (!path->nodes[i])
  2165. continue;
  2166. if (!path->locks[i])
  2167. continue;
  2168. btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
  2169. path->locks[i] = 0;
  2170. }
  2171. }
  2172. /*
  2173. * helper function for btrfs_search_slot. The goal is to find a block
  2174. * in cache without setting the path to blocking. If we find the block
  2175. * we return zero and the path is unchanged.
  2176. *
  2177. * If we can't find the block, we set the path blocking and do some
  2178. * reada. -EAGAIN is returned and the search must be repeated.
  2179. */
  2180. static int
  2181. read_block_for_search(struct btrfs_trans_handle *trans,
  2182. struct btrfs_root *root, struct btrfs_path *p,
  2183. struct extent_buffer **eb_ret, int level, int slot,
  2184. struct btrfs_key *key, u64 time_seq)
  2185. {
  2186. u64 blocknr;
  2187. u64 gen;
  2188. struct extent_buffer *b = *eb_ret;
  2189. struct extent_buffer *tmp;
  2190. int ret;
  2191. blocknr = btrfs_node_blockptr(b, slot);
  2192. gen = btrfs_node_ptr_generation(b, slot);
  2193. tmp = btrfs_find_tree_block(root->fs_info, blocknr);
  2194. if (tmp) {
  2195. /* first we do an atomic uptodate check */
  2196. if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
  2197. *eb_ret = tmp;
  2198. return 0;
  2199. }
  2200. /* the pages were up to date, but we failed
  2201. * the generation number check. Do a full
  2202. * read for the generation number that is correct.
  2203. * We must do this without dropping locks so
  2204. * we can trust our generation number
  2205. */
  2206. btrfs_set_path_blocking(p);
  2207. /* now we're allowed to do a blocking uptodate check */
  2208. ret = btrfs_read_buffer(tmp, gen);
  2209. if (!ret) {
  2210. *eb_ret = tmp;
  2211. return 0;
  2212. }
  2213. free_extent_buffer(tmp);
  2214. btrfs_release_path(p);
  2215. return -EIO;
  2216. }
  2217. /*
  2218. * reduce lock contention at high levels
  2219. * of the btree by dropping locks before
  2220. * we read. Don't release the lock on the current
  2221. * level because we need to walk this node to figure
  2222. * out which blocks to read.
  2223. */
  2224. btrfs_unlock_up_safe(p, level + 1);
  2225. btrfs_set_path_blocking(p);
  2226. free_extent_buffer(tmp);
  2227. if (p->reada != READA_NONE)
  2228. reada_for_search(root, p, level, slot, key->objectid);
  2229. btrfs_release_path(p);
  2230. ret = -EAGAIN;
  2231. tmp = read_tree_block(root, blocknr, 0);
  2232. if (!IS_ERR(tmp)) {
  2233. /*
  2234. * If the read above didn't mark this buffer up to date,
  2235. * it will never end up being up to date. Set ret to EIO now
  2236. * and give up so that our caller doesn't loop forever
  2237. * on our EAGAINs.
  2238. */
  2239. if (!btrfs_buffer_uptodate(tmp, 0, 0))
  2240. ret = -EIO;
  2241. free_extent_buffer(tmp);
  2242. } else {
  2243. ret = PTR_ERR(tmp);
  2244. }
  2245. return ret;
  2246. }
  2247. /*
  2248. * helper function for btrfs_search_slot. This does all of the checks
  2249. * for node-level blocks and does any balancing required based on
  2250. * the ins_len.
  2251. *
  2252. * If no extra work was required, zero is returned. If we had to
  2253. * drop the path, -EAGAIN is returned and btrfs_search_slot must
  2254. * start over
  2255. */
  2256. static int
  2257. setup_nodes_for_search(struct btrfs_trans_handle *trans,
  2258. struct btrfs_root *root, struct btrfs_path *p,
  2259. struct extent_buffer *b, int level, int ins_len,
  2260. int *write_lock_level)
  2261. {
  2262. int ret;
  2263. if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
  2264. BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
  2265. int sret;
  2266. if (*write_lock_level < level + 1) {
  2267. *write_lock_level = level + 1;
  2268. btrfs_release_path(p);
  2269. goto again;
  2270. }
  2271. btrfs_set_path_blocking(p);
  2272. reada_for_balance(root, p, level);
  2273. sret = split_node(trans, root, p, level);
  2274. btrfs_clear_path_blocking(p, NULL, 0);
  2275. BUG_ON(sret > 0);
  2276. if (sret) {
  2277. ret = sret;
  2278. goto done;
  2279. }
  2280. b = p->nodes[level];
  2281. } else if (ins_len < 0 && btrfs_header_nritems(b) <
  2282. BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
  2283. int sret;
  2284. if (*write_lock_level < level + 1) {
  2285. *write_lock_level = level + 1;
  2286. btrfs_release_path(p);
  2287. goto again;
  2288. }
  2289. btrfs_set_path_blocking(p);
  2290. reada_for_balance(root, p, level);
  2291. sret = balance_level(trans, root, p, level);
  2292. btrfs_clear_path_blocking(p, NULL, 0);
  2293. if (sret) {
  2294. ret = sret;
  2295. goto done;
  2296. }
  2297. b = p->nodes[level];
  2298. if (!b) {
  2299. btrfs_release_path(p);
  2300. goto again;
  2301. }
  2302. BUG_ON(btrfs_header_nritems(b) == 1);
  2303. }
  2304. return 0;
  2305. again:
  2306. ret = -EAGAIN;
  2307. done:
  2308. return ret;
  2309. }
  2310. static void key_search_validate(struct extent_buffer *b,
  2311. struct btrfs_key *key,
  2312. int level)
  2313. {
  2314. #ifdef CONFIG_BTRFS_ASSERT
  2315. struct btrfs_disk_key disk_key;
  2316. btrfs_cpu_key_to_disk(&disk_key, key);
  2317. if (level == 0)
  2318. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2319. offsetof(struct btrfs_leaf, items[0].key),
  2320. sizeof(disk_key)));
  2321. else
  2322. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2323. offsetof(struct btrfs_node, ptrs[0].key),
  2324. sizeof(disk_key)));
  2325. #endif
  2326. }
  2327. static int key_search(struct extent_buffer *b, struct btrfs_key *key,
  2328. int level, int *prev_cmp, int *slot)
  2329. {
  2330. if (*prev_cmp != 0) {
  2331. *prev_cmp = bin_search(b, key, level, slot);
  2332. return *prev_cmp;
  2333. }
  2334. key_search_validate(b, key, level);
  2335. *slot = 0;
  2336. return 0;
  2337. }
  2338. int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
  2339. u64 iobjectid, u64 ioff, u8 key_type,
  2340. struct btrfs_key *found_key)
  2341. {
  2342. int ret;
  2343. struct btrfs_key key;
  2344. struct extent_buffer *eb;
  2345. ASSERT(path);
  2346. ASSERT(found_key);
  2347. key.type = key_type;
  2348. key.objectid = iobjectid;
  2349. key.offset = ioff;
  2350. ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  2351. if (ret < 0)
  2352. return ret;
  2353. eb = path->nodes[0];
  2354. if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
  2355. ret = btrfs_next_leaf(fs_root, path);
  2356. if (ret)
  2357. return ret;
  2358. eb = path->nodes[0];
  2359. }
  2360. btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
  2361. if (found_key->type != key.type ||
  2362. found_key->objectid != key.objectid)
  2363. return 1;
  2364. return 0;
  2365. }
  2366. /*
  2367. * look for key in the tree. path is filled in with nodes along the way
  2368. * if key is found, we return zero and you can find the item in the leaf
  2369. * level of the path (level 0)
  2370. *
  2371. * If the key isn't found, the path points to the slot where it should
  2372. * be inserted, and 1 is returned. If there are other errors during the
  2373. * search a negative error number is returned.
  2374. *
  2375. * if ins_len > 0, nodes and leaves will be split as we walk down the
  2376. * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
  2377. * possible)
  2378. */
  2379. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  2380. *root, struct btrfs_key *key, struct btrfs_path *p, int
  2381. ins_len, int cow)
  2382. {
  2383. struct extent_buffer *b;
  2384. int slot;
  2385. int ret;
  2386. int err;
  2387. int level;
  2388. int lowest_unlock = 1;
  2389. int root_lock;
  2390. /* everything at write_lock_level or lower must be write locked */
  2391. int write_lock_level = 0;
  2392. u8 lowest_level = 0;
  2393. int min_write_lock_level;
  2394. int prev_cmp;
  2395. lowest_level = p->lowest_level;
  2396. WARN_ON(lowest_level && ins_len > 0);
  2397. WARN_ON(p->nodes[0] != NULL);
  2398. BUG_ON(!cow && ins_len);
  2399. if (ins_len < 0) {
  2400. lowest_unlock = 2;
  2401. /* when we are removing items, we might have to go up to level
  2402. * two as we update tree pointers Make sure we keep write
  2403. * for those levels as well
  2404. */
  2405. write_lock_level = 2;
  2406. } else if (ins_len > 0) {
  2407. /*
  2408. * for inserting items, make sure we have a write lock on
  2409. * level 1 so we can update keys
  2410. */
  2411. write_lock_level = 1;
  2412. }
  2413. if (!cow)
  2414. write_lock_level = -1;
  2415. if (cow && (p->keep_locks || p->lowest_level))
  2416. write_lock_level = BTRFS_MAX_LEVEL;
  2417. min_write_lock_level = write_lock_level;
  2418. again:
  2419. prev_cmp = -1;
  2420. /*
  2421. * we try very hard to do read locks on the root
  2422. */
  2423. root_lock = BTRFS_READ_LOCK;
  2424. level = 0;
  2425. if (p->search_commit_root) {
  2426. /*
  2427. * the commit roots are read only
  2428. * so we always do read locks
  2429. */
  2430. if (p->need_commit_sem)
  2431. down_read(&root->fs_info->commit_root_sem);
  2432. b = root->commit_root;
  2433. extent_buffer_get(b);
  2434. level = btrfs_header_level(b);
  2435. if (p->need_commit_sem)
  2436. up_read(&root->fs_info->commit_root_sem);
  2437. if (!p->skip_locking)
  2438. btrfs_tree_read_lock(b);
  2439. } else {
  2440. if (p->skip_locking) {
  2441. b = btrfs_root_node(root);
  2442. level = btrfs_header_level(b);
  2443. } else {
  2444. /* we don't know the level of the root node
  2445. * until we actually have it read locked
  2446. */
  2447. b = btrfs_read_lock_root_node(root);
  2448. level = btrfs_header_level(b);
  2449. if (level <= write_lock_level) {
  2450. /* whoops, must trade for write lock */
  2451. btrfs_tree_read_unlock(b);
  2452. free_extent_buffer(b);
  2453. b = btrfs_lock_root_node(root);
  2454. root_lock = BTRFS_WRITE_LOCK;
  2455. /* the level might have changed, check again */
  2456. level = btrfs_header_level(b);
  2457. }
  2458. }
  2459. }
  2460. p->nodes[level] = b;
  2461. if (!p->skip_locking)
  2462. p->locks[level] = root_lock;
  2463. while (b) {
  2464. level = btrfs_header_level(b);
  2465. /*
  2466. * setup the path here so we can release it under lock
  2467. * contention with the cow code
  2468. */
  2469. if (cow) {
  2470. /*
  2471. * if we don't really need to cow this block
  2472. * then we don't want to set the path blocking,
  2473. * so we test it here
  2474. */
  2475. if (!should_cow_block(trans, root, b)) {
  2476. trans->dirty = true;
  2477. goto cow_done;
  2478. }
  2479. /*
  2480. * must have write locks on this node and the
  2481. * parent
  2482. */
  2483. if (level > write_lock_level ||
  2484. (level + 1 > write_lock_level &&
  2485. level + 1 < BTRFS_MAX_LEVEL &&
  2486. p->nodes[level + 1])) {
  2487. write_lock_level = level + 1;
  2488. btrfs_release_path(p);
  2489. goto again;
  2490. }
  2491. btrfs_set_path_blocking(p);
  2492. err = btrfs_cow_block(trans, root, b,
  2493. p->nodes[level + 1],
  2494. p->slots[level + 1], &b);
  2495. if (err) {
  2496. ret = err;
  2497. goto done;
  2498. }
  2499. }
  2500. cow_done:
  2501. p->nodes[level] = b;
  2502. btrfs_clear_path_blocking(p, NULL, 0);
  2503. /*
  2504. * we have a lock on b and as long as we aren't changing
  2505. * the tree, there is no way to for the items in b to change.
  2506. * It is safe to drop the lock on our parent before we
  2507. * go through the expensive btree search on b.
  2508. *
  2509. * If we're inserting or deleting (ins_len != 0), then we might
  2510. * be changing slot zero, which may require changing the parent.
  2511. * So, we can't drop the lock until after we know which slot
  2512. * we're operating on.
  2513. */
  2514. if (!ins_len && !p->keep_locks) {
  2515. int u = level + 1;
  2516. if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
  2517. btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
  2518. p->locks[u] = 0;
  2519. }
  2520. }
  2521. ret = key_search(b, key, level, &prev_cmp, &slot);
  2522. if (ret < 0)
  2523. goto done;
  2524. if (level != 0) {
  2525. int dec = 0;
  2526. if (ret && slot > 0) {
  2527. dec = 1;
  2528. slot -= 1;
  2529. }
  2530. p->slots[level] = slot;
  2531. err = setup_nodes_for_search(trans, root, p, b, level,
  2532. ins_len, &write_lock_level);
  2533. if (err == -EAGAIN)
  2534. goto again;
  2535. if (err) {
  2536. ret = err;
  2537. goto done;
  2538. }
  2539. b = p->nodes[level];
  2540. slot = p->slots[level];
  2541. /*
  2542. * slot 0 is special, if we change the key
  2543. * we have to update the parent pointer
  2544. * which means we must have a write lock
  2545. * on the parent
  2546. */
  2547. if (slot == 0 && ins_len &&
  2548. write_lock_level < level + 1) {
  2549. write_lock_level = level + 1;
  2550. btrfs_release_path(p);
  2551. goto again;
  2552. }
  2553. unlock_up(p, level, lowest_unlock,
  2554. min_write_lock_level, &write_lock_level);
  2555. if (level == lowest_level) {
  2556. if (dec)
  2557. p->slots[level]++;
  2558. goto done;
  2559. }
  2560. err = read_block_for_search(trans, root, p,
  2561. &b, level, slot, key, 0);
  2562. if (err == -EAGAIN)
  2563. goto again;
  2564. if (err) {
  2565. ret = err;
  2566. goto done;
  2567. }
  2568. if (!p->skip_locking) {
  2569. level = btrfs_header_level(b);
  2570. if (level <= write_lock_level) {
  2571. err = btrfs_try_tree_write_lock(b);
  2572. if (!err) {
  2573. btrfs_set_path_blocking(p);
  2574. btrfs_tree_lock(b);
  2575. btrfs_clear_path_blocking(p, b,
  2576. BTRFS_WRITE_LOCK);
  2577. }
  2578. p->locks[level] = BTRFS_WRITE_LOCK;
  2579. } else {
  2580. err = btrfs_tree_read_lock_atomic(b);
  2581. if (!err) {
  2582. btrfs_set_path_blocking(p);
  2583. btrfs_tree_read_lock(b);
  2584. btrfs_clear_path_blocking(p, b,
  2585. BTRFS_READ_LOCK);
  2586. }
  2587. p->locks[level] = BTRFS_READ_LOCK;
  2588. }
  2589. p->nodes[level] = b;
  2590. }
  2591. } else {
  2592. p->slots[level] = slot;
  2593. if (ins_len > 0 &&
  2594. btrfs_leaf_free_space(root, b) < ins_len) {
  2595. if (write_lock_level < 1) {
  2596. write_lock_level = 1;
  2597. btrfs_release_path(p);
  2598. goto again;
  2599. }
  2600. btrfs_set_path_blocking(p);
  2601. err = split_leaf(trans, root, key,
  2602. p, ins_len, ret == 0);
  2603. btrfs_clear_path_blocking(p, NULL, 0);
  2604. BUG_ON(err > 0);
  2605. if (err) {
  2606. ret = err;
  2607. goto done;
  2608. }
  2609. }
  2610. if (!p->search_for_split)
  2611. unlock_up(p, level, lowest_unlock,
  2612. min_write_lock_level, &write_lock_level);
  2613. goto done;
  2614. }
  2615. }
  2616. ret = 1;
  2617. done:
  2618. /*
  2619. * we don't really know what they plan on doing with the path
  2620. * from here on, so for now just mark it as blocking
  2621. */
  2622. if (!p->leave_spinning)
  2623. btrfs_set_path_blocking(p);
  2624. if (ret < 0 && !p->skip_release_on_error)
  2625. btrfs_release_path(p);
  2626. return ret;
  2627. }
  2628. /*
  2629. * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
  2630. * current state of the tree together with the operations recorded in the tree
  2631. * modification log to search for the key in a previous version of this tree, as
  2632. * denoted by the time_seq parameter.
  2633. *
  2634. * Naturally, there is no support for insert, delete or cow operations.
  2635. *
  2636. * The resulting path and return value will be set up as if we called
  2637. * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
  2638. */
  2639. int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
  2640. struct btrfs_path *p, u64 time_seq)
  2641. {
  2642. struct extent_buffer *b;
  2643. int slot;
  2644. int ret;
  2645. int err;
  2646. int level;
  2647. int lowest_unlock = 1;
  2648. u8 lowest_level = 0;
  2649. int prev_cmp = -1;
  2650. lowest_level = p->lowest_level;
  2651. WARN_ON(p->nodes[0] != NULL);
  2652. if (p->search_commit_root) {
  2653. BUG_ON(time_seq);
  2654. return btrfs_search_slot(NULL, root, key, p, 0, 0);
  2655. }
  2656. again:
  2657. b = get_old_root(root, time_seq);
  2658. level = btrfs_header_level(b);
  2659. p->locks[level] = BTRFS_READ_LOCK;
  2660. while (b) {
  2661. level = btrfs_header_level(b);
  2662. p->nodes[level] = b;
  2663. btrfs_clear_path_blocking(p, NULL, 0);
  2664. /*
  2665. * we have a lock on b and as long as we aren't changing
  2666. * the tree, there is no way to for the items in b to change.
  2667. * It is safe to drop the lock on our parent before we
  2668. * go through the expensive btree search on b.
  2669. */
  2670. btrfs_unlock_up_safe(p, level + 1);
  2671. /*
  2672. * Since we can unwind ebs we want to do a real search every
  2673. * time.
  2674. */
  2675. prev_cmp = -1;
  2676. ret = key_search(b, key, level, &prev_cmp, &slot);
  2677. if (level != 0) {
  2678. int dec = 0;
  2679. if (ret && slot > 0) {
  2680. dec = 1;
  2681. slot -= 1;
  2682. }
  2683. p->slots[level] = slot;
  2684. unlock_up(p, level, lowest_unlock, 0, NULL);
  2685. if (level == lowest_level) {
  2686. if (dec)
  2687. p->slots[level]++;
  2688. goto done;
  2689. }
  2690. err = read_block_for_search(NULL, root, p, &b, level,
  2691. slot, key, time_seq);
  2692. if (err == -EAGAIN)
  2693. goto again;
  2694. if (err) {
  2695. ret = err;
  2696. goto done;
  2697. }
  2698. level = btrfs_header_level(b);
  2699. err = btrfs_tree_read_lock_atomic(b);
  2700. if (!err) {
  2701. btrfs_set_path_blocking(p);
  2702. btrfs_tree_read_lock(b);
  2703. btrfs_clear_path_blocking(p, b,
  2704. BTRFS_READ_LOCK);
  2705. }
  2706. b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
  2707. if (!b) {
  2708. ret = -ENOMEM;
  2709. goto done;
  2710. }
  2711. p->locks[level] = BTRFS_READ_LOCK;
  2712. p->nodes[level] = b;
  2713. } else {
  2714. p->slots[level] = slot;
  2715. unlock_up(p, level, lowest_unlock, 0, NULL);
  2716. goto done;
  2717. }
  2718. }
  2719. ret = 1;
  2720. done:
  2721. if (!p->leave_spinning)
  2722. btrfs_set_path_blocking(p);
  2723. if (ret < 0)
  2724. btrfs_release_path(p);
  2725. return ret;
  2726. }
  2727. /*
  2728. * helper to use instead of search slot if no exact match is needed but
  2729. * instead the next or previous item should be returned.
  2730. * When find_higher is true, the next higher item is returned, the next lower
  2731. * otherwise.
  2732. * When return_any and find_higher are both true, and no higher item is found,
  2733. * return the next lower instead.
  2734. * When return_any is true and find_higher is false, and no lower item is found,
  2735. * return the next higher instead.
  2736. * It returns 0 if any item is found, 1 if none is found (tree empty), and
  2737. * < 0 on error
  2738. */
  2739. int btrfs_search_slot_for_read(struct btrfs_root *root,
  2740. struct btrfs_key *key, struct btrfs_path *p,
  2741. int find_higher, int return_any)
  2742. {
  2743. int ret;
  2744. struct extent_buffer *leaf;
  2745. again:
  2746. ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
  2747. if (ret <= 0)
  2748. return ret;
  2749. /*
  2750. * a return value of 1 means the path is at the position where the
  2751. * item should be inserted. Normally this is the next bigger item,
  2752. * but in case the previous item is the last in a leaf, path points
  2753. * to the first free slot in the previous leaf, i.e. at an invalid
  2754. * item.
  2755. */
  2756. leaf = p->nodes[0];
  2757. if (find_higher) {
  2758. if (p->slots[0] >= btrfs_header_nritems(leaf)) {
  2759. ret = btrfs_next_leaf(root, p);
  2760. if (ret <= 0)
  2761. return ret;
  2762. if (!return_any)
  2763. return 1;
  2764. /*
  2765. * no higher item found, return the next
  2766. * lower instead
  2767. */
  2768. return_any = 0;
  2769. find_higher = 0;
  2770. btrfs_release_path(p);
  2771. goto again;
  2772. }
  2773. } else {
  2774. if (p->slots[0] == 0) {
  2775. ret = btrfs_prev_leaf(root, p);
  2776. if (ret < 0)
  2777. return ret;
  2778. if (!ret) {
  2779. leaf = p->nodes[0];
  2780. if (p->slots[0] == btrfs_header_nritems(leaf))
  2781. p->slots[0]--;
  2782. return 0;
  2783. }
  2784. if (!return_any)
  2785. return 1;
  2786. /*
  2787. * no lower item found, return the next
  2788. * higher instead
  2789. */
  2790. return_any = 0;
  2791. find_higher = 1;
  2792. btrfs_release_path(p);
  2793. goto again;
  2794. } else {
  2795. --p->slots[0];
  2796. }
  2797. }
  2798. return 0;
  2799. }
  2800. /*
  2801. * adjust the pointers going up the tree, starting at level
  2802. * making sure the right key of each node is points to 'key'.
  2803. * This is used after shifting pointers to the left, so it stops
  2804. * fixing up pointers when a given leaf/node is not in slot 0 of the
  2805. * higher levels
  2806. *
  2807. */
  2808. static void fixup_low_keys(struct btrfs_fs_info *fs_info,
  2809. struct btrfs_path *path,
  2810. struct btrfs_disk_key *key, int level)
  2811. {
  2812. int i;
  2813. struct extent_buffer *t;
  2814. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2815. int tslot = path->slots[i];
  2816. if (!path->nodes[i])
  2817. break;
  2818. t = path->nodes[i];
  2819. tree_mod_log_set_node_key(fs_info, t, tslot, 1);
  2820. btrfs_set_node_key(t, key, tslot);
  2821. btrfs_mark_buffer_dirty(path->nodes[i]);
  2822. if (tslot != 0)
  2823. break;
  2824. }
  2825. }
  2826. /*
  2827. * update item key.
  2828. *
  2829. * This function isn't completely safe. It's the caller's responsibility
  2830. * that the new key won't break the order
  2831. */
  2832. void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
  2833. struct btrfs_path *path,
  2834. struct btrfs_key *new_key)
  2835. {
  2836. struct btrfs_disk_key disk_key;
  2837. struct extent_buffer *eb;
  2838. int slot;
  2839. eb = path->nodes[0];
  2840. slot = path->slots[0];
  2841. if (slot > 0) {
  2842. btrfs_item_key(eb, &disk_key, slot - 1);
  2843. BUG_ON(comp_keys(&disk_key, new_key) >= 0);
  2844. }
  2845. if (slot < btrfs_header_nritems(eb) - 1) {
  2846. btrfs_item_key(eb, &disk_key, slot + 1);
  2847. BUG_ON(comp_keys(&disk_key, new_key) <= 0);
  2848. }
  2849. btrfs_cpu_key_to_disk(&disk_key, new_key);
  2850. btrfs_set_item_key(eb, &disk_key, slot);
  2851. btrfs_mark_buffer_dirty(eb);
  2852. if (slot == 0)
  2853. fixup_low_keys(fs_info, path, &disk_key, 1);
  2854. }
  2855. /*
  2856. * try to push data from one node into the next node left in the
  2857. * tree.
  2858. *
  2859. * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
  2860. * error, and > 0 if there was no room in the left hand block.
  2861. */
  2862. static int push_node_left(struct btrfs_trans_handle *trans,
  2863. struct btrfs_root *root, struct extent_buffer *dst,
  2864. struct extent_buffer *src, int empty)
  2865. {
  2866. int push_items = 0;
  2867. int src_nritems;
  2868. int dst_nritems;
  2869. int ret = 0;
  2870. src_nritems = btrfs_header_nritems(src);
  2871. dst_nritems = btrfs_header_nritems(dst);
  2872. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  2873. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2874. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2875. if (!empty && src_nritems <= 8)
  2876. return 1;
  2877. if (push_items <= 0)
  2878. return 1;
  2879. if (empty) {
  2880. push_items = min(src_nritems, push_items);
  2881. if (push_items < src_nritems) {
  2882. /* leave at least 8 pointers in the node if
  2883. * we aren't going to empty it
  2884. */
  2885. if (src_nritems - push_items < 8) {
  2886. if (push_items <= 8)
  2887. return 1;
  2888. push_items -= 8;
  2889. }
  2890. }
  2891. } else
  2892. push_items = min(src_nritems - 8, push_items);
  2893. ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
  2894. push_items);
  2895. if (ret) {
  2896. btrfs_abort_transaction(trans, ret);
  2897. return ret;
  2898. }
  2899. copy_extent_buffer(dst, src,
  2900. btrfs_node_key_ptr_offset(dst_nritems),
  2901. btrfs_node_key_ptr_offset(0),
  2902. push_items * sizeof(struct btrfs_key_ptr));
  2903. if (push_items < src_nritems) {
  2904. /*
  2905. * don't call tree_mod_log_eb_move here, key removal was already
  2906. * fully logged by tree_mod_log_eb_copy above.
  2907. */
  2908. memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
  2909. btrfs_node_key_ptr_offset(push_items),
  2910. (src_nritems - push_items) *
  2911. sizeof(struct btrfs_key_ptr));
  2912. }
  2913. btrfs_set_header_nritems(src, src_nritems - push_items);
  2914. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2915. btrfs_mark_buffer_dirty(src);
  2916. btrfs_mark_buffer_dirty(dst);
  2917. return ret;
  2918. }
  2919. /*
  2920. * try to push data from one node into the next node right in the
  2921. * tree.
  2922. *
  2923. * returns 0 if some ptrs were pushed, < 0 if there was some horrible
  2924. * error, and > 0 if there was no room in the right hand block.
  2925. *
  2926. * this will only push up to 1/2 the contents of the left node over
  2927. */
  2928. static int balance_node_right(struct btrfs_trans_handle *trans,
  2929. struct btrfs_root *root,
  2930. struct extent_buffer *dst,
  2931. struct extent_buffer *src)
  2932. {
  2933. int push_items = 0;
  2934. int max_push;
  2935. int src_nritems;
  2936. int dst_nritems;
  2937. int ret = 0;
  2938. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2939. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2940. src_nritems = btrfs_header_nritems(src);
  2941. dst_nritems = btrfs_header_nritems(dst);
  2942. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  2943. if (push_items <= 0)
  2944. return 1;
  2945. if (src_nritems < 4)
  2946. return 1;
  2947. max_push = src_nritems / 2 + 1;
  2948. /* don't try to empty the node */
  2949. if (max_push >= src_nritems)
  2950. return 1;
  2951. if (max_push < push_items)
  2952. push_items = max_push;
  2953. tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
  2954. memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
  2955. btrfs_node_key_ptr_offset(0),
  2956. (dst_nritems) *
  2957. sizeof(struct btrfs_key_ptr));
  2958. ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
  2959. src_nritems - push_items, push_items);
  2960. if (ret) {
  2961. btrfs_abort_transaction(trans, ret);
  2962. return ret;
  2963. }
  2964. copy_extent_buffer(dst, src,
  2965. btrfs_node_key_ptr_offset(0),
  2966. btrfs_node_key_ptr_offset(src_nritems - push_items),
  2967. push_items * sizeof(struct btrfs_key_ptr));
  2968. btrfs_set_header_nritems(src, src_nritems - push_items);
  2969. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2970. btrfs_mark_buffer_dirty(src);
  2971. btrfs_mark_buffer_dirty(dst);
  2972. return ret;
  2973. }
  2974. /*
  2975. * helper function to insert a new root level in the tree.
  2976. * A new node is allocated, and a single item is inserted to
  2977. * point to the existing root
  2978. *
  2979. * returns zero on success or < 0 on failure.
  2980. */
  2981. static noinline int insert_new_root(struct btrfs_trans_handle *trans,
  2982. struct btrfs_root *root,
  2983. struct btrfs_path *path, int level)
  2984. {
  2985. u64 lower_gen;
  2986. struct extent_buffer *lower;
  2987. struct extent_buffer *c;
  2988. struct extent_buffer *old;
  2989. struct btrfs_disk_key lower_key;
  2990. BUG_ON(path->nodes[level]);
  2991. BUG_ON(path->nodes[level-1] != root->node);
  2992. lower = path->nodes[level-1];
  2993. if (level == 1)
  2994. btrfs_item_key(lower, &lower_key, 0);
  2995. else
  2996. btrfs_node_key(lower, &lower_key, 0);
  2997. c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  2998. &lower_key, level, root->node->start, 0);
  2999. if (IS_ERR(c))
  3000. return PTR_ERR(c);
  3001. root_add_used(root, root->nodesize);
  3002. memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
  3003. btrfs_set_header_nritems(c, 1);
  3004. btrfs_set_header_level(c, level);
  3005. btrfs_set_header_bytenr(c, c->start);
  3006. btrfs_set_header_generation(c, trans->transid);
  3007. btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
  3008. btrfs_set_header_owner(c, root->root_key.objectid);
  3009. write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
  3010. BTRFS_FSID_SIZE);
  3011. write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
  3012. btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
  3013. btrfs_set_node_key(c, &lower_key, 0);
  3014. btrfs_set_node_blockptr(c, 0, lower->start);
  3015. lower_gen = btrfs_header_generation(lower);
  3016. WARN_ON(lower_gen != trans->transid);
  3017. btrfs_set_node_ptr_generation(c, 0, lower_gen);
  3018. btrfs_mark_buffer_dirty(c);
  3019. old = root->node;
  3020. tree_mod_log_set_root_pointer(root, c, 0);
  3021. rcu_assign_pointer(root->node, c);
  3022. /* the super has an extra ref to root->node */
  3023. free_extent_buffer(old);
  3024. add_root_to_dirty_list(root);
  3025. extent_buffer_get(c);
  3026. path->nodes[level] = c;
  3027. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  3028. path->slots[level] = 0;
  3029. return 0;
  3030. }
  3031. /*
  3032. * worker function to insert a single pointer in a node.
  3033. * the node should have enough room for the pointer already
  3034. *
  3035. * slot and level indicate where you want the key to go, and
  3036. * blocknr is the block the key points to.
  3037. */
  3038. static void insert_ptr(struct btrfs_trans_handle *trans,
  3039. struct btrfs_root *root, struct btrfs_path *path,
  3040. struct btrfs_disk_key *key, u64 bytenr,
  3041. int slot, int level)
  3042. {
  3043. struct extent_buffer *lower;
  3044. int nritems;
  3045. int ret;
  3046. BUG_ON(!path->nodes[level]);
  3047. btrfs_assert_tree_locked(path->nodes[level]);
  3048. lower = path->nodes[level];
  3049. nritems = btrfs_header_nritems(lower);
  3050. BUG_ON(slot > nritems);
  3051. BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
  3052. if (slot != nritems) {
  3053. if (level)
  3054. tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
  3055. slot, nritems - slot);
  3056. memmove_extent_buffer(lower,
  3057. btrfs_node_key_ptr_offset(slot + 1),
  3058. btrfs_node_key_ptr_offset(slot),
  3059. (nritems - slot) * sizeof(struct btrfs_key_ptr));
  3060. }
  3061. if (level) {
  3062. ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
  3063. MOD_LOG_KEY_ADD, GFP_NOFS);
  3064. BUG_ON(ret < 0);
  3065. }
  3066. btrfs_set_node_key(lower, key, slot);
  3067. btrfs_set_node_blockptr(lower, slot, bytenr);
  3068. WARN_ON(trans->transid == 0);
  3069. btrfs_set_node_ptr_generation(lower, slot, trans->transid);
  3070. btrfs_set_header_nritems(lower, nritems + 1);
  3071. btrfs_mark_buffer_dirty(lower);
  3072. }
  3073. /*
  3074. * split the node at the specified level in path in two.
  3075. * The path is corrected to point to the appropriate node after the split
  3076. *
  3077. * Before splitting this tries to make some room in the node by pushing
  3078. * left and right, if either one works, it returns right away.
  3079. *
  3080. * returns 0 on success and < 0 on failure
  3081. */
  3082. static noinline int split_node(struct btrfs_trans_handle *trans,
  3083. struct btrfs_root *root,
  3084. struct btrfs_path *path, int level)
  3085. {
  3086. struct extent_buffer *c;
  3087. struct extent_buffer *split;
  3088. struct btrfs_disk_key disk_key;
  3089. int mid;
  3090. int ret;
  3091. u32 c_nritems;
  3092. c = path->nodes[level];
  3093. WARN_ON(btrfs_header_generation(c) != trans->transid);
  3094. if (c == root->node) {
  3095. /*
  3096. * trying to split the root, lets make a new one
  3097. *
  3098. * tree mod log: We don't log_removal old root in
  3099. * insert_new_root, because that root buffer will be kept as a
  3100. * normal node. We are going to log removal of half of the
  3101. * elements below with tree_mod_log_eb_copy. We're holding a
  3102. * tree lock on the buffer, which is why we cannot race with
  3103. * other tree_mod_log users.
  3104. */
  3105. ret = insert_new_root(trans, root, path, level + 1);
  3106. if (ret)
  3107. return ret;
  3108. } else {
  3109. ret = push_nodes_for_insert(trans, root, path, level);
  3110. c = path->nodes[level];
  3111. if (!ret && btrfs_header_nritems(c) <
  3112. BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
  3113. return 0;
  3114. if (ret < 0)
  3115. return ret;
  3116. }
  3117. c_nritems = btrfs_header_nritems(c);
  3118. mid = (c_nritems + 1) / 2;
  3119. btrfs_node_key(c, &disk_key, mid);
  3120. split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3121. &disk_key, level, c->start, 0);
  3122. if (IS_ERR(split))
  3123. return PTR_ERR(split);
  3124. root_add_used(root, root->nodesize);
  3125. memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
  3126. btrfs_set_header_level(split, btrfs_header_level(c));
  3127. btrfs_set_header_bytenr(split, split->start);
  3128. btrfs_set_header_generation(split, trans->transid);
  3129. btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
  3130. btrfs_set_header_owner(split, root->root_key.objectid);
  3131. write_extent_buffer(split, root->fs_info->fsid,
  3132. btrfs_header_fsid(), BTRFS_FSID_SIZE);
  3133. write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
  3134. btrfs_header_chunk_tree_uuid(split),
  3135. BTRFS_UUID_SIZE);
  3136. ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0,
  3137. mid, c_nritems - mid);
  3138. if (ret) {
  3139. btrfs_abort_transaction(trans, ret);
  3140. return ret;
  3141. }
  3142. copy_extent_buffer(split, c,
  3143. btrfs_node_key_ptr_offset(0),
  3144. btrfs_node_key_ptr_offset(mid),
  3145. (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
  3146. btrfs_set_header_nritems(split, c_nritems - mid);
  3147. btrfs_set_header_nritems(c, mid);
  3148. ret = 0;
  3149. btrfs_mark_buffer_dirty(c);
  3150. btrfs_mark_buffer_dirty(split);
  3151. insert_ptr(trans, root, path, &disk_key, split->start,
  3152. path->slots[level + 1] + 1, level + 1);
  3153. if (path->slots[level] >= mid) {
  3154. path->slots[level] -= mid;
  3155. btrfs_tree_unlock(c);
  3156. free_extent_buffer(c);
  3157. path->nodes[level] = split;
  3158. path->slots[level + 1] += 1;
  3159. } else {
  3160. btrfs_tree_unlock(split);
  3161. free_extent_buffer(split);
  3162. }
  3163. return ret;
  3164. }
  3165. /*
  3166. * how many bytes are required to store the items in a leaf. start
  3167. * and nr indicate which items in the leaf to check. This totals up the
  3168. * space used both by the item structs and the item data
  3169. */
  3170. static int leaf_space_used(struct extent_buffer *l, int start, int nr)
  3171. {
  3172. struct btrfs_item *start_item;
  3173. struct btrfs_item *end_item;
  3174. struct btrfs_map_token token;
  3175. int data_len;
  3176. int nritems = btrfs_header_nritems(l);
  3177. int end = min(nritems, start + nr) - 1;
  3178. if (!nr)
  3179. return 0;
  3180. btrfs_init_map_token(&token);
  3181. start_item = btrfs_item_nr(start);
  3182. end_item = btrfs_item_nr(end);
  3183. data_len = btrfs_token_item_offset(l, start_item, &token) +
  3184. btrfs_token_item_size(l, start_item, &token);
  3185. data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
  3186. data_len += sizeof(struct btrfs_item) * nr;
  3187. WARN_ON(data_len < 0);
  3188. return data_len;
  3189. }
  3190. /*
  3191. * The space between the end of the leaf items and
  3192. * the start of the leaf data. IOW, how much room
  3193. * the leaf has left for both items and data
  3194. */
  3195. noinline int btrfs_leaf_free_space(struct btrfs_root *root,
  3196. struct extent_buffer *leaf)
  3197. {
  3198. int nritems = btrfs_header_nritems(leaf);
  3199. int ret;
  3200. ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
  3201. if (ret < 0) {
  3202. btrfs_crit(root->fs_info,
  3203. "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
  3204. ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
  3205. leaf_space_used(leaf, 0, nritems), nritems);
  3206. }
  3207. return ret;
  3208. }
  3209. /*
  3210. * min slot controls the lowest index we're willing to push to the
  3211. * right. We'll push up to and including min_slot, but no lower
  3212. */
  3213. static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
  3214. struct btrfs_root *root,
  3215. struct btrfs_path *path,
  3216. int data_size, int empty,
  3217. struct extent_buffer *right,
  3218. int free_space, u32 left_nritems,
  3219. u32 min_slot)
  3220. {
  3221. struct extent_buffer *left = path->nodes[0];
  3222. struct extent_buffer *upper = path->nodes[1];
  3223. struct btrfs_map_token token;
  3224. struct btrfs_disk_key disk_key;
  3225. int slot;
  3226. u32 i;
  3227. int push_space = 0;
  3228. int push_items = 0;
  3229. struct btrfs_item *item;
  3230. u32 nr;
  3231. u32 right_nritems;
  3232. u32 data_end;
  3233. u32 this_item_size;
  3234. btrfs_init_map_token(&token);
  3235. if (empty)
  3236. nr = 0;
  3237. else
  3238. nr = max_t(u32, 1, min_slot);
  3239. if (path->slots[0] >= left_nritems)
  3240. push_space += data_size;
  3241. slot = path->slots[1];
  3242. i = left_nritems - 1;
  3243. while (i >= nr) {
  3244. item = btrfs_item_nr(i);
  3245. if (!empty && push_items > 0) {
  3246. if (path->slots[0] > i)
  3247. break;
  3248. if (path->slots[0] == i) {
  3249. int space = btrfs_leaf_free_space(root, left);
  3250. if (space + push_space * 2 > free_space)
  3251. break;
  3252. }
  3253. }
  3254. if (path->slots[0] == i)
  3255. push_space += data_size;
  3256. this_item_size = btrfs_item_size(left, item);
  3257. if (this_item_size + sizeof(*item) + push_space > free_space)
  3258. break;
  3259. push_items++;
  3260. push_space += this_item_size + sizeof(*item);
  3261. if (i == 0)
  3262. break;
  3263. i--;
  3264. }
  3265. if (push_items == 0)
  3266. goto out_unlock;
  3267. WARN_ON(!empty && push_items == left_nritems);
  3268. /* push left to right */
  3269. right_nritems = btrfs_header_nritems(right);
  3270. push_space = btrfs_item_end_nr(left, left_nritems - push_items);
  3271. push_space -= leaf_data_end(root, left);
  3272. /* make room in the right data area */
  3273. data_end = leaf_data_end(root, right);
  3274. memmove_extent_buffer(right,
  3275. btrfs_leaf_data(right) + data_end - push_space,
  3276. btrfs_leaf_data(right) + data_end,
  3277. BTRFS_LEAF_DATA_SIZE(root) - data_end);
  3278. /* copy from the left data area */
  3279. copy_extent_buffer(right, left, btrfs_leaf_data(right) +
  3280. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  3281. btrfs_leaf_data(left) + leaf_data_end(root, left),
  3282. push_space);
  3283. memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
  3284. btrfs_item_nr_offset(0),
  3285. right_nritems * sizeof(struct btrfs_item));
  3286. /* copy the items from left to right */
  3287. copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
  3288. btrfs_item_nr_offset(left_nritems - push_items),
  3289. push_items * sizeof(struct btrfs_item));
  3290. /* update the item pointers */
  3291. right_nritems += push_items;
  3292. btrfs_set_header_nritems(right, right_nritems);
  3293. push_space = BTRFS_LEAF_DATA_SIZE(root);
  3294. for (i = 0; i < right_nritems; i++) {
  3295. item = btrfs_item_nr(i);
  3296. push_space -= btrfs_token_item_size(right, item, &token);
  3297. btrfs_set_token_item_offset(right, item, push_space, &token);
  3298. }
  3299. left_nritems -= push_items;
  3300. btrfs_set_header_nritems(left, left_nritems);
  3301. if (left_nritems)
  3302. btrfs_mark_buffer_dirty(left);
  3303. else
  3304. clean_tree_block(trans, root->fs_info, left);
  3305. btrfs_mark_buffer_dirty(right);
  3306. btrfs_item_key(right, &disk_key, 0);
  3307. btrfs_set_node_key(upper, &disk_key, slot + 1);
  3308. btrfs_mark_buffer_dirty(upper);
  3309. /* then fixup the leaf pointer in the path */
  3310. if (path->slots[0] >= left_nritems) {
  3311. path->slots[0] -= left_nritems;
  3312. if (btrfs_header_nritems(path->nodes[0]) == 0)
  3313. clean_tree_block(trans, root->fs_info, path->nodes[0]);
  3314. btrfs_tree_unlock(path->nodes[0]);
  3315. free_extent_buffer(path->nodes[0]);
  3316. path->nodes[0] = right;
  3317. path->slots[1] += 1;
  3318. } else {
  3319. btrfs_tree_unlock(right);
  3320. free_extent_buffer(right);
  3321. }
  3322. return 0;
  3323. out_unlock:
  3324. btrfs_tree_unlock(right);
  3325. free_extent_buffer(right);
  3326. return 1;
  3327. }
  3328. /*
  3329. * push some data in the path leaf to the right, trying to free up at
  3330. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3331. *
  3332. * returns 1 if the push failed because the other node didn't have enough
  3333. * room, 0 if everything worked out and < 0 if there were major errors.
  3334. *
  3335. * this will push starting from min_slot to the end of the leaf. It won't
  3336. * push any slot lower than min_slot
  3337. */
  3338. static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
  3339. *root, struct btrfs_path *path,
  3340. int min_data_size, int data_size,
  3341. int empty, u32 min_slot)
  3342. {
  3343. struct extent_buffer *left = path->nodes[0];
  3344. struct extent_buffer *right;
  3345. struct extent_buffer *upper;
  3346. int slot;
  3347. int free_space;
  3348. u32 left_nritems;
  3349. int ret;
  3350. if (!path->nodes[1])
  3351. return 1;
  3352. slot = path->slots[1];
  3353. upper = path->nodes[1];
  3354. if (slot >= btrfs_header_nritems(upper) - 1)
  3355. return 1;
  3356. btrfs_assert_tree_locked(path->nodes[1]);
  3357. right = read_node_slot(root, upper, slot + 1);
  3358. /*
  3359. * slot + 1 is not valid or we fail to read the right node,
  3360. * no big deal, just return.
  3361. */
  3362. if (IS_ERR(right))
  3363. return 1;
  3364. btrfs_tree_lock(right);
  3365. btrfs_set_lock_blocking(right);
  3366. free_space = btrfs_leaf_free_space(root, right);
  3367. if (free_space < data_size)
  3368. goto out_unlock;
  3369. /* cow and double check */
  3370. ret = btrfs_cow_block(trans, root, right, upper,
  3371. slot + 1, &right);
  3372. if (ret)
  3373. goto out_unlock;
  3374. free_space = btrfs_leaf_free_space(root, right);
  3375. if (free_space < data_size)
  3376. goto out_unlock;
  3377. left_nritems = btrfs_header_nritems(left);
  3378. if (left_nritems == 0)
  3379. goto out_unlock;
  3380. if (path->slots[0] == left_nritems && !empty) {
  3381. /* Key greater than all keys in the leaf, right neighbor has
  3382. * enough room for it and we're not emptying our leaf to delete
  3383. * it, therefore use right neighbor to insert the new item and
  3384. * no need to touch/dirty our left leaft. */
  3385. btrfs_tree_unlock(left);
  3386. free_extent_buffer(left);
  3387. path->nodes[0] = right;
  3388. path->slots[0] = 0;
  3389. path->slots[1]++;
  3390. return 0;
  3391. }
  3392. return __push_leaf_right(trans, root, path, min_data_size, empty,
  3393. right, free_space, left_nritems, min_slot);
  3394. out_unlock:
  3395. btrfs_tree_unlock(right);
  3396. free_extent_buffer(right);
  3397. return 1;
  3398. }
  3399. /*
  3400. * push some data in the path leaf to the left, trying to free up at
  3401. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3402. *
  3403. * max_slot can put a limit on how far into the leaf we'll push items. The
  3404. * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
  3405. * items
  3406. */
  3407. static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
  3408. struct btrfs_root *root,
  3409. struct btrfs_path *path, int data_size,
  3410. int empty, struct extent_buffer *left,
  3411. int free_space, u32 right_nritems,
  3412. u32 max_slot)
  3413. {
  3414. struct btrfs_disk_key disk_key;
  3415. struct extent_buffer *right = path->nodes[0];
  3416. int i;
  3417. int push_space = 0;
  3418. int push_items = 0;
  3419. struct btrfs_item *item;
  3420. u32 old_left_nritems;
  3421. u32 nr;
  3422. int ret = 0;
  3423. u32 this_item_size;
  3424. u32 old_left_item_size;
  3425. struct btrfs_map_token token;
  3426. btrfs_init_map_token(&token);
  3427. if (empty)
  3428. nr = min(right_nritems, max_slot);
  3429. else
  3430. nr = min(right_nritems - 1, max_slot);
  3431. for (i = 0; i < nr; i++) {
  3432. item = btrfs_item_nr(i);
  3433. if (!empty && push_items > 0) {
  3434. if (path->slots[0] < i)
  3435. break;
  3436. if (path->slots[0] == i) {
  3437. int space = btrfs_leaf_free_space(root, right);
  3438. if (space + push_space * 2 > free_space)
  3439. break;
  3440. }
  3441. }
  3442. if (path->slots[0] == i)
  3443. push_space += data_size;
  3444. this_item_size = btrfs_item_size(right, item);
  3445. if (this_item_size + sizeof(*item) + push_space > free_space)
  3446. break;
  3447. push_items++;
  3448. push_space += this_item_size + sizeof(*item);
  3449. }
  3450. if (push_items == 0) {
  3451. ret = 1;
  3452. goto out;
  3453. }
  3454. WARN_ON(!empty && push_items == btrfs_header_nritems(right));
  3455. /* push data from right to left */
  3456. copy_extent_buffer(left, right,
  3457. btrfs_item_nr_offset(btrfs_header_nritems(left)),
  3458. btrfs_item_nr_offset(0),
  3459. push_items * sizeof(struct btrfs_item));
  3460. push_space = BTRFS_LEAF_DATA_SIZE(root) -
  3461. btrfs_item_offset_nr(right, push_items - 1);
  3462. copy_extent_buffer(left, right, btrfs_leaf_data(left) +
  3463. leaf_data_end(root, left) - push_space,
  3464. btrfs_leaf_data(right) +
  3465. btrfs_item_offset_nr(right, push_items - 1),
  3466. push_space);
  3467. old_left_nritems = btrfs_header_nritems(left);
  3468. BUG_ON(old_left_nritems <= 0);
  3469. old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
  3470. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  3471. u32 ioff;
  3472. item = btrfs_item_nr(i);
  3473. ioff = btrfs_token_item_offset(left, item, &token);
  3474. btrfs_set_token_item_offset(left, item,
  3475. ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
  3476. &token);
  3477. }
  3478. btrfs_set_header_nritems(left, old_left_nritems + push_items);
  3479. /* fixup right node */
  3480. if (push_items > right_nritems)
  3481. WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
  3482. right_nritems);
  3483. if (push_items < right_nritems) {
  3484. push_space = btrfs_item_offset_nr(right, push_items - 1) -
  3485. leaf_data_end(root, right);
  3486. memmove_extent_buffer(right, btrfs_leaf_data(right) +
  3487. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  3488. btrfs_leaf_data(right) +
  3489. leaf_data_end(root, right), push_space);
  3490. memmove_extent_buffer(right, btrfs_item_nr_offset(0),
  3491. btrfs_item_nr_offset(push_items),
  3492. (btrfs_header_nritems(right) - push_items) *
  3493. sizeof(struct btrfs_item));
  3494. }
  3495. right_nritems -= push_items;
  3496. btrfs_set_header_nritems(right, right_nritems);
  3497. push_space = BTRFS_LEAF_DATA_SIZE(root);
  3498. for (i = 0; i < right_nritems; i++) {
  3499. item = btrfs_item_nr(i);
  3500. push_space = push_space - btrfs_token_item_size(right,
  3501. item, &token);
  3502. btrfs_set_token_item_offset(right, item, push_space, &token);
  3503. }
  3504. btrfs_mark_buffer_dirty(left);
  3505. if (right_nritems)
  3506. btrfs_mark_buffer_dirty(right);
  3507. else
  3508. clean_tree_block(trans, root->fs_info, right);
  3509. btrfs_item_key(right, &disk_key, 0);
  3510. fixup_low_keys(root->fs_info, path, &disk_key, 1);
  3511. /* then fixup the leaf pointer in the path */
  3512. if (path->slots[0] < push_items) {
  3513. path->slots[0] += old_left_nritems;
  3514. btrfs_tree_unlock(path->nodes[0]);
  3515. free_extent_buffer(path->nodes[0]);
  3516. path->nodes[0] = left;
  3517. path->slots[1] -= 1;
  3518. } else {
  3519. btrfs_tree_unlock(left);
  3520. free_extent_buffer(left);
  3521. path->slots[0] -= push_items;
  3522. }
  3523. BUG_ON(path->slots[0] < 0);
  3524. return ret;
  3525. out:
  3526. btrfs_tree_unlock(left);
  3527. free_extent_buffer(left);
  3528. return ret;
  3529. }
  3530. /*
  3531. * push some data in the path leaf to the left, trying to free up at
  3532. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3533. *
  3534. * max_slot can put a limit on how far into the leaf we'll push items. The
  3535. * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
  3536. * items
  3537. */
  3538. static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
  3539. *root, struct btrfs_path *path, int min_data_size,
  3540. int data_size, int empty, u32 max_slot)
  3541. {
  3542. struct extent_buffer *right = path->nodes[0];
  3543. struct extent_buffer *left;
  3544. int slot;
  3545. int free_space;
  3546. u32 right_nritems;
  3547. int ret = 0;
  3548. slot = path->slots[1];
  3549. if (slot == 0)
  3550. return 1;
  3551. if (!path->nodes[1])
  3552. return 1;
  3553. right_nritems = btrfs_header_nritems(right);
  3554. if (right_nritems == 0)
  3555. return 1;
  3556. btrfs_assert_tree_locked(path->nodes[1]);
  3557. left = read_node_slot(root, path->nodes[1], slot - 1);
  3558. /*
  3559. * slot - 1 is not valid or we fail to read the left node,
  3560. * no big deal, just return.
  3561. */
  3562. if (IS_ERR(left))
  3563. return 1;
  3564. btrfs_tree_lock(left);
  3565. btrfs_set_lock_blocking(left);
  3566. free_space = btrfs_leaf_free_space(root, left);
  3567. if (free_space < data_size) {
  3568. ret = 1;
  3569. goto out;
  3570. }
  3571. /* cow and double check */
  3572. ret = btrfs_cow_block(trans, root, left,
  3573. path->nodes[1], slot - 1, &left);
  3574. if (ret) {
  3575. /* we hit -ENOSPC, but it isn't fatal here */
  3576. if (ret == -ENOSPC)
  3577. ret = 1;
  3578. goto out;
  3579. }
  3580. free_space = btrfs_leaf_free_space(root, left);
  3581. if (free_space < data_size) {
  3582. ret = 1;
  3583. goto out;
  3584. }
  3585. return __push_leaf_left(trans, root, path, min_data_size,
  3586. empty, left, free_space, right_nritems,
  3587. max_slot);
  3588. out:
  3589. btrfs_tree_unlock(left);
  3590. free_extent_buffer(left);
  3591. return ret;
  3592. }
  3593. /*
  3594. * split the path's leaf in two, making sure there is at least data_size
  3595. * available for the resulting leaf level of the path.
  3596. */
  3597. static noinline void copy_for_split(struct btrfs_trans_handle *trans,
  3598. struct btrfs_root *root,
  3599. struct btrfs_path *path,
  3600. struct extent_buffer *l,
  3601. struct extent_buffer *right,
  3602. int slot, int mid, int nritems)
  3603. {
  3604. int data_copy_size;
  3605. int rt_data_off;
  3606. int i;
  3607. struct btrfs_disk_key disk_key;
  3608. struct btrfs_map_token token;
  3609. btrfs_init_map_token(&token);
  3610. nritems = nritems - mid;
  3611. btrfs_set_header_nritems(right, nritems);
  3612. data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
  3613. copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
  3614. btrfs_item_nr_offset(mid),
  3615. nritems * sizeof(struct btrfs_item));
  3616. copy_extent_buffer(right, l,
  3617. btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
  3618. data_copy_size, btrfs_leaf_data(l) +
  3619. leaf_data_end(root, l), data_copy_size);
  3620. rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
  3621. btrfs_item_end_nr(l, mid);
  3622. for (i = 0; i < nritems; i++) {
  3623. struct btrfs_item *item = btrfs_item_nr(i);
  3624. u32 ioff;
  3625. ioff = btrfs_token_item_offset(right, item, &token);
  3626. btrfs_set_token_item_offset(right, item,
  3627. ioff + rt_data_off, &token);
  3628. }
  3629. btrfs_set_header_nritems(l, mid);
  3630. btrfs_item_key(right, &disk_key, 0);
  3631. insert_ptr(trans, root, path, &disk_key, right->start,
  3632. path->slots[1] + 1, 1);
  3633. btrfs_mark_buffer_dirty(right);
  3634. btrfs_mark_buffer_dirty(l);
  3635. BUG_ON(path->slots[0] != slot);
  3636. if (mid <= slot) {
  3637. btrfs_tree_unlock(path->nodes[0]);
  3638. free_extent_buffer(path->nodes[0]);
  3639. path->nodes[0] = right;
  3640. path->slots[0] -= mid;
  3641. path->slots[1] += 1;
  3642. } else {
  3643. btrfs_tree_unlock(right);
  3644. free_extent_buffer(right);
  3645. }
  3646. BUG_ON(path->slots[0] < 0);
  3647. }
  3648. /*
  3649. * double splits happen when we need to insert a big item in the middle
  3650. * of a leaf. A double split can leave us with 3 mostly empty leaves:
  3651. * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
  3652. * A B C
  3653. *
  3654. * We avoid this by trying to push the items on either side of our target
  3655. * into the adjacent leaves. If all goes well we can avoid the double split
  3656. * completely.
  3657. */
  3658. static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
  3659. struct btrfs_root *root,
  3660. struct btrfs_path *path,
  3661. int data_size)
  3662. {
  3663. int ret;
  3664. int progress = 0;
  3665. int slot;
  3666. u32 nritems;
  3667. int space_needed = data_size;
  3668. slot = path->slots[0];
  3669. if (slot < btrfs_header_nritems(path->nodes[0]))
  3670. space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
  3671. /*
  3672. * try to push all the items after our slot into the
  3673. * right leaf
  3674. */
  3675. ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
  3676. if (ret < 0)
  3677. return ret;
  3678. if (ret == 0)
  3679. progress++;
  3680. nritems = btrfs_header_nritems(path->nodes[0]);
  3681. /*
  3682. * our goal is to get our slot at the start or end of a leaf. If
  3683. * we've done so we're done
  3684. */
  3685. if (path->slots[0] == 0 || path->slots[0] == nritems)
  3686. return 0;
  3687. if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
  3688. return 0;
  3689. /* try to push all the items before our slot into the next leaf */
  3690. slot = path->slots[0];
  3691. ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
  3692. if (ret < 0)
  3693. return ret;
  3694. if (ret == 0)
  3695. progress++;
  3696. if (progress)
  3697. return 0;
  3698. return 1;
  3699. }
  3700. /*
  3701. * split the path's leaf in two, making sure there is at least data_size
  3702. * available for the resulting leaf level of the path.
  3703. *
  3704. * returns 0 if all went well and < 0 on failure.
  3705. */
  3706. static noinline int split_leaf(struct btrfs_trans_handle *trans,
  3707. struct btrfs_root *root,
  3708. struct btrfs_key *ins_key,
  3709. struct btrfs_path *path, int data_size,
  3710. int extend)
  3711. {
  3712. struct btrfs_disk_key disk_key;
  3713. struct extent_buffer *l;
  3714. u32 nritems;
  3715. int mid;
  3716. int slot;
  3717. struct extent_buffer *right;
  3718. struct btrfs_fs_info *fs_info = root->fs_info;
  3719. int ret = 0;
  3720. int wret;
  3721. int split;
  3722. int num_doubles = 0;
  3723. int tried_avoid_double = 0;
  3724. l = path->nodes[0];
  3725. slot = path->slots[0];
  3726. if (extend && data_size + btrfs_item_size_nr(l, slot) +
  3727. sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
  3728. return -EOVERFLOW;
  3729. /* first try to make some room by pushing left and right */
  3730. if (data_size && path->nodes[1]) {
  3731. int space_needed = data_size;
  3732. if (slot < btrfs_header_nritems(l))
  3733. space_needed -= btrfs_leaf_free_space(root, l);
  3734. wret = push_leaf_right(trans, root, path, space_needed,
  3735. space_needed, 0, 0);
  3736. if (wret < 0)
  3737. return wret;
  3738. if (wret) {
  3739. wret = push_leaf_left(trans, root, path, space_needed,
  3740. space_needed, 0, (u32)-1);
  3741. if (wret < 0)
  3742. return wret;
  3743. }
  3744. l = path->nodes[0];
  3745. /* did the pushes work? */
  3746. if (btrfs_leaf_free_space(root, l) >= data_size)
  3747. return 0;
  3748. }
  3749. if (!path->nodes[1]) {
  3750. ret = insert_new_root(trans, root, path, 1);
  3751. if (ret)
  3752. return ret;
  3753. }
  3754. again:
  3755. split = 1;
  3756. l = path->nodes[0];
  3757. slot = path->slots[0];
  3758. nritems = btrfs_header_nritems(l);
  3759. mid = (nritems + 1) / 2;
  3760. if (mid <= slot) {
  3761. if (nritems == 1 ||
  3762. leaf_space_used(l, mid, nritems - mid) + data_size >
  3763. BTRFS_LEAF_DATA_SIZE(root)) {
  3764. if (slot >= nritems) {
  3765. split = 0;
  3766. } else {
  3767. mid = slot;
  3768. if (mid != nritems &&
  3769. leaf_space_used(l, mid, nritems - mid) +
  3770. data_size > BTRFS_LEAF_DATA_SIZE(root)) {
  3771. if (data_size && !tried_avoid_double)
  3772. goto push_for_double;
  3773. split = 2;
  3774. }
  3775. }
  3776. }
  3777. } else {
  3778. if (leaf_space_used(l, 0, mid) + data_size >
  3779. BTRFS_LEAF_DATA_SIZE(root)) {
  3780. if (!extend && data_size && slot == 0) {
  3781. split = 0;
  3782. } else if ((extend || !data_size) && slot == 0) {
  3783. mid = 1;
  3784. } else {
  3785. mid = slot;
  3786. if (mid != nritems &&
  3787. leaf_space_used(l, mid, nritems - mid) +
  3788. data_size > BTRFS_LEAF_DATA_SIZE(root)) {
  3789. if (data_size && !tried_avoid_double)
  3790. goto push_for_double;
  3791. split = 2;
  3792. }
  3793. }
  3794. }
  3795. }
  3796. if (split == 0)
  3797. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  3798. else
  3799. btrfs_item_key(l, &disk_key, mid);
  3800. right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3801. &disk_key, 0, l->start, 0);
  3802. if (IS_ERR(right))
  3803. return PTR_ERR(right);
  3804. root_add_used(root, root->nodesize);
  3805. memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
  3806. btrfs_set_header_bytenr(right, right->start);
  3807. btrfs_set_header_generation(right, trans->transid);
  3808. btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
  3809. btrfs_set_header_owner(right, root->root_key.objectid);
  3810. btrfs_set_header_level(right, 0);
  3811. write_extent_buffer(right, fs_info->fsid,
  3812. btrfs_header_fsid(), BTRFS_FSID_SIZE);
  3813. write_extent_buffer(right, fs_info->chunk_tree_uuid,
  3814. btrfs_header_chunk_tree_uuid(right),
  3815. BTRFS_UUID_SIZE);
  3816. if (split == 0) {
  3817. if (mid <= slot) {
  3818. btrfs_set_header_nritems(right, 0);
  3819. insert_ptr(trans, root, path, &disk_key, right->start,
  3820. path->slots[1] + 1, 1);
  3821. btrfs_tree_unlock(path->nodes[0]);
  3822. free_extent_buffer(path->nodes[0]);
  3823. path->nodes[0] = right;
  3824. path->slots[0] = 0;
  3825. path->slots[1] += 1;
  3826. } else {
  3827. btrfs_set_header_nritems(right, 0);
  3828. insert_ptr(trans, root, path, &disk_key, right->start,
  3829. path->slots[1], 1);
  3830. btrfs_tree_unlock(path->nodes[0]);
  3831. free_extent_buffer(path->nodes[0]);
  3832. path->nodes[0] = right;
  3833. path->slots[0] = 0;
  3834. if (path->slots[1] == 0)
  3835. fixup_low_keys(fs_info, path, &disk_key, 1);
  3836. }
  3837. btrfs_mark_buffer_dirty(right);
  3838. return ret;
  3839. }
  3840. copy_for_split(trans, root, path, l, right, slot, mid, nritems);
  3841. if (split == 2) {
  3842. BUG_ON(num_doubles != 0);
  3843. num_doubles++;
  3844. goto again;
  3845. }
  3846. return 0;
  3847. push_for_double:
  3848. push_for_double_split(trans, root, path, data_size);
  3849. tried_avoid_double = 1;
  3850. if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
  3851. return 0;
  3852. goto again;
  3853. }
  3854. static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
  3855. struct btrfs_root *root,
  3856. struct btrfs_path *path, int ins_len)
  3857. {
  3858. struct btrfs_key key;
  3859. struct extent_buffer *leaf;
  3860. struct btrfs_file_extent_item *fi;
  3861. u64 extent_len = 0;
  3862. u32 item_size;
  3863. int ret;
  3864. leaf = path->nodes[0];
  3865. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3866. BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
  3867. key.type != BTRFS_EXTENT_CSUM_KEY);
  3868. if (btrfs_leaf_free_space(root, leaf) >= ins_len)
  3869. return 0;
  3870. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3871. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3872. fi = btrfs_item_ptr(leaf, path->slots[0],
  3873. struct btrfs_file_extent_item);
  3874. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  3875. }
  3876. btrfs_release_path(path);
  3877. path->keep_locks = 1;
  3878. path->search_for_split = 1;
  3879. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3880. path->search_for_split = 0;
  3881. if (ret > 0)
  3882. ret = -EAGAIN;
  3883. if (ret < 0)
  3884. goto err;
  3885. ret = -EAGAIN;
  3886. leaf = path->nodes[0];
  3887. /* if our item isn't there, return now */
  3888. if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
  3889. goto err;
  3890. /* the leaf has changed, it now has room. return now */
  3891. if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
  3892. goto err;
  3893. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3894. fi = btrfs_item_ptr(leaf, path->slots[0],
  3895. struct btrfs_file_extent_item);
  3896. if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
  3897. goto err;
  3898. }
  3899. btrfs_set_path_blocking(path);
  3900. ret = split_leaf(trans, root, &key, path, ins_len, 1);
  3901. if (ret)
  3902. goto err;
  3903. path->keep_locks = 0;
  3904. btrfs_unlock_up_safe(path, 1);
  3905. return 0;
  3906. err:
  3907. path->keep_locks = 0;
  3908. return ret;
  3909. }
  3910. static noinline int split_item(struct btrfs_trans_handle *trans,
  3911. struct btrfs_root *root,
  3912. struct btrfs_path *path,
  3913. struct btrfs_key *new_key,
  3914. unsigned long split_offset)
  3915. {
  3916. struct extent_buffer *leaf;
  3917. struct btrfs_item *item;
  3918. struct btrfs_item *new_item;
  3919. int slot;
  3920. char *buf;
  3921. u32 nritems;
  3922. u32 item_size;
  3923. u32 orig_offset;
  3924. struct btrfs_disk_key disk_key;
  3925. leaf = path->nodes[0];
  3926. BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
  3927. btrfs_set_path_blocking(path);
  3928. item = btrfs_item_nr(path->slots[0]);
  3929. orig_offset = btrfs_item_offset(leaf, item);
  3930. item_size = btrfs_item_size(leaf, item);
  3931. buf = kmalloc(item_size, GFP_NOFS);
  3932. if (!buf)
  3933. return -ENOMEM;
  3934. read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
  3935. path->slots[0]), item_size);
  3936. slot = path->slots[0] + 1;
  3937. nritems = btrfs_header_nritems(leaf);
  3938. if (slot != nritems) {
  3939. /* shift the items */
  3940. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
  3941. btrfs_item_nr_offset(slot),
  3942. (nritems - slot) * sizeof(struct btrfs_item));
  3943. }
  3944. btrfs_cpu_key_to_disk(&disk_key, new_key);
  3945. btrfs_set_item_key(leaf, &disk_key, slot);
  3946. new_item = btrfs_item_nr(slot);
  3947. btrfs_set_item_offset(leaf, new_item, orig_offset);
  3948. btrfs_set_item_size(leaf, new_item, item_size - split_offset);
  3949. btrfs_set_item_offset(leaf, item,
  3950. orig_offset + item_size - split_offset);
  3951. btrfs_set_item_size(leaf, item, split_offset);
  3952. btrfs_set_header_nritems(leaf, nritems + 1);
  3953. /* write the data for the start of the original item */
  3954. write_extent_buffer(leaf, buf,
  3955. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3956. split_offset);
  3957. /* write the data for the new item */
  3958. write_extent_buffer(leaf, buf + split_offset,
  3959. btrfs_item_ptr_offset(leaf, slot),
  3960. item_size - split_offset);
  3961. btrfs_mark_buffer_dirty(leaf);
  3962. BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
  3963. kfree(buf);
  3964. return 0;
  3965. }
  3966. /*
  3967. * This function splits a single item into two items,
  3968. * giving 'new_key' to the new item and splitting the
  3969. * old one at split_offset (from the start of the item).
  3970. *
  3971. * The path may be released by this operation. After
  3972. * the split, the path is pointing to the old item. The
  3973. * new item is going to be in the same node as the old one.
  3974. *
  3975. * Note, the item being split must be smaller enough to live alone on
  3976. * a tree block with room for one extra struct btrfs_item
  3977. *
  3978. * This allows us to split the item in place, keeping a lock on the
  3979. * leaf the entire time.
  3980. */
  3981. int btrfs_split_item(struct btrfs_trans_handle *trans,
  3982. struct btrfs_root *root,
  3983. struct btrfs_path *path,
  3984. struct btrfs_key *new_key,
  3985. unsigned long split_offset)
  3986. {
  3987. int ret;
  3988. ret = setup_leaf_for_split(trans, root, path,
  3989. sizeof(struct btrfs_item));
  3990. if (ret)
  3991. return ret;
  3992. ret = split_item(trans, root, path, new_key, split_offset);
  3993. return ret;
  3994. }
  3995. /*
  3996. * This function duplicate a item, giving 'new_key' to the new item.
  3997. * It guarantees both items live in the same tree leaf and the new item
  3998. * is contiguous with the original item.
  3999. *
  4000. * This allows us to split file extent in place, keeping a lock on the
  4001. * leaf the entire time.
  4002. */
  4003. int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
  4004. struct btrfs_root *root,
  4005. struct btrfs_path *path,
  4006. struct btrfs_key *new_key)
  4007. {
  4008. struct extent_buffer *leaf;
  4009. int ret;
  4010. u32 item_size;
  4011. leaf = path->nodes[0];
  4012. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  4013. ret = setup_leaf_for_split(trans, root, path,
  4014. item_size + sizeof(struct btrfs_item));
  4015. if (ret)
  4016. return ret;
  4017. path->slots[0]++;
  4018. setup_items_for_insert(root, path, new_key, &item_size,
  4019. item_size, item_size +
  4020. sizeof(struct btrfs_item), 1);
  4021. leaf = path->nodes[0];
  4022. memcpy_extent_buffer(leaf,
  4023. btrfs_item_ptr_offset(leaf, path->slots[0]),
  4024. btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
  4025. item_size);
  4026. return 0;
  4027. }
  4028. /*
  4029. * make the item pointed to by the path smaller. new_size indicates
  4030. * how small to make it, and from_end tells us if we just chop bytes
  4031. * off the end of the item or if we shift the item to chop bytes off
  4032. * the front.
  4033. */
  4034. void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
  4035. u32 new_size, int from_end)
  4036. {
  4037. int slot;
  4038. struct extent_buffer *leaf;
  4039. struct btrfs_item *item;
  4040. u32 nritems;
  4041. unsigned int data_end;
  4042. unsigned int old_data_start;
  4043. unsigned int old_size;
  4044. unsigned int size_diff;
  4045. int i;
  4046. struct btrfs_map_token token;
  4047. btrfs_init_map_token(&token);
  4048. leaf = path->nodes[0];
  4049. slot = path->slots[0];
  4050. old_size = btrfs_item_size_nr(leaf, slot);
  4051. if (old_size == new_size)
  4052. return;
  4053. nritems = btrfs_header_nritems(leaf);
  4054. data_end = leaf_data_end(root, leaf);
  4055. old_data_start = btrfs_item_offset_nr(leaf, slot);
  4056. size_diff = old_size - new_size;
  4057. BUG_ON(slot < 0);
  4058. BUG_ON(slot >= nritems);
  4059. /*
  4060. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4061. */
  4062. /* first correct the data pointers */
  4063. for (i = slot; i < nritems; i++) {
  4064. u32 ioff;
  4065. item = btrfs_item_nr(i);
  4066. ioff = btrfs_token_item_offset(leaf, item, &token);
  4067. btrfs_set_token_item_offset(leaf, item,
  4068. ioff + size_diff, &token);
  4069. }
  4070. /* shift the data */
  4071. if (from_end) {
  4072. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4073. data_end + size_diff, btrfs_leaf_data(leaf) +
  4074. data_end, old_data_start + new_size - data_end);
  4075. } else {
  4076. struct btrfs_disk_key disk_key;
  4077. u64 offset;
  4078. btrfs_item_key(leaf, &disk_key, slot);
  4079. if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
  4080. unsigned long ptr;
  4081. struct btrfs_file_extent_item *fi;
  4082. fi = btrfs_item_ptr(leaf, slot,
  4083. struct btrfs_file_extent_item);
  4084. fi = (struct btrfs_file_extent_item *)(
  4085. (unsigned long)fi - size_diff);
  4086. if (btrfs_file_extent_type(leaf, fi) ==
  4087. BTRFS_FILE_EXTENT_INLINE) {
  4088. ptr = btrfs_item_ptr_offset(leaf, slot);
  4089. memmove_extent_buffer(leaf, ptr,
  4090. (unsigned long)fi,
  4091. BTRFS_FILE_EXTENT_INLINE_DATA_START);
  4092. }
  4093. }
  4094. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4095. data_end + size_diff, btrfs_leaf_data(leaf) +
  4096. data_end, old_data_start - data_end);
  4097. offset = btrfs_disk_key_offset(&disk_key);
  4098. btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
  4099. btrfs_set_item_key(leaf, &disk_key, slot);
  4100. if (slot == 0)
  4101. fixup_low_keys(root->fs_info, path, &disk_key, 1);
  4102. }
  4103. item = btrfs_item_nr(slot);
  4104. btrfs_set_item_size(leaf, item, new_size);
  4105. btrfs_mark_buffer_dirty(leaf);
  4106. if (btrfs_leaf_free_space(root, leaf) < 0) {
  4107. btrfs_print_leaf(root, leaf);
  4108. BUG();
  4109. }
  4110. }
  4111. /*
  4112. * make the item pointed to by the path bigger, data_size is the added size.
  4113. */
  4114. void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
  4115. u32 data_size)
  4116. {
  4117. int slot;
  4118. struct extent_buffer *leaf;
  4119. struct btrfs_item *item;
  4120. u32 nritems;
  4121. unsigned int data_end;
  4122. unsigned int old_data;
  4123. unsigned int old_size;
  4124. int i;
  4125. struct btrfs_map_token token;
  4126. btrfs_init_map_token(&token);
  4127. leaf = path->nodes[0];
  4128. nritems = btrfs_header_nritems(leaf);
  4129. data_end = leaf_data_end(root, leaf);
  4130. if (btrfs_leaf_free_space(root, leaf) < data_size) {
  4131. btrfs_print_leaf(root, leaf);
  4132. BUG();
  4133. }
  4134. slot = path->slots[0];
  4135. old_data = btrfs_item_end_nr(leaf, slot);
  4136. BUG_ON(slot < 0);
  4137. if (slot >= nritems) {
  4138. btrfs_print_leaf(root, leaf);
  4139. btrfs_crit(root->fs_info, "slot %d too large, nritems %d",
  4140. slot, nritems);
  4141. BUG_ON(1);
  4142. }
  4143. /*
  4144. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4145. */
  4146. /* first correct the data pointers */
  4147. for (i = slot; i < nritems; i++) {
  4148. u32 ioff;
  4149. item = btrfs_item_nr(i);
  4150. ioff = btrfs_token_item_offset(leaf, item, &token);
  4151. btrfs_set_token_item_offset(leaf, item,
  4152. ioff - data_size, &token);
  4153. }
  4154. /* shift the data */
  4155. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4156. data_end - data_size, btrfs_leaf_data(leaf) +
  4157. data_end, old_data - data_end);
  4158. data_end = old_data;
  4159. old_size = btrfs_item_size_nr(leaf, slot);
  4160. item = btrfs_item_nr(slot);
  4161. btrfs_set_item_size(leaf, item, old_size + data_size);
  4162. btrfs_mark_buffer_dirty(leaf);
  4163. if (btrfs_leaf_free_space(root, leaf) < 0) {
  4164. btrfs_print_leaf(root, leaf);
  4165. BUG();
  4166. }
  4167. }
  4168. /*
  4169. * this is a helper for btrfs_insert_empty_items, the main goal here is
  4170. * to save stack depth by doing the bulk of the work in a function
  4171. * that doesn't call btrfs_search_slot
  4172. */
  4173. void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
  4174. struct btrfs_key *cpu_key, u32 *data_size,
  4175. u32 total_data, u32 total_size, int nr)
  4176. {
  4177. struct btrfs_item *item;
  4178. int i;
  4179. u32 nritems;
  4180. unsigned int data_end;
  4181. struct btrfs_disk_key disk_key;
  4182. struct extent_buffer *leaf;
  4183. int slot;
  4184. struct btrfs_map_token token;
  4185. if (path->slots[0] == 0) {
  4186. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  4187. fixup_low_keys(root->fs_info, path, &disk_key, 1);
  4188. }
  4189. btrfs_unlock_up_safe(path, 1);
  4190. btrfs_init_map_token(&token);
  4191. leaf = path->nodes[0];
  4192. slot = path->slots[0];
  4193. nritems = btrfs_header_nritems(leaf);
  4194. data_end = leaf_data_end(root, leaf);
  4195. if (btrfs_leaf_free_space(root, leaf) < total_size) {
  4196. btrfs_print_leaf(root, leaf);
  4197. btrfs_crit(root->fs_info, "not enough freespace need %u have %d",
  4198. total_size, btrfs_leaf_free_space(root, leaf));
  4199. BUG();
  4200. }
  4201. if (slot != nritems) {
  4202. unsigned int old_data = btrfs_item_end_nr(leaf, slot);
  4203. if (old_data < data_end) {
  4204. btrfs_print_leaf(root, leaf);
  4205. btrfs_crit(root->fs_info, "slot %d old_data %d data_end %d",
  4206. slot, old_data, data_end);
  4207. BUG_ON(1);
  4208. }
  4209. /*
  4210. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4211. */
  4212. /* first correct the data pointers */
  4213. for (i = slot; i < nritems; i++) {
  4214. u32 ioff;
  4215. item = btrfs_item_nr( i);
  4216. ioff = btrfs_token_item_offset(leaf, item, &token);
  4217. btrfs_set_token_item_offset(leaf, item,
  4218. ioff - total_data, &token);
  4219. }
  4220. /* shift the items */
  4221. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
  4222. btrfs_item_nr_offset(slot),
  4223. (nritems - slot) * sizeof(struct btrfs_item));
  4224. /* shift the data */
  4225. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4226. data_end - total_data, btrfs_leaf_data(leaf) +
  4227. data_end, old_data - data_end);
  4228. data_end = old_data;
  4229. }
  4230. /* setup the item for the new data */
  4231. for (i = 0; i < nr; i++) {
  4232. btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
  4233. btrfs_set_item_key(leaf, &disk_key, slot + i);
  4234. item = btrfs_item_nr(slot + i);
  4235. btrfs_set_token_item_offset(leaf, item,
  4236. data_end - data_size[i], &token);
  4237. data_end -= data_size[i];
  4238. btrfs_set_token_item_size(leaf, item, data_size[i], &token);
  4239. }
  4240. btrfs_set_header_nritems(leaf, nritems + nr);
  4241. btrfs_mark_buffer_dirty(leaf);
  4242. if (btrfs_leaf_free_space(root, leaf) < 0) {
  4243. btrfs_print_leaf(root, leaf);
  4244. BUG();
  4245. }
  4246. }
  4247. /*
  4248. * Given a key and some data, insert items into the tree.
  4249. * This does all the path init required, making room in the tree if needed.
  4250. */
  4251. int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
  4252. struct btrfs_root *root,
  4253. struct btrfs_path *path,
  4254. struct btrfs_key *cpu_key, u32 *data_size,
  4255. int nr)
  4256. {
  4257. int ret = 0;
  4258. int slot;
  4259. int i;
  4260. u32 total_size = 0;
  4261. u32 total_data = 0;
  4262. for (i = 0; i < nr; i++)
  4263. total_data += data_size[i];
  4264. total_size = total_data + (nr * sizeof(struct btrfs_item));
  4265. ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
  4266. if (ret == 0)
  4267. return -EEXIST;
  4268. if (ret < 0)
  4269. return ret;
  4270. slot = path->slots[0];
  4271. BUG_ON(slot < 0);
  4272. setup_items_for_insert(root, path, cpu_key, data_size,
  4273. total_data, total_size, nr);
  4274. return 0;
  4275. }
  4276. /*
  4277. * Given a key and some data, insert an item into the tree.
  4278. * This does all the path init required, making room in the tree if needed.
  4279. */
  4280. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  4281. *root, struct btrfs_key *cpu_key, void *data, u32
  4282. data_size)
  4283. {
  4284. int ret = 0;
  4285. struct btrfs_path *path;
  4286. struct extent_buffer *leaf;
  4287. unsigned long ptr;
  4288. path = btrfs_alloc_path();
  4289. if (!path)
  4290. return -ENOMEM;
  4291. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  4292. if (!ret) {
  4293. leaf = path->nodes[0];
  4294. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  4295. write_extent_buffer(leaf, data, ptr, data_size);
  4296. btrfs_mark_buffer_dirty(leaf);
  4297. }
  4298. btrfs_free_path(path);
  4299. return ret;
  4300. }
  4301. /*
  4302. * delete the pointer from a given node.
  4303. *
  4304. * the tree should have been previously balanced so the deletion does not
  4305. * empty a node.
  4306. */
  4307. static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
  4308. int level, int slot)
  4309. {
  4310. struct extent_buffer *parent = path->nodes[level];
  4311. u32 nritems;
  4312. int ret;
  4313. nritems = btrfs_header_nritems(parent);
  4314. if (slot != nritems - 1) {
  4315. if (level)
  4316. tree_mod_log_eb_move(root->fs_info, parent, slot,
  4317. slot + 1, nritems - slot - 1);
  4318. memmove_extent_buffer(parent,
  4319. btrfs_node_key_ptr_offset(slot),
  4320. btrfs_node_key_ptr_offset(slot + 1),
  4321. sizeof(struct btrfs_key_ptr) *
  4322. (nritems - slot - 1));
  4323. } else if (level) {
  4324. ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
  4325. MOD_LOG_KEY_REMOVE, GFP_NOFS);
  4326. BUG_ON(ret < 0);
  4327. }
  4328. nritems--;
  4329. btrfs_set_header_nritems(parent, nritems);
  4330. if (nritems == 0 && parent == root->node) {
  4331. BUG_ON(btrfs_header_level(root->node) != 1);
  4332. /* just turn the root into a leaf and break */
  4333. btrfs_set_header_level(root->node, 0);
  4334. } else if (slot == 0) {
  4335. struct btrfs_disk_key disk_key;
  4336. btrfs_node_key(parent, &disk_key, 0);
  4337. fixup_low_keys(root->fs_info, path, &disk_key, level + 1);
  4338. }
  4339. btrfs_mark_buffer_dirty(parent);
  4340. }
  4341. /*
  4342. * a helper function to delete the leaf pointed to by path->slots[1] and
  4343. * path->nodes[1].
  4344. *
  4345. * This deletes the pointer in path->nodes[1] and frees the leaf
  4346. * block extent. zero is returned if it all worked out, < 0 otherwise.
  4347. *
  4348. * The path must have already been setup for deleting the leaf, including
  4349. * all the proper balancing. path->nodes[1] must be locked.
  4350. */
  4351. static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
  4352. struct btrfs_root *root,
  4353. struct btrfs_path *path,
  4354. struct extent_buffer *leaf)
  4355. {
  4356. WARN_ON(btrfs_header_generation(leaf) != trans->transid);
  4357. del_ptr(root, path, 1, path->slots[1]);
  4358. /*
  4359. * btrfs_free_extent is expensive, we want to make sure we
  4360. * aren't holding any locks when we call it
  4361. */
  4362. btrfs_unlock_up_safe(path, 0);
  4363. root_sub_used(root, leaf->len);
  4364. extent_buffer_get(leaf);
  4365. btrfs_free_tree_block(trans, root, leaf, 0, 1);
  4366. free_extent_buffer_stale(leaf);
  4367. }
  4368. /*
  4369. * delete the item at the leaf level in path. If that empties
  4370. * the leaf, remove it from the tree
  4371. */
  4372. int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4373. struct btrfs_path *path, int slot, int nr)
  4374. {
  4375. struct extent_buffer *leaf;
  4376. struct btrfs_item *item;
  4377. u32 last_off;
  4378. u32 dsize = 0;
  4379. int ret = 0;
  4380. int wret;
  4381. int i;
  4382. u32 nritems;
  4383. struct btrfs_map_token token;
  4384. btrfs_init_map_token(&token);
  4385. leaf = path->nodes[0];
  4386. last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
  4387. for (i = 0; i < nr; i++)
  4388. dsize += btrfs_item_size_nr(leaf, slot + i);
  4389. nritems = btrfs_header_nritems(leaf);
  4390. if (slot + nr != nritems) {
  4391. int data_end = leaf_data_end(root, leaf);
  4392. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4393. data_end + dsize,
  4394. btrfs_leaf_data(leaf) + data_end,
  4395. last_off - data_end);
  4396. for (i = slot + nr; i < nritems; i++) {
  4397. u32 ioff;
  4398. item = btrfs_item_nr(i);
  4399. ioff = btrfs_token_item_offset(leaf, item, &token);
  4400. btrfs_set_token_item_offset(leaf, item,
  4401. ioff + dsize, &token);
  4402. }
  4403. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
  4404. btrfs_item_nr_offset(slot + nr),
  4405. sizeof(struct btrfs_item) *
  4406. (nritems - slot - nr));
  4407. }
  4408. btrfs_set_header_nritems(leaf, nritems - nr);
  4409. nritems -= nr;
  4410. /* delete the leaf if we've emptied it */
  4411. if (nritems == 0) {
  4412. if (leaf == root->node) {
  4413. btrfs_set_header_level(leaf, 0);
  4414. } else {
  4415. btrfs_set_path_blocking(path);
  4416. clean_tree_block(trans, root->fs_info, leaf);
  4417. btrfs_del_leaf(trans, root, path, leaf);
  4418. }
  4419. } else {
  4420. int used = leaf_space_used(leaf, 0, nritems);
  4421. if (slot == 0) {
  4422. struct btrfs_disk_key disk_key;
  4423. btrfs_item_key(leaf, &disk_key, 0);
  4424. fixup_low_keys(root->fs_info, path, &disk_key, 1);
  4425. }
  4426. /* delete the leaf if it is mostly empty */
  4427. if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
  4428. /* push_leaf_left fixes the path.
  4429. * make sure the path still points to our leaf
  4430. * for possible call to del_ptr below
  4431. */
  4432. slot = path->slots[1];
  4433. extent_buffer_get(leaf);
  4434. btrfs_set_path_blocking(path);
  4435. wret = push_leaf_left(trans, root, path, 1, 1,
  4436. 1, (u32)-1);
  4437. if (wret < 0 && wret != -ENOSPC)
  4438. ret = wret;
  4439. if (path->nodes[0] == leaf &&
  4440. btrfs_header_nritems(leaf)) {
  4441. wret = push_leaf_right(trans, root, path, 1,
  4442. 1, 1, 0);
  4443. if (wret < 0 && wret != -ENOSPC)
  4444. ret = wret;
  4445. }
  4446. if (btrfs_header_nritems(leaf) == 0) {
  4447. path->slots[1] = slot;
  4448. btrfs_del_leaf(trans, root, path, leaf);
  4449. free_extent_buffer(leaf);
  4450. ret = 0;
  4451. } else {
  4452. /* if we're still in the path, make sure
  4453. * we're dirty. Otherwise, one of the
  4454. * push_leaf functions must have already
  4455. * dirtied this buffer
  4456. */
  4457. if (path->nodes[0] == leaf)
  4458. btrfs_mark_buffer_dirty(leaf);
  4459. free_extent_buffer(leaf);
  4460. }
  4461. } else {
  4462. btrfs_mark_buffer_dirty(leaf);
  4463. }
  4464. }
  4465. return ret;
  4466. }
  4467. /*
  4468. * search the tree again to find a leaf with lesser keys
  4469. * returns 0 if it found something or 1 if there are no lesser leaves.
  4470. * returns < 0 on io errors.
  4471. *
  4472. * This may release the path, and so you may lose any locks held at the
  4473. * time you call it.
  4474. */
  4475. int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
  4476. {
  4477. struct btrfs_key key;
  4478. struct btrfs_disk_key found_key;
  4479. int ret;
  4480. btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
  4481. if (key.offset > 0) {
  4482. key.offset--;
  4483. } else if (key.type > 0) {
  4484. key.type--;
  4485. key.offset = (u64)-1;
  4486. } else if (key.objectid > 0) {
  4487. key.objectid--;
  4488. key.type = (u8)-1;
  4489. key.offset = (u64)-1;
  4490. } else {
  4491. return 1;
  4492. }
  4493. btrfs_release_path(path);
  4494. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4495. if (ret < 0)
  4496. return ret;
  4497. btrfs_item_key(path->nodes[0], &found_key, 0);
  4498. ret = comp_keys(&found_key, &key);
  4499. /*
  4500. * We might have had an item with the previous key in the tree right
  4501. * before we released our path. And after we released our path, that
  4502. * item might have been pushed to the first slot (0) of the leaf we
  4503. * were holding due to a tree balance. Alternatively, an item with the
  4504. * previous key can exist as the only element of a leaf (big fat item).
  4505. * Therefore account for these 2 cases, so that our callers (like
  4506. * btrfs_previous_item) don't miss an existing item with a key matching
  4507. * the previous key we computed above.
  4508. */
  4509. if (ret <= 0)
  4510. return 0;
  4511. return 1;
  4512. }
  4513. /*
  4514. * A helper function to walk down the tree starting at min_key, and looking
  4515. * for nodes or leaves that are have a minimum transaction id.
  4516. * This is used by the btree defrag code, and tree logging
  4517. *
  4518. * This does not cow, but it does stuff the starting key it finds back
  4519. * into min_key, so you can call btrfs_search_slot with cow=1 on the
  4520. * key and get a writable path.
  4521. *
  4522. * This does lock as it descends, and path->keep_locks should be set
  4523. * to 1 by the caller.
  4524. *
  4525. * This honors path->lowest_level to prevent descent past a given level
  4526. * of the tree.
  4527. *
  4528. * min_trans indicates the oldest transaction that you are interested
  4529. * in walking through. Any nodes or leaves older than min_trans are
  4530. * skipped over (without reading them).
  4531. *
  4532. * returns zero if something useful was found, < 0 on error and 1 if there
  4533. * was nothing in the tree that matched the search criteria.
  4534. */
  4535. int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
  4536. struct btrfs_path *path,
  4537. u64 min_trans)
  4538. {
  4539. struct extent_buffer *cur;
  4540. struct btrfs_key found_key;
  4541. int slot;
  4542. int sret;
  4543. u32 nritems;
  4544. int level;
  4545. int ret = 1;
  4546. int keep_locks = path->keep_locks;
  4547. path->keep_locks = 1;
  4548. again:
  4549. cur = btrfs_read_lock_root_node(root);
  4550. level = btrfs_header_level(cur);
  4551. WARN_ON(path->nodes[level]);
  4552. path->nodes[level] = cur;
  4553. path->locks[level] = BTRFS_READ_LOCK;
  4554. if (btrfs_header_generation(cur) < min_trans) {
  4555. ret = 1;
  4556. goto out;
  4557. }
  4558. while (1) {
  4559. nritems = btrfs_header_nritems(cur);
  4560. level = btrfs_header_level(cur);
  4561. sret = bin_search(cur, min_key, level, &slot);
  4562. /* at the lowest level, we're done, setup the path and exit */
  4563. if (level == path->lowest_level) {
  4564. if (slot >= nritems)
  4565. goto find_next_key;
  4566. ret = 0;
  4567. path->slots[level] = slot;
  4568. btrfs_item_key_to_cpu(cur, &found_key, slot);
  4569. goto out;
  4570. }
  4571. if (sret && slot > 0)
  4572. slot--;
  4573. /*
  4574. * check this node pointer against the min_trans parameters.
  4575. * If it is too old, old, skip to the next one.
  4576. */
  4577. while (slot < nritems) {
  4578. u64 gen;
  4579. gen = btrfs_node_ptr_generation(cur, slot);
  4580. if (gen < min_trans) {
  4581. slot++;
  4582. continue;
  4583. }
  4584. break;
  4585. }
  4586. find_next_key:
  4587. /*
  4588. * we didn't find a candidate key in this node, walk forward
  4589. * and find another one
  4590. */
  4591. if (slot >= nritems) {
  4592. path->slots[level] = slot;
  4593. btrfs_set_path_blocking(path);
  4594. sret = btrfs_find_next_key(root, path, min_key, level,
  4595. min_trans);
  4596. if (sret == 0) {
  4597. btrfs_release_path(path);
  4598. goto again;
  4599. } else {
  4600. goto out;
  4601. }
  4602. }
  4603. /* save our key for returning back */
  4604. btrfs_node_key_to_cpu(cur, &found_key, slot);
  4605. path->slots[level] = slot;
  4606. if (level == path->lowest_level) {
  4607. ret = 0;
  4608. goto out;
  4609. }
  4610. btrfs_set_path_blocking(path);
  4611. cur = read_node_slot(root, cur, slot);
  4612. if (IS_ERR(cur)) {
  4613. ret = PTR_ERR(cur);
  4614. goto out;
  4615. }
  4616. btrfs_tree_read_lock(cur);
  4617. path->locks[level - 1] = BTRFS_READ_LOCK;
  4618. path->nodes[level - 1] = cur;
  4619. unlock_up(path, level, 1, 0, NULL);
  4620. btrfs_clear_path_blocking(path, NULL, 0);
  4621. }
  4622. out:
  4623. path->keep_locks = keep_locks;
  4624. if (ret == 0) {
  4625. btrfs_unlock_up_safe(path, path->lowest_level + 1);
  4626. btrfs_set_path_blocking(path);
  4627. memcpy(min_key, &found_key, sizeof(found_key));
  4628. }
  4629. return ret;
  4630. }
  4631. static int tree_move_down(struct btrfs_root *root,
  4632. struct btrfs_path *path,
  4633. int *level, int root_level)
  4634. {
  4635. struct extent_buffer *eb;
  4636. BUG_ON(*level == 0);
  4637. eb = read_node_slot(root, path->nodes[*level], path->slots[*level]);
  4638. if (IS_ERR(eb))
  4639. return PTR_ERR(eb);
  4640. path->nodes[*level - 1] = eb;
  4641. path->slots[*level - 1] = 0;
  4642. (*level)--;
  4643. return 0;
  4644. }
  4645. static int tree_move_next_or_upnext(struct btrfs_root *root,
  4646. struct btrfs_path *path,
  4647. int *level, int root_level)
  4648. {
  4649. int ret = 0;
  4650. int nritems;
  4651. nritems = btrfs_header_nritems(path->nodes[*level]);
  4652. path->slots[*level]++;
  4653. while (path->slots[*level] >= nritems) {
  4654. if (*level == root_level)
  4655. return -1;
  4656. /* move upnext */
  4657. path->slots[*level] = 0;
  4658. free_extent_buffer(path->nodes[*level]);
  4659. path->nodes[*level] = NULL;
  4660. (*level)++;
  4661. path->slots[*level]++;
  4662. nritems = btrfs_header_nritems(path->nodes[*level]);
  4663. ret = 1;
  4664. }
  4665. return ret;
  4666. }
  4667. /*
  4668. * Returns 1 if it had to move up and next. 0 is returned if it moved only next
  4669. * or down.
  4670. */
  4671. static int tree_advance(struct btrfs_root *root,
  4672. struct btrfs_path *path,
  4673. int *level, int root_level,
  4674. int allow_down,
  4675. struct btrfs_key *key)
  4676. {
  4677. int ret;
  4678. if (*level == 0 || !allow_down) {
  4679. ret = tree_move_next_or_upnext(root, path, level, root_level);
  4680. } else {
  4681. ret = tree_move_down(root, path, level, root_level);
  4682. }
  4683. if (ret >= 0) {
  4684. if (*level == 0)
  4685. btrfs_item_key_to_cpu(path->nodes[*level], key,
  4686. path->slots[*level]);
  4687. else
  4688. btrfs_node_key_to_cpu(path->nodes[*level], key,
  4689. path->slots[*level]);
  4690. }
  4691. return ret;
  4692. }
  4693. static int tree_compare_item(struct btrfs_root *left_root,
  4694. struct btrfs_path *left_path,
  4695. struct btrfs_path *right_path,
  4696. char *tmp_buf)
  4697. {
  4698. int cmp;
  4699. int len1, len2;
  4700. unsigned long off1, off2;
  4701. len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
  4702. len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
  4703. if (len1 != len2)
  4704. return 1;
  4705. off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
  4706. off2 = btrfs_item_ptr_offset(right_path->nodes[0],
  4707. right_path->slots[0]);
  4708. read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
  4709. cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
  4710. if (cmp)
  4711. return 1;
  4712. return 0;
  4713. }
  4714. #define ADVANCE 1
  4715. #define ADVANCE_ONLY_NEXT -1
  4716. /*
  4717. * This function compares two trees and calls the provided callback for
  4718. * every changed/new/deleted item it finds.
  4719. * If shared tree blocks are encountered, whole subtrees are skipped, making
  4720. * the compare pretty fast on snapshotted subvolumes.
  4721. *
  4722. * This currently works on commit roots only. As commit roots are read only,
  4723. * we don't do any locking. The commit roots are protected with transactions.
  4724. * Transactions are ended and rejoined when a commit is tried in between.
  4725. *
  4726. * This function checks for modifications done to the trees while comparing.
  4727. * If it detects a change, it aborts immediately.
  4728. */
  4729. int btrfs_compare_trees(struct btrfs_root *left_root,
  4730. struct btrfs_root *right_root,
  4731. btrfs_changed_cb_t changed_cb, void *ctx)
  4732. {
  4733. int ret;
  4734. int cmp;
  4735. struct btrfs_path *left_path = NULL;
  4736. struct btrfs_path *right_path = NULL;
  4737. struct btrfs_key left_key;
  4738. struct btrfs_key right_key;
  4739. char *tmp_buf = NULL;
  4740. int left_root_level;
  4741. int right_root_level;
  4742. int left_level;
  4743. int right_level;
  4744. int left_end_reached;
  4745. int right_end_reached;
  4746. int advance_left;
  4747. int advance_right;
  4748. u64 left_blockptr;
  4749. u64 right_blockptr;
  4750. u64 left_gen;
  4751. u64 right_gen;
  4752. left_path = btrfs_alloc_path();
  4753. if (!left_path) {
  4754. ret = -ENOMEM;
  4755. goto out;
  4756. }
  4757. right_path = btrfs_alloc_path();
  4758. if (!right_path) {
  4759. ret = -ENOMEM;
  4760. goto out;
  4761. }
  4762. tmp_buf = kmalloc(left_root->nodesize, GFP_KERNEL | __GFP_NOWARN);
  4763. if (!tmp_buf) {
  4764. tmp_buf = vmalloc(left_root->nodesize);
  4765. if (!tmp_buf) {
  4766. ret = -ENOMEM;
  4767. goto out;
  4768. }
  4769. }
  4770. left_path->search_commit_root = 1;
  4771. left_path->skip_locking = 1;
  4772. right_path->search_commit_root = 1;
  4773. right_path->skip_locking = 1;
  4774. /*
  4775. * Strategy: Go to the first items of both trees. Then do
  4776. *
  4777. * If both trees are at level 0
  4778. * Compare keys of current items
  4779. * If left < right treat left item as new, advance left tree
  4780. * and repeat
  4781. * If left > right treat right item as deleted, advance right tree
  4782. * and repeat
  4783. * If left == right do deep compare of items, treat as changed if
  4784. * needed, advance both trees and repeat
  4785. * If both trees are at the same level but not at level 0
  4786. * Compare keys of current nodes/leafs
  4787. * If left < right advance left tree and repeat
  4788. * If left > right advance right tree and repeat
  4789. * If left == right compare blockptrs of the next nodes/leafs
  4790. * If they match advance both trees but stay at the same level
  4791. * and repeat
  4792. * If they don't match advance both trees while allowing to go
  4793. * deeper and repeat
  4794. * If tree levels are different
  4795. * Advance the tree that needs it and repeat
  4796. *
  4797. * Advancing a tree means:
  4798. * If we are at level 0, try to go to the next slot. If that's not
  4799. * possible, go one level up and repeat. Stop when we found a level
  4800. * where we could go to the next slot. We may at this point be on a
  4801. * node or a leaf.
  4802. *
  4803. * If we are not at level 0 and not on shared tree blocks, go one
  4804. * level deeper.
  4805. *
  4806. * If we are not at level 0 and on shared tree blocks, go one slot to
  4807. * the right if possible or go up and right.
  4808. */
  4809. down_read(&left_root->fs_info->commit_root_sem);
  4810. left_level = btrfs_header_level(left_root->commit_root);
  4811. left_root_level = left_level;
  4812. left_path->nodes[left_level] = left_root->commit_root;
  4813. extent_buffer_get(left_path->nodes[left_level]);
  4814. right_level = btrfs_header_level(right_root->commit_root);
  4815. right_root_level = right_level;
  4816. right_path->nodes[right_level] = right_root->commit_root;
  4817. extent_buffer_get(right_path->nodes[right_level]);
  4818. up_read(&left_root->fs_info->commit_root_sem);
  4819. if (left_level == 0)
  4820. btrfs_item_key_to_cpu(left_path->nodes[left_level],
  4821. &left_key, left_path->slots[left_level]);
  4822. else
  4823. btrfs_node_key_to_cpu(left_path->nodes[left_level],
  4824. &left_key, left_path->slots[left_level]);
  4825. if (right_level == 0)
  4826. btrfs_item_key_to_cpu(right_path->nodes[right_level],
  4827. &right_key, right_path->slots[right_level]);
  4828. else
  4829. btrfs_node_key_to_cpu(right_path->nodes[right_level],
  4830. &right_key, right_path->slots[right_level]);
  4831. left_end_reached = right_end_reached = 0;
  4832. advance_left = advance_right = 0;
  4833. while (1) {
  4834. if (advance_left && !left_end_reached) {
  4835. ret = tree_advance(left_root, left_path, &left_level,
  4836. left_root_level,
  4837. advance_left != ADVANCE_ONLY_NEXT,
  4838. &left_key);
  4839. if (ret == -1)
  4840. left_end_reached = ADVANCE;
  4841. else if (ret < 0)
  4842. goto out;
  4843. advance_left = 0;
  4844. }
  4845. if (advance_right && !right_end_reached) {
  4846. ret = tree_advance(right_root, right_path, &right_level,
  4847. right_root_level,
  4848. advance_right != ADVANCE_ONLY_NEXT,
  4849. &right_key);
  4850. if (ret == -1)
  4851. right_end_reached = ADVANCE;
  4852. else if (ret < 0)
  4853. goto out;
  4854. advance_right = 0;
  4855. }
  4856. if (left_end_reached && right_end_reached) {
  4857. ret = 0;
  4858. goto out;
  4859. } else if (left_end_reached) {
  4860. if (right_level == 0) {
  4861. ret = changed_cb(left_root, right_root,
  4862. left_path, right_path,
  4863. &right_key,
  4864. BTRFS_COMPARE_TREE_DELETED,
  4865. ctx);
  4866. if (ret < 0)
  4867. goto out;
  4868. }
  4869. advance_right = ADVANCE;
  4870. continue;
  4871. } else if (right_end_reached) {
  4872. if (left_level == 0) {
  4873. ret = changed_cb(left_root, right_root,
  4874. left_path, right_path,
  4875. &left_key,
  4876. BTRFS_COMPARE_TREE_NEW,
  4877. ctx);
  4878. if (ret < 0)
  4879. goto out;
  4880. }
  4881. advance_left = ADVANCE;
  4882. continue;
  4883. }
  4884. if (left_level == 0 && right_level == 0) {
  4885. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4886. if (cmp < 0) {
  4887. ret = changed_cb(left_root, right_root,
  4888. left_path, right_path,
  4889. &left_key,
  4890. BTRFS_COMPARE_TREE_NEW,
  4891. ctx);
  4892. if (ret < 0)
  4893. goto out;
  4894. advance_left = ADVANCE;
  4895. } else if (cmp > 0) {
  4896. ret = changed_cb(left_root, right_root,
  4897. left_path, right_path,
  4898. &right_key,
  4899. BTRFS_COMPARE_TREE_DELETED,
  4900. ctx);
  4901. if (ret < 0)
  4902. goto out;
  4903. advance_right = ADVANCE;
  4904. } else {
  4905. enum btrfs_compare_tree_result result;
  4906. WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
  4907. ret = tree_compare_item(left_root, left_path,
  4908. right_path, tmp_buf);
  4909. if (ret)
  4910. result = BTRFS_COMPARE_TREE_CHANGED;
  4911. else
  4912. result = BTRFS_COMPARE_TREE_SAME;
  4913. ret = changed_cb(left_root, right_root,
  4914. left_path, right_path,
  4915. &left_key, result, ctx);
  4916. if (ret < 0)
  4917. goto out;
  4918. advance_left = ADVANCE;
  4919. advance_right = ADVANCE;
  4920. }
  4921. } else if (left_level == right_level) {
  4922. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4923. if (cmp < 0) {
  4924. advance_left = ADVANCE;
  4925. } else if (cmp > 0) {
  4926. advance_right = ADVANCE;
  4927. } else {
  4928. left_blockptr = btrfs_node_blockptr(
  4929. left_path->nodes[left_level],
  4930. left_path->slots[left_level]);
  4931. right_blockptr = btrfs_node_blockptr(
  4932. right_path->nodes[right_level],
  4933. right_path->slots[right_level]);
  4934. left_gen = btrfs_node_ptr_generation(
  4935. left_path->nodes[left_level],
  4936. left_path->slots[left_level]);
  4937. right_gen = btrfs_node_ptr_generation(
  4938. right_path->nodes[right_level],
  4939. right_path->slots[right_level]);
  4940. if (left_blockptr == right_blockptr &&
  4941. left_gen == right_gen) {
  4942. /*
  4943. * As we're on a shared block, don't
  4944. * allow to go deeper.
  4945. */
  4946. advance_left = ADVANCE_ONLY_NEXT;
  4947. advance_right = ADVANCE_ONLY_NEXT;
  4948. } else {
  4949. advance_left = ADVANCE;
  4950. advance_right = ADVANCE;
  4951. }
  4952. }
  4953. } else if (left_level < right_level) {
  4954. advance_right = ADVANCE;
  4955. } else {
  4956. advance_left = ADVANCE;
  4957. }
  4958. }
  4959. out:
  4960. btrfs_free_path(left_path);
  4961. btrfs_free_path(right_path);
  4962. kvfree(tmp_buf);
  4963. return ret;
  4964. }
  4965. /*
  4966. * this is similar to btrfs_next_leaf, but does not try to preserve
  4967. * and fixup the path. It looks for and returns the next key in the
  4968. * tree based on the current path and the min_trans parameters.
  4969. *
  4970. * 0 is returned if another key is found, < 0 if there are any errors
  4971. * and 1 is returned if there are no higher keys in the tree
  4972. *
  4973. * path->keep_locks should be set to 1 on the search made before
  4974. * calling this function.
  4975. */
  4976. int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
  4977. struct btrfs_key *key, int level, u64 min_trans)
  4978. {
  4979. int slot;
  4980. struct extent_buffer *c;
  4981. WARN_ON(!path->keep_locks);
  4982. while (level < BTRFS_MAX_LEVEL) {
  4983. if (!path->nodes[level])
  4984. return 1;
  4985. slot = path->slots[level] + 1;
  4986. c = path->nodes[level];
  4987. next:
  4988. if (slot >= btrfs_header_nritems(c)) {
  4989. int ret;
  4990. int orig_lowest;
  4991. struct btrfs_key cur_key;
  4992. if (level + 1 >= BTRFS_MAX_LEVEL ||
  4993. !path->nodes[level + 1])
  4994. return 1;
  4995. if (path->locks[level + 1]) {
  4996. level++;
  4997. continue;
  4998. }
  4999. slot = btrfs_header_nritems(c) - 1;
  5000. if (level == 0)
  5001. btrfs_item_key_to_cpu(c, &cur_key, slot);
  5002. else
  5003. btrfs_node_key_to_cpu(c, &cur_key, slot);
  5004. orig_lowest = path->lowest_level;
  5005. btrfs_release_path(path);
  5006. path->lowest_level = level;
  5007. ret = btrfs_search_slot(NULL, root, &cur_key, path,
  5008. 0, 0);
  5009. path->lowest_level = orig_lowest;
  5010. if (ret < 0)
  5011. return ret;
  5012. c = path->nodes[level];
  5013. slot = path->slots[level];
  5014. if (ret == 0)
  5015. slot++;
  5016. goto next;
  5017. }
  5018. if (level == 0)
  5019. btrfs_item_key_to_cpu(c, key, slot);
  5020. else {
  5021. u64 gen = btrfs_node_ptr_generation(c, slot);
  5022. if (gen < min_trans) {
  5023. slot++;
  5024. goto next;
  5025. }
  5026. btrfs_node_key_to_cpu(c, key, slot);
  5027. }
  5028. return 0;
  5029. }
  5030. return 1;
  5031. }
  5032. /*
  5033. * search the tree again to find a leaf with greater keys
  5034. * returns 0 if it found something or 1 if there are no greater leaves.
  5035. * returns < 0 on io errors.
  5036. */
  5037. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
  5038. {
  5039. return btrfs_next_old_leaf(root, path, 0);
  5040. }
  5041. int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
  5042. u64 time_seq)
  5043. {
  5044. int slot;
  5045. int level;
  5046. struct extent_buffer *c;
  5047. struct extent_buffer *next;
  5048. struct btrfs_key key;
  5049. u32 nritems;
  5050. int ret;
  5051. int old_spinning = path->leave_spinning;
  5052. int next_rw_lock = 0;
  5053. nritems = btrfs_header_nritems(path->nodes[0]);
  5054. if (nritems == 0)
  5055. return 1;
  5056. btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
  5057. again:
  5058. level = 1;
  5059. next = NULL;
  5060. next_rw_lock = 0;
  5061. btrfs_release_path(path);
  5062. path->keep_locks = 1;
  5063. path->leave_spinning = 1;
  5064. if (time_seq)
  5065. ret = btrfs_search_old_slot(root, &key, path, time_seq);
  5066. else
  5067. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5068. path->keep_locks = 0;
  5069. if (ret < 0)
  5070. return ret;
  5071. nritems = btrfs_header_nritems(path->nodes[0]);
  5072. /*
  5073. * by releasing the path above we dropped all our locks. A balance
  5074. * could have added more items next to the key that used to be
  5075. * at the very end of the block. So, check again here and
  5076. * advance the path if there are now more items available.
  5077. */
  5078. if (nritems > 0 && path->slots[0] < nritems - 1) {
  5079. if (ret == 0)
  5080. path->slots[0]++;
  5081. ret = 0;
  5082. goto done;
  5083. }
  5084. /*
  5085. * So the above check misses one case:
  5086. * - after releasing the path above, someone has removed the item that
  5087. * used to be at the very end of the block, and balance between leafs
  5088. * gets another one with bigger key.offset to replace it.
  5089. *
  5090. * This one should be returned as well, or we can get leaf corruption
  5091. * later(esp. in __btrfs_drop_extents()).
  5092. *
  5093. * And a bit more explanation about this check,
  5094. * with ret > 0, the key isn't found, the path points to the slot
  5095. * where it should be inserted, so the path->slots[0] item must be the
  5096. * bigger one.
  5097. */
  5098. if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
  5099. ret = 0;
  5100. goto done;
  5101. }
  5102. while (level < BTRFS_MAX_LEVEL) {
  5103. if (!path->nodes[level]) {
  5104. ret = 1;
  5105. goto done;
  5106. }
  5107. slot = path->slots[level] + 1;
  5108. c = path->nodes[level];
  5109. if (slot >= btrfs_header_nritems(c)) {
  5110. level++;
  5111. if (level == BTRFS_MAX_LEVEL) {
  5112. ret = 1;
  5113. goto done;
  5114. }
  5115. continue;
  5116. }
  5117. if (next) {
  5118. btrfs_tree_unlock_rw(next, next_rw_lock);
  5119. free_extent_buffer(next);
  5120. }
  5121. next = c;
  5122. next_rw_lock = path->locks[level];
  5123. ret = read_block_for_search(NULL, root, path, &next, level,
  5124. slot, &key, 0);
  5125. if (ret == -EAGAIN)
  5126. goto again;
  5127. if (ret < 0) {
  5128. btrfs_release_path(path);
  5129. goto done;
  5130. }
  5131. if (!path->skip_locking) {
  5132. ret = btrfs_try_tree_read_lock(next);
  5133. if (!ret && time_seq) {
  5134. /*
  5135. * If we don't get the lock, we may be racing
  5136. * with push_leaf_left, holding that lock while
  5137. * itself waiting for the leaf we've currently
  5138. * locked. To solve this situation, we give up
  5139. * on our lock and cycle.
  5140. */
  5141. free_extent_buffer(next);
  5142. btrfs_release_path(path);
  5143. cond_resched();
  5144. goto again;
  5145. }
  5146. if (!ret) {
  5147. btrfs_set_path_blocking(path);
  5148. btrfs_tree_read_lock(next);
  5149. btrfs_clear_path_blocking(path, next,
  5150. BTRFS_READ_LOCK);
  5151. }
  5152. next_rw_lock = BTRFS_READ_LOCK;
  5153. }
  5154. break;
  5155. }
  5156. path->slots[level] = slot;
  5157. while (1) {
  5158. level--;
  5159. c = path->nodes[level];
  5160. if (path->locks[level])
  5161. btrfs_tree_unlock_rw(c, path->locks[level]);
  5162. free_extent_buffer(c);
  5163. path->nodes[level] = next;
  5164. path->slots[level] = 0;
  5165. if (!path->skip_locking)
  5166. path->locks[level] = next_rw_lock;
  5167. if (!level)
  5168. break;
  5169. ret = read_block_for_search(NULL, root, path, &next, level,
  5170. 0, &key, 0);
  5171. if (ret == -EAGAIN)
  5172. goto again;
  5173. if (ret < 0) {
  5174. btrfs_release_path(path);
  5175. goto done;
  5176. }
  5177. if (!path->skip_locking) {
  5178. ret = btrfs_try_tree_read_lock(next);
  5179. if (!ret) {
  5180. btrfs_set_path_blocking(path);
  5181. btrfs_tree_read_lock(next);
  5182. btrfs_clear_path_blocking(path, next,
  5183. BTRFS_READ_LOCK);
  5184. }
  5185. next_rw_lock = BTRFS_READ_LOCK;
  5186. }
  5187. }
  5188. ret = 0;
  5189. done:
  5190. unlock_up(path, 0, 1, 0, NULL);
  5191. path->leave_spinning = old_spinning;
  5192. if (!old_spinning)
  5193. btrfs_set_path_blocking(path);
  5194. return ret;
  5195. }
  5196. /*
  5197. * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
  5198. * searching until it gets past min_objectid or finds an item of 'type'
  5199. *
  5200. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5201. */
  5202. int btrfs_previous_item(struct btrfs_root *root,
  5203. struct btrfs_path *path, u64 min_objectid,
  5204. int type)
  5205. {
  5206. struct btrfs_key found_key;
  5207. struct extent_buffer *leaf;
  5208. u32 nritems;
  5209. int ret;
  5210. while (1) {
  5211. if (path->slots[0] == 0) {
  5212. btrfs_set_path_blocking(path);
  5213. ret = btrfs_prev_leaf(root, path);
  5214. if (ret != 0)
  5215. return ret;
  5216. } else {
  5217. path->slots[0]--;
  5218. }
  5219. leaf = path->nodes[0];
  5220. nritems = btrfs_header_nritems(leaf);
  5221. if (nritems == 0)
  5222. return 1;
  5223. if (path->slots[0] == nritems)
  5224. path->slots[0]--;
  5225. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5226. if (found_key.objectid < min_objectid)
  5227. break;
  5228. if (found_key.type == type)
  5229. return 0;
  5230. if (found_key.objectid == min_objectid &&
  5231. found_key.type < type)
  5232. break;
  5233. }
  5234. return 1;
  5235. }
  5236. /*
  5237. * search in extent tree to find a previous Metadata/Data extent item with
  5238. * min objecitd.
  5239. *
  5240. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5241. */
  5242. int btrfs_previous_extent_item(struct btrfs_root *root,
  5243. struct btrfs_path *path, u64 min_objectid)
  5244. {
  5245. struct btrfs_key found_key;
  5246. struct extent_buffer *leaf;
  5247. u32 nritems;
  5248. int ret;
  5249. while (1) {
  5250. if (path->slots[0] == 0) {
  5251. btrfs_set_path_blocking(path);
  5252. ret = btrfs_prev_leaf(root, path);
  5253. if (ret != 0)
  5254. return ret;
  5255. } else {
  5256. path->slots[0]--;
  5257. }
  5258. leaf = path->nodes[0];
  5259. nritems = btrfs_header_nritems(leaf);
  5260. if (nritems == 0)
  5261. return 1;
  5262. if (path->slots[0] == nritems)
  5263. path->slots[0]--;
  5264. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5265. if (found_key.objectid < min_objectid)
  5266. break;
  5267. if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
  5268. found_key.type == BTRFS_METADATA_ITEM_KEY)
  5269. return 0;
  5270. if (found_key.objectid == min_objectid &&
  5271. found_key.type < BTRFS_EXTENT_ITEM_KEY)
  5272. break;
  5273. }
  5274. return 1;
  5275. }