tree-log.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890
  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, 0,
  86. BTRFS_TREE_LOG_OBJECTID,
  87. trans->transid, 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->nbytes = cpu_to_le64(root->leafsize);
  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. if (overwrite_root &&
  385. key->type == BTRFS_EXTENT_DATA_KEY) {
  386. int extent_type;
  387. struct btrfs_file_extent_item *fi;
  388. fi = (struct btrfs_file_extent_item *)dst_ptr;
  389. extent_type = btrfs_file_extent_type(path->nodes[0], fi);
  390. if (extent_type == BTRFS_FILE_EXTENT_REG) {
  391. struct btrfs_key ins;
  392. ins.objectid = btrfs_file_extent_disk_bytenr(
  393. path->nodes[0], fi);
  394. ins.offset = btrfs_file_extent_disk_num_bytes(
  395. path->nodes[0], fi);
  396. ins.type = BTRFS_EXTENT_ITEM_KEY;
  397. /*
  398. * is this extent already allocated in the extent
  399. * allocation tree? If so, just add a reference
  400. */
  401. ret = btrfs_lookup_extent(root, ins.objectid,
  402. ins.offset);
  403. if (ret == 0) {
  404. ret = btrfs_inc_extent_ref(trans, root,
  405. ins.objectid, ins.offset,
  406. path->nodes[0]->start,
  407. root->root_key.objectid,
  408. trans->transid, key->objectid);
  409. } else {
  410. /*
  411. * insert the extent pointer in the extent
  412. * allocation tree
  413. */
  414. ret = btrfs_alloc_logged_extent(trans, root,
  415. path->nodes[0]->start,
  416. root->root_key.objectid,
  417. trans->transid, key->objectid,
  418. &ins);
  419. BUG_ON(ret);
  420. }
  421. }
  422. }
  423. no_copy:
  424. btrfs_mark_buffer_dirty(path->nodes[0]);
  425. btrfs_release_path(root, path);
  426. return 0;
  427. }
  428. /*
  429. * simple helper to read an inode off the disk from a given root
  430. * This can only be called for subvolume roots and not for the log
  431. */
  432. static noinline struct inode *read_one_inode(struct btrfs_root *root,
  433. u64 objectid)
  434. {
  435. struct inode *inode;
  436. inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
  437. if (inode->i_state & I_NEW) {
  438. BTRFS_I(inode)->root = root;
  439. BTRFS_I(inode)->location.objectid = objectid;
  440. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  441. BTRFS_I(inode)->location.offset = 0;
  442. btrfs_read_locked_inode(inode);
  443. unlock_new_inode(inode);
  444. }
  445. if (is_bad_inode(inode)) {
  446. iput(inode);
  447. inode = NULL;
  448. }
  449. return inode;
  450. }
  451. /* replays a single extent in 'eb' at 'slot' with 'key' into the
  452. * subvolume 'root'. path is released on entry and should be released
  453. * on exit.
  454. *
  455. * extents in the log tree have not been allocated out of the extent
  456. * tree yet. So, this completes the allocation, taking a reference
  457. * as required if the extent already exists or creating a new extent
  458. * if it isn't in the extent allocation tree yet.
  459. *
  460. * The extent is inserted into the file, dropping any existing extents
  461. * from the file that overlap the new one.
  462. */
  463. static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
  464. struct btrfs_root *root,
  465. struct btrfs_path *path,
  466. struct extent_buffer *eb, int slot,
  467. struct btrfs_key *key)
  468. {
  469. int found_type;
  470. u64 mask = root->sectorsize - 1;
  471. u64 extent_end;
  472. u64 alloc_hint;
  473. u64 start = key->offset;
  474. struct btrfs_file_extent_item *item;
  475. struct inode *inode = NULL;
  476. unsigned long size;
  477. int ret = 0;
  478. item = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
  479. found_type = btrfs_file_extent_type(eb, item);
  480. if (found_type == BTRFS_FILE_EXTENT_REG)
  481. extent_end = start + btrfs_file_extent_num_bytes(eb, item);
  482. else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  483. size = btrfs_file_extent_inline_len(eb,
  484. btrfs_item_nr(eb, slot));
  485. extent_end = (start + size + mask) & ~mask;
  486. } else {
  487. ret = 0;
  488. goto out;
  489. }
  490. inode = read_one_inode(root, key->objectid);
  491. if (!inode) {
  492. ret = -EIO;
  493. goto out;
  494. }
  495. /*
  496. * first check to see if we already have this extent in the
  497. * file. This must be done before the btrfs_drop_extents run
  498. * so we don't try to drop this extent.
  499. */
  500. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  501. start, 0);
  502. if (ret == 0 && found_type == BTRFS_FILE_EXTENT_REG) {
  503. struct btrfs_file_extent_item cmp1;
  504. struct btrfs_file_extent_item cmp2;
  505. struct btrfs_file_extent_item *existing;
  506. struct extent_buffer *leaf;
  507. leaf = path->nodes[0];
  508. existing = btrfs_item_ptr(leaf, path->slots[0],
  509. struct btrfs_file_extent_item);
  510. read_extent_buffer(eb, &cmp1, (unsigned long)item,
  511. sizeof(cmp1));
  512. read_extent_buffer(leaf, &cmp2, (unsigned long)existing,
  513. sizeof(cmp2));
  514. /*
  515. * we already have a pointer to this exact extent,
  516. * we don't have to do anything
  517. */
  518. if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) {
  519. btrfs_release_path(root, path);
  520. goto out;
  521. }
  522. }
  523. btrfs_release_path(root, path);
  524. /* drop any overlapping extents */
  525. ret = btrfs_drop_extents(trans, root, inode,
  526. start, extent_end, start, &alloc_hint);
  527. BUG_ON(ret);
  528. /* insert the extent */
  529. ret = overwrite_item(trans, root, path, eb, slot, key);
  530. BUG_ON(ret);
  531. /* btrfs_drop_extents changes i_bytes & i_blocks, update it here */
  532. inode_add_bytes(inode, extent_end - start);
  533. btrfs_update_inode(trans, root, inode);
  534. out:
  535. if (inode)
  536. iput(inode);
  537. return ret;
  538. }
  539. /*
  540. * when cleaning up conflicts between the directory names in the
  541. * subvolume, directory names in the log and directory names in the
  542. * inode back references, we may have to unlink inodes from directories.
  543. *
  544. * This is a helper function to do the unlink of a specific directory
  545. * item
  546. */
  547. static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
  548. struct btrfs_root *root,
  549. struct btrfs_path *path,
  550. struct inode *dir,
  551. struct btrfs_dir_item *di)
  552. {
  553. struct inode *inode;
  554. char *name;
  555. int name_len;
  556. struct extent_buffer *leaf;
  557. struct btrfs_key location;
  558. int ret;
  559. leaf = path->nodes[0];
  560. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  561. name_len = btrfs_dir_name_len(leaf, di);
  562. name = kmalloc(name_len, GFP_NOFS);
  563. read_extent_buffer(leaf, name, (unsigned long)(di + 1), name_len);
  564. btrfs_release_path(root, path);
  565. inode = read_one_inode(root, location.objectid);
  566. BUG_ON(!inode);
  567. btrfs_inc_nlink(inode);
  568. ret = btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  569. kfree(name);
  570. iput(inode);
  571. return ret;
  572. }
  573. /*
  574. * helper function to see if a given name and sequence number found
  575. * in an inode back reference are already in a directory and correctly
  576. * point to this inode
  577. */
  578. static noinline int inode_in_dir(struct btrfs_root *root,
  579. struct btrfs_path *path,
  580. u64 dirid, u64 objectid, u64 index,
  581. const char *name, int name_len)
  582. {
  583. struct btrfs_dir_item *di;
  584. struct btrfs_key location;
  585. int match = 0;
  586. di = btrfs_lookup_dir_index_item(NULL, root, path, dirid,
  587. index, name, name_len, 0);
  588. if (di && !IS_ERR(di)) {
  589. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  590. if (location.objectid != objectid)
  591. goto out;
  592. } else
  593. goto out;
  594. btrfs_release_path(root, path);
  595. di = btrfs_lookup_dir_item(NULL, root, path, dirid, name, name_len, 0);
  596. if (di && !IS_ERR(di)) {
  597. btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
  598. if (location.objectid != objectid)
  599. goto out;
  600. } else
  601. goto out;
  602. match = 1;
  603. out:
  604. btrfs_release_path(root, path);
  605. return match;
  606. }
  607. /*
  608. * helper function to check a log tree for a named back reference in
  609. * an inode. This is used to decide if a back reference that is
  610. * found in the subvolume conflicts with what we find in the log.
  611. *
  612. * inode backreferences may have multiple refs in a single item,
  613. * during replay we process one reference at a time, and we don't
  614. * want to delete valid links to a file from the subvolume if that
  615. * link is also in the log.
  616. */
  617. static noinline int backref_in_log(struct btrfs_root *log,
  618. struct btrfs_key *key,
  619. char *name, int namelen)
  620. {
  621. struct btrfs_path *path;
  622. struct btrfs_inode_ref *ref;
  623. unsigned long ptr;
  624. unsigned long ptr_end;
  625. unsigned long name_ptr;
  626. int found_name_len;
  627. int item_size;
  628. int ret;
  629. int match = 0;
  630. path = btrfs_alloc_path();
  631. ret = btrfs_search_slot(NULL, log, key, path, 0, 0);
  632. if (ret != 0)
  633. goto out;
  634. item_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
  635. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  636. ptr_end = ptr + item_size;
  637. while (ptr < ptr_end) {
  638. ref = (struct btrfs_inode_ref *)ptr;
  639. found_name_len = btrfs_inode_ref_name_len(path->nodes[0], ref);
  640. if (found_name_len == namelen) {
  641. name_ptr = (unsigned long)(ref + 1);
  642. ret = memcmp_extent_buffer(path->nodes[0], name,
  643. name_ptr, namelen);
  644. if (ret == 0) {
  645. match = 1;
  646. goto out;
  647. }
  648. }
  649. ptr = (unsigned long)(ref + 1) + found_name_len;
  650. }
  651. out:
  652. btrfs_free_path(path);
  653. return match;
  654. }
  655. /*
  656. * replay one inode back reference item found in the log tree.
  657. * eb, slot and key refer to the buffer and key found in the log tree.
  658. * root is the destination we are replaying into, and path is for temp
  659. * use by this function. (it should be released on return).
  660. */
  661. static noinline int add_inode_ref(struct btrfs_trans_handle *trans,
  662. struct btrfs_root *root,
  663. struct btrfs_root *log,
  664. struct btrfs_path *path,
  665. struct extent_buffer *eb, int slot,
  666. struct btrfs_key *key)
  667. {
  668. struct inode *dir;
  669. int ret;
  670. struct btrfs_key location;
  671. struct btrfs_inode_ref *ref;
  672. struct btrfs_dir_item *di;
  673. struct inode *inode;
  674. char *name;
  675. int namelen;
  676. unsigned long ref_ptr;
  677. unsigned long ref_end;
  678. location.objectid = key->objectid;
  679. location.type = BTRFS_INODE_ITEM_KEY;
  680. location.offset = 0;
  681. /*
  682. * it is possible that we didn't log all the parent directories
  683. * for a given inode. If we don't find the dir, just don't
  684. * copy the back ref in. The link count fixup code will take
  685. * care of the rest
  686. */
  687. dir = read_one_inode(root, key->offset);
  688. if (!dir)
  689. return -ENOENT;
  690. inode = read_one_inode(root, key->objectid);
  691. BUG_ON(!dir);
  692. ref_ptr = btrfs_item_ptr_offset(eb, slot);
  693. ref_end = ref_ptr + btrfs_item_size_nr(eb, slot);
  694. again:
  695. ref = (struct btrfs_inode_ref *)ref_ptr;
  696. namelen = btrfs_inode_ref_name_len(eb, ref);
  697. name = kmalloc(namelen, GFP_NOFS);
  698. BUG_ON(!name);
  699. read_extent_buffer(eb, name, (unsigned long)(ref + 1), namelen);
  700. /* if we already have a perfect match, we're done */
  701. if (inode_in_dir(root, path, dir->i_ino, inode->i_ino,
  702. btrfs_inode_ref_index(eb, ref),
  703. name, namelen)) {
  704. goto out;
  705. }
  706. /*
  707. * look for a conflicting back reference in the metadata.
  708. * if we find one we have to unlink that name of the file
  709. * before we add our new link. Later on, we overwrite any
  710. * existing back reference, and we don't want to create
  711. * dangling pointers in the directory.
  712. */
  713. conflict_again:
  714. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  715. if (ret == 0) {
  716. char *victim_name;
  717. int victim_name_len;
  718. struct btrfs_inode_ref *victim_ref;
  719. unsigned long ptr;
  720. unsigned long ptr_end;
  721. struct extent_buffer *leaf = path->nodes[0];
  722. /* are we trying to overwrite a back ref for the root directory
  723. * if so, just jump out, we're done
  724. */
  725. if (key->objectid == key->offset)
  726. goto out_nowrite;
  727. /* check all the names in this back reference to see
  728. * if they are in the log. if so, we allow them to stay
  729. * otherwise they must be unlinked as a conflict
  730. */
  731. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  732. ptr_end = ptr + btrfs_item_size_nr(leaf, path->slots[0]);
  733. while(ptr < ptr_end) {
  734. victim_ref = (struct btrfs_inode_ref *)ptr;
  735. victim_name_len = btrfs_inode_ref_name_len(leaf,
  736. victim_ref);
  737. victim_name = kmalloc(victim_name_len, GFP_NOFS);
  738. BUG_ON(!victim_name);
  739. read_extent_buffer(leaf, victim_name,
  740. (unsigned long)(victim_ref + 1),
  741. victim_name_len);
  742. if (!backref_in_log(log, key, victim_name,
  743. victim_name_len)) {
  744. btrfs_inc_nlink(inode);
  745. btrfs_release_path(root, path);
  746. ret = btrfs_unlink_inode(trans, root, dir,
  747. inode, victim_name,
  748. victim_name_len);
  749. kfree(victim_name);
  750. btrfs_release_path(root, path);
  751. goto conflict_again;
  752. }
  753. kfree(victim_name);
  754. ptr = (unsigned long)(victim_ref + 1) + victim_name_len;
  755. }
  756. BUG_ON(ret);
  757. }
  758. btrfs_release_path(root, path);
  759. /* look for a conflicting sequence number */
  760. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  761. btrfs_inode_ref_index(eb, ref),
  762. name, namelen, 0);
  763. if (di && !IS_ERR(di)) {
  764. ret = drop_one_dir_item(trans, root, path, dir, di);
  765. BUG_ON(ret);
  766. }
  767. btrfs_release_path(root, path);
  768. /* look for a conflicting name */
  769. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  770. name, namelen, 0);
  771. if (di && !IS_ERR(di)) {
  772. ret = drop_one_dir_item(trans, root, path, dir, di);
  773. BUG_ON(ret);
  774. }
  775. btrfs_release_path(root, path);
  776. /* insert our name */
  777. ret = btrfs_add_link(trans, dir, inode, name, namelen, 0,
  778. btrfs_inode_ref_index(eb, ref));
  779. BUG_ON(ret);
  780. btrfs_update_inode(trans, root, inode);
  781. out:
  782. ref_ptr = (unsigned long)(ref + 1) + namelen;
  783. kfree(name);
  784. if (ref_ptr < ref_end)
  785. goto again;
  786. /* finally write the back reference in the inode */
  787. ret = overwrite_item(trans, root, path, eb, slot, key);
  788. BUG_ON(ret);
  789. out_nowrite:
  790. btrfs_release_path(root, path);
  791. iput(dir);
  792. iput(inode);
  793. return 0;
  794. }
  795. /*
  796. * replay one csum item from the log tree into the subvolume 'root'
  797. * eb, slot and key all refer to the log tree
  798. * path is for temp use by this function and should be released on return
  799. *
  800. * This copies the checksums out of the log tree and inserts them into
  801. * the subvolume. Any existing checksums for this range in the file
  802. * are overwritten, and new items are added where required.
  803. *
  804. * We keep this simple by reusing the btrfs_ordered_sum code from
  805. * the data=ordered mode. This basically means making a copy
  806. * of all the checksums in ram, which we have to do anyway for kmap
  807. * rules.
  808. *
  809. * The copy is then sent down to btrfs_csum_file_blocks, which
  810. * does all the hard work of finding existing items in the file
  811. * or adding new ones.
  812. */
  813. static noinline int replay_one_csum(struct btrfs_trans_handle *trans,
  814. struct btrfs_root *root,
  815. struct btrfs_path *path,
  816. struct extent_buffer *eb, int slot,
  817. struct btrfs_key *key)
  818. {
  819. int ret;
  820. u32 item_size = btrfs_item_size_nr(eb, slot);
  821. u64 cur_offset;
  822. unsigned long file_bytes;
  823. struct btrfs_ordered_sum *sums;
  824. struct btrfs_sector_sum *sector_sum;
  825. struct inode *inode;
  826. unsigned long ptr;
  827. file_bytes = (item_size / BTRFS_CRC32_SIZE) * root->sectorsize;
  828. inode = read_one_inode(root, key->objectid);
  829. if (!inode) {
  830. return -EIO;
  831. }
  832. sums = kzalloc(btrfs_ordered_sum_size(root, file_bytes), GFP_NOFS);
  833. if (!sums) {
  834. iput(inode);
  835. return -ENOMEM;
  836. }
  837. INIT_LIST_HEAD(&sums->list);
  838. sums->len = file_bytes;
  839. sums->file_offset = key->offset;
  840. /*
  841. * copy all the sums into the ordered sum struct
  842. */
  843. sector_sum = sums->sums;
  844. cur_offset = key->offset;
  845. ptr = btrfs_item_ptr_offset(eb, slot);
  846. while(item_size > 0) {
  847. sector_sum->offset = cur_offset;
  848. read_extent_buffer(eb, &sector_sum->sum, ptr, BTRFS_CRC32_SIZE);
  849. sector_sum++;
  850. item_size -= BTRFS_CRC32_SIZE;
  851. ptr += BTRFS_CRC32_SIZE;
  852. cur_offset += root->sectorsize;
  853. }
  854. /* let btrfs_csum_file_blocks add them into the file */
  855. ret = btrfs_csum_file_blocks(trans, root, inode, sums);
  856. BUG_ON(ret);
  857. kfree(sums);
  858. iput(inode);
  859. return 0;
  860. }
  861. /*
  862. * There are a few corners where the link count of the file can't
  863. * be properly maintained during replay. So, instead of adding
  864. * lots of complexity to the log code, we just scan the backrefs
  865. * for any file that has been through replay.
  866. *
  867. * The scan will update the link count on the inode to reflect the
  868. * number of back refs found. If it goes down to zero, the iput
  869. * will free the inode.
  870. */
  871. static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
  872. struct btrfs_root *root,
  873. struct inode *inode)
  874. {
  875. struct btrfs_path *path;
  876. int ret;
  877. struct btrfs_key key;
  878. u64 nlink = 0;
  879. unsigned long ptr;
  880. unsigned long ptr_end;
  881. int name_len;
  882. key.objectid = inode->i_ino;
  883. key.type = BTRFS_INODE_REF_KEY;
  884. key.offset = (u64)-1;
  885. path = btrfs_alloc_path();
  886. while(1) {
  887. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  888. if (ret < 0)
  889. break;
  890. if (ret > 0) {
  891. if (path->slots[0] == 0)
  892. break;
  893. path->slots[0]--;
  894. }
  895. btrfs_item_key_to_cpu(path->nodes[0], &key,
  896. path->slots[0]);
  897. if (key.objectid != inode->i_ino ||
  898. key.type != BTRFS_INODE_REF_KEY)
  899. break;
  900. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  901. ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
  902. path->slots[0]);
  903. while(ptr < ptr_end) {
  904. struct btrfs_inode_ref *ref;
  905. ref = (struct btrfs_inode_ref *)ptr;
  906. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  907. ref);
  908. ptr = (unsigned long)(ref + 1) + name_len;
  909. nlink++;
  910. }
  911. if (key.offset == 0)
  912. break;
  913. key.offset--;
  914. btrfs_release_path(root, path);
  915. }
  916. btrfs_free_path(path);
  917. if (nlink != inode->i_nlink) {
  918. inode->i_nlink = nlink;
  919. btrfs_update_inode(trans, root, inode);
  920. }
  921. BTRFS_I(inode)->index_cnt = (u64)-1;
  922. return 0;
  923. }
  924. static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
  925. struct btrfs_root *root,
  926. struct btrfs_path *path)
  927. {
  928. int ret;
  929. struct btrfs_key key;
  930. struct inode *inode;
  931. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  932. key.type = BTRFS_ORPHAN_ITEM_KEY;
  933. key.offset = (u64)-1;
  934. while(1) {
  935. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  936. if (ret < 0)
  937. break;
  938. if (ret == 1) {
  939. if (path->slots[0] == 0)
  940. break;
  941. path->slots[0]--;
  942. }
  943. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  944. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  945. key.type != BTRFS_ORPHAN_ITEM_KEY)
  946. break;
  947. ret = btrfs_del_item(trans, root, path);
  948. BUG_ON(ret);
  949. btrfs_release_path(root, path);
  950. inode = read_one_inode(root, key.offset);
  951. BUG_ON(!inode);
  952. ret = fixup_inode_link_count(trans, root, inode);
  953. BUG_ON(ret);
  954. iput(inode);
  955. if (key.offset == 0)
  956. break;
  957. key.offset--;
  958. }
  959. btrfs_release_path(root, path);
  960. return 0;
  961. }
  962. /*
  963. * record a given inode in the fixup dir so we can check its link
  964. * count when replay is done. The link count is incremented here
  965. * so the inode won't go away until we check it
  966. */
  967. static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  968. struct btrfs_root *root,
  969. struct btrfs_path *path,
  970. u64 objectid)
  971. {
  972. struct btrfs_key key;
  973. int ret = 0;
  974. struct inode *inode;
  975. inode = read_one_inode(root, objectid);
  976. BUG_ON(!inode);
  977. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  978. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  979. key.offset = objectid;
  980. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  981. btrfs_release_path(root, path);
  982. if (ret == 0) {
  983. btrfs_inc_nlink(inode);
  984. btrfs_update_inode(trans, root, inode);
  985. } else if (ret == -EEXIST) {
  986. ret = 0;
  987. } else {
  988. BUG();
  989. }
  990. iput(inode);
  991. return ret;
  992. }
  993. /*
  994. * when replaying the log for a directory, we only insert names
  995. * for inodes that actually exist. This means an fsync on a directory
  996. * does not implicitly fsync all the new files in it
  997. */
  998. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  999. struct btrfs_root *root,
  1000. struct btrfs_path *path,
  1001. u64 dirid, u64 index,
  1002. char *name, int name_len, u8 type,
  1003. struct btrfs_key *location)
  1004. {
  1005. struct inode *inode;
  1006. struct inode *dir;
  1007. int ret;
  1008. inode = read_one_inode(root, location->objectid);
  1009. if (!inode)
  1010. return -ENOENT;
  1011. dir = read_one_inode(root, dirid);
  1012. if (!dir) {
  1013. iput(inode);
  1014. return -EIO;
  1015. }
  1016. ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
  1017. /* FIXME, put inode into FIXUP list */
  1018. iput(inode);
  1019. iput(dir);
  1020. return ret;
  1021. }
  1022. /*
  1023. * take a single entry in a log directory item and replay it into
  1024. * the subvolume.
  1025. *
  1026. * if a conflicting item exists in the subdirectory already,
  1027. * the inode it points to is unlinked and put into the link count
  1028. * fix up tree.
  1029. *
  1030. * If a name from the log points to a file or directory that does
  1031. * not exist in the FS, it is skipped. fsyncs on directories
  1032. * do not force down inodes inside that directory, just changes to the
  1033. * names or unlinks in a directory.
  1034. */
  1035. static noinline int replay_one_name(struct btrfs_trans_handle *trans,
  1036. struct btrfs_root *root,
  1037. struct btrfs_path *path,
  1038. struct extent_buffer *eb,
  1039. struct btrfs_dir_item *di,
  1040. struct btrfs_key *key)
  1041. {
  1042. char *name;
  1043. int name_len;
  1044. struct btrfs_dir_item *dst_di;
  1045. struct btrfs_key found_key;
  1046. struct btrfs_key log_key;
  1047. struct inode *dir;
  1048. u8 log_type;
  1049. int exists;
  1050. int ret;
  1051. dir = read_one_inode(root, key->objectid);
  1052. BUG_ON(!dir);
  1053. name_len = btrfs_dir_name_len(eb, di);
  1054. name = kmalloc(name_len, GFP_NOFS);
  1055. log_type = btrfs_dir_type(eb, di);
  1056. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1057. name_len);
  1058. btrfs_dir_item_key_to_cpu(eb, di, &log_key);
  1059. exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
  1060. if (exists == 0)
  1061. exists = 1;
  1062. else
  1063. exists = 0;
  1064. btrfs_release_path(root, path);
  1065. if (key->type == BTRFS_DIR_ITEM_KEY) {
  1066. dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
  1067. name, name_len, 1);
  1068. }
  1069. else if (key->type == BTRFS_DIR_INDEX_KEY) {
  1070. dst_di = btrfs_lookup_dir_index_item(trans, root, path,
  1071. key->objectid,
  1072. key->offset, name,
  1073. name_len, 1);
  1074. } else {
  1075. BUG();
  1076. }
  1077. if (!dst_di || IS_ERR(dst_di)) {
  1078. /* we need a sequence number to insert, so we only
  1079. * do inserts for the BTRFS_DIR_INDEX_KEY types
  1080. */
  1081. if (key->type != BTRFS_DIR_INDEX_KEY)
  1082. goto out;
  1083. goto insert;
  1084. }
  1085. btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
  1086. /* the existing item matches the logged item */
  1087. if (found_key.objectid == log_key.objectid &&
  1088. found_key.type == log_key.type &&
  1089. found_key.offset == log_key.offset &&
  1090. btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
  1091. goto out;
  1092. }
  1093. /*
  1094. * don't drop the conflicting directory entry if the inode
  1095. * for the new entry doesn't exist
  1096. */
  1097. if (!exists)
  1098. goto out;
  1099. ret = drop_one_dir_item(trans, root, path, dir, dst_di);
  1100. BUG_ON(ret);
  1101. if (key->type == BTRFS_DIR_INDEX_KEY)
  1102. goto insert;
  1103. out:
  1104. btrfs_release_path(root, path);
  1105. kfree(name);
  1106. iput(dir);
  1107. return 0;
  1108. insert:
  1109. btrfs_release_path(root, path);
  1110. ret = insert_one_name(trans, root, path, key->objectid, key->offset,
  1111. name, name_len, log_type, &log_key);
  1112. if (ret && ret != -ENOENT)
  1113. BUG();
  1114. goto out;
  1115. }
  1116. /*
  1117. * find all the names in a directory item and reconcile them into
  1118. * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
  1119. * one name in a directory item, but the same code gets used for
  1120. * both directory index types
  1121. */
  1122. static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
  1123. struct btrfs_root *root,
  1124. struct btrfs_path *path,
  1125. struct extent_buffer *eb, int slot,
  1126. struct btrfs_key *key)
  1127. {
  1128. int ret;
  1129. u32 item_size = btrfs_item_size_nr(eb, slot);
  1130. struct btrfs_dir_item *di;
  1131. int name_len;
  1132. unsigned long ptr;
  1133. unsigned long ptr_end;
  1134. ptr = btrfs_item_ptr_offset(eb, slot);
  1135. ptr_end = ptr + item_size;
  1136. while(ptr < ptr_end) {
  1137. di = (struct btrfs_dir_item *)ptr;
  1138. name_len = btrfs_dir_name_len(eb, di);
  1139. ret = replay_one_name(trans, root, path, eb, di, key);
  1140. BUG_ON(ret);
  1141. ptr = (unsigned long)(di + 1);
  1142. ptr += name_len;
  1143. }
  1144. return 0;
  1145. }
  1146. /*
  1147. * directory replay has two parts. There are the standard directory
  1148. * items in the log copied from the subvolume, and range items
  1149. * created in the log while the subvolume was logged.
  1150. *
  1151. * The range items tell us which parts of the key space the log
  1152. * is authoritative for. During replay, if a key in the subvolume
  1153. * directory is in a logged range item, but not actually in the log
  1154. * that means it was deleted from the directory before the fsync
  1155. * and should be removed.
  1156. */
  1157. static noinline int find_dir_range(struct btrfs_root *root,
  1158. struct btrfs_path *path,
  1159. u64 dirid, int key_type,
  1160. u64 *start_ret, u64 *end_ret)
  1161. {
  1162. struct btrfs_key key;
  1163. u64 found_end;
  1164. struct btrfs_dir_log_item *item;
  1165. int ret;
  1166. int nritems;
  1167. if (*start_ret == (u64)-1)
  1168. return 1;
  1169. key.objectid = dirid;
  1170. key.type = key_type;
  1171. key.offset = *start_ret;
  1172. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1173. if (ret < 0)
  1174. goto out;
  1175. if (ret > 0) {
  1176. if (path->slots[0] == 0)
  1177. goto out;
  1178. path->slots[0]--;
  1179. }
  1180. if (ret != 0)
  1181. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1182. if (key.type != key_type || key.objectid != dirid) {
  1183. ret = 1;
  1184. goto next;
  1185. }
  1186. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1187. struct btrfs_dir_log_item);
  1188. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1189. if (*start_ret >= key.offset && *start_ret <= found_end) {
  1190. ret = 0;
  1191. *start_ret = key.offset;
  1192. *end_ret = found_end;
  1193. goto out;
  1194. }
  1195. ret = 1;
  1196. next:
  1197. /* check the next slot in the tree to see if it is a valid item */
  1198. nritems = btrfs_header_nritems(path->nodes[0]);
  1199. if (path->slots[0] >= nritems) {
  1200. ret = btrfs_next_leaf(root, path);
  1201. if (ret)
  1202. goto out;
  1203. } else {
  1204. path->slots[0]++;
  1205. }
  1206. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1207. if (key.type != key_type || key.objectid != dirid) {
  1208. ret = 1;
  1209. goto out;
  1210. }
  1211. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1212. struct btrfs_dir_log_item);
  1213. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1214. *start_ret = key.offset;
  1215. *end_ret = found_end;
  1216. ret = 0;
  1217. out:
  1218. btrfs_release_path(root, path);
  1219. return ret;
  1220. }
  1221. /*
  1222. * this looks for a given directory item in the log. If the directory
  1223. * item is not in the log, the item is removed and the inode it points
  1224. * to is unlinked
  1225. */
  1226. static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
  1227. struct btrfs_root *root,
  1228. struct btrfs_root *log,
  1229. struct btrfs_path *path,
  1230. struct btrfs_path *log_path,
  1231. struct inode *dir,
  1232. struct btrfs_key *dir_key)
  1233. {
  1234. int ret;
  1235. struct extent_buffer *eb;
  1236. int slot;
  1237. u32 item_size;
  1238. struct btrfs_dir_item *di;
  1239. struct btrfs_dir_item *log_di;
  1240. int name_len;
  1241. unsigned long ptr;
  1242. unsigned long ptr_end;
  1243. char *name;
  1244. struct inode *inode;
  1245. struct btrfs_key location;
  1246. again:
  1247. eb = path->nodes[0];
  1248. slot = path->slots[0];
  1249. item_size = btrfs_item_size_nr(eb, slot);
  1250. ptr = btrfs_item_ptr_offset(eb, slot);
  1251. ptr_end = ptr + item_size;
  1252. while(ptr < ptr_end) {
  1253. di = (struct btrfs_dir_item *)ptr;
  1254. name_len = btrfs_dir_name_len(eb, di);
  1255. name = kmalloc(name_len, GFP_NOFS);
  1256. if (!name) {
  1257. ret = -ENOMEM;
  1258. goto out;
  1259. }
  1260. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1261. name_len);
  1262. log_di = NULL;
  1263. if (dir_key->type == BTRFS_DIR_ITEM_KEY) {
  1264. log_di = btrfs_lookup_dir_item(trans, log, log_path,
  1265. dir_key->objectid,
  1266. name, name_len, 0);
  1267. } else if (dir_key->type == BTRFS_DIR_INDEX_KEY) {
  1268. log_di = btrfs_lookup_dir_index_item(trans, log,
  1269. log_path,
  1270. dir_key->objectid,
  1271. dir_key->offset,
  1272. name, name_len, 0);
  1273. }
  1274. if (!log_di || IS_ERR(log_di)) {
  1275. btrfs_dir_item_key_to_cpu(eb, di, &location);
  1276. btrfs_release_path(root, path);
  1277. btrfs_release_path(log, log_path);
  1278. inode = read_one_inode(root, location.objectid);
  1279. BUG_ON(!inode);
  1280. ret = link_to_fixup_dir(trans, root,
  1281. path, location.objectid);
  1282. BUG_ON(ret);
  1283. btrfs_inc_nlink(inode);
  1284. ret = btrfs_unlink_inode(trans, root, dir, inode,
  1285. name, name_len);
  1286. BUG_ON(ret);
  1287. kfree(name);
  1288. iput(inode);
  1289. /* there might still be more names under this key
  1290. * check and repeat if required
  1291. */
  1292. ret = btrfs_search_slot(NULL, root, dir_key, path,
  1293. 0, 0);
  1294. if (ret == 0)
  1295. goto again;
  1296. ret = 0;
  1297. goto out;
  1298. }
  1299. btrfs_release_path(log, log_path);
  1300. kfree(name);
  1301. ptr = (unsigned long)(di + 1);
  1302. ptr += name_len;
  1303. }
  1304. ret = 0;
  1305. out:
  1306. btrfs_release_path(root, path);
  1307. btrfs_release_path(log, log_path);
  1308. return ret;
  1309. }
  1310. /*
  1311. * deletion replay happens before we copy any new directory items
  1312. * out of the log or out of backreferences from inodes. It
  1313. * scans the log to find ranges of keys that log is authoritative for,
  1314. * and then scans the directory to find items in those ranges that are
  1315. * not present in the log.
  1316. *
  1317. * Anything we don't find in the log is unlinked and removed from the
  1318. * directory.
  1319. */
  1320. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  1321. struct btrfs_root *root,
  1322. struct btrfs_root *log,
  1323. struct btrfs_path *path,
  1324. u64 dirid)
  1325. {
  1326. u64 range_start;
  1327. u64 range_end;
  1328. int key_type = BTRFS_DIR_LOG_ITEM_KEY;
  1329. int ret = 0;
  1330. struct btrfs_key dir_key;
  1331. struct btrfs_key found_key;
  1332. struct btrfs_path *log_path;
  1333. struct inode *dir;
  1334. dir_key.objectid = dirid;
  1335. dir_key.type = BTRFS_DIR_ITEM_KEY;
  1336. log_path = btrfs_alloc_path();
  1337. if (!log_path)
  1338. return -ENOMEM;
  1339. dir = read_one_inode(root, dirid);
  1340. /* it isn't an error if the inode isn't there, that can happen
  1341. * because we replay the deletes before we copy in the inode item
  1342. * from the log
  1343. */
  1344. if (!dir) {
  1345. btrfs_free_path(log_path);
  1346. return 0;
  1347. }
  1348. again:
  1349. range_start = 0;
  1350. range_end = 0;
  1351. while(1) {
  1352. ret = find_dir_range(log, path, dirid, key_type,
  1353. &range_start, &range_end);
  1354. if (ret != 0)
  1355. break;
  1356. dir_key.offset = range_start;
  1357. while(1) {
  1358. int nritems;
  1359. ret = btrfs_search_slot(NULL, root, &dir_key, path,
  1360. 0, 0);
  1361. if (ret < 0)
  1362. goto out;
  1363. nritems = btrfs_header_nritems(path->nodes[0]);
  1364. if (path->slots[0] >= nritems) {
  1365. ret = btrfs_next_leaf(root, path);
  1366. if (ret)
  1367. break;
  1368. }
  1369. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1370. path->slots[0]);
  1371. if (found_key.objectid != dirid ||
  1372. found_key.type != dir_key.type)
  1373. goto next_type;
  1374. if (found_key.offset > range_end)
  1375. break;
  1376. ret = check_item_in_log(trans, root, log, path,
  1377. log_path, dir, &found_key);
  1378. BUG_ON(ret);
  1379. if (found_key.offset == (u64)-1)
  1380. break;
  1381. dir_key.offset = found_key.offset + 1;
  1382. }
  1383. btrfs_release_path(root, path);
  1384. if (range_end == (u64)-1)
  1385. break;
  1386. range_start = range_end + 1;
  1387. }
  1388. next_type:
  1389. ret = 0;
  1390. if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
  1391. key_type = BTRFS_DIR_LOG_INDEX_KEY;
  1392. dir_key.type = BTRFS_DIR_INDEX_KEY;
  1393. btrfs_release_path(root, path);
  1394. goto again;
  1395. }
  1396. out:
  1397. btrfs_release_path(root, path);
  1398. btrfs_free_path(log_path);
  1399. iput(dir);
  1400. return ret;
  1401. }
  1402. /*
  1403. * the process_func used to replay items from the log tree. This
  1404. * gets called in two different stages. The first stage just looks
  1405. * for inodes and makes sure they are all copied into the subvolume.
  1406. *
  1407. * The second stage copies all the other item types from the log into
  1408. * the subvolume. The two stage approach is slower, but gets rid of
  1409. * lots of complexity around inodes referencing other inodes that exist
  1410. * only in the log (references come from either directory items or inode
  1411. * back refs).
  1412. */
  1413. static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
  1414. struct walk_control *wc, u64 gen)
  1415. {
  1416. int nritems;
  1417. struct btrfs_path *path;
  1418. struct btrfs_root *root = wc->replay_dest;
  1419. struct btrfs_key key;
  1420. u32 item_size;
  1421. int level;
  1422. int i;
  1423. int ret;
  1424. btrfs_read_buffer(eb, gen);
  1425. level = btrfs_header_level(eb);
  1426. if (level != 0)
  1427. return 0;
  1428. path = btrfs_alloc_path();
  1429. BUG_ON(!path);
  1430. nritems = btrfs_header_nritems(eb);
  1431. for (i = 0; i < nritems; i++) {
  1432. btrfs_item_key_to_cpu(eb, &key, i);
  1433. item_size = btrfs_item_size_nr(eb, i);
  1434. /* inode keys are done during the first stage */
  1435. if (key.type == BTRFS_INODE_ITEM_KEY &&
  1436. wc->stage == LOG_WALK_REPLAY_INODES) {
  1437. struct inode *inode;
  1438. struct btrfs_inode_item *inode_item;
  1439. u32 mode;
  1440. inode_item = btrfs_item_ptr(eb, i,
  1441. struct btrfs_inode_item);
  1442. mode = btrfs_inode_mode(eb, inode_item);
  1443. if (S_ISDIR(mode)) {
  1444. ret = replay_dir_deletes(wc->trans,
  1445. root, log, path, key.objectid);
  1446. BUG_ON(ret);
  1447. }
  1448. ret = overwrite_item(wc->trans, root, path,
  1449. eb, i, &key);
  1450. BUG_ON(ret);
  1451. /* for regular files, truncate away
  1452. * extents past the new EOF
  1453. */
  1454. if (S_ISREG(mode)) {
  1455. inode = read_one_inode(root,
  1456. key.objectid);
  1457. BUG_ON(!inode);
  1458. ret = btrfs_truncate_inode_items(wc->trans,
  1459. root, inode, inode->i_size,
  1460. BTRFS_EXTENT_DATA_KEY);
  1461. BUG_ON(ret);
  1462. iput(inode);
  1463. }
  1464. ret = link_to_fixup_dir(wc->trans, root,
  1465. path, key.objectid);
  1466. BUG_ON(ret);
  1467. }
  1468. if (wc->stage < LOG_WALK_REPLAY_ALL)
  1469. continue;
  1470. /* these keys are simply copied */
  1471. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  1472. ret = overwrite_item(wc->trans, root, path,
  1473. eb, i, &key);
  1474. BUG_ON(ret);
  1475. } else if (key.type == BTRFS_INODE_REF_KEY) {
  1476. ret = add_inode_ref(wc->trans, root, log, path,
  1477. eb, i, &key);
  1478. BUG_ON(ret && ret != -ENOENT);
  1479. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  1480. ret = replay_one_extent(wc->trans, root, path,
  1481. eb, i, &key);
  1482. BUG_ON(ret);
  1483. } else if (key.type == BTRFS_CSUM_ITEM_KEY) {
  1484. ret = replay_one_csum(wc->trans, root, path,
  1485. eb, i, &key);
  1486. BUG_ON(ret);
  1487. } else if (key.type == BTRFS_DIR_ITEM_KEY ||
  1488. key.type == BTRFS_DIR_INDEX_KEY) {
  1489. ret = replay_one_dir_item(wc->trans, root, path,
  1490. eb, i, &key);
  1491. BUG_ON(ret);
  1492. }
  1493. }
  1494. btrfs_free_path(path);
  1495. return 0;
  1496. }
  1497. static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans,
  1498. struct btrfs_root *root,
  1499. struct btrfs_path *path, int *level,
  1500. struct walk_control *wc)
  1501. {
  1502. u64 root_owner;
  1503. u64 root_gen;
  1504. u64 bytenr;
  1505. u64 ptr_gen;
  1506. struct extent_buffer *next;
  1507. struct extent_buffer *cur;
  1508. struct extent_buffer *parent;
  1509. u32 blocksize;
  1510. int ret = 0;
  1511. WARN_ON(*level < 0);
  1512. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1513. while(*level > 0) {
  1514. WARN_ON(*level < 0);
  1515. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1516. cur = path->nodes[*level];
  1517. if (btrfs_header_level(cur) != *level)
  1518. WARN_ON(1);
  1519. if (path->slots[*level] >=
  1520. btrfs_header_nritems(cur))
  1521. break;
  1522. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  1523. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  1524. blocksize = btrfs_level_size(root, *level - 1);
  1525. parent = path->nodes[*level];
  1526. root_owner = btrfs_header_owner(parent);
  1527. root_gen = btrfs_header_generation(parent);
  1528. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  1529. wc->process_func(root, next, wc, ptr_gen);
  1530. if (*level == 1) {
  1531. path->slots[*level]++;
  1532. if (wc->free) {
  1533. btrfs_read_buffer(next, ptr_gen);
  1534. btrfs_tree_lock(next);
  1535. clean_tree_block(trans, root, next);
  1536. btrfs_wait_tree_block_writeback(next);
  1537. btrfs_tree_unlock(next);
  1538. ret = btrfs_drop_leaf_ref(trans, root, next);
  1539. BUG_ON(ret);
  1540. WARN_ON(root_owner !=
  1541. BTRFS_TREE_LOG_OBJECTID);
  1542. ret = btrfs_free_reserved_extent(root,
  1543. bytenr, blocksize);
  1544. BUG_ON(ret);
  1545. }
  1546. free_extent_buffer(next);
  1547. continue;
  1548. }
  1549. btrfs_read_buffer(next, ptr_gen);
  1550. WARN_ON(*level <= 0);
  1551. if (path->nodes[*level-1])
  1552. free_extent_buffer(path->nodes[*level-1]);
  1553. path->nodes[*level-1] = next;
  1554. *level = btrfs_header_level(next);
  1555. path->slots[*level] = 0;
  1556. cond_resched();
  1557. }
  1558. WARN_ON(*level < 0);
  1559. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1560. if (path->nodes[*level] == root->node) {
  1561. parent = path->nodes[*level];
  1562. } else {
  1563. parent = path->nodes[*level + 1];
  1564. }
  1565. bytenr = path->nodes[*level]->start;
  1566. blocksize = btrfs_level_size(root, *level);
  1567. root_owner = btrfs_header_owner(parent);
  1568. root_gen = btrfs_header_generation(parent);
  1569. wc->process_func(root, path->nodes[*level], wc,
  1570. btrfs_header_generation(path->nodes[*level]));
  1571. if (wc->free) {
  1572. next = path->nodes[*level];
  1573. btrfs_tree_lock(next);
  1574. clean_tree_block(trans, root, next);
  1575. btrfs_wait_tree_block_writeback(next);
  1576. btrfs_tree_unlock(next);
  1577. if (*level == 0) {
  1578. ret = btrfs_drop_leaf_ref(trans, root, next);
  1579. BUG_ON(ret);
  1580. }
  1581. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  1582. ret = btrfs_free_reserved_extent(root, bytenr, blocksize);
  1583. BUG_ON(ret);
  1584. }
  1585. free_extent_buffer(path->nodes[*level]);
  1586. path->nodes[*level] = NULL;
  1587. *level += 1;
  1588. cond_resched();
  1589. return 0;
  1590. }
  1591. static int noinline walk_up_log_tree(struct btrfs_trans_handle *trans,
  1592. struct btrfs_root *root,
  1593. struct btrfs_path *path, int *level,
  1594. struct walk_control *wc)
  1595. {
  1596. u64 root_owner;
  1597. u64 root_gen;
  1598. int i;
  1599. int slot;
  1600. int ret;
  1601. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1602. slot = path->slots[i];
  1603. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  1604. struct extent_buffer *node;
  1605. node = path->nodes[i];
  1606. path->slots[i]++;
  1607. *level = i;
  1608. WARN_ON(*level == 0);
  1609. return 0;
  1610. } else {
  1611. struct extent_buffer *parent;
  1612. if (path->nodes[*level] == root->node)
  1613. parent = path->nodes[*level];
  1614. else
  1615. parent = path->nodes[*level + 1];
  1616. root_owner = btrfs_header_owner(parent);
  1617. root_gen = btrfs_header_generation(parent);
  1618. wc->process_func(root, path->nodes[*level], wc,
  1619. btrfs_header_generation(path->nodes[*level]));
  1620. if (wc->free) {
  1621. struct extent_buffer *next;
  1622. next = path->nodes[*level];
  1623. btrfs_tree_lock(next);
  1624. clean_tree_block(trans, root, next);
  1625. btrfs_wait_tree_block_writeback(next);
  1626. btrfs_tree_unlock(next);
  1627. if (*level == 0) {
  1628. ret = btrfs_drop_leaf_ref(trans, root,
  1629. next);
  1630. BUG_ON(ret);
  1631. }
  1632. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  1633. ret = btrfs_free_reserved_extent(root,
  1634. path->nodes[*level]->start,
  1635. path->nodes[*level]->len);
  1636. BUG_ON(ret);
  1637. }
  1638. free_extent_buffer(path->nodes[*level]);
  1639. path->nodes[*level] = NULL;
  1640. *level = i + 1;
  1641. }
  1642. }
  1643. return 1;
  1644. }
  1645. /*
  1646. * drop the reference count on the tree rooted at 'snap'. This traverses
  1647. * the tree freeing any blocks that have a ref count of zero after being
  1648. * decremented.
  1649. */
  1650. static int walk_log_tree(struct btrfs_trans_handle *trans,
  1651. struct btrfs_root *log, struct walk_control *wc)
  1652. {
  1653. int ret = 0;
  1654. int wret;
  1655. int level;
  1656. struct btrfs_path *path;
  1657. int i;
  1658. int orig_level;
  1659. path = btrfs_alloc_path();
  1660. BUG_ON(!path);
  1661. level = btrfs_header_level(log->node);
  1662. orig_level = level;
  1663. path->nodes[level] = log->node;
  1664. extent_buffer_get(log->node);
  1665. path->slots[level] = 0;
  1666. while(1) {
  1667. wret = walk_down_log_tree(trans, log, path, &level, wc);
  1668. if (wret > 0)
  1669. break;
  1670. if (wret < 0)
  1671. ret = wret;
  1672. wret = walk_up_log_tree(trans, log, path, &level, wc);
  1673. if (wret > 0)
  1674. break;
  1675. if (wret < 0)
  1676. ret = wret;
  1677. }
  1678. /* was the root node processed? if not, catch it here */
  1679. if (path->nodes[orig_level]) {
  1680. wc->process_func(log, path->nodes[orig_level], wc,
  1681. btrfs_header_generation(path->nodes[orig_level]));
  1682. if (wc->free) {
  1683. struct extent_buffer *next;
  1684. next = path->nodes[orig_level];
  1685. btrfs_tree_lock(next);
  1686. clean_tree_block(trans, log, next);
  1687. btrfs_wait_tree_block_writeback(next);
  1688. btrfs_tree_unlock(next);
  1689. if (orig_level == 0) {
  1690. ret = btrfs_drop_leaf_ref(trans, log,
  1691. next);
  1692. BUG_ON(ret);
  1693. }
  1694. WARN_ON(log->root_key.objectid !=
  1695. BTRFS_TREE_LOG_OBJECTID);
  1696. ret = btrfs_free_reserved_extent(log, next->start,
  1697. next->len);
  1698. BUG_ON(ret);
  1699. }
  1700. }
  1701. for (i = 0; i <= orig_level; i++) {
  1702. if (path->nodes[i]) {
  1703. free_extent_buffer(path->nodes[i]);
  1704. path->nodes[i] = NULL;
  1705. }
  1706. }
  1707. btrfs_free_path(path);
  1708. if (wc->free)
  1709. free_extent_buffer(log->node);
  1710. return ret;
  1711. }
  1712. int wait_log_commit(struct btrfs_root *log)
  1713. {
  1714. DEFINE_WAIT(wait);
  1715. u64 transid = log->fs_info->tree_log_transid;
  1716. do {
  1717. prepare_to_wait(&log->fs_info->tree_log_wait, &wait,
  1718. TASK_UNINTERRUPTIBLE);
  1719. mutex_unlock(&log->fs_info->tree_log_mutex);
  1720. if (atomic_read(&log->fs_info->tree_log_commit))
  1721. schedule();
  1722. finish_wait(&log->fs_info->tree_log_wait, &wait);
  1723. mutex_lock(&log->fs_info->tree_log_mutex);
  1724. } while(transid == log->fs_info->tree_log_transid &&
  1725. atomic_read(&log->fs_info->tree_log_commit));
  1726. return 0;
  1727. }
  1728. /*
  1729. * btrfs_sync_log does sends a given tree log down to the disk and
  1730. * updates the super blocks to record it. When this call is done,
  1731. * you know that any inodes previously logged are safely on disk
  1732. */
  1733. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  1734. struct btrfs_root *root)
  1735. {
  1736. int ret;
  1737. unsigned long batch;
  1738. struct btrfs_root *log = root->log_root;
  1739. mutex_lock(&log->fs_info->tree_log_mutex);
  1740. if (atomic_read(&log->fs_info->tree_log_commit)) {
  1741. wait_log_commit(log);
  1742. goto out;
  1743. }
  1744. atomic_set(&log->fs_info->tree_log_commit, 1);
  1745. while(1) {
  1746. batch = log->fs_info->tree_log_batch;
  1747. mutex_unlock(&log->fs_info->tree_log_mutex);
  1748. schedule_timeout_uninterruptible(1);
  1749. mutex_lock(&log->fs_info->tree_log_mutex);
  1750. while(atomic_read(&log->fs_info->tree_log_writers)) {
  1751. DEFINE_WAIT(wait);
  1752. prepare_to_wait(&log->fs_info->tree_log_wait, &wait,
  1753. TASK_UNINTERRUPTIBLE);
  1754. mutex_unlock(&log->fs_info->tree_log_mutex);
  1755. if (atomic_read(&log->fs_info->tree_log_writers))
  1756. schedule();
  1757. mutex_lock(&log->fs_info->tree_log_mutex);
  1758. finish_wait(&log->fs_info->tree_log_wait, &wait);
  1759. }
  1760. if (batch == log->fs_info->tree_log_batch)
  1761. break;
  1762. }
  1763. ret = btrfs_write_and_wait_marked_extents(log, &log->dirty_log_pages);
  1764. BUG_ON(ret);
  1765. ret = btrfs_write_and_wait_marked_extents(root->fs_info->log_root_tree,
  1766. &root->fs_info->log_root_tree->dirty_log_pages);
  1767. BUG_ON(ret);
  1768. btrfs_set_super_log_root(&root->fs_info->super_for_commit,
  1769. log->fs_info->log_root_tree->node->start);
  1770. btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
  1771. btrfs_header_level(log->fs_info->log_root_tree->node));
  1772. write_ctree_super(trans, log->fs_info->tree_root);
  1773. log->fs_info->tree_log_transid++;
  1774. log->fs_info->tree_log_batch = 0;
  1775. atomic_set(&log->fs_info->tree_log_commit, 0);
  1776. smp_mb();
  1777. if (waitqueue_active(&log->fs_info->tree_log_wait))
  1778. wake_up(&log->fs_info->tree_log_wait);
  1779. out:
  1780. mutex_unlock(&log->fs_info->tree_log_mutex);
  1781. return 0;
  1782. }
  1783. /* * free all the extents used by the tree log. This should be called
  1784. * at commit time of the full transaction
  1785. */
  1786. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  1787. {
  1788. int ret;
  1789. struct btrfs_root *log;
  1790. struct key;
  1791. u64 start;
  1792. u64 end;
  1793. struct walk_control wc = {
  1794. .free = 1,
  1795. .process_func = process_one_buffer
  1796. };
  1797. if (!root->log_root)
  1798. return 0;
  1799. log = root->log_root;
  1800. ret = walk_log_tree(trans, log, &wc);
  1801. BUG_ON(ret);
  1802. while(1) {
  1803. ret = find_first_extent_bit(&log->dirty_log_pages,
  1804. 0, &start, &end, EXTENT_DIRTY);
  1805. if (ret)
  1806. break;
  1807. clear_extent_dirty(&log->dirty_log_pages,
  1808. start, end, GFP_NOFS);
  1809. }
  1810. log = root->log_root;
  1811. ret = btrfs_del_root(trans, root->fs_info->log_root_tree,
  1812. &log->root_key);
  1813. BUG_ON(ret);
  1814. root->log_root = NULL;
  1815. kfree(root->log_root);
  1816. return 0;
  1817. }
  1818. /*
  1819. * helper function to update the item for a given subvolumes log root
  1820. * in the tree of log roots
  1821. */
  1822. static int update_log_root(struct btrfs_trans_handle *trans,
  1823. struct btrfs_root *log)
  1824. {
  1825. u64 bytenr = btrfs_root_bytenr(&log->root_item);
  1826. int ret;
  1827. if (log->node->start == bytenr)
  1828. return 0;
  1829. btrfs_set_root_bytenr(&log->root_item, log->node->start);
  1830. btrfs_set_root_level(&log->root_item, btrfs_header_level(log->node));
  1831. ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
  1832. &log->root_key, &log->root_item);
  1833. BUG_ON(ret);
  1834. return ret;
  1835. }
  1836. /*
  1837. * If both a file and directory are logged, and unlinks or renames are
  1838. * mixed in, we have a few interesting corners:
  1839. *
  1840. * create file X in dir Y
  1841. * link file X to X.link in dir Y
  1842. * fsync file X
  1843. * unlink file X but leave X.link
  1844. * fsync dir Y
  1845. *
  1846. * After a crash we would expect only X.link to exist. But file X
  1847. * didn't get fsync'd again so the log has back refs for X and X.link.
  1848. *
  1849. * We solve this by removing directory entries and inode backrefs from the
  1850. * log when a file that was logged in the current transaction is
  1851. * unlinked. Any later fsync will include the updated log entries, and
  1852. * we'll be able to reconstruct the proper directory items from backrefs.
  1853. *
  1854. * This optimizations allows us to avoid relogging the entire inode
  1855. * or the entire directory.
  1856. */
  1857. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  1858. struct btrfs_root *root,
  1859. const char *name, int name_len,
  1860. struct inode *dir, u64 index)
  1861. {
  1862. struct btrfs_root *log;
  1863. struct btrfs_dir_item *di;
  1864. struct btrfs_path *path;
  1865. int ret;
  1866. int bytes_del = 0;
  1867. if (BTRFS_I(dir)->logged_trans < trans->transid)
  1868. return 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. if (BTRFS_I(inode)->logged_trans < trans->transid)
  1931. return 0;
  1932. ret = join_running_log_trans(root);
  1933. if (ret)
  1934. return 0;
  1935. log = root->log_root;
  1936. mutex_lock(&BTRFS_I(inode)->log_mutex);
  1937. ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino,
  1938. dirid, &index);
  1939. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  1940. end_log_trans(root);
  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. static noinline int copy_items(struct btrfs_trans_handle *trans,
  2178. struct btrfs_root *log,
  2179. struct btrfs_path *dst_path,
  2180. struct extent_buffer *src,
  2181. int start_slot, int nr, int inode_only)
  2182. {
  2183. unsigned long src_offset;
  2184. unsigned long dst_offset;
  2185. struct btrfs_file_extent_item *extent;
  2186. struct btrfs_inode_item *inode_item;
  2187. int ret;
  2188. struct btrfs_key *ins_keys;
  2189. u32 *ins_sizes;
  2190. char *ins_data;
  2191. int i;
  2192. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  2193. nr * sizeof(u32), GFP_NOFS);
  2194. ins_sizes = (u32 *)ins_data;
  2195. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  2196. for (i = 0; i < nr; i++) {
  2197. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  2198. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  2199. }
  2200. ret = btrfs_insert_empty_items(trans, log, dst_path,
  2201. ins_keys, ins_sizes, nr);
  2202. BUG_ON(ret);
  2203. for (i = 0; i < nr; i++) {
  2204. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  2205. dst_path->slots[0]);
  2206. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  2207. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  2208. src_offset, ins_sizes[i]);
  2209. if (inode_only == LOG_INODE_EXISTS &&
  2210. ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  2211. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  2212. dst_path->slots[0],
  2213. struct btrfs_inode_item);
  2214. btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
  2215. /* set the generation to zero so the recover code
  2216. * can tell the difference between an logging
  2217. * just to say 'this inode exists' and a logging
  2218. * to say 'update this inode with these values'
  2219. */
  2220. btrfs_set_inode_generation(dst_path->nodes[0],
  2221. inode_item, 0);
  2222. }
  2223. /* take a reference on file data extents so that truncates
  2224. * or deletes of this inode don't have to relog the inode
  2225. * again
  2226. */
  2227. if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
  2228. int found_type;
  2229. extent = btrfs_item_ptr(src, start_slot + i,
  2230. struct btrfs_file_extent_item);
  2231. found_type = btrfs_file_extent_type(src, extent);
  2232. if (found_type == BTRFS_FILE_EXTENT_REG) {
  2233. u64 ds = btrfs_file_extent_disk_bytenr(src,
  2234. extent);
  2235. u64 dl = btrfs_file_extent_disk_num_bytes(src,
  2236. extent);
  2237. /* ds == 0 is a hole */
  2238. if (ds != 0) {
  2239. ret = btrfs_inc_extent_ref(trans, log,
  2240. ds, dl,
  2241. dst_path->nodes[0]->start,
  2242. BTRFS_TREE_LOG_OBJECTID,
  2243. trans->transid,
  2244. ins_keys[i].objectid);
  2245. BUG_ON(ret);
  2246. }
  2247. }
  2248. }
  2249. dst_path->slots[0]++;
  2250. }
  2251. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  2252. btrfs_release_path(log, dst_path);
  2253. kfree(ins_data);
  2254. return 0;
  2255. }
  2256. /* log a single inode in the tree log.
  2257. * At least one parent directory for this inode must exist in the tree
  2258. * or be logged already.
  2259. *
  2260. * Any items from this inode changed by the current transaction are copied
  2261. * to the log tree. An extra reference is taken on any extents in this
  2262. * file, allowing us to avoid a whole pile of corner cases around logging
  2263. * blocks that have been removed from the tree.
  2264. *
  2265. * See LOG_INODE_ALL and related defines for a description of what inode_only
  2266. * does.
  2267. *
  2268. * This handles both files and directories.
  2269. */
  2270. static int __btrfs_log_inode(struct btrfs_trans_handle *trans,
  2271. struct btrfs_root *root, struct inode *inode,
  2272. int inode_only)
  2273. {
  2274. struct btrfs_path *path;
  2275. struct btrfs_path *dst_path;
  2276. struct btrfs_key min_key;
  2277. struct btrfs_key max_key;
  2278. struct btrfs_root *log = root->log_root;
  2279. struct extent_buffer *src = NULL;
  2280. u32 size;
  2281. int ret;
  2282. int nritems;
  2283. int ins_start_slot = 0;
  2284. int ins_nr;
  2285. log = root->log_root;
  2286. path = btrfs_alloc_path();
  2287. dst_path = btrfs_alloc_path();
  2288. min_key.objectid = inode->i_ino;
  2289. min_key.type = BTRFS_INODE_ITEM_KEY;
  2290. min_key.offset = 0;
  2291. max_key.objectid = inode->i_ino;
  2292. if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
  2293. max_key.type = BTRFS_XATTR_ITEM_KEY;
  2294. else
  2295. max_key.type = (u8)-1;
  2296. max_key.offset = (u64)-1;
  2297. /*
  2298. * if this inode has already been logged and we're in inode_only
  2299. * mode, we don't want to delete the things that have already
  2300. * been written to the log.
  2301. *
  2302. * But, if the inode has been through an inode_only log,
  2303. * the logged_trans field is not set. This allows us to catch
  2304. * any new names for this inode in the backrefs by logging it
  2305. * again
  2306. */
  2307. if (inode_only == LOG_INODE_EXISTS &&
  2308. BTRFS_I(inode)->logged_trans == trans->transid) {
  2309. btrfs_free_path(path);
  2310. btrfs_free_path(dst_path);
  2311. goto out;
  2312. }
  2313. mutex_lock(&BTRFS_I(inode)->log_mutex);
  2314. /*
  2315. * a brute force approach to making sure we get the most uptodate
  2316. * copies of everything.
  2317. */
  2318. if (S_ISDIR(inode->i_mode)) {
  2319. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  2320. if (inode_only == LOG_INODE_EXISTS)
  2321. max_key_type = BTRFS_XATTR_ITEM_KEY;
  2322. ret = drop_objectid_items(trans, log, path,
  2323. inode->i_ino, max_key_type);
  2324. } else {
  2325. ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
  2326. }
  2327. BUG_ON(ret);
  2328. path->keep_locks = 1;
  2329. while(1) {
  2330. ins_nr = 0;
  2331. ret = btrfs_search_forward(root, &min_key, &max_key,
  2332. path, 0, trans->transid);
  2333. if (ret != 0)
  2334. break;
  2335. again:
  2336. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  2337. if (min_key.objectid != inode->i_ino)
  2338. break;
  2339. if (min_key.type > max_key.type)
  2340. break;
  2341. src = path->nodes[0];
  2342. size = btrfs_item_size_nr(src, path->slots[0]);
  2343. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  2344. ins_nr++;
  2345. goto next_slot;
  2346. } else if (!ins_nr) {
  2347. ins_start_slot = path->slots[0];
  2348. ins_nr = 1;
  2349. goto next_slot;
  2350. }
  2351. ret = copy_items(trans, log, dst_path, src, ins_start_slot,
  2352. ins_nr, inode_only);
  2353. BUG_ON(ret);
  2354. ins_nr = 1;
  2355. ins_start_slot = path->slots[0];
  2356. next_slot:
  2357. nritems = btrfs_header_nritems(path->nodes[0]);
  2358. path->slots[0]++;
  2359. if (path->slots[0] < nritems) {
  2360. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  2361. path->slots[0]);
  2362. goto again;
  2363. }
  2364. if (ins_nr) {
  2365. ret = copy_items(trans, log, dst_path, src,
  2366. ins_start_slot,
  2367. ins_nr, inode_only);
  2368. BUG_ON(ret);
  2369. ins_nr = 0;
  2370. }
  2371. btrfs_release_path(root, path);
  2372. if (min_key.offset < (u64)-1)
  2373. min_key.offset++;
  2374. else if (min_key.type < (u8)-1)
  2375. min_key.type++;
  2376. else if (min_key.objectid < (u64)-1)
  2377. min_key.objectid++;
  2378. else
  2379. break;
  2380. }
  2381. if (ins_nr) {
  2382. ret = copy_items(trans, log, dst_path, src,
  2383. ins_start_slot,
  2384. ins_nr, inode_only);
  2385. BUG_ON(ret);
  2386. ins_nr = 0;
  2387. }
  2388. WARN_ON(ins_nr);
  2389. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
  2390. btrfs_release_path(root, path);
  2391. btrfs_release_path(log, dst_path);
  2392. BTRFS_I(inode)->log_dirty_trans = 0;
  2393. ret = log_directory_changes(trans, root, inode, path, dst_path);
  2394. BUG_ON(ret);
  2395. }
  2396. BTRFS_I(inode)->logged_trans = trans->transid;
  2397. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  2398. btrfs_free_path(path);
  2399. btrfs_free_path(dst_path);
  2400. mutex_lock(&root->fs_info->tree_log_mutex);
  2401. ret = update_log_root(trans, log);
  2402. BUG_ON(ret);
  2403. mutex_unlock(&root->fs_info->tree_log_mutex);
  2404. out:
  2405. return 0;
  2406. }
  2407. int btrfs_log_inode(struct btrfs_trans_handle *trans,
  2408. struct btrfs_root *root, struct inode *inode,
  2409. int inode_only)
  2410. {
  2411. int ret;
  2412. start_log_trans(trans, root);
  2413. ret = __btrfs_log_inode(trans, root, inode, inode_only);
  2414. end_log_trans(root);
  2415. return ret;
  2416. }
  2417. /*
  2418. * helper function around btrfs_log_inode to make sure newly created
  2419. * parent directories also end up in the log. A minimal inode and backref
  2420. * only logging is done of any parent directories that are older than
  2421. * the last committed transaction
  2422. */
  2423. int btrfs_log_dentry(struct btrfs_trans_handle *trans,
  2424. struct btrfs_root *root, struct dentry *dentry)
  2425. {
  2426. int inode_only = LOG_INODE_ALL;
  2427. struct super_block *sb;
  2428. int ret;
  2429. start_log_trans(trans, root);
  2430. sb = dentry->d_inode->i_sb;
  2431. while(1) {
  2432. ret = __btrfs_log_inode(trans, root, dentry->d_inode,
  2433. inode_only);
  2434. BUG_ON(ret);
  2435. inode_only = LOG_INODE_EXISTS;
  2436. dentry = dentry->d_parent;
  2437. if (!dentry || !dentry->d_inode || sb != dentry->d_inode->i_sb)
  2438. break;
  2439. if (BTRFS_I(dentry->d_inode)->generation <=
  2440. root->fs_info->last_trans_committed)
  2441. break;
  2442. }
  2443. end_log_trans(root);
  2444. return 0;
  2445. }
  2446. /*
  2447. * it is not safe to log dentry if the chunk root has added new
  2448. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  2449. * If this returns 1, you must commit the transaction to safely get your
  2450. * data on disk.
  2451. */
  2452. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  2453. struct btrfs_root *root, struct dentry *dentry)
  2454. {
  2455. u64 gen;
  2456. gen = root->fs_info->last_trans_new_blockgroup;
  2457. if (gen > root->fs_info->last_trans_committed)
  2458. return 1;
  2459. else
  2460. return btrfs_log_dentry(trans, root, dentry);
  2461. }
  2462. /*
  2463. * should be called during mount to recover any replay any log trees
  2464. * from the FS
  2465. */
  2466. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  2467. {
  2468. int ret;
  2469. struct btrfs_path *path;
  2470. struct btrfs_trans_handle *trans;
  2471. struct btrfs_key key;
  2472. struct btrfs_key found_key;
  2473. struct btrfs_key tmp_key;
  2474. struct btrfs_root *log;
  2475. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  2476. u64 highest_inode;
  2477. struct walk_control wc = {
  2478. .process_func = process_one_buffer,
  2479. .stage = 0,
  2480. };
  2481. fs_info->log_root_recovering = 1;
  2482. path = btrfs_alloc_path();
  2483. BUG_ON(!path);
  2484. trans = btrfs_start_transaction(fs_info->tree_root, 1);
  2485. wc.trans = trans;
  2486. wc.pin = 1;
  2487. walk_log_tree(trans, log_root_tree, &wc);
  2488. again:
  2489. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  2490. key.offset = (u64)-1;
  2491. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  2492. while(1) {
  2493. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  2494. if (ret < 0)
  2495. break;
  2496. if (ret > 0) {
  2497. if (path->slots[0] == 0)
  2498. break;
  2499. path->slots[0]--;
  2500. }
  2501. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2502. path->slots[0]);
  2503. btrfs_release_path(log_root_tree, path);
  2504. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  2505. break;
  2506. log = btrfs_read_fs_root_no_radix(log_root_tree,
  2507. &found_key);
  2508. BUG_ON(!log);
  2509. tmp_key.objectid = found_key.offset;
  2510. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  2511. tmp_key.offset = (u64)-1;
  2512. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  2513. BUG_ON(!wc.replay_dest);
  2514. btrfs_record_root_in_trans(wc.replay_dest);
  2515. ret = walk_log_tree(trans, log, &wc);
  2516. BUG_ON(ret);
  2517. if (wc.stage == LOG_WALK_REPLAY_ALL) {
  2518. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  2519. path);
  2520. BUG_ON(ret);
  2521. }
  2522. ret = btrfs_find_highest_inode(wc.replay_dest, &highest_inode);
  2523. if (ret == 0) {
  2524. wc.replay_dest->highest_inode = highest_inode;
  2525. wc.replay_dest->last_inode_alloc = highest_inode;
  2526. }
  2527. key.offset = found_key.offset - 1;
  2528. free_extent_buffer(log->node);
  2529. kfree(log);
  2530. if (found_key.offset == 0)
  2531. break;
  2532. }
  2533. btrfs_release_path(log_root_tree, path);
  2534. /* step one is to pin it all, step two is to replay just inodes */
  2535. if (wc.pin) {
  2536. wc.pin = 0;
  2537. wc.process_func = replay_one_buffer;
  2538. wc.stage = LOG_WALK_REPLAY_INODES;
  2539. goto again;
  2540. }
  2541. /* step three is to replay everything */
  2542. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  2543. wc.stage++;
  2544. goto again;
  2545. }
  2546. btrfs_free_path(path);
  2547. free_extent_buffer(log_root_tree->node);
  2548. log_root_tree->log_root = NULL;
  2549. fs_info->log_root_recovering = 0;
  2550. /* step 4: commit the transaction, which also unpins the blocks */
  2551. btrfs_commit_transaction(trans, fs_info->tree_root);
  2552. kfree(log_root_tree);
  2553. return 0;
  2554. }