tree-log.c 86 KB

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