ctree.c 146 KB

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