tree-log.c 164 KB

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