transaction.c 28 KB

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