tree-log.c 84 KB

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