tree-log.c 161 KB

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