ctree.c 148 KB

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