transaction.c 35 KB

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