tree-log.c 76 KB

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