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