ctree.c 138 KB

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