tree-log.c 85 KB

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