transaction.c 22 KB

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