tree-log.c 76 KB

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