transaction.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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/sched.h>
  20. #include <linux/writeback.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/blkdev.h>
  23. #include "ctree.h"
  24. #include "disk-io.h"
  25. #include "transaction.h"
  26. #include "locking.h"
  27. #include "tree-log.h"
  28. #define BTRFS_ROOT_TRANS_TAG 0
  29. static noinline void put_transaction(struct btrfs_transaction *transaction)
  30. {
  31. WARN_ON(transaction->use_count == 0);
  32. transaction->use_count--;
  33. if (transaction->use_count == 0) {
  34. list_del_init(&transaction->list);
  35. memset(transaction, 0, sizeof(*transaction));
  36. kmem_cache_free(btrfs_transaction_cachep, transaction);
  37. }
  38. }
  39. static noinline void switch_commit_root(struct btrfs_root *root)
  40. {
  41. free_extent_buffer(root->commit_root);
  42. root->commit_root = btrfs_root_node(root);
  43. }
  44. /*
  45. * either allocate a new transaction or hop into the existing one
  46. */
  47. static noinline int join_transaction(struct btrfs_root *root)
  48. {
  49. struct btrfs_transaction *cur_trans;
  50. cur_trans = root->fs_info->running_transaction;
  51. if (!cur_trans) {
  52. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  53. GFP_NOFS);
  54. BUG_ON(!cur_trans);
  55. root->fs_info->generation++;
  56. 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. 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_node = NULL;
  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. GFP_NOFS);
  78. spin_lock(&root->fs_info->new_trans_lock);
  79. root->fs_info->running_transaction = cur_trans;
  80. spin_unlock(&root->fs_info->new_trans_lock);
  81. } else {
  82. cur_trans->num_writers++;
  83. cur_trans->num_joined++;
  84. }
  85. return 0;
  86. }
  87. /*
  88. * this does all the record keeping required to make sure that a reference
  89. * counted root is properly recorded in a given transaction. This is required
  90. * to make sure the old root from before we joined the transaction is deleted
  91. * when the transaction commits
  92. */
  93. static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
  94. struct btrfs_root *root)
  95. {
  96. if (root->ref_cows && root->last_trans < trans->transid) {
  97. WARN_ON(root == root->fs_info->extent_root);
  98. WARN_ON(root->commit_root != root->node);
  99. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  100. (unsigned long)root->root_key.objectid,
  101. BTRFS_ROOT_TRANS_TAG);
  102. root->last_trans = trans->transid;
  103. btrfs_init_reloc_root(trans, root);
  104. }
  105. return 0;
  106. }
  107. int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
  108. struct btrfs_root *root)
  109. {
  110. if (!root->ref_cows)
  111. return 0;
  112. mutex_lock(&root->fs_info->trans_mutex);
  113. if (root->last_trans == trans->transid) {
  114. mutex_unlock(&root->fs_info->trans_mutex);
  115. return 0;
  116. }
  117. record_root_in_trans(trans, root);
  118. mutex_unlock(&root->fs_info->trans_mutex);
  119. return 0;
  120. }
  121. /* wait for commit against the current transaction to become unblocked
  122. * when this is done, it is safe to start a new transaction, but the current
  123. * transaction might not be fully on disk.
  124. */
  125. static void wait_current_trans(struct btrfs_root *root)
  126. {
  127. struct btrfs_transaction *cur_trans;
  128. cur_trans = root->fs_info->running_transaction;
  129. if (cur_trans && cur_trans->blocked) {
  130. DEFINE_WAIT(wait);
  131. cur_trans->use_count++;
  132. while (1) {
  133. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  134. TASK_UNINTERRUPTIBLE);
  135. if (cur_trans->blocked) {
  136. mutex_unlock(&root->fs_info->trans_mutex);
  137. schedule();
  138. mutex_lock(&root->fs_info->trans_mutex);
  139. finish_wait(&root->fs_info->transaction_wait,
  140. &wait);
  141. } else {
  142. finish_wait(&root->fs_info->transaction_wait,
  143. &wait);
  144. break;
  145. }
  146. }
  147. put_transaction(cur_trans);
  148. }
  149. }
  150. enum btrfs_trans_type {
  151. TRANS_START,
  152. TRANS_JOIN,
  153. TRANS_USERSPACE,
  154. };
  155. static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
  156. int num_blocks, int type)
  157. {
  158. struct btrfs_trans_handle *h =
  159. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  160. int ret;
  161. mutex_lock(&root->fs_info->trans_mutex);
  162. if (!root->fs_info->log_root_recovering &&
  163. ((type == TRANS_START && !root->fs_info->open_ioctl_trans) ||
  164. type == TRANS_USERSPACE))
  165. wait_current_trans(root);
  166. ret = join_transaction(root);
  167. BUG_ON(ret);
  168. h->transid = root->fs_info->running_transaction->transid;
  169. h->transaction = root->fs_info->running_transaction;
  170. h->blocks_reserved = num_blocks;
  171. h->blocks_used = 0;
  172. h->block_group = 0;
  173. h->alloc_exclude_nr = 0;
  174. h->alloc_exclude_start = 0;
  175. h->delayed_ref_updates = 0;
  176. if (!current->journal_info && type != TRANS_USERSPACE)
  177. current->journal_info = h;
  178. root->fs_info->running_transaction->use_count++;
  179. record_root_in_trans(h, root);
  180. mutex_unlock(&root->fs_info->trans_mutex);
  181. return h;
  182. }
  183. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  184. int num_blocks)
  185. {
  186. return start_transaction(root, num_blocks, TRANS_START);
  187. }
  188. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
  189. int num_blocks)
  190. {
  191. return start_transaction(root, num_blocks, TRANS_JOIN);
  192. }
  193. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
  194. int num_blocks)
  195. {
  196. return start_transaction(r, num_blocks, TRANS_USERSPACE);
  197. }
  198. /* wait for a transaction commit to be fully complete */
  199. static noinline int wait_for_commit(struct btrfs_root *root,
  200. struct btrfs_transaction *commit)
  201. {
  202. DEFINE_WAIT(wait);
  203. mutex_lock(&root->fs_info->trans_mutex);
  204. while (!commit->commit_done) {
  205. prepare_to_wait(&commit->commit_wait, &wait,
  206. TASK_UNINTERRUPTIBLE);
  207. if (commit->commit_done)
  208. break;
  209. mutex_unlock(&root->fs_info->trans_mutex);
  210. schedule();
  211. mutex_lock(&root->fs_info->trans_mutex);
  212. }
  213. mutex_unlock(&root->fs_info->trans_mutex);
  214. finish_wait(&commit->commit_wait, &wait);
  215. return 0;
  216. }
  217. #if 0
  218. /*
  219. * rate limit against the drop_snapshot code. This helps to slow down new
  220. * operations if the drop_snapshot code isn't able to keep up.
  221. */
  222. static void throttle_on_drops(struct btrfs_root *root)
  223. {
  224. struct btrfs_fs_info *info = root->fs_info;
  225. int harder_count = 0;
  226. harder:
  227. if (atomic_read(&info->throttles)) {
  228. DEFINE_WAIT(wait);
  229. int thr;
  230. thr = atomic_read(&info->throttle_gen);
  231. do {
  232. prepare_to_wait(&info->transaction_throttle,
  233. &wait, TASK_UNINTERRUPTIBLE);
  234. if (!atomic_read(&info->throttles)) {
  235. finish_wait(&info->transaction_throttle, &wait);
  236. break;
  237. }
  238. schedule();
  239. finish_wait(&info->transaction_throttle, &wait);
  240. } while (thr == atomic_read(&info->throttle_gen));
  241. harder_count++;
  242. if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
  243. harder_count < 2)
  244. goto harder;
  245. if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
  246. harder_count < 10)
  247. goto harder;
  248. if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
  249. harder_count < 20)
  250. goto harder;
  251. }
  252. }
  253. #endif
  254. void btrfs_throttle(struct btrfs_root *root)
  255. {
  256. mutex_lock(&root->fs_info->trans_mutex);
  257. if (!root->fs_info->open_ioctl_trans)
  258. wait_current_trans(root);
  259. mutex_unlock(&root->fs_info->trans_mutex);
  260. }
  261. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  262. struct btrfs_root *root, int throttle)
  263. {
  264. struct btrfs_transaction *cur_trans;
  265. struct btrfs_fs_info *info = root->fs_info;
  266. int count = 0;
  267. while (count < 4) {
  268. unsigned long cur = trans->delayed_ref_updates;
  269. trans->delayed_ref_updates = 0;
  270. if (cur &&
  271. trans->transaction->delayed_refs.num_heads_ready > 64) {
  272. trans->delayed_ref_updates = 0;
  273. /*
  274. * do a full flush if the transaction is trying
  275. * to close
  276. */
  277. if (trans->transaction->delayed_refs.flushing)
  278. cur = 0;
  279. btrfs_run_delayed_refs(trans, root, cur);
  280. } else {
  281. break;
  282. }
  283. count++;
  284. }
  285. mutex_lock(&info->trans_mutex);
  286. cur_trans = info->running_transaction;
  287. WARN_ON(cur_trans != trans->transaction);
  288. WARN_ON(cur_trans->num_writers < 1);
  289. cur_trans->num_writers--;
  290. if (waitqueue_active(&cur_trans->writer_wait))
  291. wake_up(&cur_trans->writer_wait);
  292. put_transaction(cur_trans);
  293. mutex_unlock(&info->trans_mutex);
  294. if (current->journal_info == trans)
  295. current->journal_info = NULL;
  296. memset(trans, 0, sizeof(*trans));
  297. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  298. if (throttle)
  299. btrfs_run_delayed_iputs(root);
  300. return 0;
  301. }
  302. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  303. struct btrfs_root *root)
  304. {
  305. return __btrfs_end_transaction(trans, root, 0);
  306. }
  307. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  308. struct btrfs_root *root)
  309. {
  310. return __btrfs_end_transaction(trans, root, 1);
  311. }
  312. /*
  313. * when btree blocks are allocated, they have some corresponding bits set for
  314. * them in one of two extent_io trees. This is used to make sure all of
  315. * those extents are sent to disk but does not wait on them
  316. */
  317. int btrfs_write_marked_extents(struct btrfs_root *root,
  318. struct extent_io_tree *dirty_pages, int mark)
  319. {
  320. int ret;
  321. int err = 0;
  322. int werr = 0;
  323. struct page *page;
  324. struct inode *btree_inode = root->fs_info->btree_inode;
  325. u64 start = 0;
  326. u64 end;
  327. unsigned long index;
  328. while (1) {
  329. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  330. mark);
  331. if (ret)
  332. break;
  333. while (start <= end) {
  334. cond_resched();
  335. index = start >> PAGE_CACHE_SHIFT;
  336. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  337. page = find_get_page(btree_inode->i_mapping, index);
  338. if (!page)
  339. continue;
  340. btree_lock_page_hook(page);
  341. if (!page->mapping) {
  342. unlock_page(page);
  343. page_cache_release(page);
  344. continue;
  345. }
  346. if (PageWriteback(page)) {
  347. if (PageDirty(page))
  348. wait_on_page_writeback(page);
  349. else {
  350. unlock_page(page);
  351. page_cache_release(page);
  352. continue;
  353. }
  354. }
  355. err = write_one_page(page, 0);
  356. if (err)
  357. werr = err;
  358. page_cache_release(page);
  359. }
  360. }
  361. if (err)
  362. werr = err;
  363. return werr;
  364. }
  365. /*
  366. * when btree blocks are allocated, they have some corresponding bits set for
  367. * them in one of two extent_io trees. This is used to make sure all of
  368. * those extents are on disk for transaction or log commit. We wait
  369. * on all the pages and clear them from the dirty pages state tree
  370. */
  371. int btrfs_wait_marked_extents(struct btrfs_root *root,
  372. struct extent_io_tree *dirty_pages, int mark)
  373. {
  374. int ret;
  375. int err = 0;
  376. int werr = 0;
  377. struct page *page;
  378. struct inode *btree_inode = root->fs_info->btree_inode;
  379. u64 start = 0;
  380. u64 end;
  381. unsigned long index;
  382. while (1) {
  383. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  384. mark);
  385. if (ret)
  386. break;
  387. clear_extent_bits(dirty_pages, start, end, mark, GFP_NOFS);
  388. while (start <= end) {
  389. index = start >> PAGE_CACHE_SHIFT;
  390. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  391. page = find_get_page(btree_inode->i_mapping, index);
  392. if (!page)
  393. continue;
  394. if (PageDirty(page)) {
  395. btree_lock_page_hook(page);
  396. wait_on_page_writeback(page);
  397. err = write_one_page(page, 0);
  398. if (err)
  399. werr = err;
  400. }
  401. wait_on_page_writeback(page);
  402. page_cache_release(page);
  403. cond_resched();
  404. }
  405. }
  406. if (err)
  407. werr = err;
  408. return werr;
  409. }
  410. /*
  411. * when btree blocks are allocated, they have some corresponding bits set for
  412. * them in one of two extent_io trees. This is used to make sure all of
  413. * those extents are on disk for transaction or log commit
  414. */
  415. int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
  416. struct extent_io_tree *dirty_pages, int mark)
  417. {
  418. int ret;
  419. int ret2;
  420. ret = btrfs_write_marked_extents(root, dirty_pages, mark);
  421. ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
  422. return ret || ret2;
  423. }
  424. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  425. struct btrfs_root *root)
  426. {
  427. if (!trans || !trans->transaction) {
  428. struct inode *btree_inode;
  429. btree_inode = root->fs_info->btree_inode;
  430. return filemap_write_and_wait(btree_inode->i_mapping);
  431. }
  432. return btrfs_write_and_wait_marked_extents(root,
  433. &trans->transaction->dirty_pages,
  434. EXTENT_DIRTY);
  435. }
  436. /*
  437. * this is used to update the root pointer in the tree of tree roots.
  438. *
  439. * But, in the case of the extent allocation tree, updating the root
  440. * pointer may allocate blocks which may change the root of the extent
  441. * allocation tree.
  442. *
  443. * So, this loops and repeats and makes sure the cowonly root didn't
  444. * change while the root pointer was being updated in the metadata.
  445. */
  446. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  447. struct btrfs_root *root)
  448. {
  449. int ret;
  450. u64 old_root_bytenr;
  451. u64 old_root_used;
  452. struct btrfs_root *tree_root = root->fs_info->tree_root;
  453. old_root_used = btrfs_root_used(&root->root_item);
  454. btrfs_write_dirty_block_groups(trans, root);
  455. while (1) {
  456. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  457. if (old_root_bytenr == root->node->start &&
  458. old_root_used == btrfs_root_used(&root->root_item))
  459. break;
  460. btrfs_set_root_node(&root->root_item, root->node);
  461. ret = btrfs_update_root(trans, tree_root,
  462. &root->root_key,
  463. &root->root_item);
  464. BUG_ON(ret);
  465. old_root_used = btrfs_root_used(&root->root_item);
  466. ret = btrfs_write_dirty_block_groups(trans, root);
  467. BUG_ON(ret);
  468. }
  469. if (root != root->fs_info->extent_root)
  470. switch_commit_root(root);
  471. return 0;
  472. }
  473. /*
  474. * update all the cowonly tree roots on disk
  475. */
  476. static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
  477. struct btrfs_root *root)
  478. {
  479. struct btrfs_fs_info *fs_info = root->fs_info;
  480. struct list_head *next;
  481. struct extent_buffer *eb;
  482. int ret;
  483. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  484. BUG_ON(ret);
  485. eb = btrfs_lock_root_node(fs_info->tree_root);
  486. btrfs_cow_block(trans, fs_info->tree_root, eb, NULL, 0, &eb);
  487. btrfs_tree_unlock(eb);
  488. free_extent_buffer(eb);
  489. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  490. BUG_ON(ret);
  491. while (!list_empty(&fs_info->dirty_cowonly_roots)) {
  492. next = fs_info->dirty_cowonly_roots.next;
  493. list_del_init(next);
  494. root = list_entry(next, struct btrfs_root, dirty_list);
  495. update_cowonly_root(trans, root);
  496. }
  497. down_write(&fs_info->extent_commit_sem);
  498. switch_commit_root(fs_info->extent_root);
  499. up_write(&fs_info->extent_commit_sem);
  500. return 0;
  501. }
  502. /*
  503. * dead roots are old snapshots that need to be deleted. This allocates
  504. * a dirty root struct and adds it into the list of dead roots that need to
  505. * be deleted
  506. */
  507. int btrfs_add_dead_root(struct btrfs_root *root)
  508. {
  509. mutex_lock(&root->fs_info->trans_mutex);
  510. list_add(&root->root_list, &root->fs_info->dead_roots);
  511. mutex_unlock(&root->fs_info->trans_mutex);
  512. return 0;
  513. }
  514. /*
  515. * update all the cowonly tree roots on disk
  516. */
  517. static noinline int commit_fs_roots(struct btrfs_trans_handle *trans,
  518. struct btrfs_root *root)
  519. {
  520. struct btrfs_root *gang[8];
  521. struct btrfs_fs_info *fs_info = root->fs_info;
  522. int i;
  523. int ret;
  524. int err = 0;
  525. while (1) {
  526. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  527. (void **)gang, 0,
  528. ARRAY_SIZE(gang),
  529. BTRFS_ROOT_TRANS_TAG);
  530. if (ret == 0)
  531. break;
  532. for (i = 0; i < ret; i++) {
  533. root = gang[i];
  534. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  535. (unsigned long)root->root_key.objectid,
  536. BTRFS_ROOT_TRANS_TAG);
  537. btrfs_free_log(trans, root);
  538. btrfs_update_reloc_root(trans, root);
  539. if (root->commit_root != root->node) {
  540. switch_commit_root(root);
  541. btrfs_set_root_node(&root->root_item,
  542. root->node);
  543. }
  544. err = btrfs_update_root(trans, fs_info->tree_root,
  545. &root->root_key,
  546. &root->root_item);
  547. if (err)
  548. break;
  549. }
  550. }
  551. return err;
  552. }
  553. /*
  554. * defrag a given btree. If cacheonly == 1, this won't read from the disk,
  555. * otherwise every leaf in the btree is read and defragged.
  556. */
  557. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  558. {
  559. struct btrfs_fs_info *info = root->fs_info;
  560. int ret;
  561. struct btrfs_trans_handle *trans;
  562. unsigned long nr;
  563. smp_mb();
  564. if (root->defrag_running)
  565. return 0;
  566. trans = btrfs_start_transaction(root, 1);
  567. while (1) {
  568. root->defrag_running = 1;
  569. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  570. nr = trans->blocks_used;
  571. btrfs_end_transaction(trans, root);
  572. btrfs_btree_balance_dirty(info->tree_root, nr);
  573. cond_resched();
  574. trans = btrfs_start_transaction(root, 1);
  575. if (root->fs_info->closing || ret != -EAGAIN)
  576. break;
  577. }
  578. root->defrag_running = 0;
  579. smp_mb();
  580. btrfs_end_transaction(trans, root);
  581. return 0;
  582. }
  583. #if 0
  584. /*
  585. * when dropping snapshots, we generate a ton of delayed refs, and it makes
  586. * sense not to join the transaction while it is trying to flush the current
  587. * queue of delayed refs out.
  588. *
  589. * This is used by the drop snapshot code only
  590. */
  591. static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
  592. {
  593. DEFINE_WAIT(wait);
  594. mutex_lock(&info->trans_mutex);
  595. while (info->running_transaction &&
  596. info->running_transaction->delayed_refs.flushing) {
  597. prepare_to_wait(&info->transaction_wait, &wait,
  598. TASK_UNINTERRUPTIBLE);
  599. mutex_unlock(&info->trans_mutex);
  600. schedule();
  601. mutex_lock(&info->trans_mutex);
  602. finish_wait(&info->transaction_wait, &wait);
  603. }
  604. mutex_unlock(&info->trans_mutex);
  605. return 0;
  606. }
  607. /*
  608. * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
  609. * all of them
  610. */
  611. int btrfs_drop_dead_root(struct btrfs_root *root)
  612. {
  613. struct btrfs_trans_handle *trans;
  614. struct btrfs_root *tree_root = root->fs_info->tree_root;
  615. unsigned long nr;
  616. int ret;
  617. while (1) {
  618. /*
  619. * we don't want to jump in and create a bunch of
  620. * delayed refs if the transaction is starting to close
  621. */
  622. wait_transaction_pre_flush(tree_root->fs_info);
  623. trans = btrfs_start_transaction(tree_root, 1);
  624. /*
  625. * we've joined a transaction, make sure it isn't
  626. * closing right now
  627. */
  628. if (trans->transaction->delayed_refs.flushing) {
  629. btrfs_end_transaction(trans, tree_root);
  630. continue;
  631. }
  632. ret = btrfs_drop_snapshot(trans, root);
  633. if (ret != -EAGAIN)
  634. break;
  635. ret = btrfs_update_root(trans, tree_root,
  636. &root->root_key,
  637. &root->root_item);
  638. if (ret)
  639. break;
  640. nr = trans->blocks_used;
  641. ret = btrfs_end_transaction(trans, tree_root);
  642. BUG_ON(ret);
  643. btrfs_btree_balance_dirty(tree_root, nr);
  644. cond_resched();
  645. }
  646. BUG_ON(ret);
  647. ret = btrfs_del_root(trans, tree_root, &root->root_key);
  648. BUG_ON(ret);
  649. nr = trans->blocks_used;
  650. ret = btrfs_end_transaction(trans, tree_root);
  651. BUG_ON(ret);
  652. free_extent_buffer(root->node);
  653. free_extent_buffer(root->commit_root);
  654. kfree(root);
  655. btrfs_btree_balance_dirty(tree_root, nr);
  656. return ret;
  657. }
  658. #endif
  659. /*
  660. * new snapshots need to be created at a very specific time in the
  661. * transaction commit. This does the actual creation
  662. */
  663. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  664. struct btrfs_fs_info *fs_info,
  665. struct btrfs_pending_snapshot *pending)
  666. {
  667. struct btrfs_key key;
  668. struct btrfs_root_item *new_root_item;
  669. struct btrfs_root *tree_root = fs_info->tree_root;
  670. struct btrfs_root *root = pending->root;
  671. struct extent_buffer *tmp;
  672. struct extent_buffer *old;
  673. int ret;
  674. u64 objectid;
  675. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  676. if (!new_root_item) {
  677. ret = -ENOMEM;
  678. goto fail;
  679. }
  680. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  681. if (ret)
  682. goto fail;
  683. record_root_in_trans(trans, root);
  684. btrfs_set_root_last_snapshot(&root->root_item, trans->transid);
  685. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  686. key.objectid = objectid;
  687. /* record when the snapshot was created in key.offset */
  688. key.offset = trans->transid;
  689. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  690. old = btrfs_lock_root_node(root);
  691. btrfs_cow_block(trans, root, old, NULL, 0, &old);
  692. btrfs_set_lock_blocking(old);
  693. btrfs_copy_root(trans, root, old, &tmp, objectid);
  694. btrfs_tree_unlock(old);
  695. free_extent_buffer(old);
  696. btrfs_set_root_node(new_root_item, tmp);
  697. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  698. new_root_item);
  699. btrfs_tree_unlock(tmp);
  700. free_extent_buffer(tmp);
  701. if (ret)
  702. goto fail;
  703. key.offset = (u64)-1;
  704. memcpy(&pending->root_key, &key, sizeof(key));
  705. fail:
  706. kfree(new_root_item);
  707. return ret;
  708. }
  709. static noinline int finish_pending_snapshot(struct btrfs_fs_info *fs_info,
  710. struct btrfs_pending_snapshot *pending)
  711. {
  712. int ret;
  713. int namelen;
  714. u64 index = 0;
  715. struct btrfs_trans_handle *trans;
  716. struct inode *parent_inode;
  717. struct btrfs_root *parent_root;
  718. parent_inode = pending->dentry->d_parent->d_inode;
  719. parent_root = BTRFS_I(parent_inode)->root;
  720. trans = btrfs_join_transaction(parent_root, 1);
  721. /*
  722. * insert the directory item
  723. */
  724. namelen = strlen(pending->name);
  725. ret = btrfs_set_inode_index(parent_inode, &index);
  726. ret = btrfs_insert_dir_item(trans, parent_root,
  727. pending->name, namelen,
  728. parent_inode->i_ino,
  729. &pending->root_key, BTRFS_FT_DIR, index);
  730. if (ret)
  731. goto fail;
  732. btrfs_i_size_write(parent_inode, parent_inode->i_size + namelen * 2);
  733. ret = btrfs_update_inode(trans, parent_root, parent_inode);
  734. BUG_ON(ret);
  735. ret = btrfs_add_root_ref(trans, parent_root->fs_info->tree_root,
  736. pending->root_key.objectid,
  737. parent_root->root_key.objectid,
  738. parent_inode->i_ino, index, pending->name,
  739. namelen);
  740. BUG_ON(ret);
  741. fail:
  742. btrfs_end_transaction(trans, fs_info->fs_root);
  743. return ret;
  744. }
  745. /*
  746. * create all the snapshots we've scheduled for creation
  747. */
  748. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  749. struct btrfs_fs_info *fs_info)
  750. {
  751. struct btrfs_pending_snapshot *pending;
  752. struct list_head *head = &trans->transaction->pending_snapshots;
  753. int ret;
  754. list_for_each_entry(pending, head, list) {
  755. ret = create_pending_snapshot(trans, fs_info, pending);
  756. BUG_ON(ret);
  757. }
  758. return 0;
  759. }
  760. static noinline int finish_pending_snapshots(struct btrfs_trans_handle *trans,
  761. struct btrfs_fs_info *fs_info)
  762. {
  763. struct btrfs_pending_snapshot *pending;
  764. struct list_head *head = &trans->transaction->pending_snapshots;
  765. int ret;
  766. while (!list_empty(head)) {
  767. pending = list_entry(head->next,
  768. struct btrfs_pending_snapshot, list);
  769. ret = finish_pending_snapshot(fs_info, pending);
  770. BUG_ON(ret);
  771. list_del(&pending->list);
  772. kfree(pending->name);
  773. kfree(pending);
  774. }
  775. return 0;
  776. }
  777. static void update_super_roots(struct btrfs_root *root)
  778. {
  779. struct btrfs_root_item *root_item;
  780. struct btrfs_super_block *super;
  781. super = &root->fs_info->super_copy;
  782. root_item = &root->fs_info->chunk_root->root_item;
  783. super->chunk_root = root_item->bytenr;
  784. super->chunk_root_generation = root_item->generation;
  785. super->chunk_root_level = root_item->level;
  786. root_item = &root->fs_info->tree_root->root_item;
  787. super->root = root_item->bytenr;
  788. super->generation = root_item->generation;
  789. super->root_level = root_item->level;
  790. }
  791. int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
  792. {
  793. int ret = 0;
  794. spin_lock(&info->new_trans_lock);
  795. if (info->running_transaction)
  796. ret = info->running_transaction->in_commit;
  797. spin_unlock(&info->new_trans_lock);
  798. return ret;
  799. }
  800. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  801. struct btrfs_root *root)
  802. {
  803. unsigned long joined = 0;
  804. unsigned long timeout = 1;
  805. struct btrfs_transaction *cur_trans;
  806. struct btrfs_transaction *prev_trans = NULL;
  807. DEFINE_WAIT(wait);
  808. int ret;
  809. int should_grow = 0;
  810. unsigned long now = get_seconds();
  811. int flush_on_commit = btrfs_test_opt(root, FLUSHONCOMMIT);
  812. btrfs_run_ordered_operations(root, 0);
  813. /* make a pass through all the delayed refs we have so far
  814. * any runnings procs may add more while we are here
  815. */
  816. ret = btrfs_run_delayed_refs(trans, root, 0);
  817. BUG_ON(ret);
  818. cur_trans = trans->transaction;
  819. /*
  820. * set the flushing flag so procs in this transaction have to
  821. * start sending their work down.
  822. */
  823. cur_trans->delayed_refs.flushing = 1;
  824. ret = btrfs_run_delayed_refs(trans, root, 0);
  825. BUG_ON(ret);
  826. mutex_lock(&root->fs_info->trans_mutex);
  827. if (cur_trans->in_commit) {
  828. cur_trans->use_count++;
  829. mutex_unlock(&root->fs_info->trans_mutex);
  830. btrfs_end_transaction(trans, root);
  831. ret = wait_for_commit(root, cur_trans);
  832. BUG_ON(ret);
  833. mutex_lock(&root->fs_info->trans_mutex);
  834. put_transaction(cur_trans);
  835. mutex_unlock(&root->fs_info->trans_mutex);
  836. return 0;
  837. }
  838. trans->transaction->in_commit = 1;
  839. trans->transaction->blocked = 1;
  840. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  841. prev_trans = list_entry(cur_trans->list.prev,
  842. struct btrfs_transaction, list);
  843. if (!prev_trans->commit_done) {
  844. prev_trans->use_count++;
  845. mutex_unlock(&root->fs_info->trans_mutex);
  846. wait_for_commit(root, prev_trans);
  847. mutex_lock(&root->fs_info->trans_mutex);
  848. put_transaction(prev_trans);
  849. }
  850. }
  851. if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
  852. should_grow = 1;
  853. do {
  854. int snap_pending = 0;
  855. joined = cur_trans->num_joined;
  856. if (!list_empty(&trans->transaction->pending_snapshots))
  857. snap_pending = 1;
  858. WARN_ON(cur_trans != trans->transaction);
  859. prepare_to_wait(&cur_trans->writer_wait, &wait,
  860. TASK_UNINTERRUPTIBLE);
  861. if (cur_trans->num_writers > 1)
  862. timeout = MAX_SCHEDULE_TIMEOUT;
  863. else if (should_grow)
  864. timeout = 1;
  865. mutex_unlock(&root->fs_info->trans_mutex);
  866. if (flush_on_commit) {
  867. btrfs_start_delalloc_inodes(root, 1);
  868. ret = btrfs_wait_ordered_extents(root, 0, 1);
  869. BUG_ON(ret);
  870. } else if (snap_pending) {
  871. ret = btrfs_wait_ordered_extents(root, 0, 1);
  872. BUG_ON(ret);
  873. }
  874. /*
  875. * rename don't use btrfs_join_transaction, so, once we
  876. * set the transaction to blocked above, we aren't going
  877. * to get any new ordered operations. We can safely run
  878. * it here and no for sure that nothing new will be added
  879. * to the list
  880. */
  881. btrfs_run_ordered_operations(root, 1);
  882. smp_mb();
  883. if (cur_trans->num_writers > 1 || should_grow)
  884. schedule_timeout(timeout);
  885. mutex_lock(&root->fs_info->trans_mutex);
  886. finish_wait(&cur_trans->writer_wait, &wait);
  887. } while (cur_trans->num_writers > 1 ||
  888. (should_grow && cur_trans->num_joined != joined));
  889. ret = create_pending_snapshots(trans, root->fs_info);
  890. BUG_ON(ret);
  891. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  892. BUG_ON(ret);
  893. WARN_ON(cur_trans != trans->transaction);
  894. /* btrfs_commit_tree_roots is responsible for getting the
  895. * various roots consistent with each other. Every pointer
  896. * in the tree of tree roots has to point to the most up to date
  897. * root for every subvolume and other tree. So, we have to keep
  898. * the tree logging code from jumping in and changing any
  899. * of the trees.
  900. *
  901. * At this point in the commit, there can't be any tree-log
  902. * writers, but a little lower down we drop the trans mutex
  903. * and let new people in. By holding the tree_log_mutex
  904. * from now until after the super is written, we avoid races
  905. * with the tree-log code.
  906. */
  907. mutex_lock(&root->fs_info->tree_log_mutex);
  908. ret = commit_fs_roots(trans, root);
  909. BUG_ON(ret);
  910. /* commit_fs_roots gets rid of all the tree log roots, it is now
  911. * safe to free the root of tree log roots
  912. */
  913. btrfs_free_log_root_tree(trans, root->fs_info);
  914. ret = commit_cowonly_roots(trans, root);
  915. BUG_ON(ret);
  916. btrfs_prepare_extent_commit(trans, root);
  917. cur_trans = root->fs_info->running_transaction;
  918. spin_lock(&root->fs_info->new_trans_lock);
  919. root->fs_info->running_transaction = NULL;
  920. spin_unlock(&root->fs_info->new_trans_lock);
  921. btrfs_set_root_node(&root->fs_info->tree_root->root_item,
  922. root->fs_info->tree_root->node);
  923. switch_commit_root(root->fs_info->tree_root);
  924. btrfs_set_root_node(&root->fs_info->chunk_root->root_item,
  925. root->fs_info->chunk_root->node);
  926. switch_commit_root(root->fs_info->chunk_root);
  927. update_super_roots(root);
  928. if (!root->fs_info->log_root_recovering) {
  929. btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
  930. btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
  931. }
  932. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  933. sizeof(root->fs_info->super_copy));
  934. trans->transaction->blocked = 0;
  935. wake_up(&root->fs_info->transaction_wait);
  936. mutex_unlock(&root->fs_info->trans_mutex);
  937. ret = btrfs_write_and_wait_transaction(trans, root);
  938. BUG_ON(ret);
  939. write_ctree_super(trans, root, 0);
  940. /*
  941. * the super is written, we can safely allow the tree-loggers
  942. * to go about their business
  943. */
  944. mutex_unlock(&root->fs_info->tree_log_mutex);
  945. btrfs_finish_extent_commit(trans, root);
  946. /* do the directory inserts of any pending snapshot creations */
  947. finish_pending_snapshots(trans, root->fs_info);
  948. mutex_lock(&root->fs_info->trans_mutex);
  949. cur_trans->commit_done = 1;
  950. root->fs_info->last_trans_committed = cur_trans->transid;
  951. wake_up(&cur_trans->commit_wait);
  952. put_transaction(cur_trans);
  953. put_transaction(cur_trans);
  954. mutex_unlock(&root->fs_info->trans_mutex);
  955. if (current->journal_info == trans)
  956. current->journal_info = NULL;
  957. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  958. if (current != root->fs_info->transaction_kthread)
  959. btrfs_run_delayed_iputs(root);
  960. return ret;
  961. }
  962. /*
  963. * interface function to delete all the snapshots we have scheduled for deletion
  964. */
  965. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  966. {
  967. LIST_HEAD(list);
  968. struct btrfs_fs_info *fs_info = root->fs_info;
  969. mutex_lock(&fs_info->trans_mutex);
  970. list_splice_init(&fs_info->dead_roots, &list);
  971. mutex_unlock(&fs_info->trans_mutex);
  972. while (!list_empty(&list)) {
  973. root = list_entry(list.next, struct btrfs_root, root_list);
  974. list_del(&root->root_list);
  975. if (btrfs_header_backref_rev(root->node) <
  976. BTRFS_MIXED_BACKREF_REV)
  977. btrfs_drop_snapshot(root, 0);
  978. else
  979. btrfs_drop_snapshot(root, 1);
  980. }
  981. return 0;
  982. }