ctree.c 148 KB

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