transaction.c 47 KB

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