ctree.c 147 KB

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