transaction.c 37 KB

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