transaction.c 40 KB

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