tree-log.c 76 KB

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