tree-log.c 160 KB

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