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