ctree.c 152 KB

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