tree-log.c 156 KB

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