transaction.c 30 KB

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