ctree.c 124 KB

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