ctree.c 152 KB

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