tree-log.c 156 KB

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