tree-log.c 156 KB

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