transaction.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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. /* make a pass through all the delayed refs we have so far
  861. * any runnings procs may add more while we are here
  862. */
  863. ret = btrfs_run_delayed_refs(trans, root, 0);
  864. BUG_ON(ret);
  865. cur_trans = trans->transaction;
  866. /*
  867. * set the flushing flag so procs in this transaction have to
  868. * start sending their work down.
  869. */
  870. cur_trans->delayed_refs.flushing = 1;
  871. ret = btrfs_run_delayed_refs(trans, root, 0);
  872. BUG_ON(ret);
  873. mutex_lock(&root->fs_info->trans_mutex);
  874. INIT_LIST_HEAD(&dirty_fs_roots);
  875. if (cur_trans->in_commit) {
  876. cur_trans->use_count++;
  877. mutex_unlock(&root->fs_info->trans_mutex);
  878. btrfs_end_transaction(trans, root);
  879. ret = wait_for_commit(root, cur_trans);
  880. BUG_ON(ret);
  881. mutex_lock(&root->fs_info->trans_mutex);
  882. put_transaction(cur_trans);
  883. mutex_unlock(&root->fs_info->trans_mutex);
  884. return 0;
  885. }
  886. pinned_copy = kmalloc(sizeof(*pinned_copy), GFP_NOFS);
  887. if (!pinned_copy)
  888. return -ENOMEM;
  889. extent_io_tree_init(pinned_copy,
  890. root->fs_info->btree_inode->i_mapping, GFP_NOFS);
  891. trans->transaction->in_commit = 1;
  892. trans->transaction->blocked = 1;
  893. if (cur_trans->list.prev != &root->fs_info->trans_list) {
  894. prev_trans = list_entry(cur_trans->list.prev,
  895. struct btrfs_transaction, list);
  896. if (!prev_trans->commit_done) {
  897. prev_trans->use_count++;
  898. mutex_unlock(&root->fs_info->trans_mutex);
  899. wait_for_commit(root, prev_trans);
  900. mutex_lock(&root->fs_info->trans_mutex);
  901. put_transaction(prev_trans);
  902. }
  903. }
  904. if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
  905. should_grow = 1;
  906. do {
  907. int snap_pending = 0;
  908. joined = cur_trans->num_joined;
  909. if (!list_empty(&trans->transaction->pending_snapshots))
  910. snap_pending = 1;
  911. WARN_ON(cur_trans != trans->transaction);
  912. prepare_to_wait(&cur_trans->writer_wait, &wait,
  913. TASK_UNINTERRUPTIBLE);
  914. if (cur_trans->num_writers > 1)
  915. timeout = MAX_SCHEDULE_TIMEOUT;
  916. else if (should_grow)
  917. timeout = 1;
  918. mutex_unlock(&root->fs_info->trans_mutex);
  919. if (snap_pending) {
  920. ret = btrfs_wait_ordered_extents(root, 1);
  921. BUG_ON(ret);
  922. }
  923. smp_mb();
  924. if (cur_trans->num_writers > 1 || should_grow)
  925. schedule_timeout(timeout);
  926. mutex_lock(&root->fs_info->trans_mutex);
  927. finish_wait(&cur_trans->writer_wait, &wait);
  928. } while (cur_trans->num_writers > 1 ||
  929. (should_grow && cur_trans->num_joined != joined));
  930. ret = create_pending_snapshots(trans, root->fs_info);
  931. BUG_ON(ret);
  932. ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
  933. BUG_ON(ret);
  934. WARN_ON(cur_trans != trans->transaction);
  935. /* btrfs_commit_tree_roots is responsible for getting the
  936. * various roots consistent with each other. Every pointer
  937. * in the tree of tree roots has to point to the most up to date
  938. * root for every subvolume and other tree. So, we have to keep
  939. * the tree logging code from jumping in and changing any
  940. * of the trees.
  941. *
  942. * At this point in the commit, there can't be any tree-log
  943. * writers, but a little lower down we drop the trans mutex
  944. * and let new people in. By holding the tree_log_mutex
  945. * from now until after the super is written, we avoid races
  946. * with the tree-log code.
  947. */
  948. mutex_lock(&root->fs_info->tree_log_mutex);
  949. /*
  950. * keep tree reloc code from adding new reloc trees
  951. */
  952. mutex_lock(&root->fs_info->tree_reloc_mutex);
  953. ret = add_dirty_roots(trans, &root->fs_info->fs_roots_radix,
  954. &dirty_fs_roots);
  955. BUG_ON(ret);
  956. /* add_dirty_roots gets rid of all the tree log roots, it is now
  957. * safe to free the root of tree log roots
  958. */
  959. btrfs_free_log_root_tree(trans, root->fs_info);
  960. ret = btrfs_commit_tree_roots(trans, root);
  961. BUG_ON(ret);
  962. cur_trans = root->fs_info->running_transaction;
  963. spin_lock(&root->fs_info->new_trans_lock);
  964. root->fs_info->running_transaction = NULL;
  965. spin_unlock(&root->fs_info->new_trans_lock);
  966. btrfs_set_super_generation(&root->fs_info->super_copy,
  967. cur_trans->transid);
  968. btrfs_set_super_root(&root->fs_info->super_copy,
  969. root->fs_info->tree_root->node->start);
  970. btrfs_set_super_root_level(&root->fs_info->super_copy,
  971. btrfs_header_level(root->fs_info->tree_root->node));
  972. btrfs_set_super_chunk_root(&root->fs_info->super_copy,
  973. chunk_root->node->start);
  974. btrfs_set_super_chunk_root_level(&root->fs_info->super_copy,
  975. btrfs_header_level(chunk_root->node));
  976. btrfs_set_super_chunk_root_generation(&root->fs_info->super_copy,
  977. btrfs_header_generation(chunk_root->node));
  978. if (!root->fs_info->log_root_recovering) {
  979. btrfs_set_super_log_root(&root->fs_info->super_copy, 0);
  980. btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0);
  981. }
  982. memcpy(&root->fs_info->super_for_commit, &root->fs_info->super_copy,
  983. sizeof(root->fs_info->super_copy));
  984. btrfs_copy_pinned(root, pinned_copy);
  985. trans->transaction->blocked = 0;
  986. wake_up(&root->fs_info->transaction_throttle);
  987. wake_up(&root->fs_info->transaction_wait);
  988. mutex_unlock(&root->fs_info->trans_mutex);
  989. ret = btrfs_write_and_wait_transaction(trans, root);
  990. BUG_ON(ret);
  991. write_ctree_super(trans, root, 0);
  992. /*
  993. * the super is written, we can safely allow the tree-loggers
  994. * to go about their business
  995. */
  996. mutex_unlock(&root->fs_info->tree_log_mutex);
  997. btrfs_finish_extent_commit(trans, root, pinned_copy);
  998. kfree(pinned_copy);
  999. btrfs_drop_dead_reloc_roots(root);
  1000. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  1001. /* do the directory inserts of any pending snapshot creations */
  1002. finish_pending_snapshots(trans, root->fs_info);
  1003. mutex_lock(&root->fs_info->trans_mutex);
  1004. cur_trans->commit_done = 1;
  1005. root->fs_info->last_trans_committed = cur_trans->transid;
  1006. wake_up(&cur_trans->commit_wait);
  1007. put_transaction(cur_trans);
  1008. put_transaction(cur_trans);
  1009. list_splice_init(&dirty_fs_roots, &root->fs_info->dead_roots);
  1010. if (root->fs_info->closing)
  1011. list_splice_init(&root->fs_info->dead_roots, &dirty_fs_roots);
  1012. mutex_unlock(&root->fs_info->trans_mutex);
  1013. kmem_cache_free(btrfs_trans_handle_cachep, trans);
  1014. if (root->fs_info->closing)
  1015. drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
  1016. return ret;
  1017. }
  1018. /*
  1019. * interface function to delete all the snapshots we have scheduled for deletion
  1020. */
  1021. int btrfs_clean_old_snapshots(struct btrfs_root *root)
  1022. {
  1023. struct list_head dirty_roots;
  1024. INIT_LIST_HEAD(&dirty_roots);
  1025. again:
  1026. mutex_lock(&root->fs_info->trans_mutex);
  1027. list_splice_init(&root->fs_info->dead_roots, &dirty_roots);
  1028. mutex_unlock(&root->fs_info->trans_mutex);
  1029. if (!list_empty(&dirty_roots)) {
  1030. drop_dirty_roots(root, &dirty_roots);
  1031. goto again;
  1032. }
  1033. return 0;
  1034. }