transaction.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. * Copyright (C) 2007 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/fs.h>
  19. #include <linux/sched.h>
  20. #include <linux/writeback.h>
  21. #include <linux/pagemap.h>
  22. #include "ctree.h"
  23. #include "disk-io.h"
  24. #include "transaction.h"
  25. #include "locking.h"
  26. static int total_trans = 0;
  27. extern struct kmem_cache *btrfs_trans_handle_cachep;
  28. extern struct kmem_cache *btrfs_transaction_cachep;
  29. #define BTRFS_ROOT_TRANS_TAG 0
  30. #define BTRFS_ROOT_DEFRAG_TAG 1
  31. static noinline void put_transaction(struct btrfs_transaction *transaction)
  32. {
  33. WARN_ON(transaction->use_count == 0);
  34. transaction->use_count--;
  35. if (transaction->use_count == 0) {
  36. WARN_ON(total_trans == 0);
  37. total_trans--;
  38. list_del_init(&transaction->list);
  39. memset(transaction, 0, sizeof(*transaction));
  40. kmem_cache_free(btrfs_transaction_cachep, transaction);
  41. }
  42. }
  43. static noinline int join_transaction(struct btrfs_root *root)
  44. {
  45. struct btrfs_transaction *cur_trans;
  46. cur_trans = root->fs_info->running_transaction;
  47. if (!cur_trans) {
  48. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  49. GFP_NOFS);
  50. total_trans++;
  51. BUG_ON(!cur_trans);
  52. root->fs_info->generation++;
  53. root->fs_info->last_alloc = 0;
  54. root->fs_info->last_data_alloc = 0;
  55. cur_trans->num_writers = 1;
  56. cur_trans->num_joined = 0;
  57. cur_trans->transid = root->fs_info->generation;
  58. init_waitqueue_head(&cur_trans->writer_wait);
  59. init_waitqueue_head(&cur_trans->commit_wait);
  60. cur_trans->in_commit = 0;
  61. cur_trans->use_count = 1;
  62. cur_trans->commit_done = 0;
  63. cur_trans->start_time = get_seconds();
  64. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  65. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  66. btrfs_ordered_inode_tree_init(&cur_trans->ordered_inode_tree);
  67. extent_io_tree_init(&cur_trans->dirty_pages,
  68. root->fs_info->btree_inode->i_mapping,
  69. GFP_NOFS);
  70. spin_lock(&root->fs_info->new_trans_lock);
  71. root->fs_info->running_transaction = cur_trans;
  72. spin_unlock(&root->fs_info->new_trans_lock);
  73. } else {
  74. cur_trans->num_writers++;
  75. cur_trans->num_joined++;
  76. }
  77. return 0;
  78. }
  79. static noinline int record_root_in_trans(struct btrfs_root *root)
  80. {
  81. u64 running_trans_id = root->fs_info->running_transaction->transid;
  82. if (root->ref_cows && root->last_trans < running_trans_id) {
  83. WARN_ON(root == root->fs_info->extent_root);
  84. if (root->root_item.refs != 0) {
  85. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  86. (unsigned long)root->root_key.objectid,
  87. BTRFS_ROOT_TRANS_TAG);
  88. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  89. (unsigned long)root->root_key.objectid,
  90. BTRFS_ROOT_DEFRAG_TAG);
  91. root->commit_root = btrfs_root_node(root);
  92. } else {
  93. WARN_ON(1);
  94. }
  95. root->last_trans = running_trans_id;
  96. }
  97. return 0;
  98. }
  99. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  100. int num_blocks)
  101. {
  102. struct btrfs_trans_handle *h =
  103. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  104. int ret;
  105. mutex_lock(&root->fs_info->trans_mutex);
  106. ret = join_transaction(root);
  107. BUG_ON(ret);
  108. record_root_in_trans(root);
  109. h->transid = root->fs_info->running_transaction->transid;
  110. h->transaction = root->fs_info->running_transaction;
  111. h->blocks_reserved = num_blocks;
  112. h->blocks_used = 0;
  113. h->block_group = NULL;
  114. h->alloc_exclude_nr = 0;
  115. h->alloc_exclude_start = 0;
  116. root->fs_info->running_transaction->use_count++;
  117. mutex_unlock(&root->fs_info->trans_mutex);
  118. return h;
  119. }
  120. static noinline int wait_for_commit(struct btrfs_root *root,
  121. struct btrfs_transaction *commit)
  122. {
  123. DEFINE_WAIT(wait);
  124. mutex_lock(&root->fs_info->trans_mutex);
  125. while(!commit->commit_done) {
  126. prepare_to_wait(&commit->commit_wait, &wait,
  127. TASK_UNINTERRUPTIBLE);
  128. if (commit->commit_done)
  129. break;
  130. mutex_unlock(&root->fs_info->trans_mutex);
  131. schedule();
  132. mutex_lock(&root->fs_info->trans_mutex);
  133. }
  134. mutex_unlock(&root->fs_info->trans_mutex);
  135. finish_wait(&commit->commit_wait, &wait);
  136. return 0;
  137. }
  138. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  139. struct btrfs_root *root, int throttle)
  140. {
  141. struct btrfs_transaction *cur_trans;
  142. mutex_lock(&root->fs_info->trans_mutex);
  143. cur_trans = root->fs_info->running_transaction;
  144. WARN_ON(cur_trans != trans->transaction);
  145. WARN_ON(cur_trans->num_writers < 1);
  146. cur_trans->num_writers--;
  147. if (waitqueue_active(&cur_trans->writer_wait))
  148. wake_up(&cur_trans->writer_wait);
  149. if (cur_trans->in_commit && throttle) {
  150. int ret;
  151. mutex_unlock(&root->fs_info->trans_mutex);
  152. ret = wait_for_commit(root, cur_trans);
  153. BUG_ON(ret);
  154. mutex_lock(&root->fs_info->trans_mutex);
  155. }
  156. put_transaction(cur_trans);
  157. mutex_unlock(&root->fs_info->trans_mutex);
  158. memset(trans, 0, sizeof(*trans));
  159. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  160. return 0;
  161. }
  162. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  163. struct btrfs_root *root)
  164. {
  165. return __btrfs_end_transaction(trans, root, 0);
  166. }
  167. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  168. struct btrfs_root *root)
  169. {
  170. return __btrfs_end_transaction(trans, root, 1);
  171. }
  172. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  173. struct btrfs_root *root)
  174. {
  175. int ret;
  176. int err;
  177. int werr = 0;
  178. struct extent_io_tree *dirty_pages;
  179. struct page *page;
  180. struct inode *btree_inode = root->fs_info->btree_inode;
  181. u64 start;
  182. u64 end;
  183. unsigned long index;
  184. if (!trans || !trans->transaction) {
  185. return filemap_write_and_wait(btree_inode->i_mapping);
  186. }
  187. dirty_pages = &trans->transaction->dirty_pages;
  188. while(1) {
  189. ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
  190. EXTENT_DIRTY);
  191. if (ret)
  192. break;
  193. clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
  194. while(start <= end) {
  195. index = start >> PAGE_CACHE_SHIFT;
  196. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  197. page = find_lock_page(btree_inode->i_mapping, index);
  198. if (!page)
  199. continue;
  200. if (PageWriteback(page)) {
  201. if (PageDirty(page))
  202. wait_on_page_writeback(page);
  203. else {
  204. unlock_page(page);
  205. page_cache_release(page);
  206. continue;
  207. }
  208. }
  209. err = write_one_page(page, 0);
  210. if (err)
  211. werr = err;
  212. page_cache_release(page);
  213. }
  214. }
  215. err = filemap_fdatawait(btree_inode->i_mapping);
  216. if (err)
  217. werr = err;
  218. return werr;
  219. }
  220. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  221. struct btrfs_root *root)
  222. {
  223. int ret;
  224. u64 old_root_bytenr;
  225. struct btrfs_root *tree_root = root->fs_info->tree_root;
  226. btrfs_write_dirty_block_groups(trans, root);
  227. while(1) {
  228. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  229. if (old_root_bytenr == root->node->start)
  230. break;
  231. btrfs_set_root_bytenr(&root->root_item,
  232. root->node->start);
  233. btrfs_set_root_level(&root->root_item,
  234. btrfs_header_level(root->node));
  235. ret = btrfs_update_root(trans, tree_root,
  236. &root->root_key,
  237. &root->root_item);
  238. BUG_ON(ret);
  239. btrfs_write_dirty_block_groups(trans, root);
  240. }
  241. return 0;
  242. }
  243. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  244. struct btrfs_root *root)
  245. {
  246. struct btrfs_fs_info *fs_info = root->fs_info;
  247. struct list_head *next;
  248. while(!list_empty(&fs_info->dirty_cowonly_roots)) {
  249. next = fs_info->dirty_cowonly_roots.next;
  250. list_del_init(next);
  251. root = list_entry(next, struct btrfs_root, dirty_list);
  252. update_cowonly_root(trans, root);
  253. }
  254. return 0;
  255. }
  256. struct dirty_root {
  257. struct list_head list;
  258. struct btrfs_root *root;
  259. struct btrfs_root *latest_root;
  260. };
  261. int btrfs_add_dead_root(struct btrfs_root *root,
  262. struct btrfs_root *latest,
  263. struct list_head *dead_list)
  264. {
  265. struct dirty_root *dirty;
  266. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  267. if (!dirty)
  268. return -ENOMEM;
  269. dirty->root = root;
  270. dirty->latest_root = latest;
  271. list_add(&dirty->list, dead_list);
  272. return 0;
  273. }
  274. static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
  275. struct radix_tree_root *radix,
  276. struct list_head *list)
  277. {
  278. struct dirty_root *dirty;
  279. struct btrfs_root *gang[8];
  280. struct btrfs_root *root;
  281. int i;
  282. int ret;
  283. int err = 0;
  284. u32 refs;
  285. while(1) {
  286. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  287. ARRAY_SIZE(gang),
  288. BTRFS_ROOT_TRANS_TAG);
  289. if (ret == 0)
  290. break;
  291. for (i = 0; i < ret; i++) {
  292. root = gang[i];
  293. radix_tree_tag_clear(radix,
  294. (unsigned long)root->root_key.objectid,
  295. BTRFS_ROOT_TRANS_TAG);
  296. if (root->commit_root == root->node) {
  297. WARN_ON(root->node->start !=
  298. btrfs_root_bytenr(&root->root_item));
  299. free_extent_buffer(root->commit_root);
  300. root->commit_root = NULL;
  301. /* make sure to update the root on disk
  302. * so we get any updates to the block used
  303. * counts
  304. */
  305. err = btrfs_update_root(trans,
  306. root->fs_info->tree_root,
  307. &root->root_key,
  308. &root->root_item);
  309. continue;
  310. }
  311. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  312. BUG_ON(!dirty);
  313. dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
  314. BUG_ON(!dirty->root);
  315. memset(&root->root_item.drop_progress, 0,
  316. sizeof(struct btrfs_disk_key));
  317. root->root_item.drop_level = 0;
  318. memcpy(dirty->root, root, sizeof(*root));
  319. dirty->root->node = root->commit_root;
  320. dirty->latest_root = root;
  321. root->commit_root = NULL;
  322. root->root_key.offset = root->fs_info->generation;
  323. btrfs_set_root_bytenr(&root->root_item,
  324. root->node->start);
  325. btrfs_set_root_level(&root->root_item,
  326. btrfs_header_level(root->node));
  327. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  328. &root->root_key,
  329. &root->root_item);
  330. if (err)
  331. break;
  332. refs = btrfs_root_refs(&dirty->root->root_item);
  333. btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
  334. err = btrfs_update_root(trans, root->fs_info->tree_root,
  335. &dirty->root->root_key,
  336. &dirty->root->root_item);
  337. BUG_ON(err);
  338. if (refs == 1) {
  339. list_add(&dirty->list, list);
  340. } else {
  341. WARN_ON(1);
  342. kfree(dirty->root);
  343. kfree(dirty);
  344. }
  345. }
  346. }
  347. return err;
  348. }
  349. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  350. {
  351. struct btrfs_fs_info *info = root->fs_info;
  352. int ret;
  353. struct btrfs_trans_handle *trans;
  354. unsigned long nr;
  355. smp_mb();
  356. if (root->defrag_running)
  357. return 0;
  358. trans = btrfs_start_transaction(root, 1);
  359. while (1) {
  360. root->defrag_running = 1;
  361. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  362. nr = trans->blocks_used;
  363. btrfs_end_transaction(trans, root);
  364. btrfs_btree_balance_dirty(info->tree_root, nr);
  365. cond_resched();
  366. trans = btrfs_start_transaction(root, 1);
  367. if (ret != -EAGAIN)
  368. break;
  369. }
  370. root->defrag_running = 0;
  371. smp_mb();
  372. radix_tree_tag_clear(&info->fs_roots_radix,
  373. (unsigned long)root->root_key.objectid,
  374. BTRFS_ROOT_DEFRAG_TAG);
  375. btrfs_end_transaction(trans, root);
  376. return 0;
  377. }
  378. int btrfs_defrag_dirty_roots(struct btrfs_fs_info *info)
  379. {
  380. struct btrfs_root *gang[1];
  381. struct btrfs_root *root;
  382. int i;
  383. int ret;
  384. int err = 0;
  385. u64 last = 0;
  386. while(1) {
  387. ret = radix_tree_gang_lookup_tag(&info->fs_roots_radix,
  388. (void **)gang, last,
  389. ARRAY_SIZE(gang),
  390. BTRFS_ROOT_DEFRAG_TAG);
  391. if (ret == 0)
  392. break;
  393. for (i = 0; i < ret; i++) {
  394. root = gang[i];
  395. last = root->root_key.objectid + 1;
  396. btrfs_defrag_root(root, 1);
  397. }
  398. }
  399. btrfs_defrag_root(info->extent_root, 1);
  400. return err;
  401. }
  402. static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
  403. struct list_head *list)
  404. {
  405. struct dirty_root *dirty;
  406. struct btrfs_trans_handle *trans;
  407. unsigned long nr;
  408. u64 num_bytes;
  409. u64 bytes_used;
  410. int ret = 0;
  411. int err;
  412. while(!list_empty(list)) {
  413. struct btrfs_root *root;
  414. dirty = list_entry(list->next, struct dirty_root, list);
  415. list_del_init(&dirty->list);
  416. num_bytes = btrfs_root_used(&dirty->root->root_item);
  417. root = dirty->latest_root;
  418. atomic_inc(&root->fs_info->throttles);
  419. mutex_lock(&root->fs_info->drop_mutex);
  420. while(1) {
  421. trans = btrfs_start_transaction(tree_root, 1);
  422. ret = btrfs_drop_snapshot(trans, dirty->root);
  423. if (ret != -EAGAIN) {
  424. break;
  425. }
  426. err = btrfs_update_root(trans,
  427. tree_root,
  428. &dirty->root->root_key,
  429. &dirty->root->root_item);
  430. if (err)
  431. ret = err;
  432. nr = trans->blocks_used;
  433. ret = btrfs_end_transaction_throttle(trans, tree_root);
  434. BUG_ON(ret);
  435. mutex_unlock(&root->fs_info->drop_mutex);
  436. btrfs_btree_balance_dirty(tree_root, nr);
  437. cond_resched();
  438. mutex_lock(&root->fs_info->drop_mutex);
  439. }
  440. BUG_ON(ret);
  441. atomic_dec(&root->fs_info->throttles);
  442. mutex_lock(&root->fs_info->alloc_mutex);
  443. num_bytes -= btrfs_root_used(&dirty->root->root_item);
  444. bytes_used = btrfs_root_used(&root->root_item);
  445. if (num_bytes) {
  446. record_root_in_trans(root);
  447. btrfs_set_root_used(&root->root_item,
  448. bytes_used - num_bytes);
  449. }
  450. mutex_unlock(&root->fs_info->alloc_mutex);
  451. ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
  452. if (ret) {
  453. BUG();
  454. break;
  455. }
  456. mutex_unlock(&root->fs_info->drop_mutex);
  457. nr = trans->blocks_used;
  458. ret = btrfs_end_transaction(trans, tree_root);
  459. BUG_ON(ret);
  460. free_extent_buffer(dirty->root->node);
  461. kfree(dirty->root);
  462. kfree(dirty);
  463. btrfs_btree_balance_dirty(tree_root, nr);
  464. cond_resched();
  465. }
  466. return ret;
  467. }
  468. int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
  469. struct btrfs_root *root)
  470. {
  471. struct btrfs_transaction *cur_trans = trans->transaction;
  472. struct inode *inode;
  473. u64 root_objectid = 0;
  474. u64 objectid = 0;
  475. int ret;
  476. atomic_inc(&root->fs_info->throttles);
  477. while(1) {
  478. ret = btrfs_find_first_ordered_inode(
  479. &cur_trans->ordered_inode_tree,
  480. &root_objectid, &objectid, &inode);
  481. if (!ret)
  482. break;
  483. mutex_unlock(&root->fs_info->trans_mutex);
  484. if (S_ISREG(inode->i_mode)) {
  485. atomic_inc(&BTRFS_I(inode)->ordered_writeback);
  486. filemap_fdatawrite(inode->i_mapping);
  487. atomic_dec(&BTRFS_I(inode)->ordered_writeback);
  488. }
  489. iput(inode);
  490. mutex_lock(&root->fs_info->trans_mutex);
  491. }
  492. while(1) {
  493. root_objectid = 0;
  494. objectid = 0;
  495. ret = btrfs_find_del_first_ordered_inode(
  496. &cur_trans->ordered_inode_tree,
  497. &root_objectid, &objectid, &inode);
  498. if (!ret)
  499. break;
  500. mutex_unlock(&root->fs_info->trans_mutex);
  501. if (S_ISREG(inode->i_mode)) {
  502. atomic_inc(&BTRFS_I(inode)->ordered_writeback);
  503. filemap_write_and_wait(inode->i_mapping);
  504. atomic_dec(&BTRFS_I(inode)->ordered_writeback);
  505. }
  506. atomic_dec(&inode->i_count);
  507. iput(inode);
  508. mutex_lock(&root->fs_info->trans_mutex);
  509. }
  510. atomic_dec(&root->fs_info->throttles);
  511. return 0;
  512. }
  513. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  514. struct btrfs_fs_info *fs_info,
  515. struct btrfs_pending_snapshot *pending)
  516. {
  517. struct btrfs_key key;
  518. struct btrfs_root_item *new_root_item;
  519. struct btrfs_root *tree_root = fs_info->tree_root;
  520. struct btrfs_root *root = pending->root;
  521. struct extent_buffer *tmp;
  522. struct extent_buffer *old;
  523. int ret;
  524. int namelen;
  525. u64 objectid;
  526. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  527. if (!new_root_item) {
  528. ret = -ENOMEM;
  529. goto fail;
  530. }
  531. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  532. if (ret)
  533. goto fail;
  534. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  535. key.objectid = objectid;
  536. key.offset = 1;
  537. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  538. old = btrfs_lock_root_node(root);
  539. btrfs_cow_block(trans, root, old, NULL, 0, &old);
  540. btrfs_copy_root(trans, root, old, &tmp, objectid);
  541. btrfs_tree_unlock(old);
  542. free_extent_buffer(old);
  543. btrfs_set_root_bytenr(new_root_item, tmp->start);
  544. btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
  545. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  546. new_root_item);
  547. btrfs_tree_unlock(tmp);
  548. free_extent_buffer(tmp);
  549. if (ret)
  550. goto fail;
  551. /*
  552. * insert the directory item
  553. */
  554. key.offset = (u64)-1;
  555. namelen = strlen(pending->name);
  556. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  557. pending->name, namelen,
  558. root->fs_info->sb->s_root->d_inode->i_ino,
  559. &key, BTRFS_FT_DIR);
  560. if (ret)
  561. goto fail;
  562. ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
  563. pending->name, strlen(pending->name), objectid,
  564. root->fs_info->sb->s_root->d_inode->i_ino);
  565. /* Invalidate existing dcache entry for new snapshot. */
  566. btrfs_invalidate_dcache_root(root, pending->name, namelen);
  567. fail:
  568. kfree(new_root_item);
  569. return ret;
  570. }
  571. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  572. struct btrfs_fs_info *fs_info)
  573. {
  574. struct btrfs_pending_snapshot *pending;
  575. struct list_head *head = &trans->transaction->pending_snapshots;
  576. int ret;
  577. while(!list_empty(head)) {
  578. pending = list_entry(head->next,
  579. struct btrfs_pending_snapshot, list);
  580. ret = create_pending_snapshot(trans, fs_info, pending);
  581. BUG_ON(ret);
  582. list_del(&pending->list);
  583. kfree(pending->name);
  584. kfree(pending);
  585. }
  586. return 0;
  587. }
  588. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  589. struct btrfs_root *root)
  590. {
  591. unsigned long joined = 0;
  592. unsigned long timeout = 1;
  593. struct btrfs_transaction *cur_trans;
  594. struct btrfs_transaction *prev_trans = NULL;
  595. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  596. struct list_head dirty_fs_roots;
  597. struct extent_io_tree *pinned_copy;
  598. DEFINE_WAIT(wait);
  599. int ret;
  600. INIT_LIST_HEAD(&dirty_fs_roots);
  601. mutex_lock(&root->fs_info->trans_mutex);
  602. if (trans->transaction->in_commit) {
  603. cur_trans = trans->transaction;
  604. trans->transaction->use_count++;
  605. mutex_unlock(&root->fs_info->trans_mutex);
  606. btrfs_end_transaction(trans, root);
  607. ret = wait_for_commit(root, cur_trans);
  608. BUG_ON(ret);
  609. mutex_lock(&root->fs_info->trans_mutex);
  610. put_transaction(cur_trans);
  611. mutex_unlock(&root->fs_info->trans_mutex);
  612. return 0;
  613. }
  614. pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
  615. if (!pinned_copy)
  616. return -ENOMEM;
  617. extent_io_tree_init(pinned_copy,
  618. root->fs_info->btree_inode->i_mapping, GFP_NOFS);
  619. trans->transaction->in_commit = 1;
  620. printk("trans %Lu in commit\n", trans->transid);
  621. cur_trans = trans->transaction;
  622. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  623. prev_trans = list_entry(cur_trans->list.prev,
  624. struct btrfs_transaction, list);
  625. if (!prev_trans->commit_done) {
  626. prev_trans->use_count++;
  627. mutex_unlock(&root->fs_info->trans_mutex);
  628. wait_for_commit(root, prev_trans);
  629. mutex_lock(&root->fs_info->trans_mutex);
  630. put_transaction(prev_trans);
  631. }
  632. }
  633. do {
  634. joined = cur_trans->num_joined;
  635. WARN_ON(cur_trans != trans->transaction);
  636. prepare_to_wait(&cur_trans->writer_wait, &wait,
  637. TASK_UNINTERRUPTIBLE);
  638. if (cur_trans->num_writers > 1)
  639. timeout = MAX_SCHEDULE_TIMEOUT;
  640. else
  641. timeout = 1;
  642. mutex_unlock(&root->fs_info->trans_mutex);
  643. schedule_timeout(timeout);
  644. mutex_lock(&root->fs_info->trans_mutex);
  645. finish_wait(&cur_trans->writer_wait, &wait);
  646. ret = btrfs_write_ordered_inodes(trans, root);
  647. } while (cur_trans->num_writers > 1 ||
  648. (cur_trans->num_joined != joined));
  649. ret = create_pending_snapshots(trans, root->fs_info);
  650. BUG_ON(ret);
  651. WARN_ON(cur_trans != trans->transaction);
  652. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  653. &dirty_fs_roots);
  654. BUG_ON(ret);
  655. ret = btrfs_commit_tree_roots(trans, root);
  656. BUG_ON(ret);
  657. cur_trans = root->fs_info->running_transaction;
  658. spin_lock(&root->fs_info->new_trans_lock);
  659. root->fs_info->running_transaction = NULL;
  660. spin_unlock(&root->fs_info->new_trans_lock);
  661. btrfs_set_super_generation(&root->fs_info->super_copy,
  662. cur_trans->transid);
  663. btrfs_set_super_root(&root->fs_info->super_copy,
  664. root->fs_info->tree_root->node->start);
  665. btrfs_set_super_root_level(&root->fs_info->super_copy,
  666. btrfs_header_level(root->fs_info->tree_root->node));
  667. btrfs_set_super_chunk_root(&root->fs_info->super_copy,
  668. chunk_root->node->start);
  669. btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
  670. btrfs_header_level(chunk_root->node));
  671. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  672. sizeof(root->fs_info->super_copy));
  673. btrfs_copy_pinned(root, pinned_copy);
  674. mutex_unlock(&root->fs_info->trans_mutex);
  675. ret = btrfs_write_and_wait_transaction(trans, root);
  676. BUG_ON(ret);
  677. write_ctree_super(trans, root);
  678. btrfs_finish_extent_commit(trans, root, pinned_copy);
  679. mutex_lock(&root->fs_info->trans_mutex);
  680. kfree(pinned_copy);
  681. cur_trans->commit_done = 1;
  682. printk("trans %Lu done in commit\n", cur_trans->transid);
  683. root->fs_info->last_trans_committed = cur_trans->transid;
  684. wake_up(&cur_trans->commit_wait);
  685. put_transaction(cur_trans);
  686. put_transaction(cur_trans);
  687. if (root->fs_info->closing)
  688. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  689. else
  690. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  691. mutex_unlock(&root->fs_info->trans_mutex);
  692. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  693. if (root->fs_info->closing) {
  694. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  695. }
  696. return ret;
  697. }
  698. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  699. {
  700. struct list_head dirty_roots;
  701. INIT_LIST_HEAD(&dirty_roots);
  702. again:
  703. mutex_lock(&root->fs_info->trans_mutex);
  704. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  705. mutex_unlock(&root->fs_info->trans_mutex);
  706. if (!list_empty(&dirty_roots)) {
  707. drop_dirty_roots(root, &dirty_roots);
  708. goto again;
  709. }
  710. return 0;
  711. }