transaction.c 25 KB

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