transaction.c 30 KB

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