ctree.c 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929
  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 && path->locks[i]) {
  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. free_extent_buffer(tmp);
  2160. if (p->reada != READA_NONE)
  2161. reada_for_search(fs_info, p, level, slot, key->objectid);
  2162. ret = -EAGAIN;
  2163. tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
  2164. &first_key);
  2165. if (!IS_ERR(tmp)) {
  2166. /*
  2167. * If the read above didn't mark this buffer up to date,
  2168. * it will never end up being up to date. Set ret to EIO now
  2169. * and give up so that our caller doesn't loop forever
  2170. * on our EAGAINs.
  2171. */
  2172. if (!btrfs_buffer_uptodate(tmp, 0, 0))
  2173. ret = -EIO;
  2174. free_extent_buffer(tmp);
  2175. } else {
  2176. ret = PTR_ERR(tmp);
  2177. }
  2178. btrfs_release_path(p);
  2179. return ret;
  2180. }
  2181. /*
  2182. * helper function for btrfs_search_slot. This does all of the checks
  2183. * for node-level blocks and does any balancing required based on
  2184. * the ins_len.
  2185. *
  2186. * If no extra work was required, zero is returned. If we had to
  2187. * drop the path, -EAGAIN is returned and btrfs_search_slot must
  2188. * start over
  2189. */
  2190. static int
  2191. setup_nodes_for_search(struct btrfs_trans_handle *trans,
  2192. struct btrfs_root *root, struct btrfs_path *p,
  2193. struct extent_buffer *b, int level, int ins_len,
  2194. int *write_lock_level)
  2195. {
  2196. struct btrfs_fs_info *fs_info = root->fs_info;
  2197. int ret;
  2198. if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
  2199. BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
  2200. int sret;
  2201. if (*write_lock_level < level + 1) {
  2202. *write_lock_level = level + 1;
  2203. btrfs_release_path(p);
  2204. goto again;
  2205. }
  2206. btrfs_set_path_blocking(p);
  2207. reada_for_balance(fs_info, p, level);
  2208. sret = split_node(trans, root, p, level);
  2209. btrfs_clear_path_blocking(p, NULL, 0);
  2210. BUG_ON(sret > 0);
  2211. if (sret) {
  2212. ret = sret;
  2213. goto done;
  2214. }
  2215. b = p->nodes[level];
  2216. } else if (ins_len < 0 && btrfs_header_nritems(b) <
  2217. BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
  2218. int sret;
  2219. if (*write_lock_level < level + 1) {
  2220. *write_lock_level = level + 1;
  2221. btrfs_release_path(p);
  2222. goto again;
  2223. }
  2224. btrfs_set_path_blocking(p);
  2225. reada_for_balance(fs_info, p, level);
  2226. sret = balance_level(trans, root, p, level);
  2227. btrfs_clear_path_blocking(p, NULL, 0);
  2228. if (sret) {
  2229. ret = sret;
  2230. goto done;
  2231. }
  2232. b = p->nodes[level];
  2233. if (!b) {
  2234. btrfs_release_path(p);
  2235. goto again;
  2236. }
  2237. BUG_ON(btrfs_header_nritems(b) == 1);
  2238. }
  2239. return 0;
  2240. again:
  2241. ret = -EAGAIN;
  2242. done:
  2243. return ret;
  2244. }
  2245. static void key_search_validate(struct extent_buffer *b,
  2246. const struct btrfs_key *key,
  2247. int level)
  2248. {
  2249. #ifdef CONFIG_BTRFS_ASSERT
  2250. struct btrfs_disk_key disk_key;
  2251. btrfs_cpu_key_to_disk(&disk_key, key);
  2252. if (level == 0)
  2253. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2254. offsetof(struct btrfs_leaf, items[0].key),
  2255. sizeof(disk_key)));
  2256. else
  2257. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2258. offsetof(struct btrfs_node, ptrs[0].key),
  2259. sizeof(disk_key)));
  2260. #endif
  2261. }
  2262. static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
  2263. int level, int *prev_cmp, int *slot)
  2264. {
  2265. if (*prev_cmp != 0) {
  2266. *prev_cmp = btrfs_bin_search(b, key, level, slot);
  2267. return *prev_cmp;
  2268. }
  2269. key_search_validate(b, key, level);
  2270. *slot = 0;
  2271. return 0;
  2272. }
  2273. int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
  2274. u64 iobjectid, u64 ioff, u8 key_type,
  2275. struct btrfs_key *found_key)
  2276. {
  2277. int ret;
  2278. struct btrfs_key key;
  2279. struct extent_buffer *eb;
  2280. ASSERT(path);
  2281. ASSERT(found_key);
  2282. key.type = key_type;
  2283. key.objectid = iobjectid;
  2284. key.offset = ioff;
  2285. ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  2286. if (ret < 0)
  2287. return ret;
  2288. eb = path->nodes[0];
  2289. if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
  2290. ret = btrfs_next_leaf(fs_root, path);
  2291. if (ret)
  2292. return ret;
  2293. eb = path->nodes[0];
  2294. }
  2295. btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
  2296. if (found_key->type != key.type ||
  2297. found_key->objectid != key.objectid)
  2298. return 1;
  2299. return 0;
  2300. }
  2301. /*
  2302. * btrfs_search_slot - look for a key in a tree and perform necessary
  2303. * modifications to preserve tree invariants.
  2304. *
  2305. * @trans: Handle of transaction, used when modifying the tree
  2306. * @p: Holds all btree nodes along the search path
  2307. * @root: The root node of the tree
  2308. * @key: The key we are looking for
  2309. * @ins_len: Indicates purpose of search, for inserts it is 1, for
  2310. * deletions it's -1. 0 for plain searches
  2311. * @cow: boolean should CoW operations be performed. Must always be 1
  2312. * when modifying the tree.
  2313. *
  2314. * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
  2315. * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
  2316. *
  2317. * If @key is found, 0 is returned and you can find the item in the leaf level
  2318. * of the path (level 0)
  2319. *
  2320. * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
  2321. * points to the slot where it should be inserted
  2322. *
  2323. * If an error is encountered while searching the tree a negative error number
  2324. * is returned
  2325. */
  2326. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2327. const struct btrfs_key *key, struct btrfs_path *p,
  2328. int ins_len, int cow)
  2329. {
  2330. struct btrfs_fs_info *fs_info = root->fs_info;
  2331. struct extent_buffer *b;
  2332. int slot;
  2333. int ret;
  2334. int err;
  2335. int level;
  2336. int lowest_unlock = 1;
  2337. int root_lock;
  2338. /* everything at write_lock_level or lower must be write locked */
  2339. int write_lock_level = 0;
  2340. u8 lowest_level = 0;
  2341. int min_write_lock_level;
  2342. int prev_cmp;
  2343. lowest_level = p->lowest_level;
  2344. WARN_ON(lowest_level && ins_len > 0);
  2345. WARN_ON(p->nodes[0] != NULL);
  2346. BUG_ON(!cow && ins_len);
  2347. if (ins_len < 0) {
  2348. lowest_unlock = 2;
  2349. /* when we are removing items, we might have to go up to level
  2350. * two as we update tree pointers Make sure we keep write
  2351. * for those levels as well
  2352. */
  2353. write_lock_level = 2;
  2354. } else if (ins_len > 0) {
  2355. /*
  2356. * for inserting items, make sure we have a write lock on
  2357. * level 1 so we can update keys
  2358. */
  2359. write_lock_level = 1;
  2360. }
  2361. if (!cow)
  2362. write_lock_level = -1;
  2363. if (cow && (p->keep_locks || p->lowest_level))
  2364. write_lock_level = BTRFS_MAX_LEVEL;
  2365. min_write_lock_level = write_lock_level;
  2366. again:
  2367. prev_cmp = -1;
  2368. /*
  2369. * we try very hard to do read locks on the root
  2370. */
  2371. root_lock = BTRFS_READ_LOCK;
  2372. level = 0;
  2373. if (p->search_commit_root) {
  2374. /*
  2375. * the commit roots are read only
  2376. * so we always do read locks
  2377. */
  2378. if (p->need_commit_sem)
  2379. down_read(&fs_info->commit_root_sem);
  2380. b = root->commit_root;
  2381. extent_buffer_get(b);
  2382. level = btrfs_header_level(b);
  2383. if (p->need_commit_sem)
  2384. up_read(&fs_info->commit_root_sem);
  2385. if (!p->skip_locking)
  2386. btrfs_tree_read_lock(b);
  2387. } else {
  2388. if (p->skip_locking) {
  2389. b = btrfs_root_node(root);
  2390. level = btrfs_header_level(b);
  2391. } else {
  2392. /* we don't know the level of the root node
  2393. * until we actually have it read locked
  2394. */
  2395. b = btrfs_read_lock_root_node(root);
  2396. level = btrfs_header_level(b);
  2397. if (level <= write_lock_level) {
  2398. /* whoops, must trade for write lock */
  2399. btrfs_tree_read_unlock(b);
  2400. free_extent_buffer(b);
  2401. b = btrfs_lock_root_node(root);
  2402. root_lock = BTRFS_WRITE_LOCK;
  2403. /* the level might have changed, check again */
  2404. level = btrfs_header_level(b);
  2405. }
  2406. }
  2407. }
  2408. p->nodes[level] = b;
  2409. if (!p->skip_locking)
  2410. p->locks[level] = root_lock;
  2411. while (b) {
  2412. level = btrfs_header_level(b);
  2413. /*
  2414. * setup the path here so we can release it under lock
  2415. * contention with the cow code
  2416. */
  2417. if (cow) {
  2418. bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
  2419. /*
  2420. * if we don't really need to cow this block
  2421. * then we don't want to set the path blocking,
  2422. * so we test it here
  2423. */
  2424. if (!should_cow_block(trans, root, b)) {
  2425. trans->dirty = true;
  2426. goto cow_done;
  2427. }
  2428. /*
  2429. * must have write locks on this node and the
  2430. * parent
  2431. */
  2432. if (level > write_lock_level ||
  2433. (level + 1 > write_lock_level &&
  2434. level + 1 < BTRFS_MAX_LEVEL &&
  2435. p->nodes[level + 1])) {
  2436. write_lock_level = level + 1;
  2437. btrfs_release_path(p);
  2438. goto again;
  2439. }
  2440. btrfs_set_path_blocking(p);
  2441. if (last_level)
  2442. err = btrfs_cow_block(trans, root, b, NULL, 0,
  2443. &b);
  2444. else
  2445. err = btrfs_cow_block(trans, root, b,
  2446. p->nodes[level + 1],
  2447. p->slots[level + 1], &b);
  2448. if (err) {
  2449. ret = err;
  2450. goto done;
  2451. }
  2452. }
  2453. cow_done:
  2454. p->nodes[level] = b;
  2455. btrfs_clear_path_blocking(p, NULL, 0);
  2456. /*
  2457. * we have a lock on b and as long as we aren't changing
  2458. * the tree, there is no way to for the items in b to change.
  2459. * It is safe to drop the lock on our parent before we
  2460. * go through the expensive btree search on b.
  2461. *
  2462. * If we're inserting or deleting (ins_len != 0), then we might
  2463. * be changing slot zero, which may require changing the parent.
  2464. * So, we can't drop the lock until after we know which slot
  2465. * we're operating on.
  2466. */
  2467. if (!ins_len && !p->keep_locks) {
  2468. int u = level + 1;
  2469. if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
  2470. btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
  2471. p->locks[u] = 0;
  2472. }
  2473. }
  2474. ret = key_search(b, key, level, &prev_cmp, &slot);
  2475. if (ret < 0)
  2476. goto done;
  2477. if (level != 0) {
  2478. int dec = 0;
  2479. if (ret && slot > 0) {
  2480. dec = 1;
  2481. slot -= 1;
  2482. }
  2483. p->slots[level] = slot;
  2484. err = setup_nodes_for_search(trans, root, p, b, level,
  2485. ins_len, &write_lock_level);
  2486. if (err == -EAGAIN)
  2487. goto again;
  2488. if (err) {
  2489. ret = err;
  2490. goto done;
  2491. }
  2492. b = p->nodes[level];
  2493. slot = p->slots[level];
  2494. /*
  2495. * slot 0 is special, if we change the key
  2496. * we have to update the parent pointer
  2497. * which means we must have a write lock
  2498. * on the parent
  2499. */
  2500. if (slot == 0 && ins_len &&
  2501. write_lock_level < level + 1) {
  2502. write_lock_level = level + 1;
  2503. btrfs_release_path(p);
  2504. goto again;
  2505. }
  2506. unlock_up(p, level, lowest_unlock,
  2507. min_write_lock_level, &write_lock_level);
  2508. if (level == lowest_level) {
  2509. if (dec)
  2510. p->slots[level]++;
  2511. goto done;
  2512. }
  2513. err = read_block_for_search(root, p, &b, level,
  2514. slot, key);
  2515. if (err == -EAGAIN)
  2516. goto again;
  2517. if (err) {
  2518. ret = err;
  2519. goto done;
  2520. }
  2521. if (!p->skip_locking) {
  2522. level = btrfs_header_level(b);
  2523. if (level <= write_lock_level) {
  2524. err = btrfs_try_tree_write_lock(b);
  2525. if (!err) {
  2526. btrfs_set_path_blocking(p);
  2527. btrfs_tree_lock(b);
  2528. btrfs_clear_path_blocking(p, b,
  2529. BTRFS_WRITE_LOCK);
  2530. }
  2531. p->locks[level] = BTRFS_WRITE_LOCK;
  2532. } else {
  2533. err = btrfs_tree_read_lock_atomic(b);
  2534. if (!err) {
  2535. btrfs_set_path_blocking(p);
  2536. btrfs_tree_read_lock(b);
  2537. btrfs_clear_path_blocking(p, b,
  2538. BTRFS_READ_LOCK);
  2539. }
  2540. p->locks[level] = BTRFS_READ_LOCK;
  2541. }
  2542. p->nodes[level] = b;
  2543. }
  2544. } else {
  2545. p->slots[level] = slot;
  2546. if (ins_len > 0 &&
  2547. btrfs_leaf_free_space(fs_info, b) < ins_len) {
  2548. if (write_lock_level < 1) {
  2549. write_lock_level = 1;
  2550. btrfs_release_path(p);
  2551. goto again;
  2552. }
  2553. btrfs_set_path_blocking(p);
  2554. err = split_leaf(trans, root, key,
  2555. p, ins_len, ret == 0);
  2556. btrfs_clear_path_blocking(p, NULL, 0);
  2557. BUG_ON(err > 0);
  2558. if (err) {
  2559. ret = err;
  2560. goto done;
  2561. }
  2562. }
  2563. if (!p->search_for_split)
  2564. unlock_up(p, level, lowest_unlock,
  2565. min_write_lock_level, &write_lock_level);
  2566. goto done;
  2567. }
  2568. }
  2569. ret = 1;
  2570. done:
  2571. /*
  2572. * we don't really know what they plan on doing with the path
  2573. * from here on, so for now just mark it as blocking
  2574. */
  2575. if (!p->leave_spinning)
  2576. btrfs_set_path_blocking(p);
  2577. if (ret < 0 && !p->skip_release_on_error)
  2578. btrfs_release_path(p);
  2579. return ret;
  2580. }
  2581. /*
  2582. * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
  2583. * current state of the tree together with the operations recorded in the tree
  2584. * modification log to search for the key in a previous version of this tree, as
  2585. * denoted by the time_seq parameter.
  2586. *
  2587. * Naturally, there is no support for insert, delete or cow operations.
  2588. *
  2589. * The resulting path and return value will be set up as if we called
  2590. * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
  2591. */
  2592. int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
  2593. struct btrfs_path *p, u64 time_seq)
  2594. {
  2595. struct btrfs_fs_info *fs_info = root->fs_info;
  2596. struct extent_buffer *b;
  2597. int slot;
  2598. int ret;
  2599. int err;
  2600. int level;
  2601. int lowest_unlock = 1;
  2602. u8 lowest_level = 0;
  2603. int prev_cmp = -1;
  2604. lowest_level = p->lowest_level;
  2605. WARN_ON(p->nodes[0] != NULL);
  2606. if (p->search_commit_root) {
  2607. BUG_ON(time_seq);
  2608. return btrfs_search_slot(NULL, root, key, p, 0, 0);
  2609. }
  2610. again:
  2611. b = get_old_root(root, time_seq);
  2612. level = btrfs_header_level(b);
  2613. p->locks[level] = BTRFS_READ_LOCK;
  2614. while (b) {
  2615. level = btrfs_header_level(b);
  2616. p->nodes[level] = b;
  2617. btrfs_clear_path_blocking(p, NULL, 0);
  2618. /*
  2619. * we have a lock on b and as long as we aren't changing
  2620. * the tree, there is no way to for the items in b to change.
  2621. * It is safe to drop the lock on our parent before we
  2622. * go through the expensive btree search on b.
  2623. */
  2624. btrfs_unlock_up_safe(p, level + 1);
  2625. /*
  2626. * Since we can unwind ebs we want to do a real search every
  2627. * time.
  2628. */
  2629. prev_cmp = -1;
  2630. ret = key_search(b, key, level, &prev_cmp, &slot);
  2631. if (level != 0) {
  2632. int dec = 0;
  2633. if (ret && slot > 0) {
  2634. dec = 1;
  2635. slot -= 1;
  2636. }
  2637. p->slots[level] = slot;
  2638. unlock_up(p, level, lowest_unlock, 0, NULL);
  2639. if (level == lowest_level) {
  2640. if (dec)
  2641. p->slots[level]++;
  2642. goto done;
  2643. }
  2644. err = read_block_for_search(root, p, &b, level,
  2645. slot, key);
  2646. if (err == -EAGAIN)
  2647. goto again;
  2648. if (err) {
  2649. ret = err;
  2650. goto done;
  2651. }
  2652. level = btrfs_header_level(b);
  2653. err = btrfs_tree_read_lock_atomic(b);
  2654. if (!err) {
  2655. btrfs_set_path_blocking(p);
  2656. btrfs_tree_read_lock(b);
  2657. btrfs_clear_path_blocking(p, b,
  2658. BTRFS_READ_LOCK);
  2659. }
  2660. b = tree_mod_log_rewind(fs_info, p, b, time_seq);
  2661. if (!b) {
  2662. ret = -ENOMEM;
  2663. goto done;
  2664. }
  2665. p->locks[level] = BTRFS_READ_LOCK;
  2666. p->nodes[level] = b;
  2667. } else {
  2668. p->slots[level] = slot;
  2669. unlock_up(p, level, lowest_unlock, 0, NULL);
  2670. goto done;
  2671. }
  2672. }
  2673. ret = 1;
  2674. done:
  2675. if (!p->leave_spinning)
  2676. btrfs_set_path_blocking(p);
  2677. if (ret < 0)
  2678. btrfs_release_path(p);
  2679. return ret;
  2680. }
  2681. /*
  2682. * helper to use instead of search slot if no exact match is needed but
  2683. * instead the next or previous item should be returned.
  2684. * When find_higher is true, the next higher item is returned, the next lower
  2685. * otherwise.
  2686. * When return_any and find_higher are both true, and no higher item is found,
  2687. * return the next lower instead.
  2688. * When return_any is true and find_higher is false, and no lower item is found,
  2689. * return the next higher instead.
  2690. * It returns 0 if any item is found, 1 if none is found (tree empty), and
  2691. * < 0 on error
  2692. */
  2693. int btrfs_search_slot_for_read(struct btrfs_root *root,
  2694. const struct btrfs_key *key,
  2695. struct btrfs_path *p, int find_higher,
  2696. int return_any)
  2697. {
  2698. int ret;
  2699. struct extent_buffer *leaf;
  2700. again:
  2701. ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
  2702. if (ret <= 0)
  2703. return ret;
  2704. /*
  2705. * a return value of 1 means the path is at the position where the
  2706. * item should be inserted. Normally this is the next bigger item,
  2707. * but in case the previous item is the last in a leaf, path points
  2708. * to the first free slot in the previous leaf, i.e. at an invalid
  2709. * item.
  2710. */
  2711. leaf = p->nodes[0];
  2712. if (find_higher) {
  2713. if (p->slots[0] >= btrfs_header_nritems(leaf)) {
  2714. ret = btrfs_next_leaf(root, p);
  2715. if (ret <= 0)
  2716. return ret;
  2717. if (!return_any)
  2718. return 1;
  2719. /*
  2720. * no higher item found, return the next
  2721. * lower instead
  2722. */
  2723. return_any = 0;
  2724. find_higher = 0;
  2725. btrfs_release_path(p);
  2726. goto again;
  2727. }
  2728. } else {
  2729. if (p->slots[0] == 0) {
  2730. ret = btrfs_prev_leaf(root, p);
  2731. if (ret < 0)
  2732. return ret;
  2733. if (!ret) {
  2734. leaf = p->nodes[0];
  2735. if (p->slots[0] == btrfs_header_nritems(leaf))
  2736. p->slots[0]--;
  2737. return 0;
  2738. }
  2739. if (!return_any)
  2740. return 1;
  2741. /*
  2742. * no lower item found, return the next
  2743. * higher instead
  2744. */
  2745. return_any = 0;
  2746. find_higher = 1;
  2747. btrfs_release_path(p);
  2748. goto again;
  2749. } else {
  2750. --p->slots[0];
  2751. }
  2752. }
  2753. return 0;
  2754. }
  2755. /*
  2756. * adjust the pointers going up the tree, starting at level
  2757. * making sure the right key of each node is points to 'key'.
  2758. * This is used after shifting pointers to the left, so it stops
  2759. * fixing up pointers when a given leaf/node is not in slot 0 of the
  2760. * higher levels
  2761. *
  2762. */
  2763. static void fixup_low_keys(struct btrfs_fs_info *fs_info,
  2764. struct btrfs_path *path,
  2765. struct btrfs_disk_key *key, int level)
  2766. {
  2767. int i;
  2768. struct extent_buffer *t;
  2769. int ret;
  2770. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2771. int tslot = path->slots[i];
  2772. if (!path->nodes[i])
  2773. break;
  2774. t = path->nodes[i];
  2775. ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
  2776. GFP_ATOMIC);
  2777. BUG_ON(ret < 0);
  2778. btrfs_set_node_key(t, key, tslot);
  2779. btrfs_mark_buffer_dirty(path->nodes[i]);
  2780. if (tslot != 0)
  2781. break;
  2782. }
  2783. }
  2784. /*
  2785. * update item key.
  2786. *
  2787. * This function isn't completely safe. It's the caller's responsibility
  2788. * that the new key won't break the order
  2789. */
  2790. void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
  2791. struct btrfs_path *path,
  2792. const struct btrfs_key *new_key)
  2793. {
  2794. struct btrfs_disk_key disk_key;
  2795. struct extent_buffer *eb;
  2796. int slot;
  2797. eb = path->nodes[0];
  2798. slot = path->slots[0];
  2799. if (slot > 0) {
  2800. btrfs_item_key(eb, &disk_key, slot - 1);
  2801. BUG_ON(comp_keys(&disk_key, new_key) >= 0);
  2802. }
  2803. if (slot < btrfs_header_nritems(eb) - 1) {
  2804. btrfs_item_key(eb, &disk_key, slot + 1);
  2805. BUG_ON(comp_keys(&disk_key, new_key) <= 0);
  2806. }
  2807. btrfs_cpu_key_to_disk(&disk_key, new_key);
  2808. btrfs_set_item_key(eb, &disk_key, slot);
  2809. btrfs_mark_buffer_dirty(eb);
  2810. if (slot == 0)
  2811. fixup_low_keys(fs_info, path, &disk_key, 1);
  2812. }
  2813. /*
  2814. * try to push data from one node into the next node left in the
  2815. * tree.
  2816. *
  2817. * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
  2818. * error, and > 0 if there was no room in the left hand block.
  2819. */
  2820. static int push_node_left(struct btrfs_trans_handle *trans,
  2821. struct btrfs_fs_info *fs_info,
  2822. struct extent_buffer *dst,
  2823. struct extent_buffer *src, int empty)
  2824. {
  2825. int push_items = 0;
  2826. int src_nritems;
  2827. int dst_nritems;
  2828. int ret = 0;
  2829. src_nritems = btrfs_header_nritems(src);
  2830. dst_nritems = btrfs_header_nritems(dst);
  2831. push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
  2832. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2833. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2834. if (!empty && src_nritems <= 8)
  2835. return 1;
  2836. if (push_items <= 0)
  2837. return 1;
  2838. if (empty) {
  2839. push_items = min(src_nritems, push_items);
  2840. if (push_items < src_nritems) {
  2841. /* leave at least 8 pointers in the node if
  2842. * we aren't going to empty it
  2843. */
  2844. if (src_nritems - push_items < 8) {
  2845. if (push_items <= 8)
  2846. return 1;
  2847. push_items -= 8;
  2848. }
  2849. }
  2850. } else
  2851. push_items = min(src_nritems - 8, push_items);
  2852. ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
  2853. push_items);
  2854. if (ret) {
  2855. btrfs_abort_transaction(trans, ret);
  2856. return ret;
  2857. }
  2858. copy_extent_buffer(dst, src,
  2859. btrfs_node_key_ptr_offset(dst_nritems),
  2860. btrfs_node_key_ptr_offset(0),
  2861. push_items * sizeof(struct btrfs_key_ptr));
  2862. if (push_items < src_nritems) {
  2863. /*
  2864. * Don't call tree_mod_log_insert_move here, key removal was
  2865. * already fully logged by tree_mod_log_eb_copy above.
  2866. */
  2867. memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
  2868. btrfs_node_key_ptr_offset(push_items),
  2869. (src_nritems - push_items) *
  2870. sizeof(struct btrfs_key_ptr));
  2871. }
  2872. btrfs_set_header_nritems(src, src_nritems - push_items);
  2873. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2874. btrfs_mark_buffer_dirty(src);
  2875. btrfs_mark_buffer_dirty(dst);
  2876. return ret;
  2877. }
  2878. /*
  2879. * try to push data from one node into the next node right in the
  2880. * tree.
  2881. *
  2882. * returns 0 if some ptrs were pushed, < 0 if there was some horrible
  2883. * error, and > 0 if there was no room in the right hand block.
  2884. *
  2885. * this will only push up to 1/2 the contents of the left node over
  2886. */
  2887. static int balance_node_right(struct btrfs_trans_handle *trans,
  2888. struct btrfs_fs_info *fs_info,
  2889. struct extent_buffer *dst,
  2890. struct extent_buffer *src)
  2891. {
  2892. int push_items = 0;
  2893. int max_push;
  2894. int src_nritems;
  2895. int dst_nritems;
  2896. int ret = 0;
  2897. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2898. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2899. src_nritems = btrfs_header_nritems(src);
  2900. dst_nritems = btrfs_header_nritems(dst);
  2901. push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
  2902. if (push_items <= 0)
  2903. return 1;
  2904. if (src_nritems < 4)
  2905. return 1;
  2906. max_push = src_nritems / 2 + 1;
  2907. /* don't try to empty the node */
  2908. if (max_push >= src_nritems)
  2909. return 1;
  2910. if (max_push < push_items)
  2911. push_items = max_push;
  2912. ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
  2913. BUG_ON(ret < 0);
  2914. memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
  2915. btrfs_node_key_ptr_offset(0),
  2916. (dst_nritems) *
  2917. sizeof(struct btrfs_key_ptr));
  2918. ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
  2919. src_nritems - push_items, push_items);
  2920. if (ret) {
  2921. btrfs_abort_transaction(trans, ret);
  2922. return ret;
  2923. }
  2924. copy_extent_buffer(dst, src,
  2925. btrfs_node_key_ptr_offset(0),
  2926. btrfs_node_key_ptr_offset(src_nritems - push_items),
  2927. push_items * sizeof(struct btrfs_key_ptr));
  2928. btrfs_set_header_nritems(src, src_nritems - push_items);
  2929. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2930. btrfs_mark_buffer_dirty(src);
  2931. btrfs_mark_buffer_dirty(dst);
  2932. return ret;
  2933. }
  2934. /*
  2935. * helper function to insert a new root level in the tree.
  2936. * A new node is allocated, and a single item is inserted to
  2937. * point to the existing root
  2938. *
  2939. * returns zero on success or < 0 on failure.
  2940. */
  2941. static noinline int insert_new_root(struct btrfs_trans_handle *trans,
  2942. struct btrfs_root *root,
  2943. struct btrfs_path *path, int level)
  2944. {
  2945. struct btrfs_fs_info *fs_info = root->fs_info;
  2946. u64 lower_gen;
  2947. struct extent_buffer *lower;
  2948. struct extent_buffer *c;
  2949. struct extent_buffer *old;
  2950. struct btrfs_disk_key lower_key;
  2951. int ret;
  2952. BUG_ON(path->nodes[level]);
  2953. BUG_ON(path->nodes[level-1] != root->node);
  2954. lower = path->nodes[level-1];
  2955. if (level == 1)
  2956. btrfs_item_key(lower, &lower_key, 0);
  2957. else
  2958. btrfs_node_key(lower, &lower_key, 0);
  2959. c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  2960. &lower_key, level, root->node->start, 0);
  2961. if (IS_ERR(c))
  2962. return PTR_ERR(c);
  2963. root_add_used(root, fs_info->nodesize);
  2964. memzero_extent_buffer(c, 0, sizeof(struct btrfs_header));
  2965. btrfs_set_header_nritems(c, 1);
  2966. btrfs_set_header_level(c, level);
  2967. btrfs_set_header_bytenr(c, c->start);
  2968. btrfs_set_header_generation(c, trans->transid);
  2969. btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
  2970. btrfs_set_header_owner(c, root->root_key.objectid);
  2971. write_extent_buffer_fsid(c, fs_info->fsid);
  2972. write_extent_buffer_chunk_tree_uuid(c, fs_info->chunk_tree_uuid);
  2973. btrfs_set_node_key(c, &lower_key, 0);
  2974. btrfs_set_node_blockptr(c, 0, lower->start);
  2975. lower_gen = btrfs_header_generation(lower);
  2976. WARN_ON(lower_gen != trans->transid);
  2977. btrfs_set_node_ptr_generation(c, 0, lower_gen);
  2978. btrfs_mark_buffer_dirty(c);
  2979. old = root->node;
  2980. ret = tree_mod_log_insert_root(root->node, c, 0);
  2981. BUG_ON(ret < 0);
  2982. rcu_assign_pointer(root->node, c);
  2983. /* the super has an extra ref to root->node */
  2984. free_extent_buffer(old);
  2985. add_root_to_dirty_list(root);
  2986. extent_buffer_get(c);
  2987. path->nodes[level] = c;
  2988. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  2989. path->slots[level] = 0;
  2990. return 0;
  2991. }
  2992. /*
  2993. * worker function to insert a single pointer in a node.
  2994. * the node should have enough room for the pointer already
  2995. *
  2996. * slot and level indicate where you want the key to go, and
  2997. * blocknr is the block the key points to.
  2998. */
  2999. static void insert_ptr(struct btrfs_trans_handle *trans,
  3000. struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  3001. struct btrfs_disk_key *key, u64 bytenr,
  3002. int slot, int level)
  3003. {
  3004. struct extent_buffer *lower;
  3005. int nritems;
  3006. int ret;
  3007. BUG_ON(!path->nodes[level]);
  3008. btrfs_assert_tree_locked(path->nodes[level]);
  3009. lower = path->nodes[level];
  3010. nritems = btrfs_header_nritems(lower);
  3011. BUG_ON(slot > nritems);
  3012. BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  3013. if (slot != nritems) {
  3014. if (level) {
  3015. ret = tree_mod_log_insert_move(lower, slot + 1, slot,
  3016. nritems - slot);
  3017. BUG_ON(ret < 0);
  3018. }
  3019. memmove_extent_buffer(lower,
  3020. btrfs_node_key_ptr_offset(slot + 1),
  3021. btrfs_node_key_ptr_offset(slot),
  3022. (nritems - slot) * sizeof(struct btrfs_key_ptr));
  3023. }
  3024. if (level) {
  3025. ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
  3026. GFP_NOFS);
  3027. BUG_ON(ret < 0);
  3028. }
  3029. btrfs_set_node_key(lower, key, slot);
  3030. btrfs_set_node_blockptr(lower, slot, bytenr);
  3031. WARN_ON(trans->transid == 0);
  3032. btrfs_set_node_ptr_generation(lower, slot, trans->transid);
  3033. btrfs_set_header_nritems(lower, nritems + 1);
  3034. btrfs_mark_buffer_dirty(lower);
  3035. }
  3036. /*
  3037. * split the node at the specified level in path in two.
  3038. * The path is corrected to point to the appropriate node after the split
  3039. *
  3040. * Before splitting this tries to make some room in the node by pushing
  3041. * left and right, if either one works, it returns right away.
  3042. *
  3043. * returns 0 on success and < 0 on failure
  3044. */
  3045. static noinline int split_node(struct btrfs_trans_handle *trans,
  3046. struct btrfs_root *root,
  3047. struct btrfs_path *path, int level)
  3048. {
  3049. struct btrfs_fs_info *fs_info = root->fs_info;
  3050. struct extent_buffer *c;
  3051. struct extent_buffer *split;
  3052. struct btrfs_disk_key disk_key;
  3053. int mid;
  3054. int ret;
  3055. u32 c_nritems;
  3056. c = path->nodes[level];
  3057. WARN_ON(btrfs_header_generation(c) != trans->transid);
  3058. if (c == root->node) {
  3059. /*
  3060. * trying to split the root, lets make a new one
  3061. *
  3062. * tree mod log: We don't log_removal old root in
  3063. * insert_new_root, because that root buffer will be kept as a
  3064. * normal node. We are going to log removal of half of the
  3065. * elements below with tree_mod_log_eb_copy. We're holding a
  3066. * tree lock on the buffer, which is why we cannot race with
  3067. * other tree_mod_log users.
  3068. */
  3069. ret = insert_new_root(trans, root, path, level + 1);
  3070. if (ret)
  3071. return ret;
  3072. } else {
  3073. ret = push_nodes_for_insert(trans, root, path, level);
  3074. c = path->nodes[level];
  3075. if (!ret && btrfs_header_nritems(c) <
  3076. BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
  3077. return 0;
  3078. if (ret < 0)
  3079. return ret;
  3080. }
  3081. c_nritems = btrfs_header_nritems(c);
  3082. mid = (c_nritems + 1) / 2;
  3083. btrfs_node_key(c, &disk_key, mid);
  3084. split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3085. &disk_key, level, c->start, 0);
  3086. if (IS_ERR(split))
  3087. return PTR_ERR(split);
  3088. root_add_used(root, fs_info->nodesize);
  3089. memzero_extent_buffer(split, 0, sizeof(struct btrfs_header));
  3090. btrfs_set_header_level(split, btrfs_header_level(c));
  3091. btrfs_set_header_bytenr(split, split->start);
  3092. btrfs_set_header_generation(split, trans->transid);
  3093. btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
  3094. btrfs_set_header_owner(split, root->root_key.objectid);
  3095. write_extent_buffer_fsid(split, fs_info->fsid);
  3096. write_extent_buffer_chunk_tree_uuid(split, fs_info->chunk_tree_uuid);
  3097. ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
  3098. if (ret) {
  3099. btrfs_abort_transaction(trans, ret);
  3100. return ret;
  3101. }
  3102. copy_extent_buffer(split, c,
  3103. btrfs_node_key_ptr_offset(0),
  3104. btrfs_node_key_ptr_offset(mid),
  3105. (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
  3106. btrfs_set_header_nritems(split, c_nritems - mid);
  3107. btrfs_set_header_nritems(c, mid);
  3108. ret = 0;
  3109. btrfs_mark_buffer_dirty(c);
  3110. btrfs_mark_buffer_dirty(split);
  3111. insert_ptr(trans, fs_info, path, &disk_key, split->start,
  3112. path->slots[level + 1] + 1, level + 1);
  3113. if (path->slots[level] >= mid) {
  3114. path->slots[level] -= mid;
  3115. btrfs_tree_unlock(c);
  3116. free_extent_buffer(c);
  3117. path->nodes[level] = split;
  3118. path->slots[level + 1] += 1;
  3119. } else {
  3120. btrfs_tree_unlock(split);
  3121. free_extent_buffer(split);
  3122. }
  3123. return ret;
  3124. }
  3125. /*
  3126. * how many bytes are required to store the items in a leaf. start
  3127. * and nr indicate which items in the leaf to check. This totals up the
  3128. * space used both by the item structs and the item data
  3129. */
  3130. static int leaf_space_used(struct extent_buffer *l, int start, int nr)
  3131. {
  3132. struct btrfs_item *start_item;
  3133. struct btrfs_item *end_item;
  3134. struct btrfs_map_token token;
  3135. int data_len;
  3136. int nritems = btrfs_header_nritems(l);
  3137. int end = min(nritems, start + nr) - 1;
  3138. if (!nr)
  3139. return 0;
  3140. btrfs_init_map_token(&token);
  3141. start_item = btrfs_item_nr(start);
  3142. end_item = btrfs_item_nr(end);
  3143. data_len = btrfs_token_item_offset(l, start_item, &token) +
  3144. btrfs_token_item_size(l, start_item, &token);
  3145. data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
  3146. data_len += sizeof(struct btrfs_item) * nr;
  3147. WARN_ON(data_len < 0);
  3148. return data_len;
  3149. }
  3150. /*
  3151. * The space between the end of the leaf items and
  3152. * the start of the leaf data. IOW, how much room
  3153. * the leaf has left for both items and data
  3154. */
  3155. noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
  3156. struct extent_buffer *leaf)
  3157. {
  3158. int nritems = btrfs_header_nritems(leaf);
  3159. int ret;
  3160. ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
  3161. if (ret < 0) {
  3162. btrfs_crit(fs_info,
  3163. "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
  3164. ret,
  3165. (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
  3166. leaf_space_used(leaf, 0, nritems), nritems);
  3167. }
  3168. return ret;
  3169. }
  3170. /*
  3171. * min slot controls the lowest index we're willing to push to the
  3172. * right. We'll push up to and including min_slot, but no lower
  3173. */
  3174. static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info,
  3175. struct btrfs_path *path,
  3176. int data_size, int empty,
  3177. struct extent_buffer *right,
  3178. int free_space, u32 left_nritems,
  3179. u32 min_slot)
  3180. {
  3181. struct extent_buffer *left = path->nodes[0];
  3182. struct extent_buffer *upper = path->nodes[1];
  3183. struct btrfs_map_token token;
  3184. struct btrfs_disk_key disk_key;
  3185. int slot;
  3186. u32 i;
  3187. int push_space = 0;
  3188. int push_items = 0;
  3189. struct btrfs_item *item;
  3190. u32 nr;
  3191. u32 right_nritems;
  3192. u32 data_end;
  3193. u32 this_item_size;
  3194. btrfs_init_map_token(&token);
  3195. if (empty)
  3196. nr = 0;
  3197. else
  3198. nr = max_t(u32, 1, min_slot);
  3199. if (path->slots[0] >= left_nritems)
  3200. push_space += data_size;
  3201. slot = path->slots[1];
  3202. i = left_nritems - 1;
  3203. while (i >= nr) {
  3204. item = btrfs_item_nr(i);
  3205. if (!empty && push_items > 0) {
  3206. if (path->slots[0] > i)
  3207. break;
  3208. if (path->slots[0] == i) {
  3209. int space = btrfs_leaf_free_space(fs_info, left);
  3210. if (space + push_space * 2 > free_space)
  3211. break;
  3212. }
  3213. }
  3214. if (path->slots[0] == i)
  3215. push_space += data_size;
  3216. this_item_size = btrfs_item_size(left, item);
  3217. if (this_item_size + sizeof(*item) + push_space > free_space)
  3218. break;
  3219. push_items++;
  3220. push_space += this_item_size + sizeof(*item);
  3221. if (i == 0)
  3222. break;
  3223. i--;
  3224. }
  3225. if (push_items == 0)
  3226. goto out_unlock;
  3227. WARN_ON(!empty && push_items == left_nritems);
  3228. /* push left to right */
  3229. right_nritems = btrfs_header_nritems(right);
  3230. push_space = btrfs_item_end_nr(left, left_nritems - push_items);
  3231. push_space -= leaf_data_end(fs_info, left);
  3232. /* make room in the right data area */
  3233. data_end = leaf_data_end(fs_info, right);
  3234. memmove_extent_buffer(right,
  3235. BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
  3236. BTRFS_LEAF_DATA_OFFSET + data_end,
  3237. BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
  3238. /* copy from the left data area */
  3239. copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
  3240. BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
  3241. BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left),
  3242. push_space);
  3243. memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
  3244. btrfs_item_nr_offset(0),
  3245. right_nritems * sizeof(struct btrfs_item));
  3246. /* copy the items from left to right */
  3247. copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
  3248. btrfs_item_nr_offset(left_nritems - push_items),
  3249. push_items * sizeof(struct btrfs_item));
  3250. /* update the item pointers */
  3251. right_nritems += push_items;
  3252. btrfs_set_header_nritems(right, right_nritems);
  3253. push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
  3254. for (i = 0; i < right_nritems; i++) {
  3255. item = btrfs_item_nr(i);
  3256. push_space -= btrfs_token_item_size(right, item, &token);
  3257. btrfs_set_token_item_offset(right, item, push_space, &token);
  3258. }
  3259. left_nritems -= push_items;
  3260. btrfs_set_header_nritems(left, left_nritems);
  3261. if (left_nritems)
  3262. btrfs_mark_buffer_dirty(left);
  3263. else
  3264. clean_tree_block(fs_info, left);
  3265. btrfs_mark_buffer_dirty(right);
  3266. btrfs_item_key(right, &disk_key, 0);
  3267. btrfs_set_node_key(upper, &disk_key, slot + 1);
  3268. btrfs_mark_buffer_dirty(upper);
  3269. /* then fixup the leaf pointer in the path */
  3270. if (path->slots[0] >= left_nritems) {
  3271. path->slots[0] -= left_nritems;
  3272. if (btrfs_header_nritems(path->nodes[0]) == 0)
  3273. clean_tree_block(fs_info, path->nodes[0]);
  3274. btrfs_tree_unlock(path->nodes[0]);
  3275. free_extent_buffer(path->nodes[0]);
  3276. path->nodes[0] = right;
  3277. path->slots[1] += 1;
  3278. } else {
  3279. btrfs_tree_unlock(right);
  3280. free_extent_buffer(right);
  3281. }
  3282. return 0;
  3283. out_unlock:
  3284. btrfs_tree_unlock(right);
  3285. free_extent_buffer(right);
  3286. return 1;
  3287. }
  3288. /*
  3289. * push some data in the path leaf to the right, trying to free up at
  3290. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3291. *
  3292. * returns 1 if the push failed because the other node didn't have enough
  3293. * room, 0 if everything worked out and < 0 if there were major errors.
  3294. *
  3295. * this will push starting from min_slot to the end of the leaf. It won't
  3296. * push any slot lower than min_slot
  3297. */
  3298. static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
  3299. *root, struct btrfs_path *path,
  3300. int min_data_size, int data_size,
  3301. int empty, u32 min_slot)
  3302. {
  3303. struct btrfs_fs_info *fs_info = root->fs_info;
  3304. struct extent_buffer *left = path->nodes[0];
  3305. struct extent_buffer *right;
  3306. struct extent_buffer *upper;
  3307. int slot;
  3308. int free_space;
  3309. u32 left_nritems;
  3310. int ret;
  3311. if (!path->nodes[1])
  3312. return 1;
  3313. slot = path->slots[1];
  3314. upper = path->nodes[1];
  3315. if (slot >= btrfs_header_nritems(upper) - 1)
  3316. return 1;
  3317. btrfs_assert_tree_locked(path->nodes[1]);
  3318. right = read_node_slot(fs_info, upper, slot + 1);
  3319. /*
  3320. * slot + 1 is not valid or we fail to read the right node,
  3321. * no big deal, just return.
  3322. */
  3323. if (IS_ERR(right))
  3324. return 1;
  3325. btrfs_tree_lock(right);
  3326. btrfs_set_lock_blocking(right);
  3327. free_space = btrfs_leaf_free_space(fs_info, right);
  3328. if (free_space < data_size)
  3329. goto out_unlock;
  3330. /* cow and double check */
  3331. ret = btrfs_cow_block(trans, root, right, upper,
  3332. slot + 1, &right);
  3333. if (ret)
  3334. goto out_unlock;
  3335. free_space = btrfs_leaf_free_space(fs_info, right);
  3336. if (free_space < data_size)
  3337. goto out_unlock;
  3338. left_nritems = btrfs_header_nritems(left);
  3339. if (left_nritems == 0)
  3340. goto out_unlock;
  3341. if (path->slots[0] == left_nritems && !empty) {
  3342. /* Key greater than all keys in the leaf, right neighbor has
  3343. * enough room for it and we're not emptying our leaf to delete
  3344. * it, therefore use right neighbor to insert the new item and
  3345. * no need to touch/dirty our left leaft. */
  3346. btrfs_tree_unlock(left);
  3347. free_extent_buffer(left);
  3348. path->nodes[0] = right;
  3349. path->slots[0] = 0;
  3350. path->slots[1]++;
  3351. return 0;
  3352. }
  3353. return __push_leaf_right(fs_info, path, min_data_size, empty,
  3354. right, free_space, left_nritems, min_slot);
  3355. out_unlock:
  3356. btrfs_tree_unlock(right);
  3357. free_extent_buffer(right);
  3358. return 1;
  3359. }
  3360. /*
  3361. * push some data in the path leaf to the left, trying to free up at
  3362. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3363. *
  3364. * max_slot can put a limit on how far into the leaf we'll push items. The
  3365. * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
  3366. * items
  3367. */
  3368. static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info,
  3369. struct btrfs_path *path, int data_size,
  3370. int empty, struct extent_buffer *left,
  3371. int free_space, u32 right_nritems,
  3372. u32 max_slot)
  3373. {
  3374. struct btrfs_disk_key disk_key;
  3375. struct extent_buffer *right = path->nodes[0];
  3376. int i;
  3377. int push_space = 0;
  3378. int push_items = 0;
  3379. struct btrfs_item *item;
  3380. u32 old_left_nritems;
  3381. u32 nr;
  3382. int ret = 0;
  3383. u32 this_item_size;
  3384. u32 old_left_item_size;
  3385. struct btrfs_map_token token;
  3386. btrfs_init_map_token(&token);
  3387. if (empty)
  3388. nr = min(right_nritems, max_slot);
  3389. else
  3390. nr = min(right_nritems - 1, max_slot);
  3391. for (i = 0; i < nr; i++) {
  3392. item = btrfs_item_nr(i);
  3393. if (!empty && push_items > 0) {
  3394. if (path->slots[0] < i)
  3395. break;
  3396. if (path->slots[0] == i) {
  3397. int space = btrfs_leaf_free_space(fs_info, right);
  3398. if (space + push_space * 2 > free_space)
  3399. break;
  3400. }
  3401. }
  3402. if (path->slots[0] == i)
  3403. push_space += data_size;
  3404. this_item_size = btrfs_item_size(right, item);
  3405. if (this_item_size + sizeof(*item) + push_space > free_space)
  3406. break;
  3407. push_items++;
  3408. push_space += this_item_size + sizeof(*item);
  3409. }
  3410. if (push_items == 0) {
  3411. ret = 1;
  3412. goto out;
  3413. }
  3414. WARN_ON(!empty && push_items == btrfs_header_nritems(right));
  3415. /* push data from right to left */
  3416. copy_extent_buffer(left, right,
  3417. btrfs_item_nr_offset(btrfs_header_nritems(left)),
  3418. btrfs_item_nr_offset(0),
  3419. push_items * sizeof(struct btrfs_item));
  3420. push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
  3421. btrfs_item_offset_nr(right, push_items - 1);
  3422. copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
  3423. leaf_data_end(fs_info, left) - push_space,
  3424. BTRFS_LEAF_DATA_OFFSET +
  3425. btrfs_item_offset_nr(right, push_items - 1),
  3426. push_space);
  3427. old_left_nritems = btrfs_header_nritems(left);
  3428. BUG_ON(old_left_nritems <= 0);
  3429. old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
  3430. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  3431. u32 ioff;
  3432. item = btrfs_item_nr(i);
  3433. ioff = btrfs_token_item_offset(left, item, &token);
  3434. btrfs_set_token_item_offset(left, item,
  3435. ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
  3436. &token);
  3437. }
  3438. btrfs_set_header_nritems(left, old_left_nritems + push_items);
  3439. /* fixup right node */
  3440. if (push_items > right_nritems)
  3441. WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
  3442. right_nritems);
  3443. if (push_items < right_nritems) {
  3444. push_space = btrfs_item_offset_nr(right, push_items - 1) -
  3445. leaf_data_end(fs_info, right);
  3446. memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
  3447. BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
  3448. BTRFS_LEAF_DATA_OFFSET +
  3449. leaf_data_end(fs_info, right), push_space);
  3450. memmove_extent_buffer(right, btrfs_item_nr_offset(0),
  3451. btrfs_item_nr_offset(push_items),
  3452. (btrfs_header_nritems(right) - push_items) *
  3453. sizeof(struct btrfs_item));
  3454. }
  3455. right_nritems -= push_items;
  3456. btrfs_set_header_nritems(right, right_nritems);
  3457. push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
  3458. for (i = 0; i < right_nritems; i++) {
  3459. item = btrfs_item_nr(i);
  3460. push_space = push_space - btrfs_token_item_size(right,
  3461. item, &token);
  3462. btrfs_set_token_item_offset(right, item, push_space, &token);
  3463. }
  3464. btrfs_mark_buffer_dirty(left);
  3465. if (right_nritems)
  3466. btrfs_mark_buffer_dirty(right);
  3467. else
  3468. clean_tree_block(fs_info, right);
  3469. btrfs_item_key(right, &disk_key, 0);
  3470. fixup_low_keys(fs_info, path, &disk_key, 1);
  3471. /* then fixup the leaf pointer in the path */
  3472. if (path->slots[0] < push_items) {
  3473. path->slots[0] += old_left_nritems;
  3474. btrfs_tree_unlock(path->nodes[0]);
  3475. free_extent_buffer(path->nodes[0]);
  3476. path->nodes[0] = left;
  3477. path->slots[1] -= 1;
  3478. } else {
  3479. btrfs_tree_unlock(left);
  3480. free_extent_buffer(left);
  3481. path->slots[0] -= push_items;
  3482. }
  3483. BUG_ON(path->slots[0] < 0);
  3484. return ret;
  3485. out:
  3486. btrfs_tree_unlock(left);
  3487. free_extent_buffer(left);
  3488. return ret;
  3489. }
  3490. /*
  3491. * push some data in the path leaf to the left, trying to free up at
  3492. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3493. *
  3494. * max_slot can put a limit on how far into the leaf we'll push items. The
  3495. * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
  3496. * items
  3497. */
  3498. static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
  3499. *root, struct btrfs_path *path, int min_data_size,
  3500. int data_size, int empty, u32 max_slot)
  3501. {
  3502. struct btrfs_fs_info *fs_info = root->fs_info;
  3503. struct extent_buffer *right = path->nodes[0];
  3504. struct extent_buffer *left;
  3505. int slot;
  3506. int free_space;
  3507. u32 right_nritems;
  3508. int ret = 0;
  3509. slot = path->slots[1];
  3510. if (slot == 0)
  3511. return 1;
  3512. if (!path->nodes[1])
  3513. return 1;
  3514. right_nritems = btrfs_header_nritems(right);
  3515. if (right_nritems == 0)
  3516. return 1;
  3517. btrfs_assert_tree_locked(path->nodes[1]);
  3518. left = read_node_slot(fs_info, path->nodes[1], slot - 1);
  3519. /*
  3520. * slot - 1 is not valid or we fail to read the left node,
  3521. * no big deal, just return.
  3522. */
  3523. if (IS_ERR(left))
  3524. return 1;
  3525. btrfs_tree_lock(left);
  3526. btrfs_set_lock_blocking(left);
  3527. free_space = btrfs_leaf_free_space(fs_info, left);
  3528. if (free_space < data_size) {
  3529. ret = 1;
  3530. goto out;
  3531. }
  3532. /* cow and double check */
  3533. ret = btrfs_cow_block(trans, root, left,
  3534. path->nodes[1], slot - 1, &left);
  3535. if (ret) {
  3536. /* we hit -ENOSPC, but it isn't fatal here */
  3537. if (ret == -ENOSPC)
  3538. ret = 1;
  3539. goto out;
  3540. }
  3541. free_space = btrfs_leaf_free_space(fs_info, left);
  3542. if (free_space < data_size) {
  3543. ret = 1;
  3544. goto out;
  3545. }
  3546. return __push_leaf_left(fs_info, path, min_data_size,
  3547. empty, left, free_space, right_nritems,
  3548. max_slot);
  3549. out:
  3550. btrfs_tree_unlock(left);
  3551. free_extent_buffer(left);
  3552. return ret;
  3553. }
  3554. /*
  3555. * split the path's leaf in two, making sure there is at least data_size
  3556. * available for the resulting leaf level of the path.
  3557. */
  3558. static noinline void copy_for_split(struct btrfs_trans_handle *trans,
  3559. struct btrfs_fs_info *fs_info,
  3560. struct btrfs_path *path,
  3561. struct extent_buffer *l,
  3562. struct extent_buffer *right,
  3563. int slot, int mid, int nritems)
  3564. {
  3565. int data_copy_size;
  3566. int rt_data_off;
  3567. int i;
  3568. struct btrfs_disk_key disk_key;
  3569. struct btrfs_map_token token;
  3570. btrfs_init_map_token(&token);
  3571. nritems = nritems - mid;
  3572. btrfs_set_header_nritems(right, nritems);
  3573. data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
  3574. copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
  3575. btrfs_item_nr_offset(mid),
  3576. nritems * sizeof(struct btrfs_item));
  3577. copy_extent_buffer(right, l,
  3578. BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
  3579. data_copy_size, BTRFS_LEAF_DATA_OFFSET +
  3580. leaf_data_end(fs_info, l), data_copy_size);
  3581. rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
  3582. for (i = 0; i < nritems; i++) {
  3583. struct btrfs_item *item = btrfs_item_nr(i);
  3584. u32 ioff;
  3585. ioff = btrfs_token_item_offset(right, item, &token);
  3586. btrfs_set_token_item_offset(right, item,
  3587. ioff + rt_data_off, &token);
  3588. }
  3589. btrfs_set_header_nritems(l, mid);
  3590. btrfs_item_key(right, &disk_key, 0);
  3591. insert_ptr(trans, fs_info, path, &disk_key, right->start,
  3592. path->slots[1] + 1, 1);
  3593. btrfs_mark_buffer_dirty(right);
  3594. btrfs_mark_buffer_dirty(l);
  3595. BUG_ON(path->slots[0] != slot);
  3596. if (mid <= slot) {
  3597. btrfs_tree_unlock(path->nodes[0]);
  3598. free_extent_buffer(path->nodes[0]);
  3599. path->nodes[0] = right;
  3600. path->slots[0] -= mid;
  3601. path->slots[1] += 1;
  3602. } else {
  3603. btrfs_tree_unlock(right);
  3604. free_extent_buffer(right);
  3605. }
  3606. BUG_ON(path->slots[0] < 0);
  3607. }
  3608. /*
  3609. * double splits happen when we need to insert a big item in the middle
  3610. * of a leaf. A double split can leave us with 3 mostly empty leaves:
  3611. * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
  3612. * A B C
  3613. *
  3614. * We avoid this by trying to push the items on either side of our target
  3615. * into the adjacent leaves. If all goes well we can avoid the double split
  3616. * completely.
  3617. */
  3618. static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
  3619. struct btrfs_root *root,
  3620. struct btrfs_path *path,
  3621. int data_size)
  3622. {
  3623. struct btrfs_fs_info *fs_info = root->fs_info;
  3624. int ret;
  3625. int progress = 0;
  3626. int slot;
  3627. u32 nritems;
  3628. int space_needed = data_size;
  3629. slot = path->slots[0];
  3630. if (slot < btrfs_header_nritems(path->nodes[0]))
  3631. space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
  3632. /*
  3633. * try to push all the items after our slot into the
  3634. * right leaf
  3635. */
  3636. ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
  3637. if (ret < 0)
  3638. return ret;
  3639. if (ret == 0)
  3640. progress++;
  3641. nritems = btrfs_header_nritems(path->nodes[0]);
  3642. /*
  3643. * our goal is to get our slot at the start or end of a leaf. If
  3644. * we've done so we're done
  3645. */
  3646. if (path->slots[0] == 0 || path->slots[0] == nritems)
  3647. return 0;
  3648. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
  3649. return 0;
  3650. /* try to push all the items before our slot into the next leaf */
  3651. slot = path->slots[0];
  3652. space_needed = data_size;
  3653. if (slot > 0)
  3654. space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
  3655. ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
  3656. if (ret < 0)
  3657. return ret;
  3658. if (ret == 0)
  3659. progress++;
  3660. if (progress)
  3661. return 0;
  3662. return 1;
  3663. }
  3664. /*
  3665. * split the path's leaf in two, making sure there is at least data_size
  3666. * available for the resulting leaf level of the path.
  3667. *
  3668. * returns 0 if all went well and < 0 on failure.
  3669. */
  3670. static noinline int split_leaf(struct btrfs_trans_handle *trans,
  3671. struct btrfs_root *root,
  3672. const struct btrfs_key *ins_key,
  3673. struct btrfs_path *path, int data_size,
  3674. int extend)
  3675. {
  3676. struct btrfs_disk_key disk_key;
  3677. struct extent_buffer *l;
  3678. u32 nritems;
  3679. int mid;
  3680. int slot;
  3681. struct extent_buffer *right;
  3682. struct btrfs_fs_info *fs_info = root->fs_info;
  3683. int ret = 0;
  3684. int wret;
  3685. int split;
  3686. int num_doubles = 0;
  3687. int tried_avoid_double = 0;
  3688. l = path->nodes[0];
  3689. slot = path->slots[0];
  3690. if (extend && data_size + btrfs_item_size_nr(l, slot) +
  3691. sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
  3692. return -EOVERFLOW;
  3693. /* first try to make some room by pushing left and right */
  3694. if (data_size && path->nodes[1]) {
  3695. int space_needed = data_size;
  3696. if (slot < btrfs_header_nritems(l))
  3697. space_needed -= btrfs_leaf_free_space(fs_info, l);
  3698. wret = push_leaf_right(trans, root, path, space_needed,
  3699. space_needed, 0, 0);
  3700. if (wret < 0)
  3701. return wret;
  3702. if (wret) {
  3703. space_needed = data_size;
  3704. if (slot > 0)
  3705. space_needed -= btrfs_leaf_free_space(fs_info,
  3706. l);
  3707. wret = push_leaf_left(trans, root, path, space_needed,
  3708. space_needed, 0, (u32)-1);
  3709. if (wret < 0)
  3710. return wret;
  3711. }
  3712. l = path->nodes[0];
  3713. /* did the pushes work? */
  3714. if (btrfs_leaf_free_space(fs_info, l) >= data_size)
  3715. return 0;
  3716. }
  3717. if (!path->nodes[1]) {
  3718. ret = insert_new_root(trans, root, path, 1);
  3719. if (ret)
  3720. return ret;
  3721. }
  3722. again:
  3723. split = 1;
  3724. l = path->nodes[0];
  3725. slot = path->slots[0];
  3726. nritems = btrfs_header_nritems(l);
  3727. mid = (nritems + 1) / 2;
  3728. if (mid <= slot) {
  3729. if (nritems == 1 ||
  3730. leaf_space_used(l, mid, nritems - mid) + data_size >
  3731. BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3732. if (slot >= nritems) {
  3733. split = 0;
  3734. } else {
  3735. mid = slot;
  3736. if (mid != nritems &&
  3737. leaf_space_used(l, mid, nritems - mid) +
  3738. data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3739. if (data_size && !tried_avoid_double)
  3740. goto push_for_double;
  3741. split = 2;
  3742. }
  3743. }
  3744. }
  3745. } else {
  3746. if (leaf_space_used(l, 0, mid) + data_size >
  3747. BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3748. if (!extend && data_size && slot == 0) {
  3749. split = 0;
  3750. } else if ((extend || !data_size) && slot == 0) {
  3751. mid = 1;
  3752. } else {
  3753. mid = slot;
  3754. if (mid != nritems &&
  3755. leaf_space_used(l, mid, nritems - mid) +
  3756. data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3757. if (data_size && !tried_avoid_double)
  3758. goto push_for_double;
  3759. split = 2;
  3760. }
  3761. }
  3762. }
  3763. }
  3764. if (split == 0)
  3765. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  3766. else
  3767. btrfs_item_key(l, &disk_key, mid);
  3768. right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
  3769. &disk_key, 0, l->start, 0);
  3770. if (IS_ERR(right))
  3771. return PTR_ERR(right);
  3772. root_add_used(root, fs_info->nodesize);
  3773. memzero_extent_buffer(right, 0, sizeof(struct btrfs_header));
  3774. btrfs_set_header_bytenr(right, right->start);
  3775. btrfs_set_header_generation(right, trans->transid);
  3776. btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
  3777. btrfs_set_header_owner(right, root->root_key.objectid);
  3778. btrfs_set_header_level(right, 0);
  3779. write_extent_buffer_fsid(right, fs_info->fsid);
  3780. write_extent_buffer_chunk_tree_uuid(right, fs_info->chunk_tree_uuid);
  3781. if (split == 0) {
  3782. if (mid <= slot) {
  3783. btrfs_set_header_nritems(right, 0);
  3784. insert_ptr(trans, fs_info, path, &disk_key,
  3785. right->start, path->slots[1] + 1, 1);
  3786. btrfs_tree_unlock(path->nodes[0]);
  3787. free_extent_buffer(path->nodes[0]);
  3788. path->nodes[0] = right;
  3789. path->slots[0] = 0;
  3790. path->slots[1] += 1;
  3791. } else {
  3792. btrfs_set_header_nritems(right, 0);
  3793. insert_ptr(trans, fs_info, path, &disk_key,
  3794. right->start, path->slots[1], 1);
  3795. btrfs_tree_unlock(path->nodes[0]);
  3796. free_extent_buffer(path->nodes[0]);
  3797. path->nodes[0] = right;
  3798. path->slots[0] = 0;
  3799. if (path->slots[1] == 0)
  3800. fixup_low_keys(fs_info, path, &disk_key, 1);
  3801. }
  3802. /*
  3803. * We create a new leaf 'right' for the required ins_len and
  3804. * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
  3805. * the content of ins_len to 'right'.
  3806. */
  3807. return ret;
  3808. }
  3809. copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
  3810. if (split == 2) {
  3811. BUG_ON(num_doubles != 0);
  3812. num_doubles++;
  3813. goto again;
  3814. }
  3815. return 0;
  3816. push_for_double:
  3817. push_for_double_split(trans, root, path, data_size);
  3818. tried_avoid_double = 1;
  3819. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
  3820. return 0;
  3821. goto again;
  3822. }
  3823. static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
  3824. struct btrfs_root *root,
  3825. struct btrfs_path *path, int ins_len)
  3826. {
  3827. struct btrfs_fs_info *fs_info = root->fs_info;
  3828. struct btrfs_key key;
  3829. struct extent_buffer *leaf;
  3830. struct btrfs_file_extent_item *fi;
  3831. u64 extent_len = 0;
  3832. u32 item_size;
  3833. int ret;
  3834. leaf = path->nodes[0];
  3835. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3836. BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
  3837. key.type != BTRFS_EXTENT_CSUM_KEY);
  3838. if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
  3839. return 0;
  3840. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3841. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3842. fi = btrfs_item_ptr(leaf, path->slots[0],
  3843. struct btrfs_file_extent_item);
  3844. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  3845. }
  3846. btrfs_release_path(path);
  3847. path->keep_locks = 1;
  3848. path->search_for_split = 1;
  3849. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3850. path->search_for_split = 0;
  3851. if (ret > 0)
  3852. ret = -EAGAIN;
  3853. if (ret < 0)
  3854. goto err;
  3855. ret = -EAGAIN;
  3856. leaf = path->nodes[0];
  3857. /* if our item isn't there, return now */
  3858. if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
  3859. goto err;
  3860. /* the leaf has changed, it now has room. return now */
  3861. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
  3862. goto err;
  3863. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3864. fi = btrfs_item_ptr(leaf, path->slots[0],
  3865. struct btrfs_file_extent_item);
  3866. if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
  3867. goto err;
  3868. }
  3869. btrfs_set_path_blocking(path);
  3870. ret = split_leaf(trans, root, &key, path, ins_len, 1);
  3871. if (ret)
  3872. goto err;
  3873. path->keep_locks = 0;
  3874. btrfs_unlock_up_safe(path, 1);
  3875. return 0;
  3876. err:
  3877. path->keep_locks = 0;
  3878. return ret;
  3879. }
  3880. static noinline int split_item(struct btrfs_fs_info *fs_info,
  3881. struct btrfs_path *path,
  3882. const struct btrfs_key *new_key,
  3883. unsigned long split_offset)
  3884. {
  3885. struct extent_buffer *leaf;
  3886. struct btrfs_item *item;
  3887. struct btrfs_item *new_item;
  3888. int slot;
  3889. char *buf;
  3890. u32 nritems;
  3891. u32 item_size;
  3892. u32 orig_offset;
  3893. struct btrfs_disk_key disk_key;
  3894. leaf = path->nodes[0];
  3895. BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
  3896. btrfs_set_path_blocking(path);
  3897. item = btrfs_item_nr(path->slots[0]);
  3898. orig_offset = btrfs_item_offset(leaf, item);
  3899. item_size = btrfs_item_size(leaf, item);
  3900. buf = kmalloc(item_size, GFP_NOFS);
  3901. if (!buf)
  3902. return -ENOMEM;
  3903. read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
  3904. path->slots[0]), item_size);
  3905. slot = path->slots[0] + 1;
  3906. nritems = btrfs_header_nritems(leaf);
  3907. if (slot != nritems) {
  3908. /* shift the items */
  3909. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
  3910. btrfs_item_nr_offset(slot),
  3911. (nritems - slot) * sizeof(struct btrfs_item));
  3912. }
  3913. btrfs_cpu_key_to_disk(&disk_key, new_key);
  3914. btrfs_set_item_key(leaf, &disk_key, slot);
  3915. new_item = btrfs_item_nr(slot);
  3916. btrfs_set_item_offset(leaf, new_item, orig_offset);
  3917. btrfs_set_item_size(leaf, new_item, item_size - split_offset);
  3918. btrfs_set_item_offset(leaf, item,
  3919. orig_offset + item_size - split_offset);
  3920. btrfs_set_item_size(leaf, item, split_offset);
  3921. btrfs_set_header_nritems(leaf, nritems + 1);
  3922. /* write the data for the start of the original item */
  3923. write_extent_buffer(leaf, buf,
  3924. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3925. split_offset);
  3926. /* write the data for the new item */
  3927. write_extent_buffer(leaf, buf + split_offset,
  3928. btrfs_item_ptr_offset(leaf, slot),
  3929. item_size - split_offset);
  3930. btrfs_mark_buffer_dirty(leaf);
  3931. BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
  3932. kfree(buf);
  3933. return 0;
  3934. }
  3935. /*
  3936. * This function splits a single item into two items,
  3937. * giving 'new_key' to the new item and splitting the
  3938. * old one at split_offset (from the start of the item).
  3939. *
  3940. * The path may be released by this operation. After
  3941. * the split, the path is pointing to the old item. The
  3942. * new item is going to be in the same node as the old one.
  3943. *
  3944. * Note, the item being split must be smaller enough to live alone on
  3945. * a tree block with room for one extra struct btrfs_item
  3946. *
  3947. * This allows us to split the item in place, keeping a lock on the
  3948. * leaf the entire time.
  3949. */
  3950. int btrfs_split_item(struct btrfs_trans_handle *trans,
  3951. struct btrfs_root *root,
  3952. struct btrfs_path *path,
  3953. const struct btrfs_key *new_key,
  3954. unsigned long split_offset)
  3955. {
  3956. int ret;
  3957. ret = setup_leaf_for_split(trans, root, path,
  3958. sizeof(struct btrfs_item));
  3959. if (ret)
  3960. return ret;
  3961. ret = split_item(root->fs_info, path, new_key, split_offset);
  3962. return ret;
  3963. }
  3964. /*
  3965. * This function duplicate a item, giving 'new_key' to the new item.
  3966. * It guarantees both items live in the same tree leaf and the new item
  3967. * is contiguous with the original item.
  3968. *
  3969. * This allows us to split file extent in place, keeping a lock on the
  3970. * leaf the entire time.
  3971. */
  3972. int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
  3973. struct btrfs_root *root,
  3974. struct btrfs_path *path,
  3975. const struct btrfs_key *new_key)
  3976. {
  3977. struct extent_buffer *leaf;
  3978. int ret;
  3979. u32 item_size;
  3980. leaf = path->nodes[0];
  3981. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3982. ret = setup_leaf_for_split(trans, root, path,
  3983. item_size + sizeof(struct btrfs_item));
  3984. if (ret)
  3985. return ret;
  3986. path->slots[0]++;
  3987. setup_items_for_insert(root, path, new_key, &item_size,
  3988. item_size, item_size +
  3989. sizeof(struct btrfs_item), 1);
  3990. leaf = path->nodes[0];
  3991. memcpy_extent_buffer(leaf,
  3992. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3993. btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
  3994. item_size);
  3995. return 0;
  3996. }
  3997. /*
  3998. * make the item pointed to by the path smaller. new_size indicates
  3999. * how small to make it, and from_end tells us if we just chop bytes
  4000. * off the end of the item or if we shift the item to chop bytes off
  4001. * the front.
  4002. */
  4003. void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
  4004. struct btrfs_path *path, u32 new_size, int from_end)
  4005. {
  4006. int slot;
  4007. struct extent_buffer *leaf;
  4008. struct btrfs_item *item;
  4009. u32 nritems;
  4010. unsigned int data_end;
  4011. unsigned int old_data_start;
  4012. unsigned int old_size;
  4013. unsigned int size_diff;
  4014. int i;
  4015. struct btrfs_map_token token;
  4016. btrfs_init_map_token(&token);
  4017. leaf = path->nodes[0];
  4018. slot = path->slots[0];
  4019. old_size = btrfs_item_size_nr(leaf, slot);
  4020. if (old_size == new_size)
  4021. return;
  4022. nritems = btrfs_header_nritems(leaf);
  4023. data_end = leaf_data_end(fs_info, leaf);
  4024. old_data_start = btrfs_item_offset_nr(leaf, slot);
  4025. size_diff = old_size - new_size;
  4026. BUG_ON(slot < 0);
  4027. BUG_ON(slot >= nritems);
  4028. /*
  4029. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4030. */
  4031. /* first correct the data pointers */
  4032. for (i = slot; i < nritems; i++) {
  4033. u32 ioff;
  4034. item = btrfs_item_nr(i);
  4035. ioff = btrfs_token_item_offset(leaf, item, &token);
  4036. btrfs_set_token_item_offset(leaf, item,
  4037. ioff + size_diff, &token);
  4038. }
  4039. /* shift the data */
  4040. if (from_end) {
  4041. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4042. data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
  4043. data_end, old_data_start + new_size - data_end);
  4044. } else {
  4045. struct btrfs_disk_key disk_key;
  4046. u64 offset;
  4047. btrfs_item_key(leaf, &disk_key, slot);
  4048. if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
  4049. unsigned long ptr;
  4050. struct btrfs_file_extent_item *fi;
  4051. fi = btrfs_item_ptr(leaf, slot,
  4052. struct btrfs_file_extent_item);
  4053. fi = (struct btrfs_file_extent_item *)(
  4054. (unsigned long)fi - size_diff);
  4055. if (btrfs_file_extent_type(leaf, fi) ==
  4056. BTRFS_FILE_EXTENT_INLINE) {
  4057. ptr = btrfs_item_ptr_offset(leaf, slot);
  4058. memmove_extent_buffer(leaf, ptr,
  4059. (unsigned long)fi,
  4060. BTRFS_FILE_EXTENT_INLINE_DATA_START);
  4061. }
  4062. }
  4063. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4064. data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
  4065. data_end, old_data_start - data_end);
  4066. offset = btrfs_disk_key_offset(&disk_key);
  4067. btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
  4068. btrfs_set_item_key(leaf, &disk_key, slot);
  4069. if (slot == 0)
  4070. fixup_low_keys(fs_info, path, &disk_key, 1);
  4071. }
  4072. item = btrfs_item_nr(slot);
  4073. btrfs_set_item_size(leaf, item, new_size);
  4074. btrfs_mark_buffer_dirty(leaf);
  4075. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4076. btrfs_print_leaf(leaf);
  4077. BUG();
  4078. }
  4079. }
  4080. /*
  4081. * make the item pointed to by the path bigger, data_size is the added size.
  4082. */
  4083. void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  4084. u32 data_size)
  4085. {
  4086. int slot;
  4087. struct extent_buffer *leaf;
  4088. struct btrfs_item *item;
  4089. u32 nritems;
  4090. unsigned int data_end;
  4091. unsigned int old_data;
  4092. unsigned int old_size;
  4093. int i;
  4094. struct btrfs_map_token token;
  4095. btrfs_init_map_token(&token);
  4096. leaf = path->nodes[0];
  4097. nritems = btrfs_header_nritems(leaf);
  4098. data_end = leaf_data_end(fs_info, leaf);
  4099. if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
  4100. btrfs_print_leaf(leaf);
  4101. BUG();
  4102. }
  4103. slot = path->slots[0];
  4104. old_data = btrfs_item_end_nr(leaf, slot);
  4105. BUG_ON(slot < 0);
  4106. if (slot >= nritems) {
  4107. btrfs_print_leaf(leaf);
  4108. btrfs_crit(fs_info, "slot %d too large, nritems %d",
  4109. slot, nritems);
  4110. BUG_ON(1);
  4111. }
  4112. /*
  4113. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4114. */
  4115. /* first correct the data pointers */
  4116. for (i = slot; i < nritems; i++) {
  4117. u32 ioff;
  4118. item = btrfs_item_nr(i);
  4119. ioff = btrfs_token_item_offset(leaf, item, &token);
  4120. btrfs_set_token_item_offset(leaf, item,
  4121. ioff - data_size, &token);
  4122. }
  4123. /* shift the data */
  4124. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4125. data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
  4126. data_end, old_data - data_end);
  4127. data_end = old_data;
  4128. old_size = btrfs_item_size_nr(leaf, slot);
  4129. item = btrfs_item_nr(slot);
  4130. btrfs_set_item_size(leaf, item, old_size + data_size);
  4131. btrfs_mark_buffer_dirty(leaf);
  4132. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4133. btrfs_print_leaf(leaf);
  4134. BUG();
  4135. }
  4136. }
  4137. /*
  4138. * this is a helper for btrfs_insert_empty_items, the main goal here is
  4139. * to save stack depth by doing the bulk of the work in a function
  4140. * that doesn't call btrfs_search_slot
  4141. */
  4142. void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
  4143. const struct btrfs_key *cpu_key, u32 *data_size,
  4144. u32 total_data, u32 total_size, int nr)
  4145. {
  4146. struct btrfs_fs_info *fs_info = root->fs_info;
  4147. struct btrfs_item *item;
  4148. int i;
  4149. u32 nritems;
  4150. unsigned int data_end;
  4151. struct btrfs_disk_key disk_key;
  4152. struct extent_buffer *leaf;
  4153. int slot;
  4154. struct btrfs_map_token token;
  4155. if (path->slots[0] == 0) {
  4156. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  4157. fixup_low_keys(fs_info, path, &disk_key, 1);
  4158. }
  4159. btrfs_unlock_up_safe(path, 1);
  4160. btrfs_init_map_token(&token);
  4161. leaf = path->nodes[0];
  4162. slot = path->slots[0];
  4163. nritems = btrfs_header_nritems(leaf);
  4164. data_end = leaf_data_end(fs_info, leaf);
  4165. if (btrfs_leaf_free_space(fs_info, leaf) < total_size) {
  4166. btrfs_print_leaf(leaf);
  4167. btrfs_crit(fs_info, "not enough freespace need %u have %d",
  4168. total_size, btrfs_leaf_free_space(fs_info, leaf));
  4169. BUG();
  4170. }
  4171. if (slot != nritems) {
  4172. unsigned int old_data = btrfs_item_end_nr(leaf, slot);
  4173. if (old_data < data_end) {
  4174. btrfs_print_leaf(leaf);
  4175. btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
  4176. slot, old_data, data_end);
  4177. BUG_ON(1);
  4178. }
  4179. /*
  4180. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4181. */
  4182. /* first correct the data pointers */
  4183. for (i = slot; i < nritems; i++) {
  4184. u32 ioff;
  4185. item = btrfs_item_nr(i);
  4186. ioff = btrfs_token_item_offset(leaf, item, &token);
  4187. btrfs_set_token_item_offset(leaf, item,
  4188. ioff - total_data, &token);
  4189. }
  4190. /* shift the items */
  4191. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
  4192. btrfs_item_nr_offset(slot),
  4193. (nritems - slot) * sizeof(struct btrfs_item));
  4194. /* shift the data */
  4195. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4196. data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
  4197. data_end, old_data - data_end);
  4198. data_end = old_data;
  4199. }
  4200. /* setup the item for the new data */
  4201. for (i = 0; i < nr; i++) {
  4202. btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
  4203. btrfs_set_item_key(leaf, &disk_key, slot + i);
  4204. item = btrfs_item_nr(slot + i);
  4205. btrfs_set_token_item_offset(leaf, item,
  4206. data_end - data_size[i], &token);
  4207. data_end -= data_size[i];
  4208. btrfs_set_token_item_size(leaf, item, data_size[i], &token);
  4209. }
  4210. btrfs_set_header_nritems(leaf, nritems + nr);
  4211. btrfs_mark_buffer_dirty(leaf);
  4212. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4213. btrfs_print_leaf(leaf);
  4214. BUG();
  4215. }
  4216. }
  4217. /*
  4218. * Given a key and some data, insert items into the tree.
  4219. * This does all the path init required, making room in the tree if needed.
  4220. */
  4221. int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
  4222. struct btrfs_root *root,
  4223. struct btrfs_path *path,
  4224. const struct btrfs_key *cpu_key, u32 *data_size,
  4225. int nr)
  4226. {
  4227. int ret = 0;
  4228. int slot;
  4229. int i;
  4230. u32 total_size = 0;
  4231. u32 total_data = 0;
  4232. for (i = 0; i < nr; i++)
  4233. total_data += data_size[i];
  4234. total_size = total_data + (nr * sizeof(struct btrfs_item));
  4235. ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
  4236. if (ret == 0)
  4237. return -EEXIST;
  4238. if (ret < 0)
  4239. return ret;
  4240. slot = path->slots[0];
  4241. BUG_ON(slot < 0);
  4242. setup_items_for_insert(root, path, cpu_key, data_size,
  4243. total_data, total_size, nr);
  4244. return 0;
  4245. }
  4246. /*
  4247. * Given a key and some data, insert an item into the tree.
  4248. * This does all the path init required, making room in the tree if needed.
  4249. */
  4250. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4251. const struct btrfs_key *cpu_key, void *data,
  4252. u32 data_size)
  4253. {
  4254. int ret = 0;
  4255. struct btrfs_path *path;
  4256. struct extent_buffer *leaf;
  4257. unsigned long ptr;
  4258. path = btrfs_alloc_path();
  4259. if (!path)
  4260. return -ENOMEM;
  4261. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  4262. if (!ret) {
  4263. leaf = path->nodes[0];
  4264. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  4265. write_extent_buffer(leaf, data, ptr, data_size);
  4266. btrfs_mark_buffer_dirty(leaf);
  4267. }
  4268. btrfs_free_path(path);
  4269. return ret;
  4270. }
  4271. /*
  4272. * delete the pointer from a given node.
  4273. *
  4274. * the tree should have been previously balanced so the deletion does not
  4275. * empty a node.
  4276. */
  4277. static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
  4278. int level, int slot)
  4279. {
  4280. struct btrfs_fs_info *fs_info = root->fs_info;
  4281. struct extent_buffer *parent = path->nodes[level];
  4282. u32 nritems;
  4283. int ret;
  4284. nritems = btrfs_header_nritems(parent);
  4285. if (slot != nritems - 1) {
  4286. if (level) {
  4287. ret = tree_mod_log_insert_move(parent, slot, slot + 1,
  4288. nritems - slot - 1);
  4289. BUG_ON(ret < 0);
  4290. }
  4291. memmove_extent_buffer(parent,
  4292. btrfs_node_key_ptr_offset(slot),
  4293. btrfs_node_key_ptr_offset(slot + 1),
  4294. sizeof(struct btrfs_key_ptr) *
  4295. (nritems - slot - 1));
  4296. } else if (level) {
  4297. ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
  4298. GFP_NOFS);
  4299. BUG_ON(ret < 0);
  4300. }
  4301. nritems--;
  4302. btrfs_set_header_nritems(parent, nritems);
  4303. if (nritems == 0 && parent == root->node) {
  4304. BUG_ON(btrfs_header_level(root->node) != 1);
  4305. /* just turn the root into a leaf and break */
  4306. btrfs_set_header_level(root->node, 0);
  4307. } else if (slot == 0) {
  4308. struct btrfs_disk_key disk_key;
  4309. btrfs_node_key(parent, &disk_key, 0);
  4310. fixup_low_keys(fs_info, path, &disk_key, level + 1);
  4311. }
  4312. btrfs_mark_buffer_dirty(parent);
  4313. }
  4314. /*
  4315. * a helper function to delete the leaf pointed to by path->slots[1] and
  4316. * path->nodes[1].
  4317. *
  4318. * This deletes the pointer in path->nodes[1] and frees the leaf
  4319. * block extent. zero is returned if it all worked out, < 0 otherwise.
  4320. *
  4321. * The path must have already been setup for deleting the leaf, including
  4322. * all the proper balancing. path->nodes[1] must be locked.
  4323. */
  4324. static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
  4325. struct btrfs_root *root,
  4326. struct btrfs_path *path,
  4327. struct extent_buffer *leaf)
  4328. {
  4329. WARN_ON(btrfs_header_generation(leaf) != trans->transid);
  4330. del_ptr(root, path, 1, path->slots[1]);
  4331. /*
  4332. * btrfs_free_extent is expensive, we want to make sure we
  4333. * aren't holding any locks when we call it
  4334. */
  4335. btrfs_unlock_up_safe(path, 0);
  4336. root_sub_used(root, leaf->len);
  4337. extent_buffer_get(leaf);
  4338. btrfs_free_tree_block(trans, root, leaf, 0, 1);
  4339. free_extent_buffer_stale(leaf);
  4340. }
  4341. /*
  4342. * delete the item at the leaf level in path. If that empties
  4343. * the leaf, remove it from the tree
  4344. */
  4345. int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4346. struct btrfs_path *path, int slot, int nr)
  4347. {
  4348. struct btrfs_fs_info *fs_info = root->fs_info;
  4349. struct extent_buffer *leaf;
  4350. struct btrfs_item *item;
  4351. u32 last_off;
  4352. u32 dsize = 0;
  4353. int ret = 0;
  4354. int wret;
  4355. int i;
  4356. u32 nritems;
  4357. struct btrfs_map_token token;
  4358. btrfs_init_map_token(&token);
  4359. leaf = path->nodes[0];
  4360. last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
  4361. for (i = 0; i < nr; i++)
  4362. dsize += btrfs_item_size_nr(leaf, slot + i);
  4363. nritems = btrfs_header_nritems(leaf);
  4364. if (slot + nr != nritems) {
  4365. int data_end = leaf_data_end(fs_info, leaf);
  4366. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4367. data_end + dsize,
  4368. BTRFS_LEAF_DATA_OFFSET + data_end,
  4369. last_off - data_end);
  4370. for (i = slot + nr; i < nritems; i++) {
  4371. u32 ioff;
  4372. item = btrfs_item_nr(i);
  4373. ioff = btrfs_token_item_offset(leaf, item, &token);
  4374. btrfs_set_token_item_offset(leaf, item,
  4375. ioff + dsize, &token);
  4376. }
  4377. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
  4378. btrfs_item_nr_offset(slot + nr),
  4379. sizeof(struct btrfs_item) *
  4380. (nritems - slot - nr));
  4381. }
  4382. btrfs_set_header_nritems(leaf, nritems - nr);
  4383. nritems -= nr;
  4384. /* delete the leaf if we've emptied it */
  4385. if (nritems == 0) {
  4386. if (leaf == root->node) {
  4387. btrfs_set_header_level(leaf, 0);
  4388. } else {
  4389. btrfs_set_path_blocking(path);
  4390. clean_tree_block(fs_info, leaf);
  4391. btrfs_del_leaf(trans, root, path, leaf);
  4392. }
  4393. } else {
  4394. int used = leaf_space_used(leaf, 0, nritems);
  4395. if (slot == 0) {
  4396. struct btrfs_disk_key disk_key;
  4397. btrfs_item_key(leaf, &disk_key, 0);
  4398. fixup_low_keys(fs_info, path, &disk_key, 1);
  4399. }
  4400. /* delete the leaf if it is mostly empty */
  4401. if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
  4402. /* push_leaf_left fixes the path.
  4403. * make sure the path still points to our leaf
  4404. * for possible call to del_ptr below
  4405. */
  4406. slot = path->slots[1];
  4407. extent_buffer_get(leaf);
  4408. btrfs_set_path_blocking(path);
  4409. wret = push_leaf_left(trans, root, path, 1, 1,
  4410. 1, (u32)-1);
  4411. if (wret < 0 && wret != -ENOSPC)
  4412. ret = wret;
  4413. if (path->nodes[0] == leaf &&
  4414. btrfs_header_nritems(leaf)) {
  4415. wret = push_leaf_right(trans, root, path, 1,
  4416. 1, 1, 0);
  4417. if (wret < 0 && wret != -ENOSPC)
  4418. ret = wret;
  4419. }
  4420. if (btrfs_header_nritems(leaf) == 0) {
  4421. path->slots[1] = slot;
  4422. btrfs_del_leaf(trans, root, path, leaf);
  4423. free_extent_buffer(leaf);
  4424. ret = 0;
  4425. } else {
  4426. /* if we're still in the path, make sure
  4427. * we're dirty. Otherwise, one of the
  4428. * push_leaf functions must have already
  4429. * dirtied this buffer
  4430. */
  4431. if (path->nodes[0] == leaf)
  4432. btrfs_mark_buffer_dirty(leaf);
  4433. free_extent_buffer(leaf);
  4434. }
  4435. } else {
  4436. btrfs_mark_buffer_dirty(leaf);
  4437. }
  4438. }
  4439. return ret;
  4440. }
  4441. /*
  4442. * search the tree again to find a leaf with lesser keys
  4443. * returns 0 if it found something or 1 if there are no lesser leaves.
  4444. * returns < 0 on io errors.
  4445. *
  4446. * This may release the path, and so you may lose any locks held at the
  4447. * time you call it.
  4448. */
  4449. int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
  4450. {
  4451. struct btrfs_key key;
  4452. struct btrfs_disk_key found_key;
  4453. int ret;
  4454. btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
  4455. if (key.offset > 0) {
  4456. key.offset--;
  4457. } else if (key.type > 0) {
  4458. key.type--;
  4459. key.offset = (u64)-1;
  4460. } else if (key.objectid > 0) {
  4461. key.objectid--;
  4462. key.type = (u8)-1;
  4463. key.offset = (u64)-1;
  4464. } else {
  4465. return 1;
  4466. }
  4467. btrfs_release_path(path);
  4468. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4469. if (ret < 0)
  4470. return ret;
  4471. btrfs_item_key(path->nodes[0], &found_key, 0);
  4472. ret = comp_keys(&found_key, &key);
  4473. /*
  4474. * We might have had an item with the previous key in the tree right
  4475. * before we released our path. And after we released our path, that
  4476. * item might have been pushed to the first slot (0) of the leaf we
  4477. * were holding due to a tree balance. Alternatively, an item with the
  4478. * previous key can exist as the only element of a leaf (big fat item).
  4479. * Therefore account for these 2 cases, so that our callers (like
  4480. * btrfs_previous_item) don't miss an existing item with a key matching
  4481. * the previous key we computed above.
  4482. */
  4483. if (ret <= 0)
  4484. return 0;
  4485. return 1;
  4486. }
  4487. /*
  4488. * A helper function to walk down the tree starting at min_key, and looking
  4489. * for nodes or leaves that are have a minimum transaction id.
  4490. * This is used by the btree defrag code, and tree logging
  4491. *
  4492. * This does not cow, but it does stuff the starting key it finds back
  4493. * into min_key, so you can call btrfs_search_slot with cow=1 on the
  4494. * key and get a writable path.
  4495. *
  4496. * This honors path->lowest_level to prevent descent past a given level
  4497. * of the tree.
  4498. *
  4499. * min_trans indicates the oldest transaction that you are interested
  4500. * in walking through. Any nodes or leaves older than min_trans are
  4501. * skipped over (without reading them).
  4502. *
  4503. * returns zero if something useful was found, < 0 on error and 1 if there
  4504. * was nothing in the tree that matched the search criteria.
  4505. */
  4506. int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
  4507. struct btrfs_path *path,
  4508. u64 min_trans)
  4509. {
  4510. struct btrfs_fs_info *fs_info = root->fs_info;
  4511. struct extent_buffer *cur;
  4512. struct btrfs_key found_key;
  4513. int slot;
  4514. int sret;
  4515. u32 nritems;
  4516. int level;
  4517. int ret = 1;
  4518. int keep_locks = path->keep_locks;
  4519. path->keep_locks = 1;
  4520. again:
  4521. cur = btrfs_read_lock_root_node(root);
  4522. level = btrfs_header_level(cur);
  4523. WARN_ON(path->nodes[level]);
  4524. path->nodes[level] = cur;
  4525. path->locks[level] = BTRFS_READ_LOCK;
  4526. if (btrfs_header_generation(cur) < min_trans) {
  4527. ret = 1;
  4528. goto out;
  4529. }
  4530. while (1) {
  4531. nritems = btrfs_header_nritems(cur);
  4532. level = btrfs_header_level(cur);
  4533. sret = btrfs_bin_search(cur, min_key, level, &slot);
  4534. /* at the lowest level, we're done, setup the path and exit */
  4535. if (level == path->lowest_level) {
  4536. if (slot >= nritems)
  4537. goto find_next_key;
  4538. ret = 0;
  4539. path->slots[level] = slot;
  4540. btrfs_item_key_to_cpu(cur, &found_key, slot);
  4541. goto out;
  4542. }
  4543. if (sret && slot > 0)
  4544. slot--;
  4545. /*
  4546. * check this node pointer against the min_trans parameters.
  4547. * If it is too old, old, skip to the next one.
  4548. */
  4549. while (slot < nritems) {
  4550. u64 gen;
  4551. gen = btrfs_node_ptr_generation(cur, slot);
  4552. if (gen < min_trans) {
  4553. slot++;
  4554. continue;
  4555. }
  4556. break;
  4557. }
  4558. find_next_key:
  4559. /*
  4560. * we didn't find a candidate key in this node, walk forward
  4561. * and find another one
  4562. */
  4563. if (slot >= nritems) {
  4564. path->slots[level] = slot;
  4565. btrfs_set_path_blocking(path);
  4566. sret = btrfs_find_next_key(root, path, min_key, level,
  4567. min_trans);
  4568. if (sret == 0) {
  4569. btrfs_release_path(path);
  4570. goto again;
  4571. } else {
  4572. goto out;
  4573. }
  4574. }
  4575. /* save our key for returning back */
  4576. btrfs_node_key_to_cpu(cur, &found_key, slot);
  4577. path->slots[level] = slot;
  4578. if (level == path->lowest_level) {
  4579. ret = 0;
  4580. goto out;
  4581. }
  4582. btrfs_set_path_blocking(path);
  4583. cur = read_node_slot(fs_info, cur, slot);
  4584. if (IS_ERR(cur)) {
  4585. ret = PTR_ERR(cur);
  4586. goto out;
  4587. }
  4588. btrfs_tree_read_lock(cur);
  4589. path->locks[level - 1] = BTRFS_READ_LOCK;
  4590. path->nodes[level - 1] = cur;
  4591. unlock_up(path, level, 1, 0, NULL);
  4592. btrfs_clear_path_blocking(path, NULL, 0);
  4593. }
  4594. out:
  4595. path->keep_locks = keep_locks;
  4596. if (ret == 0) {
  4597. btrfs_unlock_up_safe(path, path->lowest_level + 1);
  4598. btrfs_set_path_blocking(path);
  4599. memcpy(min_key, &found_key, sizeof(found_key));
  4600. }
  4601. return ret;
  4602. }
  4603. static int tree_move_down(struct btrfs_fs_info *fs_info,
  4604. struct btrfs_path *path,
  4605. int *level)
  4606. {
  4607. struct extent_buffer *eb;
  4608. BUG_ON(*level == 0);
  4609. eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]);
  4610. if (IS_ERR(eb))
  4611. return PTR_ERR(eb);
  4612. path->nodes[*level - 1] = eb;
  4613. path->slots[*level - 1] = 0;
  4614. (*level)--;
  4615. return 0;
  4616. }
  4617. static int tree_move_next_or_upnext(struct btrfs_path *path,
  4618. int *level, int root_level)
  4619. {
  4620. int ret = 0;
  4621. int nritems;
  4622. nritems = btrfs_header_nritems(path->nodes[*level]);
  4623. path->slots[*level]++;
  4624. while (path->slots[*level] >= nritems) {
  4625. if (*level == root_level)
  4626. return -1;
  4627. /* move upnext */
  4628. path->slots[*level] = 0;
  4629. free_extent_buffer(path->nodes[*level]);
  4630. path->nodes[*level] = NULL;
  4631. (*level)++;
  4632. path->slots[*level]++;
  4633. nritems = btrfs_header_nritems(path->nodes[*level]);
  4634. ret = 1;
  4635. }
  4636. return ret;
  4637. }
  4638. /*
  4639. * Returns 1 if it had to move up and next. 0 is returned if it moved only next
  4640. * or down.
  4641. */
  4642. static int tree_advance(struct btrfs_fs_info *fs_info,
  4643. struct btrfs_path *path,
  4644. int *level, int root_level,
  4645. int allow_down,
  4646. struct btrfs_key *key)
  4647. {
  4648. int ret;
  4649. if (*level == 0 || !allow_down) {
  4650. ret = tree_move_next_or_upnext(path, level, root_level);
  4651. } else {
  4652. ret = tree_move_down(fs_info, path, level);
  4653. }
  4654. if (ret >= 0) {
  4655. if (*level == 0)
  4656. btrfs_item_key_to_cpu(path->nodes[*level], key,
  4657. path->slots[*level]);
  4658. else
  4659. btrfs_node_key_to_cpu(path->nodes[*level], key,
  4660. path->slots[*level]);
  4661. }
  4662. return ret;
  4663. }
  4664. static int tree_compare_item(struct btrfs_path *left_path,
  4665. struct btrfs_path *right_path,
  4666. char *tmp_buf)
  4667. {
  4668. int cmp;
  4669. int len1, len2;
  4670. unsigned long off1, off2;
  4671. len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
  4672. len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
  4673. if (len1 != len2)
  4674. return 1;
  4675. off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
  4676. off2 = btrfs_item_ptr_offset(right_path->nodes[0],
  4677. right_path->slots[0]);
  4678. read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
  4679. cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
  4680. if (cmp)
  4681. return 1;
  4682. return 0;
  4683. }
  4684. #define ADVANCE 1
  4685. #define ADVANCE_ONLY_NEXT -1
  4686. /*
  4687. * This function compares two trees and calls the provided callback for
  4688. * every changed/new/deleted item it finds.
  4689. * If shared tree blocks are encountered, whole subtrees are skipped, making
  4690. * the compare pretty fast on snapshotted subvolumes.
  4691. *
  4692. * This currently works on commit roots only. As commit roots are read only,
  4693. * we don't do any locking. The commit roots are protected with transactions.
  4694. * Transactions are ended and rejoined when a commit is tried in between.
  4695. *
  4696. * This function checks for modifications done to the trees while comparing.
  4697. * If it detects a change, it aborts immediately.
  4698. */
  4699. int btrfs_compare_trees(struct btrfs_root *left_root,
  4700. struct btrfs_root *right_root,
  4701. btrfs_changed_cb_t changed_cb, void *ctx)
  4702. {
  4703. struct btrfs_fs_info *fs_info = left_root->fs_info;
  4704. int ret;
  4705. int cmp;
  4706. struct btrfs_path *left_path = NULL;
  4707. struct btrfs_path *right_path = NULL;
  4708. struct btrfs_key left_key;
  4709. struct btrfs_key right_key;
  4710. char *tmp_buf = NULL;
  4711. int left_root_level;
  4712. int right_root_level;
  4713. int left_level;
  4714. int right_level;
  4715. int left_end_reached;
  4716. int right_end_reached;
  4717. int advance_left;
  4718. int advance_right;
  4719. u64 left_blockptr;
  4720. u64 right_blockptr;
  4721. u64 left_gen;
  4722. u64 right_gen;
  4723. left_path = btrfs_alloc_path();
  4724. if (!left_path) {
  4725. ret = -ENOMEM;
  4726. goto out;
  4727. }
  4728. right_path = btrfs_alloc_path();
  4729. if (!right_path) {
  4730. ret = -ENOMEM;
  4731. goto out;
  4732. }
  4733. tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
  4734. if (!tmp_buf) {
  4735. ret = -ENOMEM;
  4736. goto out;
  4737. }
  4738. left_path->search_commit_root = 1;
  4739. left_path->skip_locking = 1;
  4740. right_path->search_commit_root = 1;
  4741. right_path->skip_locking = 1;
  4742. /*
  4743. * Strategy: Go to the first items of both trees. Then do
  4744. *
  4745. * If both trees are at level 0
  4746. * Compare keys of current items
  4747. * If left < right treat left item as new, advance left tree
  4748. * and repeat
  4749. * If left > right treat right item as deleted, advance right tree
  4750. * and repeat
  4751. * If left == right do deep compare of items, treat as changed if
  4752. * needed, advance both trees and repeat
  4753. * If both trees are at the same level but not at level 0
  4754. * Compare keys of current nodes/leafs
  4755. * If left < right advance left tree and repeat
  4756. * If left > right advance right tree and repeat
  4757. * If left == right compare blockptrs of the next nodes/leafs
  4758. * If they match advance both trees but stay at the same level
  4759. * and repeat
  4760. * If they don't match advance both trees while allowing to go
  4761. * deeper and repeat
  4762. * If tree levels are different
  4763. * Advance the tree that needs it and repeat
  4764. *
  4765. * Advancing a tree means:
  4766. * If we are at level 0, try to go to the next slot. If that's not
  4767. * possible, go one level up and repeat. Stop when we found a level
  4768. * where we could go to the next slot. We may at this point be on a
  4769. * node or a leaf.
  4770. *
  4771. * If we are not at level 0 and not on shared tree blocks, go one
  4772. * level deeper.
  4773. *
  4774. * If we are not at level 0 and on shared tree blocks, go one slot to
  4775. * the right if possible or go up and right.
  4776. */
  4777. down_read(&fs_info->commit_root_sem);
  4778. left_level = btrfs_header_level(left_root->commit_root);
  4779. left_root_level = left_level;
  4780. left_path->nodes[left_level] =
  4781. btrfs_clone_extent_buffer(left_root->commit_root);
  4782. if (!left_path->nodes[left_level]) {
  4783. up_read(&fs_info->commit_root_sem);
  4784. ret = -ENOMEM;
  4785. goto out;
  4786. }
  4787. extent_buffer_get(left_path->nodes[left_level]);
  4788. right_level = btrfs_header_level(right_root->commit_root);
  4789. right_root_level = right_level;
  4790. right_path->nodes[right_level] =
  4791. btrfs_clone_extent_buffer(right_root->commit_root);
  4792. if (!right_path->nodes[right_level]) {
  4793. up_read(&fs_info->commit_root_sem);
  4794. ret = -ENOMEM;
  4795. goto out;
  4796. }
  4797. extent_buffer_get(right_path->nodes[right_level]);
  4798. up_read(&fs_info->commit_root_sem);
  4799. if (left_level == 0)
  4800. btrfs_item_key_to_cpu(left_path->nodes[left_level],
  4801. &left_key, left_path->slots[left_level]);
  4802. else
  4803. btrfs_node_key_to_cpu(left_path->nodes[left_level],
  4804. &left_key, left_path->slots[left_level]);
  4805. if (right_level == 0)
  4806. btrfs_item_key_to_cpu(right_path->nodes[right_level],
  4807. &right_key, right_path->slots[right_level]);
  4808. else
  4809. btrfs_node_key_to_cpu(right_path->nodes[right_level],
  4810. &right_key, right_path->slots[right_level]);
  4811. left_end_reached = right_end_reached = 0;
  4812. advance_left = advance_right = 0;
  4813. while (1) {
  4814. if (advance_left && !left_end_reached) {
  4815. ret = tree_advance(fs_info, left_path, &left_level,
  4816. left_root_level,
  4817. advance_left != ADVANCE_ONLY_NEXT,
  4818. &left_key);
  4819. if (ret == -1)
  4820. left_end_reached = ADVANCE;
  4821. else if (ret < 0)
  4822. goto out;
  4823. advance_left = 0;
  4824. }
  4825. if (advance_right && !right_end_reached) {
  4826. ret = tree_advance(fs_info, right_path, &right_level,
  4827. right_root_level,
  4828. advance_right != ADVANCE_ONLY_NEXT,
  4829. &right_key);
  4830. if (ret == -1)
  4831. right_end_reached = ADVANCE;
  4832. else if (ret < 0)
  4833. goto out;
  4834. advance_right = 0;
  4835. }
  4836. if (left_end_reached && right_end_reached) {
  4837. ret = 0;
  4838. goto out;
  4839. } else if (left_end_reached) {
  4840. if (right_level == 0) {
  4841. ret = changed_cb(left_path, right_path,
  4842. &right_key,
  4843. BTRFS_COMPARE_TREE_DELETED,
  4844. ctx);
  4845. if (ret < 0)
  4846. goto out;
  4847. }
  4848. advance_right = ADVANCE;
  4849. continue;
  4850. } else if (right_end_reached) {
  4851. if (left_level == 0) {
  4852. ret = changed_cb(left_path, right_path,
  4853. &left_key,
  4854. BTRFS_COMPARE_TREE_NEW,
  4855. ctx);
  4856. if (ret < 0)
  4857. goto out;
  4858. }
  4859. advance_left = ADVANCE;
  4860. continue;
  4861. }
  4862. if (left_level == 0 && right_level == 0) {
  4863. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4864. if (cmp < 0) {
  4865. ret = changed_cb(left_path, right_path,
  4866. &left_key,
  4867. BTRFS_COMPARE_TREE_NEW,
  4868. ctx);
  4869. if (ret < 0)
  4870. goto out;
  4871. advance_left = ADVANCE;
  4872. } else if (cmp > 0) {
  4873. ret = changed_cb(left_path, right_path,
  4874. &right_key,
  4875. BTRFS_COMPARE_TREE_DELETED,
  4876. ctx);
  4877. if (ret < 0)
  4878. goto out;
  4879. advance_right = ADVANCE;
  4880. } else {
  4881. enum btrfs_compare_tree_result result;
  4882. WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
  4883. ret = tree_compare_item(left_path, right_path,
  4884. tmp_buf);
  4885. if (ret)
  4886. result = BTRFS_COMPARE_TREE_CHANGED;
  4887. else
  4888. result = BTRFS_COMPARE_TREE_SAME;
  4889. ret = changed_cb(left_path, right_path,
  4890. &left_key, result, ctx);
  4891. if (ret < 0)
  4892. goto out;
  4893. advance_left = ADVANCE;
  4894. advance_right = ADVANCE;
  4895. }
  4896. } else if (left_level == right_level) {
  4897. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4898. if (cmp < 0) {
  4899. advance_left = ADVANCE;
  4900. } else if (cmp > 0) {
  4901. advance_right = ADVANCE;
  4902. } else {
  4903. left_blockptr = btrfs_node_blockptr(
  4904. left_path->nodes[left_level],
  4905. left_path->slots[left_level]);
  4906. right_blockptr = btrfs_node_blockptr(
  4907. right_path->nodes[right_level],
  4908. right_path->slots[right_level]);
  4909. left_gen = btrfs_node_ptr_generation(
  4910. left_path->nodes[left_level],
  4911. left_path->slots[left_level]);
  4912. right_gen = btrfs_node_ptr_generation(
  4913. right_path->nodes[right_level],
  4914. right_path->slots[right_level]);
  4915. if (left_blockptr == right_blockptr &&
  4916. left_gen == right_gen) {
  4917. /*
  4918. * As we're on a shared block, don't
  4919. * allow to go deeper.
  4920. */
  4921. advance_left = ADVANCE_ONLY_NEXT;
  4922. advance_right = ADVANCE_ONLY_NEXT;
  4923. } else {
  4924. advance_left = ADVANCE;
  4925. advance_right = ADVANCE;
  4926. }
  4927. }
  4928. } else if (left_level < right_level) {
  4929. advance_right = ADVANCE;
  4930. } else {
  4931. advance_left = ADVANCE;
  4932. }
  4933. }
  4934. out:
  4935. btrfs_free_path(left_path);
  4936. btrfs_free_path(right_path);
  4937. kvfree(tmp_buf);
  4938. return ret;
  4939. }
  4940. /*
  4941. * this is similar to btrfs_next_leaf, but does not try to preserve
  4942. * and fixup the path. It looks for and returns the next key in the
  4943. * tree based on the current path and the min_trans parameters.
  4944. *
  4945. * 0 is returned if another key is found, < 0 if there are any errors
  4946. * and 1 is returned if there are no higher keys in the tree
  4947. *
  4948. * path->keep_locks should be set to 1 on the search made before
  4949. * calling this function.
  4950. */
  4951. int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
  4952. struct btrfs_key *key, int level, u64 min_trans)
  4953. {
  4954. int slot;
  4955. struct extent_buffer *c;
  4956. WARN_ON(!path->keep_locks);
  4957. while (level < BTRFS_MAX_LEVEL) {
  4958. if (!path->nodes[level])
  4959. return 1;
  4960. slot = path->slots[level] + 1;
  4961. c = path->nodes[level];
  4962. next:
  4963. if (slot >= btrfs_header_nritems(c)) {
  4964. int ret;
  4965. int orig_lowest;
  4966. struct btrfs_key cur_key;
  4967. if (level + 1 >= BTRFS_MAX_LEVEL ||
  4968. !path->nodes[level + 1])
  4969. return 1;
  4970. if (path->locks[level + 1]) {
  4971. level++;
  4972. continue;
  4973. }
  4974. slot = btrfs_header_nritems(c) - 1;
  4975. if (level == 0)
  4976. btrfs_item_key_to_cpu(c, &cur_key, slot);
  4977. else
  4978. btrfs_node_key_to_cpu(c, &cur_key, slot);
  4979. orig_lowest = path->lowest_level;
  4980. btrfs_release_path(path);
  4981. path->lowest_level = level;
  4982. ret = btrfs_search_slot(NULL, root, &cur_key, path,
  4983. 0, 0);
  4984. path->lowest_level = orig_lowest;
  4985. if (ret < 0)
  4986. return ret;
  4987. c = path->nodes[level];
  4988. slot = path->slots[level];
  4989. if (ret == 0)
  4990. slot++;
  4991. goto next;
  4992. }
  4993. if (level == 0)
  4994. btrfs_item_key_to_cpu(c, key, slot);
  4995. else {
  4996. u64 gen = btrfs_node_ptr_generation(c, slot);
  4997. if (gen < min_trans) {
  4998. slot++;
  4999. goto next;
  5000. }
  5001. btrfs_node_key_to_cpu(c, key, slot);
  5002. }
  5003. return 0;
  5004. }
  5005. return 1;
  5006. }
  5007. /*
  5008. * search the tree again to find a leaf with greater keys
  5009. * returns 0 if it found something or 1 if there are no greater leaves.
  5010. * returns < 0 on io errors.
  5011. */
  5012. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
  5013. {
  5014. return btrfs_next_old_leaf(root, path, 0);
  5015. }
  5016. int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
  5017. u64 time_seq)
  5018. {
  5019. int slot;
  5020. int level;
  5021. struct extent_buffer *c;
  5022. struct extent_buffer *next;
  5023. struct btrfs_key key;
  5024. u32 nritems;
  5025. int ret;
  5026. int old_spinning = path->leave_spinning;
  5027. int next_rw_lock = 0;
  5028. nritems = btrfs_header_nritems(path->nodes[0]);
  5029. if (nritems == 0)
  5030. return 1;
  5031. btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
  5032. again:
  5033. level = 1;
  5034. next = NULL;
  5035. next_rw_lock = 0;
  5036. btrfs_release_path(path);
  5037. path->keep_locks = 1;
  5038. path->leave_spinning = 1;
  5039. if (time_seq)
  5040. ret = btrfs_search_old_slot(root, &key, path, time_seq);
  5041. else
  5042. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5043. path->keep_locks = 0;
  5044. if (ret < 0)
  5045. return ret;
  5046. nritems = btrfs_header_nritems(path->nodes[0]);
  5047. /*
  5048. * by releasing the path above we dropped all our locks. A balance
  5049. * could have added more items next to the key that used to be
  5050. * at the very end of the block. So, check again here and
  5051. * advance the path if there are now more items available.
  5052. */
  5053. if (nritems > 0 && path->slots[0] < nritems - 1) {
  5054. if (ret == 0)
  5055. path->slots[0]++;
  5056. ret = 0;
  5057. goto done;
  5058. }
  5059. /*
  5060. * So the above check misses one case:
  5061. * - after releasing the path above, someone has removed the item that
  5062. * used to be at the very end of the block, and balance between leafs
  5063. * gets another one with bigger key.offset to replace it.
  5064. *
  5065. * This one should be returned as well, or we can get leaf corruption
  5066. * later(esp. in __btrfs_drop_extents()).
  5067. *
  5068. * And a bit more explanation about this check,
  5069. * with ret > 0, the key isn't found, the path points to the slot
  5070. * where it should be inserted, so the path->slots[0] item must be the
  5071. * bigger one.
  5072. */
  5073. if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
  5074. ret = 0;
  5075. goto done;
  5076. }
  5077. while (level < BTRFS_MAX_LEVEL) {
  5078. if (!path->nodes[level]) {
  5079. ret = 1;
  5080. goto done;
  5081. }
  5082. slot = path->slots[level] + 1;
  5083. c = path->nodes[level];
  5084. if (slot >= btrfs_header_nritems(c)) {
  5085. level++;
  5086. if (level == BTRFS_MAX_LEVEL) {
  5087. ret = 1;
  5088. goto done;
  5089. }
  5090. continue;
  5091. }
  5092. if (next) {
  5093. btrfs_tree_unlock_rw(next, next_rw_lock);
  5094. free_extent_buffer(next);
  5095. }
  5096. next = c;
  5097. next_rw_lock = path->locks[level];
  5098. ret = read_block_for_search(root, path, &next, level,
  5099. slot, &key);
  5100. if (ret == -EAGAIN)
  5101. goto again;
  5102. if (ret < 0) {
  5103. btrfs_release_path(path);
  5104. goto done;
  5105. }
  5106. if (!path->skip_locking) {
  5107. ret = btrfs_try_tree_read_lock(next);
  5108. if (!ret && time_seq) {
  5109. /*
  5110. * If we don't get the lock, we may be racing
  5111. * with push_leaf_left, holding that lock while
  5112. * itself waiting for the leaf we've currently
  5113. * locked. To solve this situation, we give up
  5114. * on our lock and cycle.
  5115. */
  5116. free_extent_buffer(next);
  5117. btrfs_release_path(path);
  5118. cond_resched();
  5119. goto again;
  5120. }
  5121. if (!ret) {
  5122. btrfs_set_path_blocking(path);
  5123. btrfs_tree_read_lock(next);
  5124. btrfs_clear_path_blocking(path, next,
  5125. BTRFS_READ_LOCK);
  5126. }
  5127. next_rw_lock = BTRFS_READ_LOCK;
  5128. }
  5129. break;
  5130. }
  5131. path->slots[level] = slot;
  5132. while (1) {
  5133. level--;
  5134. c = path->nodes[level];
  5135. if (path->locks[level])
  5136. btrfs_tree_unlock_rw(c, path->locks[level]);
  5137. free_extent_buffer(c);
  5138. path->nodes[level] = next;
  5139. path->slots[level] = 0;
  5140. if (!path->skip_locking)
  5141. path->locks[level] = next_rw_lock;
  5142. if (!level)
  5143. break;
  5144. ret = read_block_for_search(root, path, &next, level,
  5145. 0, &key);
  5146. if (ret == -EAGAIN)
  5147. goto again;
  5148. if (ret < 0) {
  5149. btrfs_release_path(path);
  5150. goto done;
  5151. }
  5152. if (!path->skip_locking) {
  5153. ret = btrfs_try_tree_read_lock(next);
  5154. if (!ret) {
  5155. btrfs_set_path_blocking(path);
  5156. btrfs_tree_read_lock(next);
  5157. btrfs_clear_path_blocking(path, next,
  5158. BTRFS_READ_LOCK);
  5159. }
  5160. next_rw_lock = BTRFS_READ_LOCK;
  5161. }
  5162. }
  5163. ret = 0;
  5164. done:
  5165. unlock_up(path, 0, 1, 0, NULL);
  5166. path->leave_spinning = old_spinning;
  5167. if (!old_spinning)
  5168. btrfs_set_path_blocking(path);
  5169. return ret;
  5170. }
  5171. /*
  5172. * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
  5173. * searching until it gets past min_objectid or finds an item of 'type'
  5174. *
  5175. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5176. */
  5177. int btrfs_previous_item(struct btrfs_root *root,
  5178. struct btrfs_path *path, u64 min_objectid,
  5179. int type)
  5180. {
  5181. struct btrfs_key found_key;
  5182. struct extent_buffer *leaf;
  5183. u32 nritems;
  5184. int ret;
  5185. while (1) {
  5186. if (path->slots[0] == 0) {
  5187. btrfs_set_path_blocking(path);
  5188. ret = btrfs_prev_leaf(root, path);
  5189. if (ret != 0)
  5190. return ret;
  5191. } else {
  5192. path->slots[0]--;
  5193. }
  5194. leaf = path->nodes[0];
  5195. nritems = btrfs_header_nritems(leaf);
  5196. if (nritems == 0)
  5197. return 1;
  5198. if (path->slots[0] == nritems)
  5199. path->slots[0]--;
  5200. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5201. if (found_key.objectid < min_objectid)
  5202. break;
  5203. if (found_key.type == type)
  5204. return 0;
  5205. if (found_key.objectid == min_objectid &&
  5206. found_key.type < type)
  5207. break;
  5208. }
  5209. return 1;
  5210. }
  5211. /*
  5212. * search in extent tree to find a previous Metadata/Data extent item with
  5213. * min objecitd.
  5214. *
  5215. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5216. */
  5217. int btrfs_previous_extent_item(struct btrfs_root *root,
  5218. struct btrfs_path *path, u64 min_objectid)
  5219. {
  5220. struct btrfs_key found_key;
  5221. struct extent_buffer *leaf;
  5222. u32 nritems;
  5223. int ret;
  5224. while (1) {
  5225. if (path->slots[0] == 0) {
  5226. btrfs_set_path_blocking(path);
  5227. ret = btrfs_prev_leaf(root, path);
  5228. if (ret != 0)
  5229. return ret;
  5230. } else {
  5231. path->slots[0]--;
  5232. }
  5233. leaf = path->nodes[0];
  5234. nritems = btrfs_header_nritems(leaf);
  5235. if (nritems == 0)
  5236. return 1;
  5237. if (path->slots[0] == nritems)
  5238. path->slots[0]--;
  5239. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5240. if (found_key.objectid < min_objectid)
  5241. break;
  5242. if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
  5243. found_key.type == BTRFS_METADATA_ITEM_KEY)
  5244. return 0;
  5245. if (found_key.objectid == min_objectid &&
  5246. found_key.type < BTRFS_EXTENT_ITEM_KEY)
  5247. break;
  5248. }
  5249. return 1;
  5250. }