transaction.c 38 KB

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