transaction.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 "ctree.h"
  23. #include "disk-io.h"
  24. #include "transaction.h"
  25. #include "locking.h"
  26. #include "ref-cache.h"
  27. #include "tree-log.h"
  28. static int total_trans = 0;
  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. WARN_ON(total_trans == 0);
  38. total_trans--;
  39. list_del_init(&transaction->list);
  40. memset(transaction, 0, sizeof(*transaction));
  41. kmem_cache_free(btrfs_transaction_cachep, transaction);
  42. }
  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. total_trans++;
  55. BUG_ON(!cur_trans);
  56. root->fs_info->generation++;
  57. root->fs_info->last_alloc = 0;
  58. root->fs_info->last_data_alloc = 0;
  59. root->fs_info->last_log_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_write_dirty_block_groups(trans, root);
  391. while(1) {
  392. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  393. if (old_root_bytenr == root->node->start)
  394. break;
  395. btrfs_set_root_bytenr(&root->root_item,
  396. root->node->start);
  397. btrfs_set_root_level(&root->root_item,
  398. btrfs_header_level(root->node));
  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. }
  405. return 0;
  406. }
  407. /*
  408. * update all the cowonly tree roots on disk
  409. */
  410. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  411. struct btrfs_root *root)
  412. {
  413. struct btrfs_fs_info *fs_info = root->fs_info;
  414. struct list_head *next;
  415. while(!list_empty(&fs_info->dirty_cowonly_roots)) {
  416. next = fs_info->dirty_cowonly_roots.next;
  417. list_del_init(next);
  418. root = list_entry(next, struct btrfs_root, dirty_list);
  419. update_cowonly_root(trans, root);
  420. }
  421. return 0;
  422. }
  423. /*
  424. * dead roots are old snapshots that need to be deleted. This allocates
  425. * a dirty root struct and adds it into the list of dead roots that need to
  426. * be deleted
  427. */
  428. int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
  429. {
  430. struct btrfs_dirty_root *dirty;
  431. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  432. if (!dirty)
  433. return -ENOMEM;
  434. dirty->root = root;
  435. dirty->latest_root = latest;
  436. mutex_lock(&root->fs_info->trans_mutex);
  437. list_add(&dirty->list, &latest->fs_info->dead_roots);
  438. mutex_unlock(&root->fs_info->trans_mutex);
  439. return 0;
  440. }
  441. /*
  442. * at transaction commit time we need to schedule the old roots for
  443. * deletion via btrfs_drop_snapshot. This runs through all the
  444. * reference counted roots that were modified in the current
  445. * transaction and puts them into the drop list
  446. */
  447. static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
  448. struct radix_tree_root *radix,
  449. struct list_head *list)
  450. {
  451. struct btrfs_dirty_root *dirty;
  452. struct btrfs_root *gang[8];
  453. struct btrfs_root *root;
  454. int i;
  455. int ret;
  456. int err = 0;
  457. u32 refs;
  458. while(1) {
  459. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  460. ARRAY_SIZE(gang),
  461. BTRFS_ROOT_TRANS_TAG);
  462. if (ret == 0)
  463. break;
  464. for (i = 0; i < ret; i++) {
  465. root = gang[i];
  466. radix_tree_tag_clear(radix,
  467. (unsigned long)root->root_key.objectid,
  468. BTRFS_ROOT_TRANS_TAG);
  469. BUG_ON(!root->ref_tree);
  470. dirty = root->dirty_root;
  471. btrfs_free_log(trans, root);
  472. btrfs_free_reloc_root(root);
  473. if (root->commit_root == root->node) {
  474. WARN_ON(root->node->start !=
  475. btrfs_root_bytenr(&root->root_item));
  476. free_extent_buffer(root->commit_root);
  477. root->commit_root = NULL;
  478. root->dirty_root = NULL;
  479. spin_lock(&root->list_lock);
  480. list_del_init(&dirty->root->dead_list);
  481. spin_unlock(&root->list_lock);
  482. kfree(dirty->root);
  483. kfree(dirty);
  484. /* make sure to update the root on disk
  485. * so we get any updates to the block used
  486. * counts
  487. */
  488. err = btrfs_update_root(trans,
  489. root->fs_info->tree_root,
  490. &root->root_key,
  491. &root->root_item);
  492. continue;
  493. }
  494. memset(&root->root_item.drop_progress, 0,
  495. sizeof(struct btrfs_disk_key));
  496. root->root_item.drop_level = 0;
  497. root->commit_root = NULL;
  498. root->dirty_root = NULL;
  499. root->root_key.offset = root->fs_info->generation;
  500. btrfs_set_root_bytenr(&root->root_item,
  501. root->node->start);
  502. btrfs_set_root_level(&root->root_item,
  503. btrfs_header_level(root->node));
  504. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  505. &root->root_key,
  506. &root->root_item);
  507. if (err)
  508. break;
  509. refs = btrfs_root_refs(&dirty->root->root_item);
  510. btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
  511. err = btrfs_update_root(trans, root->fs_info->tree_root,
  512. &dirty->root->root_key,
  513. &dirty->root->root_item);
  514. BUG_ON(err);
  515. if (refs == 1) {
  516. list_add(&dirty->list, list);
  517. } else {
  518. WARN_ON(1);
  519. free_extent_buffer(dirty->root->node);
  520. kfree(dirty->root);
  521. kfree(dirty);
  522. }
  523. }
  524. }
  525. return err;
  526. }
  527. /*
  528. * defrag a given btree. If cacheonly == 1, this won't read from the disk,
  529. * otherwise every leaf in the btree is read and defragged.
  530. */
  531. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  532. {
  533. struct btrfs_fs_info *info = root->fs_info;
  534. int ret;
  535. struct btrfs_trans_handle *trans;
  536. unsigned long nr;
  537. smp_mb();
  538. if (root->defrag_running)
  539. return 0;
  540. trans = btrfs_start_transaction(root, 1);
  541. while (1) {
  542. root->defrag_running = 1;
  543. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  544. nr = trans->blocks_used;
  545. btrfs_end_transaction(trans, root);
  546. btrfs_btree_balance_dirty(info->tree_root, nr);
  547. cond_resched();
  548. trans = btrfs_start_transaction(root, 1);
  549. if (root->fs_info->closing || ret != -EAGAIN)
  550. break;
  551. }
  552. root->defrag_running = 0;
  553. smp_mb();
  554. btrfs_end_transaction(trans, root);
  555. return 0;
  556. }
  557. /*
  558. * Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
  559. * all of them
  560. */
  561. static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
  562. struct list_head *list)
  563. {
  564. struct btrfs_dirty_root *dirty;
  565. struct btrfs_trans_handle *trans;
  566. unsigned long nr;
  567. u64 num_bytes;
  568. u64 bytes_used;
  569. u64 max_useless;
  570. int ret = 0;
  571. int err;
  572. while(!list_empty(list)) {
  573. struct btrfs_root *root;
  574. dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
  575. list_del_init(&dirty->list);
  576. num_bytes = btrfs_root_used(&dirty->root->root_item);
  577. root = dirty->latest_root;
  578. atomic_inc(&root->fs_info->throttles);
  579. while(1) {
  580. trans = btrfs_start_transaction(tree_root, 1);
  581. mutex_lock(&root->fs_info->drop_mutex);
  582. ret = btrfs_drop_snapshot(trans, dirty->root);
  583. if (ret != -EAGAIN) {
  584. break;
  585. }
  586. mutex_unlock(&root->fs_info->drop_mutex);
  587. err = btrfs_update_root(trans,
  588. tree_root,
  589. &dirty->root->root_key,
  590. &dirty->root->root_item);
  591. if (err)
  592. ret = err;
  593. nr = trans->blocks_used;
  594. ret = btrfs_end_transaction(trans, tree_root);
  595. BUG_ON(ret);
  596. btrfs_btree_balance_dirty(tree_root, nr);
  597. cond_resched();
  598. }
  599. BUG_ON(ret);
  600. atomic_dec(&root->fs_info->throttles);
  601. wake_up(&root->fs_info->transaction_throttle);
  602. mutex_lock(&root->fs_info->alloc_mutex);
  603. num_bytes -= btrfs_root_used(&dirty->root->root_item);
  604. bytes_used = btrfs_root_used(&root->root_item);
  605. if (num_bytes) {
  606. btrfs_record_root_in_trans(root);
  607. btrfs_set_root_used(&root->root_item,
  608. bytes_used - num_bytes);
  609. }
  610. mutex_unlock(&root->fs_info->alloc_mutex);
  611. ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
  612. if (ret) {
  613. BUG();
  614. break;
  615. }
  616. mutex_unlock(&root->fs_info->drop_mutex);
  617. spin_lock(&root->list_lock);
  618. list_del_init(&dirty->root->dead_list);
  619. if (!list_empty(&root->dead_list)) {
  620. struct btrfs_root *oldest;
  621. oldest = list_entry(root->dead_list.prev,
  622. struct btrfs_root, dead_list);
  623. max_useless = oldest->root_key.offset - 1;
  624. } else {
  625. max_useless = root->root_key.offset - 1;
  626. }
  627. spin_unlock(&root->list_lock);
  628. nr = trans->blocks_used;
  629. ret = btrfs_end_transaction(trans, tree_root);
  630. BUG_ON(ret);
  631. ret = btrfs_remove_leaf_refs(root, max_useless, 0);
  632. BUG_ON(ret);
  633. free_extent_buffer(dirty->root->node);
  634. kfree(dirty->root);
  635. kfree(dirty);
  636. btrfs_btree_balance_dirty(tree_root, nr);
  637. cond_resched();
  638. }
  639. return ret;
  640. }
  641. /*
  642. * new snapshots need to be created at a very specific time in the
  643. * transaction commit. This does the actual creation
  644. */
  645. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  646. struct btrfs_fs_info *fs_info,
  647. struct btrfs_pending_snapshot *pending)
  648. {
  649. struct btrfs_key key;
  650. struct btrfs_root_item *new_root_item;
  651. struct btrfs_root *tree_root = fs_info->tree_root;
  652. struct btrfs_root *root = pending->root;
  653. struct extent_buffer *tmp;
  654. struct extent_buffer *old;
  655. int ret;
  656. int namelen;
  657. u64 objectid;
  658. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  659. if (!new_root_item) {
  660. ret = -ENOMEM;
  661. goto fail;
  662. }
  663. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  664. if (ret)
  665. goto fail;
  666. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  667. key.objectid = objectid;
  668. key.offset = trans->transid;
  669. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  670. old = btrfs_lock_root_node(root);
  671. btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
  672. btrfs_copy_root(trans, root, old, &tmp, objectid);
  673. btrfs_tree_unlock(old);
  674. free_extent_buffer(old);
  675. btrfs_set_root_bytenr(new_root_item, tmp->start);
  676. btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
  677. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  678. new_root_item);
  679. btrfs_tree_unlock(tmp);
  680. free_extent_buffer(tmp);
  681. if (ret)
  682. goto fail;
  683. /*
  684. * insert the directory item
  685. */
  686. key.offset = (u64)-1;
  687. namelen = strlen(pending->name);
  688. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  689. pending->name, namelen,
  690. root->fs_info->sb->s_root->d_inode->i_ino,
  691. &key, BTRFS_FT_DIR, 0);
  692. if (ret)
  693. goto fail;
  694. ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
  695. pending->name, strlen(pending->name), objectid,
  696. root->fs_info->sb->s_root->d_inode->i_ino, 0);
  697. /* Invalidate existing dcache entry for new snapshot. */
  698. btrfs_invalidate_dcache_root(root, pending->name, namelen);
  699. fail:
  700. kfree(new_root_item);
  701. return ret;
  702. }
  703. /*
  704. * create all the snapshots we've scheduled for creation
  705. */
  706. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  707. struct btrfs_fs_info *fs_info)
  708. {
  709. struct btrfs_pending_snapshot *pending;
  710. struct list_head *head = &trans->transaction->pending_snapshots;
  711. int ret;
  712. while(!list_empty(head)) {
  713. pending = list_entry(head->next,
  714. struct btrfs_pending_snapshot, list);
  715. ret = create_pending_snapshot(trans, fs_info, pending);
  716. BUG_ON(ret);
  717. list_del(&pending->list);
  718. kfree(pending->name);
  719. kfree(pending);
  720. }
  721. return 0;
  722. }
  723. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  724. struct btrfs_root *root)
  725. {
  726. unsigned long joined = 0;
  727. unsigned long timeout = 1;
  728. struct btrfs_transaction *cur_trans;
  729. struct btrfs_transaction *prev_trans = NULL;
  730. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  731. struct list_head dirty_fs_roots;
  732. struct extent_io_tree *pinned_copy;
  733. DEFINE_WAIT(wait);
  734. int ret;
  735. INIT_LIST_HEAD(&dirty_fs_roots);
  736. mutex_lock(&root->fs_info->trans_mutex);
  737. if (trans->transaction->in_commit) {
  738. cur_trans = trans->transaction;
  739. trans->transaction->use_count++;
  740. mutex_unlock(&root->fs_info->trans_mutex);
  741. btrfs_end_transaction(trans, root);
  742. ret = wait_for_commit(root, cur_trans);
  743. BUG_ON(ret);
  744. mutex_lock(&root->fs_info->trans_mutex);
  745. put_transaction(cur_trans);
  746. mutex_unlock(&root->fs_info->trans_mutex);
  747. return 0;
  748. }
  749. pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
  750. if (!pinned_copy)
  751. return -ENOMEM;
  752. extent_io_tree_init(pinned_copy,
  753. root->fs_info->btree_inode->i_mapping, GFP_NOFS);
  754. trans->transaction->in_commit = 1;
  755. trans->transaction->blocked = 1;
  756. cur_trans = trans->transaction;
  757. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  758. prev_trans = list_entry(cur_trans->list.prev,
  759. struct btrfs_transaction, list);
  760. if (!prev_trans->commit_done) {
  761. prev_trans->use_count++;
  762. mutex_unlock(&root->fs_info->trans_mutex);
  763. wait_for_commit(root, prev_trans);
  764. mutex_lock(&root->fs_info->trans_mutex);
  765. put_transaction(prev_trans);
  766. }
  767. }
  768. do {
  769. int snap_pending = 0;
  770. joined = cur_trans->num_joined;
  771. if (!list_empty(&trans->transaction->pending_snapshots))
  772. snap_pending = 1;
  773. WARN_ON(cur_trans != trans->transaction);
  774. prepare_to_wait(&cur_trans->writer_wait, &wait,
  775. TASK_UNINTERRUPTIBLE);
  776. if (cur_trans->num_writers > 1)
  777. timeout = MAX_SCHEDULE_TIMEOUT;
  778. else
  779. timeout = 1;
  780. mutex_unlock(&root->fs_info->trans_mutex);
  781. if (snap_pending) {
  782. ret = btrfs_wait_ordered_extents(root, 1);
  783. BUG_ON(ret);
  784. }
  785. schedule_timeout(timeout);
  786. mutex_lock(&root->fs_info->trans_mutex);
  787. finish_wait(&cur_trans->writer_wait, &wait);
  788. } while (cur_trans->num_writers > 1 ||
  789. (cur_trans->num_joined != joined));
  790. ret = create_pending_snapshots(trans, root->fs_info);
  791. BUG_ON(ret);
  792. WARN_ON(cur_trans != trans->transaction);
  793. /* btrfs_commit_tree_roots is responsible for getting the
  794. * various roots consistent with each other. Every pointer
  795. * in the tree of tree roots has to point to the most up to date
  796. * root for every subvolume and other tree. So, we have to keep
  797. * the tree logging code from jumping in and changing any
  798. * of the trees.
  799. *
  800. * At this point in the commit, there can't be any tree-log
  801. * writers, but a little lower down we drop the trans mutex
  802. * and let new people in. By holding the tree_log_mutex
  803. * from now until after the super is written, we avoid races
  804. * with the tree-log code.
  805. */
  806. mutex_lock(&root->fs_info->tree_log_mutex);
  807. /*
  808. * keep tree reloc code from adding new reloc trees
  809. */
  810. mutex_lock(&root->fs_info->tree_reloc_mutex);
  811. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  812. &dirty_fs_roots);
  813. BUG_ON(ret);
  814. /* add_dirty_roots gets rid of all the tree log roots, it is now
  815. * safe to free the root of tree log roots
  816. */
  817. btrfs_free_log_root_tree(trans, root->fs_info);
  818. btrfs_free_reloc_mappings(root);
  819. ret = btrfs_commit_tree_roots(trans, root);
  820. BUG_ON(ret);
  821. cur_trans = root->fs_info->running_transaction;
  822. spin_lock(&root->fs_info->new_trans_lock);
  823. root->fs_info->running_transaction = NULL;
  824. spin_unlock(&root->fs_info->new_trans_lock);
  825. btrfs_set_super_generation(&root->fs_info->super_copy,
  826. cur_trans->transid);
  827. btrfs_set_super_root(&root->fs_info->super_copy,
  828. root->fs_info->tree_root->node->start);
  829. btrfs_set_super_root_level(&root->fs_info->super_copy,
  830. btrfs_header_level(root->fs_info->tree_root->node));
  831. btrfs_set_super_chunk_root(&root->fs_info->super_copy,
  832. chunk_root->node->start);
  833. btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
  834. btrfs_header_level(chunk_root->node));
  835. if (!root->fs_info->log_root_recovering) {
  836. btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
  837. btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
  838. }
  839. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  840. sizeof(root->fs_info->super_copy));
  841. btrfs_copy_pinned(root, pinned_copy);
  842. trans->transaction->blocked = 0;
  843. wake_up(&root->fs_info->transaction_throttle);
  844. wake_up(&root->fs_info->transaction_wait);
  845. mutex_unlock(&root->fs_info->trans_mutex);
  846. ret = btrfs_write_and_wait_transaction(trans, root);
  847. BUG_ON(ret);
  848. write_ctree_super(trans, root);
  849. /*
  850. * the super is written, we can safely allow the tree-loggers
  851. * to go about their business
  852. */
  853. mutex_unlock(&root->fs_info->tree_log_mutex);
  854. btrfs_finish_extent_commit(trans, root, pinned_copy);
  855. kfree(pinned_copy);
  856. btrfs_drop_dead_reloc_roots(root);
  857. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  858. mutex_lock(&root->fs_info->trans_mutex);
  859. cur_trans->commit_done = 1;
  860. root->fs_info->last_trans_committed = cur_trans->transid;
  861. wake_up(&cur_trans->commit_wait);
  862. put_transaction(cur_trans);
  863. put_transaction(cur_trans);
  864. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  865. if (root->fs_info->closing)
  866. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  867. mutex_unlock(&root->fs_info->trans_mutex);
  868. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  869. if (root->fs_info->closing) {
  870. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  871. }
  872. return ret;
  873. }
  874. /*
  875. * interface function to delete all the snapshots we have scheduled for deletion
  876. */
  877. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  878. {
  879. struct list_head dirty_roots;
  880. INIT_LIST_HEAD(&dirty_roots);
  881. again:
  882. mutex_lock(&root->fs_info->trans_mutex);
  883. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  884. mutex_unlock(&root->fs_info->trans_mutex);
  885. if (!list_empty(&dirty_roots)) {
  886. drop_dirty_roots(root, &dirty_roots);
  887. goto again;
  888. }
  889. return 0;
  890. }