transaction.c 30 KB

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