transaction.c 37 KB

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