tree-log.c 154 KB

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