tree-log.c 155 KB

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