ctree.c 147 KB

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