tree-log.c 84 KB

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