tree-log.c 151 KB

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