transaction.c 30 KB

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