transaction.c 22 KB

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