tree-log.c 164 KB

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