transaction.c 46 KB

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