transaction.c 42 KB

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