tree-log.c 84 KB

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