tree-log.c 74 KB

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