ctree.c 149 KB

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