ctree.c 135 KB

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