transaction.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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 <linux/blkdev.h>
  23. #include "ctree.h"
  24. #include "disk-io.h"
  25. #include "transaction.h"
  26. #include "locking.h"
  27. #include "ref-cache.h"
  28. #include "tree-log.h"
  29. static int total_trans = 0;
  30. extern struct kmem_cache *btrfs_trans_handle_cachep;
  31. extern struct kmem_cache *btrfs_transaction_cachep;
  32. #define BTRFS_ROOT_TRANS_TAG 0
  33. static noinline void put_transaction(struct btrfs_transaction *transaction)
  34. {
  35. WARN_ON(transaction->use_count == 0);
  36. transaction->use_count--;
  37. if (transaction->use_count == 0) {
  38. WARN_ON(total_trans == 0);
  39. total_trans--;
  40. list_del_init(&transaction->list);
  41. memset(transaction, 0, sizeof(*transaction));
  42. kmem_cache_free(btrfs_transaction_cachep, transaction);
  43. }
  44. }
  45. /*
  46. * either allocate a new transaction or hop into the existing one
  47. */
  48. static noinline int join_transaction(struct btrfs_root *root)
  49. {
  50. struct btrfs_transaction *cur_trans;
  51. cur_trans = root->fs_info->running_transaction;
  52. if (!cur_trans) {
  53. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  54. GFP_NOFS);
  55. total_trans++;
  56. BUG_ON(!cur_trans);
  57. root->fs_info->generation++;
  58. root->fs_info->last_alloc = 0;
  59. root->fs_info->last_data_alloc = 0;
  60. cur_trans->num_writers = 1;
  61. cur_trans->num_joined = 0;
  62. cur_trans->transid = root->fs_info->generation;
  63. init_waitqueue_head(&cur_trans->writer_wait);
  64. init_waitqueue_head(&cur_trans->commit_wait);
  65. cur_trans->in_commit = 0;
  66. cur_trans->blocked = 0;
  67. cur_trans->use_count = 1;
  68. cur_trans->commit_done = 0;
  69. cur_trans->start_time = get_seconds();
  70. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  71. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  72. extent_io_tree_init(&cur_trans->dirty_pages,
  73. root->fs_info->btree_inode->i_mapping,
  74. GFP_NOFS);
  75. spin_lock(&root->fs_info->new_trans_lock);
  76. root->fs_info->running_transaction = cur_trans;
  77. spin_unlock(&root->fs_info->new_trans_lock);
  78. } else {
  79. cur_trans->num_writers++;
  80. cur_trans->num_joined++;
  81. }
  82. return 0;
  83. }
  84. /*
  85. * this does all the record keeping required to make sure that a
  86. * reference counted root is properly recorded in a given transaction.
  87. * This is required to make sure the old root from before we joined the transaction
  88. * is deleted when the transaction commits
  89. */
  90. noinline int btrfs_record_root_in_trans(struct btrfs_root *root)
  91. {
  92. struct btrfs_dirty_root *dirty;
  93. u64 running_trans_id = root->fs_info->running_transaction->transid;
  94. if (root->ref_cows && root->last_trans < running_trans_id) {
  95. WARN_ON(root == root->fs_info->extent_root);
  96. if (root->root_item.refs != 0) {
  97. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  98. (unsigned long)root->root_key.objectid,
  99. BTRFS_ROOT_TRANS_TAG);
  100. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  101. BUG_ON(!dirty);
  102. dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
  103. BUG_ON(!dirty->root);
  104. dirty->latest_root = root;
  105. INIT_LIST_HEAD(&dirty->list);
  106. root->commit_root = btrfs_root_node(root);
  107. memcpy(dirty->root, root, sizeof(*root));
  108. spin_lock_init(&dirty->root->node_lock);
  109. spin_lock_init(&dirty->root->list_lock);
  110. mutex_init(&dirty->root->objectid_mutex);
  111. mutex_init(&dirty->root->log_mutex);
  112. INIT_LIST_HEAD(&dirty->root->dead_list);
  113. dirty->root->node = root->commit_root;
  114. dirty->root->commit_root = NULL;
  115. spin_lock(&root->list_lock);
  116. list_add(&dirty->root->dead_list, &root->dead_list);
  117. spin_unlock(&root->list_lock);
  118. root->dirty_root = dirty;
  119. } else {
  120. WARN_ON(1);
  121. }
  122. root->last_trans = running_trans_id;
  123. }
  124. return 0;
  125. }
  126. /* wait for commit against the current transaction to become unblocked
  127. * when this is done, it is safe to start a new transaction, but the current
  128. * transaction might not be fully on disk.
  129. */
  130. static void wait_current_trans(struct btrfs_root *root)
  131. {
  132. struct btrfs_transaction *cur_trans;
  133. cur_trans = root->fs_info->running_transaction;
  134. if (cur_trans && cur_trans->blocked) {
  135. DEFINE_WAIT(wait);
  136. cur_trans->use_count++;
  137. while(1) {
  138. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  139. TASK_UNINTERRUPTIBLE);
  140. if (cur_trans->blocked) {
  141. mutex_unlock(&root->fs_info->trans_mutex);
  142. schedule();
  143. mutex_lock(&root->fs_info->trans_mutex);
  144. finish_wait(&root->fs_info->transaction_wait,
  145. &wait);
  146. } else {
  147. finish_wait(&root->fs_info->transaction_wait,
  148. &wait);
  149. break;
  150. }
  151. }
  152. put_transaction(cur_trans);
  153. }
  154. }
  155. static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
  156. int num_blocks, int wait)
  157. {
  158. struct btrfs_trans_handle *h =
  159. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  160. int ret;
  161. mutex_lock(&root->fs_info->trans_mutex);
  162. if (!root->fs_info->log_root_recovering &&
  163. ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2))
  164. wait_current_trans(root);
  165. ret = join_transaction(root);
  166. BUG_ON(ret);
  167. btrfs_record_root_in_trans(root);
  168. h->transid = root->fs_info->running_transaction->transid;
  169. h->transaction = root->fs_info->running_transaction;
  170. h->blocks_reserved = num_blocks;
  171. h->blocks_used = 0;
  172. h->block_group = NULL;
  173. h->alloc_exclude_nr = 0;
  174. h->alloc_exclude_start = 0;
  175. root->fs_info->running_transaction->use_count++;
  176. mutex_unlock(&root->fs_info->trans_mutex);
  177. return h;
  178. }
  179. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  180. int num_blocks)
  181. {
  182. return start_transaction(root, num_blocks, 1);
  183. }
  184. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
  185. int num_blocks)
  186. {
  187. return start_transaction(root, num_blocks, 0);
  188. }
  189. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
  190. int num_blocks)
  191. {
  192. return start_transaction(r, num_blocks, 2);
  193. }
  194. /* wait for a transaction commit to be fully complete */
  195. static noinline int wait_for_commit(struct btrfs_root *root,
  196. struct btrfs_transaction *commit)
  197. {
  198. DEFINE_WAIT(wait);
  199. mutex_lock(&root->fs_info->trans_mutex);
  200. while(!commit->commit_done) {
  201. prepare_to_wait(&commit->commit_wait, &wait,
  202. TASK_UNINTERRUPTIBLE);
  203. if (commit->commit_done)
  204. break;
  205. mutex_unlock(&root->fs_info->trans_mutex);
  206. schedule();
  207. mutex_lock(&root->fs_info->trans_mutex);
  208. }
  209. mutex_unlock(&root->fs_info->trans_mutex);
  210. finish_wait(&commit->commit_wait, &wait);
  211. return 0;
  212. }
  213. /*
  214. * rate limit against the drop_snapshot code. This helps to slow down new operations
  215. * if the drop_snapshot code isn't able to keep up.
  216. */
  217. static void throttle_on_drops(struct btrfs_root *root)
  218. {
  219. struct btrfs_fs_info *info = root->fs_info;
  220. int harder_count = 0;
  221. harder:
  222. if (atomic_read(&info->throttles)) {
  223. DEFINE_WAIT(wait);
  224. int thr;
  225. thr = atomic_read(&info->throttle_gen);
  226. do {
  227. prepare_to_wait(&info->transaction_throttle,
  228. &wait, TASK_UNINTERRUPTIBLE);
  229. if (!atomic_read(&info->throttles)) {
  230. finish_wait(&info->transaction_throttle, &wait);
  231. break;
  232. }
  233. schedule();
  234. finish_wait(&info->transaction_throttle, &wait);
  235. } while (thr == atomic_read(&info->throttle_gen));
  236. harder_count++;
  237. if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
  238. harder_count < 2)
  239. goto harder;
  240. if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
  241. harder_count < 10)
  242. goto harder;
  243. if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
  244. harder_count < 20)
  245. goto harder;
  246. }
  247. }
  248. void btrfs_throttle(struct btrfs_root *root)
  249. {
  250. mutex_lock(&root->fs_info->trans_mutex);
  251. if (!root->fs_info->open_ioctl_trans)
  252. wait_current_trans(root);
  253. mutex_unlock(&root->fs_info->trans_mutex);
  254. throttle_on_drops(root);
  255. }
  256. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  257. struct btrfs_root *root, int throttle)
  258. {
  259. struct btrfs_transaction *cur_trans;
  260. struct btrfs_fs_info *info = root->fs_info;
  261. mutex_lock(&info->trans_mutex);
  262. cur_trans = info->running_transaction;
  263. WARN_ON(cur_trans != trans->transaction);
  264. WARN_ON(cur_trans->num_writers < 1);
  265. cur_trans->num_writers--;
  266. if (waitqueue_active(&cur_trans->writer_wait))
  267. wake_up(&cur_trans->writer_wait);
  268. put_transaction(cur_trans);
  269. mutex_unlock(&info->trans_mutex);
  270. memset(trans, 0, sizeof(*trans));
  271. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  272. if (throttle)
  273. throttle_on_drops(root);
  274. return 0;
  275. }
  276. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  277. struct btrfs_root *root)
  278. {
  279. return __btrfs_end_transaction(trans, root, 0);
  280. }
  281. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  282. struct btrfs_root *root)
  283. {
  284. return __btrfs_end_transaction(trans, root, 1);
  285. }
  286. /*
  287. * when btree blocks are allocated, they have some corresponding bits set for
  288. * them in one of two extent_io trees. This is used to make sure all of
  289. * those extents are on disk for transaction or log commit
  290. */
  291. int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
  292. struct extent_io_tree *dirty_pages)
  293. {
  294. int ret;
  295. int err = 0;
  296. int werr = 0;
  297. struct page *page;
  298. struct inode *btree_inode = root->fs_info->btree_inode;
  299. struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree;
  300. u64 start = 0;
  301. u64 end;
  302. unsigned long index;
  303. while(1) {
  304. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  305. EXTENT_DIRTY);
  306. if (ret)
  307. break;
  308. while(start <= end) {
  309. cond_resched();
  310. index = start >> PAGE_CACHE_SHIFT;
  311. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  312. page = find_get_page(btree_inode->i_mapping, index);
  313. if (!page)
  314. continue;
  315. btree_lock_page_hook(page);
  316. if (!page->mapping) {
  317. unlock_page(page);
  318. page_cache_release(page);
  319. continue;
  320. }
  321. if (PageWriteback(page)) {
  322. if (PageDirty(page))
  323. wait_on_page_writeback(page);
  324. else {
  325. unlock_page(page);
  326. page_cache_release(page);
  327. continue;
  328. }
  329. }
  330. err = write_one_page(page, 0);
  331. if (err)
  332. werr = err;
  333. page_cache_release(page);
  334. }
  335. }
  336. /*
  337. * we unplug once and then use the wait_on_extent_bit for
  338. * everything else
  339. */
  340. blk_run_address_space(btree_inode->i_mapping);
  341. while(1) {
  342. ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
  343. EXTENT_DIRTY);
  344. if (ret)
  345. break;
  346. clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
  347. while(start <= end) {
  348. index = start >> PAGE_CACHE_SHIFT;
  349. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  350. page = find_get_page(btree_inode->i_mapping, index);
  351. if (!page)
  352. continue;
  353. if (PageDirty(page)) {
  354. btree_lock_page_hook(page);
  355. wait_on_page_writeback(page);
  356. err = write_one_page(page, 0);
  357. if (err)
  358. werr = err;
  359. }
  360. if (PageWriteback(page)) {
  361. /*
  362. * we don't wait on the page writeback bit
  363. * because that triggers a lot of unplugs.
  364. * The extent bits are much nicer to
  365. * the disks, but come with a slightly
  366. * higher latency because we aren't forcing
  367. * unplugs.
  368. */
  369. wait_on_extent_writeback(io_tree,
  370. page_offset(page),
  371. page_offset(page) +
  372. PAGE_CACHE_SIZE - 1);
  373. }
  374. if (PageWriteback(page)) {
  375. /*
  376. * the state bits get cleared before the
  377. * page bits, lets add some extra
  378. * paranoia here
  379. */
  380. wait_on_page_writeback(page);
  381. }
  382. page_cache_release(page);
  383. cond_resched();
  384. }
  385. }
  386. if (err)
  387. werr = err;
  388. return werr;
  389. }
  390. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  391. struct btrfs_root *root)
  392. {
  393. if (!trans || !trans->transaction) {
  394. struct inode *btree_inode;
  395. btree_inode = root->fs_info->btree_inode;
  396. return filemap_write_and_wait(btree_inode->i_mapping);
  397. }
  398. return btrfs_write_and_wait_marked_extents(root,
  399. &trans->transaction->dirty_pages);
  400. }
  401. /*
  402. * this is used to update the root pointer in the tree of tree roots.
  403. *
  404. * But, in the case of the extent allocation tree, updating the root
  405. * pointer may allocate blocks which may change the root of the extent
  406. * allocation tree.
  407. *
  408. * So, this loops and repeats and makes sure the cowonly root didn't
  409. * change while the root pointer was being updated in the metadata.
  410. */
  411. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  412. struct btrfs_root *root)
  413. {
  414. int ret;
  415. u64 old_root_bytenr;
  416. struct btrfs_root *tree_root = root->fs_info->tree_root;
  417. btrfs_extent_post_op(trans, root);
  418. btrfs_write_dirty_block_groups(trans, root);
  419. btrfs_extent_post_op(trans, root);
  420. while(1) {
  421. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  422. if (old_root_bytenr == root->node->start)
  423. break;
  424. btrfs_set_root_bytenr(&root->root_item,
  425. root->node->start);
  426. btrfs_set_root_level(&root->root_item,
  427. btrfs_header_level(root->node));
  428. btrfs_set_root_generation(&root->root_item, trans->transid);
  429. btrfs_extent_post_op(trans, root);
  430. ret = btrfs_update_root(trans, tree_root,
  431. &root->root_key,
  432. &root->root_item);
  433. BUG_ON(ret);
  434. btrfs_write_dirty_block_groups(trans, root);
  435. btrfs_extent_post_op(trans, root);
  436. }
  437. return 0;
  438. }
  439. /*
  440. * update all the cowonly tree roots on disk
  441. */
  442. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  443. struct btrfs_root *root)
  444. {
  445. struct btrfs_fs_info *fs_info = root->fs_info;
  446. struct list_head *next;
  447. struct extent_buffer *eb;
  448. btrfs_extent_post_op(trans, fs_info->tree_root);
  449. eb = btrfs_lock_root_node(fs_info->tree_root);
  450. btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb, 0);
  451. btrfs_tree_unlock(eb);
  452. free_extent_buffer(eb);
  453. btrfs_extent_post_op(trans, fs_info->tree_root);
  454. while(!list_empty(&fs_info->dirty_cowonly_roots)) {
  455. next = fs_info->dirty_cowonly_roots.next;
  456. list_del_init(next);
  457. root = list_entry(next, struct btrfs_root, dirty_list);
  458. update_cowonly_root(trans, root);
  459. }
  460. return 0;
  461. }
  462. /*
  463. * dead roots are old snapshots that need to be deleted. This allocates
  464. * a dirty root struct and adds it into the list of dead roots that need to
  465. * be deleted
  466. */
  467. int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
  468. {
  469. struct btrfs_dirty_root *dirty;
  470. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  471. if (!dirty)
  472. return -ENOMEM;
  473. dirty->root = root;
  474. dirty->latest_root = latest;
  475. mutex_lock(&root->fs_info->trans_mutex);
  476. list_add(&dirty->list, &latest->fs_info->dead_roots);
  477. mutex_unlock(&root->fs_info->trans_mutex);
  478. return 0;
  479. }
  480. /*
  481. * at transaction commit time we need to schedule the old roots for
  482. * deletion via btrfs_drop_snapshot. This runs through all the
  483. * reference counted roots that were modified in the current
  484. * transaction and puts them into the drop list
  485. */
  486. static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
  487. struct radix_tree_root *radix,
  488. struct list_head *list)
  489. {
  490. struct btrfs_dirty_root *dirty;
  491. struct btrfs_root *gang[8];
  492. struct btrfs_root *root;
  493. int i;
  494. int ret;
  495. int err = 0;
  496. u32 refs;
  497. while(1) {
  498. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  499. ARRAY_SIZE(gang),
  500. BTRFS_ROOT_TRANS_TAG);
  501. if (ret == 0)
  502. break;
  503. for (i = 0; i < ret; i++) {
  504. root = gang[i];
  505. radix_tree_tag_clear(radix,
  506. (unsigned long)root->root_key.objectid,
  507. BTRFS_ROOT_TRANS_TAG);
  508. BUG_ON(!root->ref_tree);
  509. dirty = root->dirty_root;
  510. btrfs_free_log(trans, root);
  511. btrfs_free_reloc_root(trans, root);
  512. if (root->commit_root == root->node) {
  513. WARN_ON(root->node->start !=
  514. btrfs_root_bytenr(&root->root_item));
  515. free_extent_buffer(root->commit_root);
  516. root->commit_root = NULL;
  517. root->dirty_root = NULL;
  518. spin_lock(&root->list_lock);
  519. list_del_init(&dirty->root->dead_list);
  520. spin_unlock(&root->list_lock);
  521. kfree(dirty->root);
  522. kfree(dirty);
  523. /* make sure to update the root on disk
  524. * so we get any updates to the block used
  525. * counts
  526. */
  527. err = btrfs_update_root(trans,
  528. root->fs_info->tree_root,
  529. &root->root_key,
  530. &root->root_item);
  531. continue;
  532. }
  533. memset(&root->root_item.drop_progress, 0,
  534. sizeof(struct btrfs_disk_key));
  535. root->root_item.drop_level = 0;
  536. root->commit_root = NULL;
  537. root->dirty_root = NULL;
  538. root->root_key.offset = root->fs_info->generation;
  539. btrfs_set_root_bytenr(&root->root_item,
  540. root->node->start);
  541. btrfs_set_root_level(&root->root_item,
  542. btrfs_header_level(root->node));
  543. btrfs_set_root_generation(&root->root_item,
  544. root->root_key.offset);
  545. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  546. &root->root_key,
  547. &root->root_item);
  548. if (err)
  549. break;
  550. refs = btrfs_root_refs(&dirty->root->root_item);
  551. btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
  552. err = btrfs_update_root(trans, root->fs_info->tree_root,
  553. &dirty->root->root_key,
  554. &dirty->root->root_item);
  555. BUG_ON(err);
  556. if (refs == 1) {
  557. list_add(&dirty->list, list);
  558. } else {
  559. WARN_ON(1);
  560. free_extent_buffer(dirty->root->node);
  561. kfree(dirty->root);
  562. kfree(dirty);
  563. }
  564. }
  565. }
  566. return err;
  567. }
  568. /*
  569. * defrag a given btree. If cacheonly == 1, this won't read from the disk,
  570. * otherwise every leaf in the btree is read and defragged.
  571. */
  572. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  573. {
  574. struct btrfs_fs_info *info = root->fs_info;
  575. int ret;
  576. struct btrfs_trans_handle *trans;
  577. unsigned long nr;
  578. smp_mb();
  579. if (root->defrag_running)
  580. return 0;
  581. trans = btrfs_start_transaction(root, 1);
  582. while (1) {
  583. root->defrag_running = 1;
  584. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  585. nr = trans->blocks_used;
  586. btrfs_end_transaction(trans, root);
  587. btrfs_btree_balance_dirty(info->tree_root, nr);
  588. cond_resched();
  589. trans = btrfs_start_transaction(root, 1);
  590. if (root->fs_info->closing || ret != -EAGAIN)
  591. break;
  592. }
  593. root->defrag_running = 0;
  594. smp_mb();
  595. btrfs_end_transaction(trans, root);
  596. return 0;
  597. }
  598. /*
  599. * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
  600. * all of them
  601. */
  602. static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
  603. struct list_head *list)
  604. {
  605. struct btrfs_dirty_root *dirty;
  606. struct btrfs_trans_handle *trans;
  607. unsigned long nr;
  608. u64 num_bytes;
  609. u64 bytes_used;
  610. u64 max_useless;
  611. int ret = 0;
  612. int err;
  613. while(!list_empty(list)) {
  614. struct btrfs_root *root;
  615. dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
  616. list_del_init(&dirty->list);
  617. num_bytes = btrfs_root_used(&dirty->root->root_item);
  618. root = dirty->latest_root;
  619. atomic_inc(&root->fs_info->throttles);
  620. while(1) {
  621. trans = btrfs_start_transaction(tree_root, 1);
  622. mutex_lock(&root->fs_info->drop_mutex);
  623. ret = btrfs_drop_snapshot(trans, dirty->root);
  624. if (ret != -EAGAIN) {
  625. break;
  626. }
  627. mutex_unlock(&root->fs_info->drop_mutex);
  628. err = btrfs_update_root(trans,
  629. tree_root,
  630. &dirty->root->root_key,
  631. &dirty->root->root_item);
  632. if (err)
  633. ret = err;
  634. nr = trans->blocks_used;
  635. ret = btrfs_end_transaction(trans, tree_root);
  636. BUG_ON(ret);
  637. btrfs_btree_balance_dirty(tree_root, nr);
  638. cond_resched();
  639. }
  640. BUG_ON(ret);
  641. atomic_dec(&root->fs_info->throttles);
  642. wake_up(&root->fs_info->transaction_throttle);
  643. num_bytes -= btrfs_root_used(&dirty->root->root_item);
  644. bytes_used = btrfs_root_used(&root->root_item);
  645. if (num_bytes) {
  646. btrfs_record_root_in_trans(root);
  647. btrfs_set_root_used(&root->root_item,
  648. bytes_used - num_bytes);
  649. }
  650. ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
  651. if (ret) {
  652. BUG();
  653. break;
  654. }
  655. mutex_unlock(&root->fs_info->drop_mutex);
  656. spin_lock(&root->list_lock);
  657. list_del_init(&dirty->root->dead_list);
  658. if (!list_empty(&root->dead_list)) {
  659. struct btrfs_root *oldest;
  660. oldest = list_entry(root->dead_list.prev,
  661. struct btrfs_root, dead_list);
  662. max_useless = oldest->root_key.offset - 1;
  663. } else {
  664. max_useless = root->root_key.offset - 1;
  665. }
  666. spin_unlock(&root->list_lock);
  667. nr = trans->blocks_used;
  668. ret = btrfs_end_transaction(trans, tree_root);
  669. BUG_ON(ret);
  670. ret = btrfs_remove_leaf_refs(root, max_useless, 0);
  671. BUG_ON(ret);
  672. free_extent_buffer(dirty->root->node);
  673. kfree(dirty->root);
  674. kfree(dirty);
  675. btrfs_btree_balance_dirty(tree_root, nr);
  676. cond_resched();
  677. }
  678. return ret;
  679. }
  680. /*
  681. * new snapshots need to be created at a very specific time in the
  682. * transaction commit. This does the actual creation
  683. */
  684. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  685. struct btrfs_fs_info *fs_info,
  686. struct btrfs_pending_snapshot *pending)
  687. {
  688. struct btrfs_key key;
  689. struct btrfs_root_item *new_root_item;
  690. struct btrfs_root *tree_root = fs_info->tree_root;
  691. struct btrfs_root *root = pending->root;
  692. struct extent_buffer *tmp;
  693. struct extent_buffer *old;
  694. int ret;
  695. int namelen;
  696. u64 objectid;
  697. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  698. if (!new_root_item) {
  699. ret = -ENOMEM;
  700. goto fail;
  701. }
  702. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  703. if (ret)
  704. goto fail;
  705. btrfs_record_root_in_trans(root);
  706. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  707. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  708. key.objectid = objectid;
  709. key.offset = trans->transid;
  710. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  711. old = btrfs_lock_root_node(root);
  712. btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
  713. btrfs_copy_root(trans, root, old, &tmp, objectid);
  714. btrfs_tree_unlock(old);
  715. free_extent_buffer(old);
  716. btrfs_set_root_bytenr(new_root_item, tmp->start);
  717. btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
  718. btrfs_set_root_generation(new_root_item, trans->transid);
  719. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  720. new_root_item);
  721. btrfs_tree_unlock(tmp);
  722. free_extent_buffer(tmp);
  723. if (ret)
  724. goto fail;
  725. /*
  726. * insert the directory item
  727. */
  728. key.offset = (u64)-1;
  729. namelen = strlen(pending->name);
  730. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  731. pending->name, namelen,
  732. root->fs_info->sb->s_root->d_inode->i_ino,
  733. &key, BTRFS_FT_DIR, 0);
  734. if (ret)
  735. goto fail;
  736. ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
  737. pending->name, strlen(pending->name), objectid,
  738. root->fs_info->sb->s_root->d_inode->i_ino, 0);
  739. /* Invalidate existing dcache entry for new snapshot. */
  740. btrfs_invalidate_dcache_root(root, pending->name, namelen);
  741. fail:
  742. kfree(new_root_item);
  743. return ret;
  744. }
  745. /*
  746. * create all the snapshots we've scheduled for creation
  747. */
  748. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  749. struct btrfs_fs_info *fs_info)
  750. {
  751. struct btrfs_pending_snapshot *pending;
  752. struct list_head *head = &trans->transaction->pending_snapshots;
  753. int ret;
  754. while(!list_empty(head)) {
  755. pending = list_entry(head->next,
  756. struct btrfs_pending_snapshot, list);
  757. ret = create_pending_snapshot(trans, fs_info, pending);
  758. BUG_ON(ret);
  759. list_del(&pending->list);
  760. kfree(pending->name);
  761. kfree(pending);
  762. }
  763. return 0;
  764. }
  765. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  766. struct btrfs_root *root)
  767. {
  768. unsigned long joined = 0;
  769. unsigned long timeout = 1;
  770. struct btrfs_transaction *cur_trans;
  771. struct btrfs_transaction *prev_trans = NULL;
  772. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  773. struct list_head dirty_fs_roots;
  774. struct extent_io_tree *pinned_copy;
  775. DEFINE_WAIT(wait);
  776. int ret;
  777. INIT_LIST_HEAD(&dirty_fs_roots);
  778. mutex_lock(&root->fs_info->trans_mutex);
  779. if (trans->transaction->in_commit) {
  780. cur_trans = trans->transaction;
  781. trans->transaction->use_count++;
  782. mutex_unlock(&root->fs_info->trans_mutex);
  783. btrfs_end_transaction(trans, root);
  784. ret = wait_for_commit(root, cur_trans);
  785. BUG_ON(ret);
  786. mutex_lock(&root->fs_info->trans_mutex);
  787. put_transaction(cur_trans);
  788. mutex_unlock(&root->fs_info->trans_mutex);
  789. return 0;
  790. }
  791. pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
  792. if (!pinned_copy)
  793. return -ENOMEM;
  794. extent_io_tree_init(pinned_copy,
  795. root->fs_info->btree_inode->i_mapping, GFP_NOFS);
  796. trans->transaction->in_commit = 1;
  797. trans->transaction->blocked = 1;
  798. cur_trans = trans->transaction;
  799. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  800. prev_trans = list_entry(cur_trans->list.prev,
  801. struct btrfs_transaction, list);
  802. if (!prev_trans->commit_done) {
  803. prev_trans->use_count++;
  804. mutex_unlock(&root->fs_info->trans_mutex);
  805. wait_for_commit(root, prev_trans);
  806. mutex_lock(&root->fs_info->trans_mutex);
  807. put_transaction(prev_trans);
  808. }
  809. }
  810. do {
  811. int snap_pending = 0;
  812. joined = cur_trans->num_joined;
  813. if (!list_empty(&trans->transaction->pending_snapshots))
  814. snap_pending = 1;
  815. WARN_ON(cur_trans != trans->transaction);
  816. prepare_to_wait(&cur_trans->writer_wait, &wait,
  817. TASK_UNINTERRUPTIBLE);
  818. if (cur_trans->num_writers > 1)
  819. timeout = MAX_SCHEDULE_TIMEOUT;
  820. else
  821. timeout = 1;
  822. mutex_unlock(&root->fs_info->trans_mutex);
  823. if (snap_pending) {
  824. ret = btrfs_wait_ordered_extents(root, 1);
  825. BUG_ON(ret);
  826. }
  827. schedule_timeout(timeout);
  828. mutex_lock(&root->fs_info->trans_mutex);
  829. finish_wait(&cur_trans->writer_wait, &wait);
  830. } while (cur_trans->num_writers > 1 ||
  831. (cur_trans->num_joined != joined));
  832. ret = create_pending_snapshots(trans, root->fs_info);
  833. BUG_ON(ret);
  834. WARN_ON(cur_trans != trans->transaction);
  835. /* btrfs_commit_tree_roots is responsible for getting the
  836. * various roots consistent with each other. Every pointer
  837. * in the tree of tree roots has to point to the most up to date
  838. * root for every subvolume and other tree. So, we have to keep
  839. * the tree logging code from jumping in and changing any
  840. * of the trees.
  841. *
  842. * At this point in the commit, there can't be any tree-log
  843. * writers, but a little lower down we drop the trans mutex
  844. * and let new people in. By holding the tree_log_mutex
  845. * from now until after the super is written, we avoid races
  846. * with the tree-log code.
  847. */
  848. mutex_lock(&root->fs_info->tree_log_mutex);
  849. /*
  850. * keep tree reloc code from adding new reloc trees
  851. */
  852. mutex_lock(&root->fs_info->tree_reloc_mutex);
  853. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  854. &dirty_fs_roots);
  855. BUG_ON(ret);
  856. /* add_dirty_roots gets rid of all the tree log roots, it is now
  857. * safe to free the root of tree log roots
  858. */
  859. btrfs_free_log_root_tree(trans, root->fs_info);
  860. ret = btrfs_commit_tree_roots(trans, root);
  861. BUG_ON(ret);
  862. cur_trans = root->fs_info->running_transaction;
  863. spin_lock(&root->fs_info->new_trans_lock);
  864. root->fs_info->running_transaction = NULL;
  865. spin_unlock(&root->fs_info->new_trans_lock);
  866. btrfs_set_super_generation(&root->fs_info->super_copy,
  867. cur_trans->transid);
  868. btrfs_set_super_root(&root->fs_info->super_copy,
  869. root->fs_info->tree_root->node->start);
  870. btrfs_set_super_root_level(&root->fs_info->super_copy,
  871. btrfs_header_level(root->fs_info->tree_root->node));
  872. btrfs_set_super_chunk_root(&root->fs_info->super_copy,
  873. chunk_root->node->start);
  874. btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
  875. btrfs_header_level(chunk_root->node));
  876. btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
  877. btrfs_header_generation(chunk_root->node));
  878. if (!root->fs_info->log_root_recovering) {
  879. btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
  880. btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
  881. }
  882. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  883. sizeof(root->fs_info->super_copy));
  884. btrfs_copy_pinned(root, pinned_copy);
  885. trans->transaction->blocked = 0;
  886. wake_up(&root->fs_info->transaction_throttle);
  887. wake_up(&root->fs_info->transaction_wait);
  888. mutex_unlock(&root->fs_info->trans_mutex);
  889. ret = btrfs_write_and_wait_transaction(trans, root);
  890. BUG_ON(ret);
  891. write_ctree_super(trans, root);
  892. /*
  893. * the super is written, we can safely allow the tree-loggers
  894. * to go about their business
  895. */
  896. mutex_unlock(&root->fs_info->tree_log_mutex);
  897. btrfs_finish_extent_commit(trans, root, pinned_copy);
  898. kfree(pinned_copy);
  899. btrfs_drop_dead_reloc_roots(root);
  900. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  901. mutex_lock(&root->fs_info->trans_mutex);
  902. cur_trans->commit_done = 1;
  903. root->fs_info->last_trans_committed = cur_trans->transid;
  904. wake_up(&cur_trans->commit_wait);
  905. put_transaction(cur_trans);
  906. put_transaction(cur_trans);
  907. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  908. if (root->fs_info->closing)
  909. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  910. mutex_unlock(&root->fs_info->trans_mutex);
  911. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  912. if (root->fs_info->closing) {
  913. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  914. }
  915. return ret;
  916. }
  917. /*
  918. * interface function to delete all the snapshots we have scheduled for deletion
  919. */
  920. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  921. {
  922. struct list_head dirty_roots;
  923. INIT_LIST_HEAD(&dirty_roots);
  924. again:
  925. mutex_lock(&root->fs_info->trans_mutex);
  926. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  927. mutex_unlock(&root->fs_info->trans_mutex);
  928. if (!list_empty(&dirty_roots)) {
  929. drop_dirty_roots(root, &dirty_roots);
  930. goto again;
  931. }
  932. return 0;
  933. }