tree-log.c 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972
  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. }
  3542. btrfs_item_key_to_cpu(src, &key, i);
  3543. if (!btrfs_comp_cpu_keys(&key, &last_key))
  3544. done = true;
  3545. if (key.objectid != btrfs_ino(inode) ||
  3546. key.type != BTRFS_EXTENT_DATA_KEY) {
  3547. i++;
  3548. continue;
  3549. }
  3550. extent = btrfs_item_ptr(src, i, struct btrfs_file_extent_item);
  3551. if (btrfs_file_extent_type(src, extent) ==
  3552. BTRFS_FILE_EXTENT_INLINE) {
  3553. len = btrfs_file_extent_inline_len(src, i, extent);
  3554. extent_end = ALIGN(key.offset + len,
  3555. fs_info->sectorsize);
  3556. } else {
  3557. len = btrfs_file_extent_num_bytes(src, extent);
  3558. extent_end = key.offset + len;
  3559. }
  3560. i++;
  3561. if (*last_extent == key.offset) {
  3562. *last_extent = extent_end;
  3563. continue;
  3564. }
  3565. offset = *last_extent;
  3566. len = key.offset - *last_extent;
  3567. ret = btrfs_insert_file_extent(trans, log, btrfs_ino(inode),
  3568. offset, 0, 0, len, 0, len, 0, 0, 0);
  3569. if (ret)
  3570. break;
  3571. *last_extent = extent_end;
  3572. }
  3573. /*
  3574. * Need to let the callers know we dropped the path so they should
  3575. * re-search.
  3576. */
  3577. if (!ret && need_find_last_extent)
  3578. ret = 1;
  3579. return ret;
  3580. }
  3581. static int extent_cmp(void *priv, struct list_head *a, struct list_head *b)
  3582. {
  3583. struct extent_map *em1, *em2;
  3584. em1 = list_entry(a, struct extent_map, list);
  3585. em2 = list_entry(b, struct extent_map, list);
  3586. if (em1->start < em2->start)
  3587. return -1;
  3588. else if (em1->start > em2->start)
  3589. return 1;
  3590. return 0;
  3591. }
  3592. static int wait_ordered_extents(struct btrfs_trans_handle *trans,
  3593. struct inode *inode,
  3594. struct btrfs_root *root,
  3595. const struct extent_map *em,
  3596. const struct list_head *logged_list,
  3597. bool *ordered_io_error)
  3598. {
  3599. struct btrfs_fs_info *fs_info = root->fs_info;
  3600. struct btrfs_ordered_extent *ordered;
  3601. struct btrfs_root *log = root->log_root;
  3602. u64 mod_start = em->mod_start;
  3603. u64 mod_len = em->mod_len;
  3604. const bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  3605. u64 csum_offset;
  3606. u64 csum_len;
  3607. LIST_HEAD(ordered_sums);
  3608. int ret = 0;
  3609. *ordered_io_error = false;
  3610. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  3611. em->block_start == EXTENT_MAP_HOLE)
  3612. return 0;
  3613. /*
  3614. * Wait far any ordered extent that covers our extent map. If it
  3615. * finishes without an error, first check and see if our csums are on
  3616. * our outstanding ordered extents.
  3617. */
  3618. list_for_each_entry(ordered, logged_list, log_list) {
  3619. struct btrfs_ordered_sum *sum;
  3620. if (!mod_len)
  3621. break;
  3622. if (ordered->file_offset + ordered->len <= mod_start ||
  3623. mod_start + mod_len <= ordered->file_offset)
  3624. continue;
  3625. if (!test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) &&
  3626. !test_bit(BTRFS_ORDERED_IOERR, &ordered->flags) &&
  3627. !test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
  3628. const u64 start = ordered->file_offset;
  3629. const u64 end = ordered->file_offset + ordered->len - 1;
  3630. WARN_ON(ordered->inode != inode);
  3631. filemap_fdatawrite_range(inode->i_mapping, start, end);
  3632. }
  3633. wait_event(ordered->wait,
  3634. (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags) ||
  3635. test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)));
  3636. if (test_bit(BTRFS_ORDERED_IOERR, &ordered->flags)) {
  3637. /*
  3638. * Clear the AS_EIO/AS_ENOSPC flags from the inode's
  3639. * i_mapping flags, so that the next fsync won't get
  3640. * an outdated io error too.
  3641. */
  3642. filemap_check_errors(inode->i_mapping);
  3643. *ordered_io_error = true;
  3644. break;
  3645. }
  3646. /*
  3647. * We are going to copy all the csums on this ordered extent, so
  3648. * go ahead and adjust mod_start and mod_len in case this
  3649. * ordered extent has already been logged.
  3650. */
  3651. if (ordered->file_offset > mod_start) {
  3652. if (ordered->file_offset + ordered->len >=
  3653. mod_start + mod_len)
  3654. mod_len = ordered->file_offset - mod_start;
  3655. /*
  3656. * If we have this case
  3657. *
  3658. * |--------- logged extent ---------|
  3659. * |----- ordered extent ----|
  3660. *
  3661. * Just don't mess with mod_start and mod_len, we'll
  3662. * just end up logging more csums than we need and it
  3663. * will be ok.
  3664. */
  3665. } else {
  3666. if (ordered->file_offset + ordered->len <
  3667. mod_start + mod_len) {
  3668. mod_len = (mod_start + mod_len) -
  3669. (ordered->file_offset + ordered->len);
  3670. mod_start = ordered->file_offset +
  3671. ordered->len;
  3672. } else {
  3673. mod_len = 0;
  3674. }
  3675. }
  3676. if (skip_csum)
  3677. continue;
  3678. /*
  3679. * To keep us from looping for the above case of an ordered
  3680. * extent that falls inside of the logged extent.
  3681. */
  3682. if (test_and_set_bit(BTRFS_ORDERED_LOGGED_CSUM,
  3683. &ordered->flags))
  3684. continue;
  3685. list_for_each_entry(sum, &ordered->list, list) {
  3686. ret = btrfs_csum_file_blocks(trans, log, sum);
  3687. if (ret)
  3688. break;
  3689. }
  3690. }
  3691. if (*ordered_io_error || !mod_len || ret || skip_csum)
  3692. return ret;
  3693. if (em->compress_type) {
  3694. csum_offset = 0;
  3695. csum_len = max(em->block_len, em->orig_block_len);
  3696. } else {
  3697. csum_offset = mod_start - em->start;
  3698. csum_len = mod_len;
  3699. }
  3700. /* block start is already adjusted for the file extent offset. */
  3701. ret = btrfs_lookup_csums_range(fs_info->csum_root,
  3702. em->block_start + csum_offset,
  3703. em->block_start + csum_offset +
  3704. csum_len - 1, &ordered_sums, 0);
  3705. if (ret)
  3706. return ret;
  3707. while (!list_empty(&ordered_sums)) {
  3708. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  3709. struct btrfs_ordered_sum,
  3710. list);
  3711. if (!ret)
  3712. ret = btrfs_csum_file_blocks(trans, log, sums);
  3713. list_del(&sums->list);
  3714. kfree(sums);
  3715. }
  3716. return ret;
  3717. }
  3718. static int log_one_extent(struct btrfs_trans_handle *trans,
  3719. struct btrfs_inode *inode, struct btrfs_root *root,
  3720. const struct extent_map *em,
  3721. struct btrfs_path *path,
  3722. const struct list_head *logged_list,
  3723. struct btrfs_log_ctx *ctx)
  3724. {
  3725. struct btrfs_root *log = root->log_root;
  3726. struct btrfs_file_extent_item *fi;
  3727. struct extent_buffer *leaf;
  3728. struct btrfs_map_token token;
  3729. struct btrfs_key key;
  3730. u64 extent_offset = em->start - em->orig_start;
  3731. u64 block_len;
  3732. int ret;
  3733. int extent_inserted = 0;
  3734. bool ordered_io_err = false;
  3735. ret = wait_ordered_extents(trans, &inode->vfs_inode, root, em,
  3736. logged_list, &ordered_io_err);
  3737. if (ret)
  3738. return ret;
  3739. if (ordered_io_err) {
  3740. ctx->io_err = -EIO;
  3741. return ctx->io_err;
  3742. }
  3743. btrfs_init_map_token(&token);
  3744. ret = __btrfs_drop_extents(trans, log, &inode->vfs_inode, path, em->start,
  3745. em->start + em->len, NULL, 0, 1,
  3746. sizeof(*fi), &extent_inserted);
  3747. if (ret)
  3748. return ret;
  3749. if (!extent_inserted) {
  3750. key.objectid = btrfs_ino(inode);
  3751. key.type = BTRFS_EXTENT_DATA_KEY;
  3752. key.offset = em->start;
  3753. ret = btrfs_insert_empty_item(trans, log, path, &key,
  3754. sizeof(*fi));
  3755. if (ret)
  3756. return ret;
  3757. }
  3758. leaf = path->nodes[0];
  3759. fi = btrfs_item_ptr(leaf, path->slots[0],
  3760. struct btrfs_file_extent_item);
  3761. btrfs_set_token_file_extent_generation(leaf, fi, trans->transid,
  3762. &token);
  3763. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  3764. btrfs_set_token_file_extent_type(leaf, fi,
  3765. BTRFS_FILE_EXTENT_PREALLOC,
  3766. &token);
  3767. else
  3768. btrfs_set_token_file_extent_type(leaf, fi,
  3769. BTRFS_FILE_EXTENT_REG,
  3770. &token);
  3771. block_len = max(em->block_len, em->orig_block_len);
  3772. if (em->compress_type != BTRFS_COMPRESS_NONE) {
  3773. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3774. em->block_start,
  3775. &token);
  3776. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3777. &token);
  3778. } else if (em->block_start < EXTENT_MAP_LAST_BYTE) {
  3779. btrfs_set_token_file_extent_disk_bytenr(leaf, fi,
  3780. em->block_start -
  3781. extent_offset, &token);
  3782. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, block_len,
  3783. &token);
  3784. } else {
  3785. btrfs_set_token_file_extent_disk_bytenr(leaf, fi, 0, &token);
  3786. btrfs_set_token_file_extent_disk_num_bytes(leaf, fi, 0,
  3787. &token);
  3788. }
  3789. btrfs_set_token_file_extent_offset(leaf, fi, extent_offset, &token);
  3790. btrfs_set_token_file_extent_num_bytes(leaf, fi, em->len, &token);
  3791. btrfs_set_token_file_extent_ram_bytes(leaf, fi, em->ram_bytes, &token);
  3792. btrfs_set_token_file_extent_compression(leaf, fi, em->compress_type,
  3793. &token);
  3794. btrfs_set_token_file_extent_encryption(leaf, fi, 0, &token);
  3795. btrfs_set_token_file_extent_other_encoding(leaf, fi, 0, &token);
  3796. btrfs_mark_buffer_dirty(leaf);
  3797. btrfs_release_path(path);
  3798. return ret;
  3799. }
  3800. static int btrfs_log_changed_extents(struct btrfs_trans_handle *trans,
  3801. struct btrfs_root *root,
  3802. struct btrfs_inode *inode,
  3803. struct btrfs_path *path,
  3804. struct list_head *logged_list,
  3805. struct btrfs_log_ctx *ctx,
  3806. const u64 start,
  3807. const u64 end)
  3808. {
  3809. struct extent_map *em, *n;
  3810. struct list_head extents;
  3811. struct extent_map_tree *tree = &inode->extent_tree;
  3812. u64 logged_start, logged_end;
  3813. u64 test_gen;
  3814. int ret = 0;
  3815. int num = 0;
  3816. INIT_LIST_HEAD(&extents);
  3817. down_write(&inode->dio_sem);
  3818. write_lock(&tree->lock);
  3819. test_gen = root->fs_info->last_trans_committed;
  3820. logged_start = start;
  3821. logged_end = end;
  3822. list_for_each_entry_safe(em, n, &tree->modified_extents, list) {
  3823. list_del_init(&em->list);
  3824. /*
  3825. * Just an arbitrary number, this can be really CPU intensive
  3826. * once we start getting a lot of extents, and really once we
  3827. * have a bunch of extents we just want to commit since it will
  3828. * be faster.
  3829. */
  3830. if (++num > 32768) {
  3831. list_del_init(&tree->modified_extents);
  3832. ret = -EFBIG;
  3833. goto process;
  3834. }
  3835. if (em->generation <= test_gen)
  3836. continue;
  3837. if (em->start < logged_start)
  3838. logged_start = em->start;
  3839. if ((em->start + em->len - 1) > logged_end)
  3840. logged_end = em->start + em->len - 1;
  3841. /* Need a ref to keep it from getting evicted from cache */
  3842. refcount_inc(&em->refs);
  3843. set_bit(EXTENT_FLAG_LOGGING, &em->flags);
  3844. list_add_tail(&em->list, &extents);
  3845. num++;
  3846. }
  3847. list_sort(NULL, &extents, extent_cmp);
  3848. btrfs_get_logged_extents(inode, logged_list, logged_start, logged_end);
  3849. /*
  3850. * Some ordered extents started by fsync might have completed
  3851. * before we could collect them into the list logged_list, which
  3852. * means they're gone, not in our logged_list nor in the inode's
  3853. * ordered tree. We want the application/user space to know an
  3854. * error happened while attempting to persist file data so that
  3855. * it can take proper action. If such error happened, we leave
  3856. * without writing to the log tree and the fsync must report the
  3857. * file data write error and not commit the current transaction.
  3858. */
  3859. ret = filemap_check_errors(inode->vfs_inode.i_mapping);
  3860. if (ret)
  3861. ctx->io_err = ret;
  3862. process:
  3863. while (!list_empty(&extents)) {
  3864. em = list_entry(extents.next, struct extent_map, list);
  3865. list_del_init(&em->list);
  3866. /*
  3867. * If we had an error we just need to delete everybody from our
  3868. * private list.
  3869. */
  3870. if (ret) {
  3871. clear_em_logging(tree, em);
  3872. free_extent_map(em);
  3873. continue;
  3874. }
  3875. write_unlock(&tree->lock);
  3876. ret = log_one_extent(trans, inode, root, em, path, logged_list,
  3877. ctx);
  3878. write_lock(&tree->lock);
  3879. clear_em_logging(tree, em);
  3880. free_extent_map(em);
  3881. }
  3882. WARN_ON(!list_empty(&extents));
  3883. write_unlock(&tree->lock);
  3884. up_write(&inode->dio_sem);
  3885. btrfs_release_path(path);
  3886. return ret;
  3887. }
  3888. static int logged_inode_size(struct btrfs_root *log, struct btrfs_inode *inode,
  3889. struct btrfs_path *path, u64 *size_ret)
  3890. {
  3891. struct btrfs_key key;
  3892. int ret;
  3893. key.objectid = btrfs_ino(inode);
  3894. key.type = BTRFS_INODE_ITEM_KEY;
  3895. key.offset = 0;
  3896. ret = btrfs_search_slot(NULL, log, &key, path, 0, 0);
  3897. if (ret < 0) {
  3898. return ret;
  3899. } else if (ret > 0) {
  3900. *size_ret = 0;
  3901. } else {
  3902. struct btrfs_inode_item *item;
  3903. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3904. struct btrfs_inode_item);
  3905. *size_ret = btrfs_inode_size(path->nodes[0], item);
  3906. }
  3907. btrfs_release_path(path);
  3908. return 0;
  3909. }
  3910. /*
  3911. * At the moment we always log all xattrs. This is to figure out at log replay
  3912. * time which xattrs must have their deletion replayed. If a xattr is missing
  3913. * in the log tree and exists in the fs/subvol tree, we delete it. This is
  3914. * because if a xattr is deleted, the inode is fsynced and a power failure
  3915. * happens, causing the log to be replayed the next time the fs is mounted,
  3916. * we want the xattr to not exist anymore (same behaviour as other filesystems
  3917. * with a journal, ext3/4, xfs, f2fs, etc).
  3918. */
  3919. static int btrfs_log_all_xattrs(struct btrfs_trans_handle *trans,
  3920. struct btrfs_root *root,
  3921. struct btrfs_inode *inode,
  3922. struct btrfs_path *path,
  3923. struct btrfs_path *dst_path)
  3924. {
  3925. int ret;
  3926. struct btrfs_key key;
  3927. const u64 ino = btrfs_ino(inode);
  3928. int ins_nr = 0;
  3929. int start_slot = 0;
  3930. key.objectid = ino;
  3931. key.type = BTRFS_XATTR_ITEM_KEY;
  3932. key.offset = 0;
  3933. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3934. if (ret < 0)
  3935. return ret;
  3936. while (true) {
  3937. int slot = path->slots[0];
  3938. struct extent_buffer *leaf = path->nodes[0];
  3939. int nritems = btrfs_header_nritems(leaf);
  3940. if (slot >= nritems) {
  3941. if (ins_nr > 0) {
  3942. u64 last_extent = 0;
  3943. ret = copy_items(trans, inode, dst_path, path,
  3944. &last_extent, start_slot,
  3945. ins_nr, 1, 0);
  3946. /* can't be 1, extent items aren't processed */
  3947. ASSERT(ret <= 0);
  3948. if (ret < 0)
  3949. return ret;
  3950. ins_nr = 0;
  3951. }
  3952. ret = btrfs_next_leaf(root, path);
  3953. if (ret < 0)
  3954. return ret;
  3955. else if (ret > 0)
  3956. break;
  3957. continue;
  3958. }
  3959. btrfs_item_key_to_cpu(leaf, &key, slot);
  3960. if (key.objectid != ino || key.type != BTRFS_XATTR_ITEM_KEY)
  3961. break;
  3962. if (ins_nr == 0)
  3963. start_slot = slot;
  3964. ins_nr++;
  3965. path->slots[0]++;
  3966. cond_resched();
  3967. }
  3968. if (ins_nr > 0) {
  3969. u64 last_extent = 0;
  3970. ret = copy_items(trans, inode, dst_path, path,
  3971. &last_extent, start_slot,
  3972. ins_nr, 1, 0);
  3973. /* can't be 1, extent items aren't processed */
  3974. ASSERT(ret <= 0);
  3975. if (ret < 0)
  3976. return ret;
  3977. }
  3978. return 0;
  3979. }
  3980. /*
  3981. * If the no holes feature is enabled we need to make sure any hole between the
  3982. * last extent and the i_size of our inode is explicitly marked in the log. This
  3983. * is to make sure that doing something like:
  3984. *
  3985. * 1) create file with 128Kb of data
  3986. * 2) truncate file to 64Kb
  3987. * 3) truncate file to 256Kb
  3988. * 4) fsync file
  3989. * 5) <crash/power failure>
  3990. * 6) mount fs and trigger log replay
  3991. *
  3992. * Will give us a file with a size of 256Kb, the first 64Kb of data match what
  3993. * the file had in its first 64Kb of data at step 1 and the last 192Kb of the
  3994. * file correspond to a hole. The presence of explicit holes in a log tree is
  3995. * what guarantees that log replay will remove/adjust file extent items in the
  3996. * fs/subvol tree.
  3997. *
  3998. * Here we do not need to care about holes between extents, that is already done
  3999. * by copy_items(). We also only need to do this in the full sync path, where we
  4000. * lookup for extents from the fs/subvol tree only. In the fast path case, we
  4001. * lookup the list of modified extent maps and if any represents a hole, we
  4002. * insert a corresponding extent representing a hole in the log tree.
  4003. */
  4004. static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
  4005. struct btrfs_root *root,
  4006. struct btrfs_inode *inode,
  4007. struct btrfs_path *path)
  4008. {
  4009. struct btrfs_fs_info *fs_info = root->fs_info;
  4010. int ret;
  4011. struct btrfs_key key;
  4012. u64 hole_start;
  4013. u64 hole_size;
  4014. struct extent_buffer *leaf;
  4015. struct btrfs_root *log = root->log_root;
  4016. const u64 ino = btrfs_ino(inode);
  4017. const u64 i_size = i_size_read(&inode->vfs_inode);
  4018. if (!btrfs_fs_incompat(fs_info, NO_HOLES))
  4019. return 0;
  4020. key.objectid = ino;
  4021. key.type = BTRFS_EXTENT_DATA_KEY;
  4022. key.offset = (u64)-1;
  4023. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4024. ASSERT(ret != 0);
  4025. if (ret < 0)
  4026. return ret;
  4027. ASSERT(path->slots[0] > 0);
  4028. path->slots[0]--;
  4029. leaf = path->nodes[0];
  4030. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4031. if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
  4032. /* inode does not have any extents */
  4033. hole_start = 0;
  4034. hole_size = i_size;
  4035. } else {
  4036. struct btrfs_file_extent_item *extent;
  4037. u64 len;
  4038. /*
  4039. * If there's an extent beyond i_size, an explicit hole was
  4040. * already inserted by copy_items().
  4041. */
  4042. if (key.offset >= i_size)
  4043. return 0;
  4044. extent = btrfs_item_ptr(leaf, path->slots[0],
  4045. struct btrfs_file_extent_item);
  4046. if (btrfs_file_extent_type(leaf, extent) ==
  4047. BTRFS_FILE_EXTENT_INLINE) {
  4048. len = btrfs_file_extent_inline_len(leaf,
  4049. path->slots[0],
  4050. extent);
  4051. ASSERT(len == i_size ||
  4052. (len == fs_info->sectorsize &&
  4053. btrfs_file_extent_compression(leaf, extent) !=
  4054. BTRFS_COMPRESS_NONE));
  4055. return 0;
  4056. }
  4057. len = btrfs_file_extent_num_bytes(leaf, extent);
  4058. /* Last extent goes beyond i_size, no need to log a hole. */
  4059. if (key.offset + len > i_size)
  4060. return 0;
  4061. hole_start = key.offset + len;
  4062. hole_size = i_size - hole_start;
  4063. }
  4064. btrfs_release_path(path);
  4065. /* Last extent ends at i_size. */
  4066. if (hole_size == 0)
  4067. return 0;
  4068. hole_size = ALIGN(hole_size, fs_info->sectorsize);
  4069. ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
  4070. hole_size, 0, hole_size, 0, 0, 0);
  4071. return ret;
  4072. }
  4073. /*
  4074. * When we are logging a new inode X, check if it doesn't have a reference that
  4075. * matches the reference from some other inode Y created in a past transaction
  4076. * and that was renamed in the current transaction. If we don't do this, then at
  4077. * log replay time we can lose inode Y (and all its files if it's a directory):
  4078. *
  4079. * mkdir /mnt/x
  4080. * echo "hello world" > /mnt/x/foobar
  4081. * sync
  4082. * mv /mnt/x /mnt/y
  4083. * mkdir /mnt/x # or touch /mnt/x
  4084. * xfs_io -c fsync /mnt/x
  4085. * <power fail>
  4086. * mount fs, trigger log replay
  4087. *
  4088. * After the log replay procedure, we would lose the first directory and all its
  4089. * files (file foobar).
  4090. * For the case where inode Y is not a directory we simply end up losing it:
  4091. *
  4092. * echo "123" > /mnt/foo
  4093. * sync
  4094. * mv /mnt/foo /mnt/bar
  4095. * echo "abc" > /mnt/foo
  4096. * xfs_io -c fsync /mnt/foo
  4097. * <power fail>
  4098. *
  4099. * We also need this for cases where a snapshot entry is replaced by some other
  4100. * entry (file or directory) otherwise we end up with an unreplayable log due to
  4101. * attempts to delete the snapshot entry (entry of type BTRFS_ROOT_ITEM_KEY) as
  4102. * if it were a regular entry:
  4103. *
  4104. * mkdir /mnt/x
  4105. * btrfs subvolume snapshot /mnt /mnt/x/snap
  4106. * btrfs subvolume delete /mnt/x/snap
  4107. * rmdir /mnt/x
  4108. * mkdir /mnt/x
  4109. * fsync /mnt/x or fsync some new file inside it
  4110. * <power fail>
  4111. *
  4112. * The snapshot delete, rmdir of x, mkdir of a new x and the fsync all happen in
  4113. * the same transaction.
  4114. */
  4115. static int btrfs_check_ref_name_override(struct extent_buffer *eb,
  4116. const int slot,
  4117. const struct btrfs_key *key,
  4118. struct btrfs_inode *inode,
  4119. u64 *other_ino)
  4120. {
  4121. int ret;
  4122. struct btrfs_path *search_path;
  4123. char *name = NULL;
  4124. u32 name_len = 0;
  4125. u32 item_size = btrfs_item_size_nr(eb, slot);
  4126. u32 cur_offset = 0;
  4127. unsigned long ptr = btrfs_item_ptr_offset(eb, slot);
  4128. search_path = btrfs_alloc_path();
  4129. if (!search_path)
  4130. return -ENOMEM;
  4131. search_path->search_commit_root = 1;
  4132. search_path->skip_locking = 1;
  4133. while (cur_offset < item_size) {
  4134. u64 parent;
  4135. u32 this_name_len;
  4136. u32 this_len;
  4137. unsigned long name_ptr;
  4138. struct btrfs_dir_item *di;
  4139. if (key->type == BTRFS_INODE_REF_KEY) {
  4140. struct btrfs_inode_ref *iref;
  4141. iref = (struct btrfs_inode_ref *)(ptr + cur_offset);
  4142. parent = key->offset;
  4143. this_name_len = btrfs_inode_ref_name_len(eb, iref);
  4144. name_ptr = (unsigned long)(iref + 1);
  4145. this_len = sizeof(*iref) + this_name_len;
  4146. } else {
  4147. struct btrfs_inode_extref *extref;
  4148. extref = (struct btrfs_inode_extref *)(ptr +
  4149. cur_offset);
  4150. parent = btrfs_inode_extref_parent(eb, extref);
  4151. this_name_len = btrfs_inode_extref_name_len(eb, extref);
  4152. name_ptr = (unsigned long)&extref->name;
  4153. this_len = sizeof(*extref) + this_name_len;
  4154. }
  4155. if (this_name_len > name_len) {
  4156. char *new_name;
  4157. new_name = krealloc(name, this_name_len, GFP_NOFS);
  4158. if (!new_name) {
  4159. ret = -ENOMEM;
  4160. goto out;
  4161. }
  4162. name_len = this_name_len;
  4163. name = new_name;
  4164. }
  4165. read_extent_buffer(eb, name, name_ptr, this_name_len);
  4166. di = btrfs_lookup_dir_item(NULL, inode->root, search_path,
  4167. parent, name, this_name_len, 0);
  4168. if (di && !IS_ERR(di)) {
  4169. struct btrfs_key di_key;
  4170. btrfs_dir_item_key_to_cpu(search_path->nodes[0],
  4171. di, &di_key);
  4172. if (di_key.type == BTRFS_INODE_ITEM_KEY) {
  4173. ret = 1;
  4174. *other_ino = di_key.objectid;
  4175. } else {
  4176. ret = -EAGAIN;
  4177. }
  4178. goto out;
  4179. } else if (IS_ERR(di)) {
  4180. ret = PTR_ERR(di);
  4181. goto out;
  4182. }
  4183. btrfs_release_path(search_path);
  4184. cur_offset += this_len;
  4185. }
  4186. ret = 0;
  4187. out:
  4188. btrfs_free_path(search_path);
  4189. kfree(name);
  4190. return ret;
  4191. }
  4192. /* log a single inode in the tree log.
  4193. * At least one parent directory for this inode must exist in the tree
  4194. * or be logged already.
  4195. *
  4196. * Any items from this inode changed by the current transaction are copied
  4197. * to the log tree. An extra reference is taken on any extents in this
  4198. * file, allowing us to avoid a whole pile of corner cases around logging
  4199. * blocks that have been removed from the tree.
  4200. *
  4201. * See LOG_INODE_ALL and related defines for a description of what inode_only
  4202. * does.
  4203. *
  4204. * This handles both files and directories.
  4205. */
  4206. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  4207. struct btrfs_root *root, struct btrfs_inode *inode,
  4208. int inode_only,
  4209. const loff_t start,
  4210. const loff_t end,
  4211. struct btrfs_log_ctx *ctx)
  4212. {
  4213. struct btrfs_fs_info *fs_info = root->fs_info;
  4214. struct btrfs_path *path;
  4215. struct btrfs_path *dst_path;
  4216. struct btrfs_key min_key;
  4217. struct btrfs_key max_key;
  4218. struct btrfs_root *log = root->log_root;
  4219. LIST_HEAD(logged_list);
  4220. u64 last_extent = 0;
  4221. int err = 0;
  4222. int ret;
  4223. int nritems;
  4224. int ins_start_slot = 0;
  4225. int ins_nr;
  4226. bool fast_search = false;
  4227. u64 ino = btrfs_ino(inode);
  4228. struct extent_map_tree *em_tree = &inode->extent_tree;
  4229. u64 logged_isize = 0;
  4230. bool need_log_inode_item = true;
  4231. path = btrfs_alloc_path();
  4232. if (!path)
  4233. return -ENOMEM;
  4234. dst_path = btrfs_alloc_path();
  4235. if (!dst_path) {
  4236. btrfs_free_path(path);
  4237. return -ENOMEM;
  4238. }
  4239. min_key.objectid = ino;
  4240. min_key.type = BTRFS_INODE_ITEM_KEY;
  4241. min_key.offset = 0;
  4242. max_key.objectid = ino;
  4243. /* today the code can only do partial logging of directories */
  4244. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  4245. (!test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4246. &inode->runtime_flags) &&
  4247. inode_only >= LOG_INODE_EXISTS))
  4248. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4249. else
  4250. max_key.type = (u8)-1;
  4251. max_key.offset = (u64)-1;
  4252. /*
  4253. * Only run delayed items if we are a dir or a new file.
  4254. * Otherwise commit the delayed inode only, which is needed in
  4255. * order for the log replay code to mark inodes for link count
  4256. * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
  4257. */
  4258. if (S_ISDIR(inode->vfs_inode.i_mode) ||
  4259. inode->generation > fs_info->last_trans_committed)
  4260. ret = btrfs_commit_inode_delayed_items(trans, inode);
  4261. else
  4262. ret = btrfs_commit_inode_delayed_inode(inode);
  4263. if (ret) {
  4264. btrfs_free_path(path);
  4265. btrfs_free_path(dst_path);
  4266. return ret;
  4267. }
  4268. if (inode_only == LOG_OTHER_INODE) {
  4269. inode_only = LOG_INODE_EXISTS;
  4270. mutex_lock_nested(&inode->log_mutex, SINGLE_DEPTH_NESTING);
  4271. } else {
  4272. mutex_lock(&inode->log_mutex);
  4273. }
  4274. /*
  4275. * a brute force approach to making sure we get the most uptodate
  4276. * copies of everything.
  4277. */
  4278. if (S_ISDIR(inode->vfs_inode.i_mode)) {
  4279. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  4280. if (inode_only == LOG_INODE_EXISTS)
  4281. max_key_type = BTRFS_XATTR_ITEM_KEY;
  4282. ret = drop_objectid_items(trans, log, path, ino, max_key_type);
  4283. } else {
  4284. if (inode_only == LOG_INODE_EXISTS) {
  4285. /*
  4286. * Make sure the new inode item we write to the log has
  4287. * the same isize as the current one (if it exists).
  4288. * This is necessary to prevent data loss after log
  4289. * replay, and also to prevent doing a wrong expanding
  4290. * truncate - for e.g. create file, write 4K into offset
  4291. * 0, fsync, write 4K into offset 4096, add hard link,
  4292. * fsync some other file (to sync log), power fail - if
  4293. * we use the inode's current i_size, after log replay
  4294. * we get a 8Kb file, with the last 4Kb extent as a hole
  4295. * (zeroes), as if an expanding truncate happened,
  4296. * instead of getting a file of 4Kb only.
  4297. */
  4298. err = logged_inode_size(log, inode, path, &logged_isize);
  4299. if (err)
  4300. goto out_unlock;
  4301. }
  4302. if (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4303. &inode->runtime_flags)) {
  4304. if (inode_only == LOG_INODE_EXISTS) {
  4305. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4306. ret = drop_objectid_items(trans, log, path, ino,
  4307. max_key.type);
  4308. } else {
  4309. clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  4310. &inode->runtime_flags);
  4311. clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4312. &inode->runtime_flags);
  4313. while(1) {
  4314. ret = btrfs_truncate_inode_items(trans,
  4315. log, &inode->vfs_inode, 0, 0);
  4316. if (ret != -EAGAIN)
  4317. break;
  4318. }
  4319. }
  4320. } else if (test_and_clear_bit(BTRFS_INODE_COPY_EVERYTHING,
  4321. &inode->runtime_flags) ||
  4322. inode_only == LOG_INODE_EXISTS) {
  4323. if (inode_only == LOG_INODE_ALL)
  4324. fast_search = true;
  4325. max_key.type = BTRFS_XATTR_ITEM_KEY;
  4326. ret = drop_objectid_items(trans, log, path, ino,
  4327. max_key.type);
  4328. } else {
  4329. if (inode_only == LOG_INODE_ALL)
  4330. fast_search = true;
  4331. goto log_extents;
  4332. }
  4333. }
  4334. if (ret) {
  4335. err = ret;
  4336. goto out_unlock;
  4337. }
  4338. while (1) {
  4339. ins_nr = 0;
  4340. ret = btrfs_search_forward(root, &min_key,
  4341. path, trans->transid);
  4342. if (ret < 0) {
  4343. err = ret;
  4344. goto out_unlock;
  4345. }
  4346. if (ret != 0)
  4347. break;
  4348. again:
  4349. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  4350. if (min_key.objectid != ino)
  4351. break;
  4352. if (min_key.type > max_key.type)
  4353. break;
  4354. if (min_key.type == BTRFS_INODE_ITEM_KEY)
  4355. need_log_inode_item = false;
  4356. if ((min_key.type == BTRFS_INODE_REF_KEY ||
  4357. min_key.type == BTRFS_INODE_EXTREF_KEY) &&
  4358. inode->generation == trans->transid) {
  4359. u64 other_ino = 0;
  4360. ret = btrfs_check_ref_name_override(path->nodes[0],
  4361. path->slots[0], &min_key, inode,
  4362. &other_ino);
  4363. if (ret < 0) {
  4364. err = ret;
  4365. goto out_unlock;
  4366. } else if (ret > 0 && ctx &&
  4367. other_ino != btrfs_ino(BTRFS_I(ctx->inode))) {
  4368. struct btrfs_key inode_key;
  4369. struct inode *other_inode;
  4370. if (ins_nr > 0) {
  4371. ins_nr++;
  4372. } else {
  4373. ins_nr = 1;
  4374. ins_start_slot = path->slots[0];
  4375. }
  4376. ret = copy_items(trans, inode, dst_path, path,
  4377. &last_extent, ins_start_slot,
  4378. ins_nr, inode_only,
  4379. logged_isize);
  4380. if (ret < 0) {
  4381. err = ret;
  4382. goto out_unlock;
  4383. }
  4384. ins_nr = 0;
  4385. btrfs_release_path(path);
  4386. inode_key.objectid = other_ino;
  4387. inode_key.type = BTRFS_INODE_ITEM_KEY;
  4388. inode_key.offset = 0;
  4389. other_inode = btrfs_iget(fs_info->sb,
  4390. &inode_key, root,
  4391. NULL);
  4392. /*
  4393. * If the other inode that had a conflicting dir
  4394. * entry was deleted in the current transaction,
  4395. * we don't need to do more work nor fallback to
  4396. * a transaction commit.
  4397. */
  4398. if (IS_ERR(other_inode) &&
  4399. PTR_ERR(other_inode) == -ENOENT) {
  4400. goto next_key;
  4401. } else if (IS_ERR(other_inode)) {
  4402. err = PTR_ERR(other_inode);
  4403. goto out_unlock;
  4404. }
  4405. /*
  4406. * We are safe logging the other inode without
  4407. * acquiring its i_mutex as long as we log with
  4408. * the LOG_INODE_EXISTS mode. We're safe against
  4409. * concurrent renames of the other inode as well
  4410. * because during a rename we pin the log and
  4411. * update the log with the new name before we
  4412. * unpin it.
  4413. */
  4414. err = btrfs_log_inode(trans, root,
  4415. BTRFS_I(other_inode),
  4416. LOG_OTHER_INODE, 0, LLONG_MAX,
  4417. ctx);
  4418. iput(other_inode);
  4419. if (err)
  4420. goto out_unlock;
  4421. else
  4422. goto next_key;
  4423. }
  4424. }
  4425. /* Skip xattrs, we log them later with btrfs_log_all_xattrs() */
  4426. if (min_key.type == BTRFS_XATTR_ITEM_KEY) {
  4427. if (ins_nr == 0)
  4428. goto next_slot;
  4429. ret = copy_items(trans, inode, dst_path, path,
  4430. &last_extent, ins_start_slot,
  4431. ins_nr, inode_only, logged_isize);
  4432. if (ret < 0) {
  4433. err = ret;
  4434. goto out_unlock;
  4435. }
  4436. ins_nr = 0;
  4437. if (ret) {
  4438. btrfs_release_path(path);
  4439. continue;
  4440. }
  4441. goto next_slot;
  4442. }
  4443. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  4444. ins_nr++;
  4445. goto next_slot;
  4446. } else if (!ins_nr) {
  4447. ins_start_slot = path->slots[0];
  4448. ins_nr = 1;
  4449. goto next_slot;
  4450. }
  4451. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4452. ins_start_slot, ins_nr, inode_only,
  4453. logged_isize);
  4454. if (ret < 0) {
  4455. err = ret;
  4456. goto out_unlock;
  4457. }
  4458. if (ret) {
  4459. ins_nr = 0;
  4460. btrfs_release_path(path);
  4461. continue;
  4462. }
  4463. ins_nr = 1;
  4464. ins_start_slot = path->slots[0];
  4465. next_slot:
  4466. nritems = btrfs_header_nritems(path->nodes[0]);
  4467. path->slots[0]++;
  4468. if (path->slots[0] < nritems) {
  4469. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  4470. path->slots[0]);
  4471. goto again;
  4472. }
  4473. if (ins_nr) {
  4474. ret = copy_items(trans, inode, dst_path, path,
  4475. &last_extent, ins_start_slot,
  4476. ins_nr, inode_only, logged_isize);
  4477. if (ret < 0) {
  4478. err = ret;
  4479. goto out_unlock;
  4480. }
  4481. ret = 0;
  4482. ins_nr = 0;
  4483. }
  4484. btrfs_release_path(path);
  4485. next_key:
  4486. if (min_key.offset < (u64)-1) {
  4487. min_key.offset++;
  4488. } else if (min_key.type < max_key.type) {
  4489. min_key.type++;
  4490. min_key.offset = 0;
  4491. } else {
  4492. break;
  4493. }
  4494. }
  4495. if (ins_nr) {
  4496. ret = copy_items(trans, inode, dst_path, path, &last_extent,
  4497. ins_start_slot, ins_nr, inode_only,
  4498. logged_isize);
  4499. if (ret < 0) {
  4500. err = ret;
  4501. goto out_unlock;
  4502. }
  4503. ret = 0;
  4504. ins_nr = 0;
  4505. }
  4506. btrfs_release_path(path);
  4507. btrfs_release_path(dst_path);
  4508. err = btrfs_log_all_xattrs(trans, root, inode, path, dst_path);
  4509. if (err)
  4510. goto out_unlock;
  4511. if (max_key.type >= BTRFS_EXTENT_DATA_KEY && !fast_search) {
  4512. btrfs_release_path(path);
  4513. btrfs_release_path(dst_path);
  4514. err = btrfs_log_trailing_hole(trans, root, inode, path);
  4515. if (err)
  4516. goto out_unlock;
  4517. }
  4518. log_extents:
  4519. btrfs_release_path(path);
  4520. btrfs_release_path(dst_path);
  4521. if (need_log_inode_item) {
  4522. err = log_inode_item(trans, log, dst_path, inode);
  4523. if (err)
  4524. goto out_unlock;
  4525. }
  4526. if (fast_search) {
  4527. ret = btrfs_log_changed_extents(trans, root, inode, dst_path,
  4528. &logged_list, ctx, start, end);
  4529. if (ret) {
  4530. err = ret;
  4531. goto out_unlock;
  4532. }
  4533. } else if (inode_only == LOG_INODE_ALL) {
  4534. struct extent_map *em, *n;
  4535. write_lock(&em_tree->lock);
  4536. /*
  4537. * We can't just remove every em if we're called for a ranged
  4538. * fsync - that is, one that doesn't cover the whole possible
  4539. * file range (0 to LLONG_MAX). This is because we can have
  4540. * em's that fall outside the range we're logging and therefore
  4541. * their ordered operations haven't completed yet
  4542. * (btrfs_finish_ordered_io() not invoked yet). This means we
  4543. * didn't get their respective file extent item in the fs/subvol
  4544. * tree yet, and need to let the next fast fsync (one which
  4545. * consults the list of modified extent maps) find the em so
  4546. * that it logs a matching file extent item and waits for the
  4547. * respective ordered operation to complete (if it's still
  4548. * running).
  4549. *
  4550. * Removing every em outside the range we're logging would make
  4551. * the next fast fsync not log their matching file extent items,
  4552. * therefore making us lose data after a log replay.
  4553. */
  4554. list_for_each_entry_safe(em, n, &em_tree->modified_extents,
  4555. list) {
  4556. const u64 mod_end = em->mod_start + em->mod_len - 1;
  4557. if (em->mod_start >= start && mod_end <= end)
  4558. list_del_init(&em->list);
  4559. }
  4560. write_unlock(&em_tree->lock);
  4561. }
  4562. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->vfs_inode.i_mode)) {
  4563. ret = log_directory_changes(trans, root, inode, path, dst_path,
  4564. ctx);
  4565. if (ret) {
  4566. err = ret;
  4567. goto out_unlock;
  4568. }
  4569. }
  4570. spin_lock(&inode->lock);
  4571. inode->logged_trans = trans->transid;
  4572. inode->last_log_commit = inode->last_sub_trans;
  4573. spin_unlock(&inode->lock);
  4574. out_unlock:
  4575. if (unlikely(err))
  4576. btrfs_put_logged_extents(&logged_list);
  4577. else
  4578. btrfs_submit_logged_extents(&logged_list, log);
  4579. mutex_unlock(&inode->log_mutex);
  4580. btrfs_free_path(path);
  4581. btrfs_free_path(dst_path);
  4582. return err;
  4583. }
  4584. /*
  4585. * Check if we must fallback to a transaction commit when logging an inode.
  4586. * This must be called after logging the inode and is used only in the context
  4587. * when fsyncing an inode requires the need to log some other inode - in which
  4588. * case we can't lock the i_mutex of each other inode we need to log as that
  4589. * can lead to deadlocks with concurrent fsync against other inodes (as we can
  4590. * log inodes up or down in the hierarchy) or rename operations for example. So
  4591. * we take the log_mutex of the inode after we have logged it and then check for
  4592. * its last_unlink_trans value - this is safe because any task setting
  4593. * last_unlink_trans must take the log_mutex and it must do this before it does
  4594. * the actual unlink operation, so if we do this check before a concurrent task
  4595. * sets last_unlink_trans it means we've logged a consistent version/state of
  4596. * all the inode items, otherwise we are not sure and must do a transaction
  4597. * commit (the concurrent task might have only updated last_unlink_trans before
  4598. * we logged the inode or it might have also done the unlink).
  4599. */
  4600. static bool btrfs_must_commit_transaction(struct btrfs_trans_handle *trans,
  4601. struct btrfs_inode *inode)
  4602. {
  4603. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  4604. bool ret = false;
  4605. mutex_lock(&inode->log_mutex);
  4606. if (inode->last_unlink_trans > fs_info->last_trans_committed) {
  4607. /*
  4608. * Make sure any commits to the log are forced to be full
  4609. * commits.
  4610. */
  4611. btrfs_set_log_full_commit(fs_info, trans);
  4612. ret = true;
  4613. }
  4614. mutex_unlock(&inode->log_mutex);
  4615. return ret;
  4616. }
  4617. /*
  4618. * follow the dentry parent pointers up the chain and see if any
  4619. * of the directories in it require a full commit before they can
  4620. * be logged. Returns zero if nothing special needs to be done or 1 if
  4621. * a full commit is required.
  4622. */
  4623. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  4624. struct btrfs_inode *inode,
  4625. struct dentry *parent,
  4626. struct super_block *sb,
  4627. u64 last_committed)
  4628. {
  4629. int ret = 0;
  4630. struct dentry *old_parent = NULL;
  4631. struct btrfs_inode *orig_inode = inode;
  4632. /*
  4633. * for regular files, if its inode is already on disk, we don't
  4634. * have to worry about the parents at all. This is because
  4635. * we can use the last_unlink_trans field to record renames
  4636. * and other fun in this file.
  4637. */
  4638. if (S_ISREG(inode->vfs_inode.i_mode) &&
  4639. inode->generation <= last_committed &&
  4640. inode->last_unlink_trans <= last_committed)
  4641. goto out;
  4642. if (!S_ISDIR(inode->vfs_inode.i_mode)) {
  4643. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4644. goto out;
  4645. inode = BTRFS_I(d_inode(parent));
  4646. }
  4647. while (1) {
  4648. /*
  4649. * If we are logging a directory then we start with our inode,
  4650. * not our parent's inode, so we need to skip setting the
  4651. * logged_trans so that further down in the log code we don't
  4652. * think this inode has already been logged.
  4653. */
  4654. if (inode != orig_inode)
  4655. inode->logged_trans = trans->transid;
  4656. smp_mb();
  4657. if (btrfs_must_commit_transaction(trans, inode)) {
  4658. ret = 1;
  4659. break;
  4660. }
  4661. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  4662. break;
  4663. if (IS_ROOT(parent)) {
  4664. inode = BTRFS_I(d_inode(parent));
  4665. if (btrfs_must_commit_transaction(trans, inode))
  4666. ret = 1;
  4667. break;
  4668. }
  4669. parent = dget_parent(parent);
  4670. dput(old_parent);
  4671. old_parent = parent;
  4672. inode = BTRFS_I(d_inode(parent));
  4673. }
  4674. dput(old_parent);
  4675. out:
  4676. return ret;
  4677. }
  4678. struct btrfs_dir_list {
  4679. u64 ino;
  4680. struct list_head list;
  4681. };
  4682. /*
  4683. * Log the inodes of the new dentries of a directory. See log_dir_items() for
  4684. * details about the why it is needed.
  4685. * This is a recursive operation - if an existing dentry corresponds to a
  4686. * directory, that directory's new entries are logged too (same behaviour as
  4687. * ext3/4, xfs, f2fs, reiserfs, nilfs2). Note that when logging the inodes
  4688. * the dentries point to we do not lock their i_mutex, otherwise lockdep
  4689. * complains about the following circular lock dependency / possible deadlock:
  4690. *
  4691. * CPU0 CPU1
  4692. * ---- ----
  4693. * lock(&type->i_mutex_dir_key#3/2);
  4694. * lock(sb_internal#2);
  4695. * lock(&type->i_mutex_dir_key#3/2);
  4696. * lock(&sb->s_type->i_mutex_key#14);
  4697. *
  4698. * Where sb_internal is the lock (a counter that works as a lock) acquired by
  4699. * sb_start_intwrite() in btrfs_start_transaction().
  4700. * Not locking i_mutex of the inodes is still safe because:
  4701. *
  4702. * 1) For regular files we log with a mode of LOG_INODE_EXISTS. It's possible
  4703. * that while logging the inode new references (names) are added or removed
  4704. * from the inode, leaving the logged inode item with a link count that does
  4705. * not match the number of logged inode reference items. This is fine because
  4706. * at log replay time we compute the real number of links and correct the
  4707. * link count in the inode item (see replay_one_buffer() and
  4708. * link_to_fixup_dir());
  4709. *
  4710. * 2) For directories we log with a mode of LOG_INODE_ALL. It's possible that
  4711. * while logging the inode's items new items with keys BTRFS_DIR_ITEM_KEY and
  4712. * BTRFS_DIR_INDEX_KEY are added to fs/subvol tree and the logged inode item
  4713. * has a size that doesn't match the sum of the lengths of all the logged
  4714. * names. This does not result in a problem because if a dir_item key is
  4715. * logged but its matching dir_index key is not logged, at log replay time we
  4716. * don't use it to replay the respective name (see replay_one_name()). On the
  4717. * other hand if only the dir_index key ends up being logged, the respective
  4718. * name is added to the fs/subvol tree with both the dir_item and dir_index
  4719. * keys created (see replay_one_name()).
  4720. * The directory's inode item with a wrong i_size is not a problem as well,
  4721. * since we don't use it at log replay time to set the i_size in the inode
  4722. * item of the fs/subvol tree (see overwrite_item()).
  4723. */
  4724. static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
  4725. struct btrfs_root *root,
  4726. struct btrfs_inode *start_inode,
  4727. struct btrfs_log_ctx *ctx)
  4728. {
  4729. struct btrfs_fs_info *fs_info = root->fs_info;
  4730. struct btrfs_root *log = root->log_root;
  4731. struct btrfs_path *path;
  4732. LIST_HEAD(dir_list);
  4733. struct btrfs_dir_list *dir_elem;
  4734. int ret = 0;
  4735. path = btrfs_alloc_path();
  4736. if (!path)
  4737. return -ENOMEM;
  4738. dir_elem = kmalloc(sizeof(*dir_elem), GFP_NOFS);
  4739. if (!dir_elem) {
  4740. btrfs_free_path(path);
  4741. return -ENOMEM;
  4742. }
  4743. dir_elem->ino = btrfs_ino(start_inode);
  4744. list_add_tail(&dir_elem->list, &dir_list);
  4745. while (!list_empty(&dir_list)) {
  4746. struct extent_buffer *leaf;
  4747. struct btrfs_key min_key;
  4748. int nritems;
  4749. int i;
  4750. dir_elem = list_first_entry(&dir_list, struct btrfs_dir_list,
  4751. list);
  4752. if (ret)
  4753. goto next_dir_inode;
  4754. min_key.objectid = dir_elem->ino;
  4755. min_key.type = BTRFS_DIR_ITEM_KEY;
  4756. min_key.offset = 0;
  4757. again:
  4758. btrfs_release_path(path);
  4759. ret = btrfs_search_forward(log, &min_key, path, trans->transid);
  4760. if (ret < 0) {
  4761. goto next_dir_inode;
  4762. } else if (ret > 0) {
  4763. ret = 0;
  4764. goto next_dir_inode;
  4765. }
  4766. process_leaf:
  4767. leaf = path->nodes[0];
  4768. nritems = btrfs_header_nritems(leaf);
  4769. for (i = path->slots[0]; i < nritems; i++) {
  4770. struct btrfs_dir_item *di;
  4771. struct btrfs_key di_key;
  4772. struct inode *di_inode;
  4773. struct btrfs_dir_list *new_dir_elem;
  4774. int log_mode = LOG_INODE_EXISTS;
  4775. int type;
  4776. btrfs_item_key_to_cpu(leaf, &min_key, i);
  4777. if (min_key.objectid != dir_elem->ino ||
  4778. min_key.type != BTRFS_DIR_ITEM_KEY)
  4779. goto next_dir_inode;
  4780. di = btrfs_item_ptr(leaf, i, struct btrfs_dir_item);
  4781. type = btrfs_dir_type(leaf, di);
  4782. if (btrfs_dir_transid(leaf, di) < trans->transid &&
  4783. type != BTRFS_FT_DIR)
  4784. continue;
  4785. btrfs_dir_item_key_to_cpu(leaf, di, &di_key);
  4786. if (di_key.type == BTRFS_ROOT_ITEM_KEY)
  4787. continue;
  4788. btrfs_release_path(path);
  4789. di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
  4790. if (IS_ERR(di_inode)) {
  4791. ret = PTR_ERR(di_inode);
  4792. goto next_dir_inode;
  4793. }
  4794. if (btrfs_inode_in_log(BTRFS_I(di_inode), trans->transid)) {
  4795. iput(di_inode);
  4796. break;
  4797. }
  4798. ctx->log_new_dentries = false;
  4799. if (type == BTRFS_FT_DIR || type == BTRFS_FT_SYMLINK)
  4800. log_mode = LOG_INODE_ALL;
  4801. ret = btrfs_log_inode(trans, root, BTRFS_I(di_inode),
  4802. log_mode, 0, LLONG_MAX, ctx);
  4803. if (!ret &&
  4804. btrfs_must_commit_transaction(trans, BTRFS_I(di_inode)))
  4805. ret = 1;
  4806. iput(di_inode);
  4807. if (ret)
  4808. goto next_dir_inode;
  4809. if (ctx->log_new_dentries) {
  4810. new_dir_elem = kmalloc(sizeof(*new_dir_elem),
  4811. GFP_NOFS);
  4812. if (!new_dir_elem) {
  4813. ret = -ENOMEM;
  4814. goto next_dir_inode;
  4815. }
  4816. new_dir_elem->ino = di_key.objectid;
  4817. list_add_tail(&new_dir_elem->list, &dir_list);
  4818. }
  4819. break;
  4820. }
  4821. if (i == nritems) {
  4822. ret = btrfs_next_leaf(log, path);
  4823. if (ret < 0) {
  4824. goto next_dir_inode;
  4825. } else if (ret > 0) {
  4826. ret = 0;
  4827. goto next_dir_inode;
  4828. }
  4829. goto process_leaf;
  4830. }
  4831. if (min_key.offset < (u64)-1) {
  4832. min_key.offset++;
  4833. goto again;
  4834. }
  4835. next_dir_inode:
  4836. list_del(&dir_elem->list);
  4837. kfree(dir_elem);
  4838. }
  4839. btrfs_free_path(path);
  4840. return ret;
  4841. }
  4842. static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
  4843. struct btrfs_inode *inode,
  4844. struct btrfs_log_ctx *ctx)
  4845. {
  4846. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  4847. int ret;
  4848. struct btrfs_path *path;
  4849. struct btrfs_key key;
  4850. struct btrfs_root *root = inode->root;
  4851. const u64 ino = btrfs_ino(inode);
  4852. path = btrfs_alloc_path();
  4853. if (!path)
  4854. return -ENOMEM;
  4855. path->skip_locking = 1;
  4856. path->search_commit_root = 1;
  4857. key.objectid = ino;
  4858. key.type = BTRFS_INODE_REF_KEY;
  4859. key.offset = 0;
  4860. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4861. if (ret < 0)
  4862. goto out;
  4863. while (true) {
  4864. struct extent_buffer *leaf = path->nodes[0];
  4865. int slot = path->slots[0];
  4866. u32 cur_offset = 0;
  4867. u32 item_size;
  4868. unsigned long ptr;
  4869. if (slot >= btrfs_header_nritems(leaf)) {
  4870. ret = btrfs_next_leaf(root, path);
  4871. if (ret < 0)
  4872. goto out;
  4873. else if (ret > 0)
  4874. break;
  4875. continue;
  4876. }
  4877. btrfs_item_key_to_cpu(leaf, &key, slot);
  4878. /* BTRFS_INODE_EXTREF_KEY is BTRFS_INODE_REF_KEY + 1 */
  4879. if (key.objectid != ino || key.type > BTRFS_INODE_EXTREF_KEY)
  4880. break;
  4881. item_size = btrfs_item_size_nr(leaf, slot);
  4882. ptr = btrfs_item_ptr_offset(leaf, slot);
  4883. while (cur_offset < item_size) {
  4884. struct btrfs_key inode_key;
  4885. struct inode *dir_inode;
  4886. inode_key.type = BTRFS_INODE_ITEM_KEY;
  4887. inode_key.offset = 0;
  4888. if (key.type == BTRFS_INODE_EXTREF_KEY) {
  4889. struct btrfs_inode_extref *extref;
  4890. extref = (struct btrfs_inode_extref *)
  4891. (ptr + cur_offset);
  4892. inode_key.objectid = btrfs_inode_extref_parent(
  4893. leaf, extref);
  4894. cur_offset += sizeof(*extref);
  4895. cur_offset += btrfs_inode_extref_name_len(leaf,
  4896. extref);
  4897. } else {
  4898. inode_key.objectid = key.offset;
  4899. cur_offset = item_size;
  4900. }
  4901. dir_inode = btrfs_iget(fs_info->sb, &inode_key,
  4902. root, NULL);
  4903. /* If parent inode was deleted, skip it. */
  4904. if (IS_ERR(dir_inode))
  4905. continue;
  4906. if (ctx)
  4907. ctx->log_new_dentries = false;
  4908. ret = btrfs_log_inode(trans, root, BTRFS_I(dir_inode),
  4909. LOG_INODE_ALL, 0, LLONG_MAX, ctx);
  4910. if (!ret &&
  4911. btrfs_must_commit_transaction(trans, BTRFS_I(dir_inode)))
  4912. ret = 1;
  4913. if (!ret && ctx && ctx->log_new_dentries)
  4914. ret = log_new_dir_dentries(trans, root,
  4915. BTRFS_I(dir_inode), ctx);
  4916. iput(dir_inode);
  4917. if (ret)
  4918. goto out;
  4919. }
  4920. path->slots[0]++;
  4921. }
  4922. ret = 0;
  4923. out:
  4924. btrfs_free_path(path);
  4925. return ret;
  4926. }
  4927. /*
  4928. * helper function around btrfs_log_inode to make sure newly created
  4929. * parent directories also end up in the log. A minimal inode and backref
  4930. * only logging is done of any parent directories that are older than
  4931. * the last committed transaction
  4932. */
  4933. static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  4934. struct btrfs_root *root,
  4935. struct btrfs_inode *inode,
  4936. struct dentry *parent,
  4937. const loff_t start,
  4938. const loff_t end,
  4939. int inode_only,
  4940. struct btrfs_log_ctx *ctx)
  4941. {
  4942. struct btrfs_fs_info *fs_info = root->fs_info;
  4943. struct super_block *sb;
  4944. struct dentry *old_parent = NULL;
  4945. int ret = 0;
  4946. u64 last_committed = fs_info->last_trans_committed;
  4947. bool log_dentries = false;
  4948. struct btrfs_inode *orig_inode = inode;
  4949. sb = inode->vfs_inode.i_sb;
  4950. if (btrfs_test_opt(fs_info, NOTREELOG)) {
  4951. ret = 1;
  4952. goto end_no_trans;
  4953. }
  4954. /*
  4955. * The prev transaction commit doesn't complete, we need do
  4956. * full commit by ourselves.
  4957. */
  4958. if (fs_info->last_trans_log_full_commit >
  4959. fs_info->last_trans_committed) {
  4960. ret = 1;
  4961. goto end_no_trans;
  4962. }
  4963. if (root != inode->root || btrfs_root_refs(&root->root_item) == 0) {
  4964. ret = 1;
  4965. goto end_no_trans;
  4966. }
  4967. ret = check_parent_dirs_for_sync(trans, inode, parent, sb,
  4968. last_committed);
  4969. if (ret)
  4970. goto end_no_trans;
  4971. if (btrfs_inode_in_log(inode, trans->transid)) {
  4972. ret = BTRFS_NO_LOG_SYNC;
  4973. goto end_no_trans;
  4974. }
  4975. ret = start_log_trans(trans, root, ctx);
  4976. if (ret)
  4977. goto end_no_trans;
  4978. ret = btrfs_log_inode(trans, root, inode, inode_only, start, end, ctx);
  4979. if (ret)
  4980. goto end_trans;
  4981. /*
  4982. * for regular files, if its inode is already on disk, we don't
  4983. * have to worry about the parents at all. This is because
  4984. * we can use the last_unlink_trans field to record renames
  4985. * and other fun in this file.
  4986. */
  4987. if (S_ISREG(inode->vfs_inode.i_mode) &&
  4988. inode->generation <= last_committed &&
  4989. inode->last_unlink_trans <= last_committed) {
  4990. ret = 0;
  4991. goto end_trans;
  4992. }
  4993. if (S_ISDIR(inode->vfs_inode.i_mode) && ctx && ctx->log_new_dentries)
  4994. log_dentries = true;
  4995. /*
  4996. * On unlink we must make sure all our current and old parent directory
  4997. * inodes are fully logged. This is to prevent leaving dangling
  4998. * directory index entries in directories that were our parents but are
  4999. * not anymore. Not doing this results in old parent directory being
  5000. * impossible to delete after log replay (rmdir will always fail with
  5001. * error -ENOTEMPTY).
  5002. *
  5003. * Example 1:
  5004. *
  5005. * mkdir testdir
  5006. * touch testdir/foo
  5007. * ln testdir/foo testdir/bar
  5008. * sync
  5009. * unlink testdir/bar
  5010. * xfs_io -c fsync testdir/foo
  5011. * <power failure>
  5012. * mount fs, triggers log replay
  5013. *
  5014. * If we don't log the parent directory (testdir), after log replay the
  5015. * directory still has an entry pointing to the file inode using the bar
  5016. * name, but a matching BTRFS_INODE_[REF|EXTREF]_KEY does not exist and
  5017. * the file inode has a link count of 1.
  5018. *
  5019. * Example 2:
  5020. *
  5021. * mkdir testdir
  5022. * touch foo
  5023. * ln foo testdir/foo2
  5024. * ln foo testdir/foo3
  5025. * sync
  5026. * unlink testdir/foo3
  5027. * xfs_io -c fsync foo
  5028. * <power failure>
  5029. * mount fs, triggers log replay
  5030. *
  5031. * Similar as the first example, after log replay the parent directory
  5032. * testdir still has an entry pointing to the inode file with name foo3
  5033. * but the file inode does not have a matching BTRFS_INODE_REF_KEY item
  5034. * and has a link count of 2.
  5035. */
  5036. if (inode->last_unlink_trans > last_committed) {
  5037. ret = btrfs_log_all_parents(trans, orig_inode, ctx);
  5038. if (ret)
  5039. goto end_trans;
  5040. }
  5041. while (1) {
  5042. if (!parent || d_really_is_negative(parent) || sb != parent->d_sb)
  5043. break;
  5044. inode = BTRFS_I(d_inode(parent));
  5045. if (root != inode->root)
  5046. break;
  5047. if (inode->generation > last_committed) {
  5048. ret = btrfs_log_inode(trans, root, inode,
  5049. LOG_INODE_EXISTS, 0, LLONG_MAX, ctx);
  5050. if (ret)
  5051. goto end_trans;
  5052. }
  5053. if (IS_ROOT(parent))
  5054. break;
  5055. parent = dget_parent(parent);
  5056. dput(old_parent);
  5057. old_parent = parent;
  5058. }
  5059. if (log_dentries)
  5060. ret = log_new_dir_dentries(trans, root, orig_inode, ctx);
  5061. else
  5062. ret = 0;
  5063. end_trans:
  5064. dput(old_parent);
  5065. if (ret < 0) {
  5066. btrfs_set_log_full_commit(fs_info, trans);
  5067. ret = 1;
  5068. }
  5069. if (ret)
  5070. btrfs_remove_log_ctx(root, ctx);
  5071. btrfs_end_log_trans(root);
  5072. end_no_trans:
  5073. return ret;
  5074. }
  5075. /*
  5076. * it is not safe to log dentry if the chunk root has added new
  5077. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  5078. * If this returns 1, you must commit the transaction to safely get your
  5079. * data on disk.
  5080. */
  5081. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  5082. struct btrfs_root *root, struct dentry *dentry,
  5083. const loff_t start,
  5084. const loff_t end,
  5085. struct btrfs_log_ctx *ctx)
  5086. {
  5087. struct dentry *parent = dget_parent(dentry);
  5088. int ret;
  5089. ret = btrfs_log_inode_parent(trans, root, BTRFS_I(d_inode(dentry)),
  5090. parent, start, end, LOG_INODE_ALL, ctx);
  5091. dput(parent);
  5092. return ret;
  5093. }
  5094. /*
  5095. * should be called during mount to recover any replay any log trees
  5096. * from the FS
  5097. */
  5098. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  5099. {
  5100. int ret;
  5101. struct btrfs_path *path;
  5102. struct btrfs_trans_handle *trans;
  5103. struct btrfs_key key;
  5104. struct btrfs_key found_key;
  5105. struct btrfs_key tmp_key;
  5106. struct btrfs_root *log;
  5107. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  5108. struct walk_control wc = {
  5109. .process_func = process_one_buffer,
  5110. .stage = 0,
  5111. };
  5112. path = btrfs_alloc_path();
  5113. if (!path)
  5114. return -ENOMEM;
  5115. set_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5116. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  5117. if (IS_ERR(trans)) {
  5118. ret = PTR_ERR(trans);
  5119. goto error;
  5120. }
  5121. wc.trans = trans;
  5122. wc.pin = 1;
  5123. ret = walk_log_tree(trans, log_root_tree, &wc);
  5124. if (ret) {
  5125. btrfs_handle_fs_error(fs_info, ret,
  5126. "Failed to pin buffers while recovering log root tree.");
  5127. goto error;
  5128. }
  5129. again:
  5130. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  5131. key.offset = (u64)-1;
  5132. key.type = BTRFS_ROOT_ITEM_KEY;
  5133. while (1) {
  5134. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  5135. if (ret < 0) {
  5136. btrfs_handle_fs_error(fs_info, ret,
  5137. "Couldn't find tree log root.");
  5138. goto error;
  5139. }
  5140. if (ret > 0) {
  5141. if (path->slots[0] == 0)
  5142. break;
  5143. path->slots[0]--;
  5144. }
  5145. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  5146. path->slots[0]);
  5147. btrfs_release_path(path);
  5148. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  5149. break;
  5150. log = btrfs_read_fs_root(log_root_tree, &found_key);
  5151. if (IS_ERR(log)) {
  5152. ret = PTR_ERR(log);
  5153. btrfs_handle_fs_error(fs_info, ret,
  5154. "Couldn't read tree log root.");
  5155. goto error;
  5156. }
  5157. tmp_key.objectid = found_key.offset;
  5158. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  5159. tmp_key.offset = (u64)-1;
  5160. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  5161. if (IS_ERR(wc.replay_dest)) {
  5162. ret = PTR_ERR(wc.replay_dest);
  5163. free_extent_buffer(log->node);
  5164. free_extent_buffer(log->commit_root);
  5165. kfree(log);
  5166. btrfs_handle_fs_error(fs_info, ret,
  5167. "Couldn't read target root for tree log recovery.");
  5168. goto error;
  5169. }
  5170. wc.replay_dest->log_root = log;
  5171. btrfs_record_root_in_trans(trans, wc.replay_dest);
  5172. ret = walk_log_tree(trans, log, &wc);
  5173. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5174. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  5175. path);
  5176. }
  5177. if (!ret && wc.stage == LOG_WALK_REPLAY_ALL) {
  5178. struct btrfs_root *root = wc.replay_dest;
  5179. btrfs_release_path(path);
  5180. /*
  5181. * We have just replayed everything, and the highest
  5182. * objectid of fs roots probably has changed in case
  5183. * some inode_item's got replayed.
  5184. *
  5185. * root->objectid_mutex is not acquired as log replay
  5186. * could only happen during mount.
  5187. */
  5188. ret = btrfs_find_highest_objectid(root,
  5189. &root->highest_objectid);
  5190. }
  5191. key.offset = found_key.offset - 1;
  5192. wc.replay_dest->log_root = NULL;
  5193. free_extent_buffer(log->node);
  5194. free_extent_buffer(log->commit_root);
  5195. kfree(log);
  5196. if (ret)
  5197. goto error;
  5198. if (found_key.offset == 0)
  5199. break;
  5200. }
  5201. btrfs_release_path(path);
  5202. /* step one is to pin it all, step two is to replay just inodes */
  5203. if (wc.pin) {
  5204. wc.pin = 0;
  5205. wc.process_func = replay_one_buffer;
  5206. wc.stage = LOG_WALK_REPLAY_INODES;
  5207. goto again;
  5208. }
  5209. /* step three is to replay everything */
  5210. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  5211. wc.stage++;
  5212. goto again;
  5213. }
  5214. btrfs_free_path(path);
  5215. /* step 4: commit the transaction, which also unpins the blocks */
  5216. ret = btrfs_commit_transaction(trans);
  5217. if (ret)
  5218. return ret;
  5219. free_extent_buffer(log_root_tree->node);
  5220. log_root_tree->log_root = NULL;
  5221. clear_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags);
  5222. kfree(log_root_tree);
  5223. return 0;
  5224. error:
  5225. if (wc.trans)
  5226. btrfs_end_transaction(wc.trans);
  5227. btrfs_free_path(path);
  5228. return ret;
  5229. }
  5230. /*
  5231. * there are some corner cases where we want to force a full
  5232. * commit instead of allowing a directory to be logged.
  5233. *
  5234. * They revolve around files there were unlinked from the directory, and
  5235. * this function updates the parent directory so that a full commit is
  5236. * properly done if it is fsync'd later after the unlinks are done.
  5237. *
  5238. * Must be called before the unlink operations (updates to the subvolume tree,
  5239. * inodes, etc) are done.
  5240. */
  5241. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  5242. struct btrfs_inode *dir, struct btrfs_inode *inode,
  5243. int for_rename)
  5244. {
  5245. /*
  5246. * when we're logging a file, if it hasn't been renamed
  5247. * or unlinked, and its inode is fully committed on disk,
  5248. * we don't have to worry about walking up the directory chain
  5249. * to log its parents.
  5250. *
  5251. * So, we use the last_unlink_trans field to put this transid
  5252. * into the file. When the file is logged we check it and
  5253. * don't log the parents if the file is fully on disk.
  5254. */
  5255. mutex_lock(&inode->log_mutex);
  5256. inode->last_unlink_trans = trans->transid;
  5257. mutex_unlock(&inode->log_mutex);
  5258. /*
  5259. * if this directory was already logged any new
  5260. * names for this file/dir will get recorded
  5261. */
  5262. smp_mb();
  5263. if (dir->logged_trans == trans->transid)
  5264. return;
  5265. /*
  5266. * if the inode we're about to unlink was logged,
  5267. * the log will be properly updated for any new names
  5268. */
  5269. if (inode->logged_trans == trans->transid)
  5270. return;
  5271. /*
  5272. * when renaming files across directories, if the directory
  5273. * there we're unlinking from gets fsync'd later on, there's
  5274. * no way to find the destination directory later and fsync it
  5275. * properly. So, we have to be conservative and force commits
  5276. * so the new name gets discovered.
  5277. */
  5278. if (for_rename)
  5279. goto record;
  5280. /* we can safely do the unlink without any special recording */
  5281. return;
  5282. record:
  5283. mutex_lock(&dir->log_mutex);
  5284. dir->last_unlink_trans = trans->transid;
  5285. mutex_unlock(&dir->log_mutex);
  5286. }
  5287. /*
  5288. * Make sure that if someone attempts to fsync the parent directory of a deleted
  5289. * snapshot, it ends up triggering a transaction commit. This is to guarantee
  5290. * that after replaying the log tree of the parent directory's root we will not
  5291. * see the snapshot anymore and at log replay time we will not see any log tree
  5292. * corresponding to the deleted snapshot's root, which could lead to replaying
  5293. * it after replaying the log tree of the parent directory (which would replay
  5294. * the snapshot delete operation).
  5295. *
  5296. * Must be called before the actual snapshot destroy operation (updates to the
  5297. * parent root and tree of tree roots trees, etc) are done.
  5298. */
  5299. void btrfs_record_snapshot_destroy(struct btrfs_trans_handle *trans,
  5300. struct btrfs_inode *dir)
  5301. {
  5302. mutex_lock(&dir->log_mutex);
  5303. dir->last_unlink_trans = trans->transid;
  5304. mutex_unlock(&dir->log_mutex);
  5305. }
  5306. /*
  5307. * Call this after adding a new name for a file and it will properly
  5308. * update the log to reflect the new name.
  5309. *
  5310. * It will return zero if all goes well, and it will return 1 if a
  5311. * full transaction commit is required.
  5312. */
  5313. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  5314. struct btrfs_inode *inode, struct btrfs_inode *old_dir,
  5315. struct dentry *parent)
  5316. {
  5317. struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
  5318. struct btrfs_root *root = inode->root;
  5319. /*
  5320. * this will force the logging code to walk the dentry chain
  5321. * up for the file
  5322. */
  5323. if (!S_ISDIR(inode->vfs_inode.i_mode))
  5324. inode->last_unlink_trans = trans->transid;
  5325. /*
  5326. * if this inode hasn't been logged and directory we're renaming it
  5327. * from hasn't been logged, we don't need to log it
  5328. */
  5329. if (inode->logged_trans <= fs_info->last_trans_committed &&
  5330. (!old_dir || old_dir->logged_trans <= fs_info->last_trans_committed))
  5331. return 0;
  5332. return btrfs_log_inode_parent(trans, root, inode, parent, 0,
  5333. LLONG_MAX, LOG_INODE_EXISTS, NULL);
  5334. }