tree-log.c 155 KB

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