tree-log.c 124 KB

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