transaction.c 23 KB

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