tree-log.c 76 KB

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