ctree.c 152 KB

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