tree-log.c 154 KB

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