ctree.c 152 KB

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