tree-log.c 74 KB

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