ctree.c 152 KB

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