transaction.c 47 KB

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