ctree.c 153 KB

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