transaction.c 37 KB

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