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