transaction.c 39 KB

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