transaction.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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. static int total_trans = 0;
  28. extern struct kmem_cache *btrfs_trans_handle_cachep;
  29. extern struct kmem_cache *btrfs_transaction_cachep;
  30. #define BTRFS_ROOT_TRANS_TAG 0
  31. static noinline void put_transaction(struct btrfs_transaction *transaction)
  32. {
  33. WARN_ON(transaction->use_count == 0);
  34. transaction->use_count--;
  35. if (transaction->use_count == 0) {
  36. WARN_ON(total_trans == 0);
  37. total_trans--;
  38. list_del_init(&transaction->list);
  39. memset(transaction, 0, sizeof(*transaction));
  40. kmem_cache_free(btrfs_transaction_cachep, transaction);
  41. }
  42. }
  43. static noinline int join_transaction(struct btrfs_root *root)
  44. {
  45. struct btrfs_transaction *cur_trans;
  46. cur_trans = root->fs_info->running_transaction;
  47. if (!cur_trans) {
  48. cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
  49. GFP_NOFS);
  50. total_trans++;
  51. BUG_ON(!cur_trans);
  52. root->fs_info->generation++;
  53. root->fs_info->last_alloc = 0;
  54. root->fs_info->last_data_alloc = 0;
  55. cur_trans->num_writers = 1;
  56. cur_trans->num_joined = 0;
  57. cur_trans->transid = root->fs_info->generation;
  58. init_waitqueue_head(&cur_trans->writer_wait);
  59. init_waitqueue_head(&cur_trans->commit_wait);
  60. cur_trans->in_commit = 0;
  61. cur_trans->blocked = 0;
  62. cur_trans->use_count = 1;
  63. cur_trans->commit_done = 0;
  64. cur_trans->start_time = get_seconds();
  65. INIT_LIST_HEAD(&cur_trans->pending_snapshots);
  66. list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
  67. extent_io_tree_init(&cur_trans->dirty_pages,
  68. root->fs_info->btree_inode->i_mapping,
  69. GFP_NOFS);
  70. spin_lock(&root->fs_info->new_trans_lock);
  71. root->fs_info->running_transaction = cur_trans;
  72. spin_unlock(&root->fs_info->new_trans_lock);
  73. } else {
  74. cur_trans->num_writers++;
  75. cur_trans->num_joined++;
  76. }
  77. return 0;
  78. }
  79. static noinline int record_root_in_trans(struct btrfs_root *root)
  80. {
  81. struct btrfs_dirty_root *dirty;
  82. u64 running_trans_id = root->fs_info->running_transaction->transid;
  83. if (root->ref_cows && root->last_trans < running_trans_id) {
  84. WARN_ON(root == root->fs_info->extent_root);
  85. if (root->root_item.refs != 0) {
  86. radix_tree_tag_set(&root->fs_info->fs_roots_radix,
  87. (unsigned long)root->root_key.objectid,
  88. BTRFS_ROOT_TRANS_TAG);
  89. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  90. BUG_ON(!dirty);
  91. dirty->root = kmalloc(sizeof(*dirty->root), GFP_NOFS);
  92. BUG_ON(!dirty->root);
  93. dirty->latest_root = root;
  94. INIT_LIST_HEAD(&dirty->list);
  95. root->commit_root = btrfs_root_node(root);
  96. memcpy(dirty->root, root, sizeof(*root));
  97. spin_lock_init(&dirty->root->node_lock);
  98. spin_lock_init(&dirty->root->list_lock);
  99. mutex_init(&dirty->root->objectid_mutex);
  100. INIT_LIST_HEAD(&dirty->root->dead_list);
  101. dirty->root->node = root->commit_root;
  102. dirty->root->commit_root = NULL;
  103. spin_lock(&root->list_lock);
  104. list_add(&dirty->root->dead_list, &root->dead_list);
  105. spin_unlock(&root->list_lock);
  106. root->dirty_root = dirty;
  107. } else {
  108. WARN_ON(1);
  109. }
  110. root->last_trans = running_trans_id;
  111. }
  112. return 0;
  113. }
  114. static void wait_current_trans(struct btrfs_root *root)
  115. {
  116. struct btrfs_transaction *cur_trans;
  117. cur_trans = root->fs_info->running_transaction;
  118. if (cur_trans && cur_trans->blocked) {
  119. DEFINE_WAIT(wait);
  120. cur_trans->use_count++;
  121. while(1) {
  122. prepare_to_wait(&root->fs_info->transaction_wait, &wait,
  123. TASK_UNINTERRUPTIBLE);
  124. if (cur_trans->blocked) {
  125. mutex_unlock(&root->fs_info->trans_mutex);
  126. schedule();
  127. mutex_lock(&root->fs_info->trans_mutex);
  128. finish_wait(&root->fs_info->transaction_wait,
  129. &wait);
  130. } else {
  131. finish_wait(&root->fs_info->transaction_wait,
  132. &wait);
  133. break;
  134. }
  135. }
  136. put_transaction(cur_trans);
  137. }
  138. }
  139. struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
  140. int num_blocks, int wait)
  141. {
  142. struct btrfs_trans_handle *h =
  143. kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
  144. int ret;
  145. mutex_lock(&root->fs_info->trans_mutex);
  146. if ((wait == 1 && !root->fs_info->open_ioctl_trans) || wait == 2)
  147. wait_current_trans(root);
  148. ret = join_transaction(root);
  149. BUG_ON(ret);
  150. record_root_in_trans(root);
  151. h->transid = root->fs_info->running_transaction->transid;
  152. h->transaction = root->fs_info->running_transaction;
  153. h->blocks_reserved = num_blocks;
  154. h->blocks_used = 0;
  155. h->block_group = NULL;
  156. h->alloc_exclude_nr = 0;
  157. h->alloc_exclude_start = 0;
  158. root->fs_info->running_transaction->use_count++;
  159. mutex_unlock(&root->fs_info->trans_mutex);
  160. return h;
  161. }
  162. struct btrfs_trans_handle *btrfs_start_transaction(struct btrfs_root *root,
  163. int num_blocks)
  164. {
  165. return start_transaction(root, num_blocks, 1);
  166. }
  167. struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
  168. int num_blocks)
  169. {
  170. return start_transaction(root, num_blocks, 0);
  171. }
  172. struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
  173. int num_blocks)
  174. {
  175. return start_transaction(r, num_blocks, 2);
  176. }
  177. static noinline int wait_for_commit(struct btrfs_root *root,
  178. struct btrfs_transaction *commit)
  179. {
  180. DEFINE_WAIT(wait);
  181. mutex_lock(&root->fs_info->trans_mutex);
  182. while(!commit->commit_done) {
  183. prepare_to_wait(&commit->commit_wait, &wait,
  184. TASK_UNINTERRUPTIBLE);
  185. if (commit->commit_done)
  186. break;
  187. mutex_unlock(&root->fs_info->trans_mutex);
  188. schedule();
  189. mutex_lock(&root->fs_info->trans_mutex);
  190. }
  191. mutex_unlock(&root->fs_info->trans_mutex);
  192. finish_wait(&commit->commit_wait, &wait);
  193. return 0;
  194. }
  195. static void throttle_on_drops(struct btrfs_root *root)
  196. {
  197. struct btrfs_fs_info *info = root->fs_info;
  198. int harder_count = 0;
  199. harder:
  200. if (atomic_read(&info->throttles)) {
  201. DEFINE_WAIT(wait);
  202. int thr;
  203. thr = atomic_read(&info->throttle_gen);
  204. do {
  205. prepare_to_wait(&info->transaction_throttle,
  206. &wait, TASK_UNINTERRUPTIBLE);
  207. if (!atomic_read(&info->throttles)) {
  208. finish_wait(&info->transaction_throttle, &wait);
  209. break;
  210. }
  211. schedule();
  212. finish_wait(&info->transaction_throttle, &wait);
  213. } while (thr == atomic_read(&info->throttle_gen));
  214. harder_count++;
  215. if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
  216. harder_count < 2)
  217. goto harder;
  218. if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
  219. harder_count < 10)
  220. goto harder;
  221. if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
  222. harder_count < 20)
  223. goto harder;
  224. }
  225. }
  226. void btrfs_throttle(struct btrfs_root *root)
  227. {
  228. mutex_lock(&root->fs_info->trans_mutex);
  229. if (!root->fs_info->open_ioctl_trans)
  230. wait_current_trans(root);
  231. mutex_unlock(&root->fs_info->trans_mutex);
  232. throttle_on_drops(root);
  233. }
  234. static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
  235. struct btrfs_root *root, int throttle)
  236. {
  237. struct btrfs_transaction *cur_trans;
  238. struct btrfs_fs_info *info = root->fs_info;
  239. mutex_lock(&info->trans_mutex);
  240. cur_trans = info->running_transaction;
  241. WARN_ON(cur_trans != trans->transaction);
  242. WARN_ON(cur_trans->num_writers < 1);
  243. cur_trans->num_writers--;
  244. if (waitqueue_active(&cur_trans->writer_wait))
  245. wake_up(&cur_trans->writer_wait);
  246. put_transaction(cur_trans);
  247. mutex_unlock(&info->trans_mutex);
  248. memset(trans, 0, sizeof(*trans));
  249. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  250. if (throttle)
  251. throttle_on_drops(root);
  252. return 0;
  253. }
  254. int btrfs_end_transaction(struct btrfs_trans_handle *trans,
  255. struct btrfs_root *root)
  256. {
  257. return __btrfs_end_transaction(trans, root, 0);
  258. }
  259. int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
  260. struct btrfs_root *root)
  261. {
  262. return __btrfs_end_transaction(trans, root, 1);
  263. }
  264. int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
  265. struct btrfs_root *root)
  266. {
  267. int ret;
  268. int err = 0;
  269. int werr = 0;
  270. struct extent_io_tree *dirty_pages;
  271. struct page *page;
  272. struct inode *btree_inode = root->fs_info->btree_inode;
  273. u64 start = 0;
  274. u64 end;
  275. unsigned long index;
  276. if (!trans || !trans->transaction) {
  277. return filemap_write_and_wait(btree_inode->i_mapping);
  278. }
  279. dirty_pages = &trans->transaction->dirty_pages;
  280. while(1) {
  281. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  282. EXTENT_DIRTY);
  283. if (ret)
  284. break;
  285. while(start <= end) {
  286. if (btrfs_congested_async(root->fs_info, 0))
  287. congestion_wait(WRITE, HZ/10);
  288. cond_resched();
  289. index = start >> PAGE_CACHE_SHIFT;
  290. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  291. page = find_lock_page(btree_inode->i_mapping, index);
  292. if (!page)
  293. continue;
  294. if (PageWriteback(page)) {
  295. if (PageDirty(page))
  296. wait_on_page_writeback(page);
  297. else {
  298. unlock_page(page);
  299. page_cache_release(page);
  300. continue;
  301. }
  302. }
  303. err = write_one_page(page, 0);
  304. if (err)
  305. werr = err;
  306. page_cache_release(page);
  307. }
  308. }
  309. while(1) {
  310. ret = find_first_extent_bit(dirty_pages, 0, &start, &end,
  311. EXTENT_DIRTY);
  312. if (ret)
  313. break;
  314. clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
  315. while(start <= end) {
  316. index = start >> PAGE_CACHE_SHIFT;
  317. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  318. page = find_get_page(btree_inode->i_mapping, index);
  319. if (!page)
  320. continue;
  321. if (PageDirty(page)) {
  322. lock_page(page);
  323. err = write_one_page(page, 0);
  324. if (err)
  325. werr = err;
  326. }
  327. wait_on_page_writeback(page);
  328. page_cache_release(page);
  329. cond_resched();
  330. }
  331. }
  332. if (err)
  333. werr = err;
  334. return werr;
  335. }
  336. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  337. struct btrfs_root *root)
  338. {
  339. int ret;
  340. u64 old_root_bytenr;
  341. struct btrfs_root *tree_root = root->fs_info->tree_root;
  342. btrfs_write_dirty_block_groups(trans, root);
  343. while(1) {
  344. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  345. if (old_root_bytenr == root->node->start)
  346. break;
  347. btrfs_set_root_bytenr(&root->root_item,
  348. root->node->start);
  349. btrfs_set_root_level(&root->root_item,
  350. btrfs_header_level(root->node));
  351. ret = btrfs_update_root(trans, tree_root,
  352. &root->root_key,
  353. &root->root_item);
  354. BUG_ON(ret);
  355. btrfs_write_dirty_block_groups(trans, root);
  356. }
  357. return 0;
  358. }
  359. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  360. struct btrfs_root *root)
  361. {
  362. struct btrfs_fs_info *fs_info = root->fs_info;
  363. struct list_head *next;
  364. while(!list_empty(&fs_info->dirty_cowonly_roots)) {
  365. next = fs_info->dirty_cowonly_roots.next;
  366. list_del_init(next);
  367. root = list_entry(next, struct btrfs_root, dirty_list);
  368. update_cowonly_root(trans, root);
  369. }
  370. return 0;
  371. }
  372. int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
  373. {
  374. struct btrfs_dirty_root *dirty;
  375. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  376. if (!dirty)
  377. return -ENOMEM;
  378. dirty->root = root;
  379. dirty->latest_root = latest;
  380. mutex_lock(&root->fs_info->trans_mutex);
  381. list_add(&dirty->list, &latest->fs_info->dead_roots);
  382. mutex_unlock(&root->fs_info->trans_mutex);
  383. return 0;
  384. }
  385. static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
  386. struct radix_tree_root *radix,
  387. struct list_head *list)
  388. {
  389. struct btrfs_dirty_root *dirty;
  390. struct btrfs_root *gang[8];
  391. struct btrfs_root *root;
  392. int i;
  393. int ret;
  394. int err = 0;
  395. u32 refs;
  396. while(1) {
  397. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  398. ARRAY_SIZE(gang),
  399. BTRFS_ROOT_TRANS_TAG);
  400. if (ret == 0)
  401. break;
  402. for (i = 0; i < ret; i++) {
  403. root = gang[i];
  404. radix_tree_tag_clear(radix,
  405. (unsigned long)root->root_key.objectid,
  406. BTRFS_ROOT_TRANS_TAG);
  407. BUG_ON(!root->ref_tree);
  408. dirty = root->dirty_root;
  409. if (root->commit_root == root->node) {
  410. WARN_ON(root->node->start !=
  411. btrfs_root_bytenr(&root->root_item));
  412. free_extent_buffer(root->commit_root);
  413. root->commit_root = NULL;
  414. root->dirty_root = NULL;
  415. spin_lock(&root->list_lock);
  416. list_del_init(&dirty->root->dead_list);
  417. spin_unlock(&root->list_lock);
  418. kfree(dirty->root);
  419. kfree(dirty);
  420. /* make sure to update the root on disk
  421. * so we get any updates to the block used
  422. * counts
  423. */
  424. err = btrfs_update_root(trans,
  425. root->fs_info->tree_root,
  426. &root->root_key,
  427. &root->root_item);
  428. continue;
  429. }
  430. memset(&root->root_item.drop_progress, 0,
  431. sizeof(struct btrfs_disk_key));
  432. root->root_item.drop_level = 0;
  433. root->commit_root = NULL;
  434. root->dirty_root = NULL;
  435. root->root_key.offset = root->fs_info->generation;
  436. btrfs_set_root_bytenr(&root->root_item,
  437. root->node->start);
  438. btrfs_set_root_level(&root->root_item,
  439. btrfs_header_level(root->node));
  440. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  441. &root->root_key,
  442. &root->root_item);
  443. if (err)
  444. break;
  445. refs = btrfs_root_refs(&dirty->root->root_item);
  446. btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
  447. err = btrfs_update_root(trans, root->fs_info->tree_root,
  448. &dirty->root->root_key,
  449. &dirty->root->root_item);
  450. BUG_ON(err);
  451. if (refs == 1) {
  452. list_add(&dirty->list, list);
  453. } else {
  454. WARN_ON(1);
  455. free_extent_buffer(dirty->root->node);
  456. kfree(dirty->root);
  457. kfree(dirty);
  458. }
  459. }
  460. }
  461. return err;
  462. }
  463. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  464. {
  465. struct btrfs_fs_info *info = root->fs_info;
  466. int ret;
  467. struct btrfs_trans_handle *trans;
  468. unsigned long nr;
  469. smp_mb();
  470. if (root->defrag_running)
  471. return 0;
  472. trans = btrfs_start_transaction(root, 1);
  473. while (1) {
  474. root->defrag_running = 1;
  475. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  476. nr = trans->blocks_used;
  477. btrfs_end_transaction(trans, root);
  478. btrfs_btree_balance_dirty(info->tree_root, nr);
  479. cond_resched();
  480. trans = btrfs_start_transaction(root, 1);
  481. if (root->fs_info->closing || ret != -EAGAIN)
  482. break;
  483. }
  484. root->defrag_running = 0;
  485. smp_mb();
  486. btrfs_end_transaction(trans, root);
  487. return 0;
  488. }
  489. static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
  490. struct list_head *list)
  491. {
  492. struct btrfs_dirty_root *dirty;
  493. struct btrfs_trans_handle *trans;
  494. unsigned long nr;
  495. u64 num_bytes;
  496. u64 bytes_used;
  497. u64 max_useless;
  498. int ret = 0;
  499. int err;
  500. while(!list_empty(list)) {
  501. struct btrfs_root *root;
  502. dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
  503. list_del_init(&dirty->list);
  504. num_bytes = btrfs_root_used(&dirty->root->root_item);
  505. root = dirty->latest_root;
  506. atomic_inc(&root->fs_info->throttles);
  507. mutex_lock(&root->fs_info->drop_mutex);
  508. while(1) {
  509. trans = btrfs_start_transaction(tree_root, 1);
  510. ret = btrfs_drop_snapshot(trans, dirty->root);
  511. if (ret != -EAGAIN) {
  512. break;
  513. }
  514. err = btrfs_update_root(trans,
  515. tree_root,
  516. &dirty->root->root_key,
  517. &dirty->root->root_item);
  518. if (err)
  519. ret = err;
  520. nr = trans->blocks_used;
  521. ret = btrfs_end_transaction(trans, tree_root);
  522. BUG_ON(ret);
  523. mutex_unlock(&root->fs_info->drop_mutex);
  524. btrfs_btree_balance_dirty(tree_root, nr);
  525. cond_resched();
  526. mutex_lock(&root->fs_info->drop_mutex);
  527. }
  528. BUG_ON(ret);
  529. atomic_dec(&root->fs_info->throttles);
  530. wake_up(&root->fs_info->transaction_throttle);
  531. mutex_lock(&root->fs_info->alloc_mutex);
  532. num_bytes -= btrfs_root_used(&dirty->root->root_item);
  533. bytes_used = btrfs_root_used(&root->root_item);
  534. if (num_bytes) {
  535. record_root_in_trans(root);
  536. btrfs_set_root_used(&root->root_item,
  537. bytes_used - num_bytes);
  538. }
  539. mutex_unlock(&root->fs_info->alloc_mutex);
  540. ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
  541. if (ret) {
  542. BUG();
  543. break;
  544. }
  545. mutex_unlock(&root->fs_info->drop_mutex);
  546. spin_lock(&root->list_lock);
  547. list_del_init(&dirty->root->dead_list);
  548. if (!list_empty(&root->dead_list)) {
  549. struct btrfs_root *oldest;
  550. oldest = list_entry(root->dead_list.prev,
  551. struct btrfs_root, dead_list);
  552. max_useless = oldest->root_key.offset - 1;
  553. } else {
  554. max_useless = root->root_key.offset - 1;
  555. }
  556. spin_unlock(&root->list_lock);
  557. nr = trans->blocks_used;
  558. ret = btrfs_end_transaction(trans, tree_root);
  559. BUG_ON(ret);
  560. ret = btrfs_remove_leaf_refs(root, max_useless);
  561. BUG_ON(ret);
  562. free_extent_buffer(dirty->root->node);
  563. kfree(dirty->root);
  564. kfree(dirty);
  565. btrfs_btree_balance_dirty(tree_root, nr);
  566. cond_resched();
  567. }
  568. return ret;
  569. }
  570. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  571. struct btrfs_fs_info *fs_info,
  572. struct btrfs_pending_snapshot *pending)
  573. {
  574. struct btrfs_key key;
  575. struct btrfs_root_item *new_root_item;
  576. struct btrfs_root *tree_root = fs_info->tree_root;
  577. struct btrfs_root *root = pending->root;
  578. struct extent_buffer *tmp;
  579. struct extent_buffer *old;
  580. int ret;
  581. int namelen;
  582. u64 objectid;
  583. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  584. if (!new_root_item) {
  585. ret = -ENOMEM;
  586. goto fail;
  587. }
  588. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  589. if (ret)
  590. goto fail;
  591. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  592. key.objectid = objectid;
  593. key.offset = 1;
  594. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  595. old = btrfs_lock_root_node(root);
  596. btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
  597. btrfs_copy_root(trans, root, old, &tmp, objectid);
  598. btrfs_tree_unlock(old);
  599. free_extent_buffer(old);
  600. btrfs_set_root_bytenr(new_root_item, tmp->start);
  601. btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
  602. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  603. new_root_item);
  604. btrfs_tree_unlock(tmp);
  605. free_extent_buffer(tmp);
  606. if (ret)
  607. goto fail;
  608. /*
  609. * insert the directory item
  610. */
  611. key.offset = (u64)-1;
  612. namelen = strlen(pending->name);
  613. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  614. pending->name, namelen,
  615. root->fs_info->sb->s_root->d_inode->i_ino,
  616. &key, BTRFS_FT_DIR, 0);
  617. if (ret)
  618. goto fail;
  619. ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
  620. pending->name, strlen(pending->name), objectid,
  621. root->fs_info->sb->s_root->d_inode->i_ino, 0);
  622. /* Invalidate existing dcache entry for new snapshot. */
  623. btrfs_invalidate_dcache_root(root, pending->name, namelen);
  624. fail:
  625. kfree(new_root_item);
  626. return ret;
  627. }
  628. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  629. struct btrfs_fs_info *fs_info)
  630. {
  631. struct btrfs_pending_snapshot *pending;
  632. struct list_head *head = &trans->transaction->pending_snapshots;
  633. int ret;
  634. while(!list_empty(head)) {
  635. pending = list_entry(head->next,
  636. struct btrfs_pending_snapshot, list);
  637. ret = create_pending_snapshot(trans, fs_info, pending);
  638. BUG_ON(ret);
  639. list_del(&pending->list);
  640. kfree(pending->name);
  641. kfree(pending);
  642. }
  643. return 0;
  644. }
  645. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  646. struct btrfs_root *root)
  647. {
  648. unsigned long joined = 0;
  649. unsigned long timeout = 1;
  650. struct btrfs_transaction *cur_trans;
  651. struct btrfs_transaction *prev_trans = NULL;
  652. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  653. struct list_head dirty_fs_roots;
  654. struct extent_io_tree *pinned_copy;
  655. DEFINE_WAIT(wait);
  656. int ret;
  657. INIT_LIST_HEAD(&dirty_fs_roots);
  658. mutex_lock(&root->fs_info->trans_mutex);
  659. if (trans->transaction->in_commit) {
  660. cur_trans = trans->transaction;
  661. trans->transaction->use_count++;
  662. mutex_unlock(&root->fs_info->trans_mutex);
  663. btrfs_end_transaction(trans, root);
  664. ret = wait_for_commit(root, cur_trans);
  665. BUG_ON(ret);
  666. mutex_lock(&root->fs_info->trans_mutex);
  667. put_transaction(cur_trans);
  668. mutex_unlock(&root->fs_info->trans_mutex);
  669. return 0;
  670. }
  671. pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
  672. if (!pinned_copy)
  673. return -ENOMEM;
  674. extent_io_tree_init(pinned_copy,
  675. root->fs_info->btree_inode->i_mapping, GFP_NOFS);
  676. trans->transaction->in_commit = 1;
  677. trans->transaction->blocked = 1;
  678. cur_trans = trans->transaction;
  679. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  680. prev_trans = list_entry(cur_trans->list.prev,
  681. struct btrfs_transaction, list);
  682. if (!prev_trans->commit_done) {
  683. prev_trans->use_count++;
  684. mutex_unlock(&root->fs_info->trans_mutex);
  685. wait_for_commit(root, prev_trans);
  686. mutex_lock(&root->fs_info->trans_mutex);
  687. put_transaction(prev_trans);
  688. }
  689. }
  690. do {
  691. int snap_pending = 0;
  692. joined = cur_trans->num_joined;
  693. if (!list_empty(&trans->transaction->pending_snapshots))
  694. snap_pending = 1;
  695. WARN_ON(cur_trans != trans->transaction);
  696. prepare_to_wait(&cur_trans->writer_wait, &wait,
  697. TASK_UNINTERRUPTIBLE);
  698. if (cur_trans->num_writers > 1)
  699. timeout = MAX_SCHEDULE_TIMEOUT;
  700. else
  701. timeout = 1;
  702. mutex_unlock(&root->fs_info->trans_mutex);
  703. if (snap_pending) {
  704. ret = btrfs_wait_ordered_extents(root, 1);
  705. BUG_ON(ret);
  706. }
  707. schedule_timeout(timeout);
  708. mutex_lock(&root->fs_info->trans_mutex);
  709. finish_wait(&cur_trans->writer_wait, &wait);
  710. } while (cur_trans->num_writers > 1 ||
  711. (cur_trans->num_joined != joined));
  712. ret = create_pending_snapshots(trans, root->fs_info);
  713. BUG_ON(ret);
  714. WARN_ON(cur_trans != trans->transaction);
  715. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  716. &dirty_fs_roots);
  717. BUG_ON(ret);
  718. ret = btrfs_commit_tree_roots(trans, root);
  719. BUG_ON(ret);
  720. cur_trans = root->fs_info->running_transaction;
  721. spin_lock(&root->fs_info->new_trans_lock);
  722. root->fs_info->running_transaction = NULL;
  723. spin_unlock(&root->fs_info->new_trans_lock);
  724. btrfs_set_super_generation(&root->fs_info->super_copy,
  725. cur_trans->transid);
  726. btrfs_set_super_root(&root->fs_info->super_copy,
  727. root->fs_info->tree_root->node->start);
  728. btrfs_set_super_root_level(&root->fs_info->super_copy,
  729. btrfs_header_level(root->fs_info->tree_root->node));
  730. btrfs_set_super_chunk_root(&root->fs_info->super_copy,
  731. chunk_root->node->start);
  732. btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
  733. btrfs_header_level(chunk_root->node));
  734. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  735. sizeof(root->fs_info->super_copy));
  736. btrfs_copy_pinned(root, pinned_copy);
  737. trans->transaction->blocked = 0;
  738. wake_up(&root->fs_info->transaction_throttle);
  739. wake_up(&root->fs_info->transaction_wait);
  740. mutex_unlock(&root->fs_info->trans_mutex);
  741. ret = btrfs_write_and_wait_transaction(trans, root);
  742. BUG_ON(ret);
  743. write_ctree_super(trans, root);
  744. btrfs_finish_extent_commit(trans, root, pinned_copy);
  745. mutex_lock(&root->fs_info->trans_mutex);
  746. kfree(pinned_copy);
  747. cur_trans->commit_done = 1;
  748. root->fs_info->last_trans_committed = cur_trans->transid;
  749. wake_up(&cur_trans->commit_wait);
  750. put_transaction(cur_trans);
  751. put_transaction(cur_trans);
  752. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  753. if (root->fs_info->closing)
  754. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  755. mutex_unlock(&root->fs_info->trans_mutex);
  756. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  757. if (root->fs_info->closing) {
  758. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  759. }
  760. return ret;
  761. }
  762. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  763. {
  764. struct list_head dirty_roots;
  765. INIT_LIST_HEAD(&dirty_roots);
  766. again:
  767. mutex_lock(&root->fs_info->trans_mutex);
  768. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  769. mutex_unlock(&root->fs_info->trans_mutex);
  770. if (!list_empty(&dirty_roots)) {
  771. drop_dirty_roots(root, &dirty_roots);
  772. goto again;
  773. }
  774. return 0;
  775. }