tree-log.c 154 KB

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