transaction.c 33 KB

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