transaction.c 30 KB

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