tree-log.c 84 KB

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