tree-log.c 159 KB

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