transaction.c 40 KB

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