tree-log.c 160 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002
  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);
  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)
  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);
  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)
  2159. {
  2160. int nritems;
  2161. struct btrfs_path *path;
  2162. struct btrfs_root *root = wc->replay_dest;
  2163. struct btrfs_key key;
  2164. int level;
  2165. int i;
  2166. int ret;
  2167. ret = btrfs_read_buffer(eb, gen);
  2168. if (ret)
  2169. return ret;
  2170. level = btrfs_header_level(eb);
  2171. if (level != 0)
  2172. return 0;
  2173. path = btrfs_alloc_path();
  2174. if (!path)
  2175. return -ENOMEM;
  2176. nritems = btrfs_header_nritems(eb);
  2177. for (i = 0; i < nritems; i++) {
  2178. btrfs_item_key_to_cpu(eb, &key, i);
  2179. /* inode keys are done during the first stage */
  2180. if (key.type == BTRFS_INODE_ITEM_KEY &&
  2181. wc->stage == LOG_WALK_REPLAY_INODES) {
  2182. struct btrfs_inode_item *inode_item;
  2183. u32 mode;
  2184. inode_item = btrfs_item_ptr(eb, i,
  2185. struct btrfs_inode_item);
  2186. ret = replay_xattr_deletes(wc->trans, root, log,
  2187. path, key.objectid);
  2188. if (ret)
  2189. break;
  2190. mode = btrfs_inode_mode(eb, inode_item);
  2191. if (S_ISDIR(mode)) {
  2192. ret = replay_dir_deletes(wc->trans,
  2193. root, log, path, key.objectid, 0);
  2194. if (ret)
  2195. break;
  2196. }
  2197. ret = overwrite_item(wc->trans, root, path,
  2198. eb, i, &key);
  2199. if (ret)
  2200. break;
  2201. /* for regular files, make sure corresponding
  2202. * orphan item exist. extents past the new EOF
  2203. * will be truncated later by orphan cleanup.
  2204. */
  2205. if (S_ISREG(mode)) {
  2206. ret = insert_orphan_item(wc->trans, root,
  2207. key.objectid);
  2208. if (ret)
  2209. break;
  2210. }
  2211. ret = link_to_fixup_dir(wc->trans, root,
  2212. path, key.objectid);
  2213. if (ret)
  2214. break;
  2215. }
  2216. if (key.type == BTRFS_DIR_INDEX_KEY &&
  2217. wc->stage == LOG_WALK_REPLAY_DIR_INDEX) {
  2218. ret = replay_one_dir_item(wc->trans, root, path,
  2219. eb, i, &key);
  2220. if (ret)
  2221. break;
  2222. }
  2223. if (wc->stage < LOG_WALK_REPLAY_ALL)
  2224. continue;
  2225. /* these keys are simply copied */
  2226. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  2227. ret = overwrite_item(wc->trans, root, path,
  2228. eb, i, &key);
  2229. if (ret)
  2230. break;
  2231. } else if (key.type == BTRFS_INODE_REF_KEY ||
  2232. key.type == BTRFS_INODE_EXTREF_KEY) {
  2233. ret = add_inode_ref(wc->trans, root, log, path,
  2234. eb, i, &key);
  2235. if (ret && ret != -ENOENT)
  2236. break;
  2237. ret = 0;
  2238. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  2239. ret = replay_one_extent(wc->trans, root, path,
  2240. eb, i, &key);
  2241. if (ret)
  2242. break;
  2243. } else if (key.type == BTRFS_DIR_ITEM_KEY) {
  2244. ret = replay_one_dir_item(wc->trans, root, path,
  2245. eb, i, &key);
  2246. if (ret)
  2247. break;
  2248. }
  2249. }
  2250. btrfs_free_path(path);
  2251. return ret;
  2252. }
  2253. static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
  2254. struct btrfs_root *root,
  2255. struct btrfs_path *path, int *level,
  2256. struct walk_control *wc)
  2257. {
  2258. struct btrfs_fs_info *fs_info = root->fs_info;
  2259. u64 root_owner;
  2260. u64 bytenr;
  2261. u64 ptr_gen;
  2262. struct extent_buffer *next;
  2263. struct extent_buffer *cur;
  2264. struct extent_buffer *parent;
  2265. u32 blocksize;
  2266. int ret = 0;
  2267. WARN_ON(*level < 0);
  2268. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2269. while (*level > 0) {
  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. blocksize = fs_info->nodesize;
  2280. parent = path->nodes[*level];
  2281. root_owner = btrfs_header_owner(parent);
  2282. next = btrfs_find_create_tree_block(fs_info, bytenr);
  2283. if (IS_ERR(next))
  2284. return PTR_ERR(next);
  2285. if (*level == 1) {
  2286. ret = wc->process_func(root, next, wc, ptr_gen);
  2287. if (ret) {
  2288. free_extent_buffer(next);
  2289. return ret;
  2290. }
  2291. path->slots[*level]++;
  2292. if (wc->free) {
  2293. ret = btrfs_read_buffer(next, ptr_gen);
  2294. if (ret) {
  2295. free_extent_buffer(next);
  2296. return ret;
  2297. }
  2298. if (trans) {
  2299. btrfs_tree_lock(next);
  2300. btrfs_set_lock_blocking(next);
  2301. clean_tree_block(fs_info, next);
  2302. btrfs_wait_tree_block_writeback(next);
  2303. btrfs_tree_unlock(next);
  2304. } else {
  2305. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2306. clear_extent_buffer_dirty(next);
  2307. }
  2308. WARN_ON(root_owner !=
  2309. BTRFS_TREE_LOG_OBJECTID);
  2310. ret = btrfs_free_and_pin_reserved_extent(
  2311. fs_info, bytenr,
  2312. blocksize);
  2313. if (ret) {
  2314. free_extent_buffer(next);
  2315. return ret;
  2316. }
  2317. }
  2318. free_extent_buffer(next);
  2319. continue;
  2320. }
  2321. ret = btrfs_read_buffer(next, ptr_gen);
  2322. if (ret) {
  2323. free_extent_buffer(next);
  2324. return ret;
  2325. }
  2326. WARN_ON(*level <= 0);
  2327. if (path->nodes[*level-1])
  2328. free_extent_buffer(path->nodes[*level-1]);
  2329. path->nodes[*level-1] = next;
  2330. *level = btrfs_header_level(next);
  2331. path->slots[*level] = 0;
  2332. cond_resched();
  2333. }
  2334. WARN_ON(*level < 0);
  2335. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2336. path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
  2337. cond_resched();
  2338. return 0;
  2339. }
  2340. static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
  2341. struct btrfs_root *root,
  2342. struct btrfs_path *path, int *level,
  2343. struct walk_control *wc)
  2344. {
  2345. struct btrfs_fs_info *fs_info = root->fs_info;
  2346. u64 root_owner;
  2347. int i;
  2348. int slot;
  2349. int ret;
  2350. for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  2351. slot = path->slots[i];
  2352. if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
  2353. path->slots[i]++;
  2354. *level = i;
  2355. WARN_ON(*level == 0);
  2356. return 0;
  2357. } else {
  2358. struct extent_buffer *parent;
  2359. if (path->nodes[*level] == root->node)
  2360. parent = path->nodes[*level];
  2361. else
  2362. parent = path->nodes[*level + 1];
  2363. root_owner = btrfs_header_owner(parent);
  2364. ret = wc->process_func(root, path->nodes[*level], wc,
  2365. btrfs_header_generation(path->nodes[*level]));
  2366. if (ret)
  2367. return ret;
  2368. if (wc->free) {
  2369. struct extent_buffer *next;
  2370. next = path->nodes[*level];
  2371. if (trans) {
  2372. btrfs_tree_lock(next);
  2373. btrfs_set_lock_blocking(next);
  2374. clean_tree_block(fs_info, next);
  2375. btrfs_wait_tree_block_writeback(next);
  2376. btrfs_tree_unlock(next);
  2377. } else {
  2378. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2379. clear_extent_buffer_dirty(next);
  2380. }
  2381. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  2382. ret = btrfs_free_and_pin_reserved_extent(
  2383. fs_info,
  2384. path->nodes[*level]->start,
  2385. path->nodes[*level]->len);
  2386. if (ret)
  2387. return ret;
  2388. }
  2389. free_extent_buffer(path->nodes[*level]);
  2390. path->nodes[*level] = NULL;
  2391. *level = i + 1;
  2392. }
  2393. }
  2394. return 1;
  2395. }
  2396. /*
  2397. * drop the reference count on the tree rooted at 'snap'. This traverses
  2398. * the tree freeing any blocks that have a ref count of zero after being
  2399. * decremented.
  2400. */
  2401. static int walk_log_tree(struct btrfs_trans_handle *trans,
  2402. struct btrfs_root *log, struct walk_control *wc)
  2403. {
  2404. struct btrfs_fs_info *fs_info = log->fs_info;
  2405. int ret = 0;
  2406. int wret;
  2407. int level;
  2408. struct btrfs_path *path;
  2409. int orig_level;
  2410. path = btrfs_alloc_path();
  2411. if (!path)
  2412. return -ENOMEM;
  2413. level = btrfs_header_level(log->node);
  2414. orig_level = level;
  2415. path->nodes[level] = log->node;
  2416. extent_buffer_get(log->node);
  2417. path->slots[level] = 0;
  2418. while (1) {
  2419. wret = walk_down_log_tree(trans, log, path, &level, wc);
  2420. if (wret > 0)
  2421. break;
  2422. if (wret < 0) {
  2423. ret = wret;
  2424. goto out;
  2425. }
  2426. wret = walk_up_log_tree(trans, log, path, &level, wc);
  2427. if (wret > 0)
  2428. break;
  2429. if (wret < 0) {
  2430. ret = wret;
  2431. goto out;
  2432. }
  2433. }
  2434. /* was the root node processed? if not, catch it here */
  2435. if (path->nodes[orig_level]) {
  2436. ret = wc->process_func(log, path->nodes[orig_level], wc,
  2437. btrfs_header_generation(path->nodes[orig_level]));
  2438. if (ret)
  2439. goto out;
  2440. if (wc->free) {
  2441. struct extent_buffer *next;
  2442. next = path->nodes[orig_level];
  2443. if (trans) {
  2444. btrfs_tree_lock(next);
  2445. btrfs_set_lock_blocking(next);
  2446. clean_tree_block(fs_info, next);
  2447. btrfs_wait_tree_block_writeback(next);
  2448. btrfs_tree_unlock(next);
  2449. } else {
  2450. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &next->bflags))
  2451. clear_extent_buffer_dirty(next);
  2452. }
  2453. WARN_ON(log->root_key.objectid !=
  2454. BTRFS_TREE_LOG_OBJECTID);
  2455. ret = btrfs_free_and_pin_reserved_extent(fs_info,
  2456. next->start, next->len);
  2457. if (ret)
  2458. goto out;
  2459. }
  2460. }
  2461. out:
  2462. btrfs_free_path(path);
  2463. return ret;
  2464. }
  2465. /*
  2466. * helper function to update the item for a given subvolumes log root
  2467. * in the tree of log roots
  2468. */
  2469. static int update_log_root(struct btrfs_trans_handle *trans,
  2470. struct btrfs_root *log)
  2471. {
  2472. struct btrfs_fs_info *fs_info = log->fs_info;
  2473. int ret;
  2474. if (log->log_transid == 1) {
  2475. /* insert root item on the first sync */
  2476. ret = btrfs_insert_root(trans, fs_info->log_root_tree,
  2477. &log->root_key, &log->root_item);
  2478. } else {
  2479. ret = btrfs_update_root(trans, fs_info->log_root_tree,
  2480. &log->root_key, &log->root_item);
  2481. }
  2482. return ret;
  2483. }
  2484. static void wait_log_commit(struct btrfs_root *root, int transid)
  2485. {
  2486. DEFINE_WAIT(wait);
  2487. int index = transid % 2;
  2488. /*
  2489. * we only allow two pending log transactions at a time,
  2490. * so we know that if ours is more than 2 older than the
  2491. * current transaction, we're done
  2492. */
  2493. for (;;) {
  2494. prepare_to_wait(&root->log_commit_wait[index],
  2495. &wait, TASK_UNINTERRUPTIBLE);
  2496. if (!(root->log_transid_committed < transid &&
  2497. atomic_read(&root->log_commit[index])))
  2498. break;
  2499. mutex_unlock(&root->log_mutex);
  2500. schedule();
  2501. mutex_lock(&root->log_mutex);
  2502. }
  2503. finish_wait(&root->log_commit_wait[index], &wait);
  2504. }
  2505. static void wait_for_writer(struct btrfs_root *root)
  2506. {
  2507. DEFINE_WAIT(wait);
  2508. for (;;) {
  2509. prepare_to_wait(&root->log_writer_wait, &wait,
  2510. TASK_UNINTERRUPTIBLE);
  2511. if (!atomic_read(&root->log_writers))
  2512. break;
  2513. mutex_unlock(&root->log_mutex);
  2514. schedule();
  2515. mutex_lock(&root->log_mutex);
  2516. }
  2517. finish_wait(&root->log_writer_wait, &wait);
  2518. }
  2519. static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
  2520. struct btrfs_log_ctx *ctx)
  2521. {
  2522. if (!ctx)
  2523. return;
  2524. mutex_lock(&root->log_mutex);
  2525. list_del_init(&ctx->list);
  2526. mutex_unlock(&root->log_mutex);
  2527. }
  2528. /*
  2529. * Invoked in log mutex context, or be sure there is no other task which
  2530. * can access the list.
  2531. */
  2532. static inline void btrfs_remove_all_log_ctxs(struct btrfs_root *root,
  2533. int index, int error)
  2534. {
  2535. struct btrfs_log_ctx *ctx;
  2536. struct btrfs_log_ctx *safe;
  2537. list_for_each_entry_safe(ctx, safe, &root->log_ctxs[index], list) {
  2538. list_del_init(&ctx->list);
  2539. ctx->log_ret = error;
  2540. }
  2541. INIT_LIST_HEAD(&root->log_ctxs[index]);
  2542. }
  2543. /*
  2544. * btrfs_sync_log does sends a given tree log down to the disk and
  2545. * updates the super blocks to record it. When this call is done,
  2546. * you know that any inodes previously logged are safely on disk only
  2547. * if it returns 0.
  2548. *
  2549. * Any other return value means you need to call btrfs_commit_transaction.
  2550. * Some of the edge cases for fsyncing directories that have had unlinks
  2551. * or renames done in the past mean that sometimes the only safe
  2552. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  2553. * that has happened.
  2554. */
  2555. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  2556. struct btrfs_root *root, struct btrfs_log_ctx *ctx)
  2557. {
  2558. int index1;
  2559. int index2;
  2560. int mark;
  2561. int ret;
  2562. struct btrfs_fs_info *fs_info = root->fs_info;
  2563. struct btrfs_root *log = root->log_root;
  2564. struct btrfs_root *log_root_tree = fs_info->log_root_tree;
  2565. int log_transid = 0;
  2566. struct btrfs_log_ctx root_log_ctx;
  2567. struct blk_plug plug;
  2568. mutex_lock(&root->log_mutex);
  2569. log_transid = ctx->log_transid;
  2570. if (root->log_transid_committed >= log_transid) {
  2571. mutex_unlock(&root->log_mutex);
  2572. return ctx->log_ret;
  2573. }
  2574. index1 = log_transid % 2;
  2575. if (atomic_read(&root->log_commit[index1])) {
  2576. wait_log_commit(root, log_transid);
  2577. mutex_unlock(&root->log_mutex);
  2578. return ctx->log_ret;
  2579. }
  2580. ASSERT(log_transid == root->log_transid);
  2581. atomic_set(&root->log_commit[index1], 1);
  2582. /* wait for previous tree log sync to complete */
  2583. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  2584. wait_log_commit(root, log_transid - 1);
  2585. while (1) {
  2586. int batch = atomic_read(&root->log_batch);
  2587. /* when we're on an ssd, just kick the log commit out */
  2588. if (!btrfs_test_opt(fs_info, SSD) &&
  2589. test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
  2590. mutex_unlock(&root->log_mutex);
  2591. schedule_timeout_uninterruptible(1);
  2592. mutex_lock(&root->log_mutex);
  2593. }
  2594. wait_for_writer(root);
  2595. if (batch == atomic_read(&root->log_batch))
  2596. break;
  2597. }
  2598. /* bail out if we need to do a full commit */
  2599. if (btrfs_need_log_full_commit(fs_info, trans)) {
  2600. ret = -EAGAIN;
  2601. btrfs_free_logged_extents(log, log_transid);
  2602. mutex_unlock(&root->log_mutex);
  2603. goto out;
  2604. }
  2605. if (log_transid % 2 == 0)
  2606. mark = EXTENT_DIRTY;
  2607. else
  2608. mark = EXTENT_NEW;
  2609. /* we start IO on all the marked extents here, but we don't actually
  2610. * wait for them until later.
  2611. */
  2612. blk_start_plug(&plug);
  2613. ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
  2614. if (ret) {
  2615. blk_finish_plug(&plug);
  2616. btrfs_abort_transaction(trans, ret);
  2617. btrfs_free_logged_extents(log, log_transid);
  2618. btrfs_set_log_full_commit(fs_info, trans);
  2619. mutex_unlock(&root->log_mutex);
  2620. goto out;
  2621. }
  2622. btrfs_set_root_node(&log->root_item, log->node);
  2623. root->log_transid++;
  2624. log->log_transid = root->log_transid;
  2625. root->log_start_pid = 0;
  2626. /*
  2627. * IO has been started, blocks of the log tree have WRITTEN flag set
  2628. * in their headers. new modifications of the log will be written to
  2629. * new positions. so it's safe to allow log writers to go in.
  2630. */
  2631. mutex_unlock(&root->log_mutex);
  2632. btrfs_init_log_ctx(&root_log_ctx, NULL);
  2633. mutex_lock(&log_root_tree->log_mutex);
  2634. atomic_inc(&log_root_tree->log_batch);
  2635. atomic_inc(&log_root_tree->log_writers);
  2636. index2 = log_root_tree->log_transid % 2;
  2637. list_add_tail(&root_log_ctx.list, &log_root_tree->log_ctxs[index2]);
  2638. root_log_ctx.log_transid = log_root_tree->log_transid;
  2639. mutex_unlock(&log_root_tree->log_mutex);
  2640. ret = update_log_root(trans, log);
  2641. mutex_lock(&log_root_tree->log_mutex);
  2642. if (atomic_dec_and_test(&log_root_tree->log_writers)) {
  2643. /*
  2644. * Implicit memory barrier after atomic_dec_and_test
  2645. */
  2646. if (waitqueue_active(&log_root_tree->log_writer_wait))
  2647. wake_up(&log_root_tree->log_writer_wait);
  2648. }
  2649. if (ret) {
  2650. if (!list_empty(&root_log_ctx.list))
  2651. list_del_init(&root_log_ctx.list);
  2652. blk_finish_plug(&plug);
  2653. btrfs_set_log_full_commit(fs_info, trans);
  2654. if (ret != -ENOSPC) {
  2655. btrfs_abort_transaction(trans, ret);
  2656. mutex_unlock(&log_root_tree->log_mutex);
  2657. goto out;
  2658. }
  2659. btrfs_wait_tree_log_extents(log, mark);
  2660. btrfs_free_logged_extents(log, log_transid);
  2661. mutex_unlock(&log_root_tree->log_mutex);
  2662. ret = -EAGAIN;
  2663. goto out;
  2664. }
  2665. if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
  2666. blk_finish_plug(&plug);
  2667. list_del_init(&root_log_ctx.list);
  2668. mutex_unlock(&log_root_tree->log_mutex);
  2669. ret = root_log_ctx.log_ret;
  2670. goto out;
  2671. }
  2672. index2 = root_log_ctx.log_transid % 2;
  2673. if (atomic_read(&log_root_tree->log_commit[index2])) {
  2674. blk_finish_plug(&plug);
  2675. ret = btrfs_wait_tree_log_extents(log, mark);
  2676. btrfs_wait_logged_extents(trans, log, log_transid);
  2677. wait_log_commit(log_root_tree,
  2678. root_log_ctx.log_transid);
  2679. mutex_unlock(&log_root_tree->log_mutex);
  2680. if (!ret)
  2681. ret = root_log_ctx.log_ret;
  2682. goto out;
  2683. }
  2684. ASSERT(root_log_ctx.log_transid == log_root_tree->log_transid);
  2685. atomic_set(&log_root_tree->log_commit[index2], 1);
  2686. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  2687. wait_log_commit(log_root_tree,
  2688. root_log_ctx.log_transid - 1);
  2689. }
  2690. wait_for_writer(log_root_tree);
  2691. /*
  2692. * now that we've moved on to the tree of log tree roots,
  2693. * check the full commit flag again
  2694. */
  2695. if (btrfs_need_log_full_commit(fs_info, trans)) {
  2696. blk_finish_plug(&plug);
  2697. btrfs_wait_tree_log_extents(log, mark);
  2698. btrfs_free_logged_extents(log, log_transid);
  2699. mutex_unlock(&log_root_tree->log_mutex);
  2700. ret = -EAGAIN;
  2701. goto out_wake_log_root;
  2702. }
  2703. ret = btrfs_write_marked_extents(fs_info,
  2704. &log_root_tree->dirty_log_pages,
  2705. EXTENT_DIRTY | EXTENT_NEW);
  2706. blk_finish_plug(&plug);
  2707. if (ret) {
  2708. btrfs_set_log_full_commit(fs_info, trans);
  2709. btrfs_abort_transaction(trans, ret);
  2710. btrfs_free_logged_extents(log, log_transid);
  2711. mutex_unlock(&log_root_tree->log_mutex);
  2712. goto out_wake_log_root;
  2713. }
  2714. ret = btrfs_wait_tree_log_extents(log, mark);
  2715. if (!ret)
  2716. ret = btrfs_wait_tree_log_extents(log_root_tree,
  2717. EXTENT_NEW | EXTENT_DIRTY);
  2718. if (ret) {
  2719. btrfs_set_log_full_commit(fs_info, trans);
  2720. btrfs_free_logged_extents(log, log_transid);
  2721. mutex_unlock(&log_root_tree->log_mutex);
  2722. goto out_wake_log_root;
  2723. }
  2724. btrfs_wait_logged_extents(trans, log, log_transid);
  2725. btrfs_set_super_log_root(fs_info->super_for_commit,
  2726. log_root_tree->node->start);
  2727. btrfs_set_super_log_root_level(fs_info->super_for_commit,
  2728. btrfs_header_level(log_root_tree->node));
  2729. log_root_tree->log_transid++;
  2730. mutex_unlock(&log_root_tree->log_mutex);
  2731. /*
  2732. * nobody else is going to jump in and write the the ctree
  2733. * super here because the log_commit atomic below is protecting
  2734. * us. We must be called with a transaction handle pinning
  2735. * the running transaction open, so a full commit can't hop
  2736. * in and cause problems either.
  2737. */
  2738. ret = write_all_supers(fs_info, 1);
  2739. if (ret) {
  2740. btrfs_set_log_full_commit(fs_info, trans);
  2741. btrfs_abort_transaction(trans, ret);
  2742. goto out_wake_log_root;
  2743. }
  2744. mutex_lock(&root->log_mutex);
  2745. if (root->last_log_commit < log_transid)
  2746. root->last_log_commit = log_transid;
  2747. mutex_unlock(&root->log_mutex);
  2748. out_wake_log_root:
  2749. mutex_lock(&log_root_tree->log_mutex);
  2750. btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
  2751. log_root_tree->log_transid_committed++;
  2752. atomic_set(&log_root_tree->log_commit[index2], 0);
  2753. mutex_unlock(&log_root_tree->log_mutex);
  2754. /*
  2755. * The barrier before waitqueue_active is implied by mutex_unlock
  2756. */
  2757. if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
  2758. wake_up(&log_root_tree->log_commit_wait[index2]);
  2759. out:
  2760. mutex_lock(&root->log_mutex);
  2761. btrfs_remove_all_log_ctxs(root, index1, ret);
  2762. root->log_transid_committed++;
  2763. atomic_set(&root->log_commit[index1], 0);
  2764. mutex_unlock(&root->log_mutex);
  2765. /*
  2766. * The barrier before waitqueue_active is implied by mutex_unlock
  2767. */
  2768. if (waitqueue_active(&root->log_commit_wait[index1]))
  2769. wake_up(&root->log_commit_wait[index1]);
  2770. return ret;
  2771. }
  2772. static void free_log_tree(struct btrfs_trans_handle *trans,
  2773. struct btrfs_root *log)
  2774. {
  2775. int ret;
  2776. u64 start;
  2777. u64 end;
  2778. struct walk_control wc = {
  2779. .free = 1,
  2780. .process_func = process_one_buffer
  2781. };
  2782. ret = walk_log_tree(trans, log, &wc);
  2783. /* I don't think this can happen but just in case */
  2784. if (ret)
  2785. btrfs_abort_transaction(trans, ret);
  2786. while (1) {
  2787. ret = find_first_extent_bit(&log->dirty_log_pages,
  2788. 0, &start, &end,
  2789. EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT,
  2790. NULL);
  2791. if (ret)
  2792. break;
  2793. clear_extent_bits(&log->dirty_log_pages, start, end,
  2794. EXTENT_DIRTY | EXTENT_NEW | EXTENT_NEED_WAIT);
  2795. }
  2796. /*
  2797. * We may have short-circuited the log tree with the full commit logic
  2798. * and left ordered extents on our list, so clear these out to keep us
  2799. * from leaking inodes and memory.
  2800. */
  2801. btrfs_free_logged_extents(log, 0);
  2802. btrfs_free_logged_extents(log, 1);
  2803. free_extent_buffer(log->node);
  2804. kfree(log);
  2805. }
  2806. /*
  2807. * free all the extents used by the tree log. This should be called
  2808. * at commit time of the full transaction
  2809. */
  2810. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  2811. {
  2812. if (root->log_root) {
  2813. free_log_tree(trans, root->log_root);
  2814. root->log_root = NULL;
  2815. }
  2816. return 0;
  2817. }
  2818. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  2819. struct btrfs_fs_info *fs_info)
  2820. {
  2821. if (fs_info->log_root_tree) {
  2822. free_log_tree(trans, fs_info->log_root_tree);
  2823. fs_info->log_root_tree = NULL;
  2824. }
  2825. return 0;
  2826. }
  2827. /*
  2828. * If both a file and directory are logged, and unlinks or renames are
  2829. * mixed in, we have a few interesting corners:
  2830. *
  2831. * create file X in dir Y
  2832. * link file X to X.link in dir Y
  2833. * fsync file X
  2834. * unlink file X but leave X.link
  2835. * fsync dir Y
  2836. *
  2837. * After a crash we would expect only X.link to exist. But file X
  2838. * didn't get fsync'd again so the log has back refs for X and X.link.
  2839. *
  2840. * We solve this by removing directory entries and inode backrefs from the
  2841. * log when a file that was logged in the current transaction is
  2842. * unlinked. Any later fsync will include the updated log entries, and
  2843. * we'll be able to reconstruct the proper directory items from backrefs.
  2844. *
  2845. * This optimizations allows us to avoid relogging the entire inode
  2846. * or the entire directory.
  2847. */
  2848. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  2849. struct btrfs_root *root,
  2850. const char *name, int name_len,
  2851. struct btrfs_inode *dir, u64 index)
  2852. {
  2853. struct btrfs_root *log;
  2854. struct btrfs_dir_item *di;
  2855. struct btrfs_path *path;
  2856. int ret;
  2857. int err = 0;
  2858. int bytes_del = 0;
  2859. u64 dir_ino = btrfs_ino(dir);
  2860. if (dir->logged_trans < trans->transid)
  2861. return 0;
  2862. ret = join_running_log_trans(root);
  2863. if (ret)
  2864. return 0;
  2865. mutex_lock(&dir->log_mutex);
  2866. log = root->log_root;
  2867. path = btrfs_alloc_path();
  2868. if (!path) {
  2869. err = -ENOMEM;
  2870. goto out_unlock;
  2871. }
  2872. di = btrfs_lookup_dir_item(trans, log, path, dir_ino,
  2873. name, name_len, -1);
  2874. if (IS_ERR(di)) {
  2875. err = PTR_ERR(di);
  2876. goto fail;
  2877. }
  2878. if (di) {
  2879. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2880. bytes_del += name_len;
  2881. if (ret) {
  2882. err = ret;
  2883. goto fail;
  2884. }
  2885. }
  2886. btrfs_release_path(path);
  2887. di = btrfs_lookup_dir_index_item(trans, log, path, dir_ino,
  2888. index, name, name_len, -1);
  2889. if (IS_ERR(di)) {
  2890. err = PTR_ERR(di);
  2891. goto fail;
  2892. }
  2893. if (di) {
  2894. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  2895. bytes_del += name_len;
  2896. if (ret) {
  2897. err = ret;
  2898. goto fail;
  2899. }
  2900. }
  2901. /* update the directory size in the log to reflect the names
  2902. * we have removed
  2903. */
  2904. if (bytes_del) {
  2905. struct btrfs_key key;
  2906. key.objectid = dir_ino;
  2907. key.offset = 0;
  2908. key.type = BTRFS_INODE_ITEM_KEY;
  2909. btrfs_release_path(path);
  2910. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  2911. if (ret < 0) {
  2912. err = ret;
  2913. goto fail;
  2914. }
  2915. if (ret == 0) {
  2916. struct btrfs_inode_item *item;
  2917. u64 i_size;
  2918. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2919. struct btrfs_inode_item);
  2920. i_size = btrfs_inode_size(path->nodes[0], item);
  2921. if (i_size > bytes_del)
  2922. i_size -= bytes_del;
  2923. else
  2924. i_size = 0;
  2925. btrfs_set_inode_size(path->nodes[0], item, i_size);
  2926. btrfs_mark_buffer_dirty(path->nodes[0]);
  2927. } else
  2928. ret = 0;
  2929. btrfs_release_path(path);
  2930. }
  2931. fail:
  2932. btrfs_free_path(path);
  2933. out_unlock:
  2934. mutex_unlock(&dir->log_mutex);
  2935. if (ret == -ENOSPC) {
  2936. btrfs_set_log_full_commit(root->fs_info, trans);
  2937. ret = 0;
  2938. } else if (ret < 0)
  2939. btrfs_abort_transaction(trans, ret);
  2940. btrfs_end_log_trans(root);
  2941. return err;
  2942. }
  2943. /* see comments for btrfs_del_dir_entries_in_log */
  2944. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  2945. struct btrfs_root *root,
  2946. const char *name, int name_len,
  2947. struct btrfs_inode *inode, u64 dirid)
  2948. {
  2949. struct btrfs_fs_info *fs_info = root->fs_info;
  2950. struct btrfs_root *log;
  2951. u64 index;
  2952. int ret;
  2953. if (inode->logged_trans < trans->transid)
  2954. return 0;
  2955. ret = join_running_log_trans(root);
  2956. if (ret)
  2957. return 0;
  2958. log = root->log_root;
  2959. mutex_lock(&inode->log_mutex);
  2960. ret = btrfs_del_inode_ref(trans, log, name, name_len, btrfs_ino(inode),
  2961. dirid, &index);
  2962. mutex_unlock(&inode->log_mutex);
  2963. if (ret == -ENOSPC) {
  2964. btrfs_set_log_full_commit(fs_info, trans);
  2965. ret = 0;
  2966. } else if (ret < 0 && ret != -ENOENT)
  2967. btrfs_abort_transaction(trans, ret);
  2968. btrfs_end_log_trans(root);
  2969. return ret;
  2970. }
  2971. /*
  2972. * creates a range item in the log for 'dirid'. first_offset and
  2973. * last_offset tell us which parts of the key space the log should
  2974. * be considered authoritative for.
  2975. */
  2976. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  2977. struct btrfs_root *log,
  2978. struct btrfs_path *path,
  2979. int key_type, u64 dirid,
  2980. u64 first_offset, u64 last_offset)
  2981. {
  2982. int ret;
  2983. struct btrfs_key key;
  2984. struct btrfs_dir_log_item *item;
  2985. key.objectid = dirid;
  2986. key.offset = first_offset;
  2987. if (key_type == BTRFS_DIR_ITEM_KEY)
  2988. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  2989. else
  2990. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  2991. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  2992. if (ret)
  2993. return ret;
  2994. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2995. struct btrfs_dir_log_item);
  2996. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  2997. btrfs_mark_buffer_dirty(path->nodes[0]);
  2998. btrfs_release_path(path);
  2999. return 0;
  3000. }
  3001. /*
  3002. * log all the items included in the current transaction for a given
  3003. * directory. This also creates the range items in the log tree required
  3004. * to replay anything deleted before the fsync
  3005. */
  3006. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  3007. struct btrfs_root *root, struct btrfs_inode *inode,
  3008. struct btrfs_path *path,
  3009. struct btrfs_path *dst_path, int key_type,
  3010. struct btrfs_log_ctx *ctx,
  3011. u64 min_offset, u64 *last_offset_ret)
  3012. {
  3013. struct btrfs_key min_key;
  3014. struct btrfs_root *log = root->log_root;
  3015. struct extent_buffer *src;
  3016. int err = 0;
  3017. int ret;
  3018. int i;
  3019. int nritems;
  3020. u64 first_offset = min_offset;
  3021. u64 last_offset = (u64)-1;
  3022. u64 ino = btrfs_ino(inode);
  3023. log = root->log_root;
  3024. min_key.objectid = ino;
  3025. min_key.type = key_type;
  3026. min_key.offset = min_offset;
  3027. ret = btrfs_search_forward(root, &min_key, path, trans->transid);
  3028. /*
  3029. * we didn't find anything from this transaction, see if there
  3030. * is anything at all
  3031. */
  3032. if (ret != 0 || min_key.objectid != ino || min_key.type != key_type) {
  3033. min_key.objectid = ino;
  3034. min_key.type = key_type;
  3035. min_key.offset = (u64)-1;
  3036. btrfs_release_path(path);
  3037. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3038. if (ret < 0) {
  3039. btrfs_release_path(path);
  3040. return ret;
  3041. }
  3042. ret = btrfs_previous_item(root, path, ino, key_type);
  3043. /* if ret == 0 there are items for this type,
  3044. * create a range to tell us the last key of this type.
  3045. * otherwise, there are no items in this directory after
  3046. * *min_offset, and we create a range to indicate that.
  3047. */
  3048. if (ret == 0) {
  3049. struct btrfs_key tmp;
  3050. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  3051. path->slots[0]);
  3052. if (key_type == tmp.type)
  3053. first_offset = max(min_offset, tmp.offset) + 1;
  3054. }
  3055. goto done;
  3056. }
  3057. /* go backward to find any previous key */
  3058. ret = btrfs_previous_item(root, path, ino, key_type);
  3059. if (ret == 0) {
  3060. struct btrfs_key tmp;
  3061. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3062. if (key_type == tmp.type) {
  3063. first_offset = tmp.offset;
  3064. ret = overwrite_item(trans, log, dst_path,
  3065. path->nodes[0], path->slots[0],
  3066. &tmp);
  3067. if (ret) {
  3068. err = ret;
  3069. goto done;
  3070. }
  3071. }
  3072. }
  3073. btrfs_release_path(path);
  3074. /* find the first key from this transaction again */
  3075. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  3076. if (WARN_ON(ret != 0))
  3077. goto done;
  3078. /*
  3079. * we have a block from this transaction, log every item in it
  3080. * from our directory
  3081. */
  3082. while (1) {
  3083. struct btrfs_key tmp;
  3084. src = path->nodes[0];
  3085. nritems = btrfs_header_nritems(src);
  3086. for (i = path->slots[0]; i < nritems; i++) {
  3087. struct btrfs_dir_item *di;
  3088. btrfs_item_key_to_cpu(src, &min_key, i);
  3089. if (min_key.objectid != ino || min_key.type != key_type)
  3090. goto done;
  3091. ret = overwrite_item(trans, log, dst_path, src, i,
  3092. &min_key);
  3093. if (ret) {
  3094. err = ret;
  3095. goto done;
  3096. }
  3097. /*
  3098. * We must make sure that when we log a directory entry,
  3099. * the corresponding inode, after log replay, has a
  3100. * matching link count. For example:
  3101. *
  3102. * touch foo
  3103. * mkdir mydir
  3104. * sync
  3105. * ln foo mydir/bar
  3106. * xfs_io -c "fsync" mydir
  3107. * <crash>
  3108. * <mount fs and log replay>
  3109. *
  3110. * Would result in a fsync log that when replayed, our
  3111. * file inode would have a link count of 1, but we get
  3112. * two directory entries pointing to the same inode.
  3113. * After removing one of the names, it would not be
  3114. * possible to remove the other name, which resulted
  3115. * always in stale file handle errors, and would not
  3116. * be possible to rmdir the parent directory, since
  3117. * its i_size could never decrement to the value
  3118. * BTRFS_EMPTY_DIR_SIZE, resulting in -ENOTEMPTY errors.
  3119. */
  3120. di = btrfs_item_ptr(src, i, struct btrfs_dir_item);
  3121. btrfs_dir_item_key_to_cpu(src, di, &tmp);
  3122. if (ctx &&
  3123. (btrfs_dir_transid(src, di) == trans->transid ||
  3124. btrfs_dir_type(src, di) == BTRFS_FT_DIR) &&
  3125. tmp.type != BTRFS_ROOT_ITEM_KEY)
  3126. ctx->log_new_dentries = true;
  3127. }
  3128. path->slots[0] = nritems;
  3129. /*
  3130. * look ahead to the next item and see if it is also
  3131. * from this directory and from this transaction
  3132. */
  3133. ret = btrfs_next_leaf(root, path);
  3134. if (ret == 1) {
  3135. last_offset = (u64)-1;
  3136. goto done;
  3137. }
  3138. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  3139. if (tmp.objectid != ino || tmp.type != key_type) {
  3140. last_offset = (u64)-1;
  3141. goto done;
  3142. }
  3143. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  3144. ret = overwrite_item(trans, log, dst_path,
  3145. path->nodes[0], path->slots[0],
  3146. &tmp);
  3147. if (ret)
  3148. err = ret;
  3149. else
  3150. last_offset = tmp.offset;
  3151. goto done;
  3152. }
  3153. }
  3154. done:
  3155. btrfs_release_path(path);
  3156. btrfs_release_path(dst_path);
  3157. if (err == 0) {
  3158. *last_offset_ret = last_offset;
  3159. /*
  3160. * insert the log range keys to indicate where the log
  3161. * is valid
  3162. */
  3163. ret = insert_dir_log_key(trans, log, path, key_type,
  3164. ino, first_offset, last_offset);
  3165. if (ret)
  3166. err = ret;
  3167. }
  3168. return err;
  3169. }
  3170. /*
  3171. * logging directories is very similar to logging inodes, We find all the items
  3172. * from the current transaction and write them to the log.
  3173. *
  3174. * The recovery code scans the directory in the subvolume, and if it finds a
  3175. * key in the range logged that is not present in the log tree, then it means
  3176. * that dir entry was unlinked during the transaction.
  3177. *
  3178. * In order for that scan to work, we must include one key smaller than
  3179. * the smallest logged by this transaction and one key larger than the largest
  3180. * key logged by this transaction.
  3181. */
  3182. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  3183. struct btrfs_root *root, struct btrfs_inode *inode,
  3184. struct btrfs_path *path,
  3185. struct btrfs_path *dst_path,
  3186. struct btrfs_log_ctx *ctx)
  3187. {
  3188. u64 min_key;
  3189. u64 max_key;
  3190. int ret;
  3191. int key_type = BTRFS_DIR_ITEM_KEY;
  3192. again:
  3193. min_key = 0;
  3194. max_key = 0;
  3195. while (1) {
  3196. ret = log_dir_items(trans, root, inode, path, dst_path, key_type,
  3197. ctx, min_key, &max_key);
  3198. if (ret)
  3199. return ret;
  3200. if (max_key == (u64)-1)
  3201. break;
  3202. min_key = max_key + 1;
  3203. }
  3204. if (key_type == BTRFS_DIR_ITEM_KEY) {
  3205. key_type = BTRFS_DIR_INDEX_KEY;
  3206. goto again;
  3207. }
  3208. return 0;
  3209. }
  3210. /*
  3211. * a helper function to drop items from the log before we relog an
  3212. * inode. max_key_type indicates the highest item type to remove.
  3213. * This cannot be run for file data extents because it does not
  3214. * free the extents they point to.
  3215. */
  3216. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  3217. struct btrfs_root *log,
  3218. struct btrfs_path *path,
  3219. u64 objectid, int max_key_type)
  3220. {
  3221. int ret;
  3222. struct btrfs_key key;
  3223. struct btrfs_key found_key;
  3224. int start_slot;
  3225. key.objectid = objectid;
  3226. key.type = max_key_type;
  3227. key.offset = (u64)-1;
  3228. while (1) {
  3229. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  3230. BUG_ON(ret == 0); /* Logic error */
  3231. if (ret < 0)
  3232. break;
  3233. if (path->slots[0] == 0)
  3234. break;
  3235. path->slots[0]--;
  3236. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  3237. path->slots[0]);
  3238. if (found_key.objectid != objectid)
  3239. break;
  3240. found_key.offset = 0;
  3241. found_key.type = 0;
  3242. ret = btrfs_bin_search(path->nodes[0], &found_key, 0,
  3243. &start_slot);
  3244. ret = btrfs_del_items(trans, log, path, start_slot,
  3245. path->slots[0] - start_slot + 1);
  3246. /*
  3247. * If start slot isn't 0 then we don't need to re-search, we've
  3248. * found the last guy with the objectid in this tree.
  3249. */
  3250. if (ret || start_slot != 0)
  3251. break;
  3252. btrfs_release_path(path);
  3253. }
  3254. btrfs_release_path(path);
  3255. if (ret > 0)
  3256. ret = 0;
  3257. return ret;
  3258. }
  3259. static void fill_inode_item(struct btrfs_trans_handle *trans,
  3260. struct extent_buffer *leaf,
  3261. struct btrfs_inode_item *item,
  3262. struct inode *inode, int log_inode_only,
  3263. u64 logged_isize)
  3264. {
  3265. struct btrfs_map_token token;
  3266. btrfs_init_map_token(&token);
  3267. if (log_inode_only) {
  3268. /* set the generation to zero so the recover code
  3269. * can tell the difference between an logging
  3270. * just to say 'this inode exists' and a logging
  3271. * to say 'update this inode with these values'
  3272. */
  3273. btrfs_set_token_inode_generation(leaf, item, 0, &token);
  3274. btrfs_set_token_inode_size(leaf, item, logged_isize, &token);
  3275. } else {
  3276. btrfs_set_token_inode_generation(leaf, item,
  3277. BTRFS_I(inode)->generation,
  3278. &token);
  3279. btrfs_set_token_inode_size(leaf, item, inode->i_size, &token);
  3280. }
  3281. btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
  3282. btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
  3283. btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
  3284. btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
  3285. btrfs_set_token_timespec_sec(leaf, &item->atime,
  3286. inode->i_atime.tv_sec, &token);
  3287. btrfs_set_token_timespec_nsec(leaf, &item->atime,
  3288. inode->i_atime.tv_nsec, &token);
  3289. btrfs_set_token_timespec_sec(leaf, &item->mtime,
  3290. inode->i_mtime.tv_sec, &token);
  3291. btrfs_set_token_timespec_nsec(leaf, &item->mtime,
  3292. inode->i_mtime.tv_nsec, &token);
  3293. btrfs_set_token_timespec_sec(leaf, &item->ctime,
  3294. inode->i_ctime.tv_sec, &token);
  3295. btrfs_set_token_timespec_nsec(leaf, &item->ctime,
  3296. inode->i_ctime.tv_nsec, &token);
  3297. btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
  3298. &token);
  3299. btrfs_set_token_inode_sequence(leaf, item,
  3300. inode_peek_iversion(inode), &token);
  3301. btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
  3302. btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
  3303. btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
  3304. btrfs_set_token_inode_block_group(leaf, item, 0, &token);
  3305. }
  3306. static int log_inode_item(struct btrfs_trans_handle *trans,
  3307. struct btrfs_root *log, struct btrfs_path *path,
  3308. struct btrfs_inode *inode)
  3309. {
  3310. struct btrfs_inode_item *inode_item;
  3311. int ret;
  3312. ret = btrfs_insert_empty_item(trans, log, path,
  3313. &inode->location, sizeof(*inode_item));
  3314. if (ret && ret != -EEXIST)
  3315. return ret;
  3316. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3317. struct btrfs_inode_item);
  3318. fill_inode_item(trans, path->nodes[0], inode_item, &inode->vfs_inode,
  3319. 0, 0);
  3320. btrfs_release_path(path);
  3321. return 0;
  3322. }
  3323. static noinline int copy_items(struct btrfs_trans_handle *trans,
  3324. struct btrfs_inode *inode,
  3325. struct btrfs_path *dst_path,
  3326. struct btrfs_path *src_path, u64 *last_extent,
  3327. int start_slot, int nr, int inode_only,
  3328. u64 logged_isize)
  3329. {
  3330. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  3331. unsigned long src_offset;
  3332. unsigned long dst_offset;
  3333. struct btrfs_root *log = inode->root->log_root;
  3334. struct btrfs_file_extent_item *extent;
  3335. struct btrfs_inode_item *inode_item;
  3336. struct extent_buffer *src = src_path->nodes[0];
  3337. struct btrfs_key first_key, last_key, key;
  3338. int ret;
  3339. struct btrfs_key *ins_keys;
  3340. u32 *ins_sizes;
  3341. char *ins_data;
  3342. int i;
  3343. struct list_head ordered_sums;
  3344. int skip_csum = inode->flags & BTRFS_INODE_NODATASUM;
  3345. bool has_extents = false;
  3346. bool need_find_last_extent = true;
  3347. bool done = false;
  3348. INIT_LIST_HEAD(&ordered_sums);
  3349. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  3350. nr * sizeof(u32), GFP_NOFS);
  3351. if (!ins_data)
  3352. return -ENOMEM;
  3353. first_key.objectid = (u64)-1;
  3354. ins_sizes = (u32 *)ins_data;
  3355. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  3356. for (i = 0; i < nr; i++) {
  3357. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  3358. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  3359. }
  3360. ret = btrfs_insert_empty_items(trans, log, dst_path,
  3361. ins_keys, ins_sizes, nr);
  3362. if (ret) {
  3363. kfree(ins_data);
  3364. return ret;
  3365. }
  3366. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  3367. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  3368. dst_path->slots[0]);
  3369. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  3370. if (i == nr - 1)
  3371. last_key = ins_keys[i];
  3372. if (ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  3373. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  3374. dst_path->slots[0],
  3375. struct btrfs_inode_item);
  3376. fill_inode_item(trans, dst_path->nodes[0], inode_item,
  3377. &inode->vfs_inode,
  3378. inode_only == LOG_INODE_EXISTS,
  3379. logged_isize);
  3380. } else {
  3381. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  3382. src_offset, ins_sizes[i]);
  3383. }
  3384. /*
  3385. * We set need_find_last_extent here in case we know we were
  3386. * processing other items and then walk into the first extent in
  3387. * the inode. If we don't hit an extent then nothing changes,
  3388. * we'll do the last search the next time around.
  3389. */
  3390. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY) {
  3391. has_extents = true;
  3392. if (first_key.objectid == (u64)-1)
  3393. first_key = ins_keys[i];
  3394. } else {
  3395. need_find_last_extent = false;
  3396. }
  3397. /* take a reference on file data extents so that truncates
  3398. * or deletes of this inode don't have to relog the inode
  3399. * again
  3400. */
  3401. if (ins_keys[i].type == BTRFS_EXTENT_DATA_KEY &&
  3402. !skip_csum) {
  3403. int found_type;
  3404. extent = btrfs_item_ptr(src, start_slot + i,
  3405. struct btrfs_file_extent_item);
  3406. if (btrfs_file_extent_generation(src, extent) < trans->transid)
  3407. continue;
  3408. found_type = btrfs_file_extent_type(src, extent);
  3409. if (found_type == BTRFS_FILE_EXTENT_REG) {
  3410. u64 ds, dl, cs, cl;
  3411. ds = btrfs_file_extent_disk_bytenr(src,
  3412. extent);
  3413. /* ds == 0 is a hole */
  3414. if (ds == 0)
  3415. continue;
  3416. dl = btrfs_file_extent_disk_num_bytes(src,
  3417. extent);
  3418. cs = btrfs_file_extent_offset(src, extent);
  3419. cl = btrfs_file_extent_num_bytes(src,
  3420. extent);
  3421. if (btrfs_file_extent_compression(src,
  3422. extent)) {
  3423. cs = 0;
  3424. cl = dl;
  3425. }
  3426. ret = btrfs_lookup_csums_range(
  3427. fs_info->csum_root,
  3428. ds + cs, ds + cs + cl - 1,
  3429. &ordered_sums, 0);
  3430. if (ret) {
  3431. btrfs_release_path(dst_path);
  3432. kfree(ins_data);
  3433. return ret;
  3434. }
  3435. }
  3436. }
  3437. }
  3438. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  3439. btrfs_release_path(dst_path);
  3440. kfree(ins_data);
  3441. /*
  3442. * we have to do this after the loop above to avoid changing the
  3443. * log tree while trying to change the log tree.
  3444. */
  3445. ret = 0;
  3446. while (!list_empty(&ordered_sums)) {
  3447. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3448. struct btrfs_ordered_sum,
  3449. list);
  3450. if (!ret)
  3451. ret = btrfs_csum_file_blocks(trans, log, sums);
  3452. list_del(&sums->list);
  3453. kfree(sums);
  3454. }
  3455. if (!has_extents)
  3456. return ret;
  3457. if (need_find_last_extent && *last_extent == first_key.offset) {
  3458. /*
  3459. * We don't have any leafs between our current one and the one
  3460. * we processed before that can have file extent items for our
  3461. * inode (and have a generation number smaller than our current
  3462. * transaction id).
  3463. */
  3464. need_find_last_extent = false;
  3465. }
  3466. /*
  3467. * Because we use btrfs_search_forward we could skip leaves that were
  3468. * not modified and then assume *last_extent is valid when it really
  3469. * isn't. So back up to the previous leaf and read the end of the last
  3470. * extent before we go and fill in holes.
  3471. */
  3472. if (need_find_last_extent) {
  3473. u64 len;
  3474. ret = btrfs_prev_leaf(inode->root, src_path);
  3475. if (ret < 0)
  3476. return ret;
  3477. if (ret)
  3478. goto fill_holes;
  3479. if (src_path->slots[0])
  3480. src_path->slots[0]--;
  3481. src = src_path->nodes[0];
  3482. btrfs_item_key_to_cpu(src, &key, src_path->slots[0]);
  3483. if (key.objectid != btrfs_ino(inode) ||
  3484. key.type != BTRFS_EXTENT_DATA_KEY)
  3485. goto fill_holes;
  3486. extent = btrfs_item_ptr(src, src_path->slots[0],
  3487. struct btrfs_file_extent_item);
  3488. if (btrfs_file_extent_type(src, extent) ==
  3489. BTRFS_FILE_EXTENT_INLINE) {
  3490. len = btrfs_file_extent_inline_len(src,
  3491. src_path->slots[0],
  3492. extent);
  3493. *last_extent = ALIGN(key.offset + len,
  3494. fs_info->sectorsize);
  3495. } else {
  3496. len = btrfs_file_extent_num_bytes(src, extent);
  3497. *last_extent = key.offset + len;
  3498. }
  3499. }
  3500. fill_holes:
  3501. /* So we did prev_leaf, now we need to move to the next leaf, but a few
  3502. * things could have happened
  3503. *
  3504. * 1) A merge could have happened, so we could currently be on a leaf
  3505. * that holds what we were copying in the first place.
  3506. * 2) A split could have happened, and now not all of the items we want
  3507. * are on the same leaf.
  3508. *
  3509. * So we need to adjust how we search for holes, we need to drop the
  3510. * path and re-search for the first extent key we found, and then walk
  3511. * forward until we hit the last one we copied.
  3512. */
  3513. if (need_find_last_extent) {
  3514. /* btrfs_prev_leaf could return 1 without releasing the path */
  3515. btrfs_release_path(src_path);
  3516. ret = btrfs_search_slot(NULL, inode->root, &first_key,
  3517. src_path, 0, 0);
  3518. if (ret < 0)
  3519. return ret;
  3520. ASSERT(ret == 0);
  3521. src = src_path->nodes[0];
  3522. i = src_path->slots[0];
  3523. } else {
  3524. i = start_slot;
  3525. }
  3526. /*
  3527. * Ok so here we need to go through and fill in any holes we may have
  3528. * to make sure that holes are punched for those areas in case they had
  3529. * extents previously.
  3530. */
  3531. while (!done) {
  3532. u64 offset, len;
  3533. u64 extent_end;
  3534. if (i >= btrfs_header_nritems(src_path->nodes[0])) {
  3535. ret = btrfs_next_leaf(inode->root, src_path);
  3536. if (ret < 0)
  3537. return ret;
  3538. ASSERT(ret == 0);
  3539. src = src_path->nodes[0];
  3540. i = 0;
  3541. need_find_last_extent = true;
  3542. }
  3543. btrfs_item_key_to_cpu(src, &key, i);
  3544. if (!btrfs_comp_cpu_keys(&key, &last_key))
  3545. done = true;
  3546. if (key.objectid != btrfs_ino(inode) ||
  3547. key.type != BTRFS_EXTENT_DATA_KEY) {
  3548. i++;
  3549. continue;
  3550. }
  3551. extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
  3552. if (btrfs_file_extent_type(src, extent) ==
  3553. BTRFS_FILE_EXTENT_INLINE) {
  3554. len = btrfs_file_extent_inline_len(src, i, extent);
  3555. extent_end = ALIGN(key.offset + len,
  3556. fs_info->sectorsize);
  3557. } else {
  3558. len = btrfs_file_extent_num_bytes(src, extent);
  3559. extent_end = key.offset + len;
  3560. }
  3561. i++;
  3562. if (*last_extent == key.offset) {
  3563. *last_extent = extent_end;
  3564. continue;
  3565. }
  3566. offset = *last_extent;
  3567. len = key.offset - *last_extent;
  3568. ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
  3569. offset, 0, 0, len, 0, len, 0, 0, 0);
  3570. if (ret)
  3571. break;
  3572. *last_extent = extent_end;
  3573. }
  3574. /*
  3575. * Check if there is a hole between the last extent found in our leaf
  3576. * and the first extent in the next leaf. If there is one, we need to
  3577. * log an explicit hole so that at replay time we can punch the hole.
  3578. */
  3579. if (ret == 0 &&
  3580. key.objectid == btrfs_ino(inode) &&
  3581. key.type == BTRFS_EXTENT_DATA_KEY &&
  3582. i == btrfs_header_nritems(src_path->nodes[0])) {
  3583. ret = btrfs_next_leaf(inode->root, src_path);
  3584. need_find_last_extent = true;
  3585. if (ret > 0) {
  3586. ret = 0;
  3587. } else if (ret == 0) {
  3588. btrfs_item_key_to_cpu(src_path->nodes[0], &key,
  3589. src_path->slots[0]);
  3590. if (key.objectid == btrfs_ino(inode) &&
  3591. key.type == BTRFS_EXTENT_DATA_KEY &&
  3592. *last_extent < key.offset) {
  3593. const u64 len = key.offset - *last_extent;
  3594. ret = btrfs_insert_file_extent(trans, log,
  3595. btrfs_ino(inode),
  3596. *last_extent, 0,
  3597. 0, len, 0, len,
  3598. 0, 0, 0);
  3599. }
  3600. }
  3601. }
  3602. /*
  3603. * Need to let the callers know we dropped the path so they should
  3604. * re-search.
  3605. */
  3606. if (!ret && need_find_last_extent)
  3607. ret = 1;
  3608. return ret;
  3609. }
  3610. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  3611. {
  3612. struct extent_map *em1, *em2;
  3613. em1 = list_entry(a, struct extent_map, list);
  3614. em2 = list_entry(b, struct extent_map, list);
  3615. if (em1->start < em2->start)
  3616. return -1;
  3617. else if (em1->start > em2->start)
  3618. return 1;
  3619. return 0;
  3620. }
  3621. static int wait_ordered_extents(struct btrfs_trans_handle *trans,
  3622. struct inode *inode,
  3623. struct btrfs_root *root,
  3624. const struct extent_map *em,
  3625. const struct list_head *logged_list,
  3626. bool *ordered_io_error)
  3627. {
  3628. struct btrfs_fs_info *fs_info = root->fs_info;
  3629. struct btrfs_ordered_extent *ordered;
  3630. struct btrfs_root *log = root->log_root;
  3631. u64 mod_start = em->mod_start;
  3632. u64 mod_len = em->mod_len;
  3633. const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  3634. u64 csum_offset;
  3635. u64 csum_len;
  3636. LIST_HEAD(ordered_sums);
  3637. int ret = 0;
  3638. *ordered_io_error = false;
  3639. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  3640. em->block_start == EXTENT_MAP_HOLE)
  3641. return 0;
  3642. /*
  3643. * Wait far any ordered extent that covers our extent map. If it
  3644. * finishes without an error, first check and see if our csums are on
  3645. * our outstanding ordered extents.
  3646. */
  3647. list_for_each_entry(ordered, logged_list, log_list) {
  3648. struct btrfs_ordered_sum *sum;
  3649. if (!mod_len)
  3650. break;
  3651. if (ordered->file_offset + ordered->len <= mod_start ||
  3652. mod_start + mod_len <= ordered->file_offset)
  3653. continue;
  3654. if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
  3655. !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
  3656. !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
  3657. const u64 start = ordered->file_offset;
  3658. const u64 end = ordered->file_offset + ordered->len - 1;
  3659. WARN_ON(ordered->inode != inode);
  3660. filemap_fdatawrite_range(inode->i_mapping, start, end);
  3661. }
  3662. wait_event(ordered->wait,
  3663. (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
  3664. test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
  3665. if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
  3666. /*
  3667. * Clear the AS_EIO/AS_ENOSPC flags from the inode's
  3668. * i_mapping flags, so that the next fsync won't get
  3669. * an outdated io error too.
  3670. */
  3671. filemap_check_errors(inode->i_mapping);
  3672. *ordered_io_error = true;
  3673. break;
  3674. }
  3675. /*
  3676. * We are going to copy all the csums on this ordered extent, so
  3677. * go ahead and adjust mod_start and mod_len in case this
  3678. * ordered extent has already been logged.
  3679. */
  3680. if (ordered->file_offset > mod_start) {
  3681. if (ordered->file_offset + ordered->len >=
  3682. mod_start + mod_len)
  3683. mod_len = ordered->file_offset - mod_start;
  3684. /*
  3685. * If we have this case
  3686. *
  3687. * |--------- logged extent ---------|
  3688. * |----- ordered extent ----|
  3689. *
  3690. * Just don't mess with mod_start and mod_len, we'll
  3691. * just end up logging more csums than we need and it
  3692. * will be ok.
  3693. */
  3694. } else {
  3695. if (ordered->file_offset + ordered->len <
  3696. mod_start + mod_len) {
  3697. mod_len = (mod_start + mod_len) -
  3698. (ordered->file_offset + ordered->len);
  3699. mod_start = ordered->file_offset +
  3700. ordered->len;
  3701. } else {
  3702. mod_len = 0;
  3703. }
  3704. }
  3705. if (skip_csum)
  3706. continue;
  3707. /*
  3708. * To keep us from looping for the above case of an ordered
  3709. * extent that falls inside of the logged extent.
  3710. */
  3711. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
  3712. &ordered->flags))
  3713. continue;
  3714. list_for_each_entry(sum, &ordered->list, list) {
  3715. ret = btrfs_csum_file_blocks(trans, log, sum);
  3716. if (ret)
  3717. break;
  3718. }
  3719. }
  3720. if (*ordered_io_error || !mod_len || ret || skip_csum)
  3721. return ret;
  3722. if (em->compress_type) {
  3723. csum_offset = 0;
  3724. csum_len = max(em->block_len, em->orig_block_len);
  3725. } else {
  3726. csum_offset = mod_start - em->start;
  3727. csum_len = mod_len;
  3728. }
  3729. /* block start is already adjusted for the file extent offset. */
  3730. ret = btrfs_lookup_csums_range(fs_info->csum_root,
  3731. em->block_start + csum_offset,
  3732. em->block_start + csum_offset +
  3733. csum_len - 1, &ordered_sums, 0);
  3734. if (ret)
  3735. return ret;
  3736. while (!list_empty(&ordered_sums)) {
  3737. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3738. struct btrfs_ordered_sum,
  3739. list);
  3740. if (!ret)
  3741. ret = btrfs_csum_file_blocks(trans, log, sums);
  3742. list_del(&sums->list);
  3743. kfree(sums);
  3744. }
  3745. return ret;
  3746. }
  3747. static int log_one_extent(struct btrfs_trans_handle *trans,
  3748. struct btrfs_inode *inode, struct btrfs_root *root,
  3749. const struct extent_map *em,
  3750. struct btrfs_path *path,
  3751. const struct list_head *logged_list,
  3752. struct btrfs_log_ctx *ctx)
  3753. {
  3754. struct btrfs_root *log = root->log_root;
  3755. struct btrfs_file_extent_item *fi;
  3756. struct extent_buffer *leaf;
  3757. struct btrfs_map_token token;
  3758. struct btrfs_key key;
  3759. u64 extent_offset = em->start - em->orig_start;
  3760. u64 block_len;
  3761. int ret;
  3762. int extent_inserted = 0;
  3763. bool ordered_io_err = false;
  3764. ret = wait_ordered_extents(trans, &inode->vfs_inode, root, em,
  3765. logged_list, &ordered_io_err);
  3766. if (ret)
  3767. return ret;
  3768. if (ordered_io_err) {
  3769. ctx->io_err = -EIO;
  3770. return ctx->io_err;
  3771. }
  3772. btrfs_init_map_token(&token);
  3773. ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
  3774. em->start + em->len, NULL, 0, 1,
  3775. sizeof(*fi), &extent_inserted);
  3776. if (ret)
  3777. return ret;
  3778. if (!extent_inserted) {
  3779. key.objectid = btrfs_ino(inode);
  3780. key.type = BTRFS_EXTENT_DATA_KEY;
  3781. key.offset = em->start;
  3782. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3783. sizeof(*fi));
  3784. if (ret)
  3785. return ret;
  3786. }
  3787. leaf = path->nodes[0];
  3788. fi = btrfs_item_ptr(leaf, path->slots[0],
  3789. struct btrfs_file_extent_item);
  3790. btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
  3791. &token);
  3792. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  3793. btrfs_set_token_file_extent_type(leaf, fi,
  3794. BTRFS_FILE_EXTENT_PREALLOC,
  3795. &token);
  3796. else
  3797. btrfs_set_token_file_extent_type(leaf, fi,
  3798. BTRFS_FILE_EXTENT_REG,
  3799. &token);
  3800. block_len = max(em->block_len, em->orig_block_len);
  3801. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  3802. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3803. em->block_start,
  3804. &token);
  3805. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3806. &token);
  3807. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  3808. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3809. em->block_start -
  3810. extent_offset, &token);
  3811. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3812. &token);
  3813. } else {
  3814. btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
  3815. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
  3816. &token);
  3817. }
  3818. btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
  3819. btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
  3820. btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
  3821. btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
  3822. &token);
  3823. btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
  3824. btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
  3825. btrfs_mark_buffer_dirty(leaf);
  3826. btrfs_release_path(path);
  3827. return ret;
  3828. }
  3829. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3830. struct btrfs_root *root,
  3831. struct btrfs_inode *inode,
  3832. struct btrfs_path *path,
  3833. struct list_head *logged_list,
  3834. struct btrfs_log_ctx *ctx,
  3835. const u64 start,
  3836. const u64 end)
  3837. {
  3838. struct extent_map *em, *n;
  3839. struct list_head extents;
  3840. struct extent_map_tree *tree = &inode->extent_tree;
  3841. u64 logged_start, logged_end;
  3842. u64 test_gen;
  3843. int ret = 0;
  3844. int num = 0;
  3845. INIT_LIST_HEAD(&extents);
  3846. down_write(&inode->dio_sem);
  3847. write_lock(&tree->lock);
  3848. test_gen = root->fs_info->last_trans_committed;
  3849. logged_start = start;
  3850. logged_end = end;
  3851. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3852. list_del_init(&em->list);
  3853. /*
  3854. * Just an arbitrary number, this can be really CPU intensive
  3855. * once we start getting a lot of extents, and really once we
  3856. * have a bunch of extents we just want to commit since it will
  3857. * be faster.
  3858. */
  3859. if (++num > 32768) {
  3860. list_del_init(&tree->modified_extents);
  3861. ret = -EFBIG;
  3862. goto process;
  3863. }
  3864. if (em->generation <= test_gen)
  3865. continue;
  3866. if (em->start < logged_start)
  3867. logged_start = em->start;
  3868. if ((em->start + em->len - 1) > logged_end)
  3869. logged_end = em->start + em->len - 1;
  3870. /* Need a ref to keep it from getting evicted from cache */
  3871. refcount_inc(&em->refs);
  3872. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  3873. list_add_tail(&em->list, &extents);
  3874. num++;
  3875. }
  3876. list_sort(NULL, &extents, extent_cmp);
  3877. btrfs_get_logged_extents(inode, logged_list, logged_start, logged_end);
  3878. /*
  3879. * Some ordered extents started by fsync might have completed
  3880. * before we could collect them into the list logged_list, which
  3881. * means they're gone, not in our logged_list nor in the inode's
  3882. * ordered tree. We want the application/user space to know an
  3883. * error happened while attempting to persist file data so that
  3884. * it can take proper action. If such error happened, we leave
  3885. * without writing to the log tree and the fsync must report the
  3886. * file data write error and not commit the current transaction.
  3887. */
  3888. ret = filemap_check_errors(inode->vfs_inode.i_mapping);
  3889. if (ret)
  3890. ctx->io_err = ret;
  3891. process:
  3892. while (!list_empty(&extents)) {
  3893. em = list_entry(extents.next, struct extent_map, list);
  3894. list_del_init(&em->list);
  3895. /*
  3896. * If we had an error we just need to delete everybody from our
  3897. * private list.
  3898. */
  3899. if (ret) {
  3900. clear_em_logging(tree, em);
  3901. free_extent_map(em);
  3902. continue;
  3903. }
  3904. write_unlock(&tree->lock);
  3905. ret = log_one_extent(trans, inode, root, em, path, logged_list,
  3906. ctx);
  3907. write_lock(&tree->lock);
  3908. clear_em_logging(tree, em);
  3909. free_extent_map(em);
  3910. }
  3911. WARN_ON(!list_empty(&extents));
  3912. write_unlock(&tree->lock);
  3913. up_write(&inode->dio_sem);
  3914. btrfs_release_path(path);
  3915. return ret;
  3916. }
  3917. static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
  3918. struct btrfs_path *path, u64 *size_ret)
  3919. {
  3920. struct btrfs_key key;
  3921. int ret;
  3922. key.objectid = btrfs_ino(inode);
  3923. key.type = BTRFS_INODE_ITEM_KEY;
  3924. key.offset = 0;
  3925. ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
  3926. if (ret < 0) {
  3927. return ret;
  3928. } else if (ret > 0) {
  3929. *size_ret = 0;
  3930. } else {
  3931. struct btrfs_inode_item *item;
  3932. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3933. struct btrfs_inode_item);
  3934. *size_ret = btrfs_inode_size(path->nodes[0], item);
  3935. }
  3936. btrfs_release_path(path);
  3937. return 0;
  3938. }
  3939. /*
  3940. * At the moment we always log all xattrs. This is to figure out at log replay
  3941. * time which xattrs must have their deletion replayed. If a xattr is missing
  3942. * in the log tree and exists in the fs/subvol tree, we delete it. This is
  3943. * because if a xattr is deleted, the inode is fsynced and a power failure
  3944. * happens, causing the log to be replayed the next time the fs is mounted,
  3945. * we want the xattr to not exist anymore (same behaviour as other filesystems
  3946. * with a journal, ext3/4, xfs, f2fs, etc).
  3947. */
  3948. static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
  3949. struct btrfs_root *root,
  3950. struct btrfs_inode *inode,
  3951. struct btrfs_path *path,
  3952. struct btrfs_path *dst_path)
  3953. {
  3954. int ret;
  3955. struct btrfs_key key;
  3956. const u64 ino = btrfs_ino(inode);
  3957. int ins_nr = 0;
  3958. int start_slot = 0;
  3959. key.objectid = ino;
  3960. key.type = BTRFS_XATTR_ITEM_KEY;
  3961. key.offset = 0;
  3962. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3963. if (ret < 0)
  3964. return ret;
  3965. while (true) {
  3966. int slot = path->slots[0];
  3967. struct extent_buffer *leaf = path->nodes[0];
  3968. int nritems = btrfs_header_nritems(leaf);
  3969. if (slot >= nritems) {
  3970. if (ins_nr > 0) {
  3971. u64 last_extent = 0;
  3972. ret = copy_items(trans, inode, dst_path, path,
  3973. &last_extent, start_slot,
  3974. ins_nr, 1, 0);
  3975. /* can't be 1, extent items aren't processed */
  3976. ASSERT(ret <= 0);
  3977. if (ret < 0)
  3978. return ret;
  3979. ins_nr = 0;
  3980. }
  3981. ret = btrfs_next_leaf(root, path);
  3982. if (ret < 0)
  3983. return ret;
  3984. else if (ret > 0)
  3985. break;
  3986. continue;
  3987. }
  3988. btrfs_item_key_to_cpu(leaf, &key, slot);
  3989. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
  3990. break;
  3991. if (ins_nr == 0)
  3992. start_slot = slot;
  3993. ins_nr++;
  3994. path->slots[0]++;
  3995. cond_resched();
  3996. }
  3997. if (ins_nr > 0) {
  3998. u64 last_extent = 0;
  3999. ret = copy_items(trans, inode, dst_path, path,
  4000. &last_extent, start_slot,
  4001. ins_nr, 1, 0);
  4002. /* can't be 1, extent items aren't processed */
  4003. ASSERT(ret <= 0);
  4004. if (ret < 0)
  4005. return ret;
  4006. }
  4007. return 0;
  4008. }
  4009. /*
  4010. * If the no holes feature is enabled we need to make sure any hole between the
  4011. * last extent and the i_size of our inode is explicitly marked in the log. This
  4012. * is to make sure that doing something like:
  4013. *
  4014. * 1) create file with 128Kb of data
  4015. * 2) truncate file to 64Kb
  4016. * 3) truncate file to 256Kb
  4017. * 4) fsync file
  4018. * 5) <crash/power failure>
  4019. * 6) mount fs and trigger log replay
  4020. *
  4021. * Will give us a file with a size of 256Kb, the first 64Kb of data match what
  4022. * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
  4023. * file correspond to a hole. The presence of explicit holes in a log tree is
  4024. * what guarantees that log replay will remove/adjust file extent items in the
  4025. * fs/subvol tree.
  4026. *
  4027. * Here we do not need to care about holes between extents, that is already done
  4028. * by copy_items(). We also only need to do this in the full sync path, where we
  4029. * lookup for extents from the fs/subvol tree only. In the fast path case, we
  4030. * lookup the list of modified extent maps and if any represents a hole, we
  4031. * insert a corresponding extent representing a hole in the log tree.
  4032. */
  4033. static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
  4034. struct btrfs_root *root,
  4035. struct btrfs_inode *inode,
  4036. struct btrfs_path *path)
  4037. {
  4038. struct btrfs_fs_info *fs_info = root->fs_info;
  4039. int ret;
  4040. struct btrfs_key key;
  4041. u64 hole_start;
  4042. u64 hole_size;
  4043. struct extent_buffer *leaf;
  4044. struct btrfs_root *log = root->log_root;
  4045. const u64 ino = btrfs_ino(inode);
  4046. const u64 i_size = i_size_read(&inode->vfs_inode);
  4047. if (!btrfs_fs_incompat(fs_info, NO_HOLES))
  4048. return 0;
  4049. key.objectid = ino;
  4050. key.type = BTRFS_EXTENT_DATA_KEY;
  4051. key.offset = (u64)-1;
  4052. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4053. ASSERT(ret != 0);
  4054. if (ret < 0)
  4055. return ret;
  4056. ASSERT(path->slots[0] > 0);
  4057. path->slots[0]--;
  4058. leaf = path->nodes[0];
  4059. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4060. if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
  4061. /* inode does not have any extents */
  4062. hole_start = 0;
  4063. hole_size = i_size;
  4064. } else {
  4065. struct btrfs_file_extent_item *extent;
  4066. u64 len;
  4067. /*
  4068. * If there's an extent beyond i_size, an explicit hole was
  4069. * already inserted by copy_items().
  4070. */
  4071. if (key.offset >= i_size)
  4072. return 0;
  4073. extent = btrfs_item_ptr(leaf, path->slots[0],
  4074. struct btrfs_file_extent_item);
  4075. if (btrfs_file_extent_type(leaf, extent) ==
  4076. BTRFS_FILE_EXTENT_INLINE) {
  4077. len = btrfs_file_extent_inline_len(leaf,
  4078. path->slots[0],
  4079. extent);
  4080. ASSERT(len == i_size ||
  4081. (len == fs_info->sectorsize &&
  4082. btrfs_file_extent_compression(leaf, extent) !=
  4083. BTRFS_COMPRESS_NONE));
  4084. return 0;
  4085. }
  4086. len = btrfs_file_extent_num_bytes(leaf, extent);
  4087. /* Last extent goes beyond i_size, no need to log a hole. */
  4088. if (key.offset + len > i_size)
  4089. return 0;
  4090. hole_start = key.offset + len;
  4091. hole_size = i_size - hole_start;
  4092. }
  4093. btrfs_release_path(path);
  4094. /* Last extent ends at i_size. */
  4095. if (hole_size == 0)
  4096. return 0;
  4097. hole_size = ALIGN(hole_size, fs_info->sectorsize);
  4098. ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
  4099. hole_size, 0, hole_size, 0, 0, 0);
  4100. return ret;
  4101. }
  4102. /*
  4103. * When we are logging a new inode X, check if it doesn't have a reference that
  4104. * matches the reference from some other inode Y created in a past transaction
  4105. * and that was renamed in the current transaction. If we don't do this, then at
  4106. * log replay time we can lose inode Y (and all its files if it's a directory):
  4107. *
  4108. * mkdir /mnt/x
  4109. * echo "hello world" > /mnt/x/foobar
  4110. * sync
  4111. * mv /mnt/x /mnt/y
  4112. * mkdir /mnt/x # or touch /mnt/x
  4113. * xfs_io -c fsync /mnt/x
  4114. * <power fail>
  4115. * mount fs, trigger log replay
  4116. *
  4117. * After the log replay procedure, we would lose the first directory and all its
  4118. * files (file foobar).
  4119. * For the case where inode Y is not a directory we simply end up losing it:
  4120. *
  4121. * echo "123" > /mnt/foo
  4122. * sync
  4123. * mv /mnt/foo /mnt/bar
  4124. * echo "abc" > /mnt/foo
  4125. * xfs_io -c fsync /mnt/foo
  4126. * <power fail>
  4127. *
  4128. * We also need this for cases where a snapshot entry is replaced by some other
  4129. * entry (file or directory) otherwise we end up with an unreplayable log due to
  4130. * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
  4131. * if it were a regular entry:
  4132. *
  4133. * mkdir /mnt/x
  4134. * btrfs subvolume snapshot /mnt /mnt/x/snap
  4135. * btrfs subvolume delete /mnt/x/snap
  4136. * rmdir /mnt/x
  4137. * mkdir /mnt/x
  4138. * fsync /mnt/x or fsync some new file inside it
  4139. * <power fail>
  4140. *
  4141. * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
  4142. * the same transaction.
  4143. */
  4144. static int btrfs_check_ref_name_override(struct extent_buffer *eb,
  4145. const int slot,
  4146. const struct btrfs_key *key,
  4147. struct btrfs_inode *inode,
  4148. u64 *other_ino)
  4149. {
  4150. int ret;
  4151. struct btrfs_path *search_path;
  4152. char *name = NULL;
  4153. u32 name_len = 0;
  4154. u32 item_size = btrfs_item_size_nr(eb, slot);
  4155. u32 cur_offset = 0;
  4156. unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
  4157. search_path = btrfs_alloc_path();
  4158. if (!search_path)
  4159. return -ENOMEM;
  4160. search_path->search_commit_root = 1;
  4161. search_path->skip_locking = 1;
  4162. while (cur_offset < item_size) {
  4163. u64 parent;
  4164. u32 this_name_len;
  4165. u32 this_len;
  4166. unsigned long name_ptr;
  4167. struct btrfs_dir_item *di;
  4168. if (key->type == BTRFS_INODE_REF_KEY) {
  4169. struct btrfs_inode_ref *iref;
  4170. iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  4171. parent = key->offset;
  4172. this_name_len = btrfs_inode_ref_name_len(eb, iref);
  4173. name_ptr = (unsigned long)(iref + 1);
  4174. this_len = sizeof(*iref) + this_name_len;
  4175. } else {
  4176. struct btrfs_inode_extref *extref;
  4177. extref = (struct btrfs_inode_extref *)(ptr +
  4178. cur_offset);
  4179. parent = btrfs_inode_extref_parent(eb, extref);
  4180. this_name_len = btrfs_inode_extref_name_len(eb, extref);
  4181. name_ptr = (unsigned long)&extref->name;
  4182. this_len = sizeof(*extref) + this_name_len;
  4183. }
  4184. if (this_name_len > name_len) {
  4185. char *new_name;
  4186. new_name = krealloc(name, this_name_len, GFP_NOFS);
  4187. if (!new_name) {
  4188. ret = -ENOMEM;
  4189. goto out;
  4190. }
  4191. name_len = this_name_len;
  4192. name = new_name;
  4193. }
  4194. read_extent_buffer(eb, name, name_ptr, this_name_len);
  4195. di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
  4196. parent, name, this_name_len, 0);
  4197. if (di && !IS_ERR(di)) {
  4198. struct btrfs_key di_key;
  4199. btrfs_dir_item_key_to_cpu(search_path->nodes[0],
  4200. di, &di_key);
  4201. if (di_key.type == BTRFS_INODE_ITEM_KEY) {
  4202. ret = 1;
  4203. *other_ino = di_key.objectid;
  4204. } else {
  4205. ret = -EAGAIN;
  4206. }
  4207. goto out;
  4208. } else if (IS_ERR(di)) {
  4209. ret = PTR_ERR(di);
  4210. goto out;
  4211. }
  4212. btrfs_release_path(search_path);
  4213. cur_offset += this_len;
  4214. }
  4215. ret = 0;
  4216. out:
  4217. btrfs_free_path(search_path);
  4218. kfree(name);
  4219. return ret;
  4220. }
  4221. /* log a single inode in the tree log.
  4222. * At least one parent directory for this inode must exist in the tree
  4223. * or be logged already.
  4224. *
  4225. * Any items from this inode changed by the current transaction are copied
  4226. * to the log tree. An extra reference is taken on any extents in this
  4227. * file, allowing us to avoid a whole pile of corner cases around logging
  4228. * blocks that have been removed from the tree.
  4229. *
  4230. * See LOG_INODE_ALL and related defines for a description of what inode_only
  4231. * does.
  4232. *
  4233. * This handles both files and directories.
  4234. */
  4235. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  4236. struct btrfs_root *root, struct btrfs_inode *inode,
  4237. int inode_only,
  4238. const loff_t start,
  4239. const loff_t end,
  4240. struct btrfs_log_ctx *ctx)
  4241. {
  4242. struct btrfs_fs_info *fs_info = root->fs_info;
  4243. struct btrfs_path *path;
  4244. struct btrfs_path *dst_path;
  4245. struct btrfs_key min_key;
  4246. struct btrfs_key max_key;
  4247. struct btrfs_root *log = root->log_root;
  4248. LIST_HEAD(logged_list);
  4249. u64 last_extent = 0;
  4250. int err = 0;
  4251. int ret;
  4252. int nritems;
  4253. int ins_start_slot = 0;
  4254. int ins_nr;
  4255. bool fast_search = false;
  4256. u64 ino = btrfs_ino(inode);
  4257. struct extent_map_tree *em_tree = &inode->extent_tree;
  4258. u64 logged_isize = 0;
  4259. bool need_log_inode_item = true;
  4260. path = btrfs_alloc_path();
  4261. if (!path)
  4262. return -ENOMEM;
  4263. dst_path = btrfs_alloc_path();
  4264. if (!dst_path) {
  4265. btrfs_free_path(path);
  4266. return -ENOMEM;
  4267. }
  4268. min_key.objectid = ino;
  4269. min_key.type = BTRFS_INODE_ITEM_KEY;
  4270. min_key.offset = 0;
  4271. max_key.objectid = ino;
  4272. /* today the code can only do partial logging of directories */
  4273. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  4274. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4275. &inode->runtime_flags) &&
  4276. inode_only >= LOG_INODE_EXISTS))
  4277. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4278. else
  4279. max_key.type = (u8)-1;
  4280. max_key.offset = (u64)-1;
  4281. /*
  4282. * Only run delayed items if we are a dir or a new file.
  4283. * Otherwise commit the delayed inode only, which is needed in
  4284. * order for the log replay code to mark inodes for link count
  4285. * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
  4286. */
  4287. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  4288. inode->generation > fs_info->last_trans_committed)
  4289. ret = btrfs_commit_inode_delayed_items(trans, inode);
  4290. else
  4291. ret = btrfs_commit_inode_delayed_inode(inode);
  4292. if (ret) {
  4293. btrfs_free_path(path);
  4294. btrfs_free_path(dst_path);
  4295. return ret;
  4296. }
  4297. if (inode_only == LOG_OTHER_INODE) {
  4298. inode_only = LOG_INODE_EXISTS;
  4299. mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
  4300. } else {
  4301. mutex_lock(&inode->log_mutex);
  4302. }
  4303. /*
  4304. * a brute force approach to making sure we get the most uptodate
  4305. * copies of everything.
  4306. */
  4307. if (S_ISDIR(inode->vfs_inode.i_mode)) {
  4308. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  4309. if (inode_only == LOG_INODE_EXISTS)
  4310. max_key_type = BTRFS_XATTR_ITEM_KEY;
  4311. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  4312. } else {
  4313. if (inode_only == LOG_INODE_EXISTS) {
  4314. /*
  4315. * Make sure the new inode item we write to the log has
  4316. * the same isize as the current one (if it exists).
  4317. * This is necessary to prevent data loss after log
  4318. * replay, and also to prevent doing a wrong expanding
  4319. * truncate - for e.g. create file, write 4K into offset
  4320. * 0, fsync, write 4K into offset 4096, add hard link,
  4321. * fsync some other file (to sync log), power fail - if
  4322. * we use the inode's current i_size, after log replay
  4323. * we get a 8Kb file, with the last 4Kb extent as a hole
  4324. * (zeroes), as if an expanding truncate happened,
  4325. * instead of getting a file of 4Kb only.
  4326. */
  4327. err = logged_inode_size(log, inode, path, &logged_isize);
  4328. if (err)
  4329. goto out_unlock;
  4330. }
  4331. if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4332. &inode->runtime_flags)) {
  4333. if (inode_only == LOG_INODE_EXISTS) {
  4334. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4335. ret = drop_objectid_items(trans, log, path, ino,
  4336. max_key.type);
  4337. } else {
  4338. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4339. &inode->runtime_flags);
  4340. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4341. &inode->runtime_flags);
  4342. while(1) {
  4343. ret = btrfs_truncate_inode_items(trans,
  4344. log, &inode->vfs_inode, 0, 0);
  4345. if (ret != -EAGAIN)
  4346. break;
  4347. }
  4348. }
  4349. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4350. &inode->runtime_flags) ||
  4351. inode_only == LOG_INODE_EXISTS) {
  4352. if (inode_only == LOG_INODE_ALL)
  4353. fast_search = true;
  4354. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4355. ret = drop_objectid_items(trans, log, path, ino,
  4356. max_key.type);
  4357. } else {
  4358. if (inode_only == LOG_INODE_ALL)
  4359. fast_search = true;
  4360. goto log_extents;
  4361. }
  4362. }
  4363. if (ret) {
  4364. err = ret;
  4365. goto out_unlock;
  4366. }
  4367. while (1) {
  4368. ins_nr = 0;
  4369. ret = btrfs_search_forward(root, &min_key,
  4370. path, trans->transid);
  4371. if (ret < 0) {
  4372. err = ret;
  4373. goto out_unlock;
  4374. }
  4375. if (ret != 0)
  4376. break;
  4377. again:
  4378. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  4379. if (min_key.objectid != ino)
  4380. break;
  4381. if (min_key.type > max_key.type)
  4382. break;
  4383. if (min_key.type == BTRFS_INODE_ITEM_KEY)
  4384. need_log_inode_item = false;
  4385. if ((min_key.type == BTRFS_INODE_REF_KEY ||
  4386. min_key.type == BTRFS_INODE_EXTREF_KEY) &&
  4387. inode->generation == trans->transid) {
  4388. u64 other_ino = 0;
  4389. ret = btrfs_check_ref_name_override(path->nodes[0],
  4390. path->slots[0], &min_key, inode,
  4391. &other_ino);
  4392. if (ret < 0) {
  4393. err = ret;
  4394. goto out_unlock;
  4395. } else if (ret > 0 && ctx &&
  4396. other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
  4397. struct btrfs_key inode_key;
  4398. struct inode *other_inode;
  4399. if (ins_nr > 0) {
  4400. ins_nr++;
  4401. } else {
  4402. ins_nr = 1;
  4403. ins_start_slot = path->slots[0];
  4404. }
  4405. ret = copy_items(trans, inode, dst_path, path,
  4406. &last_extent, ins_start_slot,
  4407. ins_nr, inode_only,
  4408. logged_isize);
  4409. if (ret < 0) {
  4410. err = ret;
  4411. goto out_unlock;
  4412. }
  4413. ins_nr = 0;
  4414. btrfs_release_path(path);
  4415. inode_key.objectid = other_ino;
  4416. inode_key.type = BTRFS_INODE_ITEM_KEY;
  4417. inode_key.offset = 0;
  4418. other_inode = btrfs_iget(fs_info->sb,
  4419. &inode_key, root,
  4420. NULL);
  4421. /*
  4422. * If the other inode that had a conflicting dir
  4423. * entry was deleted in the current transaction,
  4424. * we don't need to do more work nor fallback to
  4425. * a transaction commit.
  4426. */
  4427. if (IS_ERR(other_inode) &&
  4428. PTR_ERR(other_inode) == -ENOENT) {
  4429. goto next_key;
  4430. } else if (IS_ERR(other_inode)) {
  4431. err = PTR_ERR(other_inode);
  4432. goto out_unlock;
  4433. }
  4434. /*
  4435. * We are safe logging the other inode without
  4436. * acquiring its i_mutex as long as we log with
  4437. * the LOG_INODE_EXISTS mode. We're safe against
  4438. * concurrent renames of the other inode as well
  4439. * because during a rename we pin the log and
  4440. * update the log with the new name before we
  4441. * unpin it.
  4442. */
  4443. err = btrfs_log_inode(trans, root,
  4444. BTRFS_I(other_inode),
  4445. LOG_OTHER_INODE, 0, LLONG_MAX,
  4446. ctx);
  4447. iput(other_inode);
  4448. if (err)
  4449. goto out_unlock;
  4450. else
  4451. goto next_key;
  4452. }
  4453. }
  4454. /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
  4455. if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
  4456. if (ins_nr == 0)
  4457. goto next_slot;
  4458. ret = copy_items(trans, inode, dst_path, path,
  4459. &last_extent, ins_start_slot,
  4460. ins_nr, inode_only, logged_isize);
  4461. if (ret < 0) {
  4462. err = ret;
  4463. goto out_unlock;
  4464. }
  4465. ins_nr = 0;
  4466. if (ret) {
  4467. btrfs_release_path(path);
  4468. continue;
  4469. }
  4470. goto next_slot;
  4471. }
  4472. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  4473. ins_nr++;
  4474. goto next_slot;
  4475. } else if (!ins_nr) {
  4476. ins_start_slot = path->slots[0];
  4477. ins_nr = 1;
  4478. goto next_slot;
  4479. }
  4480. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4481. ins_start_slot, ins_nr, inode_only,
  4482. logged_isize);
  4483. if (ret < 0) {
  4484. err = ret;
  4485. goto out_unlock;
  4486. }
  4487. if (ret) {
  4488. ins_nr = 0;
  4489. btrfs_release_path(path);
  4490. continue;
  4491. }
  4492. ins_nr = 1;
  4493. ins_start_slot = path->slots[0];
  4494. next_slot:
  4495. nritems = btrfs_header_nritems(path->nodes[0]);
  4496. path->slots[0]++;
  4497. if (path->slots[0] < nritems) {
  4498. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  4499. path->slots[0]);
  4500. goto again;
  4501. }
  4502. if (ins_nr) {
  4503. ret = copy_items(trans, inode, dst_path, path,
  4504. &last_extent, ins_start_slot,
  4505. ins_nr, inode_only, logged_isize);
  4506. if (ret < 0) {
  4507. err = ret;
  4508. goto out_unlock;
  4509. }
  4510. ret = 0;
  4511. ins_nr = 0;
  4512. }
  4513. btrfs_release_path(path);
  4514. next_key:
  4515. if (min_key.offset < (u64)-1) {
  4516. min_key.offset++;
  4517. } else if (min_key.type < max_key.type) {
  4518. min_key.type++;
  4519. min_key.offset = 0;
  4520. } else {
  4521. break;
  4522. }
  4523. }
  4524. if (ins_nr) {
  4525. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4526. ins_start_slot, ins_nr, inode_only,
  4527. logged_isize);
  4528. if (ret < 0) {
  4529. err = ret;
  4530. goto out_unlock;
  4531. }
  4532. ret = 0;
  4533. ins_nr = 0;
  4534. }
  4535. btrfs_release_path(path);
  4536. btrfs_release_path(dst_path);
  4537. err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
  4538. if (err)
  4539. goto out_unlock;
  4540. if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
  4541. btrfs_release_path(path);
  4542. btrfs_release_path(dst_path);
  4543. err = btrfs_log_trailing_hole(trans, root, inode, path);
  4544. if (err)
  4545. goto out_unlock;
  4546. }
  4547. log_extents:
  4548. btrfs_release_path(path);
  4549. btrfs_release_path(dst_path);
  4550. if (need_log_inode_item) {
  4551. err = log_inode_item(trans, log, dst_path, inode);
  4552. if (err)
  4553. goto out_unlock;
  4554. }
  4555. if (fast_search) {
  4556. ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
  4557. &logged_list, ctx, start, end);
  4558. if (ret) {
  4559. err = ret;
  4560. goto out_unlock;
  4561. }
  4562. } else if (inode_only == LOG_INODE_ALL) {
  4563. struct extent_map *em, *n;
  4564. write_lock(&em_tree->lock);
  4565. /*
  4566. * We can't just remove every em if we're called for a ranged
  4567. * fsync - that is, one that doesn't cover the whole possible
  4568. * file range (0 to LLONG_MAX). This is because we can have
  4569. * em's that fall outside the range we're logging and therefore
  4570. * their ordered operations haven't completed yet
  4571. * (btrfs_finish_ordered_io() not invoked yet). This means we
  4572. * didn't get their respective file extent item in the fs/subvol
  4573. * tree yet, and need to let the next fast fsync (one which
  4574. * consults the list of modified extent maps) find the em so
  4575. * that it logs a matching file extent item and waits for the
  4576. * respective ordered operation to complete (if it's still
  4577. * running).
  4578. *
  4579. * Removing every em outside the range we're logging would make
  4580. * the next fast fsync not log their matching file extent items,
  4581. * therefore making us lose data after a log replay.
  4582. */
  4583. list_for_each_entry_safe(em, n, &em_tree->modified_extents,
  4584. list) {
  4585. const u64 mod_end = em->mod_start + em->mod_len - 1;
  4586. if (em->mod_start >= start && mod_end <= end)
  4587. list_del_init(&em->list);
  4588. }
  4589. write_unlock(&em_tree->lock);
  4590. }
  4591. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
  4592. ret = log_directory_changes(trans, root, inode, path, dst_path,
  4593. ctx);
  4594. if (ret) {
  4595. err = ret;
  4596. goto out_unlock;
  4597. }
  4598. }
  4599. spin_lock(&inode->lock);
  4600. inode->logged_trans = trans->transid;
  4601. inode->last_log_commit = inode->last_sub_trans;
  4602. spin_unlock(&inode->lock);
  4603. out_unlock:
  4604. if (unlikely(err))
  4605. btrfs_put_logged_extents(&logged_list);
  4606. else
  4607. btrfs_submit_logged_extents(&logged_list, log);
  4608. mutex_unlock(&inode->log_mutex);
  4609. btrfs_free_path(path);
  4610. btrfs_free_path(dst_path);
  4611. return err;
  4612. }
  4613. /*
  4614. * Check if we must fallback to a transaction commit when logging an inode.
  4615. * This must be called after logging the inode and is used only in the context
  4616. * when fsyncing an inode requires the need to log some other inode - in which
  4617. * case we can't lock the i_mutex of each other inode we need to log as that
  4618. * can lead to deadlocks with concurrent fsync against other inodes (as we can
  4619. * log inodes up or down in the hierarchy) or rename operations for example. So
  4620. * we take the log_mutex of the inode after we have logged it and then check for
  4621. * its last_unlink_trans value - this is safe because any task setting
  4622. * last_unlink_trans must take the log_mutex and it must do this before it does
  4623. * the actual unlink operation, so if we do this check before a concurrent task
  4624. * sets last_unlink_trans it means we've logged a consistent version/state of
  4625. * all the inode items, otherwise we are not sure and must do a transaction
  4626. * commit (the concurrent task might have only updated last_unlink_trans before
  4627. * we logged the inode or it might have also done the unlink).
  4628. */
  4629. static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
  4630. struct btrfs_inode *inode)
  4631. {
  4632. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  4633. bool ret = false;
  4634. mutex_lock(&inode->log_mutex);
  4635. if (inode->last_unlink_trans > fs_info->last_trans_committed) {
  4636. /*
  4637. * Make sure any commits to the log are forced to be full
  4638. * commits.
  4639. */
  4640. btrfs_set_log_full_commit(fs_info, trans);
  4641. ret = true;
  4642. }
  4643. mutex_unlock(&inode->log_mutex);
  4644. return ret;
  4645. }
  4646. /*
  4647. * follow the dentry parent pointers up the chain and see if any
  4648. * of the directories in it require a full commit before they can
  4649. * be logged. Returns zero if nothing special needs to be done or 1 if
  4650. * a full commit is required.
  4651. */
  4652. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  4653. struct btrfs_inode *inode,
  4654. struct dentry *parent,
  4655. struct super_block *sb,
  4656. u64 last_committed)
  4657. {
  4658. int ret = 0;
  4659. struct dentry *old_parent = NULL;
  4660. struct btrfs_inode *orig_inode = inode;
  4661. /*
  4662. * for regular files, if its inode is already on disk, we don't
  4663. * have to worry about the parents at all. This is because
  4664. * we can use the last_unlink_trans field to record renames
  4665. * and other fun in this file.
  4666. */
  4667. if (S_ISREG(inode->vfs_inode.i_mode) &&
  4668. inode->generation <= last_committed &&
  4669. inode->last_unlink_trans <= last_committed)
  4670. goto out;
  4671. if (!S_ISDIR(inode->vfs_inode.i_mode)) {
  4672. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4673. goto out;
  4674. inode = BTRFS_I(d_inode(parent));
  4675. }
  4676. while (1) {
  4677. /*
  4678. * If we are logging a directory then we start with our inode,
  4679. * not our parent's inode, so we need to skip setting the
  4680. * logged_trans so that further down in the log code we don't
  4681. * think this inode has already been logged.
  4682. */
  4683. if (inode != orig_inode)
  4684. inode->logged_trans = trans->transid;
  4685. smp_mb();
  4686. if (btrfs_must_commit_transaction(trans, inode)) {
  4687. ret = 1;
  4688. break;
  4689. }
  4690. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4691. break;
  4692. if (IS_ROOT(parent)) {
  4693. inode = BTRFS_I(d_inode(parent));
  4694. if (btrfs_must_commit_transaction(trans, inode))
  4695. ret = 1;
  4696. break;
  4697. }
  4698. parent = dget_parent(parent);
  4699. dput(old_parent);
  4700. old_parent = parent;
  4701. inode = BTRFS_I(d_inode(parent));
  4702. }
  4703. dput(old_parent);
  4704. out:
  4705. return ret;
  4706. }
  4707. struct btrfs_dir_list {
  4708. u64 ino;
  4709. struct list_head list;
  4710. };
  4711. /*
  4712. * Log the inodes of the new dentries of a directory. See log_dir_items() for
  4713. * details about the why it is needed.
  4714. * This is a recursive operation - if an existing dentry corresponds to a
  4715. * directory, that directory's new entries are logged too (same behaviour as
  4716. * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
  4717. * the dentries point to we do not lock their i_mutex, otherwise lockdep
  4718. * complains about the following circular lock dependency / possible deadlock:
  4719. *
  4720. * CPU0 CPU1
  4721. * ---- ----
  4722. * lock(&type->i_mutex_dir_key#3/2);
  4723. * lock(sb_internal#2);
  4724. * lock(&type->i_mutex_dir_key#3/2);
  4725. * lock(&sb->s_type->i_mutex_key#14);
  4726. *
  4727. * Where sb_internal is the lock (a counter that works as a lock) acquired by
  4728. * sb_start_intwrite() in btrfs_start_transaction().
  4729. * Not locking i_mutex of the inodes is still safe because:
  4730. *
  4731. * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
  4732. * that while logging the inode new references (names) are added or removed
  4733. * from the inode, leaving the logged inode item with a link count that does
  4734. * not match the number of logged inode reference items. This is fine because
  4735. * at log replay time we compute the real number of links and correct the
  4736. * link count in the inode item (see replay_one_buffer() and
  4737. * link_to_fixup_dir());
  4738. *
  4739. * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
  4740. * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
  4741. * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
  4742. * has a size that doesn't match the sum of the lengths of all the logged
  4743. * names. This does not result in a problem because if a dir_item key is
  4744. * logged but its matching dir_index key is not logged, at log replay time we
  4745. * don't use it to replay the respective name (see replay_one_name()). On the
  4746. * other hand if only the dir_index key ends up being logged, the respective
  4747. * name is added to the fs/subvol tree with both the dir_item and dir_index
  4748. * keys created (see replay_one_name()).
  4749. * The directory's inode item with a wrong i_size is not a problem as well,
  4750. * since we don't use it at log replay time to set the i_size in the inode
  4751. * item of the fs/subvol tree (see overwrite_item()).
  4752. */
  4753. static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
  4754. struct btrfs_root *root,
  4755. struct btrfs_inode *start_inode,
  4756. struct btrfs_log_ctx *ctx)
  4757. {
  4758. struct btrfs_fs_info *fs_info = root->fs_info;
  4759. struct btrfs_root *log = root->log_root;
  4760. struct btrfs_path *path;
  4761. LIST_HEAD(dir_list);
  4762. struct btrfs_dir_list *dir_elem;
  4763. int ret = 0;
  4764. path = btrfs_alloc_path();
  4765. if (!path)
  4766. return -ENOMEM;
  4767. dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
  4768. if (!dir_elem) {
  4769. btrfs_free_path(path);
  4770. return -ENOMEM;
  4771. }
  4772. dir_elem->ino = btrfs_ino(start_inode);
  4773. list_add_tail(&dir_elem->list, &dir_list);
  4774. while (!list_empty(&dir_list)) {
  4775. struct extent_buffer *leaf;
  4776. struct btrfs_key min_key;
  4777. int nritems;
  4778. int i;
  4779. dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
  4780. list);
  4781. if (ret)
  4782. goto next_dir_inode;
  4783. min_key.objectid = dir_elem->ino;
  4784. min_key.type = BTRFS_DIR_ITEM_KEY;
  4785. min_key.offset = 0;
  4786. again:
  4787. btrfs_release_path(path);
  4788. ret = btrfs_search_forward(log, &min_key, path, trans->transid);
  4789. if (ret < 0) {
  4790. goto next_dir_inode;
  4791. } else if (ret > 0) {
  4792. ret = 0;
  4793. goto next_dir_inode;
  4794. }
  4795. process_leaf:
  4796. leaf = path->nodes[0];
  4797. nritems = btrfs_header_nritems(leaf);
  4798. for (i = path->slots[0]; i < nritems; i++) {
  4799. struct btrfs_dir_item *di;
  4800. struct btrfs_key di_key;
  4801. struct inode *di_inode;
  4802. struct btrfs_dir_list *new_dir_elem;
  4803. int log_mode = LOG_INODE_EXISTS;
  4804. int type;
  4805. btrfs_item_key_to_cpu(leaf, &min_key, i);
  4806. if (min_key.objectid != dir_elem->ino ||
  4807. min_key.type != BTRFS_DIR_ITEM_KEY)
  4808. goto next_dir_inode;
  4809. di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
  4810. type = btrfs_dir_type(leaf, di);
  4811. if (btrfs_dir_transid(leaf, di) < trans->transid &&
  4812. type != BTRFS_FT_DIR)
  4813. continue;
  4814. btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
  4815. if (di_key.type == BTRFS_ROOT_ITEM_KEY)
  4816. continue;
  4817. btrfs_release_path(path);
  4818. di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
  4819. if (IS_ERR(di_inode)) {
  4820. ret = PTR_ERR(di_inode);
  4821. goto next_dir_inode;
  4822. }
  4823. if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
  4824. iput(di_inode);
  4825. break;
  4826. }
  4827. ctx->log_new_dentries = false;
  4828. if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
  4829. log_mode = LOG_INODE_ALL;
  4830. ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
  4831. log_mode, 0, LLONG_MAX, ctx);
  4832. if (!ret &&
  4833. btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
  4834. ret = 1;
  4835. iput(di_inode);
  4836. if (ret)
  4837. goto next_dir_inode;
  4838. if (ctx->log_new_dentries) {
  4839. new_dir_elem = kmalloc(sizeof(*new_dir_elem),
  4840. GFP_NOFS);
  4841. if (!new_dir_elem) {
  4842. ret = -ENOMEM;
  4843. goto next_dir_inode;
  4844. }
  4845. new_dir_elem->ino = di_key.objectid;
  4846. list_add_tail(&new_dir_elem->list, &dir_list);
  4847. }
  4848. break;
  4849. }
  4850. if (i == nritems) {
  4851. ret = btrfs_next_leaf(log, path);
  4852. if (ret < 0) {
  4853. goto next_dir_inode;
  4854. } else if (ret > 0) {
  4855. ret = 0;
  4856. goto next_dir_inode;
  4857. }
  4858. goto process_leaf;
  4859. }
  4860. if (min_key.offset < (u64)-1) {
  4861. min_key.offset++;
  4862. goto again;
  4863. }
  4864. next_dir_inode:
  4865. list_del(&dir_elem->list);
  4866. kfree(dir_elem);
  4867. }
  4868. btrfs_free_path(path);
  4869. return ret;
  4870. }
  4871. static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
  4872. struct btrfs_inode *inode,
  4873. struct btrfs_log_ctx *ctx)
  4874. {
  4875. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  4876. int ret;
  4877. struct btrfs_path *path;
  4878. struct btrfs_key key;
  4879. struct btrfs_root *root = inode->root;
  4880. const u64 ino = btrfs_ino(inode);
  4881. path = btrfs_alloc_path();
  4882. if (!path)
  4883. return -ENOMEM;
  4884. path->skip_locking = 1;
  4885. path->search_commit_root = 1;
  4886. key.objectid = ino;
  4887. key.type = BTRFS_INODE_REF_KEY;
  4888. key.offset = 0;
  4889. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4890. if (ret < 0)
  4891. goto out;
  4892. while (true) {
  4893. struct extent_buffer *leaf = path->nodes[0];
  4894. int slot = path->slots[0];
  4895. u32 cur_offset = 0;
  4896. u32 item_size;
  4897. unsigned long ptr;
  4898. if (slot >= btrfs_header_nritems(leaf)) {
  4899. ret = btrfs_next_leaf(root, path);
  4900. if (ret < 0)
  4901. goto out;
  4902. else if (ret > 0)
  4903. break;
  4904. continue;
  4905. }
  4906. btrfs_item_key_to_cpu(leaf, &key, slot);
  4907. /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
  4908. if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
  4909. break;
  4910. item_size = btrfs_item_size_nr(leaf, slot);
  4911. ptr = btrfs_item_ptr_offset(leaf, slot);
  4912. while (cur_offset < item_size) {
  4913. struct btrfs_key inode_key;
  4914. struct inode *dir_inode;
  4915. inode_key.type = BTRFS_INODE_ITEM_KEY;
  4916. inode_key.offset = 0;
  4917. if (key.type == BTRFS_INODE_EXTREF_KEY) {
  4918. struct btrfs_inode_extref *extref;
  4919. extref = (struct btrfs_inode_extref *)
  4920. (ptr + cur_offset);
  4921. inode_key.objectid = btrfs_inode_extref_parent(
  4922. leaf, extref);
  4923. cur_offset += sizeof(*extref);
  4924. cur_offset += btrfs_inode_extref_name_len(leaf,
  4925. extref);
  4926. } else {
  4927. inode_key.objectid = key.offset;
  4928. cur_offset = item_size;
  4929. }
  4930. dir_inode = btrfs_iget(fs_info->sb, &inode_key,
  4931. root, NULL);
  4932. /* If parent inode was deleted, skip it. */
  4933. if (IS_ERR(dir_inode))
  4934. continue;
  4935. if (ctx)
  4936. ctx->log_new_dentries = false;
  4937. ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
  4938. LOG_INODE_ALL, 0, LLONG_MAX, ctx);
  4939. if (!ret &&
  4940. btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
  4941. ret = 1;
  4942. if (!ret && ctx && ctx->log_new_dentries)
  4943. ret = log_new_dir_dentries(trans, root,
  4944. BTRFS_I(dir_inode), ctx);
  4945. iput(dir_inode);
  4946. if (ret)
  4947. goto out;
  4948. }
  4949. path->slots[0]++;
  4950. }
  4951. ret = 0;
  4952. out:
  4953. btrfs_free_path(path);
  4954. return ret;
  4955. }
  4956. /*
  4957. * helper function around btrfs_log_inode to make sure newly created
  4958. * parent directories also end up in the log. A minimal inode and backref
  4959. * only logging is done of any parent directories that are older than
  4960. * the last committed transaction
  4961. */
  4962. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  4963. struct btrfs_inode *inode,
  4964. struct dentry *parent,
  4965. const loff_t start,
  4966. const loff_t end,
  4967. int inode_only,
  4968. struct btrfs_log_ctx *ctx)
  4969. {
  4970. struct btrfs_root *root = inode->root;
  4971. struct btrfs_fs_info *fs_info = root->fs_info;
  4972. struct super_block *sb;
  4973. struct dentry *old_parent = NULL;
  4974. int ret = 0;
  4975. u64 last_committed = fs_info->last_trans_committed;
  4976. bool log_dentries = false;
  4977. struct btrfs_inode *orig_inode = inode;
  4978. sb = inode->vfs_inode.i_sb;
  4979. if (btrfs_test_opt(fs_info, NOTREELOG)) {
  4980. ret = 1;
  4981. goto end_no_trans;
  4982. }
  4983. /*
  4984. * The prev transaction commit doesn't complete, we need do
  4985. * full commit by ourselves.
  4986. */
  4987. if (fs_info->last_trans_log_full_commit >
  4988. fs_info->last_trans_committed) {
  4989. ret = 1;
  4990. goto end_no_trans;
  4991. }
  4992. if (btrfs_root_refs(&root->root_item) == 0) {
  4993. ret = 1;
  4994. goto end_no_trans;
  4995. }
  4996. ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
  4997. last_committed);
  4998. if (ret)
  4999. goto end_no_trans;
  5000. if (btrfs_inode_in_log(inode, trans->transid)) {
  5001. ret = BTRFS_NO_LOG_SYNC;
  5002. goto end_no_trans;
  5003. }
  5004. ret = start_log_trans(trans, root, ctx);
  5005. if (ret)
  5006. goto end_no_trans;
  5007. ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
  5008. if (ret)
  5009. goto end_trans;
  5010. /*
  5011. * for regular files, if its inode is already on disk, we don't
  5012. * have to worry about the parents at all. This is because
  5013. * we can use the last_unlink_trans field to record renames
  5014. * and other fun in this file.
  5015. */
  5016. if (S_ISREG(inode->vfs_inode.i_mode) &&
  5017. inode->generation <= last_committed &&
  5018. inode->last_unlink_trans <= last_committed) {
  5019. ret = 0;
  5020. goto end_trans;
  5021. }
  5022. if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
  5023. log_dentries = true;
  5024. /*
  5025. * On unlink we must make sure all our current and old parent directory
  5026. * inodes are fully logged. This is to prevent leaving dangling
  5027. * directory index entries in directories that were our parents but are
  5028. * not anymore. Not doing this results in old parent directory being
  5029. * impossible to delete after log replay (rmdir will always fail with
  5030. * error -ENOTEMPTY).
  5031. *
  5032. * Example 1:
  5033. *
  5034. * mkdir testdir
  5035. * touch testdir/foo
  5036. * ln testdir/foo testdir/bar
  5037. * sync
  5038. * unlink testdir/bar
  5039. * xfs_io -c fsync testdir/foo
  5040. * <power failure>
  5041. * mount fs, triggers log replay
  5042. *
  5043. * If we don't log the parent directory (testdir), after log replay the
  5044. * directory still has an entry pointing to the file inode using the bar
  5045. * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
  5046. * the file inode has a link count of 1.
  5047. *
  5048. * Example 2:
  5049. *
  5050. * mkdir testdir
  5051. * touch foo
  5052. * ln foo testdir/foo2
  5053. * ln foo testdir/foo3
  5054. * sync
  5055. * unlink testdir/foo3
  5056. * xfs_io -c fsync foo
  5057. * <power failure>
  5058. * mount fs, triggers log replay
  5059. *
  5060. * Similar as the first example, after log replay the parent directory
  5061. * testdir still has an entry pointing to the inode file with name foo3
  5062. * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
  5063. * and has a link count of 2.
  5064. */
  5065. if (inode->last_unlink_trans > last_committed) {
  5066. ret = btrfs_log_all_parents(trans, orig_inode, ctx);
  5067. if (ret)
  5068. goto end_trans;
  5069. }
  5070. while (1) {
  5071. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  5072. break;
  5073. inode = BTRFS_I(d_inode(parent));
  5074. if (root != inode->root)
  5075. break;
  5076. if (inode->generation > last_committed) {
  5077. ret = btrfs_log_inode(trans, root, inode,
  5078. LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
  5079. if (ret)
  5080. goto end_trans;
  5081. }
  5082. if (IS_ROOT(parent))
  5083. break;
  5084. parent = dget_parent(parent);
  5085. dput(old_parent);
  5086. old_parent = parent;
  5087. }
  5088. if (log_dentries)
  5089. ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
  5090. else
  5091. ret = 0;
  5092. end_trans:
  5093. dput(old_parent);
  5094. if (ret < 0) {
  5095. btrfs_set_log_full_commit(fs_info, trans);
  5096. ret = 1;
  5097. }
  5098. if (ret)
  5099. btrfs_remove_log_ctx(root, ctx);
  5100. btrfs_end_log_trans(root);
  5101. end_no_trans:
  5102. return ret;
  5103. }
  5104. /*
  5105. * it is not safe to log dentry if the chunk root has added new
  5106. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  5107. * If this returns 1, you must commit the transaction to safely get your
  5108. * data on disk.
  5109. */
  5110. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  5111. struct dentry *dentry,
  5112. const loff_t start,
  5113. const loff_t end,
  5114. struct btrfs_log_ctx *ctx)
  5115. {
  5116. struct dentry *parent = dget_parent(dentry);
  5117. int ret;
  5118. ret = btrfs_log_inode_parent(trans, BTRFS_I(d_inode(dentry)), parent,
  5119. start, end, LOG_INODE_ALL, ctx);
  5120. dput(parent);
  5121. return ret;
  5122. }
  5123. /*
  5124. * should be called during mount to recover any replay any log trees
  5125. * from the FS
  5126. */
  5127. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  5128. {
  5129. int ret;
  5130. struct btrfs_path *path;
  5131. struct btrfs_trans_handle *trans;
  5132. struct btrfs_key key;
  5133. struct btrfs_key found_key;
  5134. struct btrfs_key tmp_key;
  5135. struct btrfs_root *log;
  5136. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  5137. struct walk_control wc = {
  5138. .process_func = process_one_buffer,
  5139. .stage = 0,
  5140. };
  5141. path = btrfs_alloc_path();
  5142. if (!path)
  5143. return -ENOMEM;
  5144. set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5145. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  5146. if (IS_ERR(trans)) {
  5147. ret = PTR_ERR(trans);
  5148. goto error;
  5149. }
  5150. wc.trans = trans;
  5151. wc.pin = 1;
  5152. ret = walk_log_tree(trans, log_root_tree, &wc);
  5153. if (ret) {
  5154. btrfs_handle_fs_error(fs_info, ret,
  5155. "Failed to pin buffers while recovering log root tree.");
  5156. goto error;
  5157. }
  5158. again:
  5159. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  5160. key.offset = (u64)-1;
  5161. key.type = BTRFS_ROOT_ITEM_KEY;
  5162. while (1) {
  5163. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  5164. if (ret < 0) {
  5165. btrfs_handle_fs_error(fs_info, ret,
  5166. "Couldn't find tree log root.");
  5167. goto error;
  5168. }
  5169. if (ret > 0) {
  5170. if (path->slots[0] == 0)
  5171. break;
  5172. path->slots[0]--;
  5173. }
  5174. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  5175. path->slots[0]);
  5176. btrfs_release_path(path);
  5177. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  5178. break;
  5179. log = btrfs_read_fs_root(log_root_tree, &found_key);
  5180. if (IS_ERR(log)) {
  5181. ret = PTR_ERR(log);
  5182. btrfs_handle_fs_error(fs_info, ret,
  5183. "Couldn't read tree log root.");
  5184. goto error;
  5185. }
  5186. tmp_key.objectid = found_key.offset;
  5187. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  5188. tmp_key.offset = (u64)-1;
  5189. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  5190. if (IS_ERR(wc.replay_dest)) {
  5191. ret = PTR_ERR(wc.replay_dest);
  5192. free_extent_buffer(log->node);
  5193. free_extent_buffer(log->commit_root);
  5194. kfree(log);
  5195. btrfs_handle_fs_error(fs_info, ret,
  5196. "Couldn't read target root for tree log recovery.");
  5197. goto error;
  5198. }
  5199. wc.replay_dest->log_root = log;
  5200. btrfs_record_root_in_trans(trans, wc.replay_dest);
  5201. ret = walk_log_tree(trans, log, &wc);
  5202. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5203. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  5204. path);
  5205. }
  5206. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5207. struct btrfs_root *root = wc.replay_dest;
  5208. btrfs_release_path(path);
  5209. /*
  5210. * We have just replayed everything, and the highest
  5211. * objectid of fs roots probably has changed in case
  5212. * some inode_item's got replayed.
  5213. *
  5214. * root->objectid_mutex is not acquired as log replay
  5215. * could only happen during mount.
  5216. */
  5217. ret = btrfs_find_highest_objectid(root,
  5218. &root->highest_objectid);
  5219. }
  5220. key.offset = found_key.offset - 1;
  5221. wc.replay_dest->log_root = NULL;
  5222. free_extent_buffer(log->node);
  5223. free_extent_buffer(log->commit_root);
  5224. kfree(log);
  5225. if (ret)
  5226. goto error;
  5227. if (found_key.offset == 0)
  5228. break;
  5229. }
  5230. btrfs_release_path(path);
  5231. /* step one is to pin it all, step two is to replay just inodes */
  5232. if (wc.pin) {
  5233. wc.pin = 0;
  5234. wc.process_func = replay_one_buffer;
  5235. wc.stage = LOG_WALK_REPLAY_INODES;
  5236. goto again;
  5237. }
  5238. /* step three is to replay everything */
  5239. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  5240. wc.stage++;
  5241. goto again;
  5242. }
  5243. btrfs_free_path(path);
  5244. /* step 4: commit the transaction, which also unpins the blocks */
  5245. ret = btrfs_commit_transaction(trans);
  5246. if (ret)
  5247. return ret;
  5248. free_extent_buffer(log_root_tree->node);
  5249. log_root_tree->log_root = NULL;
  5250. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5251. kfree(log_root_tree);
  5252. return 0;
  5253. error:
  5254. if (wc.trans)
  5255. btrfs_end_transaction(wc.trans);
  5256. btrfs_free_path(path);
  5257. return ret;
  5258. }
  5259. /*
  5260. * there are some corner cases where we want to force a full
  5261. * commit instead of allowing a directory to be logged.
  5262. *
  5263. * They revolve around files there were unlinked from the directory, and
  5264. * this function updates the parent directory so that a full commit is
  5265. * properly done if it is fsync'd later after the unlinks are done.
  5266. *
  5267. * Must be called before the unlink operations (updates to the subvolume tree,
  5268. * inodes, etc) are done.
  5269. */
  5270. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  5271. struct btrfs_inode *dir, struct btrfs_inode *inode,
  5272. int for_rename)
  5273. {
  5274. /*
  5275. * when we're logging a file, if it hasn't been renamed
  5276. * or unlinked, and its inode is fully committed on disk,
  5277. * we don't have to worry about walking up the directory chain
  5278. * to log its parents.
  5279. *
  5280. * So, we use the last_unlink_trans field to put this transid
  5281. * into the file. When the file is logged we check it and
  5282. * don't log the parents if the file is fully on disk.
  5283. */
  5284. mutex_lock(&inode->log_mutex);
  5285. inode->last_unlink_trans = trans->transid;
  5286. mutex_unlock(&inode->log_mutex);
  5287. /*
  5288. * if this directory was already logged any new
  5289. * names for this file/dir will get recorded
  5290. */
  5291. smp_mb();
  5292. if (dir->logged_trans == trans->transid)
  5293. return;
  5294. /*
  5295. * if the inode we're about to unlink was logged,
  5296. * the log will be properly updated for any new names
  5297. */
  5298. if (inode->logged_trans == trans->transid)
  5299. return;
  5300. /*
  5301. * when renaming files across directories, if the directory
  5302. * there we're unlinking from gets fsync'd later on, there's
  5303. * no way to find the destination directory later and fsync it
  5304. * properly. So, we have to be conservative and force commits
  5305. * so the new name gets discovered.
  5306. */
  5307. if (for_rename)
  5308. goto record;
  5309. /* we can safely do the unlink without any special recording */
  5310. return;
  5311. record:
  5312. mutex_lock(&dir->log_mutex);
  5313. dir->last_unlink_trans = trans->transid;
  5314. mutex_unlock(&dir->log_mutex);
  5315. }
  5316. /*
  5317. * Make sure that if someone attempts to fsync the parent directory of a deleted
  5318. * snapshot, it ends up triggering a transaction commit. This is to guarantee
  5319. * that after replaying the log tree of the parent directory's root we will not
  5320. * see the snapshot anymore and at log replay time we will not see any log tree
  5321. * corresponding to the deleted snapshot's root, which could lead to replaying
  5322. * it after replaying the log tree of the parent directory (which would replay
  5323. * the snapshot delete operation).
  5324. *
  5325. * Must be called before the actual snapshot destroy operation (updates to the
  5326. * parent root and tree of tree roots trees, etc) are done.
  5327. */
  5328. void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
  5329. struct btrfs_inode *dir)
  5330. {
  5331. mutex_lock(&dir->log_mutex);
  5332. dir->last_unlink_trans = trans->transid;
  5333. mutex_unlock(&dir->log_mutex);
  5334. }
  5335. /*
  5336. * Call this after adding a new name for a file and it will properly
  5337. * update the log to reflect the new name.
  5338. *
  5339. * It will return zero if all goes well, and it will return 1 if a
  5340. * full transaction commit is required.
  5341. */
  5342. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  5343. struct btrfs_inode *inode, struct btrfs_inode *old_dir,
  5344. struct dentry *parent)
  5345. {
  5346. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  5347. /*
  5348. * this will force the logging code to walk the dentry chain
  5349. * up for the file
  5350. */
  5351. if (!S_ISDIR(inode->vfs_inode.i_mode))
  5352. inode->last_unlink_trans = trans->transid;
  5353. /*
  5354. * if this inode hasn't been logged and directory we're renaming it
  5355. * from hasn't been logged, we don't need to log it
  5356. */
  5357. if (inode->logged_trans <= fs_info->last_trans_committed &&
  5358. (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
  5359. return 0;
  5360. return btrfs_log_inode_parent(trans, inode, parent, 0, LLONG_MAX,
  5361. LOG_INODE_EXISTS, NULL);
  5362. }