tree-log.c 145 KB

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