transaction.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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;
  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;
  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, 0, &start, &end,
  282. EXTENT_DIRTY);
  283. if (ret)
  284. break;
  285. clear_extent_dirty(dirty_pages, start, end, GFP_NOFS);
  286. while(start <= end) {
  287. index = start >> PAGE_CACHE_SHIFT;
  288. start = (u64)(index + 1) << PAGE_CACHE_SHIFT;
  289. page = find_lock_page(btree_inode->i_mapping, index);
  290. if (!page)
  291. continue;
  292. if (PageWriteback(page)) {
  293. if (PageDirty(page))
  294. wait_on_page_writeback(page);
  295. else {
  296. unlock_page(page);
  297. page_cache_release(page);
  298. continue;
  299. }
  300. }
  301. err = write_one_page(page, 0);
  302. if (err)
  303. werr = err;
  304. page_cache_release(page);
  305. }
  306. }
  307. err = filemap_fdatawait(btree_inode->i_mapping);
  308. if (err)
  309. werr = err;
  310. return werr;
  311. }
  312. static int update_cowonly_root(struct btrfs_trans_handle *trans,
  313. struct btrfs_root *root)
  314. {
  315. int ret;
  316. u64 old_root_bytenr;
  317. struct btrfs_root *tree_root = root->fs_info->tree_root;
  318. btrfs_write_dirty_block_groups(trans, root);
  319. while(1) {
  320. old_root_bytenr = btrfs_root_bytenr(&root->root_item);
  321. if (old_root_bytenr == root->node->start)
  322. break;
  323. btrfs_set_root_bytenr(&root->root_item,
  324. root->node->start);
  325. btrfs_set_root_level(&root->root_item,
  326. btrfs_header_level(root->node));
  327. ret = btrfs_update_root(trans, tree_root,
  328. &root->root_key,
  329. &root->root_item);
  330. BUG_ON(ret);
  331. btrfs_write_dirty_block_groups(trans, root);
  332. }
  333. return 0;
  334. }
  335. int btrfs_commit_tree_roots(struct btrfs_trans_handle *trans,
  336. struct btrfs_root *root)
  337. {
  338. struct btrfs_fs_info *fs_info = root->fs_info;
  339. struct list_head *next;
  340. while(!list_empty(&fs_info->dirty_cowonly_roots)) {
  341. next = fs_info->dirty_cowonly_roots.next;
  342. list_del_init(next);
  343. root = list_entry(next, struct btrfs_root, dirty_list);
  344. update_cowonly_root(trans, root);
  345. }
  346. return 0;
  347. }
  348. int btrfs_add_dead_root(struct btrfs_root *root, struct btrfs_root *latest)
  349. {
  350. struct btrfs_dirty_root *dirty;
  351. dirty = kmalloc(sizeof(*dirty), GFP_NOFS);
  352. if (!dirty)
  353. return -ENOMEM;
  354. dirty->root = root;
  355. dirty->latest_root = latest;
  356. mutex_lock(&root->fs_info->trans_mutex);
  357. list_add(&dirty->list, &latest->fs_info->dead_roots);
  358. mutex_unlock(&root->fs_info->trans_mutex);
  359. return 0;
  360. }
  361. static noinline int add_dirty_roots(struct btrfs_trans_handle *trans,
  362. struct radix_tree_root *radix,
  363. struct list_head *list)
  364. {
  365. struct btrfs_dirty_root *dirty;
  366. struct btrfs_root *gang[8];
  367. struct btrfs_root *root;
  368. int i;
  369. int ret;
  370. int err = 0;
  371. u32 refs;
  372. while(1) {
  373. ret = radix_tree_gang_lookup_tag(radix, (void **)gang, 0,
  374. ARRAY_SIZE(gang),
  375. BTRFS_ROOT_TRANS_TAG);
  376. if (ret == 0)
  377. break;
  378. for (i = 0; i < ret; i++) {
  379. root = gang[i];
  380. radix_tree_tag_clear(radix,
  381. (unsigned long)root->root_key.objectid,
  382. BTRFS_ROOT_TRANS_TAG);
  383. BUG_ON(!root->ref_tree);
  384. dirty = root->dirty_root;
  385. if (root->commit_root == root->node) {
  386. WARN_ON(root->node->start !=
  387. btrfs_root_bytenr(&root->root_item));
  388. free_extent_buffer(root->commit_root);
  389. root->commit_root = NULL;
  390. root->dirty_root = NULL;
  391. spin_lock(&root->list_lock);
  392. list_del_init(&dirty->root->dead_list);
  393. spin_unlock(&root->list_lock);
  394. kfree(dirty->root);
  395. kfree(dirty);
  396. /* make sure to update the root on disk
  397. * so we get any updates to the block used
  398. * counts
  399. */
  400. err = btrfs_update_root(trans,
  401. root->fs_info->tree_root,
  402. &root->root_key,
  403. &root->root_item);
  404. continue;
  405. }
  406. memset(&root->root_item.drop_progress, 0,
  407. sizeof(struct btrfs_disk_key));
  408. root->root_item.drop_level = 0;
  409. root->commit_root = NULL;
  410. root->dirty_root = NULL;
  411. root->root_key.offset = root->fs_info->generation;
  412. btrfs_set_root_bytenr(&root->root_item,
  413. root->node->start);
  414. btrfs_set_root_level(&root->root_item,
  415. btrfs_header_level(root->node));
  416. err = btrfs_insert_root(trans, root->fs_info->tree_root,
  417. &root->root_key,
  418. &root->root_item);
  419. if (err)
  420. break;
  421. refs = btrfs_root_refs(&dirty->root->root_item);
  422. btrfs_set_root_refs(&dirty->root->root_item, refs - 1);
  423. err = btrfs_update_root(trans, root->fs_info->tree_root,
  424. &dirty->root->root_key,
  425. &dirty->root->root_item);
  426. BUG_ON(err);
  427. if (refs == 1) {
  428. list_add(&dirty->list, list);
  429. } else {
  430. WARN_ON(1);
  431. free_extent_buffer(dirty->root->node);
  432. kfree(dirty->root);
  433. kfree(dirty);
  434. }
  435. }
  436. }
  437. return err;
  438. }
  439. int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
  440. {
  441. struct btrfs_fs_info *info = root->fs_info;
  442. int ret;
  443. struct btrfs_trans_handle *trans;
  444. unsigned long nr;
  445. smp_mb();
  446. if (root->defrag_running)
  447. return 0;
  448. trans = btrfs_start_transaction(root, 1);
  449. while (1) {
  450. root->defrag_running = 1;
  451. ret = btrfs_defrag_leaves(trans, root, cacheonly);
  452. nr = trans->blocks_used;
  453. btrfs_end_transaction(trans, root);
  454. btrfs_btree_balance_dirty(info->tree_root, nr);
  455. cond_resched();
  456. trans = btrfs_start_transaction(root, 1);
  457. if (root->fs_info->closing || ret != -EAGAIN)
  458. break;
  459. }
  460. root->defrag_running = 0;
  461. smp_mb();
  462. btrfs_end_transaction(trans, root);
  463. return 0;
  464. }
  465. static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
  466. struct list_head *list)
  467. {
  468. struct btrfs_dirty_root *dirty;
  469. struct btrfs_trans_handle *trans;
  470. unsigned long nr;
  471. u64 num_bytes;
  472. u64 bytes_used;
  473. u64 max_useless;
  474. int ret = 0;
  475. int err;
  476. while(!list_empty(list)) {
  477. struct btrfs_root *root;
  478. dirty = list_entry(list->prev, struct btrfs_dirty_root, list);
  479. list_del_init(&dirty->list);
  480. num_bytes = btrfs_root_used(&dirty->root->root_item);
  481. root = dirty->latest_root;
  482. atomic_inc(&root->fs_info->throttles);
  483. mutex_lock(&root->fs_info->drop_mutex);
  484. while(1) {
  485. trans = btrfs_start_transaction(tree_root, 1);
  486. ret = btrfs_drop_snapshot(trans, dirty->root);
  487. if (ret != -EAGAIN) {
  488. break;
  489. }
  490. err = btrfs_update_root(trans,
  491. tree_root,
  492. &dirty->root->root_key,
  493. &dirty->root->root_item);
  494. if (err)
  495. ret = err;
  496. nr = trans->blocks_used;
  497. ret = btrfs_end_transaction(trans, tree_root);
  498. BUG_ON(ret);
  499. mutex_unlock(&root->fs_info->drop_mutex);
  500. btrfs_btree_balance_dirty(tree_root, nr);
  501. cond_resched();
  502. mutex_lock(&root->fs_info->drop_mutex);
  503. }
  504. BUG_ON(ret);
  505. atomic_dec(&root->fs_info->throttles);
  506. wake_up(&root->fs_info->transaction_throttle);
  507. mutex_lock(&root->fs_info->alloc_mutex);
  508. num_bytes -= btrfs_root_used(&dirty->root->root_item);
  509. bytes_used = btrfs_root_used(&root->root_item);
  510. if (num_bytes) {
  511. record_root_in_trans(root);
  512. btrfs_set_root_used(&root->root_item,
  513. bytes_used - num_bytes);
  514. }
  515. mutex_unlock(&root->fs_info->alloc_mutex);
  516. ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
  517. if (ret) {
  518. BUG();
  519. break;
  520. }
  521. mutex_unlock(&root->fs_info->drop_mutex);
  522. spin_lock(&root->list_lock);
  523. list_del_init(&dirty->root->dead_list);
  524. if (!list_empty(&root->dead_list)) {
  525. struct btrfs_root *oldest;
  526. oldest = list_entry(root->dead_list.prev,
  527. struct btrfs_root, dead_list);
  528. max_useless = oldest->root_key.offset - 1;
  529. } else {
  530. max_useless = root->root_key.offset - 1;
  531. }
  532. spin_unlock(&root->list_lock);
  533. nr = trans->blocks_used;
  534. ret = btrfs_end_transaction(trans, tree_root);
  535. BUG_ON(ret);
  536. ret = btrfs_remove_leaf_refs(root, max_useless);
  537. BUG_ON(ret);
  538. free_extent_buffer(dirty->root->node);
  539. kfree(dirty->root);
  540. kfree(dirty);
  541. btrfs_btree_balance_dirty(tree_root, nr);
  542. cond_resched();
  543. }
  544. return ret;
  545. }
  546. static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
  547. struct btrfs_fs_info *fs_info,
  548. struct btrfs_pending_snapshot *pending)
  549. {
  550. struct btrfs_key key;
  551. struct btrfs_root_item *new_root_item;
  552. struct btrfs_root *tree_root = fs_info->tree_root;
  553. struct btrfs_root *root = pending->root;
  554. struct extent_buffer *tmp;
  555. struct extent_buffer *old;
  556. int ret;
  557. int namelen;
  558. u64 objectid;
  559. new_root_item = kmalloc(sizeof(*new_root_item), GFP_NOFS);
  560. if (!new_root_item) {
  561. ret = -ENOMEM;
  562. goto fail;
  563. }
  564. ret = btrfs_find_free_objectid(trans, tree_root, 0, &objectid);
  565. if (ret)
  566. goto fail;
  567. memcpy(new_root_item, &root->root_item, sizeof(*new_root_item));
  568. key.objectid = objectid;
  569. key.offset = 1;
  570. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  571. old = btrfs_lock_root_node(root);
  572. btrfs_cow_block(trans, root, old, NULL, 0, &old, 0);
  573. btrfs_copy_root(trans, root, old, &tmp, objectid);
  574. btrfs_tree_unlock(old);
  575. free_extent_buffer(old);
  576. btrfs_set_root_bytenr(new_root_item, tmp->start);
  577. btrfs_set_root_level(new_root_item, btrfs_header_level(tmp));
  578. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  579. new_root_item);
  580. btrfs_tree_unlock(tmp);
  581. free_extent_buffer(tmp);
  582. if (ret)
  583. goto fail;
  584. /*
  585. * insert the directory item
  586. */
  587. key.offset = (u64)-1;
  588. namelen = strlen(pending->name);
  589. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  590. pending->name, namelen,
  591. root->fs_info->sb->s_root->d_inode->i_ino,
  592. &key, BTRFS_FT_DIR, 0);
  593. if (ret)
  594. goto fail;
  595. ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
  596. pending->name, strlen(pending->name), objectid,
  597. root->fs_info->sb->s_root->d_inode->i_ino, 0);
  598. /* Invalidate existing dcache entry for new snapshot. */
  599. btrfs_invalidate_dcache_root(root, pending->name, namelen);
  600. fail:
  601. kfree(new_root_item);
  602. return ret;
  603. }
  604. static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
  605. struct btrfs_fs_info *fs_info)
  606. {
  607. struct btrfs_pending_snapshot *pending;
  608. struct list_head *head = &trans->transaction->pending_snapshots;
  609. int ret;
  610. while(!list_empty(head)) {
  611. pending = list_entry(head->next,
  612. struct btrfs_pending_snapshot, list);
  613. ret = create_pending_snapshot(trans, fs_info, pending);
  614. BUG_ON(ret);
  615. list_del(&pending->list);
  616. kfree(pending->name);
  617. kfree(pending);
  618. }
  619. return 0;
  620. }
  621. int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
  622. struct btrfs_root *root)
  623. {
  624. unsigned long joined = 0;
  625. unsigned long timeout = 1;
  626. struct btrfs_transaction *cur_trans;
  627. struct btrfs_transaction *prev_trans = NULL;
  628. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  629. struct list_head dirty_fs_roots;
  630. struct extent_io_tree *pinned_copy;
  631. DEFINE_WAIT(wait);
  632. int ret;
  633. INIT_LIST_HEAD(&dirty_fs_roots);
  634. mutex_lock(&root->fs_info->trans_mutex);
  635. if (trans->transaction->in_commit) {
  636. cur_trans = trans->transaction;
  637. trans->transaction->use_count++;
  638. mutex_unlock(&root->fs_info->trans_mutex);
  639. btrfs_end_transaction(trans, root);
  640. ret = wait_for_commit(root, cur_trans);
  641. BUG_ON(ret);
  642. mutex_lock(&root->fs_info->trans_mutex);
  643. put_transaction(cur_trans);
  644. mutex_unlock(&root->fs_info->trans_mutex);
  645. return 0;
  646. }
  647. pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
  648. if (!pinned_copy)
  649. return -ENOMEM;
  650. extent_io_tree_init(pinned_copy,
  651. root->fs_info->btree_inode->i_mapping, GFP_NOFS);
  652. trans->transaction->in_commit = 1;
  653. trans->transaction->blocked = 1;
  654. cur_trans = trans->transaction;
  655. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  656. prev_trans = list_entry(cur_trans->list.prev,
  657. struct btrfs_transaction, list);
  658. if (!prev_trans->commit_done) {
  659. prev_trans->use_count++;
  660. mutex_unlock(&root->fs_info->trans_mutex);
  661. wait_for_commit(root, prev_trans);
  662. mutex_lock(&root->fs_info->trans_mutex);
  663. put_transaction(prev_trans);
  664. }
  665. }
  666. do {
  667. int snap_pending = 0;
  668. joined = cur_trans->num_joined;
  669. if (!list_empty(&trans->transaction->pending_snapshots))
  670. snap_pending = 1;
  671. WARN_ON(cur_trans != trans->transaction);
  672. prepare_to_wait(&cur_trans->writer_wait, &wait,
  673. TASK_UNINTERRUPTIBLE);
  674. if (cur_trans->num_writers > 1)
  675. timeout = MAX_SCHEDULE_TIMEOUT;
  676. else
  677. timeout = 1;
  678. mutex_unlock(&root->fs_info->trans_mutex);
  679. if (snap_pending) {
  680. ret = btrfs_wait_ordered_extents(root, 1);
  681. BUG_ON(ret);
  682. }
  683. schedule_timeout(timeout);
  684. mutex_lock(&root->fs_info->trans_mutex);
  685. finish_wait(&cur_trans->writer_wait, &wait);
  686. } while (cur_trans->num_writers > 1 ||
  687. (cur_trans->num_joined != joined));
  688. ret = create_pending_snapshots(trans, root->fs_info);
  689. BUG_ON(ret);
  690. WARN_ON(cur_trans != trans->transaction);
  691. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  692. &dirty_fs_roots);
  693. BUG_ON(ret);
  694. ret = btrfs_commit_tree_roots(trans, root);
  695. BUG_ON(ret);
  696. cur_trans = root->fs_info->running_transaction;
  697. spin_lock(&root->fs_info->new_trans_lock);
  698. root->fs_info->running_transaction = NULL;
  699. spin_unlock(&root->fs_info->new_trans_lock);
  700. btrfs_set_super_generation(&root->fs_info->super_copy,
  701. cur_trans->transid);
  702. btrfs_set_super_root(&root->fs_info->super_copy,
  703. root->fs_info->tree_root->node->start);
  704. btrfs_set_super_root_level(&root->fs_info->super_copy,
  705. btrfs_header_level(root->fs_info->tree_root->node));
  706. btrfs_set_super_chunk_root(&root->fs_info->super_copy,
  707. chunk_root->node->start);
  708. btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
  709. btrfs_header_level(chunk_root->node));
  710. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  711. sizeof(root->fs_info->super_copy));
  712. btrfs_copy_pinned(root, pinned_copy);
  713. trans->transaction->blocked = 0;
  714. wake_up(&root->fs_info->transaction_throttle);
  715. wake_up(&root->fs_info->transaction_wait);
  716. mutex_unlock(&root->fs_info->trans_mutex);
  717. ret = btrfs_write_and_wait_transaction(trans, root);
  718. BUG_ON(ret);
  719. write_ctree_super(trans, root);
  720. btrfs_finish_extent_commit(trans, root, pinned_copy);
  721. mutex_lock(&root->fs_info->trans_mutex);
  722. kfree(pinned_copy);
  723. cur_trans->commit_done = 1;
  724. root->fs_info->last_trans_committed = cur_trans->transid;
  725. wake_up(&cur_trans->commit_wait);
  726. put_transaction(cur_trans);
  727. put_transaction(cur_trans);
  728. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  729. if (root->fs_info->closing)
  730. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  731. mutex_unlock(&root->fs_info->trans_mutex);
  732. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  733. if (root->fs_info->closing) {
  734. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  735. }
  736. return ret;
  737. }
  738. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  739. {
  740. struct list_head dirty_roots;
  741. INIT_LIST_HEAD(&dirty_roots);
  742. again:
  743. mutex_lock(&root->fs_info->trans_mutex);
  744. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  745. mutex_unlock(&root->fs_info->trans_mutex);
  746. if (!list_empty(&dirty_roots)) {
  747. drop_dirty_roots(root, &dirty_roots);
  748. goto again;
  749. }
  750. return 0;
  751. }