transaction.c 22 KB

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