transaction.c 22 KB

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