tree-log.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298
  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 "ctree.h"
  21. #include "transaction.h"
  22. #include "disk-io.h"
  23. #include "locking.h"
  24. #include "print-tree.h"
  25. #include "compat.h"
  26. #include "tree-log.h"
  27. /* magic values for the inode_only field in btrfs_log_inode:
  28. *
  29. * LOG_INODE_ALL means to log everything
  30. * LOG_INODE_EXISTS means to log just enough to recreate the inode
  31. * during log replay
  32. */
  33. #define LOG_INODE_ALL 0
  34. #define LOG_INODE_EXISTS 1
  35. /*
  36. * directory trouble cases
  37. *
  38. * 1) on rename or unlink, if the inode being unlinked isn't in the fsync
  39. * log, we must force a full commit before doing an fsync of the directory
  40. * where the unlink was done.
  41. * ---> record transid of last unlink/rename per directory
  42. *
  43. * mkdir foo/some_dir
  44. * normal commit
  45. * rename foo/some_dir foo2/some_dir
  46. * mkdir foo/some_dir
  47. * fsync foo/some_dir/some_file
  48. *
  49. * The fsync above will unlink the original some_dir without recording
  50. * it in its new location (foo2). After a crash, some_dir will be gone
  51. * unless the fsync of some_file forces a full commit
  52. *
  53. * 2) we must log any new names for any file or dir that is in the fsync
  54. * log. ---> check inode while renaming/linking.
  55. *
  56. * 2a) we must log any new names for any file or dir during rename
  57. * when the directory they are being removed from was logged.
  58. * ---> check inode and old parent dir during rename
  59. *
  60. * 2a is actually the more important variant. With the extra logging
  61. * a crash might unlink the old name without recreating the new one
  62. *
  63. * 3) after a crash, we must go through any directories with a link count
  64. * of zero and redo the rm -rf
  65. *
  66. * mkdir f1/foo
  67. * normal commit
  68. * rm -rf f1/foo
  69. * fsync(f1)
  70. *
  71. * The directory f1 was fully removed from the FS, but fsync was never
  72. * called on f1, only its parent dir. After a crash the rm -rf must
  73. * be replayed. This must be able to recurse down the entire
  74. * directory tree. The inode link count fixup code takes care of the
  75. * ugly details.
  76. */
  77. /*
  78. * stages for the tree walking. The first
  79. * stage (0) is to only pin down the blocks we find
  80. * the second stage (1) is to make sure that all the inodes
  81. * we find in the log are created in the subvolume.
  82. *
  83. * The last stage is to deal with directories and links and extents
  84. * and all the other fun semantics
  85. */
  86. #define LOG_WALK_PIN_ONLY 0
  87. #define LOG_WALK_REPLAY_INODES 1
  88. #define LOG_WALK_REPLAY_ALL 2
  89. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  90. struct btrfs_root *root, struct inode *inode,
  91. int inode_only);
  92. static int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  93. struct btrfs_root *root,
  94. struct btrfs_path *path, u64 objectid);
  95. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  96. struct btrfs_root *root,
  97. struct btrfs_root *log,
  98. struct btrfs_path *path,
  99. u64 dirid, int del_all);
  100. /*
  101. * tree logging is a special write ahead log used to make sure that
  102. * fsyncs and O_SYNCs can happen without doing full tree commits.
  103. *
  104. * Full tree commits are expensive because they require commonly
  105. * modified blocks to be recowed, creating many dirty pages in the
  106. * extent tree an 4x-6x higher write load than ext3.
  107. *
  108. * Instead of doing a tree commit on every fsync, we use the
  109. * key ranges and transaction ids to find items for a given file or directory
  110. * that have changed in this transaction. Those items are copied into
  111. * a special tree (one per subvolume root), that tree is written to disk
  112. * and then the fsync is considered complete.
  113. *
  114. * After a crash, items are copied out of the log-tree back into the
  115. * subvolume tree. Any file data extents found are recorded in the extent
  116. * allocation tree, and the log-tree freed.
  117. *
  118. * The log tree is read three times, once to pin down all the extents it is
  119. * using in ram and once, once to create all the inodes logged in the tree
  120. * and once to do all the other items.
  121. */
  122. /*
  123. * start a sub transaction and setup the log tree
  124. * this increments the log tree writer count to make the people
  125. * syncing the tree wait for us to finish
  126. */
  127. static int start_log_trans(struct btrfs_trans_handle *trans,
  128. struct btrfs_root *root)
  129. {
  130. int ret;
  131. int err = 0;
  132. mutex_lock(&root->log_mutex);
  133. if (root->log_root) {
  134. if (!root->log_start_pid) {
  135. root->log_start_pid = current->pid;
  136. root->log_multiple_pids = false;
  137. } else if (root->log_start_pid != current->pid) {
  138. root->log_multiple_pids = true;
  139. }
  140. root->log_batch++;
  141. atomic_inc(&root->log_writers);
  142. mutex_unlock(&root->log_mutex);
  143. return 0;
  144. }
  145. root->log_multiple_pids = false;
  146. root->log_start_pid = current->pid;
  147. mutex_lock(&root->fs_info->tree_log_mutex);
  148. if (!root->fs_info->log_root_tree) {
  149. ret = btrfs_init_log_root_tree(trans, root->fs_info);
  150. if (ret)
  151. err = ret;
  152. }
  153. if (err == 0 && !root->log_root) {
  154. ret = btrfs_add_log_tree(trans, root);
  155. if (ret)
  156. err = ret;
  157. }
  158. mutex_unlock(&root->fs_info->tree_log_mutex);
  159. root->log_batch++;
  160. atomic_inc(&root->log_writers);
  161. mutex_unlock(&root->log_mutex);
  162. return err;
  163. }
  164. /*
  165. * returns 0 if there was a log transaction running and we were able
  166. * to join, or returns -ENOENT if there were not transactions
  167. * in progress
  168. */
  169. static int join_running_log_trans(struct btrfs_root *root)
  170. {
  171. int ret = -ENOENT;
  172. smp_mb();
  173. if (!root->log_root)
  174. return -ENOENT;
  175. mutex_lock(&root->log_mutex);
  176. if (root->log_root) {
  177. ret = 0;
  178. atomic_inc(&root->log_writers);
  179. }
  180. mutex_unlock(&root->log_mutex);
  181. return ret;
  182. }
  183. /*
  184. * This either makes the current running log transaction wait
  185. * until you call btrfs_end_log_trans() or it makes any future
  186. * log transactions wait until you call btrfs_end_log_trans()
  187. */
  188. int btrfs_pin_log_trans(struct btrfs_root *root)
  189. {
  190. int ret = -ENOENT;
  191. mutex_lock(&root->log_mutex);
  192. atomic_inc(&root->log_writers);
  193. mutex_unlock(&root->log_mutex);
  194. return ret;
  195. }
  196. /*
  197. * indicate we're done making changes to the log tree
  198. * and wake up anyone waiting to do a sync
  199. */
  200. int btrfs_end_log_trans(struct btrfs_root *root)
  201. {
  202. if (atomic_dec_and_test(&root->log_writers)) {
  203. smp_mb();
  204. if (waitqueue_active(&root->log_writer_wait))
  205. wake_up(&root->log_writer_wait);
  206. }
  207. return 0;
  208. }
  209. /*
  210. * the walk control struct is used to pass state down the chain when
  211. * processing the log tree. The stage field tells us which part
  212. * of the log tree processing we are currently doing. The others
  213. * are state fields used for that specific part
  214. */
  215. struct walk_control {
  216. /* should we free the extent on disk when done? This is used
  217. * at transaction commit time while freeing a log tree
  218. */
  219. int free;
  220. /* should we write out the extent buffer? This is used
  221. * while flushing the log tree to disk during a sync
  222. */
  223. int write;
  224. /* should we wait for the extent buffer io to finish? Also used
  225. * while flushing the log tree to disk for a sync
  226. */
  227. int wait;
  228. /* pin only walk, we record which extents on disk belong to the
  229. * log trees
  230. */
  231. int pin;
  232. /* what stage of the replay code we're currently in */
  233. int stage;
  234. /* the root we are currently replaying */
  235. struct btrfs_root *replay_dest;
  236. /* the trans handle for the current replay */
  237. struct btrfs_trans_handle *trans;
  238. /* the function that gets used to process blocks we find in the
  239. * tree. Note the extent_buffer might not be up to date when it is
  240. * passed in, and it must be checked or read if you need the data
  241. * inside it
  242. */
  243. int (*process_func)(struct btrfs_root *log, struct extent_buffer *eb,
  244. struct walk_control *wc, u64 gen);
  245. };
  246. /*
  247. * process_func used to pin down extents, write them or wait on them
  248. */
  249. static int process_one_buffer(struct btrfs_root *log,
  250. struct extent_buffer *eb,
  251. struct walk_control *wc, u64 gen)
  252. {
  253. if (wc->pin)
  254. btrfs_pin_extent(log->fs_info->extent_root,
  255. eb->start, eb->len, 0);
  256. if (btrfs_buffer_uptodate(eb, gen)) {
  257. if (wc->write)
  258. btrfs_write_tree_block(eb);
  259. if (wc->wait)
  260. btrfs_wait_tree_block_writeback(eb);
  261. }
  262. return 0;
  263. }
  264. /*
  265. * Item overwrite used by replay and tree logging. eb, slot and key all refer
  266. * to the src data we are copying out.
  267. *
  268. * root is the tree we are copying into, and path is a scratch
  269. * path for use in this function (it should be released on entry and
  270. * will be released on exit).
  271. *
  272. * If the key is already in the destination tree the existing item is
  273. * overwritten. If the existing item isn't big enough, it is extended.
  274. * If it is too large, it is truncated.
  275. *
  276. * If the key isn't in the destination yet, a new item is inserted.
  277. */
  278. static noinline int overwrite_item(struct btrfs_trans_handle *trans,
  279. struct btrfs_root *root,
  280. struct btrfs_path *path,
  281. struct extent_buffer *eb, int slot,
  282. struct btrfs_key *key)
  283. {
  284. int ret;
  285. u32 item_size;
  286. u64 saved_i_size = 0;
  287. int save_old_i_size = 0;
  288. unsigned long src_ptr;
  289. unsigned long dst_ptr;
  290. int overwrite_root = 0;
  291. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  292. overwrite_root = 1;
  293. item_size = btrfs_item_size_nr(eb, slot);
  294. src_ptr = btrfs_item_ptr_offset(eb, slot);
  295. /* look for the key in the destination tree */
  296. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  297. if (ret == 0) {
  298. char *src_copy;
  299. char *dst_copy;
  300. u32 dst_size = btrfs_item_size_nr(path->nodes[0],
  301. path->slots[0]);
  302. if (dst_size != item_size)
  303. goto insert;
  304. if (item_size == 0) {
  305. btrfs_release_path(root, path);
  306. return 0;
  307. }
  308. dst_copy = kmalloc(item_size, GFP_NOFS);
  309. src_copy = kmalloc(item_size, GFP_NOFS);
  310. if (!dst_copy || !src_copy) {
  311. btrfs_release_path(root, path);
  312. kfree(dst_copy);
  313. kfree(src_copy);
  314. return -ENOMEM;
  315. }
  316. read_extent_buffer(eb, src_copy, src_ptr, item_size);
  317. dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  318. read_extent_buffer(path->nodes[0], dst_copy, dst_ptr,
  319. item_size);
  320. ret = memcmp(dst_copy, src_copy, item_size);
  321. kfree(dst_copy);
  322. kfree(src_copy);
  323. /*
  324. * they have the same contents, just return, this saves
  325. * us from cowing blocks in the destination tree and doing
  326. * extra writes that may not have been done by a previous
  327. * sync
  328. */
  329. if (ret == 0) {
  330. btrfs_release_path(root, path);
  331. return 0;
  332. }
  333. }
  334. insert:
  335. btrfs_release_path(root, path);
  336. /* try to insert the key into the destination tree */
  337. ret = btrfs_insert_empty_item(trans, root, path,
  338. key, item_size);
  339. /* make sure any existing item is the correct size */
  340. if (ret == -EEXIST) {
  341. u32 found_size;
  342. found_size = btrfs_item_size_nr(path->nodes[0],
  343. path->slots[0]);
  344. if (found_size > item_size) {
  345. btrfs_truncate_item(trans, root, path, item_size, 1);
  346. } else if (found_size < item_size) {
  347. ret = btrfs_extend_item(trans, root, path,
  348. item_size - found_size);
  349. BUG_ON(ret);
  350. }
  351. } else if (ret) {
  352. return ret;
  353. }
  354. dst_ptr = btrfs_item_ptr_offset(path->nodes[0],
  355. path->slots[0]);
  356. /* don't overwrite an existing inode if the generation number
  357. * was logged as zero. This is done when the tree logging code
  358. * is just logging an inode to make sure it exists after recovery.
  359. *
  360. * Also, don't overwrite i_size on directories during replay.
  361. * log replay inserts and removes directory items based on the
  362. * state of the tree found in the subvolume, and i_size is modified
  363. * as it goes
  364. */
  365. if (key->type == BTRFS_INODE_ITEM_KEY && ret == -EEXIST) {
  366. struct btrfs_inode_item *src_item;
  367. struct btrfs_inode_item *dst_item;
  368. src_item = (struct btrfs_inode_item *)src_ptr;
  369. dst_item = (struct btrfs_inode_item *)dst_ptr;
  370. if (btrfs_inode_generation(eb, src_item) == 0)
  371. goto no_copy;
  372. if (overwrite_root &&
  373. S_ISDIR(btrfs_inode_mode(eb, src_item)) &&
  374. S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) {
  375. save_old_i_size = 1;
  376. saved_i_size = btrfs_inode_size(path->nodes[0],
  377. dst_item);
  378. }
  379. }
  380. copy_extent_buffer(path->nodes[0], eb, dst_ptr,
  381. src_ptr, item_size);
  382. if (save_old_i_size) {
  383. struct btrfs_inode_item *dst_item;
  384. dst_item = (struct btrfs_inode_item *)dst_ptr;
  385. btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size);
  386. }
  387. /* make sure the generation is filled in */
  388. if (key->type == BTRFS_INODE_ITEM_KEY) {
  389. struct btrfs_inode_item *dst_item;
  390. dst_item = (struct btrfs_inode_item *)dst_ptr;
  391. if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) {
  392. btrfs_set_inode_generation(path->nodes[0], dst_item,
  393. trans->transid);
  394. }
  395. }
  396. no_copy:
  397. btrfs_mark_buffer_dirty(path->nodes[0]);
  398. btrfs_release_path(root, path);
  399. return 0;
  400. }
  401. /*
  402. * simple helper to read an inode off the disk from a given root
  403. * This can only be called for subvolume roots and not for the log
  404. */
  405. static noinline struct inode *read_one_inode(struct btrfs_root *root,
  406. u64 objectid)
  407. {
  408. struct btrfs_key key;
  409. struct inode *inode;
  410. key.objectid = objectid;
  411. key.type = BTRFS_INODE_ITEM_KEY;
  412. key.offset = 0;
  413. inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
  414. if (IS_ERR(inode)) {
  415. inode = NULL;
  416. } else if (is_bad_inode(inode)) {
  417. iput(inode);
  418. inode = NULL;
  419. }
  420. return inode;
  421. }
  422. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  423. * subvolume 'root'. path is released on entry and should be released
  424. * on exit.
  425. *
  426. * extents in the log tree have not been allocated out of the extent
  427. * tree yet. So, this completes the allocation, taking a reference
  428. * as required if the extent already exists or creating a new extent
  429. * if it isn't in the extent allocation tree yet.
  430. *
  431. * The extent is inserted into the file, dropping any existing extents
  432. * from the file that overlap the new one.
  433. */
  434. static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
  435. struct btrfs_root *root,
  436. struct btrfs_path *path,
  437. struct extent_buffer *eb, int slot,
  438. struct btrfs_key *key)
  439. {
  440. int found_type;
  441. u64 mask = root->sectorsize - 1;
  442. u64 extent_end;
  443. u64 alloc_hint;
  444. u64 start = key->offset;
  445. u64 saved_nbytes;
  446. struct btrfs_file_extent_item *item;
  447. struct inode *inode = NULL;
  448. unsigned long size;
  449. int ret = 0;
  450. item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  451. found_type = btrfs_file_extent_type(eb, item);
  452. if (found_type == BTRFS_FILE_EXTENT_REG ||
  453. found_type == BTRFS_FILE_EXTENT_PREALLOC)
  454. extent_end = start + btrfs_file_extent_num_bytes(eb, item);
  455. else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  456. size = btrfs_file_extent_inline_len(eb, item);
  457. extent_end = (start + size + mask) & ~mask;
  458. } else {
  459. ret = 0;
  460. goto out;
  461. }
  462. inode = read_one_inode(root, key->objectid);
  463. if (!inode) {
  464. ret = -EIO;
  465. goto out;
  466. }
  467. /*
  468. * first check to see if we already have this extent in the
  469. * file. This must be done before the btrfs_drop_extents run
  470. * so we don't try to drop this extent.
  471. */
  472. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  473. start, 0);
  474. if (ret == 0 &&
  475. (found_type == BTRFS_FILE_EXTENT_REG ||
  476. found_type == BTRFS_FILE_EXTENT_PREALLOC)) {
  477. struct btrfs_file_extent_item cmp1;
  478. struct btrfs_file_extent_item cmp2;
  479. struct btrfs_file_extent_item *existing;
  480. struct extent_buffer *leaf;
  481. leaf = path->nodes[0];
  482. existing = btrfs_item_ptr(leaf, path->slots[0],
  483. struct btrfs_file_extent_item);
  484. read_extent_buffer(eb, &cmp1, (unsigned long)item,
  485. sizeof(cmp1));
  486. read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
  487. sizeof(cmp2));
  488. /*
  489. * we already have a pointer to this exact extent,
  490. * we don't have to do anything
  491. */
  492. if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
  493. btrfs_release_path(root, path);
  494. goto out;
  495. }
  496. }
  497. btrfs_release_path(root, path);
  498. saved_nbytes = inode_get_bytes(inode);
  499. /* drop any overlapping extents */
  500. ret = btrfs_drop_extents(trans, inode, start, extent_end,
  501. &alloc_hint, 1);
  502. BUG_ON(ret);
  503. if (found_type == BTRFS_FILE_EXTENT_REG ||
  504. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  505. u64 offset;
  506. unsigned long dest_offset;
  507. struct btrfs_key ins;
  508. ret = btrfs_insert_empty_item(trans, root, path, key,
  509. sizeof(*item));
  510. BUG_ON(ret);
  511. dest_offset = btrfs_item_ptr_offset(path->nodes[0],
  512. path->slots[0]);
  513. copy_extent_buffer(path->nodes[0], eb, dest_offset,
  514. (unsigned long)item, sizeof(*item));
  515. ins.objectid = btrfs_file_extent_disk_bytenr(eb, item);
  516. ins.offset = btrfs_file_extent_disk_num_bytes(eb, item);
  517. ins.type = BTRFS_EXTENT_ITEM_KEY;
  518. offset = key->offset - btrfs_file_extent_offset(eb, item);
  519. if (ins.objectid > 0) {
  520. u64 csum_start;
  521. u64 csum_end;
  522. LIST_HEAD(ordered_sums);
  523. /*
  524. * is this extent already allocated in the extent
  525. * allocation tree? If so, just add a reference
  526. */
  527. ret = btrfs_lookup_extent(root, ins.objectid,
  528. ins.offset);
  529. if (ret == 0) {
  530. ret = btrfs_inc_extent_ref(trans, root,
  531. ins.objectid, ins.offset,
  532. 0, root->root_key.objectid,
  533. key->objectid, offset);
  534. } else {
  535. /*
  536. * insert the extent pointer in the extent
  537. * allocation tree
  538. */
  539. ret = btrfs_alloc_logged_file_extent(trans,
  540. root, root->root_key.objectid,
  541. key->objectid, offset, &ins);
  542. BUG_ON(ret);
  543. }
  544. btrfs_release_path(root, path);
  545. if (btrfs_file_extent_compression(eb, item)) {
  546. csum_start = ins.objectid;
  547. csum_end = csum_start + ins.offset;
  548. } else {
  549. csum_start = ins.objectid +
  550. btrfs_file_extent_offset(eb, item);
  551. csum_end = csum_start +
  552. btrfs_file_extent_num_bytes(eb, item);
  553. }
  554. ret = btrfs_lookup_csums_range(root->log_root,
  555. csum_start, csum_end - 1,
  556. &ordered_sums);
  557. BUG_ON(ret);
  558. while (!list_empty(&ordered_sums)) {
  559. struct btrfs_ordered_sum *sums;
  560. sums = list_entry(ordered_sums.next,
  561. struct btrfs_ordered_sum,
  562. list);
  563. ret = btrfs_csum_file_blocks(trans,
  564. root->fs_info->csum_root,
  565. sums);
  566. BUG_ON(ret);
  567. list_del(&sums->list);
  568. kfree(sums);
  569. }
  570. } else {
  571. btrfs_release_path(root, path);
  572. }
  573. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  574. /* inline extents are easy, we just overwrite them */
  575. ret = overwrite_item(trans, root, path, eb, slot, key);
  576. BUG_ON(ret);
  577. }
  578. inode_set_bytes(inode, saved_nbytes);
  579. btrfs_update_inode(trans, root, inode);
  580. out:
  581. if (inode)
  582. iput(inode);
  583. return ret;
  584. }
  585. /*
  586. * when cleaning up conflicts between the directory names in the
  587. * subvolume, directory names in the log and directory names in the
  588. * inode back references, we may have to unlink inodes from directories.
  589. *
  590. * This is a helper function to do the unlink of a specific directory
  591. * item
  592. */
  593. static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
  594. struct btrfs_root *root,
  595. struct btrfs_path *path,
  596. struct inode *dir,
  597. struct btrfs_dir_item *di)
  598. {
  599. struct inode *inode;
  600. char *name;
  601. int name_len;
  602. struct extent_buffer *leaf;
  603. struct btrfs_key location;
  604. int ret;
  605. leaf = path->nodes[0];
  606. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  607. name_len = btrfs_dir_name_len(leaf, di);
  608. name = kmalloc(name_len, GFP_NOFS);
  609. if (!name)
  610. return -ENOMEM;
  611. read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
  612. btrfs_release_path(root, path);
  613. inode = read_one_inode(root, location.objectid);
  614. BUG_ON(!inode);
  615. ret = link_to_fixup_dir(trans, root, path, location.objectid);
  616. BUG_ON(ret);
  617. ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  618. BUG_ON(ret);
  619. kfree(name);
  620. iput(inode);
  621. return ret;
  622. }
  623. /*
  624. * helper function to see if a given name and sequence number found
  625. * in an inode back reference are already in a directory and correctly
  626. * point to this inode
  627. */
  628. static noinline int inode_in_dir(struct btrfs_root *root,
  629. struct btrfs_path *path,
  630. u64 dirid, u64 objectid, u64 index,
  631. const char *name, int name_len)
  632. {
  633. struct btrfs_dir_item *di;
  634. struct btrfs_key location;
  635. int match = 0;
  636. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  637. index, name, name_len, 0);
  638. if (di && !IS_ERR(di)) {
  639. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  640. if (location.objectid != objectid)
  641. goto out;
  642. } else
  643. goto out;
  644. btrfs_release_path(root, path);
  645. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
  646. if (di && !IS_ERR(di)) {
  647. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  648. if (location.objectid != objectid)
  649. goto out;
  650. } else
  651. goto out;
  652. match = 1;
  653. out:
  654. btrfs_release_path(root, path);
  655. return match;
  656. }
  657. /*
  658. * helper function to check a log tree for a named back reference in
  659. * an inode. This is used to decide if a back reference that is
  660. * found in the subvolume conflicts with what we find in the log.
  661. *
  662. * inode backreferences may have multiple refs in a single item,
  663. * during replay we process one reference at a time, and we don't
  664. * want to delete valid links to a file from the subvolume if that
  665. * link is also in the log.
  666. */
  667. static noinline int backref_in_log(struct btrfs_root *log,
  668. struct btrfs_key *key,
  669. char *name, int namelen)
  670. {
  671. struct btrfs_path *path;
  672. struct btrfs_inode_ref *ref;
  673. unsigned long ptr;
  674. unsigned long ptr_end;
  675. unsigned long name_ptr;
  676. int found_name_len;
  677. int item_size;
  678. int ret;
  679. int match = 0;
  680. path = btrfs_alloc_path();
  681. if (!path)
  682. return -ENOMEM;
  683. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  684. if (ret != 0)
  685. goto out;
  686. item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  687. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  688. ptr_end = ptr + item_size;
  689. while (ptr < ptr_end) {
  690. ref = (struct btrfs_inode_ref *)ptr;
  691. found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
  692. if (found_name_len == namelen) {
  693. name_ptr = (unsigned long)(ref + 1);
  694. ret = memcmp_extent_buffer(path->nodes[0], name,
  695. name_ptr, namelen);
  696. if (ret == 0) {
  697. match = 1;
  698. goto out;
  699. }
  700. }
  701. ptr = (unsigned long)(ref + 1) + found_name_len;
  702. }
  703. out:
  704. btrfs_free_path(path);
  705. return match;
  706. }
  707. /*
  708. * replay one inode back reference item found in the log tree.
  709. * eb, slot and key refer to the buffer and key found in the log tree.
  710. * root is the destination we are replaying into, and path is for temp
  711. * use by this function. (it should be released on return).
  712. */
  713. static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
  714. struct btrfs_root *root,
  715. struct btrfs_root *log,
  716. struct btrfs_path *path,
  717. struct extent_buffer *eb, int slot,
  718. struct btrfs_key *key)
  719. {
  720. struct inode *dir;
  721. int ret;
  722. struct btrfs_inode_ref *ref;
  723. struct btrfs_dir_item *di;
  724. struct inode *inode;
  725. char *name;
  726. int namelen;
  727. unsigned long ref_ptr;
  728. unsigned long ref_end;
  729. /*
  730. * it is possible that we didn't log all the parent directories
  731. * for a given inode. If we don't find the dir, just don't
  732. * copy the back ref in. The link count fixup code will take
  733. * care of the rest
  734. */
  735. dir = read_one_inode(root, key->offset);
  736. if (!dir)
  737. return -ENOENT;
  738. inode = read_one_inode(root, key->objectid);
  739. BUG_ON(!inode);
  740. ref_ptr = btrfs_item_ptr_offset(eb, slot);
  741. ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
  742. again:
  743. ref = (struct btrfs_inode_ref *)ref_ptr;
  744. namelen = btrfs_inode_ref_name_len(eb, ref);
  745. name = kmalloc(namelen, GFP_NOFS);
  746. BUG_ON(!name);
  747. read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
  748. /* if we already have a perfect match, we're done */
  749. if (inode_in_dir(root, path, dir->i_ino, inode->i_ino,
  750. btrfs_inode_ref_index(eb, ref),
  751. name, namelen)) {
  752. goto out;
  753. }
  754. /*
  755. * look for a conflicting back reference in the metadata.
  756. * if we find one we have to unlink that name of the file
  757. * before we add our new link. Later on, we overwrite any
  758. * existing back reference, and we don't want to create
  759. * dangling pointers in the directory.
  760. */
  761. conflict_again:
  762. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  763. if (ret == 0) {
  764. char *victim_name;
  765. int victim_name_len;
  766. struct btrfs_inode_ref *victim_ref;
  767. unsigned long ptr;
  768. unsigned long ptr_end;
  769. struct extent_buffer *leaf = path->nodes[0];
  770. /* are we trying to overwrite a back ref for the root directory
  771. * if so, just jump out, we're done
  772. */
  773. if (key->objectid == key->offset)
  774. goto out_nowrite;
  775. /* check all the names in this back reference to see
  776. * if they are in the log. if so, we allow them to stay
  777. * otherwise they must be unlinked as a conflict
  778. */
  779. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  780. ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
  781. while (ptr < ptr_end) {
  782. victim_ref = (struct btrfs_inode_ref *)ptr;
  783. victim_name_len = btrfs_inode_ref_name_len(leaf,
  784. victim_ref);
  785. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  786. BUG_ON(!victim_name);
  787. read_extent_buffer(leaf, victim_name,
  788. (unsigned long)(victim_ref + 1),
  789. victim_name_len);
  790. if (!backref_in_log(log, key, victim_name,
  791. victim_name_len)) {
  792. btrfs_inc_nlink(inode);
  793. btrfs_release_path(root, path);
  794. ret = btrfs_unlink_inode(trans, root, dir,
  795. inode, victim_name,
  796. victim_name_len);
  797. kfree(victim_name);
  798. btrfs_release_path(root, path);
  799. goto conflict_again;
  800. }
  801. kfree(victim_name);
  802. ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
  803. }
  804. BUG_ON(ret);
  805. }
  806. btrfs_release_path(root, path);
  807. /* look for a conflicting sequence number */
  808. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  809. btrfs_inode_ref_index(eb, ref),
  810. name, namelen, 0);
  811. if (di && !IS_ERR(di)) {
  812. ret = drop_one_dir_item(trans, root, path, dir, di);
  813. BUG_ON(ret);
  814. }
  815. btrfs_release_path(root, path);
  816. /* look for a conflicting name */
  817. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  818. name, namelen, 0);
  819. if (di && !IS_ERR(di)) {
  820. ret = drop_one_dir_item(trans, root, path, dir, di);
  821. BUG_ON(ret);
  822. }
  823. btrfs_release_path(root, path);
  824. /* insert our name */
  825. ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
  826. btrfs_inode_ref_index(eb, ref));
  827. BUG_ON(ret);
  828. btrfs_update_inode(trans, root, inode);
  829. out:
  830. ref_ptr = (unsigned long)(ref + 1) + namelen;
  831. kfree(name);
  832. if (ref_ptr < ref_end)
  833. goto again;
  834. /* finally write the back reference in the inode */
  835. ret = overwrite_item(trans, root, path, eb, slot, key);
  836. BUG_ON(ret);
  837. out_nowrite:
  838. btrfs_release_path(root, path);
  839. iput(dir);
  840. iput(inode);
  841. return 0;
  842. }
  843. static int insert_orphan_item(struct btrfs_trans_handle *trans,
  844. struct btrfs_root *root, u64 offset)
  845. {
  846. int ret;
  847. ret = btrfs_find_orphan_item(root, offset);
  848. if (ret > 0)
  849. ret = btrfs_insert_orphan_item(trans, root, offset);
  850. return ret;
  851. }
  852. /*
  853. * There are a few corners where the link count of the file can't
  854. * be properly maintained during replay. So, instead of adding
  855. * lots of complexity to the log code, we just scan the backrefs
  856. * for any file that has been through replay.
  857. *
  858. * The scan will update the link count on the inode to reflect the
  859. * number of back refs found. If it goes down to zero, the iput
  860. * will free the inode.
  861. */
  862. static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
  863. struct btrfs_root *root,
  864. struct inode *inode)
  865. {
  866. struct btrfs_path *path;
  867. int ret;
  868. struct btrfs_key key;
  869. u64 nlink = 0;
  870. unsigned long ptr;
  871. unsigned long ptr_end;
  872. int name_len;
  873. key.objectid = inode->i_ino;
  874. key.type = BTRFS_INODE_REF_KEY;
  875. key.offset = (u64)-1;
  876. path = btrfs_alloc_path();
  877. if (!path)
  878. return -ENOMEM;
  879. while (1) {
  880. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  881. if (ret < 0)
  882. break;
  883. if (ret > 0) {
  884. if (path->slots[0] == 0)
  885. break;
  886. path->slots[0]--;
  887. }
  888. btrfs_item_key_to_cpu(path->nodes[0], &key,
  889. path->slots[0]);
  890. if (key.objectid != inode->i_ino ||
  891. key.type != BTRFS_INODE_REF_KEY)
  892. break;
  893. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  894. ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
  895. path->slots[0]);
  896. while (ptr < ptr_end) {
  897. struct btrfs_inode_ref *ref;
  898. ref = (struct btrfs_inode_ref *)ptr;
  899. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  900. ref);
  901. ptr = (unsigned long)(ref + 1) + name_len;
  902. nlink++;
  903. }
  904. if (key.offset == 0)
  905. break;
  906. key.offset--;
  907. btrfs_release_path(root, path);
  908. }
  909. btrfs_release_path(root, path);
  910. if (nlink != inode->i_nlink) {
  911. inode->i_nlink = nlink;
  912. btrfs_update_inode(trans, root, inode);
  913. }
  914. BTRFS_I(inode)->index_cnt = (u64)-1;
  915. if (inode->i_nlink == 0) {
  916. if (S_ISDIR(inode->i_mode)) {
  917. ret = replay_dir_deletes(trans, root, NULL, path,
  918. inode->i_ino, 1);
  919. BUG_ON(ret);
  920. }
  921. ret = insert_orphan_item(trans, root, inode->i_ino);
  922. BUG_ON(ret);
  923. }
  924. btrfs_free_path(path);
  925. return 0;
  926. }
  927. static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
  928. struct btrfs_root *root,
  929. struct btrfs_path *path)
  930. {
  931. int ret;
  932. struct btrfs_key key;
  933. struct inode *inode;
  934. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  935. key.type = BTRFS_ORPHAN_ITEM_KEY;
  936. key.offset = (u64)-1;
  937. while (1) {
  938. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  939. if (ret < 0)
  940. break;
  941. if (ret == 1) {
  942. if (path->slots[0] == 0)
  943. break;
  944. path->slots[0]--;
  945. }
  946. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  947. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  948. key.type != BTRFS_ORPHAN_ITEM_KEY)
  949. break;
  950. ret = btrfs_del_item(trans, root, path);
  951. BUG_ON(ret);
  952. btrfs_release_path(root, path);
  953. inode = read_one_inode(root, key.offset);
  954. BUG_ON(!inode);
  955. ret = fixup_inode_link_count(trans, root, inode);
  956. BUG_ON(ret);
  957. iput(inode);
  958. /*
  959. * fixup on a directory may create new entries,
  960. * make sure we always look for the highset possible
  961. * offset
  962. */
  963. key.offset = (u64)-1;
  964. }
  965. btrfs_release_path(root, path);
  966. return 0;
  967. }
  968. /*
  969. * record a given inode in the fixup dir so we can check its link
  970. * count when replay is done. The link count is incremented here
  971. * so the inode won't go away until we check it
  972. */
  973. static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  974. struct btrfs_root *root,
  975. struct btrfs_path *path,
  976. u64 objectid)
  977. {
  978. struct btrfs_key key;
  979. int ret = 0;
  980. struct inode *inode;
  981. inode = read_one_inode(root, objectid);
  982. BUG_ON(!inode);
  983. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  984. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  985. key.offset = objectid;
  986. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  987. btrfs_release_path(root, path);
  988. if (ret == 0) {
  989. btrfs_inc_nlink(inode);
  990. btrfs_update_inode(trans, root, inode);
  991. } else if (ret == -EEXIST) {
  992. ret = 0;
  993. } else {
  994. BUG();
  995. }
  996. iput(inode);
  997. return ret;
  998. }
  999. /*
  1000. * when replaying the log for a directory, we only insert names
  1001. * for inodes that actually exist. This means an fsync on a directory
  1002. * does not implicitly fsync all the new files in it
  1003. */
  1004. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  1005. struct btrfs_root *root,
  1006. struct btrfs_path *path,
  1007. u64 dirid, u64 index,
  1008. char *name, int name_len, u8 type,
  1009. struct btrfs_key *location)
  1010. {
  1011. struct inode *inode;
  1012. struct inode *dir;
  1013. int ret;
  1014. inode = read_one_inode(root, location->objectid);
  1015. if (!inode)
  1016. return -ENOENT;
  1017. dir = read_one_inode(root, dirid);
  1018. if (!dir) {
  1019. iput(inode);
  1020. return -EIO;
  1021. }
  1022. ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
  1023. /* FIXME, put inode into FIXUP list */
  1024. iput(inode);
  1025. iput(dir);
  1026. return ret;
  1027. }
  1028. /*
  1029. * take a single entry in a log directory item and replay it into
  1030. * the subvolume.
  1031. *
  1032. * if a conflicting item exists in the subdirectory already,
  1033. * the inode it points to is unlinked and put into the link count
  1034. * fix up tree.
  1035. *
  1036. * If a name from the log points to a file or directory that does
  1037. * not exist in the FS, it is skipped. fsyncs on directories
  1038. * do not force down inodes inside that directory, just changes to the
  1039. * names or unlinks in a directory.
  1040. */
  1041. static noinline int replay_one_name(struct btrfs_trans_handle *trans,
  1042. struct btrfs_root *root,
  1043. struct btrfs_path *path,
  1044. struct extent_buffer *eb,
  1045. struct btrfs_dir_item *di,
  1046. struct btrfs_key *key)
  1047. {
  1048. char *name;
  1049. int name_len;
  1050. struct btrfs_dir_item *dst_di;
  1051. struct btrfs_key found_key;
  1052. struct btrfs_key log_key;
  1053. struct inode *dir;
  1054. u8 log_type;
  1055. int exists;
  1056. int ret;
  1057. dir = read_one_inode(root, key->objectid);
  1058. BUG_ON(!dir);
  1059. name_len = btrfs_dir_name_len(eb, di);
  1060. name = kmalloc(name_len, GFP_NOFS);
  1061. if (!name)
  1062. return -ENOMEM;
  1063. log_type = btrfs_dir_type(eb, di);
  1064. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1065. name_len);
  1066. btrfs_dir_item_key_to_cpu(eb, di, &log_key);
  1067. exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
  1068. if (exists == 0)
  1069. exists = 1;
  1070. else
  1071. exists = 0;
  1072. btrfs_release_path(root, path);
  1073. if (key->type == BTRFS_DIR_ITEM_KEY) {
  1074. dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
  1075. name, name_len, 1);
  1076. } else if (key->type == BTRFS_DIR_INDEX_KEY) {
  1077. dst_di = btrfs_lookup_dir_index_item(trans, root, path,
  1078. key->objectid,
  1079. key->offset, name,
  1080. name_len, 1);
  1081. } else {
  1082. BUG();
  1083. }
  1084. if (!dst_di || IS_ERR(dst_di)) {
  1085. /* we need a sequence number to insert, so we only
  1086. * do inserts for the BTRFS_DIR_INDEX_KEY types
  1087. */
  1088. if (key->type != BTRFS_DIR_INDEX_KEY)
  1089. goto out;
  1090. goto insert;
  1091. }
  1092. btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
  1093. /* the existing item matches the logged item */
  1094. if (found_key.objectid == log_key.objectid &&
  1095. found_key.type == log_key.type &&
  1096. found_key.offset == log_key.offset &&
  1097. btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
  1098. goto out;
  1099. }
  1100. /*
  1101. * don't drop the conflicting directory entry if the inode
  1102. * for the new entry doesn't exist
  1103. */
  1104. if (!exists)
  1105. goto out;
  1106. ret = drop_one_dir_item(trans, root, path, dir, dst_di);
  1107. BUG_ON(ret);
  1108. if (key->type == BTRFS_DIR_INDEX_KEY)
  1109. goto insert;
  1110. out:
  1111. btrfs_release_path(root, path);
  1112. kfree(name);
  1113. iput(dir);
  1114. return 0;
  1115. insert:
  1116. btrfs_release_path(root, path);
  1117. ret = insert_one_name(trans, root, path, key->objectid, key->offset,
  1118. name, name_len, log_type, &log_key);
  1119. BUG_ON(ret && ret != -ENOENT);
  1120. goto out;
  1121. }
  1122. /*
  1123. * find all the names in a directory item and reconcile them into
  1124. * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
  1125. * one name in a directory item, but the same code gets used for
  1126. * both directory index types
  1127. */
  1128. static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
  1129. struct btrfs_root *root,
  1130. struct btrfs_path *path,
  1131. struct extent_buffer *eb, int slot,
  1132. struct btrfs_key *key)
  1133. {
  1134. int ret;
  1135. u32 item_size = btrfs_item_size_nr(eb, slot);
  1136. struct btrfs_dir_item *di;
  1137. int name_len;
  1138. unsigned long ptr;
  1139. unsigned long ptr_end;
  1140. ptr = btrfs_item_ptr_offset(eb, slot);
  1141. ptr_end = ptr + item_size;
  1142. while (ptr < ptr_end) {
  1143. di = (struct btrfs_dir_item *)ptr;
  1144. if (verify_dir_item(root, eb, di))
  1145. return -EIO;
  1146. name_len = btrfs_dir_name_len(eb, di);
  1147. ret = replay_one_name(trans, root, path, eb, di, key);
  1148. BUG_ON(ret);
  1149. ptr = (unsigned long)(di + 1);
  1150. ptr += name_len;
  1151. }
  1152. return 0;
  1153. }
  1154. /*
  1155. * directory replay has two parts. There are the standard directory
  1156. * items in the log copied from the subvolume, and range items
  1157. * created in the log while the subvolume was logged.
  1158. *
  1159. * The range items tell us which parts of the key space the log
  1160. * is authoritative for. During replay, if a key in the subvolume
  1161. * directory is in a logged range item, but not actually in the log
  1162. * that means it was deleted from the directory before the fsync
  1163. * and should be removed.
  1164. */
  1165. static noinline int find_dir_range(struct btrfs_root *root,
  1166. struct btrfs_path *path,
  1167. u64 dirid, int key_type,
  1168. u64 *start_ret, u64 *end_ret)
  1169. {
  1170. struct btrfs_key key;
  1171. u64 found_end;
  1172. struct btrfs_dir_log_item *item;
  1173. int ret;
  1174. int nritems;
  1175. if (*start_ret == (u64)-1)
  1176. return 1;
  1177. key.objectid = dirid;
  1178. key.type = key_type;
  1179. key.offset = *start_ret;
  1180. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1181. if (ret < 0)
  1182. goto out;
  1183. if (ret > 0) {
  1184. if (path->slots[0] == 0)
  1185. goto out;
  1186. path->slots[0]--;
  1187. }
  1188. if (ret != 0)
  1189. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1190. if (key.type != key_type || key.objectid != dirid) {
  1191. ret = 1;
  1192. goto next;
  1193. }
  1194. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1195. struct btrfs_dir_log_item);
  1196. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1197. if (*start_ret >= key.offset && *start_ret <= found_end) {
  1198. ret = 0;
  1199. *start_ret = key.offset;
  1200. *end_ret = found_end;
  1201. goto out;
  1202. }
  1203. ret = 1;
  1204. next:
  1205. /* check the next slot in the tree to see if it is a valid item */
  1206. nritems = btrfs_header_nritems(path->nodes[0]);
  1207. if (path->slots[0] >= nritems) {
  1208. ret = btrfs_next_leaf(root, path);
  1209. if (ret)
  1210. goto out;
  1211. } else {
  1212. path->slots[0]++;
  1213. }
  1214. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1215. if (key.type != key_type || key.objectid != dirid) {
  1216. ret = 1;
  1217. goto out;
  1218. }
  1219. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1220. struct btrfs_dir_log_item);
  1221. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1222. *start_ret = key.offset;
  1223. *end_ret = found_end;
  1224. ret = 0;
  1225. out:
  1226. btrfs_release_path(root, path);
  1227. return ret;
  1228. }
  1229. /*
  1230. * this looks for a given directory item in the log. If the directory
  1231. * item is not in the log, the item is removed and the inode it points
  1232. * to is unlinked
  1233. */
  1234. static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
  1235. struct btrfs_root *root,
  1236. struct btrfs_root *log,
  1237. struct btrfs_path *path,
  1238. struct btrfs_path *log_path,
  1239. struct inode *dir,
  1240. struct btrfs_key *dir_key)
  1241. {
  1242. int ret;
  1243. struct extent_buffer *eb;
  1244. int slot;
  1245. u32 item_size;
  1246. struct btrfs_dir_item *di;
  1247. struct btrfs_dir_item *log_di;
  1248. int name_len;
  1249. unsigned long ptr;
  1250. unsigned long ptr_end;
  1251. char *name;
  1252. struct inode *inode;
  1253. struct btrfs_key location;
  1254. again:
  1255. eb = path->nodes[0];
  1256. slot = path->slots[0];
  1257. item_size = btrfs_item_size_nr(eb, slot);
  1258. ptr = btrfs_item_ptr_offset(eb, slot);
  1259. ptr_end = ptr + item_size;
  1260. while (ptr < ptr_end) {
  1261. di = (struct btrfs_dir_item *)ptr;
  1262. if (verify_dir_item(root, eb, di)) {
  1263. ret = -EIO;
  1264. goto out;
  1265. }
  1266. name_len = btrfs_dir_name_len(eb, di);
  1267. name = kmalloc(name_len, GFP_NOFS);
  1268. if (!name) {
  1269. ret = -ENOMEM;
  1270. goto out;
  1271. }
  1272. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1273. name_len);
  1274. log_di = NULL;
  1275. if (log && dir_key->type == BTRFS_DIR_ITEM_KEY) {
  1276. log_di = btrfs_lookup_dir_item(trans, log, log_path,
  1277. dir_key->objectid,
  1278. name, name_len, 0);
  1279. } else if (log && dir_key->type == BTRFS_DIR_INDEX_KEY) {
  1280. log_di = btrfs_lookup_dir_index_item(trans, log,
  1281. log_path,
  1282. dir_key->objectid,
  1283. dir_key->offset,
  1284. name, name_len, 0);
  1285. }
  1286. if (!log_di || IS_ERR(log_di)) {
  1287. btrfs_dir_item_key_to_cpu(eb, di, &location);
  1288. btrfs_release_path(root, path);
  1289. btrfs_release_path(log, log_path);
  1290. inode = read_one_inode(root, location.objectid);
  1291. BUG_ON(!inode);
  1292. ret = link_to_fixup_dir(trans, root,
  1293. path, location.objectid);
  1294. BUG_ON(ret);
  1295. btrfs_inc_nlink(inode);
  1296. ret = btrfs_unlink_inode(trans, root, dir, inode,
  1297. name, name_len);
  1298. BUG_ON(ret);
  1299. kfree(name);
  1300. iput(inode);
  1301. /* there might still be more names under this key
  1302. * check and repeat if required
  1303. */
  1304. ret = btrfs_search_slot(NULL, root, dir_key, path,
  1305. 0, 0);
  1306. if (ret == 0)
  1307. goto again;
  1308. ret = 0;
  1309. goto out;
  1310. }
  1311. btrfs_release_path(log, log_path);
  1312. kfree(name);
  1313. ptr = (unsigned long)(di + 1);
  1314. ptr += name_len;
  1315. }
  1316. ret = 0;
  1317. out:
  1318. btrfs_release_path(root, path);
  1319. btrfs_release_path(log, log_path);
  1320. return ret;
  1321. }
  1322. /*
  1323. * deletion replay happens before we copy any new directory items
  1324. * out of the log or out of backreferences from inodes. It
  1325. * scans the log to find ranges of keys that log is authoritative for,
  1326. * and then scans the directory to find items in those ranges that are
  1327. * not present in the log.
  1328. *
  1329. * Anything we don't find in the log is unlinked and removed from the
  1330. * directory.
  1331. */
  1332. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  1333. struct btrfs_root *root,
  1334. struct btrfs_root *log,
  1335. struct btrfs_path *path,
  1336. u64 dirid, int del_all)
  1337. {
  1338. u64 range_start;
  1339. u64 range_end;
  1340. int key_type = BTRFS_DIR_LOG_ITEM_KEY;
  1341. int ret = 0;
  1342. struct btrfs_key dir_key;
  1343. struct btrfs_key found_key;
  1344. struct btrfs_path *log_path;
  1345. struct inode *dir;
  1346. dir_key.objectid = dirid;
  1347. dir_key.type = BTRFS_DIR_ITEM_KEY;
  1348. log_path = btrfs_alloc_path();
  1349. if (!log_path)
  1350. return -ENOMEM;
  1351. dir = read_one_inode(root, dirid);
  1352. /* it isn't an error if the inode isn't there, that can happen
  1353. * because we replay the deletes before we copy in the inode item
  1354. * from the log
  1355. */
  1356. if (!dir) {
  1357. btrfs_free_path(log_path);
  1358. return 0;
  1359. }
  1360. again:
  1361. range_start = 0;
  1362. range_end = 0;
  1363. while (1) {
  1364. if (del_all)
  1365. range_end = (u64)-1;
  1366. else {
  1367. ret = find_dir_range(log, path, dirid, key_type,
  1368. &range_start, &range_end);
  1369. if (ret != 0)
  1370. break;
  1371. }
  1372. dir_key.offset = range_start;
  1373. while (1) {
  1374. int nritems;
  1375. ret = btrfs_search_slot(NULL, root, &dir_key, path,
  1376. 0, 0);
  1377. if (ret < 0)
  1378. goto out;
  1379. nritems = btrfs_header_nritems(path->nodes[0]);
  1380. if (path->slots[0] >= nritems) {
  1381. ret = btrfs_next_leaf(root, path);
  1382. if (ret)
  1383. break;
  1384. }
  1385. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1386. path->slots[0]);
  1387. if (found_key.objectid != dirid ||
  1388. found_key.type != dir_key.type)
  1389. goto next_type;
  1390. if (found_key.offset > range_end)
  1391. break;
  1392. ret = check_item_in_log(trans, root, log, path,
  1393. log_path, dir,
  1394. &found_key);
  1395. BUG_ON(ret);
  1396. if (found_key.offset == (u64)-1)
  1397. break;
  1398. dir_key.offset = found_key.offset + 1;
  1399. }
  1400. btrfs_release_path(root, path);
  1401. if (range_end == (u64)-1)
  1402. break;
  1403. range_start = range_end + 1;
  1404. }
  1405. next_type:
  1406. ret = 0;
  1407. if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
  1408. key_type = BTRFS_DIR_LOG_INDEX_KEY;
  1409. dir_key.type = BTRFS_DIR_INDEX_KEY;
  1410. btrfs_release_path(root, path);
  1411. goto again;
  1412. }
  1413. out:
  1414. btrfs_release_path(root, path);
  1415. btrfs_free_path(log_path);
  1416. iput(dir);
  1417. return ret;
  1418. }
  1419. /*
  1420. * the process_func used to replay items from the log tree. This
  1421. * gets called in two different stages. The first stage just looks
  1422. * for inodes and makes sure they are all copied into the subvolume.
  1423. *
  1424. * The second stage copies all the other item types from the log into
  1425. * the subvolume. The two stage approach is slower, but gets rid of
  1426. * lots of complexity around inodes referencing other inodes that exist
  1427. * only in the log (references come from either directory items or inode
  1428. * back refs).
  1429. */
  1430. static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
  1431. struct walk_control *wc, u64 gen)
  1432. {
  1433. int nritems;
  1434. struct btrfs_path *path;
  1435. struct btrfs_root *root = wc->replay_dest;
  1436. struct btrfs_key key;
  1437. int level;
  1438. int i;
  1439. int ret;
  1440. btrfs_read_buffer(eb, gen);
  1441. level = btrfs_header_level(eb);
  1442. if (level != 0)
  1443. return 0;
  1444. path = btrfs_alloc_path();
  1445. BUG_ON(!path);
  1446. nritems = btrfs_header_nritems(eb);
  1447. for (i = 0; i < nritems; i++) {
  1448. btrfs_item_key_to_cpu(eb, &key, i);
  1449. /* inode keys are done during the first stage */
  1450. if (key.type == BTRFS_INODE_ITEM_KEY &&
  1451. wc->stage == LOG_WALK_REPLAY_INODES) {
  1452. struct btrfs_inode_item *inode_item;
  1453. u32 mode;
  1454. inode_item = btrfs_item_ptr(eb, i,
  1455. struct btrfs_inode_item);
  1456. mode = btrfs_inode_mode(eb, inode_item);
  1457. if (S_ISDIR(mode)) {
  1458. ret = replay_dir_deletes(wc->trans,
  1459. root, log, path, key.objectid, 0);
  1460. BUG_ON(ret);
  1461. }
  1462. ret = overwrite_item(wc->trans, root, path,
  1463. eb, i, &key);
  1464. BUG_ON(ret);
  1465. /* for regular files, make sure corresponding
  1466. * orhpan item exist. extents past the new EOF
  1467. * will be truncated later by orphan cleanup.
  1468. */
  1469. if (S_ISREG(mode)) {
  1470. ret = insert_orphan_item(wc->trans, root,
  1471. key.objectid);
  1472. BUG_ON(ret);
  1473. }
  1474. ret = link_to_fixup_dir(wc->trans, root,
  1475. path, key.objectid);
  1476. BUG_ON(ret);
  1477. }
  1478. if (wc->stage < LOG_WALK_REPLAY_ALL)
  1479. continue;
  1480. /* these keys are simply copied */
  1481. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  1482. ret = overwrite_item(wc->trans, root, path,
  1483. eb, i, &key);
  1484. BUG_ON(ret);
  1485. } else if (key.type == BTRFS_INODE_REF_KEY) {
  1486. ret = add_inode_ref(wc->trans, root, log, path,
  1487. eb, i, &key);
  1488. BUG_ON(ret && ret != -ENOENT);
  1489. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  1490. ret = replay_one_extent(wc->trans, root, path,
  1491. eb, i, &key);
  1492. BUG_ON(ret);
  1493. } else if (key.type == BTRFS_DIR_ITEM_KEY ||
  1494. key.type == BTRFS_DIR_INDEX_KEY) {
  1495. ret = replay_one_dir_item(wc->trans, root, path,
  1496. eb, i, &key);
  1497. BUG_ON(ret);
  1498. }
  1499. }
  1500. btrfs_free_path(path);
  1501. return 0;
  1502. }
  1503. static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
  1504. struct btrfs_root *root,
  1505. struct btrfs_path *path, int *level,
  1506. struct walk_control *wc)
  1507. {
  1508. u64 root_owner;
  1509. u64 bytenr;
  1510. u64 ptr_gen;
  1511. struct extent_buffer *next;
  1512. struct extent_buffer *cur;
  1513. struct extent_buffer *parent;
  1514. u32 blocksize;
  1515. int ret = 0;
  1516. WARN_ON(*level < 0);
  1517. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1518. while (*level > 0) {
  1519. WARN_ON(*level < 0);
  1520. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1521. cur = path->nodes[*level];
  1522. if (btrfs_header_level(cur) != *level)
  1523. WARN_ON(1);
  1524. if (path->slots[*level] >=
  1525. btrfs_header_nritems(cur))
  1526. break;
  1527. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  1528. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  1529. blocksize = btrfs_level_size(root, *level - 1);
  1530. parent = path->nodes[*level];
  1531. root_owner = btrfs_header_owner(parent);
  1532. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  1533. if (!next)
  1534. return -ENOMEM;
  1535. if (*level == 1) {
  1536. wc->process_func(root, next, wc, ptr_gen);
  1537. path->slots[*level]++;
  1538. if (wc->free) {
  1539. btrfs_read_buffer(next, ptr_gen);
  1540. btrfs_tree_lock(next);
  1541. clean_tree_block(trans, root, next);
  1542. btrfs_set_lock_blocking(next);
  1543. btrfs_wait_tree_block_writeback(next);
  1544. btrfs_tree_unlock(next);
  1545. WARN_ON(root_owner !=
  1546. BTRFS_TREE_LOG_OBJECTID);
  1547. ret = btrfs_free_reserved_extent(root,
  1548. bytenr, blocksize);
  1549. BUG_ON(ret);
  1550. }
  1551. free_extent_buffer(next);
  1552. continue;
  1553. }
  1554. btrfs_read_buffer(next, ptr_gen);
  1555. WARN_ON(*level <= 0);
  1556. if (path->nodes[*level-1])
  1557. free_extent_buffer(path->nodes[*level-1]);
  1558. path->nodes[*level-1] = next;
  1559. *level = btrfs_header_level(next);
  1560. path->slots[*level] = 0;
  1561. cond_resched();
  1562. }
  1563. WARN_ON(*level < 0);
  1564. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1565. path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
  1566. cond_resched();
  1567. return 0;
  1568. }
  1569. static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
  1570. struct btrfs_root *root,
  1571. struct btrfs_path *path, int *level,
  1572. struct walk_control *wc)
  1573. {
  1574. u64 root_owner;
  1575. int i;
  1576. int slot;
  1577. int ret;
  1578. for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1579. slot = path->slots[i];
  1580. if (slot + 1 < btrfs_header_nritems(path->nodes[i])) {
  1581. path->slots[i]++;
  1582. *level = i;
  1583. WARN_ON(*level == 0);
  1584. return 0;
  1585. } else {
  1586. struct extent_buffer *parent;
  1587. if (path->nodes[*level] == root->node)
  1588. parent = path->nodes[*level];
  1589. else
  1590. parent = path->nodes[*level + 1];
  1591. root_owner = btrfs_header_owner(parent);
  1592. wc->process_func(root, path->nodes[*level], wc,
  1593. btrfs_header_generation(path->nodes[*level]));
  1594. if (wc->free) {
  1595. struct extent_buffer *next;
  1596. next = path->nodes[*level];
  1597. btrfs_tree_lock(next);
  1598. clean_tree_block(trans, root, next);
  1599. btrfs_set_lock_blocking(next);
  1600. btrfs_wait_tree_block_writeback(next);
  1601. btrfs_tree_unlock(next);
  1602. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  1603. ret = btrfs_free_reserved_extent(root,
  1604. path->nodes[*level]->start,
  1605. path->nodes[*level]->len);
  1606. BUG_ON(ret);
  1607. }
  1608. free_extent_buffer(path->nodes[*level]);
  1609. path->nodes[*level] = NULL;
  1610. *level = i + 1;
  1611. }
  1612. }
  1613. return 1;
  1614. }
  1615. /*
  1616. * drop the reference count on the tree rooted at 'snap'. This traverses
  1617. * the tree freeing any blocks that have a ref count of zero after being
  1618. * decremented.
  1619. */
  1620. static int walk_log_tree(struct btrfs_trans_handle *trans,
  1621. struct btrfs_root *log, struct walk_control *wc)
  1622. {
  1623. int ret = 0;
  1624. int wret;
  1625. int level;
  1626. struct btrfs_path *path;
  1627. int i;
  1628. int orig_level;
  1629. path = btrfs_alloc_path();
  1630. BUG_ON(!path);
  1631. level = btrfs_header_level(log->node);
  1632. orig_level = level;
  1633. path->nodes[level] = log->node;
  1634. extent_buffer_get(log->node);
  1635. path->slots[level] = 0;
  1636. while (1) {
  1637. wret = walk_down_log_tree(trans, log, path, &level, wc);
  1638. if (wret > 0)
  1639. break;
  1640. if (wret < 0)
  1641. ret = wret;
  1642. wret = walk_up_log_tree(trans, log, path, &level, wc);
  1643. if (wret > 0)
  1644. break;
  1645. if (wret < 0)
  1646. ret = wret;
  1647. }
  1648. /* was the root node processed? if not, catch it here */
  1649. if (path->nodes[orig_level]) {
  1650. wc->process_func(log, path->nodes[orig_level], wc,
  1651. btrfs_header_generation(path->nodes[orig_level]));
  1652. if (wc->free) {
  1653. struct extent_buffer *next;
  1654. next = path->nodes[orig_level];
  1655. btrfs_tree_lock(next);
  1656. clean_tree_block(trans, log, next);
  1657. btrfs_set_lock_blocking(next);
  1658. btrfs_wait_tree_block_writeback(next);
  1659. btrfs_tree_unlock(next);
  1660. WARN_ON(log->root_key.objectid !=
  1661. BTRFS_TREE_LOG_OBJECTID);
  1662. ret = btrfs_free_reserved_extent(log, next->start,
  1663. next->len);
  1664. BUG_ON(ret);
  1665. }
  1666. }
  1667. for (i = 0; i <= orig_level; i++) {
  1668. if (path->nodes[i]) {
  1669. free_extent_buffer(path->nodes[i]);
  1670. path->nodes[i] = NULL;
  1671. }
  1672. }
  1673. btrfs_free_path(path);
  1674. return ret;
  1675. }
  1676. /*
  1677. * helper function to update the item for a given subvolumes log root
  1678. * in the tree of log roots
  1679. */
  1680. static int update_log_root(struct btrfs_trans_handle *trans,
  1681. struct btrfs_root *log)
  1682. {
  1683. int ret;
  1684. if (log->log_transid == 1) {
  1685. /* insert root item on the first sync */
  1686. ret = btrfs_insert_root(trans, log->fs_info->log_root_tree,
  1687. &log->root_key, &log->root_item);
  1688. } else {
  1689. ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
  1690. &log->root_key, &log->root_item);
  1691. }
  1692. return ret;
  1693. }
  1694. static int wait_log_commit(struct btrfs_trans_handle *trans,
  1695. struct btrfs_root *root, unsigned long transid)
  1696. {
  1697. DEFINE_WAIT(wait);
  1698. int index = transid % 2;
  1699. /*
  1700. * we only allow two pending log transactions at a time,
  1701. * so we know that if ours is more than 2 older than the
  1702. * current transaction, we're done
  1703. */
  1704. do {
  1705. prepare_to_wait(&root->log_commit_wait[index],
  1706. &wait, TASK_UNINTERRUPTIBLE);
  1707. mutex_unlock(&root->log_mutex);
  1708. if (root->fs_info->last_trans_log_full_commit !=
  1709. trans->transid && root->log_transid < transid + 2 &&
  1710. atomic_read(&root->log_commit[index]))
  1711. schedule();
  1712. finish_wait(&root->log_commit_wait[index], &wait);
  1713. mutex_lock(&root->log_mutex);
  1714. } while (root->log_transid < transid + 2 &&
  1715. atomic_read(&root->log_commit[index]));
  1716. return 0;
  1717. }
  1718. static int wait_for_writer(struct btrfs_trans_handle *trans,
  1719. struct btrfs_root *root)
  1720. {
  1721. DEFINE_WAIT(wait);
  1722. while (atomic_read(&root->log_writers)) {
  1723. prepare_to_wait(&root->log_writer_wait,
  1724. &wait, TASK_UNINTERRUPTIBLE);
  1725. mutex_unlock(&root->log_mutex);
  1726. if (root->fs_info->last_trans_log_full_commit !=
  1727. trans->transid && atomic_read(&root->log_writers))
  1728. schedule();
  1729. mutex_lock(&root->log_mutex);
  1730. finish_wait(&root->log_writer_wait, &wait);
  1731. }
  1732. return 0;
  1733. }
  1734. /*
  1735. * btrfs_sync_log does sends a given tree log down to the disk and
  1736. * updates the super blocks to record it. When this call is done,
  1737. * you know that any inodes previously logged are safely on disk only
  1738. * if it returns 0.
  1739. *
  1740. * Any other return value means you need to call btrfs_commit_transaction.
  1741. * Some of the edge cases for fsyncing directories that have had unlinks
  1742. * or renames done in the past mean that sometimes the only safe
  1743. * fsync is to commit the whole FS. When btrfs_sync_log returns -EAGAIN,
  1744. * that has happened.
  1745. */
  1746. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  1747. struct btrfs_root *root)
  1748. {
  1749. int index1;
  1750. int index2;
  1751. int mark;
  1752. int ret;
  1753. struct btrfs_root *log = root->log_root;
  1754. struct btrfs_root *log_root_tree = root->fs_info->log_root_tree;
  1755. unsigned long log_transid = 0;
  1756. mutex_lock(&root->log_mutex);
  1757. index1 = root->log_transid % 2;
  1758. if (atomic_read(&root->log_commit[index1])) {
  1759. wait_log_commit(trans, root, root->log_transid);
  1760. mutex_unlock(&root->log_mutex);
  1761. return 0;
  1762. }
  1763. atomic_set(&root->log_commit[index1], 1);
  1764. /* wait for previous tree log sync to complete */
  1765. if (atomic_read(&root->log_commit[(index1 + 1) % 2]))
  1766. wait_log_commit(trans, root, root->log_transid - 1);
  1767. while (1) {
  1768. unsigned long batch = root->log_batch;
  1769. if (root->log_multiple_pids) {
  1770. mutex_unlock(&root->log_mutex);
  1771. schedule_timeout_uninterruptible(1);
  1772. mutex_lock(&root->log_mutex);
  1773. }
  1774. wait_for_writer(trans, root);
  1775. if (batch == root->log_batch)
  1776. break;
  1777. }
  1778. /* bail out if we need to do a full commit */
  1779. if (root->fs_info->last_trans_log_full_commit == trans->transid) {
  1780. ret = -EAGAIN;
  1781. mutex_unlock(&root->log_mutex);
  1782. goto out;
  1783. }
  1784. log_transid = root->log_transid;
  1785. if (log_transid % 2 == 0)
  1786. mark = EXTENT_DIRTY;
  1787. else
  1788. mark = EXTENT_NEW;
  1789. /* we start IO on all the marked extents here, but we don't actually
  1790. * wait for them until later.
  1791. */
  1792. ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark);
  1793. BUG_ON(ret);
  1794. btrfs_set_root_node(&log->root_item, log->node);
  1795. root->log_batch = 0;
  1796. root->log_transid++;
  1797. log->log_transid = root->log_transid;
  1798. root->log_start_pid = 0;
  1799. smp_mb();
  1800. /*
  1801. * IO has been started, blocks of the log tree have WRITTEN flag set
  1802. * in their headers. new modifications of the log will be written to
  1803. * new positions. so it's safe to allow log writers to go in.
  1804. */
  1805. mutex_unlock(&root->log_mutex);
  1806. mutex_lock(&log_root_tree->log_mutex);
  1807. log_root_tree->log_batch++;
  1808. atomic_inc(&log_root_tree->log_writers);
  1809. mutex_unlock(&log_root_tree->log_mutex);
  1810. ret = update_log_root(trans, log);
  1811. mutex_lock(&log_root_tree->log_mutex);
  1812. if (atomic_dec_and_test(&log_root_tree->log_writers)) {
  1813. smp_mb();
  1814. if (waitqueue_active(&log_root_tree->log_writer_wait))
  1815. wake_up(&log_root_tree->log_writer_wait);
  1816. }
  1817. if (ret) {
  1818. BUG_ON(ret != -ENOSPC);
  1819. root->fs_info->last_trans_log_full_commit = trans->transid;
  1820. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  1821. mutex_unlock(&log_root_tree->log_mutex);
  1822. ret = -EAGAIN;
  1823. goto out;
  1824. }
  1825. index2 = log_root_tree->log_transid % 2;
  1826. if (atomic_read(&log_root_tree->log_commit[index2])) {
  1827. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  1828. wait_log_commit(trans, log_root_tree,
  1829. log_root_tree->log_transid);
  1830. mutex_unlock(&log_root_tree->log_mutex);
  1831. ret = 0;
  1832. goto out;
  1833. }
  1834. atomic_set(&log_root_tree->log_commit[index2], 1);
  1835. if (atomic_read(&log_root_tree->log_commit[(index2 + 1) % 2])) {
  1836. wait_log_commit(trans, log_root_tree,
  1837. log_root_tree->log_transid - 1);
  1838. }
  1839. wait_for_writer(trans, log_root_tree);
  1840. /*
  1841. * now that we've moved on to the tree of log tree roots,
  1842. * check the full commit flag again
  1843. */
  1844. if (root->fs_info->last_trans_log_full_commit == trans->transid) {
  1845. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  1846. mutex_unlock(&log_root_tree->log_mutex);
  1847. ret = -EAGAIN;
  1848. goto out_wake_log_root;
  1849. }
  1850. ret = btrfs_write_and_wait_marked_extents(log_root_tree,
  1851. &log_root_tree->dirty_log_pages,
  1852. EXTENT_DIRTY | EXTENT_NEW);
  1853. BUG_ON(ret);
  1854. btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
  1855. btrfs_set_super_log_root(&root->fs_info->super_for_commit,
  1856. log_root_tree->node->start);
  1857. btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
  1858. btrfs_header_level(log_root_tree->node));
  1859. log_root_tree->log_batch = 0;
  1860. log_root_tree->log_transid++;
  1861. smp_mb();
  1862. mutex_unlock(&log_root_tree->log_mutex);
  1863. /*
  1864. * nobody else is going to jump in and write the the ctree
  1865. * super here because the log_commit atomic below is protecting
  1866. * us. We must be called with a transaction handle pinning
  1867. * the running transaction open, so a full commit can't hop
  1868. * in and cause problems either.
  1869. */
  1870. write_ctree_super(trans, root->fs_info->tree_root, 1);
  1871. ret = 0;
  1872. mutex_lock(&root->log_mutex);
  1873. if (root->last_log_commit < log_transid)
  1874. root->last_log_commit = log_transid;
  1875. mutex_unlock(&root->log_mutex);
  1876. out_wake_log_root:
  1877. atomic_set(&log_root_tree->log_commit[index2], 0);
  1878. smp_mb();
  1879. if (waitqueue_active(&log_root_tree->log_commit_wait[index2]))
  1880. wake_up(&log_root_tree->log_commit_wait[index2]);
  1881. out:
  1882. atomic_set(&root->log_commit[index1], 0);
  1883. smp_mb();
  1884. if (waitqueue_active(&root->log_commit_wait[index1]))
  1885. wake_up(&root->log_commit_wait[index1]);
  1886. return ret;
  1887. }
  1888. static void free_log_tree(struct btrfs_trans_handle *trans,
  1889. struct btrfs_root *log)
  1890. {
  1891. int ret;
  1892. u64 start;
  1893. u64 end;
  1894. struct walk_control wc = {
  1895. .free = 1,
  1896. .process_func = process_one_buffer
  1897. };
  1898. ret = walk_log_tree(trans, log, &wc);
  1899. BUG_ON(ret);
  1900. while (1) {
  1901. ret = find_first_extent_bit(&log->dirty_log_pages,
  1902. 0, &start, &end, EXTENT_DIRTY | EXTENT_NEW);
  1903. if (ret)
  1904. break;
  1905. clear_extent_bits(&log->dirty_log_pages, start, end,
  1906. EXTENT_DIRTY | EXTENT_NEW, GFP_NOFS);
  1907. }
  1908. free_extent_buffer(log->node);
  1909. kfree(log);
  1910. }
  1911. /*
  1912. * free all the extents used by the tree log. This should be called
  1913. * at commit time of the full transaction
  1914. */
  1915. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  1916. {
  1917. if (root->log_root) {
  1918. free_log_tree(trans, root->log_root);
  1919. root->log_root = NULL;
  1920. }
  1921. return 0;
  1922. }
  1923. int btrfs_free_log_root_tree(struct btrfs_trans_handle *trans,
  1924. struct btrfs_fs_info *fs_info)
  1925. {
  1926. if (fs_info->log_root_tree) {
  1927. free_log_tree(trans, fs_info->log_root_tree);
  1928. fs_info->log_root_tree = NULL;
  1929. }
  1930. return 0;
  1931. }
  1932. /*
  1933. * If both a file and directory are logged, and unlinks or renames are
  1934. * mixed in, we have a few interesting corners:
  1935. *
  1936. * create file X in dir Y
  1937. * link file X to X.link in dir Y
  1938. * fsync file X
  1939. * unlink file X but leave X.link
  1940. * fsync dir Y
  1941. *
  1942. * After a crash we would expect only X.link to exist. But file X
  1943. * didn't get fsync'd again so the log has back refs for X and X.link.
  1944. *
  1945. * We solve this by removing directory entries and inode backrefs from the
  1946. * log when a file that was logged in the current transaction is
  1947. * unlinked. Any later fsync will include the updated log entries, and
  1948. * we'll be able to reconstruct the proper directory items from backrefs.
  1949. *
  1950. * This optimizations allows us to avoid relogging the entire inode
  1951. * or the entire directory.
  1952. */
  1953. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  1954. struct btrfs_root *root,
  1955. const char *name, int name_len,
  1956. struct inode *dir, u64 index)
  1957. {
  1958. struct btrfs_root *log;
  1959. struct btrfs_dir_item *di;
  1960. struct btrfs_path *path;
  1961. int ret;
  1962. int err = 0;
  1963. int bytes_del = 0;
  1964. if (BTRFS_I(dir)->logged_trans < trans->transid)
  1965. return 0;
  1966. ret = join_running_log_trans(root);
  1967. if (ret)
  1968. return 0;
  1969. mutex_lock(&BTRFS_I(dir)->log_mutex);
  1970. log = root->log_root;
  1971. path = btrfs_alloc_path();
  1972. if (!path)
  1973. return -ENOMEM;
  1974. di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
  1975. name, name_len, -1);
  1976. if (IS_ERR(di)) {
  1977. err = PTR_ERR(di);
  1978. goto fail;
  1979. }
  1980. if (di) {
  1981. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  1982. bytes_del += name_len;
  1983. BUG_ON(ret);
  1984. }
  1985. btrfs_release_path(log, path);
  1986. di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino,
  1987. index, name, name_len, -1);
  1988. if (IS_ERR(di)) {
  1989. err = PTR_ERR(di);
  1990. goto fail;
  1991. }
  1992. if (di) {
  1993. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  1994. bytes_del += name_len;
  1995. BUG_ON(ret);
  1996. }
  1997. /* update the directory size in the log to reflect the names
  1998. * we have removed
  1999. */
  2000. if (bytes_del) {
  2001. struct btrfs_key key;
  2002. key.objectid = dir->i_ino;
  2003. key.offset = 0;
  2004. key.type = BTRFS_INODE_ITEM_KEY;
  2005. btrfs_release_path(log, path);
  2006. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  2007. if (ret < 0) {
  2008. err = ret;
  2009. goto fail;
  2010. }
  2011. if (ret == 0) {
  2012. struct btrfs_inode_item *item;
  2013. u64 i_size;
  2014. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2015. struct btrfs_inode_item);
  2016. i_size = btrfs_inode_size(path->nodes[0], item);
  2017. if (i_size > bytes_del)
  2018. i_size -= bytes_del;
  2019. else
  2020. i_size = 0;
  2021. btrfs_set_inode_size(path->nodes[0], item, i_size);
  2022. btrfs_mark_buffer_dirty(path->nodes[0]);
  2023. } else
  2024. ret = 0;
  2025. btrfs_release_path(log, path);
  2026. }
  2027. fail:
  2028. btrfs_free_path(path);
  2029. mutex_unlock(&BTRFS_I(dir)->log_mutex);
  2030. if (ret == -ENOSPC) {
  2031. root->fs_info->last_trans_log_full_commit = trans->transid;
  2032. ret = 0;
  2033. }
  2034. btrfs_end_log_trans(root);
  2035. return err;
  2036. }
  2037. /* see comments for btrfs_del_dir_entries_in_log */
  2038. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  2039. struct btrfs_root *root,
  2040. const char *name, int name_len,
  2041. struct inode *inode, u64 dirid)
  2042. {
  2043. struct btrfs_root *log;
  2044. u64 index;
  2045. int ret;
  2046. if (BTRFS_I(inode)->logged_trans < trans->transid)
  2047. return 0;
  2048. ret = join_running_log_trans(root);
  2049. if (ret)
  2050. return 0;
  2051. log = root->log_root;
  2052. mutex_lock(&BTRFS_I(inode)->log_mutex);
  2053. ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino,
  2054. dirid, &index);
  2055. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  2056. if (ret == -ENOSPC) {
  2057. root->fs_info->last_trans_log_full_commit = trans->transid;
  2058. ret = 0;
  2059. }
  2060. btrfs_end_log_trans(root);
  2061. return ret;
  2062. }
  2063. /*
  2064. * creates a range item in the log for 'dirid'. first_offset and
  2065. * last_offset tell us which parts of the key space the log should
  2066. * be considered authoritative for.
  2067. */
  2068. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  2069. struct btrfs_root *log,
  2070. struct btrfs_path *path,
  2071. int key_type, u64 dirid,
  2072. u64 first_offset, u64 last_offset)
  2073. {
  2074. int ret;
  2075. struct btrfs_key key;
  2076. struct btrfs_dir_log_item *item;
  2077. key.objectid = dirid;
  2078. key.offset = first_offset;
  2079. if (key_type == BTRFS_DIR_ITEM_KEY)
  2080. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  2081. else
  2082. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  2083. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  2084. if (ret)
  2085. return ret;
  2086. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2087. struct btrfs_dir_log_item);
  2088. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  2089. btrfs_mark_buffer_dirty(path->nodes[0]);
  2090. btrfs_release_path(log, path);
  2091. return 0;
  2092. }
  2093. /*
  2094. * log all the items included in the current transaction for a given
  2095. * directory. This also creates the range items in the log tree required
  2096. * to replay anything deleted before the fsync
  2097. */
  2098. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  2099. struct btrfs_root *root, struct inode *inode,
  2100. struct btrfs_path *path,
  2101. struct btrfs_path *dst_path, int key_type,
  2102. u64 min_offset, u64 *last_offset_ret)
  2103. {
  2104. struct btrfs_key min_key;
  2105. struct btrfs_key max_key;
  2106. struct btrfs_root *log = root->log_root;
  2107. struct extent_buffer *src;
  2108. int err = 0;
  2109. int ret;
  2110. int i;
  2111. int nritems;
  2112. u64 first_offset = min_offset;
  2113. u64 last_offset = (u64)-1;
  2114. log = root->log_root;
  2115. max_key.objectid = inode->i_ino;
  2116. max_key.offset = (u64)-1;
  2117. max_key.type = key_type;
  2118. min_key.objectid = inode->i_ino;
  2119. min_key.type = key_type;
  2120. min_key.offset = min_offset;
  2121. path->keep_locks = 1;
  2122. ret = btrfs_search_forward(root, &min_key, &max_key,
  2123. path, 0, trans->transid);
  2124. /*
  2125. * we didn't find anything from this transaction, see if there
  2126. * is anything at all
  2127. */
  2128. if (ret != 0 || min_key.objectid != inode->i_ino ||
  2129. min_key.type != key_type) {
  2130. min_key.objectid = inode->i_ino;
  2131. min_key.type = key_type;
  2132. min_key.offset = (u64)-1;
  2133. btrfs_release_path(root, path);
  2134. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2135. if (ret < 0) {
  2136. btrfs_release_path(root, path);
  2137. return ret;
  2138. }
  2139. ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
  2140. /* if ret == 0 there are items for this type,
  2141. * create a range to tell us the last key of this type.
  2142. * otherwise, there are no items in this directory after
  2143. * *min_offset, and we create a range to indicate that.
  2144. */
  2145. if (ret == 0) {
  2146. struct btrfs_key tmp;
  2147. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  2148. path->slots[0]);
  2149. if (key_type == tmp.type)
  2150. first_offset = max(min_offset, tmp.offset) + 1;
  2151. }
  2152. goto done;
  2153. }
  2154. /* go backward to find any previous key */
  2155. ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
  2156. if (ret == 0) {
  2157. struct btrfs_key tmp;
  2158. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2159. if (key_type == tmp.type) {
  2160. first_offset = tmp.offset;
  2161. ret = overwrite_item(trans, log, dst_path,
  2162. path->nodes[0], path->slots[0],
  2163. &tmp);
  2164. if (ret) {
  2165. err = ret;
  2166. goto done;
  2167. }
  2168. }
  2169. }
  2170. btrfs_release_path(root, path);
  2171. /* find the first key from this transaction again */
  2172. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2173. if (ret != 0) {
  2174. WARN_ON(1);
  2175. goto done;
  2176. }
  2177. /*
  2178. * we have a block from this transaction, log every item in it
  2179. * from our directory
  2180. */
  2181. while (1) {
  2182. struct btrfs_key tmp;
  2183. src = path->nodes[0];
  2184. nritems = btrfs_header_nritems(src);
  2185. for (i = path->slots[0]; i < nritems; i++) {
  2186. btrfs_item_key_to_cpu(src, &min_key, i);
  2187. if (min_key.objectid != inode->i_ino ||
  2188. min_key.type != key_type)
  2189. goto done;
  2190. ret = overwrite_item(trans, log, dst_path, src, i,
  2191. &min_key);
  2192. if (ret) {
  2193. err = ret;
  2194. goto done;
  2195. }
  2196. }
  2197. path->slots[0] = nritems;
  2198. /*
  2199. * look ahead to the next item and see if it is also
  2200. * from this directory and from this transaction
  2201. */
  2202. ret = btrfs_next_leaf(root, path);
  2203. if (ret == 1) {
  2204. last_offset = (u64)-1;
  2205. goto done;
  2206. }
  2207. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2208. if (tmp.objectid != inode->i_ino || tmp.type != key_type) {
  2209. last_offset = (u64)-1;
  2210. goto done;
  2211. }
  2212. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  2213. ret = overwrite_item(trans, log, dst_path,
  2214. path->nodes[0], path->slots[0],
  2215. &tmp);
  2216. if (ret)
  2217. err = ret;
  2218. else
  2219. last_offset = tmp.offset;
  2220. goto done;
  2221. }
  2222. }
  2223. done:
  2224. btrfs_release_path(root, path);
  2225. btrfs_release_path(log, dst_path);
  2226. if (err == 0) {
  2227. *last_offset_ret = last_offset;
  2228. /*
  2229. * insert the log range keys to indicate where the log
  2230. * is valid
  2231. */
  2232. ret = insert_dir_log_key(trans, log, path, key_type,
  2233. inode->i_ino, first_offset,
  2234. last_offset);
  2235. if (ret)
  2236. err = ret;
  2237. }
  2238. return err;
  2239. }
  2240. /*
  2241. * logging directories is very similar to logging inodes, We find all the items
  2242. * from the current transaction and write them to the log.
  2243. *
  2244. * The recovery code scans the directory in the subvolume, and if it finds a
  2245. * key in the range logged that is not present in the log tree, then it means
  2246. * that dir entry was unlinked during the transaction.
  2247. *
  2248. * In order for that scan to work, we must include one key smaller than
  2249. * the smallest logged by this transaction and one key larger than the largest
  2250. * key logged by this transaction.
  2251. */
  2252. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  2253. struct btrfs_root *root, struct inode *inode,
  2254. struct btrfs_path *path,
  2255. struct btrfs_path *dst_path)
  2256. {
  2257. u64 min_key;
  2258. u64 max_key;
  2259. int ret;
  2260. int key_type = BTRFS_DIR_ITEM_KEY;
  2261. again:
  2262. min_key = 0;
  2263. max_key = 0;
  2264. while (1) {
  2265. ret = log_dir_items(trans, root, inode, path,
  2266. dst_path, key_type, min_key,
  2267. &max_key);
  2268. if (ret)
  2269. return ret;
  2270. if (max_key == (u64)-1)
  2271. break;
  2272. min_key = max_key + 1;
  2273. }
  2274. if (key_type == BTRFS_DIR_ITEM_KEY) {
  2275. key_type = BTRFS_DIR_INDEX_KEY;
  2276. goto again;
  2277. }
  2278. return 0;
  2279. }
  2280. /*
  2281. * a helper function to drop items from the log before we relog an
  2282. * inode. max_key_type indicates the highest item type to remove.
  2283. * This cannot be run for file data extents because it does not
  2284. * free the extents they point to.
  2285. */
  2286. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  2287. struct btrfs_root *log,
  2288. struct btrfs_path *path,
  2289. u64 objectid, int max_key_type)
  2290. {
  2291. int ret;
  2292. struct btrfs_key key;
  2293. struct btrfs_key found_key;
  2294. key.objectid = objectid;
  2295. key.type = max_key_type;
  2296. key.offset = (u64)-1;
  2297. while (1) {
  2298. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  2299. BUG_ON(ret == 0);
  2300. if (ret < 0)
  2301. break;
  2302. if (path->slots[0] == 0)
  2303. break;
  2304. path->slots[0]--;
  2305. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2306. path->slots[0]);
  2307. if (found_key.objectid != objectid)
  2308. break;
  2309. ret = btrfs_del_item(trans, log, path);
  2310. BUG_ON(ret);
  2311. btrfs_release_path(log, path);
  2312. }
  2313. btrfs_release_path(log, path);
  2314. return ret;
  2315. }
  2316. static noinline int copy_items(struct btrfs_trans_handle *trans,
  2317. struct btrfs_root *log,
  2318. struct btrfs_path *dst_path,
  2319. struct extent_buffer *src,
  2320. int start_slot, int nr, int inode_only)
  2321. {
  2322. unsigned long src_offset;
  2323. unsigned long dst_offset;
  2324. struct btrfs_file_extent_item *extent;
  2325. struct btrfs_inode_item *inode_item;
  2326. int ret;
  2327. struct btrfs_key *ins_keys;
  2328. u32 *ins_sizes;
  2329. char *ins_data;
  2330. int i;
  2331. struct list_head ordered_sums;
  2332. INIT_LIST_HEAD(&ordered_sums);
  2333. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  2334. nr * sizeof(u32), GFP_NOFS);
  2335. if (!ins_data)
  2336. return -ENOMEM;
  2337. ins_sizes = (u32 *)ins_data;
  2338. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  2339. for (i = 0; i < nr; i++) {
  2340. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  2341. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  2342. }
  2343. ret = btrfs_insert_empty_items(trans, log, dst_path,
  2344. ins_keys, ins_sizes, nr);
  2345. if (ret) {
  2346. kfree(ins_data);
  2347. return ret;
  2348. }
  2349. for (i = 0; i < nr; i++, dst_path->slots[0]++) {
  2350. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  2351. dst_path->slots[0]);
  2352. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  2353. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  2354. src_offset, ins_sizes[i]);
  2355. if (inode_only == LOG_INODE_EXISTS &&
  2356. ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  2357. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  2358. dst_path->slots[0],
  2359. struct btrfs_inode_item);
  2360. btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
  2361. /* set the generation to zero so the recover code
  2362. * can tell the difference between an logging
  2363. * just to say 'this inode exists' and a logging
  2364. * to say 'update this inode with these values'
  2365. */
  2366. btrfs_set_inode_generation(dst_path->nodes[0],
  2367. inode_item, 0);
  2368. }
  2369. /* take a reference on file data extents so that truncates
  2370. * or deletes of this inode don't have to relog the inode
  2371. * again
  2372. */
  2373. if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
  2374. int found_type;
  2375. extent = btrfs_item_ptr(src, start_slot + i,
  2376. struct btrfs_file_extent_item);
  2377. found_type = btrfs_file_extent_type(src, extent);
  2378. if (found_type == BTRFS_FILE_EXTENT_REG ||
  2379. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  2380. u64 ds, dl, cs, cl;
  2381. ds = btrfs_file_extent_disk_bytenr(src,
  2382. extent);
  2383. /* ds == 0 is a hole */
  2384. if (ds == 0)
  2385. continue;
  2386. dl = btrfs_file_extent_disk_num_bytes(src,
  2387. extent);
  2388. cs = btrfs_file_extent_offset(src, extent);
  2389. cl = btrfs_file_extent_num_bytes(src,
  2390. extent);
  2391. if (btrfs_file_extent_compression(src,
  2392. extent)) {
  2393. cs = 0;
  2394. cl = dl;
  2395. }
  2396. ret = btrfs_lookup_csums_range(
  2397. log->fs_info->csum_root,
  2398. ds + cs, ds + cs + cl - 1,
  2399. &ordered_sums);
  2400. BUG_ON(ret);
  2401. }
  2402. }
  2403. }
  2404. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  2405. btrfs_release_path(log, dst_path);
  2406. kfree(ins_data);
  2407. /*
  2408. * we have to do this after the loop above to avoid changing the
  2409. * log tree while trying to change the log tree.
  2410. */
  2411. ret = 0;
  2412. while (!list_empty(&ordered_sums)) {
  2413. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  2414. struct btrfs_ordered_sum,
  2415. list);
  2416. if (!ret)
  2417. ret = btrfs_csum_file_blocks(trans, log, sums);
  2418. list_del(&sums->list);
  2419. kfree(sums);
  2420. }
  2421. return ret;
  2422. }
  2423. /* log a single inode in the tree log.
  2424. * At least one parent directory for this inode must exist in the tree
  2425. * or be logged already.
  2426. *
  2427. * Any items from this inode changed by the current transaction are copied
  2428. * to the log tree. An extra reference is taken on any extents in this
  2429. * file, allowing us to avoid a whole pile of corner cases around logging
  2430. * blocks that have been removed from the tree.
  2431. *
  2432. * See LOG_INODE_ALL and related defines for a description of what inode_only
  2433. * does.
  2434. *
  2435. * This handles both files and directories.
  2436. */
  2437. static int btrfs_log_inode(struct btrfs_trans_handle *trans,
  2438. struct btrfs_root *root, struct inode *inode,
  2439. int inode_only)
  2440. {
  2441. struct btrfs_path *path;
  2442. struct btrfs_path *dst_path;
  2443. struct btrfs_key min_key;
  2444. struct btrfs_key max_key;
  2445. struct btrfs_root *log = root->log_root;
  2446. struct extent_buffer *src = NULL;
  2447. int err = 0;
  2448. int ret;
  2449. int nritems;
  2450. int ins_start_slot = 0;
  2451. int ins_nr;
  2452. log = root->log_root;
  2453. path = btrfs_alloc_path();
  2454. if (!path)
  2455. return -ENOMEM;
  2456. dst_path = btrfs_alloc_path();
  2457. if (!dst_path) {
  2458. btrfs_free_path(path);
  2459. return -ENOMEM;
  2460. }
  2461. min_key.objectid = inode->i_ino;
  2462. min_key.type = BTRFS_INODE_ITEM_KEY;
  2463. min_key.offset = 0;
  2464. max_key.objectid = inode->i_ino;
  2465. /* today the code can only do partial logging of directories */
  2466. if (!S_ISDIR(inode->i_mode))
  2467. inode_only = LOG_INODE_ALL;
  2468. if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
  2469. max_key.type = BTRFS_XATTR_ITEM_KEY;
  2470. else
  2471. max_key.type = (u8)-1;
  2472. max_key.offset = (u64)-1;
  2473. mutex_lock(&BTRFS_I(inode)->log_mutex);
  2474. /*
  2475. * a brute force approach to making sure we get the most uptodate
  2476. * copies of everything.
  2477. */
  2478. if (S_ISDIR(inode->i_mode)) {
  2479. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  2480. if (inode_only == LOG_INODE_EXISTS)
  2481. max_key_type = BTRFS_XATTR_ITEM_KEY;
  2482. ret = drop_objectid_items(trans, log, path,
  2483. inode->i_ino, max_key_type);
  2484. } else {
  2485. ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
  2486. }
  2487. if (ret) {
  2488. err = ret;
  2489. goto out_unlock;
  2490. }
  2491. path->keep_locks = 1;
  2492. while (1) {
  2493. ins_nr = 0;
  2494. ret = btrfs_search_forward(root, &min_key, &max_key,
  2495. path, 0, trans->transid);
  2496. if (ret != 0)
  2497. break;
  2498. again:
  2499. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  2500. if (min_key.objectid != inode->i_ino)
  2501. break;
  2502. if (min_key.type > max_key.type)
  2503. break;
  2504. src = path->nodes[0];
  2505. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  2506. ins_nr++;
  2507. goto next_slot;
  2508. } else if (!ins_nr) {
  2509. ins_start_slot = path->slots[0];
  2510. ins_nr = 1;
  2511. goto next_slot;
  2512. }
  2513. ret = copy_items(trans, log, dst_path, src, ins_start_slot,
  2514. ins_nr, inode_only);
  2515. if (ret) {
  2516. err = ret;
  2517. goto out_unlock;
  2518. }
  2519. ins_nr = 1;
  2520. ins_start_slot = path->slots[0];
  2521. next_slot:
  2522. nritems = btrfs_header_nritems(path->nodes[0]);
  2523. path->slots[0]++;
  2524. if (path->slots[0] < nritems) {
  2525. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  2526. path->slots[0]);
  2527. goto again;
  2528. }
  2529. if (ins_nr) {
  2530. ret = copy_items(trans, log, dst_path, src,
  2531. ins_start_slot,
  2532. ins_nr, inode_only);
  2533. if (ret) {
  2534. err = ret;
  2535. goto out_unlock;
  2536. }
  2537. ins_nr = 0;
  2538. }
  2539. btrfs_release_path(root, path);
  2540. if (min_key.offset < (u64)-1)
  2541. min_key.offset++;
  2542. else if (min_key.type < (u8)-1)
  2543. min_key.type++;
  2544. else if (min_key.objectid < (u64)-1)
  2545. min_key.objectid++;
  2546. else
  2547. break;
  2548. }
  2549. if (ins_nr) {
  2550. ret = copy_items(trans, log, dst_path, src,
  2551. ins_start_slot,
  2552. ins_nr, inode_only);
  2553. if (ret) {
  2554. err = ret;
  2555. goto out_unlock;
  2556. }
  2557. ins_nr = 0;
  2558. }
  2559. WARN_ON(ins_nr);
  2560. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
  2561. btrfs_release_path(root, path);
  2562. btrfs_release_path(log, dst_path);
  2563. ret = log_directory_changes(trans, root, inode, path, dst_path);
  2564. if (ret) {
  2565. err = ret;
  2566. goto out_unlock;
  2567. }
  2568. }
  2569. BTRFS_I(inode)->logged_trans = trans->transid;
  2570. out_unlock:
  2571. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  2572. btrfs_free_path(path);
  2573. btrfs_free_path(dst_path);
  2574. return err;
  2575. }
  2576. /*
  2577. * follow the dentry parent pointers up the chain and see if any
  2578. * of the directories in it require a full commit before they can
  2579. * be logged. Returns zero if nothing special needs to be done or 1 if
  2580. * a full commit is required.
  2581. */
  2582. static noinline int check_parent_dirs_for_sync(struct btrfs_trans_handle *trans,
  2583. struct inode *inode,
  2584. struct dentry *parent,
  2585. struct super_block *sb,
  2586. u64 last_committed)
  2587. {
  2588. int ret = 0;
  2589. struct btrfs_root *root;
  2590. struct dentry *old_parent = NULL;
  2591. /*
  2592. * for regular files, if its inode is already on disk, we don't
  2593. * have to worry about the parents at all. This is because
  2594. * we can use the last_unlink_trans field to record renames
  2595. * and other fun in this file.
  2596. */
  2597. if (S_ISREG(inode->i_mode) &&
  2598. BTRFS_I(inode)->generation <= last_committed &&
  2599. BTRFS_I(inode)->last_unlink_trans <= last_committed)
  2600. goto out;
  2601. if (!S_ISDIR(inode->i_mode)) {
  2602. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  2603. goto out;
  2604. inode = parent->d_inode;
  2605. }
  2606. while (1) {
  2607. BTRFS_I(inode)->logged_trans = trans->transid;
  2608. smp_mb();
  2609. if (BTRFS_I(inode)->last_unlink_trans > last_committed) {
  2610. root = BTRFS_I(inode)->root;
  2611. /*
  2612. * make sure any commits to the log are forced
  2613. * to be full commits
  2614. */
  2615. root->fs_info->last_trans_log_full_commit =
  2616. trans->transid;
  2617. ret = 1;
  2618. break;
  2619. }
  2620. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  2621. break;
  2622. if (IS_ROOT(parent))
  2623. break;
  2624. parent = dget_parent(parent);
  2625. dput(old_parent);
  2626. old_parent = parent;
  2627. inode = parent->d_inode;
  2628. }
  2629. dput(old_parent);
  2630. out:
  2631. return ret;
  2632. }
  2633. static int inode_in_log(struct btrfs_trans_handle *trans,
  2634. struct inode *inode)
  2635. {
  2636. struct btrfs_root *root = BTRFS_I(inode)->root;
  2637. int ret = 0;
  2638. mutex_lock(&root->log_mutex);
  2639. if (BTRFS_I(inode)->logged_trans == trans->transid &&
  2640. BTRFS_I(inode)->last_sub_trans <= root->last_log_commit)
  2641. ret = 1;
  2642. mutex_unlock(&root->log_mutex);
  2643. return ret;
  2644. }
  2645. /*
  2646. * helper function around btrfs_log_inode to make sure newly created
  2647. * parent directories also end up in the log. A minimal inode and backref
  2648. * only logging is done of any parent directories that are older than
  2649. * the last committed transaction
  2650. */
  2651. int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
  2652. struct btrfs_root *root, struct inode *inode,
  2653. struct dentry *parent, int exists_only)
  2654. {
  2655. int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
  2656. struct super_block *sb;
  2657. struct dentry *old_parent = NULL;
  2658. int ret = 0;
  2659. u64 last_committed = root->fs_info->last_trans_committed;
  2660. sb = inode->i_sb;
  2661. if (btrfs_test_opt(root, NOTREELOG)) {
  2662. ret = 1;
  2663. goto end_no_trans;
  2664. }
  2665. if (root->fs_info->last_trans_log_full_commit >
  2666. root->fs_info->last_trans_committed) {
  2667. ret = 1;
  2668. goto end_no_trans;
  2669. }
  2670. if (root != BTRFS_I(inode)->root ||
  2671. btrfs_root_refs(&root->root_item) == 0) {
  2672. ret = 1;
  2673. goto end_no_trans;
  2674. }
  2675. ret = check_parent_dirs_for_sync(trans, inode, parent,
  2676. sb, last_committed);
  2677. if (ret)
  2678. goto end_no_trans;
  2679. if (inode_in_log(trans, inode)) {
  2680. ret = BTRFS_NO_LOG_SYNC;
  2681. goto end_no_trans;
  2682. }
  2683. ret = start_log_trans(trans, root);
  2684. if (ret)
  2685. goto end_trans;
  2686. ret = btrfs_log_inode(trans, root, inode, inode_only);
  2687. if (ret)
  2688. goto end_trans;
  2689. /*
  2690. * for regular files, if its inode is already on disk, we don't
  2691. * have to worry about the parents at all. This is because
  2692. * we can use the last_unlink_trans field to record renames
  2693. * and other fun in this file.
  2694. */
  2695. if (S_ISREG(inode->i_mode) &&
  2696. BTRFS_I(inode)->generation <= last_committed &&
  2697. BTRFS_I(inode)->last_unlink_trans <= last_committed) {
  2698. ret = 0;
  2699. goto end_trans;
  2700. }
  2701. inode_only = LOG_INODE_EXISTS;
  2702. while (1) {
  2703. if (!parent || !parent->d_inode || sb != parent->d_inode->i_sb)
  2704. break;
  2705. inode = parent->d_inode;
  2706. if (root != BTRFS_I(inode)->root)
  2707. break;
  2708. if (BTRFS_I(inode)->generation >
  2709. root->fs_info->last_trans_committed) {
  2710. ret = btrfs_log_inode(trans, root, inode, inode_only);
  2711. if (ret)
  2712. goto end_trans;
  2713. }
  2714. if (IS_ROOT(parent))
  2715. break;
  2716. parent = dget_parent(parent);
  2717. dput(old_parent);
  2718. old_parent = parent;
  2719. }
  2720. ret = 0;
  2721. end_trans:
  2722. dput(old_parent);
  2723. if (ret < 0) {
  2724. BUG_ON(ret != -ENOSPC);
  2725. root->fs_info->last_trans_log_full_commit = trans->transid;
  2726. ret = 1;
  2727. }
  2728. btrfs_end_log_trans(root);
  2729. end_no_trans:
  2730. return ret;
  2731. }
  2732. /*
  2733. * it is not safe to log dentry if the chunk root has added new
  2734. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  2735. * If this returns 1, you must commit the transaction to safely get your
  2736. * data on disk.
  2737. */
  2738. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  2739. struct btrfs_root *root, struct dentry *dentry)
  2740. {
  2741. struct dentry *parent = dget_parent(dentry);
  2742. int ret;
  2743. ret = btrfs_log_inode_parent(trans, root, dentry->d_inode, parent, 0);
  2744. dput(parent);
  2745. return ret;
  2746. }
  2747. /*
  2748. * should be called during mount to recover any replay any log trees
  2749. * from the FS
  2750. */
  2751. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  2752. {
  2753. int ret;
  2754. struct btrfs_path *path;
  2755. struct btrfs_trans_handle *trans;
  2756. struct btrfs_key key;
  2757. struct btrfs_key found_key;
  2758. struct btrfs_key tmp_key;
  2759. struct btrfs_root *log;
  2760. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  2761. struct walk_control wc = {
  2762. .process_func = process_one_buffer,
  2763. .stage = 0,
  2764. };
  2765. fs_info->log_root_recovering = 1;
  2766. path = btrfs_alloc_path();
  2767. BUG_ON(!path);
  2768. trans = btrfs_start_transaction(fs_info->tree_root, 0);
  2769. BUG_ON(IS_ERR(trans));
  2770. wc.trans = trans;
  2771. wc.pin = 1;
  2772. walk_log_tree(trans, log_root_tree, &wc);
  2773. again:
  2774. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  2775. key.offset = (u64)-1;
  2776. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  2777. while (1) {
  2778. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  2779. if (ret < 0)
  2780. break;
  2781. if (ret > 0) {
  2782. if (path->slots[0] == 0)
  2783. break;
  2784. path->slots[0]--;
  2785. }
  2786. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2787. path->slots[0]);
  2788. btrfs_release_path(log_root_tree, path);
  2789. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  2790. break;
  2791. log = btrfs_read_fs_root_no_radix(log_root_tree,
  2792. &found_key);
  2793. BUG_ON(!log);
  2794. tmp_key.objectid = found_key.offset;
  2795. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  2796. tmp_key.offset = (u64)-1;
  2797. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  2798. BUG_ON(!wc.replay_dest);
  2799. wc.replay_dest->log_root = log;
  2800. btrfs_record_root_in_trans(trans, wc.replay_dest);
  2801. ret = walk_log_tree(trans, log, &wc);
  2802. BUG_ON(ret);
  2803. if (wc.stage == LOG_WALK_REPLAY_ALL) {
  2804. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  2805. path);
  2806. BUG_ON(ret);
  2807. }
  2808. key.offset = found_key.offset - 1;
  2809. wc.replay_dest->log_root = NULL;
  2810. free_extent_buffer(log->node);
  2811. free_extent_buffer(log->commit_root);
  2812. kfree(log);
  2813. if (found_key.offset == 0)
  2814. break;
  2815. }
  2816. btrfs_release_path(log_root_tree, path);
  2817. /* step one is to pin it all, step two is to replay just inodes */
  2818. if (wc.pin) {
  2819. wc.pin = 0;
  2820. wc.process_func = replay_one_buffer;
  2821. wc.stage = LOG_WALK_REPLAY_INODES;
  2822. goto again;
  2823. }
  2824. /* step three is to replay everything */
  2825. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  2826. wc.stage++;
  2827. goto again;
  2828. }
  2829. btrfs_free_path(path);
  2830. free_extent_buffer(log_root_tree->node);
  2831. log_root_tree->log_root = NULL;
  2832. fs_info->log_root_recovering = 0;
  2833. /* step 4: commit the transaction, which also unpins the blocks */
  2834. btrfs_commit_transaction(trans, fs_info->tree_root);
  2835. kfree(log_root_tree);
  2836. return 0;
  2837. }
  2838. /*
  2839. * there are some corner cases where we want to force a full
  2840. * commit instead of allowing a directory to be logged.
  2841. *
  2842. * They revolve around files there were unlinked from the directory, and
  2843. * this function updates the parent directory so that a full commit is
  2844. * properly done if it is fsync'd later after the unlinks are done.
  2845. */
  2846. void btrfs_record_unlink_dir(struct btrfs_trans_handle *trans,
  2847. struct inode *dir, struct inode *inode,
  2848. int for_rename)
  2849. {
  2850. /*
  2851. * when we're logging a file, if it hasn't been renamed
  2852. * or unlinked, and its inode is fully committed on disk,
  2853. * we don't have to worry about walking up the directory chain
  2854. * to log its parents.
  2855. *
  2856. * So, we use the last_unlink_trans field to put this transid
  2857. * into the file. When the file is logged we check it and
  2858. * don't log the parents if the file is fully on disk.
  2859. */
  2860. if (S_ISREG(inode->i_mode))
  2861. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  2862. /*
  2863. * if this directory was already logged any new
  2864. * names for this file/dir will get recorded
  2865. */
  2866. smp_mb();
  2867. if (BTRFS_I(dir)->logged_trans == trans->transid)
  2868. return;
  2869. /*
  2870. * if the inode we're about to unlink was logged,
  2871. * the log will be properly updated for any new names
  2872. */
  2873. if (BTRFS_I(inode)->logged_trans == trans->transid)
  2874. return;
  2875. /*
  2876. * when renaming files across directories, if the directory
  2877. * there we're unlinking from gets fsync'd later on, there's
  2878. * no way to find the destination directory later and fsync it
  2879. * properly. So, we have to be conservative and force commits
  2880. * so the new name gets discovered.
  2881. */
  2882. if (for_rename)
  2883. goto record;
  2884. /* we can safely do the unlink without any special recording */
  2885. return;
  2886. record:
  2887. BTRFS_I(dir)->last_unlink_trans = trans->transid;
  2888. }
  2889. /*
  2890. * Call this after adding a new name for a file and it will properly
  2891. * update the log to reflect the new name.
  2892. *
  2893. * It will return zero if all goes well, and it will return 1 if a
  2894. * full transaction commit is required.
  2895. */
  2896. int btrfs_log_new_name(struct btrfs_trans_handle *trans,
  2897. struct inode *inode, struct inode *old_dir,
  2898. struct dentry *parent)
  2899. {
  2900. struct btrfs_root * root = BTRFS_I(inode)->root;
  2901. /*
  2902. * this will force the logging code to walk the dentry chain
  2903. * up for the file
  2904. */
  2905. if (S_ISREG(inode->i_mode))
  2906. BTRFS_I(inode)->last_unlink_trans = trans->transid;
  2907. /*
  2908. * if this inode hasn't been logged and directory we're renaming it
  2909. * from hasn't been logged, we don't need to log it
  2910. */
  2911. if (BTRFS_I(inode)->logged_trans <=
  2912. root->fs_info->last_trans_committed &&
  2913. (!old_dir || BTRFS_I(old_dir)->logged_trans <=
  2914. root->fs_info->last_trans_committed))
  2915. return 0;
  2916. return btrfs_log_inode_parent(trans, root, inode, parent, 1);
  2917. }