ctree.c 133 KB

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