transaction.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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. #include "inode-map.h"
  30. #define BTRFS_ROOT_TRANS_TAG 0
  31. static noinline void put_transaction(struct btrfs_transaction *transaction)
  32. {
  33. WARN_ON(atomic_read(&transaction->use_count) == 0);
  34. if (atomic_dec_and_test(&transaction->use_count)) {
  35. memset(transaction, 0, sizeof(*transaction));
  36. kmem_cache_free(btrfs_transaction_cachep, transaction);
  37. }
  38. }
  39. static noinline void switch_commit_root(struct btrfs_root *root)
  40. {
  41. free_extent_buffer(root->commit_root);
  42. root->commit_root = btrfs_root_node(root);
  43. }
  44. /*
  45. * either allocate a new transaction or hop into the existing one
  46. */
  47. static noinline int join_transaction(struct btrfs_root *root)
  48. {
  49. struct btrfs_transaction *cur_trans;
  50. cur_trans = root->fs_info->running_transaction;
  51. if (!cur_trans) {
  52. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  53. GFP_NOFS);
  54. if (!cur_trans)
  55. return -ENOMEM;
  56. root->fs_info->generation++;
  57. atomic_set(&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. atomic_set(&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. spin_lock(&root->fs_info->new_trans_lock);
  79. root->fs_info->running_transaction = cur_trans;
  80. spin_unlock(&root->fs_info->new_trans_lock);
  81. } else {
  82. atomic_inc(&cur_trans->num_writers);
  83. cur_trans->num_joined++;
  84. }
  85. return 0;
  86. }
  87. /*
  88. * this does all the record keeping required to make sure that a reference
  89. * counted root is properly recorded in a given transaction. This is required
  90. * to make sure the old root from before we joined the transaction is deleted
  91. * when the transaction commits
  92. */
  93. static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
  94. struct btrfs_root *root)
  95. {
  96. if (root->ref_cows && root->last_trans < trans->transid) {
  97. WARN_ON(root == root->fs_info->extent_root);
  98. WARN_ON(root->commit_root != root->node);
  99. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  100. (unsigned long)root->root_key.objectid,
  101. BTRFS_ROOT_TRANS_TAG);
  102. root->last_trans = trans->transid;
  103. btrfs_init_reloc_root(trans, root);
  104. }
  105. return 0;
  106. }
  107. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  108. struct btrfs_root *root)
  109. {
  110. if (!root->ref_cows)
  111. return 0;
  112. mutex_lock(&root->fs_info->trans_mutex);
  113. if (root->last_trans == trans->transid) {
  114. mutex_unlock(&root->fs_info->trans_mutex);
  115. return 0;
  116. }
  117. record_root_in_trans(trans, root);
  118. mutex_unlock(&root->fs_info->trans_mutex);
  119. return 0;
  120. }
  121. /* wait for commit against the current transaction to become unblocked
  122. * when this is done, it is safe to start a new transaction, but the current
  123. * transaction might not be fully on disk.
  124. */
  125. static void wait_current_trans(struct btrfs_root *root)
  126. {
  127. struct btrfs_transaction *cur_trans;
  128. cur_trans = root->fs_info->running_transaction;
  129. if (cur_trans && cur_trans->blocked) {
  130. DEFINE_WAIT(wait);
  131. atomic_inc(&cur_trans->use_count);
  132. while (1) {
  133. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  134. TASK_UNINTERRUPTIBLE);
  135. if (!cur_trans->blocked)
  136. break;
  137. mutex_unlock(&root->fs_info->trans_mutex);
  138. schedule();
  139. mutex_lock(&root->fs_info->trans_mutex);
  140. }
  141. finish_wait(&root->fs_info->transaction_wait, &wait);
  142. put_transaction(cur_trans);
  143. }
  144. }
  145. enum btrfs_trans_type {
  146. TRANS_START,
  147. TRANS_JOIN,
  148. TRANS_USERSPACE,
  149. TRANS_JOIN_NOLOCK,
  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. if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
  167. return ERR_PTR(-EROFS);
  168. again:
  169. h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  170. if (!h)
  171. return ERR_PTR(-ENOMEM);
  172. if (type != TRANS_JOIN_NOLOCK)
  173. mutex_lock(&root->fs_info->trans_mutex);
  174. if (may_wait_transaction(root, type))
  175. wait_current_trans(root);
  176. ret = join_transaction(root);
  177. if (ret < 0) {
  178. kmem_cache_free(btrfs_trans_handle_cachep, h);
  179. if (type != TRANS_JOIN_NOLOCK)
  180. mutex_unlock(&root->fs_info->trans_mutex);
  181. return ERR_PTR(ret);
  182. }
  183. cur_trans = root->fs_info->running_transaction;
  184. atomic_inc(&cur_trans->use_count);
  185. if (type != TRANS_JOIN_NOLOCK)
  186. mutex_unlock(&root->fs_info->trans_mutex);
  187. h->transid = cur_trans->transid;
  188. h->transaction = cur_trans;
  189. h->blocks_used = 0;
  190. h->block_group = 0;
  191. h->bytes_reserved = 0;
  192. h->delayed_ref_updates = 0;
  193. h->block_rsv = NULL;
  194. smp_mb();
  195. if (cur_trans->blocked && may_wait_transaction(root, type)) {
  196. btrfs_commit_transaction(h, root);
  197. goto again;
  198. }
  199. if (num_items > 0) {
  200. ret = btrfs_trans_reserve_metadata(h, root, num_items);
  201. if (ret == -EAGAIN && !retries) {
  202. retries++;
  203. btrfs_commit_transaction(h, root);
  204. goto again;
  205. } else if (ret == -EAGAIN) {
  206. /*
  207. * We have already retried and got EAGAIN, so really we
  208. * don't have space, so set ret to -ENOSPC.
  209. */
  210. ret = -ENOSPC;
  211. }
  212. if (ret < 0) {
  213. btrfs_end_transaction(h, root);
  214. return ERR_PTR(ret);
  215. }
  216. }
  217. if (type != TRANS_JOIN_NOLOCK)
  218. mutex_lock(&root->fs_info->trans_mutex);
  219. record_root_in_trans(h, root);
  220. if (type != TRANS_JOIN_NOLOCK)
  221. mutex_unlock(&root->fs_info->trans_mutex);
  222. if (!current->journal_info && type != TRANS_USERSPACE)
  223. current->journal_info = h;
  224. return h;
  225. }
  226. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  227. int num_items)
  228. {
  229. return start_transaction(root, num_items, TRANS_START);
  230. }
  231. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
  232. int num_blocks)
  233. {
  234. return start_transaction(root, 0, TRANS_JOIN);
  235. }
  236. struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root,
  237. int num_blocks)
  238. {
  239. return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
  240. }
  241. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
  242. int num_blocks)
  243. {
  244. return start_transaction(r, 0, TRANS_USERSPACE);
  245. }
  246. /* wait for a transaction commit to be fully complete */
  247. static noinline int wait_for_commit(struct btrfs_root *root,
  248. struct btrfs_transaction *commit)
  249. {
  250. DEFINE_WAIT(wait);
  251. mutex_lock(&root->fs_info->trans_mutex);
  252. while (!commit->commit_done) {
  253. prepare_to_wait(&commit->commit_wait, &wait,
  254. TASK_UNINTERRUPTIBLE);
  255. if (commit->commit_done)
  256. break;
  257. mutex_unlock(&root->fs_info->trans_mutex);
  258. schedule();
  259. mutex_lock(&root->fs_info->trans_mutex);
  260. }
  261. mutex_unlock(&root->fs_info->trans_mutex);
  262. finish_wait(&commit->commit_wait, &wait);
  263. return 0;
  264. }
  265. int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
  266. {
  267. struct btrfs_transaction *cur_trans = NULL, *t;
  268. int ret;
  269. mutex_lock(&root->fs_info->trans_mutex);
  270. ret = 0;
  271. if (transid) {
  272. if (transid <= root->fs_info->last_trans_committed)
  273. goto out_unlock;
  274. /* find specified transaction */
  275. list_for_each_entry(t, &root->fs_info->trans_list, list) {
  276. if (t->transid == transid) {
  277. cur_trans = t;
  278. break;
  279. }
  280. if (t->transid > transid)
  281. break;
  282. }
  283. ret = -EINVAL;
  284. if (!cur_trans)
  285. goto out_unlock; /* bad transid */
  286. } else {
  287. /* find newest transaction that is committing | committed */
  288. list_for_each_entry_reverse(t, &root->fs_info->trans_list,
  289. list) {
  290. if (t->in_commit) {
  291. if (t->commit_done)
  292. goto out_unlock;
  293. cur_trans = t;
  294. break;
  295. }
  296. }
  297. if (!cur_trans)
  298. goto out_unlock; /* nothing committing|committed */
  299. }
  300. atomic_inc(&cur_trans->use_count);
  301. mutex_unlock(&root->fs_info->trans_mutex);
  302. wait_for_commit(root, cur_trans);
  303. mutex_lock(&root->fs_info->trans_mutex);
  304. put_transaction(cur_trans);
  305. ret = 0;
  306. out_unlock:
  307. mutex_unlock(&root->fs_info->trans_mutex);
  308. return ret;
  309. }
  310. void btrfs_throttle(struct btrfs_root *root)
  311. {
  312. mutex_lock(&root->fs_info->trans_mutex);
  313. if (!root->fs_info->open_ioctl_trans)
  314. wait_current_trans(root);
  315. mutex_unlock(&root->fs_info->trans_mutex);
  316. }
  317. static int should_end_transaction(struct btrfs_trans_handle *trans,
  318. struct btrfs_root *root)
  319. {
  320. int ret;
  321. ret = btrfs_block_rsv_check(trans, root,
  322. &root->fs_info->global_block_rsv, 0, 5);
  323. return ret ? 1 : 0;
  324. }
  325. int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
  326. struct btrfs_root *root)
  327. {
  328. struct btrfs_transaction *cur_trans = trans->transaction;
  329. int updates;
  330. if (cur_trans->blocked || cur_trans->delayed_refs.flushing)
  331. return 1;
  332. updates = trans->delayed_ref_updates;
  333. trans->delayed_ref_updates = 0;
  334. if (updates)
  335. btrfs_run_delayed_refs(trans, root, updates);
  336. return should_end_transaction(trans, root);
  337. }
  338. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  339. struct btrfs_root *root, int throttle, int lock)
  340. {
  341. struct btrfs_transaction *cur_trans = trans->transaction;
  342. struct btrfs_fs_info *info = root->fs_info;
  343. int count = 0;
  344. while (count < 4) {
  345. unsigned long cur = trans->delayed_ref_updates;
  346. trans->delayed_ref_updates = 0;
  347. if (cur &&
  348. trans->transaction->delayed_refs.num_heads_ready > 64) {
  349. trans->delayed_ref_updates = 0;
  350. /*
  351. * do a full flush if the transaction is trying
  352. * to close
  353. */
  354. if (trans->transaction->delayed_refs.flushing)
  355. cur = 0;
  356. btrfs_run_delayed_refs(trans, root, cur);
  357. } else {
  358. break;
  359. }
  360. count++;
  361. }
  362. btrfs_trans_release_metadata(trans, root);
  363. if (lock && !root->fs_info->open_ioctl_trans &&
  364. should_end_transaction(trans, root))
  365. trans->transaction->blocked = 1;
  366. if (lock && cur_trans->blocked && !cur_trans->in_commit) {
  367. if (throttle)
  368. return btrfs_commit_transaction(trans, root);
  369. else
  370. wake_up_process(info->transaction_kthread);
  371. }
  372. WARN_ON(cur_trans != info->running_transaction);
  373. WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
  374. atomic_dec(&cur_trans->num_writers);
  375. smp_mb();
  376. if (waitqueue_active(&cur_trans->writer_wait))
  377. wake_up(&cur_trans->writer_wait);
  378. put_transaction(cur_trans);
  379. if (current->journal_info == trans)
  380. current->journal_info = NULL;
  381. memset(trans, 0, sizeof(*trans));
  382. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  383. if (throttle)
  384. btrfs_run_delayed_iputs(root);
  385. return 0;
  386. }
  387. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  388. struct btrfs_root *root)
  389. {
  390. int ret;
  391. ret = __btrfs_end_transaction(trans, root, 0, 1);
  392. if (ret)
  393. return ret;
  394. return 0;
  395. }
  396. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  397. struct btrfs_root *root)
  398. {
  399. int ret;
  400. ret = __btrfs_end_transaction(trans, root, 1, 1);
  401. if (ret)
  402. return ret;
  403. return 0;
  404. }
  405. int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
  406. struct btrfs_root *root)
  407. {
  408. int ret;
  409. ret = __btrfs_end_transaction(trans, root, 0, 0);
  410. if (ret)
  411. return ret;
  412. return 0;
  413. }
  414. int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
  415. struct btrfs_root *root)
  416. {
  417. return __btrfs_end_transaction(trans, root, 1, 1);
  418. }
  419. /*
  420. * when btree blocks are allocated, they have some corresponding bits set for
  421. * them in one of two extent_io trees. This is used to make sure all of
  422. * those extents are sent to disk but does not wait on them
  423. */
  424. int btrfs_write_marked_extents(struct btrfs_root *root,
  425. struct extent_io_tree *dirty_pages, int mark)
  426. {
  427. int ret;
  428. int err = 0;
  429. int werr = 0;
  430. struct page *page;
  431. struct inode *btree_inode = root->fs_info->btree_inode;
  432. u64 start = 0;
  433. u64 end;
  434. unsigned long index;
  435. while (1) {
  436. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  437. mark);
  438. if (ret)
  439. break;
  440. while (start <= end) {
  441. cond_resched();
  442. index = start >> PAGE_CACHE_SHIFT;
  443. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  444. page = find_get_page(btree_inode->i_mapping, index);
  445. if (!page)
  446. continue;
  447. btree_lock_page_hook(page);
  448. if (!page->mapping) {
  449. unlock_page(page);
  450. page_cache_release(page);
  451. continue;
  452. }
  453. if (PageWriteback(page)) {
  454. if (PageDirty(page))
  455. wait_on_page_writeback(page);
  456. else {
  457. unlock_page(page);
  458. page_cache_release(page);
  459. continue;
  460. }
  461. }
  462. err = write_one_page(page, 0);
  463. if (err)
  464. werr = err;
  465. page_cache_release(page);
  466. }
  467. }
  468. if (err)
  469. werr = err;
  470. return werr;
  471. }
  472. /*
  473. * when btree blocks are allocated, they have some corresponding bits set for
  474. * them in one of two extent_io trees. This is used to make sure all of
  475. * those extents are on disk for transaction or log commit. We wait
  476. * on all the pages and clear them from the dirty pages state tree
  477. */
  478. int btrfs_wait_marked_extents(struct btrfs_root *root,
  479. struct extent_io_tree *dirty_pages, int mark)
  480. {
  481. int ret;
  482. int err = 0;
  483. int werr = 0;
  484. struct page *page;
  485. struct inode *btree_inode = root->fs_info->btree_inode;
  486. u64 start = 0;
  487. u64 end;
  488. unsigned long index;
  489. while (1) {
  490. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  491. mark);
  492. if (ret)
  493. break;
  494. clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
  495. while (start <= end) {
  496. index = start >> PAGE_CACHE_SHIFT;
  497. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  498. page = find_get_page(btree_inode->i_mapping, index);
  499. if (!page)
  500. continue;
  501. if (PageDirty(page)) {
  502. btree_lock_page_hook(page);
  503. wait_on_page_writeback(page);
  504. err = write_one_page(page, 0);
  505. if (err)
  506. werr = err;
  507. }
  508. wait_on_page_writeback(page);
  509. page_cache_release(page);
  510. cond_resched();
  511. }
  512. }
  513. if (err)
  514. werr = err;
  515. return werr;
  516. }
  517. /*
  518. * when btree blocks are allocated, they have some corresponding bits set for
  519. * them in one of two extent_io trees. This is used to make sure all of
  520. * those extents are on disk for transaction or log commit
  521. */
  522. int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
  523. struct extent_io_tree *dirty_pages, int mark)
  524. {
  525. int ret;
  526. int ret2;
  527. ret = btrfs_write_marked_extents(root, dirty_pages, mark);
  528. ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
  529. return ret || ret2;
  530. }
  531. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  532. struct btrfs_root *root)
  533. {
  534. if (!trans || !trans->transaction) {
  535. struct inode *btree_inode;
  536. btree_inode = root->fs_info->btree_inode;
  537. return filemap_write_and_wait(btree_inode->i_mapping);
  538. }
  539. return btrfs_write_and_wait_marked_extents(root,
  540. &trans->transaction->dirty_pages,
  541. EXTENT_DIRTY);
  542. }
  543. /*
  544. * this is used to update the root pointer in the tree of tree roots.
  545. *
  546. * But, in the case of the extent allocation tree, updating the root
  547. * pointer may allocate blocks which may change the root of the extent
  548. * allocation tree.
  549. *
  550. * So, this loops and repeats and makes sure the cowonly root didn't
  551. * change while the root pointer was being updated in the metadata.
  552. */
  553. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  554. struct btrfs_root *root)
  555. {
  556. int ret;
  557. u64 old_root_bytenr;
  558. u64 old_root_used;
  559. struct btrfs_root *tree_root = root->fs_info->tree_root;
  560. old_root_used = btrfs_root_used(&root->root_item);
  561. btrfs_write_dirty_block_groups(trans, root);
  562. while (1) {
  563. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  564. if (old_root_bytenr == root->node->start &&
  565. old_root_used == btrfs_root_used(&root->root_item))
  566. break;
  567. btrfs_set_root_node(&root->root_item, root->node);
  568. ret = btrfs_update_root(trans, tree_root,
  569. &root->root_key,
  570. &root->root_item);
  571. BUG_ON(ret);
  572. old_root_used = btrfs_root_used(&root->root_item);
  573. ret = btrfs_write_dirty_block_groups(trans, root);
  574. BUG_ON(ret);
  575. }
  576. if (root != root->fs_info->extent_root)
  577. switch_commit_root(root);
  578. return 0;
  579. }
  580. /*
  581. * update all the cowonly tree roots on disk
  582. */
  583. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
  584. struct btrfs_root *root)
  585. {
  586. struct btrfs_fs_info *fs_info = root->fs_info;
  587. struct list_head *next;
  588. struct extent_buffer *eb;
  589. int ret;
  590. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  591. BUG_ON(ret);
  592. eb = btrfs_lock_root_node(fs_info->tree_root);
  593. btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
  594. btrfs_tree_unlock(eb);
  595. free_extent_buffer(eb);
  596. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  597. BUG_ON(ret);
  598. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  599. next = fs_info->dirty_cowonly_roots.next;
  600. list_del_init(next);
  601. root = list_entry(next, struct btrfs_root, dirty_list);
  602. update_cowonly_root(trans, root);
  603. }
  604. down_write(&fs_info->extent_commit_sem);
  605. switch_commit_root(fs_info->extent_root);
  606. up_write(&fs_info->extent_commit_sem);
  607. return 0;
  608. }
  609. /*
  610. * dead roots are old snapshots that need to be deleted. This allocates
  611. * a dirty root struct and adds it into the list of dead roots that need to
  612. * be deleted
  613. */
  614. int btrfs_add_dead_root(struct btrfs_root *root)
  615. {
  616. mutex_lock(&root->fs_info->trans_mutex);
  617. list_add(&root->root_list, &root->fs_info->dead_roots);
  618. mutex_unlock(&root->fs_info->trans_mutex);
  619. return 0;
  620. }
  621. /*
  622. * update all the cowonly tree roots on disk
  623. */
  624. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
  625. struct btrfs_root *root)
  626. {
  627. struct btrfs_root *gang[8];
  628. struct btrfs_fs_info *fs_info = root->fs_info;
  629. int i;
  630. int ret;
  631. int err = 0;
  632. while (1) {
  633. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  634. (void **)gang, 0,
  635. ARRAY_SIZE(gang),
  636. BTRFS_ROOT_TRANS_TAG);
  637. if (ret == 0)
  638. break;
  639. for (i = 0; i < ret; i++) {
  640. root = gang[i];
  641. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  642. (unsigned long)root->root_key.objectid,
  643. BTRFS_ROOT_TRANS_TAG);
  644. btrfs_free_log(trans, root);
  645. btrfs_update_reloc_root(trans, root);
  646. btrfs_orphan_commit_root(trans, root);
  647. btrfs_save_ino_cache(root, trans);
  648. if (root->commit_root != root->node) {
  649. mutex_lock(&root->fs_commit_mutex);
  650. switch_commit_root(root);
  651. btrfs_unpin_free_ino(root);
  652. mutex_unlock(&root->fs_commit_mutex);
  653. btrfs_set_root_node(&root->root_item,
  654. root->node);
  655. }
  656. err = btrfs_update_root(trans, fs_info->tree_root,
  657. &root->root_key,
  658. &root->root_item);
  659. if (err)
  660. break;
  661. }
  662. }
  663. return err;
  664. }
  665. /*
  666. * defrag a given btree. If cacheonly == 1, this won't read from the disk,
  667. * otherwise every leaf in the btree is read and defragged.
  668. */
  669. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  670. {
  671. struct btrfs_fs_info *info = root->fs_info;
  672. struct btrfs_trans_handle *trans;
  673. int ret;
  674. unsigned long nr;
  675. if (xchg(&root->defrag_running, 1))
  676. return 0;
  677. while (1) {
  678. trans = btrfs_start_transaction(root, 0);
  679. if (IS_ERR(trans))
  680. return PTR_ERR(trans);
  681. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  682. nr = trans->blocks_used;
  683. btrfs_end_transaction(trans, root);
  684. btrfs_btree_balance_dirty(info->tree_root, nr);
  685. cond_resched();
  686. if (root->fs_info->closing || ret != -EAGAIN)
  687. break;
  688. }
  689. root->defrag_running = 0;
  690. return ret;
  691. }
  692. /*
  693. * new snapshots need to be created at a very specific time in the
  694. * transaction commit. This does the actual creation
  695. */
  696. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  697. struct btrfs_fs_info *fs_info,
  698. struct btrfs_pending_snapshot *pending)
  699. {
  700. struct btrfs_key key;
  701. struct btrfs_root_item *new_root_item;
  702. struct btrfs_root *tree_root = fs_info->tree_root;
  703. struct btrfs_root *root = pending->root;
  704. struct btrfs_root *parent_root;
  705. struct inode *parent_inode;
  706. struct dentry *parent;
  707. struct dentry *dentry;
  708. struct extent_buffer *tmp;
  709. struct extent_buffer *old;
  710. int ret;
  711. u64 to_reserve = 0;
  712. u64 index = 0;
  713. u64 objectid;
  714. u64 root_flags;
  715. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  716. if (!new_root_item) {
  717. pending->error = -ENOMEM;
  718. goto fail;
  719. }
  720. ret = btrfs_find_free_objectid(tree_root, &objectid);
  721. if (ret) {
  722. pending->error = ret;
  723. goto fail;
  724. }
  725. btrfs_reloc_pre_snapshot(trans, pending, &to_reserve);
  726. btrfs_orphan_pre_snapshot(trans, pending, &to_reserve);
  727. if (to_reserve > 0) {
  728. ret = btrfs_block_rsv_add(trans, root, &pending->block_rsv,
  729. to_reserve);
  730. if (ret) {
  731. pending->error = ret;
  732. goto fail;
  733. }
  734. }
  735. key.objectid = objectid;
  736. key.offset = (u64)-1;
  737. key.type = BTRFS_ROOT_ITEM_KEY;
  738. trans->block_rsv = &pending->block_rsv;
  739. dentry = pending->dentry;
  740. parent = dget_parent(dentry);
  741. parent_inode = parent->d_inode;
  742. parent_root = BTRFS_I(parent_inode)->root;
  743. record_root_in_trans(trans, parent_root);
  744. /*
  745. * insert the directory item
  746. */
  747. ret = btrfs_set_inode_index(parent_inode, &index);
  748. BUG_ON(ret);
  749. ret = btrfs_insert_dir_item(trans, parent_root,
  750. dentry->d_name.name, dentry->d_name.len,
  751. parent_inode, &key,
  752. BTRFS_FT_DIR, index);
  753. BUG_ON(ret);
  754. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  755. dentry->d_name.len * 2);
  756. ret = btrfs_update_inode(trans, parent_root, parent_inode);
  757. BUG_ON(ret);
  758. record_root_in_trans(trans, root);
  759. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  760. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  761. btrfs_check_and_init_root_item(new_root_item);
  762. root_flags = btrfs_root_flags(new_root_item);
  763. if (pending->readonly)
  764. root_flags |= BTRFS_ROOT_SUBVOL_RDONLY;
  765. else
  766. root_flags &= ~BTRFS_ROOT_SUBVOL_RDONLY;
  767. btrfs_set_root_flags(new_root_item, root_flags);
  768. old = btrfs_lock_root_node(root);
  769. btrfs_cow_block(trans, root, old, NULL, 0, &old);
  770. btrfs_set_lock_blocking(old);
  771. btrfs_copy_root(trans, root, old, &tmp, objectid);
  772. btrfs_tree_unlock(old);
  773. free_extent_buffer(old);
  774. btrfs_set_root_node(new_root_item, tmp);
  775. /* record when the snapshot was created in key.offset */
  776. key.offset = trans->transid;
  777. ret = btrfs_insert_root(trans, tree_root, &key, new_root_item);
  778. btrfs_tree_unlock(tmp);
  779. free_extent_buffer(tmp);
  780. BUG_ON(ret);
  781. /*
  782. * insert root back/forward references
  783. */
  784. ret = btrfs_add_root_ref(trans, tree_root, objectid,
  785. parent_root->root_key.objectid,
  786. btrfs_ino(parent_inode), index,
  787. dentry->d_name.name, dentry->d_name.len);
  788. BUG_ON(ret);
  789. dput(parent);
  790. key.offset = (u64)-1;
  791. pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key);
  792. BUG_ON(IS_ERR(pending->snap));
  793. btrfs_reloc_post_snapshot(trans, pending);
  794. btrfs_orphan_post_snapshot(trans, pending);
  795. fail:
  796. kfree(new_root_item);
  797. btrfs_block_rsv_release(root, &pending->block_rsv, (u64)-1);
  798. return 0;
  799. }
  800. /*
  801. * create all the snapshots we've scheduled for creation
  802. */
  803. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  804. struct btrfs_fs_info *fs_info)
  805. {
  806. struct btrfs_pending_snapshot *pending;
  807. struct list_head *head = &trans->transaction->pending_snapshots;
  808. int ret;
  809. list_for_each_entry(pending, head, list) {
  810. /*
  811. * We must deal with the delayed items before creating
  812. * snapshots, or we will create a snapthot with inconsistent
  813. * information.
  814. */
  815. ret = btrfs_run_delayed_items(trans, fs_info->fs_root);
  816. BUG_ON(ret);
  817. ret = create_pending_snapshot(trans, fs_info, pending);
  818. BUG_ON(ret);
  819. }
  820. return 0;
  821. }
  822. static void update_super_roots(struct btrfs_root *root)
  823. {
  824. struct btrfs_root_item *root_item;
  825. struct btrfs_super_block *super;
  826. super = &root->fs_info->super_copy;
  827. root_item = &root->fs_info->chunk_root->root_item;
  828. super->chunk_root = root_item->bytenr;
  829. super->chunk_root_generation = root_item->generation;
  830. super->chunk_root_level = root_item->level;
  831. root_item = &root->fs_info->tree_root->root_item;
  832. super->root = root_item->bytenr;
  833. super->generation = root_item->generation;
  834. super->root_level = root_item->level;
  835. if (super->cache_generation != 0 || btrfs_test_opt(root, SPACE_CACHE))
  836. super->cache_generation = root_item->generation;
  837. }
  838. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  839. {
  840. int ret = 0;
  841. spin_lock(&info->new_trans_lock);
  842. if (info->running_transaction)
  843. ret = info->running_transaction->in_commit;
  844. spin_unlock(&info->new_trans_lock);
  845. return ret;
  846. }
  847. int btrfs_transaction_blocked(struct btrfs_fs_info *info)
  848. {
  849. int ret = 0;
  850. spin_lock(&info->new_trans_lock);
  851. if (info->running_transaction)
  852. ret = info->running_transaction->blocked;
  853. spin_unlock(&info->new_trans_lock);
  854. return ret;
  855. }
  856. /*
  857. * wait for the current transaction commit to start and block subsequent
  858. * transaction joins
  859. */
  860. static void wait_current_trans_commit_start(struct btrfs_root *root,
  861. struct btrfs_transaction *trans)
  862. {
  863. DEFINE_WAIT(wait);
  864. if (trans->in_commit)
  865. return;
  866. while (1) {
  867. prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
  868. TASK_UNINTERRUPTIBLE);
  869. if (trans->in_commit) {
  870. finish_wait(&root->fs_info->transaction_blocked_wait,
  871. &wait);
  872. break;
  873. }
  874. mutex_unlock(&root->fs_info->trans_mutex);
  875. schedule();
  876. mutex_lock(&root->fs_info->trans_mutex);
  877. finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
  878. }
  879. }
  880. /*
  881. * wait for the current transaction to start and then become unblocked.
  882. * caller holds ref.
  883. */
  884. static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
  885. struct btrfs_transaction *trans)
  886. {
  887. DEFINE_WAIT(wait);
  888. if (trans->commit_done || (trans->in_commit && !trans->blocked))
  889. return;
  890. while (1) {
  891. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  892. TASK_UNINTERRUPTIBLE);
  893. if (trans->commit_done ||
  894. (trans->in_commit && !trans->blocked)) {
  895. finish_wait(&root->fs_info->transaction_wait,
  896. &wait);
  897. break;
  898. }
  899. mutex_unlock(&root->fs_info->trans_mutex);
  900. schedule();
  901. mutex_lock(&root->fs_info->trans_mutex);
  902. finish_wait(&root->fs_info->transaction_wait,
  903. &wait);
  904. }
  905. }
  906. /*
  907. * commit transactions asynchronously. once btrfs_commit_transaction_async
  908. * returns, any subsequent transaction will not be allowed to join.
  909. */
  910. struct btrfs_async_commit {
  911. struct btrfs_trans_handle *newtrans;
  912. struct btrfs_root *root;
  913. struct delayed_work work;
  914. };
  915. static void do_async_commit(struct work_struct *work)
  916. {
  917. struct btrfs_async_commit *ac =
  918. container_of(work, struct btrfs_async_commit, work.work);
  919. btrfs_commit_transaction(ac->newtrans, ac->root);
  920. kfree(ac);
  921. }
  922. int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
  923. struct btrfs_root *root,
  924. int wait_for_unblock)
  925. {
  926. struct btrfs_async_commit *ac;
  927. struct btrfs_transaction *cur_trans;
  928. ac = kmalloc(sizeof(*ac), GFP_NOFS);
  929. if (!ac)
  930. return -ENOMEM;
  931. INIT_DELAYED_WORK(&ac->work, do_async_commit);
  932. ac->root = root;
  933. ac->newtrans = btrfs_join_transaction(root, 0);
  934. if (IS_ERR(ac->newtrans)) {
  935. int err = PTR_ERR(ac->newtrans);
  936. kfree(ac);
  937. return err;
  938. }
  939. /* take transaction reference */
  940. mutex_lock(&root->fs_info->trans_mutex);
  941. cur_trans = trans->transaction;
  942. atomic_inc(&cur_trans->use_count);
  943. mutex_unlock(&root->fs_info->trans_mutex);
  944. btrfs_end_transaction(trans, root);
  945. schedule_delayed_work(&ac->work, 0);
  946. /* wait for transaction to start and unblock */
  947. mutex_lock(&root->fs_info->trans_mutex);
  948. if (wait_for_unblock)
  949. wait_current_trans_commit_start_and_unblock(root, cur_trans);
  950. else
  951. wait_current_trans_commit_start(root, cur_trans);
  952. put_transaction(cur_trans);
  953. mutex_unlock(&root->fs_info->trans_mutex);
  954. return 0;
  955. }
  956. /*
  957. * btrfs_transaction state sequence:
  958. * in_commit = 0, blocked = 0 (initial)
  959. * in_commit = 1, blocked = 1
  960. * blocked = 0
  961. * commit_done = 1
  962. */
  963. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  964. struct btrfs_root *root)
  965. {
  966. unsigned long joined = 0;
  967. struct btrfs_transaction *cur_trans;
  968. struct btrfs_transaction *prev_trans = NULL;
  969. DEFINE_WAIT(wait);
  970. int ret;
  971. int should_grow = 0;
  972. unsigned long now = get_seconds();
  973. int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
  974. btrfs_run_ordered_operations(root, 0);
  975. /* make a pass through all the delayed refs we have so far
  976. * any runnings procs may add more while we are here
  977. */
  978. ret = btrfs_run_delayed_refs(trans, root, 0);
  979. BUG_ON(ret);
  980. btrfs_trans_release_metadata(trans, root);
  981. cur_trans = trans->transaction;
  982. /*
  983. * set the flushing flag so procs in this transaction have to
  984. * start sending their work down.
  985. */
  986. cur_trans->delayed_refs.flushing = 1;
  987. ret = btrfs_run_delayed_refs(trans, root, 0);
  988. BUG_ON(ret);
  989. mutex_lock(&root->fs_info->trans_mutex);
  990. if (cur_trans->in_commit) {
  991. atomic_inc(&cur_trans->use_count);
  992. mutex_unlock(&root->fs_info->trans_mutex);
  993. btrfs_end_transaction(trans, root);
  994. ret = wait_for_commit(root, cur_trans);
  995. BUG_ON(ret);
  996. mutex_lock(&root->fs_info->trans_mutex);
  997. put_transaction(cur_trans);
  998. mutex_unlock(&root->fs_info->trans_mutex);
  999. return 0;
  1000. }
  1001. trans->transaction->in_commit = 1;
  1002. trans->transaction->blocked = 1;
  1003. wake_up(&root->fs_info->transaction_blocked_wait);
  1004. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  1005. prev_trans = list_entry(cur_trans->list.prev,
  1006. struct btrfs_transaction, list);
  1007. if (!prev_trans->commit_done) {
  1008. atomic_inc(&prev_trans->use_count);
  1009. mutex_unlock(&root->fs_info->trans_mutex);
  1010. wait_for_commit(root, prev_trans);
  1011. mutex_lock(&root->fs_info->trans_mutex);
  1012. put_transaction(prev_trans);
  1013. }
  1014. }
  1015. if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
  1016. should_grow = 1;
  1017. do {
  1018. int snap_pending = 0;
  1019. joined = cur_trans->num_joined;
  1020. if (!list_empty(&trans->transaction->pending_snapshots))
  1021. snap_pending = 1;
  1022. WARN_ON(cur_trans != trans->transaction);
  1023. mutex_unlock(&root->fs_info->trans_mutex);
  1024. if (flush_on_commit || snap_pending) {
  1025. btrfs_start_delalloc_inodes(root, 1);
  1026. ret = btrfs_wait_ordered_extents(root, 0, 1);
  1027. BUG_ON(ret);
  1028. }
  1029. ret = btrfs_run_delayed_items(trans, root);
  1030. BUG_ON(ret);
  1031. /*
  1032. * rename don't use btrfs_join_transaction, so, once we
  1033. * set the transaction to blocked above, we aren't going
  1034. * to get any new ordered operations. We can safely run
  1035. * it here and no for sure that nothing new will be added
  1036. * to the list
  1037. */
  1038. btrfs_run_ordered_operations(root, 1);
  1039. prepare_to_wait(&cur_trans->writer_wait, &wait,
  1040. TASK_UNINTERRUPTIBLE);
  1041. smp_mb();
  1042. if (atomic_read(&cur_trans->num_writers) > 1)
  1043. schedule_timeout(MAX_SCHEDULE_TIMEOUT);
  1044. else if (should_grow)
  1045. schedule_timeout(1);
  1046. mutex_lock(&root->fs_info->trans_mutex);
  1047. finish_wait(&cur_trans->writer_wait, &wait);
  1048. } while (atomic_read(&cur_trans->num_writers) > 1 ||
  1049. (should_grow && cur_trans->num_joined != joined));
  1050. ret = create_pending_snapshots(trans, root->fs_info);
  1051. BUG_ON(ret);
  1052. ret = btrfs_run_delayed_items(trans, root);
  1053. BUG_ON(ret);
  1054. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  1055. BUG_ON(ret);
  1056. WARN_ON(cur_trans != trans->transaction);
  1057. btrfs_scrub_pause(root);
  1058. /* btrfs_commit_tree_roots is responsible for getting the
  1059. * various roots consistent with each other. Every pointer
  1060. * in the tree of tree roots has to point to the most up to date
  1061. * root for every subvolume and other tree. So, we have to keep
  1062. * the tree logging code from jumping in and changing any
  1063. * of the trees.
  1064. *
  1065. * At this point in the commit, there can't be any tree-log
  1066. * writers, but a little lower down we drop the trans mutex
  1067. * and let new people in. By holding the tree_log_mutex
  1068. * from now until after the super is written, we avoid races
  1069. * with the tree-log code.
  1070. */
  1071. mutex_lock(&root->fs_info->tree_log_mutex);
  1072. ret = commit_fs_roots(trans, root);
  1073. BUG_ON(ret);
  1074. /* commit_fs_roots gets rid of all the tree log roots, it is now
  1075. * safe to free the root of tree log roots
  1076. */
  1077. btrfs_free_log_root_tree(trans, root->fs_info);
  1078. ret = commit_cowonly_roots(trans, root);
  1079. BUG_ON(ret);
  1080. btrfs_prepare_extent_commit(trans, root);
  1081. cur_trans = root->fs_info->running_transaction;
  1082. spin_lock(&root->fs_info->new_trans_lock);
  1083. root->fs_info->running_transaction = NULL;
  1084. spin_unlock(&root->fs_info->new_trans_lock);
  1085. btrfs_set_root_node(&root->fs_info->tree_root->root_item,
  1086. root->fs_info->tree_root->node);
  1087. switch_commit_root(root->fs_info->tree_root);
  1088. btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
  1089. root->fs_info->chunk_root->node);
  1090. switch_commit_root(root->fs_info->chunk_root);
  1091. update_super_roots(root);
  1092. if (!root->fs_info->log_root_recovering) {
  1093. btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
  1094. btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
  1095. }
  1096. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  1097. sizeof(root->fs_info->super_copy));
  1098. trans->transaction->blocked = 0;
  1099. wake_up(&root->fs_info->transaction_wait);
  1100. mutex_unlock(&root->fs_info->trans_mutex);
  1101. ret = btrfs_write_and_wait_transaction(trans, root);
  1102. BUG_ON(ret);
  1103. write_ctree_super(trans, root, 0);
  1104. /*
  1105. * the super is written, we can safely allow the tree-loggers
  1106. * to go about their business
  1107. */
  1108. mutex_unlock(&root->fs_info->tree_log_mutex);
  1109. btrfs_finish_extent_commit(trans, root);
  1110. mutex_lock(&root->fs_info->trans_mutex);
  1111. cur_trans->commit_done = 1;
  1112. root->fs_info->last_trans_committed = cur_trans->transid;
  1113. wake_up(&cur_trans->commit_wait);
  1114. list_del_init(&cur_trans->list);
  1115. put_transaction(cur_trans);
  1116. put_transaction(cur_trans);
  1117. trace_btrfs_transaction_commit(root);
  1118. mutex_unlock(&root->fs_info->trans_mutex);
  1119. btrfs_scrub_continue(root);
  1120. if (current->journal_info == trans)
  1121. current->journal_info = NULL;
  1122. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1123. if (current != root->fs_info->transaction_kthread)
  1124. btrfs_run_delayed_iputs(root);
  1125. return ret;
  1126. }
  1127. /*
  1128. * interface function to delete all the snapshots we have scheduled for deletion
  1129. */
  1130. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  1131. {
  1132. LIST_HEAD(list);
  1133. struct btrfs_fs_info *fs_info = root->fs_info;
  1134. mutex_lock(&fs_info->trans_mutex);
  1135. list_splice_init(&fs_info->dead_roots, &list);
  1136. mutex_unlock(&fs_info->trans_mutex);
  1137. while (!list_empty(&list)) {
  1138. root = list_entry(list.next, struct btrfs_root, root_list);
  1139. list_del(&root->root_list);
  1140. btrfs_kill_all_delayed_nodes(root);
  1141. if (btrfs_header_backref_rev(root->node) <
  1142. BTRFS_MIXED_BACKREF_REV)
  1143. btrfs_drop_snapshot(root, NULL, 0);
  1144. else
  1145. btrfs_drop_snapshot(root, NULL, 1);
  1146. }
  1147. return 0;
  1148. }