transaction.c 30 KB

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