transaction.c 28 KB

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