tree-log.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823
  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_reserved_extent(root,
  1537. bytenr, blocksize);
  1538. BUG_ON(ret);
  1539. }
  1540. free_extent_buffer(next);
  1541. continue;
  1542. }
  1543. btrfs_read_buffer(next, ptr_gen);
  1544. WARN_ON(*level <= 0);
  1545. if (path->nodes[*level-1])
  1546. free_extent_buffer(path->nodes[*level-1]);
  1547. path->nodes[*level-1] = next;
  1548. *level = btrfs_header_level(next);
  1549. path->slots[*level] = 0;
  1550. cond_resched();
  1551. }
  1552. WARN_ON(*level < 0);
  1553. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1554. if (path->nodes[*level] == root->node) {
  1555. parent = path->nodes[*level];
  1556. } else {
  1557. parent = path->nodes[*level + 1];
  1558. }
  1559. bytenr = path->nodes[*level]->start;
  1560. blocksize = btrfs_level_size(root, *level);
  1561. root_owner = btrfs_header_owner(parent);
  1562. root_gen = btrfs_header_generation(parent);
  1563. wc->process_func(root, path->nodes[*level], wc,
  1564. btrfs_header_generation(path->nodes[*level]));
  1565. if (wc->free) {
  1566. next = path->nodes[*level];
  1567. btrfs_tree_lock(next);
  1568. clean_tree_block(trans, root, next);
  1569. btrfs_wait_tree_block_writeback(next);
  1570. btrfs_tree_unlock(next);
  1571. if (*level == 0) {
  1572. ret = btrfs_drop_leaf_ref(trans, root, next);
  1573. BUG_ON(ret);
  1574. }
  1575. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  1576. ret = btrfs_free_reserved_extent(root, bytenr, blocksize);
  1577. BUG_ON(ret);
  1578. }
  1579. free_extent_buffer(path->nodes[*level]);
  1580. path->nodes[*level] = NULL;
  1581. *level += 1;
  1582. cond_resched();
  1583. return 0;
  1584. }
  1585. static int noinline walk_up_log_tree(struct btrfs_trans_handle *trans,
  1586. struct btrfs_root *root,
  1587. struct btrfs_path *path, int *level,
  1588. struct walk_control *wc)
  1589. {
  1590. u64 root_owner;
  1591. u64 root_gen;
  1592. int i;
  1593. int slot;
  1594. int ret;
  1595. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1596. slot = path->slots[i];
  1597. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  1598. struct extent_buffer *node;
  1599. node = path->nodes[i];
  1600. path->slots[i]++;
  1601. *level = i;
  1602. WARN_ON(*level == 0);
  1603. return 0;
  1604. } else {
  1605. if (path->nodes[*level] == root->node) {
  1606. root_owner = root->root_key.objectid;
  1607. root_gen =
  1608. btrfs_header_generation(path->nodes[*level]);
  1609. } else {
  1610. struct extent_buffer *node;
  1611. node = path->nodes[*level + 1];
  1612. root_owner = btrfs_header_owner(node);
  1613. root_gen = btrfs_header_generation(node);
  1614. }
  1615. wc->process_func(root, path->nodes[*level], wc,
  1616. btrfs_header_generation(path->nodes[*level]));
  1617. if (wc->free) {
  1618. struct extent_buffer *next;
  1619. next = path->nodes[*level];
  1620. btrfs_tree_lock(next);
  1621. clean_tree_block(trans, root, next);
  1622. btrfs_wait_tree_block_writeback(next);
  1623. btrfs_tree_unlock(next);
  1624. if (*level == 0) {
  1625. ret = btrfs_drop_leaf_ref(trans, root,
  1626. next);
  1627. BUG_ON(ret);
  1628. }
  1629. WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
  1630. ret = btrfs_free_reserved_extent(root,
  1631. path->nodes[*level]->start,
  1632. path->nodes[*level]->len);
  1633. BUG_ON(ret);
  1634. }
  1635. free_extent_buffer(path->nodes[*level]);
  1636. path->nodes[*level] = NULL;
  1637. *level = i + 1;
  1638. }
  1639. }
  1640. return 1;
  1641. }
  1642. /*
  1643. * drop the reference count on the tree rooted at 'snap'. This traverses
  1644. * the tree freeing any blocks that have a ref count of zero after being
  1645. * decremented.
  1646. */
  1647. static int walk_log_tree(struct btrfs_trans_handle *trans,
  1648. struct btrfs_root *log, struct walk_control *wc)
  1649. {
  1650. int ret = 0;
  1651. int wret;
  1652. int level;
  1653. struct btrfs_path *path;
  1654. int i;
  1655. int orig_level;
  1656. path = btrfs_alloc_path();
  1657. BUG_ON(!path);
  1658. level = btrfs_header_level(log->node);
  1659. orig_level = level;
  1660. path->nodes[level] = log->node;
  1661. extent_buffer_get(log->node);
  1662. path->slots[level] = 0;
  1663. while(1) {
  1664. wret = walk_down_log_tree(trans, log, path, &level, wc);
  1665. if (wret > 0)
  1666. break;
  1667. if (wret < 0)
  1668. ret = wret;
  1669. wret = walk_up_log_tree(trans, log, path, &level, wc);
  1670. if (wret > 0)
  1671. break;
  1672. if (wret < 0)
  1673. ret = wret;
  1674. }
  1675. /* was the root node processed? if not, catch it here */
  1676. if (path->nodes[orig_level]) {
  1677. wc->process_func(log, path->nodes[orig_level], wc,
  1678. btrfs_header_generation(path->nodes[orig_level]));
  1679. if (wc->free) {
  1680. struct extent_buffer *next;
  1681. next = path->nodes[orig_level];
  1682. btrfs_tree_lock(next);
  1683. clean_tree_block(trans, log, next);
  1684. btrfs_wait_tree_block_writeback(next);
  1685. btrfs_tree_unlock(next);
  1686. if (orig_level == 0) {
  1687. ret = btrfs_drop_leaf_ref(trans, log,
  1688. next);
  1689. BUG_ON(ret);
  1690. }
  1691. WARN_ON(log->root_key.objectid !=
  1692. BTRFS_TREE_LOG_OBJECTID);
  1693. ret = btrfs_free_reserved_extent(log, next->start,
  1694. next->len);
  1695. BUG_ON(ret);
  1696. }
  1697. }
  1698. for (i = 0; i <= orig_level; i++) {
  1699. if (path->nodes[i]) {
  1700. free_extent_buffer(path->nodes[i]);
  1701. path->nodes[i] = NULL;
  1702. }
  1703. }
  1704. btrfs_free_path(path);
  1705. if (wc->free)
  1706. free_extent_buffer(log->node);
  1707. return ret;
  1708. }
  1709. int wait_log_commit(struct btrfs_root *log)
  1710. {
  1711. DEFINE_WAIT(wait);
  1712. u64 transid = log->fs_info->tree_log_transid;
  1713. do {
  1714. prepare_to_wait(&log->fs_info->tree_log_wait, &wait,
  1715. TASK_UNINTERRUPTIBLE);
  1716. mutex_unlock(&log->fs_info->tree_log_mutex);
  1717. if (atomic_read(&log->fs_info->tree_log_commit))
  1718. schedule();
  1719. finish_wait(&log->fs_info->tree_log_wait, &wait);
  1720. mutex_lock(&log->fs_info->tree_log_mutex);
  1721. } while(transid == log->fs_info->tree_log_transid &&
  1722. atomic_read(&log->fs_info->tree_log_commit));
  1723. return 0;
  1724. }
  1725. /*
  1726. * btrfs_sync_log does sends a given tree log down to the disk and
  1727. * updates the super blocks to record it. When this call is done,
  1728. * you know that any inodes previously logged are safely on disk
  1729. */
  1730. int btrfs_sync_log(struct btrfs_trans_handle *trans,
  1731. struct btrfs_root *root)
  1732. {
  1733. int ret;
  1734. unsigned long batch;
  1735. struct btrfs_root *log = root->log_root;
  1736. struct walk_control wc = {
  1737. .write = 1,
  1738. .process_func = process_one_buffer
  1739. };
  1740. mutex_lock(&log->fs_info->tree_log_mutex);
  1741. if (atomic_read(&log->fs_info->tree_log_commit)) {
  1742. wait_log_commit(log);
  1743. goto out;
  1744. }
  1745. atomic_set(&log->fs_info->tree_log_commit, 1);
  1746. while(1) {
  1747. batch = log->fs_info->tree_log_batch;
  1748. mutex_unlock(&log->fs_info->tree_log_mutex);
  1749. schedule_timeout_uninterruptible(1);
  1750. mutex_lock(&log->fs_info->tree_log_mutex);
  1751. while(atomic_read(&log->fs_info->tree_log_writers)) {
  1752. DEFINE_WAIT(wait);
  1753. prepare_to_wait(&log->fs_info->tree_log_wait, &wait,
  1754. TASK_UNINTERRUPTIBLE);
  1755. mutex_unlock(&log->fs_info->tree_log_mutex);
  1756. if (atomic_read(&log->fs_info->tree_log_writers))
  1757. schedule();
  1758. mutex_lock(&log->fs_info->tree_log_mutex);
  1759. finish_wait(&log->fs_info->tree_log_wait, &wait);
  1760. }
  1761. if (batch == log->fs_info->tree_log_batch)
  1762. break;
  1763. }
  1764. ret = walk_log_tree(trans, log, &wc);
  1765. BUG_ON(ret);
  1766. ret = walk_log_tree(trans, log->fs_info->log_root_tree, &wc);
  1767. BUG_ON(ret);
  1768. wc.wait = 1;
  1769. ret = walk_log_tree(trans, log, &wc);
  1770. BUG_ON(ret);
  1771. ret = walk_log_tree(trans, log->fs_info->log_root_tree, &wc);
  1772. BUG_ON(ret);
  1773. btrfs_set_super_log_root(&root->fs_info->super_for_commit,
  1774. log->fs_info->log_root_tree->node->start);
  1775. btrfs_set_super_log_root_level(&root->fs_info->super_for_commit,
  1776. btrfs_header_level(log->fs_info->log_root_tree->node));
  1777. write_ctree_super(trans, log->fs_info->tree_root);
  1778. log->fs_info->tree_log_transid++;
  1779. log->fs_info->tree_log_batch = 0;
  1780. atomic_set(&log->fs_info->tree_log_commit, 0);
  1781. smp_mb();
  1782. if (waitqueue_active(&log->fs_info->tree_log_wait))
  1783. wake_up(&log->fs_info->tree_log_wait);
  1784. out:
  1785. mutex_unlock(&log->fs_info->tree_log_mutex);
  1786. return 0;
  1787. }
  1788. /* * free all the extents used by the tree log. This should be called
  1789. * at commit time of the full transaction
  1790. */
  1791. int btrfs_free_log(struct btrfs_trans_handle *trans, struct btrfs_root *root)
  1792. {
  1793. int ret;
  1794. struct btrfs_root *log;
  1795. struct key;
  1796. struct walk_control wc = {
  1797. .free = 1,
  1798. .process_func = process_one_buffer
  1799. };
  1800. if (!root->log_root)
  1801. return 0;
  1802. log = root->log_root;
  1803. ret = walk_log_tree(trans, log, &wc);
  1804. BUG_ON(ret);
  1805. log = root->log_root;
  1806. ret = btrfs_del_root(trans, root->fs_info->log_root_tree,
  1807. &log->root_key);
  1808. BUG_ON(ret);
  1809. root->log_root = NULL;
  1810. kfree(root->log_root);
  1811. return 0;
  1812. }
  1813. /*
  1814. * helper function to update the item for a given subvolumes log root
  1815. * in the tree of log roots
  1816. */
  1817. static int update_log_root(struct btrfs_trans_handle *trans,
  1818. struct btrfs_root *log)
  1819. {
  1820. u64 bytenr = btrfs_root_bytenr(&log->root_item);
  1821. int ret;
  1822. if (log->node->start == bytenr)
  1823. return 0;
  1824. btrfs_set_root_bytenr(&log->root_item, log->node->start);
  1825. btrfs_set_root_level(&log->root_item, btrfs_header_level(log->node));
  1826. ret = btrfs_update_root(trans, log->fs_info->log_root_tree,
  1827. &log->root_key, &log->root_item);
  1828. BUG_ON(ret);
  1829. return ret;
  1830. }
  1831. /*
  1832. * If both a file and directory are logged, and unlinks or renames are
  1833. * mixed in, we have a few interesting corners:
  1834. *
  1835. * create file X in dir Y
  1836. * link file X to X.link in dir Y
  1837. * fsync file X
  1838. * unlink file X but leave X.link
  1839. * fsync dir Y
  1840. *
  1841. * After a crash we would expect only X.link to exist. But file X
  1842. * didn't get fsync'd again so the log has back refs for X and X.link.
  1843. *
  1844. * We solve this by removing directory entries and inode backrefs from the
  1845. * log when a file that was logged in the current transaction is
  1846. * unlinked. Any later fsync will include the updated log entries, and
  1847. * we'll be able to reconstruct the proper directory items from backrefs.
  1848. *
  1849. * This optimizations allows us to avoid relogging the entire inode
  1850. * or the entire directory.
  1851. */
  1852. int btrfs_del_dir_entries_in_log(struct btrfs_trans_handle *trans,
  1853. struct btrfs_root *root,
  1854. const char *name, int name_len,
  1855. struct inode *dir, u64 index)
  1856. {
  1857. struct btrfs_root *log;
  1858. struct btrfs_dir_item *di;
  1859. struct btrfs_path *path;
  1860. int ret;
  1861. int bytes_del = 0;
  1862. if (BTRFS_I(dir)->logged_trans < trans->transid)
  1863. return 0;
  1864. ret = join_running_log_trans(root);
  1865. if (ret)
  1866. return 0;
  1867. mutex_lock(&BTRFS_I(dir)->log_mutex);
  1868. log = root->log_root;
  1869. path = btrfs_alloc_path();
  1870. di = btrfs_lookup_dir_item(trans, log, path, dir->i_ino,
  1871. name, name_len, -1);
  1872. if (di && !IS_ERR(di)) {
  1873. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  1874. bytes_del += name_len;
  1875. BUG_ON(ret);
  1876. }
  1877. btrfs_release_path(log, path);
  1878. di = btrfs_lookup_dir_index_item(trans, log, path, dir->i_ino,
  1879. index, name, name_len, -1);
  1880. if (di && !IS_ERR(di)) {
  1881. ret = btrfs_delete_one_dir_name(trans, log, path, di);
  1882. bytes_del += name_len;
  1883. BUG_ON(ret);
  1884. }
  1885. /* update the directory size in the log to reflect the names
  1886. * we have removed
  1887. */
  1888. if (bytes_del) {
  1889. struct btrfs_key key;
  1890. key.objectid = dir->i_ino;
  1891. key.offset = 0;
  1892. key.type = BTRFS_INODE_ITEM_KEY;
  1893. btrfs_release_path(log, path);
  1894. ret = btrfs_search_slot(trans, log, &key, path, 0, 1);
  1895. if (ret == 0) {
  1896. struct btrfs_inode_item *item;
  1897. u64 i_size;
  1898. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1899. struct btrfs_inode_item);
  1900. i_size = btrfs_inode_size(path->nodes[0], item);
  1901. if (i_size > bytes_del)
  1902. i_size -= bytes_del;
  1903. else
  1904. i_size = 0;
  1905. btrfs_set_inode_size(path->nodes[0], item, i_size);
  1906. btrfs_mark_buffer_dirty(path->nodes[0]);
  1907. } else
  1908. ret = 0;
  1909. btrfs_release_path(log, path);
  1910. }
  1911. btrfs_free_path(path);
  1912. mutex_unlock(&BTRFS_I(dir)->log_mutex);
  1913. end_log_trans(root);
  1914. return 0;
  1915. }
  1916. /* see comments for btrfs_del_dir_entries_in_log */
  1917. int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
  1918. struct btrfs_root *root,
  1919. const char *name, int name_len,
  1920. struct inode *inode, u64 dirid)
  1921. {
  1922. struct btrfs_root *log;
  1923. u64 index;
  1924. int ret;
  1925. if (BTRFS_I(inode)->logged_trans < trans->transid)
  1926. return 0;
  1927. ret = join_running_log_trans(root);
  1928. if (ret)
  1929. return 0;
  1930. log = root->log_root;
  1931. mutex_lock(&BTRFS_I(inode)->log_mutex);
  1932. ret = btrfs_del_inode_ref(trans, log, name, name_len, inode->i_ino,
  1933. dirid, &index);
  1934. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  1935. end_log_trans(root);
  1936. return ret;
  1937. }
  1938. /*
  1939. * creates a range item in the log for 'dirid'. first_offset and
  1940. * last_offset tell us which parts of the key space the log should
  1941. * be considered authoritative for.
  1942. */
  1943. static noinline int insert_dir_log_key(struct btrfs_trans_handle *trans,
  1944. struct btrfs_root *log,
  1945. struct btrfs_path *path,
  1946. int key_type, u64 dirid,
  1947. u64 first_offset, u64 last_offset)
  1948. {
  1949. int ret;
  1950. struct btrfs_key key;
  1951. struct btrfs_dir_log_item *item;
  1952. key.objectid = dirid;
  1953. key.offset = first_offset;
  1954. if (key_type == BTRFS_DIR_ITEM_KEY)
  1955. key.type = BTRFS_DIR_LOG_ITEM_KEY;
  1956. else
  1957. key.type = BTRFS_DIR_LOG_INDEX_KEY;
  1958. ret = btrfs_insert_empty_item(trans, log, path, &key, sizeof(*item));
  1959. BUG_ON(ret);
  1960. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1961. struct btrfs_dir_log_item);
  1962. btrfs_set_dir_log_end(path->nodes[0], item, last_offset);
  1963. btrfs_mark_buffer_dirty(path->nodes[0]);
  1964. btrfs_release_path(log, path);
  1965. return 0;
  1966. }
  1967. /*
  1968. * log all the items included in the current transaction for a given
  1969. * directory. This also creates the range items in the log tree required
  1970. * to replay anything deleted before the fsync
  1971. */
  1972. static noinline int log_dir_items(struct btrfs_trans_handle *trans,
  1973. struct btrfs_root *root, struct inode *inode,
  1974. struct btrfs_path *path,
  1975. struct btrfs_path *dst_path, int key_type,
  1976. u64 min_offset, u64 *last_offset_ret)
  1977. {
  1978. struct btrfs_key min_key;
  1979. struct btrfs_key max_key;
  1980. struct btrfs_root *log = root->log_root;
  1981. struct extent_buffer *src;
  1982. int ret;
  1983. int i;
  1984. int nritems;
  1985. u64 first_offset = min_offset;
  1986. u64 last_offset = (u64)-1;
  1987. log = root->log_root;
  1988. max_key.objectid = inode->i_ino;
  1989. max_key.offset = (u64)-1;
  1990. max_key.type = key_type;
  1991. min_key.objectid = inode->i_ino;
  1992. min_key.type = key_type;
  1993. min_key.offset = min_offset;
  1994. path->keep_locks = 1;
  1995. ret = btrfs_search_forward(root, &min_key, &max_key,
  1996. path, 0, trans->transid);
  1997. /*
  1998. * we didn't find anything from this transaction, see if there
  1999. * is anything at all
  2000. */
  2001. if (ret != 0 || min_key.objectid != inode->i_ino ||
  2002. min_key.type != key_type) {
  2003. min_key.objectid = inode->i_ino;
  2004. min_key.type = key_type;
  2005. min_key.offset = (u64)-1;
  2006. btrfs_release_path(root, path);
  2007. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2008. if (ret < 0) {
  2009. btrfs_release_path(root, path);
  2010. return ret;
  2011. }
  2012. ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
  2013. /* if ret == 0 there are items for this type,
  2014. * create a range to tell us the last key of this type.
  2015. * otherwise, there are no items in this directory after
  2016. * *min_offset, and we create a range to indicate that.
  2017. */
  2018. if (ret == 0) {
  2019. struct btrfs_key tmp;
  2020. btrfs_item_key_to_cpu(path->nodes[0], &tmp,
  2021. path->slots[0]);
  2022. if (key_type == tmp.type) {
  2023. first_offset = max(min_offset, tmp.offset) + 1;
  2024. }
  2025. }
  2026. goto done;
  2027. }
  2028. /* go backward to find any previous key */
  2029. ret = btrfs_previous_item(root, path, inode->i_ino, key_type);
  2030. if (ret == 0) {
  2031. struct btrfs_key tmp;
  2032. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2033. if (key_type == tmp.type) {
  2034. first_offset = tmp.offset;
  2035. ret = overwrite_item(trans, log, dst_path,
  2036. path->nodes[0], path->slots[0],
  2037. &tmp);
  2038. }
  2039. }
  2040. btrfs_release_path(root, path);
  2041. /* find the first key from this transaction again */
  2042. ret = btrfs_search_slot(NULL, root, &min_key, path, 0, 0);
  2043. if (ret != 0) {
  2044. WARN_ON(1);
  2045. goto done;
  2046. }
  2047. /*
  2048. * we have a block from this transaction, log every item in it
  2049. * from our directory
  2050. */
  2051. while(1) {
  2052. struct btrfs_key tmp;
  2053. src = path->nodes[0];
  2054. nritems = btrfs_header_nritems(src);
  2055. for (i = path->slots[0]; i < nritems; i++) {
  2056. btrfs_item_key_to_cpu(src, &min_key, i);
  2057. if (min_key.objectid != inode->i_ino ||
  2058. min_key.type != key_type)
  2059. goto done;
  2060. ret = overwrite_item(trans, log, dst_path, src, i,
  2061. &min_key);
  2062. BUG_ON(ret);
  2063. }
  2064. path->slots[0] = nritems;
  2065. /*
  2066. * look ahead to the next item and see if it is also
  2067. * from this directory and from this transaction
  2068. */
  2069. ret = btrfs_next_leaf(root, path);
  2070. if (ret == 1) {
  2071. last_offset = (u64)-1;
  2072. goto done;
  2073. }
  2074. btrfs_item_key_to_cpu(path->nodes[0], &tmp, path->slots[0]);
  2075. if (tmp.objectid != inode->i_ino || tmp.type != key_type) {
  2076. last_offset = (u64)-1;
  2077. goto done;
  2078. }
  2079. if (btrfs_header_generation(path->nodes[0]) != trans->transid) {
  2080. ret = overwrite_item(trans, log, dst_path,
  2081. path->nodes[0], path->slots[0],
  2082. &tmp);
  2083. BUG_ON(ret);
  2084. last_offset = tmp.offset;
  2085. goto done;
  2086. }
  2087. }
  2088. done:
  2089. *last_offset_ret = last_offset;
  2090. btrfs_release_path(root, path);
  2091. btrfs_release_path(log, dst_path);
  2092. /* insert the log range keys to indicate where the log is valid */
  2093. ret = insert_dir_log_key(trans, log, path, key_type, inode->i_ino,
  2094. first_offset, last_offset);
  2095. BUG_ON(ret);
  2096. return 0;
  2097. }
  2098. /*
  2099. * logging directories is very similar to logging inodes, We find all the items
  2100. * from the current transaction and write them to the log.
  2101. *
  2102. * The recovery code scans the directory in the subvolume, and if it finds a
  2103. * key in the range logged that is not present in the log tree, then it means
  2104. * that dir entry was unlinked during the transaction.
  2105. *
  2106. * In order for that scan to work, we must include one key smaller than
  2107. * the smallest logged by this transaction and one key larger than the largest
  2108. * key logged by this transaction.
  2109. */
  2110. static noinline int log_directory_changes(struct btrfs_trans_handle *trans,
  2111. struct btrfs_root *root, struct inode *inode,
  2112. struct btrfs_path *path,
  2113. struct btrfs_path *dst_path)
  2114. {
  2115. u64 min_key;
  2116. u64 max_key;
  2117. int ret;
  2118. int key_type = BTRFS_DIR_ITEM_KEY;
  2119. again:
  2120. min_key = 0;
  2121. max_key = 0;
  2122. while(1) {
  2123. ret = log_dir_items(trans, root, inode, path,
  2124. dst_path, key_type, min_key,
  2125. &max_key);
  2126. BUG_ON(ret);
  2127. if (max_key == (u64)-1)
  2128. break;
  2129. min_key = max_key + 1;
  2130. }
  2131. if (key_type == BTRFS_DIR_ITEM_KEY) {
  2132. key_type = BTRFS_DIR_INDEX_KEY;
  2133. goto again;
  2134. }
  2135. return 0;
  2136. }
  2137. /*
  2138. * a helper function to drop items from the log before we relog an
  2139. * inode. max_key_type indicates the highest item type to remove.
  2140. * This cannot be run for file data extents because it does not
  2141. * free the extents they point to.
  2142. */
  2143. static int drop_objectid_items(struct btrfs_trans_handle *trans,
  2144. struct btrfs_root *log,
  2145. struct btrfs_path *path,
  2146. u64 objectid, int max_key_type)
  2147. {
  2148. int ret;
  2149. struct btrfs_key key;
  2150. struct btrfs_key found_key;
  2151. key.objectid = objectid;
  2152. key.type = max_key_type;
  2153. key.offset = (u64)-1;
  2154. while(1) {
  2155. ret = btrfs_search_slot(trans, log, &key, path, -1, 1);
  2156. if (ret != 1)
  2157. break;
  2158. if (path->slots[0] == 0)
  2159. break;
  2160. path->slots[0]--;
  2161. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2162. path->slots[0]);
  2163. if (found_key.objectid != objectid)
  2164. break;
  2165. ret = btrfs_del_item(trans, log, path);
  2166. BUG_ON(ret);
  2167. btrfs_release_path(log, path);
  2168. }
  2169. btrfs_release_path(log, path);
  2170. return 0;
  2171. }
  2172. /* log a single inode in the tree log.
  2173. * At least one parent directory for this inode must exist in the tree
  2174. * or be logged already.
  2175. *
  2176. * Any items from this inode changed by the current transaction are copied
  2177. * to the log tree. An extra reference is taken on any extents in this
  2178. * file, allowing us to avoid a whole pile of corner cases around logging
  2179. * blocks that have been removed from the tree.
  2180. *
  2181. * See LOG_INODE_ALL and related defines for a description of what inode_only
  2182. * does.
  2183. *
  2184. * This handles both files and directories.
  2185. */
  2186. static int __btrfs_log_inode(struct btrfs_trans_handle *trans,
  2187. struct btrfs_root *root, struct inode *inode,
  2188. int inode_only)
  2189. {
  2190. struct btrfs_path *path;
  2191. struct btrfs_path *dst_path;
  2192. struct btrfs_key min_key;
  2193. struct btrfs_key max_key;
  2194. struct btrfs_root *log = root->log_root;
  2195. unsigned long src_offset;
  2196. unsigned long dst_offset;
  2197. struct extent_buffer *src;
  2198. struct btrfs_file_extent_item *extent;
  2199. struct btrfs_inode_item *inode_item;
  2200. u32 size;
  2201. int ret;
  2202. int nritems;
  2203. log = root->log_root;
  2204. path = btrfs_alloc_path();
  2205. dst_path = btrfs_alloc_path();
  2206. min_key.objectid = inode->i_ino;
  2207. min_key.type = BTRFS_INODE_ITEM_KEY;
  2208. min_key.offset = 0;
  2209. max_key.objectid = inode->i_ino;
  2210. if (inode_only == LOG_INODE_EXISTS || S_ISDIR(inode->i_mode))
  2211. max_key.type = BTRFS_XATTR_ITEM_KEY;
  2212. else
  2213. max_key.type = (u8)-1;
  2214. max_key.offset = (u64)-1;
  2215. /*
  2216. * if this inode has already been logged and we're in inode_only
  2217. * mode, we don't want to delete the things that have already
  2218. * been written to the log.
  2219. *
  2220. * But, if the inode has been through an inode_only log,
  2221. * the logged_trans field is not set. This allows us to catch
  2222. * any new names for this inode in the backrefs by logging it
  2223. * again
  2224. */
  2225. if (inode_only == LOG_INODE_EXISTS &&
  2226. BTRFS_I(inode)->logged_trans == trans->transid) {
  2227. btrfs_free_path(path);
  2228. btrfs_free_path(dst_path);
  2229. goto out;
  2230. }
  2231. mutex_lock(&BTRFS_I(inode)->log_mutex);
  2232. /*
  2233. * a brute force approach to making sure we get the most uptodate
  2234. * copies of everything.
  2235. */
  2236. if (S_ISDIR(inode->i_mode)) {
  2237. int max_key_type = BTRFS_DIR_LOG_INDEX_KEY;
  2238. if (inode_only == LOG_INODE_EXISTS)
  2239. max_key_type = BTRFS_XATTR_ITEM_KEY;
  2240. ret = drop_objectid_items(trans, log, path,
  2241. inode->i_ino, max_key_type);
  2242. } else {
  2243. ret = btrfs_truncate_inode_items(trans, log, inode, 0, 0);
  2244. }
  2245. BUG_ON(ret);
  2246. path->keep_locks = 1;
  2247. while(1) {
  2248. ret = btrfs_search_forward(root, &min_key, &max_key,
  2249. path, 0, trans->transid);
  2250. if (ret != 0)
  2251. break;
  2252. again:
  2253. if (min_key.objectid != inode->i_ino)
  2254. break;
  2255. if (min_key.type > max_key.type)
  2256. break;
  2257. src = path->nodes[0];
  2258. size = btrfs_item_size_nr(src, path->slots[0]);
  2259. ret = btrfs_insert_empty_item(trans, log, dst_path, &min_key,
  2260. size);
  2261. if (ret)
  2262. BUG();
  2263. dst_offset = btrfs_item_ptr_offset(dst_path->nodes[0],
  2264. dst_path->slots[0]);
  2265. src_offset = btrfs_item_ptr_offset(src, path->slots[0]);
  2266. copy_extent_buffer(dst_path->nodes[0], src, dst_offset,
  2267. src_offset, size);
  2268. if (inode_only == LOG_INODE_EXISTS &&
  2269. min_key.type == BTRFS_INODE_ITEM_KEY) {
  2270. inode_item = btrfs_item_ptr(dst_path->nodes[0],
  2271. dst_path->slots[0],
  2272. struct btrfs_inode_item);
  2273. btrfs_set_inode_size(dst_path->nodes[0], inode_item, 0);
  2274. /* set the generation to zero so the recover code
  2275. * can tell the difference between an logging
  2276. * just to say 'this inode exists' and a logging
  2277. * to say 'update this inode with these values'
  2278. */
  2279. btrfs_set_inode_generation(dst_path->nodes[0],
  2280. inode_item, 0);
  2281. }
  2282. /* take a reference on file data extents so that truncates
  2283. * or deletes of this inode don't have to relog the inode
  2284. * again
  2285. */
  2286. if (btrfs_key_type(&min_key) == BTRFS_EXTENT_DATA_KEY) {
  2287. int found_type;
  2288. extent = btrfs_item_ptr(src, path->slots[0],
  2289. struct btrfs_file_extent_item);
  2290. found_type = btrfs_file_extent_type(src, extent);
  2291. if (found_type == BTRFS_FILE_EXTENT_REG) {
  2292. u64 ds = btrfs_file_extent_disk_bytenr(src,
  2293. extent);
  2294. u64 dl = btrfs_file_extent_disk_num_bytes(src,
  2295. extent);
  2296. /* ds == 0 is a hole */
  2297. if (ds != 0) {
  2298. ret = btrfs_inc_extent_ref(trans, log,
  2299. ds, dl,
  2300. log->root_key.objectid,
  2301. 0,
  2302. inode->i_ino,
  2303. min_key.offset);
  2304. BUG_ON(ret);
  2305. }
  2306. }
  2307. }
  2308. btrfs_mark_buffer_dirty(dst_path->nodes[0]);
  2309. btrfs_release_path(log, dst_path);
  2310. nritems = btrfs_header_nritems(path->nodes[0]);
  2311. path->slots[0]++;
  2312. if (path->slots[0] < nritems) {
  2313. btrfs_item_key_to_cpu(path->nodes[0], &min_key,
  2314. path->slots[0]);
  2315. goto again;
  2316. }
  2317. btrfs_release_path(root, path);
  2318. if (min_key.offset < (u64)-1)
  2319. min_key.offset++;
  2320. else if (min_key.type < (u8)-1)
  2321. min_key.type++;
  2322. else if (min_key.objectid < (u64)-1)
  2323. min_key.objectid++;
  2324. else
  2325. break;
  2326. }
  2327. if (inode_only == LOG_INODE_ALL && S_ISDIR(inode->i_mode) &&
  2328. BTRFS_I(inode)->log_dirty_trans >= trans->transid) {
  2329. btrfs_release_path(root, path);
  2330. btrfs_release_path(log, dst_path);
  2331. BTRFS_I(inode)->log_dirty_trans = 0;
  2332. ret = log_directory_changes(trans, root, inode, path, dst_path);
  2333. BUG_ON(ret);
  2334. }
  2335. BTRFS_I(inode)->logged_trans = trans->transid;
  2336. mutex_unlock(&BTRFS_I(inode)->log_mutex);
  2337. btrfs_free_path(path);
  2338. btrfs_free_path(dst_path);
  2339. mutex_lock(&root->fs_info->tree_log_mutex);
  2340. ret = update_log_root(trans, log);
  2341. BUG_ON(ret);
  2342. mutex_unlock(&root->fs_info->tree_log_mutex);
  2343. out:
  2344. return 0;
  2345. }
  2346. int btrfs_log_inode(struct btrfs_trans_handle *trans,
  2347. struct btrfs_root *root, struct inode *inode,
  2348. int inode_only)
  2349. {
  2350. int ret;
  2351. start_log_trans(trans, root);
  2352. ret = __btrfs_log_inode(trans, root, inode, inode_only);
  2353. end_log_trans(root);
  2354. return ret;
  2355. }
  2356. /*
  2357. * helper function around btrfs_log_inode to make sure newly created
  2358. * parent directories also end up in the log. A minimal inode and backref
  2359. * only logging is done of any parent directories that are older than
  2360. * the last committed transaction
  2361. */
  2362. int btrfs_log_dentry(struct btrfs_trans_handle *trans,
  2363. struct btrfs_root *root, struct dentry *dentry)
  2364. {
  2365. int inode_only = LOG_INODE_ALL;
  2366. struct super_block *sb;
  2367. int ret;
  2368. start_log_trans(trans, root);
  2369. sb = dentry->d_inode->i_sb;
  2370. while(1) {
  2371. ret = __btrfs_log_inode(trans, root, dentry->d_inode,
  2372. inode_only);
  2373. BUG_ON(ret);
  2374. inode_only = LOG_INODE_EXISTS;
  2375. dentry = dentry->d_parent;
  2376. if (!dentry || !dentry->d_inode || sb != dentry->d_inode->i_sb)
  2377. break;
  2378. if (BTRFS_I(dentry->d_inode)->generation <=
  2379. root->fs_info->last_trans_committed)
  2380. break;
  2381. }
  2382. end_log_trans(root);
  2383. return 0;
  2384. }
  2385. /*
  2386. * it is not safe to log dentry if the chunk root has added new
  2387. * chunks. This returns 0 if the dentry was logged, and 1 otherwise.
  2388. * If this returns 1, you must commit the transaction to safely get your
  2389. * data on disk.
  2390. */
  2391. int btrfs_log_dentry_safe(struct btrfs_trans_handle *trans,
  2392. struct btrfs_root *root, struct dentry *dentry)
  2393. {
  2394. u64 gen;
  2395. gen = root->fs_info->last_trans_new_blockgroup;
  2396. if (gen > root->fs_info->last_trans_committed)
  2397. return 1;
  2398. else
  2399. return btrfs_log_dentry(trans, root, dentry);
  2400. }
  2401. /*
  2402. * should be called during mount to recover any replay any log trees
  2403. * from the FS
  2404. */
  2405. int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
  2406. {
  2407. int ret;
  2408. struct btrfs_path *path;
  2409. struct btrfs_trans_handle *trans;
  2410. struct btrfs_key key;
  2411. struct btrfs_key found_key;
  2412. struct btrfs_key tmp_key;
  2413. struct btrfs_root *log;
  2414. struct btrfs_fs_info *fs_info = log_root_tree->fs_info;
  2415. u64 highest_inode;
  2416. struct walk_control wc = {
  2417. .process_func = process_one_buffer,
  2418. .stage = 0,
  2419. };
  2420. fs_info->log_root_recovering = 1;
  2421. path = btrfs_alloc_path();
  2422. BUG_ON(!path);
  2423. trans = btrfs_start_transaction(fs_info->tree_root, 1);
  2424. wc.trans = trans;
  2425. wc.pin = 1;
  2426. walk_log_tree(trans, log_root_tree, &wc);
  2427. again:
  2428. key.objectid = BTRFS_TREE_LOG_OBJECTID;
  2429. key.offset = (u64)-1;
  2430. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  2431. while(1) {
  2432. ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
  2433. if (ret < 0)
  2434. break;
  2435. if (ret > 0) {
  2436. if (path->slots[0] == 0)
  2437. break;
  2438. path->slots[0]--;
  2439. }
  2440. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  2441. path->slots[0]);
  2442. btrfs_release_path(log_root_tree, path);
  2443. if (found_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  2444. break;
  2445. log = btrfs_read_fs_root_no_radix(log_root_tree,
  2446. &found_key);
  2447. BUG_ON(!log);
  2448. tmp_key.objectid = found_key.offset;
  2449. tmp_key.type = BTRFS_ROOT_ITEM_KEY;
  2450. tmp_key.offset = (u64)-1;
  2451. wc.replay_dest = btrfs_read_fs_root_no_name(fs_info, &tmp_key);
  2452. BUG_ON(!wc.replay_dest);
  2453. btrfs_record_root_in_trans(wc.replay_dest);
  2454. ret = walk_log_tree(trans, log, &wc);
  2455. BUG_ON(ret);
  2456. if (wc.stage == LOG_WALK_REPLAY_ALL) {
  2457. ret = fixup_inode_link_counts(trans, wc.replay_dest,
  2458. path);
  2459. BUG_ON(ret);
  2460. }
  2461. ret = btrfs_find_highest_inode(wc.replay_dest, &highest_inode);
  2462. if (ret == 0) {
  2463. wc.replay_dest->highest_inode = highest_inode;
  2464. wc.replay_dest->last_inode_alloc = highest_inode;
  2465. }
  2466. key.offset = found_key.offset - 1;
  2467. free_extent_buffer(log->node);
  2468. kfree(log);
  2469. if (found_key.offset == 0)
  2470. break;
  2471. }
  2472. btrfs_release_path(log_root_tree, path);
  2473. /* step one is to pin it all, step two is to replay just inodes */
  2474. if (wc.pin) {
  2475. wc.pin = 0;
  2476. wc.process_func = replay_one_buffer;
  2477. wc.stage = LOG_WALK_REPLAY_INODES;
  2478. goto again;
  2479. }
  2480. /* step three is to replay everything */
  2481. if (wc.stage < LOG_WALK_REPLAY_ALL) {
  2482. wc.stage++;
  2483. goto again;
  2484. }
  2485. btrfs_free_path(path);
  2486. free_extent_buffer(log_root_tree->node);
  2487. log_root_tree->log_root = NULL;
  2488. fs_info->log_root_recovering = 0;
  2489. /* step 4: commit the transaction, which also unpins the blocks */
  2490. btrfs_commit_transaction(trans, fs_info->tree_root);
  2491. kfree(log_root_tree);
  2492. return 0;
  2493. }