tree-log.c 155 KB

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