tree-log.c 84 KB

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