tree-log.c 161 KB

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