ctree.c 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938
  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, root, 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, root, 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, root, 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_test_is_dummy_root(root))
  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. * NULL is returned on error.
  1647. */
  1648. static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
  1649. struct extent_buffer *parent, int slot)
  1650. {
  1651. int level = btrfs_header_level(parent);
  1652. struct extent_buffer *eb;
  1653. if (slot < 0)
  1654. return NULL;
  1655. if (slot >= btrfs_header_nritems(parent))
  1656. return NULL;
  1657. BUG_ON(level == 0);
  1658. eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
  1659. btrfs_node_ptr_generation(parent, slot));
  1660. if (IS_ERR(eb) || !extent_buffer_uptodate(eb)) {
  1661. if (!IS_ERR(eb))
  1662. free_extent_buffer(eb);
  1663. eb = NULL;
  1664. }
  1665. return eb;
  1666. }
  1667. /*
  1668. * node level balancing, used to make sure nodes are in proper order for
  1669. * item deletion. We balance from the top down, so we have to make sure
  1670. * that a deletion won't leave an node completely empty later on.
  1671. */
  1672. static noinline int balance_level(struct btrfs_trans_handle *trans,
  1673. struct btrfs_root *root,
  1674. struct btrfs_path *path, int level)
  1675. {
  1676. struct extent_buffer *right = NULL;
  1677. struct extent_buffer *mid;
  1678. struct extent_buffer *left = NULL;
  1679. struct extent_buffer *parent = NULL;
  1680. int ret = 0;
  1681. int wret;
  1682. int pslot;
  1683. int orig_slot = path->slots[level];
  1684. u64 orig_ptr;
  1685. if (level == 0)
  1686. return 0;
  1687. mid = path->nodes[level];
  1688. WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
  1689. path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
  1690. WARN_ON(btrfs_header_generation(mid) != trans->transid);
  1691. orig_ptr = btrfs_node_blockptr(mid, orig_slot);
  1692. if (level < BTRFS_MAX_LEVEL - 1) {
  1693. parent = path->nodes[level + 1];
  1694. pslot = path->slots[level + 1];
  1695. }
  1696. /*
  1697. * deal with the case where there is only one pointer in the root
  1698. * by promoting the node below to a root
  1699. */
  1700. if (!parent) {
  1701. struct extent_buffer *child;
  1702. if (btrfs_header_nritems(mid) != 1)
  1703. return 0;
  1704. /* promote the child to a root */
  1705. child = read_node_slot(root, mid, 0);
  1706. if (!child) {
  1707. ret = -EROFS;
  1708. btrfs_handle_fs_error(root->fs_info, ret, NULL);
  1709. goto enospc;
  1710. }
  1711. btrfs_tree_lock(child);
  1712. btrfs_set_lock_blocking(child);
  1713. ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
  1714. if (ret) {
  1715. btrfs_tree_unlock(child);
  1716. free_extent_buffer(child);
  1717. goto enospc;
  1718. }
  1719. tree_mod_log_set_root_pointer(root, child, 1);
  1720. rcu_assign_pointer(root->node, child);
  1721. add_root_to_dirty_list(root);
  1722. btrfs_tree_unlock(child);
  1723. path->locks[level] = 0;
  1724. path->nodes[level] = NULL;
  1725. clean_tree_block(trans, root->fs_info, mid);
  1726. btrfs_tree_unlock(mid);
  1727. /* once for the path */
  1728. free_extent_buffer(mid);
  1729. root_sub_used(root, mid->len);
  1730. btrfs_free_tree_block(trans, root, mid, 0, 1);
  1731. /* once for the root ptr */
  1732. free_extent_buffer_stale(mid);
  1733. return 0;
  1734. }
  1735. if (btrfs_header_nritems(mid) >
  1736. BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
  1737. return 0;
  1738. left = read_node_slot(root, parent, pslot - 1);
  1739. if (left) {
  1740. btrfs_tree_lock(left);
  1741. btrfs_set_lock_blocking(left);
  1742. wret = btrfs_cow_block(trans, root, left,
  1743. parent, pslot - 1, &left);
  1744. if (wret) {
  1745. ret = wret;
  1746. goto enospc;
  1747. }
  1748. }
  1749. right = read_node_slot(root, parent, pslot + 1);
  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. /* first, try to make some room in the middle buffer */
  1896. if (left) {
  1897. u32 left_nr;
  1898. btrfs_tree_lock(left);
  1899. btrfs_set_lock_blocking(left);
  1900. left_nr = btrfs_header_nritems(left);
  1901. if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
  1902. wret = 1;
  1903. } else {
  1904. ret = btrfs_cow_block(trans, root, left, parent,
  1905. pslot - 1, &left);
  1906. if (ret)
  1907. wret = 1;
  1908. else {
  1909. wret = push_node_left(trans, root,
  1910. left, mid, 0);
  1911. }
  1912. }
  1913. if (wret < 0)
  1914. ret = wret;
  1915. if (wret == 0) {
  1916. struct btrfs_disk_key disk_key;
  1917. orig_slot += left_nr;
  1918. btrfs_node_key(mid, &disk_key, 0);
  1919. tree_mod_log_set_node_key(root->fs_info, parent,
  1920. pslot, 0);
  1921. btrfs_set_node_key(parent, &disk_key, pslot);
  1922. btrfs_mark_buffer_dirty(parent);
  1923. if (btrfs_header_nritems(left) > orig_slot) {
  1924. path->nodes[level] = left;
  1925. path->slots[level + 1] -= 1;
  1926. path->slots[level] = orig_slot;
  1927. btrfs_tree_unlock(mid);
  1928. free_extent_buffer(mid);
  1929. } else {
  1930. orig_slot -=
  1931. btrfs_header_nritems(left);
  1932. path->slots[level] = orig_slot;
  1933. btrfs_tree_unlock(left);
  1934. free_extent_buffer(left);
  1935. }
  1936. return 0;
  1937. }
  1938. btrfs_tree_unlock(left);
  1939. free_extent_buffer(left);
  1940. }
  1941. right = read_node_slot(root, parent, pslot + 1);
  1942. /*
  1943. * then try to empty the right most buffer into the middle
  1944. */
  1945. if (right) {
  1946. u32 right_nr;
  1947. btrfs_tree_lock(right);
  1948. btrfs_set_lock_blocking(right);
  1949. right_nr = btrfs_header_nritems(right);
  1950. if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
  1951. wret = 1;
  1952. } else {
  1953. ret = btrfs_cow_block(trans, root, right,
  1954. parent, pslot + 1,
  1955. &right);
  1956. if (ret)
  1957. wret = 1;
  1958. else {
  1959. wret = balance_node_right(trans, root,
  1960. right, mid);
  1961. }
  1962. }
  1963. if (wret < 0)
  1964. ret = wret;
  1965. if (wret == 0) {
  1966. struct btrfs_disk_key disk_key;
  1967. btrfs_node_key(right, &disk_key, 0);
  1968. tree_mod_log_set_node_key(root->fs_info, parent,
  1969. pslot + 1, 0);
  1970. btrfs_set_node_key(parent, &disk_key, pslot + 1);
  1971. btrfs_mark_buffer_dirty(parent);
  1972. if (btrfs_header_nritems(mid) <= orig_slot) {
  1973. path->nodes[level] = right;
  1974. path->slots[level + 1] += 1;
  1975. path->slots[level] = orig_slot -
  1976. btrfs_header_nritems(mid);
  1977. btrfs_tree_unlock(mid);
  1978. free_extent_buffer(mid);
  1979. } else {
  1980. btrfs_tree_unlock(right);
  1981. free_extent_buffer(right);
  1982. }
  1983. return 0;
  1984. }
  1985. btrfs_tree_unlock(right);
  1986. free_extent_buffer(right);
  1987. }
  1988. return 1;
  1989. }
  1990. /*
  1991. * readahead one full node of leaves, finding things that are close
  1992. * to the block in 'slot', and triggering ra on them.
  1993. */
  1994. static void reada_for_search(struct btrfs_root *root,
  1995. struct btrfs_path *path,
  1996. int level, int slot, u64 objectid)
  1997. {
  1998. struct extent_buffer *node;
  1999. struct btrfs_disk_key disk_key;
  2000. u32 nritems;
  2001. u64 search;
  2002. u64 target;
  2003. u64 nread = 0;
  2004. u64 gen;
  2005. struct extent_buffer *eb;
  2006. u32 nr;
  2007. u32 blocksize;
  2008. u32 nscan = 0;
  2009. if (level != 1)
  2010. return;
  2011. if (!path->nodes[level])
  2012. return;
  2013. node = path->nodes[level];
  2014. search = btrfs_node_blockptr(node, slot);
  2015. blocksize = root->nodesize;
  2016. eb = btrfs_find_tree_block(root->fs_info, search);
  2017. if (eb) {
  2018. free_extent_buffer(eb);
  2019. return;
  2020. }
  2021. target = search;
  2022. nritems = btrfs_header_nritems(node);
  2023. nr = slot;
  2024. while (1) {
  2025. if (path->reada == READA_BACK) {
  2026. if (nr == 0)
  2027. break;
  2028. nr--;
  2029. } else if (path->reada == READA_FORWARD) {
  2030. nr++;
  2031. if (nr >= nritems)
  2032. break;
  2033. }
  2034. if (path->reada == READA_BACK && objectid) {
  2035. btrfs_node_key(node, &disk_key, nr);
  2036. if (btrfs_disk_key_objectid(&disk_key) != objectid)
  2037. break;
  2038. }
  2039. search = btrfs_node_blockptr(node, nr);
  2040. if ((search <= target && target - search <= 65536) ||
  2041. (search > target && search - target <= 65536)) {
  2042. gen = btrfs_node_ptr_generation(node, nr);
  2043. readahead_tree_block(root, search);
  2044. nread += blocksize;
  2045. }
  2046. nscan++;
  2047. if ((nread > 65536 || nscan > 32))
  2048. break;
  2049. }
  2050. }
  2051. static noinline void reada_for_balance(struct btrfs_root *root,
  2052. struct btrfs_path *path, int level)
  2053. {
  2054. int slot;
  2055. int nritems;
  2056. struct extent_buffer *parent;
  2057. struct extent_buffer *eb;
  2058. u64 gen;
  2059. u64 block1 = 0;
  2060. u64 block2 = 0;
  2061. parent = path->nodes[level + 1];
  2062. if (!parent)
  2063. return;
  2064. nritems = btrfs_header_nritems(parent);
  2065. slot = path->slots[level + 1];
  2066. if (slot > 0) {
  2067. block1 = btrfs_node_blockptr(parent, slot - 1);
  2068. gen = btrfs_node_ptr_generation(parent, slot - 1);
  2069. eb = btrfs_find_tree_block(root->fs_info, block1);
  2070. /*
  2071. * if we get -eagain from btrfs_buffer_uptodate, we
  2072. * don't want to return eagain here. That will loop
  2073. * forever
  2074. */
  2075. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  2076. block1 = 0;
  2077. free_extent_buffer(eb);
  2078. }
  2079. if (slot + 1 < nritems) {
  2080. block2 = btrfs_node_blockptr(parent, slot + 1);
  2081. gen = btrfs_node_ptr_generation(parent, slot + 1);
  2082. eb = btrfs_find_tree_block(root->fs_info, block2);
  2083. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  2084. block2 = 0;
  2085. free_extent_buffer(eb);
  2086. }
  2087. if (block1)
  2088. readahead_tree_block(root, block1);
  2089. if (block2)
  2090. readahead_tree_block(root, block2);
  2091. }
  2092. /*
  2093. * when we walk down the tree, it is usually safe to unlock the higher layers
  2094. * in the tree. The exceptions are when our path goes through slot 0, because
  2095. * operations on the tree might require changing key pointers higher up in the
  2096. * tree.
  2097. *
  2098. * callers might also have set path->keep_locks, which tells this code to keep
  2099. * the lock if the path points to the last slot in the block. This is part of
  2100. * walking through the tree, and selecting the next slot in the higher block.
  2101. *
  2102. * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
  2103. * if lowest_unlock is 1, level 0 won't be unlocked
  2104. */
  2105. static noinline void unlock_up(struct btrfs_path *path, int level,
  2106. int lowest_unlock, int min_write_lock_level,
  2107. int *write_lock_level)
  2108. {
  2109. int i;
  2110. int skip_level = level;
  2111. int no_skips = 0;
  2112. struct extent_buffer *t;
  2113. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2114. if (!path->nodes[i])
  2115. break;
  2116. if (!path->locks[i])
  2117. break;
  2118. if (!no_skips && path->slots[i] == 0) {
  2119. skip_level = i + 1;
  2120. continue;
  2121. }
  2122. if (!no_skips && path->keep_locks) {
  2123. u32 nritems;
  2124. t = path->nodes[i];
  2125. nritems = btrfs_header_nritems(t);
  2126. if (nritems < 1 || path->slots[i] >= nritems - 1) {
  2127. skip_level = i + 1;
  2128. continue;
  2129. }
  2130. }
  2131. if (skip_level < i && i >= lowest_unlock)
  2132. no_skips = 1;
  2133. t = path->nodes[i];
  2134. if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
  2135. btrfs_tree_unlock_rw(t, path->locks[i]);
  2136. path->locks[i] = 0;
  2137. if (write_lock_level &&
  2138. i > min_write_lock_level &&
  2139. i <= *write_lock_level) {
  2140. *write_lock_level = i - 1;
  2141. }
  2142. }
  2143. }
  2144. }
  2145. /*
  2146. * This releases any locks held in the path starting at level and
  2147. * going all the way up to the root.
  2148. *
  2149. * btrfs_search_slot will keep the lock held on higher nodes in a few
  2150. * corner cases, such as COW of the block at slot zero in the node. This
  2151. * ignores those rules, and it should only be called when there are no
  2152. * more updates to be done higher up in the tree.
  2153. */
  2154. noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
  2155. {
  2156. int i;
  2157. if (path->keep_locks)
  2158. return;
  2159. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2160. if (!path->nodes[i])
  2161. continue;
  2162. if (!path->locks[i])
  2163. continue;
  2164. btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
  2165. path->locks[i] = 0;
  2166. }
  2167. }
  2168. /*
  2169. * helper function for btrfs_search_slot. The goal is to find a block
  2170. * in cache without setting the path to blocking. If we find the block
  2171. * we return zero and the path is unchanged.
  2172. *
  2173. * If we can't find the block, we set the path blocking and do some
  2174. * reada. -EAGAIN is returned and the search must be repeated.
  2175. */
  2176. static int
  2177. read_block_for_search(struct btrfs_trans_handle *trans,
  2178. struct btrfs_root *root, struct btrfs_path *p,
  2179. struct extent_buffer **eb_ret, int level, int slot,
  2180. struct btrfs_key *key, u64 time_seq)
  2181. {
  2182. u64 blocknr;
  2183. u64 gen;
  2184. struct extent_buffer *b = *eb_ret;
  2185. struct extent_buffer *tmp;
  2186. int ret;
  2187. blocknr = btrfs_node_blockptr(b, slot);
  2188. gen = btrfs_node_ptr_generation(b, slot);
  2189. tmp = btrfs_find_tree_block(root->fs_info, blocknr);
  2190. if (tmp) {
  2191. /* first we do an atomic uptodate check */
  2192. if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
  2193. *eb_ret = tmp;
  2194. return 0;
  2195. }
  2196. /* the pages were up to date, but we failed
  2197. * the generation number check. Do a full
  2198. * read for the generation number that is correct.
  2199. * We must do this without dropping locks so
  2200. * we can trust our generation number
  2201. */
  2202. btrfs_set_path_blocking(p);
  2203. /* now we're allowed to do a blocking uptodate check */
  2204. ret = btrfs_read_buffer(tmp, gen);
  2205. if (!ret) {
  2206. *eb_ret = tmp;
  2207. return 0;
  2208. }
  2209. free_extent_buffer(tmp);
  2210. btrfs_release_path(p);
  2211. return -EIO;
  2212. }
  2213. /*
  2214. * reduce lock contention at high levels
  2215. * of the btree by dropping locks before
  2216. * we read. Don't release the lock on the current
  2217. * level because we need to walk this node to figure
  2218. * out which blocks to read.
  2219. */
  2220. btrfs_unlock_up_safe(p, level + 1);
  2221. btrfs_set_path_blocking(p);
  2222. free_extent_buffer(tmp);
  2223. if (p->reada != READA_NONE)
  2224. reada_for_search(root, p, level, slot, key->objectid);
  2225. btrfs_release_path(p);
  2226. ret = -EAGAIN;
  2227. tmp = read_tree_block(root, blocknr, 0);
  2228. if (!IS_ERR(tmp)) {
  2229. /*
  2230. * If the read above didn't mark this buffer up to date,
  2231. * it will never end up being up to date. Set ret to EIO now
  2232. * and give up so that our caller doesn't loop forever
  2233. * on our EAGAINs.
  2234. */
  2235. if (!btrfs_buffer_uptodate(tmp, 0, 0))
  2236. ret = -EIO;
  2237. free_extent_buffer(tmp);
  2238. } else {
  2239. ret = PTR_ERR(tmp);
  2240. }
  2241. return ret;
  2242. }
  2243. /*
  2244. * helper function for btrfs_search_slot. This does all of the checks
  2245. * for node-level blocks and does any balancing required based on
  2246. * the ins_len.
  2247. *
  2248. * If no extra work was required, zero is returned. If we had to
  2249. * drop the path, -EAGAIN is returned and btrfs_search_slot must
  2250. * start over
  2251. */
  2252. static int
  2253. setup_nodes_for_search(struct btrfs_trans_handle *trans,
  2254. struct btrfs_root *root, struct btrfs_path *p,
  2255. struct extent_buffer *b, int level, int ins_len,
  2256. int *write_lock_level)
  2257. {
  2258. int ret;
  2259. if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
  2260. BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
  2261. int sret;
  2262. if (*write_lock_level < level + 1) {
  2263. *write_lock_level = level + 1;
  2264. btrfs_release_path(p);
  2265. goto again;
  2266. }
  2267. btrfs_set_path_blocking(p);
  2268. reada_for_balance(root, p, level);
  2269. sret = split_node(trans, root, p, level);
  2270. btrfs_clear_path_blocking(p, NULL, 0);
  2271. BUG_ON(sret > 0);
  2272. if (sret) {
  2273. ret = sret;
  2274. goto done;
  2275. }
  2276. b = p->nodes[level];
  2277. } else if (ins_len < 0 && btrfs_header_nritems(b) <
  2278. BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
  2279. int sret;
  2280. if (*write_lock_level < level + 1) {
  2281. *write_lock_level = level + 1;
  2282. btrfs_release_path(p);
  2283. goto again;
  2284. }
  2285. btrfs_set_path_blocking(p);
  2286. reada_for_balance(root, p, level);
  2287. sret = balance_level(trans, root, p, level);
  2288. btrfs_clear_path_blocking(p, NULL, 0);
  2289. if (sret) {
  2290. ret = sret;
  2291. goto done;
  2292. }
  2293. b = p->nodes[level];
  2294. if (!b) {
  2295. btrfs_release_path(p);
  2296. goto again;
  2297. }
  2298. BUG_ON(btrfs_header_nritems(b) == 1);
  2299. }
  2300. return 0;
  2301. again:
  2302. ret = -EAGAIN;
  2303. done:
  2304. return ret;
  2305. }
  2306. static void key_search_validate(struct extent_buffer *b,
  2307. struct btrfs_key *key,
  2308. int level)
  2309. {
  2310. #ifdef CONFIG_BTRFS_ASSERT
  2311. struct btrfs_disk_key disk_key;
  2312. btrfs_cpu_key_to_disk(&disk_key, key);
  2313. if (level == 0)
  2314. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2315. offsetof(struct btrfs_leaf, items[0].key),
  2316. sizeof(disk_key)));
  2317. else
  2318. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2319. offsetof(struct btrfs_node, ptrs[0].key),
  2320. sizeof(disk_key)));
  2321. #endif
  2322. }
  2323. static int key_search(struct extent_buffer *b, struct btrfs_key *key,
  2324. int level, int *prev_cmp, int *slot)
  2325. {
  2326. if (*prev_cmp != 0) {
  2327. *prev_cmp = bin_search(b, key, level, slot);
  2328. return *prev_cmp;
  2329. }
  2330. key_search_validate(b, key, level);
  2331. *slot = 0;
  2332. return 0;
  2333. }
  2334. int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
  2335. u64 iobjectid, u64 ioff, u8 key_type,
  2336. struct btrfs_key *found_key)
  2337. {
  2338. int ret;
  2339. struct btrfs_key key;
  2340. struct extent_buffer *eb;
  2341. ASSERT(path);
  2342. ASSERT(found_key);
  2343. key.type = key_type;
  2344. key.objectid = iobjectid;
  2345. key.offset = ioff;
  2346. ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  2347. if (ret < 0)
  2348. return ret;
  2349. eb = path->nodes[0];
  2350. if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
  2351. ret = btrfs_next_leaf(fs_root, path);
  2352. if (ret)
  2353. return ret;
  2354. eb = path->nodes[0];
  2355. }
  2356. btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
  2357. if (found_key->type != key.type ||
  2358. found_key->objectid != key.objectid)
  2359. return 1;
  2360. return 0;
  2361. }
  2362. /*
  2363. * look for key in the tree. path is filled in with nodes along the way
  2364. * if key is found, we return zero and you can find the item in the leaf
  2365. * level of the path (level 0)
  2366. *
  2367. * If the key isn't found, the path points to the slot where it should
  2368. * be inserted, and 1 is returned. If there are other errors during the
  2369. * search a negative error number is returned.
  2370. *
  2371. * if ins_len > 0, nodes and leaves will be split as we walk down the
  2372. * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
  2373. * possible)
  2374. */
  2375. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  2376. *root, struct btrfs_key *key, struct btrfs_path *p, int
  2377. ins_len, int cow)
  2378. {
  2379. struct extent_buffer *b;
  2380. int slot;
  2381. int ret;
  2382. int err;
  2383. int level;
  2384. int lowest_unlock = 1;
  2385. int root_lock;
  2386. /* everything at write_lock_level or lower must be write locked */
  2387. int write_lock_level = 0;
  2388. u8 lowest_level = 0;
  2389. int min_write_lock_level;
  2390. int prev_cmp;
  2391. lowest_level = p->lowest_level;
  2392. WARN_ON(lowest_level && ins_len > 0);
  2393. WARN_ON(p->nodes[0] != NULL);
  2394. BUG_ON(!cow && ins_len);
  2395. if (ins_len < 0) {
  2396. lowest_unlock = 2;
  2397. /* when we are removing items, we might have to go up to level
  2398. * two as we update tree pointers Make sure we keep write
  2399. * for those levels as well
  2400. */
  2401. write_lock_level = 2;
  2402. } else if (ins_len > 0) {
  2403. /*
  2404. * for inserting items, make sure we have a write lock on
  2405. * level 1 so we can update keys
  2406. */
  2407. write_lock_level = 1;
  2408. }
  2409. if (!cow)
  2410. write_lock_level = -1;
  2411. if (cow && (p->keep_locks || p->lowest_level))
  2412. write_lock_level = BTRFS_MAX_LEVEL;
  2413. min_write_lock_level = write_lock_level;
  2414. again:
  2415. prev_cmp = -1;
  2416. /*
  2417. * we try very hard to do read locks on the root
  2418. */
  2419. root_lock = BTRFS_READ_LOCK;
  2420. level = 0;
  2421. if (p->search_commit_root) {
  2422. /*
  2423. * the commit roots are read only
  2424. * so we always do read locks
  2425. */
  2426. if (p->need_commit_sem)
  2427. down_read(&root->fs_info->commit_root_sem);
  2428. b = root->commit_root;
  2429. extent_buffer_get(b);
  2430. level = btrfs_header_level(b);
  2431. if (p->need_commit_sem)
  2432. up_read(&root->fs_info->commit_root_sem);
  2433. if (!p->skip_locking)
  2434. btrfs_tree_read_lock(b);
  2435. } else {
  2436. if (p->skip_locking) {
  2437. b = btrfs_root_node(root);
  2438. level = btrfs_header_level(b);
  2439. } else {
  2440. /* we don't know the level of the root node
  2441. * until we actually have it read locked
  2442. */
  2443. b = btrfs_read_lock_root_node(root);
  2444. level = btrfs_header_level(b);
  2445. if (level <= write_lock_level) {
  2446. /* whoops, must trade for write lock */
  2447. btrfs_tree_read_unlock(b);
  2448. free_extent_buffer(b);
  2449. b = btrfs_lock_root_node(root);
  2450. root_lock = BTRFS_WRITE_LOCK;
  2451. /* the level might have changed, check again */
  2452. level = btrfs_header_level(b);
  2453. }
  2454. }
  2455. }
  2456. p->nodes[level] = b;
  2457. if (!p->skip_locking)
  2458. p->locks[level] = root_lock;
  2459. while (b) {
  2460. level = btrfs_header_level(b);
  2461. /*
  2462. * setup the path here so we can release it under lock
  2463. * contention with the cow code
  2464. */
  2465. if (cow) {
  2466. /*
  2467. * if we don't really need to cow this block
  2468. * then we don't want to set the path blocking,
  2469. * so we test it here
  2470. */
  2471. if (!should_cow_block(trans, root, b)) {
  2472. trans->dirty = true;
  2473. goto cow_done;
  2474. }
  2475. /*
  2476. * must have write locks on this node and the
  2477. * parent
  2478. */
  2479. if (level > write_lock_level ||
  2480. (level + 1 > write_lock_level &&
  2481. level + 1 < BTRFS_MAX_LEVEL &&
  2482. p->nodes[level + 1])) {
  2483. write_lock_level = level + 1;
  2484. btrfs_release_path(p);
  2485. goto again;
  2486. }
  2487. btrfs_set_path_blocking(p);
  2488. err = btrfs_cow_block(trans, root, b,
  2489. p->nodes[level + 1],
  2490. p->slots[level + 1], &b);
  2491. if (err) {
  2492. ret = err;
  2493. goto done;
  2494. }
  2495. }
  2496. cow_done:
  2497. p->nodes[level] = b;
  2498. btrfs_clear_path_blocking(p, NULL, 0);
  2499. /*
  2500. * we have a lock on b and as long as we aren't changing
  2501. * the tree, there is no way to for the items in b to change.
  2502. * It is safe to drop the lock on our parent before we
  2503. * go through the expensive btree search on b.
  2504. *
  2505. * If we're inserting or deleting (ins_len != 0), then we might
  2506. * be changing slot zero, which may require changing the parent.
  2507. * So, we can't drop the lock until after we know which slot
  2508. * we're operating on.
  2509. */
  2510. if (!ins_len && !p->keep_locks) {
  2511. int u = level + 1;
  2512. if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
  2513. btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
  2514. p->locks[u] = 0;
  2515. }
  2516. }
  2517. ret = key_search(b, key, level, &prev_cmp, &slot);
  2518. if (ret < 0)
  2519. goto done;
  2520. if (level != 0) {
  2521. int dec = 0;
  2522. if (ret && slot > 0) {
  2523. dec = 1;
  2524. slot -= 1;
  2525. }
  2526. p->slots[level] = slot;
  2527. err = setup_nodes_for_search(trans, root, p, b, level,
  2528. ins_len, &write_lock_level);
  2529. if (err == -EAGAIN)
  2530. goto again;
  2531. if (err) {
  2532. ret = err;
  2533. goto done;
  2534. }
  2535. b = p->nodes[level];
  2536. slot = p->slots[level];
  2537. /*
  2538. * slot 0 is special, if we change the key
  2539. * we have to update the parent pointer
  2540. * which means we must have a write lock
  2541. * on the parent
  2542. */
  2543. if (slot == 0 && ins_len &&
  2544. write_lock_level < level + 1) {
  2545. write_lock_level = level + 1;
  2546. btrfs_release_path(p);
  2547. goto again;
  2548. }
  2549. unlock_up(p, level, lowest_unlock,
  2550. min_write_lock_level, &write_lock_level);
  2551. if (level == lowest_level) {
  2552. if (dec)
  2553. p->slots[level]++;
  2554. goto done;
  2555. }
  2556. err = read_block_for_search(trans, root, p,
  2557. &b, level, slot, key, 0);
  2558. if (err == -EAGAIN)
  2559. goto again;
  2560. if (err) {
  2561. ret = err;
  2562. goto done;
  2563. }
  2564. if (!p->skip_locking) {
  2565. level = btrfs_header_level(b);
  2566. if (level <= write_lock_level) {
  2567. err = btrfs_try_tree_write_lock(b);
  2568. if (!err) {
  2569. btrfs_set_path_blocking(p);
  2570. btrfs_tree_lock(b);
  2571. btrfs_clear_path_blocking(p, b,
  2572. BTRFS_WRITE_LOCK);
  2573. }
  2574. p->locks[level] = BTRFS_WRITE_LOCK;
  2575. } else {
  2576. err = btrfs_tree_read_lock_atomic(b);
  2577. if (!err) {
  2578. btrfs_set_path_blocking(p);
  2579. btrfs_tree_read_lock(b);
  2580. btrfs_clear_path_blocking(p, b,
  2581. BTRFS_READ_LOCK);
  2582. }
  2583. p->locks[level] = BTRFS_READ_LOCK;
  2584. }
  2585. p->nodes[level] = b;
  2586. }
  2587. } else {
  2588. p->slots[level] = slot;
  2589. if (ins_len > 0 &&
  2590. btrfs_leaf_free_space(root, b) < ins_len) {
  2591. if (write_lock_level < 1) {
  2592. write_lock_level = 1;
  2593. btrfs_release_path(p);
  2594. goto again;
  2595. }
  2596. btrfs_set_path_blocking(p);
  2597. err = split_leaf(trans, root, key,
  2598. p, ins_len, ret == 0);
  2599. btrfs_clear_path_blocking(p, NULL, 0);
  2600. BUG_ON(err > 0);
  2601. if (err) {
  2602. ret = err;
  2603. goto done;
  2604. }
  2605. }
  2606. if (!p->search_for_split)
  2607. unlock_up(p, level, lowest_unlock,
  2608. min_write_lock_level, &write_lock_level);
  2609. goto done;
  2610. }
  2611. }
  2612. ret = 1;
  2613. done:
  2614. /*
  2615. * we don't really know what they plan on doing with the path
  2616. * from here on, so for now just mark it as blocking
  2617. */
  2618. if (!p->leave_spinning)
  2619. btrfs_set_path_blocking(p);
  2620. if (ret < 0 && !p->skip_release_on_error)
  2621. btrfs_release_path(p);
  2622. return ret;
  2623. }
  2624. /*
  2625. * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
  2626. * current state of the tree together with the operations recorded in the tree
  2627. * modification log to search for the key in a previous version of this tree, as
  2628. * denoted by the time_seq parameter.
  2629. *
  2630. * Naturally, there is no support for insert, delete or cow operations.
  2631. *
  2632. * The resulting path and return value will be set up as if we called
  2633. * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
  2634. */
  2635. int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
  2636. struct btrfs_path *p, u64 time_seq)
  2637. {
  2638. struct extent_buffer *b;
  2639. int slot;
  2640. int ret;
  2641. int err;
  2642. int level;
  2643. int lowest_unlock = 1;
  2644. u8 lowest_level = 0;
  2645. int prev_cmp = -1;
  2646. lowest_level = p->lowest_level;
  2647. WARN_ON(p->nodes[0] != NULL);
  2648. if (p->search_commit_root) {
  2649. BUG_ON(time_seq);
  2650. return btrfs_search_slot(NULL, root, key, p, 0, 0);
  2651. }
  2652. again:
  2653. b = get_old_root(root, time_seq);
  2654. level = btrfs_header_level(b);
  2655. p->locks[level] = BTRFS_READ_LOCK;
  2656. while (b) {
  2657. level = btrfs_header_level(b);
  2658. p->nodes[level] = b;
  2659. btrfs_clear_path_blocking(p, NULL, 0);
  2660. /*
  2661. * we have a lock on b and as long as we aren't changing
  2662. * the tree, there is no way to for the items in b to change.
  2663. * It is safe to drop the lock on our parent before we
  2664. * go through the expensive btree search on b.
  2665. */
  2666. btrfs_unlock_up_safe(p, level + 1);
  2667. /*
  2668. * Since we can unwind ebs we want to do a real search every
  2669. * time.
  2670. */
  2671. prev_cmp = -1;
  2672. ret = key_search(b, key, level, &prev_cmp, &slot);
  2673. if (level != 0) {
  2674. int dec = 0;
  2675. if (ret && slot > 0) {
  2676. dec = 1;
  2677. slot -= 1;
  2678. }
  2679. p->slots[level] = slot;
  2680. unlock_up(p, level, lowest_unlock, 0, NULL);
  2681. if (level == lowest_level) {
  2682. if (dec)
  2683. p->slots[level]++;
  2684. goto done;
  2685. }
  2686. err = read_block_for_search(NULL, root, p, &b, level,
  2687. slot, key, time_seq);
  2688. if (err == -EAGAIN)
  2689. goto again;
  2690. if (err) {
  2691. ret = err;
  2692. goto done;
  2693. }
  2694. level = btrfs_header_level(b);
  2695. err = btrfs_tree_read_lock_atomic(b);
  2696. if (!err) {
  2697. btrfs_set_path_blocking(p);
  2698. btrfs_tree_read_lock(b);
  2699. btrfs_clear_path_blocking(p, b,
  2700. BTRFS_READ_LOCK);
  2701. }
  2702. b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
  2703. if (!b) {
  2704. ret = -ENOMEM;
  2705. goto done;
  2706. }
  2707. p->locks[level] = BTRFS_READ_LOCK;
  2708. p->nodes[level] = b;
  2709. } else {
  2710. p->slots[level] = slot;
  2711. unlock_up(p, level, lowest_unlock, 0, NULL);
  2712. goto done;
  2713. }
  2714. }
  2715. ret = 1;
  2716. done:
  2717. if (!p->leave_spinning)
  2718. btrfs_set_path_blocking(p);
  2719. if (ret < 0)
  2720. btrfs_release_path(p);
  2721. return ret;
  2722. }
  2723. /*
  2724. * helper to use instead of search slot if no exact match is needed but
  2725. * instead the next or previous item should be returned.
  2726. * When find_higher is true, the next higher item is returned, the next lower
  2727. * otherwise.
  2728. * When return_any and find_higher are both true, and no higher item is found,
  2729. * return the next lower instead.
  2730. * When return_any is true and find_higher is false, and no lower item is found,
  2731. * return the next higher instead.
  2732. * It returns 0 if any item is found, 1 if none is found (tree empty), and
  2733. * < 0 on error
  2734. */
  2735. int btrfs_search_slot_for_read(struct btrfs_root *root,
  2736. struct btrfs_key *key, struct btrfs_path *p,
  2737. int find_higher, int return_any)
  2738. {
  2739. int ret;
  2740. struct extent_buffer *leaf;
  2741. again:
  2742. ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
  2743. if (ret <= 0)
  2744. return ret;
  2745. /*
  2746. * a return value of 1 means the path is at the position where the
  2747. * item should be inserted. Normally this is the next bigger item,
  2748. * but in case the previous item is the last in a leaf, path points
  2749. * to the first free slot in the previous leaf, i.e. at an invalid
  2750. * item.
  2751. */
  2752. leaf = p->nodes[0];
  2753. if (find_higher) {
  2754. if (p->slots[0] >= btrfs_header_nritems(leaf)) {
  2755. ret = btrfs_next_leaf(root, p);
  2756. if (ret <= 0)
  2757. return ret;
  2758. if (!return_any)
  2759. return 1;
  2760. /*
  2761. * no higher item found, return the next
  2762. * lower instead
  2763. */
  2764. return_any = 0;
  2765. find_higher = 0;
  2766. btrfs_release_path(p);
  2767. goto again;
  2768. }
  2769. } else {
  2770. if (p->slots[0] == 0) {
  2771. ret = btrfs_prev_leaf(root, p);
  2772. if (ret < 0)
  2773. return ret;
  2774. if (!ret) {
  2775. leaf = p->nodes[0];
  2776. if (p->slots[0] == btrfs_header_nritems(leaf))
  2777. p->slots[0]--;
  2778. return 0;
  2779. }
  2780. if (!return_any)
  2781. return 1;
  2782. /*
  2783. * no lower item found, return the next
  2784. * higher instead
  2785. */
  2786. return_any = 0;
  2787. find_higher = 1;
  2788. btrfs_release_path(p);
  2789. goto again;
  2790. } else {
  2791. --p->slots[0];
  2792. }
  2793. }
  2794. return 0;
  2795. }
  2796. /*
  2797. * adjust the pointers going up the tree, starting at level
  2798. * making sure the right key of each node is points to 'key'.
  2799. * This is used after shifting pointers to the left, so it stops
  2800. * fixing up pointers when a given leaf/node is not in slot 0 of the
  2801. * higher levels
  2802. *
  2803. */
  2804. static void fixup_low_keys(struct btrfs_fs_info *fs_info,
  2805. struct btrfs_path *path,
  2806. struct btrfs_disk_key *key, int level)
  2807. {
  2808. int i;
  2809. struct extent_buffer *t;
  2810. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2811. int tslot = path->slots[i];
  2812. if (!path->nodes[i])
  2813. break;
  2814. t = path->nodes[i];
  2815. tree_mod_log_set_node_key(fs_info, t, tslot, 1);
  2816. btrfs_set_node_key(t, key, tslot);
  2817. btrfs_mark_buffer_dirty(path->nodes[i]);
  2818. if (tslot != 0)
  2819. break;
  2820. }
  2821. }
  2822. /*
  2823. * update item key.
  2824. *
  2825. * This function isn't completely safe. It's the caller's responsibility
  2826. * that the new key won't break the order
  2827. */
  2828. void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
  2829. struct btrfs_path *path,
  2830. struct btrfs_key *new_key)
  2831. {
  2832. struct btrfs_disk_key disk_key;
  2833. struct extent_buffer *eb;
  2834. int slot;
  2835. eb = path->nodes[0];
  2836. slot = path->slots[0];
  2837. if (slot > 0) {
  2838. btrfs_item_key(eb, &disk_key, slot - 1);
  2839. BUG_ON(comp_keys(&disk_key, new_key) >= 0);
  2840. }
  2841. if (slot < btrfs_header_nritems(eb) - 1) {
  2842. btrfs_item_key(eb, &disk_key, slot + 1);
  2843. BUG_ON(comp_keys(&disk_key, new_key) <= 0);
  2844. }
  2845. btrfs_cpu_key_to_disk(&disk_key, new_key);
  2846. btrfs_set_item_key(eb, &disk_key, slot);
  2847. btrfs_mark_buffer_dirty(eb);
  2848. if (slot == 0)
  2849. fixup_low_keys(fs_info, path, &disk_key, 1);
  2850. }
  2851. /*
  2852. * try to push data from one node into the next node left in the
  2853. * tree.
  2854. *
  2855. * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
  2856. * error, and > 0 if there was no room in the left hand block.
  2857. */
  2858. static int push_node_left(struct btrfs_trans_handle *trans,
  2859. struct btrfs_root *root, struct extent_buffer *dst,
  2860. struct extent_buffer *src, int empty)
  2861. {
  2862. int push_items = 0;
  2863. int src_nritems;
  2864. int dst_nritems;
  2865. int ret = 0;
  2866. src_nritems = btrfs_header_nritems(src);
  2867. dst_nritems = btrfs_header_nritems(dst);
  2868. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  2869. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2870. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2871. if (!empty && src_nritems <= 8)
  2872. return 1;
  2873. if (push_items <= 0)
  2874. return 1;
  2875. if (empty) {
  2876. push_items = min(src_nritems, push_items);
  2877. if (push_items < src_nritems) {
  2878. /* leave at least 8 pointers in the node if
  2879. * we aren't going to empty it
  2880. */
  2881. if (src_nritems - push_items < 8) {
  2882. if (push_items <= 8)
  2883. return 1;
  2884. push_items -= 8;
  2885. }
  2886. }
  2887. } else
  2888. push_items = min(src_nritems - 8, push_items);
  2889. ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
  2890. push_items);
  2891. if (ret) {
  2892. btrfs_abort_transaction(trans, root, ret);
  2893. return ret;
  2894. }
  2895. copy_extent_buffer(dst, src,
  2896. btrfs_node_key_ptr_offset(dst_nritems),
  2897. btrfs_node_key_ptr_offset(0),
  2898. push_items * sizeof(struct btrfs_key_ptr));
  2899. if (push_items < src_nritems) {
  2900. /*
  2901. * don't call tree_mod_log_eb_move here, key removal was already
  2902. * fully logged by tree_mod_log_eb_copy above.
  2903. */
  2904. memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
  2905. btrfs_node_key_ptr_offset(push_items),
  2906. (src_nritems - push_items) *
  2907. sizeof(struct btrfs_key_ptr));
  2908. }
  2909. btrfs_set_header_nritems(src, src_nritems - push_items);
  2910. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2911. btrfs_mark_buffer_dirty(src);
  2912. btrfs_mark_buffer_dirty(dst);
  2913. return ret;
  2914. }
  2915. /*
  2916. * try to push data from one node into the next node right in the
  2917. * tree.
  2918. *
  2919. * returns 0 if some ptrs were pushed, < 0 if there was some horrible
  2920. * error, and > 0 if there was no room in the right hand block.
  2921. *
  2922. * this will only push up to 1/2 the contents of the left node over
  2923. */
  2924. static int balance_node_right(struct btrfs_trans_handle *trans,
  2925. struct btrfs_root *root,
  2926. struct extent_buffer *dst,
  2927. struct extent_buffer *src)
  2928. {
  2929. int push_items = 0;
  2930. int max_push;
  2931. int src_nritems;
  2932. int dst_nritems;
  2933. int ret = 0;
  2934. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2935. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2936. src_nritems = btrfs_header_nritems(src);
  2937. dst_nritems = btrfs_header_nritems(dst);
  2938. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  2939. if (push_items <= 0)
  2940. return 1;
  2941. if (src_nritems < 4)
  2942. return 1;
  2943. max_push = src_nritems / 2 + 1;
  2944. /* don't try to empty the node */
  2945. if (max_push >= src_nritems)
  2946. return 1;
  2947. if (max_push < push_items)
  2948. push_items = max_push;
  2949. tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
  2950. memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
  2951. btrfs_node_key_ptr_offset(0),
  2952. (dst_nritems) *
  2953. sizeof(struct btrfs_key_ptr));
  2954. ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
  2955. src_nritems - push_items, push_items);
  2956. if (ret) {
  2957. btrfs_abort_transaction(trans, root, ret);
  2958. return ret;
  2959. }
  2960. copy_extent_buffer(dst, src,
  2961. btrfs_node_key_ptr_offset(0),
  2962. btrfs_node_key_ptr_offset(src_nritems - push_items),
  2963. push_items * sizeof(struct btrfs_key_ptr));
  2964. btrfs_set_header_nritems(src, src_nritems - push_items);
  2965. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2966. btrfs_mark_buffer_dirty(src);
  2967. btrfs_mark_buffer_dirty(dst);
  2968. return ret;
  2969. }
  2970. /*
  2971. * helper function to insert a new root level in the tree.
  2972. * A new node is allocated, and a single item is inserted to
  2973. * point to the existing root
  2974. *
  2975. * returns zero on success or < 0 on failure.
  2976. */
  2977. static noinline int insert_new_root(struct btrfs_trans_handle *trans,
  2978. struct btrfs_root *root,
  2979. struct btrfs_path *path, int level)
  2980. {
  2981. u64 lower_gen;
  2982. struct extent_buffer *lower;
  2983. struct extent_buffer *c;
  2984. struct extent_buffer *old;
  2985. struct btrfs_disk_key lower_key;
  2986. BUG_ON(path->nodes[level]);
  2987. BUG_ON(path->nodes[level-1] != root->node);
  2988. lower = path->nodes[level-1];
  2989. if (level == 1)
  2990. btrfs_item_key(lower, &lower_key, 0);
  2991. else
  2992. btrfs_node_key(lower, &lower_key, 0);
  2993. c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  2994. &lower_key, level, root->node->start, 0);
  2995. if (IS_ERR(c))
  2996. return PTR_ERR(c);
  2997. root_add_used(root, root->nodesize);
  2998. memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
  2999. btrfs_set_header_nritems(c, 1);
  3000. btrfs_set_header_level(c, level);
  3001. btrfs_set_header_bytenr(c, c->start);
  3002. btrfs_set_header_generation(c, trans->transid);
  3003. btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
  3004. btrfs_set_header_owner(c, root->root_key.objectid);
  3005. write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
  3006. BTRFS_FSID_SIZE);
  3007. write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
  3008. btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
  3009. btrfs_set_node_key(c, &lower_key, 0);
  3010. btrfs_set_node_blockptr(c, 0, lower->start);
  3011. lower_gen = btrfs_header_generation(lower);
  3012. WARN_ON(lower_gen != trans->transid);
  3013. btrfs_set_node_ptr_generation(c, 0, lower_gen);
  3014. btrfs_mark_buffer_dirty(c);
  3015. old = root->node;
  3016. tree_mod_log_set_root_pointer(root, c, 0);
  3017. rcu_assign_pointer(root->node, c);
  3018. /* the super has an extra ref to root->node */
  3019. free_extent_buffer(old);
  3020. add_root_to_dirty_list(root);
  3021. extent_buffer_get(c);
  3022. path->nodes[level] = c;
  3023. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  3024. path->slots[level] = 0;
  3025. return 0;
  3026. }
  3027. /*
  3028. * worker function to insert a single pointer in a node.
  3029. * the node should have enough room for the pointer already
  3030. *
  3031. * slot and level indicate where you want the key to go, and
  3032. * blocknr is the block the key points to.
  3033. */
  3034. static void insert_ptr(struct btrfs_trans_handle *trans,
  3035. struct btrfs_root *root, struct btrfs_path *path,
  3036. struct btrfs_disk_key *key, u64 bytenr,
  3037. int slot, int level)
  3038. {
  3039. struct extent_buffer *lower;
  3040. int nritems;
  3041. int ret;
  3042. BUG_ON(!path->nodes[level]);
  3043. btrfs_assert_tree_locked(path->nodes[level]);
  3044. lower = path->nodes[level];
  3045. nritems = btrfs_header_nritems(lower);
  3046. BUG_ON(slot > nritems);
  3047. BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
  3048. if (slot != nritems) {
  3049. if (level)
  3050. tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
  3051. slot, nritems - slot);
  3052. memmove_extent_buffer(lower,
  3053. btrfs_node_key_ptr_offset(slot + 1),
  3054. btrfs_node_key_ptr_offset(slot),
  3055. (nritems - slot) * sizeof(struct btrfs_key_ptr));
  3056. }
  3057. if (level) {
  3058. ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
  3059. MOD_LOG_KEY_ADD, GFP_NOFS);
  3060. BUG_ON(ret < 0);
  3061. }
  3062. btrfs_set_node_key(lower, key, slot);
  3063. btrfs_set_node_blockptr(lower, slot, bytenr);
  3064. WARN_ON(trans->transid == 0);
  3065. btrfs_set_node_ptr_generation(lower, slot, trans->transid);
  3066. btrfs_set_header_nritems(lower, nritems + 1);
  3067. btrfs_mark_buffer_dirty(lower);
  3068. }
  3069. /*
  3070. * split the node at the specified level in path in two.
  3071. * The path is corrected to point to the appropriate node after the split
  3072. *
  3073. * Before splitting this tries to make some room in the node by pushing
  3074. * left and right, if either one works, it returns right away.
  3075. *
  3076. * returns 0 on success and < 0 on failure
  3077. */
  3078. static noinline int split_node(struct btrfs_trans_handle *trans,
  3079. struct btrfs_root *root,
  3080. struct btrfs_path *path, int level)
  3081. {
  3082. struct extent_buffer *c;
  3083. struct extent_buffer *split;
  3084. struct btrfs_disk_key disk_key;
  3085. int mid;
  3086. int ret;
  3087. u32 c_nritems;
  3088. c = path->nodes[level];
  3089. WARN_ON(btrfs_header_generation(c) != trans->transid);
  3090. if (c == root->node) {
  3091. /*
  3092. * trying to split the root, lets make a new one
  3093. *
  3094. * tree mod log: We don't log_removal old root in
  3095. * insert_new_root, because that root buffer will be kept as a
  3096. * normal node. We are going to log removal of half of the
  3097. * elements below with tree_mod_log_eb_copy. We're holding a
  3098. * tree lock on the buffer, which is why we cannot race with
  3099. * other tree_mod_log users.
  3100. */
  3101. ret = insert_new_root(trans, root, path, level + 1);
  3102. if (ret)
  3103. return ret;
  3104. } else {
  3105. ret = push_nodes_for_insert(trans, root, path, level);
  3106. c = path->nodes[level];
  3107. if (!ret && btrfs_header_nritems(c) <
  3108. BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
  3109. return 0;
  3110. if (ret < 0)
  3111. return ret;
  3112. }
  3113. c_nritems = btrfs_header_nritems(c);
  3114. mid = (c_nritems + 1) / 2;
  3115. btrfs_node_key(c, &disk_key, mid);
  3116. split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3117. &disk_key, level, c->start, 0);
  3118. if (IS_ERR(split))
  3119. return PTR_ERR(split);
  3120. root_add_used(root, root->nodesize);
  3121. memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
  3122. btrfs_set_header_level(split, btrfs_header_level(c));
  3123. btrfs_set_header_bytenr(split, split->start);
  3124. btrfs_set_header_generation(split, trans->transid);
  3125. btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
  3126. btrfs_set_header_owner(split, root->root_key.objectid);
  3127. write_extent_buffer(split, root->fs_info->fsid,
  3128. btrfs_header_fsid(), BTRFS_FSID_SIZE);
  3129. write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
  3130. btrfs_header_chunk_tree_uuid(split),
  3131. BTRFS_UUID_SIZE);
  3132. ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0,
  3133. mid, c_nritems - mid);
  3134. if (ret) {
  3135. btrfs_abort_transaction(trans, root, ret);
  3136. return ret;
  3137. }
  3138. copy_extent_buffer(split, c,
  3139. btrfs_node_key_ptr_offset(0),
  3140. btrfs_node_key_ptr_offset(mid),
  3141. (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
  3142. btrfs_set_header_nritems(split, c_nritems - mid);
  3143. btrfs_set_header_nritems(c, mid);
  3144. ret = 0;
  3145. btrfs_mark_buffer_dirty(c);
  3146. btrfs_mark_buffer_dirty(split);
  3147. insert_ptr(trans, root, path, &disk_key, split->start,
  3148. path->slots[level + 1] + 1, level + 1);
  3149. if (path->slots[level] >= mid) {
  3150. path->slots[level] -= mid;
  3151. btrfs_tree_unlock(c);
  3152. free_extent_buffer(c);
  3153. path->nodes[level] = split;
  3154. path->slots[level + 1] += 1;
  3155. } else {
  3156. btrfs_tree_unlock(split);
  3157. free_extent_buffer(split);
  3158. }
  3159. return ret;
  3160. }
  3161. /*
  3162. * how many bytes are required to store the items in a leaf. start
  3163. * and nr indicate which items in the leaf to check. This totals up the
  3164. * space used both by the item structs and the item data
  3165. */
  3166. static int leaf_space_used(struct extent_buffer *l, int start, int nr)
  3167. {
  3168. struct btrfs_item *start_item;
  3169. struct btrfs_item *end_item;
  3170. struct btrfs_map_token token;
  3171. int data_len;
  3172. int nritems = btrfs_header_nritems(l);
  3173. int end = min(nritems, start + nr) - 1;
  3174. if (!nr)
  3175. return 0;
  3176. btrfs_init_map_token(&token);
  3177. start_item = btrfs_item_nr(start);
  3178. end_item = btrfs_item_nr(end);
  3179. data_len = btrfs_token_item_offset(l, start_item, &token) +
  3180. btrfs_token_item_size(l, start_item, &token);
  3181. data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
  3182. data_len += sizeof(struct btrfs_item) * nr;
  3183. WARN_ON(data_len < 0);
  3184. return data_len;
  3185. }
  3186. /*
  3187. * The space between the end of the leaf items and
  3188. * the start of the leaf data. IOW, how much room
  3189. * the leaf has left for both items and data
  3190. */
  3191. noinline int btrfs_leaf_free_space(struct btrfs_root *root,
  3192. struct extent_buffer *leaf)
  3193. {
  3194. int nritems = btrfs_header_nritems(leaf);
  3195. int ret;
  3196. ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
  3197. if (ret < 0) {
  3198. btrfs_crit(root->fs_info,
  3199. "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
  3200. ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
  3201. leaf_space_used(leaf, 0, nritems), nritems);
  3202. }
  3203. return ret;
  3204. }
  3205. /*
  3206. * min slot controls the lowest index we're willing to push to the
  3207. * right. We'll push up to and including min_slot, but no lower
  3208. */
  3209. static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
  3210. struct btrfs_root *root,
  3211. struct btrfs_path *path,
  3212. int data_size, int empty,
  3213. struct extent_buffer *right,
  3214. int free_space, u32 left_nritems,
  3215. u32 min_slot)
  3216. {
  3217. struct extent_buffer *left = path->nodes[0];
  3218. struct extent_buffer *upper = path->nodes[1];
  3219. struct btrfs_map_token token;
  3220. struct btrfs_disk_key disk_key;
  3221. int slot;
  3222. u32 i;
  3223. int push_space = 0;
  3224. int push_items = 0;
  3225. struct btrfs_item *item;
  3226. u32 nr;
  3227. u32 right_nritems;
  3228. u32 data_end;
  3229. u32 this_item_size;
  3230. btrfs_init_map_token(&token);
  3231. if (empty)
  3232. nr = 0;
  3233. else
  3234. nr = max_t(u32, 1, min_slot);
  3235. if (path->slots[0] >= left_nritems)
  3236. push_space += data_size;
  3237. slot = path->slots[1];
  3238. i = left_nritems - 1;
  3239. while (i >= nr) {
  3240. item = btrfs_item_nr(i);
  3241. if (!empty && push_items > 0) {
  3242. if (path->slots[0] > i)
  3243. break;
  3244. if (path->slots[0] == i) {
  3245. int space = btrfs_leaf_free_space(root, left);
  3246. if (space + push_space * 2 > free_space)
  3247. break;
  3248. }
  3249. }
  3250. if (path->slots[0] == i)
  3251. push_space += data_size;
  3252. this_item_size = btrfs_item_size(left, item);
  3253. if (this_item_size + sizeof(*item) + push_space > free_space)
  3254. break;
  3255. push_items++;
  3256. push_space += this_item_size + sizeof(*item);
  3257. if (i == 0)
  3258. break;
  3259. i--;
  3260. }
  3261. if (push_items == 0)
  3262. goto out_unlock;
  3263. WARN_ON(!empty && push_items == left_nritems);
  3264. /* push left to right */
  3265. right_nritems = btrfs_header_nritems(right);
  3266. push_space = btrfs_item_end_nr(left, left_nritems - push_items);
  3267. push_space -= leaf_data_end(root, left);
  3268. /* make room in the right data area */
  3269. data_end = leaf_data_end(root, right);
  3270. memmove_extent_buffer(right,
  3271. btrfs_leaf_data(right) + data_end - push_space,
  3272. btrfs_leaf_data(right) + data_end,
  3273. BTRFS_LEAF_DATA_SIZE(root) - data_end);
  3274. /* copy from the left data area */
  3275. copy_extent_buffer(right, left, btrfs_leaf_data(right) +
  3276. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  3277. btrfs_leaf_data(left) + leaf_data_end(root, left),
  3278. push_space);
  3279. memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
  3280. btrfs_item_nr_offset(0),
  3281. right_nritems * sizeof(struct btrfs_item));
  3282. /* copy the items from left to right */
  3283. copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
  3284. btrfs_item_nr_offset(left_nritems - push_items),
  3285. push_items * sizeof(struct btrfs_item));
  3286. /* update the item pointers */
  3287. right_nritems += push_items;
  3288. btrfs_set_header_nritems(right, right_nritems);
  3289. push_space = BTRFS_LEAF_DATA_SIZE(root);
  3290. for (i = 0; i < right_nritems; i++) {
  3291. item = btrfs_item_nr(i);
  3292. push_space -= btrfs_token_item_size(right, item, &token);
  3293. btrfs_set_token_item_offset(right, item, push_space, &token);
  3294. }
  3295. left_nritems -= push_items;
  3296. btrfs_set_header_nritems(left, left_nritems);
  3297. if (left_nritems)
  3298. btrfs_mark_buffer_dirty(left);
  3299. else
  3300. clean_tree_block(trans, root->fs_info, left);
  3301. btrfs_mark_buffer_dirty(right);
  3302. btrfs_item_key(right, &disk_key, 0);
  3303. btrfs_set_node_key(upper, &disk_key, slot + 1);
  3304. btrfs_mark_buffer_dirty(upper);
  3305. /* then fixup the leaf pointer in the path */
  3306. if (path->slots[0] >= left_nritems) {
  3307. path->slots[0] -= left_nritems;
  3308. if (btrfs_header_nritems(path->nodes[0]) == 0)
  3309. clean_tree_block(trans, root->fs_info, path->nodes[0]);
  3310. btrfs_tree_unlock(path->nodes[0]);
  3311. free_extent_buffer(path->nodes[0]);
  3312. path->nodes[0] = right;
  3313. path->slots[1] += 1;
  3314. } else {
  3315. btrfs_tree_unlock(right);
  3316. free_extent_buffer(right);
  3317. }
  3318. return 0;
  3319. out_unlock:
  3320. btrfs_tree_unlock(right);
  3321. free_extent_buffer(right);
  3322. return 1;
  3323. }
  3324. /*
  3325. * push some data in the path leaf to the right, trying to free up at
  3326. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3327. *
  3328. * returns 1 if the push failed because the other node didn't have enough
  3329. * room, 0 if everything worked out and < 0 if there were major errors.
  3330. *
  3331. * this will push starting from min_slot to the end of the leaf. It won't
  3332. * push any slot lower than min_slot
  3333. */
  3334. static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
  3335. *root, struct btrfs_path *path,
  3336. int min_data_size, int data_size,
  3337. int empty, u32 min_slot)
  3338. {
  3339. struct extent_buffer *left = path->nodes[0];
  3340. struct extent_buffer *right;
  3341. struct extent_buffer *upper;
  3342. int slot;
  3343. int free_space;
  3344. u32 left_nritems;
  3345. int ret;
  3346. if (!path->nodes[1])
  3347. return 1;
  3348. slot = path->slots[1];
  3349. upper = path->nodes[1];
  3350. if (slot >= btrfs_header_nritems(upper) - 1)
  3351. return 1;
  3352. btrfs_assert_tree_locked(path->nodes[1]);
  3353. right = read_node_slot(root, upper, slot + 1);
  3354. if (right == NULL)
  3355. return 1;
  3356. btrfs_tree_lock(right);
  3357. btrfs_set_lock_blocking(right);
  3358. free_space = btrfs_leaf_free_space(root, right);
  3359. if (free_space < data_size)
  3360. goto out_unlock;
  3361. /* cow and double check */
  3362. ret = btrfs_cow_block(trans, root, right, upper,
  3363. slot + 1, &right);
  3364. if (ret)
  3365. goto out_unlock;
  3366. free_space = btrfs_leaf_free_space(root, right);
  3367. if (free_space < data_size)
  3368. goto out_unlock;
  3369. left_nritems = btrfs_header_nritems(left);
  3370. if (left_nritems == 0)
  3371. goto out_unlock;
  3372. if (path->slots[0] == left_nritems && !empty) {
  3373. /* Key greater than all keys in the leaf, right neighbor has
  3374. * enough room for it and we're not emptying our leaf to delete
  3375. * it, therefore use right neighbor to insert the new item and
  3376. * no need to touch/dirty our left leaft. */
  3377. btrfs_tree_unlock(left);
  3378. free_extent_buffer(left);
  3379. path->nodes[0] = right;
  3380. path->slots[0] = 0;
  3381. path->slots[1]++;
  3382. return 0;
  3383. }
  3384. return __push_leaf_right(trans, root, path, min_data_size, empty,
  3385. right, free_space, left_nritems, min_slot);
  3386. out_unlock:
  3387. btrfs_tree_unlock(right);
  3388. free_extent_buffer(right);
  3389. return 1;
  3390. }
  3391. /*
  3392. * push some data in the path leaf to the left, trying to free up at
  3393. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3394. *
  3395. * max_slot can put a limit on how far into the leaf we'll push items. The
  3396. * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
  3397. * items
  3398. */
  3399. static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
  3400. struct btrfs_root *root,
  3401. struct btrfs_path *path, int data_size,
  3402. int empty, struct extent_buffer *left,
  3403. int free_space, u32 right_nritems,
  3404. u32 max_slot)
  3405. {
  3406. struct btrfs_disk_key disk_key;
  3407. struct extent_buffer *right = path->nodes[0];
  3408. int i;
  3409. int push_space = 0;
  3410. int push_items = 0;
  3411. struct btrfs_item *item;
  3412. u32 old_left_nritems;
  3413. u32 nr;
  3414. int ret = 0;
  3415. u32 this_item_size;
  3416. u32 old_left_item_size;
  3417. struct btrfs_map_token token;
  3418. btrfs_init_map_token(&token);
  3419. if (empty)
  3420. nr = min(right_nritems, max_slot);
  3421. else
  3422. nr = min(right_nritems - 1, max_slot);
  3423. for (i = 0; i < nr; i++) {
  3424. item = btrfs_item_nr(i);
  3425. if (!empty && push_items > 0) {
  3426. if (path->slots[0] < i)
  3427. break;
  3428. if (path->slots[0] == i) {
  3429. int space = btrfs_leaf_free_space(root, right);
  3430. if (space + push_space * 2 > free_space)
  3431. break;
  3432. }
  3433. }
  3434. if (path->slots[0] == i)
  3435. push_space += data_size;
  3436. this_item_size = btrfs_item_size(right, item);
  3437. if (this_item_size + sizeof(*item) + push_space > free_space)
  3438. break;
  3439. push_items++;
  3440. push_space += this_item_size + sizeof(*item);
  3441. }
  3442. if (push_items == 0) {
  3443. ret = 1;
  3444. goto out;
  3445. }
  3446. WARN_ON(!empty && push_items == btrfs_header_nritems(right));
  3447. /* push data from right to left */
  3448. copy_extent_buffer(left, right,
  3449. btrfs_item_nr_offset(btrfs_header_nritems(left)),
  3450. btrfs_item_nr_offset(0),
  3451. push_items * sizeof(struct btrfs_item));
  3452. push_space = BTRFS_LEAF_DATA_SIZE(root) -
  3453. btrfs_item_offset_nr(right, push_items - 1);
  3454. copy_extent_buffer(left, right, btrfs_leaf_data(left) +
  3455. leaf_data_end(root, left) - push_space,
  3456. btrfs_leaf_data(right) +
  3457. btrfs_item_offset_nr(right, push_items - 1),
  3458. push_space);
  3459. old_left_nritems = btrfs_header_nritems(left);
  3460. BUG_ON(old_left_nritems <= 0);
  3461. old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
  3462. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  3463. u32 ioff;
  3464. item = btrfs_item_nr(i);
  3465. ioff = btrfs_token_item_offset(left, item, &token);
  3466. btrfs_set_token_item_offset(left, item,
  3467. ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
  3468. &token);
  3469. }
  3470. btrfs_set_header_nritems(left, old_left_nritems + push_items);
  3471. /* fixup right node */
  3472. if (push_items > right_nritems)
  3473. WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
  3474. right_nritems);
  3475. if (push_items < right_nritems) {
  3476. push_space = btrfs_item_offset_nr(right, push_items - 1) -
  3477. leaf_data_end(root, right);
  3478. memmove_extent_buffer(right, btrfs_leaf_data(right) +
  3479. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  3480. btrfs_leaf_data(right) +
  3481. leaf_data_end(root, right), push_space);
  3482. memmove_extent_buffer(right, btrfs_item_nr_offset(0),
  3483. btrfs_item_nr_offset(push_items),
  3484. (btrfs_header_nritems(right) - push_items) *
  3485. sizeof(struct btrfs_item));
  3486. }
  3487. right_nritems -= push_items;
  3488. btrfs_set_header_nritems(right, right_nritems);
  3489. push_space = BTRFS_LEAF_DATA_SIZE(root);
  3490. for (i = 0; i < right_nritems; i++) {
  3491. item = btrfs_item_nr(i);
  3492. push_space = push_space - btrfs_token_item_size(right,
  3493. item, &token);
  3494. btrfs_set_token_item_offset(right, item, push_space, &token);
  3495. }
  3496. btrfs_mark_buffer_dirty(left);
  3497. if (right_nritems)
  3498. btrfs_mark_buffer_dirty(right);
  3499. else
  3500. clean_tree_block(trans, root->fs_info, right);
  3501. btrfs_item_key(right, &disk_key, 0);
  3502. fixup_low_keys(root->fs_info, path, &disk_key, 1);
  3503. /* then fixup the leaf pointer in the path */
  3504. if (path->slots[0] < push_items) {
  3505. path->slots[0] += old_left_nritems;
  3506. btrfs_tree_unlock(path->nodes[0]);
  3507. free_extent_buffer(path->nodes[0]);
  3508. path->nodes[0] = left;
  3509. path->slots[1] -= 1;
  3510. } else {
  3511. btrfs_tree_unlock(left);
  3512. free_extent_buffer(left);
  3513. path->slots[0] -= push_items;
  3514. }
  3515. BUG_ON(path->slots[0] < 0);
  3516. return ret;
  3517. out:
  3518. btrfs_tree_unlock(left);
  3519. free_extent_buffer(left);
  3520. return ret;
  3521. }
  3522. /*
  3523. * push some data in the path leaf to the left, trying to free up at
  3524. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3525. *
  3526. * max_slot can put a limit on how far into the leaf we'll push items. The
  3527. * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
  3528. * items
  3529. */
  3530. static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
  3531. *root, struct btrfs_path *path, int min_data_size,
  3532. int data_size, int empty, u32 max_slot)
  3533. {
  3534. struct extent_buffer *right = path->nodes[0];
  3535. struct extent_buffer *left;
  3536. int slot;
  3537. int free_space;
  3538. u32 right_nritems;
  3539. int ret = 0;
  3540. slot = path->slots[1];
  3541. if (slot == 0)
  3542. return 1;
  3543. if (!path->nodes[1])
  3544. return 1;
  3545. right_nritems = btrfs_header_nritems(right);
  3546. if (right_nritems == 0)
  3547. return 1;
  3548. btrfs_assert_tree_locked(path->nodes[1]);
  3549. left = read_node_slot(root, path->nodes[1], slot - 1);
  3550. if (left == NULL)
  3551. return 1;
  3552. btrfs_tree_lock(left);
  3553. btrfs_set_lock_blocking(left);
  3554. free_space = btrfs_leaf_free_space(root, left);
  3555. if (free_space < data_size) {
  3556. ret = 1;
  3557. goto out;
  3558. }
  3559. /* cow and double check */
  3560. ret = btrfs_cow_block(trans, root, left,
  3561. path->nodes[1], slot - 1, &left);
  3562. if (ret) {
  3563. /* we hit -ENOSPC, but it isn't fatal here */
  3564. if (ret == -ENOSPC)
  3565. ret = 1;
  3566. goto out;
  3567. }
  3568. free_space = btrfs_leaf_free_space(root, left);
  3569. if (free_space < data_size) {
  3570. ret = 1;
  3571. goto out;
  3572. }
  3573. return __push_leaf_left(trans, root, path, min_data_size,
  3574. empty, left, free_space, right_nritems,
  3575. max_slot);
  3576. out:
  3577. btrfs_tree_unlock(left);
  3578. free_extent_buffer(left);
  3579. return ret;
  3580. }
  3581. /*
  3582. * split the path's leaf in two, making sure there is at least data_size
  3583. * available for the resulting leaf level of the path.
  3584. */
  3585. static noinline void copy_for_split(struct btrfs_trans_handle *trans,
  3586. struct btrfs_root *root,
  3587. struct btrfs_path *path,
  3588. struct extent_buffer *l,
  3589. struct extent_buffer *right,
  3590. int slot, int mid, int nritems)
  3591. {
  3592. int data_copy_size;
  3593. int rt_data_off;
  3594. int i;
  3595. struct btrfs_disk_key disk_key;
  3596. struct btrfs_map_token token;
  3597. btrfs_init_map_token(&token);
  3598. nritems = nritems - mid;
  3599. btrfs_set_header_nritems(right, nritems);
  3600. data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
  3601. copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
  3602. btrfs_item_nr_offset(mid),
  3603. nritems * sizeof(struct btrfs_item));
  3604. copy_extent_buffer(right, l,
  3605. btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
  3606. data_copy_size, btrfs_leaf_data(l) +
  3607. leaf_data_end(root, l), data_copy_size);
  3608. rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
  3609. btrfs_item_end_nr(l, mid);
  3610. for (i = 0; i < nritems; i++) {
  3611. struct btrfs_item *item = btrfs_item_nr(i);
  3612. u32 ioff;
  3613. ioff = btrfs_token_item_offset(right, item, &token);
  3614. btrfs_set_token_item_offset(right, item,
  3615. ioff + rt_data_off, &token);
  3616. }
  3617. btrfs_set_header_nritems(l, mid);
  3618. btrfs_item_key(right, &disk_key, 0);
  3619. insert_ptr(trans, root, path, &disk_key, right->start,
  3620. path->slots[1] + 1, 1);
  3621. btrfs_mark_buffer_dirty(right);
  3622. btrfs_mark_buffer_dirty(l);
  3623. BUG_ON(path->slots[0] != slot);
  3624. if (mid <= slot) {
  3625. btrfs_tree_unlock(path->nodes[0]);
  3626. free_extent_buffer(path->nodes[0]);
  3627. path->nodes[0] = right;
  3628. path->slots[0] -= mid;
  3629. path->slots[1] += 1;
  3630. } else {
  3631. btrfs_tree_unlock(right);
  3632. free_extent_buffer(right);
  3633. }
  3634. BUG_ON(path->slots[0] < 0);
  3635. }
  3636. /*
  3637. * double splits happen when we need to insert a big item in the middle
  3638. * of a leaf. A double split can leave us with 3 mostly empty leaves:
  3639. * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
  3640. * A B C
  3641. *
  3642. * We avoid this by trying to push the items on either side of our target
  3643. * into the adjacent leaves. If all goes well we can avoid the double split
  3644. * completely.
  3645. */
  3646. static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
  3647. struct btrfs_root *root,
  3648. struct btrfs_path *path,
  3649. int data_size)
  3650. {
  3651. int ret;
  3652. int progress = 0;
  3653. int slot;
  3654. u32 nritems;
  3655. int space_needed = data_size;
  3656. slot = path->slots[0];
  3657. if (slot < btrfs_header_nritems(path->nodes[0]))
  3658. space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
  3659. /*
  3660. * try to push all the items after our slot into the
  3661. * right leaf
  3662. */
  3663. ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
  3664. if (ret < 0)
  3665. return ret;
  3666. if (ret == 0)
  3667. progress++;
  3668. nritems = btrfs_header_nritems(path->nodes[0]);
  3669. /*
  3670. * our goal is to get our slot at the start or end of a leaf. If
  3671. * we've done so we're done
  3672. */
  3673. if (path->slots[0] == 0 || path->slots[0] == nritems)
  3674. return 0;
  3675. if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
  3676. return 0;
  3677. /* try to push all the items before our slot into the next leaf */
  3678. slot = path->slots[0];
  3679. ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
  3680. if (ret < 0)
  3681. return ret;
  3682. if (ret == 0)
  3683. progress++;
  3684. if (progress)
  3685. return 0;
  3686. return 1;
  3687. }
  3688. /*
  3689. * split the path's leaf in two, making sure there is at least data_size
  3690. * available for the resulting leaf level of the path.
  3691. *
  3692. * returns 0 if all went well and < 0 on failure.
  3693. */
  3694. static noinline int split_leaf(struct btrfs_trans_handle *trans,
  3695. struct btrfs_root *root,
  3696. struct btrfs_key *ins_key,
  3697. struct btrfs_path *path, int data_size,
  3698. int extend)
  3699. {
  3700. struct btrfs_disk_key disk_key;
  3701. struct extent_buffer *l;
  3702. u32 nritems;
  3703. int mid;
  3704. int slot;
  3705. struct extent_buffer *right;
  3706. struct btrfs_fs_info *fs_info = root->fs_info;
  3707. int ret = 0;
  3708. int wret;
  3709. int split;
  3710. int num_doubles = 0;
  3711. int tried_avoid_double = 0;
  3712. l = path->nodes[0];
  3713. slot = path->slots[0];
  3714. if (extend && data_size + btrfs_item_size_nr(l, slot) +
  3715. sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
  3716. return -EOVERFLOW;
  3717. /* first try to make some room by pushing left and right */
  3718. if (data_size && path->nodes[1]) {
  3719. int space_needed = data_size;
  3720. if (slot < btrfs_header_nritems(l))
  3721. space_needed -= btrfs_leaf_free_space(root, l);
  3722. wret = push_leaf_right(trans, root, path, space_needed,
  3723. space_needed, 0, 0);
  3724. if (wret < 0)
  3725. return wret;
  3726. if (wret) {
  3727. wret = push_leaf_left(trans, root, path, space_needed,
  3728. space_needed, 0, (u32)-1);
  3729. if (wret < 0)
  3730. return wret;
  3731. }
  3732. l = path->nodes[0];
  3733. /* did the pushes work? */
  3734. if (btrfs_leaf_free_space(root, l) >= data_size)
  3735. return 0;
  3736. }
  3737. if (!path->nodes[1]) {
  3738. ret = insert_new_root(trans, root, path, 1);
  3739. if (ret)
  3740. return ret;
  3741. }
  3742. again:
  3743. split = 1;
  3744. l = path->nodes[0];
  3745. slot = path->slots[0];
  3746. nritems = btrfs_header_nritems(l);
  3747. mid = (nritems + 1) / 2;
  3748. if (mid <= slot) {
  3749. if (nritems == 1 ||
  3750. leaf_space_used(l, mid, nritems - mid) + data_size >
  3751. BTRFS_LEAF_DATA_SIZE(root)) {
  3752. if (slot >= nritems) {
  3753. split = 0;
  3754. } else {
  3755. mid = slot;
  3756. if (mid != nritems &&
  3757. leaf_space_used(l, mid, nritems - mid) +
  3758. data_size > BTRFS_LEAF_DATA_SIZE(root)) {
  3759. if (data_size && !tried_avoid_double)
  3760. goto push_for_double;
  3761. split = 2;
  3762. }
  3763. }
  3764. }
  3765. } else {
  3766. if (leaf_space_used(l, 0, mid) + data_size >
  3767. BTRFS_LEAF_DATA_SIZE(root)) {
  3768. if (!extend && data_size && slot == 0) {
  3769. split = 0;
  3770. } else if ((extend || !data_size) && slot == 0) {
  3771. mid = 1;
  3772. } else {
  3773. mid = slot;
  3774. if (mid != nritems &&
  3775. leaf_space_used(l, mid, nritems - mid) +
  3776. data_size > BTRFS_LEAF_DATA_SIZE(root)) {
  3777. if (data_size && !tried_avoid_double)
  3778. goto push_for_double;
  3779. split = 2;
  3780. }
  3781. }
  3782. }
  3783. }
  3784. if (split == 0)
  3785. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  3786. else
  3787. btrfs_item_key(l, &disk_key, mid);
  3788. right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3789. &disk_key, 0, l->start, 0);
  3790. if (IS_ERR(right))
  3791. return PTR_ERR(right);
  3792. root_add_used(root, root->nodesize);
  3793. memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
  3794. btrfs_set_header_bytenr(right, right->start);
  3795. btrfs_set_header_generation(right, trans->transid);
  3796. btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
  3797. btrfs_set_header_owner(right, root->root_key.objectid);
  3798. btrfs_set_header_level(right, 0);
  3799. write_extent_buffer(right, fs_info->fsid,
  3800. btrfs_header_fsid(), BTRFS_FSID_SIZE);
  3801. write_extent_buffer(right, fs_info->chunk_tree_uuid,
  3802. btrfs_header_chunk_tree_uuid(right),
  3803. BTRFS_UUID_SIZE);
  3804. if (split == 0) {
  3805. if (mid <= slot) {
  3806. btrfs_set_header_nritems(right, 0);
  3807. insert_ptr(trans, root, path, &disk_key, right->start,
  3808. path->slots[1] + 1, 1);
  3809. btrfs_tree_unlock(path->nodes[0]);
  3810. free_extent_buffer(path->nodes[0]);
  3811. path->nodes[0] = right;
  3812. path->slots[0] = 0;
  3813. path->slots[1] += 1;
  3814. } else {
  3815. btrfs_set_header_nritems(right, 0);
  3816. insert_ptr(trans, root, path, &disk_key, right->start,
  3817. path->slots[1], 1);
  3818. btrfs_tree_unlock(path->nodes[0]);
  3819. free_extent_buffer(path->nodes[0]);
  3820. path->nodes[0] = right;
  3821. path->slots[0] = 0;
  3822. if (path->slots[1] == 0)
  3823. fixup_low_keys(fs_info, path, &disk_key, 1);
  3824. }
  3825. btrfs_mark_buffer_dirty(right);
  3826. return ret;
  3827. }
  3828. copy_for_split(trans, root, path, l, right, slot, mid, nritems);
  3829. if (split == 2) {
  3830. BUG_ON(num_doubles != 0);
  3831. num_doubles++;
  3832. goto again;
  3833. }
  3834. return 0;
  3835. push_for_double:
  3836. push_for_double_split(trans, root, path, data_size);
  3837. tried_avoid_double = 1;
  3838. if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
  3839. return 0;
  3840. goto again;
  3841. }
  3842. static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
  3843. struct btrfs_root *root,
  3844. struct btrfs_path *path, int ins_len)
  3845. {
  3846. struct btrfs_key key;
  3847. struct extent_buffer *leaf;
  3848. struct btrfs_file_extent_item *fi;
  3849. u64 extent_len = 0;
  3850. u32 item_size;
  3851. int ret;
  3852. leaf = path->nodes[0];
  3853. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3854. BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
  3855. key.type != BTRFS_EXTENT_CSUM_KEY);
  3856. if (btrfs_leaf_free_space(root, leaf) >= ins_len)
  3857. return 0;
  3858. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3859. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3860. fi = btrfs_item_ptr(leaf, path->slots[0],
  3861. struct btrfs_file_extent_item);
  3862. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  3863. }
  3864. btrfs_release_path(path);
  3865. path->keep_locks = 1;
  3866. path->search_for_split = 1;
  3867. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3868. path->search_for_split = 0;
  3869. if (ret > 0)
  3870. ret = -EAGAIN;
  3871. if (ret < 0)
  3872. goto err;
  3873. ret = -EAGAIN;
  3874. leaf = path->nodes[0];
  3875. /* if our item isn't there, return now */
  3876. if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
  3877. goto err;
  3878. /* the leaf has changed, it now has room. return now */
  3879. if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
  3880. goto err;
  3881. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3882. fi = btrfs_item_ptr(leaf, path->slots[0],
  3883. struct btrfs_file_extent_item);
  3884. if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
  3885. goto err;
  3886. }
  3887. btrfs_set_path_blocking(path);
  3888. ret = split_leaf(trans, root, &key, path, ins_len, 1);
  3889. if (ret)
  3890. goto err;
  3891. path->keep_locks = 0;
  3892. btrfs_unlock_up_safe(path, 1);
  3893. return 0;
  3894. err:
  3895. path->keep_locks = 0;
  3896. return ret;
  3897. }
  3898. static noinline int split_item(struct btrfs_trans_handle *trans,
  3899. struct btrfs_root *root,
  3900. struct btrfs_path *path,
  3901. struct btrfs_key *new_key,
  3902. unsigned long split_offset)
  3903. {
  3904. struct extent_buffer *leaf;
  3905. struct btrfs_item *item;
  3906. struct btrfs_item *new_item;
  3907. int slot;
  3908. char *buf;
  3909. u32 nritems;
  3910. u32 item_size;
  3911. u32 orig_offset;
  3912. struct btrfs_disk_key disk_key;
  3913. leaf = path->nodes[0];
  3914. BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
  3915. btrfs_set_path_blocking(path);
  3916. item = btrfs_item_nr(path->slots[0]);
  3917. orig_offset = btrfs_item_offset(leaf, item);
  3918. item_size = btrfs_item_size(leaf, item);
  3919. buf = kmalloc(item_size, GFP_NOFS);
  3920. if (!buf)
  3921. return -ENOMEM;
  3922. read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
  3923. path->slots[0]), item_size);
  3924. slot = path->slots[0] + 1;
  3925. nritems = btrfs_header_nritems(leaf);
  3926. if (slot != nritems) {
  3927. /* shift the items */
  3928. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
  3929. btrfs_item_nr_offset(slot),
  3930. (nritems - slot) * sizeof(struct btrfs_item));
  3931. }
  3932. btrfs_cpu_key_to_disk(&disk_key, new_key);
  3933. btrfs_set_item_key(leaf, &disk_key, slot);
  3934. new_item = btrfs_item_nr(slot);
  3935. btrfs_set_item_offset(leaf, new_item, orig_offset);
  3936. btrfs_set_item_size(leaf, new_item, item_size - split_offset);
  3937. btrfs_set_item_offset(leaf, item,
  3938. orig_offset + item_size - split_offset);
  3939. btrfs_set_item_size(leaf, item, split_offset);
  3940. btrfs_set_header_nritems(leaf, nritems + 1);
  3941. /* write the data for the start of the original item */
  3942. write_extent_buffer(leaf, buf,
  3943. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3944. split_offset);
  3945. /* write the data for the new item */
  3946. write_extent_buffer(leaf, buf + split_offset,
  3947. btrfs_item_ptr_offset(leaf, slot),
  3948. item_size - split_offset);
  3949. btrfs_mark_buffer_dirty(leaf);
  3950. BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
  3951. kfree(buf);
  3952. return 0;
  3953. }
  3954. /*
  3955. * This function splits a single item into two items,
  3956. * giving 'new_key' to the new item and splitting the
  3957. * old one at split_offset (from the start of the item).
  3958. *
  3959. * The path may be released by this operation. After
  3960. * the split, the path is pointing to the old item. The
  3961. * new item is going to be in the same node as the old one.
  3962. *
  3963. * Note, the item being split must be smaller enough to live alone on
  3964. * a tree block with room for one extra struct btrfs_item
  3965. *
  3966. * This allows us to split the item in place, keeping a lock on the
  3967. * leaf the entire time.
  3968. */
  3969. int btrfs_split_item(struct btrfs_trans_handle *trans,
  3970. struct btrfs_root *root,
  3971. struct btrfs_path *path,
  3972. struct btrfs_key *new_key,
  3973. unsigned long split_offset)
  3974. {
  3975. int ret;
  3976. ret = setup_leaf_for_split(trans, root, path,
  3977. sizeof(struct btrfs_item));
  3978. if (ret)
  3979. return ret;
  3980. ret = split_item(trans, root, path, new_key, split_offset);
  3981. return ret;
  3982. }
  3983. /*
  3984. * This function duplicate a item, giving 'new_key' to the new item.
  3985. * It guarantees both items live in the same tree leaf and the new item
  3986. * is contiguous with the original item.
  3987. *
  3988. * This allows us to split file extent in place, keeping a lock on the
  3989. * leaf the entire time.
  3990. */
  3991. int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
  3992. struct btrfs_root *root,
  3993. struct btrfs_path *path,
  3994. struct btrfs_key *new_key)
  3995. {
  3996. struct extent_buffer *leaf;
  3997. int ret;
  3998. u32 item_size;
  3999. leaf = path->nodes[0];
  4000. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  4001. ret = setup_leaf_for_split(trans, root, path,
  4002. item_size + sizeof(struct btrfs_item));
  4003. if (ret)
  4004. return ret;
  4005. path->slots[0]++;
  4006. setup_items_for_insert(root, path, new_key, &item_size,
  4007. item_size, item_size +
  4008. sizeof(struct btrfs_item), 1);
  4009. leaf = path->nodes[0];
  4010. memcpy_extent_buffer(leaf,
  4011. btrfs_item_ptr_offset(leaf, path->slots[0]),
  4012. btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
  4013. item_size);
  4014. return 0;
  4015. }
  4016. /*
  4017. * make the item pointed to by the path smaller. new_size indicates
  4018. * how small to make it, and from_end tells us if we just chop bytes
  4019. * off the end of the item or if we shift the item to chop bytes off
  4020. * the front.
  4021. */
  4022. void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
  4023. u32 new_size, int from_end)
  4024. {
  4025. int slot;
  4026. struct extent_buffer *leaf;
  4027. struct btrfs_item *item;
  4028. u32 nritems;
  4029. unsigned int data_end;
  4030. unsigned int old_data_start;
  4031. unsigned int old_size;
  4032. unsigned int size_diff;
  4033. int i;
  4034. struct btrfs_map_token token;
  4035. btrfs_init_map_token(&token);
  4036. leaf = path->nodes[0];
  4037. slot = path->slots[0];
  4038. old_size = btrfs_item_size_nr(leaf, slot);
  4039. if (old_size == new_size)
  4040. return;
  4041. nritems = btrfs_header_nritems(leaf);
  4042. data_end = leaf_data_end(root, leaf);
  4043. old_data_start = btrfs_item_offset_nr(leaf, slot);
  4044. size_diff = old_size - new_size;
  4045. BUG_ON(slot < 0);
  4046. BUG_ON(slot >= nritems);
  4047. /*
  4048. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4049. */
  4050. /* first correct the data pointers */
  4051. for (i = slot; i < nritems; i++) {
  4052. u32 ioff;
  4053. item = btrfs_item_nr(i);
  4054. ioff = btrfs_token_item_offset(leaf, item, &token);
  4055. btrfs_set_token_item_offset(leaf, item,
  4056. ioff + size_diff, &token);
  4057. }
  4058. /* shift the data */
  4059. if (from_end) {
  4060. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4061. data_end + size_diff, btrfs_leaf_data(leaf) +
  4062. data_end, old_data_start + new_size - data_end);
  4063. } else {
  4064. struct btrfs_disk_key disk_key;
  4065. u64 offset;
  4066. btrfs_item_key(leaf, &disk_key, slot);
  4067. if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
  4068. unsigned long ptr;
  4069. struct btrfs_file_extent_item *fi;
  4070. fi = btrfs_item_ptr(leaf, slot,
  4071. struct btrfs_file_extent_item);
  4072. fi = (struct btrfs_file_extent_item *)(
  4073. (unsigned long)fi - size_diff);
  4074. if (btrfs_file_extent_type(leaf, fi) ==
  4075. BTRFS_FILE_EXTENT_INLINE) {
  4076. ptr = btrfs_item_ptr_offset(leaf, slot);
  4077. memmove_extent_buffer(leaf, ptr,
  4078. (unsigned long)fi,
  4079. BTRFS_FILE_EXTENT_INLINE_DATA_START);
  4080. }
  4081. }
  4082. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4083. data_end + size_diff, btrfs_leaf_data(leaf) +
  4084. data_end, old_data_start - data_end);
  4085. offset = btrfs_disk_key_offset(&disk_key);
  4086. btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
  4087. btrfs_set_item_key(leaf, &disk_key, slot);
  4088. if (slot == 0)
  4089. fixup_low_keys(root->fs_info, path, &disk_key, 1);
  4090. }
  4091. item = btrfs_item_nr(slot);
  4092. btrfs_set_item_size(leaf, item, new_size);
  4093. btrfs_mark_buffer_dirty(leaf);
  4094. if (btrfs_leaf_free_space(root, leaf) < 0) {
  4095. btrfs_print_leaf(root, leaf);
  4096. BUG();
  4097. }
  4098. }
  4099. /*
  4100. * make the item pointed to by the path bigger, data_size is the added size.
  4101. */
  4102. void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
  4103. u32 data_size)
  4104. {
  4105. int slot;
  4106. struct extent_buffer *leaf;
  4107. struct btrfs_item *item;
  4108. u32 nritems;
  4109. unsigned int data_end;
  4110. unsigned int old_data;
  4111. unsigned int old_size;
  4112. int i;
  4113. struct btrfs_map_token token;
  4114. btrfs_init_map_token(&token);
  4115. leaf = path->nodes[0];
  4116. nritems = btrfs_header_nritems(leaf);
  4117. data_end = leaf_data_end(root, leaf);
  4118. if (btrfs_leaf_free_space(root, leaf) < data_size) {
  4119. btrfs_print_leaf(root, leaf);
  4120. BUG();
  4121. }
  4122. slot = path->slots[0];
  4123. old_data = btrfs_item_end_nr(leaf, slot);
  4124. BUG_ON(slot < 0);
  4125. if (slot >= nritems) {
  4126. btrfs_print_leaf(root, leaf);
  4127. btrfs_crit(root->fs_info, "slot %d too large, nritems %d",
  4128. slot, nritems);
  4129. BUG_ON(1);
  4130. }
  4131. /*
  4132. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4133. */
  4134. /* first correct the data pointers */
  4135. for (i = slot; i < nritems; i++) {
  4136. u32 ioff;
  4137. item = btrfs_item_nr(i);
  4138. ioff = btrfs_token_item_offset(leaf, item, &token);
  4139. btrfs_set_token_item_offset(leaf, item,
  4140. ioff - data_size, &token);
  4141. }
  4142. /* shift the data */
  4143. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4144. data_end - data_size, btrfs_leaf_data(leaf) +
  4145. data_end, old_data - data_end);
  4146. data_end = old_data;
  4147. old_size = btrfs_item_size_nr(leaf, slot);
  4148. item = btrfs_item_nr(slot);
  4149. btrfs_set_item_size(leaf, item, old_size + data_size);
  4150. btrfs_mark_buffer_dirty(leaf);
  4151. if (btrfs_leaf_free_space(root, leaf) < 0) {
  4152. btrfs_print_leaf(root, leaf);
  4153. BUG();
  4154. }
  4155. }
  4156. /*
  4157. * this is a helper for btrfs_insert_empty_items, the main goal here is
  4158. * to save stack depth by doing the bulk of the work in a function
  4159. * that doesn't call btrfs_search_slot
  4160. */
  4161. void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
  4162. struct btrfs_key *cpu_key, u32 *data_size,
  4163. u32 total_data, u32 total_size, int nr)
  4164. {
  4165. struct btrfs_item *item;
  4166. int i;
  4167. u32 nritems;
  4168. unsigned int data_end;
  4169. struct btrfs_disk_key disk_key;
  4170. struct extent_buffer *leaf;
  4171. int slot;
  4172. struct btrfs_map_token token;
  4173. if (path->slots[0] == 0) {
  4174. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  4175. fixup_low_keys(root->fs_info, path, &disk_key, 1);
  4176. }
  4177. btrfs_unlock_up_safe(path, 1);
  4178. btrfs_init_map_token(&token);
  4179. leaf = path->nodes[0];
  4180. slot = path->slots[0];
  4181. nritems = btrfs_header_nritems(leaf);
  4182. data_end = leaf_data_end(root, leaf);
  4183. if (btrfs_leaf_free_space(root, leaf) < total_size) {
  4184. btrfs_print_leaf(root, leaf);
  4185. btrfs_crit(root->fs_info, "not enough freespace need %u have %d",
  4186. total_size, btrfs_leaf_free_space(root, leaf));
  4187. BUG();
  4188. }
  4189. if (slot != nritems) {
  4190. unsigned int old_data = btrfs_item_end_nr(leaf, slot);
  4191. if (old_data < data_end) {
  4192. btrfs_print_leaf(root, leaf);
  4193. btrfs_crit(root->fs_info, "slot %d old_data %d data_end %d",
  4194. slot, old_data, data_end);
  4195. BUG_ON(1);
  4196. }
  4197. /*
  4198. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4199. */
  4200. /* first correct the data pointers */
  4201. for (i = slot; i < nritems; i++) {
  4202. u32 ioff;
  4203. item = btrfs_item_nr( i);
  4204. ioff = btrfs_token_item_offset(leaf, item, &token);
  4205. btrfs_set_token_item_offset(leaf, item,
  4206. ioff - total_data, &token);
  4207. }
  4208. /* shift the items */
  4209. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
  4210. btrfs_item_nr_offset(slot),
  4211. (nritems - slot) * sizeof(struct btrfs_item));
  4212. /* shift the data */
  4213. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4214. data_end - total_data, btrfs_leaf_data(leaf) +
  4215. data_end, old_data - data_end);
  4216. data_end = old_data;
  4217. }
  4218. /* setup the item for the new data */
  4219. for (i = 0; i < nr; i++) {
  4220. btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
  4221. btrfs_set_item_key(leaf, &disk_key, slot + i);
  4222. item = btrfs_item_nr(slot + i);
  4223. btrfs_set_token_item_offset(leaf, item,
  4224. data_end - data_size[i], &token);
  4225. data_end -= data_size[i];
  4226. btrfs_set_token_item_size(leaf, item, data_size[i], &token);
  4227. }
  4228. btrfs_set_header_nritems(leaf, nritems + nr);
  4229. btrfs_mark_buffer_dirty(leaf);
  4230. if (btrfs_leaf_free_space(root, leaf) < 0) {
  4231. btrfs_print_leaf(root, leaf);
  4232. BUG();
  4233. }
  4234. }
  4235. /*
  4236. * Given a key and some data, insert items into the tree.
  4237. * This does all the path init required, making room in the tree if needed.
  4238. */
  4239. int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
  4240. struct btrfs_root *root,
  4241. struct btrfs_path *path,
  4242. struct btrfs_key *cpu_key, u32 *data_size,
  4243. int nr)
  4244. {
  4245. int ret = 0;
  4246. int slot;
  4247. int i;
  4248. u32 total_size = 0;
  4249. u32 total_data = 0;
  4250. for (i = 0; i < nr; i++)
  4251. total_data += data_size[i];
  4252. total_size = total_data + (nr * sizeof(struct btrfs_item));
  4253. ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
  4254. if (ret == 0)
  4255. return -EEXIST;
  4256. if (ret < 0)
  4257. return ret;
  4258. slot = path->slots[0];
  4259. BUG_ON(slot < 0);
  4260. setup_items_for_insert(root, path, cpu_key, data_size,
  4261. total_data, total_size, nr);
  4262. return 0;
  4263. }
  4264. /*
  4265. * Given a key and some data, insert an item into the tree.
  4266. * This does all the path init required, making room in the tree if needed.
  4267. */
  4268. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  4269. *root, struct btrfs_key *cpu_key, void *data, u32
  4270. data_size)
  4271. {
  4272. int ret = 0;
  4273. struct btrfs_path *path;
  4274. struct extent_buffer *leaf;
  4275. unsigned long ptr;
  4276. path = btrfs_alloc_path();
  4277. if (!path)
  4278. return -ENOMEM;
  4279. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  4280. if (!ret) {
  4281. leaf = path->nodes[0];
  4282. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  4283. write_extent_buffer(leaf, data, ptr, data_size);
  4284. btrfs_mark_buffer_dirty(leaf);
  4285. }
  4286. btrfs_free_path(path);
  4287. return ret;
  4288. }
  4289. /*
  4290. * delete the pointer from a given node.
  4291. *
  4292. * the tree should have been previously balanced so the deletion does not
  4293. * empty a node.
  4294. */
  4295. static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
  4296. int level, int slot)
  4297. {
  4298. struct extent_buffer *parent = path->nodes[level];
  4299. u32 nritems;
  4300. int ret;
  4301. nritems = btrfs_header_nritems(parent);
  4302. if (slot != nritems - 1) {
  4303. if (level)
  4304. tree_mod_log_eb_move(root->fs_info, parent, slot,
  4305. slot + 1, nritems - slot - 1);
  4306. memmove_extent_buffer(parent,
  4307. btrfs_node_key_ptr_offset(slot),
  4308. btrfs_node_key_ptr_offset(slot + 1),
  4309. sizeof(struct btrfs_key_ptr) *
  4310. (nritems - slot - 1));
  4311. } else if (level) {
  4312. ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
  4313. MOD_LOG_KEY_REMOVE, GFP_NOFS);
  4314. BUG_ON(ret < 0);
  4315. }
  4316. nritems--;
  4317. btrfs_set_header_nritems(parent, nritems);
  4318. if (nritems == 0 && parent == root->node) {
  4319. BUG_ON(btrfs_header_level(root->node) != 1);
  4320. /* just turn the root into a leaf and break */
  4321. btrfs_set_header_level(root->node, 0);
  4322. } else if (slot == 0) {
  4323. struct btrfs_disk_key disk_key;
  4324. btrfs_node_key(parent, &disk_key, 0);
  4325. fixup_low_keys(root->fs_info, path, &disk_key, level + 1);
  4326. }
  4327. btrfs_mark_buffer_dirty(parent);
  4328. }
  4329. /*
  4330. * a helper function to delete the leaf pointed to by path->slots[1] and
  4331. * path->nodes[1].
  4332. *
  4333. * This deletes the pointer in path->nodes[1] and frees the leaf
  4334. * block extent. zero is returned if it all worked out, < 0 otherwise.
  4335. *
  4336. * The path must have already been setup for deleting the leaf, including
  4337. * all the proper balancing. path->nodes[1] must be locked.
  4338. */
  4339. static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
  4340. struct btrfs_root *root,
  4341. struct btrfs_path *path,
  4342. struct extent_buffer *leaf)
  4343. {
  4344. WARN_ON(btrfs_header_generation(leaf) != trans->transid);
  4345. del_ptr(root, path, 1, path->slots[1]);
  4346. /*
  4347. * btrfs_free_extent is expensive, we want to make sure we
  4348. * aren't holding any locks when we call it
  4349. */
  4350. btrfs_unlock_up_safe(path, 0);
  4351. root_sub_used(root, leaf->len);
  4352. extent_buffer_get(leaf);
  4353. btrfs_free_tree_block(trans, root, leaf, 0, 1);
  4354. free_extent_buffer_stale(leaf);
  4355. }
  4356. /*
  4357. * delete the item at the leaf level in path. If that empties
  4358. * the leaf, remove it from the tree
  4359. */
  4360. int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4361. struct btrfs_path *path, int slot, int nr)
  4362. {
  4363. struct extent_buffer *leaf;
  4364. struct btrfs_item *item;
  4365. u32 last_off;
  4366. u32 dsize = 0;
  4367. int ret = 0;
  4368. int wret;
  4369. int i;
  4370. u32 nritems;
  4371. struct btrfs_map_token token;
  4372. btrfs_init_map_token(&token);
  4373. leaf = path->nodes[0];
  4374. last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
  4375. for (i = 0; i < nr; i++)
  4376. dsize += btrfs_item_size_nr(leaf, slot + i);
  4377. nritems = btrfs_header_nritems(leaf);
  4378. if (slot + nr != nritems) {
  4379. int data_end = leaf_data_end(root, leaf);
  4380. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4381. data_end + dsize,
  4382. btrfs_leaf_data(leaf) + data_end,
  4383. last_off - data_end);
  4384. for (i = slot + nr; i < nritems; i++) {
  4385. u32 ioff;
  4386. item = btrfs_item_nr(i);
  4387. ioff = btrfs_token_item_offset(leaf, item, &token);
  4388. btrfs_set_token_item_offset(leaf, item,
  4389. ioff + dsize, &token);
  4390. }
  4391. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
  4392. btrfs_item_nr_offset(slot + nr),
  4393. sizeof(struct btrfs_item) *
  4394. (nritems - slot - nr));
  4395. }
  4396. btrfs_set_header_nritems(leaf, nritems - nr);
  4397. nritems -= nr;
  4398. /* delete the leaf if we've emptied it */
  4399. if (nritems == 0) {
  4400. if (leaf == root->node) {
  4401. btrfs_set_header_level(leaf, 0);
  4402. } else {
  4403. btrfs_set_path_blocking(path);
  4404. clean_tree_block(trans, root->fs_info, leaf);
  4405. btrfs_del_leaf(trans, root, path, leaf);
  4406. }
  4407. } else {
  4408. int used = leaf_space_used(leaf, 0, nritems);
  4409. if (slot == 0) {
  4410. struct btrfs_disk_key disk_key;
  4411. btrfs_item_key(leaf, &disk_key, 0);
  4412. fixup_low_keys(root->fs_info, path, &disk_key, 1);
  4413. }
  4414. /* delete the leaf if it is mostly empty */
  4415. if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
  4416. /* push_leaf_left fixes the path.
  4417. * make sure the path still points to our leaf
  4418. * for possible call to del_ptr below
  4419. */
  4420. slot = path->slots[1];
  4421. extent_buffer_get(leaf);
  4422. btrfs_set_path_blocking(path);
  4423. wret = push_leaf_left(trans, root, path, 1, 1,
  4424. 1, (u32)-1);
  4425. if (wret < 0 && wret != -ENOSPC)
  4426. ret = wret;
  4427. if (path->nodes[0] == leaf &&
  4428. btrfs_header_nritems(leaf)) {
  4429. wret = push_leaf_right(trans, root, path, 1,
  4430. 1, 1, 0);
  4431. if (wret < 0 && wret != -ENOSPC)
  4432. ret = wret;
  4433. }
  4434. if (btrfs_header_nritems(leaf) == 0) {
  4435. path->slots[1] = slot;
  4436. btrfs_del_leaf(trans, root, path, leaf);
  4437. free_extent_buffer(leaf);
  4438. ret = 0;
  4439. } else {
  4440. /* if we're still in the path, make sure
  4441. * we're dirty. Otherwise, one of the
  4442. * push_leaf functions must have already
  4443. * dirtied this buffer
  4444. */
  4445. if (path->nodes[0] == leaf)
  4446. btrfs_mark_buffer_dirty(leaf);
  4447. free_extent_buffer(leaf);
  4448. }
  4449. } else {
  4450. btrfs_mark_buffer_dirty(leaf);
  4451. }
  4452. }
  4453. return ret;
  4454. }
  4455. /*
  4456. * search the tree again to find a leaf with lesser keys
  4457. * returns 0 if it found something or 1 if there are no lesser leaves.
  4458. * returns < 0 on io errors.
  4459. *
  4460. * This may release the path, and so you may lose any locks held at the
  4461. * time you call it.
  4462. */
  4463. int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
  4464. {
  4465. struct btrfs_key key;
  4466. struct btrfs_disk_key found_key;
  4467. int ret;
  4468. btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
  4469. if (key.offset > 0) {
  4470. key.offset--;
  4471. } else if (key.type > 0) {
  4472. key.type--;
  4473. key.offset = (u64)-1;
  4474. } else if (key.objectid > 0) {
  4475. key.objectid--;
  4476. key.type = (u8)-1;
  4477. key.offset = (u64)-1;
  4478. } else {
  4479. return 1;
  4480. }
  4481. btrfs_release_path(path);
  4482. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4483. if (ret < 0)
  4484. return ret;
  4485. btrfs_item_key(path->nodes[0], &found_key, 0);
  4486. ret = comp_keys(&found_key, &key);
  4487. /*
  4488. * We might have had an item with the previous key in the tree right
  4489. * before we released our path. And after we released our path, that
  4490. * item might have been pushed to the first slot (0) of the leaf we
  4491. * were holding due to a tree balance. Alternatively, an item with the
  4492. * previous key can exist as the only element of a leaf (big fat item).
  4493. * Therefore account for these 2 cases, so that our callers (like
  4494. * btrfs_previous_item) don't miss an existing item with a key matching
  4495. * the previous key we computed above.
  4496. */
  4497. if (ret <= 0)
  4498. return 0;
  4499. return 1;
  4500. }
  4501. /*
  4502. * A helper function to walk down the tree starting at min_key, and looking
  4503. * for nodes or leaves that are have a minimum transaction id.
  4504. * This is used by the btree defrag code, and tree logging
  4505. *
  4506. * This does not cow, but it does stuff the starting key it finds back
  4507. * into min_key, so you can call btrfs_search_slot with cow=1 on the
  4508. * key and get a writable path.
  4509. *
  4510. * This does lock as it descends, and path->keep_locks should be set
  4511. * to 1 by the caller.
  4512. *
  4513. * This honors path->lowest_level to prevent descent past a given level
  4514. * of the tree.
  4515. *
  4516. * min_trans indicates the oldest transaction that you are interested
  4517. * in walking through. Any nodes or leaves older than min_trans are
  4518. * skipped over (without reading them).
  4519. *
  4520. * returns zero if something useful was found, < 0 on error and 1 if there
  4521. * was nothing in the tree that matched the search criteria.
  4522. */
  4523. int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
  4524. struct btrfs_path *path,
  4525. u64 min_trans)
  4526. {
  4527. struct extent_buffer *cur;
  4528. struct btrfs_key found_key;
  4529. int slot;
  4530. int sret;
  4531. u32 nritems;
  4532. int level;
  4533. int ret = 1;
  4534. int keep_locks = path->keep_locks;
  4535. path->keep_locks = 1;
  4536. again:
  4537. cur = btrfs_read_lock_root_node(root);
  4538. level = btrfs_header_level(cur);
  4539. WARN_ON(path->nodes[level]);
  4540. path->nodes[level] = cur;
  4541. path->locks[level] = BTRFS_READ_LOCK;
  4542. if (btrfs_header_generation(cur) < min_trans) {
  4543. ret = 1;
  4544. goto out;
  4545. }
  4546. while (1) {
  4547. nritems = btrfs_header_nritems(cur);
  4548. level = btrfs_header_level(cur);
  4549. sret = bin_search(cur, min_key, level, &slot);
  4550. /* at the lowest level, we're done, setup the path and exit */
  4551. if (level == path->lowest_level) {
  4552. if (slot >= nritems)
  4553. goto find_next_key;
  4554. ret = 0;
  4555. path->slots[level] = slot;
  4556. btrfs_item_key_to_cpu(cur, &found_key, slot);
  4557. goto out;
  4558. }
  4559. if (sret && slot > 0)
  4560. slot--;
  4561. /*
  4562. * check this node pointer against the min_trans parameters.
  4563. * If it is too old, old, skip to the next one.
  4564. */
  4565. while (slot < nritems) {
  4566. u64 gen;
  4567. gen = btrfs_node_ptr_generation(cur, slot);
  4568. if (gen < min_trans) {
  4569. slot++;
  4570. continue;
  4571. }
  4572. break;
  4573. }
  4574. find_next_key:
  4575. /*
  4576. * we didn't find a candidate key in this node, walk forward
  4577. * and find another one
  4578. */
  4579. if (slot >= nritems) {
  4580. path->slots[level] = slot;
  4581. btrfs_set_path_blocking(path);
  4582. sret = btrfs_find_next_key(root, path, min_key, level,
  4583. min_trans);
  4584. if (sret == 0) {
  4585. btrfs_release_path(path);
  4586. goto again;
  4587. } else {
  4588. goto out;
  4589. }
  4590. }
  4591. /* save our key for returning back */
  4592. btrfs_node_key_to_cpu(cur, &found_key, slot);
  4593. path->slots[level] = slot;
  4594. if (level == path->lowest_level) {
  4595. ret = 0;
  4596. goto out;
  4597. }
  4598. btrfs_set_path_blocking(path);
  4599. cur = read_node_slot(root, cur, slot);
  4600. BUG_ON(!cur); /* -ENOMEM */
  4601. btrfs_tree_read_lock(cur);
  4602. path->locks[level - 1] = BTRFS_READ_LOCK;
  4603. path->nodes[level - 1] = cur;
  4604. unlock_up(path, level, 1, 0, NULL);
  4605. btrfs_clear_path_blocking(path, NULL, 0);
  4606. }
  4607. out:
  4608. path->keep_locks = keep_locks;
  4609. if (ret == 0) {
  4610. btrfs_unlock_up_safe(path, path->lowest_level + 1);
  4611. btrfs_set_path_blocking(path);
  4612. memcpy(min_key, &found_key, sizeof(found_key));
  4613. }
  4614. return ret;
  4615. }
  4616. static void tree_move_down(struct btrfs_root *root,
  4617. struct btrfs_path *path,
  4618. int *level, int root_level)
  4619. {
  4620. BUG_ON(*level == 0);
  4621. path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
  4622. path->slots[*level]);
  4623. path->slots[*level - 1] = 0;
  4624. (*level)--;
  4625. }
  4626. static int tree_move_next_or_upnext(struct btrfs_root *root,
  4627. struct btrfs_path *path,
  4628. int *level, int root_level)
  4629. {
  4630. int ret = 0;
  4631. int nritems;
  4632. nritems = btrfs_header_nritems(path->nodes[*level]);
  4633. path->slots[*level]++;
  4634. while (path->slots[*level] >= nritems) {
  4635. if (*level == root_level)
  4636. return -1;
  4637. /* move upnext */
  4638. path->slots[*level] = 0;
  4639. free_extent_buffer(path->nodes[*level]);
  4640. path->nodes[*level] = NULL;
  4641. (*level)++;
  4642. path->slots[*level]++;
  4643. nritems = btrfs_header_nritems(path->nodes[*level]);
  4644. ret = 1;
  4645. }
  4646. return ret;
  4647. }
  4648. /*
  4649. * Returns 1 if it had to move up and next. 0 is returned if it moved only next
  4650. * or down.
  4651. */
  4652. static int tree_advance(struct btrfs_root *root,
  4653. struct btrfs_path *path,
  4654. int *level, int root_level,
  4655. int allow_down,
  4656. struct btrfs_key *key)
  4657. {
  4658. int ret;
  4659. if (*level == 0 || !allow_down) {
  4660. ret = tree_move_next_or_upnext(root, path, level, root_level);
  4661. } else {
  4662. tree_move_down(root, path, level, root_level);
  4663. ret = 0;
  4664. }
  4665. if (ret >= 0) {
  4666. if (*level == 0)
  4667. btrfs_item_key_to_cpu(path->nodes[*level], key,
  4668. path->slots[*level]);
  4669. else
  4670. btrfs_node_key_to_cpu(path->nodes[*level], key,
  4671. path->slots[*level]);
  4672. }
  4673. return ret;
  4674. }
  4675. static int tree_compare_item(struct btrfs_root *left_root,
  4676. struct btrfs_path *left_path,
  4677. struct btrfs_path *right_path,
  4678. char *tmp_buf)
  4679. {
  4680. int cmp;
  4681. int len1, len2;
  4682. unsigned long off1, off2;
  4683. len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
  4684. len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
  4685. if (len1 != len2)
  4686. return 1;
  4687. off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
  4688. off2 = btrfs_item_ptr_offset(right_path->nodes[0],
  4689. right_path->slots[0]);
  4690. read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
  4691. cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
  4692. if (cmp)
  4693. return 1;
  4694. return 0;
  4695. }
  4696. #define ADVANCE 1
  4697. #define ADVANCE_ONLY_NEXT -1
  4698. /*
  4699. * This function compares two trees and calls the provided callback for
  4700. * every changed/new/deleted item it finds.
  4701. * If shared tree blocks are encountered, whole subtrees are skipped, making
  4702. * the compare pretty fast on snapshotted subvolumes.
  4703. *
  4704. * This currently works on commit roots only. As commit roots are read only,
  4705. * we don't do any locking. The commit roots are protected with transactions.
  4706. * Transactions are ended and rejoined when a commit is tried in between.
  4707. *
  4708. * This function checks for modifications done to the trees while comparing.
  4709. * If it detects a change, it aborts immediately.
  4710. */
  4711. int btrfs_compare_trees(struct btrfs_root *left_root,
  4712. struct btrfs_root *right_root,
  4713. btrfs_changed_cb_t changed_cb, void *ctx)
  4714. {
  4715. int ret;
  4716. int cmp;
  4717. struct btrfs_path *left_path = NULL;
  4718. struct btrfs_path *right_path = NULL;
  4719. struct btrfs_key left_key;
  4720. struct btrfs_key right_key;
  4721. char *tmp_buf = NULL;
  4722. int left_root_level;
  4723. int right_root_level;
  4724. int left_level;
  4725. int right_level;
  4726. int left_end_reached;
  4727. int right_end_reached;
  4728. int advance_left;
  4729. int advance_right;
  4730. u64 left_blockptr;
  4731. u64 right_blockptr;
  4732. u64 left_gen;
  4733. u64 right_gen;
  4734. left_path = btrfs_alloc_path();
  4735. if (!left_path) {
  4736. ret = -ENOMEM;
  4737. goto out;
  4738. }
  4739. right_path = btrfs_alloc_path();
  4740. if (!right_path) {
  4741. ret = -ENOMEM;
  4742. goto out;
  4743. }
  4744. tmp_buf = kmalloc(left_root->nodesize, GFP_KERNEL | __GFP_NOWARN);
  4745. if (!tmp_buf) {
  4746. tmp_buf = vmalloc(left_root->nodesize);
  4747. if (!tmp_buf) {
  4748. ret = -ENOMEM;
  4749. goto out;
  4750. }
  4751. }
  4752. left_path->search_commit_root = 1;
  4753. left_path->skip_locking = 1;
  4754. right_path->search_commit_root = 1;
  4755. right_path->skip_locking = 1;
  4756. /*
  4757. * Strategy: Go to the first items of both trees. Then do
  4758. *
  4759. * If both trees are at level 0
  4760. * Compare keys of current items
  4761. * If left < right treat left item as new, advance left tree
  4762. * and repeat
  4763. * If left > right treat right item as deleted, advance right tree
  4764. * and repeat
  4765. * If left == right do deep compare of items, treat as changed if
  4766. * needed, advance both trees and repeat
  4767. * If both trees are at the same level but not at level 0
  4768. * Compare keys of current nodes/leafs
  4769. * If left < right advance left tree and repeat
  4770. * If left > right advance right tree and repeat
  4771. * If left == right compare blockptrs of the next nodes/leafs
  4772. * If they match advance both trees but stay at the same level
  4773. * and repeat
  4774. * If they don't match advance both trees while allowing to go
  4775. * deeper and repeat
  4776. * If tree levels are different
  4777. * Advance the tree that needs it and repeat
  4778. *
  4779. * Advancing a tree means:
  4780. * If we are at level 0, try to go to the next slot. If that's not
  4781. * possible, go one level up and repeat. Stop when we found a level
  4782. * where we could go to the next slot. We may at this point be on a
  4783. * node or a leaf.
  4784. *
  4785. * If we are not at level 0 and not on shared tree blocks, go one
  4786. * level deeper.
  4787. *
  4788. * If we are not at level 0 and on shared tree blocks, go one slot to
  4789. * the right if possible or go up and right.
  4790. */
  4791. down_read(&left_root->fs_info->commit_root_sem);
  4792. left_level = btrfs_header_level(left_root->commit_root);
  4793. left_root_level = left_level;
  4794. left_path->nodes[left_level] = left_root->commit_root;
  4795. extent_buffer_get(left_path->nodes[left_level]);
  4796. right_level = btrfs_header_level(right_root->commit_root);
  4797. right_root_level = right_level;
  4798. right_path->nodes[right_level] = right_root->commit_root;
  4799. extent_buffer_get(right_path->nodes[right_level]);
  4800. up_read(&left_root->fs_info->commit_root_sem);
  4801. if (left_level == 0)
  4802. btrfs_item_key_to_cpu(left_path->nodes[left_level],
  4803. &left_key, left_path->slots[left_level]);
  4804. else
  4805. btrfs_node_key_to_cpu(left_path->nodes[left_level],
  4806. &left_key, left_path->slots[left_level]);
  4807. if (right_level == 0)
  4808. btrfs_item_key_to_cpu(right_path->nodes[right_level],
  4809. &right_key, right_path->slots[right_level]);
  4810. else
  4811. btrfs_node_key_to_cpu(right_path->nodes[right_level],
  4812. &right_key, right_path->slots[right_level]);
  4813. left_end_reached = right_end_reached = 0;
  4814. advance_left = advance_right = 0;
  4815. while (1) {
  4816. if (advance_left && !left_end_reached) {
  4817. ret = tree_advance(left_root, left_path, &left_level,
  4818. left_root_level,
  4819. advance_left != ADVANCE_ONLY_NEXT,
  4820. &left_key);
  4821. if (ret < 0)
  4822. left_end_reached = ADVANCE;
  4823. advance_left = 0;
  4824. }
  4825. if (advance_right && !right_end_reached) {
  4826. ret = tree_advance(right_root, right_path, &right_level,
  4827. right_root_level,
  4828. advance_right != ADVANCE_ONLY_NEXT,
  4829. &right_key);
  4830. if (ret < 0)
  4831. right_end_reached = ADVANCE;
  4832. advance_right = 0;
  4833. }
  4834. if (left_end_reached && right_end_reached) {
  4835. ret = 0;
  4836. goto out;
  4837. } else if (left_end_reached) {
  4838. if (right_level == 0) {
  4839. ret = changed_cb(left_root, right_root,
  4840. left_path, right_path,
  4841. &right_key,
  4842. BTRFS_COMPARE_TREE_DELETED,
  4843. ctx);
  4844. if (ret < 0)
  4845. goto out;
  4846. }
  4847. advance_right = ADVANCE;
  4848. continue;
  4849. } else if (right_end_reached) {
  4850. if (left_level == 0) {
  4851. ret = changed_cb(left_root, right_root,
  4852. left_path, right_path,
  4853. &left_key,
  4854. BTRFS_COMPARE_TREE_NEW,
  4855. ctx);
  4856. if (ret < 0)
  4857. goto out;
  4858. }
  4859. advance_left = ADVANCE;
  4860. continue;
  4861. }
  4862. if (left_level == 0 && right_level == 0) {
  4863. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4864. if (cmp < 0) {
  4865. ret = changed_cb(left_root, right_root,
  4866. left_path, right_path,
  4867. &left_key,
  4868. BTRFS_COMPARE_TREE_NEW,
  4869. ctx);
  4870. if (ret < 0)
  4871. goto out;
  4872. advance_left = ADVANCE;
  4873. } else if (cmp > 0) {
  4874. ret = changed_cb(left_root, right_root,
  4875. left_path, right_path,
  4876. &right_key,
  4877. BTRFS_COMPARE_TREE_DELETED,
  4878. ctx);
  4879. if (ret < 0)
  4880. goto out;
  4881. advance_right = ADVANCE;
  4882. } else {
  4883. enum btrfs_compare_tree_result result;
  4884. WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
  4885. ret = tree_compare_item(left_root, left_path,
  4886. right_path, tmp_buf);
  4887. if (ret)
  4888. result = BTRFS_COMPARE_TREE_CHANGED;
  4889. else
  4890. result = BTRFS_COMPARE_TREE_SAME;
  4891. ret = changed_cb(left_root, right_root,
  4892. left_path, right_path,
  4893. &left_key, result, ctx);
  4894. if (ret < 0)
  4895. goto out;
  4896. advance_left = ADVANCE;
  4897. advance_right = ADVANCE;
  4898. }
  4899. } else if (left_level == right_level) {
  4900. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4901. if (cmp < 0) {
  4902. advance_left = ADVANCE;
  4903. } else if (cmp > 0) {
  4904. advance_right = ADVANCE;
  4905. } else {
  4906. left_blockptr = btrfs_node_blockptr(
  4907. left_path->nodes[left_level],
  4908. left_path->slots[left_level]);
  4909. right_blockptr = btrfs_node_blockptr(
  4910. right_path->nodes[right_level],
  4911. right_path->slots[right_level]);
  4912. left_gen = btrfs_node_ptr_generation(
  4913. left_path->nodes[left_level],
  4914. left_path->slots[left_level]);
  4915. right_gen = btrfs_node_ptr_generation(
  4916. right_path->nodes[right_level],
  4917. right_path->slots[right_level]);
  4918. if (left_blockptr == right_blockptr &&
  4919. left_gen == right_gen) {
  4920. /*
  4921. * As we're on a shared block, don't
  4922. * allow to go deeper.
  4923. */
  4924. advance_left = ADVANCE_ONLY_NEXT;
  4925. advance_right = ADVANCE_ONLY_NEXT;
  4926. } else {
  4927. advance_left = ADVANCE;
  4928. advance_right = ADVANCE;
  4929. }
  4930. }
  4931. } else if (left_level < right_level) {
  4932. advance_right = ADVANCE;
  4933. } else {
  4934. advance_left = ADVANCE;
  4935. }
  4936. }
  4937. out:
  4938. btrfs_free_path(left_path);
  4939. btrfs_free_path(right_path);
  4940. kvfree(tmp_buf);
  4941. return ret;
  4942. }
  4943. /*
  4944. * this is similar to btrfs_next_leaf, but does not try to preserve
  4945. * and fixup the path. It looks for and returns the next key in the
  4946. * tree based on the current path and the min_trans parameters.
  4947. *
  4948. * 0 is returned if another key is found, < 0 if there are any errors
  4949. * and 1 is returned if there are no higher keys in the tree
  4950. *
  4951. * path->keep_locks should be set to 1 on the search made before
  4952. * calling this function.
  4953. */
  4954. int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
  4955. struct btrfs_key *key, int level, u64 min_trans)
  4956. {
  4957. int slot;
  4958. struct extent_buffer *c;
  4959. WARN_ON(!path->keep_locks);
  4960. while (level < BTRFS_MAX_LEVEL) {
  4961. if (!path->nodes[level])
  4962. return 1;
  4963. slot = path->slots[level] + 1;
  4964. c = path->nodes[level];
  4965. next:
  4966. if (slot >= btrfs_header_nritems(c)) {
  4967. int ret;
  4968. int orig_lowest;
  4969. struct btrfs_key cur_key;
  4970. if (level + 1 >= BTRFS_MAX_LEVEL ||
  4971. !path->nodes[level + 1])
  4972. return 1;
  4973. if (path->locks[level + 1]) {
  4974. level++;
  4975. continue;
  4976. }
  4977. slot = btrfs_header_nritems(c) - 1;
  4978. if (level == 0)
  4979. btrfs_item_key_to_cpu(c, &cur_key, slot);
  4980. else
  4981. btrfs_node_key_to_cpu(c, &cur_key, slot);
  4982. orig_lowest = path->lowest_level;
  4983. btrfs_release_path(path);
  4984. path->lowest_level = level;
  4985. ret = btrfs_search_slot(NULL, root, &cur_key, path,
  4986. 0, 0);
  4987. path->lowest_level = orig_lowest;
  4988. if (ret < 0)
  4989. return ret;
  4990. c = path->nodes[level];
  4991. slot = path->slots[level];
  4992. if (ret == 0)
  4993. slot++;
  4994. goto next;
  4995. }
  4996. if (level == 0)
  4997. btrfs_item_key_to_cpu(c, key, slot);
  4998. else {
  4999. u64 gen = btrfs_node_ptr_generation(c, slot);
  5000. if (gen < min_trans) {
  5001. slot++;
  5002. goto next;
  5003. }
  5004. btrfs_node_key_to_cpu(c, key, slot);
  5005. }
  5006. return 0;
  5007. }
  5008. return 1;
  5009. }
  5010. /*
  5011. * search the tree again to find a leaf with greater keys
  5012. * returns 0 if it found something or 1 if there are no greater leaves.
  5013. * returns < 0 on io errors.
  5014. */
  5015. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
  5016. {
  5017. return btrfs_next_old_leaf(root, path, 0);
  5018. }
  5019. int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
  5020. u64 time_seq)
  5021. {
  5022. int slot;
  5023. int level;
  5024. struct extent_buffer *c;
  5025. struct extent_buffer *next;
  5026. struct btrfs_key key;
  5027. u32 nritems;
  5028. int ret;
  5029. int old_spinning = path->leave_spinning;
  5030. int next_rw_lock = 0;
  5031. nritems = btrfs_header_nritems(path->nodes[0]);
  5032. if (nritems == 0)
  5033. return 1;
  5034. btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
  5035. again:
  5036. level = 1;
  5037. next = NULL;
  5038. next_rw_lock = 0;
  5039. btrfs_release_path(path);
  5040. path->keep_locks = 1;
  5041. path->leave_spinning = 1;
  5042. if (time_seq)
  5043. ret = btrfs_search_old_slot(root, &key, path, time_seq);
  5044. else
  5045. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5046. path->keep_locks = 0;
  5047. if (ret < 0)
  5048. return ret;
  5049. nritems = btrfs_header_nritems(path->nodes[0]);
  5050. /*
  5051. * by releasing the path above we dropped all our locks. A balance
  5052. * could have added more items next to the key that used to be
  5053. * at the very end of the block. So, check again here and
  5054. * advance the path if there are now more items available.
  5055. */
  5056. if (nritems > 0 && path->slots[0] < nritems - 1) {
  5057. if (ret == 0)
  5058. path->slots[0]++;
  5059. ret = 0;
  5060. goto done;
  5061. }
  5062. /*
  5063. * So the above check misses one case:
  5064. * - after releasing the path above, someone has removed the item that
  5065. * used to be at the very end of the block, and balance between leafs
  5066. * gets another one with bigger key.offset to replace it.
  5067. *
  5068. * This one should be returned as well, or we can get leaf corruption
  5069. * later(esp. in __btrfs_drop_extents()).
  5070. *
  5071. * And a bit more explanation about this check,
  5072. * with ret > 0, the key isn't found, the path points to the slot
  5073. * where it should be inserted, so the path->slots[0] item must be the
  5074. * bigger one.
  5075. */
  5076. if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
  5077. ret = 0;
  5078. goto done;
  5079. }
  5080. while (level < BTRFS_MAX_LEVEL) {
  5081. if (!path->nodes[level]) {
  5082. ret = 1;
  5083. goto done;
  5084. }
  5085. slot = path->slots[level] + 1;
  5086. c = path->nodes[level];
  5087. if (slot >= btrfs_header_nritems(c)) {
  5088. level++;
  5089. if (level == BTRFS_MAX_LEVEL) {
  5090. ret = 1;
  5091. goto done;
  5092. }
  5093. continue;
  5094. }
  5095. if (next) {
  5096. btrfs_tree_unlock_rw(next, next_rw_lock);
  5097. free_extent_buffer(next);
  5098. }
  5099. next = c;
  5100. next_rw_lock = path->locks[level];
  5101. ret = read_block_for_search(NULL, root, path, &next, level,
  5102. slot, &key, 0);
  5103. if (ret == -EAGAIN)
  5104. goto again;
  5105. if (ret < 0) {
  5106. btrfs_release_path(path);
  5107. goto done;
  5108. }
  5109. if (!path->skip_locking) {
  5110. ret = btrfs_try_tree_read_lock(next);
  5111. if (!ret && time_seq) {
  5112. /*
  5113. * If we don't get the lock, we may be racing
  5114. * with push_leaf_left, holding that lock while
  5115. * itself waiting for the leaf we've currently
  5116. * locked. To solve this situation, we give up
  5117. * on our lock and cycle.
  5118. */
  5119. free_extent_buffer(next);
  5120. btrfs_release_path(path);
  5121. cond_resched();
  5122. goto again;
  5123. }
  5124. if (!ret) {
  5125. btrfs_set_path_blocking(path);
  5126. btrfs_tree_read_lock(next);
  5127. btrfs_clear_path_blocking(path, next,
  5128. BTRFS_READ_LOCK);
  5129. }
  5130. next_rw_lock = BTRFS_READ_LOCK;
  5131. }
  5132. break;
  5133. }
  5134. path->slots[level] = slot;
  5135. while (1) {
  5136. level--;
  5137. c = path->nodes[level];
  5138. if (path->locks[level])
  5139. btrfs_tree_unlock_rw(c, path->locks[level]);
  5140. free_extent_buffer(c);
  5141. path->nodes[level] = next;
  5142. path->slots[level] = 0;
  5143. if (!path->skip_locking)
  5144. path->locks[level] = next_rw_lock;
  5145. if (!level)
  5146. break;
  5147. ret = read_block_for_search(NULL, root, path, &next, level,
  5148. 0, &key, 0);
  5149. if (ret == -EAGAIN)
  5150. goto again;
  5151. if (ret < 0) {
  5152. btrfs_release_path(path);
  5153. goto done;
  5154. }
  5155. if (!path->skip_locking) {
  5156. ret = btrfs_try_tree_read_lock(next);
  5157. if (!ret) {
  5158. btrfs_set_path_blocking(path);
  5159. btrfs_tree_read_lock(next);
  5160. btrfs_clear_path_blocking(path, next,
  5161. BTRFS_READ_LOCK);
  5162. }
  5163. next_rw_lock = BTRFS_READ_LOCK;
  5164. }
  5165. }
  5166. ret = 0;
  5167. done:
  5168. unlock_up(path, 0, 1, 0, NULL);
  5169. path->leave_spinning = old_spinning;
  5170. if (!old_spinning)
  5171. btrfs_set_path_blocking(path);
  5172. return ret;
  5173. }
  5174. /*
  5175. * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
  5176. * searching until it gets past min_objectid or finds an item of 'type'
  5177. *
  5178. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5179. */
  5180. int btrfs_previous_item(struct btrfs_root *root,
  5181. struct btrfs_path *path, u64 min_objectid,
  5182. int type)
  5183. {
  5184. struct btrfs_key found_key;
  5185. struct extent_buffer *leaf;
  5186. u32 nritems;
  5187. int ret;
  5188. while (1) {
  5189. if (path->slots[0] == 0) {
  5190. btrfs_set_path_blocking(path);
  5191. ret = btrfs_prev_leaf(root, path);
  5192. if (ret != 0)
  5193. return ret;
  5194. } else {
  5195. path->slots[0]--;
  5196. }
  5197. leaf = path->nodes[0];
  5198. nritems = btrfs_header_nritems(leaf);
  5199. if (nritems == 0)
  5200. return 1;
  5201. if (path->slots[0] == nritems)
  5202. path->slots[0]--;
  5203. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5204. if (found_key.objectid < min_objectid)
  5205. break;
  5206. if (found_key.type == type)
  5207. return 0;
  5208. if (found_key.objectid == min_objectid &&
  5209. found_key.type < type)
  5210. break;
  5211. }
  5212. return 1;
  5213. }
  5214. /*
  5215. * search in extent tree to find a previous Metadata/Data extent item with
  5216. * min objecitd.
  5217. *
  5218. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5219. */
  5220. int btrfs_previous_extent_item(struct btrfs_root *root,
  5221. struct btrfs_path *path, u64 min_objectid)
  5222. {
  5223. struct btrfs_key found_key;
  5224. struct extent_buffer *leaf;
  5225. u32 nritems;
  5226. int ret;
  5227. while (1) {
  5228. if (path->slots[0] == 0) {
  5229. btrfs_set_path_blocking(path);
  5230. ret = btrfs_prev_leaf(root, path);
  5231. if (ret != 0)
  5232. return ret;
  5233. } else {
  5234. path->slots[0]--;
  5235. }
  5236. leaf = path->nodes[0];
  5237. nritems = btrfs_header_nritems(leaf);
  5238. if (nritems == 0)
  5239. return 1;
  5240. if (path->slots[0] == nritems)
  5241. path->slots[0]--;
  5242. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5243. if (found_key.objectid < min_objectid)
  5244. break;
  5245. if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
  5246. found_key.type == BTRFS_METADATA_ITEM_KEY)
  5247. return 0;
  5248. if (found_key.objectid == min_objectid &&
  5249. found_key.type < BTRFS_EXTENT_ITEM_KEY)
  5250. break;
  5251. }
  5252. return 1;
  5253. }