tree-log.c 161 KB

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