transaction.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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/slab.h>
  20. #include <linux/sched.h>
  21. #include <linux/writeback.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/blkdev.h>
  24. #include "ctree.h"
  25. #include "disk-io.h"
  26. #include "transaction.h"
  27. #include "locking.h"
  28. #include "tree-log.h"
  29. #define BTRFS_ROOT_TRANS_TAG 0
  30. static noinline void put_transaction(struct btrfs_transaction *transaction)
  31. {
  32. WARN_ON(transaction->use_count == 0);
  33. transaction->use_count--;
  34. if (transaction->use_count == 0) {
  35. list_del_init(&transaction->list);
  36. memset(transaction, 0, sizeof(*transaction));
  37. kmem_cache_free(btrfs_transaction_cachep, transaction);
  38. }
  39. }
  40. static noinline void switch_commit_root(struct btrfs_root *root)
  41. {
  42. free_extent_buffer(root->commit_root);
  43. root->commit_root = btrfs_root_node(root);
  44. }
  45. /*
  46. * either allocate a new transaction or hop into the existing one
  47. */
  48. static noinline int join_transaction(struct btrfs_root *root)
  49. {
  50. struct btrfs_transaction *cur_trans;
  51. cur_trans = root->fs_info->running_transaction;
  52. if (!cur_trans) {
  53. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  54. GFP_NOFS);
  55. BUG_ON(!cur_trans);
  56. root->fs_info->generation++;
  57. cur_trans->num_writers = 1;
  58. cur_trans->num_joined = 0;
  59. cur_trans->transid = root->fs_info->generation;
  60. init_waitqueue_head(&cur_trans->writer_wait);
  61. init_waitqueue_head(&cur_trans->commit_wait);
  62. cur_trans->in_commit = 0;
  63. cur_trans->blocked = 0;
  64. cur_trans->use_count = 1;
  65. cur_trans->commit_done = 0;
  66. cur_trans->start_time = get_seconds();
  67. cur_trans->delayed_refs.root = RB_ROOT;
  68. cur_trans->delayed_refs.num_entries = 0;
  69. cur_trans->delayed_refs.num_heads_ready = 0;
  70. cur_trans->delayed_refs.num_heads = 0;
  71. cur_trans->delayed_refs.flushing = 0;
  72. cur_trans->delayed_refs.run_delayed_start = 0;
  73. spin_lock_init(&cur_trans->delayed_refs.lock);
  74. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  75. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  76. extent_io_tree_init(&cur_trans->dirty_pages,
  77. root->fs_info->btree_inode->i_mapping,
  78. GFP_NOFS);
  79. spin_lock(&root->fs_info->new_trans_lock);
  80. root->fs_info->running_transaction = cur_trans;
  81. spin_unlock(&root->fs_info->new_trans_lock);
  82. } else {
  83. cur_trans->num_writers++;
  84. cur_trans->num_joined++;
  85. }
  86. return 0;
  87. }
  88. /*
  89. * this does all the record keeping required to make sure that a reference
  90. * counted root is properly recorded in a given transaction. This is required
  91. * to make sure the old root from before we joined the transaction is deleted
  92. * when the transaction commits
  93. */
  94. static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
  95. struct btrfs_root *root)
  96. {
  97. if (root->ref_cows && root->last_trans < trans->transid) {
  98. WARN_ON(root == root->fs_info->extent_root);
  99. WARN_ON(root->commit_root != root->node);
  100. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  101. (unsigned long)root->root_key.objectid,
  102. BTRFS_ROOT_TRANS_TAG);
  103. root->last_trans = trans->transid;
  104. btrfs_init_reloc_root(trans, root);
  105. }
  106. return 0;
  107. }
  108. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  109. struct btrfs_root *root)
  110. {
  111. if (!root->ref_cows)
  112. return 0;
  113. mutex_lock(&root->fs_info->trans_mutex);
  114. if (root->last_trans == trans->transid) {
  115. mutex_unlock(&root->fs_info->trans_mutex);
  116. return 0;
  117. }
  118. record_root_in_trans(trans, root);
  119. mutex_unlock(&root->fs_info->trans_mutex);
  120. return 0;
  121. }
  122. /* wait for commit against the current transaction to become unblocked
  123. * when this is done, it is safe to start a new transaction, but the current
  124. * transaction might not be fully on disk.
  125. */
  126. static void wait_current_trans(struct btrfs_root *root)
  127. {
  128. struct btrfs_transaction *cur_trans;
  129. cur_trans = root->fs_info->running_transaction;
  130. if (cur_trans && cur_trans->blocked) {
  131. DEFINE_WAIT(wait);
  132. cur_trans->use_count++;
  133. while (1) {
  134. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  135. TASK_UNINTERRUPTIBLE);
  136. if (!cur_trans->blocked)
  137. break;
  138. mutex_unlock(&root->fs_info->trans_mutex);
  139. schedule();
  140. mutex_lock(&root->fs_info->trans_mutex);
  141. }
  142. finish_wait(&root->fs_info->transaction_wait, &wait);
  143. put_transaction(cur_trans);
  144. }
  145. }
  146. enum btrfs_trans_type {
  147. TRANS_START,
  148. TRANS_JOIN,
  149. TRANS_USERSPACE,
  150. };
  151. static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
  152. int num_blocks, int type)
  153. {
  154. struct btrfs_trans_handle *h =
  155. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  156. int ret;
  157. mutex_lock(&root->fs_info->trans_mutex);
  158. if (!root->fs_info->log_root_recovering &&
  159. ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
  160. type == TRANS_USERSPACE))
  161. wait_current_trans(root);
  162. ret = join_transaction(root);
  163. BUG_ON(ret);
  164. h->transid = root->fs_info->running_transaction->transid;
  165. h->transaction = root->fs_info->running_transaction;
  166. h->blocks_reserved = num_blocks;
  167. h->blocks_used = 0;
  168. h->block_group = 0;
  169. h->alloc_exclude_nr = 0;
  170. h->alloc_exclude_start = 0;
  171. h->delayed_ref_updates = 0;
  172. if (!current->journal_info && type != TRANS_USERSPACE)
  173. current->journal_info = h;
  174. root->fs_info->running_transaction->use_count++;
  175. record_root_in_trans(h, root);
  176. mutex_unlock(&root->fs_info->trans_mutex);
  177. return h;
  178. }
  179. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  180. int num_blocks)
  181. {
  182. return start_transaction(root, num_blocks, TRANS_START);
  183. }
  184. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
  185. int num_blocks)
  186. {
  187. return start_transaction(root, num_blocks, TRANS_JOIN);
  188. }
  189. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
  190. int num_blocks)
  191. {
  192. return start_transaction(r, num_blocks, TRANS_USERSPACE);
  193. }
  194. /* wait for a transaction commit to be fully complete */
  195. static noinline int wait_for_commit(struct btrfs_root *root,
  196. struct btrfs_transaction *commit)
  197. {
  198. DEFINE_WAIT(wait);
  199. mutex_lock(&root->fs_info->trans_mutex);
  200. while (!commit->commit_done) {
  201. prepare_to_wait(&commit->commit_wait, &wait,
  202. TASK_UNINTERRUPTIBLE);
  203. if (commit->commit_done)
  204. break;
  205. mutex_unlock(&root->fs_info->trans_mutex);
  206. schedule();
  207. mutex_lock(&root->fs_info->trans_mutex);
  208. }
  209. mutex_unlock(&root->fs_info->trans_mutex);
  210. finish_wait(&commit->commit_wait, &wait);
  211. return 0;
  212. }
  213. #if 0
  214. /*
  215. * rate limit against the drop_snapshot code. This helps to slow down new
  216. * operations if the drop_snapshot code isn't able to keep up.
  217. */
  218. static void throttle_on_drops(struct btrfs_root *root)
  219. {
  220. struct btrfs_fs_info *info = root->fs_info;
  221. int harder_count = 0;
  222. harder:
  223. if (atomic_read(&info->throttles)) {
  224. DEFINE_WAIT(wait);
  225. int thr;
  226. thr = atomic_read(&info->throttle_gen);
  227. do {
  228. prepare_to_wait(&info->transaction_throttle,
  229. &wait, TASK_UNINTERRUPTIBLE);
  230. if (!atomic_read(&info->throttles)) {
  231. finish_wait(&info->transaction_throttle, &wait);
  232. break;
  233. }
  234. schedule();
  235. finish_wait(&info->transaction_throttle, &wait);
  236. } while (thr == atomic_read(&info->throttle_gen));
  237. harder_count++;
  238. if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
  239. harder_count < 2)
  240. goto harder;
  241. if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
  242. harder_count < 10)
  243. goto harder;
  244. if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
  245. harder_count < 20)
  246. goto harder;
  247. }
  248. }
  249. #endif
  250. void btrfs_throttle(struct btrfs_root *root)
  251. {
  252. mutex_lock(&root->fs_info->trans_mutex);
  253. if (!root->fs_info->open_ioctl_trans)
  254. wait_current_trans(root);
  255. mutex_unlock(&root->fs_info->trans_mutex);
  256. }
  257. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  258. struct btrfs_root *root, int throttle)
  259. {
  260. struct btrfs_transaction *cur_trans;
  261. struct btrfs_fs_info *info = root->fs_info;
  262. int count = 0;
  263. while (count < 4) {
  264. unsigned long cur = trans->delayed_ref_updates;
  265. trans->delayed_ref_updates = 0;
  266. if (cur &&
  267. trans->transaction->delayed_refs.num_heads_ready > 64) {
  268. trans->delayed_ref_updates = 0;
  269. /*
  270. * do a full flush if the transaction is trying
  271. * to close
  272. */
  273. if (trans->transaction->delayed_refs.flushing)
  274. cur = 0;
  275. btrfs_run_delayed_refs(trans, root, cur);
  276. } else {
  277. break;
  278. }
  279. count++;
  280. }
  281. mutex_lock(&info->trans_mutex);
  282. cur_trans = info->running_transaction;
  283. WARN_ON(cur_trans != trans->transaction);
  284. WARN_ON(cur_trans->num_writers < 1);
  285. cur_trans->num_writers--;
  286. if (waitqueue_active(&cur_trans->writer_wait))
  287. wake_up(&cur_trans->writer_wait);
  288. put_transaction(cur_trans);
  289. mutex_unlock(&info->trans_mutex);
  290. if (current->journal_info == trans)
  291. current->journal_info = NULL;
  292. memset(trans, 0, sizeof(*trans));
  293. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  294. if (throttle)
  295. btrfs_run_delayed_iputs(root);
  296. return 0;
  297. }
  298. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  299. struct btrfs_root *root)
  300. {
  301. return __btrfs_end_transaction(trans, root, 0);
  302. }
  303. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  304. struct btrfs_root *root)
  305. {
  306. return __btrfs_end_transaction(trans, root, 1);
  307. }
  308. /*
  309. * when btree blocks are allocated, they have some corresponding bits set for
  310. * them in one of two extent_io trees. This is used to make sure all of
  311. * those extents are sent to disk but does not wait on them
  312. */
  313. int btrfs_write_marked_extents(struct btrfs_root *root,
  314. struct extent_io_tree *dirty_pages, int mark)
  315. {
  316. int ret;
  317. int err = 0;
  318. int werr = 0;
  319. struct page *page;
  320. struct inode *btree_inode = root->fs_info->btree_inode;
  321. u64 start = 0;
  322. u64 end;
  323. unsigned long index;
  324. while (1) {
  325. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  326. mark);
  327. if (ret)
  328. break;
  329. while (start <= end) {
  330. cond_resched();
  331. index = start >> PAGE_CACHE_SHIFT;
  332. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  333. page = find_get_page(btree_inode->i_mapping, index);
  334. if (!page)
  335. continue;
  336. btree_lock_page_hook(page);
  337. if (!page->mapping) {
  338. unlock_page(page);
  339. page_cache_release(page);
  340. continue;
  341. }
  342. if (PageWriteback(page)) {
  343. if (PageDirty(page))
  344. wait_on_page_writeback(page);
  345. else {
  346. unlock_page(page);
  347. page_cache_release(page);
  348. continue;
  349. }
  350. }
  351. err = write_one_page(page, 0);
  352. if (err)
  353. werr = err;
  354. page_cache_release(page);
  355. }
  356. }
  357. if (err)
  358. werr = err;
  359. return werr;
  360. }
  361. /*
  362. * when btree blocks are allocated, they have some corresponding bits set for
  363. * them in one of two extent_io trees. This is used to make sure all of
  364. * those extents are on disk for transaction or log commit. We wait
  365. * on all the pages and clear them from the dirty pages state tree
  366. */
  367. int btrfs_wait_marked_extents(struct btrfs_root *root,
  368. struct extent_io_tree *dirty_pages, int mark)
  369. {
  370. int ret;
  371. int err = 0;
  372. int werr = 0;
  373. struct page *page;
  374. struct inode *btree_inode = root->fs_info->btree_inode;
  375. u64 start = 0;
  376. u64 end;
  377. unsigned long index;
  378. while (1) {
  379. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  380. mark);
  381. if (ret)
  382. break;
  383. clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
  384. while (start <= end) {
  385. index = start >> PAGE_CACHE_SHIFT;
  386. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  387. page = find_get_page(btree_inode->i_mapping, index);
  388. if (!page)
  389. continue;
  390. if (PageDirty(page)) {
  391. btree_lock_page_hook(page);
  392. wait_on_page_writeback(page);
  393. err = write_one_page(page, 0);
  394. if (err)
  395. werr = err;
  396. }
  397. wait_on_page_writeback(page);
  398. page_cache_release(page);
  399. cond_resched();
  400. }
  401. }
  402. if (err)
  403. werr = err;
  404. return werr;
  405. }
  406. /*
  407. * when btree blocks are allocated, they have some corresponding bits set for
  408. * them in one of two extent_io trees. This is used to make sure all of
  409. * those extents are on disk for transaction or log commit
  410. */
  411. int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
  412. struct extent_io_tree *dirty_pages, int mark)
  413. {
  414. int ret;
  415. int ret2;
  416. ret = btrfs_write_marked_extents(root, dirty_pages, mark);
  417. ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
  418. return ret || ret2;
  419. }
  420. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  421. struct btrfs_root *root)
  422. {
  423. if (!trans || !trans->transaction) {
  424. struct inode *btree_inode;
  425. btree_inode = root->fs_info->btree_inode;
  426. return filemap_write_and_wait(btree_inode->i_mapping);
  427. }
  428. return btrfs_write_and_wait_marked_extents(root,
  429. &trans->transaction->dirty_pages,
  430. EXTENT_DIRTY);
  431. }
  432. /*
  433. * this is used to update the root pointer in the tree of tree roots.
  434. *
  435. * But, in the case of the extent allocation tree, updating the root
  436. * pointer may allocate blocks which may change the root of the extent
  437. * allocation tree.
  438. *
  439. * So, this loops and repeats and makes sure the cowonly root didn't
  440. * change while the root pointer was being updated in the metadata.
  441. */
  442. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  443. struct btrfs_root *root)
  444. {
  445. int ret;
  446. u64 old_root_bytenr;
  447. u64 old_root_used;
  448. struct btrfs_root *tree_root = root->fs_info->tree_root;
  449. old_root_used = btrfs_root_used(&root->root_item);
  450. btrfs_write_dirty_block_groups(trans, root);
  451. while (1) {
  452. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  453. if (old_root_bytenr == root->node->start &&
  454. old_root_used == btrfs_root_used(&root->root_item))
  455. break;
  456. btrfs_set_root_node(&root->root_item, root->node);
  457. ret = btrfs_update_root(trans, tree_root,
  458. &root->root_key,
  459. &root->root_item);
  460. BUG_ON(ret);
  461. old_root_used = btrfs_root_used(&root->root_item);
  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 btrfs_root *parent_root;
  668. struct inode *parent_inode;
  669. struct extent_buffer *tmp;
  670. struct extent_buffer *old;
  671. int ret;
  672. u64 objectid;
  673. int namelen;
  674. u64 index = 0;
  675. parent_inode = pending->dentry->d_parent->d_inode;
  676. parent_root = BTRFS_I(parent_inode)->root;
  677. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  678. if (!new_root_item) {
  679. ret = -ENOMEM;
  680. goto fail;
  681. }
  682. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  683. if (ret)
  684. goto fail;
  685. key.objectid = objectid;
  686. /* record when the snapshot was created in key.offset */
  687. key.offset = trans->transid;
  688. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  689. memcpy(&pending->root_key, &key, sizeof(key));
  690. pending->root_key.offset = (u64)-1;
  691. record_root_in_trans(trans, parent_root);
  692. /*
  693. * insert the directory item
  694. */
  695. namelen = strlen(pending->name);
  696. ret = btrfs_set_inode_index(parent_inode, &index);
  697. BUG_ON(ret);
  698. ret = btrfs_insert_dir_item(trans, parent_root,
  699. pending->name, namelen,
  700. parent_inode->i_ino,
  701. &pending->root_key, BTRFS_FT_DIR, index);
  702. BUG_ON(ret);
  703. btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
  704. ret = btrfs_update_inode(trans, parent_root, parent_inode);
  705. BUG_ON(ret);
  706. record_root_in_trans(trans, root);
  707. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  708. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  709. old = btrfs_lock_root_node(root);
  710. btrfs_cow_block(trans, root, old, NULL, 0, &old);
  711. btrfs_set_lock_blocking(old);
  712. btrfs_copy_root(trans, root, old, &tmp, objectid);
  713. btrfs_tree_unlock(old);
  714. free_extent_buffer(old);
  715. btrfs_set_root_node(new_root_item, tmp);
  716. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  717. new_root_item);
  718. BUG_ON(ret);
  719. btrfs_tree_unlock(tmp);
  720. free_extent_buffer(tmp);
  721. ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
  722. pending->root_key.objectid,
  723. parent_root->root_key.objectid,
  724. parent_inode->i_ino, index, pending->name,
  725. namelen);
  726. BUG_ON(ret);
  727. fail:
  728. kfree(new_root_item);
  729. return ret;
  730. }
  731. /*
  732. * create all the snapshots we've scheduled for creation
  733. */
  734. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  735. struct btrfs_fs_info *fs_info)
  736. {
  737. struct btrfs_pending_snapshot *pending;
  738. struct list_head *head = &trans->transaction->pending_snapshots;
  739. int ret;
  740. list_for_each_entry(pending, head, list) {
  741. ret = create_pending_snapshot(trans, fs_info, pending);
  742. BUG_ON(ret);
  743. }
  744. return 0;
  745. }
  746. static void update_super_roots(struct btrfs_root *root)
  747. {
  748. struct btrfs_root_item *root_item;
  749. struct btrfs_super_block *super;
  750. super = &root->fs_info->super_copy;
  751. root_item = &root->fs_info->chunk_root->root_item;
  752. super->chunk_root = root_item->bytenr;
  753. super->chunk_root_generation = root_item->generation;
  754. super->chunk_root_level = root_item->level;
  755. root_item = &root->fs_info->tree_root->root_item;
  756. super->root = root_item->bytenr;
  757. super->generation = root_item->generation;
  758. super->root_level = root_item->level;
  759. }
  760. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  761. {
  762. int ret = 0;
  763. spin_lock(&info->new_trans_lock);
  764. if (info->running_transaction)
  765. ret = info->running_transaction->in_commit;
  766. spin_unlock(&info->new_trans_lock);
  767. return ret;
  768. }
  769. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  770. struct btrfs_root *root)
  771. {
  772. unsigned long joined = 0;
  773. unsigned long timeout = 1;
  774. struct btrfs_transaction *cur_trans;
  775. struct btrfs_transaction *prev_trans = NULL;
  776. DEFINE_WAIT(wait);
  777. int ret;
  778. int should_grow = 0;
  779. unsigned long now = get_seconds();
  780. int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
  781. btrfs_run_ordered_operations(root, 0);
  782. /* make a pass through all the delayed refs we have so far
  783. * any runnings procs may add more while we are here
  784. */
  785. ret = btrfs_run_delayed_refs(trans, root, 0);
  786. BUG_ON(ret);
  787. cur_trans = trans->transaction;
  788. /*
  789. * set the flushing flag so procs in this transaction have to
  790. * start sending their work down.
  791. */
  792. cur_trans->delayed_refs.flushing = 1;
  793. ret = btrfs_run_delayed_refs(trans, root, 0);
  794. BUG_ON(ret);
  795. mutex_lock(&root->fs_info->trans_mutex);
  796. if (cur_trans->in_commit) {
  797. cur_trans->use_count++;
  798. mutex_unlock(&root->fs_info->trans_mutex);
  799. btrfs_end_transaction(trans, root);
  800. ret = wait_for_commit(root, cur_trans);
  801. BUG_ON(ret);
  802. mutex_lock(&root->fs_info->trans_mutex);
  803. put_transaction(cur_trans);
  804. mutex_unlock(&root->fs_info->trans_mutex);
  805. return 0;
  806. }
  807. trans->transaction->in_commit = 1;
  808. trans->transaction->blocked = 1;
  809. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  810. prev_trans = list_entry(cur_trans->list.prev,
  811. struct btrfs_transaction, list);
  812. if (!prev_trans->commit_done) {
  813. prev_trans->use_count++;
  814. mutex_unlock(&root->fs_info->trans_mutex);
  815. wait_for_commit(root, prev_trans);
  816. mutex_lock(&root->fs_info->trans_mutex);
  817. put_transaction(prev_trans);
  818. }
  819. }
  820. if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
  821. should_grow = 1;
  822. do {
  823. int snap_pending = 0;
  824. joined = cur_trans->num_joined;
  825. if (!list_empty(&trans->transaction->pending_snapshots))
  826. snap_pending = 1;
  827. WARN_ON(cur_trans != trans->transaction);
  828. prepare_to_wait(&cur_trans->writer_wait, &wait,
  829. TASK_UNINTERRUPTIBLE);
  830. if (cur_trans->num_writers > 1)
  831. timeout = MAX_SCHEDULE_TIMEOUT;
  832. else if (should_grow)
  833. timeout = 1;
  834. mutex_unlock(&root->fs_info->trans_mutex);
  835. if (flush_on_commit || snap_pending) {
  836. btrfs_start_delalloc_inodes(root, 1);
  837. ret = btrfs_wait_ordered_extents(root, 0, 1);
  838. BUG_ON(ret);
  839. }
  840. /*
  841. * rename don't use btrfs_join_transaction, so, once we
  842. * set the transaction to blocked above, we aren't going
  843. * to get any new ordered operations. We can safely run
  844. * it here and no for sure that nothing new will be added
  845. * to the list
  846. */
  847. btrfs_run_ordered_operations(root, 1);
  848. smp_mb();
  849. if (cur_trans->num_writers > 1 || should_grow)
  850. schedule_timeout(timeout);
  851. mutex_lock(&root->fs_info->trans_mutex);
  852. finish_wait(&cur_trans->writer_wait, &wait);
  853. } while (cur_trans->num_writers > 1 ||
  854. (should_grow && cur_trans->num_joined != joined));
  855. ret = create_pending_snapshots(trans, root->fs_info);
  856. BUG_ON(ret);
  857. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  858. BUG_ON(ret);
  859. WARN_ON(cur_trans != trans->transaction);
  860. /* btrfs_commit_tree_roots is responsible for getting the
  861. * various roots consistent with each other. Every pointer
  862. * in the tree of tree roots has to point to the most up to date
  863. * root for every subvolume and other tree. So, we have to keep
  864. * the tree logging code from jumping in and changing any
  865. * of the trees.
  866. *
  867. * At this point in the commit, there can't be any tree-log
  868. * writers, but a little lower down we drop the trans mutex
  869. * and let new people in. By holding the tree_log_mutex
  870. * from now until after the super is written, we avoid races
  871. * with the tree-log code.
  872. */
  873. mutex_lock(&root->fs_info->tree_log_mutex);
  874. ret = commit_fs_roots(trans, root);
  875. BUG_ON(ret);
  876. /* commit_fs_roots gets rid of all the tree log roots, it is now
  877. * safe to free the root of tree log roots
  878. */
  879. btrfs_free_log_root_tree(trans, root->fs_info);
  880. ret = commit_cowonly_roots(trans, root);
  881. BUG_ON(ret);
  882. btrfs_prepare_extent_commit(trans, root);
  883. cur_trans = root->fs_info->running_transaction;
  884. spin_lock(&root->fs_info->new_trans_lock);
  885. root->fs_info->running_transaction = NULL;
  886. spin_unlock(&root->fs_info->new_trans_lock);
  887. btrfs_set_root_node(&root->fs_info->tree_root->root_item,
  888. root->fs_info->tree_root->node);
  889. switch_commit_root(root->fs_info->tree_root);
  890. btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
  891. root->fs_info->chunk_root->node);
  892. switch_commit_root(root->fs_info->chunk_root);
  893. update_super_roots(root);
  894. if (!root->fs_info->log_root_recovering) {
  895. btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
  896. btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
  897. }
  898. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  899. sizeof(root->fs_info->super_copy));
  900. trans->transaction->blocked = 0;
  901. wake_up(&root->fs_info->transaction_wait);
  902. mutex_unlock(&root->fs_info->trans_mutex);
  903. ret = btrfs_write_and_wait_transaction(trans, root);
  904. BUG_ON(ret);
  905. write_ctree_super(trans, root, 0);
  906. /*
  907. * the super is written, we can safely allow the tree-loggers
  908. * to go about their business
  909. */
  910. mutex_unlock(&root->fs_info->tree_log_mutex);
  911. btrfs_finish_extent_commit(trans, root);
  912. mutex_lock(&root->fs_info->trans_mutex);
  913. cur_trans->commit_done = 1;
  914. root->fs_info->last_trans_committed = cur_trans->transid;
  915. wake_up(&cur_trans->commit_wait);
  916. put_transaction(cur_trans);
  917. put_transaction(cur_trans);
  918. mutex_unlock(&root->fs_info->trans_mutex);
  919. if (current->journal_info == trans)
  920. current->journal_info = NULL;
  921. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  922. if (current != root->fs_info->transaction_kthread)
  923. btrfs_run_delayed_iputs(root);
  924. return ret;
  925. }
  926. /*
  927. * interface function to delete all the snapshots we have scheduled for deletion
  928. */
  929. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  930. {
  931. LIST_HEAD(list);
  932. struct btrfs_fs_info *fs_info = root->fs_info;
  933. mutex_lock(&fs_info->trans_mutex);
  934. list_splice_init(&fs_info->dead_roots, &list);
  935. mutex_unlock(&fs_info->trans_mutex);
  936. while (!list_empty(&list)) {
  937. root = list_entry(list.next, struct btrfs_root, root_list);
  938. list_del(&root->root_list);
  939. if (btrfs_header_backref_rev(root->node) <
  940. BTRFS_MIXED_BACKREF_REV)
  941. btrfs_drop_snapshot(root, 0);
  942. else
  943. btrfs_drop_snapshot(root, 1);
  944. }
  945. return 0;
  946. }