tree-log.c 84 KB

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