ctree.c 153 KB

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