ctree.c 152 KB

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