tree-log.c 163 KB

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