ctree.c 115 KB

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