tree-log.c 93 KB

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