tree-log.c 76 KB

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