transaction.c 29 KB

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