tree-log.c 92 KB

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