ctree.c 146 KB

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