transaction.c 30 KB

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