tree-log.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998
  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. unsigned long ptr;
  833. file_bytes = (item_size / csum_size) * root->sectorsize;
  834. sums = kzalloc(btrfs_ordered_sum_size(root, file_bytes), GFP_NOFS);
  835. if (!sums) {
  836. return -ENOMEM;
  837. }
  838. INIT_LIST_HEAD(&sums->list);
  839. sums->len = file_bytes;
  840. sums->bytenr = key->offset;
  841. /*
  842. * copy all the sums into the ordered sum struct
  843. */
  844. sector_sum = sums->sums;
  845. cur_offset = key->offset;
  846. ptr = btrfs_item_ptr_offset(eb, slot);
  847. while(item_size > 0) {
  848. sector_sum->bytenr = cur_offset;
  849. read_extent_buffer(eb, &sector_sum->sum, ptr, csum_size);
  850. sector_sum++;
  851. item_size -= csum_size;
  852. ptr += csum_size;
  853. cur_offset += root->sectorsize;
  854. }
  855. /* let btrfs_csum_file_blocks add them into the file */
  856. ret = btrfs_csum_file_blocks(trans, root->fs_info->csum_root, sums);
  857. BUG_ON(ret);
  858. kfree(sums);
  859. return 0;
  860. }
  861. /*
  862. * There are a few corners where the link count of the file can't
  863. * be properly maintained during replay. So, instead of adding
  864. * lots of complexity to the log code, we just scan the backrefs
  865. * for any file that has been through replay.
  866. *
  867. * The scan will update the link count on the inode to reflect the
  868. * number of back refs found. If it goes down to zero, the iput
  869. * will free the inode.
  870. */
  871. static noinline int fixup_inode_link_count(struct btrfs_trans_handle *trans,
  872. struct btrfs_root *root,
  873. struct inode *inode)
  874. {
  875. struct btrfs_path *path;
  876. int ret;
  877. struct btrfs_key key;
  878. u64 nlink = 0;
  879. unsigned long ptr;
  880. unsigned long ptr_end;
  881. int name_len;
  882. key.objectid = inode->i_ino;
  883. key.type = BTRFS_INODE_REF_KEY;
  884. key.offset = (u64)-1;
  885. path = btrfs_alloc_path();
  886. while(1) {
  887. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  888. if (ret < 0)
  889. break;
  890. if (ret > 0) {
  891. if (path->slots[0] == 0)
  892. break;
  893. path->slots[0]--;
  894. }
  895. btrfs_item_key_to_cpu(path->nodes[0], &key,
  896. path->slots[0]);
  897. if (key.objectid != inode->i_ino ||
  898. key.type != BTRFS_INODE_REF_KEY)
  899. break;
  900. ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
  901. ptr_end = ptr + btrfs_item_size_nr(path->nodes[0],
  902. path->slots[0]);
  903. while(ptr < ptr_end) {
  904. struct btrfs_inode_ref *ref;
  905. ref = (struct btrfs_inode_ref *)ptr;
  906. name_len = btrfs_inode_ref_name_len(path->nodes[0],
  907. ref);
  908. ptr = (unsigned long)(ref + 1) + name_len;
  909. nlink++;
  910. }
  911. if (key.offset == 0)
  912. break;
  913. key.offset--;
  914. btrfs_release_path(root, path);
  915. }
  916. btrfs_free_path(path);
  917. if (nlink != inode->i_nlink) {
  918. inode->i_nlink = nlink;
  919. btrfs_update_inode(trans, root, inode);
  920. }
  921. BTRFS_I(inode)->index_cnt = (u64)-1;
  922. return 0;
  923. }
  924. static noinline int fixup_inode_link_counts(struct btrfs_trans_handle *trans,
  925. struct btrfs_root *root,
  926. struct btrfs_path *path)
  927. {
  928. int ret;
  929. struct btrfs_key key;
  930. struct inode *inode;
  931. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  932. key.type = BTRFS_ORPHAN_ITEM_KEY;
  933. key.offset = (u64)-1;
  934. while(1) {
  935. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  936. if (ret < 0)
  937. break;
  938. if (ret == 1) {
  939. if (path->slots[0] == 0)
  940. break;
  941. path->slots[0]--;
  942. }
  943. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  944. if (key.objectid != BTRFS_TREE_LOG_FIXUP_OBJECTID ||
  945. key.type != BTRFS_ORPHAN_ITEM_KEY)
  946. break;
  947. ret = btrfs_del_item(trans, root, path);
  948. BUG_ON(ret);
  949. btrfs_release_path(root, path);
  950. inode = read_one_inode(root, key.offset);
  951. BUG_ON(!inode);
  952. ret = fixup_inode_link_count(trans, root, inode);
  953. BUG_ON(ret);
  954. iput(inode);
  955. if (key.offset == 0)
  956. break;
  957. key.offset--;
  958. }
  959. btrfs_release_path(root, path);
  960. return 0;
  961. }
  962. /*
  963. * record a given inode in the fixup dir so we can check its link
  964. * count when replay is done. The link count is incremented here
  965. * so the inode won't go away until we check it
  966. */
  967. static noinline int link_to_fixup_dir(struct btrfs_trans_handle *trans,
  968. struct btrfs_root *root,
  969. struct btrfs_path *path,
  970. u64 objectid)
  971. {
  972. struct btrfs_key key;
  973. int ret = 0;
  974. struct inode *inode;
  975. inode = read_one_inode(root, objectid);
  976. BUG_ON(!inode);
  977. key.objectid = BTRFS_TREE_LOG_FIXUP_OBJECTID;
  978. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  979. key.offset = objectid;
  980. ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
  981. btrfs_release_path(root, path);
  982. if (ret == 0) {
  983. btrfs_inc_nlink(inode);
  984. btrfs_update_inode(trans, root, inode);
  985. } else if (ret == -EEXIST) {
  986. ret = 0;
  987. } else {
  988. BUG();
  989. }
  990. iput(inode);
  991. return ret;
  992. }
  993. /*
  994. * when replaying the log for a directory, we only insert names
  995. * for inodes that actually exist. This means an fsync on a directory
  996. * does not implicitly fsync all the new files in it
  997. */
  998. static noinline int insert_one_name(struct btrfs_trans_handle *trans,
  999. struct btrfs_root *root,
  1000. struct btrfs_path *path,
  1001. u64 dirid, u64 index,
  1002. char *name, int name_len, u8 type,
  1003. struct btrfs_key *location)
  1004. {
  1005. struct inode *inode;
  1006. struct inode *dir;
  1007. int ret;
  1008. inode = read_one_inode(root, location->objectid);
  1009. if (!inode)
  1010. return -ENOENT;
  1011. dir = read_one_inode(root, dirid);
  1012. if (!dir) {
  1013. iput(inode);
  1014. return -EIO;
  1015. }
  1016. ret = btrfs_add_link(trans, dir, inode, name, name_len, 1, index);
  1017. /* FIXME, put inode into FIXUP list */
  1018. iput(inode);
  1019. iput(dir);
  1020. return ret;
  1021. }
  1022. /*
  1023. * take a single entry in a log directory item and replay it into
  1024. * the subvolume.
  1025. *
  1026. * if a conflicting item exists in the subdirectory already,
  1027. * the inode it points to is unlinked and put into the link count
  1028. * fix up tree.
  1029. *
  1030. * If a name from the log points to a file or directory that does
  1031. * not exist in the FS, it is skipped. fsyncs on directories
  1032. * do not force down inodes inside that directory, just changes to the
  1033. * names or unlinks in a directory.
  1034. */
  1035. static noinline int replay_one_name(struct btrfs_trans_handle *trans,
  1036. struct btrfs_root *root,
  1037. struct btrfs_path *path,
  1038. struct extent_buffer *eb,
  1039. struct btrfs_dir_item *di,
  1040. struct btrfs_key *key)
  1041. {
  1042. char *name;
  1043. int name_len;
  1044. struct btrfs_dir_item *dst_di;
  1045. struct btrfs_key found_key;
  1046. struct btrfs_key log_key;
  1047. struct inode *dir;
  1048. u8 log_type;
  1049. int exists;
  1050. int ret;
  1051. dir = read_one_inode(root, key->objectid);
  1052. BUG_ON(!dir);
  1053. name_len = btrfs_dir_name_len(eb, di);
  1054. name = kmalloc(name_len, GFP_NOFS);
  1055. log_type = btrfs_dir_type(eb, di);
  1056. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1057. name_len);
  1058. btrfs_dir_item_key_to_cpu(eb, di, &log_key);
  1059. exists = btrfs_lookup_inode(trans, root, path, &log_key, 0);
  1060. if (exists == 0)
  1061. exists = 1;
  1062. else
  1063. exists = 0;
  1064. btrfs_release_path(root, path);
  1065. if (key->type == BTRFS_DIR_ITEM_KEY) {
  1066. dst_di = btrfs_lookup_dir_item(trans, root, path, key->objectid,
  1067. name, name_len, 1);
  1068. }
  1069. else if (key->type == BTRFS_DIR_INDEX_KEY) {
  1070. dst_di = btrfs_lookup_dir_index_item(trans, root, path,
  1071. key->objectid,
  1072. key->offset, name,
  1073. name_len, 1);
  1074. } else {
  1075. BUG();
  1076. }
  1077. if (!dst_di || IS_ERR(dst_di)) {
  1078. /* we need a sequence number to insert, so we only
  1079. * do inserts for the BTRFS_DIR_INDEX_KEY types
  1080. */
  1081. if (key->type != BTRFS_DIR_INDEX_KEY)
  1082. goto out;
  1083. goto insert;
  1084. }
  1085. btrfs_dir_item_key_to_cpu(path->nodes[0], dst_di, &found_key);
  1086. /* the existing item matches the logged item */
  1087. if (found_key.objectid == log_key.objectid &&
  1088. found_key.type == log_key.type &&
  1089. found_key.offset == log_key.offset &&
  1090. btrfs_dir_type(path->nodes[0], dst_di) == log_type) {
  1091. goto out;
  1092. }
  1093. /*
  1094. * don't drop the conflicting directory entry if the inode
  1095. * for the new entry doesn't exist
  1096. */
  1097. if (!exists)
  1098. goto out;
  1099. ret = drop_one_dir_item(trans, root, path, dir, dst_di);
  1100. BUG_ON(ret);
  1101. if (key->type == BTRFS_DIR_INDEX_KEY)
  1102. goto insert;
  1103. out:
  1104. btrfs_release_path(root, path);
  1105. kfree(name);
  1106. iput(dir);
  1107. return 0;
  1108. insert:
  1109. btrfs_release_path(root, path);
  1110. ret = insert_one_name(trans, root, path, key->objectid, key->offset,
  1111. name, name_len, log_type, &log_key);
  1112. if (ret && ret != -ENOENT)
  1113. BUG();
  1114. goto out;
  1115. }
  1116. /*
  1117. * find all the names in a directory item and reconcile them into
  1118. * the subvolume. Only BTRFS_DIR_ITEM_KEY types will have more than
  1119. * one name in a directory item, but the same code gets used for
  1120. * both directory index types
  1121. */
  1122. static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
  1123. struct btrfs_root *root,
  1124. struct btrfs_path *path,
  1125. struct extent_buffer *eb, int slot,
  1126. struct btrfs_key *key)
  1127. {
  1128. int ret;
  1129. u32 item_size = btrfs_item_size_nr(eb, slot);
  1130. struct btrfs_dir_item *di;
  1131. int name_len;
  1132. unsigned long ptr;
  1133. unsigned long ptr_end;
  1134. ptr = btrfs_item_ptr_offset(eb, slot);
  1135. ptr_end = ptr + item_size;
  1136. while(ptr < ptr_end) {
  1137. di = (struct btrfs_dir_item *)ptr;
  1138. name_len = btrfs_dir_name_len(eb, di);
  1139. ret = replay_one_name(trans, root, path, eb, di, key);
  1140. BUG_ON(ret);
  1141. ptr = (unsigned long)(di + 1);
  1142. ptr += name_len;
  1143. }
  1144. return 0;
  1145. }
  1146. /*
  1147. * directory replay has two parts. There are the standard directory
  1148. * items in the log copied from the subvolume, and range items
  1149. * created in the log while the subvolume was logged.
  1150. *
  1151. * The range items tell us which parts of the key space the log
  1152. * is authoritative for. During replay, if a key in the subvolume
  1153. * directory is in a logged range item, but not actually in the log
  1154. * that means it was deleted from the directory before the fsync
  1155. * and should be removed.
  1156. */
  1157. static noinline int find_dir_range(struct btrfs_root *root,
  1158. struct btrfs_path *path,
  1159. u64 dirid, int key_type,
  1160. u64 *start_ret, u64 *end_ret)
  1161. {
  1162. struct btrfs_key key;
  1163. u64 found_end;
  1164. struct btrfs_dir_log_item *item;
  1165. int ret;
  1166. int nritems;
  1167. if (*start_ret == (u64)-1)
  1168. return 1;
  1169. key.objectid = dirid;
  1170. key.type = key_type;
  1171. key.offset = *start_ret;
  1172. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1173. if (ret < 0)
  1174. goto out;
  1175. if (ret > 0) {
  1176. if (path->slots[0] == 0)
  1177. goto out;
  1178. path->slots[0]--;
  1179. }
  1180. if (ret != 0)
  1181. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1182. if (key.type != key_type || key.objectid != dirid) {
  1183. ret = 1;
  1184. goto next;
  1185. }
  1186. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1187. struct btrfs_dir_log_item);
  1188. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1189. if (*start_ret >= key.offset && *start_ret <= found_end) {
  1190. ret = 0;
  1191. *start_ret = key.offset;
  1192. *end_ret = found_end;
  1193. goto out;
  1194. }
  1195. ret = 1;
  1196. next:
  1197. /* check the next slot in the tree to see if it is a valid item */
  1198. nritems = btrfs_header_nritems(path->nodes[0]);
  1199. if (path->slots[0] >= nritems) {
  1200. ret = btrfs_next_leaf(root, path);
  1201. if (ret)
  1202. goto out;
  1203. } else {
  1204. path->slots[0]++;
  1205. }
  1206. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1207. if (key.type != key_type || key.objectid != dirid) {
  1208. ret = 1;
  1209. goto out;
  1210. }
  1211. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1212. struct btrfs_dir_log_item);
  1213. found_end = btrfs_dir_log_end(path->nodes[0], item);
  1214. *start_ret = key.offset;
  1215. *end_ret = found_end;
  1216. ret = 0;
  1217. out:
  1218. btrfs_release_path(root, path);
  1219. return ret;
  1220. }
  1221. /*
  1222. * this looks for a given directory item in the log. If the directory
  1223. * item is not in the log, the item is removed and the inode it points
  1224. * to is unlinked
  1225. */
  1226. static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
  1227. struct btrfs_root *root,
  1228. struct btrfs_root *log,
  1229. struct btrfs_path *path,
  1230. struct btrfs_path *log_path,
  1231. struct inode *dir,
  1232. struct btrfs_key *dir_key)
  1233. {
  1234. int ret;
  1235. struct extent_buffer *eb;
  1236. int slot;
  1237. u32 item_size;
  1238. struct btrfs_dir_item *di;
  1239. struct btrfs_dir_item *log_di;
  1240. int name_len;
  1241. unsigned long ptr;
  1242. unsigned long ptr_end;
  1243. char *name;
  1244. struct inode *inode;
  1245. struct btrfs_key location;
  1246. again:
  1247. eb = path->nodes[0];
  1248. slot = path->slots[0];
  1249. item_size = btrfs_item_size_nr(eb, slot);
  1250. ptr = btrfs_item_ptr_offset(eb, slot);
  1251. ptr_end = ptr + item_size;
  1252. while(ptr < ptr_end) {
  1253. di = (struct btrfs_dir_item *)ptr;
  1254. name_len = btrfs_dir_name_len(eb, di);
  1255. name = kmalloc(name_len, GFP_NOFS);
  1256. if (!name) {
  1257. ret = -ENOMEM;
  1258. goto out;
  1259. }
  1260. read_extent_buffer(eb, name, (unsigned long)(di + 1),
  1261. name_len);
  1262. log_di = NULL;
  1263. if (dir_key->type == BTRFS_DIR_ITEM_KEY) {
  1264. log_di = btrfs_lookup_dir_item(trans, log, log_path,
  1265. dir_key->objectid,
  1266. name, name_len, 0);
  1267. } else if (dir_key->type == BTRFS_DIR_INDEX_KEY) {
  1268. log_di = btrfs_lookup_dir_index_item(trans, log,
  1269. log_path,
  1270. dir_key->objectid,
  1271. dir_key->offset,
  1272. name, name_len, 0);
  1273. }
  1274. if (!log_di || IS_ERR(log_di)) {
  1275. btrfs_dir_item_key_to_cpu(eb, di, &location);
  1276. btrfs_release_path(root, path);
  1277. btrfs_release_path(log, log_path);
  1278. inode = read_one_inode(root, location.objectid);
  1279. BUG_ON(!inode);
  1280. ret = link_to_fixup_dir(trans, root,
  1281. path, location.objectid);
  1282. BUG_ON(ret);
  1283. btrfs_inc_nlink(inode);
  1284. ret = btrfs_unlink_inode(trans, root, dir, inode,
  1285. name, name_len);
  1286. BUG_ON(ret);
  1287. kfree(name);
  1288. iput(inode);
  1289. /* there might still be more names under this key
  1290. * check and repeat if required
  1291. */
  1292. ret = btrfs_search_slot(NULL, root, dir_key, path,
  1293. 0, 0);
  1294. if (ret == 0)
  1295. goto again;
  1296. ret = 0;
  1297. goto out;
  1298. }
  1299. btrfs_release_path(log, log_path);
  1300. kfree(name);
  1301. ptr = (unsigned long)(di + 1);
  1302. ptr += name_len;
  1303. }
  1304. ret = 0;
  1305. out:
  1306. btrfs_release_path(root, path);
  1307. btrfs_release_path(log, log_path);
  1308. return ret;
  1309. }
  1310. /*
  1311. * deletion replay happens before we copy any new directory items
  1312. * out of the log or out of backreferences from inodes. It
  1313. * scans the log to find ranges of keys that log is authoritative for,
  1314. * and then scans the directory to find items in those ranges that are
  1315. * not present in the log.
  1316. *
  1317. * Anything we don't find in the log is unlinked and removed from the
  1318. * directory.
  1319. */
  1320. static noinline int replay_dir_deletes(struct btrfs_trans_handle *trans,
  1321. struct btrfs_root *root,
  1322. struct btrfs_root *log,
  1323. struct btrfs_path *path,
  1324. u64 dirid)
  1325. {
  1326. u64 range_start;
  1327. u64 range_end;
  1328. int key_type = BTRFS_DIR_LOG_ITEM_KEY;
  1329. int ret = 0;
  1330. struct btrfs_key dir_key;
  1331. struct btrfs_key found_key;
  1332. struct btrfs_path *log_path;
  1333. struct inode *dir;
  1334. dir_key.objectid = dirid;
  1335. dir_key.type = BTRFS_DIR_ITEM_KEY;
  1336. log_path = btrfs_alloc_path();
  1337. if (!log_path)
  1338. return -ENOMEM;
  1339. dir = read_one_inode(root, dirid);
  1340. /* it isn't an error if the inode isn't there, that can happen
  1341. * because we replay the deletes before we copy in the inode item
  1342. * from the log
  1343. */
  1344. if (!dir) {
  1345. btrfs_free_path(log_path);
  1346. return 0;
  1347. }
  1348. again:
  1349. range_start = 0;
  1350. range_end = 0;
  1351. while(1) {
  1352. ret = find_dir_range(log, path, dirid, key_type,
  1353. &range_start, &range_end);
  1354. if (ret != 0)
  1355. break;
  1356. dir_key.offset = range_start;
  1357. while(1) {
  1358. int nritems;
  1359. ret = btrfs_search_slot(NULL, root, &dir_key, path,
  1360. 0, 0);
  1361. if (ret < 0)
  1362. goto out;
  1363. nritems = btrfs_header_nritems(path->nodes[0]);
  1364. if (path->slots[0] >= nritems) {
  1365. ret = btrfs_next_leaf(root, path);
  1366. if (ret)
  1367. break;
  1368. }
  1369. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1370. path->slots[0]);
  1371. if (found_key.objectid != dirid ||
  1372. found_key.type != dir_key.type)
  1373. goto next_type;
  1374. if (found_key.offset > range_end)
  1375. break;
  1376. ret = check_item_in_log(trans, root, log, path,
  1377. log_path, dir, &found_key);
  1378. BUG_ON(ret);
  1379. if (found_key.offset == (u64)-1)
  1380. break;
  1381. dir_key.offset = found_key.offset + 1;
  1382. }
  1383. btrfs_release_path(root, path);
  1384. if (range_end == (u64)-1)
  1385. break;
  1386. range_start = range_end + 1;
  1387. }
  1388. next_type:
  1389. ret = 0;
  1390. if (key_type == BTRFS_DIR_LOG_ITEM_KEY) {
  1391. key_type = BTRFS_DIR_LOG_INDEX_KEY;
  1392. dir_key.type = BTRFS_DIR_INDEX_KEY;
  1393. btrfs_release_path(root, path);
  1394. goto again;
  1395. }
  1396. out:
  1397. btrfs_release_path(root, path);
  1398. btrfs_free_path(log_path);
  1399. iput(dir);
  1400. return ret;
  1401. }
  1402. /*
  1403. * the process_func used to replay items from the log tree. This
  1404. * gets called in two different stages. The first stage just looks
  1405. * for inodes and makes sure they are all copied into the subvolume.
  1406. *
  1407. * The second stage copies all the other item types from the log into
  1408. * the subvolume. The two stage approach is slower, but gets rid of
  1409. * lots of complexity around inodes referencing other inodes that exist
  1410. * only in the log (references come from either directory items or inode
  1411. * back refs).
  1412. */
  1413. static int replay_one_buffer(struct btrfs_root *log, struct extent_buffer *eb,
  1414. struct walk_control *wc, u64 gen)
  1415. {
  1416. int nritems;
  1417. struct btrfs_path *path;
  1418. struct btrfs_root *root = wc->replay_dest;
  1419. struct btrfs_key key;
  1420. u32 item_size;
  1421. int level;
  1422. int i;
  1423. int ret;
  1424. btrfs_read_buffer(eb, gen);
  1425. level = btrfs_header_level(eb);
  1426. if (level != 0)
  1427. return 0;
  1428. path = btrfs_alloc_path();
  1429. BUG_ON(!path);
  1430. nritems = btrfs_header_nritems(eb);
  1431. for (i = 0; i < nritems; i++) {
  1432. btrfs_item_key_to_cpu(eb, &key, i);
  1433. item_size = btrfs_item_size_nr(eb, i);
  1434. /* inode keys are done during the first stage */
  1435. if (key.type == BTRFS_INODE_ITEM_KEY &&
  1436. wc->stage == LOG_WALK_REPLAY_INODES) {
  1437. struct inode *inode;
  1438. struct btrfs_inode_item *inode_item;
  1439. u32 mode;
  1440. inode_item = btrfs_item_ptr(eb, i,
  1441. struct btrfs_inode_item);
  1442. mode = btrfs_inode_mode(eb, inode_item);
  1443. if (S_ISDIR(mode)) {
  1444. ret = replay_dir_deletes(wc->trans,
  1445. root, log, path, key.objectid);
  1446. BUG_ON(ret);
  1447. }
  1448. ret = overwrite_item(wc->trans, root, path,
  1449. eb, i, &key);
  1450. BUG_ON(ret);
  1451. /* for regular files, truncate away
  1452. * extents past the new EOF
  1453. */
  1454. if (S_ISREG(mode)) {
  1455. inode = read_one_inode(root,
  1456. key.objectid);
  1457. BUG_ON(!inode);
  1458. ret = btrfs_truncate_inode_items(wc->trans,
  1459. root, inode, inode->i_size,
  1460. BTRFS_EXTENT_DATA_KEY);
  1461. BUG_ON(ret);
  1462. iput(inode);
  1463. }
  1464. ret = link_to_fixup_dir(wc->trans, root,
  1465. path, key.objectid);
  1466. BUG_ON(ret);
  1467. }
  1468. if (wc->stage < LOG_WALK_REPLAY_ALL)
  1469. continue;
  1470. /* these keys are simply copied */
  1471. if (key.type == BTRFS_XATTR_ITEM_KEY) {
  1472. ret = overwrite_item(wc->trans, root, path,
  1473. eb, i, &key);
  1474. BUG_ON(ret);
  1475. } else if (key.type == BTRFS_INODE_REF_KEY) {
  1476. ret = add_inode_ref(wc->trans, root, log, path,
  1477. eb, i, &key);
  1478. BUG_ON(ret && ret != -ENOENT);
  1479. } else if (key.type == BTRFS_EXTENT_DATA_KEY) {
  1480. ret = replay_one_extent(wc->trans, root, path,
  1481. eb, i, &key);
  1482. BUG_ON(ret);
  1483. } else if (key.type == BTRFS_EXTENT_CSUM_KEY) {
  1484. ret = replay_one_csum(wc->trans, root, path,
  1485. eb, i, &key);
  1486. BUG_ON(ret);
  1487. } else if (key.type == BTRFS_DIR_ITEM_KEY ||
  1488. key.type == BTRFS_DIR_INDEX_KEY) {
  1489. ret = replay_one_dir_item(wc->trans, root, path,
  1490. eb, i, &key);
  1491. BUG_ON(ret);
  1492. }
  1493. }
  1494. btrfs_free_path(path);
  1495. return 0;
  1496. }
  1497. static int noinline walk_down_log_tree(struct btrfs_trans_handle *trans,
  1498. struct btrfs_root *root,
  1499. struct btrfs_path *path, int *level,
  1500. struct walk_control *wc)
  1501. {
  1502. u64 root_owner;
  1503. u64 root_gen;
  1504. u64 bytenr;
  1505. u64 ptr_gen;
  1506. struct extent_buffer *next;
  1507. struct extent_buffer *cur;
  1508. struct extent_buffer *parent;
  1509. u32 blocksize;
  1510. int ret = 0;
  1511. WARN_ON(*level < 0);
  1512. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1513. while(*level > 0) {
  1514. WARN_ON(*level < 0);
  1515. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1516. cur = path->nodes[*level];
  1517. if (btrfs_header_level(cur) != *level)
  1518. WARN_ON(1);
  1519. if (path->slots[*level] >=
  1520. btrfs_header_nritems(cur))
  1521. break;
  1522. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  1523. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  1524. blocksize = btrfs_level_size(root, *level - 1);
  1525. parent = path->nodes[*level];
  1526. root_owner = btrfs_header_owner(parent);
  1527. root_gen = btrfs_header_generation(parent);
  1528. next = btrfs_find_create_tree_block(root, bytenr, blocksize);
  1529. wc->process_func(root, next, wc, ptr_gen);
  1530. if (*level == 1) {
  1531. path->slots[*level]++;
  1532. if (wc->free) {
  1533. btrfs_read_buffer(next, ptr_gen);
  1534. btrfs_tree_lock(next);
  1535. clean_tree_block(trans, root, next);
  1536. btrfs_wait_tree_block_writeback(next);
  1537. btrfs_tree_unlock(next);
  1538. ret = btrfs_drop_leaf_ref(trans, root, next);
  1539. BUG_ON(ret);
  1540. WARN_ON(root_owner !=
  1541. BTRFS_TREE_LOG_OBJECTID);
  1542. ret = btrfs_free_reserved_extent(root,
  1543. bytenr, blocksize);
  1544. BUG_ON(ret);
  1545. }
  1546. free_extent_buffer(next);
  1547. continue;
  1548. }
  1549. btrfs_read_buffer(next, ptr_gen);
  1550. WARN_ON(*level <= 0);
  1551. if (path->nodes[*level-1])
  1552. free_extent_buffer(path->nodes[*level-1]);
  1553. path->nodes[*level-1] = next;
  1554. *level = btrfs_header_level(next);
  1555. path->slots[*level] = 0;
  1556. cond_resched();
  1557. }
  1558. WARN_ON(*level < 0);
  1559. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1560. if (path->nodes[*level] == root->node) {
  1561. parent = path->nodes[*level];
  1562. } else {
  1563. parent = path->nodes[*level + 1];
  1564. }
  1565. bytenr = path->nodes[*level]->start;
  1566. blocksize = btrfs_level_size(root, *level);
  1567. root_owner = btrfs_header_owner(parent);
  1568. root_gen = btrfs_header_generation(parent);
  1569. wc->process_func(root, path->nodes[*level], wc,
  1570. btrfs_header_generation(path->nodes[*level]));
  1571. if (wc->free) {
  1572. next = path->nodes[*level];
  1573. btrfs_tree_lock(next);
  1574. clean_tree_block(trans, root, next);
  1575. btrfs_wait_tree_block_writeback(next);
  1576. btrfs_tree_unlock(next);
  1577. if (*level == 0) {
  1578. ret = btrfs_drop_leaf_ref(trans, root, next);
  1579. BUG_ON(ret);
  1580. }
  1581. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  1582. ret = btrfs_free_reserved_extent(root, bytenr, blocksize);
  1583. BUG_ON(ret);
  1584. }
  1585. free_extent_buffer(path->nodes[*level]);
  1586. path->nodes[*level] = NULL;
  1587. *level += 1;
  1588. cond_resched();
  1589. return 0;
  1590. }
  1591. static int noinline walk_up_log_tree(struct btrfs_trans_handle *trans,
  1592. struct btrfs_root *root,
  1593. struct btrfs_path *path, int *level,
  1594. struct walk_control *wc)
  1595. {
  1596. u64 root_owner;
  1597. u64 root_gen;
  1598. int i;
  1599. int slot;
  1600. int ret;
  1601. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1602. slot = path->slots[i];
  1603. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  1604. struct extent_buffer *node;
  1605. node = path->nodes[i];
  1606. path->slots[i]++;
  1607. *level = i;
  1608. WARN_ON(*level == 0);
  1609. return 0;
  1610. } else {
  1611. struct extent_buffer *parent;
  1612. if (path->nodes[*level] == root->node)
  1613. parent = path->nodes[*level];
  1614. else
  1615. parent = path->nodes[*level + 1];
  1616. root_owner = btrfs_header_owner(parent);
  1617. root_gen = btrfs_header_generation(parent);
  1618. wc->process_func(root, path->nodes[*level], wc,
  1619. btrfs_header_generation(path->nodes[*level]));
  1620. if (wc->free) {
  1621. struct extent_buffer *next;
  1622. next = path->nodes[*level];
  1623. btrfs_tree_lock(next);
  1624. clean_tree_block(trans, root, next);
  1625. btrfs_wait_tree_block_writeback(next);
  1626. btrfs_tree_unlock(next);
  1627. if (*level == 0) {
  1628. ret = btrfs_drop_leaf_ref(trans, root,
  1629. next);
  1630. BUG_ON(ret);
  1631. }
  1632. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  1633. ret = btrfs_free_reserved_extent(root,
  1634. path->nodes[*level]->start,
  1635. path->nodes[*level]->len);
  1636. BUG_ON(ret);
  1637. }
  1638. free_extent_buffer(path->nodes[*level]);
  1639. path->nodes[*level] = NULL;
  1640. *level = i + 1;
  1641. }
  1642. }
  1643. return 1;
  1644. }
  1645. /*
  1646. * drop the reference count on the tree rooted at 'snap'. This traverses
  1647. * the tree freeing any blocks that have a ref count of zero after being
  1648. * decremented.
  1649. */
  1650. static int walk_log_tree(struct btrfs_trans_handle *trans,
  1651. struct btrfs_root *log, struct walk_control *wc)
  1652. {
  1653. int ret = 0;
  1654. int wret;
  1655. int level;
  1656. struct btrfs_path *path;
  1657. int i;
  1658. int orig_level;
  1659. path = btrfs_alloc_path();
  1660. BUG_ON(!path);
  1661. level = btrfs_header_level(log->node);
  1662. orig_level = level;
  1663. path->nodes[level] = log->node;
  1664. extent_buffer_get(log->node);
  1665. path->slots[level] = 0;
  1666. while(1) {
  1667. wret = walk_down_log_tree(trans, log, path, &level, wc);
  1668. if (wret > 0)
  1669. break;
  1670. if (wret < 0)
  1671. ret = wret;
  1672. wret = walk_up_log_tree(trans, log, path, &level, wc);
  1673. if (wret > 0)
  1674. break;
  1675. if (wret < 0)
  1676. ret = wret;
  1677. }
  1678. /* was the root node processed? if not, catch it here */
  1679. if (path->nodes[orig_level]) {
  1680. wc->process_func(log, path->nodes[orig_level], wc,
  1681. btrfs_header_generation(path->nodes[orig_level]));
  1682. if (wc->free) {
  1683. struct extent_buffer *next;
  1684. next = path->nodes[orig_level];
  1685. btrfs_tree_lock(next);
  1686. clean_tree_block(trans, log, next);
  1687. btrfs_wait_tree_block_writeback(next);
  1688. btrfs_tree_unlock(next);
  1689. if (orig_level == 0) {
  1690. ret = btrfs_drop_leaf_ref(trans, log,
  1691. next);
  1692. BUG_ON(ret);
  1693. }
  1694. WARN_ON(log->root_key.objectid !=
  1695. BTRFS_TREE_LOG_OBJECTID);
  1696. ret = btrfs_free_reserved_extent(log, next->start,
  1697. next->len);
  1698. BUG_ON(ret);
  1699. }
  1700. }
  1701. for (i = 0; i <= orig_level; i++) {
  1702. if (path->nodes[i]) {
  1703. free_extent_buffer(path->nodes[i]);
  1704. path->nodes[i] = NULL;
  1705. }
  1706. }
  1707. btrfs_free_path(path);
  1708. if (wc->free)
  1709. free_extent_buffer(log->node);
  1710. return ret;
  1711. }
  1712. static int wait_log_commit(struct btrfs_root *log)
  1713. {
  1714. DEFINE_WAIT(wait);
  1715. u64 transid = log->fs_info->tree_log_transid;
  1716. do {
  1717. prepare_to_wait(&log->fs_info->tree_log_wait, &wait,
  1718. TASK_UNINTERRUPTIBLE);
  1719. mutex_unlock(&log->fs_info->tree_log_mutex);
  1720. if (atomic_read(&log->fs_info->tree_log_commit))
  1721. schedule();
  1722. finish_wait(&log->fs_info->tree_log_wait, &wait);
  1723. mutex_lock(&log->fs_info->tree_log_mutex);
  1724. } while(transid == log->fs_info->tree_log_transid &&
  1725. atomic_read(&log->fs_info->tree_log_commit));
  1726. return 0;
  1727. }
  1728. /*
  1729. * btrfs_sync_log does sends a given tree log down to the disk and
  1730. * updates the super blocks to record it. When this call is done,
  1731. * you know that any inodes previously logged are safely on disk
  1732. */
  1733. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  1734. struct btrfs_root *root)
  1735. {
  1736. int ret;
  1737. unsigned long batch;
  1738. struct btrfs_root *log = root->log_root;
  1739. mutex_lock(&log->fs_info->tree_log_mutex);
  1740. if (atomic_read(&log->fs_info->tree_log_commit)) {
  1741. wait_log_commit(log);
  1742. goto out;
  1743. }
  1744. atomic_set(&log->fs_info->tree_log_commit, 1);
  1745. while(1) {
  1746. batch = log->fs_info->tree_log_batch;
  1747. mutex_unlock(&log->fs_info->tree_log_mutex);
  1748. schedule_timeout_uninterruptible(1);
  1749. mutex_lock(&log->fs_info->tree_log_mutex);
  1750. while(atomic_read(&log->fs_info->tree_log_writers)) {
  1751. DEFINE_WAIT(wait);
  1752. prepare_to_wait(&log->fs_info->tree_log_wait, &wait,
  1753. TASK_UNINTERRUPTIBLE);
  1754. mutex_unlock(&log->fs_info->tree_log_mutex);
  1755. if (atomic_read(&log->fs_info->tree_log_writers))
  1756. schedule();
  1757. mutex_lock(&log->fs_info->tree_log_mutex);
  1758. finish_wait(&log->fs_info->tree_log_wait, &wait);
  1759. }
  1760. if (batch == log->fs_info->tree_log_batch)
  1761. break;
  1762. }
  1763. ret = btrfs_write_and_wait_marked_extents(log, &log->dirty_log_pages);
  1764. BUG_ON(ret);
  1765. ret = btrfs_write_and_wait_marked_extents(root->fs_info->log_root_tree,
  1766. &root->fs_info->log_root_tree->dirty_log_pages);
  1767. BUG_ON(ret);
  1768. btrfs_set_super_log_root(&root->fs_info->super_for_commit,
  1769. log->fs_info->log_root_tree->node->start);
  1770. btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
  1771. btrfs_header_level(log->fs_info->log_root_tree->node));
  1772. write_ctree_super(trans, log->fs_info->tree_root, 2);
  1773. log->fs_info->tree_log_transid++;
  1774. log->fs_info->tree_log_batch = 0;
  1775. atomic_set(&log->fs_info->tree_log_commit, 0);
  1776. smp_mb();
  1777. if (waitqueue_active(&log->fs_info->tree_log_wait))
  1778. wake_up(&log->fs_info->tree_log_wait);
  1779. out:
  1780. mutex_unlock(&log->fs_info->tree_log_mutex);
  1781. return 0;
  1782. }
  1783. /* * free all the extents used by the tree log. This should be called
  1784. * at commit time of the full transaction
  1785. */
  1786. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  1787. {
  1788. int ret;
  1789. struct btrfs_root *log;
  1790. struct key;
  1791. u64 start;
  1792. u64 end;
  1793. struct walk_control wc = {
  1794. .free = 1,
  1795. .process_func = process_one_buffer
  1796. };
  1797. if (!root->log_root)
  1798. return 0;
  1799. log = root->log_root;
  1800. ret = walk_log_tree(trans, log, &wc);
  1801. BUG_ON(ret);
  1802. while(1) {
  1803. ret = find_first_extent_bit(&log->dirty_log_pages,
  1804. 0, &start, &end, EXTENT_DIRTY);
  1805. if (ret)
  1806. break;
  1807. clear_extent_dirty(&log->dirty_log_pages,
  1808. start, end, GFP_NOFS);
  1809. }
  1810. log = root->log_root;
  1811. ret = btrfs_del_root(trans, root->fs_info->log_root_tree,
  1812. &log->root_key);
  1813. BUG_ON(ret);
  1814. root->log_root = NULL;
  1815. kfree(root->log_root);
  1816. return 0;
  1817. }
  1818. /*
  1819. * helper function to update the item for a given subvolumes log root
  1820. * in the tree of log roots
  1821. */
  1822. static int update_log_root(struct btrfs_trans_handle *trans,
  1823. struct btrfs_root *log)
  1824. {
  1825. u64 bytenr = btrfs_root_bytenr(&log->root_item);
  1826. int ret;
  1827. if (log->node->start == bytenr)
  1828. return 0;
  1829. btrfs_set_root_bytenr(&log->root_item, log->node->start);
  1830. btrfs_set_root_generation(&log->root_item, trans->transid);
  1831. btrfs_set_root_level(&log->root_item, btrfs_header_level(log->node));
  1832. ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
  1833. &log->root_key, &log->root_item);
  1834. BUG_ON(ret);
  1835. return ret;
  1836. }
  1837. /*
  1838. * If both a file and directory are logged, and unlinks or renames are
  1839. * mixed in, we have a few interesting corners:
  1840. *
  1841. * create file X in dir Y
  1842. * link file X to X.link in dir Y
  1843. * fsync file X
  1844. * unlink file X but leave X.link
  1845. * fsync dir Y
  1846. *
  1847. * After a crash we would expect only X.link to exist. But file X
  1848. * didn't get fsync'd again so the log has back refs for X and X.link.
  1849. *
  1850. * We solve this by removing directory entries and inode backrefs from the
  1851. * log when a file that was logged in the current transaction is
  1852. * unlinked. Any later fsync will include the updated log entries, and
  1853. * we'll be able to reconstruct the proper directory items from backrefs.
  1854. *
  1855. * This optimizations allows us to avoid relogging the entire inode
  1856. * or the entire directory.
  1857. */
  1858. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  1859. struct btrfs_root *root,
  1860. const char *name, int name_len,
  1861. struct inode *dir, u64 index)
  1862. {
  1863. struct btrfs_root *log;
  1864. struct btrfs_dir_item *di;
  1865. struct btrfs_path *path;
  1866. int ret;
  1867. int bytes_del = 0;
  1868. if (BTRFS_I(dir)->logged_trans < trans->transid)
  1869. return 0;
  1870. ret = join_running_log_trans(root);
  1871. if (ret)
  1872. return 0;
  1873. mutex_lock(&BTRFS_I(dir)->log_mutex);
  1874. log = root->log_root;
  1875. path = btrfs_alloc_path();
  1876. di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
  1877. name, name_len, -1);
  1878. if (di && !IS_ERR(di)) {
  1879. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  1880. bytes_del += name_len;
  1881. BUG_ON(ret);
  1882. }
  1883. btrfs_release_path(log, path);
  1884. di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino,
  1885. index, name, name_len, -1);
  1886. if (di && !IS_ERR(di)) {
  1887. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  1888. bytes_del += name_len;
  1889. BUG_ON(ret);
  1890. }
  1891. /* update the directory size in the log to reflect the names
  1892. * we have removed
  1893. */
  1894. if (bytes_del) {
  1895. struct btrfs_key key;
  1896. key.objectid = dir->i_ino;
  1897. key.offset = 0;
  1898. key.type = BTRFS_INODE_ITEM_KEY;
  1899. btrfs_release_path(log, path);
  1900. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  1901. if (ret == 0) {
  1902. struct btrfs_inode_item *item;
  1903. u64 i_size;
  1904. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1905. struct btrfs_inode_item);
  1906. i_size = btrfs_inode_size(path->nodes[0], item);
  1907. if (i_size > bytes_del)
  1908. i_size -= bytes_del;
  1909. else
  1910. i_size = 0;
  1911. btrfs_set_inode_size(path->nodes[0], item, i_size);
  1912. btrfs_mark_buffer_dirty(path->nodes[0]);
  1913. } else
  1914. ret = 0;
  1915. btrfs_release_path(log, path);
  1916. }
  1917. btrfs_free_path(path);
  1918. mutex_unlock(&BTRFS_I(dir)->log_mutex);
  1919. end_log_trans(root);
  1920. return 0;
  1921. }
  1922. /* see comments for btrfs_del_dir_entries_in_log */
  1923. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  1924. struct btrfs_root *root,
  1925. const char *name, int name_len,
  1926. struct inode *inode, u64 dirid)
  1927. {
  1928. struct btrfs_root *log;
  1929. u64 index;
  1930. int ret;
  1931. if (BTRFS_I(inode)->logged_trans < trans->transid)
  1932. return 0;
  1933. ret = join_running_log_trans(root);
  1934. if (ret)
  1935. return 0;
  1936. log = root->log_root;
  1937. mutex_lock(&BTRFS_I(inode)->log_mutex);
  1938. ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino,
  1939. dirid, &index);
  1940. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  1941. end_log_trans(root);
  1942. return ret;
  1943. }
  1944. /*
  1945. * creates a range item in the log for 'dirid'. first_offset and
  1946. * last_offset tell us which parts of the key space the log should
  1947. * be considered authoritative for.
  1948. */
  1949. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  1950. struct btrfs_root *log,
  1951. struct btrfs_path *path,
  1952. int key_type, u64 dirid,
  1953. u64 first_offset, u64 last_offset)
  1954. {
  1955. int ret;
  1956. struct btrfs_key key;
  1957. struct btrfs_dir_log_item *item;
  1958. key.objectid = dirid;
  1959. key.offset = first_offset;
  1960. if (key_type == BTRFS_DIR_ITEM_KEY)
  1961. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  1962. else
  1963. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  1964. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  1965. BUG_ON(ret);
  1966. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1967. struct btrfs_dir_log_item);
  1968. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  1969. btrfs_mark_buffer_dirty(path->nodes[0]);
  1970. btrfs_release_path(log, path);
  1971. return 0;
  1972. }
  1973. /*
  1974. * log all the items included in the current transaction for a given
  1975. * directory. This also creates the range items in the log tree required
  1976. * to replay anything deleted before the fsync
  1977. */
  1978. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  1979. struct btrfs_root *root, struct inode *inode,
  1980. struct btrfs_path *path,
  1981. struct btrfs_path *dst_path, int key_type,
  1982. u64 min_offset, u64 *last_offset_ret)
  1983. {
  1984. struct btrfs_key min_key;
  1985. struct btrfs_key max_key;
  1986. struct btrfs_root *log = root->log_root;
  1987. struct extent_buffer *src;
  1988. int ret;
  1989. int i;
  1990. int nritems;
  1991. u64 first_offset = min_offset;
  1992. u64 last_offset = (u64)-1;
  1993. log = root->log_root;
  1994. max_key.objectid = inode->i_ino;
  1995. max_key.offset = (u64)-1;
  1996. max_key.type = key_type;
  1997. min_key.objectid = inode->i_ino;
  1998. min_key.type = key_type;
  1999. min_key.offset = min_offset;
  2000. path->keep_locks = 1;
  2001. ret = btrfs_search_forward(root, &min_key, &max_key,
  2002. path, 0, trans->transid);
  2003. /*
  2004. * we didn't find anything from this transaction, see if there
  2005. * is anything at all
  2006. */
  2007. if (ret != 0 || min_key.objectid != inode->i_ino ||
  2008. min_key.type != key_type) {
  2009. min_key.objectid = inode->i_ino;
  2010. min_key.type = key_type;
  2011. min_key.offset = (u64)-1;
  2012. btrfs_release_path(root, path);
  2013. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2014. if (ret < 0) {
  2015. btrfs_release_path(root, path);
  2016. return ret;
  2017. }
  2018. ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
  2019. /* if ret == 0 there are items for this type,
  2020. * create a range to tell us the last key of this type.
  2021. * otherwise, there are no items in this directory after
  2022. * *min_offset, and we create a range to indicate that.
  2023. */
  2024. if (ret == 0) {
  2025. struct btrfs_key tmp;
  2026. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  2027. path->slots[0]);
  2028. if (key_type == tmp.type) {
  2029. first_offset = max(min_offset, tmp.offset) + 1;
  2030. }
  2031. }
  2032. goto done;
  2033. }
  2034. /* go backward to find any previous key */
  2035. ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
  2036. if (ret == 0) {
  2037. struct btrfs_key tmp;
  2038. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2039. if (key_type == tmp.type) {
  2040. first_offset = tmp.offset;
  2041. ret = overwrite_item(trans, log, dst_path,
  2042. path->nodes[0], path->slots[0],
  2043. &tmp);
  2044. }
  2045. }
  2046. btrfs_release_path(root, path);
  2047. /* find the first key from this transaction again */
  2048. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2049. if (ret != 0) {
  2050. WARN_ON(1);
  2051. goto done;
  2052. }
  2053. /*
  2054. * we have a block from this transaction, log every item in it
  2055. * from our directory
  2056. */
  2057. while(1) {
  2058. struct btrfs_key tmp;
  2059. src = path->nodes[0];
  2060. nritems = btrfs_header_nritems(src);
  2061. for (i = path->slots[0]; i < nritems; i++) {
  2062. btrfs_item_key_to_cpu(src, &min_key, i);
  2063. if (min_key.objectid != inode->i_ino ||
  2064. min_key.type != key_type)
  2065. goto done;
  2066. ret = overwrite_item(trans, log, dst_path, src, i,
  2067. &min_key);
  2068. BUG_ON(ret);
  2069. }
  2070. path->slots[0] = nritems;
  2071. /*
  2072. * look ahead to the next item and see if it is also
  2073. * from this directory and from this transaction
  2074. */
  2075. ret = btrfs_next_leaf(root, path);
  2076. if (ret == 1) {
  2077. last_offset = (u64)-1;
  2078. goto done;
  2079. }
  2080. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2081. if (tmp.objectid != inode->i_ino || tmp.type != key_type) {
  2082. last_offset = (u64)-1;
  2083. goto done;
  2084. }
  2085. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  2086. ret = overwrite_item(trans, log, dst_path,
  2087. path->nodes[0], path->slots[0],
  2088. &tmp);
  2089. BUG_ON(ret);
  2090. last_offset = tmp.offset;
  2091. goto done;
  2092. }
  2093. }
  2094. done:
  2095. *last_offset_ret = last_offset;
  2096. btrfs_release_path(root, path);
  2097. btrfs_release_path(log, dst_path);
  2098. /* insert the log range keys to indicate where the log is valid */
  2099. ret = insert_dir_log_key(trans, log, path, key_type, inode->i_ino,
  2100. first_offset, last_offset);
  2101. BUG_ON(ret);
  2102. return 0;
  2103. }
  2104. /*
  2105. * logging directories is very similar to logging inodes, We find all the items
  2106. * from the current transaction and write them to the log.
  2107. *
  2108. * The recovery code scans the directory in the subvolume, and if it finds a
  2109. * key in the range logged that is not present in the log tree, then it means
  2110. * that dir entry was unlinked during the transaction.
  2111. *
  2112. * In order for that scan to work, we must include one key smaller than
  2113. * the smallest logged by this transaction and one key larger than the largest
  2114. * key logged by this transaction.
  2115. */
  2116. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  2117. struct btrfs_root *root, struct inode *inode,
  2118. struct btrfs_path *path,
  2119. struct btrfs_path *dst_path)
  2120. {
  2121. u64 min_key;
  2122. u64 max_key;
  2123. int ret;
  2124. int key_type = BTRFS_DIR_ITEM_KEY;
  2125. again:
  2126. min_key = 0;
  2127. max_key = 0;
  2128. while(1) {
  2129. ret = log_dir_items(trans, root, inode, path,
  2130. dst_path, key_type, min_key,
  2131. &max_key);
  2132. BUG_ON(ret);
  2133. if (max_key == (u64)-1)
  2134. break;
  2135. min_key = max_key + 1;
  2136. }
  2137. if (key_type == BTRFS_DIR_ITEM_KEY) {
  2138. key_type = BTRFS_DIR_INDEX_KEY;
  2139. goto again;
  2140. }
  2141. return 0;
  2142. }
  2143. /*
  2144. * a helper function to drop items from the log before we relog an
  2145. * inode. max_key_type indicates the highest item type to remove.
  2146. * This cannot be run for file data extents because it does not
  2147. * free the extents they point to.
  2148. */
  2149. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  2150. struct btrfs_root *log,
  2151. struct btrfs_path *path,
  2152. u64 objectid, int max_key_type)
  2153. {
  2154. int ret;
  2155. struct btrfs_key key;
  2156. struct btrfs_key found_key;
  2157. key.objectid = objectid;
  2158. key.type = max_key_type;
  2159. key.offset = (u64)-1;
  2160. while(1) {
  2161. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  2162. if (ret != 1)
  2163. break;
  2164. if (path->slots[0] == 0)
  2165. break;
  2166. path->slots[0]--;
  2167. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2168. path->slots[0]);
  2169. if (found_key.objectid != objectid)
  2170. break;
  2171. ret = btrfs_del_item(trans, log, path);
  2172. BUG_ON(ret);
  2173. btrfs_release_path(log, path);
  2174. }
  2175. btrfs_release_path(log, path);
  2176. return 0;
  2177. }
  2178. static noinline int copy_extent_csums(struct btrfs_trans_handle *trans,
  2179. struct list_head *list,
  2180. struct btrfs_root *root,
  2181. u64 disk_bytenr, u64 len)
  2182. {
  2183. struct btrfs_ordered_sum *sums;
  2184. struct btrfs_sector_sum *sector_sum;
  2185. int ret;
  2186. struct btrfs_path *path;
  2187. struct btrfs_csum_item *item = NULL;
  2188. u64 end = disk_bytenr + len;
  2189. u64 item_start_offset = 0;
  2190. u64 item_last_offset = 0;
  2191. u32 diff;
  2192. u32 sum;
  2193. u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
  2194. sums = kzalloc(btrfs_ordered_sum_size(root, len), GFP_NOFS);
  2195. sector_sum = sums->sums;
  2196. sums->bytenr = disk_bytenr;
  2197. sums->len = len;
  2198. list_add_tail(&sums->list, list);
  2199. path = btrfs_alloc_path();
  2200. while(disk_bytenr < end) {
  2201. if (!item || disk_bytenr < item_start_offset ||
  2202. disk_bytenr >= item_last_offset) {
  2203. struct btrfs_key found_key;
  2204. u32 item_size;
  2205. if (item)
  2206. btrfs_release_path(root, path);
  2207. item = btrfs_lookup_csum(NULL, root, path,
  2208. disk_bytenr, 0);
  2209. if (IS_ERR(item)) {
  2210. ret = PTR_ERR(item);
  2211. if (ret == -ENOENT || ret == -EFBIG)
  2212. ret = 0;
  2213. sum = 0;
  2214. printk("log no csum found for byte %llu\n",
  2215. (unsigned long long)disk_bytenr);
  2216. item = NULL;
  2217. btrfs_release_path(root, path);
  2218. goto found;
  2219. }
  2220. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2221. path->slots[0]);
  2222. item_start_offset = found_key.offset;
  2223. item_size = btrfs_item_size_nr(path->nodes[0],
  2224. path->slots[0]);
  2225. item_last_offset = item_start_offset +
  2226. (item_size / csum_size) *
  2227. root->sectorsize;
  2228. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2229. struct btrfs_csum_item);
  2230. }
  2231. /*
  2232. * this byte range must be able to fit inside
  2233. * a single leaf so it will also fit inside a u32
  2234. */
  2235. diff = disk_bytenr - item_start_offset;
  2236. diff = diff / root->sectorsize;
  2237. diff = diff * csum_size;
  2238. read_extent_buffer(path->nodes[0], &sum,
  2239. ((unsigned long)item) + diff,
  2240. csum_size);
  2241. found:
  2242. sector_sum->bytenr = disk_bytenr;
  2243. sector_sum->sum = sum;
  2244. disk_bytenr += root->sectorsize;
  2245. sector_sum++;
  2246. }
  2247. btrfs_free_path(path);
  2248. return 0;
  2249. }
  2250. static noinline int copy_items(struct btrfs_trans_handle *trans,
  2251. struct btrfs_root *log,
  2252. struct btrfs_path *dst_path,
  2253. struct extent_buffer *src,
  2254. int start_slot, int nr, int inode_only)
  2255. {
  2256. unsigned long src_offset;
  2257. unsigned long dst_offset;
  2258. struct btrfs_file_extent_item *extent;
  2259. struct btrfs_inode_item *inode_item;
  2260. int ret;
  2261. struct btrfs_key *ins_keys;
  2262. u32 *ins_sizes;
  2263. char *ins_data;
  2264. int i;
  2265. struct list_head ordered_sums;
  2266. INIT_LIST_HEAD(&ordered_sums);
  2267. ins_data = kmalloc(nr * sizeof(struct btrfs_key) +
  2268. nr * sizeof(u32), GFP_NOFS);
  2269. ins_sizes = (u32 *)ins_data;
  2270. ins_keys = (struct btrfs_key *)(ins_data + nr * sizeof(u32));
  2271. for (i = 0; i < nr; i++) {
  2272. ins_sizes[i] = btrfs_item_size_nr(src, i + start_slot);
  2273. btrfs_item_key_to_cpu(src, ins_keys + i, i + start_slot);
  2274. }
  2275. ret = btrfs_insert_empty_items(trans, log, dst_path,
  2276. ins_keys, ins_sizes, nr);
  2277. BUG_ON(ret);
  2278. for (i = 0; i < nr; i++) {
  2279. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  2280. dst_path->slots[0]);
  2281. src_offset = btrfs_item_ptr_offset(src, start_slot + i);
  2282. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  2283. src_offset, ins_sizes[i]);
  2284. if (inode_only == LOG_INODE_EXISTS &&
  2285. ins_keys[i].type == BTRFS_INODE_ITEM_KEY) {
  2286. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  2287. dst_path->slots[0],
  2288. struct btrfs_inode_item);
  2289. btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
  2290. /* set the generation to zero so the recover code
  2291. * can tell the difference between an logging
  2292. * just to say 'this inode exists' and a logging
  2293. * to say 'update this inode with these values'
  2294. */
  2295. btrfs_set_inode_generation(dst_path->nodes[0],
  2296. inode_item, 0);
  2297. }
  2298. /* take a reference on file data extents so that truncates
  2299. * or deletes of this inode don't have to relog the inode
  2300. * again
  2301. */
  2302. if (btrfs_key_type(ins_keys + i) == BTRFS_EXTENT_DATA_KEY) {
  2303. int found_type;
  2304. extent = btrfs_item_ptr(src, start_slot + i,
  2305. struct btrfs_file_extent_item);
  2306. found_type = btrfs_file_extent_type(src, extent);
  2307. if (found_type == BTRFS_FILE_EXTENT_REG ||
  2308. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  2309. u64 ds = btrfs_file_extent_disk_bytenr(src,
  2310. extent);
  2311. u64 dl = btrfs_file_extent_disk_num_bytes(src,
  2312. extent);
  2313. u64 cs = btrfs_file_extent_offset(src, extent);
  2314. u64 cl = btrfs_file_extent_num_bytes(src,
  2315. extent);;
  2316. if (btrfs_file_extent_compression(src,
  2317. extent)) {
  2318. cs = 0;
  2319. cl = dl;
  2320. }
  2321. /* ds == 0 is a hole */
  2322. if (ds != 0) {
  2323. ret = btrfs_inc_extent_ref(trans, log,
  2324. ds, dl,
  2325. dst_path->nodes[0]->start,
  2326. BTRFS_TREE_LOG_OBJECTID,
  2327. trans->transid,
  2328. ins_keys[i].objectid);
  2329. BUG_ON(ret);
  2330. ret = copy_extent_csums(trans,
  2331. &ordered_sums,
  2332. log->fs_info->csum_root,
  2333. ds + cs, cl);
  2334. BUG_ON(ret);
  2335. }
  2336. }
  2337. }
  2338. dst_path->slots[0]++;
  2339. }
  2340. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  2341. btrfs_release_path(log, dst_path);
  2342. kfree(ins_data);
  2343. /*
  2344. * we have to do this after the loop above to avoid changing the
  2345. * log tree while trying to change the log tree.
  2346. */
  2347. while(!list_empty(&ordered_sums)) {
  2348. struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next,
  2349. struct btrfs_ordered_sum,
  2350. list);
  2351. ret = btrfs_csum_file_blocks(trans, log, sums);
  2352. BUG_ON(ret);
  2353. list_del(&sums->list);
  2354. kfree(sums);
  2355. }
  2356. return 0;
  2357. }
  2358. /* log a single inode in the tree log.
  2359. * At least one parent directory for this inode must exist in the tree
  2360. * or be logged already.
  2361. *
  2362. * Any items from this inode changed by the current transaction are copied
  2363. * to the log tree. An extra reference is taken on any extents in this
  2364. * file, allowing us to avoid a whole pile of corner cases around logging
  2365. * blocks that have been removed from the tree.
  2366. *
  2367. * See LOG_INODE_ALL and related defines for a description of what inode_only
  2368. * does.
  2369. *
  2370. * This handles both files and directories.
  2371. */
  2372. static int __btrfs_log_inode(struct btrfs_trans_handle *trans,
  2373. struct btrfs_root *root, struct inode *inode,
  2374. int inode_only)
  2375. {
  2376. struct btrfs_path *path;
  2377. struct btrfs_path *dst_path;
  2378. struct btrfs_key min_key;
  2379. struct btrfs_key max_key;
  2380. struct btrfs_root *log = root->log_root;
  2381. struct extent_buffer *src = NULL;
  2382. u32 size;
  2383. int ret;
  2384. int nritems;
  2385. int ins_start_slot = 0;
  2386. int ins_nr;
  2387. log = root->log_root;
  2388. path = btrfs_alloc_path();
  2389. dst_path = btrfs_alloc_path();
  2390. min_key.objectid = inode->i_ino;
  2391. min_key.type = BTRFS_INODE_ITEM_KEY;
  2392. min_key.offset = 0;
  2393. max_key.objectid = inode->i_ino;
  2394. if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
  2395. max_key.type = BTRFS_XATTR_ITEM_KEY;
  2396. else
  2397. max_key.type = (u8)-1;
  2398. max_key.offset = (u64)-1;
  2399. /*
  2400. * if this inode has already been logged and we're in inode_only
  2401. * mode, we don't want to delete the things that have already
  2402. * been written to the log.
  2403. *
  2404. * But, if the inode has been through an inode_only log,
  2405. * the logged_trans field is not set. This allows us to catch
  2406. * any new names for this inode in the backrefs by logging it
  2407. * again
  2408. */
  2409. if (inode_only == LOG_INODE_EXISTS &&
  2410. BTRFS_I(inode)->logged_trans == trans->transid) {
  2411. btrfs_free_path(path);
  2412. btrfs_free_path(dst_path);
  2413. goto out;
  2414. }
  2415. mutex_lock(&BTRFS_I(inode)->log_mutex);
  2416. /*
  2417. * a brute force approach to making sure we get the most uptodate
  2418. * copies of everything.
  2419. */
  2420. if (S_ISDIR(inode->i_mode)) {
  2421. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  2422. if (inode_only == LOG_INODE_EXISTS)
  2423. max_key_type = BTRFS_XATTR_ITEM_KEY;
  2424. ret = drop_objectid_items(trans, log, path,
  2425. inode->i_ino, max_key_type);
  2426. } else {
  2427. ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
  2428. }
  2429. BUG_ON(ret);
  2430. path->keep_locks = 1;
  2431. while(1) {
  2432. ins_nr = 0;
  2433. ret = btrfs_search_forward(root, &min_key, &max_key,
  2434. path, 0, trans->transid);
  2435. if (ret != 0)
  2436. break;
  2437. again:
  2438. /* note, ins_nr might be > 0 here, cleanup outside the loop */
  2439. if (min_key.objectid != inode->i_ino)
  2440. break;
  2441. if (min_key.type > max_key.type)
  2442. break;
  2443. src = path->nodes[0];
  2444. size = btrfs_item_size_nr(src, path->slots[0]);
  2445. if (ins_nr && ins_start_slot + ins_nr == path->slots[0]) {
  2446. ins_nr++;
  2447. goto next_slot;
  2448. } else if (!ins_nr) {
  2449. ins_start_slot = path->slots[0];
  2450. ins_nr = 1;
  2451. goto next_slot;
  2452. }
  2453. ret = copy_items(trans, log, dst_path, src, ins_start_slot,
  2454. ins_nr, inode_only);
  2455. BUG_ON(ret);
  2456. ins_nr = 1;
  2457. ins_start_slot = path->slots[0];
  2458. next_slot:
  2459. nritems = btrfs_header_nritems(path->nodes[0]);
  2460. path->slots[0]++;
  2461. if (path->slots[0] < nritems) {
  2462. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  2463. path->slots[0]);
  2464. goto again;
  2465. }
  2466. if (ins_nr) {
  2467. ret = copy_items(trans, log, dst_path, src,
  2468. ins_start_slot,
  2469. ins_nr, inode_only);
  2470. BUG_ON(ret);
  2471. ins_nr = 0;
  2472. }
  2473. btrfs_release_path(root, path);
  2474. if (min_key.offset < (u64)-1)
  2475. min_key.offset++;
  2476. else if (min_key.type < (u8)-1)
  2477. min_key.type++;
  2478. else if (min_key.objectid < (u64)-1)
  2479. min_key.objectid++;
  2480. else
  2481. break;
  2482. }
  2483. if (ins_nr) {
  2484. ret = copy_items(trans, log, dst_path, src,
  2485. ins_start_slot,
  2486. ins_nr, inode_only);
  2487. BUG_ON(ret);
  2488. ins_nr = 0;
  2489. }
  2490. WARN_ON(ins_nr);
  2491. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode)) {
  2492. btrfs_release_path(root, path);
  2493. btrfs_release_path(log, dst_path);
  2494. BTRFS_I(inode)->log_dirty_trans = 0;
  2495. ret = log_directory_changes(trans, root, inode, path, dst_path);
  2496. BUG_ON(ret);
  2497. }
  2498. BTRFS_I(inode)->logged_trans = trans->transid;
  2499. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  2500. btrfs_free_path(path);
  2501. btrfs_free_path(dst_path);
  2502. mutex_lock(&root->fs_info->tree_log_mutex);
  2503. ret = update_log_root(trans, log);
  2504. BUG_ON(ret);
  2505. mutex_unlock(&root->fs_info->tree_log_mutex);
  2506. out:
  2507. return 0;
  2508. }
  2509. int btrfs_log_inode(struct btrfs_trans_handle *trans,
  2510. struct btrfs_root *root, struct inode *inode,
  2511. int inode_only)
  2512. {
  2513. int ret;
  2514. start_log_trans(trans, root);
  2515. ret = __btrfs_log_inode(trans, root, inode, inode_only);
  2516. end_log_trans(root);
  2517. return ret;
  2518. }
  2519. /*
  2520. * helper function around btrfs_log_inode to make sure newly created
  2521. * parent directories also end up in the log. A minimal inode and backref
  2522. * only logging is done of any parent directories that are older than
  2523. * the last committed transaction
  2524. */
  2525. int btrfs_log_dentry(struct btrfs_trans_handle *trans,
  2526. struct btrfs_root *root, struct dentry *dentry)
  2527. {
  2528. int inode_only = LOG_INODE_ALL;
  2529. struct super_block *sb;
  2530. int ret;
  2531. start_log_trans(trans, root);
  2532. sb = dentry->d_inode->i_sb;
  2533. while(1) {
  2534. ret = __btrfs_log_inode(trans, root, dentry->d_inode,
  2535. inode_only);
  2536. BUG_ON(ret);
  2537. inode_only = LOG_INODE_EXISTS;
  2538. dentry = dentry->d_parent;
  2539. if (!dentry || !dentry->d_inode || sb != dentry->d_inode->i_sb)
  2540. break;
  2541. if (BTRFS_I(dentry->d_inode)->generation <=
  2542. root->fs_info->last_trans_committed)
  2543. break;
  2544. }
  2545. end_log_trans(root);
  2546. return 0;
  2547. }
  2548. /*
  2549. * it is not safe to log dentry if the chunk root has added new
  2550. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  2551. * If this returns 1, you must commit the transaction to safely get your
  2552. * data on disk.
  2553. */
  2554. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  2555. struct btrfs_root *root, struct dentry *dentry)
  2556. {
  2557. u64 gen;
  2558. gen = root->fs_info->last_trans_new_blockgroup;
  2559. if (gen > root->fs_info->last_trans_committed)
  2560. return 1;
  2561. else
  2562. return btrfs_log_dentry(trans, root, dentry);
  2563. }
  2564. /*
  2565. * should be called during mount to recover any replay any log trees
  2566. * from the FS
  2567. */
  2568. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  2569. {
  2570. int ret;
  2571. struct btrfs_path *path;
  2572. struct btrfs_trans_handle *trans;
  2573. struct btrfs_key key;
  2574. struct btrfs_key found_key;
  2575. struct btrfs_key tmp_key;
  2576. struct btrfs_root *log;
  2577. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  2578. u64 highest_inode;
  2579. struct walk_control wc = {
  2580. .process_func = process_one_buffer,
  2581. .stage = 0,
  2582. };
  2583. fs_info->log_root_recovering = 1;
  2584. path = btrfs_alloc_path();
  2585. BUG_ON(!path);
  2586. trans = btrfs_start_transaction(fs_info->tree_root, 1);
  2587. wc.trans = trans;
  2588. wc.pin = 1;
  2589. walk_log_tree(trans, log_root_tree, &wc);
  2590. again:
  2591. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  2592. key.offset = (u64)-1;
  2593. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  2594. while(1) {
  2595. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  2596. if (ret < 0)
  2597. break;
  2598. if (ret > 0) {
  2599. if (path->slots[0] == 0)
  2600. break;
  2601. path->slots[0]--;
  2602. }
  2603. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2604. path->slots[0]);
  2605. btrfs_release_path(log_root_tree, path);
  2606. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  2607. break;
  2608. log = btrfs_read_fs_root_no_radix(log_root_tree,
  2609. &found_key);
  2610. BUG_ON(!log);
  2611. tmp_key.objectid = found_key.offset;
  2612. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  2613. tmp_key.offset = (u64)-1;
  2614. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  2615. BUG_ON(!wc.replay_dest);
  2616. btrfs_record_root_in_trans(wc.replay_dest);
  2617. ret = walk_log_tree(trans, log, &wc);
  2618. BUG_ON(ret);
  2619. if (wc.stage == LOG_WALK_REPLAY_ALL) {
  2620. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  2621. path);
  2622. BUG_ON(ret);
  2623. }
  2624. ret = btrfs_find_highest_inode(wc.replay_dest, &highest_inode);
  2625. if (ret == 0) {
  2626. wc.replay_dest->highest_inode = highest_inode;
  2627. wc.replay_dest->last_inode_alloc = highest_inode;
  2628. }
  2629. key.offset = found_key.offset - 1;
  2630. free_extent_buffer(log->node);
  2631. kfree(log);
  2632. if (found_key.offset == 0)
  2633. break;
  2634. }
  2635. btrfs_release_path(log_root_tree, path);
  2636. /* step one is to pin it all, step two is to replay just inodes */
  2637. if (wc.pin) {
  2638. wc.pin = 0;
  2639. wc.process_func = replay_one_buffer;
  2640. wc.stage = LOG_WALK_REPLAY_INODES;
  2641. goto again;
  2642. }
  2643. /* step three is to replay everything */
  2644. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  2645. wc.stage++;
  2646. goto again;
  2647. }
  2648. btrfs_free_path(path);
  2649. free_extent_buffer(log_root_tree->node);
  2650. log_root_tree->log_root = NULL;
  2651. fs_info->log_root_recovering = 0;
  2652. /* step 4: commit the transaction, which also unpins the blocks */
  2653. btrfs_commit_transaction(trans, fs_info->tree_root);
  2654. kfree(log_root_tree);
  2655. return 0;
  2656. }