tree-log.c 76 KB

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