transaction.c 42 KB

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