tree-log.c 160 KB

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