transaction.c 33 KB

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