tree-log.c 85 KB

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