transaction.c 40 KB

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