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