tree-log.c 133 KB

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