transaction.c 30 KB

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