tree-log.c 157 KB

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