tree-log.c 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Oracle. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/slab.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/list_sort.h>
  9. #include <linux/iversion.h>
  10. #include "ctree.h"
  11. #include "tree-log.h"
  12. #include "disk-io.h"
  13. #include "locking.h"
  14. #include "print-tree.h"
  15. #include "backref.h"
  16. #include "compression.h"
  17. #include "qgroup.h"
  18. #include "inode-map.h"
  19. /* magic values for the inode_only field in btrfs_log_inode:
  20. *
  21. * LOG_INODE_ALL means to log everything
  22. * LOG_INODE_EXISTS means to log just enough to recreate the inode
  23. * during log replay
  24. */
  25. #define LOG_INODE_ALL 0
  26. #define LOG_INODE_EXISTS 1
  27. #define LOG_OTHER_INODE 2
  28. /*
  29. * directory trouble cases
  30. *
  31. * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
  32. * log, we must force a full commit before doing an fsync of the directory
  33. * where the unlink was done.
  34. * ---> record transid of last unlink/rename per directory
  35. *
  36. * mkdir foo/some_dir
  37. * normal commit
  38. * rename foo/some_dir foo2/some_dir
  39. * mkdir foo/some_dir
  40. * fsync foo/some_dir/some_file
  41. *
  42. * The fsync above will unlink the original some_dir without recording
  43. * it in its new location (foo2). After a crash, some_dir will be gone
  44. * unless the fsync of some_file forces a full commit
  45. *
  46. * 2) we must log any new names for any file or dir that is in the fsync
  47. * log. ---> check inode while renaming/linking.
  48. *
  49. * 2a) we must log any new names for any file or dir during rename
  50. * when the directory they are being removed from was logged.
  51. * ---> check inode and old parent dir during rename
  52. *
  53. * 2a is actually the more important variant. With the extra logging
  54. * a crash might unlink the old name without recreating the new one
  55. *
  56. * 3) after a crash, we must go through any directories with a link count
  57. * of zero and redo the rm -rf
  58. *
  59. * mkdir f1/foo
  60. * normal commit
  61. * rm -rf f1/foo
  62. * fsync(f1)
  63. *
  64. * The directory f1 was fully removed from the FS, but fsync was never
  65. * called on f1, only its parent dir. After a crash the rm -rf must
  66. * be replayed. This must be able to recurse down the entire
  67. * directory tree. The inode link count fixup code takes care of the
  68. * ugly details.
  69. */
  70. /*
  71. * stages for the tree walking. The first
  72. * stage (0) is to only pin down the blocks we find
  73. * the second stage (1) is to make sure that all the inodes
  74. * we find in the log are created in the subvolume.
  75. *
  76. * The last stage is to deal with directories and links and extents
  77. * and all the other fun semantics
  78. */
  79. #define LOG_WALK_PIN_ONLY 0
  80. #define LOG_WALK_REPLAY_INODES 1
  81. #define LOG_WALK_REPLAY_DIR_INDEX 2
  82. #define LOG_WALK_REPLAY_ALL 3
  83. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  84. struct btrfs_root *root, struct btrfs_inode *inode,
  85. int inode_only,
  86. const loff_t start,
  87. const loff_t end,
  88. struct btrfs_log_ctx *ctx);
  89. static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  90. struct btrfs_root *root,
  91. struct btrfs_path *path, u64 objectid);
  92. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  93. struct btrfs_root *root,
  94. struct btrfs_root *log,
  95. struct btrfs_path *path,
  96. u64 dirid, int del_all);
  97. /*
  98. * tree logging is a special write ahead log used to make sure that
  99. * fsyncs and O_SYNCs can happen without doing full tree commits.
  100. *
  101. * Full tree commits are expensive because they require commonly
  102. * modified blocks to be recowed, creating many dirty pages in the
  103. * extent tree an 4x-6x higher write load than ext3.
  104. *
  105. * Instead of doing a tree commit on every fsync, we use the
  106. * key ranges and transaction ids to find items for a given file or directory
  107. * that have changed in this transaction. Those items are copied into
  108. * a special tree (one per subvolume root), that tree is written to disk
  109. * and then the fsync is considered complete.
  110. *
  111. * After a crash, items are copied out of the log-tree back into the
  112. * subvolume tree. Any file data extents found are recorded in the extent
  113. * allocation tree, and the log-tree freed.
  114. *
  115. * The log tree is read three times, once to pin down all the extents it is
  116. * using in ram and once, once to create all the inodes logged in the tree
  117. * and once to do all the other items.
  118. */
  119. /*
  120. * start a sub transaction and setup the log tree
  121. * this increments the log tree writer count to make the people
  122. * syncing the tree wait for us to finish
  123. */
  124. static int start_log_trans(struct btrfs_trans_handle *trans,
  125. struct btrfs_root *root,
  126. struct btrfs_log_ctx *ctx)
  127. {
  128. struct btrfs_fs_info *fs_info = root->fs_info;
  129. int ret = 0;
  130. mutex_lock(&root->log_mutex);
  131. if (root->log_root) {
  132. if (btrfs_need_log_full_commit(fs_info, trans)) {
  133. ret = -EAGAIN;
  134. goto out;
  135. }
  136. if (!root->log_start_pid) {
  137. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  138. root->log_start_pid = current->pid;
  139. } else if (root->log_start_pid != current->pid) {
  140. set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  141. }
  142. } else {
  143. mutex_lock(&fs_info->tree_log_mutex);
  144. if (!fs_info->log_root_tree)
  145. ret = btrfs_init_log_root_tree(trans, fs_info);
  146. mutex_unlock(&fs_info->tree_log_mutex);
  147. if (ret)
  148. goto out;
  149. ret = btrfs_add_log_tree(trans, root);
  150. if (ret)
  151. goto out;
  152. clear_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
  153. root->log_start_pid = current->pid;
  154. }
  155. atomic_inc(&root->log_batch);
  156. atomic_inc(&root->log_writers);
  157. if (ctx) {
  158. int index = root->log_transid % 2;
  159. list_add_tail(&ctx->list, &root->log_ctxs[index]);
  160. ctx->log_transid = root->log_transid;
  161. }
  162. out:
  163. mutex_unlock(&root->log_mutex);
  164. return ret;
  165. }
  166. /*
  167. * returns 0 if there was a log transaction running and we were able
  168. * to join, or returns -ENOENT if there were not transactions
  169. * in progress
  170. */
  171. static int join_running_log_trans(struct btrfs_root *root)
  172. {
  173. int ret = -ENOENT;
  174. smp_mb();
  175. if (!root->log_root)
  176. return -ENOENT;
  177. mutex_lock(&root->log_mutex);
  178. if (root->log_root) {
  179. ret = 0;
  180. atomic_inc(&root->log_writers);
  181. }
  182. mutex_unlock(&root->log_mutex);
  183. return ret;
  184. }
  185. /*
  186. * This either makes the current running log transaction wait
  187. * until you call btrfs_end_log_trans() or it makes any future
  188. * log transactions wait until you call btrfs_end_log_trans()
  189. */
  190. void btrfs_pin_log_trans(struct btrfs_root *root)
  191. {
  192. mutex_lock(&root->log_mutex);
  193. atomic_inc(&root->log_writers);
  194. mutex_unlock(&root->log_mutex);
  195. }
  196. /*
  197. * indicate we're done making changes to the log tree
  198. * and wake up anyone waiting to do a sync
  199. */
  200. void btrfs_end_log_trans(struct btrfs_root *root)
  201. {
  202. if (atomic_dec_and_test(&root->log_writers)) {
  203. /* atomic_dec_and_test implies a barrier */
  204. cond_wake_up_nomb(&root->log_writer_wait);
  205. }
  206. }
  207. /*
  208. * the walk control struct is used to pass state down the chain when
  209. * processing the log tree. The stage field tells us which part
  210. * of the log tree processing we are currently doing. The others
  211. * are state fields used for that specific part
  212. */
  213. struct walk_control {
  214. /* should we free the extent on disk when done? This is used
  215. * at transaction commit time while freeing a log tree
  216. */
  217. int free;
  218. /* should we write out the extent buffer? This is used
  219. * while flushing the log tree to disk during a sync
  220. */
  221. int write;
  222. /* should we wait for the extent buffer io to finish? Also used
  223. * while flushing the log tree to disk for a sync
  224. */
  225. int wait;
  226. /* pin only walk, we record which extents on disk belong to the
  227. * log trees
  228. */
  229. int pin;
  230. /* what stage of the replay code we're currently in */
  231. int stage;
  232. /*
  233. * Ignore any items from the inode currently being processed. Needs
  234. * to be set every time we find a BTRFS_INODE_ITEM_KEY and we are in
  235. * the LOG_WALK_REPLAY_INODES stage.
  236. */
  237. bool ignore_cur_inode;
  238. /* the root we are currently replaying */
  239. struct btrfs_root *replay_dest;
  240. /* the trans handle for the current replay */
  241. struct btrfs_trans_handle *trans;
  242. /* the function that gets used to process blocks we find in the
  243. * tree. Note the extent_buffer might not be up to date when it is
  244. * passed in, and it must be checked or read if you need the data
  245. * inside it
  246. */
  247. int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
  248. struct walk_control *wc, u64 gen, int level);
  249. };
  250. /*
  251. * process_func used to pin down extents, write them or wait on them
  252. */
  253. static int process_one_buffer(struct btrfs_root *log,
  254. struct extent_buffer *eb,
  255. struct walk_control *wc, u64 gen, int level)
  256. {
  257. struct btrfs_fs_info *fs_info = log->fs_info;
  258. int ret = 0;
  259. /*
  260. * If this fs is mixed then we need to be able to process the leaves to
  261. * pin down any logged extents, so we have to read the block.
  262. */
  263. if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
  264. ret = btrfs_read_buffer(eb, gen, level, NULL);
  265. if (ret)
  266. return ret;
  267. }
  268. if (wc->pin)
  269. ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
  270. eb->len);
  271. if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
  272. if (wc->pin && btrfs_header_level(eb) == 0)
  273. ret = btrfs_exclude_logged_extents(fs_info, eb);
  274. if (wc->write)
  275. btrfs_write_tree_block(eb);
  276. if (wc->wait)
  277. btrfs_wait_tree_block_writeback(eb);
  278. }
  279. return ret;
  280. }
  281. /*
  282. * Item overwrite used by replay and tree logging. eb, slot and key all refer
  283. * to the src data we are copying out.
  284. *
  285. * root is the tree we are copying into, and path is a scratch
  286. * path for use in this function (it should be released on entry and
  287. * will be released on exit).
  288. *
  289. * If the key is already in the destination tree the existing item is
  290. * overwritten. If the existing item isn't big enough, it is extended.
  291. * If it is too large, it is truncated.
  292. *
  293. * If the key isn't in the destination yet, a new item is inserted.
  294. */
  295. static noinline int overwrite_item(struct btrfs_trans_handle *trans,
  296. struct btrfs_root *root,
  297. struct btrfs_path *path,
  298. struct extent_buffer *eb, int slot,
  299. struct btrfs_key *key)
  300. {
  301. struct btrfs_fs_info *fs_info = root->fs_info;
  302. int ret;
  303. u32 item_size;
  304. u64 saved_i_size = 0;
  305. int save_old_i_size = 0;
  306. unsigned long src_ptr;
  307. unsigned long dst_ptr;
  308. int overwrite_root = 0;
  309. bool inode_item = key->type == BTRFS_INODE_ITEM_KEY;
  310. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  311. overwrite_root = 1;
  312. item_size = btrfs_item_size_nr(eb, slot);
  313. src_ptr = btrfs_item_ptr_offset(eb, slot);
  314. /* look for the key in the destination tree */
  315. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  316. if (ret < 0)
  317. return ret;
  318. if (ret == 0) {
  319. char *src_copy;
  320. char *dst_copy;
  321. u32 dst_size = btrfs_item_size_nr(path->nodes[0],
  322. path->slots[0]);
  323. if (dst_size != item_size)
  324. goto insert;
  325. if (item_size == 0) {
  326. btrfs_release_path(path);
  327. return 0;
  328. }
  329. dst_copy = kmalloc(item_size, GFP_NOFS);
  330. src_copy = kmalloc(item_size, GFP_NOFS);
  331. if (!dst_copy || !src_copy) {
  332. btrfs_release_path(path);
  333. kfree(dst_copy);
  334. kfree(src_copy);
  335. return -ENOMEM;
  336. }
  337. read_extent_buffer(eb, src_copy, src_ptr, item_size);
  338. dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  339. read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
  340. item_size);
  341. ret = memcmp(dst_copy, src_copy, item_size);
  342. kfree(dst_copy);
  343. kfree(src_copy);
  344. /*
  345. * they have the same contents, just return, this saves
  346. * us from cowing blocks in the destination tree and doing
  347. * extra writes that may not have been done by a previous
  348. * sync
  349. */
  350. if (ret == 0) {
  351. btrfs_release_path(path);
  352. return 0;
  353. }
  354. /*
  355. * We need to load the old nbytes into the inode so when we
  356. * replay the extents we've logged we get the right nbytes.
  357. */
  358. if (inode_item) {
  359. struct btrfs_inode_item *item;
  360. u64 nbytes;
  361. u32 mode;
  362. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  363. struct btrfs_inode_item);
  364. nbytes = btrfs_inode_nbytes(path->nodes[0], item);
  365. item = btrfs_item_ptr(eb, slot,
  366. struct btrfs_inode_item);
  367. btrfs_set_inode_nbytes(eb, item, nbytes);
  368. /*
  369. * If this is a directory we need to reset the i_size to
  370. * 0 so that we can set it up properly when replaying
  371. * the rest of the items in this log.
  372. */
  373. mode = btrfs_inode_mode(eb, item);
  374. if (S_ISDIR(mode))
  375. btrfs_set_inode_size(eb, item, 0);
  376. }
  377. } else if (inode_item) {
  378. struct btrfs_inode_item *item;
  379. u32 mode;
  380. /*
  381. * New inode, set nbytes to 0 so that the nbytes comes out
  382. * properly when we replay the extents.
  383. */
  384. item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
  385. btrfs_set_inode_nbytes(eb, item, 0);
  386. /*
  387. * If this is a directory we need to reset the i_size to 0 so
  388. * that we can set it up properly when replaying the rest of
  389. * the items in this log.
  390. */
  391. mode = btrfs_inode_mode(eb, item);
  392. if (S_ISDIR(mode))
  393. btrfs_set_inode_size(eb, item, 0);
  394. }
  395. insert:
  396. btrfs_release_path(path);
  397. /* try to insert the key into the destination tree */
  398. path->skip_release_on_error = 1;
  399. ret = btrfs_insert_empty_item(trans, root, path,
  400. key, item_size);
  401. path->skip_release_on_error = 0;
  402. /* make sure any existing item is the correct size */
  403. if (ret == -EEXIST || ret == -EOVERFLOW) {
  404. u32 found_size;
  405. found_size = btrfs_item_size_nr(path->nodes[0],
  406. path->slots[0]);
  407. if (found_size > item_size)
  408. btrfs_truncate_item(fs_info, path, item_size, 1);
  409. else if (found_size < item_size)
  410. btrfs_extend_item(fs_info, path,
  411. item_size - found_size);
  412. } else if (ret) {
  413. return ret;
  414. }
  415. dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
  416. path->slots[0]);
  417. /* don't overwrite an existing inode if the generation number
  418. * was logged as zero. This is done when the tree logging code
  419. * is just logging an inode to make sure it exists after recovery.
  420. *
  421. * Also, don't overwrite i_size on directories during replay.
  422. * log replay inserts and removes directory items based on the
  423. * state of the tree found in the subvolume, and i_size is modified
  424. * as it goes
  425. */
  426. if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
  427. struct btrfs_inode_item *src_item;
  428. struct btrfs_inode_item *dst_item;
  429. src_item = (struct btrfs_inode_item *)src_ptr;
  430. dst_item = (struct btrfs_inode_item *)dst_ptr;
  431. if (btrfs_inode_generation(eb, src_item) == 0) {
  432. struct extent_buffer *dst_eb = path->nodes[0];
  433. const u64 ino_size = btrfs_inode_size(eb, src_item);
  434. /*
  435. * For regular files an ino_size == 0 is used only when
  436. * logging that an inode exists, as part of a directory
  437. * fsync, and the inode wasn't fsynced before. In this
  438. * case don't set the size of the inode in the fs/subvol
  439. * tree, otherwise we would be throwing valid data away.
  440. */
  441. if (S_ISREG(btrfs_inode_mode(eb, src_item)) &&
  442. S_ISREG(btrfs_inode_mode(dst_eb, dst_item)) &&
  443. ino_size != 0) {
  444. struct btrfs_map_token token;
  445. btrfs_init_map_token(&token);
  446. btrfs_set_token_inode_size(dst_eb, dst_item,
  447. ino_size, &token);
  448. }
  449. goto no_copy;
  450. }
  451. if (overwrite_root &&
  452. S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
  453. S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
  454. save_old_i_size = 1;
  455. saved_i_size = btrfs_inode_size(path->nodes[0],
  456. dst_item);
  457. }
  458. }
  459. copy_extent_buffer(path->nodes[0], eb, dst_ptr,
  460. src_ptr, item_size);
  461. if (save_old_i_size) {
  462. struct btrfs_inode_item *dst_item;
  463. dst_item = (struct btrfs_inode_item *)dst_ptr;
  464. btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
  465. }
  466. /* make sure the generation is filled in */
  467. if (key->type == BTRFS_INODE_ITEM_KEY) {
  468. struct btrfs_inode_item *dst_item;
  469. dst_item = (struct btrfs_inode_item *)dst_ptr;
  470. if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
  471. btrfs_set_inode_generation(path->nodes[0], dst_item,
  472. trans->transid);
  473. }
  474. }
  475. no_copy:
  476. btrfs_mark_buffer_dirty(path->nodes[0]);
  477. btrfs_release_path(path);
  478. return 0;
  479. }
  480. /*
  481. * simple helper to read an inode off the disk from a given root
  482. * This can only be called for subvolume roots and not for the log
  483. */
  484. static noinline struct inode *read_one_inode(struct btrfs_root *root,
  485. u64 objectid)
  486. {
  487. struct btrfs_key key;
  488. struct inode *inode;
  489. key.objectid = objectid;
  490. key.type = BTRFS_INODE_ITEM_KEY;
  491. key.offset = 0;
  492. inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
  493. if (IS_ERR(inode))
  494. inode = NULL;
  495. return inode;
  496. }
  497. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  498. * subvolume 'root'. path is released on entry and should be released
  499. * on exit.
  500. *
  501. * extents in the log tree have not been allocated out of the extent
  502. * tree yet. So, this completes the allocation, taking a reference
  503. * as required if the extent already exists or creating a new extent
  504. * if it isn't in the extent allocation tree yet.
  505. *
  506. * The extent is inserted into the file, dropping any existing extents
  507. * from the file that overlap the new one.
  508. */
  509. static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
  510. struct btrfs_root *root,
  511. struct btrfs_path *path,
  512. struct extent_buffer *eb, int slot,
  513. struct btrfs_key *key)
  514. {
  515. struct btrfs_fs_info *fs_info = root->fs_info;
  516. int found_type;
  517. u64 extent_end;
  518. u64 start = key->offset;
  519. u64 nbytes = 0;
  520. struct btrfs_file_extent_item *item;
  521. struct inode *inode = NULL;
  522. unsigned long size;
  523. int ret = 0;
  524. item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  525. found_type = btrfs_file_extent_type(eb, item);
  526. if (found_type == BTRFS_FILE_EXTENT_REG ||
  527. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  528. nbytes = btrfs_file_extent_num_bytes(eb, item);
  529. extent_end = start + nbytes;
  530. /*
  531. * We don't add to the inodes nbytes if we are prealloc or a
  532. * hole.
  533. */
  534. if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
  535. nbytes = 0;
  536. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  537. size = btrfs_file_extent_ram_bytes(eb, item);
  538. nbytes = btrfs_file_extent_ram_bytes(eb, item);
  539. extent_end = ALIGN(start + size,
  540. fs_info->sectorsize);
  541. } else {
  542. ret = 0;
  543. goto out;
  544. }
  545. inode = read_one_inode(root, key->objectid);
  546. if (!inode) {
  547. ret = -EIO;
  548. goto out;
  549. }
  550. /*
  551. * first check to see if we already have this extent in the
  552. * file. This must be done before the btrfs_drop_extents run
  553. * so we don't try to drop this extent.
  554. */
  555. ret = btrfs_lookup_file_extent(trans, root, path,
  556. btrfs_ino(BTRFS_I(inode)), start, 0);
  557. if (ret == 0 &&
  558. (found_type == BTRFS_FILE_EXTENT_REG ||
  559. found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
  560. struct btrfs_file_extent_item cmp1;
  561. struct btrfs_file_extent_item cmp2;
  562. struct btrfs_file_extent_item *existing;
  563. struct extent_buffer *leaf;
  564. leaf = path->nodes[0];
  565. existing = btrfs_item_ptr(leaf, path->slots[0],
  566. struct btrfs_file_extent_item);
  567. read_extent_buffer(eb, &cmp1, (unsigned long)item,
  568. sizeof(cmp1));
  569. read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
  570. sizeof(cmp2));
  571. /*
  572. * we already have a pointer to this exact extent,
  573. * we don't have to do anything
  574. */
  575. if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
  576. btrfs_release_path(path);
  577. goto out;
  578. }
  579. }
  580. btrfs_release_path(path);
  581. /* drop any overlapping extents */
  582. ret = btrfs_drop_extents(trans, root, inode, start, extent_end, 1);
  583. if (ret)
  584. goto out;
  585. if (found_type == BTRFS_FILE_EXTENT_REG ||
  586. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  587. u64 offset;
  588. unsigned long dest_offset;
  589. struct btrfs_key ins;
  590. if (btrfs_file_extent_disk_bytenr(eb, item) == 0 &&
  591. btrfs_fs_incompat(fs_info, NO_HOLES))
  592. goto update_inode;
  593. ret = btrfs_insert_empty_item(trans, root, path, key,
  594. sizeof(*item));
  595. if (ret)
  596. goto out;
  597. dest_offset = btrfs_item_ptr_offset(path->nodes[0],
  598. path->slots[0]);
  599. copy_extent_buffer(path->nodes[0], eb, dest_offset,
  600. (unsigned long)item, sizeof(*item));
  601. ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  602. ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  603. ins.type = BTRFS_EXTENT_ITEM_KEY;
  604. offset = key->offset - btrfs_file_extent_offset(eb, item);
  605. /*
  606. * Manually record dirty extent, as here we did a shallow
  607. * file extent item copy and skip normal backref update,
  608. * but modifying extent tree all by ourselves.
  609. * So need to manually record dirty extent for qgroup,
  610. * as the owner of the file extent changed from log tree
  611. * (doesn't affect qgroup) to fs/file tree(affects qgroup)
  612. */
  613. ret = btrfs_qgroup_trace_extent(trans,
  614. btrfs_file_extent_disk_bytenr(eb, item),
  615. btrfs_file_extent_disk_num_bytes(eb, item),
  616. GFP_NOFS);
  617. if (ret < 0)
  618. goto out;
  619. if (ins.objectid > 0) {
  620. u64 csum_start;
  621. u64 csum_end;
  622. LIST_HEAD(ordered_sums);
  623. /*
  624. * is this extent already allocated in the extent
  625. * allocation tree? If so, just add a reference
  626. */
  627. ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
  628. ins.offset);
  629. if (ret == 0) {
  630. ret = btrfs_inc_extent_ref(trans, root,
  631. ins.objectid, ins.offset,
  632. 0, root->root_key.objectid,
  633. key->objectid, offset);
  634. if (ret)
  635. goto out;
  636. } else {
  637. /*
  638. * insert the extent pointer in the extent
  639. * allocation tree
  640. */
  641. ret = btrfs_alloc_logged_file_extent(trans,
  642. root->root_key.objectid,
  643. key->objectid, offset, &ins);
  644. if (ret)
  645. goto out;
  646. }
  647. btrfs_release_path(path);
  648. if (btrfs_file_extent_compression(eb, item)) {
  649. csum_start = ins.objectid;
  650. csum_end = csum_start + ins.offset;
  651. } else {
  652. csum_start = ins.objectid +
  653. btrfs_file_extent_offset(eb, item);
  654. csum_end = csum_start +
  655. btrfs_file_extent_num_bytes(eb, item);
  656. }
  657. ret = btrfs_lookup_csums_range(root->log_root,
  658. csum_start, csum_end - 1,
  659. &ordered_sums, 0);
  660. if (ret)
  661. goto out;
  662. /*
  663. * Now delete all existing cums in the csum root that
  664. * cover our range. We do this because we can have an
  665. * extent that is completely referenced by one file
  666. * extent item and partially referenced by another
  667. * file extent item (like after using the clone or
  668. * extent_same ioctls). In this case if we end up doing
  669. * the replay of the one that partially references the
  670. * extent first, and we do not do the csum deletion
  671. * below, we can get 2 csum items in the csum tree that
  672. * overlap each other. For example, imagine our log has
  673. * the two following file extent items:
  674. *
  675. * key (257 EXTENT_DATA 409600)
  676. * extent data disk byte 12845056 nr 102400
  677. * extent data offset 20480 nr 20480 ram 102400
  678. *
  679. * key (257 EXTENT_DATA 819200)
  680. * extent data disk byte 12845056 nr 102400
  681. * extent data offset 0 nr 102400 ram 102400
  682. *
  683. * Where the second one fully references the 100K extent
  684. * that starts at disk byte 12845056, and the log tree
  685. * has a single csum item that covers the entire range
  686. * of the extent:
  687. *
  688. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  689. *
  690. * After the first file extent item is replayed, the
  691. * csum tree gets the following csum item:
  692. *
  693. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  694. *
  695. * Which covers the 20K sub-range starting at offset 20K
  696. * of our extent. Now when we replay the second file
  697. * extent item, if we do not delete existing csum items
  698. * that cover any of its blocks, we end up getting two
  699. * csum items in our csum tree that overlap each other:
  700. *
  701. * key (EXTENT_CSUM EXTENT_CSUM 12845056) itemsize 100
  702. * key (EXTENT_CSUM EXTENT_CSUM 12865536) itemsize 20
  703. *
  704. * Which is a problem, because after this anyone trying
  705. * to lookup up for the checksum of any block of our
  706. * extent starting at an offset of 40K or higher, will
  707. * end up looking at the second csum item only, which
  708. * does not contain the checksum for any block starting
  709. * at offset 40K or higher of our extent.
  710. */
  711. while (!list_empty(&ordered_sums)) {
  712. struct btrfs_ordered_sum *sums;
  713. sums = list_entry(ordered_sums.next,
  714. struct btrfs_ordered_sum,
  715. list);
  716. if (!ret)
  717. ret = btrfs_del_csums(trans, fs_info,
  718. sums->bytenr,
  719. sums->len);
  720. if (!ret)
  721. ret = btrfs_csum_file_blocks(trans,
  722. fs_info->csum_root, sums);
  723. list_del(&sums->list);
  724. kfree(sums);
  725. }
  726. if (ret)
  727. goto out;
  728. } else {
  729. btrfs_release_path(path);
  730. }
  731. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  732. /* inline extents are easy, we just overwrite them */
  733. ret = overwrite_item(trans, root, path, eb, slot, key);
  734. if (ret)
  735. goto out;
  736. }
  737. inode_add_bytes(inode, nbytes);
  738. update_inode:
  739. ret = btrfs_update_inode(trans, root, inode);
  740. out:
  741. if (inode)
  742. iput(inode);
  743. return ret;
  744. }
  745. /*
  746. * when cleaning up conflicts between the directory names in the
  747. * subvolume, directory names in the log and directory names in the
  748. * inode back references, we may have to unlink inodes from directories.
  749. *
  750. * This is a helper function to do the unlink of a specific directory
  751. * item
  752. */
  753. static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
  754. struct btrfs_root *root,
  755. struct btrfs_path *path,
  756. struct btrfs_inode *dir,
  757. struct btrfs_dir_item *di)
  758. {
  759. struct inode *inode;
  760. char *name;
  761. int name_len;
  762. struct extent_buffer *leaf;
  763. struct btrfs_key location;
  764. int ret;
  765. leaf = path->nodes[0];
  766. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  767. name_len = btrfs_dir_name_len(leaf, di);
  768. name = kmalloc(name_len, GFP_NOFS);
  769. if (!name)
  770. return -ENOMEM;
  771. read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
  772. btrfs_release_path(path);
  773. inode = read_one_inode(root, location.objectid);
  774. if (!inode) {
  775. ret = -EIO;
  776. goto out;
  777. }
  778. ret = link_to_fixup_dir(trans, root, path, location.objectid);
  779. if (ret)
  780. goto out;
  781. ret = btrfs_unlink_inode(trans, root, dir, BTRFS_I(inode), name,
  782. name_len);
  783. if (ret)
  784. goto out;
  785. else
  786. ret = btrfs_run_delayed_items(trans);
  787. out:
  788. kfree(name);
  789. iput(inode);
  790. return ret;
  791. }
  792. /*
  793. * helper function to see if a given name and sequence number found
  794. * in an inode back reference are already in a directory and correctly
  795. * point to this inode
  796. */
  797. static noinline int inode_in_dir(struct btrfs_root *root,
  798. struct btrfs_path *path,
  799. u64 dirid, u64 objectid, u64 index,
  800. const char *name, int name_len)
  801. {
  802. struct btrfs_dir_item *di;
  803. struct btrfs_key location;
  804. int match = 0;
  805. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  806. index, name, name_len, 0);
  807. if (di && !IS_ERR(di)) {
  808. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  809. if (location.objectid != objectid)
  810. goto out;
  811. } else
  812. goto out;
  813. btrfs_release_path(path);
  814. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
  815. if (di && !IS_ERR(di)) {
  816. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  817. if (location.objectid != objectid)
  818. goto out;
  819. } else
  820. goto out;
  821. match = 1;
  822. out:
  823. btrfs_release_path(path);
  824. return match;
  825. }
  826. /*
  827. * helper function to check a log tree for a named back reference in
  828. * an inode. This is used to decide if a back reference that is
  829. * found in the subvolume conflicts with what we find in the log.
  830. *
  831. * inode backreferences may have multiple refs in a single item,
  832. * during replay we process one reference at a time, and we don't
  833. * want to delete valid links to a file from the subvolume if that
  834. * link is also in the log.
  835. */
  836. static noinline int backref_in_log(struct btrfs_root *log,
  837. struct btrfs_key *key,
  838. u64 ref_objectid,
  839. const char *name, int namelen)
  840. {
  841. struct btrfs_path *path;
  842. struct btrfs_inode_ref *ref;
  843. unsigned long ptr;
  844. unsigned long ptr_end;
  845. unsigned long name_ptr;
  846. int found_name_len;
  847. int item_size;
  848. int ret;
  849. int match = 0;
  850. path = btrfs_alloc_path();
  851. if (!path)
  852. return -ENOMEM;
  853. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  854. if (ret != 0)
  855. goto out;
  856. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  857. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  858. if (btrfs_find_name_in_ext_backref(path->nodes[0],
  859. path->slots[0],
  860. ref_objectid,
  861. name, namelen, NULL))
  862. match = 1;
  863. goto out;
  864. }
  865. item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  866. ptr_end = ptr + item_size;
  867. while (ptr < ptr_end) {
  868. ref = (struct btrfs_inode_ref *)ptr;
  869. found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
  870. if (found_name_len == namelen) {
  871. name_ptr = (unsigned long)(ref + 1);
  872. ret = memcmp_extent_buffer(path->nodes[0], name,
  873. name_ptr, namelen);
  874. if (ret == 0) {
  875. match = 1;
  876. goto out;
  877. }
  878. }
  879. ptr = (unsigned long)(ref + 1) + found_name_len;
  880. }
  881. out:
  882. btrfs_free_path(path);
  883. return match;
  884. }
  885. static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
  886. struct btrfs_root *root,
  887. struct btrfs_path *path,
  888. struct btrfs_root *log_root,
  889. struct btrfs_inode *dir,
  890. struct btrfs_inode *inode,
  891. u64 inode_objectid, u64 parent_objectid,
  892. u64 ref_index, char *name, int namelen,
  893. int *search_done)
  894. {
  895. int ret;
  896. char *victim_name;
  897. int victim_name_len;
  898. struct extent_buffer *leaf;
  899. struct btrfs_dir_item *di;
  900. struct btrfs_key search_key;
  901. struct btrfs_inode_extref *extref;
  902. again:
  903. /* Search old style refs */
  904. search_key.objectid = inode_objectid;
  905. search_key.type = BTRFS_INODE_REF_KEY;
  906. search_key.offset = parent_objectid;
  907. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  908. if (ret == 0) {
  909. struct btrfs_inode_ref *victim_ref;
  910. unsigned long ptr;
  911. unsigned long ptr_end;
  912. leaf = path->nodes[0];
  913. /* are we trying to overwrite a back ref for the root directory
  914. * if so, just jump out, we're done
  915. */
  916. if (search_key.objectid == search_key.offset)
  917. return 1;
  918. /* check all the names in this back reference to see
  919. * if they are in the log. if so, we allow them to stay
  920. * otherwise they must be unlinked as a conflict
  921. */
  922. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  923. ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
  924. while (ptr < ptr_end) {
  925. victim_ref = (struct btrfs_inode_ref *)ptr;
  926. victim_name_len = btrfs_inode_ref_name_len(leaf,
  927. victim_ref);
  928. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  929. if (!victim_name)
  930. return -ENOMEM;
  931. read_extent_buffer(leaf, victim_name,
  932. (unsigned long)(victim_ref + 1),
  933. victim_name_len);
  934. if (!backref_in_log(log_root, &search_key,
  935. parent_objectid,
  936. victim_name,
  937. victim_name_len)) {
  938. inc_nlink(&inode->vfs_inode);
  939. btrfs_release_path(path);
  940. ret = btrfs_unlink_inode(trans, root, dir, inode,
  941. victim_name, victim_name_len);
  942. kfree(victim_name);
  943. if (ret)
  944. return ret;
  945. ret = btrfs_run_delayed_items(trans);
  946. if (ret)
  947. return ret;
  948. *search_done = 1;
  949. goto again;
  950. }
  951. kfree(victim_name);
  952. ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
  953. }
  954. /*
  955. * NOTE: we have searched root tree and checked the
  956. * corresponding ref, it does not need to check again.
  957. */
  958. *search_done = 1;
  959. }
  960. btrfs_release_path(path);
  961. /* Same search but for extended refs */
  962. extref = btrfs_lookup_inode_extref(NULL, root, path, name, namelen,
  963. inode_objectid, parent_objectid, 0,
  964. 0);
  965. if (!IS_ERR_OR_NULL(extref)) {
  966. u32 item_size;
  967. u32 cur_offset = 0;
  968. unsigned long base;
  969. struct inode *victim_parent;
  970. leaf = path->nodes[0];
  971. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  972. base = btrfs_item_ptr_offset(leaf, path->slots[0]);
  973. while (cur_offset < item_size) {
  974. extref = (struct btrfs_inode_extref *)(base + cur_offset);
  975. victim_name_len = btrfs_inode_extref_name_len(leaf, extref);
  976. if (btrfs_inode_extref_parent(leaf, extref) != parent_objectid)
  977. goto next;
  978. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  979. if (!victim_name)
  980. return -ENOMEM;
  981. read_extent_buffer(leaf, victim_name, (unsigned long)&extref->name,
  982. victim_name_len);
  983. search_key.objectid = inode_objectid;
  984. search_key.type = BTRFS_INODE_EXTREF_KEY;
  985. search_key.offset = btrfs_extref_hash(parent_objectid,
  986. victim_name,
  987. victim_name_len);
  988. ret = 0;
  989. if (!backref_in_log(log_root, &search_key,
  990. parent_objectid, victim_name,
  991. victim_name_len)) {
  992. ret = -ENOENT;
  993. victim_parent = read_one_inode(root,
  994. parent_objectid);
  995. if (victim_parent) {
  996. inc_nlink(&inode->vfs_inode);
  997. btrfs_release_path(path);
  998. ret = btrfs_unlink_inode(trans, root,
  999. BTRFS_I(victim_parent),
  1000. inode,
  1001. victim_name,
  1002. victim_name_len);
  1003. if (!ret)
  1004. ret = btrfs_run_delayed_items(
  1005. trans);
  1006. }
  1007. iput(victim_parent);
  1008. kfree(victim_name);
  1009. if (ret)
  1010. return ret;
  1011. *search_done = 1;
  1012. goto again;
  1013. }
  1014. kfree(victim_name);
  1015. next:
  1016. cur_offset += victim_name_len + sizeof(*extref);
  1017. }
  1018. *search_done = 1;
  1019. }
  1020. btrfs_release_path(path);
  1021. /* look for a conflicting sequence number */
  1022. di = btrfs_lookup_dir_index_item(trans, root, path, btrfs_ino(dir),
  1023. ref_index, name, namelen, 0);
  1024. if (di && !IS_ERR(di)) {
  1025. ret = drop_one_dir_item(trans, root, path, dir, di);
  1026. if (ret)
  1027. return ret;
  1028. }
  1029. btrfs_release_path(path);
  1030. /* look for a conflicing name */
  1031. di = btrfs_lookup_dir_item(trans, root, path, btrfs_ino(dir),
  1032. name, namelen, 0);
  1033. if (di && !IS_ERR(di)) {
  1034. ret = drop_one_dir_item(trans, root, path, dir, di);
  1035. if (ret)
  1036. return ret;
  1037. }
  1038. btrfs_release_path(path);
  1039. return 0;
  1040. }
  1041. static int extref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1042. u32 *namelen, char **name, u64 *index,
  1043. u64 *parent_objectid)
  1044. {
  1045. struct btrfs_inode_extref *extref;
  1046. extref = (struct btrfs_inode_extref *)ref_ptr;
  1047. *namelen = btrfs_inode_extref_name_len(eb, extref);
  1048. *name = kmalloc(*namelen, GFP_NOFS);
  1049. if (*name == NULL)
  1050. return -ENOMEM;
  1051. read_extent_buffer(eb, *name, (unsigned long)&extref->name,
  1052. *namelen);
  1053. if (index)
  1054. *index = btrfs_inode_extref_index(eb, extref);
  1055. if (parent_objectid)
  1056. *parent_objectid = btrfs_inode_extref_parent(eb, extref);
  1057. return 0;
  1058. }
  1059. static int ref_get_fields(struct extent_buffer *eb, unsigned long ref_ptr,
  1060. u32 *namelen, char **name, u64 *index)
  1061. {
  1062. struct btrfs_inode_ref *ref;
  1063. ref = (struct btrfs_inode_ref *)ref_ptr;
  1064. *namelen = btrfs_inode_ref_name_len(eb, ref);
  1065. *name = kmalloc(*namelen, GFP_NOFS);
  1066. if (*name == NULL)
  1067. return -ENOMEM;
  1068. read_extent_buffer(eb, *name, (unsigned long)(ref + 1), *namelen);
  1069. if (index)
  1070. *index = btrfs_inode_ref_index(eb, ref);
  1071. return 0;
  1072. }
  1073. /*
  1074. * Take an inode reference item from the log tree and iterate all names from the
  1075. * inode reference item in the subvolume tree with the same key (if it exists).
  1076. * For any name that is not in the inode reference item from the log tree, do a
  1077. * proper unlink of that name (that is, remove its entry from the inode
  1078. * reference item and both dir index keys).
  1079. */
  1080. static int unlink_old_inode_refs(struct btrfs_trans_handle *trans,
  1081. struct btrfs_root *root,
  1082. struct btrfs_path *path,
  1083. struct btrfs_inode *inode,
  1084. struct extent_buffer *log_eb,
  1085. int log_slot,
  1086. struct btrfs_key *key)
  1087. {
  1088. int ret;
  1089. unsigned long ref_ptr;
  1090. unsigned long ref_end;
  1091. struct extent_buffer *eb;
  1092. again:
  1093. btrfs_release_path(path);
  1094. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  1095. if (ret > 0) {
  1096. ret = 0;
  1097. goto out;
  1098. }
  1099. if (ret < 0)
  1100. goto out;
  1101. eb = path->nodes[0];
  1102. ref_ptr = btrfs_item_ptr_offset(eb, path->slots[0]);
  1103. ref_end = ref_ptr + btrfs_item_size_nr(eb, path->slots[0]);
  1104. while (ref_ptr < ref_end) {
  1105. char *name = NULL;
  1106. int namelen;
  1107. u64 parent_id;
  1108. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1109. ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
  1110. NULL, &parent_id);
  1111. } else {
  1112. parent_id = key->offset;
  1113. ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
  1114. NULL);
  1115. }
  1116. if (ret)
  1117. goto out;
  1118. if (key->type == BTRFS_INODE_EXTREF_KEY)
  1119. ret = btrfs_find_name_in_ext_backref(log_eb, log_slot,
  1120. parent_id, name,
  1121. namelen, NULL);
  1122. else
  1123. ret = btrfs_find_name_in_backref(log_eb, log_slot, name,
  1124. namelen, NULL);
  1125. if (!ret) {
  1126. struct inode *dir;
  1127. btrfs_release_path(path);
  1128. dir = read_one_inode(root, parent_id);
  1129. if (!dir) {
  1130. ret = -ENOENT;
  1131. kfree(name);
  1132. goto out;
  1133. }
  1134. ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
  1135. inode, name, namelen);
  1136. kfree(name);
  1137. iput(dir);
  1138. if (ret)
  1139. goto out;
  1140. goto again;
  1141. }
  1142. kfree(name);
  1143. ref_ptr += namelen;
  1144. if (key->type == BTRFS_INODE_EXTREF_KEY)
  1145. ref_ptr += sizeof(struct btrfs_inode_extref);
  1146. else
  1147. ref_ptr += sizeof(struct btrfs_inode_ref);
  1148. }
  1149. ret = 0;
  1150. out:
  1151. btrfs_release_path(path);
  1152. return ret;
  1153. }
  1154. static int btrfs_inode_ref_exists(struct inode *inode, struct inode *dir,
  1155. const u8 ref_type, const char *name,
  1156. const int namelen)
  1157. {
  1158. struct btrfs_key key;
  1159. struct btrfs_path *path;
  1160. const u64 parent_id = btrfs_ino(BTRFS_I(dir));
  1161. int ret;
  1162. path = btrfs_alloc_path();
  1163. if (!path)
  1164. return -ENOMEM;
  1165. key.objectid = btrfs_ino(BTRFS_I(inode));
  1166. key.type = ref_type;
  1167. if (key.type == BTRFS_INODE_REF_KEY)
  1168. key.offset = parent_id;
  1169. else
  1170. key.offset = btrfs_extref_hash(parent_id, name, namelen);
  1171. ret = btrfs_search_slot(NULL, BTRFS_I(inode)->root, &key, path, 0, 0);
  1172. if (ret < 0)
  1173. goto out;
  1174. if (ret > 0) {
  1175. ret = 0;
  1176. goto out;
  1177. }
  1178. if (key.type == BTRFS_INODE_EXTREF_KEY)
  1179. ret = btrfs_find_name_in_ext_backref(path->nodes[0],
  1180. path->slots[0], parent_id,
  1181. name, namelen, NULL);
  1182. else
  1183. ret = btrfs_find_name_in_backref(path->nodes[0], path->slots[0],
  1184. name, namelen, NULL);
  1185. out:
  1186. btrfs_free_path(path);
  1187. return ret;
  1188. }
  1189. /*
  1190. * replay one inode back reference item found in the log tree.
  1191. * eb, slot and key refer to the buffer and key found in the log tree.
  1192. * root is the destination we are replaying into, and path is for temp
  1193. * use by this function. (it should be released on return).
  1194. */
  1195. static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
  1196. struct btrfs_root *root,
  1197. struct btrfs_root *log,
  1198. struct btrfs_path *path,
  1199. struct extent_buffer *eb, int slot,
  1200. struct btrfs_key *key)
  1201. {
  1202. struct inode *dir = NULL;
  1203. struct inode *inode = NULL;
  1204. unsigned long ref_ptr;
  1205. unsigned long ref_end;
  1206. char *name = NULL;
  1207. int namelen;
  1208. int ret;
  1209. int search_done = 0;
  1210. int log_ref_ver = 0;
  1211. u64 parent_objectid;
  1212. u64 inode_objectid;
  1213. u64 ref_index = 0;
  1214. int ref_struct_size;
  1215. ref_ptr = btrfs_item_ptr_offset(eb, slot);
  1216. ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
  1217. if (key->type == BTRFS_INODE_EXTREF_KEY) {
  1218. struct btrfs_inode_extref *r;
  1219. ref_struct_size = sizeof(struct btrfs_inode_extref);
  1220. log_ref_ver = 1;
  1221. r = (struct btrfs_inode_extref *)ref_ptr;
  1222. parent_objectid = btrfs_inode_extref_parent(eb, r);
  1223. } else {
  1224. ref_struct_size = sizeof(struct btrfs_inode_ref);
  1225. parent_objectid = key->offset;
  1226. }
  1227. inode_objectid = key->objectid;
  1228. /*
  1229. * it is possible that we didn't log all the parent directories
  1230. * for a given inode. If we don't find the dir, just don't
  1231. * copy the back ref in. The link count fixup code will take
  1232. * care of the rest
  1233. */
  1234. dir = read_one_inode(root, parent_objectid);
  1235. if (!dir) {
  1236. ret = -ENOENT;
  1237. goto out;
  1238. }
  1239. inode = read_one_inode(root, inode_objectid);
  1240. if (!inode) {
  1241. ret = -EIO;
  1242. goto out;
  1243. }
  1244. while (ref_ptr < ref_end) {
  1245. if (log_ref_ver) {
  1246. ret = extref_get_fields(eb, ref_ptr, &namelen, &name,
  1247. &ref_index, &parent_objectid);
  1248. /*
  1249. * parent object can change from one array
  1250. * item to another.
  1251. */
  1252. if (!dir)
  1253. dir = read_one_inode(root, parent_objectid);
  1254. if (!dir) {
  1255. ret = -ENOENT;
  1256. goto out;
  1257. }
  1258. } else {
  1259. ret = ref_get_fields(eb, ref_ptr, &namelen, &name,
  1260. &ref_index);
  1261. }
  1262. if (ret)
  1263. goto out;
  1264. /* if we already have a perfect match, we're done */
  1265. if (!inode_in_dir(root, path, btrfs_ino(BTRFS_I(dir)),
  1266. btrfs_ino(BTRFS_I(inode)), ref_index,
  1267. name, namelen)) {
  1268. /*
  1269. * look for a conflicting back reference in the
  1270. * metadata. if we find one we have to unlink that name
  1271. * of the file before we add our new link. Later on, we
  1272. * overwrite any existing back reference, and we don't
  1273. * want to create dangling pointers in the directory.
  1274. */
  1275. if (!search_done) {
  1276. ret = __add_inode_ref(trans, root, path, log,
  1277. BTRFS_I(dir),
  1278. BTRFS_I(inode),
  1279. inode_objectid,
  1280. parent_objectid,
  1281. ref_index, name, namelen,
  1282. &search_done);
  1283. if (ret) {
  1284. if (ret == 1)
  1285. ret = 0;
  1286. goto out;
  1287. }
  1288. }
  1289. /*
  1290. * If a reference item already exists for this inode
  1291. * with the same parent and name, but different index,
  1292. * drop it and the corresponding directory index entries
  1293. * from the parent before adding the new reference item
  1294. * and dir index entries, otherwise we would fail with
  1295. * -EEXIST returned from btrfs_add_link() below.
  1296. */
  1297. ret = btrfs_inode_ref_exists(inode, dir, key->type,
  1298. name, namelen);
  1299. if (ret > 0) {
  1300. ret = btrfs_unlink_inode(trans, root,
  1301. BTRFS_I(dir),
  1302. BTRFS_I(inode),
  1303. name, namelen);
  1304. /*
  1305. * If we dropped the link count to 0, bump it so
  1306. * that later the iput() on the inode will not
  1307. * free it. We will fixup the link count later.
  1308. */
  1309. if (!ret && inode->i_nlink == 0)
  1310. inc_nlink(inode);
  1311. }
  1312. if (ret < 0)
  1313. goto out;
  1314. /* insert our name */
  1315. ret = btrfs_add_link(trans, BTRFS_I(dir),
  1316. BTRFS_I(inode),
  1317. name, namelen, 0, ref_index);
  1318. if (ret)
  1319. goto out;
  1320. btrfs_update_inode(trans, root, inode);
  1321. }
  1322. ref_ptr = (unsigned long)(ref_ptr + ref_struct_size) + namelen;
  1323. kfree(name);
  1324. name = NULL;
  1325. if (log_ref_ver) {
  1326. iput(dir);
  1327. dir = NULL;
  1328. }
  1329. }
  1330. /*
  1331. * Before we overwrite the inode reference item in the subvolume tree
  1332. * with the item from the log tree, we must unlink all names from the
  1333. * parent directory that are in the subvolume's tree inode reference
  1334. * item, otherwise we end up with an inconsistent subvolume tree where
  1335. * dir index entries exist for a name but there is no inode reference
  1336. * item with the same name.
  1337. */
  1338. ret = unlink_old_inode_refs(trans, root, path, BTRFS_I(inode), eb, slot,
  1339. key);
  1340. if (ret)
  1341. goto out;
  1342. /* finally write the back reference in the inode */
  1343. ret = overwrite_item(trans, root, path, eb, slot, key);
  1344. out:
  1345. btrfs_release_path(path);
  1346. kfree(name);
  1347. iput(dir);
  1348. iput(inode);
  1349. return ret;
  1350. }
  1351. static int insert_orphan_item(struct btrfs_trans_handle *trans,
  1352. struct btrfs_root *root, u64 ino)
  1353. {
  1354. int ret;
  1355. ret = btrfs_insert_orphan_item(trans, root, ino);
  1356. if (ret == -EEXIST)
  1357. ret = 0;
  1358. return ret;
  1359. }
  1360. static int count_inode_extrefs(struct btrfs_root *root,
  1361. struct btrfs_inode *inode, struct btrfs_path *path)
  1362. {
  1363. int ret = 0;
  1364. int name_len;
  1365. unsigned int nlink = 0;
  1366. u32 item_size;
  1367. u32 cur_offset = 0;
  1368. u64 inode_objectid = btrfs_ino(inode);
  1369. u64 offset = 0;
  1370. unsigned long ptr;
  1371. struct btrfs_inode_extref *extref;
  1372. struct extent_buffer *leaf;
  1373. while (1) {
  1374. ret = btrfs_find_one_extref(root, inode_objectid, offset, path,
  1375. &extref, &offset);
  1376. if (ret)
  1377. break;
  1378. leaf = path->nodes[0];
  1379. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  1380. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1381. cur_offset = 0;
  1382. while (cur_offset < item_size) {
  1383. extref = (struct btrfs_inode_extref *) (ptr + cur_offset);
  1384. name_len = btrfs_inode_extref_name_len(leaf, extref);
  1385. nlink++;
  1386. cur_offset += name_len + sizeof(*extref);
  1387. }
  1388. offset++;
  1389. btrfs_release_path(path);
  1390. }
  1391. btrfs_release_path(path);
  1392. if (ret < 0 && ret != -ENOENT)
  1393. return ret;
  1394. return nlink;
  1395. }
  1396. static int count_inode_refs(struct btrfs_root *root,
  1397. struct btrfs_inode *inode, struct btrfs_path *path)
  1398. {
  1399. int ret;
  1400. struct btrfs_key key;
  1401. unsigned int nlink = 0;
  1402. unsigned long ptr;
  1403. unsigned long ptr_end;
  1404. int name_len;
  1405. u64 ino = btrfs_ino(inode);
  1406. key.objectid = ino;
  1407. key.type = BTRFS_INODE_REF_KEY;
  1408. key.offset = (u64)-1;
  1409. while (1) {
  1410. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1411. if (ret < 0)
  1412. break;
  1413. if (ret > 0) {
  1414. if (path->slots[0] == 0)
  1415. break;
  1416. path->slots[0]--;
  1417. }
  1418. process_slot:
  1419. btrfs_item_key_to_cpu(path->nodes[0], &key,
  1420. path->slots[0]);
  1421. if (key.objectid != ino ||
  1422. key.type != BTRFS_INODE_REF_KEY)
  1423. break;
  1424. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  1425. ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
  1426. path->slots[0]);
  1427. while (ptr < ptr_end) {
  1428. struct btrfs_inode_ref *ref;
  1429. ref = (struct btrfs_inode_ref *)ptr;
  1430. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  1431. ref);
  1432. ptr = (unsigned long)(ref + 1) + name_len;
  1433. nlink++;
  1434. }
  1435. if (key.offset == 0)
  1436. break;
  1437. if (path->slots[0] > 0) {
  1438. path->slots[0]--;
  1439. goto process_slot;
  1440. }
  1441. key.offset--;
  1442. btrfs_release_path(path);
  1443. }
  1444. btrfs_release_path(path);
  1445. return nlink;
  1446. }
  1447. /*
  1448. * There are a few corners where the link count of the file can't
  1449. * be properly maintained during replay. So, instead of adding
  1450. * lots of complexity to the log code, we just scan the backrefs
  1451. * for any file that has been through replay.
  1452. *
  1453. * The scan will update the link count on the inode to reflect the
  1454. * number of back refs found. If it goes down to zero, the iput
  1455. * will free the inode.
  1456. */
  1457. static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
  1458. struct btrfs_root *root,
  1459. struct inode *inode)
  1460. {
  1461. struct btrfs_path *path;
  1462. int ret;
  1463. u64 nlink = 0;
  1464. u64 ino = btrfs_ino(BTRFS_I(inode));
  1465. path = btrfs_alloc_path();
  1466. if (!path)
  1467. return -ENOMEM;
  1468. ret = count_inode_refs(root, BTRFS_I(inode), path);
  1469. if (ret < 0)
  1470. goto out;
  1471. nlink = ret;
  1472. ret = count_inode_extrefs(root, BTRFS_I(inode), path);
  1473. if (ret < 0)
  1474. goto out;
  1475. nlink += ret;
  1476. ret = 0;
  1477. if (nlink != inode->i_nlink) {
  1478. set_nlink(inode, nlink);
  1479. btrfs_update_inode(trans, root, inode);
  1480. }
  1481. BTRFS_I(inode)->index_cnt = (u64)-1;
  1482. if (inode->i_nlink == 0) {
  1483. if (S_ISDIR(inode->i_mode)) {
  1484. ret = replay_dir_deletes(trans, root, NULL, path,
  1485. ino, 1);
  1486. if (ret)
  1487. goto out;
  1488. }
  1489. ret = insert_orphan_item(trans, root, ino);
  1490. }
  1491. out:
  1492. btrfs_free_path(path);
  1493. return ret;
  1494. }
  1495. static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
  1496. struct btrfs_root *root,
  1497. struct btrfs_path *path)
  1498. {
  1499. int ret;
  1500. struct btrfs_key key;
  1501. struct inode *inode;
  1502. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1503. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1504. key.offset = (u64)-1;
  1505. while (1) {
  1506. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1507. if (ret < 0)
  1508. break;
  1509. if (ret == 1) {
  1510. if (path->slots[0] == 0)
  1511. break;
  1512. path->slots[0]--;
  1513. }
  1514. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1515. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  1516. key.type != BTRFS_ORPHAN_ITEM_KEY)
  1517. break;
  1518. ret = btrfs_del_item(trans, root, path);
  1519. if (ret)
  1520. goto out;
  1521. btrfs_release_path(path);
  1522. inode = read_one_inode(root, key.offset);
  1523. if (!inode)
  1524. return -EIO;
  1525. ret = fixup_inode_link_count(trans, root, inode);
  1526. iput(inode);
  1527. if (ret)
  1528. goto out;
  1529. /*
  1530. * fixup on a directory may create new entries,
  1531. * make sure we always look for the highset possible
  1532. * offset
  1533. */
  1534. key.offset = (u64)-1;
  1535. }
  1536. ret = 0;
  1537. out:
  1538. btrfs_release_path(path);
  1539. return ret;
  1540. }
  1541. /*
  1542. * record a given inode in the fixup dir so we can check its link
  1543. * count when replay is done. The link count is incremented here
  1544. * so the inode won't go away until we check it
  1545. */
  1546. static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  1547. struct btrfs_root *root,
  1548. struct btrfs_path *path,
  1549. u64 objectid)
  1550. {
  1551. struct btrfs_key key;
  1552. int ret = 0;
  1553. struct inode *inode;
  1554. inode = read_one_inode(root, objectid);
  1555. if (!inode)
  1556. return -EIO;
  1557. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  1558. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1559. key.offset = objectid;
  1560. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  1561. btrfs_release_path(path);
  1562. if (ret == 0) {
  1563. if (!inode->i_nlink)
  1564. set_nlink(inode, 1);
  1565. else
  1566. inc_nlink(inode);
  1567. ret = btrfs_update_inode(trans, root, inode);
  1568. } else if (ret == -EEXIST) {
  1569. ret = 0;
  1570. } else {
  1571. BUG(); /* Logic Error */
  1572. }
  1573. iput(inode);
  1574. return ret;
  1575. }
  1576. /*
  1577. * when replaying the log for a directory, we only insert names
  1578. * for inodes that actually exist. This means an fsync on a directory
  1579. * does not implicitly fsync all the new files in it
  1580. */
  1581. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  1582. struct btrfs_root *root,
  1583. u64 dirid, u64 index,
  1584. char *name, int name_len,
  1585. struct btrfs_key *location)
  1586. {
  1587. struct inode *inode;
  1588. struct inode *dir;
  1589. int ret;
  1590. inode = read_one_inode(root, location->objectid);
  1591. if (!inode)
  1592. return -ENOENT;
  1593. dir = read_one_inode(root, dirid);
  1594. if (!dir) {
  1595. iput(inode);
  1596. return -EIO;
  1597. }
  1598. ret = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode), name,
  1599. name_len, 1, index);
  1600. /* FIXME, put inode into FIXUP list */
  1601. iput(inode);
  1602. iput(dir);
  1603. return ret;
  1604. }
  1605. /*
  1606. * Return true if an inode reference exists in the log for the given name,
  1607. * inode and parent inode.
  1608. */
  1609. static bool name_in_log_ref(struct btrfs_root *log_root,
  1610. const char *name, const int name_len,
  1611. const u64 dirid, const u64 ino)
  1612. {
  1613. struct btrfs_key search_key;
  1614. search_key.objectid = ino;
  1615. search_key.type = BTRFS_INODE_REF_KEY;
  1616. search_key.offset = dirid;
  1617. if (backref_in_log(log_root, &search_key, dirid, name, name_len))
  1618. return true;
  1619. search_key.type = BTRFS_INODE_EXTREF_KEY;
  1620. search_key.offset = btrfs_extref_hash(dirid, name, name_len);
  1621. if (backref_in_log(log_root, &search_key, dirid, name, name_len))
  1622. return true;
  1623. return false;
  1624. }
  1625. /*
  1626. * take a single entry in a log directory item and replay it into
  1627. * the subvolume.
  1628. *
  1629. * if a conflicting item exists in the subdirectory already,
  1630. * the inode it points to is unlinked and put into the link count
  1631. * fix up tree.
  1632. *
  1633. * If a name from the log points to a file or directory that does
  1634. * not exist in the FS, it is skipped. fsyncs on directories
  1635. * do not force down inodes inside that directory, just changes to the
  1636. * names or unlinks in a directory.
  1637. *
  1638. * Returns < 0 on error, 0 if the name wasn't replayed (dentry points to a
  1639. * non-existing inode) and 1 if the name was replayed.
  1640. */
  1641. static noinline int replay_one_name(struct btrfs_trans_handle *trans,
  1642. struct btrfs_root *root,
  1643. struct btrfs_path *path,
  1644. struct extent_buffer *eb,
  1645. struct btrfs_dir_item *di,
  1646. struct btrfs_key *key)
  1647. {
  1648. char *name;
  1649. int name_len;
  1650. struct btrfs_dir_item *dst_di;
  1651. struct btrfs_key found_key;
  1652. struct btrfs_key log_key;
  1653. struct inode *dir;
  1654. u8 log_type;
  1655. int exists;
  1656. int ret = 0;
  1657. bool update_size = (key->type == BTRFS_DIR_INDEX_KEY);
  1658. bool name_added = false;
  1659. dir = read_one_inode(root, key->objectid);
  1660. if (!dir)
  1661. return -EIO;
  1662. name_len = btrfs_dir_name_len(eb, di);
  1663. name = kmalloc(name_len, GFP_NOFS);
  1664. if (!name) {
  1665. ret = -ENOMEM;
  1666. goto out;
  1667. }
  1668. log_type = btrfs_dir_type(eb, di);
  1669. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1670. name_len);
  1671. btrfs_dir_item_key_to_cpu(eb, di, &log_key);
  1672. exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
  1673. if (exists == 0)
  1674. exists = 1;
  1675. else
  1676. exists = 0;
  1677. btrfs_release_path(path);
  1678. if (key->type == BTRFS_DIR_ITEM_KEY) {
  1679. dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
  1680. name, name_len, 1);
  1681. } else if (key->type == BTRFS_DIR_INDEX_KEY) {
  1682. dst_di = btrfs_lookup_dir_index_item(trans, root, path,
  1683. key->objectid,
  1684. key->offset, name,
  1685. name_len, 1);
  1686. } else {
  1687. /* Corruption */
  1688. ret = -EINVAL;
  1689. goto out;
  1690. }
  1691. if (IS_ERR_OR_NULL(dst_di)) {
  1692. /* we need a sequence number to insert, so we only
  1693. * do inserts for the BTRFS_DIR_INDEX_KEY types
  1694. */
  1695. if (key->type != BTRFS_DIR_INDEX_KEY)
  1696. goto out;
  1697. goto insert;
  1698. }
  1699. btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
  1700. /* the existing item matches the logged item */
  1701. if (found_key.objectid == log_key.objectid &&
  1702. found_key.type == log_key.type &&
  1703. found_key.offset == log_key.offset &&
  1704. btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
  1705. update_size = false;
  1706. goto out;
  1707. }
  1708. /*
  1709. * don't drop the conflicting directory entry if the inode
  1710. * for the new entry doesn't exist
  1711. */
  1712. if (!exists)
  1713. goto out;
  1714. ret = drop_one_dir_item(trans, root, path, BTRFS_I(dir), dst_di);
  1715. if (ret)
  1716. goto out;
  1717. if (key->type == BTRFS_DIR_INDEX_KEY)
  1718. goto insert;
  1719. out:
  1720. btrfs_release_path(path);
  1721. if (!ret && update_size) {
  1722. btrfs_i_size_write(BTRFS_I(dir), dir->i_size + name_len * 2);
  1723. ret = btrfs_update_inode(trans, root, dir);
  1724. }
  1725. kfree(name);
  1726. iput(dir);
  1727. if (!ret && name_added)
  1728. ret = 1;
  1729. return ret;
  1730. insert:
  1731. if (name_in_log_ref(root->log_root, name, name_len,
  1732. key->objectid, log_key.objectid)) {
  1733. /* The dentry will be added later. */
  1734. ret = 0;
  1735. update_size = false;
  1736. goto out;
  1737. }
  1738. btrfs_release_path(path);
  1739. ret = insert_one_name(trans, root, key->objectid, key->offset,
  1740. name, name_len, &log_key);
  1741. if (ret && ret != -ENOENT && ret != -EEXIST)
  1742. goto out;
  1743. if (!ret)
  1744. name_added = true;
  1745. update_size = false;
  1746. ret = 0;
  1747. goto out;
  1748. }
  1749. /*
  1750. * find all the names in a directory item and reconcile them into
  1751. * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
  1752. * one name in a directory item, but the same code gets used for
  1753. * both directory index types
  1754. */
  1755. static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
  1756. struct btrfs_root *root,
  1757. struct btrfs_path *path,
  1758. struct extent_buffer *eb, int slot,
  1759. struct btrfs_key *key)
  1760. {
  1761. int ret = 0;
  1762. u32 item_size = btrfs_item_size_nr(eb, slot);
  1763. struct btrfs_dir_item *di;
  1764. int name_len;
  1765. unsigned long ptr;
  1766. unsigned long ptr_end;
  1767. struct btrfs_path *fixup_path = NULL;
  1768. ptr = btrfs_item_ptr_offset(eb, slot);
  1769. ptr_end = ptr + item_size;
  1770. while (ptr < ptr_end) {
  1771. di = (struct btrfs_dir_item *)ptr;
  1772. name_len = btrfs_dir_name_len(eb, di);
  1773. ret = replay_one_name(trans, root, path, eb, di, key);
  1774. if (ret < 0)
  1775. break;
  1776. ptr = (unsigned long)(di + 1);
  1777. ptr += name_len;
  1778. /*
  1779. * If this entry refers to a non-directory (directories can not
  1780. * have a link count > 1) and it was added in the transaction
  1781. * that was not committed, make sure we fixup the link count of
  1782. * the inode it the entry points to. Otherwise something like
  1783. * the following would result in a directory pointing to an
  1784. * inode with a wrong link that does not account for this dir
  1785. * entry:
  1786. *
  1787. * mkdir testdir
  1788. * touch testdir/foo
  1789. * touch testdir/bar
  1790. * sync
  1791. *
  1792. * ln testdir/bar testdir/bar_link
  1793. * ln testdir/foo testdir/foo_link
  1794. * xfs_io -c "fsync" testdir/bar
  1795. *
  1796. * <power failure>
  1797. *
  1798. * mount fs, log replay happens
  1799. *
  1800. * File foo would remain with a link count of 1 when it has two
  1801. * entries pointing to it in the directory testdir. This would
  1802. * make it impossible to ever delete the parent directory has
  1803. * it would result in stale dentries that can never be deleted.
  1804. */
  1805. if (ret == 1 && btrfs_dir_type(eb, di) != BTRFS_FT_DIR) {
  1806. struct btrfs_key di_key;
  1807. if (!fixup_path) {
  1808. fixup_path = btrfs_alloc_path();
  1809. if (!fixup_path) {
  1810. ret = -ENOMEM;
  1811. break;
  1812. }
  1813. }
  1814. btrfs_dir_item_key_to_cpu(eb, di, &di_key);
  1815. ret = link_to_fixup_dir(trans, root, fixup_path,
  1816. di_key.objectid);
  1817. if (ret)
  1818. break;
  1819. }
  1820. ret = 0;
  1821. }
  1822. btrfs_free_path(fixup_path);
  1823. return ret;
  1824. }
  1825. /*
  1826. * directory replay has two parts. There are the standard directory
  1827. * items in the log copied from the subvolume, and range items
  1828. * created in the log while the subvolume was logged.
  1829. *
  1830. * The range items tell us which parts of the key space the log
  1831. * is authoritative for. During replay, if a key in the subvolume
  1832. * directory is in a logged range item, but not actually in the log
  1833. * that means it was deleted from the directory before the fsync
  1834. * and should be removed.
  1835. */
  1836. static noinline int find_dir_range(struct btrfs_root *root,
  1837. struct btrfs_path *path,
  1838. u64 dirid, int key_type,
  1839. u64 *start_ret, u64 *end_ret)
  1840. {
  1841. struct btrfs_key key;
  1842. u64 found_end;
  1843. struct btrfs_dir_log_item *item;
  1844. int ret;
  1845. int nritems;
  1846. if (*start_ret == (u64)-1)
  1847. return 1;
  1848. key.objectid = dirid;
  1849. key.type = key_type;
  1850. key.offset = *start_ret;
  1851. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1852. if (ret < 0)
  1853. goto out;
  1854. if (ret > 0) {
  1855. if (path->slots[0] == 0)
  1856. goto out;
  1857. path->slots[0]--;
  1858. }
  1859. if (ret != 0)
  1860. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1861. if (key.type != key_type || key.objectid != dirid) {
  1862. ret = 1;
  1863. goto next;
  1864. }
  1865. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1866. struct btrfs_dir_log_item);
  1867. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1868. if (*start_ret >= key.offset && *start_ret <= found_end) {
  1869. ret = 0;
  1870. *start_ret = key.offset;
  1871. *end_ret = found_end;
  1872. goto out;
  1873. }
  1874. ret = 1;
  1875. next:
  1876. /* check the next slot in the tree to see if it is a valid item */
  1877. nritems = btrfs_header_nritems(path->nodes[0]);
  1878. path->slots[0]++;
  1879. if (path->slots[0] >= nritems) {
  1880. ret = btrfs_next_leaf(root, path);
  1881. if (ret)
  1882. goto out;
  1883. }
  1884. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1885. if (key.type != key_type || key.objectid != dirid) {
  1886. ret = 1;
  1887. goto out;
  1888. }
  1889. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1890. struct btrfs_dir_log_item);
  1891. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1892. *start_ret = key.offset;
  1893. *end_ret = found_end;
  1894. ret = 0;
  1895. out:
  1896. btrfs_release_path(path);
  1897. return ret;
  1898. }
  1899. /*
  1900. * this looks for a given directory item in the log. If the directory
  1901. * item is not in the log, the item is removed and the inode it points
  1902. * to is unlinked
  1903. */
  1904. static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
  1905. struct btrfs_root *root,
  1906. struct btrfs_root *log,
  1907. struct btrfs_path *path,
  1908. struct btrfs_path *log_path,
  1909. struct inode *dir,
  1910. struct btrfs_key *dir_key)
  1911. {
  1912. int ret;
  1913. struct extent_buffer *eb;
  1914. int slot;
  1915. u32 item_size;
  1916. struct btrfs_dir_item *di;
  1917. struct btrfs_dir_item *log_di;
  1918. int name_len;
  1919. unsigned long ptr;
  1920. unsigned long ptr_end;
  1921. char *name;
  1922. struct inode *inode;
  1923. struct btrfs_key location;
  1924. again:
  1925. eb = path->nodes[0];
  1926. slot = path->slots[0];
  1927. item_size = btrfs_item_size_nr(eb, slot);
  1928. ptr = btrfs_item_ptr_offset(eb, slot);
  1929. ptr_end = ptr + item_size;
  1930. while (ptr < ptr_end) {
  1931. di = (struct btrfs_dir_item *)ptr;
  1932. name_len = btrfs_dir_name_len(eb, di);
  1933. name = kmalloc(name_len, GFP_NOFS);
  1934. if (!name) {
  1935. ret = -ENOMEM;
  1936. goto out;
  1937. }
  1938. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1939. name_len);
  1940. log_di = NULL;
  1941. if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
  1942. log_di = btrfs_lookup_dir_item(trans, log, log_path,
  1943. dir_key->objectid,
  1944. name, name_len, 0);
  1945. } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
  1946. log_di = btrfs_lookup_dir_index_item(trans, log,
  1947. log_path,
  1948. dir_key->objectid,
  1949. dir_key->offset,
  1950. name, name_len, 0);
  1951. }
  1952. if (!log_di || log_di == ERR_PTR(-ENOENT)) {
  1953. btrfs_dir_item_key_to_cpu(eb, di, &location);
  1954. btrfs_release_path(path);
  1955. btrfs_release_path(log_path);
  1956. inode = read_one_inode(root, location.objectid);
  1957. if (!inode) {
  1958. kfree(name);
  1959. return -EIO;
  1960. }
  1961. ret = link_to_fixup_dir(trans, root,
  1962. path, location.objectid);
  1963. if (ret) {
  1964. kfree(name);
  1965. iput(inode);
  1966. goto out;
  1967. }
  1968. inc_nlink(inode);
  1969. ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
  1970. BTRFS_I(inode), name, name_len);
  1971. if (!ret)
  1972. ret = btrfs_run_delayed_items(trans);
  1973. kfree(name);
  1974. iput(inode);
  1975. if (ret)
  1976. goto out;
  1977. /* there might still be more names under this key
  1978. * check and repeat if required
  1979. */
  1980. ret = btrfs_search_slot(NULL, root, dir_key, path,
  1981. 0, 0);
  1982. if (ret == 0)
  1983. goto again;
  1984. ret = 0;
  1985. goto out;
  1986. } else if (IS_ERR(log_di)) {
  1987. kfree(name);
  1988. return PTR_ERR(log_di);
  1989. }
  1990. btrfs_release_path(log_path);
  1991. kfree(name);
  1992. ptr = (unsigned long)(di + 1);
  1993. ptr += name_len;
  1994. }
  1995. ret = 0;
  1996. out:
  1997. btrfs_release_path(path);
  1998. btrfs_release_path(log_path);
  1999. return ret;
  2000. }
  2001. static int replay_xattr_deletes(struct btrfs_trans_handle *trans,
  2002. struct btrfs_root *root,
  2003. struct btrfs_root *log,
  2004. struct btrfs_path *path,
  2005. const u64 ino)
  2006. {
  2007. struct btrfs_key search_key;
  2008. struct btrfs_path *log_path;
  2009. int i;
  2010. int nritems;
  2011. int ret;
  2012. log_path = btrfs_alloc_path();
  2013. if (!log_path)
  2014. return -ENOMEM;
  2015. search_key.objectid = ino;
  2016. search_key.type = BTRFS_XATTR_ITEM_KEY;
  2017. search_key.offset = 0;
  2018. again:
  2019. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  2020. if (ret < 0)
  2021. goto out;
  2022. process_leaf:
  2023. nritems = btrfs_header_nritems(path->nodes[0]);
  2024. for (i = path->slots[0]; i < nritems; i++) {
  2025. struct btrfs_key key;
  2026. struct btrfs_dir_item *di;
  2027. struct btrfs_dir_item *log_di;
  2028. u32 total_size;
  2029. u32 cur;
  2030. btrfs_item_key_to_cpu(path->nodes[0], &key, i);
  2031. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY) {
  2032. ret = 0;
  2033. goto out;
  2034. }
  2035. di = btrfs_item_ptr(path->nodes[0], i, struct btrfs_dir_item);
  2036. total_size = btrfs_item_size_nr(path->nodes[0], i);
  2037. cur = 0;
  2038. while (cur < total_size) {
  2039. u16 name_len = btrfs_dir_name_len(path->nodes[0], di);
  2040. u16 data_len = btrfs_dir_data_len(path->nodes[0], di);
  2041. u32 this_len = sizeof(*di) + name_len + data_len;
  2042. char *name;
  2043. name = kmalloc(name_len, GFP_NOFS);
  2044. if (!name) {
  2045. ret = -ENOMEM;
  2046. goto out;
  2047. }
  2048. read_extent_buffer(path->nodes[0], name,
  2049. (unsigned long)(di + 1), name_len);
  2050. log_di = btrfs_lookup_xattr(NULL, log, log_path, ino,
  2051. name, name_len, 0);
  2052. btrfs_release_path(log_path);
  2053. if (!log_di) {
  2054. /* Doesn't exist in log tree, so delete it. */
  2055. btrfs_release_path(path);
  2056. di = btrfs_lookup_xattr(trans, root, path, ino,
  2057. name, name_len, -1);
  2058. kfree(name);
  2059. if (IS_ERR(di)) {
  2060. ret = PTR_ERR(di);
  2061. goto out;
  2062. }
  2063. ASSERT(di);
  2064. ret = btrfs_delete_one_dir_name(trans, root,
  2065. path, di);
  2066. if (ret)
  2067. goto out;
  2068. btrfs_release_path(path);
  2069. search_key = key;
  2070. goto again;
  2071. }
  2072. kfree(name);
  2073. if (IS_ERR(log_di)) {
  2074. ret = PTR_ERR(log_di);
  2075. goto out;
  2076. }
  2077. cur += this_len;
  2078. di = (struct btrfs_dir_item *)((char *)di + this_len);
  2079. }
  2080. }
  2081. ret = btrfs_next_leaf(root, path);
  2082. if (ret > 0)
  2083. ret = 0;
  2084. else if (ret == 0)
  2085. goto process_leaf;
  2086. out:
  2087. btrfs_free_path(log_path);
  2088. btrfs_release_path(path);
  2089. return ret;
  2090. }
  2091. /*
  2092. * deletion replay happens before we copy any new directory items
  2093. * out of the log or out of backreferences from inodes. It
  2094. * scans the log to find ranges of keys that log is authoritative for,
  2095. * and then scans the directory to find items in those ranges that are
  2096. * not present in the log.
  2097. *
  2098. * Anything we don't find in the log is unlinked and removed from the
  2099. * directory.
  2100. */
  2101. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  2102. struct btrfs_root *root,
  2103. struct btrfs_root *log,
  2104. struct btrfs_path *path,
  2105. u64 dirid, int del_all)
  2106. {
  2107. u64 range_start;
  2108. u64 range_end;
  2109. int key_type = BTRFS_DIR_LOG_ITEM_KEY;
  2110. int ret = 0;
  2111. struct btrfs_key dir_key;
  2112. struct btrfs_key found_key;
  2113. struct btrfs_path *log_path;
  2114. struct inode *dir;
  2115. dir_key.objectid = dirid;
  2116. dir_key.type = BTRFS_DIR_ITEM_KEY;
  2117. log_path = btrfs_alloc_path();
  2118. if (!log_path)
  2119. return -ENOMEM;
  2120. dir = read_one_inode(root, dirid);
  2121. /* it isn't an error if the inode isn't there, that can happen
  2122. * because we replay the deletes before we copy in the inode item
  2123. * from the log
  2124. */
  2125. if (!dir) {
  2126. btrfs_free_path(log_path);
  2127. return 0;
  2128. }
  2129. again:
  2130. range_start = 0;
  2131. range_end = 0;
  2132. while (1) {
  2133. if (del_all)
  2134. range_end = (u64)-1;
  2135. else {
  2136. ret = find_dir_range(log, path, dirid, key_type,
  2137. &range_start, &range_end);
  2138. if (ret != 0)
  2139. break;
  2140. }
  2141. dir_key.offset = range_start;
  2142. while (1) {
  2143. int nritems;
  2144. ret = btrfs_search_slot(NULL, root, &dir_key, path,
  2145. 0, 0);
  2146. if (ret < 0)
  2147. goto out;
  2148. nritems = btrfs_header_nritems(path->nodes[0]);
  2149. if (path->slots[0] >= nritems) {
  2150. ret = btrfs_next_leaf(root, path);
  2151. if (ret == 1)
  2152. break;
  2153. else if (ret < 0)
  2154. goto out;
  2155. }
  2156. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2157. path->slots[0]);
  2158. if (found_key.objectid != dirid ||
  2159. found_key.type != dir_key.type)
  2160. goto next_type;
  2161. if (found_key.offset > range_end)
  2162. break;
  2163. ret = check_item_in_log(trans, root, log, path,
  2164. log_path, dir,
  2165. &found_key);
  2166. if (ret)
  2167. goto out;
  2168. if (found_key.offset == (u64)-1)
  2169. break;
  2170. dir_key.offset = found_key.offset + 1;
  2171. }
  2172. btrfs_release_path(path);
  2173. if (range_end == (u64)-1)
  2174. break;
  2175. range_start = range_end + 1;
  2176. }
  2177. next_type:
  2178. ret = 0;
  2179. if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
  2180. key_type = BTRFS_DIR_LOG_INDEX_KEY;
  2181. dir_key.type = BTRFS_DIR_INDEX_KEY;
  2182. btrfs_release_path(path);
  2183. goto again;
  2184. }
  2185. out:
  2186. btrfs_release_path(path);
  2187. btrfs_free_path(log_path);
  2188. iput(dir);
  2189. return ret;
  2190. }
  2191. /*
  2192. * the process_func used to replay items from the log tree. This
  2193. * gets called in two different stages. The first stage just looks
  2194. * for inodes and makes sure they are all copied into the subvolume.
  2195. *
  2196. * The second stage copies all the other item types from the log into
  2197. * the subvolume. The two stage approach is slower, but gets rid of
  2198. * lots of complexity around inodes referencing other inodes that exist
  2199. * only in the log (references come from either directory items or inode
  2200. * back refs).
  2201. */
  2202. static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
  2203. struct walk_control *wc, u64 gen, int level)
  2204. {
  2205. int nritems;
  2206. struct btrfs_path *path;
  2207. struct btrfs_root *root = wc->replay_dest;
  2208. struct btrfs_key key;
  2209. int i;
  2210. int ret;
  2211. ret = btrfs_read_buffer(eb, gen, level, NULL);
  2212. if (ret)
  2213. return ret;
  2214. level = btrfs_header_level(eb);
  2215. if (level != 0)
  2216. return 0;
  2217. path = btrfs_alloc_path();
  2218. if (!path)
  2219. return -ENOMEM;
  2220. nritems = btrfs_header_nritems(eb);
  2221. for (i = 0; i < nritems; i++) {
  2222. btrfs_item_key_to_cpu(eb, &key, i);
  2223. /* inode keys are done during the first stage */
  2224. if (key.type == BTRFS_INODE_ITEM_KEY &&
  2225. wc->stage == LOG_WALK_REPLAY_INODES) {
  2226. struct btrfs_inode_item *inode_item;
  2227. u32 mode;
  2228. inode_item = btrfs_item_ptr(eb, i,
  2229. struct btrfs_inode_item);
  2230. /*
  2231. * If we have a tmpfile (O_TMPFILE) that got fsync'ed
  2232. * and never got linked before the fsync, skip it, as
  2233. * replaying it is pointless since it would be deleted
  2234. * later. We skip logging tmpfiles, but it's always
  2235. * possible we are replaying a log created with a kernel
  2236. * that used to log tmpfiles.
  2237. */
  2238. if (btrfs_inode_nlink(eb, inode_item) == 0) {
  2239. wc->ignore_cur_inode = true;
  2240. continue;
  2241. } else {
  2242. wc->ignore_cur_inode = false;
  2243. }
  2244. ret = replay_xattr_deletes(wc->trans, root, log,
  2245. path, key.objectid);
  2246. if (ret)
  2247. break;
  2248. mode = btrfs_inode_mode(eb, inode_item);
  2249. if (S_ISDIR(mode)) {
  2250. ret = replay_dir_deletes(wc->trans,
  2251. root, log, path, key.objectid, 0);
  2252. if (ret)
  2253. break;
  2254. }
  2255. ret = overwrite_item(wc->trans, root, path,
  2256. eb, i, &key);
  2257. if (ret)
  2258. break;
  2259. /*
  2260. * Before replaying extents, truncate the inode to its
  2261. * size. We need to do it now and not after log replay
  2262. * because before an fsync we can have prealloc extents
  2263. * added beyond the inode's i_size. If we did it after,
  2264. * through orphan cleanup for example, we would drop
  2265. * those prealloc extents just after replaying them.
  2266. */
  2267. if (S_ISREG(mode)) {
  2268. struct inode *inode;
  2269. u64 from;
  2270. inode = read_one_inode(root, key.objectid);
  2271. if (!inode) {
  2272. ret = -EIO;
  2273. break;
  2274. }
  2275. from = ALIGN(i_size_read(inode),
  2276. root->fs_info->sectorsize);
  2277. ret = btrfs_drop_extents(wc->trans, root, inode,
  2278. from, (u64)-1, 1);
  2279. if (!ret) {
  2280. /* Update the inode's nbytes. */
  2281. ret = btrfs_update_inode(wc->trans,
  2282. root, inode);
  2283. }
  2284. iput(inode);
  2285. if (ret)
  2286. break;
  2287. }
  2288. ret = link_to_fixup_dir(wc->trans, root,
  2289. path, key.objectid);
  2290. if (ret)
  2291. break;
  2292. }
  2293. if (wc->ignore_cur_inode)
  2294. continue;
  2295. if (key.type == BTRFS_DIR_INDEX_KEY &&
  2296. wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
  2297. ret = replay_one_dir_item(wc->trans, root, path,
  2298. eb, i, &key);
  2299. if (ret)
  2300. break;
  2301. }
  2302. if (wc->stage < LOG_WALK_REPLAY_ALL)
  2303. continue;
  2304. /* these keys are simply copied */
  2305. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  2306. ret = overwrite_item(wc->trans, root, path,
  2307. eb, i, &key);
  2308. if (ret)
  2309. break;
  2310. } else if (key.type == BTRFS_INODE_REF_KEY ||
  2311. key.type == BTRFS_INODE_EXTREF_KEY) {
  2312. ret = add_inode_ref(wc->trans, root, log, path,
  2313. eb, i, &key);
  2314. if (ret && ret != -ENOENT)
  2315. break;
  2316. ret = 0;
  2317. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  2318. ret = replay_one_extent(wc->trans, root, path,
  2319. eb, i, &key);
  2320. if (ret)
  2321. break;
  2322. } else if (key.type == BTRFS_DIR_ITEM_KEY) {
  2323. ret = replay_one_dir_item(wc->trans, root, path,
  2324. eb, i, &key);
  2325. if (ret)
  2326. break;
  2327. }
  2328. }
  2329. btrfs_free_path(path);
  2330. return ret;
  2331. }
  2332. static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
  2333. struct btrfs_root *root,
  2334. struct btrfs_path *path, int *level,
  2335. struct walk_control *wc)
  2336. {
  2337. struct btrfs_fs_info *fs_info = root->fs_info;
  2338. u64 root_owner;
  2339. u64 bytenr;
  2340. u64 ptr_gen;
  2341. struct extent_buffer *next;
  2342. struct extent_buffer *cur;
  2343. struct extent_buffer *parent;
  2344. u32 blocksize;
  2345. int ret = 0;
  2346. WARN_ON(*level < 0);
  2347. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2348. while (*level > 0) {
  2349. struct btrfs_key first_key;
  2350. WARN_ON(*level < 0);
  2351. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2352. cur = path->nodes[*level];
  2353. WARN_ON(btrfs_header_level(cur) != *level);
  2354. if (path->slots[*level] >=
  2355. btrfs_header_nritems(cur))
  2356. break;
  2357. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2358. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2359. btrfs_node_key_to_cpu(cur, &first_key, path->slots[*level]);
  2360. blocksize = fs_info->nodesize;
  2361. parent = path->nodes[*level];
  2362. root_owner = btrfs_header_owner(parent);
  2363. next = btrfs_find_create_tree_block(fs_info, bytenr);
  2364. if (IS_ERR(next))
  2365. return PTR_ERR(next);
  2366. if (*level == 1) {
  2367. ret = wc->process_func(root, next, wc, ptr_gen,
  2368. *level - 1);
  2369. if (ret) {
  2370. free_extent_buffer(next);
  2371. return ret;
  2372. }
  2373. path->slots[*level]++;
  2374. if (wc->free) {
  2375. ret = btrfs_read_buffer(next, ptr_gen,
  2376. *level - 1, &first_key);
  2377. if (ret) {
  2378. free_extent_buffer(next);
  2379. return ret;
  2380. }
  2381. if (trans) {
  2382. btrfs_tree_lock(next);
  2383. btrfs_set_lock_blocking(next);
  2384. clean_tree_block(fs_info, next);
  2385. btrfs_wait_tree_block_writeback(next);
  2386. btrfs_tree_unlock(next);
  2387. } else {
  2388. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2389. clear_extent_buffer_dirty(next);
  2390. }
  2391. WARN_ON(root_owner !=
  2392. BTRFS_TREE_LOG_OBJECTID);
  2393. ret = btrfs_free_and_pin_reserved_extent(
  2394. fs_info, bytenr,
  2395. blocksize);
  2396. if (ret) {
  2397. free_extent_buffer(next);
  2398. return ret;
  2399. }
  2400. }
  2401. free_extent_buffer(next);
  2402. continue;
  2403. }
  2404. ret = btrfs_read_buffer(next, ptr_gen, *level - 1, &first_key);
  2405. if (ret) {
  2406. free_extent_buffer(next);
  2407. return ret;
  2408. }
  2409. WARN_ON(*level <= 0);
  2410. if (path->nodes[*level-1])
  2411. free_extent_buffer(path->nodes[*level-1]);
  2412. path->nodes[*level-1] = next;
  2413. *level = btrfs_header_level(next);
  2414. path->slots[*level] = 0;
  2415. cond_resched();
  2416. }
  2417. WARN_ON(*level < 0);
  2418. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2419. path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
  2420. cond_resched();
  2421. return 0;
  2422. }
  2423. static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
  2424. struct btrfs_root *root,
  2425. struct btrfs_path *path, int *level,
  2426. struct walk_control *wc)
  2427. {
  2428. struct btrfs_fs_info *fs_info = root->fs_info;
  2429. u64 root_owner;
  2430. int i;
  2431. int slot;
  2432. int ret;
  2433. for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  2434. slot = path->slots[i];
  2435. if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
  2436. path->slots[i]++;
  2437. *level = i;
  2438. WARN_ON(*level == 0);
  2439. return 0;
  2440. } else {
  2441. struct extent_buffer *parent;
  2442. if (path->nodes[*level] == root->node)
  2443. parent = path->nodes[*level];
  2444. else
  2445. parent = path->nodes[*level + 1];
  2446. root_owner = btrfs_header_owner(parent);
  2447. ret = wc->process_func(root, path->nodes[*level], wc,
  2448. btrfs_header_generation(path->nodes[*level]),
  2449. *level);
  2450. if (ret)
  2451. return ret;
  2452. if (wc->free) {
  2453. struct extent_buffer *next;
  2454. next = path->nodes[*level];
  2455. if (trans) {
  2456. btrfs_tree_lock(next);
  2457. btrfs_set_lock_blocking(next);
  2458. clean_tree_block(fs_info, next);
  2459. btrfs_wait_tree_block_writeback(next);
  2460. btrfs_tree_unlock(next);
  2461. } else {
  2462. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2463. clear_extent_buffer_dirty(next);
  2464. }
  2465. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  2466. ret = btrfs_free_and_pin_reserved_extent(
  2467. fs_info,
  2468. path->nodes[*level]->start,
  2469. path->nodes[*level]->len);
  2470. if (ret)
  2471. return ret;
  2472. }
  2473. free_extent_buffer(path->nodes[*level]);
  2474. path->nodes[*level] = NULL;
  2475. *level = i + 1;
  2476. }
  2477. }
  2478. return 1;
  2479. }
  2480. /*
  2481. * drop the reference count on the tree rooted at 'snap'. This traverses
  2482. * the tree freeing any blocks that have a ref count of zero after being
  2483. * decremented.
  2484. */
  2485. static int walk_log_tree(struct btrfs_trans_handle *trans,
  2486. struct btrfs_root *log, struct walk_control *wc)
  2487. {
  2488. struct btrfs_fs_info *fs_info = log->fs_info;
  2489. int ret = 0;
  2490. int wret;
  2491. int level;
  2492. struct btrfs_path *path;
  2493. int orig_level;
  2494. path = btrfs_alloc_path();
  2495. if (!path)
  2496. return -ENOMEM;
  2497. level = btrfs_header_level(log->node);
  2498. orig_level = level;
  2499. path->nodes[level] = log->node;
  2500. extent_buffer_get(log->node);
  2501. path->slots[level] = 0;
  2502. while (1) {
  2503. wret = walk_down_log_tree(trans, log, path, &level, wc);
  2504. if (wret > 0)
  2505. break;
  2506. if (wret < 0) {
  2507. ret = wret;
  2508. goto out;
  2509. }
  2510. wret = walk_up_log_tree(trans, log, path, &level, wc);
  2511. if (wret > 0)
  2512. break;
  2513. if (wret < 0) {
  2514. ret = wret;
  2515. goto out;
  2516. }
  2517. }
  2518. /* was the root node processed? if not, catch it here */
  2519. if (path->nodes[orig_level]) {
  2520. ret = wc->process_func(log, path->nodes[orig_level], wc,
  2521. btrfs_header_generation(path->nodes[orig_level]),
  2522. orig_level);
  2523. if (ret)
  2524. goto out;
  2525. if (wc->free) {
  2526. struct extent_buffer *next;
  2527. next = path->nodes[orig_level];
  2528. if (trans) {
  2529. btrfs_tree_lock(next);
  2530. btrfs_set_lock_blocking(next);
  2531. clean_tree_block(fs_info, next);
  2532. btrfs_wait_tree_block_writeback(next);
  2533. btrfs_tree_unlock(next);
  2534. } else {
  2535. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2536. clear_extent_buffer_dirty(next);
  2537. }
  2538. WARN_ON(log->root_key.objectid !=
  2539. BTRFS_TREE_LOG_OBJECTID);
  2540. ret = btrfs_free_and_pin_reserved_extent(fs_info,
  2541. next->start, next->len);
  2542. if (ret)
  2543. goto out;
  2544. }
  2545. }
  2546. out:
  2547. btrfs_free_path(path);
  2548. return ret;
  2549. }
  2550. /*
  2551. * helper function to update the item for a given subvolumes log root
  2552. * in the tree of log roots
  2553. */
  2554. static int update_log_root(struct btrfs_trans_handle *trans,
  2555. struct btrfs_root *log)
  2556. {
  2557. struct btrfs_fs_info *fs_info = log->fs_info;
  2558. int ret;
  2559. if (log->log_transid == 1) {
  2560. /* insert root item on the first sync */
  2561. ret = btrfs_insert_root(trans, fs_info->log_root_tree,
  2562. &log->root_key, &log->root_item);
  2563. } else {
  2564. ret = btrfs_update_root(trans, fs_info->log_root_tree,
  2565. &log->root_key, &log->root_item);
  2566. }
  2567. return ret;
  2568. }
  2569. static void wait_log_commit(struct btrfs_root *root, int transid)
  2570. {
  2571. DEFINE_WAIT(wait);
  2572. int index = transid % 2;
  2573. /*
  2574. * we only allow two pending log transactions at a time,
  2575. * so we know that if ours is more than 2 older than the
  2576. * current transaction, we're done
  2577. */
  2578. for (;;) {
  2579. prepare_to_wait(&root->log_commit_wait[index],
  2580. &wait, TASK_UNINTERRUPTIBLE);
  2581. if (!(root->log_transid_committed < transid &&
  2582. atomic_read(&root->log_commit[index])))
  2583. break;
  2584. mutex_unlock(&root->log_mutex);
  2585. schedule();
  2586. mutex_lock(&root->log_mutex);
  2587. }
  2588. finish_wait(&root->log_commit_wait[index], &wait);
  2589. }
  2590. static void wait_for_writer(struct btrfs_root *root)
  2591. {
  2592. DEFINE_WAIT(wait);
  2593. for (;;) {
  2594. prepare_to_wait(&root->log_writer_wait, &wait,
  2595. TASK_UNINTERRUPTIBLE);
  2596. if (!atomic_read(&root->log_writers))
  2597. break;
  2598. mutex_unlock(&root->log_mutex);
  2599. schedule();
  2600. mutex_lock(&root->log_mutex);
  2601. }
  2602. finish_wait(&root->log_writer_wait, &wait);
  2603. }
  2604. static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
  2605. struct btrfs_log_ctx *ctx)
  2606. {
  2607. if (!ctx)
  2608. return;
  2609. mutex_lock(&root->log_mutex);
  2610. list_del_init(&ctx->list);
  2611. mutex_unlock(&root->log_mutex);
  2612. }
  2613. /*
  2614. * Invoked in log mutex context, or be sure there is no other task which
  2615. * can access the list.
  2616. */
  2617. static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
  2618. int index, int error)
  2619. {
  2620. struct btrfs_log_ctx *ctx;
  2621. struct btrfs_log_ctx *safe;
  2622. list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
  2623. list_del_init(&ctx->list);
  2624. ctx->log_ret = error;
  2625. }
  2626. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2627. }
  2628. /*
  2629. * btrfs_sync_log does sends a given tree log down to the disk and
  2630. * updates the super blocks to record it. When this call is done,
  2631. * you know that any inodes previously logged are safely on disk only
  2632. * if it returns 0.
  2633. *
  2634. * Any other return value means you need to call btrfs_commit_transaction.
  2635. * Some of the edge cases for fsyncing directories that have had unlinks
  2636. * or renames done in the past mean that sometimes the only safe
  2637. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2638. * that has happened.
  2639. */
  2640. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2641. struct btrfs_root *root, struct btrfs_log_ctx *ctx)
  2642. {
  2643. int index1;
  2644. int index2;
  2645. int mark;
  2646. int ret;
  2647. struct btrfs_fs_info *fs_info = root->fs_info;
  2648. struct btrfs_root *log = root->log_root;
  2649. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  2650. int log_transid = 0;
  2651. struct btrfs_log_ctx root_log_ctx;
  2652. struct blk_plug plug;
  2653. mutex_lock(&root->log_mutex);
  2654. log_transid = ctx->log_transid;
  2655. if (root->log_transid_committed >= log_transid) {
  2656. mutex_unlock(&root->log_mutex);
  2657. return ctx->log_ret;
  2658. }
  2659. index1 = log_transid % 2;
  2660. if (atomic_read(&root->log_commit[index1])) {
  2661. wait_log_commit(root, log_transid);
  2662. mutex_unlock(&root->log_mutex);
  2663. return ctx->log_ret;
  2664. }
  2665. ASSERT(log_transid == root->log_transid);
  2666. atomic_set(&root->log_commit[index1], 1);
  2667. /* wait for previous tree log sync to complete */
  2668. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  2669. wait_log_commit(root, log_transid - 1);
  2670. while (1) {
  2671. int batch = atomic_read(&root->log_batch);
  2672. /* when we're on an ssd, just kick the log commit out */
  2673. if (!btrfs_test_opt(fs_info, SSD) &&
  2674. test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
  2675. mutex_unlock(&root->log_mutex);
  2676. schedule_timeout_uninterruptible(1);
  2677. mutex_lock(&root->log_mutex);
  2678. }
  2679. wait_for_writer(root);
  2680. if (batch == atomic_read(&root->log_batch))
  2681. break;
  2682. }
  2683. /* bail out if we need to do a full commit */
  2684. if (btrfs_need_log_full_commit(fs_info, trans)) {
  2685. ret = -EAGAIN;
  2686. mutex_unlock(&root->log_mutex);
  2687. goto out;
  2688. }
  2689. if (log_transid % 2 == 0)
  2690. mark = EXTENT_DIRTY;
  2691. else
  2692. mark = EXTENT_NEW;
  2693. /* we start IO on all the marked extents here, but we don't actually
  2694. * wait for them until later.
  2695. */
  2696. blk_start_plug(&plug);
  2697. ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
  2698. if (ret) {
  2699. blk_finish_plug(&plug);
  2700. btrfs_abort_transaction(trans, ret);
  2701. btrfs_set_log_full_commit(fs_info, trans);
  2702. mutex_unlock(&root->log_mutex);
  2703. goto out;
  2704. }
  2705. btrfs_set_root_node(&log->root_item, log->node);
  2706. root->log_transid++;
  2707. log->log_transid = root->log_transid;
  2708. root->log_start_pid = 0;
  2709. /*
  2710. * IO has been started, blocks of the log tree have WRITTEN flag set
  2711. * in their headers. new modifications of the log will be written to
  2712. * new positions. so it's safe to allow log writers to go in.
  2713. */
  2714. mutex_unlock(&root->log_mutex);
  2715. btrfs_init_log_ctx(&root_log_ctx, NULL);
  2716. mutex_lock(&log_root_tree->log_mutex);
  2717. atomic_inc(&log_root_tree->log_batch);
  2718. atomic_inc(&log_root_tree->log_writers);
  2719. index2 = log_root_tree->log_transid % 2;
  2720. list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
  2721. root_log_ctx.log_transid = log_root_tree->log_transid;
  2722. mutex_unlock(&log_root_tree->log_mutex);
  2723. ret = update_log_root(trans, log);
  2724. mutex_lock(&log_root_tree->log_mutex);
  2725. if (atomic_dec_and_test(&log_root_tree->log_writers)) {
  2726. /* atomic_dec_and_test implies a barrier */
  2727. cond_wake_up_nomb(&log_root_tree->log_writer_wait);
  2728. }
  2729. if (ret) {
  2730. if (!list_empty(&root_log_ctx.list))
  2731. list_del_init(&root_log_ctx.list);
  2732. blk_finish_plug(&plug);
  2733. btrfs_set_log_full_commit(fs_info, trans);
  2734. if (ret != -ENOSPC) {
  2735. btrfs_abort_transaction(trans, ret);
  2736. mutex_unlock(&log_root_tree->log_mutex);
  2737. goto out;
  2738. }
  2739. btrfs_wait_tree_log_extents(log, mark);
  2740. mutex_unlock(&log_root_tree->log_mutex);
  2741. ret = -EAGAIN;
  2742. goto out;
  2743. }
  2744. if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
  2745. blk_finish_plug(&plug);
  2746. list_del_init(&root_log_ctx.list);
  2747. mutex_unlock(&log_root_tree->log_mutex);
  2748. ret = root_log_ctx.log_ret;
  2749. goto out;
  2750. }
  2751. index2 = root_log_ctx.log_transid % 2;
  2752. if (atomic_read(&log_root_tree->log_commit[index2])) {
  2753. blk_finish_plug(&plug);
  2754. ret = btrfs_wait_tree_log_extents(log, mark);
  2755. wait_log_commit(log_root_tree,
  2756. root_log_ctx.log_transid);
  2757. mutex_unlock(&log_root_tree->log_mutex);
  2758. if (!ret)
  2759. ret = root_log_ctx.log_ret;
  2760. goto out;
  2761. }
  2762. ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
  2763. atomic_set(&log_root_tree->log_commit[index2], 1);
  2764. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  2765. wait_log_commit(log_root_tree,
  2766. root_log_ctx.log_transid - 1);
  2767. }
  2768. wait_for_writer(log_root_tree);
  2769. /*
  2770. * now that we've moved on to the tree of log tree roots,
  2771. * check the full commit flag again
  2772. */
  2773. if (btrfs_need_log_full_commit(fs_info, trans)) {
  2774. blk_finish_plug(&plug);
  2775. btrfs_wait_tree_log_extents(log, mark);
  2776. mutex_unlock(&log_root_tree->log_mutex);
  2777. ret = -EAGAIN;
  2778. goto out_wake_log_root;
  2779. }
  2780. ret = btrfs_write_marked_extents(fs_info,
  2781. &log_root_tree->dirty_log_pages,
  2782. EXTENT_DIRTY | EXTENT_NEW);
  2783. blk_finish_plug(&plug);
  2784. if (ret) {
  2785. btrfs_set_log_full_commit(fs_info, trans);
  2786. btrfs_abort_transaction(trans, ret);
  2787. mutex_unlock(&log_root_tree->log_mutex);
  2788. goto out_wake_log_root;
  2789. }
  2790. ret = btrfs_wait_tree_log_extents(log, mark);
  2791. if (!ret)
  2792. ret = btrfs_wait_tree_log_extents(log_root_tree,
  2793. EXTENT_NEW | EXTENT_DIRTY);
  2794. if (ret) {
  2795. btrfs_set_log_full_commit(fs_info, trans);
  2796. mutex_unlock(&log_root_tree->log_mutex);
  2797. goto out_wake_log_root;
  2798. }
  2799. btrfs_set_super_log_root(fs_info->super_for_commit,
  2800. log_root_tree->node->start);
  2801. btrfs_set_super_log_root_level(fs_info->super_for_commit,
  2802. btrfs_header_level(log_root_tree->node));
  2803. log_root_tree->log_transid++;
  2804. mutex_unlock(&log_root_tree->log_mutex);
  2805. /*
  2806. * nobody else is going to jump in and write the the ctree
  2807. * super here because the log_commit atomic below is protecting
  2808. * us. We must be called with a transaction handle pinning
  2809. * the running transaction open, so a full commit can't hop
  2810. * in and cause problems either.
  2811. */
  2812. ret = write_all_supers(fs_info, 1);
  2813. if (ret) {
  2814. btrfs_set_log_full_commit(fs_info, trans);
  2815. btrfs_abort_transaction(trans, ret);
  2816. goto out_wake_log_root;
  2817. }
  2818. mutex_lock(&root->log_mutex);
  2819. if (root->last_log_commit < log_transid)
  2820. root->last_log_commit = log_transid;
  2821. mutex_unlock(&root->log_mutex);
  2822. out_wake_log_root:
  2823. mutex_lock(&log_root_tree->log_mutex);
  2824. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  2825. log_root_tree->log_transid_committed++;
  2826. atomic_set(&log_root_tree->log_commit[index2], 0);
  2827. mutex_unlock(&log_root_tree->log_mutex);
  2828. /*
  2829. * The barrier before waitqueue_active (in cond_wake_up) is needed so
  2830. * all the updates above are seen by the woken threads. It might not be
  2831. * necessary, but proving that seems to be hard.
  2832. */
  2833. cond_wake_up(&log_root_tree->log_commit_wait[index2]);
  2834. out:
  2835. mutex_lock(&root->log_mutex);
  2836. btrfs_remove_all_log_ctxs(root, index1, ret);
  2837. root->log_transid_committed++;
  2838. atomic_set(&root->log_commit[index1], 0);
  2839. mutex_unlock(&root->log_mutex);
  2840. /*
  2841. * The barrier before waitqueue_active (in cond_wake_up) is needed so
  2842. * all the updates above are seen by the woken threads. It might not be
  2843. * necessary, but proving that seems to be hard.
  2844. */
  2845. cond_wake_up(&root->log_commit_wait[index1]);
  2846. return ret;
  2847. }
  2848. static void free_log_tree(struct btrfs_trans_handle *trans,
  2849. struct btrfs_root *log)
  2850. {
  2851. int ret;
  2852. u64 start;
  2853. u64 end;
  2854. struct walk_control wc = {
  2855. .free = 1,
  2856. .process_func = process_one_buffer
  2857. };
  2858. ret = walk_log_tree(trans, log, &wc);
  2859. if (ret) {
  2860. if (trans)
  2861. btrfs_abort_transaction(trans, ret);
  2862. else
  2863. btrfs_handle_fs_error(log->fs_info, ret, NULL);
  2864. }
  2865. while (1) {
  2866. ret = find_first_extent_bit(&log->dirty_log_pages,
  2867. 0, &start, &end,
  2868. EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
  2869. NULL);
  2870. if (ret)
  2871. break;
  2872. clear_extent_bits(&log->dirty_log_pages, start, end,
  2873. EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
  2874. }
  2875. free_extent_buffer(log->node);
  2876. kfree(log);
  2877. }
  2878. /*
  2879. * free all the extents used by the tree log. This should be called
  2880. * at commit time of the full transaction
  2881. */
  2882. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2883. {
  2884. if (root->log_root) {
  2885. free_log_tree(trans, root->log_root);
  2886. root->log_root = NULL;
  2887. }
  2888. return 0;
  2889. }
  2890. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2891. struct btrfs_fs_info *fs_info)
  2892. {
  2893. if (fs_info->log_root_tree) {
  2894. free_log_tree(trans, fs_info->log_root_tree);
  2895. fs_info->log_root_tree = NULL;
  2896. }
  2897. return 0;
  2898. }
  2899. /*
  2900. * If both a file and directory are logged, and unlinks or renames are
  2901. * mixed in, we have a few interesting corners:
  2902. *
  2903. * create file X in dir Y
  2904. * link file X to X.link in dir Y
  2905. * fsync file X
  2906. * unlink file X but leave X.link
  2907. * fsync dir Y
  2908. *
  2909. * After a crash we would expect only X.link to exist. But file X
  2910. * didn't get fsync'd again so the log has back refs for X and X.link.
  2911. *
  2912. * We solve this by removing directory entries and inode backrefs from the
  2913. * log when a file that was logged in the current transaction is
  2914. * unlinked. Any later fsync will include the updated log entries, and
  2915. * we'll be able to reconstruct the proper directory items from backrefs.
  2916. *
  2917. * This optimizations allows us to avoid relogging the entire inode
  2918. * or the entire directory.
  2919. */
  2920. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  2921. struct btrfs_root *root,
  2922. const char *name, int name_len,
  2923. struct btrfs_inode *dir, u64 index)
  2924. {
  2925. struct btrfs_root *log;
  2926. struct btrfs_dir_item *di;
  2927. struct btrfs_path *path;
  2928. int ret;
  2929. int err = 0;
  2930. int bytes_del = 0;
  2931. u64 dir_ino = btrfs_ino(dir);
  2932. if (dir->logged_trans < trans->transid)
  2933. return 0;
  2934. ret = join_running_log_trans(root);
  2935. if (ret)
  2936. return 0;
  2937. mutex_lock(&dir->log_mutex);
  2938. log = root->log_root;
  2939. path = btrfs_alloc_path();
  2940. if (!path) {
  2941. err = -ENOMEM;
  2942. goto out_unlock;
  2943. }
  2944. di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
  2945. name, name_len, -1);
  2946. if (IS_ERR(di)) {
  2947. err = PTR_ERR(di);
  2948. goto fail;
  2949. }
  2950. if (di) {
  2951. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2952. bytes_del += name_len;
  2953. if (ret) {
  2954. err = ret;
  2955. goto fail;
  2956. }
  2957. }
  2958. btrfs_release_path(path);
  2959. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  2960. index, name, name_len, -1);
  2961. if (IS_ERR(di)) {
  2962. err = PTR_ERR(di);
  2963. goto fail;
  2964. }
  2965. if (di) {
  2966. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2967. bytes_del += name_len;
  2968. if (ret) {
  2969. err = ret;
  2970. goto fail;
  2971. }
  2972. }
  2973. /* update the directory size in the log to reflect the names
  2974. * we have removed
  2975. */
  2976. if (bytes_del) {
  2977. struct btrfs_key key;
  2978. key.objectid = dir_ino;
  2979. key.offset = 0;
  2980. key.type = BTRFS_INODE_ITEM_KEY;
  2981. btrfs_release_path(path);
  2982. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  2983. if (ret < 0) {
  2984. err = ret;
  2985. goto fail;
  2986. }
  2987. if (ret == 0) {
  2988. struct btrfs_inode_item *item;
  2989. u64 i_size;
  2990. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2991. struct btrfs_inode_item);
  2992. i_size = btrfs_inode_size(path->nodes[0], item);
  2993. if (i_size > bytes_del)
  2994. i_size -= bytes_del;
  2995. else
  2996. i_size = 0;
  2997. btrfs_set_inode_size(path->nodes[0], item, i_size);
  2998. btrfs_mark_buffer_dirty(path->nodes[0]);
  2999. } else
  3000. ret = 0;
  3001. btrfs_release_path(path);
  3002. }
  3003. fail:
  3004. btrfs_free_path(path);
  3005. out_unlock:
  3006. mutex_unlock(&dir->log_mutex);
  3007. if (ret == -ENOSPC) {
  3008. btrfs_set_log_full_commit(root->fs_info, trans);
  3009. ret = 0;
  3010. } else if (ret < 0)
  3011. btrfs_abort_transaction(trans, ret);
  3012. btrfs_end_log_trans(root);
  3013. return err;
  3014. }
  3015. /* see comments for btrfs_del_dir_entries_in_log */
  3016. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  3017. struct btrfs_root *root,
  3018. const char *name, int name_len,
  3019. struct btrfs_inode *inode, u64 dirid)
  3020. {
  3021. struct btrfs_fs_info *fs_info = root->fs_info;
  3022. struct btrfs_root *log;
  3023. u64 index;
  3024. int ret;
  3025. if (inode->logged_trans < trans->transid)
  3026. return 0;
  3027. ret = join_running_log_trans(root);
  3028. if (ret)
  3029. return 0;
  3030. log = root->log_root;
  3031. mutex_lock(&inode->log_mutex);
  3032. ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
  3033. dirid, &index);
  3034. mutex_unlock(&inode->log_mutex);
  3035. if (ret == -ENOSPC) {
  3036. btrfs_set_log_full_commit(fs_info, trans);
  3037. ret = 0;
  3038. } else if (ret < 0 && ret != -ENOENT)
  3039. btrfs_abort_transaction(trans, ret);
  3040. btrfs_end_log_trans(root);
  3041. return ret;
  3042. }
  3043. /*
  3044. * creates a range item in the log for 'dirid'. first_offset and
  3045. * last_offset tell us which parts of the key space the log should
  3046. * be considered authoritative for.
  3047. */
  3048. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  3049. struct btrfs_root *log,
  3050. struct btrfs_path *path,
  3051. int key_type, u64 dirid,
  3052. u64 first_offset, u64 last_offset)
  3053. {
  3054. int ret;
  3055. struct btrfs_key key;
  3056. struct btrfs_dir_log_item *item;
  3057. key.objectid = dirid;
  3058. key.offset = first_offset;
  3059. if (key_type == BTRFS_DIR_ITEM_KEY)
  3060. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  3061. else
  3062. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  3063. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  3064. if (ret)
  3065. return ret;
  3066. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3067. struct btrfs_dir_log_item);
  3068. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  3069. btrfs_mark_buffer_dirty(path->nodes[0]);
  3070. btrfs_release_path(path);
  3071. return 0;
  3072. }
  3073. /*
  3074. * log all the items included in the current transaction for a given
  3075. * directory. This also creates the range items in the log tree required
  3076. * to replay anything deleted before the fsync
  3077. */
  3078. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  3079. struct btrfs_root *root, struct btrfs_inode *inode,
  3080. struct btrfs_path *path,
  3081. struct btrfs_path *dst_path, int key_type,
  3082. struct btrfs_log_ctx *ctx,
  3083. u64 min_offset, u64 *last_offset_ret)
  3084. {
  3085. struct btrfs_key min_key;
  3086. struct btrfs_root *log = root->log_root;
  3087. struct extent_buffer *src;
  3088. int err = 0;
  3089. int ret;
  3090. int i;
  3091. int nritems;
  3092. u64 first_offset = min_offset;
  3093. u64 last_offset = (u64)-1;
  3094. u64 ino = btrfs_ino(inode);
  3095. log = root->log_root;
  3096. min_key.objectid = ino;
  3097. min_key.type = key_type;
  3098. min_key.offset = min_offset;
  3099. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  3100. /*
  3101. * we didn't find anything from this transaction, see if there
  3102. * is anything at all
  3103. */
  3104. if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
  3105. min_key.objectid = ino;
  3106. min_key.type = key_type;
  3107. min_key.offset = (u64)-1;
  3108. btrfs_release_path(path);
  3109. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3110. if (ret < 0) {
  3111. btrfs_release_path(path);
  3112. return ret;
  3113. }
  3114. ret = btrfs_previous_item(root, path, ino, key_type);
  3115. /* if ret == 0 there are items for this type,
  3116. * create a range to tell us the last key of this type.
  3117. * otherwise, there are no items in this directory after
  3118. * *min_offset, and we create a range to indicate that.
  3119. */
  3120. if (ret == 0) {
  3121. struct btrfs_key tmp;
  3122. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  3123. path->slots[0]);
  3124. if (key_type == tmp.type)
  3125. first_offset = max(min_offset, tmp.offset) + 1;
  3126. }
  3127. goto done;
  3128. }
  3129. /* go backward to find any previous key */
  3130. ret = btrfs_previous_item(root, path, ino, key_type);
  3131. if (ret == 0) {
  3132. struct btrfs_key tmp;
  3133. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3134. if (key_type == tmp.type) {
  3135. first_offset = tmp.offset;
  3136. ret = overwrite_item(trans, log, dst_path,
  3137. path->nodes[0], path->slots[0],
  3138. &tmp);
  3139. if (ret) {
  3140. err = ret;
  3141. goto done;
  3142. }
  3143. }
  3144. }
  3145. btrfs_release_path(path);
  3146. /* find the first key from this transaction again */
  3147. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3148. if (WARN_ON(ret != 0))
  3149. goto done;
  3150. /*
  3151. * we have a block from this transaction, log every item in it
  3152. * from our directory
  3153. */
  3154. while (1) {
  3155. struct btrfs_key tmp;
  3156. src = path->nodes[0];
  3157. nritems = btrfs_header_nritems(src);
  3158. for (i = path->slots[0]; i < nritems; i++) {
  3159. struct btrfs_dir_item *di;
  3160. btrfs_item_key_to_cpu(src, &min_key, i);
  3161. if (min_key.objectid != ino || min_key.type != key_type)
  3162. goto done;
  3163. ret = overwrite_item(trans, log, dst_path, src, i,
  3164. &min_key);
  3165. if (ret) {
  3166. err = ret;
  3167. goto done;
  3168. }
  3169. /*
  3170. * We must make sure that when we log a directory entry,
  3171. * the corresponding inode, after log replay, has a
  3172. * matching link count. For example:
  3173. *
  3174. * touch foo
  3175. * mkdir mydir
  3176. * sync
  3177. * ln foo mydir/bar
  3178. * xfs_io -c "fsync" mydir
  3179. * <crash>
  3180. * <mount fs and log replay>
  3181. *
  3182. * Would result in a fsync log that when replayed, our
  3183. * file inode would have a link count of 1, but we get
  3184. * two directory entries pointing to the same inode.
  3185. * After removing one of the names, it would not be
  3186. * possible to remove the other name, which resulted
  3187. * always in stale file handle errors, and would not
  3188. * be possible to rmdir the parent directory, since
  3189. * its i_size could never decrement to the value
  3190. * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
  3191. */
  3192. di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
  3193. btrfs_dir_item_key_to_cpu(src, di, &tmp);
  3194. if (ctx &&
  3195. (btrfs_dir_transid(src, di) == trans->transid ||
  3196. btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
  3197. tmp.type != BTRFS_ROOT_ITEM_KEY)
  3198. ctx->log_new_dentries = true;
  3199. }
  3200. path->slots[0] = nritems;
  3201. /*
  3202. * look ahead to the next item and see if it is also
  3203. * from this directory and from this transaction
  3204. */
  3205. ret = btrfs_next_leaf(root, path);
  3206. if (ret) {
  3207. if (ret == 1)
  3208. last_offset = (u64)-1;
  3209. else
  3210. err = ret;
  3211. goto done;
  3212. }
  3213. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3214. if (tmp.objectid != ino || tmp.type != key_type) {
  3215. last_offset = (u64)-1;
  3216. goto done;
  3217. }
  3218. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  3219. ret = overwrite_item(trans, log, dst_path,
  3220. path->nodes[0], path->slots[0],
  3221. &tmp);
  3222. if (ret)
  3223. err = ret;
  3224. else
  3225. last_offset = tmp.offset;
  3226. goto done;
  3227. }
  3228. }
  3229. done:
  3230. btrfs_release_path(path);
  3231. btrfs_release_path(dst_path);
  3232. if (err == 0) {
  3233. *last_offset_ret = last_offset;
  3234. /*
  3235. * insert the log range keys to indicate where the log
  3236. * is valid
  3237. */
  3238. ret = insert_dir_log_key(trans, log, path, key_type,
  3239. ino, first_offset, last_offset);
  3240. if (ret)
  3241. err = ret;
  3242. }
  3243. return err;
  3244. }
  3245. /*
  3246. * logging directories is very similar to logging inodes, We find all the items
  3247. * from the current transaction and write them to the log.
  3248. *
  3249. * The recovery code scans the directory in the subvolume, and if it finds a
  3250. * key in the range logged that is not present in the log tree, then it means
  3251. * that dir entry was unlinked during the transaction.
  3252. *
  3253. * In order for that scan to work, we must include one key smaller than
  3254. * the smallest logged by this transaction and one key larger than the largest
  3255. * key logged by this transaction.
  3256. */
  3257. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  3258. struct btrfs_root *root, struct btrfs_inode *inode,
  3259. struct btrfs_path *path,
  3260. struct btrfs_path *dst_path,
  3261. struct btrfs_log_ctx *ctx)
  3262. {
  3263. u64 min_key;
  3264. u64 max_key;
  3265. int ret;
  3266. int key_type = BTRFS_DIR_ITEM_KEY;
  3267. again:
  3268. min_key = 0;
  3269. max_key = 0;
  3270. while (1) {
  3271. ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
  3272. ctx, min_key, &max_key);
  3273. if (ret)
  3274. return ret;
  3275. if (max_key == (u64)-1)
  3276. break;
  3277. min_key = max_key + 1;
  3278. }
  3279. if (key_type == BTRFS_DIR_ITEM_KEY) {
  3280. key_type = BTRFS_DIR_INDEX_KEY;
  3281. goto again;
  3282. }
  3283. return 0;
  3284. }
  3285. /*
  3286. * a helper function to drop items from the log before we relog an
  3287. * inode. max_key_type indicates the highest item type to remove.
  3288. * This cannot be run for file data extents because it does not
  3289. * free the extents they point to.
  3290. */
  3291. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  3292. struct btrfs_root *log,
  3293. struct btrfs_path *path,
  3294. u64 objectid, int max_key_type)
  3295. {
  3296. int ret;
  3297. struct btrfs_key key;
  3298. struct btrfs_key found_key;
  3299. int start_slot;
  3300. key.objectid = objectid;
  3301. key.type = max_key_type;
  3302. key.offset = (u64)-1;
  3303. while (1) {
  3304. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  3305. BUG_ON(ret == 0); /* Logic error */
  3306. if (ret < 0)
  3307. break;
  3308. if (path->slots[0] == 0)
  3309. break;
  3310. path->slots[0]--;
  3311. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3312. path->slots[0]);
  3313. if (found_key.objectid != objectid)
  3314. break;
  3315. found_key.offset = 0;
  3316. found_key.type = 0;
  3317. ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
  3318. &start_slot);
  3319. ret = btrfs_del_items(trans, log, path, start_slot,
  3320. path->slots[0] - start_slot + 1);
  3321. /*
  3322. * If start slot isn't 0 then we don't need to re-search, we've
  3323. * found the last guy with the objectid in this tree.
  3324. */
  3325. if (ret || start_slot != 0)
  3326. break;
  3327. btrfs_release_path(path);
  3328. }
  3329. btrfs_release_path(path);
  3330. if (ret > 0)
  3331. ret = 0;
  3332. return ret;
  3333. }
  3334. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3335. struct extent_buffer *leaf,
  3336. struct btrfs_inode_item *item,
  3337. struct inode *inode, int log_inode_only,
  3338. u64 logged_isize)
  3339. {
  3340. struct btrfs_map_token token;
  3341. btrfs_init_map_token(&token);
  3342. if (log_inode_only) {
  3343. /* set the generation to zero so the recover code
  3344. * can tell the difference between an logging
  3345. * just to say 'this inode exists' and a logging
  3346. * to say 'update this inode with these values'
  3347. */
  3348. btrfs_set_token_inode_generation(leaf, item, 0, &token);
  3349. btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
  3350. } else {
  3351. btrfs_set_token_inode_generation(leaf, item,
  3352. BTRFS_I(inode)->generation,
  3353. &token);
  3354. btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
  3355. }
  3356. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  3357. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  3358. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  3359. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  3360. btrfs_set_token_timespec_sec(leaf, &item->atime,
  3361. inode->i_atime.tv_sec, &token);
  3362. btrfs_set_token_timespec_nsec(leaf, &item->atime,
  3363. inode->i_atime.tv_nsec, &token);
  3364. btrfs_set_token_timespec_sec(leaf, &item->mtime,
  3365. inode->i_mtime.tv_sec, &token);
  3366. btrfs_set_token_timespec_nsec(leaf, &item->mtime,
  3367. inode->i_mtime.tv_nsec, &token);
  3368. btrfs_set_token_timespec_sec(leaf, &item->ctime,
  3369. inode->i_ctime.tv_sec, &token);
  3370. btrfs_set_token_timespec_nsec(leaf, &item->ctime,
  3371. inode->i_ctime.tv_nsec, &token);
  3372. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  3373. &token);
  3374. btrfs_set_token_inode_sequence(leaf, item,
  3375. inode_peek_iversion(inode), &token);
  3376. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  3377. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  3378. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  3379. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  3380. }
  3381. static int log_inode_item(struct btrfs_trans_handle *trans,
  3382. struct btrfs_root *log, struct btrfs_path *path,
  3383. struct btrfs_inode *inode)
  3384. {
  3385. struct btrfs_inode_item *inode_item;
  3386. int ret;
  3387. ret = btrfs_insert_empty_item(trans, log, path,
  3388. &inode->location, sizeof(*inode_item));
  3389. if (ret && ret != -EEXIST)
  3390. return ret;
  3391. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3392. struct btrfs_inode_item);
  3393. fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
  3394. 0, 0);
  3395. btrfs_release_path(path);
  3396. return 0;
  3397. }
  3398. static noinline int copy_items(struct btrfs_trans_handle *trans,
  3399. struct btrfs_inode *inode,
  3400. struct btrfs_path *dst_path,
  3401. struct btrfs_path *src_path, u64 *last_extent,
  3402. int start_slot, int nr, int inode_only,
  3403. u64 logged_isize)
  3404. {
  3405. struct btrfs_fs_info *fs_info = trans->fs_info;
  3406. unsigned long src_offset;
  3407. unsigned long dst_offset;
  3408. struct btrfs_root *log = inode->root->log_root;
  3409. struct btrfs_file_extent_item *extent;
  3410. struct btrfs_inode_item *inode_item;
  3411. struct extent_buffer *src = src_path->nodes[0];
  3412. struct btrfs_key first_key, last_key, key;
  3413. int ret;
  3414. struct btrfs_key *ins_keys;
  3415. u32 *ins_sizes;
  3416. char *ins_data;
  3417. int i;
  3418. struct list_head ordered_sums;
  3419. int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
  3420. bool has_extents = false;
  3421. bool need_find_last_extent = true;
  3422. bool done = false;
  3423. INIT_LIST_HEAD(&ordered_sums);
  3424. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  3425. nr * sizeof(u32), GFP_NOFS);
  3426. if (!ins_data)
  3427. return -ENOMEM;
  3428. first_key.objectid = (u64)-1;
  3429. ins_sizes = (u32 *)ins_data;
  3430. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  3431. for (i = 0; i < nr; i++) {
  3432. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  3433. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  3434. }
  3435. ret = btrfs_insert_empty_items(trans, log, dst_path,
  3436. ins_keys, ins_sizes, nr);
  3437. if (ret) {
  3438. kfree(ins_data);
  3439. return ret;
  3440. }
  3441. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  3442. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  3443. dst_path->slots[0]);
  3444. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  3445. if (i == nr - 1)
  3446. last_key = ins_keys[i];
  3447. if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  3448. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  3449. dst_path->slots[0],
  3450. struct btrfs_inode_item);
  3451. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  3452. &inode->vfs_inode,
  3453. inode_only == LOG_INODE_EXISTS,
  3454. logged_isize);
  3455. } else {
  3456. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  3457. src_offset, ins_sizes[i]);
  3458. }
  3459. /*
  3460. * We set need_find_last_extent here in case we know we were
  3461. * processing other items and then walk into the first extent in
  3462. * the inode. If we don't hit an extent then nothing changes,
  3463. * we'll do the last search the next time around.
  3464. */
  3465. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
  3466. has_extents = true;
  3467. if (first_key.objectid == (u64)-1)
  3468. first_key = ins_keys[i];
  3469. } else {
  3470. need_find_last_extent = false;
  3471. }
  3472. /* take a reference on file data extents so that truncates
  3473. * or deletes of this inode don't have to relog the inode
  3474. * again
  3475. */
  3476. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
  3477. !skip_csum) {
  3478. int found_type;
  3479. extent = btrfs_item_ptr(src, start_slot + i,
  3480. struct btrfs_file_extent_item);
  3481. if (btrfs_file_extent_generation(src, extent) < trans->transid)
  3482. continue;
  3483. found_type = btrfs_file_extent_type(src, extent);
  3484. if (found_type == BTRFS_FILE_EXTENT_REG) {
  3485. u64 ds, dl, cs, cl;
  3486. ds = btrfs_file_extent_disk_bytenr(src,
  3487. extent);
  3488. /* ds == 0 is a hole */
  3489. if (ds == 0)
  3490. continue;
  3491. dl = btrfs_file_extent_disk_num_bytes(src,
  3492. extent);
  3493. cs = btrfs_file_extent_offset(src, extent);
  3494. cl = btrfs_file_extent_num_bytes(src,
  3495. extent);
  3496. if (btrfs_file_extent_compression(src,
  3497. extent)) {
  3498. cs = 0;
  3499. cl = dl;
  3500. }
  3501. ret = btrfs_lookup_csums_range(
  3502. fs_info->csum_root,
  3503. ds + cs, ds + cs + cl - 1,
  3504. &ordered_sums, 0);
  3505. if (ret) {
  3506. btrfs_release_path(dst_path);
  3507. kfree(ins_data);
  3508. return ret;
  3509. }
  3510. }
  3511. }
  3512. }
  3513. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  3514. btrfs_release_path(dst_path);
  3515. kfree(ins_data);
  3516. /*
  3517. * we have to do this after the loop above to avoid changing the
  3518. * log tree while trying to change the log tree.
  3519. */
  3520. ret = 0;
  3521. while (!list_empty(&ordered_sums)) {
  3522. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3523. struct btrfs_ordered_sum,
  3524. list);
  3525. if (!ret)
  3526. ret = btrfs_csum_file_blocks(trans, log, sums);
  3527. list_del(&sums->list);
  3528. kfree(sums);
  3529. }
  3530. if (!has_extents)
  3531. return ret;
  3532. if (need_find_last_extent && *last_extent == first_key.offset) {
  3533. /*
  3534. * We don't have any leafs between our current one and the one
  3535. * we processed before that can have file extent items for our
  3536. * inode (and have a generation number smaller than our current
  3537. * transaction id).
  3538. */
  3539. need_find_last_extent = false;
  3540. }
  3541. /*
  3542. * Because we use btrfs_search_forward we could skip leaves that were
  3543. * not modified and then assume *last_extent is valid when it really
  3544. * isn't. So back up to the previous leaf and read the end of the last
  3545. * extent before we go and fill in holes.
  3546. */
  3547. if (need_find_last_extent) {
  3548. u64 len;
  3549. ret = btrfs_prev_leaf(inode->root, src_path);
  3550. if (ret < 0)
  3551. return ret;
  3552. if (ret)
  3553. goto fill_holes;
  3554. if (src_path->slots[0])
  3555. src_path->slots[0]--;
  3556. src = src_path->nodes[0];
  3557. btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
  3558. if (key.objectid != btrfs_ino(inode) ||
  3559. key.type != BTRFS_EXTENT_DATA_KEY)
  3560. goto fill_holes;
  3561. extent = btrfs_item_ptr(src, src_path->slots[0],
  3562. struct btrfs_file_extent_item);
  3563. if (btrfs_file_extent_type(src, extent) ==
  3564. BTRFS_FILE_EXTENT_INLINE) {
  3565. len = btrfs_file_extent_ram_bytes(src, extent);
  3566. *last_extent = ALIGN(key.offset + len,
  3567. fs_info->sectorsize);
  3568. } else {
  3569. len = btrfs_file_extent_num_bytes(src, extent);
  3570. *last_extent = key.offset + len;
  3571. }
  3572. }
  3573. fill_holes:
  3574. /* So we did prev_leaf, now we need to move to the next leaf, but a few
  3575. * things could have happened
  3576. *
  3577. * 1) A merge could have happened, so we could currently be on a leaf
  3578. * that holds what we were copying in the first place.
  3579. * 2) A split could have happened, and now not all of the items we want
  3580. * are on the same leaf.
  3581. *
  3582. * So we need to adjust how we search for holes, we need to drop the
  3583. * path and re-search for the first extent key we found, and then walk
  3584. * forward until we hit the last one we copied.
  3585. */
  3586. if (need_find_last_extent) {
  3587. /* btrfs_prev_leaf could return 1 without releasing the path */
  3588. btrfs_release_path(src_path);
  3589. ret = btrfs_search_slot(NULL, inode->root, &first_key,
  3590. src_path, 0, 0);
  3591. if (ret < 0)
  3592. return ret;
  3593. ASSERT(ret == 0);
  3594. src = src_path->nodes[0];
  3595. i = src_path->slots[0];
  3596. } else {
  3597. i = start_slot;
  3598. }
  3599. /*
  3600. * Ok so here we need to go through and fill in any holes we may have
  3601. * to make sure that holes are punched for those areas in case they had
  3602. * extents previously.
  3603. */
  3604. while (!done) {
  3605. u64 offset, len;
  3606. u64 extent_end;
  3607. if (i >= btrfs_header_nritems(src_path->nodes[0])) {
  3608. ret = btrfs_next_leaf(inode->root, src_path);
  3609. if (ret < 0)
  3610. return ret;
  3611. ASSERT(ret == 0);
  3612. src = src_path->nodes[0];
  3613. i = 0;
  3614. need_find_last_extent = true;
  3615. }
  3616. btrfs_item_key_to_cpu(src, &key, i);
  3617. if (!btrfs_comp_cpu_keys(&key, &last_key))
  3618. done = true;
  3619. if (key.objectid != btrfs_ino(inode) ||
  3620. key.type != BTRFS_EXTENT_DATA_KEY) {
  3621. i++;
  3622. continue;
  3623. }
  3624. extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
  3625. if (btrfs_file_extent_type(src, extent) ==
  3626. BTRFS_FILE_EXTENT_INLINE) {
  3627. len = btrfs_file_extent_ram_bytes(src, extent);
  3628. extent_end = ALIGN(key.offset + len,
  3629. fs_info->sectorsize);
  3630. } else {
  3631. len = btrfs_file_extent_num_bytes(src, extent);
  3632. extent_end = key.offset + len;
  3633. }
  3634. i++;
  3635. if (*last_extent == key.offset) {
  3636. *last_extent = extent_end;
  3637. continue;
  3638. }
  3639. offset = *last_extent;
  3640. len = key.offset - *last_extent;
  3641. ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
  3642. offset, 0, 0, len, 0, len, 0, 0, 0);
  3643. if (ret)
  3644. break;
  3645. *last_extent = extent_end;
  3646. }
  3647. /*
  3648. * Check if there is a hole between the last extent found in our leaf
  3649. * and the first extent in the next leaf. If there is one, we need to
  3650. * log an explicit hole so that at replay time we can punch the hole.
  3651. */
  3652. if (ret == 0 &&
  3653. key.objectid == btrfs_ino(inode) &&
  3654. key.type == BTRFS_EXTENT_DATA_KEY &&
  3655. i == btrfs_header_nritems(src_path->nodes[0])) {
  3656. ret = btrfs_next_leaf(inode->root, src_path);
  3657. need_find_last_extent = true;
  3658. if (ret > 0) {
  3659. ret = 0;
  3660. } else if (ret == 0) {
  3661. btrfs_item_key_to_cpu(src_path->nodes[0], &key,
  3662. src_path->slots[0]);
  3663. if (key.objectid == btrfs_ino(inode) &&
  3664. key.type == BTRFS_EXTENT_DATA_KEY &&
  3665. *last_extent < key.offset) {
  3666. const u64 len = key.offset - *last_extent;
  3667. ret = btrfs_insert_file_extent(trans, log,
  3668. btrfs_ino(inode),
  3669. *last_extent, 0,
  3670. 0, len, 0, len,
  3671. 0, 0, 0);
  3672. }
  3673. }
  3674. }
  3675. /*
  3676. * Need to let the callers know we dropped the path so they should
  3677. * re-search.
  3678. */
  3679. if (!ret && need_find_last_extent)
  3680. ret = 1;
  3681. return ret;
  3682. }
  3683. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  3684. {
  3685. struct extent_map *em1, *em2;
  3686. em1 = list_entry(a, struct extent_map, list);
  3687. em2 = list_entry(b, struct extent_map, list);
  3688. if (em1->start < em2->start)
  3689. return -1;
  3690. else if (em1->start > em2->start)
  3691. return 1;
  3692. return 0;
  3693. }
  3694. static int log_extent_csums(struct btrfs_trans_handle *trans,
  3695. struct btrfs_inode *inode,
  3696. struct btrfs_root *log_root,
  3697. const struct extent_map *em)
  3698. {
  3699. u64 csum_offset;
  3700. u64 csum_len;
  3701. LIST_HEAD(ordered_sums);
  3702. int ret = 0;
  3703. if (inode->flags & BTRFS_INODE_NODATASUM ||
  3704. test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  3705. em->block_start == EXTENT_MAP_HOLE)
  3706. return 0;
  3707. /* If we're compressed we have to save the entire range of csums. */
  3708. if (em->compress_type) {
  3709. csum_offset = 0;
  3710. csum_len = max(em->block_len, em->orig_block_len);
  3711. } else {
  3712. csum_offset = em->mod_start - em->start;
  3713. csum_len = em->mod_len;
  3714. }
  3715. /* block start is already adjusted for the file extent offset. */
  3716. ret = btrfs_lookup_csums_range(trans->fs_info->csum_root,
  3717. em->block_start + csum_offset,
  3718. em->block_start + csum_offset +
  3719. csum_len - 1, &ordered_sums, 0);
  3720. if (ret)
  3721. return ret;
  3722. while (!list_empty(&ordered_sums)) {
  3723. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3724. struct btrfs_ordered_sum,
  3725. list);
  3726. if (!ret)
  3727. ret = btrfs_csum_file_blocks(trans, log_root, sums);
  3728. list_del(&sums->list);
  3729. kfree(sums);
  3730. }
  3731. return ret;
  3732. }
  3733. static int log_one_extent(struct btrfs_trans_handle *trans,
  3734. struct btrfs_inode *inode, struct btrfs_root *root,
  3735. const struct extent_map *em,
  3736. struct btrfs_path *path,
  3737. struct btrfs_log_ctx *ctx)
  3738. {
  3739. struct btrfs_root *log = root->log_root;
  3740. struct btrfs_file_extent_item *fi;
  3741. struct extent_buffer *leaf;
  3742. struct btrfs_map_token token;
  3743. struct btrfs_key key;
  3744. u64 extent_offset = em->start - em->orig_start;
  3745. u64 block_len;
  3746. int ret;
  3747. int extent_inserted = 0;
  3748. ret = log_extent_csums(trans, inode, log, em);
  3749. if (ret)
  3750. return ret;
  3751. btrfs_init_map_token(&token);
  3752. ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
  3753. em->start + em->len, NULL, 0, 1,
  3754. sizeof(*fi), &extent_inserted);
  3755. if (ret)
  3756. return ret;
  3757. if (!extent_inserted) {
  3758. key.objectid = btrfs_ino(inode);
  3759. key.type = BTRFS_EXTENT_DATA_KEY;
  3760. key.offset = em->start;
  3761. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3762. sizeof(*fi));
  3763. if (ret)
  3764. return ret;
  3765. }
  3766. leaf = path->nodes[0];
  3767. fi = btrfs_item_ptr(leaf, path->slots[0],
  3768. struct btrfs_file_extent_item);
  3769. btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
  3770. &token);
  3771. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  3772. btrfs_set_token_file_extent_type(leaf, fi,
  3773. BTRFS_FILE_EXTENT_PREALLOC,
  3774. &token);
  3775. else
  3776. btrfs_set_token_file_extent_type(leaf, fi,
  3777. BTRFS_FILE_EXTENT_REG,
  3778. &token);
  3779. block_len = max(em->block_len, em->orig_block_len);
  3780. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  3781. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3782. em->block_start,
  3783. &token);
  3784. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3785. &token);
  3786. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  3787. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3788. em->block_start -
  3789. extent_offset, &token);
  3790. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3791. &token);
  3792. } else {
  3793. btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
  3794. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
  3795. &token);
  3796. }
  3797. btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
  3798. btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
  3799. btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
  3800. btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
  3801. &token);
  3802. btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
  3803. btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
  3804. btrfs_mark_buffer_dirty(leaf);
  3805. btrfs_release_path(path);
  3806. return ret;
  3807. }
  3808. /*
  3809. * Log all prealloc extents beyond the inode's i_size to make sure we do not
  3810. * lose them after doing a fast fsync and replaying the log. We scan the
  3811. * subvolume's root instead of iterating the inode's extent map tree because
  3812. * otherwise we can log incorrect extent items based on extent map conversion.
  3813. * That can happen due to the fact that extent maps are merged when they
  3814. * are not in the extent map tree's list of modified extents.
  3815. */
  3816. static int btrfs_log_prealloc_extents(struct btrfs_trans_handle *trans,
  3817. struct btrfs_inode *inode,
  3818. struct btrfs_path *path)
  3819. {
  3820. struct btrfs_root *root = inode->root;
  3821. struct btrfs_key key;
  3822. const u64 i_size = i_size_read(&inode->vfs_inode);
  3823. const u64 ino = btrfs_ino(inode);
  3824. struct btrfs_path *dst_path = NULL;
  3825. u64 last_extent = (u64)-1;
  3826. int ins_nr = 0;
  3827. int start_slot;
  3828. int ret;
  3829. if (!(inode->flags & BTRFS_INODE_PREALLOC))
  3830. return 0;
  3831. key.objectid = ino;
  3832. key.type = BTRFS_EXTENT_DATA_KEY;
  3833. key.offset = i_size;
  3834. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3835. if (ret < 0)
  3836. goto out;
  3837. while (true) {
  3838. struct extent_buffer *leaf = path->nodes[0];
  3839. int slot = path->slots[0];
  3840. if (slot >= btrfs_header_nritems(leaf)) {
  3841. if (ins_nr > 0) {
  3842. ret = copy_items(trans, inode, dst_path, path,
  3843. &last_extent, start_slot,
  3844. ins_nr, 1, 0);
  3845. if (ret < 0)
  3846. goto out;
  3847. ins_nr = 0;
  3848. }
  3849. ret = btrfs_next_leaf(root, path);
  3850. if (ret < 0)
  3851. goto out;
  3852. if (ret > 0) {
  3853. ret = 0;
  3854. break;
  3855. }
  3856. continue;
  3857. }
  3858. btrfs_item_key_to_cpu(leaf, &key, slot);
  3859. if (key.objectid > ino)
  3860. break;
  3861. if (WARN_ON_ONCE(key.objectid < ino) ||
  3862. key.type < BTRFS_EXTENT_DATA_KEY ||
  3863. key.offset < i_size) {
  3864. path->slots[0]++;
  3865. continue;
  3866. }
  3867. if (last_extent == (u64)-1) {
  3868. last_extent = key.offset;
  3869. /*
  3870. * Avoid logging extent items logged in past fsync calls
  3871. * and leading to duplicate keys in the log tree.
  3872. */
  3873. do {
  3874. ret = btrfs_truncate_inode_items(trans,
  3875. root->log_root,
  3876. &inode->vfs_inode,
  3877. i_size,
  3878. BTRFS_EXTENT_DATA_KEY);
  3879. } while (ret == -EAGAIN);
  3880. if (ret)
  3881. goto out;
  3882. }
  3883. if (ins_nr == 0)
  3884. start_slot = slot;
  3885. ins_nr++;
  3886. path->slots[0]++;
  3887. if (!dst_path) {
  3888. dst_path = btrfs_alloc_path();
  3889. if (!dst_path) {
  3890. ret = -ENOMEM;
  3891. goto out;
  3892. }
  3893. }
  3894. }
  3895. if (ins_nr > 0) {
  3896. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  3897. start_slot, ins_nr, 1, 0);
  3898. if (ret > 0)
  3899. ret = 0;
  3900. }
  3901. out:
  3902. btrfs_release_path(path);
  3903. btrfs_free_path(dst_path);
  3904. return ret;
  3905. }
  3906. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3907. struct btrfs_root *root,
  3908. struct btrfs_inode *inode,
  3909. struct btrfs_path *path,
  3910. struct btrfs_log_ctx *ctx,
  3911. const u64 start,
  3912. const u64 end)
  3913. {
  3914. struct extent_map *em, *n;
  3915. struct list_head extents;
  3916. struct extent_map_tree *tree = &inode->extent_tree;
  3917. u64 logged_start, logged_end;
  3918. u64 test_gen;
  3919. int ret = 0;
  3920. int num = 0;
  3921. INIT_LIST_HEAD(&extents);
  3922. down_write(&inode->dio_sem);
  3923. write_lock(&tree->lock);
  3924. test_gen = root->fs_info->last_trans_committed;
  3925. logged_start = start;
  3926. logged_end = end;
  3927. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3928. list_del_init(&em->list);
  3929. /*
  3930. * Just an arbitrary number, this can be really CPU intensive
  3931. * once we start getting a lot of extents, and really once we
  3932. * have a bunch of extents we just want to commit since it will
  3933. * be faster.
  3934. */
  3935. if (++num > 32768) {
  3936. list_del_init(&tree->modified_extents);
  3937. ret = -EFBIG;
  3938. goto process;
  3939. }
  3940. if (em->generation <= test_gen)
  3941. continue;
  3942. /* We log prealloc extents beyond eof later. */
  3943. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) &&
  3944. em->start >= i_size_read(&inode->vfs_inode))
  3945. continue;
  3946. if (em->start < logged_start)
  3947. logged_start = em->start;
  3948. if ((em->start + em->len - 1) > logged_end)
  3949. logged_end = em->start + em->len - 1;
  3950. /* Need a ref to keep it from getting evicted from cache */
  3951. refcount_inc(&em->refs);
  3952. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  3953. list_add_tail(&em->list, &extents);
  3954. num++;
  3955. }
  3956. list_sort(NULL, &extents, extent_cmp);
  3957. process:
  3958. while (!list_empty(&extents)) {
  3959. em = list_entry(extents.next, struct extent_map, list);
  3960. list_del_init(&em->list);
  3961. /*
  3962. * If we had an error we just need to delete everybody from our
  3963. * private list.
  3964. */
  3965. if (ret) {
  3966. clear_em_logging(tree, em);
  3967. free_extent_map(em);
  3968. continue;
  3969. }
  3970. write_unlock(&tree->lock);
  3971. ret = log_one_extent(trans, inode, root, em, path, ctx);
  3972. write_lock(&tree->lock);
  3973. clear_em_logging(tree, em);
  3974. free_extent_map(em);
  3975. }
  3976. WARN_ON(!list_empty(&extents));
  3977. write_unlock(&tree->lock);
  3978. up_write(&inode->dio_sem);
  3979. btrfs_release_path(path);
  3980. if (!ret)
  3981. ret = btrfs_log_prealloc_extents(trans, inode, path);
  3982. return ret;
  3983. }
  3984. static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
  3985. struct btrfs_path *path, u64 *size_ret)
  3986. {
  3987. struct btrfs_key key;
  3988. int ret;
  3989. key.objectid = btrfs_ino(inode);
  3990. key.type = BTRFS_INODE_ITEM_KEY;
  3991. key.offset = 0;
  3992. ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
  3993. if (ret < 0) {
  3994. return ret;
  3995. } else if (ret > 0) {
  3996. *size_ret = 0;
  3997. } else {
  3998. struct btrfs_inode_item *item;
  3999. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4000. struct btrfs_inode_item);
  4001. *size_ret = btrfs_inode_size(path->nodes[0], item);
  4002. }
  4003. btrfs_release_path(path);
  4004. return 0;
  4005. }
  4006. /*
  4007. * At the moment we always log all xattrs. This is to figure out at log replay
  4008. * time which xattrs must have their deletion replayed. If a xattr is missing
  4009. * in the log tree and exists in the fs/subvol tree, we delete it. This is
  4010. * because if a xattr is deleted, the inode is fsynced and a power failure
  4011. * happens, causing the log to be replayed the next time the fs is mounted,
  4012. * we want the xattr to not exist anymore (same behaviour as other filesystems
  4013. * with a journal, ext3/4, xfs, f2fs, etc).
  4014. */
  4015. static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
  4016. struct btrfs_root *root,
  4017. struct btrfs_inode *inode,
  4018. struct btrfs_path *path,
  4019. struct btrfs_path *dst_path)
  4020. {
  4021. int ret;
  4022. struct btrfs_key key;
  4023. const u64 ino = btrfs_ino(inode);
  4024. int ins_nr = 0;
  4025. int start_slot = 0;
  4026. key.objectid = ino;
  4027. key.type = BTRFS_XATTR_ITEM_KEY;
  4028. key.offset = 0;
  4029. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4030. if (ret < 0)
  4031. return ret;
  4032. while (true) {
  4033. int slot = path->slots[0];
  4034. struct extent_buffer *leaf = path->nodes[0];
  4035. int nritems = btrfs_header_nritems(leaf);
  4036. if (slot >= nritems) {
  4037. if (ins_nr > 0) {
  4038. u64 last_extent = 0;
  4039. ret = copy_items(trans, inode, dst_path, path,
  4040. &last_extent, start_slot,
  4041. ins_nr, 1, 0);
  4042. /* can't be 1, extent items aren't processed */
  4043. ASSERT(ret <= 0);
  4044. if (ret < 0)
  4045. return ret;
  4046. ins_nr = 0;
  4047. }
  4048. ret = btrfs_next_leaf(root, path);
  4049. if (ret < 0)
  4050. return ret;
  4051. else if (ret > 0)
  4052. break;
  4053. continue;
  4054. }
  4055. btrfs_item_key_to_cpu(leaf, &key, slot);
  4056. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
  4057. break;
  4058. if (ins_nr == 0)
  4059. start_slot = slot;
  4060. ins_nr++;
  4061. path->slots[0]++;
  4062. cond_resched();
  4063. }
  4064. if (ins_nr > 0) {
  4065. u64 last_extent = 0;
  4066. ret = copy_items(trans, inode, dst_path, path,
  4067. &last_extent, start_slot,
  4068. ins_nr, 1, 0);
  4069. /* can't be 1, extent items aren't processed */
  4070. ASSERT(ret <= 0);
  4071. if (ret < 0)
  4072. return ret;
  4073. }
  4074. return 0;
  4075. }
  4076. /*
  4077. * If the no holes feature is enabled we need to make sure any hole between the
  4078. * last extent and the i_size of our inode is explicitly marked in the log. This
  4079. * is to make sure that doing something like:
  4080. *
  4081. * 1) create file with 128Kb of data
  4082. * 2) truncate file to 64Kb
  4083. * 3) truncate file to 256Kb
  4084. * 4) fsync file
  4085. * 5) <crash/power failure>
  4086. * 6) mount fs and trigger log replay
  4087. *
  4088. * Will give us a file with a size of 256Kb, the first 64Kb of data match what
  4089. * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
  4090. * file correspond to a hole. The presence of explicit holes in a log tree is
  4091. * what guarantees that log replay will remove/adjust file extent items in the
  4092. * fs/subvol tree.
  4093. *
  4094. * Here we do not need to care about holes between extents, that is already done
  4095. * by copy_items(). We also only need to do this in the full sync path, where we
  4096. * lookup for extents from the fs/subvol tree only. In the fast path case, we
  4097. * lookup the list of modified extent maps and if any represents a hole, we
  4098. * insert a corresponding extent representing a hole in the log tree.
  4099. */
  4100. static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
  4101. struct btrfs_root *root,
  4102. struct btrfs_inode *inode,
  4103. struct btrfs_path *path)
  4104. {
  4105. struct btrfs_fs_info *fs_info = root->fs_info;
  4106. int ret;
  4107. struct btrfs_key key;
  4108. u64 hole_start;
  4109. u64 hole_size;
  4110. struct extent_buffer *leaf;
  4111. struct btrfs_root *log = root->log_root;
  4112. const u64 ino = btrfs_ino(inode);
  4113. const u64 i_size = i_size_read(&inode->vfs_inode);
  4114. if (!btrfs_fs_incompat(fs_info, NO_HOLES))
  4115. return 0;
  4116. key.objectid = ino;
  4117. key.type = BTRFS_EXTENT_DATA_KEY;
  4118. key.offset = (u64)-1;
  4119. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4120. ASSERT(ret != 0);
  4121. if (ret < 0)
  4122. return ret;
  4123. ASSERT(path->slots[0] > 0);
  4124. path->slots[0]--;
  4125. leaf = path->nodes[0];
  4126. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4127. if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
  4128. /* inode does not have any extents */
  4129. hole_start = 0;
  4130. hole_size = i_size;
  4131. } else {
  4132. struct btrfs_file_extent_item *extent;
  4133. u64 len;
  4134. /*
  4135. * If there's an extent beyond i_size, an explicit hole was
  4136. * already inserted by copy_items().
  4137. */
  4138. if (key.offset >= i_size)
  4139. return 0;
  4140. extent = btrfs_item_ptr(leaf, path->slots[0],
  4141. struct btrfs_file_extent_item);
  4142. if (btrfs_file_extent_type(leaf, extent) ==
  4143. BTRFS_FILE_EXTENT_INLINE) {
  4144. len = btrfs_file_extent_ram_bytes(leaf, extent);
  4145. ASSERT(len == i_size ||
  4146. (len == fs_info->sectorsize &&
  4147. btrfs_file_extent_compression(leaf, extent) !=
  4148. BTRFS_COMPRESS_NONE));
  4149. return 0;
  4150. }
  4151. len = btrfs_file_extent_num_bytes(leaf, extent);
  4152. /* Last extent goes beyond i_size, no need to log a hole. */
  4153. if (key.offset + len > i_size)
  4154. return 0;
  4155. hole_start = key.offset + len;
  4156. hole_size = i_size - hole_start;
  4157. }
  4158. btrfs_release_path(path);
  4159. /* Last extent ends at i_size. */
  4160. if (hole_size == 0)
  4161. return 0;
  4162. hole_size = ALIGN(hole_size, fs_info->sectorsize);
  4163. ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
  4164. hole_size, 0, hole_size, 0, 0, 0);
  4165. return ret;
  4166. }
  4167. /*
  4168. * When we are logging a new inode X, check if it doesn't have a reference that
  4169. * matches the reference from some other inode Y created in a past transaction
  4170. * and that was renamed in the current transaction. If we don't do this, then at
  4171. * log replay time we can lose inode Y (and all its files if it's a directory):
  4172. *
  4173. * mkdir /mnt/x
  4174. * echo "hello world" > /mnt/x/foobar
  4175. * sync
  4176. * mv /mnt/x /mnt/y
  4177. * mkdir /mnt/x # or touch /mnt/x
  4178. * xfs_io -c fsync /mnt/x
  4179. * <power fail>
  4180. * mount fs, trigger log replay
  4181. *
  4182. * After the log replay procedure, we would lose the first directory and all its
  4183. * files (file foobar).
  4184. * For the case where inode Y is not a directory we simply end up losing it:
  4185. *
  4186. * echo "123" > /mnt/foo
  4187. * sync
  4188. * mv /mnt/foo /mnt/bar
  4189. * echo "abc" > /mnt/foo
  4190. * xfs_io -c fsync /mnt/foo
  4191. * <power fail>
  4192. *
  4193. * We also need this for cases where a snapshot entry is replaced by some other
  4194. * entry (file or directory) otherwise we end up with an unreplayable log due to
  4195. * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
  4196. * if it were a regular entry:
  4197. *
  4198. * mkdir /mnt/x
  4199. * btrfs subvolume snapshot /mnt /mnt/x/snap
  4200. * btrfs subvolume delete /mnt/x/snap
  4201. * rmdir /mnt/x
  4202. * mkdir /mnt/x
  4203. * fsync /mnt/x or fsync some new file inside it
  4204. * <power fail>
  4205. *
  4206. * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
  4207. * the same transaction.
  4208. */
  4209. static int btrfs_check_ref_name_override(struct extent_buffer *eb,
  4210. const int slot,
  4211. const struct btrfs_key *key,
  4212. struct btrfs_inode *inode,
  4213. u64 *other_ino)
  4214. {
  4215. int ret;
  4216. struct btrfs_path *search_path;
  4217. char *name = NULL;
  4218. u32 name_len = 0;
  4219. u32 item_size = btrfs_item_size_nr(eb, slot);
  4220. u32 cur_offset = 0;
  4221. unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
  4222. search_path = btrfs_alloc_path();
  4223. if (!search_path)
  4224. return -ENOMEM;
  4225. search_path->search_commit_root = 1;
  4226. search_path->skip_locking = 1;
  4227. while (cur_offset < item_size) {
  4228. u64 parent;
  4229. u32 this_name_len;
  4230. u32 this_len;
  4231. unsigned long name_ptr;
  4232. struct btrfs_dir_item *di;
  4233. if (key->type == BTRFS_INODE_REF_KEY) {
  4234. struct btrfs_inode_ref *iref;
  4235. iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  4236. parent = key->offset;
  4237. this_name_len = btrfs_inode_ref_name_len(eb, iref);
  4238. name_ptr = (unsigned long)(iref + 1);
  4239. this_len = sizeof(*iref) + this_name_len;
  4240. } else {
  4241. struct btrfs_inode_extref *extref;
  4242. extref = (struct btrfs_inode_extref *)(ptr +
  4243. cur_offset);
  4244. parent = btrfs_inode_extref_parent(eb, extref);
  4245. this_name_len = btrfs_inode_extref_name_len(eb, extref);
  4246. name_ptr = (unsigned long)&extref->name;
  4247. this_len = sizeof(*extref) + this_name_len;
  4248. }
  4249. if (this_name_len > name_len) {
  4250. char *new_name;
  4251. new_name = krealloc(name, this_name_len, GFP_NOFS);
  4252. if (!new_name) {
  4253. ret = -ENOMEM;
  4254. goto out;
  4255. }
  4256. name_len = this_name_len;
  4257. name = new_name;
  4258. }
  4259. read_extent_buffer(eb, name, name_ptr, this_name_len);
  4260. di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
  4261. parent, name, this_name_len, 0);
  4262. if (di && !IS_ERR(di)) {
  4263. struct btrfs_key di_key;
  4264. btrfs_dir_item_key_to_cpu(search_path->nodes[0],
  4265. di, &di_key);
  4266. if (di_key.type == BTRFS_INODE_ITEM_KEY) {
  4267. ret = 1;
  4268. *other_ino = di_key.objectid;
  4269. } else {
  4270. ret = -EAGAIN;
  4271. }
  4272. goto out;
  4273. } else if (IS_ERR(di)) {
  4274. ret = PTR_ERR(di);
  4275. goto out;
  4276. }
  4277. btrfs_release_path(search_path);
  4278. cur_offset += this_len;
  4279. }
  4280. ret = 0;
  4281. out:
  4282. btrfs_free_path(search_path);
  4283. kfree(name);
  4284. return ret;
  4285. }
  4286. /* log a single inode in the tree log.
  4287. * At least one parent directory for this inode must exist in the tree
  4288. * or be logged already.
  4289. *
  4290. * Any items from this inode changed by the current transaction are copied
  4291. * to the log tree. An extra reference is taken on any extents in this
  4292. * file, allowing us to avoid a whole pile of corner cases around logging
  4293. * blocks that have been removed from the tree.
  4294. *
  4295. * See LOG_INODE_ALL and related defines for a description of what inode_only
  4296. * does.
  4297. *
  4298. * This handles both files and directories.
  4299. */
  4300. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  4301. struct btrfs_root *root, struct btrfs_inode *inode,
  4302. int inode_only,
  4303. const loff_t start,
  4304. const loff_t end,
  4305. struct btrfs_log_ctx *ctx)
  4306. {
  4307. struct btrfs_fs_info *fs_info = root->fs_info;
  4308. struct btrfs_path *path;
  4309. struct btrfs_path *dst_path;
  4310. struct btrfs_key min_key;
  4311. struct btrfs_key max_key;
  4312. struct btrfs_root *log = root->log_root;
  4313. u64 last_extent = 0;
  4314. int err = 0;
  4315. int ret;
  4316. int nritems;
  4317. int ins_start_slot = 0;
  4318. int ins_nr;
  4319. bool fast_search = false;
  4320. u64 ino = btrfs_ino(inode);
  4321. struct extent_map_tree *em_tree = &inode->extent_tree;
  4322. u64 logged_isize = 0;
  4323. bool need_log_inode_item = true;
  4324. bool xattrs_logged = false;
  4325. path = btrfs_alloc_path();
  4326. if (!path)
  4327. return -ENOMEM;
  4328. dst_path = btrfs_alloc_path();
  4329. if (!dst_path) {
  4330. btrfs_free_path(path);
  4331. return -ENOMEM;
  4332. }
  4333. min_key.objectid = ino;
  4334. min_key.type = BTRFS_INODE_ITEM_KEY;
  4335. min_key.offset = 0;
  4336. max_key.objectid = ino;
  4337. /* today the code can only do partial logging of directories */
  4338. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  4339. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4340. &inode->runtime_flags) &&
  4341. inode_only >= LOG_INODE_EXISTS))
  4342. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4343. else
  4344. max_key.type = (u8)-1;
  4345. max_key.offset = (u64)-1;
  4346. /*
  4347. * Only run delayed items if we are a dir or a new file.
  4348. * Otherwise commit the delayed inode only, which is needed in
  4349. * order for the log replay code to mark inodes for link count
  4350. * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
  4351. */
  4352. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  4353. inode->generation > fs_info->last_trans_committed)
  4354. ret = btrfs_commit_inode_delayed_items(trans, inode);
  4355. else
  4356. ret = btrfs_commit_inode_delayed_inode(inode);
  4357. if (ret) {
  4358. btrfs_free_path(path);
  4359. btrfs_free_path(dst_path);
  4360. return ret;
  4361. }
  4362. if (inode_only == LOG_OTHER_INODE) {
  4363. inode_only = LOG_INODE_EXISTS;
  4364. mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
  4365. } else {
  4366. mutex_lock(&inode->log_mutex);
  4367. }
  4368. /*
  4369. * a brute force approach to making sure we get the most uptodate
  4370. * copies of everything.
  4371. */
  4372. if (S_ISDIR(inode->vfs_inode.i_mode)) {
  4373. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  4374. if (inode_only == LOG_INODE_EXISTS)
  4375. max_key_type = BTRFS_XATTR_ITEM_KEY;
  4376. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  4377. } else {
  4378. if (inode_only == LOG_INODE_EXISTS) {
  4379. /*
  4380. * Make sure the new inode item we write to the log has
  4381. * the same isize as the current one (if it exists).
  4382. * This is necessary to prevent data loss after log
  4383. * replay, and also to prevent doing a wrong expanding
  4384. * truncate - for e.g. create file, write 4K into offset
  4385. * 0, fsync, write 4K into offset 4096, add hard link,
  4386. * fsync some other file (to sync log), power fail - if
  4387. * we use the inode's current i_size, after log replay
  4388. * we get a 8Kb file, with the last 4Kb extent as a hole
  4389. * (zeroes), as if an expanding truncate happened,
  4390. * instead of getting a file of 4Kb only.
  4391. */
  4392. err = logged_inode_size(log, inode, path, &logged_isize);
  4393. if (err)
  4394. goto out_unlock;
  4395. }
  4396. if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4397. &inode->runtime_flags)) {
  4398. if (inode_only == LOG_INODE_EXISTS) {
  4399. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4400. ret = drop_objectid_items(trans, log, path, ino,
  4401. max_key.type);
  4402. } else {
  4403. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4404. &inode->runtime_flags);
  4405. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4406. &inode->runtime_flags);
  4407. while(1) {
  4408. ret = btrfs_truncate_inode_items(trans,
  4409. log, &inode->vfs_inode, 0, 0);
  4410. if (ret != -EAGAIN)
  4411. break;
  4412. }
  4413. }
  4414. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4415. &inode->runtime_flags) ||
  4416. inode_only == LOG_INODE_EXISTS) {
  4417. if (inode_only == LOG_INODE_ALL)
  4418. fast_search = true;
  4419. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4420. ret = drop_objectid_items(trans, log, path, ino,
  4421. max_key.type);
  4422. } else {
  4423. if (inode_only == LOG_INODE_ALL)
  4424. fast_search = true;
  4425. goto log_extents;
  4426. }
  4427. }
  4428. if (ret) {
  4429. err = ret;
  4430. goto out_unlock;
  4431. }
  4432. while (1) {
  4433. ins_nr = 0;
  4434. ret = btrfs_search_forward(root, &min_key,
  4435. path, trans->transid);
  4436. if (ret < 0) {
  4437. err = ret;
  4438. goto out_unlock;
  4439. }
  4440. if (ret != 0)
  4441. break;
  4442. again:
  4443. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  4444. if (min_key.objectid != ino)
  4445. break;
  4446. if (min_key.type > max_key.type)
  4447. break;
  4448. if (min_key.type == BTRFS_INODE_ITEM_KEY)
  4449. need_log_inode_item = false;
  4450. if ((min_key.type == BTRFS_INODE_REF_KEY ||
  4451. min_key.type == BTRFS_INODE_EXTREF_KEY) &&
  4452. inode->generation == trans->transid) {
  4453. u64 other_ino = 0;
  4454. ret = btrfs_check_ref_name_override(path->nodes[0],
  4455. path->slots[0], &min_key, inode,
  4456. &other_ino);
  4457. if (ret < 0) {
  4458. err = ret;
  4459. goto out_unlock;
  4460. } else if (ret > 0 && ctx &&
  4461. other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
  4462. struct btrfs_key inode_key;
  4463. struct inode *other_inode;
  4464. if (ins_nr > 0) {
  4465. ins_nr++;
  4466. } else {
  4467. ins_nr = 1;
  4468. ins_start_slot = path->slots[0];
  4469. }
  4470. ret = copy_items(trans, inode, dst_path, path,
  4471. &last_extent, ins_start_slot,
  4472. ins_nr, inode_only,
  4473. logged_isize);
  4474. if (ret < 0) {
  4475. err = ret;
  4476. goto out_unlock;
  4477. }
  4478. ins_nr = 0;
  4479. btrfs_release_path(path);
  4480. inode_key.objectid = other_ino;
  4481. inode_key.type = BTRFS_INODE_ITEM_KEY;
  4482. inode_key.offset = 0;
  4483. other_inode = btrfs_iget(fs_info->sb,
  4484. &inode_key, root,
  4485. NULL);
  4486. /*
  4487. * If the other inode that had a conflicting dir
  4488. * entry was deleted in the current transaction,
  4489. * we don't need to do more work nor fallback to
  4490. * a transaction commit.
  4491. */
  4492. if (other_inode == ERR_PTR(-ENOENT)) {
  4493. goto next_key;
  4494. } else if (IS_ERR(other_inode)) {
  4495. err = PTR_ERR(other_inode);
  4496. goto out_unlock;
  4497. }
  4498. /*
  4499. * We are safe logging the other inode without
  4500. * acquiring its i_mutex as long as we log with
  4501. * the LOG_INODE_EXISTS mode. We're safe against
  4502. * concurrent renames of the other inode as well
  4503. * because during a rename we pin the log and
  4504. * update the log with the new name before we
  4505. * unpin it.
  4506. */
  4507. err = btrfs_log_inode(trans, root,
  4508. BTRFS_I(other_inode),
  4509. LOG_OTHER_INODE, 0, LLONG_MAX,
  4510. ctx);
  4511. iput(other_inode);
  4512. if (err)
  4513. goto out_unlock;
  4514. else
  4515. goto next_key;
  4516. }
  4517. }
  4518. /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
  4519. if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
  4520. if (ins_nr == 0)
  4521. goto next_slot;
  4522. ret = copy_items(trans, inode, dst_path, path,
  4523. &last_extent, ins_start_slot,
  4524. ins_nr, inode_only, logged_isize);
  4525. if (ret < 0) {
  4526. err = ret;
  4527. goto out_unlock;
  4528. }
  4529. ins_nr = 0;
  4530. if (ret) {
  4531. btrfs_release_path(path);
  4532. continue;
  4533. }
  4534. goto next_slot;
  4535. }
  4536. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  4537. ins_nr++;
  4538. goto next_slot;
  4539. } else if (!ins_nr) {
  4540. ins_start_slot = path->slots[0];
  4541. ins_nr = 1;
  4542. goto next_slot;
  4543. }
  4544. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4545. ins_start_slot, ins_nr, inode_only,
  4546. logged_isize);
  4547. if (ret < 0) {
  4548. err = ret;
  4549. goto out_unlock;
  4550. }
  4551. if (ret) {
  4552. ins_nr = 0;
  4553. btrfs_release_path(path);
  4554. continue;
  4555. }
  4556. ins_nr = 1;
  4557. ins_start_slot = path->slots[0];
  4558. next_slot:
  4559. nritems = btrfs_header_nritems(path->nodes[0]);
  4560. path->slots[0]++;
  4561. if (path->slots[0] < nritems) {
  4562. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  4563. path->slots[0]);
  4564. goto again;
  4565. }
  4566. if (ins_nr) {
  4567. ret = copy_items(trans, inode, dst_path, path,
  4568. &last_extent, ins_start_slot,
  4569. ins_nr, inode_only, logged_isize);
  4570. if (ret < 0) {
  4571. err = ret;
  4572. goto out_unlock;
  4573. }
  4574. ret = 0;
  4575. ins_nr = 0;
  4576. }
  4577. btrfs_release_path(path);
  4578. next_key:
  4579. if (min_key.offset < (u64)-1) {
  4580. min_key.offset++;
  4581. } else if (min_key.type < max_key.type) {
  4582. min_key.type++;
  4583. min_key.offset = 0;
  4584. } else {
  4585. break;
  4586. }
  4587. }
  4588. if (ins_nr) {
  4589. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4590. ins_start_slot, ins_nr, inode_only,
  4591. logged_isize);
  4592. if (ret < 0) {
  4593. err = ret;
  4594. goto out_unlock;
  4595. }
  4596. ret = 0;
  4597. ins_nr = 0;
  4598. }
  4599. btrfs_release_path(path);
  4600. btrfs_release_path(dst_path);
  4601. err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
  4602. if (err)
  4603. goto out_unlock;
  4604. xattrs_logged = true;
  4605. if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
  4606. btrfs_release_path(path);
  4607. btrfs_release_path(dst_path);
  4608. err = btrfs_log_trailing_hole(trans, root, inode, path);
  4609. if (err)
  4610. goto out_unlock;
  4611. }
  4612. log_extents:
  4613. btrfs_release_path(path);
  4614. btrfs_release_path(dst_path);
  4615. if (need_log_inode_item) {
  4616. err = log_inode_item(trans, log, dst_path, inode);
  4617. if (!err && !xattrs_logged) {
  4618. err = btrfs_log_all_xattrs(trans, root, inode, path,
  4619. dst_path);
  4620. btrfs_release_path(path);
  4621. }
  4622. if (err)
  4623. goto out_unlock;
  4624. }
  4625. if (fast_search) {
  4626. ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
  4627. ctx, start, end);
  4628. if (ret) {
  4629. err = ret;
  4630. goto out_unlock;
  4631. }
  4632. } else if (inode_only == LOG_INODE_ALL) {
  4633. struct extent_map *em, *n;
  4634. write_lock(&em_tree->lock);
  4635. /*
  4636. * We can't just remove every em if we're called for a ranged
  4637. * fsync - that is, one that doesn't cover the whole possible
  4638. * file range (0 to LLONG_MAX). This is because we can have
  4639. * em's that fall outside the range we're logging and therefore
  4640. * their ordered operations haven't completed yet
  4641. * (btrfs_finish_ordered_io() not invoked yet). This means we
  4642. * didn't get their respective file extent item in the fs/subvol
  4643. * tree yet, and need to let the next fast fsync (one which
  4644. * consults the list of modified extent maps) find the em so
  4645. * that it logs a matching file extent item and waits for the
  4646. * respective ordered operation to complete (if it's still
  4647. * running).
  4648. *
  4649. * Removing every em outside the range we're logging would make
  4650. * the next fast fsync not log their matching file extent items,
  4651. * therefore making us lose data after a log replay.
  4652. */
  4653. list_for_each_entry_safe(em, n, &em_tree->modified_extents,
  4654. list) {
  4655. const u64 mod_end = em->mod_start + em->mod_len - 1;
  4656. if (em->mod_start >= start && mod_end <= end)
  4657. list_del_init(&em->list);
  4658. }
  4659. write_unlock(&em_tree->lock);
  4660. }
  4661. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
  4662. ret = log_directory_changes(trans, root, inode, path, dst_path,
  4663. ctx);
  4664. if (ret) {
  4665. err = ret;
  4666. goto out_unlock;
  4667. }
  4668. }
  4669. spin_lock(&inode->lock);
  4670. inode->logged_trans = trans->transid;
  4671. inode->last_log_commit = inode->last_sub_trans;
  4672. spin_unlock(&inode->lock);
  4673. out_unlock:
  4674. mutex_unlock(&inode->log_mutex);
  4675. btrfs_free_path(path);
  4676. btrfs_free_path(dst_path);
  4677. return err;
  4678. }
  4679. /*
  4680. * Check if we must fallback to a transaction commit when logging an inode.
  4681. * This must be called after logging the inode and is used only in the context
  4682. * when fsyncing an inode requires the need to log some other inode - in which
  4683. * case we can't lock the i_mutex of each other inode we need to log as that
  4684. * can lead to deadlocks with concurrent fsync against other inodes (as we can
  4685. * log inodes up or down in the hierarchy) or rename operations for example. So
  4686. * we take the log_mutex of the inode after we have logged it and then check for
  4687. * its last_unlink_trans value - this is safe because any task setting
  4688. * last_unlink_trans must take the log_mutex and it must do this before it does
  4689. * the actual unlink operation, so if we do this check before a concurrent task
  4690. * sets last_unlink_trans it means we've logged a consistent version/state of
  4691. * all the inode items, otherwise we are not sure and must do a transaction
  4692. * commit (the concurrent task might have only updated last_unlink_trans before
  4693. * we logged the inode or it might have also done the unlink).
  4694. */
  4695. static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
  4696. struct btrfs_inode *inode)
  4697. {
  4698. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  4699. bool ret = false;
  4700. mutex_lock(&inode->log_mutex);
  4701. if (inode->last_unlink_trans > fs_info->last_trans_committed) {
  4702. /*
  4703. * Make sure any commits to the log are forced to be full
  4704. * commits.
  4705. */
  4706. btrfs_set_log_full_commit(fs_info, trans);
  4707. ret = true;
  4708. }
  4709. mutex_unlock(&inode->log_mutex);
  4710. return ret;
  4711. }
  4712. /*
  4713. * follow the dentry parent pointers up the chain and see if any
  4714. * of the directories in it require a full commit before they can
  4715. * be logged. Returns zero if nothing special needs to be done or 1 if
  4716. * a full commit is required.
  4717. */
  4718. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  4719. struct btrfs_inode *inode,
  4720. struct dentry *parent,
  4721. struct super_block *sb,
  4722. u64 last_committed)
  4723. {
  4724. int ret = 0;
  4725. struct dentry *old_parent = NULL;
  4726. struct btrfs_inode *orig_inode = inode;
  4727. /*
  4728. * for regular files, if its inode is already on disk, we don't
  4729. * have to worry about the parents at all. This is because
  4730. * we can use the last_unlink_trans field to record renames
  4731. * and other fun in this file.
  4732. */
  4733. if (S_ISREG(inode->vfs_inode.i_mode) &&
  4734. inode->generation <= last_committed &&
  4735. inode->last_unlink_trans <= last_committed)
  4736. goto out;
  4737. if (!S_ISDIR(inode->vfs_inode.i_mode)) {
  4738. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4739. goto out;
  4740. inode = BTRFS_I(d_inode(parent));
  4741. }
  4742. while (1) {
  4743. /*
  4744. * If we are logging a directory then we start with our inode,
  4745. * not our parent's inode, so we need to skip setting the
  4746. * logged_trans so that further down in the log code we don't
  4747. * think this inode has already been logged.
  4748. */
  4749. if (inode != orig_inode)
  4750. inode->logged_trans = trans->transid;
  4751. smp_mb();
  4752. if (btrfs_must_commit_transaction(trans, inode)) {
  4753. ret = 1;
  4754. break;
  4755. }
  4756. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4757. break;
  4758. if (IS_ROOT(parent)) {
  4759. inode = BTRFS_I(d_inode(parent));
  4760. if (btrfs_must_commit_transaction(trans, inode))
  4761. ret = 1;
  4762. break;
  4763. }
  4764. parent = dget_parent(parent);
  4765. dput(old_parent);
  4766. old_parent = parent;
  4767. inode = BTRFS_I(d_inode(parent));
  4768. }
  4769. dput(old_parent);
  4770. out:
  4771. return ret;
  4772. }
  4773. struct btrfs_dir_list {
  4774. u64 ino;
  4775. struct list_head list;
  4776. };
  4777. /*
  4778. * Log the inodes of the new dentries of a directory. See log_dir_items() for
  4779. * details about the why it is needed.
  4780. * This is a recursive operation - if an existing dentry corresponds to a
  4781. * directory, that directory's new entries are logged too (same behaviour as
  4782. * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
  4783. * the dentries point to we do not lock their i_mutex, otherwise lockdep
  4784. * complains about the following circular lock dependency / possible deadlock:
  4785. *
  4786. * CPU0 CPU1
  4787. * ---- ----
  4788. * lock(&type->i_mutex_dir_key#3/2);
  4789. * lock(sb_internal#2);
  4790. * lock(&type->i_mutex_dir_key#3/2);
  4791. * lock(&sb->s_type->i_mutex_key#14);
  4792. *
  4793. * Where sb_internal is the lock (a counter that works as a lock) acquired by
  4794. * sb_start_intwrite() in btrfs_start_transaction().
  4795. * Not locking i_mutex of the inodes is still safe because:
  4796. *
  4797. * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
  4798. * that while logging the inode new references (names) are added or removed
  4799. * from the inode, leaving the logged inode item with a link count that does
  4800. * not match the number of logged inode reference items. This is fine because
  4801. * at log replay time we compute the real number of links and correct the
  4802. * link count in the inode item (see replay_one_buffer() and
  4803. * link_to_fixup_dir());
  4804. *
  4805. * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
  4806. * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
  4807. * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
  4808. * has a size that doesn't match the sum of the lengths of all the logged
  4809. * names. This does not result in a problem because if a dir_item key is
  4810. * logged but its matching dir_index key is not logged, at log replay time we
  4811. * don't use it to replay the respective name (see replay_one_name()). On the
  4812. * other hand if only the dir_index key ends up being logged, the respective
  4813. * name is added to the fs/subvol tree with both the dir_item and dir_index
  4814. * keys created (see replay_one_name()).
  4815. * The directory's inode item with a wrong i_size is not a problem as well,
  4816. * since we don't use it at log replay time to set the i_size in the inode
  4817. * item of the fs/subvol tree (see overwrite_item()).
  4818. */
  4819. static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
  4820. struct btrfs_root *root,
  4821. struct btrfs_inode *start_inode,
  4822. struct btrfs_log_ctx *ctx)
  4823. {
  4824. struct btrfs_fs_info *fs_info = root->fs_info;
  4825. struct btrfs_root *log = root->log_root;
  4826. struct btrfs_path *path;
  4827. LIST_HEAD(dir_list);
  4828. struct btrfs_dir_list *dir_elem;
  4829. int ret = 0;
  4830. path = btrfs_alloc_path();
  4831. if (!path)
  4832. return -ENOMEM;
  4833. dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
  4834. if (!dir_elem) {
  4835. btrfs_free_path(path);
  4836. return -ENOMEM;
  4837. }
  4838. dir_elem->ino = btrfs_ino(start_inode);
  4839. list_add_tail(&dir_elem->list, &dir_list);
  4840. while (!list_empty(&dir_list)) {
  4841. struct extent_buffer *leaf;
  4842. struct btrfs_key min_key;
  4843. int nritems;
  4844. int i;
  4845. dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
  4846. list);
  4847. if (ret)
  4848. goto next_dir_inode;
  4849. min_key.objectid = dir_elem->ino;
  4850. min_key.type = BTRFS_DIR_ITEM_KEY;
  4851. min_key.offset = 0;
  4852. again:
  4853. btrfs_release_path(path);
  4854. ret = btrfs_search_forward(log, &min_key, path, trans->transid);
  4855. if (ret < 0) {
  4856. goto next_dir_inode;
  4857. } else if (ret > 0) {
  4858. ret = 0;
  4859. goto next_dir_inode;
  4860. }
  4861. process_leaf:
  4862. leaf = path->nodes[0];
  4863. nritems = btrfs_header_nritems(leaf);
  4864. for (i = path->slots[0]; i < nritems; i++) {
  4865. struct btrfs_dir_item *di;
  4866. struct btrfs_key di_key;
  4867. struct inode *di_inode;
  4868. struct btrfs_dir_list *new_dir_elem;
  4869. int log_mode = LOG_INODE_EXISTS;
  4870. int type;
  4871. btrfs_item_key_to_cpu(leaf, &min_key, i);
  4872. if (min_key.objectid != dir_elem->ino ||
  4873. min_key.type != BTRFS_DIR_ITEM_KEY)
  4874. goto next_dir_inode;
  4875. di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
  4876. type = btrfs_dir_type(leaf, di);
  4877. if (btrfs_dir_transid(leaf, di) < trans->transid &&
  4878. type != BTRFS_FT_DIR)
  4879. continue;
  4880. btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
  4881. if (di_key.type == BTRFS_ROOT_ITEM_KEY)
  4882. continue;
  4883. btrfs_release_path(path);
  4884. di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
  4885. if (IS_ERR(di_inode)) {
  4886. ret = PTR_ERR(di_inode);
  4887. goto next_dir_inode;
  4888. }
  4889. if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
  4890. iput(di_inode);
  4891. break;
  4892. }
  4893. ctx->log_new_dentries = false;
  4894. if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
  4895. log_mode = LOG_INODE_ALL;
  4896. ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
  4897. log_mode, 0, LLONG_MAX, ctx);
  4898. if (!ret &&
  4899. btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
  4900. ret = 1;
  4901. iput(di_inode);
  4902. if (ret)
  4903. goto next_dir_inode;
  4904. if (ctx->log_new_dentries) {
  4905. new_dir_elem = kmalloc(sizeof(*new_dir_elem),
  4906. GFP_NOFS);
  4907. if (!new_dir_elem) {
  4908. ret = -ENOMEM;
  4909. goto next_dir_inode;
  4910. }
  4911. new_dir_elem->ino = di_key.objectid;
  4912. list_add_tail(&new_dir_elem->list, &dir_list);
  4913. }
  4914. break;
  4915. }
  4916. if (i == nritems) {
  4917. ret = btrfs_next_leaf(log, path);
  4918. if (ret < 0) {
  4919. goto next_dir_inode;
  4920. } else if (ret > 0) {
  4921. ret = 0;
  4922. goto next_dir_inode;
  4923. }
  4924. goto process_leaf;
  4925. }
  4926. if (min_key.offset < (u64)-1) {
  4927. min_key.offset++;
  4928. goto again;
  4929. }
  4930. next_dir_inode:
  4931. list_del(&dir_elem->list);
  4932. kfree(dir_elem);
  4933. }
  4934. btrfs_free_path(path);
  4935. return ret;
  4936. }
  4937. static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
  4938. struct btrfs_inode *inode,
  4939. struct btrfs_log_ctx *ctx)
  4940. {
  4941. struct btrfs_fs_info *fs_info = trans->fs_info;
  4942. int ret;
  4943. struct btrfs_path *path;
  4944. struct btrfs_key key;
  4945. struct btrfs_root *root = inode->root;
  4946. const u64 ino = btrfs_ino(inode);
  4947. path = btrfs_alloc_path();
  4948. if (!path)
  4949. return -ENOMEM;
  4950. path->skip_locking = 1;
  4951. path->search_commit_root = 1;
  4952. key.objectid = ino;
  4953. key.type = BTRFS_INODE_REF_KEY;
  4954. key.offset = 0;
  4955. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4956. if (ret < 0)
  4957. goto out;
  4958. while (true) {
  4959. struct extent_buffer *leaf = path->nodes[0];
  4960. int slot = path->slots[0];
  4961. u32 cur_offset = 0;
  4962. u32 item_size;
  4963. unsigned long ptr;
  4964. if (slot >= btrfs_header_nritems(leaf)) {
  4965. ret = btrfs_next_leaf(root, path);
  4966. if (ret < 0)
  4967. goto out;
  4968. else if (ret > 0)
  4969. break;
  4970. continue;
  4971. }
  4972. btrfs_item_key_to_cpu(leaf, &key, slot);
  4973. /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
  4974. if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
  4975. break;
  4976. item_size = btrfs_item_size_nr(leaf, slot);
  4977. ptr = btrfs_item_ptr_offset(leaf, slot);
  4978. while (cur_offset < item_size) {
  4979. struct btrfs_key inode_key;
  4980. struct inode *dir_inode;
  4981. inode_key.type = BTRFS_INODE_ITEM_KEY;
  4982. inode_key.offset = 0;
  4983. if (key.type == BTRFS_INODE_EXTREF_KEY) {
  4984. struct btrfs_inode_extref *extref;
  4985. extref = (struct btrfs_inode_extref *)
  4986. (ptr + cur_offset);
  4987. inode_key.objectid = btrfs_inode_extref_parent(
  4988. leaf, extref);
  4989. cur_offset += sizeof(*extref);
  4990. cur_offset += btrfs_inode_extref_name_len(leaf,
  4991. extref);
  4992. } else {
  4993. inode_key.objectid = key.offset;
  4994. cur_offset = item_size;
  4995. }
  4996. dir_inode = btrfs_iget(fs_info->sb, &inode_key,
  4997. root, NULL);
  4998. /*
  4999. * If the parent inode was deleted, return an error to
  5000. * fallback to a transaction commit. This is to prevent
  5001. * getting an inode that was moved from one parent A to
  5002. * a parent B, got its former parent A deleted and then
  5003. * it got fsync'ed, from existing at both parents after
  5004. * a log replay (and the old parent still existing).
  5005. * Example:
  5006. *
  5007. * mkdir /mnt/A
  5008. * mkdir /mnt/B
  5009. * touch /mnt/B/bar
  5010. * sync
  5011. * mv /mnt/B/bar /mnt/A/bar
  5012. * mv -T /mnt/A /mnt/B
  5013. * fsync /mnt/B/bar
  5014. * <power fail>
  5015. *
  5016. * If we ignore the old parent B which got deleted,
  5017. * after a log replay we would have file bar linked
  5018. * at both parents and the old parent B would still
  5019. * exist.
  5020. */
  5021. if (IS_ERR(dir_inode)) {
  5022. ret = PTR_ERR(dir_inode);
  5023. goto out;
  5024. }
  5025. if (ctx)
  5026. ctx->log_new_dentries = false;
  5027. ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
  5028. LOG_INODE_ALL, 0, LLONG_MAX, ctx);
  5029. if (!ret &&
  5030. btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
  5031. ret = 1;
  5032. if (!ret && ctx && ctx->log_new_dentries)
  5033. ret = log_new_dir_dentries(trans, root,
  5034. BTRFS_I(dir_inode), ctx);
  5035. iput(dir_inode);
  5036. if (ret)
  5037. goto out;
  5038. }
  5039. path->slots[0]++;
  5040. }
  5041. ret = 0;
  5042. out:
  5043. btrfs_free_path(path);
  5044. return ret;
  5045. }
  5046. /*
  5047. * helper function around btrfs_log_inode to make sure newly created
  5048. * parent directories also end up in the log. A minimal inode and backref
  5049. * only logging is done of any parent directories that are older than
  5050. * the last committed transaction
  5051. */
  5052. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  5053. struct btrfs_inode *inode,
  5054. struct dentry *parent,
  5055. const loff_t start,
  5056. const loff_t end,
  5057. int inode_only,
  5058. struct btrfs_log_ctx *ctx)
  5059. {
  5060. struct btrfs_root *root = inode->root;
  5061. struct btrfs_fs_info *fs_info = root->fs_info;
  5062. struct super_block *sb;
  5063. struct dentry *old_parent = NULL;
  5064. int ret = 0;
  5065. u64 last_committed = fs_info->last_trans_committed;
  5066. bool log_dentries = false;
  5067. struct btrfs_inode *orig_inode = inode;
  5068. sb = inode->vfs_inode.i_sb;
  5069. if (btrfs_test_opt(fs_info, NOTREELOG)) {
  5070. ret = 1;
  5071. goto end_no_trans;
  5072. }
  5073. /*
  5074. * The prev transaction commit doesn't complete, we need do
  5075. * full commit by ourselves.
  5076. */
  5077. if (fs_info->last_trans_log_full_commit >
  5078. fs_info->last_trans_committed) {
  5079. ret = 1;
  5080. goto end_no_trans;
  5081. }
  5082. if (btrfs_root_refs(&root->root_item) == 0) {
  5083. ret = 1;
  5084. goto end_no_trans;
  5085. }
  5086. ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
  5087. last_committed);
  5088. if (ret)
  5089. goto end_no_trans;
  5090. /*
  5091. * Skip already logged inodes or inodes corresponding to tmpfiles
  5092. * (since logging them is pointless, a link count of 0 means they
  5093. * will never be accessible).
  5094. */
  5095. if (btrfs_inode_in_log(inode, trans->transid) ||
  5096. inode->vfs_inode.i_nlink == 0) {
  5097. ret = BTRFS_NO_LOG_SYNC;
  5098. goto end_no_trans;
  5099. }
  5100. ret = start_log_trans(trans, root, ctx);
  5101. if (ret)
  5102. goto end_no_trans;
  5103. ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
  5104. if (ret)
  5105. goto end_trans;
  5106. /*
  5107. * for regular files, if its inode is already on disk, we don't
  5108. * have to worry about the parents at all. This is because
  5109. * we can use the last_unlink_trans field to record renames
  5110. * and other fun in this file.
  5111. */
  5112. if (S_ISREG(inode->vfs_inode.i_mode) &&
  5113. inode->generation <= last_committed &&
  5114. inode->last_unlink_trans <= last_committed) {
  5115. ret = 0;
  5116. goto end_trans;
  5117. }
  5118. if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
  5119. log_dentries = true;
  5120. /*
  5121. * On unlink we must make sure all our current and old parent directory
  5122. * inodes are fully logged. This is to prevent leaving dangling
  5123. * directory index entries in directories that were our parents but are
  5124. * not anymore. Not doing this results in old parent directory being
  5125. * impossible to delete after log replay (rmdir will always fail with
  5126. * error -ENOTEMPTY).
  5127. *
  5128. * Example 1:
  5129. *
  5130. * mkdir testdir
  5131. * touch testdir/foo
  5132. * ln testdir/foo testdir/bar
  5133. * sync
  5134. * unlink testdir/bar
  5135. * xfs_io -c fsync testdir/foo
  5136. * <power failure>
  5137. * mount fs, triggers log replay
  5138. *
  5139. * If we don't log the parent directory (testdir), after log replay the
  5140. * directory still has an entry pointing to the file inode using the bar
  5141. * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
  5142. * the file inode has a link count of 1.
  5143. *
  5144. * Example 2:
  5145. *
  5146. * mkdir testdir
  5147. * touch foo
  5148. * ln foo testdir/foo2
  5149. * ln foo testdir/foo3
  5150. * sync
  5151. * unlink testdir/foo3
  5152. * xfs_io -c fsync foo
  5153. * <power failure>
  5154. * mount fs, triggers log replay
  5155. *
  5156. * Similar as the first example, after log replay the parent directory
  5157. * testdir still has an entry pointing to the inode file with name foo3
  5158. * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
  5159. * and has a link count of 2.
  5160. */
  5161. if (inode->last_unlink_trans > last_committed) {
  5162. ret = btrfs_log_all_parents(trans, orig_inode, ctx);
  5163. if (ret)
  5164. goto end_trans;
  5165. }
  5166. while (1) {
  5167. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  5168. break;
  5169. inode = BTRFS_I(d_inode(parent));
  5170. if (root != inode->root)
  5171. break;
  5172. if (inode->generation > last_committed) {
  5173. ret = btrfs_log_inode(trans, root, inode,
  5174. LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
  5175. if (ret)
  5176. goto end_trans;
  5177. }
  5178. if (IS_ROOT(parent))
  5179. break;
  5180. parent = dget_parent(parent);
  5181. dput(old_parent);
  5182. old_parent = parent;
  5183. }
  5184. if (log_dentries)
  5185. ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
  5186. else
  5187. ret = 0;
  5188. end_trans:
  5189. dput(old_parent);
  5190. if (ret < 0) {
  5191. btrfs_set_log_full_commit(fs_info, trans);
  5192. ret = 1;
  5193. }
  5194. if (ret)
  5195. btrfs_remove_log_ctx(root, ctx);
  5196. btrfs_end_log_trans(root);
  5197. end_no_trans:
  5198. return ret;
  5199. }
  5200. /*
  5201. * it is not safe to log dentry if the chunk root has added new
  5202. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  5203. * If this returns 1, you must commit the transaction to safely get your
  5204. * data on disk.
  5205. */
  5206. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  5207. struct dentry *dentry,
  5208. const loff_t start,
  5209. const loff_t end,
  5210. struct btrfs_log_ctx *ctx)
  5211. {
  5212. struct dentry *parent = dget_parent(dentry);
  5213. int ret;
  5214. ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
  5215. start, end, LOG_INODE_ALL, ctx);
  5216. dput(parent);
  5217. return ret;
  5218. }
  5219. /*
  5220. * should be called during mount to recover any replay any log trees
  5221. * from the FS
  5222. */
  5223. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  5224. {
  5225. int ret;
  5226. struct btrfs_path *path;
  5227. struct btrfs_trans_handle *trans;
  5228. struct btrfs_key key;
  5229. struct btrfs_key found_key;
  5230. struct btrfs_key tmp_key;
  5231. struct btrfs_root *log;
  5232. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  5233. struct walk_control wc = {
  5234. .process_func = process_one_buffer,
  5235. .stage = 0,
  5236. };
  5237. path = btrfs_alloc_path();
  5238. if (!path)
  5239. return -ENOMEM;
  5240. set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5241. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  5242. if (IS_ERR(trans)) {
  5243. ret = PTR_ERR(trans);
  5244. goto error;
  5245. }
  5246. wc.trans = trans;
  5247. wc.pin = 1;
  5248. ret = walk_log_tree(trans, log_root_tree, &wc);
  5249. if (ret) {
  5250. btrfs_handle_fs_error(fs_info, ret,
  5251. "Failed to pin buffers while recovering log root tree.");
  5252. goto error;
  5253. }
  5254. again:
  5255. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  5256. key.offset = (u64)-1;
  5257. key.type = BTRFS_ROOT_ITEM_KEY;
  5258. while (1) {
  5259. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  5260. if (ret < 0) {
  5261. btrfs_handle_fs_error(fs_info, ret,
  5262. "Couldn't find tree log root.");
  5263. goto error;
  5264. }
  5265. if (ret > 0) {
  5266. if (path->slots[0] == 0)
  5267. break;
  5268. path->slots[0]--;
  5269. }
  5270. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  5271. path->slots[0]);
  5272. btrfs_release_path(path);
  5273. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  5274. break;
  5275. log = btrfs_read_fs_root(log_root_tree, &found_key);
  5276. if (IS_ERR(log)) {
  5277. ret = PTR_ERR(log);
  5278. btrfs_handle_fs_error(fs_info, ret,
  5279. "Couldn't read tree log root.");
  5280. goto error;
  5281. }
  5282. tmp_key.objectid = found_key.offset;
  5283. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  5284. tmp_key.offset = (u64)-1;
  5285. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  5286. if (IS_ERR(wc.replay_dest)) {
  5287. ret = PTR_ERR(wc.replay_dest);
  5288. free_extent_buffer(log->node);
  5289. free_extent_buffer(log->commit_root);
  5290. kfree(log);
  5291. btrfs_handle_fs_error(fs_info, ret,
  5292. "Couldn't read target root for tree log recovery.");
  5293. goto error;
  5294. }
  5295. wc.replay_dest->log_root = log;
  5296. btrfs_record_root_in_trans(trans, wc.replay_dest);
  5297. ret = walk_log_tree(trans, log, &wc);
  5298. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5299. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  5300. path);
  5301. }
  5302. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5303. struct btrfs_root *root = wc.replay_dest;
  5304. btrfs_release_path(path);
  5305. /*
  5306. * We have just replayed everything, and the highest
  5307. * objectid of fs roots probably has changed in case
  5308. * some inode_item's got replayed.
  5309. *
  5310. * root->objectid_mutex is not acquired as log replay
  5311. * could only happen during mount.
  5312. */
  5313. ret = btrfs_find_highest_objectid(root,
  5314. &root->highest_objectid);
  5315. }
  5316. key.offset = found_key.offset - 1;
  5317. wc.replay_dest->log_root = NULL;
  5318. free_extent_buffer(log->node);
  5319. free_extent_buffer(log->commit_root);
  5320. kfree(log);
  5321. if (ret)
  5322. goto error;
  5323. if (found_key.offset == 0)
  5324. break;
  5325. }
  5326. btrfs_release_path(path);
  5327. /* step one is to pin it all, step two is to replay just inodes */
  5328. if (wc.pin) {
  5329. wc.pin = 0;
  5330. wc.process_func = replay_one_buffer;
  5331. wc.stage = LOG_WALK_REPLAY_INODES;
  5332. goto again;
  5333. }
  5334. /* step three is to replay everything */
  5335. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  5336. wc.stage++;
  5337. goto again;
  5338. }
  5339. btrfs_free_path(path);
  5340. /* step 4: commit the transaction, which also unpins the blocks */
  5341. ret = btrfs_commit_transaction(trans);
  5342. if (ret)
  5343. return ret;
  5344. free_extent_buffer(log_root_tree->node);
  5345. log_root_tree->log_root = NULL;
  5346. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5347. kfree(log_root_tree);
  5348. return 0;
  5349. error:
  5350. if (wc.trans)
  5351. btrfs_end_transaction(wc.trans);
  5352. btrfs_free_path(path);
  5353. return ret;
  5354. }
  5355. /*
  5356. * there are some corner cases where we want to force a full
  5357. * commit instead of allowing a directory to be logged.
  5358. *
  5359. * They revolve around files there were unlinked from the directory, and
  5360. * this function updates the parent directory so that a full commit is
  5361. * properly done if it is fsync'd later after the unlinks are done.
  5362. *
  5363. * Must be called before the unlink operations (updates to the subvolume tree,
  5364. * inodes, etc) are done.
  5365. */
  5366. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  5367. struct btrfs_inode *dir, struct btrfs_inode *inode,
  5368. int for_rename)
  5369. {
  5370. /*
  5371. * when we're logging a file, if it hasn't been renamed
  5372. * or unlinked, and its inode is fully committed on disk,
  5373. * we don't have to worry about walking up the directory chain
  5374. * to log its parents.
  5375. *
  5376. * So, we use the last_unlink_trans field to put this transid
  5377. * into the file. When the file is logged we check it and
  5378. * don't log the parents if the file is fully on disk.
  5379. */
  5380. mutex_lock(&inode->log_mutex);
  5381. inode->last_unlink_trans = trans->transid;
  5382. mutex_unlock(&inode->log_mutex);
  5383. /*
  5384. * if this directory was already logged any new
  5385. * names for this file/dir will get recorded
  5386. */
  5387. smp_mb();
  5388. if (dir->logged_trans == trans->transid)
  5389. return;
  5390. /*
  5391. * if the inode we're about to unlink was logged,
  5392. * the log will be properly updated for any new names
  5393. */
  5394. if (inode->logged_trans == trans->transid)
  5395. return;
  5396. /*
  5397. * when renaming files across directories, if the directory
  5398. * there we're unlinking from gets fsync'd later on, there's
  5399. * no way to find the destination directory later and fsync it
  5400. * properly. So, we have to be conservative and force commits
  5401. * so the new name gets discovered.
  5402. */
  5403. if (for_rename)
  5404. goto record;
  5405. /* we can safely do the unlink without any special recording */
  5406. return;
  5407. record:
  5408. mutex_lock(&dir->log_mutex);
  5409. dir->last_unlink_trans = trans->transid;
  5410. mutex_unlock(&dir->log_mutex);
  5411. }
  5412. /*
  5413. * Make sure that if someone attempts to fsync the parent directory of a deleted
  5414. * snapshot, it ends up triggering a transaction commit. This is to guarantee
  5415. * that after replaying the log tree of the parent directory's root we will not
  5416. * see the snapshot anymore and at log replay time we will not see any log tree
  5417. * corresponding to the deleted snapshot's root, which could lead to replaying
  5418. * it after replaying the log tree of the parent directory (which would replay
  5419. * the snapshot delete operation).
  5420. *
  5421. * Must be called before the actual snapshot destroy operation (updates to the
  5422. * parent root and tree of tree roots trees, etc) are done.
  5423. */
  5424. void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
  5425. struct btrfs_inode *dir)
  5426. {
  5427. mutex_lock(&dir->log_mutex);
  5428. dir->last_unlink_trans = trans->transid;
  5429. mutex_unlock(&dir->log_mutex);
  5430. }
  5431. /*
  5432. * Call this after adding a new name for a file and it will properly
  5433. * update the log to reflect the new name.
  5434. *
  5435. * @ctx can not be NULL when @sync_log is false, and should be NULL when it's
  5436. * true (because it's not used).
  5437. *
  5438. * Return value depends on whether @sync_log is true or false.
  5439. * When true: returns BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
  5440. * committed by the caller, and BTRFS_DONT_NEED_TRANS_COMMIT
  5441. * otherwise.
  5442. * When false: returns BTRFS_DONT_NEED_LOG_SYNC if the caller does not need to
  5443. * to sync the log, BTRFS_NEED_LOG_SYNC if it needs to sync the log,
  5444. * or BTRFS_NEED_TRANS_COMMIT if the transaction needs to be
  5445. * committed (without attempting to sync the log).
  5446. */
  5447. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  5448. struct btrfs_inode *inode, struct btrfs_inode *old_dir,
  5449. struct dentry *parent,
  5450. bool sync_log, struct btrfs_log_ctx *ctx)
  5451. {
  5452. struct btrfs_fs_info *fs_info = trans->fs_info;
  5453. int ret;
  5454. /*
  5455. * this will force the logging code to walk the dentry chain
  5456. * up for the file
  5457. */
  5458. if (!S_ISDIR(inode->vfs_inode.i_mode))
  5459. inode->last_unlink_trans = trans->transid;
  5460. /*
  5461. * if this inode hasn't been logged and directory we're renaming it
  5462. * from hasn't been logged, we don't need to log it
  5463. */
  5464. if (inode->logged_trans <= fs_info->last_trans_committed &&
  5465. (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
  5466. return sync_log ? BTRFS_DONT_NEED_TRANS_COMMIT :
  5467. BTRFS_DONT_NEED_LOG_SYNC;
  5468. if (sync_log) {
  5469. struct btrfs_log_ctx ctx2;
  5470. btrfs_init_log_ctx(&ctx2, &inode->vfs_inode);
  5471. ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
  5472. LOG_INODE_EXISTS, &ctx2);
  5473. if (ret == BTRFS_NO_LOG_SYNC)
  5474. return BTRFS_DONT_NEED_TRANS_COMMIT;
  5475. else if (ret)
  5476. return BTRFS_NEED_TRANS_COMMIT;
  5477. ret = btrfs_sync_log(trans, inode->root, &ctx2);
  5478. if (ret)
  5479. return BTRFS_NEED_TRANS_COMMIT;
  5480. return BTRFS_DONT_NEED_TRANS_COMMIT;
  5481. }
  5482. ASSERT(ctx);
  5483. ret = btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
  5484. LOG_INODE_EXISTS, ctx);
  5485. if (ret == BTRFS_NO_LOG_SYNC)
  5486. return BTRFS_DONT_NEED_LOG_SYNC;
  5487. else if (ret)
  5488. return BTRFS_NEED_TRANS_COMMIT;
  5489. return BTRFS_NEED_LOG_SYNC;
  5490. }