transaction.c 36 KB

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