transaction.c 42 KB

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