ctree.c 147 KB

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